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_alloc(K_FOREVER);
63 	zassert_not_null(buf, "Failed to create HCI cmd object");
64 
65 	cp = net_buf_add(buf, sizeof(*cp));
66 	cp->handle = g_adv->handle;
67 	cp->cte_len = g_cte_len;
68 	cp->cte_type = BT_HCI_LE_AOD_CTE_2US;
69 	cp->cte_count = 5;
70 
71 	dest_ant_ids = net_buf_add(buf, ARRAY_SIZE(ant_ids));
72 	memcpy(dest_ant_ids, ant_ids, ARRAY_SIZE(ant_ids));
73 
74 	cp->switch_pattern_len = ARRAY_SIZE(ant_ids);
75 
76 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CL_CTE_TX_PARAMS, buf,
77 				   NULL);
78 	zassert_equal(err, 0, "Failed to  set CTE parameters");
79 }
80 
common_set_adv_params(void)81 void common_set_adv_params(void)
82 {
83 	int err;
84 
85 	err = bt_le_per_adv_set_param(g_adv, &per_param);
86 	zassert_equal(err, 0, "Failed to set periodic advertising params");
87 }
88 
common_per_adv_enable(void)89 void common_per_adv_enable(void)
90 {
91 	int err;
92 
93 	err = bt_le_per_adv_start(g_adv);
94 	zassert_equal(err, 0, "Failed to start periodic advertising");
95 
96 	err = bt_le_ext_adv_start(g_adv, &ext_adv_start_param);
97 	zassert_equal(err, 0, "Failed to start extended advertising");
98 }
99 
common_per_adv_disable(void)100 void common_per_adv_disable(void)
101 {
102 	int err;
103 
104 	err = bt_le_per_adv_stop(g_adv);
105 	zassert_equal(err, 0, "Failed to stop periodic advertising");
106 
107 	err = bt_le_ext_adv_stop(g_adv);
108 	zassert_equal(err, 0, "Failed to stop extended advertising");
109 }
110