Minor temporary patch to allow DFU packet callback
Fork of BLE_API by
services/URIBeacon2Service.h@148:0072d73e966f, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:19 2014 +0000
- Revision:
- 148:0072d73e966f
- Parent:
- 147:f772d9b93808
- Child:
- 149:6a7666d72a83
Synchronized with git rev 74849dd9
Author: Rohit Grover
fleshed out encodeURIData to detect prefixes.
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 | 148:0072d73e966f | 50 | const char *prefixes[] = { |
rgrover1 | 148:0072d73e966f | 51 | "http://www.", |
rgrover1 | 148:0072d73e966f | 52 | "https://www.", |
rgrover1 | 148:0072d73e966f | 53 | "http://", |
rgrover1 | 148:0072d73e966f | 54 | "https://", |
rgrover1 | 148:0072d73e966f | 55 | "urn:uuid:" |
rgrover1 | 148:0072d73e966f | 56 | }; |
rgrover1 | 148:0072d73e966f | 57 | size_t encodedBytes = 0; |
rgrover1 | 148:0072d73e966f | 58 | |
rgrover1 | 148:0072d73e966f | 59 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 148:0072d73e966f | 60 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 148:0072d73e966f | 61 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 148:0072d73e966f | 62 | if (strncmp(urldata, prefixes[i], prefixLen) == 0) { |
rgrover1 | 148:0072d73e966f | 63 | payload[payloadIndex++] = i; |
rgrover1 | 148:0072d73e966f | 64 | ++encodedBytes; |
rgrover1 | 148:0072d73e966f | 65 | |
rgrover1 | 148:0072d73e966f | 66 | urldata += prefixLen; |
rgrover1 | 148:0072d73e966f | 67 | sizeofURLData -= prefixLen; |
rgrover1 | 148:0072d73e966f | 68 | break; |
rgrover1 | 148:0072d73e966f | 69 | } |
rgrover1 | 148:0072d73e966f | 70 | } |
rgrover1 | 148:0072d73e966f | 71 | |
rgrover1 | 148:0072d73e966f | 72 | memcpy(&payload[payloadIndex], urldata, sizeofURLData); |
rgrover1 | 148:0072d73e966f | 73 | encodedBytes += sizeofURLData; |
rgrover1 | 148:0072d73e966f | 74 | |
rgrover1 | 148:0072d73e966f | 75 | return encodedBytes; |
rgrover1 | 147:f772d9b93808 | 76 | } |
rgrover1 | 147:f772d9b93808 | 77 | |
rgrover1 | 148:0072d73e966f | 78 | // size_t encodePrefix(const char *&urldata, ) |
rgrover1 | 148:0072d73e966f | 79 | |
rgrover1 | 147:f772d9b93808 | 80 | // URIBeacon2Service(BLEDevice &_ble, uint8_t level = 100) : |
rgrover1 | 147:f772d9b93808 | 81 | // ble(_ble), |
rgrover1 | 147:f772d9b93808 | 82 | // batteryLevel(level), |
rgrover1 | 147:f772d9b93808 | 83 | // batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), |
rgrover1 | 147:f772d9b93808 | 84 | // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
rgrover1 | 147:f772d9b93808 | 85 | |
rgrover1 | 147:f772d9b93808 | 86 | // static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 147:f772d9b93808 | 87 | // if (serviceAdded) { |
rgrover1 | 147:f772d9b93808 | 88 | // return; |
rgrover1 | 147:f772d9b93808 | 89 | // } |
rgrover1 | 147:f772d9b93808 | 90 | |
rgrover1 | 147:f772d9b93808 | 91 | // GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
rgrover1 | 147:f772d9b93808 | 92 | // GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 147:f772d9b93808 | 93 | |
rgrover1 | 147:f772d9b93808 | 94 | // ble.addService(batteryService); |
rgrover1 | 147:f772d9b93808 | 95 | // serviceAdded = true; |
rgrover1 | 147:f772d9b93808 | 96 | // } |
rgrover1 | 147:f772d9b93808 | 97 | // |
rgrover1 | 147:f772d9b93808 | 98 | |
rgrover1 | 147:f772d9b93808 | 99 | private: |
rgrover1 | 147:f772d9b93808 | 100 | static const size_t MAX_SIZEOF_PAYLOAD = 32; /* TODO */ |
rgrover1 | 147:f772d9b93808 | 101 | |
rgrover1 | 147:f772d9b93808 | 102 | private: |
rgrover1 | 147:f772d9b93808 | 103 | BLEDevice &ble; |
rgrover1 | 148:0072d73e966f | 104 | |
rgrover1 | 148:0072d73e966f | 105 | size_t payloadIndex; |
rgrover1 | 147:f772d9b93808 | 106 | uint8_t payload[MAX_SIZEOF_PAYLOAD]; |
rgrover1 | 147:f772d9b93808 | 107 | // uint8_t batteryLevel; |
rgrover1 | 147:f772d9b93808 | 108 | // GattCharacteristic batteryLevelCharacteristic; |
rgrover1 | 147:f772d9b93808 | 109 | }; |
rgrover1 | 147:f772d9b93808 | 110 | |
rgrover1 | 147:f772d9b93808 | 111 | #endif /* #ifndef __BLE_URI_BEACON_2_SERVICE_H__*/ |