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/sys/byteorder.h>
14 #include <host/hci_core.h>
15 
16 #include <bt_common.h>
17 #include <bt_conn_common.h>
18 #include "test_cte_set_rx_params.h"
19 
20 static uint16_t g_conn_handle;
21 
22 static uint8_t g_ant_ids[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
23 
24 static struct ut_bt_df_conn_cte_rx_params g_params;
25 
26 /* Macros delivering common values for unit tests */
27 #define CONN_HANDLE_INVALID (CONFIG_BT_MAX_CONN + 1)
28 #define ANTENNA_SWITCHING_SLOT_INVALID 0x3 /* BT_HCI_LE_ANTENNA_SWITCHING_SLOT_2US + 1 */
29 
30 /* Antenna switch pattern length is stored in 1 octet. If BT Core spec. extends the max value to
31  * UINT8_MAX expected failures may not be checked. If storage size is increased, tests shall be
32  * updated.
33  */
34 BUILD_ASSERT(CONFIG_BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN < UINT8_MAX,
35 	     "Can't test expected failures for switch pattern length longer or "
36 	     "equal to UINT8_MAX.");
37 #define SWITCH_PATTERN_LEN_TOO_LONG (CONFIG_BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN + 1)
38 
39 BUILD_ASSERT(BT_HCI_LE_SWITCH_PATTERN_LEN_MIN > 0x0,
40 	     "Can't test expected failures for switch pattern length smaller or equal to zero.");
41 #define SWITCH_PATTERN_LEN_TOO_SHORT (BT_HCI_LE_SWITCH_PATTERN_LEN_MIN - 1)
42 
send_set_conn_cte_rx_params(uint16_t conn_handle,const struct ut_bt_df_conn_cte_rx_params * params,bool enable)43 int send_set_conn_cte_rx_params(uint16_t conn_handle,
44 				const struct ut_bt_df_conn_cte_rx_params *params, bool enable)
45 {
46 	struct bt_hci_cp_le_set_conn_cte_rx_params *cp;
47 	struct net_buf *buf;
48 
49 	uint8_t ant_ids_num = (params != NULL ? params->switch_pattern_len : 0);
50 
51 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_CONN_CTE_RX_PARAMS, sizeof(*cp) + ant_ids_num);
52 	if (!buf) {
53 		return -ENOBUFS;
54 	}
55 
56 	cp = net_buf_add(buf, sizeof(*cp));
57 	(void)memset(cp, 0, sizeof(*cp));
58 	cp->handle = sys_cpu_to_le16(conn_handle);
59 	cp->sampling_enable = enable ? 1 : 0;
60 
61 	if (params != NULL) {
62 		cp->slot_durations = params->slot_durations;
63 
64 		if (ant_ids_num) {
65 			uint8_t *dest_ant_ids = net_buf_add(buf, ant_ids_num);
66 
67 			if (params->ant_ids) {
68 				(void)memcpy(dest_ant_ids, params->ant_ids, ant_ids_num);
69 			}
70 		}
71 		cp->switch_pattern_len = params->switch_pattern_len;
72 	}
73 
74 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CONN_CTE_RX_PARAMS, buf, NULL);
75 }
76 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_enable_with_invalid_conn_handle)77 ZTEST(test_hci_set_conn_cte_rx_params, test_set_conn_cte_rx_params_enable_with_invalid_conn_handle)
78 {
79 	int err;
80 
81 	err = send_set_conn_cte_rx_params(CONN_HANDLE_INVALID, &g_params, true);
82 	zassert_equal(err, -EIO,
83 		      "Unexpected error value for set iq sampling params with wrong conn handle");
84 }
85 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_enable_invalid_slot_durations)86 ZTEST(test_hci_set_conn_cte_rx_params, test_set_conn_cte_rx_params_enable_invalid_slot_durations)
87 {
88 	int err;
89 
90 	g_params.slot_durations = ANTENNA_SWITCHING_SLOT_INVALID;
91 
92 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, true);
93 	zassert_equal(err, -EIO,
94 		      "Unexpected error value for set iq sampling params with invalid slot "
95 		      "durations");
96 }
97 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_enable_with_too_long_switch_pattern_len)98 ZTEST(test_hci_set_conn_cte_rx_params,
99 	test_set_conn_cte_rx_params_enable_with_too_long_switch_pattern_len)
100 {
101 	int err;
102 	uint8_t ant_ids[SWITCH_PATTERN_LEN_TOO_LONG] = { 0 };
103 
104 	g_params.switch_pattern_len = SWITCH_PATTERN_LEN_TOO_LONG;
105 	g_params.ant_ids = ant_ids;
106 
107 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, true);
108 	zassert_equal(err, -EIO,
109 		      "Unexpected error value for set iq sampling params with switch pattern set "
110 		      "length beyond max value");
111 }
112 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_enable_with_too_short_switch_pattern_len)113 ZTEST(test_hci_set_conn_cte_rx_params,
114 	test_set_conn_cte_rx_params_enable_with_too_short_switch_pattern_len)
115 {
116 	int err;
117 	uint8_t ant_ids[SWITCH_PATTERN_LEN_TOO_SHORT] = { 0 };
118 
119 	g_params.switch_pattern_len = SWITCH_PATTERN_LEN_TOO_SHORT;
120 	g_params.ant_ids = &ant_ids[0];
121 
122 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, true);
123 	zassert_equal(err, -EIO,
124 		      "Unexpected error value for set iq sampling params with switch pattern set "
125 		      "length below min value");
126 }
127 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_enable_with_ant_ids_ptr_null)128 ZTEST(test_hci_set_conn_cte_rx_params, test_set_conn_cte_rx_params_enable_with_ant_ids_ptr_null)
129 {
130 	int err;
131 
132 	g_params.ant_ids = NULL;
133 
134 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, true);
135 	/* If size of the command buffer equals to expected value, controller is not able to
136 	 * identify wrong or missing antenna IDs. It will use provided values as if they were
137 	 * valid antenna IDs.
138 	 */
139 	zassert_equal(err, 0,
140 		      "Unexpected error value for set iq sampling params with antenna ids "
141 		      "pointing NULL");
142 }
143 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_enable_with_correct_params)144 ZTEST(test_hci_set_conn_cte_rx_params, test_set_conn_cte_rx_params_enable_with_correct_params)
145 {
146 	int err;
147 
148 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, true);
149 	zassert_equal(err, 0,
150 		      "Unexpected error value for set iq sampling params enabled with "
151 		      "correct params");
152 }
153 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_disable_with_correct_params)154 ZTEST(test_hci_set_conn_cte_rx_params, test_set_conn_cte_rx_params_disable_with_correct_params)
155 {
156 	int err;
157 
158 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, false);
159 	zassert_equal(err, 0,
160 		      "Unexpected error value for set iq sampling params disable with "
161 		      "correct params");
162 }
163 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_disable_with_invalid_slot_duration)164 ZTEST(test_hci_set_conn_cte_rx_params,
165 	test_set_conn_cte_rx_params_disable_with_invalid_slot_duration)
166 {
167 	int err;
168 
169 	g_params.slot_durations = ANTENNA_SWITCHING_SLOT_INVALID;
170 
171 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, false);
172 	zassert_equal(err, 0,
173 		      "Unexpected error value for set iq sampling params disable with "
174 		      "invalid slot durations");
175 }
176 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_disable_with_too_long_switch_pattern_len)177 ZTEST(test_hci_set_conn_cte_rx_params,
178 	test_set_conn_cte_rx_params_disable_with_too_long_switch_pattern_len)
179 {
180 	int err;
181 	uint8_t ant_ids[SWITCH_PATTERN_LEN_TOO_LONG] = { 0 };
182 
183 	g_params.switch_pattern_len = SWITCH_PATTERN_LEN_TOO_LONG;
184 	g_params.ant_ids = ant_ids;
185 
186 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, false);
187 	zassert_equal(err, 0,
188 		      "Unexpected error value for set iq sampling params disable with "
189 		      "switch pattern length above max value");
190 }
191 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_disable_with_too_short_switch_pattern_len)192 ZTEST(test_hci_set_conn_cte_rx_params,
193 	test_set_conn_cte_rx_params_disable_with_too_short_switch_pattern_len)
194 {
195 	int err;
196 	uint8_t ant_ids[SWITCH_PATTERN_LEN_TOO_SHORT] = { 0 };
197 
198 	g_params.switch_pattern_len = SWITCH_PATTERN_LEN_TOO_SHORT;
199 	g_params.ant_ids = ant_ids;
200 
201 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, false);
202 	zassert_equal(err, 0,
203 		      "Unexpected error value for set iq sampling params disable with "
204 		      "switch pattern length below min value");
205 }
206 
ZTEST(test_hci_set_conn_cte_rx_params,test_set_conn_cte_rx_params_disable_with_ant_ids_ptr_null)207 ZTEST(test_hci_set_conn_cte_rx_params, test_set_conn_cte_rx_params_disable_with_ant_ids_ptr_null)
208 {
209 	int err;
210 
211 	g_params.ant_ids = NULL;
212 
213 	err = send_set_conn_cte_rx_params(g_conn_handle, &g_params, false);
214 	zassert_equal(err, 0,
215 		      "Unexpected error value for set iq sampling params disable with "
216 		      "antenna ids pointing NULL");
217 }
218 
connection_setup(void * data)219 static void connection_setup(void *data)
220 {
221 	g_params.slot_durations = BT_HCI_LE_ANTENNA_SWITCHING_SLOT_1US;
222 	g_params.switch_pattern_len = ARRAY_SIZE(g_ant_ids);
223 	g_params.ant_ids = g_ant_ids;
224 
225 	g_conn_handle = ut_bt_create_connection();
226 }
227 
connection_teardown(void * data)228 static void connection_teardown(void *data) { ut_bt_destroy_connection(g_conn_handle); }
229 
230 ZTEST_SUITE(test_hci_set_conn_cte_rx_params, NULL, ut_bt_setup, connection_setup,
231 	    connection_teardown, ut_bt_teardown);
232