Skip to content

Commit 68b9e26

Browse files
committed
Add NimBLEDevice::setOwnAddr and NimBLEUtils::generateAddr functions.
Adds a utility to generate random BLE addresses and set the address used.
1 parent 5665fd8 commit 68b9e26

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/NimBLEDevice.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,32 @@ bool NimBLEDevice::setOwnAddrType(uint8_t type) {
10041004
return rc == 0;
10051005
} // setOwnAddrType
10061006

1007+
/**
1008+
* @brief Set the device address to use.
1009+
* @param [in] addr The address to set.
1010+
* @return True if the address was set successfully.
1011+
* @details To use the address generated the address type must be set to random with `setOwnAddrType`.
1012+
*/
1013+
bool NimBLEDevice::setOwnAddr(const NimBLEAddress& addr) {
1014+
return setOwnAddr(addr.getBase()->val);
1015+
} // setOwnAddr
1016+
1017+
/**
1018+
* @brief Set the device address to use.
1019+
* @param [in] addr The address to set.
1020+
* @return True if the address was set successfully.
1021+
* @details To use the address generated the address type must be set to random with `setOwnAddrType`.
1022+
*/
1023+
bool NimBLEDevice::setOwnAddr(const uint8_t* addr) {
1024+
int rc = ble_hs_id_set_rnd(addr);
1025+
if (rc != 0) {
1026+
NIMBLE_LOGE(LOG_TAG, "Failed to set address, rc=%d", rc);
1027+
return false;
1028+
}
1029+
1030+
return true;
1031+
} // setOwnAddr
1032+
10071033
/* -------------------------------------------------------------------------- */
10081034
/* SECURITY */
10091035
/* -------------------------------------------------------------------------- */

src/NimBLEDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class NimBLEDevice {
117117
static bool setPower(int8_t dbm);
118118
static int getPower();
119119
static bool setOwnAddrType(uint8_t type);
120+
static bool setOwnAddr(const NimBLEAddress& addr);
121+
static bool setOwnAddr(const uint8_t* addr);
120122
static void setScanDuplicateCacheSize(uint16_t cacheSize);
121123
static void setScanFilterMode(uint8_t type);
122124
static bool setCustomGapHandler(gap_event_handler handler);

src/NimBLEUtils.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#if defined(CONFIG_BT_ENABLED)
1111

1212
#include "NimBLEUtils.h"
13+
#include "NimBLEAddress.h"
1314
#include "NimBLELog.h"
1415

1516
#include <stdlib.h>
@@ -476,4 +477,20 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
476477
#endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
477478
} // gapEventToString
478479

480+
/**
481+
* @brief Generate a random BLE address.
482+
* @param [in] nrpa True to generate a non-resolvable private address,
483+
* false to generate a random static address
484+
* @return The generated address or a NULL address if there was an error.
485+
*/
486+
NimBLEAddress NimBLEUtils::generateAddr(bool nrpa) {
487+
ble_addr_t addr{};
488+
int rc = ble_hs_id_gen_rnd(nrpa, &addr);
489+
if (rc != 0) {
490+
NIMBLE_LOGE(LOG_TAG, "Generate address failed, rc=%d", rc);
491+
}
492+
493+
return NimBLEAddress{addr};
494+
} // generateAddr
495+
479496
#endif //CONFIG_BT_ENABLED

src/NimBLEUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include <string>
2727

28+
class NimBLEAddress;
29+
2830
typedef struct {
2931
void *pATT;
3032
TaskHandle_t task;
@@ -43,6 +45,7 @@ class NimBLEUtils {
4345
static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length);
4446
static const char* advTypeToString(uint8_t advType);
4547
static const char* returnCodeToString(int rc);
48+
static NimBLEAddress generateAddr(bool nrpa);
4649
};
4750

4851

0 commit comments

Comments
 (0)