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