1 /** @file
2  * @brief Advanced Audio Distribution Profile header.
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  * Copyright 2024 NXP
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_A2DP_H_
12 #define ZEPHYR_INCLUDE_BLUETOOTH_A2DP_H_
13 
14 #include <stdint.h>
15 
16 #include <zephyr/bluetooth/bluetooth.h>
17 #include <zephyr/bluetooth/l2cap.h>
18 #include <zephyr/bluetooth/classic/avdtp.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define BT_A2DP_STREAM_BUF_RESERVE (12u + BT_L2CAP_BUF_SIZE(0))
25 
26 /** SBC IE length */
27 #define BT_A2DP_SBC_IE_LENGTH      (4u)
28 /** MPEG1,2 IE length */
29 #define BT_A2DP_MPEG_1_2_IE_LENGTH (4u)
30 /** MPEG2,4 IE length */
31 #define BT_A2DP_MPEG_2_4_IE_LENGTH (6u)
32 /** The max IE (Codec Info Element) length */
33 #define A2DP_MAX_IE_LENGTH (8U)
34 
35 /** @brief define the audio endpoint
36  *  @param _role BT_AVDTP_SOURCE or BT_AVDTP_SINK.
37  *  @param _codec value of enum bt_a2dp_codec_id.
38  *  @param _capability the codec capability.
39  */
40 #define BT_A2DP_EP_INIT(_role, _codec, _capability)\
41 {\
42 	.codec_type = _codec,\
43 	.sep = {.sep_info = {.media_type = BT_AVDTP_AUDIO, .tsep = _role}},\
44 	.codec_cap = _capability,\
45 	.stream = NULL,\
46 }
47 
48 /** @brief define the audio sink endpoint
49  *  @param _codec value of enum bt_a2dp_codec_id.
50  *  @param _capability the codec capability.
51  */
52 #define BT_A2DP_SINK_EP_INIT(_codec, _capability)\
53 BT_A2DP_EP_INIT(BT_AVDTP_SINK, _codec, _capability)
54 
55 /** @brief define the audio source endpoint
56  *  @param _codec value of enum bt_a2dp_codec_id.
57  *  @param _capability the codec capability.
58  */
59 #define BT_A2DP_SOURCE_EP_INIT(_codec, _capability)\
60 BT_A2DP_EP_INIT(BT_AVDTP_SOURCE, _codec, _capability)
61 
62 /** @brief define the SBC sink endpoint that can be used as
63  * bt_a2dp_register_endpoint's parameter.
64  *
65  * SBC is mandatory as a2dp specification, BT_A2DP_SBC_SINK_EP_DEFAULT
66  * is more convenient for user to register SBC endpoint.
67  *
68  *  @param _name unique structure name postfix.
69  *  @param _freq sbc codec frequency.
70  *               for example: A2DP_SBC_SAMP_FREQ_44100 | A2DP_SBC_SAMP_FREQ_48000
71  *  @param _ch_mode sbc codec channel mode.
72  *               for example: A2DP_SBC_CH_MODE_MONO | A2DP_SBC_CH_MODE_STREO
73  *  @param _blk_len sbc codec block length.
74  *               for example: A2DP_SBC_BLK_LEN_16
75  *  @param _subband sbc codec subband.
76  *               for example: A2DP_SBC_SUBBAND_8
77  *  @param _alloc_mthd sbc codec allocate method.
78  *               for example: A2DP_SBC_ALLOC_MTHD_LOUDNESS
79  *  @param _min_bitpool sbc codec min bit pool. for example: 18
80  *  @param _max_bitpool sbc codec max bit pool. for example: 35
81  *  @
82  */
83 #define BT_A2DP_SBC_SINK_EP(_name, _freq, _ch_mode, _blk_len, _subband,\
84 _alloc_mthd, _min_bitpool, _max_bitpool)\
85 static struct bt_a2dp_codec_ie bt_a2dp_ep_cap_ie##_name =\
86 {.len = BT_A2DP_SBC_IE_LENGTH, .codec_ie = {_freq | _ch_mode,\
87 _blk_len | _subband | _alloc_mthd, _min_bitpool, _max_bitpool}};\
88 static struct bt_a2dp_ep _name = BT_A2DP_SINK_EP_INIT(BT_A2DP_SBC,\
89 (&bt_a2dp_ep_cap_ie##_name))
90 
91 /** @brief define the SBC source endpoint that can be used as bt_a2dp_register_endpoint's
92  * parameter.
93  *
94  * SBC is mandatory as a2dp specification, BT_A2DP_SBC_SOURCE_EP_DEFAULT
95  * is more convenient for user to register SBC endpoint.
96  *
97  *  @param _name the endpoint variable name.
98  *  @param _freq sbc codec frequency.
99  *               for example: A2DP_SBC_SAMP_FREQ_44100 | A2DP_SBC_SAMP_FREQ_48000
100  *  @param _ch_mode sbc codec channel mode.
101  *               for example: A2DP_SBC_CH_MODE_MONO | A2DP_SBC_CH_MODE_STREO
102  *  @param _blk_len sbc codec block length.
103  *               for example: A2DP_SBC_BLK_LEN_16
104  *  @param _subband sbc codec subband.
105  *               for example: A2DP_SBC_SUBBAND_8
106  *  @param _alloc_mthd sbc codec allocate method.
107  *               for example: A2DP_SBC_ALLOC_MTHD_LOUDNESS
108  *  @param _min_bitpool sbc codec min bit pool. for example: 18
109  *  @param _max_bitpool sbc codec max bit pool. for example: 35
110  */
111 #define BT_A2DP_SBC_SOURCE_EP(_name, _freq, _ch_mode, _blk_len, _subband,\
112 _alloc_mthd, _min_bitpool, _max_bitpool)\
113 static struct bt_a2dp_codec_ie bt_a2dp_ep_cap_ie##_name =\
114 {.len = BT_A2DP_SBC_IE_LENGTH, .codec_ie = {_freq | _ch_mode,\
115 _blk_len | _subband | _alloc_mthd, _min_bitpool, _max_bitpool}};\
116 static struct bt_a2dp_ep _name = BT_A2DP_SOURCE_EP_INIT(BT_A2DP_SBC,\
117 &bt_a2dp_ep_cap_ie##_name)
118 
119 /** @brief define the default SBC sink endpoint that can be used as
120  * bt_a2dp_register_endpoint's parameter.
121  *
122  * SBC is mandatory as a2dp specification, BT_A2DP_SBC_SINK_EP_DEFAULT
123  * is more convenient for user to register SBC endpoint.
124  *
125  *  @param _name the endpoint variable name.
126  */
127 #define BT_A2DP_SBC_SINK_EP_DEFAULT(_name)\
128 static struct bt_a2dp_codec_ie bt_a2dp_ep_cap_ie##_name =\
129 {.len = BT_A2DP_SBC_IE_LENGTH, .codec_ie = {A2DP_SBC_SAMP_FREQ_44100 |\
130 A2DP_SBC_SAMP_FREQ_48000 | A2DP_SBC_CH_MODE_MONO | A2DP_SBC_CH_MODE_STREO |\
131 A2DP_SBC_CH_MODE_JOINT, A2DP_SBC_BLK_LEN_16 |\
132 A2DP_SBC_SUBBAND_8 | A2DP_SBC_ALLOC_MTHD_LOUDNESS, 18U, 35U}};\
133 static struct bt_a2dp_ep _name = BT_A2DP_SINK_EP_INIT(BT_A2DP_SBC,\
134 &bt_a2dp_ep_cap_ie##_name)
135 
136 /** @brief define the default SBC source endpoint that can be used as bt_a2dp_register_endpoint's
137  * parameter.
138  *
139  * SBC is mandatory as a2dp specification, BT_A2DP_SBC_SOURCE_EP_DEFAULT
140  * is more convenient for user to register SBC endpoint.
141  *
142  *  @param _name the endpoint variable name.
143  */
144 #define BT_A2DP_SBC_SOURCE_EP_DEFAULT(_name)\
145 static struct bt_a2dp_codec_ie bt_a2dp_ep_cap_ie##_name =\
146 {.len = BT_A2DP_SBC_IE_LENGTH, .codec_ie = {A2DP_SBC_SAMP_FREQ_44100 | \
147 A2DP_SBC_SAMP_FREQ_48000 | A2DP_SBC_CH_MODE_MONO | A2DP_SBC_CH_MODE_STREO | \
148 A2DP_SBC_CH_MODE_JOINT, A2DP_SBC_BLK_LEN_16 | A2DP_SBC_SUBBAND_8 | A2DP_SBC_ALLOC_MTHD_LOUDNESS,\
149 18U, 35U},};\
150 static struct bt_a2dp_ep _name = BT_A2DP_SOURCE_EP_INIT(BT_A2DP_SBC,\
151 &bt_a2dp_ep_cap_ie##_name)
152 
153 /** @brief define the SBC default configuration.
154  *
155  *  @param _name unique structure name postfix.
156  *  @param _freq_cfg sbc codec frequency.
157  *               for example: A2DP_SBC_SAMP_FREQ_44100
158  *  @param _ch_mode_cfg sbc codec channel mode.
159  *               for example: A2DP_SBC_CH_MODE_JOINT
160  *  @param _blk_len_cfg sbc codec block length.
161  *               for example: A2DP_SBC_BLK_LEN_16
162  *  @param _subband_cfg sbc codec subband.
163  *               for example: A2DP_SBC_SUBBAND_8
164  *  @param _alloc_mthd_cfg sbc codec allocate method.
165  *               for example: A2DP_SBC_ALLOC_MTHD_LOUDNESS
166  *  @param _min_bitpool_cfg sbc codec min bit pool. for example: 18
167  *  @param _max_bitpool_cfg sbc codec max bit pool. for example: 35
168  */
169 #define BT_A2DP_SBC_EP_CFG(_name, _freq_cfg, _ch_mode_cfg, _blk_len_cfg, _subband_cfg,\
170 _alloc_mthd_cfg, _min_bitpool_cfg, _max_bitpool_cfg)\
171 static struct bt_a2dp_codec_ie bt_a2dp_codec_ie##_name = {\
172 .len = BT_A2DP_SBC_IE_LENGTH, .codec_ie = {_freq_cfg | _ch_mode_cfg,\
173 _blk_len_cfg | _subband_cfg | _alloc_mthd_cfg, _min_bitpool_cfg, _max_bitpool_cfg},};\
174 struct bt_a2dp_codec_cfg _name = {.codec_config = &bt_a2dp_codec_ie##_name,}
175 
176 /** @brief define the SBC default configuration.
177  *
178  *  @param _name unique structure name postfix.
179  *  @param _freq_cfg the frequency to configure the remote same codec type endpoint.
180  */
181 #define BT_A2DP_SBC_EP_CFG_DEFAULT(_name, _freq_cfg)\
182 static struct bt_a2dp_codec_ie bt_a2dp_codec_ie##_name = {\
183 .len = BT_A2DP_SBC_IE_LENGTH, .codec_ie = {_freq_cfg | A2DP_SBC_CH_MODE_JOINT,\
184 A2DP_SBC_BLK_LEN_16 | A2DP_SBC_SUBBAND_8 | A2DP_SBC_ALLOC_MTHD_LOUDNESS, 18U, 35U},};\
185 struct bt_a2dp_codec_cfg _name = {.codec_config = &bt_a2dp_codec_ie##_name,}
186 
187 /**
188  * @brief A2DP error code
189  */
190 enum bt_a2dp_err_code {
191 	/** Media Codec Type is not valid */
192 	BT_A2DP_INVALID_CODEC_TYPE = 0xC1,
193 	/** Media Codec Type is not supported */
194 	BT_A2DP_NOT_SUPPORTED_CODEC_TYPE = 0xC2,
195 	/** Sampling Frequency is not valid or multiple values have been selected */
196 	BT_A2DP_INVALID_SAMPLING_FREQUENCY = 0xC3,
197 	/** Sampling Frequency is not supported */
198 	BT_A2DP_NOT_SUPPORTED_SAMPLING_FREQUENCY = 0xC4,
199 	/** Channel Mode is not valid or multiple values have been selected */
200 	BT_A2DP_INVALID_CHANNEL_MODE = 0xC5,
201 	/** Channel Mode is not supported */
202 	BT_A2DP_NOT_SUPPORTED_CHANNEL_MODE = 0xC6,
203 	/** None or multiple values have been selected for Number of Subbands */
204 	BT_A2DP_INVALID_SUBBANDS = 0xC7,
205 	/** Number of Subbands is not supported */
206 	BT_A2DP_NOT_SUPPORTED_SUBBANDS = 0xC8,
207 	/** None or multiple values have been selected for Allocation Method */
208 	BT_A2DP_INVALID_ALLOCATION_METHOD = 0xC9,
209 	/** Allocation Method is not supported */
210 	BT_A2DP_NOT_SUPPORTED_ALLOCATION_METHOD = 0xCA,
211 	/** Minimum Bitpool Value is not valid */
212 	BT_A2DP_INVALID_MINIMUM_BITPOOL_VALUE = 0xCB,
213 	/** Minimum Bitpool Value is not supported */
214 	BT_A2DP_NOT_SUPPORTED_MINIMUM_BITPOOL_VALUE = 0xCC,
215 	/** Maximum Bitpool Value is not valid */
216 	BT_A2DP_INVALID_MAXIMUM_BITPOOL_VALUE = 0xCD,
217 	/** Maximum Bitpool Value is not supported */
218 	BT_A2DP_NOT_SUPPORTED_MAXIMUM_BITPOOL_VALUE = 0xCE,
219 	/** None or multiple values have been selected for Layer */
220 	BT_A2DP_INVALID_LAYER = 0xCF,
221 	/** Layer is not supported */
222 	BT_A2DP_NOT_SUPPORTED_LAYER = 0xD0,
223 	/** CRC is not supported */
224 	BT_A2DP_NOT_SUPPORTED_CRC = 0xD1,
225 	/** MPF-2 is not supported */
226 	BT_A2DP_NOT_SUPPORTED_MPF = 0xD2,
227 	/** VBR is not supported */
228 	BT_A2DP_NOT_SUPPORTED_VBR = 0xD3,
229 	/** None or multiple values have been selected for Bit Rate */
230 	BT_A2DP_INVALID_BIT_RATE = 0xD4,
231 	/** Bit Rate is not supported */
232 	BT_A2DP_NOT_SUPPORTED_BIT_RATE = 0xD5,
233 	/** Either 1) Object type is not valid or
234 	 * 2) None or multiple values have been selected for Object Type
235 	 */
236 	BT_A2DP_INVALID_OBJECT_TYPE = 0xD6,
237 	/** Object Type is not supported */
238 	BT_A2DP_NOT_SUPPORTED_OBJECT_TYPE = 0xD7,
239 	/** Either 1) Channels is not valid or
240 	 * 2) None or multiple values have been selected for Channels
241 	 */
242 	BT_A2DP_INVALID_CHANNELS = 0xD8,
243 	/** Channels is not supported */
244 	BT_A2DP_NOT_SUPPORTED_CHANNELS = 0xD9,
245 	/** Version is not valid */
246 	BT_A2DP_INVALID_VERSION = 0xDA,
247 	/** Version is not supported */
248 	BT_A2DP_NOT_SUPPORTED_VERSION = 0xDB,
249 	/** Maximum SUL is not acceptable for the Decoder in the SNK */
250 	BT_A2DP_NOT_SUPPORTED_MAXIMUM_SUL = 0xDC,
251 	/** None or multiple values have been selected for Block Length */
252 	BT_A2DP_INVALID_BLOCK_LENGTH = 0xDD,
253 	/** The requested CP Type is not supported */
254 	BT_A2DP_INVALID_CP_TYPE = 0xE0,
255 	/** The format of Content Protection Service Capability/Content
256 	 * Protection Scheme Dependent Data is not correct
257 	 */
258 	BT_A2DP_INVALID_CP_FORMAT = 0xE1,
259 	/** The codec parameter is invalid.
260 	 * Used if a more specific error code does not exist for the codec in use
261 	 */
262 	BT_A2DP_INVALID_CODEC_PARAMETER = 0xE2,
263 	/** The codec parameter is not supported.
264 	 * Used if a more specific error code does not exist for the codec in use
265 	 */
266 	BT_A2DP_NOT_SUPPORTED_CODEC_PARAMETER = 0xE3,
267 	/** Combination of Object Type and DRC is invalid */
268 	BT_A2DP_INVALID_DRC = 0xE4,
269 	/** DRC is not supported */
270 	BT_A2DP_NOT_SUPPORTED_DRC = 0xE5,
271 };
272 
273 /** @brief Codec Type */
274 enum bt_a2dp_codec_type {
275 	/** Codec SBC */
276 	BT_A2DP_SBC = 0x00,
277 	/** Codec MPEG-1 */
278 	BT_A2DP_MPEG1 = 0x01,
279 	/** Codec MPEG-2 */
280 	BT_A2DP_MPEG2 = 0x02,
281 	/** Codec ATRAC */
282 	BT_A2DP_ATRAC = 0x04,
283 	/** Codec Non-A2DP */
284 	BT_A2DP_VENDOR = 0xff
285 };
286 
287 /** @brief A2DP structure */
288 struct bt_a2dp;
289 
290 /* Internal to pass build */
291 struct bt_a2dp_stream;
292 
293 /** @brief codec information elements for the endpoint */
294 struct bt_a2dp_codec_ie {
295 	/** Length of codec_cap */
296 	uint8_t len;
297 	/** codec information element */
298 	uint8_t codec_ie[A2DP_MAX_IE_LENGTH];
299 };
300 
301 /** @brief The endpoint configuration */
302 struct bt_a2dp_codec_cfg {
303 	/** The media codec configuration content */
304 	struct bt_a2dp_codec_ie *codec_config;
305 };
306 
307 /** @brief Stream End Point */
308 struct bt_a2dp_ep {
309 	/** Code Type @ref bt_a2dp_codec_type */
310 	uint8_t codec_type;
311 	/** Capabilities */
312 	struct bt_a2dp_codec_ie *codec_cap;
313 	/** AVDTP Stream End Point Identifier */
314 	struct bt_avdtp_sep sep;
315 	/* Internally used stream object pointer */
316 	struct bt_a2dp_stream *stream;
317 };
318 
319 struct bt_a2dp_ep_info {
320 	/** Code Type @ref bt_a2dp_codec_type */
321 	uint8_t codec_type;
322 	/** Codec capabilities, if SBC, use function of a2dp_codec_sbc.h to parse it */
323 	struct bt_a2dp_codec_ie codec_cap;
324 	/** Stream End Point Information */
325 	struct bt_avdtp_sep_info sep_info;
326 };
327 
328 /** @brief Helper enum to be used as return value of bt_a2dp_discover_ep_cb.
329  *  The value informs the caller to perform further pending actions or stop them.
330  */
331 enum {
332 	BT_A2DP_DISCOVER_EP_STOP = 0,
333 	BT_A2DP_DISCOVER_EP_CONTINUE,
334 };
335 
336 /** @typedef bt_a2dp_discover_ep_cb
337  *
338  *  @brief Called when a stream endpoint is discovered.
339  *
340  *  A function of this type is given by the user to the bt_a2dp_discover_param
341  *  object. It'll be called on each valid stream endpoint discovery completion.
342  *  When no more endpoint then NULL is passed to the user. Otherwise user can get
343  *  valid endpoint information from parameter info, user can set parameter ep to
344  *  get the endpoint after the callback is return.
345  *  The returned function value allows the user to control retrieving follow-up
346  *  endpoints if any. If the user doesn't want to read more endpoints since
347  *  current found endpoints fulfill its requirements then should return
348  *  BT_A2DP_DISCOVER_EP_STOP. Otherwise returned value means
349  *  more subcall iterations are allowable.
350  *
351  *  @param a2dp a2dp connection object identifying a2dp connection to queried remote.
352  *  @param info Object pointing to the information of the callbacked endpoint.
353  *  @param ep If the user want to use this found endpoint, user can set value to it
354  *  to get the endpoint that can be used further in other A2DP APIs. It is NULL if info
355  *  is NULL (no more endpoint is found).
356  *
357  *  @return BT_A2DP_DISCOVER_EP_STOP in case of no more need to continue discovery
358  *  for next endpoint. By returning BT_A2DP_DISCOVER_EP_STOP user allows this
359  *  discovery continuation.
360  */
361 typedef uint8_t (*bt_a2dp_discover_ep_cb)(struct bt_a2dp *a2dp,
362 		struct bt_a2dp_ep_info *info, struct bt_a2dp_ep **ep);
363 
364 struct bt_a2dp_discover_param {
365 	/** discover callback */
366 	bt_a2dp_discover_ep_cb cb;
367 	/** The discovered endpoint info that is callbacked by cb */
368 	struct bt_a2dp_ep_info info;
369 	/** The max count of remote endpoints that can be got,
370 	 *  it save endpoint info internally.
371 	 */
372 	struct bt_avdtp_sep_info *seps_info;
373 	/** The max count of seps (stream endpoint) that can be got in this call route */
374 	uint8_t sep_count;
375 };
376 
377 /** @brief The connecting callback */
378 struct bt_a2dp_cb {
379 	/** @brief A a2dp connection has been established.
380 	 *
381 	 *  This callback notifies the application of a a2dp connection.
382 	 *  It means the AVDTP L2CAP connection.
383 	 *  In case the err parameter is non-zero it means that the
384 	 *  connection establishment failed.
385 	 *
386 	 *  @param a2dp a2dp connection object.
387 	 *  @param err error code.
388 	 */
389 	void (*connected)(struct bt_a2dp *a2dp, int err);
390 	/** @brief A a2dp connection has been disconnected.
391 	 *
392 	 *  This callback notifies the application that a a2dp connection
393 	 *  has been disconnected.
394 	 *
395 	 *  @param a2dp a2dp connection object.
396 	 */
397 	void (*disconnected)(struct bt_a2dp *a2dp);
398 	/**
399 	 * @brief Endpoint config request callback
400 	 *
401 	 * The callback is called whenever an endpoint is requested to be
402 	 * configured.
403 	 *
404 	 *  @param a2dp a2dp connection object.
405 	 *  @param[in]  ep        Local Audio Endpoint being configured.
406 	 *  @param[in]  codec_cfg Codec configuration.
407 	 *  @param[out] stream    Pointer to stream that will be configured for the endpoint.
408 	 *  @param[out] rsp_err_code  give the error code if response error.
409 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
410 	 *
411 	 * @return 0 in case of success or negative value in case of error.
412 	 */
413 	int (*config_req)(struct bt_a2dp *a2dp, struct bt_a2dp_ep *ep,
414 			struct bt_a2dp_codec_cfg *codec_cfg, struct bt_a2dp_stream **stream,
415 			uint8_t *rsp_err_code);
416 	/** @brief Callback function for bt_a2dp_stream_config()
417 	 *
418 	 *  Called when the codec configure operation is completed.
419 	 *
420 	 *  @param[in] stream    Pointer to stream object.
421 	 *  @param[in] rsp_err_code the remote responded error code
422 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
423 	 */
424 	void (*config_rsp)(struct bt_a2dp_stream *stream, uint8_t rsp_err_code);
425 	/**
426 	 * @brief stream establishment request callback
427 	 *
428 	 * The callback is called whenever an stream is requested to be
429 	 * established (open cmd and create the stream l2cap channel).
430 	 *
431 	 *  @param[in] stream    Pointer to stream object.
432 	 *  @param[out] rsp_err_code  give the error code if response error.
433 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
434 	 *
435 	 * @return 0 in case of success or negative value in case of error.
436 	 */
437 	int (*establish_req)(struct bt_a2dp_stream *stream, uint8_t *rsp_err_code);
438 	/** @brief Callback function for bt_a2dp_stream_establish()
439 	 *
440 	 *  Called when the establishment operation is completed.
441 	 *  (open cmd and create the stream l2cap channel).
442 	 *
443 	 *  @param[in] stream    Pointer to stream object.
444 	 *  @param[in] rsp_err_code the remote responded error code
445 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
446 	 */
447 	void (*establish_rsp)(struct bt_a2dp_stream *stream, uint8_t rsp_err_code);
448 	/**
449 	 * @brief stream release request callback
450 	 *
451 	 * The callback is called whenever an stream is requested to be
452 	 * released (release cmd and release the l2cap channel)
453 	 *
454 	 *  @param[in] stream    Pointer to stream object.
455 	 *  @param[out] rsp_err_code  give the error code if response error.
456 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
457 	 *
458 	 * @return 0 in case of success or negative value in case of error.
459 	 */
460 	int (*release_req)(struct bt_a2dp_stream *stream, uint8_t *rsp_err_code);
461 	/** @brief Callback function for bt_a2dp_stream_release()
462 	 *
463 	 *  Called when the release operation is completed.
464 	 *  (release cmd and release the l2cap channel)
465 	 *
466 	 *  @param[in] stream    Pointer to stream object.
467 	 *  @param[in] rsp_err_code the remote responded error code
468 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
469 	 */
470 	void (*release_rsp)(struct bt_a2dp_stream *stream, uint8_t rsp_err_code);
471 	/**
472 	 * @brief stream start request callback
473 	 *
474 	 * The callback is called whenever an stream is requested to be
475 	 * started.
476 	 *
477 	 *  @param[in] stream    Pointer to stream object.
478 	 *  @param[out] rsp_err_code  give the error code if response error.
479 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
480 	 *
481 	 * @return 0 in case of success or negative value in case of error.
482 	 */
483 	int (*start_req)(struct bt_a2dp_stream *stream, uint8_t *rsp_err_code);
484 	/** @brief Callback function for bt_a2dp_stream_start()
485 	 *
486 	 *  Called when the start operation is completed.
487 	 *
488 	 *  @param[in] stream    Pointer to stream object.
489 	 *  @param[in] rsp_err_code the remote responded error code
490 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
491 	 */
492 	void (*start_rsp)(struct bt_a2dp_stream *stream, uint8_t rsp_err_code);
493 	/**
494 	 * @brief Endpoint suspend request callback
495 	 *
496 	 * The callback is called whenever an stream is requested to be
497 	 * suspended.
498 	 *
499 	 *  @param[in] stream    Pointer to stream object.
500 	 *  @param[out] rsp_err_code  give the error code if response error.
501 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
502 	 *
503 	 * @return 0 in case of success or negative value in case of error.
504 	 */
505 	int (*suspend_req)(struct bt_a2dp_stream *stream, uint8_t *rsp_err_code);
506 	/** @brief Callback function for bt_a2dp_stream_suspend()
507 	 *
508 	 *  Called when the suspend operation is completed.
509 	 *
510 	 *  @param[in] stream    Pointer to stream object.
511 	 *  @param[in] rsp_err_code the remote responded error code
512 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
513 	 */
514 	void (*suspend_rsp)(struct bt_a2dp_stream *stream, uint8_t rsp_err_code);
515 	/**
516 	 * @brief Endpoint config request callback
517 	 *
518 	 * The callback is called whenever an endpoint is requested to be
519 	 * reconfigured.
520 	 *
521 	 *  @param[in] stream    Pointer to stream object.
522 	 *  @param[out] rsp_err_code  give the error code if response error.
523 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
524 	 *
525 	 * @return 0 in case of success or negative value in case of error.
526 	 */
527 	int (*reconfig_req)(struct bt_a2dp_stream *stream, uint8_t *rsp_err_code);
528 	/** @brief Callback function for bt_a2dp_stream_reconfig()
529 	 *
530 	 *  Called when the reconfig operation is completed.
531 	 *
532 	 *  @param[in] stream    Pointer to stream object.
533 	 *  @param[in] rsp_err_code the remote responded error code
534 	 *                          bt_a2dp_err_code or bt_avdtp_err_code
535 	 */
536 	void (*reconfig_rsp)(struct bt_a2dp_stream *stream, uint8_t rsp_err_code);
537 };
538 
539 /** @brief A2DP Connect.
540  *
541  *  This function is to be called after the conn parameter is obtained by
542  *  performing a GAP procedure. The API is to be used to establish A2DP
543  *  connection between devices.
544  *  This function only establish AVDTP L2CAP Signaling connection.
545  *  After connection success, the callback that is registered by
546  *  bt_a2dp_register_connect_callback is called.
547  *
548  *  @param conn Pointer to bt_conn structure.
549  *
550  *  @return pointer to struct bt_a2dp in case of success or NULL in case
551  *  of error.
552  */
553 struct bt_a2dp *bt_a2dp_connect(struct bt_conn *conn);
554 
555 /** @brief disconnect l2cap a2dp
556  *
557  * This function close AVDTP L2CAP Signaling connection.
558  * It closes the AVDTP L2CAP Media connection too if it is established.
559  *
560  *  @param a2dp The a2dp instance.
561  *
562  *  @return 0 in case of success and error code in case of error.
563  */
564 int bt_a2dp_disconnect(struct bt_a2dp *a2dp);
565 
566 /** @brief Endpoint Registration.
567  *
568  *  @param ep Pointer to bt_a2dp_ep structure.
569  *  @param media_type Media type that the Endpoint is.
570  *  @param role Role of Endpoint.
571  *
572  *  @return 0 in case of success and error code in case of error.
573  */
574 int bt_a2dp_register_ep(struct bt_a2dp_ep *ep, uint8_t media_type, uint8_t role);
575 
576 /** @brief register callback.
577  *
578  *  The cb is called when bt_a2dp_connect is called or it is operated by remote device.
579  *
580  *  @param cb The callback function.
581  *
582  *  @return 0 in case of success and error code in case of error.
583  */
584 int bt_a2dp_register_cb(struct bt_a2dp_cb *cb);
585 
586 /** @brief Discover remote endpoints.
587  *
588  *  @param a2dp The a2dp instance.
589  *  @param param the discover used param.
590  *
591  *  @return 0 in case of success and error code in case of error.
592  */
593 int bt_a2dp_discover(struct bt_a2dp *a2dp, struct bt_a2dp_discover_param *param);
594 
595 /** @brief A2DP Stream */
596 struct bt_a2dp_stream {
597 	/** local endpoint */
598 	struct bt_a2dp_ep *local_ep;
599 	/** remote endpoint */
600 	struct bt_a2dp_ep *remote_ep;
601 	/** remote endpoint's Stream End Point ID */
602 	uint8_t remote_ep_id;
603 	/** Audio stream operations */
604 	struct bt_a2dp_stream_ops *ops;
605 	/** the a2dp connection */
606 	struct bt_a2dp *a2dp;
607 	/** the stream current configuration */
608 	struct bt_a2dp_codec_ie codec_config;
609 };
610 
611 /** @brief The stream endpoint related operations */
612 struct bt_a2dp_stream_ops {
613 	/**
614 	 * @brief Stream configured callback
615 	 *
616 	 * The callback is called whenever an Audio Stream has been configured.
617 	 *
618 	 * @param stream Stream object that has been configured.
619 	 */
620 	void (*configured)(struct bt_a2dp_stream *stream);
621 	/**
622 	 * @brief Stream establishment callback
623 	 *
624 	 * The callback is called whenever an Audio Stream has been established.
625 	 *
626 	 * @param stream Stream object that has been established.
627 	 */
628 	void (*established)(struct bt_a2dp_stream *stream);
629 	/**
630 	 * @brief Stream release callback
631 	 *
632 	 * The callback is called whenever an Audio Stream has been released.
633 	 *
634 	 * @param stream Stream object that has been released.
635 	 */
636 	void (*released)(struct bt_a2dp_stream *stream);
637 	/**
638 	 * @brief Stream start callback
639 	 *
640 	 * The callback is called whenever an Audio Stream has been started.
641 	 *
642 	 * @param stream Stream object that has been started.
643 	 */
644 	void (*started)(struct bt_a2dp_stream *stream);
645 	/**
646 	 * @brief Stream suspend callback
647 	 *
648 	 * The callback is called whenever an Audio Stream has been suspended.
649 	 *
650 	 * @param stream Stream object that has been suspended.
651 	 */
652 	void (*suspended)(struct bt_a2dp_stream *stream);
653 	/**
654 	 * @brief Stream reconfigured callback
655 	 *
656 	 * The callback is called whenever an Audio Stream has been reconfigured.
657 	 *
658 	 * @param stream Stream object that has been reconfigured.
659 	 */
660 	void (*reconfigured)(struct bt_a2dp_stream *stream);
661 #if defined(CONFIG_BT_A2DP_SINK)
662 	/** @brief the media streaming data, only for sink
663 	 *
664 	 *  @param buf the data buf
665 	 *  @param seq_num the sequence number
666 	 *  @param ts the time stamp
667 	 */
668 	void (*recv)(struct bt_a2dp_stream *stream,
669 		struct net_buf *buf, uint16_t seq_num, uint32_t ts);
670 #endif
671 #if defined(CONFIG_BT_A2DP_SOURCE)
672 	/**
673 	 * @brief Stream audio HCI sent callback
674 	 *
675 	 * This callback will be called once the controller marks the SDU
676 	 * as completed. When the controller does so is implementation
677 	 * dependent. It could be after the SDU is enqueued for transmission,
678 	 * or after it is sent on air or flushed.
679 	 *
680 	 * This callback is only used if the ISO data path is HCI.
681 	 *
682 	 * @param stream Stream object.
683 	 */
684 	void (*sent)(struct bt_a2dp_stream *stream);
685 #endif
686 };
687 
688 /**
689  * @brief Register Audio callbacks for a stream.
690  *
691  * Register Audio callbacks for a stream.
692  *
693  * @param stream Stream object.
694  * @param ops    Stream operations structure.
695  */
696 void bt_a2dp_stream_cb_register(struct bt_a2dp_stream *stream, struct bt_a2dp_stream_ops *ops);
697 
698 /** @brief configure endpoint.
699  *
700  *  bt_a2dp_discover can be used to find remote's endpoints.
701  *  This function to configure the selected endpoint that is found by
702  *  bt_a2dp_discover.
703  *  This function sends AVDTP_SET_CONFIGURATION.
704  *
705  *  @param a2dp The a2dp instance.
706  *  @param stream Stream object.
707  *  @param local_ep The configured endpoint that is registered.
708  *  @param remote_ep The remote endpoint.
709  *  @param config The config to configure the endpoint.
710  *
711  *  @return 0 in case of success and error code in case of error.
712  */
713 int bt_a2dp_stream_config(struct bt_a2dp *a2dp, struct bt_a2dp_stream *stream,
714 		struct bt_a2dp_ep *local_ep, struct bt_a2dp_ep *remote_ep,
715 		struct bt_a2dp_codec_cfg *config);
716 
717 /** @brief establish a2dp streamer.
718  *
719  * This function sends the AVDTP_OPEN command and create the l2cap channel.
720  *
721  *  @param stream The stream object.
722  *
723  *  @return 0 in case of success and error code in case of error.
724  */
725 int bt_a2dp_stream_establish(struct bt_a2dp_stream *stream);
726 
727 /** @brief release a2dp streamer.
728  *
729  * This function sends the AVDTP_CLOSE command and release the l2cap channel.
730  *
731  *  @param stream The stream object.
732  *
733  *  @return 0 in case of success and error code in case of error.
734  */
735 int bt_a2dp_stream_release(struct bt_a2dp_stream *stream);
736 
737 /** @brief start a2dp streamer.
738  *
739  * This function sends the AVDTP_START command.
740  *
741  *  @param stream The stream object.
742  *
743  *  @return 0 in case of success and error code in case of error.
744  */
745 int bt_a2dp_stream_start(struct bt_a2dp_stream *stream);
746 
747 /** @brief suspend a2dp streamer.
748  *
749  * This function sends the AVDTP_SUSPEND command.
750  *
751  *  @param stream The stream object.
752  *
753  *  @return 0 in case of success and error code in case of error.
754  */
755 int bt_a2dp_stream_suspend(struct bt_a2dp_stream *stream);
756 
757 /** @brief re-configure a2dp streamer
758  *
759  * This function sends the AVDTP_RECONFIGURE command.
760  *
761  *  @param stream The stream object.
762  *  @param config The config to configure the stream.
763  *
764  *  @return 0 in case of success and error code in case of error.
765  */
766 int bt_a2dp_stream_reconfig(struct bt_a2dp_stream *stream, struct bt_a2dp_codec_cfg *config);
767 
768 /** @brief get the stream l2cap mtu
769  *
770  *  @param stream The stream object.
771  *
772  *  @return mtu value
773  */
774 uint32_t bt_a2dp_get_mtu(struct bt_a2dp_stream *stream);
775 
776 #if defined(CONFIG_BT_A2DP_SOURCE)
777 /** @brief send a2dp media data
778  *
779  * Only A2DP source side can call this function.
780  *
781  *  @param stream The stream object.
782  *  @param buf  The data.
783  *  @param seq_num The sequence number.
784  *  @param ts The time stamp.
785  *
786  *  @return 0 in case of success and error code in case of error.
787  */
788 int bt_a2dp_stream_send(struct bt_a2dp_stream *stream,  struct net_buf *buf,
789 			uint16_t seq_num, uint32_t ts);
790 #endif
791 
792 #ifdef __cplusplus
793 }
794 #endif
795 
796 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_A2DP_H_ */
797