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 = BT_LE_ADV_PARAM_INIT(BT_LE_ADV_OPT_ONE_TIME,
46 							    BT_GAP_ADV_FAST_INT_MIN_2,
47 							    BT_GAP_ADV_FAST_INT_MAX_2,
48 							    NULL);
49 	param.options |= options;
50 
51 	err = bt_le_adv_start(&param, ad, ARRAY_SIZE(ad), NULL, 0);
52 	if (err) {
53 		FAIL("Failed to start advertising (err %d)\n", err);
54 	}
55 }
56 
generate_new_rpa(void)57 void generate_new_rpa(void)
58 {
59 	/* This will generate a new RPA and mark it valid */
60 	struct bt_le_oob oob_local = { 0 };
61 
62 	bt_le_oob_get_local(BT_ID_DEFAULT, &oob_local);
63 }
64 
dut_procedure(void)65 void dut_procedure(void)
66 {
67 	int err;
68 
69 	/* open a backchannel to the peer */
70 	backchannel_init(CENTRAL_SIM_ID);
71 
72 	/* override public address so the scanner can test if we're using it or not */
73 	set_public_addr();
74 
75 	LOG_DBG("enable bt");
76 	err = bt_enable(NULL);
77 	if (err) {
78 		FAIL("Failed to enable bluetooth (err %d\n)", err);
79 	}
80 
81 	LOG_DBG("generate new RPA");
82 	generate_new_rpa();
83 
84 	LOG_DBG("start adv with identity");
85 	start_advertising(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_USE_IDENTITY);
86 
87 	/* wait for the tester to validate we're using our identity address */
88 	LOG_DBG("wait for validation by tester");
89 	backchannel_sync_wait();
90 	LOG_DBG("wait for validation by tester");
91 	err = bt_le_adv_stop();
92 	if (err) {
93 		FAIL("Failed to stop advertising (err %d\n)", err);
94 	}
95 
96 	LOG_DBG("start adv with RPA");
97 	start_advertising(BT_LE_ADV_OPT_CONNECTABLE);
98 
99 	/* signal tester it can start scanning again, expecting an RPA this time */
100 	backchannel_sync_send();
101 
102 	/* Test pass verdict is decided by the tester */
103 	PASS("DUT done\n");
104 }
105