Skip to content

Commit 96c60fb

Browse files
authored
Add option to use resolvable and non-resolvable private address. (#174)
Adds the possibility to configure a resolvable or non-resolvable address (BLE privacy).
1 parent edaa367 commit 96c60fb

6 files changed

+36
-6
lines changed

src/NimBLEAdvertising.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,12 @@ void NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdv
476476
}
477477

478478
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
479-
rc = ble_gap_adv_start(0, NULL, duration,
479+
rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, NULL, duration,
480480
&m_advParams,
481481
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent,
482482
(pServer != nullptr) ? (void*)pServer : (void*)this);
483483
#else
484-
rc = ble_gap_adv_start(0, NULL, duration,
484+
rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, NULL, duration,
485485
&m_advParams, NimBLEAdvertising::handleGapEvent, this);
486486
#endif
487487
if (rc != 0) {

src/NimBLEClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
208208
* Loop on BLE_HS_EBUSY if the scan hasn't stopped yet.
209209
*/
210210
do {
211-
rc = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &peerAddr_t,
211+
rc = ble_gap_connect(NimBLEDevice::m_own_addr_type, &peerAddr_t,
212212
m_connectTimeout, &m_pConnParams,
213213
NimBLEClient::handleGapEvent, this);
214214
switch (rc) {

src/NimBLEDevice.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "nimble/nimble_port.h"
2626
#include "nimble/nimble_port_freertos.h"
2727
#include "host/ble_hs.h"
28+
#include "host/ble_hs_pvcy.h"
2829
#include "host/util/util.h"
2930
#include "services/gap/ble_svc_gap.h"
3031
#include "services/gatt/ble_svc_gatt.h"
@@ -60,6 +61,7 @@ std::list <NimBLEClient*> NimBLEDevice::m_cList;
6061
#endif
6162
std::list <NimBLEAddress> NimBLEDevice::m_ignoreList;
6263
NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr;
64+
uint8_t NimBLEDevice::m_own_addr_type = BLE_OWN_ADDR_PUBLIC;
6365

6466

6567
/**
@@ -698,6 +700,34 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) {
698700
} // setSecurityCallbacks
699701

700702

703+
/**
704+
* @brief Set the own address type.
705+
* @param own_addr_type Own Bluetooth Device address type.\n
706+
* The available bits are defined as:
707+
* * 0x00: BLE_OWN_ADDR_PUBLIC
708+
* * 0x01: BLE_OWN_ADDR_RANDOM
709+
* * 0x02: BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT
710+
* * 0x03: BLE_OWN_ADDR_RPA_RANDOM_DEFAULT
711+
*/
712+
void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) {
713+
m_own_addr_type = own_addr_type;
714+
switch (own_addr_type) {
715+
case BLE_OWN_ADDR_PUBLIC:
716+
ble_hs_pvcy_rpa_config(NIMBLE_HOST_DISABLE_PRIVACY);
717+
break;
718+
case BLE_OWN_ADDR_RANDOM:
719+
setSecurityInitKey(BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID);
720+
ble_hs_pvcy_rpa_config(useNRPA ? NIMBLE_HOST_ENABLE_NRPA : NIMBLE_HOST_ENABLE_RPA);
721+
break;
722+
case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT:
723+
case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
724+
setSecurityInitKey(BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID);
725+
ble_hs_pvcy_rpa_config(NIMBLE_HOST_ENABLE_RPA);
726+
break;
727+
}
728+
} // setOwnAddrType
729+
730+
701731
/**
702732
* @brief Start the connection securing and authorization for this connection.
703733
* @param conn_id The connection id of the peer device.

src/NimBLEDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class NimBLEDevice {
116116
static void setSecurityPasskey(uint32_t pin);
117117
static uint32_t getSecurityPasskey();
118118
static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
119+
static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
119120
static int startSecurity(uint16_t conn_id);
120121
static int setMTU(uint16_t mtu);
121122
static uint16_t getMTU();
@@ -182,6 +183,7 @@ class NimBLEDevice {
182183
static uint32_t m_passkey;
183184
static ble_gap_event_listener m_listener;
184185
static gap_event_handler m_customGapHandler;
186+
static uint8_t m_own_addr_type;
185187
};
186188

187189

src/NimBLEScan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ static const char* LOG_TAG = "NimBLEScan";
3030
* @brief Scan constuctor.
3131
*/
3232
NimBLEScan::NimBLEScan() {
33-
m_own_addr_type = 0;
3433
m_scan_params.filter_policy = BLE_HCI_SCAN_FILT_NO_WL;
3534
m_scan_params.passive = 1; // If set, don’t send scan requests to advertisers (i.e., don’t request additional advertising data).
3635
m_scan_params.itvl = 0; // This is defined as the time interval from when the Controller started its last LE scan until it begins the subsequent LE scan. (units=0.625 msec)
@@ -271,7 +270,7 @@ bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResul
271270
m_ignoreResults = true;
272271
}
273272

274-
int rc = ble_gap_disc(m_own_addr_type, duration, &m_scan_params,
273+
int rc = ble_gap_disc(NimBLEDevice::m_own_addr_type, duration, &m_scan_params,
275274
NimBLEScan::handleGapEvent, this);
276275

277276
switch(rc) {

src/NimBLEScan.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class NimBLEScan {
8888
NimBLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr;
8989
void (*m_scanCompleteCB)(NimBLEScanResults scanResults);
9090
ble_gap_disc_params m_scan_params;
91-
uint8_t m_own_addr_type;
9291
bool m_ignoreResults;
9392
bool m_wantDuplicates;
9493
NimBLEScanResults m_scanResults;

0 commit comments

Comments
 (0)