test
Dependents: BLE_PowerBank_HeyFaradey
Fork of BLE_API by
Revision 832:5dac246f8f02, committed 2015-09-29
- Comitter:
- rgrover1
- Date:
- Tue Sep 29 09:54:19 2015 +0100
- Parent:
- 831:47413688398a
- Child:
- 833:d494ad3e87bd
- Commit message:
- Synchronized with git rev 5aa78955
Author: Rohit Grover
mostly white-sapce improvements
Changed in this revision
--- a/ble/services/EddystoneConfigService.h Tue Sep 29 09:54:19 2015 +0100 +++ b/ble/services/EddystoneConfigService.h Tue Sep 29 09:54:19 2015 +0100 @@ -14,17 +14,13 @@ * limitations under the License. */ -#ifndef SERVICES_EDDYSTONEBEACONCONFIGSERVICE_H_ -#define SERVICES_EDDYSTONEBEACONCONFIGSERVICE_H_ +#ifndef SERVICES_EDDYSTONE_BEACON_CONFIG_SERVICE_H_ +#define SERVICES_EDDYSTONE_BEACON_CONFIG_SERVICE_H_ #include "mbed.h" #include "ble/BLE.h" #include "ble/services/EddystoneService.h" -#define EDDYSTONE_NUM_UID_FRAMES 10 -#define EDDYSTONE_NUM_URL_FRAMES 50 - - #define UUID_URI_BEACON(FIRST, SECOND) { \ 0xee, 0x0c, FIRST, SECOND, 0x87, 0x86, 0x40, 0xba, \ 0xab, 0x96, 0x99, 0xb9, 0x1a, 0xc9, 0x81, 0xd8, \ @@ -40,7 +36,7 @@ static const uint8_t UUID_TX_POWER_MODE_CHAR[] = UUID_URI_BEACON(0x20, 0x87); static const uint8_t UUID_BEACON_PERIOD_CHAR[] = UUID_URI_BEACON(0x20, 0x88); static const uint8_t UUID_RESET_CHAR[] = UUID_URI_BEACON(0x20, 0x89); -extern const uint8_t BEACON_EDDYSTONE[2]; +extern const uint8_t BEACON_EDDYSTONE[2]; /** * @class EddystoneConfigService @@ -50,34 +46,34 @@ */ class EddystoneConfigService { -public: +public: /** * @brief Transmission Power Modes for UriBeacon */ - static const uint8_t TX_POWER_MODE_LOWEST = 0; /*!< Lowest TX power mode */ - static const uint8_t TX_POWER_MODE_LOW = 1; /*!< Low TX power mode */ - static const uint8_t TX_POWER_MODE_MEDIUM = 2; /*!< Medium TX power mode */ - static const uint8_t TX_POWER_MODE_HIGH = 3; /*!< High TX power mode */ - static const unsigned NUM_POWER_MODES = 4; /*!< Number of Power Modes defined */ + enum { + TX_POWER_MODE_LOWEST, + TX_POWER_MODE_LOW, + TX_POWER_MODE_MEDIUM, + TX_POWER_MODE_HIGH, + NUM_POWER_MODES + }; - static const int ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service. - static const int SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets + static const unsigned ADVERTISING_INTERVAL_MSEC = 1000; // Advertising interval for config service. + static const unsigned SERVICE_DATA_MAX = 31; // Maximum size of service data in ADV packets - typedef uint8_t Lock_t[16]; /* 128 bits */ + typedef uint8_t Lock_t[16]; /* 128 bits */ typedef int8_t PowerLevels_t[NUM_POWER_MODES]; // There are currently 3 subframes defined, URI, UID, and TLM #define EDDYSTONE_MAX_FRAMETYPE 3 - void (*frames[EDDYSTONE_MAX_FRAMETYPE])(uint8_t *, uint32_t); - uint8_t frameIndex; - static const int URI_DATA_MAX = 18; - typedef uint8_t UriData_t[URI_DATA_MAX]; + static const unsigned URI_DATA_MAX = 18; + typedef uint8_t UriData_t[URI_DATA_MAX]; // UID Frame Type subfields - static const int UID_NAMESPACEID_SIZE = 10; - typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE]; - static const int UID_INSTANCEID_SIZE = 6; - typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE]; + static const size_t UID_NAMESPACEID_SIZE = 10; + typedef uint8_t UIDNamespaceID_t[UID_NAMESPACEID_SIZE]; + static const size_t UID_INSTANCEID_SIZE = 6; + typedef uint8_t UIDInstanceID_t[UID_INSTANCEID_SIZE]; // Eddystone Frame Type ID static const uint8_t FRAME_TYPE_UID = 0x00; @@ -89,27 +85,28 @@ struct Params_t { // Config Data - bool isConfigured; // Flag for configuration being complete, True = configured, false = not configured. Reset at instantiation, used for external callbacks. - uint8_t lockedState; - Lock_t lock; - uint8_t flags; - PowerLevels_t advPowerLevels; // Current value of AdvertisedPowerLevels - uint8_t txPowerMode; // Firmware power levels used with setTxPower() - uint16_t beaconPeriod; + bool isConfigured; // Flag for configuration being complete, + // True = configured, false = not configured. Reset at instantiation, used for external callbacks. + uint8_t lockedState; + Lock_t lock; + uint8_t flags; + PowerLevels_t advPowerLevels; // Current value of AdvertisedPowerLevels + uint8_t txPowerMode; // Firmware power levels used with setTxPower() + uint16_t beaconPeriod; // TLM Frame Data - uint8_t tlmVersion; // version of TLM packet - bool tlmEnabled; - float tlmBeaconPeriod; // how often to broadcat TLM frame, in S + uint8_t tlmVersion; // version of TLM packet + bool tlmEnabled; + float tlmBeaconPeriod; // how often to broadcat TLM frame, in seconds. // URI Frame Data - uint8_t uriDataLength; - UriData_t uriData; - bool uriEnabled; - float uriBeaconPeriod; // how often to broadcast URIFrame, in S + uint8_t uriDataLength; + UriData_t uriData; + bool uriEnabled; + float uriBeaconPeriod; // how often to broadcast URIFrame, in seconds. // UID Frame Data - UIDNamespaceID_t uidNamespaceID; // UUID type, Namespace ID, 10B - UIDInstanceID_t uidInstanceID; // UUID type, Instance ID, 6B - bool uidEnabled; - float uidBeaconPeriod; // how often to broadcast UID Frame, in S + UIDNamespaceID_t uidNamespaceID; // UUID type, Namespace ID, 10B + UIDInstanceID_t uidInstanceID; // UUID type, Instance ID, 6B + bool uidEnabled; + float uidBeaconPeriod; // how often to broadcast UID Frame, in seconds. }; /** @@ -149,27 +146,9 @@ advPowerLevelsChar(UUID_ADV_POWER_LEVELS_CHAR, ¶ms.advPowerLevels), txPowerModeChar(UUID_TX_POWER_MODE_CHAR, ¶ms.txPowerMode), beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, ¶ms.beaconPeriod), - resetChar(UUID_RESET_CHAR, &resetFlag) - { + resetChar(UUID_RESET_CHAR, &resetFlag) { // set eddystone as not configured yet. Used to exit config before timeout if GATT services are written to. params.isConfigured = false; - } - - /* - * @breif Start EddystoneConfig advertising. This function should be called after the EddystoneConfig constructor and after all the frames have been added. - * @paramsP[in] resetToDefaultsFlag - * Applies to the state of the 'paramsIn' parameter. - * If true, it indicates that paramsIn is potentially - * un-initialized, and default values should be used - * instead. Otherwise, paramsIn overrides the defaults. - * - */ - void start(bool resetToDefaultsFlag){ - INFO("reset to defaults flag = %d", resetToDefaultsFlag); - if (!resetToDefaultsFlag && (params.uriDataLength > URI_DATA_MAX)) { - INFO("Reset to Defaults triggered"); - resetToDefaultsFlag = true; - } lockChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::lockAuthorizationCallback); unlockChar.setWriteAuthorizationCallback(this, &EddystoneConfigService::unlockAuthorizationCallback); @@ -188,16 +167,33 @@ GattService configService(UUID_URI_BEACON_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.addService(configService); + ble.onDataWritten(this, &EddystoneConfigService::onDataWrittenCallback); + } + + /** + * @brief Start EddystoneConfig advertising. This function should be called + * after the EddystoneConfig constructor and after all the frames have been added. + * + * @paramsP[in] resetToDefaultsFlag + * Applies to the state of the 'paramsIn' parameter. + * If true, it indicates that paramsIn is potentially + * un-initialized, and default values should be used + * instead. Otherwise, paramsIn overrides the defaults. + */ + void start(bool resetToDefaultsFlag){ + INFO("reset to defaults flag = %d", resetToDefaultsFlag); + if (!resetToDefaultsFlag && (params.uriDataLength > URI_DATA_MAX)) { + INFO("Reset to Defaults triggered"); + resetToDefaultsFlag = true; + } + if (resetToDefaultsFlag) { resetToDefaults(); } else { updateCharacteristicValues(); } - - ble.onDataWritten(this, &EddystoneConfigService::onDataWrittenCallback); setupEddystoneConfigAdvertisements(); /* Setup advertising for the configService. */ - initSucceeded = true; } @@ -207,48 +203,47 @@ bool initSuccessfully(void) const { return initSucceeded; } - + /* - * @breif Function to update the default values for the TLM frame. Only applied if Reset Defaults is applied. + * @brief Function to update the default values for the TLM frame. Only applied if Reset Defaults is applied. * * @param[in] tlmVersionIn Version of the TLM frame being used * @param[in] advPeriodInMin how long between TLM frames being advertised, this is measured in minutes. * */ void setDefaultTLMFrameData(uint8_t tlmVersionIn = 0, float advPeriodInSec = 60){ - DBG("Setting Default TLM Data, version = %d, advPeriodInMind= %f",tlmVersionIn,advPeriodInSec); - defaultTlmVersion = tlmVersionIn; - TlmBatteryVoltage = 0; - TlmBeaconTemp = 0; - TlmPduCount = 0; - TlmTimeSinceBoot = 0; + DBG("Setting Default TLM Data, version = %d, advPeriodInMind= %f", tlmVersionIn, advPeriodInSec); + defaultTlmVersion = tlmVersionIn; + TlmBatteryVoltage = 0; + TlmBeaconTemp = 0; + TlmPduCount = 0; + TlmTimeSinceBoot = 0; defaultTlmAdvPeriod = advPeriodInSec; - tlmIsSet = true; // flag to add this to eddystone service when config is done - + tlmIsSet = true; // flag to add this to eddystone service when config is done } - + /* - * @breif Function to update the default values for the URI frame. Only applied if Reset Defaults is applied. + * @brief Function to update the default values for the URI frame. Only applied if Reset Defaults is applied. * * @param[in] uriIn url to advertise * @param[in] advPeriod how long to advertise the url for, measured in number of ADV frames. * */ - void setDefaultURIFrameData(const char * uriIn, float advPeriod = 1){ + void setDefaultURIFrameData(const char *uriIn, float advPeriod = 1){ DBG("Setting Default URI Data"); // Set URL Frame - EddystoneService::encodeURL(uriIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting + EddystoneService::encodeURL(uriIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting if (defaultUriDataLength > URI_DATA_MAX) { return; } INFO("\t URI input = %s : %d", uriIn, defaultUriDataLength); - INFO("\t default URI = %s : %d ",defaultUriData,defaultUriDataLength ); + INFO("\t default URI = %s : %d ", defaultUriData, defaultUriDataLength ); defaultUriAdvPeriod = advPeriod; - urlIsSet = true; // flag to add this to eddystone service when config is done + urlIsSet = true; // flag to add this to eddystone service when config is done } - + /* - * @breif Function to update the default values for the UID frame. Only applied if Reset Defaults is applied. + * @brief Function to update the default values for the UID frame. Only applied if Reset Defaults is applied. * * @param[in] namespaceID 10Byte Namespace ID * @param[in] instanceID 6Byte Instance ID @@ -261,7 +256,7 @@ memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE); memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE); defaultUidAdvPeriod = advPeriod; - uidIsSet = true; // flag to add this to eddystone service when config is done + uidIsSet = true; // flag to add this to eddystone service when config is done } /* Start out by advertising the configService for a limited time after @@ -304,24 +299,26 @@ extern void saveURIBeaconConfigParams(const Params_t *paramsP); /* forward declaration; necessary to avoid a circular dependency. */ saveURIBeaconConfigParams(¶ms); INFO("Saved Params to Memory.") - // Setup Eddystone Service - static EddystoneService eddyServ(ble,params.beaconPeriod,radioPowerLevels[params.txPowerMode]); + // Setup Eddystone Service + static EddystoneService eddyServ(ble, params.beaconPeriod, radioPowerLevels[params.txPowerMode]); // Set configured frames (TLM,UID,URI...etc) - if(params.tlmEnabled){ - eddyServ.setTLMFrameData(params.tlmVersion,params.tlmBeaconPeriod); + if (params.tlmEnabled) { + eddyServ.setTLMFrameData(params.tlmVersion, params.tlmBeaconPeriod); } - if(params.uriEnabled){ + if (params.uriEnabled) { eddyServ.setURLFrameData(params.advPowerLevels[params.txPowerMode], (const char *) params.uriData, params.uriBeaconPeriod); } - if(params.uidEnabled){ - eddyServ.setUIDFrameData(params.advPowerLevels[params.txPowerMode], (uint8_t *) params.uidNamespaceID, (uint8_t *) params.uidInstanceID, params.uidBeaconPeriod); - } - // Start Advertising the eddystone service. - eddyServ.start(); + if (params.uidEnabled) { + eddyServ.setUIDFrameData(params.advPowerLevels[params.txPowerMode], + (uint8_t *)params.uidNamespaceID, + (uint8_t *)params.uidInstanceID, + params.uidBeaconPeriod); + } + // Start Advertising the eddystone service. + eddyServ.start(); } private: - /* * This callback is invoked when a GATT client attempts to modify any of the * characteristics of this service. Attempts to do so are also applied to @@ -342,32 +339,32 @@ INFO("Device Unlocked"); } else if (handle == uriDataChar.getValueHandle()) { params.uriDataLength = writeParams->len; - memset(params.uriData,0x00,URI_DATA_MAX); // clear URI string + memset(params.uriData, 0x00, URI_DATA_MAX); // clear URI string memcpy(params.uriData, writeParams->data, params.uriDataLength); // set URI string params.uriEnabled = true; INFO("URI = %s, URILen = %d", writeParams->data, writeParams->len); } else if (handle == flagsChar.getValueHandle()) { params.flags = *(writeParams->data); - INFO("flagsChar = 0x%x",params.flags); + INFO("flagsChar = 0x%x", params.flags); } else if (handle == advPowerLevelsChar.getValueHandle()) { memcpy(params.advPowerLevels, writeParams->data, sizeof(PowerLevels_t)); - INFO("PowerLevelsChar = %4x",params.advPowerLevels); + INFO("PowerLevelsChar = %4x", params.advPowerLevels); } else if (handle == txPowerModeChar.getValueHandle()) { params.txPowerMode = *(writeParams->data); - INFO("TxPowerModeChar = %d",params.txPowerMode); + INFO("TxPowerModeChar = %d", params.txPowerMode); } else if (handle == beaconPeriodChar.getValueHandle()) { params.beaconPeriod = *((uint16_t *)(writeParams->data)); - INFO("BeaconPeriod = %d",params.beaconPeriod); + INFO("BeaconPeriod = %d", params.beaconPeriod); /* Re-map beaconPeriod to within permissible bounds if necessary. */ if (params.beaconPeriod != 0) { bool paramsUpdated = false; if (params.beaconPeriod < ble.getMinAdvertisingInterval()) { params.beaconPeriod = ble.getMinAdvertisingInterval(); - paramsUpdated = true; + paramsUpdated = true; } else if (params.beaconPeriod > ble.getMaxAdvertisingInterval()) { params.beaconPeriod = ble.getMaxAdvertisingInterval(); - paramsUpdated = true; + paramsUpdated = true; } if (paramsUpdated) { ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), reinterpret_cast<uint8_t *>(¶ms.beaconPeriod), sizeof(uint16_t)); @@ -387,30 +384,30 @@ void resetToDefaults(void) { INFO("Resetting to defaults"); // General - params.lockedState = false; + params.lockedState = false; memset(params.lock, 0, sizeof(Lock_t)); - params.flags = 0x10; + params.flags = 0x10; memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t)); - params.txPowerMode = TX_POWER_MODE_LOW; - params.beaconPeriod = 1000; - + params.txPowerMode = TX_POWER_MODE_LOW; + params.beaconPeriod = 1000; + // TLM Frame - params.tlmVersion = defaultTlmVersion; + params.tlmVersion = defaultTlmVersion; params.tlmBeaconPeriod = defaultTlmAdvPeriod; - params.tlmEnabled = tlmIsSet; - + params.tlmEnabled = tlmIsSet; + // URL Frame memcpy(params.uriData, defaultUriData, URI_DATA_MAX); - params.uriDataLength = defaultUriDataLength; - params.uriBeaconPeriod = defaultUriAdvPeriod; - params.uriEnabled = urlIsSet; - + params.uriDataLength = defaultUriDataLength; + params.uriBeaconPeriod = defaultUriAdvPeriod; + params.uriEnabled = urlIsSet; + // UID Frame memcpy(params.uidNamespaceID, defaultUidNamespaceID, UID_NAMESPACEID_SIZE); memcpy(params.uidInstanceID, defaultUidInstanceID, UID_INSTANCEID_SIZE); params.uidBeaconPeriod = defaultUidAdvPeriod; - params.uidEnabled = uidIsSet; - + params.uidEnabled = uidIsSet; + updateCharacteristicValues(); } @@ -420,8 +417,7 @@ */ void updateCharacteristicValues(void) { ble.updateCharacteristicValue(lockedStateChar.getValueHandle(), ¶ms.lockedState, 1); - ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength); // TODO: this isnt updating the initial URI in config mode, need to figure out why. - INFO("updateCharacteristicValues - URI, %s : %d",params.uriData, params.uriDataLength); + ble.updateCharacteristicValue(uriDataChar.getValueHandle(), params.uriData, params.uriDataLength); ble.updateCharacteristicValue(flagsChar.getValueHandle(), ¶ms.flags, 1); ble.updateCharacteristicValue(beaconPeriodChar.getValueHandle(), reinterpret_cast<uint8_t *>(¶ms.beaconPeriod), sizeof(uint16_t)); @@ -443,7 +439,6 @@ } } - void unlockAuthorizationCallback(GattWriteAuthCallbackParams *authParams) { if ((!params.lockedState) && (authParams->len == sizeof(Lock_t))) { authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; @@ -495,41 +490,41 @@ } } - BLEDevice &ble; - Params_t ¶ms; - Ticker timeSinceBootTick; - Timeout switchFrame; -// Default value that is restored on reset - PowerLevels_t &defaultAdvPowerLevels; // this goes into the advertising frames (radio power measured at 1m from device) - PowerLevels_t &radioPowerLevels; // this configures the power levels of the radio - uint8_t lockedState; - bool initSucceeded; - uint8_t resetFlag; - bool switchFlag; + BLEDevice &ble; + Params_t ¶ms; + Ticker timeSinceBootTick; + Timeout switchFrame; + // Default value that is restored on reset + PowerLevels_t &defaultAdvPowerLevels; // this goes into the advertising frames (radio power measured at 1m from device) + PowerLevels_t &radioPowerLevels; // this configures the power levels of the radio + uint8_t lockedState; + bool initSucceeded; + uint8_t resetFlag; + bool switchFlag; -//UID Default value that is restored on reset - UIDNamespaceID_t defaultUidNamespaceID; - UIDInstanceID_t defaultUidInstanceID; - float defaultUidAdvPeriod; - int8_t defaultUidPower; - uint16_t uidRFU; - bool uidIsSet; + //UID Default value that is restored on reset + UIDNamespaceID_t defaultUidNamespaceID; + UIDInstanceID_t defaultUidInstanceID; + float defaultUidAdvPeriod; + int8_t defaultUidPower; + uint16_t uidRFU; + bool uidIsSet; -//URI Default value that is restored on reset - uint8_t defaultUriDataLength; - UriData_t defaultUriData; - int8_t defaultUrlPower; - float defaultUriAdvPeriod; - bool urlIsSet; + //URI Default value that is restored on reset + uint8_t defaultUriDataLength; + UriData_t defaultUriData; + int8_t defaultUrlPower; + float defaultUriAdvPeriod; + bool urlIsSet; -//TLM Default value that is restored on reset - uint8_t defaultTlmVersion; - float defaultTlmAdvPeriod; - volatile uint16_t TlmBatteryVoltage; - volatile uint16_t TlmBeaconTemp; - volatile uint32_t TlmPduCount; - volatile uint32_t TlmTimeSinceBoot; - bool tlmIsSet; + //TLM Default value that is restored on reset + uint8_t defaultTlmVersion; + float defaultTlmAdvPeriod; + volatile uint16_t TlmBatteryVoltage; + volatile uint16_t TlmBeaconTemp; + volatile uint32_t TlmPduCount; + volatile uint32_t TlmTimeSinceBoot; + bool tlmIsSet; ReadOnlyGattCharacteristic<uint8_t> lockedStateChar; WriteOnlyGattCharacteristic<Lock_t> lockChar; @@ -540,7 +535,6 @@ ReadWriteGattCharacteristic<uint8_t> txPowerModeChar; ReadWriteGattCharacteristic<uint16_t> beaconPeriodChar; WriteOnlyGattCharacteristic<uint8_t> resetChar; - }; -#endif // SERVICES_EDDYSTONEBEACONCONFIGSERVICE_H_ \ No newline at end of file +#endif // SERVICES_EDDYSTONE_BEACON_CONFIG_SERVICE_H_ \ No newline at end of file
--- a/ble/services/EddystoneService.h Tue Sep 29 09:54:19 2015 +0100 +++ b/ble/services/EddystoneService.h Tue Sep 29 09:54:19 2015 +0100 @@ -24,20 +24,32 @@ //Debug is disabled by default #if 0 -#define DBG(x, ...) printf("[EddyStone: DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define WARN(x, ...) printf("[EddyStone: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define ERR(x, ...) printf("[EddyStone: ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#else +#define DBG(MSG, ...) printf("[EddyStone: DBG]" MSG " \t[%s,%d]\r\n", \ + ## __VA_ARGS__, \ + __FILE__, \ + __LINE__); +#define WARN(MSG, ...) printf("[EddyStone: WARN]" MSG " \t[%s,%d]\r\n", \ + ## __VA_ARGS__, \ + __FILE__, \ + __LINE__); +#define ERR(MSG, ...) printf("[EddyStone: ERR]" MSG " \t[%s,%d]\r\n", \ + ## __VA_ARGS__, \ + __FILE__, \ + __LINE__); +#else // if 0 #define DBG(x, ...) //wait_us(10); #define WARN(x, ...) //wait_us(10); #define ERR(x, ...) -#endif +#endif // if 0 #if 0 -#define INFO(x, ...) printf("[EddyStone: INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#else +#define INFO(x, ...) printf("[EddyStone: INFO]"x " \t[%s,%d]\r\n", \ + ## __VA_ARGS__, \ + __FILE__, \ + __LINE__); +#else // if 0 #define INFO(x, ...) -#endif +#endif // if 0 /** * @class Eddystone @@ -62,7 +74,7 @@ void (*frames[EDDYSTONE_MAX_FRAMETYPE])(uint8_t *, uint32_t); static const int URI_DATA_MAX = 18; typedef uint8_t UriData_t[URI_DATA_MAX]; - CircularBuffer<FrameTypes,EDDYSTONE_MAX_FRAMETYPE> overflow; + CircularBuffer<FrameTypes, EDDYSTONE_MAX_FRAMETYPE> overflow; // UID Frame Type subfields static const int UID_NAMESPACEID_SIZE = 10; @@ -78,90 +90,98 @@ static const uint8_t FRAME_SIZE_TLM = 14; // TLM frame is a constant 14Bytes static const uint8_t FRAME_SIZE_UID = 20; // includes RFU bytes - /* - * Set Eddystone UID Frame information. - * @param[in] power TX Power in dB measured at 0 meters from the device. Range of -100 to +20 dB. - * @param namespaceID 10B namespace ID - * @param instanceID 6B instance ID - * @param RFU 2B of RFU, initialized to 0x0000 and not broadcast, included for future reference. - * - */ - void setUIDFrameData(int8_t power, UIDNamespaceID_t namespaceID, UIDInstanceID_t instanceID, uint32_t uidAdvPeriodIn, uint16_t RFU = 0x0000) { - if(0 == uidAdvPeriodIn){ + /** + * Set Eddystone UID Frame information. + * + * @param[in] power TX Power in dB measured at 0 meters from the device. Range of -100 to +20 dB. + * @param[in] namespaceID 10B namespace ID + * @param[in] instanceID 6B instance ID + * @param[in] RFU 2B of RFU, initialized to 0x0000 and not broadcast, included for future reference. + */ + void setUIDFrameData(int8_t power, + UIDNamespaceID_t namespaceID, + UIDInstanceID_t instanceID, + uint32_t uidAdvPeriodIn, + uint16_t RFU = 0x0000) { + if (0 == uidAdvPeriodIn) { uidIsSet = false; return; } - if(power > 20) { + if (power > 20) { power = 20; } - if(power < -100) { + if (power < -100) { power = -100; } + defaultUidPower = power; memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE); memcpy(defaultUidInstanceID, instanceID, UID_INSTANCEID_SIZE); - uidRFU = (uint16_t)RFU; // this is probably bad form, but it doesnt really matter yet. + uidRFU = (uint16_t)RFU; // this is probably bad form, but it doesn't really matter yet. uidAdvPeriod = uidAdvPeriodIn; - uidIsSet = true; // set toggle to advertise UID frames - return; + uidIsSet = true; // set toggle to advertise UID frames } /* * Construct UID frame from private variables * @param[in/out] Data pointer to array to store constructed frame in - * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to + * @param[in] maxSize number of bytes left in array, effectively how much empty space is available to write to * @return number of bytes used. negative number indicates error message. */ - int constructUIDFrame(uint8_t * Data, uint8_t maxSize) { + unsigned constructUIDFrame(uint8_t *Data, uint8_t maxSize) { + unsigned index = 0; - int index = 0; Data[index++] = FRAME_TYPE_UID; // 1B Type - if(defaultUidPower > 20) { - defaultUidPower = 20; // enforce range of vaild values. + + if (defaultUidPower > 20) { + defaultUidPower = 20; // enforce range of vaild values. } - if(defaultUidPower < -100) { + if (defaultUidPower < -100) { defaultUidPower = -100; } Data[index++] = defaultUidPower; // 1B Power @ 0meter + DBG("UID NamespaceID = '0x"); - for(int x = 0; x < UID_NAMESPACEID_SIZE; x++) { // 10B Namespce ID + for (size_t x = 0; x < UID_NAMESPACEID_SIZE; x++) { // 10B Namespace ID Data[index++] = defaultUidNamespaceID[x]; - DBG("%x,",defaultUidNamespaceID[x]); + DBG("%x,", defaultUidNamespaceID[x]); } DBG("'\r\n"); - DBG("UID InstancdID = '0x"); - for(int x = 0; x< UID_INSTANCEID_SIZE; x++) { // 6B Instance ID + + DBG("UID InstanceID = '0x"); + for (size_t x = 0; x< UID_INSTANCEID_SIZE; x++) { // 6B Instance ID Data[index++] = defaultUidInstanceID[x]; - DBG("%x,",defaultUidInstanceID[x]); + DBG("%x,", defaultUidInstanceID[x]); } DBG("'\r\n"); - if(0 != uidRFU) { // 2B RFU, include if non-zero, otherwise ignore + + if (0 != uidRFU) { // 2B RFU, include if non-zero, otherwise ignore Data[index++] = (uint8_t)(uidRFU >> 0); Data[index++] = (uint8_t)(uidRFU >> 8); } - DBG("construcUIDFrame %d, %d",maxSize,index); + DBG("construcUIDFrame %d, %d", maxSize, index); return index; } - /* - * Set Eddystone URL Frame information. - * @param[in] power TX Power in dB measured at 0 meters from the device. - * @param url URL to encode - * @param urlAdvPeriodIn How long to advertise the URL frame (measured in # of adv periods) - * @return false on success, true on failure. - */ - bool setURLFrameData(int8_t power, const char * urlIn, uint32_t urlAdvPeriodIn) { - if(0 == urlAdvPeriodIn){ + /** + * Set Eddystone URL Frame information. + * @param[in] power TX Power in dB measured at 0 meters from the device. + * @param[in] url URL to encode + * @param[in] urlAdvPeriodIn How long to advertise the URL frame (measured in # of adv periods) + * @return false on success, true on failure. + */ + bool setURLFrameData(int8_t power, const char *urlIn, uint32_t urlAdvPeriodIn) { + if (0 == urlAdvPeriodIn) { urlIsSet = false; return false; } defaultUrlPower = power; encodeURL(urlIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting if (defaultUriDataLength > URI_DATA_MAX) { - return true; // error, URL is too big + return true; // error, URL is too big } urlAdvPeriod = urlAdvPeriodIn; - urlIsSet = true; + urlIsSet = true; return false; } @@ -171,14 +191,14 @@ * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to * @return number of bytes used. negative number indicates error message. */ - int constructURLFrame(uint8_t * Data, uint8_t maxSize) { + int constructURLFrame(uint8_t *Data, uint8_t maxSize) { int index = 0; Data[index++] = FRAME_TYPE_URL; // 1B Type Data[index++] = defaultUrlPower; // 1B TX Power - for(int x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL + for (int x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL Data[index++] = defaultUriData[x]; } - DBG("constructURLFrame: %d, %d",maxSize,index); + DBG("constructURLFrame: %d, %d", maxSize, index); return index; } @@ -190,19 +210,23 @@ * @param beaconTemp in 8.8 floating point notation * */ - void setTLMFrameData(uint8_t version = 0, uint32_t advPeriod = 60, uint16_t batteryVoltage = 0, uint16_t beaconTemp = 0, uint32_t pduCount = 0, uint32_t timeSinceBoot = 0) { - if(0 == advPeriod){ + void setTLMFrameData(uint8_t version = 0, + uint32_t advPeriod = 60, + uint16_t batteryVoltage = 0, + uint16_t beaconTemp = 0, + uint32_t pduCount = 0, + uint32_t timeSinceBoot = 0) { + if (0 == advPeriod) { tlmIsSet = false; return; } - TlmVersion = version; + TlmVersion = version; TlmBatteryVoltage = batteryVoltage; - TlmBeaconTemp = beaconTemp; - TlmPduCount = pduCount; // reset - TlmTimeSinceBoot = timeSinceBoot; // reset - TlmAdvPeriod = advPeriod; - tlmIsSet = true; // TLM Data has been enabled - return; + TlmBeaconTemp = beaconTemp; + TlmPduCount = pduCount; // reset + TlmTimeSinceBoot = timeSinceBoot; // reset + TlmAdvPeriod = advPeriod; + tlmIsSet = true; // TLM Data has been enabled } /* @@ -211,23 +235,23 @@ * @param[in] maxSize number of bytes left in array, effectively how much emtpy space is available to write to * @return number of bytes used. negative number indicates error message. */ - int constructTLMFrame(uint8_t * Data, uint8_t maxSize) { + int constructTLMFrame(uint8_t *Data, uint8_t maxSize) { int index = 0; Data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry Data[index++] = TlmVersion; // TLM Version Number - Data[index++] = (uint8_t)(TlmBatteryVoltage>>8); // Battery Voltage[0] - Data[index++] = (uint8_t)(TlmBatteryVoltage>>0); // Battery Voltage[1] - Data[index++] = (uint8_t)(TlmBeaconTemp>>8); // Beacon Temp[0] - Data[index++] = (uint8_t)(TlmBeaconTemp>>0); // Beacon Temp[1] - Data[index++] = (uint8_t)(TlmPduCount>>24); // PDU Count [0] - Data[index++] = (uint8_t)(TlmPduCount>>16); // PDU Count [1] - Data[index++] = (uint8_t)(TlmPduCount>>8); // PDU Count [2] - Data[index++] = (uint8_t)(TlmPduCount>>0); // PDU Count [3] - Data[index++] = (uint8_t)(TlmTimeSinceBoot>>24); // Time Since Boot [0] - Data[index++] = (uint8_t)(TlmTimeSinceBoot>>16); // Time Since Boot [1] - Data[index++] = (uint8_t)(TlmTimeSinceBoot>>8); // Time Since Boot [2] - Data[index++] = (uint8_t)(TlmTimeSinceBoot>>0); // Time Since Boot [3] - DBG("constructURLFrame: %d, %d",maxSize,index); + Data[index++] = (uint8_t)(TlmBatteryVoltage >> 8); // Battery Voltage[0] + Data[index++] = (uint8_t)(TlmBatteryVoltage >> 0); // Battery Voltage[1] + Data[index++] = (uint8_t)(TlmBeaconTemp >> 8); // Beacon Temp[0] + Data[index++] = (uint8_t)(TlmBeaconTemp >> 0); // Beacon Temp[1] + Data[index++] = (uint8_t)(TlmPduCount >> 24); // PDU Count [0] + Data[index++] = (uint8_t)(TlmPduCount >> 16); // PDU Count [1] + Data[index++] = (uint8_t)(TlmPduCount >> 8); // PDU Count [2] + Data[index++] = (uint8_t)(TlmPduCount >> 0); // PDU Count [3] + Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 24); // Time Since Boot [0] + Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 16); // Time Since Boot [1] + Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 8); // Time Since Boot [2] + Data[index++] = (uint8_t)(TlmTimeSinceBoot >> 0); // Time Since Boot [3] + DBG("constructURLFrame: %d, %d", maxSize, index); return index; } @@ -238,7 +262,6 @@ */ void updateTlmBatteryVoltage(uint16_t voltagemv) { TlmBatteryVoltage = voltagemv; - return; } /* @@ -248,7 +271,6 @@ */ void updateTlmBeaconTemp(uint16_t temp) { TlmBeaconTemp = temp; - return; } /* @@ -258,7 +280,6 @@ */ void updateTlmPduCount(uint32_t pduCount) { TlmPduCount = pduCount; - return; } /* @@ -268,7 +289,6 @@ */ void updateTlmTimeSinceBoot(uint32_t timeSinceBoot) { TlmTimeSinceBoot = timeSinceBoot; - return; } /* @@ -286,11 +306,7 @@ bool updateAdvPacket(uint8_t serviceData[], unsigned serviceDataLen) { // Fields from the Service DBG("Updating AdvFrame: %d", serviceDataLen); -// printf("\r\n"); -// for(int x = 0; x<serviceDataLen; x++) { -// printf("%2.2x:",serviceData[x]); -// } -// printf("\r\n"); + ble.clearAdvertisingPayload(); ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); ble.setAdvertisingInterval(100); @@ -309,40 +325,45 @@ * broken up into two functions. */ void swapOutFrames(FrameTypes frameType) { - uint8_t serviceData[SERVICE_DATA_MAX]; + uint8_t serviceData[SERVICE_DATA_MAX]; unsigned serviceDataLen = 0; //hard code in the eddystone UUID serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; // if certain frames are not enabled, then skip them. Worst case TLM is always enabled - switch(frameType) { + switch (frameType) { case tlm: // TLM frame - if(tlmIsSet) { - DBG("Swapping in TLM Frame: version=%x, Batt=%d, Temp = %d, PDUCnt = %d, TimeSinceBoot=%d",TlmVersion, TlmBatteryVoltage, TlmBeaconTemp, TlmPduCount, TlmTimeSinceBoot); - serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,20); - DBG("\t Swapping in TLM Frame: len=%d",serviceDataLen); - updateAdvPacket(serviceData,serviceDataLen); + if (tlmIsSet) { + DBG("Swapping in TLM Frame: version=%x, Batt=%d, Temp = %d, PDUCnt = %d, TimeSinceBoot=%d", + TlmVersion, + TlmBatteryVoltage, + TlmBeaconTemp, + TlmPduCount, + TlmTimeSinceBoot); + serviceDataLen += constructTLMFrame(serviceData + serviceDataLen, 20); + DBG("\t Swapping in TLM Frame: len=%d", serviceDataLen); + updateAdvPacket(serviceData, serviceDataLen); } break; case url: // URL Frame - if(urlIsSet) { - DBG("Swapping in URL Frame: Power: %d",defaultUrlPower); - serviceDataLen += constructURLFrame(serviceData+serviceDataLen,20); - DBG("\t Swapping in URL Frame: len=%d ",serviceDataLen); - updateAdvPacket(serviceData,serviceDataLen); + if (urlIsSet) { + DBG("Swapping in URL Frame: Power: %d", defaultUrlPower); + serviceDataLen += constructURLFrame(serviceData + serviceDataLen, 20); + DBG("\t Swapping in URL Frame: len=%d ", serviceDataLen); + updateAdvPacket(serviceData, serviceDataLen); //switchFlag = false; } break; case uid: // UID Frame - if(uidIsSet) { - DBG("Swapping in UID Frame: Power: %d",defaultUidPower); - serviceDataLen += constructUIDFrame(serviceData+serviceDataLen,20); - DBG("\t Swapping in UID Frame: len=%d",serviceDataLen); - updateAdvPacket(serviceData,serviceDataLen); + if (uidIsSet) { + DBG("Swapping in UID Frame: Power: %d", defaultUidPower); + serviceDataLen += constructUIDFrame(serviceData + serviceDataLen, 20); + DBG("\t Swapping in UID Frame: len=%d", serviceDataLen); + updateAdvPacket(serviceData, serviceDataLen); //switchFlag = false; } break; @@ -357,7 +378,7 @@ */ void urlCallback(void) { DBG("urlCallback"); - if( false == advLock) { + if (false == advLock) { advLock = true; DBG("advLock = url") frameIndex = url; @@ -365,11 +386,10 @@ ble.startAdvertising(); } else { // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free - INFO("URI(%d) cannot complete, %d is currently broadcasting",url, frameIndex); + INFO("URI(%d) cannot complete, %d is currently broadcasting", url, frameIndex); FrameTypes x = url; overflow.push(x); } - return; } /* @@ -377,7 +397,7 @@ */ void uidCallback(void) { DBG("uidCallback"); - if( false == advLock) { + if (false == advLock) { advLock = true; DBG("advLock = uid") frameIndex = uid; @@ -385,11 +405,10 @@ ble.startAdvertising(); } else { // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free - INFO("UID(%d) cannot complete, %d is currently broadcasting", uid,frameIndex); + INFO("UID(%d) cannot complete, %d is currently broadcasting", uid, frameIndex); FrameTypes x = uid; // have to do this to satisfy cont vs volatile keywords... sigh... overflow.push(x); } - return; } /* @@ -397,7 +416,7 @@ */ void tlmCallback(void) { DBG("tlmCallback"); - if( false == advLock) { + if (false == advLock) { // OK to broadcast advLock = true; DBG("advLock = tlm") @@ -406,15 +425,14 @@ ble.startAdvertising(); } else { // Someone else is broadcasting, toss it into the overflow buffer to retransmit when free - INFO("TLM(%d) cannot complete, %d is currently broadcasting",tlm ,frameIndex); + INFO("TLM(%d) cannot complete, %d is currently broadcasting", tlm, frameIndex); FrameTypes x = tlm; overflow.push(x); } - return; } void stopAdvCallback(void) { - if(overflow.empty()) { + if (overflow.empty()) { // if nothing left to transmit, stop ble.stopAdvertising(); advLock = false; // unlock lock @@ -422,7 +440,7 @@ // transmit other packets at current time index FrameTypes x = NONE; overflow.pop(x); - INFO("Re-Transmitting %d",x); + INFO("Re-Transmitting %d", x); swapOutFrames(x); } } @@ -435,13 +453,12 @@ // Update PDUCount TlmPduCount++; // True just before an frame is sent, false just after a frame is sent - if(radioActive) { + if (radioActive) { // Do Nothing } else { // Packet has been sent, disable advertising stopAdv.attach_us(this, &EddystoneService::stopAdvCallback, 1); } - return; } /* @@ -453,15 +470,14 @@ * @param txPowerLevel sets the broadcasting power level. * */ - EddystoneService(BLEDevice &bleIn, - uint16_t beaconPeriodus = 100, - uint8_t txPowerIn = 0) : + EddystoneService(BLEDevice &bleIn, + uint16_t beaconPeriodus = 100, + uint8_t txPowerIn = 0) : ble(bleIn), advPeriodus(beaconPeriodus), txPower(txPowerIn), advLock(false), frameIndex(NONE) { - } /* @@ -479,25 +495,22 @@ uidTicker.attach(this, &EddystoneService::uidCallback, uidAdvPeriod); DBG("attached uidCallback every %d seconds", uidAdvPeriod); } - if(tlmIsSet) { + if (tlmIsSet) { frameIndex = tlm; // Make double sure the PDUCount and TimeSinceBoot fields are set to zero at reset updateTlmPduCount(0); updateTlmTimeSinceBoot(0); - timeSinceBootTick.attach(this,&EddystoneService::tsbCallback,0.1); // incriment the TimeSinceBoot ticker every 0.1s + timeSinceBootTick.attach(this, &EddystoneService::tsbCallback, 0.1); // incriment the TimeSinceBoot ticker every 0.1s tlmTicker.attach(this, &EddystoneService::tlmCallback, TlmAdvPeriod); DBG("attached tlmCallback every %d seconds", TlmAdvPeriod); } - if(NONE == frameIndex) { + if (NONE == frameIndex) { error("No Frames were Initialized! Please initialize a frame before starting an eddystone beacon."); } //uidRFU = 0; - ble.setTxPower(txPower); -// ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); -// ble.setAdvertisingInterval(advPeriodus); -// ble.gap().startAdvertising(); - ble.gap().onRadioNotification(this,&EddystoneService::radioNotificationCallback); + ble.setTxPower(txPower); + ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback); } private: @@ -530,29 +543,29 @@ Ticker uidTicker; // TLM Frame Variables - uint8_t TlmVersion; - volatile uint16_t TlmBatteryVoltage; - volatile uint16_t TlmBeaconTemp; - volatile uint32_t TlmPduCount; - volatile uint32_t TlmTimeSinceBoot; - bool tlmIsSet; // flag that enables / disables TLM frames - uint32_t TlmAdvPeriod; // number of minutes between adv frames - Ticker tlmTicker; + uint8_t TlmVersion; + volatile uint16_t TlmBatteryVoltage; + volatile uint16_t TlmBeaconTemp; + volatile uint32_t TlmPduCount; + volatile uint32_t TlmTimeSinceBoot; + bool tlmIsSet; // flag that enables / disables TLM frames + uint32_t TlmAdvPeriod; // number of minutes between adv frames + Ticker tlmTicker; public: /* * Encode a human-readable URI into the binary format defined by URIBeacon spec (https://github.com/google/uribeacon/tree/master/specification). */ static void encodeURL(const char *uriDataIn, UriData_t uriDataOut, uint8_t &sizeofURIDataOut) { - DBG("Encode URL = %s",uriDataIn); - const char *prefixes[] = { + DBG("Encode URL = %s", uriDataIn); + const char *prefixes[] = { "http://www.", "https://www.", "http://", "https://", }; const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); - const char *suffixes[] = { + const char *suffixes[] = { ".com/", ".org/", ".edu/",