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