xref: /btstack/src/classic/avdtp.h (revision 503a627edab6ba8492c3d0cdd9ac598fe2b0f08a)
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 MATTHIAS
24  * RINGWALD 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  * avdtp.h
40  *
41  * Audio/Video Distribution Transport Protocol
42  *
43  * This protocol defines A/V stream negotiation, establishment, and transmission
44  * procedures. Also specified are the message formats that are exchanged between
45  * such devices to transport their A/V streams in A/V distribution applications.
46  *
47  * Media packets are unidirectional, they travel downstream from AVDTP Source to AVDTP Sink.
48  */
49 
50 #ifndef AVDTP_H
51 #define AVDTP_H
52 
53 #include <stdint.h>
54 #include "hci.h"
55 #include "btstack_ring_buffer.h"
56 
57 #if defined __cplusplus
58 extern "C" {
59 #endif
60 
61 #define AVDTP_MAX_NUM_SEPS 10
62 #define AVDTP_MAX_CSRC_NUM 15
63 #define AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN 10
64 
65 // Supported Features
66 #define AVDTP_SOURCE_FEATURE_MASK_PLAYER        0x0001u
67 #define AVDTP_SOURCE_FEATURE_MASK_MICROPHONE    0x0002u
68 #define AVDTP_SOURCE_FEATURE_MASK_TUNER         0x0004u
69 #define AVDTP_SOURCE_FEATURE_MASK_MIXER         0x0008u
70 
71 #define AVDTP_SINK_FEATURE_MASK_HEADPHONE       0x0001u
72 #define AVDTP_SINK_FEATURE_MASK_SPEAKER         0x0002u
73 #define AVDTP_SINK_FEATURE_MASK_RECORDER        0x0004u
74 #define AVDTP_SINK_FEATURE_MASK_AMPLIFIER       0x0008u
75 
76 // ACP to INT, Signal Response Header Error Codes
77 #define AVDTP_ERROR_CODE_BAD_HEADER_FORMAT     0x01
78 
79 // ACP to INT, Signal Response Payload Format Error Codes
80 #define AVDTP_ERROR_CODE_BAD_LENGTH                 0x11
81 #define AVDTP_ERROR_CODE_BAD_ACP_SEID               0x12
82 #define AVDTP_ERROR_CODE_SEP_IN_USE                 0x13
83 #define AVDTP_ERROR_CODE_SEP_NOT_IN_USE             0x14
84 #define AVDTP_ERROR_CODE_BAD_SERV_CATEGORY          0x17
85 #define AVDTP_ERROR_CODE_BAD_PAYLOAD_FORMAT         0x18
86 #define AVDTP_ERROR_CODE_NOT_SUPPORTED_COMMAND      0x19
87 #define AVDTP_ERROR_CODE_INVALID_CAPABILITIES       0x1A
88 
89 // ACP to INT, Signal Response Transport Service Capabilities Error Codes
90 #define AVDTP_ERROR_CODE_BAD_RECOVERY_TYPE          0x22
91 #define AVDTP_ERROR_CODE_BAD_MEDIA_TRANSPORT_FORMAT 0x23
92 #define AVDTP_ERROR_CODE_BAD_RECOVERY_FORMAT        0x25
93 #define AVDTP_ERROR_CODE_BAD_ROHC_FORMAT            0x26
94 #define AVDTP_ERROR_CODE_BAD_CP_FORMAT              0x27
95 #define AVDTP_ERROR_CODE_BAD_MULTIPLEXING_FORMAT    0x28
96 #define AVDTP_ERROR_CODE_UNSUPPORTED_CONFIGURATION  0x29
97 
98 // ACP to INT, Procedure Error Codes
99 #define AVDTP_ERROR_CODE_BAD_STATE                  0x31
100 
101 // Internal Error Codes
102 #define AVDTP_INVALID_SEP_SEID                      0xFF
103 
104 
105 // Signal Identifier fields
106 typedef enum {
107     AVDTP_SI_NONE = 0x00,
108     AVDTP_SI_DISCOVER = 0x01,
109     AVDTP_SI_GET_CAPABILITIES,
110     AVDTP_SI_SET_CONFIGURATION,
111     AVDTP_SI_GET_CONFIGURATION,
112     AVDTP_SI_RECONFIGURE, //5
113     AVDTP_SI_OPEN,  //6
114     AVDTP_SI_START, //7
115     AVDTP_SI_CLOSE,
116     AVDTP_SI_SUSPEND,
117     AVDTP_SI_ABORT, //10
118     AVDTP_SI_SECURITY_CONTROL,
119     AVDTP_SI_GET_ALL_CAPABILITIES, //12
120     AVDTP_SI_DELAYREPORT
121 } avdtp_signal_identifier_t;
122 
123 typedef enum {
124     AVDTP_SINGLE_PACKET = 0,
125     AVDTP_START_PACKET    ,
126     AVDTP_CONTINUE_PACKET ,
127     AVDTP_END_PACKET
128 } avdtp_packet_type_t;
129 
130 typedef enum {
131     AVDTP_CMD_MSG = 0,
132     AVDTP_GENERAL_REJECT_MSG   ,
133     AVDTP_RESPONSE_ACCEPT_MSG ,
134     AVDTP_RESPONSE_REJECT_MSG
135 } avdtp_message_type_t;
136 
137 typedef enum {
138     AVDTP_AUDIO = 0,
139     AVDTP_VIDEO,
140     AVDTP_MULTIMEDIA
141 } avdtp_media_type_t;
142 
143 typedef enum {
144     AVDTP_CODEC_SBC             = 0x00,
145     AVDTP_CODEC_MPEG_1_2_AUDIO  = 0x01,
146     AVDTP_CODEC_MPEG_2_4_AAC    = 0x02,
147     AVDTP_CODEC_ATRAC_FAMILY    = 0x04,
148     AVDTP_CODEC_NON_A2DP        = 0xFF
149 } avdtp_media_codec_type_t;
150 
151 typedef enum {
152     AVDTP_CONTENT_PROTECTION_DTCP = 0x0001,
153     AVDTP_CONTENT_PROTECTION_SCMS_T = 0x0002
154 } avdtp_content_protection_type_t;
155 
156 typedef enum {
157     AVDTP_SOURCE = 0,
158     AVDTP_SINK
159 } avdtp_sep_type_t;
160 
161 typedef enum {
162     AVDTP_ROLE_SOURCE = 0,
163     AVDTP_ROLE_SINK
164 } avdtp_role_t;
165 
166 typedef enum {
167     AVDTP_SERVICE_CATEGORY_INVALID_0 = 0x00,
168     AVDTP_MEDIA_TRANSPORT = 0X01,
169     AVDTP_REPORTING,
170     AVDTP_RECOVERY,
171     AVDTP_CONTENT_PROTECTION, //4
172     AVDTP_HEADER_COMPRESSION, //5
173     AVDTP_MULTIPLEXING,       //6
174     AVDTP_MEDIA_CODEC,        //7
175     AVDTP_DELAY_REPORTING,    //8
176     AVDTP_SERVICE_CATEGORY_INVALID_FF = 0xFF
177 } avdtp_service_category_t;
178 
179 typedef struct {
180     uint8_t recovery_type;                  // 0x01 = RFC2733
181     uint8_t maximum_recovery_window_size;   // 0x01 to 0x18, for a Transport Packet
182     uint8_t maximum_number_media_packets;   // 0x01 to 0x18, The maximum number of media packets a specific parity code covers
183 } avdtp_recovery_capabilities_t;
184 
185 typedef struct {
186     avdtp_media_type_t       media_type;
187     avdtp_media_codec_type_t media_codec_type;
188     uint16_t  media_codec_information_len;
189     uint8_t * media_codec_information;
190 } adtvp_media_codec_capabilities_t;
191 
192 
193 typedef struct {
194     uint16_t cp_type;
195     uint16_t cp_type_value_len;
196     uint8_t cp_type_value[AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN];
197 } adtvp_content_protection_t;
198 
199 typedef struct{
200     uint8_t back_ch;  // byte0 - bit 8; 0=Not Available/Not Used; 1=Available/In Use
201     uint8_t media;    // byte0 - bit 7
202     uint8_t recovery; // byte0 - bit 6
203 } avdtp_header_compression_capabilities_t;
204 
205 typedef struct{
206     uint8_t fragmentation; // byte0 - bit 8, Allow Adaptation Layer Fragmentation, 0 no, 1 yes
207     // Request/indicate value of the Transport Session Identifier for a media, reporting, or recovery transport sessions, respectively
208     uint8_t transport_identifiers_num;
209     uint8_t transport_session_identifiers[3];   // byte1, upper 5bits, 0x01 to 0x1E
210     // Request/indicate value for TCID for a media, reporting, or transport session
211     uint8_t tcid[3];         // byte2 0x01 to 0x1E
212 } avdtp_multiplexing_mode_capabilities_t;
213 
214 typedef struct{
215     avdtp_recovery_capabilities_t recovery;
216     adtvp_media_codec_capabilities_t media_codec;
217     adtvp_content_protection_t content_protection;
218     avdtp_header_compression_capabilities_t header_compression;
219     avdtp_multiplexing_mode_capabilities_t multiplexing_mode;
220 } avdtp_capabilities_t;
221 
222 typedef enum{
223     AVDTP_SBC_48000 = 1,
224     AVDTP_SBC_44100 = 2,
225     AVDTP_SBC_32000 = 4,
226     AVDTP_SBC_16000 = 8
227 } avdtp_sbc_sampling_frequency_t;
228 
229 typedef enum{
230     AVDTP_SBC_JOINT_STEREO  = 1,
231     AVDTP_SBC_STEREO        = 2,
232     AVDTP_SBC_DUAL_CHANNEL  = 4,
233     AVDTP_SBC_MONO          = 8
234 } avdtp_sbc_channel_mode_t;
235 
236 typedef enum{
237     AVDTP_SBC_BLOCK_LENGTH_16 = 1,
238     AVDTP_SBC_BLOCK_LENGTH_12 = 2,
239     AVDTP_SBC_BLOCK_LENGTH_8  = 4,
240     AVDTP_SBC_BLOCK_LENGTH_4  = 8
241 } avdtp_sbc_block_length_t;
242 
243 typedef enum{
244     AVDTP_SBC_SUBBANDS_8 = 1,
245     AVDTP_SBC_SUBBANDS_4 = 2
246 } avdtp_sbc_subbands_t;
247 
248 typedef enum{
249     AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS = 1,
250     AVDTP_SBC_ALLOCATION_METHOD_SNR      = 2
251 } avdtp_sbc_allocation_method_t;
252 
253 typedef struct {
254     uint8_t fragmentation;
255     uint8_t starting_packet; // of fragmented SBC frame
256     uint8_t last_packet;     // of fragmented SBC frame
257     uint8_t num_frames;
258 } avdtp_sbc_codec_header_t;
259 
260 typedef enum {
261     AVDTP_MPEG_LAYER_1 = 1,
262     AVDTP_MPEG_LAYER_2,
263     AVDTP_MPEG_LAYER_3,
264 } avdtp_mpeg_layer_t;
265 
266 
267 typedef enum {
268     AVDTP_AAC_MPEG2_LC = 1,
269     AVDTP_AAC_MPEG4_LC,
270     AVDTP_AAC_MPEG4_LTP,
271     AVDTP_AAC_MPEG4_SCALABLE
272 } avdtp_aac_object_type_t;
273 
274 typedef enum {
275     AVDTP_ATRAC_VERSION_1 = 1,
276     AVDTP_ATRAC_VERSION_2,
277     AVDTP_ATRAC_VERSION_3
278 } avdtp_atrac_version_t;
279 
280 // used for MPEG1/2 Audio, ATRAC (no stereo mode)
281 typedef enum {
282     AVDTP_CHANNEL_MODE_MONO = 1,
283     AVDTP_CHANNEL_MODE_DUAL_CHANNEL,
284     AVDTP_CHANNEL_MODE_STEREO,
285     AVDTP_CHANNEL_MODE_JOINT_STEREO,
286 } avdtp_channel_mode_t;
287 
288 typedef struct {
289     uint16_t                        sampling_frequency;
290     avdtp_channel_mode_t            channel_mode;
291     uint8_t                         block_length;
292     uint8_t                         subbands;
293     avdtp_sbc_allocation_method_t   allocation_method;
294     uint8_t                         min_bitpool_value;
295     uint8_t                         max_bitpool_value;
296 } avdtp_configuration_sbc_t;
297 
298 typedef struct {
299     avdtp_mpeg_layer_t      layer;
300     uint8_t                 crc;
301     avdtp_channel_mode_t    channel_mode;
302     uint8_t                 media_payload_format;
303     uint16_t                sampling_frequency;
304     uint8_t                 vbr;
305     uint8_t                 bit_rate_index;
306 } avdtp_configuration_mpeg_audio_t;
307 
308 typedef struct {
309     avdtp_aac_object_type_t object_type;
310     uint32_t                sampling_frequency;
311     uint8_t                 channels;
312     uint32_t                bit_rate;
313     uint8_t                 vbr;
314 } avdtp_configuration_mpeg_aac_t;
315 
316 typedef struct {
317     avdtp_atrac_version_t   version;
318     avdtp_channel_mode_t    channel_mode;
319     uint16_t                sampling_frequency;
320     uint8_t                 vbr;
321     uint8_t                 bit_rate_index;
322     uint16_t                maximum_sul;
323 } avdtp_configuration_atrac_t;
324 
325 
326 
327 typedef struct {
328     uint8_t version;
329     uint8_t padding;
330     uint8_t extension;
331     uint8_t csrc_count;
332     uint8_t marker;
333     uint8_t payload_type;
334 
335     uint16_t sequence_number;
336     uint32_t timestamp;
337     uint32_t synchronization_source;
338 
339     uint32_t csrc_list[AVDTP_MAX_CSRC_NUM];
340 } avdtp_media_packet_header_t;
341 
342 typedef enum {
343     AVDTP_BASIC_SERVICE_MODE = 0,
344     AVDTP_MULTIPLEXING_SERVICE_MODE
345 } avdtp_service_mode_t;
346 
347 typedef enum {
348     AVDTP_STREAM_ENDPOINT_IDLE = 0,
349     AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE,
350     AVDTP_STREAM_ENDPOINT_CONFIGURED,
351 
352     AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM,
353     AVDTP_STREAM_ENDPOINT_W4_ACCEPT_OPEN_STREAM,
354     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED,
355 
356     AVDTP_STREAM_ENDPOINT_OPENED,
357     AVDTP_STREAM_ENDPOINT_STREAMING,
358 
359     AVDTP_STREAM_ENDPOINT_CLOSING,
360     AVDTP_STREAM_ENDPOINT_ABORTING,
361     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED
362 } avdtp_stream_endpoint_state_t;
363 
364 typedef enum {
365     AVDTP_INITIATOR_STREAM_CONFIG_IDLE = 0,
366     AVDTP_INITIATOR_W2_SET_CONFIGURATION,
367     AVDTP_INITIATOR_W2_SUSPEND_STREAM_WITH_SEID,
368     AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID,
369 
370     AVDTP_INITIATOR_W2_OPEN_STREAM,
371 
372     AVDTP_INITIATOR_W2_STREAMING_ABORT,
373     AVDTP_INITIATOR_FRAGMENTATED_COMMAND,
374     AVDTP_INITIATOR_W4_ANSWER
375 } avdtp_initiator_stream_endpoint_state_t;
376 
377 typedef enum {
378     AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE = 0,
379     AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES,
380     AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES,
381     AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT,
382     AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION,
383     AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE,
384     AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION,
385     AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM,
386     AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM,
387     AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM,
388     AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM,
389     AVDTP_ACCEPTOR_W2_SUSPEND_STREAM_WITH_SEID,
390     AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM,
391     AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
392     AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
393     AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD,
394     AVDTP_ACCEPTOR_STREAMING
395 } avdtp_acceptor_stream_endpoint_state_t;
396 
397 typedef struct {
398     uint8_t seid;           // 0x01 – 0x3E, 6bit
399     uint8_t in_use;         // 1 bit, 0 - not in use, 1 - in use
400     avdtp_media_type_t media_type;     // 4 bit
401     avdtp_sep_type_t   type;       // 1 bit, 0 - SRC, 1 - SNK
402 
403     uint16_t registered_service_categories;
404     avdtp_capabilities_t capabilities;
405 
406     uint16_t configured_service_categories;
407     avdtp_capabilities_t configuration;
408 } avdtp_sep_t;
409 
410 
411 typedef enum {
412     AVDTP_SIGNALING_CONNECTION_IDLE = 0,
413     AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SINK,
414     AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE,
415     AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SOURCE,
416     AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE,
417     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED,
418     AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY,
419     AVDTP_SIGNALING_CONNECTION_OPENED,
420     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED
421 } avdtp_connection_state_t;
422 
423 typedef enum {
424     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE = 0,
425     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS,
426     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
427     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
428     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE
429 } avdtp_acceptor_connection_state_t;
430 
431 typedef enum {
432     AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE = 0,
433     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS,
434     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES,
435     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_SDP_QUERY_COMPLETE_THEN_GET_ALL_CAPABILITIES,
436     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES,
437     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES,
438     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION,
439     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_DELAY_REPORT,
440     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER
441 } avdtp_initiator_connection_state_t;
442 
443 typedef struct {
444     uint8_t  command[200];
445     uint16_t size;
446     uint16_t offset;
447     uint8_t  acp_seid;
448     uint8_t  int_seid;
449     uint16_t transaction_label;
450     uint16_t num_packets;
451     avdtp_signal_identifier_t   signal_identifier;
452     avdtp_message_type_t        message_type;
453     avdtp_packet_type_t         packet_type;
454 } avdtp_signaling_packet_t;
455 
456 typedef enum {
457     AVDTP_CONFIGURATION_STATE_IDLE = 0,
458     AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED,
459     AVDTP_CONFIGURATION_STATE_LOCAL_CONFIGURED,
460     AVDTP_CONFIGURATION_STATE_REMOTE_INITIATED,
461     AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED,
462 } avtdp_configuration_state_t;
463 
464 typedef enum {
465     A2DP_IDLE = 0,
466     A2DP_W4_CONNECTED,
467     A2DP_CONNECTED,
468     A2DP_DISCOVER_SEPS,
469     A2DP_GET_CAPABILITIES,
470     A2DP_W2_GET_ALL_CAPABILITIES, //5
471     A2DP_DISCOVERY_DONE,
472     A2DP_SET_CONFIGURATION,
473     A2DP_W4_GET_CONFIGURATION,
474     A2DP_W4_SET_CONFIGURATION,
475     A2DP_CONFIGURED,
476     A2DP_W2_SUSPEND_STREAM_WITH_SEID, //10
477     A2DP_W2_RECONFIGURE_WITH_SEID,
478     A2DP_W2_OPEN_STREAM_WITH_SEID,
479     A2DP_W4_OPEN_STREAM_WITH_SEID,
480     A2DP_W2_START_STREAM_WITH_SEID,
481     A2DP_W2_ABORT_STREAM_WITH_SEID,   //15
482     A2DP_W2_STOP_STREAM_WITH_SEID,
483     A2DP_STREAMING_OPENED
484 } a2dp_state_t;
485 
486 
487 typedef struct {
488     btstack_linked_item_t    item;
489     bd_addr_t remote_addr;
490 
491     uint16_t avdtp_cid;
492     hci_con_handle_t con_handle;
493 
494     // SDP results
495     uint16_t avdtp_l2cap_psm;
496     uint16_t avdtp_version;
497     bool     sink_supported;
498     bool     source_supported;
499 
500     uint16_t l2cap_signaling_cid;
501     uint16_t l2cap_mtu;
502 
503     avdtp_connection_state_t state;
504     avdtp_acceptor_connection_state_t  acceptor_connection_state;
505     avdtp_initiator_connection_state_t initiator_connection_state;
506 
507     // used to reassemble fragmented commands
508     avdtp_signaling_packet_t acceptor_signaling_packet;
509 
510     // used to prepare outgoing signaling packets
511     avdtp_signaling_packet_t initiator_signaling_packet;
512 
513     uint8_t initiator_local_seid;
514     uint8_t initiator_remote_seid;
515 
516     uint8_t acceptor_local_seid;
517 
518     uint16_t delay_ms;
519 
520     // for repeating the set_configuration
521     void * active_stream_endpoint;
522 
523     uint8_t initiator_transaction_label;
524     uint8_t acceptor_transaction_label;
525     bool    wait_to_send_acceptor;
526 	bool    wait_to_send_initiator;
527 
528     uint8_t suspended_seids[AVDTP_MAX_NUM_SEPS];
529     uint8_t num_suspended_seids;
530 
531     uint8_t reject_service_category;
532     avdtp_signal_identifier_t reject_signal_identifier;
533     uint8_t error_code;
534 
535     // configuration state machine
536     avtdp_configuration_state_t configuration_state;
537     // btstack_timer_source_t configuration_timer;
538 
539     bool incoming_declined;
540     btstack_timer_source_t retry_timer;
541 
542     bool         a2dp_source_discover_seps;
543     bool         a2dp_source_outgoing_active;
544     bool         a2dp_source_have_config;
545     bool         a2dp_source_stream_endpoint_configured;
546     a2dp_state_t a2dp_source_state;
547 
548 } avdtp_connection_t;
549 
550 
551 typedef struct avdtp_stream_endpoint {
552     btstack_linked_item_t    item;
553 
554     // original capabilities configured via avdtp_register_x_category
555     avdtp_sep_t sep;
556 
557     // media codec configuration - provided by user
558     uint16_t  media_codec_configuration_len;
559     uint8_t * media_codec_configuration_info;
560 
561     avdtp_sep_t remote_sep;
562     hci_con_handle_t media_con_handle;
563     uint16_t l2cap_media_cid;
564     uint16_t l2cap_reporting_cid;
565     uint16_t l2cap_recovery_cid;
566 
567     avdtp_stream_endpoint_state_t  state;
568     avdtp_acceptor_stream_endpoint_state_t  acceptor_config_state;
569     avdtp_initiator_stream_endpoint_state_t initiator_config_state;
570     a2dp_state_t a2dp_state;
571     // active connection
572     avdtp_connection_t * connection;
573 
574     // currently active remote seid
575     avdtp_capabilities_t remote_capabilities;
576     uint16_t remote_capabilities_bitmap;
577 
578     uint16_t remote_configuration_bitmap;
579     avdtp_capabilities_t remote_configuration;
580 
581     // temporary codec config used by A2DP Source
582     uint8_t set_config_remote_seid;
583     avdtp_media_codec_type_t media_codec_type;
584     uint8_t media_codec_info[8];
585 
586     // preferred SBC codec settings
587     uint32_t preferred_sampling_frequency;
588     uint8_t  preferred_channel_mode;
589 
590     // register request for media L2cap connection release
591     uint8_t media_disconnect;
592     uint8_t media_connect;
593     uint8_t start_stream;
594     uint8_t close_stream;
595     uint8_t request_can_send_now;
596     uint8_t abort_stream;
597     uint8_t suspend_stream;
598     uint16_t sequence_number;
599 } avdtp_stream_endpoint_t;
600 
601 void avdtp_init(void);
602 void avdtp_deinit(void);
603 
604 avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr);
605 avdtp_connection_t * avdtp_get_connection_for_avdtp_cid(uint16_t avdtp_cid);
606 avdtp_connection_t * avdtp_get_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid);
607 btstack_linked_list_t * avdtp_get_connections(void);
608 btstack_linked_list_t * avdtp_get_stream_endpoints(void);
609 
610 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_seid(uint16_t seid);
611 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec(avdtp_media_codec_type_t codec_type);
612 avdtp_stream_endpoint_t * avdtp_get_source_stream_endpoint_for_media_codec_other(uint32_t vendor_id, uint16_t codec_id);
613 
614 btstack_packet_handler_t avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t *stream_endpoint);
615 void avdtp_emit_sink_and_source(uint8_t * packet, uint16_t size);
616 void avdtp_emit_source(uint8_t * packet, uint16_t size);
617 
618 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint);
619 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint);
620 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint);
621 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets);
622 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);
623 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery);
624 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);
625 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation);
626 
627 // sink only
628 void avdtp_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size));
629 
630 /**
631  * @brief Register media configuration validator. Can reject insuitable configuration or report stream endpoint as currently busy
632  * @note validator has to return AVDTP error codes like: AVDTP_ERROR_CODE_SEP_IN_USE or AVDTP_ERROR_CODE_UNSUPPORTED_CONFIGURATION
633  * @param callback
634  */
635 void avdtp_register_media_config_validator(uint8_t (*callback)(const avdtp_stream_endpoint_t * stream_endpoint, avdtp_media_codec_type_t media_codec_type, const uint8_t * media_codec_info, uint16_t media_codec_info_len));
636 
637 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
638 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type);
639 void avdtp_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint);
640 
641 uint8_t avdtp_connect(bd_addr_t remote, avdtp_role_t role, uint16_t * avdtp_cid);
642 uint8_t avdtp_disconnect(uint16_t avdtp_cid);
643 void    avdtp_register_sink_packet_handler(btstack_packet_handler_t callback);
644 void    avdtp_register_source_packet_handler(btstack_packet_handler_t callback);
645 
646 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid);
647 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid);
648 uint8_t avdtp_stop_stream (uint16_t avdtp_cid, uint8_t local_seid);
649 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid);
650 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid);
651 
652 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid);
653 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid);
654 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid);
655 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid);
656 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);
657 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);
658 uint8_t avdtp_validate_media_configuration(const avdtp_stream_endpoint_t * stream_endpoint, avdtp_media_codec_type_t media_codec_type, const uint8_t * media_codec_info, uint16_t media_codec_info_len);
659 
660 // frequency will be used by avdtp_choose_sbc_sampling_frequency (if supported by both endpoints)
661 void    avdtp_set_preferred_sampling_frequeny(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency);
662 
663 // channel_mode will be used by avdtp_choose_sbc_channel_mode (if supported by both endpoints)
664 void    avdtp_set_preferred_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t channel_mode);
665 
666 void    avdtp_set_preferred_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency);
667 
668 avdtp_channel_mode_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap);
669 avdtp_sbc_allocation_method_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap);
670 uint16_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap);
671 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap);
672 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap);
673 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value);
674 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value);
675 
676 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint);
677 
678 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint);
679 
680 uint16_t avdtp_get_next_transaction_label(void);
681 
682 #if defined __cplusplus
683 }
684 #endif
685 
686 #endif // AVDTP_H
687