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 #define ULL_LLCP_UNITTEST
11 
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <zephyr/sys/slist.h>
15 #include <zephyr/sys/util.h>
16 #include "hal/ccm.h"
17 
18 #include "util/util.h"
19 #include "util/mem.h"
20 #include "util/memq.h"
21 #include "util/dbuf.h"
22 
23 #include "pdu_df.h"
24 #include "lll/pdu_vendor.h"
25 #include "pdu.h"
26 #include "ll.h"
27 #include "ll_settings.h"
28 
29 #include "lll.h"
30 #include "lll/lll_df_types.h"
31 #include "lll_conn.h"
32 #include "lll_conn_iso.h"
33 
34 #include "ull_tx_queue.h"
35 
36 #include "isoal.h"
37 #include "ull_iso_types.h"
38 #include "ull_conn_iso_types.h"
39 
40 #include "ull_internal.h"
41 #include "ull_conn_types.h"
42 #include "ull_llcp.h"
43 #include "ull_conn_internal.h"
44 #include "ull_llcp_internal.h"
45 
46 #include "helper_pdu.h"
47 #include "helper_util.h"
48 
49 #define PREFER_S8_CODING 1
50 #define PREFER_S2_CODING 0
51 
52 #define HOST_INITIATED 1U
53 
54 static struct ll_conn conn;
55 
phy_setup(void * data)56 static void phy_setup(void *data)
57 {
58 	test_setup(&conn);
59 
60 	/* Emulate initial conn state */
61 	conn.phy_pref_rx = PHY_1M | PHY_2M | PHY_CODED;
62 	conn.phy_pref_tx = PHY_1M | PHY_2M | PHY_CODED;
63 	conn.lll.phy_flags = PREFER_S2_CODING;
64 	conn.lll.phy_tx_time = PHY_1M;
65 	conn.lll.phy_rx = PHY_1M;
66 	conn.lll.phy_tx = PHY_1M;
67 
68 	/* Init DLE data */
69 	ull_conn_default_tx_octets_set(251);
70 	ull_conn_default_tx_time_set(2120);
71 	ull_dle_init(&conn, PHY_1M);
72 	/* Emulate different remote numbers to trigger update of eff */
73 	conn.lll.dle.remote.max_tx_octets = PDU_DC_PAYLOAD_SIZE_MIN * 3;
74 	conn.lll.dle.remote.max_rx_octets = PDU_DC_PAYLOAD_SIZE_MIN * 3;
75 	conn.lll.dle.remote.max_tx_time = PDU_DC_MAX_US(conn.lll.dle.remote.max_tx_octets,
76 							 PHY_1M);
77 	conn.lll.dle.remote.max_rx_time = PDU_DC_MAX_US(conn.lll.dle.remote.max_rx_octets,
78 							 PHY_1M);
79 	ull_dle_update_eff(&conn);
80 }
81 
82 #define CHECK_PREF_PHY_STATE(_conn, _tx, _rx)                               \
83 	do {                                                                    \
84 		zassert_equal(_conn.phy_pref_rx, _rx,                              \
85 			      "Preferred RX PHY mismatch %d (actual) != %d (expected)", \
86 			      _conn.phy_pref_rx, _rx);                                 \
87 		zassert_equal(_conn.phy_pref_tx, _tx,                              \
88 			      "Preferred TX PHY mismatch %d (actual) != %d (expected)", \
89 			      _conn.phy_pref_tx, _tx);                                 \
90 	} while (0)
91 
92 #define CHECK_CURRENT_PHY_STATE(_conn, _tx, _flags, _rx)                    \
93 	do {                                                                    \
94 		zassert_equal(_conn.lll.phy_rx, _rx,                               \
95 			      "Current RX PHY mismatch %d (actual) != %d (expected)",   \
96 			      _conn.lll.phy_rx, _rx);                                  \
97 		zassert_equal(_conn.lll.phy_tx, _tx,                               \
98 			      "Current TX PHY mismatch %d (actual) != %d (expected)",   \
99 			      _conn.lll.phy_tx, _tx);                                  \
100 		zassert_equal(_conn.lll.phy_rx, _rx,                               \
101 			      "Current Flags mismatch %d (actual) != %d (expected)",    \
102 			      _conn.lll.phy_flags, _flags);                            \
103 	} while (0)
104 
is_instant_reached(struct ll_conn * conn,uint16_t instant)105 static bool is_instant_reached(struct ll_conn *conn, uint16_t instant)
106 {
107 	return ((event_counter(conn) - instant) & 0xFFFF) <= 0x7FFF;
108 }
109 
110 /* +-----+                +-------+              +-----+
111  * | UT  |                | LL_A  |              | LT  |
112  * +-----+                +-------+              +-----+
113  *    |                       |                     |
114  */
ZTEST(phy_central,test_phy_update_central_loc)115 ZTEST(phy_central, test_phy_update_central_loc)
116 {
117 	uint8_t err;
118 	struct node_tx *tx;
119 	struct node_rx_pdu *ntf;
120 	struct pdu_data *pdu;
121 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
122 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M,
123 					       .tx_phys = PHY_1M | PHY_2M };
124 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 8,
125 						   .c_to_p_phy = PHY_2M,
126 						   .p_to_c_phy = PHY_2M };
127 	struct pdu_data_llctrl_length_rsp length_ntf = {
128 		3 * PDU_DC_PAYLOAD_SIZE_MIN, PDU_DC_MAX_US(3 * PDU_DC_PAYLOAD_SIZE_MIN, PHY_2M),
129 		3 * PDU_DC_PAYLOAD_SIZE_MIN, PDU_DC_MAX_US(3 * PDU_DC_PAYLOAD_SIZE_MIN, PHY_2M)
130 	};
131 	uint16_t instant;
132 
133 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
134 
135 	/* 'Trigger' DLE ntf on PHY update, as this forces change to eff tx/rx times */
136 	conn.lll.dle.eff.max_rx_time = 0;
137 
138 	/* Role */
139 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
140 
141 	/* Connect */
142 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
143 
144 	/* Initiate an PHY Update Procedure */
145 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
146 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
147 
148 	/* Steal all ntf buffers, to trigger TX stall on non avail of NTF buffer for DLE */
149 	while (ll_pdu_rx_alloc_peek(1)) {
150 		ntf = ll_pdu_rx_alloc();
151 		/* Make sure we use a correct type or the release won't work */
152 		ntf->hdr.type = NODE_RX_TYPE_DC_PDU;
153 	}
154 
155 	/* Prepare */
156 	event_prepare(&conn);
157 
158 	/* Tx Queue should have one LL Control PDU */
159 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
160 	lt_rx_q_is_empty(&conn);
161 
162 	/* Check that data tx was paused */
163 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
164 
165 	/* Rx */
166 	lt_tx(LL_PHY_RSP, &conn, &rsp);
167 
168 	/* TX Ack */
169 	event_tx_ack(&conn, tx);
170 
171 	/* Done */
172 	event_done(&conn);
173 
174 	/* Check that data tx was paused */
175 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
176 
177 	/* Release Tx */
178 	ull_cp_release_tx(&conn, tx);
179 
180 	/* Prepare */
181 	event_prepare(&conn);
182 
183 	/* No TX yet as unable to pre-allocate NTF buffer for DLE */
184 	lt_rx_q_is_empty(&conn);
185 
186 	/* Done */
187 	event_done(&conn);
188 
189 	/* Release RX node to now allow pre-alloc for DLE NTF */
190 	release_ntf(ntf);
191 
192 	/* Prepare */
193 	event_prepare(&conn);
194 
195 	/* Tx Queue should have one LL Control PDU */
196 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind);
197 	lt_rx_q_is_empty(&conn);
198 
199 	/* TX Ack */
200 	event_tx_ack(&conn, tx);
201 
202 	/* Check that data tx is no longer paused */
203 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
204 
205 	/* Done */
206 	event_done(&conn);
207 
208 	/* Save Instant */
209 	pdu = (struct pdu_data *)tx->pdu;
210 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
211 
212 	/* Release Tx */
213 	ull_cp_release_tx(&conn, tx);
214 
215 	/* */
216 	while (!is_instant_reached(&conn, instant)) {
217 		/* Prepare */
218 		event_prepare(&conn);
219 
220 		/* Tx Queue should NOT have a LL Control PDU */
221 		lt_rx_q_is_empty(&conn);
222 
223 		/* Done */
224 		event_done(&conn);
225 
226 		CHECK_CURRENT_PHY_STATE(conn, PHY_1M, PREFER_S8_CODING, PHY_1M);
227 
228 		/* There should NOT be a host notification */
229 		ut_rx_q_is_empty();
230 	}
231 
232 	/* Prepare */
233 	event_prepare(&conn);
234 
235 	/* Tx Queue should NOT have a LL Control PDU */
236 	lt_rx_q_is_empty(&conn);
237 
238 	/* Done */
239 	event_done(&conn);
240 
241 	/* There should be two host notifications, one pu and one dle */
242 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
243 	/* Release Ntf */
244 	release_ntf(ntf);
245 
246 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
247 	/* Release Ntf */
248 	release_ntf(ntf);
249 
250 	ut_rx_q_is_empty();
251 
252 
253 	CHECK_CURRENT_PHY_STATE(conn, PHY_2M, PREFER_S8_CODING, PHY_2M);
254 	CHECK_PREF_PHY_STATE(conn, PHY_2M, PHY_2M);
255 
256 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
257 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
258 }
259 
ZTEST(phy_central,test_phy_update_central_loc_invalid)260 ZTEST(phy_central, test_phy_update_central_loc_invalid)
261 {
262 	uint8_t err;
263 	struct node_tx *tx;
264 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
265 
266 	struct pdu_data_llctrl_reject_ind reject_ind = { };
267 
268 	/* Role */
269 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
270 
271 	/* Connect */
272 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
273 
274 	/* Initiate an PHY Update Procedure */
275 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
276 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
277 
278 	/* Prepare */
279 	event_prepare(&conn);
280 
281 	/* Tx Queue should have one LL Control PDU */
282 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
283 	lt_rx_q_is_empty(&conn);
284 
285 	/* Check that data tx was paused */
286 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
287 
288 	/* Rx */
289 	lt_tx(LL_REJECT_IND, &conn, &reject_ind);
290 
291 	/* TX Ack */
292 	event_tx_ack(&conn, tx);
293 
294 	/* Done */
295 	event_done(&conn);
296 
297 	/* Check that data tx was paused */
298 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
299 
300 	/* Release Tx */
301 	ull_cp_release_tx(&conn, tx);
302 
303 	/* Termination 'triggered' */
304 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
305 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
306 
307 	/* There should be nohost notifications */
308 	ut_rx_q_is_empty();
309 
310 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
311 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
312 }
313 
ZTEST(phy_central,test_phy_update_central_loc_unsupp_feat)314 ZTEST(phy_central, test_phy_update_central_loc_unsupp_feat)
315 {
316 	uint8_t err;
317 	struct node_tx *tx;
318 	struct node_rx_pdu *ntf;
319 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
320 
321 	struct pdu_data_llctrl_unknown_rsp unknown_rsp = { .type = PDU_DATA_LLCTRL_TYPE_PHY_REQ };
322 
323 	struct node_rx_pu pu = { .status = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE };
324 
325 	/* Role */
326 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
327 
328 	/* Connect */
329 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
330 
331 	/* Initiate an PHY Update Procedure */
332 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
333 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
334 
335 	/* Prepare */
336 	event_prepare(&conn);
337 
338 	/* Tx Queue should have one LL Control PDU */
339 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
340 	lt_rx_q_is_empty(&conn);
341 
342 	/* Check that data tx was paused */
343 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
344 
345 	/* Rx */
346 	lt_tx(LL_UNKNOWN_RSP, &conn, &unknown_rsp);
347 
348 	/* TX Ack */
349 	event_tx_ack(&conn, tx);
350 
351 	/* Done */
352 	event_done(&conn);
353 
354 	/* Check that data tx is no longer paused */
355 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
356 
357 	/* Release Tx */
358 	ull_cp_release_tx(&conn, tx);
359 
360 	/* Prepare */
361 	event_prepare(&conn);
362 
363 	/* Done */
364 	event_done(&conn);
365 
366 	/* There should be one host notification */
367 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
368 	ut_rx_q_is_empty();
369 
370 	/* Release Ntf */
371 	release_ntf(ntf);
372 
373 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
374 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
375 }
376 
ZTEST(phy_central,test_phy_update_central_rem)377 ZTEST(phy_central, test_phy_update_central_rem)
378 {
379 	struct node_tx *tx;
380 	struct node_rx_pdu *ntf;
381 	struct pdu_data *pdu;
382 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_2M };
383 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 8,
384 						   .c_to_p_phy = 0,
385 						   .p_to_c_phy = PHY_2M };
386 	uint16_t instant;
387 
388 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
389 
390 	/* Role */
391 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
392 
393 	/* Connect */
394 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
395 
396 	/* Steal all ntf buffers, to trigger TX stall on non avail of NTF buffer for DLE */
397 	while (ll_pdu_rx_alloc_peek(1)) {
398 		ntf = ll_pdu_rx_alloc();
399 		/* Make sure we use a correct type or the release won't work */
400 		ntf->hdr.type = NODE_RX_TYPE_DC_PDU;
401 	}
402 
403 	/* Prepare */
404 	event_prepare(&conn);
405 
406 	/* Rx */
407 	lt_tx(LL_PHY_REQ, &conn, &req);
408 
409 	/* TX Ack */
410 	event_tx_ack(&conn, tx);
411 
412 	/* Done */
413 	event_done(&conn);
414 
415 	/* Prepare */
416 	event_prepare(&conn);
417 
418 	/* No TX yet as unable to pre-allocate NTF buffer for DLE */
419 	lt_rx_q_is_empty(&conn);
420 
421 	/* Done */
422 	event_done(&conn);
423 
424 	/* Release RX node to now allow pre-alloc for DLE NTF */
425 	release_ntf(ntf);
426 
427 	/* Check that data tx was paused */
428 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
429 
430 	/* Prepare */
431 	event_prepare(&conn);
432 
433 	/* Tx Queue should have one LL Control PDU */
434 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind);
435 	lt_rx_q_is_empty(&conn);
436 
437 	/* TX Ack */
438 	event_tx_ack(&conn, tx);
439 
440 	/* Done */
441 	event_done(&conn);
442 
443 	/* Check that data tx is no longer paused */
444 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
445 
446 	/* Save Instant */
447 	pdu = (struct pdu_data *)tx->pdu;
448 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
449 
450 	/* Release Tx */
451 	ull_cp_release_tx(&conn, tx);
452 
453 	/* */
454 	while (!is_instant_reached(&conn, instant)) {
455 		/* Prepare */
456 		event_prepare(&conn);
457 
458 		/* Tx Queue should NOT have a LL Control PDU */
459 		lt_rx_q_is_empty(&conn);
460 
461 		/* Done */
462 		event_done(&conn);
463 
464 		/* There should NOT be a host notification */
465 		ut_rx_q_is_empty();
466 	}
467 
468 	/* Prepare */
469 	event_prepare(&conn);
470 
471 	/* Tx Queue should NOT have a LL Control PDU */
472 	lt_rx_q_is_empty(&conn);
473 
474 	/* Done */
475 	event_done(&conn);
476 
477 	/* There should be one host notification */
478 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
479 	ut_rx_q_is_empty();
480 
481 	/* Release Ntf */
482 	release_ntf(ntf);
483 	CHECK_CURRENT_PHY_STATE(conn, PHY_1M, PREFER_S8_CODING, PHY_2M);
484 	CHECK_PREF_PHY_STATE(conn, PHY_1M | PHY_2M | PHY_CODED, PHY_1M | PHY_2M | PHY_CODED);
485 
486 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
487 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
488 }
489 
ZTEST(phy_periph,test_phy_update_periph_loc)490 ZTEST(phy_periph, test_phy_update_periph_loc)
491 {
492 	uint8_t err;
493 	struct node_tx *tx;
494 	struct node_rx_pdu *ntf;
495 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
496 	uint16_t instant;
497 
498 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
499 
500 	struct pdu_data_llctrl_phy_upd_ind phy_update_ind = { .c_to_p_phy = PHY_2M,
501 							      .p_to_c_phy = PHY_2M };
502 
503 	/* Role */
504 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
505 
506 	/* Connect */
507 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
508 
509 	/* Initiate an PHY Update Procedure */
510 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
511 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
512 
513 	/* Prepare */
514 	event_prepare(&conn);
515 
516 	/* Tx Queue should have one LL Control PDU */
517 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
518 	lt_rx_q_is_empty(&conn);
519 
520 	/* Check that data tx was paused */
521 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
522 
523 	/* TX Ack */
524 	event_tx_ack(&conn, tx);
525 
526 	/* Check that data tx is no longer paused */
527 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
528 
529 	/* Done */
530 	event_done(&conn);
531 
532 	/* Release Tx */
533 	ull_cp_release_tx(&conn, tx);
534 
535 	/* Prepare */
536 	event_prepare(&conn);
537 
538 	/* Tx Queue should NOT have a LL Control PDU */
539 	lt_rx_q_is_empty(&conn);
540 
541 	/* Rx */
542 	phy_update_ind.instant = instant = event_counter(&conn) + 6;
543 	lt_tx(LL_PHY_UPDATE_IND, &conn, &phy_update_ind);
544 
545 	/* Done */
546 	event_done(&conn);
547 
548 	/* Check that data tx is no longer paused */
549 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
550 
551 	/* */
552 	while (!is_instant_reached(&conn, instant)) {
553 		/* Prepare */
554 		event_prepare(&conn);
555 
556 		/* Tx Queue should NOT have a LL Control PDU */
557 		lt_rx_q_is_empty(&conn);
558 
559 		/* Done */
560 		event_done(&conn);
561 
562 		/* There should NOT be a host notification */
563 		ut_rx_q_is_empty();
564 	}
565 
566 	/* Prepare */
567 	event_prepare(&conn);
568 
569 	/* Tx Queue should NOT have a LL Control PDU */
570 	lt_rx_q_is_empty(&conn);
571 
572 	/* Done */
573 	event_done(&conn);
574 
575 	/* There should be one host notification */
576 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
577 	ut_rx_q_is_empty();
578 
579 	/* Release Ntf */
580 	release_ntf(ntf);
581 	CHECK_CURRENT_PHY_STATE(conn, PHY_2M, PREFER_S8_CODING, PHY_2M);
582 	CHECK_PREF_PHY_STATE(conn, PHY_2M, PHY_2M);
583 
584 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
585 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
586 }
587 
ZTEST(phy_periph,test_phy_update_periph_loc_invalid_central)588 ZTEST(phy_periph, test_phy_update_periph_loc_invalid_central)
589 {
590 	uint8_t err;
591 	struct node_tx *tx;
592 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
593 	uint16_t instant;
594 	struct pdu_data_llctrl_conn_param_req conn_param_req = {  };
595 
596 	struct pdu_data_llctrl_phy_upd_ind phy_update_ind = { .c_to_p_phy = PHY_2M,
597 							      .p_to_c_phy = PHY_2M };
598 
599 	/* Role */
600 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
601 
602 	/* Connect */
603 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
604 
605 	/* Initiate an PHY Update Procedure */
606 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
607 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
608 
609 	/* Prepare */
610 	event_prepare(&conn);
611 
612 	/* Tx Queue should have one LL Control PDU */
613 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
614 	lt_rx_q_is_empty(&conn);
615 
616 	/* Check that data tx was paused */
617 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
618 
619 	/* TX Ack */
620 	event_tx_ack(&conn, tx);
621 
622 	/* Check that data tx is no longer paused */
623 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
624 
625 	/* Done */
626 	event_done(&conn);
627 
628 	/* Release Tx */
629 	ull_cp_release_tx(&conn, tx);
630 
631 	/* Prepare */
632 	event_prepare(&conn);
633 
634 	/* Tx Queue should NOT have a LL Control PDU */
635 	lt_rx_q_is_empty(&conn);
636 
637 	/* Rx */
638 	phy_update_ind.instant = instant = event_counter(&conn) + 6;
639 	lt_tx(LL_PHY_UPDATE_IND, &conn, &phy_update_ind);
640 
641 	/* Done */
642 	event_done(&conn);
643 
644 	/* Check that data tx is no longer paused */
645 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
646 
647 	/* One event ahead */
648 	/* Prepare */
649 	event_prepare(&conn);
650 
651 	/* Tx Queue should NOT have a LL Control PDU */
652 	lt_rx_q_is_empty(&conn);
653 
654 	/* Done */
655 	event_done(&conn);
656 
657 	/* There should NOT be a host notification */
658 	ut_rx_q_is_empty();
659 
660 	/* 'Inject' invalid param request from central */
661 	/* Prepare */
662 	event_prepare(&conn);
663 	/* Rx */
664 	lt_tx(LL_CONNECTION_PARAM_REQ, &conn, &conn_param_req);
665 
666 	/* Done */
667 	event_done(&conn);
668 
669 	/* Termination 'triggered' */
670 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_DIFF_TRANS_COLLISION,
671 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
672 }
673 
ZTEST(phy_periph,test_phy_update_periph_rem)674 ZTEST(phy_periph, test_phy_update_periph_rem)
675 {
676 	struct node_tx *tx;
677 	struct node_rx_pdu *ntf;
678 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_2M };
679 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M | PHY_CODED,
680 					       .tx_phys = PHY_1M | PHY_2M | PHY_CODED };
681 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 7,
682 						   .c_to_p_phy = 0,
683 						   .p_to_c_phy = PHY_2M };
684 	uint16_t instant;
685 
686 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
687 
688 	/* Role */
689 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
690 
691 	/* Connect */
692 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
693 
694 	/* Prepare */
695 	event_prepare(&conn);
696 
697 	/* Tx Queue should NOT have a LL Control PDU */
698 	lt_rx_q_is_empty(&conn);
699 
700 	/* Rx */
701 	lt_tx(LL_PHY_REQ, &conn, &req);
702 
703 	/* Done */
704 	event_done(&conn);
705 
706 	/* We received a REQ, so data tx should be paused */
707 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
708 
709 	/* Prepare */
710 	event_prepare(&conn);
711 
712 	/* Tx Queue should have one LL Control PDU */
713 	lt_rx(LL_PHY_RSP, &conn, &tx, &rsp);
714 	lt_rx_q_is_empty(&conn);
715 
716 	/* Rx */
717 	ind.instant = instant = event_counter(&conn) + 6;
718 	lt_tx(LL_PHY_UPDATE_IND, &conn, &ind);
719 
720 	/* We are sending RSP, so data tx should be paused until after tx ack */
721 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
722 
723 	/* TX Ack */
724 	event_tx_ack(&conn, tx);
725 
726 	/* Check that data tx is no longer paused */
727 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
728 
729 	/* Done */
730 	event_done(&conn);
731 
732 	/* Release Tx */
733 	ull_cp_release_tx(&conn, tx);
734 
735 	/* */
736 	while (!is_instant_reached(&conn, instant)) {
737 		/* Prepare */
738 		event_prepare(&conn);
739 
740 		/* Tx Queue should NOT have a LL Control PDU */
741 		lt_rx_q_is_empty(&conn);
742 
743 		/* Done */
744 		event_done(&conn);
745 
746 		/* There should NOT be a host notification */
747 		ut_rx_q_is_empty();
748 	}
749 
750 	/* Prepare */
751 	event_prepare(&conn);
752 
753 	/* Tx Queue should NOT have a LL Control PDU */
754 	lt_rx_q_is_empty(&conn);
755 
756 	/* Done */
757 	event_done(&conn);
758 
759 	/* There should be one host notification */
760 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
761 	ut_rx_q_is_empty();
762 
763 	/* Release Ntf */
764 	release_ntf(ntf);
765 
766 	CHECK_CURRENT_PHY_STATE(conn, PHY_2M, PREFER_S8_CODING, PHY_1M);
767 	CHECK_PREF_PHY_STATE(conn, PHY_1M | PHY_2M | PHY_CODED, PHY_1M | PHY_2M | PHY_CODED);
768 
769 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
770 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
771 }
772 
ZTEST(phy_periph,test_phy_update_periph_loc_unsupp_feat)773 ZTEST(phy_periph, test_phy_update_periph_loc_unsupp_feat)
774 {
775 	uint8_t err;
776 	struct node_tx *tx;
777 	struct node_rx_pdu *ntf;
778 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
779 
780 	struct pdu_data_llctrl_unknown_rsp unknown_rsp = { .type = PDU_DATA_LLCTRL_TYPE_PHY_REQ };
781 
782 	struct node_rx_pu pu = { .status = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE };
783 
784 	/* Role */
785 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
786 
787 	/* Connect */
788 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
789 
790 	/* Initiate an PHY Update Procedure */
791 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
792 	zassert_equal(err, BT_HCI_ERR_SUCCESS, NULL);
793 
794 	/* Prepare */
795 	event_prepare(&conn);
796 
797 	/* Tx Queue should have one LL Control PDU */
798 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
799 	lt_rx_q_is_empty(&conn);
800 
801 	/* Rx */
802 	lt_tx(LL_UNKNOWN_RSP, &conn, &unknown_rsp);
803 
804 	/* Check that data tx was paused */
805 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
806 
807 	/* TX Ack */
808 	event_tx_ack(&conn, tx);
809 
810 	/* Check that data tx is no longer paused */
811 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
812 
813 	/* Done */
814 	event_done(&conn);
815 
816 	/* Check that data tx is no longer paused */
817 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
818 
819 	/* Release Tx */
820 	ull_cp_release_tx(&conn, tx);
821 
822 	/* Prepare */
823 	event_prepare(&conn);
824 
825 	/* Done */
826 	event_done(&conn);
827 
828 	/* There should be one host notification */
829 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
830 	ut_rx_q_is_empty();
831 
832 	/* Release Ntf */
833 	release_ntf(ntf);
834 
835 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
836 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
837 }
838 
ZTEST(phy_periph,test_phy_update_periph_rem_invalid)839 ZTEST(phy_periph, test_phy_update_periph_rem_invalid)
840 {
841 	struct node_tx *tx;
842 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_2M };
843 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M | PHY_CODED,
844 					       .tx_phys = PHY_1M | PHY_2M | PHY_CODED };
845 	struct pdu_data_llctrl_reject_ind reject_ind = { };
846 
847 	/* Role */
848 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
849 
850 	/* Connect */
851 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
852 
853 	/* Prepare */
854 	event_prepare(&conn);
855 
856 	/* Tx Queue should NOT have a LL Control PDU */
857 	lt_rx_q_is_empty(&conn);
858 
859 	/* Rx */
860 	lt_tx(LL_PHY_REQ, &conn, &req);
861 
862 	/* Done */
863 	event_done(&conn);
864 
865 	/* We received a REQ, so data tx should be paused */
866 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
867 
868 	/* Prepare */
869 	event_prepare(&conn);
870 
871 	/* Tx Queue should have one LL Control PDU */
872 	lt_rx(LL_PHY_RSP, &conn, &tx, &rsp);
873 	lt_rx_q_is_empty(&conn);
874 
875 	/* Inject invalid PDU */
876 	lt_tx(LL_REJECT_IND, &conn, &reject_ind);
877 
878 	/* TX Ack */
879 	event_tx_ack(&conn, tx);
880 
881 	/* Done */
882 	event_done(&conn);
883 
884 	/* Termination 'triggered' */
885 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
886 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
887 
888 	/* Release Tx */
889 	ull_cp_release_tx(&conn, tx);
890 
891 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
892 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
893 }
894 
ZTEST(phy_central,test_phy_update_central_loc_collision)895 ZTEST(phy_central, test_phy_update_central_loc_collision)
896 {
897 	uint8_t err;
898 	struct node_tx *tx;
899 	struct node_rx_pdu *ntf;
900 	struct pdu_data *pdu;
901 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
902 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M,
903 					       .tx_phys = PHY_1M | PHY_2M };
904 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 9,
905 						   .c_to_p_phy = PHY_2M,
906 						   .p_to_c_phy = PHY_2M };
907 	uint16_t instant;
908 
909 	struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
910 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_PHY_REQ,
911 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
912 	};
913 
914 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
915 
916 	/* Role */
917 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
918 
919 	/* Emulate valid feature exchange */
920 	conn.llcp.fex.valid = 1;
921 
922 	/* Connect */
923 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
924 
925 	/* Initiate an PHY Update Procedure */
926 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
927 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
928 
929 	/*** ***/
930 
931 	/* Prepare */
932 	event_prepare(&conn);
933 
934 	/* Tx Queue should have one LL Control PDU */
935 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
936 	lt_rx_q_is_empty(&conn);
937 
938 	/* Rx - emulate colliding PHY_REQ from peer */
939 	lt_tx(LL_PHY_REQ, &conn, &req);
940 
941 	/* Check that data tx is paused */
942 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
943 
944 	/* TX Ack */
945 	event_tx_ack(&conn, tx);
946 
947 	/* Check that data tx is paused */
948 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
949 
950 	/* Done */
951 	event_done(&conn);
952 
953 	/* Check that data tx is paused */
954 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
955 
956 	/* Release Tx */
957 	ull_cp_release_tx(&conn, tx);
958 
959 	/*** ***/
960 
961 	/* Prepare */
962 	event_prepare(&conn);
963 	test_print_conn(&conn);
964 	/* Tx Queue should have one LL Control PDU */
965 	printf("Tx REJECT\n");
966 	lt_rx(LL_REJECT_EXT_IND, &conn, &tx, &reject_ext_ind);
967 	lt_rx_q_is_empty(&conn);
968 
969 	/* TX Ack */
970 	event_tx_ack(&conn, tx);
971 
972 	/* Done */
973 	printf("Done again\n");
974 	event_done(&conn);
975 
976 	/* Release Tx */
977 	ull_cp_release_tx(&conn, tx);
978 
979 	/*** ***/
980 
981 	/* Prepare */
982 	event_prepare(&conn);
983 
984 	/* Tx Queue should NOT have a LL Control PDU */
985 	printf("Empty\n");
986 	lt_rx_q_is_empty(&conn);
987 
988 	/* Rx */
989 	printf("Tx again\n");
990 	lt_tx(LL_PHY_RSP, &conn, &rsp);
991 
992 	/* TX Ack */
993 	event_tx_ack(&conn, tx);
994 
995 	/* Done */
996 	event_done(&conn);
997 
998 	/* Check that data tx is paused */
999 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1000 
1001 	/*** ***/
1002 
1003 	/* Prepare */
1004 	event_prepare(&conn);
1005 
1006 	/* Tx Queue should have one LL Control PDU */
1007 	printf("And again\n");
1008 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind);
1009 	lt_rx_q_is_empty(&conn);
1010 
1011 	/* TX Ack */
1012 	event_tx_ack(&conn, tx);
1013 
1014 	/* Done */
1015 	event_done(&conn);
1016 
1017 	/* Check that data tx is not paused */
1018 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1019 
1020 	/* Save Instant */
1021 	pdu = (struct pdu_data *)tx->pdu;
1022 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
1023 
1024 	/* Release Tx */
1025 	ull_cp_release_tx(&conn, tx);
1026 
1027 	/* */
1028 	while (!is_instant_reached(&conn, instant)) {
1029 		/* Prepare */
1030 		event_prepare(&conn);
1031 
1032 		/* Tx Queue should NOT have a LL Control PDU */
1033 		lt_rx_q_is_empty(&conn);
1034 
1035 		/* Done */
1036 		event_done(&conn);
1037 
1038 		/* There should NOT be a host notification */
1039 		ut_rx_q_is_empty();
1040 	}
1041 
1042 	/*** ***/
1043 
1044 	/* Prepare */
1045 	event_prepare(&conn);
1046 
1047 	/* Tx Queue should NOT have a LL Control PDU */
1048 	lt_rx_q_is_empty(&conn);
1049 
1050 	/* Done */
1051 	event_done(&conn);
1052 
1053 	/* There should be one host notification */
1054 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1055 	ut_rx_q_is_empty();
1056 
1057 	/* Release Ntf */
1058 	release_ntf(ntf);
1059 
1060 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1061 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
1062 }
1063 
ZTEST(phy_central,test_phy_update_central_rem_collision)1064 ZTEST(phy_central, test_phy_update_central_rem_collision)
1065 {
1066 	uint8_t err;
1067 	struct node_tx *tx;
1068 	struct node_rx_pdu *ntf;
1069 	struct pdu_data *pdu;
1070 	struct pdu_data_llctrl_phy_req req_peripheral = { .rx_phys = PHY_1M, .tx_phys = PHY_2M };
1071 	struct pdu_data_llctrl_phy_req req_central = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
1072 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M,
1073 					       .tx_phys = PHY_1M | PHY_2M };
1074 	struct pdu_data_llctrl_phy_upd_ind ind_1 = { .instant = 7,
1075 						     .c_to_p_phy = 0,
1076 						     .p_to_c_phy = PHY_2M };
1077 	struct pdu_data_llctrl_phy_upd_ind ind_2 = {
1078 		.instant = 15, .c_to_p_phy = PHY_2M, .p_to_c_phy = 0};
1079 	uint16_t instant;
1080 
1081 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
1082 
1083 	/* Role */
1084 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
1085 
1086 	/* Connect */
1087 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1088 
1089 	/*** ***/
1090 
1091 	/* Prepare */
1092 	event_prepare(&conn);
1093 
1094 	/* Rx */
1095 	lt_tx(LL_PHY_REQ, &conn, &req_peripheral);
1096 
1097 	/* Done */
1098 	event_done(&conn);
1099 
1100 	/* Check that data tx was paused */
1101 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1102 
1103 	/*** ***/
1104 
1105 	/* Initiate an PHY Update Procedure */
1106 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
1107 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1108 
1109 	/*** ***/
1110 
1111 	/* Prepare */
1112 	event_prepare(&conn);
1113 
1114 	/* Tx Queue should have one LL Control PDU */
1115 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind_1);
1116 	lt_rx_q_is_empty(&conn);
1117 
1118 	/* TX Ack */
1119 	event_tx_ack(&conn, tx);
1120 
1121 	/* Done */
1122 	event_done(&conn);
1123 
1124 	/* Check that data tx is no longer paused */
1125 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1126 
1127 	/* Save Instant */
1128 	pdu = (struct pdu_data *)tx->pdu;
1129 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
1130 
1131 	/* Release Tx */
1132 	ull_cp_release_tx(&conn, tx);
1133 
1134 	/* */
1135 	while (!is_instant_reached(&conn, instant)) {
1136 		/* Prepare */
1137 		event_prepare(&conn);
1138 
1139 		/* Tx Queue should NOT have a LL Control PDU */
1140 		lt_rx_q_is_empty(&conn);
1141 
1142 		/* Done */
1143 		event_done(&conn);
1144 
1145 		/* There should NOT be a host notification */
1146 		ut_rx_q_is_empty();
1147 	}
1148 
1149 	/* Execute connection event that is an instant. It is required to send notifications to
1150 	 * Host that complete already started PHY update procedure.
1151 	 */
1152 
1153 	/* Prepare */
1154 	event_prepare(&conn);
1155 
1156 	/* Tx Queue should NOT have a LL Control PDU */
1157 	lt_rx_q_is_empty(&conn);
1158 
1159 	/* Done */
1160 	event_done(&conn);
1161 
1162 	/* Start execution of a paused local PHY update procedure. It is delayed by one connection
1163 	 * event due to completion of remote PHY update at end of the "at instant" conneciton event.
1164 	 */
1165 
1166 	/* Check that data tx is no longer paused */
1167 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1168 
1169 	/* Prepare */
1170 	event_prepare(&conn);
1171 
1172 	/* Tx Queue should have one LL Control PDU */
1173 	lt_rx(LL_PHY_REQ, &conn, &tx, &req_central);
1174 	lt_rx_q_is_empty(&conn);
1175 
1176 	/* Rx */
1177 	lt_tx(LL_PHY_RSP, &conn, &rsp);
1178 
1179 	/* TX Ack */
1180 	event_tx_ack(&conn, tx);
1181 
1182 	/* Done */
1183 	event_done(&conn);
1184 
1185 	/* Check that data tx was paused */
1186 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1187 
1188 	/* Release Tx */
1189 	ull_cp_release_tx(&conn, tx);
1190 
1191 	/* There should be one host notification */
1192 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1193 	ut_rx_q_is_empty();
1194 
1195 	/* Release Ntf */
1196 	release_ntf(ntf);
1197 
1198 	/* Prepare */
1199 	event_prepare(&conn);
1200 
1201 	/* Tx Queue should have one LL Control PDU */
1202 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind_2);
1203 	lt_rx_q_is_empty(&conn);
1204 
1205 	/* TX Ack */
1206 	event_tx_ack(&conn, tx);
1207 
1208 	/* Done */
1209 	event_done(&conn);
1210 
1211 	/* Check that data tx is no longer paused */
1212 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1213 
1214 	/* Save Instant */
1215 	pdu = (struct pdu_data *)tx->pdu;
1216 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
1217 
1218 	/* Release Tx */
1219 	ull_cp_release_tx(&conn, tx);
1220 
1221 	/* */
1222 	while (!is_instant_reached(&conn, instant)) {
1223 		/* Prepare */
1224 		event_prepare(&conn);
1225 
1226 		/* Tx Queue should NOT have a LL Control PDU */
1227 		lt_rx_q_is_empty(&conn);
1228 
1229 		/* Done */
1230 		event_done(&conn);
1231 
1232 		/* There should NOT be a host notification */
1233 		ut_rx_q_is_empty();
1234 	}
1235 
1236 	/* Prepare */
1237 	event_prepare(&conn);
1238 
1239 	/* Tx Queue should NOT have a LL Control PDU */
1240 	lt_rx_q_is_empty(&conn);
1241 
1242 	/* Done */
1243 	event_done(&conn);
1244 
1245 	/* There should be one host notification */
1246 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1247 	ut_rx_q_is_empty();
1248 
1249 	/* Release Ntf */
1250 	release_ntf(ntf);
1251 
1252 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1253 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
1254 }
1255 
ZTEST(phy_periph,test_phy_update_periph_loc_collision)1256 ZTEST(phy_periph, test_phy_update_periph_loc_collision)
1257 {
1258 	uint8_t err;
1259 	struct node_tx *tx;
1260 	struct node_rx_pdu *ntf;
1261 	struct pdu_data_llctrl_phy_req req_central = { .rx_phys = PHY_1M, .tx_phys = PHY_2M };
1262 	struct pdu_data_llctrl_phy_req req_peripheral = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
1263 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_2M, .tx_phys = PHY_2M };
1264 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 7,
1265 						   .c_to_p_phy = PHY_2M,
1266 						   .p_to_c_phy = PHY_1M };
1267 	uint16_t instant;
1268 
1269 	struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
1270 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_PHY_REQ,
1271 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
1272 	};
1273 
1274 	struct node_rx_pu pu = { 0 };
1275 
1276 	/* Role */
1277 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1278 
1279 	/* Connect */
1280 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1281 
1282 	/*** ***/
1283 
1284 	/* Initiate an PHY Update Procedure */
1285 	err = ull_cp_phy_update(&conn, PHY_2M, PREFER_S8_CODING, PHY_2M, HOST_INITIATED);
1286 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1287 
1288 	/* Prepare */
1289 	event_prepare(&conn);
1290 
1291 	/* Tx Queue should have one LL Control PDU */
1292 	lt_rx(LL_PHY_REQ, &conn, &tx, &req_peripheral);
1293 	lt_rx_q_is_empty(&conn);
1294 
1295 	/* Rx */
1296 	lt_tx(LL_PHY_REQ, &conn, &req_central);
1297 
1298 	/* Check that data tx was paused */
1299 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1300 
1301 	/* TX Ack */
1302 	event_tx_ack(&conn, tx);
1303 
1304 	/* Check that data tx is no longer paused */
1305 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1306 
1307 	/* Done */
1308 	event_done(&conn);
1309 
1310 	/* Release Tx */
1311 	ull_cp_release_tx(&conn, tx);
1312 
1313 	/* Prepare */
1314 	event_prepare(&conn);
1315 
1316 	/* Tx Queue should have one LL Control PDU */
1317 	lt_rx(LL_PHY_RSP, &conn, &tx, &rsp);
1318 	lt_rx_q_is_empty(&conn);
1319 
1320 	/* Rx */
1321 	lt_tx(LL_REJECT_EXT_IND, &conn, &reject_ext_ind);
1322 
1323 	/* Done */
1324 	event_done(&conn);
1325 
1326 	/* Prepare */
1327 	event_prepare(&conn);
1328 
1329 	/* Done */
1330 	event_done(&conn);
1331 
1332 	/* There should be one host notification */
1333 	pu.status = BT_HCI_ERR_LL_PROC_COLLISION;
1334 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1335 	ut_rx_q_is_empty();
1336 
1337 	/* Release Ntf */
1338 	release_ntf(ntf);
1339 
1340 	/* Prepare */
1341 	event_prepare(&conn);
1342 
1343 	/* Rx */
1344 	ind.instant = instant = event_counter(&conn) + 6;
1345 	lt_tx(LL_PHY_UPDATE_IND, &conn, &ind);
1346 
1347 	/* TX Ack */
1348 	event_tx_ack(&conn, tx);
1349 
1350 	/* Done */
1351 	event_done(&conn);
1352 
1353 	/* Release Tx */
1354 	ull_cp_release_tx(&conn, tx);
1355 
1356 	/* Check that data tx is no longer paused */
1357 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1358 
1359 	/* */
1360 	while (!is_instant_reached(&conn, instant)) {
1361 		/* Prepare */
1362 		event_prepare(&conn);
1363 
1364 		/* Tx Queue should NOT have a LL Control PDU */
1365 		lt_rx_q_is_empty(&conn);
1366 
1367 		/* Done */
1368 		event_done(&conn);
1369 
1370 		/* There should NOT be a host notification */
1371 		ut_rx_q_is_empty();
1372 	}
1373 
1374 	/* Prepare */
1375 	event_prepare(&conn);
1376 
1377 	/* Tx Queue should NOT have a LL Control PDU */
1378 	lt_rx_q_is_empty(&conn);
1379 
1380 	/* Done */
1381 	event_done(&conn);
1382 
1383 	/* There should be one host notification */
1384 	pu.status = BT_HCI_ERR_SUCCESS;
1385 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1386 	ut_rx_q_is_empty();
1387 
1388 	/* Release Ntf */
1389 	release_ntf(ntf);
1390 
1391 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1392 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
1393 }
1394 
ZTEST(phy_central,test_phy_update_central_loc_no_act_change)1395 ZTEST(phy_central, test_phy_update_central_loc_no_act_change)
1396 {
1397 	uint8_t err;
1398 	struct node_tx *tx;
1399 	struct node_rx_pdu *ntf;
1400 	struct pdu_data *pdu;
1401 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_1M };
1402 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M,
1403 					       .tx_phys = PHY_1M | PHY_2M };
1404 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 0, .c_to_p_phy = 0, .p_to_c_phy = 0 };
1405 	uint16_t instant;
1406 
1407 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
1408 
1409 	/* Role */
1410 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
1411 
1412 	/* Connect */
1413 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1414 
1415 	/* Initiate an PHY Update Procedure */
1416 	err = ull_cp_phy_update(&conn, PHY_1M, PREFER_S8_CODING, PHY_1M, HOST_INITIATED);
1417 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1418 
1419 	/* Prepare */
1420 	event_prepare(&conn);
1421 
1422 	/* Tx Queue should have one LL Control PDU */
1423 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
1424 	lt_rx_q_is_empty(&conn);
1425 
1426 	/* Rx */
1427 	lt_tx(LL_PHY_RSP, &conn, &rsp);
1428 
1429 	/* TX Ack */
1430 	event_tx_ack(&conn, tx);
1431 
1432 	/* Done */
1433 	event_done(&conn);
1434 
1435 	/* Check that data tx was paused */
1436 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1437 
1438 	/* Release Tx */
1439 	ull_cp_release_tx(&conn, tx);
1440 
1441 	/* Prepare */
1442 	event_prepare(&conn);
1443 
1444 	/* Tx Queue should have one LL Control PDU */
1445 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind);
1446 	lt_rx_q_is_empty(&conn);
1447 
1448 	/* TX Ack */
1449 	event_tx_ack(&conn, tx);
1450 
1451 	/* Check that data tx is no longer paused */
1452 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1453 
1454 	/* Done */
1455 	event_done(&conn);
1456 
1457 	/* Save Instant */
1458 	pdu = (struct pdu_data *)tx->pdu;
1459 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
1460 
1461 	/* Check if instant is zero, due to no actual PHY change */
1462 	zassert_equal(instant, 0, "Unexpected instant %d", instant);
1463 
1464 	/* Release Tx */
1465 	ull_cp_release_tx(&conn, tx);
1466 
1467 	/* There should be one host notifications, due to host initiated PHY upd */
1468 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1469 	ut_rx_q_is_empty();
1470 
1471 	/* Release Ntf */
1472 	release_ntf(ntf);
1473 
1474 	CHECK_CURRENT_PHY_STATE(conn, PHY_1M, PREFER_S8_CODING, PHY_1M);
1475 	CHECK_PREF_PHY_STATE(conn, PHY_1M, PHY_1M);
1476 
1477 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(), "Free CTX buffers %d",
1478 		      llcp_ctx_buffers_free());
1479 }
1480 
ZTEST(phy_central,test_phy_update_central_rem_no_actual_change)1481 ZTEST(phy_central, test_phy_update_central_rem_no_actual_change)
1482 {
1483 	struct node_tx *tx;
1484 	struct pdu_data *pdu;
1485 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_1M };
1486 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 0, .c_to_p_phy = 0, .p_to_c_phy = 0 };
1487 	uint16_t instant;
1488 
1489 	/* Role */
1490 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
1491 
1492 	/* Connect */
1493 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1494 
1495 	/* Prepare */
1496 	event_prepare(&conn);
1497 
1498 	/* Rx */
1499 	lt_tx(LL_PHY_REQ, &conn, &req);
1500 
1501 	/* TX Ack */
1502 	event_tx_ack(&conn, tx);
1503 
1504 	/* Done */
1505 	event_done(&conn);
1506 
1507 	/* Check that data tx was paused */
1508 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1509 
1510 	/* Prepare */
1511 	event_prepare(&conn);
1512 
1513 	/* Tx Queue should have one LL Control PDU */
1514 	lt_rx(LL_PHY_UPDATE_IND, &conn, &tx, &ind);
1515 	lt_rx_q_is_empty(&conn);
1516 
1517 	/* TX Ack */
1518 	event_tx_ack(&conn, tx);
1519 
1520 	/* Done */
1521 	event_done(&conn);
1522 
1523 	/* Check that data tx is no longer paused */
1524 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1525 
1526 	/* Save Instant */
1527 	pdu = (struct pdu_data *)tx->pdu;
1528 	instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
1529 
1530 	/* Check if instant is zero, due to no actual PHY change */
1531 	zassert_equal(instant, 0, "Unexpected instant %d", instant);
1532 
1533 	/* Release Tx */
1534 	ull_cp_release_tx(&conn, tx);
1535 
1536 	/* There is not actual PHY change, so there shouldn't be host notification */
1537 	ut_rx_q_is_empty();
1538 
1539 	CHECK_CURRENT_PHY_STATE(conn, PHY_1M, PREFER_S8_CODING, PHY_1M);
1540 	CHECK_PREF_PHY_STATE(conn, PHY_1M | PHY_2M | PHY_CODED, PHY_1M | PHY_2M | PHY_CODED);
1541 
1542 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(), "Free CTX buffers %d",
1543 		      llcp_ctx_buffers_free());
1544 }
1545 
ZTEST(phy_periph,test_phy_update_periph_loc_no_actual_change)1546 ZTEST(phy_periph, test_phy_update_periph_loc_no_actual_change)
1547 {
1548 	uint8_t err;
1549 	struct node_tx *tx;
1550 	struct node_rx_pdu *ntf;
1551 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_1M };
1552 	struct node_rx_pu pu = { .status = BT_HCI_ERR_SUCCESS };
1553 	struct pdu_data_llctrl_phy_upd_ind phy_update_ind = { .instant = 0,
1554 							      .c_to_p_phy = 0,
1555 							      .p_to_c_phy = 0 };
1556 
1557 	/* Role */
1558 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1559 
1560 	/* Connect */
1561 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1562 
1563 	/* Initiate an PHY Update Procedure */
1564 	err = ull_cp_phy_update(&conn, PHY_1M, PREFER_S8_CODING, PHY_1M, HOST_INITIATED);
1565 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
1566 
1567 	/* Prepare */
1568 	event_prepare(&conn);
1569 
1570 	/* Tx Queue should have one LL Control PDU */
1571 	lt_rx(LL_PHY_REQ, &conn, &tx, &req);
1572 	lt_rx_q_is_empty(&conn);
1573 
1574 	/* Check that data tx was paused */
1575 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1576 
1577 	/* TX Ack */
1578 	event_tx_ack(&conn, tx);
1579 
1580 	/* Check that data tx is no longer paused */
1581 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1582 
1583 	/* Done */
1584 	event_done(&conn);
1585 
1586 	/* Release Tx */
1587 	ull_cp_release_tx(&conn, tx);
1588 
1589 	/* Prepare */
1590 	event_prepare(&conn);
1591 
1592 	/* Tx Queue should NOT have a LL Control PDU */
1593 	lt_rx_q_is_empty(&conn);
1594 
1595 	/* Rx */
1596 	lt_tx(LL_PHY_UPDATE_IND, &conn, &phy_update_ind);
1597 
1598 	/* Done */
1599 	event_done(&conn);
1600 
1601 	/* Prepare */
1602 	event_prepare(&conn);
1603 
1604 	/* Done */
1605 	event_done(&conn);
1606 
1607 	/* Check that data tx is no longer paused */
1608 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1609 
1610 	/* There should be one notification due to Host initiated PHY UPD */
1611 	ut_rx_node(NODE_PHY_UPDATE, &ntf, &pu);
1612 	ut_rx_q_is_empty();
1613 
1614 	/* Release Ntf */
1615 	release_ntf(ntf);
1616 
1617 	CHECK_CURRENT_PHY_STATE(conn, PHY_1M, PREFER_S8_CODING, PHY_1M);
1618 	CHECK_PREF_PHY_STATE(conn, PHY_1M, PHY_1M);
1619 
1620 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(), "Free CTX buffers %d",
1621 		      llcp_ctx_buffers_free());
1622 }
1623 
ZTEST(phy_periph,test_phy_update_periph_rem_no_actual_change)1624 ZTEST(phy_periph, test_phy_update_periph_rem_no_actual_change)
1625 {
1626 	struct node_tx *tx;
1627 	struct pdu_data_llctrl_phy_req req = { .rx_phys = PHY_1M, .tx_phys = PHY_1M };
1628 	struct pdu_data_llctrl_phy_req rsp = { .rx_phys = PHY_1M | PHY_2M | PHY_CODED,
1629 					       .tx_phys = PHY_1M | PHY_2M | PHY_CODED };
1630 	struct pdu_data_llctrl_phy_upd_ind ind = { .instant = 0, .c_to_p_phy = 0, .p_to_c_phy = 0 };
1631 
1632 	/* Role */
1633 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1634 
1635 	/* Connect */
1636 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1637 
1638 	/* Prepare */
1639 	event_prepare(&conn);
1640 
1641 	/* Tx Queue should NOT have a LL Control PDU */
1642 	lt_rx_q_is_empty(&conn);
1643 
1644 	/* Rx */
1645 	lt_tx(LL_PHY_REQ, &conn, &req);
1646 
1647 	/* Done */
1648 	event_done(&conn);
1649 
1650 	/* We received a REQ, so data tx should be paused */
1651 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1652 
1653 	/* Prepare */
1654 	event_prepare(&conn);
1655 
1656 	/* Tx Queue should have one LL Control PDU */
1657 	lt_rx(LL_PHY_RSP, &conn, &tx, &rsp);
1658 	lt_rx_q_is_empty(&conn);
1659 
1660 	/* Rx */
1661 	lt_tx(LL_PHY_UPDATE_IND, &conn, &ind);
1662 
1663 	/* We are sending RSP, so data tx should be paused until after tx ack */
1664 	zassert_equal(conn.tx_q.pause_data, 1U, "Data tx is not paused");
1665 
1666 	/* TX Ack */
1667 	event_tx_ack(&conn, tx);
1668 
1669 	/* Check that data tx is no longer paused */
1670 	zassert_equal(conn.tx_q.pause_data, 0U, "Data tx is paused");
1671 
1672 	/* Done */
1673 	event_done(&conn);
1674 
1675 	/* Release Tx */
1676 	ull_cp_release_tx(&conn, tx);
1677 
1678 	/* There should be no host notification */
1679 	ut_rx_q_is_empty();
1680 
1681 	/* Prepare */
1682 	event_prepare(&conn);
1683 
1684 	/* Done */
1685 	event_done(&conn);
1686 
1687 	CHECK_CURRENT_PHY_STATE(conn, PHY_1M, PREFER_S8_CODING, PHY_1M);
1688 	CHECK_PREF_PHY_STATE(conn, PHY_1M | PHY_2M | PHY_CODED, PHY_1M | PHY_2M | PHY_CODED);
1689 
1690 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(), "Free CTX buffers %d",
1691 		      llcp_ctx_buffers_free());
1692 }
1693 
1694 ZTEST_SUITE(phy_central, NULL, NULL, phy_setup, NULL, NULL);
1695 ZTEST_SUITE(phy_periph, NULL, NULL, phy_setup, NULL, NULL);
1696