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