1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/types.h>
8 #include <zephyr/sys/byteorder.h>
9 #include <zephyr/ztest.h>
10 
11 #define ULL_LLCP_UNITTEST
12 
13 #include <zephyr/bluetooth/hci.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/sys/slist.h>
16 #include <zephyr/sys/util.h>
17 #include "hal/ccm.h"
18 
19 #include "util/util.h"
20 #include "util/mem.h"
21 #include "util/memq.h"
22 #include "util/dbuf.h"
23 
24 #include "pdu_df.h"
25 #include "lll/pdu_vendor.h"
26 #include "pdu.h"
27 #include "ll.h"
28 #include "ll_settings.h"
29 
30 #include "lll.h"
31 #include "lll/lll_df_types.h"
32 #include "lll_conn.h"
33 #include "lll_conn_iso.h"
34 
35 #include "ull_tx_queue.h"
36 
37 #include "isoal.h"
38 #include "ull_iso_types.h"
39 #include "ull_conn_iso_types.h"
40 #include "ull_conn_types.h"
41 
42 #include "ull_llcp.h"
43 #include "ull_llcp_internal.h"
44 #include "ull_conn_internal.h"
45 
46 #include "ll_feat.h"
47 
48 #include "helper_pdu.h"
49 #include "helper_util.h"
50 #include "helper_features.h"
51 
52 static struct ll_conn *conn_from_pool;
53 
hci_setup(void * data)54 static void hci_setup(void *data)
55 {
56 	ull_conn_init();
57 
58 	conn_from_pool = ll_conn_acquire();
59 	zassert_not_null(conn_from_pool, "Could not allocate connection memory", NULL);
60 
61 	test_setup(conn_from_pool);
62 }
63 
64 /*
65  * +-----+                     +-------+            +-----+
66  * | UT  |                     | LL_A  |            | LT  |
67  * +-----+                     +-------+            +-----+
68  *    |                            |                   |
69  *    | Start                      |                   |
70  *    | Feature Exchange Proc.     |                   |
71  *    |--------------------------->|                   |
72  *    |                            |                   |
73  *    |                            | LL_FEATURE_REQ    |
74  *    |                            |------------------>|
75  *    |                            |                   |
76  *    |                            |    LL_FEATURE_RSP |
77  *    |                            |<------------------|
78  *    |                            |                   |
79  *    |     Feature Exchange Proc. |                   |
80  *    |                   Complete |                   |
81  *    |<---------------------------|                   |
82  *    |                            |                   |
83  */
ZTEST(hci_fex,test_hci_feat_exchange_central_loc)84 ZTEST(hci_fex, test_hci_feat_exchange_central_loc)
85 {
86 	uint64_t err;
87 	uint64_t set_featureset[] = {
88 		DEFAULT_FEATURE,
89 		DEFAULT_FEATURE };
90 	uint64_t rsp_featureset[] = { ((LL_FEAT_BIT_MASK_VALID & FEAT_FILTER_OCTET0) |
91 				       DEFAULT_FEATURE) &
92 					      LL_FEAT_BIT_MASK_VALID,
93 				      0x0 };
94 	int feat_to_test = ARRAY_SIZE(set_featureset);
95 
96 	struct node_tx *tx;
97 	struct node_rx_pdu *ntf;
98 
99 	struct pdu_data_llctrl_feature_req local_feature_req;
100 	struct pdu_data_llctrl_feature_rsp remote_feature_rsp;
101 	int feat_counter;
102 	uint16_t conn_handle;
103 
104 	for (feat_counter = 0; feat_counter < feat_to_test; feat_counter++) {
105 		conn_handle = ll_conn_handle_get(conn_from_pool);
106 
107 		sys_put_le64(set_featureset[feat_counter], local_feature_req.features);
108 		sys_put_le64(rsp_featureset[feat_counter], remote_feature_rsp.features);
109 
110 		test_set_role(conn_from_pool, BT_HCI_ROLE_CENTRAL);
111 		/* Connect */
112 		ull_cp_state_set(conn_from_pool, ULL_CP_CONNECTED);
113 
114 		/* Initiate a Feature Exchange Procedure via HCI */
115 		err = ll_feature_req_send(conn_handle);
116 
117 		zassert_equal(err, BT_HCI_ERR_SUCCESS, "Error: %d", err);
118 
119 		event_prepare(conn_from_pool);
120 
121 		/* Tx Queue should have one LL Control PDU */
122 		lt_rx(LL_FEATURE_REQ, conn_from_pool, &tx, &local_feature_req);
123 		lt_rx_q_is_empty(conn_from_pool);
124 
125 		/* Rx */
126 		lt_tx(LL_FEATURE_RSP, conn_from_pool, &remote_feature_rsp);
127 
128 		event_done(conn_from_pool);
129 		/* There should be one host notification */
130 
131 		ut_rx_pdu(LL_FEATURE_RSP, &ntf, &remote_feature_rsp);
132 
133 		ut_rx_q_is_empty();
134 
135 		zassert_equal(conn_from_pool->lll.event_counter, feat_counter + 1,
136 			      "Wrong event count %d\n", conn_from_pool->lll.event_counter);
137 
138 		ull_cp_release_tx(conn_from_pool, tx);
139 		release_ntf(ntf);
140 
141 		ll_conn_release(conn_from_pool);
142 	}
143 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
144 				  "Free CTX buffers %d", llcp_ctx_buffers_free());
145 }
146 
ZTEST(hci_fex,test_hci_feat_exchange_wrong_handle)147 ZTEST(hci_fex, test_hci_feat_exchange_wrong_handle)
148 {
149 	uint16_t conn_handle;
150 	uint64_t err;
151 	int ctx_counter;
152 	struct proc_ctx *ctx;
153 
154 	conn_handle = ll_conn_handle_get(conn_from_pool);
155 
156 	err = ll_feature_req_send(conn_handle + 1);
157 
158 	zassert_equal(err, BT_HCI_ERR_UNKNOWN_CONN_ID, "Wrong reply for wrong handle\n");
159 
160 	ctx_counter = 0;
161 	do {
162 		ctx = llcp_create_local_procedure(PROC_FEATURE_EXCHANGE);
163 		ctx_counter++;
164 	} while (ctx != NULL);
165 
166 	zassert_equal(ctx_counter, CONFIG_BT_CTLR_LLCP_LOCAL_PROC_CTX_BUF_NUM + 1,
167 				   "Error in setup of test\n");
168 
169 	err = ll_feature_req_send(conn_handle);
170 	zassert_equal(err, BT_HCI_ERR_CMD_DISALLOWED, "Wrong reply for wrong handle\n");
171 
172 	zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt() - (ctx_counter - 1),
173 		      "Free CTX buffers %d", llcp_ctx_buffers_free());
174 }
175 
176 ZTEST_SUITE(hci_fex, NULL, NULL, hci_setup, NULL, NULL);
177