1 /*  Bluetooth ISO */
2 
3 /*
4  * Copyright (c) 2020 Intel Corporation
5  * Copyright (c) 2021 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr.h>
11 #include <sys/byteorder.h>
12 #include <sys/check.h>
13 
14 #include <bluetooth/hci.h>
15 #include <bluetooth/bluetooth.h>
16 #include <bluetooth/conn.h>
17 #include <bluetooth/iso.h>
18 
19 #include "host/hci_core.h"
20 #include "host/conn_internal.h"
21 #include "iso_internal.h"
22 
23 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_ISO)
24 #define LOG_MODULE_NAME bt_iso
25 #include "common/log.h"
26 
27 NET_BUF_POOL_FIXED_DEFINE(iso_tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
28 			  CONFIG_BT_ISO_TX_MTU, NULL);
29 NET_BUF_POOL_FIXED_DEFINE(iso_rx_pool, CONFIG_BT_ISO_RX_BUF_COUNT,
30 			  CONFIG_BT_ISO_RX_MTU, NULL);
31 
32 static struct bt_iso_recv_info iso_info_data[CONFIG_BT_ISO_RX_BUF_COUNT];
33 #define iso_info(buf) (&iso_info_data[net_buf_id(buf)])
34 #define iso_chan(_iso) ((_iso)->iso.chan);
35 
36 #if CONFIG_BT_ISO_TX_FRAG_COUNT > 0
37 NET_BUF_POOL_FIXED_DEFINE(iso_frag_pool, CONFIG_BT_ISO_TX_FRAG_COUNT,
38 			  CONFIG_BT_ISO_TX_MTU, NULL);
39 #endif /* CONFIG_BT_ISO_TX_FRAG_COUNT > 0 */
40 
41 struct bt_conn iso_conns[CONFIG_BT_ISO_MAX_CHAN];
42 
43 /* TODO: Allow more than one server? */
44 #if defined(CONFIG_BT_ISO_UNICAST)
45 struct bt_iso_cig cigs[CONFIG_BT_ISO_MAX_CIG];
46 static struct bt_iso_server *iso_server;
47 #endif /* CONFIG_BT_ISO_UNICAST */
48 #if defined(CONFIG_BT_ISO_BROADCAST)
49 struct bt_iso_big bigs[CONFIG_BT_ISO_MAX_BIG];
50 #endif /* defined(CONFIG_BT_ISO_BROADCAST) */
51 
52 /* Prototype */
53 int hci_le_remove_cig(uint8_t cig_id);
54 
55 struct bt_iso_data_path {
56 	/* Data Path direction */
57 	uint8_t dir;
58 	/* Data Path ID */
59 	uint8_t pid;
60 	/* Data Path param reference */
61 	struct bt_iso_chan_path *path;
62 };
63 
64 
bt_iso_get_rx(k_timeout_t timeout)65 struct net_buf *bt_iso_get_rx(k_timeout_t timeout)
66 {
67 	struct net_buf *buf = net_buf_alloc(&iso_rx_pool, timeout);
68 
69 	if (buf) {
70 		net_buf_reserve(buf, BT_BUF_RESERVE);
71 		bt_buf_set_type(buf, BT_BUF_ISO_IN);
72 	}
73 
74 	return buf;
75 }
76 
bt_iso_send_cb(struct bt_conn * iso,void * user_data)77 static void bt_iso_send_cb(struct bt_conn *iso, void *user_data)
78 {
79 	struct bt_iso_chan *chan = iso->iso.chan;
80 	struct bt_iso_chan_ops *ops;
81 
82 	__ASSERT(chan != NULL, "NULL chan for iso %p", iso);
83 
84 	ops = chan->ops;
85 
86 	if (ops != NULL && ops->sent != NULL) {
87 		ops->sent(chan);
88 	}
89 }
90 
hci_iso(struct net_buf * buf)91 void hci_iso(struct net_buf *buf)
92 {
93 	struct bt_hci_iso_hdr *hdr;
94 	uint16_t handle, len;
95 	struct bt_conn *iso;
96 	uint8_t flags;
97 
98 	BT_DBG("buf %p", buf);
99 
100 	BT_ASSERT(buf->len >= sizeof(*hdr));
101 
102 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
103 	len = sys_le16_to_cpu(hdr->len);
104 	handle = sys_le16_to_cpu(hdr->handle);
105 	flags = bt_iso_flags(handle);
106 
107 	iso(buf)->handle = bt_iso_handle(handle);
108 	iso(buf)->index = BT_CONN_INDEX_INVALID;
109 
110 	BT_DBG("handle %u len %u flags %u", iso(buf)->handle, len, flags);
111 
112 	if (buf->len != len) {
113 		BT_ERR("ISO data length mismatch (%u != %u)", buf->len, len);
114 		net_buf_unref(buf);
115 		return;
116 	}
117 
118 	iso = bt_conn_lookup_handle(iso(buf)->handle);
119 	if (iso == NULL) {
120 		BT_ERR("Unable to find conn for handle %u", iso(buf)->handle);
121 		net_buf_unref(buf);
122 		return;
123 	}
124 
125 	iso(buf)->index = bt_conn_index(iso);
126 
127 	bt_conn_recv(iso, buf, flags);
128 	bt_conn_unref(iso);
129 }
130 
iso_new(void)131 struct bt_conn *iso_new(void)
132 {
133 	struct bt_conn *iso = bt_conn_new(iso_conns, ARRAY_SIZE(iso_conns));
134 
135 	if (iso) {
136 		iso->type = BT_CONN_TYPE_ISO;
137 	} else {
138 		BT_DBG("Could not create new ISO");
139 	}
140 
141 	return iso;
142 }
143 
144 #if defined(CONFIG_NET_BUF_LOG)
bt_iso_create_pdu_timeout_debug(struct net_buf_pool * pool,size_t reserve,k_timeout_t timeout,const char * func,int line)145 struct net_buf *bt_iso_create_pdu_timeout_debug(struct net_buf_pool *pool,
146 						size_t reserve,
147 						k_timeout_t timeout,
148 						const char *func, int line)
149 #else
150 struct net_buf *bt_iso_create_pdu_timeout(struct net_buf_pool *pool,
151 					  size_t reserve, k_timeout_t timeout)
152 #endif
153 {
154 	if (!pool) {
155 		pool = &iso_tx_pool;
156 	}
157 
158 	reserve += sizeof(struct bt_hci_iso_data_hdr);
159 
160 #if defined(CONFIG_NET_BUF_LOG)
161 	return bt_conn_create_pdu_timeout_debug(pool, reserve, timeout, func,
162 						line);
163 #else
164 	return bt_conn_create_pdu_timeout(pool, reserve, timeout);
165 #endif
166 }
167 
168 #if defined(CONFIG_NET_BUF_LOG)
bt_iso_create_frag_timeout_debug(size_t reserve,k_timeout_t timeout,const char * func,int line)169 struct net_buf *bt_iso_create_frag_timeout_debug(size_t reserve,
170 						 k_timeout_t timeout,
171 						 const char *func, int line)
172 #else
173 struct net_buf *bt_iso_create_frag_timeout(size_t reserve, k_timeout_t timeout)
174 #endif
175 {
176 	struct net_buf_pool *pool = NULL;
177 
178 #if CONFIG_BT_ISO_TX_FRAG_COUNT > 0
179 	pool = &iso_frag_pool;
180 #endif /* CONFIG_BT_ISO_TX_FRAG_COUNT > 0 */
181 
182 #if defined(CONFIG_NET_BUF_LOG)
183 	return bt_conn_create_pdu_timeout_debug(pool, reserve, timeout, func,
184 						line);
185 #else
186 	return bt_conn_create_pdu_timeout(pool, reserve, timeout);
187 #endif
188 }
189 
190 
hci_le_setup_iso_data_path(struct bt_conn * iso,struct bt_iso_data_path * path)191 static int hci_le_setup_iso_data_path(struct bt_conn *iso,
192 				      struct bt_iso_data_path *path)
193 {
194 	struct bt_hci_cp_le_setup_iso_path *cp;
195 	struct bt_hci_rp_le_setup_iso_path *rp;
196 	struct net_buf *buf, *rsp;
197 	uint8_t *cc;
198 	int err;
199 
200 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SETUP_ISO_PATH, sizeof(*cp));
201 	if (!buf) {
202 		return -ENOBUFS;
203 	}
204 
205 	cp = net_buf_add(buf, sizeof(*cp));
206 	cp->handle = sys_cpu_to_le16(iso->handle);
207 	cp->path_dir = path->dir;
208 	cp->path_id = path->pid;
209 	cp->codec_id.coding_format = path->path->format;
210 	cp->codec_id.company_id = sys_cpu_to_le16(path->path->cid);
211 	cp->codec_id.vs_codec_id = sys_cpu_to_le16(path->path->vid);
212 	sys_put_le24(path->path->delay, cp->controller_delay);
213 	cp->codec_config_len = path->path->cc_len;
214 	cc = net_buf_add(buf, cp->codec_config_len);
215 	memcpy(cc, path->path->cc, cp->codec_config_len);
216 
217 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SETUP_ISO_PATH, buf, &rsp);
218 	if (err) {
219 		return err;
220 	}
221 
222 	rp = (void *)rsp->data;
223 	if (rp->status || (sys_le16_to_cpu(rp->handle) != iso->handle)) {
224 		err = -EIO;
225 	}
226 
227 	net_buf_unref(rsp);
228 
229 	return err;
230 }
231 
hci_le_remove_iso_data_path(struct bt_conn * iso,uint8_t dir)232 static int hci_le_remove_iso_data_path(struct bt_conn *iso, uint8_t dir)
233 {
234 	struct bt_hci_cp_le_remove_iso_path *cp;
235 	struct bt_hci_rp_le_remove_iso_path *rp;
236 	struct net_buf *buf, *rsp;
237 	int err;
238 
239 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ISO_PATH, sizeof(*cp));
240 	if (!buf) {
241 		return -ENOBUFS;
242 	}
243 
244 	cp = net_buf_add(buf, sizeof(*cp));
245 	cp->handle = iso->handle;
246 	cp->path_dir = dir;
247 
248 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ISO_PATH, buf, &rsp);
249 	if (err) {
250 		return err;
251 	}
252 
253 	rp = (void *)rsp->data;
254 	if (rp->status || (sys_le16_to_cpu(rp->handle) != iso->handle)) {
255 		err = -EIO;
256 	}
257 
258 	net_buf_unref(rsp);
259 
260 	return err;
261 }
262 
bt_iso_chan_add(struct bt_conn * iso,struct bt_iso_chan * chan)263 static void bt_iso_chan_add(struct bt_conn *iso, struct bt_iso_chan *chan)
264 {
265 	/* Attach ISO channel to the connection */
266 	chan->iso = iso;
267 	iso->iso.chan = chan;
268 
269 	BT_DBG("iso %p chan %p", iso, chan);
270 }
271 
bt_iso_setup_data_path(struct bt_conn * iso)272 static int bt_iso_setup_data_path(struct bt_conn *iso)
273 {
274 	int err;
275 	struct bt_iso_chan *chan;
276 	struct bt_iso_chan_path default_path = { .pid = BT_ISO_DATA_PATH_HCI };
277 	struct bt_iso_data_path out_path = {
278 		.dir = BT_HCI_DATAPATH_DIR_CTLR_TO_HOST };
279 	struct bt_iso_data_path in_path = {
280 		.dir = BT_HCI_DATAPATH_DIR_HOST_TO_CTLR };
281 	struct bt_iso_chan_io_qos *tx_qos;
282 	struct bt_iso_chan_io_qos *rx_qos;
283 
284 	chan = iso_chan(iso);
285 	if (chan == NULL) {
286 		return -EINVAL;
287 	}
288 
289 	tx_qos = chan->qos->tx;
290 	rx_qos = chan->qos->rx;
291 
292 	in_path.path = tx_qos && tx_qos->path ? tx_qos->path : &default_path;
293 	out_path.path = rx_qos && rx_qos->path ? rx_qos->path : &default_path;
294 
295 	if (!tx_qos) {
296 		in_path.pid = BT_ISO_DATA_PATH_DISABLED;
297 	}
298 
299 	if (!rx_qos) {
300 		out_path.pid = BT_ISO_DATA_PATH_DISABLED;
301 	}
302 
303 	if (iso->iso.is_bis) {
304 		/* Only set one data path for BIS as per the spec */
305 		if (tx_qos) {
306 			return hci_le_setup_iso_data_path(iso, &in_path);
307 
308 		} else {
309 			return hci_le_setup_iso_data_path(iso, &out_path);
310 		}
311 
312 	} else {
313 		/* Setup both directions for CIS*/
314 		err = hci_le_setup_iso_data_path(iso, &in_path);
315 		if (err) {
316 			return err;
317 		}
318 
319 		return hci_le_setup_iso_data_path(iso, &out_path);
320 
321 	}
322 }
323 
bt_iso_connected(struct bt_conn * iso)324 void bt_iso_connected(struct bt_conn *iso)
325 {
326 	struct bt_iso_chan *chan;
327 
328 	if (iso == NULL || iso->type != BT_CONN_TYPE_ISO) {
329 		BT_DBG("Invalid parameters: iso %p iso->type %u", iso,
330 		       iso ? iso->type : 0);
331 		return;
332 	}
333 
334 	BT_DBG("%p", iso);
335 
336 	if (bt_iso_setup_data_path(iso)) {
337 		BT_ERR("Unable to setup data path");
338 		if (iso->iso.is_bis && IS_ENABLED(CONFIG_BT_CONN)) {
339 			bt_conn_disconnect(iso,
340 					   BT_HCI_ERR_REMOTE_USER_TERM_CONN);
341 		}
342 		/* TODO: Handle BIG terminate for BIS */
343 		return;
344 	}
345 
346 	chan = iso_chan(iso);
347 	if (chan == NULL) {
348 		BT_ERR("Could not lookup chan from connected ISO");
349 		return;
350 	}
351 
352 	bt_iso_chan_set_state(chan, BT_ISO_CONNECTED);
353 
354 	if (chan->ops->connected) {
355 		chan->ops->connected(chan);
356 	}
357 }
358 
bt_iso_remove_data_path(struct bt_conn * iso)359 void bt_iso_remove_data_path(struct bt_conn *iso)
360 {
361 	BT_DBG("%p", iso);
362 
363 	if (iso->iso.is_bis) {
364 		struct bt_iso_chan *chan;
365 		struct bt_iso_chan_io_qos *tx_qos;
366 		uint8_t dir;
367 
368 		chan = iso_chan(iso);
369 		if (chan == NULL) {
370 			return;
371 		}
372 
373 		tx_qos = chan->qos->tx;
374 
375 		/* Only remove one data path for BIS as per the spec */
376 		if (tx_qos) {
377 			dir = BT_HCI_DATAPATH_DIR_HOST_TO_CTLR;
378 		} else {
379 			dir = BT_HCI_DATAPATH_DIR_CTLR_TO_HOST;
380 		}
381 
382 		(void)hci_le_remove_iso_data_path(iso, dir);
383 	} else {
384 		/* Remove both directions for CIS*/
385 
386 		/* TODO: Check which has been setup first to avoid removing
387 		 * data paths that are not setup
388 		 */
389 		(void)hci_le_remove_iso_data_path(iso,
390 						  BT_HCI_DATAPATH_DIR_CTLR_TO_HOST);
391 		(void)hci_le_remove_iso_data_path(iso,
392 						  BT_HCI_DATAPATH_DIR_HOST_TO_CTLR);
393 	}
394 }
395 
bt_iso_chan_disconnected(struct bt_iso_chan * chan,uint8_t reason)396 static void bt_iso_chan_disconnected(struct bt_iso_chan *chan, uint8_t reason)
397 {
398 	BT_DBG("%p, reason 0x%02x", chan, reason);
399 
400 	__ASSERT(chan->iso != NULL, "NULL conn for iso chan %p", chan);
401 
402 	bt_iso_chan_set_state(chan, BT_ISO_DISCONNECTED);
403 
404 	/* The peripheral does not have the concept of a CIG, so once a CIS
405 	 * disconnects it is completely freed by unref'ing it
406 	 */
407 	if (IS_ENABLED(CONFIG_BT_ISO_UNICAST) && !chan->iso->iso.is_bis) {
408 		bt_iso_cleanup_acl(chan->iso);
409 
410 		if (chan->iso->role == BT_HCI_ROLE_PERIPHERAL) {
411 			bt_conn_unref(chan->iso);
412 			chan->iso = NULL;
413 		} else {
414 			/* ISO data paths are automatically removed when the
415 			 * peripheral disconnects, so we only need to
416 			 * move it for the central
417 			 */
418 			bt_iso_remove_data_path(chan->iso);
419 		}
420 	}
421 
422 	if (chan->ops->disconnected) {
423 		chan->ops->disconnected(chan, reason);
424 	}
425 }
426 
bt_iso_disconnected(struct bt_conn * iso)427 void bt_iso_disconnected(struct bt_conn *iso)
428 {
429 	struct bt_iso_chan *chan;
430 
431 	if (iso == NULL || iso->type != BT_CONN_TYPE_ISO) {
432 		BT_DBG("Invalid parameters: iso %p iso->type %u", iso,
433 		       iso ? iso->type : 0);
434 		return;
435 	}
436 
437 	BT_DBG("%p", iso);
438 
439 	chan = iso_chan(iso);
440 	if (chan == NULL) {
441 		BT_ERR("Could not lookup chan from disconnected ISO");
442 		return;
443 	}
444 
445 	bt_iso_chan_disconnected(chan, iso->err);
446 }
447 
448 #if defined(CONFIG_BT_DEBUG_ISO)
bt_iso_chan_state_str(uint8_t state)449 const char *bt_iso_chan_state_str(uint8_t state)
450 {
451 	switch (state) {
452 	case BT_ISO_DISCONNECTED:
453 		return "disconnected";
454 	case BT_ISO_CONNECT:
455 		return "connect";
456 	case BT_ISO_CONNECTED:
457 		return "connected";
458 	case BT_ISO_DISCONNECT:
459 		return "disconnect";
460 	default:
461 		return "unknown";
462 	}
463 }
464 
bt_iso_chan_set_state_debug(struct bt_iso_chan * chan,uint8_t state,const char * func,int line)465 void bt_iso_chan_set_state_debug(struct bt_iso_chan *chan, uint8_t state,
466 				 const char *func, int line)
467 {
468 	BT_DBG("chan %p iso %p %s -> %s", chan, chan->iso,
469 	       bt_iso_chan_state_str(chan->state),
470 	       bt_iso_chan_state_str(state));
471 
472 	/* check transitions validness */
473 	switch (state) {
474 	case BT_ISO_DISCONNECTED:
475 		/* regardless of old state always allows this states */
476 		break;
477 	case BT_ISO_CONNECT:
478 		if (chan->state != BT_ISO_DISCONNECTED) {
479 			BT_WARN("%s()%d: invalid transition", func, line);
480 		}
481 		break;
482 	case BT_ISO_CONNECTED:
483 		if (chan->state != BT_ISO_CONNECT) {
484 			BT_WARN("%s()%d: invalid transition", func, line);
485 		}
486 		break;
487 	case BT_ISO_DISCONNECT:
488 		if (chan->state != BT_ISO_CONNECTED) {
489 			BT_WARN("%s()%d: invalid transition", func, line);
490 		}
491 		break;
492 	default:
493 		BT_ERR("%s()%d: unknown (%u) state was set", func, line, state);
494 		return;
495 	}
496 
497 	chan->state = state;
498 }
499 #else
bt_iso_chan_set_state(struct bt_iso_chan * chan,uint8_t state)500 void bt_iso_chan_set_state(struct bt_iso_chan *chan, uint8_t state)
501 {
502 	chan->state = state;
503 }
504 #endif /* CONFIG_BT_DEBUG_ISO */
505 
bt_iso_recv(struct bt_conn * iso,struct net_buf * buf,uint8_t flags)506 void bt_iso_recv(struct bt_conn *iso, struct net_buf *buf, uint8_t flags)
507 {
508 	struct bt_hci_iso_data_hdr *hdr;
509 	struct bt_iso_chan *chan;
510 	uint8_t pb, ts;
511 	uint16_t len, pkt_seq_no;
512 
513 	pb = bt_iso_flags_pb(flags);
514 	ts = bt_iso_flags_ts(flags);
515 
516 	BT_DBG("handle %u len %u flags 0x%02x pb 0x%02x ts 0x%02x",
517 	       iso->handle, buf->len, flags, pb, ts);
518 
519 	/* When the PB_Flag does not equal 0b00, the fields Time_Stamp,
520 	 * Packet_Sequence_Number, Packet_Status_Flag and ISO_SDU_Length
521 	 * are omitted from the HCI ISO Data packet.
522 	 */
523 	switch (pb) {
524 	case BT_ISO_START:
525 	case BT_ISO_SINGLE:
526 		/* The ISO_Data_Load field contains either the first fragment
527 		 * of an SDU or a complete SDU.
528 		 */
529 		if (ts) {
530 			struct bt_hci_iso_ts_data_hdr *ts_hdr;
531 
532 			ts_hdr = net_buf_pull_mem(buf, sizeof(*ts_hdr));
533 			iso_info(buf)->ts = sys_le32_to_cpu(ts_hdr->ts);
534 
535 			hdr = &ts_hdr->data;
536 		} else {
537 			hdr = net_buf_pull_mem(buf, sizeof(*hdr));
538 			/* TODO: Generate a timestamp? */
539 			iso_info(buf)->ts = 0x00000000;
540 		}
541 
542 		len = sys_le16_to_cpu(hdr->slen);
543 		flags = bt_iso_pkt_flags(len);
544 		len = bt_iso_pkt_len(len);
545 		pkt_seq_no = sys_le16_to_cpu(hdr->sn);
546 		iso_info(buf)->sn = pkt_seq_no;
547 
548 		if (flags == BT_ISO_DATA_VALID) {
549 			iso_info(buf)->flags = BT_ISO_FLAGS_VALID;
550 		} else if (flags == BT_ISO_DATA_INVALID) {
551 			iso_info(buf)->flags = BT_ISO_FLAGS_ERROR;
552 		} else if (flags == BT_ISO_DATA_NOP) {
553 			iso_info(buf)->flags = BT_ISO_FLAGS_LOST;
554 		} else {
555 			BT_WARN("Invalid ISO packet status flag: %u", flags);
556 			iso_info(buf)->flags = 0;
557 		}
558 
559 		BT_DBG("%s, len %u total %u flags 0x%02x timestamp %u",
560 		       pb == BT_ISO_START ? "Start" : "Single", buf->len, len,
561 		       flags, iso_info(buf)->ts);
562 
563 		if (iso->rx) {
564 			BT_ERR("Unexpected ISO %s fragment",
565 			       pb == BT_ISO_START ? "Start" : "Single");
566 			bt_conn_reset_rx_state(iso);
567 		}
568 
569 		iso->rx = buf;
570 		iso->rx_len = len - buf->len;
571 		if (iso->rx_len) {
572 			/* if iso->rx_len then package is longer than the
573 			 * buf->len and cannot fit in a SINGLE package
574 			 */
575 			if (pb == BT_ISO_SINGLE) {
576 				BT_ERR("Unexpected ISO single fragment");
577 				bt_conn_reset_rx_state(iso);
578 			}
579 			return;
580 		}
581 		break;
582 
583 	case BT_ISO_CONT:
584 		/* The ISO_Data_Load field contains a continuation fragment of
585 		 * an SDU.
586 		 */
587 		if (!iso->rx) {
588 			BT_ERR("Unexpected ISO continuation fragment");
589 			net_buf_unref(buf);
590 			return;
591 		}
592 
593 		BT_DBG("Cont, len %u rx_len %u", buf->len, iso->rx_len);
594 
595 		if (buf->len > net_buf_tailroom(iso->rx)) {
596 			BT_ERR("Not enough buffer space for ISO data");
597 			bt_conn_reset_rx_state(iso);
598 			net_buf_unref(buf);
599 			return;
600 		}
601 
602 		net_buf_add_mem(iso->rx, buf->data, buf->len);
603 		iso->rx_len -= buf->len;
604 		net_buf_unref(buf);
605 		return;
606 
607 	case BT_ISO_END:
608 		/* The ISO_Data_Load field contains the last fragment of an
609 		 * SDU.
610 		 */
611 		BT_DBG("End, len %u rx_len %u", buf->len, iso->rx_len);
612 
613 		if (iso->rx == NULL) {
614 			BT_ERR("Unexpected ISO end fragment");
615 			net_buf_unref(buf);
616 			return;
617 		}
618 
619 		if (buf->len > net_buf_tailroom(iso->rx)) {
620 			BT_ERR("Not enough buffer space for ISO data");
621 			bt_conn_reset_rx_state(iso);
622 			net_buf_unref(buf);
623 			return;
624 		}
625 
626 		(void)net_buf_add_mem(iso->rx, buf->data, buf->len);
627 		iso->rx_len -= buf->len;
628 		net_buf_unref(buf);
629 
630 		break;
631 	default:
632 		BT_ERR("Unexpected ISO pb flags (0x%02x)", pb);
633 		bt_conn_reset_rx_state(iso);
634 		net_buf_unref(buf);
635 		return;
636 	}
637 
638 	chan = iso_chan(iso);
639 	if (chan == NULL) {
640 		BT_ERR("Could not lookup chan from receiving ISO");
641 	} else if (chan->ops->recv != NULL) {
642 		chan->ops->recv(chan, iso_info(iso->rx), iso->rx);
643 	}
644 
645 	bt_conn_reset_rx_state(iso);
646 }
647 
bt_iso_chan_send(struct bt_iso_chan * chan,struct net_buf * buf)648 int bt_iso_chan_send(struct bt_iso_chan *chan, struct net_buf *buf)
649 {
650 	struct bt_hci_iso_data_hdr *hdr;
651 	static uint16_t sn;
652 
653 	CHECKIF(!chan || !buf) {
654 		BT_DBG("Invalid parameters: chan %p buf %p", chan, buf);
655 		return -EINVAL;
656 	}
657 
658 	BT_DBG("chan %p len %zu", chan, net_buf_frags_len(buf));
659 
660 	if (chan->state != BT_ISO_CONNECTED) {
661 		BT_DBG("Not connected");
662 		return -ENOTCONN;
663 	}
664 
665 	hdr = net_buf_push(buf, sizeof(*hdr));
666 	hdr->sn = sys_cpu_to_le16(sn++);
667 	hdr->slen = sys_cpu_to_le16(bt_iso_pkt_len_pack(net_buf_frags_len(buf)
668 							- sizeof(*hdr),
669 							BT_ISO_DATA_VALID));
670 
671 	return bt_conn_send_cb(chan->iso, buf, bt_iso_send_cb, NULL);
672 }
673 
valid_chan_io_qos(const struct bt_iso_chan_io_qos * io_qos,bool is_tx)674 static bool valid_chan_io_qos(const struct bt_iso_chan_io_qos *io_qos,
675 			      bool is_tx)
676 {
677 	const size_t max_mtu = (is_tx ? CONFIG_BT_ISO_TX_MTU : CONFIG_BT_ISO_RX_MTU) -
678 					BT_ISO_CHAN_SEND_RESERVE;
679 	const size_t max_sdu = MIN(max_mtu, BT_ISO_MAX_SDU);
680 
681 	if (io_qos->sdu > max_sdu) {
682 		BT_DBG("sdu (%u) shall be smaller than %zu",
683 		       io_qos->sdu, max_sdu);
684 		return false;
685 	}
686 
687 	if (io_qos->phy > BT_GAP_LE_PHY_CODED) {
688 		BT_DBG("Invalid phy %u", io_qos->phy);
689 		return false;
690 	}
691 
692 	return true;
693 }
694 
695 #if defined(CONFIG_BT_ISO_UNICAST)
valid_chan_qos(const struct bt_iso_chan_qos * qos)696 static bool valid_chan_qos(const struct bt_iso_chan_qos *qos)
697 {
698 	if (qos->rx != NULL) {
699 		if (!valid_chan_io_qos(qos->rx, false)) {
700 			BT_DBG("Invalid rx qos");
701 			return false;
702 		}
703 	} else if (qos->tx == NULL) {
704 		BT_DBG("Both rx and tx qos are NULL");
705 		return false;
706 	}
707 
708 	if (qos->tx != NULL) {
709 		if (!valid_chan_io_qos(qos->tx, true)) {
710 			BT_DBG("Invalid tx qos");
711 			return false;
712 		}
713 	}
714 
715 	return true;
716 }
717 
bt_iso_cleanup_acl(struct bt_conn * iso)718 void bt_iso_cleanup_acl(struct bt_conn *iso)
719 {
720 	BT_DBG("%p", iso);
721 
722 	if (iso->iso.acl) {
723 		bt_conn_unref(iso->iso.acl);
724 		iso->iso.acl = NULL;
725 	}
726 }
727 
hci_le_cis_estabilished(struct net_buf * buf)728 void hci_le_cis_estabilished(struct net_buf *buf)
729 {
730 	struct bt_hci_evt_le_cis_established *evt = (void *)buf->data;
731 	uint16_t handle = sys_le16_to_cpu(evt->conn_handle);
732 	struct bt_conn *iso;
733 
734 	BT_DBG("status %u handle %u", evt->status, handle);
735 
736 	/* ISO connection handles are already assigned at this point */
737 	iso = bt_conn_lookup_handle(handle);
738 	if (!iso) {
739 		BT_ERR("No connection found for handle %u", handle);
740 		return;
741 	}
742 
743 	CHECKIF(iso->type != BT_CONN_TYPE_ISO) {
744 		BT_DBG("Invalid connection type %u", iso->type);
745 		return;
746 	}
747 
748 	if (!evt->status) {
749 		/* TODO: Add CIG sync delay */
750 		bt_conn_set_state(iso, BT_CONN_CONNECTED);
751 		bt_conn_unref(iso);
752 		return;
753 	}
754 
755 	iso->err = evt->status;
756 	bt_iso_disconnected(iso);
757 	bt_conn_unref(iso);
758 }
759 
hci_le_reject_cis(uint16_t handle,uint8_t reason)760 int hci_le_reject_cis(uint16_t handle, uint8_t reason)
761 {
762 	struct bt_hci_cp_le_reject_cis *cp;
763 	struct net_buf *buf;
764 	int err;
765 
766 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REJECT_CIS, sizeof(*cp));
767 	if (!buf) {
768 		return -ENOBUFS;
769 	}
770 
771 	cp = net_buf_add(buf, sizeof(*cp));
772 	cp->handle = sys_cpu_to_le16(handle);
773 	cp->reason = reason;
774 
775 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REJECT_CIS, buf, NULL);
776 	if (err) {
777 		return err;
778 	}
779 
780 	return 0;
781 }
782 
hci_le_accept_cis(uint16_t handle)783 int hci_le_accept_cis(uint16_t handle)
784 {
785 	struct bt_hci_cp_le_accept_cis *cp;
786 	struct net_buf *buf;
787 	int err;
788 
789 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_ACCEPT_CIS, sizeof(*cp));
790 	if (!buf) {
791 		return -ENOBUFS;
792 	}
793 
794 	cp = net_buf_add(buf, sizeof(*cp));
795 	cp->handle = sys_cpu_to_le16(handle);
796 
797 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_ACCEPT_CIS, buf, NULL);
798 	if (err) {
799 		return err;
800 	}
801 
802 	return 0;
803 }
804 
hci_le_cis_req(struct net_buf * buf)805 void hci_le_cis_req(struct net_buf *buf)
806 {
807 	struct bt_hci_evt_le_cis_req *evt = (void *)buf->data;
808 	uint16_t acl_handle = sys_le16_to_cpu(evt->acl_handle);
809 	uint16_t cis_handle = sys_le16_to_cpu(evt->cis_handle);
810 	struct bt_conn *acl, *iso;
811 	int err;
812 
813 	BT_DBG("acl_handle %u cis_handle %u cig_id %u cis %u",
814 		acl_handle, cis_handle, evt->cig_id, evt->cis_id);
815 
816 	/* Lookup existing connection with same handle */
817 	iso = bt_conn_lookup_handle(cis_handle);
818 	if (iso) {
819 		BT_ERR("Invalid ISO handle %u", cis_handle);
820 		hci_le_reject_cis(cis_handle, BT_HCI_ERR_CONN_LIMIT_EXCEEDED);
821 		bt_conn_unref(iso);
822 		return;
823 	}
824 
825 	/* Lookup ACL connection to attach */
826 	acl = bt_conn_lookup_handle(acl_handle);
827 	if (!acl) {
828 		BT_ERR("Invalid ACL handle %u", acl_handle);
829 		hci_le_reject_cis(cis_handle, BT_HCI_ERR_UNKNOWN_CONN_ID);
830 		return;
831 	}
832 
833 	/* Add ISO connection */
834 	iso = bt_conn_add_iso(acl);
835 
836 	bt_conn_unref(acl);
837 
838 	if (!iso) {
839 		BT_ERR("Could not create and add ISO to ACL %u", acl_handle);
840 		hci_le_reject_cis(cis_handle,
841 				  BT_HCI_ERR_INSUFFICIENT_RESOURCES);
842 		return;
843 	}
844 
845 	iso->iso.cig_id = evt->cig_id;
846 	iso->iso.cis_id = evt->cis_id;
847 
848 	/* Request application to accept */
849 	err = bt_iso_accept(acl, iso);
850 	if (err) {
851 		BT_DBG("App rejected ISO %d", err);
852 		bt_conn_unref(iso);
853 		hci_le_reject_cis(cis_handle,
854 				  BT_HCI_ERR_INSUFFICIENT_RESOURCES);
855 		return;
856 	}
857 
858 	iso->handle = cis_handle;
859 	iso->role = BT_HCI_ROLE_PERIPHERAL;
860 	bt_conn_set_state(iso, BT_CONN_CONNECT);
861 
862 	err = hci_le_accept_cis(cis_handle);
863 	if (err) {
864 		bt_conn_unref(iso);
865 		hci_le_reject_cis(cis_handle,
866 				  BT_HCI_ERR_INSUFFICIENT_RESOURCES);
867 		return;
868 	}
869 }
870 
hci_le_remove_cig(uint8_t cig_id)871 int hci_le_remove_cig(uint8_t cig_id)
872 {
873 	struct bt_hci_cp_le_remove_cig *req;
874 	struct net_buf *buf;
875 
876 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_CIG, sizeof(*req));
877 	if (!buf) {
878 		return -ENOBUFS;
879 	}
880 
881 	req = net_buf_add(buf, sizeof(*req));
882 
883 	memset(req, 0, sizeof(*req));
884 
885 	req->cig_id = cig_id;
886 
887 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_CIG, buf, NULL);
888 }
889 
bt_conn_add_iso(struct bt_conn * acl)890 struct bt_conn *bt_conn_add_iso(struct bt_conn *acl)
891 {
892 	struct bt_conn *iso = iso_new();
893 
894 	if (iso == NULL) {
895 		BT_ERR("Unable to allocate ISO connection");
896 		return NULL;
897 	}
898 
899 	iso->iso.acl = bt_conn_ref(acl);
900 
901 	return iso;
902 }
903 
hci_le_set_cig_params(const struct bt_iso_cig * cig,const struct bt_iso_cig_create_param * param)904 static struct net_buf *hci_le_set_cig_params(const struct bt_iso_cig *cig,
905 					     const struct bt_iso_cig_create_param *param)
906 {
907 	struct bt_hci_cp_le_set_cig_params *req;
908 	struct bt_hci_cis_params *cis_param;
909 	struct net_buf *buf;
910 	struct net_buf *rsp;
911 	int i, err;
912 
913 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_CIG_PARAMS,
914 				sizeof(*req) + sizeof(*cis_param) * param->num_cis);
915 	if (!buf) {
916 		return NULL;
917 	}
918 
919 	req = net_buf_add(buf, sizeof(*req));
920 
921 	memset(req, 0, sizeof(*req));
922 
923 	req->cig_id = cig->id;
924 	req->c_latency = sys_cpu_to_le16(param->latency);
925 	req->p_latency = sys_cpu_to_le16(param->latency);
926 	sys_put_le24(param->interval, req->c_interval);
927 	sys_put_le24(param->interval, req->p_interval);
928 
929 	req->sca = param->sca;
930 	req->packing = param->packing;
931 	req->framing = param->framing;
932 	req->num_cis = param->num_cis;
933 
934 	/* Program the cis parameters */
935 	for (i = 0; i < param->num_cis; i++) {
936 		struct bt_iso_chan *cis = param->cis_channels[i];
937 		struct bt_iso_chan_qos *qos = cis->qos;
938 
939 		cis_param = net_buf_add(buf, sizeof(*cis_param));
940 
941 		memset(cis_param, 0, sizeof(*cis_param));
942 
943 		cis_param->cis_id = cis->iso->iso.cis_id;
944 
945 		if (!qos->tx && !qos->rx) {
946 			BT_ERR("Both TX and RX QoS are disabled");
947 			net_buf_unref(buf);
948 			return NULL;
949 		}
950 
951 		if (!qos->tx) {
952 			/* Use RX PHY if TX is not set (disabled)
953 			 * to avoid setting invalid values
954 			 */
955 			cis_param->c_phy = qos->rx->phy;
956 		} else {
957 			cis_param->c_sdu = sys_cpu_to_le16(qos->tx->sdu);
958 			cis_param->c_phy = qos->tx->phy;
959 			cis_param->c_rtn = qos->tx->rtn;
960 		}
961 
962 		if (!qos->rx) {
963 			/* Use TX PHY if RX is not set (disabled)
964 			 * to avoid setting invalid values
965 			 */
966 			cis_param->p_phy = qos->tx->phy;
967 		} else {
968 			cis_param->p_sdu = sys_cpu_to_le16(qos->rx->sdu);
969 			cis_param->p_phy = qos->rx->phy;
970 			cis_param->p_rtn = qos->rx->rtn;
971 		}
972 	}
973 
974 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CIG_PARAMS, buf, &rsp);
975 	if (err) {
976 		return NULL;
977 	}
978 
979 	return rsp;
980 }
981 
get_free_cig(void)982 static struct bt_iso_cig *get_free_cig(void)
983 {
984 	/* We can use the index in the `cigs` array as CIG ID */
985 
986 	for (int i = 0; i < ARRAY_SIZE(cigs); i++) {
987 		if (!cigs[i].initialized) {
988 			cigs[i].initialized = true;
989 			cigs[i].id = i;
990 			return &cigs[i];
991 		}
992 	}
993 
994 	BT_DBG("Could not allocate any more CIGs");
995 
996 	return NULL;
997 }
998 
cig_init_cis(struct bt_iso_cig * cig)999 static int cig_init_cis(struct bt_iso_cig *cig)
1000 {
1001 	for (int i = 0; i < cig->num_cis; i++) {
1002 		struct bt_iso_chan *cis = cig->cis[i];
1003 
1004 		CHECKIF(cis == NULL) {
1005 			BT_DBG("CIS was NULL");
1006 			return -EINVAL;
1007 		}
1008 
1009 		CHECKIF(!valid_chan_qos(cis->qos)) {
1010 			BT_DBG("Invalid QOS");
1011 			return -EINVAL;
1012 		}
1013 
1014 		if (cis->iso != NULL) {
1015 			BT_DBG("CIS conn was already allocated");
1016 			return -EALREADY;
1017 		}
1018 
1019 		cis->iso = iso_new();
1020 		if (cis->iso == NULL) {
1021 			BT_ERR("Unable to allocate CIS connection");
1022 			return -ENOMEM;
1023 		}
1024 
1025 		cis->iso->iso.cig_id = cig->id;
1026 		cis->iso->iso.is_bis = false;
1027 		cis->iso->iso.cis_id = i;
1028 
1029 		bt_iso_chan_add(cis->iso, cis);
1030 	}
1031 
1032 	return 0;
1033 }
1034 
cleanup_cig(struct bt_iso_cig * cig)1035 static void cleanup_cig(struct bt_iso_cig *cig)
1036 {
1037 	for (int i = 0; i < cig->num_cis; i++) {
1038 		struct bt_iso_chan *cis = cig->cis[i];
1039 
1040 		if (cis != NULL && cis->iso != NULL) {
1041 			bt_conn_unref(cis->iso);
1042 			cis->iso = NULL;
1043 		}
1044 	}
1045 
1046 	memset(cig, 0, sizeof(*cig));
1047 }
1048 
bt_iso_cig_create(const struct bt_iso_cig_create_param * param,struct bt_iso_cig ** out_cig)1049 int bt_iso_cig_create(const struct bt_iso_cig_create_param *param,
1050 		      struct bt_iso_cig **out_cig)
1051 {
1052 	int err;
1053 	struct net_buf *rsp;
1054 	struct bt_iso_cig *cig;
1055 	struct bt_hci_rp_le_set_cig_params *cig_rsp;
1056 
1057 	CHECKIF(out_cig == NULL) {
1058 		BT_DBG("out_cig is NULL");
1059 		return -EINVAL;
1060 	}
1061 
1062 	*out_cig = NULL;
1063 
1064 	/* Check if controller is ISO capable as a central */
1065 	if (!BT_FEAT_LE_CIS_CENTRAL(bt_dev.le.features)) {
1066 		return -ENOTSUP;
1067 	}
1068 
1069 	CHECKIF(param->cis_channels == NULL) {
1070 		BT_DBG("NULL CIS channels");
1071 		return -EINVAL;
1072 	}
1073 
1074 	CHECKIF(param->num_cis == 0) {
1075 		BT_DBG("Invalid number of CIS %u", param->num_cis);
1076 		return -EINVAL;
1077 	}
1078 
1079 	for (int i = 0; i < param->num_cis; i++) {
1080 		CHECKIF(param->cis_channels[i] == NULL) {
1081 			BT_DBG("NULL channel in cis_channels[%d]", i);
1082 			return -EINVAL;
1083 		}
1084 	}
1085 
1086 	CHECKIF(param->framing != BT_ISO_FRAMING_UNFRAMED &&
1087 		param->framing != BT_ISO_FRAMING_FRAMED) {
1088 		BT_DBG("Invalid framing parameter: %u", param->framing);
1089 		return -EINVAL;
1090 	}
1091 
1092 	CHECKIF(param->packing != BT_ISO_PACKING_SEQUENTIAL &&
1093 		param->packing != BT_ISO_PACKING_INTERLEAVED) {
1094 		BT_DBG("Invalid packing parameter: %u", param->packing);
1095 		return -EINVAL;
1096 	}
1097 
1098 	CHECKIF(param->num_cis > BT_ISO_MAX_GROUP_ISO_COUNT ||
1099 		param->num_cis > CONFIG_BT_ISO_MAX_CHAN) {
1100 		BT_DBG("num_cis (%u) shall be lower than: %u", param->num_cis,
1101 		       MAX(CONFIG_BT_ISO_MAX_CHAN, BT_ISO_MAX_GROUP_ISO_COUNT));
1102 		return -EINVAL;
1103 	}
1104 
1105 	CHECKIF(param->interval < BT_ISO_INTERVAL_MIN ||
1106 		param->interval > BT_ISO_INTERVAL_MAX) {
1107 		BT_DBG("Invalid interval: %u", param->interval);
1108 		return -EINVAL;
1109 	}
1110 
1111 	CHECKIF(param->latency < BT_ISO_LATENCY_MIN ||
1112 		param->latency > BT_ISO_LATENCY_MAX) {
1113 		BT_DBG("Invalid latency: %u", param->latency);
1114 		return -EINVAL;
1115 	}
1116 
1117 	cig = get_free_cig();
1118 
1119 	if (!cig) {
1120 		return -ENOMEM;
1121 	}
1122 
1123 	cig->cis = param->cis_channels;
1124 	cig->num_cis = param->num_cis;
1125 
1126 	err = cig_init_cis(cig);
1127 	if (err) {
1128 		BT_DBG("Could not init CIS %d", err);
1129 		cleanup_cig(cig);
1130 		return err;
1131 	}
1132 
1133 	rsp = hci_le_set_cig_params(cig, param);
1134 	if (rsp == NULL) {
1135 		BT_WARN("Unexpected response to hci_le_set_cig_params");
1136 		err = -EIO;
1137 		cleanup_cig(cig);
1138 		return err;
1139 	}
1140 
1141 	cig_rsp = (void *)rsp->data;
1142 
1143 	if (rsp->len < sizeof(cig_rsp) ||
1144 	    cig_rsp->num_handles != param->num_cis) {
1145 		BT_WARN("Unexpected response to hci_le_set_cig_params");
1146 		err = -EIO;
1147 		net_buf_unref(rsp);
1148 		cleanup_cig(cig);
1149 		return err;
1150 	}
1151 
1152 	for (int i = 0; i < cig_rsp->num_handles; i++) {
1153 		struct bt_iso_chan *chan;
1154 
1155 		chan = param->cis_channels[i];
1156 
1157 		/* Assign the connection handle */
1158 		chan->iso->handle = sys_le16_to_cpu(cig_rsp->handle[i]);
1159 	}
1160 
1161 	net_buf_unref(rsp);
1162 
1163 	*out_cig = cig;
1164 
1165 	return err;
1166 }
1167 
bt_iso_cig_terminate(struct bt_iso_cig * cig)1168 int bt_iso_cig_terminate(struct bt_iso_cig *cig)
1169 {
1170 	int err;
1171 
1172 	CHECKIF(cig == NULL) {
1173 		BT_DBG("cig is NULL");
1174 		return -EINVAL;
1175 	}
1176 
1177 	for (int i = 0; i < cig->num_cis; i++) {
1178 		if (cig->cis[i]->state != BT_ISO_DISCONNECTED) {
1179 			BT_DBG("[%d]: Channel is not disconnected", i);
1180 			return -EINVAL;
1181 		}
1182 	}
1183 
1184 	err = hci_le_remove_cig(cig->id);
1185 	if (err != 0) {
1186 		BT_DBG("Failed to terminate CIG: %d", err);
1187 		return err;
1188 	}
1189 
1190 	cleanup_cig(cig);
1191 
1192 	return 0;
1193 }
1194 
hci_le_create_cis(const struct bt_iso_connect_param * param,size_t count)1195 static int hci_le_create_cis(const struct bt_iso_connect_param *param,
1196 			     size_t count)
1197 {
1198 	struct bt_hci_cis *cis;
1199 	struct bt_hci_cp_le_create_cis *req;
1200 	struct net_buf *buf;
1201 
1202 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CIS,
1203 				sizeof(*req) + sizeof(*cis) * count);
1204 	if (!buf) {
1205 		return -ENOBUFS;
1206 	}
1207 
1208 	req = net_buf_add(buf, sizeof(*req));
1209 
1210 	memset(req, 0, sizeof(*req));
1211 
1212 	req->num_cis = count;
1213 
1214 	/* Program the cis parameters */
1215 	for (int i = 0; i < count; i++) {
1216 		cis = net_buf_add(buf, sizeof(*cis));
1217 
1218 		memset(cis, 0, sizeof(*cis));
1219 
1220 		cis->cis_handle = sys_cpu_to_le16(param[i].iso_chan->iso->handle);
1221 		cis->acl_handle = sys_cpu_to_le16(param[i].acl->handle);
1222 	}
1223 
1224 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_CIS, buf, NULL);
1225 }
1226 
bt_iso_accept(struct bt_conn * acl,struct bt_conn * iso)1227 int bt_iso_accept(struct bt_conn *acl, struct bt_conn *iso)
1228 {
1229 	struct bt_iso_chan *chan;
1230 	int err;
1231 
1232 	CHECKIF(!iso || iso->type != BT_CONN_TYPE_ISO) {
1233 		BT_DBG("Invalid parameters: iso %p iso->type %u", iso,
1234 		       iso ? iso->type : 0);
1235 		return -EINVAL;
1236 	}
1237 
1238 	BT_DBG("%p", iso);
1239 
1240 	if (!iso_server) {
1241 		return -ENOMEM;
1242 	}
1243 
1244 	err = iso_server->accept(acl, &chan);
1245 	if (err < 0) {
1246 		BT_ERR("Server failed to accept: %d", err);
1247 		return err;
1248 	}
1249 
1250 	bt_iso_chan_add(iso, chan);
1251 	bt_iso_chan_set_state(chan, BT_ISO_CONNECT);
1252 
1253 	return 0;
1254 }
1255 
bt_iso_chan_connect(const struct bt_iso_connect_param * param,size_t count)1256 int bt_iso_chan_connect(const struct bt_iso_connect_param *param, size_t count)
1257 {
1258 	int err;
1259 
1260 	CHECKIF(param == NULL || count == 0) {
1261 		BT_DBG("param is NULL");
1262 		return -EINVAL;
1263 	}
1264 
1265 	CHECKIF(count == 0) {
1266 		BT_DBG("Invalid count %zu", count);
1267 		return -EINVAL;
1268 	}
1269 
1270 	CHECKIF(count > CONFIG_BT_ISO_MAX_CHAN) {
1271 		return -EINVAL;
1272 	}
1273 
1274 	/* Validate input */
1275 	for (int i = 0; i < count; i++) {
1276 		CHECKIF(param[i].iso_chan == NULL) {
1277 			BT_DBG("[%d]: Invalid iso (%p)", i, param[i].iso_chan);
1278 			return -EINVAL;
1279 		}
1280 
1281 		CHECKIF(param[i].acl == NULL) {
1282 			BT_DBG("[%d]: Invalid acl (%p)", i, param[i].acl);
1283 			return -EINVAL;
1284 		}
1285 
1286 		CHECKIF((param[i].acl->type & BT_CONN_TYPE_LE) == 0) {
1287 			BT_DBG("[%d]: acl type (%u) shall be an LE connection",
1288 			       i, param[i].acl->type);
1289 			return -EINVAL;
1290 		}
1291 
1292 		if (param[i].iso_chan->iso == NULL) {
1293 			BT_DBG("[%d]: ISO has not been initialized in a CIG",
1294 			       i);
1295 			return -EINVAL;
1296 		}
1297 
1298 		if (param[i].iso_chan->state != BT_ISO_DISCONNECTED) {
1299 			BT_DBG("[%d]: ISO is not in the BT_ISO_DISCONNECTED state: %u",
1300 			       i, param[i].iso_chan->state);
1301 			return -EINVAL;
1302 		}
1303 	}
1304 
1305 	err = hci_le_create_cis(param, count);
1306 	if (err) {
1307 		BT_DBG("Failed to connect CISes: %d", err);
1308 		return err;
1309 	}
1310 
1311 	/* Set connection states */
1312 	for (int i = 0; i < count; i++) {
1313 		param[i].iso_chan->iso->iso.acl = bt_conn_ref(param[i].acl);
1314 		bt_conn_set_state(param[i].iso_chan->iso, BT_CONN_CONNECT);
1315 		bt_iso_chan_set_state(param[i].iso_chan, BT_ISO_CONNECT);
1316 	}
1317 
1318 	return 0;
1319 }
1320 
bt_iso_chan_disconnect(struct bt_iso_chan * chan)1321 int bt_iso_chan_disconnect(struct bt_iso_chan *chan)
1322 {
1323 	CHECKIF(!chan) {
1324 		BT_DBG("Invalid parameter: chan %p", chan);
1325 		return -EINVAL;
1326 	}
1327 
1328 	CHECKIF(chan->iso == NULL) {
1329 		BT_DBG("Channel has not been initialized in a CIG");
1330 		return -EINVAL;
1331 	}
1332 
1333 	if (chan->iso->iso.acl == NULL) {
1334 		BT_DBG("Channel is not connected");
1335 		return -ENOTCONN;
1336 	}
1337 
1338 	return bt_conn_disconnect(chan->iso, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
1339 }
1340 
bt_iso_server_register(struct bt_iso_server * server)1341 int bt_iso_server_register(struct bt_iso_server *server)
1342 {
1343 	CHECKIF(!server) {
1344 		BT_DBG("Invalid parameter: server %p", server);
1345 		return -EINVAL;
1346 	}
1347 
1348 	/* Check if controller is ISO capable */
1349 	if (!BT_FEAT_LE_CIS_PERIPHERAL(bt_dev.le.features)) {
1350 		return -ENOTSUP;
1351 	}
1352 
1353 	if (iso_server) {
1354 		return -EADDRINUSE;
1355 	}
1356 
1357 	if (!server->accept) {
1358 		return -EINVAL;
1359 	}
1360 
1361 	if (server->sec_level > BT_SECURITY_L3) {
1362 		return -EINVAL;
1363 	} else if (server->sec_level < BT_SECURITY_L1) {
1364 		/* Level 0 is only applicable for BR/EDR */
1365 		server->sec_level = BT_SECURITY_L1;
1366 	}
1367 
1368 	BT_DBG("%p", server);
1369 
1370 	iso_server = server;
1371 
1372 	return 0;
1373 }
1374 #endif /* CONFIG_BT_ISO_UNICAST */
1375 
1376 #if defined(CONFIG_BT_ISO_BROADCAST)
1377 
get_free_big(void)1378 static struct bt_iso_big *get_free_big(void)
1379 {
1380 	/* We can use the index in the `bigs` array as BIG handles, for both
1381 	 * broadcaster and receiver (even if the device is both!)
1382 	 */
1383 
1384 	for (int i = 0; i < ARRAY_SIZE(bigs); i++) {
1385 		if (!atomic_test_and_set_bit(bigs[i].flags, BT_BIG_INITIALIZED)) {
1386 			bigs[i].handle = i;
1387 			return &bigs[i];
1388 		}
1389 	}
1390 
1391 	BT_DBG("Could not allocate any more BIGs");
1392 
1393 	return NULL;
1394 }
1395 
big_lookup_flag(int bit)1396 static struct bt_iso_big *big_lookup_flag(int bit)
1397 {
1398 	for (int i = 0; i < ARRAY_SIZE(bigs); i++) {
1399 		if (atomic_test_bit(bigs[i].flags, bit)) {
1400 			return &bigs[i];
1401 		}
1402 	}
1403 
1404 	BT_DBG("No BIG with flag bit %d set", bit);
1405 
1406 	return NULL;
1407 }
1408 
cleanup_big(struct bt_iso_big * big)1409 static void cleanup_big(struct bt_iso_big *big)
1410 {
1411 	for (int i = 0; i < big->num_bis; i++) {
1412 		struct bt_iso_chan *bis = big->bis[i];
1413 
1414 		if (bis != NULL && bis->iso != NULL) {
1415 			bt_conn_unref(bis->iso);
1416 			bis->iso = NULL;
1417 		}
1418 	}
1419 
1420 	memset(big, 0, sizeof(*big));
1421 }
1422 
big_disconnect(struct bt_iso_big * big,uint8_t reason)1423 static void big_disconnect(struct bt_iso_big *big, uint8_t reason)
1424 {
1425 	for (int i = 0; i < big->num_bis; i++) {
1426 		big->bis[i]->iso->err = reason;
1427 
1428 		bt_iso_disconnected(big->bis[i]->iso);
1429 	}
1430 }
1431 
big_init_bis(struct bt_iso_big * big,bool broadcaster)1432 static int big_init_bis(struct bt_iso_big *big, bool broadcaster)
1433 {
1434 	for (int i = 0; i < big->num_bis; i++) {
1435 		struct bt_iso_chan *bis = big->bis[i];
1436 
1437 		if (!bis) {
1438 			BT_DBG("BIS was NULL");
1439 			return -EINVAL;
1440 		}
1441 
1442 		if (bis->iso) {
1443 			BT_DBG("BIS conn was already allocated");
1444 			return -EALREADY;
1445 		}
1446 
1447 		CHECKIF(bis->qos == NULL) {
1448 			BT_DBG("BIS QOS is NULL");
1449 			return -EINVAL;
1450 		}
1451 
1452 		if (broadcaster) {
1453 			CHECKIF(bis->qos->tx == NULL ||
1454 				!valid_chan_io_qos(bis->qos->tx, true)) {
1455 				BT_DBG("Invalid BIS QOS");
1456 				return -EINVAL;
1457 			}
1458 		} else {
1459 			CHECKIF(bis->qos->rx == NULL) {
1460 				BT_DBG("Invalid BIS QOS");
1461 				return -EINVAL;
1462 			}
1463 		}
1464 
1465 		bis->iso = iso_new();
1466 
1467 		if (!bis->iso) {
1468 			BT_ERR("Unable to allocate BIS connection");
1469 			return -ENOMEM;
1470 		}
1471 
1472 		bis->iso->iso.big_handle = big->handle;
1473 		bis->iso->iso.is_bis = true;
1474 		bis->iso->iso.bis_id = bt_conn_index(bis->iso);
1475 
1476 		bt_iso_chan_add(bis->iso, bis);
1477 	}
1478 
1479 	return 0;
1480 }
1481 
hci_le_create_big(struct bt_le_ext_adv * padv,struct bt_iso_big * big,struct bt_iso_big_create_param * param)1482 static int hci_le_create_big(struct bt_le_ext_adv *padv, struct bt_iso_big *big,
1483 			     struct bt_iso_big_create_param *param)
1484 {
1485 	struct bt_hci_cp_le_create_big *req;
1486 	struct bt_hci_cmd_state_set state;
1487 	struct net_buf *buf;
1488 	int err;
1489 	static struct bt_iso_chan_qos *qos;
1490 
1491 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_BIG, sizeof(*req));
1492 
1493 	if (!buf) {
1494 		return -ENOBUFS;
1495 	}
1496 
1497 	/* All BIS will share the same QOS */
1498 	qos = big->bis[0]->qos;
1499 
1500 	req = net_buf_add(buf, sizeof(*req));
1501 	req->big_handle = big->handle;
1502 	req->adv_handle = padv->handle;
1503 	req->num_bis = big->num_bis;
1504 	sys_put_le24(param->interval, req->sdu_interval);
1505 	req->max_sdu = sys_cpu_to_le16(qos->tx->sdu);
1506 	req->max_latency = sys_cpu_to_le16(param->latency);
1507 	req->rtn = qos->tx->rtn;
1508 	req->phy = qos->tx->phy;
1509 	req->packing = param->packing;
1510 	req->framing = param->framing;
1511 	req->encryption = param->encryption;
1512 	if (req->encryption) {
1513 		memcpy(req->bcode, param->bcode, sizeof(req->bcode));
1514 	} else {
1515 		memset(req->bcode, 0, sizeof(req->bcode));
1516 	}
1517 
1518 	bt_hci_cmd_state_set_init(buf, &state, big->flags, BT_BIG_PENDING, true);
1519 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_BIG, buf, NULL);
1520 
1521 	if (err) {
1522 		return err;
1523 	}
1524 
1525 	for (int i = 0; i < big->num_bis; i++) {
1526 		bt_iso_chan_set_state(big->bis[i], BT_ISO_CONNECT);
1527 	}
1528 	return err;
1529 }
1530 
bt_iso_big_create(struct bt_le_ext_adv * padv,struct bt_iso_big_create_param * param,struct bt_iso_big ** out_big)1531 int bt_iso_big_create(struct bt_le_ext_adv *padv, struct bt_iso_big_create_param *param,
1532 		      struct bt_iso_big **out_big)
1533 {
1534 	int err;
1535 	struct bt_iso_big *big;
1536 
1537 	if (!atomic_test_bit(padv->flags, BT_PER_ADV_PARAMS_SET)) {
1538 		BT_DBG("PA params not set; invalid adv object");
1539 		return -EINVAL;
1540 	}
1541 
1542 	CHECKIF(!param->bis_channels) {
1543 		BT_DBG("NULL BIS channels");
1544 		return -EINVAL;
1545 	}
1546 
1547 	CHECKIF(!param->num_bis) {
1548 		BT_DBG("Invalid number of BIS %u", param->num_bis);
1549 		return -EINVAL;
1550 	}
1551 
1552 	for (int i = 0; i < param->num_bis; i++) {
1553 		CHECKIF(param->bis_channels[i] == NULL) {
1554 			BT_DBG("NULL channel in bis_channels[%d]", i);
1555 			return -EINVAL;
1556 		}
1557 	}
1558 
1559 	CHECKIF(param->framing != BT_ISO_FRAMING_UNFRAMED &&
1560 		param->framing != BT_ISO_FRAMING_FRAMED) {
1561 		BT_DBG("Invalid framing parameter: %u", param->framing);
1562 		return -EINVAL;
1563 	}
1564 
1565 	CHECKIF(param->packing != BT_ISO_PACKING_SEQUENTIAL &&
1566 		param->packing != BT_ISO_PACKING_INTERLEAVED) {
1567 		BT_DBG("Invalid packing parameter: %u", param->packing);
1568 		return -EINVAL;
1569 	}
1570 
1571 	CHECKIF(param->num_bis > BT_ISO_MAX_GROUP_ISO_COUNT ||
1572 		param->num_bis > CONFIG_BT_ISO_MAX_CHAN) {
1573 		BT_DBG("num_bis (%u) shall be lower than: %u", param->num_bis,
1574 		       MAX(CONFIG_BT_ISO_MAX_CHAN, BT_ISO_MAX_GROUP_ISO_COUNT));
1575 		return -EINVAL;
1576 	}
1577 
1578 	CHECKIF(param->interval < BT_ISO_INTERVAL_MIN ||
1579 		param->interval > BT_ISO_INTERVAL_MAX) {
1580 		BT_DBG("Invalid interval: %u", param->interval);
1581 		return -EINVAL;
1582 	}
1583 
1584 	CHECKIF(param->latency < BT_ISO_LATENCY_MIN ||
1585 		param->latency > BT_ISO_LATENCY_MAX) {
1586 		BT_DBG("Invalid latency: %u", param->latency);
1587 		return -EINVAL;
1588 	}
1589 
1590 	big = get_free_big();
1591 
1592 	if (!big) {
1593 		return -ENOMEM;
1594 	}
1595 
1596 	big->bis = param->bis_channels;
1597 	big->num_bis = param->num_bis;
1598 
1599 	err = big_init_bis(big, true);
1600 	if (err) {
1601 		BT_DBG("Could not init BIG %d", err);
1602 		cleanup_big(big);
1603 		return err;
1604 	}
1605 
1606 	err = hci_le_create_big(padv, big, param);
1607 	if (err) {
1608 		BT_DBG("Could not create BIG %d", err);
1609 		cleanup_big(big);
1610 		return err;
1611 	}
1612 
1613 	*out_big = big;
1614 
1615 	return err;
1616 }
1617 
hci_le_terminate_big(struct bt_iso_big * big)1618 static int hci_le_terminate_big(struct bt_iso_big *big)
1619 {
1620 	struct bt_hci_cp_le_terminate_big *req;
1621 	struct net_buf *buf;
1622 
1623 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_TERMINATE_BIG, sizeof(*req));
1624 	if (!buf) {
1625 		return -ENOBUFS;
1626 	}
1627 
1628 	req = net_buf_add(buf, sizeof(*req));
1629 	req->big_handle = big->handle;
1630 	req->reason = BT_HCI_ERR_REMOTE_USER_TERM_CONN;
1631 
1632 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_TERMINATE_BIG, buf, NULL);
1633 }
1634 
hci_le_big_sync_term(struct bt_iso_big * big)1635 static int hci_le_big_sync_term(struct bt_iso_big *big)
1636 {
1637 	struct bt_hci_cp_le_big_terminate_sync *req;
1638 	struct bt_hci_rp_le_big_terminate_sync *evt;
1639 	struct net_buf *buf;
1640 	struct net_buf *rsp;
1641 	int err;
1642 
1643 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_BIG_TERMINATE_SYNC, sizeof(*req));
1644 	if (!buf) {
1645 		return -ENOBUFS;
1646 	}
1647 
1648 	req = net_buf_add(buf, sizeof(*req));
1649 	req->big_handle = big->handle;
1650 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_BIG_TERMINATE_SYNC, buf, &rsp);
1651 	if (err) {
1652 		return err;
1653 	}
1654 
1655 	evt = (struct bt_hci_rp_le_big_terminate_sync *)rsp->data;
1656 	if (evt->status || (evt->big_handle != big->handle)) {
1657 		err = -EIO;
1658 	}
1659 
1660 	net_buf_unref(rsp);
1661 
1662 	return err;
1663 }
1664 
bt_iso_big_terminate(struct bt_iso_big * big)1665 int bt_iso_big_terminate(struct bt_iso_big *big)
1666 {
1667 	int err;
1668 	bool broadcaster;
1669 
1670 	if (!atomic_test_bit(big->flags, BT_BIG_INITIALIZED) || !big->num_bis || !big->bis) {
1671 		BT_DBG("BIG not initialized");
1672 		return -EINVAL;
1673 	}
1674 
1675 	for (int i = 0; i < big->num_bis; i++) {
1676 		if (!big->bis[i]) {
1677 			BT_DBG("BIG BIS[%d] not initialized", i);
1678 			return -EINVAL;
1679 		}
1680 	}
1681 
1682 	/* They all have the same QOS dir so we can just check the first */
1683 	broadcaster = big->bis[0]->qos->tx ? true : false;
1684 
1685 	if (broadcaster) {
1686 		err = hci_le_terminate_big(big);
1687 
1688 		/* Wait for BT_HCI_EVT_LE_BIG_TERMINATE before cleaning up
1689 		 * the BIG in hci_le_big_terminate
1690 		 */
1691 		if (!err) {
1692 			for (int i = 0; i < big->num_bis; i++) {
1693 				bt_iso_chan_set_state(big->bis[i], BT_ISO_DISCONNECT);
1694 			}
1695 		}
1696 	} else {
1697 		err = hci_le_big_sync_term(big);
1698 
1699 		if (!err) {
1700 			big_disconnect(big, BT_HCI_ERR_LOCALHOST_TERM_CONN);
1701 			cleanup_big(big);
1702 		}
1703 	}
1704 
1705 	if (err) {
1706 		BT_DBG("Could not terminate BIG %d", err);
1707 	}
1708 
1709 	return err;
1710 }
1711 
hci_le_big_complete(struct net_buf * buf)1712 void hci_le_big_complete(struct net_buf *buf)
1713 {
1714 	struct bt_hci_evt_le_big_complete *evt = (void *)buf->data;
1715 	struct bt_iso_big *big;
1716 
1717 	if (evt->big_handle >= ARRAY_SIZE(bigs)) {
1718 		BT_WARN("Invalid BIG handle");
1719 
1720 		big = big_lookup_flag(BT_BIG_PENDING);
1721 		if (big) {
1722 			big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
1723 			cleanup_big(big);
1724 		}
1725 
1726 		return;
1727 	}
1728 
1729 	big = &bigs[evt->big_handle];
1730 	atomic_clear_bit(big->flags, BT_BIG_PENDING);
1731 
1732 	BT_DBG("BIG[%u] %p completed, status %u", big->handle, big, evt->status);
1733 
1734 	if (evt->status || evt->num_bis != big->num_bis) {
1735 		if (evt->status == BT_HCI_ERR_SUCCESS && evt->num_bis != big->num_bis) {
1736 			BT_ERR("Invalid number of BIS created, was %u expected %u",
1737 			       evt->num_bis, big->num_bis);
1738 		}
1739 		big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
1740 		cleanup_big(big);
1741 		return;
1742 	}
1743 
1744 	for (int i = 0; i < big->num_bis; i++) {
1745 		struct bt_iso_chan *bis = big->bis[i];
1746 
1747 		bis->iso->handle = sys_le16_to_cpu(evt->handle[i]);
1748 		bt_conn_set_state(bis->iso, BT_CONN_CONNECTED);
1749 	}
1750 }
1751 
hci_le_big_terminate(struct net_buf * buf)1752 void hci_le_big_terminate(struct net_buf *buf)
1753 {
1754 	struct bt_hci_evt_le_big_terminate *evt = (void *)buf->data;
1755 	struct bt_iso_big *big;
1756 
1757 	if (evt->big_handle >= ARRAY_SIZE(bigs)) {
1758 		BT_WARN("Invalid BIG handle");
1759 		return;
1760 	}
1761 
1762 	big = &bigs[evt->big_handle];
1763 
1764 	BT_DBG("BIG[%u] %p terminated", big->handle, big);
1765 
1766 	big_disconnect(big, evt->reason);
1767 	cleanup_big(big);
1768 }
1769 
hci_le_big_sync_established(struct net_buf * buf)1770 void hci_le_big_sync_established(struct net_buf *buf)
1771 {
1772 	struct bt_hci_evt_le_big_sync_established *evt = (void *)buf->data;
1773 	struct bt_iso_big *big;
1774 
1775 	if (evt->big_handle >= ARRAY_SIZE(bigs)) {
1776 		BT_WARN("Invalid BIG handle");
1777 		big = big_lookup_flag(BT_BIG_SYNCING);
1778 		if (big) {
1779 			big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
1780 			cleanup_big(big);
1781 		}
1782 
1783 		return;
1784 	}
1785 
1786 	big = &bigs[evt->big_handle];
1787 	atomic_clear_bit(big->flags, BT_BIG_SYNCING);
1788 
1789 	BT_DBG("BIG[%u] %p sync established, status %u", big->handle, big, evt->status);
1790 
1791 	if (evt->status || evt->num_bis != big->num_bis) {
1792 		if (evt->status == BT_HCI_ERR_SUCCESS && evt->num_bis != big->num_bis) {
1793 			BT_ERR("Invalid number of BIS synced, was %u expected %u",
1794 			       evt->num_bis, big->num_bis);
1795 		}
1796 		big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
1797 		cleanup_big(big);
1798 		return;
1799 	}
1800 
1801 	for (int i = 0; i < big->num_bis; i++) {
1802 		struct bt_iso_chan *bis = big->bis[i];
1803 		uint16_t bis_handle = sys_le16_to_cpu(evt->handle[i]);
1804 
1805 		bis->iso->handle = bis_handle;
1806 
1807 		bt_conn_set_state(bis->iso, BT_CONN_CONNECTED);
1808 	}
1809 
1810 	/* TODO: Deal with the rest of the fields in the event,
1811 	 * if it makes sense
1812 	 */
1813 }
1814 
hci_le_big_sync_lost(struct net_buf * buf)1815 void hci_le_big_sync_lost(struct net_buf *buf)
1816 {
1817 	struct bt_hci_evt_le_big_sync_lost *evt = (void *)buf->data;
1818 	struct bt_iso_big *big;
1819 
1820 	if (evt->big_handle >= ARRAY_SIZE(bigs)) {
1821 		BT_WARN("Invalid BIG handle");
1822 		return;
1823 	}
1824 
1825 	big = &bigs[evt->big_handle];
1826 
1827 	BT_DBG("BIG[%u] %p sync lost", big->handle, big);
1828 
1829 	big_disconnect(big, evt->reason);
1830 	cleanup_big(big);
1831 }
1832 
hci_le_big_create_sync(const struct bt_le_per_adv_sync * sync,struct bt_iso_big * big,const struct bt_iso_big_sync_param * param)1833 static int hci_le_big_create_sync(const struct bt_le_per_adv_sync *sync, struct bt_iso_big *big,
1834 				  const struct bt_iso_big_sync_param *param)
1835 {
1836 	struct bt_hci_cp_le_big_create_sync *req;
1837 	struct bt_hci_cmd_state_set state;
1838 	struct net_buf *buf;
1839 	int err;
1840 	uint8_t bit_idx = 0;
1841 
1842 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_BIG_CREATE_SYNC, sizeof(*req) + big->num_bis);
1843 	if (!buf) {
1844 		return -ENOBUFS;
1845 	}
1846 
1847 	req = net_buf_add(buf, sizeof(*req) + big->num_bis);
1848 	req->big_handle = big->handle;
1849 	req->sync_handle = sys_cpu_to_le16(sync->handle);
1850 	req->encryption = param->encryption;
1851 	if (req->encryption) {
1852 		memcpy(req->bcode, param->bcode, sizeof(req->bcode));
1853 	} else {
1854 		memset(req->bcode, 0, sizeof(req->bcode));
1855 	}
1856 	req->mse = param->mse;
1857 	req->sync_timeout = sys_cpu_to_le16(param->sync_timeout);
1858 	req->num_bis = big->num_bis;
1859 	/* Transform from bitfield to array */
1860 	for (int i = 1; i <= BT_ISO_MAX_GROUP_ISO_COUNT; i++) {
1861 		if (param->bis_bitfield & BIT(i)) {
1862 			if (bit_idx == big->num_bis) {
1863 				BT_DBG("BIG cannot contain %u BISes", bit_idx + 1);
1864 				return -EINVAL;
1865 			}
1866 			req->bis[bit_idx++] = i;
1867 		}
1868 	}
1869 
1870 	if (bit_idx != big->num_bis) {
1871 		BT_DBG("Number of bits in bis_bitfield (%u) doesn't match num_bis (%u)",
1872 		       bit_idx, big->num_bis);
1873 		return -EINVAL;
1874 	}
1875 
1876 	bt_hci_cmd_state_set_init(buf, &state, big->flags, BT_BIG_SYNCING, true);
1877 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_BIG_CREATE_SYNC, buf, NULL);
1878 
1879 	return err;
1880 }
1881 
bt_iso_big_sync(struct bt_le_per_adv_sync * sync,struct bt_iso_big_sync_param * param,struct bt_iso_big ** out_big)1882 int bt_iso_big_sync(struct bt_le_per_adv_sync *sync, struct bt_iso_big_sync_param *param,
1883 		    struct bt_iso_big **out_big)
1884 {
1885 	int err;
1886 	struct bt_iso_big *big;
1887 
1888 	if (!atomic_test_bit(sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1889 		BT_DBG("PA sync not synced");
1890 		return -EINVAL;
1891 	}
1892 
1893 	CHECKIF(param->mse > BT_ISO_SYNC_MSE_MAX) {
1894 		BT_DBG("Invalid MSE 0x%02x", param->mse);
1895 		return -EINVAL;
1896 	}
1897 
1898 	CHECKIF(param->sync_timeout < BT_ISO_SYNC_TIMEOUT_MIN ||
1899 		param->sync_timeout > BT_ISO_SYNC_TIMEOUT_MAX) {
1900 		BT_DBG("Invalid sync timeout 0x%04x", param->sync_timeout);
1901 		return -EINVAL;
1902 	}
1903 
1904 	CHECKIF(param->bis_bitfield <= BIT(0)) {
1905 		BT_DBG("Invalid BIS bitfield 0x%08x", param->bis_bitfield);
1906 		return -EINVAL;
1907 	}
1908 
1909 	CHECKIF(!param->bis_channels) {
1910 		BT_DBG("NULL BIS channels");
1911 		return -EINVAL;
1912 	}
1913 
1914 	CHECKIF(!param->num_bis) {
1915 		BT_DBG("Invalid number of BIS %u", param->num_bis);
1916 		return -EINVAL;
1917 	}
1918 
1919 	for (int i = 0; i < param->num_bis; i++) {
1920 		if (param->bis_channels[i] == NULL) {
1921 			BT_DBG("NULL channel in bis_channels[%d]", i);
1922 			return -EINVAL;
1923 		}
1924 	}
1925 
1926 	big = get_free_big();
1927 
1928 	if (!big) {
1929 		return -ENOMEM;
1930 	}
1931 
1932 	big->bis = param->bis_channels;
1933 	big->num_bis = param->num_bis;
1934 
1935 	err = big_init_bis(big, false);
1936 	if (err) {
1937 		BT_DBG("Could not init BIG %d", err);
1938 		cleanup_big(big);
1939 		return err;
1940 	}
1941 
1942 	err = hci_le_big_create_sync(sync, big, param);
1943 	if (err) {
1944 		BT_DBG("Could not create BIG sync %d", err);
1945 		cleanup_big(big);
1946 		return err;
1947 	}
1948 
1949 	for (int i = 0; i < big->num_bis; i++) {
1950 		bt_iso_chan_set_state(big->bis[i], BT_ISO_CONNECT);
1951 	}
1952 
1953 	*out_big = big;
1954 
1955 	return 0;
1956 }
1957 #endif /* defined(CONFIG_BT_ISO_BROADCAST) */
1958