wersion 1.9

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Tue Aug 11 15:13:13 2015 +0100
Revision:
785:c74f567162d4
Parent:
784:1e817a72a166
Child:
786:d6d7087d8377
Synchronized with git rev 99c72530
Author: Jean-Philippe Brucker
Add connection handle in Gatt callback parameters

This commit paves the way for multiple device implementation:
read/write/HVX callbacks will be able to distinguish between
peripherals by comparing per-device connection handles.

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