Bluetooth: host: adv: set the address in bt_le_adv_resume

The address could have changed. The issue linked (and bsim test in previous
commit) shows a scenario where that could happen:

- we start scanning, host will use NRPA
- we start advertising, host will use identity address
- a device connects (as a central)
-> advertising resume fails for some unrelated reason
- another device connects (as a peripheral)
- the central device disconnects, we resume scanning
- the peripheral disconnects, the stack resumes advertising
-> but it mistakenly advertises using the NRPA set by the scanner

Fixes #52059 .

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
This commit is contained in:
Jonathan Rico 2022-12-09 16:14:19 +01:00 committed by Fabio Baltieri
commit dc4d63e97b
2 changed files with 32 additions and 0 deletions

View file

@ -1445,6 +1445,25 @@ int bt_le_adv_stop(void)
}
#if defined(CONFIG_BT_PERIPHERAL)
static uint32_t adv_get_options(const struct bt_le_ext_adv *adv)
{
uint32_t options = 0;
if (!atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
options |= BT_LE_ADV_OPT_ONE_TIME;
}
if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
options |= BT_LE_ADV_OPT_CONNECTABLE;
}
if (atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
options |= BT_LE_ADV_OPT_USE_IDENTITY;
}
return options;
}
void bt_le_adv_resume(void)
{
struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
@ -1477,6 +1496,17 @@ void bt_le_adv_resume(void)
if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
!atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
bt_id_set_adv_private_addr(adv);
} else {
uint8_t own_addr_type;
bool dir_adv = adv_is_directed(adv);
uint32_t options = adv_get_options(adv);
/* Always set the address. Don't assume it has not changed. */
err = bt_id_set_adv_own_addr(adv, options, dir_adv, &own_addr_type);
if (err) {
LOG_ERR("Controller cannot resume connectable advertising (%d)", err);
return;
}
}
err = bt_le_adv_set_enable(adv, true);

View file

@ -1724,6 +1724,8 @@ int bt_id_set_scan_own_addr(bool active_scan, uint8_t *own_addr_type)
if (err) {
return err;
}
} else {
LOG_DBG("Not changing the address");
}
}