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