xref: /btstack/src/classic/avdtp.h (revision f9f2075ceac5e9dc08e9abea437e43d733a3a0ea)
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 
56 #if defined __cplusplus
57 extern "C" {
58 #endif
59 
60 #define MAX_NUM_SEPS 10
61 
62 // protocols
63 #define PSM_AVCTP 0x0017
64 #define PSM_AVDTP 0x0019
65 
66 // service classes
67 #define AUDIO_SOURCE_GROUP          0x110A
68 #define AUDIO_SINK_GROUP            0x110B
69 #define ADVANCED_AUDIO_DISTRIBUTION 0X110D
70 
71 #define MAX_CSRC_NUM 15
72 
73 // Supported Features
74 #define AVDTP_SOURCE_SF_Player      0x0001
75 #define AVDTP_SOURCE_SF_Microphone  0x0002
76 #define AVDTP_SOURCE_SF_Tuner       0x0004
77 #define AVDTP_SOURCE_SF_Mixer       0x0008
78 
79 #define AVDTP_SINK_SF_Headphone     0x0001
80 #define AVDTP_SINK_SF_Speaker       0x0002
81 #define AVDTP_SINK_SF_Recorder      0x0004
82 #define AVDTP_SINK_SF_Amplifier     0x0008
83 
84 // ACP to INT, Signal Response Header Error Codes
85 #define BAD_HEADER_FORMAT 0x01
86 
87 // ACP to INT, Signal Response Payload Format Error Codes
88 #define BAD_LENGTH 0x11
89 #define BAD_ACP_SEID 0x12
90 #define SEP_IN_USE 0x13
91 #define SEP_NOT_IN_USE 0x14
92 #define BAD_SERV_CATEGORY 0x17
93 #define BAD_PAYLOAD_FORMAT 0x18
94 #define NOT_SUPPORTED_COMMAND 0x19
95 #define INVALID_CAPABILITIES 0x1A
96 
97 // ACP to INT, Signal Response Transport Service Capabilities Error Codes
98 #define BAD_RECOVERY_TYPE 0x22
99 #define BAD_MEDIA_TRANSPORT_FORMAT 0x23
100 #define BAD_RECOVERY_FORMAT 0x25
101 #define BAD_ROHC_FORMAT 0x26
102 #define BAD_CP_FORMAT 0x27
103 #define BAD_MULTIPLEXING_FORMAT 0x28
104 #define UNSUPPORTED_CONFIGURATION 0x29
105 
106 // ACP to INT, Procedure Error Codes
107 #define BAD_STATE 0x31
108 // Signal Identifier fields
109 typedef enum {
110     AVDTP_SI_DISCOVER = 0x01,
111     AVDTP_SI_GET_CAPABILITIES,
112     AVDTP_SI_SET_CONFIGURATION,
113     AVDTP_SI_GET_CONFIGURATION,
114     AVDTP_SI_RECONFIGURE, //5
115     AVDTP_SI_OPEN,  //6
116     AVDTP_SI_START, //7
117     AVDTP_SI_CLOSE,
118     AVDTP_SI_SUSPEND,
119     AVDTP_SI_ABORT, //10
120     AVDTP_SI_SECURITY_CONTROL,
121     AVDTP_SI_GET_ALL_CAPABILITIES, //12
122     AVDTP_SI_DELAYREPORT
123 } avdtp_signal_identifier_t;
124 
125 typedef enum {
126     AVDTP_SINGLE_PACKET= 0,
127     AVDTP_START_PACKET    ,
128     AVDTP_CONTINUE_PACKET ,
129     AVDTP_END_PACKET
130 } avdtp_packet_type_t;
131 
132 typedef enum {
133     AVDTP_CMD_MSG = 0,
134     AVDTP_GENERAL_REJECT_MSG   ,
135     AVDTP_RESPONSE_ACCEPT_MSG ,
136     AVDTP_RESPONSE_REJECT_MSG
137 } avdtp_message_type_t;
138 
139 typedef enum{
140     AVDTP_AUDIO = 0,
141     AVDTP_VIDEO,
142     AVDTP_MULTIMEDIA
143 } avdtp_media_type_t;
144 
145 typedef enum{
146     AVDTP_CODEC_SBC             = 0x00,
147     AVDTP_CODEC_MPEG_1_2_AUDIO  = 0x01,
148     AVDTP_CODEC_MPEG_2_4_AAC    = 0x02,
149     AVDTP_CODEC_ATRAC_FAMILY    = 0x04,
150     AVDTP_CODEC_NON_A2DP        = 0xFF
151 } avdtp_media_codec_type_t;
152 
153 typedef enum{
154     AVDTP_CONTENT_PROTECTION_DTCP = 0x0001,
155     AVDTP_CONTENT_PROTECTION_SCMS_T = 0x0002
156 } avdtp_content_protection_type_t;
157 
158 typedef enum{
159     AVDTP_SOURCE = 0,
160     AVDTP_SINK
161 } avdtp_sep_type_t;
162 
163 typedef enum {
164     AVDTP_SERVICE_CATEGORY_INVALID_0 = 0x00,
165     AVDTP_MEDIA_TRANSPORT = 0X01,
166     AVDTP_REPORTING,
167     AVDTP_RECOVERY,
168     AVDTP_CONTENT_PROTECTION, //4
169     AVDTP_HEADER_COMPRESSION, //5
170     AVDTP_MULTIPLEXING,       //6
171     AVDTP_MEDIA_CODEC,        //7
172     AVDTP_DELAY_REPORTING,    //8
173     AVDTP_SERVICE_CATEGORY_INVALID_FF = 0xFF
174 } avdtp_service_category_t;
175 
176 typedef struct {
177     uint8_t recovery_type;                  // 0x01 = RFC2733
178     uint8_t maximum_recovery_window_size;   // 0x01 to 0x18, for a Transport Packet
179     uint8_t maximum_number_media_packets;   // 0x01 to 0x18, The maximum number of media packets a specific parity code covers
180 } avdtp_recovery_capabilities_t;
181 
182 typedef struct {
183     avdtp_media_type_t       media_type;
184     avdtp_media_codec_type_t media_codec_type;
185     uint16_t  media_codec_information_len;
186     const uint8_t * media_codec_information;
187 } adtvp_media_codec_capabilities_t;
188 
189 typedef struct {
190     uint16_t cp_type;
191     uint16_t cp_type_value_len;
192     const uint8_t * cp_type_value;
193 } adtvp_content_protection_t;
194 
195 typedef struct{
196     uint8_t back_ch;  // byte0 - bit 8; 0=Not Available/Not Used; 1=Available/In Use
197     uint8_t media;    // byte0 - bit 7
198     uint8_t recovery; // byte0 - bit 6
199 } avdtp_header_compression_capabilities_t;
200 
201 typedef struct{
202     uint8_t fragmentation; // byte0 - bit 8, Allow Adaptation Layer Fragmentation, 0 no, 1 yes
203     // Request/indicate value of the Transport Session Identifier for a media, reporting, or recovery transport sessions, respectively
204     uint8_t transport_identifiers_num;
205     uint8_t transport_session_identifiers[3];   // byte1, upper 5bits, 0x01 to 0x1E
206     // Request/indicate value for TCID for a media, reporting, or transport session
207     uint8_t tcid[3];         // byte2 0x01 to 0x1E
208 } avdtp_multiplexing_mode_capabilities_t;
209 
210 typedef struct{
211     avdtp_recovery_capabilities_t recovery;
212     adtvp_media_codec_capabilities_t media_codec;
213     adtvp_content_protection_t content_protection;
214     avdtp_header_compression_capabilities_t header_compression;
215     avdtp_multiplexing_mode_capabilities_t multiplexing_mode;
216 } avdtp_capabilities_t;
217 
218 typedef enum{
219     AVDTP_SBC_48000 = 1,
220     AVDTP_SBC_44100 = 2,
221     AVDTP_SBC_32000 = 4,
222     AVDTP_SBC_16000 = 8
223 } avdtp_sbc_sampling_frequency_t;
224 
225 typedef enum{
226     AVDTP_SBC_JOINT_STEREO  = 1,
227     AVDTP_SBC_STEREO        = 2,
228     AVDTP_SBC_DUAL_CHANNEL  = 4,
229     AVDTP_SBC_MONO          = 8
230 } avdtp_sbc_channel_mode_t;
231 
232 typedef enum{
233     AVDTP_SBC_BLOCK_LENGTH_16 = 1,
234     AVDTP_SBC_BLOCK_LENGTH_12 = 2,
235     AVDTP_SBC_BLOCK_LENGTH_8  = 4,
236     AVDTP_SBC_BLOCK_LENGTH_4  = 8
237 } avdtp_sbc_block_length_t;
238 
239 typedef enum{
240     AVDTP_SBC_SUBBANDS_8 = 1,
241     AVDTP_SBC_SUBBANDS_4 = 2
242 } avdtp_sbc_subbands_t;
243 
244 typedef enum{
245     AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS = 1,
246     AVDTP_SBC_ALLOCATION_METHOD_SNR      = 2
247 } avdtp_sbc_allocation_method_t;
248 
249 typedef struct {
250     uint8_t fragmentation;
251     uint8_t starting_packet; // of fragmented SBC frame
252     uint8_t last_packet;     // of fragmented SBC frame
253     uint8_t num_frames;
254 } avdtp_sbc_codec_header_t;
255 
256 // typedef struct {
257 //     uint8_t transaction_label;
258 //     avdtp_packet_type_t packet_type;
259 //     avdtp_message_type_t message_type;
260 //     uint8_t signal_identifier;
261 // } avdtp_signaling_packet_header_t;
262 
263 typedef struct {
264     uint8_t version;
265     uint8_t padding;
266     uint8_t extension;
267     uint8_t csrc_count;
268     uint8_t marker;
269     uint8_t payload_type;
270 
271     uint16_t sequence_number;
272     uint32_t timestamp;
273     uint32_t synchronization_source;
274 
275     uint32_t csrc_list[MAX_CSRC_NUM];
276 } avdtp_media_packet_header_t;
277 
278 typedef enum {
279     AVDTP_BASIC_SERVICE_MODE,
280     AVDTP_MULTIPLEXING_SERVICE_MODE
281 } avdtp_service_mode_t;
282 
283 typedef enum {
284     AVDTP_STREAM_ENDPOINT_IDLE,
285     AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE,
286     AVDTP_STREAM_ENDPOINT_CONFIGURED,
287     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED,
288     AVDTP_STREAM_ENDPOINT_OPENED,
289     AVDTP_STREAM_ENDPOINT_STREAMING,
290     AVDTP_STREAM_ENDPOINT_CLOSING,
291     AVDTP_STREAM_ENDPOINT_ABORTING,
292     AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED
293 } avdtp_stream_endpoint_state_t;
294 
295 typedef enum {
296     AVDTP_INITIATOR_STREAM_CONFIG_IDLE,
297     AVDTP_INITIATOR_W2_SET_CONFIGURATION,
298     AVDTP_INITIATOR_W2_SUSPEND_STREAM_WITH_SEID,
299     AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID,
300     AVDTP_INITIATOR_W2_MEDIA_CONNECT,
301     AVDTP_INITIATOR_W2_STREAMING_START,
302     AVDTP_INITIATOR_W2_STREAMING_STOP,
303     AVDTP_INITIATOR_W2_STREAMING_ABORT,
304     AVDTP_INITIATOR_FRAGMENTATED_COMMAND,
305     AVDTP_INITIATOR_W4_ANSWER
306 } avdtp_initiator_stream_endpoint_state_t;
307 
308 typedef enum {
309     AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE,
310     AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES,
311     AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES,
312     AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION,
313     AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE,
314     AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION,
315     AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM,
316 
317     AVDTP_ACCEPTOR_W4_L2CAP_FOR_MEDIA_CONNECTED,
318 
319     AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM,
320     AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM,
321     AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM,
322     AVDTP_ACCEPTOR_W2_SUSPEND_STREAM_WITH_SEID,
323     AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM,
324     AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
325     AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
326     AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD,
327     AVDTP_ACCEPTOR_STREAMING
328 } avdtp_acceptor_stream_endpoint_state_t;
329 
330 typedef struct {
331     uint8_t seid;           // 0x01 – 0x3E, 6bit
332     uint8_t in_use;         // 1 bit, 0 - not in use, 1 - in use
333     avdtp_media_type_t media_type;     // 4 bit
334     avdtp_sep_type_t   type;       // 1 bit, 0 - SRC, 1 - SNK
335 
336     uint16_t registered_service_categories;
337     avdtp_capabilities_t capabilities;
338 
339     uint16_t configured_service_categories;
340     avdtp_capabilities_t configuration;
341 } avdtp_sep_t;
342 
343 
344 typedef enum {
345     AVDTP_SIGNALING_CONNECTION_IDLE,
346     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED,
347     AVDTP_SIGNALING_CONNECTION_OPENED,
348     AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED
349 } avdtp_connection_state_t;
350 
351 typedef enum {
352     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE,
353     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS,
354     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE,
355     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE,
356     AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE
357 } avdtp_acceptor_connection_state_t;
358 
359 typedef enum {
360     AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE,
361     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS,
362     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES,
363     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES,
364     AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION,
365     AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER
366 } avdtp_initiator_connection_state_t;
367 
368 typedef struct {
369     uint8_t command[200];
370     uint16_t size;
371     uint16_t offset;
372     avdtp_signal_identifier_t signal_identifier;
373     avdtp_message_type_t message_type;
374     avdtp_packet_type_t  packet_type;
375     uint8_t acp_seid;
376     uint8_t int_seid;
377     uint16_t transaction_label;
378     uint16_t num_packets;
379 } avdtp_signaling_packet_t;
380 
381 typedef struct {
382     btstack_linked_item_t    item;
383 
384     bd_addr_t remote_addr;
385     hci_con_handle_t con_handle;
386     uint16_t l2cap_signaling_cid;
387     avdtp_service_mode_t service_mode;
388 
389     avdtp_connection_state_t state;
390     avdtp_acceptor_connection_state_t  acceptor_connection_state;
391     avdtp_initiator_connection_state_t initiator_connection_state;
392 
393     // used for fragmentation
394     // avdtp_signaling_packet_header_t signaling_header;
395     avdtp_signaling_packet_t signaling_packet;
396 
397     uint8_t disconnect;
398     uint8_t initiator_transaction_label;
399     uint8_t acceptor_transaction_label;
400     uint8_t query_seid;
401     uint8_t int_seid;
402     uint8_t acp_seid;
403 
404     avdtp_capabilities_t remote_capabilities;
405     uint16_t remote_capabilities_bitmap;
406 
407     uint8_t wait_to_send_acceptor;
408     uint8_t wait_to_send_initiator;
409     uint8_t wait_to_send_self;
410 
411     uint8_t suspended_seids[MAX_NUM_SEPS];
412     uint8_t num_suspended_seids;
413 
414     uint8_t reject_service_category;
415     avdtp_signal_identifier_t reject_signal_identifier;
416     uint8_t error_code;
417 } avdtp_connection_t;
418 
419 
420 typedef struct avdtp_stream_endpoint {
421     btstack_linked_item_t    item;
422 
423     // original capabilities
424     avdtp_sep_t sep;
425     uint16_t l2cap_media_cid;
426     uint16_t l2cap_reporting_cid;
427     uint16_t l2cap_recovery_cid;
428 
429     avdtp_stream_endpoint_state_t  state;
430     avdtp_acceptor_stream_endpoint_state_t  acceptor_config_state;
431     avdtp_initiator_stream_endpoint_state_t initiator_config_state;
432 
433     // active connection
434     avdtp_connection_t * connection;
435     // store configurations with remote seps
436     avdtp_sep_t remote_seps[MAX_NUM_SEPS];
437     uint8_t remote_seps_num;
438 
439     // currently active remote seid
440     uint8_t remote_sep_index;
441     // register request for media L2cap connection release
442     uint8_t media_disconnect;
443     uint8_t media_connect;
444 } avdtp_stream_endpoint_t;
445 
446 #if defined __cplusplus
447 }
448 #endif
449 
450 #endif // __AVDTP_H