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