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 #include <zephyr/toolchain.h>
16 #include <zephyr/settings/settings.h>
17 
18 #include "common/bt_str.h"
19 
20 #define ID_A_INDEX 1
21 #define ID_B_INDEX 2
22 
23 #define ADV_SET_INDEX_ONE   0x00
24 #define ADV_SET_INDEX_TWO   0x01
25 #define ADV_SET_INDEX_THREE 0x02
26 
27 static struct bt_le_ext_adv *adv_set[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
28 
29 static const struct bt_data ad_id[] = {
30 	BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA, ADV_SET_INDEX_ONE),
31 	BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA, ADV_SET_INDEX_TWO),
32 	BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA, ADV_SET_INDEX_THREE),
33 };
34 
cb_rpa_expired(struct bt_le_ext_adv * adv)35 bool cb_rpa_expired(struct bt_le_ext_adv *adv)
36 {
37 	/* Return true to rotate the current RPA */
38 	int err;
39 	struct bt_le_ext_adv_info info;
40 
41 	err = bt_le_ext_adv_get_info(adv, &info);
42 	if (err) {
43 		return false;
44 	}
45 	printk("advertiser[%d] RPA %s\n", info.id, bt_addr_le_str(info.addr));
46 	return true;
47 }
48 
create_adv(struct bt_le_ext_adv ** adv,int id)49 static void create_adv(struct bt_le_ext_adv **adv, int id)
50 {
51 	int err;
52 	struct bt_le_adv_param params;
53 	static struct bt_le_ext_adv_cb cb_adv;
54 
55 	cb_adv.rpa_expired = cb_rpa_expired;
56 	memset(&params, 0, sizeof(struct bt_le_adv_param));
57 
58 	params.options |= BT_LE_ADV_OPT_EXT_ADV;
59 	params.id = id;
60 	params.sid = 0;
61 	params.interval_min = BT_GAP_ADV_FAST_INT_MIN_1;
62 	params.interval_max = BT_GAP_ADV_FAST_INT_MAX_1;
63 
64 	err = bt_le_ext_adv_create(&params, &cb_adv, adv);
65 	if (err) {
66 		FAIL("Failed to create advertiser (%d)\n", err);
67 	}
68 }
69 
start_advertising(void)70 void start_advertising(void)
71 {
72 	int err;
73 	size_t bt_id_count;
74 
75 	/* Enable bluetooth */
76 	err = bt_enable(NULL);
77 	if (err) {
78 		FAIL("Failed to enable bluetooth (err %d\n)", err);
79 	}
80 
81 	err = settings_load();
82 	if (err) {
83 		FAIL("Failed to enable settings (err %d\n)", err);
84 	}
85 
86 	bt_id_get(NULL, &bt_id_count);
87 	if (bt_id_count == 1) {
88 		int id_a;
89 		int id_b;
90 
91 		printk("No extra identity found in settings, creating new ones...\n");
92 
93 		id_a = bt_id_create(NULL, NULL);
94 		if (id_a != ID_A_INDEX) {
95 			FAIL("bt_id_create id_a failed (err %d)\n", id_a);
96 		}
97 
98 		id_b = bt_id_create(NULL, NULL);
99 		if (id_b != ID_B_INDEX) {
100 			FAIL("bt_id_create id_b failed (err %d)\n", id_b);
101 		}
102 	} else {
103 		printk("Extra identities loaded from settings\n");
104 	}
105 
106 	bt_id_get(NULL, &bt_id_count);
107 	if (bt_id_count != CONFIG_BT_ID_MAX) {
108 		FAIL("bt_id_get returned incorrect number of identities %u\n", bt_id_count);
109 	}
110 
111 	for (int i = 0; i < CONFIG_BT_EXT_ADV_MAX_ADV_SET; i++) {
112 
113 		if (i != ADV_SET_INDEX_THREE) {
114 			/* Create advertising set 1 and 2 with same id */
115 			create_adv(&adv_set[i], ID_A_INDEX);
116 		} else {
117 			/* Create advertising set 3 with different id */
118 			create_adv(&adv_set[i], ID_B_INDEX);
119 		}
120 
121 		/* Set extended advertising data */
122 		err = bt_le_ext_adv_set_data(adv_set[i], &ad_id[i], 1, NULL, 0);
123 		if (err) {
124 			FAIL("Failed to set advertising data for set %d (err %d)\n", i, err);
125 		}
126 
127 		err = bt_le_ext_adv_start(adv_set[i], BT_LE_EXT_ADV_START_DEFAULT);
128 		if (err) {
129 			FAIL("Failed to start advertising (err %d)\n", err);
130 		}
131 	}
132 }
133 
dut_procedure(void)134 void dut_procedure(void)
135 {
136 	start_advertising();
137 
138 	/* Nothing to do */
139 	PASS("PASS\n");
140 }
141