I don't know why this is happening.
Fork of BLE_API by
services/URIBeaconConfigService.h@310:0b79de98eceb, 2015-03-09 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Mar 09 16:23:55 2015 +0000
- Revision:
- 310:0b79de98eceb
- Parent:
- 309:bc91f7ba346d
- Child:
- 311:11417b970b12
Synchronized with git rev 517d7c9e
Author: Rohit Grover
white space diffs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 171:6092e61690dc | 1 | /* mbed Microcontroller Library |
rgrover1 | 171:6092e61690dc | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 171:6092e61690dc | 3 | * |
rgrover1 | 171:6092e61690dc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 171:6092e61690dc | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 171:6092e61690dc | 6 | * You may obtain a copy of the License at |
rgrover1 | 171:6092e61690dc | 7 | * |
rgrover1 | 171:6092e61690dc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 171:6092e61690dc | 9 | * |
rgrover1 | 171:6092e61690dc | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 171:6092e61690dc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 171:6092e61690dc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 171:6092e61690dc | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 171:6092e61690dc | 14 | * limitations under the License. |
rgrover1 | 171:6092e61690dc | 15 | */ |
rgrover1 | 171:6092e61690dc | 16 | |
rgrover1 | 287:cca148e535b1 | 17 | #ifndef SERVICES_URIBEACONCONFIGSERVICE_H_ |
rgrover1 | 287:cca148e535b1 | 18 | #define SERVICES_URIBEACONCONFIGSERVICE_H_ |
rgrover1 | 171:6092e61690dc | 19 | |
rgrover1 | 287:cca148e535b1 | 20 | #define UUID_URI_BEACON(FIRST, SECOND) { \ |
rgrover1 | 225:f6cbfd817d16 | 21 | 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ |
rgrover1 | 225:f6cbfd817d16 | 22 | 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \ |
rgrover1 | 171:6092e61690dc | 23 | } |
rgrover1 | 287:cca148e535b1 | 24 | |
rgrover1 | 287:cca148e535b1 | 25 | static const uint8_t UUID_URI_BEACON_SERVICE[] = UUID_URI_BEACON(0x20, 0x80); |
rgrover1 | 287:cca148e535b1 | 26 | static const uint8_t UUID_LOCK_STATE_CHAR[] = UUID_URI_BEACON(0x20, 0x81); |
rgrover1 | 287:cca148e535b1 | 27 | static const uint8_t UUID_LOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x82); |
rgrover1 | 287:cca148e535b1 | 28 | static const uint8_t UUID_UNLOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x83); |
rgrover1 | 287:cca148e535b1 | 29 | static const uint8_t UUID_URI_DATA_CHAR[] = UUID_URI_BEACON(0x20, 0x84); |
rgrover1 | 287:cca148e535b1 | 30 | static const uint8_t UUID_FLAGS_CHAR[] = UUID_URI_BEACON(0x20, 0x85); |
rgrover1 | 287:cca148e535b1 | 31 | static const uint8_t UUID_ADV_POWER_LEVELS_CHAR[] = UUID_URI_BEACON(0x20, 0x86); |
rgrover1 | 287:cca148e535b1 | 32 | static const uint8_t UUID_TX_POWER_MODE_CHAR[] = UUID_URI_BEACON(0x20, 0x87); |
rgrover1 | 287:cca148e535b1 | 33 | static const uint8_t UUID_BEACON_PERIOD_CHAR[] = UUID_URI_BEACON(0x20, 0x88); |
rgrover1 | 287:cca148e535b1 | 34 | static const uint8_t UUID_RESET_CHAR[] = UUID_URI_BEACON(0x20, 0x89); |
rgrover1 | 287:cca148e535b1 | 35 | static const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 171:6092e61690dc | 36 | |
mbedAustin | 231:1c4a4fd961a5 | 37 | /** |
mbedAustin | 231:1c4a4fd961a5 | 38 | * @class URIBeaconConfigService |
rgrover1 | 242:0e9201b67e2f | 39 | * @brief UriBeacon Configuration Service. Can be used to set URL, adjust power levels, and set flags. |
rgrover1 | 287:cca148e535b1 | 40 | * See http://uribeacon.org |
rgrover1 | 287:cca148e535b1 | 41 | * |
mbedAustin | 231:1c4a4fd961a5 | 42 | */ |
rgrover1 | 207:e88130dc254c | 43 | class URIBeaconConfigService { |
rgrover1 | 287:cca148e535b1 | 44 | public: |
mbedAustin | 231:1c4a4fd961a5 | 45 | /** |
rgrover1 | 242:0e9201b67e2f | 46 | * @brief Transmission Power Modes for UriBeacon |
rgrover1 | 242:0e9201b67e2f | 47 | */ |
rgrover1 | 287:cca148e535b1 | 48 | static const uint8_t TX_POWER_MODE_LOWEST = 0; /*!< Lowest TX power mode */ |
rgrover1 | 287:cca148e535b1 | 49 | static const uint8_t TX_POWER_MODE_LOW = 1; /*!< Low TX power mode */ |
rgrover1 | 287:cca148e535b1 | 50 | static const uint8_t TX_POWER_MODE_MEDIUM = 2; /*!< Medium TX power mode */ |
rgrover1 | 287:cca148e535b1 | 51 | static const uint8_t TX_POWER_MODE_HIGH = 3; /*!< High TX power mode */ |
rgrover1 | 287:cca148e535b1 | 52 | static const unsigned int NUM_POWER_MODES = 4; /*!< Number of Power Modes defined */ |
rgrover1 | 287:cca148e535b1 | 53 | |
rgrover1 | 309:bc91f7ba346d | 54 | static const int ADVERTISING_TIMEOUT_SECONDS = 60; // Seconds after power-on that config service is available. |
rgrover1 | 309:bc91f7ba346d | 55 | static const int ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service. |
rgrover1 | 309:bc91f7ba346d | 56 | static const int SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets |
rgrover1 | 287:cca148e535b1 | 57 | |
rgrover1 | 287:cca148e535b1 | 58 | typedef uint8_t Lock_t[16]; /* 128 bits */ |
rgrover1 | 287:cca148e535b1 | 59 | typedef int8_t PowerLevels_t[NUM_POWER_MODES]; |
rgrover1 | 287:cca148e535b1 | 60 | |
rgrover1 | 287:cca148e535b1 | 61 | static const int URI_DATA_MAX = 18; |
rgrover1 | 287:cca148e535b1 | 62 | typedef uint8_t UriData_t[URI_DATA_MAX]; |
rgrover1 | 287:cca148e535b1 | 63 | |
rgrover1 | 287:cca148e535b1 | 64 | struct Params_t { |
rgrover1 | 287:cca148e535b1 | 65 | Lock_t lock; |
rgrover1 | 287:cca148e535b1 | 66 | uint8_t uriDataLength; |
rgrover1 | 287:cca148e535b1 | 67 | UriData_t uriData; |
rgrover1 | 287:cca148e535b1 | 68 | uint8_t flags; |
rgrover1 | 310:0b79de98eceb | 69 | PowerLevels_t advPowerLevels; // Current value of AdvertisedPowerLevels |
rgrover1 | 310:0b79de98eceb | 70 | uint8_t txPowerMode; // Firmware power levels used with setTxPower() |
rgrover1 | 287:cca148e535b1 | 71 | uint16_t beaconPeriod; |
rgrover1 | 175:4e85f7225f8f | 72 | }; |
rgrover1 | 175:4e85f7225f8f | 73 | |
rgrover1 | 181:bbb6ce1082c3 | 74 | /** |
rgrover1 | 287:cca148e535b1 | 75 | * @param[ref] ble |
rgrover1 | 287:cca148e535b1 | 76 | * BLEDevice object for the underlying controller. |
rgrover1 | 287:cca148e535b1 | 77 | * @param[in/out] paramsIn |
rgrover1 | 287:cca148e535b1 | 78 | * Reference to application-visible beacon state, loaded |
rgrover1 | 287:cca148e535b1 | 79 | * from persistent storage at startup. |
rgrover1 | 287:cca148e535b1 | 80 | * @param[in] resetToDefaultsFlag |
rgrover1 | 287:cca148e535b1 | 81 | * reset params state to the defaults. |
rgrover1 | 287:cca148e535b1 | 82 | * @param[in] defaultUriDataIn |
rgrover1 | 309:bc91f7ba346d | 83 | * Default un-encoded URI; applies only if the resetToDefaultsFlag is true. |
rgrover1 | 287:cca148e535b1 | 84 | * @param[in] defaultAdvPowerLevelsIn |
rgrover1 | 287:cca148e535b1 | 85 | * Default power-levels array; applies only if the resetToDefaultsFlag is true. |
rgrover1 | 181:bbb6ce1082c3 | 86 | */ |
rgrover1 | 287:cca148e535b1 | 87 | URIBeaconConfigService(BLEDevice &bleIn, |
rgrover1 | 287:cca148e535b1 | 88 | Params_t ¶msIn, |
rgrover1 | 287:cca148e535b1 | 89 | bool resetToDefaultsFlag, |
rgrover1 | 309:bc91f7ba346d | 90 | const char *defaultURIDataIn, |
rgrover1 | 287:cca148e535b1 | 91 | PowerLevels_t &defaultAdvPowerLevelsIn) : |
rgrover1 | 192:3fd3dcf49005 | 92 | ble(bleIn), |
rgrover1 | 287:cca148e535b1 | 93 | params(paramsIn), |
rgrover1 | 309:bc91f7ba346d | 94 | defaultUriDataLength(), |
rgrover1 | 309:bc91f7ba346d | 95 | defaultUriData(), |
rgrover1 | 287:cca148e535b1 | 96 | defaultAdvPowerLevels(defaultAdvPowerLevelsIn), |
rgrover1 | 192:3fd3dcf49005 | 97 | initSucceeded(false), |
rgrover1 | 309:bc91f7ba346d | 98 | resetFlag(), |
rgrover1 | 309:bc91f7ba346d | 99 | configAdvertisementTimeoutTicker(), |
rgrover1 | 287:cca148e535b1 | 100 | lockedStateChar(UUID_LOCK_STATE_CHAR, &lockedState), |
rgrover1 | 287:cca148e535b1 | 101 | lockChar(UUID_LOCK_CHAR, ¶ms.lock), |
rgrover1 | 300:d9a39f759a6a | 102 | uriDataChar(UUID_URI_DATA_CHAR, params.uriData, 0, URI_DATA_MAX, |
rgrover1 | 287:cca148e535b1 | 103 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 287:cca148e535b1 | 104 | unlockChar(UUID_UNLOCK_CHAR, ¶ms.lock), |
rgrover1 | 287:cca148e535b1 | 105 | flagsChar(UUID_FLAGS_CHAR, ¶ms.flags), |
rgrover1 | 287:cca148e535b1 | 106 | advPowerLevelsChar(UUID_ADV_POWER_LEVELS_CHAR, ¶ms.advPowerLevels), |
rgrover1 | 287:cca148e535b1 | 107 | txPowerModeChar(UUID_TX_POWER_MODE_CHAR, ¶ms.txPowerMode), |
rgrover1 | 287:cca148e535b1 | 108 | beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, ¶ms.beaconPeriod), |
rgrover1 | 287:cca148e535b1 | 109 | resetChar(UUID_RESET_CHAR, &resetFlag) { |
rgrover1 | 309:bc91f7ba346d | 110 | |
rgrover1 | 309:bc91f7ba346d | 111 | encodeURI(defaultURIDataIn, defaultUriData, defaultUriDataLength); |
rgrover1 | 309:bc91f7ba346d | 112 | if (defaultUriDataLength > URI_DATA_MAX) { |
rgrover1 | 192:3fd3dcf49005 | 113 | return; |
rgrover1 | 192:3fd3dcf49005 | 114 | } |
rgrover1 | 192:3fd3dcf49005 | 115 | |
rgrover1 | 287:cca148e535b1 | 116 | if (params.uriDataLength > URI_DATA_MAX) { |
rgrover1 | 287:cca148e535b1 | 117 | resetToDefaultsFlag = true; |
rgrover1 | 218:8ae02569fab9 | 118 | } |
rgrover1 | 218:8ae02569fab9 | 119 | |
rgrover1 | 287:cca148e535b1 | 120 | lockedState = isLocked(); |
rgrover1 | 171:6092e61690dc | 121 | |
rgrover1 | 287:cca148e535b1 | 122 | if (resetToDefaultsFlag) { |
rgrover1 | 287:cca148e535b1 | 123 | resetToDefaults(); |
rgrover1 | 287:cca148e535b1 | 124 | } else { |
rgrover1 | 287:cca148e535b1 | 125 | updateCharacteristicValues(); |
rgrover1 | 287:cca148e535b1 | 126 | } |
rgrover1 | 274:f540619754bb | 127 | |
rgrover1 | 272:65b9d9c87ed1 | 128 | lockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::lockAuthorizationCallback); |
rgrover1 | 272:65b9d9c87ed1 | 129 | unlockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::unlockAuthorizationCallback); |
rgrover1 | 250:6862d374e613 | 130 | uriDataChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::uriDataWriteAuthorizationCallback); |
rgrover1 | 255:cdb7231f83df | 131 | flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::flagsAuthorizationCallback); |
rgrover1 | 287:cca148e535b1 | 132 | advPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); |
rgrover1 | 287:cca148e535b1 | 133 | txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); |
rgrover1 | 250:6862d374e613 | 134 | beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); |
rgrover1 | 250:6862d374e613 | 135 | resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); |
rgrover1 | 250:6862d374e613 | 136 | |
rgrover1 | 287:cca148e535b1 | 137 | static GattCharacteristic *charTable[] = { |
rgrover1 | 287:cca148e535b1 | 138 | &lockedStateChar, &lockChar, &unlockChar, &uriDataChar, |
rgrover1 | 287:cca148e535b1 | 139 | &flagsChar, &advPowerLevelsChar, &txPowerModeChar, &beaconPeriodChar, &resetChar |
rgrover1 | 287:cca148e535b1 | 140 | }; |
rgrover1 | 182:d16f8c11816b | 141 | |
rgrover1 | 287:cca148e535b1 | 142 | GattService configService(UUID_URI_BEACON_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 287:cca148e535b1 | 143 | |
rgrover1 | 287:cca148e535b1 | 144 | ble.addService(configService); |
rgrover1 | 287:cca148e535b1 | 145 | ble.onDataWritten(this, &URIBeaconConfigService::onDataWrittenCallback); |
rgrover1 | 287:cca148e535b1 | 146 | |
rgrover1 | 309:bc91f7ba346d | 147 | /* Start out by advertising the configService for a limited time after |
rgrover1 | 309:bc91f7ba346d | 148 | * startup; and switch to the normal non-connectible beacon functionality |
rgrover1 | 309:bc91f7ba346d | 149 | * afterwards. */ |
rgrover1 | 309:bc91f7ba346d | 150 | setupUriBeaconConfigAdvertisements(); |
rgrover1 | 309:bc91f7ba346d | 151 | |
rgrover1 | 287:cca148e535b1 | 152 | initSucceeded = true; |
rgrover1 | 192:3fd3dcf49005 | 153 | } |
rgrover1 | 192:3fd3dcf49005 | 154 | |
rgrover1 | 193:ac2feceb7e87 | 155 | bool configuredSuccessfully(void) const { |
rgrover1 | 192:3fd3dcf49005 | 156 | return initSucceeded; |
rgrover1 | 171:6092e61690dc | 157 | } |
rgrover1 | 171:6092e61690dc | 158 | |
rgrover1 | 309:bc91f7ba346d | 159 | /* Start out by advertising the configService for a limited time after |
rgrover1 | 309:bc91f7ba346d | 160 | * startup; and switch to the normal non-connectible beacon functionality |
rgrover1 | 309:bc91f7ba346d | 161 | * afterwards. */ |
rgrover1 | 309:bc91f7ba346d | 162 | void setupUriBeaconConfigAdvertisements() |
rgrover1 | 309:bc91f7ba346d | 163 | { |
rgrover1 | 309:bc91f7ba346d | 164 | char DEVICE_NAME[] = "mUriBeacon Config"; |
rgrover1 | 309:bc91f7ba346d | 165 | |
rgrover1 | 309:bc91f7ba346d | 166 | ble.clearAdvertisingPayload(); |
rgrover1 | 309:bc91f7ba346d | 167 | |
rgrover1 | 309:bc91f7ba346d | 168 | // Stops advertising the UriBeacon Config Service after a delay |
rgrover1 | 309:bc91f7ba346d | 169 | configAdvertisementTimeoutTicker.attach(this, &URIBeaconConfigService::timeout, ADVERTISING_TIMEOUT_SECONDS); |
rgrover1 | 309:bc91f7ba346d | 170 | |
rgrover1 | 309:bc91f7ba346d | 171 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 309:bc91f7ba346d | 172 | |
rgrover1 | 309:bc91f7ba346d | 173 | // UUID is in different order in the ADV frame (!) |
rgrover1 | 309:bc91f7ba346d | 174 | uint8_t reversedServiceUUID[sizeof(UUID_URI_BEACON_SERVICE)]; |
rgrover1 | 309:bc91f7ba346d | 175 | for (unsigned int i = 0; i < sizeof(UUID_URI_BEACON_SERVICE); i++) { |
rgrover1 | 309:bc91f7ba346d | 176 | reversedServiceUUID[i] = |
rgrover1 | 309:bc91f7ba346d | 177 | UUID_URI_BEACON_SERVICE[sizeof(UUID_URI_BEACON_SERVICE) - i - 1]; |
rgrover1 | 309:bc91f7ba346d | 178 | } |
rgrover1 | 309:bc91f7ba346d | 179 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, reversedServiceUUID, sizeof(reversedServiceUUID)); |
rgrover1 | 309:bc91f7ba346d | 180 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG); |
rgrover1 | 309:bc91f7ba346d | 181 | ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, reinterpret_cast<uint8_t *>(&DEVICE_NAME), sizeof(DEVICE_NAME)); |
rgrover1 | 309:bc91f7ba346d | 182 | ble.accumulateScanResponse( |
rgrover1 | 309:bc91f7ba346d | 183 | GapAdvertisingData::TX_POWER_LEVEL, |
rgrover1 | 309:bc91f7ba346d | 184 | reinterpret_cast<uint8_t *>( |
rgrover1 | 309:bc91f7ba346d | 185 | &defaultAdvPowerLevels[URIBeaconConfigService::TX_POWER_MODE_LOW]), |
rgrover1 | 309:bc91f7ba346d | 186 | sizeof(uint8_t)); |
rgrover1 | 309:bc91f7ba346d | 187 | |
rgrover1 | 309:bc91f7ba346d | 188 | /////// TODO |
rgrover1 | 309:bc91f7ba346d | 189 | // ble.setTxPower( |
rgrover1 | 309:bc91f7ba346d | 190 | // firmwarePowerLevels[URIBeaconConfigService::TX_POWER_MODE_LOW]); |
rgrover1 | 309:bc91f7ba346d | 191 | |
rgrover1 | 309:bc91f7ba346d | 192 | ble.setDeviceName(reinterpret_cast<uint8_t *>(&DEVICE_NAME)); |
rgrover1 | 309:bc91f7ba346d | 193 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 309:bc91f7ba346d | 194 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADVERTISING_INTERVAL_MSEC)); |
rgrover1 | 309:bc91f7ba346d | 195 | } |
rgrover1 | 309:bc91f7ba346d | 196 | |
rgrover1 | 309:bc91f7ba346d | 197 | void setupUriBeaconAdvertisements() |
rgrover1 | 309:bc91f7ba346d | 198 | { |
rgrover1 | 309:bc91f7ba346d | 199 | // uint8_t serviceData[SERVICE_DATA_MAX]; |
rgrover1 | 309:bc91f7ba346d | 200 | // int serviceDataLen = 0; |
rgrover1 | 309:bc91f7ba346d | 201 | |
rgrover1 | 309:bc91f7ba346d | 202 | // advertisingStateLed = 1; |
rgrover1 | 309:bc91f7ba346d | 203 | // connectionStateLed = 1; |
rgrover1 | 309:bc91f7ba346d | 204 | |
rgrover1 | 309:bc91f7ba346d | 205 | ble.shutdown(); |
rgrover1 | 309:bc91f7ba346d | 206 | ble.init(); |
rgrover1 | 309:bc91f7ba346d | 207 | |
rgrover1 | 309:bc91f7ba346d | 208 | // Fields from the Service |
rgrover1 | 309:bc91f7ba346d | 209 | // int beaconPeriod = persistentData.params.beaconPeriod; |
rgrover1 | 309:bc91f7ba346d | 210 | // int txPowerMode = persistentData.params.txPowerMode; |
rgrover1 | 309:bc91f7ba346d | 211 | // int uriDataLength = persistentData.params.uriDataLength; |
rgrover1 | 309:bc91f7ba346d | 212 | // URIBeaconConfigService::UriData_t &uriData = persistentData.params.uriData; |
rgrover1 | 309:bc91f7ba346d | 213 | // URIBeaconConfigService::PowerLevels_t &advPowerLevels = persistentData.params.advPowerLevels; |
rgrover1 | 309:bc91f7ba346d | 214 | // uint8_t flags = persistentData.params.flags; |
rgrover1 | 309:bc91f7ba346d | 215 | |
rgrover1 | 309:bc91f7ba346d | 216 | // pstorageSave(); |
rgrover1 | 309:bc91f7ba346d | 217 | |
rgrover1 | 309:bc91f7ba346d | 218 | // delete uriBeaconConfig; |
rgrover1 | 309:bc91f7ba346d | 219 | // uriBeaconConfig = NULL; |
rgrover1 | 309:bc91f7ba346d | 220 | |
rgrover1 | 309:bc91f7ba346d | 221 | // ble.clearAdvertisingPayload(); |
rgrover1 | 309:bc91f7ba346d | 222 | // ble.setTxPower(firmwarePowerLevels[txPowerMode]); |
rgrover1 | 309:bc91f7ba346d | 223 | |
rgrover1 | 309:bc91f7ba346d | 224 | // ble.setAdvertisingType( |
rgrover1 | 309:bc91f7ba346d | 225 | // GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
rgrover1 | 309:bc91f7ba346d | 226 | |
rgrover1 | 309:bc91f7ba346d | 227 | // ble.setAdvertisingInterval( |
rgrover1 | 309:bc91f7ba346d | 228 | // Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriod)); |
rgrover1 | 309:bc91f7ba346d | 229 | |
rgrover1 | 309:bc91f7ba346d | 230 | // ble.accumulateAdvertisingPayload( |
rgrover1 | 309:bc91f7ba346d | 231 | // GapAdvertisingData::BREDR_NOT_SUPPORTED | |
rgrover1 | 309:bc91f7ba346d | 232 | // GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 309:bc91f7ba346d | 233 | |
rgrover1 | 309:bc91f7ba346d | 234 | // ble.accumulateAdvertisingPayload( |
rgrover1 | 309:bc91f7ba346d | 235 | // GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, |
rgrover1 | 309:bc91f7ba346d | 236 | // sizeof(BEACON_UUID)); |
rgrover1 | 309:bc91f7ba346d | 237 | |
rgrover1 | 309:bc91f7ba346d | 238 | // serviceData[serviceDataLen++] = BEACON_UUID[0]; |
rgrover1 | 309:bc91f7ba346d | 239 | // serviceData[serviceDataLen++] = BEACON_UUID[1]; |
rgrover1 | 309:bc91f7ba346d | 240 | // serviceData[serviceDataLen++] = flags; |
rgrover1 | 309:bc91f7ba346d | 241 | // serviceData[serviceDataLen++] = advPowerLevels[txPowerMode]; |
rgrover1 | 309:bc91f7ba346d | 242 | // for (int j=0; j < uriDataLength; j++) { |
rgrover1 | 309:bc91f7ba346d | 243 | // serviceData[serviceDataLen++] = uriData[j]; |
rgrover1 | 309:bc91f7ba346d | 244 | // } |
rgrover1 | 309:bc91f7ba346d | 245 | |
rgrover1 | 309:bc91f7ba346d | 246 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen); |
rgrover1 | 309:bc91f7ba346d | 247 | |
rgrover1 | 309:bc91f7ba346d | 248 | // ble.startAdvertising(); |
rgrover1 | 309:bc91f7ba346d | 249 | } |
rgrover1 | 309:bc91f7ba346d | 250 | |
rgrover1 | 309:bc91f7ba346d | 251 | // After advertising timeout, stop config and switch to UriBeacon |
rgrover1 | 309:bc91f7ba346d | 252 | void timeout(void) |
rgrover1 | 309:bc91f7ba346d | 253 | { |
rgrover1 | 309:bc91f7ba346d | 254 | Gap::GapState_t state; |
rgrover1 | 309:bc91f7ba346d | 255 | state = ble.getGapState(); |
rgrover1 | 309:bc91f7ba346d | 256 | if (!state.connected) { |
rgrover1 | 309:bc91f7ba346d | 257 | setupUriBeaconAdvertisements(); |
rgrover1 | 309:bc91f7ba346d | 258 | configAdvertisementTimeoutTicker.detach(); |
rgrover1 | 309:bc91f7ba346d | 259 | } |
rgrover1 | 309:bc91f7ba346d | 260 | } |
rgrover1 | 287:cca148e535b1 | 261 | |
rgrover1 | 287:cca148e535b1 | 262 | private: |
rgrover1 | 287:cca148e535b1 | 263 | // True if the lock bits are non-zero |
rgrover1 | 287:cca148e535b1 | 264 | bool isLocked() { |
rgrover1 | 287:cca148e535b1 | 265 | Lock_t testLock; |
rgrover1 | 287:cca148e535b1 | 266 | memset(testLock, 0, sizeof(Lock_t)); |
rgrover1 | 287:cca148e535b1 | 267 | return memcmp(params.lock, testLock, sizeof(Lock_t)); |
rgrover1 | 171:6092e61690dc | 268 | } |
rgrover1 | 171:6092e61690dc | 269 | |
rgrover1 | 287:cca148e535b1 | 270 | /* |
rgrover1 | 287:cca148e535b1 | 271 | * This callback is invoked when a GATT client attempts to modify any of the |
rgrover1 | 287:cca148e535b1 | 272 | * characteristics of this service. Attempts to do so are also applied to |
rgrover1 | 287:cca148e535b1 | 273 | * the internal state of this service object. |
rgrover1 | 286:898ff71b9502 | 274 | */ |
rgrover1 | 287:cca148e535b1 | 275 | void onDataWrittenCallback(const GattCharacteristicWriteCBParams *writeParams) { |
rgrover1 | 287:cca148e535b1 | 276 | uint16_t handle = writeParams->charHandle; |
rgrover1 | 286:898ff71b9502 | 277 | |
rgrover1 | 287:cca148e535b1 | 278 | if (handle == lockChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 279 | // Validated earlier |
rgrover1 | 287:cca148e535b1 | 280 | memcpy(params.lock, writeParams->data, sizeof(Lock_t)); |
rgrover1 | 287:cca148e535b1 | 281 | // use isLocked() in case bits are being set to all 0's |
rgrover1 | 287:cca148e535b1 | 282 | lockedState = isLocked(); |
rgrover1 | 287:cca148e535b1 | 283 | } else if (handle == unlockChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 284 | // Validated earlier |
rgrover1 | 287:cca148e535b1 | 285 | memset(params.lock, 0, sizeof(Lock_t)); |
rgrover1 | 287:cca148e535b1 | 286 | lockedState = false; |
rgrover1 | 287:cca148e535b1 | 287 | } else if (handle == uriDataChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 288 | params.uriDataLength = writeParams->len; |
rgrover1 | 287:cca148e535b1 | 289 | memcpy(params.uriData, writeParams->data, params.uriDataLength); |
rgrover1 | 287:cca148e535b1 | 290 | } else if (handle == flagsChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 291 | params.flags = *(writeParams->data); |
rgrover1 | 287:cca148e535b1 | 292 | } else if (handle == advPowerLevelsChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 293 | memcpy(params.advPowerLevels, writeParams->data, sizeof(PowerLevels_t)); |
rgrover1 | 287:cca148e535b1 | 294 | } else if (handle == txPowerModeChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 295 | params.txPowerMode = *(writeParams->data); |
rgrover1 | 287:cca148e535b1 | 296 | } else if (handle == beaconPeriodChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 297 | params.beaconPeriod = *((uint16_t *)(writeParams->data)); |
rgrover1 | 287:cca148e535b1 | 298 | } else if (handle == resetChar.getValueHandle()) { |
rgrover1 | 287:cca148e535b1 | 299 | resetToDefaults(); |
rgrover1 | 185:7cd70497aec8 | 300 | } |
rgrover1 | 185:7cd70497aec8 | 301 | } |
rgrover1 | 185:7cd70497aec8 | 302 | |
rgrover1 | 287:cca148e535b1 | 303 | /* |
rgrover1 | 287:cca148e535b1 | 304 | * Reset the default values. |
rgrover1 | 242:0e9201b67e2f | 305 | */ |
rgrover1 | 287:cca148e535b1 | 306 | void resetToDefaults(void) { |
rgrover1 | 287:cca148e535b1 | 307 | lockedState = false; |
rgrover1 | 287:cca148e535b1 | 308 | memset(params.lock, 0, sizeof(Lock_t)); |
rgrover1 | 287:cca148e535b1 | 309 | memcpy(params.uriData, defaultUriData, URI_DATA_MAX); |
rgrover1 | 287:cca148e535b1 | 310 | params.uriDataLength = defaultUriDataLength; |
rgrover1 | 287:cca148e535b1 | 311 | params.flags = 0; |
rgrover1 | 287:cca148e535b1 | 312 | memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t)); |
rgrover1 | 287:cca148e535b1 | 313 | params.txPowerMode = TX_POWER_MODE_LOW; |
rgrover1 | 287:cca148e535b1 | 314 | params.beaconPeriod = 1000; |
rgrover1 | 287:cca148e535b1 | 315 | updateCharacteristicValues(); |
rgrover1 | 211:f181effe5de3 | 316 | } |
rgrover1 | 211:f181effe5de3 | 317 | |
mbedAustin | 232:4cfb5b8a4fb9 | 318 | /* |
rgrover1 | 287:cca148e535b1 | 319 | * Internal helper function used to update the GATT database following any |
rgrover1 | 287:cca148e535b1 | 320 | * change to the internal state of the service object. |
rgrover1 | 242:0e9201b67e2f | 321 | */ |
rgrover1 | 287:cca148e535b1 | 322 | void updateCharacteristicValues(void) { |
rgrover1 | 287:cca148e535b1 | 323 | ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), &lockedState, 1); |
rgrover1 | 287:cca148e535b1 | 324 | ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength); |
rgrover1 | 287:cca148e535b1 | 325 | ble.updateCharacteristicValue(flagsChar.getValueHandle(), ¶ms.flags, 1); |
rgrover1 | 287:cca148e535b1 | 326 | ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), |
rgrover1 | 287:cca148e535b1 | 327 | reinterpret_cast<uint8_t *>(¶ms.beaconPeriod), sizeof(uint16_t)); |
rgrover1 | 287:cca148e535b1 | 328 | ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), ¶ms.txPowerMode, 1); |
rgrover1 | 287:cca148e535b1 | 329 | ble.updateCharacteristicValue(advPowerLevelsChar.getValueHandle(), |
rgrover1 | 287:cca148e535b1 | 330 | reinterpret_cast<uint8_t *>(params.advPowerLevels), sizeof(PowerLevels_t)); |
rgrover1 | 287:cca148e535b1 | 331 | } |
rgrover1 | 287:cca148e535b1 | 332 | |
rgrover1 | 287:cca148e535b1 | 333 | private: |
rgrover1 | 287:cca148e535b1 | 334 | void lockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 287:cca148e535b1 | 335 | authParams->authorizationReply = (authParams->len == sizeof(Lock_t)) && !lockedState; |
rgrover1 | 287:cca148e535b1 | 336 | } |
rgrover1 | 287:cca148e535b1 | 337 | |
rgrover1 | 287:cca148e535b1 | 338 | |
rgrover1 | 287:cca148e535b1 | 339 | void unlockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 287:cca148e535b1 | 340 | if (!lockedState || (authParams->len == sizeof(Lock_t) && (memcmp(authParams->data, params.lock, sizeof(Lock_t)) == 0))) { |
rgrover1 | 287:cca148e535b1 | 341 | authParams->authorizationReply = true; |
rgrover1 | 287:cca148e535b1 | 342 | } else { |
rgrover1 | 287:cca148e535b1 | 343 | authParams->authorizationReply = false; |
rgrover1 | 287:cca148e535b1 | 344 | } |
rgrover1 | 287:cca148e535b1 | 345 | } |
rgrover1 | 287:cca148e535b1 | 346 | |
rgrover1 | 287:cca148e535b1 | 347 | void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 287:cca148e535b1 | 348 | if (lockedState || (authParams->offset != 0) || (authParams->len > URI_DATA_MAX)) { |
rgrover1 | 287:cca148e535b1 | 349 | authParams->authorizationReply = false; |
rgrover1 | 287:cca148e535b1 | 350 | } |
rgrover1 | 287:cca148e535b1 | 351 | } |
rgrover1 | 287:cca148e535b1 | 352 | |
rgrover1 | 287:cca148e535b1 | 353 | void flagsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 287:cca148e535b1 | 354 | if (lockedState || authParams->len != 1) { |
rgrover1 | 287:cca148e535b1 | 355 | authParams->authorizationReply = false; |
rgrover1 | 287:cca148e535b1 | 356 | } |
rgrover1 | 287:cca148e535b1 | 357 | } |
rgrover1 | 287:cca148e535b1 | 358 | |
rgrover1 | 287:cca148e535b1 | 359 | void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *authParams) { |
rgrover1 | 287:cca148e535b1 | 360 | if (lockedState) { |
rgrover1 | 287:cca148e535b1 | 361 | authParams->authorizationReply = false; |
rgrover1 | 287:cca148e535b1 | 362 | } |
rgrover1 | 287:cca148e535b1 | 363 | } |
rgrover1 | 287:cca148e535b1 | 364 | |
rgrover1 | 287:cca148e535b1 | 365 | BLEDevice &ble; |
rgrover1 | 287:cca148e535b1 | 366 | Params_t ¶ms; |
rgrover1 | 287:cca148e535b1 | 367 | // Default value that is restored on reset |
rgrover1 | 309:bc91f7ba346d | 368 | size_t defaultUriDataLength; |
rgrover1 | 309:bc91f7ba346d | 369 | UriData_t defaultUriData; |
rgrover1 | 287:cca148e535b1 | 370 | // Default value that is restored on reset |
rgrover1 | 287:cca148e535b1 | 371 | PowerLevels_t &defaultAdvPowerLevels; |
rgrover1 | 287:cca148e535b1 | 372 | uint8_t lockedState; |
rgrover1 | 287:cca148e535b1 | 373 | bool initSucceeded; |
rgrover1 | 287:cca148e535b1 | 374 | uint8_t resetFlag; |
rgrover1 | 309:bc91f7ba346d | 375 | Ticker configAdvertisementTimeoutTicker; |
rgrover1 | 287:cca148e535b1 | 376 | |
rgrover1 | 287:cca148e535b1 | 377 | ReadOnlyGattCharacteristic<uint8_t> lockedStateChar; |
rgrover1 | 287:cca148e535b1 | 378 | WriteOnlyGattCharacteristic<Lock_t> lockChar; |
rgrover1 | 287:cca148e535b1 | 379 | GattCharacteristic uriDataChar; |
rgrover1 | 287:cca148e535b1 | 380 | WriteOnlyGattCharacteristic<Lock_t> unlockChar; |
rgrover1 | 287:cca148e535b1 | 381 | ReadWriteGattCharacteristic<uint8_t> flagsChar; |
rgrover1 | 287:cca148e535b1 | 382 | ReadWriteGattCharacteristic<PowerLevels_t> advPowerLevelsChar; |
rgrover1 | 287:cca148e535b1 | 383 | ReadWriteGattCharacteristic<uint8_t> txPowerModeChar; |
rgrover1 | 287:cca148e535b1 | 384 | ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar; |
rgrover1 | 287:cca148e535b1 | 385 | WriteOnlyGattCharacteristic<uint8_t> resetChar; |
rgrover1 | 287:cca148e535b1 | 386 | |
rgrover1 | 287:cca148e535b1 | 387 | public: |
rgrover1 | 287:cca148e535b1 | 388 | /* |
rgrover1 | 287:cca148e535b1 | 389 | * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification). |
rgrover1 | 287:cca148e535b1 | 390 | */ |
rgrover1 | 287:cca148e535b1 | 391 | static void encodeURI(const char *uriDataIn, UriData_t uriDataOut, size_t &sizeofURIDataOut) { |
rgrover1 | 287:cca148e535b1 | 392 | sizeofURIDataOut = 0; |
rgrover1 | 287:cca148e535b1 | 393 | memset(uriDataOut, 0, sizeof(UriData_t)); |
rgrover1 | 287:cca148e535b1 | 394 | |
rgrover1 | 287:cca148e535b1 | 395 | if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) { |
rgrover1 | 287:cca148e535b1 | 396 | return; |
rgrover1 | 284:82fefb5be8ed | 397 | } |
rgrover1 | 284:82fefb5be8ed | 398 | |
rgrover1 | 287:cca148e535b1 | 399 | /* |
rgrover1 | 287:cca148e535b1 | 400 | * handle prefix |
rgrover1 | 287:cca148e535b1 | 401 | */ |
rgrover1 | 284:82fefb5be8ed | 402 | const char *prefixes[] = { |
rgrover1 | 284:82fefb5be8ed | 403 | "http://www.", |
rgrover1 | 284:82fefb5be8ed | 404 | "https://www.", |
rgrover1 | 284:82fefb5be8ed | 405 | "http://", |
rgrover1 | 284:82fefb5be8ed | 406 | "https://", |
rgrover1 | 284:82fefb5be8ed | 407 | "urn:uuid:" |
rgrover1 | 284:82fefb5be8ed | 408 | }; |
rgrover1 | 287:cca148e535b1 | 409 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 284:82fefb5be8ed | 410 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 284:82fefb5be8ed | 411 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 287:cca148e535b1 | 412 | if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) { |
rgrover1 | 287:cca148e535b1 | 413 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 287:cca148e535b1 | 414 | uriDataIn += prefixLen; |
rgrover1 | 284:82fefb5be8ed | 415 | break; |
rgrover1 | 284:82fefb5be8ed | 416 | } |
rgrover1 | 284:82fefb5be8ed | 417 | } |
rgrover1 | 284:82fefb5be8ed | 418 | |
rgrover1 | 287:cca148e535b1 | 419 | /* |
rgrover1 | 287:cca148e535b1 | 420 | * handle suffixes |
rgrover1 | 287:cca148e535b1 | 421 | */ |
rgrover1 | 284:82fefb5be8ed | 422 | const char *suffixes[] = { |
rgrover1 | 284:82fefb5be8ed | 423 | ".com/", |
rgrover1 | 284:82fefb5be8ed | 424 | ".org/", |
rgrover1 | 284:82fefb5be8ed | 425 | ".edu/", |
rgrover1 | 284:82fefb5be8ed | 426 | ".net/", |
rgrover1 | 284:82fefb5be8ed | 427 | ".info/", |
rgrover1 | 284:82fefb5be8ed | 428 | ".biz/", |
rgrover1 | 284:82fefb5be8ed | 429 | ".gov/", |
rgrover1 | 284:82fefb5be8ed | 430 | ".com", |
rgrover1 | 284:82fefb5be8ed | 431 | ".org", |
rgrover1 | 284:82fefb5be8ed | 432 | ".edu", |
rgrover1 | 284:82fefb5be8ed | 433 | ".net", |
rgrover1 | 284:82fefb5be8ed | 434 | ".info", |
rgrover1 | 284:82fefb5be8ed | 435 | ".biz", |
rgrover1 | 284:82fefb5be8ed | 436 | ".gov" |
rgrover1 | 284:82fefb5be8ed | 437 | }; |
rgrover1 | 284:82fefb5be8ed | 438 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 287:cca148e535b1 | 439 | while (*uriDataIn && (sizeofURIDataOut < URI_DATA_MAX)) { |
rgrover1 | 284:82fefb5be8ed | 440 | /* check for suffix match */ |
rgrover1 | 284:82fefb5be8ed | 441 | unsigned i; |
rgrover1 | 284:82fefb5be8ed | 442 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 284:82fefb5be8ed | 443 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 287:cca148e535b1 | 444 | if (suffixLen == 0) { |
rgrover1 | 284:82fefb5be8ed | 445 | continue; |
rgrover1 | 284:82fefb5be8ed | 446 | } |
rgrover1 | 284:82fefb5be8ed | 447 | |
rgrover1 | 287:cca148e535b1 | 448 | if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) { |
rgrover1 | 287:cca148e535b1 | 449 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 287:cca148e535b1 | 450 | uriDataIn += suffixLen; |
rgrover1 | 284:82fefb5be8ed | 451 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 284:82fefb5be8ed | 452 | } |
rgrover1 | 284:82fefb5be8ed | 453 | } |
rgrover1 | 284:82fefb5be8ed | 454 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 284:82fefb5be8ed | 455 | if (i == NUM_SUFFIXES) { |
rgrover1 | 287:cca148e535b1 | 456 | uriDataOut[sizeofURIDataOut++] = *uriDataIn; |
rgrover1 | 287:cca148e535b1 | 457 | ++uriDataIn; |
rgrover1 | 284:82fefb5be8ed | 458 | } |
rgrover1 | 284:82fefb5be8ed | 459 | } |
rgrover1 | 284:82fefb5be8ed | 460 | } |
rgrover1 | 171:6092e61690dc | 461 | }; |
rgrover1 | 171:6092e61690dc | 462 | |
rgrover1 | 287:cca148e535b1 | 463 | #endif // SERVICES_URIBEACONCONFIGSERVICE_H_ |