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