Fixes to BLE_API for mbedOS Alpha1 release. Mostly relating to doxygen fixes.
Fork of BLE_API by
services/URIBeaconConfigService.h@217:b48d47c39020, 2014-12-01 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Dec 01 14:31:19 2014 +0000
- Revision:
- 217:b48d47c39020
- Parent:
- 216:3f5e47361919
- Child:
- 218:8ae02569fab9
Synchronized with git rev d3cc9ed9
Author: Rohit Grover
white space diffs.
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 | 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 | 206:49646c933822 | 22 | #define UUID_INITIALIZER_LIST(FIRST, SECOND) { \ |
rgrover1 | 206:49646c933822 | 23 | 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ |
rgrover1 | 206:49646c933822 | 24 | 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \ |
rgrover1 | 171:6092e61690dc | 25 | } |
rgrover1 | 171:6092e61690dc | 26 | const uint8_t URIBeacon2ControlServiceUUID[] = UUID_INITIALIZER_LIST(0x20, 0x80); |
rgrover1 | 171:6092e61690dc | 27 | const uint8_t lockedStateCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x81); |
rgrover1 | 171:6092e61690dc | 28 | const uint8_t uriDataCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x84); |
rgrover1 | 171:6092e61690dc | 29 | const uint8_t flagsCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x85); |
rgrover1 | 178:b6d6a2a11e86 | 30 | const uint8_t txPowerLevelsCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x86); |
rgrover1 | 171:6092e61690dc | 31 | const uint8_t beaconPeriodCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x88); |
rgrover1 | 189:a23091b54ab5 | 32 | const uint8_t resetCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x89); |
rgrover1 | 171:6092e61690dc | 33 | |
rgrover1 | 207:e88130dc254c | 34 | class URIBeaconConfigService { |
rgrover1 | 180:afcd2f9c2ada | 35 | public: |
rgrover1 | 175:4e85f7225f8f | 36 | enum TXPowerModes_t { |
rgrover1 | 175:4e85f7225f8f | 37 | TX_POWER_MODE_LOWEST = 0, |
rgrover1 | 175:4e85f7225f8f | 38 | TX_POWER_MODE_LOW = 1, |
rgrover1 | 175:4e85f7225f8f | 39 | TX_POWER_MODE_MEDIUM = 2, |
rgrover1 | 175:4e85f7225f8f | 40 | TX_POWER_MODE_HIGH = 3, |
rgrover1 | 177:17f4b5924fe9 | 41 | NUM_POWER_MODES |
rgrover1 | 175:4e85f7225f8f | 42 | }; |
rgrover1 | 175:4e85f7225f8f | 43 | |
rgrover1 | 181:bbb6ce1082c3 | 44 | /** |
rgrover1 | 181:bbb6ce1082c3 | 45 | * @param[ref] ble |
rgrover1 | 181:bbb6ce1082c3 | 46 | * BLEDevice object for the underlying controller. |
rgrover1 | 208:88a3a58769bf | 47 | * @param[in] uridata |
rgrover1 | 181:bbb6ce1082c3 | 48 | * URI as a null-terminated string. |
rgrover1 | 181:bbb6ce1082c3 | 49 | * @param[in] flagsIn |
rgrover1 | 181:bbb6ce1082c3 | 50 | * UriBeacon Flags. |
rgrover1 | 181:bbb6ce1082c3 | 51 | * @param[in] effectiveTxPowerIn |
rgrover1 | 181:bbb6ce1082c3 | 52 | * UriBeacon Tx Power Level in dBm. |
rgrover1 | 181:bbb6ce1082c3 | 53 | * @param[in] beaconPeriodIn |
rgrover1 | 181:bbb6ce1082c3 | 54 | * The period in milliseconds that a UriBeacon packet is |
rgrover1 | 181:bbb6ce1082c3 | 55 | * transmitted. A value of zero disables UriBeacon |
rgrover1 | 181:bbb6ce1082c3 | 56 | * transmissions. |
rgrover1 | 181:bbb6ce1082c3 | 57 | */ |
rgrover1 | 217:b48d47c39020 | 58 | URIBeaconConfigService(BLEDevice &bleIn, |
rgrover1 | 217:b48d47c39020 | 59 | const char *uriDataIn, |
rgrover1 | 217:b48d47c39020 | 60 | uint8_t flagsIn = 0, |
rgrover1 | 217:b48d47c39020 | 61 | int8_t effectiveTxPowerIn = 0, |
rgrover1 | 217:b48d47c39020 | 62 | uint16_t beaconPeriodIn = 1000) : |
rgrover1 | 192:3fd3dcf49005 | 63 | ble(bleIn), |
rgrover1 | 192:3fd3dcf49005 | 64 | payloadIndex(0), |
rgrover1 | 192:3fd3dcf49005 | 65 | serviceDataPayload(), |
rgrover1 | 192:3fd3dcf49005 | 66 | initSucceeded(false), |
rgrover1 | 192:3fd3dcf49005 | 67 | lockedState(false), |
rgrover1 | 192:3fd3dcf49005 | 68 | uriDataLength(0), |
rgrover1 | 192:3fd3dcf49005 | 69 | uriData(), |
rgrover1 | 192:3fd3dcf49005 | 70 | flags(flagsIn), |
rgrover1 | 200:80dd3e50705c | 71 | effectiveTxPower(effectiveTxPowerIn), |
rgrover1 | 192:3fd3dcf49005 | 72 | powerLevels(), |
rgrover1 | 201:9bb7b3f45c20 | 73 | beaconPeriod(beaconPeriodIn), |
rgrover1 | 192:3fd3dcf49005 | 74 | lockedStateChar(lockedStateCharUUID, reinterpret_cast<uint8_t *>(&lockedState), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 215:f9d0169b9c28 | 75 | uriDataChar(uriDataCharUUID, uriData, MAX_SIZE_URI_DATA_CHAR_VALUE, MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 215:f9d0169b9c28 | 76 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 192:3fd3dcf49005 | 77 | flagsChar(flagsCharUUID, &flags, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 192:3fd3dcf49005 | 78 | txPowerLevelsChar(txPowerLevelsCharUUID, |
rgrover1 | 192:3fd3dcf49005 | 79 | reinterpret_cast<uint8_t *>(powerLevels), |
rgrover1 | 192:3fd3dcf49005 | 80 | NUM_POWER_MODES * sizeof(int8_t), |
rgrover1 | 192:3fd3dcf49005 | 81 | NUM_POWER_MODES * sizeof(int8_t), |
rgrover1 | 192:3fd3dcf49005 | 82 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 192:3fd3dcf49005 | 83 | beaconPeriodChar(beaconPeriodCharUUID, reinterpret_cast<uint8_t *>(&beaconPeriod), 2, 2, |
rgrover1 | 192:3fd3dcf49005 | 84 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 215:f9d0169b9c28 | 85 | resetChar(resetCharUUID, reinterpret_cast<uint8_t *>(&resetFlag), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) |
rgrover1 | 192:3fd3dcf49005 | 86 | { |
rgrover1 | 194:669eced4cd5e | 87 | if ((uriDataIn == NULL) || ((uriDataLength = strlen(uriDataIn)) == 0) || (uriDataLength > MAX_SIZE_URI_DATA_CHAR_VALUE)) { |
rgrover1 | 192:3fd3dcf49005 | 88 | return; |
rgrover1 | 192:3fd3dcf49005 | 89 | } |
rgrover1 | 194:669eced4cd5e | 90 | strcpy(reinterpret_cast<char *>(uriData), uriDataIn); |
rgrover1 | 192:3fd3dcf49005 | 91 | |
rgrover1 | 210:0c14030d1bd0 | 92 | configureGAP(); |
rgrover1 | 171:6092e61690dc | 93 | |
rgrover1 | 192:3fd3dcf49005 | 94 | GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar, &flagsChar, &txPowerLevelsChar, &beaconPeriodChar, &resetChar}; |
rgrover1 | 192:3fd3dcf49005 | 95 | GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 182:d16f8c11816b | 96 | |
rgrover1 | 192:3fd3dcf49005 | 97 | ble.addService(beaconControlService); |
rgrover1 | 207:e88130dc254c | 98 | ble.onDataWritten(this, &URIBeaconConfigService::onDataWritten); |
rgrover1 | 192:3fd3dcf49005 | 99 | } |
rgrover1 | 192:3fd3dcf49005 | 100 | |
rgrover1 | 193:ac2feceb7e87 | 101 | bool configuredSuccessfully(void) const { |
rgrover1 | 192:3fd3dcf49005 | 102 | return initSucceeded; |
rgrover1 | 171:6092e61690dc | 103 | } |
rgrover1 | 171:6092e61690dc | 104 | |
rgrover1 | 173:05c4b1cea65f | 105 | /** |
rgrover1 | 196:febbd68b1095 | 106 | * Please note that the following public APIs are offered to allow modifying |
rgrover1 | 196:febbd68b1095 | 107 | * the service programmatically. It is also possible to do so over BLE GATT |
rgrover1 | 196:febbd68b1095 | 108 | * transactions. |
rgrover1 | 196:febbd68b1095 | 109 | */ |
rgrover1 | 196:febbd68b1095 | 110 | public: |
rgrover1 | 196:febbd68b1095 | 111 | /** |
rgrover1 | 173:05c4b1cea65f | 112 | * Update flags of the URIBeacon dynamically. |
rgrover1 | 173:05c4b1cea65f | 113 | * |
rgrover1 | 173:05c4b1cea65f | 114 | * @param[in] flagsIn |
rgrover1 | 173:05c4b1cea65f | 115 | * |
rgrover1 | 173:05c4b1cea65f | 116 | * ### UriBeacon Flags |
rgrover1 | 173:05c4b1cea65f | 117 | * Bit | Description |
rgrover1 | 173:05c4b1cea65f | 118 | * :---- | :---------- |
rgrover1 | 173:05c4b1cea65f | 119 | * 0 | Invisible Hint |
rgrover1 | 173:05c4b1cea65f | 120 | * 1..7 | Reserved for future use. Must be zero. |
rgrover1 | 173:05c4b1cea65f | 121 | * |
rgrover1 | 173:05c4b1cea65f | 122 | * The `Invisible Hint` flag is a command for the user-agent that tells |
rgrover1 | 173:05c4b1cea65f | 123 | * it not to access or display the UriBeacon. This is a guideline only, |
rgrover1 | 173:05c4b1cea65f | 124 | * and is not a blocking method. User agents may, with user approval, |
rgrover1 | 173:05c4b1cea65f | 125 | * display invisible beacons. |
rgrover1 | 173:05c4b1cea65f | 126 | */ |
rgrover1 | 171:6092e61690dc | 127 | void setFlags(uint8_t flagsIn) { |
rgrover1 | 171:6092e61690dc | 128 | flags = flagsIn; |
rgrover1 | 210:0c14030d1bd0 | 129 | configureGAP(); |
rgrover1 | 211:f181effe5de3 | 130 | updateFlagsCharacteristic(); |
rgrover1 | 171:6092e61690dc | 131 | } |
rgrover1 | 171:6092e61690dc | 132 | |
rgrover1 | 177:17f4b5924fe9 | 133 | /** |
rgrover1 | 177:17f4b5924fe9 | 134 | * Update the txPower for a particular mode in the powerLevels table. |
rgrover1 | 177:17f4b5924fe9 | 135 | */ |
rgrover1 | 177:17f4b5924fe9 | 136 | void setTxPowerLevel(TXPowerModes_t mode, int8_t txPowerIn) { |
rgrover1 | 177:17f4b5924fe9 | 137 | powerLevels[mode] = txPowerIn; |
rgrover1 | 177:17f4b5924fe9 | 138 | } |
rgrover1 | 177:17f4b5924fe9 | 139 | |
rgrover1 | 177:17f4b5924fe9 | 140 | /** |
rgrover1 | 177:17f4b5924fe9 | 141 | * Set the effective power mode from one of the values in the powerLevels tables. |
rgrover1 | 177:17f4b5924fe9 | 142 | */ |
rgrover1 | 191:6710298104b0 | 143 | void useTxPowerMode(TXPowerModes_t mode) { |
rgrover1 | 200:80dd3e50705c | 144 | effectiveTxPower = powerLevels[mode]; |
rgrover1 | 210:0c14030d1bd0 | 145 | configureGAP(); |
rgrover1 | 171:6092e61690dc | 146 | } |
rgrover1 | 171:6092e61690dc | 147 | |
rgrover1 | 183:41d6ffadba96 | 148 | /** |
rgrover1 | 183:41d6ffadba96 | 149 | * The period in milliseconds that a UriBeacon packet is transmitted. |
rgrover1 | 183:41d6ffadba96 | 150 | * |
rgrover1 | 183:41d6ffadba96 | 151 | * @Note: A value of zero disables UriBeacon transmissions. |
rgrover1 | 183:41d6ffadba96 | 152 | */ |
rgrover1 | 171:6092e61690dc | 153 | void setBeaconPeriod(uint16_t beaconPeriodIn) { |
rgrover1 | 171:6092e61690dc | 154 | beaconPeriod = beaconPeriodIn; |
rgrover1 | 210:0c14030d1bd0 | 155 | configureGAP(); |
rgrover1 | 211:f181effe5de3 | 156 | updateBeaconPeriodCharacteristic(); |
rgrover1 | 171:6092e61690dc | 157 | } |
rgrover1 | 171:6092e61690dc | 158 | |
rgrover1 | 196:febbd68b1095 | 159 | private: |
rgrover1 | 186:ce34008cea95 | 160 | /** |
rgrover1 | 186:ce34008cea95 | 161 | * Setup the advertisement payload and GAP settings. |
rgrover1 | 186:ce34008cea95 | 162 | */ |
rgrover1 | 210:0c14030d1bd0 | 163 | void configureGAP(void) { |
rgrover1 | 171:6092e61690dc | 164 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 171:6092e61690dc | 165 | |
rgrover1 | 206:49646c933822 | 166 | payloadIndex = 0; |
rgrover1 | 171:6092e61690dc | 167 | serviceDataPayload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 171:6092e61690dc | 168 | serviceDataPayload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 171:6092e61690dc | 169 | serviceDataPayload[payloadIndex++] = flags; |
rgrover1 | 200:80dd3e50705c | 170 | serviceDataPayload[payloadIndex++] = effectiveTxPower; |
rgrover1 | 171:6092e61690dc | 171 | |
rgrover1 | 206:49646c933822 | 172 | const char *urlData = reinterpret_cast<char *>(uriData); |
rgrover1 | 206:49646c933822 | 173 | size_t sizeofURLData = uriDataLength; |
rgrover1 | 206:49646c933822 | 174 | size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData); |
rgrover1 | 171:6092e61690dc | 175 | |
rgrover1 | 171:6092e61690dc | 176 | ble.clearAdvertisingPayload(); |
rgrover1 | 171:6092e61690dc | 177 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 171:6092e61690dc | 178 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4); |
rgrover1 | 171:6092e61690dc | 179 | |
rgrover1 | 201:9bb7b3f45c20 | 180 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriod)); |
rgrover1 | 200:80dd3e50705c | 181 | ble.setTxPower(effectiveTxPower); |
rgrover1 | 171:6092e61690dc | 182 | } |
rgrover1 | 171:6092e61690dc | 183 | |
rgrover1 | 171:6092e61690dc | 184 | size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) { |
rgrover1 | 213:ac3b745af1d0 | 185 | if (!sizeofURLData) { |
rgrover1 | 213:ac3b745af1d0 | 186 | return 0; |
rgrover1 | 213:ac3b745af1d0 | 187 | } |
rgrover1 | 213:ac3b745af1d0 | 188 | |
rgrover1 | 171:6092e61690dc | 189 | const char *prefixes[] = { |
rgrover1 | 171:6092e61690dc | 190 | "http://www.", |
rgrover1 | 171:6092e61690dc | 191 | "https://www.", |
rgrover1 | 171:6092e61690dc | 192 | "http://", |
rgrover1 | 171:6092e61690dc | 193 | "https://", |
rgrover1 | 171:6092e61690dc | 194 | "urn:uuid:" |
rgrover1 | 171:6092e61690dc | 195 | }; |
rgrover1 | 171:6092e61690dc | 196 | |
rgrover1 | 206:49646c933822 | 197 | size_t encodedBytes = 0; |
rgrover1 | 171:6092e61690dc | 198 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 171:6092e61690dc | 199 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 171:6092e61690dc | 200 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 171:6092e61690dc | 201 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 171:6092e61690dc | 202 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 206:49646c933822 | 203 | encodedBytes = 1; |
rgrover1 | 171:6092e61690dc | 204 | |
rgrover1 | 206:49646c933822 | 205 | urldata += prefixLen; |
rgrover1 | 171:6092e61690dc | 206 | sizeofURLData -= prefixLen; |
rgrover1 | 171:6092e61690dc | 207 | break; |
rgrover1 | 171:6092e61690dc | 208 | } |
rgrover1 | 171:6092e61690dc | 209 | } |
rgrover1 | 171:6092e61690dc | 210 | |
rgrover1 | 171:6092e61690dc | 211 | return encodedBytes; |
rgrover1 | 171:6092e61690dc | 212 | } |
rgrover1 | 171:6092e61690dc | 213 | |
rgrover1 | 171:6092e61690dc | 214 | size_t encodeURI(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 171:6092e61690dc | 215 | const char *suffixes[] = { |
rgrover1 | 171:6092e61690dc | 216 | ".com/", |
rgrover1 | 171:6092e61690dc | 217 | ".org/", |
rgrover1 | 171:6092e61690dc | 218 | ".edu/", |
rgrover1 | 171:6092e61690dc | 219 | ".net/", |
rgrover1 | 171:6092e61690dc | 220 | ".info/", |
rgrover1 | 171:6092e61690dc | 221 | ".biz/", |
rgrover1 | 171:6092e61690dc | 222 | ".gov/", |
rgrover1 | 171:6092e61690dc | 223 | ".com", |
rgrover1 | 171:6092e61690dc | 224 | ".org", |
rgrover1 | 171:6092e61690dc | 225 | ".edu", |
rgrover1 | 171:6092e61690dc | 226 | ".net", |
rgrover1 | 171:6092e61690dc | 227 | ".info", |
rgrover1 | 171:6092e61690dc | 228 | ".biz", |
rgrover1 | 171:6092e61690dc | 229 | ".gov" |
rgrover1 | 171:6092e61690dc | 230 | }; |
rgrover1 | 171:6092e61690dc | 231 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 171:6092e61690dc | 232 | |
rgrover1 | 171:6092e61690dc | 233 | size_t encodedBytes = 0; |
rgrover1 | 171:6092e61690dc | 234 | while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) { |
rgrover1 | 171:6092e61690dc | 235 | /* check for suffix match */ |
rgrover1 | 171:6092e61690dc | 236 | unsigned i; |
rgrover1 | 171:6092e61690dc | 237 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 171:6092e61690dc | 238 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 171:6092e61690dc | 239 | if ((suffixLen == 0) || (sizeofURLData < suffixLen)) { |
rgrover1 | 171:6092e61690dc | 240 | continue; |
rgrover1 | 171:6092e61690dc | 241 | } |
rgrover1 | 171:6092e61690dc | 242 | |
rgrover1 | 171:6092e61690dc | 243 | if (strncmp(urldata, suffixes[i], suffixLen) == 0) { |
rgrover1 | 171:6092e61690dc | 244 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 171:6092e61690dc | 245 | ++encodedBytes; |
rgrover1 | 171:6092e61690dc | 246 | urldata += suffixLen; |
rgrover1 | 171:6092e61690dc | 247 | sizeofURLData -= suffixLen; |
rgrover1 | 171:6092e61690dc | 248 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 171:6092e61690dc | 249 | } |
rgrover1 | 171:6092e61690dc | 250 | } |
rgrover1 | 171:6092e61690dc | 251 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 171:6092e61690dc | 252 | if (i == NUM_SUFFIXES) { |
rgrover1 | 171:6092e61690dc | 253 | serviceDataPayload[payloadIndex++] = *urldata; |
rgrover1 | 171:6092e61690dc | 254 | ++encodedBytes; |
rgrover1 | 171:6092e61690dc | 255 | ++urldata; |
rgrover1 | 171:6092e61690dc | 256 | --sizeofURLData; |
rgrover1 | 171:6092e61690dc | 257 | } |
rgrover1 | 171:6092e61690dc | 258 | } |
rgrover1 | 192:3fd3dcf49005 | 259 | if (sizeofURLData == 0) { |
rgrover1 | 192:3fd3dcf49005 | 260 | initSucceeded = true; |
rgrover1 | 182:d16f8c11816b | 261 | } |
rgrover1 | 171:6092e61690dc | 262 | |
rgrover1 | 171:6092e61690dc | 263 | return encodedBytes; |
rgrover1 | 171:6092e61690dc | 264 | } |
rgrover1 | 171:6092e61690dc | 265 | |
rgrover1 | 203:7b5178d2fa03 | 266 | void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 185:7cd70497aec8 | 267 | if (params->charHandle == uriDataChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 268 | if (lockedState) { /* When locked, the device isn't allowed to update the uriData characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 269 | /* Restore GATT database with previous value. */ |
rgrover1 | 211:f181effe5de3 | 270 | updateURIDataCharacteristic(); |
rgrover1 | 185:7cd70497aec8 | 271 | return; |
rgrover1 | 185:7cd70497aec8 | 272 | } |
rgrover1 | 185:7cd70497aec8 | 273 | |
rgrover1 | 185:7cd70497aec8 | 274 | /* We don't handle very large writes at the moment. */ |
rgrover1 | 185:7cd70497aec8 | 275 | if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) { |
rgrover1 | 185:7cd70497aec8 | 276 | return; |
rgrover1 | 185:7cd70497aec8 | 277 | } |
rgrover1 | 185:7cd70497aec8 | 278 | |
rgrover1 | 185:7cd70497aec8 | 279 | uriDataLength = params->len; |
rgrover1 | 187:057c547facd2 | 280 | memcpy(uriData, params->data, uriDataLength); |
rgrover1 | 185:7cd70497aec8 | 281 | } else if (params->charHandle == flagsChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 282 | if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 283 | /* Restore GATT database with previous value. */ |
rgrover1 | 211:f181effe5de3 | 284 | updateFlagsCharacteristic(); |
rgrover1 | 185:7cd70497aec8 | 285 | return; |
rgrover1 | 185:7cd70497aec8 | 286 | } else { |
rgrover1 | 185:7cd70497aec8 | 287 | flags = *(params->data); |
rgrover1 | 185:7cd70497aec8 | 288 | } |
rgrover1 | 185:7cd70497aec8 | 289 | } else if (params->charHandle == txPowerLevelsChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 290 | if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 291 | /* Restore GATT database with previous value. */ |
rgrover1 | 185:7cd70497aec8 | 292 | ble.updateCharacteristicValue(txPowerLevelsChar.getValueAttribute().getHandle(), reinterpret_cast<uint8_t *>(powerLevels), NUM_POWER_MODES * sizeof(int8_t)); |
rgrover1 | 185:7cd70497aec8 | 293 | return; |
rgrover1 | 185:7cd70497aec8 | 294 | } else { |
rgrover1 | 185:7cd70497aec8 | 295 | memcpy(powerLevels, params->data, NUM_POWER_MODES * sizeof(int8_t)); |
rgrover1 | 185:7cd70497aec8 | 296 | } |
rgrover1 | 185:7cd70497aec8 | 297 | } else if (params->charHandle == beaconPeriodChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 298 | if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 299 | /* Restore GATT database with previous value. */ |
rgrover1 | 211:f181effe5de3 | 300 | updateBeaconPeriodCharacteristic(); |
rgrover1 | 185:7cd70497aec8 | 301 | return; |
rgrover1 | 185:7cd70497aec8 | 302 | } else { |
rgrover1 | 185:7cd70497aec8 | 303 | beaconPeriod = *((uint16_t *)(params->data)); |
rgrover1 | 185:7cd70497aec8 | 304 | } |
rgrover1 | 189:a23091b54ab5 | 305 | } else if (params->charHandle == resetChar.getValueAttribute().getHandle()) { |
rgrover1 | 200:80dd3e50705c | 306 | resetDefaults(); |
rgrover1 | 185:7cd70497aec8 | 307 | } |
rgrover1 | 210:0c14030d1bd0 | 308 | configureGAP(); |
rgrover1 | 185:7cd70497aec8 | 309 | ble.setAdvertisingPayload(); |
rgrover1 | 185:7cd70497aec8 | 310 | } |
rgrover1 | 185:7cd70497aec8 | 311 | |
rgrover1 | 200:80dd3e50705c | 312 | void resetDefaults(void) { |
rgrover1 | 216:3f5e47361919 | 313 | lockedState = false; |
rgrover1 | 212:852d45a52016 | 314 | uriDataLength = 0; |
rgrover1 | 216:3f5e47361919 | 315 | memset(uriData, 0, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 212:852d45a52016 | 316 | flags = 0; |
rgrover1 | 212:852d45a52016 | 317 | effectiveTxPower = 0; |
rgrover1 | 216:3f5e47361919 | 318 | memset(powerLevels, 0, sizeof(powerLevels)); |
rgrover1 | 212:852d45a52016 | 319 | beaconPeriod = 0; |
rgrover1 | 189:a23091b54ab5 | 320 | |
rgrover1 | 211:f181effe5de3 | 321 | updateGATT(); |
rgrover1 | 211:f181effe5de3 | 322 | } |
rgrover1 | 211:f181effe5de3 | 323 | |
rgrover1 | 211:f181effe5de3 | 324 | void updateGATT(void) { |
rgrover1 | 214:7d79fa197212 | 325 | updateLockedStateCharacteristic(); |
rgrover1 | 211:f181effe5de3 | 326 | updateURIDataCharacteristic(); |
rgrover1 | 211:f181effe5de3 | 327 | updateFlagsCharacteristic(); |
rgrover1 | 211:f181effe5de3 | 328 | updateBeaconPeriodCharacteristic(); |
rgrover1 | 211:f181effe5de3 | 329 | } |
rgrover1 | 211:f181effe5de3 | 330 | |
rgrover1 | 214:7d79fa197212 | 331 | void updateLockedStateCharacteristic(void) { |
rgrover1 | 214:7d79fa197212 | 332 | ble.updateCharacteristicValue(lockedStateChar.getValueAttribute().getHandle(), reinterpret_cast<uint8_t *>(&lockedState), sizeof(lockedState)); |
rgrover1 | 214:7d79fa197212 | 333 | } |
rgrover1 | 214:7d79fa197212 | 334 | |
rgrover1 | 211:f181effe5de3 | 335 | void updateURIDataCharacteristic(void) { |
rgrover1 | 189:a23091b54ab5 | 336 | ble.updateCharacteristicValue(uriDataChar.getValueAttribute().getHandle(), uriData, uriDataLength); |
rgrover1 | 211:f181effe5de3 | 337 | } |
rgrover1 | 211:f181effe5de3 | 338 | |
rgrover1 | 211:f181effe5de3 | 339 | void updateFlagsCharacteristic(void) { |
rgrover1 | 189:a23091b54ab5 | 340 | ble.updateCharacteristicValue(flagsChar.getValueAttribute().getHandle(), &flags, 1 /* size */); |
rgrover1 | 211:f181effe5de3 | 341 | } |
rgrover1 | 211:f181effe5de3 | 342 | |
rgrover1 | 211:f181effe5de3 | 343 | void updateBeaconPeriodCharacteristic(void) { |
rgrover1 | 189:a23091b54ab5 | 344 | ble.updateCharacteristicValue(beaconPeriodChar.getValueAttribute().getHandle(), reinterpret_cast<uint8_t *>(&beaconPeriod), sizeof(uint16_t)); |
rgrover1 | 189:a23091b54ab5 | 345 | } |
rgrover1 | 189:a23091b54ab5 | 346 | |
rgrover1 | 185:7cd70497aec8 | 347 | private: |
rgrover1 | 185:7cd70497aec8 | 348 | /** |
rgrover1 | 185:7cd70497aec8 | 349 | * For debugging only. |
rgrover1 | 185:7cd70497aec8 | 350 | */ |
rgrover1 | 185:7cd70497aec8 | 351 | void dumpEncodedSeviceData() const { |
rgrover1 | 185:7cd70497aec8 | 352 | printf("encoded: '"); |
rgrover1 | 185:7cd70497aec8 | 353 | for (unsigned i = 0; i < payloadIndex; i++) { |
rgrover1 | 185:7cd70497aec8 | 354 | printf(" %02x", serviceDataPayload[i]); |
rgrover1 | 185:7cd70497aec8 | 355 | } |
rgrover1 | 185:7cd70497aec8 | 356 | printf("'\r\n"); |
rgrover1 | 185:7cd70497aec8 | 357 | } |
rgrover1 | 185:7cd70497aec8 | 358 | |
rgrover1 | 171:6092e61690dc | 359 | private: |
rgrover1 | 199:6b57874115f6 | 360 | static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 18; /* Uri Data must be between 0 and 18 bytes in length. */ |
rgrover1 | 204:479f81fd5d3b | 361 | static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; /* This is chosen arbitrarily. It should be large enough |
rgrover1 | 204:479f81fd5d3b | 362 | * to hold any reasonable uncompressed URI. */ |
rgrover1 | 171:6092e61690dc | 363 | |
rgrover1 | 171:6092e61690dc | 364 | private: |
rgrover1 | 206:49646c933822 | 365 | BLEDevice &ble; |
rgrover1 | 171:6092e61690dc | 366 | |
rgrover1 | 206:49646c933822 | 367 | size_t payloadIndex; |
rgrover1 | 206:49646c933822 | 368 | uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD]; |
rgrover1 | 206:49646c933822 | 369 | bool initSucceeded; |
rgrover1 | 182:d16f8c11816b | 370 | |
rgrover1 | 206:49646c933822 | 371 | bool lockedState; |
rgrover1 | 206:49646c933822 | 372 | uint16_t uriDataLength; |
rgrover1 | 206:49646c933822 | 373 | uint8_t uriData[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 206:49646c933822 | 374 | uint8_t flags; |
rgrover1 | 206:49646c933822 | 375 | int8_t effectiveTxPower; |
rgrover1 | 206:49646c933822 | 376 | int8_t powerLevels[NUM_POWER_MODES]; |
rgrover1 | 206:49646c933822 | 377 | uint16_t beaconPeriod; |
rgrover1 | 206:49646c933822 | 378 | bool resetFlag; |
rgrover1 | 171:6092e61690dc | 379 | |
rgrover1 | 206:49646c933822 | 380 | GattCharacteristic lockedStateChar; |
rgrover1 | 206:49646c933822 | 381 | GattCharacteristic uriDataChar; |
rgrover1 | 206:49646c933822 | 382 | GattCharacteristic flagsChar; |
rgrover1 | 206:49646c933822 | 383 | GattCharacteristic txPowerLevelsChar; |
rgrover1 | 206:49646c933822 | 384 | GattCharacteristic beaconPeriodChar; |
rgrover1 | 206:49646c933822 | 385 | GattCharacteristic resetChar; |
rgrover1 | 171:6092e61690dc | 386 | }; |
rgrover1 | 171:6092e61690dc | 387 | |
rgrover1 | 207:e88130dc254c | 388 | #endif /* #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__*/ |