Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Feb 13 16:37:42 2015 +0000
Revision:
286:898ff71b9502
Parent:
285:984353e66610
Child:
287:cca148e535b1
Synchronized with git rev 8fe57958
Author: Austin Blackstone
added API for creating iBeacons

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 171:6092e61690dc 1 /* mbed Microcontroller Library
rgrover1 171:6092e61690dc 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 171:6092e61690dc 3 *
rgrover1 171:6092e61690dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 171:6092e61690dc 5 * you may not use this file except in compliance with the License.
rgrover1 171:6092e61690dc 6 * You may obtain a copy of the License at
rgrover1 171:6092e61690dc 7 *
rgrover1 171:6092e61690dc 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 171:6092e61690dc 9 *
rgrover1 171:6092e61690dc 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 171:6092e61690dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 171:6092e61690dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 171:6092e61690dc 13 * See the License for the specific language governing permissions and
rgrover1 171:6092e61690dc 14 * limitations under the License.
rgrover1 171:6092e61690dc 15 */
rgrover1 171:6092e61690dc 16
rgrover1 286:898ff71b9502 17 #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__
rgrover1 286:898ff71b9502 18 #define __BLE_URI_BEACON_CONFIG_SERVICE_H__
rgrover1 171:6092e61690dc 19
rgrover1 286:898ff71b9502 20 #include "BLEDevice.h"
rgrover1 286:898ff71b9502 21
rgrover1 286:898ff71b9502 22 #define URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(FIRST, SECOND) { \
rgrover1 225:f6cbfd817d16 23 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \
rgrover1 225:f6cbfd817d16 24 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \
rgrover1 171:6092e61690dc 25 }
rgrover1 286:898ff71b9502 26 static const uint8_t URIBeacon2ControlServiceUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x80);
rgrover1 286:898ff71b9502 27 static const uint8_t lockedStateCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x81);
rgrover1 286:898ff71b9502 28 static const uint8_t lockCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x82);
rgrover1 286:898ff71b9502 29 static const uint8_t unlockCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x83);
rgrover1 286:898ff71b9502 30 static const uint8_t uriDataCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x84);
rgrover1 286:898ff71b9502 31 static const uint8_t flagsCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x85);
rgrover1 286:898ff71b9502 32 static const uint8_t txPowerLevelsCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x86);
rgrover1 286:898ff71b9502 33 static const uint8_t txPowerModeCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x87);
rgrover1 286:898ff71b9502 34 static const uint8_t beaconPeriodCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x88);
rgrover1 286:898ff71b9502 35 static const uint8_t resetCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x89);
rgrover1 171:6092e61690dc 36
mbedAustin 231:1c4a4fd961a5 37 /**
mbedAustin 231:1c4a4fd961a5 38 * @class URIBeaconConfigService
rgrover1 242:0e9201b67e2f 39 * @brief UriBeacon Configuration Service. Can be used to set URL, adjust power levels, and set flags.
mbedAustin 231:1c4a4fd961a5 40 */
rgrover1 207:e88130dc254c 41 class URIBeaconConfigService {
rgrover1 286:898ff71b9502 42 public:
mbedAustin 231:1c4a4fd961a5 43 /**
rgrover1 286:898ff71b9502 44 * @enum TXPowerModes_t
rgrover1 242:0e9201b67e2f 45 * @brief Transmission Power Modes for UriBeacon
rgrover1 242:0e9201b67e2f 46 */
rgrover1 286:898ff71b9502 47 enum TXPowerModes_t {
rgrover1 286:898ff71b9502 48 TX_POWER_MODE_LOWEST = 0, /*!< Lowest TX power mode */
rgrover1 286:898ff71b9502 49 TX_POWER_MODE_LOW = 1, /*!< Low TX power mode */
rgrover1 286:898ff71b9502 50 TX_POWER_MODE_MEDIUM = 2, /*!< Medium TX power mode */
rgrover1 286:898ff71b9502 51 TX_POWER_MODE_HIGH = 3, /*!< High TX power mode */
rgrover1 286:898ff71b9502 52 NUM_POWER_MODES /*!< Number of Power Modes defined */
rgrover1 175:4e85f7225f8f 53 };
rgrover1 175:4e85f7225f8f 54
rgrover1 286:898ff71b9502 55 static const size_t SIZEOF_LOCK_BITS = 16; /* uint128 */
rgrover1 286:898ff71b9502 56 typedef uint8_t LockBits_t[SIZEOF_LOCK_BITS];
rgrover1 269:d818d11dca4e 57
rgrover1 181:bbb6ce1082c3 58 /**
rgrover1 286:898ff71b9502 59 * @param[ref] ble
rgrover1 286:898ff71b9502 60 * BLEDevice object for the underlying controller.
rgrover1 286:898ff71b9502 61 * @param[in] uridata
rgrover1 286:898ff71b9502 62 * URI as a null-terminated string.
rgrover1 286:898ff71b9502 63 * @param[in] flagsIn
rgrover1 286:898ff71b9502 64 * UriBeacon Flags.
rgrover1 286:898ff71b9502 65 * @param[in] powerLevels[]
rgrover1 286:898ff71b9502 66 * Table of UriBeacon Tx Power Levels in dBm.
rgrover1 286:898ff71b9502 67 * @param[in] powerMode
rgrover1 286:898ff71b9502 68 * Currently effective power mode.
rgrover1 286:898ff71b9502 69 * @param[in] beaconPeriodIn
rgrover1 286:898ff71b9502 70 * The period in milliseconds that a UriBeacon packet is
rgrover1 286:898ff71b9502 71 * transmitted. A value of zero disables UriBeacon
rgrover1 286:898ff71b9502 72 * transmissions.
rgrover1 181:bbb6ce1082c3 73 */
rgrover1 286:898ff71b9502 74 URIBeaconConfigService(BLEDevice &bleIn,
rgrover1 286:898ff71b9502 75 const char *uriDataIn,
rgrover1 286:898ff71b9502 76 uint8_t flagsIn = 0,
rgrover1 286:898ff71b9502 77 const int8_t powerLevelsIn[NUM_POWER_MODES] = NULL,
rgrover1 286:898ff71b9502 78 TXPowerModes_t powerModeIn = TX_POWER_MODE_LOW,
rgrover1 286:898ff71b9502 79 uint16_t beaconPeriodIn = 1000) :
rgrover1 192:3fd3dcf49005 80 ble(bleIn),
rgrover1 286:898ff71b9502 81 payloadIndex(0),
rgrover1 286:898ff71b9502 82 serviceDataPayload(),
rgrover1 192:3fd3dcf49005 83 initSucceeded(false),
rgrover1 286:898ff71b9502 84 lockedState(false),
rgrover1 286:898ff71b9502 85 lockBits(),
rgrover1 286:898ff71b9502 86 uriDataLength(0),
rgrover1 286:898ff71b9502 87 uriData(),
rgrover1 286:898ff71b9502 88 flags(flagsIn),
rgrover1 286:898ff71b9502 89 powerLevels(),
rgrover1 286:898ff71b9502 90 beaconPeriod(beaconPeriodIn),
rgrover1 286:898ff71b9502 91 lockedStateChar(lockedStateCharUUID, &lockedState),
rgrover1 286:898ff71b9502 92 lockChar(lockCharUUID, lockBits),
rgrover1 286:898ff71b9502 93 unlockChar(unlockCharUUID, lockBits),
rgrover1 286:898ff71b9502 94 uriDataChar(uriDataCharUUID, uriData),
rgrover1 286:898ff71b9502 95 flagsChar(flagsCharUUID, &flags),
rgrover1 286:898ff71b9502 96 txPowerLevelsChar(txPowerLevelsCharUUID, powerLevels),
rgrover1 286:898ff71b9502 97 txPowerModeChar(txPowerModeCharUUID, &txPowerMode),
rgrover1 286:898ff71b9502 98 beaconPeriodChar(beaconPeriodCharUUID, &beaconPeriod),
rgrover1 286:898ff71b9502 99 resetChar(resetCharUUID, &resetFlag)
rgrover1 286:898ff71b9502 100 {
rgrover1 286:898ff71b9502 101 if ((uriDataIn == NULL) || ((uriDataLength = strlen(uriDataIn)) == 0) || (uriDataLength > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
rgrover1 192:3fd3dcf49005 102 return;
rgrover1 192:3fd3dcf49005 103 }
rgrover1 286:898ff71b9502 104 strcpy(reinterpret_cast<char *>(uriData), uriDataIn);
rgrover1 192:3fd3dcf49005 105
rgrover1 286:898ff71b9502 106 if (powerLevelsIn != NULL) {
rgrover1 286:898ff71b9502 107 memcpy(powerLevels, powerLevelsIn, sizeof(powerLevels));
rgrover1 286:898ff71b9502 108 updateTxPowerLevelsCharacteristic();
rgrover1 218:8ae02569fab9 109 }
rgrover1 218:8ae02569fab9 110
rgrover1 286:898ff71b9502 111 configureGAP();
rgrover1 171:6092e61690dc 112
rgrover1 286:898ff71b9502 113 // enable the following for debugging the state of the lock
rgrover1 286:898ff71b9502 114 // lockedStateChar.setReadAuthorizationCallback(this, &URIBeaconConfigService::lockedStateAuthorizationCallback);
rgrover1 274:f540619754bb 115
rgrover1 272:65b9d9c87ed1 116 lockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::lockAuthorizationCallback);
rgrover1 272:65b9d9c87ed1 117 unlockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::unlockAuthorizationCallback);
rgrover1 250:6862d374e613 118 uriDataChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::uriDataWriteAuthorizationCallback);
rgrover1 255:cdb7231f83df 119 flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::flagsAuthorizationCallback);
rgrover1 286:898ff71b9502 120 txPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 286:898ff71b9502 121 txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::powerModeAuthorizationCallback);
rgrover1 250:6862d374e613 122 beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 123 resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 124
rgrover1 286:898ff71b9502 125 GattCharacteristic *charTable[] = {&lockedStateChar,
rgrover1 286:898ff71b9502 126 &lockChar,
rgrover1 286:898ff71b9502 127 &unlockChar,
rgrover1 286:898ff71b9502 128 &uriDataChar,
rgrover1 286:898ff71b9502 129 &flagsChar,
rgrover1 286:898ff71b9502 130 &txPowerLevelsChar,
rgrover1 286:898ff71b9502 131 &beaconPeriodChar,
rgrover1 286:898ff71b9502 132 &resetChar};
rgrover1 286:898ff71b9502 133 GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 286:898ff71b9502 134 ble.addService(beaconControlService);
rgrover1 182:d16f8c11816b 135
rgrover1 286:898ff71b9502 136 ble.onDataWritten(this, &URIBeaconConfigService::onDataWritten);
rgrover1 192:3fd3dcf49005 137 }
rgrover1 192:3fd3dcf49005 138
rgrover1 193:ac2feceb7e87 139 bool configuredSuccessfully(void) const {
rgrover1 192:3fd3dcf49005 140 return initSucceeded;
rgrover1 171:6092e61690dc 141 }
rgrover1 171:6092e61690dc 142
rgrover1 286:898ff71b9502 143 /**
rgrover1 286:898ff71b9502 144 * Please note that the following public APIs are offered to allow modifying
rgrover1 286:898ff71b9502 145 * the service programmatically. It is also possible to do so over BLE GATT
rgrover1 286:898ff71b9502 146 * transactions.
rgrover1 286:898ff71b9502 147 */
rgrover1 286:898ff71b9502 148 public:
rgrover1 286:898ff71b9502 149 /**
rgrover1 286:898ff71b9502 150 * Update flags of the URIBeacon dynamically.
rgrover1 286:898ff71b9502 151 *
rgrover1 286:898ff71b9502 152 * @param[in] flagsIn
rgrover1 286:898ff71b9502 153 * @verbatim
rgrover1 286:898ff71b9502 154 * ### UriBeacon Flags
rgrover1 286:898ff71b9502 155 * Bit | Description
rgrover1 286:898ff71b9502 156 * :---- | :----------
rgrover1 286:898ff71b9502 157 * 0 | Invisible Hint
rgrover1 286:898ff71b9502 158 * 1..7 | Reserved for future use. Must be zero.
rgrover1 286:898ff71b9502 159 * @endverbatim
rgrover1 286:898ff71b9502 160 * The `Invisible Hint` flag is a command for the user-agent that tells
rgrover1 286:898ff71b9502 161 * it not to access or display the UriBeacon. This is a guideline only,
rgrover1 286:898ff71b9502 162 * and is not a blocking method. User agents may, with user approval,
rgrover1 286:898ff71b9502 163 * display invisible beacons.
rgrover1 286:898ff71b9502 164 */
rgrover1 286:898ff71b9502 165 void setFlags(uint8_t flagsIn) {
rgrover1 286:898ff71b9502 166 flags = flagsIn;
rgrover1 286:898ff71b9502 167 configureGAP();
rgrover1 286:898ff71b9502 168 updateFlagsCharacteristic();
rgrover1 171:6092e61690dc 169 }
rgrover1 171:6092e61690dc 170
rgrover1 286:898ff71b9502 171 /**
rgrover1 286:898ff71b9502 172 * @brief Update the txPowerLevels table.
rgrover1 286:898ff71b9502 173 *
rgrover1 286:898ff71b9502 174 * @param[in] powerLevelsIn
rgrover1 286:898ff71b9502 175 * Array of power levels
rgrover1 242:0e9201b67e2f 176 */
rgrover1 286:898ff71b9502 177 void setTxPowerLevels(const int8_t powerLevelsIn[NUM_POWER_MODES]) {
rgrover1 286:898ff71b9502 178 memcpy(powerLevels, powerLevelsIn, sizeof(powerLevels));
rgrover1 286:898ff71b9502 179 configureGAP();
rgrover1 286:898ff71b9502 180 updateTxPowerLevelsCharacteristic();
rgrover1 286:898ff71b9502 181 }
rgrover1 278:8a935a2d4a16 182
rgrover1 286:898ff71b9502 183 /**
rgrover1 286:898ff71b9502 184 * @brief Set the effective power mode from one of the values in the powerLevels tables.
rgrover1 286:898ff71b9502 185 *
rgrover1 286:898ff71b9502 186 * @param[in] mode
rgrover1 286:898ff71b9502 187 * Set the TX Power Mode.
rgrover1 286:898ff71b9502 188 */
rgrover1 286:898ff71b9502 189 void setTxPowerMode(TXPowerModes_t mode) {
rgrover1 286:898ff71b9502 190 txPowerMode = mode;
rgrover1 286:898ff71b9502 191 configureGAP();
rgrover1 286:898ff71b9502 192 updateTxPowerModeCharacteristic();
rgrover1 286:898ff71b9502 193 }
rgrover1 286:898ff71b9502 194
rgrover1 286:898ff71b9502 195 /**
rgrover1 286:898ff71b9502 196 * The period in milliseconds that a UriBeacon packet is transmitted.
rgrover1 286:898ff71b9502 197 *
rgrover1 286:898ff71b9502 198 * @note A value of zero disables UriBeacon transmissions.
rgrover1 286:898ff71b9502 199 *
rgrover1 286:898ff71b9502 200 * @param beaconPeriodIn
rgrover1 286:898ff71b9502 201 * Beacon advertising period in milliseconds
rgrover1 286:898ff71b9502 202 */
rgrover1 286:898ff71b9502 203 void setBeaconPeriod(uint16_t beaconPeriodIn) {
rgrover1 286:898ff71b9502 204 beaconPeriod = beaconPeriodIn;
rgrover1 286:898ff71b9502 205 configureGAP();
rgrover1 286:898ff71b9502 206 updateBeaconPeriodCharacteristic();
rgrover1 286:898ff71b9502 207 }
rgrover1 286:898ff71b9502 208
rgrover1 286:898ff71b9502 209 protected:
rgrover1 286:898ff71b9502 210 void updateLockBits(const LockBits_t lockBitsIn) {
rgrover1 286:898ff71b9502 211 static const uint8_t allZeroes[SIZEOF_LOCK_BITS] = {0, 0, 0, 0, 0, 0, 0, 0,
rgrover1 286:898ff71b9502 212 0, 0, 0, 0, 0, 0, 0, 0};
rgrover1 286:898ff71b9502 213
rgrover1 286:898ff71b9502 214 memcpy(lockBits, lockBitsIn, SIZEOF_LOCK_BITS);
rgrover1 286:898ff71b9502 215 if (memcmp(lockBits, allZeroes, SIZEOF_LOCK_BITS)) {
rgrover1 286:898ff71b9502 216 lockedState = true;
rgrover1 185:7cd70497aec8 217 }
rgrover1 185:7cd70497aec8 218 }
rgrover1 185:7cd70497aec8 219
rgrover1 286:898ff71b9502 220 void copyLockBitsInto(LockBits_t lockBitsOut) const {
rgrover1 286:898ff71b9502 221 memcpy(lockBitsOut, lockBits, SIZEOF_LOCK_BITS);
rgrover1 286:898ff71b9502 222 }
rgrover1 286:898ff71b9502 223
rgrover1 286:898ff71b9502 224 void resetLockBits(void) {
rgrover1 286:898ff71b9502 225 lockedState = false;
rgrover1 286:898ff71b9502 226 memset(lockBits, 0, SIZEOF_LOCK_BITS);
rgrover1 286:898ff71b9502 227 storage_saveLockBits();
rgrover1 286:898ff71b9502 228 }
rgrover1 286:898ff71b9502 229
rgrover1 286:898ff71b9502 230 /**
rgrover1 286:898ff71b9502 231 * APIs around making lockBits persistent.
rgrover1 286:898ff71b9502 232 */
rgrover1 286:898ff71b9502 233 private:
rgrover1 286:898ff71b9502 234 /**
rgrover1 286:898ff71b9502 235 * Have we previously saved lockedBits? Once set, this state is expected to persist.
rgrover1 286:898ff71b9502 236 * @return true if we've previously saved locked bits.
rgrover1 286:898ff71b9502 237 */
rgrover1 286:898ff71b9502 238 virtual bool storage_haveSavedLockBits() const {
rgrover1 286:898ff71b9502 239 /* Expecting to be overridden. Left empty to allow the default URIBeacon to be instantiated if persistence isn't required. */
rgrover1 286:898ff71b9502 240 return false;
rgrover1 286:898ff71b9502 241 }
rgrover1 286:898ff71b9502 242
rgrover1 286:898ff71b9502 243 /**
rgrover1 286:898ff71b9502 244 * Save the current value of lockBits into persistent storage; this value is then retrievable by lockLockBits() until a subsequent call to saveLockBits().
rgrover1 242:0e9201b67e2f 245 */
rgrover1 286:898ff71b9502 246 virtual void storage_saveLockBits() {
rgrover1 286:898ff71b9502 247 /* Expecting to be overridden. Left empty to allow the default URIBeacon to be instantiated if persistence isn't required. */
rgrover1 286:898ff71b9502 248 }
rgrover1 286:898ff71b9502 249
rgrover1 286:898ff71b9502 250 /**
rgrover1 286:898ff71b9502 251 * Retrieve the saved lockBits from persistent storage and update the class member 'lockBits'.
rgrover1 286:898ff71b9502 252 */
rgrover1 286:898ff71b9502 253 virtual void storage_loadLockBits() {
rgrover1 286:898ff71b9502 254 /* Expecting to be overridden. Left empty to allow the default URIBeacon to be instantiated if persistence isn't required. */
rgrover1 286:898ff71b9502 255 }
rgrover1 286:898ff71b9502 256
rgrover1 286:898ff71b9502 257 private:
rgrover1 286:898ff71b9502 258 /*
rgrover1 286:898ff71b9502 259 * Setup the advertisement payload and GAP settings.
rgrover1 286:898ff71b9502 260 */
rgrover1 286:898ff71b9502 261 void configureGAP(void) {
rgrover1 286:898ff71b9502 262 const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
rgrover1 286:898ff71b9502 263
rgrover1 286:898ff71b9502 264 payloadIndex = 0;
rgrover1 286:898ff71b9502 265 serviceDataPayload[payloadIndex++] = BEACON_UUID[0];
rgrover1 286:898ff71b9502 266 serviceDataPayload[payloadIndex++] = BEACON_UUID[1];
rgrover1 286:898ff71b9502 267 serviceDataPayload[payloadIndex++] = flags;
rgrover1 286:898ff71b9502 268 serviceDataPayload[payloadIndex++] = powerLevels[txPowerMode];
rgrover1 286:898ff71b9502 269
rgrover1 286:898ff71b9502 270 const char *urlData = reinterpret_cast<char *>(uriData);
rgrover1 286:898ff71b9502 271 size_t sizeofURLData = uriDataLength;
rgrover1 286:898ff71b9502 272 size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData);
rgrover1 286:898ff71b9502 273
rgrover1 286:898ff71b9502 274 ble.clearAdvertisingPayload();
rgrover1 286:898ff71b9502 275 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
rgrover1 286:898ff71b9502 276 ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4);
rgrover1 286:898ff71b9502 277
rgrover1 286:898ff71b9502 278 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriod));
rgrover1 286:898ff71b9502 279 ble.setTxPower(powerLevels[txPowerMode]);
rgrover1 211:f181effe5de3 280 }
rgrover1 211:f181effe5de3 281
mbedAustin 232:4cfb5b8a4fb9 282 /*
rgrover1 286:898ff71b9502 283 * Encode the URI Prefix to a single byte if possible.
rgrover1 242:0e9201b67e2f 284 */
rgrover1 286:898ff71b9502 285 size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) {
rgrover1 286:898ff71b9502 286 if (!sizeofURLData) {
rgrover1 286:898ff71b9502 287 return 0;
rgrover1 284:82fefb5be8ed 288 }
rgrover1 284:82fefb5be8ed 289
rgrover1 286:898ff71b9502 290 /* These are the URI Prefixes that can be abbreviated.*/
rgrover1 284:82fefb5be8ed 291 const char *prefixes[] = {
rgrover1 284:82fefb5be8ed 292 "http://www.",
rgrover1 284:82fefb5be8ed 293 "https://www.",
rgrover1 284:82fefb5be8ed 294 "http://",
rgrover1 284:82fefb5be8ed 295 "https://",
rgrover1 284:82fefb5be8ed 296 "urn:uuid:"
rgrover1 284:82fefb5be8ed 297 };
rgrover1 286:898ff71b9502 298
rgrover1 286:898ff71b9502 299 size_t encodedBytes = 0;
rgrover1 286:898ff71b9502 300 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
rgrover1 284:82fefb5be8ed 301 for (unsigned i = 0; i < NUM_PREFIXES; i++) {
rgrover1 284:82fefb5be8ed 302 size_t prefixLen = strlen(prefixes[i]);
rgrover1 286:898ff71b9502 303 if (strncmp(urldata, prefixes[i], prefixLen) == 0) {
rgrover1 286:898ff71b9502 304 serviceDataPayload[payloadIndex++] = i;
rgrover1 286:898ff71b9502 305 encodedBytes = 1;
rgrover1 286:898ff71b9502 306
rgrover1 286:898ff71b9502 307 urldata += prefixLen;
rgrover1 286:898ff71b9502 308 sizeofURLData -= prefixLen;
rgrover1 284:82fefb5be8ed 309 break;
rgrover1 284:82fefb5be8ed 310 }
rgrover1 284:82fefb5be8ed 311 }
rgrover1 284:82fefb5be8ed 312
rgrover1 286:898ff71b9502 313 return encodedBytes;
rgrover1 286:898ff71b9502 314 }
rgrover1 286:898ff71b9502 315
rgrover1 286:898ff71b9502 316 /*
rgrover1 286:898ff71b9502 317 * Encode the URI Suffix to a single byte if possible.
rgrover1 286:898ff71b9502 318 */
rgrover1 286:898ff71b9502 319 size_t encodeURI(const char *urldata, size_t sizeofURLData) {
rgrover1 286:898ff71b9502 320 /* These are the URI suffixes that can be abbreviated. */
rgrover1 284:82fefb5be8ed 321 const char *suffixes[] = {
rgrover1 284:82fefb5be8ed 322 ".com/",
rgrover1 284:82fefb5be8ed 323 ".org/",
rgrover1 284:82fefb5be8ed 324 ".edu/",
rgrover1 284:82fefb5be8ed 325 ".net/",
rgrover1 284:82fefb5be8ed 326 ".info/",
rgrover1 284:82fefb5be8ed 327 ".biz/",
rgrover1 284:82fefb5be8ed 328 ".gov/",
rgrover1 284:82fefb5be8ed 329 ".com",
rgrover1 284:82fefb5be8ed 330 ".org",
rgrover1 284:82fefb5be8ed 331 ".edu",
rgrover1 284:82fefb5be8ed 332 ".net",
rgrover1 284:82fefb5be8ed 333 ".info",
rgrover1 284:82fefb5be8ed 334 ".biz",
rgrover1 284:82fefb5be8ed 335 ".gov"
rgrover1 284:82fefb5be8ed 336 };
rgrover1 284:82fefb5be8ed 337 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
rgrover1 286:898ff71b9502 338
rgrover1 286:898ff71b9502 339 size_t encodedBytes = 0;
rgrover1 286:898ff71b9502 340 while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) {
rgrover1 284:82fefb5be8ed 341 /* check for suffix match */
rgrover1 284:82fefb5be8ed 342 unsigned i;
rgrover1 284:82fefb5be8ed 343 for (i = 0; i < NUM_SUFFIXES; i++) {
rgrover1 284:82fefb5be8ed 344 size_t suffixLen = strlen(suffixes[i]);
rgrover1 286:898ff71b9502 345 if ((suffixLen == 0) || (sizeofURLData < suffixLen)) {
rgrover1 284:82fefb5be8ed 346 continue;
rgrover1 284:82fefb5be8ed 347 }
rgrover1 284:82fefb5be8ed 348
rgrover1 286:898ff71b9502 349 if (strncmp(urldata, suffixes[i], suffixLen) == 0) {
rgrover1 286:898ff71b9502 350 serviceDataPayload[payloadIndex++] = i;
rgrover1 286:898ff71b9502 351 ++encodedBytes;
rgrover1 286:898ff71b9502 352 urldata += suffixLen;
rgrover1 286:898ff71b9502 353 sizeofURLData -= suffixLen;
rgrover1 284:82fefb5be8ed 354 break; /* from the for loop for checking against suffixes */
rgrover1 284:82fefb5be8ed 355 }
rgrover1 284:82fefb5be8ed 356 }
rgrover1 284:82fefb5be8ed 357 /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
rgrover1 284:82fefb5be8ed 358 if (i == NUM_SUFFIXES) {
rgrover1 286:898ff71b9502 359 serviceDataPayload[payloadIndex++] = *urldata;
rgrover1 286:898ff71b9502 360 ++encodedBytes;
rgrover1 286:898ff71b9502 361 ++urldata;
rgrover1 286:898ff71b9502 362 --sizeofURLData;
rgrover1 284:82fefb5be8ed 363 }
rgrover1 284:82fefb5be8ed 364 }
rgrover1 286:898ff71b9502 365 if (sizeofURLData == 0) {
rgrover1 286:898ff71b9502 366 initSucceeded = true;
rgrover1 286:898ff71b9502 367 }
rgrover1 286:898ff71b9502 368
rgrover1 286:898ff71b9502 369 return encodedBytes;
rgrover1 284:82fefb5be8ed 370 }
rgrover1 286:898ff71b9502 371
rgrover1 286:898ff71b9502 372 /*
rgrover1 286:898ff71b9502 373 * This callback is invoked when a GATT client attempts to modify any of the
rgrover1 286:898ff71b9502 374 * characteristics of this service. Attempts to do so are also applied to
rgrover1 286:898ff71b9502 375 * the internal state of this service object.
rgrover1 286:898ff71b9502 376 */
rgrover1 286:898ff71b9502 377 void onDataWritten(const GattCharacteristicWriteCBParams *params) {
rgrover1 286:898ff71b9502 378 uint16_t handle = params->charHandle;
rgrover1 286:898ff71b9502 379 if (handle == lockChar.getValueHandle()) {
rgrover1 286:898ff71b9502 380 updateLockBits(params->data);
rgrover1 286:898ff71b9502 381 storage_saveLockBits();
rgrover1 286:898ff71b9502 382 } else if (handle == unlockChar.getValueHandle()) {
rgrover1 286:898ff71b9502 383 memset(lockBits, 0, SIZEOF_LOCK_BITS);
rgrover1 286:898ff71b9502 384 lockedState = false;
rgrover1 286:898ff71b9502 385 storage_saveLockBits();
rgrover1 286:898ff71b9502 386 } else if (handle == uriDataChar.getValueHandle()) {
rgrover1 286:898ff71b9502 387 uriDataLength = params->len;
rgrover1 286:898ff71b9502 388 memcpy(uriData, params->data, uriDataLength);
rgrover1 286:898ff71b9502 389 } else if (handle == flagsChar.getValueHandle()) {
rgrover1 286:898ff71b9502 390 flags = *(params->data);
rgrover1 286:898ff71b9502 391 } else if (handle == txPowerLevelsChar.getValueHandle()) {
rgrover1 286:898ff71b9502 392 memcpy(powerLevels, params->data, NUM_POWER_MODES * sizeof(int8_t));
rgrover1 286:898ff71b9502 393 } else if (handle == txPowerModeChar.getValueHandle()) {
rgrover1 286:898ff71b9502 394 txPowerMode = *reinterpret_cast<const TXPowerModes_t *>(params->data);
rgrover1 286:898ff71b9502 395 } else if (handle == beaconPeriodChar.getValueHandle()) {
rgrover1 286:898ff71b9502 396 beaconPeriod = *((uint16_t *)(params->data));
rgrover1 286:898ff71b9502 397 } else if (handle == resetChar.getValueHandle()) {
rgrover1 286:898ff71b9502 398 resetDefaults();
rgrover1 286:898ff71b9502 399 }
rgrover1 286:898ff71b9502 400 configureGAP();
rgrover1 286:898ff71b9502 401 ble.setAdvertisingPayload();
rgrover1 286:898ff71b9502 402 }
rgrover1 286:898ff71b9502 403
rgrover1 286:898ff71b9502 404 /*
rgrover1 286:898ff71b9502 405 * Reset the default values.
rgrover1 286:898ff71b9502 406 */
rgrover1 286:898ff71b9502 407 void resetDefaults(void) {
rgrover1 286:898ff71b9502 408 uriDataLength = 0;
rgrover1 286:898ff71b9502 409 memset(uriData, 0, MAX_SIZE_URI_DATA_CHAR_VALUE);
rgrover1 286:898ff71b9502 410 flags = 0;
rgrover1 286:898ff71b9502 411 memset(powerLevels, 0, sizeof(powerLevels));
rgrover1 286:898ff71b9502 412 txPowerMode = TX_POWER_MODE_LOW;
rgrover1 286:898ff71b9502 413 beaconPeriod = 0;
rgrover1 286:898ff71b9502 414 resetLockBits();
rgrover1 286:898ff71b9502 415
rgrover1 286:898ff71b9502 416 updateGATT();
rgrover1 286:898ff71b9502 417 }
rgrover1 286:898ff71b9502 418
rgrover1 286:898ff71b9502 419 /*
rgrover1 286:898ff71b9502 420 * Internal helper function used to update the GATT database following any
rgrover1 286:898ff71b9502 421 * change to the internal state of the service object.
rgrover1 286:898ff71b9502 422 */
rgrover1 286:898ff71b9502 423 void updateGATT(void) {
rgrover1 286:898ff71b9502 424 updateLockedStateCharacteristic();
rgrover1 286:898ff71b9502 425 updateURIDataCharacteristic();
rgrover1 286:898ff71b9502 426 updateFlagsCharacteristic();
rgrover1 286:898ff71b9502 427 updateBeaconPeriodCharacteristic();
rgrover1 286:898ff71b9502 428 updateTxPowerLevelsCharacteristic();
rgrover1 286:898ff71b9502 429 updateTxPowerModeCharacteristic();
rgrover1 286:898ff71b9502 430 }
rgrover1 286:898ff71b9502 431
rgrover1 286:898ff71b9502 432 void updateLockedStateCharacteristic(void) {
rgrover1 286:898ff71b9502 433 ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), reinterpret_cast<uint8_t *>(&lockedState), sizeof(lockedState));
rgrover1 286:898ff71b9502 434 }
rgrover1 286:898ff71b9502 435
rgrover1 286:898ff71b9502 436 void updateURIDataCharacteristic(void) {
rgrover1 286:898ff71b9502 437 ble.updateCharacteristicValue(uriDataChar.getValueHandle(), uriData, uriDataLength);
rgrover1 286:898ff71b9502 438 }
rgrover1 286:898ff71b9502 439
rgrover1 286:898ff71b9502 440 void updateFlagsCharacteristic(void) {
rgrover1 286:898ff71b9502 441 ble.updateCharacteristicValue(flagsChar.getValueHandle(), &flags, 1 /* size */);
rgrover1 286:898ff71b9502 442 }
rgrover1 286:898ff71b9502 443
rgrover1 286:898ff71b9502 444 void updateBeaconPeriodCharacteristic(void) {
rgrover1 286:898ff71b9502 445 ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), reinterpret_cast<uint8_t *>(&beaconPeriod), sizeof(uint16_t));
rgrover1 286:898ff71b9502 446 }
rgrover1 286:898ff71b9502 447
rgrover1 286:898ff71b9502 448 void updateTxPowerModeCharacteristic(void) {
rgrover1 286:898ff71b9502 449 ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), reinterpret_cast<uint8_t *>(&txPowerMode), sizeof(uint8_t));
rgrover1 286:898ff71b9502 450 }
rgrover1 286:898ff71b9502 451
rgrover1 286:898ff71b9502 452 void updateTxPowerLevelsCharacteristic(void) {
rgrover1 286:898ff71b9502 453 ble.updateCharacteristicValue(txPowerLevelsChar.getValueHandle(), reinterpret_cast<uint8_t *>(powerLevels), NUM_POWER_MODES * sizeof(int8_t));
rgrover1 286:898ff71b9502 454 }
rgrover1 286:898ff71b9502 455
rgrover1 286:898ff71b9502 456 private:
rgrover1 286:898ff71b9502 457 // enable the following for debugging the state of the lock
rgrover1 286:898ff71b9502 458 // void lockedStateAuthorizationCallback(GattCharacteristicReadAuthCBParams *params) {
rgrover1 286:898ff71b9502 459 // printf("read authorization callback: lockedState is %u\r\n", lockedState);
rgrover1 286:898ff71b9502 460 // params->authorizationReply = true;
rgrover1 286:898ff71b9502 461 // }
rgrover1 286:898ff71b9502 462
rgrover1 286:898ff71b9502 463 void lockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 286:898ff71b9502 464 params->authorizationReply = !lockedState;
rgrover1 286:898ff71b9502 465 }
rgrover1 286:898ff71b9502 466
rgrover1 286:898ff71b9502 467 void unlockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 286:898ff71b9502 468 if (lockedState && (memcmp(params->data, lockBits, SIZEOF_LOCK_BITS) == 0)) {
rgrover1 286:898ff71b9502 469 params->authorizationReply = true;
rgrover1 286:898ff71b9502 470 } else {
rgrover1 286:898ff71b9502 471 params->authorizationReply = false;
rgrover1 286:898ff71b9502 472 }
rgrover1 286:898ff71b9502 473 }
rgrover1 286:898ff71b9502 474
rgrover1 286:898ff71b9502 475 void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 286:898ff71b9502 476 if (lockedState || (params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
rgrover1 286:898ff71b9502 477 params->authorizationReply = false;
rgrover1 286:898ff71b9502 478 }
rgrover1 286:898ff71b9502 479 }
rgrover1 286:898ff71b9502 480
rgrover1 286:898ff71b9502 481 void flagsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 286:898ff71b9502 482 if (lockedState || ((*(params->data) & 0xFE) != 0)) {
rgrover1 286:898ff71b9502 483 params->authorizationReply = false;
rgrover1 286:898ff71b9502 484 }
rgrover1 286:898ff71b9502 485 }
rgrover1 286:898ff71b9502 486
rgrover1 286:898ff71b9502 487 void powerModeAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 286:898ff71b9502 488 if (lockedState || (*(params->data) >= NUM_POWER_MODES)) {
rgrover1 286:898ff71b9502 489 params->authorizationReply = false;
rgrover1 286:898ff71b9502 490 }
rgrover1 286:898ff71b9502 491 }
rgrover1 286:898ff71b9502 492
rgrover1 286:898ff71b9502 493 void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 286:898ff71b9502 494 if (lockedState) {
rgrover1 286:898ff71b9502 495 params->authorizationReply = false;
rgrover1 286:898ff71b9502 496 }
rgrover1 286:898ff71b9502 497 }
rgrover1 286:898ff71b9502 498
rgrover1 286:898ff71b9502 499 private:
rgrover1 286:898ff71b9502 500 /**
rgrover1 286:898ff71b9502 501 * For debugging only. Print Hex representation of ServiceDataPayload to the console.
rgrover1 286:898ff71b9502 502 */
rgrover1 286:898ff71b9502 503 // void dumpEncodedSeviceData() const {
rgrover1 286:898ff71b9502 504 // printf("encoded: '");
rgrover1 286:898ff71b9502 505 // for (unsigned i = 0; i < payloadIndex; i++) {
rgrover1 286:898ff71b9502 506 // printf(" %02x", serviceDataPayload[i]);
rgrover1 286:898ff71b9502 507 // }
rgrover1 286:898ff71b9502 508 // printf("'\r\n");
rgrover1 286:898ff71b9502 509 // }
rgrover1 286:898ff71b9502 510
rgrover1 286:898ff71b9502 511 private:
rgrover1 286:898ff71b9502 512 static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 22; /* Uri Data must be between 0 and 18 bytes in length; and
rgrover1 286:898ff71b9502 513 * together with the 4-byte header, the service data must
rgrover1 286:898ff71b9502 514 * fit within 22 bytes. */
rgrover1 286:898ff71b9502 515 static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; /* This is chosen arbitrarily. It should be large enough
rgrover1 286:898ff71b9502 516 * to hold any reasonable uncompressed URI. */
rgrover1 286:898ff71b9502 517 private:
rgrover1 286:898ff71b9502 518 BLEDevice &ble;
rgrover1 286:898ff71b9502 519
rgrover1 286:898ff71b9502 520 size_t payloadIndex;
rgrover1 286:898ff71b9502 521 uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD];
rgrover1 286:898ff71b9502 522 bool initSucceeded;
rgrover1 286:898ff71b9502 523
rgrover1 286:898ff71b9502 524 bool lockedState;
rgrover1 286:898ff71b9502 525 uint8_t lockBits[SIZEOF_LOCK_BITS];
rgrover1 286:898ff71b9502 526
rgrover1 286:898ff71b9502 527 uint16_t uriDataLength;
rgrover1 286:898ff71b9502 528 uint8_t uriData[MAX_SIZE_URI_DATA_CHAR_VALUE];
rgrover1 286:898ff71b9502 529 uint8_t flags;
rgrover1 286:898ff71b9502 530 int8_t powerLevels[NUM_POWER_MODES];
rgrover1 286:898ff71b9502 531 TXPowerModes_t txPowerMode;
rgrover1 286:898ff71b9502 532 uint16_t beaconPeriod;
rgrover1 286:898ff71b9502 533 bool resetFlag;
rgrover1 286:898ff71b9502 534
rgrover1 286:898ff71b9502 535 ReadOnlyGattCharacteristic<bool> lockedStateChar;
rgrover1 286:898ff71b9502 536 WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_LOCK_BITS> lockChar;
rgrover1 286:898ff71b9502 537 WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_LOCK_BITS> unlockChar;
rgrover1 286:898ff71b9502 538 ReadWriteArrayGattCharacteristic<uint8_t, MAX_SIZE_URI_DATA_CHAR_VALUE> uriDataChar;
rgrover1 286:898ff71b9502 539 ReadWriteGattCharacteristic<uint8_t> flagsChar;
rgrover1 286:898ff71b9502 540 ReadWriteArrayGattCharacteristic<int8_t, NUM_POWER_MODES> txPowerLevelsChar;
rgrover1 286:898ff71b9502 541 ReadWriteGattCharacteristic<TXPowerModes_t> txPowerModeChar;
rgrover1 286:898ff71b9502 542 ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar;
rgrover1 286:898ff71b9502 543 WriteOnlyGattCharacteristic<bool> resetChar;
rgrover1 171:6092e61690dc 544 };
rgrover1 171:6092e61690dc 545
rgrover1 286:898ff71b9502 546 #endif /* #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__*/