Minor temporary patch to allow DFU packet callback

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 28 14:11:20 2014 +0000
Revision:
157:8bd620881bc9
Parent:
156:cc021c5ba5b8
Child:
158:08f609d8a6d4
Synchronized with git rev fc8788b5
Author: Rohit Grover
MAX_SIZEOF_SERVICE_DATA_PAYLOAD to 27

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 151:cb5df770f05b 24 URIBeacon2Service(BLEDevice &ble_, const char *urldata, uint8_t flags = 0, uint8_t power = 0) : ble(ble_), payloadIndex(0), serviceDataPayload() {
rgrover1 147:f772d9b93808 25 const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
rgrover1 147:f772d9b93808 26
rgrover1 151:cb5df770f05b 27 serviceDataPayload[payloadIndex++] = BEACON_UUID[0];
rgrover1 151:cb5df770f05b 28 serviceDataPayload[payloadIndex++] = BEACON_UUID[1];
rgrover1 151:cb5df770f05b 29 serviceDataPayload[payloadIndex++] = flags;
rgrover1 151:cb5df770f05b 30 serviceDataPayload[payloadIndex++] = power;
rgrover1 150:aa2d70369df0 31
rgrover1 150:aa2d70369df0 32 size_t sizeofURLData = strlen(urldata);
rgrover1 151:cb5df770f05b 33 size_t encodedBytes = encodeServiceData(urldata, sizeofURLData);
rgrover1 147:f772d9b93808 34
rgrover1 152:fc0bb2ba916b 35 ble.setTxPower(power);
rgrover1 147:f772d9b93808 36 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
rgrover1 151:cb5df770f05b 37 ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4);
rgrover1 147:f772d9b93808 38 }
rgrover1 147:f772d9b93808 39
rgrover1 151:cb5df770f05b 40 void dumpEncodedSeviceData() const {
rgrover1 150:aa2d70369df0 41 printf("encoded: '");
rgrover1 150:aa2d70369df0 42 for (unsigned i = 0; i < payloadIndex; i++) {
rgrover1 151:cb5df770f05b 43 printf(" %02x", serviceDataPayload[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 151:cb5df770f05b 49 size_t encodeServiceData(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 151:cb5df770f05b 54 return encodeURISchemePrefix(urldata, sizeofURLData) + encodeURI(urldata, sizeofURLData);
rgrover1 149:6a7666d72a83 55 }
rgrover1 149:6a7666d72a83 56
rgrover1 151:cb5df770f05b 57 size_t encodeURISchemePrefix(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 151:cb5df770f05b 71 serviceDataPayload[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 151:cb5df770f05b 83 size_t encodeURI(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 155:a3fafce0bc0b 103 while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_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 151:cb5df770f05b 113 serviceDataPayload[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 151:cb5df770f05b 122 serviceDataPayload[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 private:
rgrover1 157:8bd620881bc9 133 static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 27;
rgrover1 147:f772d9b93808 134
rgrover1 147:f772d9b93808 135 private:
rgrover1 147:f772d9b93808 136 BLEDevice &ble;
rgrover1 148:0072d73e966f 137
rgrover1 148:0072d73e966f 138 size_t payloadIndex;
rgrover1 155:a3fafce0bc0b 139 uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD];
rgrover1 147:f772d9b93808 140 };
rgrover1 147:f772d9b93808 141
rgrover1 147:f772d9b93808 142 #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/