Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

Committer:
mbedAustin
Date:
Thu Sep 03 02:55:49 2015 +0000
Revision:
37:22175faa76be
Parent:
36:ccb188bcfef1
Child:
38:8fdb16f67e7c
changed thing to make UID's work correctly;

Who changed what in which revision?

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