1 /*
2  * Copyright (c) 2022 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 
cis_terminate_setup(void * data)47 static void cis_terminate_setup(void *data)
48 {
49 	test_setup(&conn);
50 }
51 
test_cis_terminate_rem(uint8_t role)52 static void test_cis_terminate_rem(uint8_t role)
53 {
54 	struct pdu_data_llctrl_cis_terminate_ind remote_cis_terminate_ind;
55 
56 	/* Role */
57 	test_set_role(&conn, role);
58 
59 	/* Connect */
60 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
61 
62 	/* Prepare */
63 	event_prepare(&conn);
64 
65 	/* Rx */
66 	lt_tx(LL_CIS_TERMINATE_IND, &conn, &remote_cis_terminate_ind);
67 
68 	/* Done */
69 	event_done(&conn);
70 
71 	/* There should be no host notification */
72 	ut_rx_q_is_empty();
73 
74 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
75 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
76 }
77 
ZTEST(cis_terminate,test_cis_terminate_cen_rem)78 ZTEST(cis_terminate, test_cis_terminate_cen_rem)
79 {
80 	test_cis_terminate_rem(BT_HCI_ROLE_CENTRAL);
81 }
82 
ZTEST(cis_terminate,test_cis_terminate_per_rem)83 ZTEST(cis_terminate, test_cis_terminate_per_rem)
84 {
85 	test_cis_terminate_rem(BT_HCI_ROLE_PERIPHERAL);
86 }
87 
test_cis_terminate_loc(uint8_t role)88 void test_cis_terminate_loc(uint8_t role)
89 {
90 	uint8_t err;
91 	struct node_tx *tx;
92 	struct ll_conn_iso_stream cis = { 0 };
93 	struct ll_conn_iso_group group = { 0 };
94 
95 	struct pdu_data_llctrl_cis_terminate_ind local_cis_terminate_ind = {
96 		.cig_id = 0x03,
97 		.cis_id = 0x04,
98 		.error_code = 0x06,
99 	};
100 
101 	/* Role */
102 	test_set_role(&conn, role);
103 
104 	/* Connect */
105 	ull_cp_state_set(&conn, ULL_CP_CONNECTED);
106 
107 	/* Mock CIS/ACL */
108 	cis.lll.acl_handle = conn.lll.handle;
109 	group.cig_id = local_cis_terminate_ind.cig_id;
110 	cis.cis_id = local_cis_terminate_ind.cis_id;
111 	cis.group = &group;
112 
113 	/* Initiate an CIS Terminate Procedure */
114 	err = ull_cp_cis_terminate(&conn, &cis, local_cis_terminate_ind.error_code);
115 	zassert_equal(err, BT_HCI_ERR_SUCCESS);
116 
117 	/* Prepare */
118 	event_prepare(&conn);
119 
120 	lt_rx_q_is_empty(&conn);
121 
122 	/* Done */
123 	event_done(&conn);
124 
125 	/* 'Signal' CIS terminated */
126 	conn.llcp.cis.terminate_ack = 1;
127 
128 	/* Prepare */
129 	event_prepare(&conn);
130 
131 	/* Tx Queue should now have one LL Control PDU */
132 	lt_rx(LL_CIS_TERMINATE_IND, &conn, &tx, &local_cis_terminate_ind);
133 	lt_rx_q_is_empty(&conn);
134 
135 	/* RX Ack */
136 	event_tx_ack(&conn, tx);
137 
138 	/* Done */
139 	event_done(&conn);
140 
141 	/* Release tx node */
142 	ull_cp_release_tx(&conn, tx);
143 
144 	/* There should be no host notification */
145 	ut_rx_q_is_empty();
146 
147 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
148 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
149 }
150 
ZTEST(cis_terminate,test_cis_terminate_cen_loc)151 ZTEST(cis_terminate, test_cis_terminate_cen_loc)
152 {
153 	test_cis_terminate_loc(BT_HCI_ROLE_CENTRAL);
154 }
155 
ZTEST(cis_terminate,test_cis_terminate_per_loc)156 ZTEST(cis_terminate, test_cis_terminate_per_loc)
157 {
158 	test_cis_terminate_loc(BT_HCI_ROLE_PERIPHERAL);
159 }
160 
161 ZTEST_SUITE(cis_terminate, NULL, NULL, cis_terminate_setup, NULL, NULL);
162