High level Bluetooth Low Energy API and radio abstraction layer

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Aug 07 15:55:21 2015 +0100
Revision:
771:26b809199308
Child:
782:5736312322d9
Synchronized with git rev bff44f94
Author: Rohit Grover
Adding an initial implementation for Eddystone beacon service.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 771:26b809199308 1 /* mbed Microcontroller Library
rgrover1 771:26b809199308 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 771:26b809199308 3 *
rgrover1 771:26b809199308 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 771:26b809199308 5 * you may not use this file except in compliance with the License.
rgrover1 771:26b809199308 6 * You may obtain a copy of the License at
rgrover1 771:26b809199308 7 *
rgrover1 771:26b809199308 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 771:26b809199308 9 *
rgrover1 771:26b809199308 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 771:26b809199308 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 771:26b809199308 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 771:26b809199308 13 * See the License for the specific language governing permissions and
rgrover1 771:26b809199308 14 * limitations under the License.
rgrover1 771:26b809199308 15 */
rgrover1 771:26b809199308 16
rgrover1 771:26b809199308 17 #ifndef SERVICES_EDDYSTONEBEACON_H_
rgrover1 771:26b809199308 18 #define SERVICES_EDDYSTONEBEACON_H_
rgrover1 771:26b809199308 19
rgrover1 771:26b809199308 20 #include "ble/BLE.h"
rgrover1 771:26b809199308 21 #include "mbed.h"
rgrover1 771:26b809199308 22
rgrover1 771:26b809199308 23 static const uint8_t BEACON_EDDYSTONE[] = {0xAA, 0xFE};
rgrover1 771:26b809199308 24
rgrover1 771:26b809199308 25 #if 0
rgrover1 771:26b809199308 26 #define INFO(MSG, ...) printf("[EddyStone: INFO]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 771:26b809199308 27 #define DBG(MSG, ...) printf("[EddyStone: DBG]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 771:26b809199308 28 #define WARN(MSG, ...) printf("[EddyStone: WARN]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 771:26b809199308 29 #define ERR(MSG, ...) printf("[EddyStone: ERR]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 771:26b809199308 30 #else
rgrover1 771:26b809199308 31 #define INFO(MSG, ...)
rgrover1 771:26b809199308 32 #define DBG(MSG, ...)
rgrover1 771:26b809199308 33 #define WARN(MSG, ...)
rgrover1 771:26b809199308 34 #define ERR(MSG, ...)
rgrover1 771:26b809199308 35 #endif // if 0
rgrover1 771:26b809199308 36
rgrover1 771:26b809199308 37 /**
rgrover1 771:26b809199308 38 * @class Eddystone
rgrover1 771:26b809199308 39 * @brief Eddystone Configuration Service. Can be used to set URL, adjust power levels, and set flags.
rgrover1 771:26b809199308 40 * See https://github.com/google/eddystone
rgrover1 771:26b809199308 41 *
rgrover1 771:26b809199308 42 */
rgrover1 771:26b809199308 43 class EddystoneService
rgrover1 771:26b809199308 44 {
rgrover1 771:26b809199308 45 public:
rgrover1 771:26b809199308 46 static const unsigned ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service.
rgrover1 771:26b809199308 47 static const size_t SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets
rgrover1 771:26b809199308 48
rgrover1 771:26b809199308 49 static const size_t URI_DATA_MAX = 18;
rgrover1 771:26b809199308 50 typedef uint8_t UriData_t[URI_DATA_MAX];
rgrover1 771:26b809199308 51
rgrover1 771:26b809199308 52 // UID Frame Type subfields
rgrover1 771:26b809199308 53 static const size_t UID_NAMESPACEID_SIZE = 10;
rgrover1 771:26b809199308 54 static const size_t UID_INSTANCEID_SIZE = 6;
rgrover1 771:26b809199308 55 typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE];
rgrover1 771:26b809199308 56 typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE];
rgrover1 771:26b809199308 57
rgrover1 771:26b809199308 58 // Eddystone Frame Type ID
rgrover1 771:26b809199308 59 enum {
rgrover1 771:26b809199308 60 FRAME_TYPE_UID = 0x00,
rgrover1 771:26b809199308 61 FRAME_TYPE_URL = 0x10,
rgrover1 771:26b809199308 62 FRAME_TYPE_TLM = 0x20,
rgrover1 771:26b809199308 63 NUM_FRAMETYPES = 3
rgrover1 771:26b809199308 64 };
rgrover1 771:26b809199308 65
rgrover1 771:26b809199308 66 static const uint8_t FRAME_SIZE_UID = 20; // includes RFU bytes
rgrover1 771:26b809199308 67 static const uint8_t FRAME_SIZE_TLM = 14; // TLM frame is a constant 14Bytes
rgrover1 771:26b809199308 68
rgrover1 771:26b809199308 69 public:
rgrover1 771:26b809199308 70 /**
rgrover1 771:26b809199308 71 * Constructor: sets up the parameters used by the Eddystone beacon.
rgrover1 771:26b809199308 72 * This function should be used in lieu of the config service.
rgrover1 771:26b809199308 73 *
rgrover1 771:26b809199308 74 * @param bleIn
rgrover1 771:26b809199308 75 * ble object for the underlying controller.
rgrover1 771:26b809199308 76 * @oaram interval
rgrover1 771:26b809199308 77 * This is is how often broadcasts are made (in units of milliseconds).
rgrover1 771:26b809199308 78 * @param txPowerLevel
rgrover1 771:26b809199308 79 * The broadcasting power level (in dbM).
rgrover1 771:26b809199308 80 * @param UIDNamespaceID
rgrover1 771:26b809199308 81 * 10-Byte Namespace UUID.
rgrover1 771:26b809199308 82 * @param UIDInstanceID
rgrover1 771:26b809199308 83 * 6-Byte Instance UUID.
rgrover1 771:26b809199308 84 * @param url
rgrover1 771:26b809199308 85 * Shortened URL to broadcast (pass in as a string).
rgrover1 771:26b809199308 86 * @param tlmVersion
rgrover1 771:26b809199308 87 * version of telemetry data field to use (default to 0x00).
rgrover1 771:26b809199308 88 */
rgrover1 771:26b809199308 89 EddystoneService(BLEDevice &bleIn,
rgrover1 771:26b809199308 90 uint16_t interval = 100,
rgrover1 771:26b809199308 91 uint8_t txPowerLevel = 0,
rgrover1 771:26b809199308 92 const uint8_t *UIDNamespaceID = NULL,
rgrover1 771:26b809199308 93 const uint8_t *UIDInstanceID = NULL,
rgrover1 771:26b809199308 94 const char *url = NULL,
rgrover1 771:26b809199308 95 uint8_t tlmVersion = 0) :
rgrover1 771:26b809199308 96 ble(bleIn),
rgrover1 771:26b809199308 97 uidIsSet(false),
rgrover1 771:26b809199308 98 urlIsSet(false),
rgrover1 771:26b809199308 99 frameIndex(0),
rgrover1 771:26b809199308 100 uidRFU(0),
rgrover1 771:26b809199308 101 switchFlag(true),
rgrover1 771:26b809199308 102 TlmPduCount(0),
rgrover1 771:26b809199308 103 TlmTimeSinceBoot(0)
rgrover1 771:26b809199308 104 {
rgrover1 771:26b809199308 105 // Check optional frames, set their 'isSet' flags appropriately
rgrover1 771:26b809199308 106 if ((UIDNamespaceID != NULL) & (UIDInstanceID != NULL)) {
rgrover1 771:26b809199308 107 setUIDFrameData(txPowerLevel, UIDNamespaceID, UIDInstanceID);
rgrover1 771:26b809199308 108 uidIsSet = true;
rgrover1 771:26b809199308 109 } else if (url != NULL) {
rgrover1 771:26b809199308 110 setURLFrameData(txPowerLevel, url);
rgrover1 771:26b809199308 111 urlIsSet = true;
rgrover1 771:26b809199308 112 } else {
rgrover1 771:26b809199308 113 // Default TLM frame to version 0x00, start all values at zero to be spec compliant.
rgrover1 771:26b809199308 114 setTLMFrameData(tlmVersion, 0x00, 0x00);
rgrover1 771:26b809199308 115 }
rgrover1 771:26b809199308 116
rgrover1 771:26b809199308 117 ble.gap().setTxPower(txPowerLevel);
rgrover1 771:26b809199308 118 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
rgrover1 771:26b809199308 119 ble.gap().setAdvertisingInterval(interval);
rgrover1 771:26b809199308 120
rgrover1 771:26b809199308 121 ble.gap().clearAdvertisingPayload();
rgrover1 771:26b809199308 122 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 771:26b809199308 123 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE));
rgrover1 771:26b809199308 124
rgrover1 771:26b809199308 125 // Construct TLM Frame in initial advertising.
rgrover1 771:26b809199308 126 uint8_t serviceData[SERVICE_DATA_MAX];
rgrover1 771:26b809199308 127 unsigned serviceDataLen = 0;
rgrover1 771:26b809199308 128 serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0];
rgrover1 771:26b809199308 129 serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1];
rgrover1 771:26b809199308 130 serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, SERVICE_DATA_MAX);
rgrover1 771:26b809199308 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen);
rgrover1 771:26b809199308 132
rgrover1 771:26b809199308 133 ble.gap().startAdvertising();
rgrover1 771:26b809199308 134 ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback);
rgrover1 771:26b809199308 135
rgrover1 771:26b809199308 136 timeSinceBootTick.attach(this, &EddystoneService::tsbCallback, 0.1); // increment the TimeSinceBoot ticker every 0.1s
rgrover1 771:26b809199308 137 }
rgrover1 771:26b809199308 138
rgrover1 771:26b809199308 139 /*
rgrover1 771:26b809199308 140 * Update the TLM frame battery voltage value
rgrover1 771:26b809199308 141 * @param[in] voltagemv Voltage to update the TLM field battery voltage with (in mV)
rgrover1 771:26b809199308 142 * @return nothing
rgrover1 771:26b809199308 143 */
rgrover1 771:26b809199308 144 void updateTlmBatteryVoltage(uint16_t voltagemv) {
rgrover1 771:26b809199308 145 TlmBatteryVoltage = voltagemv;
rgrover1 771:26b809199308 146 }
rgrover1 771:26b809199308 147
rgrover1 771:26b809199308 148 /*
rgrover1 771:26b809199308 149 * Update the TLM frame beacon temperature
rgrover1 771:26b809199308 150 * @param[in] temp Temperature of beacon (in 8.8fpn)
rgrover1 771:26b809199308 151 * @return nothing
rgrover1 771:26b809199308 152 */
rgrover1 771:26b809199308 153 void updateTlmBeaconTemp(uint16_t temp) {
rgrover1 771:26b809199308 154 TlmBeaconTemp = temp;
rgrover1 771:26b809199308 155 }
rgrover1 771:26b809199308 156
rgrover1 771:26b809199308 157 /*
rgrover1 771:26b809199308 158 * Update the TLM frame PDU Count field
rgrover1 771:26b809199308 159 * @param[in] pduCount Number of Advertising frames sent since power-up
rgrover1 771:26b809199308 160 */
rgrover1 771:26b809199308 161 void updateTlmPduCount(uint32_t pduCount) {
rgrover1 771:26b809199308 162 TlmPduCount = pduCount;
rgrover1 771:26b809199308 163 }
rgrover1 771:26b809199308 164
rgrover1 771:26b809199308 165 /*
rgrover1 771:26b809199308 166 * Update the TLM frame Time since boot in 0.1s increments
rgrover1 771:26b809199308 167 * @param[in] timeSinceBoot Time since boot in 0.1s increments
rgrover1 771:26b809199308 168 */
rgrover1 771:26b809199308 169 void updateTlmTimeSinceBoot(uint32_t timeSinceBoot) {
rgrover1 771:26b809199308 170 TlmTimeSinceBoot = timeSinceBoot;
rgrover1 771:26b809199308 171 }
rgrover1 771:26b809199308 172
rgrover1 771:26b809199308 173 protected:
rgrover1 771:26b809199308 174 /*
rgrover1 771:26b809199308 175 * callback function, called every 0.1s, increments the TimeSinceBoot field in the TLM frame
rgrover1 771:26b809199308 176 */
rgrover1 771:26b809199308 177 virtual void tsbCallback(void) {
rgrover1 771:26b809199308 178 TlmTimeSinceBoot++;
rgrover1 771:26b809199308 179 }
rgrover1 771:26b809199308 180
rgrover1 771:26b809199308 181 private:
rgrover1 771:26b809199308 182 /*
rgrover1 771:26b809199308 183 * Set Eddystone UID Frame information.
rgrover1 771:26b809199308 184 *
rgrover1 771:26b809199308 185 * @param[in] power TX Power in dB measured at 0 meters from the device. Range of -100 to +20 dB.
rgrover1 771:26b809199308 186 * @param namespaceID 10B namespace ID
rgrover1 771:26b809199308 187 * @param instanceID 6B instance ID
rgrover1 771:26b809199308 188 * @param RFU 2B of RFU, initialized to 0x0000 and not broadcast, included for future reference.
rgrover1 771:26b809199308 189 */
rgrover1 771:26b809199308 190 void setUIDFrameData(int8_t power, const UIDNamespaceID_t namespaceID, const UIDInstanceID_t instanceID, uint16_t RFU = 0x0000) {
rgrover1 771:26b809199308 191 if (power > 20) {
rgrover1 771:26b809199308 192 power = 20;
rgrover1 771:26b809199308 193 }
rgrover1 771:26b809199308 194 if (power < -100) {
rgrover1 771:26b809199308 195 power = -100;
rgrover1 771:26b809199308 196 }
rgrover1 771:26b809199308 197 defaultUidPower = power;
rgrover1 771:26b809199308 198
rgrover1 771:26b809199308 199 memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE);
rgrover1 771:26b809199308 200 memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE);
rgrover1 771:26b809199308 201
rgrover1 771:26b809199308 202 uidRFU = (uint16_t)RFU; // this is probably bad form, but it doesn't really matter yet.
rgrover1 771:26b809199308 203 }
rgrover1 771:26b809199308 204
rgrover1 771:26b809199308 205 /**
rgrover1 771:26b809199308 206 * Construct UID frame from private variables
rgrover1 771:26b809199308 207 *
rgrover1 771:26b809199308 208 * @param[in,out] data
rgrover1 771:26b809199308 209 * pointer to array to store constructed frame in
rgrover1 771:26b809199308 210 * @param[in] maxSize
rgrover1 771:26b809199308 211 * number of bytes left in array, effectively how much empty space is available to write to
rgrover1 771:26b809199308 212 *
rgrover1 771:26b809199308 213 * @return number of bytes used. negative number indicates error message.
rgrover1 771:26b809199308 214 */
rgrover1 771:26b809199308 215 int constructUIDFrame(uint8_t *data, uint8_t maxSize) {
rgrover1 771:26b809199308 216 if (maxSize < FRAME_SIZE_UID) {
rgrover1 771:26b809199308 217 return -1;
rgrover1 771:26b809199308 218 }
rgrover1 771:26b809199308 219
rgrover1 771:26b809199308 220 unsigned index = 0;
rgrover1 771:26b809199308 221 data[index++] = FRAME_TYPE_UID; // 1B Type
rgrover1 771:26b809199308 222
rgrover1 771:26b809199308 223 if (defaultUidPower > 20) { // enforce range of valid values.
rgrover1 771:26b809199308 224 defaultUidPower = 20;
rgrover1 771:26b809199308 225 }
rgrover1 771:26b809199308 226 if (defaultUidPower < -100) {
rgrover1 771:26b809199308 227 defaultUidPower = -100;
rgrover1 771:26b809199308 228 }
rgrover1 771:26b809199308 229 data[index++] = defaultUidPower; // 1B Power @ 0meter
rgrover1 771:26b809199308 230
rgrover1 771:26b809199308 231 for (size_t x = 0; x < UID_NAMESPACEID_SIZE; x++) { // 10B Namespce ID
rgrover1 771:26b809199308 232 data[index++] = defaultUidNamespaceID[x];
rgrover1 771:26b809199308 233 }
rgrover1 771:26b809199308 234
rgrover1 771:26b809199308 235 for (size_t x = 0; x< UID_INSTANCEID_SIZE; x++) { // 6B Instance ID
rgrover1 771:26b809199308 236 data[index++] = defaultUidInstanceID[x];
rgrover1 771:26b809199308 237 }
rgrover1 771:26b809199308 238
rgrover1 771:26b809199308 239 if (uidRFU) { // 2B RFU, include if non-zero, otherwise ignore
rgrover1 771:26b809199308 240 data[index++] = (uint8_t)(uidRFU >> 0);
rgrover1 771:26b809199308 241 data[index++] = (uint8_t)(uidRFU >> 8);
rgrover1 771:26b809199308 242 }
rgrover1 771:26b809199308 243
rgrover1 771:26b809199308 244 return index;
rgrover1 771:26b809199308 245 }
rgrover1 771:26b809199308 246
rgrover1 771:26b809199308 247 /*
rgrover1 771:26b809199308 248 * Update advertising data
rgrover1 771:26b809199308 249 * @return true on success, false on failure
rgrover1 771:26b809199308 250 */
rgrover1 771:26b809199308 251 bool updateAdvPacket(const uint8_t serviceData[], unsigned serviceDataLen) {
rgrover1 771:26b809199308 252 return (ble.gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen) == BLE_ERROR_NONE);
rgrover1 771:26b809199308 253 }
rgrover1 771:26b809199308 254
rgrover1 771:26b809199308 255 /*
rgrover1 771:26b809199308 256 * Set Eddystone URL Frame information.
rgrover1 771:26b809199308 257 * @param[in] power TX Power in dB measured at 0 meters from the device.
rgrover1 771:26b809199308 258 * @param url URL to encode
rgrover1 771:26b809199308 259 * @return false on success, true on failure.
rgrover1 771:26b809199308 260 */
rgrover1 771:26b809199308 261 bool setURLFrameData(int8_t power, const char *url) {
rgrover1 771:26b809199308 262 defaultUrlPower = power;
rgrover1 771:26b809199308 263 encodeURL(url, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting
rgrover1 771:26b809199308 264 if (defaultUriDataLength > URI_DATA_MAX) {
rgrover1 771:26b809199308 265 return true; // error, URL is too big
rgrover1 771:26b809199308 266 }
rgrover1 771:26b809199308 267 return false;
rgrover1 771:26b809199308 268 }
rgrover1 771:26b809199308 269
rgrover1 771:26b809199308 270 /**
rgrover1 771:26b809199308 271 * Construct URL frame from private variables.
rgrover1 771:26b809199308 272 *
rgrover1 771:26b809199308 273 * @param[in,out] data
rgrover1 771:26b809199308 274 * pointer to array to store constructed frame in
rgrover1 771:26b809199308 275 * @param[in] maxSize
rgrover1 771:26b809199308 276 * number of bytes left in array, effectively how much empty space is available to write to.
rgrover1 771:26b809199308 277 *
rgrover1 771:26b809199308 278 * @return number of bytes used. negative number indicates error message.
rgrover1 771:26b809199308 279 */
rgrover1 771:26b809199308 280 int constructURLFrame(uint8_t *data, uint8_t maxSize) {
rgrover1 771:26b809199308 281 if (maxSize < (1 + 1 + defaultUriDataLength)) {
rgrover1 771:26b809199308 282 return -1;
rgrover1 771:26b809199308 283 }
rgrover1 771:26b809199308 284
rgrover1 771:26b809199308 285 unsigned index = 0;
rgrover1 771:26b809199308 286 data[index++] = FRAME_TYPE_URL; // 1B Type
rgrover1 771:26b809199308 287 data[index++] = defaultUrlPower; // 1B TX Power
rgrover1 771:26b809199308 288 for (size_t x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL
rgrover1 771:26b809199308 289 data[index++] = defaultUriData[x];
rgrover1 771:26b809199308 290 }
rgrover1 771:26b809199308 291
rgrover1 771:26b809199308 292 return index;
rgrover1 771:26b809199308 293 }
rgrover1 771:26b809199308 294
rgrover1 771:26b809199308 295 /*
rgrover1 771:26b809199308 296 * Set Eddystone TLM Frame information.
rgrover1 771:26b809199308 297 * @param[in] Version of the TLM beacon data format
rgrover1 771:26b809199308 298 * @param batteryVoltage in millivolts
rgrover1 771:26b809199308 299 * @param beaconTemp in 8.8 floating point notation
rgrover1 771:26b809199308 300 *
rgrover1 771:26b809199308 301 */
rgrover1 771:26b809199308 302 void setTLMFrameData(uint8_t version, uint16_t batteryVoltage, uint16_t beaconTemp, uint32_t pduCount = 0, uint32_t timeSinceBoot = 0) {
rgrover1 771:26b809199308 303 TlmVersion = version;
rgrover1 771:26b809199308 304 TlmBatteryVoltage = batteryVoltage;
rgrover1 771:26b809199308 305 TlmBeaconTemp = beaconTemp;
rgrover1 771:26b809199308 306 TlmPduCount = pduCount; // reset
rgrover1 771:26b809199308 307 TlmTimeSinceBoot = timeSinceBoot; // reset
rgrover1 771:26b809199308 308 }
rgrover1 771:26b809199308 309
rgrover1 771:26b809199308 310 /*
rgrover1 771:26b809199308 311 * Construct TLM frame from private variables
rgrover1 771:26b809199308 312 * @param[in,out] data
rgrover1 771:26b809199308 313 * pointer to array to store constructed frame in
rgrover1 771:26b809199308 314 * @param[in] maxSize
rgrover1 771:26b809199308 315 * number of bytes left in array, effectively how much empty space is available to write to.
rgrover1 771:26b809199308 316 *
rgrover1 771:26b809199308 317 * @return number of bytes used. negative number indicates error message.
rgrover1 771:26b809199308 318 */
rgrover1 771:26b809199308 319 int constructTLMFrame(uint8_t *data, uint8_t maxSize) {
rgrover1 771:26b809199308 320 if (maxSize < FRAME_SIZE_TLM) {
rgrover1 771:26b809199308 321 return -1;
rgrover1 771:26b809199308 322 }
rgrover1 771:26b809199308 323
rgrover1 771:26b809199308 324 unsigned index = 0;
rgrover1 771:26b809199308 325 data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry
rgrover1 771:26b809199308 326 data[index++] = TlmVersion; // TLM Version Number
rgrover1 771:26b809199308 327 data[index++] = (uint8_t)(TlmBatteryVoltage >> 8); // Battery Voltage[0]
rgrover1 771:26b809199308 328 data[index++] = (uint8_t)(TlmBatteryVoltage >> 0); // Battery Voltage[1]
rgrover1 771:26b809199308 329 data[index++] = (uint8_t)(TlmBeaconTemp >> 8); // Beacon Temp[0]
rgrover1 771:26b809199308 330 data[index++] = (uint8_t)(TlmBeaconTemp >> 0); // Beacon Temp[1]
rgrover1 771:26b809199308 331 data[index++] = (uint8_t)(TlmPduCount >> 24); // PDU Count [0]
rgrover1 771:26b809199308 332 data[index++] = (uint8_t)(TlmPduCount >> 16); // PDU Count [1]
rgrover1 771:26b809199308 333 data[index++] = (uint8_t)(TlmPduCount >> 8); // PDU Count [2]
rgrover1 771:26b809199308 334 data[index++] = (uint8_t)(TlmPduCount >> 0); // PDU Count [3]
rgrover1 771:26b809199308 335 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 24); // Time Since Boot [0]
rgrover1 771:26b809199308 336 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 16); // Time Since Boot [1]
rgrover1 771:26b809199308 337 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 8); // Time Since Boot [2]
rgrover1 771:26b809199308 338 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 0); // Time Since Boot [3]
rgrover1 771:26b809199308 339
rgrover1 771:26b809199308 340 return index;
rgrover1 771:26b809199308 341 }
rgrover1 771:26b809199308 342
rgrover1 771:26b809199308 343 /*
rgrover1 771:26b809199308 344 * State machine for switching out frames.
rgrover1 771:26b809199308 345 * This function is called by the radioNotificationCallback when a frame needs to get swapped out.
rgrover1 771:26b809199308 346 * This function exists because of time constraints in the radioNotificationCallback, so it is effectively
rgrover1 771:26b809199308 347 * broken up into two functions.
rgrover1 771:26b809199308 348 */
rgrover1 771:26b809199308 349 void swapOutFrames(void) {
rgrover1 771:26b809199308 350 uint8_t serviceData[SERVICE_DATA_MAX];
rgrover1 771:26b809199308 351
rgrover1 771:26b809199308 352 //hard code in the Eddystone UUID
rgrover1 771:26b809199308 353 unsigned serviceDataLen = 0;
rgrover1 771:26b809199308 354 serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0];
rgrover1 771:26b809199308 355 serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1];
rgrover1 771:26b809199308 356
rgrover1 771:26b809199308 357 // if certain frames are not enabled, then skip them. Worst case TLM is always enabled
rgrover1 771:26b809199308 358 switch (frameIndex) {
rgrover1 771:26b809199308 359 case 1:
rgrover1 771:26b809199308 360 // URL Frame
rgrover1 771:26b809199308 361 if (urlIsSet) {
rgrover1 771:26b809199308 362 INFO("Swapping in URL Frame: Power: %d", defaultUrlPower);
rgrover1 771:26b809199308 363 serviceDataLen += constructURLFrame(serviceData + serviceDataLen, 20);
rgrover1 771:26b809199308 364 DBG("\t Swapping in URL Frame: len=%d ", serviceDataLen);
rgrover1 771:26b809199308 365 updateAdvPacket(serviceData, serviceDataLen);
rgrover1 771:26b809199308 366 switchFlag = false;
rgrover1 771:26b809199308 367 frameIndex++;
rgrover1 771:26b809199308 368 break;
rgrover1 771:26b809199308 369 }
rgrover1 771:26b809199308 370
rgrover1 771:26b809199308 371 case 2:
rgrover1 771:26b809199308 372 // UID Frame
rgrover1 771:26b809199308 373 if (uidIsSet) {
rgrover1 771:26b809199308 374 INFO("Swapping in UID Frame: Power: %d", defaultUidPower);
rgrover1 771:26b809199308 375 serviceDataLen += constructUIDFrame(serviceData + serviceDataLen, 20);
rgrover1 771:26b809199308 376 DBG("\t Swapping in UID Frame: len=%d", serviceDataLen);
rgrover1 771:26b809199308 377 updateAdvPacket(serviceData, serviceDataLen);
rgrover1 771:26b809199308 378 switchFlag = false;
rgrover1 771:26b809199308 379 frameIndex++;
rgrover1 771:26b809199308 380 break;
rgrover1 771:26b809199308 381 }
rgrover1 771:26b809199308 382
rgrover1 771:26b809199308 383 default:
rgrover1 771:26b809199308 384 // TLM frame
rgrover1 771:26b809199308 385 INFO("Swapping in TLM Frame: version=%x, Batt=%d, Temp = %d, PDUCnt = %d, TimeSinceBoot=%d",
rgrover1 771:26b809199308 386 TlmVersion,
rgrover1 771:26b809199308 387 TlmBatteryVoltage,
rgrover1 771:26b809199308 388 TlmBeaconTemp,
rgrover1 771:26b809199308 389 TlmPduCount,
rgrover1 771:26b809199308 390 TlmTimeSinceBoot);
rgrover1 771:26b809199308 391 serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, 20);
rgrover1 771:26b809199308 392 DBG("\t Swapping in TLM Frame: len=%d", serviceDataLen);
rgrover1 771:26b809199308 393 updateAdvPacket(serviceData, serviceDataLen);
rgrover1 771:26b809199308 394 frameIndex++;
rgrover1 771:26b809199308 395 break;
rgrover1 771:26b809199308 396 }
rgrover1 771:26b809199308 397 }
rgrover1 771:26b809199308 398
rgrover1 771:26b809199308 399 /*
rgrover1 771:26b809199308 400 * Callback from onRadioNotification(), used to update the PDUCounter and process next state.
rgrover1 771:26b809199308 401 */
rgrover1 771:26b809199308 402 void radioNotificationCallback(bool radioActive) {
rgrover1 771:26b809199308 403 static const unsigned EDDYSTONE_SWAPFRAME_DELAYMS = 1;
rgrover1 771:26b809199308 404
rgrover1 771:26b809199308 405 // Update PDUCount
rgrover1 771:26b809199308 406 TlmPduCount++;
rgrover1 771:26b809199308 407 frameIndex = frameIndex % NUM_FRAMETYPES;
rgrover1 771:26b809199308 408
rgrover1 771:26b809199308 409 if (!radioActive) { // false just after a frame is sent
rgrover1 771:26b809199308 410 // state machine to control which packet is being sent
rgrover1 771:26b809199308 411 switch (frameIndex) {
rgrover1 771:26b809199308 412 case 0: // TLM Frame
rgrover1 771:26b809199308 413 switchFrame.attach_us(this, &EddystoneService::swapOutFrames, EDDYSTONE_SWAPFRAME_DELAYMS);
rgrover1 771:26b809199308 414 switchFlag = true;
rgrover1 771:26b809199308 415 break;
rgrover1 771:26b809199308 416
rgrover1 771:26b809199308 417 case 1: // URL Frame
rgrover1 771:26b809199308 418 // switch out packets
rgrover1 771:26b809199308 419 if (switchFlag) {
rgrover1 771:26b809199308 420 switchFrame.attach_us(this, &EddystoneService::swapOutFrames, EDDYSTONE_SWAPFRAME_DELAYMS);
rgrover1 771:26b809199308 421 switchFlag = false;
rgrover1 771:26b809199308 422 } else {
rgrover1 771:26b809199308 423 if ((TlmPduCount % 10) == 0) { // every 10 adv packets switch the frame
rgrover1 771:26b809199308 424 switchFlag = true;
rgrover1 771:26b809199308 425 }
rgrover1 771:26b809199308 426 }
rgrover1 771:26b809199308 427 break;
rgrover1 771:26b809199308 428
rgrover1 771:26b809199308 429 case 2: // UIDFrame
rgrover1 771:26b809199308 430 // switch out packets
rgrover1 771:26b809199308 431 if (switchFlag) {
rgrover1 771:26b809199308 432 switchFrame.attach_us(this, &EddystoneService::swapOutFrames, EDDYSTONE_SWAPFRAME_DELAYMS);
rgrover1 771:26b809199308 433 switchFlag = false;
rgrover1 771:26b809199308 434 } else {
rgrover1 771:26b809199308 435 if ((TlmPduCount % 10) == 0) { // every 10 adv packets switch the frame
rgrover1 771:26b809199308 436 switchFlag = true;
rgrover1 771:26b809199308 437 }
rgrover1 771:26b809199308 438 }
rgrover1 771:26b809199308 439 break;
rgrover1 771:26b809199308 440 }
rgrover1 771:26b809199308 441 }
rgrover1 771:26b809199308 442 }
rgrover1 771:26b809199308 443
rgrover1 771:26b809199308 444 private:
rgrover1 771:26b809199308 445 BLEDevice &ble;
rgrover1 771:26b809199308 446 Ticker timeSinceBootTick;
rgrover1 771:26b809199308 447 Timeout switchFrame;
rgrover1 771:26b809199308 448 // Default value that is restored on reset
rgrover1 771:26b809199308 449 size_t defaultUriDataLength;
rgrover1 771:26b809199308 450 UriData_t defaultUriData;
rgrover1 771:26b809199308 451 UIDNamespaceID_t defaultUidNamespaceID;
rgrover1 771:26b809199308 452 UIDInstanceID_t defaultUidInstanceID;
rgrover1 771:26b809199308 453 int8_t defaultUidPower;
rgrover1 771:26b809199308 454 int8_t defaultUrlPower;
rgrover1 771:26b809199308 455 unsigned frameIndex;
rgrover1 771:26b809199308 456 uint16_t uidRFU;
rgrover1 771:26b809199308 457 bool uidIsSet;
rgrover1 771:26b809199308 458 bool urlIsSet;
rgrover1 771:26b809199308 459 bool switchFlag;
rgrover1 771:26b809199308 460
rgrover1 771:26b809199308 461 // Private Variables for Telemetry Data
rgrover1 771:26b809199308 462 uint8_t TlmVersion;
rgrover1 771:26b809199308 463 volatile uint16_t TlmBatteryVoltage;
rgrover1 771:26b809199308 464 volatile uint16_t TlmBeaconTemp;
rgrover1 771:26b809199308 465 volatile uint32_t TlmPduCount;
rgrover1 771:26b809199308 466 volatile uint32_t TlmTimeSinceBoot;
rgrover1 771:26b809199308 467
rgrover1 771:26b809199308 468 public:
rgrover1 771:26b809199308 469 /*
rgrover1 771:26b809199308 470 * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification).
rgrover1 771:26b809199308 471 */
rgrover1 771:26b809199308 472 static void encodeURL(const char *uriDataIn, UriData_t uriDataOut, size_t &sizeofURIDataOut) {
rgrover1 771:26b809199308 473 const char *prefixes[] = {
rgrover1 771:26b809199308 474 "http://www.",
rgrover1 771:26b809199308 475 "https://www.",
rgrover1 771:26b809199308 476 "http://",
rgrover1 771:26b809199308 477 "https://",
rgrover1 771:26b809199308 478 };
rgrover1 771:26b809199308 479 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
rgrover1 771:26b809199308 480 const char *suffixes[] = {
rgrover1 771:26b809199308 481 ".com/",
rgrover1 771:26b809199308 482 ".org/",
rgrover1 771:26b809199308 483 ".edu/",
rgrover1 771:26b809199308 484 ".net/",
rgrover1 771:26b809199308 485 ".info/",
rgrover1 771:26b809199308 486 ".biz/",
rgrover1 771:26b809199308 487 ".gov/",
rgrover1 771:26b809199308 488 ".com",
rgrover1 771:26b809199308 489 ".org",
rgrover1 771:26b809199308 490 ".edu",
rgrover1 771:26b809199308 491 ".net",
rgrover1 771:26b809199308 492 ".info",
rgrover1 771:26b809199308 493 ".biz",
rgrover1 771:26b809199308 494 ".gov"
rgrover1 771:26b809199308 495 };
rgrover1 771:26b809199308 496 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
rgrover1 771:26b809199308 497
rgrover1 771:26b809199308 498 sizeofURIDataOut = 0;
rgrover1 771:26b809199308 499 memset(uriDataOut, 0, sizeof(UriData_t));
rgrover1 771:26b809199308 500
rgrover1 771:26b809199308 501 if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) {
rgrover1 771:26b809199308 502 return;
rgrover1 771:26b809199308 503 }
rgrover1 771:26b809199308 504
rgrover1 771:26b809199308 505 /*
rgrover1 771:26b809199308 506 * handle prefix
rgrover1 771:26b809199308 507 */
rgrover1 771:26b809199308 508 for (unsigned i = 0; i < NUM_PREFIXES; i++) {
rgrover1 771:26b809199308 509 size_t prefixLen = strlen(prefixes[i]);
rgrover1 771:26b809199308 510 if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) {
rgrover1 771:26b809199308 511 uriDataOut[sizeofURIDataOut++] = i;
rgrover1 771:26b809199308 512 uriDataIn += prefixLen;
rgrover1 771:26b809199308 513 break;
rgrover1 771:26b809199308 514 }
rgrover1 771:26b809199308 515 }
rgrover1 771:26b809199308 516
rgrover1 771:26b809199308 517 /*
rgrover1 771:26b809199308 518 * handle suffixes
rgrover1 771:26b809199308 519 */
rgrover1 771:26b809199308 520 while (*uriDataIn && (sizeofURIDataOut < URI_DATA_MAX)) {
rgrover1 771:26b809199308 521 /* check for suffix match */
rgrover1 771:26b809199308 522 unsigned i;
rgrover1 771:26b809199308 523 for (i = 0; i < NUM_SUFFIXES; i++) {
rgrover1 771:26b809199308 524 size_t suffixLen = strlen(suffixes[i]);
rgrover1 771:26b809199308 525 if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) {
rgrover1 771:26b809199308 526 uriDataOut[sizeofURIDataOut++] = i;
rgrover1 771:26b809199308 527 uriDataIn += suffixLen;
rgrover1 771:26b809199308 528 break; /* from the for loop for checking against suffixes */
rgrover1 771:26b809199308 529 }
rgrover1 771:26b809199308 530 }
rgrover1 771:26b809199308 531 /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
rgrover1 771:26b809199308 532 if (i == NUM_SUFFIXES) {
rgrover1 771:26b809199308 533 uriDataOut[sizeofURIDataOut++] = *uriDataIn;
rgrover1 771:26b809199308 534 ++uriDataIn;
rgrover1 771:26b809199308 535 }
rgrover1 771:26b809199308 536 }
rgrover1 771:26b809199308 537 }
rgrover1 771:26b809199308 538 };
rgrover1 771:26b809199308 539
rgrover1 771:26b809199308 540 #endif // SERVICES_EDDYSTONEBEACON_H_