@@ -467,11 +467,7 @@ ble_error_t Gap::stopScan()
467467{
468468 ble_error_t err;
469469
470- if ((!_scan_enabled && !_scan_pending) || _scan_pending
471- #if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
472- || (_connect_to_host_resolved_address_state != ConnectionToHostResolvedAddressState::idle)
473- #endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
474- ) {
470+ if ((!_scan_enabled && !_scan_pending) || _scan_pending || _initiating) {
475471 return BLE_STACK_BUSY;
476472 }
477473
@@ -532,15 +528,26 @@ ble_error_t Gap::connect(
532528 ble_error_t ret = BLE_ERROR_INTERNAL_STACK_FAILURE;
533529
534530#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
531+ /* if host resolution is used we need to connect in two passes, first we scan for addresses to find
532+ * a resolving match and then we call connect again with the correct address */
535533 if (_connect_to_host_resolved_address_state == ConnectionToHostResolvedAddressState::idle) {
536534 if (peerAddressType == peer_address_type_t ::RANDOM_STATIC_IDENTITY ||
537535 peerAddressType == peer_address_type_t ::PUBLIC_IDENTITY) {
536+
537+ _connect_to_host_resolved_address_parameters = new ConnectionParameters (connectionParams);
538+ if (!_connect_to_host_resolved_address_parameters) {
539+ return BLE_ERROR_NO_MEM;
540+ }
541+
538542 _connect_to_host_resolved_address_type = peerAddressType;
539543 _connect_to_host_resolved_address = peerAddress;
540544 _connect_to_host_resolved_address_state = ConnectionToHostResolvedAddressState::scan;
541545 }
542546 } else if (_connect_to_host_resolved_address_state == ConnectionToHostResolvedAddressState::connect) {
547+ /* the first pass of connect has completed and this is the second connect that doesn't require
548+ * address resolution */
543549 _connect_to_host_resolved_address_state = ConnectionToHostResolvedAddressState::idle;
550+ _initiating = false ;
544551 }
545552
546553#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
@@ -567,13 +574,7 @@ ble_error_t Gap::connect(
567574 duplicates_filter_t ::ENABLE,
568575 (scan_period_t )0
569576 );
570- if (ret == BLE_ERROR_NONE) {
571- _connect_to_host_resolved_address_parameters = new ConnectionParameters (connectionParams);
572- if (!_connect_to_host_resolved_address_parameters) {
573- _connect_to_host_resolved_address_state = ConnectionToHostResolvedAddressState::idle;
574- ret = BLE_ERROR_NO_MEM;
575- }
576- } else {
577+ if (ret != BLE_ERROR_NONE) {
577578 _connect_to_host_resolved_address_state = ConnectionToHostResolvedAddressState::idle;
578579 }
579580 } else
@@ -1031,7 +1032,7 @@ ble_error_t Gap::reset()
10311032 shutdownCallChain.clear ();
10321033
10331034 _event_handler = nullptr ;
1034-
1035+ _initiating = false ;
10351036#if BLE_FEATURE_PRIVACY
10361037 _privacy_initialization_pending = false ;
10371038#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
@@ -1182,6 +1183,7 @@ void Gap::connecting_to_host_resolved_address_failed(bool inform_user)
11821183 _connect_to_host_resolved_address_state = ConnectionToHostResolvedAddressState::idle;
11831184 delete _connect_to_host_resolved_address_parameters;
11841185 _connect_to_host_resolved_address_parameters = nullptr ;
1186+ _initiating = false ;
11851187}
11861188#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
11871189
@@ -2900,11 +2902,7 @@ ble_error_t Gap::startScan(
29002902 scan_period_t period
29012903)
29022904{
2903- if (_scan_pending || _scan_address_refresh
2904- #if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
2905- || (_connect_to_host_resolved_address_state != ConnectionToHostResolvedAddressState::idle)
2906- #endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION
2907- ) {
2905+ if (_scan_pending || _scan_address_refresh || _initiating) {
29082906 return BLE_STACK_BUSY;
29092907 }
29102908
0 commit comments