1 /** @file
2  *  @brief Internal APIs for Bluetooth ISO handling.
3  */
4 
5 /*
6  * Copyright (c) 2020 Intel Corporation
7  * Copyright (c) 2021-2022 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #include <zephyr/bluetooth/iso.h>
13 
14 struct iso_data {
15 	/* Extend the bt_buf user data */
16 	struct bt_buf_data buf_data;
17 
18 	/* Index into the bt_conn storage array */
19 	uint8_t  index;
20 
21 	/** ISO connection handle */
22 	uint16_t handle;
23 };
24 
25 enum bt_iso_cig_state {
26 	BT_ISO_CIG_STATE_IDLE,
27 	BT_ISO_CIG_STATE_CONFIGURED,
28 	BT_ISO_CIG_STATE_ACTIVE,
29 	BT_ISO_CIG_STATE_INACTIVE
30 };
31 
32 struct bt_iso_cig {
33 	/** List of ISO channels to setup as CIS (the CIG). */
34 	sys_slist_t cis_channels;
35 
36 	/** Total number of CISes in the CIG. */
37 	uint8_t  num_cis;
38 
39 	/** The CIG ID */
40 	uint8_t id;
41 
42 	/** The CIG state
43 	 *
44 	 * Refer to BT Core Spec 5.3, Vol 6, Part 6, Figure 4.63
45 	 */
46 	enum bt_iso_cig_state state;
47 };
48 
49 enum {
50 	BT_BIG_INITIALIZED,
51 
52 	/* Creating a BIG as a broadcaster */
53 	BT_BIG_PENDING,
54 	/* Creating a BIG as a receiver */
55 	BT_BIG_SYNCING,
56 
57 	BT_BIG_NUM_FLAGS,
58 };
59 
60 struct bt_iso_big {
61 	/** List of ISO channels to setup as BIS (the BIG). */
62 	sys_slist_t bis_channels;
63 
64 	/** Total number of BISes in the BIG. */
65 	uint8_t  num_bis;
66 
67 	/** The BIG handle */
68 	uint8_t handle;
69 
70 	ATOMIC_DEFINE(flags, BT_BIG_NUM_FLAGS);
71 };
72 
73 #define iso(buf) ((struct iso_data *)net_buf_user_data(buf))
74 
75 /* Process ISO buffer */
76 void hci_iso(struct net_buf *buf);
77 
78 /* Allocates RX buffer */
79 struct net_buf *bt_iso_get_rx(k_timeout_t timeout);
80 
81 /* Process CIS Established event */
82 void hci_le_cis_established(struct net_buf *buf);
83 
84 /* Process CIS Request event */
85 void hci_le_cis_req(struct net_buf *buf);
86 
87 /** Process BIG complete event */
88 void hci_le_big_complete(struct net_buf *buf);
89 
90 /** Process BIG terminate event */
91 void hci_le_big_terminate(struct net_buf *buf);
92 
93 /** Process BIG sync established event */
94 void hci_le_big_sync_established(struct net_buf *buf);
95 
96 /** Process BIG sync lost event */
97 void hci_le_big_sync_lost(struct net_buf *buf);
98 
99 /* Notify ISO channels of a new connection */
100 void bt_iso_connected(struct bt_conn *iso);
101 
102 /* Notify ISO channels of a disconnect event */
103 void bt_iso_disconnected(struct bt_conn *iso);
104 
105 /* Notify ISO connected channels of security changed */
106 void bt_iso_security_changed(struct bt_conn *acl, uint8_t hci_status);
107 
108 /* Allocate ISO PDU */
109 #if defined(CONFIG_NET_BUF_LOG)
110 struct net_buf *bt_iso_create_pdu_timeout_debug(struct net_buf_pool *pool,
111 						size_t reserve,
112 						k_timeout_t timeout,
113 						const char *func, int line);
114 #define bt_iso_create_pdu_timeout(_pool, _reserve, _timeout) \
115 	bt_iso_create_pdu_timeout_debug(_pool, _reserve, _timeout, \
116 					__func__, __LINE__)
117 
118 #define bt_iso_create_pdu(_pool, _reserve) \
119 	bt_iso_create_pdu_timeout_debug(_pool, _reserve, K_FOREVER, \
120 					__func__, __LINE__)
121 #else
122 struct net_buf *bt_iso_create_pdu_timeout(struct net_buf_pool *pool,
123 					  size_t reserve, k_timeout_t timeout);
124 
125 #define bt_iso_create_pdu(_pool, _reserve) \
126 	bt_iso_create_pdu_timeout(_pool, _reserve, K_FOREVER)
127 #endif
128 
129 /* Allocate ISO Fragment */
130 #if defined(CONFIG_NET_BUF_LOG)
131 struct net_buf *bt_iso_create_frag_timeout_debug(size_t reserve,
132 						 k_timeout_t timeout,
133 						 const char *func, int line);
134 
135 #define bt_iso_create_frag_timeout(_reserve, _timeout) \
136 	bt_iso_create_frag_timeout_debug(_reserve, _timeout, \
137 					 __func__, __LINE__)
138 
139 #define bt_iso_create_frag(_reserve) \
140 	bt_iso_create_frag_timeout_debug(_reserve, K_FOREVER, \
141 					 __func__, __LINE__)
142 #else
143 struct net_buf *bt_iso_create_frag_timeout(size_t reserve, k_timeout_t timeout);
144 
145 #define bt_iso_create_frag(_reserve) \
146 	bt_iso_create_frag_timeout(_reserve, K_FOREVER)
147 #endif
148 
149 #if defined(CONFIG_BT_ISO_LOG_LEVEL_DBG)
150 void bt_iso_chan_set_state_debug(struct bt_iso_chan *chan,
151 				 enum bt_iso_state state,
152 				 const char *func, int line);
153 #define bt_iso_chan_set_state(_chan, _state) \
154 	bt_iso_chan_set_state_debug(_chan, _state, __func__, __LINE__)
155 #else
156 void bt_iso_chan_set_state(struct bt_iso_chan *chan, enum bt_iso_state state);
157 #endif /* CONFIG_BT_ISO_LOG_LEVEL_DBG */
158 
159 /* Process incoming data for a connection */
160 void bt_iso_recv(struct bt_conn *iso, struct net_buf *buf, uint8_t flags);
161 
162 /* Whether the HCI ISO data packet contains a timestamp or not.
163  * Per spec, the TS flag can only be set for the first fragment.
164  */
165 enum bt_iso_timestamp {
166 	BT_ISO_TS_ABSENT = 0,
167 	BT_ISO_TS_PRESENT,
168 };
169