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