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:
Thu Jan 22 09:59:44 2015 +0000
Revision:
277:1407d2f1ce3c
Parent:
276:daa42f59bdb8
Child:
278:8a935a2d4a16
Synchronized with git rev 4a268db0
Author: Rohit Grover
Release 0.2.11
=============

Enhancements
~~~~~~~~~~~~

* Use template classes to simplify definitions of GattCharacteristic within services.

Bugfixes
~~~~~~~~

Compatibility
~~~~~~~~~~~~~

This release is API compatible with 0.2.4.

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