Improve readability with getHandle inline

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 28 14:11:22 2014 +0000
Revision:
169:e030a9308fcc
Synchronized with git rev 2138c544
Author: Rohit Grover
mark OndataWritten() as protected

Who changed what in which revision?

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