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