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 Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 28 14:11:20 2014 +0000
Revision:
150:aa2d70369df0
Parent:
149:6a7666d72a83
Child:
151:cb5df770f05b
Synchronized with git rev 2586b27a
Author: Rohit Grover
add copyURLCheckingForSuffixes()

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 147:f772d9b93808 22 class URIBeacon2Service {
rgrover1 147:f772d9b93808 23 public:
rgrover1 147:f772d9b93808 24 // ee0c2080-8786-40ba-ab96-99b91ac981d8
rgrover1 150:aa2d70369df0 25 URIBeacon2Service(BLEDevice &ble_, const char *urldata) : ble(ble_), payloadIndex(0), payload() {
rgrover1 147:f772d9b93808 26 const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
rgrover1 147:f772d9b93808 27
rgrover1 148:0072d73e966f 28 payload[payloadIndex++] = BEACON_UUID[0];
rgrover1 148:0072d73e966f 29 payload[payloadIndex++] = BEACON_UUID[1];
rgrover1 148:0072d73e966f 30 payload[payloadIndex++] = 0x00; /* flags */
rgrover1 148:0072d73e966f 31 payload[payloadIndex++] = 0x20; /* power */
rgrover1 150:aa2d70369df0 32
rgrover1 150:aa2d70369df0 33 size_t sizeofURLData = strlen(urldata);
rgrover1 148:0072d73e966f 34 size_t encodedBytes = encodeURIData(urldata, sizeofURLData);
rgrover1 147:f772d9b93808 35
rgrover1 147:f772d9b93808 36 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
rgrover1 148:0072d73e966f 37 ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, payload, encodedBytes + 4);
rgrover1 147:f772d9b93808 38 }
rgrover1 147:f772d9b93808 39
rgrover1 150:aa2d70369df0 40 void dumpEncoded() {
rgrover1 150:aa2d70369df0 41 printf("encoded: '");
rgrover1 150:aa2d70369df0 42 for (unsigned i = 0; i < payloadIndex; i++) {
rgrover1 150:aa2d70369df0 43 printf(" %02x", payload[i]);
rgrover1 150:aa2d70369df0 44 }
rgrover1 150:aa2d70369df0 45 printf("'\r\n");
rgrover1 150:aa2d70369df0 46 }
rgrover1 150:aa2d70369df0 47
rgrover1 147:f772d9b93808 48 private:
rgrover1 148:0072d73e966f 49 size_t encodeURIData(const char *urldata, size_t sizeofURLData) {
rgrover1 148:0072d73e966f 50 if (sizeofURLData == 0) {
rgrover1 148:0072d73e966f 51 return 0;
rgrover1 148:0072d73e966f 52 }
rgrover1 148:0072d73e966f 53
rgrover1 150:aa2d70369df0 54 return encodePrefix(urldata, sizeofURLData) + copyURLCheckingForSuffixes(urldata, sizeofURLData);
rgrover1 149:6a7666d72a83 55 }
rgrover1 149:6a7666d72a83 56
rgrover1 149:6a7666d72a83 57 size_t encodePrefix(const char *&urldata, size_t &sizeofURLData) {
rgrover1 148:0072d73e966f 58 const char *prefixes[] = {
rgrover1 148:0072d73e966f 59 "http://www.",
rgrover1 148:0072d73e966f 60 "https://www.",
rgrover1 148:0072d73e966f 61 "http://",
rgrover1 148:0072d73e966f 62 "https://",
rgrover1 148:0072d73e966f 63 "urn:uuid:"
rgrover1 148:0072d73e966f 64 };
rgrover1 149:6a7666d72a83 65
rgrover1 148:0072d73e966f 66 size_t encodedBytes = 0;
rgrover1 148:0072d73e966f 67 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
rgrover1 148:0072d73e966f 68 for (unsigned i = 0; i < NUM_PREFIXES; i++) {
rgrover1 148:0072d73e966f 69 size_t prefixLen = strlen(prefixes[i]);
rgrover1 148:0072d73e966f 70 if (strncmp(urldata, prefixes[i], prefixLen) == 0) {
rgrover1 148:0072d73e966f 71 payload[payloadIndex++] = i;
rgrover1 149:6a7666d72a83 72 encodedBytes = 1;
rgrover1 148:0072d73e966f 73
rgrover1 148:0072d73e966f 74 urldata += prefixLen;
rgrover1 148:0072d73e966f 75 sizeofURLData -= prefixLen;
rgrover1 148:0072d73e966f 76 break;
rgrover1 148:0072d73e966f 77 }
rgrover1 148:0072d73e966f 78 }
rgrover1 148:0072d73e966f 79
rgrover1 148:0072d73e966f 80 return encodedBytes;
rgrover1 147:f772d9b93808 81 }
rgrover1 147:f772d9b93808 82
rgrover1 150:aa2d70369df0 83 size_t copyURLCheckingForSuffixes(const char *urldata, size_t sizeofURLData) {
rgrover1 150:aa2d70369df0 84 const char *suffixes[] = {
rgrover1 150:aa2d70369df0 85 ".com/",
rgrover1 150:aa2d70369df0 86 ".org/",
rgrover1 150:aa2d70369df0 87 ".edu/",
rgrover1 150:aa2d70369df0 88 ".net/",
rgrover1 150:aa2d70369df0 89 ".info/",
rgrover1 150:aa2d70369df0 90 ".biz/",
rgrover1 150:aa2d70369df0 91 ".gov/",
rgrover1 150:aa2d70369df0 92 ".com",
rgrover1 150:aa2d70369df0 93 ".org",
rgrover1 150:aa2d70369df0 94 ".edu",
rgrover1 150:aa2d70369df0 95 ".net",
rgrover1 150:aa2d70369df0 96 ".info",
rgrover1 150:aa2d70369df0 97 ".biz",
rgrover1 150:aa2d70369df0 98 ".gov"
rgrover1 150:aa2d70369df0 99 };
rgrover1 150:aa2d70369df0 100 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
rgrover1 150:aa2d70369df0 101
rgrover1 150:aa2d70369df0 102 size_t encodedBytes = 0;
rgrover1 150:aa2d70369df0 103 while (sizeofURLData && (payloadIndex < MAX_SIZEOF_PAYLOAD)) {
rgrover1 150:aa2d70369df0 104 /* check for suffix match */
rgrover1 150:aa2d70369df0 105 unsigned i;
rgrover1 150:aa2d70369df0 106 for (i = 0; i < NUM_SUFFIXES; i++) {
rgrover1 150:aa2d70369df0 107 size_t suffixLen = strlen(suffixes[i]);
rgrover1 150:aa2d70369df0 108 if ((suffixLen == 0) || (sizeofURLData < suffixLen)) {
rgrover1 150:aa2d70369df0 109 continue;
rgrover1 150:aa2d70369df0 110 }
rgrover1 150:aa2d70369df0 111
rgrover1 150:aa2d70369df0 112 if (strncmp(urldata, suffixes[i], suffixLen) == 0) {
rgrover1 150:aa2d70369df0 113 payload[payloadIndex++] = i;
rgrover1 150:aa2d70369df0 114 ++encodedBytes;
rgrover1 150:aa2d70369df0 115 urldata += suffixLen;
rgrover1 150:aa2d70369df0 116 sizeofURLData -= suffixLen;
rgrover1 150:aa2d70369df0 117 break; /* from the for loop for checking against suffixes */
rgrover1 150:aa2d70369df0 118 }
rgrover1 150:aa2d70369df0 119 }
rgrover1 150:aa2d70369df0 120 /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
rgrover1 150:aa2d70369df0 121 if (i == NUM_SUFFIXES) {
rgrover1 150:aa2d70369df0 122 payload[payloadIndex++] = *urldata;
rgrover1 150:aa2d70369df0 123 ++encodedBytes;
rgrover1 150:aa2d70369df0 124 ++urldata;
rgrover1 150:aa2d70369df0 125 --sizeofURLData;
rgrover1 150:aa2d70369df0 126 }
rgrover1 150:aa2d70369df0 127 }
rgrover1 150:aa2d70369df0 128
rgrover1 150:aa2d70369df0 129 return encodedBytes;
rgrover1 150:aa2d70369df0 130 }
rgrover1 150:aa2d70369df0 131
rgrover1 147:f772d9b93808 132 // URIBeacon2Service(BLEDevice &_ble, uint8_t level = 100) :
rgrover1 147:f772d9b93808 133 // ble(_ble),
rgrover1 147:f772d9b93808 134 // batteryLevel(level),
rgrover1 147:f772d9b93808 135 // batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel),
rgrover1 147:f772d9b93808 136 // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
rgrover1 147:f772d9b93808 137
rgrover1 147:f772d9b93808 138 // static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
rgrover1 147:f772d9b93808 139 // if (serviceAdded) {
rgrover1 147:f772d9b93808 140 // return;
rgrover1 147:f772d9b93808 141 // }
rgrover1 147:f772d9b93808 142
rgrover1 147:f772d9b93808 143 // GattCharacteristic *charTable[] = {&batteryLevelCharacteristic};
rgrover1 147:f772d9b93808 144 // GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 147:f772d9b93808 145
rgrover1 147:f772d9b93808 146 // ble.addService(batteryService);
rgrover1 147:f772d9b93808 147 // serviceAdded = true;
rgrover1 147:f772d9b93808 148 // }
rgrover1 147:f772d9b93808 149 //
rgrover1 147:f772d9b93808 150
rgrover1 147:f772d9b93808 151 private:
rgrover1 147:f772d9b93808 152 static const size_t MAX_SIZEOF_PAYLOAD = 32; /* TODO */
rgrover1 147:f772d9b93808 153
rgrover1 147:f772d9b93808 154 private:
rgrover1 147:f772d9b93808 155 BLEDevice &ble;
rgrover1 148:0072d73e966f 156
rgrover1 148:0072d73e966f 157 size_t payloadIndex;
rgrover1 147:f772d9b93808 158 uint8_t payload[MAX_SIZEOF_PAYLOAD];
rgrover1 147:f772d9b93808 159 // uint8_t batteryLevel;
rgrover1 147:f772d9b93808 160 // GattCharacteristic batteryLevelCharacteristic;
rgrover1 147:f772d9b93808 161 };
rgrover1 147:f772d9b93808 162
rgrover1 147:f772d9b93808 163 #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/