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