1 /** @file
2  *  @brief Internal APIs for Bluetooth ISO handling.
3  */
4 
5 /*
6  * Copyright (c) 2020 Intel Corporation
7  * Copyright (c) 2021-2024 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #include <stdint.h>
13 
14 #include <zephyr/bluetooth/conn.h>
15 #include <zephyr/bluetooth/buf.h>
16 #include <zephyr/bluetooth/iso.h>
17 #include <zephyr/kernel.h>
18 #include <zephyr/net_buf.h>
19 #include <zephyr/sys/atomic.h>
20 #include <zephyr/sys/slist.h>
21 #include <zephyr/sys_clock.h>
22 
23 enum bt_iso_cig_state {
24 	BT_ISO_CIG_STATE_IDLE,
25 	BT_ISO_CIG_STATE_CONFIGURED,
26 	BT_ISO_CIG_STATE_ACTIVE,
27 	BT_ISO_CIG_STATE_INACTIVE
28 };
29 
30 struct bt_iso_cig {
31 	/** List of ISO channels to setup as CIS (the CIG). */
32 	sys_slist_t cis_channels;
33 
34 	/** Total number of CISes in the CIG. */
35 	uint8_t  num_cis;
36 
37 	/** The CIG ID */
38 	uint8_t id;
39 
40 	/** The CIG state
41 	 *
42 	 * Refer to BT Core Spec 5.3, Vol 6, Part 6, Figure 4.63
43 	 */
44 	enum bt_iso_cig_state state;
45 };
46 
47 enum {
48 	BT_BIG_INITIALIZED,
49 
50 	/* Creating a BIG as a broadcaster */
51 	BT_BIG_PENDING,
52 	/* Creating a BIG as a receiver */
53 	BT_BIG_SYNCING,
54 
55 	BT_BIG_NUM_FLAGS,
56 };
57 
58 struct bt_iso_big {
59 	/** List of ISO channels to setup as BIS (the BIG). */
60 	sys_slist_t bis_channels;
61 
62 	/** Total number of BISes in the BIG. */
63 	uint8_t  num_bis;
64 
65 	/** The BIG handle */
66 	uint8_t handle;
67 
68 	ATOMIC_DEFINE(flags, BT_BIG_NUM_FLAGS);
69 };
70 
71 #define iso(buf) ((struct bt_conn_rx *)net_buf_user_data(buf))
72 
73 /* Process ISO buffer */
74 void hci_iso(struct net_buf *buf);
75 
76 /* Allocates RX buffer */
77 struct net_buf *bt_iso_get_rx(k_timeout_t timeout);
78 
79 /** A callback used to notify about freed buffer in the iso rx pool. */
80 typedef void (*bt_iso_buf_rx_freed_cb_t)(void);
81 
82 /** Set a callback to notify about freed buffer in the iso rx pool.
83  *
84  * @param cb Callback to notify about freed buffer in the iso rx pool. If NULL, the callback is
85  *           disabled.
86  */
87 void bt_iso_buf_rx_freed_cb_set(bt_iso_buf_rx_freed_cb_t cb);
88 
89 /* Process CIS Established event */
90 void hci_le_cis_established(struct net_buf *buf);
91 void hci_le_cis_established_v2(struct net_buf *buf);
92 
93 /* Process CIS Request event */
94 void hci_le_cis_req(struct net_buf *buf);
95 
96 /** Process BIG complete event */
97 void hci_le_big_complete(struct net_buf *buf);
98 
99 /** Process BIG terminate event */
100 void hci_le_big_terminate(struct net_buf *buf);
101 
102 /** Process BIG sync established event */
103 void hci_le_big_sync_established(struct net_buf *buf);
104 
105 /** Process BIG sync lost event */
106 void hci_le_big_sync_lost(struct net_buf *buf);
107 
108 /* Notify ISO channels of a new connection */
109 void bt_iso_connected(struct bt_conn *iso);
110 
111 /* Notify ISO channels of a disconnect event */
112 void bt_iso_disconnected(struct bt_conn *iso);
113 
114 /* Notify ISO connected channels of security changed */
115 void bt_iso_security_changed(struct bt_conn *acl, uint8_t hci_status);
116 
117 #if defined(CONFIG_BT_ISO_LOG_LEVEL_DBG)
118 void bt_iso_chan_set_state_debug(struct bt_iso_chan *chan,
119 				 enum bt_iso_state state,
120 				 const char *func, int line);
121 #define bt_iso_chan_set_state(_chan, _state) \
122 	bt_iso_chan_set_state_debug(_chan, _state, __func__, __LINE__)
123 #else
124 void bt_iso_chan_set_state(struct bt_iso_chan *chan, enum bt_iso_state state);
125 #endif /* CONFIG_BT_ISO_LOG_LEVEL_DBG */
126 
127 /* Process incoming data for a connection */
128 void bt_iso_recv(struct bt_conn *iso, struct net_buf *buf, uint8_t flags);
129 
130 /* Whether the HCI ISO data packet contains a timestamp or not.
131  * Per spec, the TS flag can only be set for the first fragment.
132  */
133 enum bt_iso_timestamp {
134 	BT_ISO_TS_ABSENT = 0,
135 	BT_ISO_TS_PRESENT,
136 };
137