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