1 /*
2 * Copyright (c) 2020 Demant
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/types.h>
8 #include <zephyr/ztest.h>
9
10 #include <zephyr/bluetooth/hci.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/slist.h>
13 #include <zephyr/sys/util.h>
14 #include "hal/ccm.h"
15
16 #include "util/util.h"
17 #include "util/mem.h"
18 #include "util/memq.h"
19 #include "util/dbuf.h"
20
21 #include "pdu_df.h"
22 #include "lll/pdu_vendor.h"
23 #include "pdu.h"
24 #include "ll.h"
25 #include "ll_settings.h"
26 #include "ll_feat.h"
27
28 #include "lll.h"
29 #include "lll/lll_df_types.h"
30 #include "lll_conn.h"
31 #include "lll_conn_iso.h"
32
33 #include "ull_tx_queue.h"
34
35 #include "isoal.h"
36 #include "ull_iso_types.h"
37 #include "ull_conn_iso_types.h"
38 #include "ull_conn_types.h"
39 #include "ull_llcp.h"
40 #include "ull_llcp_internal.h"
41
42 #include "helper_pdu.h"
43 #include "helper_util.h"
44
45 static struct ll_conn conn;
46
47 /*
48 * Note API and internal test are not yet split out here
49 */
50
ZTEST(public,test_api_init)51 ZTEST(public, test_api_init)
52 {
53 ull_cp_init();
54 ull_tx_q_init(&conn.tx_q);
55
56 ull_llcp_init(&conn);
57
58 zassert_true(llcp_lr_is_disconnected(&conn));
59 zassert_true(llcp_rr_is_disconnected(&conn));
60 }
61
ZTEST(public,test_api_connect)62 ZTEST(public, test_api_connect)
63 {
64 ull_cp_init();
65 ull_tx_q_init(&conn.tx_q);
66 ull_llcp_init(&conn);
67
68 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
69 zassert_true(llcp_lr_is_idle(&conn));
70 zassert_true(llcp_rr_is_idle(&conn));
71 }
72
ZTEST(public,test_api_disconnect)73 ZTEST(public, test_api_disconnect)
74 {
75 ull_cp_init();
76 ull_tx_q_init(&conn.tx_q);
77 ull_llcp_init(&conn);
78
79 ull_cp_state_set(&conn, ULL_CP_DISCONNECTED);
80 zassert_true(llcp_lr_is_disconnected(&conn));
81 zassert_true(llcp_rr_is_disconnected(&conn));
82
83 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
84 zassert_true(llcp_lr_is_idle(&conn));
85 zassert_true(llcp_rr_is_idle(&conn));
86
87 ull_cp_state_set(&conn, ULL_CP_DISCONNECTED);
88 zassert_true(llcp_lr_is_disconnected(&conn));
89 zassert_true(llcp_rr_is_disconnected(&conn));
90 }
91
ZTEST(public,test_int_disconnect_loc)92 ZTEST(public, test_int_disconnect_loc)
93 {
94 uint64_t err;
95 int nr_free_ctx;
96 struct node_tx *tx;
97 struct ll_conn conn;
98
99 struct pdu_data_llctrl_version_ind local_version_ind = {
100 .version_number = LL_VERSION_NUMBER,
101 .company_id = CONFIG_BT_CTLR_COMPANY_ID,
102 .sub_version_number = CONFIG_BT_CTLR_SUBVERSION_NUMBER,
103 };
104
105 test_setup(&conn);
106 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
107 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
108
109 nr_free_ctx = llcp_ctx_buffers_free();
110 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt());
111
112 err = ull_cp_version_exchange(&conn);
113 zassert_equal(err, BT_HCI_ERR_SUCCESS);
114
115 nr_free_ctx = llcp_ctx_buffers_free();
116 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt() - 1);
117
118 event_prepare(&conn);
119 lt_rx(LL_VERSION_IND, &conn, &tx, &local_version_ind);
120 lt_rx_q_is_empty(&conn);
121 event_done(&conn);
122
123 /*
124 * Now we disconnect before getting a response
125 */
126 ull_cp_state_set(&conn, ULL_CP_DISCONNECTED);
127
128 nr_free_ctx = llcp_ctx_buffers_free();
129 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt());
130
131 ut_rx_q_is_empty();
132
133 /*
134 * nothing should happen when running a new event
135 */
136 event_prepare(&conn);
137 event_done(&conn);
138
139 nr_free_ctx = llcp_ctx_buffers_free();
140 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt());
141
142 /*
143 * all buffers should still be empty
144 */
145 lt_rx_q_is_empty(&conn);
146 ut_rx_q_is_empty();
147 }
148
ZTEST(public,test_int_disconnect_rem)149 ZTEST(public, test_int_disconnect_rem)
150 {
151 int nr_free_ctx;
152 struct pdu_data_llctrl_version_ind remote_version_ind = {
153 .version_number = 0x55,
154 .company_id = 0xABCD,
155 .sub_version_number = 0x1234,
156 };
157 struct ll_conn conn;
158
159 test_setup(&conn);
160
161 /* Role */
162 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
163
164 /* Connect */
165 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
166
167 nr_free_ctx = llcp_ctx_buffers_free();
168 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt());
169 /* Prepare */
170 event_prepare(&conn);
171
172 /* Rx */
173 lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
174
175 nr_free_ctx = llcp_ctx_buffers_free();
176 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt());
177
178 /* Disconnect before we reply */
179
180 /* Done */
181 event_done(&conn);
182
183 ull_cp_state_set(&conn, ULL_CP_DISCONNECTED);
184
185 /* Prepare */
186 event_prepare(&conn);
187
188 /* Done */
189 event_done(&conn);
190
191 nr_free_ctx = llcp_ctx_buffers_free();
192 zassert_equal(nr_free_ctx, test_ctx_buffers_cnt());
193
194 /* There should not be a host notifications */
195 ut_rx_q_is_empty();
196 }
197
198 #define SIZE 2
199
ZTEST(public,test_int_pause_resume_data_path)200 ZTEST(public, test_int_pause_resume_data_path)
201 {
202 struct node_tx *node;
203 struct node_tx nodes[SIZE] = { 0 };
204
205 ull_cp_init();
206 ull_tx_q_init(&conn.tx_q);
207
208 /*** #1: Not paused when initialized ***/
209
210 /* Enqueue data nodes */
211 for (int i = 0U; i < SIZE; i++) {
212 ull_tx_q_enqueue_data(&conn.tx_q, &nodes[i]);
213 }
214
215 /* Dequeue data nodes */
216 for (int i = 0U; i < SIZE; i++) {
217 node = ull_tx_q_dequeue(&conn.tx_q);
218 zassert_equal_ptr(node, &nodes[i], NULL);
219 }
220
221 /* Tx Queue shall be empty */
222 node = ull_tx_q_dequeue(&conn.tx_q);
223 zassert_equal_ptr(node, NULL, "");
224
225
226 /*** #2: Single pause/resume ***/
227
228 /* Pause data path */
229 llcp_tx_pause_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE);
230
231 /* Tx Queue shall be empty */
232 node = ull_tx_q_dequeue(&conn.tx_q);
233 zassert_equal_ptr(node, NULL, "");
234
235 /* Enqueue data nodes */
236 for (int i = 0U; i < SIZE; i++) {
237 ull_tx_q_enqueue_data(&conn.tx_q, &nodes[i]);
238 }
239
240 /* Tx Queue shall be empty */
241 node = ull_tx_q_dequeue(&conn.tx_q);
242 zassert_equal_ptr(node, NULL, "");
243
244 /* Resume data path */
245 llcp_tx_resume_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE);
246
247 /* Dequeue data nodes */
248 for (int i = 0U; i < SIZE; i++) {
249 node = ull_tx_q_dequeue(&conn.tx_q);
250 zassert_equal_ptr(node, &nodes[i], NULL);
251 }
252
253 /* Tx Queue shall be empty */
254 node = ull_tx_q_dequeue(&conn.tx_q);
255 zassert_equal_ptr(node, NULL, "");
256
257
258 /*** #3: Multiple pause/resume ***/
259
260 /* Pause data path */
261 llcp_tx_pause_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE);
262 llcp_tx_pause_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_DATA_LENGTH);
263
264 /* Tx Queue shall be empty */
265 node = ull_tx_q_dequeue(&conn.tx_q);
266 zassert_equal_ptr(node, NULL, "");
267
268 /* Enqueue data nodes */
269 for (int i = 0U; i < SIZE; i++) {
270 ull_tx_q_enqueue_data(&conn.tx_q, &nodes[i]);
271 }
272
273 /* Tx Queue shall be empty */
274 node = ull_tx_q_dequeue(&conn.tx_q);
275 zassert_equal_ptr(node, NULL, "");
276
277 /* Resume data path */
278 llcp_tx_resume_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_DATA_LENGTH);
279
280 /* Tx Queue shall be empty */
281 node = ull_tx_q_dequeue(&conn.tx_q);
282 zassert_equal_ptr(node, NULL, "");
283
284 /* Resume data path */
285 llcp_tx_resume_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE);
286
287 /* Dequeue data nodes */
288 for (int i = 0U; i < SIZE; i++) {
289 node = ull_tx_q_dequeue(&conn.tx_q);
290 zassert_equal_ptr(node, &nodes[i], NULL);
291 }
292
293 /* Tx Queue shall be empty */
294 node = ull_tx_q_dequeue(&conn.tx_q);
295 zassert_equal_ptr(node, NULL, "");
296
297
298 /*** #4: Asymetric pause/resume ***/
299
300 /* Pause data path */
301 llcp_tx_pause_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE);
302
303 /* Tx Queue shall be empty */
304 node = ull_tx_q_dequeue(&conn.tx_q);
305 zassert_equal_ptr(node, NULL, "");
306
307 /* Enqueue data nodes */
308 for (int i = 0U; i < SIZE; i++) {
309 ull_tx_q_enqueue_data(&conn.tx_q, &nodes[i]);
310 }
311
312 /* Tx Queue shall be empty */
313 node = ull_tx_q_dequeue(&conn.tx_q);
314 zassert_equal_ptr(node, NULL, "");
315
316 /* Resume data path (wrong mask) */
317 llcp_tx_resume_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_DATA_LENGTH);
318
319 /* Tx Queue shall be empty */
320 node = ull_tx_q_dequeue(&conn.tx_q);
321 zassert_equal_ptr(node, NULL, "");
322
323 /* Resume data path */
324 llcp_tx_resume_data(&conn, LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE);
325
326 /* Dequeue data nodes */
327 for (int i = 0U; i < SIZE; i++) {
328 node = ull_tx_q_dequeue(&conn.tx_q);
329 zassert_equal_ptr(node, &nodes[i], NULL);
330 }
331
332 /* Tx Queue shall be empty */
333 node = ull_tx_q_dequeue(&conn.tx_q);
334 zassert_equal_ptr(node, NULL, "");
335 }
336
ZTEST(public,test_check_peek_proc)337 ZTEST(public, test_check_peek_proc)
338 {
339 struct proc_ctx *ctx1, *ctx2;
340
341 ctx1 = llcp_create_local_procedure(PROC_CHAN_MAP_UPDATE);
342 llcp_lr_enqueue(&conn, ctx1);
343
344 zassert_is_null(llcp_lr_peek_proc(&conn, PROC_CIS_CREATE), "CTX is not null");
345 zassert_equal(llcp_lr_peek_proc(&conn, PROC_CHAN_MAP_UPDATE), ctx1, "CTX is not correct");
346
347 ctx2 = llcp_create_local_procedure(PROC_CIS_CREATE);
348 llcp_lr_enqueue(&conn, ctx2);
349
350 zassert_equal(llcp_lr_peek_proc(&conn, PROC_CHAN_MAP_UPDATE), ctx1, "CTX is not correct");
351 zassert_equal(llcp_lr_peek_proc(&conn, PROC_CIS_CREATE), ctx2, "CTX is not correct");
352 zassert_is_null(llcp_lr_peek_proc(&conn, PROC_CIS_TERMINATE), "CTX is not null");
353 }
354
ZTEST(internal,test_int_mem_proc_ctx)355 ZTEST(internal, test_int_mem_proc_ctx)
356 {
357 struct proc_ctx *ctx1;
358 struct proc_ctx *ctx2;
359 int nr_of_free_ctx;
360
361 ull_cp_init();
362
363 nr_of_free_ctx = llcp_ctx_buffers_free();
364 zassert_equal(nr_of_free_ctx, CONFIG_BT_CTLR_LLCP_LOCAL_PROC_CTX_BUF_NUM +
365 CONFIG_BT_CTLR_LLCP_REMOTE_PROC_CTX_BUF_NUM, NULL);
366
367 for (int i = 0U; i < CONFIG_BT_CTLR_LLCP_LOCAL_PROC_CTX_BUF_NUM; i++) {
368 ctx1 = llcp_proc_ctx_acquire();
369
370 /* The previous acquire should be valid */
371 zassert_not_null(ctx1, NULL);
372 }
373
374 nr_of_free_ctx = llcp_local_ctx_buffers_free();
375 zassert_equal(nr_of_free_ctx, 0);
376
377 ctx2 = llcp_proc_ctx_acquire();
378
379 /* The last acquire should fail */
380 zassert_is_null(ctx2, NULL);
381
382 llcp_proc_ctx_release(ctx1);
383 nr_of_free_ctx = llcp_local_ctx_buffers_free();
384 zassert_equal(nr_of_free_ctx, 1);
385
386 ctx1 = llcp_proc_ctx_acquire();
387
388 /* Releasing returns the context to the avilable pool */
389 zassert_not_null(ctx1, NULL);
390 }
391
ZTEST(internal,test_int_mem_tx)392 ZTEST(internal, test_int_mem_tx)
393 {
394 bool peek;
395 #if defined(LLCP_TX_CTRL_BUF_QUEUE_ENABLE)
396 #define TX_BUFFER_POOL_SIZE (CONFIG_BT_CTLR_LLCP_COMMON_TX_CTRL_BUF_NUM + \
397 CONFIG_BT_CTLR_LLCP_PER_CONN_TX_CTRL_BUF_NUM)
398 #else /* LLCP_TX_CTRL_BUF_QUEUE_ENABLE */
399 #define TX_BUFFER_POOL_SIZE (CONFIG_BT_CTLR_LLCP_COMMON_TX_CTRL_BUF_NUM + \
400 CONFIG_BT_CTLR_LLCP_CONN * \
401 CONFIG_BT_CTLR_LLCP_PER_CONN_TX_CTRL_BUF_NUM)
402 #endif /* LLCP_TX_CTRL_BUF_QUEUE_ENABLE */
403 struct ll_conn conn;
404 struct node_tx *txl[TX_BUFFER_POOL_SIZE];
405 struct proc_ctx *ctx;
406
407 ull_cp_init();
408 ull_llcp_init(&conn);
409
410 ctx = llcp_create_local_procedure(PROC_CONN_UPDATE);
411
412 for (int i = 0U; i < TX_BUFFER_POOL_SIZE; i++) {
413 peek = llcp_tx_alloc_peek(&conn, ctx);
414
415 /* The previous tx alloc peek should be valid */
416 zassert_true(peek, NULL);
417
418 txl[i] = llcp_tx_alloc(&conn, ctx);
419
420 /* The previous alloc should be valid */
421 zassert_not_null(txl[i], NULL);
422 }
423
424 peek = llcp_tx_alloc_peek(&conn, ctx);
425
426 /* The last tx alloc peek should fail */
427 zassert_false(peek, NULL);
428
429 /* Release all */
430 for (int i = 0U; i < TX_BUFFER_POOL_SIZE; i++) {
431 ull_cp_release_tx(&conn, txl[i]);
432 }
433
434 for (int i = 0U; i < TX_BUFFER_POOL_SIZE; i++) {
435 peek = llcp_tx_alloc_peek(&conn, ctx);
436
437 /* The previous tx alloc peek should be valid */
438 zassert_true(peek, NULL);
439
440 txl[i] = llcp_tx_alloc(&conn, ctx);
441
442 /* The previous alloc should be valid */
443 zassert_not_null(txl[i], NULL);
444 }
445
446 peek = llcp_tx_alloc_peek(&conn, ctx);
447
448 /* The last tx alloc peek should fail */
449 zassert_false(peek, NULL);
450
451 /* Release all */
452 for (int i = 0U; i < TX_BUFFER_POOL_SIZE; i++) {
453 ull_cp_release_tx(&conn, txl[i]);
454 }
455 }
456
ZTEST(internal,test_int_create_proc)457 ZTEST(internal, test_int_create_proc)
458 {
459 struct proc_ctx *ctx;
460
461 ull_cp_init();
462
463 ctx = llcp_create_procedure(PROC_VERSION_EXCHANGE);
464 zassert_not_null(ctx, NULL);
465
466 zassert_equal(ctx->proc, PROC_VERSION_EXCHANGE);
467
468 for (int i = 0U; i < CONFIG_BT_CTLR_LLCP_LOCAL_PROC_CTX_BUF_NUM; i++) {
469 zassert_not_null(ctx, NULL);
470 ctx = llcp_create_procedure(PROC_VERSION_EXCHANGE);
471 }
472
473 zassert_is_null(ctx, NULL);
474 }
475
ZTEST(internal,test_int_llcp_init)476 ZTEST(internal, test_int_llcp_init)
477 {
478 struct ll_conn conn;
479
480 ull_cp_init();
481
482 ull_llcp_init(&conn);
483
484 memset(&conn.llcp, 0xAA, sizeof(conn.llcp));
485
486 ull_llcp_init(&conn);
487
488 zassert_equal(conn.llcp.local.pause, 0);
489 zassert_equal(conn.llcp.remote.pause, 0);
490 }
491
ZTEST(internal,test_int_local_pending_requests)492 ZTEST(internal, test_int_local_pending_requests)
493 {
494 struct ll_conn conn;
495 struct proc_ctx *peek_ctx;
496 struct proc_ctx *dequeue_ctx;
497 struct proc_ctx ctx;
498
499 ull_cp_init();
500 ull_tx_q_init(&conn.tx_q);
501 ull_llcp_init(&conn);
502
503 peek_ctx = llcp_lr_peek(&conn);
504 zassert_is_null(peek_ctx, NULL);
505
506 dequeue_ctx = llcp_lr_dequeue(&conn);
507 zassert_is_null(dequeue_ctx, NULL);
508
509 llcp_lr_enqueue(&conn, &ctx);
510 peek_ctx = (struct proc_ctx *)sys_slist_peek_head(&conn.llcp.local.pend_proc_list);
511 zassert_equal_ptr(peek_ctx, &ctx, NULL);
512
513 peek_ctx = llcp_lr_peek(&conn);
514 zassert_equal_ptr(peek_ctx, &ctx, NULL);
515
516 dequeue_ctx = llcp_lr_dequeue(&conn);
517 zassert_equal_ptr(dequeue_ctx, &ctx, NULL);
518
519 peek_ctx = llcp_lr_peek(&conn);
520 zassert_is_null(peek_ctx, NULL);
521
522 dequeue_ctx = llcp_lr_dequeue(&conn);
523 zassert_is_null(dequeue_ctx, NULL);
524 }
525
ZTEST(internal,test_int_remote_pending_requests)526 ZTEST(internal, test_int_remote_pending_requests)
527 {
528 struct ll_conn conn;
529 struct proc_ctx *peek_ctx;
530 struct proc_ctx *dequeue_ctx;
531 struct proc_ctx ctx;
532
533 ull_cp_init();
534 ull_tx_q_init(&conn.tx_q);
535 ull_llcp_init(&conn);
536
537 peek_ctx = llcp_rr_peek(&conn);
538 zassert_is_null(peek_ctx, NULL);
539
540 dequeue_ctx = llcp_rr_dequeue(&conn);
541 zassert_is_null(dequeue_ctx, NULL);
542
543 llcp_rr_enqueue(&conn, &ctx);
544 peek_ctx = (struct proc_ctx *)sys_slist_peek_head(&conn.llcp.remote.pend_proc_list);
545 zassert_equal_ptr(peek_ctx, &ctx, NULL);
546
547 peek_ctx = llcp_rr_peek(&conn);
548 zassert_equal_ptr(peek_ctx, &ctx, NULL);
549
550 dequeue_ctx = llcp_rr_dequeue(&conn);
551 zassert_equal_ptr(dequeue_ctx, &ctx, NULL);
552
553 peek_ctx = llcp_rr_peek(&conn);
554 zassert_is_null(peek_ctx, NULL);
555
556 dequeue_ctx = llcp_rr_dequeue(&conn);
557 zassert_is_null(dequeue_ctx, NULL);
558 }
559
560 ZTEST_SUITE(public, NULL, NULL, NULL, NULL, NULL);
561 ZTEST_SUITE(internal, NULL, NULL, NULL, NULL, NULL);
562