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