Improve readability with getHandle inline
Fork of BLE_API by
services/URIBeacon2Service.h@158:08f609d8a6d4, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:20 2014 +0000
- Revision:
- 158:08f609d8a6d4
- Parent:
- 157:8bd620881bc9
- Child:
- 159:e915a0cebcce
Synchronized with git rev 29a046fa
Author: Rohit Grover
extracted a setup().
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 | 147:f772d9b93808 | 22 | class URIBeacon2Service { |
rgrover1 | 147:f772d9b93808 | 23 | public: |
rgrover1 | 158:08f609d8a6d4 | 24 | URIBeacon2Service(BLEDevice &ble_, const char *urldata, uint8_t flags_ = 0, uint8_t power_ = 0) : |
rgrover1 | 158:08f609d8a6d4 | 25 | ble(ble_), payloadIndex(0), serviceDataPayload(), |
rgrover1 | 158:08f609d8a6d4 | 26 | uriDataLength(0), |
rgrover1 | 158:08f609d8a6d4 | 27 | uriDataValue(), |
rgrover1 | 158:08f609d8a6d4 | 28 | flags(flags_), |
rgrover1 | 158:08f609d8a6d4 | 29 | power(power_) |
rgrover1 | 158:08f609d8a6d4 | 30 | { |
rgrover1 | 158:08f609d8a6d4 | 31 | if ((urldata == NULL) || ((uriDataLength = strlen(urldata)) == 0)) { |
rgrover1 | 158:08f609d8a6d4 | 32 | return; |
rgrover1 | 158:08f609d8a6d4 | 33 | } |
rgrover1 | 158:08f609d8a6d4 | 34 | strncpy(reinterpret_cast<char *>(uriDataValue), urldata, MAX_SIZE_URI_DATA_CHAR_VALUE); |
rgrover1 | 147:f772d9b93808 | 35 | |
rgrover1 | 158:08f609d8a6d4 | 36 | setup(); |
rgrover1 | 147:f772d9b93808 | 37 | } |
rgrover1 | 147:f772d9b93808 | 38 | |
rgrover1 | 151:cb5df770f05b | 39 | void dumpEncodedSeviceData() const { |
rgrover1 | 150:aa2d70369df0 | 40 | printf("encoded: '"); |
rgrover1 | 150:aa2d70369df0 | 41 | for (unsigned i = 0; i < payloadIndex; i++) { |
rgrover1 | 151:cb5df770f05b | 42 | printf(" %02x", serviceDataPayload[i]); |
rgrover1 | 150:aa2d70369df0 | 43 | } |
rgrover1 | 150:aa2d70369df0 | 44 | printf("'\r\n"); |
rgrover1 | 150:aa2d70369df0 | 45 | } |
rgrover1 | 150:aa2d70369df0 | 46 | |
rgrover1 | 147:f772d9b93808 | 47 | private: |
rgrover1 | 158:08f609d8a6d4 | 48 | void setup(void) { |
rgrover1 | 158:08f609d8a6d4 | 49 | payloadIndex = 0; |
rgrover1 | 148:0072d73e966f | 50 | |
rgrover1 | 158:08f609d8a6d4 | 51 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 158:08f609d8a6d4 | 52 | serviceDataPayload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 158:08f609d8a6d4 | 53 | serviceDataPayload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 158:08f609d8a6d4 | 54 | serviceDataPayload[payloadIndex++] = flags; |
rgrover1 | 158:08f609d8a6d4 | 55 | serviceDataPayload[payloadIndex++] = power; |
rgrover1 | 158:08f609d8a6d4 | 56 | |
rgrover1 | 158:08f609d8a6d4 | 57 | const char *urlData = reinterpret_cast<char *>(uriDataValue); |
rgrover1 | 158:08f609d8a6d4 | 58 | size_t sizeofURLData = uriDataLength; |
rgrover1 | 158:08f609d8a6d4 | 59 | size_t encodedBytes = encodeURISchemePrefix(urlData, sizeofURLData) + encodeURI(urlData, sizeofURLData); |
rgrover1 | 158:08f609d8a6d4 | 60 | |
rgrover1 | 158:08f609d8a6d4 | 61 | ble.setTxPower(power); |
rgrover1 | 158:08f609d8a6d4 | 62 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 158:08f609d8a6d4 | 63 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceDataPayload, encodedBytes + 4); |
rgrover1 | 149:6a7666d72a83 | 64 | } |
rgrover1 | 149:6a7666d72a83 | 65 | |
rgrover1 | 151:cb5df770f05b | 66 | size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) { |
rgrover1 | 148:0072d73e966f | 67 | const char *prefixes[] = { |
rgrover1 | 148:0072d73e966f | 68 | "http://www.", |
rgrover1 | 148:0072d73e966f | 69 | "https://www.", |
rgrover1 | 148:0072d73e966f | 70 | "http://", |
rgrover1 | 148:0072d73e966f | 71 | "https://", |
rgrover1 | 148:0072d73e966f | 72 | "urn:uuid:" |
rgrover1 | 148:0072d73e966f | 73 | }; |
rgrover1 | 149:6a7666d72a83 | 74 | |
rgrover1 | 148:0072d73e966f | 75 | size_t encodedBytes = 0; |
rgrover1 | 148:0072d73e966f | 76 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 148:0072d73e966f | 77 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 148:0072d73e966f | 78 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 148:0072d73e966f | 79 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 151:cb5df770f05b | 80 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 149:6a7666d72a83 | 81 | encodedBytes = 1; |
rgrover1 | 148:0072d73e966f | 82 | |
rgrover1 | 148:0072d73e966f | 83 | urldata += prefixLen; |
rgrover1 | 148:0072d73e966f | 84 | sizeofURLData -= prefixLen; |
rgrover1 | 148:0072d73e966f | 85 | break; |
rgrover1 | 148:0072d73e966f | 86 | } |
rgrover1 | 148:0072d73e966f | 87 | } |
rgrover1 | 148:0072d73e966f | 88 | |
rgrover1 | 148:0072d73e966f | 89 | return encodedBytes; |
rgrover1 | 147:f772d9b93808 | 90 | } |
rgrover1 | 147:f772d9b93808 | 91 | |
rgrover1 | 151:cb5df770f05b | 92 | size_t encodeURI(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 150:aa2d70369df0 | 93 | const char *suffixes[] = { |
rgrover1 | 150:aa2d70369df0 | 94 | ".com/", |
rgrover1 | 150:aa2d70369df0 | 95 | ".org/", |
rgrover1 | 150:aa2d70369df0 | 96 | ".edu/", |
rgrover1 | 150:aa2d70369df0 | 97 | ".net/", |
rgrover1 | 150:aa2d70369df0 | 98 | ".info/", |
rgrover1 | 150:aa2d70369df0 | 99 | ".biz/", |
rgrover1 | 150:aa2d70369df0 | 100 | ".gov/", |
rgrover1 | 150:aa2d70369df0 | 101 | ".com", |
rgrover1 | 150:aa2d70369df0 | 102 | ".org", |
rgrover1 | 150:aa2d70369df0 | 103 | ".edu", |
rgrover1 | 150:aa2d70369df0 | 104 | ".net", |
rgrover1 | 150:aa2d70369df0 | 105 | ".info", |
rgrover1 | 150:aa2d70369df0 | 106 | ".biz", |
rgrover1 | 150:aa2d70369df0 | 107 | ".gov" |
rgrover1 | 150:aa2d70369df0 | 108 | }; |
rgrover1 | 150:aa2d70369df0 | 109 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 150:aa2d70369df0 | 110 | |
rgrover1 | 150:aa2d70369df0 | 111 | size_t encodedBytes = 0; |
rgrover1 | 155:a3fafce0bc0b | 112 | while (sizeofURLData && (payloadIndex < MAX_SIZEOF_SERVICE_DATA_PAYLOAD)) { |
rgrover1 | 150:aa2d70369df0 | 113 | /* check for suffix match */ |
rgrover1 | 150:aa2d70369df0 | 114 | unsigned i; |
rgrover1 | 150:aa2d70369df0 | 115 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 150:aa2d70369df0 | 116 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 150:aa2d70369df0 | 117 | if ((suffixLen == 0) || (sizeofURLData < suffixLen)) { |
rgrover1 | 150:aa2d70369df0 | 118 | continue; |
rgrover1 | 150:aa2d70369df0 | 119 | } |
rgrover1 | 150:aa2d70369df0 | 120 | |
rgrover1 | 150:aa2d70369df0 | 121 | if (strncmp(urldata, suffixes[i], suffixLen) == 0) { |
rgrover1 | 151:cb5df770f05b | 122 | serviceDataPayload[payloadIndex++] = i; |
rgrover1 | 150:aa2d70369df0 | 123 | ++encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 124 | urldata += suffixLen; |
rgrover1 | 150:aa2d70369df0 | 125 | sizeofURLData -= suffixLen; |
rgrover1 | 150:aa2d70369df0 | 126 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 150:aa2d70369df0 | 127 | } |
rgrover1 | 150:aa2d70369df0 | 128 | } |
rgrover1 | 150:aa2d70369df0 | 129 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 150:aa2d70369df0 | 130 | if (i == NUM_SUFFIXES) { |
rgrover1 | 151:cb5df770f05b | 131 | serviceDataPayload[payloadIndex++] = *urldata; |
rgrover1 | 150:aa2d70369df0 | 132 | ++encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 133 | ++urldata; |
rgrover1 | 150:aa2d70369df0 | 134 | --sizeofURLData; |
rgrover1 | 150:aa2d70369df0 | 135 | } |
rgrover1 | 150:aa2d70369df0 | 136 | } |
rgrover1 | 150:aa2d70369df0 | 137 | |
rgrover1 | 150:aa2d70369df0 | 138 | return encodedBytes; |
rgrover1 | 150:aa2d70369df0 | 139 | } |
rgrover1 | 150:aa2d70369df0 | 140 | |
rgrover1 | 147:f772d9b93808 | 141 | private: |
rgrover1 | 157:8bd620881bc9 | 142 | static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 27; |
rgrover1 | 158:08f609d8a6d4 | 143 | static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; |
rgrover1 | 147:f772d9b93808 | 144 | |
rgrover1 | 147:f772d9b93808 | 145 | private: |
rgrover1 | 147:f772d9b93808 | 146 | BLEDevice &ble; |
rgrover1 | 148:0072d73e966f | 147 | |
rgrover1 | 158:08f609d8a6d4 | 148 | size_t payloadIndex; |
rgrover1 | 158:08f609d8a6d4 | 149 | uint8_t serviceDataPayload[MAX_SIZEOF_SERVICE_DATA_PAYLOAD]; |
rgrover1 | 158:08f609d8a6d4 | 150 | uint16_t uriDataLength; |
rgrover1 | 158:08f609d8a6d4 | 151 | uint8_t uriDataValue[MAX_SIZE_URI_DATA_CHAR_VALUE]; |
rgrover1 | 158:08f609d8a6d4 | 152 | uint8_t flags; |
rgrover1 | 158:08f609d8a6d4 | 153 | uint8_t power; |
rgrover1 | 147:f772d9b93808 | 154 | }; |
rgrover1 | 147:f772d9b93808 | 155 | |
rgrover1 | 147:f772d9b93808 | 156 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |