Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /* mbed Microcontroller Library
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2006-2013 ARM Limited
nexpaq 0:6c56fb4bc5f0 3 *
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the "License");
nexpaq 0:6c56fb4bc5f0 5 * you may not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an "AS IS" BASIS,
nexpaq 0:6c56fb4bc5f0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16
nexpaq 0:6c56fb4bc5f0 17 #ifndef SERVICES_EDDYSTONE_BEACON_CONFIG_SERVICE_H_
nexpaq 0:6c56fb4bc5f0 18 #define SERVICES_EDDYSTONE_BEACON_CONFIG_SERVICE_H_
nexpaq 0:6c56fb4bc5f0 19
nexpaq 0:6c56fb4bc5f0 20 #warning ble/services/EddystoneConfigService.h is deprecated. Please use the example in 'github.com/ARMmbed/ble-examples/tree/master/BLE_EddystoneService'.
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 #include "mbed.h"
nexpaq 0:6c56fb4bc5f0 23 #include "ble/BLE.h"
nexpaq 0:6c56fb4bc5f0 24 #include "ble/services/EddystoneService.h"
nexpaq 0:6c56fb4bc5f0 25
nexpaq 0:6c56fb4bc5f0 26 #define UUID_URI_BEACON(FIRST, SECOND) { \
nexpaq 0:6c56fb4bc5f0 27 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \
nexpaq 0:6c56fb4bc5f0 28 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \
nexpaq 0:6c56fb4bc5f0 29 }
nexpaq 0:6c56fb4bc5f0 30
nexpaq 0:6c56fb4bc5f0 31 static const uint8_t UUID_URI_BEACON_SERVICE[] = UUID_URI_BEACON(0x20, 0x80);
nexpaq 0:6c56fb4bc5f0 32 static const uint8_t UUID_LOCK_STATE_CHAR[] = UUID_URI_BEACON(0x20, 0x81);
nexpaq 0:6c56fb4bc5f0 33 static const uint8_t UUID_LOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x82);
nexpaq 0:6c56fb4bc5f0 34 static const uint8_t UUID_UNLOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x83);
nexpaq 0:6c56fb4bc5f0 35 static const uint8_t UUID_URI_DATA_CHAR[] = UUID_URI_BEACON(0x20, 0x84);
nexpaq 0:6c56fb4bc5f0 36 static const uint8_t UUID_FLAGS_CHAR[] = UUID_URI_BEACON(0x20, 0x85);
nexpaq 0:6c56fb4bc5f0 37 static const uint8_t UUID_ADV_POWER_LEVELS_CHAR[] = UUID_URI_BEACON(0x20, 0x86);
nexpaq 0:6c56fb4bc5f0 38 static const uint8_t UUID_TX_POWER_MODE_CHAR[] = UUID_URI_BEACON(0x20, 0x87);
nexpaq 0:6c56fb4bc5f0 39 static const uint8_t UUID_BEACON_PERIOD_CHAR[] = UUID_URI_BEACON(0x20, 0x88);
nexpaq 0:6c56fb4bc5f0 40 static const uint8_t UUID_RESET_CHAR[] = UUID_URI_BEACON(0x20, 0x89);
nexpaq 0:6c56fb4bc5f0 41 extern const uint8_t BEACON_EDDYSTONE[2];
nexpaq 0:6c56fb4bc5f0 42
nexpaq 0:6c56fb4bc5f0 43 /**
nexpaq 0:6c56fb4bc5f0 44 * @class EddystoneConfigService
nexpaq 0:6c56fb4bc5f0 45 * @brief Eddystone Configuration Service. Used to set URL, adjust power levels, and set flags.
nexpaq 0:6c56fb4bc5f0 46 * See https://github.com/google/eddystone
nexpaq 0:6c56fb4bc5f0 47 *
nexpaq 0:6c56fb4bc5f0 48 */
nexpaq 0:6c56fb4bc5f0 49 class EddystoneConfigService
nexpaq 0:6c56fb4bc5f0 50 {
nexpaq 0:6c56fb4bc5f0 51 public:
nexpaq 0:6c56fb4bc5f0 52 /**
nexpaq 0:6c56fb4bc5f0 53 * @brief Transmission Power Modes for UriBeacon
nexpaq 0:6c56fb4bc5f0 54 */
nexpaq 0:6c56fb4bc5f0 55 enum {
nexpaq 0:6c56fb4bc5f0 56 TX_POWER_MODE_LOWEST,
nexpaq 0:6c56fb4bc5f0 57 TX_POWER_MODE_LOW,
nexpaq 0:6c56fb4bc5f0 58 TX_POWER_MODE_MEDIUM,
nexpaq 0:6c56fb4bc5f0 59 TX_POWER_MODE_HIGH,
nexpaq 0:6c56fb4bc5f0 60 NUM_POWER_MODES
nexpaq 0:6c56fb4bc5f0 61 };
nexpaq 0:6c56fb4bc5f0 62
nexpaq 0:6c56fb4bc5f0 63 static const unsigned ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service.
nexpaq 0:6c56fb4bc5f0 64 static const unsigned SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets.
nexpaq 0:6c56fb4bc5f0 65
nexpaq 0:6c56fb4bc5f0 66 typedef uint8_t Lock_t[16]; /* 128 bits. */
nexpaq 0:6c56fb4bc5f0 67 typedef int8_t PowerLevels_t[NUM_POWER_MODES];
nexpaq 0:6c56fb4bc5f0 68
nexpaq 0:6c56fb4bc5f0 69 // There are currently three subframes defined: URI, UID, and TLM.
nexpaq 0:6c56fb4bc5f0 70 #define EDDYSTONE_MAX_FRAMETYPE 3
nexpaq 0:6c56fb4bc5f0 71 static const unsigned URI_DATA_MAX = 18;
nexpaq 0:6c56fb4bc5f0 72 typedef uint8_t UriData_t[URI_DATA_MAX];
nexpaq 0:6c56fb4bc5f0 73
nexpaq 0:6c56fb4bc5f0 74 // UID Frame Type subfields.
nexpaq 0:6c56fb4bc5f0 75 static const size_t UID_NAMESPACEID_SIZE = 10;
nexpaq 0:6c56fb4bc5f0 76 typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE];
nexpaq 0:6c56fb4bc5f0 77 static const size_t UID_INSTANCEID_SIZE = 6;
nexpaq 0:6c56fb4bc5f0 78 typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE];
nexpaq 0:6c56fb4bc5f0 79
nexpaq 0:6c56fb4bc5f0 80 // Eddystone Frame Type ID.
nexpaq 0:6c56fb4bc5f0 81 static const uint8_t FRAME_TYPE_UID = 0x00;
nexpaq 0:6c56fb4bc5f0 82 static const uint8_t FRAME_TYPE_URL = 0x10;
nexpaq 0:6c56fb4bc5f0 83 static const uint8_t FRAME_TYPE_TLM = 0x20;
nexpaq 0:6c56fb4bc5f0 84
nexpaq 0:6c56fb4bc5f0 85 static const uint8_t FRAME_SIZE_TLM = 14; // TLM frame is a constant 14B.
nexpaq 0:6c56fb4bc5f0 86 static const uint8_t FRAME_SIZE_UID = 20; // includes RFU bytes.
nexpaq 0:6c56fb4bc5f0 87
nexpaq 0:6c56fb4bc5f0 88 struct Params_t {
nexpaq 0:6c56fb4bc5f0 89 // Config Data
nexpaq 0:6c56fb4bc5f0 90 bool isConfigured; // Flag for configuration being complete:
nexpaq 0:6c56fb4bc5f0 91 // True = configured, False = not configured. Reset at instantiation, used for external callbacks.
nexpaq 0:6c56fb4bc5f0 92 uint8_t lockedState;
nexpaq 0:6c56fb4bc5f0 93 Lock_t lock;
nexpaq 0:6c56fb4bc5f0 94 uint8_t flags;
nexpaq 0:6c56fb4bc5f0 95 PowerLevels_t advPowerLevels; // Current value of AdvertisedPowerLevels.
nexpaq 0:6c56fb4bc5f0 96 uint8_t txPowerMode; // Firmware power levels used with setTxPower().
nexpaq 0:6c56fb4bc5f0 97 uint16_t beaconPeriod;
nexpaq 0:6c56fb4bc5f0 98 // TLM Frame Data
nexpaq 0:6c56fb4bc5f0 99 uint8_t tlmVersion; // Version of TLM packet.
nexpaq 0:6c56fb4bc5f0 100 bool tlmEnabled;
nexpaq 0:6c56fb4bc5f0 101 float tlmBeaconPeriod; // How often to broadcat TLM frame, in seconds.
nexpaq 0:6c56fb4bc5f0 102 // URI Frame Data
nexpaq 0:6c56fb4bc5f0 103 uint8_t uriDataLength;
nexpaq 0:6c56fb4bc5f0 104 UriData_t uriData;
nexpaq 0:6c56fb4bc5f0 105 bool uriEnabled;
nexpaq 0:6c56fb4bc5f0 106 float uriBeaconPeriod; // How often to broadcast URIFrame, in seconds.
nexpaq 0:6c56fb4bc5f0 107 // UID Frame Data
nexpaq 0:6c56fb4bc5f0 108 UIDNamespaceID_t uidNamespaceID; // UUID type, Namespace ID, 10B.
nexpaq 0:6c56fb4bc5f0 109 UIDInstanceID_t uidInstanceID; // UUID type, Instance ID, 6B.
nexpaq 0:6c56fb4bc5f0 110 bool uidEnabled;
nexpaq 0:6c56fb4bc5f0 111 float uidBeaconPeriod; // How often to broadcast UID Frame, in seconds.
nexpaq 0:6c56fb4bc5f0 112 };
nexpaq 0:6c56fb4bc5f0 113
nexpaq 0:6c56fb4bc5f0 114 /**
nexpaq 0:6c56fb4bc5f0 115 * @param[ref] ble
nexpaq 0:6c56fb4bc5f0 116 * BLEDevice object for the underlying controller.
nexpaq 0:6c56fb4bc5f0 117 * @param[in/out] paramsIn
nexpaq 0:6c56fb4bc5f0 118 * Reference to application-visible beacon state, loaded
nexpaq 0:6c56fb4bc5f0 119 * from persistent storage at startup.
nexpaq 0:6c56fb4bc5f0 120 * @param[in] defaultAdvPowerLevelsIn
nexpaq 0:6c56fb4bc5f0 121 * Default power-levels array; applies only if resetToDefaultsFlag is true.
nexpaq 0:6c56fb4bc5f0 122 */
nexpaq 0:6c56fb4bc5f0 123 EddystoneConfigService(BLEDevice &bleIn,
nexpaq 0:6c56fb4bc5f0 124 Params_t &paramsIn,
nexpaq 0:6c56fb4bc5f0 125 PowerLevels_t &defaultAdvPowerLevelsIn,
nexpaq 0:6c56fb4bc5f0 126 PowerLevels_t &radioPowerLevelsIn) :
nexpaq 0:6c56fb4bc5f0 127 ble(bleIn),
nexpaq 0:6c56fb4bc5f0 128 params(paramsIn), // Initialize URL data.
nexpaq 0:6c56fb4bc5f0 129 defaultAdvPowerLevels(defaultAdvPowerLevelsIn),
nexpaq 0:6c56fb4bc5f0 130 radioPowerLevels(radioPowerLevelsIn),
nexpaq 0:6c56fb4bc5f0 131 initSucceeded(false),
nexpaq 0:6c56fb4bc5f0 132 resetFlag(),
nexpaq 0:6c56fb4bc5f0 133 defaultUidNamespaceID(), // Initialize UID data.
nexpaq 0:6c56fb4bc5f0 134 defaultUidInstanceID(),
nexpaq 0:6c56fb4bc5f0 135 defaultUidPower(defaultAdvPowerLevelsIn[params.txPowerMode]),
nexpaq 0:6c56fb4bc5f0 136 uidIsSet(false),
nexpaq 0:6c56fb4bc5f0 137 defaultUriDataLength(),
nexpaq 0:6c56fb4bc5f0 138 defaultUriData(),
nexpaq 0:6c56fb4bc5f0 139 defaultUrlPower(defaultAdvPowerLevelsIn[params.txPowerMode]),
nexpaq 0:6c56fb4bc5f0 140 urlIsSet(false),
nexpaq 0:6c56fb4bc5f0 141 tlmIsSet(false),
nexpaq 0:6c56fb4bc5f0 142 lockedStateChar(UUID_LOCK_STATE_CHAR, &params.lockedState),
nexpaq 0:6c56fb4bc5f0 143 lockChar(UUID_LOCK_CHAR, &params.lock),
nexpaq 0:6c56fb4bc5f0 144 uriDataChar(UUID_URI_DATA_CHAR, params.uriData, 0, URI_DATA_MAX,
nexpaq 0:6c56fb4bc5f0 145 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
nexpaq 0:6c56fb4bc5f0 146 unlockChar(UUID_UNLOCK_CHAR, &params.lock),
nexpaq 0:6c56fb4bc5f0 147 flagsChar(UUID_FLAGS_CHAR, &params.flags),
nexpaq 0:6c56fb4bc5f0 148 advPowerLevelsChar(UUID_ADV_POWER_LEVELS_CHAR, &params.advPowerLevels),
nexpaq 0:6c56fb4bc5f0 149 txPowerModeChar(UUID_TX_POWER_MODE_CHAR, &params.txPowerMode),
nexpaq 0:6c56fb4bc5f0 150 beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, &params.beaconPeriod),
nexpaq 0:6c56fb4bc5f0 151 resetChar(UUID_RESET_CHAR, &resetFlag) {
nexpaq 0:6c56fb4bc5f0 152 // Set Eddystone as not configured yet. Used to exit config before timeout if GATT services are written to.
nexpaq 0:6c56fb4bc5f0 153 params.isConfigured = false;
nexpaq 0:6c56fb4bc5f0 154
nexpaq 0:6c56fb4bc5f0 155 lockChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::lockAuthorizationCallback);
nexpaq 0:6c56fb4bc5f0 156 unlockChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::unlockAuthorizationCallback);
nexpaq 0:6c56fb4bc5f0 157 uriDataChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::uriDataWriteAuthorizationCallback);
nexpaq 0:6c56fb4bc5f0 158 flagsChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<uint8_t>);
nexpaq 0:6c56fb4bc5f0 159 advPowerLevelsChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<PowerLevels_t>);
nexpaq 0:6c56fb4bc5f0 160 txPowerModeChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::powerModeAuthorizationCallback);
nexpaq 0:6c56fb4bc5f0 161 beaconPeriodChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<uint16_t>);
nexpaq 0:6c56fb4bc5f0 162 resetChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<uint8_t>);
nexpaq 0:6c56fb4bc5f0 163
nexpaq 0:6c56fb4bc5f0 164 static GattCharacteristic *charTable[] = {
nexpaq 0:6c56fb4bc5f0 165 &lockedStateChar, &lockChar, &unlockChar, &uriDataChar,
nexpaq 0:6c56fb4bc5f0 166 &flagsChar, &advPowerLevelsChar, &txPowerModeChar, &beaconPeriodChar, &resetChar
nexpaq 0:6c56fb4bc5f0 167 };
nexpaq 0:6c56fb4bc5f0 168
nexpaq 0:6c56fb4bc5f0 169 GattService configService(UUID_URI_BEACON_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
nexpaq 0:6c56fb4bc5f0 170
nexpaq 0:6c56fb4bc5f0 171 ble.addService(configService);
nexpaq 0:6c56fb4bc5f0 172 ble.onDataWritten(this, &EddystoneConfigService::onDataWrittenCallback);
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174
nexpaq 0:6c56fb4bc5f0 175 /**
nexpaq 0:6c56fb4bc5f0 176 * @brief Start EddystoneConfig advertising. This function should be called
nexpaq 0:6c56fb4bc5f0 177 * after the EddystoneConfig constructor and after all the frames have been added.
nexpaq 0:6c56fb4bc5f0 178 *
nexpaq 0:6c56fb4bc5f0 179 * @paramsP[in] resetToDefaultsFlag
nexpaq 0:6c56fb4bc5f0 180 * Applies to the state of the 'paramsIn' parameter.
nexpaq 0:6c56fb4bc5f0 181 * If true, it indicates that paramsIn is potentially
nexpaq 0:6c56fb4bc5f0 182 * un-initialized, and default values should be used
nexpaq 0:6c56fb4bc5f0 183 * instead. Otherwise, paramsIn overrides the defaults.
nexpaq 0:6c56fb4bc5f0 184 */
nexpaq 0:6c56fb4bc5f0 185 void start(bool resetToDefaultsFlag){
nexpaq 0:6c56fb4bc5f0 186 INFO("reset to defaults flag = %d", resetToDefaultsFlag);
nexpaq 0:6c56fb4bc5f0 187 if (!resetToDefaultsFlag && (params.uriDataLength > URI_DATA_MAX)) {
nexpaq 0:6c56fb4bc5f0 188 INFO("Reset to Defaults triggered");
nexpaq 0:6c56fb4bc5f0 189 resetToDefaultsFlag = true;
nexpaq 0:6c56fb4bc5f0 190 }
nexpaq 0:6c56fb4bc5f0 191
nexpaq 0:6c56fb4bc5f0 192 if (resetToDefaultsFlag) {
nexpaq 0:6c56fb4bc5f0 193 resetToDefaults();
nexpaq 0:6c56fb4bc5f0 194 } else {
nexpaq 0:6c56fb4bc5f0 195 updateCharacteristicValues();
nexpaq 0:6c56fb4bc5f0 196 }
nexpaq 0:6c56fb4bc5f0 197
nexpaq 0:6c56fb4bc5f0 198 setupEddystoneConfigAdvertisements(); /* Set up advertising for the config service. */
nexpaq 0:6c56fb4bc5f0 199 initSucceeded = true;
nexpaq 0:6c56fb4bc5f0 200 }
nexpaq 0:6c56fb4bc5f0 201
nexpaq 0:6c56fb4bc5f0 202 /*
nexpaq 0:6c56fb4bc5f0 203 * Check if Eddystone initialized successfully.
nexpaq 0:6c56fb4bc5f0 204 */
nexpaq 0:6c56fb4bc5f0 205 bool initSuccessfully(void) const {
nexpaq 0:6c56fb4bc5f0 206 return initSucceeded;
nexpaq 0:6c56fb4bc5f0 207 }
nexpaq 0:6c56fb4bc5f0 208
nexpaq 0:6c56fb4bc5f0 209 /*
nexpaq 0:6c56fb4bc5f0 210 * @brief Function to update the default values for the TLM frame. Only applied if Reset Defaults is applied.
nexpaq 0:6c56fb4bc5f0 211 *
nexpaq 0:6c56fb4bc5f0 212 * @param[in] tlmVersionIn Version of the TLM frame being used.
nexpaq 0:6c56fb4bc5f0 213 * @param[in] advPeriodInMin How long between TLM frames being advertised, measured in minutes.
nexpaq 0:6c56fb4bc5f0 214 *
nexpaq 0:6c56fb4bc5f0 215 */
nexpaq 0:6c56fb4bc5f0 216 void setDefaultTLMFrameData(uint8_t tlmVersionIn = 0, float advPeriodInSec = 60){
nexpaq 0:6c56fb4bc5f0 217 DBG("Setting Default TLM Data, version = %d, advPeriodInMind= %f", tlmVersionIn, advPeriodInSec);
nexpaq 0:6c56fb4bc5f0 218 defaultTlmVersion = tlmVersionIn;
nexpaq 0:6c56fb4bc5f0 219 TlmBatteryVoltage = 0;
nexpaq 0:6c56fb4bc5f0 220 TlmBeaconTemp = 0x8000;
nexpaq 0:6c56fb4bc5f0 221 TlmPduCount = 0;
nexpaq 0:6c56fb4bc5f0 222 TlmTimeSinceBoot = 0;
nexpaq 0:6c56fb4bc5f0 223 defaultTlmAdvPeriod = advPeriodInSec;
nexpaq 0:6c56fb4bc5f0 224 tlmIsSet = true; // Flag to add this to Eddystone service when config is done.
nexpaq 0:6c56fb4bc5f0 225 }
nexpaq 0:6c56fb4bc5f0 226
nexpaq 0:6c56fb4bc5f0 227 /*
nexpaq 0:6c56fb4bc5f0 228 * @brief Function to update the default values for the URI frame. Only applied if Reset Defaults is applied.
nexpaq 0:6c56fb4bc5f0 229 *
nexpaq 0:6c56fb4bc5f0 230 * @param[in] uriIn URL to advertise.
nexpaq 0:6c56fb4bc5f0 231 * @param[in] advPeriod How long to advertise the URL, measured in number of ADV frames.
nexpaq 0:6c56fb4bc5f0 232 *
nexpaq 0:6c56fb4bc5f0 233 */
nexpaq 0:6c56fb4bc5f0 234 void setDefaultURIFrameData(const char *uriIn, float advPeriod = 1){
nexpaq 0:6c56fb4bc5f0 235 DBG("Setting Default URI Data");
nexpaq 0:6c56fb4bc5f0 236 // Set URL Frame
nexpaq 0:6c56fb4bc5f0 237 EddystoneService::encodeURL(uriIn, defaultUriData, defaultUriDataLength); // Encode URL to URL Formatting.
nexpaq 0:6c56fb4bc5f0 238 if (defaultUriDataLength > URI_DATA_MAX) {
nexpaq 0:6c56fb4bc5f0 239 return;
nexpaq 0:6c56fb4bc5f0 240 }
nexpaq 0:6c56fb4bc5f0 241 INFO("\t URI input = %s : %d", uriIn, defaultUriDataLength);
nexpaq 0:6c56fb4bc5f0 242 INFO("\t default URI = %s : %d ", defaultUriData, defaultUriDataLength );
nexpaq 0:6c56fb4bc5f0 243 defaultUriAdvPeriod = advPeriod;
nexpaq 0:6c56fb4bc5f0 244 urlIsSet = true; // Flag to add this to Eddystone service when config is done.
nexpaq 0:6c56fb4bc5f0 245 }
nexpaq 0:6c56fb4bc5f0 246
nexpaq 0:6c56fb4bc5f0 247 /*
nexpaq 0:6c56fb4bc5f0 248 * @brief Function to update the default values for the UID frame. Only applied if Reset Defaults is applied.
nexpaq 0:6c56fb4bc5f0 249 *
nexpaq 0:6c56fb4bc5f0 250 * @param[in] namespaceID 10Byte Namespace ID.
nexpaq 0:6c56fb4bc5f0 251 * @param[in] instanceID 6Byte Instance ID.
nexpaq 0:6c56fb4bc5f0 252 * @param[in] advPeriod How long to advertise the URL, measured in the number of ADV frames.
nexpaq 0:6c56fb4bc5f0 253 *
nexpaq 0:6c56fb4bc5f0 254 */
nexpaq 0:6c56fb4bc5f0 255 void setDefaultUIDFrameData(UIDNamespaceID_t *namespaceID, UIDInstanceID_t *instanceID, float advPeriod = 10){
nexpaq 0:6c56fb4bc5f0 256 //Set UID frame
nexpaq 0:6c56fb4bc5f0 257 DBG("Setting default UID Data");
nexpaq 0:6c56fb4bc5f0 258 memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE);
nexpaq 0:6c56fb4bc5f0 259 memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE);
nexpaq 0:6c56fb4bc5f0 260 defaultUidAdvPeriod = advPeriod;
nexpaq 0:6c56fb4bc5f0 261 uidIsSet = true; // Flag to add this to Eddystone service when config is done.
nexpaq 0:6c56fb4bc5f0 262 }
nexpaq 0:6c56fb4bc5f0 263
nexpaq 0:6c56fb4bc5f0 264 /* Start out by advertising the config service for a limited time after
nexpaq 0:6c56fb4bc5f0 265 * startup, then switch to the normal non-connectible beacon functionality.
nexpaq 0:6c56fb4bc5f0 266 */
nexpaq 0:6c56fb4bc5f0 267 void setupEddystoneConfigAdvertisements() {
nexpaq 0:6c56fb4bc5f0 268 const char DEVICE_NAME[] = "eddystone Config";
nexpaq 0:6c56fb4bc5f0 269
nexpaq 0:6c56fb4bc5f0 270 ble.clearAdvertisingPayload();
nexpaq 0:6c56fb4bc5f0 271
nexpaq 0:6c56fb4bc5f0 272 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
nexpaq 0:6c56fb4bc5f0 273
nexpaq 0:6c56fb4bc5f0 274 // UUID is in a different order in the ADV frame (!)
nexpaq 0:6c56fb4bc5f0 275 uint8_t reversedServiceUUID[sizeof(UUID_URI_BEACON_SERVICE)];
nexpaq 0:6c56fb4bc5f0 276 for (unsigned int i = 0; i < sizeof(UUID_URI_BEACON_SERVICE); i++) {
nexpaq 0:6c56fb4bc5f0 277 reversedServiceUUID[i] = UUID_URI_BEACON_SERVICE[sizeof(UUID_URI_BEACON_SERVICE) - i - 1];
nexpaq 0:6c56fb4bc5f0 278 }
nexpaq 0:6c56fb4bc5f0 279 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, reversedServiceUUID, sizeof(reversedServiceUUID));
nexpaq 0:6c56fb4bc5f0 280 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG);
nexpaq 0:6c56fb4bc5f0 281 ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, reinterpret_cast<const uint8_t *>(&DEVICE_NAME), sizeof(DEVICE_NAME));
nexpaq 0:6c56fb4bc5f0 282 ble.accumulateScanResponse(
nexpaq 0:6c56fb4bc5f0 283 GapAdvertisingData::TX_POWER_LEVEL,
nexpaq 0:6c56fb4bc5f0 284 reinterpret_cast<uint8_t *>(&defaultAdvPowerLevels[EddystoneConfigService::TX_POWER_MODE_LOW]),
nexpaq 0:6c56fb4bc5f0 285 sizeof(uint8_t));
nexpaq 0:6c56fb4bc5f0 286
nexpaq 0:6c56fb4bc5f0 287 ble.setTxPower(radioPowerLevels[params.txPowerMode]);
nexpaq 0:6c56fb4bc5f0 288 ble.setDeviceName(reinterpret_cast<const uint8_t *>(&DEVICE_NAME));
nexpaq 0:6c56fb4bc5f0 289 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
nexpaq 0:6c56fb4bc5f0 290 ble.setAdvertisingInterval(ADVERTISING_INTERVAL_MSEC);
nexpaq 0:6c56fb4bc5f0 291 }
nexpaq 0:6c56fb4bc5f0 292
nexpaq 0:6c56fb4bc5f0 293 /*
nexpaq 0:6c56fb4bc5f0 294 * This function actually impliments the Eddystone Beacon service. It can be called with the help of the wrapper function
nexpaq 0:6c56fb4bc5f0 295 * to load saved config params, or it can be called explicitly to reset the Eddystone beacon to hardcoded values on each reset.
nexpaq 0:6c56fb4bc5f0 296 *
nexpaq 0:6c56fb4bc5f0 297 */
nexpaq 0:6c56fb4bc5f0 298 void setupEddystoneAdvertisements() {
nexpaq 0:6c56fb4bc5f0 299 DBG("Switching Config -> adv");
nexpaq 0:6c56fb4bc5f0 300 // Save params to storage.
nexpaq 0:6c56fb4bc5f0 301 extern void saveURIBeaconConfigParams(const Params_t *paramsP); /* forward declaration; necessary to avoid a circular dependency. */
nexpaq 0:6c56fb4bc5f0 302 saveURIBeaconConfigParams(&params);
nexpaq 0:6c56fb4bc5f0 303 INFO("Saved Params to Memory.")
nexpaq 0:6c56fb4bc5f0 304 // Set up Eddystone Service.
nexpaq 0:6c56fb4bc5f0 305 static EddystoneService eddyServ(ble, params.beaconPeriod, radioPowerLevels[params.txPowerMode]);
nexpaq 0:6c56fb4bc5f0 306 // Set configured frames (TLM, UID, URI and so on).
nexpaq 0:6c56fb4bc5f0 307 if (params.tlmEnabled) {
nexpaq 0:6c56fb4bc5f0 308 eddyServ.setTLMFrameData(params.tlmVersion, params.tlmBeaconPeriod);
nexpaq 0:6c56fb4bc5f0 309 }
nexpaq 0:6c56fb4bc5f0 310 if (params.uriEnabled) {
nexpaq 0:6c56fb4bc5f0 311 eddyServ.setURLFrameEncodedData(params.advPowerLevels[params.txPowerMode], (const char *) params.uriData, params.uriDataLength, params.uriBeaconPeriod);
nexpaq 0:6c56fb4bc5f0 312 }
nexpaq 0:6c56fb4bc5f0 313 if (params.uidEnabled) {
nexpaq 0:6c56fb4bc5f0 314 eddyServ.setUIDFrameData(params.advPowerLevels[params.txPowerMode],
nexpaq 0:6c56fb4bc5f0 315 (uint8_t *)params.uidNamespaceID,
nexpaq 0:6c56fb4bc5f0 316 (uint8_t *)params.uidInstanceID,
nexpaq 0:6c56fb4bc5f0 317 params.uidBeaconPeriod);
nexpaq 0:6c56fb4bc5f0 318 }
nexpaq 0:6c56fb4bc5f0 319 // Start advertising the Eddystone service.
nexpaq 0:6c56fb4bc5f0 320 eddyServ.start();
nexpaq 0:6c56fb4bc5f0 321 }
nexpaq 0:6c56fb4bc5f0 322
nexpaq 0:6c56fb4bc5f0 323 private:
nexpaq 0:6c56fb4bc5f0 324 /*
nexpaq 0:6c56fb4bc5f0 325 * This callback is invoked when a GATT client attempts to modify any of the
nexpaq 0:6c56fb4bc5f0 326 * characteristics of this service. Attempts to do so are also applied to
nexpaq 0:6c56fb4bc5f0 327 * the internal state of this service object.
nexpaq 0:6c56fb4bc5f0 328 */
nexpaq 0:6c56fb4bc5f0 329 void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) {
nexpaq 0:6c56fb4bc5f0 330 uint16_t handle = writeParams->handle;
nexpaq 0:6c56fb4bc5f0 331
nexpaq 0:6c56fb4bc5f0 332 if (handle == lockChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 333 // Validated earlier.
nexpaq 0:6c56fb4bc5f0 334 memcpy(params.lock, writeParams->data, sizeof(Lock_t));
nexpaq 0:6c56fb4bc5f0 335 // Set the state to be locked by the lock code (note: zeros are a valid lock).
nexpaq 0:6c56fb4bc5f0 336 params.lockedState = true;
nexpaq 0:6c56fb4bc5f0 337 INFO("Device Locked");
nexpaq 0:6c56fb4bc5f0 338 } else if (handle == unlockChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 339 // Validated earlier.
nexpaq 0:6c56fb4bc5f0 340 params.lockedState = false;
nexpaq 0:6c56fb4bc5f0 341 INFO("Device Unlocked");
nexpaq 0:6c56fb4bc5f0 342 } else if (handle == uriDataChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 343 params.uriDataLength = writeParams->len;
nexpaq 0:6c56fb4bc5f0 344 memset(params.uriData, 0x00, URI_DATA_MAX); // Clear URI string.
nexpaq 0:6c56fb4bc5f0 345 memcpy(params.uriData, writeParams->data, writeParams->len); // Set URI string.
nexpaq 0:6c56fb4bc5f0 346 params.uriEnabled = true;
nexpaq 0:6c56fb4bc5f0 347 INFO("URI = %s, URILen = %d", writeParams->data, writeParams->len);
nexpaq 0:6c56fb4bc5f0 348 } else if (handle == flagsChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 349 params.flags = *(writeParams->data);
nexpaq 0:6c56fb4bc5f0 350 INFO("flagsChar = 0x%x", params.flags);
nexpaq 0:6c56fb4bc5f0 351 } else if (handle == advPowerLevelsChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 352 memcpy(params.advPowerLevels, writeParams->data, sizeof(PowerLevels_t));
nexpaq 0:6c56fb4bc5f0 353 INFO("PowerLevelsChar = %4x", params.advPowerLevels);
nexpaq 0:6c56fb4bc5f0 354 } else if (handle == txPowerModeChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 355 params.txPowerMode = *(writeParams->data);
nexpaq 0:6c56fb4bc5f0 356 INFO("TxPowerModeChar = %d", params.txPowerMode);
nexpaq 0:6c56fb4bc5f0 357 } else if (handle == beaconPeriodChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 358 params.beaconPeriod = *((uint16_t *)(writeParams->data));
nexpaq 0:6c56fb4bc5f0 359 INFO("BeaconPeriod = %d", params.beaconPeriod);
nexpaq 0:6c56fb4bc5f0 360
nexpaq 0:6c56fb4bc5f0 361 /* Re-map beaconPeriod to within permissible bounds if necessary. */
nexpaq 0:6c56fb4bc5f0 362 if (params.beaconPeriod != 0) {
nexpaq 0:6c56fb4bc5f0 363 bool paramsUpdated = false;
nexpaq 0:6c56fb4bc5f0 364 if (params.beaconPeriod < ble.getMinAdvertisingInterval()) {
nexpaq 0:6c56fb4bc5f0 365 params.beaconPeriod = ble.getMinAdvertisingInterval();
nexpaq 0:6c56fb4bc5f0 366 paramsUpdated = true;
nexpaq 0:6c56fb4bc5f0 367 } else if (params.beaconPeriod > ble.getMaxAdvertisingInterval()) {
nexpaq 0:6c56fb4bc5f0 368 params.beaconPeriod = ble.getMaxAdvertisingInterval();
nexpaq 0:6c56fb4bc5f0 369 paramsUpdated = true;
nexpaq 0:6c56fb4bc5f0 370 }
nexpaq 0:6c56fb4bc5f0 371 if (paramsUpdated) {
nexpaq 0:6c56fb4bc5f0 372 ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), reinterpret_cast<uint8_t *>(&params.beaconPeriod), sizeof(uint16_t));
nexpaq 0:6c56fb4bc5f0 373 }
nexpaq 0:6c56fb4bc5f0 374 }
nexpaq 0:6c56fb4bc5f0 375 } else if (handle == resetChar.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 376 INFO("Reset triggered from Config Service, resetting to defaults");
nexpaq 0:6c56fb4bc5f0 377 resetToDefaults();
nexpaq 0:6c56fb4bc5f0 378 }
nexpaq 0:6c56fb4bc5f0 379 updateCharacteristicValues();
nexpaq 0:6c56fb4bc5f0 380 params.isConfigured = true; // Some configuration data has been passed; on disconnect switch to advertising mode.
nexpaq 0:6c56fb4bc5f0 381 }
nexpaq 0:6c56fb4bc5f0 382
nexpaq 0:6c56fb4bc5f0 383 /*
nexpaq 0:6c56fb4bc5f0 384 * Reset the default values.
nexpaq 0:6c56fb4bc5f0 385 */
nexpaq 0:6c56fb4bc5f0 386 void resetToDefaults(void) {
nexpaq 0:6c56fb4bc5f0 387 INFO("Resetting to defaults");
nexpaq 0:6c56fb4bc5f0 388 // General.
nexpaq 0:6c56fb4bc5f0 389 params.lockedState = false;
nexpaq 0:6c56fb4bc5f0 390 memset(params.lock, 0, sizeof(Lock_t));
nexpaq 0:6c56fb4bc5f0 391 params.flags = 0x10;
nexpaq 0:6c56fb4bc5f0 392 memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t));
nexpaq 0:6c56fb4bc5f0 393 params.txPowerMode = TX_POWER_MODE_LOW;
nexpaq 0:6c56fb4bc5f0 394 params.beaconPeriod = (uint16_t) defaultUriAdvPeriod * 1000;
nexpaq 0:6c56fb4bc5f0 395
nexpaq 0:6c56fb4bc5f0 396 // TLM Frame.
nexpaq 0:6c56fb4bc5f0 397 params.tlmVersion = defaultTlmVersion;
nexpaq 0:6c56fb4bc5f0 398 params.tlmBeaconPeriod = defaultTlmAdvPeriod;
nexpaq 0:6c56fb4bc5f0 399 params.tlmEnabled = tlmIsSet;
nexpaq 0:6c56fb4bc5f0 400
nexpaq 0:6c56fb4bc5f0 401 // URL Frame.
nexpaq 0:6c56fb4bc5f0 402 memcpy(params.uriData, defaultUriData, URI_DATA_MAX);
nexpaq 0:6c56fb4bc5f0 403 params.uriDataLength = defaultUriDataLength;
nexpaq 0:6c56fb4bc5f0 404 params.uriBeaconPeriod = defaultUriAdvPeriod;
nexpaq 0:6c56fb4bc5f0 405 params.uriEnabled = urlIsSet;
nexpaq 0:6c56fb4bc5f0 406
nexpaq 0:6c56fb4bc5f0 407 // UID Frame.
nexpaq 0:6c56fb4bc5f0 408 memcpy(params.uidNamespaceID, defaultUidNamespaceID, UID_NAMESPACEID_SIZE);
nexpaq 0:6c56fb4bc5f0 409 memcpy(params.uidInstanceID, defaultUidInstanceID, UID_INSTANCEID_SIZE);
nexpaq 0:6c56fb4bc5f0 410 params.uidBeaconPeriod = defaultUidAdvPeriod;
nexpaq 0:6c56fb4bc5f0 411 params.uidEnabled = uidIsSet;
nexpaq 0:6c56fb4bc5f0 412
nexpaq 0:6c56fb4bc5f0 413 updateCharacteristicValues();
nexpaq 0:6c56fb4bc5f0 414 }
nexpaq 0:6c56fb4bc5f0 415
nexpaq 0:6c56fb4bc5f0 416 /*
nexpaq 0:6c56fb4bc5f0 417 * Internal helper function used to update the GATT database following any
nexpaq 0:6c56fb4bc5f0 418 * change to the internal state of the service object.
nexpaq 0:6c56fb4bc5f0 419 */
nexpaq 0:6c56fb4bc5f0 420 void updateCharacteristicValues(void) {
nexpaq 0:6c56fb4bc5f0 421 ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), &params.lockedState, 1);
nexpaq 0:6c56fb4bc5f0 422 ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength);
nexpaq 0:6c56fb4bc5f0 423 ble.updateCharacteristicValue(flagsChar.getValueHandle(), &params.flags, 1);
nexpaq 0:6c56fb4bc5f0 424 ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(),
nexpaq 0:6c56fb4bc5f0 425 reinterpret_cast<uint8_t *>(&params.beaconPeriod), sizeof(uint16_t));
nexpaq 0:6c56fb4bc5f0 426 ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), &params.txPowerMode, 1);
nexpaq 0:6c56fb4bc5f0 427 ble.updateCharacteristicValue(advPowerLevelsChar.getValueHandle(),
nexpaq 0:6c56fb4bc5f0 428 reinterpret_cast<uint8_t *>(params.advPowerLevels), sizeof(PowerLevels_t));
nexpaq 0:6c56fb4bc5f0 429 }
nexpaq 0:6c56fb4bc5f0 430
nexpaq 0:6c56fb4bc5f0 431 private:
nexpaq 0:6c56fb4bc5f0 432 void lockAuthorizationCallback(GattWriteAuthCallbackParams *authParams) {
nexpaq 0:6c56fb4bc5f0 433 if (params.lockedState) {
nexpaq 0:6c56fb4bc5f0 434 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION;
nexpaq 0:6c56fb4bc5f0 435 } else if (authParams->len != sizeof(Lock_t)) {
nexpaq 0:6c56fb4bc5f0 436 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH;
nexpaq 0:6c56fb4bc5f0 437 } else if (authParams->offset != 0) {
nexpaq 0:6c56fb4bc5f0 438 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET;
nexpaq 0:6c56fb4bc5f0 439 } else {
nexpaq 0:6c56fb4bc5f0 440 authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
nexpaq 0:6c56fb4bc5f0 441 }
nexpaq 0:6c56fb4bc5f0 442 }
nexpaq 0:6c56fb4bc5f0 443
nexpaq 0:6c56fb4bc5f0 444 void unlockAuthorizationCallback(GattWriteAuthCallbackParams *authParams) {
nexpaq 0:6c56fb4bc5f0 445 if ((!params.lockedState) && (authParams->len == sizeof(Lock_t))) {
nexpaq 0:6c56fb4bc5f0 446 authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
nexpaq 0:6c56fb4bc5f0 447 } else if (authParams->len != sizeof(Lock_t)) {
nexpaq 0:6c56fb4bc5f0 448 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH;
nexpaq 0:6c56fb4bc5f0 449 } else if (authParams->offset != 0) {
nexpaq 0:6c56fb4bc5f0 450 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET;
nexpaq 0:6c56fb4bc5f0 451 } else if (memcmp(authParams->data, params.lock, sizeof(Lock_t)) != 0) {
nexpaq 0:6c56fb4bc5f0 452 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION;
nexpaq 0:6c56fb4bc5f0 453 } else {
nexpaq 0:6c56fb4bc5f0 454 authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
nexpaq 0:6c56fb4bc5f0 455 }
nexpaq 0:6c56fb4bc5f0 456 }
nexpaq 0:6c56fb4bc5f0 457
nexpaq 0:6c56fb4bc5f0 458 void uriDataWriteAuthorizationCallback(GattWriteAuthCallbackParams *authParams) {
nexpaq 0:6c56fb4bc5f0 459 if (params.lockedState) {
nexpaq 0:6c56fb4bc5f0 460 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION;
nexpaq 0:6c56fb4bc5f0 461 } else if (authParams->offset != 0) {
nexpaq 0:6c56fb4bc5f0 462 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET;
nexpaq 0:6c56fb4bc5f0 463 } else {
nexpaq 0:6c56fb4bc5f0 464 authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
nexpaq 0:6c56fb4bc5f0 465 }
nexpaq 0:6c56fb4bc5f0 466 }
nexpaq 0:6c56fb4bc5f0 467
nexpaq 0:6c56fb4bc5f0 468 void powerModeAuthorizationCallback(GattWriteAuthCallbackParams *authParams) {
nexpaq 0:6c56fb4bc5f0 469 if (params.lockedState) {
nexpaq 0:6c56fb4bc5f0 470 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION;
nexpaq 0:6c56fb4bc5f0 471 } else if (authParams->len != sizeof(uint8_t)) {
nexpaq 0:6c56fb4bc5f0 472 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH;
nexpaq 0:6c56fb4bc5f0 473 } else if (authParams->offset != 0) {
nexpaq 0:6c56fb4bc5f0 474 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET;
nexpaq 0:6c56fb4bc5f0 475 } else if (*((uint8_t *)authParams->data) >= NUM_POWER_MODES) {
nexpaq 0:6c56fb4bc5f0 476 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED;
nexpaq 0:6c56fb4bc5f0 477 } else {
nexpaq 0:6c56fb4bc5f0 478 authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
nexpaq 0:6c56fb4bc5f0 479 }
nexpaq 0:6c56fb4bc5f0 480 }
nexpaq 0:6c56fb4bc5f0 481
nexpaq 0:6c56fb4bc5f0 482 template <typename T>
nexpaq 0:6c56fb4bc5f0 483 void basicAuthorizationCallback(GattWriteAuthCallbackParams *authParams) {
nexpaq 0:6c56fb4bc5f0 484 if (params.lockedState) {
nexpaq 0:6c56fb4bc5f0 485 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION;
nexpaq 0:6c56fb4bc5f0 486 } else if (authParams->len != sizeof(T)) {
nexpaq 0:6c56fb4bc5f0 487 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH;
nexpaq 0:6c56fb4bc5f0 488 } else if (authParams->offset != 0) {
nexpaq 0:6c56fb4bc5f0 489 authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET;
nexpaq 0:6c56fb4bc5f0 490 } else {
nexpaq 0:6c56fb4bc5f0 491 authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
nexpaq 0:6c56fb4bc5f0 492 }
nexpaq 0:6c56fb4bc5f0 493 }
nexpaq 0:6c56fb4bc5f0 494
nexpaq 0:6c56fb4bc5f0 495 BLEDevice &ble;
nexpaq 0:6c56fb4bc5f0 496 Params_t &params;
nexpaq 0:6c56fb4bc5f0 497 Ticker timeSinceBootTick;
nexpaq 0:6c56fb4bc5f0 498 Timeout switchFrame;
nexpaq 0:6c56fb4bc5f0 499 // Default value that is restored on reset.
nexpaq 0:6c56fb4bc5f0 500 PowerLevels_t &defaultAdvPowerLevels; // This goes into the advertising frames (radio power measured at 1m from device).
nexpaq 0:6c56fb4bc5f0 501 PowerLevels_t &radioPowerLevels; // This configures the power levels of the radio.
nexpaq 0:6c56fb4bc5f0 502 uint8_t lockedState;
nexpaq 0:6c56fb4bc5f0 503 bool initSucceeded;
nexpaq 0:6c56fb4bc5f0 504 uint8_t resetFlag;
nexpaq 0:6c56fb4bc5f0 505 bool switchFlag;
nexpaq 0:6c56fb4bc5f0 506
nexpaq 0:6c56fb4bc5f0 507 //UID default value that is restored on reset.
nexpaq 0:6c56fb4bc5f0 508 UIDNamespaceID_t defaultUidNamespaceID;
nexpaq 0:6c56fb4bc5f0 509 UIDInstanceID_t defaultUidInstanceID;
nexpaq 0:6c56fb4bc5f0 510 float defaultUidAdvPeriod;
nexpaq 0:6c56fb4bc5f0 511 int8_t defaultUidPower;
nexpaq 0:6c56fb4bc5f0 512 uint16_t uidRFU;
nexpaq 0:6c56fb4bc5f0 513 bool uidIsSet;
nexpaq 0:6c56fb4bc5f0 514
nexpaq 0:6c56fb4bc5f0 515 //URI default value that is restored on reset.
nexpaq 0:6c56fb4bc5f0 516 uint8_t defaultUriDataLength;
nexpaq 0:6c56fb4bc5f0 517 UriData_t defaultUriData;
nexpaq 0:6c56fb4bc5f0 518 int8_t defaultUrlPower;
nexpaq 0:6c56fb4bc5f0 519 float defaultUriAdvPeriod;
nexpaq 0:6c56fb4bc5f0 520 bool urlIsSet;
nexpaq 0:6c56fb4bc5f0 521
nexpaq 0:6c56fb4bc5f0 522 //TLM default value that is restored on reset.
nexpaq 0:6c56fb4bc5f0 523 uint8_t defaultTlmVersion;
nexpaq 0:6c56fb4bc5f0 524 float defaultTlmAdvPeriod;
nexpaq 0:6c56fb4bc5f0 525 volatile uint16_t TlmBatteryVoltage;
nexpaq 0:6c56fb4bc5f0 526 volatile uint16_t TlmBeaconTemp;
nexpaq 0:6c56fb4bc5f0 527 volatile uint32_t TlmPduCount;
nexpaq 0:6c56fb4bc5f0 528 volatile uint32_t TlmTimeSinceBoot;
nexpaq 0:6c56fb4bc5f0 529 bool tlmIsSet;
nexpaq 0:6c56fb4bc5f0 530
nexpaq 0:6c56fb4bc5f0 531 ReadOnlyGattCharacteristic<uint8_t> lockedStateChar;
nexpaq 0:6c56fb4bc5f0 532 WriteOnlyGattCharacteristic<Lock_t> lockChar;
nexpaq 0:6c56fb4bc5f0 533 GattCharacteristic uriDataChar;
nexpaq 0:6c56fb4bc5f0 534 WriteOnlyGattCharacteristic<Lock_t> unlockChar;
nexpaq 0:6c56fb4bc5f0 535 ReadWriteGattCharacteristic<uint8_t> flagsChar;
nexpaq 0:6c56fb4bc5f0 536 ReadWriteGattCharacteristic<PowerLevels_t> advPowerLevelsChar;
nexpaq 0:6c56fb4bc5f0 537 ReadWriteGattCharacteristic<uint8_t> txPowerModeChar;
nexpaq 0:6c56fb4bc5f0 538 ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar;
nexpaq 0:6c56fb4bc5f0 539 WriteOnlyGattCharacteristic<uint8_t> resetChar;
nexpaq 0:6c56fb4bc5f0 540 };
nexpaq 0:6c56fb4bc5f0 541
nexpaq 0:6c56fb4bc5f0 542 #endif // SERVICES_EDDYSTONE_BEACON_CONFIG_SERVICE_H_