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