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