xref: /btstack/src/classic/avdtp.h (revision 8201da882231bee1019f0a84a70494b65e0d959a)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * Audio/Video Distribution Transport Protocol (AVDTP)
40  *
41  * This protocol defines A/V stream negotiation, establishment, and transmission
42  * procedures. Also specified are the message formats that are exchanged between
43  * such devices to transport their A/V streams in A/V distribution applications.
44  *
45  * Media packets are unidirectional, they travel downstream from AVDTP Source to AVDTP Sink.
46  */
47 
48 #ifndef AVDTP_H
49 #define AVDTP_H
50 
51 #include <stdint.h>
52 #include "hci.h"
53 #include "btstack_ring_buffer.h"
54 
55 #if defined __cplusplus
56 extern "C" {
57 #endif
58 
59 // Limit L2CAP MTU to fit largest packet for BR/EDR
60 #define AVDTP_L2CAP_MTU (HCI_ACL_3DH5_SIZE - L2CAP_HEADER_SIZE)
61 
62 #define AVDTP_MAX_NUM_SEPS 10
63 #define AVDTP_MAX_CSRC_NUM 15
64 #define AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN 10
65 
66 // Supported Features
67 #define AVDTP_SOURCE_FEATURE_MASK_PLAYER        0x0001u
68 #define AVDTP_SOURCE_FEATURE_MASK_MICROPHONE    0x0002u
69 #define AVDTP_SOURCE_FEATURE_MASK_TUNER         0x0004u
70 #define AVDTP_SOURCE_FEATURE_MASK_MIXER         0x0008u
71 
72 #define AVDTP_SINK_FEATURE_MASK_HEADPHONE       0x0001u
73 #define AVDTP_SINK_FEATURE_MASK_SPEAKER         0x0002u
74 #define AVDTP_SINK_FEATURE_MASK_RECORDER        0x0004u
75 #define AVDTP_SINK_FEATURE_MASK_AMPLIFIER       0x0008u
76 
77 // ACP to INT, Signal Response Header Error Codes
78 #define AVDTP_ERROR_CODE_BAD_HEADER_FORMAT     0x01
79 
80 // ACP to INT, Signal Response Payload Format Error Codes
81 #define AVDTP_ERROR_CODE_BAD_LENGTH                 0x11
82 #define AVDTP_ERROR_CODE_BAD_ACP_SEID               0x12
83 #define AVDTP_ERROR_CODE_SEP_IN_USE                 0x13
84 #define AVDTP_ERROR_CODE_SEP_NOT_IN_USE             0x14
85 #define AVDTP_ERROR_CODE_BAD_SERV_CATEGORY          0x17
86 #define AVDTP_ERROR_CODE_BAD_PAYLOAD_FORMAT         0x18
87 #define AVDTP_ERROR_CODE_NOT_SUPPORTED_COMMAND      0x19
88 #define AVDTP_ERROR_CODE_INVALID_CAPABILITIES       0x1A
89 
90 // ACP to INT, Signal Response Transport Service Capabilities Error Codes
91 #define AVDTP_ERROR_CODE_BAD_RECOVERY_TYPE          0x22
92 #define AVDTP_ERROR_CODE_BAD_MEDIA_TRANSPORT_FORMAT 0x23
93 #define AVDTP_ERROR_CODE_BAD_RECOVERY_FORMAT        0x25
94 #define AVDTP_ERROR_CODE_BAD_ROHC_FORMAT            0x26
95 #define AVDTP_ERROR_CODE_BAD_CP_FORMAT              0x27
96 #define AVDTP_ERROR_CODE_BAD_MULTIPLEXING_FORMAT    0x28
97 #define AVDTP_ERROR_CODE_UNSUPPORTED_CONFIGURATION  0x29
98 
99 // ACP to INT, Procedure Error Codes
100 #define AVDTP_ERROR_CODE_BAD_STATE                  0x31
101 
102 // Internal Error Codes
103 #define AVDTP_INVALID_SEP_SEID                      0xFF
104 
105 typedef enum {
106     CODEC_SPECIFIC_ERROR_CODE_ACCEPT                             = 0x00,
107     CODEC_SPECIFIC_ERROR_CODE_INVALID_CODEC_TYPE                 = 0xC1,
108     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_CODEC_TYPE           = 0xC2,
109     CODEC_SPECIFIC_ERROR_CODE_INVALID_SAMPLING_FREQUENCY         = 0xC3,
110     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_SAMPLING_FREQUENCY   = 0xC4,
111     CODEC_SPECIFIC_ERROR_CODE_INVALID_CHANNEL_MODE               = 0xC5,
112     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_CHANNEL_MODE         = 0xC6,
113     CODEC_SPECIFIC_ERROR_CODE_INVALID_SUBBANDS                   = 0xC7,
114     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_SUBBANDS             = 0xC8,
115     CODEC_SPECIFIC_ERROR_CODE_INVALID_ALLOCATION_METHOD          = 0xC9,
116     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_ALLOCATION_METHOD    = 0xCA,
117     CODEC_SPECIFIC_ERROR_CODE_INVALID_MINIMUM_BITPOOL_VALUE      = 0xCB,
118     CODEC_SPECIFIC_ERROR_CODE_INVALID_MAXIMUM_BITPOOL_VALUE      = 0xCD,
119     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_VBR                  = 0xD3,
120     CODEC_SPECIFIC_ERROR_CODE_INVALID_OBJECT_TYPE                = 0xD6,
121     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_OBJECT_TYPE          = 0xD7,
122     CODEC_SPECIFIC_ERROR_CODE_INVALID_CHANNELS                   = 0xD8,
123     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_CHANNELS             = 0xD9,
124     CODEC_SPECIFIC_ERROR_CODE_INVALID_BLOCK_LENGTH               = 0xDD,
125     CODEC_SPECIFIC_ERROR_CODE_INVALID_DRC                        = 0xE4,
126 } codec_specific_error_code_t;
127 
128 // Signal Identifier fields
129 typedef enum {
130     AVDTP_SI_NONE = 0x00,
131     AVDTP_SI_DISCOVER = 0x01,
132     AVDTP_SI_GET_CAPABILITIES,
133     AVDTP_SI_SET_CONFIGURATION,
134     AVDTP_SI_GET_CONFIGURATION,
135     AVDTP_SI_RECONFIGURE, //5
136     AVDTP_SI_OPEN,  //6
137     AVDTP_SI_START, //7
138     AVDTP_SI_CLOSE,
139     AVDTP_SI_SUSPEND,
140     AVDTP_SI_ABORT, //10
141     AVDTP_SI_SECURITY_CONTROL,
142     AVDTP_SI_GET_ALL_CAPABILITIES, //12
143     AVDTP_SI_DELAYREPORT,
144 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
145     AVDTP_SI_ACCEPT_START
146 #endif
147 } avdtp_signal_identifier_t;
148 
149 typedef enum {
150     AVDTP_SINGLE_PACKET = 0,
151     AVDTP_START_PACKET    ,
152     AVDTP_CONTINUE_PACKET ,
153     AVDTP_END_PACKET
154 } avdtp_packet_type_t;
155 
156 typedef enum {
157     AVDTP_CMD_MSG = 0,
158     AVDTP_GENERAL_REJECT_MSG   ,
159     AVDTP_RESPONSE_ACCEPT_MSG ,
160     AVDTP_RESPONSE_REJECT_MSG
161 } avdtp_message_type_t;
162 
163 typedef enum {
164     AVDTP_AUDIO = 0,
165     AVDTP_VIDEO,
166     AVDTP_MULTIMEDIA
167 } avdtp_media_type_t;
168 
169 typedef enum {
170     AVDTP_CODEC_SBC             = 0x00,
171     AVDTP_CODEC_MPEG_1_2_AUDIO  = 0x01,
172     AVDTP_CODEC_MPEG_2_4_AAC    = 0x02,
173     AVDTP_CODEC_MPEG_D_USAC     = 0x03,
174     AVDTP_CODEC_ATRAC_FAMILY    = 0x04,
175     AVDTP_CODEC_NON_A2DP        = 0xFF
176 } avdtp_media_codec_type_t;
177 
178 typedef enum {
179     AVDTP_USAC_OBJECT_TYPE_MPEG_D_DRC = 0x00,
180     AVDTP_USAC_OBJECT_TYPE_RFU
181 } avdtp_usac_object_type_t;
182 
183 typedef enum {
184     AVDTP_CONTENT_PROTECTION_DTCP = 0x0001,
185     AVDTP_CONTENT_PROTECTION_SCMS_T = 0x0002
186 } avdtp_content_protection_type_t;
187 
188 typedef enum {
189     AVDTP_SOURCE = 0,
190     AVDTP_SINK
191 } avdtp_sep_type_t;
192 
193 typedef enum {
194     AVDTP_ROLE_SOURCE = 0,
195     AVDTP_ROLE_SINK
196 } avdtp_role_t;
197 
198 typedef enum {
199     AVDTP_SERVICE_CATEGORY_INVALID_0 = 0x00,
200     AVDTP_MEDIA_TRANSPORT = 0X01,
201     AVDTP_REPORTING,
202     AVDTP_RECOVERY,
203     AVDTP_CONTENT_PROTECTION, //4
204     AVDTP_HEADER_COMPRESSION, //5
205     AVDTP_MULTIPLEXING,       //6
206     AVDTP_MEDIA_CODEC,        //7
207     AVDTP_DELAY_REPORTING,    //8
208     AVDTP_SERVICE_CATEGORY_INVALID_FF = 0xFF
209 } avdtp_service_category_t;
210 
211 typedef struct {
212     uint8_t recovery_type;                  // 0x01 = RFC2733
213     uint8_t maximum_recovery_window_size;   // 0x01 to 0x18, for a Transport Packet
214     uint8_t maximum_number_media_packets;   // 0x01 to 0x18, The maximum number of media packets a specific parity code covers
215 } avdtp_recovery_capabilities_t;
216 
217 typedef struct {
218     avdtp_media_type_t       media_type;
219     avdtp_media_codec_type_t media_codec_type;
220     uint16_t  media_codec_information_len;
221     uint8_t * media_codec_information;
222 } adtvp_media_codec_capabilities_t;
223 
224 
225 typedef struct {
226     uint16_t cp_type;
227     uint16_t cp_type_value_len;
228     uint8_t cp_type_value[AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN];
229 } adtvp_content_protection_t;
230 
231 typedef struct{
232     uint8_t back_ch;  // byte0 - bit 8; 0=Not Available/Not Used; 1=Available/In Use
233     uint8_t media;    // byte0 - bit 7
234     uint8_t recovery; // byte0 - bit 6
235 } avdtp_header_compression_capabilities_t;
236 
237 typedef struct{
238     uint8_t fragmentation; // byte0 - bit 8, Allow Adaptation Layer Fragmentation, 0 no, 1 yes
239     // Request/indicate value of the Transport Session Identifier for a media, reporting, or recovery transport sessions, respectively
240     uint8_t transport_identifiers_num;
241     uint8_t transport_session_identifiers[3];   // byte1, upper 5bits, 0x01 to 0x1E
242     // Request/indicate value for TCID for a media, reporting, or transport session
243     uint8_t tcid[3];         // byte2 0x01 to 0x1E
244 } avdtp_multiplexing_mode_capabilities_t;
245 
246 typedef struct{
247     avdtp_recovery_capabilities_t recovery;
248     adtvp_media_codec_capabilities_t media_codec;
249     adtvp_content_protection_t content_protection;
250     avdtp_header_compression_capabilities_t header_compression;
251     avdtp_multiplexing_mode_capabilities_t multiplexing_mode;
252 } avdtp_capabilities_t;
253 
254 typedef enum{
255     AVDTP_SBC_48000 = 1,
256     AVDTP_SBC_44100 = 2,
257     AVDTP_SBC_32000 = 4,
258     AVDTP_SBC_16000 = 8
259 } avdtp_sbc_sampling_frequency_t;
260 
261 typedef enum{
262     AVDTP_SBC_JOINT_STEREO  = 1,
263     AVDTP_SBC_STEREO        = 2,
264     AVDTP_SBC_DUAL_CHANNEL  = 4,
265     AVDTP_SBC_MONO          = 8
266 } avdtp_sbc_channel_mode_t;
267 
268 typedef enum{
269     AVDTP_SBC_BLOCK_LENGTH_16 = 1,
270     AVDTP_SBC_BLOCK_LENGTH_12 = 2,
271     AVDTP_SBC_BLOCK_LENGTH_8  = 4,
272     AVDTP_SBC_BLOCK_LENGTH_4  = 8
273 } avdtp_sbc_block_length_t;
274 
275 typedef enum{
276     AVDTP_SBC_SUBBANDS_8 = 1,
277     AVDTP_SBC_SUBBANDS_4 = 2
278 } avdtp_sbc_subbands_t;
279 
280 typedef enum{
281     AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS = 1,
282     AVDTP_SBC_ALLOCATION_METHOD_SNR      = 2
283 } avdtp_sbc_allocation_method_t;
284 
285 typedef struct {
286     uint8_t fragmentation;
287     uint8_t starting_packet; // of fragmented SBC frame
288     uint8_t last_packet;     // of fragmented SBC frame
289     uint8_t num_frames;
290 } avdtp_sbc_codec_header_t;
291 
292 typedef enum {
293     AVDTP_MPEG_LAYER_1 = 1,
294     AVDTP_MPEG_LAYER_2,
295     AVDTP_MPEG_LAYER_3,
296 } avdtp_mpeg_layer_t;
297 
298 
299 typedef enum {
300     AVDTP_AAC_MPEG2_LC = 1,
301     AVDTP_AAC_MPEG4_LC,
302     AVDTP_AAC_MPEG4_LTP,
303     AVDTP_AAC_MPEG4_SCALABLE,
304     AVDTP_AAC_MPEG4_HE_AAC,
305     AVDTP_AAC_MPEG4_HE_AACv2,
306     AVDTP_AAC_MPEG4_HE_AAC_ELDv2
307 } avdtp_aac_object_type_t;
308 
309 typedef enum {
310     AVDTP_ATRAC_VERSION_1 = 1,
311     AVDTP_ATRAC_VERSION_2,
312     AVDTP_ATRAC_VERSION_3
313 } avdtp_atrac_version_t;
314 
315 // used for MPEG1/2 Audio, ATRAC (no stereo mode)
316 typedef enum {
317     AVDTP_CHANNEL_MODE_MONO = 1,
318     AVDTP_CHANNEL_MODE_DUAL_CHANNEL,
319     AVDTP_CHANNEL_MODE_STEREO,
320     AVDTP_CHANNEL_MODE_JOINT_STEREO,
321 } avdtp_channel_mode_t;
322 
323 typedef struct {
324     uint16_t                        sampling_frequency;
325     avdtp_channel_mode_t            channel_mode;
326     uint8_t                         block_length;
327     uint8_t                         subbands;
328     avdtp_sbc_allocation_method_t   allocation_method;
329     uint8_t                         min_bitpool_value;
330     uint8_t                         max_bitpool_value;
331 } avdtp_configuration_sbc_t;
332 
333 typedef struct {
334     avdtp_mpeg_layer_t      layer;
335     uint8_t                 crc;
336     avdtp_channel_mode_t    channel_mode;
337     uint8_t                 media_payload_format;
338     uint16_t                sampling_frequency;
339     uint8_t                 vbr;
340     uint8_t                 bit_rate_index;
341 } avdtp_configuration_mpeg_audio_t;
342 
343 typedef struct {
344     avdtp_aac_object_type_t object_type;
345     uint32_t                sampling_frequency;
346     uint8_t                 channels;
347     uint32_t                bit_rate;
348     uint8_t                 vbr;
349     bool                    drc;
350 } avdtp_configuration_mpeg_aac_t;
351 
352 typedef struct {
353     avdtp_usac_object_type_t object_type;
354     uint32_t                sampling_frequency;
355     uint8_t                 channels;
356     uint32_t                bit_rate;
357     uint8_t                 vbr;
358 } avdtp_configuration_mpegd_usac_t;
359 
360 typedef struct {
361     avdtp_atrac_version_t   version;
362     avdtp_channel_mode_t    channel_mode;
363     uint16_t                sampling_frequency;
364     uint8_t                 vbr;
365     uint8_t                 bit_rate_index;
366     uint16_t                maximum_sul;
367 } avdtp_configuration_atrac_t;
368 
369 
370 
371 typedef struct {
372     uint8_t version;
373     uint8_t padding;
374     uint8_t extension;
375     uint8_t csrc_count;
376     uint8_t marker;
377     uint8_t payload_type;
378 
379     uint16_t sequence_number;
380     uint32_t timestamp;
381     uint32_t synchronization_source;
382 
383     uint32_t csrc_list[AVDTP_MAX_CSRC_NUM];
384 } avdtp_media_packet_header_t;
385 
386 typedef enum {
387     AVDTP_BASIC_SERVICE_MODE = 0,
388     AVDTP_MULTIPLEXING_SERVICE_MODE
389 } avdtp_service_mode_t;
390 
391 typedef enum {
392     AVDTP_STREAM_ENDPOINT_IDLE = 0,
393     AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE,
394     AVDTP_STREAM_ENDPOINT_CONFIGURED,
395 
396     AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM,
397     AVDTP_STREAM_ENDPOINT_W4_ACCEPT_OPEN_STREAM,
398     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED,
399 
400     AVDTP_STREAM_ENDPOINT_OPENED,
401     AVDTP_STREAM_ENDPOINT_STREAMING,
402 
403     AVDTP_STREAM_ENDPOINT_CLOSING,
404     AVDTP_STREAM_ENDPOINT_ABORTING,
405     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED
406 } avdtp_stream_endpoint_state_t;
407 
408 typedef enum {
409     AVDTP_INITIATOR_STREAM_CONFIG_IDLE = 0,
410     AVDTP_INITIATOR_W2_SET_CONFIGURATION,
411     AVDTP_INITIATOR_W2_SUSPEND_STREAM_WITH_SEID,
412     AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID,
413 
414     AVDTP_INITIATOR_W2_OPEN_STREAM,
415 
416     AVDTP_INITIATOR_W2_STREAMING_ABORT,
417     AVDTP_INITIATOR_FRAGMENTATED_COMMAND,
418     AVDTP_INITIATOR_W4_ANSWER
419 } avdtp_initiator_stream_endpoint_state_t;
420 
421 typedef enum {
422     AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE = 0,
423     AVDTP_ACCEPTOR_W2_ACCEPT_GET_CAPABILITIES,
424     AVDTP_ACCEPTOR_W2_ACCEPT_GET_ALL_CAPABILITIES,
425     AVDTP_ACCEPTOR_W2_ACCEPT_DELAY_REPORT,
426     AVDTP_ACCEPTOR_W2_ACCEPT_SET_CONFIGURATION,
427     AVDTP_ACCEPTOR_W2_ACCEPT_RECONFIGURE,
428     AVDTP_ACCEPTOR_W2_ACCEPT_GET_CONFIGURATION,
429     AVDTP_ACCEPTOR_W2_ACCEPT_OPEN_STREAM,
430 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
431     AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM,
432     AVDTP_ACCEPTOR_W2_REJECT_START_STREAM,
433 #endif
434     AVDTP_ACCEPTOR_W2_ACCEPT_START_STREAM,
435     AVDTP_ACCEPTOR_W2_ACCEPT_CLOSE_STREAM,
436     AVDTP_ACCEPTOR_W2_ACCEPT_ABORT_STREAM,
437     AVDTP_ACCEPTOR_W2_ACCEPT_SUSPEND_STREAM,
438     AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
439     AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
440     AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD
441 } avdtp_acceptor_stream_endpoint_state_t;
442 
443 typedef struct {
444     uint8_t seid;           // 0x01 – 0x3E, 6bit
445     uint8_t in_use;         // 1 bit, 0 - not in use, 1 - in use
446     avdtp_media_type_t media_type;     // 4 bit
447     avdtp_sep_type_t   type;       // 1 bit, 0 - SRC, 1 - SNK
448 
449     uint16_t registered_service_categories;
450     avdtp_capabilities_t capabilities;
451 
452     uint16_t configured_service_categories;
453     avdtp_capabilities_t configuration;
454 } avdtp_sep_t;
455 
456 
457 typedef enum {
458     AVDTP_SIGNALING_CONNECTION_IDLE = 0,
459     AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SINK,
460     AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE,
461     AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SOURCE,
462     AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE,
463     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED,
464     AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY,
465     AVDTP_SIGNALING_CONNECTION_OPENED,
466     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED
467 } avdtp_connection_state_t;
468 
469 typedef enum {
470     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE = 0,
471     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS,
472     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
473     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
474     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE
475 } avdtp_acceptor_connection_state_t;
476 
477 typedef enum {
478     AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE = 0,
479     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS,
480     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES_FROM_REMOTE_SINK,
481     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES_FROM_REMOTE_SOURCE,
482     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_SDP_QUERY_COMPLETE_THEN_GET_ALL_CAPABILITIES,
483     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES,
484     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES,
485     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION,
486     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_DELAY_REPORT,
487     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER
488 } avdtp_initiator_connection_state_t;
489 
490 typedef struct {
491     uint8_t  command[200];
492     uint16_t size;
493     uint16_t offset;
494     uint8_t  acp_seid;
495     uint8_t  int_seid;
496     uint8_t transaction_label;
497     uint16_t num_packets;
498     avdtp_signal_identifier_t   signal_identifier;
499     avdtp_message_type_t        message_type;
500     avdtp_packet_type_t         packet_type;
501 } avdtp_signaling_packet_t;
502 
503 typedef enum {
504     AVDTP_CONFIGURATION_STATE_IDLE = 0,
505     AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED,
506     AVDTP_CONFIGURATION_STATE_LOCAL_CONFIGURED,
507     AVDTP_CONFIGURATION_STATE_REMOTE_INITIATED,
508     AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED,
509 } avtdp_configuration_state_t;
510 
511 typedef enum {
512     A2DP_IDLE = 0,
513     A2DP_W4_CONNECTED,
514     A2DP_CONNECTED,
515     A2DP_DISCOVER_SEPS,
516     A2DP_GET_CAPABILITIES,
517     A2DP_W2_GET_ALL_CAPABILITIES, //5
518     A2DP_DISCOVERY_DONE,
519     A2DP_SET_CONFIGURATION,
520     A2DP_W4_GET_CONFIGURATION,
521     A2DP_W4_SET_CONFIGURATION,
522     A2DP_CONFIGURED,
523     A2DP_W2_SUSPEND_STREAM_WITH_SEID, //10
524     A2DP_W2_RECONFIGURE_WITH_SEID,
525     A2DP_W2_OPEN_STREAM_WITH_SEID,
526     A2DP_W4_OPEN_STREAM_WITH_SEID,
527     A2DP_W2_START_STREAM_WITH_SEID,
528     A2DP_W2_ABORT_STREAM_WITH_SEID,   //15
529     A2DP_W2_STOP_STREAM_WITH_SEID,
530     A2DP_STREAMING_OPENED
531 } a2dp_state_t;
532 
533 typedef struct {
534     bool         discover_seps;
535     bool         outgoing_active;
536     bool         have_config;
537     bool         stream_endpoint_configured;
538     a2dp_state_t state;
539     struct avdtp_stream_endpoint * local_stream_endpoint;
540 } a2dp_config_process_t;
541 
542 typedef struct {
543     btstack_linked_item_t    item;
544     bd_addr_t remote_addr;
545 
546     uint16_t avdtp_cid;
547     hci_con_handle_t con_handle;
548 
549     // SDP results
550     uint16_t avdtp_l2cap_psm;
551     uint16_t avdtp_version;
552     bool     sink_supported;
553     bool     source_supported;
554 
555     uint16_t l2cap_signaling_cid;
556     uint16_t l2cap_mtu;
557 
558     avdtp_connection_state_t state;
559     avdtp_acceptor_connection_state_t  acceptor_connection_state;
560     avdtp_initiator_connection_state_t initiator_connection_state;
561 
562     // used to reassemble fragmented commands
563     avdtp_signaling_packet_t acceptor_signaling_packet;
564 
565     // used to prepare outgoing signaling packets
566     avdtp_signaling_packet_t initiator_signaling_packet;
567 
568     uint8_t initiator_local_seid;
569     uint8_t initiator_remote_seid;
570 
571     uint8_t acceptor_local_seid;
572 
573     uint16_t delay_ms;
574 
575     // for repeating the set_configuration
576     void * active_stream_endpoint;
577 
578     uint8_t initiator_transaction_label;
579     uint8_t acceptor_transaction_label;
580     bool    wait_to_send_acceptor;
581 	bool    wait_to_send_initiator;
582 
583     uint8_t suspended_seids[AVDTP_MAX_NUM_SEPS];
584     uint8_t num_suspended_seids;
585 
586     uint8_t reject_service_category;
587     avdtp_signal_identifier_t reject_signal_identifier;
588     uint8_t error_code;
589 
590     // configuration state machine
591     avtdp_configuration_state_t configuration_state;
592 
593     bool incoming_declined;
594     btstack_timer_source_t retry_timer;
595 
596     // A2DP SOURCE
597     a2dp_config_process_t a2dp_source_config_process;
598 
599     // A2DP SINK
600     a2dp_config_process_t a2dp_sink_config_process;
601 
602 } avdtp_connection_t;
603 
604 
605 typedef struct avdtp_stream_endpoint {
606     btstack_linked_item_t    item;
607 
608     // original capabilities configured via avdtp_register_x_category
609     avdtp_sep_t sep;
610 
611     // media codec configuration - provided by user
612     uint16_t  media_codec_configuration_len;
613     uint8_t * media_codec_configuration_info;
614 
615     avdtp_sep_t remote_sep;
616     hci_con_handle_t media_con_handle;
617     uint16_t l2cap_media_cid;
618     uint16_t l2cap_reporting_cid;
619     uint16_t l2cap_recovery_cid;
620 
621     avdtp_stream_endpoint_state_t  state;
622     avdtp_acceptor_stream_endpoint_state_t  acceptor_config_state;
623     avdtp_initiator_stream_endpoint_state_t initiator_config_state;
624     a2dp_state_t a2dp_state;
625     // active connection
626     avdtp_connection_t * connection;
627 
628     // currently active remote seid
629     avdtp_capabilities_t remote_capabilities;
630     uint16_t remote_capabilities_bitmap;
631 
632     uint16_t remote_configuration_bitmap;
633     avdtp_capabilities_t remote_configuration;
634 
635     // temporary codec config used by A2DP Source
636     uint8_t set_config_remote_seid;
637     avdtp_media_codec_type_t media_codec_type;
638     uint8_t media_codec_info[8];
639 
640     // preferred SBC codec settings
641     uint32_t preferred_sampling_frequency;
642     uint8_t  preferred_channel_mode;
643 
644     // register request for media L2cap connection release
645     uint8_t media_disconnect;
646     uint8_t media_connect;
647     uint8_t start_stream;
648     uint8_t close_stream;
649     bool  request_can_send_now;
650     uint8_t abort_stream;
651     uint8_t suspend_stream;
652     uint16_t sequence_number;
653 } avdtp_stream_endpoint_t;
654 
655 void avdtp_init(void);
656 void avdtp_deinit(void);
657 
658 avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr);
659 avdtp_connection_t * avdtp_get_connection_for_avdtp_cid(uint16_t avdtp_cid);
660 avdtp_connection_t * avdtp_get_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid);
661 btstack_linked_list_t * avdtp_get_connections(void);
662 btstack_linked_list_t * avdtp_get_stream_endpoints(void);
663 
664 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_seid(uint16_t seid);
665 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec(avdtp_media_codec_type_t codec_type);
666 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec_other(uint32_t vendor_id, uint16_t codec_id);
667 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec_and_type(avdtp_media_codec_type_t codec_type, avdtp_sep_type_t sep_type);
668 
669 btstack_packet_handler_t avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t *stream_endpoint);
670 void avdtp_emit_sink_and_source(uint8_t * packet, uint16_t size);
671 void avdtp_emit_source(uint8_t * packet, uint16_t size);
672 
673 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint);
674 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint);
675 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint);
676 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets);
677 void avdtp_register_content_protection_category(avdtp_stream_endpoint_t * stream_endpoint, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len);
678 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery);
679 void avdtp_register_media_codec_category(avdtp_stream_endpoint_t * stream_endpoint, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, const uint8_t *media_codec_info, uint16_t media_codec_info_len);
680 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation);
681 
682 // sink only
683 void avdtp_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size));
684 
685 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
686 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type);
687 void avdtp_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint);
688 
689 uint8_t avdtp_connect(bd_addr_t remote, avdtp_role_t role, uint16_t * avdtp_cid);
690 uint8_t avdtp_disconnect(uint16_t avdtp_cid);
691 void    avdtp_register_sink_packet_handler(btstack_packet_handler_t callback);
692 void    avdtp_register_source_packet_handler(btstack_packet_handler_t callback);
693 
694 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid);
695 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid);
696 uint8_t avdtp_stop_stream (uint16_t avdtp_cid, uint8_t local_seid);
697 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid);
698 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid);
699 
700 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid);
701 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid);
702 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_role_t role);
703 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid);
704 uint8_t avdtp_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration);
705 uint8_t avdtp_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration);
706 uint8_t avdtp_validate_media_configuration(const avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid,
707                                            uint8_t reconfigure, const adtvp_media_codec_capabilities_t *media_codec);
708 
709 // frequency will be used by avdtp_choose_sbc_sampling_frequency (if supported by both endpoints)
710 void    avdtp_set_preferred_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency);
711 
712 // channels_num will be used by avdtp_choose_sbc_channel_mode (if supported by both endpoints)
713 void    avdtp_set_preferred_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t channel_mode);
714 
715 void    avdtp_set_preferred_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency);
716 
717 /**
718  * @brief Get highest sampling frequency
719  * @param sampling_frequency_bitmap
720  * @return highest frequency or 0
721  */
722 uint16_t avdtp_get_highest_sampling_frequency(uint8_t sampling_frequency_bitmap);
723 
724 avdtp_channel_mode_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap);
725 avdtp_sbc_allocation_method_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap);
726 uint16_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap);
727 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap);
728 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap);
729 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value);
730 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value);
731 
732 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint);
733 
734 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint);
735 
736 uint8_t avdtp_get_next_transaction_label(void);
737 
738 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
739 uint8_t avdtp_start_stream_accept(uint16_t avdtp_cid, uint8_t local_seid);
740 uint8_t avdtp_start_stream_reject(uint16_t avdtp_cid, uint8_t local_seid);
741 #endif
742 
743 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
744 void avdtp_init_fuzz(void);
745 void avdtp_packet_handler_fuzz(uint8_t *packet, uint16_t size);
746 #endif
747 
748 #if defined __cplusplus
749 }
750 #endif
751 
752 #endif // AVDTP_H
753