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 dummy uint8_t to ensure room for terminate
167 		 * reason.
168 		 * HCI will reference the value using the pdu member of
169 		 * struct node_rx_pdu.
170 		 *
171 		 */
172 		struct {
173 			struct node_rx_pdu rx;
174 			uint8_t dummy_reason;
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 		} central;
206 #endif /* CONFIG_BT_CENTRAL */
207 	};
208 
209 	/* Cancel the prepare in the instant a Connection Update takes place */
210 	uint8_t cancel_prepare:1;
211 
212 #if defined(CONFIG_BT_CTLR_LE_ENC)
213 	/* Pause Rx data PDU's */
214 	uint8_t pause_rx_data:1;
215 #endif /* CONFIG_BT_CTLR_LE_ENC */
216 
217 #if defined(CONFIG_BT_CTLR_LE_PING)
218 	uint16_t appto_reload;
219 	uint16_t appto_expire;
220 	uint16_t apto_reload;
221 	uint16_t apto_expire;
222 #endif /* CONFIG_BT_CTLR_LE_PING */
223 
224 	uint16_t connect_expire;
225 	uint16_t supervision_timeout;
226 	uint16_t supervision_expire;
227 	uint32_t connect_accept_to;
228 
229 #if defined(CONFIG_BT_CTLR_PHY)
230 	uint8_t phy_pref_tx:3;
231 	uint8_t phy_pref_rx:3;
232 #endif /* CONFIG_BT_CTLR_PHY */
233 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
234 	uint16_t default_tx_octets;
235 
236 #if defined(CONFIG_BT_CTLR_PHY)
237 	uint16_t default_tx_time;
238 #endif /* CONFIG_BT_CTLR_PHY */
239 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
240 
241 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN)
242 	uint8_t own_id_addr_type:1;
243 	uint8_t peer_id_addr_type:1;
244 	uint8_t own_id_addr[BDADDR_SIZE];
245 	uint8_t peer_id_addr[BDADDR_SIZE];
246 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN */
247 
248 #if defined(CONFIG_BT_CTLR_LLID_DATA_START_EMPTY)
249 	/* Detect empty L2CAP start frame */
250 	uint8_t  start_empty:1;
251 #endif /* CONFIG_BT_CTLR_LLID_DATA_START_EMPTY */
252 }; /* struct ll_conn */
253 
254 struct node_rx_cc {
255 	uint8_t  status;
256 	uint8_t  role;
257 	uint8_t  peer_addr_type;
258 	uint8_t  peer_addr[BDADDR_SIZE];
259 #if defined(CONFIG_BT_CTLR_PRIVACY)
260 	uint8_t  peer_rpa[BDADDR_SIZE];
261 	uint8_t  local_rpa[BDADDR_SIZE];
262 #endif /* CONFIG_BT_CTLR_PRIVACY */
263 	uint16_t interval;
264 	uint16_t latency;
265 	uint16_t timeout;
266 	uint8_t  sca;
267 };
268 
269 struct node_rx_cu {
270 	uint8_t  status;
271 	uint16_t interval;
272 	uint16_t latency;
273 	uint16_t timeout;
274 };
275 
276 struct node_rx_cs {
277 	uint8_t csa;
278 };
279 
280 struct node_rx_pu {
281 	uint8_t status;
282 	uint8_t tx;
283 	uint8_t rx;
284 };
285 
286 struct node_rx_sca {
287 	uint8_t status;
288 	uint8_t sca;
289 };
290