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