Skip to content

Commit 215cea0

Browse files
committed
BLE: Add an HCI driver API to set the random static address.
1 parent ee1d998 commit 215cea0

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

features/FEATURE_BLE/ble/gap/Gap.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,13 @@ class Gap : public StaticInterface<Impl, Gap> {
12571257
#endif // BLE_FEATURE_PRIVACY
12581258

12591259
#if !defined(DOXYGEN_ONLY)
1260+
/*
1261+
* API reserverved for the controller driver to set the random static address.
1262+
* Setting a new random static address while the controller is operating is
1263+
* forbidden by the Bluetooth specification.
1264+
*/
1265+
ble_error_t setRandomStaticAddress(const ble::address_t& address);
1266+
12601267
protected:
12611268
/** Can only be called if use_non_deprecated_scan_api() hasn't been called.
12621269
* This guards against mixed use of deprecated and nondeprecated API.
@@ -1407,6 +1414,7 @@ class Gap : public StaticInterface<Impl, Gap> {
14071414
ble_error_t getCentralPrivacyConfiguration_(
14081415
central_privay_configuration_t *configuration
14091416
);
1417+
ble_error_t setRandomStaticAddress_(const ble::address_t& address);
14101418
void useVersionOneAPI_() const;
14111419
void useVersionTwoAPI_() const;
14121420

features/FEATURE_BLE/ble/generic/GenericGap.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ class GenericGap :
306306
const BLEProtocol::AddressBytes_t address
307307
);
308308

309+
/**
310+
* @see Gap::setRandomStaticAddress
311+
*/
312+
ble_error_t setRandomStaticAddress_(const ble::address_t& address);
313+
309314
/**
310315
* @see Gap::getAddress
311316
*/

features/FEATURE_BLE/source/gap/Gap.tpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ ble_error_t Gap<Impl>::getCentralPrivacyConfiguration(
494494
#endif // BLE_ROLE_OBSERVER
495495
#endif // BLE_FEATURE_PRIVACY
496496

497+
template<class Impl>
498+
ble_error_t Gap<Impl>::setRandomStaticAddress(const ble::address_t& address)
499+
{
500+
return impl()->setRandomStaticAddress_(address);
501+
}
502+
497503
// -----------------------------------------------------------------------------
498504
/* ------------------------- Default implementations ------------------------ */
499505
// -----------------------------------------------------------------------------
@@ -861,6 +867,12 @@ ble_error_t Gap<Impl>::getCentralPrivacyConfiguration_(
861867
return BLE_ERROR_NOT_IMPLEMENTED;
862868
}
863869

870+
template<class Impl>
871+
ble_error_t Gap<Impl>::setRandomStaticAddress_(const ble::address_t& address)
872+
{
873+
return BLE_ERROR_NOT_IMPLEMENTED;
874+
}
875+
864876
} // namespace interface
865877
} // namespace ble
866878

features/FEATURE_BLE/source/generic/GenericGap.tpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,26 @@ ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEve
510510
}
511511
}
512512

513+
template <template<class> class PalGapImpl, class PalSecurityManager, class ConnectionEventMonitorEventHandler>
514+
ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEventHandler>::setRandomStaticAddress_(
515+
const ble::address_t& address
516+
)
517+
{
518+
if (is_random_static_address(address.data()) == false) {
519+
return BLE_ERROR_INVALID_PARAM;
520+
}
521+
522+
ble_error_t err = _pal_gap.set_random_address(address);
523+
if (err) {
524+
return err;
525+
}
526+
527+
_address_type = LegacyAddressType::RANDOM_STATIC;
528+
_address = address;
529+
_random_static_identity_address = address;
530+
return BLE_ERROR_NONE;
531+
}
532+
513533
template <template<class> class PalGapImpl, class PalSecurityManager, class ConnectionEventMonitorEventHandler>
514534
ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEventHandler>::getAddress_(
515535
LegacyAddressType_t *type,

features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ buf_pool_desc_t CordioHCIDriver::get_default_buffer_pool_description()
9696
return buf_pool_desc_t(buffer, pool_desc);
9797
}
9898

99+
void CordioHCIDriver::set_random_static_address(const ble::address_t& address)
100+
{
101+
ble_error_t err = cordio::BLE::deviceInstance().getGap().setRandomStaticAddress(address);
102+
MBED_ASSERT(err == BLE_ERROR_NONE);
103+
}
104+
105+
99106
void CordioHCIDriver::start_reset_sequence()
100107
{
101108
/* send an HCI Reset command to start the sequence */
@@ -148,10 +155,7 @@ void CordioHCIDriver::handle_reset_sequence(uint8_t *pMsg)
148155

149156
if (get_random_static_address(static_address)) {
150157
// note: will send the HCI command to send the random address
151-
cordio::BLE::deviceInstance().getGap().setAddress(
152-
BLEProtocol::AddressType::RANDOM_STATIC,
153-
static_address.data()
154-
);
158+
set_random_static_address(static_address.data());
155159
} else {
156160
/* send next command in sequence */
157161
HciLeReadBufSizeCmd();

features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ class CordioHCIDriver {
151151
*/
152152
buf_pool_desc_t get_default_buffer_pool_description();
153153

154+
/**
155+
* Allows the driver to set a random static address. Unlike the HCI command
156+
* this function reports the random static address to the whole BLE system.
157+
* @param random_static_address The random static address to set.
158+
*/
159+
void set_random_static_address(const ble::address_t& random_static_address);
160+
154161
private:
155162
/**
156163
* Initialize the chip.

0 commit comments

Comments
 (0)