Lancaster University's fork of the mbed BLE API. Lives on github, https://github.com/lancaster-university/BLE_API

Dependents:   microbit-dal microbit-dal microbit-ble-open microbit-dal ... more

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Tue Aug 11 15:13:13 2015 +0100
Revision:
783:9959291e9a3f
Parent:
782:5736312322d9
Child:
784:1e817a72a166
Synchronized with git rev bc899071
Author: Rohit Grover
oops. ble depends on nrf51822-v0.4.5.

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 783:9959291e9a3f 26 #define INFO(MSG, ...) printf("[EddyStone: INFO]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 783:9959291e9a3f 27 #define DBG(MSG, ...) printf("[EddyStone: DBG]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 783:9959291e9a3f 28 #define WARN(MSG, ...) printf("[EddyStone: WARN]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 783:9959291e9a3f 29 #define ERR(MSG, ...) printf("[EddyStone: ERR]" MSG " \t[%s,%d]\r\n", ##__VA_ARGS__, __FILE__, __LINE__);
rgrover1 771:26b809199308 30 #else
rgrover1 783:9959291e9a3f 31 #define INFO(MSG, ...)
rgrover1 783:9959291e9a3f 32 #define DBG(MSG, ...)
rgrover1 783:9959291e9a3f 33 #define WARN(MSG, ...)
rgrover1 783:9959291e9a3f 34 #define ERR(MSG, ...)
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 46 static const unsigned ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service.
rgrover1 783:9959291e9a3f 47 static const size_t SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets
rgrover1 771:26b809199308 48
rgrover1 783:9959291e9a3f 49 static const size_t URI_DATA_MAX = 18;
rgrover1 783:9959291e9a3f 50 typedef uint8_t UriData_t[URI_DATA_MAX];
rgrover1 771:26b809199308 51
rgrover1 771:26b809199308 52 // UID Frame Type subfields
rgrover1 783:9959291e9a3f 53 static const size_t UID_NAMESPACEID_SIZE = 10;
rgrover1 783:9959291e9a3f 54 static const size_t UID_INSTANCEID_SIZE = 6;
rgrover1 783:9959291e9a3f 55 typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE];
rgrover1 783:9959291e9a3f 56 typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE];
rgrover1 771:26b809199308 57
rgrover1 771:26b809199308 58 // Eddystone Frame Type ID
rgrover1 783:9959291e9a3f 59 enum {
rgrover1 783:9959291e9a3f 60 FRAME_TYPE_UID = 0x00,
rgrover1 783:9959291e9a3f 61 FRAME_TYPE_URL = 0x10,
rgrover1 783:9959291e9a3f 62 FRAME_TYPE_TLM = 0x20,
rgrover1 783:9959291e9a3f 63 NUM_FRAMETYPES = 3
rgrover1 783:9959291e9a3f 64 };
rgrover1 783:9959291e9a3f 65
rgrover1 783:9959291e9a3f 66 static const uint8_t FRAME_SIZE_UID = 20; // includes RFU bytes
rgrover1 783:9959291e9a3f 67 static const uint8_t FRAME_SIZE_TLM = 14; // TLM frame is a constant 14Bytes
rgrover1 771:26b809199308 68
rgrover1 783:9959291e9a3f 69 public:
rgrover1 783:9959291e9a3f 70 /**
rgrover1 783:9959291e9a3f 71 * Constructor: sets up the parameters used by the Eddystone beacon.
rgrover1 783:9959291e9a3f 72 * This function should be used in lieu of the config service.
rgrover1 783:9959291e9a3f 73 *
rgrover1 783:9959291e9a3f 74 * @param bleIn
rgrover1 783:9959291e9a3f 75 * ble object for the underlying controller.
rgrover1 783:9959291e9a3f 76 * @oaram interval
rgrover1 783:9959291e9a3f 77 * This is is how often broadcasts are made (in units of milliseconds).
rgrover1 783:9959291e9a3f 78 * @param txPowerLevel
rgrover1 783:9959291e9a3f 79 * The broadcasting power level (in dbM).
rgrover1 783:9959291e9a3f 80 * @param UIDNamespaceID
rgrover1 783:9959291e9a3f 81 * 10-Byte Namespace UUID.
rgrover1 783:9959291e9a3f 82 * @param UIDInstanceID
rgrover1 783:9959291e9a3f 83 * 6-Byte Instance UUID.
rgrover1 783:9959291e9a3f 84 * @param url
rgrover1 783:9959291e9a3f 85 * Shortened URL to broadcast (pass in as a string).
rgrover1 783:9959291e9a3f 86 * @param tlmVersion
rgrover1 783:9959291e9a3f 87 * version of telemetry data field to use (default to 0x00).
rgrover1 783:9959291e9a3f 88 */
rgrover1 783:9959291e9a3f 89 EddystoneService(BLEDevice &bleIn,
rgrover1 783:9959291e9a3f 90 uint16_t interval = 100,
rgrover1 783:9959291e9a3f 91 uint8_t txPowerLevel = 0,
rgrover1 783:9959291e9a3f 92 const uint8_t *UIDNamespaceID = NULL,
rgrover1 783:9959291e9a3f 93 const uint8_t *UIDInstanceID = NULL,
rgrover1 783:9959291e9a3f 94 const char *url = NULL,
rgrover1 783:9959291e9a3f 95 uint8_t tlmVersion = 0) :
rgrover1 783:9959291e9a3f 96 ble(bleIn),
rgrover1 783:9959291e9a3f 97 uidIsSet(false),
rgrover1 783:9959291e9a3f 98 urlIsSet(false),
rgrover1 783:9959291e9a3f 99 frameIndex(0),
rgrover1 783:9959291e9a3f 100 uidRFU(0),
rgrover1 783:9959291e9a3f 101 switchFlag(true),
rgrover1 783:9959291e9a3f 102 TlmPduCount(0),
rgrover1 783:9959291e9a3f 103 TlmTimeSinceBoot(0)
rgrover1 783:9959291e9a3f 104 {
rgrover1 783:9959291e9a3f 105 // Check optional frames, set their 'isSet' flags appropriately
rgrover1 783:9959291e9a3f 106 if ((UIDNamespaceID != NULL) & (UIDInstanceID != NULL)) {
rgrover1 783:9959291e9a3f 107 setUIDFrameData(txPowerLevel, UIDNamespaceID, UIDInstanceID);
rgrover1 783:9959291e9a3f 108 uidIsSet = true;
rgrover1 783:9959291e9a3f 109 } else if (url != NULL) {
rgrover1 783:9959291e9a3f 110 setURLFrameData(txPowerLevel, url);
rgrover1 783:9959291e9a3f 111 urlIsSet = true;
rgrover1 783:9959291e9a3f 112 } else {
rgrover1 783:9959291e9a3f 113 // Default TLM frame to version 0x00, start all values at zero to be spec compliant.
rgrover1 783:9959291e9a3f 114 setTLMFrameData(tlmVersion, 0x00, 0x00);
rgrover1 783:9959291e9a3f 115 }
rgrover1 783:9959291e9a3f 116
rgrover1 783:9959291e9a3f 117 ble.gap().setTxPower(txPowerLevel);
rgrover1 783:9959291e9a3f 118 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
rgrover1 783:9959291e9a3f 119 ble.gap().setAdvertisingInterval(interval);
rgrover1 783:9959291e9a3f 120
rgrover1 783:9959291e9a3f 121 ble.gap().clearAdvertisingPayload();
rgrover1 783:9959291e9a3f 122 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 783:9959291e9a3f 123 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE));
rgrover1 783:9959291e9a3f 124
rgrover1 783:9959291e9a3f 125 // Construct TLM Frame in initial advertising.
rgrover1 783:9959291e9a3f 126 uint8_t serviceData[SERVICE_DATA_MAX];
rgrover1 783:9959291e9a3f 127 unsigned serviceDataLen = 0;
rgrover1 783:9959291e9a3f 128 serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0];
rgrover1 783:9959291e9a3f 129 serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1];
rgrover1 783:9959291e9a3f 130 serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, SERVICE_DATA_MAX);
rgrover1 783:9959291e9a3f 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen);
rgrover1 783:9959291e9a3f 132
rgrover1 783:9959291e9a3f 133 ble.gap().startAdvertising();
rgrover1 783:9959291e9a3f 134 ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback);
rgrover1 783:9959291e9a3f 135
rgrover1 783:9959291e9a3f 136 timeSinceBootTick.attach(this, &EddystoneService::tsbCallback, 0.1); // increment the TimeSinceBoot ticker every 0.1s
rgrover1 783:9959291e9a3f 137 }
rgrover1 771:26b809199308 138
rgrover1 782:5736312322d9 139 /*
rgrover1 783:9959291e9a3f 140 * Update the TLM frame battery voltage value
rgrover1 783:9959291e9a3f 141 * @param[in] voltagemv Voltage to update the TLM field battery voltage with (in mV)
rgrover1 783:9959291e9a3f 142 * @return nothing
rgrover1 783:9959291e9a3f 143 */
rgrover1 783:9959291e9a3f 144 void updateTlmBatteryVoltage(uint16_t voltagemv) {
rgrover1 783:9959291e9a3f 145 TlmBatteryVoltage = voltagemv;
rgrover1 783:9959291e9a3f 146 }
rgrover1 783:9959291e9a3f 147
rgrover1 783:9959291e9a3f 148 /*
rgrover1 783:9959291e9a3f 149 * Update the TLM frame beacon temperature
rgrover1 783:9959291e9a3f 150 * @param[in] temp Temperature of beacon (in 8.8fpn)
rgrover1 783:9959291e9a3f 151 * @return nothing
rgrover1 783:9959291e9a3f 152 */
rgrover1 783:9959291e9a3f 153 void updateTlmBeaconTemp(uint16_t temp) {
rgrover1 783:9959291e9a3f 154 TlmBeaconTemp = temp;
rgrover1 783:9959291e9a3f 155 }
rgrover1 783:9959291e9a3f 156
rgrover1 783:9959291e9a3f 157 /*
rgrover1 783:9959291e9a3f 158 * Update the TLM frame PDU Count field
rgrover1 783:9959291e9a3f 159 * @param[in] pduCount Number of Advertising frames sent since power-up
rgrover1 783:9959291e9a3f 160 */
rgrover1 783:9959291e9a3f 161 void updateTlmPduCount(uint32_t pduCount) {
rgrover1 783:9959291e9a3f 162 TlmPduCount = pduCount;
rgrover1 783:9959291e9a3f 163 }
rgrover1 783:9959291e9a3f 164
rgrover1 783:9959291e9a3f 165 /*
rgrover1 783:9959291e9a3f 166 * Update the TLM frame Time since boot in 0.1s increments
rgrover1 783:9959291e9a3f 167 * @param[in] timeSinceBoot Time since boot in 0.1s increments
rgrover1 783:9959291e9a3f 168 */
rgrover1 783:9959291e9a3f 169 void updateTlmTimeSinceBoot(uint32_t timeSinceBoot) {
rgrover1 783:9959291e9a3f 170 TlmTimeSinceBoot = timeSinceBoot;
rgrover1 783:9959291e9a3f 171 }
rgrover1 783:9959291e9a3f 172
rgrover1 783:9959291e9a3f 173 protected:
rgrover1 783:9959291e9a3f 174 /*
rgrover1 783:9959291e9a3f 175 * callback function, called every 0.1s, increments the TimeSinceBoot field in the TLM frame
rgrover1 783:9959291e9a3f 176 */
rgrover1 783:9959291e9a3f 177 virtual void tsbCallback(void) {
rgrover1 783:9959291e9a3f 178 TlmTimeSinceBoot++;
rgrover1 783:9959291e9a3f 179 }
rgrover1 783:9959291e9a3f 180
rgrover1 783:9959291e9a3f 181 private:
rgrover1 783:9959291e9a3f 182 /*
rgrover1 783:9959291e9a3f 183 * Set Eddystone UID Frame information.
rgrover1 783:9959291e9a3f 184 *
rgrover1 783:9959291e9a3f 185 * @param[in] power TX Power in dB measured at 0 meters from the device. Range of -100 to +20 dB.
rgrover1 783:9959291e9a3f 186 * @param namespaceID 10B namespace ID
rgrover1 783:9959291e9a3f 187 * @param instanceID 6B instance ID
rgrover1 783:9959291e9a3f 188 * @param RFU 2B of RFU, initialized to 0x0000 and not broadcast, included for future reference.
rgrover1 783:9959291e9a3f 189 */
rgrover1 783:9959291e9a3f 190 void setUIDFrameData(int8_t power, const UIDNamespaceID_t namespaceID, const UIDInstanceID_t instanceID, uint16_t RFU = 0x0000) {
rgrover1 783:9959291e9a3f 191 if (power > 20) {
rgrover1 782:5736312322d9 192 power = 20;
rgrover1 771:26b809199308 193 }
rgrover1 783:9959291e9a3f 194 if (power < -100) {
rgrover1 782:5736312322d9 195 power = -100;
rgrover1 782:5736312322d9 196 }
rgrover1 782:5736312322d9 197 defaultUidPower = power;
rgrover1 783:9959291e9a3f 198
rgrover1 782:5736312322d9 199 memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE);
rgrover1 782:5736312322d9 200 memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE);
rgrover1 783:9959291e9a3f 201
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 205 /**
rgrover1 783:9959291e9a3f 206 * Construct UID frame from private variables
rgrover1 783:9959291e9a3f 207 *
rgrover1 783:9959291e9a3f 208 * @param[in,out] data
rgrover1 783:9959291e9a3f 209 * pointer to array to store constructed frame in
rgrover1 783:9959291e9a3f 210 * @param[in] maxSize
rgrover1 783:9959291e9a3f 211 * number of bytes left in array, effectively how much empty space is available to write to
rgrover1 783:9959291e9a3f 212 *
rgrover1 783:9959291e9a3f 213 * @return number of bytes used. negative number indicates error message.
rgrover1 783:9959291e9a3f 214 */
rgrover1 783:9959291e9a3f 215 int constructUIDFrame(uint8_t *data, uint8_t maxSize) {
rgrover1 783:9959291e9a3f 216 if (maxSize < FRAME_SIZE_UID) {
rgrover1 783:9959291e9a3f 217 return -1;
rgrover1 783:9959291e9a3f 218 }
rgrover1 771:26b809199308 219
rgrover1 783:9959291e9a3f 220 unsigned index = 0;
rgrover1 783:9959291e9a3f 221 data[index++] = FRAME_TYPE_UID; // 1B Type
rgrover1 783:9959291e9a3f 222
rgrover1 783:9959291e9a3f 223 if (defaultUidPower > 20) { // enforce range of valid values.
rgrover1 783:9959291e9a3f 224 defaultUidPower = 20;
rgrover1 771:26b809199308 225 }
rgrover1 783:9959291e9a3f 226 if (defaultUidPower < -100) {
rgrover1 771:26b809199308 227 defaultUidPower = -100;
rgrover1 771:26b809199308 228 }
rgrover1 783:9959291e9a3f 229 data[index++] = defaultUidPower; // 1B Power @ 0meter
rgrover1 783:9959291e9a3f 230
rgrover1 783:9959291e9a3f 231 for (size_t x = 0; x < UID_NAMESPACEID_SIZE; x++) { // 10B Namespce ID
rgrover1 783:9959291e9a3f 232 data[index++] = defaultUidNamespaceID[x];
rgrover1 771:26b809199308 233 }
rgrover1 783:9959291e9a3f 234
rgrover1 783:9959291e9a3f 235 for (size_t x = 0; x< UID_INSTANCEID_SIZE; x++) { // 6B Instance ID
rgrover1 783:9959291e9a3f 236 data[index++] = defaultUidInstanceID[x];
rgrover1 771:26b809199308 237 }
rgrover1 783:9959291e9a3f 238
rgrover1 783:9959291e9a3f 239 if (uidRFU) { // 2B RFU, include if non-zero, otherwise ignore
rgrover1 783:9959291e9a3f 240 data[index++] = (uint8_t)(uidRFU >> 0);
rgrover1 783:9959291e9a3f 241 data[index++] = (uint8_t)(uidRFU >> 8);
rgrover1 771:26b809199308 242 }
rgrover1 783:9959291e9a3f 243
rgrover1 771:26b809199308 244 return index;
rgrover1 771:26b809199308 245 }
rgrover1 771:26b809199308 246
rgrover1 771:26b809199308 247 /*
rgrover1 783:9959291e9a3f 248 * Update advertising data
rgrover1 783:9959291e9a3f 249 * @return true on success, false on failure
rgrover1 783:9959291e9a3f 250 */
rgrover1 783:9959291e9a3f 251 bool updateAdvPacket(const uint8_t serviceData[], unsigned serviceDataLen) {
rgrover1 783:9959291e9a3f 252 return (ble.gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen) == BLE_ERROR_NONE);
rgrover1 783:9959291e9a3f 253 }
rgrover1 783:9959291e9a3f 254
rgrover1 783:9959291e9a3f 255 /*
rgrover1 783:9959291e9a3f 256 * Set Eddystone URL Frame information.
rgrover1 783:9959291e9a3f 257 * @param[in] power TX Power in dB measured at 0 meters from the device.
rgrover1 783:9959291e9a3f 258 * @param url URL to encode
rgrover1 783:9959291e9a3f 259 * @return false on success, true on failure.
rgrover1 783:9959291e9a3f 260 */
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 270 /**
rgrover1 783:9959291e9a3f 271 * Construct URL frame from private variables.
rgrover1 783:9959291e9a3f 272 *
rgrover1 783:9959291e9a3f 273 * @param[in,out] data
rgrover1 783:9959291e9a3f 274 * pointer to array to store constructed frame in
rgrover1 783:9959291e9a3f 275 * @param[in] maxSize
rgrover1 783:9959291e9a3f 276 * number of bytes left in array, effectively how much empty space is available to write to.
rgrover1 783:9959291e9a3f 277 *
rgrover1 783:9959291e9a3f 278 * @return number of bytes used. negative number indicates error message.
rgrover1 783:9959291e9a3f 279 */
rgrover1 783:9959291e9a3f 280 int constructURLFrame(uint8_t *data, uint8_t maxSize) {
rgrover1 783:9959291e9a3f 281 if (maxSize < (1 + 1 + defaultUriDataLength)) {
rgrover1 783:9959291e9a3f 282 return -1;
rgrover1 771:26b809199308 283 }
rgrover1 771:26b809199308 284
rgrover1 783:9959291e9a3f 285 unsigned index = 0;
rgrover1 783:9959291e9a3f 286 data[index++] = FRAME_TYPE_URL; // 1B Type
rgrover1 783:9959291e9a3f 287 data[index++] = defaultUrlPower; // 1B TX Power
rgrover1 783:9959291e9a3f 288 for (size_t x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL
rgrover1 783:9959291e9a3f 289 data[index++] = defaultUriData[x];
rgrover1 783:9959291e9a3f 290 }
rgrover1 771:26b809199308 291
rgrover1 771:26b809199308 292 return index;
rgrover1 771:26b809199308 293 }
rgrover1 771:26b809199308 294
rgrover1 771:26b809199308 295 /*
rgrover1 783:9959291e9a3f 296 * Set Eddystone TLM Frame information.
rgrover1 783:9959291e9a3f 297 * @param[in] Version of the TLM beacon data format
rgrover1 783:9959291e9a3f 298 * @param batteryVoltage in millivolts
rgrover1 783:9959291e9a3f 299 * @param beaconTemp in 8.8 floating point notation
rgrover1 783:9959291e9a3f 300 *
rgrover1 783:9959291e9a3f 301 */
rgrover1 783:9959291e9a3f 302 void setTLMFrameData(uint8_t version, uint16_t batteryVoltage, uint16_t beaconTemp, uint32_t pduCount = 0, uint32_t timeSinceBoot = 0) {
rgrover1 783:9959291e9a3f 303 TlmVersion = version;
rgrover1 783:9959291e9a3f 304 TlmBatteryVoltage = batteryVoltage;
rgrover1 783:9959291e9a3f 305 TlmBeaconTemp = beaconTemp;
rgrover1 783:9959291e9a3f 306 TlmPduCount = pduCount; // reset
rgrover1 783:9959291e9a3f 307 TlmTimeSinceBoot = timeSinceBoot; // reset
rgrover1 771:26b809199308 308 }
rgrover1 771:26b809199308 309
rgrover1 771:26b809199308 310 /*
rgrover1 783:9959291e9a3f 311 * Construct TLM frame from private variables
rgrover1 783:9959291e9a3f 312 * @param[in,out] data
rgrover1 783:9959291e9a3f 313 * pointer to array to store constructed frame in
rgrover1 783:9959291e9a3f 314 * @param[in] maxSize
rgrover1 783:9959291e9a3f 315 * number of bytes left in array, effectively how much empty space is available to write to.
rgrover1 783:9959291e9a3f 316 *
rgrover1 783:9959291e9a3f 317 * @return number of bytes used. negative number indicates error message.
rgrover1 783:9959291e9a3f 318 */
rgrover1 783:9959291e9a3f 319 int constructTLMFrame(uint8_t *data, uint8_t maxSize) {
rgrover1 783:9959291e9a3f 320 if (maxSize < FRAME_SIZE_TLM) {
rgrover1 783:9959291e9a3f 321 return -1;
rgrover1 783:9959291e9a3f 322 }
rgrover1 771:26b809199308 323
rgrover1 783:9959291e9a3f 324 unsigned index = 0;
rgrover1 783:9959291e9a3f 325 data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry
rgrover1 783:9959291e9a3f 326 data[index++] = TlmVersion; // TLM Version Number
rgrover1 783:9959291e9a3f 327 data[index++] = (uint8_t)(TlmBatteryVoltage >> 8); // Battery Voltage[0]
rgrover1 783:9959291e9a3f 328 data[index++] = (uint8_t)(TlmBatteryVoltage >> 0); // Battery Voltage[1]
rgrover1 783:9959291e9a3f 329 data[index++] = (uint8_t)(TlmBeaconTemp >> 8); // Beacon Temp[0]
rgrover1 783:9959291e9a3f 330 data[index++] = (uint8_t)(TlmBeaconTemp >> 0); // Beacon Temp[1]
rgrover1 783:9959291e9a3f 331 data[index++] = (uint8_t)(TlmPduCount >> 24); // PDU Count [0]
rgrover1 783:9959291e9a3f 332 data[index++] = (uint8_t)(TlmPduCount >> 16); // PDU Count [1]
rgrover1 783:9959291e9a3f 333 data[index++] = (uint8_t)(TlmPduCount >> 8); // PDU Count [2]
rgrover1 783:9959291e9a3f 334 data[index++] = (uint8_t)(TlmPduCount >> 0); // PDU Count [3]
rgrover1 783:9959291e9a3f 335 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 24); // Time Since Boot [0]
rgrover1 783:9959291e9a3f 336 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 16); // Time Since Boot [1]
rgrover1 783:9959291e9a3f 337 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 8); // Time Since Boot [2]
rgrover1 783:9959291e9a3f 338 data[index++] = (uint8_t)(TlmTimeSinceBoot >> 0); // Time Since Boot [3]
rgrover1 783:9959291e9a3f 339
rgrover1 783:9959291e9a3f 340 return index;
rgrover1 771:26b809199308 341 }
rgrover1 771:26b809199308 342
rgrover1 771:26b809199308 343 /*
rgrover1 783:9959291e9a3f 344 * State machine for switching out frames.
rgrover1 783:9959291e9a3f 345 * This function is called by the radioNotificationCallback when a frame needs to get swapped out.
rgrover1 783:9959291e9a3f 346 * This function exists because of time constraints in the radioNotificationCallback, so it is effectively
rgrover1 783:9959291e9a3f 347 * broken up into two functions.
rgrover1 783:9959291e9a3f 348 */
rgrover1 771:26b809199308 349 void swapOutFrames(void) {
rgrover1 771:26b809199308 350 uint8_t serviceData[SERVICE_DATA_MAX];
rgrover1 783:9959291e9a3f 351
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 358 switch (frameIndex) {
rgrover1 771:26b809199308 359 case 1:
rgrover1 771:26b809199308 360 // URL Frame
rgrover1 783:9959291e9a3f 361 if (urlIsSet) {
rgrover1 783:9959291e9a3f 362 INFO("Swapping in URL Frame: Power: %d", defaultUrlPower);
rgrover1 783:9959291e9a3f 363 serviceDataLen += constructURLFrame(serviceData + serviceDataLen, 20);
rgrover1 783:9959291e9a3f 364 DBG("\t Swapping in URL Frame: len=%d ", serviceDataLen);
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 370
rgrover1 771:26b809199308 371 case 2:
rgrover1 771:26b809199308 372 // UID Frame
rgrover1 783:9959291e9a3f 373 if (uidIsSet) {
rgrover1 783:9959291e9a3f 374 INFO("Swapping in UID Frame: Power: %d", defaultUidPower);
rgrover1 783:9959291e9a3f 375 serviceDataLen += constructUIDFrame(serviceData + serviceDataLen, 20);
rgrover1 783:9959291e9a3f 376 DBG("\t Swapping in UID Frame: len=%d", serviceDataLen);
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 382
rgrover1 771:26b809199308 383 default:
rgrover1 771:26b809199308 384 // TLM frame
rgrover1 783:9959291e9a3f 385 INFO("Swapping in TLM Frame: version=%x, Batt=%d, Temp = %d, PDUCnt = %d, TimeSinceBoot=%d",
rgrover1 783:9959291e9a3f 386 TlmVersion,
rgrover1 783:9959291e9a3f 387 TlmBatteryVoltage,
rgrover1 783:9959291e9a3f 388 TlmBeaconTemp,
rgrover1 783:9959291e9a3f 389 TlmPduCount,
rgrover1 783:9959291e9a3f 390 TlmTimeSinceBoot);
rgrover1 783:9959291e9a3f 391 serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, 20);
rgrover1 783:9959291e9a3f 392 DBG("\t Swapping in TLM Frame: len=%d", serviceDataLen);
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 400 * Callback from onRadioNotification(), used to update the PDUCounter and process next state.
rgrover1 783:9959291e9a3f 401 */
rgrover1 771:26b809199308 402 void radioNotificationCallback(bool radioActive) {
rgrover1 783:9959291e9a3f 403 static const unsigned EDDYSTONE_SWAPFRAME_DELAYMS = 1;
rgrover1 783:9959291e9a3f 404
rgrover1 771:26b809199308 405 // Update PDUCount
rgrover1 771:26b809199308 406 TlmPduCount++;
rgrover1 783:9959291e9a3f 407 frameIndex = frameIndex % NUM_FRAMETYPES;
rgrover1 771:26b809199308 408
rgrover1 783:9959291e9a3f 409 if (!radioActive) { // false just after a frame is sent
rgrover1 771:26b809199308 410 // state machine to control which packet is being sent
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 416
rgrover1 771:26b809199308 417 case 1: // URL Frame
rgrover1 771:26b809199308 418 // switch out packets
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 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 783:9959291e9a3f 428
rgrover1 771:26b809199308 429 case 2: // UIDFrame
rgrover1 771:26b809199308 430 // switch out packets
rgrover1 783:9959291e9a3f 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 783:9959291e9a3f 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 783:9959291e9a3f 445 BLEDevice &ble;
rgrover1 783:9959291e9a3f 446 Ticker timeSinceBootTick;
rgrover1 783:9959291e9a3f 447 Timeout switchFrame;
rgrover1 783:9959291e9a3f 448 // Default value that is restored on reset
rgrover1 783:9959291e9a3f 449 size_t defaultUriDataLength;
rgrover1 783:9959291e9a3f 450 UriData_t defaultUriData;
rgrover1 783:9959291e9a3f 451 UIDNamespaceID_t defaultUidNamespaceID;
rgrover1 783:9959291e9a3f 452 UIDInstanceID_t defaultUidInstanceID;
rgrover1 783:9959291e9a3f 453 int8_t defaultUidPower;
rgrover1 783:9959291e9a3f 454 int8_t defaultUrlPower;
rgrover1 783:9959291e9a3f 455 unsigned frameIndex;
rgrover1 783:9959291e9a3f 456 uint16_t uidRFU;
rgrover1 783:9959291e9a3f 457 bool uidIsSet;
rgrover1 783:9959291e9a3f 458 bool urlIsSet;
rgrover1 783:9959291e9a3f 459 bool switchFlag;
rgrover1 771:26b809199308 460
rgrover1 783:9959291e9a3f 461 // Private Variables for Telemetry Data
rgrover1 783:9959291e9a3f 462 uint8_t TlmVersion;
rgrover1 783:9959291e9a3f 463 volatile uint16_t TlmBatteryVoltage;
rgrover1 783:9959291e9a3f 464 volatile uint16_t TlmBeaconTemp;
rgrover1 783:9959291e9a3f 465 volatile uint32_t TlmPduCount;
rgrover1 783:9959291e9a3f 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_