1 /*
2  * Copyright (c) 2020 Demant
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/types.h>
8 #include <zephyr/ztest.h>
9 
10 #include <zephyr/bluetooth/hci.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/slist.h>
13 #include <zephyr/sys/util.h>
14 #include "hal/ccm.h"
15 
16 #include "util/util.h"
17 #include "util/mem.h"
18 #include "util/memq.h"
19 #include "util/dbuf.h"
20 
21 #include "pdu_df.h"
22 #include "lll/pdu_vendor.h"
23 #include "pdu.h"
24 #include "ll.h"
25 #include "ll_settings.h"
26 #include "ll_feat.h"
27 
28 #include "lll.h"
29 #include "lll/lll_df_types.h"
30 #include "lll_conn.h"
31 #include "lll_conn_iso.h"
32 
33 #include "ull_tx_queue.h"
34 
35 #include "isoal.h"
36 #include "ull_iso_types.h"
37 #include "ull_conn_iso_types.h"
38 
39 #include "ull_conn_types.h"
40 #include "ull_llcp.h"
41 #include "ull_llcp_internal.h"
42 
43 #include "helper_pdu.h"
44 #include "helper_util.h"
45 
46 static struct ll_conn conn;
47 
version_setup(void * data)48 static void version_setup(void *data)
49 {
50 	test_setup(&conn);
51 }
52 
53 /* +-----+                     +-------+            +-----+
54  * | UT  |                     | LL_A  |            | LT  |
55  * +-----+                     +-------+            +-----+
56  *    |                            |                   |
57  *    | Start                      |                   |
58  *    | Version Exchange Proc.     |                   |
59  *    |--------------------------->|                   |
60  *    |                            |                   |
61  *    |                            | LL_VERSION_IND    |
62  *    |                            |------------------>|
63  *    |                            |                   |
64  *    |                            |    LL_VERSION_IND |
65  *    |                            |<------------------|
66  *    |                            |                   |
67  *    |     Version Exchange Proc. |                   |
68  *    |                   Complete |                   |
69  *    |<---------------------------|                   |
70  *    |                            |                   |
71  */
ZTEST(version_central,test_version_exchange_central_loc)72 ZTEST(version_central, test_version_exchange_central_loc)
73 {
74 	uint8_t err;
75 	struct node_tx *tx;
76 	struct node_rx_pdu *ntf;
77 
78 	struct pdu_data_llctrl_version_ind local_version_ind = {
79 		.version_number = LL_VERSION_NUMBER,
80 		.company_id = CONFIG_BT_CTLR_COMPANY_ID,
81 		.sub_version_number = CONFIG_BT_CTLR_SUBVERSION_NUMBER,
82 	};
83 
84 	struct pdu_data_llctrl_version_ind remote_version_ind = {
85 		.version_number = 0x55,
86 		.company_id = 0xABCD,
87 		.sub_version_number = 0x1234,
88 	};
89 
90 	/* Role */
91 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
92 
93 	/* Connect */
94 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
95 
96 	/* Initiate a Version Exchange Procedure */
97 	err = ull_cp_version_exchange(&conn);
98 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
99 
100 	/* Prepare */
101 	event_prepare(&conn);
102 
103 	/* Tx Queue should have one LL Control PDU */
104 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
105 	lt_rx_q_is_empty(&conn);
106 
107 	/* Rx */
108 	lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
109 
110 	/* Done */
111 	event_done(&conn);
112 
113 	/* There should be one host notification */
114 	ut_rx_pdu(LL_VERSION_IND, &ntf, &remote_version_ind);
115 	ut_rx_q_is_empty();
116 
117 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
118 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
119 }
120 
121 /* +-----+                     +-------+            +-----+
122  * | UT  |                     | LL_A  |            | LT  |
123  * +-----+                     +-------+            +-----+
124  *    |                            |                   |
125  *    | Start                      |                   |
126  *    | Version Exchange Proc.     |                   |
127  *    |--------------------------->|                   |
128  *    |                            |                   |
129  *    |                            | LL_VERSION_IND    |
130  *    |                            |------------------>|
131  *    |                            |                   |
132  *    |                            |  LL_<INVALID>_RSP |
133  *    |                            |<------------------|
134  *    |                            |                   |
135  *  ~~~~~~~~~~~~~~~~~~~ TERMINATE CONN ~~~~~~~~~~~~~~~~~~
136  *    |                            |                   |
137  */
ZTEST(version_central,test_version_exchange_central_loc_invalid_rsp)138 ZTEST(version_central, test_version_exchange_central_loc_invalid_rsp)
139 {
140 	uint8_t err;
141 	struct node_tx *tx;
142 
143 	struct pdu_data_llctrl_version_ind local_version_ind = {
144 		.version_number = LL_VERSION_NUMBER,
145 		.company_id = CONFIG_BT_CTLR_COMPANY_ID,
146 		.sub_version_number = CONFIG_BT_CTLR_SUBVERSION_NUMBER,
147 	};
148 
149 	struct pdu_data_llctrl_unknown_rsp unknown_rsp = {
150 		.type = PDU_DATA_LLCTRL_TYPE_VERSION_IND
151 	};
152 
153 	struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
154 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_VERSION_IND,
155 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
156 	};
157 
158 	struct pdu_data_llctrl_reject_ind reject_ind = {
159 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
160 	};
161 
162 	/* Role */
163 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
164 
165 	/* Connect */
166 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
167 
168 	/* Initiate a Version Exchange Procedure */
169 	err = ull_cp_version_exchange(&conn);
170 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
171 
172 	/* Prepare */
173 	event_prepare(&conn);
174 
175 	/* Tx Queue should have one LL Control PDU */
176 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
177 	lt_rx_q_is_empty(&conn);
178 
179 	/* Rx */
180 	lt_tx(LL_UNKNOWN_RSP, &conn, &unknown_rsp);
181 
182 	/* Done */
183 	event_done(&conn);
184 
185 	/* Release Tx */
186 	ull_cp_release_tx(&conn, tx);
187 
188 	/* Termination 'triggered' */
189 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
190 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
191 
192 	/* Clear termination flag for subsequent test cycle */
193 	conn.llcp_terminate.reason_final = 0;
194 
195 	/* There should be no host notifications */
196 	ut_rx_q_is_empty();
197 
198 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
199 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
200 
201 	/* Cheat, to allow second VEX */
202 	conn.llcp.vex.sent = 0;
203 
204 	/* Initiate another Version Exchange Procedure */
205 	err = ull_cp_version_exchange(&conn);
206 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
207 
208 	/* Prepare */
209 	event_prepare(&conn);
210 
211 	/* Tx Queue should have one LL Control PDU */
212 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
213 	lt_rx_q_is_empty(&conn);
214 
215 	/* Rx */
216 	lt_tx(LL_REJECT_EXT_IND, &conn, &reject_ext_ind);
217 
218 	/* Done */
219 	event_done(&conn);
220 
221 	/* Release Tx */
222 	ull_cp_release_tx(&conn, tx);
223 
224 	/* Termination 'triggered' */
225 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
226 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
227 
228 	/* Clear termination flag for subsequent test cycle */
229 	conn.llcp_terminate.reason_final = 0;
230 
231 	/* There should be no host notifications */
232 	ut_rx_q_is_empty();
233 
234 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
235 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
236 
237 	/* Cheat, to allow second VEX */
238 	conn.llcp.vex.sent = 0;
239 
240 	/* Initiate yet another Version Exchange Procedure */
241 	err = ull_cp_version_exchange(&conn);
242 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
243 
244 	/* Prepare */
245 	event_prepare(&conn);
246 
247 	/* Tx Queue should have one LL Control PDU */
248 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
249 	lt_rx_q_is_empty(&conn);
250 
251 	/* Rx */
252 	lt_tx(LL_REJECT_IND, &conn, &reject_ind);
253 
254 	/* Done */
255 	event_done(&conn);
256 
257 	/* Release Tx */
258 	ull_cp_release_tx(&conn, tx);
259 
260 	/* Termination 'triggered' */
261 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
262 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
263 
264 	/* There should be no host notifications */
265 	ut_rx_q_is_empty();
266 
267 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
268 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
269 }
270 
ZTEST(version_central,test_version_exchange_central_loc_2)271 ZTEST(version_central, test_version_exchange_central_loc_2)
272 {
273 	uint8_t err;
274 
275 	ull_cp_init();
276 	ull_tx_q_init(&conn.tx_q);
277 	ull_llcp_init(&conn);
278 
279 	err = ull_cp_version_exchange(&conn);
280 
281 	for (int i = 0U; i < CONFIG_BT_CTLR_LLCP_LOCAL_PROC_CTX_BUF_NUM; i++) {
282 		zassert_equal(err, BT_HCI_ERR_SUCCESS);
283 		err = ull_cp_version_exchange(&conn);
284 	}
285 
286 	zassert_not_equal(err, BT_HCI_ERR_SUCCESS, NULL);
287 
288 	zassert_equal(llcp_ctx_buffers_free(),
289 		      test_ctx_buffers_cnt() - CONFIG_BT_CTLR_LLCP_LOCAL_PROC_CTX_BUF_NUM,
290 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
291 }
292 
293 /* +-----+ +-------+            +-----+
294  * | UT  | | LL_A  |            | LT  |
295  * +-----+ +-------+            +-----+
296  *    |        |                   |
297  *    |        |    LL_VERSION_IND |
298  *    |        |<------------------|
299  *    |        |                   |
300  *    |        | LL_VERSION_IND    |
301  *    |        |------------------>|
302  *    |        |                   |
303  */
ZTEST(version_central,test_version_exchange_central_rem)304 ZTEST(version_central, test_version_exchange_central_rem)
305 {
306 	struct node_tx *tx;
307 
308 	struct pdu_data_llctrl_version_ind local_version_ind = {
309 		.version_number = LL_VERSION_NUMBER,
310 		.company_id = CONFIG_BT_CTLR_COMPANY_ID,
311 		.sub_version_number = CONFIG_BT_CTLR_SUBVERSION_NUMBER,
312 	};
313 
314 	struct pdu_data_llctrl_version_ind remote_version_ind = {
315 		.version_number = 0x55,
316 		.company_id = 0xABCD,
317 		.sub_version_number = 0x1234,
318 	};
319 
320 	/* Role */
321 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
322 
323 	/* Connect */
324 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
325 
326 	/* Prepare */
327 	event_prepare(&conn);
328 
329 	/* Rx */
330 	lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
331 
332 	/* Done */
333 	event_done(&conn);
334 
335 	/* Prepare */
336 	event_prepare(&conn);
337 
338 	/* Tx Queue should have one LL Control PDU */
339 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
340 	lt_rx_q_is_empty(&conn);
341 
342 	/* Done */
343 	event_done(&conn);
344 
345 	/* There should not be a host notifications */
346 	ut_rx_q_is_empty();
347 
348 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
349 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
350 }
351 
352 /* +-----+                     +-------+            +-----+
353  * | UT  |                     | LL_A  |            | LT  |
354  * +-----+                     +-------+            +-----+
355  *    |                            |                   |
356  *    |                            |    LL_VERSION_IND |
357  *    |                            |<------------------|
358  *    |                            |                   |
359  *    |                            | LL_VERSION_IND    |
360  *    |                            |------------------>|
361  *    |                            |                   |
362  *    | Start                      |                   |
363  *    | Version Exchange Proc.     |                   |
364  *    |--------------------------->|                   |
365  *    |                            |                   |
366  *    |     Version Exchange Proc. |                   |
367  *    |                   Complete |                   |
368  *    |<---------------------------|                   |
369  *    |                            |                   |
370  */
ZTEST(version_central,test_version_exchange_central_rem_2)371 ZTEST(version_central, test_version_exchange_central_rem_2)
372 {
373 	uint8_t err;
374 	struct node_tx *tx;
375 	struct node_rx_pdu *ntf;
376 
377 	struct pdu_data_llctrl_version_ind local_version_ind = {
378 		.version_number = LL_VERSION_NUMBER,
379 		.company_id = CONFIG_BT_CTLR_COMPANY_ID,
380 		.sub_version_number = CONFIG_BT_CTLR_SUBVERSION_NUMBER,
381 	};
382 
383 	struct pdu_data_llctrl_version_ind remote_version_ind = {
384 		.version_number = 0x55,
385 		.company_id = 0xABCD,
386 		.sub_version_number = 0x1234,
387 	};
388 
389 	/* Role */
390 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
391 
392 	/* Connect */
393 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
394 
395 	/* Rx */
396 	lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
397 
398 	/* Initiate a Version Exchange Procedure */
399 	err = ull_cp_version_exchange(&conn);
400 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
401 
402 	/* Prepare */
403 	event_prepare(&conn);
404 
405 	/* Tx Queue should have one LL Control PDU */
406 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
407 	lt_rx_q_is_empty(&conn);
408 
409 	/* Done */
410 	event_done(&conn);
411 
412 	/* There should be one host notification */
413 	ut_rx_pdu(LL_VERSION_IND, &ntf, &remote_version_ind);
414 	ut_rx_q_is_empty();
415 
416 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
417 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
418 }
419 
420 /* +-----+                     +-------+            +-----+
421  * | UT  |                     | LL_A  |            | LT  |
422  * +-----+                     +-------+            +-----+
423  *    |                            |                   |
424  *    | Start                      |                   |
425  *    | Version Exchange Proc.     |                   |
426  *    |--------------------------->|                   |
427  *    |                            |                   |
428  *    |                            | LL_VERSION_IND    |
429  *    |                            |------------------>|
430  *    |                            |                   |
431  *    |                            |    LL_VERSION_IND |
432  *    |                            |<------------------|
433  *    |                            |                   |
434  *    |     Version Exchange Proc. |                   |
435  *    |                   Complete |                   |
436  *    |<---------------------------|                   |
437  *    | Start                      |                   |
438  *    | Version Exchange Proc.     |                   |
439  *    |--------------------------->|                   |
440  *    |                            |                   |
441  *    |     Version Exchange Proc. |                   |
442  *    |                   Complete |                   |
443  *    |<---------------------------|                   |
444  *    |                            |                   |
445  */
ZTEST(version_central,test_version_exchange_central_loc_twice)446 ZTEST(version_central, test_version_exchange_central_loc_twice)
447 {
448 	uint8_t err;
449 	struct node_tx *tx;
450 	struct node_rx_pdu *ntf;
451 
452 	struct pdu_data_llctrl_version_ind local_version_ind = {
453 		.version_number = LL_VERSION_NUMBER,
454 		.company_id = CONFIG_BT_CTLR_COMPANY_ID,
455 		.sub_version_number = CONFIG_BT_CTLR_SUBVERSION_NUMBER,
456 	};
457 
458 	struct pdu_data_llctrl_version_ind remote_version_ind = {
459 		.version_number = 0x55,
460 		.company_id = 0xABCD,
461 		.sub_version_number = 0x1234,
462 	};
463 
464 	/* Role */
465 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
466 
467 	/* Connect */
468 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
469 
470 	/* Initiate a Version Exchange Procedure */
471 	err = ull_cp_version_exchange(&conn);
472 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
473 
474 	/* Initiate a Version Exchange Procedure */
475 	err = ull_cp_version_exchange(&conn);
476 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
477 
478 	/* Prepare */
479 	event_prepare(&conn);
480 
481 	/* Tx Queue should have one LL Control PDU */
482 	lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
483 	lt_rx_q_is_empty(&conn);
484 
485 	/* Rx */
486 	lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
487 
488 	/* Done */
489 	event_done(&conn);
490 
491 	/* There should be one host notification */
492 	ut_rx_pdu(LL_VERSION_IND, &ntf, &remote_version_ind);
493 	ut_rx_q_is_empty();
494 
495 	/* Prepare */
496 	event_prepare(&conn);
497 
498 	/* Done */
499 	event_done(&conn);
500 
501 	/* Cached values should be used, no over the air comm */
502 	lt_rx_q_is_empty(&conn);
503 
504 	/* There should be one host notification */
505 	ut_rx_pdu(LL_VERSION_IND, &ntf, &remote_version_ind);
506 	ut_rx_q_is_empty();
507 
508 	/* Second attempt to run the version exchange completes immediately in idle state.
509 	 * The context is released just after that.
510 	 */
511 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
512 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
513 }
514 
515 ZTEST_SUITE(version_central, NULL, NULL, version_setup, NULL, NULL);
516