BLE_API for nRF51
Fork of BLE_API by
ble/services/EddystoneService.h@1201:0c728c14f30e, 2016-05-18 (annotated)
- Committer:
- chiang404
- Date:
- Wed May 18 11:21:11 2016 +0000
- Revision:
- 1201:0c728c14f30e
- Parent:
- 832:5dac246f8f02
- Child:
- 841:468a5863cacb
Security UART
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 | 786:d6d7087d8377 | 188 | /* |
rgrover1 | 786:d6d7087d8377 | 189 | * Construct URL frame from private variables |
rgrover1 | 786:d6d7087d8377 | 190 | * @param[in/out] Data pointer to array to store constructed frame in |
rgrover1 | 786:d6d7087d8377 | 191 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
rgrover1 | 786:d6d7087d8377 | 192 | * @return number of bytes used. negative number indicates error message. |
rgrover1 | 786:d6d7087d8377 | 193 | */ |
rgrover1 | 832:5dac246f8f02 | 194 | int constructURLFrame(uint8_t *Data, uint8_t maxSize) { |
rgrover1 | 786:d6d7087d8377 | 195 | int index = 0; |
rgrover1 | 786:d6d7087d8377 | 196 | Data[index++] = FRAME_TYPE_URL; // 1B Type |
rgrover1 | 786:d6d7087d8377 | 197 | Data[index++] = defaultUrlPower; // 1B TX Power |
rgrover1 | 832:5dac246f8f02 | 198 | for (int x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL |
rgrover1 | 786:d6d7087d8377 | 199 | Data[index++] = defaultUriData[x]; |
rgrover1 | 771:26b809199308 | 200 | } |
rgrover1 | 832:5dac246f8f02 | 201 | DBG("constructURLFrame: %d, %d", maxSize, index); |
rgrover1 | 786:d6d7087d8377 | 202 | return index; |
rgrover1 | 786:d6d7087d8377 | 203 | } |
rgrover1 | 771:26b809199308 | 204 | |
rgrover1 | 786:d6d7087d8377 | 205 | /* |
rgrover1 | 786:d6d7087d8377 | 206 | * Set Eddystone TLM Frame information. |
rgrover1 | 786:d6d7087d8377 | 207 | * @param[in] Version of the TLM beacon data format |
rgrover1 | 830:a6ef72e0697a | 208 | * @param[in] advPeriod how often to advertise the TLM frame for (in minutes) |
rgrover1 | 786:d6d7087d8377 | 209 | * @param batteryVoltage in milivolts |
rgrover1 | 786:d6d7087d8377 | 210 | * @param beaconTemp in 8.8 floating point notation |
rgrover1 | 786:d6d7087d8377 | 211 | * |
rgrover1 | 786:d6d7087d8377 | 212 | */ |
rgrover1 | 832:5dac246f8f02 | 213 | void setTLMFrameData(uint8_t version = 0, |
rgrover1 | 832:5dac246f8f02 | 214 | uint32_t advPeriod = 60, |
rgrover1 | 832:5dac246f8f02 | 215 | uint16_t batteryVoltage = 0, |
rgrover1 | 832:5dac246f8f02 | 216 | uint16_t beaconTemp = 0, |
rgrover1 | 832:5dac246f8f02 | 217 | uint32_t pduCount = 0, |
rgrover1 | 832:5dac246f8f02 | 218 | uint32_t timeSinceBoot = 0) { |
rgrover1 | 832:5dac246f8f02 | 219 | if (0 == advPeriod) { |
rgrover1 | 830:a6ef72e0697a | 220 | tlmIsSet = false; |
rgrover1 | 830:a6ef72e0697a | 221 | return; |
rgrover1 | 830:a6ef72e0697a | 222 | } |
rgrover1 | 832:5dac246f8f02 | 223 | TlmVersion = version; |
rgrover1 | 786:d6d7087d8377 | 224 | TlmBatteryVoltage = batteryVoltage; |
rgrover1 | 832:5dac246f8f02 | 225 | TlmBeaconTemp = beaconTemp; |
rgrover1 | 832:5dac246f8f02 | 226 | TlmPduCount = pduCount; // reset |
rgrover1 | 832:5dac246f8f02 | 227 | TlmTimeSinceBoot = timeSinceBoot; // reset |
rgrover1 | 832:5dac246f8f02 | 228 | TlmAdvPeriod = advPeriod; |
rgrover1 | 832:5dac246f8f02 | 229 | tlmIsSet = true; // TLM Data has been enabled |
rgrover1 | 786:d6d7087d8377 | 230 | } |
rgrover1 | 771:26b809199308 | 231 | |
rgrover1 | 786:d6d7087d8377 | 232 | /* |
rgrover1 | 786:d6d7087d8377 | 233 | * Construct TLM frame from private variables |
rgrover1 | 786:d6d7087d8377 | 234 | * @param[in/out] Data pointer to array to store constructed frame in |
rgrover1 | 786:d6d7087d8377 | 235 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
rgrover1 | 786:d6d7087d8377 | 236 | * @return number of bytes used. negative number indicates error message. |
rgrover1 | 786:d6d7087d8377 | 237 | */ |
rgrover1 | 832:5dac246f8f02 | 238 | int constructTLMFrame(uint8_t *Data, uint8_t maxSize) { |
rgrover1 | 786:d6d7087d8377 | 239 | int index = 0; |
rgrover1 | 786:d6d7087d8377 | 240 | Data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry |
rgrover1 | 786:d6d7087d8377 | 241 | Data[index++] = TlmVersion; // TLM Version Number |
rgrover1 | 832:5dac246f8f02 | 242 | Data[index++] = (uint8_t)(TlmBatteryVoltage >> 8); // Battery Voltage[0] |
rgrover1 | 832:5dac246f8f02 | 243 | Data[index++] = (uint8_t)(TlmBatteryVoltage >> 0); // Battery Voltage[1] |
rgrover1 | 832:5dac246f8f02 | 244 | Data[index++] = (uint8_t)(TlmBeaconTemp >> 8); // Beacon Temp[0] |
rgrover1 | 832:5dac246f8f02 | 245 | Data[index++] = (uint8_t)(TlmBeaconTemp >> 0); // Beacon Temp[1] |
rgrover1 | 832:5dac246f8f02 | 246 | Data[index++] = (uint8_t)(TlmPduCount >> 24); // PDU Count [0] |
rgrover1 | 832:5dac246f8f02 | 247 | Data[index++] = (uint8_t)(TlmPduCount >> 16); // PDU Count [1] |
rgrover1 | 832:5dac246f8f02 | 248 | Data[index++] = (uint8_t)(TlmPduCount >> 8); // PDU Count [2] |
rgrover1 | 832:5dac246f8f02 | 249 | Data[index++] = (uint8_t)(TlmPduCount >> 0); // PDU Count [3] |
rgrover1 | 832:5dac246f8f02 | 250 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 24); // Time Since Boot [0] |
rgrover1 | 832:5dac246f8f02 | 251 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 16); // Time Since Boot [1] |
rgrover1 | 832:5dac246f8f02 | 252 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 8); // Time Since Boot [2] |
rgrover1 | 832:5dac246f8f02 | 253 | Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 0); // Time Since Boot [3] |
rgrover1 | 832:5dac246f8f02 | 254 | DBG("constructURLFrame: %d, %d", maxSize, index); |
rgrover1 | 771:26b809199308 | 255 | return index; |
rgrover1 | 771:26b809199308 | 256 | } |
rgrover1 | 771:26b809199308 | 257 | |
rgrover1 | 771:26b809199308 | 258 | /* |
rgrover1 | 786:d6d7087d8377 | 259 | * Update the TLM frame battery voltage value |
rgrover1 | 786:d6d7087d8377 | 260 | * @param[in] voltagemv Voltage to update the TLM field battery voltage with (in mV) |
rgrover1 | 786:d6d7087d8377 | 261 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 262 | */ |
rgrover1 | 786:d6d7087d8377 | 263 | void updateTlmBatteryVoltage(uint16_t voltagemv) { |
rgrover1 | 786:d6d7087d8377 | 264 | TlmBatteryVoltage = voltagemv; |
rgrover1 | 786:d6d7087d8377 | 265 | } |
rgrover1 | 786:d6d7087d8377 | 266 | |
rgrover1 | 786:d6d7087d8377 | 267 | /* |
rgrover1 | 786:d6d7087d8377 | 268 | * Update the TLM frame beacon temperature |
rgrover1 | 786:d6d7087d8377 | 269 | * @param[in] temp Temperature of beacon (in 8.8fpn) |
rgrover1 | 786:d6d7087d8377 | 270 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 271 | */ |
rgrover1 | 786:d6d7087d8377 | 272 | void updateTlmBeaconTemp(uint16_t temp) { |
rgrover1 | 786:d6d7087d8377 | 273 | TlmBeaconTemp = temp; |
rgrover1 | 786:d6d7087d8377 | 274 | } |
rgrover1 | 786:d6d7087d8377 | 275 | |
rgrover1 | 786:d6d7087d8377 | 276 | /* |
rgrover1 | 786:d6d7087d8377 | 277 | * Update the TLM frame PDU Count field |
rgrover1 | 786:d6d7087d8377 | 278 | * @param[in] pduCount Number of Advertisiting frames sent since powerup |
rgrover1 | 786:d6d7087d8377 | 279 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 280 | */ |
rgrover1 | 786:d6d7087d8377 | 281 | void updateTlmPduCount(uint32_t pduCount) { |
rgrover1 | 786:d6d7087d8377 | 282 | TlmPduCount = pduCount; |
rgrover1 | 771:26b809199308 | 283 | } |
rgrover1 | 771:26b809199308 | 284 | |
rgrover1 | 771:26b809199308 | 285 | /* |
rgrover1 | 786:d6d7087d8377 | 286 | * Update the TLM frame Time since boot in 0.1s incriments |
rgrover1 | 786:d6d7087d8377 | 287 | * @param[in] timeSinceBoot Time since boot in 0.1s incriments |
rgrover1 | 786:d6d7087d8377 | 288 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 289 | */ |
rgrover1 | 786:d6d7087d8377 | 290 | void updateTlmTimeSinceBoot(uint32_t timeSinceBoot) { |
rgrover1 | 786:d6d7087d8377 | 291 | TlmTimeSinceBoot = timeSinceBoot; |
rgrover1 | 786:d6d7087d8377 | 292 | } |
rgrover1 | 771:26b809199308 | 293 | |
rgrover1 | 786:d6d7087d8377 | 294 | /* |
rgrover1 | 786:d6d7087d8377 | 295 | * callback function, called every 0.1s, incriments the TimeSinceBoot field in the TLM frame |
rgrover1 | 786:d6d7087d8377 | 296 | * @return nothing |
rgrover1 | 786:d6d7087d8377 | 297 | */ |
rgrover1 | 786:d6d7087d8377 | 298 | void tsbCallback(void) { |
rgrover1 | 786:d6d7087d8377 | 299 | TlmTimeSinceBoot++; |
rgrover1 | 771:26b809199308 | 300 | } |
rgrover1 | 771:26b809199308 | 301 | |
rgrover1 | 771:26b809199308 | 302 | /* |
rgrover1 | 786:d6d7087d8377 | 303 | * Update advertising data |
rgrover1 | 786:d6d7087d8377 | 304 | * @return true on success, false on failure |
rgrover1 | 786:d6d7087d8377 | 305 | */ |
rgrover1 | 786:d6d7087d8377 | 306 | bool updateAdvPacket(uint8_t serviceData[], unsigned serviceDataLen) { |
rgrover1 | 786:d6d7087d8377 | 307 | // Fields from the Service |
rgrover1 | 786:d6d7087d8377 | 308 | DBG("Updating AdvFrame: %d", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 309 | |
rgrover1 | 786:d6d7087d8377 | 310 | ble.clearAdvertisingPayload(); |
rgrover1 | 830:a6ef72e0697a | 311 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
rgrover1 | 830:a6ef72e0697a | 312 | ble.setAdvertisingInterval(100); |
rgrover1 | 786:d6d7087d8377 | 313 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 786:d6d7087d8377 | 314 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE)); |
rgrover1 | 786:d6d7087d8377 | 315 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen); |
rgrover1 | 786:d6d7087d8377 | 316 | |
rgrover1 | 830:a6ef72e0697a | 317 | |
rgrover1 | 786:d6d7087d8377 | 318 | return true; |
rgrover1 | 786:d6d7087d8377 | 319 | } |
rgrover1 | 786:d6d7087d8377 | 320 | |
rgrover1 | 786:d6d7087d8377 | 321 | /* |
rgrover1 | 786:d6d7087d8377 | 322 | * State machine for switching out frames. |
rgrover1 | 786:d6d7087d8377 | 323 | * This function is called by the radioNotificationCallback when a frame needs to get swapped out. |
rgrover1 | 786:d6d7087d8377 | 324 | * This function exists because of time constraints in the radioNotificationCallback, so it is effectively |
rgrover1 | 786:d6d7087d8377 | 325 | * broken up into two functions. |
rgrover1 | 786:d6d7087d8377 | 326 | */ |
rgrover1 | 830:a6ef72e0697a | 327 | void swapOutFrames(FrameTypes frameType) { |
rgrover1 | 832:5dac246f8f02 | 328 | uint8_t serviceData[SERVICE_DATA_MAX]; |
rgrover1 | 771:26b809199308 | 329 | unsigned serviceDataLen = 0; |
rgrover1 | 786:d6d7087d8377 | 330 | //hard code in the eddystone UUID |
rgrover1 | 771:26b809199308 | 331 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; |
rgrover1 | 771:26b809199308 | 332 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; |
rgrover1 | 771:26b809199308 | 333 | |
rgrover1 | 771:26b809199308 | 334 | // if certain frames are not enabled, then skip them. Worst case TLM is always enabled |
rgrover1 | 832:5dac246f8f02 | 335 | switch (frameType) { |
rgrover1 | 830:a6ef72e0697a | 336 | case tlm: |
rgrover1 | 830:a6ef72e0697a | 337 | // TLM frame |
rgrover1 | 832:5dac246f8f02 | 338 | if (tlmIsSet) { |
rgrover1 | 832:5dac246f8f02 | 339 | DBG("Swapping in TLM Frame: version=%x, Batt=%d, Temp = %d, PDUCnt = %d, TimeSinceBoot=%d", |
rgrover1 | 832:5dac246f8f02 | 340 | TlmVersion, |
rgrover1 | 832:5dac246f8f02 | 341 | TlmBatteryVoltage, |
rgrover1 | 832:5dac246f8f02 | 342 | TlmBeaconTemp, |
rgrover1 | 832:5dac246f8f02 | 343 | TlmPduCount, |
rgrover1 | 832:5dac246f8f02 | 344 | TlmTimeSinceBoot); |
rgrover1 | 832:5dac246f8f02 | 345 | serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, 20); |
rgrover1 | 832:5dac246f8f02 | 346 | DBG("\t Swapping in TLM Frame: len=%d", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 347 | updateAdvPacket(serviceData, serviceDataLen); |
rgrover1 | 830:a6ef72e0697a | 348 | } |
rgrover1 | 830:a6ef72e0697a | 349 | break; |
rgrover1 | 830:a6ef72e0697a | 350 | case url: |
rgrover1 | 771:26b809199308 | 351 | // URL Frame |
rgrover1 | 832:5dac246f8f02 | 352 | if (urlIsSet) { |
rgrover1 | 832:5dac246f8f02 | 353 | DBG("Swapping in URL Frame: Power: %d", defaultUrlPower); |
rgrover1 | 832:5dac246f8f02 | 354 | serviceDataLen += constructURLFrame(serviceData + serviceDataLen, 20); |
rgrover1 | 832:5dac246f8f02 | 355 | DBG("\t Swapping in URL Frame: len=%d ", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 356 | updateAdvPacket(serviceData, serviceDataLen); |
rgrover1 | 830:a6ef72e0697a | 357 | //switchFlag = false; |
rgrover1 | 771:26b809199308 | 358 | } |
rgrover1 | 830:a6ef72e0697a | 359 | break; |
rgrover1 | 830:a6ef72e0697a | 360 | case uid: |
rgrover1 | 771:26b809199308 | 361 | // UID Frame |
rgrover1 | 832:5dac246f8f02 | 362 | if (uidIsSet) { |
rgrover1 | 832:5dac246f8f02 | 363 | DBG("Swapping in UID Frame: Power: %d", defaultUidPower); |
rgrover1 | 832:5dac246f8f02 | 364 | serviceDataLen += constructUIDFrame(serviceData + serviceDataLen, 20); |
rgrover1 | 832:5dac246f8f02 | 365 | DBG("\t Swapping in UID Frame: len=%d", serviceDataLen); |
rgrover1 | 832:5dac246f8f02 | 366 | updateAdvPacket(serviceData, serviceDataLen); |
rgrover1 | 830:a6ef72e0697a | 367 | //switchFlag = false; |
rgrover1 | 771:26b809199308 | 368 | } |
rgrover1 | 830:a6ef72e0697a | 369 | break; |
rgrover1 | 771:26b809199308 | 370 | default: |
rgrover1 | 830:a6ef72e0697a | 371 | ERR("You have not initialized a Frame yet, please initialize one before starting a beacon"); |
rgrover1 | 830:a6ef72e0697a | 372 | ERR("uidIsSet = %d, urlIsSet = %d, tlmIsSet = %d", uidIsSet, urlIsSet, tlmIsSet); |
rgrover1 | 830:a6ef72e0697a | 373 | } |
rgrover1 | 830:a6ef72e0697a | 374 | } |
rgrover1 | 830:a6ef72e0697a | 375 | |
rgrover1 | 830:a6ef72e0697a | 376 | /* |
rgrover1 | 830:a6ef72e0697a | 377 | * Callback to swap in URL frame |
rgrover1 | 830:a6ef72e0697a | 378 | */ |
rgrover1 | 830:a6ef72e0697a | 379 | void urlCallback(void) { |
rgrover1 | 830:a6ef72e0697a | 380 | DBG("urlCallback"); |
rgrover1 | 832:5dac246f8f02 | 381 | if (false == advLock) { |
rgrover1 | 830:a6ef72e0697a | 382 | advLock = true; |
rgrover1 | 830:a6ef72e0697a | 383 | DBG("advLock = url") |
rgrover1 | 830:a6ef72e0697a | 384 | frameIndex = url; |
rgrover1 | 830:a6ef72e0697a | 385 | swapOutFrames(frameIndex); |
rgrover1 | 830:a6ef72e0697a | 386 | ble.startAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 387 | } else { |
rgrover1 | 830:a6ef72e0697a | 388 | // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free |
rgrover1 | 832:5dac246f8f02 | 389 | INFO("URI(%d) cannot complete, %d is currently broadcasting", url, frameIndex); |
rgrover1 | 830:a6ef72e0697a | 390 | FrameTypes x = url; |
rgrover1 | 830:a6ef72e0697a | 391 | overflow.push(x); |
rgrover1 | 830:a6ef72e0697a | 392 | } |
rgrover1 | 830:a6ef72e0697a | 393 | } |
rgrover1 | 830:a6ef72e0697a | 394 | |
rgrover1 | 830:a6ef72e0697a | 395 | /* |
rgrover1 | 830:a6ef72e0697a | 396 | * Callback to swap in UID frame |
rgrover1 | 830:a6ef72e0697a | 397 | */ |
rgrover1 | 830:a6ef72e0697a | 398 | void uidCallback(void) { |
rgrover1 | 830:a6ef72e0697a | 399 | DBG("uidCallback"); |
rgrover1 | 832:5dac246f8f02 | 400 | if (false == advLock) { |
rgrover1 | 830:a6ef72e0697a | 401 | advLock = true; |
rgrover1 | 830:a6ef72e0697a | 402 | DBG("advLock = uid") |
rgrover1 | 830:a6ef72e0697a | 403 | frameIndex = uid; |
rgrover1 | 830:a6ef72e0697a | 404 | swapOutFrames(frameIndex); |
rgrover1 | 830:a6ef72e0697a | 405 | ble.startAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 406 | } else { |
rgrover1 | 830:a6ef72e0697a | 407 | // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free |
rgrover1 | 832:5dac246f8f02 | 408 | INFO("UID(%d) cannot complete, %d is currently broadcasting", uid, frameIndex); |
rgrover1 | 830:a6ef72e0697a | 409 | FrameTypes x = uid; // have to do this to satisfy cont vs volatile keywords... sigh... |
rgrover1 | 830:a6ef72e0697a | 410 | overflow.push(x); |
rgrover1 | 830:a6ef72e0697a | 411 | } |
rgrover1 | 830:a6ef72e0697a | 412 | } |
rgrover1 | 830:a6ef72e0697a | 413 | |
rgrover1 | 830:a6ef72e0697a | 414 | /* |
rgrover1 | 830:a6ef72e0697a | 415 | * Callback to swap in TLM frame |
rgrover1 | 830:a6ef72e0697a | 416 | */ |
rgrover1 | 830:a6ef72e0697a | 417 | void tlmCallback(void) { |
rgrover1 | 830:a6ef72e0697a | 418 | DBG("tlmCallback"); |
rgrover1 | 832:5dac246f8f02 | 419 | if (false == advLock) { |
rgrover1 | 830:a6ef72e0697a | 420 | // OK to broadcast |
rgrover1 | 830:a6ef72e0697a | 421 | advLock = true; |
rgrover1 | 830:a6ef72e0697a | 422 | DBG("advLock = tlm") |
rgrover1 | 830:a6ef72e0697a | 423 | frameIndex = tlm; |
rgrover1 | 830:a6ef72e0697a | 424 | swapOutFrames(frameIndex); |
rgrover1 | 830:a6ef72e0697a | 425 | ble.startAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 426 | } else { |
rgrover1 | 830:a6ef72e0697a | 427 | // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free |
rgrover1 | 832:5dac246f8f02 | 428 | INFO("TLM(%d) cannot complete, %d is currently broadcasting", tlm, frameIndex); |
rgrover1 | 830:a6ef72e0697a | 429 | FrameTypes x = tlm; |
rgrover1 | 830:a6ef72e0697a | 430 | overflow.push(x); |
rgrover1 | 830:a6ef72e0697a | 431 | } |
rgrover1 | 830:a6ef72e0697a | 432 | } |
rgrover1 | 830:a6ef72e0697a | 433 | |
rgrover1 | 830:a6ef72e0697a | 434 | void stopAdvCallback(void) { |
rgrover1 | 832:5dac246f8f02 | 435 | if (overflow.empty()) { |
rgrover1 | 830:a6ef72e0697a | 436 | // if nothing left to transmit, stop |
rgrover1 | 830:a6ef72e0697a | 437 | ble.stopAdvertising(); |
rgrover1 | 830:a6ef72e0697a | 438 | advLock = false; // unlock lock |
rgrover1 | 830:a6ef72e0697a | 439 | } else { |
rgrover1 | 830:a6ef72e0697a | 440 | // transmit other packets at current time index |
rgrover1 | 830:a6ef72e0697a | 441 | FrameTypes x = NONE; |
rgrover1 | 830:a6ef72e0697a | 442 | overflow.pop(x); |
rgrover1 | 832:5dac246f8f02 | 443 | INFO("Re-Transmitting %d", x); |
rgrover1 | 830:a6ef72e0697a | 444 | swapOutFrames(x); |
rgrover1 | 771:26b809199308 | 445 | } |
rgrover1 | 771:26b809199308 | 446 | } |
rgrover1 | 771:26b809199308 | 447 | |
rgrover1 | 771:26b809199308 | 448 | /* |
rgrover1 | 786:d6d7087d8377 | 449 | * Callback from onRadioNotification(), used to update the PDUCounter and process next state. |
rgrover1 | 786:d6d7087d8377 | 450 | */ |
rgrover1 | 786:d6d7087d8377 | 451 | #define EDDYSTONE_SWAPFRAME_DELAYMS 1 |
rgrover1 | 771:26b809199308 | 452 | void radioNotificationCallback(bool radioActive) { |
rgrover1 | 771:26b809199308 | 453 | // Update PDUCount |
rgrover1 | 771:26b809199308 | 454 | TlmPduCount++; |
rgrover1 | 830:a6ef72e0697a | 455 | // True just before an frame is sent, false just after a frame is sent |
rgrover1 | 832:5dac246f8f02 | 456 | if (radioActive) { |
rgrover1 | 786:d6d7087d8377 | 457 | // Do Nothing |
rgrover1 | 786:d6d7087d8377 | 458 | } else { |
rgrover1 | 830:a6ef72e0697a | 459 | // Packet has been sent, disable advertising |
rgrover1 | 830:a6ef72e0697a | 460 | stopAdv.attach_us(this, &EddystoneService::stopAdvCallback, 1); |
rgrover1 | 771:26b809199308 | 461 | } |
rgrover1 | 786:d6d7087d8377 | 462 | } |
rgrover1 | 786:d6d7087d8377 | 463 | |
rgrover1 | 786:d6d7087d8377 | 464 | /* |
rgrover1 | 786:d6d7087d8377 | 465 | * This function explicityly sets the parameters used by the Eddystone beacon. |
rgrover1 | 786:d6d7087d8377 | 466 | * this function should be used in leu of the config service. |
rgrover1 | 786:d6d7087d8377 | 467 | * |
rgrover1 | 786:d6d7087d8377 | 468 | * @param bleIn ble object used to broadcast eddystone information |
rgrover1 | 830:a6ef72e0697a | 469 | * @param beaconPeriodus is how often ble broadcasts are mde, in mili seconds |
rgrover1 | 786:d6d7087d8377 | 470 | * @param txPowerLevel sets the broadcasting power level. |
rgrover1 | 786:d6d7087d8377 | 471 | * |
rgrover1 | 786:d6d7087d8377 | 472 | */ |
rgrover1 | 832:5dac246f8f02 | 473 | EddystoneService(BLEDevice &bleIn, |
rgrover1 | 832:5dac246f8f02 | 474 | uint16_t beaconPeriodus = 100, |
rgrover1 | 832:5dac246f8f02 | 475 | uint8_t txPowerIn = 0) : |
rgrover1 | 830:a6ef72e0697a | 476 | ble(bleIn), |
rgrover1 | 830:a6ef72e0697a | 477 | advPeriodus(beaconPeriodus), |
rgrover1 | 830:a6ef72e0697a | 478 | txPower(txPowerIn), |
rgrover1 | 830:a6ef72e0697a | 479 | advLock(false), |
rgrover1 | 830:a6ef72e0697a | 480 | frameIndex(NONE) { |
rgrover1 | 830:a6ef72e0697a | 481 | } |
rgrover1 | 830:a6ef72e0697a | 482 | |
rgrover1 | 830:a6ef72e0697a | 483 | /* |
rgrover1 | 830:a6ef72e0697a | 484 | * @breif this function starts eddystone advertising based on configured frames. |
rgrover1 | 830:a6ef72e0697a | 485 | */ |
rgrover1 | 830:a6ef72e0697a | 486 | void start(void) { |
rgrover1 | 830:a6ef72e0697a | 487 | // Initialize Frame transition, start with URL to pass eddystone validator app on first try |
rgrover1 | 830:a6ef72e0697a | 488 | if (urlIsSet) { |
rgrover1 | 830:a6ef72e0697a | 489 | frameIndex = url; |
rgrover1 | 830:a6ef72e0697a | 490 | urlTicker.attach(this, &EddystoneService::urlCallback, urlAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 491 | DBG("attached urlCallback every %d seconds", urlAdvPeriod); |
rgrover1 | 786:d6d7087d8377 | 492 | } |
rgrover1 | 830:a6ef72e0697a | 493 | if (uidIsSet) { |
rgrover1 | 830:a6ef72e0697a | 494 | frameIndex = uid; |
rgrover1 | 830:a6ef72e0697a | 495 | uidTicker.attach(this, &EddystoneService::uidCallback, uidAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 496 | DBG("attached uidCallback every %d seconds", uidAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 497 | } |
rgrover1 | 832:5dac246f8f02 | 498 | if (tlmIsSet) { |
rgrover1 | 830:a6ef72e0697a | 499 | frameIndex = tlm; |
rgrover1 | 830:a6ef72e0697a | 500 | // Make double sure the PDUCount and TimeSinceBoot fields are set to zero at reset |
rgrover1 | 830:a6ef72e0697a | 501 | updateTlmPduCount(0); |
rgrover1 | 830:a6ef72e0697a | 502 | updateTlmTimeSinceBoot(0); |
rgrover1 | 832:5dac246f8f02 | 503 | timeSinceBootTick.attach(this, &EddystoneService::tsbCallback, 0.1); // incriment the TimeSinceBoot ticker every 0.1s |
rgrover1 | 830:a6ef72e0697a | 504 | tlmTicker.attach(this, &EddystoneService::tlmCallback, TlmAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 505 | DBG("attached tlmCallback every %d seconds", TlmAdvPeriod); |
rgrover1 | 830:a6ef72e0697a | 506 | } |
rgrover1 | 832:5dac246f8f02 | 507 | if (NONE == frameIndex) { |
rgrover1 | 830:a6ef72e0697a | 508 | error("No Frames were Initialized! Please initialize a frame before starting an eddystone beacon."); |
rgrover1 | 830:a6ef72e0697a | 509 | } |
rgrover1 | 830:a6ef72e0697a | 510 | //uidRFU = 0; |
rgrover1 | 786:d6d7087d8377 | 511 | |
rgrover1 | 832:5dac246f8f02 | 512 | ble.setTxPower(txPower); |
rgrover1 | 832:5dac246f8f02 | 513 | ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback); |
rgrover1 | 771:26b809199308 | 514 | } |
rgrover1 | 771:26b809199308 | 515 | |
rgrover1 | 771:26b809199308 | 516 | private: |
rgrover1 | 786:d6d7087d8377 | 517 | |
rgrover1 | 830:a6ef72e0697a | 518 | // Eddystone Variables |
rgrover1 | 786:d6d7087d8377 | 519 | BLEDevice &ble; |
rgrover1 | 830:a6ef72e0697a | 520 | uint16_t advPeriodus; |
rgrover1 | 830:a6ef72e0697a | 521 | uint8_t txPower; |
rgrover1 | 830:a6ef72e0697a | 522 | Ticker timeSinceBootTick; // counter that counts time since boot |
rgrover1 | 830:a6ef72e0697a | 523 | volatile bool advLock; |
rgrover1 | 830:a6ef72e0697a | 524 | volatile FrameTypes frameIndex; |
rgrover1 | 830:a6ef72e0697a | 525 | Timeout stopAdv; |
rgrover1 | 830:a6ef72e0697a | 526 | |
rgrover1 | 830:a6ef72e0697a | 527 | |
rgrover1 | 830:a6ef72e0697a | 528 | // URI Frame Variables |
rgrover1 | 830:a6ef72e0697a | 529 | uint8_t defaultUriDataLength; |
rgrover1 | 786:d6d7087d8377 | 530 | UriData_t defaultUriData; |
rgrover1 | 830:a6ef72e0697a | 531 | int8_t defaultUrlPower; |
rgrover1 | 830:a6ef72e0697a | 532 | bool urlIsSet; // flag that enables / disable URI Frames |
rgrover1 | 830:a6ef72e0697a | 533 | uint32_t urlAdvPeriod; // how long the url frame will be advertised for |
rgrover1 | 830:a6ef72e0697a | 534 | Ticker urlTicker; |
rgrover1 | 830:a6ef72e0697a | 535 | |
rgrover1 | 830:a6ef72e0697a | 536 | // UID Frame Variables |
rgrover1 | 786:d6d7087d8377 | 537 | UIDNamespaceID_t defaultUidNamespaceID; |
rgrover1 | 786:d6d7087d8377 | 538 | UIDInstanceID_t defaultUidInstanceID; |
rgrover1 | 786:d6d7087d8377 | 539 | int8_t defaultUidPower; |
rgrover1 | 786:d6d7087d8377 | 540 | uint16_t uidRFU; |
rgrover1 | 830:a6ef72e0697a | 541 | bool uidIsSet; // flag that enables / disable UID Frames |
rgrover1 | 830:a6ef72e0697a | 542 | uint32_t uidAdvPeriod; // how long the uid frame will be advertised for |
rgrover1 | 830:a6ef72e0697a | 543 | Ticker uidTicker; |
rgrover1 | 786:d6d7087d8377 | 544 | |
rgrover1 | 830:a6ef72e0697a | 545 | // TLM Frame Variables |
rgrover1 | 832:5dac246f8f02 | 546 | uint8_t TlmVersion; |
rgrover1 | 832:5dac246f8f02 | 547 | volatile uint16_t TlmBatteryVoltage; |
rgrover1 | 832:5dac246f8f02 | 548 | volatile uint16_t TlmBeaconTemp; |
rgrover1 | 832:5dac246f8f02 | 549 | volatile uint32_t TlmPduCount; |
rgrover1 | 832:5dac246f8f02 | 550 | volatile uint32_t TlmTimeSinceBoot; |
rgrover1 | 832:5dac246f8f02 | 551 | bool tlmIsSet; // flag that enables / disables TLM frames |
rgrover1 | 832:5dac246f8f02 | 552 | uint32_t TlmAdvPeriod; // number of minutes between adv frames |
rgrover1 | 832:5dac246f8f02 | 553 | Ticker tlmTicker; |
rgrover1 | 771:26b809199308 | 554 | |
rgrover1 | 771:26b809199308 | 555 | public: |
rgrover1 | 771:26b809199308 | 556 | /* |
rgrover1 | 771:26b809199308 | 557 | * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification). |
rgrover1 | 771:26b809199308 | 558 | */ |
rgrover1 | 830:a6ef72e0697a | 559 | static void encodeURL(const char *uriDataIn, UriData_t uriDataOut, uint8_t &sizeofURIDataOut) { |
rgrover1 | 832:5dac246f8f02 | 560 | DBG("Encode URL = %s", uriDataIn); |
rgrover1 | 832:5dac246f8f02 | 561 | const char *prefixes[] = { |
rgrover1 | 771:26b809199308 | 562 | "http://www.", |
rgrover1 | 771:26b809199308 | 563 | "https://www.", |
rgrover1 | 771:26b809199308 | 564 | "http://", |
rgrover1 | 771:26b809199308 | 565 | "https://", |
rgrover1 | 771:26b809199308 | 566 | }; |
rgrover1 | 771:26b809199308 | 567 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
rgrover1 | 832:5dac246f8f02 | 568 | const char *suffixes[] = { |
rgrover1 | 771:26b809199308 | 569 | ".com/", |
rgrover1 | 771:26b809199308 | 570 | ".org/", |
rgrover1 | 771:26b809199308 | 571 | ".edu/", |
rgrover1 | 771:26b809199308 | 572 | ".net/", |
rgrover1 | 771:26b809199308 | 573 | ".info/", |
rgrover1 | 771:26b809199308 | 574 | ".biz/", |
rgrover1 | 771:26b809199308 | 575 | ".gov/", |
rgrover1 | 771:26b809199308 | 576 | ".com", |
rgrover1 | 771:26b809199308 | 577 | ".org", |
rgrover1 | 771:26b809199308 | 578 | ".edu", |
rgrover1 | 771:26b809199308 | 579 | ".net", |
rgrover1 | 771:26b809199308 | 580 | ".info", |
rgrover1 | 771:26b809199308 | 581 | ".biz", |
rgrover1 | 771:26b809199308 | 582 | ".gov" |
rgrover1 | 771:26b809199308 | 583 | }; |
rgrover1 | 771:26b809199308 | 584 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
rgrover1 | 771:26b809199308 | 585 | |
rgrover1 | 771:26b809199308 | 586 | sizeofURIDataOut = 0; |
rgrover1 | 771:26b809199308 | 587 | memset(uriDataOut, 0, sizeof(UriData_t)); |
rgrover1 | 771:26b809199308 | 588 | |
rgrover1 | 771:26b809199308 | 589 | if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) { |
rgrover1 | 771:26b809199308 | 590 | return; |
rgrover1 | 771:26b809199308 | 591 | } |
rgrover1 | 771:26b809199308 | 592 | |
rgrover1 | 771:26b809199308 | 593 | /* |
rgrover1 | 771:26b809199308 | 594 | * handle prefix |
rgrover1 | 771:26b809199308 | 595 | */ |
rgrover1 | 771:26b809199308 | 596 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
rgrover1 | 771:26b809199308 | 597 | size_t prefixLen = strlen(prefixes[i]); |
rgrover1 | 771:26b809199308 | 598 | if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) { |
rgrover1 | 771:26b809199308 | 599 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 771:26b809199308 | 600 | uriDataIn += prefixLen; |
rgrover1 | 771:26b809199308 | 601 | break; |
rgrover1 | 771:26b809199308 | 602 | } |
rgrover1 | 771:26b809199308 | 603 | } |
rgrover1 | 771:26b809199308 | 604 | |
rgrover1 | 771:26b809199308 | 605 | /* |
rgrover1 | 771:26b809199308 | 606 | * handle suffixes |
rgrover1 | 771:26b809199308 | 607 | */ |
rgrover1 | 771:26b809199308 | 608 | while (*uriDataIn && (sizeofURIDataOut < URI_DATA_MAX)) { |
rgrover1 | 771:26b809199308 | 609 | /* check for suffix match */ |
rgrover1 | 771:26b809199308 | 610 | unsigned i; |
rgrover1 | 771:26b809199308 | 611 | for (i = 0; i < NUM_SUFFIXES; i++) { |
rgrover1 | 771:26b809199308 | 612 | size_t suffixLen = strlen(suffixes[i]); |
rgrover1 | 771:26b809199308 | 613 | if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) { |
rgrover1 | 771:26b809199308 | 614 | uriDataOut[sizeofURIDataOut++] = i; |
rgrover1 | 771:26b809199308 | 615 | uriDataIn += suffixLen; |
rgrover1 | 771:26b809199308 | 616 | break; /* from the for loop for checking against suffixes */ |
rgrover1 | 771:26b809199308 | 617 | } |
rgrover1 | 771:26b809199308 | 618 | } |
rgrover1 | 771:26b809199308 | 619 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
rgrover1 | 830:a6ef72e0697a | 620 | INFO("Encoding URI: No Suffix Found"); |
rgrover1 | 771:26b809199308 | 621 | if (i == NUM_SUFFIXES) { |
rgrover1 | 771:26b809199308 | 622 | uriDataOut[sizeofURIDataOut++] = *uriDataIn; |
rgrover1 | 771:26b809199308 | 623 | ++uriDataIn; |
rgrover1 | 771:26b809199308 | 624 | } |
rgrover1 | 771:26b809199308 | 625 | } |
rgrover1 | 771:26b809199308 | 626 | } |
rgrover1 | 771:26b809199308 | 627 | }; |
rgrover1 | 771:26b809199308 | 628 | |
rgrover1 | 771:26b809199308 | 629 | #endif // SERVICES_EDDYSTONEBEACON_H_ |