1 /* @file
2  * @brief Internal APIs for ASCS handling
3 
4  * Copyright (c) 2020 Intel Corporation
5  * Copyright (c) 2022-2023 Nordic Semiconductor ASA
6  * Copyright (c) 2024 Demant A/S
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #ifndef BT_ASCS_INTERNAL_H
12 #define BT_ASCS_INTERNAL_H
13 
14 #include <zephyr/bluetooth/audio/audio.h>
15 #include <zephyr/bluetooth/audio/bap.h>
16 #include <zephyr/bluetooth/conn.h>
17 
18 #define BT_ASCS_ASE_ID_NONE              0x00
19 
20 /* The number of ASEs in the notification when the opcode is unsupported or the length of the
21  * control point write request is incorrect
22  */
23 #define BT_ASCS_UNSUPP_OR_LENGTH_ERR_NUM_ASE 0xFFU
24 
25 /* Transport QoS Packing */
26 #define BT_ASCS_QOS_PACKING_SEQ          0x00
27 #define BT_ASCS_QOS_PACKING_INT          0x01
28 
29 /* Transport QoS Framing */
30 #define BT_ASCS_QOS_FRAMING_UNFRAMED     0x00
31 #define BT_ASCS_QOS_FRAMING_FRAMED       0x01
32 
33 /* Format of the ASE characteristic, defined in Table 4.2 */
34 struct bt_ascs_ase_status {
35 	uint8_t  id;
36 	uint8_t  state;
37 	uint8_t  params[0];
38 } __packed;
39 
40 struct bt_ascs_codec_config {
41 	uint8_t len;
42 	uint8_t type;
43 	uint8_t data[0];
44 } __packed;
45 
46 struct bt_ascs_codec {
47 	uint8_t  id;
48 	uint16_t cid;
49 	uint16_t vid;
50 } __packed;
51 
52 #define BT_ASCS_PD_NO_PREF 0x00000000
53 
54 /* ASE_State = 0x01 (Codec Configured), defined in Table 4.3. */
55 struct bt_ascs_ase_status_config {
56 	uint8_t  framing;
57 	uint8_t  phy;
58 	uint8_t  rtn;
59 	uint16_t latency;
60 	uint8_t  pd_min[3];
61 	uint8_t  pd_max[3];
62 	uint8_t  prefer_pd_min[3];
63 	uint8_t  prefer_pd_max[3];
64 	struct bt_ascs_codec codec;
65 	uint8_t  cc_len;
66 	/* LTV-formatted Codec-Specific Configuration */
67 	struct bt_ascs_codec_config cc[0];
68 } __packed;
69 
70 /* ASE_State = 0x02 (QoS Configured), defined in Table 4.4. */
71 struct bt_ascs_ase_status_qos {
72 	uint8_t  cig_id;
73 	uint8_t  cis_id;
74 	uint8_t  interval[3];
75 	uint8_t  framing;
76 	uint8_t  phy;
77 	uint16_t sdu;
78 	uint8_t  rtn;
79 	uint16_t latency;
80 	uint8_t  pd[3];
81 } __packed;
82 
83 /* ASE_Status = 0x03 (Enabling) defined in Table 4.5.
84  */
85 struct bt_ascs_ase_status_enable {
86 	uint8_t  cig_id;
87 	uint8_t  cis_id;
88 	uint8_t  metadata_len;
89 	uint8_t  metadata[0];
90 } __packed;
91 
92 /* ASE_Status =  0x04 (Streaming) defined in Table 4.5.
93  */
94 struct bt_ascs_ase_status_stream {
95 	uint8_t  cig_id;
96 	uint8_t  cis_id;
97 	uint8_t  metadata_len;
98 	uint8_t  metadata[0];
99 } __packed;
100 
101 /* ASE_Status = 0x05 (Disabling) as defined in Table 4.5.
102  */
103 struct bt_ascs_ase_status_disable {
104 	uint8_t  cig_id;
105 	uint8_t  cis_id;
106 	uint8_t  metadata_len;
107 	uint8_t  metadata[0];
108 } __packed;
109 
110 /* ASE Control Point Protocol */
111 struct bt_ascs_ase_cp {
112 	/* Request/Notification opcode */
113 	uint8_t  op;
114 	uint8_t  pdu[0];
115 } __packed;
116 
117 /* Opcodes */
118 #define BT_ASCS_CONFIG_OP                0x01
119 
120 #define BT_ASCS_CONFIG_LATENCY_LOW       0x01
121 #define BT_ASCS_CONFIG_LATENCY_MEDIUM    0x02
122 #define BT_ASCS_CONFIG_LATENCY_HIGH      0x03
123 
124 #define BT_ASCS_CONFIG_PHY_LE_1M         0x01
125 #define BT_ASCS_CONFIG_PHY_LE_2M         0x02
126 #define BT_ASCS_CONFIG_PHY_LE_CODED      0x03
127 
128 struct bt_ascs_config {
129 	/* ASE ID */
130 	uint8_t  ase;
131 	/* Target latency */
132 	uint8_t  latency;
133 	/* Target PHY */
134 	uint8_t  phy;
135 	/* Codec ID */
136 	struct bt_ascs_codec codec;
137 	/* Codec Specific Config Length */
138 	uint8_t  cc_len;
139 	/* LTV-formatted Codec-Specific Configuration */
140 	struct bt_ascs_codec_config cc[0];
141 } __packed;
142 
143 struct bt_ascs_config_op {
144 	/* Number of ASEs */
145 	uint8_t  num_ases;
146 	/* Config Parameters */
147 	struct bt_ascs_config cfg[0];
148 } __packed;
149 
150 #define BT_ASCS_QOS_OP                   0x02
151 struct bt_ascs_qos {
152 	/* ASE ID */
153 	uint8_t  ase;
154 	/* CIG ID*/
155 	uint8_t  cig;
156 	/* CIG ID*/
157 	uint8_t  cis;
158 	/* Frame interval */
159 	uint8_t  interval[3];
160 	/* Frame framing */
161 	uint8_t  framing;
162 	/* PHY */
163 	uint8_t  phy;
164 	/* Maximum SDU Size */
165 	uint16_t sdu;
166 	/* Retransmission Effort */
167 	uint8_t  rtn;
168 	/* Transport Latency */
169 	uint16_t latency;
170 	/* Presentation Delay */
171 	uint8_t  pd[3];
172 } __packed;
173 
174 struct bt_ascs_qos_op {
175 	/* Number of ASEs */
176 	uint8_t  num_ases;
177 	/* QoS Parameters */
178 	struct bt_ascs_qos qos[0];
179 } __packed;
180 
181 #define BT_ASCS_ENABLE_OP                0x03
182 struct bt_ascs_metadata {
183 	/* ASE ID */
184 	uint8_t  ase;
185 	/* Metadata length */
186 	uint8_t  len;
187 	/* LTV-formatted Metadata */
188 	uint8_t  data[0];
189 } __packed;
190 
191 struct bt_ascs_enable_op {
192 	/* Number of ASEs */
193 	uint8_t  num_ases;
194 	/* Metadata */
195 	struct bt_ascs_metadata metadata[0];
196 } __packed;
197 
198 #define BT_ASCS_START_OP                 0x04
199 struct bt_ascs_start_op {
200 	/* Number of ASEs */
201 	uint8_t  num_ases;
202 	/* ASE IDs */
203 	uint8_t  ase[0];
204 } __packed;
205 
206 #define BT_ASCS_DISABLE_OP               0x05
207 struct bt_ascs_disable_op {
208 	/* Number of ASEs */
209 	uint8_t  num_ases;
210 	/* ASE IDs */
211 	uint8_t  ase[0];
212 } __packed;
213 
214 #define BT_ASCS_STOP_OP                  0x06
215 struct bt_ascs_stop_op {
216 	/* Number of ASEs */
217 	uint8_t  num_ases;
218 	/* ASE IDs */
219 	uint8_t  ase[0];
220 } __packed;
221 
222 #define BT_ASCS_METADATA_OP              0x07
223 struct bt_ascs_metadata_op {
224 	/* Number of ASEs */
225 	uint8_t  num_ases;
226 	/* Metadata */
227 	struct bt_ascs_metadata metadata[0];
228 } __packed;
229 
230 #define BT_ASCS_RELEASE_OP              0x08
231 struct bt_ascs_release_op {
232 	/* Number of ASEs */
233 	uint8_t  num_ases;
234 	/* Ase IDs */
235 	uint8_t  ase[0];
236 } __packed;
237 
238 struct bt_ascs_cp_ase_rsp {
239 	/* ASE ID */
240 	uint8_t  id;
241 	/* Response code */
242 	uint8_t  code;
243 	/* Response reason */
244 	uint8_t  reason;
245 } __packed;
246 
247 struct bt_ascs_cp_rsp {
248 	/* Opcode */
249 	uint8_t  op;
250 	/* Number of ASEs */
251 	uint8_t  num_ase;
252 	/* ASE response */
253 	struct bt_ascs_cp_ase_rsp ase_rsp[0];
254 } __packed;
255 
bt_ascs_op_str(uint8_t op)256 static inline const char *bt_ascs_op_str(uint8_t op)
257 {
258 	switch (op) {
259 	case BT_ASCS_CONFIG_OP:
260 		return "Config Codec";
261 	case BT_ASCS_QOS_OP:
262 		return "Config QoS";
263 	case BT_ASCS_ENABLE_OP:
264 		return "Enable";
265 	case BT_ASCS_START_OP:
266 		return "Receiver Start Ready";
267 	case BT_ASCS_DISABLE_OP:
268 		return "Disable";
269 	case BT_ASCS_STOP_OP:
270 		return "Receiver Stop Ready";
271 	case BT_ASCS_METADATA_OP:
272 		return "Update Metadata";
273 	case BT_ASCS_RELEASE_OP:
274 		return "Release";
275 	}
276 
277 	return "Unknown";
278 }
279 
bt_ascs_rsp_str(uint8_t code)280 static inline const char *bt_ascs_rsp_str(uint8_t code)
281 {
282 	switch (code) {
283 	case BT_BAP_ASCS_RSP_CODE_SUCCESS:
284 		return "Success";
285 	case BT_BAP_ASCS_RSP_CODE_NOT_SUPPORTED:
286 		return "Unsupported Opcode";
287 	case BT_BAP_ASCS_RSP_CODE_INVALID_LENGTH:
288 		return "Invalid Length";
289 	case BT_BAP_ASCS_RSP_CODE_INVALID_ASE:
290 		return "Invalid ASE_ID";
291 	case BT_BAP_ASCS_RSP_CODE_INVALID_ASE_STATE:
292 		return "Invalid ASE State";
293 	case BT_BAP_ASCS_RSP_CODE_INVALID_DIR:
294 		return "Invalid ASE Direction";
295 	case BT_BAP_ASCS_RSP_CODE_CAP_UNSUPPORTED:
296 		return "Unsupported Capabilities";
297 	case BT_BAP_ASCS_RSP_CODE_CONF_UNSUPPORTED:
298 		return "Unsupported Configuration Value";
299 	case BT_BAP_ASCS_RSP_CODE_CONF_REJECTED:
300 		return "Rejected Configuration Value";
301 	case BT_BAP_ASCS_RSP_CODE_CONF_INVALID:
302 		return "Invalid Configuration Value";
303 	case BT_BAP_ASCS_RSP_CODE_METADATA_UNSUPPORTED:
304 		return "Unsupported Metadata";
305 	case BT_BAP_ASCS_RSP_CODE_METADATA_REJECTED:
306 		return "Rejected Metadata";
307 	case BT_BAP_ASCS_RSP_CODE_METADATA_INVALID:
308 		return "Invalid Metadata";
309 	case BT_BAP_ASCS_RSP_CODE_NO_MEM:
310 		return "Insufficient Resources";
311 	case BT_BAP_ASCS_RSP_CODE_UNSPECIFIED:
312 		return "Unspecified Error";
313 	}
314 
315 	return "Unknown";
316 }
317 
bt_ascs_reason_str(uint8_t reason)318 static inline const char *bt_ascs_reason_str(uint8_t reason)
319 {
320 	switch (reason) {
321 	case BT_BAP_ASCS_REASON_NONE:
322 		return "None";
323 	case BT_BAP_ASCS_REASON_CODEC:
324 		return "Codec ID";
325 	case BT_BAP_ASCS_REASON_CODEC_DATA:
326 		return "Codec Specific Configuration";
327 	case BT_BAP_ASCS_REASON_INTERVAL:
328 		return "SDU Interval";
329 	case BT_BAP_ASCS_REASON_FRAMING:
330 		return "Framing";
331 	case BT_BAP_ASCS_REASON_PHY:
332 		return "PHY";
333 	case BT_BAP_ASCS_REASON_SDU:
334 		return "Maximum SDU Size";
335 	case BT_BAP_ASCS_REASON_RTN:
336 		return "Retransmission Number";
337 	case BT_BAP_ASCS_REASON_LATENCY:
338 		return "Maximum Transport Delay";
339 	case BT_BAP_ASCS_REASON_PD:
340 		return "Presentation Delay";
341 	case BT_BAP_ASCS_REASON_CIS:
342 		return "Invalid ASE CIS Mapping";
343 	}
344 
345 	return "Unknown";
346 }
347 
348 int bt_ascs_init(const struct bt_bap_unicast_server_cb *cb);
349 void bt_ascs_cleanup(void);
350 
351 int ascs_ep_set_state(struct bt_bap_ep *ep, uint8_t state);
352 
353 int bt_ascs_config_ase(struct bt_conn *conn, struct bt_bap_stream *stream,
354 		       struct bt_audio_codec_cfg *codec_cfg,
355 		       const struct bt_bap_qos_cfg_pref *qos_pref);
356 int bt_ascs_disable_ase(struct bt_bap_ep *ep);
357 int bt_ascs_release_ase(struct bt_bap_ep *ep);
358 
359 void bt_ascs_foreach_ep(struct bt_conn *conn, bt_bap_ep_func_t func, void *user_data);
360 
361 int bt_ascs_register(uint8_t snk_cnt, uint8_t src_cnt);
362 int bt_ascs_unregister(void);
363 
364 #endif /* BT_ASCS_INTERNAL_H */
365