I don't know why this is happening.

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Wed Jan 21 09:32:52 2015 +0000
Revision:
269:d818d11dca4e
Parent:
268:bc525cdc88f7
Child:
270:e2e0f4c850fd
Synchronized with git rev 8b9a5013
Author: Rohit Grover
Add persistence API to be able to save and retrieve lockBits.

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