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 struct ll_conn {
158 	struct ull_hdr  ull;
159 	struct lll_conn lll;
160 
161 	struct ull_tx_q tx_q;
162 	struct llcp_struct llcp;
163 
164 	struct {
165 		uint8_t reason_final;
166 		/* node rx type with memory aligned storage for terminate
167 		 * reason.
168 		 * HCI will reference the value using the pdu member of
169 		 * struct node_rx_pdu.
170 		 */
171 		struct {
172 			struct node_rx_hdr hdr;
173 
174 			uint8_t reason __aligned(4);
175 		} node_rx;
176 	} llcp_terminate;
177 
178 /*
179  * TODO: all the following comes from the legacy LL llcp structure
180  * and/or needs to be properly integrated in the control procedures
181  */
182 	union {
183 		struct {
184 #if defined(CONFIG_BT_CTLR_CONN_META)
185 			uint8_t  is_must_expire:1;
186 #endif /* CONFIG_BT_CTLR_CONN_META */
187 		} common;
188 #if defined(CONFIG_BT_PERIPHERAL)
189 		struct {
190 #if defined(CONFIG_BT_CTLR_CONN_META)
191 			uint8_t  is_must_expire:1;
192 #endif /* CONFIG_BT_CTLR_CONN_META */
193 			uint8_t  latency_cancel:1;
194 			uint8_t  sca:3;
195 			uint32_t force;
196 			uint32_t ticks_to_offset;
197 		} periph;
198 #endif /* CONFIG_BT_PERIPHERAL */
199 
200 #if defined(CONFIG_BT_CENTRAL)
201 		struct {
202 #if defined(CONFIG_BT_CTLR_CONN_META)
203 			uint8_t  is_must_expire:1;
204 #endif /* CONFIG_BT_CTLR_CONN_META */
205 			uint8_t terminate_ack:1;
206 		} central;
207 #endif /* CONFIG_BT_CENTRAL */
208 	};
209 
210 	/* Cancel the prepare in the instant a Connection Update takes place */
211 	uint8_t cancel_prepare:1;
212 
213 #if defined(CONFIG_BT_CTLR_LE_ENC)
214 	/* Pause Rx data PDU's */
215 	uint8_t pause_rx_data:1;
216 #endif /* CONFIG_BT_CTLR_LE_ENC */
217 
218 #if defined(CONFIG_BT_CTLR_LE_PING)
219 	uint16_t appto_reload;
220 	uint16_t appto_expire;
221 	uint16_t apto_reload;
222 	uint16_t apto_expire;
223 #endif /* CONFIG_BT_CTLR_LE_PING */
224 
225 	uint16_t connect_expire;
226 	uint16_t supervision_timeout;
227 	uint16_t supervision_expire;
228 	uint32_t connect_accept_to;
229 
230 #if defined(CONFIG_BT_CTLR_PHY)
231 	uint8_t phy_pref_tx:3;
232 	uint8_t phy_pref_rx:3;
233 #endif /* CONFIG_BT_CTLR_PHY */
234 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
235 	uint16_t default_tx_octets;
236 
237 #if defined(CONFIG_BT_CTLR_PHY)
238 	uint16_t default_tx_time;
239 #endif /* CONFIG_BT_CTLR_PHY */
240 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
241 
242 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN)
243 	uint8_t own_id_addr_type:1;
244 	uint8_t peer_id_addr_type:1;
245 	uint8_t own_id_addr[BDADDR_SIZE];
246 	uint8_t peer_id_addr[BDADDR_SIZE];
247 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN */
248 
249 #if defined(CONFIG_BT_CTLR_LLID_DATA_START_EMPTY)
250 	/* Detect empty L2CAP start frame */
251 	uint8_t  start_empty:1;
252 #endif /* CONFIG_BT_CTLR_LLID_DATA_START_EMPTY */
253 }; /* struct ll_conn */
254 
255 struct node_rx_cc {
256 	uint8_t  status;
257 	uint8_t  role;
258 	uint8_t  peer_addr_type;
259 	uint8_t  peer_addr[BDADDR_SIZE];
260 #if defined(CONFIG_BT_CTLR_PRIVACY)
261 	uint8_t  peer_rpa[BDADDR_SIZE];
262 	uint8_t  local_rpa[BDADDR_SIZE];
263 #endif /* CONFIG_BT_CTLR_PRIVACY */
264 	uint16_t interval;
265 	uint16_t latency;
266 	uint16_t timeout;
267 	uint8_t  sca;
268 };
269 
270 struct node_rx_cu {
271 	uint8_t  status;
272 	uint16_t interval;
273 	uint16_t latency;
274 	uint16_t timeout;
275 };
276 
277 struct node_rx_cs {
278 	uint8_t csa;
279 };
280 
281 struct node_rx_pu {
282 	uint8_t status;
283 	uint8_t tx;
284 	uint8_t rx;
285 };
286 
287 struct node_rx_sca {
288 	uint8_t status;
289 	uint8_t sca;
290 };
291