Button initiated config service
Dependencies: BLE_API_EddystoneConfigService_2 mbed nRF51822
Fork of BLE_EddystoneBeaconConfigService_3 by
EddystoneConfigService.h@32:985642364cf5, 2015-07-27 (annotated)
- Committer:
- mbedAustin
- Date:
- Mon Jul 27 21:00:35 2015 +0000
- Revision:
- 32:985642364cf5
- Parent:
- 31:11e7a505a4be
- Child:
- 33:8c65879e7d52
Things are working, just not correctly. Passing all but the last few validation tests. Seems that the URI being passed, flags and a couple other things are not propagating properly.
Who changed what in which revision?
User | Revision | Line number | New 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 | 23:05e9bb3b13af | 17 | #ifndef SERVICES_ZIPBEACONCONFIGSERVICE_H_ |
mbedAustin | 23:05e9bb3b13af | 18 | #define SERVICES_ZIPBEACONCONFIGSERVICE_H_ |
mbedAustin | 23:05e9bb3b13af | 19 | |
mbedAustin | 23:05e9bb3b13af | 20 | #include "mbed.h" |
mbedAustin | 23:05e9bb3b13af | 21 | #include "ble/BLE.h" |
mbedAustin | 23:05e9bb3b13af | 22 | |
mbedAustin | 23:05e9bb3b13af | 23 | |
mbedAustin | 23:05e9bb3b13af | 24 | #define UUID_URI_BEACON(FIRST, SECOND) { \ |
mbedAustin | 23:05e9bb3b13af | 25 | 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ |
mbedAustin | 23:05e9bb3b13af | 26 | 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \ |
mbedAustin | 23:05e9bb3b13af | 27 | } |
mbedAustin | 23:05e9bb3b13af | 28 | |
mbedAustin | 23:05e9bb3b13af | 29 | static const uint8_t UUID_URI_BEACON_SERVICE[] = UUID_URI_BEACON(0x20, 0x80); |
mbedAustin | 23:05e9bb3b13af | 30 | static const uint8_t UUID_LOCK_STATE_CHAR[] = UUID_URI_BEACON(0x20, 0x81); |
mbedAustin | 23:05e9bb3b13af | 31 | static const uint8_t UUID_LOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x82); |
mbedAustin | 23:05e9bb3b13af | 32 | static const uint8_t UUID_UNLOCK_CHAR[] = UUID_URI_BEACON(0x20, 0x83); |
mbedAustin | 23:05e9bb3b13af | 33 | static const uint8_t UUID_URI_DATA_CHAR[] = UUID_URI_BEACON(0x20, 0x84); |
mbedAustin | 23:05e9bb3b13af | 34 | static const uint8_t UUID_FLAGS_CHAR[] = UUID_URI_BEACON(0x20, 0x85); |
mbedAustin | 23:05e9bb3b13af | 35 | static const uint8_t UUID_ADV_POWER_LEVELS_CHAR[] = UUID_URI_BEACON(0x20, 0x86); |
mbedAustin | 23:05e9bb3b13af | 36 | static const uint8_t UUID_TX_POWER_MODE_CHAR[] = UUID_URI_BEACON(0x20, 0x87); |
mbedAustin | 23:05e9bb3b13af | 37 | static const uint8_t UUID_BEACON_PERIOD_CHAR[] = UUID_URI_BEACON(0x20, 0x88); |
mbedAustin | 23:05e9bb3b13af | 38 | static const uint8_t UUID_RESET_CHAR[] = UUID_URI_BEACON(0x20, 0x89); |
mbedAustin | 23:05e9bb3b13af | 39 | static const uint8_t BEACON_EDDYSTONE[] = {0xAA, 0xFE}; |
mbedAustin | 23:05e9bb3b13af | 40 | |
mbedAustin | 23:05e9bb3b13af | 41 | //Debug is disabled by default |
mbedAustin | 32:985642364cf5 | 42 | #if 1 |
mbedAustin | 23:05e9bb3b13af | 43 | #define DBG(x, ...) printf("[EddyStone: DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
mbedAustin | 23:05e9bb3b13af | 44 | #define WARN(x, ...) printf("[EddyStone: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
mbedAustin | 23:05e9bb3b13af | 45 | #define ERR(x, ...) printf("[EddyStone: ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
mbedAustin | 32:985642364cf5 | 46 | #define INFO(x, ...) printf(x, ##__VA_ARGS__) |
mbedAustin | 23:05e9bb3b13af | 47 | #else |
mbedAustin | 23:05e9bb3b13af | 48 | #define DBG(x, ...) //wait_us(10); |
mbedAustin | 23:05e9bb3b13af | 49 | #define WARN(x, ...) //wait_us(10); |
mbedAustin | 23:05e9bb3b13af | 50 | #define ERR(x, ...) |
mbedAustin | 32:985642364cf5 | 51 | #define INFO(x, ...) |
mbedAustin | 23:05e9bb3b13af | 52 | #endif |
mbedAustin | 23:05e9bb3b13af | 53 | |
mbedAustin | 23:05e9bb3b13af | 54 | /** |
mbedAustin | 23:05e9bb3b13af | 55 | * @class EddystoneConfigService |
mbedAustin | 23:05e9bb3b13af | 56 | * @brief Eddystone Configuration Service. Can be used to set URL, adjust power levels, and set flags. |
mbedAustin | 25:e20bed9e466f | 57 | * See https://github.com/google/eddystone |
mbedAustin | 23:05e9bb3b13af | 58 | * |
mbedAustin | 23:05e9bb3b13af | 59 | */ |
mbedAustin | 23:05e9bb3b13af | 60 | class EddystoneConfigService |
mbedAustin | 23:05e9bb3b13af | 61 | { |
mbedAustin | 23:05e9bb3b13af | 62 | public: |
mbedAustin | 23:05e9bb3b13af | 63 | /** |
mbedAustin | 23:05e9bb3b13af | 64 | * @brief Transmission Power Modes for UriBeacon |
mbedAustin | 23:05e9bb3b13af | 65 | */ |
mbedAustin | 23:05e9bb3b13af | 66 | static const uint8_t TX_POWER_MODE_LOWEST = 0; /*!< Lowest TX power mode */ |
mbedAustin | 23:05e9bb3b13af | 67 | static const uint8_t TX_POWER_MODE_LOW = 1; /*!< Low TX power mode */ |
mbedAustin | 23:05e9bb3b13af | 68 | static const uint8_t TX_POWER_MODE_MEDIUM = 2; /*!< Medium TX power mode */ |
mbedAustin | 23:05e9bb3b13af | 69 | static const uint8_t TX_POWER_MODE_HIGH = 3; /*!< High TX power mode */ |
mbedAustin | 23:05e9bb3b13af | 70 | static const unsigned int NUM_POWER_MODES = 4; /*!< Number of Power Modes defined */ |
mbedAustin | 23:05e9bb3b13af | 71 | |
mbedAustin | 23:05e9bb3b13af | 72 | static const int ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service. |
mbedAustin | 23:05e9bb3b13af | 73 | static const int SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets |
mbedAustin | 23:05e9bb3b13af | 74 | |
mbedAustin | 23:05e9bb3b13af | 75 | typedef uint8_t Lock_t[16]; /* 128 bits */ |
mbedAustin | 23:05e9bb3b13af | 76 | typedef int8_t PowerLevels_t[NUM_POWER_MODES]; |
mbedAustin | 23:05e9bb3b13af | 77 | |
mbedAustin | 23:05e9bb3b13af | 78 | // There are currently 3 subframes defined, URI, UID, and TLM |
mbedAustin | 23:05e9bb3b13af | 79 | #define EDDYSTONE_MAX_FRAMETYPE 3 |
mbedAustin | 23:05e9bb3b13af | 80 | void (*frames[EDDYSTONE_MAX_FRAMETYPE])(uint8_t *, uint32_t); |
mbedAustin | 23:05e9bb3b13af | 81 | uint8_t frameIndex; |
mbedAustin | 23:05e9bb3b13af | 82 | static const int URI_DATA_MAX = 18; |
mbedAustin | 23:05e9bb3b13af | 83 | typedef uint8_t UriData_t[URI_DATA_MAX]; |
mbedAustin | 23:05e9bb3b13af | 84 | |
mbedAustin | 23:05e9bb3b13af | 85 | // UID Frame Type subfields |
mbedAustin | 23:05e9bb3b13af | 86 | static const int UID_NAMESPACEID_SIZE = 10; |
mbedAustin | 23:05e9bb3b13af | 87 | typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE]; |
mbedAustin | 23:05e9bb3b13af | 88 | static const int UID_INSTANCEID_SIZE = 6; |
mbedAustin | 23:05e9bb3b13af | 89 | typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE]; |
mbedAustin | 23:05e9bb3b13af | 90 | |
mbedAustin | 23:05e9bb3b13af | 91 | // Eddystone Frame Type ID |
mbedAustin | 23:05e9bb3b13af | 92 | static const uint8_t FRAME_TYPE_UID = 0x00; |
mbedAustin | 23:05e9bb3b13af | 93 | static const uint8_t FRAME_TYPE_URL = 0x10; |
mbedAustin | 23:05e9bb3b13af | 94 | static const uint8_t FRAME_TYPE_TLM = 0x20; |
mbedAustin | 23:05e9bb3b13af | 95 | |
mbedAustin | 23:05e9bb3b13af | 96 | static const uint8_t FRAME_SIZE_TLM = 14; // TLM frame is a constant 14Bytes |
mbedAustin | 23:05e9bb3b13af | 97 | static const uint8_t FRAME_SIZE_UID = 20; // includes RFU bytes |
mbedAustin | 23:05e9bb3b13af | 98 | |
mbedAustin | 23:05e9bb3b13af | 99 | struct Params_t { |
mbedAustin | 23:05e9bb3b13af | 100 | Lock_t lock; |
mbedAustin | 23:05e9bb3b13af | 101 | uint8_t uriDataLength; |
mbedAustin | 23:05e9bb3b13af | 102 | UriData_t uriData; |
mbedAustin | 23:05e9bb3b13af | 103 | uint8_t flags; |
mbedAustin | 23:05e9bb3b13af | 104 | PowerLevels_t advPowerLevels; // Current value of AdvertisedPowerLevels |
mbedAustin | 23:05e9bb3b13af | 105 | uint8_t txPowerMode; // Firmware power levels used with setTxPower() |
mbedAustin | 23:05e9bb3b13af | 106 | uint16_t beaconPeriod; |
mbedAustin | 31:11e7a505a4be | 107 | //uint8_t tlmVersion; // version of TLM packet |
mbedAustin | 31:11e7a505a4be | 108 | //UIDNamespaceID_t uidNamespaceID; // UUID type, Namespace ID, 10B |
mbedAustin | 31:11e7a505a4be | 109 | //UIDInstanceID_t uidInstanceID; // UUID type, Instance ID, 6B |
mbedAustin | 23:05e9bb3b13af | 110 | }; |
mbedAustin | 23:05e9bb3b13af | 111 | |
mbedAustin | 23:05e9bb3b13af | 112 | /** |
mbedAustin | 23:05e9bb3b13af | 113 | * @param[ref] ble |
mbedAustin | 23:05e9bb3b13af | 114 | * BLEDevice object for the underlying controller. |
mbedAustin | 23:05e9bb3b13af | 115 | * @param[in/out] paramsIn |
mbedAustin | 23:05e9bb3b13af | 116 | * Reference to application-visible beacon state, loaded |
mbedAustin | 23:05e9bb3b13af | 117 | * from persistent storage at startup. |
mbedAustin | 23:05e9bb3b13af | 118 | * @paramsP[in] resetToDefaultsFlag |
mbedAustin | 23:05e9bb3b13af | 119 | * Applies to the state of the 'paramsIn' parameter. |
mbedAustin | 23:05e9bb3b13af | 120 | * If true, it indicates that paramsIn is potentially |
mbedAustin | 23:05e9bb3b13af | 121 | * un-initialized, and default values should be used |
mbedAustin | 23:05e9bb3b13af | 122 | * instead. Otherwise, paramsIn overrides the defaults. |
mbedAustin | 23:05e9bb3b13af | 123 | * @param[in] defaultUriDataIn |
mbedAustin | 23:05e9bb3b13af | 124 | * Default un-encoded URI; applies only if the resetToDefaultsFlag is true. |
mbedAustin | 23:05e9bb3b13af | 125 | * @param[in] defaultAdvPowerLevelsIn |
mbedAustin | 23:05e9bb3b13af | 126 | * Default power-levels array; applies only if the resetToDefaultsFlag is true. |
mbedAustin | 23:05e9bb3b13af | 127 | */ |
mbedAustin | 23:05e9bb3b13af | 128 | EddystoneConfigService(BLEDevice &bleIn, |
mbedAustin | 23:05e9bb3b13af | 129 | Params_t ¶msIn, |
mbedAustin | 23:05e9bb3b13af | 130 | bool resetToDefaultsFlag, |
mbedAustin | 23:05e9bb3b13af | 131 | const char *defaultURIDataIn, |
mbedAustin | 31:11e7a505a4be | 132 | UIDNamespaceID_t uidNamespaceID, |
mbedAustin | 31:11e7a505a4be | 133 | UIDInstanceID_t uidInstanceID, |
mbedAustin | 31:11e7a505a4be | 134 | uint8_t tlmVersion, |
mbedAustin | 23:05e9bb3b13af | 135 | PowerLevels_t &defaultAdvPowerLevelsIn) : |
mbedAustin | 23:05e9bb3b13af | 136 | ble(bleIn), |
mbedAustin | 31:11e7a505a4be | 137 | params(paramsIn), // Initialize URL Data |
mbedAustin | 23:05e9bb3b13af | 138 | defaultUriDataLength(), |
mbedAustin | 23:05e9bb3b13af | 139 | defaultUriData(), |
mbedAustin | 31:11e7a505a4be | 140 | //defaultUidNamespaceID(uidNamespaceID), // Initialize UID Data |
mbedAustin | 31:11e7a505a4be | 141 | //defaultUidInstanceID(uidInstanceID), |
mbedAustin | 31:11e7a505a4be | 142 | defaultUrlPower(params.advPowerLevels[params.txPowerMode]), |
mbedAustin | 31:11e7a505a4be | 143 | defaultUidPower(params.advPowerLevels[params.txPowerMode]), |
mbedAustin | 31:11e7a505a4be | 144 | uidIsSet(true), |
mbedAustin | 31:11e7a505a4be | 145 | urlIsSet(true), |
mbedAustin | 23:05e9bb3b13af | 146 | defaultAdvPowerLevels(defaultAdvPowerLevelsIn), |
mbedAustin | 23:05e9bb3b13af | 147 | initSucceeded(false), |
mbedAustin | 23:05e9bb3b13af | 148 | resetFlag(), |
mbedAustin | 31:11e7a505a4be | 149 | TlmVersion(tlmVersion), // Initialize TLM Data |
mbedAustin | 31:11e7a505a4be | 150 | TlmBatteryVoltage(0), |
mbedAustin | 31:11e7a505a4be | 151 | TlmBeaconTemp(0), |
mbedAustin | 31:11e7a505a4be | 152 | TlmPduCount(0), |
mbedAustin | 31:11e7a505a4be | 153 | TlmTimeSinceBoot(0), |
mbedAustin | 23:05e9bb3b13af | 154 | lockedStateChar(UUID_LOCK_STATE_CHAR, &lockedState), |
mbedAustin | 23:05e9bb3b13af | 155 | lockChar(UUID_LOCK_CHAR, ¶ms.lock), |
mbedAustin | 23:05e9bb3b13af | 156 | uriDataChar(UUID_URI_DATA_CHAR, params.uriData, 0, URI_DATA_MAX, |
mbedAustin | 23:05e9bb3b13af | 157 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE), |
mbedAustin | 23:05e9bb3b13af | 158 | unlockChar(UUID_UNLOCK_CHAR, ¶ms.lock), |
mbedAustin | 23:05e9bb3b13af | 159 | flagsChar(UUID_FLAGS_CHAR, ¶ms.flags), |
mbedAustin | 23:05e9bb3b13af | 160 | advPowerLevelsChar(UUID_ADV_POWER_LEVELS_CHAR, ¶ms.advPowerLevels), |
mbedAustin | 23:05e9bb3b13af | 161 | txPowerModeChar(UUID_TX_POWER_MODE_CHAR, ¶ms.txPowerMode), |
mbedAustin | 23:05e9bb3b13af | 162 | beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, ¶ms.beaconPeriod), |
mbedAustin | 31:11e7a505a4be | 163 | resetChar(UUID_RESET_CHAR, &resetFlag) |
mbedAustin | 31:11e7a505a4be | 164 | { |
mbedAustin | 31:11e7a505a4be | 165 | //Set UID frame |
mbedAustin | 31:11e7a505a4be | 166 | memcpy(defaultUidNamespaceID,uidNamespaceID,UID_NAMESPACEID_SIZE); |
mbedAustin | 31:11e7a505a4be | 167 | memcpy(defaultUidInstanceID,uidInstanceID,UID_INSTANCEID_SIZE); |
mbedAustin | 31:11e7a505a4be | 168 | |
mbedAustin | 31:11e7a505a4be | 169 | // Set URL Frame |
mbedAustin | 23:05e9bb3b13af | 170 | encodeURI(defaultURIDataIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting |
mbedAustin | 23:05e9bb3b13af | 171 | if (defaultUriDataLength > URI_DATA_MAX) { |
mbedAustin | 23:05e9bb3b13af | 172 | return; |
mbedAustin | 23:05e9bb3b13af | 173 | } |
mbedAustin | 23:05e9bb3b13af | 174 | |
mbedAustin | 23:05e9bb3b13af | 175 | if (!resetToDefaultsFlag && (params.uriDataLength > URI_DATA_MAX)) { |
mbedAustin | 23:05e9bb3b13af | 176 | resetToDefaultsFlag = true; |
mbedAustin | 23:05e9bb3b13af | 177 | } |
mbedAustin | 23:05e9bb3b13af | 178 | if (resetToDefaultsFlag) { |
mbedAustin | 23:05e9bb3b13af | 179 | resetToDefaults(); |
mbedAustin | 23:05e9bb3b13af | 180 | } else { |
mbedAustin | 23:05e9bb3b13af | 181 | updateCharacteristicValues(); |
mbedAustin | 23:05e9bb3b13af | 182 | } |
mbedAustin | 23:05e9bb3b13af | 183 | |
mbedAustin | 23:05e9bb3b13af | 184 | lockedState = isLocked(); |
mbedAustin | 23:05e9bb3b13af | 185 | |
mbedAustin | 23:05e9bb3b13af | 186 | lockChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::lockAuthorizationCallback); |
mbedAustin | 23:05e9bb3b13af | 187 | unlockChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::unlockAuthorizationCallback); |
mbedAustin | 23:05e9bb3b13af | 188 | uriDataChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::uriDataWriteAuthorizationCallback); |
mbedAustin | 23:05e9bb3b13af | 189 | flagsChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<uint8_t>); |
mbedAustin | 23:05e9bb3b13af | 190 | advPowerLevelsChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<PowerLevels_t>); |
mbedAustin | 23:05e9bb3b13af | 191 | txPowerModeChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::powerModeAuthorizationCallback); |
mbedAustin | 23:05e9bb3b13af | 192 | beaconPeriodChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<uint16_t>); |
mbedAustin | 23:05e9bb3b13af | 193 | resetChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::basicAuthorizationCallback<uint8_t>); |
mbedAustin | 23:05e9bb3b13af | 194 | |
mbedAustin | 23:05e9bb3b13af | 195 | static GattCharacteristic *charTable[] = { |
mbedAustin | 23:05e9bb3b13af | 196 | &lockedStateChar, &lockChar, &unlockChar, &uriDataChar, |
mbedAustin | 23:05e9bb3b13af | 197 | &flagsChar, &advPowerLevelsChar, &txPowerModeChar, &beaconPeriodChar, &resetChar |
mbedAustin | 23:05e9bb3b13af | 198 | }; |
mbedAustin | 23:05e9bb3b13af | 199 | |
mbedAustin | 23:05e9bb3b13af | 200 | GattService configService(UUID_URI_BEACON_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
mbedAustin | 23:05e9bb3b13af | 201 | |
mbedAustin | 23:05e9bb3b13af | 202 | ble.addService(configService); |
mbedAustin | 23:05e9bb3b13af | 203 | ble.onDataWritten(this, &EddystoneConfigService::onDataWrittenCallback); |
mbedAustin | 23:05e9bb3b13af | 204 | |
mbedAustin | 23:05e9bb3b13af | 205 | setupEddystoneConfigAdvertisements(); /* Setup advertising for the configService. */ |
mbedAustin | 23:05e9bb3b13af | 206 | |
mbedAustin | 23:05e9bb3b13af | 207 | initSucceeded = true; |
mbedAustin | 23:05e9bb3b13af | 208 | } |
mbedAustin | 23:05e9bb3b13af | 209 | |
mbedAustin | 23:05e9bb3b13af | 210 | bool configuredSuccessfully(void) const { |
mbedAustin | 23:05e9bb3b13af | 211 | return initSucceeded; |
mbedAustin | 23:05e9bb3b13af | 212 | } |
mbedAustin | 23:05e9bb3b13af | 213 | |
mbedAustin | 23:05e9bb3b13af | 214 | /* Start out by advertising the configService for a limited time after |
mbedAustin | 23:05e9bb3b13af | 215 | * startup; and switch to the normal non-connectible beacon functionality |
mbedAustin | 23:05e9bb3b13af | 216 | * afterwards. */ |
mbedAustin | 23:05e9bb3b13af | 217 | void setupEddystoneConfigAdvertisements() { |
mbedAustin | 23:05e9bb3b13af | 218 | const char DEVICE_NAME[] = "eddystone Config"; |
mbedAustin | 23:05e9bb3b13af | 219 | |
mbedAustin | 23:05e9bb3b13af | 220 | ble.clearAdvertisingPayload(); |
mbedAustin | 23:05e9bb3b13af | 221 | |
mbedAustin | 23:05e9bb3b13af | 222 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mbedAustin | 23:05e9bb3b13af | 223 | |
mbedAustin | 23:05e9bb3b13af | 224 | // UUID is in different order in the ADV frame (!) |
mbedAustin | 23:05e9bb3b13af | 225 | uint8_t reversedServiceUUID[sizeof(UUID_URI_BEACON_SERVICE)]; |
mbedAustin | 23:05e9bb3b13af | 226 | for (unsigned int i = 0; i < sizeof(UUID_URI_BEACON_SERVICE); i++) { |
mbedAustin | 23:05e9bb3b13af | 227 | reversedServiceUUID[i] = UUID_URI_BEACON_SERVICE[sizeof(UUID_URI_BEACON_SERVICE) - i - 1]; |
mbedAustin | 23:05e9bb3b13af | 228 | } |
mbedAustin | 23:05e9bb3b13af | 229 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, reversedServiceUUID, sizeof(reversedServiceUUID)); |
mbedAustin | 23:05e9bb3b13af | 230 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG); |
mbedAustin | 23:05e9bb3b13af | 231 | ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, reinterpret_cast<const uint8_t *>(&DEVICE_NAME), sizeof(DEVICE_NAME)); |
mbedAustin | 23:05e9bb3b13af | 232 | ble.accumulateScanResponse( |
mbedAustin | 23:05e9bb3b13af | 233 | GapAdvertisingData::TX_POWER_LEVEL, |
mbedAustin | 23:05e9bb3b13af | 234 | reinterpret_cast<uint8_t *>(&defaultAdvPowerLevels[EddystoneConfigService::TX_POWER_MODE_LOW]), |
mbedAustin | 23:05e9bb3b13af | 235 | sizeof(uint8_t)); |
mbedAustin | 23:05e9bb3b13af | 236 | |
mbedAustin | 23:05e9bb3b13af | 237 | ble.setTxPower(params.advPowerLevels[params.txPowerMode]); |
mbedAustin | 23:05e9bb3b13af | 238 | ble.setDeviceName(reinterpret_cast<const uint8_t *>(&DEVICE_NAME)); |
mbedAustin | 23:05e9bb3b13af | 239 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mbedAustin | 23:05e9bb3b13af | 240 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADVERTISING_INTERVAL_MSEC)); |
mbedAustin | 23:05e9bb3b13af | 241 | } |
mbedAustin | 23:05e9bb3b13af | 242 | |
mbedAustin | 23:05e9bb3b13af | 243 | /* |
mbedAustin | 23:05e9bb3b13af | 244 | * Set Eddystone UID Frame information. |
mbedAustin | 23:05e9bb3b13af | 245 | * @param[in] power TX Power in dB measured at 0 meters from the device. Range of -100 to +20 dB. |
mbedAustin | 23:05e9bb3b13af | 246 | * @param namespaceID 10B namespace ID |
mbedAustin | 23:05e9bb3b13af | 247 | * @param instanceID 6B instance ID |
mbedAustin | 23:05e9bb3b13af | 248 | * @param RFU 2B of RFU, initialized to 0x0000 and not broadcast, included for future reference. |
mbedAustin | 23:05e9bb3b13af | 249 | * |
mbedAustin | 23:05e9bb3b13af | 250 | */ |
mbedAustin | 23:05e9bb3b13af | 251 | void setUIDFrameData(int8_t power, UIDNamespaceID_t namespaceID, UIDInstanceID_t instanceID, uint16_t RFU = 0x0000) { |
mbedAustin | 23:05e9bb3b13af | 252 | if(power > 20) { |
mbedAustin | 23:05e9bb3b13af | 253 | power = 20; |
mbedAustin | 23:05e9bb3b13af | 254 | } |
mbedAustin | 23:05e9bb3b13af | 255 | if(power < -100) { |
mbedAustin | 23:05e9bb3b13af | 256 | power = -100; |
mbedAustin | 23:05e9bb3b13af | 257 | } |
mbedAustin | 23:05e9bb3b13af | 258 | defaultUidPower = power; |
mbedAustin | 23:05e9bb3b13af | 259 | memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE); |
mbedAustin | 23:05e9bb3b13af | 260 | memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE); |
mbedAustin | 23:05e9bb3b13af | 261 | uidRFU = (uint16_t)RFU; // this is probably bad form, but it doesnt really matter yet. |
mbedAustin | 23:05e9bb3b13af | 262 | return; |
mbedAustin | 23:05e9bb3b13af | 263 | } |
mbedAustin | 23:05e9bb3b13af | 264 | |
mbedAustin | 23:05e9bb3b13af | 265 | /* |
mbedAustin | 23:05e9bb3b13af | 266 | * Construct UID frame from private variables |
mbedAustin | 23:05e9bb3b13af | 267 | * @param[in/out] Data pointer to array to store constructed frame in |
mbedAustin | 23:05e9bb3b13af | 268 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
mbedAustin | 23:05e9bb3b13af | 269 | * @return number of bytes used. negative number indicates error message. |
mbedAustin | 23:05e9bb3b13af | 270 | */ |
mbedAustin | 23:05e9bb3b13af | 271 | int constructUIDFrame(uint8_t * Data, uint8_t maxSize) { |
mbedAustin | 23:05e9bb3b13af | 272 | |
mbedAustin | 23:05e9bb3b13af | 273 | int index = 0; |
mbedAustin | 23:05e9bb3b13af | 274 | Data[index++] = FRAME_TYPE_UID; // 1B Type |
mbedAustin | 23:05e9bb3b13af | 275 | if(defaultUidPower > 20) { |
mbedAustin | 23:05e9bb3b13af | 276 | defaultUidPower = 20; // enforce range of vaild values. |
mbedAustin | 23:05e9bb3b13af | 277 | } |
mbedAustin | 23:05e9bb3b13af | 278 | if(defaultUidPower < -100) { |
mbedAustin | 23:05e9bb3b13af | 279 | defaultUidPower = -100; |
mbedAustin | 23:05e9bb3b13af | 280 | } |
mbedAustin | 23:05e9bb3b13af | 281 | Data[index++] = defaultUidPower; // 1B Power @ 0meter |
mbedAustin | 23:05e9bb3b13af | 282 | for(int x = 0; x < UID_NAMESPACEID_SIZE; x++) { // 10B Namespce ID |
mbedAustin | 23:05e9bb3b13af | 283 | Data[index++] = defaultUidNamespaceID[x]; |
mbedAustin | 23:05e9bb3b13af | 284 | } |
mbedAustin | 23:05e9bb3b13af | 285 | for(int x = 0; x< UID_INSTANCEID_SIZE; x++) { // 6B Instance ID |
mbedAustin | 23:05e9bb3b13af | 286 | Data[index++] = defaultUidInstanceID[x]; |
mbedAustin | 23:05e9bb3b13af | 287 | } |
mbedAustin | 23:05e9bb3b13af | 288 | if(0 != uidRFU) { // 2B RFU, include if non-zero, otherwise ignore |
mbedAustin | 23:05e9bb3b13af | 289 | Data[index++] = (uint8_t)(uidRFU >> 0); |
mbedAustin | 23:05e9bb3b13af | 290 | Data[index++] = (uint8_t)(uidRFU >> 8); |
mbedAustin | 23:05e9bb3b13af | 291 | } |
mbedAustin | 23:05e9bb3b13af | 292 | DBG("construcUIDFrame %d, %d",maxSize,index); |
mbedAustin | 23:05e9bb3b13af | 293 | return index; |
mbedAustin | 23:05e9bb3b13af | 294 | } |
mbedAustin | 23:05e9bb3b13af | 295 | |
mbedAustin | 23:05e9bb3b13af | 296 | /* |
mbedAustin | 23:05e9bb3b13af | 297 | * Set Eddystone URL Frame information. |
mbedAustin | 23:05e9bb3b13af | 298 | * @param[in] power TX Power in dB measured at 0 meters from the device. |
mbedAustin | 23:05e9bb3b13af | 299 | * @param url URL to encode |
mbedAustin | 23:05e9bb3b13af | 300 | * @return false on success, true on failure. |
mbedAustin | 23:05e9bb3b13af | 301 | */ |
mbedAustin | 23:05e9bb3b13af | 302 | bool setURLFrameData(int8_t power, const char * url) { |
mbedAustin | 23:05e9bb3b13af | 303 | defaultUrlPower = power; |
mbedAustin | 23:05e9bb3b13af | 304 | encodeURI(url, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting |
mbedAustin | 23:05e9bb3b13af | 305 | if (defaultUriDataLength > URI_DATA_MAX) { |
mbedAustin | 23:05e9bb3b13af | 306 | return true; // error, URL is too big |
mbedAustin | 23:05e9bb3b13af | 307 | } |
mbedAustin | 23:05e9bb3b13af | 308 | return false; |
mbedAustin | 23:05e9bb3b13af | 309 | } |
mbedAustin | 23:05e9bb3b13af | 310 | |
mbedAustin | 23:05e9bb3b13af | 311 | /* |
mbedAustin | 23:05e9bb3b13af | 312 | * Construct URL frame from private variables |
mbedAustin | 23:05e9bb3b13af | 313 | * @param[in/out] Data pointer to array to store constructed frame in |
mbedAustin | 23:05e9bb3b13af | 314 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
mbedAustin | 23:05e9bb3b13af | 315 | * @return number of bytes used. negative number indicates error message. |
mbedAustin | 23:05e9bb3b13af | 316 | */ |
mbedAustin | 23:05e9bb3b13af | 317 | int constructURLFrame(uint8_t * Data, uint8_t maxSize) { |
mbedAustin | 23:05e9bb3b13af | 318 | int index = 0; |
mbedAustin | 23:05e9bb3b13af | 319 | Data[index++] = FRAME_TYPE_URL; // 1B Type |
mbedAustin | 25:e20bed9e466f | 320 | Data[index++] = defaultUrlPower; // 1B TX Power |
mbedAustin | 23:05e9bb3b13af | 321 | for(int x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL |
mbedAustin | 23:05e9bb3b13af | 322 | Data[index++] = defaultUriData[x]; |
mbedAustin | 23:05e9bb3b13af | 323 | } |
mbedAustin | 23:05e9bb3b13af | 324 | DBG("constructURLFrame: %d, %d",maxSize,index); |
mbedAustin | 23:05e9bb3b13af | 325 | return index; |
mbedAustin | 23:05e9bb3b13af | 326 | } |
mbedAustin | 23:05e9bb3b13af | 327 | |
mbedAustin | 23:05e9bb3b13af | 328 | /* |
mbedAustin | 23:05e9bb3b13af | 329 | * Set Eddystone TLM Frame information. |
mbedAustin | 23:05e9bb3b13af | 330 | * @param[in] Version of the TLM beacon data format |
mbedAustin | 23:05e9bb3b13af | 331 | * @param batteryVoltage in milivolts |
mbedAustin | 23:05e9bb3b13af | 332 | * @param beaconTemp in 8.8 floating point notation |
mbedAustin | 23:05e9bb3b13af | 333 | * |
mbedAustin | 23:05e9bb3b13af | 334 | */ |
mbedAustin | 23:05e9bb3b13af | 335 | void setTLMFrameData(uint8_t version, uint16_t batteryVoltage, uint16_t beaconTemp, uint32_t pduCount = 0, uint32_t timeSinceBoot = 0) { |
mbedAustin | 23:05e9bb3b13af | 336 | TlmVersion = version; |
mbedAustin | 23:05e9bb3b13af | 337 | TlmBatteryVoltage = batteryVoltage; |
mbedAustin | 23:05e9bb3b13af | 338 | TlmBeaconTemp = beaconTemp; |
mbedAustin | 23:05e9bb3b13af | 339 | TlmPduCount = pduCount; // reset |
mbedAustin | 23:05e9bb3b13af | 340 | TlmTimeSinceBoot = timeSinceBoot; // reset |
mbedAustin | 23:05e9bb3b13af | 341 | return; |
mbedAustin | 23:05e9bb3b13af | 342 | } |
mbedAustin | 23:05e9bb3b13af | 343 | |
mbedAustin | 23:05e9bb3b13af | 344 | /* |
mbedAustin | 23:05e9bb3b13af | 345 | * Construct TLM frame from private variables |
mbedAustin | 23:05e9bb3b13af | 346 | * @param[in/out] Data pointer to array to store constructed frame in |
mbedAustin | 23:05e9bb3b13af | 347 | * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to |
mbedAustin | 23:05e9bb3b13af | 348 | * @return number of bytes used. negative number indicates error message. |
mbedAustin | 23:05e9bb3b13af | 349 | */ |
mbedAustin | 23:05e9bb3b13af | 350 | int constructTLMFrame(uint8_t * Data, uint8_t maxSize) { |
mbedAustin | 23:05e9bb3b13af | 351 | int index = 0; |
mbedAustin | 23:05e9bb3b13af | 352 | Data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry |
mbedAustin | 23:05e9bb3b13af | 353 | Data[index++] = TlmVersion; // TLM Version Number |
mbedAustin | 23:05e9bb3b13af | 354 | Data[index++] = (uint8_t)(TlmBatteryVoltage>>8); // Battery Voltage[0] |
mbedAustin | 23:05e9bb3b13af | 355 | Data[index++] = (uint8_t)(TlmBatteryVoltage>>0); // Battery Voltage[1] |
mbedAustin | 23:05e9bb3b13af | 356 | Data[index++] = (uint8_t)(TlmBeaconTemp>>8); // Beacon Temp[0] |
mbedAustin | 23:05e9bb3b13af | 357 | Data[index++] = (uint8_t)(TlmBeaconTemp>>0); // Beacon Temp[1] |
mbedAustin | 23:05e9bb3b13af | 358 | Data[index++] = (uint8_t)(TlmPduCount>>24); // PDU Count [0] |
mbedAustin | 23:05e9bb3b13af | 359 | Data[index++] = (uint8_t)(TlmPduCount>>16); // PDU Count [1] |
mbedAustin | 23:05e9bb3b13af | 360 | Data[index++] = (uint8_t)(TlmPduCount>>8); // PDU Count [2] |
mbedAustin | 23:05e9bb3b13af | 361 | Data[index++] = (uint8_t)(TlmPduCount>>0); // PDU Count [3] |
mbedAustin | 23:05e9bb3b13af | 362 | Data[index++] = (uint8_t)(TlmTimeSinceBoot>>24); // Time Since Boot [0] |
mbedAustin | 23:05e9bb3b13af | 363 | Data[index++] = (uint8_t)(TlmTimeSinceBoot>>16); // Time Since Boot [1] |
mbedAustin | 23:05e9bb3b13af | 364 | Data[index++] = (uint8_t)(TlmTimeSinceBoot>>8); // Time Since Boot [2] |
mbedAustin | 23:05e9bb3b13af | 365 | Data[index++] = (uint8_t)(TlmTimeSinceBoot>>0); // Time Since Boot [3] |
mbedAustin | 23:05e9bb3b13af | 366 | DBG("constructURLFrame: %d, %d",maxSize,index); |
mbedAustin | 23:05e9bb3b13af | 367 | return index; |
mbedAustin | 23:05e9bb3b13af | 368 | } |
mbedAustin | 23:05e9bb3b13af | 369 | |
mbedAustin | 23:05e9bb3b13af | 370 | /* |
mbedAustin | 23:05e9bb3b13af | 371 | * Update the TLM frame battery voltage value |
mbedAustin | 23:05e9bb3b13af | 372 | * @param[in] voltagemv Voltage to update the TLM field battery voltage with (in mV) |
mbedAustin | 23:05e9bb3b13af | 373 | * @return nothing |
mbedAustin | 23:05e9bb3b13af | 374 | */ |
mbedAustin | 23:05e9bb3b13af | 375 | void updateTlmBatteryVoltage(uint16_t voltagemv) { |
mbedAustin | 23:05e9bb3b13af | 376 | TlmBatteryVoltage = voltagemv; |
mbedAustin | 23:05e9bb3b13af | 377 | return; |
mbedAustin | 23:05e9bb3b13af | 378 | } |
mbedAustin | 23:05e9bb3b13af | 379 | |
mbedAustin | 23:05e9bb3b13af | 380 | /* |
mbedAustin | 23:05e9bb3b13af | 381 | * Update the TLM frame beacon temperature |
mbedAustin | 23:05e9bb3b13af | 382 | * @param[in] temp Temperature of beacon (in 8.8fpn) |
mbedAustin | 23:05e9bb3b13af | 383 | * @return nothing |
mbedAustin | 23:05e9bb3b13af | 384 | */ |
mbedAustin | 23:05e9bb3b13af | 385 | void updateTlmBeaconTemp(uint16_t temp) { |
mbedAustin | 23:05e9bb3b13af | 386 | TlmBeaconTemp = temp; |
mbedAustin | 23:05e9bb3b13af | 387 | return; |
mbedAustin | 23:05e9bb3b13af | 388 | } |
mbedAustin | 23:05e9bb3b13af | 389 | |
mbedAustin | 23:05e9bb3b13af | 390 | /* |
mbedAustin | 23:05e9bb3b13af | 391 | * Update the TLM frame PDU Count field |
mbedAustin | 23:05e9bb3b13af | 392 | * @param[in] pduCount Number of Advertisiting frames sent since powerup |
mbedAustin | 23:05e9bb3b13af | 393 | * @return nothing |
mbedAustin | 23:05e9bb3b13af | 394 | */ |
mbedAustin | 23:05e9bb3b13af | 395 | void updateTlmPduCount(uint32_t pduCount) { |
mbedAustin | 23:05e9bb3b13af | 396 | TlmPduCount = pduCount; |
mbedAustin | 23:05e9bb3b13af | 397 | return; |
mbedAustin | 23:05e9bb3b13af | 398 | } |
mbedAustin | 23:05e9bb3b13af | 399 | |
mbedAustin | 23:05e9bb3b13af | 400 | /* |
mbedAustin | 23:05e9bb3b13af | 401 | * Update the TLM frame Time since boot in 0.1s incriments |
mbedAustin | 23:05e9bb3b13af | 402 | * @param[in] timeSinceBoot Time since boot in 0.1s incriments |
mbedAustin | 23:05e9bb3b13af | 403 | * @return nothing |
mbedAustin | 23:05e9bb3b13af | 404 | */ |
mbedAustin | 23:05e9bb3b13af | 405 | void updateTlmTimeSinceBoot(uint32_t timeSinceBoot) { |
mbedAustin | 23:05e9bb3b13af | 406 | TlmTimeSinceBoot = timeSinceBoot; |
mbedAustin | 23:05e9bb3b13af | 407 | return; |
mbedAustin | 23:05e9bb3b13af | 408 | } |
mbedAustin | 23:05e9bb3b13af | 409 | |
mbedAustin | 23:05e9bb3b13af | 410 | /* |
mbedAustin | 23:05e9bb3b13af | 411 | * callback function, called every 0.1s, incriments the TimeSinceBoot field in the TLM frame |
mbedAustin | 23:05e9bb3b13af | 412 | * @return nothing |
mbedAustin | 23:05e9bb3b13af | 413 | */ |
mbedAustin | 23:05e9bb3b13af | 414 | void tsbCallback(void) { |
mbedAustin | 23:05e9bb3b13af | 415 | TlmTimeSinceBoot++; |
mbedAustin | 23:05e9bb3b13af | 416 | } |
mbedAustin | 23:05e9bb3b13af | 417 | |
mbedAustin | 23:05e9bb3b13af | 418 | /* |
mbedAustin | 23:05e9bb3b13af | 419 | * Update advertising data |
mbedAustin | 23:05e9bb3b13af | 420 | * @return true on success, false on failure |
mbedAustin | 23:05e9bb3b13af | 421 | */ |
mbedAustin | 23:05e9bb3b13af | 422 | bool updateAdvPacket(uint8_t serviceData[], unsigned serviceDataLen) { |
mbedAustin | 23:05e9bb3b13af | 423 | // Fields from the Service |
mbedAustin | 23:05e9bb3b13af | 424 | DBG("Updating AdvFrame: %d", serviceDataLen); |
mbedAustin | 23:05e9bb3b13af | 425 | // printf("\r\n"); |
mbedAustin | 23:05e9bb3b13af | 426 | // for(int x = 0; x<serviceDataLen; x++) { |
mbedAustin | 23:05e9bb3b13af | 427 | // printf("%2.2x:",serviceData[x]); |
mbedAustin | 23:05e9bb3b13af | 428 | // } |
mbedAustin | 23:05e9bb3b13af | 429 | // printf("\r\n"); |
mbedAustin | 23:05e9bb3b13af | 430 | ble.clearAdvertisingPayload(); |
mbedAustin | 23:05e9bb3b13af | 431 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mbedAustin | 23:05e9bb3b13af | 432 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE)); |
mbedAustin | 23:05e9bb3b13af | 433 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen); |
mbedAustin | 23:05e9bb3b13af | 434 | |
mbedAustin | 23:05e9bb3b13af | 435 | return true; |
mbedAustin | 23:05e9bb3b13af | 436 | } |
mbedAustin | 23:05e9bb3b13af | 437 | |
mbedAustin | 23:05e9bb3b13af | 438 | /* |
mbedAustin | 23:05e9bb3b13af | 439 | * State machine for switching out frames. |
mbedAustin | 23:05e9bb3b13af | 440 | * This function is called by the radioNotificationCallback when a frame needs to get swapped out. |
mbedAustin | 23:05e9bb3b13af | 441 | * This function exists because of time constraints in the radioNotificationCallback, so it is effectively |
mbedAustin | 23:05e9bb3b13af | 442 | * broken up into two functions. |
mbedAustin | 23:05e9bb3b13af | 443 | */ |
mbedAustin | 23:05e9bb3b13af | 444 | void swapOutFrames(void) { |
mbedAustin | 23:05e9bb3b13af | 445 | uint8_t serviceData[SERVICE_DATA_MAX]; |
mbedAustin | 23:05e9bb3b13af | 446 | unsigned serviceDataLen = 0; |
mbedAustin | 23:05e9bb3b13af | 447 | //hard code in the eddystone UUID |
mbedAustin | 23:05e9bb3b13af | 448 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; |
mbedAustin | 23:05e9bb3b13af | 449 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; |
mbedAustin | 23:05e9bb3b13af | 450 | |
mbedAustin | 25:e20bed9e466f | 451 | // if certain frames are not enabled, then skip them. Worst case TLM is always enabled |
mbedAustin | 23:05e9bb3b13af | 452 | switch(frameIndex) { |
mbedAustin | 25:e20bed9e466f | 453 | case 1: |
mbedAustin | 25:e20bed9e466f | 454 | // URL Frame |
mbedAustin | 25:e20bed9e466f | 455 | if(urlIsSet) { |
mbedAustin | 25:e20bed9e466f | 456 | serviceDataLen += constructURLFrame(serviceData+serviceDataLen,20); |
mbedAustin | 25:e20bed9e466f | 457 | DBG("\t Swapping in URL Frame: len=%d ",serviceDataLen); |
mbedAustin | 25:e20bed9e466f | 458 | updateAdvPacket(serviceData,serviceDataLen); |
mbedAustin | 25:e20bed9e466f | 459 | switchFlag = false; |
mbedAustin | 25:e20bed9e466f | 460 | frameIndex++; |
mbedAustin | 25:e20bed9e466f | 461 | break; |
mbedAustin | 25:e20bed9e466f | 462 | } |
mbedAustin | 25:e20bed9e466f | 463 | case 2: |
mbedAustin | 25:e20bed9e466f | 464 | // UID Frame |
mbedAustin | 25:e20bed9e466f | 465 | if(uidIsSet) { |
mbedAustin | 25:e20bed9e466f | 466 | serviceDataLen += constructUIDFrame(serviceData+serviceDataLen,20); |
mbedAustin | 25:e20bed9e466f | 467 | DBG("\t Swapping in UID Frame: len=%d",serviceDataLen); |
mbedAustin | 25:e20bed9e466f | 468 | updateAdvPacket(serviceData,serviceDataLen); |
mbedAustin | 25:e20bed9e466f | 469 | switchFlag = false; |
mbedAustin | 25:e20bed9e466f | 470 | frameIndex++; |
mbedAustin | 25:e20bed9e466f | 471 | break; |
mbedAustin | 25:e20bed9e466f | 472 | } |
mbedAustin | 25:e20bed9e466f | 473 | default: |
mbedAustin | 25:e20bed9e466f | 474 | // TLM frame |
mbedAustin | 23:05e9bb3b13af | 475 | serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,20); |
mbedAustin | 23:05e9bb3b13af | 476 | DBG("\t Swapping in TLM Frame: len=%d",serviceDataLen); |
mbedAustin | 23:05e9bb3b13af | 477 | updateAdvPacket(serviceData,serviceDataLen); |
mbedAustin | 23:05e9bb3b13af | 478 | frameIndex++; |
mbedAustin | 23:05e9bb3b13af | 479 | break; |
mbedAustin | 23:05e9bb3b13af | 480 | } |
mbedAustin | 23:05e9bb3b13af | 481 | } |
mbedAustin | 23:05e9bb3b13af | 482 | |
mbedAustin | 23:05e9bb3b13af | 483 | /* |
mbedAustin | 23:05e9bb3b13af | 484 | * Callback from onRadioNotification(), used to update the PDUCounter and process next state. |
mbedAustin | 23:05e9bb3b13af | 485 | */ |
mbedAustin | 25:e20bed9e466f | 486 | #define EDDYSTONE_SWAPFRAME_DELAYMS 1 |
mbedAustin | 23:05e9bb3b13af | 487 | void radioNotificationCallback(bool radioActive) { |
mbedAustin | 23:05e9bb3b13af | 488 | //DBG("RadioNotificationCallback : %d, %d, %d, %d",radioActive,frameIndex,TlmPduCount,TlmTimeSinceBoot); |
mbedAustin | 23:05e9bb3b13af | 489 | // Update PDUCount |
mbedAustin | 23:05e9bb3b13af | 490 | TlmPduCount++; |
mbedAustin | 23:05e9bb3b13af | 491 | frameIndex = frameIndex % EDDYSTONE_MAX_FRAMETYPE; |
mbedAustin | 23:05e9bb3b13af | 492 | |
mbedAustin | 23:05e9bb3b13af | 493 | |
mbedAustin | 23:05e9bb3b13af | 494 | // True just before an frame is sent, fale just after a frame is sent |
mbedAustin | 23:05e9bb3b13af | 495 | if(radioActive) { |
mbedAustin | 23:05e9bb3b13af | 496 | // Do Nothing |
mbedAustin | 23:05e9bb3b13af | 497 | } else { |
mbedAustin | 23:05e9bb3b13af | 498 | // state machine to control which packet is being sent |
mbedAustin | 23:05e9bb3b13af | 499 | switch(frameIndex) { |
mbedAustin | 25:e20bed9e466f | 500 | case 0: // TLM Frame |
mbedAustin | 23:05e9bb3b13af | 501 | switchFrame.attach_us(this, &EddystoneConfigService::swapOutFrames, EDDYSTONE_SWAPFRAME_DELAYMS); |
mbedAustin | 23:05e9bb3b13af | 502 | switchFlag = true; |
mbedAustin | 23:05e9bb3b13af | 503 | break; |
mbedAustin | 25:e20bed9e466f | 504 | case 1: // URL Frame |
mbedAustin | 23:05e9bb3b13af | 505 | // switch out packets |
mbedAustin | 23:05e9bb3b13af | 506 | if(switchFlag) { |
mbedAustin | 23:05e9bb3b13af | 507 | switchFrame.attach_us(this, &EddystoneConfigService::swapOutFrames, EDDYSTONE_SWAPFRAME_DELAYMS); |
mbedAustin | 23:05e9bb3b13af | 508 | switchFlag = false; |
mbedAustin | 23:05e9bb3b13af | 509 | } else { |
mbedAustin | 23:05e9bb3b13af | 510 | if((TlmPduCount % 10) == 0) { // every 10 adv packets switch the frame |
mbedAustin | 23:05e9bb3b13af | 511 | switchFlag = true; |
mbedAustin | 23:05e9bb3b13af | 512 | } |
mbedAustin | 23:05e9bb3b13af | 513 | } |
mbedAustin | 23:05e9bb3b13af | 514 | break; |
mbedAustin | 25:e20bed9e466f | 515 | case 2: // UIDFrame |
mbedAustin | 23:05e9bb3b13af | 516 | // switch out packets |
mbedAustin | 25:e20bed9e466f | 517 | if(switchFlag ) { |
mbedAustin | 23:05e9bb3b13af | 518 | switchFrame.attach_us(this, &EddystoneConfigService::swapOutFrames, EDDYSTONE_SWAPFRAME_DELAYMS); |
mbedAustin | 23:05e9bb3b13af | 519 | switchFlag = false; |
mbedAustin | 23:05e9bb3b13af | 520 | } else { |
mbedAustin | 23:05e9bb3b13af | 521 | if((TlmPduCount % 10) == 0) { // every 10 adv packets switch the frame |
mbedAustin | 23:05e9bb3b13af | 522 | switchFlag = true; |
mbedAustin | 23:05e9bb3b13af | 523 | } |
mbedAustin | 23:05e9bb3b13af | 524 | } |
mbedAustin | 23:05e9bb3b13af | 525 | break; |
mbedAustin | 23:05e9bb3b13af | 526 | } |
mbedAustin | 23:05e9bb3b13af | 527 | } |
mbedAustin | 23:05e9bb3b13af | 528 | |
mbedAustin | 23:05e9bb3b13af | 529 | return; |
mbedAustin | 23:05e9bb3b13af | 530 | } |
mbedAustin | 23:05e9bb3b13af | 531 | |
mbedAustin | 25:e20bed9e466f | 532 | /* |
mbedAustin | 25:e20bed9e466f | 533 | * This function explicityly sets the parameters used by the Eddystone beacon. |
mbedAustin | 31:11e7a505a4be | 534 | * this function can be used in leu of the config service. |
mbedAustin | 25:e20bed9e466f | 535 | * |
mbedAustin | 25:e20bed9e466f | 536 | * @param bleIn ble object used to broadcast eddystone information |
mbedAustin | 25:e20bed9e466f | 537 | * @oaram beaconPeriodus is how often ble broadcasts are mde, in mili seconds |
mbedAustin | 25:e20bed9e466f | 538 | * @param txPowerLevel sets the broadcasting power level. |
mbedAustin | 25:e20bed9e466f | 539 | * @param uidNamespaceID 10Byte Namespace UUID |
mbedAustin | 25:e20bed9e466f | 540 | * @param uidInstanceID 6Byte Instance UUID |
mbedAustin | 25:e20bed9e466f | 541 | * @param url shortened URL to broadcast (pass in as a string) |
mbedAustin | 25:e20bed9e466f | 542 | * @param urlLen length of shortened url |
mbedAustin | 25:e20bed9e466f | 543 | * @param tlmVersion version of telemetry data field to use (default to 0x00) |
mbedAustin | 25:e20bed9e466f | 544 | * |
mbedAustin | 25:e20bed9e466f | 545 | */ |
mbedAustin | 31:11e7a505a4be | 546 | void setupEddystoneAdvertisements( //BLEDevice &bleIn, |
mbedAustin | 25:e20bed9e466f | 547 | uint16_t beaconPeriodus, |
mbedAustin | 25:e20bed9e466f | 548 | uint8_t txPowerLevel, |
mbedAustin | 25:e20bed9e466f | 549 | uint8_t * uidNamespaceID = NULL, |
mbedAustin | 25:e20bed9e466f | 550 | uint8_t * uidInstanceID = NULL, |
mbedAustin | 25:e20bed9e466f | 551 | const char * url = NULL, |
mbedAustin | 25:e20bed9e466f | 552 | uint8_t urlLen = 0, |
mbedAustin | 31:11e7a505a4be | 553 | uint8_t tlmVersion = 0x00) |
mbedAustin | 25:e20bed9e466f | 554 | { |
mbedAustin | 31:11e7a505a4be | 555 | // ble = bleIn; // TODO, need to set the BLE variable to make this function standalone |
mbedAustin | 25:e20bed9e466f | 556 | uint8_t serviceData[SERVICE_DATA_MAX]; |
mbedAustin | 25:e20bed9e466f | 557 | unsigned serviceDataLen = 0; |
mbedAustin | 25:e20bed9e466f | 558 | ERR("This function is not fully implemented yet, dont use it!!"); |
mbedAustin | 25:e20bed9e466f | 559 | // Check optional frames, set their 'isSet' flags appropriately |
mbedAustin | 25:e20bed9e466f | 560 | if((uidNamespaceID != NULL) & (uidInstanceID != NULL)) { |
mbedAustin | 25:e20bed9e466f | 561 | uidIsSet = true; |
mbedAustin | 25:e20bed9e466f | 562 | setUIDFrameData(txPowerLevel,uidNamespaceID, uidInstanceID); |
mbedAustin | 25:e20bed9e466f | 563 | } else { |
mbedAustin | 25:e20bed9e466f | 564 | uidIsSet = false; |
mbedAustin | 25:e20bed9e466f | 565 | } |
mbedAustin | 25:e20bed9e466f | 566 | if(url != NULL) { |
mbedAustin | 25:e20bed9e466f | 567 | urlIsSet = true; |
mbedAustin | 25:e20bed9e466f | 568 | setURLFrameData(txPowerLevel,url); |
mbedAustin | 25:e20bed9e466f | 569 | } else { |
mbedAustin | 31:11e7a505a4be | 570 | urlIsSet = false; |
mbedAustin | 25:e20bed9e466f | 571 | } |
mbedAustin | 25:e20bed9e466f | 572 | // Default TLM frame to version 0x00, start all values at zero to be spec compliant. |
mbedAustin | 25:e20bed9e466f | 573 | setTLMFrameData(tlmVersion, 0x00,0x00); |
mbedAustin | 25:e20bed9e466f | 574 | |
mbedAustin | 25:e20bed9e466f | 575 | // Initialize Frame transition |
mbedAustin | 25:e20bed9e466f | 576 | frameIndex = 0; |
mbedAustin | 25:e20bed9e466f | 577 | uidRFU = 0; |
mbedAustin | 25:e20bed9e466f | 578 | switchFlag = true; |
mbedAustin | 25:e20bed9e466f | 579 | |
mbedAustin | 25:e20bed9e466f | 580 | /* Reinitialize the BLE stack. This will clear away the existing services and advertising state. */ |
mbedAustin | 25:e20bed9e466f | 581 | ble.shutdown(); |
mbedAustin | 25:e20bed9e466f | 582 | ble.init(); |
mbedAustin | 25:e20bed9e466f | 583 | ble.setTxPower(txPowerLevel); |
mbedAustin | 25:e20bed9e466f | 584 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
mbedAustin | 25:e20bed9e466f | 585 | ble.setAdvertisingInterval(beaconPeriodus); |
mbedAustin | 25:e20bed9e466f | 586 | |
mbedAustin | 25:e20bed9e466f | 587 | // Make double sure the PDUCount and TimeSinceBoot fields are set to zero at reset |
mbedAustin | 25:e20bed9e466f | 588 | updateTlmPduCount(0); |
mbedAustin | 25:e20bed9e466f | 589 | updateTlmTimeSinceBoot(0); |
mbedAustin | 25:e20bed9e466f | 590 | |
mbedAustin | 25:e20bed9e466f | 591 | // Construct TLM Frame in initial advertising. |
mbedAustin | 25:e20bed9e466f | 592 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; |
mbedAustin | 25:e20bed9e466f | 593 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; |
mbedAustin | 25:e20bed9e466f | 594 | serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); |
mbedAustin | 25:e20bed9e466f | 595 | |
mbedAustin | 25:e20bed9e466f | 596 | updateAdvPacket(serviceData, serviceDataLen); |
mbedAustin | 25:e20bed9e466f | 597 | ble.gap().startAdvertising(); |
mbedAustin | 25:e20bed9e466f | 598 | ble.gap().onRadioNotification(this,&EddystoneConfigService::radioNotificationCallback); |
mbedAustin | 25:e20bed9e466f | 599 | timeSinceBootTick.attach(this,&EddystoneConfigService::tsbCallback,0.1); // incriment the TimeSinceBoot ticker every 0.1s |
mbedAustin | 25:e20bed9e466f | 600 | |
mbedAustin | 25:e20bed9e466f | 601 | } |
mbedAustin | 25:e20bed9e466f | 602 | |
mbedAustin | 25:e20bed9e466f | 603 | /* |
mbedAustin | 25:e20bed9e466f | 604 | * This function actually impliments the Eddystone Beacon service. It can be called with the help of the wrapper function |
mbedAustin | 25:e20bed9e466f | 605 | * 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 | 606 | * |
mbedAustin | 25:e20bed9e466f | 607 | */ |
mbedAustin | 23:05e9bb3b13af | 608 | void setupEddystoneAdvertisements() { |
mbedAustin | 23:05e9bb3b13af | 609 | DBG("Switching Config -> adv"); |
mbedAustin | 23:05e9bb3b13af | 610 | uint8_t serviceData[SERVICE_DATA_MAX]; |
mbedAustin | 23:05e9bb3b13af | 611 | unsigned serviceDataLen = 0; |
mbedAustin | 31:11e7a505a4be | 612 | unsigned beaconPeriod = params.beaconPeriod; |
mbedAustin | 32:985642364cf5 | 613 | |
mbedAustin | 32:985642364cf5 | 614 | setUIDFrameData(params.txPowerMode,defaultUidNamespaceID, defaultUidInstanceID); |
mbedAustin | 32:985642364cf5 | 615 | //setURLFrameData(params.txPowerMode,(char *)params.uriData); |
mbedAustin | 23:05e9bb3b13af | 616 | |
mbedAustin | 23:05e9bb3b13af | 617 | // Initialize Frame transition |
mbedAustin | 25:e20bed9e466f | 618 | frameIndex = 0; |
mbedAustin | 23:05e9bb3b13af | 619 | uidRFU = 0; |
mbedAustin | 23:05e9bb3b13af | 620 | switchFlag = true; |
mbedAustin | 23:05e9bb3b13af | 621 | |
mbedAustin | 23:05e9bb3b13af | 622 | |
mbedAustin | 23:05e9bb3b13af | 623 | /* Reinitialize the BLE stack. This will clear away the existing services and advertising state. */ |
mbedAustin | 23:05e9bb3b13af | 624 | ble.shutdown(); |
mbedAustin | 23:05e9bb3b13af | 625 | ble.init(); |
mbedAustin | 32:985642364cf5 | 626 | ble.setTxPower(params.txPowerMode); |
mbedAustin | 23:05e9bb3b13af | 627 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
mbedAustin | 23:05e9bb3b13af | 628 | ble.setAdvertisingInterval(beaconPeriod); |
mbedAustin | 23:05e9bb3b13af | 629 | extern void saveURIBeaconConfigParams(const Params_t *paramsP); /* forward declaration; necessary to avoid a circular dependency. */ |
mbedAustin | 23:05e9bb3b13af | 630 | saveURIBeaconConfigParams(¶ms); |
mbedAustin | 23:05e9bb3b13af | 631 | |
mbedAustin | 32:985642364cf5 | 632 | //setTLMFrameData(0x00,0x2222,0x3333,0x01,0x02); // Initialize TLM Data, for testing, remove for release |
mbedAustin | 23:05e9bb3b13af | 633 | updateTlmPduCount(0); |
mbedAustin | 23:05e9bb3b13af | 634 | updateTlmTimeSinceBoot(0); |
mbedAustin | 23:05e9bb3b13af | 635 | |
mbedAustin | 23:05e9bb3b13af | 636 | // Construct TLM Frame in initial advertising. |
mbedAustin | 23:05e9bb3b13af | 637 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; |
mbedAustin | 23:05e9bb3b13af | 638 | serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; |
mbedAustin | 32:985642364cf5 | 639 | serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); |
mbedAustin | 23:05e9bb3b13af | 640 | |
mbedAustin | 23:05e9bb3b13af | 641 | updateAdvPacket(serviceData, serviceDataLen); |
mbedAustin | 23:05e9bb3b13af | 642 | ble.gap().startAdvertising(); |
mbedAustin | 23:05e9bb3b13af | 643 | ble.gap().onRadioNotification(this,&EddystoneConfigService::radioNotificationCallback); |
mbedAustin | 23:05e9bb3b13af | 644 | timeSinceBootTick.attach(this,&EddystoneConfigService::tsbCallback,0.1); // incriment the TimeSinceBoot ticker every 0.1s |
mbedAustin | 23:05e9bb3b13af | 645 | } |
mbedAustin | 23:05e9bb3b13af | 646 | |
mbedAustin | 23:05e9bb3b13af | 647 | private: |
mbedAustin | 23:05e9bb3b13af | 648 | // True if the lock bits are non-zero |
mbedAustin | 23:05e9bb3b13af | 649 | bool isLocked() { |
mbedAustin | 23:05e9bb3b13af | 650 | Lock_t testLock; |
mbedAustin | 23:05e9bb3b13af | 651 | memset(testLock, 0, sizeof(Lock_t)); |
mbedAustin | 23:05e9bb3b13af | 652 | return memcmp(params.lock, testLock, sizeof(Lock_t)); |
mbedAustin | 23:05e9bb3b13af | 653 | } |
mbedAustin | 23:05e9bb3b13af | 654 | |
mbedAustin | 23:05e9bb3b13af | 655 | /* |
mbedAustin | 23:05e9bb3b13af | 656 | * This callback is invoked when a GATT client attempts to modify any of the |
mbedAustin | 23:05e9bb3b13af | 657 | * characteristics of this service. Attempts to do so are also applied to |
mbedAustin | 23:05e9bb3b13af | 658 | * the internal state of this service object. |
mbedAustin | 23:05e9bb3b13af | 659 | */ |
mbedAustin | 23:05e9bb3b13af | 660 | void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) { |
mbedAustin | 23:05e9bb3b13af | 661 | uint16_t handle = writeParams->handle; |
mbedAustin | 23:05e9bb3b13af | 662 | |
mbedAustin | 23:05e9bb3b13af | 663 | if (handle == lockChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 664 | // Validated earlier |
mbedAustin | 23:05e9bb3b13af | 665 | memcpy(params.lock, writeParams->data, sizeof(Lock_t)); |
mbedAustin | 23:05e9bb3b13af | 666 | // use isLocked() in case bits are being set to all 0's |
mbedAustin | 23:05e9bb3b13af | 667 | lockedState = isLocked(); |
mbedAustin | 23:05e9bb3b13af | 668 | } else if (handle == unlockChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 669 | // Validated earlier |
mbedAustin | 23:05e9bb3b13af | 670 | memset(params.lock, 0, sizeof(Lock_t)); |
mbedAustin | 23:05e9bb3b13af | 671 | lockedState = false; |
mbedAustin | 23:05e9bb3b13af | 672 | } else if (handle == uriDataChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 673 | params.uriDataLength = writeParams->len; |
mbedAustin | 23:05e9bb3b13af | 674 | memcpy(params.uriData, writeParams->data, params.uriDataLength); |
mbedAustin | 32:985642364cf5 | 675 | //memcpy(defaultUriData, writeParams->data, params.uriDataLength); |
mbedAustin | 32:985642364cf5 | 676 | //defaultUriDataLength = params.uriDataLength; |
mbedAustin | 32:985642364cf5 | 677 | DBG("URI = %s, URILen = %d", writeParams->data, writeParams->len); |
mbedAustin | 23:05e9bb3b13af | 678 | } else if (handle == flagsChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 679 | params.flags = *(writeParams->data); |
mbedAustin | 32:985642364cf5 | 680 | DBG("flagsChar = 0x%x",params.flags); |
mbedAustin | 23:05e9bb3b13af | 681 | } else if (handle == advPowerLevelsChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 682 | memcpy(params.advPowerLevels, writeParams->data, sizeof(PowerLevels_t)); |
mbedAustin | 32:985642364cf5 | 683 | DBG("PowerLevelsChar = %4x",writeParams->data); |
mbedAustin | 23:05e9bb3b13af | 684 | } else if (handle == txPowerModeChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 685 | params.txPowerMode = *(writeParams->data); |
mbedAustin | 32:985642364cf5 | 686 | DBG("TxPowerModeChar = %d",params.txPowerMode); |
mbedAustin | 23:05e9bb3b13af | 687 | } else if (handle == beaconPeriodChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 688 | params.beaconPeriod = *((uint16_t *)(writeParams->data)); |
mbedAustin | 23:05e9bb3b13af | 689 | |
mbedAustin | 23:05e9bb3b13af | 690 | /* Re-map beaconPeriod to within permissible bounds if necessary. */ |
mbedAustin | 23:05e9bb3b13af | 691 | if (params.beaconPeriod != 0) { |
mbedAustin | 23:05e9bb3b13af | 692 | bool paramsUpdated = false; |
mbedAustin | 23:05e9bb3b13af | 693 | if (params.beaconPeriod < ble.getMinAdvertisingInterval()) { |
mbedAustin | 23:05e9bb3b13af | 694 | params.beaconPeriod = ble.getMinAdvertisingInterval(); |
mbedAustin | 23:05e9bb3b13af | 695 | paramsUpdated = true; |
mbedAustin | 23:05e9bb3b13af | 696 | } else if (params.beaconPeriod > ble.getMaxAdvertisingInterval()) { |
mbedAustin | 23:05e9bb3b13af | 697 | params.beaconPeriod = ble.getMaxAdvertisingInterval(); |
mbedAustin | 23:05e9bb3b13af | 698 | paramsUpdated = true; |
mbedAustin | 23:05e9bb3b13af | 699 | } |
mbedAustin | 23:05e9bb3b13af | 700 | if (paramsUpdated) { |
mbedAustin | 23:05e9bb3b13af | 701 | ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), reinterpret_cast<uint8_t *>(¶ms.beaconPeriod), sizeof(uint16_t)); |
mbedAustin | 23:05e9bb3b13af | 702 | } |
mbedAustin | 23:05e9bb3b13af | 703 | } |
mbedAustin | 23:05e9bb3b13af | 704 | } else if (handle == resetChar.getValueHandle()) { |
mbedAustin | 23:05e9bb3b13af | 705 | resetToDefaults(); |
mbedAustin | 23:05e9bb3b13af | 706 | } |
mbedAustin | 23:05e9bb3b13af | 707 | } |
mbedAustin | 23:05e9bb3b13af | 708 | |
mbedAustin | 23:05e9bb3b13af | 709 | /* |
mbedAustin | 23:05e9bb3b13af | 710 | * Reset the default values. |
mbedAustin | 23:05e9bb3b13af | 711 | */ |
mbedAustin | 23:05e9bb3b13af | 712 | void resetToDefaults(void) { |
mbedAustin | 23:05e9bb3b13af | 713 | lockedState = false; |
mbedAustin | 23:05e9bb3b13af | 714 | memset(params.lock, 0, sizeof(Lock_t)); |
mbedAustin | 23:05e9bb3b13af | 715 | memcpy(params.uriData, defaultUriData, URI_DATA_MAX); |
mbedAustin | 23:05e9bb3b13af | 716 | params.uriDataLength = defaultUriDataLength; |
mbedAustin | 23:05e9bb3b13af | 717 | params.flags = 0; |
mbedAustin | 23:05e9bb3b13af | 718 | memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t)); |
mbedAustin | 23:05e9bb3b13af | 719 | params.txPowerMode = TX_POWER_MODE_LOW; |
mbedAustin | 23:05e9bb3b13af | 720 | params.beaconPeriod = 1000; |
mbedAustin | 31:11e7a505a4be | 721 | //memcpy(params.uidNamespaceID, defaultUidNamespaceID, UID_NAMESPACEID_SIZE); |
mbedAustin | 31:11e7a505a4be | 722 | //memcpy(params.uidInstanceID, defaultUidInstanceID, UID_INSTANCEID_SIZE); |
mbedAustin | 31:11e7a505a4be | 723 | //params.tlmVersion = 0; |
mbedAustin | 23:05e9bb3b13af | 724 | updateCharacteristicValues(); |
mbedAustin | 23:05e9bb3b13af | 725 | } |
mbedAustin | 23:05e9bb3b13af | 726 | |
mbedAustin | 23:05e9bb3b13af | 727 | /* |
mbedAustin | 23:05e9bb3b13af | 728 | * Internal helper function used to update the GATT database following any |
mbedAustin | 23:05e9bb3b13af | 729 | * change to the internal state of the service object. |
mbedAustin | 23:05e9bb3b13af | 730 | */ |
mbedAustin | 23:05e9bb3b13af | 731 | void updateCharacteristicValues(void) { |
mbedAustin | 23:05e9bb3b13af | 732 | ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), &lockedState, 1); |
mbedAustin | 23:05e9bb3b13af | 733 | ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength); |
mbedAustin | 23:05e9bb3b13af | 734 | ble.updateCharacteristicValue(flagsChar.getValueHandle(), ¶ms.flags, 1); |
mbedAustin | 23:05e9bb3b13af | 735 | ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), |
mbedAustin | 23:05e9bb3b13af | 736 | reinterpret_cast<uint8_t *>(¶ms.beaconPeriod), sizeof(uint16_t)); |
mbedAustin | 23:05e9bb3b13af | 737 | ble.updateCharacteristicValue(txPowerModeChar.getValueHandle(), ¶ms.txPowerMode, 1); |
mbedAustin | 23:05e9bb3b13af | 738 | ble.updateCharacteristicValue(advPowerLevelsChar.getValueHandle(), |
mbedAustin | 23:05e9bb3b13af | 739 | reinterpret_cast<uint8_t *>(params.advPowerLevels), sizeof(PowerLevels_t)); |
mbedAustin | 23:05e9bb3b13af | 740 | } |
mbedAustin | 23:05e9bb3b13af | 741 | |
mbedAustin | 23:05e9bb3b13af | 742 | private: |
mbedAustin | 23:05e9bb3b13af | 743 | void lockAuthorizationCallback(GattWriteAuthCallbackParams *authParams) { |
mbedAustin | 23:05e9bb3b13af | 744 | if (lockedState) { |
mbedAustin | 23:05e9bb3b13af | 745 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; |
mbedAustin | 23:05e9bb3b13af | 746 | } else if (authParams->len != sizeof(Lock_t)) { |
mbedAustin | 23:05e9bb3b13af | 747 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; |
mbedAustin | 23:05e9bb3b13af | 748 | } else if (authParams->offset != 0) { |
mbedAustin | 23:05e9bb3b13af | 749 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbedAustin | 23:05e9bb3b13af | 750 | } else { |
mbedAustin | 23:05e9bb3b13af | 751 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbedAustin | 23:05e9bb3b13af | 752 | } |
mbedAustin | 23:05e9bb3b13af | 753 | } |
mbedAustin | 23:05e9bb3b13af | 754 | |
mbedAustin | 23:05e9bb3b13af | 755 | |
mbedAustin | 23:05e9bb3b13af | 756 | void unlockAuthorizationCallback(GattWriteAuthCallbackParams *authParams) { |
mbedAustin | 23:05e9bb3b13af | 757 | if (!lockedState) { |
mbedAustin | 23:05e9bb3b13af | 758 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbedAustin | 23:05e9bb3b13af | 759 | } else if (authParams->len != sizeof(Lock_t)) { |
mbedAustin | 23:05e9bb3b13af | 760 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; |
mbedAustin | 23:05e9bb3b13af | 761 | } else if (authParams->offset != 0) { |
mbedAustin | 23:05e9bb3b13af | 762 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbedAustin | 23:05e9bb3b13af | 763 | } else if (memcmp(authParams->data, params.lock, sizeof(Lock_t)) != 0) { |
mbedAustin | 23:05e9bb3b13af | 764 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; |
mbedAustin | 23:05e9bb3b13af | 765 | } else { |
mbedAustin | 23:05e9bb3b13af | 766 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbedAustin | 23:05e9bb3b13af | 767 | } |
mbedAustin | 23:05e9bb3b13af | 768 | } |
mbedAustin | 23:05e9bb3b13af | 769 | |
mbedAustin | 23:05e9bb3b13af | 770 | void uriDataWriteAuthorizationCallback(GattWriteAuthCallbackParams *authParams) { |
mbedAustin | 23:05e9bb3b13af | 771 | if (lockedState) { |
mbedAustin | 23:05e9bb3b13af | 772 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; |
mbedAustin | 23:05e9bb3b13af | 773 | } else if (authParams->offset != 0) { |
mbedAustin | 23:05e9bb3b13af | 774 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbedAustin | 23:05e9bb3b13af | 775 | } else { |
mbedAustin | 23:05e9bb3b13af | 776 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbedAustin | 23:05e9bb3b13af | 777 | } |
mbedAustin | 23:05e9bb3b13af | 778 | } |
mbedAustin | 23:05e9bb3b13af | 779 | |
mbedAustin | 23:05e9bb3b13af | 780 | void powerModeAuthorizationCallback(GattWriteAuthCallbackParams *authParams) { |
mbedAustin | 23:05e9bb3b13af | 781 | if (lockedState) { |
mbedAustin | 23:05e9bb3b13af | 782 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; |
mbedAustin | 23:05e9bb3b13af | 783 | } else if (authParams->len != sizeof(uint8_t)) { |
mbedAustin | 23:05e9bb3b13af | 784 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; |
mbedAustin | 23:05e9bb3b13af | 785 | } else if (authParams->offset != 0) { |
mbedAustin | 23:05e9bb3b13af | 786 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbedAustin | 23:05e9bb3b13af | 787 | } else if (*((uint8_t *)authParams->data) >= NUM_POWER_MODES) { |
mbedAustin | 23:05e9bb3b13af | 788 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED; |
mbedAustin | 23:05e9bb3b13af | 789 | } else { |
mbedAustin | 23:05e9bb3b13af | 790 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbedAustin | 23:05e9bb3b13af | 791 | } |
mbedAustin | 23:05e9bb3b13af | 792 | } |
mbedAustin | 23:05e9bb3b13af | 793 | |
mbedAustin | 23:05e9bb3b13af | 794 | template <typename T> |
mbedAustin | 23:05e9bb3b13af | 795 | void basicAuthorizationCallback(GattWriteAuthCallbackParams *authParams) { |
mbedAustin | 23:05e9bb3b13af | 796 | if (lockedState) { |
mbedAustin | 23:05e9bb3b13af | 797 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; |
mbedAustin | 23:05e9bb3b13af | 798 | } else if (authParams->len != sizeof(T)) { |
mbedAustin | 23:05e9bb3b13af | 799 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; |
mbedAustin | 23:05e9bb3b13af | 800 | } else if (authParams->offset != 0) { |
mbedAustin | 23:05e9bb3b13af | 801 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbedAustin | 23:05e9bb3b13af | 802 | } else { |
mbedAustin | 23:05e9bb3b13af | 803 | authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbedAustin | 23:05e9bb3b13af | 804 | } |
mbedAustin | 23:05e9bb3b13af | 805 | } |
mbedAustin | 23:05e9bb3b13af | 806 | |
mbedAustin | 23:05e9bb3b13af | 807 | BLEDevice &ble; |
mbedAustin | 23:05e9bb3b13af | 808 | Params_t ¶ms; |
mbedAustin | 23:05e9bb3b13af | 809 | Ticker timeSinceBootTick; |
mbedAustin | 23:05e9bb3b13af | 810 | Timeout switchFrame; |
mbedAustin | 23:05e9bb3b13af | 811 | // Default value that is restored on reset |
mbedAustin | 23:05e9bb3b13af | 812 | size_t defaultUriDataLength; |
mbedAustin | 23:05e9bb3b13af | 813 | UriData_t defaultUriData; |
mbedAustin | 23:05e9bb3b13af | 814 | UIDNamespaceID_t defaultUidNamespaceID; |
mbedAustin | 23:05e9bb3b13af | 815 | UIDInstanceID_t defaultUidInstanceID; |
mbedAustin | 23:05e9bb3b13af | 816 | int8_t defaultUidPower; |
mbedAustin | 23:05e9bb3b13af | 817 | int8_t defaultUrlPower; |
mbedAustin | 23:05e9bb3b13af | 818 | uint16_t uidRFU; |
mbedAustin | 25:e20bed9e466f | 819 | bool uidIsSet; |
mbedAustin | 25:e20bed9e466f | 820 | bool urlIsSet; |
mbedAustin | 23:05e9bb3b13af | 821 | // Default value that is restored on reset |
mbedAustin | 23:05e9bb3b13af | 822 | PowerLevels_t &defaultAdvPowerLevels; |
mbedAustin | 23:05e9bb3b13af | 823 | uint8_t lockedState; |
mbedAustin | 23:05e9bb3b13af | 824 | bool initSucceeded; |
mbedAustin | 23:05e9bb3b13af | 825 | uint8_t resetFlag; |
mbedAustin | 31:11e7a505a4be | 826 | bool switchFlag; |
mbedAustin | 23:05e9bb3b13af | 827 | |
mbedAustin | 23:05e9bb3b13af | 828 | // Private Variables for Telemetry Data |
mbedAustin | 23:05e9bb3b13af | 829 | uint8_t TlmVersion; |
mbedAustin | 23:05e9bb3b13af | 830 | volatile uint16_t TlmBatteryVoltage; |
mbedAustin | 23:05e9bb3b13af | 831 | volatile uint16_t TlmBeaconTemp; |
mbedAustin | 23:05e9bb3b13af | 832 | volatile uint32_t TlmPduCount; |
mbedAustin | 23:05e9bb3b13af | 833 | volatile uint32_t TlmTimeSinceBoot; |
mbedAustin | 23:05e9bb3b13af | 834 | |
mbedAustin | 23:05e9bb3b13af | 835 | ReadOnlyGattCharacteristic<uint8_t> lockedStateChar; |
mbedAustin | 23:05e9bb3b13af | 836 | WriteOnlyGattCharacteristic<Lock_t> lockChar; |
mbedAustin | 23:05e9bb3b13af | 837 | GattCharacteristic uriDataChar; |
mbedAustin | 23:05e9bb3b13af | 838 | WriteOnlyGattCharacteristic<Lock_t> unlockChar; |
mbedAustin | 23:05e9bb3b13af | 839 | ReadWriteGattCharacteristic<uint8_t> flagsChar; |
mbedAustin | 23:05e9bb3b13af | 840 | ReadWriteGattCharacteristic<PowerLevels_t> advPowerLevelsChar; |
mbedAustin | 23:05e9bb3b13af | 841 | ReadWriteGattCharacteristic<uint8_t> txPowerModeChar; |
mbedAustin | 23:05e9bb3b13af | 842 | ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar; |
mbedAustin | 23:05e9bb3b13af | 843 | WriteOnlyGattCharacteristic<uint8_t> resetChar; |
mbedAustin | 23:05e9bb3b13af | 844 | |
mbedAustin | 23:05e9bb3b13af | 845 | public: |
mbedAustin | 23:05e9bb3b13af | 846 | /* |
mbedAustin | 23:05e9bb3b13af | 847 | * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification). |
mbedAustin | 23:05e9bb3b13af | 848 | */ |
mbedAustin | 23:05e9bb3b13af | 849 | static void encodeURI(const char *uriDataIn, UriData_t uriDataOut, size_t &sizeofURIDataOut) { |
mbedAustin | 23:05e9bb3b13af | 850 | const char *prefixes[] = { |
mbedAustin | 23:05e9bb3b13af | 851 | "http://www.", |
mbedAustin | 23:05e9bb3b13af | 852 | "https://www.", |
mbedAustin | 23:05e9bb3b13af | 853 | "http://", |
mbedAustin | 23:05e9bb3b13af | 854 | "https://", |
mbedAustin | 23:05e9bb3b13af | 855 | }; |
mbedAustin | 23:05e9bb3b13af | 856 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
mbedAustin | 23:05e9bb3b13af | 857 | const char *suffixes[] = { |
mbedAustin | 23:05e9bb3b13af | 858 | ".com/", |
mbedAustin | 23:05e9bb3b13af | 859 | ".org/", |
mbedAustin | 23:05e9bb3b13af | 860 | ".edu/", |
mbedAustin | 23:05e9bb3b13af | 861 | ".net/", |
mbedAustin | 23:05e9bb3b13af | 862 | ".info/", |
mbedAustin | 23:05e9bb3b13af | 863 | ".biz/", |
mbedAustin | 23:05e9bb3b13af | 864 | ".gov/", |
mbedAustin | 23:05e9bb3b13af | 865 | ".com", |
mbedAustin | 23:05e9bb3b13af | 866 | ".org", |
mbedAustin | 23:05e9bb3b13af | 867 | ".edu", |
mbedAustin | 23:05e9bb3b13af | 868 | ".net", |
mbedAustin | 23:05e9bb3b13af | 869 | ".info", |
mbedAustin | 23:05e9bb3b13af | 870 | ".biz", |
mbedAustin | 23:05e9bb3b13af | 871 | ".gov" |
mbedAustin | 23:05e9bb3b13af | 872 | }; |
mbedAustin | 23:05e9bb3b13af | 873 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
mbedAustin | 23:05e9bb3b13af | 874 | |
mbedAustin | 23:05e9bb3b13af | 875 | sizeofURIDataOut = 0; |
mbedAustin | 23:05e9bb3b13af | 876 | memset(uriDataOut, 0, sizeof(UriData_t)); |
mbedAustin | 23:05e9bb3b13af | 877 | |
mbedAustin | 23:05e9bb3b13af | 878 | if ((uriDataIn == NULL) || (strlen(uriDataIn) == 0)) { |
mbedAustin | 23:05e9bb3b13af | 879 | return; |
mbedAustin | 23:05e9bb3b13af | 880 | } |
mbedAustin | 23:05e9bb3b13af | 881 | |
mbedAustin | 23:05e9bb3b13af | 882 | /* |
mbedAustin | 23:05e9bb3b13af | 883 | * handle prefix |
mbedAustin | 23:05e9bb3b13af | 884 | */ |
mbedAustin | 23:05e9bb3b13af | 885 | for (unsigned i = 0; i < NUM_PREFIXES; i++) { |
mbedAustin | 23:05e9bb3b13af | 886 | size_t prefixLen = strlen(prefixes[i]); |
mbedAustin | 23:05e9bb3b13af | 887 | if (strncmp(uriDataIn, prefixes[i], prefixLen) == 0) { |
mbedAustin | 23:05e9bb3b13af | 888 | uriDataOut[sizeofURIDataOut++] = i; |
mbedAustin | 23:05e9bb3b13af | 889 | uriDataIn += prefixLen; |
mbedAustin | 23:05e9bb3b13af | 890 | break; |
mbedAustin | 23:05e9bb3b13af | 891 | } |
mbedAustin | 23:05e9bb3b13af | 892 | } |
mbedAustin | 23:05e9bb3b13af | 893 | |
mbedAustin | 23:05e9bb3b13af | 894 | /* |
mbedAustin | 23:05e9bb3b13af | 895 | * handle suffixes |
mbedAustin | 23:05e9bb3b13af | 896 | */ |
mbedAustin | 23:05e9bb3b13af | 897 | while (*uriDataIn && (sizeofURIDataOut < URI_DATA_MAX)) { |
mbedAustin | 23:05e9bb3b13af | 898 | /* check for suffix match */ |
mbedAustin | 23:05e9bb3b13af | 899 | unsigned i; |
mbedAustin | 23:05e9bb3b13af | 900 | for (i = 0; i < NUM_SUFFIXES; i++) { |
mbedAustin | 23:05e9bb3b13af | 901 | size_t suffixLen = strlen(suffixes[i]); |
mbedAustin | 23:05e9bb3b13af | 902 | if (strncmp(uriDataIn, suffixes[i], suffixLen) == 0) { |
mbedAustin | 23:05e9bb3b13af | 903 | uriDataOut[sizeofURIDataOut++] = i; |
mbedAustin | 23:05e9bb3b13af | 904 | uriDataIn += suffixLen; |
mbedAustin | 23:05e9bb3b13af | 905 | break; /* from the for loop for checking against suffixes */ |
mbedAustin | 23:05e9bb3b13af | 906 | } |
mbedAustin | 23:05e9bb3b13af | 907 | } |
mbedAustin | 23:05e9bb3b13af | 908 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
mbedAustin | 23:05e9bb3b13af | 909 | if (i == NUM_SUFFIXES) { |
mbedAustin | 23:05e9bb3b13af | 910 | uriDataOut[sizeofURIDataOut++] = *uriDataIn; |
mbedAustin | 23:05e9bb3b13af | 911 | ++uriDataIn; |
mbedAustin | 23:05e9bb3b13af | 912 | } |
mbedAustin | 23:05e9bb3b13af | 913 | } |
mbedAustin | 23:05e9bb3b13af | 914 | } |
mbedAustin | 23:05e9bb3b13af | 915 | }; |
mbedAustin | 23:05e9bb3b13af | 916 | |
mbedAustin | 23:05e9bb3b13af | 917 | #endif // SERVICES_ZIPBEACONCONFIGSERVICE_H_ |