1 /*
2  * Copyright (c) 2020 Demant
3  * Copyright (c) 2020 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/types.h>
9 #include <zephyr/ztest.h>
10 
11 #include <zephyr/bluetooth/hci.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/slist.h>
14 #include <zephyr/sys/util.h>
15 #include "hal/ccm.h"
16 
17 #include "util/util.h"
18 #include "util/mem.h"
19 #include "util/memq.h"
20 #include "util/dbuf.h"
21 
22 #include "pdu_df.h"
23 #include "lll/pdu_vendor.h"
24 #include "pdu.h"
25 #include "ll.h"
26 #include "ll_settings.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 
39 #include "ull_conn_types.h"
40 #include "ull_llcp.h"
41 #include "ull_conn_internal.h"
42 #include "ull_llcp_internal.h"
43 
44 #include "helper_pdu.h"
45 #include "helper_util.h"
46 
47 static struct ll_conn conn;
48 
term_setup(void * data)49 static void term_setup(void *data)
50 {
51 	test_setup(&conn);
52 }
53 
test_terminate_rem(uint8_t role)54 static void test_terminate_rem(uint8_t role)
55 {
56 	struct pdu_data_llctrl_terminate_ind remote_terminate_ind = {
57 		.error_code = 0x05,
58 	};
59 
60 	/* Role */
61 	test_set_role(&conn, role);
62 
63 	/* Connect */
64 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
65 
66 	/* Prepare */
67 	event_prepare(&conn);
68 
69 	/* Rx */
70 	lt_tx(LL_TERMINATE_IND, &conn, &remote_terminate_ind);
71 
72 	/* Done */
73 	event_done(&conn);
74 
75 	/* Prepare */
76 	event_prepare(&conn);
77 
78 	/* Done */
79 	event_done(&conn);
80 
81 	/* There should be no host notification */
82 	ut_rx_q_is_empty();
83 
84 
85 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
86 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
87 }
88 
ZTEST(term_central,test_terminate_central_rem)89 ZTEST(term_central, test_terminate_central_rem)
90 {
91 	test_terminate_rem(BT_HCI_ROLE_CENTRAL);
92 }
93 
ZTEST(term_periph,test_terminate_periph_rem)94 ZTEST(term_periph, test_terminate_periph_rem)
95 {
96 	test_terminate_rem(BT_HCI_ROLE_PERIPHERAL);
97 }
98 
test_terminate_loc(uint8_t role)99 static void test_terminate_loc(uint8_t role)
100 {
101 	uint8_t err;
102 	struct node_tx *tx;
103 
104 	struct pdu_data_llctrl_terminate_ind local_terminate_ind = {
105 		.error_code = 0x06,
106 	};
107 
108 	/* Role */
109 	test_set_role(&conn, role);
110 
111 	/* Connect */
112 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
113 
114 	/* Initiate an LE Ping Procedure */
115 	err = ull_cp_terminate(&conn, 0x06);
116 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
117 
118 	/* Prepare */
119 	event_prepare(&conn);
120 
121 	/* Tx Queue should have one LL Control PDU */
122 	lt_rx(LL_TERMINATE_IND, &conn, &tx, &local_terminate_ind);
123 	lt_rx_q_is_empty(&conn);
124 
125 	/* RX Ack */
126 	event_tx_ack(&conn, tx);
127 
128 	/* Done */
129 	event_done(&conn);
130 
131 	/* Release tx node */
132 	ull_cp_release_tx(&conn, tx);
133 
134 	/* There should be no host notification */
135 	ut_rx_q_is_empty();
136 
137 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
138 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
139 }
140 
ZTEST(term_central,test_terminate_central_loc)141 ZTEST(term_central, test_terminate_central_loc)
142 {
143 	test_terminate_loc(BT_HCI_ROLE_CENTRAL);
144 }
145 
ZTEST(term_periph,test_terminate_periph_loc)146 ZTEST(term_periph, test_terminate_periph_loc)
147 {
148 	test_terminate_loc(BT_HCI_ROLE_PERIPHERAL);
149 }
150 
151 ZTEST_SUITE(term_central, NULL, NULL, term_setup, NULL, NULL);
152 ZTEST_SUITE(term_periph, NULL, NULL, term_setup, NULL, NULL);
153