Eddystone Beacon only for TYBLE16

Dependencies:   TYBLE16_BASE

Please refer flowing link.
/users/kenjiArai/notebook/tyble16-module-will-become-a-mbed-family--mbedliza/

Committer:
kenjiArai
Date:
Sun Feb 25 03:41:51 2018 +0000
Revision:
1:95acd5ad69e8
Parent:
0:5087fec653b2
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5087fec653b2 1 /* mbed Microcontroller Library
kenjiArai 0:5087fec653b2 2 * Copyright (c) 2006-2015 ARM Limited
kenjiArai 0:5087fec653b2 3 *
kenjiArai 0:5087fec653b2 4 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 0:5087fec653b2 5 * you may not use this file except in compliance with the License.
kenjiArai 0:5087fec653b2 6 * You may obtain a copy of the License at
kenjiArai 0:5087fec653b2 7 *
kenjiArai 0:5087fec653b2 8 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5087fec653b2 9 *
kenjiArai 0:5087fec653b2 10 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5087fec653b2 11 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 0:5087fec653b2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5087fec653b2 13 * See the License for the specific language governing permissions and
kenjiArai 0:5087fec653b2 14 * limitations under the License.
kenjiArai 0:5087fec653b2 15 */
kenjiArai 0:5087fec653b2 16
kenjiArai 0:5087fec653b2 17 #ifndef __EDDYSTONESERVICE_H__
kenjiArai 0:5087fec653b2 18 #define __EDDYSTONESERVICE_H__
kenjiArai 0:5087fec653b2 19
kenjiArai 0:5087fec653b2 20 #include "ble/BLE.h"
kenjiArai 0:5087fec653b2 21 #include "EddystoneTypes.h"
kenjiArai 0:5087fec653b2 22 #include "URLFrame.h"
kenjiArai 0:5087fec653b2 23 #include "UIDFrame.h"
kenjiArai 0:5087fec653b2 24 #include "TLMFrame.h"
kenjiArai 0:5087fec653b2 25 #include <string.h>
kenjiArai 0:5087fec653b2 26 #ifdef YOTTA_CFG_MBED_OS
kenjiArai 0:5087fec653b2 27 #include "mbed-drivers/mbed.h"
kenjiArai 0:5087fec653b2 28 #else
kenjiArai 0:5087fec653b2 29 #include "mbed.h"
kenjiArai 0:5087fec653b2 30 #endif
kenjiArai 0:5087fec653b2 31
kenjiArai 0:5087fec653b2 32 class EddystoneService
kenjiArai 0:5087fec653b2 33 {
kenjiArai 0:5087fec653b2 34 public:
kenjiArai 0:5087fec653b2 35 static const uint16_t TOTAL_CHARACTERISTICS = 9;
kenjiArai 0:5087fec653b2 36
kenjiArai 0:5087fec653b2 37 static const uint32_t DEFAULT_CONFIG_PERIOD_MSEC = 1000;
kenjiArai 0:5087fec653b2 38 static const uint16_t DEFAULT_BEACON_PERIOD_MSEC = 1000;
kenjiArai 0:5087fec653b2 39
kenjiArai 0:5087fec653b2 40 /* Operation modes of the EddystoneService:
kenjiArai 0:5087fec653b2 41 * NONE: EddystoneService has been initialised but no memory has been
kenjiArai 0:5087fec653b2 42 * dynamically allocated. Additionally, no services are running
kenjiArai 0:5087fec653b2 43 * nothing is being advertised.
kenjiArai 0:5087fec653b2 44 * CONFIG: EddystoneService has been initialised, the configuration
kenjiArai 0:5087fec653b2 45 * service started and memory has been allocated for BLE
kenjiArai 0:5087fec653b2 46 * characteristics. Memory consumption peaks during CONFIG
kenjiArai 0:5087fec653b2 47 * mode.
kenjiArai 0:5087fec653b2 48 * BEACON: Eddystone service is running as a beacon advertising URL,
kenjiArai 0:5087fec653b2 49 * UID and/or TLM frames depending on how it is configured.
kenjiArai 0:5087fec653b2 50 * Note: The main app can change the mode of EddystoneService at any point
kenjiArai 0:5087fec653b2 51 * of time by calling startConfigService() or startBeaconService().
kenjiArai 0:5087fec653b2 52 * Resources from the previous mode will be freed. It is currently NOT
kenjiArai 0:5087fec653b2 53 * possible to force EddystoneService back into MODE_NONE.
kenjiArai 0:5087fec653b2 54 */
kenjiArai 0:5087fec653b2 55 enum OperationModes {
kenjiArai 0:5087fec653b2 56 EDDYSTONE_MODE_NONE,
kenjiArai 0:5087fec653b2 57 EDDYSTONE_MODE_CONFIG,
kenjiArai 0:5087fec653b2 58 EDDYSTONE_MODE_BEACON
kenjiArai 0:5087fec653b2 59 };
kenjiArai 0:5087fec653b2 60
kenjiArai 0:5087fec653b2 61 struct EddystoneParams_t {
kenjiArai 0:5087fec653b2 62 bool lockState;
kenjiArai 0:5087fec653b2 63 Lock_t lock;
kenjiArai 0:5087fec653b2 64 Lock_t unlock;
kenjiArai 0:5087fec653b2 65 uint8_t flags;
kenjiArai 0:5087fec653b2 66 PowerLevels_t advPowerLevels;
kenjiArai 0:5087fec653b2 67 uint8_t txPowerMode;
kenjiArai 0:5087fec653b2 68 uint16_t beaconPeriod;
kenjiArai 0:5087fec653b2 69 uint8_t tlmVersion;
kenjiArai 0:5087fec653b2 70 uint8_t urlDataLength;
kenjiArai 0:5087fec653b2 71 UrlData_t urlData;
kenjiArai 0:5087fec653b2 72 UIDNamespaceID_t uidNamespaceID;
kenjiArai 0:5087fec653b2 73 UIDInstanceID_t uidInstanceID;
kenjiArai 0:5087fec653b2 74 };
kenjiArai 0:5087fec653b2 75
kenjiArai 0:5087fec653b2 76 enum EddystoneError_t {
kenjiArai 0:5087fec653b2 77 EDDYSTONE_ERROR_NONE,
kenjiArai 0:5087fec653b2 78 EDDYSTONE_ERROR_INVALID_BEACON_PERIOD,
kenjiArai 0:5087fec653b2 79 EDDYSTONE_ERROR_INVALID_CONSEC_FRAMES,
kenjiArai 0:5087fec653b2 80 EDDYSTONE_ERROR_INVALID_ADVERTISING_INTERVAL
kenjiArai 0:5087fec653b2 81 };
kenjiArai 0:5087fec653b2 82
kenjiArai 0:5087fec653b2 83 enum FrameType {
kenjiArai 0:5087fec653b2 84 EDDYSTONE_FRAME_URL,
kenjiArai 0:5087fec653b2 85 EDDYSTONE_FRAME_UID,
kenjiArai 0:5087fec653b2 86 EDDYSTONE_FRAME_TLM,
kenjiArai 0:5087fec653b2 87 NUM_EDDYSTONE_FRAMES
kenjiArai 0:5087fec653b2 88 };
kenjiArai 0:5087fec653b2 89
kenjiArai 0:5087fec653b2 90 /* Initialise the EddystoneService using parameters from persistent storage */
kenjiArai 0:5087fec653b2 91 EddystoneService(BLE &bleIn,
kenjiArai 0:5087fec653b2 92 EddystoneParams_t &paramsIn,
kenjiArai 0:5087fec653b2 93 const PowerLevels_t &advPowerLevelsIn,
kenjiArai 0:5087fec653b2 94 const PowerLevels_t &radioPowerLevelsIn,
kenjiArai 0:5087fec653b2 95 uint32_t advConfigIntervalIn = DEFAULT_CONFIG_PERIOD_MSEC);
kenjiArai 0:5087fec653b2 96
kenjiArai 0:5087fec653b2 97 /* When using this constructor we need to call setURLData,
kenjiArai 0:5087fec653b2 98 * setTMLData and setUIDData to initialise values manually
kenjiArai 0:5087fec653b2 99 */
kenjiArai 0:5087fec653b2 100 EddystoneService(BLE &bleIn,
kenjiArai 0:5087fec653b2 101 const PowerLevels_t &advPowerLevelsIn,
kenjiArai 0:5087fec653b2 102 const PowerLevels_t &radioPowerLevelsIn,
kenjiArai 0:5087fec653b2 103 uint32_t advConfigIntervalIn = DEFAULT_CONFIG_PERIOD_MSEC);
kenjiArai 0:5087fec653b2 104
kenjiArai 0:5087fec653b2 105 /* Setup callback to update BatteryVoltage in TLM frame */
kenjiArai 0:5087fec653b2 106 void onTLMBatteryVoltageUpdate(TlmUpdateCallback_t tlmBatteryVoltageCallbackIn);
kenjiArai 0:5087fec653b2 107
kenjiArai 0:5087fec653b2 108 /* Setup callback to update BeaconTemperature in TLM frame */
kenjiArai 0:5087fec653b2 109 void onTLMBeaconTemperatureUpdate(TlmUpdateCallback_t tlmBeaconTemperatureCallbackIn);
kenjiArai 0:5087fec653b2 110
kenjiArai 0:5087fec653b2 111 void setTLMData(uint8_t tlmVersionIn = 0);
kenjiArai 0:5087fec653b2 112
kenjiArai 0:5087fec653b2 113 void setURLData(const char *urlDataIn);
kenjiArai 0:5087fec653b2 114
kenjiArai 0:5087fec653b2 115 void setUIDData(const UIDNamespaceID_t *uidNamespaceIDIn, const UIDInstanceID_t *uidInstanceIDIn);
kenjiArai 0:5087fec653b2 116
kenjiArai 0:5087fec653b2 117 EddystoneError_t startConfigService(void);
kenjiArai 0:5087fec653b2 118
kenjiArai 0:5087fec653b2 119 EddystoneError_t startBeaconService(uint16_t consecUrlFramesIn = 2, uint16_t consecUidFramesIn = 2, uint16_t consecTlmFramesIn = 2);
kenjiArai 0:5087fec653b2 120
kenjiArai 0:5087fec653b2 121 /* It is not the responsibility of the Eddystone implementation to store
kenjiArai 0:5087fec653b2 122 * the configured parameters in persistent storage since this is
kenjiArai 0:5087fec653b2 123 * platform-specific. So we provide this function that returns the
kenjiArai 0:5087fec653b2 124 * configured values that need to be stored and the main application
kenjiArai 0:5087fec653b2 125 * takes care of storing them.
kenjiArai 0:5087fec653b2 126 */
kenjiArai 0:5087fec653b2 127 void getEddystoneParams(EddystoneParams_t *params);
kenjiArai 0:5087fec653b2 128
kenjiArai 0:5087fec653b2 129 private:
kenjiArai 0:5087fec653b2 130 /* Helper function used only once during constructing the object to avoid
kenjiArai 0:5087fec653b2 131 * duplicated code.
kenjiArai 0:5087fec653b2 132 */
kenjiArai 0:5087fec653b2 133 void eddystoneConstructorHelper(const PowerLevels_t &advPowerLevelsIn,
kenjiArai 0:5087fec653b2 134 const PowerLevels_t &radioPowerLevelsIn,
kenjiArai 0:5087fec653b2 135 uint32_t advConfigIntervalIn);
kenjiArai 0:5087fec653b2 136
kenjiArai 0:5087fec653b2 137 /* When changing modes, we shutdown and init the BLE instance, so
kenjiArai 0:5087fec653b2 138 * this is needed to complete the initialisation task.
kenjiArai 0:5087fec653b2 139 */
kenjiArai 0:5087fec653b2 140 void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext);
kenjiArai 0:5087fec653b2 141
kenjiArai 0:5087fec653b2 142 void swapAdvertisedFrame(void);
kenjiArai 0:5087fec653b2 143
kenjiArai 0:5087fec653b2 144 void updateAdvertisementPacket(const uint8_t* rawFrame, size_t rawFrameLength);
kenjiArai 0:5087fec653b2 145
kenjiArai 0:5087fec653b2 146 /* Helper function that calls user-defined functions to update Battery Voltage and Temperature (if available),
kenjiArai 0:5087fec653b2 147 * then updates the raw frame data and finally updates the actual advertised packet. This operation must be
kenjiArai 0:5087fec653b2 148 * done fairly often because the TLM frame TimeSinceBoot must have a 0.1 secs resolution according to the
kenjiArai 0:5087fec653b2 149 * Eddystone specification.
kenjiArai 0:5087fec653b2 150 */
kenjiArai 0:5087fec653b2 151 void updateRawTLMFrame(void);
kenjiArai 0:5087fec653b2 152
kenjiArai 0:5087fec653b2 153 void setupBeaconService(void);
kenjiArai 0:5087fec653b2 154
kenjiArai 0:5087fec653b2 155 void setupConfigService(void);
kenjiArai 0:5087fec653b2 156
kenjiArai 0:5087fec653b2 157 void freeConfigCharacteristics(void);
kenjiArai 0:5087fec653b2 158
kenjiArai 0:5087fec653b2 159 void freeBeaconFrames(void);
kenjiArai 0:5087fec653b2 160
kenjiArai 0:5087fec653b2 161 void radioNotificationCallback(bool radioActive);
kenjiArai 0:5087fec653b2 162
kenjiArai 0:5087fec653b2 163 /*
kenjiArai 0:5087fec653b2 164 * Internal helper function used to update the GATT database following any
kenjiArai 0:5087fec653b2 165 * change to the internal state of the service object.
kenjiArai 0:5087fec653b2 166 */
kenjiArai 0:5087fec653b2 167 void updateCharacteristicValues(void);
kenjiArai 0:5087fec653b2 168
kenjiArai 0:5087fec653b2 169 void setupEddystoneConfigAdvertisements(void);
kenjiArai 0:5087fec653b2 170
kenjiArai 0:5087fec653b2 171 void lockAuthorizationCallback(GattWriteAuthCallbackParams *authParams);
kenjiArai 0:5087fec653b2 172
kenjiArai 0:5087fec653b2 173 void unlockAuthorizationCallback(GattWriteAuthCallbackParams *authParams);
kenjiArai 0:5087fec653b2 174
kenjiArai 0:5087fec653b2 175 void urlDataWriteAuthorizationCallback(GattWriteAuthCallbackParams *authParams);
kenjiArai 0:5087fec653b2 176
kenjiArai 0:5087fec653b2 177 void powerModeAuthorizationCallback(GattWriteAuthCallbackParams *authParams);
kenjiArai 0:5087fec653b2 178
kenjiArai 0:5087fec653b2 179 template <typename T>
kenjiArai 0:5087fec653b2 180 void basicAuthorizationCallback(GattWriteAuthCallbackParams *authParams);
kenjiArai 0:5087fec653b2 181
kenjiArai 0:5087fec653b2 182 /*
kenjiArai 0:5087fec653b2 183 * This callback is invoked when a GATT client attempts to modify any of the
kenjiArai 0:5087fec653b2 184 * characteristics of this service. Attempts to do so are also applied to
kenjiArai 0:5087fec653b2 185 * the internal state of this service object.
kenjiArai 0:5087fec653b2 186 */
kenjiArai 0:5087fec653b2 187 void onDataWrittenCallback(const GattWriteCallbackParams *writeParams);
kenjiArai 0:5087fec653b2 188
kenjiArai 0:5087fec653b2 189 uint16_t correctAdvertisementPeriod(uint16_t beaconPeriodIn) const;
kenjiArai 0:5087fec653b2 190
kenjiArai 0:5087fec653b2 191 BLE &ble;
kenjiArai 0:5087fec653b2 192 uint32_t advConfigInterval;
kenjiArai 0:5087fec653b2 193 uint8_t operationMode;
kenjiArai 0:5087fec653b2 194
kenjiArai 0:5087fec653b2 195 URLFrame urlFrame;
kenjiArai 0:5087fec653b2 196 UIDFrame uidFrame;
kenjiArai 0:5087fec653b2 197 TLMFrame tlmFrame;
kenjiArai 0:5087fec653b2 198
kenjiArai 0:5087fec653b2 199 PowerLevels_t radioPowerLevels;
kenjiArai 0:5087fec653b2 200 PowerLevels_t advPowerLevels;
kenjiArai 0:5087fec653b2 201 bool lockState;
kenjiArai 0:5087fec653b2 202 bool resetFlag;
kenjiArai 0:5087fec653b2 203 Lock_t lock;
kenjiArai 0:5087fec653b2 204 Lock_t unlock;
kenjiArai 0:5087fec653b2 205 uint8_t flags;
kenjiArai 0:5087fec653b2 206 uint8_t txPowerMode;
kenjiArai 0:5087fec653b2 207 uint16_t beaconPeriod;
kenjiArai 0:5087fec653b2 208
kenjiArai 0:5087fec653b2 209 ReadOnlyGattCharacteristic<bool> *lockStateChar;
kenjiArai 0:5087fec653b2 210 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(Lock_t)> *lockChar;
kenjiArai 0:5087fec653b2 211 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(Lock_t)> *unlockChar;
kenjiArai 0:5087fec653b2 212 GattCharacteristic *urlDataChar;
kenjiArai 0:5087fec653b2 213 ReadWriteGattCharacteristic<uint8_t> *flagsChar;
kenjiArai 0:5087fec653b2 214 ReadWriteArrayGattCharacteristic<int8_t, sizeof(PowerLevels_t)> *advPowerLevelsChar;
kenjiArai 0:5087fec653b2 215 ReadWriteGattCharacteristic<uint8_t> *txPowerModeChar;
kenjiArai 0:5087fec653b2 216 ReadWriteGattCharacteristic<uint16_t> *beaconPeriodChar;
kenjiArai 0:5087fec653b2 217 WriteOnlyGattCharacteristic<bool> *resetChar;
kenjiArai 0:5087fec653b2 218
kenjiArai 0:5087fec653b2 219 uint8_t *rawUrlFrame;
kenjiArai 0:5087fec653b2 220 uint8_t *rawUidFrame;
kenjiArai 0:5087fec653b2 221 uint8_t *rawTlmFrame;
kenjiArai 0:5087fec653b2 222
kenjiArai 0:5087fec653b2 223 uint16_t consecFrames[NUM_EDDYSTONE_FRAMES];
kenjiArai 0:5087fec653b2 224 uint16_t currentConsecFrames[NUM_EDDYSTONE_FRAMES];
kenjiArai 0:5087fec653b2 225 uint8_t currentAdvertisedFrame;
kenjiArai 0:5087fec653b2 226
kenjiArai 0:5087fec653b2 227 TlmUpdateCallback_t tlmBatteryVoltageCallback;
kenjiArai 0:5087fec653b2 228 TlmUpdateCallback_t tlmBeaconTemperatureCallback;
kenjiArai 0:5087fec653b2 229
kenjiArai 0:5087fec653b2 230 Timer timeSinceBootTimer;
kenjiArai 0:5087fec653b2 231 #ifndef YOTTA_CFG_MBED_OS
kenjiArai 0:5087fec653b2 232 Timeout swapAdvertisedFrameTimeout;
kenjiArai 0:5087fec653b2 233 #endif
kenjiArai 0:5087fec653b2 234
kenjiArai 0:5087fec653b2 235 GattCharacteristic *charTable[TOTAL_CHARACTERISTICS];
kenjiArai 0:5087fec653b2 236 };
kenjiArai 0:5087fec653b2 237
kenjiArai 0:5087fec653b2 238 #endif /* __EDDYSTONESERVICE_H__ */