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->hdr.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,
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 by an integer number
169 	 * of ISO intervals. I.e.:
170 	 *   offset = (delayed * CONN_interval) MOD ISO_interval
171 	 */
172 	if (delay_conn_events) {
173 		uint32_t conn_interval_us  = conn->lll.interval * CONN_INT_UNIT_US;
174 		uint32_t iso_interval_us   = ctx->data.cis_create.iso_interval * ISO_INT_UNIT_US;
175 		uint32_t offset_us = (delay_conn_events * conn_interval_us) % iso_interval_us;
176 
177 		ctx->data.cis_create.cis_offset_min += offset_us;
178 		ctx->data.cis_create.cis_offset_max += offset_us;
179 	}
180 
181 	llcp_pdu_encode_cis_rsp(ctx, pdu);
182 	ctx->tx_opcode = pdu->llctrl.opcode;
183 
184 	/* Enqueue LL Control PDU towards LLL */
185 	llcp_tx_enqueue(conn, tx);
186 }
187 
llcp_rp_cc_tx_reject(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t opcode)188 static void llcp_rp_cc_tx_reject(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t opcode)
189 {
190 	struct node_tx *tx;
191 	struct pdu_data *pdu;
192 
193 	/* Allocate tx node */
194 	tx = ctx->node_ref.tx;
195 	LL_ASSERT(tx);
196 	ctx->node_ref.tx = NULL;
197 
198 	pdu = (struct pdu_data *)tx->pdu;
199 
200 	/* Encode LL Control PDU */
201 	llcp_pdu_encode_reject_ext_ind(pdu, opcode, ctx->data.cis_create.error);
202 	ctx->tx_opcode = pdu->llctrl.opcode;
203 
204 	/* Enqueue LL Control PDU towards LLL */
205 	llcp_tx_enqueue(conn, tx);
206 }
207 
rp_cc_ntf_create(struct ll_conn * conn,struct proc_ctx * ctx)208 static void rp_cc_ntf_create(struct ll_conn *conn, struct proc_ctx *ctx)
209 {
210 	struct node_rx_pdu *ntf;
211 	struct node_rx_conn_iso_req *pdu;
212 
213 	ntf = ctx->node_ref.rx;
214 	ctx->node_ref.rx = NULL;
215 	LL_ASSERT(ntf);
216 
217 	ntf->hdr.type = NODE_RX_TYPE_CIS_REQUEST;
218 	ntf->hdr.handle = conn->lll.handle;
219 	pdu = (struct node_rx_conn_iso_req *)ntf->pdu;
220 
221 	pdu->cig_id = ctx->data.cis_create.cig_id;
222 	pdu->cis_id = ctx->data.cis_create.cis_id;
223 	pdu->cis_handle = ctx->data.cis_create.cis_handle;
224 
225 	ctx->data.cis_create.host_request_to = 0U;
226 }
227 
rp_cc_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)228 static void rp_cc_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
229 {
230 	cc_ntf_established(conn, ctx);
231 	llcp_rr_complete(conn);
232 	ctx->state = RP_CC_STATE_IDLE;
233 }
234 
rp_cc_send_cis_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)235 static void rp_cc_send_cis_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
236 			       void *param)
237 {
238 	if (llcp_rr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
239 		ctx->state = RP_CC_STATE_WAIT_TX_CIS_RSP;
240 	} else {
241 		llcp_rp_cc_tx_rsp(conn, ctx);
242 
243 		/* Wait for the LL_CIS_IND */
244 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_CIS_IND;
245 		ctx->state = RP_CC_STATE_WAIT_RX_CIS_IND;
246 	}
247 }
248 
rp_cc_send_reject_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)249 static void rp_cc_send_reject_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
250 				  void *param)
251 {
252 	if (llcp_rr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
253 		ctx->state = RP_CC_STATE_WAIT_TX_REJECT_IND;
254 	} else {
255 		/* Allocate TX node to use, store in case we need to wait for NTF node */
256 		ctx->node_ref.tx = llcp_tx_alloc(conn, ctx);
257 		if (ctx->data.cis_create.error == BT_HCI_ERR_CONN_ACCEPT_TIMEOUT) {
258 			/* We complete with error, so we must generate NTF, thus we must make sure
259 			 * we have a node to use for NTF before TX'ing
260 			 */
261 			if (!llcp_ntf_alloc_is_available()) {
262 				ctx->state = RP_CC_STATE_WAIT_NTF_AVAIL;
263 				return;
264 			}
265 			ctx->node_ref.rx = llcp_ntf_alloc();
266 
267 			/* Mark node as RETAIN to trigger put/sched */
268 			ctx->node_ref.rx->hdr.type = NODE_RX_TYPE_RETAIN;
269 		}
270 
271 		llcp_rp_cc_tx_reject(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
272 
273 		if (ctx->data.cis_create.error == BT_HCI_ERR_CONN_ACCEPT_TIMEOUT) {
274 			/* We reject due to an accept timeout, so we should generate NTF */
275 			rp_cc_complete(conn, ctx, evt, param);
276 		} else {
277 			/* Otherwise we quietly complete the procedure */
278 			llcp_rr_complete(conn);
279 			ctx->state = RP_CC_STATE_IDLE;
280 		}
281 	}
282 }
283 
rp_cc_state_idle(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)284 static void rp_cc_state_idle(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
285 			     void *param)
286 {
287 	switch (evt) {
288 	case RP_CC_EVT_RUN:
289 		ctx->state = RP_CC_STATE_WAIT_RX_CIS_REQ;
290 		break;
291 	default:
292 		/* Ignore other evts */
293 		break;
294 	}
295 }
296 
phy_valid(uint8_t phy)297 static inline bool phy_valid(uint8_t phy)
298 {
299 	/* This is equivalent to:
300 	 * exactly one bit set, and no bit set is rfu's
301 	 */
302 	return (phy == PHY_1M || phy == PHY_2M || phy == PHY_CODED);
303 }
304 
rp_cc_check_phy(struct ll_conn * conn,struct proc_ctx * ctx,struct pdu_data * pdu)305 static uint8_t rp_cc_check_phy(struct ll_conn *conn, struct proc_ctx *ctx,
306 					    struct pdu_data *pdu)
307 {
308 	if (!phy_valid(pdu->llctrl.cis_req.c_phy) ||
309 	    !phy_valid(pdu->llctrl.cis_req.p_phy)) {
310 		/* zero, more than one or any rfu bit selected in either phy */
311 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
312 	}
313 
314 #if defined(CONFIG_BT_CTLR_PHY)
315 	const uint8_t phys = pdu->llctrl.cis_req.p_phy | pdu->llctrl.cis_req.c_phy;
316 
317 	if (((phys & PHY_2M) && !feature_phy_2m(conn)) ||
318 	    ((phys & PHY_CODED) && !feature_phy_coded(conn))) {
319 		/* Unsupported phy selected */
320 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
321 	}
322 #endif /* CONFIG_BT_CTLR_PHY */
323 
324 	return BT_HCI_ERR_SUCCESS;
325 }
326 
rp_cc_state_wait_rx_cis_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)327 static void rp_cc_state_wait_rx_cis_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
328 					void *param)
329 {
330 	struct pdu_data *pdu = (struct pdu_data *)param;
331 
332 	switch (evt) {
333 	case RP_CC_EVT_CIS_REQ:
334 		/* Handle CIS request */
335 		llcp_pdu_decode_cis_req(ctx, pdu);
336 
337 		/* Check PHY */
338 		ctx->data.cis_create.error = rp_cc_check_phy(conn, ctx, pdu);
339 
340 		if (ctx->data.cis_create.error == BT_HCI_ERR_SUCCESS) {
341 			ctx->data.cis_create.error =
342 				ull_peripheral_iso_acquire(conn, &pdu->llctrl.cis_req,
343 							   &ctx->data.cis_create.cis_handle);
344 		}
345 
346 		if (ctx->data.cis_create.error == BT_HCI_ERR_SUCCESS) {
347 			/* Now controller accepts, so go ask the host to accept or decline */
348 			rp_cc_ntf_create(conn, ctx);
349 			ctx->state = RP_CC_STATE_WAIT_REPLY;
350 		} else {
351 			/* Now controller rejects, right out */
352 			rp_cc_send_reject_ind(conn, ctx, evt, param);
353 		}
354 		break;
355 	default:
356 		/* Ignore other evts */
357 		break;
358 	}
359 }
360 
rp_cc_state_wait_tx_cis_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)361 static void rp_cc_state_wait_tx_cis_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
362 					void *param)
363 {
364 	switch (evt) {
365 	case RP_CC_EVT_RUN:
366 		rp_cc_send_cis_rsp(conn, ctx, evt, param);
367 		break;
368 	default:
369 		/* Ignore other evts */
370 		break;
371 	}
372 }
373 
rp_cc_state_wait_tx_reject_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)374 static void rp_cc_state_wait_tx_reject_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
375 					 void *param)
376 {
377 	switch (evt) {
378 	case RP_CC_EVT_RUN:
379 		rp_cc_send_reject_ind(conn, ctx, evt, param);
380 		break;
381 	default:
382 		/* Ignore other evts */
383 		break;
384 	}
385 }
386 
rp_cc_state_wait_rx_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)387 static void rp_cc_state_wait_rx_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
388 					void *param)
389 {
390 	struct pdu_data *pdu = (struct pdu_data *)param;
391 
392 	switch (evt) {
393 	case RP_CC_EVT_CIS_IND:
394 		llcp_pdu_decode_cis_ind(ctx, pdu);
395 		if (!ull_peripheral_iso_setup(&pdu->llctrl.cis_ind, ctx->data.cis_create.cig_id,
396 					      ctx->data.cis_create.cis_handle,
397 					      &ctx->data.cis_create.conn_event_count)) {
398 
399 			/* CIS has been setup, go wait for 'instant' before starting */
400 			ctx->state = RP_CC_STATE_WAIT_INSTANT;
401 
402 			/* Mark node as RETAIN to keep until we need for NTF */
403 			llcp_rx_node_retain(ctx);
404 
405 			/* Check if this connection event is where we need to start the CIS */
406 			rp_cc_check_instant(conn, ctx, evt, param);
407 			break;
408 		}
409 		/* If we get to here the CIG_ID referred in req/acquire has become void/invalid */
410 		/* This cannot happen unless the universe has started to deflate */
411 		LL_ASSERT(0);
412 	case RP_CC_EVT_REJECT:
413 		/* Handle CIS creation rejection */
414 		break;
415 
416 	default:
417 		/* Ignore other evts */
418 		break;
419 	}
420 }
421 
rp_cc_state_wait_ntf_avail(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)422 static void rp_cc_state_wait_ntf_avail(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
423 				 void *param)
424 {
425 	switch (evt) {
426 	case RP_CC_EVT_RUN:
427 		if (llcp_ntf_alloc_is_available()) {
428 			ctx->node_ref.rx = llcp_ntf_alloc();
429 			/* Mark node as RETAIN to trigger put/sched */
430 			ctx->node_ref.rx->hdr.type = NODE_RX_TYPE_RETAIN;
431 
432 			/* Now we're good to TX reject and complete procedure*/
433 			llcp_rp_cc_tx_reject(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
434 			rp_cc_complete(conn, ctx, evt, param);
435 		}
436 		break;
437 	default:
438 		/* Ignore other evts */
439 		break;
440 	}
441 }
442 
443 
rp_cc_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)444 static void rp_cc_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
445 				void *param)
446 {
447 	uint16_t start_event_count;
448 	uint16_t event_counter;
449 
450 	event_counter = ull_conn_event_counter(conn);
451 	start_event_count = ctx->data.cis_create.conn_event_count;
452 
453 	if (is_instant_reached_or_passed(start_event_count, event_counter)) {
454 		uint16_t instant_latency = (event_counter - start_event_count) & 0xffff;
455 
456 		/* Start CIS */
457 		ull_conn_iso_start(conn, ctx->data.cis_create.cis_handle,
458 				   conn->llcp.prep.ticks_at_expire,
459 				   conn->llcp.prep.remainder,
460 				   instant_latency);
461 
462 		/* Now we can wait for CIS to become established */
463 		ctx->state = RP_CC_STATE_WAIT_CIS_ESTABLISHED;
464 	}
465 }
466 
rp_cc_state_wait_reply(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)467 static void rp_cc_state_wait_reply(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
468 				   void *param)
469 {
470 
471 	switch (evt) {
472 	case RP_CC_EVT_CIS_REQ_ACCEPT:
473 		/* Continue procedure in next prepare run */
474 		ctx->state = RP_CC_STATE_WAIT_TX_CIS_RSP;
475 		break;
476 	case RP_CC_EVT_RUN:
477 		/* Update 'time' and check for timeout on the Reply */
478 		ctx->data.cis_create.host_request_to += (conn->lll.interval * CONN_INT_UNIT_US);
479 		if (ctx->data.cis_create.host_request_to < conn->connect_accept_to) {
480 			break;
481 		}
482 		/* Reject 'reason/error' */
483 		ctx->data.cis_create.error = BT_HCI_ERR_CONN_ACCEPT_TIMEOUT;
484 		/* If timeout is hit, fall through and reject */
485 	case RP_CC_EVT_CIS_REQ_REJECT:
486 		/* CIS Request is rejected, so clean up CIG/CIS acquisitions */
487 		ull_peripheral_iso_release(ctx->data.cis_create.cis_handle);
488 		/* Continue procedure in next prepare run */
489 		ctx->state = RP_CC_STATE_WAIT_TX_REJECT_IND;
490 		break;
491 	default:
492 		/* Ignore other evts */
493 		break;
494 	}
495 }
496 
497 
rp_cc_state_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)498 static void rp_cc_state_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
499 				     void *param)
500 {
501 	switch (evt) {
502 	case RP_CC_EVT_RUN:
503 		rp_cc_check_instant(conn, ctx, evt, param);
504 		break;
505 	default:
506 		/* Ignore other evts */
507 		break;
508 	}
509 }
510 
511 
rp_cc_state_wait_cis_established(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)512 static void rp_cc_state_wait_cis_established(struct ll_conn *conn, struct proc_ctx *ctx,
513 					     uint8_t evt, void *param)
514 {
515 	switch (evt) {
516 	case RP_CC_EVT_CIS_ESTABLISHED:
517 		rp_cc_complete(conn, ctx, evt, param);
518 		break;
519 	default:
520 		/* Ignore other evts */
521 		break;
522 	}
523 }
524 
525 
rp_cc_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)526 static void rp_cc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
527 {
528 	switch (ctx->state) {
529 	/* Create Procedure */
530 	case RP_CC_STATE_IDLE:
531 		rp_cc_state_idle(conn, ctx, evt, param);
532 		break;
533 	case RP_CC_STATE_WAIT_RX_CIS_REQ:
534 		rp_cc_state_wait_rx_cis_req(conn, ctx, evt, param);
535 		break;
536 	case RP_CC_STATE_WAIT_TX_REJECT_IND:
537 		rp_cc_state_wait_tx_reject_ind(conn, ctx, evt, param);
538 		break;
539 	case RP_CC_STATE_WAIT_TX_CIS_RSP:
540 		rp_cc_state_wait_tx_cis_rsp(conn, ctx, evt, param);
541 		break;
542 	case RP_CC_STATE_WAIT_REPLY:
543 		rp_cc_state_wait_reply(conn, ctx, evt, param);
544 		break;
545 	case RP_CC_STATE_WAIT_RX_CIS_IND:
546 		rp_cc_state_wait_rx_cis_ind(conn, ctx, evt, param);
547 		break;
548 	case RP_CC_STATE_WAIT_INSTANT:
549 		rp_cc_state_wait_instant(conn, ctx, evt, param);
550 		break;
551 	case RP_CC_STATE_WAIT_CIS_ESTABLISHED:
552 		rp_cc_state_wait_cis_established(conn, ctx, evt, param);
553 		break;
554 	case RP_CC_STATE_WAIT_NTF_AVAIL:
555 		rp_cc_state_wait_ntf_avail(conn, ctx, evt, param);
556 		break;
557 	default:
558 		/* Unknown state */
559 		LL_ASSERT(0);
560 	}
561 }
562 
llcp_rp_cc_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)563 void llcp_rp_cc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
564 {
565 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
566 
567 	switch (pdu->llctrl.opcode) {
568 	case PDU_DATA_LLCTRL_TYPE_CIS_REQ:
569 		rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_REQ, pdu);
570 		break;
571 	case PDU_DATA_LLCTRL_TYPE_CIS_IND:
572 		rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_IND, pdu);
573 		break;
574 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
575 	case PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND:
576 		rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_REJECT, pdu);
577 		break;
578 	default:
579 		/* Invalid behaviour */
580 		/* Invalid PDU received so terminate connection */
581 		conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
582 		llcp_rr_complete(conn);
583 		ctx->state = RP_CC_STATE_IDLE;
584 		break;
585 	}
586 }
587 
llcp_rp_cc_init_proc(struct proc_ctx * ctx)588 void llcp_rp_cc_init_proc(struct proc_ctx *ctx)
589 {
590 	switch (ctx->proc) {
591 	case PROC_CIS_CREATE:
592 		ctx->state = RP_CC_STATE_IDLE;
593 		break;
594 	default:
595 		LL_ASSERT(0);
596 	}
597 }
598 
llcp_rp_cc_awaiting_reply(struct proc_ctx * ctx)599 bool llcp_rp_cc_awaiting_reply(struct proc_ctx *ctx)
600 {
601 	return (ctx->state == RP_CC_STATE_WAIT_REPLY);
602 }
603 
llcp_rp_cc_awaiting_established(struct proc_ctx * ctx)604 bool llcp_rp_cc_awaiting_established(struct proc_ctx *ctx)
605 {
606 	return (ctx->state == RP_CC_STATE_WAIT_CIS_ESTABLISHED);
607 }
608 
llcp_rp_cc_accept(struct ll_conn * conn,struct proc_ctx * ctx)609 void llcp_rp_cc_accept(struct ll_conn *conn, struct proc_ctx *ctx)
610 {
611 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_REQ_ACCEPT, NULL);
612 }
613 
llcp_rp_cc_reject(struct ll_conn * conn,struct proc_ctx * ctx)614 void llcp_rp_cc_reject(struct ll_conn *conn, struct proc_ctx *ctx)
615 {
616 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_REQ_REJECT, NULL);
617 }
618 
llcp_rp_cc_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)619 void llcp_rp_cc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
620 {
621 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_RUN, param);
622 }
623 
llcp_rp_cc_awaiting_instant(struct proc_ctx * ctx)624 bool llcp_rp_cc_awaiting_instant(struct proc_ctx *ctx)
625 {
626 	return (ctx->state == RP_CC_STATE_WAIT_INSTANT);
627 }
628 
llcp_rp_cc_established(struct ll_conn * conn,struct proc_ctx * ctx)629 void llcp_rp_cc_established(struct ll_conn *conn, struct proc_ctx *ctx)
630 {
631 	rp_cc_execute_fsm(conn, ctx, RP_CC_EVT_CIS_ESTABLISHED, NULL);
632 }
633 #endif /* CONFIG_BT_PERIPHERAL */
634 
635 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
636 static void lp_cc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param);
637 
638 /* LLCP Local Procedure FSM states */
639 enum {
640 	LP_CC_STATE_IDLE,
641 	LP_CC_STATE_WAIT_OFFSET_CALC,
642 	LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ,
643 	LP_CC_STATE_WAIT_TX_CIS_REQ,
644 	LP_CC_STATE_WAIT_RX_CIS_RSP,
645 	LP_CC_STATE_WAIT_TX_CIS_IND,
646 	LP_CC_STATE_WAIT_INSTANT,
647 	LP_CC_STATE_WAIT_ESTABLISHED,
648 };
649 
650 /* LLCP Local Procedure CIS Creation FSM events */
651 enum {
652 	/* Procedure run */
653 	LP_CC_EVT_RUN,
654 
655 	/* Offset calculation reply received */
656 	LP_CC_EVT_OFFSET_CALC_REPLY,
657 
658 	/* Response received */
659 	LP_CC_EVT_CIS_RSP,
660 
661 	/* Reject response received */
662 	LP_CC_EVT_REJECT,
663 
664 	/* CIS established */
665 	LP_CC_EVT_ESTABLISHED,
666 
667 	/* Unknown response received */
668 	LP_CC_EVT_UNKNOWN,
669 };
670 
lp_cc_tx(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t opcode)671 static void lp_cc_tx(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t opcode)
672 {
673 	struct node_tx *tx;
674 	struct pdu_data *pdu;
675 
676 	/* Allocate tx node */
677 	tx = llcp_tx_alloc(conn, ctx);
678 	LL_ASSERT(tx);
679 
680 	pdu = (struct pdu_data *)tx->pdu;
681 
682 	/* Encode LL Control PDU */
683 	switch (opcode) {
684 	case PDU_DATA_LLCTRL_TYPE_CIS_REQ:
685 		llcp_pdu_encode_cis_req(ctx, pdu);
686 		break;
687 	case PDU_DATA_LLCTRL_TYPE_CIS_IND:
688 		llcp_pdu_encode_cis_ind(ctx, pdu);
689 		break;
690 	default:
691 		/* Unknown opcode */
692 		LL_ASSERT(0);
693 		break;
694 	}
695 
696 	ctx->tx_opcode = pdu->llctrl.opcode;
697 
698 	/* Enqueue LL Control PDU towards LLL */
699 	llcp_tx_enqueue(conn, tx);
700 }
701 
llcp_lp_cc_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)702 void llcp_lp_cc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
703 {
704 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
705 
706 	switch (pdu->llctrl.opcode) {
707 	case PDU_DATA_LLCTRL_TYPE_CIS_RSP:
708 		lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_CIS_RSP, pdu);
709 		break;
710 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
711 	case PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND:
712 		lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_REJECT, pdu);
713 		break;
714 	default:
715 		/* Invalid behaviour */
716 		/* Invalid PDU received so terminate connection */
717 		conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
718 		llcp_lr_complete(conn);
719 		ctx->state = LP_CC_STATE_IDLE;
720 		break;
721 	}
722 }
723 
llcp_lp_cc_offset_calc_reply(struct ll_conn * conn,struct proc_ctx * ctx)724 void llcp_lp_cc_offset_calc_reply(struct ll_conn *conn, struct proc_ctx *ctx)
725 {
726 	lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_OFFSET_CALC_REPLY, NULL);
727 }
728 
lp_cc_offset_calc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)729 static void lp_cc_offset_calc_req(struct ll_conn *conn, struct proc_ctx *ctx,
730 				  uint8_t evt, void *param)
731 {
732 	if (llcp_lr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
733 		ctx->state = LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ;
734 	} else {
735 		int err;
736 
737 		/* Update conn_event_count */
738 		err = ull_central_iso_cis_offset_get(ctx->data.cis_create.cis_handle,
739 						     &ctx->data.cis_create.cis_offset_min,
740 						     &ctx->data.cis_create.cis_offset_max,
741 						     &ctx->data.cis_create.conn_event_count);
742 		if (err) {
743 			ctx->state = LP_CC_STATE_WAIT_OFFSET_CALC;
744 
745 			return;
746 		}
747 
748 		lp_cc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
749 
750 		ctx->state = LP_CC_STATE_WAIT_RX_CIS_RSP;
751 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_CIS_RSP;
752 	}
753 }
754 
lp_cc_st_wait_offset_calc_tx_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)755 static void lp_cc_st_wait_offset_calc_tx_req(struct ll_conn *conn,
756 					     struct proc_ctx *ctx,
757 					     uint8_t evt, void *param)
758 {
759 	switch (evt) {
760 	case LP_CC_EVT_RUN:
761 		lp_cc_offset_calc_req(conn, ctx, evt, param);
762 		break;
763 	default:
764 		/* Ignore other evts */
765 		break;
766 	}
767 }
768 
lp_cc_st_wait_offset_calc(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)769 static void lp_cc_st_wait_offset_calc(struct ll_conn *conn,
770 				      struct proc_ctx *ctx,
771 				      uint8_t evt, void *param)
772 {
773 	switch (evt) {
774 	case LP_CC_EVT_RUN:
775 		/* TODO: May be have a timeout calculating the CIS offset?
776 		 *       otherwise, ignore
777 		 */
778 		break;
779 	case LP_CC_EVT_OFFSET_CALC_REPLY:
780 		ctx->state = LP_CC_STATE_WAIT_TX_CIS_REQ;
781 		break;
782 	default:
783 		/* Ignore other evts */
784 		break;
785 	}
786 }
787 
lp_cc_send_cis_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)788 static void lp_cc_send_cis_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
789 			       void *param)
790 {
791 	if (llcp_lr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
792 		ctx->state = LP_CC_STATE_WAIT_TX_CIS_REQ;
793 	} else {
794 		lp_cc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_REQ);
795 
796 		ctx->state = LP_CC_STATE_WAIT_RX_CIS_RSP;
797 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_CIS_RSP;
798 	}
799 }
800 
lp_cc_st_wait_tx_cis_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)801 static void lp_cc_st_wait_tx_cis_req(struct ll_conn *conn, struct proc_ctx *ctx,
802 				     uint8_t evt, void *param)
803 {
804 	switch (evt) {
805 	case LP_CC_EVT_RUN:
806 		lp_cc_send_cis_req(conn, ctx, evt, param);
807 		break;
808 	default:
809 		/* Ignore other evts */
810 		break;
811 	}
812 }
813 
lp_cc_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)814 static void lp_cc_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
815 {
816 	cc_ntf_established(conn, ctx);
817 	llcp_lr_complete(conn);
818 	ctx->state = LP_CC_STATE_IDLE;
819 }
820 
lp_cc_st_idle(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)821 static void lp_cc_st_idle(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
822 {
823 	switch (evt) {
824 	case LP_CC_EVT_RUN:
825 		switch (ctx->proc) {
826 		case PROC_CIS_CREATE:
827 			/* In case feature exchange completed after CIS create was enqueued
828 			 * peer CIS peripheral support should be confirmed
829 			 */
830 			if (feature_peer_iso_peripheral(conn)) {
831 				lp_cc_offset_calc_req(conn, ctx, evt, param);
832 			} else {
833 				/* Peer doesn't support CIS Peripheral so report unsupported */
834 				ctx->data.cis_create.error = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE;
835 				lp_cc_complete(conn, ctx, evt, param);
836 			}
837 			break;
838 		default:
839 			/* Unknown procedure */
840 			LL_ASSERT(0);
841 			break;
842 		}
843 		break;
844 	default:
845 		/* Ignore other evts */
846 		break;
847 	}
848 }
849 
cc_prepare_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx)850 static void cc_prepare_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx)
851 {
852 	uint8_t err;
853 
854 	/* Setup central parameters based on CIS_RSP */
855 	err = ull_central_iso_setup(ctx->data.cis_create.cis_handle,
856 				    &ctx->data.cis_create.cig_sync_delay,
857 				    &ctx->data.cis_create.cis_sync_delay,
858 				    &ctx->data.cis_create.cis_offset_min,
859 				    &ctx->data.cis_create.cis_offset_max,
860 				    &ctx->data.cis_create.conn_event_count,
861 				    ctx->data.cis_create.aa);
862 	LL_ASSERT(!err);
863 
864 	ctx->state = LP_CC_STATE_WAIT_INSTANT;
865 	ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_UNUSED;
866 }
867 
lp_cc_send_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)868 static void lp_cc_send_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
869 			       void *param)
870 {
871 	if (llcp_lr_ispaused(conn) || !llcp_tx_alloc_peek(conn, ctx)) {
872 		ctx->state = LP_CC_STATE_WAIT_TX_CIS_IND;
873 	} else {
874 		cc_prepare_cis_ind(conn, ctx);
875 		lp_cc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_CIS_IND);
876 	}
877 }
878 
lp_cc_st_wait_rx_cis_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)879 static void lp_cc_st_wait_rx_cis_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
880 				     void *param)
881 {
882 	struct pdu_data *pdu = (struct pdu_data *)param;
883 
884 	switch (evt) {
885 	case LP_CC_EVT_CIS_RSP:
886 		/* TODO: Reject response if outside offset range? */
887 		llcp_pdu_decode_cis_rsp(ctx, param);
888 
889 		/* Mark RX node to NOT release */
890 		llcp_rx_node_retain(ctx);
891 
892 		lp_cc_send_cis_ind(conn, ctx, evt, param);
893 		break;
894 	case LP_CC_EVT_UNKNOWN:
895 		/* Unsupported in peer, so disable locally for this connection */
896 		feature_unmask_features(conn, LL_FEAT_BIT_CIS_PERIPHERAL);
897 		ctx->data.cis_create.error = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE;
898 		lp_cc_complete(conn, ctx, evt, param);
899 		break;
900 	case LP_CC_EVT_REJECT:
901 		if (pdu->llctrl.reject_ext_ind.error_code == BT_HCI_ERR_UNSUPP_REMOTE_FEATURE) {
902 			/* Unsupported in peer, so disable locally for this connection */
903 			feature_unmask_features(conn, LL_FEAT_BIT_CIS_PERIPHERAL);
904 		}
905 		ctx->data.cis_create.error = pdu->llctrl.reject_ext_ind.error_code;
906 		lp_cc_complete(conn, ctx, evt, param);
907 		break;
908 	default:
909 		/* Ignore other evts */
910 		break;
911 	}
912 }
913 
lp_cc_st_wait_tx_cis_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)914 static void lp_cc_st_wait_tx_cis_ind(struct ll_conn *conn, struct proc_ctx *ctx,
915 				     uint8_t evt, void *param)
916 {
917 	switch (evt) {
918 	case LP_CC_EVT_RUN:
919 		lp_cc_send_cis_ind(conn, ctx, evt, param);
920 		break;
921 	default:
922 		/* Ignore other evts */
923 		break;
924 	}
925 }
926 
lp_cc_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)927 static void lp_cc_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
928 				void *param)
929 {
930 	uint16_t start_event_count;
931 	uint16_t instant_latency;
932 	uint16_t event_counter;
933 
934 	event_counter = ull_conn_event_counter(conn);
935 	start_event_count = ctx->data.cis_create.conn_event_count;
936 
937 	instant_latency = (event_counter - start_event_count) & 0xffff;
938 	if (instant_latency <= 0x7fff) {
939 		/* Start CIS */
940 		ull_conn_iso_start(conn, ctx->data.cis_create.cis_handle,
941 				   conn->llcp.prep.ticks_at_expire,
942 				   conn->llcp.prep.remainder,
943 				   instant_latency);
944 
945 		/* Now we can wait for CIS to become established */
946 		ctx->state = LP_CC_STATE_WAIT_ESTABLISHED;
947 	}
948 }
949 
lp_cc_st_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)950 static void lp_cc_st_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
951 				  void *param)
952 {
953 	switch (evt) {
954 	case LP_CC_EVT_RUN:
955 		lp_cc_check_instant(conn, ctx, evt, param);
956 		break;
957 	default:
958 		/* Ignore other evts */
959 		break;
960 	}
961 }
962 
lp_cc_st_wait_established(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)963 static void lp_cc_st_wait_established(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
964 				      void *param)
965 {
966 	switch (evt) {
967 	case LP_CC_EVT_ESTABLISHED:
968 		/* CIS was established, so let's go ahead and complete procedure */
969 		lp_cc_complete(conn, ctx, evt, param);
970 		break;
971 	default:
972 		/* Ignore other evts */
973 		break;
974 	}
975 }
976 
lp_cc_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)977 static void lp_cc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
978 {
979 	switch (ctx->state) {
980 	case LP_CC_STATE_IDLE:
981 		lp_cc_st_idle(conn, ctx, evt, param);
982 		break;
983 	case LP_CC_STATE_WAIT_OFFSET_CALC_TX_REQ:
984 		lp_cc_st_wait_offset_calc_tx_req(conn, ctx, evt, param);
985 		break;
986 	case LP_CC_STATE_WAIT_OFFSET_CALC:
987 		lp_cc_st_wait_offset_calc(conn, ctx, evt, param);
988 		break;
989 	case LP_CC_STATE_WAIT_TX_CIS_REQ:
990 		lp_cc_st_wait_tx_cis_req(conn, ctx, evt, param);
991 		break;
992 	case LP_CC_STATE_WAIT_RX_CIS_RSP:
993 		lp_cc_st_wait_rx_cis_rsp(conn, ctx, evt, param);
994 		break;
995 	case LP_CC_STATE_WAIT_TX_CIS_IND:
996 		lp_cc_st_wait_tx_cis_ind(conn, ctx, evt, param);
997 		break;
998 	case LP_CC_STATE_WAIT_INSTANT:
999 		lp_cc_st_wait_instant(conn, ctx, evt, param);
1000 		break;
1001 	case LP_CC_STATE_WAIT_ESTABLISHED:
1002 		lp_cc_st_wait_established(conn, ctx, evt, param);
1003 		break;
1004 	default:
1005 		/* Unknown state */
1006 		LL_ASSERT(0);
1007 		break;
1008 	}
1009 }
1010 
llcp_lp_cc_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)1011 void llcp_lp_cc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
1012 {
1013 	lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_RUN, param);
1014 }
1015 
llcp_lp_cc_is_active(struct proc_ctx * ctx)1016 bool llcp_lp_cc_is_active(struct proc_ctx *ctx)
1017 {
1018 	return ctx->state != LP_CC_STATE_IDLE;
1019 }
1020 
llcp_lp_cc_awaiting_established(struct proc_ctx * ctx)1021 bool llcp_lp_cc_awaiting_established(struct proc_ctx *ctx)
1022 {
1023 	return (ctx->state == LP_CC_STATE_WAIT_ESTABLISHED);
1024 }
1025 
llcp_lp_cc_established(struct ll_conn * conn,struct proc_ctx * ctx)1026 void llcp_lp_cc_established(struct ll_conn *conn, struct proc_ctx *ctx)
1027 {
1028 	lp_cc_execute_fsm(conn, ctx, LP_CC_EVT_ESTABLISHED, NULL);
1029 }
1030 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
1031