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/URIBeacon2Service.h@199:6b57874115f6, 2014-12-01 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Dec 01 14:31:17 2014 +0000
- Revision:
- 199:6b57874115f6
- Parent:
- 196:febbd68b1095
- Child:
- 200:80dd3e50705c
Synchronized with git rev 6e85b53f
Author: Rohit Grover
Uri Data must be between 0 and 18 bytes
https://github.com/mbedmicro/BLE_API/issues/6
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 | 171:6092e61690dc | 17 | #ifndef __BLE_URI_BEACON_2_SERVICE_H__ |
rgrover1 | 171:6092e61690dc | 18 | #define __BLE_URI_BEACON_2_SERVICE_H__ |
rgrover1 | 171:6092e61690dc | 19 | |
rgrover1 | 171:6092e61690dc | 20 | #include "BLEDevice.h" |
rgrover1 | 171:6092e61690dc | 21 | |
rgrover1 | 171:6092e61690dc | 22 | #define UUID_INITIALIZER_LIST(FIRST, SECOND) { \ |
rgrover1 | 171:6092e61690dc | 23 | 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ |
rgrover1 | 171:6092e61690dc | 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 | 171:6092e61690dc | 34 | class URIBeacon2Service { |
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 | 181:bbb6ce1082c3 | 47 | * @param[in] urldata |
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 | 192:3fd3dcf49005 | 58 | URIBeacon2Service(BLEDevice &bleIn, const char *uriDataIn, uint8_t flagsIn = 0, int8_t effectiveTxPowerIn = 0, uint16_t beaconPeriodIn = 1000) : |
rgrover1 | 192:3fd3dcf49005 | 59 | ble(bleIn), |
rgrover1 | 192:3fd3dcf49005 | 60 | payloadIndex(0), |
rgrover1 | 192:3fd3dcf49005 | 61 | serviceDataPayload(), |
rgrover1 | 192:3fd3dcf49005 | 62 | initSucceeded(false), |
rgrover1 | 192:3fd3dcf49005 | 63 | lockedState(false), |
rgrover1 | 192:3fd3dcf49005 | 64 | uriDataLength(0), |
rgrover1 | 192:3fd3dcf49005 | 65 | uriData(), |
rgrover1 | 192:3fd3dcf49005 | 66 | flags(flagsIn), |
rgrover1 | 192:3fd3dcf49005 | 67 | effectivePower(effectiveTxPowerIn), |
rgrover1 | 192:3fd3dcf49005 | 68 | powerLevels(), |
rgrover1 | 192:3fd3dcf49005 | 69 | beaconPeriod(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriodIn)), |
rgrover1 | 192:3fd3dcf49005 | 70 | lockedStateChar(lockedStateCharUUID, reinterpret_cast<uint8_t *>(&lockedState), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 192:3fd3dcf49005 | 71 | uriDataChar(uriDataCharUUID, |
rgrover1 | 192:3fd3dcf49005 | 72 | uriData, |
rgrover1 | 192:3fd3dcf49005 | 73 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 192:3fd3dcf49005 | 74 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 192:3fd3dcf49005 | 75 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
rgrover1 | 192:3fd3dcf49005 | 76 | flagsChar(flagsCharUUID, &flags, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 192:3fd3dcf49005 | 77 | txPowerLevelsChar(txPowerLevelsCharUUID, |
rgrover1 | 192:3fd3dcf49005 | 78 | reinterpret_cast<uint8_t *>(powerLevels), |
rgrover1 | 192:3fd3dcf49005 | 79 | NUM_POWER_MODES * sizeof(int8_t), |
rgrover1 | 192:3fd3dcf49005 | 80 | NUM_POWER_MODES * sizeof(int8_t), |
rgrover1 | 192:3fd3dcf49005 | 81 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 192:3fd3dcf49005 | 82 | beaconPeriodChar(beaconPeriodCharUUID, reinterpret_cast<uint8_t *>(&beaconPeriod), 2, 2, |
rgrover1 | 192:3fd3dcf49005 | 83 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 192:3fd3dcf49005 | 84 | resetChar(resetCharUUID, reinterpret_cast<uint8_t *>(&resetFlag), 1, 1, |
rgrover1 | 192:3fd3dcf49005 | 85 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) |
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 | 192:3fd3dcf49005 | 92 | configure(); |
rgrover1 | 192:3fd3dcf49005 | 93 | if (initSucceeded) { |
rgrover1 | 192:3fd3dcf49005 | 94 | /* Preserve the originals to be able to reset() upon request. */ |
rgrover1 | 192:3fd3dcf49005 | 95 | memcpy(originalURIData, uriDataIn, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 192:3fd3dcf49005 | 96 | originalFlags = flagsIn; |
rgrover1 | 192:3fd3dcf49005 | 97 | originalEffectiveTxPower = effectiveTxPowerIn; |
rgrover1 | 192:3fd3dcf49005 | 98 | originalBeaconPeriod = beaconPeriodIn; |
rgrover1 | 171:6092e61690dc | 99 | } |
rgrover1 | 171:6092e61690dc | 100 | |
rgrover1 | 192:3fd3dcf49005 | 101 | GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar, &flagsChar, &txPowerLevelsChar, &beaconPeriodChar, &resetChar}; |
rgrover1 | 192:3fd3dcf49005 | 102 | GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 182:d16f8c11816b | 103 | |
rgrover1 | 192:3fd3dcf49005 | 104 | ble.addService(beaconControlService); |
rgrover1 | 192:3fd3dcf49005 | 105 | ble.onDataWritten(this, &URIBeacon2Service::onDataWritten); |
rgrover1 | 192:3fd3dcf49005 | 106 | } |
rgrover1 | 192:3fd3dcf49005 | 107 | |
rgrover1 | 193:ac2feceb7e87 | 108 | bool configuredSuccessfully(void) const { |
rgrover1 | 192:3fd3dcf49005 | 109 | return initSucceeded; |
rgrover1 | 171:6092e61690dc | 110 | } |
rgrover1 | 171:6092e61690dc | 111 | |
rgrover1 | 173:05c4b1cea65f | 112 | /** |
rgrover1 | 196:febbd68b1095 | 113 | * Please note that the following public APIs are offered to allow modifying |
rgrover1 | 196:febbd68b1095 | 114 | * the service programmatically. It is also possible to do so over BLE GATT |
rgrover1 | 196:febbd68b1095 | 115 | * transactions. |
rgrover1 | 196:febbd68b1095 | 116 | */ |
rgrover1 | 196:febbd68b1095 | 117 | public: |
rgrover1 | 196:febbd68b1095 | 118 | /** |
rgrover1 | 173:05c4b1cea65f | 119 | * Update flags of the URIBeacon dynamically. |
rgrover1 | 173:05c4b1cea65f | 120 | * |
rgrover1 | 173:05c4b1cea65f | 121 | * @param[in] flagsIn |
rgrover1 | 173:05c4b1cea65f | 122 | * |
rgrover1 | 173:05c4b1cea65f | 123 | * ### UriBeacon Flags |
rgrover1 | 173:05c4b1cea65f | 124 | * Bit | Description |
rgrover1 | 173:05c4b1cea65f | 125 | * :---- | :---------- |
rgrover1 | 173:05c4b1cea65f | 126 | * 0 | Invisible Hint |
rgrover1 | 173:05c4b1cea65f | 127 | * 1..7 | Reserved for future use. Must be zero. |
rgrover1 | 173:05c4b1cea65f | 128 | * |
rgrover1 | 173:05c4b1cea65f | 129 | * The `Invisible Hint` flag is a command for the user-agent that tells |
rgrover1 | 173:05c4b1cea65f | 130 | * it not to access or display the UriBeacon. This is a guideline only, |
rgrover1 | 173:05c4b1cea65f | 131 | * and is not a blocking method. User agents may, with user approval, |
rgrover1 | 173:05c4b1cea65f | 132 | * display invisible beacons. |
rgrover1 | 173:05c4b1cea65f | 133 | */ |
rgrover1 | 171:6092e61690dc | 134 | void setFlags(uint8_t flagsIn) { |
rgrover1 | 171:6092e61690dc | 135 | flags = flagsIn; |
rgrover1 | 184:ec211f2288ef | 136 | configure(); |
rgrover1 | 171:6092e61690dc | 137 | } |
rgrover1 | 171:6092e61690dc | 138 | |
rgrover1 | 177:17f4b5924fe9 | 139 | /** |
rgrover1 | 177:17f4b5924fe9 | 140 | * Update the txPower for a particular mode in the powerLevels table. |
rgrover1 | 177:17f4b5924fe9 | 141 | */ |
rgrover1 | 177:17f4b5924fe9 | 142 | void setTxPowerLevel(TXPowerModes_t mode, int8_t txPowerIn) { |
rgrover1 | 177:17f4b5924fe9 | 143 | powerLevels[mode] = txPowerIn; |
rgrover1 | 177:17f4b5924fe9 | 144 | } |
rgrover1 | 177:17f4b5924fe9 | 145 | |
rgrover1 | 177:17f4b5924fe9 | 146 | /** |
rgrover1 | 177:17f4b5924fe9 | 147 | * Set the effective power mode from one of the values in the powerLevels tables. |
rgrover1 | 177:17f4b5924fe9 | 148 | */ |
rgrover1 | 191:6710298104b0 | 149 | void useTxPowerMode(TXPowerModes_t mode) { |
rgrover1 | 177:17f4b5924fe9 | 150 | effectivePower = powerLevels[mode]; |
rgrover1 | 184:ec211f2288ef | 151 | configure(); |
rgrover1 | 171:6092e61690dc | 152 | } |
rgrover1 | 171:6092e61690dc | 153 | |
rgrover1 | 183:41d6ffadba96 | 154 | /** |
rgrover1 | 183:41d6ffadba96 | 155 | * The period in milliseconds that a UriBeacon packet is transmitted. |
rgrover1 | 183:41d6ffadba96 | 156 | * |
rgrover1 | 183:41d6ffadba96 | 157 | * @Note: A value of zero disables UriBeacon transmissions. |
rgrover1 | 183:41d6ffadba96 | 158 | */ |
rgrover1 | 171:6092e61690dc | 159 | void setBeaconPeriod(uint16_t beaconPeriodIn) { |
rgrover1 | 171:6092e61690dc | 160 | beaconPeriod = beaconPeriodIn; |
rgrover1 | 184:ec211f2288ef | 161 | configure(); |
rgrover1 | 171:6092e61690dc | 162 | } |
rgrover1 | 171:6092e61690dc | 163 | |
rgrover1 | 196:febbd68b1095 | 164 | private: |
rgrover1 | 186:ce34008cea95 | 165 | /** |
rgrover1 | 186:ce34008cea95 | 166 | * Setup the advertisement payload and GAP settings. |
rgrover1 | 186:ce34008cea95 | 167 | */ |
rgrover1 | 184:ec211f2288ef | 168 | void configure(void) { |
rgrover1 | 171:6092e61690dc | 169 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 171:6092e61690dc | 170 | |
rgrover1 | 171:6092e61690dc | 171 | payloadIndex = 0; |
rgrover1 | 171:6092e61690dc | 172 | serviceDataPayload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 171:6092e61690dc | 173 | serviceDataPayload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 171:6092e61690dc | 174 | serviceDataPayload[payloadIndex++] = flags; |
rgrover1 | 174:5ae7781362b6 | 175 | serviceDataPayload[payloadIndex++] = effectivePower; |
rgrover1 | 171:6092e61690dc | 176 | |
rgrover1 | 187:057c547facd2 | 177 | const char *urlData = reinterpret_cast<char *>(uriData); |
rgrover1 | 171:6092e61690dc | 178 | size_t sizeofURLData = uriDataLength; |
rgrover1 | 183:41d6ffadba96 | 179 | size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData); |
rgrover1 | 171:6092e61690dc | 180 | |
rgrover1 | 171:6092e61690dc | 181 | ble.clearAdvertisingPayload(); |
rgrover1 | 171:6092e61690dc | 182 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 171:6092e61690dc | 183 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4); |
rgrover1 | 171:6092e61690dc | 184 | |
rgrover1 | 171:6092e61690dc | 185 | ble.setAdvertisingInterval(beaconPeriod); |
rgrover1 | 174:5ae7781362b6 | 186 | ble.setTxPower(effectivePower); |
rgrover1 | 171:6092e61690dc | 187 | } |
rgrover1 | 171:6092e61690dc | 188 | |
rgrover1 | 171:6092e61690dc | 189 | size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) { |
rgrover1 | 171:6092e61690dc | 190 | const char *prefixes[] = { |
rgrover1 | 171:6092e61690dc | 191 | "http://www.", |
rgrover1 | 171:6092e61690dc | 192 | "https://www.", |
rgrover1 | 171:6092e61690dc | 193 | "http://", |
rgrover1 | 171:6092e61690dc | 194 | "https://", |
rgrover1 | 171:6092e61690dc | 195 | "urn:uuid:" |
rgrover1 | 171:6092e61690dc | 196 | }; |
rgrover1 | 171:6092e61690dc | 197 | |
rgrover1 | 171:6092e61690dc | 198 | size_t encodedBytes = 0; |
rgrover1 | 171:6092e61690dc | 199 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 171:6092e61690dc | 200 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 171:6092e61690dc | 201 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 171:6092e61690dc | 202 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 171:6092e61690dc | 203 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 171:6092e61690dc | 204 | encodedBytes = 1; |
rgrover1 | 171:6092e61690dc | 205 | |
rgrover1 | 171:6092e61690dc | 206 | urldata += prefixLen; |
rgrover1 | 171:6092e61690dc | 207 | sizeofURLData -= prefixLen; |
rgrover1 | 171:6092e61690dc | 208 | break; |
rgrover1 | 171:6092e61690dc | 209 | } |
rgrover1 | 171:6092e61690dc | 210 | } |
rgrover1 | 171:6092e61690dc | 211 | |
rgrover1 | 171:6092e61690dc | 212 | return encodedBytes; |
rgrover1 | 171:6092e61690dc | 213 | } |
rgrover1 | 171:6092e61690dc | 214 | |
rgrover1 | 171:6092e61690dc | 215 | size_t encodeURI(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 171:6092e61690dc | 216 | const char *suffixes[] = { |
rgrover1 | 171:6092e61690dc | 217 | ".com/", |
rgrover1 | 171:6092e61690dc | 218 | ".org/", |
rgrover1 | 171:6092e61690dc | 219 | ".edu/", |
rgrover1 | 171:6092e61690dc | 220 | ".net/", |
rgrover1 | 171:6092e61690dc | 221 | ".info/", |
rgrover1 | 171:6092e61690dc | 222 | ".biz/", |
rgrover1 | 171:6092e61690dc | 223 | ".gov/", |
rgrover1 | 171:6092e61690dc | 224 | ".com", |
rgrover1 | 171:6092e61690dc | 225 | ".org", |
rgrover1 | 171:6092e61690dc | 226 | ".edu", |
rgrover1 | 171:6092e61690dc | 227 | ".net", |
rgrover1 | 171:6092e61690dc | 228 | ".info", |
rgrover1 | 171:6092e61690dc | 229 | ".biz", |
rgrover1 | 171:6092e61690dc | 230 | ".gov" |
rgrover1 | 171:6092e61690dc | 231 | }; |
rgrover1 | 171:6092e61690dc | 232 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 171:6092e61690dc | 233 | |
rgrover1 | 171:6092e61690dc | 234 | size_t encodedBytes = 0; |
rgrover1 | 171:6092e61690dc | 235 | while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) { |
rgrover1 | 171:6092e61690dc | 236 | /* check for suffix match */ |
rgrover1 | 171:6092e61690dc | 237 | unsigned i; |
rgrover1 | 171:6092e61690dc | 238 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 171:6092e61690dc | 239 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 171:6092e61690dc | 240 | if ((suffixLen == 0) || (sizeofURLData < suffixLen)) { |
rgrover1 | 171:6092e61690dc | 241 | continue; |
rgrover1 | 171:6092e61690dc | 242 | } |
rgrover1 | 171:6092e61690dc | 243 | |
rgrover1 | 171:6092e61690dc | 244 | if (strncmp(urldata, suffixes[i], suffixLen) == 0) { |
rgrover1 | 171:6092e61690dc | 245 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 171:6092e61690dc | 246 | ++encodedBytes; |
rgrover1 | 171:6092e61690dc | 247 | urldata += suffixLen; |
rgrover1 | 171:6092e61690dc | 248 | sizeofURLData -= suffixLen; |
rgrover1 | 171:6092e61690dc | 249 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 171:6092e61690dc | 250 | } |
rgrover1 | 171:6092e61690dc | 251 | } |
rgrover1 | 171:6092e61690dc | 252 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 171:6092e61690dc | 253 | if (i == NUM_SUFFIXES) { |
rgrover1 | 171:6092e61690dc | 254 | serviceDataPayload[payloadIndex++] = *urldata; |
rgrover1 | 171:6092e61690dc | 255 | ++encodedBytes; |
rgrover1 | 171:6092e61690dc | 256 | ++urldata; |
rgrover1 | 171:6092e61690dc | 257 | --sizeofURLData; |
rgrover1 | 171:6092e61690dc | 258 | } |
rgrover1 | 171:6092e61690dc | 259 | } |
rgrover1 | 192:3fd3dcf49005 | 260 | if (sizeofURLData == 0) { |
rgrover1 | 192:3fd3dcf49005 | 261 | initSucceeded = true; |
rgrover1 | 182:d16f8c11816b | 262 | } |
rgrover1 | 171:6092e61690dc | 263 | |
rgrover1 | 171:6092e61690dc | 264 | return encodedBytes; |
rgrover1 | 171:6092e61690dc | 265 | } |
rgrover1 | 171:6092e61690dc | 266 | |
rgrover1 | 185:7cd70497aec8 | 267 | /** |
rgrover1 | 185:7cd70497aec8 | 268 | * This callback allows the DFU service to receive the initial trigger to |
rgrover1 | 185:7cd70497aec8 | 269 | * handover control to the bootloader; but first the application is given a |
rgrover1 | 185:7cd70497aec8 | 270 | * chance to clean up. |
rgrover1 | 185:7cd70497aec8 | 271 | */ |
rgrover1 | 185:7cd70497aec8 | 272 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 185:7cd70497aec8 | 273 | if (params->charHandle == uriDataChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 274 | if (lockedState) { /* When locked, the device isn't allowed to update the uriData characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 275 | /* Restore GATT database with previous value. */ |
rgrover1 | 187:057c547facd2 | 276 | ble.updateCharacteristicValue(uriDataChar.getValueAttribute().getHandle(), uriData, uriDataLength); |
rgrover1 | 185:7cd70497aec8 | 277 | return; |
rgrover1 | 185:7cd70497aec8 | 278 | } |
rgrover1 | 185:7cd70497aec8 | 279 | |
rgrover1 | 185:7cd70497aec8 | 280 | /* We don't handle very large writes at the moment. */ |
rgrover1 | 185:7cd70497aec8 | 281 | if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) { |
rgrover1 | 185:7cd70497aec8 | 282 | return; |
rgrover1 | 185:7cd70497aec8 | 283 | } |
rgrover1 | 185:7cd70497aec8 | 284 | |
rgrover1 | 185:7cd70497aec8 | 285 | uriDataLength = params->len; |
rgrover1 | 187:057c547facd2 | 286 | memcpy(uriData, params->data, uriDataLength); |
rgrover1 | 185:7cd70497aec8 | 287 | } else if (params->charHandle == flagsChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 288 | if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 289 | /* Restore GATT database with previous value. */ |
rgrover1 | 185:7cd70497aec8 | 290 | ble.updateCharacteristicValue(flagsChar.getValueAttribute().getHandle(), &flags, 1 /* size */); |
rgrover1 | 185:7cd70497aec8 | 291 | return; |
rgrover1 | 185:7cd70497aec8 | 292 | } else { |
rgrover1 | 185:7cd70497aec8 | 293 | flags = *(params->data); |
rgrover1 | 185:7cd70497aec8 | 294 | } |
rgrover1 | 185:7cd70497aec8 | 295 | } else if (params->charHandle == txPowerLevelsChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 296 | if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 297 | /* Restore GATT database with previous value. */ |
rgrover1 | 185:7cd70497aec8 | 298 | ble.updateCharacteristicValue(txPowerLevelsChar.getValueAttribute().getHandle(), reinterpret_cast<uint8_t *>(powerLevels), NUM_POWER_MODES * sizeof(int8_t)); |
rgrover1 | 185:7cd70497aec8 | 299 | return; |
rgrover1 | 185:7cd70497aec8 | 300 | } else { |
rgrover1 | 185:7cd70497aec8 | 301 | memcpy(powerLevels, params->data, NUM_POWER_MODES * sizeof(int8_t)); |
rgrover1 | 185:7cd70497aec8 | 302 | } |
rgrover1 | 185:7cd70497aec8 | 303 | } else if (params->charHandle == beaconPeriodChar.getValueAttribute().getHandle()) { |
rgrover1 | 185:7cd70497aec8 | 304 | if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */ |
rgrover1 | 185:7cd70497aec8 | 305 | /* Restore GATT database with previous value. */ |
rgrover1 | 190:ad1ed2b5ea52 | 306 | ble.updateCharacteristicValue(beaconPeriodChar.getValueAttribute().getHandle(), reinterpret_cast<uint8_t *>(&beaconPeriod), sizeof(uint16_t)); |
rgrover1 | 185:7cd70497aec8 | 307 | return; |
rgrover1 | 185:7cd70497aec8 | 308 | } else { |
rgrover1 | 185:7cd70497aec8 | 309 | beaconPeriod = *((uint16_t *)(params->data)); |
rgrover1 | 185:7cd70497aec8 | 310 | } |
rgrover1 | 189:a23091b54ab5 | 311 | } else if (params->charHandle == resetChar.getValueAttribute().getHandle()) { |
rgrover1 | 189:a23091b54ab5 | 312 | resetOriginals(); |
rgrover1 | 185:7cd70497aec8 | 313 | } |
rgrover1 | 185:7cd70497aec8 | 314 | configure(); |
rgrover1 | 185:7cd70497aec8 | 315 | ble.setAdvertisingPayload(); |
rgrover1 | 185:7cd70497aec8 | 316 | } |
rgrover1 | 185:7cd70497aec8 | 317 | |
rgrover1 | 189:a23091b54ab5 | 318 | void resetOriginals(void) { |
rgrover1 | 189:a23091b54ab5 | 319 | memcpy(uriData, originalURIData, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 189:a23091b54ab5 | 320 | memset(powerLevels, 0, sizeof(powerLevels)); |
rgrover1 | 189:a23091b54ab5 | 321 | flags = originalFlags; |
rgrover1 | 189:a23091b54ab5 | 322 | effectivePower = originalEffectiveTxPower; |
rgrover1 | 189:a23091b54ab5 | 323 | beaconPeriod = originalBeaconPeriod; |
rgrover1 | 189:a23091b54ab5 | 324 | |
rgrover1 | 189:a23091b54ab5 | 325 | ble.updateCharacteristicValue(uriDataChar.getValueAttribute().getHandle(), uriData, uriDataLength); |
rgrover1 | 189:a23091b54ab5 | 326 | ble.updateCharacteristicValue(flagsChar.getValueAttribute().getHandle(), &flags, 1 /* size */); |
rgrover1 | 189:a23091b54ab5 | 327 | ble.updateCharacteristicValue(beaconPeriodChar.getValueAttribute().getHandle(), reinterpret_cast<uint8_t *>(&beaconPeriod), sizeof(uint16_t)); |
rgrover1 | 189:a23091b54ab5 | 328 | |
rgrover1 | 189:a23091b54ab5 | 329 | configure(); |
rgrover1 | 189:a23091b54ab5 | 330 | } |
rgrover1 | 189:a23091b54ab5 | 331 | |
rgrover1 | 185:7cd70497aec8 | 332 | private: |
rgrover1 | 185:7cd70497aec8 | 333 | /** |
rgrover1 | 185:7cd70497aec8 | 334 | * For debugging only. |
rgrover1 | 185:7cd70497aec8 | 335 | */ |
rgrover1 | 185:7cd70497aec8 | 336 | void dumpEncodedSeviceData() const { |
rgrover1 | 185:7cd70497aec8 | 337 | printf("encoded: '"); |
rgrover1 | 185:7cd70497aec8 | 338 | for (unsigned i = 0; i < payloadIndex; i++) { |
rgrover1 | 185:7cd70497aec8 | 339 | printf(" %02x", serviceDataPayload[i]); |
rgrover1 | 185:7cd70497aec8 | 340 | } |
rgrover1 | 185:7cd70497aec8 | 341 | printf("'\r\n"); |
rgrover1 | 185:7cd70497aec8 | 342 | } |
rgrover1 | 185:7cd70497aec8 | 343 | |
rgrover1 | 171:6092e61690dc | 344 | private: |
rgrover1 | 199:6b57874115f6 | 345 | static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 18; /* Uri Data must be between 0 and 18 bytes in length. */ |
rgrover1 | 171:6092e61690dc | 346 | static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; |
rgrover1 | 171:6092e61690dc | 347 | |
rgrover1 | 171:6092e61690dc | 348 | private: |
rgrover1 | 171:6092e61690dc | 349 | BLEDevice &ble; |
rgrover1 | 171:6092e61690dc | 350 | |
rgrover1 | 171:6092e61690dc | 351 | size_t payloadIndex; |
rgrover1 | 171:6092e61690dc | 352 | uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD]; |
rgrover1 | 192:3fd3dcf49005 | 353 | bool initSucceeded; |
rgrover1 | 182:d16f8c11816b | 354 | |
rgrover1 | 171:6092e61690dc | 355 | bool lockedState; |
rgrover1 | 171:6092e61690dc | 356 | uint16_t uriDataLength; |
rgrover1 | 187:057c547facd2 | 357 | uint8_t uriData[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 171:6092e61690dc | 358 | uint8_t flags; |
rgrover1 | 174:5ae7781362b6 | 359 | int8_t effectivePower; |
rgrover1 | 177:17f4b5924fe9 | 360 | int8_t powerLevels[NUM_POWER_MODES]; |
rgrover1 | 171:6092e61690dc | 361 | uint16_t beaconPeriod; |
rgrover1 | 189:a23091b54ab5 | 362 | bool resetFlag; |
rgrover1 | 171:6092e61690dc | 363 | |
rgrover1 | 188:9020a5ac80c8 | 364 | uint8_t originalURIData[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 188:9020a5ac80c8 | 365 | uint8_t originalFlags; |
rgrover1 | 189:a23091b54ab5 | 366 | int8_t originalEffectiveTxPower; |
rgrover1 | 189:a23091b54ab5 | 367 | uint16_t originalBeaconPeriod; |
rgrover1 | 188:9020a5ac80c8 | 368 | |
rgrover1 | 171:6092e61690dc | 369 | GattCharacteristic lockedStateChar; |
rgrover1 | 171:6092e61690dc | 370 | GattCharacteristic uriDataChar; |
rgrover1 | 171:6092e61690dc | 371 | GattCharacteristic flagsChar; |
rgrover1 | 178:b6d6a2a11e86 | 372 | GattCharacteristic txPowerLevelsChar; |
rgrover1 | 171:6092e61690dc | 373 | GattCharacteristic beaconPeriodChar; |
rgrover1 | 189:a23091b54ab5 | 374 | GattCharacteristic resetChar; |
rgrover1 | 171:6092e61690dc | 375 | }; |
rgrover1 | 171:6092e61690dc | 376 | |
rgrover1 | 171:6092e61690dc | 377 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |