jgh
Fork of BLE_API by
ble/services/EddystoneService.h@841:468a5863cacb, 2015-11-02 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Nov 02 09:09:04 2015 +0000
- Revision:
- 841:468a5863cacb
- Parent:
- 832:5dac246f8f02
- Child:
- 842:4f7f7a4b0cb2
Synchronized with git rev 18dcc913
Author: Andres Amaya Garcia
Introduced fix for defect IOTSFW-1058
Introduced a fix for defect IOTSFW-1058 that caused the
BLE_EddystoneBeaconConfigService example in ARMmbed/ble-examples repo to fail.
Refer to JIRA defect for more details.
Who changed what in which revision?
User | Revision | Line number | New 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 | 830:a6ef72e0697a | 22 | #include "CircularBuffer.h" |
rgrover1 | 771:26b809199308 | 23 | static const uint8_t BEACON_EDDYSTONE[] = {0xAA, 0xFE}; |
rgrover1 | 771:26b809199308 | 24 | |
rgrover1 | 786:d6d7087d8377 | 25 | //Debug is disabled by default |
rgrover1 | 771:26b809199308 | 26 | #if 0 |
rgrover1 | 832:5dac246f8f02 | 27 | #define DBG(MSG, ...) printf("[EddyStone: DBG]" MSG " \t[%s,%d]\r\n", \ |
rgrover1 | 832:5dac246f8f02 | 28 | ## __VA_ARGS__, \ |
rgrover1 | 832:5dac246f8f02 | 29 | __FILE__, \ |
rgrover1 | 832:5dac246f8f02 | 30 | __LINE__); |
rgrover1 | 832:5dac246f8f02 | 31 | #define WARN(MSG, ...) printf("[EddyStone: WARN]" MSG " \t[%s,%d]\r\n", \ |
rgrover1 | 832:5dac246f8f02 | 32 | ## __VA_ARGS__, \ |
rgrover1 | 832:5dac246f8f02 | 33 | __FILE__, \ |
rgrover1 | 832:5dac246f8f02 | 34 | __LINE__); |
rgrover1 | 832:5dac246f8f02 | 35 | #define ERR(MSG, ...) printf("[EddyStone: ERR]" MSG " \t[%s,%d]\r\n", \ |
rgrover1 | 832:5dac246f8f02 | 36 | ## __VA_ARGS__, \ |
rgrover1 | 832:5dac246f8f02 | 37 | __FILE__, \ |
rgrover1 | 832:5dac246f8f02 | 38 | __LINE__); |
rgrover1 | 832:5dac246f8f02 | 39 | #else // if 0 |
rgrover1 | 786:d6d7087d8377 | 40 | #define DBG(x, ...) //wait_us(10); |
rgrover1 | 786:d6d7087d8377 | 41 | #define WARN(x, ...) //wait_us(10); |
rgrover1 | 786:d6d7087d8377 | 42 | #define ERR(x, ...) |
rgrover1 | 832:5dac246f8f02 | 43 | #endif // if 0 |
rgrover1 | 786:d6d7087d8377 | 44 | |
rgrover1 | 786:d6d7087d8377 | 45 | #if 0 |
rgrover1 | 832:5dac246f8f02 | 46 | #define INFO(x, ...) printf("[EddyStone: INFO]"x " \t[%s,%d]\r\n", \ |
rgrover1 | 832:5dac246f8f02 | 47 | ## __VA_ARGS__, \ |
rgrover1 | 832:5dac246f8f02 | 48 | __FILE__, \ |
rgrover1 | 832:5dac246f8f02 | 49 | __LINE__); |
rgrover1 | 832:5dac246f8f02 | 50 | #else // if 0 |
rgrover1 | 786:d6d7087d8377 | 51 | #define INFO(x, ...) |
rgrover1 | 832:5dac246f8f02 | 52 | #endif // if 0 |
rgrover1 | 771:26b809199308 | 53 | |
rgrover1 | 771:26b809199308 | 54 | /** |
rgrover1 | 771:26b809199308 | 55 | * @class Eddystone |
rgrover1 | 771:26b809199308 | 56 | * @brief Eddystone Configuration Service. Can be used to set URL, adjust power levels, and set flags. |
rgrover1 | 771:26b809199308 | 57 | * See https://github.com/google/eddystone |
rgrover1 | 771:26b809199308 | 58 | * |
rgrover1 | 771:26b809199308 | 59 | */ |
rgrover1 | 771:26b809199308 | 60 | class EddystoneService |
rgrover1 | 771:26b809199308 | 61 | { |
rgrover1 | 771:26b809199308 | 62 | public: |
rgrover1 | 830:a6ef72e0697a | 63 | enum FrameTypes { |
rgrover1 | 830:a6ef72e0697a | 64 | NONE, |
rgrover1 | 830:a6ef72e0697a | 65 | url, |
rgrover1 | 830:a6ef72e0697a | 66 | uid, |
rgrover1 | 830:a6ef72e0697a | 67 | tlm |
rgrover1 | 830:a6ef72e0697a | 68 | }; |
rgrover1 | 786:d6d7087d8377 | 69 | |
rgrover1 | 786:d6d7087d8377 | 70 | static const int SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets |
rgrover1 | 771:26b809199308 | 71 | |
rgrover1 | 786:d6d7087d8377 | 72 | // There are currently 3 subframes defined, URI, UID, and TLM |
rgrover1 | 786:d6d7087d8377 | 73 | #define EDDYSTONE_MAX_FRAMETYPE 3 |
rgrover1 | 786:d6d7087d8377 | 74 | void (*frames[EDDYSTONE_MAX_FRAMETYPE])(uint8_t *, uint32_t); |
rgrover1 | 786:d6d7087d8377 | 75 | static const int URI_DATA_MAX = 18; |
rgrover1 | 786:d6d7087d8377 | 76 | typedef uint8_t UriData_t[URI_DATA_MAX]; |
rgrover1 | 832:5dac246f8f02 | 77 | CircularBuffer<FrameTypes, EDDYSTONE_MAX_FRAMETYPE> overflow; |
rgrover1 | 771:26b809199308 | 78 | |
rgrover1 | 771:26b809199308 | 79 | // UID Frame Type subfields |
rgrover1 | 786:d6d7087d8377 | 80 | static const int UID_NAMESPACEID_SIZE = 10; |
rgrover1 | 786:d6d7087d8377 | 81 | typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE]; |
rgrover1 | 786:d6d7087d8377 | 82 | static const int UID_INSTANCEID_SIZE = 6; |
rgrover1 | 786:d6d7087d8377 | 83 | typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE]; |
rgrover1 | 771:26b809199308 | 84 | |
rgrover1 | 771:26b809199308 | 85 | // Eddystone Frame Type ID |
rgrover1 | 786:d6d7087d8377 | 86 | static const uint8_t FRAME_TYPE_UID = 0x00; |
rgrover1 | 786:d6d7087d8377 | 87 | static const uint8_t FRAME_TYPE_URL = 0x10; |
rgrover1 | 786:d6d7087d8377 | 88 | static const uint8_t FRAME_TYPE_TLM = 0x20; |
rgrover1 | 785:c74f567162d4 | 89 | |
rgrover1 | 786:d6d7087d8377 | 90 | static const uint8_t FRAME_SIZE_TLM = 14; // TLM frame is a constant 14Bytes |
rgrover1 | 785:c74f567162d4 | 91 | static const uint8_t FRAME_SIZE_UID = 20; // includes RFU bytes |
rgrover1 | 783:9959291e9a3f | 92 | |
rgrover1 | 832:5dac246f8f02 | 93 | /** |
rgrover1 | 832:5dac246f8f02 | 94 | * Set Eddystone UID Frame information. |
rgrover1 | 832:5dac246f8f02 | 95 | * |
rgrover1 | 832:5dac246f8f02 | 96 | * @param[in] power TX Power in dB measured at 0 meters from the device. Range of -100 to +20 dB. |
rgrover1 | 832:5dac246f8f02 | 97 | * @param[in] namespaceID 10B namespace ID |
rgrover1 | 832:5dac246f8f02 | 98 | * @param[in] instanceID 6B instance ID |
rgrover1 | 832:5dac246f8f02 | 99 | * @param[in] RFU 2B of RFU, initialized to 0x0000 and not broadcast, included for future reference. |
rgrover1 | 832:5dac246f8f02 | 100 | */ |
rgrover1 | 832:5dac246f8f02 | 101 | void setUIDFrameData(int8_t power, |
rgrover1 | 832:5dac246f8f02 | 102 | UIDNamespaceID_t namespaceID, |
rgrover1 | 832:5dac246f8f02 | 103 | UIDInstanceID_t instanceID, |
rgrover1 | 832:5dac246f8f02 | 104 | uint32_t uidAdvPeriodIn, |
rgrover1 | 832:5dac246f8f02 | 105 | uint16_t RFU = 0x0000) { |
rgrover1 | 832:5dac246f8f02 | 106 | if (0 == uidAdvPeriodIn) { |
rgrover1 | 830:a6ef72e0697a | 107 | uidIsSet = false; |
rgrover1 | 830:a6ef72e0697a | 108 | return; |
rgrover1 | 830:a6ef72e0697a | 109 | } |
rgrover1 | 832:5dac246f8f02 | 110 | if (power > 20) { |
rgrover1 | 786:d6d7087d8377 | 111 | power = 20; |
rgrover1 | 785:c74f567162d4 | 112 | } |
rgrover1 | 832:5dac246f8f02 | 113 | if (power < -100) { |
rgrover1 | 786:d6d7087d8377 | 114 | power = -100; |
rgrover1 | 786:d6d7087d8377 | 115 | } |
rgrover1 | 832:5dac246f8f02 | 116 | |
rgrover1 | 786:d6d7087d8377 | 117 | defaultUidPower = power; |
rgrover1 | 786:d6d7087d8377 | 118 | memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE); |
rgrover1 | 786:d6d7087d8377 | 119 | memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE); |
rgrover1 | 832:5dac246f8f02 | 120 | uidRFU = (uint16_t)RFU; // this is probably bad form, but it doesn't really matter yet. |
rgrover1 | 830:a6ef72e0697a | 121 | uidAdvPeriod = uidAdvPeriodIn; |
rgrover1 | 832:5dac246f8f02 | 122 | uidIsSet = true; // set toggle to advertise UID frames |
rgrover1 | 785:c74f567162d4 | 123 | } |
rgrover1 | 771:26b809199308 | 124 | |
rgrover1 | 784:1e817a72a166 | 125 | /* |
rgrover1 | 786:d6d7087d8377 | 126 | * Construct UID frame from private variables |
rgrover1 | 786:d6d7087d8377 | 127 | * @param[in/out] Data pointer to array to store constructed frame in |
rgrover1 | 832:5dac246f8f02 | 128 | * @param[in] maxSize number of bytes left in array, effectively how much empty space is available to write to |
rgrover1 | 786:d6d7087d8377 | 129 | * @return number of bytes used. negative number indicates error message. |
rgrover1 | 786:d6d7087d8377 | 130 | */ |
rgrover1 | 832:5dac246f8f02 | 131 | unsigned constructUIDFrame(uint8_t *Data, uint8_t maxSize) { |
rgrover1 | 832:5dac246f8f02 | 132 | unsigned index = 0; |
rgrover1 | 785:c74f567162d4 | 133 | |
rgrover1 | 786:d6d7087d8377 | 134 | Data[index++] = FRAME_TYPE_UID; // 1B Type |
rgrover1 | 832:5dac246f8f02 | 135 | |
rgrover1 | 832:5dac246f8f02 | 136 | if (defaultUidPower > 20) { |
rgrover1 | 832:5dac246f8f02 | 137 | defaultUidPower = 20; // enforce range of vaild values. |
rgrover1 | 784:1e817a72a166 | 138 | } |
rgrover1 | 832:5dac246f8f02 | 139 | if (defaultUidPower < -100) { |
rgrover1 | 771:26b809199308 | 140 | defaultUidPower = -100; |
rgrover1 | 771:26b809199308 | 141 | } |
rgrover1 | 786:d6d7087d8377 | 142 | Data[index++] = defaultUidPower; // 1B Power @ 0meter |
rgrover1 | 832:5dac246f8f02 | 143 | |
rgrover1 | 830:a6ef72e0697a | 144 | DBG("UID NamespaceID = '0x"); |
rgrover1 | 832:5dac246f8f02 | 145 | for (size_t x = 0; x < UID_NAMESPACEID_SIZE; x++) { // 10B Namespace ID |
rgrover1 | 786:d6d7087d8377 | 146 | Data[index++] = defaultUidNamespaceID[x]; |
rgrover1 | 832:5dac246f8f02 | 147 | DBG("%x,", defaultUidNamespaceID[x]); |
rgrover1 | 771:26b809199308 | 148 | } |
rgrover1 | 830:a6ef72e0697a | 149 | DBG("'\r\n"); |
rgrover1 | 832:5dac246f8f02 | 150 | |
rgrover1 | 832:5dac246f8f02 | 151 | DBG("UID InstanceID = '0x"); |
rgrover1 | 832:5dac246f8f02 | 152 | for (size_t x = 0; x< UID_INSTANCEID_SIZE; x++) { // 6B Instance ID |
rgrover1 | 786:d6d7087d8377 | 153 | Data[index++] = defaultUidInstanceID[x]; |
rgrover1 | 832:5dac246f8f02 | 154 | DBG("%x,", defaultUidInstanceID[x]); |
rgrover1 | 771:26b809199308 | 155 | } |
rgrover1 | 830:a6ef72e0697a | 156 | DBG("'\r\n"); |
rgrover1 | 832:5dac246f8f02 | 157 | |
rgrover1 | 832:5dac246f8f02 | 158 | if (0 != uidRFU) { // 2B RFU, include if non-zero, otherwise ignore |
rgrover1 | 786:d6d7087d8377 | 159 | Data[index++] = (uint8_t)(uidRFU >> 0); |
rgrover1 | 786:d6d7087d8377 | 160 | Data[index++] = (uint8_t)(uidRFU >> 8); |
rgrover1 | 771:26b809199308 | 161 | } |
rgrover1 | 832:5dac246f8f02 | 162 | DBG("construcUIDFrame %d, %d", maxSize, index); |
rgrover1 | 771:26b809199308 | 163 | return index; |
rgrover1 | 771:26b809199308 | 164 | } |
rgrover1 | 771:26b809199308 | 165 | |
rgrover1 | 832:5dac246f8f02 | 166 | /** |
rgrover1 | 832:5dac246f8f02 | 167 | * Set Eddystone URL Frame information. |
rgrover1 | 832:5dac246f8f02 | 168 | * @param[in] power TX Power in dB measured at 0 meters from the device. |
rgrover1 | 832:5dac246f8f02 | 169 | * @param[in] url URL to encode |
rgrover1 | 832:5dac246f8f02 | 170 | * @param[in] urlAdvPeriodIn How long to advertise the URL frame (measured in # of adv periods) |
rgrover1 | 832:5dac246f8f02 | 171 | * @return false on success, true on failure. |
rgrover1 | 832:5dac246f8f02 | 172 | */ |
rgrover1 | 832:5dac246f8f02 | 173 | bool setURLFrameData(int8_t power, const char *urlIn, uint32_t urlAdvPeriodIn) { |
rgrover1 | 832:5dac246f8f02 | 174 | if (0 == urlAdvPeriodIn) { |
rgrover1 | 830:a6ef72e0697a | 175 | urlIsSet = false; |
rgrover1 | 830:a6ef72e0697a | 176 | return false; |
rgrover1 | 830:a6ef72e0697a | 177 | } |
rgrover1 | 771:26b809199308 | 178 | defaultUrlPower = power; |
rgrover1 | 830:a6ef72e0697a | 179 | encodeURL(urlIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting |
rgrover1 | 771:26b809199308 | 180 | if (defaultUriDataLength > URI_DATA_MAX) { |
rgrover1 | 832:5dac246f8f02 | 181 | return true; // error, URL is too big |
rgrover1 | 771:26b809199308 | 182 | } |
rgrover1 | 830:a6ef72e0697a | 183 | urlAdvPeriod = urlAdvPeriodIn; |
rgrover1 | 832:5dac246f8f02 | 184 | urlIsSet = true; |
rgrover1 | 771:26b809199308 | 185 | return false; |
rgrover1 | 771:26b809199308 | 186 | } |
rgrover1 | 771:26b809199308 | 187 | |
rgrover1 | 841:468a5863cacb | 188 | /** |
rgrover1 | 841:468a5863cacb | 189 | * Set Eddystone URL Frame information. |
rgrover1 | 841:468a5863cacb | 190 | * @param[in] power TX Power in dB measured at 0 meters from the device. |
rgrover1 | 841:468a5863cacb | 191 | * @param[in] url Encoded URL |
rgrover1 | 841:468a5863cacb | 192 | * @param[in] urlAdvPeriodIn How long to advertise the URL frame (measured in # of adv periods) |
rgrover1 | 841:468a5863cacb | 193 | * @return false on success, true on failure. |
rgrover1 | 841:468a5863cacb | 194 | */ |
rgrover1 | 841:468a5863cacb | 195 | bool setURLFrameEncodedData(int8_t power, const char *encodedUrlIn, uint8_t encodedUriInLength, uint32_t urlAdvPeriodIn) { |
rgrover1 | 841:468a5863cacb | 196 | if (0 == urlAdvPeriodIn) { |
rgrover1 | 841:468a5863cacb | 197 | urlIsSet = false; |
rgrover1 | 841:468a5863cacb | 198 | return false; |
rgrover1 | 841:468a5863cacb | 199 | } |
rgrover1 | 841:468a5863cacb | 200 | defaultUrlPower = power; |
rgrover1 | 841:468a5863cacb | 201 | memcpy(defaultUriData, encodedUrlIn, URI_DATA_MAX); |
rgrover1 | 841:468a5863cacb | 202 | defaultUriDataLength = encodedUriInLength; |
rgrover1 | 841:468a5863cacb | 203 | if (defaultUriDataLength > URI_DATA_MAX) { |
rgrover1 | 841:468a5863cacb | 204 | return true; // error, URL is too big |
rgrover1 | 841:468a5863cacb | 205 | } |
rgrover1 | 841:468a5863cacb | 206 | urlAdvPeriod = urlAdvPeriodIn; |
rgrover1 | 841:468a5863cacb | 207 | urlIsSet = true; |
rgrover1 | 841:468a5863cacb | 208 | return false; |
rgrover1 | 841:468a5863cacb | 209 | } |
rgrover1 | 841:468a5863cacb | 210 | |
rgrover1 | 786:d6d7087d8377 | 211 | /* |
rgrover1 | 786:d6d7087d8377 | 212 | * Construct URL frame from private variables |
rgrover1 | 786:d6d7087d8377 | 213 | * @param[in/out] Data pointer to array to store constructed frame in |
rgrover1 | 786:d6d7087d8377 | 214 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
rgrover1 | 786:d6d7087d8377 | 215 | * @return number of bytes used. negative number indicates error message. |
rgrover1 | 786:d6d7087d8377 | 216 | */ |
rgrover1 | 832:5dac246f8f02 | 217 | int constructURLFrame(uint8_t *Data, uint8_t maxSize) { |
rgrover1 | 786:d6d7087d8377 | 218 | int index = 0; |
rgrover1 | 786:d6d7087d8377 | 219 | Data[index++] = FRAME_TYPE_URL; // 1B Type |
rgrover1 | 786:d6d7087d8377 | 220 | Data[index++] = defaultUrlPower; // 1B TX Power |
rgrover1 | 832:5dac246f8f02 | 221 | for (int x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL |
rgrover1 | 786:d6d7087d8377 | 222 | Data[index++] = defaultUriData[x]; |
rgrover1 | 771:26b809199308 | 223 | } |
rgrover1 | 832:5dac246f8f02 | 224 | DBG("constructURLFrame: %d, %d", maxSize, index); |
rgrover1 | 786:d6d7087d8377 | 225 | return index; |
rgrover1 | 786:d6d7087d8377 | 226 | } |
rgrover1 | 771:26b809199308 | 227 | |
rgrover1 | 786:d6d7087d8377 | 228 | /* |
rgrover1 | 786:d6d7087d8377 | 229 | * Set Eddystone TLM Frame information. |
rgrover1 | 786:d6d7087d8377 | 230 | * @param[in] Version of the TLM beacon data format |
rgrover1 | 830:a6ef72e0697a | 231 | * @param[in] advPeriod how often to advertise the TLM frame for (in minutes) |
rgrover1 | 786:d6d7087d8377 | 232 | * @param batteryVoltage in milivolts |
rgrover1 | 786:d6d7087d8377 | 233 | * @param beaconTemp in 8.8 floating point notation |
rgrover1 | 786:d6d7087d8377 | 234 | * |
rgrover1 | 786:d6d7087d8377 | 235 | */ |
rgrover1 | 832:5dac246f8f02 | 236 | void setTLMFrameData(uint8_t version = 0, |
rgrover1 | 832:5dac246f8f02 | 237 | uint32_t advPeriod = 60, |
rgrover1 | 832:5dac246f8f02 | 238 | uint16_t batteryVoltage = 0, |
rgrover1 | 832:5dac246f8f02 | 239 | uint16_t beaconTemp = 0, |
rgrover1 | 832:5dac246f8f02 | 240 | uint32_t pduCount = 0, |
rgrover1 | 832:5dac246f8f02 | 241 | uint32_t timeSinceBoot = 0) { |
rgrover1 | 832:5dac246f8f02 | 242 | if (0 == advPeriod) { |
rgrover1 | 830:a6ef72e0697a | 243 | tlmIsSet = false; |
rgrover1 | 830:a6ef72e0697a | 244 | return; |
rgrover1 | 830:a6ef72e0697a | 245 | } |
rgrover1 | 832:5dac246f8f02 | 246 | TlmVersion = version; |
rgrover1 | 786:d6d7087d8377 | 247 | TlmBatteryVoltage = batteryVoltage; |
rgrover1 | 832:5dac246f8f02 | 248 | TlmBeaconTemp = beaconTemp; |
rgrover1 | 832:5dac246f8f02 | 249 | TlmPduCount = pduCount; // reset |
rgrover1 | 832:5dac246f8f02 | 250 | TlmTimeSinceBoot = timeSinceBoot; // reset |
rgrover1 | 832:5dac246f8f02 | 251 | TlmAdvPeriod = advPeriod; |
rgrover1 | 832:5dac246f8f02 | 252 | tlmIsSet = true; // TLM Data has been enabled |
rgrover1 | 786:d6d7087d8377 | 253 | } |
rgrover1 | 771:26b809199308 | 254 | |
rgrover1 | 786:d6d7087d8377 | 255 | /* |
rgrover1 | 786:d6d7087d8377 | 256 | * Construct TLM frame from private variables |
rgrover1 | 786:d6d7087d8377 | 257 | * @param[in/out] Data pointer to array to store constructed frame in |
rgrover1 | 786:d6d7087d8377 | 258 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
rgrover1 | 786:d6d7087d8377 | 259 | * @return number of bytes used. negative number indicates error message. |
rgrover1 | 786:d6d7087d8377 | 260 | */ |
rgrover1 | 832:5dac246f8f02 | 261 | int constructTLMFrame(uint8_t *Data, uint8_t maxSize) { |
rgrover1 | 786:d6d7087d8377 | 262 | int index = 0; |
rgrover1 | 786:d6d7087d8377 | 263 | Data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry |
rgrover1 | 786:d6d7087d8377 | 264 | Data[index++] = TlmVersion; // TLM Version Number |
rgrover1 | 832:5dac246f8f02 | 265 | Data[index++] = (uint8_t)(TlmBatteryVoltage >> 8); // Battery Voltage[0] |
rgrover1 | 832:5dac246f8f02 | 266 | Data[index++] = (uint8_t)(TlmBatteryVoltage >> 0); // Battery Voltage[1] |
rgrover1 | 832:5dac246f8f02 | 267 | Data[index++] = (uint8_t)(TlmBeaconTemp >> 8); // Beacon Temp[0] |
rgrover1 | 832:5dac246f8f02 | 268 | Data[index++] = (uint8_t)(TlmBeaconTemp >> 0); // Beacon Temp[1] |
rgrover1 | 832:5dac246f8f02 | 269 | Data[index++] = (uint8_t)(TlmPduCount >> 24); // PDU Count [0] |
rgrover1 | 832:5dac246f8f02 | 270 | Data[index++] = (uint8_t)(TlmPduCount >> 16); // PDU Count [1] |
rgrover1 | 832:5dac246f8f02 | 271 | Data[index++] = (uint8_t)(TlmPduCount >> 8); // PDU Count [2] |
rgrover1 | 832:5dac246f8f02 | 272 | Data[index++] = (uint8_t)(TlmPduCount >> 0); // PDU Count [3] |
rgrover1 | 832:5dac246f8f02 | 273 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 24); // Time Since Boot [0] |
rgrover1 | 832:5dac246f8f02 | 274 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 16); // Time Since Boot [1] |
rgrover1 | 832:5dac246f8f02 | 275 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 8); // Time Since Boot [2] |
rgrover1 | 832:5dac246f8f02 | 276 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 0); // Time Since Boot [3] |
rgrover1 | 832:5dac246f8f02 | 277 | DBG("constructURLFrame: %d, %d", maxSize, index); |
rgrover1 | 771:26b809199308 | 278 | return index; |
rgrover1 | 771:26b809199308 | 279 | } |
rgrover1 | 771:26b809199308 | 280 | |
rgrover1 | 771:26b809199308 | 281 | /* |
rgrover1 | 786:d6d7087d8377 | 282 | * Update the TLM frame battery voltage value |
rgrover1 | 786:d6d7087d8377 | 283 | * @param[in] voltagemv Voltage to update the TLM field battery voltage with (in mV) |
rgrover1 | 786:d6d7087d8377 | 284 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 285 | */ |
rgrover1 | 786:d6d7087d8377 | 286 | void updateTlmBatteryVoltage(uint16_t voltagemv) { |
rgrover1 | 786:d6d7087d8377 | 287 | TlmBatteryVoltage = voltagemv; |
rgrover1 | 786:d6d7087d8377 | 288 | } |
rgrover1 | 786:d6d7087d8377 | 289 | |
rgrover1 | 786:d6d7087d8377 | 290 | /* |
rgrover1 | 786:d6d7087d8377 | 291 | * Update the TLM frame beacon temperature |
rgrover1 | 786:d6d7087d8377 | 292 | * @param[in] temp Temperature of beacon (in 8.8fpn) |
rgrover1 | 786:d6d7087d8377 | 293 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 294 | */ |
rgrover1 | 786:d6d7087d8377 | 295 | void updateTlmBeaconTemp(uint16_t temp) { |
rgrover1 | 786:d6d7087d8377 | 296 | TlmBeaconTemp = temp; |
rgrover1 | 786:d6d7087d8377 | 297 | } |
rgrover1 | 786:d6d7087d8377 | 298 | |
rgrover1 | 786:d6d7087d8377 | 299 | /* |
rgrover1 | 786:d6d7087d8377 | 300 | * Update the TLM frame PDU Count field |
rgrover1 | 786:d6d7087d8377 | 301 | * @param[in] pduCount Number of Advertisiting frames sent since powerup |
rgrover1 | 786:d6d7087d8377 | 302 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 303 | */ |
rgrover1 | 786:d6d7087d8377 | 304 | void updateTlmPduCount(uint32_t pduCount) { |
rgrover1 | 786:d6d7087d8377 | 305 | TlmPduCount = pduCount; |
rgrover1 | 771:26b809199308 | 306 | } |
rgrover1 | 771:26b809199308 | 307 | |
rgrover1 | 771:26b809199308 | 308 | /* |
rgrover1 | 786:d6d7087d8377 | 309 | * Update the TLM frame Time since boot in 0.1s incriments |
rgrover1 | 786:d6d7087d8377 | 310 | * @param[in] timeSinceBoot Time since boot in 0.1s incriments |
rgrover1 | 786:d6d7087d8377 | 311 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 312 | */ |
rgrover1 | 786:d6d7087d8377 | 313 | void updateTlmTimeSinceBoot(uint32_t timeSinceBoot) { |
rgrover1 | 786:d6d7087d8377 | 314 | TlmTimeSinceBoot = timeSinceBoot; |
rgrover1 | 786:d6d7087d8377 | 315 | } |
rgrover1 | 771:26b809199308 | 316 | |
rgrover1 | 786:d6d7087d8377 | 317 | /* |
rgrover1 | 786:d6d7087d8377 | 318 | * callback function, called every 0.1s, incriments the TimeSinceBoot field in the TLM frame |
rgrover1 | 786:d6d7087d8377 | 319 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 320 | */ |
rgrover1 | 786:d6d7087d8377 | 321 | void tsbCallback(void) { |
rgrover1 | 786:d6d7087d8377 | 322 | TlmTimeSinceBoot++; |
rgrover1 | 771:26b809199308 | 323 | } |
rgrover1 | 771:26b809199308 | 324 | |
rgrover1 | 771:26b809199308 | 325 | /* |
rgrover1 | 786:d6d7087d8377 | 326 | * Update advertising data |
rgrover1 | 786:d6d7087d8377 | 327 | * @return true on success, false on failure |
rgrover1 | 786:d6d7087d8377 | 328 | */ |
rgrover1 | 786:d6d7087d8377 | 329 | bool updateAdvPacket(uint8_t serviceData[], unsigned serviceDataLen) { |
rgrover1 | 786:d6d7087d8377 | 330 | // Fields from the Service |
rgrover1 | 786:d6d7087d8377 | 331 | DBG("Updating AdvFrame: %d", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 332 | |
rgrover1 | 786:d6d7087d8377 | 333 | ble.clearAdvertisingPayload(); |
rgrover1 | 830:a6ef72e0697a | 334 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
rgrover1 | 830:a6ef72e0697a | 335 | ble.setAdvertisingInterval(100); |
rgrover1 | 786:d6d7087d8377 | 336 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 786:d6d7087d8377 | 337 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE)); |
rgrover1 | 786:d6d7087d8377 | 338 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen); |
rgrover1 | 786:d6d7087d8377 | 339 | |
rgrover1 | 830:a6ef72e0697a | 340 | |
rgrover1 | 786:d6d7087d8377 | 341 | return true; |
rgrover1 | 786:d6d7087d8377 | 342 | } |
rgrover1 | 786:d6d7087d8377 | 343 | |
rgrover1 | 786:d6d7087d8377 | 344 | /* |
rgrover1 | 786:d6d7087d8377 | 345 | * State machine for switching out frames. |
rgrover1 | 786:d6d7087d8377 | 346 | * This function is called by the radioNotificationCallback when a frame needs to get swapped out. |
rgrover1 | 786:d6d7087d8377 | 347 | * This function exists because of time constraints in the radioNotificationCallback, so it is effectively |
rgrover1 | 786:d6d7087d8377 | 348 | * broken up into two functions. |
rgrover1 | 786:d6d7087d8377 | 349 | */ |
rgrover1 | 830:a6ef72e0697a | 350 | void swapOutFrames(FrameTypes frameType) { |
rgrover1 | 832:5dac246f8f02 | 351 | uint8_t serviceData[SERVICE_DATA_MAX]; |
rgrover1 | 771:26b809199308 | 352 | unsigned serviceDataLen = 0; |
rgrover1 | 786:d6d7087d8377 | 353 | //hard code in the eddystone UUID |
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 | 832:5dac246f8f02 | 358 | switch (frameType) { |
rgrover1 | 830:a6ef72e0697a | 359 | case tlm: |
rgrover1 | 830:a6ef72e0697a | 360 | // TLM frame |
rgrover1 | 832:5dac246f8f02 | 361 | if (tlmIsSet) { |
rgrover1 | 832:5dac246f8f02 | 362 | DBG("Swapping in TLM Frame: version=%x, Batt=%d, Temp = %d, PDUCnt = %d, TimeSinceBoot=%d", |
rgrover1 | 832:5dac246f8f02 | 363 | TlmVersion, |
rgrover1 | 832:5dac246f8f02 | 364 | TlmBatteryVoltage, |
rgrover1 | 832:5dac246f8f02 | 365 | TlmBeaconTemp, |
rgrover1 | 832:5dac246f8f02 | 366 | TlmPduCount, |
rgrover1 | 832:5dac246f8f02 | 367 | TlmTimeSinceBoot); |
rgrover1 | 832:5dac246f8f02 | 368 | serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, 20); |
rgrover1 | 832:5dac246f8f02 | 369 | DBG("\t Swapping in TLM Frame: len=%d", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 370 | updateAdvPacket(serviceData, serviceDataLen); |
rgrover1 | 830:a6ef72e0697a | 371 | } |
rgrover1 | 830:a6ef72e0697a | 372 | break; |
rgrover1 | 830:a6ef72e0697a | 373 | case url: |
rgrover1 | 771:26b809199308 | 374 | // URL Frame |
rgrover1 | 832:5dac246f8f02 | 375 | if (urlIsSet) { |
rgrover1 | 832:5dac246f8f02 | 376 | DBG("Swapping in URL Frame: Power: %d", defaultUrlPower); |
rgrover1 | 832:5dac246f8f02 | 377 | serviceDataLen += constructURLFrame(serviceData + serviceDataLen, 20); |
rgrover1 | 832:5dac246f8f02 | 378 | DBG("\t Swapping in URL Frame: len=%d ", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 379 | updateAdvPacket(serviceData, serviceDataLen); |
rgrover1 | 830:a6ef72e0697a | 380 | //switchFlag = false; |
rgrover1 | 771:26b809199308 | 381 | } |
rgrover1 | 830:a6ef72e0697a | 382 | break; |
rgrover1 | 830:a6ef72e0697a | 383 | case uid: |
rgrover1 | 771:26b809199308 | 384 | // UID Frame |
rgrover1 | 832:5dac246f8f02 | 385 | if (uidIsSet) { |
rgrover1 | 832:5dac246f8f02 | 386 | DBG("Swapping in UID Frame: Power: %d", defaultUidPower); |
rgrover1 | 832:5dac246f8f02 | 387 | serviceDataLen += constructUIDFrame(serviceData + serviceDataLen, 20); |
rgrover1 | 832:5dac246f8f02 | 388 | DBG("\t Swapping in UID Frame: len=%d", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 389 | updateAdvPacket(serviceData, serviceDataLen); |
rgrover1 | 830:a6ef72e0697a | 390 | //switchFlag = false; |
rgrover1 | 771:26b809199308 | 391 | } |
rgrover1 | 830:a6ef72e0697a | 392 | break; |
rgrover1 | 771:26b809199308 | 393 | default: |
rgrover1 | 830:a6ef72e0697a | 394 | ERR("You have not initialized a Frame yet, please initialize one before starting a beacon"); |
rgrover1 | 830:a6ef72e0697a | 395 | ERR("uidIsSet = %d, urlIsSet = %d, tlmIsSet = %d", uidIsSet, urlIsSet, tlmIsSet); |
rgrover1 | 830:a6ef72e0697a | 396 | } |
rgrover1 | 830:a6ef72e0697a | 397 | } |
rgrover1 | 830:a6ef72e0697a | 398 | |
rgrover1 | 830:a6ef72e0697a | 399 | /* |
rgrover1 | 830:a6ef72e0697a | 400 | * Callback to swap in URL frame |
rgrover1 | 830:a6ef72e0697a | 401 | */ |
rgrover1 | 830:a6ef72e0697a | 402 | void urlCallback(void) { |
rgrover1 | 830:a6ef72e0697a | 403 | DBG("urlCallback"); |
rgrover1 | 832:5dac246f8f02 | 404 | if (false == advLock) { |
rgrover1 | 830:a6ef72e0697a | 405 | advLock = true; |
rgrover1 | 830:a6ef72e0697a | 406 | DBG("advLock = url") |
rgrover1 | 830:a6ef72e0697a | 407 | frameIndex = url; |
rgrover1 | 830:a6ef72e0697a | 408 | swapOutFrames(frameIndex); |
rgrover1 | 830:a6ef72e0697a | 409 | ble.startAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 410 | } else { |
rgrover1 | 830:a6ef72e0697a | 411 | // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free |
rgrover1 | 832:5dac246f8f02 | 412 | INFO("URI(%d) cannot complete, %d is currently broadcasting", url, frameIndex); |
rgrover1 | 830:a6ef72e0697a | 413 | FrameTypes x = url; |
rgrover1 | 830:a6ef72e0697a | 414 | overflow.push(x); |
rgrover1 | 830:a6ef72e0697a | 415 | } |
rgrover1 | 830:a6ef72e0697a | 416 | } |
rgrover1 | 830:a6ef72e0697a | 417 | |
rgrover1 | 830:a6ef72e0697a | 418 | /* |
rgrover1 | 830:a6ef72e0697a | 419 | * Callback to swap in UID frame |
rgrover1 | 830:a6ef72e0697a | 420 | */ |
rgrover1 | 830:a6ef72e0697a | 421 | void uidCallback(void) { |
rgrover1 | 830:a6ef72e0697a | 422 | DBG("uidCallback"); |
rgrover1 | 832:5dac246f8f02 | 423 | if (false == advLock) { |
rgrover1 | 830:a6ef72e0697a | 424 | advLock = true; |
rgrover1 | 830:a6ef72e0697a | 425 | DBG("advLock = uid") |
rgrover1 | 830:a6ef72e0697a | 426 | frameIndex = uid; |
rgrover1 | 830:a6ef72e0697a | 427 | swapOutFrames(frameIndex); |
rgrover1 | 830:a6ef72e0697a | 428 | ble.startAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 429 | } else { |
rgrover1 | 830:a6ef72e0697a | 430 | // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free |
rgrover1 | 832:5dac246f8f02 | 431 | INFO("UID(%d) cannot complete, %d is currently broadcasting", uid, frameIndex); |
rgrover1 | 830:a6ef72e0697a | 432 | FrameTypes x = uid; // have to do this to satisfy cont vs volatile keywords... sigh... |
rgrover1 | 830:a6ef72e0697a | 433 | overflow.push(x); |
rgrover1 | 830:a6ef72e0697a | 434 | } |
rgrover1 | 830:a6ef72e0697a | 435 | } |
rgrover1 | 830:a6ef72e0697a | 436 | |
rgrover1 | 830:a6ef72e0697a | 437 | /* |
rgrover1 | 830:a6ef72e0697a | 438 | * Callback to swap in TLM frame |
rgrover1 | 830:a6ef72e0697a | 439 | */ |
rgrover1 | 830:a6ef72e0697a | 440 | void tlmCallback(void) { |
rgrover1 | 830:a6ef72e0697a | 441 | DBG("tlmCallback"); |
rgrover1 | 832:5dac246f8f02 | 442 | if (false == advLock) { |
rgrover1 | 830:a6ef72e0697a | 443 | // OK to broadcast |
rgrover1 | 830:a6ef72e0697a | 444 | advLock = true; |
rgrover1 | 830:a6ef72e0697a | 445 | DBG("advLock = tlm") |
rgrover1 | 830:a6ef72e0697a | 446 | frameIndex = tlm; |
rgrover1 | 830:a6ef72e0697a | 447 | swapOutFrames(frameIndex); |
rgrover1 | 830:a6ef72e0697a | 448 | ble.startAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 449 | } else { |
rgrover1 | 830:a6ef72e0697a | 450 | // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free |
rgrover1 | 832:5dac246f8f02 | 451 | INFO("TLM(%d) cannot complete, %d is currently broadcasting", tlm, frameIndex); |
rgrover1 | 830:a6ef72e0697a | 452 | FrameTypes x = tlm; |
rgrover1 | 830:a6ef72e0697a | 453 | overflow.push(x); |
rgrover1 | 830:a6ef72e0697a | 454 | } |
rgrover1 | 830:a6ef72e0697a | 455 | } |
rgrover1 | 830:a6ef72e0697a | 456 | |
rgrover1 | 830:a6ef72e0697a | 457 | void stopAdvCallback(void) { |
rgrover1 | 832:5dac246f8f02 | 458 | if (overflow.empty()) { |
rgrover1 | 830:a6ef72e0697a | 459 | // if nothing left to transmit, stop |
rgrover1 | 830:a6ef72e0697a | 460 | ble.stopAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 461 | advLock = false; // unlock lock |
rgrover1 | 830:a6ef72e0697a | 462 | } else { |
rgrover1 | 830:a6ef72e0697a | 463 | // transmit other packets at current time index |
rgrover1 | 830:a6ef72e0697a | 464 | FrameTypes x = NONE; |
rgrover1 | 830:a6ef72e0697a | 465 | overflow.pop(x); |
rgrover1 | 832:5dac246f8f02 | 466 | INFO("Re-Transmitting %d", x); |
rgrover1 | 830:a6ef72e0697a | 467 | swapOutFrames(x); |
rgrover1 | 771:26b809199308 | 468 | } |
rgrover1 | 771:26b809199308 | 469 | } |
rgrover1 | 771:26b809199308 | 470 | |
rgrover1 | 771:26b809199308 | 471 | /* |
rgrover1 | 786:d6d7087d8377 | 472 | * Callback from onRadioNotification(), used to update the PDUCounter and process next state. |
rgrover1 | 786:d6d7087d8377 | 473 | */ |
rgrover1 | 786:d6d7087d8377 | 474 | #define EDDYSTONE_SWAPFRAME_DELAYMS 1 |
rgrover1 | 771:26b809199308 | 475 | void radioNotificationCallback(bool radioActive) { |
rgrover1 | 771:26b809199308 | 476 | // Update PDUCount |
rgrover1 | 771:26b809199308 | 477 | TlmPduCount++; |
rgrover1 | 830:a6ef72e0697a | 478 | // True just before an frame is sent, false just after a frame is sent |
rgrover1 | 832:5dac246f8f02 | 479 | if (radioActive) { |
rgrover1 | 786:d6d7087d8377 | 480 | // Do Nothing |
rgrover1 | 786:d6d7087d8377 | 481 | } else { |
rgrover1 | 830:a6ef72e0697a | 482 | // Packet has been sent, disable advertising |
rgrover1 | 830:a6ef72e0697a | 483 | stopAdv.attach_us(this, &EddystoneService::stopAdvCallback, 1); |
rgrover1 | 771:26b809199308 | 484 | } |
rgrover1 | 786:d6d7087d8377 | 485 | } |
rgrover1 | 786:d6d7087d8377 | 486 | |
rgrover1 | 786:d6d7087d8377 | 487 | /* |
rgrover1 | 786:d6d7087d8377 | 488 | * This function explicityly sets the parameters used by the Eddystone beacon. |
rgrover1 | 786:d6d7087d8377 | 489 | * this function should be used in leu of the config service. |
rgrover1 | 786:d6d7087d8377 | 490 | * |
rgrover1 | 786:d6d7087d8377 | 491 | * @param bleIn ble object used to broadcast eddystone information |
rgrover1 | 830:a6ef72e0697a | 492 | * @param beaconPeriodus is how often ble broadcasts are mde, in mili seconds |
rgrover1 | 786:d6d7087d8377 | 493 | * @param txPowerLevel sets the broadcasting power level. |
rgrover1 | 786:d6d7087d8377 | 494 | * |
rgrover1 | 786:d6d7087d8377 | 495 | */ |
rgrover1 | 832:5dac246f8f02 | 496 | EddystoneService(BLEDevice &bleIn, |
rgrover1 | 832:5dac246f8f02 | 497 | uint16_t beaconPeriodus = 100, |
rgrover1 | 832:5dac246f8f02 | 498 | uint8_t txPowerIn = 0) : |
rgrover1 | 830:a6ef72e0697a | 499 | ble(bleIn), |
rgrover1 | 830:a6ef72e0697a | 500 | advPeriodus(beaconPeriodus), |
rgrover1 | 830:a6ef72e0697a | 501 | txPower(txPowerIn), |
rgrover1 | 830:a6ef72e0697a | 502 | advLock(false), |
rgrover1 | 830:a6ef72e0697a | 503 | frameIndex(NONE) { |
rgrover1 | 830:a6ef72e0697a | 504 | } |
rgrover1 | 830:a6ef72e0697a | 505 | |
rgrover1 | 830:a6ef72e0697a | 506 | /* |
rgrover1 | 830:a6ef72e0697a | 507 | * @breif this function starts eddystone advertising based on configured frames. |
rgrover1 | 830:a6ef72e0697a | 508 | */ |
rgrover1 | 830:a6ef72e0697a | 509 | void start(void) { |
rgrover1 | 830:a6ef72e0697a | 510 | // Initialize Frame transition, start with URL to pass eddystone validator app on first try |
rgrover1 | 830:a6ef72e0697a | 511 | if (urlIsSet) { |
rgrover1 | 830:a6ef72e0697a | 512 | frameIndex = url; |
rgrover1 | 830:a6ef72e0697a | 513 | urlTicker.attach(this, &EddystoneService::urlCallback, urlAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 514 | DBG("attached urlCallback every %d seconds", urlAdvPeriod); |
rgrover1 | 786:d6d7087d8377 | 515 | } |
rgrover1 | 830:a6ef72e0697a | 516 | if (uidIsSet) { |
rgrover1 | 830:a6ef72e0697a | 517 | frameIndex = uid; |
rgrover1 | 830:a6ef72e0697a | 518 | uidTicker.attach(this, &EddystoneService::uidCallback, uidAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 519 | DBG("attached uidCallback every %d seconds", uidAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 520 | } |
rgrover1 | 832:5dac246f8f02 | 521 | if (tlmIsSet) { |
rgrover1 | 830:a6ef72e0697a | 522 | frameIndex = tlm; |
rgrover1 | 830:a6ef72e0697a | 523 | // Make double sure the PDUCount and TimeSinceBoot fields are set to zero at reset |
rgrover1 | 830:a6ef72e0697a | 524 | updateTlmPduCount(0); |
rgrover1 | 830:a6ef72e0697a | 525 | updateTlmTimeSinceBoot(0); |
rgrover1 | 832:5dac246f8f02 | 526 | timeSinceBootTick.attach(this, &EddystoneService::tsbCallback, 0.1); // incriment the TimeSinceBoot ticker every 0.1s |
rgrover1 | 830:a6ef72e0697a | 527 | tlmTicker.attach(this, &EddystoneService::tlmCallback, TlmAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 528 | DBG("attached tlmCallback every %d seconds", TlmAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 529 | } |
rgrover1 | 832:5dac246f8f02 | 530 | if (NONE == frameIndex) { |
rgrover1 | 830:a6ef72e0697a | 531 | error("No Frames were Initialized! Please initialize a frame before starting an eddystone beacon."); |
rgrover1 | 830:a6ef72e0697a | 532 | } |
rgrover1 | 830:a6ef72e0697a | 533 | //uidRFU = 0; |
rgrover1 | 786:d6d7087d8377 | 534 | |
rgrover1 | 832:5dac246f8f02 | 535 | ble.setTxPower(txPower); |
rgrover1 | 832:5dac246f8f02 | 536 | ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback); |
rgrover1 | 771:26b809199308 | 537 | } |
rgrover1 | 771:26b809199308 | 538 | |
rgrover1 | 771:26b809199308 | 539 | private: |
rgrover1 | 786:d6d7087d8377 | 540 | |
rgrover1 | 830:a6ef72e0697a | 541 | // Eddystone Variables |
rgrover1 | 786:d6d7087d8377 | 542 | BLEDevice &ble; |
rgrover1 | 830:a6ef72e0697a | 543 | uint16_t advPeriodus; |
rgrover1 | 830:a6ef72e0697a | 544 | uint8_t txPower; |
rgrover1 | 830:a6ef72e0697a | 545 | Ticker timeSinceBootTick; // counter that counts time since boot |
rgrover1 | 830:a6ef72e0697a | 546 | volatile bool advLock; |
rgrover1 | 830:a6ef72e0697a | 547 | volatile FrameTypes frameIndex; |
rgrover1 | 830:a6ef72e0697a | 548 | Timeout stopAdv; |
rgrover1 | 830:a6ef72e0697a | 549 | |
rgrover1 | 830:a6ef72e0697a | 550 | |
rgrover1 | 830:a6ef72e0697a | 551 | // URI Frame Variables |
rgrover1 | 830:a6ef72e0697a | 552 | uint8_t defaultUriDataLength; |
rgrover1 | 786:d6d7087d8377 | 553 | UriData_t defaultUriData; |
rgrover1 | 830:a6ef72e0697a | 554 | int8_t defaultUrlPower; |
rgrover1 | 830:a6ef72e0697a | 555 | bool urlIsSet; // flag that enables / disable URI Frames |
rgrover1 | 830:a6ef72e0697a | 556 | uint32_t urlAdvPeriod; // how long the url frame will be advertised for |
rgrover1 | 830:a6ef72e0697a | 557 | Ticker urlTicker; |
rgrover1 | 830:a6ef72e0697a | 558 | |
rgrover1 | 830:a6ef72e0697a | 559 | // UID Frame Variables |
rgrover1 | 786:d6d7087d8377 | 560 | UIDNamespaceID_t defaultUidNamespaceID; |
rgrover1 | 786:d6d7087d8377 | 561 | UIDInstanceID_t defaultUidInstanceID; |
rgrover1 | 786:d6d7087d8377 | 562 | int8_t defaultUidPower; |
rgrover1 | 786:d6d7087d8377 | 563 | uint16_t uidRFU; |
rgrover1 | 830:a6ef72e0697a | 564 | bool uidIsSet; // flag that enables / disable UID Frames |
rgrover1 | 830:a6ef72e0697a | 565 | uint32_t uidAdvPeriod; // how long the uid frame will be advertised for |
rgrover1 | 830:a6ef72e0697a | 566 | Ticker uidTicker; |
rgrover1 | 786:d6d7087d8377 | 567 | |
rgrover1 | 830:a6ef72e0697a | 568 | // TLM Frame Variables |
rgrover1 | 832:5dac246f8f02 | 569 | uint8_t TlmVersion; |
rgrover1 | 832:5dac246f8f02 | 570 | volatile uint16_t TlmBatteryVoltage; |
rgrover1 | 832:5dac246f8f02 | 571 | volatile uint16_t TlmBeaconTemp; |
rgrover1 | 832:5dac246f8f02 | 572 | volatile uint32_t TlmPduCount; |
rgrover1 | 832:5dac246f8f02 | 573 | volatile uint32_t TlmTimeSinceBoot; |
rgrover1 | 832:5dac246f8f02 | 574 | bool tlmIsSet; // flag that enables / disables TLM frames |
rgrover1 | 832:5dac246f8f02 | 575 | uint32_t TlmAdvPeriod; // number of minutes between adv frames |
rgrover1 | 832:5dac246f8f02 | 576 | Ticker tlmTicker; |
rgrover1 | 771:26b809199308 | 577 | |
rgrover1 | 771:26b809199308 | 578 | public: |
rgrover1 | 771:26b809199308 | 579 | /* |
rgrover1 | 771:26b809199308 | 580 | * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification). |
rgrover1 | 771:26b809199308 | 581 | */ |
rgrover1 | 830:a6ef72e0697a | 582 | static void encodeURL(const char *uriDataIn, UriData_t uriDataOut, uint8_t &sizeofURIDataOut) { |
rgrover1 | 832:5dac246f8f02 | 583 | DBG("Encode URL = %s", uriDataIn); |
rgrover1 | 832:5dac246f8f02 | 584 | const char *prefixes[] = { |
rgrover1 | 771:26b809199308 | 585 | "http://www.", |
rgrover1 | 771:26b809199308 | 586 | "https://www.", |
rgrover1 | 771:26b809199308 | 587 | "http://", |
rgrover1 | 771:26b809199308 | 588 | "https://", |
rgrover1 | 771:26b809199308 | 589 | }; |
rgrover1 | 771:26b809199308 | 590 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 832:5dac246f8f02 | 591 | const char *suffixes[] = { |
rgrover1 | 771:26b809199308 | 592 | ".com/", |
rgrover1 | 771:26b809199308 | 593 | ".org/", |
rgrover1 | 771:26b809199308 | 594 | ".edu/", |
rgrover1 | 771:26b809199308 | 595 | ".net/", |
rgrover1 | 771:26b809199308 | 596 | ".info/", |
rgrover1 | 771:26b809199308 | 597 | ".biz/", |
rgrover1 | 771:26b809199308 | 598 | ".gov/", |
rgrover1 | 771:26b809199308 | 599 | ".com", |
rgrover1 | 771:26b809199308 | 600 | ".org", |
rgrover1 | 771:26b809199308 | 601 | ".edu", |
rgrover1 | 771:26b809199308 | 602 | ".net", |
rgrover1 | 771:26b809199308 | 603 | ".info", |
rgrover1 | 771:26b809199308 | 604 | ".biz", |
rgrover1 | 771:26b809199308 | 605 | ".gov" |
rgrover1 | 771:26b809199308 | 606 | }; |
rgrover1 | 771:26b809199308 | 607 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 771:26b809199308 | 608 | |
rgrover1 | 771:26b809199308 | 609 | sizeofURIDataOut = 0; |
rgrover1 | 771:26b809199308 | 610 | memset(uriDataOut, 0, sizeof(UriData_t)); |
rgrover1 | 771:26b809199308 | 611 | |
rgrover1 | 771:26b809199308 | 612 | if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) { |
rgrover1 | 771:26b809199308 | 613 | return; |
rgrover1 | 771:26b809199308 | 614 | } |
rgrover1 | 771:26b809199308 | 615 | |
rgrover1 | 771:26b809199308 | 616 | /* |
rgrover1 | 771:26b809199308 | 617 | * handle prefix |
rgrover1 | 771:26b809199308 | 618 | */ |
rgrover1 | 771:26b809199308 | 619 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 771:26b809199308 | 620 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 771:26b809199308 | 621 | if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) { |
rgrover1 | 771:26b809199308 | 622 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 771:26b809199308 | 623 | uriDataIn += prefixLen; |
rgrover1 | 771:26b809199308 | 624 | break; |
rgrover1 | 771:26b809199308 | 625 | } |
rgrover1 | 771:26b809199308 | 626 | } |
rgrover1 | 771:26b809199308 | 627 | |
rgrover1 | 771:26b809199308 | 628 | /* |
rgrover1 | 771:26b809199308 | 629 | * handle suffixes |
rgrover1 | 771:26b809199308 | 630 | */ |
rgrover1 | 771:26b809199308 | 631 | while (*uriDataIn && (sizeofURIDataOut < URI_DATA_MAX)) { |
rgrover1 | 771:26b809199308 | 632 | /* check for suffix match */ |
rgrover1 | 771:26b809199308 | 633 | unsigned i; |
rgrover1 | 771:26b809199308 | 634 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 771:26b809199308 | 635 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 771:26b809199308 | 636 | if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) { |
rgrover1 | 771:26b809199308 | 637 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 771:26b809199308 | 638 | uriDataIn += suffixLen; |
rgrover1 | 771:26b809199308 | 639 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 771:26b809199308 | 640 | } |
rgrover1 | 771:26b809199308 | 641 | } |
rgrover1 | 771:26b809199308 | 642 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 830:a6ef72e0697a | 643 | INFO("Encoding URI: No Suffix Found"); |
rgrover1 | 771:26b809199308 | 644 | if (i == NUM_SUFFIXES) { |
rgrover1 | 771:26b809199308 | 645 | uriDataOut[sizeofURIDataOut++] = *uriDataIn; |
rgrover1 | 771:26b809199308 | 646 | ++uriDataIn; |
rgrover1 | 771:26b809199308 | 647 | } |
rgrover1 | 771:26b809199308 | 648 | } |
rgrover1 | 771:26b809199308 | 649 | } |
rgrover1 | 771:26b809199308 | 650 | }; |
rgrover1 | 771:26b809199308 | 651 | |
rgrover1 | 771:26b809199308 | 652 | #endif // SERVICES_EDDYSTONEBEACON_H_ |