From 68b82f5b855040b320c7865153e6289904ef132f Mon Sep 17 00:00:00 2001 From: h2zero Date: Sat, 2 Nov 2024 17:56:23 -0600 Subject: [PATCH] Add NimBLEDevice::setOwnAddr and NimBLEUtils::generateAddr functions. Adds a utility to generate random BLE addresses and set the address used. --- src/NimBLEDevice.cpp | 26 ++++++++++++++++++++++++++ src/NimBLEDevice.h | 2 ++ src/NimBLEUtils.cpp | 17 +++++++++++++++++ src/NimBLEUtils.h | 3 +++ 4 files changed, 48 insertions(+) diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 1fe6fd2..e60c9f8 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -1004,6 +1004,32 @@ bool NimBLEDevice::setOwnAddrType(uint8_t type) { return rc == 0; } // setOwnAddrType +/** + * @brief Set the device address to use. + * @param [in] addr The address to set. + * @return True if the address was set successfully. + * @details To use the address generated the address type must be set to random with `setOwnAddrType`. + */ +bool NimBLEDevice::setOwnAddr(const NimBLEAddress& addr) { + return setOwnAddr(addr.getBase()->val); +} // setOwnAddr + +/** + * @brief Set the device address to use. + * @param [in] addr The address to set. + * @return True if the address was set successfully. + * @details To use the address generated the address type must be set to random with `setOwnAddrType`. + */ +bool NimBLEDevice::setOwnAddr(const uint8_t* addr) { + int rc = ble_hs_id_set_rnd(addr); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Failed to set address, rc=%d", rc); + return false; + } + + return true; +} // setOwnAddr + /* -------------------------------------------------------------------------- */ /* SECURITY */ /* -------------------------------------------------------------------------- */ diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index 27d13f1..507b5ad 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -117,6 +117,8 @@ class NimBLEDevice { static bool setPower(int8_t dbm); static int getPower(); static bool setOwnAddrType(uint8_t type); + static bool setOwnAddr(const NimBLEAddress& addr); + static bool setOwnAddr(const uint8_t* addr); static void setScanDuplicateCacheSize(uint16_t cacheSize); static void setScanFilterMode(uint8_t type); static bool setCustomGapHandler(gap_event_handler handler); diff --git a/src/NimBLEUtils.cpp b/src/NimBLEUtils.cpp index 06e649c..cc50e13 100644 --- a/src/NimBLEUtils.cpp +++ b/src/NimBLEUtils.cpp @@ -10,6 +10,7 @@ #if defined(CONFIG_BT_ENABLED) #include "NimBLEUtils.h" +#include "NimBLEAddress.h" #include "NimBLELog.h" #include @@ -476,4 +477,20 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) { #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) } // gapEventToString +/** + * @brief Generate a random BLE address. + * @param [in] nrpa True to generate a non-resolvable private address, + * false to generate a random static address + * @return The generated address or a NULL address if there was an error. + */ +NimBLEAddress NimBLEUtils::generateAddr(bool nrpa) { + ble_addr_t addr{}; + int rc = ble_hs_id_gen_rnd(nrpa, &addr); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Generate address failed, rc=%d", rc); + } + + return NimBLEAddress{addr}; +} // generateAddr + #endif //CONFIG_BT_ENABLED diff --git a/src/NimBLEUtils.h b/src/NimBLEUtils.h index 57d22a0..a581011 100644 --- a/src/NimBLEUtils.h +++ b/src/NimBLEUtils.h @@ -25,6 +25,8 @@ #include +class NimBLEAddress; + typedef struct { void *pATT; TaskHandle_t task; @@ -43,6 +45,7 @@ public: static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length); static const char* advTypeToString(uint8_t advType); static const char* returnCodeToString(int rc); + static NimBLEAddress generateAddr(bool nrpa); };