I don't know why this is happening.
Fork of BLE_API by
services/URIBeacon2Service.h@164:93e52c3861a9, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:21 2014 +0000
- Revision:
- 164:93e52c3861a9
- Parent:
- 163:8bd70d17589f
- Child:
- 165:ca406c1d0399
Synchronized with git rev f5827433
Author: Rohit Grover
restore uriData characteristic upon receiving a write if locked.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 147:f772d9b93808 | 1 | /* mbed Microcontroller Library |
rgrover1 | 147:f772d9b93808 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 147:f772d9b93808 | 3 | * |
rgrover1 | 147:f772d9b93808 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 147:f772d9b93808 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 147:f772d9b93808 | 6 | * You may obtain a copy of the License at |
rgrover1 | 147:f772d9b93808 | 7 | * |
rgrover1 | 147:f772d9b93808 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 147:f772d9b93808 | 9 | * |
rgrover1 | 147:f772d9b93808 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 147:f772d9b93808 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 147:f772d9b93808 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 147:f772d9b93808 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 147:f772d9b93808 | 14 | * limitations under the License. |
rgrover1 | 147:f772d9b93808 | 15 | */ |
rgrover1 | 147:f772d9b93808 | 16 | |
rgrover1 | 147:f772d9b93808 | 17 | #ifndef __BLE_URI_BEACON_2_SERVICE_H__ |
rgrover1 | 147:f772d9b93808 | 18 | #define __BLE_URI_BEACON_2_SERVICE_H__ |
rgrover1 | 147:f772d9b93808 | 19 | |
rgrover1 | 147:f772d9b93808 | 20 | #include "BLEDevice.h" |
rgrover1 | 147:f772d9b93808 | 21 | |
rgrover1 | 159:e915a0cebcce | 22 | #define UUID_INITIALIZER_LIST(FIRST, SECOND) { \ |
rgrover1 | 159:e915a0cebcce | 23 | 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ |
rgrover1 | 159:e915a0cebcce | 24 | 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \ |
rgrover1 | 159:e915a0cebcce | 25 | } |
rgrover1 | 159:e915a0cebcce | 26 | const uint8_t URIBeacon2ControlServiceUUID[] = UUID_INITIALIZER_LIST(0x20, 0x80); |
rgrover1 | 159:e915a0cebcce | 27 | const uint8_t lockedStateCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x81); |
rgrover1 | 159:e915a0cebcce | 28 | const uint8_t uriDataCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x84); |
rgrover1 | 162:27a19b0fa40e | 29 | const uint8_t flagsCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x85); |
rgrover1 | 163:8bd70d17589f | 30 | const uint8_t txPowerCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x86); |
rgrover1 | 162:27a19b0fa40e | 31 | const uint8_t beaconPeriodCharUUID[] = UUID_INITIALIZER_LIST(0x20, 0x88); |
rgrover1 | 159:e915a0cebcce | 32 | |
rgrover1 | 147:f772d9b93808 | 33 | class URIBeacon2Service { |
rgrover1 | 147:f772d9b93808 | 34 | public: |
rgrover1 | 158:08f609d8a6d4 | 35 | URIBeacon2Service(BLEDevice &ble_, const char *urldata, uint8_t flags_ = 0, uint8_t power_ = 0) : |
rgrover1 | 158:08f609d8a6d4 | 36 | ble(ble_), payloadIndex(0), serviceDataPayload(), |
rgrover1 | 159:e915a0cebcce | 37 | lockedState(false), |
rgrover1 | 158:08f609d8a6d4 | 38 | uriDataLength(0), |
rgrover1 | 158:08f609d8a6d4 | 39 | uriDataValue(), |
rgrover1 | 158:08f609d8a6d4 | 40 | flags(flags_), |
rgrover1 | 159:e915a0cebcce | 41 | power(power_), |
rgrover1 | 162:27a19b0fa40e | 42 | beaconPeriod(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)), /* 1hz */ |
rgrover1 | 159:e915a0cebcce | 43 | lockedStateChar(lockedStateCharUUID, (uint8_t *)&lockedState, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 159:e915a0cebcce | 44 | uriDataChar(uriDataCharUUID, |
rgrover1 | 159:e915a0cebcce | 45 | uriDataValue, |
rgrover1 | 159:e915a0cebcce | 46 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 159:e915a0cebcce | 47 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 162:27a19b0fa40e | 48 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
rgrover1 | 162:27a19b0fa40e | 49 | flagsChar(flagsCharUUID, &flags, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) |
rgrover1 | 163:8bd70d17589f | 50 | // txPowerChar(txPowerCharUUID, &power, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) |
rgrover1 | 158:08f609d8a6d4 | 51 | { |
rgrover1 | 158:08f609d8a6d4 | 52 | if ((urldata == NULL) || ((uriDataLength = strlen(urldata)) == 0)) { |
rgrover1 | 158:08f609d8a6d4 | 53 | return; |
rgrover1 | 158:08f609d8a6d4 | 54 | } |
rgrover1 | 158:08f609d8a6d4 | 55 | strncpy(reinterpret_cast<char *>(uriDataValue), urldata, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 147:f772d9b93808 | 56 | |
rgrover1 | 158:08f609d8a6d4 | 57 | setup(); |
rgrover1 | 159:e915a0cebcce | 58 | |
rgrover1 | 159:e915a0cebcce | 59 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 159:e915a0cebcce | 60 | if (serviceAdded) { |
rgrover1 | 159:e915a0cebcce | 61 | return; |
rgrover1 | 159:e915a0cebcce | 62 | } |
rgrover1 | 159:e915a0cebcce | 63 | |
rgrover1 | 159:e915a0cebcce | 64 | GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar}; |
rgrover1 | 159:e915a0cebcce | 65 | GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 159:e915a0cebcce | 66 | |
rgrover1 | 159:e915a0cebcce | 67 | ble.addService(beaconControlService); |
rgrover1 | 159:e915a0cebcce | 68 | serviceAdded = true; |
rgrover1 | 160:155b28cb0e45 | 69 | |
rgrover1 | 160:155b28cb0e45 | 70 | ble.onDataWritten(this, &URIBeacon2Service::onDataWritten); |
rgrover1 | 147:f772d9b93808 | 71 | } |
rgrover1 | 147:f772d9b93808 | 72 | |
rgrover1 | 160:155b28cb0e45 | 73 | /** |
rgrover1 | 160:155b28cb0e45 | 74 | * This callback allows the DFU service to receive the initial trigger to |
rgrover1 | 160:155b28cb0e45 | 75 | * handover control to the bootloader; but first the application is given a |
rgrover1 | 160:155b28cb0e45 | 76 | * chance to clean up. |
rgrover1 | 160:155b28cb0e45 | 77 | */ |
rgrover1 | 160:155b28cb0e45 | 78 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 160:155b28cb0e45 | 79 | if (params->charHandle == uriDataChar.getValueAttribute().getHandle()) { |
rgrover1 | 164:93e52c3861a9 | 80 | if (lockedState) { /* when locked, the device isn't allowed to update the uriData characteristic */ |
rgrover1 | 164:93e52c3861a9 | 81 | ble.updateCharacteristicValue(uriDataChar.getValueAttribute().getHandle(), uriDataValue, uriDataLength); |
rgrover1 | 164:93e52c3861a9 | 82 | return; |
rgrover1 | 164:93e52c3861a9 | 83 | } |
rgrover1 | 164:93e52c3861a9 | 84 | |
rgrover1 | 160:155b28cb0e45 | 85 | /* we don't handle very large writes at the moment. */ |
rgrover1 | 160:155b28cb0e45 | 86 | if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) { |
rgrover1 | 160:155b28cb0e45 | 87 | return; |
rgrover1 | 160:155b28cb0e45 | 88 | } |
rgrover1 | 160:155b28cb0e45 | 89 | |
rgrover1 | 160:155b28cb0e45 | 90 | uriDataLength = params->len; |
rgrover1 | 160:155b28cb0e45 | 91 | memcpy(uriDataValue, params->data, uriDataLength); |
rgrover1 | 162:27a19b0fa40e | 92 | } else if (params->charHandle == flagsChar.getValueAttribute().getHandle()) { |
rgrover1 | 162:27a19b0fa40e | 93 | if (lockedState) { |
rgrover1 | 162:27a19b0fa40e | 94 | ble.updateCharacteristicValue(flagsChar.getValueAttribute().getHandle(), &flags, 1 /* size */); |
rgrover1 | 162:27a19b0fa40e | 95 | return; |
rgrover1 | 162:27a19b0fa40e | 96 | } else { |
rgrover1 | 162:27a19b0fa40e | 97 | flags = *(params->data); |
rgrover1 | 162:27a19b0fa40e | 98 | } |
rgrover1 | 160:155b28cb0e45 | 99 | } |
rgrover1 | 162:27a19b0fa40e | 100 | setup(); |
rgrover1 | 162:27a19b0fa40e | 101 | ble.setAdvertisingPayload(); |
rgrover1 | 160:155b28cb0e45 | 102 | } |
rgrover1 | 160:155b28cb0e45 | 103 | |
rgrover1 | 160:155b28cb0e45 | 104 | /** |
rgrover1 | 161:f17b72db118e | 105 | * For debugging only. |
rgrover1 | 160:155b28cb0e45 | 106 | */ |
rgrover1 | 151:cb5df770f05b | 107 | void dumpEncodedSeviceData() const { |
rgrover1 | 150:aa2d70369df0 | 108 | printf("encoded: '"); |
rgrover1 | 150:aa2d70369df0 | 109 | for (unsigned i = 0; i < payloadIndex; i++) { |
rgrover1 | 151:cb5df770f05b | 110 | printf(" %02x", serviceDataPayload[i]); |
rgrover1 | 150:aa2d70369df0 | 111 | } |
rgrover1 | 150:aa2d70369df0 | 112 | printf("'\r\n"); |
rgrover1 | 150:aa2d70369df0 | 113 | } |
rgrover1 | 150:aa2d70369df0 | 114 | |
rgrover1 | 147:f772d9b93808 | 115 | private: |
rgrover1 | 158:08f609d8a6d4 | 116 | void setup(void) { |
rgrover1 | 160:155b28cb0e45 | 117 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 148:0072d73e966f | 118 | |
rgrover1 | 160:155b28cb0e45 | 119 | payloadIndex = 0; |
rgrover1 | 158:08f609d8a6d4 | 120 | serviceDataPayload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 158:08f609d8a6d4 | 121 | serviceDataPayload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 158:08f609d8a6d4 | 122 | serviceDataPayload[payloadIndex++] = flags; |
rgrover1 | 158:08f609d8a6d4 | 123 | serviceDataPayload[payloadIndex++] = power; |
rgrover1 | 158:08f609d8a6d4 | 124 | |
rgrover1 | 158:08f609d8a6d4 | 125 | const char *urlData = reinterpret_cast<char *>(uriDataValue); |
rgrover1 | 158:08f609d8a6d4 | 126 | size_t sizeofURLData = uriDataLength; |
rgrover1 | 158:08f609d8a6d4 | 127 | size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData); |
rgrover1 | 158:08f609d8a6d4 | 128 | |
rgrover1 | 160:155b28cb0e45 | 129 | ble.clearAdvertisingPayload(); |
rgrover1 | 158:08f609d8a6d4 | 130 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 158:08f609d8a6d4 | 131 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4); |
rgrover1 | 160:155b28cb0e45 | 132 | |
rgrover1 | 162:27a19b0fa40e | 133 | ble.setAdvertisingInterval(beaconPeriod); |
rgrover1 | 160:155b28cb0e45 | 134 | ble.setTxPower(power); |
rgrover1 | 149:6a7666d72a83 | 135 | } |
rgrover1 | 149:6a7666d72a83 | 136 | |
rgrover1 | 151:cb5df770f05b | 137 | size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) { |
rgrover1 | 148:0072d73e966f | 138 | const char *prefixes[] = { |
rgrover1 | 148:0072d73e966f | 139 | "http://www.", |
rgrover1 | 148:0072d73e966f | 140 | "https://www.", |
rgrover1 | 148:0072d73e966f | 141 | "http://", |
rgrover1 | 148:0072d73e966f | 142 | "https://", |
rgrover1 | 148:0072d73e966f | 143 | "urn:uuid:" |
rgrover1 | 148:0072d73e966f | 144 | }; |
rgrover1 | 149:6a7666d72a83 | 145 | |
rgrover1 | 148:0072d73e966f | 146 | size_t encodedBytes = 0; |
rgrover1 | 148:0072d73e966f | 147 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 148:0072d73e966f | 148 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 148:0072d73e966f | 149 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 148:0072d73e966f | 150 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 151:cb5df770f05b | 151 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 149:6a7666d72a83 | 152 | encodedBytes = 1; |
rgrover1 | 148:0072d73e966f | 153 | |
rgrover1 | 148:0072d73e966f | 154 | urldata += prefixLen; |
rgrover1 | 148:0072d73e966f | 155 | sizeofURLData -= prefixLen; |
rgrover1 | 148:0072d73e966f | 156 | break; |
rgrover1 | 148:0072d73e966f | 157 | } |
rgrover1 | 148:0072d73e966f | 158 | } |
rgrover1 | 148:0072d73e966f | 159 | |
rgrover1 | 148:0072d73e966f | 160 | return encodedBytes; |
rgrover1 | 147:f772d9b93808 | 161 | } |
rgrover1 | 147:f772d9b93808 | 162 | |
rgrover1 | 151:cb5df770f05b | 163 | size_t encodeURI(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 150:aa2d70369df0 | 164 | const char *suffixes[] = { |
rgrover1 | 150:aa2d70369df0 | 165 | ".com/", |
rgrover1 | 150:aa2d70369df0 | 166 | ".org/", |
rgrover1 | 150:aa2d70369df0 | 167 | ".edu/", |
rgrover1 | 150:aa2d70369df0 | 168 | ".net/", |
rgrover1 | 150:aa2d70369df0 | 169 | ".info/", |
rgrover1 | 150:aa2d70369df0 | 170 | ".biz/", |
rgrover1 | 150:aa2d70369df0 | 171 | ".gov/", |
rgrover1 | 150:aa2d70369df0 | 172 | ".com", |
rgrover1 | 150:aa2d70369df0 | 173 | ".org", |
rgrover1 | 150:aa2d70369df0 | 174 | ".edu", |
rgrover1 | 150:aa2d70369df0 | 175 | ".net", |
rgrover1 | 150:aa2d70369df0 | 176 | ".info", |
rgrover1 | 150:aa2d70369df0 | 177 | ".biz", |
rgrover1 | 150:aa2d70369df0 | 178 | ".gov" |
rgrover1 | 150:aa2d70369df0 | 179 | }; |
rgrover1 | 150:aa2d70369df0 | 180 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 150:aa2d70369df0 | 181 | |
rgrover1 | 150:aa2d70369df0 | 182 | size_t encodedBytes = 0; |
rgrover1 | 155:a3fafce0bc0b | 183 | while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) { |
rgrover1 | 150:aa2d70369df0 | 184 | /* check for suffix match */ |
rgrover1 | 150:aa2d70369df0 | 185 | unsigned i; |
rgrover1 | 150:aa2d70369df0 | 186 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 150:aa2d70369df0 | 187 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 150:aa2d70369df0 | 188 | if ((suffixLen == 0) || (sizeofURLData < suffixLen)) { |
rgrover1 | 150:aa2d70369df0 | 189 | continue; |
rgrover1 | 150:aa2d70369df0 | 190 | } |
rgrover1 | 150:aa2d70369df0 | 191 | |
rgrover1 | 150:aa2d70369df0 | 192 | if (strncmp(urldata, suffixes[i], suffixLen) == 0) { |
rgrover1 | 151:cb5df770f05b | 193 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 150:aa2d70369df0 | 194 | ++encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 195 | urldata += suffixLen; |
rgrover1 | 150:aa2d70369df0 | 196 | sizeofURLData -= suffixLen; |
rgrover1 | 150:aa2d70369df0 | 197 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 150:aa2d70369df0 | 198 | } |
rgrover1 | 150:aa2d70369df0 | 199 | } |
rgrover1 | 150:aa2d70369df0 | 200 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 150:aa2d70369df0 | 201 | if (i == NUM_SUFFIXES) { |
rgrover1 | 151:cb5df770f05b | 202 | serviceDataPayload[payloadIndex++] = *urldata; |
rgrover1 | 150:aa2d70369df0 | 203 | ++encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 204 | ++urldata; |
rgrover1 | 150:aa2d70369df0 | 205 | --sizeofURLData; |
rgrover1 | 150:aa2d70369df0 | 206 | } |
rgrover1 | 150:aa2d70369df0 | 207 | } |
rgrover1 | 150:aa2d70369df0 | 208 | |
rgrover1 | 150:aa2d70369df0 | 209 | return encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 210 | } |
rgrover1 | 150:aa2d70369df0 | 211 | |
rgrover1 | 147:f772d9b93808 | 212 | private: |
rgrover1 | 157:8bd620881bc9 | 213 | static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 27; |
rgrover1 | 158:08f609d8a6d4 | 214 | static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; |
rgrover1 | 147:f772d9b93808 | 215 | |
rgrover1 | 147:f772d9b93808 | 216 | private: |
rgrover1 | 147:f772d9b93808 | 217 | BLEDevice &ble; |
rgrover1 | 148:0072d73e966f | 218 | |
rgrover1 | 158:08f609d8a6d4 | 219 | size_t payloadIndex; |
rgrover1 | 158:08f609d8a6d4 | 220 | uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD]; |
rgrover1 | 161:f17b72db118e | 221 | bool lockedState; |
rgrover1 | 158:08f609d8a6d4 | 222 | uint16_t uriDataLength; |
rgrover1 | 158:08f609d8a6d4 | 223 | uint8_t uriDataValue[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 158:08f609d8a6d4 | 224 | uint8_t flags; |
rgrover1 | 158:08f609d8a6d4 | 225 | uint8_t power; |
rgrover1 | 162:27a19b0fa40e | 226 | uint16_t beaconPeriod; |
rgrover1 | 159:e915a0cebcce | 227 | |
rgrover1 | 159:e915a0cebcce | 228 | GattCharacteristic lockedStateChar; |
rgrover1 | 159:e915a0cebcce | 229 | GattCharacteristic uriDataChar; |
rgrover1 | 162:27a19b0fa40e | 230 | GattCharacteristic flagsChar; |
rgrover1 | 163:8bd70d17589f | 231 | // GattCharacteristic txPowerChar; |
rgrover1 | 147:f772d9b93808 | 232 | }; |
rgrover1 | 147:f772d9b93808 | 233 | |
rgrover1 | 147:f772d9b93808 | 234 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |