1 /******************************************************************************
2  *
3  *  Copyright (C) 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 file contains action functions for advanced audio/video stream
22  *  state machine. these functions are shared by both audio and video
23  *  streams.
24  *
25  ******************************************************************************/
26 
27 #include "bt_target.h"
28 
29 #include <base/logging.h>
30 #include <string.h>
31 #include <vector>
32 
33 #include "avdt_api.h"
34 #include "bt_utils.h"
35 #include "bta_av_int.h"
36 #include "btif/include/btif_av_co.h"
37 #include "btif/include/btif_storage.h"
38 #include "device/include/interop.h"
39 #include "l2c_api.h"
40 #include "l2cdefs.h"
41 #include "osi/include/osi.h"
42 #include "osi/include/properties.h"
43 #include "utl.h"
44 
45 #if (BTA_AR_INCLUDED == TRUE)
46 #include "bta_ar_api.h"
47 #endif
48 
49 /*****************************************************************************
50  *  Constants
51  ****************************************************************************/
52 
53 /* the delay time in milliseconds to start service discovery on AVRCP */
54 #ifndef BTA_AV_RC_DISC_TIME_VAL
55 #define BTA_AV_RC_DISC_TIME_VAL 3500
56 #endif
57 
58 /* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed
59  * to be sent */
60 #ifndef BTA_AV_CLOSE_REQ_TIME_VAL
61 #define BTA_AV_CLOSE_REQ_TIME_VAL 4000
62 #endif
63 
64 /* number to retry on reconfigure failure - some headsets requirs this number to
65  * be more than 1 */
66 #ifndef BTA_AV_RECONFIG_RETRY
67 #define BTA_AV_RECONFIG_RETRY 6
68 #endif
69 
70 /* ACL quota we are letting FW use for A2DP Offload Tx. */
71 #define BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA 4
72 
73 static void bta_av_st_rc_timer(tBTA_AV_SCB* p_scb,
74                                UNUSED_ATTR tBTA_AV_DATA* p_data);
75 
76 /* state machine states */
77 enum {
78   BTA_AV_INIT_SST,
79   BTA_AV_INCOMING_SST,
80   BTA_AV_OPENING_SST,
81   BTA_AV_OPEN_SST,
82   BTA_AV_RCFG_SST,
83   BTA_AV_CLOSING_SST
84 };
85 
86 /* the call out functions for audio stream */
87 const tBTA_AV_CO_FUNCTS bta_av_a2dp_cos = {
88     bta_av_co_audio_init,          bta_av_co_audio_disc_res,
89     bta_av_co_audio_getconfig,     bta_av_co_audio_setconfig,
90     bta_av_co_audio_open,          bta_av_co_audio_close,
91     bta_av_co_audio_start,         bta_av_co_audio_stop,
92     bta_av_co_audio_src_data_path, bta_av_co_audio_delay,
93     bta_av_co_audio_update_mtu};
94 
95 /* ssm action functions for audio stream */
96 const tBTA_AV_SACT bta_av_a2dp_action[] = {
97     bta_av_do_disc_a2dp,    /* BTA_AV_DO_DISC  */
98     bta_av_cleanup,         /* BTA_AV_CLEANUP */
99     bta_av_free_sdb,        /* BTA_AV_FREE_SDB */
100     bta_av_config_ind,      /* BTA_AV_CONFIG_IND */
101     bta_av_disconnect_req,  /* BTA_AV_DISCONNECT_REQ */
102     bta_av_security_req,    /* BTA_AV_SECURITY_REQ */
103     bta_av_security_rsp,    /* BTA_AV_SECURITY_RSP */
104     bta_av_setconfig_rsp,   /* BTA_AV_SETCONFIG_RSP */
105     bta_av_st_rc_timer,     /* BTA_AV_ST_RC_TIMER */
106     bta_av_str_opened,      /* BTA_AV_STR_OPENED */
107     bta_av_security_ind,    /* BTA_AV_SECURITY_IND */
108     bta_av_security_cfm,    /* BTA_AV_SECURITY_CFM */
109     bta_av_do_close,        /* BTA_AV_DO_CLOSE */
110     bta_av_connect_req,     /* BTA_AV_CONNECT_REQ */
111     bta_av_sdp_failed,      /* BTA_AV_SDP_FAILED */
112     bta_av_disc_results,    /* BTA_AV_DISC_RESULTS */
113     bta_av_disc_res_as_acp, /* BTA_AV_DISC_RES_AS_ACP */
114     bta_av_open_failed,     /* BTA_AV_OPEN_FAILED */
115     bta_av_getcap_results,  /* BTA_AV_GETCAP_RESULTS */
116     bta_av_setconfig_rej,   /* BTA_AV_SETCONFIG_REJ */
117     bta_av_discover_req,    /* BTA_AV_DISCOVER_REQ */
118     bta_av_conn_failed,     /* BTA_AV_CONN_FAILED */
119     bta_av_do_start,        /* BTA_AV_DO_START */
120     bta_av_str_stopped,     /* BTA_AV_STR_STOPPED */
121     bta_av_reconfig,        /* BTA_AV_RECONFIG */
122     bta_av_data_path,       /* BTA_AV_DATA_PATH */
123     bta_av_start_ok,        /* BTA_AV_START_OK */
124     bta_av_start_failed,    /* BTA_AV_START_FAILED */
125     bta_av_str_closed,      /* BTA_AV_STR_CLOSED */
126     bta_av_clr_cong,        /* BTA_AV_CLR_CONG */
127     bta_av_suspend_cfm,     /* BTA_AV_SUSPEND_CFM */
128     bta_av_rcfg_str_ok,     /* BTA_AV_RCFG_STR_OK */
129     bta_av_rcfg_failed,     /* BTA_AV_RCFG_FAILED */
130     bta_av_rcfg_connect,    /* BTA_AV_RCFG_CONNECT */
131     bta_av_rcfg_discntd,    /* BTA_AV_RCFG_DISCNTD */
132     bta_av_suspend_cont,    /* BTA_AV_SUSPEND_CONT */
133     bta_av_rcfg_cfm,        /* BTA_AV_RCFG_CFM */
134     bta_av_rcfg_open,       /* BTA_AV_RCFG_OPEN */
135     bta_av_security_rej,    /* BTA_AV_SECURITY_REJ */
136     bta_av_open_rc,         /* BTA_AV_OPEN_RC */
137     bta_av_chk_2nd_start,   /* BTA_AV_CHK_2ND_START */
138     bta_av_save_caps,       /* BTA_AV_SAVE_CAPS */
139     bta_av_set_use_rc,      /* BTA_AV_SET_USE_RC */
140     bta_av_cco_close,       /* BTA_AV_CCO_CLOSE */
141     bta_av_switch_role,     /* BTA_AV_SWITCH_ROLE */
142     bta_av_role_res,        /* BTA_AV_ROLE_RES */
143     bta_av_delay_co,        /* BTA_AV_DELAY_CO */
144     bta_av_open_at_inc,     /* BTA_AV_OPEN_AT_INC */
145     bta_av_offload_req,     /* BTA_AV_OFFLOAD_REQ */
146     bta_av_offload_rsp,     /* BTA_AV_OFFLOAD_RSP */
147     NULL};
148 
149 /* these tables translate AVDT events to SSM events */
150 static const uint16_t bta_av_stream_evt_ok[] = {
151     BTA_AV_STR_DISC_OK_EVT,      /* AVDT_DISCOVER_CFM_EVT */
152     BTA_AV_STR_GETCAP_OK_EVT,    /* AVDT_GETCAP_CFM_EVT */
153     BTA_AV_STR_OPEN_OK_EVT,      /* AVDT_OPEN_CFM_EVT */
154     BTA_AV_STR_OPEN_OK_EVT,      /* AVDT_OPEN_IND_EVT */
155     BTA_AV_STR_CONFIG_IND_EVT,   /* AVDT_CONFIG_IND_EVT */
156     BTA_AV_STR_START_OK_EVT,     /* AVDT_START_CFM_EVT */
157     BTA_AV_STR_START_OK_EVT,     /* AVDT_START_IND_EVT */
158     BTA_AV_STR_SUSPEND_CFM_EVT,  /* AVDT_SUSPEND_CFM_EVT */
159     BTA_AV_STR_SUSPEND_CFM_EVT,  /* AVDT_SUSPEND_IND_EVT */
160     BTA_AV_STR_CLOSE_EVT,        /* AVDT_CLOSE_CFM_EVT */
161     BTA_AV_STR_CLOSE_EVT,        /* AVDT_CLOSE_IND_EVT */
162     BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
163     0,                           /* AVDT_RECONFIG_IND_EVT */
164     BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
165     BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
166     BTA_AV_STR_WRITE_CFM_EVT,    /* AVDT_WRITE_CFM_EVT */
167     BTA_AV_AVDT_CONNECT_EVT,     /* AVDT_CONNECT_IND_EVT */
168     BTA_AV_AVDT_DISCONNECT_EVT,  /* AVDT_DISCONNECT_IND_EVT */
169 #if (AVDT_REPORTING == TRUE)
170     BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
171     BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
172 #endif
173     BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
174     0                          /* AVDT_DELAY_REPORT_CFM_EVT */
175 };
176 
177 static const uint16_t bta_av_stream_evt_fail[] = {
178     BTA_AV_STR_DISC_FAIL_EVT,    /* AVDT_DISCOVER_CFM_EVT */
179     BTA_AV_STR_GETCAP_FAIL_EVT,  /* AVDT_GETCAP_CFM_EVT */
180     BTA_AV_STR_OPEN_FAIL_EVT,    /* AVDT_OPEN_CFM_EVT */
181     BTA_AV_STR_OPEN_OK_EVT,      /* AVDT_OPEN_IND_EVT */
182     BTA_AV_STR_CONFIG_IND_EVT,   /* AVDT_CONFIG_IND_EVT */
183     BTA_AV_STR_START_FAIL_EVT,   /* AVDT_START_CFM_EVT */
184     BTA_AV_STR_START_OK_EVT,     /* AVDT_START_IND_EVT */
185     BTA_AV_STR_SUSPEND_CFM_EVT,  /* AVDT_SUSPEND_CFM_EVT */
186     BTA_AV_STR_SUSPEND_CFM_EVT,  /* AVDT_SUSPEND_IND_EVT */
187     BTA_AV_STR_CLOSE_EVT,        /* AVDT_CLOSE_CFM_EVT */
188     BTA_AV_STR_CLOSE_EVT,        /* AVDT_CLOSE_IND_EVT */
189     BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
190     0,                           /* AVDT_RECONFIG_IND_EVT */
191     BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
192     BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
193     BTA_AV_STR_WRITE_CFM_EVT,    /* AVDT_WRITE_CFM_EVT */
194     BTA_AV_AVDT_CONNECT_EVT,     /* AVDT_CONNECT_IND_EVT */
195     BTA_AV_AVDT_DISCONNECT_EVT,  /* AVDT_DISCONNECT_IND_EVT */
196 #if (AVDT_REPORTING == TRUE)
197     BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
198     BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
199 #endif
200     BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
201     0                          /* AVDT_DELAY_REPORT_CFM_EVT */
202 };
203 
204 static void bta_av_stream0_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
205                                  tAVDT_CTRL* p_data);
206 static void bta_av_stream1_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
207                                  tAVDT_CTRL* p_data);
208 #if BTA_AV_NUM_STRS > 2
209 static void bta_av_stream2_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
210                                  tAVDT_CTRL* p_data);
211 #endif
212 #if BTA_AV_NUM_STRS > 3
213 static void bta_av_stream3_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
214                                  tAVDT_CTRL* p_data);
215 #endif
216 #if BTA_AV_NUM_STRS > 4
217 static void bta_av_stream4_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
218                                  tAVDT_CTRL* p_data);
219 #endif
220 #if BTA_AV_NUM_STRS > 5
221 static void bta_av_stream5_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
222                                  tAVDT_CTRL* p_data);
223 #endif
224 /* the array of callback functions to receive events from AVDT control channel
225  */
226 tAVDT_CTRL_CBACK* const bta_av_dt_cback[] = {bta_av_stream0_cback,
227                                              bta_av_stream1_cback
228 #if BTA_AV_NUM_STRS > 2
229                                              ,
230                                              bta_av_stream2_cback
231 #endif
232 #if BTA_AV_NUM_STRS > 3
233                                              ,
234                                              bta_av_stream3_cback
235 #endif
236 #if BTA_AV_NUM_STRS > 4
237                                              ,
238                                              bta_av_stream4_cback
239 #endif
240 #if BTA_AV_NUM_STRS > 5
241                                              ,
242                                              bta_av_stream5_cback
243 #endif
244 };
245 /***********************************************
246  *
247  * Function         bta_get_scb_handle
248  *
249  * Description      gives the registered AVDT handle.by checking with sep_type.
250  *
251  *
252  * Returns          void
253  **********************************************/
bta_av_get_scb_handle(tBTA_AV_SCB * p_scb,uint8_t local_sep)254 static uint8_t bta_av_get_scb_handle(tBTA_AV_SCB* p_scb, uint8_t local_sep) {
255   for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
256     if ((p_scb->seps[i].tsep == local_sep) &&
257         A2DP_CodecTypeEquals(p_scb->seps[i].codec_info,
258                              p_scb->cfg.codec_info)) {
259       return (p_scb->seps[i].av_handle);
260     }
261   }
262   APPL_TRACE_DEBUG("%s: local sep_type %d not found", __func__, local_sep)
263   return 0; /* return invalid handle */
264 }
265 
266 /***********************************************
267  *
268  * Function         bta_av_get_scb_sep_type
269  *
270  * Description      gives the sep type by cross-checking with AVDT handle
271  *
272  *
273  * Returns          void
274  **********************************************/
bta_av_get_scb_sep_type(tBTA_AV_SCB * p_scb,uint8_t tavdt_handle)275 static uint8_t bta_av_get_scb_sep_type(tBTA_AV_SCB* p_scb,
276                                        uint8_t tavdt_handle) {
277   for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
278     if (p_scb->seps[i].av_handle == tavdt_handle) return (p_scb->seps[i].tsep);
279   }
280   APPL_TRACE_DEBUG("%s: handle %d not found", __func__, tavdt_handle)
281   return AVDT_TSEP_INVALID;
282 }
283 
284 /*******************************************************************************
285  *
286  * Function         bta_av_save_addr
287  *
288  * Description      copy the bd_addr and maybe reset the supported flags
289  *
290  *
291  * Returns          void
292  *
293  ******************************************************************************/
bta_av_save_addr(tBTA_AV_SCB * p_scb,const BD_ADDR b)294 static void bta_av_save_addr(tBTA_AV_SCB* p_scb, const BD_ADDR b) {
295   APPL_TRACE_DEBUG("%s: r:%d, s:%d", __func__, p_scb->recfg_sup,
296                    p_scb->suspend_sup);
297   if (bdcmp(p_scb->peer_addr, b) != 0) {
298     APPL_TRACE_ERROR("%s: reset flags", __func__);
299     /* a new addr, reset the supported flags */
300     p_scb->recfg_sup = true;
301     p_scb->suspend_sup = true;
302   }
303 
304   /* do this copy anyway, just in case the first addr matches
305    * the control block one by accident */
306   bdcpy(p_scb->peer_addr, b);
307 }
308 
309 /*******************************************************************************
310  *
311  * Function         notify_start_failed
312  *
313  * Description      notify up-layer AV start failed
314  *
315  *
316  * Returns          void
317  *
318  ******************************************************************************/
notify_start_failed(tBTA_AV_SCB * p_scb)319 static void notify_start_failed(tBTA_AV_SCB* p_scb) {
320   tBTA_AV_START start;
321   /* if start failed, clear role */
322   p_scb->role &= ~BTA_AV_ROLE_START_INT;
323   start.chnl = p_scb->chnl;
324   start.status = BTA_AV_FAIL;
325   start.initiator = true;
326   start.hndl = p_scb->hndl;
327   (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV*)&start);
328 }
329 
330 /*******************************************************************************
331  *
332  * Function         bta_av_st_rc_timer
333  *
334  * Description      start the AVRC timer if no RC connection & CT is supported &
335  *                  RC is used or
336  *                  as ACP (we do not really know if we want AVRC)
337  *
338  * Returns          void
339  *
340  ******************************************************************************/
bta_av_st_rc_timer(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)341 static void bta_av_st_rc_timer(tBTA_AV_SCB* p_scb,
342                                UNUSED_ATTR tBTA_AV_DATA* p_data) {
343   APPL_TRACE_DEBUG("%s: rc_handle:%d, use_rc: %d", __func__, p_scb->rc_handle,
344                    p_scb->use_rc);
345   /* for outgoing RC connection as INT/CT */
346   if ((p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
347       /* (bta_av_cb.features & BTA_AV_FEAT_RCCT) && */
348       (p_scb->use_rc == true || (p_scb->role & BTA_AV_ROLE_AD_ACP))) {
349     if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
350       bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
351                           BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
352     } else {
353       p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
354     }
355   }
356 }
357 
358 /*******************************************************************************
359  *
360  * Function         bta_av_next_getcap
361  *
362  * Description      The function gets the capabilities of the next available
363  *                  stream found in the discovery results.
364  *
365  * Returns          true if we sent request to AVDT, false otherwise.
366  *
367  ******************************************************************************/
bta_av_next_getcap(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)368 static bool bta_av_next_getcap(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
369   int i;
370   tAVDT_GETCAP_REQ* p_req;
371   bool sent_cmd = false;
372   uint16_t uuid_int = p_scb->uuid_int;
373   uint8_t sep_requested = 0;
374 
375   if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
376     sep_requested = AVDT_TSEP_SNK;
377   else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
378     sep_requested = AVDT_TSEP_SRC;
379 
380   for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++) {
381     /* steam not in use, is a sink, and is the right media type (audio/video) */
382     if ((p_scb->sep_info[i].in_use == false) &&
383         (p_scb->sep_info[i].tsep == sep_requested) &&
384         (p_scb->sep_info[i].media_type == p_scb->media_type)) {
385       p_scb->sep_info_idx = i;
386 
387       /* we got a stream; get its capabilities */
388       if (p_scb->p_cap == NULL)
389         p_scb->p_cap = (tAVDT_CFG*)osi_malloc(sizeof(tAVDT_CFG));
390       if (p_scb->avdt_version >= AVDT_VERSION_SYNC) {
391         p_req = AVDT_GetAllCapReq;
392       } else {
393         p_req = AVDT_GetCapReq;
394       }
395       (*p_req)(p_scb->peer_addr, p_scb->sep_info[i].seid, p_scb->p_cap,
396                bta_av_dt_cback[p_scb->hdi]);
397       sent_cmd = true;
398       break;
399     }
400   }
401 
402   /* if no streams available then stream open fails */
403   if (!sent_cmd) {
404     bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
405   }
406 
407   return sent_cmd;
408 }
409 
410 /*******************************************************************************
411  *
412  * Function         bta_av_proc_stream_evt
413  *
414  * Description      Utility function to compose stream events.
415  *
416  * Returns          void
417  *
418  ******************************************************************************/
bta_av_proc_stream_evt(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data,int index)419 static void bta_av_proc_stream_evt(uint8_t handle, BD_ADDR bd_addr,
420                                    uint8_t event, tAVDT_CTRL* p_data,
421                                    int index) {
422   uint16_t sec_len = 0;
423   tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
424 
425   if (p_data) {
426     if (event == AVDT_SECURITY_IND_EVT) {
427       sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN)
428                     ? p_data->security_ind.len
429                     : BTA_AV_SECURITY_MAX_LEN;
430     } else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0) {
431       sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN)
432                     ? p_data->security_cfm.len
433                     : BTA_AV_SECURITY_MAX_LEN;
434     }
435   }
436 
437   if (p_scb) {
438     tBTA_AV_STR_MSG* p_msg =
439         (tBTA_AV_STR_MSG*)osi_malloc(sizeof(tBTA_AV_STR_MSG) + sec_len);
440 
441     /* copy event data, bd addr, and handle to event message buffer */
442     p_msg->hdr.offset = 0;
443 
444     if (bd_addr != NULL) {
445       bdcpy(p_msg->bd_addr, bd_addr);
446       APPL_TRACE_DEBUG("%s: bd_addr:%02x-%02x-%02x-%02x-%02x-%02x", __func__,
447                        bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3],
448                        bd_addr[4], bd_addr[5]);
449     }
450 
451     if (p_data != NULL) {
452       memcpy(&p_msg->msg, p_data, sizeof(tAVDT_CTRL));
453       /* copy config params to event message buffer */
454       switch (event) {
455         case AVDT_RECONFIG_CFM_EVT:
456           if (p_msg->msg.hdr.err_code == 0) {
457             APPL_TRACE_DEBUG(
458                 "%s: reconfig cfm event codec info = 0x%06x-%06x-%06x-%02x",
459                 __func__,
460                 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[0] << 16) +
461                     (p_msg->msg.reconfig_cfm.p_cfg->codec_info[1] << 8) +
462                     p_msg->msg.reconfig_cfm.p_cfg->codec_info[2],
463                 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[3] << 16) +
464                     (p_msg->msg.reconfig_cfm.p_cfg->codec_info[4] << 8) +
465                     p_msg->msg.reconfig_cfm.p_cfg->codec_info[5],
466                 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[6] << 16) +
467                     (p_msg->msg.reconfig_cfm.p_cfg->codec_info[7] << 8) +
468                     p_msg->msg.reconfig_cfm.p_cfg->codec_info[8],
469                 p_msg->msg.reconfig_cfm.p_cfg->codec_info[9]);
470           }
471           break;
472 
473         case AVDT_CONFIG_IND_EVT:
474           /* We might have 2 SEP signallings(A2DP + VDP) with one peer device on
475            * one L2CAP.
476            * If we already have a signalling connection with the bd_addr and the
477            * streaming
478            * SST is at INIT state, change it to INCOMING state to handle the
479            * signalling
480            * from the 2nd SEP. */
481           if ((bta_av_find_lcb(bd_addr, BTA_AV_LCB_FIND) != NULL) &&
482               (bta_av_is_scb_init(p_scb))) {
483             bta_av_set_scb_sst_incoming(p_scb);
484 
485             /* When ACP_CONNECT_EVT was received, we put first available scb to
486              * incoming state.
487              * Later when we receive AVDT_CONFIG_IND_EVT, we use a new p_scb and
488              * set its state to
489              * incoming which we do it above.
490              * We also have to set the old p_scb state to init to be used later
491              */
492             for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
493               if ((bta_av_cb.p_scb[i]) && (i != index)) {
494                 if (bta_av_cb.p_scb[i]->state == BTA_AV_INCOMING_SST) {
495                   bta_av_cb.p_scb[i]->state = BTA_AV_INIT_SST;
496                   bta_av_cb.p_scb[i]->coll_mask = 0;
497                   break;
498                 }
499               }
500             }
501           }
502 
503           memcpy(&p_msg->cfg, p_data->config_ind.p_cfg, sizeof(tAVDT_CFG));
504           break;
505 
506         case AVDT_SECURITY_IND_EVT:
507           p_msg->msg.security_ind.p_data = (uint8_t*)(p_msg + 1);
508           memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data,
509                  sec_len);
510           break;
511 
512         case AVDT_SECURITY_CFM_EVT:
513           p_msg->msg.security_cfm.p_data = (uint8_t*)(p_msg + 1);
514           if (p_data->hdr.err_code == 0) {
515             memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data,
516                    sec_len);
517           }
518           break;
519 
520         case AVDT_SUSPEND_IND_EVT:
521           p_msg->msg.hdr.err_code = 0;
522           break;
523 
524         case AVDT_CONNECT_IND_EVT:
525           p_scb->recfg_sup = true;
526           p_scb->suspend_sup = true;
527           break;
528 
529         default:
530           break;
531       }
532     } else
533       p_msg->msg.hdr.err_code = 0;
534 
535     /* look up application event */
536     if ((p_data == NULL) || (p_data->hdr.err_code == 0)) {
537       p_msg->hdr.event = bta_av_stream_evt_ok[event];
538     } else {
539       p_msg->hdr.event = bta_av_stream_evt_fail[event];
540     }
541 
542     p_msg->initiator = false;
543     if (event == AVDT_SUSPEND_CFM_EVT) p_msg->initiator = true;
544 
545     APPL_TRACE_VERBOSE("%s: hndl:x%x", __func__, p_scb->hndl);
546     p_msg->hdr.layer_specific = p_scb->hndl;
547     p_msg->handle = handle;
548     p_msg->avdt_event = event;
549     bta_sys_sendmsg(p_msg);
550   }
551 
552   if (p_data) {
553     bta_av_conn_cback(handle, bd_addr, event, p_data);
554   } else {
555     APPL_TRACE_ERROR("%s: p_data is null", __func__);
556   }
557 }
558 
559 /*******************************************************************************
560  *
561  * Function         bta_av_sink_data_cback
562  *
563  * Description      This is the AVDTP callback function for sink stream events.
564  *
565  * Returns          void
566  *
567  ******************************************************************************/
bta_av_sink_data_cback(uint8_t handle,BT_HDR * p_pkt,uint32_t time_stamp,uint8_t m_pt)568 void bta_av_sink_data_cback(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp,
569                             uint8_t m_pt) {
570   int index = 0;
571   tBTA_AV_SCB* p_scb;
572   APPL_TRACE_DEBUG(
573       "%s: avdt_handle: %d pkt_len=0x%x  offset = 0x%x "
574       "number of frames 0x%x sequence number 0x%x",
575       __func__, handle, p_pkt->len, p_pkt->offset,
576       *((uint8_t*)(p_pkt + 1) + p_pkt->offset), p_pkt->layer_specific);
577   /* Get SCB and correct sep type */
578   for (index = 0; index < BTA_AV_NUM_STRS; index++) {
579     p_scb = bta_av_cb.p_scb[index];
580     if ((p_scb->avdt_handle == handle) &&
581         (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)) {
582       break;
583     }
584   }
585   if (index == BTA_AV_NUM_STRS) {
586     /* cannot find correct handler */
587     osi_free(p_pkt);
588     return;
589   }
590   p_pkt->event = BTA_AV_SINK_MEDIA_DATA_EVT;
591   p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(BTA_AV_SINK_MEDIA_DATA_EVT,
592                                                     (tBTA_AV_MEDIA*)p_pkt);
593   /* Free the buffer: a copy of the packet has been delivered */
594   osi_free(p_pkt);
595 }
596 
597 /*******************************************************************************
598  *
599  * Function         bta_av_stream0_cback
600  *
601  * Description      This is the AVDTP callback function for stream events.
602  *
603  * Returns          void
604  *
605  ******************************************************************************/
bta_av_stream0_cback(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data)606 static void bta_av_stream0_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
607                                  tAVDT_CTRL* p_data) {
608   APPL_TRACE_VERBOSE("%s: avdt_handle: %d event=0x%x", __func__, handle, event);
609   bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 0);
610 }
611 
612 /*******************************************************************************
613  *
614  * Function         bta_av_stream1_cback
615  *
616  * Description      This is the AVDTP callback function for stream events.
617  *
618  * Returns          void
619  *
620  ******************************************************************************/
bta_av_stream1_cback(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data)621 static void bta_av_stream1_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
622                                  tAVDT_CTRL* p_data) {
623   APPL_TRACE_EVENT("%s: avdt_handle: %d event=0x%x", __func__, handle, event);
624   bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 1);
625 }
626 
627 #if BTA_AV_NUM_STRS > 2
628 /*******************************************************************************
629  *
630  * Function         bta_av_stream2_cback
631  *
632  * Description      This is the AVDTP callback function for stream events.
633  *
634  * Returns          void
635  *
636  ******************************************************************************/
bta_av_stream2_cback(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data)637 static void bta_av_stream2_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
638                                  tAVDT_CTRL* p_data) {
639   APPL_TRACE_EVENT("%s: avdt_handle: %d event=0x%x", __func__, handle, event);
640   bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 2);
641 }
642 #endif
643 
644 #if BTA_AV_NUM_STRS > 3
645 /*******************************************************************************
646  *
647  * Function         bta_av_stream3_cback
648  *
649  * Description      This is the AVDTP callback function for stream events.
650  *
651  * Returns          void
652  *
653  ******************************************************************************/
bta_av_stream3_cback(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data)654 static void bta_av_stream3_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
655                                  tAVDT_CTRL* p_data) {
656   APPL_TRACE_EVENT("%s: avdt_handle: %d event=0x%x", __func__, handle, event);
657   bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 3);
658 }
659 #endif
660 
661 /*******************************************************************************
662  *
663  * Function         bta_av_stream4_cback
664  *
665  * Description      This is the AVDTP callback function for stream events.
666  *
667  * Returns          void
668  *
669  ******************************************************************************/
670 #if BTA_AV_NUM_STRS > 4
bta_av_stream4_cback(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data)671 static void bta_av_stream4_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
672                                  tAVDT_CTRL* p_data) {
673   APPL_TRACE_EVENT("%s: avdt_handle: %d event=0x%x", __func__, handle, event);
674   bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 4);
675 }
676 #endif
677 
678 /*******************************************************************************
679  *
680  * Function         bta_av_stream5_cback
681  *
682  * Description      This is the AVDTP callback function for stream events.
683  *
684  * Returns          void
685  *
686  ******************************************************************************/
687 #if BTA_AV_NUM_STRS > 5
bta_av_stream5_cback(uint8_t handle,BD_ADDR bd_addr,uint8_t event,tAVDT_CTRL * p_data)688 static void bta_av_stream5_cback(uint8_t handle, BD_ADDR bd_addr, uint8_t event,
689                                  tAVDT_CTRL* p_data) {
690   APPL_TRACE_EVENT("%s: avdt_handle: %d event=0x%x", __func__, handle, event);
691   bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 5);
692 }
693 #endif
694 
695 /*******************************************************************************
696  *
697  * Function         bta_av_a2dp_sdp_cback
698  *
699  * Description      A2DP service discovery callback.
700  *
701  * Returns          void
702  *
703  ******************************************************************************/
bta_av_a2dp_sdp_cback(bool found,tA2DP_Service * p_service)704 static void bta_av_a2dp_sdp_cback(bool found, tA2DP_Service* p_service) {
705   tBTA_AV_SCB* p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
706 
707   if (p_scb == NULL) {
708     APPL_TRACE_ERROR("%s: no scb found for handle(0x%x)", __func__,
709                      bta_av_cb.handle);
710     return;
711   }
712 
713   tBTA_AV_SDP_RES* p_msg =
714       (tBTA_AV_SDP_RES*)osi_malloc(sizeof(tBTA_AV_SDP_RES));
715   p_msg->hdr.event =
716       (found) ? BTA_AV_SDP_DISC_OK_EVT : BTA_AV_SDP_DISC_FAIL_EVT;
717   if (found && (p_service != NULL))
718     p_scb->avdt_version = p_service->avdt_version;
719   else
720     p_scb->avdt_version = 0x00;
721   p_msg->hdr.layer_specific = bta_av_cb.handle;
722 
723   bta_sys_sendmsg(p_msg);
724 }
725 
726 /*******************************************************************************
727  *
728  * Function         bta_av_adjust_seps_idx
729  *
730  * Description      adjust the sep_idx
731  *
732  * Returns
733  *
734  ******************************************************************************/
bta_av_adjust_seps_idx(tBTA_AV_SCB * p_scb,uint8_t avdt_handle)735 static void bta_av_adjust_seps_idx(tBTA_AV_SCB* p_scb, uint8_t avdt_handle) {
736   APPL_TRACE_DEBUG("%s: codec: %s", __func__,
737                    A2DP_CodecName(p_scb->cfg.codec_info));
738   for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
739     APPL_TRACE_DEBUG("%s: av_handle: %d codec: %s", __func__,
740                      p_scb->seps[i].av_handle,
741                      A2DP_CodecName(p_scb->seps[i].codec_info));
742     if (p_scb->seps[i].av_handle && (p_scb->seps[i].av_handle == avdt_handle) &&
743         A2DP_CodecTypeEquals(p_scb->seps[i].codec_info,
744                              p_scb->cfg.codec_info)) {
745       p_scb->sep_idx = i;
746       p_scb->avdt_handle = p_scb->seps[i].av_handle;
747       break;
748     }
749   }
750 }
751 
752 /*******************************************************************************
753  *
754  * Function         bta_av_switch_role
755  *
756  * Description      Switch role was not started and a timer was started.
757  *                  another attempt to switch role now - still opening.
758  *
759  * Returns          void
760  *
761  ******************************************************************************/
bta_av_switch_role(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)762 void bta_av_switch_role(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
763   tBTA_AV_RS_RES switch_res = BTA_AV_RS_NONE;
764   tBTA_AV_API_OPEN* p_buf = &p_scb->q_info.open;
765 
766   APPL_TRACE_DEBUG("%s: wait:x%x", __func__, p_scb->wait);
767   if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
768     p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
769 
770   /* clear the masks set when the timer is started */
771   p_scb->wait &=
772       ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN | BTA_AV_WAIT_ROLE_SW_RES_START);
773 
774   if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
775     if (bta_av_switch_if_needed(p_scb) ||
776         !bta_av_link_role_ok(p_scb, A2DP_SET_MULTL_BIT)) {
777       p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
778     } else {
779       /* this should not happen in theory. Just in case...
780        * continue to do_disc_a2dp */
781       switch_res = BTA_AV_RS_DONE;
782     }
783   } else {
784     /* report failure on OPEN */
785     switch_res = BTA_AV_RS_FAIL;
786   }
787 
788   if (switch_res != BTA_AV_RS_NONE) {
789     if (bta_av_cb.rs_idx == (p_scb->hdi + 1)) {
790       bta_av_cb.rs_idx = 0;
791     }
792     p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
793     p_scb->q_tag = 0;
794     p_buf->switch_res = switch_res;
795     bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)p_buf);
796   }
797 }
798 
799 /*******************************************************************************
800  *
801  * Function         bta_av_role_res
802  *
803  * Description      Handle the role changed event
804  *
805  *
806  * Returns          void
807  *
808  ******************************************************************************/
bta_av_role_res(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)809 void bta_av_role_res(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
810   bool initiator = false;
811   tBTA_AV_START start;
812   tBTA_AV_OPEN av_open;
813 
814   APPL_TRACE_DEBUG("%s: q_tag:%d, wait:x%x, role:x%x", __func__, p_scb->q_tag,
815                    p_scb->wait, p_scb->role);
816   if (p_scb->role & BTA_AV_ROLE_START_INT) initiator = true;
817 
818   if (p_scb->q_tag == BTA_AV_Q_TAG_START) {
819     if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED) {
820       p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
821       if (p_data->role_res.hci_status != HCI_SUCCESS) {
822         p_scb->role &= ~BTA_AV_ROLE_START_INT;
823         bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
824         /* start failed because of role switch. */
825         start.chnl = p_scb->chnl;
826         start.status = BTA_AV_FAIL_ROLE;
827         start.hndl = p_scb->hndl;
828         start.initiator = initiator;
829         (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV*)&start);
830       } else {
831         bta_av_start_ok(p_scb, p_data);
832       }
833     } else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
834       p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
835   } else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
836     if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN) {
837       p_scb->role &= ~BTA_AV_ROLE_START_INT;
838       p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
839 
840       if (p_data->role_res.hci_status != HCI_SUCCESS) {
841         /* Open failed because of role switch. */
842         bdcpy(av_open.bd_addr, p_scb->peer_addr);
843         av_open.chnl = p_scb->chnl;
844         av_open.hndl = p_scb->hndl;
845         start.status = BTA_AV_FAIL_ROLE;
846         if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC)
847           av_open.sep = AVDT_TSEP_SNK;
848         else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)
849           av_open.sep = AVDT_TSEP_SRC;
850         (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV*)&av_open);
851       } else {
852         /* Continue av open process */
853         p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
854         bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)&(p_scb->q_info.open));
855       }
856     } else {
857       APPL_TRACE_WARNING(
858           "%s: unexpected role switch event: q_tag = %d wait = %d", __func__,
859           p_scb->q_tag, p_scb->wait);
860     }
861   }
862 
863   APPL_TRACE_DEBUG("%s: wait:x%x, role:x%x", __func__, p_scb->wait,
864                    p_scb->role);
865 }
866 
867 /*******************************************************************************
868  *
869  * Function         bta_av_delay_co
870  *
871  * Description      Call the delay call-out function to report the delay report
872  *                  from SNK
873  *
874  * Returns          void
875  *
876  ******************************************************************************/
bta_av_delay_co(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)877 void bta_av_delay_co(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
878   p_scb->p_cos->delay(p_scb->hndl, p_data->str_msg.msg.delay_rpt_cmd.delay);
879 }
880 
881 /*******************************************************************************
882  *
883  * Function         bta_av_do_disc_a2dp
884  *
885  * Description      Do service discovery for A2DP.
886  *
887  * Returns          void
888  *
889  ******************************************************************************/
bta_av_do_disc_a2dp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)890 void bta_av_do_disc_a2dp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
891   bool ok_continue = false;
892   tA2DP_SDP_DB_PARAMS db_params;
893   uint16_t attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
894                           ATTR_ID_PROTOCOL_DESC_LIST,
895                           ATTR_ID_BT_PROFILE_DESC_LIST};
896   uint16_t sdp_uuid = 0; /* UUID for which SDP has to be done */
897 
898   APPL_TRACE_DEBUG("%s: use_rc: %d rs:%d, oc:%d", __func__,
899                    p_data->api_open.use_rc, p_data->api_open.switch_res,
900                    bta_av_cb.audio_open_cnt);
901 
902   memcpy(&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
903 
904   switch (p_data->api_open.switch_res) {
905     case BTA_AV_RS_NONE:
906       if (bta_av_switch_if_needed(p_scb) ||
907           !bta_av_link_role_ok(p_scb, A2DP_SET_MULTL_BIT)) {
908         /* waiting for role switch result. save the api to control block */
909         memcpy(&p_scb->q_info.open, &p_data->api_open,
910                sizeof(tBTA_AV_API_OPEN));
911         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
912         p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
913       } else {
914         ok_continue = true;
915       }
916       break;
917 
918     case BTA_AV_RS_FAIL:
919       /* report a new failure event  */
920       p_scb->open_status = BTA_AV_FAIL_ROLE;
921       bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
922       break;
923 
924     case BTA_AV_RS_OK:
925       p_data = (tBTA_AV_DATA*)&p_scb->q_info.open;
926       /* continue to open if link role is ok */
927       if (bta_av_link_role_ok(p_scb, A2DP_SET_MULTL_BIT)) {
928         ok_continue = true;
929       } else {
930         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
931       }
932       break;
933 
934     case BTA_AV_RS_DONE:
935       ok_continue = true;
936       break;
937   }
938 
939   APPL_TRACE_DEBUG("%s: ok_continue: %d wait:x%x, q_tag: %d", __func__,
940                    ok_continue, p_scb->wait, p_scb->q_tag);
941   if (!ok_continue) return;
942 
943   /* clear the role switch bits */
944   p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
945 
946   if (p_scb->wait & BTA_AV_WAIT_CHECK_RC) {
947     p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
948     bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
949                         BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
950   }
951 
952   if (bta_av_cb.features & BTA_AV_FEAT_MASTER) {
953     L2CA_SetDesireRole(L2CAP_ROLE_DISALLOW_SWITCH);
954 
955     if (bta_av_cb.audio_open_cnt == 1) {
956       /* there's already an A2DP connection. do not allow switch */
957       bta_sys_clear_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
958     }
959   }
960   /* store peer addr other parameters */
961   bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
962   p_scb->sec_mask = p_data->api_open.sec_mask;
963   p_scb->use_rc = p_data->api_open.use_rc;
964 
965   bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
966 
967   if (p_scb->skip_sdp == true) {
968     tA2DP_Service a2dp_ser;
969     a2dp_ser.avdt_version = AVDT_VERSION;
970     p_scb->skip_sdp = false;
971     p_scb->uuid_int = p_data->api_open.uuid;
972     /* only one A2DP find service is active at a time */
973     bta_av_cb.handle = p_scb->hndl;
974     APPL_TRACE_WARNING("%s: Skip Sdp for incoming A2dp connection", __func__);
975     bta_av_a2dp_sdp_cback(true, &a2dp_ser);
976     return;
977   }
978 
979   /* only one A2DP find service is active at a time */
980   bta_av_cb.handle = p_scb->hndl;
981 
982   /* set up parameters */
983   db_params.db_len = BTA_AV_DISC_BUF_SIZE;
984   db_params.num_attr = 3;
985   db_params.p_attrs = attr_list;
986   p_scb->uuid_int = p_data->api_open.uuid;
987   p_scb->sdp_discovery_started = true;
988   if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SINK)
989     sdp_uuid = UUID_SERVCLASS_AUDIO_SOURCE;
990   else if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
991     sdp_uuid = UUID_SERVCLASS_AUDIO_SINK;
992 
993   APPL_TRACE_DEBUG("%s: uuid_int 0x%x, Doing SDP For 0x%x", __func__,
994                    p_scb->uuid_int, sdp_uuid);
995   if (A2DP_FindService(sdp_uuid, p_scb->peer_addr, &db_params,
996                        bta_av_a2dp_sdp_cback) == A2DP_SUCCESS)
997     return;
998 
999   /* when the code reaches here, either the DB is NULL
1000    * or A2DP_FindService is not successful */
1001   bta_av_a2dp_sdp_cback(false, NULL);
1002 }
1003 
1004 /*******************************************************************************
1005  *
1006  * Function         bta_av_cleanup
1007  *
1008  * Description      cleanup AV stream control block.
1009  *
1010  * Returns          void
1011  *
1012  ******************************************************************************/
bta_av_cleanup(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1013 void bta_av_cleanup(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1014   tBTA_AV_CONN_CHG msg;
1015   uint8_t role = BTA_AV_ROLE_AD_INT;
1016 
1017   APPL_TRACE_DEBUG("%s", __func__);
1018 
1019   /* free any buffers */
1020   osi_free_and_reset((void**)&p_scb->p_cap);
1021   p_scb->sdp_discovery_started = false;
1022   p_scb->avdt_version = 0;
1023 
1024   /* initialize some control block variables */
1025   p_scb->open_status = BTA_AV_SUCCESS;
1026 
1027   /* if de-registering shut everything down */
1028   msg.hdr.layer_specific = p_scb->hndl;
1029   p_scb->started = false;
1030   p_scb->current_codec = nullptr;
1031   p_scb->cong = false;
1032   p_scb->role = role;
1033   p_scb->cur_psc_mask = 0;
1034   p_scb->wait = 0;
1035   p_scb->num_disc_snks = 0;
1036   alarm_cancel(p_scb->avrc_ct_timer);
1037 
1038   /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
1039     vendor_get_interface()->send_command(
1040         (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP, (void*)&p_scb->l2c_cid);
1041     if (p_scb->offload_start_pending) {
1042       tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
1043       (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV*)&status);
1044     }
1045   */
1046 
1047   p_scb->offload_start_pending = false;
1048 
1049   p_scb->skip_sdp = false;
1050   if (p_scb->deregistring) {
1051     /* remove stream */
1052     for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
1053       if (p_scb->seps[i].av_handle) AVDT_RemoveStream(p_scb->seps[i].av_handle);
1054       p_scb->seps[i].av_handle = 0;
1055     }
1056 
1057     bta_av_dereg_comp((tBTA_AV_DATA*)&msg);
1058   } else {
1059     /* report stream closed to main SM */
1060     msg.is_up = false;
1061     bdcpy(msg.peer_addr, p_scb->peer_addr);
1062     bta_av_conn_chg((tBTA_AV_DATA*)&msg);
1063   }
1064 }
1065 
1066 /*******************************************************************************
1067  *
1068  * Function         bta_av_free_sdb
1069  *
1070  * Description      Free service discovery db buffer.
1071  *
1072  * Returns          void
1073  *
1074  ******************************************************************************/
bta_av_free_sdb(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1075 void bta_av_free_sdb(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1076   p_scb->sdp_discovery_started = false;
1077 }
1078 
1079 /*******************************************************************************
1080  *
1081  * Function         bta_av_config_ind
1082  *
1083  * Description      Handle a stream configuration indication from the peer.
1084  *
1085  * Returns          void
1086  *
1087  ******************************************************************************/
bta_av_config_ind(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1088 void bta_av_config_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1089   tBTA_AV_CI_SETCONFIG setconfig;
1090   tAVDT_SEP_INFO* p_info;
1091   tAVDT_CFG* p_evt_cfg = &p_data->str_msg.cfg;
1092   uint8_t psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
1093   uint8_t
1094       local_sep; /* sep type of local handle on which connection was received */
1095   tBTA_AV_STR_MSG* p_msg = (tBTA_AV_STR_MSG*)p_data;
1096 
1097   local_sep = bta_av_get_scb_sep_type(p_scb, p_msg->handle);
1098   p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1099   memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
1100   bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
1101 
1102   /* Clear collision mask */
1103   p_scb->coll_mask = 0;
1104   alarm_cancel(bta_av_cb.accept_signalling_timer);
1105 
1106   /* if no codec parameters in configuration, fail */
1107   if ((p_evt_cfg->num_codec == 0) ||
1108       /* or the peer requests for a service we do not support */
1109       ((psc_mask != p_scb->cfg.psc_mask) &&
1110        (psc_mask != (p_scb->cfg.psc_mask & ~AVDT_PSC_DELAY_RPT)))) {
1111     setconfig.hndl = p_scb->hndl; /* we may not need this */
1112     setconfig.err_code = AVDT_ERR_UNSUP_CFG;
1113     bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT,
1114                        (tBTA_AV_DATA*)&setconfig);
1115   } else {
1116     p_info = &p_scb->sep_info[0];
1117     p_info->in_use = 0;
1118     p_info->media_type = p_scb->media_type;
1119     p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
1120 
1121     /* Sep type of Peer will be oppsite role to our local sep */
1122     if (local_sep == AVDT_TSEP_SRC)
1123       p_info->tsep = AVDT_TSEP_SNK;
1124     else if (local_sep == AVDT_TSEP_SNK)
1125       p_info->tsep = AVDT_TSEP_SRC;
1126 
1127     p_scb->role |= BTA_AV_ROLE_AD_ACP;
1128     p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
1129     if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
1130       p_scb->use_rc = true;
1131     else
1132       p_scb->use_rc = false;
1133 
1134     p_scb->num_seps = 1;
1135     p_scb->sep_info_idx = 0;
1136     APPL_TRACE_DEBUG("%s: SEID: %d use_rc: %d cur_psc_mask:0x%x", __func__,
1137                      p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
1138     /*  in case of A2DP SINK this is the first time peer data is being sent to
1139      * co functions */
1140     if (local_sep == AVDT_TSEP_SNK) {
1141       p_scb->p_cos->setcfg(p_scb->hndl, p_evt_cfg->codec_info, p_info->seid,
1142                            p_scb->peer_addr, p_evt_cfg->num_protect,
1143                            p_evt_cfg->protect_info, AVDT_TSEP_SNK,
1144                            p_msg->handle);
1145     } else {
1146       p_scb->p_cos->setcfg(p_scb->hndl, p_evt_cfg->codec_info, p_info->seid,
1147                            p_scb->peer_addr, p_evt_cfg->num_protect,
1148                            p_evt_cfg->protect_info, AVDT_TSEP_SRC,
1149                            p_msg->handle);
1150     }
1151   }
1152 }
1153 
1154 /*******************************************************************************
1155  *
1156  * Function         bta_av_disconnect_req
1157  *
1158  * Description      Disconnect AVDTP connection.
1159  *
1160  * Returns          void
1161  *
1162  ******************************************************************************/
bta_av_disconnect_req(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1163 void bta_av_disconnect_req(tBTA_AV_SCB* p_scb,
1164                            UNUSED_ATTR tBTA_AV_DATA* p_data) {
1165   tBTA_AV_RCB* p_rcb;
1166 
1167   APPL_TRACE_DEBUG("%s: conn_lcb: 0x%x", __func__, bta_av_cb.conn_lcb);
1168 
1169   alarm_cancel(bta_av_cb.link_signalling_timer);
1170   alarm_cancel(p_scb->avrc_ct_timer);
1171 
1172   if (bta_av_cb.conn_lcb) {
1173     p_rcb = bta_av_get_rcb_by_shdl((uint8_t)(p_scb->hdi + 1));
1174     if (p_rcb) bta_av_del_rc(p_rcb);
1175     AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
1176   } else {
1177     bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1178   }
1179 }
1180 
1181 /*******************************************************************************
1182  *
1183  * Function         bta_av_security_req
1184  *
1185  * Description      Send an AVDTP security request.
1186  *
1187  * Returns          void
1188  *
1189  ******************************************************************************/
bta_av_security_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1190 void bta_av_security_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1191   if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1192     AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
1193                      p_data->api_protect_req.len);
1194   }
1195 }
1196 
1197 /*******************************************************************************
1198  *
1199  * Function         bta_av_security_rsp
1200  *
1201  * Description      Send an AVDTP security response.
1202  *
1203  * Returns          void
1204  *
1205  ******************************************************************************/
bta_av_security_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1206 void bta_av_security_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1207   if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1208     AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label,
1209                      p_data->api_protect_rsp.error_code,
1210                      p_data->api_protect_rsp.p_data,
1211                      p_data->api_protect_rsp.len);
1212   } else {
1213     AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL,
1214                      0);
1215   }
1216 }
1217 
1218 /*******************************************************************************
1219  *
1220  * Function         bta_av_setconfig_rsp
1221  *
1222  * Description      setconfig is OK
1223  *
1224  * Returns          void
1225  *
1226  ******************************************************************************/
bta_av_setconfig_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1227 void bta_av_setconfig_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1228   uint8_t num = p_data->ci_setconfig.num_seid + 1;
1229   uint8_t avdt_handle = p_data->ci_setconfig.avdt_handle;
1230   uint8_t* p_seid = p_data->ci_setconfig.p_seid;
1231   int i;
1232   uint8_t local_sep;
1233 
1234   /* we like this codec_type. find the sep_idx */
1235   local_sep = bta_av_get_scb_sep_type(p_scb, avdt_handle);
1236   bta_av_adjust_seps_idx(p_scb, avdt_handle);
1237   APPL_TRACE_DEBUG("%s: sep_idx: %d cur_psc_mask:0x%x", __func__,
1238                    p_scb->sep_idx, p_scb->cur_psc_mask);
1239 
1240   if ((AVDT_TSEP_SNK == local_sep) &&
1241       (p_data->ci_setconfig.err_code == AVDT_SUCCESS) &&
1242       (p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback != NULL)) {
1243     tBTA_AV_MEDIA av_sink_codec_info;
1244     memcpy(av_sink_codec_info.avk_config.bd_addr, p_scb->peer_addr,
1245            sizeof(BD_ADDR));
1246     av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
1247     p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(BTA_AV_SINK_MEDIA_CFG_EVT,
1248                                                       &av_sink_codec_info);
1249   }
1250 
1251   AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label,
1252                  p_data->ci_setconfig.err_code, p_data->ci_setconfig.category);
1253 
1254   alarm_cancel(bta_av_cb.link_signalling_timer);
1255 
1256   if (p_data->ci_setconfig.err_code == AVDT_SUCCESS) {
1257     p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
1258     if (p_data->ci_setconfig.recfg_needed)
1259       p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
1260     APPL_TRACE_DEBUG("%s: recfg_needed:%d role:x%x num:%d", __func__,
1261                      p_data->ci_setconfig.recfg_needed, p_scb->role, num);
1262     /* callout module tells BTA the number of "good" SEPs and their SEIDs.
1263      * getcap on these SEID */
1264     p_scb->num_seps = num;
1265 
1266     if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
1267       p_scb->avdt_version = AVDT_VERSION_SYNC;
1268 
1269     if (A2DP_GetCodecType(p_scb->cfg.codec_info) == A2DP_MEDIA_CT_SBC ||
1270         num > 1) {
1271       /* if SBC is used by the SNK as INT, discover req is not sent in
1272        * bta_av_config_ind.
1273                  * call disc_res now */
1274       /* this is called in A2DP SRC path only, In case of SINK we don't need it
1275        */
1276       if (local_sep == AVDT_TSEP_SRC)
1277         p_scb->p_cos->disc_res(p_scb->hndl, num, num, 0, p_scb->peer_addr,
1278                                UUID_SERVCLASS_AUDIO_SOURCE);
1279     } else {
1280       /* we do not know the peer device and it is using non-SBC codec
1281        * we need to know all the SEPs on SNK */
1282       bta_av_discover_req(p_scb, NULL);
1283       return;
1284     }
1285 
1286     for (i = 1; i < num; i++) {
1287       APPL_TRACE_DEBUG("%s: sep_info[%d] SEID: %d", __func__, i, p_seid[i - 1]);
1288       /* initialize the sep_info[] to get capabilities */
1289       p_scb->sep_info[i].in_use = false;
1290       p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
1291       p_scb->sep_info[i].media_type = p_scb->media_type;
1292       p_scb->sep_info[i].seid = p_seid[i - 1];
1293     }
1294 
1295     /* only in case of local sep as SRC we need to look for other SEPs, In case
1296      * of SINK we don't */
1297     if (local_sep == AVDT_TSEP_SRC) {
1298       /* Make sure UUID has been initialized... */
1299       if (p_scb->uuid_int == 0) p_scb->uuid_int = p_scb->open_api.uuid;
1300       bta_av_next_getcap(p_scb, p_data);
1301     }
1302   }
1303 }
1304 
1305 /*******************************************************************************
1306  *
1307  * Function         bta_av_str_opened
1308  *
1309  * Description      Stream opened OK (incoming/outgoing).
1310  *
1311  * Returns          void
1312  *
1313  ******************************************************************************/
bta_av_str_opened(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1314 void bta_av_str_opened(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1315   tBTA_AV_CONN_CHG msg;
1316   tBTA_AV_OPEN open;
1317   uint8_t* p;
1318   uint16_t mtu;
1319 
1320   msg.hdr.layer_specific = p_scb->hndl;
1321   msg.is_up = true;
1322   bdcpy(msg.peer_addr, p_scb->peer_addr);
1323   p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
1324   bta_av_conn_chg((tBTA_AV_DATA*)&msg);
1325   /* set the congestion flag, so AV would not send media packets by accident */
1326   p_scb->cong = true;
1327   p_scb->offload_start_pending = false;
1328 
1329   p_scb->stream_mtu =
1330       p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
1331   mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
1332   APPL_TRACE_DEBUG("%s: l2c_cid: 0x%x stream_mtu: %d mtu: %d", __func__,
1333                    p_scb->l2c_cid, p_scb->stream_mtu, mtu);
1334   if (mtu == 0 || mtu > p_scb->stream_mtu) mtu = p_scb->stream_mtu;
1335 
1336   /* Set the media channel as medium priority */
1337   L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_MEDIUM);
1338   L2CA_SetChnlFlushability(p_scb->l2c_cid, true);
1339 
1340   bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
1341   memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
1342 
1343   p_scb->l2c_bufs = 0;
1344   p_scb->p_cos->open(p_scb->hndl, mtu);
1345 
1346   {
1347     /* TODO check if other audio channel is open.
1348      * If yes, check if reconfig is needed
1349      * Rigt now we do not do this kind of checking.
1350      * BTA-AV is INT for 2nd audio connection.
1351      * The application needs to make sure the current codec_info is proper.
1352      * If one audio connection is open and another SNK attempts to connect to
1353      * AV,
1354      * the connection will be rejected.
1355      */
1356     /* check if other audio channel is started. If yes, start */
1357     bdcpy(open.bd_addr, p_scb->peer_addr);
1358     open.chnl = p_scb->chnl;
1359     open.hndl = p_scb->hndl;
1360     open.status = BTA_AV_SUCCESS;
1361     open.starting = bta_av_chk_start(p_scb);
1362     open.edr = 0;
1363     p = BTM_ReadRemoteFeatures(p_scb->peer_addr);
1364     if (p != NULL) {
1365       if (HCI_EDR_ACL_2MPS_SUPPORTED(p)) open.edr |= BTA_AV_EDR_2MBPS;
1366       if (HCI_EDR_ACL_3MPS_SUPPORTED(p)) open.edr |= BTA_AV_EDR_3MBPS;
1367     }
1368 #if (BTA_AR_INCLUDED == TRUE)
1369     bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr);
1370 #endif
1371     if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC)
1372       open.sep = AVDT_TSEP_SNK;
1373     else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)
1374       open.sep = AVDT_TSEP_SRC;
1375 
1376     (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV*)&open);
1377     if (open.starting) {
1378       bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
1379     }
1380   }
1381 
1382   // This code is used to pass PTS TC for AVDTP ABORT
1383   char value[PROPERTY_VALUE_MAX] = {0};
1384   if ((osi_property_get("bluetooth.pts.force_a2dp_abort", value, "false")) &&
1385       (!strcmp(value, "true"))) {
1386     APPL_TRACE_ERROR("%s: Calling AVDT_AbortReq", __func__);
1387     AVDT_AbortReq(p_scb->avdt_handle);
1388   }
1389 }
1390 
1391 /*******************************************************************************
1392  *
1393  * Function         bta_av_security_ind
1394  *
1395  * Description      Handle an AVDTP security indication.
1396  *
1397  * Returns          void
1398  *
1399  ******************************************************************************/
bta_av_security_ind(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1400 void bta_av_security_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1401   tBTA_AV_PROTECT_REQ protect_req;
1402 
1403   p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1404 
1405   if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1406     protect_req.chnl = p_scb->chnl;
1407     protect_req.hndl = p_scb->hndl;
1408     protect_req.p_data = p_data->str_msg.msg.security_ind.p_data;
1409     protect_req.len = p_data->str_msg.msg.security_ind.len;
1410 
1411     (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, (tBTA_AV*)&protect_req);
1412   }
1413   /* app doesn't support security indication; respond with failure */
1414   else {
1415     AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL,
1416                      0);
1417   }
1418 }
1419 
1420 /*******************************************************************************
1421  *
1422  * Function         bta_av_security_cfm
1423  *
1424  * Description      Handle an AVDTP security confirm.
1425  *
1426  * Returns          void
1427  *
1428  ******************************************************************************/
bta_av_security_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1429 void bta_av_security_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1430   tBTA_AV_PROTECT_RSP protect_rsp;
1431 
1432   if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1433     protect_rsp.chnl = p_scb->chnl;
1434     protect_rsp.hndl = p_scb->hndl;
1435     protect_rsp.p_data = p_data->str_msg.msg.security_cfm.p_data;
1436     protect_rsp.len = p_data->str_msg.msg.security_cfm.len;
1437     protect_rsp.err_code = p_data->str_msg.msg.hdr.err_code;
1438 
1439     (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, (tBTA_AV*)&protect_rsp);
1440   }
1441 }
1442 
1443 /*******************************************************************************
1444  *
1445  * Function         bta_av_do_close
1446  *
1447  * Description      Close stream.
1448  *
1449  * Returns          void
1450  *
1451  ******************************************************************************/
bta_av_do_close(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1452 void bta_av_do_close(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1453   /* stop stream if started */
1454   if (p_scb->co_started) {
1455     bta_av_str_stopped(p_scb, NULL);
1456   }
1457   alarm_cancel(bta_av_cb.link_signalling_timer);
1458 
1459   /* close stream */
1460   p_scb->started = false;
1461   p_scb->current_codec = nullptr;
1462 
1463   /* drop the buffers queued in L2CAP */
1464   L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1465 
1466   AVDT_CloseReq(p_scb->avdt_handle);
1467   /* just in case that the link is congested, link is flow controled by peer or
1468    * for whatever reason the the close request can not be sent in time.
1469    * when this timer expires, AVDT_DisconnectReq will be called to disconnect
1470    * the link
1471    */
1472   bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_CLOSE_REQ_TIME_VAL,
1473                       BTA_AV_API_CLOSE_EVT, p_scb->hndl);
1474 }
1475 
1476 /*******************************************************************************
1477  *
1478  * Function         bta_av_connect_req
1479  *
1480  * Description      Connect AVDTP connection.
1481  *
1482  * Returns          void
1483  *
1484  ******************************************************************************/
bta_av_connect_req(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1485 void bta_av_connect_req(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1486   p_scb->sdp_discovery_started = false;
1487   if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1488     /* SNK initiated L2C connection while SRC was doing SDP.    */
1489     /* Wait until timeout to check if SNK starts signalling.    */
1490     APPL_TRACE_EVENT("%s: coll_mask = 0x%2X", __func__, p_scb->coll_mask);
1491     p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
1492     APPL_TRACE_EVENT("%s: updated coll_mask = 0x%2X", __func__,
1493                      p_scb->coll_mask);
1494     return;
1495   }
1496 
1497   AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask,
1498                   bta_av_dt_cback[p_scb->hdi]);
1499 }
1500 
1501 /*******************************************************************************
1502  *
1503  * Function         bta_av_sdp_failed
1504  *
1505  * Description      Service discovery failed.
1506  *
1507  * Returns          void
1508  *
1509  ******************************************************************************/
bta_av_sdp_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1510 void bta_av_sdp_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1511   if (!p_scb->open_status) p_scb->open_status = BTA_AV_FAIL_SDP;
1512 
1513   p_scb->sdp_discovery_started = false;
1514   bta_av_str_closed(p_scb, p_data);
1515 }
1516 
1517 /*******************************************************************************
1518  *
1519  * Function         bta_av_disc_results
1520  *
1521  * Description      Handle the AVDTP discover results.  Search through the
1522  *                  results and find the first available stream, and get
1523  *                  its capabilities.
1524  *
1525  * Returns          void
1526  *
1527  ******************************************************************************/
bta_av_disc_results(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1528 void bta_av_disc_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1529   uint8_t num_snks = 0, num_srcs = 0, i;
1530   /* our uuid in case we initiate connection */
1531   uint16_t uuid_int = p_scb->uuid_int;
1532 
1533   APPL_TRACE_DEBUG("%s: initiator UUID 0x%x", __func__, uuid_int);
1534   /* store number of stream endpoints returned */
1535   p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1536 
1537   for (i = 0; i < p_scb->num_seps; i++) {
1538     /* steam not in use, is a sink, and is audio */
1539     if ((p_scb->sep_info[i].in_use == false) &&
1540         (p_scb->sep_info[i].media_type == p_scb->media_type)) {
1541       if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1542           (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE))
1543         num_snks++;
1544 
1545       if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SRC) &&
1546           (uuid_int == UUID_SERVCLASS_AUDIO_SINK))
1547         num_srcs++;
1548     }
1549   }
1550 
1551   p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, num_srcs,
1552                          p_scb->peer_addr, uuid_int);
1553   p_scb->num_disc_snks = num_snks;
1554   p_scb->num_disc_srcs = num_srcs;
1555 
1556   /* if we got any */
1557   if (p_scb->num_seps > 0) {
1558     /* initialize index into discovery results */
1559     p_scb->sep_info_idx = 0;
1560 
1561     /* get the capabilities of the first available stream */
1562     bta_av_next_getcap(p_scb, p_data);
1563   }
1564   /* else we got discover response but with no streams; we're done */
1565   else {
1566     bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1567   }
1568 }
1569 
1570 /*******************************************************************************
1571  *
1572  * Function         bta_av_disc_res_as_acp
1573  *
1574  * Description      Handle the AVDTP discover results.  Search through the
1575  *                  results and find the first available stream, and get
1576  *                  its capabilities.
1577  *
1578  * Returns          void
1579  *
1580  ******************************************************************************/
bta_av_disc_res_as_acp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1581 void bta_av_disc_res_as_acp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1582   uint8_t num_snks = 0, i;
1583 
1584   /* store number of stream endpoints returned */
1585   p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1586 
1587   for (i = 0; i < p_scb->num_seps; i++) {
1588     /* steam is a sink, and is audio */
1589     if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1590         (p_scb->sep_info[i].media_type == p_scb->media_type)) {
1591       p_scb->sep_info[i].in_use = false;
1592       num_snks++;
1593     }
1594   }
1595   p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, 0,
1596                          p_scb->peer_addr, UUID_SERVCLASS_AUDIO_SOURCE);
1597   p_scb->num_disc_snks = num_snks;
1598   p_scb->num_disc_srcs = 0;
1599 
1600   /* if we got any */
1601   if (p_scb->num_seps > 0) {
1602     /* initialize index into discovery results */
1603     p_scb->sep_info_idx = 0;
1604 
1605     /* get the capabilities of the first available stream */
1606     bta_av_next_getcap(p_scb, p_data);
1607   }
1608   /* else we got discover response but with no streams; we're done */
1609   else {
1610     bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1611   }
1612 }
1613 
1614 /*******************************************************************************
1615  *
1616  * Function         bta_av_save_caps
1617  *
1618  * Description      report the SNK SEP capabilities to application
1619  *
1620  * Returns          void
1621  *
1622  ******************************************************************************/
bta_av_save_caps(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1623 void bta_av_save_caps(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1624   tAVDT_CFG cfg;
1625   tAVDT_SEP_INFO* p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1626   uint8_t old_wait = p_scb->wait;
1627   bool getcap_done = false;
1628 
1629   APPL_TRACE_DEBUG("%s: num_seps:%d sep_info_idx:%d wait:x%x", __func__,
1630                    p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
1631   memcpy(&cfg, p_scb->p_cap, sizeof(tAVDT_CFG));
1632   /* let application know the capability of the SNK */
1633   p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info, &p_scb->sep_info_idx,
1634                        p_info->seid, &cfg.num_protect, cfg.protect_info);
1635 
1636   p_scb->sep_info_idx++;
1637   if (p_scb->num_seps > p_scb->sep_info_idx) {
1638     /* Some devices have seps at the end of the discover list, which is not */
1639     /* matching media type(video not audio).                                */
1640     /* In this case, we are done with getcap without sending another        */
1641     /* request to AVDT.                                                     */
1642     if (!bta_av_next_getcap(p_scb, p_data)) getcap_done = true;
1643   } else
1644     getcap_done = true;
1645 
1646   if (getcap_done) {
1647     /* we are done getting capabilities. restore the p_cb->sep_info_idx */
1648     p_scb->sep_info_idx = 0;
1649     p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON | BTA_AV_WAIT_ACP_CAPS_STARTED);
1650     if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED) {
1651       bta_av_start_ok(p_scb, NULL);
1652     }
1653   }
1654 }
1655 
1656 /*******************************************************************************
1657  *
1658  * Function         bta_av_set_use_rc
1659  *
1660  * Description      set to use AVRC for this stream control block.
1661  *
1662  * Returns          void
1663  *
1664  ******************************************************************************/
bta_av_set_use_rc(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1665 void bta_av_set_use_rc(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1666   p_scb->use_rc = true;
1667 }
1668 
1669 /*******************************************************************************
1670  *
1671  * Function         bta_av_cco_close
1672  *
1673  * Description      call close call-out function.
1674  *
1675  * Returns          void
1676  *
1677  ******************************************************************************/
bta_av_cco_close(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1678 void bta_av_cco_close(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1679   uint16_t mtu;
1680 
1681   mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
1682 
1683   p_scb->p_cos->close(p_scb->hndl);
1684 }
1685 
1686 /*******************************************************************************
1687  *
1688  * Function         bta_av_open_failed
1689  *
1690  * Description      Failed to open an AVDT stream
1691  *
1692  * Returns          void
1693  *
1694  ******************************************************************************/
bta_av_open_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1695 void bta_av_open_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1696   bool is_av_opened = false;
1697   tBTA_AV_SCB* p_opened_scb = NULL;
1698   uint8_t idx;
1699   tBTA_AV_OPEN open;
1700 
1701   APPL_TRACE_DEBUG("%s", __func__);
1702   p_scb->open_status = BTA_AV_FAIL_STREAM;
1703   bta_av_cco_close(p_scb, p_data);
1704 
1705   /* check whether there is already an opened audio or video connection with the
1706    * same device */
1707   for (idx = 0; (idx < BTA_AV_NUM_STRS) && (is_av_opened == false); idx++) {
1708     p_opened_scb = bta_av_cb.p_scb[idx];
1709     if (p_opened_scb && (p_opened_scb->state == BTA_AV_OPEN_SST) &&
1710         (!bdcmp(p_opened_scb->peer_addr, p_scb->peer_addr)))
1711       is_av_opened = true;
1712   }
1713 
1714   /* if there is already an active AV connnection with the same bd_addr,
1715      don't send disconnect req, just report the open event with
1716      BTA_AV_FAIL_GET_CAP status */
1717   if (is_av_opened == true) {
1718     bdcpy(open.bd_addr, p_scb->peer_addr);
1719     open.chnl = p_scb->chnl;
1720     open.hndl = p_scb->hndl;
1721     open.status = BTA_AV_FAIL_GET_CAP;
1722     open.starting = bta_av_chk_start(p_scb);
1723     open.edr = 0;
1724     /* set the state back to initial state */
1725     bta_av_set_scb_sst_init(p_scb);
1726 
1727     if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC)
1728       open.sep = AVDT_TSEP_SNK;
1729     else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)
1730       open.sep = AVDT_TSEP_SRC;
1731 
1732     (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV*)&open);
1733 
1734   } else {
1735     AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
1736   }
1737 }
1738 
1739 /*******************************************************************************
1740  *
1741  * Function         bta_av_getcap_results
1742  *
1743  * Description      Handle the AVDTP get capabilities results.  Check the codec
1744  *                  type and see if it matches ours.  If it does not, get the
1745  *                  capabilities of the next stream, if any.
1746  *
1747  * Returns          void
1748  *
1749  ******************************************************************************/
bta_av_getcap_results(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1750 void bta_av_getcap_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1751   tAVDT_CFG cfg;
1752   uint8_t media_type;
1753   tAVDT_SEP_INFO* p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1754   uint16_t uuid_int; /* UUID for which connection was initiatied */
1755 
1756   memcpy(&cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
1757   cfg.num_codec = 1;
1758   cfg.num_protect = p_scb->p_cap->num_protect;
1759   memcpy(cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
1760   memcpy(cfg.protect_info, p_scb->p_cap->protect_info, AVDT_PROTECT_SIZE);
1761   media_type = A2DP_GetMediaType(p_scb->p_cap->codec_info);
1762 
1763   APPL_TRACE_DEBUG("%s: num_codec %d", __func__, p_scb->p_cap->num_codec);
1764   APPL_TRACE_DEBUG("%s: media type x%x, x%x", __func__, media_type,
1765                    p_scb->media_type);
1766 
1767   /* if codec present and we get a codec configuration */
1768   if ((p_scb->p_cap->num_codec != 0) && (media_type == p_scb->media_type) &&
1769       (p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info, &p_scb->sep_info_idx,
1770                             p_info->seid, &cfg.num_protect,
1771                             cfg.protect_info) == A2DP_SUCCESS)) {
1772     /* save copy of codec configuration */
1773     memcpy(&p_scb->cfg, &cfg, sizeof(tAVDT_CFG));
1774 
1775     uuid_int = p_scb->uuid_int;
1776     APPL_TRACE_DEBUG("%s: initiator UUID = 0x%x", __func__, uuid_int);
1777     if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
1778       bta_av_adjust_seps_idx(p_scb,
1779                              bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
1780     else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
1781       bta_av_adjust_seps_idx(p_scb,
1782                              bta_av_get_scb_handle(p_scb, AVDT_TSEP_SNK));
1783 
1784     /* use only the services peer supports */
1785     cfg.psc_mask &= p_scb->p_cap->psc_mask;
1786     p_scb->cur_psc_mask = cfg.psc_mask;
1787 
1788     if ((uuid_int == UUID_SERVCLASS_AUDIO_SINK) &&
1789         (p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback != NULL)) {
1790       APPL_TRACE_DEBUG("%s: configure decoder for Sink connection", __func__);
1791       tBTA_AV_MEDIA av_sink_codec_info;
1792       memcpy(av_sink_codec_info.avk_config.bd_addr, p_scb->peer_addr,
1793              sizeof(BD_ADDR));
1794       av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
1795       p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(
1796           BTA_AV_SINK_MEDIA_CFG_EVT, &av_sink_codec_info);
1797     }
1798 
1799     if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE) {
1800       A2DP_AdjustCodec(cfg.codec_info);
1801     }
1802 
1803     /* open the stream */
1804     AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->peer_addr,
1805                  p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
1806 
1807     if (!bta_av_is_rcfg_sst(p_scb)) {
1808       /* free capabilities buffer */
1809       osi_free_and_reset((void**)&p_scb->p_cap);
1810     }
1811   } else {
1812     /* try the next stream, if any */
1813     p_scb->sep_info_idx++;
1814     bta_av_next_getcap(p_scb, p_data);
1815   }
1816 }
1817 
1818 /*******************************************************************************
1819  *
1820  * Function         bta_av_setconfig_rej
1821  *
1822  * Description      Send AVDTP set config reject.
1823  *
1824  * Returns          void
1825  *
1826  ******************************************************************************/
bta_av_setconfig_rej(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1827 void bta_av_setconfig_rej(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1828   tBTA_AV_REJECT reject;
1829   uint8_t avdt_handle = p_data->ci_setconfig.avdt_handle;
1830 
1831   bta_av_adjust_seps_idx(p_scb, avdt_handle);
1832   APPL_TRACE_DEBUG("%s: sep_idx: %d", __func__, p_scb->sep_idx);
1833   AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_UNSUP_CFG, 0);
1834 
1835   bdcpy(reject.bd_addr, p_data->str_msg.bd_addr);
1836   reject.hndl = p_scb->hndl;
1837   (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, (tBTA_AV*)&reject);
1838 }
1839 
1840 /*******************************************************************************
1841  *
1842  * Function         bta_av_discover_req
1843  *
1844  * Description      Send an AVDTP discover request to the peer.
1845  *
1846  * Returns          void
1847  *
1848  ******************************************************************************/
bta_av_discover_req(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1849 void bta_av_discover_req(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1850   /* send avdtp discover request */
1851 
1852   AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS,
1853                    bta_av_dt_cback[p_scb->hdi]);
1854 }
1855 
1856 /*******************************************************************************
1857  *
1858  * Function         bta_av_conn_failed
1859  *
1860  * Description      AVDTP connection failed.
1861  *
1862  * Returns          void
1863  *
1864  ******************************************************************************/
bta_av_conn_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1865 void bta_av_conn_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1866   p_scb->open_status = BTA_AV_FAIL_STREAM;
1867   bta_av_str_closed(p_scb, p_data);
1868 }
1869 
1870 /*******************************************************************************
1871  *
1872  * Function         bta_av_do_start
1873  *
1874  * Description      Start stream.
1875  *
1876  * Returns          void
1877  *
1878  ******************************************************************************/
bta_av_do_start(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1879 void bta_av_do_start(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1880   uint8_t policy = HCI_ENABLE_SNIFF_MODE;
1881   uint8_t cur_role;
1882 
1883   APPL_TRACE_DEBUG("%s: sco_occupied:%d, role:x%x, started:%d", __func__,
1884                    bta_av_cb.sco_occupied, p_scb->role, p_scb->started);
1885   if (bta_av_cb.sco_occupied) {
1886     bta_av_start_failed(p_scb, p_data);
1887     return;
1888   }
1889 
1890   /* disallow role switch during streaming, only if we are the master role
1891    * i.e. allow role switch, if we are slave.
1892    * It would not hurt us, if the peer device wants us to be master */
1893   if ((BTM_GetRole(p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
1894       (cur_role == BTM_ROLE_MASTER)) {
1895     policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1896   }
1897 
1898   bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
1899 
1900   if ((p_scb->started == false) &&
1901       ((p_scb->role & BTA_AV_ROLE_START_INT) == 0)) {
1902     p_scb->role |= BTA_AV_ROLE_START_INT;
1903     bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
1904 
1905     AVDT_StartReq(&p_scb->avdt_handle, 1);
1906   } else if (p_scb->started) {
1907     p_scb->role |= BTA_AV_ROLE_START_INT;
1908     if (p_scb->wait == 0) {
1909       if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
1910         notify_start_failed(p_scb);
1911       } else {
1912         bta_av_start_ok(p_scb, NULL);
1913       }
1914     }
1915   }
1916   APPL_TRACE_DEBUG("%s: started %d role:x%x", __func__, p_scb->started,
1917                    p_scb->role);
1918 }
1919 
1920 /*******************************************************************************
1921  *
1922  * Function         bta_av_str_stopped
1923  *
1924  * Description      Stream stopped.
1925  *
1926  * Returns          void
1927  *
1928  ******************************************************************************/
bta_av_str_stopped(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1929 void bta_av_str_stopped(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1930   tBTA_AV_SUSPEND suspend_rsp;
1931   uint8_t start = p_scb->started;
1932   bool sus_evt = true;
1933   BT_HDR* p_buf;
1934   uint8_t policy = HCI_ENABLE_SNIFF_MODE;
1935 
1936   APPL_TRACE_ERROR("%s: audio_open_cnt=%d, p_data %p", __func__,
1937                    bta_av_cb.audio_open_cnt, p_data);
1938 
1939   bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
1940   if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 ||
1941       bta_av_cb.audio_open_cnt == 1)
1942     policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1943   bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
1944 
1945   if (p_scb->co_started) {
1946     /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
1947     vendor_get_interface()->send_command(
1948         (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP, (void*)&p_scb->l2c_cid);
1949     if (p_scb->offload_start_pending) {
1950       tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
1951       (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV*)&status);
1952     }
1953     p_scb->offload_start_pending = false;
1954     */
1955 
1956     bta_av_stream_chg(p_scb, false);
1957     p_scb->co_started = false;
1958 
1959     p_scb->p_cos->stop(p_scb->hndl);
1960     L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
1961   }
1962 
1963   /* if q_info.a2dp_list is not empty, drop it now */
1964   if (BTA_AV_CHNL_AUDIO == p_scb->chnl) {
1965     while (!list_is_empty(p_scb->a2dp_list)) {
1966       p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
1967       list_remove(p_scb->a2dp_list, p_buf);
1968       osi_free(p_buf);
1969     }
1970 
1971     /* drop the audio buffers queued in L2CAP */
1972     if (p_data && p_data->api_stop.flush)
1973       L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1974   }
1975 
1976   suspend_rsp.chnl = p_scb->chnl;
1977   suspend_rsp.hndl = p_scb->hndl;
1978 
1979   if (p_data && p_data->api_stop.suspend) {
1980     APPL_TRACE_DEBUG("%s: suspending: %d, sup:%d", __func__, start,
1981                      p_scb->suspend_sup);
1982     if ((start) && (p_scb->suspend_sup)) {
1983       sus_evt = false;
1984       p_scb->l2c_bufs = 0;
1985       AVDT_SuspendReq(&p_scb->avdt_handle, 1);
1986     }
1987 
1988     /* send SUSPEND_EVT event only if not in reconfiguring state and sus_evt is
1989      * true*/
1990     if ((sus_evt) && (p_scb->state != BTA_AV_RCFG_SST)) {
1991       suspend_rsp.status = BTA_AV_SUCCESS;
1992       suspend_rsp.initiator = true;
1993       (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV*)&suspend_rsp);
1994     }
1995   } else {
1996     suspend_rsp.status = BTA_AV_SUCCESS;
1997     suspend_rsp.initiator = true;
1998     APPL_TRACE_EVENT("%s: status %d", __func__, suspend_rsp.status);
1999 
2000     // Send STOP_EVT event only if not in reconfiguring state.
2001     // However, we should send STOP_EVT if we are reconfiguring when taking
2002     // the Close->Configure->Open->Start path.
2003     if (p_scb->state != BTA_AV_RCFG_SST ||
2004         (p_data && p_data->api_stop.reconfig_stop)) {
2005       (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, (tBTA_AV*)&suspend_rsp);
2006     }
2007   }
2008 }
2009 
2010 /*******************************************************************************
2011  *
2012  * Function         bta_av_reconfig
2013  *
2014  * Description      process the reconfigure request.
2015  *                  save the parameter in control block and
2016  *                  suspend, reconfigure or close the stream
2017  *
2018  * Returns          void
2019  *
2020  ******************************************************************************/
bta_av_reconfig(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2021 void bta_av_reconfig(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2022   tAVDT_CFG* p_cfg;
2023   tBTA_AV_API_STOP stop;
2024   tBTA_AV_API_RCFG* p_rcfg = &p_data->api_reconfig;
2025 
2026   APPL_TRACE_DEBUG("%s: r:%d, s:%d idx: %d (o:%d)", __func__, p_scb->recfg_sup,
2027                    p_scb->suspend_sup, p_scb->rcfg_idx, p_scb->sep_info_idx);
2028 
2029   p_scb->num_recfg = 0;
2030   /* store the new configuration in control block */
2031   if (p_scb->p_cap == NULL)
2032     p_scb->p_cap = (tAVDT_CFG*)osi_malloc(sizeof(tAVDT_CFG));
2033   p_cfg = p_scb->p_cap;
2034 
2035   alarm_cancel(p_scb->avrc_ct_timer);
2036 
2037   memcpy(p_cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
2038   p_cfg->num_protect = p_rcfg->num_protect;
2039   memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
2040   memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
2041   p_scb->rcfg_idx = p_rcfg->sep_info_idx;
2042   p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
2043 
2044   // If the requested SEP index is same as the current one, then we
2045   // can Suspend->Reconfigure->Start.
2046   // Otherwise, we have to Close->Configure->Open->Start or
2047   // Close->Configure->Open for streams that are / are not started.
2048   if ((p_scb->rcfg_idx == p_scb->sep_info_idx) && p_rcfg->suspend &&
2049       p_scb->recfg_sup && p_scb->suspend_sup) {
2050     if (p_scb->started) {
2051       // Suspend->Reconfigure->Start
2052       stop.flush = false;
2053       stop.suspend = true;
2054       stop.reconfig_stop = false;
2055       bta_av_str_stopped(p_scb, (tBTA_AV_DATA*)&stop);
2056     } else {
2057       // Reconfigure
2058       APPL_TRACE_DEBUG("%s: reconfig", __func__);
2059       AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
2060       p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
2061     }
2062   } else {
2063     // Close the stream first, and then Configure it
2064     APPL_TRACE_DEBUG("%s: Close/Open started: %d state: %d num_protect: %d",
2065                      __func__, p_scb->started, p_scb->state,
2066                      p_cfg->num_protect);
2067     if (p_scb->started) {
2068       // Close->Configure->Open->Start
2069       if ((p_scb->rcfg_idx != p_scb->sep_info_idx) && p_scb->recfg_sup) {
2070         // Make sure we trigger STOP_EVT when taking the longer road to
2071         // reconfiguration, otherwise we don't call Start.
2072         stop.flush = false;
2073         stop.suspend = false;
2074         stop.reconfig_stop = true;
2075         bta_av_str_stopped(p_scb, (tBTA_AV_DATA*)&stop);
2076       } else {
2077         bta_av_str_stopped(p_scb, NULL);
2078       }
2079       p_scb->started = false;
2080     } else {
2081       // Close->Configure->Open
2082       bta_av_str_stopped(p_scb, NULL);
2083     }
2084     // Drop the buffers queued in L2CAP
2085     L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2086     AVDT_CloseReq(p_scb->avdt_handle);
2087   }
2088 }
2089 
2090 /*******************************************************************************
2091  *
2092  * Function         bta_av_data_path
2093  *
2094  * Description      Handle stream data path.
2095  *
2096  * Returns          void
2097  *
2098  ******************************************************************************/
bta_av_data_path(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2099 void bta_av_data_path(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2100   BT_HDR* p_buf = NULL;
2101   uint32_t timestamp;
2102   bool new_buf = false;
2103   uint8_t m_pt = 0x60;
2104   tAVDT_DATA_OPT_MASK opt;
2105 
2106   if (p_scb->cong) return;
2107 
2108   if (p_scb->current_codec->useRtpHeaderMarkerBit()) {
2109     m_pt |= AVDT_MARKER_SET;
2110   }
2111 
2112   // Always get the current number of bufs que'd up
2113   p_scb->l2c_bufs =
2114       (uint8_t)L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
2115 
2116   if (!list_is_empty(p_scb->a2dp_list)) {
2117     p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
2118     list_remove(p_scb->a2dp_list, p_buf);
2119     /* use q_info.a2dp data, read the timestamp */
2120     timestamp = *(uint32_t*)(p_buf + 1);
2121   } else {
2122     new_buf = true;
2123     /* A2DP_list empty, call co_data, dup data to other channels */
2124     p_buf = (BT_HDR*)p_scb->p_cos->data(p_scb->cfg.codec_info, &timestamp);
2125 
2126     if (p_buf) {
2127       /* use the offset area for the time stamp */
2128       *(uint32_t*)(p_buf + 1) = timestamp;
2129 
2130       /* dup the data to other channels */
2131       bta_av_dup_audio_buf(p_scb, p_buf);
2132     }
2133   }
2134 
2135   if (p_buf) {
2136     if (p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM)) {
2137       /* There's a buffer, just queue it to L2CAP.
2138        * There's no need to increment it here, it is always read from
2139        * L2CAP (see above).
2140        */
2141 
2142       /* opt is a bit mask, it could have several options set */
2143       opt = AVDT_DATA_OPT_NONE;
2144       if (p_scb->no_rtp_hdr) {
2145         opt |= AVDT_DATA_OPT_NO_RTP;
2146       }
2147 
2148       //
2149       // Fragment the payload if larger than the MTU.
2150       // NOTE: The fragmentation is RTP-compatibie.
2151       //
2152       size_t extra_fragments_n = 0;
2153       if (p_buf->len > 0) {
2154         extra_fragments_n = (p_buf->len / p_scb->stream_mtu) +
2155                             ((p_buf->len % p_scb->stream_mtu) ? 1 : 0) - 1;
2156       }
2157       std::vector<BT_HDR*> extra_fragments;
2158       extra_fragments.reserve(extra_fragments_n);
2159 
2160       uint8_t* data_begin = (uint8_t*)(p_buf + 1) + p_buf->offset;
2161       uint8_t* data_end = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len;
2162       while (extra_fragments_n-- > 0) {
2163         data_begin += p_scb->stream_mtu;
2164         size_t fragment_len = data_end - data_begin;
2165         if (fragment_len > p_scb->stream_mtu) fragment_len = p_scb->stream_mtu;
2166 
2167         BT_HDR* p_buf2 = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
2168         p_buf2->offset = p_buf->offset;
2169         p_buf2->len = 0;
2170         p_buf2->layer_specific = 0;
2171         uint8_t* packet2 =
2172             (uint8_t*)(p_buf2 + 1) + p_buf2->offset + p_buf2->len;
2173         memcpy(packet2, data_begin, fragment_len);
2174         p_buf2->len += fragment_len;
2175         extra_fragments.push_back(p_buf2);
2176         p_buf->len -= fragment_len;
2177       }
2178 
2179       if (!extra_fragments.empty()) {
2180         // Reset the RTP Marker bit for all fragments except the last one
2181         m_pt &= ~AVDT_MARKER_SET;
2182       }
2183       AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf, timestamp, m_pt, opt);
2184       for (size_t i = 0; i < extra_fragments.size(); i++) {
2185         if (i + 1 == extra_fragments.size()) {
2186           // Set the RTP Marker bit for the last fragment
2187           m_pt |= AVDT_MARKER_SET;
2188         }
2189         BT_HDR* p_buf2 = extra_fragments[i];
2190         AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf2, timestamp, m_pt, opt);
2191       }
2192       p_scb->cong = true;
2193     } else {
2194       /* there's a buffer, but L2CAP does not seem to be moving data */
2195       if (new_buf) {
2196         /* just got this buffer from co_data,
2197          * put it in queue */
2198         list_append(p_scb->a2dp_list, p_buf);
2199       } else {
2200         /* just dequeue it from the a2dp_list */
2201         if (list_length(p_scb->a2dp_list) < 3) {
2202           /* put it back to the queue */
2203           list_prepend(p_scb->a2dp_list, p_buf);
2204         } else {
2205           /* too many buffers in a2dp_list, drop it. */
2206           bta_av_co_audio_drop(p_scb->hndl);
2207           osi_free(p_buf);
2208         }
2209       }
2210     }
2211   }
2212 }
2213 
2214 /*******************************************************************************
2215  *
2216  * Function         bta_av_start_ok
2217  *
2218  * Description      Stream started.
2219  *
2220  * Returns          void
2221  *
2222  ******************************************************************************/
bta_av_start_ok(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2223 void bta_av_start_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2224   tBTA_AV_START start;
2225   bool initiator = false;
2226   bool suspend = false;
2227   uint16_t flush_to;
2228   uint8_t new_role = p_scb->role;
2229   BT_HDR hdr;
2230   uint8_t policy = HCI_ENABLE_SNIFF_MODE;
2231   uint8_t cur_role;
2232 
2233   APPL_TRACE_DEBUG("%s: wait:x%x, role:x%x", __func__, p_scb->wait,
2234                    p_scb->role);
2235 
2236   p_scb->started = true;
2237   p_scb->current_codec = bta_av_get_a2dp_current_codec();
2238 
2239   if (p_scb->sco_suspend) {
2240     p_scb->sco_suspend = false;
2241   }
2242 
2243   if (new_role & BTA_AV_ROLE_START_INT) initiator = true;
2244 
2245   /* for A2DP SINK we do not send get_caps */
2246   if ((p_scb->avdt_handle == p_scb->seps[p_scb->sep_idx].av_handle) &&
2247       (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)) {
2248     p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON);
2249     APPL_TRACE_DEBUG("%s: local SEP type is SNK new wait is 0x%x", __func__,
2250                      p_scb->wait);
2251   }
2252   if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED) {
2253     /* role switch has failed */
2254     p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
2255     p_data = (tBTA_AV_DATA*)&hdr;
2256     hdr.offset = BTA_AV_RS_FAIL;
2257   }
2258   APPL_TRACE_DEBUG("%s: wait:x%x", __func__, p_scb->wait);
2259 
2260   if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE)) {
2261     p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2262     if (p_data->hdr.offset == BTA_AV_RS_FAIL) {
2263       bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2264       start.chnl = p_scb->chnl;
2265       start.status = BTA_AV_FAIL_ROLE;
2266       start.hndl = p_scb->hndl;
2267       start.initiator = initiator;
2268       (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV*)&start);
2269       return;
2270     }
2271   }
2272 
2273   if (!bta_av_link_role_ok(p_scb, A2DP_SET_ONE_BIT))
2274     p_scb->q_tag = BTA_AV_Q_TAG_START;
2275   else {
2276     /* The wait flag may be set here while we are already master on the link */
2277     /* this could happen if a role switch complete event occurred during
2278      * reconfig */
2279     /* if we are now master on the link, there is no need to wait for the role
2280      * switch, */
2281     /* complete anymore so we can clear the wait for role switch flag */
2282     p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2283   }
2284 
2285   if (p_scb->wait &
2286       (BTA_AV_WAIT_ROLE_SW_RES_OPEN | BTA_AV_WAIT_ROLE_SW_RES_START)) {
2287     p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
2288     p_scb->q_tag = BTA_AV_Q_TAG_START;
2289   }
2290 
2291   if (p_scb->wait) {
2292     APPL_TRACE_ERROR("%s: wait:x%x q_tag:%d- not started", __func__,
2293                      p_scb->wait, p_scb->q_tag);
2294     /* Clear first bit of p_scb->wait and not to return from this point else
2295      * HAL layer gets blocked. And if there is delay in Get Capability response
2296      * as
2297      * first bit of p_scb->wait is cleared hence it ensures bt_av_start_ok is
2298      * not called
2299      * again from bta_av_save_caps.
2300     */
2301     p_scb->wait &= ~BTA_AV_WAIT_ACP_CAPS_ON;
2302   }
2303 
2304   /* tell role manager to check M/S role */
2305   bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2306 
2307   bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2308 
2309   if (p_scb->media_type == AVDT_MEDIA_TYPE_AUDIO) {
2310     /* in normal logic, conns should be bta_av_cb.audio_count - 1,
2311      * However, bta_av_stream_chg is not called to increase
2312      * bta_av_cb.audio_count yet.
2313      * If the code were to be re-arranged for some reasons, this number may need
2314      * to be changed
2315      */
2316     p_scb->co_started = bta_av_cb.audio_open_cnt;
2317     flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
2318   } else {
2319     flush_to = p_bta_av_cfg->video_flush_to;
2320   }
2321   L2CA_SetFlushTimeout(p_scb->peer_addr, flush_to);
2322 
2323   /* clear the congestion flag */
2324   p_scb->cong = false;
2325 
2326   if (new_role & BTA_AV_ROLE_START_INT) {
2327     new_role &= ~BTA_AV_ROLE_START_INT;
2328   } else if ((new_role & BTA_AV_ROLE_AD_ACP) &&
2329              (new_role & BTA_AV_ROLE_SUSPEND_OPT)) {
2330     suspend = true;
2331   }
2332 
2333   if (!suspend) {
2334     p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
2335     bta_av_stream_chg(p_scb, true);
2336   }
2337 
2338   {
2339     /* If sink starts stream, disable sniff mode here */
2340     if (!initiator) {
2341       /* If souce is the master role, disable role switch during streaming.
2342       * Otherwise allow role switch, if source is slave.
2343       * Because it would not hurt source, if the peer device wants source to be
2344       * master */
2345       if ((BTM_GetRole(p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
2346           (cur_role == BTM_ROLE_MASTER)) {
2347         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2348       }
2349 
2350       bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2351     }
2352 
2353     p_scb->role = new_role;
2354     p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2355     p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2356 
2357     p_scb->no_rtp_hdr = false;
2358     p_scb->p_cos->start(p_scb->hndl, p_scb->cfg.codec_info, &p_scb->no_rtp_hdr);
2359     p_scb->co_started = true;
2360 
2361     APPL_TRACE_DEBUG("%s: suspending: %d, role:x%x, init %d", __func__, suspend,
2362                      p_scb->role, initiator);
2363 
2364     start.suspending = suspend;
2365     start.initiator = initiator;
2366     start.chnl = p_scb->chnl;
2367     start.status = BTA_AV_SUCCESS;
2368     start.hndl = p_scb->hndl;
2369     (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV*)&start);
2370 
2371     if (suspend) {
2372       tBTA_AV_API_STOP stop;
2373       p_scb->role |= BTA_AV_ROLE_SUSPEND;
2374       p_scb->cong = true; /* do not allow the media data to go through */
2375       /* do not duplicate the media packets to this channel */
2376       p_scb->p_cos->stop(p_scb->hndl);
2377       p_scb->co_started = false;
2378       stop.flush = false;
2379       stop.suspend = true;
2380       stop.reconfig_stop = false;
2381       bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA*)&stop);
2382     }
2383   }
2384 }
2385 
2386 /*******************************************************************************
2387  *
2388  * Function         bta_av_start_failed
2389  *
2390  * Description      Stream start failed.
2391  *
2392  * Returns          void
2393  *
2394  ******************************************************************************/
bta_av_start_failed(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2395 void bta_av_start_failed(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2396   if (p_scb->started == false && p_scb->co_started == false) {
2397     bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2398     notify_start_failed(p_scb);
2399   }
2400 
2401   bta_sys_set_policy(BTA_ID_AV,
2402                      (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_MASTER_SLAVE_SWITCH),
2403                      p_scb->peer_addr);
2404   p_scb->sco_suspend = false;
2405 }
2406 
2407 /*******************************************************************************
2408  *
2409  * Function         bta_av_str_closed
2410  *
2411  * Description      Stream closed.
2412  *
2413  * Returns          void
2414  *
2415  ******************************************************************************/
bta_av_str_closed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2416 void bta_av_str_closed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2417   tBTA_AV data;
2418   tBTA_AV_EVT event;
2419   uint8_t policy = HCI_ENABLE_SNIFF_MODE;
2420 
2421   if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 ||
2422       bta_av_cb.audio_open_cnt == 1)
2423     policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2424   bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2425   if (bta_av_cb.audio_open_cnt <= 1) {
2426     /* last connection - restore the allow switch flag */
2427     L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
2428   }
2429 
2430   if (p_scb->open_status) {
2431     /* must be failure when opening the stream */
2432     bdcpy(data.open.bd_addr, p_scb->peer_addr);
2433     data.open.status = p_scb->open_status;
2434     data.open.chnl = p_scb->chnl;
2435     data.open.hndl = p_scb->hndl;
2436 
2437     if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC)
2438       data.open.sep = AVDT_TSEP_SNK;
2439     else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)
2440       data.open.sep = AVDT_TSEP_SRC;
2441 
2442     event = BTA_AV_OPEN_EVT;
2443     p_scb->open_status = BTA_AV_SUCCESS;
2444 
2445     bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2446     bta_av_cleanup(p_scb, p_data);
2447     (*bta_av_cb.p_cback)(event, &data);
2448   } else {
2449     /* do stop if we were started */
2450     if (p_scb->co_started) {
2451       bta_av_str_stopped(p_scb, NULL);
2452     }
2453 
2454     {
2455       p_scb->p_cos->close(p_scb->hndl);
2456       data.close.chnl = p_scb->chnl;
2457       data.close.hndl = p_scb->hndl;
2458       event = BTA_AV_CLOSE_EVT;
2459 
2460       bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2461       bta_av_cleanup(p_scb, p_data);
2462       (*bta_av_cb.p_cback)(event, &data);
2463     }
2464   }
2465 }
2466 
2467 /*******************************************************************************
2468  *
2469  * Function         bta_av_clr_cong
2470  *
2471  * Description      Clear stream congestion flag.
2472  *
2473  * Returns          void
2474  *
2475  ******************************************************************************/
bta_av_clr_cong(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2476 void bta_av_clr_cong(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2477   if (p_scb->co_started) p_scb->cong = false;
2478 }
2479 
2480 /*******************************************************************************
2481  *
2482  * Function         bta_av_suspend_cfm
2483  *
2484  * Description      process the suspend response
2485  *
2486  * Returns          void
2487  *
2488  ******************************************************************************/
bta_av_suspend_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2489 void bta_av_suspend_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2490   tBTA_AV_SUSPEND suspend_rsp;
2491   uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2492   uint8_t policy = HCI_ENABLE_SNIFF_MODE;
2493 
2494   APPL_TRACE_DEBUG("%s: audio_open_cnt = %d, err_code = %d", __func__,
2495                    bta_av_cb.audio_open_cnt, err_code);
2496 
2497   if (p_scb->started == false) {
2498     /* handle the condition where there is a collision of SUSPEND req from
2499     *either side
2500     ** Second SUSPEND req could be rejected. Do not treat this as a failure
2501     */
2502     APPL_TRACE_WARNING("%s: already suspended, ignore, err_code %d", __func__,
2503                        err_code);
2504     return;
2505   }
2506 
2507   suspend_rsp.status = BTA_AV_SUCCESS;
2508   if (err_code && (err_code != AVDT_ERR_BAD_STATE)) {
2509     /* Disable suspend feature only with explicit rejection(not with timeout) */
2510     if (err_code != AVDT_ERR_TIMEOUT) {
2511       p_scb->suspend_sup = false;
2512     }
2513     suspend_rsp.status = BTA_AV_FAIL;
2514 
2515     APPL_TRACE_ERROR("%s: suspend failed, closing connection", __func__);
2516 
2517     /* SUSPEND failed. Close connection. */
2518     bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2519   } else {
2520     /* only set started to false when suspend is successful */
2521     p_scb->started = false;
2522   }
2523 
2524   if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
2525     p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
2526     p_scb->cong = false;
2527   }
2528 
2529   bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2530   if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 ||
2531       bta_av_cb.audio_open_cnt == 1)
2532     policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2533   bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2534 
2535   /* in case that we received suspend_ind, we may need to call co_stop here */
2536   if (p_scb->co_started) {
2537     /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
2538     vendor_get_interface()->send_command(
2539         (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP, (void*)&p_scb->l2c_cid);
2540     if (p_scb->offload_start_pending) {
2541       tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
2542       (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV*)&status);
2543     }
2544     p_scb->offload_start_pending = false;
2545     */
2546 
2547     bta_av_stream_chg(p_scb, false);
2548 
2549     {
2550       p_scb->co_started = false;
2551       p_scb->p_cos->stop(p_scb->hndl);
2552     }
2553     L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
2554   }
2555 
2556   {
2557     suspend_rsp.chnl = p_scb->chnl;
2558     suspend_rsp.hndl = p_scb->hndl;
2559     suspend_rsp.initiator = p_data->str_msg.initiator;
2560     (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV*)&suspend_rsp);
2561   }
2562 }
2563 
2564 /*******************************************************************************
2565  *
2566  * Function         bta_av_rcfg_str_ok
2567  *
2568  * Description      report reconfigure successful
2569  *
2570  * Returns          void
2571  *
2572  ******************************************************************************/
bta_av_rcfg_str_ok(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2573 void bta_av_rcfg_str_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2574   tBTA_AV_RECONFIG evt;
2575 
2576   p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
2577   APPL_TRACE_DEBUG("%s: l2c_cid: %d", __func__, p_scb->l2c_cid);
2578 
2579   if (p_data != NULL) {
2580     // p_data could be NULL if the reconfig was triggered by the local device
2581     p_scb->stream_mtu =
2582         p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
2583     uint16_t mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
2584     APPL_TRACE_DEBUG("%s: l2c_cid: 0x%x stream_mtu: %d mtu: %d", __func__,
2585                      p_scb->l2c_cid, p_scb->stream_mtu, mtu);
2586     if (mtu == 0 || mtu > p_scb->stream_mtu) mtu = p_scb->stream_mtu;
2587     p_scb->p_cos->update_mtu(p_scb->hndl, mtu);
2588   }
2589 
2590   /* rc listen */
2591   bta_av_st_rc_timer(p_scb, NULL);
2592   osi_free_and_reset((void**)&p_scb->p_cap);
2593 
2594   /* No need to keep the role bits once reconfig is done. */
2595   p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2596   p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2597   p_scb->role &= ~BTA_AV_ROLE_START_INT;
2598 
2599   {
2600     /* reconfigure success  */
2601     evt.status = BTA_AV_SUCCESS;
2602     evt.chnl = p_scb->chnl;
2603     evt.hndl = p_scb->hndl;
2604     (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV*)&evt);
2605   }
2606 }
2607 
2608 /*******************************************************************************
2609  *
2610  * Function         bta_av_rcfg_failed
2611  *
2612  * Description      process reconfigure failed
2613  *
2614  * Returns          void
2615  *
2616  ******************************************************************************/
bta_av_rcfg_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2617 void bta_av_rcfg_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2618   tBTA_AV_RECONFIG evt;
2619 
2620   APPL_TRACE_DEBUG("%s: num_recfg: %d, conn_lcb:0x%x", __func__,
2621                    p_scb->num_recfg, bta_av_cb.conn_lcb);
2622   if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2623     bta_av_cco_close(p_scb, p_data);
2624     /* report failure */
2625     evt.status = BTA_AV_FAIL_STREAM;
2626     evt.chnl = p_scb->chnl;
2627     evt.hndl = p_scb->hndl;
2628     (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV*)&evt);
2629     /* go to closing state */
2630     bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2631   } else {
2632     /* open failed. try again */
2633     p_scb->num_recfg++;
2634     if (bta_av_cb.conn_lcb) {
2635       AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
2636     } else {
2637       bta_av_connect_req(p_scb, NULL);
2638     }
2639   }
2640 }
2641 
2642 /*******************************************************************************
2643  *
2644  * Function         bta_av_rcfg_connect
2645  *
2646  * Description      stream closed. reconnect the stream
2647  *
2648  * Returns          void
2649  *
2650  ******************************************************************************/
bta_av_rcfg_connect(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2651 void bta_av_rcfg_connect(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2652   p_scb->cong = false;
2653   p_scb->num_recfg++;
2654   APPL_TRACE_DEBUG("%s: num_recfg: %d", __func__, p_scb->num_recfg);
2655   if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2656     /* let bta_av_rcfg_failed report fail */
2657     bta_av_rcfg_failed(p_scb, NULL);
2658   } else
2659     AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask,
2660                     bta_av_dt_cback[p_scb->hdi]);
2661 }
2662 
2663 /*******************************************************************************
2664  *
2665  * Function         bta_av_rcfg_discntd
2666  *
2667  * Description      AVDT disconnected. reconnect the stream
2668  *
2669  * Returns          void
2670  *
2671  ******************************************************************************/
bta_av_rcfg_discntd(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2672 void bta_av_rcfg_discntd(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2673   tBTA_AV_RECONFIG evt;
2674 
2675   APPL_TRACE_DEBUG("%s: num_recfg: %d", __func__, p_scb->num_recfg);
2676   p_scb->num_recfg++;
2677   if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2678     /* report failure */
2679     evt.status = BTA_AV_FAIL_STREAM;
2680     evt.chnl = p_scb->chnl;
2681     evt.hndl = p_scb->hndl;
2682     (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV*)&evt);
2683     /* report close event & go to init state */
2684     bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2685   } else
2686     AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask,
2687                     bta_av_dt_cback[p_scb->hdi]);
2688 }
2689 
2690 /*******************************************************************************
2691  *
2692  * Function         bta_av_suspend_cont
2693  *
2694  * Description      received the suspend response.
2695  *                  continue to reconfigure the stream
2696  *
2697  * Returns          void
2698  *
2699  ******************************************************************************/
bta_av_suspend_cont(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2700 void bta_av_suspend_cont(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2701   uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2702   tBTA_AV_RECONFIG evt;
2703 
2704   p_scb->started = false;
2705   p_scb->cong = false;
2706   if (err_code) {
2707     if (AVDT_ERR_CONNECT == err_code) {
2708       /* report failure */
2709       evt.status = BTA_AV_FAIL;
2710       (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV*)&evt);
2711       bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2712     } else {
2713       APPL_TRACE_ERROR("%s: suspend rejected, try close", __func__);
2714       /* Disable suspend feature only with explicit rejection(not with timeout)
2715        */
2716       if (err_code != AVDT_ERR_TIMEOUT) {
2717         p_scb->suspend_sup = false;
2718       }
2719       /* drop the buffers queued in L2CAP */
2720       L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2721 
2722       AVDT_CloseReq(p_scb->avdt_handle);
2723     }
2724   } else {
2725     APPL_TRACE_DEBUG("%s: calling AVDT_ReconfigReq", __func__);
2726     /* reconfig the stream */
2727 
2728     AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
2729     p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
2730   }
2731 }
2732 
2733 /*******************************************************************************
2734  *
2735  * Function         bta_av_rcfg_cfm
2736  *
2737  * Description      if reconfigure is successful, report the event
2738  *                  otherwise, close the stream.
2739  *
2740  * Returns          void
2741  *
2742  ******************************************************************************/
bta_av_rcfg_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2743 void bta_av_rcfg_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2744   uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2745 
2746   APPL_TRACE_DEBUG("%s: err_code = %d", __func__, err_code);
2747 
2748   // Disable AVDTP RECONFIGURE for blacklisted devices
2749   bool disable_avdtp_reconfigure = false;
2750   {
2751     char remote_name[BTM_MAX_REM_BD_NAME_LEN] = "";
2752     bt_bdaddr_t bd_addr;
2753     for (int i = 0; i < 6; i++) bd_addr.address[i] = p_scb->peer_addr[i];
2754     if (btif_storage_get_stored_remote_name(bd_addr, remote_name)) {
2755       if (interop_match_name(INTEROP_DISABLE_AVDTP_RECONFIGURE, remote_name) ||
2756           interop_match_addr(INTEROP_DISABLE_AVDTP_RECONFIGURE,
2757                              (const bt_bdaddr_t*)&p_scb->peer_addr)) {
2758         APPL_TRACE_DEBUG(
2759             "%s: disable AVDTP RECONFIGURE: interop matched "
2760             "name %s address %02x:%02x:%02x:%02x:%02x:%02x",
2761             __func__, remote_name, p_scb->peer_addr[0], p_scb->peer_addr[1],
2762             p_scb->peer_addr[2], p_scb->peer_addr[3], p_scb->peer_addr[4],
2763             p_scb->peer_addr[5]);
2764         disable_avdtp_reconfigure = true;
2765       }
2766     }
2767   }
2768 
2769   if ((err_code != 0) || disable_avdtp_reconfigure) {
2770     APPL_TRACE_ERROR("%s: reconfig rejected, try close", __func__);
2771     /* Disable reconfiguration feature only with explicit rejection(not with
2772      * timeout) */
2773     if ((err_code != AVDT_ERR_TIMEOUT) || disable_avdtp_reconfigure) {
2774       p_scb->recfg_sup = false;
2775     }
2776     /* started flag is false when reconfigure command is sent */
2777     /* drop the buffers queued in L2CAP */
2778     L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2779     AVDT_CloseReq(p_scb->avdt_handle);
2780   } else {
2781     /* update the codec info after rcfg cfm */
2782     APPL_TRACE_DEBUG(
2783         "%s: updating from codec %s to codec %s", __func__,
2784         A2DP_CodecName(p_scb->cfg.codec_info),
2785         A2DP_CodecName(p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info));
2786     memcpy(p_scb->cfg.codec_info,
2787            p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info, AVDT_CODEC_SIZE);
2788     /* take the SSM back to OPEN state */
2789     bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
2790   }
2791 }
2792 
2793 /*******************************************************************************
2794  *
2795  * Function         bta_av_rcfg_open
2796  *
2797  * Description      AVDT is connected. open the stream with the new
2798  *                  configuration
2799  *
2800  * Returns          void
2801  *
2802  ******************************************************************************/
bta_av_rcfg_open(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2803 void bta_av_rcfg_open(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2804   APPL_TRACE_DEBUG("%s: num_disc_snks = %d", __func__, p_scb->num_disc_snks);
2805 
2806   if (p_scb->num_disc_snks == 0) {
2807     /* Need to update call-out module so that it will be ready for discover */
2808     p_scb->p_cos->stop(p_scb->hndl);
2809 
2810     /* send avdtp discover request */
2811     AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS,
2812                      bta_av_dt_cback[p_scb->hdi]);
2813   } else {
2814     memcpy(p_scb->cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
2815     /* we may choose to use a different SEP at reconfig.
2816      * adjust the sep_idx now */
2817     bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
2818 
2819     /* open the stream with the new config */
2820     p_scb->sep_info_idx = p_scb->rcfg_idx;
2821     AVDT_OpenReq(p_scb->avdt_handle, p_scb->peer_addr,
2822                  p_scb->sep_info[p_scb->sep_info_idx].seid, p_scb->p_cap);
2823   }
2824 }
2825 
2826 /*******************************************************************************
2827  *
2828  * Function         bta_av_security_rej
2829  *
2830  * Description      Send an AVDTP security reject.
2831  *
2832  * Returns          void
2833  *
2834  ******************************************************************************/
bta_av_security_rej(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2835 void bta_av_security_rej(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2836   AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
2837                    NULL, 0);
2838 }
2839 
2840 /*******************************************************************************
2841  *
2842  * Function         bta_av_chk_2nd_start
2843  *
2844  * Description      check if this is 2nd stream and if it needs to be started.
2845  *                  This function needs to be kept very similar to
2846  *                  bta_av_chk_start
2847  *
2848  * Returns          void
2849  *
2850  ******************************************************************************/
bta_av_chk_2nd_start(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2851 void bta_av_chk_2nd_start(tBTA_AV_SCB* p_scb,
2852                           UNUSED_ATTR tBTA_AV_DATA* p_data) {
2853   tBTA_AV_SCB* p_scbi;
2854   int i;
2855   bool new_started = false;
2856 
2857   if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2)) {
2858     /* more than one audio channel is connected */
2859     if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT)) {
2860       /* this channel does not need to be reconfigured.
2861        * if there is other channel streaming, start the stream now */
2862       for (i = 0; i < BTA_AV_NUM_STRS; i++) {
2863         p_scbi = bta_av_cb.p_scb[i];
2864         if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
2865           if (!new_started) {
2866             /* start the new stream */
2867             new_started = true;
2868             bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
2869           }
2870           /* may need to update the flush timeout of this already started stream
2871            */
2872           if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
2873             p_scbi->co_started = bta_av_cb.audio_open_cnt;
2874             L2CA_SetFlushTimeout(
2875                 p_scbi->peer_addr,
2876                 p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1]);
2877           }
2878         }
2879       }
2880     }
2881   }
2882 }
2883 
2884 /*******************************************************************************
2885  *
2886  * Function         bta_av_open_rc
2887  *
2888  * Description      Send a message to main SM to open RC channel.
2889  *
2890  * Returns          void
2891  *
2892  ******************************************************************************/
bta_av_open_rc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2893 void bta_av_open_rc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2894   tBTA_AV_START start;
2895 
2896   APPL_TRACE_DEBUG("%s: use_rc: %d, wait: x%x role:x%x", __func__,
2897                    p_scb->use_rc, p_scb->wait, p_scb->role);
2898   if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) &&
2899       (p_scb->q_tag == BTA_AV_Q_TAG_START)) {
2900     /* waiting for role switch for some reason & the timer expires */
2901     if (!bta_av_link_role_ok(p_scb, A2DP_SET_ONE_BIT)) {
2902       APPL_TRACE_ERROR(
2903           "%s: failed to start streaming for role management reasons!!",
2904           __func__);
2905       alarm_cancel(p_scb->avrc_ct_timer);
2906       start.chnl = p_scb->chnl;
2907       start.status = BTA_AV_FAIL_ROLE;
2908       start.initiator = true;
2909       start.hndl = p_scb->hndl;
2910       p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2911       bta_av_cb.rs_idx = 0;
2912       (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV*)&start);
2913     } else {
2914       /* role switch is done. continue to start streaming */
2915       bta_av_cb.rs_idx = 0;
2916       p_data->hdr.offset = BTA_AV_RS_OK;
2917       bta_av_start_ok(p_scb, p_data);
2918     }
2919     return;
2920   }
2921 
2922   if (p_scb->use_rc == true || (p_scb->role & BTA_AV_ROLE_AD_ACP)) {
2923     if (bta_av_cb.disc) {
2924       /* AVRC discover db is in use */
2925       if (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) {
2926         /* AVRC channel is not connected. delay a little bit */
2927         if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
2928           bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
2929                               BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
2930         } else {
2931           p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
2932         }
2933       }
2934     } else {
2935       /* use main SM for AVRC SDP activities */
2936       bta_av_rc_disc((uint8_t)(p_scb->hdi + 1));
2937     }
2938   } else {
2939     if (BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle) {
2940       /* the open API said that this handle does not want a RC connection.
2941        * disconnect it now */
2942       AVRC_Close(p_scb->rc_handle);
2943     }
2944   }
2945 }
2946 
2947 /*******************************************************************************
2948  *
2949  * Function         bta_av_open_at_inc
2950  *
2951  * Description      This function is called if API open is called by application
2952  *                  while state-machine is at incoming state.
2953  *
2954  * Returns          void
2955  *
2956  ******************************************************************************/
bta_av_open_at_inc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2957 void bta_av_open_at_inc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2958   memcpy(&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
2959 
2960   if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
2961     p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
2962 
2963     /* API open will be handled at timeout if SNK did not start signalling. */
2964     /* API open will be ignored if SNK starts signalling.                   */
2965   } else {
2966     /* SNK did not start signalling, API was called N seconds timeout. */
2967     /* We need to switch to INIT state and start opening connection. */
2968     p_scb->coll_mask = 0;
2969     bta_av_set_scb_sst_init(p_scb);
2970 
2971     tBTA_AV_API_OPEN* p_buf =
2972         (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
2973     memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
2974     p_scb->skip_sdp = true;
2975     bta_sys_sendmsg(p_buf);
2976   }
2977 }
2978 
2979 /*******************************************************************************
2980  *
2981  * Function         bta_av_offload_req
2982  *
2983  * Description      This function is called if application requests offload of
2984  *                  a2dp audio.
2985  *
2986  * Returns          void
2987  *
2988  ******************************************************************************/
bta_av_offload_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2989 void bta_av_offload_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2990   tBTA_AV_STATUS status = BTA_AV_FAIL_RESOURCES;
2991 
2992   APPL_TRACE_DEBUG("%s: stream %s, audio channels open %d", __func__,
2993                    p_scb->started ? "STARTED" : "STOPPED",
2994                    bta_av_cb.audio_open_cnt);
2995 
2996   /* Check if stream has already been started. */
2997   /* Support offload if only one audio source stream is open. */
2998   if (p_scb->started != true) {
2999     status = BTA_AV_FAIL_STREAM;
3000   }
3001 
3002   /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
3003    uint16_t mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
3004    else if (bta_av_cb.audio_open_cnt == 1 &&
3005               p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC &&
3006               p_scb->chnl == BTA_AV_CHNL_AUDIO) {
3007      bt_vendor_op_a2dp_offload_t a2dp_offload_start;
3008 
3009      if (L2CA_GetConnectionConfig(
3010              p_scb->l2c_cid, &a2dp_offload_start.acl_data_size,
3011              &a2dp_offload_start.remote_cid, &a2dp_offload_start.lm_handle)) {
3012        APPL_TRACE_DEBUG("%s: l2cmtu %d lcid 0x%02X rcid 0x%02X lm_handle
3013    0x%02X",
3014                         __func__, a2dp_offload_start.acl_data_size,
3015                         p_scb->l2c_cid, a2dp_offload_start.remote_cid,
3016                         a2dp_offload_start.lm_handle);
3017 
3018        a2dp_offload_start.bta_av_handle = p_scb->hndl;
3019        a2dp_offload_start.xmit_quota = BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA;
3020        a2dp_offload_start.stream_mtu =
3021            (mtu < p_scb->stream_mtu) ? mtu : p_scb->stream_mtu;
3022        a2dp_offload_start.local_cid = p_scb->l2c_cid;
3023        a2dp_offload_start.is_flushable = true;
3024        a2dp_offload_start.stream_source =
3025            ((uint32_t)(p_scb->cfg.codec_info[1] | p_scb->cfg.codec_info[2]));
3026 
3027        memcpy(a2dp_offload_start.codec_info, p_scb->cfg.codec_info,
3028               sizeof(a2dp_offload_start.codec_info));
3029 
3030        if (!vendor_get_interface()->send_command(
3031                (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_START,
3032                &a2dp_offload_start)) {
3033          status = BTA_AV_SUCCESS;
3034          p_scb->offload_start_pending = true;
3035        }
3036      }
3037    }
3038    */
3039   if (status != BTA_AV_SUCCESS)
3040     (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV*)&status);
3041 }
3042 
3043 /*******************************************************************************
3044  *
3045  * Function         bta_av_offload_rsp
3046  *
3047  * Description      This function is called when the vendor lib responds to
3048  *                  BT_VND_OP_A2DP_OFFLOAD_START.
3049  *
3050  * Returns          void
3051  *
3052  ******************************************************************************/
bta_av_offload_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)3053 void bta_av_offload_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
3054   tBTA_AV_STATUS status = p_data->api_status_rsp.status;
3055 
3056   APPL_TRACE_DEBUG("%s: stream %s status %s", __func__,
3057                    p_scb->started ? "STARTED" : "STOPPED",
3058                    status ? "FAIL" : "SUCCESS");
3059 
3060   /* Check if stream has already been started. */
3061   if (status == BTA_AV_SUCCESS && p_scb->started != true) {
3062     status = BTA_AV_FAIL_STREAM;
3063   }
3064 
3065   p_scb->offload_start_pending = false;
3066   (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV*)&status);
3067 }
3068