Skip to content

Commit ef7ede6

Browse files
olivier-le-sagedanieldegrasse
authored andcommitted
bluetooth: host: Do not try to set NRPA when scanning with identity
Attempting this would fail (assuming the controller is implemented correctly) because when using legacy commands it is not allowed to change the device address while scanning. It also did not make sense. If we have configured the scanner to use the identity address as own_addr, because the advertiser and scanner addresses are shared when using legacy commands, setting the adv NRPA here would overwrite the identity address used by the scanner, which I assume is not the intention. Signed-off-by: Olivier Lesage <olivier.lesage@nordicsemi.no>
1 parent 11782db commit ef7ede6

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed

subsys/bluetooth/host/id.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,22 +1998,38 @@ int bt_id_set_adv_own_addr(struct bt_le_ext_adv *adv, uint32_t options,
19981998
*/
19991999
#if defined(CONFIG_BT_OBSERVER)
20002000
bool scan_disabled = false;
2001+
bool dev_scanning = atomic_test_bit(bt_dev.flags,
2002+
BT_DEV_SCANNING);
20012003

20022004
/* If active scan with NRPA is ongoing refresh NRPA */
20032005
if (!IS_ENABLED(CONFIG_BT_PRIVACY) &&
20042006
!IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
2005-
atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
2007+
dev_scanning) {
20062008
scan_disabled = true;
20072009
bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
20082010
}
2009-
#endif /* defined(CONFIG_BT_OBSERVER) */
2010-
err = bt_id_set_adv_private_addr(adv);
2011-
*own_addr_type = BT_HCI_OWN_ADDR_RANDOM;
20122011

2013-
#if defined(CONFIG_BT_OBSERVER)
2012+
/* If we are scanning with the identity address, it does
2013+
* not make sense to set an NRPA.
2014+
*/
2015+
if (!IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) ||
2016+
!dev_scanning) {
2017+
err = bt_id_set_adv_private_addr(adv);
2018+
*own_addr_type = BT_HCI_OWN_ADDR_RANDOM;
2019+
} else {
2020+
if (id_addr->type == BT_ADDR_LE_RANDOM) {
2021+
*own_addr_type = BT_HCI_OWN_ADDR_RANDOM;
2022+
} else if (id_addr->type == BT_ADDR_LE_PUBLIC) {
2023+
*own_addr_type = BT_HCI_OWN_ADDR_PUBLIC;
2024+
}
2025+
}
2026+
20142027
if (scan_disabled) {
20152028
bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
20162029
}
2030+
#else
2031+
err = bt_id_set_adv_private_addr(adv);
2032+
*own_addr_type = BT_HCI_OWN_ADDR_RANDOM;
20172033
#endif /* defined(CONFIG_BT_OBSERVER) */
20182034
} else {
20192035
err = bt_id_set_adv_private_addr(adv);

tests/bluetooth/host/id/bt_id_set_adv_own_addr/src/main.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "mocks/crypto.h"
88
#include "mocks/scan.h"
99
#include "mocks/scan_expects.h"
10+
#include "mocks/hci_core.h"
11+
#include "mocks/hci_core_expects.h"
12+
#include "mocks/net_buf.h"
13+
#include "mocks/net_buf_expects.h"
1014
#include "testing_common_defs.h"
1115

1216
#include <zephyr/bluetooth/hci.h>
@@ -24,6 +28,7 @@ static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixt
2428
memset(&bt_dev, 0x00, sizeof(struct bt_dev));
2529

2630
CRYPTO_FFF_FAKES_LIST(RESET_FAKE);
31+
HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
2732
}
2833

