From e97582bd23cd128416c16dabf96b35420dcbb8bf Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Wed, 25 Mar 2020 14:58:02 +0000 Subject: [PATCH] Move BLE whitelist APIs from LegacyGap to ble::Gap --- features/FEATURE_BLE/ble/BLETypes.h | 22 ++++++ features/FEATURE_BLE/ble/Gap.h | 71 ------------------- features/FEATURE_BLE/ble/gap/Gap.h | 46 +++++++++++- features/FEATURE_BLE/source/LegacyGap.tpp | 32 --------- features/FEATURE_BLE/source/gap/Gap.tpp | 18 +++++ .../FEATURE_BLE/source/generic/GenericGap.tpp | 8 +-- .../source/generic/GenericSecurityManager.tpp | 2 +- 7 files changed, 90 insertions(+), 109 deletions(-) diff --git a/features/FEATURE_BLE/ble/BLETypes.h b/features/FEATURE_BLE/ble/BLETypes.h index 6759fefb96..859d8853f7 100644 --- a/features/FEATURE_BLE/ble/BLETypes.h +++ b/features/FEATURE_BLE/ble/BLETypes.h @@ -21,6 +21,7 @@ #include #include #include "ble/SafeEnum.h" +#include "ble/BLEProtocol.h" #include "platform/Span.h" #include "ble/gap/Types.h" @@ -831,6 +832,27 @@ struct coded_symbol_per_bit_t :SafeEnum { SafeEnum(value) { } }; + +/** + * Representation of a whitelist of addresses. + */ +struct whitelist_t { + /** + * Pointer to the array of the addresses composing the whitelist. + */ + BLEProtocol::Address_t *addresses; + + /** + * Number addresses in this whitelist. + */ + uint8_t size; + + /** + * Capacity of the array holding the addresses. + */ + uint8_t capacity; +}; + } // namespace ble /** diff --git a/features/FEATURE_BLE/ble/Gap.h b/features/FEATURE_BLE/ble/Gap.h index 15c5967ddb..750321f329 100644 --- a/features/FEATURE_BLE/ble/Gap.h +++ b/features/FEATURE_BLE/ble/Gap.h @@ -221,26 +221,6 @@ public: INIT_POLICY_FILTER_ALL_ADV = 1, }; - /** - * Representation of a whitelist of addresses. - */ - struct Whitelist_t { - /** - * Pointer to the array of the addresses composing the whitelist. - */ - BLEProtocol::Address_t *addresses; - - /** - * Number addresses in this whitelist. - */ - uint8_t size; - - /** - * Capacity of the array holding the addresses. - */ - uint8_t capacity; - }; - /** * Description of the states of the device. */ @@ -804,51 +784,6 @@ public: ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP); #endif // BLE_FEATURE_GATT_SERVER -#if BLE_FEATURE_WHITELIST - /** - * Get the maximum size of the whitelist. - * - * @return Maximum size of the whitelist. - * - * @note If using Mbed OS, you can configure the size of the whitelist by - * setting the YOTTA_CFG_WHITELIST_MAX_SIZE macro in your yotta config file. - */ - uint8_t getMaxWhitelistSize(void) const; - - /** - * Get the Link Layer to use the internal whitelist when scanning, - * advertising or initiating a connection depending on the filter policies. - * - * @param[in,out] whitelist Define the whitelist instance which is used - * to store the whitelist requested. In input, the caller provisions memory. - * - * @return BLE_ERROR_NONE if the implementation's whitelist was successfully - * copied into the supplied reference. - */ - ble_error_t getWhitelist(Whitelist_t &whitelist) const; - - /** - * Set the value of the whitelist to be used during GAP procedures. - * - * @param[in] whitelist A reference to a whitelist containing the addresses - * to be copied to the internal whitelist. - * - * @return BLE_ERROR_NONE if the implementation's whitelist was successfully - * populated with the addresses in the given whitelist. - * - * @note The whitelist must not contain addresses of type @ref - * BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE. This - * results in a @ref BLE_ERROR_INVALID_PARAM because the remote peer might - * change its private address at any time, and it is not possible to resolve - * it. - * - * @note If the input whitelist is larger than @ref getMaxWhitelistSize(), - * then @ref BLE_ERROR_PARAM_OUT_OF_RANGE is returned. - */ - ble_error_t setWhitelist(const Whitelist_t &whitelist); - -#endif // BLE_FEATURE_WHITELIST - /* * APIs with nonvirtual implementations. */ @@ -1130,12 +1065,6 @@ protected: /* --- Abstract calls to override --- */ - uint8_t getMaxWhitelistSize_(void) const; - - ble_error_t getWhitelist_(Whitelist_t &whitelist) const; - - ble_error_t setWhitelist_(const Whitelist_t &whitelist); - ble_error_t getAddress_( BLEProtocol::AddressType_t *typeP, BLEProtocol::AddressBytes_t address diff --git a/features/FEATURE_BLE/ble/gap/Gap.h b/features/FEATURE_BLE/ble/gap/Gap.h index 3c3d4d9add..877b8cebb4 100644 --- a/features/FEATURE_BLE/ble/gap/Gap.h +++ b/features/FEATURE_BLE/ble/gap/Gap.h @@ -20,7 +20,6 @@ #include "BLERoles.h" #include "ble/common/StaticInterface.h" #include "ble/BLETypes.h" -#include "ble/BLEProtocol.h" #include "ble/gap/AdvertisingDataBuilder.h" #include "ble/gap/AdvertisingDataSimpleBuilder.h" #include "ble/gap/ConnectionParameters.h" @@ -1256,6 +1255,48 @@ public: #endif // BLE_ROLE_OBSERVER #endif // BLE_FEATURE_PRIVACY +#if BLE_FEATURE_WHITELIST + /** + * Get the maximum size of the whitelist. + * + * @return Maximum size of the whitelist. + */ + uint8_t getMaxWhitelistSize(void) const; + + /** + * Get the Link Layer to use the internal whitelist when scanning, + * advertising or initiating a connection depending on the filter policies. + * + * @param[in,out] whitelist Define the whitelist instance which is used + * to store the whitelist requested. In input, the caller provisions memory. + * + * @return BLE_ERROR_NONE if the implementation's whitelist was successfully + * copied into the supplied reference. + */ + ble_error_t getWhitelist(whitelist_t &whitelist) const; + + /** + * Set the value of the whitelist to be used during GAP procedures. + * + * @param[in] whitelist A reference to a whitelist containing the addresses + * to be copied to the internal whitelist. + * + * @return BLE_ERROR_NONE if the implementation's whitelist was successfully + * populated with the addresses in the given whitelist. + * + * @note The whitelist must not contain addresses of type @ref + * BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE. This + * results in a @ref BLE_ERROR_INVALID_PARAM because the remote peer might + * change its private address at any time, and it is not possible to resolve + * it. + * + * @note If the input whitelist is larger than @ref getMaxWhitelistSize(), + * then @ref BLE_ERROR_PARAM_OUT_OF_RANGE is returned. + */ + ble_error_t setWhitelist(const whitelist_t &whitelist); + +#endif // BLE_FEATURE_WHITELIST + #if !defined(DOXYGEN_ONLY) /* * API reserved for the controller driver to set the random static address. @@ -1406,6 +1447,9 @@ protected: central_privay_configuration_t *configuration ); ble_error_t setRandomStaticAddress_(const ble::address_t& address); + uint8_t getMaxWhitelistSize_(void) const; + ble_error_t getWhitelist_(whitelist_t &whitelist) const; + ble_error_t setWhitelist_(const whitelist_t &whitelist); protected: /** diff --git a/features/FEATURE_BLE/source/LegacyGap.tpp b/features/FEATURE_BLE/source/LegacyGap.tpp index e1376adccc..b3db2834d6 100644 --- a/features/FEATURE_BLE/source/LegacyGap.tpp +++ b/features/FEATURE_BLE/source/LegacyGap.tpp @@ -19,23 +19,6 @@ namespace ble { namespace interface { -#if BLE_FEATURE_WHITELIST -template -uint8_t LegacyGap::getMaxWhitelistSize(void) const { - return impl()->getMaxWhitelistSize_(); -} - -template -ble_error_t LegacyGap::getWhitelist(Whitelist_t &whitelist) const { - return impl()->getWhitelist_(whitelist); -} - -template -ble_error_t LegacyGap::setWhitelist(const Whitelist_t &whitelist) { - return impl()->setWhitelist_(whitelist); -} -#endif // BLE_FEATURE_WHITELIST - template void LegacyGap::processConnectionEvent( Handle_t handle, @@ -358,21 +341,6 @@ ble_error_t LegacyGap::reset_(void) /* ------------------------- Default implementations ------------------------ */ // ----------------------------------------------------------------------------- -template -uint8_t LegacyGap::getMaxWhitelistSize_(void) const { - return 0; -} - -template -ble_error_t LegacyGap::getWhitelist_(Whitelist_t &whitelist) const { - return BLE_ERROR_NOT_IMPLEMENTED; -} - -template -ble_error_t LegacyGap::setWhitelist_(const Whitelist_t &whitelist) { - return BLE_ERROR_NOT_IMPLEMENTED; -} - template ble_error_t LegacyGap::getAddress_( BLEProtocol::AddressType_t *typeP, diff --git a/features/FEATURE_BLE/source/gap/Gap.tpp b/features/FEATURE_BLE/source/gap/Gap.tpp index c4481edb39..f285b20b4c 100644 --- a/features/FEATURE_BLE/source/gap/Gap.tpp +++ b/features/FEATURE_BLE/source/gap/Gap.tpp @@ -488,6 +488,24 @@ ble_error_t Gap::setRandomStaticAddress(const ble::address_t& address) return impl()->setRandomStaticAddress_(address); } + +#if BLE_FEATURE_WHITELIST +template +uint8_t Gap::getMaxWhitelistSize(void) const { + return impl()->getMaxWhitelistSize_(); +} + +template +ble_error_t Gap::getWhitelist(whitelist_t &whitelist) const { + return impl()->getWhitelist_(whitelist); +} + +template +ble_error_t Gap::setWhitelist(const whitelist_t &whitelist) { + return impl()->setWhitelist_(whitelist); +} +#endif // BLE_FEATURE_WHITELIST + // ----------------------------------------------------------------------------- /* ------------------------- Default implementations ------------------------ */ // ----------------------------------------------------------------------------- diff --git a/features/FEATURE_BLE/source/generic/GenericGap.tpp b/features/FEATURE_BLE/source/generic/GenericGap.tpp index 8e6228a168..e4bc34047b 100644 --- a/features/FEATURE_BLE/source/generic/GenericGap.tpp +++ b/features/FEATURE_BLE/source/generic/GenericGap.tpp @@ -320,7 +320,7 @@ static bool is_disconnection_reason_valid(::Gap::DisconnectionReason_t reason) /* * Return true if the whitelist in input is valid or false otherwise. */ -static bool is_whitelist_valid(const ::Gap::Whitelist_t &whitelist) +static bool is_whitelist_valid(const ::ble::whitelist_t &whitelist) { if (whitelist.size > whitelist.capacity) { return false; @@ -347,7 +347,7 @@ static bool is_whitelist_valid(const ::Gap::Whitelist_t &whitelist) * Return true if device is present in the whitelist. */ static bool is_in_whitelist( - const BLEProtocol::Address_t &device, const ::Gap::Whitelist_t &whitelist + const BLEProtocol::Address_t &device, const ::ble::whitelist_t &whitelist ) { for (size_t i = 0; i < whitelist.size; ++i) { @@ -890,7 +890,7 @@ uint8_t GenericGap class PalGapImpl, class PalSecurityManager, class ConnectionEventMonitorEventHandler> -ble_error_t GenericGap::getWhitelist_(Whitelist_t &whitelist) const +ble_error_t GenericGap::getWhitelist_(whitelist_t &whitelist) const { if (initialize_whitelist() == false) { return BLE_ERROR_INVALID_STATE; @@ -909,7 +909,7 @@ ble_error_t GenericGap class PalGapImpl, class PalSecurityManager, class ConnectionEventMonitorEventHandler> -ble_error_t GenericGap::setWhitelist_(const Whitelist_t &whitelist) +ble_error_t GenericGap::setWhitelist_(const whitelist_t &whitelist) { if (is_whitelist_valid(whitelist) == false) { return BLE_ERROR_INVALID_PARAM; diff --git a/features/FEATURE_BLE/source/generic/GenericSecurityManager.tpp b/features/FEATURE_BLE/source/generic/GenericSecurityManager.tpp index 08b60471af..d154cdf7c6 100644 --- a/features/FEATURE_BLE/source/generic/GenericSecurityManager.tpp +++ b/features/FEATURE_BLE/source/generic/GenericSecurityManager.tpp @@ -165,7 +165,7 @@ ble_error_t GenericSecurityManager::purgeAl } template class TPalSecurityManager, template class SigningMonitor> -ble_error_t GenericSecurityManager::generateWhitelistFromBondTable_(::Gap::Whitelist_t *whitelist) const { +ble_error_t GenericSecurityManager::generateWhitelistFromBondTable_(::ble::whitelist_t *whitelist) const { if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE; if (eventHandler) { if (!whitelist) {