1 /* Bluetooth ISO */
2
3 /*
4 * Copyright (c) 2020 Intel Corporation
5 * Copyright (c) 2021-2024 Nordic Semiconductor ASA
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string.h>
15
16 #include <zephyr/autoconf.h>
17 #include <zephyr/bluetooth/buf.h>
18 #include <zephyr/bluetooth/gap.h>
19 #include <zephyr/bluetooth/hci.h>
20 #include <zephyr/bluetooth/bluetooth.h>
21 #include <zephyr/bluetooth/conn.h>
22 #include <zephyr/bluetooth/hci_types.h>
23 #include <zephyr/bluetooth/iso.h>
24 #include <zephyr/kernel.h>
25 #include <zephyr/logging/log.h>
26 #include <zephyr/net_buf.h>
27 #include <zephyr/sys/__assert.h>
28 #include <zephyr/sys/atomic.h>
29 #include <zephyr/sys/byteorder.h>
30 #include <zephyr/sys/check.h>
31 #include <zephyr/sys/slist.h>
32 #include <zephyr/sys/util.h>
33 #include <zephyr/sys/util_macro.h>
34 #include <zephyr/sys_clock.h>
35 #include <zephyr/toolchain.h>
36
37 #include "common/assert.h"
38 #include "host/buf_view.h"
39 #include "host/hci_core.h"
40 #include "host/conn_internal.h"
41 #include "iso_internal.h"
42
43 LOG_MODULE_REGISTER(bt_iso, CONFIG_BT_ISO_LOG_LEVEL);
44
45 #if defined(CONFIG_BT_DEBUG_ISO_DATA)
46 #define BT_ISO_DATA_DBG(fmt, ...) LOG_DBG(fmt, ##__VA_ARGS__)
47 #else
48 #define BT_ISO_DATA_DBG(fmt, ...)
49 #endif /* CONFIG_BT_DEBUG_ISO_DATA */
50
51 #define iso_chan(_iso) ((_iso)->iso.chan);
52
53 #if defined(CONFIG_BT_ISO_RX)
54 static bt_iso_buf_rx_freed_cb_t buf_rx_freed_cb;
55
iso_rx_buf_destroy(struct net_buf * buf)56 static void iso_rx_buf_destroy(struct net_buf *buf)
57 {
58 net_buf_destroy(buf);
59
60 if (buf_rx_freed_cb) {
61 buf_rx_freed_cb();
62 }
63 }
64
65 NET_BUF_POOL_FIXED_DEFINE(iso_rx_pool, CONFIG_BT_ISO_RX_BUF_COUNT,
66 BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_RX_MTU), sizeof(struct iso_data),
67 iso_rx_buf_destroy);
68
69 static struct bt_iso_recv_info iso_info_data[CONFIG_BT_ISO_RX_BUF_COUNT];
70 #define iso_info(buf) (&iso_info_data[net_buf_id(buf)])
71 #endif /* CONFIG_BT_ISO_RX */
72
73 struct bt_conn iso_conns[CONFIG_BT_ISO_MAX_CHAN];
74
75 /* TODO: Allow more than one server? */
76 #if defined(CONFIG_BT_ISO_CENTRAL)
77 struct bt_iso_cig cigs[CONFIG_BT_ISO_MAX_CIG];
78
79 static struct bt_iso_cig *get_cig(const struct bt_iso_chan *iso_chan);
80 static void bt_iso_remove_data_path(struct bt_conn *iso);
81 static int hci_le_create_cis(const struct bt_iso_connect_param *param, size_t count);
82
83 #endif /* CONFIG_BT_ISO_CENTRAL */
84
85 #if defined(CONFIG_BT_ISO_PERIPHERAL)
86 static struct bt_iso_server *iso_server;
87
88 static struct bt_conn *bt_conn_add_iso(struct bt_conn *acl);
89 #endif /* CONFIG_BT_ISO_PERIPHERAL */
90
91 #if defined(CONFIG_BT_ISO_BROADCAST)
92 struct bt_iso_big bigs[CONFIG_BT_ISO_MAX_BIG];
93
94 static struct bt_iso_big *lookup_big_by_handle(uint8_t big_handle);
95 #endif /* CONFIG_BT_ISO_BROADCAST */
96
bt_iso_sent_cb(struct bt_conn * iso,void * user_data,int err)97 static void bt_iso_sent_cb(struct bt_conn *iso, void *user_data, int err)
98 {
99 #if defined(CONFIG_BT_ISO_TX)
100 struct bt_iso_chan *chan = iso->iso.chan;
101 struct bt_iso_chan_ops *ops;
102
103 __ASSERT(chan != NULL, "NULL chan for iso %p", iso);
104
105 ops = chan->ops;
106
107 if (!err && ops != NULL && ops->sent != NULL) {
108 ops->sent(chan);
109 }
110 #endif /* CONFIG_BT_ISO_TX */
111 }
112
hci_iso(struct net_buf * buf)113 void hci_iso(struct net_buf *buf)
114 {
115 struct bt_hci_iso_hdr *hdr;
116 uint16_t handle, len;
117 struct bt_conn *iso;
118 uint8_t flags;
119
120 BT_ISO_DATA_DBG("buf %p", buf);
121
122 if (buf->len < sizeof(*hdr)) {
123 LOG_ERR("Invalid HCI ISO packet size (%u)", buf->len);
124 net_buf_unref(buf);
125 return;
126 }
127
128 hdr = net_buf_pull_mem(buf, sizeof(*hdr));
129 len = bt_iso_hdr_len(sys_le16_to_cpu(hdr->len));
130 handle = sys_le16_to_cpu(hdr->handle);
131 flags = bt_iso_flags(handle);
132
133 iso(buf)->handle = bt_iso_handle(handle);
134 iso(buf)->index = BT_CONN_INDEX_INVALID;
135
136 BT_ISO_DATA_DBG("handle %u len %u flags %u", iso(buf)->handle, len, flags);
137
138 if (buf->len != len) {
139 LOG_ERR("ISO data length mismatch (%u != %u)", buf->len, len);
140 net_buf_unref(buf);
141 return;
142 }
143
144 iso = bt_conn_lookup_handle(iso(buf)->handle, BT_CONN_TYPE_ISO);
145 if (iso == NULL) {
146 LOG_ERR("Unable to find conn for handle %u", iso(buf)->handle);
147 net_buf_unref(buf);
148 return;
149 }
150
151 iso(buf)->index = bt_conn_index(iso);
152
153 bt_conn_recv(iso, buf, flags);
154 bt_conn_unref(iso);
155 }
156
157 /* Pull data from the ISO layer */
158 static struct net_buf *iso_data_pull(struct bt_conn *conn, size_t amount, size_t *length);
159
160 /* Returns true if the ISO layer has data to send on this conn */
161 static bool iso_has_data(struct bt_conn *conn);
162
iso_get_and_clear_cb(struct bt_conn * conn,struct net_buf * buf,bt_conn_tx_cb_t * cb,void ** ud)163 static void iso_get_and_clear_cb(struct bt_conn *conn, struct net_buf *buf, bt_conn_tx_cb_t *cb,
164 void **ud)
165 {
166 if (IS_ENABLED(CONFIG_BT_ISO_TX)) {
167 *cb = bt_iso_sent_cb;
168 } else {
169 *cb = NULL;
170 }
171
172 *ud = NULL;
173 }
174
iso_new(void)175 static struct bt_conn *iso_new(void)
176 {
177 struct bt_conn *iso = bt_conn_new(iso_conns, ARRAY_SIZE(iso_conns));
178
179 if (iso) {
180 iso->type = BT_CONN_TYPE_ISO;
181 iso->tx_data_pull = iso_data_pull;
182 iso->get_and_clear_cb = iso_get_and_clear_cb;
183 iso->has_data = iso_has_data;
184 } else {
185 LOG_DBG("Could not create new ISO");
186 }
187
188 return iso;
189 }
190
hci_le_setup_iso_data_path(const struct bt_conn * iso,uint8_t dir,const struct bt_iso_chan_path * path)191 static int hci_le_setup_iso_data_path(const struct bt_conn *iso, uint8_t dir,
192 const struct bt_iso_chan_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 __ASSERT(dir == BT_HCI_DATAPATH_DIR_HOST_TO_CTLR || dir == BT_HCI_DATAPATH_DIR_CTLR_TO_HOST,
201 "invalid ISO data path dir: %u", dir);
202
203 if ((path->cc == NULL && path->cc_len != 0)) {
204 LOG_DBG("Invalid ISO data path CC: %p %u", path->cc, path->cc_len);
205
206 return -EINVAL;
207 }
208
209 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SETUP_ISO_PATH, sizeof(*cp) + path->cc_len);
210 if (!buf) {
211 return -ENOBUFS;
212 }
213
214 cp = net_buf_add(buf, sizeof(*cp));
215 cp->handle = sys_cpu_to_le16(iso->handle);
216 cp->path_dir = dir;
217 cp->path_id = path->pid;
218 cp->codec_id.coding_format = path->format;
219 cp->codec_id.company_id = sys_cpu_to_le16(path->cid);
220 cp->codec_id.vs_codec_id = sys_cpu_to_le16(path->vid);
221 sys_put_le24(path->delay, cp->controller_delay);
222 cp->codec_config_len = path->cc_len;
223 cc = net_buf_add(buf, path->cc_len);
224 if (path->cc_len) {
225 memcpy(cc, path->cc, path->cc_len);
226 }
227 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SETUP_ISO_PATH, buf, &rsp);
228 if (err) {
229 return err;
230 }
231
232 rp = (void *)rsp->data;
233 if (rp->status || (sys_le16_to_cpu(rp->handle) != iso->handle)) {
234 err = -EIO;
235 }
236
237 net_buf_unref(rsp);
238
239 return err;
240 }
241
bt_iso_chan_add(struct bt_conn * iso,struct bt_iso_chan * chan)242 static void bt_iso_chan_add(struct bt_conn *iso, struct bt_iso_chan *chan)
243 {
244 /* Attach ISO channel to the connection */
245 chan->iso = iso;
246 iso->iso.chan = chan;
247 k_fifo_init(&iso->iso.txq);
248
249 LOG_DBG("iso %p chan %p", iso, chan);
250 }
251
bt_iso_setup_data_path(struct bt_iso_chan * chan)252 static int bt_iso_setup_data_path(struct bt_iso_chan *chan)
253 {
254 int err;
255 struct bt_iso_chan_path default_hci_path = {.pid = BT_ISO_DATA_PATH_HCI,
256 .format = BT_HCI_CODING_FORMAT_TRANSPARENT,
257 .cc_len = 0x00};
258 struct bt_iso_chan_path *out_path = NULL;
259 struct bt_iso_chan_path *in_path = NULL;
260 struct bt_iso_chan_io_qos *tx_qos;
261 struct bt_iso_chan_io_qos *rx_qos;
262 struct bt_conn *iso;
263 uint8_t dir;
264
265 iso = chan->iso;
266
267 tx_qos = chan->qos->tx;
268 rx_qos = chan->qos->rx;
269
270 /* The following code sets the in and out paths for ISO data.
271 * If the application provides a path for a direction (tx/rx) we use
272 * that, otherwise we simply fall back to HCI.
273 *
274 * If the direction is not set (by whether tx_qos or rx_qos is NULL),
275 * then we fallback to the HCI path object, but we disable the direction
276 * in the controller.
277 */
278
279 if (tx_qos != NULL && iso->iso.info.can_send) {
280 if (tx_qos->path != NULL) { /* Use application path */
281 in_path = tx_qos->path;
282 } else { /* else fallback to HCI path */
283 in_path = &default_hci_path;
284 }
285 }
286
287 if (rx_qos != NULL && iso->iso.info.can_recv) {
288 if (rx_qos->path != NULL) { /* Use application path */
289 out_path = rx_qos->path;
290 } else { /* else fallback to HCI path */
291 out_path = &default_hci_path;
292 }
293 }
294
295 __ASSERT(in_path || out_path, "At least one path shall be shell: in %p out %p", in_path,
296 out_path);
297
298 if (IS_ENABLED(CONFIG_BT_ISO_BROADCASTER) &&
299 iso->iso.info.type == BT_ISO_CHAN_TYPE_BROADCASTER && in_path) {
300 dir = BT_HCI_DATAPATH_DIR_HOST_TO_CTLR;
301 err = hci_le_setup_iso_data_path(iso, dir, in_path);
302 if (err != 0) {
303 LOG_DBG("Failed to set broadcaster data path: %d", err);
304 }
305
306 return err;
307 } else if (IS_ENABLED(CONFIG_BT_ISO_SYNC_RECEIVER) &&
308 iso->iso.info.type == BT_ISO_CHAN_TYPE_SYNC_RECEIVER && out_path) {
309 dir = BT_HCI_DATAPATH_DIR_CTLR_TO_HOST;
310 err = hci_le_setup_iso_data_path(iso, dir, out_path);
311 if (err != 0) {
312 LOG_DBG("Failed to set sync receiver data path: %d", err);
313 }
314
315 return err;
316 } else if (IS_ENABLED(CONFIG_BT_ISO_UNICAST) &&
317 iso->iso.info.type == BT_ISO_CHAN_TYPE_CONNECTED) {
318 if (in_path != NULL) {
319 /* Enable TX */
320 dir = BT_HCI_DATAPATH_DIR_HOST_TO_CTLR;
321 err = hci_le_setup_iso_data_path(iso, dir, in_path);
322 if (err) {
323 LOG_DBG("Failed to setup host-to-ctrl path: %d", err);
324 return err;
325 }
326 }
327
328 if (out_path != NULL) {
329 /* Enable RX */
330 dir = BT_HCI_DATAPATH_DIR_CTLR_TO_HOST;
331 err = hci_le_setup_iso_data_path(iso, dir, out_path);
332 if (err) {
333 LOG_DBG("Failed to setup ctlr-to-host path: %d", err);
334 return err;
335 }
336 }
337
338 return 0;
339 } else {
340 __ASSERT(false, "Invalid iso.info.type: %u", iso->iso.info.type);
341 return -EINVAL;
342 }
343 }
344
bt_iso_connected(struct bt_conn * iso)345 void bt_iso_connected(struct bt_conn *iso)
346 {
347 struct bt_iso_chan *chan;
348 int err;
349
350 if (iso == NULL || iso->type != BT_CONN_TYPE_ISO) {
351 LOG_DBG("Invalid parameters: iso %p iso->type %u", iso, iso ? iso->type : 0);
352 return;
353 }
354
355 LOG_DBG("%p", iso);
356
357 chan = iso_chan(iso);
358 if (chan == NULL) {
359 LOG_ERR("Could not lookup chan from connected ISO");
360 return;
361 }
362
363 err = bt_iso_setup_data_path(chan);
364 if (err != 0) {
365 if (false) {
366
367 #if defined(CONFIG_BT_ISO_BROADCAST)
368 } else if (iso->iso.info.type == BT_ISO_CHAN_TYPE_BROADCASTER ||
369 iso->iso.info.type == BT_ISO_CHAN_TYPE_SYNC_RECEIVER) {
370 struct bt_iso_big *big;
371
372 big = lookup_big_by_handle(iso->iso.big_handle);
373
374 err = bt_iso_big_terminate(big);
375 if (err != 0) {
376 LOG_ERR("Could not terminate BIG: %d", err);
377 }
378 #endif /* CONFIG_BT_ISO_BROADCAST */
379
380 } else if (IS_ENABLED(CONFIG_BT_ISO_UNICAST) &&
381 iso->iso.info.type == BT_ISO_CHAN_TYPE_CONNECTED) {
382 bt_conn_disconnect(iso, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
383 } else {
384 __ASSERT(false, "Invalid iso.info.type: %u", iso->iso.info.type);
385 }
386 return;
387 }
388
389 bt_iso_chan_set_state(chan, BT_ISO_STATE_CONNECTED);
390
391 if (chan->ops->connected) {
392 chan->ops->connected(chan);
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 LOG_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_STATE_DISCONNECTED);
403 bt_conn_set_state(chan->iso, BT_CONN_DISCONNECT_COMPLETE);
404
405 /* The peripheral does not have the concept of a CIG, so once a CIS
406 * disconnects it is completely freed by unref'ing it
407 */
408 if (IS_ENABLED(CONFIG_BT_ISO_UNICAST) &&
409 chan->iso->iso.info.type == BT_ISO_CHAN_TYPE_CONNECTED) {
410 bt_iso_cleanup_acl(chan->iso);
411
412 if (chan->iso->role == BT_HCI_ROLE_PERIPHERAL) {
413 bt_conn_unref(chan->iso);
414 chan->iso = NULL;
415 #if defined(CONFIG_BT_ISO_CENTRAL)
416 } else {
417 /* ISO data paths are automatically removed when the
418 * peripheral disconnects, so we only need to
419 * move it for the central
420 */
421 bt_iso_remove_data_path(chan->iso);
422 bool is_chan_connected;
423 struct bt_iso_cig *cig;
424 struct bt_iso_chan *cis_chan;
425
426 /* Update CIG state */
427 cig = get_cig(chan);
428 __ASSERT(cig != NULL, "CIG was NULL");
429
430 is_chan_connected = false;
431 SYS_SLIST_FOR_EACH_CONTAINER(&cig->cis_channels, cis_chan, node) {
432 if (cis_chan->state == BT_ISO_STATE_CONNECTED ||
433 cis_chan->state == BT_ISO_STATE_CONNECTING) {
434 is_chan_connected = true;
435 break;
436 }
437 }
438
439 if (!is_chan_connected) {
440 cig->state = BT_ISO_CIG_STATE_INACTIVE;
441 }
442 #endif /* CONFIG_BT_ISO_CENTRAL */
443 }
444 }
445
446 if (chan->ops->disconnected) {
447 chan->ops->disconnected(chan, reason);
448 }
449 }
450
bt_iso_disconnected(struct bt_conn * iso)451 void bt_iso_disconnected(struct bt_conn *iso)
452 {
453 struct bt_iso_chan *chan;
454
455 if (iso == NULL || iso->type != BT_CONN_TYPE_ISO) {
456 LOG_DBG("Invalid parameters: iso %p iso->type %u", iso, iso ? iso->type : 0);
457 return;
458 }
459
460 LOG_DBG("%p", iso);
461
462 chan = iso_chan(iso);
463 if (chan == NULL) {
464 LOG_ERR("Could not lookup chan from disconnected ISO");
465 return;
466 }
467
468 bt_iso_chan_disconnected(chan, iso->err);
469 }
470
471 #if defined(CONFIG_BT_ISO_LOG_LEVEL_DBG)
bt_iso_chan_state_str(uint8_t state)472 const char *bt_iso_chan_state_str(uint8_t state)
473 {
474 switch (state) {
475 case BT_ISO_STATE_DISCONNECTED:
476 return "disconnected";
477 case BT_ISO_STATE_CONNECTING:
478 return "connecting";
479 case BT_ISO_STATE_ENCRYPT_PENDING:
480 return "encryption pending";
481 case BT_ISO_STATE_CONNECTED:
482 return "connected";
483 case BT_ISO_STATE_DISCONNECTING:
484 return "disconnecting";
485 default:
486 return "unknown";
487 }
488 }
489
bt_iso_chan_set_state_debug(struct bt_iso_chan * chan,enum bt_iso_state state,const char * func,int line)490 void bt_iso_chan_set_state_debug(struct bt_iso_chan *chan, enum bt_iso_state state,
491 const char *func, int line)
492 {
493 LOG_DBG("chan %p iso %p %s -> %s", chan, chan->iso, bt_iso_chan_state_str(chan->state),
494 bt_iso_chan_state_str(state));
495
496 /* check transitions validness */
497 switch (state) {
498 case BT_ISO_STATE_DISCONNECTED:
499 /* regardless of old state always allows this states */
500 break;
501 case BT_ISO_STATE_ENCRYPT_PENDING:
502 __fallthrough;
503 case BT_ISO_STATE_CONNECTING:
504 if (chan->state != BT_ISO_STATE_DISCONNECTED) {
505 LOG_WRN("%s()%d: invalid transition", func, line);
506 }
507 break;
508 case BT_ISO_STATE_CONNECTED:
509 if (chan->state != BT_ISO_STATE_CONNECTING) {
510 LOG_WRN("%s()%d: invalid transition", func, line);
511 }
512 break;
513 case BT_ISO_STATE_DISCONNECTING:
514 if (chan->state != BT_ISO_STATE_CONNECTING &&
515 chan->state != BT_ISO_STATE_CONNECTED) {
516 LOG_WRN("%s()%d: invalid transition", func, line);
517 }
518 break;
519 default:
520 LOG_ERR("%s()%d: unknown (%u) state was set", func, line, state);
521 return;
522 }
523
524 chan->state = state;
525 }
526 #else
bt_iso_chan_set_state(struct bt_iso_chan * chan,enum bt_iso_state state)527 void bt_iso_chan_set_state(struct bt_iso_chan *chan, enum bt_iso_state state)
528 {
529 chan->state = state;
530 }
531 #endif /* CONFIG_BT_ISO_LOG_LEVEL_DBG */
532
bt_iso_chan_get_info(const struct bt_iso_chan * chan,struct bt_iso_info * info)533 int bt_iso_chan_get_info(const struct bt_iso_chan *chan, struct bt_iso_info *info)
534 {
535 CHECKIF(chan == NULL) {
536 LOG_DBG("chan is NULL");
537 return -EINVAL;
538 }
539
540 CHECKIF(chan->iso == NULL) {
541 LOG_DBG("chan->iso is NULL");
542 return -EINVAL;
543 }
544
545 CHECKIF(info == NULL) {
546 LOG_DBG("info is NULL");
547 return -EINVAL;
548 }
549
550 (void)memcpy(info, &chan->iso->iso.info, sizeof(*info));
551
552 return 0;
553 }
554
555 #if defined(CONFIG_BT_ISO_RX)
bt_iso_get_rx(k_timeout_t timeout)556 struct net_buf *bt_iso_get_rx(k_timeout_t timeout)
557 {
558 struct net_buf *buf = net_buf_alloc(&iso_rx_pool, timeout);
559
560 if (buf) {
561 net_buf_reserve(buf, BT_BUF_RESERVE);
562 bt_buf_set_type(buf, BT_BUF_ISO_IN);
563 }
564
565 return buf;
566 }
567
bt_iso_buf_rx_freed_cb_set(bt_iso_buf_rx_freed_cb_t cb)568 void bt_iso_buf_rx_freed_cb_set(bt_iso_buf_rx_freed_cb_t cb)
569 {
570 buf_rx_freed_cb = cb;
571 }
572
bt_iso_recv(struct bt_conn * iso,struct net_buf * buf,uint8_t flags)573 void bt_iso_recv(struct bt_conn *iso, struct net_buf *buf, uint8_t flags)
574 {
575 struct bt_hci_iso_sdu_hdr *hdr;
576 struct bt_iso_chan *chan;
577 uint8_t pb, ts;
578 uint16_t len, pkt_seq_no;
579
580 pb = bt_iso_flags_pb(flags);
581 ts = bt_iso_flags_ts(flags);
582
583 BT_ISO_DATA_DBG("handle %u len %u flags 0x%02x pb 0x%02x ts 0x%02x", iso->handle, buf->len,
584 flags, pb, ts);
585
586 /* When the PB_Flag does not equal BT_ISO_START or BT_ISO_SINGLE,
587 * the fields Time_Stamp, Packet_Sequence_Number, Packet_Status_Flag
588 * and ISO_SDU_Length are omitted from the HCI ISO Data packet.
589 */
590 switch (pb) {
591 case BT_ISO_START:
592 case BT_ISO_SINGLE:
593 iso_info(buf)->flags = 0;
594
595 /* The ISO_Data_Load field contains either the first fragment
596 * of an SDU or a complete SDU.
597 */
598 if (ts) {
599 struct bt_hci_iso_sdu_ts_hdr *ts_hdr;
600
601 ts_hdr = net_buf_pull_mem(buf, sizeof(*ts_hdr));
602 iso_info(buf)->ts = sys_le32_to_cpu(ts_hdr->ts);
603
604 hdr = &ts_hdr->sdu;
605 iso_info(buf)->flags |= BT_ISO_FLAGS_TS;
606 } else {
607 hdr = net_buf_pull_mem(buf, sizeof(*hdr));
608 /* TODO: Generate a timestamp? */
609 iso_info(buf)->ts = 0x00000000;
610 }
611
612 len = sys_le16_to_cpu(hdr->slen);
613 flags = bt_iso_pkt_flags(len);
614 len = bt_iso_pkt_len(len);
615 pkt_seq_no = sys_le16_to_cpu(hdr->sn);
616 iso_info(buf)->seq_num = pkt_seq_no;
617 if (flags == BT_ISO_DATA_VALID) {
618 iso_info(buf)->flags |= BT_ISO_FLAGS_VALID;
619 } else if (flags == BT_ISO_DATA_INVALID) {
620 iso_info(buf)->flags |= BT_ISO_FLAGS_ERROR;
621 } else if (flags == BT_ISO_DATA_NOP) {
622 iso_info(buf)->flags |= BT_ISO_FLAGS_LOST;
623 } else {
624 LOG_WRN("Invalid ISO packet status flag: %u", flags);
625 iso_info(buf)->flags = 0;
626 }
627
628 BT_ISO_DATA_DBG("%s, len %u total %u flags 0x%02x timestamp %u",
629 pb == BT_ISO_START ? "Start" : "Single", buf->len, len, flags,
630 iso_info(buf)->ts);
631
632 if (iso->rx) {
633 LOG_ERR("Unexpected ISO %s fragment",
634 pb == BT_ISO_START ? "Start" : "Single");
635 bt_conn_reset_rx_state(iso);
636 }
637
638 iso->rx = buf;
639 iso->rx_len = len - buf->len;
640 if (iso->rx_len) {
641 /* if iso->rx_len then package is longer than the
642 * buf->len and cannot fit in a SINGLE package
643 */
644 if (pb == BT_ISO_SINGLE) {
645 LOG_ERR("Unexpected ISO single fragment");
646 bt_conn_reset_rx_state(iso);
647 }
648 return;
649 }
650 break;
651
652 case BT_ISO_CONT:
653 /* The ISO_Data_Load field contains a continuation fragment of
654 * an SDU.
655 */
656 if (!iso->rx) {
657 LOG_ERR("Unexpected ISO continuation fragment");
658 net_buf_unref(buf);
659 return;
660 }
661
662 BT_ISO_DATA_DBG("Cont, len %u rx_len %u", buf->len, iso->rx_len);
663
664 if (buf->len > net_buf_tailroom(iso->rx)) {
665 LOG_ERR("Not enough buffer space for ISO data");
666 bt_conn_reset_rx_state(iso);
667 net_buf_unref(buf);
668 return;
669 }
670
671 net_buf_add_mem(iso->rx, buf->data, buf->len);
672 iso->rx_len -= buf->len;
673 net_buf_unref(buf);
674 return;
675
676 case BT_ISO_END:
677 /* The ISO_Data_Load field contains the last fragment of an
678 * SDU.
679 */
680 BT_ISO_DATA_DBG("End, len %u rx_len %u", buf->len, iso->rx_len);
681
682 if (iso->rx == NULL) {
683 LOG_ERR("Unexpected ISO end fragment");
684 net_buf_unref(buf);
685 return;
686 }
687
688 if (buf->len > net_buf_tailroom(iso->rx)) {
689 LOG_ERR("Not enough buffer space for ISO data");
690 bt_conn_reset_rx_state(iso);
691 net_buf_unref(buf);
692 return;
693 }
694
695 (void)net_buf_add_mem(iso->rx, buf->data, buf->len);
696 iso->rx_len -= buf->len;
697 net_buf_unref(buf);
698
699 break;
700 default:
701 LOG_ERR("Unexpected ISO pb flags (0x%02x)", pb);
702 bt_conn_reset_rx_state(iso);
703 net_buf_unref(buf);
704 return;
705 }
706
707 chan = iso_chan(iso);
708 if (chan == NULL) {
709 LOG_ERR("Could not lookup chan from receiving ISO");
710 } else if (chan->ops->recv != NULL) {
711 chan->ops->recv(chan, iso_info(iso->rx), iso->rx);
712 }
713
714 bt_conn_reset_rx_state(iso);
715 }
716 #endif /* CONFIG_BT_ISO_RX */
717
iso_has_data(struct bt_conn * conn)718 static bool iso_has_data(struct bt_conn *conn)
719 {
720 #if defined(CONFIG_BT_ISO_TX)
721 return !k_fifo_is_empty(&conn->iso.txq);
722 #else /* !CONFIG_BT_ISO_TX */
723 return false;
724 #endif /* CONFIG_BT_ISO_TX */
725 }
726
iso_data_pull(struct bt_conn * conn,size_t amount,size_t * length)727 static struct net_buf *iso_data_pull(struct bt_conn *conn, size_t amount, size_t *length)
728 {
729 #if defined(CONFIG_BT_ISO_TX)
730 BT_ISO_DATA_DBG("conn %p amount %d", conn, amount);
731
732 /* Leave the PDU buffer in the queue until we have sent all its
733 * fragments.
734 */
735 struct net_buf *frag = k_fifo_peek_head(&conn->iso.txq);
736
737 if (!frag) {
738 BT_ISO_DATA_DBG("signaled ready but no frag available");
739 /* Service other connections */
740 bt_tx_irq_raise();
741
742 return NULL;
743 }
744
745 if (conn->iso.chan->state != BT_ISO_STATE_CONNECTED) {
746 __maybe_unused struct net_buf *b = k_fifo_get(&conn->iso.txq, K_NO_WAIT);
747
748 LOG_DBG("channel has been disconnected");
749 __ASSERT_NO_MSG(b == frag);
750
751 net_buf_unref(b);
752
753 /* Service other connections */
754 bt_tx_irq_raise();
755
756 return NULL;
757 }
758
759 if (bt_buf_has_view(frag)) {
760 /* This should not happen. conn.c should wait until the view is
761 * destroyed before requesting more data.
762 */
763 LOG_DBG("already have view");
764 return NULL;
765 }
766
767 bool last_frag = amount >= frag->len;
768
769 if (last_frag) {
770 __maybe_unused struct net_buf *b = k_fifo_get(&conn->iso.txq, K_NO_WAIT);
771
772 BT_ISO_DATA_DBG("last frag, pop buf");
773 __ASSERT_NO_MSG(b == frag);
774 }
775
776 *length = frag->len;
777
778 return frag;
779 #else
780 return NULL;
781 #endif
782 }
783
784 #if defined(CONFIG_BT_ISO_TX)
iso_chan_max_data_len(const struct bt_iso_chan * chan)785 static uint16_t iso_chan_max_data_len(const struct bt_iso_chan *chan)
786 {
787 size_t max_controller_data_len;
788 uint16_t max_data_len;
789
790 if (chan->qos->tx == NULL) {
791 return 0;
792 }
793
794 max_data_len = chan->qos->tx->sdu;
795
796 /* Ensure that the SDU fits when using all the buffers */
797 max_controller_data_len = bt_dev.le.iso_mtu * bt_dev.le.iso_limit;
798
799 /* Update the max_data_len to take the max_controller_data_len into account */
800 max_data_len = MIN(max_data_len, max_controller_data_len);
801
802 return max_data_len;
803 }
804
conn_iso_send(struct bt_conn * conn,struct net_buf * buf,enum bt_iso_timestamp has_ts)805 int conn_iso_send(struct bt_conn *conn, struct net_buf *buf, enum bt_iso_timestamp has_ts)
806 {
807 if (buf->user_data_size < CONFIG_BT_CONN_TX_USER_DATA_SIZE) {
808 LOG_ERR("not enough room in user_data %d < %d pool %u", buf->user_data_size,
809 CONFIG_BT_CONN_TX_USER_DATA_SIZE, buf->pool_id);
810 return -EINVAL;
811 }
812
813 k_fifo_put(&conn->iso.txq, buf);
814 BT_ISO_DATA_DBG("%p put on list", buf);
815
816 /* only one ISO channel per conn-object */
817 bt_conn_data_ready(conn);
818
819 return 0;
820 }
821
validate_send(const struct bt_iso_chan * chan,const struct net_buf * buf,uint8_t hdr_size)822 static int validate_send(const struct bt_iso_chan *chan, const struct net_buf *buf,
823 uint8_t hdr_size)
824 {
825 const struct bt_conn *iso_conn;
826 uint16_t max_data_len;
827
828 CHECKIF(!chan || !buf) {
829 LOG_DBG("Invalid parameters: chan %p buf %p", chan, buf);
830 return -EINVAL;
831 }
832
833 BT_ISO_DATA_DBG("chan %p len %zu", chan, net_buf_frags_len(buf));
834
835 if (chan->state != BT_ISO_STATE_CONNECTED) {
836 LOG_DBG("Channel %p not connected", chan);
837 return -ENOTCONN;
838 }
839
840 iso_conn = chan->iso;
841 if (!iso_conn->iso.info.can_send) {
842 LOG_DBG("Channel %p not able to send", chan);
843 return -EINVAL;
844 }
845
846 if (net_buf_headroom(buf) != BT_BUF_ISO_SIZE(0)) {
847 LOG_DBG("Buffer headroom (%d) != BT_BUF_ISO_SIZE(0) (%d) bytes",
848 net_buf_headroom(buf), BT_BUF_ISO_SIZE(0));
849
850 /* DO NOT remove this check. We rely on precise headroom further
851 * below in the stack to determine if `buf` contains a timestamp
852 * field or not. See conn.c:contains_iso_timestamp.
853 */
854
855 return -EMSGSIZE;
856 }
857
858 if (buf->size < hdr_size) {
859 LOG_DBG("Channel %p cannot send ISO packet with buffer size %u", chan, buf->size);
860
861 return -EMSGSIZE;
862 }
863
864 max_data_len = iso_chan_max_data_len(chan);
865 if (buf->len > max_data_len) {
866 LOG_DBG("Channel %p cannot send %u octets, maximum %u", chan, buf->len,
867 max_data_len);
868 return -EMSGSIZE;
869 }
870
871 return 0;
872 }
873
bt_iso_chan_send(struct bt_iso_chan * chan,struct net_buf * buf,uint16_t seq_num)874 int bt_iso_chan_send(struct bt_iso_chan *chan, struct net_buf *buf, uint16_t seq_num)
875 {
876 struct bt_hci_iso_sdu_hdr *hdr;
877 struct bt_conn *iso_conn;
878 int err;
879
880 err = validate_send(chan, buf, BT_HCI_ISO_SDU_HDR_SIZE);
881 if (err != 0) {
882 return err;
883 }
884
885 BT_ISO_DATA_DBG("chan %p len %zu", chan, net_buf_frags_len(buf));
886
887 hdr = net_buf_push(buf, sizeof(*hdr));
888 hdr->sn = sys_cpu_to_le16(seq_num);
889 hdr->slen = sys_cpu_to_le16(
890 bt_iso_pkt_len_pack(net_buf_frags_len(buf) - sizeof(*hdr), BT_ISO_DATA_VALID));
891
892 iso_conn = chan->iso;
893
894 BT_ISO_DATA_DBG("send-iso (no ts)");
895 return conn_iso_send(iso_conn, buf, BT_ISO_TS_ABSENT);
896 }
897
bt_iso_chan_send_ts(struct bt_iso_chan * chan,struct net_buf * buf,uint16_t seq_num,uint32_t ts)898 int bt_iso_chan_send_ts(struct bt_iso_chan *chan, struct net_buf *buf, uint16_t seq_num,
899 uint32_t ts)
900 {
901 struct bt_hci_iso_sdu_ts_hdr *hdr;
902 struct bt_conn *iso_conn;
903 int err;
904
905 err = validate_send(chan, buf, BT_HCI_ISO_SDU_TS_HDR_SIZE);
906 if (err != 0) {
907 return err;
908 }
909
910 BT_ISO_DATA_DBG("chan %p len %zu", chan, net_buf_frags_len(buf));
911
912 hdr = net_buf_push(buf, sizeof(*hdr));
913 hdr->ts = ts;
914 hdr->sdu.sn = sys_cpu_to_le16(seq_num);
915 hdr->sdu.slen = sys_cpu_to_le16(
916 bt_iso_pkt_len_pack(net_buf_frags_len(buf) - sizeof(*hdr), BT_ISO_DATA_VALID));
917
918 iso_conn = chan->iso;
919
920 LOG_DBG("send-iso (ts)");
921 return conn_iso_send(iso_conn, buf, BT_ISO_TS_PRESENT);
922 }
923
924 #if defined(CONFIG_BT_ISO_CENTRAL) || defined(CONFIG_BT_ISO_BROADCASTER)
valid_chan_io_qos(const struct bt_iso_chan_io_qos * io_qos,bool is_tx,bool is_broadcast,bool advanced)925 static bool valid_chan_io_qos(const struct bt_iso_chan_io_qos *io_qos, bool is_tx,
926 bool is_broadcast, bool advanced)
927 {
928 const size_t max_mtu = (is_tx ? CONFIG_BT_ISO_TX_MTU : CONFIG_BT_ISO_RX_MTU);
929 const size_t max_sdu = MIN(max_mtu, BT_ISO_MAX_SDU);
930
931 if (io_qos->sdu > max_sdu) {
932 LOG_DBG("sdu (%u) shall be smaller or equal to %zu", io_qos->sdu, max_sdu);
933
934 return false;
935 }
936
937 if (!IN_RANGE(io_qos->phy, BT_GAP_LE_PHY_1M, BT_GAP_LE_PHY_CODED)) {
938 LOG_DBG("Invalid PHY %u", io_qos->phy);
939
940 return false;
941 }
942
943 if (IS_ENABLED(CONFIG_BT_ISO_BROADCASTER) && is_broadcast &&
944 io_qos->rtn > BT_ISO_BROADCAST_RTN_MAX) {
945 LOG_DBG("Invalid RTN %u", io_qos->phy);
946
947 return false;
948 }
949
950 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
951 if (advanced) {
952 if (IS_ENABLED(CONFIG_BT_ISO_BROADCASTER) && is_broadcast) {
953 if (!IN_RANGE(io_qos->max_pdu, BT_ISO_BROADCAST_PDU_MIN, BT_ISO_PDU_MAX)) {
954 LOG_DBG("Invalid broadcast PDU %u", io_qos->max_pdu);
955
956 return false;
957 }
958 } else if (IS_ENABLED(CONFIG_BT_ISO_CENTRAL)) {
959 if (!IN_RANGE(io_qos->max_pdu, BT_ISO_CONNECTED_PDU_MIN, BT_ISO_PDU_MAX)) {
960 LOG_DBG("Invalid unicast PDU %u", io_qos->max_pdu);
961
962 return false;
963 }
964 }
965
966 if (!IN_RANGE(io_qos->burst_number, BT_ISO_BN_MIN, BT_ISO_BN_MAX)) {
967 LOG_DBG("Invalid BN %u", io_qos->burst_number);
968
969 return false;
970 }
971 }
972 #else
973 ARG_UNUSED(advanced);
974 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
975
976 return true;
977 }
978 #endif /* CONFIG_BT_ISO_CENTRAL || CONFIG_BT_ISO_BROADCASTER */
979
bt_iso_chan_get_tx_sync(const struct bt_iso_chan * chan,struct bt_iso_tx_info * info)980 int bt_iso_chan_get_tx_sync(const struct bt_iso_chan *chan, struct bt_iso_tx_info *info)
981 {
982 struct bt_hci_cp_le_read_iso_tx_sync *cp;
983 struct bt_hci_rp_le_read_iso_tx_sync *rp;
984 struct net_buf *buf;
985 struct net_buf *rsp = NULL;
986 int err;
987
988 CHECKIF(chan == NULL) {
989 LOG_DBG("chan is NULL");
990 return -EINVAL;
991 }
992
993 CHECKIF(chan->iso == NULL) {
994 LOG_DBG("chan->iso is NULL");
995 return -EINVAL;
996 }
997
998 CHECKIF(info == NULL) {
999 LOG_DBG("info is NULL");
1000 return -EINVAL;
1001 }
1002
1003 CHECKIF(chan->state != BT_ISO_STATE_CONNECTED) {
1004 return -ENOTCONN;
1005 }
1006
1007 buf = bt_hci_cmd_create(BT_HCI_OP_LE_READ_ISO_TX_SYNC, sizeof(*cp));
1008 if (!buf) {
1009 return -ENOMEM;
1010 }
1011
1012 cp = net_buf_add(buf, sizeof(*cp));
1013 cp->handle = sys_cpu_to_le16(chan->iso->handle);
1014
1015 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_ISO_TX_SYNC, buf, &rsp);
1016 if (err) {
1017 return err;
1018 }
1019
1020 if (rsp) {
1021 rp = (struct bt_hci_rp_le_read_iso_tx_sync *)rsp->data;
1022
1023 info->ts = sys_le32_to_cpu(rp->timestamp);
1024 info->seq_num = sys_le16_to_cpu(rp->seq);
1025 info->offset = sys_get_le24(rp->offset);
1026
1027 net_buf_unref(rsp);
1028 } else {
1029 return -ENOTSUP;
1030 }
1031
1032 return 0;
1033 }
1034 #endif /* CONFIG_BT_ISO_TX */
1035
1036 #if defined(CONFIG_BT_ISO_UNICAST)
bt_iso_chan_disconnect(struct bt_iso_chan * chan)1037 int bt_iso_chan_disconnect(struct bt_iso_chan *chan)
1038 {
1039 int err;
1040
1041 CHECKIF(!chan) {
1042 LOG_DBG("Invalid parameter: chan %p", chan);
1043 return -EINVAL;
1044 }
1045
1046 CHECKIF(chan->iso == NULL) {
1047 LOG_DBG("Channel has not been initialized in a CIG");
1048 return -EINVAL;
1049 }
1050
1051 if (chan->iso->iso.acl == NULL || chan->state == BT_ISO_STATE_DISCONNECTED) {
1052 LOG_DBG("Channel is not connected");
1053 return -ENOTCONN;
1054 }
1055
1056 if (chan->state == BT_ISO_STATE_ENCRYPT_PENDING) {
1057 LOG_DBG("Channel already disconnected");
1058 bt_iso_chan_set_state(chan, BT_ISO_STATE_DISCONNECTED);
1059
1060 if (chan->ops->disconnected) {
1061 chan->ops->disconnected(chan, BT_HCI_ERR_LOCALHOST_TERM_CONN);
1062 }
1063
1064 return 0;
1065 }
1066
1067 if (chan->state == BT_ISO_STATE_DISCONNECTING) {
1068 LOG_DBG("Already disconnecting");
1069
1070 return -EALREADY;
1071 }
1072
1073 if (IS_ENABLED(CONFIG_BT_ISO_PERIPHERAL) && chan->iso->role == BT_HCI_ROLE_PERIPHERAL &&
1074 chan->state == BT_ISO_STATE_CONNECTING) {
1075 /* A CIS peripheral is not allowed to disconnect a CIS in the connecting state - It
1076 * has to wait for a CIS Established event
1077 */
1078 return -EAGAIN;
1079 }
1080
1081 err = bt_conn_disconnect(chan->iso, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
1082 if (err == 0) {
1083 bt_iso_chan_set_state(chan, BT_ISO_STATE_DISCONNECTING);
1084 }
1085
1086 return err;
1087 }
1088
bt_iso_cleanup_acl(struct bt_conn * iso)1089 void bt_iso_cleanup_acl(struct bt_conn *iso)
1090 {
1091 LOG_DBG("%p", iso);
1092
1093 if (iso->iso.acl) {
1094 bt_conn_unref(iso->iso.acl);
1095 iso->iso.acl = NULL;
1096 }
1097 }
1098
store_cis_info(const struct bt_hci_evt_le_cis_established * evt,struct bt_conn * iso)1099 static void store_cis_info(const struct bt_hci_evt_le_cis_established *evt, struct bt_conn *iso)
1100 {
1101 struct bt_conn_iso *iso_conn = &iso->iso;
1102 struct bt_iso_info *info = &iso_conn->info;
1103 struct bt_iso_unicast_info *unicast_info = &info->unicast;
1104 struct bt_iso_unicast_tx_info *peripheral = &unicast_info->peripheral;
1105 struct bt_iso_unicast_tx_info *central = &unicast_info->central;
1106 const uint8_t c_phy = bt_get_phy(evt->c_phy);
1107 const uint8_t p_phy = bt_get_phy(evt->p_phy);
1108 struct bt_iso_chan_io_qos *tx;
1109 struct bt_iso_chan_io_qos *rx;
1110 struct bt_iso_chan *chan;
1111
1112 iso_conn = &iso->iso;
1113 chan = iso_conn->chan;
1114 rx = chan->qos->rx;
1115 tx = chan->qos->tx;
1116
1117 LOG_DBG("iso_chan %p tx %p rx %p", chan, tx, rx);
1118
1119 if (iso->role == BT_HCI_ROLE_PERIPHERAL) {
1120 /* As of BT Core 6.0, we can only get the SDU size if the controller
1121 * supports bt_hci_evt_le_cis_established_v2. Since this is not guaranteeds,
1122 * we fallback to using the PDU size as the SDU size.
1123 */
1124 if (rx != NULL) {
1125 rx->phy = c_phy;
1126 rx->sdu = sys_le16_to_cpu(evt->c_max_pdu);
1127 }
1128
1129 if (tx != NULL) {
1130 tx->phy = p_phy;
1131 tx->sdu = sys_le16_to_cpu(evt->p_max_pdu);
1132 }
1133
1134 iso_conn->info.type = BT_ISO_CHAN_TYPE_CONNECTED;
1135 } else {
1136 /* values are already set for central - Verify */
1137 if (tx != NULL && tx->phy != c_phy) {
1138 LOG_WRN("Unexpected C to P PHY: %u != %u", c_phy, tx->phy);
1139 /* We assume that tx->phy has become invalid, and will use the event from
1140 * the controller as the truth
1141 */
1142 tx->phy = c_phy;
1143 }
1144
1145 if (rx != NULL && rx->phy != p_phy) {
1146 LOG_WRN("Unexpected P to C max SDU: %u != %u", p_phy, rx->phy);
1147 /* We assume that rx->phy has become invalid, and will use the event from
1148 * the controller as the truth
1149 */
1150 rx->phy = p_phy;
1151 }
1152 }
1153
1154 /* Verify if device can send */
1155 iso_conn->info.can_send = false;
1156 if (tx != NULL) {
1157 if (iso->role == BT_HCI_ROLE_PERIPHERAL && evt->p_bn > 0) {
1158 iso_conn->info.can_send = true;
1159 } else if (iso->role == BT_HCI_ROLE_CENTRAL && evt->c_bn > 0) {
1160 iso_conn->info.can_send = true;
1161 }
1162 }
1163
1164 /* Verify if device can recv */
1165 iso_conn->info.can_recv = false;
1166 if (rx != NULL) {
1167 if (iso->role == BT_HCI_ROLE_PERIPHERAL && evt->c_bn > 0) {
1168 iso_conn->info.can_recv = true;
1169 } else if (iso->role == BT_HCI_ROLE_CENTRAL && evt->p_bn > 0) {
1170 iso_conn->info.can_recv = true;
1171 }
1172 }
1173
1174 info->iso_interval = sys_le16_to_cpu(evt->interval);
1175 info->max_subevent = evt->nse;
1176
1177 unicast_info->cig_sync_delay = sys_get_le24(evt->cig_sync_delay);
1178 unicast_info->cis_sync_delay = sys_get_le24(evt->cis_sync_delay);
1179
1180 central->bn = evt->c_bn;
1181 central->phy = bt_get_phy(evt->c_phy);
1182 central->latency = sys_get_le16(evt->c_latency);
1183 central->max_pdu = sys_le16_to_cpu(evt->c_max_pdu);
1184 /* Transform to n * 1.25ms */
1185 central->flush_timeout = info->iso_interval * evt->c_ft;
1186
1187 peripheral->bn = evt->p_bn;
1188 peripheral->phy = bt_get_phy(evt->p_phy);
1189 peripheral->latency = sys_get_le16(evt->p_latency);
1190 peripheral->max_pdu = sys_le16_to_cpu(evt->p_max_pdu);
1191 /* Transform to n * 1.25ms */
1192 peripheral->flush_timeout = info->iso_interval * evt->p_ft;
1193
1194 /* The following values are only available with bt_hci_evt_le_cis_established_v2 so
1195 * initialize them to the "unknown" values
1196 */
1197 unicast_info->subinterval = BT_ISO_SUBINTERVAL_UNKNOWN;
1198
1199 if (iso->role == BT_HCI_ROLE_PERIPHERAL) {
1200 central->max_sdu = central->max_pdu;
1201 central->sdu_interval = BT_ISO_SDU_INTERVAL_UNKNOWN;
1202
1203 peripheral->max_sdu = peripheral->max_pdu;
1204 peripheral->sdu_interval = BT_ISO_SDU_INTERVAL_UNKNOWN;
1205 } else {
1206 central->max_sdu = tx == NULL ? 0 : tx->sdu;
1207 central->sdu_interval = BT_ISO_SDU_INTERVAL_UNKNOWN;
1208
1209 peripheral->max_sdu = rx == NULL ? 0 : rx->sdu;
1210 peripheral->sdu_interval = BT_ISO_SDU_INTERVAL_UNKNOWN;
1211 }
1212 }
1213
1214 /** Only store information that is not stored by store_cis_info
1215 * Assumes that store_cis_info has been called first
1216 */
store_cis_info_v2(const struct bt_hci_evt_le_cis_established_v2 * evt,struct bt_conn * iso)1217 static void store_cis_info_v2(const struct bt_hci_evt_le_cis_established_v2 *evt,
1218 struct bt_conn *iso)
1219 {
1220 struct bt_conn_iso *iso_conn = &iso->iso;
1221 struct bt_iso_info *info = &iso_conn->info;
1222 struct bt_iso_unicast_info *unicast_info = &info->unicast;
1223 struct bt_iso_unicast_tx_info *peripheral = &unicast_info->peripheral;
1224 struct bt_iso_unicast_tx_info *central = &unicast_info->central;
1225 const uint16_t c_max_sdu = sys_le16_to_cpu(evt->c_max_sdu);
1226 const uint16_t p_max_sdu = sys_le16_to_cpu(evt->p_max_sdu);
1227 struct bt_iso_chan_io_qos *tx;
1228 struct bt_iso_chan_io_qos *rx;
1229 struct bt_iso_chan *chan;
1230
1231 /* The v1 version of the event is a subset of the v2 version - We can thus use the
1232 * store_cis_info function for the majority of the info
1233 */
1234 store_cis_info((const struct bt_hci_evt_le_cis_established *)evt, iso);
1235
1236 chan = iso_conn->chan;
1237 rx = chan->qos->rx;
1238 tx = chan->qos->tx;
1239
1240 if (iso->role == BT_HCI_ROLE_PERIPHERAL) {
1241 /* Update the SDU sizes in the IO QoS fields stored by store_cis_info */
1242 if (rx != NULL) {
1243 rx->sdu = c_max_sdu;
1244 }
1245
1246 if (tx != NULL) {
1247 tx->sdu = p_max_sdu;
1248 }
1249 } else {
1250 /* values are already set for central - Verify */
1251 if (tx != NULL && tx->sdu != c_max_sdu) {
1252 LOG_WRN("Unexpected C to P max SDU: %u != %u", c_max_sdu, tx->sdu);
1253 /* We assume that tx->sdu has become invalid, and will use the event from
1254 * the controller as the truth
1255 */
1256 tx->sdu = c_max_sdu;
1257 }
1258
1259 if (rx != NULL && rx->sdu != p_max_sdu) {
1260 LOG_WRN("Unexpected P to C max SDU: %u != %u", p_max_sdu, rx->sdu);
1261 /* We assume that rx->sdu has become invalid, and will use the event from
1262 * the controller as the truth
1263 */
1264 rx->sdu = p_max_sdu;
1265 }
1266 }
1267
1268 unicast_info->subinterval = sys_get_le24(evt->sub_interval);
1269
1270 central->max_sdu = sys_le16_to_cpu(evt->c_max_sdu);
1271 central->sdu_interval = sys_get_le24(evt->c_sdu_interval);
1272
1273 peripheral->max_sdu = sys_le16_to_cpu(evt->p_max_sdu);
1274 peripheral->sdu_interval = sys_get_le24(evt->p_sdu_interval);
1275 }
1276
hci_le_cis_established(struct net_buf * buf)1277 void hci_le_cis_established(struct net_buf *buf)
1278 {
1279 struct bt_hci_evt_le_cis_established *evt = (void *)buf->data;
1280 uint16_t handle = sys_le16_to_cpu(evt->conn_handle);
1281 struct bt_conn *iso;
1282
1283 LOG_DBG("status 0x%02x %s handle %u", evt->status, bt_hci_err_to_str(evt->status), handle);
1284
1285 /* ISO connection handles are already assigned at this point */
1286 iso = bt_conn_lookup_handle(handle, BT_CONN_TYPE_ISO);
1287 if (!iso) {
1288 /* If it is a local disconnect, then we may have received the disconnect complete
1289 * event before this event, and in which case we do not expect to find the CIS
1290 * object
1291 */
1292 if (evt->status != BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
1293 LOG_ERR("No connection found for handle %u", handle);
1294 }
1295
1296 return;
1297 }
1298
1299 if (evt->status == BT_HCI_ERR_SUCCESS) {
1300 store_cis_info(evt, iso);
1301 bt_conn_set_state(iso, BT_CONN_CONNECTED);
1302 } else if (iso->role == BT_HCI_ROLE_PERIPHERAL ||
1303 evt->status != BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
1304 iso->err = evt->status;
1305 bt_iso_disconnected(iso);
1306 } /* else we wait for disconnect event */
1307
1308 bt_conn_unref(iso);
1309 }
1310
hci_le_cis_established_v2(struct net_buf * buf)1311 void hci_le_cis_established_v2(struct net_buf *buf)
1312 {
1313 struct bt_hci_evt_le_cis_established_v2 *evt = (void *)buf->data;
1314 uint16_t handle = sys_le16_to_cpu(evt->conn_handle);
1315 struct bt_conn *iso;
1316
1317 LOG_DBG("status 0x%02x %s handle %u", evt->status, bt_hci_err_to_str(evt->status), handle);
1318
1319 /* ISO connection handles are already assigned at this point */
1320 iso = bt_conn_lookup_handle(handle, BT_CONN_TYPE_ISO);
1321 if (!iso) {
1322 /* If it is a local disconnect, then we may have received the disconnect complete
1323 * event before this event, and in which case we do not expect to find the CIS
1324 * object
1325 */
1326 if (evt->status != BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
1327 LOG_ERR("No connection found for handle %u", handle);
1328 }
1329
1330 return;
1331 }
1332
1333 if (evt->status == BT_HCI_ERR_SUCCESS) {
1334 store_cis_info_v2(evt, iso);
1335 bt_conn_set_state(iso, BT_CONN_CONNECTED);
1336 } else if (iso->role == BT_HCI_ROLE_PERIPHERAL ||
1337 evt->status != BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
1338 iso->err = evt->status;
1339 bt_iso_disconnected(iso);
1340 } /* else we wait for disconnect event */
1341
1342 bt_conn_unref(iso);
1343 }
1344
1345 #if defined(CONFIG_BT_ISO_PERIPHERAL)
bt_iso_server_register(struct bt_iso_server * server)1346 int bt_iso_server_register(struct bt_iso_server *server)
1347 {
1348 CHECKIF(!server) {
1349 LOG_DBG("Invalid parameter: server %p", server);
1350 return -EINVAL;
1351 }
1352
1353 /* Check if controller is ISO capable */
1354 if (!BT_FEAT_LE_CIS_PERIPHERAL(bt_dev.le.features)) {
1355 return -ENOTSUP;
1356 }
1357
1358 if (iso_server) {
1359 return -EADDRINUSE;
1360 }
1361
1362 if (!server->accept) {
1363 return -EINVAL;
1364 }
1365
1366 #if defined(CONFIG_BT_SMP)
1367 if (server->sec_level > BT_SECURITY_L3) {
1368 return -EINVAL;
1369 } else if (server->sec_level < BT_SECURITY_L1) {
1370 /* Level 0 is only applicable for BR/EDR */
1371 server->sec_level = BT_SECURITY_L1;
1372 }
1373 #endif /* CONFIG_BT_SMP */
1374
1375 LOG_DBG("%p", server);
1376
1377 iso_server = server;
1378
1379 return 0;
1380 }
1381
bt_iso_server_unregister(struct bt_iso_server * server)1382 int bt_iso_server_unregister(struct bt_iso_server *server)
1383 {
1384 CHECKIF(!server) {
1385 LOG_DBG("Invalid parameter: server %p", server);
1386 return -EINVAL;
1387 }
1388
1389 if (iso_server != server) {
1390 return -EINVAL;
1391 }
1392
1393 iso_server = NULL;
1394
1395 return 0;
1396 }
1397
iso_accept(struct bt_conn * acl,struct bt_conn * iso)1398 static int iso_accept(struct bt_conn *acl, struct bt_conn *iso)
1399 {
1400 struct bt_iso_accept_info accept_info;
1401 struct bt_iso_chan *chan;
1402 int err;
1403
1404 CHECKIF(!iso || iso->type != BT_CONN_TYPE_ISO) {
1405 LOG_DBG("Invalid parameters: iso %p iso->type %u", iso, iso ? iso->type : 0);
1406 return -EINVAL;
1407 }
1408
1409 LOG_DBG("%p", iso);
1410
1411 accept_info.acl = acl;
1412 accept_info.cig_id = iso->iso.cig_id;
1413 accept_info.cis_id = iso->iso.cis_id;
1414
1415 err = iso_server->accept(&accept_info, &chan);
1416 if (err < 0) {
1417 LOG_ERR("Server failed to accept: %d", err);
1418 return err;
1419 }
1420
1421 #if defined(CONFIG_BT_SMP)
1422 chan->required_sec_level = iso_server->sec_level;
1423 #endif /* CONFIG_BT_SMP */
1424
1425 bt_iso_chan_add(iso, chan);
1426 bt_iso_chan_set_state(chan, BT_ISO_STATE_CONNECTING);
1427
1428 return 0;
1429 }
1430
hci_le_reject_cis(uint16_t handle,uint8_t reason)1431 static int hci_le_reject_cis(uint16_t handle, uint8_t reason)
1432 {
1433 struct bt_hci_cp_le_reject_cis *cp;
1434 struct net_buf *buf;
1435 int err;
1436
1437 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REJECT_CIS, sizeof(*cp));
1438 if (!buf) {
1439 return -ENOBUFS;
1440 }
1441
1442 cp = net_buf_add(buf, sizeof(*cp));
1443 cp->handle = sys_cpu_to_le16(handle);
1444 cp->reason = reason;
1445
1446 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REJECT_CIS, buf, NULL);
1447 if (err) {
1448 return err;
1449 }
1450
1451 return 0;
1452 }
1453
hci_le_accept_cis(uint16_t handle)1454 static int hci_le_accept_cis(uint16_t handle)
1455 {
1456 struct bt_hci_cp_le_accept_cis *cp;
1457 struct net_buf *buf;
1458 int err;
1459
1460 buf = bt_hci_cmd_create(BT_HCI_OP_LE_ACCEPT_CIS, sizeof(*cp));
1461 if (!buf) {
1462 return -ENOBUFS;
1463 }
1464
1465 cp = net_buf_add(buf, sizeof(*cp));
1466 cp->handle = sys_cpu_to_le16(handle);
1467
1468 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_ACCEPT_CIS, buf, NULL);
1469 if (err) {
1470 return err;
1471 }
1472
1473 return 0;
1474 }
1475
iso_server_check_security(struct bt_conn * conn)1476 static uint8_t iso_server_check_security(struct bt_conn *conn)
1477 {
1478 if (IS_ENABLED(CONFIG_BT_CONN_DISABLE_SECURITY)) {
1479 return BT_HCI_ERR_SUCCESS;
1480 }
1481
1482 #if defined(CONFIG_BT_SMP)
1483 if (conn->sec_level >= iso_server->sec_level) {
1484 return BT_HCI_ERR_SUCCESS;
1485 }
1486
1487 return BT_HCI_ERR_INSUFFICIENT_SECURITY;
1488 #else
1489 return BT_HCI_ERR_SUCCESS;
1490 #endif /* CONFIG_BT_SMP */
1491 }
1492
hci_le_cis_req(struct net_buf * buf)1493 void hci_le_cis_req(struct net_buf *buf)
1494 {
1495 struct bt_hci_evt_le_cis_req *evt = (void *)buf->data;
1496 uint16_t acl_handle = sys_le16_to_cpu(evt->acl_handle);
1497 uint16_t cis_handle = sys_le16_to_cpu(evt->cis_handle);
1498 struct bt_conn *acl, *iso;
1499 uint8_t sec_err;
1500 int err;
1501
1502 LOG_DBG("acl_handle %u cis_handle %u cig_id %u cis %u", acl_handle, cis_handle, evt->cig_id,
1503 evt->cis_id);
1504
1505 if (iso_server == NULL) {
1506 LOG_DBG("No ISO server registered");
1507 hci_le_reject_cis(cis_handle, BT_HCI_ERR_UNSPECIFIED);
1508 return;
1509 }
1510
1511 /* Lookup existing connection with same handle */
1512 iso = bt_conn_lookup_handle(cis_handle, BT_CONN_TYPE_ISO);
1513 if (iso) {
1514 LOG_ERR("Invalid ISO handle %u", cis_handle);
1515 hci_le_reject_cis(cis_handle, BT_HCI_ERR_CONN_LIMIT_EXCEEDED);
1516 bt_conn_unref(iso);
1517 return;
1518 }
1519
1520 /* Lookup ACL connection to attach */
1521 acl = bt_conn_lookup_handle(acl_handle, BT_CONN_TYPE_LE);
1522 if (!acl) {
1523 LOG_ERR("Invalid ACL handle %u", acl_handle);
1524 hci_le_reject_cis(cis_handle, BT_HCI_ERR_UNKNOWN_CONN_ID);
1525 return;
1526 }
1527
1528 sec_err = iso_server_check_security(acl);
1529 if (sec_err != BT_HCI_ERR_SUCCESS) {
1530 LOG_DBG("Insufficient security %u", sec_err);
1531 err = hci_le_reject_cis(cis_handle, sec_err);
1532 if (err != 0) {
1533 LOG_ERR("Failed to reject CIS");
1534 }
1535
1536 bt_conn_unref(acl);
1537 return;
1538 }
1539
1540 /* Add ISO connection */
1541 iso = bt_conn_add_iso(acl);
1542
1543 bt_conn_unref(acl);
1544
1545 if (!iso) {
1546 LOG_ERR("Could not create and add ISO to ACL %u", acl_handle);
1547 hci_le_reject_cis(cis_handle, BT_HCI_ERR_INSUFFICIENT_RESOURCES);
1548 return;
1549 }
1550
1551 iso->iso.info.type = BT_ISO_CHAN_TYPE_CONNECTED;
1552 iso->iso.cig_id = evt->cig_id;
1553 iso->iso.cis_id = evt->cis_id;
1554
1555 /* Request application to accept */
1556 err = iso_accept(acl, iso);
1557 if (err) {
1558 LOG_DBG("App rejected ISO %d", err);
1559 bt_iso_cleanup_acl(iso);
1560 bt_conn_unref(iso);
1561 hci_le_reject_cis(cis_handle, BT_HCI_ERR_INSUFFICIENT_RESOURCES);
1562 return;
1563 }
1564
1565 iso->handle = cis_handle;
1566 iso->role = BT_HCI_ROLE_PERIPHERAL;
1567 bt_conn_set_state(iso, BT_CONN_INITIATING);
1568
1569 err = hci_le_accept_cis(cis_handle);
1570 if (err) {
1571 bt_iso_cleanup_acl(iso);
1572 bt_conn_unref(iso);
1573 hci_le_reject_cis(cis_handle, BT_HCI_ERR_INSUFFICIENT_RESOURCES);
1574 return;
1575 }
1576 }
1577
bt_conn_add_iso(struct bt_conn * acl)1578 static struct bt_conn *bt_conn_add_iso(struct bt_conn *acl)
1579 {
1580 struct bt_conn *iso = iso_new();
1581
1582 if (iso == NULL) {
1583 LOG_ERR("Unable to allocate ISO connection");
1584 return NULL;
1585 }
1586
1587 iso->iso.acl = bt_conn_ref(acl);
1588
1589 return iso;
1590 }
1591 #endif /* CONFIG_BT_ISO_PERIPHERAL */
1592
1593 #if defined(CONFIG_BT_ISO_CENTRAL)
hci_le_remove_iso_data_path(struct bt_conn * iso,uint8_t dir)1594 static int hci_le_remove_iso_data_path(struct bt_conn *iso, uint8_t dir)
1595 {
1596 struct bt_hci_cp_le_remove_iso_path *cp;
1597 struct bt_hci_rp_le_remove_iso_path *rp;
1598 struct net_buf *buf, *rsp;
1599 int err;
1600
1601 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ISO_PATH, sizeof(*cp));
1602 if (!buf) {
1603 return -ENOBUFS;
1604 }
1605
1606 cp = net_buf_add(buf, sizeof(*cp));
1607 cp->handle = sys_cpu_to_le16(iso->handle);
1608 cp->path_dir = dir;
1609
1610 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ISO_PATH, buf, &rsp);
1611 if (err) {
1612 return err;
1613 }
1614
1615 rp = (void *)rsp->data;
1616 if (rp->status || (sys_le16_to_cpu(rp->handle) != iso->handle)) {
1617 err = -EIO;
1618 }
1619
1620 net_buf_unref(rsp);
1621
1622 return err;
1623 }
1624
bt_iso_remove_data_path(struct bt_conn * iso)1625 static void bt_iso_remove_data_path(struct bt_conn *iso)
1626 {
1627 enum bt_iso_chan_type type = iso->iso.info.type;
1628
1629 LOG_DBG("%p", iso);
1630
1631 /* TODO: Removing the ISO data path is never used for broadcast:
1632 * Remove the following broadcast implementation?
1633 */
1634 if ((IS_ENABLED(CONFIG_BT_ISO_BROADCASTER) && type == BT_ISO_CHAN_TYPE_BROADCASTER) ||
1635 (IS_ENABLED(CONFIG_BT_ISO_SYNC_RECEIVER) && type == BT_ISO_CHAN_TYPE_SYNC_RECEIVER)) {
1636 struct bt_iso_chan *chan;
1637 struct bt_iso_chan_io_qos *tx_qos;
1638 uint8_t dir;
1639
1640 chan = iso_chan(iso);
1641 if (chan == NULL) {
1642 return;
1643 }
1644
1645 tx_qos = chan->qos->tx;
1646
1647 /* Only remove one data path for BIS as per the spec */
1648 if (tx_qos) {
1649 dir = BIT(BT_HCI_DATAPATH_DIR_HOST_TO_CTLR);
1650 } else {
1651 dir = BIT(BT_HCI_DATAPATH_DIR_CTLR_TO_HOST);
1652 }
1653
1654 (void)hci_le_remove_iso_data_path(iso, dir);
1655 } else if (IS_ENABLED(CONFIG_BT_ISO_UNICAST) && type == BT_ISO_CHAN_TYPE_CONNECTED) {
1656 /* Remove both directions for CIS*/
1657
1658 /* TODO: Check which has been setup first to avoid removing
1659 * data paths that are not setup
1660 */
1661 (void)hci_le_remove_iso_data_path(iso, BIT(BT_HCI_DATAPATH_DIR_HOST_TO_CTLR));
1662 (void)hci_le_remove_iso_data_path(iso, BIT(BT_HCI_DATAPATH_DIR_CTLR_TO_HOST));
1663 } else {
1664 __ASSERT(false, "Invalid iso.type: %u", type);
1665 }
1666 }
1667
valid_chan_qos(const struct bt_iso_chan_qos * qos,bool advanced)1668 static bool valid_chan_qos(const struct bt_iso_chan_qos *qos, bool advanced)
1669 {
1670 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
1671 if (advanced && !IN_RANGE(qos->num_subevents, BT_ISO_NSE_MIN, BT_ISO_NSE_MAX)) {
1672 LOG_DBG("Invalid NSE: %u", qos->num_subevents);
1673
1674 return false;
1675 }
1676 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
1677
1678 if (qos->rx != NULL) {
1679 if (!valid_chan_io_qos(qos->rx, false, false, advanced)) {
1680 LOG_DBG("Invalid rx qos");
1681 return false;
1682 }
1683 } else if (qos->tx == NULL) {
1684 LOG_DBG("Both rx and tx qos are NULL");
1685 return false;
1686 }
1687
1688 if (qos->tx != NULL) {
1689 if (!valid_chan_io_qos(qos->tx, true, false, advanced)) {
1690 LOG_DBG("Invalid tx qos");
1691 return false;
1692 }
1693 }
1694
1695 return true;
1696 }
1697
hci_le_remove_cig(uint8_t cig_id)1698 static int hci_le_remove_cig(uint8_t cig_id)
1699 {
1700 struct bt_hci_cp_le_remove_cig *req;
1701 struct net_buf *buf;
1702
1703 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_CIG, sizeof(*req));
1704 if (!buf) {
1705 return -ENOBUFS;
1706 }
1707
1708 req = net_buf_add(buf, sizeof(*req));
1709
1710 memset(req, 0, sizeof(*req));
1711
1712 req->cig_id = cig_id;
1713
1714 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_CIG, buf, NULL);
1715 }
1716
hci_le_set_cig_params(const struct bt_iso_cig * cig,const struct bt_iso_cig_param * param)1717 static struct net_buf *hci_le_set_cig_params(const struct bt_iso_cig *cig,
1718 const struct bt_iso_cig_param *param)
1719 {
1720 struct bt_hci_cp_le_set_cig_params *req;
1721 struct bt_hci_cis_params *cis_param;
1722 struct net_buf *buf;
1723 struct net_buf *rsp;
1724 int i, err;
1725
1726 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_CIG_PARAMS,
1727 sizeof(*req) + sizeof(*cis_param) * param->num_cis);
1728 if (!buf) {
1729 return NULL;
1730 }
1731
1732 req = net_buf_add(buf, sizeof(*req));
1733
1734 memset(req, 0, sizeof(*req));
1735
1736 req->cig_id = cig->id;
1737 req->c_latency = sys_cpu_to_le16(param->c_to_p_latency);
1738 req->p_latency = sys_cpu_to_le16(param->p_to_c_latency);
1739 sys_put_le24(param->c_to_p_interval, req->c_interval);
1740 sys_put_le24(param->p_to_c_interval, req->p_interval);
1741
1742 req->sca = param->sca;
1743 req->packing = param->packing;
1744 req->framing = param->framing;
1745 req->num_cis = param->num_cis;
1746
1747 LOG_DBG("id %u, latency C to P %u, latency P to C %u, "
1748 "interval C to P %u, interval P to C %u, "
1749 "sca %u, packing %u, framing %u, num_cis %u",
1750 cig->id, param->c_to_p_latency, param->p_to_c_latency, param->c_to_p_interval,
1751 param->p_to_c_interval, param->sca, param->packing, param->framing, param->num_cis);
1752
1753 /* Program the cis parameters */
1754 for (i = 0; i < param->num_cis; i++) {
1755 struct bt_iso_chan *cis = param->cis_channels[i];
1756 struct bt_iso_chan_qos *qos = cis->qos;
1757
1758 cis_param = net_buf_add(buf, sizeof(*cis_param));
1759
1760 memset(cis_param, 0, sizeof(*cis_param));
1761
1762 cis_param->cis_id = cis->iso->iso.cis_id;
1763
1764 if (!qos->tx && !qos->rx) {
1765 LOG_ERR("Both TX and RX QoS are disabled");
1766 net_buf_unref(buf);
1767 return NULL;
1768 }
1769
1770 if (!qos->tx) {
1771 /* Use RX PHY if TX is not set (disabled)
1772 * to avoid setting invalid values
1773 */
1774 cis_param->c_phy = qos->rx->phy;
1775 } else {
1776 cis_param->c_sdu = sys_cpu_to_le16(qos->tx->sdu);
1777 cis_param->c_phy = qos->tx->phy;
1778 cis_param->c_rtn = qos->tx->rtn;
1779 }
1780
1781 if (!qos->rx) {
1782 /* Use TX PHY if RX is not set (disabled)
1783 * to avoid setting invalid values
1784 */
1785 cis_param->p_phy = qos->tx->phy;
1786 } else {
1787 cis_param->p_sdu = sys_cpu_to_le16(qos->rx->sdu);
1788 cis_param->p_phy = qos->rx->phy;
1789 cis_param->p_rtn = qos->rx->rtn;
1790 }
1791
1792 LOG_DBG("[%d]: id %u, c_phy %u, c_sdu %u, c_rtn %u, p_phy %u, p_sdu %u, p_rtn %u",
1793 i, cis_param->cis_id, cis_param->c_phy, cis_param->c_sdu, cis_param->c_rtn,
1794 cis_param->p_phy, cis_param->p_sdu, cis_param->p_rtn);
1795 }
1796
1797 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CIG_PARAMS, buf, &rsp);
1798 if (err) {
1799 return NULL;
1800 }
1801
1802 return rsp;
1803 }
1804
1805 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
hci_le_set_cig_test_params(const struct bt_iso_cig * cig,const struct bt_iso_cig_param * param)1806 static struct net_buf *hci_le_set_cig_test_params(const struct bt_iso_cig *cig,
1807 const struct bt_iso_cig_param *param)
1808 {
1809 struct bt_hci_cp_le_set_cig_params_test *req;
1810 struct bt_hci_cis_params_test *cis_param;
1811 struct net_buf *buf;
1812 struct net_buf *rsp;
1813 int err;
1814
1815 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_CIG_PARAMS_TEST,
1816 sizeof(*req) + sizeof(*cis_param) * param->num_cis);
1817 if (!buf) {
1818 return NULL;
1819 }
1820
1821 req = net_buf_add(buf, sizeof(*req));
1822
1823 memset(req, 0, sizeof(*req));
1824
1825 req->cig_id = cig->id;
1826 sys_put_le24(param->c_to_p_interval, req->c_interval);
1827 sys_put_le24(param->p_to_c_interval, req->p_interval);
1828
1829 req->c_ft = param->c_to_p_ft;
1830 req->p_ft = param->p_to_c_ft;
1831 req->iso_interval = sys_cpu_to_le16(param->iso_interval);
1832 req->sca = param->sca;
1833 req->packing = param->packing;
1834 req->framing = param->framing;
1835 req->num_cis = param->num_cis;
1836
1837 LOG_DBG("id %u, SDU interval C to P %u, SDU interval P to C %u, c_ft %u, p_ft %u, "
1838 "iso_interval %u, sca %u, packing %u, framing %u, num_cis %u",
1839 cig->id, param->c_to_p_interval, param->p_to_c_interval, param->c_to_p_ft,
1840 param->p_to_c_ft, param->iso_interval, param->sca, param->packing, param->framing,
1841 param->num_cis);
1842
1843 /* Program the cis parameters */
1844 for (uint8_t i = 0U; i < param->num_cis; i++) {
1845 const struct bt_iso_chan *cis = param->cis_channels[i];
1846 const struct bt_iso_chan_qos *qos = cis->qos;
1847
1848 cis_param = net_buf_add(buf, sizeof(*cis_param));
1849
1850 memset(cis_param, 0, sizeof(*cis_param));
1851
1852 cis_param->cis_id = cis->iso->iso.cis_id;
1853 cis_param->nse = qos->num_subevents;
1854
1855 if (!qos->tx && !qos->rx) {
1856 LOG_ERR("Both TX and RX QoS are disabled");
1857 net_buf_unref(buf);
1858 return NULL;
1859 }
1860
1861 if (!qos->tx) {
1862 /* Use RX PHY if TX is not set (disabled)
1863 * to avoid setting invalid values
1864 */
1865 cis_param->c_phy = qos->rx->phy;
1866 } else {
1867 cis_param->c_sdu = sys_cpu_to_le16(qos->tx->sdu);
1868 cis_param->c_pdu = sys_cpu_to_le16(qos->tx->max_pdu);
1869 cis_param->c_phy = qos->tx->phy;
1870 cis_param->c_bn = qos->tx->burst_number;
1871 }
1872
1873 if (!qos->rx) {
1874 /* Use TX PHY if RX is not set (disabled)
1875 * to avoid setting invalid values
1876 */
1877 cis_param->p_phy = qos->tx->phy;
1878 } else {
1879 cis_param->p_sdu = sys_cpu_to_le16(qos->rx->sdu);
1880 cis_param->p_pdu = sys_cpu_to_le16(qos->rx->max_pdu);
1881 cis_param->p_phy = qos->rx->phy;
1882 cis_param->p_bn = qos->rx->burst_number;
1883 }
1884
1885 LOG_DBG("[%d]: id %u, nse %u c_sdu %u, p_sdu %u, c_pdu %u, "
1886 "p_pdu %u, c_phy %u, p_phy %u, c_bn %u, p_bn %u",
1887 i, cis_param->cis_id, cis_param->nse, cis_param->c_sdu, cis_param->p_sdu,
1888 cis_param->c_pdu, cis_param->p_pdu, cis_param->c_phy, cis_param->p_phy,
1889 cis_param->c_bn, cis_param->p_bn);
1890 }
1891
1892 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CIG_PARAMS_TEST, buf, &rsp);
1893 if (err) {
1894 return NULL;
1895 }
1896
1897 return rsp;
1898 }
1899
is_advanced_cig_param(const struct bt_iso_cig_param * param)1900 static bool is_advanced_cig_param(const struct bt_iso_cig_param *param)
1901 {
1902 if (param->c_to_p_ft != 0U || param->p_to_c_ft != 0U || param->iso_interval != 0U) {
1903 return true;
1904 }
1905
1906 /* Check if any of the CIS contain any test-param-only values */
1907 for (uint8_t i = 0U; i < param->num_cis; i++) {
1908 const struct bt_iso_chan *cis = param->cis_channels[i];
1909 const struct bt_iso_chan_qos *qos = cis->qos;
1910
1911 if (qos->num_subevents > 0U) {
1912 return true;
1913 }
1914
1915 if (qos->tx != NULL) {
1916 if (qos->tx->max_pdu > 0U || qos->tx->burst_number > 0U) {
1917 return true;
1918 }
1919 }
1920
1921 if (qos->rx != NULL) {
1922 if (qos->rx->max_pdu > 0U || qos->rx->burst_number > 0U) {
1923 return true;
1924 }
1925 }
1926 }
1927
1928 return false;
1929 }
1930 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
1931
get_cig(const struct bt_iso_chan * iso_chan)1932 static struct bt_iso_cig *get_cig(const struct bt_iso_chan *iso_chan)
1933 {
1934 if (iso_chan == NULL || iso_chan->iso == NULL) {
1935 return NULL;
1936 }
1937
1938 __ASSERT(iso_chan->iso->iso.cig_id < ARRAY_SIZE(cigs), "Invalid cig_id %u",
1939 iso_chan->iso->iso.cig_id);
1940
1941 return &cigs[iso_chan->iso->iso.cig_id];
1942 }
1943
get_free_cig(void)1944 static struct bt_iso_cig *get_free_cig(void)
1945 {
1946 /* We can use the index in the `cigs` array as CIG ID */
1947
1948 for (size_t i = 0; i < ARRAY_SIZE(cigs); i++) {
1949 if (cigs[i].state == BT_ISO_CIG_STATE_IDLE) {
1950 cigs[i].state = BT_ISO_CIG_STATE_CONFIGURED;
1951 cigs[i].id = i;
1952 sys_slist_init(&cigs[i].cis_channels);
1953 return &cigs[i];
1954 }
1955 }
1956
1957 LOG_DBG("Could not allocate any more CIGs");
1958
1959 return NULL;
1960 }
1961
cis_is_in_cig(const struct bt_iso_cig * cig,const struct bt_iso_chan * cis)1962 static bool cis_is_in_cig(const struct bt_iso_cig *cig, const struct bt_iso_chan *cis)
1963 {
1964 if (cig == NULL || cis == NULL || cis->iso == NULL) {
1965 return false;
1966 }
1967
1968 return cig->id == cis->iso->iso.cig_id;
1969 }
1970
cig_init_cis(struct bt_iso_cig * cig,const struct bt_iso_cig_param * param)1971 static int cig_init_cis(struct bt_iso_cig *cig, const struct bt_iso_cig_param *param)
1972 {
1973 for (uint8_t i = 0; i < param->num_cis; i++) {
1974 struct bt_iso_chan *cis = param->cis_channels[i];
1975
1976 if (cis->iso == NULL) {
1977 struct bt_conn_iso *iso_conn;
1978
1979 cis->iso = iso_new();
1980 if (cis->iso == NULL) {
1981 LOG_ERR("Unable to allocate CIS connection");
1982 return -ENOMEM;
1983 }
1984 iso_conn = &cis->iso->iso;
1985
1986 iso_conn->cig_id = cig->id;
1987 iso_conn->info.type = BT_ISO_CHAN_TYPE_CONNECTED;
1988 iso_conn->cis_id = cig->num_cis++;
1989
1990 bt_iso_chan_add(cis->iso, cis);
1991
1992 sys_slist_append(&cig->cis_channels, &cis->node);
1993 } /* else already initialized */
1994 }
1995
1996 return 0;
1997 }
1998
cleanup_cig(struct bt_iso_cig * cig)1999 static void cleanup_cig(struct bt_iso_cig *cig)
2000 {
2001 struct bt_iso_chan *cis, *tmp;
2002
2003 SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&cig->cis_channels, cis, tmp, node) {
2004 if (cis->iso != NULL) {
2005 bt_conn_unref(cis->iso);
2006 cis->iso = NULL;
2007 }
2008
2009 sys_slist_remove(&cig->cis_channels, NULL, &cis->node);
2010 }
2011
2012 memset(cig, 0, sizeof(*cig));
2013 }
2014
valid_cig_param(const struct bt_iso_cig_param * param,bool advanced,const struct bt_iso_cig * cig)2015 static bool valid_cig_param(const struct bt_iso_cig_param *param, bool advanced,
2016 const struct bt_iso_cig *cig)
2017 {
2018 bool is_c_to_p = false;
2019 bool is_p_to_c = false;
2020
2021 if (param == NULL) {
2022 return false;
2023 }
2024
2025 for (uint8_t i = 0; i < param->num_cis; i++) {
2026 struct bt_iso_chan *cis = param->cis_channels[i];
2027
2028 if (cis == NULL) {
2029 LOG_DBG("cis_channels[%u]: NULL channel", i);
2030 return false;
2031 }
2032
2033 if (cis->qos == NULL) {
2034 LOG_DBG("cis_channels[%u]: NULL QoS", i);
2035 return false;
2036 }
2037
2038 if (cis->iso != NULL && !cis_is_in_cig(cig, cis)) {
2039 LOG_DBG("cis_channels[%u]: already allocated to CIG %p", i, get_cig(cis));
2040 return false;
2041 }
2042
2043 if (!valid_chan_qos(cis->qos, advanced)) {
2044 LOG_DBG("cis_channels[%u]: Invalid QOS", i);
2045 return false;
2046 }
2047
2048 for (uint8_t j = 0; j < i; j++) {
2049 if (cis == param->cis_channels[j]) {
2050 LOG_DBG("ISO %p duplicated at index %u and %u", cis, i, j);
2051 return false;
2052 }
2053 }
2054
2055 if (cis->qos->rx != NULL && cis->qos->rx->sdu != 0U) {
2056 is_p_to_c = true;
2057 }
2058
2059 if (cis->qos->tx != NULL && cis->qos->tx->sdu != 0U) {
2060 is_c_to_p = true;
2061 }
2062 }
2063
2064 if (param->framing != BT_ISO_FRAMING_UNFRAMED && param->framing != BT_ISO_FRAMING_FRAMED) {
2065 LOG_DBG("Invalid framing parameter: %u", param->framing);
2066 return false;
2067 }
2068
2069 if (param->packing != BT_ISO_PACKING_SEQUENTIAL &&
2070 param->packing != BT_ISO_PACKING_INTERLEAVED) {
2071 LOG_DBG("Invalid packing parameter: %u", param->packing);
2072 return false;
2073 }
2074
2075 if (param->num_cis > BT_ISO_MAX_GROUP_ISO_COUNT ||
2076 param->num_cis > CONFIG_BT_ISO_MAX_CHAN) {
2077 LOG_DBG("num_cis (%u) shall be lower than: %u", param->num_cis,
2078 MAX(CONFIG_BT_ISO_MAX_CHAN, BT_ISO_MAX_GROUP_ISO_COUNT));
2079 return false;
2080 }
2081
2082 if (is_c_to_p &&
2083 !IN_RANGE(param->c_to_p_interval, BT_ISO_SDU_INTERVAL_MIN, BT_ISO_SDU_INTERVAL_MAX)) {
2084 LOG_DBG("Invalid C to P interval: %u", param->c_to_p_interval);
2085 return false;
2086 }
2087
2088 if (is_p_to_c &&
2089 !IN_RANGE(param->p_to_c_interval, BT_ISO_SDU_INTERVAL_MIN, BT_ISO_SDU_INTERVAL_MAX)) {
2090 LOG_DBG("Invalid P to C interval: %u", param->p_to_c_interval);
2091 return false;
2092 }
2093
2094 if (!advanced) {
2095 if (is_c_to_p &&
2096 !IN_RANGE(param->c_to_p_latency, BT_ISO_LATENCY_MIN, BT_ISO_LATENCY_MAX)) {
2097 LOG_DBG("Invalid C to P latency: %u", param->c_to_p_latency);
2098 return false;
2099 }
2100
2101 if (is_p_to_c &&
2102 !IN_RANGE(param->p_to_c_latency, BT_ISO_LATENCY_MIN, BT_ISO_LATENCY_MAX)) {
2103 LOG_DBG("Invalid P to C latency: %u", param->p_to_c_latency);
2104 return false;
2105 }
2106 }
2107
2108 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
2109 if (advanced) {
2110 if (!IN_RANGE(param->c_to_p_ft, BT_ISO_FT_MIN, BT_ISO_FT_MAX)) {
2111 LOG_DBG("Invalid Central to Peripheral FT %u", param->c_to_p_ft);
2112
2113 return false;
2114 }
2115
2116 if (!IN_RANGE(param->p_to_c_ft, BT_ISO_FT_MIN, BT_ISO_FT_MAX)) {
2117 LOG_DBG("Invalid Peripheral to Central FT %u", param->p_to_c_ft);
2118
2119 return false;
2120 }
2121
2122 if (!IN_RANGE(param->iso_interval, BT_ISO_ISO_INTERVAL_MIN,
2123 BT_ISO_ISO_INTERVAL_MAX)) {
2124 LOG_DBG("Invalid ISO interval %u", param->iso_interval);
2125
2126 return false;
2127 }
2128 }
2129 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
2130
2131 return true;
2132 }
2133
bt_iso_cig_create(const struct bt_iso_cig_param * param,struct bt_iso_cig ** out_cig)2134 int bt_iso_cig_create(const struct bt_iso_cig_param *param, struct bt_iso_cig **out_cig)
2135 {
2136 int err;
2137 struct net_buf *rsp;
2138 struct bt_iso_cig *cig;
2139 struct bt_hci_rp_le_set_cig_params *cig_rsp;
2140 struct bt_iso_chan *cis;
2141 bool advanced = false;
2142 int i;
2143
2144 CHECKIF(param == NULL) {
2145 LOG_DBG("param is NULL");
2146 return -EINVAL;
2147 }
2148
2149 CHECKIF(out_cig == NULL) {
2150 LOG_DBG("out_cig is NULL");
2151 return -EINVAL;
2152 }
2153
2154 *out_cig = NULL;
2155
2156 /* Check if controller is ISO capable as a central */
2157 if (!BT_FEAT_LE_CIS_CENTRAL(bt_dev.le.features)) {
2158 return -ENOTSUP;
2159 }
2160
2161 /* TBD: Should we allow creating empty CIGs? */
2162 CHECKIF(param->cis_channels == NULL) {
2163 LOG_DBG("NULL CIS channels");
2164 return -EINVAL;
2165 }
2166
2167 CHECKIF(param->num_cis == 0) {
2168 LOG_DBG("Invalid number of CIS %u", param->num_cis);
2169 return -EINVAL;
2170 }
2171
2172 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
2173 advanced = is_advanced_cig_param(param);
2174 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
2175
2176 CHECKIF(!valid_cig_param(param, advanced, NULL)) {
2177 LOG_DBG("Invalid CIG params");
2178 return -EINVAL;
2179 }
2180
2181 cig = get_free_cig();
2182
2183 if (!cig) {
2184 return -ENOMEM;
2185 }
2186
2187 err = cig_init_cis(cig, param);
2188 if (err) {
2189 LOG_DBG("Could not init CIS %d", err);
2190 cleanup_cig(cig);
2191 return err;
2192 }
2193
2194 if (!advanced) {
2195 rsp = hci_le_set_cig_params(cig, param);
2196 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
2197 } else {
2198 rsp = hci_le_set_cig_test_params(cig, param);
2199 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
2200 }
2201
2202 if (rsp == NULL) {
2203 LOG_WRN("Unexpected response to hci_le_set_cig_params");
2204 err = -EIO;
2205 cleanup_cig(cig);
2206 return err;
2207 }
2208
2209 cig_rsp = (void *)rsp->data;
2210
2211 if (rsp->len < sizeof(cig_rsp) || cig_rsp->num_handles != param->num_cis) {
2212 LOG_WRN("Unexpected response to hci_le_set_cig_params");
2213 err = -EIO;
2214 net_buf_unref(rsp);
2215 cleanup_cig(cig);
2216 return err;
2217 }
2218
2219 i = 0;
2220 SYS_SLIST_FOR_EACH_CONTAINER(&cig->cis_channels, cis, node) {
2221 const uint16_t handle = cig_rsp->handle[i++];
2222
2223 /* Assign the connection handle */
2224 cis->iso->handle = sys_le16_to_cpu(handle);
2225 }
2226
2227 net_buf_unref(rsp);
2228
2229 *out_cig = cig;
2230
2231 return err;
2232 }
2233
restore_cig(struct bt_iso_cig * cig,uint8_t existing_num_cis)2234 static void restore_cig(struct bt_iso_cig *cig, uint8_t existing_num_cis)
2235 {
2236 struct bt_iso_chan *cis, *tmp;
2237
2238 SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&cig->cis_channels, cis, tmp, node) {
2239 /* Remove all newly added by comparing the cis_id to the number
2240 * of CIS that was previously added before
2241 * bt_iso_cig_reconfigure was called
2242 */
2243 if (cis->iso != NULL && cis->iso->iso.cis_id >= existing_num_cis) {
2244 bt_conn_unref(cis->iso);
2245 cis->iso = NULL;
2246
2247 sys_slist_remove(&cig->cis_channels, NULL, &cis->node);
2248 cig->num_cis--;
2249 }
2250 }
2251 }
2252
bt_iso_cig_reconfigure(struct bt_iso_cig * cig,const struct bt_iso_cig_param * param)2253 int bt_iso_cig_reconfigure(struct bt_iso_cig *cig, const struct bt_iso_cig_param *param)
2254 {
2255 struct bt_hci_rp_le_set_cig_params *cig_rsp;
2256 uint8_t existing_num_cis;
2257 bool advanced = false;
2258 struct net_buf *rsp;
2259 int err;
2260
2261 CHECKIF(cig == NULL) {
2262 LOG_DBG("cig is NULL");
2263 return -EINVAL;
2264 }
2265
2266 CHECKIF(param == NULL) {
2267 LOG_DBG("param is NULL");
2268 return -EINVAL;
2269 }
2270
2271 if (cig->state != BT_ISO_CIG_STATE_CONFIGURED) {
2272 LOG_DBG("Invalid CIG state: %u", cig->state);
2273 return -EINVAL;
2274 }
2275
2276 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
2277 advanced = is_advanced_cig_param(param);
2278 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
2279
2280 CHECKIF(!valid_cig_param(param, advanced, cig)) {
2281 LOG_DBG("Invalid CIG params");
2282 return -EINVAL;
2283 }
2284
2285 /* Used to restore CIG in case of error */
2286 existing_num_cis = cig->num_cis;
2287
2288 err = cig_init_cis(cig, param);
2289 if (err != 0) {
2290 LOG_DBG("Could not init CIS %d", err);
2291 restore_cig(cig, existing_num_cis);
2292 return err;
2293 }
2294
2295 if (!advanced) {
2296 rsp = hci_le_set_cig_params(cig, param);
2297 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
2298 } else {
2299 rsp = hci_le_set_cig_test_params(cig, param);
2300 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
2301 }
2302
2303 if (rsp == NULL) {
2304 LOG_WRN("Unexpected response to hci_le_set_cig_params");
2305 err = -EIO;
2306 restore_cig(cig, existing_num_cis);
2307 return err;
2308 }
2309
2310 cig_rsp = (void *)rsp->data;
2311
2312 if (rsp->len < sizeof(*cig_rsp)) {
2313 LOG_WRN("Unexpected response len to hci_le_set_cig_params %u != %zu", rsp->len,
2314 sizeof(*cig_rsp));
2315 err = -EIO;
2316 net_buf_unref(rsp);
2317 restore_cig(cig, existing_num_cis);
2318 return err;
2319 }
2320
2321 if (cig_rsp->num_handles != param->num_cis) {
2322 LOG_WRN("Unexpected response num_handles to hci_le_set_cig_params %u != %u",
2323 cig_rsp->num_handles, param->num_cis);
2324 err = -EIO;
2325 net_buf_unref(rsp);
2326 restore_cig(cig, existing_num_cis);
2327 return err;
2328 }
2329
2330 for (uint8_t i = 0u; i < param->num_cis; i++) {
2331 const uint16_t handle = cig_rsp->handle[i];
2332 struct bt_iso_chan *cis = param->cis_channels[i];
2333
2334 /* Assign the connection handle */
2335 cis->iso->handle = sys_le16_to_cpu(handle);
2336 }
2337
2338 net_buf_unref(rsp);
2339
2340 return err;
2341 }
2342
bt_iso_cig_terminate(struct bt_iso_cig * cig)2343 int bt_iso_cig_terminate(struct bt_iso_cig *cig)
2344 {
2345 int err;
2346
2347 CHECKIF(cig == NULL) {
2348 LOG_DBG("cig is NULL");
2349 return -EINVAL;
2350 }
2351
2352 if (cig->state != BT_ISO_CIG_STATE_INACTIVE && cig->state != BT_ISO_CIG_STATE_CONFIGURED) {
2353 LOG_DBG("Invalid CIG state: %u", cig->state);
2354 return -EINVAL;
2355 }
2356
2357 err = hci_le_remove_cig(cig->id);
2358 if (err != 0) {
2359 LOG_DBG("Failed to terminate CIG: %d", err);
2360 return err;
2361 }
2362
2363 cleanup_cig(cig);
2364
2365 return 0;
2366 }
2367
bt_iso_security_changed(struct bt_conn * acl,uint8_t hci_status)2368 void bt_iso_security_changed(struct bt_conn *acl, uint8_t hci_status)
2369 {
2370 struct bt_iso_connect_param param[CONFIG_BT_ISO_MAX_CHAN];
2371 size_t param_count;
2372 int err;
2373
2374 /* The peripheral does not accept any ISO requests if security is
2375 * insufficient, so we only need to handle central here.
2376 * BT_ISO_STATE_ENCRYPT_PENDING is only set by the central.
2377 */
2378 if (!IS_ENABLED(CONFIG_BT_CENTRAL) || acl->role != BT_CONN_ROLE_CENTRAL) {
2379 return;
2380 }
2381
2382 param_count = 0;
2383 for (size_t i = 0; i < ARRAY_SIZE(iso_conns); i++) {
2384 struct bt_conn *iso = &iso_conns[i];
2385 struct bt_iso_chan *iso_chan;
2386
2387 if (iso == NULL || iso->iso.acl != acl) {
2388 continue;
2389 }
2390
2391 iso_chan = iso_chan(iso);
2392 if (iso_chan->state != BT_ISO_STATE_ENCRYPT_PENDING) {
2393 continue;
2394 }
2395
2396 /* Set state to disconnected to indicate that we are no longer waiting for
2397 * encryption.
2398 * TODO: Remove the BT_ISO_STATE_ENCRYPT_PENDING state and replace with a flag to
2399 * avoid these unnecessary state changes
2400 */
2401 bt_iso_chan_set_state(iso_chan, BT_ISO_STATE_DISCONNECTED);
2402
2403 if (hci_status == BT_HCI_ERR_SUCCESS) {
2404 param[param_count].acl = acl;
2405 param[param_count].iso_chan = iso_chan;
2406 param_count++;
2407 } else {
2408 LOG_DBG("Failed to encrypt ACL %p for ISO %p: %u %s", acl, iso, hci_status,
2409 bt_hci_err_to_str(hci_status));
2410
2411 /* We utilize the disconnected callback to make the
2412 * upper layers aware of the error
2413 */
2414 if (iso_chan->ops->disconnected) {
2415 iso_chan->ops->disconnected(iso_chan, hci_status);
2416 }
2417 }
2418 }
2419
2420 if (param_count == 0) {
2421 /* Nothing to do for ISO. This happens if security is changed,
2422 * but no ISO channels were pending encryption.
2423 */
2424 return;
2425 }
2426
2427 err = hci_le_create_cis(param, param_count);
2428 if (err != 0) {
2429 LOG_ERR("Failed to connect CISes: %d", err);
2430
2431 for (size_t i = 0; i < param_count; i++) {
2432 struct bt_iso_chan *iso_chan = param[i].iso_chan;
2433
2434 /* We utilize the disconnected callback to make the
2435 * upper layers aware of the error
2436 */
2437 if (iso_chan->ops->disconnected) {
2438 iso_chan->ops->disconnected(iso_chan, hci_status);
2439 }
2440 }
2441
2442 return;
2443 }
2444
2445 /* Set connection states */
2446 for (size_t i = 0; i < param_count; i++) {
2447 struct bt_iso_chan *iso_chan = param[i].iso_chan;
2448 struct bt_iso_cig *cig = get_cig(iso_chan);
2449
2450 __ASSERT(cig != NULL, "CIG was NULL");
2451 cig->state = BT_ISO_CIG_STATE_ACTIVE;
2452
2453 bt_conn_set_state(iso_chan->iso, BT_CONN_INITIATING);
2454 bt_iso_chan_set_state(iso_chan, BT_ISO_STATE_CONNECTING);
2455 }
2456 }
2457
hci_le_create_cis(const struct bt_iso_connect_param * param,size_t count)2458 static int hci_le_create_cis(const struct bt_iso_connect_param *param, size_t count)
2459 {
2460 struct bt_hci_cis *cis;
2461 struct bt_hci_cp_le_create_cis *req;
2462 struct net_buf *buf;
2463
2464 buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CIS, sizeof(*req) + sizeof(*cis) * count);
2465 if (!buf) {
2466 return -ENOBUFS;
2467 }
2468
2469 req = net_buf_add(buf, sizeof(*req));
2470
2471 memset(req, 0, sizeof(*req));
2472
2473 /* Program the cis parameters */
2474 for (size_t i = 0; i < count; i++) {
2475 struct bt_iso_chan *iso_chan = param[i].iso_chan;
2476
2477 if (iso_chan->state == BT_ISO_STATE_ENCRYPT_PENDING) {
2478 continue;
2479 }
2480
2481 cis = net_buf_add(buf, sizeof(*cis));
2482
2483 memset(cis, 0, sizeof(*cis));
2484
2485 cis->cis_handle = sys_cpu_to_le16(param[i].iso_chan->iso->handle);
2486 cis->acl_handle = sys_cpu_to_le16(param[i].acl->handle);
2487 req->num_cis++;
2488 }
2489
2490 /* If all CIS are pending for security, do nothing,
2491 * but return a recognizable return value
2492 */
2493 if (req->num_cis == 0) {
2494 net_buf_unref(buf);
2495
2496 return -ECANCELED;
2497 }
2498
2499 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_CIS, buf, NULL);
2500 }
2501
2502 #if defined(CONFIG_BT_SMP)
iso_chan_connect_security(const struct bt_iso_connect_param * param,size_t count)2503 static int iso_chan_connect_security(const struct bt_iso_connect_param *param, size_t count)
2504 {
2505 /* conn_idx_handled is an array of booleans for which conn indexes
2506 * already have been used to call bt_conn_set_security.
2507 * Using indexes avoids looping the array when checking if it has been
2508 * handled.
2509 */
2510 bool conn_idx_handled[CONFIG_BT_MAX_CONN];
2511
2512 memset(conn_idx_handled, false, sizeof(conn_idx_handled));
2513 for (size_t i = 0; i < count; i++) {
2514 struct bt_iso_chan *iso_chan = param[i].iso_chan;
2515 struct bt_conn *acl = param[i].acl;
2516 uint8_t conn_idx = bt_conn_index(acl);
2517
2518 if (acl->sec_level < iso_chan->required_sec_level) {
2519 if (!conn_idx_handled[conn_idx]) {
2520 int err;
2521
2522 err = bt_conn_set_security(acl, iso_chan->required_sec_level);
2523 if (err != 0) {
2524 LOG_DBG("[%zu]: Failed to set security: %d", i, err);
2525
2526 /* Restore states */
2527 for (size_t j = 0; j < i; j++) {
2528 iso_chan = param[j].iso_chan;
2529
2530 bt_iso_cleanup_acl(iso_chan->iso);
2531 bt_iso_chan_set_state(iso_chan,
2532 BT_ISO_STATE_DISCONNECTED);
2533 }
2534
2535 return err;
2536 }
2537
2538 conn_idx_handled[conn_idx] = true;
2539 }
2540
2541 iso_chan->iso->iso.acl = bt_conn_ref(acl);
2542 bt_iso_chan_set_state(iso_chan, BT_ISO_STATE_ENCRYPT_PENDING);
2543 }
2544 }
2545
2546 return 0;
2547 }
2548 #endif /* CONFIG_BT_SMP */
2549
iso_chans_connecting(void)2550 static bool iso_chans_connecting(void)
2551 {
2552 for (size_t i = 0U; i < ARRAY_SIZE(iso_conns); i++) {
2553 const struct bt_conn *iso = &iso_conns[i];
2554 const struct bt_iso_chan *iso_chan;
2555
2556 if (iso == NULL || iso->iso.info.type != BT_ISO_CHAN_TYPE_CONNECTED) {
2557 continue;
2558 }
2559
2560 iso_chan = iso_chan(iso);
2561 if (iso_chan->state == BT_ISO_STATE_CONNECTING ||
2562 iso_chan->state == BT_ISO_STATE_ENCRYPT_PENDING) {
2563 return true;
2564 }
2565 }
2566
2567 return false;
2568 }
2569
bt_iso_chan_connect(const struct bt_iso_connect_param * param,size_t count)2570 int bt_iso_chan_connect(const struct bt_iso_connect_param *param, size_t count)
2571 {
2572 int err;
2573
2574 CHECKIF(param == NULL) {
2575 LOG_DBG("param is NULL");
2576 return -EINVAL;
2577 }
2578
2579 CHECKIF(count == 0) {
2580 LOG_DBG("Invalid count %zu", count);
2581 return -EINVAL;
2582 }
2583
2584 CHECKIF(count > CONFIG_BT_ISO_MAX_CHAN) {
2585 return -EINVAL;
2586 }
2587
2588 /* Validate input */
2589 for (size_t i = 0; i < count; i++) {
2590 CHECKIF(param[i].iso_chan == NULL) {
2591 LOG_DBG("[%zu]: Invalid iso (%p)", i, param[i].iso_chan);
2592 return -EINVAL;
2593 }
2594
2595 CHECKIF(param[i].acl == NULL) {
2596 LOG_DBG("[%zu]: Invalid acl (%p)", i, param[i].acl);
2597 return -EINVAL;
2598 }
2599
2600 CHECKIF((param[i].acl->type & BT_CONN_TYPE_LE) == 0) {
2601 LOG_DBG("[%zu]: acl type (%u) shall be an LE connection", i,
2602 param[i].acl->type);
2603 return -EINVAL;
2604 }
2605
2606 if (param[i].iso_chan->iso == NULL) {
2607 LOG_DBG("[%zu]: ISO has not been initialized in a CIG", i);
2608 return -EINVAL;
2609 }
2610
2611 if (param[i].iso_chan->state != BT_ISO_STATE_DISCONNECTED) {
2612 LOG_DBG("[%zu]: ISO is not in the BT_ISO_STATE_DISCONNECTED state: %u", i,
2613 param[i].iso_chan->state);
2614 return -EINVAL;
2615 }
2616 }
2617
2618 if (iso_chans_connecting()) {
2619 LOG_DBG("There are pending ISO connections");
2620 return -EBUSY;
2621 }
2622
2623 #if defined(CONFIG_BT_SMP)
2624 /* Check for and initiate security for all channels that have
2625 * requested encryption if the ACL link is not already secured
2626 */
2627 err = iso_chan_connect_security(param, count);
2628 if (err != 0) {
2629 LOG_DBG("Failed to initiate security for all CIS: %d", err);
2630 return err;
2631 }
2632 #endif /* CONFIG_BT_SMP */
2633
2634 err = hci_le_create_cis(param, count);
2635 if (err == -ECANCELED) {
2636 LOG_DBG("All channels are pending on security");
2637
2638 return 0;
2639 } else if (err != 0) {
2640 LOG_DBG("Failed to connect CISes: %d", err);
2641
2642 return err;
2643 }
2644
2645 /* Set connection states */
2646 for (size_t i = 0; i < count; i++) {
2647 struct bt_iso_chan *iso_chan = param[i].iso_chan;
2648 struct bt_iso_cig *cig;
2649
2650 if (iso_chan->state == BT_ISO_STATE_ENCRYPT_PENDING) {
2651 continue;
2652 }
2653
2654 iso_chan->iso->iso.acl = bt_conn_ref(param[i].acl);
2655 bt_conn_set_state(iso_chan->iso, BT_CONN_INITIATING);
2656 bt_iso_chan_set_state(iso_chan, BT_ISO_STATE_CONNECTING);
2657
2658 cig = get_cig(iso_chan);
2659 __ASSERT(cig != NULL, "CIG was NULL");
2660 cig->state = BT_ISO_CIG_STATE_ACTIVE;
2661 }
2662
2663 return 0;
2664 }
2665 #endif /* CONFIG_BT_ISO_CENTRAL */
2666 #endif /* CONFIG_BT_ISO_UNICAST */
2667
2668 #if defined(CONFIG_BT_ISO_BROADCAST)
2669 static sys_slist_t iso_big_cbs = SYS_SLIST_STATIC_INIT(&iso_big_cbs);
2670
lookup_big_by_handle(uint8_t big_handle)2671 static struct bt_iso_big *lookup_big_by_handle(uint8_t big_handle)
2672 {
2673 return &bigs[big_handle];
2674 }
2675
get_free_big(void)2676 static struct bt_iso_big *get_free_big(void)
2677 {
2678 /* We can use the index in the `bigs` array as BIG handles, for both
2679 * broadcaster and receiver (even if the device is both!)
2680 */
2681
2682 for (size_t i = 0; i < ARRAY_SIZE(bigs); i++) {
2683 if (!atomic_test_and_set_bit(bigs[i].flags, BT_BIG_INITIALIZED)) {
2684 bigs[i].handle = i;
2685 sys_slist_init(&bigs[i].bis_channels);
2686 return &bigs[i];
2687 }
2688 }
2689
2690 LOG_DBG("Could not allocate any more BIGs");
2691
2692 return NULL;
2693 }
2694
big_lookup_flag(int bit)2695 static struct bt_iso_big *big_lookup_flag(int bit)
2696 {
2697 for (size_t i = 0; i < ARRAY_SIZE(bigs); i++) {
2698 if (atomic_test_bit(bigs[i].flags, bit)) {
2699 return &bigs[i];
2700 }
2701 }
2702
2703 LOG_DBG("No BIG with flag bit %d set", bit);
2704
2705 return NULL;
2706 }
2707
cleanup_big(struct bt_iso_big * big)2708 static void cleanup_big(struct bt_iso_big *big)
2709 {
2710 struct bt_iso_chan *bis, *tmp;
2711
2712 SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&big->bis_channels, bis, tmp, node) {
2713 if (bis->iso != NULL) {
2714 bt_conn_unref(bis->iso);
2715 bis->iso = NULL;
2716 }
2717
2718 sys_slist_remove(&big->bis_channels, NULL, &bis->node);
2719 }
2720
2721 memset(big, 0, sizeof(*big));
2722 }
2723
big_disconnect(struct bt_iso_big * big,uint8_t reason)2724 static void big_disconnect(struct bt_iso_big *big, uint8_t reason)
2725 {
2726 struct bt_iso_chan *bis;
2727
2728 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
2729 bis->iso->err = reason;
2730
2731 bt_iso_chan_disconnected(bis, reason);
2732 }
2733
2734 if (!sys_slist_is_empty(&iso_big_cbs)) {
2735 struct bt_iso_big_cb *listener;
2736
2737 SYS_SLIST_FOR_EACH_CONTAINER(&iso_big_cbs, listener, _node) {
2738 if (listener->stopped != NULL) {
2739 listener->stopped(big, reason);
2740 }
2741 }
2742 }
2743 }
2744
big_init_bis(struct bt_iso_big * big,struct bt_iso_chan ** bis_channels,uint8_t num_bis,bool broadcaster)2745 static int big_init_bis(struct bt_iso_big *big, struct bt_iso_chan **bis_channels, uint8_t num_bis,
2746 bool broadcaster)
2747 {
2748 for (uint8_t i = 0; i < num_bis; i++) {
2749 struct bt_iso_chan *bis = bis_channels[i];
2750 struct bt_conn_iso *iso_conn;
2751
2752 bis->iso = iso_new();
2753
2754 if (!bis->iso) {
2755 LOG_ERR("Unable to allocate BIS connection");
2756 return -ENOMEM;
2757 }
2758 iso_conn = &bis->iso->iso;
2759
2760 iso_conn->big_handle = big->handle;
2761 iso_conn->info.type =
2762 broadcaster ? BT_ISO_CHAN_TYPE_BROADCASTER : BT_ISO_CHAN_TYPE_SYNC_RECEIVER;
2763 iso_conn->bis_id = bt_conn_index(bis->iso);
2764
2765 bt_iso_chan_add(bis->iso, bis);
2766
2767 sys_slist_append(&big->bis_channels, &bis->node);
2768 }
2769
2770 return 0;
2771 }
2772
bt_iso_big_register_cb(struct bt_iso_big_cb * cb)2773 int bt_iso_big_register_cb(struct bt_iso_big_cb *cb)
2774 {
2775 CHECKIF(cb == NULL) {
2776 LOG_DBG("cb is NULL");
2777
2778 return -EINVAL;
2779 }
2780
2781 if (sys_slist_find(&iso_big_cbs, &cb->_node, NULL)) {
2782 LOG_DBG("cb %p is already registered", cb);
2783
2784 return -EEXIST;
2785 }
2786
2787 sys_slist_append(&iso_big_cbs, &cb->_node);
2788
2789 return 0;
2790 }
2791
2792 #if defined(CONFIG_BT_ISO_BROADCASTER)
hci_le_create_big(struct bt_le_ext_adv * padv,struct bt_iso_big * big,struct bt_iso_big_create_param * param)2793 static int hci_le_create_big(struct bt_le_ext_adv *padv, struct bt_iso_big *big,
2794 struct bt_iso_big_create_param *param)
2795 {
2796 const struct bt_iso_chan_io_qos *qos;
2797 struct bt_hci_cp_le_create_big *req;
2798 struct bt_hci_cmd_state_set state;
2799 struct net_buf *buf;
2800 int err;
2801 struct bt_iso_chan *bis;
2802
2803 buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_BIG, sizeof(*req));
2804
2805 if (!buf) {
2806 return -ENOBUFS;
2807 }
2808
2809 bis = SYS_SLIST_PEEK_HEAD_CONTAINER(&big->bis_channels, bis, node);
2810 __ASSERT(bis != NULL, "bis was NULL");
2811
2812 /* All BIS will share the same QOS */
2813 qos = bis->qos->tx;
2814
2815 req = net_buf_add(buf, sizeof(*req));
2816 req->big_handle = big->handle;
2817 req->adv_handle = padv->handle;
2818 req->num_bis = big->num_bis;
2819 sys_put_le24(param->interval, req->sdu_interval);
2820 req->max_sdu = sys_cpu_to_le16(qos->sdu);
2821 req->max_latency = sys_cpu_to_le16(param->latency);
2822 req->rtn = qos->rtn;
2823 req->phy = qos->phy;
2824 req->packing = param->packing;
2825 req->framing = param->framing;
2826 req->encryption = param->encryption;
2827 if (req->encryption) {
2828 memcpy(req->bcode, param->bcode, sizeof(req->bcode));
2829 } else {
2830 memset(req->bcode, 0, sizeof(req->bcode));
2831 }
2832
2833 LOG_DBG("BIG handle 0x%02x, adv_handle 0x%02x, num_bis %u, sdu_interval %u, max_sdu %u, "
2834 "max_latency %u, rtn %u, phy %u, packing %u, framing %u, encryption %u",
2835 big->handle, padv->handle, big->num_bis, param->interval, qos->sdu, param->latency,
2836 qos->rtn, qos->phy, param->packing, param->framing, param->encryption);
2837
2838 bt_hci_cmd_state_set_init(buf, &state, big->flags, BT_BIG_PENDING, true);
2839 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_BIG, buf, NULL);
2840
2841 if (err) {
2842 return err;
2843 }
2844
2845 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
2846 bt_iso_chan_set_state(bis, BT_ISO_STATE_CONNECTING);
2847 }
2848
2849 return err;
2850 }
2851
2852 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
hci_le_create_big_test(const struct bt_le_ext_adv * padv,struct bt_iso_big * big,const struct bt_iso_big_create_param * param)2853 static int hci_le_create_big_test(const struct bt_le_ext_adv *padv, struct bt_iso_big *big,
2854 const struct bt_iso_big_create_param *param)
2855 {
2856 struct bt_hci_cp_le_create_big_test *req;
2857 struct bt_hci_cmd_state_set state;
2858 const struct bt_iso_chan_qos *qos;
2859 struct bt_iso_chan *bis;
2860 struct net_buf *buf;
2861 int err;
2862
2863 buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_BIG_TEST, sizeof(*req));
2864
2865 if (!buf) {
2866 return -ENOBUFS;
2867 }
2868
2869 bis = SYS_SLIST_PEEK_HEAD_CONTAINER(&big->bis_channels, bis, node);
2870 __ASSERT(bis != NULL, "bis was NULL");
2871
2872 /* All BIS will share the same QOS */
2873 qos = bis->qos;
2874
2875 req = net_buf_add(buf, sizeof(*req));
2876 req->big_handle = big->handle;
2877 req->adv_handle = padv->handle;
2878 req->num_bis = big->num_bis;
2879 sys_put_le24(param->interval, req->sdu_interval);
2880 req->iso_interval = sys_cpu_to_le16(param->iso_interval);
2881 req->nse = qos->num_subevents;
2882 req->max_sdu = sys_cpu_to_le16(qos->tx->sdu);
2883 req->max_pdu = sys_cpu_to_le16(qos->tx->max_pdu);
2884 req->phy = qos->tx->phy;
2885 req->packing = param->packing;
2886 req->framing = param->framing;
2887 req->bn = qos->tx->burst_number;
2888 req->irc = param->irc;
2889 req->pto = param->pto;
2890 req->encryption = param->encryption;
2891 if (req->encryption) {
2892 memcpy(req->bcode, param->bcode, sizeof(req->bcode));
2893 } else {
2894 memset(req->bcode, 0, sizeof(req->bcode));
2895 }
2896
2897 LOG_DBG("BIG handle %u, adv handle %u, num_bis %u, SDU interval %u, "
2898 "ISO interval %u, NSE %u, SDU %u, PDU %u, PHY %u, packing %u, "
2899 "framing %u, BN %u, IRC %u, PTO %u, encryption %u",
2900 req->big_handle, req->adv_handle, req->num_bis, param->interval,
2901 param->iso_interval, req->nse, req->max_sdu, req->max_pdu, req->phy, req->packing,
2902 req->framing, req->bn, req->irc, req->pto, req->encryption);
2903
2904 bt_hci_cmd_state_set_init(buf, &state, big->flags, BT_BIG_PENDING, true);
2905 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_BIG_TEST, buf, NULL);
2906 if (err) {
2907 return err;
2908 }
2909
2910 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
2911 bt_iso_chan_set_state(bis, BT_ISO_STATE_CONNECTING);
2912 }
2913
2914 return err;
2915 }
2916
is_advanced_big_param(const struct bt_iso_big_create_param * param)2917 static bool is_advanced_big_param(const struct bt_iso_big_create_param *param)
2918 {
2919 if (param->irc != 0U || param->iso_interval != 0U) {
2920 return true;
2921 }
2922
2923 /* Check if any of the CIS contain any test-param-only values */
2924 for (uint8_t i = 0U; i < param->num_bis; i++) {
2925 const struct bt_iso_chan *bis = param->bis_channels[i];
2926 const struct bt_iso_chan_qos *qos = bis->qos;
2927
2928 if (qos->num_subevents > 0U) {
2929 return true;
2930 }
2931
2932 __ASSERT(qos->tx != NULL, "TX cannot be NULL for broadcaster");
2933
2934 if (qos->tx->max_pdu > 0U || qos->tx->burst_number > 0U) {
2935 return true;
2936 }
2937 }
2938
2939 return false;
2940 }
2941 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
2942
valid_big_param(const struct bt_iso_big_create_param * param,bool advanced)2943 static bool valid_big_param(const struct bt_iso_big_create_param *param, bool advanced)
2944 {
2945 CHECKIF(!param->bis_channels) {
2946 LOG_DBG("NULL BIS channels");
2947
2948 return false;
2949 }
2950
2951 CHECKIF(!param->num_bis) {
2952 LOG_DBG("Invalid number of BIS %u", param->num_bis);
2953
2954 return false;
2955 }
2956
2957 for (uint8_t i = 0; i < param->num_bis; i++) {
2958 struct bt_iso_chan *bis = param->bis_channels[i];
2959
2960 CHECKIF(bis == NULL) {
2961 LOG_DBG("bis_channels[%u]: NULL channel", i);
2962
2963 return false;
2964 }
2965
2966 if (bis->iso) {
2967 LOG_DBG("bis_channels[%u]: already allocated", i);
2968
2969 return false;
2970 }
2971
2972 CHECKIF(bis->qos == NULL) {
2973 LOG_DBG("bis_channels[%u]: qos is NULL", i);
2974
2975 return false;
2976 }
2977
2978 CHECKIF(bis->qos->tx == NULL ||
2979 !valid_chan_io_qos(bis->qos->tx, true, true, advanced)) {
2980 LOG_DBG("bis_channels[%u]: Invalid QOS", i);
2981
2982 return false;
2983 }
2984 }
2985
2986 CHECKIF(param->framing != BT_ISO_FRAMING_UNFRAMED &&
2987 param->framing != BT_ISO_FRAMING_FRAMED) {
2988 LOG_DBG("Invalid framing parameter: %u", param->framing);
2989
2990 return false;
2991 }
2992
2993 CHECKIF(param->packing != BT_ISO_PACKING_SEQUENTIAL &&
2994 param->packing != BT_ISO_PACKING_INTERLEAVED) {
2995 LOG_DBG("Invalid packing parameter: %u", param->packing);
2996
2997 return false;
2998 }
2999
3000 CHECKIF(param->num_bis > BT_ISO_MAX_GROUP_ISO_COUNT ||
3001 param->num_bis > CONFIG_BT_ISO_MAX_CHAN) {
3002 LOG_DBG("num_bis (%u) shall be lower than: %u", param->num_bis,
3003 MAX(CONFIG_BT_ISO_MAX_CHAN, BT_ISO_MAX_GROUP_ISO_COUNT));
3004
3005 return false;
3006 }
3007
3008 CHECKIF(!IN_RANGE(param->interval, BT_ISO_SDU_INTERVAL_MIN, BT_ISO_SDU_INTERVAL_MAX)) {
3009 LOG_DBG("Invalid interval: %u", param->interval);
3010
3011 return false;
3012 }
3013
3014 CHECKIF(!advanced && !IN_RANGE(param->latency, BT_ISO_LATENCY_MIN, BT_ISO_LATENCY_MAX)) {
3015 LOG_DBG("Invalid latency: %u", param->latency);
3016
3017 return false;
3018 }
3019
3020 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
3021 if (advanced) {
3022 CHECKIF(!IN_RANGE(param->irc, BT_ISO_IRC_MIN, BT_ISO_IRC_MAX)) {
3023 LOG_DBG("Invalid IRC %u", param->irc);
3024
3025 return false;
3026 }
3027
3028 CHECKIF(!IN_RANGE(param->pto, BT_ISO_PTO_MIN, BT_ISO_PTO_MAX)) {
3029 LOG_DBG("Invalid PTO %u", param->pto);
3030
3031 return false;
3032 }
3033
3034 CHECKIF(!IN_RANGE(param->iso_interval, BT_ISO_ISO_INTERVAL_MIN,
3035 BT_ISO_ISO_INTERVAL_MAX)) {
3036 LOG_DBG("Invalid ISO interval %u", param->iso_interval);
3037
3038 return false;
3039 }
3040 }
3041 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
3042
3043 return true;
3044 }
3045
bt_iso_big_create(struct bt_le_ext_adv * padv,struct bt_iso_big_create_param * param,struct bt_iso_big ** out_big)3046 int bt_iso_big_create(struct bt_le_ext_adv *padv, struct bt_iso_big_create_param *param,
3047 struct bt_iso_big **out_big)
3048 {
3049 int err;
3050 struct bt_iso_big *big;
3051 bool advanced = false;
3052
3053 CHECKIF(padv == NULL) {
3054 LOG_DBG("padv is NULL");
3055 return -EINVAL;
3056 }
3057
3058 CHECKIF(param == NULL) {
3059 LOG_DBG("param is NULL");
3060 return -EINVAL;
3061 }
3062
3063 CHECKIF(out_big == NULL) {
3064 LOG_DBG("out_big is NULL");
3065 return -EINVAL;
3066 }
3067
3068 if (!atomic_test_bit(padv->flags, BT_PER_ADV_PARAMS_SET)) {
3069 LOG_DBG("PA params not set; invalid adv object");
3070 return -EINVAL;
3071 }
3072
3073 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
3074 advanced = is_advanced_big_param(param);
3075 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
3076
3077 if (!valid_big_param(param, advanced)) {
3078 LOG_DBG("Invalid BIG parameters");
3079
3080 return -EINVAL;
3081 }
3082
3083 big = get_free_big();
3084
3085 if (!big) {
3086 return -ENOMEM;
3087 }
3088
3089 err = big_init_bis(big, param->bis_channels, param->num_bis, true);
3090 if (err) {
3091 LOG_DBG("Could not init BIG %d", err);
3092 cleanup_big(big);
3093 return err;
3094 }
3095 big->num_bis = param->num_bis;
3096
3097 if (!advanced) {
3098 err = hci_le_create_big(padv, big, param);
3099 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
3100 } else {
3101 err = hci_le_create_big_test(padv, big, param);
3102 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
3103 }
3104
3105 if (err) {
3106 LOG_DBG("Could not create BIG %d", err);
3107 cleanup_big(big);
3108 return err;
3109 }
3110
3111 *out_big = big;
3112
3113 return err;
3114 }
3115
store_bis_broadcaster_info(const struct bt_hci_evt_le_big_complete * evt,struct bt_iso_info * info)3116 static void store_bis_broadcaster_info(const struct bt_hci_evt_le_big_complete *evt,
3117 struct bt_iso_info *info)
3118 {
3119 struct bt_iso_broadcaster_info *broadcaster_info = &info->broadcaster;
3120
3121 info->iso_interval = sys_le16_to_cpu(evt->iso_interval);
3122 info->max_subevent = evt->nse;
3123
3124 broadcaster_info->sync_delay = sys_get_le24(evt->sync_delay);
3125 broadcaster_info->latency = sys_get_le24(evt->latency);
3126 broadcaster_info->phy = bt_get_phy(evt->phy);
3127 broadcaster_info->bn = evt->bn;
3128 broadcaster_info->irc = evt->irc;
3129 /* Transform to n * 1.25ms */
3130 broadcaster_info->pto = info->iso_interval * evt->pto;
3131 broadcaster_info->max_pdu = sys_le16_to_cpu(evt->max_pdu);
3132
3133 info->can_send = true;
3134 info->can_recv = false;
3135 }
3136
hci_le_big_complete(struct net_buf * buf)3137 void hci_le_big_complete(struct net_buf *buf)
3138 {
3139 struct bt_hci_evt_le_big_complete *evt = (void *)buf->data;
3140 struct bt_iso_chan *bis;
3141 struct bt_iso_big *big;
3142 int i;
3143
3144 if (evt->big_handle >= ARRAY_SIZE(bigs)) {
3145 LOG_WRN("Invalid BIG handle");
3146
3147 big = big_lookup_flag(BT_BIG_PENDING);
3148 if (big) {
3149 big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
3150 cleanup_big(big);
3151 }
3152
3153 return;
3154 }
3155
3156 big = lookup_big_by_handle(evt->big_handle);
3157 atomic_clear_bit(big->flags, BT_BIG_PENDING);
3158
3159 LOG_DBG("BIG[%u] %p completed, status 0x%02x %s", big->handle, big, evt->status,
3160 bt_hci_err_to_str(evt->status));
3161
3162 if (evt->status || evt->num_bis != big->num_bis) {
3163 if (evt->status == BT_HCI_ERR_SUCCESS && evt->num_bis != big->num_bis) {
3164 LOG_ERR("Invalid number of BIS created, was %u expected %u", evt->num_bis,
3165 big->num_bis);
3166 }
3167 big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
3168 cleanup_big(big);
3169 return;
3170 }
3171
3172 i = 0;
3173 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
3174 const uint16_t handle = evt->handle[i++];
3175 struct bt_conn *iso_conn = bis->iso;
3176
3177 iso_conn->handle = sys_le16_to_cpu(handle);
3178 store_bis_broadcaster_info(evt, &iso_conn->iso.info);
3179 bt_conn_set_state(iso_conn, BT_CONN_CONNECTED);
3180 }
3181
3182 if (!sys_slist_is_empty(&iso_big_cbs)) {
3183 struct bt_iso_big_cb *listener;
3184
3185 SYS_SLIST_FOR_EACH_CONTAINER(&iso_big_cbs, listener, _node) {
3186 if (listener->started != NULL) {
3187 listener->started(big);
3188 }
3189 }
3190 }
3191 }
3192
hci_le_big_terminate(struct net_buf * buf)3193 void hci_le_big_terminate(struct net_buf *buf)
3194 {
3195 struct bt_hci_evt_le_big_terminate *evt = (void *)buf->data;
3196 struct bt_iso_big *big;
3197
3198 if (evt->big_handle >= ARRAY_SIZE(bigs)) {
3199 LOG_WRN("Invalid BIG handle");
3200 return;
3201 }
3202
3203 big = lookup_big_by_handle(evt->big_handle);
3204
3205 LOG_DBG("BIG[%u] %p terminated", big->handle, big);
3206
3207 big_disconnect(big, evt->reason);
3208 cleanup_big(big);
3209 }
3210 #endif /* CONFIG_BT_ISO_BROADCASTER */
3211
hci_le_terminate_big(struct bt_iso_big * big)3212 static int hci_le_terminate_big(struct bt_iso_big *big)
3213 {
3214 struct bt_hci_cp_le_terminate_big *req;
3215 struct net_buf *buf;
3216
3217 buf = bt_hci_cmd_create(BT_HCI_OP_LE_TERMINATE_BIG, sizeof(*req));
3218 if (!buf) {
3219 return -ENOBUFS;
3220 }
3221
3222 req = net_buf_add(buf, sizeof(*req));
3223 req->big_handle = big->handle;
3224 req->reason = BT_HCI_ERR_REMOTE_USER_TERM_CONN;
3225
3226 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_TERMINATE_BIG, buf, NULL);
3227 }
3228
hci_le_big_sync_term(struct bt_iso_big * big)3229 static int hci_le_big_sync_term(struct bt_iso_big *big)
3230 {
3231 struct bt_hci_cp_le_big_terminate_sync *req;
3232 struct bt_hci_rp_le_big_terminate_sync *evt;
3233 struct net_buf *buf;
3234 struct net_buf *rsp;
3235 int err;
3236
3237 buf = bt_hci_cmd_create(BT_HCI_OP_LE_BIG_TERMINATE_SYNC, sizeof(*req));
3238 if (!buf) {
3239 return -ENOBUFS;
3240 }
3241
3242 req = net_buf_add(buf, sizeof(*req));
3243 req->big_handle = big->handle;
3244 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_BIG_TERMINATE_SYNC, buf, &rsp);
3245 if (err) {
3246 return err;
3247 }
3248
3249 evt = (struct bt_hci_rp_le_big_terminate_sync *)rsp->data;
3250 if (evt->status || (evt->big_handle != big->handle)) {
3251 err = -EIO;
3252 }
3253
3254 net_buf_unref(rsp);
3255
3256 return err;
3257 }
3258
bt_iso_big_terminate(struct bt_iso_big * big)3259 int bt_iso_big_terminate(struct bt_iso_big *big)
3260 {
3261 struct bt_iso_chan *bis;
3262 int err;
3263
3264 CHECKIF(big == NULL) {
3265 LOG_DBG("big is NULL");
3266 return -EINVAL;
3267 }
3268
3269 if (!atomic_test_bit(big->flags, BT_BIG_INITIALIZED) || !big->num_bis) {
3270 LOG_DBG("BIG not initialized");
3271 return -EINVAL;
3272 }
3273
3274 bis = SYS_SLIST_PEEK_HEAD_CONTAINER(&big->bis_channels, bis, node);
3275 __ASSERT(bis != NULL, "bis was NULL");
3276
3277 if (IS_ENABLED(CONFIG_BT_ISO_BROADCASTER) &&
3278 bis->iso->iso.info.type == BT_ISO_CHAN_TYPE_BROADCASTER) {
3279 err = hci_le_terminate_big(big);
3280
3281 /* Wait for BT_HCI_EVT_LE_BIG_TERMINATE before cleaning up
3282 * the BIG in hci_le_big_terminate
3283 */
3284 if (!err) {
3285 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
3286 bt_iso_chan_set_state(bis, BT_ISO_STATE_DISCONNECTING);
3287 }
3288 }
3289 } else if (IS_ENABLED(CONFIG_BT_ISO_SYNC_RECEIVER) &&
3290 bis->iso->iso.info.type == BT_ISO_CHAN_TYPE_SYNC_RECEIVER) {
3291 err = hci_le_big_sync_term(big);
3292
3293 if (!err) {
3294 big_disconnect(big, BT_HCI_ERR_LOCALHOST_TERM_CONN);
3295 cleanup_big(big);
3296 }
3297 } else {
3298 err = -EINVAL;
3299 }
3300
3301 if (err) {
3302 LOG_DBG("Could not terminate BIG %d", err);
3303 }
3304
3305 return err;
3306 }
3307
3308 #if defined(CONFIG_BT_ISO_SYNC_RECEIVER)
store_bis_sync_receiver_info(const struct bt_hci_evt_le_big_sync_established * evt,struct bt_iso_info * info)3309 static void store_bis_sync_receiver_info(const struct bt_hci_evt_le_big_sync_established *evt,
3310 struct bt_iso_info *info)
3311 {
3312 struct bt_iso_sync_receiver_info *receiver_info = &info->sync_receiver;
3313
3314 info->max_subevent = evt->nse;
3315 info->iso_interval = sys_le16_to_cpu(evt->iso_interval);
3316
3317 receiver_info->latency = sys_get_le24(evt->latency);
3318 receiver_info->bn = evt->bn;
3319 receiver_info->irc = evt->irc;
3320 /* Transform to n * 1.25ms */
3321 receiver_info->pto = info->iso_interval * evt->pto;
3322 receiver_info->max_pdu = sys_le16_to_cpu(evt->max_pdu);
3323
3324 info->can_send = false;
3325 info->can_recv = true;
3326 }
3327
hci_le_big_sync_established(struct net_buf * buf)3328 void hci_le_big_sync_established(struct net_buf *buf)
3329 {
3330 struct bt_hci_evt_le_big_sync_established *evt = (void *)buf->data;
3331 struct bt_iso_chan *bis;
3332 struct bt_iso_big *big;
3333 int i;
3334
3335 if (evt->big_handle >= ARRAY_SIZE(bigs)) {
3336 LOG_WRN("Invalid BIG handle");
3337 big = big_lookup_flag(BT_BIG_SYNCING);
3338 if (big) {
3339 big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
3340 cleanup_big(big);
3341 }
3342
3343 return;
3344 }
3345
3346 big = lookup_big_by_handle(evt->big_handle);
3347 atomic_clear_bit(big->flags, BT_BIG_SYNCING);
3348
3349 LOG_DBG("BIG[%u] %p sync established, status 0x%02x %s", big->handle, big, evt->status,
3350 bt_hci_err_to_str(evt->status));
3351
3352 if (evt->status || evt->num_bis != big->num_bis) {
3353 if (evt->status == BT_HCI_ERR_SUCCESS && evt->num_bis != big->num_bis) {
3354 LOG_ERR("Invalid number of BIS synced, was %u expected %u", evt->num_bis,
3355 big->num_bis);
3356 }
3357 big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
3358 cleanup_big(big);
3359 return;
3360 }
3361
3362 i = 0;
3363 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
3364 const uint16_t handle = evt->handle[i++];
3365 struct bt_conn *iso_conn = bis->iso;
3366
3367 iso_conn->handle = sys_le16_to_cpu(handle);
3368 store_bis_sync_receiver_info(evt, &iso_conn->iso.info);
3369 bt_conn_set_state(iso_conn, BT_CONN_CONNECTED);
3370 }
3371
3372 if (!sys_slist_is_empty(&iso_big_cbs)) {
3373 struct bt_iso_big_cb *listener;
3374
3375 SYS_SLIST_FOR_EACH_CONTAINER(&iso_big_cbs, listener, _node) {
3376 if (listener->started != NULL) {
3377 listener->started(big);
3378 }
3379 }
3380 }
3381 }
3382
hci_le_big_sync_lost(struct net_buf * buf)3383 void hci_le_big_sync_lost(struct net_buf *buf)
3384 {
3385 struct bt_hci_evt_le_big_sync_lost *evt = (void *)buf->data;
3386 struct bt_iso_big *big;
3387
3388 if (evt->big_handle >= ARRAY_SIZE(bigs)) {
3389 LOG_WRN("Invalid BIG handle");
3390 return;
3391 }
3392
3393 big = lookup_big_by_handle(evt->big_handle);
3394
3395 LOG_DBG("BIG[%u] %p sync lost", big->handle, big);
3396
3397 big_disconnect(big, evt->reason);
3398 cleanup_big(big);
3399 }
3400
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)3401 static int hci_le_big_create_sync(const struct bt_le_per_adv_sync *sync, struct bt_iso_big *big,
3402 const struct bt_iso_big_sync_param *param)
3403 {
3404 struct bt_hci_cp_le_big_create_sync *req;
3405 struct bt_hci_cmd_state_set state;
3406 struct net_buf *buf;
3407 int err;
3408 uint8_t bit_idx = 0;
3409
3410 buf = bt_hci_cmd_create(BT_HCI_OP_LE_BIG_CREATE_SYNC, sizeof(*req) + big->num_bis);
3411 if (!buf) {
3412 return -ENOBUFS;
3413 }
3414
3415 req = net_buf_add(buf, sizeof(*req) + big->num_bis);
3416 req->big_handle = big->handle;
3417 req->sync_handle = sys_cpu_to_le16(sync->handle);
3418 req->encryption = param->encryption;
3419 if (req->encryption) {
3420 memcpy(req->bcode, param->bcode, sizeof(req->bcode));
3421 } else {
3422 memset(req->bcode, 0, sizeof(req->bcode));
3423 }
3424 req->mse = param->mse;
3425 req->sync_timeout = sys_cpu_to_le16(param->sync_timeout);
3426 req->num_bis = big->num_bis;
3427 /* Transform from bitfield to array */
3428 for (int i = 1; i <= BT_ISO_MAX_GROUP_ISO_COUNT; i++) {
3429 if (param->bis_bitfield & BT_ISO_BIS_INDEX_BIT(i)) {
3430 if (bit_idx == big->num_bis) {
3431 LOG_DBG("BIG cannot contain %u BISes", bit_idx + 1);
3432 return -EINVAL;
3433 }
3434 req->bis[bit_idx++] = i;
3435 }
3436 }
3437
3438 if (bit_idx != big->num_bis) {
3439 LOG_DBG("Number of bits in bis_bitfield (%u) doesn't match num_bis (%u)", bit_idx,
3440 big->num_bis);
3441 return -EINVAL;
3442 }
3443
3444 bt_hci_cmd_state_set_init(buf, &state, big->flags, BT_BIG_SYNCING, true);
3445 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_BIG_CREATE_SYNC, buf, NULL);
3446
3447 return err;
3448 }
3449
bt_iso_big_sync(struct bt_le_per_adv_sync * sync,struct bt_iso_big_sync_param * param,struct bt_iso_big ** out_big)3450 int bt_iso_big_sync(struct bt_le_per_adv_sync *sync, struct bt_iso_big_sync_param *param,
3451 struct bt_iso_big **out_big)
3452 {
3453 int err;
3454 struct bt_iso_chan *bis;
3455 struct bt_iso_big *big;
3456
3457 CHECKIF(sync == NULL) {
3458 LOG_DBG("sync is NULL");
3459 return -EINVAL;
3460 }
3461
3462 CHECKIF(param == NULL) {
3463 LOG_DBG("param is NULL");
3464 return -EINVAL;
3465 }
3466
3467 CHECKIF(out_big == NULL) {
3468 LOG_DBG("out_big is NULL");
3469 return -EINVAL;
3470 }
3471
3472 if (!atomic_test_bit(sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
3473 LOG_DBG("PA sync not synced");
3474 return -EINVAL;
3475 }
3476
3477 CHECKIF(param->mse > BT_ISO_SYNC_MSE_MAX) {
3478 LOG_DBG("Invalid MSE 0x%02x", param->mse);
3479 return -EINVAL;
3480 }
3481
3482 CHECKIF(param->sync_timeout < BT_ISO_SYNC_TIMEOUT_MIN ||
3483 param->sync_timeout > BT_ISO_SYNC_TIMEOUT_MAX) {
3484 LOG_DBG("Invalid sync timeout 0x%04x", param->sync_timeout);
3485 return -EINVAL;
3486 }
3487
3488 CHECKIF(!BT_ISO_VALID_BIS_BITFIELD(param->bis_bitfield)) {
3489 LOG_DBG("Invalid BIS bitfield 0x%08x", param->bis_bitfield);
3490 return -EINVAL;
3491 }
3492
3493 CHECKIF(!param->bis_channels) {
3494 LOG_DBG("NULL BIS channels");
3495 return -EINVAL;
3496 }
3497
3498 CHECKIF(!param->num_bis) {
3499 LOG_DBG("Invalid number of BIS %u", param->num_bis);
3500 return -EINVAL;
3501 }
3502
3503 for (uint8_t i = 0; i < param->num_bis; i++) {
3504 struct bt_iso_chan *param_bis = param->bis_channels[i];
3505
3506 CHECKIF(param_bis == NULL) {
3507 LOG_DBG("bis_channels[%u]: NULL channel", i);
3508 return -EINVAL;
3509 }
3510
3511 if (param_bis->iso) {
3512 LOG_DBG("bis_channels[%u]: already allocated", i);
3513 return -EALREADY;
3514 }
3515
3516 CHECKIF(param_bis->qos == NULL) {
3517 LOG_DBG("bis_channels[%u]: qos is NULL", i);
3518 return -EINVAL;
3519 }
3520
3521 CHECKIF(param_bis->qos->rx == NULL) {
3522 LOG_DBG("bis_channels[%u]: qos->rx is NULL", i);
3523 return -EINVAL;
3524 }
3525 }
3526
3527 big = get_free_big();
3528
3529 if (!big) {
3530 return -ENOMEM;
3531 }
3532
3533 err = big_init_bis(big, param->bis_channels, param->num_bis, false);
3534 if (err) {
3535 LOG_DBG("Could not init BIG %d", err);
3536 cleanup_big(big);
3537 return err;
3538 }
3539 big->num_bis = param->num_bis;
3540
3541 err = hci_le_big_create_sync(sync, big, param);
3542 if (err) {
3543 LOG_DBG("Could not create BIG sync %d", err);
3544 cleanup_big(big);
3545 return err;
3546 }
3547
3548 SYS_SLIST_FOR_EACH_CONTAINER(&big->bis_channels, bis, node) {
3549 bt_iso_chan_set_state(bis, BT_ISO_STATE_CONNECTING);
3550 }
3551
3552 *out_big = big;
3553
3554 return 0;
3555 }
3556 #endif /* CONFIG_BT_ISO_SYNC_RECEIVER */
3557 #endif /* CONFIG_BT_ISO_BROADCAST */
3558
bt_iso_reset(void)3559 void bt_iso_reset(void)
3560 {
3561 #if defined(CONFIG_BT_ISO_CENTRAL)
3562 for (size_t i = 0U; i < ARRAY_SIZE(cigs); i++) {
3563 struct bt_iso_cig *cig = &cigs[i];
3564 struct bt_iso_chan *cis;
3565
3566 /* Disconnect any connected CIS and call the callback
3567 * We cannot use bt_iso_chan_disconnected directly here, as that
3568 * attempts to also remove the ISO data path, which we shouldn't attempt to
3569 * during the reset, as that sends HCI commands.
3570 */
3571 SYS_SLIST_FOR_EACH_CONTAINER(&cig->cis_channels, cis, node) {
3572 if (cis->state != BT_ISO_STATE_DISCONNECTED) {
3573 bt_iso_chan_set_state(cis, BT_ISO_STATE_DISCONNECTED);
3574 bt_iso_cleanup_acl(cis->iso);
3575 if (cis->ops != NULL && cis->ops->disconnected != NULL) {
3576 cis->ops->disconnected(cis, BT_HCI_ERR_UNSPECIFIED);
3577 }
3578 }
3579 }
3580
3581 cleanup_cig(cig);
3582 }
3583 #endif /* CONFIG_BT_ISO_CENTRAL */
3584
3585 #if defined(CONFIG_BT_ISO_BROADCAST)
3586 for (size_t i = 0U; i < ARRAY_SIZE(bigs); i++) {
3587 struct bt_iso_big *big = &bigs[i];
3588
3589 big_disconnect(big, BT_HCI_ERR_UNSPECIFIED);
3590 cleanup_big(big);
3591 }
3592 #endif /* CONFIG_BT_ISO_BROADCAST */
3593 }
3594