2934
ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
@@ -232,6 +237,7 @@ ZTEST(bt_id_set_adv_own_addr, test_observer_scanning_re_enabled_after_updating_a
232237

233238
Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
234239
Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
240+
Z_TEST_SKIP_IFDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
235241
Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
236242

237243
options &= ~BT_LE_ADV_OPT_CONN;
@@ -241,6 +247,102 @@ ZTEST(bt_id_set_adv_own_addr, test_observer_scanning_re_enabled_after_updating_a
241247
atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
242248

243249
bt_id_set_adv_own_addr(&adv, options, true, &own_addr_type);
250+
zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
251+
"Address type reference was incorrectly set");
244252

245253
expect_call_count_bt_le_scan_set_enable(2, expected_args_history);
246254
}
255+
256+
/*
257+
* Test setting the advertiser address while 'CONFIG_BT_SCAN_WITH_IDENTITY' is enabled
258+
* and scanning is ongoing. The scanner is using a random identity address.
259+
*
260+
* Constraints:
261+
* - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
262+
*
263+
* Expected behaviour:
264+
* - Scanning is not disabled.
265+
* - The advertiser doesn't attempt to change the identity addr with bt_id_set_adv_private_addr()
266+
* - The advertiser uses the same identity address as the scanner.
267+
*/
268+
ZTEST(bt_id_set_adv_own_addr, test_set_adv_own_addr_while_scanning_with_identity_random)
269+
{
270+
uint32_t options = 0;
271+
struct bt_le_ext_adv adv = {0};
272+
struct net_buf net_buff;
273+
int err;
274+
uint8_t scan_own_addr_type = BT_ADDR_LE_ANONYMOUS;
275+
uint8_t adv_own_addr_type = BT_ADDR_LE_ANONYMOUS;
276+
277+
Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
278+
Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
279+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
280+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
281+
282+
bt_hci_cmd_alloc_fake.return_val = &net_buff;
283+
bt_hci_cmd_send_sync_fake.return_val = 0;
284+
285+
options &= ~BT_LE_ADV_OPT_CONN;
286+
bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_STATIC_RANDOM_LE_ADDR_1);
287+
288+
err = bt_id_set_scan_own_addr(false, &scan_own_addr_type);
289+
290+
expect_single_call_bt_hci_cmd_alloc();
291+
expect_single_call_bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RANDOM_ADDRESS);
292+
293+
zassert_ok(err, "Unexpected error code '%d' was returned", err);
294+
zassert_true(scan_own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
295+
"Address type reference was incorrectly set");
296+
297+
atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
298+
299+
bt_id_set_adv_own_addr(&adv, options, true, &adv_own_addr_type);
300+
zassert_true(adv_own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
301+
"Address type reference was incorrectly set");
302+
303+
expect_call_count_bt_le_scan_set_enable(0, NULL);
304+
}
305+
306+
/*
307+
* Test setting the advertiser address while 'CONFIG_BT_SCAN_WITH_IDENTITY' is enabled
308+
* and scanning is ongoing. The scanner is using a public identity address.
309+
*
310+
* Constraints:
311+
* - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
312+
*
313+
* Expected behaviour:
314+
* - Scanning is not disabled.
315+
* - The advertiser doesn't attempt to change the identity addr with bt_id_set_adv_private_addr()
316+
* - The advertiser uses the same identity address as the scanner.
317+
*/
318+
ZTEST(bt_id_set_adv_own_addr, test_set_adv_own_addr_while_scanning_with_identity_public)
319+
{
320+
uint32_t options = 0;
321+
struct bt_le_ext_adv adv = {0};
322+
int err;
323+
uint8_t scan_own_addr_type = BT_ADDR_LE_ANONYMOUS;
324+
uint8_t adv_own_addr_type = BT_ADDR_LE_ANONYMOUS;
325+
326+
Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
327+
Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
328+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
329+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
330+
331+
options &= ~BT_LE_ADV_OPT_CONN;
332+
333+
bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_LE_ADDR);
334+
335+
err = bt_id_set_scan_own_addr(false, &scan_own_addr_type);
336+
337+
zassert_ok(err, "Unexpected error code '%d' was returned", err);
338+
zassert_true(scan_own_addr_type == BT_HCI_OWN_ADDR_PUBLIC,
339+
"Address type reference was incorrectly set");
340+
341+
atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
342+
343+
bt_id_set_adv_own_addr(&adv, options, true, &adv_own_addr_type);
344+
zassert_true(adv_own_addr_type == BT_HCI_OWN_ADDR_PUBLIC,
345+
"Address type reference was incorrectly set");
346+
347+
expect_call_count_bt_le_scan_set_enable(0, NULL);
348+
}

tests/bluetooth/host/id/bt_id_set_adv_own_addr/testcase.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ tests:
1414
extra_configs:
1515
- CONFIG_BT_SMP=y
1616
- CONFIG_BT_PRIVACY=y
17+
bluetooth.host.bt_id_set_adv_own_addr.scan_with_identity:
18+
type: unit
19+
extra_configs:
20+
- CONFIG_BT_SCAN_WITH_IDENTITY=y

0 commit comments

Comments
 (0)