diff --git a/features/FEATURE_BLE/ble/gap/Gap.h b/features/FEATURE_BLE/ble/gap/Gap.h index ce70d5aeaf..add46609bd 100644 --- a/features/FEATURE_BLE/ble/gap/Gap.h +++ b/features/FEATURE_BLE/ble/gap/Gap.h @@ -1257,6 +1257,13 @@ public: #endif // BLE_FEATURE_PRIVACY #if !defined(DOXYGEN_ONLY) + /* + * API reserverved for the controller driver to set the random static address. + * Setting a new random static address while the controller is operating is + * forbidden by the Bluetooth specification. + */ + ble_error_t setRandomStaticAddress(const ble::address_t& address); + protected: /** Can only be called if use_non_deprecated_scan_api() hasn't been called. * This guards against mixed use of deprecated and nondeprecated API. @@ -1407,6 +1414,7 @@ protected: ble_error_t getCentralPrivacyConfiguration_( central_privay_configuration_t *configuration ); + ble_error_t setRandomStaticAddress_(const ble::address_t& address); void useVersionOneAPI_() const; void useVersionTwoAPI_() const; diff --git a/features/FEATURE_BLE/ble/generic/GenericGap.h b/features/FEATURE_BLE/ble/generic/GenericGap.h index c195aa83b8..d0c8c7ca15 100644 --- a/features/FEATURE_BLE/ble/generic/GenericGap.h +++ b/features/FEATURE_BLE/ble/generic/GenericGap.h @@ -306,6 +306,11 @@ public: const BLEProtocol::AddressBytes_t address ); + /** + * @see Gap::setRandomStaticAddress + */ + ble_error_t setRandomStaticAddress_(const ble::address_t& address); + /** * @see Gap::getAddress */ diff --git a/features/FEATURE_BLE/source/gap/Gap.tpp b/features/FEATURE_BLE/source/gap/Gap.tpp index 9526eb51bd..4053c3f771 100644 --- a/features/FEATURE_BLE/source/gap/Gap.tpp +++ b/features/FEATURE_BLE/source/gap/Gap.tpp @@ -494,6 +494,12 @@ ble_error_t Gap::getCentralPrivacyConfiguration( #endif // BLE_ROLE_OBSERVER #endif // BLE_FEATURE_PRIVACY +template +ble_error_t Gap::setRandomStaticAddress(const ble::address_t& address) +{ + return impl()->setRandomStaticAddress_(address); +} + // ----------------------------------------------------------------------------- /* ------------------------- Default implementations ------------------------ */ // ----------------------------------------------------------------------------- @@ -861,6 +867,12 @@ ble_error_t Gap::getCentralPrivacyConfiguration_( return BLE_ERROR_NOT_IMPLEMENTED; } +template +ble_error_t Gap::setRandomStaticAddress_(const ble::address_t& address) +{ + return BLE_ERROR_NOT_IMPLEMENTED; +} + } // namespace interface } // namespace ble diff --git a/features/FEATURE_BLE/source/generic/GenericGap.tpp b/features/FEATURE_BLE/source/generic/GenericGap.tpp index 1d22d59035..7f60b9f6f8 100644 --- a/features/FEATURE_BLE/source/generic/GenericGap.tpp +++ b/features/FEATURE_BLE/source/generic/GenericGap.tpp @@ -510,6 +510,26 @@ ble_error_t GenericGap class PalGapImpl, class PalSecurityManager, class ConnectionEventMonitorEventHandler> +ble_error_t GenericGap::setRandomStaticAddress_( + const ble::address_t& address +) +{ + if (is_random_static_address(address.data()) == false) { + return BLE_ERROR_INVALID_PARAM; + } + + ble_error_t err = _pal_gap.set_random_address(address); + if (err) { + return err; + } + + _address_type = LegacyAddressType::RANDOM_STATIC; + _address = address; + _random_static_identity_address = address; + return BLE_ERROR_NONE; +} + template class PalGapImpl, class PalSecurityManager, class ConnectionEventMonitorEventHandler> ble_error_t GenericGap::getAddress_( LegacyAddressType_t *type, diff --git a/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp b/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp index d166424efa..bdca48f634 100644 --- a/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp +++ b/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp @@ -96,6 +96,13 @@ buf_pool_desc_t CordioHCIDriver::get_default_buffer_pool_description() return buf_pool_desc_t(buffer, pool_desc); } +void CordioHCIDriver::set_random_static_address(const ble::address_t& address) +{ + ble_error_t err = cordio::BLE::deviceInstance().getGap().setRandomStaticAddress(address); + MBED_ASSERT(err == BLE_ERROR_NONE); +} + + void CordioHCIDriver::start_reset_sequence() { /* send an HCI Reset command to start the sequence */ @@ -148,10 +155,7 @@ void CordioHCIDriver::handle_reset_sequence(uint8_t *pMsg) if (get_random_static_address(static_address)) { // note: will send the HCI command to send the random address - cordio::BLE::deviceInstance().getGap().setAddress( - BLEProtocol::AddressType::RANDOM_STATIC, - static_address.data() - ); + set_random_static_address(static_address.data()); } else { /* send next command in sequence */ HciLeReadBufSizeCmd(); diff --git a/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h b/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h index 737bce73de..e620748e49 100644 --- a/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h +++ b/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h @@ -151,6 +151,13 @@ protected: */ buf_pool_desc_t get_default_buffer_pool_description(); + /** + * Allows the driver to set a random static address. Unlike the HCI command + * this function reports the random static address to the whole BLE system. + * @param random_static_address The random static address to set. + */ + void set_random_static_address(const ble::address_t& random_static_address); + private: /** * Initialize the chip.