1 /******************************************************************************
2  *
3  *  Copyright 2002-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This interface file contains the interface to the Audio Video
22  *  Distribution Transport Protocol (AVDTP).
23  *
24  ******************************************************************************/
25 #ifndef AVDT_API_H
26 #define AVDT_API_H
27 
28 #include <base/strings/stringprintf.h>
29 #include <cstdint>
30 #include <string>
31 
32 #include "bt_target.h"
33 #include "bt_types.h"
34 #include "osi/include/log.h"
35 
36 /*****************************************************************************
37  *  Constants
38  ****************************************************************************/
39 #define AVDT_VERSION_1_3 0x0103
40 
41 #define AVDTP_VERSION_CONFIG_KEY "AvdtpVersion"
42 
43 /* Maximum size in bytes of the codec capabilities information element. */
44 #define AVDT_CODEC_SIZE 20
45 
46 /* API function return value result codes. */
47 typedef enum : uint16_t {
48   AVDT_SUCCESS = 0,      /* Function successful */
49   AVDT_BAD_PARAMS = 1,   /* Invalid parameters */
50   AVDT_NO_RESOURCES = 2, /* Not enough resources */
51   AVDT_BAD_HANDLE = 3,   /* Bad handle */
52   AVDT_BUSY = 4,         /* A procedure is already in progress */
53   AVDT_WRITE_FAIL = 5,   /* Write failed */
54 } tAVDT_RESULT;
55 
ToAvdtResult(uint16_t result)56 inline tAVDT_RESULT ToAvdtResult(uint16_t result) {
57   ASSERT_LOG(result <= AVDT_WRITE_FAIL, "Unable to convert illegal result:%hu",
58              result);
59   return static_cast<tAVDT_RESULT>(result);
60 }
61 
62 #define CASE_RETURN_TEXT(code) \
63   case code:                   \
64     return #code
65 
avdt_result_text(const tAVDT_RESULT & result)66 inline std::string avdt_result_text(const tAVDT_RESULT& result) {
67   switch (result) {
68     CASE_RETURN_TEXT(AVDT_SUCCESS);
69     CASE_RETURN_TEXT(AVDT_BAD_PARAMS);
70     CASE_RETURN_TEXT(AVDT_NO_RESOURCES);
71     CASE_RETURN_TEXT(AVDT_BAD_HANDLE);
72     CASE_RETURN_TEXT(AVDT_BUSY);
73     CASE_RETURN_TEXT(AVDT_WRITE_FAIL);
74     default:
75       return base::StringPrintf("UNKNOWN[%hu]", result);
76   }
77 }
78 #undef CASE_RETURN_TEXT
79 
80 /* The index to access the codec type in codec_info[]. */
81 #define AVDT_CODEC_TYPE_INDEX 2
82 
83 /* The size in bytes of a Adaptation Layer header. */
84 #define AVDT_AL_HDR_SIZE 3
85 
86 /* The size in bytes of a media packet header. */
87 #define AVDT_MEDIA_HDR_SIZE 12
88 
89 /* The handle is used when reporting MULTI_AV specific events */
90 #define AVDT_MULTI_AV_HANDLE 0xFF
91 
92 /* The number of bytes needed by the protocol stack for the protocol headers
93  * of a media packet.  This is the size of the media packet header, the
94  * L2CAP packet header and HCI header.
95 */
96 #define AVDT_MEDIA_OFFSET 23
97 
98 /* The marker bit is used by the application to mark significant events such
99  * as frame boundaries in the data stream.  This constant is used to check or
100  * set the marker bit in the m_pt parameter of an AVDT_WriteReq()
101  * or AVDT_DATA_IND_EVT.
102 */
103 #define AVDT_MARKER_SET 0x80
104 
105 #define MAX_2MBPS_AVDTP_MTU 663   // 2DH5 MTU=679, -12 for AVDTP, -4 for L2CAP
106 #define MAX_3MBPS_AVDTP_MTU 1005  // 3DH5 MTU=1021, -12 for AVDTP, -4 for L2CAP
107 
108 /* SEP Type.  This indicates the stream endpoint type. */
109 #define AVDT_TSEP_SRC 0     /* Source SEP */
110 #define AVDT_TSEP_SNK 1     /* Sink SEP */
111 #define AVDT_TSEP_INVALID 3 /* Invalid SEP */
peer_stream_endpoint_text(int type)112 inline const std::string peer_stream_endpoint_text(int type) {
113   switch (type) {
114     case AVDT_TSEP_SRC:
115       return std::string("Source");
116     case AVDT_TSEP_SNK:
117       return std::string("Sink");
118     default:
119       return std::string("Invalid");
120   }
121 }
122 
123 /* initiator/acceptor role for adaption */
124 #define AVDT_INT 0 /* initiator */
125 #define AVDT_ACP 1 /* acceptor */
126 
127 /* Media Type of the stream endpoint */
128 /* The value does not include the reserved 4-bit LSBs field */
129 #define AVDT_MEDIA_TYPE_AUDIO 0 /* Audio SEP */
130 #define AVDT_MEDIA_TYPE_VIDEO 1 /* Video SEP */
131 #define AVDT_MEDIA_TYPE_MULTI 2 /* Multimedia SEP */
132 
133 /* for reporting packets (packet types) */
134 #define AVDT_RTCP_PT_SR 200   /* SR (Sender Report) */
135 #define AVDT_RTCP_PT_RR 201   /* RR (Receiver Report) */
136 #define AVDT_RTCP_PT_SDES 202 /* SDES (Source Description) */
137 typedef uint8_t AVDT_REPORT_TYPE;
138 
139 #define AVDT_RTCP_SDES_CNAME 1 /* SDES item CNAME */
140 #ifndef AVDT_MAX_CNAME_SIZE
141 #define AVDT_MAX_CNAME_SIZE 28
142 #endif
143 
144 /* Protocol service capabilities.  This indicates the protocol service
145  * capabilities of a stream endpoint.  This value is a mask.
146  * Multiple values can be combined with a bitwise OR.
147 */
148 #define AVDT_PSC_TRANS (1 << 1)     /* Media transport */
149 #define AVDT_PSC_REPORT (1 << 2)    /* Reporting */
150 #define AVDT_PSC_RECOV (1 << 3)     /* Recovery */
151 #define AVDT_PSC_HDRCMP (1 << 5)    /* Header compression */
152 #define AVDT_PSC_MUX (1 << 6)       /* Multiplexing */
153 #define AVDT_PSC_DELAY_RPT (1 << 8) /* Delay Report */
154 
155 /* Recovery type.  This indicates the recovery type. */
156 #define AVDT_RECOV_RFC2733 1 /* RFC2733 recovery */
157 
158 /* Header compression capabilities.  This indicates the header compression
159  * capabilities.  This value is a mask.  Multiple values can be combined
160  * with a bitwise OR.
161 */
162 #define AVDT_HDRCMP_MEDIA (1 << 5)  /* Available for media packets */
163 #define AVDT_HDRCMP_RECOV (1 << 6)  /* Available for recovery packets */
164 #define AVDT_HDRCMP_BACKCH (1 << 7) /* Back channel supported */
165 
166 /* Multiplexing capabilities mask. */
167 #define AVDT_MUX_FRAG (1 << 7) /* Allow Adaptation Layer Fragmentation */
168 
169 /* Application service category. This indicates the application
170  * service category.
171 */
172 #define AVDT_ASC_PROTECT 4 /* Content protection */
173 #define AVDT_ASC_CODEC 7   /* Codec */
174 
175 /* the content protection IDs assigned by BT SIG */
176 #define AVDT_CP_SCMS_T_ID 0x0002
177 #define AVDT_CP_DTCP_ID 0x0001
178 
179 #define AVDT_CP_LOSC 2
180 #define AVDT_CP_INFO_LEN 3
181 
182 #define AVDT_CP_SCMS_COPY_MASK 3
183 #define AVDT_CP_SCMS_COPY_FREE 2
184 #define AVDT_CP_SCMS_COPY_ONCE 1
185 #define AVDT_CP_SCMS_COPY_NEVER 0
186 
187 /* Error codes.  The following are error codes defined in the AVDTP and GAVDP
188  * specifications.  These error codes communicate protocol errors between
189  * AVDTP and the application.  More detailed descriptions of the error codes
190  * and their appropriate use can be found in the AVDTP and GAVDP specifications.
191  * These error codes are unrelated to the result values returned by the
192  * AVDTP API functions.
193 */
194 /* Bad packet header format */
195 #define AVDT_ERR_HEADER 0x01
196 /* Bad packet length */
197 #define AVDT_ERR_LENGTH 0x11
198 /* Invalid SEID */
199 #define AVDT_ERR_SEID 0x12
200 /* The SEP is in use */
201 #define AVDT_ERR_IN_USE 0x13
202 /* The SEP is not in use */
203 #define AVDT_ERR_NOT_IN_USE 0x14
204 /* Bad service category */
205 #define AVDT_ERR_CATEGORY 0x17
206 /* Bad payload format */
207 #define AVDT_ERR_PAYLOAD 0x18
208 /* Requested command not supported */
209 #define AVDT_ERR_NSC 0x19
210 /* Reconfigure attempted invalid capabilities */
211 #define AVDT_ERR_INVALID_CAP 0x1A
212 /* Requested recovery type not defined */
213 #define AVDT_ERR_RECOV_TYPE 0x22
214 /* Media transport capability not correct */
215 #define AVDT_ERR_MEDIA_TRANS 0x23
216 /* Recovery service capability not correct */
217 #define AVDT_ERR_RECOV_FMT 0x25
218 /* Header compression service capability not correct */
219 #define AVDT_ERR_ROHC_FMT 0x26
220 /* Content protection service capability not correct */
221 #define AVDT_ERR_CP_FMT 0x27
222 /* Multiplexing service capability not correct */
223 #define AVDT_ERR_MUX_FMT 0x28
224 /* Configuration not supported */
225 #define AVDT_ERR_UNSUP_CFG 0x29
226 /* Message cannot be processed in this state */
227 #define AVDT_ERR_BAD_STATE 0x31
228 /* Report service capability not correct */
229 #define AVDT_ERR_REPORT_FMT 0x65
230 /* Invalid service category */
231 #define AVDT_ERR_SERVICE 0x80
232 /* Insufficient resources */
233 #define AVDT_ERR_RESOURCE 0x81
234 /* Invalid Media Codec Type */
235 #define AVDT_ERR_INVALID_MCT 0xC1
236 /* Unsupported Media Codec Type */
237 #define AVDT_ERR_UNSUP_MCT 0xC2
238 /* Invalid Level */
239 #define AVDT_ERR_INVALID_LEVEL 0xC3
240 /* Unsupported Level */
241 #define AVDT_ERR_UNSUP_LEVEL 0xC4
242 /* Invalid Content Protection Type */
243 #define AVDT_ERR_INVALID_CP 0xE0
244 /* Invalid Content Protection format */
245 #define AVDT_ERR_INVALID_FORMAT 0xE1
246 
247 /* Additional error codes.  This indicates error codes used by AVDTP
248  * in addition to the ones defined in the specifications.
249 */
250 #define AVDT_ERR_CONNECT 0x07 /* Connection failed. */
251 #define AVDT_ERR_TIMEOUT 0x08 /* Response timeout. */
252 
253 /* Control callback events. */
254 #define AVDT_DISCOVER_CFM_EVT 0      /* Discover confirm */
255 #define AVDT_GETCAP_CFM_EVT 1        /* Get capabilities confirm */
256 #define AVDT_OPEN_CFM_EVT 2          /* Open confirm */
257 #define AVDT_OPEN_IND_EVT 3          /* Open indication */
258 #define AVDT_CONFIG_IND_EVT 4        /* Configuration indication */
259 #define AVDT_START_CFM_EVT 5         /* Start confirm */
260 #define AVDT_START_IND_EVT 6         /* Start indication */
261 #define AVDT_SUSPEND_CFM_EVT 7       /* Suspend confirm */
262 #define AVDT_SUSPEND_IND_EVT 8       /* Suspend indication */
263 #define AVDT_CLOSE_CFM_EVT 9         /* Close confirm */
264 #define AVDT_CLOSE_IND_EVT 10        /* Close indication */
265 #define AVDT_RECONFIG_CFM_EVT 11     /* Reconfiguration confirm */
266 #define AVDT_RECONFIG_IND_EVT 12     /* Reconfiguration indication */
267 #define AVDT_SECURITY_CFM_EVT 13     /* Security confirm */
268 #define AVDT_SECURITY_IND_EVT 14     /* Security indication */
269 #define AVDT_WRITE_CFM_EVT 15        /* Write confirm */
270 #define AVDT_CONNECT_IND_EVT 16      /* Signaling channel connected */
271 #define AVDT_DISCONNECT_IND_EVT 17   /* Signaling channel disconnected */
272 #define AVDT_REPORT_CONN_EVT 18      /* Reporting channel connected */
273 #define AVDT_REPORT_DISCONN_EVT 19   /* Reporting channel disconnected */
274 #define AVDT_DELAY_REPORT_EVT 20     /* Delay report received */
275 #define AVDT_DELAY_REPORT_CFM_EVT 21 /* Delay report response received */
276 
277 #define AVDT_MAX_EVT (AVDT_DELAY_REPORT_CFM_EVT)
278 
279 /* PSM for AVDT */
280 #define AVDT_PSM 0x0019
281 
282 /*****************************************************************************
283  *  Type Definitions
284  ****************************************************************************/
285 
286 typedef struct {
287   uint32_t ntp_sec; /* NTP time: seconds relative to 0h UTC on 1 January 1900 */
288   uint32_t ntp_frac;    /* NTP time: the fractional part */
289   uint32_t rtp_time;    /* timestamp in RTP header */
290   uint32_t pkt_count;   /* sender's packet count: since starting transmission
291                         * up until the time this SR packet was generated. */
292   uint32_t octet_count; /* sender's octet count: same comment */
293 } tAVDT_SENDER_INFO;
294 
295 typedef struct {
296   uint8_t frag_lost; /* fraction lost since last RR */
297   uint32_t
298       packet_lost; /* cumulative number of packets lost since the beginning */
299   uint32_t seq_num_rcvd; /* extended highest sequence number received */
300   uint32_t jitter;       /* interarrival jitter */
301   uint32_t lsr;          /* last SR timestamp */
302   uint32_t dlsr;         /* delay since last SR */
303 } tAVDT_REPORT_BLK;
304 
305 typedef union {
306   tAVDT_SENDER_INFO sr;
307   tAVDT_REPORT_BLK rr;
308   uint8_t cname[AVDT_MAX_CNAME_SIZE + 1];
309 } tAVDT_REPORT_DATA;
310 
311 /**
312  * AVDTP Registration Control Block.
313  */
314 class AvdtpRcb {
315  public:
AvdtpRcb()316   AvdtpRcb()
317       : ctrl_mtu(0),
318         ret_tout(0),
319         sig_tout(0),
320         idle_tout(0),
321         scb_index(0) {}
322   AvdtpRcb& operator=(const AvdtpRcb&) = default;
323 
Reset()324   void Reset() {
325     ctrl_mtu = 0;
326     ret_tout = 0;
327     sig_tout = 0;
328     idle_tout = 0;
329     scb_index = 0;
330   }
331 
332   uint16_t ctrl_mtu; /* L2CAP MTU of the AVDTP signaling channel */
333   uint8_t ret_tout;  /* AVDTP signaling retransmission timeout */
334   uint8_t sig_tout;  /* AVDTP signaling message timeout */
335   uint8_t idle_tout; /* AVDTP idle signaling channel timeout */
336   uint8_t scb_index; /* The Stream Control Block index */
337 };
338 
339 /* This structure contains the SEP information.  This information is
340  * transferred during the discovery procedure.
341 */
342 typedef struct {
343   bool in_use;        /* true if stream is currently in use */
344   uint8_t seid;       /* Stream endpoint identifier */
345   uint8_t media_type; /* Media type: AVDT_MEDIA_TYPE_* */
346   uint8_t tsep;       /* SEP type */
347 } tAVDT_SEP_INFO;
348 
349 /**
350  * AVDTP SEP Configuration.
351  */
352 class AvdtpSepConfig {
353  public:
AvdtpSepConfig()354   AvdtpSepConfig()
355       : codec_info{},
356         protect_info{},
357         num_codec(0),
358         num_protect(0),
359         psc_mask(0),
360         recov_type(0),
361         recov_mrws(0),
362         recov_mnmp(0),
363         hdrcmp_mask(0) {}
364   AvdtpSepConfig& operator=(const AvdtpSepConfig&) = default;
365 
Reset()366   void Reset() {
367     memset(codec_info, 0, sizeof(codec_info));
368     memset(protect_info, 0, sizeof(protect_info));
369     num_codec = 0;
370     num_protect = 0;
371     psc_mask = 0;
372     recov_type = 0;
373     recov_mrws = 0;
374     recov_mnmp = 0;
375     hdrcmp_mask = 0;
376   }
377 
378   uint8_t codec_info[AVDT_CODEC_SIZE];     /* Codec capabilities array */
379   uint8_t protect_info[AVDT_PROTECT_SIZE]; /* Content protection capabilities */
380   uint8_t num_codec;   /* Number of media codec information elements */
381   uint8_t num_protect; /* Number of content protection information elements */
382   uint16_t psc_mask;   /* Protocol service capabilities mask */
383   uint8_t recov_type;  /* Recovery type */
384   uint8_t recov_mrws;  /* Maximum recovery window size */
385   uint8_t recov_mnmp;  /* Recovery maximum number of media packets */
386   uint8_t hdrcmp_mask; /* Header compression capabilities */
387 };
388 
389 /* Header structure for callback event parameters. */
390 typedef struct {
391   uint8_t
392       err_code; /* Zero if operation succeeded; nonzero if operation failed */
393   uint8_t err_param; /* Error parameter included for some events */
394   uint8_t label;     /* Transaction label */
395   uint8_t seid;      /* For internal use only */
396   uint8_t sig_id;    /* For internal use only */
397   uint8_t ccb_idx;   /* For internal use only */
398 } tAVDT_EVT_HDR;
399 
400 /* This data structure is associated with the AVDT_GETCAP_CFM_EVT,
401  * AVDT_RECONFIG_IND_EVT, and AVDT_RECONFIG_CFM_EVT.
402 */
403 typedef struct {
404   tAVDT_EVT_HDR hdr; /* Event header */
405   AvdtpSepConfig* p_cfg; /* Pointer to configuration for this SEP */
406 } tAVDT_CONFIG;
407 
408 /* This data structure is associated with the AVDT_CONFIG_IND_EVT. */
409 typedef struct {
410   tAVDT_EVT_HDR hdr; /* Event header */
411   AvdtpSepConfig* p_cfg; /* Pointer to configuration for this SEP */
412   uint8_t int_seid;  /* Stream endpoint ID of stream initiating the operation */
413 } tAVDT_SETCONFIG;
414 
415 /* This data structure is associated with the AVDT_OPEN_IND_EVT and
416  * AVDT_OPEN_CFM_EVT. */
417 typedef struct {
418   tAVDT_EVT_HDR hdr; /* Event header */
419   uint16_t peer_mtu; /* Transport channel L2CAP MTU of the peer */
420   uint16_t lcid;     /* L2CAP LCID for media channel */
421 } tAVDT_OPEN;
422 
423 /* This data structure is associated with the AVDT_SECURITY_IND_EVT
424  * and AVDT_SECURITY_CFM_EVT.
425 */
426 typedef struct {
427   tAVDT_EVT_HDR hdr; /* Event header */
428   uint8_t* p_data;   /* Pointer to security data */
429   uint16_t len;      /* Length in bytes of the security data */
430 } tAVDT_SECURITY;
431 
432 /* This data structure is associated with the AVDT_DISCOVER_CFM_EVT. */
433 typedef struct {
434   tAVDT_EVT_HDR hdr;          /* Event header */
435   tAVDT_SEP_INFO* p_sep_info; /* Pointer to SEP information */
436   uint8_t num_seps;           /* Number of stream endpoints */
437 } tAVDT_DISCOVER;
438 
439 /* This data structure is associated with the AVDT_DELAY_REPORT_EVT. */
440 typedef struct {
441   tAVDT_EVT_HDR hdr; /* Event header */
442   uint16_t delay;    /* Delay value */
443 } tAVDT_DELAY_RPT;
444 
445 /* Union of all control callback event data structures */
446 typedef union {
447   tAVDT_EVT_HDR hdr;
448   tAVDT_DISCOVER discover_cfm;
449   tAVDT_CONFIG getcap_cfm;
450   tAVDT_OPEN open_cfm;
451   tAVDT_OPEN open_ind;
452   tAVDT_SETCONFIG config_ind;
453   tAVDT_EVT_HDR start_cfm;
454   tAVDT_EVT_HDR suspend_cfm;
455   tAVDT_EVT_HDR close_cfm;
456   tAVDT_CONFIG reconfig_cfm;
457   tAVDT_CONFIG reconfig_ind;
458   tAVDT_SECURITY security_cfm;
459   tAVDT_SECURITY security_ind;
460   tAVDT_EVT_HDR connect_ind;
461   tAVDT_EVT_HDR disconnect_ind;
462   tAVDT_EVT_HDR report_conn;
463   tAVDT_DELAY_RPT delay_rpt_cmd;
464 } tAVDT_CTRL;
465 
466 /* This is the control callback function.  This function passes control events
467  * to the application.  This function is required for all registered stream
468  * endpoints and for the AVDT_DiscoverReq() and AVDT_GetCapReq() functions.
469  *
470 */
471 typedef void(tAVDT_CTRL_CBACK)(uint8_t handle, const RawAddress& bd_addr,
472                                uint8_t event, tAVDT_CTRL* p_data,
473                                uint8_t scb_index);
474 
475 /* This is the data callback function.  It is executed when AVDTP has a media
476  * packet ready for the application.  This function is required for SNK
477  * endpoints and not applicable for SRC endpoints.
478 */
479 typedef void(tAVDT_SINK_DATA_CBACK)(uint8_t handle, BT_HDR* p_pkt,
480                                     uint32_t time_stamp, uint8_t m_pt);
481 
482 /* This is the report callback function.  It is executed when AVDTP has a
483  * reporting packet ready for the application.  This function is required for
484  * streams created with AVDT_PSC_REPORT.
485 */
486 typedef void(tAVDT_REPORT_CBACK)(uint8_t handle, AVDT_REPORT_TYPE type,
487                                  tAVDT_REPORT_DATA* p_data);
488 
489 /**
490  * AVDTP Stream Configuration.
491  * The information is used when a stream is created.
492  */
493 class AvdtpStreamConfig {
494  public:
495   //
496   // Non-supported protocol command messages
497   //
498   // Suspend command not supported
499   static constexpr int AVDT_NSC_SUSPEND = 0x01;
500   // Reconfigure command not supported
501   static constexpr int AVDT_NSC_RECONFIG = 0x02;
502   // Security command not supported
503   static constexpr int AVDT_NSC_SECURITY = 0x04;
504 
AvdtpStreamConfig()505   AvdtpStreamConfig()
506       : p_avdt_ctrl_cback(nullptr),
507         scb_index(0),
508         p_sink_data_cback(nullptr),
509         p_report_cback(nullptr),
510         mtu(0),
511         tsep(0),
512         media_type(0),
513         nsc_mask(0) {}
514 
Reset()515   void Reset() {
516     cfg.Reset();
517     p_avdt_ctrl_cback = nullptr;
518     scb_index = 0;
519     p_sink_data_cback = nullptr;
520     p_report_cback = nullptr;
521     mtu = 0;
522     tsep = 0;
523     media_type = 0;
524     nsc_mask = 0;
525   }
526 
527   AvdtpSepConfig cfg;                   // SEP configuration
528   tAVDT_CTRL_CBACK* p_avdt_ctrl_cback;  // Control callback function
529   uint8_t scb_index;  // The index to the bta_av_cb.p_scb[] entry
530   tAVDT_SINK_DATA_CBACK* p_sink_data_cback;  // Sink data callback function
531   tAVDT_REPORT_CBACK* p_report_cback;        // Report callback function
532   uint16_t mtu;        // The L2CAP MTU of the transport channel
533   uint8_t tsep;        // SEP type
534   uint8_t media_type;  // Media type: AVDT_MEDIA_TYPE_*
535   uint16_t nsc_mask;   // Nonsupported protocol command messages
536 };
537 
538 /* AVDT data option mask is used in the write request */
539 #define AVDT_DATA_OPT_NONE 0x00          /* No option still add RTP header */
540 #define AVDT_DATA_OPT_NO_RTP (0x01 << 0) /* Skip adding RTP header */
541 
542 typedef uint8_t tAVDT_DATA_OPT_MASK;
543 
544 /*****************************************************************************
545  *  External Function Declarations
546  ****************************************************************************/
547 
548 /*******************************************************************************
549  *
550  * Function         AVDT_Register
551  *
552  * Description      This is the system level registration function for the
553  *                  AVDTP protocol.  This function initializes AVDTP and
554  *                  prepares the protocol stack for its use.  This function
555  *                  must be called once by the system or platform using AVDTP
556  *                  before the other functions of the API an be used.
557  *
558  *
559  * Returns          void
560  *
561  ******************************************************************************/
562 extern void AVDT_Register(AvdtpRcb* p_reg, tAVDT_CTRL_CBACK* p_cback);
563 
564 /*******************************************************************************
565  *
566  * Function         AVDT_Deregister
567  *
568  * Description      This function is called to deregister use AVDTP protocol.
569  *                  It is called when AVDTP is no longer being used by any
570  *                  application in the system.  Before this function can be
571  *                  called, all streams must be removed with AVDT_RemoveStream.
572  *
573  * Returns          void
574  *
575  ******************************************************************************/
576 extern void AVDT_Deregister(void);
577 
578 /*******************************************************************************
579  *
580  * Function         AVDT_AbortReq
581  *
582  * Description      Trigger Abort request to pass AVDTP Abort related mandatory
583  *                  PTS Test case.
584  *
585  * Returns          void.
586  *
587  ******************************************************************************/
588 extern void AVDT_AbortReq(uint8_t handle);
589 
590 /*******************************************************************************
591  *
592  * Function         AVDT_CreateStream
593  *
594  * Description      Create a stream endpoint.  After a stream endpoint is
595  *                  created an application can initiate a connection between
596  *                  this endpoint and an endpoint on a peer device.  In
597  *                  addition, a peer device can discover, get the capabilities,
598  *                  and connect to this endpoint.
599  *
600  *
601  * Returns          AVDT_SUCCESS if successful, otherwise error.
602  *
603  ******************************************************************************/
604 extern uint16_t AVDT_CreateStream(uint8_t peer_id, uint8_t* p_handle,
605                                   const AvdtpStreamConfig& avdtp_stream_config);
606 
607 /*******************************************************************************
608  *
609  * Function         AVDT_RemoveStream
610  *
611  * Description      Remove a stream endpoint.  This function is called when
612  *                  the application is no longer using a stream endpoint.
613  *                  If this function is called when the endpoint is connected
614  *                  the connection is closed and then the stream endpoint
615  *                  is removed.
616  *
617  *
618  * Returns          AVDT_SUCCESS if successful, otherwise error.
619  *
620  ******************************************************************************/
621 extern uint16_t AVDT_RemoveStream(uint8_t handle);
622 
623 /*******************************************************************************
624  *
625  * Function         AVDT_DiscoverReq
626  *
627  * Description      This function initiates a connection to the AVDTP service
628  *                  on the peer device, if not already present, and discovers
629  *                  the stream endpoints on the peer device.  (Please note
630  *                  that AVDTP discovery is unrelated to SDP discovery).
631  *                  This function can be called at any time regardless of
632  *                  whether there is an AVDTP connection to the peer device.
633  *
634  *                  When discovery is complete, an AVDT_DISCOVER_CFM_EVT
635  *                  is sent to the application via its callback function.
636  *                  The application must not call AVDT_GetCapReq() or
637  *                  AVDT_DiscoverReq() again to the same device until
638  *                  discovery is complete.
639  *
640  *                  The memory addressed by sep_info is allocated by the
641  *                  application.  This memory is written to by AVDTP as part
642  *                  of the discovery procedure.  This memory must remain
643  *                  accessible until the application receives the
644  *                  AVDT_DISCOVER_CFM_EVT.
645  *
646  * Returns          AVDT_SUCCESS if successful, otherwise error.
647  *
648  ******************************************************************************/
649 extern uint16_t AVDT_DiscoverReq(const RawAddress& bd_addr,
650                                  uint8_t channel_index,
651                                  tAVDT_SEP_INFO* p_sep_info, uint8_t max_seps,
652                                  tAVDT_CTRL_CBACK* p_cback);
653 
654 /*******************************************************************************
655  *
656  * Function         AVDT_GetCapReq
657  *
658  * Description      This function initiates a connection to the AVDTP service
659  *                  on the peer device, if not already present, and gets the
660  *                  capabilities of a stream endpoint on the peer device.
661  *                  This function can be called at any time regardless of
662  *                  whether there is an AVDTP connection to the peer device.
663  *
664  *                  When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
665  *                  sent to the application via its callback function.  The
666  *                  application must not call AVDT_GetCapReq() or
667  *                  AVDT_DiscoverReq() again until the procedure is complete.
668  *
669  *                  The memory pointed to by p_cfg is allocated by the
670  *                  application.  This memory is written to by AVDTP as part
671  *                  of the get capabilities procedure.  This memory must
672  *                  remain accessible until the application receives
673  *                  the AVDT_GETCAP_CFM_EVT.
674  *
675  * Returns          AVDT_SUCCESS if successful, otherwise error.
676  *
677  ******************************************************************************/
678 extern uint16_t AVDT_GetCapReq(const RawAddress& bd_addr, uint8_t channel_index,
679                                uint8_t seid, AvdtpSepConfig* p_cfg,
680                                tAVDT_CTRL_CBACK* p_cback, bool get_all_cap);
681 
682 /*******************************************************************************
683  *
684  * Function         AVDT_DelayReport
685  *
686  * Description      This functions sends a Delay Report to the peer device
687  *                  that is associated with a particular SEID.
688  *                  This function is called by SNK device.
689  *
690  * Returns          AVDT_SUCCESS if successful, otherwise error.
691  *
692  ******************************************************************************/
693 extern uint16_t AVDT_DelayReport(uint8_t handle, uint8_t seid, uint16_t delay);
694 
695 /*******************************************************************************
696  *
697  * Function         AVDT_OpenReq
698  *
699  * Description      This function initiates a connection to the AVDTP service
700  *                  on the peer device, if not already present, and connects
701  *                  to a stream endpoint on a peer device.  When the connection
702  *                  is completed, an AVDT_OPEN_CFM_EVT is sent to the
703  *                  application via the control callback function for this
704  *                  handle.
705  *
706  * Returns          AVDT_SUCCESS if successful, otherwise error.
707  *
708  ******************************************************************************/
709 extern uint16_t AVDT_OpenReq(uint8_t handle, const RawAddress& bd_addr,
710                              uint8_t channel_index, uint8_t seid,
711                              AvdtpSepConfig* p_cfg);
712 
713 /*******************************************************************************
714  *
715  * Function         AVDT_ConfigRsp
716  *
717  * Description      Respond to a configure request from the peer device.  This
718  *                  function must be called if the application receives an
719  *                  AVDT_CONFIG_IND_EVT through its control callback.
720  *
721  *
722  * Returns          AVDT_SUCCESS if successful, otherwise error.
723  *
724  ******************************************************************************/
725 extern uint16_t AVDT_ConfigRsp(uint8_t handle, uint8_t label,
726                                uint8_t error_code, uint8_t category);
727 
728 /*******************************************************************************
729  *
730  * Function         AVDT_StartReq
731  *
732  * Description      Start one or more stream endpoints.  This initiates the
733  *                  transfer of media packets for the streams.  All stream
734  *                  endpoints must previously be opened.  When the streams
735  *                  are started, an AVDT_START_CFM_EVT is sent to the
736  *                  application via the control callback function for each
737  *                  stream.
738  *
739  *
740  * Returns          AVDT_SUCCESS if successful, otherwise error.
741  *
742  ******************************************************************************/
743 extern uint16_t AVDT_StartReq(uint8_t* p_handles, uint8_t num_handles);
744 
745 /*******************************************************************************
746  *
747  * Function         AVDT_SuspendReq
748  *
749  * Description      Suspend one or more stream endpoints. This suspends the
750  *                  transfer of media packets for the streams.  All stream
751  *                  endpoints must previously be open and started.  When the
752  *                  streams are suspended, an AVDT_SUSPEND_CFM_EVT is sent to
753  *                  the application via the control callback function for
754  *                  each stream.
755  *
756  *
757  * Returns          AVDT_SUCCESS if successful, otherwise error.
758  *
759  ******************************************************************************/
760 extern uint16_t AVDT_SuspendReq(uint8_t* p_handles, uint8_t num_handles);
761 
762 /*******************************************************************************
763  *
764  * Function         AVDT_CloseReq
765  *
766  * Description      Close a stream endpoint.  This stops the transfer of media
767  *                  packets and closes the transport channel associated with
768  *                  this stream endpoint.  When the stream is closed, an
769  *                  AVDT_CLOSE_CFM_EVT is sent to the application via the
770  *                  control callback function for this handle.
771  *
772  *
773  * Returns          AVDT_SUCCESS if successful, otherwise error.
774  *
775  ******************************************************************************/
776 extern uint16_t AVDT_CloseReq(uint8_t handle);
777 
778 /*******************************************************************************
779  *
780  * Function         AVDT_ReconfigReq
781  *
782  * Description      Reconfigure a stream endpoint.  This allows the application
783  *                  to change the codec or content protection capabilities of
784  *                  a stream endpoint after it has been opened.  This function
785  *                  can only be called if the stream is opened but not started
786  *                  or if the stream has been suspended.  When the procedure
787  *                  is completed, an AVDT_RECONFIG_CFM_EVT is sent to the
788  *                  application via the control callback function for this
789  *                  handle.
790  *
791  *
792  * Returns          AVDT_SUCCESS if successful, otherwise error.
793  *
794  ******************************************************************************/
795 extern uint16_t AVDT_ReconfigReq(uint8_t handle, AvdtpSepConfig* p_cfg);
796 
797 /*******************************************************************************
798  *
799  * Function         AVDT_SecurityReq
800  *
801  * Description      Send a security request to the peer device.  When the
802  *                  security procedure is completed, an AVDT_SECURITY_CFM_EVT
803  *                  is sent to the application via the control callback function
804  *                  for this handle.  (Please note that AVDTP security
805  *                  procedures are unrelated to Bluetooth link level security.)
806  *
807  *
808  * Returns          AVDT_SUCCESS if successful, otherwise error.
809  *
810  ******************************************************************************/
811 extern uint16_t AVDT_SecurityReq(uint8_t handle, uint8_t* p_data, uint16_t len);
812 
813 /*******************************************************************************
814  *
815  * Function         AVDT_SecurityRsp
816  *
817  * Description      Respond to a security request from the peer device.
818  *                  This function must be called if the application receives
819  *                  an AVDT_SECURITY_IND_EVT through its control callback.
820  *                  (Please note that AVDTP security procedures are unrelated
821  *                  to Bluetooth link level security.)
822  *
823  *
824  * Returns          AVDT_SUCCESS if successful, otherwise error.
825  *
826  ******************************************************************************/
827 extern uint16_t AVDT_SecurityRsp(uint8_t handle, uint8_t label,
828                                  uint8_t error_code, uint8_t* p_data,
829                                  uint16_t len);
830 
831 /*******************************************************************************
832  *
833  * Function         AVDT_WriteReqOpt
834  *
835  * Description      Send a media packet to the peer device.  The stream must
836  *                  be started before this function is called.  Also, this
837  *                  function can only be called if the stream is a SRC
838  *
839  *                  When AVDTP has sent the media packet and is ready for the
840  *                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
841  *                  application via the control callback.  The application must
842  *                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
843  *                  call to AVDT_WriteReq().  If the applications calls
844  *                  AVDT_WriteReq() before it receives the event the packet
845  *                  will not be sent.  The application may make its first call
846  *                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
847  *                  or AVDT_START_IND_EVT.
848  *
849  *                  The application passes the packet using the BT_HDR structure
850  *                  This structure is described in section 2.1.  The offset
851  *                  field must be equal to or greater than AVDT_MEDIA_OFFSET
852  *                  (if NO_RTP is specified, L2CAP_MIN_OFFSET can be used)
853  *                  This allows enough space in the buffer for the L2CAP and
854  *                  AVDTP headers.
855  *
856  *                  The memory pointed to by p_pkt must be a GKI buffer
857  *                  allocated by the application.  This buffer will be freed
858  *                  by the protocol stack; the application must not free
859  *                  this buffer.
860  *
861  *                  The opt parameter allows passing specific options like:
862  *                  - NO_RTP : do not add the RTP header to buffer
863  *
864  * Returns          AVDT_SUCCESS if successful, otherwise error.
865  *
866  ******************************************************************************/
867 extern uint16_t AVDT_WriteReqOpt(uint8_t handle, BT_HDR* p_pkt,
868                                  uint32_t time_stamp, uint8_t m_pt,
869                                  tAVDT_DATA_OPT_MASK opt);
870 
871 /*******************************************************************************
872  *
873  * Function         AVDT_ConnectReq
874  *
875  * Description      This function initiates an AVDTP signaling connection
876  *                  to the peer device.  When the connection is completed, an
877  *                  AVDT_CONNECT_IND_EVT is sent to the application via its
878  *                  control callback function.  If the connection attempt fails
879  *                  an AVDT_DISCONNECT_IND_EVT is sent.  The security mask
880  *                  parameter overrides the outgoing security mask set in
881  *                  AVDT_Register().
882  *
883  * Returns          AVDT_SUCCESS if successful, otherwise error.
884  *
885  ******************************************************************************/
886 extern uint16_t AVDT_ConnectReq(const RawAddress& bd_addr,
887                                 uint8_t channel_index,
888                                 tAVDT_CTRL_CBACK* p_cback);
889 
890 /*******************************************************************************
891  *
892  * Function         AVDT_DisconnectReq
893  *
894  * Description      This function disconnect an AVDTP signaling connection
895  *                  to the peer device.  When disconnected an
896  *                  AVDT_DISCONNECT_IND_EVT is sent to the application via its
897  *                  control callback function.
898  *
899  * Returns          AVDT_SUCCESS if successful, otherwise error.
900  *
901  ******************************************************************************/
902 extern uint16_t AVDT_DisconnectReq(const RawAddress& bd_addr,
903                                    tAVDT_CTRL_CBACK* p_cback);
904 
905 /*******************************************************************************
906  *
907  * Function         AVDT_GetL2CapChannel
908  *
909  * Description      Get the L2CAP CID used by the handle.
910  *
911  * Returns          CID if successful, otherwise 0.
912  *
913  ******************************************************************************/
914 extern uint16_t AVDT_GetL2CapChannel(uint8_t handle);
915 
916 /******************************************************************************
917  *
918  * Function         AVDT_SetTraceLevel
919  *
920  * Description      Sets the trace level for AVDT. If 0xff is passed, the
921  *                  current trace level is returned.
922  *
923  *                  Input Parameters:
924  *                      new_level:  The level to set the AVDT tracing to:
925  *                      0xff-returns the current setting.
926  *                      0-turns off tracing.
927  *                      >= 1-Errors.
928  *                      >= 2-Warnings.
929  *                      >= 3-APIs.
930  *                      >= 4-Events.
931  *                      >= 5-Debug.
932  *
933  * Returns          The new trace level or current trace level if
934  *                  the input parameter is 0xff.
935  *
936  *****************************************************************************/
937 extern uint8_t AVDT_SetTraceLevel(uint8_t new_level);
938 
939 /**
940  * Dump debug-related information for the Stack AVDTP module.
941  *
942  * @param fd the file descriptor to use for writing the ASCII formatted
943  * information
944  */
945 void stack_debug_avdtp_api_dump(int fd);
946 
947 #endif /* AVDT_API_H */
948