I don't know why this is happening.

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 28 14:11:22 2014 +0000
Revision:
167:8057dc59ac08
Parent:
166:a03959283d48
Synchronized with git rev 178df43c
Author: Rohit Grover
add beaconPeriodCharacteristic

Who changed what in which revision?

UserRevisionLine numberNew 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 167:8057dc59ac08 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 167:8057dc59ac08 51 beaconPeriodChar(beaconPeriodCharUUID, (uint8_t *)&beaconPeriod, 2, 2,
rgrover1 167:8057dc59ac08 52 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)
rgrover1 158:08f609d8a6d4 53 {
rgrover1 158:08f609d8a6d4 54 if ((urldata == NULL) || ((uriDataLength = strlen(urldata)) == 0)) {
rgrover1 158:08f609d8a6d4 55 return;
rgrover1 158:08f609d8a6d4 56 }
rgrover1 158:08f609d8a6d4 57 strncpy(reinterpret_cast<char *>(uriDataValue), urldata, MAX_SIZE_URI_DATA_CHAR_VALUE);
rgrover1 147:f772d9b93808 58
rgrover1 158:08f609d8a6d4 59 setup();
rgrover1 159:e915a0cebcce 60
rgrover1 159:e915a0cebcce 61 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
rgrover1 159:e915a0cebcce 62 if (serviceAdded) {
rgrover1 159:e915a0cebcce 63 return;
rgrover1 159:e915a0cebcce 64 }
rgrover1 159:e915a0cebcce 65
rgrover1 167:8057dc59ac08 66 GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar, &flagsChar, &beaconPeriodChar};
rgrover1 159:e915a0cebcce 67 GattService beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 159:e915a0cebcce 68
rgrover1 159:e915a0cebcce 69 ble.addService(beaconControlService);
rgrover1 159:e915a0cebcce 70 serviceAdded = true;
rgrover1 160:155b28cb0e45 71
rgrover1 160:155b28cb0e45 72 ble.onDataWritten(this, &URIBeacon2Service::onDataWritten);
rgrover1 147:f772d9b93808 73 }
rgrover1 147:f772d9b93808 74
rgrover1 160:155b28cb0e45 75 /**
rgrover1 160:155b28cb0e45 76 * This callback allows the DFU service to receive the initial trigger to
rgrover1 160:155b28cb0e45 77 * handover control to the bootloader; but first the application is given a
rgrover1 160:155b28cb0e45 78 * chance to clean up.
rgrover1 160:155b28cb0e45 79 */
rgrover1 160:155b28cb0e45 80 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
rgrover1 160:155b28cb0e45 81 if (params->charHandle == uriDataChar.getValueAttribute().getHandle()) {
rgrover1 166:a03959283d48 82 if (lockedState) { /* When locked, the device isn't allowed to update the uriData characteristic. */
rgrover1 166:a03959283d48 83 /* Restore GATT database with previous value. */
rgrover1 164:93e52c3861a9 84 ble.updateCharacteristicValue(uriDataChar.getValueAttribute().getHandle(), uriDataValue, uriDataLength);
rgrover1 164:93e52c3861a9 85 return;
rgrover1 164:93e52c3861a9 86 }
rgrover1 164:93e52c3861a9 87
rgrover1 165:ca406c1d0399 88 /* We don't handle very large writes at the moment. */
rgrover1 160:155b28cb0e45 89 if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
rgrover1 160:155b28cb0e45 90 return;
rgrover1 160:155b28cb0e45 91 }
rgrover1 160:155b28cb0e45 92
rgrover1 160:155b28cb0e45 93 uriDataLength = params->len;
rgrover1 160:155b28cb0e45 94 memcpy(uriDataValue, params->data, uriDataLength);
rgrover1 162:27a19b0fa40e 95 } else if (params->charHandle == flagsChar.getValueAttribute().getHandle()) {
rgrover1 166:a03959283d48 96 if (lockedState) { /* When locked, the device isn't allowed to update the flags characteristic. */
rgrover1 166:a03959283d48 97 /* Restore GATT database with previous value. */
rgrover1 162:27a19b0fa40e 98 ble.updateCharacteristicValue(flagsChar.getValueAttribute().getHandle(), &flags, 1 /* size */);
rgrover1 162:27a19b0fa40e 99 return;
rgrover1 162:27a19b0fa40e 100 } else {
rgrover1 162:27a19b0fa40e 101 flags = *(params->data);
rgrover1 162:27a19b0fa40e 102 }
rgrover1 167:8057dc59ac08 103 } else if (params->charHandle == beaconPeriodChar.getValueAttribute().getHandle()) {
rgrover1 167:8057dc59ac08 104 if (lockedState) { /* When locked, the device isn't allowed to update the flags characteristic. */
rgrover1 167:8057dc59ac08 105 /* Restore GATT database with previous value. */
rgrover1 167:8057dc59ac08 106 ble.updateCharacteristicValue(beaconPeriodChar.getValueAttribute().getHandle(), (uint8_t *)&beaconPeriod, 2 /* size */);
rgrover1 167:8057dc59ac08 107 return;
rgrover1 167:8057dc59ac08 108 } else {
rgrover1 167:8057dc59ac08 109 beaconPeriod = *((uint16_t *)(params->data));
rgrover1 167:8057dc59ac08 110 }
rgrover1 160:155b28cb0e45 111 }
rgrover1 162:27a19b0fa40e 112 setup();
rgrover1 162:27a19b0fa40e 113 ble.setAdvertisingPayload();
rgrover1 160:155b28cb0e45 114 }
rgrover1 160:155b28cb0e45 115
rgrover1 160:155b28cb0e45 116 /**
rgrover1 161:f17b72db118e 117 * For debugging only.
rgrover1 160:155b28cb0e45 118 */
rgrover1 151:cb5df770f05b 119 void dumpEncodedSeviceData() const {
rgrover1 150:aa2d70369df0 120 printf("encoded: '");
rgrover1 150:aa2d70369df0 121 for (unsigned i = 0; i < payloadIndex; i++) {
rgrover1 151:cb5df770f05b 122 printf(" %02x", serviceDataPayload[i]);
rgrover1 150:aa2d70369df0 123 }
rgrover1 150:aa2d70369df0 124 printf("'\r\n");
rgrover1 150:aa2d70369df0 125 }
rgrover1 150:aa2d70369df0 126
rgrover1 147:f772d9b93808 127 private:
rgrover1 158:08f609d8a6d4 128 void setup(void) {
rgrover1 160:155b28cb0e45 129 const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
rgrover1 148:0072d73e966f 130
rgrover1 160:155b28cb0e45 131 payloadIndex = 0;
rgrover1 158:08f609d8a6d4 132 serviceDataPayload[payloadIndex++] = BEACON_UUID[0];
rgrover1 158:08f609d8a6d4 133 serviceDataPayload[payloadIndex++] = BEACON_UUID[1];
rgrover1 158:08f609d8a6d4 134 serviceDataPayload[payloadIndex++] = flags;
rgrover1 158:08f609d8a6d4 135 serviceDataPayload[payloadIndex++] = power;
rgrover1 158:08f609d8a6d4 136
rgrover1 158:08f609d8a6d4 137 const char *urlData = reinterpret_cast<char *>(uriDataValue);
rgrover1 158:08f609d8a6d4 138 size_t sizeofURLData = uriDataLength;
rgrover1 158:08f609d8a6d4 139 size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData);
rgrover1 158:08f609d8a6d4 140
rgrover1 160:155b28cb0e45 141 ble.clearAdvertisingPayload();
rgrover1 158:08f609d8a6d4 142 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
rgrover1 158:08f609d8a6d4 143 ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4);
rgrover1 160:155b28cb0e45 144
rgrover1 162:27a19b0fa40e 145 ble.setAdvertisingInterval(beaconPeriod);
rgrover1 160:155b28cb0e45 146 ble.setTxPower(power);
rgrover1 149:6a7666d72a83 147 }
rgrover1 149:6a7666d72a83 148
rgrover1 151:cb5df770f05b 149 size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) {
rgrover1 148:0072d73e966f 150 const char *prefixes[] = {
rgrover1 148:0072d73e966f 151 "http://www.",
rgrover1 148:0072d73e966f 152 "https://www.",
rgrover1 148:0072d73e966f 153 "http://",
rgrover1 148:0072d73e966f 154 "https://",
rgrover1 148:0072d73e966f 155 "urn:uuid:"
rgrover1 148:0072d73e966f 156 };
rgrover1 149:6a7666d72a83 157
rgrover1 148:0072d73e966f 158 size_t encodedBytes = 0;
rgrover1 148:0072d73e966f 159 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
rgrover1 148:0072d73e966f 160 for (unsigned i = 0; i < NUM_PREFIXES; i++) {
rgrover1 148:0072d73e966f 161 size_t prefixLen = strlen(prefixes[i]);
rgrover1 148:0072d73e966f 162 if (strncmp(urldata, prefixes[i], prefixLen) == 0) {
rgrover1 151:cb5df770f05b 163 serviceDataPayload[payloadIndex++] = i;
rgrover1 149:6a7666d72a83 164 encodedBytes = 1;
rgrover1 148:0072d73e966f 165
rgrover1 148:0072d73e966f 166 urldata += prefixLen;
rgrover1 148:0072d73e966f 167 sizeofURLData -= prefixLen;
rgrover1 148:0072d73e966f 168 break;
rgrover1 148:0072d73e966f 169 }
rgrover1 148:0072d73e966f 170 }
rgrover1 148:0072d73e966f 171
rgrover1 148:0072d73e966f 172 return encodedBytes;
rgrover1 147:f772d9b93808 173 }
rgrover1 147:f772d9b93808 174
rgrover1 151:cb5df770f05b 175 size_t encodeURI(const char *urldata, size_t sizeofURLData) {
rgrover1 150:aa2d70369df0 176 const char *suffixes[] = {
rgrover1 150:aa2d70369df0 177 ".com/",
rgrover1 150:aa2d70369df0 178 ".org/",
rgrover1 150:aa2d70369df0 179 ".edu/",
rgrover1 150:aa2d70369df0 180 ".net/",
rgrover1 150:aa2d70369df0 181 ".info/",
rgrover1 150:aa2d70369df0 182 ".biz/",
rgrover1 150:aa2d70369df0 183 ".gov/",
rgrover1 150:aa2d70369df0 184 ".com",
rgrover1 150:aa2d70369df0 185 ".org",
rgrover1 150:aa2d70369df0 186 ".edu",
rgrover1 150:aa2d70369df0 187 ".net",
rgrover1 150:aa2d70369df0 188 ".info",
rgrover1 150:aa2d70369df0 189 ".biz",
rgrover1 150:aa2d70369df0 190 ".gov"
rgrover1 150:aa2d70369df0 191 };
rgrover1 150:aa2d70369df0 192 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
rgrover1 150:aa2d70369df0 193
rgrover1 150:aa2d70369df0 194 size_t encodedBytes = 0;
rgrover1 155:a3fafce0bc0b 195 while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) {
rgrover1 150:aa2d70369df0 196 /* check for suffix match */
rgrover1 150:aa2d70369df0 197 unsigned i;
rgrover1 150:aa2d70369df0 198 for (i = 0; i < NUM_SUFFIXES; i++) {
rgrover1 150:aa2d70369df0 199 size_t suffixLen = strlen(suffixes[i]);
rgrover1 150:aa2d70369df0 200 if ((suffixLen == 0) || (sizeofURLData < suffixLen)) {
rgrover1 150:aa2d70369df0 201 continue;
rgrover1 150:aa2d70369df0 202 }
rgrover1 150:aa2d70369df0 203
rgrover1 150:aa2d70369df0 204 if (strncmp(urldata, suffixes[i], suffixLen) == 0) {
rgrover1 151:cb5df770f05b 205 serviceDataPayload[payloadIndex++] = i;
rgrover1 150:aa2d70369df0 206 ++encodedBytes;
rgrover1 150:aa2d70369df0 207 urldata += suffixLen;
rgrover1 150:aa2d70369df0 208 sizeofURLData -= suffixLen;
rgrover1 150:aa2d70369df0 209 break; /* from the for loop for checking against suffixes */
rgrover1 150:aa2d70369df0 210 }
rgrover1 150:aa2d70369df0 211 }
rgrover1 150:aa2d70369df0 212 /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
rgrover1 150:aa2d70369df0 213 if (i == NUM_SUFFIXES) {
rgrover1 151:cb5df770f05b 214 serviceDataPayload[payloadIndex++] = *urldata;
rgrover1 150:aa2d70369df0 215 ++encodedBytes;
rgrover1 150:aa2d70369df0 216 ++urldata;
rgrover1 150:aa2d70369df0 217 --sizeofURLData;
rgrover1 150:aa2d70369df0 218 }
rgrover1 150:aa2d70369df0 219 }
rgrover1 150:aa2d70369df0 220
rgrover1 150:aa2d70369df0 221 return encodedBytes;
rgrover1 150:aa2d70369df0 222 }
rgrover1 150:aa2d70369df0 223
rgrover1 147:f772d9b93808 224 private:
rgrover1 157:8bd620881bc9 225 static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 27;
rgrover1 158:08f609d8a6d4 226 static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48;
rgrover1 147:f772d9b93808 227
rgrover1 147:f772d9b93808 228 private:
rgrover1 147:f772d9b93808 229 BLEDevice &ble;
rgrover1 148:0072d73e966f 230
rgrover1 158:08f609d8a6d4 231 size_t payloadIndex;
rgrover1 158:08f609d8a6d4 232 uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD];
rgrover1 161:f17b72db118e 233 bool lockedState;
rgrover1 158:08f609d8a6d4 234 uint16_t uriDataLength;
rgrover1 158:08f609d8a6d4 235 uint8_t uriDataValue[MAX_SIZE_URI_DATA_CHAR_VALUE];
rgrover1 158:08f609d8a6d4 236 uint8_t flags;
rgrover1 158:08f609d8a6d4 237 uint8_t power;
rgrover1 162:27a19b0fa40e 238 uint16_t beaconPeriod;
rgrover1 159:e915a0cebcce 239
rgrover1 159:e915a0cebcce 240 GattCharacteristic lockedStateChar;
rgrover1 159:e915a0cebcce 241 GattCharacteristic uriDataChar;
rgrover1 162:27a19b0fa40e 242 GattCharacteristic flagsChar;
rgrover1 163:8bd70d17589f 243 // GattCharacteristic txPowerChar;
rgrover1 167:8057dc59ac08 244 GattCharacteristic beaconPeriodChar;
rgrover1 147:f772d9b93808 245 };
rgrover1 147:f772d9b93808 246
rgrover1 147:f772d9b93808 247 #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/