Skip to content

Commit bb35cba

Browse files
committed
BLE: Add function to signal privacy initialization.
1 parent 5216a9a commit bb35cba

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

connectivity/FEATURE_BLE/include/ble/Gap.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,14 @@ class Gap {
538538
)
539539
{
540540
}
541+
542+
/**
543+
* Function invoked when the privacy subsystem has been enabled and is
544+
* ready to be used.
545+
*/
546+
virtual void onPrivacyEnabled()
547+
{
548+
}
541549
protected:
542550
/**
543551
* Prevent polymorphic deletion and avoid unnecessary virtual destructor
@@ -1244,6 +1252,14 @@ class Gap {
12441252
* resolved and advertisement packets are forwarded to the application
12451253
* even if the advertiser private address is unknown.
12461254
*
1255+
* @par Initialization of the privacy subsystem
1256+
*
1257+
* When privacy is enabled, the system generates new resolvable and non
1258+
* resolvable private addresses. Scan, Advertising and Connecting to a peer
1259+
* won't be available until the generation process completes. When addresses
1260+
* have been generated, the application is notified that privacy
1261+
* initialisation as completed with a call to EventHandler::onPrivacyEnabled .
1262+
*
12471263
* @param[in] enable Should be set to true to enable the privacy mode and
12481264
* false to disable it.
12491265
*

connectivity/FEATURE_BLE/source/generic/GapImpl.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,20 @@ ble_error_t Gap::enablePrivacy(bool enable)
925925

926926
if (_privacy_enabled) {
927927
_address_registry.start_private_address_generation();
928+
if (_address_registry.get_non_resolvable_private_address() != address_t {} &&
929+
_address_registry.get_resolvable_private_address() != address_t{}
930+
) {
931+
_event_queue.post([this] {
932+
if (_event_handler) {
933+
_event_handler->onPrivacyEnabled();
934+
}
935+
});
936+
} else {
937+
_privacy_initialization_pending = true;
938+
}
928939
} else {
929940
_address_registry.stop_private_address_generation();
941+
_privacy_initialization_pending = false;
930942
}
931943

932944
#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
@@ -1016,6 +1028,10 @@ ble_error_t Gap::reset()
10161028

10171029
_event_handler = nullptr;
10181030

1031+
#if BLE_FEATURE_PRIVACY
1032+
_privacy_initialization_pending = false;
1033+
#endif
1034+
10191035
#if BLE_ROLE_BROADCASTER
10201036
_advertising_timeout.detach();
10211037
#endif
@@ -3174,6 +3190,15 @@ void Gap::on_private_address_generated(bool connectable)
31743190
return;
31753191
}
31763192

3193+
if (_privacy_initialization_pending &&
3194+
_address_registry.get_resolvable_private_address() != address_t{} &&
3195+
_address_registry.get_non_resolvable_private_address() != address_t{}
3196+
) {
3197+
_privacy_initialization_pending = false;
3198+
if (_event_handler) {
3199+
_event_handler->onPrivacyEnabled();
3200+
}
3201+
}
31773202

31783203
// refresh for address for all connectable advertising sets
31793204
for (size_t i = 0; i < BLE_GAP_MAX_ADVERTISING_SETS; ++i) {
@@ -3372,6 +3397,10 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_
33723397
break;
33733398
}
33743399

3400+
if (*desired_address == address_t{}) {
3401+
return nullptr;
3402+
}
3403+
33753404
if (!address_in_use) {
33763405
return desired_address;
33773406
}

connectivity/FEATURE_BLE/source/generic/GapImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ class Gap :
832832

833833
bool _privacy_enabled;
834834
#if BLE_FEATURE_PRIVACY
835+
bool _privacy_initialization_pending = false;
835836
#if BLE_ROLE_PERIPHERAL
836837
peripheral_privacy_configuration_t _peripheral_privacy_configuration;
837838
#endif // BLE_ROLE_PERIPHERAL

0 commit comments

Comments
 (0)