Skip to content

Commit db187bb

Browse files
joerchannordicjm
authored andcommitted
[nrf fromtree] Bluetooth: host: Call RPA expired on disabled advertising sets
Call the RPA expired callback on advertising sets even when they are not enabled. The RPA expired callback will now be called on advertising sets when the BT_ADV_RPA_VALID flag is cleared for the set. Signed-off-by: Joakim Andersson <[email protected]> (cherry picked from commit 51d510a) Signed-off-by: Martin Tverdal <[email protected]>
1 parent 7d59ac1 commit db187bb

File tree

3 files changed

+65
-47
lines changed

3 files changed

+65
-47
lines changed

subsys/bluetooth/host/adv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv)
16291629
}
16301630

16311631
if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
1632-
atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1632+
bt_id_adv_limited_stopped(adv);
16331633

16341634
#if defined(CONFIG_BT_SMP)
16351635
bt_id_pending_keys_update();
@@ -2005,7 +2005,7 @@ void bt_hci_le_adv_set_terminated(struct net_buf *buf)
20052005
}
20062006

20072007
if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
2008-
atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
2008+
bt_id_adv_limited_stopped(adv);
20092009

20102010
#if defined(CONFIG_BT_SMP)
20112011
bt_id_pending_keys_update();

subsys/bluetooth/host/id.c

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ static void adv_id_check_func(struct bt_le_ext_adv *adv, void *data)
7171
}
7272
}
7373

74-
#if defined(CONFIG_BT_PRIVACY)
7574
static void adv_is_private_enabled(struct bt_le_ext_adv *adv, void *data)
7675
{
7776
bool *adv_enabled = data;
@@ -81,7 +80,6 @@ static void adv_is_private_enabled(struct bt_le_ext_adv *adv, void *data)
8180
*adv_enabled = true;
8281
}
8382
}
84-
#endif /* defined(CONFIG_BT_PRIVACY) */
8583

8684
#if defined(CONFIG_BT_SMP)
8785
static void adv_is_limited_enabled(struct bt_le_ext_adv *adv, void *data)
@@ -184,14 +182,33 @@ int bt_id_set_adv_random_addr(struct bt_le_ext_adv *adv,
184182
return 0;
185183
}
186184

187-
static void adv_disabled_rpa_invalidate(struct bt_le_ext_adv *adv, void *data)
185+
static void adv_rpa_expired(struct bt_le_ext_adv *adv)
188186
{
189-
/* Don't invalidate RPA of enabled advertising set. */
190-
if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
187+
bool rpa_invalid = true;
188+
189+
#if defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_PRIVACY)
190+
/* Notify the user about the RPA timeout and set the RPA validity. */
191+
if (atomic_test_bit(adv->flags, BT_ADV_RPA_VALID) &&
192+
adv->cb && adv->cb->rpa_expired) {
193+
rpa_invalid = adv->cb->rpa_expired(adv);
194+
}
195+
#endif
196+
if (rpa_invalid) {
191197
atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
192198
}
193199
}
194200

201+
static void adv_rpa_invalidate(struct bt_le_ext_adv *adv, void *data)
202+
{
203+
/* RPA of Advertisers limited by timeot or number of packets only expire
204+
* when they are stopped.
205+
*/
206+
if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED) &&
207+
!atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
208+
adv_rpa_expired(adv);
209+
}
210+
}
211+
195212
static void le_rpa_invalidate(void)
196213
{
197214
/* Invalidate RPA */
@@ -200,13 +217,8 @@ static void le_rpa_invalidate(void)
200217
atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
201218
}
202219

203-
/* Invalidate RPA of advertising sets that are disabled.
204-
*
205-
* Enabled advertising set have to call rpa_expired callback first to
206-
* determine if the RPA of the advertising set should be rotated.
207-
*/
208220
if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
209-
bt_le_ext_adv_foreach(adv_disabled_rpa_invalidate, NULL);
221+
bt_le_ext_adv_foreach(adv_rpa_invalidate, NULL);
210222
}
211223
}
212224

@@ -298,6 +310,13 @@ int bt_id_set_adv_private_addr(struct bt_le_ext_adv *adv)
298310

299311
/* check if RPA is valid */
300312
if (atomic_test_bit(adv->flags, BT_ADV_RPA_VALID)) {
313+
/* Schedule the RPA timer if it is not running.
314+
* The RPA may be valid without the timer running.
315+
*/
316+
if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) {
317+
le_rpa_timeout_submit();
318+
}
319+
301320
return 0;
302321
}
303322

@@ -390,10 +409,9 @@ int bt_id_set_adv_private_addr(struct bt_le_ext_adv *adv)
390409
}
391410
#endif /* defined(CONFIG_BT_PRIVACY) */
392411

