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