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