I don't know why this is happening.

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Mon Mar 02 11:50:48 2015 +0000
Revision:
300:d9a39f759a6a
Parent:
287:cca148e535b1
Child:
309:bc91f7ba346d
Synchronized with git rev 91b4b8f8
Author: Rohit Grover
initialize the min/max length values correctly in the constructor for URIDataChar.
Previously, the minLength value was always set to URI_DATA_MAX, whereas maxLen got initialized by the values stored persistently. Except for the case of default initialization, the value of maxLength would always be less than then minLength; causing improper behaviour from the underlying stack while setting up characterisitics.

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