From 3f9798b52df47e4239befa49e93ca6b943fb2bf2 Mon Sep 17 00:00:00 2001 From: Paul Szczeanek Date: Tue, 13 Oct 2020 16:21:29 +0100 Subject: [PATCH 1/3] Fix conditional compilation based on config --- .../FEATURE_BLE/include/ble/SecurityManager.h | 2 + .../FEATURE_BLE/source/SecurityManager.cpp | 4 +- .../cordio/source/BLEInstanceBaseImpl.cpp | 23 +- .../source/cordio/source/PalGapImpl.cpp | 21 +- .../source/cordio/source/PalGapImpl.h | 2 + .../PalPrivateAddressControllerImpl.cpp | 3 + .../source/PalPrivateAddressControllerImpl.h | 6 +- .../cordio/source/PalSecurityManagerImpl.cpp | 4 + .../FEATURE_BLE/source/generic/GapImpl.cpp | 214 ++++++++++-------- .../FEATURE_BLE/source/generic/GapImpl.h | 64 +++++- .../generic/PrivateAddressController.cpp | 4 + .../source/generic/PrivateAddressController.h | 4 + .../source/generic/SecurityManagerImpl.cpp | 21 +- .../source/generic/SecurityManagerImpl.h | 18 +- connectivity/FEATURE_BLE/source/pal/PalGap.h | 22 ++ .../source/pal/PalPrivateAddressController.h | 3 + 16 files changed, 282 insertions(+), 133 deletions(-) diff --git a/connectivity/FEATURE_BLE/include/ble/SecurityManager.h b/connectivity/FEATURE_BLE/include/ble/SecurityManager.h index 4f6bb67cc9..b3bc6e582f 100644 --- a/connectivity/FEATURE_BLE/include/ble/SecurityManager.h +++ b/connectivity/FEATURE_BLE/include/ble/SecurityManager.h @@ -835,6 +835,7 @@ public: // Privacy // +#if BLE_FEATURE_PRIVACY /** * Sets how often the address is rotated when privacy is enabled. * @@ -845,6 +846,7 @@ public: ble_error_t setPrivateAddressTimeout( uint16_t timeout_in_seconds ); +#endif // BLE_FEATURE_PRIVACY /* Event callback handlers. */ public: diff --git a/connectivity/FEATURE_BLE/source/SecurityManager.cpp b/connectivity/FEATURE_BLE/source/SecurityManager.cpp index 71c16c86e4..3cebbb713f 100644 --- a/connectivity/FEATURE_BLE/source/SecurityManager.cpp +++ b/connectivity/FEATURE_BLE/source/SecurityManager.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 @@ -192,12 +192,14 @@ ble_error_t SecurityManager::getSigningKey(ble::connection_handle_t connectionHa return impl->getSigningKey(connectionHandle, authenticated); } +#if BLE_FEATURE_PRIVACY ble_error_t SecurityManager::setPrivateAddressTimeout( uint16_t timeout_in_seconds ) { return impl->setPrivateAddressTimeout(timeout_in_seconds); } +#endif // BLE_FEATURE_PRIVACY void SecurityManager::onShutdown(const SecurityManagerShutdownCallback_t& callback) { diff --git a/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp index e32fbeeb4d..f7aa9bb750 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp @@ -200,9 +200,10 @@ ble::impl::Gap &BLEInstanceBase::getGapImpl() static ble::impl::Gap gap( _event_queue, ble::impl::PalGap::get_gap(), - cordio_gap_service, - ble::impl::PalSecurityManager::get_security_manager(), - getPrivateAddressRegistry() + cordio_gap_service +#if BLE_FEATURE_PRIVACY + , getPrivateAddressRegistry() +#endif // BLE_FEATURE_PRIVACY ); return gap; } @@ -221,7 +222,6 @@ const ble::Gap &BLEInstanceBase::getGap() const }; #if BLE_FEATURE_GATT_SERVER - ble::impl::GattServer &BLEInstanceBase::getGattServerImpl() { return ble::impl::GattServer::getInstance(); @@ -239,11 +239,9 @@ const ble::GattServer &BLEInstanceBase::getGattServer() const auto &self = const_cast(*this); return const_cast(self.getGattServer()); } - #endif // BLE_FEATURE_GATT_SERVER #if BLE_FEATURE_GATT_CLIENT - ble::impl::GattClient &BLEInstanceBase::getGattClientImpl() { static ble::impl::GattClient gatt_client(getPalGattClient()); @@ -262,11 +260,9 @@ PalGattClient &BLEInstanceBase::getPalGattClient() static PalAttClientToGattClient pal_gatt_client(impl::PalAttClient::get_client()); return pal_gatt_client; } - #endif // BLE_FEATURE_GATT_CLIENT #if BLE_FEATURE_SECURITY - ble::impl::SecurityManager &BLEInstanceBase::getSecurityManagerImpl() { // Creation of a proxy monitor to let the security manager register to @@ -286,13 +282,16 @@ ble::impl::SecurityManager &BLEInstanceBase::getSecurityManagerImpl() static ble::impl::SecurityManager m_instance( ble::impl::PalSecurityManager::get_security_manager(), getGapImpl(), - signing_event_monitor, - getPrivateAddressRegistry() + signing_event_monitor +#if BLE_FEATURE_PRIVACY + , getPrivateAddressRegistry() +#endif //BLE_FEATURE_PRIVACY ); return m_instance; } +#if BLE_FEATURE_SECURITY ble::SecurityManager &BLEInstanceBase::getSecurityManager() { static ble::SecurityManager m_instance(&getSecurityManagerImpl()); @@ -304,6 +303,7 @@ const ble::SecurityManager &BLEInstanceBase::getSecurityManager() const const BLEInstanceBase &self = const_cast(*this); return const_cast(self.getSecurityManager()); } +#endif // BLE_FEATURE_SECURITY #if BLE_FEATURE_PRIVACY ble::PrivateAddressController &BLEInstanceBase::getPrivateAddressRegistry() @@ -315,8 +315,7 @@ ble::PrivateAddressController &BLEInstanceBase::getPrivateAddressRegistry() ); return registry; } -#endif - +#endif // BLE_FEATURE_PRIVACY #endif // BLE_FEATURE_SECURITY void BLEInstanceBase::waitForEvent() diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp index 80bdea8ae1..0c6b963012 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp @@ -16,6 +16,7 @@ * limitations under the License. */ #include "source/PalGapImpl.h" +#include "ble/common/BLERoles.h" #include "hci_api.h" #include "dm_api.h" #include "dm_main.h" @@ -571,7 +572,9 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) } break; #endif // BLE_FEATURE_PHY_MANAGEMENT + #if BLE_FEATURE_PERIODIC_ADVERTISING +#if BLE_ROLE_OBSERVER case DM_PER_ADV_SYNC_EST_IND: { if (!handler) { break; @@ -619,8 +622,10 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) handler->on_periodic_advertising_sync_loss(evt->syncHandle); } break; +#endif // BLE_ROLE_OBSERVER #endif // BLE_FEATURE_PERIODIC_ADVERTISING +#if BLE_ROLE_BROADCASTER case DM_ADV_START_IND: if (!handler) { break; @@ -634,6 +639,7 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) } handler->on_legacy_advertising_stopped(); break; +#endif // BLE_ROLE_BROADCASTER #if BLE_FEATURE_EXTENDED_ADVERTISING && BLE_ROLE_BROADCASTER case DM_SCAN_REQ_RCVD_IND: { @@ -743,7 +749,7 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) #endif // BLE_FEATURE_EXTENDED_ADVERTISING #endif // BLE_ROLE_OBSERVER -#if BLE_ROLE_CENTRAL || BLE_ROLE_PERIPHERAL +#if BLE_FEATURE_CONNECTABLE case DM_REM_CONN_PARAM_REQ_IND: { if (!handler) { break; @@ -769,7 +775,7 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) get_gap().get_running_conn_direct_adv_cb(evt->hdr.param); if (adv_cb) { adv_cb->state = direct_adv_cb_t::free; - +#if BLE_ROLE_BROADCASTER if (handler) { handler->on_advertising_set_terminated( hci_error_code_t(evt->status), @@ -778,6 +784,7 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) 0 ); } +#endif // BLE_ROLE_BROADCASTER } } } @@ -794,7 +801,7 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) } } break; -#endif // BLE_ROLE_CENTRAL || BLE_ROLE_PERIPHERAL +#endif // BLE_FEATURE_CONNECTABLE } // all handlers are stored in a static array @@ -802,12 +809,12 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) #if BLE_ROLE_OBSERVER &event_handler, #endif // BLE_ROLE_OBSERVER -#if BLE_ROLE_CENTRAL || BLE_ROLE_PERIPHERAL +#if BLE_FEATURE_CONNECTABLE &event_handler, &event_handler, &event_handler, &event_handler, -#endif // BLE_ROLE_CENTRAL || BLE_ROLE_PERIPHERAL +#endif // BLE_FEATURE_CONNECTABLE &dummy_gap_event_handler }; @@ -846,7 +853,7 @@ ble_error_t PalGap::set_advertising_set_random_address( return BLE_ERROR_NONE; } - +#if BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t PalGap::set_extended_advertising_parameters( advertising_handle_t advertising_handle, advertising_event_properties_t event_properties, @@ -986,7 +993,7 @@ ble_error_t PalGap::set_extended_advertising_parameters( peer_address_type ); } - +#endif // BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t PalGap::set_periodic_advertising_parameters( advertising_handle_t advertising_handle, diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h index 9b4e99fbbc..7a46f15ddc 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h @@ -174,6 +174,7 @@ public: const address_t &address ) final; +#if BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t set_extended_advertising_parameters( advertising_handle_t advertising_handle, advertising_event_properties_t event_properties, @@ -191,6 +192,7 @@ public: uint8_t advertising_sid, bool scan_request_notification ) final; +#endif // BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t set_periodic_advertising_parameters( advertising_handle_t advertising_handle, diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp index 816fddd421..fda0aedebe 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.cpp @@ -15,6 +15,8 @@ * limitations under the License. */ +#if BLE_FEATURE_PRIVACY + #include "PalPrivateAddressControllerImpl.h" #include "dm_api.h" @@ -232,3 +234,4 @@ bool PalPrivateAddressController::cordio_handler(const wsfMsgHdr_t *msg) } // namespace impl } // namespace ble +#endif // BLE_FEATURE_PRIVACY diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.h index 6076f8cbee..564303bee1 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.h +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalPrivateAddressControllerImpl.h @@ -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 @@ -18,6 +18,8 @@ #ifndef BLE_CORDIO_PAL_PRIVATE_ADDRESS_CONTROLLER_IMPL_H #define BLE_CORDIO_PAL_PRIVATE_ADDRESS_CONTROLLER_IMPL_H +#if BLE_FEATURE_PRIVACY + #include "source/pal/PalPrivateAddressController.h" #include "dm_api.h" @@ -91,4 +93,6 @@ private: } // namespace impl } // namespace ble +#endif // BLE_FEATURE_PRIVACY + #endif //BLE_CORDIO_PAL_PRIVATE_ADDRESS_CONTROLLER_IMPL_H diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp index 42c8b668fe..ee76884a1a 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp @@ -16,6 +16,8 @@ * limitations under the License. */ +#if BLE_FEATURE_SECURITY + #include #include "ble/common/BLERoles.h" @@ -789,3 +791,5 @@ PalSecurityManagerEventHandler *PalSecurityManager::get_event_handler() } // namespace impl } // namespace ble + +#endif // BLE_FEATURE_SECURITY diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp index 04a857424c..79dff45e97 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp @@ -76,6 +76,7 @@ static bool is_in_range(T value, T lower_bound, T higher_bound) return true; } +#if BLE_FEATURE_CONNECTABLE /* * Return true of the connection parameters are acceptable as preferred connection * parameters. @@ -128,6 +129,7 @@ bool is_preferred_connection_params_valid(const Gap::PreferredConnectionParams_t return true; } +#endif // BLE_FEATURE_CONNECTABLE /** * Check if random bytes of an address are valid. @@ -231,6 +233,7 @@ bool is_random_address(const address_t &address) is_random_static_address(address); } +#if BLE_FEATURE_WHITELIST /* * Return true if the whitelist in input is valid or false otherwise. */ @@ -294,7 +297,9 @@ whitelist_address_type_t to_whitelist_address_type( whitelist_address_type_t::PUBLIC_DEVICE_ADDRESS : whitelist_address_type_t::RANDOM_DEVICE_ADDRESS; } +#endif // BLE_FEATURE_WHITELIST +#if BLE_FEATURE_CONNECTABLE microsecond_t minSupervisionTimeout( const conn_interval_t &maxConnectionInterval, const slave_latency_t &slaveLatency @@ -304,6 +309,7 @@ microsecond_t minSupervisionTimeout( // Section 7.8.12 LE Create Connection return (1 + slaveLatency.value()) * maxConnectionInterval * 2; } +#endif // BLE_FEATURE_CONNECTABLE bool is_connectable_advertising(const AdvertisingParameters ¶ms) { @@ -313,36 +319,49 @@ bool is_connectable_advertising(const AdvertisingParameters ¶ms) } // end of anonymous namespace +#if BLE_FEATURE_PRIVACY +#if BLE_ROLE_BROADCASTER const peripheral_privacy_configuration_t Gap::default_peripheral_privacy_configuration = { /* use_non_resolvable_random_address */ false, /* resolution_strategy */ peripheral_privacy_configuration_t::PERFORM_PAIRING_PROCEDURE }; +#endif // BLE_ROLE_BROADCASTER - +#if BLE_ROLE_OBSERVER const central_privacy_configuration_t Gap::default_central_privacy_configuration = { /* use_non_resolvable_random_address */ false, /* resolution_strategy */ central_privacy_configuration_t::RESOLVE_AND_FORWARD }; +#endif // BLE_ROLE_OBSERVER +#endif // BLE_FEATURE_PRIVACY Gap::Gap( ble::PalEventQueue &event_queue, ble::PalGap &pal_gap, - ble::PalGenericAccessService &generic_access_service, - ble::PalSecurityManager &pal_sm, - ble::PrivateAddressController &pal_addr_reg + ble::PalGenericAccessService &generic_access_service +#if BLE_FEATURE_PRIVACY + , ble::PrivateAddressController &pal_addr_reg +#endif //BLE_FEATURE_PRIVACY ) : _event_queue(event_queue), _pal_gap(pal_gap), _gap_service(generic_access_service), - _pal_sm(pal_sm), +#if BLE_FEATURE_PRIVACY _address_registry(pal_addr_reg), +#endif // BLE_FEATURE_PRIVACY _address_type(own_address_type_t::PUBLIC), _initiator_policy_mode(initiator_policy_t::NO_FILTER), _scanning_filter_policy(scanning_filter_policy_t::NO_FILTER), _advertising_filter_policy(advertising_filter_policy_t::NO_FILTER), _whitelist(), _privacy_enabled(false), +#if BLE_FEATURE_PRIVACY +#if BLE_ROLE_BROADCASTER _peripheral_privacy_configuration(default_peripheral_privacy_configuration), +#endif // BLE_ROLE_BROADCASTER +#if BLE_ROLE_OBSERVER _central_privacy_configuration(default_central_privacy_configuration), +#endif // BLE_ROLE_OBSERVER +#endif // BLE_FEATURE_PRIVACY _scan_enabled(false), _advertising_timeout(), _scan_timeout(), @@ -358,7 +377,9 @@ Gap::Gap( _random_static_identity_address = _pal_gap.get_random_address(); _pal_gap.set_event_handler(this); +#if BLE_FEATURE_PRIVACY _address_registry.set_event_handler(this); +#endif // BLE_FEATURE_PRIVACY } @@ -501,7 +522,7 @@ ble_error_t Gap::connect( if (_connect_to_host_resolved_address_state == ConnectionToHostResolvedAddressState::scan) { return BLE_ERROR_OPERATION_NOT_PERMITTED; } -#endif BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION if (!connectionParams.getNumberOfEnabledPhys()) { return BLE_ERROR_INVALID_PARAM; @@ -674,10 +695,7 @@ ble_error_t Gap::updateConnectionParameters( maxConnectionEventLength.value() ); } -#endif - -#if BLE_FEATURE_CONNECTABLE ble_error_t Gap::acceptConnectionParametersUpdate( connection_handle_t connectionHandle, conn_interval_t minConnectionInterval, @@ -702,10 +720,7 @@ ble_error_t Gap::acceptConnectionParametersUpdate( maxConnectionEventLength.value() ); } -#endif - -#if BLE_FEATURE_CONNECTABLE ble_error_t Gap::rejectConnectionParametersUpdate( connection_handle_t connectionHandle ) @@ -715,8 +730,7 @@ ble_error_t Gap::rejectConnectionParametersUpdate( hci_error_code_t::UNACCEPTABLE_CONNECTION_PARAMETERS ); } -#endif - +#endif // BLE_FEATURE_CONNECTABLE #if BLE_ROLE_CENTRAL ble_error_t Gap::cancelConnect() @@ -739,17 +753,14 @@ ble_error_t Gap::cancelConnect() } return ret; } -#endif +#endif // BLE_ROLE_CENTRAL #if BLE_FEATURE_PHY_MANAGEMENT ble_error_t Gap::readPhy(ble::connection_handle_t connection) { return _pal_gap.read_phy(connection); } -#endif - -#if BLE_FEATURE_PHY_MANAGEMENT ble_error_t Gap::setPreferredPhys( const phy_set_t *txPhys, const phy_set_t *rxPhys @@ -759,10 +770,7 @@ ble_error_t Gap::setPreferredPhys( phy_set_t rx_phys(rxPhys ? rxPhys->value() : 0); return _pal_gap.set_preferred_phys(tx_phys, rx_phys); } -#endif - -#if BLE_FEATURE_PHY_MANAGEMENT ble_error_t Gap::setPhy( ble::connection_handle_t connection, const phy_set_t *txPhys, @@ -774,8 +782,6 @@ ble_error_t Gap::setPhy( phy_set_t rx_phys(rxPhys ? rxPhys->value() : 0); return _pal_gap.set_phy(connection, tx_phys, rx_phys, codedSymbol); } -#endif - void Gap::on_read_phy( hci_error_code_t hci_status, @@ -793,7 +799,7 @@ void Gap::on_read_phy( _event_handler->onPhyUpdateComplete(status, connection_handle, tx_phy, rx_phy); } } - +#endif // BLE_FEATURE_PHY_MANAGEMENT void Gap::on_data_length_change( connection_handle_t connection_handle, @@ -806,7 +812,7 @@ void Gap::on_data_length_change( } } - +#if BLE_FEATURE_PHY_MANAGEMENT void Gap::on_phy_update_complete( hci_error_code_t hci_status, ble::connection_handle_t connection_handle, @@ -823,7 +829,7 @@ void Gap::on_phy_update_complete( _event_handler->onPhyUpdateComplete(status, connection_handle, tx_phy, rx_phy); } } - +#endif // BLE_FEATURE_PHY_MANAGEMENT #if BLE_FEATURE_CONNECTABLE ble_error_t Gap::disconnect( @@ -833,18 +839,14 @@ ble_error_t Gap::disconnect( { return _pal_gap.disconnect(connectionHandle, reason); } -#endif - +#endif // BLE_FEATURE_CONNECTABLE #if BLE_FEATURE_WHITELIST uint8_t Gap::getMaxWhitelistSize(void) const { return _pal_gap.read_white_list_capacity(); } -#endif - -#if BLE_FEATURE_WHITELIST ble_error_t Gap::getWhitelist(whitelist_t &whitelist) const { if (initialize_whitelist() == false) { @@ -862,10 +864,7 @@ ble_error_t Gap::getWhitelist(whitelist_t &whitelist) const whitelist.capacity = _whitelist.capacity; return BLE_ERROR_NONE; } -#endif - -#if BLE_FEATURE_WHITELIST ble_error_t Gap::setWhitelist(const whitelist_t &whitelist) { if (is_whitelist_valid(whitelist) == false) { @@ -957,8 +956,7 @@ ble_error_t Gap::setWhitelist(const whitelist_t &whitelist) return BLE_ERROR_NONE; } -#endif - +#endif // BLE_FEATURE_WHITELIST #if BLE_FEATURE_PRIVACY ble_error_t Gap::enablePrivacy(bool enable) @@ -1000,7 +998,7 @@ ble_error_t Gap::enablePrivacy(bool enable) return BLE_ERROR_NONE; } -#endif +#endif // BLE_FEATURE_PRIVACY #if BLE_FEATURE_PRIVACY @@ -1019,12 +1017,7 @@ ble_error_t Gap::setPeripheralPrivacyConfiguration( return BLE_ERROR_NONE; } -#endif -#endif - -#if BLE_FEATURE_PRIVACY -#if BLE_ROLE_BROADCASTER ble_error_t Gap::getPeripheralPrivacyConfiguration( peripheral_privacy_configuration_t *configuration ) @@ -1033,9 +1026,8 @@ ble_error_t Gap::getPeripheralPrivacyConfiguration( return BLE_ERROR_NONE; } -#endif -#endif - +#endif // BLE_ROLE_BROADCASTER +#endif // BLE_FEATURE_PRIVACY #if BLE_FEATURE_PRIVACY #if BLE_ROLE_OBSERVER @@ -1053,12 +1045,7 @@ ble_error_t Gap::setCentralPrivacyConfiguration( return BLE_ERROR_NONE; } -#endif -#endif - -#if BLE_FEATURE_PRIVACY -#if BLE_ROLE_OBSERVER ble_error_t Gap::getCentralPrivacyConfiguration( central_privacy_configuration_t *configuration ) @@ -1067,9 +1054,8 @@ ble_error_t Gap::getCentralPrivacyConfiguration( return BLE_ERROR_NONE; } -#endif -#endif - +#endif // BLE_ROLE_OBSERVER +#endif // BLE_FEATURE_PRIVACY ble_error_t Gap::reset() { @@ -1159,6 +1145,7 @@ Gap::GapShutdownCallbackChain_t &Gap::onShutdown() return shutdownCallChain; } +#if BLE_ROLE_OBSERVER void Gap::on_scan_started(bool success) { _scan_pending = false; @@ -1193,19 +1180,22 @@ void Gap::on_scan_stopped(bool success) wait_for_advertising_stop = false; restart_advertising = false; } -#endif +#endif // BLE_FEATURE_EXTENDED_ADVERTISING if (_scan_address_refresh && !wait_for_advertising_stop) { +#if BLE_ROLE_BROADCASTER if (restart_advertising) { _address_refresh_sets.clear(LEGACY_ADVERTISING_HANDLE); startAdvertising(LEGACY_ADVERTISING_HANDLE); _adv_started_from_refresh.set(LEGACY_ADVERTISING_HANDLE); } +#endif // BLE_ROLE_BROADCASTER _scan_address_refresh = false; startScan(); } } +#endif // BLE_ROLE_OBSERVER #if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void Gap::connecting_to_host_resolved_address_failed(bool inform_user) @@ -1234,6 +1224,7 @@ void Gap::connecting_to_host_resolved_address_failed(bool inform_user) } #endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#if BLE_ROLE_OBSERVER void Gap::on_scan_timeout() { if (!_scan_enabled) { @@ -1248,8 +1239,9 @@ void Gap::on_scan_timeout() _event_handler->onScanTimeout(ScanTimeoutEvent()); } } +#endif // BLE_ROLE_OBSERVER - +#if BLE_ROLE_OBSERVER void Gap::process_legacy_scan_timeout() { if (!_scan_enabled) { @@ -1263,14 +1255,14 @@ void Gap::process_legacy_scan_timeout() _event_handler->onScanTimeout(ScanTimeoutEvent()); } } +#endif // BLE_ROLE_OBSERVER - +#if BLE_ROLE_BROADCASTER void Gap::on_advertising_timeout() { _event_queue.post(mbed::callback(this, &Gap::process_advertising_timeout)); } - void Gap::process_advertising_timeout() { if (!_active_sets.get(LEGACY_ADVERTISING_HANDLE)) { @@ -1279,7 +1271,7 @@ void Gap::process_advertising_timeout() stopAdvertising(LEGACY_ADVERTISING_HANDLE); } - +#endif // BLE_ROLE_BROADCASTER void Gap::on_gap_event_received(const GapEvent &e) { @@ -1315,7 +1307,7 @@ void Gap::on_gap_event_received(const GapEvent &e) } } - +#if BLE_ROLE_OBSERVER void Gap::on_advertising_report(const GapAdvertisingReportEvent &e) { for (size_t i = 0; i < e.size(); ++i) { @@ -1373,8 +1365,9 @@ void Gap::on_advertising_report(const GapAdvertisingReportEvent &e) ); } } +#endif // BLE_ROLE_OBSERVER - +#if BLE_FEATURE_CONNECTABLE void Gap::on_connection_complete(const GapConnectionCompleteEvent &e) { if (e.role == connection_role_t::CENTRAL) { @@ -1435,7 +1428,6 @@ void Gap::on_connection_complete(const GapConnectionCompleteEvent &e) } } - void Gap::on_disconnection_complete(const GapDisconnectionCompleteEvent &e) { if (e.status == hci_error_code_t::SUCCESS) { @@ -1461,7 +1453,6 @@ void Gap::on_disconnection_complete(const GapDisconnectionCompleteEvent &e) } } - void Gap::on_connection_parameter_request(const GapRemoteConnectionParameterRequestEvent &e) { if (_user_manage_connection_parameter_requests) { @@ -1491,7 +1482,6 @@ void Gap::on_connection_parameter_request(const GapRemoteConnectionParameterRequ } } - void Gap::on_connection_update(const GapConnectionUpdateEvent &e) { if (!_event_handler) { @@ -1508,7 +1498,7 @@ void Gap::on_connection_update(const GapConnectionUpdateEvent &e) ) ); } - +#endif //BLE_FEATURE_CONNECTABLE void Gap::on_unexpected_error(const GapUnexpectedErrorEvent &e) { @@ -1516,7 +1506,6 @@ void Gap::on_unexpected_error(const GapUnexpectedErrorEvent &e) // has been updated. } - own_address_type_t Gap::get_own_address_type(AddressUseType_t address_use_type) { #if BLE_FEATURE_PRIVACY @@ -1555,7 +1544,7 @@ own_address_type_t Gap::get_own_address_type(AddressUseType_t address_use_type) } } - +#if BLE_FEATURE_WHITELIST bool Gap::initialize_whitelist() const { if (_whitelist.addresses != nullptr) { @@ -1578,6 +1567,7 @@ bool Gap::initialize_whitelist() const return true; } +#endif // BLE_FEATURE_WHITELIST #if BLE_FEATURE_PRIVACY && !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION ble_error_t Gap::update_ll_address_resolution_setting() @@ -1792,6 +1782,8 @@ ble_error_t Gap::setAdvertisingParameters( } #endif +#if BLE_ROLE_BROADCASTER +#if BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t Gap::setExtendedAdvertisingParameters( advertising_handle_t handle, const AdvertisingParameters ¶ms @@ -1841,7 +1833,8 @@ ble_error_t Gap::setExtendedAdvertisingParameters( return err; } - +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif // BLE_ROLE_BROADCASTER #if BLE_ROLE_BROADCASTER ble_error_t Gap::setAdvertisingPayload( @@ -1874,7 +1867,7 @@ ble_error_t Gap::setAdvertisingScanResponse( } #endif - +#if BLE_ROLE_BROADCASTER ble_error_t Gap::setAdvertisingData( advertising_handle_t handle, Span payload, @@ -1998,7 +1991,7 @@ ble_error_t Gap::setAdvertisingData( return BLE_ERROR_NONE; #endif // BLE_FEATURE_EXTENDED_ADVERTISING } - +#endif // BLE_ROLE_BROADCASTER #if BLE_ROLE_BROADCASTER ble_error_t Gap::startAdvertising( @@ -2028,12 +2021,14 @@ ble_error_t Gap::startAdvertising( MBED_WARNING(MBED_ERROR_INVALID_SIZE, "Payload size exceeds size allowed for connectable advertising."); return BLE_ERROR_INVALID_STATE; } +#endif // BLE_FEATURE_EXTENDED_ADVERTISING const address_t* random_address = get_random_address(controller_operation_t::advertising, handle); if (!random_address) { return BLE_ERROR_INVALID_STATE; } +#if BLE_FEATURE_EXTENDED_ADVERTISING if (is_extended_advertising_available()) { // Addresses can be updated if the set is not advertising if (!_active_sets.get(handle)) { @@ -2345,9 +2340,11 @@ bool Gap::isPeriodicAdvertisingActive(advertising_handle_t handle) return _active_periodic_sets.get(handle); } -#endif -#endif +#endif // BLE_FEATURE_PERIODIC_ADVERTISING +#endif // BLE_ROLE_BROADCASTER +#if BLE_ROLE_OBSERVER +#if BLE_FEATURE_EXTENDED_ADVERTISING void Gap::on_extended_advertising_report( advertising_event_t event_type, const connection_peer_address_type_t *address_type, @@ -2390,6 +2387,8 @@ void Gap::on_extended_advertising_report( event ); } +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif //BLE_ROLE_OBSERVER #if BLE_FEATURE_CONNECTABLE void Gap::report_internal_connection_complete(const ConnectionCompleteEvent& event) @@ -2600,7 +2599,8 @@ void Gap::signal_advertising_report( AdvertisingReportEvent& event ) { -#if BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#if BLE_FEATURE_PRIVACY +#if 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 */ @@ -2684,10 +2684,11 @@ void Gap::signal_advertising_report( is_random_private_resolvable_address(event.getPeerAddress())) { return; } +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#endif // BLE_FEATURE_PRIVACY _event_handler->onAdvertisingReport( event ); -#endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } #endif //BLE_ROLE_OBSERVER @@ -2699,8 +2700,6 @@ void Gap::conclude_signal_advertising_report_after_address_resolution( const address_t *identity_address ) { - - /* fix the report with the new address if there's an identity found */ if (identity_address) { const peer_address_type_t peer_address_type = (identity_address_type == target_peer_address_type_t::RANDOM) ? @@ -2735,6 +2734,8 @@ void Gap::conclude_signal_advertising_report_after_address_resolution( #endif // BLE_ROLE_OBSERVER #endif // BLE_FEATURE_PRIVACY && BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#if BLE_ROLE_OBSERVER +#if BLE_FEATURE_PERIODIC_ADVERTISING void Gap::on_periodic_advertising_sync_established( hci_error_code_t error, sync_handle_t sync_handle, @@ -2800,7 +2801,10 @@ void Gap::on_periodic_advertising_sync_loss(sync_handle_t sync_handle) PeriodicAdvertisingSyncLoss(sync_handle) ); } +#endif // BLE_FEATURE_PERIODIC_ADVERTISING +#endif // BLE_ROLE_OBSERVER +#if BLE_ROLE_BROADCASTER void Gap::on_legacy_advertising_started() { _active_sets.set(LEGACY_ADVERTISING_HANDLE); @@ -2828,10 +2832,12 @@ void Gap::on_legacy_advertising_stopped() _address_refresh_sets.clear(LEGACY_ADVERTISING_HANDLE); startAdvertising(LEGACY_ADVERTISING_HANDLE); _adv_started_from_refresh.set(LEGACY_ADVERTISING_HANDLE); +#if BLE_ROLE_OBSERVER if (restart_scan) { _scan_address_refresh = false; startScan(); } +#endif // BLE_ROLE_OBSERVER } else if (_event_handler) { _event_handler->onAdvertisingEnd(AdvertisingEndEvent()); } @@ -2884,7 +2890,6 @@ void Gap::on_advertising_set_terminated( ); } - void Gap::on_scan_request_received( advertising_handle_t advertising_handle, connection_peer_address_type_t scanner_address_type, @@ -2903,8 +2908,9 @@ void Gap::on_scan_request_received( ) ); } +#endif // BLE_ROLE_BROADCASTER - +#if BLE_FEATURE_CONNECTABLE void Gap::on_connection_update_complete( hci_error_code_t status, connection_handle_t connection_handle, @@ -2928,7 +2934,6 @@ void Gap::on_connection_update_complete( ); } - void Gap::on_remote_connection_parameter( connection_handle_t connection_handle, uint16_t connection_interval_min, @@ -2960,7 +2965,7 @@ void Gap::on_remote_connection_parameter( ); } } - +#endif // BLE_FEATURE_CONNECTABLE #if BLE_ROLE_OBSERVER ble_error_t Gap::setScanParameters(const ScanParameters ¶ms) @@ -3283,6 +3288,8 @@ bool Gap::is_extended_advertising_available() #endif // BLE_FEATURE_EXTENDED_ADVERTISING } +#if BLE_ROLE_BROADCASTER +#if BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t Gap::prepare_legacy_advertising_set(const AdvertisingParameters& parameters) { if (_existing_sets.get(LEGACY_ADVERTISING_HANDLE)) { @@ -3300,13 +3307,15 @@ ble_error_t Gap::prepare_legacy_advertising_set(const AdvertisingParameters& par return err; } +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif // BLE_ROLE_BROADCASTER void Gap::setEventHandler(Gap::EventHandler *handler) { _event_handler = handler; } - +#if BLE_FEATURE_PRIVACY void Gap::on_resolvable_private_addresses_generated(const address_t &address) { on_private_address_generated(true); @@ -3333,6 +3342,7 @@ void Gap::on_private_address_generated(bool connectable) } } +#if BLE_ROLE_BROADCASTER // refresh for address for all connectable advertising sets for (size_t i = 0; i < BLE_GAP_MAX_ADVERTISING_SETS; ++i) { if (!_pending_sets.get(i) && _active_sets.get(i) && @@ -3345,7 +3355,9 @@ void Gap::on_private_address_generated(bool connectable) _address_refresh_sets.set(i); } } +#endif // BLE_ROLE_BROADCASTER +#if BLE_ROLE_OBSERVER // refresh scanning address if (_scan_enabled && !_scan_pending && _scan_interruptible && !_central_privacy_configuration.use_non_resolvable_random_address == connectable @@ -3356,9 +3368,9 @@ void Gap::on_private_address_generated(bool connectable) } _scan_address_refresh = true; } +#endif // BLE_ROLE_OBSERVER } -#if BLE_FEATURE_PRIVACY #if BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION void Gap::on_address_resolution_completed( const address_t &peer_resolvable_address, @@ -3452,26 +3464,36 @@ void Gap::update_advertising_set_connectable_attribute( } } -// Note a call to this functions implies that the const address_t *Gap::get_random_address(controller_operation_t operation, size_t set_id) { - const auto &resolvable_address = _address_registry.get_resolvable_private_address(); - const auto &non_resolvable_address = _address_registry.get_non_resolvable_private_address(); - bool central_non_resolvable = _central_privacy_configuration.use_non_resolvable_random_address; - bool peripheral_non_resolvable = _peripheral_privacy_configuration.use_non_resolvable_random_address; - - const address_t *address_in_use = nullptr; - const address_t *desired_address = nullptr; - - // If privacy is not enabled, then the random address is always the random - // static address. - if (_privacy_enabled == false) { + // If privacy is not enabled, then the random address is always the random static address. +#if BLE_FEATURE_PRIVACY + if (_privacy_enabled == false) +#endif // BLE_FEATURE_PRIVACY + { return &_random_static_identity_address; } +#if BLE_FEATURE_PRIVACY + const auto &resolvable_address = _address_registry.get_resolvable_private_address(); + const auto &non_resolvable_address = _address_registry.get_non_resolvable_private_address(); + +#if BLE_ROLE_OBSERVER + bool central_non_resolvable = _central_privacy_configuration.use_non_resolvable_random_address; +#else + bool central_non_resolvable = false; +#endif // BLE_ROLE_OBSERVER +#if BLE_ROLE_BROADCASTER + bool peripheral_non_resolvable = _peripheral_privacy_configuration.use_non_resolvable_random_address; +#else + bool peripheral_non_resolvable = false; +#endif // BLE_ROLE_BROADCASTER + const address_t *address_in_use = nullptr; + const address_t *desired_address = nullptr; + bool advertising_use_main_address = true; - // Extended advertising is a special case as the address isn't shared with - // the main address. + + // Extended advertising is a special case as each set has its own address #if !CORDIO_GLOBAL_RANDOM_ADDRESS_FOR_CONNECTION #if BLE_FEATURE_EXTENDED_ADVERTISING if (is_extended_advertising_available()) { @@ -3488,7 +3510,6 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_ #endif #endif - // For other cases we first compute the address being used and then compares // it to the address to use to determine if the address is correct or not. if (_initiating) { @@ -3544,6 +3565,7 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_ } return desired_address; +#endif // BLE_FEATURE_PRIVACY } diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index 27c04826a2..dc0d5cec57 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -59,10 +59,15 @@ class BLEInstanceBase; class Gap : public ble::PalConnectionMonitor, - public PalGapEventHandler, - public PrivateAddressController::EventHandler { + public PalGapEventHandler +#if BLE_FEATURE_PRIVACY + , public PrivateAddressController::EventHandler +#endif //BLE_FEATURE_PRIVACY + { friend PalConnectionMonitor; +#if BLE_FEATURE_PRIVACY friend PalGapEventHandler; +#endif //BLE_FEATURE_PRIVACY friend PalGap; friend impl::BLEInstanceBase; @@ -72,17 +77,23 @@ class Gap : public: using PreferredConnectionParams_t = ::ble::Gap::PreferredConnectionParams_t ; +#if BLE_FEATURE_PRIVACY +#if BLE_ROLE_BROADCASTER /** * Default peripheral privacy configuration. */ static const peripheral_privacy_configuration_t default_peripheral_privacy_configuration; +#endif // BLE_ROLE_BROADCASTER +#if BLE_ROLE_OBSERVER /** * Default peripheral privacy configuration. */ static const central_privacy_configuration_t default_central_privacy_configuration; +#endif // BLE_ROLE_OBSERVER +#endif // BLE_FEATURE_PRIVACY public: void setEventHandler(EventHandler *handler); @@ -542,13 +553,15 @@ private: Gap( ble::PalEventQueue &event_queue, ble::PalGap &pal_gap, - ble::PalGenericAccessService &generic_access_service, - ble::PalSecurityManager &pal_sm, - ble::PrivateAddressController &pal_addr_reg + ble::PalGenericAccessService &generic_access_service +#if BLE_FEATURE_PRIVACY + , ble::PrivateAddressController &pal_addr_reg +#endif // BLE_FEATURE_PRIVACY ); ~Gap(); +#if BLE_ROLE_BROADCASTER ble_error_t setAdvertisingData( advertising_handle_t handle, Span payload, @@ -559,11 +572,15 @@ private: void on_advertising_timeout(); void process_advertising_timeout(); +#endif // BLE_ROLE_BROADCASTER void on_gap_event_received(const GapEvent &e); +#if BLE_ROLE_OBSERVER void on_advertising_report(const GapAdvertisingReportEvent &e); +#endif // BLE_ROLE_OBSERVER +#if BLE_FEATURE_CONNECTABLE void on_connection_complete(const GapConnectionCompleteEvent &e); void on_disconnection_complete(const GapDisconnectionCompleteEvent &e); @@ -573,6 +590,7 @@ private: ); void on_connection_update(const GapConnectionUpdateEvent &e); +#endif // BLE_FEATURE_CONNECTABLE void on_unexpected_error(const GapUnexpectedErrorEvent &e); @@ -585,20 +603,30 @@ private: own_address_type_t get_own_address_type(AddressUseType_t address_use_type); +#if BLE_FEATURE_WHITELIST bool initialize_whitelist() const; +#endif // BLE_FEATURE_WHITELIST #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 +#if BLE_ROLE_BROADCASTER +#if BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t setExtendedAdvertisingParameters( advertising_handle_t handle, const AdvertisingParameters ¶meters ); +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif // BLE_ROLE_BROADCASTER bool is_extended_advertising_available(); +#if BLE_ROLE_BROADCASTER +#if BLE_FEATURE_EXTENDED_ADVERTISING ble_error_t prepare_legacy_advertising_set(const AdvertisingParameters& parameters); +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif // BLE_ROLE_BROADCASTER #if BLE_FEATURE_CONNECTABLE /** Call the internal handlers that report to the security manager and GATT @@ -665,12 +693,14 @@ private: /* implements PalGap::EventHandler */ private: +#if BLE_FEATURE_PHY_MANAGEMENT void on_read_phy( hci_error_code_t hci_status, connection_handle_t connection_handle, phy_t tx_phy, phy_t rx_phy ) override; +#endif // BLE_FEATURE_PHY_MANAGEMENT void on_data_length_change( connection_handle_t connection_handle, @@ -678,13 +708,17 @@ private: uint16_t rx_size ) override; +#if BLE_FEATURE_PHY_MANAGEMENT void on_phy_update_complete( hci_error_code_t hci_status, connection_handle_t connection_handle, phy_t tx_phy, phy_t rx_phy ) override; +#endif // BLE_FEATURE_PHY_MANAGEMENT +#if BLE_ROLE_OBSERVER +#if BLE_FEATURE_EXTENDED_ADVERTISING void on_extended_advertising_report( advertising_event_t event_type, const connection_peer_address_type_t *address_type, @@ -700,7 +734,9 @@ private: uint8_t data_length, const uint8_t *data ) override; +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#if BLE_FEATURE_PERIODIC_ADVERTISING void on_periodic_advertising_sync_established( hci_error_code_t error, sync_handle_t sync_handle, @@ -722,7 +758,10 @@ private: ) override; void on_periodic_advertising_sync_loss(sync_handle_t sync_handle) override; +#endif // BLE_FEATURE_PERIODIC_ADVERTISING +#endif // BLE_ROLE_OBSERVER +#if BLE_ROLE_BROADCASTER void on_legacy_advertising_started() override; void on_legacy_advertising_stopped() override; @@ -741,7 +780,9 @@ private: connection_peer_address_type_t scanner_address_type, const ble::address_t &address ) override; +#endif // BLE_ROLE_BROADCASTER +#if BLE_FEATURE_CONNECTABLE void on_connection_update_complete( hci_error_code_t status, connection_handle_t connection_handle, @@ -757,7 +798,9 @@ private: uint16_t connection_latency, uint16_t supervision_timeout ) override; +#endif // BLE_FEATURE_CONNECTABLE +#if BLE_ROLE_OBSERVER void on_scan_started(bool success) override; void on_scan_stopped(bool success) override; @@ -765,7 +808,9 @@ private: void on_scan_timeout() override; void process_legacy_scan_timeout(); +#endif // BLE_ROLE_OBSERVER +#if BLE_FEATURE_PRIVACY /* Implement PrivateAddressController::EventHandler */ private: void on_resolvable_private_addresses_generated(const address_t &address) final; @@ -773,14 +818,16 @@ 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 + +#if 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 +#endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION +#endif // BLE_FEATURE_PRIVACY private: bool is_advertising() const; @@ -826,8 +873,9 @@ private: PalEventQueue &_event_queue; PalGap &_pal_gap; PalGenericAccessService &_gap_service; - PalSecurityManager &_pal_sm; +#if BLE_FEATURE_PRIVACY PrivateAddressController &_address_registry; +#endif // BLE_FEATURE_PRIVACY ble::own_address_type_t _address_type; initiator_policy_t _initiator_policy_mode; scanning_filter_policy_t _scanning_filter_policy; diff --git a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp index 7934d972e4..f1f9c70e5a 100644 --- a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp +++ b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.cpp @@ -15,6 +15,8 @@ * limitations under the License. */ +#if BLE_FEATURE_PRIVACY + #include "PrivateAddressController.h" namespace ble { @@ -739,3 +741,5 @@ void PrivateAddressController::add_resolution_entry_to_cache( #endif // BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION } // namespace ble + +#endif // BLE_FEATURE_PRIVACY diff --git a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h index b1a517ad23..e24de9d6c4 100644 --- a/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h +++ b/connectivity/FEATURE_BLE/source/generic/PrivateAddressController.h @@ -18,6 +18,8 @@ #ifndef BLE_GENERIC_PRIVATE_ADDRESS_CONTROLLER_H #define BLE_GENERIC_PRIVATE_ADDRESS_CONTROLLER_H +#if BLE_FEATURE_PRIVACY + #include #include "drivers/LowPowerTicker.h" @@ -344,4 +346,6 @@ private: } +#endif // BLE_FEATURE_PRIVACY + #endif //BLE_GENERIC_PRIVATE_ADDRESS_CONTROLLER_H diff --git a/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp b/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp index 02db71a9ad..a057f60e55 100644 --- a/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp @@ -16,6 +16,8 @@ * limitations under the License. */ +#if BLE_FEATURE_SECURITY + #include "ble/BLE.h" #include "ble/common/BLERoles.h" @@ -136,7 +138,7 @@ ble_error_t SecurityManager::init( // set the local identity address and irk if (result == BLE_ERROR_NONE) { result = init_identity(); - } + } #endif // BLE_FEATURE_PRIVACY if (result != BLE_ERROR_NONE) { @@ -739,7 +741,7 @@ ble_error_t SecurityManager::getSigningKey(connection_handle_t connection, bool // Privacy // - +#if BLE_FEATURE_PRIVACY ble_error_t SecurityManager::setPrivateAddressTimeout(uint16_t timeout_in_seconds) { if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE; @@ -748,6 +750,7 @@ ble_error_t SecurityManager::setPrivateAddressTimeout(uint16_t timeout_in_second ); return BLE_ERROR_NONE; } +#endif // BLE_FEATURE_PRIVACY //////////////////////////////////////////////////////////////////////////// // Authentication @@ -980,7 +983,7 @@ ble_error_t SecurityManager::init_database( return BLE_ERROR_NONE; } - +#if BLE_FEATURE_PRIVACY ble_error_t SecurityManager::init_resolving_list() { if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE; @@ -1006,8 +1009,9 @@ ble_error_t SecurityManager::init_resolving_list() return BLE_ERROR_NONE; } +#endif // BLE_FEATURE_PRIVACY - +#if BLE_FEATURE_SIGNING ble_error_t SecurityManager::init_signing() { if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE; @@ -1028,8 +1032,9 @@ ble_error_t SecurityManager::init_signing() return _pal.set_csrk(*pcsrk, local_sign_counter); } +#endif // BLE_FEATURE_SIGNING - +#if BLE_FEATURE_PRIVACY ble_error_t SecurityManager::init_identity() { if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE; @@ -1076,7 +1081,7 @@ ble_error_t SecurityManager::init_identity() } return err; } - +#endif // BLE_FEATURE_PRIVACY ble_error_t SecurityManager::get_random_data(uint8_t *buffer, size_t size) { @@ -1383,6 +1388,7 @@ void SecurityManager::on_identity_list_retrieved( { typedef advertising_peer_address_type_t address_type_t; +#if BLE_FEATURE_PRIVACY _private_address_controller.clear_resolving_list(); for (size_t i = 0; i < count; ++i) { _private_address_controller.add_device_to_resolving_list( @@ -1393,6 +1399,7 @@ void SecurityManager::on_identity_list_retrieved( identity_list[i].irk ); } +#endif // BLE_FEATURE_PRIVACY delete [] identity_list.data(); } @@ -2080,3 +2087,5 @@ void SecurityManager::setSecurityManagerEventHandler(EventHandler* handler) } /* namespace impl */ } /* namespace ble */ + +#endif // BLE_FEATURE_SECURITY diff --git a/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h b/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h index 576fd6ae92..a969b36dd9 100644 --- a/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h @@ -185,9 +185,11 @@ public: // Privacy // +#if BLE_FEATURE_PRIVACY ble_error_t setPrivateAddressTimeout( uint16_t timeout_in_seconds ); +#endif // BLE_FEATURE_PRIVACY /* Event callback handlers. */ public: @@ -451,14 +453,18 @@ public: SecurityManager( ble::PalSecurityManager &palImpl, ble::PalConnectionMonitor &connMonitorImpl, - ble::PalSigningMonitor &signingMonitorImpl, - PrivateAddressController &privateAddressController + ble::PalSigningMonitor &signingMonitorImpl +#if BLE_FEATURE_PRIVACY + , PrivateAddressController &privateAddressController +#endif // BLE_FEATURE_PRIVACY ) : eventHandler(nullptr), _pal(palImpl), _connection_monitor(connMonitorImpl), _signing_monitor(signingMonitorImpl), _db(nullptr), +#if BLE_FEATURE_PRIVACY _private_address_controller(privateAddressController), +#endif // BLE_FEATURE_PRIVACY _default_authentication(0), _default_key_distribution(KeyDistribution::KEY_DISTRIBUTION_ALL), _pairing_authorisation_required(false), @@ -493,11 +499,17 @@ private: ble_error_t init_database(const char *db_path = nullptr); +#if BLE_FEATURE_PRIVACY ble_error_t init_resolving_list(); +#endif // BLE_FEATURE_PRIVACY +#if BLE_FEATURE_SIGNING ble_error_t init_signing(); +#endif // BLE_FEATURE_SIGNING +#if BLE_FEATURE_PRIVACY ble_error_t init_identity(); +#endif // BLE_FEATURE_PRIVACY ble_error_t get_random_data( uint8_t *buffer, @@ -641,7 +653,9 @@ private: PalSigningMonitor &_signing_monitor; SecurityDb *_db; +#if BLE_FEATURE_PRIVACY PrivateAddressController &_private_address_controller; +#endif // BLE_FEATURE_PRIVACY /* OOB data */ address_t _oob_local_address; diff --git a/connectivity/FEATURE_BLE/source/pal/PalGap.h b/connectivity/FEATURE_BLE/source/pal/PalGap.h index ca538de892..e0caee3050 100644 --- a/connectivity/FEATURE_BLE/source/pal/PalGap.h +++ b/connectivity/FEATURE_BLE/source/pal/PalGap.h @@ -21,6 +21,7 @@ #include "platform/Callback.h" +#include "ble/common/BLERoles.h" #include "source/pal/GapTypes.h" #include "source/pal/GapEvents.h" #include "ble/common/blecommon.h" @@ -29,6 +30,8 @@ namespace ble { struct PalGapEventHandler { public: + +#if BLE_FEATURE_PHY_MANAGEMENT /** * @copydoc PalGap::EventHandler::onReadPhy */ @@ -38,6 +41,7 @@ public: ble::phy_t tx_phy, ble::phy_t rx_phy ) = 0; +#endif // BLE_FEATURE_PHY_MANAGEMENT /** * @copydoc PalGap::EventHandler::onDataLengthChange @@ -48,6 +52,7 @@ public: uint16_t rx_size ) = 0; +#if BLE_FEATURE_PHY_MANAGEMENT /** * @copydoc PalGap::EventHandler::onPhyUpdateComplete */ @@ -57,7 +62,10 @@ public: ble::phy_t tx_phy, ble::phy_t rx_phy ) = 0; +#endif // BLE_FEATURE_PHY_MANAGEMENT +#if BLE_FEATURE_EXTENDED_ADVERTISING +#if BLE_ROLE_OBSERVER /** Called on advertising report event. * * @param event_type Type of advertising used. @@ -92,7 +100,11 @@ public: uint8_t data_length, const uint8_t *data_size ) = 0; +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif // BLE_ROLE_OBSERVER +#if BLE_FEATURE_PERIODIC_ADVERTISING +#if BLE_ROLE_OBSERVER /** Called on advertising sync event. * * @param error SUCCESS if synchronisation was achieved. @@ -140,7 +152,10 @@ public: virtual void on_periodic_advertising_sync_loss( sync_handle_t sync_handle ) = 0; +#endif // BLE_FEATURE_PERIODIC_ADVERTISING +#endif // BLE_ROLE_OBSERVER +#if BLE_ROLE_OBSERVER /** * Called when scanning start */ @@ -155,7 +170,9 @@ public: /** Called when scanning times out. */ virtual void on_scan_timeout( ) = 0; +#endif // BLE_ROLE_OBSERVER +#if BLE_ROLE_BROADCASTER /** * Called when legacy advertising has been effectively started. */ @@ -196,7 +213,9 @@ public: connection_peer_address_type_t scanner_address_type, const address_t &address ) = 0; +#endif // BLE_ROLE_BROADCASTER +#if BLE_FEATURE_CONNECTABLE virtual void on_connection_update_complete( hci_error_code_t status, connection_handle_t connection_handle, @@ -212,6 +231,7 @@ public: uint16_t connection_latency, uint16_t supervision_timeout ) = 0; +#endif // BLE_FEATURE_CONNECTABLE }; /** @@ -375,6 +395,7 @@ public: advertising_filter_policy_t advertising_filter_policy ) = 0; +#if BLE_FEATURE_EXTENDED_ADVERTISING /** * Define the advertising parameters of an advertising set. * @@ -451,6 +472,7 @@ public: uint8_t advertising_sid, bool scan_request_notification ) = 0; +#endif // BLE_FEATURE_EXTENDED_ADVERTISING /** * Configure periodic advertising parameters of an advertising set. diff --git a/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h b/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h index 657adf9e47..0f286181d5 100644 --- a/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h +++ b/connectivity/FEATURE_BLE/source/pal/PalPrivateAddressController.h @@ -18,6 +18,8 @@ #ifndef BLE_PAL_PRIVATE_ADDRESS_CONTROLLER_H #define BLE_PAL_PRIVATE_ADDRESS_CONTROLLER_H +#if BLE_FEATURE_PRIVACY + #include "ble/common/BLETypes.h" #include "ble/common/blecommon.h" #include "GapTypes.h" @@ -244,5 +246,6 @@ struct PalPrivateAddressController { } // namespace ble +#endif // BLE_FEATURE_PRIVACY #endif //BLE_PAL_PRIVATE_ADDRESS_CONTROLLER_H From 9f09bb49560c5adf3f5b9772dfd51beb4610b8dc Mon Sep 17 00:00:00 2001 From: Paul Szczeanek Date: Tue, 13 Oct 2020 17:40:37 +0100 Subject: [PATCH 2/3] rename private address controller to match security manager --- .../FEATURE_BLE/source/generic/GapImpl.cpp | 46 +++++++++---------- .../FEATURE_BLE/source/generic/GapImpl.h | 4 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp index 79dff45e97..3fc2c045df 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp @@ -340,13 +340,13 @@ Gap::Gap( ble::PalGap &pal_gap, ble::PalGenericAccessService &generic_access_service #if BLE_FEATURE_PRIVACY - , ble::PrivateAddressController &pal_addr_reg + , ble::PrivateAddressController &private_address_controller #endif //BLE_FEATURE_PRIVACY ) : _event_queue(event_queue), _pal_gap(pal_gap), _gap_service(generic_access_service), #if BLE_FEATURE_PRIVACY - _address_registry(pal_addr_reg), + _private_address_controller(private_address_controller), #endif // BLE_FEATURE_PRIVACY _address_type(own_address_type_t::PUBLIC), _initiator_policy_mode(initiator_policy_t::NO_FILTER), @@ -378,7 +378,7 @@ Gap::Gap( _pal_gap.set_event_handler(this); #if BLE_FEATURE_PRIVACY - _address_registry.set_event_handler(this); + _private_address_controller.set_event_handler(this); #endif // BLE_FEATURE_PRIVACY } @@ -973,9 +973,9 @@ ble_error_t Gap::enablePrivacy(bool enable) _privacy_enabled = enable; if (_privacy_enabled) { - _address_registry.start_private_address_generation(); - if (_address_registry.get_non_resolvable_private_address() != address_t {} && - _address_registry.get_resolvable_private_address() != address_t{} + _private_address_controller.start_private_address_generation(); + if (_private_address_controller.get_non_resolvable_private_address() != address_t {} && + _private_address_controller.get_resolvable_private_address() != address_t{} ) { _event_queue.post([this] { if (_event_handler) { @@ -986,12 +986,12 @@ ble_error_t Gap::enablePrivacy(bool enable) _privacy_initialization_pending = true; } } else { - _address_registry.stop_private_address_generation(); + _private_address_controller.stop_private_address_generation(); _privacy_initialization_pending = false; } #if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION - if (_address_registry.is_controller_privacy_supported()) { + if (_private_address_controller.is_controller_privacy_supported()) { update_ll_address_resolution_setting(); } #endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION @@ -1010,7 +1010,7 @@ ble_error_t Gap::setPeripheralPrivacyConfiguration( _peripheral_privacy_configuration = *configuration; #if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION - if (_address_registry.is_controller_privacy_supported()) { + if (_private_address_controller.is_controller_privacy_supported()) { update_ll_address_resolution_setting(); } #endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION @@ -1038,7 +1038,7 @@ ble_error_t Gap::setCentralPrivacyConfiguration( _central_privacy_configuration = *configuration; #if !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION - if (_address_registry.is_controller_privacy_supported()) { + if (_private_address_controller.is_controller_privacy_supported()) { update_ll_address_resolution_setting(); } #endif // !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION @@ -1589,7 +1589,7 @@ ble_error_t Gap::update_ll_address_resolution_setting() #endif // BLE_ROLE_OBSERVER } - return _address_registry.enable_controller_address_resolution(enable); + return _private_address_controller.enable_controller_address_resolution(enable); } #endif // BLE_FEATURE_PRIVACY && !BLE_GAP_HOST_BASED_PRIVATE_ADDRESS_RESOLUTION @@ -2451,7 +2451,7 @@ void Gap::signal_connection_complete( /* first try to resolve synchronously in cache */ if (!address_resolved) { - address_resolved = _address_registry.resolve_address_in_host_cache( + address_resolved = _private_address_controller.resolve_address_in_host_cache( event.getPeerAddress(), &peer_address_type, &peer_address @@ -2476,7 +2476,7 @@ void Gap::signal_connection_complete( _event_handler->onConnectionComplete(event); } else { bool resolution_pending = false; - ble_error_t ret = _address_registry.queue_resolve_address_on_host(event.getPeerAddress()); + ble_error_t ret = _private_address_controller.queue_resolve_address_on_host(event.getPeerAddress()); if (ret == BLE_ERROR_NONE) { ConnectionCompleteEvent* event_copy = new(std::nothrow) ConnectionCompleteEvent(event); @@ -2532,7 +2532,7 @@ bool Gap::apply_peripheral_privacy_connection_policy( switch (_peripheral_privacy_configuration.resolution_strategy) { case peripheral_privacy_configuration_t::REJECT_NON_RESOLVED_ADDRESS: /* if there is no bond then allow unresolved addresses */ - if (_address_registry.read_resolving_list_size() == 0) { + if (_private_address_controller.read_resolving_list_size() == 0) { return true; } _pal_gap.disconnect( @@ -2576,7 +2576,7 @@ void Gap::conclude_signal_connection_complete_after_address_resolution( if (identity_address) { /* move old address to resolvable address */ event.setPeerResolvablePrivateAddress(event.getPeerAddress()); - event.setLocalResolvablePrivateAddress(_address_registry.get_resolvable_private_address()); + event.setLocalResolvablePrivateAddress(_private_address_controller.get_resolvable_private_address()); event.setPeerAddress(*identity_address); event.setPeerAddressType(identity_address_type == target_peer_address_type_t::RANDOM ? @@ -2616,7 +2616,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_host_cache( + address_resolved = _private_address_controller.resolve_address_in_host_cache( event.getPeerAddress(), &peer_address_type, &peer_address @@ -2663,7 +2663,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_on_host(event.getPeerAddress()); + ret = _private_address_controller.queue_resolve_address_on_host(event.getPeerAddress()); } if (ret == BLE_ERROR_NONE) { @@ -2678,7 +2678,7 @@ void Gap::signal_advertising_report( } #else /* filter out unresolved address if at least one bond exists */ - if (_address_registry.read_resolving_list_size() > 0 && + if (_private_address_controller.read_resolving_list_size() > 0 && _central_privacy_configuration.resolution_strategy == central_privacy_configuration_t::RESOLVE_AND_FILTER && event.getPeerAddressType() != peer_address_type_t::PUBLIC && is_random_private_resolvable_address(event.getPeerAddress())) { @@ -2724,7 +2724,7 @@ void Gap::conclude_signal_advertising_report_after_address_resolution( event.setPeerAddressType(peer_address_type); } else if (_central_privacy_configuration.resolution_strategy == central_privacy_configuration_t::RESOLVE_AND_FILTER && - _address_registry.read_resolving_list_size() > 0) { + _private_address_controller.read_resolving_list_size() > 0) { /* filter out unresolved address if at least one bond exists */ return; } @@ -3333,8 +3333,8 @@ void Gap::on_private_address_generated(bool connectable) } if (_privacy_initialization_pending && - _address_registry.get_resolvable_private_address() != address_t{} && - _address_registry.get_non_resolvable_private_address() != address_t{} + _private_address_controller.get_resolvable_private_address() != address_t{} && + _private_address_controller.get_non_resolvable_private_address() != address_t{} ) { _privacy_initialization_pending = false; if (_event_handler) { @@ -3475,8 +3475,8 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_ } #if BLE_FEATURE_PRIVACY - const auto &resolvable_address = _address_registry.get_resolvable_private_address(); - const auto &non_resolvable_address = _address_registry.get_non_resolvable_private_address(); + const auto &resolvable_address = _private_address_controller.get_resolvable_private_address(); + const auto &non_resolvable_address = _private_address_controller.get_non_resolvable_private_address(); #if BLE_ROLE_OBSERVER bool central_non_resolvable = _central_privacy_configuration.use_non_resolvable_random_address; diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index dc0d5cec57..59dc5f1c19 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -555,7 +555,7 @@ private: ble::PalGap &pal_gap, ble::PalGenericAccessService &generic_access_service #if BLE_FEATURE_PRIVACY - , ble::PrivateAddressController &pal_addr_reg + , ble::PrivateAddressController &private_address_controller #endif // BLE_FEATURE_PRIVACY ); @@ -874,7 +874,7 @@ private: PalGap &_pal_gap; PalGenericAccessService &_gap_service; #if BLE_FEATURE_PRIVACY - PrivateAddressController &_address_registry; + PrivateAddressController &_private_address_controller; #endif // BLE_FEATURE_PRIVACY ble::own_address_type_t _address_type; initiator_policy_t _initiator_policy_mode; From 119d4593616b14bed12a7018d8129aeac942e934 Mon Sep 17 00:00:00 2001 From: Paul Szczeanek Date: Wed, 14 Oct 2020 09:58:11 +0100 Subject: [PATCH 3/3] fix removed friend function --- connectivity/FEATURE_BLE/source/generic/GapImpl.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index 59dc5f1c19..91ec155a84 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -65,9 +65,7 @@ class Gap : #endif //BLE_FEATURE_PRIVACY { friend PalConnectionMonitor; -#if BLE_FEATURE_PRIVACY friend PalGapEventHandler; -#endif //BLE_FEATURE_PRIVACY friend PalGap; friend impl::BLEInstanceBase;