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