1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "bs_bt_utils.h"
8
9 #include <stdint.h>
10 #include <string.h>
11
12 #include <zephyr/bluetooth/addr.h>
13 #include <zephyr/bluetooth/bluetooth.h>
14 #include <zephyr/bluetooth/conn.h>
15
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(dut, 4);
18
19 static const struct bt_data ad[] = {
20 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
21 };
22
23 bt_addr_le_t dut_addr = {BT_ADDR_LE_RANDOM, {{0x0A, 0x89, 0x67, 0x45, 0x23, 0xC1}}};
24
set_public_addr(void)25 static void set_public_addr(void)
26 {
27 /* dummy irk so we don't get -EINVAL because of BT_PRIVACY=y */
28 uint8_t irk[16];
29 int err;
30
31 for (uint8_t i = 0; i < 16; i++) {
32 irk[i] = i;
33 }
34
35 err = bt_id_create(&dut_addr, irk);
36 if (err) {
37 FAIL("Failed to override addr %d\n", err);
38 }
39 }
40
start_advertising(uint32_t options)41 void start_advertising(uint32_t options)
42 {
43 int err;
44
45 struct bt_le_adv_param param =
46 BT_LE_ADV_PARAM_INIT(0, BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, NULL);
47 param.options |= options;
48
49 err = bt_le_adv_start(¶m, ad, ARRAY_SIZE(ad), NULL, 0);
50 if (err) {
51 FAIL("Failed to start advertising (err %d)\n", err);
52 }
53 }
54
generate_new_rpa(void)55 void generate_new_rpa(void)
56 {
57 /* This will generate a new RPA and mark it valid */
58 struct bt_le_oob oob_local = { 0 };
59
60 bt_le_oob_get_local(BT_ID_DEFAULT, &oob_local);
61 }
62
dut_procedure(void)63 void dut_procedure(void)
64 {
65 int err;
66
67 /* open a backchannel to the peer */
68 backchannel_init(CENTRAL_SIM_ID);
69
70 /* override public address so the scanner can test if we're using it or not */
71 set_public_addr();
72
73 LOG_DBG("enable bt");
74 err = bt_enable(NULL);
75 if (err) {
76 FAIL("Failed to enable bluetooth (err %d\n)", err);
77 }
78
79 LOG_DBG("generate new RPA");
80 generate_new_rpa();
81
82 LOG_DBG("start adv with identity");
83 start_advertising(BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_USE_IDENTITY);
84
85 /* wait for the tester to validate we're using our identity address */
86 LOG_DBG("wait for validation by tester");
87 backchannel_sync_wait();
88 LOG_DBG("wait for validation by tester");
89 err = bt_le_adv_stop();
90 if (err) {
91 FAIL("Failed to stop advertising (err %d\n)", err);
92 }
93
94 LOG_DBG("start adv with RPA");
95 start_advertising(BT_LE_ADV_OPT_CONN);
96
97 /* Test pass verdict is decided by the tester */
98 PASS("DUT done\n");
99 }
100