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 
le_ping_setup(void * data)47 static void le_ping_setup(void *data)
48 {
49 	test_setup(&conn);
50 }
51 
52 /* +-----+                     +-------+            +-----+
53  * | UT  |                     | LL_A  |            | LT  |
54  * +-----+                     +-------+            +-----+
55  *    |                            |                   |
56  *    | Start                      |                   |
57  *    | LE Ping Proc.              |                   |
58  *    |--------------------------->|                   |
59  *    |                            |                   |
60  *    |                            | LL_LE_PING_REQ    |
61  *    |                            |------------------>|
62  *    |                            |                   |
63  *    |                            |    LL_LE_PING_RSP |
64  *    |                            |<------------------|
65  *    |                            |                   |
66  *    | Start                      |                   |
67  *    | LE Ping Proc.              |                   |
68  *    |--------------------------->|                   |
69  *    |                            |                   |
70  *    |                            | LL_LE_PING_REQ    |
71  *    |                            |------------------>|
72  *    |                            |                   |
73  *    |                            |    LL_UNKNOWN_RSP |
74  *    |                            |<------------------|
75  *    |                            |                   |
76  */
ZTEST(ping_central,test_ping_central_loc)77 ZTEST(ping_central, test_ping_central_loc)
78 {
79 	uint8_t err;
80 	struct node_tx *tx;
81 
82 	struct pdu_data_llctrl_ping_req local_ping_req = {};
83 
84 	struct pdu_data_llctrl_ping_rsp remote_ping_rsp = {};
85 
86 	struct pdu_data_llctrl_unknown_rsp unknown_rsp = {
87 		.type = PDU_DATA_LLCTRL_TYPE_PING_REQ
88 	};
89 
90 	/* Role */
91 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
92 
93 	/* Connect */
94 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
95 
96 	/* Initiate an LE Ping Procedure */
97 	err = ull_cp_le_ping(&conn);
98 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
99 
100 	/* Prepare */
101 	event_prepare(&conn);
102 
103 	/* Tx Queue should have one LL Control PDU */
104 	lt_rx(LL_LE_PING_REQ, &conn, &tx, &local_ping_req);
105 	lt_rx_q_is_empty(&conn);
106 
107 	/* Rx */
108 	lt_tx(LL_LE_PING_RSP, &conn, &remote_ping_rsp);
109 
110 	/* Done */
111 	event_done(&conn);
112 
113 	/* Release tx node */
114 	ull_cp_release_tx(&conn, tx);
115 
116 	/* There should not be a host notifications */
117 	ut_rx_q_is_empty();
118 
119 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
120 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
121 
122 	/* Initiate another LE Ping Procedure */
123 	err = ull_cp_le_ping(&conn);
124 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
125 
126 	/* Prepare */
127 	event_prepare(&conn);
128 
129 	/* Tx Queue should have one LL Control PDU */
130 	lt_rx(LL_LE_PING_REQ, &conn, &tx, &local_ping_req);
131 	lt_rx_q_is_empty(&conn);
132 
133 	/* Rx */
134 	lt_tx(LL_UNKNOWN_RSP, &conn, &unknown_rsp);
135 
136 	/* Done */
137 	event_done(&conn);
138 
139 	/* Release tx node */
140 	ull_cp_release_tx(&conn, tx);
141 
142 	/* There should not be a host notifications */
143 	ut_rx_q_is_empty();
144 
145 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
146 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
147 
148 }
149 
150 /* +-----+                     +-------+            +-----+
151  * | UT  |                     | LL_A  |            | LT  |
152  * +-----+                     +-------+            +-----+
153  *    |                            |                   |
154  *    | Start                      |                   |
155  *    | LE Ping Proc.              |                   |
156  *    |--------------------------->|                   |
157  *    |                            |                   |
158  *    |                            | LL_LE_PING_REQ    |
159  *    |                            |------------------>|
160  *    |                            |                   |
161  *    |                            | LL_<INVALID>_RSP  |
162  *    |                            |<------------------|
163  *    |                            |                   |
164  *  ~~~~~~~~~~~~~~~~~ TERMINATE CONNECTION ~~~~~~~~~~~~~~
165  *    |                            |                   |
166  */
ZTEST(ping_central,test_ping_central_loc_invalid_rsp)167 ZTEST(ping_central, test_ping_central_loc_invalid_rsp)
168 {
169 	uint8_t err;
170 	struct node_tx *tx;
171 
172 	struct pdu_data_llctrl_reject_ind reject_ind = {
173 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
174 	};
175 	struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
176 		.reject_opcode = PDU_DATA_LLCTRL_TYPE_PING_REQ,
177 		.error_code = BT_HCI_ERR_LL_PROC_COLLISION
178 	};
179 	struct pdu_data_llctrl_ping_req local_ping_req = {};
180 
181 	/* Role */
182 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
183 
184 	/* Connect */
185 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
186 
187 	/* Initiate an LE Ping Procedure */
188 	err = ull_cp_le_ping(&conn);
189 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
190 
191 	/* Prepare */
192 	event_prepare(&conn);
193 
194 	/* Tx Queue should have one LL Control PDU */
195 	lt_rx(LL_LE_PING_REQ, &conn, &tx, &local_ping_req);
196 	lt_rx_q_is_empty(&conn);
197 
198 	/* Rx */
199 	lt_tx(LL_REJECT_EXT_IND, &conn, &reject_ext_ind);
200 
201 	/* Done */
202 	event_done(&conn);
203 
204 	/* Release tx node */
205 	ull_cp_release_tx(&conn, tx);
206 
207 	/* Termination 'triggered' */
208 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
209 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
210 
211 	/* Clear termination flag for subsequent test cycle */
212 	conn.llcp_terminate.reason_final = 0;
213 
214 	/* There should not be a host notifications */
215 	ut_rx_q_is_empty();
216 
217 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
218 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
219 
220 	/* Initiate another LE Ping Procedure */
221 	err = ull_cp_le_ping(&conn);
222 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
223 
224 	/* Prepare */
225 	event_prepare(&conn);
226 
227 	/* Tx Queue should have one LL Control PDU */
228 	lt_rx(LL_LE_PING_REQ, &conn, &tx, &local_ping_req);
229 	lt_rx_q_is_empty(&conn);
230 
231 	/* Rx */
232 	lt_tx(LL_REJECT_IND, &conn, &reject_ind);
233 
234 	/* Done */
235 	event_done(&conn);
236 
237 	/* Release tx node */
238 	ull_cp_release_tx(&conn, tx);
239 
240 	/* Termination 'triggered' */
241 	zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_LMP_PDU_NOT_ALLOWED,
242 		      "Terminate reason %d", conn.llcp_terminate.reason_final);
243 
244 	/* There should not be a host notifications */
245 	ut_rx_q_is_empty();
246 
247 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
248 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
249 
250 }
251 
252 /* +-----+                     +-------+            +-----+
253  * | UT  |                     | LL_A  |            | LT  |
254  * +-----+                     +-------+            +-----+
255  *    |                            |                   |
256  *    | Start                      |                   |
257  *    | LE Ping Proc.              |                   |
258  *    |--------------------------->|                   |
259  *    |                            |                   |
260  *    |                            | LL_LE_PING_REQ    |
261  *    |                            |------------------>|
262  *    |                            |                   |
263  *    |                            |    LL_LE_PING_RSP |
264  *    |                            |<------------------|
265  *    |                            |                   |
266  *    |                            |                   |
267  */
ZTEST(ping_periph,test_ping_periph_loc)268 ZTEST(ping_periph, test_ping_periph_loc)
269 {
270 	uint8_t err;
271 	struct node_tx *tx;
272 
273 	struct pdu_data_llctrl_ping_req local_ping_req = {};
274 
275 	struct pdu_data_llctrl_ping_rsp remote_ping_rsp = {};
276 
277 	/* Role */
278 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
279 
280 	/* Connect */
281 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
282 
283 	/* Initiate an LE Ping Procedure */
284 	err = ull_cp_le_ping(&conn);
285 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
286 
287 	/* Prepare */
288 	event_prepare(&conn);
289 
290 	/* Tx Queue should have one LL Control PDU */
291 	lt_rx(LL_LE_PING_REQ, &conn, &tx, &local_ping_req);
292 	lt_rx_q_is_empty(&conn);
293 
294 	/* Rx */
295 	lt_tx(LL_LE_PING_RSP, &conn, &remote_ping_rsp);
296 
297 	/* Done */
298 	event_done(&conn);
299 
300 	/* Release tx node */
301 	ull_cp_release_tx(&conn, tx);
302 
303 	/* There should not be a host notifications */
304 	ut_rx_q_is_empty();
305 
306 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
307 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
308 }
309 
310 /* +-----+ +-------+            +-----+
311  * | UT  | | LL_A  |            | LT  |
312  * +-----+ +-------+            +-----+
313  *    |        |                   |
314  *    |        |    LL_LE_PING_REQ |
315  *    |        |<------------------|
316  *    |        |                   |
317  *    |        | LL_LE_PING_RSP    |
318  *    |        |------------------>|
319  *    |        |                   |
320  */
ZTEST(ping_central,test_ping_central_rem)321 ZTEST(ping_central, test_ping_central_rem)
322 {
323 	struct node_tx *tx;
324 
325 	struct pdu_data_llctrl_ping_req local_ping_req = {};
326 
327 	struct pdu_data_llctrl_ping_rsp remote_ping_rsp = {};
328 
329 	/* Role */
330 	test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
331 
332 	/* Connect */
333 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
334 
335 	/* Prepare */
336 	event_prepare(&conn);
337 
338 	/* Tx */
339 	lt_tx(LL_LE_PING_REQ, &conn, &local_ping_req);
340 
341 	/* Done */
342 	event_done(&conn);
343 
344 	/* Prepare */
345 	event_prepare(&conn);
346 
347 	/* Tx Queue should have one LL Control PDU */
348 	lt_rx(LL_LE_PING_RSP, &conn, &tx, &remote_ping_rsp);
349 	lt_rx_q_is_empty(&conn);
350 
351 	/* Done */
352 	event_done(&conn);
353 
354 	/* Release tx node */
355 	ull_cp_release_tx(&conn, tx);
356 
357 	/* There should not be a host notifications */
358 	ut_rx_q_is_empty();
359 
360 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
361 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
362 }
363 
364 /* +-----+ +-------+            +-----+
365  * | UT  | | LL_A  |            | LT  |
366  * +-----+ +-------+            +-----+
367  *    |        |                   |
368  *    |        |    LL_LE_PING_REQ |
369  *    |        |<------------------|
370  *    |        |                   |
371  *    |        | LL_LE_PING_RSP    |
372  *    |        |------------------>|
373  *    |        |                   |
374  */
ZTEST(ping_periph,test_ping_periph_rem)375 ZTEST(ping_periph, test_ping_periph_rem)
376 {
377 	struct node_tx *tx;
378 
379 	struct pdu_data_llctrl_ping_req local_ping_req = {};
380 
381 	struct pdu_data_llctrl_ping_rsp remote_ping_rsp = {};
382 
383 	/* Role */
384 	test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
385 
386 	/* Connect */
387 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
388 
389 	/* Prepare */
390 	event_prepare(&conn);
391 
392 	/* Tx */
393 	lt_tx(LL_LE_PING_REQ, &conn, &local_ping_req);
394 
395 	/* Done */
396 	event_done(&conn);
397 
398 	/* Prepare */
399 	event_prepare(&conn);
400 
401 	/* Tx Queue should have one LL Control PDU */
402 	lt_rx(LL_LE_PING_RSP, &conn, &tx, &remote_ping_rsp);
403 	lt_rx_q_is_empty(&conn);
404 
405 	/* Done */
406 	event_done(&conn);
407 
408 	/* Release tx node */
409 	ull_cp_release_tx(&conn, tx);
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 ZTEST_SUITE(ping_central, NULL, NULL, le_ping_setup, NULL, NULL);
419 ZTEST_SUITE(ping_periph, NULL, NULL, le_ping_setup, NULL, NULL);
420