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