1 /*
2  * Copyright (c) 2022 Demant
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/slist.h>
11 #include <zephyr/sys/util.h>
12 
13 #include <zephyr/bluetooth/hci_types.h>
14 
15 #include "hal/ecb.h"
16 #include "hal/ccm.h"
17 
18 #include "util/util.h"
19 #include "util/mem.h"
20 #include "util/memq.h"
21 #include "util/dbuf.h"
22 
23 #include "pdu_df.h"
24 #include "lll/pdu_vendor.h"
25 #include "pdu.h"
26 
27 #include "ll.h"
28 #include "ll_feat.h"
29 #include "ll_settings.h"
30 
31 #include "lll.h"
32 #include "lll/lll_df_types.h"
33 #include "lll_conn.h"
34 #include "lll_conn_iso.h"
35 
36 #include "ull_tx_queue.h"
37 
38 #include "isoal.h"
39 #include "ull_iso_types.h"
40 #include "ull_conn_types.h"
41 #include "ull_conn_iso_types.h"
42 #include "ull_internal.h"
43 #include "ull_llcp.h"
44 #include "ull_llcp_internal.h"
45 #include "ull_llcp_features.h"
46 #include "ull_conn_internal.h"
47 
48 #include "ull_iso_internal.h"
49 #include "ull_conn_iso_internal.h"
50 #include "ull_peripheral_iso_internal.h"
51 #include "ull_central_iso_internal.h"
52 
53 #include <soc.h>
54 #include "hal/debug.h"
55 
cc_ntf_established(struct ll_conn * conn,struct proc_ctx * ctx)56 static void cc_ntf_established(struct ll_conn *conn, struct proc_ctx *ctx)
57 {
58 	struct node_rx_conn_iso_estab *pdu;
59 	struct node_rx_pdu *ntf;
60 	uint8_t piggy_back;
61 
62 	/* Allocate ntf node */
63 	ntf = ctx->node_ref.rx;
64 	LL_ASSERT(ntf);
65 	ctx->node_ref.rx = NULL;
66 
67 	piggy_back = (ntf->hdr.type != NODE_RX_TYPE_RETAIN);
68 
69 	ntf->hdr.type = NODE_RX_TYPE_CIS_ESTABLISHED;
70 	ntf->hdr.handle = conn->lll.handle;
71 	ntf->rx_ftr.param = ll_conn_iso_stream_get(ctx->data.cis_create.cis_handle);
72 
73 	pdu = (struct node_rx_conn_iso_estab *)ntf->pdu;
74 
75 	pdu->cis_handle = ctx->data.cis_create.cis_handle;
76 	pdu->status = ctx->data.cis_create.error;
77 
78 	if (!piggy_back) {
79 		/* Enqueue notification towards LL */
80 		ll_rx_put_sched(ntf->hdr.link, ntf);
81 	}
82 }
83 
84 #if defined(CONFIG_BT_PERIPHERAL)
85 /* LLCP Remote Procedure FSM states */
86 enum {
87 	/* Establish Procedure */
88 	RP_CC_STATE_IDLE = LLCP_STATE_IDLE,
89 	RP_CC_STATE_WAIT_RX_CIS_REQ,
90 	RP_CC_STATE_WAIT_REPLY,
91 	RP_CC_STATE_WAIT_TX_CIS_RSP,
92 	RP_CC_STATE_WAIT_TX_REJECT_IND,
93 	RP_CC_STATE_WAIT_RX_CIS_IND,
94 	RP_CC_STATE_WAIT_INSTANT,
95 	RP_CC_STATE_WAIT_CIS_ESTABLISHED,
96 	RP_CC_STATE_WAIT_NTF_AVAIL,
97 };
98 
99 /* LLCP Remote Procedure FSM events */
100 enum {
101 	/* Procedure prepared */
102 	RP_CC_EVT_RUN,
103 
104 	/* Request received */
105 	RP_CC_EVT_CIS_REQ,
106 
107 	/* Response received */
108 	RP_CC_EVT_CIS_RSP,
109 
110 	/* Indication received */
111 	RP_CC_EVT_CIS_IND,
112 
113 	/* Create request accept reply */
114 	RP_CC_EVT_CIS_REQ_ACCEPT,
115 
116 	/* Create request decline reply */
117 	RP_CC_EVT_CIS_REQ_REJECT,
118 
119 	/* Reject response received */
120 	RP_CC_EVT_REJECT,
121 
122 	/* Established */
123 	RP_CC_EVT_CIS_ESTABLISHED,
124 
125 	/* Unknown response received */
126 	RP_CC_EVT_UNKNOWN,
127 };
128 
129 static void rp_cc_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
130 				void *param);
131 
132 /*
133  * LLCP Remote Procedure FSM
134  */
135 
llcp_rp_cc_tx_rsp(struct ll_conn * conn,struct proc_ctx * ctx)136 static void llcp_rp_cc_tx_rsp(struct ll_conn *conn, struct proc_ctx *ctx)
137 {
138 	uint16_t delay_conn_events;
139 	uint16_t conn_event_count;
140 	struct pdu_data *pdu;
141 	struct node_tx *tx;
142 
143 	/* Allocate tx node */
144 	tx = llcp_tx_alloc(conn, ctx);
145 	LL_ASSERT(tx);
146 
147 	pdu = (struct pdu_data *)tx->pdu;
148 	conn_event_count = ctx->data.cis_create.conn_event_count;
149 
150 	/* Postpone if instant is in this or next connection event. This would handle obsolete value
151 	 * due to retransmission, as well as incorrect behavior by central.
152 	 * We need at least 2 connection events to get ready. First for receiving the indication,
153 	 * the second for setting up the CIS.
154 	 */
155 	ctx->data.cis_create.conn_event_count = MAX(ctx->data.cis_create.conn_event_count,
156 						    ull_conn_event_counter(conn) + 2U);
157 
158 	delay_conn_events = ctx->data.cis_create.conn_event_count - conn_event_count;
159 
160 	/* If instant is postponed, calculate the offset to add to CIS_Offset_Min and
161 	 * CIS_Offset_Max.
162 	 *
163 	 * BT Core v5.3, Vol 6, Part B, section 5.1.15:
164 	 * Two windows are equivalent if they have the same width and the difference between their
165 	 * start times is an integer multiple of ISO_Interval for the CIS.
166 	 *
167 	 * The offset shall compensate for the relation between ISO- and connection interval. The
168 	 * offset translates to what is additionally needed to move the window up to an integer
169 	 * number of ISO intervals.
170 	 */
171 	if (delay_conn_events) {
172 		uint32_t conn_interval_us = conn->lll.interval * CONN_INT_UNIT_US;
173 		uint32_t iso_interval_us  = ctx->data.cis_create.iso_interval * ISO_INT_UNIT_US;
174 		uint8_t  iso_intervals;
175 		uint32_t offset_us;
176 
177 		iso_intervals = DIV_ROUND_UP(delay_conn_events * conn_interval_us,
178 					     iso_interval_us);
179 		offset_us = (iso_intervals * iso_interval_us) -
180 			    (delay_conn_events * conn_interval_us);
181 
182 		ctx->data.cis_create.cis_offset_min += offset_us;
183 		ctx->data.cis_create.cis_offset_max += offset_us;
184 	}
185 
186 	llcp_pdu_encode_cis_rsp(ctx, pdu);
187 	ctx->tx_opcode = pdu->llctrl.opcode;
188 
189 	/* Enqueue LL Control PDU towards LLL */
190 	llcp_tx_enqueue(conn, tx);
191 }
192 
llcp_rp_cc_tx_reject(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t opcode)193 static void llcp_rp_cc_tx_reject(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t opcode)
194 {
195 	struct node_tx *tx;
196 	struct pdu_data *pdu;
197 
198 	/* Allocate tx node */
199 	tx = ctx->node_ref.tx;
200 	LL_ASSERT(tx);
201 	ctx->node_ref.tx = NULL;
202 
203 	pdu = (struct pdu_data *)tx->pdu;
204 
205 	/* Encode LL Control PDU */
206 	llcp_pdu_encode_reject_ext_ind(pdu, opcode, ctx->data.cis_create.error);
207 	ctx->tx_opcode = pdu->llctrl.opcode;
208 
209 	/* Enqueue LL Control PDU towards LLL */
210 	llcp_tx_enqueue(conn, tx);
211 }
212 
rp_cc_ntf_create(struct ll_conn * conn,struct proc_ctx * ctx)213 static void rp_cc_ntf_create(struct ll_conn *conn, struct proc_ctx *ctx)
214 {
215 	struct node_rx_pdu *ntf;
216 	struct node_rx_conn_iso_req *pdu;
217 
218 	ntf = ctx->node_ref.rx;
219 	ctx->node_ref.rx = NULL;
220 	LL_ASSERT(ntf);
221 
222 	ntf->hdr.type = NODE_RX_TYPE_CIS_REQUEST;
223 	ntf->hdr.handle = conn->lll.handle;
224 	pdu = (struct node_rx_conn_iso_req *)ntf->pdu;
225 
226 	pdu->cig_id = ctx->data.cis_create.cig_id;
227 	pdu->cis_id = ctx->data.cis_create.cis_id;
228 	pdu->cis_handle = ctx->data.cis_create.cis_handle;
229 
230 	ctx->data.cis_create.host_request_to = 0U;
231 }
232 
rp_cc_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)233 static void rp_cc_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
234 {
235 	cc_ntf_established(conn, ctx);
236 	llcp_rr_complete(conn);
237 	ctx->state = RP_CC_STATE_IDLE;
238 }
239 
rp_cc_send_cis_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)240 static void rp_cc_send_cis_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
241 			       void *param)
242 {
243 	if (llcp_rr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
244 		ctx->state = RP_CC_STATE_WAIT_TX_CIS_RSP;
245 	} else {
246 		llcp_rp_cc_tx_rsp(conn, ctx);
247 
248 		/* Wait for the LL_CIS_IND */
249 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_CIS_IND;
250 		ctx->state = RP_CC_STATE_WAIT_RX_CIS_IND;
251 	}
252 }
253 
rp_cc_send_reject_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)254 static void rp_cc_send_reject_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
255 				  void *param)
256 {
257 	if (llcp_rr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
258 		ctx->state = RP_CC_STATE_WAIT_TX_REJECT_IND;
259 	} else {
260 		/* Allocate TX node to use, store in case we need to wait for NTF node */
261 		ctx->node_ref.tx = llcp_tx_alloc(conn, ctx);
262 		if (ctx->data.cis_create.error == BT_HCI_ERR_CONN_ACCEPT_TIMEOUT) {
263 			/* We complete with error, so we must generate NTF, thus we must make sure
264 			 * we have a node to use for NTF before TX'ing
265 			 */
266 			if (!llcp_ntf_alloc_is_available()) {
267 				ctx->state = RP_CC_STATE_WAIT_NTF_AVAIL;
268 				return;
269 			}
270 			ctx->node_ref.rx = llcp_ntf_alloc();
271 
272 			/* Mark node as RETAIN to trigger put/sched */
273 			ctx->node_ref.rx->hdr.type = NODE_RX_TYPE_RETAIN;
274 		}
275 
276 		llcp_rp_cc_tx_reject(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
277 
278 		if (ctx->data.cis_create.error == BT_HCI_ERR_CONN_ACCEPT_TIMEOUT) {
279 			/* We reject due to an accept timeout, so we should generate NTF */
280 			rp_cc_complete(conn, ctx, evt, param);
281 		} else {
282 			/* Otherwise we quietly complete the procedure */
283 			llcp_rr_complete(conn);
284 			ctx->state = RP_CC_STATE_IDLE;
285 		}
286 	}
287 }
288 
rp_cc_state_idle(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)289 static void rp_cc_state_idle(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
290 			     void *param)
291 {
292 	switch (evt) {
293 	case RP_CC_EVT_RUN:
294 		ctx->state = RP_CC_STATE_WAIT_RX_CIS_REQ;
295 		break;
296 	default:
297 		/* Ignore other evts */
298 		break;
299 	}
300 }
301 
rp_cc_check_phy(struct ll_conn * conn,struct proc_ctx * ctx,struct pdu_data * pdu)302 static uint8_t rp_cc_check_phy(struct ll_conn *conn, struct proc_ctx *ctx,
303 					    struct pdu_data *pdu)
304 {
305 	if (!phy_valid(pdu->llctrl.cis_req.c_phy) ||
306 	    !phy_valid(pdu->llctrl.cis_req.p_phy)) {
307 		/* zero, more than one or any rfu bit selected in either phy */
308 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
309 	}
310 
311 #if defined(CONFIG_BT_CTLR_PHY)
312 	const uint8_t phys = pdu->llctrl.cis_req.p_phy | pdu->llctrl.cis_req.c_phy;
313 
314 	if (((phys & PHY_2M) && !feature_phy_2m(conn)) ||
315 	    ((phys & PHY_CODED) && !feature_phy_coded(conn))) {
316 		/* Unsupported phy selected */
317 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
318 	}
319 #endif /* CONFIG_BT_CTLR_PHY */
320 
321 	return BT_HCI_ERR_SUCCESS;
322 }
323 
rp_cc_state_wait_rx_cis_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)324 static void rp_cc_state_wait_rx_cis_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
325 					void *param)
326 {
327 	struct pdu_data *pdu = (struct pdu_data *)param;
328 
329 	switch (evt) {
330 	case RP_CC_EVT_CIS_REQ:
331 		/* Handle CIS request */
332 		llcp_pdu_decode_cis_req(ctx, pdu);
333 
334 		/* Check PHY */
335 		ctx->data.cis_create.error = rp_cc_check_phy(conn, ctx, pdu);
336 
337 		if (ctx->data.cis_create.error == BT_HCI_ERR_SUCCESS) {
338 			ctx->data.cis_create.error =
339 				ull_peripheral_iso_acquire(conn, &pdu->llctrl.cis_req,
340 							   &ctx->data.cis_create.cis_handle);
341 		}
342 
343 		if (ctx->data.cis_create.error == BT_HCI_ERR_SUCCESS) {
344 			/* Now controller accepts, so go ask the host to accept or decline */
345 			rp_cc_ntf_create(conn, ctx);
346 			ctx->state = RP_CC_STATE_WAIT_REPLY;
347 		} else {
348 			/* Now controller rejects, right out */
349 			rp_cc_send_reject_ind(conn, ctx, evt, param);
350 		}
351 		break;
352 	default:
353 		/* Ignore other evts */
354 		break;
355 	}
356 }
357 
rp_cc_state_wait_tx_cis_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)358 static void rp_cc_state_wait_tx_cis_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
359 					void *param)
360 {
361 	switch (evt) {
362 	case RP_CC_EVT_RUN:
363 		rp_cc_send_cis_rsp(conn, ctx, evt, param);
364 		break;
365 	default:
366 		/* Ignore other evts */
367 		break;
368 	}
369 }
370 
rp_cc_state_wait_tx_reject_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)371 static void rp_cc_state_wait_tx_reject_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
372 					 void *param)
373 {
374 	switch (evt) {
375 	case RP_CC_EVT_RUN:
376 		rp_cc_send_reject_ind(conn, ctx, evt, param);
377 		break;
378 	default:
379 		/* Ignore other evts */
380 		break;
381 	}
382 }
383 
rp_cc_state_wait_rx_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)384 static void rp_cc_state_wait_rx_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
385 					void *param)
386 {
387 	struct pdu_data *pdu = (struct pdu_data *)param;
388 
389 	switch (evt) {
390 	case RP_CC_EVT_CIS_IND:
391 		llcp_pdu_decode_cis_ind(ctx, pdu);
392 		if (!ull_peripheral_iso_setup(&pdu->llctrl.cis_ind, ctx->data.cis_create.cig_id,
393 					      ctx->data.cis_create.cis_handle,
394 					      &ctx->data.cis_create.conn_event_count)) {
395 
396 			/* CIS has been setup, go wait for 'instant' before starting */
397 			ctx->state = RP_CC_STATE_WAIT_INSTANT;
398 
399 			/* Mark node as RETAIN to keep until we need for NTF */
400 			llcp_rx_node_retain(ctx);
401 
402 			/* Check if this connection event is where we need to start the CIS */
403 			rp_cc_check_instant(conn, ctx, evt, param);
404 			break;
405 		}
406 		/* If we get to here the CIG_ID referred in req/acquire has become void/invalid */
407 		/* This cannot happen unless the universe has started to deflate */
408 		LL_ASSERT(0);
409 	case RP_CC_EVT_REJECT:
410 		/* Handle CIS creation rejection */
411 		break;
412 
413 	default:
414 		/* Ignore other evts */
415 		break;
416 	}
417 }
418 
rp_cc_state_wait_ntf_avail(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)419 static void rp_cc_state_wait_ntf_avail(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
420 				 void *param)
421 {
422 	switch (evt) {
423 	case RP_CC_EVT_RUN:
424 		if (llcp_ntf_alloc_is_available()) {
425 			ctx->node_ref.rx = llcp_ntf_alloc();
426 			/* Mark node as RETAIN to trigger put/sched */
427 			ctx->node_ref.rx->hdr.type = NODE_RX_TYPE_RETAIN;
428 
429 			/* Now we're good to TX reject and complete procedure*/
430 			llcp_rp_cc_tx_reject(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
431 			rp_cc_complete(conn, ctx, evt, param);
432 		}
433 		break;
434 	default:
435 		/* Ignore other evts */
436 		break;
437 	}
438 }
439 
440 
rp_cc_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)441 static void rp_cc_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
442 				void *param)
443 {
444 	uint16_t start_event_count;
445 	uint16_t event_counter;
446 
447 	event_counter = ull_conn_event_counter(conn);
448 	start_event_count = ctx->data.cis_create.conn_event_count;
449 
450 	if (is_instant_reached_or_passed(start_event_count, event_counter)) {
451 		uint16_t instant_latency = (event_counter - start_event_count) & 0xffff;
452 
453 		/* Start CIS */
454 		ull_conn_iso_start(conn, ctx->data.cis_create.cis_handle,
455 				   conn->llcp.prep.ticks_at_expire,
456 				   conn->llcp.prep.remainder,
457 				   instant_latency);
458 
459 		/* Now we can wait for CIS to become established */
460 		ctx->state = RP_CC_STATE_WAIT_CIS_ESTABLISHED;
461 	}
462 }
463 
rp_cc_state_wait_reply(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)464 static void rp_cc_state_wait_reply(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
465 				   void *param)
466 {
467 
468 	switch (evt) {
469 	case RP_CC_EVT_CIS_REQ_ACCEPT:
470 		/* Continue procedure in next prepare run */
471 		ctx->state = RP_CC_STATE_WAIT_TX_CIS_RSP;
472 		break;
473 	case RP_CC_EVT_RUN:
474 		/* Update 'time' and check for timeout on the Reply */
475 		ctx->data.cis_create.host_request_to += (conn->lll.interval * CONN_INT_UNIT_US);
476 		if (ctx->data.cis_create.host_request_to < conn->connect_accept_to) {
477 			break;
478 		}
479 		/* Reject 'reason/error' */
480 		ctx->data.cis_create.error = BT_HCI_ERR_CONN_ACCEPT_TIMEOUT;
481 		/* If timeout is hit, fall through and reject */
482 	case RP_CC_EVT_CIS_REQ_REJECT:
483 		/* CIS Request is rejected, so clean up CIG/CIS acquisitions */
484 		ull_peripheral_iso_release(ctx->data.cis_create.cis_handle);
485 		/* Continue procedure in next prepare run */
486 		ctx->state = RP_CC_STATE_WAIT_TX_REJECT_IND;
487 		break;
488 	default:
489 		/* Ignore other evts */
490 		break;
491 	}
492 }
493 
494 
rp_cc_state_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)495 static void rp_cc_state_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
496 				     void *param)
497 {
498 	switch (evt) {
499 	case RP_CC_EVT_RUN:
500 		rp_cc_check_instant(conn, ctx, evt, param);
501 		break;
502 	default:
503 		/* Ignore other evts */
504 		break;
505 	}
506 }
507 
508 
rp_cc_state_wait_cis_established(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)509 static void rp_cc_state_wait_cis_established(struct ll_conn *conn, struct proc_ctx *ctx,
510 					     uint8_t evt, void *param)
511 {
512 	switch (evt) {
513 	case RP_CC_EVT_CIS_ESTABLISHED:
514 		rp_cc_complete(conn, ctx, evt, param);
515 		break;
516 	default:
517 		/* Ignore other evts */
518 		break;
519 	}
520 }
521 
522 
rp_cc_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)523 static void rp_cc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
524 {
525 	switch (ctx->state) {
526 	/* Create Procedure */
527 	case RP_CC_STATE_IDLE:
528 		rp_cc_state_idle(conn, ctx, evt, param);
529 		break;
530 	case RP_CC_STATE_WAIT_RX_CIS_REQ:
531 		rp_cc_state_wait_rx_cis_req(conn, ctx, evt, param);
532 		break;
533 	case RP_CC_STATE_WAIT_TX_REJECT_IND:
534 		rp_cc_state_wait_tx_reject_ind(conn, ctx, evt, param);
535 		break;
536 	case RP_CC_STATE_WAIT_TX_CIS_RSP:
537 		rp_cc_state_wait_tx_cis_rsp(conn, ctx, evt, param);
538 		break;
539 	case RP_CC_STATE_WAIT_REPLY:
540 		rp_cc_state_wait_reply(conn, ctx, evt, param);
541 		break;
542 	case RP_CC_STATE_WAIT_RX_CIS_IND:
543 		rp_cc_state_wait_rx_cis_ind(conn, ctx, evt, param);
544 		break;
545 	case RP_CC_STATE_WAIT_INSTANT:
546 		rp_cc_state_wait_instant(conn, ctx, evt, param);
547 		break;
548 	case RP_CC_STATE_WAIT_CIS_ESTABLISHED:
549 		rp_cc_state_wait_cis_established(conn, ctx, evt, param);
550 		break;
551 	case RP_CC_STATE_WAIT_NTF_AVAIL:
552 		rp_cc_state_wait_ntf_avail(conn, ctx, evt, param);
553 		break;
554 	default:
555 		/* Unknown state */
556 		LL_ASSERT(0);
557 	}
558 }
559 
llcp_rp_cc_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)560 void llcp_rp_cc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
561 {
562 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
563 
564 	switch (pdu->llctrl.opcode) {
565 	case PDU_DATA_LLCTRL_TYPE_CIS_REQ:
566 		rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_REQ, pdu);
567 		break;
568 	case PDU_DATA_LLCTRL_TYPE_CIS_IND:
569 		rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_IND, pdu);
570 		break;
571 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
572 	case PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND:
573 		rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_REJECT, pdu);
574 		break;
575 	default:
576 		/* Invalid behaviour */
577 		/* Invalid PDU received so terminate connection */
578 		conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
579 		llcp_rr_complete(conn);
580 		ctx->state = RP_CC_STATE_IDLE;
581 		break;
582 	}
583 }
584 
llcp_rp_cc_awaiting_reply(struct proc_ctx * ctx)585 bool llcp_rp_cc_awaiting_reply(struct proc_ctx *ctx)
586 {
587 	return (ctx->state == RP_CC_STATE_WAIT_REPLY);
588 }
589 
llcp_rp_cc_awaiting_established(struct proc_ctx * ctx)590 bool llcp_rp_cc_awaiting_established(struct proc_ctx *ctx)
591 {
592 	return (ctx->state == RP_CC_STATE_WAIT_CIS_ESTABLISHED);
593 }
594 
llcp_rp_cc_accept(struct ll_conn * conn,struct proc_ctx * ctx)595 void llcp_rp_cc_accept(struct ll_conn *conn, struct proc_ctx *ctx)
596 {
597 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_REQ_ACCEPT, NULL);
598 }
599 
llcp_rp_cc_reject(struct ll_conn * conn,struct proc_ctx * ctx)600 void llcp_rp_cc_reject(struct ll_conn *conn, struct proc_ctx *ctx)
601 {
602 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_REQ_REJECT, NULL);
603 }
604 
llcp_rp_cc_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)605 void llcp_rp_cc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
606 {
607 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_RUN, param);
608 }
609 
llcp_rp_cc_awaiting_instant(struct proc_ctx * ctx)610 bool llcp_rp_cc_awaiting_instant(struct proc_ctx *ctx)
611 {
612 	return (ctx->state == RP_CC_STATE_WAIT_INSTANT);
613 }
614 
llcp_rp_cc_established(struct ll_conn * conn,struct proc_ctx * ctx)615 void llcp_rp_cc_established(struct ll_conn *conn, struct proc_ctx *ctx)
616 {
617 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_ESTABLISHED, NULL);
618 }
619 #endif /* CONFIG_BT_PERIPHERAL */
620 
621 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
622 static void lp_cc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param);
623 
624 /* LLCP Local Procedure FSM states */
625 enum {
626 	LP_CC_STATE_IDLE = LLCP_STATE_IDLE,
627 	LP_CC_STATE_WAIT_NTF_AVAIL,
628 	LP_CC_STATE_WAIT_OFFSET_CALC,
629 	LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ,
630 	LP_CC_STATE_WAIT_TX_CIS_REQ,
631 	LP_CC_STATE_WAIT_RX_CIS_RSP,
632 	LP_CC_STATE_WAIT_NOTIFY_CANCEL,
633 	LP_CC_STATE_WAIT_RX_CIS_RSP_CANCEL,
634 	LP_CC_STATE_WAIT_TX_CIS_IND,
635 	LP_CC_STATE_WAIT_INSTANT,
636 	LP_CC_STATE_WAIT_ESTABLISHED,
637 };
638 
639 /* LLCP Local Procedure CIS Creation FSM events */
640 enum {
641 	/* Procedure run */
642 	LP_CC_EVT_RUN,
643 
644 	/* Offset calculation reply received */
645 	LP_CC_EVT_OFFSET_CALC_REPLY,
646 
647 	/* Response received */
648 	LP_CC_EVT_CIS_RSP,
649 
650 	/* Reject response received */
651 	LP_CC_EVT_REJECT,
652 
653 	/* CIS established */
654 	LP_CC_EVT_ESTABLISHED,
655 
656 	/* Unknown response received */
657 	LP_CC_EVT_UNKNOWN,
658 };
659 
lp_cc_tx(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t opcode)660 static void lp_cc_tx(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t opcode)
661 {
662 	struct node_tx *tx;
663 	struct pdu_data *pdu;
664 
665 	/* Allocate tx node */
666 	tx = llcp_tx_alloc(conn, ctx);
667 	LL_ASSERT(tx);
668 
669 	pdu = (struct pdu_data *)tx->pdu;
670 
671 	/* Encode LL Control PDU */
672 	switch (opcode) {
673 	case PDU_DATA_LLCTRL_TYPE_CIS_REQ:
674 		llcp_pdu_encode_cis_req(ctx, pdu);
675 		break;
676 	case PDU_DATA_LLCTRL_TYPE_CIS_IND:
677 		llcp_pdu_encode_cis_ind(ctx, pdu);
678 		break;
679 	default:
680 		/* Unknown opcode */
681 		LL_ASSERT(0);
682 		break;
683 	}
684 
685 	ctx->tx_opcode = pdu->llctrl.opcode;
686 
687 	/* Enqueue LL Control PDU towards LLL */
688 	llcp_tx_enqueue(conn, tx);
689 }
690 
llcp_lp_cc_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)691 void llcp_lp_cc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
692 {
693 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
694 
695 	switch (pdu->llctrl.opcode) {
696 	case PDU_DATA_LLCTRL_TYPE_CIS_RSP:
697 		lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_CIS_RSP, pdu);
698 		break;
699 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
700 	case PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND:
701 		lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_REJECT, pdu);
702 		break;
703 	default:
704 		/* Invalid behaviour */
705 		/* Invalid PDU received so terminate connection */
706 		conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
707 		llcp_lr_complete(conn);
708 		ctx->state = LP_CC_STATE_IDLE;
709 		break;
710 	}
711 }
712 
llcp_lp_cc_offset_calc_reply(struct ll_conn * conn,struct proc_ctx * ctx)713 void llcp_lp_cc_offset_calc_reply(struct ll_conn *conn, struct proc_ctx *ctx)
714 {
715 	lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_OFFSET_CALC_REPLY, NULL);
716 }
717 
lp_cc_offset_calc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)718 static void lp_cc_offset_calc_req(struct ll_conn *conn, struct proc_ctx *ctx,
719 				  uint8_t evt, void *param)
720 {
721 	if (llcp_lr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
722 		ctx->state = LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ;
723 	} else {
724 		int err;
725 
726 		/* Update conn_event_count */
727 		err = ull_central_iso_cis_offset_get(ctx->data.cis_create.cis_handle,
728 						     &ctx->data.cis_create.cis_offset_min,
729 						     &ctx->data.cis_create.cis_offset_max,
730 						     &ctx->data.cis_create.conn_event_count);
731 		if (err) {
732 			ctx->state = LP_CC_STATE_WAIT_OFFSET_CALC;
733 
734 			return;
735 		}
736 
737 		lp_cc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
738 
739 		ctx->state = LP_CC_STATE_WAIT_RX_CIS_RSP;
740 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_CIS_RSP;
741 	}
742 }
743 
lp_cc_st_wait_offset_calc_tx_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)744 static void lp_cc_st_wait_offset_calc_tx_req(struct ll_conn *conn,
745 					     struct proc_ctx *ctx,
746 					     uint8_t evt, void *param)
747 {
748 	switch (evt) {
749 	case LP_CC_EVT_RUN:
750 		lp_cc_offset_calc_req(conn, ctx, evt, param);
751 		break;
752 	default:
753 		/* Ignore other evts */
754 		break;
755 	}
756 }
757 
lp_cc_st_wait_offset_calc(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)758 static void lp_cc_st_wait_offset_calc(struct ll_conn *conn,
759 				      struct proc_ctx *ctx,
760 				      uint8_t evt, void *param)
761 {
762 	switch (evt) {
763 	case LP_CC_EVT_RUN:
764 		/* TODO: May be have a timeout calculating the CIS offset?
765 		 *       otherwise, ignore
766 		 */
767 		break;
768 	case LP_CC_EVT_OFFSET_CALC_REPLY:
769 		ctx->state = LP_CC_STATE_WAIT_TX_CIS_REQ;
770 		break;
771 	default:
772 		/* Ignore other evts */
773 		break;
774 	}
775 }
776 
lp_cc_send_cis_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)777 static void lp_cc_send_cis_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
778 			       void *param)
779 {
780 	if (llcp_lr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
781 		ctx->state = LP_CC_STATE_WAIT_TX_CIS_REQ;
782 	} else {
783 		lp_cc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
784 
785 		ctx->state = LP_CC_STATE_WAIT_RX_CIS_RSP;
786 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_CIS_RSP;
787 	}
788 }
789 
lp_cc_st_wait_tx_cis_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)790 static void lp_cc_st_wait_tx_cis_req(struct ll_conn *conn, struct proc_ctx *ctx,
791 				     uint8_t evt, void *param)
792 {
793 	switch (evt) {
794 	case LP_CC_EVT_RUN:
795 		lp_cc_send_cis_req(conn, ctx, evt, param);
796 		break;
797 	default:
798 		/* Ignore other evts */
799 		break;
800 	}
801 }
802 
lp_cc_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)803 static void lp_cc_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
804 {
805 	cc_ntf_established(conn, ctx);
806 	llcp_lr_complete(conn);
807 	ctx->state = LP_CC_STATE_IDLE;
808 }
809 
lp_cc_st_idle(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)810 static void lp_cc_st_idle(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
811 {
812 	switch (evt) {
813 	case LP_CC_EVT_RUN:
814 		switch (ctx->proc) {
815 		case PROC_CIS_CREATE:
816 			/* In case feature exchange completed after CIS create was enqueued
817 			 * peer CIS peripheral support should be confirmed
818 			 */
819 			if (feature_peer_iso_peripheral(conn)) {
820 				lp_cc_offset_calc_req(conn, ctx, evt, param);
821 			} else {
822 				/* Peer doesn't support CIS Peripheral so report unsupported */
823 				ctx->data.cis_create.error = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE;
824 				ctx->state = LP_CC_STATE_WAIT_NTF_AVAIL;
825 			}
826 			break;
827 		default:
828 			/* Unknown procedure */
829 			LL_ASSERT(0);
830 			break;
831 		}
832 		break;
833 	default:
834 		/* Ignore other evts */
835 		break;
836 	}
837 }
838 
lp_cc_state_wait_ntf_avail(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)839 static void lp_cc_state_wait_ntf_avail(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
840 				 void *param)
841 {
842 	switch (evt) {
843 	case LP_CC_EVT_RUN:
844 		if (llcp_ntf_alloc_is_available()) {
845 			ctx->node_ref.rx = llcp_ntf_alloc();
846 			/* Mark node as RETAIN to trigger put/sched */
847 			ctx->node_ref.rx->hdr.type = NODE_RX_TYPE_RETAIN;
848 
849 			/* Now we're good to complete procedure*/
850 			lp_cc_complete(conn, ctx, evt, param);
851 		}
852 		break;
853 	default:
854 		/* Ignore other evts */
855 		break;
856 	}
857 }
858 
cc_prepare_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx)859 static void cc_prepare_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx)
860 {
861 	uint8_t err;
862 
863 	/* Setup central parameters based on CIS_RSP */
864 	err = ull_central_iso_setup(ctx->data.cis_create.cis_handle,
865 				    &ctx->data.cis_create.cig_sync_delay,
866 				    &ctx->data.cis_create.cis_sync_delay,
867 				    &ctx->data.cis_create.cis_offset_min,
868 				    &ctx->data.cis_create.cis_offset_max,
869 				    &ctx->data.cis_create.conn_event_count,
870 				    ctx->data.cis_create.aa);
871 	LL_ASSERT(!err);
872 
873 	ctx->state = LP_CC_STATE_WAIT_INSTANT;
874 	ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_UNUSED;
875 }
876 
lp_cc_send_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)877 static void lp_cc_send_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
878 			       void *param)
879 {
880 	if (llcp_lr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
881 		ctx->state = LP_CC_STATE_WAIT_TX_CIS_IND;
882 	} else {
883 		cc_prepare_cis_ind(conn, ctx);
884 		lp_cc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_IND);
885 	}
886 }
887 
lp_cc_st_wait_rx_cis_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)888 static void lp_cc_st_wait_rx_cis_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
889 				     void *param)
890 {
891 	struct pdu_data *pdu = (struct pdu_data *)param;
892 
893 	switch (evt) {
894 	case LP_CC_EVT_CIS_RSP:
895 		/* TODO: Reject response if outside offset range? */
896 		llcp_pdu_decode_cis_rsp(ctx, param);
897 
898 		/* Mark RX node to NOT release */
899 		llcp_rx_node_retain(ctx);
900 
901 		lp_cc_send_cis_ind(conn, ctx, evt, param);
902 		break;
903 	case LP_CC_EVT_UNKNOWN:
904 		/* Unsupported in peer, so disable locally for this connection */
905 		feature_unmask_peer_features(conn, LL_FEAT_BIT_CIS_PERIPHERAL);
906 		ctx->data.cis_create.error = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE;
907 		lp_cc_complete(conn, ctx, evt, param);
908 		break;
909 	case LP_CC_EVT_REJECT:
910 		if (pdu->llctrl.reject_ext_ind.error_code == BT_HCI_ERR_UNSUPP_REMOTE_FEATURE) {
911 			/* Unsupported in peer, so disable locally for this connection */
912 			feature_unmask_peer_features(conn, LL_FEAT_BIT_CIS_PERIPHERAL);
913 		}
914 		ctx->data.cis_create.error = pdu->llctrl.reject_ext_ind.error_code;
915 		lp_cc_complete(conn, ctx, evt, param);
916 		break;
917 	default:
918 		/* Ignore other evts */
919 		break;
920 	}
921 }
922 
lp_cc_st_wait_notify_cancel(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)923 static void lp_cc_st_wait_notify_cancel(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
924 					void *param)
925 {
926 	switch (evt) {
927 	case LP_CC_EVT_RUN:
928 		if (llcp_ntf_alloc_is_available()) {
929 			ctx->node_ref.rx = llcp_ntf_alloc();
930 
931 			/* Mark node as RETAIN to trigger put/sched */
932 			ctx->node_ref.rx->hdr.type = NODE_RX_TYPE_RETAIN;
933 			ctx->state = LP_CC_STATE_WAIT_ESTABLISHED;
934 
935 			llcp_lp_cc_established(conn, ctx);
936 		}
937 		break;
938 	default:
939 		/* Ignore other evts */
940 		break;
941 	}
942 }
943 
lp_cc_st_wait_rx_cis_rsp_cancel(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)944 static void lp_cc_st_wait_rx_cis_rsp_cancel(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
945 					    void *param)
946 {
947 	struct pdu_data *pdu;
948 	struct node_tx *tx;
949 
950 	switch (evt) {
951 	case LP_CC_EVT_CIS_RSP:
952 		/* Allocate tx node */
953 		tx = llcp_tx_alloc(conn, ctx);
954 		LL_ASSERT(tx);
955 
956 		pdu = (struct pdu_data *)tx->pdu;
957 
958 		/* Encode LL Control PDU */
959 		llcp_pdu_encode_reject_ext_ind(pdu, PDU_DATA_LLCTRL_TYPE_CIS_RSP,
960 			ctx->data.cis_create.error);
961 
962 		/* Enqueue LL Control PDU towards LLL */
963 		llcp_tx_enqueue(conn, tx);
964 		lp_cc_complete(conn, ctx, evt, param);
965 		break;
966 	case LP_CC_EVT_UNKNOWN:
967 	case LP_CC_EVT_REJECT:
968 		lp_cc_complete(conn, ctx, evt, param);
969 		break;
970 	default:
971 		/* Ignore other evts */
972 		break;
973 	}
974 }
975 
lp_cc_st_wait_tx_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)976 static void lp_cc_st_wait_tx_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx,
977 				     uint8_t evt, void *param)
978 {
979 	switch (evt) {
980 	case LP_CC_EVT_RUN:
981 		lp_cc_send_cis_ind(conn, ctx, evt, param);
982 		break;
983 	default:
984 		/* Ignore other evts */
985 		break;
986 	}
987 }
988 
lp_cc_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)989 static void lp_cc_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
990 				void *param)
991 {
992 	uint16_t start_event_count;
993 	uint16_t instant_latency;
994 	uint16_t event_counter;
995 
996 	event_counter = ull_conn_event_counter(conn);
997 	start_event_count = ctx->data.cis_create.conn_event_count;
998 
999 	instant_latency = (event_counter - start_event_count) & 0xffff;
1000 	if (instant_latency <= 0x7fff) {
1001 		/* Start CIS */
1002 		ull_conn_iso_start(conn, ctx->data.cis_create.cis_handle,
1003 				   conn->llcp.prep.ticks_at_expire,
1004 				   conn->llcp.prep.remainder,
1005 				   instant_latency);
1006 
1007 		/* Now we can wait for CIS to become established */
1008 		ctx->state = LP_CC_STATE_WAIT_ESTABLISHED;
1009 	}
1010 }
1011 
lp_cc_st_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1012 static void lp_cc_st_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
1013 				  void *param)
1014 {
1015 	switch (evt) {
1016 	case LP_CC_EVT_RUN:
1017 		lp_cc_check_instant(conn, ctx, evt, param);
1018 		break;
1019 	default:
1020 		/* Ignore other evts */
1021 		break;
1022 	}
1023 }
1024 
lp_cc_st_wait_established(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1025 static void lp_cc_st_wait_established(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
1026 				      void *param)
1027 {
1028 	switch (evt) {
1029 	case LP_CC_EVT_ESTABLISHED:
1030 		/* CIS was established, so let's go ahead and complete procedure */
1031 		lp_cc_complete(conn, ctx, evt, param);
1032 		break;
1033 	default:
1034 		/* Ignore other evts */
1035 		break;
1036 	}
1037 }
1038 
lp_cc_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1039 static void lp_cc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
1040 {
1041 	switch (ctx->state) {
1042 	case LP_CC_STATE_IDLE:
1043 		lp_cc_st_idle(conn, ctx, evt, param);
1044 		break;
1045 	case LP_CC_STATE_WAIT_NTF_AVAIL:
1046 		lp_cc_state_wait_ntf_avail(conn, ctx, evt, param);
1047 		break;
1048 	case LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ:
1049 		lp_cc_st_wait_offset_calc_tx_req(conn, ctx, evt, param);
1050 		break;
1051 	case LP_CC_STATE_WAIT_OFFSET_CALC:
1052 		lp_cc_st_wait_offset_calc(conn, ctx, evt, param);
1053 		break;
1054 	case LP_CC_STATE_WAIT_TX_CIS_REQ:
1055 		lp_cc_st_wait_tx_cis_req(conn, ctx, evt, param);
1056 		break;
1057 	case LP_CC_STATE_WAIT_RX_CIS_RSP:
1058 		lp_cc_st_wait_rx_cis_rsp(conn, ctx, evt, param);
1059 		break;
1060 	case LP_CC_STATE_WAIT_NOTIFY_CANCEL:
1061 		lp_cc_st_wait_notify_cancel(conn, ctx, evt, param);
1062 		break;
1063 	case LP_CC_STATE_WAIT_RX_CIS_RSP_CANCEL:
1064 		lp_cc_st_wait_rx_cis_rsp_cancel(conn, ctx, evt, param);
1065 		break;
1066 	case LP_CC_STATE_WAIT_TX_CIS_IND:
1067 		lp_cc_st_wait_tx_cis_ind(conn, ctx, evt, param);
1068 		break;
1069 	case LP_CC_STATE_WAIT_INSTANT:
1070 		lp_cc_st_wait_instant(conn, ctx, evt, param);
1071 		break;
1072 	case LP_CC_STATE_WAIT_ESTABLISHED:
1073 		lp_cc_st_wait_established(conn, ctx, evt, param);
1074 		break;
1075 	default:
1076 		/* Unknown state */
1077 		LL_ASSERT(0);
1078 		break;
1079 	}
1080 }
1081 
llcp_lp_cc_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)1082 void llcp_lp_cc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
1083 {
1084 	lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_RUN, param);
1085 }
1086 
llcp_lp_cc_is_active(struct proc_ctx * ctx)1087 bool llcp_lp_cc_is_active(struct proc_ctx *ctx)
1088 {
1089 	return ctx->state != LP_CC_STATE_IDLE;
1090 }
1091 
llcp_lp_cc_awaiting_established(struct proc_ctx * ctx)1092 bool llcp_lp_cc_awaiting_established(struct proc_ctx *ctx)
1093 {
1094 	return (ctx->state == LP_CC_STATE_WAIT_ESTABLISHED);
1095 }
1096 
llcp_lp_cc_established(struct ll_conn * conn,struct proc_ctx * ctx)1097 void llcp_lp_cc_established(struct ll_conn *conn, struct proc_ctx *ctx)
1098 {
1099 	lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_ESTABLISHED, NULL);
1100 }
1101 
llcp_lp_cc_cancel(struct ll_conn * conn,struct proc_ctx * ctx)1102 bool llcp_lp_cc_cancel(struct ll_conn *conn, struct proc_ctx *ctx)
1103 {
1104 	ctx->data.cis_create.error = BT_HCI_ERR_OP_CANCELLED_BY_HOST;
1105 
1106 	switch (ctx->state) {
1107 	case LP_CC_STATE_IDLE:
1108 	case LP_CC_STATE_WAIT_OFFSET_CALC:
1109 	case LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ:
1110 	case LP_CC_STATE_WAIT_TX_CIS_REQ:
1111 		ctx->state = LP_CC_STATE_WAIT_NOTIFY_CANCEL;
1112 		return true;
1113 	case LP_CC_STATE_WAIT_RX_CIS_RSP:
1114 		ctx->state = LP_CC_STATE_WAIT_RX_CIS_RSP_CANCEL;
1115 		return true;
1116 	default:
1117 		break;
1118 	}
1119 
1120 	return false;
1121 }
1122 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
1123