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