1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <stddef.h>
9 #include <zephyr/ztest.h>
10 
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/bluetooth/direction.h>
14 #include <host/hci_core.h>
15 
16 #include "common.h"
17 
18 struct bt_le_ext_adv *g_adv;
19 struct bt_le_adv_param g_param = BT_LE_ADV_PARAM_INIT(
20 	(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_NOTIFY_SCAN_REQ),
21 	BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, NULL);
22 
23 /* Example cte length value in allowed range, no particular meaning */
24 uint8_t g_cte_len = 0x14U;
25 
26 static struct bt_le_per_adv_param per_param = {
27 	.interval_min = BT_GAP_ADV_SLOW_INT_MIN,
28 	.interval_max = BT_GAP_ADV_SLOW_INT_MAX,
29 	.options = BT_LE_ADV_OPT_USE_TX_POWER,
30 };
31 
32 static struct bt_le_ext_adv_start_param ext_adv_start_param = {
33 	.timeout = 0,
34 	.num_events = 0,
35 };
36 
common_create_adv_set(void)37 void common_create_adv_set(void)
38 {
39 	int err;
40 
41 	err = bt_le_ext_adv_create(&g_param, NULL, &g_adv);
42 	zassert_equal(err, 0, "Failed to create advertiser set");
43 }
44 
common_delete_adv_set(void)45 void common_delete_adv_set(void)
46 {
47 	int err;
48 
49 	err = bt_le_ext_adv_delete(g_adv);
50 	zassert_equal(err, 0, "Failed to delete advertiser set");
51 }
52 
common_set_cl_cte_tx_params(void)53 void common_set_cl_cte_tx_params(void)
54 {
55 	uint8_t ant_ids[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
56 
57 	struct bt_hci_cp_le_set_cl_cte_tx_params *cp;
58 	uint8_t *dest_ant_ids;
59 	struct net_buf *buf;
60 	int err;
61 
62 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_CL_CTE_TX_PARAMS,
63 				sizeof(*cp) + ARRAY_SIZE(ant_ids));
64 	zassert_not_null(buf, "Failed to create HCI cmd object");
65 
66 	cp = net_buf_add(buf, sizeof(*cp));
67 	cp->handle = g_adv->handle;
68 	cp->cte_len = g_cte_len;
69 	cp->cte_type = BT_HCI_LE_AOD_CTE_2US;
70 	cp->cte_count = 5;
71 
72 	dest_ant_ids = net_buf_add(buf, ARRAY_SIZE(ant_ids));
73 	memcpy(dest_ant_ids, ant_ids, ARRAY_SIZE(ant_ids));
74 
75 	cp->switch_pattern_len = ARRAY_SIZE(ant_ids);
76 
77 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CL_CTE_TX_PARAMS, buf,
78 				   NULL);
79 	zassert_equal(err, 0, "Failed to  set CTE parameters");
80 }
81 
common_set_adv_params(void)82 void common_set_adv_params(void)
83 {
84 	int err;
85 
86 	err = bt_le_per_adv_set_param(g_adv, &per_param);
87 	zassert_equal(err, 0, "Failed to set periodic advertising params");
88 }
89 
common_per_adv_enable(void)90 void common_per_adv_enable(void)
91 {
92 	int err;
93 
94 	err = bt_le_per_adv_start(g_adv);
95 	zassert_equal(err, 0, "Failed to start periodic advertising");
96 
97 	err = bt_le_ext_adv_start(g_adv, &ext_adv_start_param);
98 	zassert_equal(err, 0, "Failed to start extended advertising");
99 }
100 
common_per_adv_disable(void)101 void common_per_adv_disable(void)
102 {
103 	int err;
104 
105 	err = bt_le_per_adv_stop(g_adv);
106 	zassert_equal(err, 0, "Failed to stop periodic advertising");
107 
108 	err = bt_le_ext_adv_stop(g_adv);
109 	zassert_equal(err, 0, "Failed to stop extended advertising");
110 }
111