1 /******************************************************************************
2  *
3  *  Copyright 2004-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 is the private interface file for the BTA advanced audio/video.
22  *
23  ******************************************************************************/
24 #ifndef BTA_AV_INT_H
25 #define BTA_AV_INT_H
26 
27 #include "avdt_api.h"
28 #include "bta_api.h"
29 #include "bta_av_api.h"
30 #include "bta_av_co.h"
31 #include "bta_sys.h"
32 #include "osi/include/list.h"
33 #include "stack/include/a2dp_api.h"
34 
35 /*****************************************************************************
36  *  Constants
37  ****************************************************************************/
38 
39 enum {
40   /* these events are handled by the AV main state machine */
41   BTA_AV_API_DISABLE_EVT = BTA_SYS_EVT_START(BTA_ID_AV),
42   BTA_AV_API_REMOTE_CMD_EVT,
43   BTA_AV_API_VENDOR_CMD_EVT,
44   BTA_AV_API_VENDOR_RSP_EVT,
45   BTA_AV_API_META_RSP_EVT,
46   BTA_AV_API_RC_CLOSE_EVT,
47   BTA_AV_AVRC_OPEN_EVT,
48   BTA_AV_AVRC_MSG_EVT,
49   BTA_AV_AVRC_NONE_EVT,
50 
51   /* these events are handled by the AV stream state machine */
52   BTA_AV_API_OPEN_EVT,
53   BTA_AV_API_CLOSE_EVT,
54   BTA_AV_AP_START_EVT, /* the following 2 events must be in the same order as
55                           the *API_*EVT */
56   BTA_AV_AP_STOP_EVT,
57   BTA_AV_API_RECONFIG_EVT,
58   BTA_AV_API_PROTECT_REQ_EVT,
59   BTA_AV_API_PROTECT_RSP_EVT,
60   BTA_AV_API_RC_OPEN_EVT,
61   BTA_AV_SRC_DATA_READY_EVT,
62   BTA_AV_CI_SETCONFIG_OK_EVT,
63   BTA_AV_CI_SETCONFIG_FAIL_EVT,
64   BTA_AV_SDP_DISC_OK_EVT,
65   BTA_AV_SDP_DISC_FAIL_EVT,
66   BTA_AV_STR_DISC_OK_EVT,
67   BTA_AV_STR_DISC_FAIL_EVT,
68   BTA_AV_STR_GETCAP_OK_EVT,
69   BTA_AV_STR_GETCAP_FAIL_EVT,
70   BTA_AV_STR_OPEN_OK_EVT,
71   BTA_AV_STR_OPEN_FAIL_EVT,
72   BTA_AV_STR_START_OK_EVT,
73   BTA_AV_STR_START_FAIL_EVT,
74   BTA_AV_STR_CLOSE_EVT,
75   BTA_AV_STR_CONFIG_IND_EVT,
76   BTA_AV_STR_SECURITY_IND_EVT,
77   BTA_AV_STR_SECURITY_CFM_EVT,
78   BTA_AV_STR_WRITE_CFM_EVT,
79   BTA_AV_STR_SUSPEND_CFM_EVT,
80   BTA_AV_STR_RECONFIG_CFM_EVT,
81   BTA_AV_AVRC_TIMER_EVT,
82   BTA_AV_AVDT_CONNECT_EVT,
83   BTA_AV_AVDT_DISCONNECT_EVT,
84   BTA_AV_ROLE_CHANGE_EVT,
85   BTA_AV_AVDT_DELAY_RPT_EVT,
86   BTA_AV_ACP_CONNECT_EVT,
87   BTA_AV_API_OFFLOAD_START_EVT,
88   BTA_AV_API_OFFLOAD_START_RSP_EVT,
89 
90   /* these events are handled outside of the state machine */
91   BTA_AV_API_ENABLE_EVT,
92   BTA_AV_API_REGISTER_EVT,
93   BTA_AV_API_DEREGISTER_EVT,
94   BTA_AV_API_DISCONNECT_EVT,
95   BTA_AV_CI_SRC_DATA_READY_EVT,
96   BTA_AV_SIG_CHG_EVT,
97   BTA_AV_SIGNALLING_TIMER_EVT,
98   BTA_AV_SDP_AVRC_DISC_EVT,
99   BTA_AV_AVRC_CLOSE_EVT,
100   BTA_AV_AVRC_BROWSE_OPEN_EVT,
101   BTA_AV_AVRC_BROWSE_CLOSE_EVT,
102   BTA_AV_CONN_CHG_EVT,
103   BTA_AV_DEREG_COMP_EVT,
104   BTA_AV_AVDT_RPT_CONN_EVT,
105   BTA_AV_API_START_EVT, /* the following 2 events must be in the same order as
106                            the *AP_*EVT */
107   BTA_AV_API_STOP_EVT
108 };
109 
110 /* events for AV control block state machine */
111 #define BTA_AV_FIRST_SM_EVT BTA_AV_API_DISABLE_EVT
112 #define BTA_AV_LAST_SM_EVT BTA_AV_AVRC_NONE_EVT
113 
114 /* events for AV stream control block state machine */
115 #define BTA_AV_FIRST_SSM_EVT BTA_AV_API_OPEN_EVT
116 
117 /* events that do not go through state machine */
118 #define BTA_AV_FIRST_NSM_EVT BTA_AV_API_ENABLE_EVT
119 #define BTA_AV_LAST_NSM_EVT BTA_AV_API_STOP_EVT
120 
121 /* API events passed to both SSMs (by bta_av_api_to_ssm) */
122 #define BTA_AV_FIRST_A2S_API_EVT BTA_AV_API_START_EVT
123 #define BTA_AV_FIRST_A2S_SSM_EVT BTA_AV_AP_START_EVT
124 
125 #define BTA_AV_LAST_EVT BTA_AV_API_STOP_EVT
126 
127 /* maximum number of SEPS in stream discovery results */
128 #define BTA_AV_NUM_SEPS 32
129 
130 /* initialization value for AVRC handle */
131 #define BTA_AV_RC_HANDLE_NONE 0xFF
132 
133 /* size of database for service discovery */
134 #define BTA_AV_DISC_BUF_SIZE 2000
135 
136 /* maximum length of AVDTP security data */
137 #define BTA_AV_SECURITY_MAX_LEN 400
138 
139 /* check number of buffers queued at L2CAP when this amount of buffers are
140  * queued to L2CAP */
141 #define BTA_AV_QUEUE_DATA_CHK_NUM L2CAP_HIGH_PRI_MIN_XMIT_QUOTA
142 
143 /* the number of ACL links with AVDT */
144 #define BTA_AV_NUM_LINKS AVDT_NUM_LINKS
145 
146 #define BTA_AV_BE_STREAM_TO_CO_ID(u32, p)                                 \
147   {                                                                       \
148     (u32) = (((uint32_t)(*((p) + 2))) + (((uint32_t)(*((p) + 1))) << 8) + \
149              (((uint32_t)(*(p))) << 16));                                 \
150     (p) += 3;                                                             \
151   }
152 
153 /*****************************************************************************
154  *  Data types
155  ****************************************************************************/
156 
157 /* function types for call-out functions */
158 typedef bool (*tBTA_AV_CO_INIT)(btav_a2dp_codec_index_t codec_index,
159                                 AvdtpSepConfig* p_cfg);
160 typedef void (*tBTA_AV_CO_DISC_RES)(tBTA_AV_HNDL bta_av_handle,
161                                     const RawAddress& peer_addr,
162                                     uint8_t num_seps, uint8_t num_snk,
163                                     uint8_t num_src, uint16_t uuid_local);
164 typedef tA2DP_STATUS (*tBTA_AV_CO_GETCFG)(tBTA_AV_HNDL bta_av_handle,
165                                           const RawAddress& peer_addr,
166                                           uint8_t* p_codec_info,
167                                           uint8_t* p_sep_info_idx, uint8_t seid,
168                                           uint8_t* p_num_protect,
169                                           uint8_t* p_protect_info);
170 typedef void (*tBTA_AV_CO_SETCFG)(tBTA_AV_HNDL bta_av_handle,
171                                   const RawAddress& peer_addr,
172                                   const uint8_t* p_codec_info, uint8_t seid,
173                                   uint8_t num_protect,
174                                   const uint8_t* p_protect_info,
175                                   uint8_t t_local_sep, uint8_t avdt_handle);
176 typedef void (*tBTA_AV_CO_OPEN)(tBTA_AV_HNDL bta_av_handle,
177                                 const RawAddress& peer_addr, uint16_t mtu);
178 typedef void (*tBTA_AV_CO_CLOSE)(tBTA_AV_HNDL bta_av_handle,
179                                  const RawAddress& peer_addr);
180 typedef void (*tBTA_AV_CO_START)(tBTA_AV_HNDL bta_av_handle,
181                                  const RawAddress& peer_addr,
182                                  const uint8_t* p_codec_info,
183                                  bool* p_no_rtp_header);
184 typedef void (*tBTA_AV_CO_STOP)(tBTA_AV_HNDL bta_av_handle,
185                                 const RawAddress& peer_addr);
186 typedef BT_HDR* (*tBTA_AV_CO_DATAPATH)(const uint8_t* p_codec_info,
187                                        uint32_t* p_timestamp);
188 typedef void (*tBTA_AV_CO_DELAY)(tBTA_AV_HNDL bta_av_handle,
189                                  const RawAddress& peer_addr, uint16_t delay);
190 typedef void (*tBTA_AV_CO_UPDATE_MTU)(tBTA_AV_HNDL bta_av_handle,
191                                       const RawAddress& peer_addr,
192                                       uint16_t mtu);
193 
194 typedef bool (*tBTA_AV_CO_CONTENT_PROTECT_IS_ACTIVE)(
195     const RawAddress& peer_addr);
196 
197 /* the call-out functions for one stream */
198 typedef struct {
199   tBTA_AV_CO_INIT init;
200   tBTA_AV_CO_DISC_RES disc_res;
201   tBTA_AV_CO_GETCFG getcfg;
202   tBTA_AV_CO_SETCFG setcfg;
203   tBTA_AV_CO_OPEN open;
204   tBTA_AV_CO_CLOSE close;
205   tBTA_AV_CO_START start;
206   tBTA_AV_CO_STOP stop;
207   tBTA_AV_CO_DATAPATH data;
208   tBTA_AV_CO_DELAY delay;
209   tBTA_AV_CO_UPDATE_MTU update_mtu;
210   tBTA_AV_CO_CONTENT_PROTECT_IS_ACTIVE cp_is_active;
211 } tBTA_AV_CO_FUNCTS;
212 
213 /* data type for BTA_AV_API_ENABLE_EVT */
214 typedef struct {
215   BT_HDR hdr;
216   tBTA_AV_CBACK* p_cback;
217   tBTA_AV_FEAT features;
218   tBTA_SEC sec_mask;
219 } tBTA_AV_API_ENABLE;
220 
221 /* data type for BTA_AV_API_REGISTER_EVT */
222 typedef struct {
223   BT_HDR hdr;
224   char p_service_name[BTA_SERVICE_NAME_LEN + 1];
225   uint8_t app_id;
226   tBTA_AV_SINK_DATA_CBACK* p_app_sink_data_cback;
227   uint16_t service_uuid;
228 } tBTA_AV_API_REG;
229 
230 enum {
231   BTA_AV_RS_NONE, /* straight API call */
232   BTA_AV_RS_OK,   /* the role switch result - successful */
233   BTA_AV_RS_FAIL, /* the role switch result - failed */
234   BTA_AV_RS_DONE  /* the role switch is done - continue */
235 };
236 typedef uint8_t tBTA_AV_RS_RES;
237 /* data type for BTA_AV_API_OPEN_EVT */
238 typedef struct {
239   BT_HDR hdr;
240   RawAddress bd_addr;
241   bool use_rc;
242   tBTA_SEC sec_mask;
243   tBTA_AV_RS_RES switch_res;
244   uint16_t uuid; /* uuid of initiator */
245 } tBTA_AV_API_OPEN;
246 
247 /* data type for BTA_AV_API_STOP_EVT */
248 typedef struct {
249   BT_HDR hdr;
250   bool suspend;
251   bool flush;
252   bool reconfig_stop;  // True if the stream is stopped for reconfiguration
253 } tBTA_AV_API_STOP;
254 
255 /* data type for BTA_AV_API_DISCONNECT_EVT */
256 typedef struct {
257   BT_HDR hdr;
258   RawAddress bd_addr;
259 } tBTA_AV_API_DISCNT;
260 
261 /* data type for BTA_AV_API_PROTECT_REQ_EVT */
262 typedef struct {
263   BT_HDR hdr;
264   uint8_t* p_data;
265   uint16_t len;
266 } tBTA_AV_API_PROTECT_REQ;
267 
268 /* data type for BTA_AV_API_PROTECT_RSP_EVT */
269 typedef struct {
270   BT_HDR hdr;
271   uint8_t* p_data;
272   uint16_t len;
273   uint8_t error_code;
274 } tBTA_AV_API_PROTECT_RSP;
275 
276 /* data type for BTA_AV_API_REMOTE_CMD_EVT */
277 typedef struct {
278   BT_HDR hdr;
279   tAVRC_MSG_PASS msg;
280   uint8_t label;
281 } tBTA_AV_API_REMOTE_CMD;
282 
283 /* data type for BTA_AV_API_VENDOR_CMD_EVT and RSP */
284 typedef struct {
285   BT_HDR hdr;
286   tAVRC_MSG_VENDOR msg;
287   uint8_t label;
288 } tBTA_AV_API_VENDOR;
289 
290 /* data type for BTA_AV_API_RC_OPEN_EVT */
291 typedef struct { BT_HDR hdr; } tBTA_AV_API_OPEN_RC;
292 
293 /* data type for BTA_AV_API_RC_CLOSE_EVT */
294 typedef struct { BT_HDR hdr; } tBTA_AV_API_CLOSE_RC;
295 
296 /* data type for BTA_AV_API_META_RSP_EVT */
297 typedef struct {
298   BT_HDR hdr;
299   bool is_rsp;
300   uint8_t label;
301   tBTA_AV_CODE rsp_code;
302   BT_HDR* p_pkt;
303 } tBTA_AV_API_META_RSP;
304 
305 /* data type for BTA_AV_API_RECONFIG_EVT */
306 typedef struct {
307   BT_HDR hdr;
308   uint8_t codec_info[AVDT_CODEC_SIZE]; /* codec configuration */
309   uint8_t* p_protect_info;
310   uint8_t num_protect;
311   bool suspend;
312   uint8_t sep_info_idx;
313 } tBTA_AV_API_RCFG;
314 
315 /* data type for BTA_AV_CI_SETCONFIG_OK_EVT and BTA_AV_CI_SETCONFIG_FAIL_EVT */
316 typedef struct {
317   BT_HDR hdr;
318   tBTA_AV_HNDL hndl;
319   uint8_t err_code;
320   uint8_t category;
321   uint8_t num_seid;
322   uint8_t* p_seid;
323   bool recfg_needed;
324   uint8_t avdt_handle; /* local sep type for which this stream will be set up */
325 } tBTA_AV_CI_SETCONFIG;
326 
327 /* data type for all stream events from AVDTP */
328 typedef struct {
329   BT_HDR hdr;
330   AvdtpSepConfig cfg; /* configuration/capabilities parameters */
331   tAVDT_CTRL msg;  /* AVDTP callback message parameters */
332   RawAddress bd_addr; /* bd address */
333   uint8_t scb_index;
334   uint8_t handle;
335   uint8_t avdt_event;
336   bool initiator; /* true, if local device initiates the SUSPEND */
337 } tBTA_AV_STR_MSG;
338 
339 /* data type for BTA_AV_AVRC_MSG_EVT */
340 typedef struct {
341   BT_HDR hdr;
342   tAVRC_MSG msg;
343   uint8_t handle;
344   uint8_t label;
345   uint8_t opcode;
346 } tBTA_AV_RC_MSG;
347 
348 /* data type for BTA_AV_AVRC_OPEN_EVT, BTA_AV_AVRC_CLOSE_EVT */
349 typedef struct {
350   BT_HDR hdr;
351   RawAddress peer_addr;
352   uint8_t handle;
353 } tBTA_AV_RC_CONN_CHG;
354 
355 /* data type for BTA_AV_CONN_CHG_EVT */
356 typedef struct {
357   BT_HDR hdr;
358   RawAddress peer_addr;
359   bool is_up;
360 } tBTA_AV_CONN_CHG;
361 
362 /* data type for BTA_AV_ROLE_CHANGE_EVT */
363 typedef struct {
364   BT_HDR hdr;
365   uint8_t new_role;
366   uint8_t hci_status;
367 } tBTA_AV_ROLE_RES;
368 
369 /* data type for BTA_AV_SDP_DISC_OK_EVT */
370 typedef struct {
371   BT_HDR hdr;
372 } tBTA_AV_SDP_RES;
373 
374 /* data type for BTA_AV_API_OFFLOAD_RSP_EVT */
375 typedef struct {
376   BT_HDR hdr;
377   tBTA_AV_STATUS status;
378 } tBTA_AV_API_STATUS_RSP;
379 
380 /* type for SEP control block */
381 typedef struct {
382   uint8_t av_handle;                   /* AVDTP handle */
383   uint8_t tsep;                        /* SEP type of local SEP */
384   uint8_t codec_info[AVDT_CODEC_SIZE]; /* Codec info */
385   tBTA_AV_SINK_DATA_CBACK*
386       p_app_sink_data_cback; /* Sink application callback for media packets */
387 } tBTA_AV_SEP;
388 
389 /* initiator/acceptor role for adaption */
390 #define BTA_AV_ROLE_AD_INT 0x00 /* initiator */
391 #define BTA_AV_ROLE_AD_ACP 0x01 /* acceptor */
392 
393 /* initiator/acceptor signaling roles */
394 #define BTA_AV_ROLE_START_ACP 0x00
395 #define BTA_AV_ROLE_START_INT 0x10 /* do not change this value */
396 
397 #define BTA_AV_ROLE_SUSPEND 0x20     /* suspending on start */
398 #define BTA_AV_ROLE_SUSPEND_OPT 0x40 /* Suspend on Start option is set */
399 
400 /* union of all event datatypes */
401 union tBTA_AV_DATA {
402   BT_HDR hdr;
403   tBTA_AV_API_ENABLE api_enable;
404   tBTA_AV_API_REG api_reg;
405   tBTA_AV_API_OPEN api_open;
406   tBTA_AV_API_STOP api_stop;
407   tBTA_AV_API_DISCNT api_discnt;
408   tBTA_AV_API_PROTECT_REQ api_protect_req;
409   tBTA_AV_API_PROTECT_RSP api_protect_rsp;
410   tBTA_AV_API_REMOTE_CMD api_remote_cmd;
411   tBTA_AV_API_VENDOR api_vendor;
412   tBTA_AV_API_RCFG api_reconfig;
413   tBTA_AV_CI_SETCONFIG ci_setconfig;
414   tBTA_AV_STR_MSG str_msg;
415   tBTA_AV_RC_MSG rc_msg;
416   tBTA_AV_RC_CONN_CHG rc_conn_chg;
417   tBTA_AV_CONN_CHG conn_chg;
418   tBTA_AV_ROLE_RES role_res;
419   tBTA_AV_SDP_RES sdp_res;
420   tBTA_AV_API_META_RSP api_meta_rsp;
421   tBTA_AV_API_STATUS_RSP api_status_rsp;
422 };
423 
424 typedef union {
425   tBTA_AV_API_OPEN open; /* used only before open and role switch
426                             is needed on another AV channel */
427 } tBTA_AV_Q_INFO;
428 
429 #define BTA_AV_Q_TAG_OPEN 0x01   /* after API_OPEN, before STR_OPENED */
430 #define BTA_AV_Q_TAG_START 0x02  /* before start sending media packets */
431 #define BTA_AV_Q_TAG_STREAM 0x03 /* during streaming */
432 
433 #define BTA_AV_WAIT_ACP_CAPS_ON 0x01 /* retriving the peer capabilities */
434 #define BTA_AV_WAIT_ACP_CAPS_STARTED \
435   0x02 /* started while retriving peer capabilities */
436 #define BTA_AV_WAIT_ROLE_SW_RES_OPEN \
437   0x04 /* waiting for role switch result after API_OPEN, before STR_OPENED */
438 #define BTA_AV_WAIT_ROLE_SW_RES_START \
439   0x08 /* waiting for role switch result before streaming */
440 #define BTA_AV_WAIT_ROLE_SW_STARTED \
441   0x10 /* started while waiting for role switch result */
442 #define BTA_AV_WAIT_ROLE_SW_RETRY 0x20 /* set when retry on timeout */
443 #define BTA_AV_WAIT_CHECK_RC \
444   0x40 /* set when the timer is used by role switch */
445 #define BTA_AV_WAIT_ROLE_SW_FAILED 0x80 /* role switch failed */
446 
447 #define BTA_AV_WAIT_ROLE_SW_BITS                                  \
448   (BTA_AV_WAIT_ROLE_SW_RES_OPEN | BTA_AV_WAIT_ROLE_SW_RES_START | \
449    BTA_AV_WAIT_ROLE_SW_STARTED | BTA_AV_WAIT_ROLE_SW_RETRY)
450 
451 /* Bitmap for collision, coll_mask */
452 #define BTA_AV_COLL_INC_TMR \
453   0x01 /* Timer is running for incoming L2C connection */
454 #define BTA_AV_COLL_API_CALLED \
455   0x02 /* API open was called while incoming timer is running */
456 
457 /* type for AV stream control block */
458 // TODO: This should be renamed and changed to a proper class
459 struct tBTA_AV_SCB final {
460  public:
461   const tBTA_AV_ACT* p_act_tbl; /* the action table for stream state machine */
462   const tBTA_AV_CO_FUNCTS* p_cos; /* the associated callout functions */
463   bool sdp_discovery_started; /* variable to determine whether SDP is started */
464   tBTA_AV_SEP seps[BTAV_A2DP_CODEC_INDEX_MAX];
465   AvdtpSepConfig peer_cap; /* buffer used for get capabilities */
466   list_t* a2dp_list; /* used for audio channels only */
467   tBTA_AV_Q_INFO q_info;
468   tAVDT_SEP_INFO sep_info[BTA_AV_NUM_SEPS]; /* stream discovery results */
469   AvdtpSepConfig cfg;                       /* local SEP configuration */
470   alarm_t* avrc_ct_timer;                   /* delay timer for AVRC CT */
471   uint16_t l2c_cid;                         /* L2CAP channel ID */
472   uint16_t stream_mtu;                      /* MTU of stream */
473   tBTA_SEC sec_mask;          /* security mask */
474   uint8_t media_type;         /* Media type: AVDT_MEDIA_TYPE_* */
475   bool cong;                  /* true if AVDTP congested */
476   tBTA_AV_STATUS open_status; /* open failure status */
477   tBTA_AV_CHNL chnl;          /* the channel: audio/video */
478   tBTA_AV_HNDL hndl;          /* the handle: ((hdi + 1)|chnl) */
479   uint16_t cur_psc_mask;      /* Protocol service capabilities mask for current
480                                  connection */
481   uint8_t avdt_handle;        /* AVDTP handle */
482   uint8_t hdi;                /* the index to SCB[] */
483   uint8_t num_seps;           /* number of seps returned by stream discovery */
484   uint8_t num_disc_snks;      /* number of discovered snks */
485   uint8_t num_disc_srcs;      /* number of discovered srcs */
486   uint8_t sep_info_idx;       /* current index into sep_info */
487   uint8_t sep_idx;            /* current index into local seps[] */
488   uint8_t rcfg_idx;           /* reconfig requested index into sep_info */
489   uint8_t state;              /* state machine state */
490   uint8_t avdt_label;         /* AVDTP label */
491   uint8_t app_id;             /* application id */
492   uint8_t num_recfg;          /* number of reconfigure sent */
493   uint8_t role;
494   uint8_t l2c_bufs;  /* the number of buffers queued to L2CAP */
495   uint8_t rc_handle; /* connected AVRCP handle */
496   bool use_rc;       /* true if AVRCP is allowed */
497   bool started;      /* true if stream started */
498   bool use_rtp_header_marker_bit; /* true if the encoded data packets have RTP
499                                    * headers, and the Marker bit in the header
500                                    * is set according to RFC 6416 */
501   uint8_t
502       co_started;    /* non-zero, if stream started from call-out perspective */
503   bool recfg_sup;    /* true if the first attempt to reconfigure the stream was
504                         successfull, else False if command fails */
505   bool suspend_sup;  /* true if Suspend stream is supported, else false if
506                         suspend command fails */
507   bool deregistering; /* true if deregistering */
508   bool sco_suspend;  /* true if SUSPEND is issued automatically for SCO */
509   uint8_t coll_mask; /* Mask to check incoming and outgoing collision */
510   tBTA_AV_API_OPEN open_api; /* Saved OPEN api message */
511   uint8_t wait;  /* set 0x1, when getting Caps as ACP, set 0x2, when started */
512   uint8_t q_tag; /* identify the associated q_info union member */
513   bool no_rtp_header; /* true if add no RTP header */
514   uint16_t uuid_int; /*intended UUID of Initiator to connect to */
515   bool offload_start_pending;
516   bool offload_started;
517 
518   /**
519    * Called to setup the state when connected to a peer.
520    *
521    * @param peer_address the peer address
522    */
523   void OnConnected(const RawAddress& peer_address);
524 
525   /**
526    * Called to clear the state when disconnected from a peer.
527    *
528    */
529   void OnDisconnected();
530 
531   /**
532    * Get the peer address.
533    */
PeerAddressfinal534   const RawAddress& PeerAddress() const { return peer_address_; }
535 
536   /**
537    * Get the AVDTP version of the peer device.
538    */
AvdtpVersionfinal539   uint16_t AvdtpVersion() const { return avdtp_version_; }
540 
541   /**
542    * Set the AVDTP version of the peer device.
543    *
544    * @param avdtp_version the AVDTP version to use
545    */
546   void SetAvdtpVersion(uint16_t avdtp_version);
547 
548   /**
549    * Check whether the entry is assigned and currenty used.
550    *
551    * @return true if the entry is assigned and currently used
552    */
IsAssignedfinal553   bool IsAssigned() const { return !peer_address_.IsEmpty(); }
554 
555  private:
556   RawAddress peer_address_;  // Peer address
557   uint16_t avdtp_version_;   // The AVDTP version of the peer device
558 };
559 
560 #define BTA_AV_RC_ROLE_MASK 0x10
561 #define BTA_AV_RC_ROLE_INT 0x00
562 #define BTA_AV_RC_ROLE_ACP 0x10
563 
564 #define BTA_AV_RC_CONN_MASK 0x20
565 
566 /* type for AV RCP control block */
567 /* index to this control block is the rc handle */
568 typedef struct {
569   uint8_t status;
570   uint8_t handle;
571   uint8_t shdl;               /* stream handle (hdi + 1) */
572   uint8_t lidx;               /* (index+1) to LCB */
573   tBTA_AV_FEAT peer_features; /* peer features mask */
574   uint16_t cover_art_psm;     /* BIP PSM for cover art feature */
575 } tBTA_AV_RCB;
576 #define BTA_AV_NUM_RCB (BTA_AV_NUM_STRS + 2)
577 
578 enum { BTA_AV_LCB_FREE, BTA_AV_LCB_FIND };
579 
580 /* type for AV ACL Link control block */
581 typedef struct {
582   RawAddress addr;  /* peer BD address */
583   uint8_t conn_msk; /* handle mask of connected stream handle */
584   uint8_t lidx;     /* index + 1 */
585 } tBTA_AV_LCB;
586 
587 /* type for stream state machine action functions */
588 typedef void (*tBTA_AV_SACT)(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
589 
590 /* type for AV control block */
591 typedef struct {
592   tBTA_AV_SCB* p_scb[BTA_AV_NUM_STRS];   /* stream control block */
593   tSDP_DISCOVERY_DB* p_disc_db;          /* pointer to discovery database */
594   tBTA_AV_CBACK* p_cback;                /* application callback function */
595   tBTA_AV_RCB rcb[BTA_AV_NUM_RCB];       /* RCB control block */
596   tBTA_AV_LCB lcb[BTA_AV_NUM_LINKS + 1]; /* link control block */
597   alarm_t* link_signalling_timer;
598   alarm_t*
599       accept_signalling_timer;  /* timer to monitor signalling when accepting */
600   uint32_t sdp_a2dp_handle;     /* SDP record handle for audio src */
601   uint32_t sdp_a2dp_snk_handle; /* SDP record handle for audio snk */
602   tBTA_AV_FEAT features;        /* features mask */
603   tBTA_SEC sec_mask;            /* security mask */
604   tBTA_AV_HNDL handle;          /* the handle for SDP activity */
605   bool disabling;               /* true if api disabled called */
606   uint8_t enabling_attempts;    // counter to wait for previous disabling
607   uint8_t
608       disc; /* (hdi+1) or (rc_handle|BTA_AV_CHNL_MSK) if p_disc_db is in use */
609   uint8_t state;          /* state machine state */
610   uint8_t conn_audio;     /* handle mask of connected audio channels */
611   uint8_t conn_lcb;       /* index mask of used LCBs */
612   uint8_t audio_open_cnt; /* number of connected audio channels */
613   uint8_t reg_audio;      /* handle mask of registered audio channels */
614   uint8_t rc_acp_handle;
615   uint8_t rc_acp_idx; /* (index + 1) to RCB */
616   uint8_t rs_idx;    /* (index + 1) to SCB for the one waiting for RS on open */
617   bool sco_occupied; /* true if SCO is being used or call is in progress */
618   uint8_t audio_streams; /* handle mask of streaming audio channels */
619 } tBTA_AV_CB;
620 
621 // total attempts are half seconds
622 constexpr uint32_t kEnablingAttemptsIntervalMs = 100;
623 constexpr uint8_t kEnablingAttemptsCountMaximum = 5;
624 
625 // A2DP offload VSC parameters
626 class tBT_A2DP_OFFLOAD {
627  public:
628   uint32_t codec_type;            /* codec types ex: SBC/AAC/LDAC/APTx */
629   uint16_t max_latency;           /* maximum latency */
630   uint16_t scms_t_enable;         /* content protection enable */
631   uint32_t sample_rate;           /* Sample rates ex: 44.1/48/88.2/96 Khz */
632   uint8_t bits_per_sample;        /* bits per sample ex: 16/24/32 */
633   uint8_t ch_mode;                /* None:0 Left:1 Right:2 */
634   uint32_t encoded_audio_bitrate; /* encoder audio bitrates */
635   uint16_t acl_hdl;               /* connection handle */
636   uint16_t l2c_rcid;              /* l2cap channel id */
637   uint16_t mtu;                   /* MTU size */
638   uint8_t codec_info[32];         /* Codec specific information */
639 };
640 
641 /* Vendor OFFLOAD VSC */
642 #define HCI_VSQC_CONTROLLER_A2DP_OPCODE 0x000A
643 
644 #define VS_HCI_A2DP_OFFLOAD_START 0x01
645 #define VS_HCI_A2DP_OFFLOAD_STOP 0x02
646 /*****************************************************************************
647  *  Global data
648  ****************************************************************************/
649 
650 /* control block declaration */
651 extern tBTA_AV_CB bta_av_cb;
652 
653 /* config struct */
654 extern const tBTA_AV_CFG* p_bta_av_cfg;
655 extern const tBTA_AV_CFG bta_avk_cfg;
656 extern const tBTA_AV_CFG bta_av_cfg;
657 extern const tBTA_AV_CFG bta_av_cfg_compatibility;
658 
659 /* rc id config struct */
660 extern uint16_t* p_bta_av_rc_id;
661 extern uint16_t* p_bta_av_rc_id_ac;
662 
663 extern const tBTA_AV_SACT bta_av_a2dp_action[];
664 extern const tBTA_AV_CO_FUNCTS bta_av_a2dp_cos;
665 extern void bta_av_sink_data_cback(uint8_t handle, BT_HDR* p_pkt,
666                                    uint32_t time_stamp, uint8_t m_pt);
667 
668 /*****************************************************************************
669  *  Function prototypes
670  ****************************************************************************/
671 /* utility functions */
672 extern tBTA_AV_SCB* bta_av_hndl_to_scb(uint16_t handle);
673 tBTA_AV_SCB* bta_av_addr_to_scb(const RawAddress& bd_addr);
674 extern bool bta_av_chk_start(tBTA_AV_SCB* p_scb);
675 extern void bta_av_restore_switch(void);
676 extern void bta_av_conn_cback(uint8_t handle, const RawAddress& bd_addr,
677                               uint8_t event, tAVDT_CTRL* p_data,
678                               uint8_t scb_index);
679 extern uint8_t bta_av_rc_create(tBTA_AV_CB* p_cb, uint8_t role, uint8_t shdl,
680                                 uint8_t lidx);
681 extern void bta_av_stream_chg(tBTA_AV_SCB* p_scb, bool started);
682 extern bool bta_av_is_scb_opening(tBTA_AV_SCB* p_scb);
683 extern bool bta_av_is_scb_incoming(tBTA_AV_SCB* p_scb);
684 extern void bta_av_set_scb_sst_init(tBTA_AV_SCB* p_scb);
685 extern bool bta_av_is_scb_init(tBTA_AV_SCB* p_scb);
686 extern void bta_av_set_scb_sst_incoming(tBTA_AV_SCB* p_scb);
687 extern tBTA_AV_LCB* bta_av_find_lcb(const RawAddress& addr, uint8_t op);
688 extern const char* bta_av_sst_code(uint8_t state);
689 extern void bta_av_free_scb(tBTA_AV_SCB* p_scb);
690 
691 /* main functions */
692 extern void bta_av_api_deregister(tBTA_AV_DATA* p_data);
693 extern void bta_av_dup_audio_buf(tBTA_AV_SCB* p_scb, BT_HDR* p_buf);
694 extern void bta_av_sm_execute(tBTA_AV_CB* p_cb, uint16_t event,
695                               tBTA_AV_DATA* p_data);
696 extern void bta_av_ssm_execute(tBTA_AV_SCB* p_scb, uint16_t event,
697                                tBTA_AV_DATA* p_data);
698 extern bool bta_av_hdl_event(BT_HDR* p_msg);
699 extern const char* bta_av_evt_code(uint16_t evt_code);
700 extern bool bta_av_switch_if_needed(tBTA_AV_SCB* p_scb);
701 extern bool bta_av_link_role_ok(tBTA_AV_SCB* p_scb, uint8_t bits);
702 
703 /* nsm action functions */
704 extern void bta_av_api_disconnect(tBTA_AV_DATA* p_data);
705 extern void bta_av_sig_chg(tBTA_AV_DATA* p_data);
706 extern void bta_av_signalling_timer(tBTA_AV_DATA* p_data);
707 extern void bta_av_rc_disc_done(tBTA_AV_DATA* p_data);
708 extern void bta_av_rc_closed(tBTA_AV_DATA* p_data);
709 extern void bta_av_rc_browse_opened(tBTA_AV_DATA* p_data);
710 extern void bta_av_rc_browse_closed(tBTA_AV_DATA* p_data);
711 extern void bta_av_rc_disc(uint8_t disc);
712 extern void bta_av_conn_chg(tBTA_AV_DATA* p_data);
713 extern void bta_av_dereg_comp(tBTA_AV_DATA* p_data);
714 
715 /* sm action functions */
716 extern void bta_av_disable(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
717 extern void bta_av_rc_opened(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
718 extern void bta_av_rc_remote_cmd(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
719 extern void bta_av_rc_vendor_cmd(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
720 extern void bta_av_rc_vendor_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
721 extern void bta_av_rc_msg(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
722 extern void bta_av_rc_close(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
723 extern void bta_av_rc_meta_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
724 extern void bta_av_rc_free_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
725 extern void bta_av_rc_free_browse_msg(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
726 
727 extern tBTA_AV_RCB* bta_av_get_rcb_by_shdl(uint8_t shdl);
728 extern void bta_av_del_rc(tBTA_AV_RCB* p_rcb);
729 
730 extern void bta_av_proc_stream_evt(uint8_t handle, const RawAddress& bd_addr,
731                                    uint8_t event, tAVDT_CTRL* p_data,
732                                    uint8_t scb_index);
733 
734 /* ssm action functions */
735 extern void bta_av_do_disc_a2dp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
736 extern void bta_av_cleanup(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
737 extern void bta_av_free_sdb(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
738 extern void bta_av_config_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
739 extern void bta_av_disconnect_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
740 extern void bta_av_security_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
741 extern void bta_av_security_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
742 extern void bta_av_setconfig_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
743 extern void bta_av_str_opened(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
744 extern void bta_av_security_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
745 extern void bta_av_security_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
746 extern void bta_av_do_close(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
747 extern void bta_av_connect_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
748 extern void bta_av_sdp_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
749 extern void bta_av_disc_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
750 extern void bta_av_disc_res_as_acp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
751 extern void bta_av_open_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
752 extern void bta_av_getcap_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
753 extern void bta_av_setconfig_rej(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
754 extern void bta_av_discover_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
755 extern void bta_av_conn_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
756 extern void bta_av_do_start(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
757 extern void bta_av_str_stopped(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
758 extern void bta_av_reconfig(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
759 extern void bta_av_data_path(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
760 extern void bta_av_start_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
761 extern void bta_av_start_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
762 extern void bta_av_str_closed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
763 extern void bta_av_clr_cong(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
764 extern void bta_av_suspend_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
765 extern void bta_av_rcfg_str_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
766 extern void bta_av_rcfg_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
767 extern void bta_av_rcfg_connect(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
768 extern void bta_av_rcfg_discntd(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
769 extern void bta_av_suspend_cont(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
770 extern void bta_av_rcfg_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
771 extern void bta_av_rcfg_open(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
772 extern void bta_av_security_rej(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
773 extern void bta_av_open_rc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
774 extern void bta_av_chk_2nd_start(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
775 extern void bta_av_save_caps(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
776 extern void bta_av_rej_conn(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
777 extern void bta_av_rej_conn(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
778 extern void bta_av_set_use_rc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
779 extern void bta_av_cco_close(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
780 extern void bta_av_switch_role(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
781 extern void bta_av_role_res(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
782 extern void bta_av_delay_co(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
783 extern void bta_av_open_at_inc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
784 extern void bta_av_offload_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
785 extern void bta_av_offload_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data);
786 extern void bta_av_vendor_offload_stop(void);
787 
788 #endif /* BTA_AV_INT_H */
789