Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

Committer:
mbedAustin
Date:
Fri Sep 11 19:38:12 2015 +0000
Revision:
44:0e27ee7800b8
Parent:
43:e4a4f9a8766f
Child:
45:1827e4f496a7
Modified API to be modular. This will make adding extra frame types in the future easier. Currently works, but has issues with persistent params.

Who changed what in which revision?

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