• 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 main implementation file for the BTA advanced audio/video.
22  *
23  ******************************************************************************/
24 
25 #define LOG_TAG "bt_bta_av"
26 
27 #include <base/logging.h>
28 #include <string.h>
29 
30 #include "bt_target.h"
31 #include "osi/include/log.h"
32 #include "osi/include/osi.h"
33 #include "osi/include/properties.h"
34 
35 #include "bta_av_co.h"
36 #include "bta_av_int.h"
37 #include "btif/include/btif_av_co.h"
38 #include "btif/include/btif_config.h"
39 #include "l2c_api.h"
40 #include "l2cdefs.h"
41 #include "utl.h"
42 
43 #if (BTA_AR_INCLUDED == TRUE)
44 #include "bta_ar_api.h"
45 #endif
46 
47 /*****************************************************************************
48  * Constants and types
49  ****************************************************************************/
50 
51 #ifndef BTA_AV_RET_TOUT
52 #define BTA_AV_RET_TOUT 4
53 #endif
54 
55 #ifndef BTA_AV_SIG_TOUT
56 #define BTA_AV_SIG_TOUT 4
57 #endif
58 
59 #ifndef BTA_AV_IDLE_TOUT
60 #define BTA_AV_IDLE_TOUT 10
61 #endif
62 
63 /* the delay time in milliseconds to retry role switch */
64 #ifndef BTA_AV_RS_TIME_VAL
65 #define BTA_AV_RS_TIME_VAL 1000
66 #endif
67 
68 #ifndef AVRCP_VERSION_PROPERTY
69 #define AVRCP_VERSION_PROPERTY "persist.bluetooth.avrcpversion"
70 #endif
71 
72 #ifndef AVRCP_1_6_STRING
73 #define AVRCP_1_6_STRING "avrcp16"
74 #endif
75 
76 #ifndef AVRCP_1_5_STRING
77 #define AVRCP_1_5_STRING "avrcp15"
78 #endif
79 
80 #ifndef AVRCP_1_4_STRING
81 #define AVRCP_1_4_STRING "avrcp14"
82 #endif
83 
84 #ifndef AVRCP_1_3_STRING
85 #define AVRCP_1_3_STRING "avrcp13"
86 #endif
87 
88 #ifndef AVRCP_DEFAULT_VERSION
89 #define AVRCP_DEFAULT_VERSION AVRCP_1_5_STRING
90 #endif
91 
92 /* state machine states */
93 enum { BTA_AV_INIT_ST, BTA_AV_OPEN_ST };
94 
95 /* state machine action enumeration list */
96 enum {
97   BTA_AV_DISABLE,
98   BTA_AV_RC_OPENED,
99   BTA_AV_RC_REMOTE_CMD,
100   BTA_AV_RC_VENDOR_CMD,
101   BTA_AV_RC_VENDOR_RSP,
102   BTA_AV_RC_FREE_RSP,
103   BTA_AV_RC_FREE_BROWSE_MSG,
104   BTA_AV_RC_META_RSP,
105   BTA_AV_RC_MSG,
106   BTA_AV_RC_CLOSE,
107   BTA_AV_RC_BROWSE_CLOSE,
108   BTA_AV_NUM_ACTIONS
109 };
110 
111 #define BTA_AV_IGNORE BTA_AV_NUM_ACTIONS
112 
113 /* type for action functions */
114 typedef void (*tBTA_AV_ACTION)(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
115 
116 /* action functions */
117 const tBTA_AV_ACTION bta_av_action[] = {
118     bta_av_disable,
119     bta_av_rc_opened,
120     bta_av_rc_remote_cmd,
121     bta_av_rc_vendor_cmd,
122     bta_av_rc_vendor_rsp,
123     bta_av_rc_free_rsp,
124     bta_av_rc_free_browse_msg,
125     bta_av_rc_meta_rsp,
126     bta_av_rc_msg,
127     bta_av_rc_close,
128 };
129 
130 /* state table information */
131 #define BTA_AV_ACTION_COL 0 /* position of actions */
132 #define BTA_AV_NEXT_STATE 1 /* position of next state */
133 #define BTA_AV_NUM_COLS 2   /* number of columns in state tables */
134 
135 /* state table for init state */
136 static const uint8_t bta_av_st_init[][BTA_AV_NUM_COLS] = {
137     /* Event                     Action 1                   Next state */
138     /* API_DISABLE_EVT */ {BTA_AV_DISABLE, BTA_AV_INIT_ST},
139     /* API_REMOTE_CMD_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
140     /* API_VENDOR_CMD_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
141     /* API_VENDOR_RSP_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
142     /* API_META_RSP_EVT */ {BTA_AV_RC_FREE_RSP, BTA_AV_INIT_ST},
143     /* API_RC_CLOSE_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
144     /* AVRC_OPEN_EVT */ {BTA_AV_RC_OPENED, BTA_AV_OPEN_ST},
145     /* AVRC_MSG_EVT */ {BTA_AV_RC_FREE_BROWSE_MSG, BTA_AV_INIT_ST},
146     /* AVRC_NONE_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
147 };
148 
149 /* state table for open state */
150 static const uint8_t bta_av_st_open[][BTA_AV_NUM_COLS] = {
151     /* Event                     Action 1                   Next state */
152     /* API_DISABLE_EVT */ {BTA_AV_DISABLE, BTA_AV_INIT_ST},
153     /* API_REMOTE_CMD_EVT */ {BTA_AV_RC_REMOTE_CMD, BTA_AV_OPEN_ST},
154     /* API_VENDOR_CMD_EVT */ {BTA_AV_RC_VENDOR_CMD, BTA_AV_OPEN_ST},
155     /* API_VENDOR_RSP_EVT */ {BTA_AV_RC_VENDOR_RSP, BTA_AV_OPEN_ST},
156     /* API_META_RSP_EVT */ {BTA_AV_RC_META_RSP, BTA_AV_OPEN_ST},
157     /* API_RC_CLOSE_EVT */ {BTA_AV_RC_CLOSE, BTA_AV_OPEN_ST},
158     /* AVRC_OPEN_EVT */ {BTA_AV_RC_OPENED, BTA_AV_OPEN_ST},
159     /* AVRC_MSG_EVT */ {BTA_AV_RC_MSG, BTA_AV_OPEN_ST},
160     /* AVRC_NONE_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
161 };
162 
163 /* type for state table */
164 typedef const uint8_t (*tBTA_AV_ST_TBL)[BTA_AV_NUM_COLS];
165 
166 /* state table */
167 static const tBTA_AV_ST_TBL bta_av_st_tbl[] = {bta_av_st_init, bta_av_st_open};
168 
169 typedef void (*tBTA_AV_NSM_ACT)(tBTA_AV_DATA* p_data);
170 static void bta_av_api_enable(tBTA_AV_DATA* p_data);
171 static void bta_av_api_register(tBTA_AV_DATA* p_data);
172 static void bta_av_ci_data(tBTA_AV_DATA* p_data);
173 static void bta_av_rpc_conn(tBTA_AV_DATA* p_data);
174 static void bta_av_api_to_ssm(tBTA_AV_DATA* p_data);
175 
176 static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
177                                  uint8_t app_id, const RawAddress& peer_addr);
178 static void bta_av_sys_rs_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
179                                 uint8_t app_id, const RawAddress& peer_addr);
180 
181 /* action functions */
182 const tBTA_AV_NSM_ACT bta_av_nsm_act[] = {
183     bta_av_api_enable,       /* BTA_AV_API_ENABLE_EVT */
184     bta_av_api_register,     /* BTA_AV_API_REGISTER_EVT */
185     bta_av_api_deregister,   /* BTA_AV_API_DEREGISTER_EVT */
186     bta_av_api_disconnect,   /* BTA_AV_API_DISCONNECT_EVT */
187     bta_av_ci_data,          /* BTA_AV_CI_SRC_DATA_READY_EVT */
188     bta_av_sig_chg,          /* BTA_AV_SIG_CHG_EVT */
189     bta_av_signalling_timer, /* BTA_AV_SIGNALLING_TIMER_EVT */
190     bta_av_rc_disc_done,     /* BTA_AV_SDP_AVRC_DISC_EVT */
191     bta_av_rc_closed,        /* BTA_AV_AVRC_CLOSE_EVT */
192     bta_av_rc_browse_opened, /* BTA_AV_AVRC_BROWSE_OPEN_EVT */
193     bta_av_rc_browse_closed, /* BTA_AV_AVRC_BROWSE_CLOSE_EVT */
194     bta_av_conn_chg,         /* BTA_AV_CONN_CHG_EVT */
195     bta_av_dereg_comp,       /* BTA_AV_DEREG_COMP_EVT */
196     bta_av_rpc_conn, /* BTA_AV_AVDT_RPT_CONN_EVT */
197     bta_av_api_to_ssm, /* BTA_AV_API_START_EVT */
198     bta_av_api_to_ssm, /* BTA_AV_API_STOP_EVT */
199 };
200 
201 /*****************************************************************************
202  * Global data
203  ****************************************************************************/
204 
205 /* AV control block */
206 tBTA_AV_CB bta_av_cb = {};
207 
208 static const char* bta_av_st_code(uint8_t state);
209 
210 /*******************************************************************************
211  *
212  * Function         bta_av_api_enable
213  *
214  * Description      Handle an API enable event.
215  *
216  *
217  * Returns          void
218  *
219  ******************************************************************************/
bta_av_api_enable(tBTA_AV_DATA * p_data)220 static void bta_av_api_enable(tBTA_AV_DATA* p_data) {
221   if (bta_av_cb.disabling) {
222     APPL_TRACE_WARNING(
223         "%s: previous (reg_audio=%#x) is still disabling (attempts=%d)",
224         __func__, bta_av_cb.reg_audio, bta_av_cb.enabling_attempts);
225     if (++bta_av_cb.enabling_attempts <= kEnablingAttemptsCountMaximum) {
226       tBTA_AV_API_ENABLE* p_buf =
227           (tBTA_AV_API_ENABLE*)osi_malloc(sizeof(tBTA_AV_API_ENABLE));
228       memcpy(p_buf, &p_data->api_enable, sizeof(tBTA_AV_API_ENABLE));
229       bta_sys_sendmsg_delayed(p_buf, base::TimeDelta::FromMilliseconds(
230                                          kEnablingAttemptsIntervalMs));
231       return;
232     }
233     if (bta_av_cb.sdp_a2dp_handle) {
234       SDP_DeleteRecord(bta_av_cb.sdp_a2dp_handle);
235       bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
236     }
237 #if (BTA_AV_SINK_INCLUDED == TRUE)
238     if (bta_av_cb.sdp_a2dp_snk_handle) {
239       SDP_DeleteRecord(bta_av_cb.sdp_a2dp_snk_handle);
240       bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
241     }
242 #endif
243 #if (BTA_AR_INCLUDED == TRUE)
244     // deregister from AVDT
245     bta_ar_dereg_avdt(BTA_ID_AV);
246 
247     // deregister from AVCT
248     bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
249     bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
250     bta_ar_dereg_avct(BTA_ID_AV);
251 #endif
252   }
253 
254   /* initialize control block */
255   memset(&bta_av_cb, 0, sizeof(tBTA_AV_CB));
256 
257   for (int i = 0; i < BTA_AV_NUM_RCB; i++)
258     bta_av_cb.rcb[i].handle = BTA_AV_RC_HANDLE_NONE;
259 
260   bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
261 
262   /*
263    * TODO: The "disable" event handling is missing - there we need
264    * to alarm_free() the alarms below.
265    */
266   bta_av_cb.link_signalling_timer = alarm_new("bta_av.link_signalling_timer");
267   bta_av_cb.accept_signalling_timer =
268       alarm_new("bta_av.accept_signalling_timer");
269 
270   /* store parameters */
271   bta_av_cb.p_cback = p_data->api_enable.p_cback;
272   bta_av_cb.features = p_data->api_enable.features;
273   bta_av_cb.sec_mask = p_data->api_enable.sec_mask;
274 
275   tBTA_AV_ENABLE enable;
276   enable.features = bta_av_cb.features;
277 
278   /* Register for SCO change event */
279   bta_sys_sco_register(bta_av_sco_chg_cback);
280 
281   /* call callback with enable event */
282   tBTA_AV bta_av_data;
283   bta_av_data.enable = enable;
284   (*bta_av_cb.p_cback)(BTA_AV_ENABLE_EVT, &bta_av_data);
285 }
286 
287 /*******************************************************************************
288  *
289  * Function         bta_av_addr_to_scb
290  *
291  * Description      find the stream control block by the peer addr
292  *
293  * Returns          void
294  *
295  ******************************************************************************/
bta_av_addr_to_scb(const RawAddress & bd_addr)296 tBTA_AV_SCB* bta_av_addr_to_scb(const RawAddress& bd_addr) {
297   tBTA_AV_SCB* p_scb = NULL;
298   int xx;
299 
300   for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
301     if (bta_av_cb.p_scb[xx]) {
302       if (bd_addr == bta_av_cb.p_scb[xx]->PeerAddress()) {
303         p_scb = bta_av_cb.p_scb[xx];
304         break;
305       }
306     }
307   }
308   return p_scb;
309 }
310 
BTA_AvObtainPeerChannelIndex(const RawAddress & peer_address)311 int BTA_AvObtainPeerChannelIndex(const RawAddress& peer_address) {
312   // Find the entry for the peer (if exists)
313   tBTA_AV_SCB* p_scb = bta_av_addr_to_scb(peer_address);
314   if (p_scb != nullptr) {
315     return p_scb->hdi;
316   }
317 
318   // Find the index for an entry that is not used
319   for (int index = 0; index < BTA_AV_NUM_STRS; index++) {
320     tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
321     if (p_scb == nullptr) {
322       continue;
323     }
324     if (p_scb->PeerAddress().IsEmpty()) {
325       return p_scb->hdi;
326     }
327   }
328 
329   return -1;
330 }
331 
332 /*******************************************************************************
333  *
334  * Function         bta_av_hndl_to_scb
335  *
336  * Description      find the stream control block by the handle
337  *
338  * Returns          void
339  *
340  ******************************************************************************/
bta_av_hndl_to_scb(uint16_t handle)341 tBTA_AV_SCB* bta_av_hndl_to_scb(uint16_t handle) {
342   tBTA_AV_HNDL hndl = (tBTA_AV_HNDL)handle;
343   tBTA_AV_SCB* p_scb = NULL;
344   uint8_t idx = (hndl & BTA_AV_HNDL_MSK);
345 
346   if (idx && (idx <= BTA_AV_NUM_STRS)) {
347     p_scb = bta_av_cb.p_scb[idx - 1];
348   }
349   return p_scb;
350 }
351 
352 /*******************************************************************************
353  *
354  * Function         bta_av_alloc_scb
355  *
356  * Description      allocate stream control block,
357  *                  register the service to stack
358  *                  create SDP record
359  *
360  * Returns          void
361  *
362  ******************************************************************************/
bta_av_alloc_scb(tBTA_AV_CHNL chnl)363 static tBTA_AV_SCB* bta_av_alloc_scb(tBTA_AV_CHNL chnl) {
364   if (chnl != BTA_AV_CHNL_AUDIO) {
365     APPL_TRACE_ERROR("%s: bad channel: %d", __func__, chnl);
366     return nullptr;
367   }
368 
369   for (int xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
370     if (bta_av_cb.p_scb[xx] != nullptr) continue;
371     // Found an empty spot
372     // TODO: After tBTA_AV_SCB is changed to a proper class, the entry
373     // here should be allocated by C++ 'new' statement.
374     tBTA_AV_SCB* p_ret = (tBTA_AV_SCB*)osi_calloc(sizeof(tBTA_AV_SCB));
375     p_ret->rc_handle = BTA_AV_RC_HANDLE_NONE;
376     p_ret->chnl = chnl;
377     p_ret->hndl = (tBTA_AV_HNDL)((xx + 1) | chnl);
378     p_ret->hdi = xx;
379     p_ret->a2dp_list = list_new(nullptr);
380     p_ret->avrc_ct_timer = alarm_new("bta_av.avrc_ct_timer");
381     bta_av_cb.p_scb[xx] = p_ret;
382     return p_ret;
383   }
384 
385   return nullptr;
386 }
387 
bta_av_free_scb(tBTA_AV_SCB * p_scb)388 void bta_av_free_scb(tBTA_AV_SCB* p_scb) {
389   if (p_scb == nullptr) return;
390   uint8_t scb_index = p_scb->hdi;
391   CHECK(scb_index < BTA_AV_NUM_STRS);
392 
393   CHECK(p_scb == bta_av_cb.p_scb[scb_index]);
394   bta_av_cb.p_scb[scb_index] = nullptr;
395   alarm_free(p_scb->avrc_ct_timer);
396   // TODO: After tBTA_AV_SCB is changed to a proper class, the entry
397   // here should be de-allocated by C++ 'delete' statement.
398   osi_free(p_scb);
399 }
400 
OnConnected(const RawAddress & peer_address)401 void tBTA_AV_SCB::OnConnected(const RawAddress& peer_address) {
402   peer_address_ = peer_address;
403 
404   if (peer_address.IsEmpty()) {
405     LOG_ERROR(LOG_TAG, "%s: Invalid peer address: %s", __func__,
406               peer_address.ToString().c_str());
407     return;
408   }
409 
410   // Read and restore the AVDTP version from local storage
411   uint16_t avdtp_version = 0;
412   size_t version_value_size = sizeof(avdtp_version);
413   if (!btif_config_get_bin(peer_address_.ToString(), AVDTP_VERSION_CONFIG_KEY,
414                            (uint8_t*)&avdtp_version, &version_value_size)) {
415     LOG_WARN(LOG_TAG, "%s: Failed to read cached peer AVDTP version for %s",
416              __func__, peer_address_.ToString().c_str());
417   } else {
418     SetAvdtpVersion(avdtp_version);
419   }
420 }
421 
OnDisconnected()422 void tBTA_AV_SCB::OnDisconnected() {
423   peer_address_ = RawAddress::kEmpty;
424   SetAvdtpVersion(0);
425 }
426 
SetAvdtpVersion(uint16_t avdtp_version)427 void tBTA_AV_SCB::SetAvdtpVersion(uint16_t avdtp_version) {
428   avdtp_version_ = avdtp_version;
429   LOG_DEBUG(LOG_TAG, "%s: AVDTP version for %s set to 0x%x", __func__,
430             peer_address_.ToString().c_str(), avdtp_version_);
431 }
432 
433 /*******************************************************************************
434  ******************************************************************************/
bta_av_conn_cback(UNUSED_ATTR uint8_t handle,const RawAddress & bd_addr,uint8_t event,tAVDT_CTRL * p_data,uint8_t scb_index)435 void bta_av_conn_cback(UNUSED_ATTR uint8_t handle, const RawAddress& bd_addr,
436                        uint8_t event, tAVDT_CTRL* p_data, uint8_t scb_index) {
437   uint16_t evt = 0;
438   tBTA_AV_SCB* p_scb = NULL;
439 
440 #if (BTA_AR_INCLUDED == TRUE)
441   if (event == BTA_AR_AVDT_CONN_EVT || event == AVDT_CONNECT_IND_EVT ||
442       event == AVDT_DISCONNECT_IND_EVT)
443 #else
444   if (event == AVDT_CONNECT_IND_EVT || event == AVDT_DISCONNECT_IND_EVT)
445 #endif
446   {
447     evt = BTA_AV_SIG_CHG_EVT;
448     if (event == AVDT_DISCONNECT_IND_EVT) {
449       p_scb = bta_av_addr_to_scb(bd_addr);
450     } else if (event == AVDT_CONNECT_IND_EVT) {
451       APPL_TRACE_DEBUG("%s: CONN_IND is ACP:%d", __func__,
452                        p_data->hdr.err_param);
453     }
454 
455     tBTA_AV_STR_MSG* p_msg =
456         (tBTA_AV_STR_MSG*)osi_malloc(sizeof(tBTA_AV_STR_MSG));
457     p_msg->hdr.event = evt;
458     p_msg->hdr.layer_specific = event;
459     p_msg->hdr.offset = p_data->hdr.err_param;
460     p_msg->bd_addr = bd_addr;
461     p_msg->scb_index = scb_index;
462     if (p_scb) {
463       APPL_TRACE_DEBUG("%s: bta_handle x%x, role x%x", __func__, p_scb->hndl,
464                        p_scb->role);
465     }
466     LOG_INFO(LOG_TAG, "%s: conn_cback bd_addr: %s", __func__,
467              bd_addr.ToString().c_str());
468     bta_sys_sendmsg(p_msg);
469   }
470 }
471 
472 /*******************************************************************************
473  *
474  * Function         bta_av_a2dp_report_cback
475  *
476  * Description      A2DP report callback.
477  *
478  * Returns          void
479  *
480  ******************************************************************************/
bta_av_a2dp_report_cback(UNUSED_ATTR uint8_t handle,UNUSED_ATTR AVDT_REPORT_TYPE type,UNUSED_ATTR tAVDT_REPORT_DATA * p_data)481 static void bta_av_a2dp_report_cback(UNUSED_ATTR uint8_t handle,
482                                      UNUSED_ATTR AVDT_REPORT_TYPE type,
483                                      UNUSED_ATTR tAVDT_REPORT_DATA* p_data) {
484   /* Do not need to handle report data for now.
485    * This empty function is here for conformance reasons. */
486 }
487 
488 /*******************************************************************************
489  *
490  * Function         bta_av_api_register
491  *
492  * Description      allocate stream control block,
493  *                  register the service to stack
494  *                  create SDP record
495  *
496  * Returns          void
497  *
498  ******************************************************************************/
bta_av_api_register(tBTA_AV_DATA * p_data)499 static void bta_av_api_register(tBTA_AV_DATA* p_data) {
500   tBTA_AV_REGISTER registr;
501   tBTA_AV_SCB* p_scb; /* stream control block */
502   AvdtpRcb reg;
503   AvdtpStreamConfig avdtp_stream_config;
504   char* p_service_name;
505   tBTA_UTL_COD cod;
506 
507   if (bta_av_cb.disabling ||
508       (bta_av_cb.features == 0 && bta_av_cb.sec_mask == 0)) {
509     APPL_TRACE_WARNING(
510         "%s: AV instance (features=%#x, sec_mask=%#x, reg_audio=%#x) is not "
511         "ready for app_id %d",
512         __func__, bta_av_cb.features, bta_av_cb.sec_mask, bta_av_cb.reg_audio,
513         p_data->api_reg.app_id);
514     tBTA_AV_API_REG* p_buf =
515         (tBTA_AV_API_REG*)osi_malloc(sizeof(tBTA_AV_API_REG));
516     memcpy(p_buf, &p_data->api_reg, sizeof(tBTA_AV_API_REG));
517     bta_sys_sendmsg_delayed(
518         p_buf, base::TimeDelta::FromMilliseconds(kEnablingAttemptsIntervalMs));
519     return;
520   }
521 
522   avdtp_stream_config.Reset();
523 
524   registr.status = BTA_AV_FAIL_RESOURCES;
525   registr.app_id = p_data->api_reg.app_id;
526   registr.chnl = (tBTA_AV_CHNL)p_data->hdr.layer_specific;
527 
528   char avrcp_version[PROPERTY_VALUE_MAX] = {0};
529   osi_property_get(AVRCP_VERSION_PROPERTY, avrcp_version,
530                    AVRCP_DEFAULT_VERSION);
531   LOG_INFO(LOG_TAG, "%s: AVRCP version used for sdp: \"%s\"", __func__,
532            avrcp_version);
533 
534   uint16_t profile_initialized = p_data->api_reg.service_uuid;
535   if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
536     p_bta_av_cfg = &bta_avk_cfg;
537   } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
538     p_bta_av_cfg = &bta_av_cfg;
539 
540     if (!strncmp(AVRCP_1_3_STRING, avrcp_version, sizeof(AVRCP_1_3_STRING))) {
541       LOG_INFO(LOG_TAG, "%s: AVRCP 1.3 capabilites used", __func__);
542       p_bta_av_cfg = &bta_av_cfg_compatibility;
543     }
544   }
545 
546   APPL_TRACE_DEBUG("%s: profile: 0x%x", __func__, profile_initialized);
547   if (p_bta_av_cfg == NULL) {
548     APPL_TRACE_ERROR("%s: AV configuration is null!", __func__);
549     return;
550   }
551 
552   do {
553     p_scb = bta_av_alloc_scb(registr.chnl);
554     if (p_scb == NULL) {
555       APPL_TRACE_ERROR("%s: failed to alloc SCB", __func__);
556       break;
557     }
558 
559     registr.hndl = p_scb->hndl;
560     p_scb->app_id = registr.app_id;
561 
562     /* initialize the stream control block */
563     registr.status = BTA_AV_SUCCESS;
564 
565     if (bta_av_cb.reg_audio == 0) {
566       /* the first channel registered. register to AVDTP */
567       reg.ctrl_mtu = p_bta_av_cfg->sig_mtu;
568       reg.ret_tout = BTA_AV_RET_TOUT;
569       reg.sig_tout = BTA_AV_SIG_TOUT;
570       reg.idle_tout = BTA_AV_IDLE_TOUT;
571       reg.sec_mask = bta_av_cb.sec_mask;
572       reg.scb_index = p_scb->hdi;
573 #if (BTA_AR_INCLUDED == TRUE)
574       bta_ar_reg_avdt(&reg, bta_av_conn_cback, BTA_ID_AV);
575 #endif
576       bta_sys_role_chg_register(&bta_av_sys_rs_cback);
577 
578       /* create remote control TG service if required */
579       if (bta_av_cb.features & (BTA_AV_FEAT_RCTG)) {
580 /* register with no authorization; let AVDTP use authorization instead */
581 #if (BTA_AR_INCLUDED == TRUE)
582 #if (BTA_AV_WITH_AVCTP_AUTHORIZATION == TRUE)
583         bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
584                         bta_av_cb.sec_mask, BTA_ID_AV);
585 #else
586         bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
587                         (uint8_t)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)),
588                         BTA_ID_AV);
589 #endif
590 
591         /* For the Audio Sink role we support additional TG to support
592          * absolute volume.
593          */
594         uint16_t profile_version = AVRC_REV_1_0;
595 
596         if (!strncmp(AVRCP_1_6_STRING, avrcp_version,
597                      sizeof(AVRCP_1_6_STRING))) {
598           profile_version = AVRC_REV_1_6;
599         } else if (!strncmp(AVRCP_1_5_STRING, avrcp_version,
600                             sizeof(AVRCP_1_5_STRING))) {
601           profile_version = AVRC_REV_1_5;
602         } else if (!strncmp(AVRCP_1_3_STRING, avrcp_version,
603                             sizeof(AVRCP_1_3_STRING))) {
604           profile_version = AVRC_REV_1_3;
605         } else {
606           profile_version = AVRC_REV_1_4;
607         }
608 
609         bta_ar_reg_avrc(
610             UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target", NULL,
611             p_bta_av_cfg->avrc_tg_cat, BTA_ID_AV,
612             (bta_av_cb.features & BTA_AV_FEAT_BROWSE), profile_version);
613 #endif
614       }
615 
616       /* Set the Capturing service class bit */
617       if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE)
618         cod.service = BTM_COD_SERVICE_CAPTURING;
619       else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK)
620         cod.service = BTM_COD_SERVICE_RENDERING;
621       utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
622     } /* if 1st channel */
623 
624     /* get stream configuration and create stream */
625     avdtp_stream_config.cfg.num_codec = 1;
626     avdtp_stream_config.nsc_mask = AvdtpStreamConfig::AVDT_NSC_RECONFIG;
627     if (!(bta_av_cb.features & BTA_AV_FEAT_PROTECT)) {
628       avdtp_stream_config.nsc_mask |= AvdtpStreamConfig::AVDT_NSC_SECURITY;
629     }
630     APPL_TRACE_DEBUG("%s: nsc_mask: 0x%x", __func__,
631                      avdtp_stream_config.nsc_mask);
632 
633     if (p_data->api_reg.p_service_name[0] == 0) {
634       p_service_name = NULL;
635     } else {
636       p_service_name = p_data->api_reg.p_service_name;
637     }
638 
639     p_scb->suspend_sup = true;
640     p_scb->recfg_sup = true;
641 
642     avdtp_stream_config.scb_index = p_scb->hdi;
643     avdtp_stream_config.p_avdt_ctrl_cback = &bta_av_proc_stream_evt;
644 
645     /* set up the audio stream control block */
646     p_scb->p_act_tbl = (const tBTA_AV_ACT*)bta_av_a2dp_action;
647     p_scb->p_cos = &bta_av_a2dp_cos;
648     p_scb->media_type = AVDT_MEDIA_TYPE_AUDIO;
649     avdtp_stream_config.cfg.psc_mask = AVDT_PSC_TRANS;
650     avdtp_stream_config.media_type = AVDT_MEDIA_TYPE_AUDIO;
651     avdtp_stream_config.mtu = p_bta_av_cfg->audio_mtu;
652     avdtp_stream_config.flush_to = L2CAP_DEFAULT_FLUSH_TO;
653     btav_a2dp_codec_index_t codec_index_min = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN;
654     btav_a2dp_codec_index_t codec_index_max = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX;
655 
656     if (bta_av_cb.features & BTA_AV_FEAT_REPORT) {
657       avdtp_stream_config.cfg.psc_mask |= AVDT_PSC_REPORT;
658       avdtp_stream_config.p_report_cback = bta_av_a2dp_report_cback;
659     }
660     if (bta_av_cb.features & BTA_AV_FEAT_DELAY_RPT)
661       avdtp_stream_config.cfg.psc_mask |= AVDT_PSC_DELAY_RPT;
662 
663     if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
664       avdtp_stream_config.tsep = AVDT_TSEP_SRC;
665       codec_index_min = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN;
666       codec_index_max = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX;
667     } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
668       avdtp_stream_config.tsep = AVDT_TSEP_SNK;
669       avdtp_stream_config.p_sink_data_cback = bta_av_sink_data_cback;
670       codec_index_min = BTAV_A2DP_CODEC_INDEX_SINK_MIN;
671       codec_index_max = BTAV_A2DP_CODEC_INDEX_SINK_MAX;
672     }
673 
674     /* Initialize handles to zero */
675     for (int xx = 0; xx < BTAV_A2DP_CODEC_INDEX_MAX; xx++) {
676       p_scb->seps[xx].av_handle = 0;
677     }
678 
679     /* keep the configuration in the stream control block */
680     p_scb->cfg = avdtp_stream_config.cfg;
681     for (int i = codec_index_min; i < codec_index_max; i++) {
682       btav_a2dp_codec_index_t codec_index =
683           static_cast<btav_a2dp_codec_index_t>(i);
684       if (!bta_av_co_is_supported_codec(codec_index)) {
685         continue;
686       }
687       if (!(*bta_av_a2dp_cos.init)(codec_index, &avdtp_stream_config.cfg)) {
688         continue;
689       }
690       if (AVDT_CreateStream(p_scb->app_id, &p_scb->seps[codec_index].av_handle,
691                             avdtp_stream_config) != AVDT_SUCCESS) {
692         APPL_TRACE_WARNING(
693             "%s: bta_handle=0x%x (app_id %d) failed to alloc an SEP index:%d",
694             __func__, p_scb->hndl, p_scb->app_id, codec_index);
695         continue;
696       }
697       /* Save a copy of the codec */
698       memcpy(p_scb->seps[codec_index].codec_info,
699              avdtp_stream_config.cfg.codec_info, AVDT_CODEC_SIZE);
700       p_scb->seps[codec_index].tsep = avdtp_stream_config.tsep;
701       if (avdtp_stream_config.tsep == AVDT_TSEP_SNK) {
702         p_scb->seps[codec_index].p_app_sink_data_cback =
703             p_data->api_reg.p_app_sink_data_cback;
704       } else {
705         /* In case of A2DP SOURCE we don't need a callback to
706          * handle media packets.
707          */
708         p_scb->seps[codec_index].p_app_sink_data_cback = NULL;
709       }
710     }
711 
712     if (!bta_av_cb.reg_audio) {
713       bta_av_cb.sdp_a2dp_handle = 0;
714       bta_av_cb.sdp_a2dp_snk_handle = 0;
715       if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
716         /* create the SDP records on the 1st audio channel */
717         bta_av_cb.sdp_a2dp_handle = SDP_CreateRecord();
718         A2DP_AddRecord(UUID_SERVCLASS_AUDIO_SOURCE, p_service_name, NULL,
719                        A2DP_SUPF_PLAYER, bta_av_cb.sdp_a2dp_handle);
720         bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
721       } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
722 #if (BTA_AV_SINK_INCLUDED == TRUE)
723         bta_av_cb.sdp_a2dp_snk_handle = SDP_CreateRecord();
724         A2DP_AddRecord(UUID_SERVCLASS_AUDIO_SINK, p_service_name, NULL,
725                        A2DP_SUPF_PLAYER, bta_av_cb.sdp_a2dp_snk_handle);
726         bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SINK);
727 #endif
728       }
729       /* start listening when A2DP is registered */
730       if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
731         bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
732 
733       /* if the AV and AVK are both supported, it cannot support the CT role
734        */
735       if (bta_av_cb.features & (BTA_AV_FEAT_RCCT)) {
736         /* if TG is not supported, we need to register to AVCT now */
737         if ((bta_av_cb.features & (BTA_AV_FEAT_RCTG)) == 0) {
738 #if (BTA_AR_INCLUDED == TRUE)
739 #if (BTA_AV_WITH_AVCTP_AUTHORIZATION == TRUE)
740           bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
741                           bta_av_cb.sec_mask, BTA_ID_AV);
742 #else
743           bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
744                           (uint8_t)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)),
745                           BTA_ID_AV);
746 #endif
747 #endif
748           bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
749         }
750 #if (BTA_AR_INCLUDED == TRUE)
751         /* create an SDP record as AVRC CT. We create 1.3 for SOURCE
752          * because we rely on feature bits being scanned by external
753          * devices more than the profile version itself.
754          *
755          * We create 1.4 for SINK since we support browsing.
756          */
757         if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
758           bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL, NULL,
759                           p_bta_av_cfg->avrc_ct_cat, BTA_ID_AV,
760                           (bta_av_cb.features & BTA_AV_FEAT_BROWSE),
761                           AVRC_REV_1_3);
762         } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
763           bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL, NULL,
764                           p_bta_av_cfg->avrc_ct_cat, BTA_ID_AV,
765                           (bta_av_cb.features & BTA_AV_FEAT_BROWSE),
766                           AVRC_REV_1_6);
767         }
768 #endif
769       }
770     }
771     bta_av_cb.reg_audio |= BTA_AV_HNDL_TO_MSK(p_scb->hdi);
772     APPL_TRACE_DEBUG("%s: reg_audio: 0x%x", __func__, bta_av_cb.reg_audio);
773   } while (0);
774 
775   /* call callback with register event */
776   tBTA_AV bta_av_data;
777   bta_av_data.registr = registr;
778   (*bta_av_cb.p_cback)(BTA_AV_REGISTER_EVT, &bta_av_data);
779 }
780 
781 /*******************************************************************************
782  *
783  * Function         bta_av_api_deregister
784  *
785  * Description      de-register a channel
786  *
787  *
788  * Returns          void
789  *
790  ******************************************************************************/
bta_av_api_deregister(tBTA_AV_DATA * p_data)791 void bta_av_api_deregister(tBTA_AV_DATA* p_data) {
792   tBTA_AV_SCB* p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
793 
794   if (p_scb) {
795     p_scb->deregistering = true;
796     bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, p_data);
797   } else {
798     bta_av_dereg_comp(p_data);
799   }
800 }
801 
802 /*******************************************************************************
803  *
804  * Function         bta_av_ci_data
805  *
806  * Description      Forward the BTA_AV_CI_SRC_DATA_READY_EVT to stream state
807  *                  machine.
808  *
809  *
810  * Returns          void
811  *
812  ******************************************************************************/
bta_av_ci_data(tBTA_AV_DATA * p_data)813 static void bta_av_ci_data(tBTA_AV_DATA* p_data) {
814   tBTA_AV_SCB* p_scb;
815   int i;
816   uint8_t chnl = (uint8_t)p_data->hdr.layer_specific;
817 
818   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
819     p_scb = bta_av_cb.p_scb[i];
820 
821     if (p_scb && p_scb->chnl == chnl) {
822       bta_av_ssm_execute(p_scb, BTA_AV_SRC_DATA_READY_EVT, p_data);
823     }
824   }
825 }
826 
827 /*******************************************************************************
828  *
829  * Function         bta_av_rpc_conn
830  *
831  * Description      report report channel open
832  *
833  * Returns          void
834  *
835  ******************************************************************************/
bta_av_rpc_conn(UNUSED_ATTR tBTA_AV_DATA * p_data)836 static void bta_av_rpc_conn(UNUSED_ATTR tBTA_AV_DATA* p_data) {}
837 
838 /*******************************************************************************
839  *
840  * Function         bta_av_api_to_ssm
841  *
842  * Description      forward the API request to stream state machine
843  *
844  *
845  * Returns          void
846  *
847  ******************************************************************************/
bta_av_api_to_ssm(tBTA_AV_DATA * p_data)848 static void bta_av_api_to_ssm(tBTA_AV_DATA* p_data) {
849   uint16_t event =
850       p_data->hdr.event - BTA_AV_FIRST_A2S_API_EVT + BTA_AV_FIRST_A2S_SSM_EVT;
851   tBTA_AV_HNDL handle = p_data->hdr.layer_specific;
852   tBTA_AV_SCB* p_scb = bta_av_hndl_to_scb(handle);
853 
854   if (p_scb != nullptr) {
855     bta_av_ssm_execute(p_scb, event, p_data);
856   }
857 }
858 
859 /*******************************************************************************
860  *
861  * Function         bta_av_chk_start
862  *
863  * Description      if this is audio channel, check if more than one audio
864  *                  channel is connected & already started.
865  *                  This function needs to be kept very similar to
866  *                  bta_av_chk_2nd_start
867  *
868  * Returns          true, if need api_start
869  *
870  ******************************************************************************/
bta_av_chk_start(tBTA_AV_SCB * p_scb)871 bool bta_av_chk_start(tBTA_AV_SCB* p_scb) {
872   bool start = false;
873 
874   if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2) &&
875       (((p_scb->role & BTA_AV_ROLE_AD_ACP) == 0) ||  // Outgoing connection or
876        (bta_av_cb.features & BTA_AV_FEAT_ACP_START))) {  // Auto-starting option
877     // More than one audio channel is connected.
878     // If this is the 2nd stream as ACP, give INT a chance to issue the START
879     // command.
880     for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
881       tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
882       if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
883         start = true;
884         // May need to update the flush timeout of this already started stream
885         if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
886           p_scbi->co_started = bta_av_cb.audio_open_cnt;
887           L2CA_SetFlushTimeout(
888               p_scbi->PeerAddress(),
889               p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1]);
890         }
891       }
892     }
893   }
894 
895   LOG_INFO(LOG_TAG,
896            "%s: peer %s channel:%d bta_av_cb.audio_open_cnt:%d role:0x%x "
897            "features:0x%x start:%s",
898            __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->chnl,
899            bta_av_cb.audio_open_cnt, p_scb->role, bta_av_cb.features,
900            logbool(start).c_str());
901   return start;
902 }
903 
904 /*******************************************************************************
905  *
906  * Function         bta_av_restore_switch
907  *
908  * Description      assume that the caller of this function already makes
909  *                  sure that there's only one ACL connection left
910  *
911  * Returns          void
912  *
913  ******************************************************************************/
bta_av_restore_switch(void)914 void bta_av_restore_switch(void) {
915   tBTA_AV_CB* p_cb = &bta_av_cb;
916   int i;
917   uint8_t mask;
918   uint8_t set_policy = (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_MASTER_SLAVE_SWITCH);
919 
920   APPL_TRACE_DEBUG("%s: reg_audio: 0x%x", __func__, bta_av_cb.reg_audio);
921   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
922     mask = BTA_AV_HNDL_TO_MSK(i);
923     if (p_cb->conn_audio == mask) {
924       if (p_cb->p_scb[i]) {
925         bta_sys_set_policy(BTA_ID_AV, set_policy,
926                            p_cb->p_scb[i]->PeerAddress());
927       }
928       break;
929     }
930   }
931 }
932 
933 /*******************************************************************************
934  *
935  * Function         bta_av_sys_rs_cback
936  *
937  * Description      Receives the role change event from dm
938  *
939  * Returns          (BTA_SYS_ROLE_CHANGE, new_role, hci_status, p_bda)
940  *
941  ******************************************************************************/
bta_av_sys_rs_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,uint8_t id,uint8_t app_id,const RawAddress & peer_addr)942 static void bta_av_sys_rs_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,
943                                 uint8_t id, uint8_t app_id,
944                                 const RawAddress& peer_addr) {
945   int i;
946   tBTA_AV_SCB* p_scb = NULL;
947   uint8_t cur_role;
948   uint8_t peer_idx = 0;
949   uint8_t set_policy = (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_MASTER_SLAVE_SWITCH);
950 
951   APPL_TRACE_DEBUG(
952       "%s: peer %s new_role:%d hci_status:0x%x bta_av_cb.rs_idx:%d", __func__,
953       peer_addr.ToString().c_str(), id, app_id, bta_av_cb.rs_idx);
954 
955   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
956     /* loop through all the SCBs to find matching peer addresses and report the
957      * role change event */
958     /* note that more than one SCB (a2dp & vdp) maybe waiting for this event */
959     p_scb = bta_av_cb.p_scb[i];
960     if (p_scb && p_scb->PeerAddress() == peer_addr) {
961       tBTA_AV_ROLE_RES* p_buf =
962           (tBTA_AV_ROLE_RES*)osi_malloc(sizeof(tBTA_AV_ROLE_RES));
963       APPL_TRACE_DEBUG(
964           "%s: peer %s found: new_role:%d, hci_status:0x%x bta_handle:0x%x",
965           __func__, peer_addr.ToString().c_str(), id, app_id, p_scb->hndl);
966       /*
967       if ((id != BTM_ROLE_MASTER) && (app_id != HCI_SUCCESS))
968       {
969           bta_sys_set_policy(BTA_ID_AV, set_policy, p_scb->PeerAddress());
970       }
971       */
972       p_buf->hdr.event = BTA_AV_ROLE_CHANGE_EVT;
973       p_buf->hdr.layer_specific = p_scb->hndl;
974       p_buf->new_role = id;
975       p_buf->hci_status = app_id;
976       bta_sys_sendmsg(p_buf);
977 
978       peer_idx = p_scb->hdi + 1; /* Handle index for the peer_addr */
979     }
980   }
981 
982   /* restore role switch policy, if role switch failed */
983   if ((HCI_SUCCESS != app_id) &&
984       (BTM_GetRole(peer_addr, &cur_role) == BTM_SUCCESS) &&
985       (cur_role == BTM_ROLE_SLAVE)) {
986     bta_sys_set_policy(BTA_ID_AV, set_policy, peer_addr);
987   }
988 
989   /* if BTA_AvOpen() was called for other device, which caused the role switch
990    * of the peer_addr,  */
991   /* we need to continue opening process for the BTA_AvOpen(). */
992   if ((bta_av_cb.rs_idx != 0) && (bta_av_cb.rs_idx != peer_idx)) {
993     if ((bta_av_cb.rs_idx - 1) < BTA_AV_NUM_STRS) {
994       p_scb = bta_av_cb.p_scb[bta_av_cb.rs_idx - 1];
995     }
996     if (p_scb && p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
997       APPL_TRACE_DEBUG("%s: peer %s rs_idx:%d, bta_handle:0x%x q_tag:%d",
998                        __func__, p_scb->PeerAddress().ToString().c_str(),
999                        bta_av_cb.rs_idx, p_scb->hndl, p_scb->q_tag);
1000 
1001       if (HCI_SUCCESS == app_id || HCI_ERR_NO_CONNECTION == app_id) {
1002         p_scb->q_info.open.switch_res = BTA_AV_RS_OK;
1003       } else {
1004         APPL_TRACE_ERROR(
1005             "%s: peer %s (p_scb peer %s) role switch failed: new_role:%d "
1006             "hci_status:0x%x",
1007             __func__, peer_addr.ToString().c_str(),
1008             p_scb->PeerAddress().ToString().c_str(), id, app_id);
1009         p_scb->q_info.open.switch_res = BTA_AV_RS_FAIL;
1010       }
1011 
1012       /* Continue av open process */
1013       bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)&(p_scb->q_info.open));
1014     }
1015 
1016     bta_av_cb.rs_idx = 0;
1017   }
1018 }
1019 
1020 /*******************************************************************************
1021  *
1022  * Function         bta_av_sco_chg_cback
1023  *
1024  * Description      receive & process the SCO connection up/down event from sys.
1025  *                  call setup also triggers this callback, to suspend av before
1026  *                  SCO activity happens, or to resume av once call ends.
1027  *
1028  * Returns          void
1029  *
1030  ******************************************************************************/
bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status,uint8_t id,UNUSED_ATTR uint8_t app_id,UNUSED_ATTR const RawAddress & peer_addr)1031 static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
1032                                  UNUSED_ATTR uint8_t app_id,
1033                                  UNUSED_ATTR const RawAddress& peer_addr) {
1034   tBTA_AV_SCB* p_scb;
1035   int i;
1036   tBTA_AV_API_STOP stop;
1037 
1038   LOG(INFO) << __func__ << ": status=" << +status << ", num_links=" << +id;
1039   if (id) {
1040     bta_av_cb.sco_occupied = true;
1041 
1042     if (bta_av_cb.features & BTA_AV_FEAT_NO_SCO_SSPD) {
1043       return;
1044     }
1045 
1046     /* either BTA_SYS_SCO_OPEN or BTA_SYS_SCO_CLOSE with remaining active SCO */
1047     for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1048       p_scb = bta_av_cb.p_scb[i];
1049 
1050       if (p_scb && p_scb->co_started && (!p_scb->sco_suspend)) {
1051         VLOG(1) << __func__ << ": suspending scb:" << i;
1052         /* scb is used and started, not suspended automatically */
1053         p_scb->sco_suspend = true;
1054         stop.flush = false;
1055         stop.suspend = true;
1056         stop.reconfig_stop = false;
1057         bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA*)&stop);
1058       }
1059     }
1060   } else {
1061     bta_av_cb.sco_occupied = false;
1062 
1063     if (bta_av_cb.features & BTA_AV_FEAT_NO_SCO_SSPD) {
1064       return;
1065     }
1066 
1067     for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1068       p_scb = bta_av_cb.p_scb[i];
1069 
1070       if (p_scb && p_scb->sco_suspend) /* scb is used and suspended for SCO */
1071       {
1072         VLOG(1) << __func__ << ": starting scb:" << i;
1073         bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
1074       }
1075     }
1076   }
1077 }
1078 
1079 /*******************************************************************************
1080  *
1081  * Function         bta_av_switch_if_needed
1082  *
1083  * Description      This function checks if there is another existing AV
1084  *                  channel that is local as slave role.
1085  *                  If so, role switch and remove it from link policy.
1086  *
1087  * Returns          true, if role switch is done
1088  *
1089  ******************************************************************************/
bta_av_switch_if_needed(tBTA_AV_SCB * p_scb)1090 bool bta_av_switch_if_needed(tBTA_AV_SCB* p_scb) {
1091   // TODO: A workaround for devices that are connected first, become
1092   // Master, and block follow-up role changes - b/72122792 .
1093   return false;
1094 #if 0
1095   uint8_t role;
1096   bool needed = false;
1097   tBTA_AV_SCB* p_scbi;
1098   int i;
1099   uint8_t mask;
1100 
1101   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1102     mask = BTA_AV_HNDL_TO_MSK(i);
1103     p_scbi = bta_av_cb.p_scb[i];
1104     if (p_scbi && (p_scb->hdi != i) &&   /* not the original channel */
1105         ((bta_av_cb.conn_audio & mask))) /* connected audio */
1106     {
1107       BTM_GetRole(p_scbi->PeerAddress(), &role);
1108       /* this channel is open - clear the role switch link policy for this link
1109        */
1110       if (BTM_ROLE_MASTER != role) {
1111         if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
1112           bta_sys_clear_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH,
1113                                p_scbi->PeerAddress());
1114         if (BTM_CMD_STARTED !=
1115             BTM_SwitchRole(p_scbi->PeerAddress(), BTM_ROLE_MASTER, NULL)) {
1116           /* can not switch role on SCBI
1117            * start the timer on SCB - because this function is ONLY called when
1118            * SCB gets API_OPEN */
1119           bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RS_TIME_VAL,
1120                               BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
1121         }
1122         needed = true;
1123         /* mark the original channel as waiting for RS result */
1124         bta_av_cb.rs_idx = p_scb->hdi + 1;
1125         break;
1126       }
1127     }
1128   }
1129   return needed;
1130 #endif
1131 }
1132 
1133 /*******************************************************************************
1134  *
1135  * Function         bta_av_link_role_ok
1136  *
1137  * Description      This function checks if the SCB has existing ACL connection
1138  *                  If so, check if the link role fits the requirements.
1139  *
1140  * Returns          true, if role is ok
1141  *
1142  ******************************************************************************/
bta_av_link_role_ok(tBTA_AV_SCB * p_scb,uint8_t bits)1143 bool bta_av_link_role_ok(tBTA_AV_SCB* p_scb, uint8_t bits) {
1144   uint8_t role;
1145   bool is_ok = true;
1146 
1147   if (BTM_GetRole(p_scb->PeerAddress(), &role) == BTM_SUCCESS) {
1148     LOG_INFO(LOG_TAG,
1149              "%s: peer %s bta_handle:0x%x role:%d conn_audio:0x%x bits:%d "
1150              "features:0x%x",
1151              __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1152              role, bta_av_cb.conn_audio, bits, bta_av_cb.features);
1153     if (BTM_ROLE_MASTER != role &&
1154         (A2DP_BitsSet(bta_av_cb.conn_audio) > bits ||
1155          (bta_av_cb.features & BTA_AV_FEAT_MASTER))) {
1156       if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
1157         bta_sys_clear_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH,
1158                              p_scb->PeerAddress());
1159 
1160       tBTM_STATUS status =
1161           BTM_SwitchRole(p_scb->PeerAddress(), BTM_ROLE_MASTER, NULL);
1162       if (status != BTM_CMD_STARTED) {
1163         /* can not switch role on SCB - start the timer on SCB */
1164         LOG_ERROR(LOG_TAG,
1165                   "%s: peer %s BTM_SwitchRole(BTM_ROLE_MASTER) error: %d",
1166                   __func__, p_scb->PeerAddress().ToString().c_str(), status);
1167       }
1168       if (status != BTM_MODE_UNSUPPORTED && status != BTM_DEV_BLACKLISTED) {
1169         is_ok = false;
1170         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_START;
1171       }
1172     }
1173   }
1174 
1175   return is_ok;
1176 }
1177 
1178 /*******************************************************************************
1179  *
1180  * Function         bta_av_dup_audio_buf
1181  *
1182  * Description      dup the audio data to the q_info.a2dp of other audio
1183  *                  channels
1184  *
1185  * Returns          void
1186  *
1187  ******************************************************************************/
bta_av_dup_audio_buf(tBTA_AV_SCB * p_scb,BT_HDR * p_buf)1188 void bta_av_dup_audio_buf(tBTA_AV_SCB* p_scb, BT_HDR* p_buf) {
1189   /* Test whether there is more than one audio channel connected */
1190   if ((p_buf == NULL) || (bta_av_cb.audio_open_cnt < 2)) return;
1191 
1192   uint16_t copy_size = BT_HDR_SIZE + p_buf->len + p_buf->offset;
1193   for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
1194     tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
1195 
1196     if (i == p_scb->hdi) continue; /* Ignore the original channel */
1197     if ((p_scbi == NULL) || !p_scbi->co_started)
1198       continue; /* Ignore if SCB is not used or started */
1199     if (!(bta_av_cb.conn_audio & BTA_AV_HNDL_TO_MSK(i)))
1200       continue; /* Audio is not connected */
1201 
1202     /* Enqueue the data */
1203     BT_HDR* p_new = (BT_HDR*)osi_malloc(copy_size);
1204     memcpy(p_new, p_buf, copy_size);
1205     list_append(p_scbi->a2dp_list, p_new);
1206 
1207     if (list_length(p_scbi->a2dp_list) > p_bta_av_cfg->audio_mqs) {
1208       // Drop the oldest packet
1209       bta_av_co_audio_drop(p_scbi->hndl, p_scbi->PeerAddress());
1210       BT_HDR* p_buf_drop = static_cast<BT_HDR*>(list_front(p_scbi->a2dp_list));
1211       list_remove(p_scbi->a2dp_list, p_buf_drop);
1212       osi_free(p_buf_drop);
1213     }
1214   }
1215 }
1216 
1217 /*******************************************************************************
1218  *
1219  * Function         bta_av_sm_execute
1220  *
1221  * Description      State machine event handling function for AV
1222  *
1223  *
1224  * Returns          void
1225  *
1226  ******************************************************************************/
bta_av_sm_execute(tBTA_AV_CB * p_cb,uint16_t event,tBTA_AV_DATA * p_data)1227 void bta_av_sm_execute(tBTA_AV_CB* p_cb, uint16_t event, tBTA_AV_DATA* p_data) {
1228   tBTA_AV_ST_TBL state_table;
1229   uint8_t action;
1230 
1231   APPL_TRACE_EVENT("%s: AV event=0x%x(%s) state=%d(%s)", __func__, event,
1232                    bta_av_evt_code(event), p_cb->state,
1233                    bta_av_st_code(p_cb->state));
1234 
1235   /* look up the state table for the current state */
1236   state_table = bta_av_st_tbl[p_cb->state];
1237 
1238   event &= 0x00FF;
1239 
1240   /* set next state */
1241   p_cb->state = state_table[event][BTA_AV_NEXT_STATE];
1242   APPL_TRACE_EVENT("%s: next state=%d event offset:%d", __func__, p_cb->state,
1243                    event);
1244 
1245   /* execute action functions */
1246   action = state_table[event][BTA_AV_ACTION_COL];
1247   if (action != BTA_AV_IGNORE) {
1248     APPL_TRACE_EVENT("%s: action executed %d", __func__, action);
1249     (*bta_av_action[action])(p_cb, p_data);
1250   }
1251 }
1252 
1253 /*******************************************************************************
1254  *
1255  * Function         bta_av_hdl_event
1256  *
1257  * Description      Advanced audio/video main event handling function.
1258  *
1259  *
1260  * Returns          bool
1261  *
1262  ******************************************************************************/
bta_av_hdl_event(BT_HDR * p_msg)1263 bool bta_av_hdl_event(BT_HDR* p_msg) {
1264   if (p_msg->event > BTA_AV_LAST_EVT) {
1265     return true; /* to free p_msg */
1266   }
1267   if (p_msg->event >= BTA_AV_FIRST_NSM_EVT) {
1268     APPL_TRACE_VERBOSE("%s: AV nsm event=0x%x(%s)", __func__, p_msg->event,
1269                        bta_av_evt_code(p_msg->event));
1270     /* non state machine events */
1271     (*bta_av_nsm_act[p_msg->event - BTA_AV_FIRST_NSM_EVT])(
1272         (tBTA_AV_DATA*)p_msg);
1273   } else if (p_msg->event >= BTA_AV_FIRST_SM_EVT &&
1274              p_msg->event <= BTA_AV_LAST_SM_EVT) {
1275     APPL_TRACE_VERBOSE("%s: AV sm event=0x%x(%s)", __func__, p_msg->event,
1276                        bta_av_evt_code(p_msg->event));
1277     /* state machine events */
1278     bta_av_sm_execute(&bta_av_cb, p_msg->event, (tBTA_AV_DATA*)p_msg);
1279   } else {
1280     APPL_TRACE_VERBOSE("%s: bta_handle=0x%x", __func__, p_msg->layer_specific);
1281     /* stream state machine events */
1282     bta_av_ssm_execute(bta_av_hndl_to_scb(p_msg->layer_specific), p_msg->event,
1283                        (tBTA_AV_DATA*)p_msg);
1284   }
1285   return true;
1286 }
1287 
1288 /*****************************************************************************
1289  *  Debug Functions
1290  ****************************************************************************/
1291 /*******************************************************************************
1292  *
1293  * Function         bta_av_st_code
1294  *
1295  * Description
1296  *
1297  * Returns          char *
1298  *
1299  ******************************************************************************/
bta_av_st_code(uint8_t state)1300 static const char* bta_av_st_code(uint8_t state) {
1301   switch (state) {
1302     case BTA_AV_INIT_ST:
1303       return "INIT";
1304     case BTA_AV_OPEN_ST:
1305       return "OPEN";
1306     default:
1307       return "unknown";
1308   }
1309 }
1310 /*******************************************************************************
1311  *
1312  * Function         bta_av_evt_code
1313  *
1314  * Description
1315  *
1316  * Returns          char *
1317  *
1318  ******************************************************************************/
bta_av_evt_code(uint16_t evt_code)1319 const char* bta_av_evt_code(uint16_t evt_code) {
1320   switch (evt_code) {
1321     case BTA_AV_API_DISABLE_EVT:
1322       return "API_DISABLE";
1323     case BTA_AV_API_REMOTE_CMD_EVT:
1324       return "API_REMOTE_CMD";
1325     case BTA_AV_API_VENDOR_CMD_EVT:
1326       return "API_VENDOR_CMD";
1327     case BTA_AV_API_VENDOR_RSP_EVT:
1328       return "API_VENDOR_RSP";
1329     case BTA_AV_API_META_RSP_EVT:
1330       return "API_META_RSP_EVT";
1331     case BTA_AV_API_RC_CLOSE_EVT:
1332       return "API_RC_CLOSE";
1333     case BTA_AV_AVRC_OPEN_EVT:
1334       return "AVRC_OPEN";
1335     case BTA_AV_AVRC_MSG_EVT:
1336       return "AVRC_MSG";
1337     case BTA_AV_AVRC_NONE_EVT:
1338       return "AVRC_NONE";
1339 
1340     case BTA_AV_API_OPEN_EVT:
1341       return "API_OPEN";
1342     case BTA_AV_API_CLOSE_EVT:
1343       return "API_CLOSE";
1344     case BTA_AV_AP_START_EVT:
1345       return "AP_START";
1346     case BTA_AV_AP_STOP_EVT:
1347       return "AP_STOP";
1348     case BTA_AV_API_RECONFIG_EVT:
1349       return "API_RECONFIG";
1350     case BTA_AV_API_PROTECT_REQ_EVT:
1351       return "API_PROTECT_REQ";
1352     case BTA_AV_API_PROTECT_RSP_EVT:
1353       return "API_PROTECT_RSP";
1354     case BTA_AV_API_RC_OPEN_EVT:
1355       return "API_RC_OPEN";
1356     case BTA_AV_SRC_DATA_READY_EVT:
1357       return "SRC_DATA_READY";
1358     case BTA_AV_CI_SETCONFIG_OK_EVT:
1359       return "CI_SETCONFIG_OK";
1360     case BTA_AV_CI_SETCONFIG_FAIL_EVT:
1361       return "CI_SETCONFIG_FAIL";
1362     case BTA_AV_SDP_DISC_OK_EVT:
1363       return "SDP_DISC_OK";
1364     case BTA_AV_SDP_DISC_FAIL_EVT:
1365       return "SDP_DISC_FAIL";
1366     case BTA_AV_STR_DISC_OK_EVT:
1367       return "STR_DISC_OK";
1368     case BTA_AV_STR_DISC_FAIL_EVT:
1369       return "STR_DISC_FAIL";
1370     case BTA_AV_STR_GETCAP_OK_EVT:
1371       return "STR_GETCAP_OK";
1372     case BTA_AV_STR_GETCAP_FAIL_EVT:
1373       return "STR_GETCAP_FAIL";
1374     case BTA_AV_STR_OPEN_OK_EVT:
1375       return "STR_OPEN_OK";
1376     case BTA_AV_STR_OPEN_FAIL_EVT:
1377       return "STR_OPEN_FAIL";
1378     case BTA_AV_STR_START_OK_EVT:
1379       return "STR_START_OK";
1380     case BTA_AV_STR_START_FAIL_EVT:
1381       return "STR_START_FAIL";
1382     case BTA_AV_STR_CLOSE_EVT:
1383       return "STR_CLOSE";
1384     case BTA_AV_STR_CONFIG_IND_EVT:
1385       return "STR_CONFIG_IND";
1386     case BTA_AV_STR_SECURITY_IND_EVT:
1387       return "STR_SECURITY_IND";
1388     case BTA_AV_STR_SECURITY_CFM_EVT:
1389       return "STR_SECURITY_CFM";
1390     case BTA_AV_STR_WRITE_CFM_EVT:
1391       return "STR_WRITE_CFM";
1392     case BTA_AV_STR_SUSPEND_CFM_EVT:
1393       return "STR_SUSPEND_CFM";
1394     case BTA_AV_STR_RECONFIG_CFM_EVT:
1395       return "STR_RECONFIG_CFM";
1396     case BTA_AV_AVRC_TIMER_EVT:
1397       return "AVRC_TIMER";
1398     case BTA_AV_AVDT_CONNECT_EVT:
1399       return "AVDT_CONNECT";
1400     case BTA_AV_AVDT_DISCONNECT_EVT:
1401       return "AVDT_DISCONNECT";
1402     case BTA_AV_ROLE_CHANGE_EVT:
1403       return "ROLE_CHANGE";
1404     case BTA_AV_AVDT_DELAY_RPT_EVT:
1405       return "AVDT_DELAY_RPT";
1406     case BTA_AV_ACP_CONNECT_EVT:
1407       return "ACP_CONNECT";
1408     case BTA_AV_API_OFFLOAD_START_EVT:
1409       return "OFFLOAD_START";
1410     case BTA_AV_API_OFFLOAD_START_RSP_EVT:
1411       return "OFFLOAD_START_RSP";
1412 
1413     case BTA_AV_API_ENABLE_EVT:
1414       return "API_ENABLE";
1415     case BTA_AV_API_REGISTER_EVT:
1416       return "API_REG";
1417     case BTA_AV_API_DEREGISTER_EVT:
1418       return "API_DEREG";
1419     case BTA_AV_API_DISCONNECT_EVT:
1420       return "API_DISCNT";
1421     case BTA_AV_CI_SRC_DATA_READY_EVT:
1422       return "CI_DATA_READY";
1423     case BTA_AV_SIG_CHG_EVT:
1424       return "SIG_CHG";
1425     case BTA_AV_SIGNALLING_TIMER_EVT:
1426       return "SIGNALLING_TIMER";
1427     case BTA_AV_SDP_AVRC_DISC_EVT:
1428       return "SDP_AVRC_DISC";
1429     case BTA_AV_AVRC_CLOSE_EVT:
1430       return "AVRC_CLOSE";
1431     case BTA_AV_AVRC_BROWSE_OPEN_EVT:
1432       return "AVRC_BROWSE_OPEN";
1433     case BTA_AV_AVRC_BROWSE_CLOSE_EVT:
1434       return "AVRC_BROWSE_CLOSE";
1435     case BTA_AV_CONN_CHG_EVT:
1436       return "CONN_CHG";
1437     case BTA_AV_DEREG_COMP_EVT:
1438       return "DEREG_COMP";
1439     case BTA_AV_AVDT_RPT_CONN_EVT:
1440       return "RPT_CONN";
1441     case BTA_AV_API_START_EVT:
1442       return "API_START";
1443     case BTA_AV_API_STOP_EVT:
1444       return "API_STOP";
1445     default:
1446       return "unknown";
1447   }
1448 }
1449 
bta_debug_av_dump(int fd)1450 void bta_debug_av_dump(int fd) {
1451   if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
1452 
1453   dprintf(fd, "\nBTA AV State:\n");
1454   dprintf(fd, "  State Machine State: %s\n", bta_av_st_code(bta_av_cb.state));
1455   dprintf(fd, "  Link signalling timer: %s\n",
1456           alarm_is_scheduled(bta_av_cb.link_signalling_timer)
1457               ? "Scheduled"
1458               : "Not scheduled");
1459   dprintf(fd, "  Accept signalling timer: %s\n",
1460           alarm_is_scheduled(bta_av_cb.accept_signalling_timer)
1461               ? "Scheduled"
1462               : "Not scheduled");
1463   dprintf(fd, "  SDP A2DP source handle: %d\n", bta_av_cb.sdp_a2dp_handle);
1464   dprintf(fd, "  SDP A2DP sink handle: %d\n", bta_av_cb.sdp_a2dp_snk_handle);
1465   dprintf(fd, "  Features: 0x%x\n", bta_av_cb.features);
1466   dprintf(fd, "  Security mask: 0x%x\n", bta_av_cb.sec_mask);
1467   dprintf(fd, "  SDP handle: %d\n", bta_av_cb.handle);
1468   dprintf(fd, "  Disabling: %s\n", bta_av_cb.disabling ? "true" : "false");
1469   dprintf(fd, "  SCO occupied: %s\n",
1470           bta_av_cb.sco_occupied ? "true" : "false");
1471   dprintf(fd, "  Connected audio channels: %d\n", bta_av_cb.audio_open_cnt);
1472   dprintf(fd, "  Connected audio channels mask: 0x%x\n", bta_av_cb.conn_audio);
1473   dprintf(fd, "  Streaming audio channels mask: 0x%x\n",
1474           bta_av_cb.audio_streams);
1475   dprintf(fd, "  Registered audio channels mask: 0x%x\n", bta_av_cb.reg_audio);
1476   dprintf(fd, "  Connected LCBs mask: 0x%x\n", bta_av_cb.conn_lcb);
1477 
1478   for (size_t i = 0; i < sizeof(bta_av_cb.lcb) / sizeof(bta_av_cb.lcb[0]);
1479        i++) {
1480     const tBTA_AV_LCB& lcb = bta_av_cb.lcb[i];
1481     if (lcb.addr.IsEmpty()) {
1482       continue;
1483     }
1484     dprintf(fd, "\n  Link control block: %zu peer: %s\n", i,
1485             lcb.addr.ToString().c_str());
1486     dprintf(fd, "    Connected stream handle mask: 0x%x\n", lcb.conn_msk);
1487     dprintf(fd, "    Index(+1) to LCB: %d\n", lcb.lidx);
1488   }
1489   for (size_t i = 0; i < BTA_AV_NUM_STRS; i++) {
1490     const tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[i];
1491     if (p_scb == nullptr) {
1492       continue;
1493     }
1494     if (p_scb->PeerAddress().IsEmpty()) {
1495       continue;
1496     }
1497     dprintf(fd, "\n  BTA ID: %zu peer: %s\n", i,
1498             p_scb->PeerAddress().ToString().c_str());
1499     dprintf(fd, "    SDP discovery started: %s\n",
1500             p_scb->sdp_discovery_started ? "true" : "false");
1501     for (size_t j = 0; j < BTAV_A2DP_CODEC_INDEX_MAX; j++) {
1502       const tBTA_AV_SEP& sep = p_scb->seps[j];
1503       if (sep.av_handle == 0) {
1504         continue;
1505       }
1506       dprintf(fd, "    SEP ID: %zu\n", j);
1507       dprintf(fd, "      SEP AVDTP handle: %d\n", sep.av_handle);
1508       dprintf(fd, "      Local SEP type: %d\n", sep.tsep);
1509       dprintf(fd, "      Codec: %s\n", A2DP_CodecName(sep.codec_info));
1510     }
1511     dprintf(fd, "    BTA info tag: %d\n", p_scb->q_tag);
1512     dprintf(fd, "    API Open peer: %s\n",
1513             p_scb->q_info.open.bd_addr.ToString().c_str());
1514     dprintf(fd, "      Use AVRCP: %s\n",
1515             p_scb->q_info.open.use_rc ? "true" : "false");
1516     dprintf(fd, "      Security mask: 0x%x\n", p_scb->q_info.open.sec_mask);
1517     dprintf(fd, "      Switch result: %d\n", p_scb->q_info.open.switch_res);
1518     dprintf(fd, "      Initiator UUID: 0x%x\n", p_scb->q_info.open.uuid);
1519     dprintf(fd, "    Saved API Open peer: %s\n",
1520             p_scb->open_api.bd_addr.ToString().c_str());
1521     dprintf(fd, "      Use AVRCP: %s\n",
1522             p_scb->open_api.use_rc ? "true" : "false");
1523     dprintf(fd, "      Security mask: 0x%x\n", p_scb->open_api.sec_mask);
1524     dprintf(fd, "      Switch result: %d\n", p_scb->open_api.switch_res);
1525     dprintf(fd, "      Initiator UUID: 0x%x\n", p_scb->open_api.uuid);
1526     // TODO: Print p_scb->sep_info[], cfg, avrc_ct_timer, current_codec ?
1527     dprintf(fd, "    L2CAP Channel ID: %d\n", p_scb->l2c_cid);
1528     dprintf(fd, "    Stream MTU: %d\n", p_scb->stream_mtu);
1529     dprintf(fd, "    AVDTP version: 0x%x\n", p_scb->AvdtpVersion());
1530     dprintf(fd, "    Security mask: 0x%x\n", p_scb->sec_mask);
1531     dprintf(fd, "    Media type: %d\n", p_scb->media_type);
1532     dprintf(fd, "    Congested: %s\n", p_scb->cong ? "true" : "false");
1533     dprintf(fd, "    Open status: %d\n", p_scb->open_status);
1534     dprintf(fd, "    Channel: %d\n", p_scb->chnl);
1535     dprintf(fd, "    BTA handle: 0x%x\n", p_scb->hndl);
1536     dprintf(fd, "    Protocol service capabilities mask: 0x%x\n",
1537             p_scb->cur_psc_mask);
1538     dprintf(fd, "    AVDTP handle: %d\n", p_scb->avdt_handle);
1539     dprintf(fd, "    Stream control block index: %d\n", p_scb->hdi);
1540     dprintf(fd, "    State machine state: %s(%d)\n",
1541             bta_av_sst_code(p_scb->state), p_scb->state);
1542     dprintf(fd, "    AVDTP label: 0x%x\n", p_scb->avdt_label);
1543     dprintf(fd, "    Application ID: %d\n", p_scb->app_id);
1544     dprintf(fd, "    Role: 0x%x\n", p_scb->role);
1545     dprintf(fd, "    Queued L2CAP buffers: %d\n", p_scb->l2c_bufs);
1546     dprintf(fd, "    AVRCP allowed: %s\n", p_scb->use_rc ? "true" : "false");
1547     dprintf(fd, "    Stream started: %s\n", p_scb->started ? "true" : "false");
1548     dprintf(fd, "    Stream call-out started: %d\n", p_scb->co_started);
1549     dprintf(fd, "    AVDTP Reconfig supported: %s\n",
1550             p_scb->recfg_sup ? "true" : "false");
1551     dprintf(fd, "    AVDTP Suspend supported: %s\n",
1552             p_scb->suspend_sup ? "true" : "false");
1553     dprintf(fd, "    Deregistering: %s\n",
1554             p_scb->deregistering ? "true" : "false");
1555     dprintf(fd, "    SCO automatic Suspend: %s\n",
1556             p_scb->sco_suspend ? "true" : "false");
1557     dprintf(fd, "    Incoming/outgoing connection collusion mask: 0x%x\n",
1558             p_scb->coll_mask);
1559     dprintf(fd, "    Wait mask: 0x%x\n", p_scb->wait);
1560     dprintf(fd, "    Don't use RTP header: %s\n",
1561             p_scb->no_rtp_header ? "true" : "false");
1562     dprintf(fd, "    Intended UUID of Initiator to connect to: 0x%x\n",
1563             p_scb->uuid_int);
1564   }
1565 }
1566