mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-21 12:40:54 +01:00
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:
parent
a2fe5b4780
commit
68b82f5b85
4 changed files with 48 additions and 0 deletions
|
@ -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 */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include "NimBLEUtils.h"
|
||||
#include "NimBLEAddress.h"
|
||||
#include "NimBLELog.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)
|
||||
} // 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
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue