1 /*
2  * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define IS_ACL_HANDLE(_handle) ((_handle) < CONFIG_BT_MAX_CONN)
8 
9 enum llcp {
10 	LLCP_NONE,
11 	LLCP_CONN_UPD,
12 	LLCP_CHAN_MAP,
13 
14 	/*
15 	 * LLCP_TERMINATE,
16 	 * LLCP_FEATURE_EXCHANGE,
17 	 * LLCP_VERSION_EXCHANGE,
18 	 */
19 
20 #if defined(CONFIG_BT_CTLR_LE_ENC)
21 	LLCP_ENCRYPTION,
22 #endif /* CONFIG_BT_CTLR_LE_ENC */
23 
24 	LLCP_CONNECTION_PARAM_REQ,
25 
26 #if defined(CONFIG_BT_CTLR_LE_PING)
27 	LLCP_PING,
28 #endif /* CONFIG_BT_CTLR_LE_PING */
29 
30 #if defined(CONFIG_BT_CTLR_PHY)
31 	LLCP_PHY_UPD,
32 #endif /* CONFIG_BT_CTLR_PHY */
33 };
34 
35 /*
36  * to reduce length and unreadability of the ll_conn struct the
37  * structures inside it have been defined first
38  */
39 struct llcp_struct {
40 	/* Local Request */
41 	struct {
42 		sys_slist_t pend_proc_list;
43 		uint8_t state;
44 		/* Procedure Response Timeout timer expire value */
45 		uint16_t prt_expire;
46 		uint8_t pause;
47 	} local;
48 
49 	/* Remote Request */
50 	struct {
51 		sys_slist_t pend_proc_list;
52 		uint8_t state;
53 		/* Procedure Response Timeout timer expire value */
54 		uint16_t prt_expire;
55 		uint8_t pause;
56 		uint8_t collision;
57 		uint8_t incompat;
58 		uint8_t reject_opcode;
59 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP) || defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
60 		uint8_t paused_cmd;
61 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP || CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
62 	} remote;
63 
64 	/* Procedure Response Timeout timer reload value */
65 	uint16_t prt_reload;
66 
67 	/* Prepare parameters */
68 	struct {
69 		uint32_t ticks_at_expire; /* Vendor specific tick units */
70 		uint32_t remainder;       /* Vendor specific remainder fraction of a tick unit */
71 		uint16_t lazy;            /* Previous skipped radio event count */
72 	} prep;
73 
74 	/* Version Exchange Procedure State */
75 	struct {
76 		uint8_t sent;
77 		uint8_t valid;
78 		struct pdu_data_llctrl_version_ind cached;
79 	} vex;
80 
81 	/*
82 	 * As of today only 36 feature bits are in use,
83 	 * so some optimisation is possible
84 	 * we also need to keep track of the features of the
85 	 * other node, so that we can send a proper
86 	 * reply over HCI to the host
87 	 * see BT Core spec 5.2 Vol 6, Part B, sec. 5.1.4
88 	 */
89 	struct {
90 		uint8_t valid;
91 		/*
92 		 * Stores features supported by peer device. The content of the member may be
93 		 * verified when feature exchange procedure has completed, valid member is set to 1.
94 		 */
95 		uint64_t features_peer;
96 		/*
97 		 * Stores features common for two connected devices. Before feature exchange
98 		 * procedure is completed, the member stores information about all features
99 		 * supported by local device. After completion of the procedure, the feature set
100 		 * may be limited to features that are common.
101 		 */
102 		uint64_t features_used;
103 	} fex;
104 
105 	/* Minimum used channels procedure state */
106 	struct {
107 		uint8_t phys;
108 		uint8_t min_used_chans;
109 	} muc;
110 
111 	/* TODO: we'll need the next few structs eventually,
112 	 * Thomas and Szymon please comment on names etc.
113 	 */
114 	struct {
115 		uint16_t *pdu_win_offset;
116 		uint32_t ticks_anchor;
117 	} conn_upd;
118 
119 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
120 	/* @brief Constant Tone Extension configuration for CTE request control procedure. */
121 	struct llcp_df_req_cfg {
122 		/* Procedure may be active periodically, active state must be stored.
123 		 * If procedure is active, request parameters update may not be issued.
124 		 */
125 		volatile uint8_t is_enabled;
126 		uint8_t cte_type;
127 		/* Minimum requested CTE length in 8us units */
128 		uint8_t min_cte_len;
129 		uint16_t req_interval;
130 		uint16_t req_expire;
131 	} cte_req;
132 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
133 
134 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
135 	struct llcp_df_rsp_cfg {
136 		uint8_t is_enabled:1;
137 		uint8_t is_active:1;
138 		uint8_t cte_types;
139 		uint8_t max_cte_len;
140 		void *disable_param;
141 		void (*disable_cb)(void *param);
142 	} cte_rsp;
143 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
144 
145 	struct {
146 		uint8_t terminate_ack;
147 	} cis;
148 
149 	uint8_t tx_buffer_alloc;
150 	uint8_t tx_q_pause_data_mask;
151 
152 	struct node_rx_pdu *rx_node_release;
153 	struct node_tx *tx_node_release;
154 
155 }; /* struct llcp_struct */
156 
157 #if defined(CONFIG_BT_CTLR_SYNC_TRANSFER_RECEIVER)
158 struct past_params {
159 	uint8_t  mode;
160 	uint8_t  cte_type;
161 	uint16_t skip;
162 	uint16_t timeout;
163 }; /* struct past_params */
164 #endif /* CONFIG_BT_CTLR_SYNC_TRANSFER_RECEIVER */
165 
166 struct ll_conn {
167 	struct ull_hdr  ull;
168 	struct lll_conn lll;
169 
170 #if defined(CONFIG_BT_CTLR_SYNC_TRANSFER_RECEIVER)
171 	struct past_params past;
172 #endif /* CONFIG_BT_CTLR_SYNC_TRANSFER_RECEIVER */
173 
174 	struct ull_tx_q tx_q;
175 	struct llcp_struct llcp;
176 
177 	struct {
178 		uint8_t reason_final;
179 		/* node rx type with dummy uint8_t to ensure room for terminate
180 		 * reason.
181 		 * HCI will reference the value using the pdu member of
182 		 * struct node_rx_pdu.
183 		 *
184 		 */
185 		struct {
186 			struct node_rx_pdu rx;
187 			uint8_t dummy_reason;
188 		} node_rx;
189 	} llcp_terminate;
190 
191 /*
192  * TODO: all the following comes from the legacy LL llcp structure
193  * and/or needs to be properly integrated in the control procedures
194  */
195 	union {
196 		struct {
197 #if defined(CONFIG_BT_CTLR_CONN_META)
198 			uint8_t  is_must_expire:1;
199 #endif /* CONFIG_BT_CTLR_CONN_META */
200 		} common;
201 #if defined(CONFIG_BT_PERIPHERAL)
202 		struct {
203 #if defined(CONFIG_BT_CTLR_CONN_META)
204 			uint8_t  is_must_expire:1;
205 #endif /* CONFIG_BT_CTLR_CONN_META */
206 			uint8_t  latency_cancel:1;
207 			uint8_t  sca:3;
208 			uint8_t  drift_skip;
209 			uint32_t force;
210 			uint32_t ticks_to_offset;
211 		} periph;
212 #endif /* CONFIG_BT_PERIPHERAL */
213 
214 #if defined(CONFIG_BT_CENTRAL)
215 		struct {
216 #if defined(CONFIG_BT_CTLR_CONN_META)
217 			uint8_t  is_must_expire:1;
218 #endif /* CONFIG_BT_CTLR_CONN_META */
219 		} central;
220 #endif /* CONFIG_BT_CENTRAL */
221 	};
222 
223 	/* Cancel the prepare in the instant a Connection Update takes place */
224 	uint8_t cancel_prepare:1;
225 
226 #if defined(CONFIG_BT_CTLR_LE_ENC)
227 	/* Pause Rx data PDU's */
228 	uint8_t pause_rx_data:1;
229 #endif /* CONFIG_BT_CTLR_LE_ENC */
230 
231 #if defined(CONFIG_BT_CTLR_LE_PING)
232 	uint16_t appto_reload;
233 	uint16_t appto_expire;
234 	uint16_t apto_reload;
235 	uint16_t apto_expire;
236 #endif /* CONFIG_BT_CTLR_LE_PING */
237 
238 	uint16_t connect_expire;
239 	uint16_t supervision_timeout;
240 	uint16_t supervision_expire;
241 	uint32_t connect_accept_to;
242 
243 #if defined(CONFIG_BT_CTLR_PHY)
244 	uint8_t phy_pref_tx:3;
245 	uint8_t phy_pref_rx:3;
246 #endif /* CONFIG_BT_CTLR_PHY */
247 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
248 	uint16_t default_tx_octets;
249 
250 #if defined(CONFIG_BT_CTLR_PHY)
251 	uint16_t default_tx_time;
252 #endif /* CONFIG_BT_CTLR_PHY */
253 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
254 
255 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN)
256 	uint8_t own_id_addr_type:1;
257 	uint8_t peer_id_addr_type:1;
258 	uint8_t own_id_addr[BDADDR_SIZE];
259 	uint8_t peer_id_addr[BDADDR_SIZE];
260 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN */
261 
262 #if defined(CONFIG_BT_CTLR_LLID_DATA_START_EMPTY)
263 	/* Detect empty L2CAP start frame */
264 	uint8_t  start_empty:1;
265 #endif /* CONFIG_BT_CTLR_LLID_DATA_START_EMPTY */
266 }; /* struct ll_conn */
267 
268 struct node_rx_cc {
269 	uint8_t  status;
270 	uint8_t  role;
271 	uint8_t  peer_addr_type;
272 	uint8_t  peer_addr[BDADDR_SIZE];
273 #if defined(CONFIG_BT_CTLR_PRIVACY)
274 	uint8_t  peer_rpa[BDADDR_SIZE];
275 	uint8_t  local_rpa[BDADDR_SIZE];
276 #endif /* CONFIG_BT_CTLR_PRIVACY */
277 	uint16_t interval;
278 	uint16_t latency;
279 	uint16_t timeout;
280 	uint8_t  sca;
281 };
282 
283 struct node_rx_cu {
284 	uint8_t  status;
285 	uint16_t interval;
286 	uint16_t latency;
287 	uint16_t timeout;
288 };
289 
290 struct node_rx_cs {
291 	uint8_t csa;
292 };
293 
294 struct node_rx_pu {
295 	uint8_t status;
296 	uint8_t tx;
297 	uint8_t rx;
298 };
299 
300 struct node_rx_sca {
301 	uint8_t status;
302 	uint8_t sca;
303 };
304