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