1 /*
2  * Copyright (c) 2020 Demant
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /* LLCP Memory Pool Descriptor */
8 struct llcp_mem_pool {
9 	void *free;
10 	uint8_t *pool;
11 };
12 
13 /* LLCP Procedure */
14 enum llcp_proc {
15 	PROC_UNKNOWN,
16 	PROC_LE_PING,
17 	PROC_FEATURE_EXCHANGE,
18 	PROC_MIN_USED_CHANS,
19 	PROC_VERSION_EXCHANGE,
20 	PROC_ENCRYPTION_START,
21 	PROC_ENCRYPTION_PAUSE,
22 	PROC_PHY_UPDATE,
23 	PROC_CONN_UPDATE,
24 	PROC_CONN_PARAM_REQ,
25 	PROC_TERMINATE,
26 	PROC_CHAN_MAP_UPDATE,
27 	PROC_DATA_LENGTH_UPDATE,
28 	PROC_CTE_REQ,
29 	PROC_CIS_CREATE,
30 	PROC_CIS_TERMINATE,
31 	PROC_SCA_UPDATE,
32 	/* A helper enum entry, to use in pause procedure context */
33 	PROC_NONE = 0x0,
34 };
35 
36 enum llcp_tx_q_pause_data_mask {
37 	LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION = 0x01,
38 	LLCP_TX_QUEUE_PAUSE_DATA_PHY_UPDATE = 0x02,
39 	LLCP_TX_QUEUE_PAUSE_DATA_DATA_LENGTH = 0x04,
40 	LLCP_TX_QUEUE_PAUSE_DATA_TERMINATE = 0x08,
41 };
42 
43 #if ((CONFIG_BT_CTLR_LLCP_COMMON_TX_CTRL_BUF_NUM <\
44 			(CONFIG_BT_CTLR_LLCP_TX_PER_CONN_TX_CTRL_BUF_NUM_MAX *\
45 			CONFIG_BT_CTLR_LLCP_CONN)) &&\
46 			(CONFIG_BT_CTLR_LLCP_PER_CONN_TX_CTRL_BUF_NUM <\
47 			CONFIG_BT_CTLR_LLCP_TX_PER_CONN_TX_CTRL_BUF_NUM_MAX))
48 #define LLCP_TX_CTRL_BUF_QUEUE_ENABLE
49 #define LLCP_TX_CTRL_BUF_COUNT ((CONFIG_BT_CTLR_LLCP_COMMON_TX_CTRL_BUF_NUM  +\
50 			(CONFIG_BT_CTLR_LLCP_CONN * CONFIG_BT_CTLR_LLCP_PER_CONN_TX_CTRL_BUF_NUM)))
51 #else
52 #define LLCP_TX_CTRL_BUF_COUNT (CONFIG_BT_CTLR_LLCP_TX_PER_CONN_TX_CTRL_BUF_NUM_MAX *\
53 				CONFIG_BT_CTLR_LLCP_CONN)
54 #endif
55 
56 #if defined(LLCP_TX_CTRL_BUF_QUEUE_ENABLE)
57 enum llcp_wait_reason {
58 	WAITING_FOR_NOTHING,
59 	WAITING_FOR_TX_BUFFER,
60 };
61 #endif /* LLCP_TX_CTRL_BUF_QUEUE_ENABLE */
62 
63 #if defined(CONFIG_BT_CTLR_LE_ENC)
64 struct llcp_enc {
65 	uint8_t error;
66 
67 	/* NOTE: To save memory, SKD(m|s) and IV(m|s) are
68 	 * generated just-in-time for PDU enqueuing and are
69 	 * therefore not present in this structure.
70 	 * Further candidates for optimizing memory layout.
71 	 *	* Overlay memory
72 	 *	* Repurpose memory used by lll.ccm_tx/rx?
73 	 */
74 
75 	/* Central: Rand and EDIV are input copies from
76 	 * HCI that only live until the LL_ENC_REQ has
77 	 * been enqueued.
78 	 *
79 	 * Peripheral: Rand and EDIV are input copies from
80 	 * the LL_ENC_REQ that only live until host
81 	 * notification has been enqueued.
82 	 */
83 
84 	/* 64 bit random number. */
85 	uint8_t rand[8];
86 
87 	/* 16 bit encrypted diversifier.*/
88 	uint8_t ediv[2];
89 
90 	/* 128 bit long term key. */
91 	uint8_t ltk[16];
92 
93 	/* SKD is the concatenation of SKDm and SKDs and
94 	 * is used to calculate the session key, SK.
95 	 *
96 	 * Lifetime:
97 	 * M    : Generate SKDm and IVm
98 	 * M->S : LL_ENC_REQ(Rand, EDIV, SKDm, IVm)
99 	 * S	: Notify host (Rand, EDIV)
100 	 * S    : Generate SKDs and IVs
101 	 * S    : Calculate SK = e(LTK, SKD)
102 	 * M<-S : LL_ENC_RSP(SKDs, IVs)
103 	 * M    : Calculate SK = e(LTK, SKD)
104 	 *
105 	 * where security function e generates 128-bit
106 	 * encryptedData from a 128-bit key and 128-bit
107 	 * plaintextData using the AES-128-bit block
108 	 * cypher as defined in FIPS-1971:
109 	 *	encryptedData = e(key, plaintextData)
110 	 */
111 	union {
112 
113 		/* 128-bit session key diversifier */
114 		uint8_t skd[16];
115 		struct {
116 			uint8_t skdm[8];
117 			uint8_t skds[8];
118 		};
119 	};
120 };
121 #endif /* CONFIG_BT_CTLR_LE_ENC */
122 
123 /* LLCP Procedure Context */
124 struct proc_ctx {
125 	/* Must be the first for sys_slist to work */
126 	sys_snode_t node;
127 
128 	/* llcp_mem_pool owner of this context */
129 	struct llcp_mem_pool *owner;
130 
131 	/* PROC_ */
132 	enum llcp_proc proc;
133 
134 	enum pdu_data_llctrl_type response_opcode;
135 
136 	/* Procedure FSM */
137 	uint8_t state;
138 
139 	/* Expected opcode to be received next */
140 	enum pdu_data_llctrl_type rx_opcode;
141 
142 	/* Greedy RX (used for central encryption) */
143 	uint8_t rx_greedy;
144 
145 	/* Last transmitted opcode used for unknown/reject */
146 	enum pdu_data_llctrl_type tx_opcode;
147 
148 	/* Instant collision */
149 	int collision;
150 
151 #if defined(LLCP_TX_CTRL_BUF_QUEUE_ENABLE)
152 	/* Wait list next pointer */
153 	sys_snode_t wait_node;
154 
155 	/* Procedure wait reason */
156 	enum llcp_wait_reason wait_reason;
157 #endif /* LLCP_TX_CTRL_BUF_QUEUE_ENABLE */
158 
159 	struct {
160 		/* Rx node link element */
161 		memq_link_t *link;
162 		/* TX node awaiting ack */
163 		struct node_tx *tx_ack;
164 		/* most recent RX node */
165 		struct node_rx_pdu *rx;
166 		/* pre-allocated TX node */
167 		struct node_tx *tx;
168 	} node_ref;
169 	/*
170 	 * This flag is set to 1 when we are finished with the control
171 	 * procedure and it is safe to release the context ctx
172 	 */
173 	int done;
174 
175 	/* Procedure data */
176 	union {
177 		/* Feature Exchange Procedure */
178 		struct {
179 			uint8_t host_initiated:1;
180 		} fex;
181 		/* Used by Minimum Used Channels Procedure */
182 #if defined(CONFIG_BT_CTLR_MIN_USED_CHAN)
183 		struct {
184 			uint8_t phys;
185 			uint8_t min_used_chans;
186 		} muc;
187 #endif /* CONFIG_BT_CTLR_MIN_USED_CHAN */
188 
189 #if defined(CONFIG_BT_CTLR_LE_ENC)
190 		/* Used by Encryption Procedure */
191 		struct llcp_enc enc;
192 #endif /* CONFIG_BT_CTLR_LE_ENC */
193 
194 #if defined(CONFIG_BT_CTLR_PHY)
195 		/* PHY Update */
196 		struct {
197 			uint8_t tx:3;
198 			uint8_t rx:3;
199 			uint8_t flags:1;
200 			uint8_t host_initiated:1;
201 			uint8_t ntf_pu:1;
202 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
203 			uint8_t ntf_dle:1;
204 			struct node_rx_pdu *ntf_dle_node;
205 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
206 			uint8_t error;
207 			uint16_t instant;
208 			uint8_t c_to_p_phy;
209 			uint8_t p_to_c_phy;
210 		} pu;
211 #endif /* CONFIG_BT_CTLR_PHY */
212 
213 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
214 		struct {
215 			uint8_t ntf_dle;
216 		} dle;
217 #endif
218 
219 		/* Connection Update & Connection Parameter Request */
220 		struct {
221 			uint8_t error;
222 			uint8_t rejected_opcode;
223 			uint8_t params_changed;
224 			uint8_t win_size;
225 			uint16_t instant;
226 			uint16_t interval_min;
227 			uint16_t interval_max;
228 			uint16_t latency;
229 			uint16_t timeout;
230 			uint32_t win_offset_us;
231 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
232 			uint8_t  preferred_periodicity;
233 			uint16_t reference_conn_event_count;
234 			uint16_t offsets[6];
235 #endif /* defined(CONFIG_BT_CTLR_CONN_PARAM_REQ) */
236 		} cu;
237 
238 		/* Use by ACL Termination Procedure */
239 		struct {
240 			uint8_t error_code;
241 		} term;
242 
243 		/* Use by Channel Map Update Procedure */
244 		struct {
245 			uint16_t instant;
246 			uint8_t chm[5];
247 		} chmu;
248 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
249 		/* Use by CTE Request Procedure */
250 		struct {
251 			uint8_t type:2;
252 			uint8_t min_len:5;
253 		} cte_req;
254 
255 		struct llcp_df_cte_remote_rsp {
256 			/* Storage for information that received LL_CTE_RSP PDU includes CTE */
257 			uint8_t has_cte;
258 		} cte_remote_rsp;
259 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
260 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
261 		/* Use by CTE Response Procedure */
262 		struct llcp_df_cte_remote_req {
263 			uint8_t cte_type;
264 			uint8_t min_cte_len;
265 		} cte_remote_req;
266 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
267 		struct {
268 			uint8_t  error;
269 			uint16_t cis_handle;
270 			uint8_t  cig_id;
271 			uint8_t  cis_id;
272 			uint16_t conn_event_count;
273 			uint16_t iso_interval;
274 			uint32_t cis_offset_min;
275 			uint32_t cis_offset_max;
276 #if defined(CONFIG_BT_PERIPHERAL)
277 			uint32_t host_request_to;
278 #endif /* defined(CONFIG_BT_PERIPHERAL) */
279 #if defined(CONFIG_BT_CENTRAL)
280 			uint32_t cig_sync_delay;
281 			uint32_t cis_sync_delay;
282 			uint8_t  c_phy;
283 			uint8_t  p_phy;
284 			uint16_t c_max_sdu;
285 			uint16_t p_max_sdu;
286 			uint8_t  framed;
287 			uint32_t c_sdu_interval;
288 			uint32_t p_sdu_interval;
289 			uint8_t  nse;
290 			uint16_t c_max_pdu;
291 			uint16_t p_max_pdu;
292 			uint32_t sub_interval;
293 			uint8_t  p_bn;
294 			uint8_t  c_bn;
295 			uint8_t  c_ft;
296 			uint8_t  p_ft;
297 			uint8_t  aa[4];
298 #endif /* defined(CONFIG_BT_CENTRAL) */
299 		} cis_create;
300 
301 		struct {
302 			uint8_t  cig_id;
303 			uint8_t  cis_id;
304 			uint8_t error_code;
305 		} cis_term;
306 #if defined(CONFIG_BT_CTLR_SCA_UPDATE)
307 		struct {
308 			uint8_t sca;
309 			uint8_t error_code;
310 		} sca_update;
311 #endif /* CONFIG_BT_CTLR_SCA_UPDATE */
312 	} data;
313 
314 	struct {
315 		uint8_t type;
316 	} unknown_response;
317 
318 	struct {
319 		uint8_t reject_opcode;
320 		uint8_t error_code;
321 	} reject_ext_ind;
322 };
323 
324 /* Procedure Incompatibility */
325 enum proc_incompat {
326 	/* Local procedure has not sent first PDU */
327 	INCOMPAT_NO_COLLISION,
328 
329 	/* Local incompatible procedure has sent first PDU */
330 	INCOMPAT_RESOLVABLE,
331 
332 	/* Local incompatible procedure has received first PDU */
333 	INCOMPAT_RESERVED,
334 };
335 
336 /* Invalid LL Control PDU Opcode */
337 #define ULL_LLCP_INVALID_OPCODE (0xFFU)
338 
is_instant_passed(uint16_t instant,uint16_t event_count)339 static inline bool is_instant_passed(uint16_t instant, uint16_t event_count)
340 {
341 	/*
342 	 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 6, Part B
343 	 * 5.5.1 ACL Control Procedures
344 	 * When a periph receives such a PDU where (Instant – connEventCount) modulo
345 	 * 65536 is less than 32767 and Instant is not equal to connEventCount, the periph
346 	 * shall listen to all the connection events until it has confirmation that the central
347 	 * has received its acknowledgment of the PDU or connEventCount equals
348 	 * Instant.
349 	 *
350 	 * x % 2^n == x & (2^n - 1)
351 	 *
352 	 * 65535 = 2^16 - 1
353 	 * 65536 = 2^16
354 	 */
355 
356 	return ((instant - event_count) & 0xFFFFU) > 0x7FFFU;
357 }
358 
is_instant_not_passed(uint16_t instant,uint16_t event_count)359 static inline bool is_instant_not_passed(uint16_t instant, uint16_t event_count)
360 {
361 	/*
362 	 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 6, Part B
363 	 * 5.5.1 ACL Control Procedures
364 	 * When a periph receives such a PDU where (Instant – connEventCount) modulo
365 	 * 65536 is less than 32767 and Instant is not equal to connEventCount, the periph
366 	 * shall listen to all the connection events until it has confirmation that the central
367 	 * has received its acknowledgment of the PDU or connEventCount equals
368 	 * Instant.
369 	 *
370 	 * x % 2^n == x & (2^n - 1)
371 	 *
372 	 * 65535 = 2^16 - 1
373 	 * 65536 = 2^16
374 	 */
375 
376 	return ((instant - event_count) & 0xFFFFU) < 0x7FFFU;
377 }
378 
is_instant_reached_or_passed(uint16_t instant,uint16_t event_count)379 static inline bool is_instant_reached_or_passed(uint16_t instant, uint16_t event_count)
380 {
381 	/*
382 	 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 6, Part B
383 	 * 5.5.1 ACL Control Procedures
384 	 * When a periph receives such a PDU where (Instant – connEventCount) modulo
385 	 * 65536 is less than 32767 and Instant is not equal to connEventCount, the periph
386 	 * shall listen to all the connection events until it has confirmation that the central
387 	 * has received its acknowledgment of the PDU or connEventCount equals
388 	 * Instant.
389 	 *
390 	 * x % 2^n == x & (2^n - 1)
391 	 *
392 	 * 65535 = 2^16 - 1
393 	 * 65536 = 2^16
394 	 */
395 
396 	return ((event_count - instant) & 0xFFFFU) <= 0x7FFFU;
397 }
398 
399 /*
400  * LLCP Resource Management
401  */
402 bool llcp_ntf_alloc_is_available(void);
403 bool llcp_ntf_alloc_num_available(uint8_t count);
404 struct node_rx_pdu *llcp_ntf_alloc(void);
405 struct proc_ctx *llcp_create_local_procedure(enum llcp_proc proc);
406 struct proc_ctx *llcp_create_remote_procedure(enum llcp_proc proc);
407 void llcp_nodes_release(struct ll_conn *conn, struct proc_ctx *ctx);
408 bool llcp_tx_alloc_peek(struct ll_conn *conn, struct proc_ctx *ctx);
409 void llcp_tx_alloc_unpeek(struct proc_ctx *ctx);
410 struct node_tx *llcp_tx_alloc(struct ll_conn *conn, struct proc_ctx *ctx);
411 void llcp_proc_ctx_release(struct proc_ctx *ctx);
412 void llcp_ntf_set_pending(struct ll_conn *conn);
413 void llcp_ntf_clear_pending(struct ll_conn *conn);
414 bool llcp_ntf_pending(struct ll_conn *conn);
415 void llcp_rx_node_retain(struct proc_ctx *ctx);
416 
417 /*
418  * ULL -> LLL Interface
419  */
420 void llcp_tx_enqueue(struct ll_conn *conn, struct node_tx *tx);
421 void llcp_tx_pause_data(struct ll_conn *conn, enum llcp_tx_q_pause_data_mask pause_mask);
422 void llcp_tx_resume_data(struct ll_conn *conn, enum llcp_tx_q_pause_data_mask resume_mask);
423 
424 /*
425  * LLCP Procedure Response Timeout
426  */
427 void llcp_lr_prt_restart(struct ll_conn *conn);
428 void llcp_lr_prt_restart_with_value(struct ll_conn *conn, uint16_t value);
429 void llcp_lr_prt_stop(struct ll_conn *conn);
430 void llcp_rr_prt_restart(struct ll_conn *conn);
431 void llcp_rr_prt_stop(struct ll_conn *conn);
432 
433 /*
434  * LLCP Local Procedure Common
435  */
436 void llcp_lp_comm_tx_ack(struct ll_conn *conn, struct proc_ctx *ctx, struct node_tx *tx);
437 void llcp_lp_comm_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
438 void llcp_lp_comm_init_proc(struct proc_ctx *ctx);
439 void llcp_lp_comm_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
440 
441 /*
442  * LLCP Remote Procedure Common
443  */
444 void llcp_rp_comm_tx_ack(struct ll_conn *conn, struct proc_ctx *ctx, struct node_tx *tx);
445 void llcp_rp_comm_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
446 void llcp_rp_comm_init_proc(struct proc_ctx *ctx);
447 void llcp_rp_comm_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
448 
449 #if defined(CONFIG_BT_CTLR_LE_ENC)
450 /*
451  * LLCP Local Procedure Encryption
452  */
453 void llcp_lp_enc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
454 void llcp_lp_enc_init_proc(struct proc_ctx *ctx);
455 void llcp_lp_enc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
456 
457 /*
458  * LLCP Remote Procedure Encryption
459  */
460 void llcp_rp_enc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
461 void llcp_rp_enc_init_proc(struct proc_ctx *ctx);
462 void llcp_rp_enc_ltk_req_reply(struct ll_conn *conn, struct proc_ctx *ctx);
463 void llcp_rp_enc_ltk_req_neg_reply(struct ll_conn *conn, struct proc_ctx *ctx);
464 bool llcp_rp_enc_ltk_req_reply_allowed(struct ll_conn *conn, struct proc_ctx *ctx);
465 void llcp_rp_enc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
466 
467 #endif /* CONFIG_BT_CTLR_LE_ENC */
468 
469 #if defined(CONFIG_BT_CTLR_PHY)
470 /*
471  * LLCP Local Procedure PHY Update
472  */
473 void llcp_lp_pu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
474 void llcp_lp_pu_init_proc(struct proc_ctx *ctx);
475 void llcp_lp_pu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
476 void llcp_lp_pu_tx_ack(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
477 void llcp_lp_pu_tx_ntf(struct ll_conn *conn, struct proc_ctx *ctx);
478 bool llcp_lp_pu_awaiting_instant(struct proc_ctx *ctx);
479 #endif /* CONFIG_BT_CTLR_PHY */
480 
481 /*
482  * LLCP Local Procedure Connection Update
483  */
484 void llcp_lp_cu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
485 void llcp_lp_cu_init_proc(struct proc_ctx *ctx);
486 void llcp_lp_cu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
487 bool llcp_lp_cu_awaiting_instant(struct proc_ctx *ctx);
488 
489 /*
490  * LLCP Local Channel Map Update
491  */
492 void llcp_lp_chmu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
493 void llcp_lp_chmu_init_proc(struct proc_ctx *ctx);
494 void llcp_lp_chmu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
495 bool llcp_lp_chmu_awaiting_instant(struct proc_ctx *ctx);
496 
497 #if defined(CONFIG_BT_CTLR_PHY)
498 /*
499  * LLCP Remote Procedure PHY Update
500  */
501 void llcp_rp_pu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
502 void llcp_rp_pu_init_proc(struct proc_ctx *ctx);
503 void llcp_rp_pu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
504 void llcp_rp_pu_tx_ack(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
505 void llcp_rp_pu_tx_ntf(struct ll_conn *conn, struct proc_ctx *ctx);
506 bool llcp_rp_pu_awaiting_instant(struct proc_ctx *ctx);
507 #endif /* CONFIG_BT_CTLR_PHY */
508 
509 /*
510  * LLCP Remote Procedure Connection Update
511  */
512 void llcp_rp_cu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
513 void llcp_rp_cu_init_proc(struct proc_ctx *ctx);
514 void llcp_rp_cu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
515 void llcp_rp_conn_param_req_reply(struct ll_conn *conn, struct proc_ctx *ctx);
516 void llcp_rp_conn_param_req_neg_reply(struct ll_conn *conn, struct proc_ctx *ctx);
517 bool llcp_rp_conn_param_req_apm_awaiting_reply(struct proc_ctx *ctx);
518 void llcp_rp_conn_param_req_apm_reply(struct ll_conn *conn, struct proc_ctx *ctx);
519 bool llcp_rp_cu_awaiting_instant(struct proc_ctx *ctx);
520 
521 /*
522  * Terminate Helper
523  */
524 void llcp_pdu_encode_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
525 void llcp_pdu_decode_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
526 
527 /*
528  * LLCP Local Request
529  */
530 struct proc_ctx *llcp_lr_peek(struct ll_conn *conn);
531 struct proc_ctx *llcp_lr_peek_proc(struct ll_conn *conn, uint8_t proc);
532 bool llcp_lr_ispaused(struct ll_conn *conn);
533 void llcp_lr_pause(struct ll_conn *conn);
534 void llcp_lr_resume(struct ll_conn *conn);
535 void llcp_lr_tx_ack(struct ll_conn *conn, struct proc_ctx *ctx, struct node_tx *tx);
536 void llcp_lr_tx_ntf(struct ll_conn *conn, struct proc_ctx *ctx);
537 void llcp_lr_rx(struct ll_conn *conn, struct proc_ctx *ctx, memq_link_t *link,
538 		struct node_rx_pdu *rx);
539 void llcp_lr_enqueue(struct ll_conn *conn, struct proc_ctx *ctx);
540 void llcp_lr_init(struct ll_conn *conn);
541 void llcp_lr_run(struct ll_conn *conn);
542 void llcp_lr_complete(struct ll_conn *conn);
543 void llcp_lr_connect(struct ll_conn *conn);
544 void llcp_lr_disconnect(struct ll_conn *conn);
545 void llcp_lr_terminate(struct ll_conn *conn);
546 void llcp_lr_check_done(struct ll_conn *conn, struct proc_ctx *ctx);
547 
548 /*
549  * LLCP Remote Request
550  */
551 void llcp_rr_set_incompat(struct ll_conn *conn, enum proc_incompat incompat);
552 bool llcp_rr_get_collision(struct ll_conn *conn);
553 void llcp_rr_set_paused_cmd(struct ll_conn *conn, enum llcp_proc);
554 enum llcp_proc llcp_rr_get_paused_cmd(struct ll_conn *conn);
555 struct proc_ctx *llcp_rr_peek(struct ll_conn *conn);
556 bool llcp_rr_ispaused(struct ll_conn *conn);
557 void llcp_rr_pause(struct ll_conn *conn);
558 void llcp_rr_resume(struct ll_conn *conn);
559 void llcp_rr_tx_ack(struct ll_conn *conn, struct proc_ctx *ctx, struct node_tx *tx);
560 void llcp_rr_tx_ntf(struct ll_conn *conn, struct proc_ctx *ctx);
561 void llcp_rr_rx(struct ll_conn *conn, struct proc_ctx *ctx, memq_link_t *link,
562 		struct node_rx_pdu *rx);
563 void llcp_rr_init(struct ll_conn *conn);
564 void llcp_rr_prepare(struct ll_conn *conn, struct node_rx_pdu *rx);
565 void llcp_rr_run(struct ll_conn *conn);
566 void llcp_rr_complete(struct ll_conn *conn);
567 void llcp_rr_connect(struct ll_conn *conn);
568 void llcp_rr_disconnect(struct ll_conn *conn);
569 void llcp_rr_terminate(struct ll_conn *conn);
570 void llcp_rr_new(struct ll_conn *conn, memq_link_t *link, struct node_rx_pdu *rx,
571 		 bool valid_pdu);
572 void llcp_rr_check_done(struct ll_conn *conn, struct proc_ctx *ctx);
573 
574 #if defined(CONFIG_BT_CTLR_LE_PING)
575 /*
576  * LE Ping Procedure Helper
577  */
578 void llcp_pdu_encode_ping_req(struct pdu_data *pdu);
579 void llcp_pdu_encode_ping_rsp(struct pdu_data *pdu);
580 #endif /* CONFIG_BT_CTLR_LE_PING */
581 /*
582  * Unknown response helper
583  */
584 
585 void llcp_pdu_encode_unknown_rsp(struct proc_ctx *ctx,
586 					struct pdu_data *pdu);
587 void llcp_pdu_decode_unknown_rsp(struct proc_ctx *ctx,
588 					struct pdu_data *pdu);
589 void llcp_ntf_encode_unknown_rsp(struct proc_ctx *ctx,
590 					struct pdu_data *pdu);
591 
592 /*
593  * Feature Exchange Procedure Helper
594  */
595 void llcp_pdu_encode_feature_req(struct ll_conn *conn,
596 					struct pdu_data *pdu);
597 void llcp_pdu_encode_feature_rsp(struct ll_conn *conn,
598 					struct pdu_data *pdu);
599 void llcp_ntf_encode_feature_rsp(struct ll_conn *conn,
600 					struct pdu_data *pdu);
601 void llcp_ntf_encode_feature_req(struct ll_conn *conn,
602 					      struct pdu_data *pdu);
603 void llcp_pdu_decode_feature_req(struct ll_conn *conn,
604 					struct pdu_data *pdu);
605 void llcp_pdu_decode_feature_rsp(struct ll_conn *conn,
606 					struct pdu_data *pdu);
607 
608 #if defined(CONFIG_BT_CTLR_MIN_USED_CHAN)
609 /*
610  * Minimum number of used channels Procedure Helper
611  */
612 #if defined(CONFIG_BT_PERIPHERAL)
613 void llcp_pdu_encode_min_used_chans_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
614 #endif /* CONFIG_BT_PERIPHERAL */
615 
616 #if defined(CONFIG_BT_CENTRAL)
617 void llcp_pdu_decode_min_used_chans_ind(struct ll_conn *conn, struct pdu_data *pdu);
618 #endif /* CONFIG_BT_CENTRAL */
619 #endif /* CONFIG_BT_CTLR_MIN_USED_CHAN */
620 
621 /*
622  * Version Exchange Procedure Helper
623  */
624 void llcp_pdu_encode_version_ind(struct pdu_data *pdu);
625 void llcp_ntf_encode_version_ind(struct ll_conn *conn, struct pdu_data *pdu);
626 void llcp_pdu_decode_version_ind(struct ll_conn *conn, struct pdu_data *pdu);
627 
628 #if defined(CONFIG_BT_CTLR_LE_ENC)
629 /*
630  * Encryption Start Procedure Helper
631  */
632 #if defined(CONFIG_BT_CENTRAL)
633 void llcp_pdu_encode_enc_req(struct proc_ctx *ctx, struct pdu_data *pdu);
634 #endif /* CONFIG_BT_CENTRAL */
635 
636 #if defined(CONFIG_BT_PERIPHERAL)
637 void llcp_ntf_encode_enc_req(struct proc_ctx *ctx, struct pdu_data *pdu);
638 void llcp_pdu_encode_enc_rsp(struct pdu_data *pdu);
639 void llcp_pdu_encode_start_enc_req(struct pdu_data *pdu);
640 #endif /* CONFIG_BT_PERIPHERAL */
641 
642 void llcp_pdu_encode_start_enc_rsp(struct pdu_data *pdu);
643 
644 #if defined(CONFIG_BT_CENTRAL)
645 void llcp_pdu_encode_pause_enc_req(struct pdu_data *pdu);
646 #endif /* CONFIG_BT_CENTRAL */
647 
648 void llcp_pdu_encode_pause_enc_rsp(struct pdu_data *pdu);
649 #endif /* CONFIG_BT_CTLR_LE_ENC */
650 
651 void llcp_pdu_encode_reject_ind(struct pdu_data *pdu, uint8_t error_code);
652 void llcp_pdu_encode_reject_ext_ind(struct pdu_data *pdu, uint8_t reject_opcode,
653 				    uint8_t error_code);
654 void llcp_pdu_decode_reject_ext_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
655 void llcp_ntf_encode_reject_ext_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
656 
657 /*
658  * PHY Update Procedure Helper
659  */
660 void llcp_pdu_encode_phy_req(struct proc_ctx *ctx, struct pdu_data *pdu);
661 void llcp_pdu_decode_phy_req(struct proc_ctx *ctx, struct pdu_data *pdu);
662 
663 #if defined(CONFIG_BT_PERIPHERAL)
664 void llcp_pdu_encode_phy_rsp(struct ll_conn *conn, struct pdu_data *pdu);
665 void llcp_pdu_decode_phy_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
666 #endif /* CONFIG_BT_PERIPHERAL */
667 
668 #if defined(CONFIG_BT_CENTRAL)
669 void llcp_pdu_encode_phy_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
670 void llcp_pdu_decode_phy_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
671 #endif /* CONFIG_BT_CENTRAL */
672 
673 /*
674  * Connection Update Procedure Helper
675  */
676 void llcp_pdu_encode_conn_param_req(struct proc_ctx *ctx, struct pdu_data *pdu);
677 void llcp_pdu_decode_conn_param_req(struct proc_ctx *ctx, struct pdu_data *pdu);
678 void llcp_pdu_encode_conn_param_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
679 void llcp_pdu_decode_conn_param_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
680 void llcp_pdu_encode_conn_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
681 void llcp_pdu_decode_conn_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
682 
683 /*
684  * Remote Channel Map Update Procedure Helper
685  */
686 void llcp_pdu_encode_chan_map_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
687 void llcp_pdu_decode_chan_map_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
688 void llcp_rp_chmu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
689 void llcp_rp_chmu_init_proc(struct proc_ctx *ctx);
690 void llcp_rp_chmu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
691 bool llcp_rp_chmu_awaiting_instant(struct proc_ctx *ctx);
692 
693 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
694 /*
695  * Data Length Update Procedure Helper
696  */
697 void llcp_pdu_encode_length_req(struct ll_conn *conn, struct pdu_data *pdu);
698 void llcp_pdu_encode_length_rsp(struct ll_conn *conn, struct pdu_data *pdu);
699 void llcp_pdu_decode_length_req(struct ll_conn *conn, struct pdu_data *pdu);
700 void llcp_pdu_decode_length_rsp(struct ll_conn *conn, struct pdu_data *pdu);
701 void llcp_ntf_encode_length_change(struct ll_conn *conn,
702 					struct pdu_data *pdu);
703 
704 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
705 
706 #if defined(CONFIG_BT_CTLR_SCA_UPDATE)
707 /*
708  * Sleep Clock Accuracy Update Procedure Helper
709  */
710 void llcp_pdu_encode_clock_accuracy_req(struct proc_ctx *ctx, struct pdu_data *pdu);
711 void llcp_pdu_encode_clock_accuracy_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
712 void llcp_pdu_decode_clock_accuracy_req(struct proc_ctx *ctx, struct pdu_data *pdu);
713 void llcp_pdu_decode_clock_accuracy_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
714 
715 #endif /* CONFIG_BT_CTLR_SCA_UPDATE */
716 
717 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
718 /*
719  * Constant Tone Request Procedure Helper
720  */
721 void llcp_pdu_encode_cte_req(struct proc_ctx *ctx, struct pdu_data *pdu);
722 void llcp_pdu_decode_cte_rsp(struct proc_ctx *ctx, const struct pdu_data *pdu);
723 void llcp_ntf_encode_cte_req(struct pdu_data *pdu);
724 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
725 
726 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
727 /*
728  * Constant Tone Response Procedure Helper
729  */
730 void llcp_pdu_decode_cte_req(struct proc_ctx *ctx, struct pdu_data *pdu);
731 void llcp_pdu_encode_cte_rsp(const struct proc_ctx *ctx, struct pdu_data *pdu);
732 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
733 
734 void llcp_lp_cc_init_proc(struct proc_ctx *ctx);
735 void llcp_lp_cc_offset_calc_reply(struct ll_conn *conn, struct proc_ctx *ctx);
736 void llcp_lp_cc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
737 void llcp_lp_cc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
738 bool llcp_lp_cc_is_active(struct proc_ctx *ctx);
739 bool llcp_lp_cc_awaiting_established(struct proc_ctx *ctx);
740 void llcp_lp_cc_established(struct ll_conn *conn, struct proc_ctx *ctx);
741 bool llcp_lp_cc_cancel(struct ll_conn *conn, struct proc_ctx *ctx);
742 
743 void llcp_rp_cc_init_proc(struct proc_ctx *ctx);
744 void llcp_rp_cc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx);
745 void llcp_rp_cc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param);
746 bool llcp_rp_cc_awaiting_reply(struct proc_ctx *ctx);
747 bool llcp_rp_cc_awaiting_established(struct proc_ctx *ctx);
748 void llcp_rp_cc_accept(struct ll_conn *conn, struct proc_ctx *ctx);
749 void llcp_rp_cc_reject(struct ll_conn *conn, struct proc_ctx *ctx);
750 bool llcp_rp_cc_awaiting_instant(struct proc_ctx *ctx);
751 void llcp_rp_cc_established(struct ll_conn *conn, struct proc_ctx *ctx);
752 
753 void llcp_pdu_decode_cis_req(struct proc_ctx *ctx, struct pdu_data *pdu);
754 void llcp_pdu_encode_cis_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
755 void llcp_pdu_decode_cis_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
756 void llcp_pdu_encode_cis_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
757 void llcp_pdu_decode_cis_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
758 void llcp_pdu_encode_cis_req(struct proc_ctx *ctx, struct pdu_data *pdu);
759 void llcp_pdu_encode_cis_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
760 void llcp_pdu_decode_cis_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
761 
762 #ifdef ZTEST_UNITTEST
763 bool llcp_lr_is_disconnected(struct ll_conn *conn);
764 bool llcp_lr_is_idle(struct ll_conn *conn);
765 struct proc_ctx *llcp_lr_dequeue(struct ll_conn *conn);
766 
767 bool llcp_rr_is_disconnected(struct ll_conn *conn);
768 bool llcp_rr_is_idle(struct ll_conn *conn);
769 struct proc_ctx *llcp_rr_dequeue(struct ll_conn *conn);
770 void llcp_rr_enqueue(struct ll_conn *conn, struct proc_ctx *ctx);
771 
772 uint16_t llcp_local_ctx_buffers_free(void);
773 uint16_t llcp_remote_ctx_buffers_free(void);
774 uint16_t llcp_ctx_buffers_free(void);
775 uint8_t llcp_common_tx_buffer_alloc_count(void);
776 struct proc_ctx *llcp_proc_ctx_acquire(void);
777 struct proc_ctx *llcp_create_procedure(enum llcp_proc proc);
778 #endif
779