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