393-
#if defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_PRIVACY)
394-
static void adv_disable_rpa(struct bt_le_ext_adv *adv, void *data)
412+
static void adv_pause_rpa(struct bt_le_ext_adv *adv, void *data)
395413
{
396-
bool rpa_invalid = true;
414+
bool *adv_enabled = data;
397415

398416
/* Disable advertising sets to prepare them for RPA update. */
399417
if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
@@ -402,21 +420,26 @@ static void adv_disable_rpa(struct bt_le_ext_adv *adv, void *data)
402420
bt_le_adv_set_enable_ext(adv, false, NULL);
403421

404422
atomic_set_bit(adv->flags, BT_ADV_RPA_UPDATE);
423+
*adv_enabled = true;
424+
}
425+
}
405426

406-
/* Notify the user about the RPA timeout and set the RPA validity. */
407-
if (adv->cb && adv->cb->rpa_expired) {
408-
rpa_invalid = adv->cb->rpa_expired(adv);
409-
}
427+
static bool le_adv_rpa_timeout(void)
428+
{
429+
bool adv_enabled = false;
410430

411-
if (rpa_invalid) {
412-
atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
431+
if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
432+
if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
433+
BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
434+
/* Pause all advertising sets using RPAs */
435+
bt_le_ext_adv_foreach(adv_pause_rpa, &adv_enabled);
413436
} else {
414-
/* Submit the timeout in case no advertising set updates their RPA
415-
* in the current period. This makes sure that the RPA timer is running.
416-
*/
417-
le_rpa_timeout_submit();
437+
/* Check if advertising set is enabled */
438+
bt_le_ext_adv_foreach(adv_is_private_enabled, &adv_enabled);
418439
}
419440
}
441+
442+
return adv_enabled;
420443
}
421444

422445
static void adv_enable_rpa(struct bt_le_ext_adv *adv, void *data)
@@ -433,16 +456,6 @@ static void adv_enable_rpa(struct bt_le_ext_adv *adv, void *data)
433456
bt_le_adv_set_enable_ext(adv, true, NULL);
434457
}
435458
}
436-
#endif /* defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_PRIVACY) */
437-
438-
static void adv_update_rpa_foreach(void)
439-
{
440-
#if defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_PRIVACY)
441-
bt_le_ext_adv_foreach(adv_disable_rpa, NULL);
442-
443-
bt_le_ext_adv_foreach(adv_enable_rpa, NULL);
444-
#endif
445-
}
446459

447460
static void le_update_private_addr(void)
448461
{
@@ -451,12 +464,6 @@ static void le_update_private_addr(void)
451464
uint8_t id = BT_ID_DEFAULT;
452465
int err;
453466

454-
if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
455-
IS_ENABLED(CONFIG_BT_EXT_ADV) &&
456-
BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
457-
adv_update_rpa_foreach();
458-
}
459-
460467
#if defined(CONFIG_BT_OBSERVER)
461468
bool scan_enabled = false;
462469

@@ -500,6 +507,12 @@ static void le_update_private_addr(void)
500507
return;
501508
}
502509

510+
if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
511+
IS_ENABLED(CONFIG_BT_EXT_ADV) &&
512+
BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
513+
bt_le_ext_adv_foreach(adv_enable_rpa, NULL);
514+
}
515+
503516
if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
504517
adv && adv_enabled) {
505518
bt_le_adv_set_enable_legacy(adv, true);
@@ -519,14 +532,15 @@ static void le_force_rpa_timeout(void)
519532

520533
k_work_cancel_delayable_sync(&bt_dev.rpa_update, &sync);
521534
#endif
535+
(void)le_adv_rpa_timeout();
522536
le_rpa_invalidate();
523537
le_update_private_addr();
524538
}
525539

526540
#if defined(CONFIG_BT_PRIVACY)
527541
static void rpa_timeout(struct k_work *work)
528542
{
529-
bool adv_enabled = false;
543+
bool adv_enabled;
530544

531545
BT_DBG("");
532546

@@ -541,12 +555,9 @@ static void rpa_timeout(struct k_work *work)
541555
}
542556
}
543557

558+
adv_enabled = le_adv_rpa_timeout();
544559
le_rpa_invalidate();
545560

546-
if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
547-
bt_le_ext_adv_foreach(adv_is_private_enabled, &adv_enabled);
548-
}
549-
550561
/* IF no roles using the RPA is running we can stop the RPA timer */
551562
if (!(adv_enabled ||
552563
atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING) ||
@@ -669,6 +680,11 @@ bool bt_id_adv_random_addr_check(const struct bt_le_adv_param *param)
669680
return true;
670681
}
671682

683+
void bt_id_adv_limited_stopped(struct bt_le_ext_adv *adv)
684+
{
685+
adv_rpa_expired(adv);
686+
}
687+
672688
#if defined(CONFIG_BT_SMP)
673689
static int le_set_privacy_mode(const bt_addr_le_t *addr, uint8_t mode)
674690
{

subsys/bluetooth/host/id.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ int bt_id_set_adv_private_addr(struct bt_le_ext_adv *adv);
4343
int bt_id_set_private_addr(uint8_t id);
4444

4545
void bt_id_pending_keys_update(void);
46+
47+
void bt_id_adv_limited_stopped(struct bt_le_ext_adv *adv);

0 commit comments

Comments
 (0)