xref: /btstack/src/classic/avdtp.h (revision f8c3a13077a8b5dbbfd2515cb0d524d29e7e0443)
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_INVALID_SUBBANDS                   = 0xC7,
113     CODEC_SPECIFIC_ERROR_CODE_INVALID_ALLOCATION_METHOD          = 0xC9,
114     CODEC_SPECIFIC_ERROR_CODE_INVALID_MINIMUM_BITPOOL_VALUE      = 0xCB,
115     CODEC_SPECIFIC_ERROR_CODE_INVALID_MAXIMUM_BITPOOL_VALUE      = 0xCD,
116     CODEC_SPECIFIC_ERROR_CODE_INVALID_OBJECT_TYPE                = 0xD6,
117     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_OBJECT_TYPE          = 0xD7,
118     CODEC_SPECIFIC_ERROR_CODE_INVALID_CHANNELS                   = 0xD8,
119     CODEC_SPECIFIC_ERROR_CODE_NOT_SUPPORTED_CHANNELS             = 0xD9,
120     CODEC_SPECIFIC_ERROR_CODE_INVALID_BLOCK_LENGTH               = 0xDD,
121     CODEC_SPECIFIC_ERROR_CODE_INVALID_DRC                        = 0xE4,
122 } codec_specific_error_code_t;
123 
124 // Signal Identifier fields
125 typedef enum {
126     AVDTP_SI_NONE = 0x00,
127     AVDTP_SI_DISCOVER = 0x01,
128     AVDTP_SI_GET_CAPABILITIES,
129     AVDTP_SI_SET_CONFIGURATION,
130     AVDTP_SI_GET_CONFIGURATION,
131     AVDTP_SI_RECONFIGURE, //5
132     AVDTP_SI_OPEN,  //6
133     AVDTP_SI_START, //7
134     AVDTP_SI_CLOSE,
135     AVDTP_SI_SUSPEND,
136     AVDTP_SI_ABORT, //10
137     AVDTP_SI_SECURITY_CONTROL,
138     AVDTP_SI_GET_ALL_CAPABILITIES, //12
139     AVDTP_SI_DELAYREPORT,
140 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
141     AVDTP_SI_ACCEPT_START
142 #endif
143 } avdtp_signal_identifier_t;
144 
145 typedef enum {
146     AVDTP_SINGLE_PACKET = 0,
147     AVDTP_START_PACKET    ,
148     AVDTP_CONTINUE_PACKET ,
149     AVDTP_END_PACKET
150 } avdtp_packet_type_t;
151 
152 typedef enum {
153     AVDTP_CMD_MSG = 0,
154     AVDTP_GENERAL_REJECT_MSG   ,
155     AVDTP_RESPONSE_ACCEPT_MSG ,
156     AVDTP_RESPONSE_REJECT_MSG
157 } avdtp_message_type_t;
158 
159 typedef enum {
160     AVDTP_AUDIO = 0,
161     AVDTP_VIDEO,
162     AVDTP_MULTIMEDIA
163 } avdtp_media_type_t;
164 
165 typedef enum {
166     AVDTP_CODEC_SBC             = 0x00,
167     AVDTP_CODEC_MPEG_1_2_AUDIO  = 0x01,
168     AVDTP_CODEC_MPEG_2_4_AAC    = 0x02,
169     AVDTP_CODEC_ATRAC_FAMILY    = 0x04,
170     AVDTP_CODEC_NON_A2DP        = 0xFF
171 } avdtp_media_codec_type_t;
172 
173 typedef enum {
174     AVDTP_CONTENT_PROTECTION_DTCP = 0x0001,
175     AVDTP_CONTENT_PROTECTION_SCMS_T = 0x0002
176 } avdtp_content_protection_type_t;
177 
178 typedef enum {
179     AVDTP_SOURCE = 0,
180     AVDTP_SINK
181 } avdtp_sep_type_t;
182 
183 typedef enum {
184     AVDTP_ROLE_SOURCE = 0,
185     AVDTP_ROLE_SINK
186 } avdtp_role_t;
187 
188 typedef enum {
189     AVDTP_SERVICE_CATEGORY_INVALID_0 = 0x00,
190     AVDTP_MEDIA_TRANSPORT = 0X01,
191     AVDTP_REPORTING,
192     AVDTP_RECOVERY,
193     AVDTP_CONTENT_PROTECTION, //4
194     AVDTP_HEADER_COMPRESSION, //5
195     AVDTP_MULTIPLEXING,       //6
196     AVDTP_MEDIA_CODEC,        //7
197     AVDTP_DELAY_REPORTING,    //8
198     AVDTP_SERVICE_CATEGORY_INVALID_FF = 0xFF
199 } avdtp_service_category_t;
200 
201 typedef struct {
202     uint8_t recovery_type;                  // 0x01 = RFC2733
203     uint8_t maximum_recovery_window_size;   // 0x01 to 0x18, for a Transport Packet
204     uint8_t maximum_number_media_packets;   // 0x01 to 0x18, The maximum number of media packets a specific parity code covers
205 } avdtp_recovery_capabilities_t;
206 
207 typedef struct {
208     avdtp_media_type_t       media_type;
209     avdtp_media_codec_type_t media_codec_type;
210     uint16_t  media_codec_information_len;
211     uint8_t * media_codec_information;
212 } adtvp_media_codec_capabilities_t;
213 
214 
215 typedef struct {
216     uint16_t cp_type;
217     uint16_t cp_type_value_len;
218     uint8_t cp_type_value[AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN];
219 } adtvp_content_protection_t;
220 
221 typedef struct{
222     uint8_t back_ch;  // byte0 - bit 8; 0=Not Available/Not Used; 1=Available/In Use
223     uint8_t media;    // byte0 - bit 7
224     uint8_t recovery; // byte0 - bit 6
225 } avdtp_header_compression_capabilities_t;
226 
227 typedef struct{
228     uint8_t fragmentation; // byte0 - bit 8, Allow Adaptation Layer Fragmentation, 0 no, 1 yes
229     // Request/indicate value of the Transport Session Identifier for a media, reporting, or recovery transport sessions, respectively
230     uint8_t transport_identifiers_num;
231     uint8_t transport_session_identifiers[3];   // byte1, upper 5bits, 0x01 to 0x1E
232     // Request/indicate value for TCID for a media, reporting, or transport session
233     uint8_t tcid[3];         // byte2 0x01 to 0x1E
234 } avdtp_multiplexing_mode_capabilities_t;
235 
236 typedef struct{
237     avdtp_recovery_capabilities_t recovery;
238     adtvp_media_codec_capabilities_t media_codec;
239     adtvp_content_protection_t content_protection;
240     avdtp_header_compression_capabilities_t header_compression;
241     avdtp_multiplexing_mode_capabilities_t multiplexing_mode;
242 } avdtp_capabilities_t;
243 
244 typedef enum{
245     AVDTP_SBC_48000 = 1,
246     AVDTP_SBC_44100 = 2,
247     AVDTP_SBC_32000 = 4,
248     AVDTP_SBC_16000 = 8
249 } avdtp_sbc_sampling_frequency_t;
250 
251 typedef enum{
252     AVDTP_SBC_JOINT_STEREO  = 1,
253     AVDTP_SBC_STEREO        = 2,
254     AVDTP_SBC_DUAL_CHANNEL  = 4,
255     AVDTP_SBC_MONO          = 8
256 } avdtp_sbc_channel_mode_t;
257 
258 typedef enum{
259     AVDTP_SBC_BLOCK_LENGTH_16 = 1,
260     AVDTP_SBC_BLOCK_LENGTH_12 = 2,
261     AVDTP_SBC_BLOCK_LENGTH_8  = 4,
262     AVDTP_SBC_BLOCK_LENGTH_4  = 8
263 } avdtp_sbc_block_length_t;
264 
265 typedef enum{
266     AVDTP_SBC_SUBBANDS_8 = 1,
267     AVDTP_SBC_SUBBANDS_4 = 2
268 } avdtp_sbc_subbands_t;
269 
270 typedef enum{
271     AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS = 1,
272     AVDTP_SBC_ALLOCATION_METHOD_SNR      = 2
273 } avdtp_sbc_allocation_method_t;
274 
275 typedef struct {
276     uint8_t fragmentation;
277     uint8_t starting_packet; // of fragmented SBC frame
278     uint8_t last_packet;     // of fragmented SBC frame
279     uint8_t num_frames;
280 } avdtp_sbc_codec_header_t;
281 
282 typedef enum {
283     AVDTP_MPEG_LAYER_1 = 1,
284     AVDTP_MPEG_LAYER_2,
285     AVDTP_MPEG_LAYER_3,
286 } avdtp_mpeg_layer_t;
287 
288 
289 typedef enum {
290     AVDTP_AAC_MPEG2_LC = 1,
291     AVDTP_AAC_MPEG4_LC,
292     AVDTP_AAC_MPEG4_LTP,
293     AVDTP_AAC_MPEG4_SCALABLE
294 } avdtp_aac_object_type_t;
295 
296 typedef enum {
297     AVDTP_ATRAC_VERSION_1 = 1,
298     AVDTP_ATRAC_VERSION_2,
299     AVDTP_ATRAC_VERSION_3
300 } avdtp_atrac_version_t;
301 
302 // used for MPEG1/2 Audio, ATRAC (no stereo mode)
303 typedef enum {
304     AVDTP_CHANNEL_MODE_MONO = 1,
305     AVDTP_CHANNEL_MODE_DUAL_CHANNEL,
306     AVDTP_CHANNEL_MODE_STEREO,
307     AVDTP_CHANNEL_MODE_JOINT_STEREO,
308 } avdtp_channel_mode_t;
309 
310 typedef struct {
311     uint16_t                        sampling_frequency;
312     avdtp_channel_mode_t            channel_mode;
313     uint8_t                         block_length;
314     uint8_t                         subbands;
315     avdtp_sbc_allocation_method_t   allocation_method;
316     uint8_t                         min_bitpool_value;
317     uint8_t                         max_bitpool_value;
318 } avdtp_configuration_sbc_t;
319 
320 typedef struct {
321     avdtp_mpeg_layer_t      layer;
322     uint8_t                 crc;
323     avdtp_channel_mode_t    channel_mode;
324     uint8_t                 media_payload_format;
325     uint16_t                sampling_frequency;
326     uint8_t                 vbr;
327     uint8_t                 bit_rate_index;
328 } avdtp_configuration_mpeg_audio_t;
329 
330 typedef struct {
331     avdtp_aac_object_type_t object_type;
332     uint32_t                sampling_frequency;
333     uint8_t                 channels;
334     uint32_t                bit_rate;
335     uint8_t                 vbr;
336 } avdtp_configuration_mpeg_aac_t;
337 
338 typedef struct {
339     avdtp_atrac_version_t   version;
340     avdtp_channel_mode_t    channel_mode;
341     uint16_t                sampling_frequency;
342     uint8_t                 vbr;
343     uint8_t                 bit_rate_index;
344     uint16_t                maximum_sul;
345 } avdtp_configuration_atrac_t;
346 
347 
348 
349 typedef struct {
350     uint8_t version;
351     uint8_t padding;
352     uint8_t extension;
353     uint8_t csrc_count;
354     uint8_t marker;
355     uint8_t payload_type;
356 
357     uint16_t sequence_number;
358     uint32_t timestamp;
359     uint32_t synchronization_source;
360 
361     uint32_t csrc_list[AVDTP_MAX_CSRC_NUM];
362 } avdtp_media_packet_header_t;
363 
364 typedef enum {
365     AVDTP_BASIC_SERVICE_MODE = 0,
366     AVDTP_MULTIPLEXING_SERVICE_MODE
367 } avdtp_service_mode_t;
368 
369 typedef enum {
370     AVDTP_STREAM_ENDPOINT_IDLE = 0,
371     AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE,
372     AVDTP_STREAM_ENDPOINT_CONFIGURED,
373 
374     AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM,
375     AVDTP_STREAM_ENDPOINT_W4_ACCEPT_OPEN_STREAM,
376     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED,
377 
378     AVDTP_STREAM_ENDPOINT_OPENED,
379     AVDTP_STREAM_ENDPOINT_STREAMING,
380 
381     AVDTP_STREAM_ENDPOINT_CLOSING,
382     AVDTP_STREAM_ENDPOINT_ABORTING,
383     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED
384 } avdtp_stream_endpoint_state_t;
385 
386 typedef enum {
387     AVDTP_INITIATOR_STREAM_CONFIG_IDLE = 0,
388     AVDTP_INITIATOR_W2_SET_CONFIGURATION,
389     AVDTP_INITIATOR_W2_SUSPEND_STREAM_WITH_SEID,
390     AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID,
391 
392     AVDTP_INITIATOR_W2_OPEN_STREAM,
393 
394     AVDTP_INITIATOR_W2_STREAMING_ABORT,
395     AVDTP_INITIATOR_FRAGMENTATED_COMMAND,
396     AVDTP_INITIATOR_W4_ANSWER
397 } avdtp_initiator_stream_endpoint_state_t;
398 
399 typedef enum {
400     AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE = 0,
401     AVDTP_ACCEPTOR_W2_ACCEPT_GET_CAPABILITIES,
402     AVDTP_ACCEPTOR_W2_ACCEPT_GET_ALL_CAPABILITIES,
403     AVDTP_ACCEPTOR_W2_ACCEPT_DELAY_REPORT,
404     AVDTP_ACCEPTOR_W2_ACCEPT_SET_CONFIGURATION,
405     AVDTP_ACCEPTOR_W2_ACCEPT_RECONFIGURE,
406     AVDTP_ACCEPTOR_W2_ACCEPT_GET_CONFIGURATION,
407     AVDTP_ACCEPTOR_W2_ACCEPT_OPEN_STREAM,
408 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
409     AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM,
410     AVDTP_ACCEPTOR_W2_REJECT_START_STREAM,
411 #endif
412     AVDTP_ACCEPTOR_W2_ACCEPT_START_STREAM,
413     AVDTP_ACCEPTOR_W2_ACCEPT_CLOSE_STREAM,
414     AVDTP_ACCEPTOR_W2_ACCEPT_ABORT_STREAM,
415     AVDTP_ACCEPTOR_W2_ACCEPT_SUSPEND_STREAM,
416     AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
417     AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
418     AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD
419 } avdtp_acceptor_stream_endpoint_state_t;
420 
421 typedef struct {
422     uint8_t seid;           // 0x01 – 0x3E, 6bit
423     uint8_t in_use;         // 1 bit, 0 - not in use, 1 - in use
424     avdtp_media_type_t media_type;     // 4 bit
425     avdtp_sep_type_t   type;       // 1 bit, 0 - SRC, 1 - SNK
426 
427     uint16_t registered_service_categories;
428     avdtp_capabilities_t capabilities;
429 
430     uint16_t configured_service_categories;
431     avdtp_capabilities_t configuration;
432 } avdtp_sep_t;
433 
434 
435 typedef enum {
436     AVDTP_SIGNALING_CONNECTION_IDLE = 0,
437     AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SINK,
438     AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE,
439     AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SOURCE,
440     AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE,
441     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED,
442     AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY,
443     AVDTP_SIGNALING_CONNECTION_OPENED,
444     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED
445 } avdtp_connection_state_t;
446 
447 typedef enum {
448     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE = 0,
449     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS,
450     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
451     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
452     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE
453 } avdtp_acceptor_connection_state_t;
454 
455 typedef enum {
456     AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE = 0,
457     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS,
458     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES_FROM_REMOTE_SINK,
459     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES_FROM_REMOTE_SOURCE,
460     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_SDP_QUERY_COMPLETE_THEN_GET_ALL_CAPABILITIES,
461     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES,
462     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES,
463     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION,
464     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_DELAY_REPORT,
465     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER
466 } avdtp_initiator_connection_state_t;
467 
468 typedef struct {
469     uint8_t  command[200];
470     uint16_t size;
471     uint16_t offset;
472     uint8_t  acp_seid;
473     uint8_t  int_seid;
474     uint8_t transaction_label;
475     uint16_t num_packets;
476     avdtp_signal_identifier_t   signal_identifier;
477     avdtp_message_type_t        message_type;
478     avdtp_packet_type_t         packet_type;
479 } avdtp_signaling_packet_t;
480 
481 typedef enum {
482     AVDTP_CONFIGURATION_STATE_IDLE = 0,
483     AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED,
484     AVDTP_CONFIGURATION_STATE_LOCAL_CONFIGURED,
485     AVDTP_CONFIGURATION_STATE_REMOTE_INITIATED,
486     AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED,
487 } avtdp_configuration_state_t;
488 
489 typedef enum {
490     A2DP_IDLE = 0,
491     A2DP_W4_CONNECTED,
492     A2DP_CONNECTED,
493     A2DP_DISCOVER_SEPS,
494     A2DP_GET_CAPABILITIES,
495     A2DP_W2_GET_ALL_CAPABILITIES, //5
496     A2DP_DISCOVERY_DONE,
497     A2DP_SET_CONFIGURATION,
498     A2DP_W4_GET_CONFIGURATION,
499     A2DP_W4_SET_CONFIGURATION,
500     A2DP_CONFIGURED,
501     A2DP_W2_SUSPEND_STREAM_WITH_SEID, //10
502     A2DP_W2_RECONFIGURE_WITH_SEID,
503     A2DP_W2_OPEN_STREAM_WITH_SEID,
504     A2DP_W4_OPEN_STREAM_WITH_SEID,
505     A2DP_W2_START_STREAM_WITH_SEID,
506     A2DP_W2_ABORT_STREAM_WITH_SEID,   //15
507     A2DP_W2_STOP_STREAM_WITH_SEID,
508     A2DP_STREAMING_OPENED
509 } a2dp_state_t;
510 
511 typedef struct {
512     bool         discover_seps;
513     bool         outgoing_active;
514     bool         have_config;
515     bool         stream_endpoint_configured;
516     a2dp_state_t state;
517     struct avdtp_stream_endpoint * local_stream_endpoint;
518 } a2dp_config_process_t;
519 
520 typedef struct {
521     btstack_linked_item_t    item;
522     bd_addr_t remote_addr;
523 
524     uint16_t avdtp_cid;
525     hci_con_handle_t con_handle;
526 
527     // SDP results
528     uint16_t avdtp_l2cap_psm;
529     uint16_t avdtp_version;
530     bool     sink_supported;
531     bool     source_supported;
532 
533     uint16_t l2cap_signaling_cid;
534     uint16_t l2cap_mtu;
535 
536     avdtp_connection_state_t state;
537     avdtp_acceptor_connection_state_t  acceptor_connection_state;
538     avdtp_initiator_connection_state_t initiator_connection_state;
539 
540     // used to reassemble fragmented commands
541     avdtp_signaling_packet_t acceptor_signaling_packet;
542 
543     // used to prepare outgoing signaling packets
544     avdtp_signaling_packet_t initiator_signaling_packet;
545 
546     uint8_t initiator_local_seid;
547     uint8_t initiator_remote_seid;
548 
549     uint8_t acceptor_local_seid;
550 
551     uint16_t delay_ms;
552 
553     // for repeating the set_configuration
554     void * active_stream_endpoint;
555 
556     uint8_t initiator_transaction_label;
557     uint8_t acceptor_transaction_label;
558     bool    wait_to_send_acceptor;
559 	bool    wait_to_send_initiator;
560 
561     uint8_t suspended_seids[AVDTP_MAX_NUM_SEPS];
562     uint8_t num_suspended_seids;
563 
564     uint8_t reject_service_category;
565     avdtp_signal_identifier_t reject_signal_identifier;
566     uint8_t error_code;
567 
568     // configuration state machine
569     avtdp_configuration_state_t configuration_state;
570 
571     bool incoming_declined;
572     btstack_timer_source_t retry_timer;
573 
574     // A2DP SOURCE
575     a2dp_config_process_t a2dp_source_config_process;
576 
577     // A2DP SINK
578     a2dp_config_process_t a2dp_sink_config_process;
579 
580 } avdtp_connection_t;
581 
582 
583 typedef struct avdtp_stream_endpoint {
584     btstack_linked_item_t    item;
585 
586     // original capabilities configured via avdtp_register_x_category
587     avdtp_sep_t sep;
588 
589     // media codec configuration - provided by user
590     uint16_t  media_codec_configuration_len;
591     uint8_t * media_codec_configuration_info;
592 
593     avdtp_sep_t remote_sep;
594     hci_con_handle_t media_con_handle;
595     uint16_t l2cap_media_cid;
596     uint16_t l2cap_reporting_cid;
597     uint16_t l2cap_recovery_cid;
598 
599     avdtp_stream_endpoint_state_t  state;
600     avdtp_acceptor_stream_endpoint_state_t  acceptor_config_state;
601     avdtp_initiator_stream_endpoint_state_t initiator_config_state;
602     a2dp_state_t a2dp_state;
603     // active connection
604     avdtp_connection_t * connection;
605 
606     // currently active remote seid
607     avdtp_capabilities_t remote_capabilities;
608     uint16_t remote_capabilities_bitmap;
609 
610     uint16_t remote_configuration_bitmap;
611     avdtp_capabilities_t remote_configuration;
612 
613     // temporary codec config used by A2DP Source
614     uint8_t set_config_remote_seid;
615     avdtp_media_codec_type_t media_codec_type;
616     uint8_t media_codec_info[8];
617 
618     // preferred SBC codec settings
619     uint32_t preferred_sampling_frequency;
620     uint8_t  preferred_channel_mode;
621 
622     // register request for media L2cap connection release
623     uint8_t media_disconnect;
624     uint8_t media_connect;
625     uint8_t start_stream;
626     uint8_t close_stream;
627     bool  request_can_send_now;
628     uint8_t abort_stream;
629     uint8_t suspend_stream;
630     uint16_t sequence_number;
631 } avdtp_stream_endpoint_t;
632 
633 void avdtp_init(void);
634 void avdtp_deinit(void);
635 
636 avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr);
637 avdtp_connection_t * avdtp_get_connection_for_avdtp_cid(uint16_t avdtp_cid);
638 avdtp_connection_t * avdtp_get_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid);
639 btstack_linked_list_t * avdtp_get_connections(void);
640 btstack_linked_list_t * avdtp_get_stream_endpoints(void);
641 
642 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_seid(uint16_t seid);
643 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec(avdtp_media_codec_type_t codec_type);
644 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec_other(uint32_t vendor_id, uint16_t codec_id);
645 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);
646 
647 btstack_packet_handler_t avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t *stream_endpoint);
648 void avdtp_emit_sink_and_source(uint8_t * packet, uint16_t size);
649 void avdtp_emit_source(uint8_t * packet, uint16_t size);
650 
651 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint);
652 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint);
653 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint);
654 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets);
655 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);
656 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery);
657 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);
658 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation);
659 
660 // sink only
661 void avdtp_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size));
662 
663 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
664 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type);
665 void avdtp_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint);
666 
667 uint8_t avdtp_connect(bd_addr_t remote, avdtp_role_t role, uint16_t * avdtp_cid);
668 uint8_t avdtp_disconnect(uint16_t avdtp_cid);
669 void    avdtp_register_sink_packet_handler(btstack_packet_handler_t callback);
670 void    avdtp_register_source_packet_handler(btstack_packet_handler_t callback);
671 
672 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid);
673 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid);
674 uint8_t avdtp_stop_stream (uint16_t avdtp_cid, uint8_t local_seid);
675 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid);
676 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid);
677 
678 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid);
679 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid);
680 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_role_t role);
681 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid);
682 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);
683 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);
684 uint8_t avdtp_validate_media_configuration(const avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid,
685                                            uint8_t reconfigure, const adtvp_media_codec_capabilities_t *media_codec);
686 
687 // frequency will be used by avdtp_choose_sbc_sampling_frequency (if supported by both endpoints)
688 void    avdtp_set_preferred_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency);
689 
690 // channel_mode will be used by avdtp_choose_sbc_channel_mode (if supported by both endpoints)
691 void    avdtp_set_preferred_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t channel_mode);
692 
693 void    avdtp_set_preferred_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency);
694 
695 /**
696  * @brief Get highest sampling frequency
697  * @param sampling_frequency_bitmap
698  * @return highest frequency or 0
699  */
700 uint16_t avdtp_get_highest_sampling_frequency(uint8_t sampling_frequency_bitmap);
701 
702 avdtp_channel_mode_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap);
703 avdtp_sbc_allocation_method_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap);
704 uint16_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap);
705 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap);
706 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap);
707 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value);
708 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value);
709 
710 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint);
711 
712 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint);
713 
714 uint8_t avdtp_get_next_transaction_label(void);
715 
716 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
717 uint8_t avdtp_start_stream_accept(uint16_t avdtp_cid, uint8_t local_seid);
718 uint8_t avdtp_start_stream_reject(uint16_t avdtp_cid, uint8_t local_seid);
719 #endif
720 
721 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
722 void avdtp_init_fuzz(void);
723 void avdtp_packet_handler_fuzz(uint8_t *packet, uint16_t size);
724 #endif
725 
726 #if defined __cplusplus
727 }
728 #endif
729 
730 #endif // AVDTP_H
731