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@161:f17b72db118e, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:21 2014 +0000
- Revision:
- 161:f17b72db118e
- Parent:
- 160:155b28cb0e45
- Child:
- 162:27a19b0fa40e
Synchronized with git rev d1bc90fa
Author: Rohit Grover
trivial cosmetic change.
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 | 159:e915a0cebcce | 29 | |
rgrover1 | 147:f772d9b93808 | 30 | class URIBeacon2Service { |
rgrover1 | 147:f772d9b93808 | 31 | public: |
rgrover1 | 158:08f609d8a6d4 | 32 | URIBeacon2Service(BLEDevice &ble_, const char *urldata, uint8_t flags_ = 0, uint8_t power_ = 0) : |
rgrover1 | 158:08f609d8a6d4 | 33 | ble(ble_), payloadIndex(0), serviceDataPayload(), |
rgrover1 | 159:e915a0cebcce | 34 | lockedState(false), |
rgrover1 | 158:08f609d8a6d4 | 35 | uriDataLength(0), |
rgrover1 | 158:08f609d8a6d4 | 36 | uriDataValue(), |
rgrover1 | 158:08f609d8a6d4 | 37 | flags(flags_), |
rgrover1 | 159:e915a0cebcce | 38 | power(power_), |
rgrover1 | 159:e915a0cebcce | 39 | lockedStateChar(lockedStateCharUUID, (uint8_t *)&lockedState, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 159:e915a0cebcce | 40 | uriDataChar(uriDataCharUUID, |
rgrover1 | 159:e915a0cebcce | 41 | uriDataValue, |
rgrover1 | 159:e915a0cebcce | 42 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 159:e915a0cebcce | 43 | MAX_SIZE_URI_DATA_CHAR_VALUE, |
rgrover1 | 159:e915a0cebcce | 44 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) |
rgrover1 | 158:08f609d8a6d4 | 45 | { |
rgrover1 | 158:08f609d8a6d4 | 46 | if ((urldata == NULL) || ((uriDataLength = strlen(urldata)) == 0)) { |
rgrover1 | 158:08f609d8a6d4 | 47 | return; |
rgrover1 | 158:08f609d8a6d4 | 48 | } |
rgrover1 | 158:08f609d8a6d4 | 49 | strncpy(reinterpret_cast<char *>(uriDataValue), urldata, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 147:f772d9b93808 | 50 | |
rgrover1 | 158:08f609d8a6d4 | 51 | setup(); |
rgrover1 | 159:e915a0cebcce | 52 | |
rgrover1 | 159:e915a0cebcce | 53 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 159:e915a0cebcce | 54 | if (serviceAdded) { |
rgrover1 | 159:e915a0cebcce | 55 | return; |
rgrover1 | 159:e915a0cebcce | 56 | } |
rgrover1 | 159:e915a0cebcce | 57 | |
rgrover1 | 159:e915a0cebcce | 58 | GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar}; |
rgrover1 | 159:e915a0cebcce | 59 | GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 159:e915a0cebcce | 60 | |
rgrover1 | 159:e915a0cebcce | 61 | ble.addService(beaconControlService); |
rgrover1 | 159:e915a0cebcce | 62 | serviceAdded = true; |
rgrover1 | 160:155b28cb0e45 | 63 | |
rgrover1 | 160:155b28cb0e45 | 64 | ble.onDataWritten(this, &URIBeacon2Service::onDataWritten); |
rgrover1 | 147:f772d9b93808 | 65 | } |
rgrover1 | 147:f772d9b93808 | 66 | |
rgrover1 | 160:155b28cb0e45 | 67 | /** |
rgrover1 | 160:155b28cb0e45 | 68 | * This callback allows the DFU service to receive the initial trigger to |
rgrover1 | 160:155b28cb0e45 | 69 | * handover control to the bootloader; but first the application is given a |
rgrover1 | 160:155b28cb0e45 | 70 | * chance to clean up. |
rgrover1 | 160:155b28cb0e45 | 71 | */ |
rgrover1 | 160:155b28cb0e45 | 72 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 160:155b28cb0e45 | 73 | if (params->charHandle == uriDataChar.getValueAttribute().getHandle()) { |
rgrover1 | 160:155b28cb0e45 | 74 | /* we don't handle very large writes at the moment. */ |
rgrover1 | 160:155b28cb0e45 | 75 | if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) { |
rgrover1 | 160:155b28cb0e45 | 76 | return; |
rgrover1 | 160:155b28cb0e45 | 77 | } |
rgrover1 | 160:155b28cb0e45 | 78 | |
rgrover1 | 160:155b28cb0e45 | 79 | uriDataLength = params->len; |
rgrover1 | 160:155b28cb0e45 | 80 | memcpy(uriDataValue, params->data, uriDataLength); |
rgrover1 | 160:155b28cb0e45 | 81 | setup(); |
rgrover1 | 160:155b28cb0e45 | 82 | ble.setAdvertisingPayload(); |
rgrover1 | 160:155b28cb0e45 | 83 | } |
rgrover1 | 160:155b28cb0e45 | 84 | } |
rgrover1 | 160:155b28cb0e45 | 85 | |
rgrover1 | 160:155b28cb0e45 | 86 | /** |
rgrover1 | 161:f17b72db118e | 87 | * For debugging only. |
rgrover1 | 160:155b28cb0e45 | 88 | */ |
rgrover1 | 151:cb5df770f05b | 89 | void dumpEncodedSeviceData() const { |
rgrover1 | 150:aa2d70369df0 | 90 | printf("encoded: '"); |
rgrover1 | 150:aa2d70369df0 | 91 | for (unsigned i = 0; i < payloadIndex; i++) { |
rgrover1 | 151:cb5df770f05b | 92 | printf(" %02x", serviceDataPayload[i]); |
rgrover1 | 150:aa2d70369df0 | 93 | } |
rgrover1 | 150:aa2d70369df0 | 94 | printf("'\r\n"); |
rgrover1 | 150:aa2d70369df0 | 95 | } |
rgrover1 | 150:aa2d70369df0 | 96 | |
rgrover1 | 147:f772d9b93808 | 97 | private: |
rgrover1 | 158:08f609d8a6d4 | 98 | void setup(void) { |
rgrover1 | 160:155b28cb0e45 | 99 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 148:0072d73e966f | 100 | |
rgrover1 | 160:155b28cb0e45 | 101 | payloadIndex = 0; |
rgrover1 | 158:08f609d8a6d4 | 102 | serviceDataPayload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 158:08f609d8a6d4 | 103 | serviceDataPayload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 158:08f609d8a6d4 | 104 | serviceDataPayload[payloadIndex++] = flags; |
rgrover1 | 158:08f609d8a6d4 | 105 | serviceDataPayload[payloadIndex++] = power; |
rgrover1 | 158:08f609d8a6d4 | 106 | |
rgrover1 | 158:08f609d8a6d4 | 107 | const char *urlData = reinterpret_cast<char *>(uriDataValue); |
rgrover1 | 158:08f609d8a6d4 | 108 | size_t sizeofURLData = uriDataLength; |
rgrover1 | 158:08f609d8a6d4 | 109 | size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData); |
rgrover1 | 158:08f609d8a6d4 | 110 | |
rgrover1 | 160:155b28cb0e45 | 111 | ble.clearAdvertisingPayload(); |
rgrover1 | 158:08f609d8a6d4 | 112 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 158:08f609d8a6d4 | 113 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4); |
rgrover1 | 160:155b28cb0e45 | 114 | |
rgrover1 | 160:155b28cb0e45 | 115 | ble.setTxPower(power); |
rgrover1 | 149:6a7666d72a83 | 116 | } |
rgrover1 | 149:6a7666d72a83 | 117 | |
rgrover1 | 151:cb5df770f05b | 118 | size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) { |
rgrover1 | 148:0072d73e966f | 119 | const char *prefixes[] = { |
rgrover1 | 148:0072d73e966f | 120 | "http://www.", |
rgrover1 | 148:0072d73e966f | 121 | "https://www.", |
rgrover1 | 148:0072d73e966f | 122 | "http://", |
rgrover1 | 148:0072d73e966f | 123 | "https://", |
rgrover1 | 148:0072d73e966f | 124 | "urn:uuid:" |
rgrover1 | 148:0072d73e966f | 125 | }; |
rgrover1 | 149:6a7666d72a83 | 126 | |
rgrover1 | 148:0072d73e966f | 127 | size_t encodedBytes = 0; |
rgrover1 | 148:0072d73e966f | 128 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 148:0072d73e966f | 129 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 148:0072d73e966f | 130 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 148:0072d73e966f | 131 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 151:cb5df770f05b | 132 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 149:6a7666d72a83 | 133 | encodedBytes = 1; |
rgrover1 | 148:0072d73e966f | 134 | |
rgrover1 | 148:0072d73e966f | 135 | urldata += prefixLen; |
rgrover1 | 148:0072d73e966f | 136 | sizeofURLData -= prefixLen; |
rgrover1 | 148:0072d73e966f | 137 | break; |
rgrover1 | 148:0072d73e966f | 138 | } |
rgrover1 | 148:0072d73e966f | 139 | } |
rgrover1 | 148:0072d73e966f | 140 | |
rgrover1 | 148:0072d73e966f | 141 | return encodedBytes; |
rgrover1 | 147:f772d9b93808 | 142 | } |
rgrover1 | 147:f772d9b93808 | 143 | |
rgrover1 | 151:cb5df770f05b | 144 | size_t encodeURI(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 150:aa2d70369df0 | 145 | const char *suffixes[] = { |
rgrover1 | 150:aa2d70369df0 | 146 | ".com/", |
rgrover1 | 150:aa2d70369df0 | 147 | ".org/", |
rgrover1 | 150:aa2d70369df0 | 148 | ".edu/", |
rgrover1 | 150:aa2d70369df0 | 149 | ".net/", |
rgrover1 | 150:aa2d70369df0 | 150 | ".info/", |
rgrover1 | 150:aa2d70369df0 | 151 | ".biz/", |
rgrover1 | 150:aa2d70369df0 | 152 | ".gov/", |
rgrover1 | 150:aa2d70369df0 | 153 | ".com", |
rgrover1 | 150:aa2d70369df0 | 154 | ".org", |
rgrover1 | 150:aa2d70369df0 | 155 | ".edu", |
rgrover1 | 150:aa2d70369df0 | 156 | ".net", |
rgrover1 | 150:aa2d70369df0 | 157 | ".info", |
rgrover1 | 150:aa2d70369df0 | 158 | ".biz", |
rgrover1 | 150:aa2d70369df0 | 159 | ".gov" |
rgrover1 | 150:aa2d70369df0 | 160 | }; |
rgrover1 | 150:aa2d70369df0 | 161 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 150:aa2d70369df0 | 162 | |
rgrover1 | 150:aa2d70369df0 | 163 | size_t encodedBytes = 0; |
rgrover1 | 155:a3fafce0bc0b | 164 | while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) { |
rgrover1 | 150:aa2d70369df0 | 165 | /* check for suffix match */ |
rgrover1 | 150:aa2d70369df0 | 166 | unsigned i; |
rgrover1 | 150:aa2d70369df0 | 167 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 150:aa2d70369df0 | 168 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 150:aa2d70369df0 | 169 | if ((suffixLen == 0) || (sizeofURLData < suffixLen)) { |
rgrover1 | 150:aa2d70369df0 | 170 | continue; |
rgrover1 | 150:aa2d70369df0 | 171 | } |
rgrover1 | 150:aa2d70369df0 | 172 | |
rgrover1 | 150:aa2d70369df0 | 173 | if (strncmp(urldata, suffixes[i], suffixLen) == 0) { |
rgrover1 | 151:cb5df770f05b | 174 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 150:aa2d70369df0 | 175 | ++encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 176 | urldata += suffixLen; |
rgrover1 | 150:aa2d70369df0 | 177 | sizeofURLData -= suffixLen; |
rgrover1 | 150:aa2d70369df0 | 178 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 150:aa2d70369df0 | 179 | } |
rgrover1 | 150:aa2d70369df0 | 180 | } |
rgrover1 | 150:aa2d70369df0 | 181 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 150:aa2d70369df0 | 182 | if (i == NUM_SUFFIXES) { |
rgrover1 | 151:cb5df770f05b | 183 | serviceDataPayload[payloadIndex++] = *urldata; |
rgrover1 | 150:aa2d70369df0 | 184 | ++encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 185 | ++urldata; |
rgrover1 | 150:aa2d70369df0 | 186 | --sizeofURLData; |
rgrover1 | 150:aa2d70369df0 | 187 | } |
rgrover1 | 150:aa2d70369df0 | 188 | } |
rgrover1 | 150:aa2d70369df0 | 189 | |
rgrover1 | 150:aa2d70369df0 | 190 | return encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 191 | } |
rgrover1 | 150:aa2d70369df0 | 192 | |
rgrover1 | 147:f772d9b93808 | 193 | private: |
rgrover1 | 157:8bd620881bc9 | 194 | static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 27; |
rgrover1 | 158:08f609d8a6d4 | 195 | static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; |
rgrover1 | 147:f772d9b93808 | 196 | |
rgrover1 | 147:f772d9b93808 | 197 | private: |
rgrover1 | 147:f772d9b93808 | 198 | BLEDevice &ble; |
rgrover1 | 148:0072d73e966f | 199 | |
rgrover1 | 158:08f609d8a6d4 | 200 | size_t payloadIndex; |
rgrover1 | 158:08f609d8a6d4 | 201 | uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD]; |
rgrover1 | 161:f17b72db118e | 202 | bool lockedState; |
rgrover1 | 158:08f609d8a6d4 | 203 | uint16_t uriDataLength; |
rgrover1 | 158:08f609d8a6d4 | 204 | uint8_t uriDataValue[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 158:08f609d8a6d4 | 205 | uint8_t flags; |
rgrover1 | 158:08f609d8a6d4 | 206 | uint8_t power; |
rgrover1 | 159:e915a0cebcce | 207 | |
rgrover1 | 159:e915a0cebcce | 208 | GattCharacteristic lockedStateChar; |
rgrover1 | 159:e915a0cebcce | 209 | GattCharacteristic uriDataChar; |
rgrover1 | 147:f772d9b93808 | 210 | }; |
rgrover1 | 147:f772d9b93808 | 211 | |
rgrover1 | 147:f772d9b93808 | 212 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |