1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
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 
27 #include "lll.h"
28 #include "ll_feat.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 #include "ull_conn_types.h"
39 #include "ull_llcp.h"
40 #include "ull_conn_internal.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 
cte_req_setup(void * data)48 static void cte_req_setup(void *data)
49 {
50 	test_setup(&conn);
51 
52 	/* Set CTE request enable as if it was called by Host */
53 	conn.llcp.cte_req.is_enabled = 1U;
54 }
55 
fex_setup(void * data)56 static void fex_setup(void *data)
57 {
58 	cte_req_setup(data);
59 
60 	/* Emulate valid feature exchange and all features valid for local and peer devices */
61 	memset(&conn.llcp.fex, 0, sizeof(conn.llcp.fex));
62 	conn.llcp.fex.features_used = LL_FEAT;
63 	conn.llcp.fex.features_peer = LL_FEAT;
64 
65 	conn.llcp.fex.valid = 1;
66 }
67 
68 /* Tests of successful execution of CTE Request Procedure */
69 
70 /* +-----+                     +-------+            +-----+
71  * | UT  |                     | LL_A  |            | LT  |
72  * +-----+                     +-------+            +-----+
73  *    |                            |                   |
74  *    | Start initiation           |                   |
75  *    | CTE Request Proc.          |                   |
76  *    |--------------------------->|                   |
77  *    |                            |                   |
78  *    |                            | LL_LE_CTE_REQ     |
79  *    |                            |------------------>|
80  *    |                            |                   |
81  *    |                            |    LL_LE_CTE_RSP  |
82  *    |                            |<------------------|
83  *    |                            |                   |
84  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85  *    |                            |                   |
86  *    | LE Connection IQ Report    |                   |
87  *    |<---------------------------|                   |
88  *    |                            |                   |
89  *    |                            |                   |
90  */
ZTEST(cte_req_after_fex,test_cte_req_central_local)91 ZTEST(cte_req_after_fex, test_cte_req_central_local)
92 {
93 	uint8_t err;
94 	struct node_tx *tx;
95 
96 	struct pdu_data_llctrl_cte_req local_cte_req = {
97 		.cte_type_req = BT_HCI_LE_AOA_CTE,
98 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
99 	};
100 	struct pdu_data_llctrl_cte_rsp remote_cte_rsp = {};
101 	struct node_rx_pdu *ntf;
102 
103 	/* Role */
104 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
105 
106 	/* Connect */
107 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
108 
109 
110 	/* Initiate an CTE Request Procedure */
111 	conn.llcp.cte_req.is_enabled = 1U;
112 
113 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
114 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
115 
116 	/* Prepare */
117 	event_prepare(&conn);
118 
119 	/* Tx Queue should have one LL Control PDU */
120 	lt_rx(LL_CTE_REQ, &conn, &tx, &local_cte_req);
121 	lt_rx_q_is_empty(&conn);
122 
123 	/* Rx */
124 	lt_tx(LL_CTE_RSP, &conn, &remote_cte_rsp);
125 
126 	/* Done */
127 	event_done(&conn);
128 
129 	/* Receive notification of sampled CTE response */
130 	ut_rx_pdu(LL_CTE_RSP, &ntf, &remote_cte_rsp);
131 
132 	/* The RX queue should be empty now */
133 	ut_rx_q_is_empty();
134 
135 	/* Release Ntf */
136 	release_ntf(ntf);
137 
138 	/* Release tx node */
139 	ull_cp_release_tx(&conn, tx);
140 
141 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
142 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
143 }
144 
145 /* +-----+                     +-------+            +-----+
146  * | UT  |                     | LL_A  |            | LT  |
147  * +-----+                     +-------+            +-----+
148  *    |                            |                   |
149  *    | Start initiator            |                   |
150  *    | CTE Request Proc.          |                   |
151  *    |--------------------------->|                   |
152  *    |                            |                   |
153  *    |                            | LL_LE_CTE_REQ     |
154  *    |                            |------------------>|
155  *    |                            |                   |
156  *    |                            |    LL_LE_CTE_RSP  |
157  *    |                            |<------------------|
158  *    |                            |                   |
159  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160  *    |                            |                   |
161  *    | LE Connection IQ Report    |                   |
162  *    |<---------------------------|                   |
163  *    |                            |                   |
164  *    |                            |                   |
165  */
ZTEST(cte_req_after_fex,test_cte_req_peripheral_local)166 ZTEST(cte_req_after_fex, test_cte_req_peripheral_local)
167 {
168 	uint8_t err;
169 	struct node_tx *tx;
170 
171 	struct pdu_data_llctrl_cte_req local_cte_req = {
172 		.cte_type_req = BT_HCI_LE_AOA_CTE,
173 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
174 	};
175 
176 	struct pdu_data_llctrl_cte_rsp remote_cte_rsp = {};
177 	struct node_rx_pdu *ntf;
178 
179 	/* Role */
180 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
181 
182 	/* Connect */
183 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
184 
185 	/* Initiate an CTE Request Procedure */
186 	conn.llcp.cte_req.is_enabled = 1U;
187 
188 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
189 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
190 
191 	/* Prepare */
192 	event_prepare(&conn);
193 
194 	/* Tx Queue should have one LL Control PDU */
195 	lt_rx(LL_CTE_REQ, &conn, &tx, &local_cte_req);
196 	lt_rx_q_is_empty(&conn);
197 
198 	/* Rx */
199 	lt_tx(LL_CTE_RSP, &conn, &remote_cte_rsp);
200 
201 	/* Done */
202 	event_done(&conn);
203 
204 	/* Receive notification of sampled CTE response */
205 	ut_rx_pdu(LL_CTE_RSP, &ntf, &remote_cte_rsp);
206 
207 	/* The RX queue should be empty now */
208 	ut_rx_q_is_empty();
209 
210 	/* Release Ntf */
211 	release_ntf(ntf);
212 
213 	/* Release tx node */
214 	ull_cp_release_tx(&conn, tx);
215 
216 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
217 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
218 }
219 
220 /* +-----+                     +-------+            +-----+
221  * | UT  |                     | LL_A  |            | LT  |
222  * +-----+                     +-------+            +-----+
223  *    |                            |                   |
224  *    | Start responder            |                   |
225  *    | CTE Request Proc.          |                   |
226  *    |--------------------------->|                   |
227  *    |                            |                   |
228  *    |                            | LL_LE_CTE_REQ     |
229  *    |                            |<------------------|
230  *    |                            |                   |
231  *    |                            |    LL_LE_CTE_RSP  |
232  *    |                            |------------------>|
233  *    |                            |                   |
234  *    |                            |                   |
235  */
ZTEST(cte_req_after_fex,test_cte_req_central_remote)236 ZTEST(cte_req_after_fex, test_cte_req_central_remote)
237 {
238 	struct node_tx *tx;
239 
240 	struct pdu_data_llctrl_cte_req local_cte_req = {
241 		.cte_type_req = BT_HCI_LE_AOA_CTE,
242 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
243 	};
244 
245 	struct pdu_data_llctrl_cte_rsp remote_cte_rsp = {};
246 
247 	/* Role */
248 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
249 
250 	/* Connect */
251 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
252 
253 	/* Enable response for CTE request */
254 	ull_cp_cte_rsp_enable(&conn, true, BT_HCI_LE_CTE_LEN_MAX,
255 			      (BT_HCI_LE_AOA_CTE | BT_HCI_LE_AOD_CTE_1US | BT_HCI_LE_AOD_CTE_2US));
256 
257 	/* Prepare */
258 	event_prepare(&conn);
259 
260 	/* Tx */
261 	lt_tx(LL_CTE_REQ, &conn, &local_cte_req);
262 
263 	/* Done */
264 	event_done(&conn);
265 
266 	/* Prepare */
267 	event_prepare(&conn);
268 
269 	/* Tx Queue should have one LL Control PDU */
270 	lt_rx(LL_CTE_RSP, &conn, &tx, &remote_cte_rsp);
271 	lt_rx_q_is_empty(&conn);
272 
273 	/* TX Ack */
274 	event_tx_ack(&conn, tx);
275 
276 	/* Done */
277 	event_done(&conn);
278 
279 	/* Release tx node */
280 	ull_cp_release_tx(&conn, tx);
281 
282 	/* There should not be a host notifications */
283 	ut_rx_q_is_empty();
284 
285 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
286 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
287 }
288 
289 /* +-----+                     +-------+            +-----+
290  * | UT  |                     | LL_A  |            | LT  |
291  * +-----+                     +-------+            +-----+
292  *    |                            |                   |
293  *    | Start responder            |                   |
294  *    | CTE Request Proc.          |                   |
295  *    |--------------------------->|                   |
296  *    |                            |                   |
297  *    |                            | LL_LE_CTE_REQ     |
298  *    |                            |<------------------|
299  *    |                            |                   |
300  *    |                            |    LL_LE_CTE_RSP  |
301  *    |                            |------------------>|
302  *    |                            |                   |
303  *    |                            |                   |
304  */
ZTEST(cte_req_after_fex,test_cte_req_peripheral_remote)305 ZTEST(cte_req_after_fex, test_cte_req_peripheral_remote)
306 {
307 	struct node_tx *tx;
308 
309 	struct pdu_data_llctrl_cte_req local_cte_req = {
310 		.cte_type_req = BT_HCI_LE_AOA_CTE,
311 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
312 	};
313 
314 	struct pdu_data_llctrl_cte_rsp remote_cte_rsp = {};
315 
316 	/* Role */
317 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
318 
319 	/* Connect */
320 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
321 
322 	/* Enable response for CTE request */
323 	ull_cp_cte_rsp_enable(&conn, true, BT_HCI_LE_CTE_LEN_MAX,
324 			      (BT_HCI_LE_AOA_CTE | BT_HCI_LE_AOD_CTE_1US | BT_HCI_LE_AOD_CTE_2US));
325 
326 	/* Prepare */
327 	event_prepare(&conn);
328 
329 	/* Tx */
330 	lt_tx(LL_CTE_REQ, &conn, &local_cte_req);
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_CTE_RSP, &conn, &tx, &remote_cte_rsp);
340 	lt_rx_q_is_empty(&conn);
341 
342 	/* TX Ack */
343 	event_tx_ack(&conn, tx);
344 
345 	/* Done */
346 	event_done(&conn);
347 
348 	/* Release tx node */
349 	ull_cp_release_tx(&conn, tx);
350 
351 	/* There should not be a host notifications */
352 	ut_rx_q_is_empty();
353 
354 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
355 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
356 }
357 
358 /* Tests of expected failures during execution of CTE Request Procedure */
359 
360 /* +-----+                     +-------+                         +-----+
361  * | UT  |                     | LL_A  |                         | LT  |
362  * +-----+                     +-------+                         +-----+
363  *    |                            |                                |
364  *    | Start initiation           |                                |
365  *    | CTE Request Proc.          |                                |
366  *    |--------------------------->|                                |
367  *    |                            |                                |
368  *    |                            | LL_LE_CTE_REQ                  |
369  *    |                            |------------------------------->|
370  *    |                            |                                |
371  *    |                            | LL_REJECT_EXT_IND              |
372  *    |                            | BT_HCI_ERR_UNSUPP_LL_PARAM_VAL |
373  *    |                            | or BT_HCI_ERR_INVALID_LL_PARAM |
374  *    |                            |<-------------------------------|
375  *    |                            |                                |
376  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
377  *    |                            |                                |
378  *    | LE CTE Request Failed      |                                |
379  *    |<---------------------------|                                |
380  *    |                            |                                |
381  *    |                            |                                |
382  */
ZTEST(cte_req_after_fex,test_cte_req_rejected_inv_ll_param_central_local)383 ZTEST(cte_req_after_fex, test_cte_req_rejected_inv_ll_param_central_local)
384 {
385 	uint8_t err;
386 	struct node_tx *tx;
387 
388 	struct pdu_data_llctrl_cte_req local_cte_req = {
389 		.cte_type_req = BT_HCI_LE_AOD_CTE_1US,
390 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
391 	};
392 	struct pdu_data_llctrl_reject_ext_ind remote_reject_ext_ind = {
393 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_CTE_REQ,
394 		.error_code = BT_HCI_ERR_UNSUPP_LL_PARAM_VAL,
395 	};
396 	struct node_rx_pdu *ntf;
397 
398 	/* Role */
399 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
400 
401 	/* Connect */
402 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
403 
404 	/* Initiate an CTE Request Procedure */
405 	conn.llcp.cte_req.is_enabled = 1U;
406 
407 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
408 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
409 
410 	/* Prepare */
411 	event_prepare(&conn);
412 
413 	/* Tx Queue should have one LL Control PDU */
414 	lt_rx(LL_CTE_REQ, &conn, &tx, &local_cte_req);
415 	lt_rx_q_is_empty(&conn);
416 
417 	/* Rx */
418 	lt_tx(LL_REJECT_EXT_IND, &conn, &remote_reject_ext_ind);
419 
420 	/* Done */
421 	event_done(&conn);
422 
423 	/* Receive notification of sampled CTE response */
424 	ut_rx_pdu(LL_REJECT_EXT_IND, &ntf, &remote_reject_ext_ind);
425 
426 	/* The RX queue should be empty now */
427 	ut_rx_q_is_empty();
428 
429 	/* Release Ntf */
430 	release_ntf(ntf);
431 
432 	/* Release tx node */
433 	ull_cp_release_tx(&conn, tx);
434 
435 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
436 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
437 }
438 
439 /* +-----+                     +-------+                         +-----+
440  * | UT  |                     | LL_A  |                         | LT  |
441  * +-----+                     +-------+                         +-----+
442  *    |                            |                                |
443  *    | Start initiation           |                                |
444  *    | CTE Request Proc.          |                                |
445  *    |--------------------------->|                                |
446  *    |                            |                                |
447  *    |                            | LL_LE_CTE_REQ                  |
448  *    |                            |------------------------------->|
449  *    |                            |                                |
450  *    |                            | LL_REJECT_EXT_IND              |
451  *    |                            | BT_HCI_ERR_UNSUPP_LL_PARAM_VAL |
452  *    |                            | or BT_HCI_ERR_INVALID_LL_PARAM |
453  *    |                            |<-------------------------------|
454  *    |                            |                                |
455  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
456  *    |                            |                                |
457  *    | LE CTE Request Failed      |                                |
458  *    |<---------------------------|                                |
459  *    |                            |                                |
460  *    |                            |                                |
461  */
ZTEST(cte_req_after_fex,test_cte_req_rejected_inv_ll_param_peripheral_local)462 ZTEST(cte_req_after_fex, test_cte_req_rejected_inv_ll_param_peripheral_local)
463 {
464 	uint8_t err;
465 	struct node_tx *tx;
466 
467 	struct pdu_data_llctrl_cte_req local_cte_req = {
468 		.cte_type_req = BT_HCI_LE_AOD_CTE_1US,
469 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
470 	};
471 	struct pdu_data_llctrl_reject_ext_ind remote_reject_ext_ind = {
472 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_CTE_REQ,
473 		.error_code = BT_HCI_ERR_UNSUPP_LL_PARAM_VAL,
474 	};
475 	struct node_rx_pdu *ntf;
476 
477 	/* Role */
478 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
479 
480 	/* Connect */
481 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
482 
483 	/* Initiate an CTE Request Procedure */
484 	conn.llcp.cte_req.is_enabled = 1U;
485 
486 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
487 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
488 
489 	/* Prepare */
490 	event_prepare(&conn);
491 
492 	/* Tx Queue should have one LL Control PDU */
493 	lt_rx(LL_CTE_REQ, &conn, &tx, &local_cte_req);
494 	lt_rx_q_is_empty(&conn);
495 
496 	/* Rx */
497 	lt_tx(LL_REJECT_EXT_IND, &conn, &remote_reject_ext_ind);
498 
499 	/* Done */
500 	event_done(&conn);
501 
502 	/* Receive notification of sampled CTE response */
503 	ut_rx_pdu(LL_REJECT_EXT_IND, &ntf, &remote_reject_ext_ind);
504 
505 	/* The RX queue should be empty now */
506 	ut_rx_q_is_empty();
507 
508 	/* Release Ntf */
509 	release_ntf(ntf);
510 
511 	/* Release tx node */
512 	ull_cp_release_tx(&conn, tx);
513 
514 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
515 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
516 }
517 
518 /* +-----+                     +-------+                         +-----+
519  * | UT  |                     | LL_A  |                         | LT  |
520  * +-----+                     +-------+                         +-----+
521  *    |                            |                                |
522  *    | Start initiation           |                                |
523  *    | CTE Reqest Proc.           |                                |
524  *    |--------------------------->|                                |
525  *    |                            |                                |
526  *    |                            | LL_LE_CTE_REQ                  |
527  *    |                            |<-------------------------------|
528  *    |                            |                                |
529  *    |                            | LL_REJECT_EXT_IND              |
530  *    |                            | BT_HCI_ERR_UNSUPP_LL_PARAM_VAL |
531  *    |                            |------------------------------->|
532  *    |                            |                                |
533  */
ZTEST(cte_req_after_fex,test_cte_req_reject_inv_ll_param_central_remote)534 ZTEST(cte_req_after_fex, test_cte_req_reject_inv_ll_param_central_remote)
535 {
536 	struct node_tx *tx;
537 
538 	struct pdu_data_llctrl_cte_req local_cte_req = {
539 		.cte_type_req = BT_HCI_LE_AOD_CTE_2US,
540 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
541 	};
542 
543 	struct pdu_data_llctrl_reject_ext_ind remote_reject_ext_ind = {
544 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_CTE_REQ,
545 		.error_code = BT_HCI_ERR_UNSUPP_LL_PARAM_VAL,
546 	};
547 
548 	/* Role */
549 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
550 
551 	/* Connect */
552 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
553 
554 	/* Enable response for CTE request */
555 	ull_cp_cte_rsp_enable(&conn, true, BT_HCI_LE_CTE_LEN_MAX,
556 			      (BT_HCI_LE_AOA_CTE_RSP | BT_HCI_LE_AOD_CTE_RSP_1US));
557 
558 	/* Prepare */
559 	event_prepare(&conn);
560 
561 	/* Tx */
562 	lt_tx(LL_CTE_REQ, &conn, &local_cte_req);
563 
564 	/* Done */
565 	event_done(&conn);
566 
567 	/* Prepare */
568 	event_prepare(&conn);
569 
570 	/* Tx Queue should have one LL Control PDU */
571 	lt_rx(LL_REJECT_EXT_IND, &conn, &tx, &remote_reject_ext_ind);
572 	lt_rx_q_is_empty(&conn);
573 
574 	/* TX Ack */
575 	event_tx_ack(&conn, tx);
576 
577 	/* Done */
578 	event_done(&conn);
579 
580 	/* Release tx node */
581 	ull_cp_release_tx(&conn, tx);
582 
583 	/* There should not be a host notifications */
584 	ut_rx_q_is_empty();
585 
586 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
587 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
588 }
589 
590 /* +-----+                     +-------+                         +-----+
591  * | UT  |                     | LL_A  |                         | LT  |
592  * +-----+                     +-------+                         +-----+
593  *    |                            |                                |
594  *    | Start initiation           |                                |
595  *    | CTE Reqest Proc.           |                                |
596  *    |--------------------------->|                                |
597  *    |                            |                                |
598  *    |                            | LL_LE_CTE_REQ                  |
599  *    |                            |<-------------------------------|
600  *    |                            |                                |
601  *    |                            | LL_REJECT_EXT_IND              |
602  *    |                            | BT_HCI_ERR_UNSUPP_LL_PARAM_VAL |
603  *    |                            |------------------------------->|
604  *    |                            |                                |
605  */
ZTEST(cte_req_after_fex,test_cte_req_reject_inv_ll_param_peripheral_remote)606 ZTEST(cte_req_after_fex, test_cte_req_reject_inv_ll_param_peripheral_remote)
607 {
608 	struct node_tx *tx;
609 
610 	struct pdu_data_llctrl_cte_req local_cte_req = {
611 		.cte_type_req = BT_HCI_LE_AOD_CTE_2US,
612 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
613 	};
614 
615 	struct pdu_data_llctrl_reject_ext_ind remote_reject_ext_ind = {
616 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_CTE_REQ,
617 		.error_code = BT_HCI_ERR_UNSUPP_LL_PARAM_VAL,
618 	};
619 
620 	/* Role */
621 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
622 
623 	/* Connect */
624 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
625 
626 	/* Enable response for CTE request */
627 	ull_cp_cte_rsp_enable(&conn, true, BT_HCI_LE_CTE_LEN_MAX,
628 			      (BT_HCI_LE_AOA_CTE | BT_HCI_LE_AOD_CTE_1US));
629 
630 	/* Prepare */
631 	event_prepare(&conn);
632 
633 	/* Tx */
634 	lt_tx(LL_CTE_REQ, &conn, &local_cte_req);
635 
636 	/* Done */
637 	event_done(&conn);
638 
639 	/* Prepare */
640 	event_prepare(&conn);
641 
642 	/* Tx Queue should have one LL Control PDU */
643 	lt_rx(LL_REJECT_EXT_IND, &conn, &tx, &remote_reject_ext_ind);
644 	lt_rx_q_is_empty(&conn);
645 
646 	/* TX Ack */
647 	event_tx_ack(&conn, tx);
648 
649 	/* Done */
650 	event_done(&conn);
651 
652 	/* Release tx node */
653 	ull_cp_release_tx(&conn, tx);
654 
655 	/* There should not be a host notifications */
656 	ut_rx_q_is_empty();
657 
658 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
659 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
660 }
661 
662 /* +-----+                     +-------+                         +-----+
663  * | UT  |                     | LL_A  |                         | LT  |
664  * +-----+                     +-------+                         +-----+
665  *    |                            |                                |
666  *    | Start initiation           |                                |
667  *    | CTE Request Proc.          |                                |
668  *    |--------------------------->|                                |
669  *    |                            |                                |
670  *    |                            | LL_LE_CTE_REQ                  |
671  *    |                            |------------------------------->|
672  *    |                            |                                |
673  *    |                            | LL_UNKNOWN_RSP                 |
674  *    |                            |<-------------------------------|
675  *    |                            |                                |
676  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
677  *    |                            |                                |
678  *    | LE CTE Request Failed      |                                |
679  *    |<---------------------------|                                |
680  *    |                            |                                |
681  *    |                            |                                |
682  */
test_cte_req_ll_unknown_rsp_local(uint8_t role)683 static void test_cte_req_ll_unknown_rsp_local(uint8_t role)
684 {
685 	uint8_t err;
686 	struct node_tx *tx;
687 
688 	struct pdu_data_llctrl_cte_req local_cte_req = {
689 		.cte_type_req = BT_HCI_LE_AOD_CTE_1US,
690 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
691 	};
692 
693 	struct pdu_data_llctrl_unknown_rsp unknown_rsp = { .type = PDU_DATA_LLCTRL_TYPE_CTE_REQ };
694 	struct node_rx_pdu *ntf;
695 
696 	/* Role */
697 	test_set_role(&conn, role);
698 
699 	/* Connect */
700 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
701 
702 	/* Initiate an CTE Request Procedure */
703 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
704 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
705 
706 	/* Prepare */
707 	event_prepare(&conn);
708 
709 	/* Tx Queue should have one LL Control PDU */
710 	lt_rx(LL_CTE_REQ, &conn, &tx, &local_cte_req);
711 	lt_rx_q_is_empty(&conn);
712 
713 	/* Rx */
714 	lt_tx(LL_UNKNOWN_RSP, &conn, &unknown_rsp);
715 
716 	/* Done */
717 	event_done(&conn);
718 
719 	/* Receive notification of reception of unknown response. The notification is changed to
720 	 * HCI_LE_CTE_Request_Failed before send to host by HCI. This is why it is verified if CTE
721 	 * request state machine sends LL_UNKNOWN_RSP towards host.
722 	 */
723 	ut_rx_pdu(LL_UNKNOWN_RSP, &ntf, &unknown_rsp);
724 
725 	/* The RX queue should be empty now */
726 	ut_rx_q_is_empty();
727 
728 	/* Release Ntf */
729 	release_ntf(ntf);
730 
731 	/* Release tx node */
732 	ull_cp_release_tx(&conn, tx);
733 
734 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(), "Free CTX buffers %d",
735 		      llcp_ctx_buffers_free());
736 
737 	/* Verify that CTE response feature is marked as not supported by peer device */
738 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
739 	zassert_equal(err, BT_HCI_ERR_UNSUPP_REMOTE_FEATURE);
740 }
741 
ZTEST(cte_req,test_cte_req_ll_unknown_rsp_central_local)742 ZTEST(cte_req, test_cte_req_ll_unknown_rsp_central_local)
743 {
744 	test_cte_req_ll_unknown_rsp_local(BT_HCI_ROLE_CENTRAL);
745 }
746 
ZTEST(cte_req,test_cte_req_ll_unknown_rsp_peripheral_local)747 ZTEST(cte_req, test_cte_req_ll_unknown_rsp_peripheral_local)
748 {
749 	test_cte_req_ll_unknown_rsp_local(BT_HCI_ROLE_PERIPHERAL);
750 }
751 
752 /* Tests related with PHY update procedure and CTE request procedure "collision" */
753 
754 #define PREFER_S2_CODING 0U
755 #define HOST_INITIATED 1U
756 #define PHY_UPDATE_INSTANT_DELTA 6U
757 #define PHY_PREFER_ANY (PHY_1M | PHY_2M | PHY_CODED)
758 /* Arbitrary value used for setting effective maximum number of TX/RX octets */
759 #define PDU_PDU_MAX_OCTETS (PDU_DC_PAYLOAD_SIZE_MIN * 3)
760 
check_pref_phy_state(const struct ll_conn * conn,uint8_t phy_tx,uint8_t phy_rx)761 static void check_pref_phy_state(const struct ll_conn *conn, uint8_t phy_tx, uint8_t phy_rx)
762 {
763 	zassert_equal(conn->phy_pref_rx, phy_rx,
764 		      "Preferred RX PHY mismatch %d (actual) != %d (expected)", conn->phy_pref_rx,
765 		      phy_rx);
766 	zassert_equal(conn->phy_pref_tx, phy_tx,
767 		      "Preferred TX PHY mismatch %d (actual) != %d (expected)", conn->phy_pref_tx,
768 		      phy_tx);
769 }
770 
check_current_phy_state(const struct ll_conn * conn,uint8_t phy_tx,uint8_t flags,uint8_t phy_rx)771 static void check_current_phy_state(const struct ll_conn *conn, uint8_t phy_tx, uint8_t flags,
772 				    uint8_t phy_rx)
773 {
774 	zassert_equal(conn->lll.phy_rx, phy_rx,
775 		      "Current RX PHY mismatch %d (actual) != %d (expected)", conn->lll.phy_rx,
776 		      phy_rx);
777 	zassert_equal(conn->lll.phy_tx, phy_tx,
778 		      "Current TX PHY mismatch %d (actual) != %d (expected)", conn->lll.phy_tx,
779 		      phy_tx);
780 	zassert_equal(conn->lll.phy_flags, flags,
781 		      "Current Flags mismatch %d (actual) != %d (expected)", conn->lll.phy_flags,
782 		      flags);
783 }
784 
is_instant_reached(struct ll_conn * conn,uint16_t instant)785 static bool is_instant_reached(struct ll_conn *conn, uint16_t instant)
786 {
787 	/* Check if instant is in the past.
788 	 *
789 	 * NOTE: If conn_event > instant then subtract operation will result in value greater than
790 	 *       0x7FFF for uint16_t type. This is based on modulo 65536 math. The 0x7FFF is
791 	 *       maximum positive difference between actual value of connection event counter and
792 	 *       instant.
793 	 */
794 	return (uint16_t)(event_counter(conn) - instant) <= (uint16_t)0x7FFF;
795 }
796 
pu_event_counter(struct ll_conn * conn)797 static uint16_t pu_event_counter(struct ll_conn *conn)
798 {
799 	struct lll_conn *lll;
800 	uint16_t event_counter;
801 
802 	lll = &conn->lll;
803 
804 	/* Calculate current event counter */
805 	event_counter = lll->event_counter + lll->latency_prepare;
806 
807 	return event_counter;
808 }
809 
phy_update_setup(void)810 static void phy_update_setup(void)
811 {
812 	/* Emulate initial conn state */
813 	conn.phy_pref_rx = PHY_PREFER_ANY;
814 	conn.phy_pref_tx = PHY_PREFER_ANY;
815 	conn.lll.phy_flags = PREFER_S2_CODING;
816 	conn.lll.phy_tx_time = PHY_1M;
817 	conn.lll.phy_rx = PHY_1M;
818 	conn.lll.phy_tx = PHY_1M;
819 
820 	/* Init DLE data */
821 	ull_conn_default_tx_octets_set(PDU_DC_PAYLOAD_SIZE_MAX);
822 	/* PHY Coded support is enabled hence it limits the max TX time */
823 	ull_conn_default_tx_time_set(PDU_DC_PAYLOAD_TIME_MAX_CODED);
824 	/* Initialize with defauly PHY1M */
825 	ull_dle_init(&conn, PHY_1M);
826 	/* Emulate different remote numbers to trigger update of effective max TX octets and time.
827 	 * Numbers are taken arbitrary.
828 	 */
829 	conn.lll.dle.remote.max_tx_octets = PDU_PDU_MAX_OCTETS;
830 	conn.lll.dle.remote.max_rx_octets = PDU_PDU_MAX_OCTETS;
831 	conn.lll.dle.remote.max_tx_time = PDU_DC_MAX_US(conn.lll.dle.remote.max_tx_octets, PHY_1M);
832 	conn.lll.dle.remote.max_rx_time = PDU_DC_MAX_US(conn.lll.dle.remote.max_rx_octets, PHY_1M);
833 	ull_dle_update_eff(&conn);
834 }
835 
run_local_cte_req(struct pdu_data_llctrl_cte_req * cte_req)836 static void run_local_cte_req(struct pdu_data_llctrl_cte_req *cte_req)
837 {
838 	struct pdu_data_llctrl_cte_rsp remote_cte_rsp = {};
839 	struct node_tx *tx = NULL;
840 	struct node_rx_pdu *ntf;
841 
842 	/* The CTE request should already be in local control procedures queue */
843 
844 	/* Prepare */
845 	event_prepare(&conn);
846 
847 	/* Tx Queue should have one LL Control PDU */
848 	lt_rx(LL_CTE_REQ, &conn, &tx, cte_req);
849 	lt_rx_q_is_empty(&conn);
850 
851 	/* Rx */
852 	lt_tx(LL_CTE_RSP, &conn, &remote_cte_rsp);
853 
854 	/* Done */
855 	event_done(&conn);
856 
857 	/* Receive notification of sampled CTE response */
858 	ut_rx_pdu(LL_CTE_RSP, &ntf, &remote_cte_rsp);
859 
860 	/* The RX queue should be empty now */
861 	ut_rx_q_is_empty();
862 
863 	/* Release Ntf */
864 	release_ntf(ntf);
865 
866 	/* Release tx node */
867 	ull_cp_release_tx(&conn, tx);
868 }
869 
wait_for_phy_update_instant(uint8_t instant)870 static void wait_for_phy_update_instant(uint8_t instant)
871 {
872 	/* */
873 	while (!is_instant_reached(&conn, instant)) {
874 		/* Prepare */
875 		event_prepare(&conn);
876 
877 		/* Tx Queue should NOT have a LL Control PDU */
878 		lt_rx_q_is_empty(&conn);
879 
880 		/* Done */
881 		event_done(&conn);
882 
883 		check_current_phy_state(&conn, PHY_1M, PREFER_S2_CODING, PHY_1M);
884 
885 		/* There should NOT be a host notification */
886 		ut_rx_q_is_empty();
887 	}
888 }
889 
check_phy_update(bool is_local,struct pdu_data_llctrl_phy_req * phy_req,uint8_t ctx_num_at_end,bool dle_ntf)890 static void check_phy_update(bool is_local, struct pdu_data_llctrl_phy_req *phy_req,
891 		      uint8_t ctx_num_at_end, bool dle_ntf)
892 {
893 	struct pdu_data_llctrl_length_rsp length_ntf = {
894 		PDU_PDU_MAX_OCTETS, PDU_DC_MAX_US(PDU_PDU_MAX_OCTETS, phy_req->tx_phys),
895 		PDU_PDU_MAX_OCTETS, PDU_DC_MAX_US(PDU_PDU_MAX_OCTETS, phy_req->rx_phys)
896 	};
897 	struct node_rx_pu pu = {.status = BT_HCI_ERR_SUCCESS};
898 	struct node_rx_pdu *ntf;
899 
900 	/* Execute connection event that is an instant. It is required to send notifications to
901 	 * Host that complete already started PHY update procedure.
902 	 */
903 	event_prepare(&conn);
904 
905 	/* Tx Queue should NOT have a LL Control PDU */
906 	lt_rx_q_is_empty(&conn);
907 
908 	/* Done */
909 	event_done(&conn);
910 
911 	/* Tx Queue should NOT have a LL Control PDU */
912 	lt_rx_q_is_empty(&conn);
913 
914 	/* There should be two host notifications, one pu and one dle */
915 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
916 	if (dle_ntf) {
917 		ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
918 	}
919 
920 	/* Release Ntf */
921 	release_ntf(ntf);
922 
923 	/* The RX queue should be empty now */
924 	ut_rx_q_is_empty();
925 
926 	check_current_phy_state(&conn, phy_req->tx_phys, PREFER_S2_CODING, phy_req->tx_phys);
927 	if (is_local) {
928 		check_pref_phy_state(&conn, phy_req->rx_phys, phy_req->tx_phys);
929 	} else {
930 		check_pref_phy_state(&conn, PHY_PREFER_ANY, PHY_PREFER_ANY);
931 	}
932 
933 	/* There is still queued CTE REQ so number of contexts is smaller by 1 than max */
934 	zassert_equal(llcp_ctx_buffers_free(), ctx_num_at_end, "Free CTX buffers %d",
935 		      llcp_ctx_buffers_free());
936 }
937 
938 /**
939  * @brief The function executes and verifies PHY update procedure in central role.
940  *
941  * @param is_local        Flag informing if PHY request is local or remote.
942  * @param phy_req         Parameters of PHY update request.
943  * @param events_at_start Number of connection events at function start.
944  * @param ctx_num_at_end  Expected number of free procedure contexts at function end.
945  */
run_phy_update_central(bool is_local,struct pdu_data_llctrl_phy_req * phy_req,uint8_t events_at_start,uint8_t ctx_num_at_end,bool dle_ntf)946 static void run_phy_update_central(bool is_local, struct pdu_data_llctrl_phy_req *phy_req,
947 				   uint8_t events_at_start, uint8_t ctx_num_at_end, bool dle_ntf)
948 {
949 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_PREFER_ANY,
950 					       .tx_phys = PHY_PREFER_ANY };
951 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = events_at_start +
952 							      PHY_UPDATE_INSTANT_DELTA,
953 						   .c_to_p_phy = phy_req->tx_phys,
954 						   .p_to_c_phy = phy_req->rx_phys };
955 	struct node_tx *tx = NULL;
956 	struct pdu_data *pdu;
957 	uint16_t instant;
958 
959 	/* Prepare */
960 	event_prepare(&conn);
961 
962 	if (is_local) {
963 		/* Tx Queue should have one LL Control PDU */
964 		lt_rx(LL_PHY_REQ, &conn, &tx, phy_req);
965 		lt_rx_q_is_empty(&conn);
966 
967 		/* TX Ack */
968 		event_tx_ack(&conn, tx);
969 
970 		/* Rx */
971 		lt_tx(LL_PHY_RSP, &conn, &rsp);
972 
973 		ind.instant += 1;
974 	}
975 
976 	/* Done */
977 	event_done(&conn);
978 
979 	/* Check that data tx was paused */
980 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
981 
982 	if (tx != NULL) {
983 		/* Release Tx */
984 		ull_cp_release_tx(&conn, tx);
985 	}
986 	/* Prepare */
987 	event_prepare(&conn);
988 
989 	/* Tx Queue should have one LL Control PDU */
990 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind);
991 	lt_rx_q_is_empty(&conn);
992 
993 	/* TX Ack */
994 	event_tx_ack(&conn, tx);
995 
996 	/* Check that data tx is no longer paused */
997 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
998 
999 	/* Done */
1000 	event_done(&conn);
1001 
1002 	/* Save Instant */
1003 	pdu = (struct pdu_data *)tx->pdu;
1004 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
1005 
1006 	/* Release Tx */
1007 	ull_cp_release_tx(&conn, tx);
1008 
1009 	wait_for_phy_update_instant(instant);
1010 
1011 	check_phy_update(is_local, phy_req, ctx_num_at_end, dle_ntf);
1012 }
1013 
1014 /**
1015  * @brief The function executes and verifies PHY update procedure in peripheral role.
1016  *
1017  * @param is_local        Flag informing if PHY request is local or remote.
1018  * @param phy_req         Parameters of PHY update request.
1019  * @param events_at_start Number of connection events at function start.
1020  * @param ctx_num_at_end  Expected number of free procedure contexts at function end.
1021  */
run_phy_update_peripheral(bool is_local,struct pdu_data_llctrl_phy_req * phy_req,uint8_t events_at_start,uint8_t ctx_num_at_end,bool dle_ntf)1022 static void run_phy_update_peripheral(bool is_local, struct pdu_data_llctrl_phy_req *phy_req,
1023 				      uint8_t events_at_start, uint8_t ctx_num_at_end, bool dle_ntf)
1024 {
1025 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_PREFER_ANY,
1026 					       .tx_phys = PHY_PREFER_ANY };
1027 	struct pdu_data_llctrl_phy_upd_ind ind = { .c_to_p_phy = phy_req->rx_phys,
1028 						   .p_to_c_phy = phy_req->tx_phys };
1029 	struct node_tx *tx;
1030 	uint16_t instant;
1031 
1032 	/* Prepare */
1033 	event_prepare(&conn);
1034 
1035 	if (is_local) {
1036 		/* Tx Queue should have one LL Control PDU */
1037 		lt_rx(LL_PHY_REQ, &conn, &tx, phy_req);
1038 		lt_rx_q_is_empty(&conn);
1039 
1040 		/* TX Ack */
1041 		event_tx_ack(&conn, tx);
1042 	}
1043 
1044 	/* Done */
1045 	event_done(&conn);
1046 
1047 	if (is_local) {
1048 		/* Release Tx */
1049 		ull_cp_release_tx(&conn, tx);
1050 	} else {
1051 		/* Check that data tx was paused */
1052 		zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1053 	}
1054 
1055 	/* Prepare */
1056 	event_prepare(&conn);
1057 
1058 	instant = event_counter(&conn) + PHY_UPDATE_INSTANT_DELTA;
1059 	ind.instant = instant;
1060 
1061 	if (is_local) {
1062 		/* Tx Queue should NOT have a LL Control PDU */
1063 		lt_rx_q_is_empty(&conn);
1064 
1065 		/* Tx Queue should have one LL Control PDU */
1066 		lt_tx(LL_PHY_UPDATE_IND, &conn, &ind);
1067 	} else {
1068 		/* Tx Queue should have one LL Control PDU */
1069 		lt_rx(LL_PHY_RSP, &conn, &tx, &rsp);
1070 		lt_rx_q_is_empty(&conn);
1071 
1072 		/* Rx */
1073 		lt_tx(LL_PHY_UPDATE_IND, &conn, &ind);
1074 
1075 		/* We are sending RSP, so data tx should be paused until after tx ack */
1076 		zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1077 
1078 		/* TX Ack */
1079 		event_tx_ack(&conn, tx);
1080 
1081 		/* Check that data tx is no longer paused */
1082 		zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1083 	}
1084 
1085 	/* Done */
1086 	event_done(&conn);
1087 
1088 	if (!is_local) {
1089 		/* Release Tx */
1090 		ull_cp_release_tx(&conn, tx);
1091 	}
1092 
1093 	wait_for_phy_update_instant(instant);
1094 
1095 	check_phy_update(is_local, phy_req, ctx_num_at_end, dle_ntf);
1096 }
1097 
test_local_cte_req_wait_for_phy_update_complete_and_disable(uint8_t role)1098 static void test_local_cte_req_wait_for_phy_update_complete_and_disable(uint8_t role)
1099 {
1100 	uint8_t err;
1101 
1102 	struct pdu_data_llctrl_cte_req local_cte_req = {
1103 		.cte_type_req = BT_HCI_LE_AOA_CTE,
1104 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
1105 	};
1106 	struct pdu_data_llctrl_phy_req phy_req = { .rx_phys = PHY_CODED, .tx_phys = PHY_CODED };
1107 
1108 	phy_update_setup();
1109 
1110 	/* Role */
1111 	test_set_role(&conn, role);
1112 
1113 	/* Connect */
1114 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1115 
1116 	/* Initiate an PHY Update Procedure */
1117 	err = ull_cp_phy_update(&conn, PHY_CODED, PREFER_S2_CODING, PHY_CODED, HOST_INITIATED);
1118 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1119 
1120 	/* Initiate an CTE Request Procedure */
1121 	conn.llcp.cte_req.is_enabled = 1U;
1122 
1123 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
1124 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1125 
1126 	if (role == BT_HCI_ROLE_CENTRAL) {
1127 		run_phy_update_central(true, &phy_req, pu_event_counter(&conn),
1128 				       test_ctx_buffers_cnt() - 1, true);
1129 	} else {
1130 		run_phy_update_peripheral(true, &phy_req, pu_event_counter(&conn),
1131 					  test_ctx_buffers_cnt() - 1, true);
1132 	}
1133 
1134 	/* In this test CTE request is local procedure. Local procedures are handled after remote
1135 	 * procedures, hence PHY update will be handled one event after completion of CTE request.
1136 	 */
1137 
1138 	/* Prepare */
1139 	event_prepare(&conn);
1140 
1141 	/* Tx Queue should not have any LL Control PDU */
1142 	lt_rx_q_is_empty(&conn);
1143 
1144 	/* Done */
1145 	event_done(&conn);
1146 
1147 	/* There should not be a host notifications */
1148 	ut_rx_q_is_empty();
1149 
1150 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1151 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
1152 }
1153 
ZTEST(cte_req_after_fex,test_central_local_cte_req_wait_for_phy_update_complete_and_disable)1154 ZTEST(cte_req_after_fex, test_central_local_cte_req_wait_for_phy_update_complete_and_disable)
1155 {
1156 	test_local_cte_req_wait_for_phy_update_complete_and_disable(BT_HCI_ROLE_CENTRAL);
1157 }
1158 
ZTEST(cte_req_after_fex,test_peripheral_local_cte_req_wait_for_phy_update_complete_and_disable)1159 ZTEST(cte_req_after_fex, test_peripheral_local_cte_req_wait_for_phy_update_complete_and_disable)
1160 {
1161 	test_local_cte_req_wait_for_phy_update_complete_and_disable(BT_HCI_ROLE_PERIPHERAL);
1162 }
1163 
test_local_cte_req_wait_for_phy_update_complete(uint8_t role)1164 static void test_local_cte_req_wait_for_phy_update_complete(uint8_t role)
1165 {
1166 	struct pdu_data_llctrl_cte_req local_cte_req = {
1167 		.cte_type_req = BT_HCI_LE_AOA_CTE,
1168 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
1169 	};
1170 	struct pdu_data_llctrl_phy_req phy_req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
1171 
1172 	uint8_t err;
1173 
1174 	phy_update_setup();
1175 
1176 	/* Role */
1177 	test_set_role(&conn, role);
1178 
1179 	/* Connect */
1180 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1181 
1182 	/* Initiate an PHY Update Procedure */
1183 	err = ull_cp_phy_update(&conn, phy_req.rx_phys, PREFER_S2_CODING, phy_req.tx_phys,
1184 				HOST_INITIATED);
1185 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1186 
1187 	/* Initiate an CTE Request Procedure */
1188 	conn.llcp.cte_req.is_enabled = 1U;
1189 
1190 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
1191 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1192 
1193 	if (role == BT_HCI_ROLE_CENTRAL) {
1194 		run_phy_update_central(true, &phy_req, pu_event_counter(&conn),
1195 				       test_ctx_buffers_cnt() - 1, false);
1196 	} else {
1197 		run_phy_update_peripheral(true, &phy_req, pu_event_counter(&conn),
1198 					  test_ctx_buffers_cnt() - 1, false);
1199 	}
1200 
1201 	/* PHY update was completed. Handle CTE request */
1202 	run_local_cte_req(&local_cte_req);
1203 
1204 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1205 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
1206 }
1207 
ZTEST(cte_req_after_fex,test_central_local_cte_req_wait_for_phy_update_complete)1208 ZTEST(cte_req_after_fex, test_central_local_cte_req_wait_for_phy_update_complete)
1209 {
1210 	test_local_cte_req_wait_for_phy_update_complete(BT_HCI_ROLE_CENTRAL);
1211 }
1212 
ZTEST(cte_req_after_fex,test_peripheral_local_cte_req_wait_for_phy_update_complete)1213 ZTEST(cte_req_after_fex, test_peripheral_local_cte_req_wait_for_phy_update_complete)
1214 {
1215 	test_local_cte_req_wait_for_phy_update_complete(BT_HCI_ROLE_PERIPHERAL);
1216 }
1217 
test_local_phy_update_wait_for_cte_req_complete(uint8_t role)1218 static void test_local_phy_update_wait_for_cte_req_complete(uint8_t role)
1219 {
1220 	struct pdu_data_llctrl_phy_req phy_req = { .rx_phys = PHY_CODED, .tx_phys = PHY_CODED };
1221 	struct pdu_data_llctrl_cte_req local_cte_req = {
1222 		.cte_type_req = BT_HCI_LE_AOA_CTE,
1223 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
1224 	};
1225 	uint8_t err;
1226 
1227 	phy_update_setup();
1228 
1229 	/* Role */
1230 	test_set_role(&conn, role);
1231 
1232 	/* Connect */
1233 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1234 
1235 	/* Initiate an CTE Request Procedure */
1236 	conn.llcp.cte_req.is_enabled = 1U;
1237 
1238 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
1239 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1240 
1241 	/* Initiate an PHY Update Procedure */
1242 	err = ull_cp_phy_update(&conn, PHY_CODED, PREFER_S2_CODING, PHY_CODED, HOST_INITIATED);
1243 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1244 
1245 	/* Handle CTE request */
1246 	run_local_cte_req(&local_cte_req);
1247 
1248 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt() - 1,
1249 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
1250 
1251 	if (role == BT_HCI_ROLE_CENTRAL) {
1252 		run_phy_update_central(true, &phy_req, pu_event_counter(&conn),
1253 				       test_ctx_buffers_cnt(), true);
1254 	} else {
1255 		run_phy_update_peripheral(true, &phy_req, pu_event_counter(&conn),
1256 					  test_ctx_buffers_cnt(), true);
1257 	}
1258 }
1259 
ZTEST(cte_req_after_fex,test_central_local_phy_update_wait_for_cte_req_complete)1260 ZTEST(cte_req_after_fex, test_central_local_phy_update_wait_for_cte_req_complete)
1261 {
1262 	test_local_phy_update_wait_for_cte_req_complete(BT_HCI_ROLE_CENTRAL);
1263 }
1264 
ZTEST(cte_req_after_fex,test_peripheral_local_phy_update_wait_for_cte_req_complete)1265 ZTEST(cte_req_after_fex, test_peripheral_local_phy_update_wait_for_cte_req_complete)
1266 {
1267 	test_local_phy_update_wait_for_cte_req_complete(BT_HCI_ROLE_PERIPHERAL);
1268 }
1269 
run_remote_cte_req(struct pdu_data_llctrl_cte_req * cte_req)1270 static void run_remote_cte_req(struct pdu_data_llctrl_cte_req *cte_req)
1271 {
1272 	struct pdu_data_llctrl_cte_rsp remote_cte_rsp = {};
1273 	struct node_tx *tx;
1274 
1275 	/* The CTE response should already be enabled and request PDU should already be
1276 	 * received.
1277 	 */
1278 
1279 	/* Prepare */
1280 	event_prepare(&conn);
1281 
1282 	/* Tx Queue should have one LL Control PDU */
1283 	lt_rx(LL_CTE_RSP, &conn, &tx, &remote_cte_rsp);
1284 	lt_rx_q_is_empty(&conn);
1285 
1286 	/* TX Ack */
1287 	event_tx_ack(&conn, tx);
1288 
1289 	/* Done */
1290 	event_done(&conn);
1291 
1292 	/* Release tx node */
1293 	ull_cp_release_tx(&conn, tx);
1294 
1295 	/* There should not be a host notifications */
1296 	ut_rx_q_is_empty();
1297 }
1298 
test_phy_update_wait_for_remote_cte_req_complete(uint8_t role)1299 static void test_phy_update_wait_for_remote_cte_req_complete(uint8_t role)
1300 {
1301 	struct pdu_data_llctrl_cte_req local_cte_req = {
1302 		.cte_type_req = BT_HCI_LE_AOA_CTE,
1303 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
1304 	};
1305 	struct pdu_data_llctrl_phy_req phy_req = { .rx_phys = PHY_CODED, .tx_phys = PHY_CODED };
1306 	uint8_t err;
1307 
1308 	phy_update_setup();
1309 
1310 	/* Role */
1311 	test_set_role(&conn, role);
1312 
1313 	/* Connect */
1314 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1315 
1316 	/* Enable response for CTE request */
1317 	ull_cp_cte_rsp_enable(&conn, true, BT_HCI_LE_CTE_LEN_MAX,
1318 			      (BT_HCI_LE_AOA_CTE | BT_HCI_LE_AOD_CTE_1US | BT_HCI_LE_AOD_CTE_2US));
1319 
1320 	/* Prepare */
1321 	event_prepare(&conn);
1322 
1323 	/* Tx */
1324 	lt_tx(LL_CTE_REQ, &conn, &local_cte_req);
1325 
1326 	/* Done */
1327 	event_done(&conn);
1328 
1329 	/* Initiate an PHY Update Procedure */
1330 	err = ull_cp_phy_update(&conn, PHY_CODED, PREFER_S2_CODING, PHY_CODED, HOST_INITIATED);
1331 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1332 
1333 	run_remote_cte_req(&local_cte_req);
1334 
1335 	/* There should not be a host notifications */
1336 	ut_rx_q_is_empty();
1337 
1338 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt() - 1,
1339 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
1340 
1341 	if (role == BT_HCI_ROLE_CENTRAL) {
1342 		run_phy_update_central(true, &phy_req, pu_event_counter(&conn),
1343 				       test_ctx_buffers_cnt(), true);
1344 	} else {
1345 		run_phy_update_peripheral(true, &phy_req, pu_event_counter(&conn),
1346 					  test_ctx_buffers_cnt(), true);
1347 	}
1348 }
1349 
ZTEST(cte_req_after_fex,test_central_phy_update_wait_for_remote_cte_req_complete)1350 ZTEST(cte_req_after_fex, test_central_phy_update_wait_for_remote_cte_req_complete)
1351 {
1352 	test_phy_update_wait_for_remote_cte_req_complete(BT_HCI_ROLE_CENTRAL);
1353 }
1354 
ZTEST(cte_req_after_fex,test_peripheral_phy_update_wait_for_remote_cte_req_complete)1355 ZTEST(cte_req_after_fex, test_peripheral_phy_update_wait_for_remote_cte_req_complete)
1356 {
1357 	test_phy_update_wait_for_remote_cte_req_complete(BT_HCI_ROLE_PERIPHERAL);
1358 }
1359 
test_cte_req_wait_for_remote_phy_update_complete_and_disable(uint8_t role)1360 static void test_cte_req_wait_for_remote_phy_update_complete_and_disable(uint8_t role)
1361 {
1362 	uint8_t err;
1363 
1364 	struct pdu_data_llctrl_cte_req local_cte_req = {
1365 		.cte_type_req = BT_HCI_LE_AOA_CTE,
1366 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
1367 	};
1368 	struct pdu_data_llctrl_phy_req phy_req = { .rx_phys = PHY_CODED, .tx_phys = PHY_CODED };
1369 
1370 	phy_update_setup();
1371 
1372 	/* Role */
1373 	test_set_role(&conn, role);
1374 
1375 	/* Connect */
1376 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1377 
1378 	/* Prepare */
1379 	event_prepare(&conn);
1380 
1381 	/* Tx */
1382 	lt_tx(LL_PHY_REQ, &conn, &phy_req);
1383 
1384 	/* Done */
1385 	event_done(&conn);
1386 
1387 	/* Initiate an CTE Request Procedure */
1388 	conn.llcp.cte_req.is_enabled = 1U;
1389 
1390 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
1391 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1392 
1393 	if (role == BT_HCI_ROLE_CENTRAL) {
1394 		run_phy_update_central(false, &phy_req, pu_event_counter(&conn),
1395 				       test_ctx_buffers_cnt(), true);
1396 	} else {
1397 		run_phy_update_peripheral(false, &phy_req, pu_event_counter(&conn),
1398 					  test_ctx_buffers_cnt(), true);
1399 	}
1400 
1401 	/* There is no special handling of CTE REQ completion. It is done when instant happens just
1402 	 * after remote PHY update completes.
1403 	 */
1404 }
1405 
ZTEST(cte_req_after_fex,test_central_cte_req_wait_for_remote_phy_update_complete_and_disable)1406 ZTEST(cte_req_after_fex, test_central_cte_req_wait_for_remote_phy_update_complete_and_disable)
1407 {
1408 	test_cte_req_wait_for_remote_phy_update_complete_and_disable(BT_HCI_ROLE_CENTRAL);
1409 }
1410 
ZTEST(cte_req_after_fex,test_peripheral_cte_req_wait_for_remote_phy_update_complete_and_disable)1411 ZTEST(cte_req_after_fex, test_peripheral_cte_req_wait_for_remote_phy_update_complete_and_disable)
1412 {
1413 	test_cte_req_wait_for_remote_phy_update_complete_and_disable(BT_HCI_ROLE_PERIPHERAL);
1414 }
1415 
test_cte_req_wait_for_remote_phy_update_complete(uint8_t role)1416 static void test_cte_req_wait_for_remote_phy_update_complete(uint8_t role)
1417 {
1418 	uint8_t err;
1419 
1420 	struct pdu_data_llctrl_cte_req local_cte_req = {
1421 		.cte_type_req = BT_HCI_LE_AOA_CTE,
1422 		.min_cte_len_req = BT_HCI_LE_CTE_LEN_MIN,
1423 	};
1424 	struct pdu_data_llctrl_phy_req phy_req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
1425 
1426 	phy_update_setup();
1427 
1428 	/* Role */
1429 	test_set_role(&conn, role);
1430 
1431 	/* Connect */
1432 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1433 
1434 	/* Prepare */
1435 	event_prepare(&conn);
1436 
1437 	/* Tx */
1438 	lt_tx(LL_PHY_REQ, &conn, &phy_req);
1439 
1440 	/* Done */
1441 	event_done(&conn);
1442 
1443 	/* Initiate an CTE Request Procedure */
1444 	conn.llcp.cte_req.is_enabled = 1U;
1445 
1446 	err = ull_cp_cte_req(&conn, local_cte_req.min_cte_len_req, local_cte_req.cte_type_req);
1447 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1448 
1449 	if (role == BT_HCI_ROLE_CENTRAL) {
1450 		run_phy_update_central(false, &phy_req, pu_event_counter(&conn),
1451 				       test_ctx_buffers_cnt() - 1, false);
1452 	} else {
1453 		run_phy_update_peripheral(false, &phy_req, pu_event_counter(&conn),
1454 					  test_ctx_buffers_cnt() - 1, false);
1455 	}
1456 
1457 	/* There is no special handling of CTE REQ completion here. It is done when instant happens
1458 	 * just after remote PHY update completes.
1459 	 */
1460 	/* PHY update was completed. Handle CTE request */
1461 	run_local_cte_req(&local_cte_req);
1462 
1463 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(), "Free CTX buffers %d",
1464 		      llcp_ctx_buffers_free());
1465 }
1466 
ZTEST(cte_req_after_fex,test_central_cte_req_wait_for_remote_phy_update_complete)1467 ZTEST(cte_req_after_fex, test_central_cte_req_wait_for_remote_phy_update_complete)
1468 {
1469 	test_cte_req_wait_for_remote_phy_update_complete(BT_HCI_ROLE_CENTRAL);
1470 }
1471 
ZTEST(cte_req_after_fex,test_peripheral_cte_req_wait_for_remote_phy_update_complete)1472 ZTEST(cte_req_after_fex, test_peripheral_cte_req_wait_for_remote_phy_update_complete)
1473 {
1474 	test_cte_req_wait_for_remote_phy_update_complete(BT_HCI_ROLE_PERIPHERAL);
1475 }
1476 
1477 ZTEST_SUITE(cte_req, NULL, NULL, cte_req_setup, NULL, NULL);
1478 ZTEST_SUITE(cte_req_after_fex, NULL, NULL, fex_setup, NULL, NULL);
1479