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