Add NimBLEDevice::setOwnAddr and NimBLEUtils::generateAddr functions.

Adds a utility to generate random BLE addresses and set the address used.
This commit is contained in:
h2zero 2024-11-02 17:56:23 -06:00 committed by h2zero
parent a2fe5b4780
commit 68b82f5b85
4 changed files with 48 additions and 0 deletions

View file

@ -1004,6 +1004,32 @@ bool NimBLEDevice::setOwnAddrType(uint8_t type) {
return rc == 0; return rc == 0;
} // setOwnAddrType } // 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 */ /* SECURITY */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View file

@ -117,6 +117,8 @@ class NimBLEDevice {
static bool setPower(int8_t dbm); static bool setPower(int8_t dbm);
static int getPower(); static int getPower();
static bool setOwnAddrType(uint8_t type); 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 setScanDuplicateCacheSize(uint16_t cacheSize);
static void setScanFilterMode(uint8_t type); static void setScanFilterMode(uint8_t type);
static bool setCustomGapHandler(gap_event_handler handler); static bool setCustomGapHandler(gap_event_handler handler);

View file

@ -10,6 +10,7 @@
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLEAddress.h"
#include "NimBLELog.h" #include "NimBLELog.h"
#include <stdlib.h> #include <stdlib.h>
@ -476,4 +477,20 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
#endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
} // gapEventToString } // 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 #endif //CONFIG_BT_ENABLED

View file

@ -25,6 +25,8 @@
#include <string> #include <string>
class NimBLEAddress;
typedef struct { typedef struct {
void *pATT; void *pATT;
TaskHandle_t task; TaskHandle_t task;
@ -43,6 +45,7 @@ public:
static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length); static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length);
static const char* advTypeToString(uint8_t advType); static const char* advTypeToString(uint8_t advType);
static const char* returnCodeToString(int rc); static const char* returnCodeToString(int rc);
static NimBLEAddress generateAddr(bool nrpa);
}; };