Improve readability with getHandle inline
Fork of BLE_API by
services/URIBeacon2Service.h@177:17f4b5924fe9, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:23 2014 +0000
- Revision:
- 177:17f4b5924fe9
- Parent:
- 176:a213fd50613d
- Child:
- 178:b6d6a2a11e86
Synchronized with git rev 8b1e9e95
Author: Rohit Grover
add APIs to set power modes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 171:6092e61690dc | 1 | /* mbed Microcontroller Library |
rgrover1 | 171:6092e61690dc | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 171:6092e61690dc | 3 | * |
rgrover1 | 171:6092e61690dc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 171:6092e61690dc | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 171:6092e61690dc | 6 | * You may obtain a copy of the License at |
rgrover1 | 171:6092e61690dc | 7 | * |
rgrover1 | 171:6092e61690dc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 171:6092e61690dc | 9 | * |
rgrover1 | 171:6092e61690dc | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 171:6092e61690dc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 171:6092e61690dc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 171:6092e61690dc | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 171:6092e61690dc | 14 | * limitations under the License. |
rgrover1 | 171:6092e61690dc | 15 | */ |
rgrover1 | 171:6092e61690dc | 16 | |
rgrover1 | 171:6092e61690dc | 17 | #ifndef __BLE_URI_BEACON_2_SERVICE_H__ |
rgrover1 | 171:6092e61690dc | 18 | #define __BLE_URI_BEACON_2_SERVICE_H__ |
rgrover1 | 171:6092e61690dc | 19 | |
rgrover1 | 171:6092e61690dc | 20 | #include "BLEDevice.h" |
rgrover1 | 171:6092e61690dc | 21 | |
rgrover1 | 171:6092e61690dc | 22 | #define UUID_INITIALIZER_LIST(FIRST, SECOND) { \ |
rgrover1 | 171:6092e61690dc | 23 | 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ |
rgrover1 | 171:6092e61690dc | 24 | 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \ |
rgrover1 | 171:6092e61690dc | 25 | } |
rgrover1 | 171:6092e61690dc | 26 | const uint8_t URIBeacon2ControlServiceUUID[] = UUID_INITIALIZER_LIST(0x20, 0x80); |
rgrover1 | 171:6092e61690dc | 27 | const uint8_t lockedStateCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x81); |
rgrover1 | 171:6092e61690dc | 28 | const uint8_t uriDataCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x84); |
rgrover1 | 171:6092e61690dc | 29 | const uint8_t flagsCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x85); |
rgrover1 | 171:6092e61690dc | 30 | const uint8_t txPowerCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x86); |
rgrover1 | 171:6092e61690dc | 31 | const uint8_t beaconPeriodCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x88); |
rgrover1 | 171:6092e61690dc | 32 | |
rgrover1 | 171:6092e61690dc | 33 | class URIBeacon2Service { |
rgrover1 | 175:4e85f7225f8f | 34 | enum TXPowerModes_t { |
rgrover1 | 175:4e85f7225f8f | 35 | TX_POWER_MODE_LOWEST = 0, |
rgrover1 | 175:4e85f7225f8f | 36 | TX_POWER_MODE_LOW = 1, |
rgrover1 | 175:4e85f7225f8f | 37 | TX_POWER_MODE_MEDIUM = 2, |
rgrover1 | 175:4e85f7225f8f | 38 | TX_POWER_MODE_HIGH = 3, |
rgrover1 | 177:17f4b5924fe9 | 39 | NUM_POWER_MODES |
rgrover1 | 175:4e85f7225f8f | 40 | }; |
rgrover1 | 175:4e85f7225f8f | 41 | |
rgrover1 | 171:6092e61690dc | 42 | public: |
rgrover1 | 172:fb95afa93697 | 43 | /** |
rgrover1 | 172:fb95afa93697 | 44 | * @param[ref] ble |
rgrover1 | 172:fb95afa93697 | 45 | * BLEDevice object for the underlying controller. |
rgrover1 | 172:fb95afa93697 | 46 | * @param[in] urldata |
rgrover1 | 172:fb95afa93697 | 47 | * URI as a null-terminated string. |
rgrover1 | 172:fb95afa93697 | 48 | * @param[in] flagsIn |
rgrover1 | 172:fb95afa93697 | 49 | * UriBeacon Flags. |
rgrover1 | 174:5ae7781362b6 | 50 | * @param[in] effectiveTxPowerIn |
rgrover1 | 176:a213fd50613d | 51 | * UriBeacon Tx Power Level in dBm. |
rgrover1 | 172:fb95afa93697 | 52 | * @param[in] beaconPeriodIn |
rgrover1 | 172:fb95afa93697 | 53 | * The period in milliseconds that a UriBeacon packet is |
rgrover1 | 172:fb95afa93697 | 54 | * transmitted. A value of zero disables UriBeacon |
rgrover1 | 172:fb95afa93697 | 55 | * transmissions. |
rgrover1 | 172:fb95afa93697 | 56 | */ |
rgrover1 | 174:5ae7781362b6 | 57 | URIBeacon2Service(BLEDevice &ble_, const char *urldata, uint8_t flagsIn = 0, int8_t effectiveTxPowerIn = 0, uint16_t beaconPeriodIn = 1000) : |
rgrover1 | 176:a213fd50613d | 58 | ble(ble_), |
rgrover1 | 176:a213fd50613d | 59 | payloadIndex(0), |
rgrover1 | 176:a213fd50613d | 60 | serviceDataPayload(), |
rgrover1 | 171:6092e61690dc | 61 | lockedState(false), |
rgrover1 | 171:6092e61690dc | 62 | uriDataLength(0), |
rgrover1 | 171:6092e61690dc | 63 | uriDataValue(), |
rgrover1 | 171:6092e61690dc | 64 | flags(flagsIn), |
rgrover1 | 174:5ae7781362b6 | 65 | effectivePower(effectiveTxPowerIn), |
rgrover1 | 177:17f4b5924fe9 | 66 | powerLevels(), |
rgrover1 | 171:6092e61690dc | 67 | beaconPeriod(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriodIn)), |
rgrover1 | 171:6092e61690dc | 68 | lockedStateChar(lockedStateCharUUID, (uint8_t *)&lockedState, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 171:6092e61690dc | 69 | uriDataChar(uriDataCharUUID, |
rgrover1 | 171:6092e61690dc | 70 | uriDataValue, |
rgrover1 | 171:6092e61690dc | 71 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 171:6092e61690dc | 72 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 171:6092e61690dc | 73 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
rgrover1 | 171:6092e61690dc | 74 | flagsChar(flagsCharUUID, &flags, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
rgrover1 | 174:5ae7781362b6 | 75 | // txPowerChar(txPowerCharUUID, &effectivePower, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) |
rgrover1 | 171:6092e61690dc | 76 | beaconPeriodChar(beaconPeriodCharUUID, (uint8_t *)&beaconPeriod, 2, 2, |
rgrover1 | 171:6092e61690dc | 77 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) |
rgrover1 | 171:6092e61690dc | 78 | { |
rgrover1 | 171:6092e61690dc | 79 | if ((urldata == NULL) || ((uriDataLength = strlen(urldata)) == 0)) { |
rgrover1 | 171:6092e61690dc | 80 | return; |
rgrover1 | 171:6092e61690dc | 81 | } |
rgrover1 | 171:6092e61690dc | 82 | strncpy(reinterpret_cast<char *>(uriDataValue), urldata, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 171:6092e61690dc | 83 | |
rgrover1 | 171:6092e61690dc | 84 | setup(); |
rgrover1 | 171:6092e61690dc | 85 | |
rgrover1 | 171:6092e61690dc | 86 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 171:6092e61690dc | 87 | if (serviceAdded) { |
rgrover1 | 171:6092e61690dc | 88 | return; |
rgrover1 | 171:6092e61690dc | 89 | } |
rgrover1 | 171:6092e61690dc | 90 | |
rgrover1 | 171:6092e61690dc | 91 | GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar, &flagsChar, &beaconPeriodChar}; |
rgrover1 | 171:6092e61690dc | 92 | GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 171:6092e61690dc | 93 | |
rgrover1 | 171:6092e61690dc | 94 | ble.addService(beaconControlService); |
rgrover1 | 171:6092e61690dc | 95 | serviceAdded = true; |
rgrover1 | 171:6092e61690dc | 96 | |
rgrover1 | 171:6092e61690dc | 97 | ble.onDataWritten(this, &URIBeacon2Service::onDataWritten); |
rgrover1 | 171:6092e61690dc | 98 | } |
rgrover1 | 171:6092e61690dc | 99 | |
rgrover1 | 173:05c4b1cea65f | 100 | /** |
rgrover1 | 173:05c4b1cea65f | 101 | * Update flags of the URIBeacon dynamically. |
rgrover1 | 173:05c4b1cea65f | 102 | * |
rgrover1 | 173:05c4b1cea65f | 103 | * @param[in] flagsIn |
rgrover1 | 173:05c4b1cea65f | 104 | * |
rgrover1 | 173:05c4b1cea65f | 105 | * ### UriBeacon Flags |
rgrover1 | 173:05c4b1cea65f | 106 | * Bit | Description |
rgrover1 | 173:05c4b1cea65f | 107 | * :---- | :---------- |
rgrover1 | 173:05c4b1cea65f | 108 | * 0 | Invisible Hint |
rgrover1 | 173:05c4b1cea65f | 109 | * 1..7 | Reserved for future use. Must be zero. |
rgrover1 | 173:05c4b1cea65f | 110 | * |
rgrover1 | 173:05c4b1cea65f | 111 | * The `Invisible Hint` flag is a command for the user-agent that tells |
rgrover1 | 173:05c4b1cea65f | 112 | * it not to access or display the UriBeacon. This is a guideline only, |
rgrover1 | 173:05c4b1cea65f | 113 | * and is not a blocking method. User agents may, with user approval, |
rgrover1 | 173:05c4b1cea65f | 114 | * display invisible beacons. |
rgrover1 | 173:05c4b1cea65f | 115 | */ |
rgrover1 | 171:6092e61690dc | 116 | void setFlags(uint8_t flagsIn) { |
rgrover1 | 171:6092e61690dc | 117 | flags = flagsIn; |
rgrover1 | 171:6092e61690dc | 118 | setup(); |
rgrover1 | 171:6092e61690dc | 119 | } |
rgrover1 | 171:6092e61690dc | 120 | |
rgrover1 | 177:17f4b5924fe9 | 121 | /** |
rgrover1 | 177:17f4b5924fe9 | 122 | * Update the txPower for a particular mode in the powerLevels table. |
rgrover1 | 177:17f4b5924fe9 | 123 | */ |
rgrover1 | 177:17f4b5924fe9 | 124 | void setTxPowerLevel(TXPowerModes_t mode, int8_t txPowerIn) { |
rgrover1 | 177:17f4b5924fe9 | 125 | powerLevels[mode] = txPowerIn; |
rgrover1 | 177:17f4b5924fe9 | 126 | } |
rgrover1 | 177:17f4b5924fe9 | 127 | |
rgrover1 | 177:17f4b5924fe9 | 128 | /** |
rgrover1 | 177:17f4b5924fe9 | 129 | * Set the effective power mode from one of the values in the powerLevels tables. |
rgrover1 | 177:17f4b5924fe9 | 130 | */ |
rgrover1 | 177:17f4b5924fe9 | 131 | void setPowerMode(TXPowerModes_t mode) { |
rgrover1 | 177:17f4b5924fe9 | 132 | effectivePower = powerLevels[mode]; |
rgrover1 | 171:6092e61690dc | 133 | setup(); |
rgrover1 | 171:6092e61690dc | 134 | } |
rgrover1 | 171:6092e61690dc | 135 | |
rgrover1 | 171:6092e61690dc | 136 | void setBeaconPeriod(uint16_t beaconPeriodIn) { |
rgrover1 | 171:6092e61690dc | 137 | beaconPeriod = beaconPeriodIn; |
rgrover1 | 171:6092e61690dc | 138 | setup(); |
rgrover1 | 171:6092e61690dc | 139 | } |
rgrover1 | 171:6092e61690dc | 140 | |
rgrover1 | 171:6092e61690dc | 141 | protected: |
rgrover1 | 171:6092e61690dc | 142 | /** |
rgrover1 | 171:6092e61690dc | 143 | * This callback allows the DFU service to receive the initial trigger to |
rgrover1 | 171:6092e61690dc | 144 | * handover control to the bootloader; but first the application is given a |
rgrover1 | 171:6092e61690dc | 145 | * chance to clean up. |
rgrover1 | 171:6092e61690dc | 146 | */ |
rgrover1 | 171:6092e61690dc | 147 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 171:6092e61690dc | 148 | if (params->charHandle == uriDataChar.getValueAttribute().getHandle()) { |
rgrover1 | 171:6092e61690dc | 149 | if (lockedState) { /* When locked, the device isn't allowed to update the uriData characteristic. */ |
rgrover1 | 171:6092e61690dc | 150 | /* Restore GATT database with previous value. */ |
rgrover1 | 171:6092e61690dc | 151 | ble.updateCharacteristicValue(uriDataChar.getValueAttribute().getHandle(), uriDataValue, uriDataLength); |
rgrover1 | 171:6092e61690dc | 152 | return; |
rgrover1 | 171:6092e61690dc | 153 | } |
rgrover1 | 171:6092e61690dc | 154 | |
rgrover1 | 171:6092e61690dc | 155 | /* We don't handle very large writes at the moment. */ |
rgrover1 | 171:6092e61690dc | 156 | if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) { |
rgrover1 | 171:6092e61690dc | 157 | return; |
rgrover1 | 171:6092e61690dc | 158 | } |
rgrover1 | 171:6092e61690dc | 159 | |
rgrover1 | 171:6092e61690dc | 160 | uriDataLength = params->len; |
rgrover1 | 171:6092e61690dc | 161 | memcpy(uriDataValue, params->data, uriDataLength); |
rgrover1 | 171:6092e61690dc | 162 | } else if (params->charHandle == flagsChar.getValueAttribute().getHandle()) { |
rgrover1 | 171:6092e61690dc | 163 | if (lockedState) { /* When locked, the device isn't allowed to update the flags characteristic. */ |
rgrover1 | 171:6092e61690dc | 164 | /* Restore GATT database with previous value. */ |
rgrover1 | 171:6092e61690dc | 165 | ble.updateCharacteristicValue(flagsChar.getValueAttribute().getHandle(), &flags, 1 /* size */); |
rgrover1 | 171:6092e61690dc | 166 | return; |
rgrover1 | 171:6092e61690dc | 167 | } else { |
rgrover1 | 171:6092e61690dc | 168 | flags = *(params->data); |
rgrover1 | 171:6092e61690dc | 169 | } |
rgrover1 | 171:6092e61690dc | 170 | } else if (params->charHandle == beaconPeriodChar.getValueAttribute().getHandle()) { |
rgrover1 | 171:6092e61690dc | 171 | if (lockedState) { /* When locked, the device isn't allowed to update the flags characteristic. */ |
rgrover1 | 171:6092e61690dc | 172 | /* Restore GATT database with previous value. */ |
rgrover1 | 171:6092e61690dc | 173 | ble.updateCharacteristicValue(beaconPeriodChar.getValueAttribute().getHandle(), (uint8_t *)&beaconPeriod, 2 /* size */); |
rgrover1 | 171:6092e61690dc | 174 | return; |
rgrover1 | 171:6092e61690dc | 175 | } else { |
rgrover1 | 171:6092e61690dc | 176 | beaconPeriod = *((uint16_t *)(params->data)); |
rgrover1 | 171:6092e61690dc | 177 | } |
rgrover1 | 171:6092e61690dc | 178 | } |
rgrover1 | 171:6092e61690dc | 179 | setup(); |
rgrover1 | 171:6092e61690dc | 180 | ble.setAdvertisingPayload(); |
rgrover1 | 171:6092e61690dc | 181 | } |
rgrover1 | 171:6092e61690dc | 182 | |
rgrover1 | 171:6092e61690dc | 183 | /** |
rgrover1 | 171:6092e61690dc | 184 | * For debugging only. |
rgrover1 | 171:6092e61690dc | 185 | */ |
rgrover1 | 171:6092e61690dc | 186 | void dumpEncodedSeviceData() const { |
rgrover1 | 171:6092e61690dc | 187 | printf("encoded: '"); |
rgrover1 | 171:6092e61690dc | 188 | for (unsigned i = 0; i < payloadIndex; i++) { |
rgrover1 | 171:6092e61690dc | 189 | printf(" %02x", serviceDataPayload[i]); |
rgrover1 | 171:6092e61690dc | 190 | } |
rgrover1 | 171:6092e61690dc | 191 | printf("'\r\n"); |
rgrover1 | 171:6092e61690dc | 192 | } |
rgrover1 | 171:6092e61690dc | 193 | |
rgrover1 | 171:6092e61690dc | 194 | private: |
rgrover1 | 171:6092e61690dc | 195 | void setup(void) { |
rgrover1 | 171:6092e61690dc | 196 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 171:6092e61690dc | 197 | |
rgrover1 | 171:6092e61690dc | 198 | payloadIndex = 0; |
rgrover1 | 171:6092e61690dc | 199 | serviceDataPayload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 171:6092e61690dc | 200 | serviceDataPayload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 171:6092e61690dc | 201 | serviceDataPayload[payloadIndex++] = flags; |
rgrover1 | 174:5ae7781362b6 | 202 | serviceDataPayload[payloadIndex++] = effectivePower; |
rgrover1 | 171:6092e61690dc | 203 | |
rgrover1 | 171:6092e61690dc | 204 | const char *urlData = reinterpret_cast<char *>(uriDataValue); |
rgrover1 | 171:6092e61690dc | 205 | size_t sizeofURLData = uriDataLength; |
rgrover1 | 171:6092e61690dc | 206 | size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData); |
rgrover1 | 171:6092e61690dc | 207 | |
rgrover1 | 171:6092e61690dc | 208 | ble.clearAdvertisingPayload(); |
rgrover1 | 171:6092e61690dc | 209 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 171:6092e61690dc | 210 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4); |
rgrover1 | 171:6092e61690dc | 211 | |
rgrover1 | 171:6092e61690dc | 212 | ble.setAdvertisingInterval(beaconPeriod); |
rgrover1 | 174:5ae7781362b6 | 213 | ble.setTxPower(effectivePower); |
rgrover1 | 171:6092e61690dc | 214 | } |
rgrover1 | 171:6092e61690dc | 215 | |
rgrover1 | 171:6092e61690dc | 216 | size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) { |
rgrover1 | 171:6092e61690dc | 217 | const char *prefixes[] = { |
rgrover1 | 171:6092e61690dc | 218 | "http://www.", |
rgrover1 | 171:6092e61690dc | 219 | "https://www.", |
rgrover1 | 171:6092e61690dc | 220 | "http://", |
rgrover1 | 171:6092e61690dc | 221 | "https://", |
rgrover1 | 171:6092e61690dc | 222 | "urn:uuid:" |
rgrover1 | 171:6092e61690dc | 223 | }; |
rgrover1 | 171:6092e61690dc | 224 | |
rgrover1 | 171:6092e61690dc | 225 | size_t encodedBytes = 0; |
rgrover1 | 171:6092e61690dc | 226 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 171:6092e61690dc | 227 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 171:6092e61690dc | 228 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 171:6092e61690dc | 229 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 171:6092e61690dc | 230 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 171:6092e61690dc | 231 | encodedBytes = 1; |
rgrover1 | 171:6092e61690dc | 232 | |
rgrover1 | 171:6092e61690dc | 233 | urldata += prefixLen; |
rgrover1 | 171:6092e61690dc | 234 | sizeofURLData -= prefixLen; |
rgrover1 | 171:6092e61690dc | 235 | break; |
rgrover1 | 171:6092e61690dc | 236 | } |
rgrover1 | 171:6092e61690dc | 237 | } |
rgrover1 | 171:6092e61690dc | 238 | |
rgrover1 | 171:6092e61690dc | 239 | return encodedBytes; |
rgrover1 | 171:6092e61690dc | 240 | } |
rgrover1 | 171:6092e61690dc | 241 | |
rgrover1 | 171:6092e61690dc | 242 | size_t encodeURI(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 171:6092e61690dc | 243 | const char *suffixes[] = { |
rgrover1 | 171:6092e61690dc | 244 | ".com/", |
rgrover1 | 171:6092e61690dc | 245 | ".org/", |
rgrover1 | 171:6092e61690dc | 246 | ".edu/", |
rgrover1 | 171:6092e61690dc | 247 | ".net/", |
rgrover1 | 171:6092e61690dc | 248 | ".info/", |
rgrover1 | 171:6092e61690dc | 249 | ".biz/", |
rgrover1 | 171:6092e61690dc | 250 | ".gov/", |
rgrover1 | 171:6092e61690dc | 251 | ".com", |
rgrover1 | 171:6092e61690dc | 252 | ".org", |
rgrover1 | 171:6092e61690dc | 253 | ".edu", |
rgrover1 | 171:6092e61690dc | 254 | ".net", |
rgrover1 | 171:6092e61690dc | 255 | ".info", |
rgrover1 | 171:6092e61690dc | 256 | ".biz", |
rgrover1 | 171:6092e61690dc | 257 | ".gov" |
rgrover1 | 171:6092e61690dc | 258 | }; |
rgrover1 | 171:6092e61690dc | 259 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 171:6092e61690dc | 260 | |
rgrover1 | 171:6092e61690dc | 261 | size_t encodedBytes = 0; |
rgrover1 | 171:6092e61690dc | 262 | while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) { |
rgrover1 | 171:6092e61690dc | 263 | /* check for suffix match */ |
rgrover1 | 171:6092e61690dc | 264 | unsigned i; |
rgrover1 | 171:6092e61690dc | 265 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 171:6092e61690dc | 266 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 171:6092e61690dc | 267 | if ((suffixLen == 0) || (sizeofURLData < suffixLen)) { |
rgrover1 | 171:6092e61690dc | 268 | continue; |
rgrover1 | 171:6092e61690dc | 269 | } |
rgrover1 | 171:6092e61690dc | 270 | |
rgrover1 | 171:6092e61690dc | 271 | if (strncmp(urldata, suffixes[i], suffixLen) == 0) { |
rgrover1 | 171:6092e61690dc | 272 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 171:6092e61690dc | 273 | ++encodedBytes; |
rgrover1 | 171:6092e61690dc | 274 | urldata += suffixLen; |
rgrover1 | 171:6092e61690dc | 275 | sizeofURLData -= suffixLen; |
rgrover1 | 171:6092e61690dc | 276 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 171:6092e61690dc | 277 | } |
rgrover1 | 171:6092e61690dc | 278 | } |
rgrover1 | 171:6092e61690dc | 279 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 171:6092e61690dc | 280 | if (i == NUM_SUFFIXES) { |
rgrover1 | 171:6092e61690dc | 281 | serviceDataPayload[payloadIndex++] = *urldata; |
rgrover1 | 171:6092e61690dc | 282 | ++encodedBytes; |
rgrover1 | 171:6092e61690dc | 283 | ++urldata; |
rgrover1 | 171:6092e61690dc | 284 | --sizeofURLData; |
rgrover1 | 171:6092e61690dc | 285 | } |
rgrover1 | 171:6092e61690dc | 286 | } |
rgrover1 | 171:6092e61690dc | 287 | |
rgrover1 | 171:6092e61690dc | 288 | return encodedBytes; |
rgrover1 | 171:6092e61690dc | 289 | } |
rgrover1 | 171:6092e61690dc | 290 | |
rgrover1 | 171:6092e61690dc | 291 | private: |
rgrover1 | 171:6092e61690dc | 292 | static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 27; |
rgrover1 | 171:6092e61690dc | 293 | static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; |
rgrover1 | 171:6092e61690dc | 294 | |
rgrover1 | 171:6092e61690dc | 295 | private: |
rgrover1 | 171:6092e61690dc | 296 | BLEDevice &ble; |
rgrover1 | 171:6092e61690dc | 297 | |
rgrover1 | 171:6092e61690dc | 298 | size_t payloadIndex; |
rgrover1 | 171:6092e61690dc | 299 | uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD]; |
rgrover1 | 171:6092e61690dc | 300 | bool lockedState; |
rgrover1 | 171:6092e61690dc | 301 | uint16_t uriDataLength; |
rgrover1 | 171:6092e61690dc | 302 | uint8_t uriDataValue[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 171:6092e61690dc | 303 | uint8_t flags; |
rgrover1 | 174:5ae7781362b6 | 304 | int8_t effectivePower; |
rgrover1 | 177:17f4b5924fe9 | 305 | int8_t powerLevels[NUM_POWER_MODES]; |
rgrover1 | 171:6092e61690dc | 306 | uint16_t beaconPeriod; |
rgrover1 | 171:6092e61690dc | 307 | |
rgrover1 | 171:6092e61690dc | 308 | GattCharacteristic lockedStateChar; |
rgrover1 | 171:6092e61690dc | 309 | GattCharacteristic uriDataChar; |
rgrover1 | 171:6092e61690dc | 310 | GattCharacteristic flagsChar; |
rgrover1 | 171:6092e61690dc | 311 | // GattCharacteristic txPowerChar; |
rgrover1 | 171:6092e61690dc | 312 | GattCharacteristic beaconPeriodChar; |
rgrover1 | 171:6092e61690dc | 313 | }; |
rgrover1 | 171:6092e61690dc | 314 | |
rgrover1 | 171:6092e61690dc | 315 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |