diff --git a/inc/bluetooth/MicroBitBLEManager.h b/inc/bluetooth/MicroBitBLEManager.h index cb461d7..6110a9c 100644 --- a/inc/bluetooth/MicroBitBLEManager.h +++ b/inc/bluetooth/MicroBitBLEManager.h @@ -71,7 +71,8 @@ DEALINGS IN THE SOFTWARE. #define MICROBIT_BLE_MAXIMUM_BONDS 4 #define MICROBIT_BLE_ENABLE_BONDING true -#define MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL 400 +#define MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL 400 +#define MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER 0xF0 extern const int8_t MICROBIT_BLE_POWER_LEVEL[]; @@ -214,44 +215,76 @@ class MicroBitBLEManager : MicroBitComponent */ void stopAdvertising(); #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL) + /** - * Starts Bluetooth advertising of Eddystone URL frames - * @param url: the url to transmit. Must be no longer than the supported eddystone url length - * @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. - * The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. - * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-url#tx-power-level - * @param connectable: true to keep bluetooth connectable for other services, false otherwise - * @param interval: the advertising interval of the beacon - */ - void advertiseEddystoneUrl(char *url, int8_t calibratedPower, bool connectable, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); + * Set the content of Eddystone URL frames + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int advertiseEddystoneUrl(const char *url, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); /** - * Starts Bluetooth advertising of Eddystone URL frames, but accepts a ManagedString as a url. For more info see - * advertiseEddystoneUrl(char* url, int8_t calibratedPower, bool connectable, uint16_t interval) - */ - void advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower, bool connectable, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); + * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); #endif #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID) /** - * Starts Bluetooth advertising of Eddystone UID frames - * (from the Eddystone spec) the namespace portion of the ID may be used to group a particular set of beacons, while the instance ID - * identifies individual devices in the group. - * @param uid_namespace: the uid namespace. Must 10 bytes long. - * @param uid_instance: the uid instance value. Must 6 bytes long. - * @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. - * The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. - * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power - * @param connectable: true to keep bluetooth connectable for other services, false otherwise - * @param interval: the advertising interval of the beacon - */ - void advertiseEddystoneUid(char *uid_namespace, char *uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); + * Set the content of Eddystone UID frames + * + * @param uid_namespace: the uid namespace. Must 10 bytes long. + * + * @param uid_instance: the uid instance value. Must 6 bytes long. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int advertiseEddystoneUid(const char* uid_namespace, const char* uid_instance, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); /** - * Starts Bluetooth advertising of Eddystone URL frames, but accepts ManagedStrings as the uid parts. For more info see - * advertiseEddystoneUid(char* uid_namespace, char* uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) - */ - void advertiseEddystoneUid(ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); + * Set the content of Eddystone UID frames, but accepts a ManagedString as a namespace and instance. + * + * @param uid_namespace The namespace of the UID. + * + * @param uid_instance The value within the namespace. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int advertiseEddystoneUid(ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); #endif private: diff --git a/inc/bluetooth/MicroBitEddystone.h b/inc/bluetooth/MicroBitEddystone.h index 1558f00..41d9a70 100644 --- a/inc/bluetooth/MicroBitEddystone.h +++ b/inc/bluetooth/MicroBitEddystone.h @@ -37,8 +37,6 @@ DEALINGS IN THE SOFTWARE. #include "MicroBitBLEManager.h" -#define MICROBIT_BLE_EDDYSTONE_URL_ADV_INTERVAL 400 - /** * Class definition for the MicroBitEddystone. * @@ -46,62 +44,75 @@ DEALINGS IN THE SOFTWARE. class MicroBitEddystone { public: - static MicroBitEddystone *getInstance(); + + static MicroBitEddystone* getInstance(); #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL) /** - * Set the content of Eddystone URL frames - * @param url: the url to transmit. Must be no longer than the supported eddystone url length - * @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. - * The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. - * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-url#tx-power-level - * @param connectable: true to keep bluetooth connectable for other services, false otherwise - * @param interval: the advertising interval of the beacon - */ - void setEddystoneUrl(BLEDevice *ble, char *url, int8_t calibratedPower); + * Set the content of Eddystone URL frames + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int setURL(BLEDevice* ble, const char *url, int8_t calibratedPower = 0xF0); /** - * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. For more info see - * setEddystoneUrl(char* url, int8_t calibratedPower, bool connectable, uint16_t interval) - */ - void setEddystoneUrl(BLEDevice *ble, ManagedString url, int8_t calibratedPower); + * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int setURL(BLEDevice* ble, ManagedString url, int8_t calibratedPower = 0xF0); #endif #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID) /** - * Set the content of Eddystone UID frames - * @param uid_namespace: the uid namespace. Must 10 bytes long. - * @param uid_instance: the uid instance value. Must 6 bytes long. - * @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. - * The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. - * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power - * @param connectable: true to keep bluetooth connectable for other services, false otherwise - * @param interval: the advertising interval of the beacon - */ - void setEddystoneUid(BLEDevice *ble, char *uid_namespace, char *uid_instance, int8_t calibratedPower); + * Set the content of Eddystone UID frames + * + * @param uid_namespace: the uid namespace. Must 10 bytes long. + * + * @param uid_instance: the uid instance value. Must 6 bytes long. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int setUID(BLEDevice* ble, const char* uid_namespace, const char* uid_instance, int8_t calibratedPower = 0xF0); /** - * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. For more info see - * setEddystoneUid(char* uid_namespace, char* uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) - */ - void setEddystoneUid(BLEDevice *ble, ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower); + * Set the content of Eddystone UID frames, but accepts a ManagedString as a namespace and instance. + * + * @param uid_namespace The namespace of the UID. + * + * @param uid_instance The value within the namespace. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int setUID(BLEDevice* ble, ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower = 0xF0); #endif private: + /** - * Constructor. - * - * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack. - * - * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS). - * - * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself). - * Hence, the init() member function should be used to initialise the BLE stack. - */ + * Private constructor. + */ MicroBitEddystone(); + static MicroBitEddystone *_instance; }; diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index dd90565..ad428ba 100755 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -49,6 +49,7 @@ set(YOTTA_AUTO_MICROBIT-DAL_CPP_FILES "bluetooth/MicroBitBLEManager.cpp" "bluetooth/MicroBitButtonService.cpp" "bluetooth/MicroBitDFUService.cpp" + "bluetooth/MicroBitEddystone.cpp" "bluetooth/MicroBitEventService.cpp" "bluetooth/MicroBitIOPinService.cpp" "bluetooth/MicroBitLEDService.cpp" diff --git a/source/bluetooth/MicroBitBLEManager.cpp b/source/bluetooth/MicroBitBLEManager.cpp index 331623f..e9d5889 100644 --- a/source/bluetooth/MicroBitBLEManager.cpp +++ b/source/bluetooth/MicroBitBLEManager.cpp @@ -506,15 +506,20 @@ void MicroBitBLEManager::stopAdvertising() #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL) /** -* Starts Bluetooth advertising of Eddystone URL frames -* @param url: the url to transmit. Must be no longer than the supported eddystone url length -* @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. -* The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. -* More information can be found at https://github.com/google/eddystone/tree/master/eddystone-url#tx-power-level -* @param connectable: true to keep bluetooth connectable for other services, false otherwise -* @param interval: the advertising interval of the beacon -*/ -void MicroBitBLEManager::advertiseEddystoneUrl(char *url, int8_t calibratedPower, bool connectable, uint16_t interval) + * Set the content of Eddystone URL frames + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitBLEManager::advertiseEddystoneUrl(const char* url, int8_t calibratedPower, bool connectable, uint16_t interval) { ble->gap().stopAdvertising(); ble->clearAdvertisingPayload(); @@ -524,38 +529,54 @@ void MicroBitBLEManager::advertiseEddystoneUrl(char *url, int8_t calibratedPower ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - MicroBitEddystone::getInstance()->setEddystoneUrl(ble, url, calibratedPower); + int ret = MicroBitEddystone::getInstance()->setURL(ble, url, calibratedPower); #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0) ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT); #endif ble->gap().startAdvertising(); + + return ret; } /** -* Starts Bluetooth advertising of Eddystone URL frames, but accepts a ManagedString as a url. For more info see -* advertiseEddystoneUrl(char* url, int8_t calibratedPower, bool connectable, uint16_t interval) -*/ -void advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower, bool connectable, uint16_t interval) + * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitBLEManager::advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower, bool connectable, uint16_t interval) { - advertiseEddystoneUrl((char *)url.toCharArray(), calibratedPower, connectable, interval); + return advertiseEddystoneUrl((char *)url.toCharArray(), calibratedPower, connectable, interval); } #endif #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID) /** -* Starts Bluetooth advertising of Eddystone UID frames -* (from the Eddystone spec) the namespace portion of the ID may be used to group a particular set of beacons, while the instance ID -* identifies individual devices in the group. -* @param uid_namespace: the uid namespace. Must 10 bytes long. -* @param uid_instance: the uid instance value. Must 6 bytes long. -* @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. -* The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. -* More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power -* @param connectable: true to keep bluetooth connectable for other services, false otherwise -* @param interval: the advertising interval of the beacon -*/ -void MicroBitBLEManager::advertiseEddystoneUid(char *uid_namespace, char *uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) + * Set the content of Eddystone UID frames + * + * @param uid_namespace: the uid namespace. Must 10 bytes long. + * + * @param uid_instance: the uid instance value. Must 6 bytes long. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitBLEManager::advertiseEddystoneUid(const char* uid_namespace, const char* uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) { ble->gap().stopAdvertising(); ble->clearAdvertisingPayload(); @@ -565,22 +586,35 @@ void MicroBitBLEManager::advertiseEddystoneUid(char *uid_namespace, char *uid_in ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - MicroBitEddystone::getInstance()->setEddystoneUid(ble, uid_namespace, uid_instance, calibratedPower); + int ret = MicroBitEddystone::getInstance()->setUID(ble, uid_namespace, uid_instance, calibratedPower); #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0) ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT); #endif ble->gap().startAdvertising(); + + return ret; } /** -* Starts Bluetooth advertising of Eddystone UID frames, but accepts a ManagedStrings as a uid args. For more info see -* advertiseEddystoneUid(char* uid_namespace, char* uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) -* @return 0 for success or MICROBIT_INVALID_PARAMETER if parameters are not valid -*/ -void advertiseEddystoneUid(ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) + * Set the content of Eddystone UID frames, but accepts a ManagedString as a namespace and instance. + * + * @param uid_namespace The namespace of the UID. + * + * @param uid_instance The value within the namespace. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitBLEManager::advertiseEddystoneUid(ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) { - advertiseEddystoneUid((char *)uid_namespace.toCharArray(), (char *)uid_instance.toCharArray(), calibratedPower, connectable, interval); + return advertiseEddystoneUid((char *)uid_namespace.toCharArray(), (char *)uid_instance.toCharArray(), calibratedPower, connectable, interval); } #endif diff --git a/source/bluetooth/MicroBitEddystone.cpp b/source/bluetooth/MicroBitEddystone.cpp index 7c2e902..d067f61 100644 --- a/source/bluetooth/MicroBitEddystone.cpp +++ b/source/bluetooth/MicroBitEddystone.cpp @@ -26,6 +26,8 @@ DEALINGS IN THE SOFTWARE. #include "MicroBitConfig.h" #include "MicroBitEddystone.h" +MicroBitEddystone *MicroBitEddystone::_instance = NULL; + /* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ. * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this @@ -44,8 +46,6 @@ DEALINGS IN THE SOFTWARE. #pragma GCC diagnostic pop #endif -MicroBitEddystone *MicroBitEddystone::_instance; - const uint8_t EDDYSTONE_UUID[] = {0xAA, 0xFE}; #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL) @@ -77,33 +77,34 @@ MicroBitEddystone::MicroBitEddystone() { } -MicroBitEddystone *MicroBitEddystone::getInstance() +MicroBitEddystone* MicroBitEddystone::getInstance() { if (_instance == 0) - { _instance = new MicroBitEddystone; - } + return _instance; } #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL) + /** -* Set the content of Eddystone URL frames -* @param url: the url to transmit. Must be no longer than the supported eddystone url length -* @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. -* The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. -* More information can be found at https://github.com/google/eddystone/tree/master/eddystone-url#tx-power-level -*/ -void MicroBitEddystone::setEddystoneUrl(BLEDevice *ble, char *url, int8_t calibratedPower) + * Set the content of Eddystone URL frames + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitEddystone::setURL(BLEDevice* ble, const char* url, int8_t calibratedPower) { int urlDataLength = 0; char urlData[EDDYSTONE_URL_MAX_LENGTH]; memset(urlData, 0, EDDYSTONE_URL_MAX_LENGTH); - if ((url == NULL) || (strlen(url) == 0)) - { - return; - } + if (url == NULL || strlen(url) == 0) + return MICROBIT_INVALID_PARAMETER; // Prefix for (size_t i = 0; i < EDDYSTONE_URL_PREFIXES_LENGTH; i++) @@ -150,29 +151,45 @@ void MicroBitEddystone::setEddystoneUrl(BLEDevice *ble, char *url, int8_t calibr ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, EDDYSTONE_UUID, sizeof(EDDYSTONE_UUID)); ble->accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, rawFrame, index + urlDataLength); + + return MICROBIT_OK; } /** -* Set the content of Eddystone URL frames, but accepts a ManagedString as a url. For more info see -* setEddystoneUrl(char* url, int8_t calibratedPower, bool connectable, uint16_t interval) -*/ -void setEddystoneUrl(BLEDevice *ble, ManagedString url, int8_t calibratedPower) + * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitEddystone::setURL(BLEDevice* ble, ManagedString url, int8_t calibratedPower) { - setEddystoneUrl(ble, (char *)url.toCharArray(), calibratedPower); + return setURL(ble, (char *)url.toCharArray(), calibratedPower); } #endif #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID) + /** -* Set the content of Eddystone UID frames -* @param uid_namespace: the uid namespace. Must 10 bytes long. -* @param uid_instance: the uid instance value. Must 6 bytes long. -* @param calibratedPower: the calibrated to transmit at. This is the received power at 0 meters in dBm. -* The value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. -* More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power -*/ -void MicroBitEddystone::setEddystoneUid(BLEDevice *ble, char *uid_namespace, char *uid_instance, int8_t calibratedPower) + * Set the content of Eddystone UID frames + * + * @param uid_namespace: the uid namespace. Must 10 bytes long. + * + * @param uid_instance: the uid instance value. Must 6 bytes long. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitEddystone::setUID(BLEDevice* ble, const char* uid_namespace, const char* uid_instance, int8_t calibratedPower) { + if (uid_namespace == NULL || strlen(uid_namespace) == 0 || uid_instance == NULL || strlen(uid_instance) == 0) + return MICROBIT_INVALID_PARAMETER; + char uidData[EDDYSTONE_UID_NAMESPACE_MAX_LENGTH + EDDYSTONE_UID_INSTANCE_MAX_LENGTH]; // UID namespace @@ -191,15 +208,25 @@ void MicroBitEddystone::setEddystoneUid(BLEDevice *ble, char *uid_namespace, cha ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, EDDYSTONE_UUID, sizeof(EDDYSTONE_UUID)); ble->accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, rawFrame, index + EDDYSTONE_UID_NAMESPACE_MAX_LENGTH + EDDYSTONE_UID_INSTANCE_MAX_LENGTH); + + return MICROBIT_OK; } /** -* Set the content of Eddystone UID frames, but accepts ManagedStrings as a uid args. For more info see -* setEddystoneUid(char* uid_namespace, char* uid_instance, int8_t calibratedPower, bool connectable, uint16_t interval) -*/ -void MicroBitEddystone::setEddystoneUid(BLEDevice *ble, ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower) + * Set the content of Eddystone UID frames, but accepts a ManagedString as a namespace and instance. + * + * @param uid_namespace The namespace of the UID. + * + * @param uid_instance The value within the namespace. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ +int MicroBitEddystone::setUID(BLEDevice* ble, ManagedString uid_namespace, ManagedString uid_instance, int8_t calibratedPower) { - setEddystoneUid(ble, (char *)uid_namespace.toCharArray(), (char *)uid_instance.toCharArray(), calibratedPower); + return setUID(ble, (char *)uid_namespace.toCharArray(), (char *)uid_instance.toCharArray(), calibratedPower); } #endif