1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/types.h>
8 #include <zephyr/sys/byteorder.h>
9 #include <zephyr/ztest.h>
10 
11 #define ULL_LLCP_UNITTEST
12 
13 #include <zephyr/bluetooth/hci.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/sys/slist.h>
16 #include <zephyr/sys/util.h>
17 #include "hal/ccm.h"
18 
19 #include "util/util.h"
20 #include "util/mem.h"
21 #include "util/memq.h"
22 #include "util/dbuf.h"
23 
24 #include "pdu_df.h"
25 #include "lll/pdu_vendor.h"
26 #include "pdu.h"
27 #include "ll.h"
28 #include "ll_feat.h"
29 #include "ll_settings.h"
30 
31 #include "lll.h"
32 #include "lll/lll_df_types.h"
33 #include "lll_conn.h"
34 #include "lll_conn_iso.h"
35 
36 #include "ull_tx_queue.h"
37 
38 #include "isoal.h"
39 #include "ull_iso_types.h"
40 #include "ull_conn_iso_types.h"
41 #include "ull_internal.h"
42 #include "ull_conn_types.h"
43 #include "ull_llcp.h"
44 #include "ull_conn_internal.h"
45 #include "ull_llcp_internal.h"
46 #include "ull_llcp_features.h"
47 
48 #include "helper_pdu.h"
49 #include "helper_util.h"
50 #include "helper_features.h"
51 
52 static struct ll_conn conn;
53 
dle_setup(void * data)54 static void dle_setup(void *data)
55 {
56 	test_setup(&conn);
57 }
58 
59 /*
60  * Locally triggered Data Length Update procedure
61  *
62  * +-----+                     +-------+                       +-----+
63  * | UT  |                     | LL_A  |                       | LT  |
64  * +-----+                     +-------+                       +-----+
65  *    |                            |                              |
66  *    | Start                      |                              |
67  *    | Data Length Update Proc.   |                              |
68  *    |--------------------------->|                              |
69  *    |                            |  (251,2120,211,1800)         |
70  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
71  *    |                            |----------------------------->|
72  *    |                            |     (201,1720,251,2120)      |
73  *    |                            |    LL_DATA_LENGTH_UPDATE_RSP |
74  *    |                            |<-----------------------------|
75  *    | (251,2120,201,1720)        |                              |
76  *    | Data Length Update Proc.   |                              |
77  *    |                   Complete |                              |
78  *    |<---------------------------|                              |
79  *    |                            |                              |
80  */
81 
ZTEST(dle_central,test_data_length_update_central_loc)82 ZTEST(dle_central, test_data_length_update_central_loc)
83 {
84 	uint8_t err;
85 	struct node_tx *tx;
86 	struct node_rx_pdu *ntf;
87 
88 	struct pdu_data_llctrl_length_req local_length_req = { 251, 2120, 211, 1800 };
89 	struct pdu_data_llctrl_length_rsp remote_length_rsp = { 201, 1720, 251, 2120 };
90 	struct pdu_data_llctrl_length_rsp length_ntf = { 251, 2120, 201, 1720 };
91 
92 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
93 	/* Connect */
94 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
95 	/* Init DLE data */
96 	ull_conn_default_tx_octets_set(251);
97 	ull_conn_default_tx_time_set(2120);
98 	ull_dle_init(&conn, PHY_1M);
99 
100 	/* Initiate a Data Length Update Procedure */
101 	err = ull_cp_data_length_update(&conn, 211, 1800);
102 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
103 
104 	event_prepare(&conn);
105 	/* Tx Queue should have one LL Control PDU */
106 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
107 	lt_rx_q_is_empty(&conn);
108 
109 	/* TX Ack */
110 	event_tx_ack(&conn, tx);
111 
112 	/* Rx */
113 	lt_tx(LL_LENGTH_RSP, &conn, &remote_length_rsp);
114 
115 	event_done(&conn);
116 
117 	/* There should be one host notification */
118 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
119 	ut_rx_q_is_empty();
120 	zassert_equal(conn.lll.event_counter, 1, "Wrong event-count %d\n",
121 				  conn.lll.event_counter);
122 
123 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
124 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
125 }
126 
127 /*
128  * Locally triggered Data Length Update procedure
129  *
130  * +-----+                     +-------+                       +-----+
131  * | UT  |                     | LL_A  |                       | LT  |
132  * +-----+                     +-------+                       +-----+
133  *    |                            |                              |
134  *    | Start                      |                              |
135  *    | Data Length Update Proc.   |                              |
136  *    |--------------------------->|                              |
137  *    |                            |  (251,2120,211,1800)         |
138  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
139  *    |                            |----------------------------->|
140  *    |                            |                              |
141  *    |                            |         LL_UNKNOWN_RSP       |
142  *    |                            |<-----------------------------|
143  *    |                            |                              |
144  *  ~~~~~~~~~~~~~~~~~~~~~~~  Unmask DLE support ~~~~~~~~~~~~~~~~~~~~
145  *    |                            |                              |
146  *    |                            |                              |
147  */
ZTEST(dle_central,test_data_length_update_central_loc_unknown_rsp)148 ZTEST(dle_central, test_data_length_update_central_loc_unknown_rsp)
149 {
150 	uint8_t err;
151 	struct node_tx *tx;
152 	struct pdu_data_llctrl_unknown_rsp unknown_rsp = {
153 		.type = PDU_DATA_LLCTRL_TYPE_LENGTH_REQ
154 	};
155 	struct pdu_data_llctrl_length_req local_length_req = { 251, 2120, 211, 1800 };
156 
157 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
158 	/* Connect */
159 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
160 	/* Init DLE data */
161 	ull_conn_default_tx_octets_set(251);
162 	ull_conn_default_tx_time_set(2120);
163 	ull_dle_init(&conn, PHY_1M);
164 
165 	/* Confirm DLE is indicated as supported */
166 	zassert_equal(feature_dle(&conn), true, "DLE Feature masked out");
167 
168 	/* Initiate a Data Length Update Procedure */
169 	err = ull_cp_data_length_update(&conn, 211, 1800);
170 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
171 
172 	event_prepare(&conn);
173 	/* Tx Queue should have one LL Control PDU */
174 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
175 	lt_rx_q_is_empty(&conn);
176 
177 	/* TX Ack */
178 	event_tx_ack(&conn, tx);
179 
180 	/* Rx */
181 	lt_tx(LL_UNKNOWN_RSP, &conn, &unknown_rsp);
182 
183 	event_done(&conn);
184 
185 	/* Release tx node */
186 	ull_cp_release_tx(&conn, tx);
187 
188 	/* Confirm DLE is no longer indicated as supported */
189 	zassert_equal(feature_dle(&conn), false, "DLE Feature not masked out");
190 
191 	/* There should not be a host notifications */
192 	ut_rx_q_is_empty();
193 
194 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
195 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
196 }
197 
198 /*
199  * Locally triggered Data Length Update procedure
200  *
201  *
202  * Start a Feature Exchange procedure and Data Length Update procedure.
203  *
204  * The Feature Exchange procedure completes, removing Data Length Update
205  * procedure support.
206  *
207  * Expect that the already enqueued Data Length Update procedure completes
208  * without doing anything.
209  *
210  * +-----+                     +-------+                       +-----+
211  * | UT  |                     | LL_A  |                       | LT  |
212  * +-----+                     +-------+                       +-----+
213  *    |                            |                              |
214  *    | Start                      |                              |
215  *    | Feature Exchange Proc.     |                              |
216  *    |--------------------------->|                              |
217  *    |                            |                              |
218  *    | Start                      |                              |
219  *    | Data Length Update Proc.   |                              |
220  *    |--------------------------->|                              |
221  *    |                            |                              |
222  *    |                            | LL_FEATURE_REQ               |
223  *    |                            |----------------------------->|
224  *    |                            |                              |
225  *    |                            |               LL_FEATURE_RSP |
226  *    |                            |<-----------------------------|
227  *    |                            |                              |
228  *  ~~~~~~~~~~~~~~~~~~~~~~~  Unmask DLE support ~~~~~~~~~~~~~~~~~~~~
229  *    |                            |                              |
230  *    |     Feature Exchange Proc. |                              |
231  *    |                   Complete |                              |
232  *    |<---------------------------|                              |
233  *    |                            |                              |
234  */
ZTEST(dle_central,test_data_length_update_central_loc_unsupported)235 ZTEST(dle_central, test_data_length_update_central_loc_unsupported)
236 {
237 	uint8_t err;
238 	struct node_tx *tx;
239 	struct node_rx_pdu *ntf;
240 
241 	struct pdu_data_llctrl_feature_req local_feature_req;
242 	struct pdu_data_llctrl_feature_rsp remote_feature_rsp;
243 	struct pdu_data_llctrl_feature_rsp exp_remote_feature_rsp;
244 
245 	sys_put_le64(DEFAULT_FEATURE, local_feature_req.features);
246 	sys_put_le64(0ULL, remote_feature_rsp.features);
247 	sys_put_le64(0ULL, exp_remote_feature_rsp.features);
248 
249 
250 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
251 	/* Connect */
252 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
253 	/* Init DLE data */
254 	ull_conn_default_tx_octets_set(251);
255 	ull_conn_default_tx_time_set(2120);
256 	ull_dle_init(&conn, PHY_1M);
257 
258 	/* Confirm DLE is indicated as supported */
259 	zassert_equal(feature_dle(&conn), true, "DLE Feature masked out");
260 
261 	/* Initiate a Feature Exchange Procedure */
262 	err = ull_cp_feature_exchange(&conn, 1U);
263 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
264 
265 	/* Initiate a Data Length Update Procedure */
266 	err = ull_cp_data_length_update(&conn, 211, 1800);
267 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
268 
269 	event_prepare(&conn);
270 	/* Tx Queue should have one LL Control PDU */
271 	lt_rx(LL_FEATURE_REQ, &conn, &tx, &local_feature_req);
272 	lt_rx_q_is_empty(&conn);
273 
274 	/* Rx */
275 	lt_tx(LL_FEATURE_RSP, &conn, &remote_feature_rsp);
276 
277 	event_done(&conn);
278 	/* There should be one host notification */
279 
280 	ut_rx_pdu(LL_FEATURE_RSP, &ntf, &exp_remote_feature_rsp);
281 
282 	ut_rx_q_is_empty();
283 
284 	ull_cp_release_tx(&conn, tx);
285 	release_ntf(ntf);
286 
287 	/* Confirm DLE is no longer indicated as supported */
288 	zassert_equal(feature_dle(&conn), false, "DLE Feature not masked out");
289 
290 	/* Prepare another event for enqueued Data Length Update procedure */
291 	event_prepare(&conn);
292 	/* Tx Queue should have no LL Control PDU */
293 	lt_rx_q_is_empty(&conn);
294 	event_done(&conn);
295 
296 	/* Confirm DLE is no longer indicated as supported */
297 	zassert_equal(feature_dle(&conn), false, "DLE Feature not masked out");
298 
299 	/* There should not be a host notifications */
300 	ut_rx_q_is_empty();
301 
302 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
303 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
304 }
305 
306 /*
307  * Locally triggered Data Length Update procedure
308  *
309  * +-----+                     +-------+                       +-----+
310  * | UT  |                     | LL_A  |                       | LT  |
311  * +-----+                     +-------+                       +-----+
312  *    |                            |                              |
313  *    | Start                      |                              |
314  *    | Data Length Update Proc.   |                              |
315  *    |--------------------------->|                              |
316  *    |                            |  (251,2120,211,1800)         |
317  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
318  *    |                            |----------------------------->|
319  *    |                            |                              |
320  *    |                            |         LL_<INVALID>_RSP     |
321  *    |                            |<-----------------------------|
322  *    |                            |                              |
323  *   ~~~~~~~~~~~~~~~~~~~~  TERMINATE CONNECTION  ~~~~~~~~~~~~~~~~~~~
324  *    |                            |                              |
325  *    |                            |                              |
326  */
ZTEST(dle_central,test_data_length_update_central_loc_invalid_rsp)327 ZTEST(dle_central, test_data_length_update_central_loc_invalid_rsp)
328 {
329 	uint8_t err;
330 	struct node_tx *tx;
331 	struct pdu_data_llctrl_reject_ind reject_ind = {
332 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
333 	};
334 	struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
335 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_LENGTH_REQ,
336 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
337 	};
338 
339 	struct pdu_data_llctrl_length_req local_length_req = { 251, 2120, 211, 1800 };
340 
341 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
342 	/* Connect */
343 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
344 	/* Init DLE data */
345 	ull_conn_default_tx_octets_set(251);
346 	ull_conn_default_tx_time_set(2120);
347 	ull_dle_init(&conn, PHY_1M);
348 
349 	/* Initiate a Data Length Update Procedure */
350 	err = ull_cp_data_length_update(&conn, 211, 1800);
351 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
352 
353 	event_prepare(&conn);
354 	/* Tx Queue should have one LL Control PDU */
355 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
356 	lt_rx_q_is_empty(&conn);
357 
358 	/* TX Ack */
359 	event_tx_ack(&conn, tx);
360 
361 	/* Rx */
362 	lt_tx(LL_REJECT_IND, &conn, &reject_ind);
363 
364 	event_done(&conn);
365 
366 	/* Release tx node */
367 	ull_cp_release_tx(&conn, tx);
368 
369 	/* Termination 'triggered' */
370 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
371 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
372 
373 	/* Clear termination flag for subsequent test cycle */
374 	conn.llcp_terminate.reason_final = 0;
375 
376 	/* There should not be a host notifications */
377 	ut_rx_q_is_empty();
378 
379 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
380 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
381 
382 	/* Init DLE data */
383 	ull_conn_default_tx_octets_set(251);
384 	ull_conn_default_tx_time_set(2120);
385 	ull_dle_init(&conn, PHY_1M);
386 
387 	/* Initiate another Data Length Update Procedure */
388 	err = ull_cp_data_length_update(&conn, 211, 1800);
389 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
390 
391 	event_prepare(&conn);
392 	/* Tx Queue should have one LL Control PDU */
393 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
394 	lt_rx_q_is_empty(&conn);
395 
396 	/* TX Ack */
397 	event_tx_ack(&conn, tx);
398 
399 	/* Rx */
400 	lt_tx(LL_REJECT_EXT_IND, &conn, &reject_ext_ind);
401 
402 	event_done(&conn);
403 
404 	/* Release tx node */
405 	ull_cp_release_tx(&conn, tx);
406 
407 	/* Termination 'triggered' */
408 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
409 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
410 
411 	/* There should not be a host notifications */
412 	ut_rx_q_is_empty();
413 
414 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
415 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
416 }
417 
418 /*
419  * Locally triggered Data Length Update procedure - with no update to eff and thus no ntf
420  *
421  * +-----+                     +-------+                       +-----+
422  * | UT  |                     | LL_A  |                       | LT  |
423  * +-----+                     +-------+                       +-----+
424  *    |                            |                              |
425  *    | Start                      |                              |
426  *    | Data Length Update Proc.   |                              |
427  *    |--------------------------->|                              |
428  *    |                            |  (251,2120,211,1800)         |
429  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
430  *    |                            |----------------------------->|
431  *    |                            |     (27,328,27,328)          |
432  *    |                            |    LL_DATA_LENGTH_UPDATE_RSP |
433  *    |                            |<-----------------------------|
434  *    |                            |                              |
435  */
ZTEST(dle_central,test_data_length_update_central_loc_no_eff_change)436 ZTEST(dle_central, test_data_length_update_central_loc_no_eff_change)
437 {
438 	uint8_t err;
439 	struct node_tx *tx;
440 
441 	struct pdu_data_llctrl_length_req local_length_req = { 251, 2120, 211, 1800 };
442 	struct pdu_data_llctrl_length_rsp remote_length_rsp = { 27, 328, 27, 328 };
443 
444 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
445 	/* Connect */
446 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
447 	/* Init DLE data */
448 	ull_conn_default_tx_octets_set(251);
449 	ull_conn_default_tx_time_set(2120);
450 	ull_dle_init(&conn, PHY_1M);
451 
452 	/* Initiate a Data Length Update Procedure */
453 	err = ull_cp_data_length_update(&conn, 211, 1800);
454 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
455 
456 	event_prepare(&conn);
457 	/* Tx Queue should have one LL Control PDU */
458 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
459 	lt_rx_q_is_empty(&conn);
460 
461 	/* TX Ack */
462 	event_tx_ack(&conn, tx);
463 
464 	/* Rx */
465 	lt_tx(LL_LENGTH_RSP, &conn, &remote_length_rsp);
466 
467 	event_done(&conn);
468 
469 	/* There should be no host notification */
470 	ut_rx_q_is_empty();
471 	zassert_equal(conn.lll.event_counter, 1, "Wrong event-count %d\n",
472 				  conn.lll.event_counter);
473 }
474 /*
475  * Locally triggered Data Length Update procedure -
476  * - first updating effective DLE and then without update to eff and thus no ntf
477  *
478  * +-----+                     +-------+                       +-----+
479  * | UT  |                     | LL_A  |                       | LT  |
480  * +-----+                     +-------+                       +-----+
481  *    |                            |                              |
482  *    | Start                      |                              |
483  *    | Data Length Update Proc.   |                              |
484  *    |--------------------------->|                              |
485  *    |                            |  (251,2120,221,1800)         |
486  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
487  *    |                            |----------------------------->|
488  *    |                            |     (101,920,251,2120)       |
489  *    |                            |    LL_DATA_LENGTH_UPDATE_RSP |
490  *    |                            |<-----------------------------|
491  *    | (251,2120,101,920)         |                              |
492  *    | Data Length Update Proc.   |                              |
493  *    |                   Complete |                              |
494  *    |<---------------------------|                              |
495  *    |                            |                              |
496  *    | Start                      |                              |
497  *    | Data Length Update Proc.   |                              |
498  *    |--------------------------->|                              |
499  *    |                            |  (251,2120,211,1800)         |
500  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
501  *    |                            |----------------------------->|
502  *    |                            |     (101, 920,251,2120)      |
503  *    |                            |    LL_DATA_LENGTH_UPDATE_RSP |
504  *    |                            |<-----------------------------|
505  *    |                            |                              |
506  */
507 
ZTEST(dle_central,test_data_length_update_central_loc_no_eff_change2)508 ZTEST(dle_central, test_data_length_update_central_loc_no_eff_change2)
509 {
510 	uint8_t err;
511 	struct node_tx *tx;
512 	struct node_rx_pdu *ntf;
513 
514 	struct pdu_data_llctrl_length_req local_length_req = { 251, 2120, 211, 1800 };
515 	struct pdu_data_llctrl_length_rsp remote_length_rsp = { 101, 920, 251, 2120 };
516 	struct pdu_data_llctrl_length_rsp length_ntf = { 251, 2120, 101, 920 };
517 	struct pdu_data_llctrl_length_req local_length_req2 = { 251, 2120, 211, 1800 };
518 	struct pdu_data_llctrl_length_rsp remote_length_rsp2 = { 101, 920, 251, 2120 };
519 
520 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
521 	/* Connect */
522 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
523 	/* Init DLE data */
524 	ull_conn_default_tx_octets_set(251);
525 	ull_conn_default_tx_time_set(2120);
526 	ull_dle_init(&conn, PHY_1M);
527 
528 	/* Initiate a Data Length Update Procedure */
529 	err = ull_cp_data_length_update(&conn, 211, 1800);
530 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
531 
532 	event_prepare(&conn);
533 	/* Tx Queue should have one LL Control PDU */
534 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
535 	lt_rx_q_is_empty(&conn);
536 
537 	/* TX Ack */
538 	event_tx_ack(&conn, tx);
539 
540 	/* Rx */
541 	lt_tx(LL_LENGTH_RSP, &conn, &remote_length_rsp);
542 
543 	event_done(&conn);
544 
545 	/* There should be one host notification */
546 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
547 	ut_rx_q_is_empty();
548 	zassert_equal(conn.lll.event_counter, 1, "Wrong event-count %d\n",
549 				  conn.lll.event_counter);
550 
551 	/* Now lets generate another DLU, but one that should not result in
552 	 * change to effective numbers, thus not generate NTF
553 	 */
554 	err = ull_cp_data_length_update(&conn, 211, 1800);
555 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
556 
557 	event_prepare(&conn);
558 	/* Tx Queue should have one LL Control PDU */
559 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req2);
560 	lt_rx_q_is_empty(&conn);
561 
562 	/* TX Ack */
563 	event_tx_ack(&conn, tx);
564 
565 	/* Rx */
566 	lt_tx(LL_LENGTH_RSP, &conn, &remote_length_rsp2);
567 
568 	event_done(&conn);
569 
570 	/* There should be no host notification */
571 	ut_rx_q_is_empty();
572 	zassert_equal(conn.lll.event_counter, 2, "Wrong event-count %d\n",
573 				  conn.lll.event_counter);
574 }
575 
ZTEST(dle_periph,test_data_length_update_periph_loc)576 ZTEST(dle_periph, test_data_length_update_periph_loc)
577 {
578 	uint64_t err;
579 	struct node_tx *tx;
580 	struct node_rx_pdu *ntf;
581 
582 	struct pdu_data_llctrl_length_req local_length_req = { 251, 2120, 211, 1800 };
583 	struct pdu_data_llctrl_length_rsp remote_length_rsp = { 211, 1800, 251, 2120 };
584 	struct pdu_data_llctrl_length_rsp length_ntf = { 251, 2120, 211, 1800 };
585 
586 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
587 	/* Connect */
588 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
589 	/* Init DLE data */
590 	ull_conn_default_tx_octets_set(251);
591 	ull_conn_default_tx_time_set(2120);
592 	ull_dle_init(&conn, PHY_1M);
593 
594 	/* Initiate a Data Length Update Procedure */
595 	err = ull_cp_data_length_update(&conn, 211, 1800);
596 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
597 
598 	event_prepare(&conn);
599 	/* Tx Queue should have one LL Control PDU */
600 	lt_rx(LL_LENGTH_REQ, &conn, &tx, &local_length_req);
601 	lt_rx_q_is_empty(&conn);
602 
603 	/* TX Ack */
604 	event_tx_ack(&conn, tx);
605 
606 	/* Rx */
607 	lt_tx(LL_LENGTH_RSP, &conn, &remote_length_rsp);
608 
609 	event_done(&conn);
610 
611 	/* There should be one host notification */
612 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
613 	ut_rx_q_is_empty();
614 	zassert_equal(conn.lll.event_counter, 1, "Wrong event-count %d\n",
615 				  conn.lll.event_counter);
616 }
617 
618 /*
619  * Remotely triggered Data Length Update procedure
620  *
621  * +-----+                     +-------+                       +-----+
622  * | UT  |                     | LL_A  |                       | LT  |
623  * +-----+                     +-------+                       +-----+
624  *    |                            |                              |
625  *    |                            |  (27, 328, 251, 2120)        |
626  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
627  *    |                            |<-----------------------------|
628  *    |                            |   (251, 2120, 211, 1800)     |
629  *    |                            |    LL_DATA_LENGTH_UPDATE_RSP |
630  *    |                            |----------------------------->|
631  *    |  (251,2120,27,328)         |                              |
632  *    | Data Length Changed        |                              |
633  *    |<---------------------------|                              |
634  *    |                            |                              |
635  */
636 
ZTEST(dle_central,test_data_length_update_central_rem)637 ZTEST(dle_central, test_data_length_update_central_rem)
638 {
639 	struct node_tx *tx;
640 
641 	struct pdu_data_llctrl_length_req remote_length_req = { 27, 328, 251, 2120 };
642 	struct pdu_data_llctrl_length_rsp local_length_rsp = { 251, 2120, 211, 1800 };
643 	struct pdu_data_llctrl_length_rsp length_ntf = { 251, 2120, 27, 328 };
644 
645 	struct node_rx_pdu *ntf;
646 
647 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
648 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
649 	/* Init DLE data */
650 	ull_conn_default_tx_octets_set(211);
651 	ull_conn_default_tx_time_set(1800);
652 	ull_dle_init(&conn, PHY_1M);
653 
654 	event_prepare(&conn);
655 
656 	/* Tx Queue should have one LL Control PDU */
657 	lt_tx(LL_LENGTH_REQ, &conn, &remote_length_req);
658 
659 	event_done(&conn);
660 
661 	event_prepare(&conn);
662 
663 	/* Tx Queue should have one LL Control PDU */
664 	lt_rx(LL_LENGTH_RSP, &conn, &tx, &local_length_rsp);
665 	lt_rx_q_is_empty(&conn);
666 
667 	/* TX Ack */
668 	event_tx_ack(&conn, tx);
669 
670 	event_done(&conn);
671 
672 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
673 	ut_rx_q_is_empty();
674 }
675 
676 /*
677  * Remotely triggered Data Length Update procedure
678  *
679  * +-----+                     +-------+                       +-----+
680  * | UT  |                     | LL_A  |                       | LT  |
681  * +-----+                     +-------+                       +-----+
682  *    |                            |                              |
683  *    |                            | (27, 328, 201, 1720)         |
684  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
685  *    |                            |<-----------------------------|
686  *    |                            |                              |
687  *    |                            |     (251, 2120, 211, 1800)   |
688  *    |                            |    LL_DATA_LENGTH_UPDATE_RSP |
689  *    |                            |----------------------------->|
690  *    |  (201,1720,27,328)         |                              |
691  *    | Data Length Changed        |                              |
692  *    |<---------------------------|                              |
693  *    |                            |                              |
694  */
695 
ZTEST(dle_periph,test_data_length_update_periph_rem)696 ZTEST(dle_periph, test_data_length_update_periph_rem)
697 {
698 	struct node_tx *tx;
699 
700 	struct pdu_data_llctrl_length_req remote_length_req = { 27, 328, 201, 1720 };
701 	struct pdu_data_llctrl_length_rsp local_length_rsp = { 251, 2120, 211, 1800 };
702 	struct pdu_data_llctrl_length_rsp length_ntf = { 201, 1720, 27, 328 };
703 	struct node_rx_pdu *ntf;
704 
705 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
706 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
707 	/* Init DLE data */
708 	ull_conn_default_tx_octets_set(211);
709 	ull_conn_default_tx_time_set(1800);
710 	ull_dle_init(&conn, PHY_1M);
711 
712 	event_prepare(&conn);
713 
714 	/* Tx Queue should have one LL Control PDU */
715 	lt_tx(LL_LENGTH_REQ, &conn, &remote_length_req);
716 
717 	event_done(&conn);
718 
719 	event_prepare(&conn);
720 
721 	/* Tx Queue should have one LL Control PDU */
722 	lt_rx(LL_LENGTH_RSP, &conn, &tx, &local_length_rsp);
723 	lt_rx_q_is_empty(&conn);
724 
725 	/* TX Ack */
726 	event_tx_ack(&conn, tx);
727 
728 	event_done(&conn);
729 
730 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
731 	ut_rx_q_is_empty();
732 }
733 
734 /*
735  * Remotely triggered Data Length Update procedure with local request piggy back
736  *
737  * +-----+                     +-------+                       +-----+
738  * | UT  |                     | LL_A  |                       | LT  |
739  * +-----+                     +-------+                       +-----+
740  *    |                            |                              |
741  *    |                            | (27, 328, 211, 1800)         |
742  *    |                            | LL_DATA_LENGTH_UPDATE_REQ    |
743  *    |                            |<-----------------------------|
744  *    | Start                      |                              |
745  *    | Data Length Update Proc.   |                              |
746  *    |--------------------------->|                              |
747  *    |                            |                              |
748  *    |                            |   (251, 2120, 211, 1800)     |
749  *    |                            |  LL_DATA_LENGTH_UPDATE_RSP   |
750  *    |                            |----------------------------->|
751  *    |  (211,1800,27,328)         |                              |
752  *    | Data Length Changed        |                              |
753  *    |<---------------------------|                              |
754  *    |                            |                              |
755  */
756 
ZTEST(dle_periph,test_data_length_update_periph_rem_and_loc)757 ZTEST(dle_periph, test_data_length_update_periph_rem_and_loc)
758 {
759 	uint64_t err;
760 	struct node_tx *tx;
761 	struct proc_ctx *ctx = NULL;
762 
763 	struct pdu_data_llctrl_length_req remote_length_req = { 27, 328, 211, 1800 };
764 	struct pdu_data_llctrl_length_rsp local_length_rsp = { 251, 2120, 211, 1800 };
765 	struct pdu_data_llctrl_length_rsp length_ntf = { 211, 1800, 27, 328 };
766 	struct node_rx_pdu *ntf;
767 
768 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
769 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
770 	/* Init DLE data */
771 	ull_conn_default_tx_octets_set(211);
772 	ull_conn_default_tx_time_set(1800);
773 	ull_dle_init(&conn, PHY_1M);
774 
775 	/* Allocate dummy procedure used to steal all buffers */
776 	ctx = llcp_create_local_procedure(PROC_VERSION_EXCHANGE);
777 
778 	/* Steal all tx buffers */
779 	while (llcp_tx_alloc_peek(&conn, ctx)) {
780 		tx = llcp_tx_alloc(&conn, ctx);
781 		zassert_not_null(tx, NULL);
782 	}
783 
784 	/* Dummy remove, as above loop might queue up ctx */
785 	llcp_tx_alloc_unpeek(ctx);
786 
787 	event_prepare(&conn);
788 
789 	/* Tx Queue should have one LL Control PDU */
790 	lt_tx(LL_LENGTH_REQ, &conn, &remote_length_req);
791 
792 	event_done(&conn);
793 
794 	event_prepare(&conn);
795 
796 	/* Tx Queue should have no LL Control PDU */
797 	lt_rx_q_is_empty(&conn);
798 
799 	/* Initiate a Data Length Update Procedure */
800 	err = ull_cp_data_length_update(&conn, 211, 1800);
801 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
802 
803 	event_done(&conn);
804 
805 	ull_cp_release_tx(&conn, tx);
806 
807 	event_prepare(&conn);
808 
809 	/* Tx Queue should have one LL Control PDU */
810 	lt_rx(LL_LENGTH_RSP, &conn, &tx, &local_length_rsp);
811 	lt_rx_q_is_empty(&conn);
812 
813 	/* TX Ack */
814 	event_tx_ack(&conn, tx);
815 
816 	event_done(&conn);
817 
818 	ut_rx_pdu(LL_LENGTH_RSP, &ntf, &length_ntf);
819 	ut_rx_q_is_empty();
820 }
821 
ZTEST(dle_util,test_data_length_update_dle_max_time_get)822 ZTEST(dle_util, test_data_length_update_dle_max_time_get)
823 {
824 	uint16_t max_time = 0xffff;
825 	uint16_t max_octets = 211;
826 
827 #ifdef CONFIG_BT_CTLR_PHY
828 	max_time = 2120;
829 #endif
830 	conn.llcp.fex.valid = 0;
831 
832 	ull_dle_local_tx_update(&conn, max_octets, max_time);
833 
834 #ifdef CONFIG_BT_CTLR_PHY
835 #ifdef CONFIG_BT_CTLR_PHY_CODED
836 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
837 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
838 #else
839 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
840 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
841 #endif
842 #else
843 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
844 	zassert_equal(conn.lll.dle.local.max_tx_time, 1800, "max_tx_time mismatch.\n");
845 #endif
846 
847 	/* Emulate complete feat exch without CODED */
848 	conn.llcp.fex.valid = 1;
849 	conn.llcp.fex.features_used = 0;
850 	ull_dle_local_tx_update(&conn, max_octets, max_time);
851 
852 #ifdef CONFIG_BT_CTLR_PHY
853 #ifdef CONFIG_BT_CTLR_PHY_CODED
854 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
855 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
856 #else
857 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
858 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
859 #endif
860 #else
861 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
862 	zassert_equal(conn.lll.dle.local.max_tx_time, 1800, "max_tx_time mismatch.\n");
863 #endif
864 
865 	/* Check the case of CODED PHY support */
866 	conn.llcp.fex.features_used = LL_FEAT_BIT_PHY_CODED;
867 	ull_dle_local_tx_update(&conn, max_octets, max_time);
868 
869 #ifdef CONFIG_BT_CTLR_PHY
870 #ifdef CONFIG_BT_CTLR_PHY_CODED
871 	zassert_equal(conn.lll.dle.local.max_rx_time, 17040, "max_rx_time mismatch.\n");
872 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
873 #else
874 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
875 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
876 #endif
877 #else
878 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
879 	zassert_equal(conn.lll.dle.local.max_tx_time, 1800, "max_tx_time mismatch.\n");
880 #endif
881 
882 	/* Finally check that MAX on max_tx_time works */
883 	max_time = 20000;
884 	ull_dle_local_tx_update(&conn, max_octets, max_time);
885 
886 #ifdef CONFIG_BT_CTLR_PHY
887 #ifdef CONFIG_BT_CTLR_PHY_CODED
888 	zassert_equal(conn.lll.dle.local.max_rx_time, 17040, "max_rx_time mismatch.\n");
889 	zassert_equal(conn.lll.dle.local.max_tx_time, 17040, "max_tx_time mismatch.\n");
890 #else
891 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
892 	zassert_equal(conn.lll.dle.local.max_tx_time, 2120, "max_tx_time mismatch.\n");
893 #endif
894 #else
895 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
896 	zassert_equal(conn.lll.dle.local.max_tx_time, 1800, "max_tx_time mismatch.\n");
897 #endif
898 
899 	/* Check that MIN works */
900 	max_time = 20;
901 	max_octets = 2;
902 	ull_dle_local_tx_update(&conn, max_octets, max_time);
903 
904 #ifdef CONFIG_BT_CTLR_PHY
905 #ifdef CONFIG_BT_CTLR_PHY_CODED
906 	zassert_equal(conn.lll.dle.local.max_rx_time, 17040, "max_rx_time mismatch.\n");
907 	zassert_equal(conn.lll.dle.local.max_tx_time, 328, "max_tx_time mismatch.\n");
908 #else
909 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
910 	zassert_equal(conn.lll.dle.local.max_tx_time, 328, "max_tx_time mismatch.\n");
911 #endif
912 #else
913 	zassert_equal(conn.lll.dle.local.max_rx_time, 2120, "max_rx_time mismatch.\n");
914 	zassert_equal(conn.lll.dle.local.max_tx_time, 328, "max_tx_time mismatch.\n");
915 #endif
916 }
917 
918 ZTEST_SUITE(dle_central, NULL, NULL, dle_setup, NULL, NULL);
919 ZTEST_SUITE(dle_periph, NULL, NULL, dle_setup, NULL, NULL);
920 ZTEST_SUITE(dle_util, NULL, NULL, dle_setup, NULL, NULL);
921