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
services/URIBeacon2Service.h@149:6a7666d72a83, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:19 2014 +0000
- Revision:
- 149:6a7666d72a83
- Parent:
- 148:0072d73e966f
- Child:
- 150:aa2d70369df0
Synchronized with git rev 3ea0f708
Author: Rohit Grover
add encodePrefix()
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 | |
rgrover1 | 147:f772d9b93808 | 25 | // ee0c2080-8786-40ba-ab96-99b91ac981d8 |
rgrover1 | 147:f772d9b93808 | 26 | |
rgrover1 | 148:0072d73e966f | 27 | URIBeacon2Service(BLEDevice &ble_, const char *urldata, size_t sizeofURLData) : ble(ble_), payloadIndex(0), payload() { |
rgrover1 | 147:f772d9b93808 | 28 | if ((sizeofURLData + 4) > 24) { |
rgrover1 | 147:f772d9b93808 | 29 | return; |
rgrover1 | 147:f772d9b93808 | 30 | } |
rgrover1 | 147:f772d9b93808 | 31 | |
rgrover1 | 147:f772d9b93808 | 32 | const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
rgrover1 | 147:f772d9b93808 | 33 | |
rgrover1 | 148:0072d73e966f | 34 | payload[payloadIndex++] = BEACON_UUID[0]; |
rgrover1 | 148:0072d73e966f | 35 | payload[payloadIndex++] = BEACON_UUID[1]; |
rgrover1 | 148:0072d73e966f | 36 | payload[payloadIndex++] = 0x00; /* flags */ |
rgrover1 | 148:0072d73e966f | 37 | payload[payloadIndex++] = 0x20; /* power */ |
rgrover1 | 148:0072d73e966f | 38 | size_t encodedBytes = encodeURIData(urldata, sizeofURLData); |
rgrover1 | 147:f772d9b93808 | 39 | |
rgrover1 | 147:f772d9b93808 | 40 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
rgrover1 | 148:0072d73e966f | 41 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, payload, encodedBytes + 4); |
rgrover1 | 147:f772d9b93808 | 42 | } |
rgrover1 | 147:f772d9b93808 | 43 | |
rgrover1 | 147:f772d9b93808 | 44 | private: |
rgrover1 | 148:0072d73e966f | 45 | size_t encodeURIData(const char *urldata, size_t sizeofURLData) { |
rgrover1 | 148:0072d73e966f | 46 | if (sizeofURLData == 0) { |
rgrover1 | 148:0072d73e966f | 47 | return 0; |
rgrover1 | 148:0072d73e966f | 48 | } |
rgrover1 | 148:0072d73e966f | 49 | |
rgrover1 | 149:6a7666d72a83 | 50 | size_t encodedBytes = encodePrefix(urldata, sizeofURLData); |
rgrover1 | 149:6a7666d72a83 | 51 | |
rgrover1 | 149:6a7666d72a83 | 52 | /* memcpy the rest for now. */ |
rgrover1 | 149:6a7666d72a83 | 53 | memcpy(&payload[payloadIndex], urldata, sizeofURLData); |
rgrover1 | 149:6a7666d72a83 | 54 | encodedBytes += sizeofURLData; |
rgrover1 | 149:6a7666d72a83 | 55 | return encodedBytes; |
rgrover1 | 149:6a7666d72a83 | 56 | } |
rgrover1 | 149:6a7666d72a83 | 57 | |
rgrover1 | 149:6a7666d72a83 | 58 | size_t encodePrefix(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 | 148:0072d73e966f | 72 | payload[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 | 147:f772d9b93808 | 84 | // URIBeacon2Service(BLEDevice &_ble, uint8_t level = 100) : |
rgrover1 | 147:f772d9b93808 | 85 | // ble(_ble), |
rgrover1 | 147:f772d9b93808 | 86 | // batteryLevel(level), |
rgrover1 | 147:f772d9b93808 | 87 | // batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), |
rgrover1 | 147:f772d9b93808 | 88 | // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
rgrover1 | 147:f772d9b93808 | 89 | |
rgrover1 | 147:f772d9b93808 | 90 | // static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 147:f772d9b93808 | 91 | // if (serviceAdded) { |
rgrover1 | 147:f772d9b93808 | 92 | // return; |
rgrover1 | 147:f772d9b93808 | 93 | // } |
rgrover1 | 147:f772d9b93808 | 94 | |
rgrover1 | 147:f772d9b93808 | 95 | // GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
rgrover1 | 147:f772d9b93808 | 96 | // GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 147:f772d9b93808 | 97 | |
rgrover1 | 147:f772d9b93808 | 98 | // ble.addService(batteryService); |
rgrover1 | 147:f772d9b93808 | 99 | // serviceAdded = true; |
rgrover1 | 147:f772d9b93808 | 100 | // } |
rgrover1 | 147:f772d9b93808 | 101 | // |
rgrover1 | 147:f772d9b93808 | 102 | |
rgrover1 | 147:f772d9b93808 | 103 | private: |
rgrover1 | 147:f772d9b93808 | 104 | static const size_t MAX_SIZEOF_PAYLOAD = 32; /* TODO */ |
rgrover1 | 147:f772d9b93808 | 105 | |
rgrover1 | 147:f772d9b93808 | 106 | private: |
rgrover1 | 147:f772d9b93808 | 107 | BLEDevice &ble; |
rgrover1 | 148:0072d73e966f | 108 | |
rgrover1 | 148:0072d73e966f | 109 | size_t payloadIndex; |
rgrover1 | 147:f772d9b93808 | 110 | uint8_t payload[MAX_SIZEOF_PAYLOAD]; |
rgrover1 | 147:f772d9b93808 | 111 | // uint8_t batteryLevel; |
rgrover1 | 147:f772d9b93808 | 112 | // GattCharacteristic batteryLevelCharacteristic; |
rgrover1 | 147:f772d9b93808 | 113 | }; |
rgrover1 | 147:f772d9b93808 | 114 | |
rgrover1 | 147:f772d9b93808 | 115 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |