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