Skip to content

Commit ae0704e

Browse files
authored
Merge pull request #12321 from pan-/ble-driver-set-random-static-address
Ble driver set random static address
2 parents cf8a17e + cb340f8 commit ae0704e

File tree

10 files changed

+64
-20
lines changed

10 files changed

+64
-20
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 reserved 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.

features/FEATURE_BLE/targets/TARGET_CORDIO_ODIN_W2/HCIDriver.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,7 @@ void ble::vendor::odin_w2::HCIDriver::handle_reset_sequence(uint8_t *pMsg)
341341
memcpy(addr, pMsg, sizeof(addr));
342342
DM_RAND_ADDR_SET(addr, DM_RAND_ADDR_STATIC);
343343
// note: will invoke set rand address
344-
cordio::BLE::deviceInstance().getGap().setAddress(
345-
BLEProtocol::AddressType::RANDOM_STATIC,
346-
addr
347-
);
344+
set_random_static_address(addr);
348345
}
349346
break;
350347

features/FEATURE_BLE/targets/TARGET_Cypress/COMPONENT_CYW43XXX/HCIDriver.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,7 @@ class HCIDriver : public cordio::CordioHCIDriver {
301301
memcpy(addr, pMsg, sizeof(addr));
302302
DM_RAND_ADDR_SET(addr, DM_RAND_ADDR_STATIC);
303303
// note: will invoke set rand address
304-
cordio::BLE::deviceInstance().getGap().setAddress(
305-
BLEProtocol::AddressType::RANDOM_STATIC,
306-
addr
307-
);
304+
set_random_static_address(addr);
308305
}
309306
break;
310307

features/FEATURE_BLE/targets/TARGET_STM/TARGET_CYW4343X/HCIDriver.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ class HCIDriver : public cordio::CordioHCIDriver {
265265
memcpy(addr, pMsg, sizeof(addr));
266266
DM_RAND_ADDR_SET(addr, DM_RAND_ADDR_STATIC);
267267
// note: will invoke set rand address
268-
cordio::BLE::deviceInstance().getGap().setAddress(
269-
BLEProtocol::AddressType::RANDOM_STATIC,
270-
addr
271-
);
268+
set_random_static_address(addr);
272269
}
273270
break;
274271

features/FEATURE_BLE/targets/TARGET_STM/TARGET_STM32WB/HCIDriver.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,7 @@ class HCIDriver : public cordio::CordioHCIDriver {
313313
memcpy(addr, pMsg, sizeof(addr));
314314
DM_RAND_ADDR_SET(addr, DM_RAND_ADDR_STATIC);
315315
// note: will invoke set rand address
316-
cordio::BLE::deviceInstance().getGap().setAddress(
317-
BLEProtocol::AddressType::RANDOM_STATIC,
318-
addr
319-
);
316+
set_random_static_address(addr);
320317
}
321318
break;
322319

0 commit comments

Comments
 (0)