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