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 
27 #include "lll.h"
28 #include "lll/lll_df_types.h"
29 #include "lll_conn.h"
30 #include "lll_conn_iso.h"
31 
32 #include "ull_tx_queue.h"
33 
34 #include "isoal.h"
35 #include "ull_iso_types.h"
36 #include "ull_conn_iso_types.h"
37 #include "ull_conn_types.h"
38 #include "ull_llcp.h"
39 #include "ull_conn_internal.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 
muc_setup(void * data)47 static void muc_setup(void *data)
48 {
49 	test_setup(&conn);
50 }
51 
52 /* +-----+                     +-------+                  +-----+
53  * | UT  |                     | LL_A  |                  | LT  |
54  * +-----+                     +-------+                  +-----+
55  *    |                            |                         |
56  *    | Start                      |                         |
57  *    | Min used chans Proc.       |                         |
58  *    |--------------------------->|                         |
59  *    |                            |                         |
60  *    |                            | LL_MIN_USED_CHANS_IND   |
61  *    |                            |------------------------>|
62  *    |                            |                 'll_ack'|
63  *    |                            |                         |
64  *    |                            |                         |
65  */
ZTEST(muc_periph,test_min_used_chans_periph_loc)66 ZTEST(muc_periph, test_min_used_chans_periph_loc)
67 {
68 	uint8_t err;
69 	struct node_tx *tx;
70 
71 	struct pdu_data_llctrl_min_used_chans_ind local_muc_ind = { .phys = 1,
72 		.min_used_chans = 2 };
73 
74 	struct pdu_data_llctrl_min_used_chans_ind remote_muc_ind = { .phys = 1,
75 		.min_used_chans = 2 };
76 
77 	/* Role */
78 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
79 
80 	/* Connect */
81 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
82 
83 	/* Initiate a Min number of Used Channels Procedure */
84 	err = ull_cp_min_used_chans(&conn, 1, 2);
85 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
86 
87 	/* Prepare */
88 	event_prepare(&conn);
89 
90 	/* Tx Queue should have one LL Control PDU */
91 	lt_rx(LL_MIN_USED_CHANS_IND, &conn,  &tx, &local_muc_ind);
92 	lt_rx_q_is_empty(&conn);
93 
94 	/* Rx */
95 	lt_tx(LL_MIN_USED_CHANS_IND, &conn,  &remote_muc_ind);
96 
97 	/* TX Ack */
98 	event_tx_ack(&conn, tx);
99 
100 	/* Done */
101 	event_done(&conn);
102 
103 	/* Release tx node */
104 	ull_cp_release_tx(&conn, tx);
105 
106 	/* There should not be a host notifications */
107 	ut_rx_q_is_empty();
108 
109 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
110 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
111 }
112 
ZTEST(muc_central,test_min_used_chans_central_loc)113 ZTEST(muc_central, test_min_used_chans_central_loc)
114 {
115 	uint8_t err;
116 
117 	/* Role */
118 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
119 
120 	/* Connect */
121 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
122 
123 	/* Initiate a Min number of Used Channels Procedure */
124 	err = ull_cp_min_used_chans(&conn, 1, 2);
125 	zassert_equal(err, BT_HCI_ERR_CMD_DISALLOWED);
126 
127 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
128 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
129 }
130 
ZTEST(muc_central,test_min_used_chans_central_rem)131 ZTEST(muc_central, test_min_used_chans_central_rem)
132 {
133 	struct pdu_data_llctrl_min_used_chans_ind remote_muc_ind = { .phys = 1,
134 		.min_used_chans = 2 };
135 
136 	/* Role */
137 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
138 
139 	/* Connect */
140 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
141 
142 	/* Prepare */
143 	event_prepare(&conn);
144 
145 	/* Rx */
146 	lt_tx(LL_MIN_USED_CHANS_IND, &conn,  &remote_muc_ind);
147 
148 	/* Done */
149 	event_done(&conn);
150 
151 	/* Prepare */
152 	event_prepare(&conn);
153 
154 	/* Tx Queue should have no LL Control PDU */
155 	lt_rx_q_is_empty(&conn);
156 
157 	/* Done */
158 	event_done(&conn);
159 
160 	/* There should not be a host notifications */
161 	ut_rx_q_is_empty();
162 
163 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
164 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
165 }
166 
167 ZTEST_SUITE(muc_central, NULL, NULL, muc_setup, NULL, NULL);
168 ZTEST_SUITE(muc_periph, NULL, NULL, muc_setup, NULL, NULL);
169