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