Skip to content

Commit

Permalink
Add NimBLEDevice::setOwnAddr and NimBLEUtils::generateAddr functions.
Browse files Browse the repository at this point in the history
Adds a utility to generate random BLE addresses and set the address used.
  • Loading branch information
h2zero committed Nov 3, 2024
1 parent 5665fd8 commit 68b9e26
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/NimBLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 2 additions & 0 deletions src/NimBLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/NimBLEUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#if defined(CONFIG_BT_ENABLED)

#include "NimBLEUtils.h"
#include "NimBLEAddress.h"
#include "NimBLELog.h"

#include <stdlib.h>
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/NimBLEUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <string>

class NimBLEAddress;

typedef struct {
void *pATT;
TaskHandle_t task;
Expand All @@ -43,6 +45,7 @@ class NimBLEUtils {
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);
};


Expand Down

0 comments on commit 68b9e26

Please sign in to comment.