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