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