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