diff --git a/connectivity/FEATURE_BLE/include/ble/gap/Types.h b/connectivity/FEATURE_BLE/include/ble/gap/Types.h index 6049746940..0b2d1a4f9f 100644 --- a/connectivity/FEATURE_BLE/include/ble/gap/Types.h +++ b/connectivity/FEATURE_BLE/include/ble/gap/Types.h @@ -455,8 +455,8 @@ struct scanning_filter_policy_t : SafeEnum { NO_FILTER = 0x00, /** - * Accept only advertising packets from devices in the whitelist except - * directed advertising packets not addressed to this device. + * Accept only advertising packets from devices in the whitelist. + * Directed advertising packets not addressed to this device will be ignored. */ FILTER_ADVERTISING = 0x01, diff --git a/connectivity/FEATURE_BLE/mbed_lib.json b/connectivity/FEATURE_BLE/mbed_lib.json index 27b4fc0a7b..f7cefee640 100644 --- a/connectivity/FEATURE_BLE/mbed_lib.json +++ b/connectivity/FEATURE_BLE/mbed_lib.json @@ -93,10 +93,20 @@ "value": 15, "macro_name": "BLE_GAP_MAX_ADVERTISING_SETS" }, + "ble-gap-host-based-private-address-resolution": { + "help": "Perform address resolution on the host, not the controller. Controller based privacy is preferred as it happens lower down the stack but this can be used in case controller based privacy is unavailable. If this is enabled the controller will not be used for privacy.", + "value": true, + "macro_name": "BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION" + }, "ble-gap-max-advertising-reports-pending-address-resolution": { "help": "How many advertising reports can be pending while awaiting private address resolution. This is only used if host privacy is enabled and controller privacy is disabled. Must be non-zero", "value": 16, "macro_name": "BLE_GAP_MAX_ADVERTISING_REPORTS_PENDING_ADDRESS_RESOLUTION" + }, + "ble-gap-host-privacy-resolved-cache-size": { + "help": "Used for host privacy. How many last resolved addresses to store to speed up resolution. This is especially valuable for resolving advertising which creates repeated queries for the same address.", + "value": 16, + "macro_name": "BLE_GAP_HOST_PRIVACY_RESOLVED_CACHE_SIZE" } } } diff --git a/connectivity/FEATURE_BLE/source/Gap.cpp b/connectivity/FEATURE_BLE/source/Gap.cpp index b45031807f..5fa3b992db 100644 --- a/connectivity/FEATURE_BLE/source/Gap.cpp +++ b/connectivity/FEATURE_BLE/source/Gap.cpp @@ -1,7 +1,7 @@ /* mbed Microcontroller Library * Copyright (c) 2020 ARM Limited * SPDX-License-Identifier: Apache-2.0 - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -506,4 +506,4 @@ ble::address_t Gap::getRandomStaticAddress() return impl->getRandomStaticAddress(); } -} // namespace ble \ No newline at end of file +} // namespace ble diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp index 2bc49b2ad7..1e87e37938 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp @@ -1,7 +1,7 @@ /* mbed Microcontroller Library * Copyright (c) 2020 ARM Limited * SPDX-License-Identifier: Apache-2.0 - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -193,6 +193,7 @@ bool PalPrivateAddressController::cordio_handler(const wsfMsgHdr_t *msg) return true; } +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION case DM_PRIV_RESOLVED_ADDR_IND: { instance()._resolving_rpa = false; @@ -203,6 +204,7 @@ bool PalPrivateAddressController::cordio_handler(const wsfMsgHdr_t *msg) handler->on_private_address_resolved(msg->status == HCI_SUCCESS); return true; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION case DM_PRIV_ADD_DEV_TO_RES_LIST_IND: // Device added to resolving list case DM_PRIV_REM_DEV_FROM_RES_LIST_IND: // Device removed from resolving list diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp index 51c739b7ec..6cdbf2414c 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp @@ -929,9 +929,11 @@ ble_error_t Gap::enablePrivacy(bool enable) _address_registry.stop_private_address_generation(); } +#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION if (_address_registry.is_controller_privacy_supported()) { update_ll_address_resolution_setting(); } +#endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION return BLE_ERROR_NONE; } @@ -946,9 +948,11 @@ ble_error_t Gap::setPeripheralPrivacyConfiguration( { _peripheral_privacy_configuration = *configuration; +#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION if (_address_registry.is_controller_privacy_supported()) { update_ll_address_resolution_setting(); } +#endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION return BLE_ERROR_NONE; } @@ -978,9 +982,11 @@ ble_error_t Gap::setCentralPrivacyConfiguration( { _central_privacy_configuration = *configuration; +#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION if (_address_registry.is_controller_privacy_supported()) { update_ll_address_resolution_setting(); } +#endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION return BLE_ERROR_NONE; } @@ -1474,7 +1480,7 @@ bool Gap::initialize_whitelist() const return true; } - +#if BLE_FEATURE_PRIVACY && !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION ble_error_t Gap::update_ll_address_resolution_setting() { // enable if privacy is enabled and resolution is requested in either central or peripheral mode @@ -1496,6 +1502,7 @@ ble_error_t Gap::update_ll_address_resolution_setting() return _address_registry.enable_controller_address_resolution(enable); } +#endif // BLE_FEATURE_PRIVACY && !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION #if BLE_ROLE_BROADCASTER uint8_t Gap::getMaxAdvertisingSetNumber() @@ -2314,12 +2321,11 @@ void Gap::signal_connection_complete( ConnectionCompleteEvent& event ) { -#if BLE_FEATURE_PRIVACY +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION bool address_resolved = false; /* if address resolution is not needed or already handled then the address is already resolved */ if (!_privacy_enabled || - _address_registry.is_controller_privacy_supported() || event.getPeerAddressType() == peer_address_type_t::PUBLIC || !is_random_private_resolvable_address(event.getPeerAddress())) { address_resolved = true; @@ -2330,7 +2336,7 @@ void Gap::signal_connection_complete( address_resolved = true; } } -#endif +#endif // BLE_ROLE_CENTRAL #if BLE_ROLE_PERIPHERAL if (event.getOwnRole() == connection_role_t::PERIPHERAL) { @@ -2347,7 +2353,7 @@ void Gap::signal_connection_complete( /* first try to resolve synchronously in cache */ if (!address_resolved) { - address_resolved = _address_registry.resolve_address_in_cache( + address_resolved = _address_registry.resolve_address_in_host_cache( event.getPeerAddress(), &peer_address_type, &peer_address @@ -2372,7 +2378,7 @@ void Gap::signal_connection_complete( _event_handler->onConnectionComplete(event); } else { bool resolution_pending = false; - ble_error_t ret = _address_registry.queue_resolve_address(event.getPeerAddress()); + ble_error_t ret = _address_registry.queue_resolve_address_on_host(event.getPeerAddress()); if (ret == BLE_ERROR_NONE) { ConnectionCompleteEvent* event_copy = new(std::nothrow) ConnectionCompleteEvent(event); @@ -2397,11 +2403,10 @@ void Gap::signal_connection_complete( #else report_internal_connection_complete(event); _event_handler->onConnectionComplete(event); -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } #if BLE_FEATURE_PRIVACY - bool Gap::apply_peripheral_privacy_connection_policy( const ConnectionCompleteEvent &event ) @@ -2451,8 +2456,9 @@ bool Gap::apply_peripheral_privacy_connection_policy( return true; #endif } +#endif // BLE_FEATURE_PRIVACY - +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void Gap::conclude_signal_connection_complete_after_address_resolution( ConnectionCompleteEvent &event, target_peer_address_type_t identity_address_type, @@ -2478,7 +2484,7 @@ void Gap::conclude_signal_connection_complete_after_address_resolution( report_internal_connection_complete(event); _event_handler->onConnectionComplete(event); } -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION #endif // BLE_FEATURE_CONNECTABLE #if BLE_ROLE_OBSERVER @@ -2486,13 +2492,12 @@ void Gap::signal_advertising_report( AdvertisingReportEvent& event ) { -#if BLE_FEATURE_PRIVACY +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION bool address_resolved = false; /* if address resolution is not needed or already handled then the address is already resolved */ if (!_privacy_enabled || _central_privacy_configuration.resolution_strategy == central_privacy_configuration_t::DO_NOT_RESOLVE || - _address_registry.is_controller_privacy_supported() || event.getPeerAddressType() == peer_address_type_t::PUBLIC || !is_random_private_resolvable_address(event.getPeerAddress())) { address_resolved = true; @@ -2503,7 +2508,7 @@ void Gap::signal_advertising_report( const address_t *peer_address = nullptr; target_peer_address_type_t peer_address_type(target_peer_address_type_t::RANDOM); - address_resolved = _address_registry.resolve_address_in_cache( + address_resolved = _address_registry.resolve_address_in_host_cache( event.getPeerAddress(), &peer_address_type, &peer_address @@ -2536,7 +2541,7 @@ void Gap::signal_advertising_report( /* if there is already an item with the same address pending don't kick off resolution*/ if (!duplicate_pending_event) { - ret = _address_registry.queue_resolve_address(event.getPeerAddress()); + ret = _address_registry.queue_resolve_address_on_host(event.getPeerAddress()); } if (ret == BLE_ERROR_NONE) { @@ -2553,11 +2558,11 @@ void Gap::signal_advertising_report( _event_handler->onAdvertisingReport( event ); -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } #endif //BLE_ROLE_OBSERVER -#if BLE_FEATURE_PRIVACY +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION #if BLE_ROLE_OBSERVER void Gap::conclude_signal_advertising_report_after_address_resolution( AdvertisingReportEvent &event, @@ -2581,7 +2586,7 @@ void Gap::conclude_signal_advertising_report_after_address_resolution( _event_handler->onAdvertisingReport(event); } #endif // BLE_ROLE_OBSERVER -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void Gap::on_periodic_advertising_sync_established( hci_error_code_t error, @@ -3179,7 +3184,8 @@ void Gap::on_private_address_generated(bool connectable) } } - +#if BLE_FEATURE_PRIVACY +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void Gap::on_address_resolution_completed( const address_t &peer_resolvable_address, bool resolved, @@ -3187,7 +3193,6 @@ void Gap::on_address_resolution_completed( const address_t &identity_address ) { -#if BLE_FEATURE_PRIVACY if (!_event_handler || !_privacy_enabled) { return; } @@ -3237,9 +3242,9 @@ void Gap::on_address_resolution_completed( delete event; } #endif // BLE_ROLE_OBSERVER -#endif // BLE_FEATURE_PRIVACY } - +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#endif // BLE_FEATURE_PRIVACY bool Gap::is_advertising() const { @@ -3297,7 +3302,7 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_ #if BLE_FEATURE_EXTENDED_ADVERTISING if (is_extended_advertising_available()) { if (operation == controller_operation_t::advertising) { - if (_set_is_connectable.get(set_id) == false && peripheral_non_resolvable) { + if (!_set_is_connectable.get(set_id) && peripheral_non_resolvable) { return &non_resolvable_address; } else { return &resolvable_address; diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index 46b8f5ff6e..c0ab2a9063 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -493,6 +493,7 @@ private: IndexType _current_index = 0; }; +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION class PendingAdvertisingReportEvent { public: PendingAdvertisingReportEvent( @@ -530,6 +531,7 @@ private: AdvertisingReportEvent event; uint8_t *advertising_data_buffer = nullptr; }; +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION private: /* Disallow copy and assignment. */ @@ -585,7 +587,9 @@ private: bool initialize_whitelist() const; +#if BLE_FEATURE_PRIVACY && !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION ble_error_t update_ll_address_resolution_setting(); +#endif // BLE_FEATURE_PRIVACY && !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION ble_error_t setExtendedAdvertisingParameters( advertising_handle_t handle, @@ -620,7 +624,9 @@ private: bool apply_peripheral_privacy_connection_policy( const ConnectionCompleteEvent &event ); +#endif // BLE_FEATURE_PRIVACY +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** Pass the connection complete event to the application after privacy resolution completed. * * @param event Event to be passed to the user application. @@ -632,7 +638,7 @@ private: target_peer_address_type_t identity_address_type, const address_t *identity_address ); -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION #endif // BLE_FEATURE_CONNECTABLE #if BLE_ROLE_OBSERVER @@ -642,7 +648,7 @@ private: */ void signal_advertising_report(AdvertisingReportEvent& report); -#if BLE_FEATURE_PRIVACY +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** Pass the advertising report to the application after privacy resolution completed. * * @param event Event to be passed to the user application. @@ -654,7 +660,7 @@ private: target_peer_address_type_t identity_address_type, const address_t *identity_address ); -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION #endif // BLE_ROLE_OBSERVER /* implements PalGap::EventHandler */ @@ -767,13 +773,14 @@ private: void on_non_resolvable_private_addresses_generated(const address_t &address) final; void on_private_address_generated(bool connectable); - +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void on_address_resolution_completed( const address_t &peer_resolvable_address, bool resolved, target_peer_address_type_t identity_address_type, const address_t &identity_address ) final; +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION private: bool is_advertising() const; @@ -803,14 +810,14 @@ private: */ ble::Gap::EventHandler *_event_handler; -#if BLE_FEATURE_PRIVACY +#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION #if BLE_ROLE_OBSERVER EventList _reports_pending_address_resolution; #endif // BLE_ROLE_OBSERVER #if BLE_FEATURE_CONNECTABLE EventList _connections_pending_address_resolution; #endif // BLE_FEATURE_CONNECTABLE -#endif // BLE_FEATURE_PRIVACY +#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION PalEventQueue &_event_queue; PalGap &_pal_gap; diff --git a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp index ea3837ed95..31638ad1fb 100644 --- a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp +++ b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp @@ -31,11 +31,13 @@ PrivateAddressController::PrivateAddressController( _pal.initialize(); _pal.set_event_handler(this); +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION auto** next = &_free_resolution_entries; for (auto &entry : _resolution_list) { *next = &entry; next = &entry.next; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } PrivateAddressController::~PrivateAddressController() @@ -132,19 +134,25 @@ bool PrivateAddressController::is_controller_privacy_supported() return _pal.is_ll_privacy_supported(); } +#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION ble_error_t PrivateAddressController::enable_controller_address_resolution(bool enable) { MBED_ASSERT(is_controller_privacy_supported()); return _pal.set_ll_address_resolution(enable); } +#endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION uint8_t PrivateAddressController::read_resolving_list_capacity() { +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + return BLE_SECURITY_DATABASE_MAX_ENTRIES; +#else if (is_controller_privacy_supported()) { return _pal.read_resolving_list_capacity(); } else { - return RESOLVING_LIST_SIZE; + return 0; } +#endif //BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } ble_error_t PrivateAddressController::add_device_to_resolving_list( @@ -157,6 +165,47 @@ ble_error_t PrivateAddressController::add_device_to_resolving_list( return BLE_ERROR_INVALID_STATE; } +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + // ensure an entry is not added twice + for (auto &entry : _resolving_list) { + if (entry.populated && + entry.peer_address_type == peer_address_type && + entry.peer_address == peer_identity_address && + entry.peer_irk == peer_irk + ) { + return BLE_ERROR_NONE; + } + } + + bool entry_added = false; + for (auto &entry : _resolving_list) { + if (entry.populated == false) { + entry.peer_address_type = peer_address_type; + entry.peer_address = peer_identity_address; + entry.peer_irk = peer_irk; + entry.populated = true; + entry_added = true; + break; + } + } + + if (!entry_added) { + return BLE_ERROR_NO_MEM; + } + + + // Remove unresolved entries from the resolved list + remove_resolution_entry_from_cache( + [&](resolution_entry_t &entry) { + return entry.identity == nullptr; + } + ); + + // reset pending resolution request + restart_resolution_process_on_host(); + + return BLE_ERROR_NO_MEM; +#else if (is_controller_privacy_supported()) { return queue_add_device_to_resolving_list( peer_address_type, @@ -164,46 +213,9 @@ ble_error_t PrivateAddressController::add_device_to_resolving_list( peer_irk ); } else { - // ensure an entry is not added twice - for (auto &entry : _resolving_list) { - if (entry.populated && - entry.peer_address_type == peer_address_type && - entry.peer_address == peer_identity_address && - entry.peer_irk == peer_irk - ) { - return BLE_ERROR_NONE; - } - } - - bool entry_added = false; - for (auto &entry : _resolving_list) { - if (entry.populated == false) { - entry.peer_address_type = peer_address_type; - entry.peer_address = peer_identity_address; - entry.peer_irk = peer_irk; - entry.populated = true; - entry_added = true; - break; - } - } - - if (!entry_added) { - return BLE_ERROR_NO_MEM; - } - - - // Remove unresolved entries from the resolved list - remove_resolution_entry_from_cache( - [&](resolution_entry_t &entry) { - return entry.identity == nullptr; - } - ); - - // reset pending resolution request - restart_resolution_process(); - - return BLE_ERROR_NO_MEM; + return BLE_ERROR_NOT_IMPLEMENTED; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } ble_error_t PrivateAddressController::remove_device_from_resolving_list( @@ -211,48 +223,58 @@ ble_error_t PrivateAddressController::remove_device_from_resolving_list( const address_t &peer_identity_address ) { +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + for (auto &entry : _resolving_list) { + if (entry.populated && + entry.peer_address_type == peer_address_type && + entry.peer_address == peer_identity_address + ) { + remove_resolution_entry_from_cache([&](resolution_entry_t& cache_entry) { + return cache_entry.identity == &entry; + }); + + entry.populated = false; + + restart_resolution_process_on_host(); + } + } + return BLE_ERROR_NONE; +#else if (is_controller_privacy_supported()) { return queue_remove_device_from_resolving_list(peer_address_type, peer_identity_address); } else { - for (auto &entry : _resolving_list) { - if (entry.populated && - entry.peer_address_type == peer_address_type && - entry.peer_address == peer_identity_address - ) { - remove_resolution_entry_from_cache([&](resolution_entry_t& cache_entry) { - return cache_entry.identity == &entry; - }); - - entry.populated = false; - - restart_resolution_process(); - } - } - return BLE_ERROR_NONE; + return BLE_ERROR_NOT_IMPLEMENTED; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } ble_error_t PrivateAddressController::clear_resolving_list() { +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + // Remove entry from the resolving list + for (auto &entry : _resolving_list) { + entry.populated = false; + } + // Remove all resolved entries from the cache + remove_resolution_entry_from_cache([&](resolution_entry_t& entry) { + return entry.identity != nullptr; + }); + + restart_resolution_process_on_host(); + + return BLE_ERROR_NONE; +#else if (is_controller_privacy_supported()) { return queue_clear_resolving_list(); } else { - // Remove entry from the resolving list - for (auto &entry : _resolving_list) { - entry.populated = false; - } - // Remove all resolved entries from the cache - remove_resolution_entry_from_cache([&](resolution_entry_t& entry) { - return entry.identity != nullptr; - }); - - restart_resolution_process(); - - return BLE_ERROR_NONE; + return BLE_ERROR_NOT_IMPLEMENTED; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } -bool PrivateAddressController::resolve_address_in_cache( +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + +bool PrivateAddressController::resolve_address_in_host_cache( const address_t &peer_address, target_peer_address_type_t *retrieved_address_type, const address_t **retrieved_address @@ -287,27 +309,24 @@ bool PrivateAddressController::resolve_address_in_cache( return false; } -ble_error_t PrivateAddressController::resolve_address( +ble_error_t PrivateAddressController::resolve_address_on_host( const address_t &peer_address, bool *resolution_complete, target_peer_address_type_t *retrieved_address_type, const address_t **retrieved_address ) { - if (is_controller_privacy_supported()) { - return BLE_ERROR_NOT_IMPLEMENTED; - } - - *resolution_complete = resolve_address_in_cache(peer_address, retrieved_address_type, retrieved_address); + *resolution_complete = resolve_address_in_host_cache(peer_address, retrieved_address_type, retrieved_address); // In the case the address has not been resolved, we start the resolution // process. if (*resolution_complete) { return BLE_ERROR_NONE; } else { - return queue_resolve_address(peer_address); + return queue_resolve_address_on_host(peer_address); } } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void PrivateAddressController::on_resolving_list_action_complete() { @@ -338,6 +357,7 @@ private: PrivacyControlBlock *_next; }; +#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION struct PrivateAddressController::PrivacyAddDevToResListControlBlock final : PrivateAddressController::PrivacyControlBlock { PrivacyAddDevToResListControlBlock( @@ -462,10 +482,12 @@ ble_error_t PrivateAddressController::queue_clear_resolving_list() queue_privacy_control_block(cb); return BLE_ERROR_NONE; } +#endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION -struct PrivateAddressController::PrivacyResolveAddress final : +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +struct PrivateAddressController::PrivacyResolveAddressOnHost final : PrivateAddressController::PrivacyControlBlock { - PrivacyResolveAddress(const address_t &peer_address) : + PrivacyResolveAddressOnHost(const address_t &peer_address) : PrivacyControlBlock(), peer_address(peer_address) { @@ -508,7 +530,7 @@ private: bool start_next_resolution_round(PrivateAddressController& self) { do { ++resolving_list_index; - if (resolving_list_index == RESOLVING_LIST_SIZE) { + if (resolving_list_index == BLE_SECURITY_DATABASE_MAX_ENTRIES) { notify_completion(self, peer_address,false,nullptr); return true; } @@ -551,9 +573,9 @@ private: bool require_restart = false; }; -ble_error_t PrivateAddressController::queue_resolve_address(const address_t &peer_address) +ble_error_t PrivateAddressController::queue_resolve_address_on_host(const address_t &peer_address) { - auto *cb = new(std::nothrow) PrivacyResolveAddress(peer_address); + auto *cb = new(std::nothrow) PrivacyResolveAddressOnHost(peer_address); if (cb == nullptr) { // Cannot go further return BLE_ERROR_NO_MEM; @@ -566,27 +588,27 @@ ble_error_t PrivateAddressController::queue_resolve_address(const address_t &pee return BLE_ERROR_NONE; } -void PrivateAddressController::restart_resolution_process() +void PrivateAddressController::restart_resolution_process_on_host() { // if processing is active, restart the one running. if (_processing_privacy_control_block && _pending_privacy_control_blocks) { - static_cast(_pending_privacy_control_blocks)->invalidate(); + static_cast(_pending_privacy_control_blocks)->invalidate(); } } void PrivateAddressController::on_private_address_resolved(bool success) { - MBED_ASSERT(is_controller_privacy_supported() == false); if (_pending_privacy_control_blocks == nullptr || _processing_privacy_control_block == false ) { return; } - auto* cb = static_cast(_pending_privacy_control_blocks); + auto* cb = static_cast(_pending_privacy_control_blocks); bool completed = cb->on_resolution_complete(*this, success); process_privacy_control_blocks(completed); } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void PrivateAddressController::clear_privacy_control_blocks() { @@ -649,6 +671,8 @@ void PrivateAddressController::process_privacy_control_blocks(bool cb_completed) _pending_privacy_control_blocks = cb; } +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + template void PrivateAddressController::remove_resolution_entry_from_cache(const Pred &predicate) { @@ -701,4 +725,6 @@ void PrivateAddressController::add_resolution_entry_to_cache( _resolved_list = entry; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + } // namespace ble diff --git a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h index 2d7a0f7aad..b7f2c0b44a 100644 --- a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h +++ b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h @@ -52,7 +52,7 @@ public: virtual void on_non_resolvable_private_addresses_generated( const address_t &address ) = 0; - +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** * Called when address resolution has been completed by the host. * @@ -73,6 +73,7 @@ public: target_peer_address_type_t identity_address_type, const address_t &identity_address ) = 0; +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION }; /** @@ -139,10 +140,12 @@ public: */ bool is_controller_privacy_supported(); +#if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** * Enable address resolution by the controller. */ ble_error_t enable_controller_address_resolution(bool enable); +#endif //!BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** * Read the number of entry that can be put in the resolving list. @@ -178,6 +181,7 @@ public: */ ble_error_t clear_resolving_list(); +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** * Resolve a private resolvable address on the host. * @@ -196,13 +200,12 @@ public: * input this will be set to null if the address has not been resolved with * the local irks or a pointer to the identity address if it has been resolved. */ - ble_error_t resolve_address( + ble_error_t resolve_address_on_host( const address_t &peer_address, bool *resolution_complete, target_peer_address_type_t *retrieved_address_type, const address_t **retrieved_address ); - /** * Resolve a private address by looking in the cache. * @@ -215,7 +218,7 @@ public: * * @return True if the address has been found in cache. */ - bool resolve_address_in_cache( + bool resolve_address_in_host_cache( const address_t &peer_address, target_peer_address_type_t *retrieved_address_type, const address_t **retrieved_address @@ -227,7 +230,8 @@ public: * * @param peer_address the address to resolve. */ - ble_error_t queue_resolve_address(const address_t &peer_address); + ble_error_t queue_resolve_address_on_host(const address_t &peer_address); +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION private: @@ -238,7 +242,9 @@ private: // EventHandler implementation void on_resolvable_private_address_generated(const address_t &rpa) final; +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void on_private_address_resolved(bool success) final; +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void on_resolving_list_action_complete() final; @@ -269,9 +275,11 @@ private: // Queue control block to clear resolving list ble_error_t queue_clear_resolving_list(); - struct PrivacyResolveAddress; +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + struct PrivacyResolveAddressOnHost; - void restart_resolution_process(); + void restart_resolution_process_on_host(); +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION // Clear all control blocks void clear_privacy_control_blocks(); @@ -283,12 +291,14 @@ private: // cb_completed is set when the previous block has completed void process_privacy_control_blocks(bool cb_completed); +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION + struct resolving_list_entry_t; + template void remove_resolution_entry_from_cache(const Pred& predicate); - struct resolving_list_entry_t; - void add_resolution_entry_to_cache(const address_t& address, resolving_list_entry_t* identity); +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION PalPrivateAddressController &_pal; PalEventQueue &_event_queue; @@ -303,6 +313,7 @@ private: PrivacyControlBlock *_pending_privacy_control_blocks = nullptr; bool _processing_privacy_control_block = false; +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION struct resolving_list_entry_t { address_t peer_address = {}; irk_t peer_irk = {}; @@ -310,10 +321,7 @@ private: bool populated = false; }; - // FIXME: Use configuration - static const size_t RESOLVING_LIST_SIZE = 8; - resolving_list_entry_t _resolving_list[RESOLVING_LIST_SIZE]; - + resolving_list_entry_t _resolving_list[BLE_SECURITY_DATABASE_MAX_ENTRIES]; struct resolution_entry_t { address_t address = address_t {}; @@ -321,11 +329,10 @@ private: resolving_list_entry_t *identity = nullptr; }; - // FIXME: Use configuration - static const size_t RESOLVED_ADDRESS_CACHE_SIZE = 16; - resolution_entry_t _resolution_list[RESOLVED_ADDRESS_CACHE_SIZE]; + resolution_entry_t _resolution_list[BLE_GAP_HOST_PRIVACY_RESOLVED_CACHE_SIZE]; resolution_entry_t *_resolved_list = nullptr; resolution_entry_t *_free_resolution_entries = nullptr; +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION }; } diff --git a/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h b/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h index 4faa2545e1..657adf9e47 100644 --- a/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h +++ b/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h @@ -30,11 +30,13 @@ namespace ble { */ struct PalPrivateAddressController { struct EventHandler { +#if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** Called when the address resolution request has completed. * * @param success true if the address has been resolved and false otherwise. */ virtual void on_private_address_resolved(bool success) = 0; +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION /** Called when the resolvable private address request has completed. *