I don't know why this is happening.

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Wed Jan 21 09:32:48 2015 +0000
Revision:
256:a6e651374060
Parent:
255:cdb7231f83df
Child:
268:bc525cdc88f7
Synchronized with git rev 22a54fcc
Author: Rohit Grover
Adds a check to ensure power mode gets updated safely.

fixes #8

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 225:f6cbfd817d16 28 static const uint8_t uriDataCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x84);
rgrover1 225:f6cbfd817d16 29 static const uint8_t flagsCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x85);
rgrover1 225:f6cbfd817d16 30 static const uint8_t txPowerLevelsCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x86);
rgrover1 225:f6cbfd817d16 31 static const uint8_t txPowerModeCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x87);
rgrover1 225:f6cbfd817d16 32 static const uint8_t beaconPeriodCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x88);
rgrover1 225:f6cbfd817d16 33 static const uint8_t resetCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x89);
rgrover1 171:6092e61690dc 34
mbedAustin 231:1c4a4fd961a5 35 /**
mbedAustin 231:1c4a4fd961a5 36 * @class URIBeaconConfigService
rgrover1 242:0e9201b67e2f 37 * @brief UriBeacon Configuration Service. Can be used to set URL, adjust power levels, and set flags.
mbedAustin 231:1c4a4fd961a5 38 */
rgrover1 207:e88130dc254c 39 class URIBeaconConfigService {
rgrover1 180:afcd2f9c2ada 40 public:
mbedAustin 231:1c4a4fd961a5 41 /**
rgrover1 242:0e9201b67e2f 42 * @enum TXPowerModes_t
rgrover1 242:0e9201b67e2f 43 * @brief Transmission Power Modes for UriBeacon
rgrover1 242:0e9201b67e2f 44 */
rgrover1 175:4e85f7225f8f 45 enum TXPowerModes_t {
mbedAustin 231:1c4a4fd961a5 46 TX_POWER_MODE_LOWEST = 0, /*!< Lowest TX power mode */
mbedAustin 231:1c4a4fd961a5 47 TX_POWER_MODE_LOW = 1, /*!< Low TX power mode */
mbedAustin 231:1c4a4fd961a5 48 TX_POWER_MODE_MEDIUM = 2, /*!< Medium TX power mode */
mbedAustin 231:1c4a4fd961a5 49 TX_POWER_MODE_HIGH = 3, /*!< High TX power mode */
mbedAustin 231:1c4a4fd961a5 50 NUM_POWER_MODES /*!< Number of Power Modes defined */
rgrover1 175:4e85f7225f8f 51 };
rgrover1 175:4e85f7225f8f 52
rgrover1 181:bbb6ce1082c3 53 /**
rgrover1 181:bbb6ce1082c3 54 * @param[ref] ble
rgrover1 181:bbb6ce1082c3 55 * BLEDevice object for the underlying controller.
rgrover1 208:88a3a58769bf 56 * @param[in] uridata
rgrover1 181:bbb6ce1082c3 57 * URI as a null-terminated string.
rgrover1 181:bbb6ce1082c3 58 * @param[in] flagsIn
rgrover1 181:bbb6ce1082c3 59 * UriBeacon Flags.
rgrover1 218:8ae02569fab9 60 * @param[in] powerLevels[]
rgrover1 218:8ae02569fab9 61 * Table of UriBeacon Tx Power Levels in dBm.
rgrover1 218:8ae02569fab9 62 * @param[in] powerMode
rgrover1 218:8ae02569fab9 63 * Currently effective power mode.
rgrover1 181:bbb6ce1082c3 64 * @param[in] beaconPeriodIn
rgrover1 181:bbb6ce1082c3 65 * The period in milliseconds that a UriBeacon packet is
rgrover1 181:bbb6ce1082c3 66 * transmitted. A value of zero disables UriBeacon
rgrover1 181:bbb6ce1082c3 67 * transmissions.
rgrover1 181:bbb6ce1082c3 68 */
rgrover1 218:8ae02569fab9 69 URIBeaconConfigService(BLEDevice &bleIn,
rgrover1 218:8ae02569fab9 70 const char *uriDataIn,
rgrover1 222:33b04c693e83 71 uint8_t flagsIn = 0,
rgrover1 218:8ae02569fab9 72 const int8_t powerLevelsIn[NUM_POWER_MODES] = NULL,
rgrover1 222:33b04c693e83 73 TXPowerModes_t powerModeIn = TX_POWER_MODE_LOW,
rgrover1 222:33b04c693e83 74 uint16_t beaconPeriodIn = 1000) :
rgrover1 192:3fd3dcf49005 75 ble(bleIn),
rgrover1 192:3fd3dcf49005 76 payloadIndex(0),
rgrover1 192:3fd3dcf49005 77 serviceDataPayload(),
rgrover1 192:3fd3dcf49005 78 initSucceeded(false),
rgrover1 192:3fd3dcf49005 79 lockedState(false),
rgrover1 192:3fd3dcf49005 80 uriDataLength(0),
rgrover1 192:3fd3dcf49005 81 uriData(),
rgrover1 192:3fd3dcf49005 82 flags(flagsIn),
rgrover1 192:3fd3dcf49005 83 powerLevels(),
rgrover1 201:9bb7b3f45c20 84 beaconPeriod(beaconPeriodIn),
rgrover1 192:3fd3dcf49005 85 lockedStateChar(lockedStateCharUUID, reinterpret_cast<uint8_t *>(&lockedState), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
rgrover1 215:f9d0169b9c28 86 uriDataChar(uriDataCharUUID, uriData, MAX_SIZE_URI_DATA_CHAR_VALUE, MAX_SIZE_URI_DATA_CHAR_VALUE,
rgrover1 225:f6cbfd817d16 87 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
rgrover1 192:3fd3dcf49005 88 flagsChar(flagsCharUUID, &flags, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
rgrover1 224:c55213f24647 89 txPowerLevelsChar(txPowerLevelsCharUUID, reinterpret_cast<uint8_t *>(powerLevels), sizeof(powerLevels), sizeof(powerLevels),
rgrover1 192:3fd3dcf49005 90 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
rgrover1 218:8ae02569fab9 91 txPowerModeChar(txPowerModeCharUUID, reinterpret_cast<uint8_t *>(&txPowerMode), sizeof(uint8_t), sizeof(uint8_t),
rgrover1 218:8ae02569fab9 92 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
rgrover1 192:3fd3dcf49005 93 beaconPeriodChar(beaconPeriodCharUUID, reinterpret_cast<uint8_t *>(&beaconPeriod), 2, 2,
rgrover1 192:3fd3dcf49005 94 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
rgrover1 215:f9d0169b9c28 95 resetChar(resetCharUUID, reinterpret_cast<uint8_t *>(&resetFlag), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)
rgrover1 192:3fd3dcf49005 96 {
rgrover1 194:669eced4cd5e 97 if ((uriDataIn == NULL) || ((uriDataLength = strlen(uriDataIn)) == 0) || (uriDataLength > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
rgrover1 192:3fd3dcf49005 98 return;
rgrover1 192:3fd3dcf49005 99 }
rgrover1 194:669eced4cd5e 100 strcpy(reinterpret_cast<char *>(uriData), uriDataIn);
rgrover1 192:3fd3dcf49005 101
rgrover1 226:ec436a9ceb31 102 if (powerLevelsIn != NULL) {
rgrover1 218:8ae02569fab9 103 memcpy(powerLevels, powerLevelsIn, sizeof(powerLevels));
rgrover1 218:8ae02569fab9 104 updateTxPowerLevelsCharacteristic();
rgrover1 218:8ae02569fab9 105 }
rgrover1 218:8ae02569fab9 106
rgrover1 210:0c14030d1bd0 107 configureGAP();
rgrover1 171:6092e61690dc 108
rgrover1 250:6862d374e613 109 uriDataChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::uriDataWriteAuthorizationCallback);
rgrover1 255:cdb7231f83df 110 flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::flagsAuthorizationCallback);
rgrover1 250:6862d374e613 111 txPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 256:a6e651374060 112 txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::powerModeAuthorizationCallback);
rgrover1 250:6862d374e613 113 beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 114 resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 115
rgrover1 192:3fd3dcf49005 116 GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar, &flagsChar, &txPowerLevelsChar, &beaconPeriodChar, &resetChar};
rgrover1 192:3fd3dcf49005 117 GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 182:d16f8c11816b 118
rgrover1 192:3fd3dcf49005 119 ble.addService(beaconControlService);
rgrover1 207:e88130dc254c 120 ble.onDataWritten(this, &URIBeaconConfigService::onDataWritten);
rgrover1 192:3fd3dcf49005 121 }
rgrover1 192:3fd3dcf49005 122
rgrover1 193:ac2feceb7e87 123 bool configuredSuccessfully(void) const {
rgrover1 192:3fd3dcf49005 124 return initSucceeded;
rgrover1 171:6092e61690dc 125 }
rgrover1 171:6092e61690dc 126
rgrover1 173:05c4b1cea65f 127 /**
rgrover1 196:febbd68b1095 128 * Please note that the following public APIs are offered to allow modifying
rgrover1 196:febbd68b1095 129 * the service programmatically. It is also possible to do so over BLE GATT
rgrover1 196:febbd68b1095 130 * transactions.
rgrover1 196:febbd68b1095 131 */
rgrover1 196:febbd68b1095 132 public:
rgrover1 196:febbd68b1095 133 /**
rgrover1 173:05c4b1cea65f 134 * Update flags of the URIBeacon dynamically.
rgrover1 173:05c4b1cea65f 135 *
rgrover1 173:05c4b1cea65f 136 * @param[in] flagsIn
mbedAustin 240:20df772f2374 137 * @verbatim
rgrover1 173:05c4b1cea65f 138 * ### UriBeacon Flags
rgrover1 173:05c4b1cea65f 139 * Bit | Description
rgrover1 173:05c4b1cea65f 140 * :---- | :----------
rgrover1 173:05c4b1cea65f 141 * 0 | Invisible Hint
rgrover1 173:05c4b1cea65f 142 * 1..7 | Reserved for future use. Must be zero.
mbedAustin 240:20df772f2374 143 * @endverbatim
rgrover1 173:05c4b1cea65f 144 * The `Invisible Hint` flag is a command for the user-agent that tells
rgrover1 173:05c4b1cea65f 145 * it not to access or display the UriBeacon. This is a guideline only,
rgrover1 173:05c4b1cea65f 146 * and is not a blocking method. User agents may, with user approval,
rgrover1 173:05c4b1cea65f 147 * display invisible beacons.
rgrover1 173:05c4b1cea65f 148 */
rgrover1 171:6092e61690dc 149 void setFlags(uint8_t flagsIn) {
rgrover1 171:6092e61690dc 150 flags = flagsIn;
rgrover1 210:0c14030d1bd0 151 configureGAP();
rgrover1 211:f181effe5de3 152 updateFlagsCharacteristic();
rgrover1 171:6092e61690dc 153 }
rgrover1 171:6092e61690dc 154
rgrover1 177:17f4b5924fe9 155 /**
rgrover1 242:0e9201b67e2f 156 * @brief Update the txPowerLevels table.
rgrover1 242:0e9201b67e2f 157 *
mbedAustin 239:58642869879c 158 * @param[in] powerLevelsIn
mbedAustin 239:58642869879c 159 * Array of power levels
rgrover1 177:17f4b5924fe9 160 */
rgrover1 219:5603d539120c 161 void setTxPowerLevels(const int8_t powerLevelsIn[NUM_POWER_MODES]) {
rgrover1 219:5603d539120c 162 memcpy(powerLevels, powerLevelsIn, sizeof(powerLevels));
rgrover1 223:01a71f5e0ce4 163 configureGAP();
rgrover1 219:5603d539120c 164 updateTxPowerLevelsCharacteristic();
rgrover1 177:17f4b5924fe9 165 }
rgrover1 177:17f4b5924fe9 166
rgrover1 177:17f4b5924fe9 167 /**
rgrover1 242:0e9201b67e2f 168 * @brief Set the effective power mode from one of the values in the powerLevels tables.
mbedAustin 239:58642869879c 169 *
mbedAustin 239:58642869879c 170 * @param[in] mode
mbedAustin 239:58642869879c 171 * Set the TX Power Mode.
rgrover1 177:17f4b5924fe9 172 */
rgrover1 219:5603d539120c 173 void setTxPowerMode(TXPowerModes_t mode) {
rgrover1 218:8ae02569fab9 174 txPowerMode = mode;
rgrover1 210:0c14030d1bd0 175 configureGAP();
rgrover1 218:8ae02569fab9 176 updateTxPowerModeCharacteristic();
rgrover1 171:6092e61690dc 177 }
rgrover1 171:6092e61690dc 178
rgrover1 183:41d6ffadba96 179 /**
rgrover1 183:41d6ffadba96 180 * The period in milliseconds that a UriBeacon packet is transmitted.
rgrover1 183:41d6ffadba96 181 *
mbedAustin 240:20df772f2374 182 * @note A value of zero disables UriBeacon transmissions.
mbedAustin 239:58642869879c 183 *
mbedAustin 239:58642869879c 184 * @param beaconPeriodIn
mbedAustin 239:58642869879c 185 * Beacon advertising period in milliseconds
rgrover1 183:41d6ffadba96 186 */
rgrover1 242:0e9201b67e2f 187 void setBeaconPeriod(uint16_t beaconPeriodIn) {
rgrover1 171:6092e61690dc 188 beaconPeriod = beaconPeriodIn;
rgrover1 210:0c14030d1bd0 189 configureGAP();
rgrover1 211:f181effe5de3 190 updateBeaconPeriodCharacteristic();
rgrover1 171:6092e61690dc 191 }
rgrover1 171:6092e61690dc 192
rgrover1 196:febbd68b1095 193 private:
mbedAustin 233:4d570d99b8cb 194 /*
rgrover1 186:ce34008cea95 195 * Setup the advertisement payload and GAP settings.
rgrover1 186:ce34008cea95 196 */
rgrover1 210:0c14030d1bd0 197 void configureGAP(void) {
rgrover1 171:6092e61690dc 198 const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
rgrover1 171:6092e61690dc 199
rgrover1 206:49646c933822 200 payloadIndex = 0;
rgrover1 171:6092e61690dc 201 serviceDataPayload[payloadIndex++] = BEACON_UUID[0];
rgrover1 171:6092e61690dc 202 serviceDataPayload[payloadIndex++] = BEACON_UUID[1];
rgrover1 171:6092e61690dc 203 serviceDataPayload[payloadIndex++] = flags;
rgrover1 218:8ae02569fab9 204 serviceDataPayload[payloadIndex++] = powerLevels[txPowerMode];
rgrover1 171:6092e61690dc 205
rgrover1 206:49646c933822 206 const char *urlData = reinterpret_cast<char *>(uriData);
rgrover1 206:49646c933822 207 size_t sizeofURLData = uriDataLength;
rgrover1 206:49646c933822 208 size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData);
rgrover1 171:6092e61690dc 209
rgrover1 171:6092e61690dc 210 ble.clearAdvertisingPayload();
rgrover1 171:6092e61690dc 211 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
rgrover1 171:6092e61690dc 212 ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4);
rgrover1 171:6092e61690dc 213
rgrover1 201:9bb7b3f45c20 214 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriod));
rgrover1 218:8ae02569fab9 215 ble.setTxPower(powerLevels[txPowerMode]);
rgrover1 171:6092e61690dc 216 }
rgrover1 171:6092e61690dc 217
mbedAustin 232:4cfb5b8a4fb9 218 /*
rgrover1 242:0e9201b67e2f 219 * Encode the URI Prefix to a single byte if possible.
rgrover1 242:0e9201b67e2f 220 */
rgrover1 171:6092e61690dc 221 size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) {
rgrover1 213:ac3b745af1d0 222 if (!sizeofURLData) {
rgrover1 213:ac3b745af1d0 223 return 0;
rgrover1 213:ac3b745af1d0 224 }
rgrover1 242:0e9201b67e2f 225
mbedAustin 232:4cfb5b8a4fb9 226 /* These are the URI Prefixes that can be abbreviated.*/
rgrover1 171:6092e61690dc 227 const char *prefixes[] = {
rgrover1 171:6092e61690dc 228 "http://www.",
rgrover1 171:6092e61690dc 229 "https://www.",
rgrover1 171:6092e61690dc 230 "http://",
rgrover1 171:6092e61690dc 231 "https://",
rgrover1 171:6092e61690dc 232 "urn:uuid:"
rgrover1 171:6092e61690dc 233 };
rgrover1 171:6092e61690dc 234
rgrover1 206:49646c933822 235 size_t encodedBytes = 0;
rgrover1 171:6092e61690dc 236 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
rgrover1 171:6092e61690dc 237 for (unsigned i = 0; i < NUM_PREFIXES; i++) {
rgrover1 171:6092e61690dc 238 size_t prefixLen = strlen(prefixes[i]);
rgrover1 171:6092e61690dc 239 if (strncmp(urldata, prefixes[i], prefixLen) == 0) {
rgrover1 171:6092e61690dc 240 serviceDataPayload[payloadIndex++] = i;
rgrover1 206:49646c933822 241 encodedBytes = 1;
rgrover1 171:6092e61690dc 242
rgrover1 206:49646c933822 243 urldata += prefixLen;
rgrover1 171:6092e61690dc 244 sizeofURLData -= prefixLen;
rgrover1 171:6092e61690dc 245 break;
rgrover1 171:6092e61690dc 246 }
rgrover1 171:6092e61690dc 247 }
rgrover1 171:6092e61690dc 248
rgrover1 171:6092e61690dc 249 return encodedBytes;
rgrover1 171:6092e61690dc 250 }
rgrover1 242:0e9201b67e2f 251
mbedAustin 232:4cfb5b8a4fb9 252 /*
rgrover1 242:0e9201b67e2f 253 * Encode the URI Suffix to a single byte if possible.
rgrover1 242:0e9201b67e2f 254 */
rgrover1 171:6092e61690dc 255 size_t encodeURI(const char *urldata, size_t sizeofURLData) {
mbedAustin 232:4cfb5b8a4fb9 256 /* These are the URI suffixes that can be abbreviated. */
rgrover1 171:6092e61690dc 257 const char *suffixes[] = {
rgrover1 171:6092e61690dc 258 ".com/",
rgrover1 171:6092e61690dc 259 ".org/",
rgrover1 171:6092e61690dc 260 ".edu/",
rgrover1 171:6092e61690dc 261 ".net/",
rgrover1 171:6092e61690dc 262 ".info/",
rgrover1 171:6092e61690dc 263 ".biz/",
rgrover1 171:6092e61690dc 264 ".gov/",
rgrover1 171:6092e61690dc 265 ".com",
rgrover1 171:6092e61690dc 266 ".org",
rgrover1 171:6092e61690dc 267 ".edu",
rgrover1 171:6092e61690dc 268 ".net",
rgrover1 171:6092e61690dc 269 ".info",
rgrover1 171:6092e61690dc 270 ".biz",
rgrover1 171:6092e61690dc 271 ".gov"
rgrover1 171:6092e61690dc 272 };
rgrover1 171:6092e61690dc 273 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
rgrover1 171:6092e61690dc 274
rgrover1 171:6092e61690dc 275 size_t encodedBytes = 0;
rgrover1 171:6092e61690dc 276 while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) {
rgrover1 171:6092e61690dc 277 /* check for suffix match */
rgrover1 171:6092e61690dc 278 unsigned i;
rgrover1 171:6092e61690dc 279 for (i = 0; i < NUM_SUFFIXES; i++) {
rgrover1 171:6092e61690dc 280 size_t suffixLen = strlen(suffixes[i]);
rgrover1 171:6092e61690dc 281 if ((suffixLen == 0) || (sizeofURLData < suffixLen)) {
rgrover1 171:6092e61690dc 282 continue;
rgrover1 171:6092e61690dc 283 }
rgrover1 171:6092e61690dc 284
rgrover1 171:6092e61690dc 285 if (strncmp(urldata, suffixes[i], suffixLen) == 0) {
rgrover1 171:6092e61690dc 286 serviceDataPayload[payloadIndex++] = i;
rgrover1 171:6092e61690dc 287 ++encodedBytes;
rgrover1 171:6092e61690dc 288 urldata += suffixLen;
rgrover1 171:6092e61690dc 289 sizeofURLData -= suffixLen;
rgrover1 171:6092e61690dc 290 break; /* from the for loop for checking against suffixes */
rgrover1 171:6092e61690dc 291 }
rgrover1 171:6092e61690dc 292 }
rgrover1 171:6092e61690dc 293 /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
rgrover1 171:6092e61690dc 294 if (i == NUM_SUFFIXES) {
rgrover1 171:6092e61690dc 295 serviceDataPayload[payloadIndex++] = *urldata;
rgrover1 171:6092e61690dc 296 ++encodedBytes;
rgrover1 171:6092e61690dc 297 ++urldata;
rgrover1 171:6092e61690dc 298 --sizeofURLData;
rgrover1 171:6092e61690dc 299 }
rgrover1 171:6092e61690dc 300 }
rgrover1 192:3fd3dcf49005 301 if (sizeofURLData == 0) {
rgrover1 192:3fd3dcf49005 302 initSucceeded = true;
rgrover1 182:d16f8c11816b 303 }
rgrover1 171:6092e61690dc 304
rgrover1 171:6092e61690dc 305 return encodedBytes;
rgrover1 171:6092e61690dc 306 }
rgrover1 171:6092e61690dc 307
mbedAustin 232:4cfb5b8a4fb9 308 /*
rgrover1 242:0e9201b67e2f 309 * This callback is invoked when a GATT client attempts to modify any of the
rgrover1 250:6862d374e613 310 * characteristics of this service. Attempts to do so are also applied to
rgrover1 250:6862d374e613 311 * the internal state of this service object.
rgrover1 242:0e9201b67e2f 312 */
rgrover1 203:7b5178d2fa03 313 void onDataWritten(const GattCharacteristicWriteCBParams *params) {
rgrover1 228:db043dbf26f8 314 uint16_t handle = params->charHandle;
rgrover1 229:6664b6c0e92b 315 if (handle == uriDataChar.getValueHandle()) {
rgrover1 185:7cd70497aec8 316 uriDataLength = params->len;
rgrover1 187:057c547facd2 317 memcpy(uriData, params->data, uriDataLength);
rgrover1 229:6664b6c0e92b 318 } else if (handle == flagsChar.getValueHandle()) {
rgrover1 250:6862d374e613 319 flags = *(params->data);
rgrover1 229:6664b6c0e92b 320 } else if (handle == txPowerLevelsChar.getValueHandle()) {
rgrover1 250:6862d374e613 321 memcpy(powerLevels, params->data, NUM_POWER_MODES * sizeof(int8_t));
rgrover1 229:6664b6c0e92b 322 } else if (handle == txPowerModeChar.getValueHandle()) {
rgrover1 250:6862d374e613 323 txPowerMode = *reinterpret_cast<const TXPowerModes_t *>(params->data);
rgrover1 229:6664b6c0e92b 324 } else if (handle == beaconPeriodChar.getValueHandle()) {
rgrover1 250:6862d374e613 325 beaconPeriod = *((uint16_t *)(params->data));
rgrover1 229:6664b6c0e92b 326 } else if (handle == resetChar.getValueHandle()) {
rgrover1 200:80dd3e50705c 327 resetDefaults();
rgrover1 185:7cd70497aec8 328 }
rgrover1 210:0c14030d1bd0 329 configureGAP();
rgrover1 185:7cd70497aec8 330 ble.setAdvertisingPayload();
rgrover1 185:7cd70497aec8 331 }
rgrover1 185:7cd70497aec8 332
mbedAustin 232:4cfb5b8a4fb9 333 /*
rgrover1 251:6ce809c972bb 334 * Reset the default values.
rgrover1 242:0e9201b67e2f 335 */
rgrover1 200:80dd3e50705c 336 void resetDefaults(void) {
rgrover1 216:3f5e47361919 337 lockedState = false;
rgrover1 212:852d45a52016 338 uriDataLength = 0;
rgrover1 216:3f5e47361919 339 memset(uriData, 0, MAX_SIZE_URI_DATA_CHAR_VALUE);
rgrover1 212:852d45a52016 340 flags = 0;
rgrover1 216:3f5e47361919 341 memset(powerLevels, 0, sizeof(powerLevels));
rgrover1 218:8ae02569fab9 342 txPowerMode = TX_POWER_MODE_LOW;
rgrover1 212:852d45a52016 343 beaconPeriod = 0;
rgrover1 189:a23091b54ab5 344
rgrover1 211:f181effe5de3 345 updateGATT();
rgrover1 211:f181effe5de3 346 }
rgrover1 211:f181effe5de3 347
mbedAustin 232:4cfb5b8a4fb9 348 /*
rgrover1 242:0e9201b67e2f 349 * Internal helper function used to update the GATT database following any
rgrover1 242:0e9201b67e2f 350 * change to the internal state of the service object.
rgrover1 242:0e9201b67e2f 351 */
rgrover1 211:f181effe5de3 352 void updateGATT(void) {
rgrover1 214:7d79fa197212 353 updateLockedStateCharacteristic();
rgrover1 211:f181effe5de3 354 updateURIDataCharacteristic();
rgrover1 211:f181effe5de3 355 updateFlagsCharacteristic();
rgrover1 211:f181effe5de3 356 updateBeaconPeriodCharacteristic();
rgrover1 220:e75e8ba1ff80 357 updateTxPowerLevelsCharacteristic();
rgrover1 220:e75e8ba1ff80 358 updateTxPowerModeCharacteristic();
rgrover1 211:f181effe5de3 359 }
rgrover1 211:f181effe5de3 360
rgrover1 214:7d79fa197212 361 void updateLockedStateCharacteristic(void) {
rgrover1 229:6664b6c0e92b 362 ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), reinterpret_cast<uint8_t *>(&lockedState), sizeof(lockedState));
rgrover1 214:7d79fa197212 363 }
rgrover1 214:7d79fa197212 364
rgrover1 211:f181effe5de3 365 void updateURIDataCharacteristic(void) {
rgrover1 229:6664b6c0e92b 366 ble.updateCharacteristicValue(uriDataChar.getValueHandle(), uriData, uriDataLength);
rgrover1 211:f181effe5de3 367 }
rgrover1 211:f181effe5de3 368
rgrover1 211:f181effe5de3 369 void updateFlagsCharacteristic(void) {
rgrover1 229:6664b6c0e92b 370 ble.updateCharacteristicValue(flagsChar.getValueHandle(), &flags, 1 /* size */);
rgrover1 211:f181effe5de3 371 }
rgrover1 211:f181effe5de3 372
rgrover1 211:f181effe5de3 373 void updateBeaconPeriodCharacteristic(void) {
rgrover1 229:6664b6c0e92b 374 ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), reinterpret_cast<uint8_t *>(&beaconPeriod), sizeof(uint16_t));
rgrover1 189:a23091b54ab5 375 }
rgrover1 189:a23091b54ab5 376
rgrover1 218:8ae02569fab9 377 void updateTxPowerModeCharacteristic(void) {
rgrover1 229:6664b6c0e92b 378 ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), reinterpret_cast<uint8_t *>(&txPowerMode), sizeof(uint8_t));
rgrover1 218:8ae02569fab9 379 }
rgrover1 218:8ae02569fab9 380
rgrover1 218:8ae02569fab9 381 void updateTxPowerLevelsCharacteristic(void) {
rgrover1 242:0e9201b67e2f 382 ble.updateCharacteristicValue(txPowerLevelsChar.getValueHandle(), reinterpret_cast<uint8_t *>(powerLevels), NUM_POWER_MODES * sizeof(int8_t));
rgrover1 218:8ae02569fab9 383 }
rgrover1 218:8ae02569fab9 384
rgrover1 242:0e9201b67e2f 385 private:
rgrover1 250:6862d374e613 386 void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 250:6862d374e613 387 if (lockedState || (params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
rgrover1 250:6862d374e613 388 params->authorizationReply = false;
rgrover1 250:6862d374e613 389 }
rgrover1 250:6862d374e613 390 }
rgrover1 250:6862d374e613 391
rgrover1 255:cdb7231f83df 392 void flagsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 250:6862d374e613 393 if (lockedState || ((*(params->data) & 0xFE) != 0)) {
rgrover1 250:6862d374e613 394 params->authorizationReply = false;
rgrover1 250:6862d374e613 395 }
rgrover1 250:6862d374e613 396 }
rgrover1 250:6862d374e613 397
rgrover1 256:a6e651374060 398 void powerModeAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 256:a6e651374060 399 if (lockedState || (*(params->data) >= NUM_POWER_MODES)) {
rgrover1 256:a6e651374060 400 params->authorizationReply = false;
rgrover1 256:a6e651374060 401 }
rgrover1 256:a6e651374060 402 }
rgrover1 256:a6e651374060 403
rgrover1 250:6862d374e613 404 void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *params) {
rgrover1 250:6862d374e613 405 if (lockedState) {
rgrover1 250:6862d374e613 406 params->authorizationReply = false;
rgrover1 250:6862d374e613 407 }
rgrover1 250:6862d374e613 408 }
rgrover1 250:6862d374e613 409
rgrover1 250:6862d374e613 410 private:
mbedAustin 233:4d570d99b8cb 411 /**
rgrover1 242:0e9201b67e2f 412 * For debugging only. Print Hex representation of ServiceDataPayload to the console.
rgrover1 185:7cd70497aec8 413 */
rgrover1 185:7cd70497aec8 414 void dumpEncodedSeviceData() const {
rgrover1 185:7cd70497aec8 415 printf("encoded: '");
rgrover1 185:7cd70497aec8 416 for (unsigned i = 0; i < payloadIndex; i++) {
rgrover1 185:7cd70497aec8 417 printf(" %02x", serviceDataPayload[i]);
rgrover1 185:7cd70497aec8 418 }
rgrover1 185:7cd70497aec8 419 printf("'\r\n");
rgrover1 185:7cd70497aec8 420 }
rgrover1 185:7cd70497aec8 421
rgrover1 171:6092e61690dc 422 private:
rgrover1 254:fb2a891a0d98 423 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 424 * together with the 4-byte header, the service data must
rgrover1 254:fb2a891a0d98 425 * fit within 22 bytes. */
rgrover1 204:479f81fd5d3b 426 static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; /* This is chosen arbitrarily. It should be large enough
rgrover1 204:479f81fd5d3b 427 * to hold any reasonable uncompressed URI. */
rgrover1 171:6092e61690dc 428
rgrover1 171:6092e61690dc 429 private:
rgrover1 206:49646c933822 430 BLEDevice &ble;
rgrover1 171:6092e61690dc 431
rgrover1 206:49646c933822 432 size_t payloadIndex;
rgrover1 206:49646c933822 433 uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD];
rgrover1 206:49646c933822 434 bool initSucceeded;
rgrover1 182:d16f8c11816b 435
rgrover1 206:49646c933822 436 bool lockedState;
rgrover1 206:49646c933822 437 uint16_t uriDataLength;
rgrover1 206:49646c933822 438 uint8_t uriData[MAX_SIZE_URI_DATA_CHAR_VALUE];
rgrover1 206:49646c933822 439 uint8_t flags;
rgrover1 206:49646c933822 440 int8_t powerLevels[NUM_POWER_MODES];
rgrover1 218:8ae02569fab9 441 TXPowerModes_t txPowerMode;
rgrover1 206:49646c933822 442 uint16_t beaconPeriod;
rgrover1 206:49646c933822 443 bool resetFlag;
rgrover1 171:6092e61690dc 444
rgrover1 206:49646c933822 445 GattCharacteristic lockedStateChar;
rgrover1 206:49646c933822 446 GattCharacteristic uriDataChar;
rgrover1 206:49646c933822 447 GattCharacteristic flagsChar;
rgrover1 206:49646c933822 448 GattCharacteristic txPowerLevelsChar;
rgrover1 218:8ae02569fab9 449 GattCharacteristic txPowerModeChar;
rgrover1 206:49646c933822 450 GattCharacteristic beaconPeriodChar;
rgrover1 206:49646c933822 451 GattCharacteristic resetChar;
rgrover1 171:6092e61690dc 452 };
rgrover1 171:6092e61690dc 453
rgrover1 207:e88130dc254c 454 #endif /* #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__*/