Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.
Fork of BLE_API by
services/URIBeaconConfigService.h@285:984353e66610, 2015-02-13 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Feb 13 13:00:41 2015 +0000
- Revision:
- 285:984353e66610
- Parent:
- 284:82fefb5be8ed
- Child:
- 286:898ff71b9502
Synchronized with git rev 94676781
Author: Rohit Grover
update comment block for the constructor of URIBeacon.
Who changed what in which revision?
User | Revision | Line number | New 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 | 285:984353e66610 | 75 | * @param[ref] ble |
rgrover1 | 285:984353e66610 | 76 | * BLEDevice object for the underlying controller. |
rgrover1 | 285:984353e66610 | 77 | * @param[in/out] paramsIn |
rgrover1 | 285:984353e66610 | 78 | * Reference to application-visible beacon state, loaded |
rgrover1 | 285:984353e66610 | 79 | * from persistent storage at startup. |
rgrover1 | 285:984353e66610 | 80 | * @param[in] resetToDefaultsFlag |
rgrover1 | 285:984353e66610 | 81 | * reset params state to the defaults. |
rgrover1 | 285:984353e66610 | 82 | * @param[in] defaultUriDataIn |
rgrover1 | 285:984353e66610 | 83 | * Default encoded URIData; applies only if the resetToDefaultsFlag is true. |
rgrover1 | 285:984353e66610 | 84 | * @param[in] defaultUriDataLengthIn |
rgrover1 | 285:984353e66610 | 85 | * Length of the default encoded URIData (from above); applies only if the resetToDefaultsFlag is true. |
rgrover1 | 285:984353e66610 | 86 | * @param[in] defaultAdvPowerLevelsIn |
rgrover1 | 285:984353e66610 | 87 | * Default power-levels array; applies only if the resetToDefaultsFlag is true. |
rgrover1 | 181:bbb6ce1082c3 | 88 | */ |
rgrover1 | 283:bef53917ea82 | 89 | URIBeaconConfigService(BLEDevice &bleIn, |
rgrover1 | 283:bef53917ea82 | 90 | Params_t ¶msIn, |
rgrover1 | 283:bef53917ea82 | 91 | bool resetToDefaultsFlag, |
rgrover1 | 283:bef53917ea82 | 92 | UriData_t &defaultUriDataIn, |
rgrover1 | 283:bef53917ea82 | 93 | int defaultUriDataLengthIn, |
rgrover1 | 283:bef53917ea82 | 94 | PowerLevels_t &defaultAdvPowerLevelsIn) : |
rgrover1 | 192:3fd3dcf49005 | 95 | ble(bleIn), |
rgrover1 | 278:8a935a2d4a16 | 96 | params(paramsIn), |
rgrover1 | 278:8a935a2d4a16 | 97 | defaultUriDataLength(defaultUriDataLengthIn), |
rgrover1 | 278:8a935a2d4a16 | 98 | defaultUriData(defaultUriDataIn), |
rgrover1 | 278:8a935a2d4a16 | 99 | defaultAdvPowerLevels(defaultAdvPowerLevelsIn), |
rgrover1 | 192:3fd3dcf49005 | 100 | initSucceeded(false), |
rgrover1 | 278:8a935a2d4a16 | 101 | lockedStateChar(UUID_LOCK_STATE_CHAR, &lockedState), |
rgrover1 | 278:8a935a2d4a16 | 102 | lockChar(UUID_LOCK_CHAR, ¶ms.lock), |
rgrover1 | 278:8a935a2d4a16 | 103 | uriDataChar(UUID_URI_DATA_CHAR, params.uriData, URI_DATA_MAX, params.uriDataLength, |
rgrover1 | 278:8a935a2d4a16 | 104 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 278:8a935a2d4a16 | 105 | unlockChar(UUID_UNLOCK_CHAR, ¶ms.lock), |
rgrover1 | 278:8a935a2d4a16 | 106 | flagsChar(UUID_FLAGS_CHAR, ¶ms.flags), |
rgrover1 | 278:8a935a2d4a16 | 107 | advPowerLevelsChar(UUID_ADV_POWER_LEVELS_CHAR, ¶ms.advPowerLevels), |
rgrover1 | 278:8a935a2d4a16 | 108 | txPowerModeChar(UUID_TX_POWER_MODE_CHAR, ¶ms.txPowerMode), |
rgrover1 | 278:8a935a2d4a16 | 109 | beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, ¶ms.beaconPeriod), |
rgrover1 | 278:8a935a2d4a16 | 110 | resetChar(UUID_RESET_CHAR, &resetFlag) { |
rgrover1 | 278:8a935a2d4a16 | 111 | if (defaultUriDataLengthIn > URI_DATA_MAX) { |
rgrover1 | 192:3fd3dcf49005 | 112 | return; |
rgrover1 | 192:3fd3dcf49005 | 113 | } |
rgrover1 | 192:3fd3dcf49005 | 114 | |
rgrover1 | 278:8a935a2d4a16 | 115 | if (params.uriDataLength > URI_DATA_MAX) { |
rgrover1 | 278:8a935a2d4a16 | 116 | resetToDefaultsFlag = true; |
rgrover1 | 218:8ae02569fab9 | 117 | } |
rgrover1 | 218:8ae02569fab9 | 118 | |
rgrover1 | 278:8a935a2d4a16 | 119 | lockedState = isLocked(); |
rgrover1 | 171:6092e61690dc | 120 | |
rgrover1 | 278:8a935a2d4a16 | 121 | if (resetToDefaultsFlag) { |
rgrover1 | 278:8a935a2d4a16 | 122 | resetToDefaults(); |
rgrover1 | 278:8a935a2d4a16 | 123 | } else { |
rgrover1 | 278:8a935a2d4a16 | 124 | updateCharacteristicValues(); |
rgrover1 | 278:8a935a2d4a16 | 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 | 278:8a935a2d4a16 | 131 | advPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); |
rgrover1 | 278:8a935a2d4a16 | 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 | 278:8a935a2d4a16 | 136 | static GattCharacteristic *charTable[] = { |
rgrover1 | 278:8a935a2d4a16 | 137 | &lockedStateChar, &lockChar, &unlockChar, &uriDataChar, |
rgrover1 | 278:8a935a2d4a16 | 138 | &flagsChar, &advPowerLevelsChar, &txPowerModeChar, &beaconPeriodChar, &resetChar |
rgrover1 | 278:8a935a2d4a16 | 139 | }; |
rgrover1 | 182:d16f8c11816b | 140 | |
rgrover1 | 278:8a935a2d4a16 | 141 | GattService configService(UUID_URI_BEACON_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 278:8a935a2d4a16 | 142 | |
rgrover1 | 279:00649389c020 | 143 | ble.addService(configService); |
rgrover1 | 278:8a935a2d4a16 | 144 | ble.onDataWritten(this, &URIBeaconConfigService::onDataWrittenCallback); |
rgrover1 | 278:8a935a2d4a16 | 145 | |
rgrover1 | 278:8a935a2d4a16 | 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 | 171:6092e61690dc | 153 | |
rgrover1 | 278:8a935a2d4a16 | 154 | private: |
rgrover1 | 282:2c490f373b9b | 155 | // True if the lock bits are non-zero |
rgrover1 | 278:8a935a2d4a16 | 156 | bool isLocked() { |
rgrover1 | 278:8a935a2d4a16 | 157 | Lock_t testLock; |
rgrover1 | 278:8a935a2d4a16 | 158 | memset(testLock, 0, sizeof(Lock_t)); |
rgrover1 | 278:8a935a2d4a16 | 159 | return memcmp(params.lock, testLock, sizeof(Lock_t)); |
rgrover1 | 171:6092e61690dc | 160 | } |
rgrover1 | 171:6092e61690dc | 161 | |
mbedAustin | 232:4cfb5b8a4fb9 | 162 | /* |
rgrover1 | 242:0e9201b67e2f | 163 | * This callback is invoked when a GATT client attempts to modify any of the |
rgrover1 | 250:6862d374e613 | 164 | * characteristics of this service. Attempts to do so are also applied to |
rgrover1 | 250:6862d374e613 | 165 | * the internal state of this service object. |
rgrover1 | 242:0e9201b67e2f | 166 | */ |
rgrover1 | 278:8a935a2d4a16 | 167 | void onDataWrittenCallback(const GattCharacteristicWriteCBParams *writeParams) { |
rgrover1 | 278:8a935a2d4a16 | 168 | uint16_t handle = writeParams->charHandle; |
rgrover1 | 278:8a935a2d4a16 | 169 | |
rgrover1 | 268:bc525cdc88f7 | 170 | if (handle == lockChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 171 | // Validated earlier |
rgrover1 | 278:8a935a2d4a16 | 172 | memcpy(params.lock, writeParams->data, sizeof(Lock_t)); |
rgrover1 | 282:2c490f373b9b | 173 | // use isLocked() in case bits are being set to all 0's |
rgrover1 | 282:2c490f373b9b | 174 | lockedState = isLocked(); |
rgrover1 | 268:bc525cdc88f7 | 175 | } else if (handle == unlockChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 176 | // Validated earlier |
rgrover1 | 278:8a935a2d4a16 | 177 | memset(params.lock, 0, sizeof(Lock_t)); |
rgrover1 | 268:bc525cdc88f7 | 178 | lockedState = false; |
rgrover1 | 268:bc525cdc88f7 | 179 | } else if (handle == uriDataChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 180 | params.uriDataLength = writeParams->len; |
rgrover1 | 278:8a935a2d4a16 | 181 | memcpy(params.uriData, writeParams->data, params.uriDataLength); |
rgrover1 | 229:6664b6c0e92b | 182 | } else if (handle == flagsChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 183 | params.flags = *(writeParams->data); |
rgrover1 | 278:8a935a2d4a16 | 184 | } else if (handle == advPowerLevelsChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 185 | memcpy(params.advPowerLevels, writeParams->data, sizeof(PowerLevels_t)); |
rgrover1 | 229:6664b6c0e92b | 186 | } else if (handle == txPowerModeChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 187 | params.txPowerMode = *(writeParams->data); |
rgrover1 | 229:6664b6c0e92b | 188 | } else if (handle == beaconPeriodChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 189 | params.beaconPeriod = *((uint16_t *)(writeParams->data)); |
rgrover1 | 229:6664b6c0e92b | 190 | } else if (handle == resetChar.getValueHandle()) { |
rgrover1 | 278:8a935a2d4a16 | 191 | resetToDefaults(); |
rgrover1 | 185:7cd70497aec8 | 192 | } |
rgrover1 | 185:7cd70497aec8 | 193 | } |
rgrover1 | 185:7cd70497aec8 | 194 | |
mbedAustin | 232:4cfb5b8a4fb9 | 195 | /* |
rgrover1 | 251:6ce809c972bb | 196 | * Reset the default values. |
rgrover1 | 242:0e9201b67e2f | 197 | */ |
rgrover1 | 278:8a935a2d4a16 | 198 | void resetToDefaults(void) { |
rgrover1 | 278:8a935a2d4a16 | 199 | lockedState = false; |
rgrover1 | 278:8a935a2d4a16 | 200 | memset(params.lock, 0, sizeof(Lock_t)); |
rgrover1 | 278:8a935a2d4a16 | 201 | memcpy(params.uriData, defaultUriData, URI_DATA_MAX); |
rgrover1 | 278:8a935a2d4a16 | 202 | params.uriDataLength = defaultUriDataLength; |
rgrover1 | 278:8a935a2d4a16 | 203 | params.flags = 0; |
rgrover1 | 278:8a935a2d4a16 | 204 | memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t)); |
rgrover1 | 278:8a935a2d4a16 | 205 | params.txPowerMode = TX_POWER_MODE_LOW; |
rgrover1 | 278:8a935a2d4a16 | 206 | params.beaconPeriod = 1000; |
rgrover1 | 278:8a935a2d4a16 | 207 | updateCharacteristicValues(); |
rgrover1 | 211:f181effe5de3 | 208 | } |
rgrover1 | 211:f181effe5de3 | 209 | |
mbedAustin | 232:4cfb5b8a4fb9 | 210 | /* |
rgrover1 | 242:0e9201b67e2f | 211 | * Internal helper function used to update the GATT database following any |
rgrover1 | 242:0e9201b67e2f | 212 | * change to the internal state of the service object. |
rgrover1 | 242:0e9201b67e2f | 213 | */ |
rgrover1 | 278:8a935a2d4a16 | 214 | void updateCharacteristicValues(void) { |
rgrover1 | 278:8a935a2d4a16 | 215 | ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), &lockedState, 1); |
rgrover1 | 278:8a935a2d4a16 | 216 | ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength); |
rgrover1 | 279:00649389c020 | 217 | ble.updateCharacteristicValue(flagsChar.getValueHandle(), ¶ms.flags, 1); |
rgrover1 | 278:8a935a2d4a16 | 218 | ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), |
rgrover1 | 279:00649389c020 | 219 | reinterpret_cast<uint8_t *>(¶ms.beaconPeriod), sizeof(uint16_t)); |
rgrover1 | 278:8a935a2d4a16 | 220 | ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), ¶ms.txPowerMode, 1); |
rgrover1 | 278:8a935a2d4a16 | 221 | ble.updateCharacteristicValue(advPowerLevelsChar.getValueHandle(), |
rgrover1 | 279:00649389c020 | 222 | reinterpret_cast<uint8_t *>(params.advPowerLevels), sizeof(PowerLevels_t)); |
rgrover1 | 189:a23091b54ab5 | 223 | } |
rgrover1 | 189:a23091b54ab5 | 224 | |
rgrover1 | 278:8a935a2d4a16 | 225 | private: |
rgrover1 | 278:8a935a2d4a16 | 226 | void lockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 278:8a935a2d4a16 | 227 | authParams->authorizationReply = (authParams->len == sizeof(Lock_t)) && !lockedState; |
rgrover1 | 218:8ae02569fab9 | 228 | } |
rgrover1 | 218:8ae02569fab9 | 229 | |
rgrover1 | 268:bc525cdc88f7 | 230 | |
rgrover1 | 278:8a935a2d4a16 | 231 | void unlockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 278:8a935a2d4a16 | 232 | if (!lockedState || (authParams->len == sizeof(Lock_t) && (memcmp(authParams->data, params.lock, sizeof(Lock_t)) == 0))) { |
rgrover1 | 278:8a935a2d4a16 | 233 | authParams->authorizationReply = true; |
rgrover1 | 268:bc525cdc88f7 | 234 | } else { |
rgrover1 | 278:8a935a2d4a16 | 235 | authParams->authorizationReply = false; |
rgrover1 | 268:bc525cdc88f7 | 236 | } |
rgrover1 | 268:bc525cdc88f7 | 237 | } |
rgrover1 | 268:bc525cdc88f7 | 238 | |
rgrover1 | 278:8a935a2d4a16 | 239 | void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 278:8a935a2d4a16 | 240 | if (lockedState || (authParams->offset != 0) || (authParams->len > URI_DATA_MAX)) { |
rgrover1 | 278:8a935a2d4a16 | 241 | authParams->authorizationReply = false; |
rgrover1 | 250:6862d374e613 | 242 | } |
rgrover1 | 250:6862d374e613 | 243 | } |
rgrover1 | 250:6862d374e613 | 244 | |
rgrover1 | 278:8a935a2d4a16 | 245 | void flagsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 278:8a935a2d4a16 | 246 | if (lockedState || authParams->len != 1) { |
rgrover1 | 278:8a935a2d4a16 | 247 | authParams->authorizationReply = false; |
rgrover1 | 250:6862d374e613 | 248 | } |
rgrover1 | 250:6862d374e613 | 249 | } |
rgrover1 | 250:6862d374e613 | 250 | |
rgrover1 | 278:8a935a2d4a16 | 251 | void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 250:6862d374e613 | 252 | if (lockedState) { |
rgrover1 | 278:8a935a2d4a16 | 253 | authParams->authorizationReply = false; |
rgrover1 | 250:6862d374e613 | 254 | } |
rgrover1 | 250:6862d374e613 | 255 | } |
rgrover1 | 250:6862d374e613 | 256 | |
rgrover1 | 283:bef53917ea82 | 257 | BLEDevice &ble; |
rgrover1 | 283:bef53917ea82 | 258 | Params_t ¶ms; |
rgrover1 | 278:8a935a2d4a16 | 259 | // Default value that is restored on reset |
rgrover1 | 283:bef53917ea82 | 260 | uint16_t defaultUriDataLength; |
rgrover1 | 283:bef53917ea82 | 261 | UriData_t &defaultUriData; |
rgrover1 | 278:8a935a2d4a16 | 262 | // Default value that is restored on reset |
rgrover1 | 283:bef53917ea82 | 263 | PowerLevels_t &defaultAdvPowerLevels; |
rgrover1 | 283:bef53917ea82 | 264 | uint8_t lockedState; |
rgrover1 | 283:bef53917ea82 | 265 | bool initSucceeded; |
rgrover1 | 283:bef53917ea82 | 266 | uint8_t resetFlag; |
rgrover1 | 171:6092e61690dc | 267 | |
rgrover1 | 283:bef53917ea82 | 268 | ReadOnlyGattCharacteristic<uint8_t> lockedStateChar; |
rgrover1 | 283:bef53917ea82 | 269 | WriteOnlyGattCharacteristic<Lock_t> lockChar; |
rgrover1 | 283:bef53917ea82 | 270 | GattCharacteristic uriDataChar; |
rgrover1 | 283:bef53917ea82 | 271 | WriteOnlyGattCharacteristic<Lock_t> unlockChar; |
rgrover1 | 283:bef53917ea82 | 272 | ReadWriteGattCharacteristic<uint8_t> flagsChar; |
rgrover1 | 278:8a935a2d4a16 | 273 | ReadWriteGattCharacteristic<PowerLevels_t> advPowerLevelsChar; |
rgrover1 | 283:bef53917ea82 | 274 | ReadWriteGattCharacteristic<uint8_t> txPowerModeChar; |
rgrover1 | 283:bef53917ea82 | 275 | ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar; |
rgrover1 | 283:bef53917ea82 | 276 | WriteOnlyGattCharacteristic<uint8_t> resetChar; |
rgrover1 | 284:82fefb5be8ed | 277 | |
rgrover1 | 284:82fefb5be8ed | 278 | public: |
rgrover1 | 284:82fefb5be8ed | 279 | /* |
rgrover1 | 284:82fefb5be8ed | 280 | * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification). |
rgrover1 | 284:82fefb5be8ed | 281 | */ |
rgrover1 | 284:82fefb5be8ed | 282 | static void encodeURI(const char *uriDataIn, UriData_t uriDataOut, size_t &sizeofURIDataOut) { |
rgrover1 | 284:82fefb5be8ed | 283 | sizeofURIDataOut = 0; |
rgrover1 | 284:82fefb5be8ed | 284 | memset(uriDataOut, 0, sizeof(UriData_t)); |
rgrover1 | 284:82fefb5be8ed | 285 | |
rgrover1 | 284:82fefb5be8ed | 286 | if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) { |
rgrover1 | 284:82fefb5be8ed | 287 | return; |
rgrover1 | 284:82fefb5be8ed | 288 | } |
rgrover1 | 284:82fefb5be8ed | 289 | |
rgrover1 | 284:82fefb5be8ed | 290 | /* |
rgrover1 | 284:82fefb5be8ed | 291 | * handle prefix |
rgrover1 | 284:82fefb5be8ed | 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 | 284:82fefb5be8ed | 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 | 284:82fefb5be8ed | 303 | if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) { |
rgrover1 | 284:82fefb5be8ed | 304 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 284:82fefb5be8ed | 305 | uriDataIn += prefixLen; |
rgrover1 | 284:82fefb5be8ed | 306 | break; |
rgrover1 | 284:82fefb5be8ed | 307 | } |
rgrover1 | 284:82fefb5be8ed | 308 | } |
rgrover1 | 284:82fefb5be8ed | 309 | |
rgrover1 | 284:82fefb5be8ed | 310 | /* |
rgrover1 | 284:82fefb5be8ed | 311 | * handle suffixes |
rgrover1 | 284:82fefb5be8ed | 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 | 284:82fefb5be8ed | 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 | 284:82fefb5be8ed | 335 | if (suffixLen == 0) { |
rgrover1 | 284:82fefb5be8ed | 336 | continue; |
rgrover1 | 284:82fefb5be8ed | 337 | } |
rgrover1 | 284:82fefb5be8ed | 338 | |
rgrover1 | 284:82fefb5be8ed | 339 | if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) { |
rgrover1 | 284:82fefb5be8ed | 340 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 284:82fefb5be8ed | 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 | 284:82fefb5be8ed | 347 | uriDataOut[sizeofURIDataOut++] = *uriDataIn; |
rgrover1 | 284:82fefb5be8ed | 348 | ++uriDataIn; |
rgrover1 | 284:82fefb5be8ed | 349 | } |
rgrover1 | 284:82fefb5be8ed | 350 | } |
rgrover1 | 284:82fefb5be8ed | 351 | } |
rgrover1 | 171:6092e61690dc | 352 | }; |
rgrover1 | 171:6092e61690dc | 353 | |
rgrover1 | 278:8a935a2d4a16 | 354 | #endif // SERVICES_URIBEACONCONFIGSERVICE_H_ |