Skip to content

Commit 18c218b

Browse files
paul-szczepanek-armpan-
authored andcommitted
allow host resolved address to be used for connection
1 parent 4744c87 commit 18c218b

File tree

2 files changed

+136
-27
lines changed

2 files changed

+136
-27
lines changed

connectivity/FEATURE_BLE/source/generic/GapImpl.cpp

Lines changed: 125 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,11 @@ ble_error_t Gap::stopScan()
468468
{
469469
ble_error_t err;
470470

471-
if ((!_scan_enabled && !_scan_pending) || _scan_pending) {
471+
if ((!_scan_enabled && !_scan_pending) || _scan_pending
472+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
473+
|| _ready_to_connect_to_host_resolved_address
474+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
475+
) {
472476
return BLE_STACK_BUSY;
473477
}
474478

@@ -524,6 +528,17 @@ ble_error_t Gap::connect(
524528

525529
ble_error_t ret = BLE_ERROR_INTERNAL_STACK_FAILURE;
526530

531+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
532+
if (_ready_to_connect_to_host_resolved_address) {
533+
_ready_to_connect_to_host_resolved_address = false;
534+
_connect_to_address_type = peer_address_type_t::ANONYMOUS;
535+
} else if (peerAddressType == peer_address_type_t::RANDOM_STATIC_IDENTITY ||
536+
peerAddressType == peer_address_type_t::PUBLIC_IDENTITY) {
537+
_connect_to_address_type = peerAddressType;
538+
_connect_to_address = peerAddress;
539+
}
540+
#endif BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
541+
527542
if (is_extended_advertising_available() == false) {
528543
phy_set_t set(connectionParams.getPhySet());
529544
if (set.count() != 1 || set.get_1m() == false) {
@@ -533,35 +548,52 @@ ble_error_t Gap::connect(
533548
if (!_scan_enabled) {
534549
if (!_active_sets.get(LEGACY_ADVERTISING_HANDLE) &&
535550
!_pending_sets.get(LEGACY_ADVERTISING_HANDLE)
536-
) {
551+
) {
537552
_pal_gap.set_random_address(*address);
538553
}
539554
} else {
540-
// ensure scan is stopped.
541-
_pal_gap.scan_enable(false, false);
555+
stopScan();
556+
}
557+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
558+
if (_connect_to_address_type != peer_address_type_t::ANONYMOUS) {
559+
ret = startScan(
560+
(scan_duration_t)connectionParams.getConnectionSupervisionTimeoutArray()[0],
561+
duplicates_filter_t::ENABLE,
562+
(scan_period_t)0
563+
);
564+
if (ret == BLE_ERROR_NONE) {
565+
_connection_parameters = new ConnectionParameters(connectionParams);
566+
if (_connection_parameters) {
567+
_ready_to_connect_to_host_resolved_address = true;
568+
} else {
569+
ret = BLE_ERROR_NO_MEM;
570+
}
571+
}
572+
} else
573+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
574+
{
575+
ret = _pal_gap.create_connection(
576+
connectionParams.getScanIntervalArray()[0],
577+
connectionParams.getScanWindowArray()[0],
578+
connectionParams.getFilter(),
579+
(connection_peer_address_type_t::type) peerAddressType.value(),
580+
peerAddress,
581+
connectionParams.getOwnAddressType(),
582+
connectionParams.getMinConnectionIntervalArray()[0],
583+
connectionParams.getMaxConnectionIntervalArray()[0],
584+
connectionParams.getSlaveLatencyArray()[0],
585+
connectionParams.getConnectionSupervisionTimeoutArray()[0],
586+
connectionParams.getMinEventLengthArray()[0],
587+
connectionParams.getMaxConnectionIntervalArray()[0]
588+
);
542589
}
543-
544-
ret = _pal_gap.create_connection(
545-
connectionParams.getScanIntervalArray()[0],
546-
connectionParams.getScanWindowArray()[0],
547-
connectionParams.getFilter(),
548-
(connection_peer_address_type_t::type) peerAddressType.value(),
549-
peerAddress,
550-
connectionParams.getOwnAddressType(),
551-
connectionParams.getMinConnectionIntervalArray()[0],
552-
connectionParams.getMaxConnectionIntervalArray()[0],
553-
connectionParams.getSlaveLatencyArray()[0],
554-
connectionParams.getConnectionSupervisionTimeoutArray()[0],
555-
connectionParams.getMinEventLengthArray()[0],
556-
connectionParams.getMaxConnectionIntervalArray()[0]
557-
);
558590
} else {
559591
// set the correct mac address before starting scanning.
560592
if (!_scan_enabled) {
561593
_pal_gap.set_random_address(*address);
562594
} else {
563595
// ensure scan is stopped.
564-
_pal_gap.extended_scan_enable(false, duplicates_filter_t::DISABLE, 0, 0);
596+
stopScan();
565597
}
566598

567599
// reduce the address type to public or random
@@ -1150,6 +1182,32 @@ void Gap::on_scan_stopped(bool success)
11501182
}
11511183
}
11521184

1185+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
1186+
void Gap::connecting_to_host_resolved_address_failed()
1187+
{
1188+
if (_event_handler) {
1189+
_event_handler->onConnectionComplete(
1190+
ConnectionCompleteEvent(
1191+
BLE_ERROR_NOT_FOUND,
1192+
INVALID_ADVERTISING_HANDLE,
1193+
connection_role_t::CENTRAL,
1194+
peer_address_type_t::ANONYMOUS,
1195+
ble::address_t(),
1196+
ble::address_t(),
1197+
ble::address_t(),
1198+
ble::conn_interval_t::max(),
1199+
/* dummy slave latency */ 0,
1200+
ble::supervision_timeout_t::max(),
1201+
/* master clock accuracy */ 0
1202+
)
1203+
);
1204+
}
1205+
_ready_to_connect_to_host_resolved_address = false;
1206+
_connect_to_address_type = peer_address_type_t::ANONYMOUS;
1207+
delete _connection_parameters;
1208+
_connection_parameters = nullptr;
1209+
}
1210+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
11531211

11541212
void Gap::on_scan_timeout()
11551213
{
@@ -1161,6 +1219,13 @@ void Gap::on_scan_timeout()
11611219
_scan_enabled = false;
11621220
_scan_pending = false;
11631221

1222+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
1223+
if (_ready_to_connect_to_host_resolved_address) {
1224+
connecting_to_host_resolved_address_failed();
1225+
return;
1226+
}
1227+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
1228+
11641229
if (_event_handler) {
11651230
_event_handler->onScanTimeout(ScanTimeoutEvent());
11661231
}
@@ -1176,6 +1241,13 @@ void Gap::process_legacy_scan_timeout()
11761241
/* legacy scanning timed out is based on timer so we need to stop the scan manually */
11771242
_pal_gap.scan_enable(false, false);
11781243

1244+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
1245+
if (_ready_to_connect_to_host_resolved_address) {
1246+
connecting_to_host_resolved_address_failed();
1247+
return;
1248+
}
1249+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
1250+
11791251
if (_event_handler) {
11801252
_event_handler->onScanTimeout(ScanTimeoutEvent());
11811253
}
@@ -2551,9 +2623,18 @@ void Gap::signal_advertising_report(
25512623

25522624
/* if successful then proceed to call the handler immediately same as for when privacy is disabled */
25532625
if (address_resolved) {
2554-
_event_handler->onAdvertisingReport(
2555-
event
2556-
);
2626+
if (_ready_to_connect_to_host_resolved_address) {
2627+
if (_connect_to_address_type == event.getDirectAddressType() &&
2628+
_connect_to_address == event.getDirectAddress()) {
2629+
connect(_connect_to_address_type, _connect_to_address, *_connection_parameters);
2630+
delete _connection_parameters;
2631+
_connection_parameters = nullptr;
2632+
}
2633+
} else {
2634+
_event_handler->onAdvertisingReport(
2635+
event
2636+
);
2637+
}
25572638
} else {
25582639
/* check if there already is a RPA like that in the list of other pending reports */
25592640
PendingAdvertisingReportEvent *duplicate_pending_event = _reports_pending_address_resolution.find(
@@ -2602,12 +2683,25 @@ void Gap::conclude_signal_advertising_report_after_address_resolution(
26022683
const address_t *identity_address
26032684
)
26042685
{
2686+
2687+
26052688
/* fix the report with the new address if there's an identity found */
26062689
if (identity_address) {
2690+
const peer_address_type_t peer_address_type = (identity_address_type == target_peer_address_type_t::RANDOM) ?
2691+
peer_address_type_t::RANDOM_STATIC_IDENTITY
2692+
: peer_address_type_t::PUBLIC_IDENTITY;
2693+
2694+
if (_ready_to_connect_to_host_resolved_address) {
2695+
if (_connect_to_address_type == peer_address_type && _connect_to_address == *identity_address) {
2696+
connect(event.getPeerAddressType(), event.getPeerAddress(), *_connection_parameters);
2697+
delete _connection_parameters;
2698+
_connection_parameters = nullptr;
2699+
return;
2700+
}
2701+
}
2702+
26072703
event.setPeerAddress(*identity_address);
2608-
event.setPeerAddressType(identity_address_type == target_peer_address_type_t::RANDOM ?
2609-
peer_address_type_t::RANDOM_STATIC_IDENTITY
2610-
: peer_address_type_t::PUBLIC_IDENTITY);
2704+
event.setPeerAddressType(peer_address_type);
26112705
} else if (_central_privacy_configuration.resolution_strategy ==
26122706
central_privacy_configuration_t::RESOLVE_AND_FILTER &&
26132707
_address_registry.read_resolving_list_size() > 0) {
@@ -2887,7 +2981,11 @@ ble_error_t Gap::startScan(
28872981
scan_period_t period
28882982
)
28892983
{
2890-
if (_scan_pending || _scan_address_refresh) {
2984+
if (_scan_pending || _scan_address_refresh
2985+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
2986+
|| _ready_to_connect_to_host_resolved_address
2987+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
2988+
) {
28912989
return BLE_STACK_BUSY;
28922990
}
28932991

connectivity/FEATURE_BLE/source/generic/GapImpl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,10 @@ class Gap :
798798

799799
const address_t *get_random_address(controller_operation_t operation, size_t advertising_set = 0);
800800

801+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
802+
void connecting_to_host_resolved_address_failed();
803+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
804+
801805
private:
802806
/**
803807
* Callchain containing all registered callback handlers for shutdown
@@ -847,6 +851,13 @@ class Gap :
847851
bool _scan_pending = false;
848852
bool _scan_interruptible = false;
849853
bool _scan_address_refresh = false;
854+
#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
855+
bool _ready_to_connect_to_host_resolved_address = false;
856+
peer_address_type_t _connect_to_address_type = peer_address_type_t::ANONYMOUS;
857+
ble::address_t _connect_to_address;
858+
ConnectionParameters *_connection_parameters = nullptr;
859+
#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
860+
850861
mbed::LowPowerTimeout _advertising_timeout;
851862
mbed::LowPowerTimeout _scan_timeout;
852863
mbed::LowPowerTicker _address_rotation_ticker;

0 commit comments

Comments
 (0)