I don't know why this is happening.

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Feb 13 13:00:41 2015 +0000
Revision:
284:82fefb5be8ed
Parent:
283:bef53917ea82
Child:
285:984353e66610
Synchronized with git rev a45afbc0
Author: Rohit Grover
add a helper to encode URIData

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 278:8a935a2d4a16 17 #ifndef SERVICES_URIBEACONCONFIGSERVICE_H_
rgrover1 278:8a935a2d4a16 18 #define SERVICES_URIBEACONCONFIGSERVICE_H_
rgrover1 171:6092e61690dc 19
rgrover1 278:8a935a2d4a16 20 #define UUID_URI_BEACON(FIRST, SECOND) { \
rgrover1 225:f6cbfd817d16 21 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \
rgrover1 225:f6cbfd817d16 22 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \
rgrover1 171:6092e61690dc 23 }
rgrover1 278:8a935a2d4a16 24
rgrover1 278:8a935a2d4a16 25 static const uint8_t UUID_URI_BEACON_SERVICE[] = UUID_URI_BEACON(0x20, 0x80);
rgrover1 278:8a935a2d4a16 26 static const uint8_t UUID_LOCK_STATE_CHAR[] = UUID_URI_BEACON(0x20, 0x81);
rgrover1 278:8a935a2d4a16 27 static const uint8_t UUID_LOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x82);
rgrover1 278:8a935a2d4a16 28 static const uint8_t UUID_UNLOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x83);
rgrover1 278:8a935a2d4a16 29 static const uint8_t UUID_URI_DATA_CHAR[] = UUID_URI_BEACON(0x20, 0x84);
rgrover1 278:8a935a2d4a16 30 static const uint8_t UUID_FLAGS_CHAR[] = UUID_URI_BEACON(0x20, 0x85);
rgrover1 278:8a935a2d4a16 31 static const uint8_t UUID_ADV_POWER_LEVELS_CHAR[] = UUID_URI_BEACON(0x20, 0x86);
rgrover1 278:8a935a2d4a16 32 static const uint8_t UUID_TX_POWER_MODE_CHAR[] = UUID_URI_BEACON(0x20, 0x87);
rgrover1 278:8a935a2d4a16 33 static const uint8_t UUID_BEACON_PERIOD_CHAR[] = UUID_URI_BEACON(0x20, 0x88);
rgrover1 278:8a935a2d4a16 34 static const uint8_t UUID_RESET_CHAR[] = UUID_URI_BEACON(0x20, 0x89);
rgrover1 278:8a935a2d4a16 35 static const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
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.
rgrover1 278:8a935a2d4a16 40 * See http://uribeacon.org
rgrover1 278:8a935a2d4a16 41 *
mbedAustin 231:1c4a4fd961a5 42 */
rgrover1 207:e88130dc254c 43 class URIBeaconConfigService {
rgrover1 278:8a935a2d4a16 44 public:
mbedAustin 231:1c4a4fd961a5 45 /**
rgrover1 242:0e9201b67e2f 46 * @brief Transmission Power Modes for UriBeacon
rgrover1 242:0e9201b67e2f 47 */
rgrover1 278:8a935a2d4a16 48 static const uint8_t TX_POWER_MODE_LOWEST = 0; /*!< Lowest TX power mode */
rgrover1 278:8a935a2d4a16 49 static const uint8_t TX_POWER_MODE_LOW = 1; /*!< Low TX power mode */
rgrover1 278:8a935a2d4a16 50 static const uint8_t TX_POWER_MODE_MEDIUM = 2; /*!< Medium TX power mode */
rgrover1 278:8a935a2d4a16 51 static const uint8_t TX_POWER_MODE_HIGH = 3; /*!< High TX power mode */
rgrover1 281:e459c5be1a10 52 static const unsigned int NUM_POWER_MODES = 4; /*!< Number of Power Modes defined */
rgrover1 278:8a935a2d4a16 53
rgrover1 278:8a935a2d4a16 54
rgrover1 278:8a935a2d4a16 55 typedef uint8_t Lock_t[16]; /* 128 bits */
rgrover1 278:8a935a2d4a16 56 typedef int8_t PowerLevels_t[NUM_POWER_MODES];
rgrover1 278:8a935a2d4a16 57
rgrover1 278:8a935a2d4a16 58 static const int URI_DATA_MAX = 18;
rgrover1 283:bef53917ea82 59 typedef uint8_t UriData_t[URI_DATA_MAX];
rgrover1 278:8a935a2d4a16 60
rgrover1 278:8a935a2d4a16 61 struct Params_t {
rgrover1 278:8a935a2d4a16 62 Lock_t lock;
rgrover1 278:8a935a2d4a16 63 uint8_t uriDataLength;
rgrover1 278:8a935a2d4a16 64 UriData_t uriData;
rgrover1 278:8a935a2d4a16 65 uint8_t flags;
rgrover1 278:8a935a2d4a16 66 // Current value of AdvertisedPowerLevels
rgrover1 278:8a935a2d4a16 67 PowerLevels_t advPowerLevels;
rgrover1 278:8a935a2d4a16 68 // Firmware power levels used with setTxPower()
rgrover1 278:8a935a2d4a16 69 uint8_t txPowerMode;
rgrover1 278:8a935a2d4a16 70 uint16_t beaconPeriod;
rgrover1 175:4e85f7225f8f 71 };
rgrover1 175:4e85f7225f8f 72
rgrover1 269:d818d11dca4e 73
rgrover1 181:bbb6ce1082c3 74 /**
rgrover1 181:bbb6ce1082c3 75 * @param[ref] ble
rgrover1 181:bbb6ce1082c3 76 * BLEDevice object for the underlying controller.
rgrover1 208:88a3a58769bf 77 * @param[in] uridata
rgrover1 181:bbb6ce1082c3 78 * URI as a null-terminated string.
rgrover1 181:bbb6ce1082c3 79 * @param[in] flagsIn
rgrover1 181:bbb6ce1082c3 80 * UriBeacon Flags.
rgrover1 218:8ae02569fab9 81 * @param[in] powerLevels[]
rgrover1 218:8ae02569fab9 82 * Table of UriBeacon Tx Power Levels in dBm.
rgrover1 218:8ae02569fab9 83 * @param[in] powerMode
rgrover1 218:8ae02569fab9 84 * Currently effective power mode.
rgrover1 181:bbb6ce1082c3 85 * @param[in] beaconPeriodIn
rgrover1 181:bbb6ce1082c3 86 * The period in milliseconds that a UriBeacon packet is
rgrover1 181:bbb6ce1082c3 87 * transmitted. A value of zero disables UriBeacon
rgrover1 181:bbb6ce1082c3 88 * transmissions.
rgrover1 181:bbb6ce1082c3 89 */
rgrover1 283:bef53917ea82 90 URIBeaconConfigService(BLEDevice &bleIn,
rgrover1 283:bef53917ea82 91 Params_t &paramsIn,
rgrover1 283:bef53917ea82 92 bool resetToDefaultsFlag,
rgrover1 283:bef53917ea82 93 UriData_t &defaultUriDataIn,
rgrover1 283:bef53917ea82 94 int defaultUriDataLengthIn,
rgrover1 283:bef53917ea82 95 PowerLevels_t &defaultAdvPowerLevelsIn) :
rgrover1 192:3fd3dcf49005 96 ble(bleIn),
rgrover1 278:8a935a2d4a16 97 params(paramsIn),
rgrover1 278:8a935a2d4a16 98 defaultUriDataLength(defaultUriDataLengthIn),
rgrover1 278:8a935a2d4a16 99 defaultUriData(defaultUriDataIn),
rgrover1 278:8a935a2d4a16 100 defaultAdvPowerLevels(defaultAdvPowerLevelsIn),
rgrover1 192:3fd3dcf49005 101 initSucceeded(false),
rgrover1 278:8a935a2d4a16 102 lockedStateChar(UUID_LOCK_STATE_CHAR, &lockedState),
rgrover1 278:8a935a2d4a16 103 lockChar(UUID_LOCK_CHAR, &params.lock),
rgrover1 278:8a935a2d4a16 104 uriDataChar(UUID_URI_DATA_CHAR, params.uriData, URI_DATA_MAX, params.uriDataLength,
rgrover1 278:8a935a2d4a16 105 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
rgrover1 278:8a935a2d4a16 106 unlockChar(UUID_UNLOCK_CHAR, &params.lock),
rgrover1 278:8a935a2d4a16 107 flagsChar(UUID_FLAGS_CHAR, &params.flags),
rgrover1 278:8a935a2d4a16 108 advPowerLevelsChar(UUID_ADV_POWER_LEVELS_CHAR, &params.advPowerLevels),
rgrover1 278:8a935a2d4a16 109 txPowerModeChar(UUID_TX_POWER_MODE_CHAR, &params.txPowerMode),
rgrover1 278:8a935a2d4a16 110 beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, &params.beaconPeriod),
rgrover1 278:8a935a2d4a16 111 resetChar(UUID_RESET_CHAR, &resetFlag) {
rgrover1 278:8a935a2d4a16 112 if (defaultUriDataLengthIn > URI_DATA_MAX) {
rgrover1 192:3fd3dcf49005 113 return;
rgrover1 192:3fd3dcf49005 114 }
rgrover1 192:3fd3dcf49005 115
rgrover1 278:8a935a2d4a16 116 if (params.uriDataLength > URI_DATA_MAX) {
rgrover1 278:8a935a2d4a16 117 resetToDefaultsFlag = true;
rgrover1 218:8ae02569fab9 118 }
rgrover1 218:8ae02569fab9 119
rgrover1 278:8a935a2d4a16 120 lockedState = isLocked();
rgrover1 171:6092e61690dc 121
rgrover1 278:8a935a2d4a16 122 if (resetToDefaultsFlag) {
rgrover1 278:8a935a2d4a16 123 resetToDefaults();
rgrover1 278:8a935a2d4a16 124 } else {
rgrover1 278:8a935a2d4a16 125 updateCharacteristicValues();
rgrover1 278:8a935a2d4a16 126 }
rgrover1 274:f540619754bb 127
rgrover1 272:65b9d9c87ed1 128 lockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::lockAuthorizationCallback);
rgrover1 272:65b9d9c87ed1 129 unlockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::unlockAuthorizationCallback);
rgrover1 250:6862d374e613 130 uriDataChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::uriDataWriteAuthorizationCallback);
rgrover1 255:cdb7231f83df 131 flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::flagsAuthorizationCallback);
rgrover1 278:8a935a2d4a16 132 advPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 278:8a935a2d4a16 133 txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 134 beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 135 resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
rgrover1 250:6862d374e613 136
rgrover1 278:8a935a2d4a16 137 static GattCharacteristic *charTable[] = {
rgrover1 278:8a935a2d4a16 138 &lockedStateChar, &lockChar, &unlockChar, &uriDataChar,
rgrover1 278:8a935a2d4a16 139 &flagsChar, &advPowerLevelsChar, &txPowerModeChar, &beaconPeriodChar, &resetChar
rgrover1 278:8a935a2d4a16 140 };
rgrover1 182:d16f8c11816b 141
rgrover1 278:8a935a2d4a16 142 GattService configService(UUID_URI_BEACON_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 278:8a935a2d4a16 143
rgrover1 279:00649389c020 144 ble.addService(configService);
rgrover1 278:8a935a2d4a16 145 ble.onDataWritten(this, &URIBeaconConfigService::onDataWrittenCallback);
rgrover1 278:8a935a2d4a16 146
rgrover1 278:8a935a2d4a16 147 initSucceeded = true;
rgrover1 192:3fd3dcf49005 148 }
rgrover1 192:3fd3dcf49005 149
rgrover1 193:ac2feceb7e87 150 bool configuredSuccessfully(void) const {
rgrover1 192:3fd3dcf49005 151 return initSucceeded;
rgrover1 171:6092e61690dc 152 }
rgrover1 171:6092e61690dc 153
rgrover1 171:6092e61690dc 154
rgrover1 278:8a935a2d4a16 155 private:
rgrover1 282:2c490f373b9b 156 // True if the lock bits are non-zero
rgrover1 278:8a935a2d4a16 157 bool isLocked() {
rgrover1 278:8a935a2d4a16 158 Lock_t testLock;
rgrover1 278:8a935a2d4a16 159 memset(testLock, 0, sizeof(Lock_t));
rgrover1 278:8a935a2d4a16 160 return memcmp(params.lock, testLock, sizeof(Lock_t));
rgrover1 171:6092e61690dc 161 }
rgrover1 171:6092e61690dc 162
mbedAustin 232:4cfb5b8a4fb9 163 /*
rgrover1 242:0e9201b67e2f 164 * This callback is invoked when a GATT client attempts to modify any of the
rgrover1 250:6862d374e613 165 * characteristics of this service. Attempts to do so are also applied to
rgrover1 250:6862d374e613 166 * the internal state of this service object.
rgrover1 242:0e9201b67e2f 167 */
rgrover1 278:8a935a2d4a16 168 void onDataWrittenCallback(const GattCharacteristicWriteCBParams *writeParams) {
rgrover1 278:8a935a2d4a16 169 uint16_t handle = writeParams->charHandle;
rgrover1 278:8a935a2d4a16 170
rgrover1 268:bc525cdc88f7 171 if (handle == lockChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 172 // Validated earlier
rgrover1 278:8a935a2d4a16 173 memcpy(params.lock, writeParams->data, sizeof(Lock_t));
rgrover1 282:2c490f373b9b 174 // use isLocked() in case bits are being set to all 0's
rgrover1 282:2c490f373b9b 175 lockedState = isLocked();
rgrover1 268:bc525cdc88f7 176 } else if (handle == unlockChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 177 // Validated earlier
rgrover1 278:8a935a2d4a16 178 memset(params.lock, 0, sizeof(Lock_t));
rgrover1 268:bc525cdc88f7 179 lockedState = false;
rgrover1 268:bc525cdc88f7 180 } else if (handle == uriDataChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 181 params.uriDataLength = writeParams->len;
rgrover1 278:8a935a2d4a16 182 memcpy(params.uriData, writeParams->data, params.uriDataLength);
rgrover1 229:6664b6c0e92b 183 } else if (handle == flagsChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 184 params.flags = *(writeParams->data);
rgrover1 278:8a935a2d4a16 185 } else if (handle == advPowerLevelsChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 186 memcpy(params.advPowerLevels, writeParams->data, sizeof(PowerLevels_t));
rgrover1 229:6664b6c0e92b 187 } else if (handle == txPowerModeChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 188 params.txPowerMode = *(writeParams->data);
rgrover1 229:6664b6c0e92b 189 } else if (handle == beaconPeriodChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 190 params.beaconPeriod = *((uint16_t *)(writeParams->data));
rgrover1 229:6664b6c0e92b 191 } else if (handle == resetChar.getValueHandle()) {
rgrover1 278:8a935a2d4a16 192 resetToDefaults();
rgrover1 185:7cd70497aec8 193 }
rgrover1 185:7cd70497aec8 194 }
rgrover1 185:7cd70497aec8 195
mbedAustin 232:4cfb5b8a4fb9 196 /*
rgrover1 251:6ce809c972bb 197 * Reset the default values.
rgrover1 242:0e9201b67e2f 198 */
rgrover1 278:8a935a2d4a16 199 void resetToDefaults(void) {
rgrover1 278:8a935a2d4a16 200 lockedState = false;
rgrover1 278:8a935a2d4a16 201 memset(params.lock, 0, sizeof(Lock_t));
rgrover1 278:8a935a2d4a16 202 memcpy(params.uriData, defaultUriData, URI_DATA_MAX);
rgrover1 278:8a935a2d4a16 203 params.uriDataLength = defaultUriDataLength;
rgrover1 278:8a935a2d4a16 204 params.flags = 0;
rgrover1 278:8a935a2d4a16 205 memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t));
rgrover1 278:8a935a2d4a16 206 params.txPowerMode = TX_POWER_MODE_LOW;
rgrover1 278:8a935a2d4a16 207 params.beaconPeriod = 1000;
rgrover1 278:8a935a2d4a16 208 updateCharacteristicValues();
rgrover1 211:f181effe5de3 209 }
rgrover1 211:f181effe5de3 210
mbedAustin 232:4cfb5b8a4fb9 211 /*
rgrover1 242:0e9201b67e2f 212 * Internal helper function used to update the GATT database following any
rgrover1 242:0e9201b67e2f 213 * change to the internal state of the service object.
rgrover1 242:0e9201b67e2f 214 */
rgrover1 278:8a935a2d4a16 215 void updateCharacteristicValues(void) {
rgrover1 278:8a935a2d4a16 216 ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), &lockedState, 1);
rgrover1 278:8a935a2d4a16 217 ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength);
rgrover1 279:00649389c020 218 ble.updateCharacteristicValue(flagsChar.getValueHandle(), &params.flags, 1);
rgrover1 278:8a935a2d4a16 219 ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(),
rgrover1 279:00649389c020 220 reinterpret_cast<uint8_t *>(&params.beaconPeriod), sizeof(uint16_t));
rgrover1 278:8a935a2d4a16 221 ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), &params.txPowerMode, 1);
rgrover1 278:8a935a2d4a16 222 ble.updateCharacteristicValue(advPowerLevelsChar.getValueHandle(),
rgrover1 279:00649389c020 223 reinterpret_cast<uint8_t *>(params.advPowerLevels), sizeof(PowerLevels_t));
rgrover1 189:a23091b54ab5 224 }
rgrover1 189:a23091b54ab5 225
rgrover1 278:8a935a2d4a16 226 private:
rgrover1 278:8a935a2d4a16 227 void lockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) {
rgrover1 278:8a935a2d4a16 228 authParams->authorizationReply = (authParams->len == sizeof(Lock_t)) && !lockedState;
rgrover1 218:8ae02569fab9 229 }
rgrover1 218:8ae02569fab9 230
rgrover1 268:bc525cdc88f7 231
rgrover1 278:8a935a2d4a16 232 void unlockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) {
rgrover1 278:8a935a2d4a16 233 if (!lockedState || (authParams->len == sizeof(Lock_t) && (memcmp(authParams->data, params.lock, sizeof(Lock_t)) == 0))) {
rgrover1 278:8a935a2d4a16 234 authParams->authorizationReply = true;
rgrover1 268:bc525cdc88f7 235 } else {
rgrover1 278:8a935a2d4a16 236 authParams->authorizationReply = false;
rgrover1 268:bc525cdc88f7 237 }
rgrover1 268:bc525cdc88f7 238 }
rgrover1 268:bc525cdc88f7 239
rgrover1 278:8a935a2d4a16 240 void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) {
rgrover1 278:8a935a2d4a16 241 if (lockedState || (authParams->offset != 0) || (authParams->len > URI_DATA_MAX)) {
rgrover1 278:8a935a2d4a16 242 authParams->authorizationReply = false;
rgrover1 250:6862d374e613 243 }
rgrover1 250:6862d374e613 244 }
rgrover1 250:6862d374e613 245
rgrover1 278:8a935a2d4a16 246 void flagsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) {
rgrover1 278:8a935a2d4a16 247 if (lockedState || authParams->len != 1) {
rgrover1 278:8a935a2d4a16 248 authParams->authorizationReply = false;
rgrover1 250:6862d374e613 249 }
rgrover1 250:6862d374e613 250 }
rgrover1 250:6862d374e613 251
rgrover1 278:8a935a2d4a16 252 void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *authParams) {
rgrover1 250:6862d374e613 253 if (lockedState) {
rgrover1 278:8a935a2d4a16 254 authParams->authorizationReply = false;
rgrover1 250:6862d374e613 255 }
rgrover1 250:6862d374e613 256 }
rgrover1 250:6862d374e613 257
rgrover1 283:bef53917ea82 258 BLEDevice &ble;
rgrover1 283:bef53917ea82 259 Params_t &params;
rgrover1 278:8a935a2d4a16 260 // Default value that is restored on reset
rgrover1 283:bef53917ea82 261 uint16_t defaultUriDataLength;
rgrover1 283:bef53917ea82 262 UriData_t &defaultUriData;
rgrover1 278:8a935a2d4a16 263 // Default value that is restored on reset
rgrover1 283:bef53917ea82 264 PowerLevels_t &defaultAdvPowerLevels;
rgrover1 283:bef53917ea82 265 uint8_t lockedState;
rgrover1 283:bef53917ea82 266 bool initSucceeded;
rgrover1 283:bef53917ea82 267 uint8_t resetFlag;
rgrover1 171:6092e61690dc 268
rgrover1 283:bef53917ea82 269 ReadOnlyGattCharacteristic<uint8_t> lockedStateChar;
rgrover1 283:bef53917ea82 270 WriteOnlyGattCharacteristic<Lock_t> lockChar;
rgrover1 283:bef53917ea82 271 GattCharacteristic uriDataChar;
rgrover1 283:bef53917ea82 272 WriteOnlyGattCharacteristic<Lock_t> unlockChar;
rgrover1 283:bef53917ea82 273 ReadWriteGattCharacteristic<uint8_t> flagsChar;
rgrover1 278:8a935a2d4a16 274 ReadWriteGattCharacteristic<PowerLevels_t> advPowerLevelsChar;
rgrover1 283:bef53917ea82 275 ReadWriteGattCharacteristic<uint8_t> txPowerModeChar;
rgrover1 283:bef53917ea82 276 ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar;
rgrover1 283:bef53917ea82 277 WriteOnlyGattCharacteristic<uint8_t> resetChar;
rgrover1 284:82fefb5be8ed 278
rgrover1 284:82fefb5be8ed 279 public:
rgrover1 284:82fefb5be8ed 280 /*
rgrover1 284:82fefb5be8ed 281 * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification).
rgrover1 284:82fefb5be8ed 282 */
rgrover1 284:82fefb5be8ed 283 static void encodeURI(const char *uriDataIn, UriData_t uriDataOut, size_t &sizeofURIDataOut) {
rgrover1 284:82fefb5be8ed 284 sizeofURIDataOut = 0;
rgrover1 284:82fefb5be8ed 285 memset(uriDataOut, 0, sizeof(UriData_t));
rgrover1 284:82fefb5be8ed 286
rgrover1 284:82fefb5be8ed 287 if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) {
rgrover1 284:82fefb5be8ed 288 return;
rgrover1 284:82fefb5be8ed 289 }
rgrover1 284:82fefb5be8ed 290
rgrover1 284:82fefb5be8ed 291 /*
rgrover1 284:82fefb5be8ed 292 * handle prefix
rgrover1 284:82fefb5be8ed 293 */
rgrover1 284:82fefb5be8ed 294 const char *prefixes[] = {
rgrover1 284:82fefb5be8ed 295 "http://www.",
rgrover1 284:82fefb5be8ed 296 "https://www.",
rgrover1 284:82fefb5be8ed 297 "http://",
rgrover1 284:82fefb5be8ed 298 "https://",
rgrover1 284:82fefb5be8ed 299 "urn:uuid:"
rgrover1 284:82fefb5be8ed 300 };
rgrover1 284:82fefb5be8ed 301 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
rgrover1 284:82fefb5be8ed 302 for (unsigned i = 0; i < NUM_PREFIXES; i++) {
rgrover1 284:82fefb5be8ed 303 size_t prefixLen = strlen(prefixes[i]);
rgrover1 284:82fefb5be8ed 304 if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) {
rgrover1 284:82fefb5be8ed 305 uriDataOut[sizeofURIDataOut++] = i;
rgrover1 284:82fefb5be8ed 306 uriDataIn += prefixLen;
rgrover1 284:82fefb5be8ed 307 break;
rgrover1 284:82fefb5be8ed 308 }
rgrover1 284:82fefb5be8ed 309 }
rgrover1 284:82fefb5be8ed 310
rgrover1 284:82fefb5be8ed 311 /*
rgrover1 284:82fefb5be8ed 312 * handle suffixes
rgrover1 284:82fefb5be8ed 313 */
rgrover1 284:82fefb5be8ed 314 const char *suffixes[] = {
rgrover1 284:82fefb5be8ed 315 ".com/",
rgrover1 284:82fefb5be8ed 316 ".org/",
rgrover1 284:82fefb5be8ed 317 ".edu/",
rgrover1 284:82fefb5be8ed 318 ".net/",
rgrover1 284:82fefb5be8ed 319 ".info/",
rgrover1 284:82fefb5be8ed 320 ".biz/",
rgrover1 284:82fefb5be8ed 321 ".gov/",
rgrover1 284:82fefb5be8ed 322 ".com",
rgrover1 284:82fefb5be8ed 323 ".org",
rgrover1 284:82fefb5be8ed 324 ".edu",
rgrover1 284:82fefb5be8ed 325 ".net",
rgrover1 284:82fefb5be8ed 326 ".info",
rgrover1 284:82fefb5be8ed 327 ".biz",
rgrover1 284:82fefb5be8ed 328 ".gov"
rgrover1 284:82fefb5be8ed 329 };
rgrover1 284:82fefb5be8ed 330 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
rgrover1 284:82fefb5be8ed 331 while (*uriDataIn && (sizeofURIDataOut < URI_DATA_MAX)) {
rgrover1 284:82fefb5be8ed 332 /* check for suffix match */
rgrover1 284:82fefb5be8ed 333 unsigned i;
rgrover1 284:82fefb5be8ed 334 for (i = 0; i < NUM_SUFFIXES; i++) {
rgrover1 284:82fefb5be8ed 335 size_t suffixLen = strlen(suffixes[i]);
rgrover1 284:82fefb5be8ed 336 if (suffixLen == 0) {
rgrover1 284:82fefb5be8ed 337 continue;
rgrover1 284:82fefb5be8ed 338 }
rgrover1 284:82fefb5be8ed 339
rgrover1 284:82fefb5be8ed 340 if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) {
rgrover1 284:82fefb5be8ed 341 uriDataOut[sizeofURIDataOut++] = i;
rgrover1 284:82fefb5be8ed 342 uriDataIn += suffixLen;
rgrover1 284:82fefb5be8ed 343 break; /* from the for loop for checking against suffixes */
rgrover1 284:82fefb5be8ed 344 }
rgrover1 284:82fefb5be8ed 345 }
rgrover1 284:82fefb5be8ed 346 /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
rgrover1 284:82fefb5be8ed 347 if (i == NUM_SUFFIXES) {
rgrover1 284:82fefb5be8ed 348 uriDataOut[sizeofURIDataOut++] = *uriDataIn;
rgrover1 284:82fefb5be8ed 349 ++uriDataIn;
rgrover1 284:82fefb5be8ed 350 }
rgrover1 284:82fefb5be8ed 351 }
rgrover1 284:82fefb5be8ed 352 }
rgrover1 171:6092e61690dc 353 };
rgrover1 171:6092e61690dc 354
rgrover1 278:8a935a2d4a16 355 #endif // SERVICES_URIBEACONCONFIGSERVICE_H_