bta_av_act.c revision 837acf40a4968449f044cfd3cd15f7f120617eb5
1/*****************************************************************************
2**
3**  Name:           bta_av_act.c
4**
5**  Description:    This file contains action functions for
6**                  advanced audio/video main state machine.
7**
8**  Copyright (c) 2004-2011, Broadcom Corp., All Rights Reserved.
9**  Broadcom Bluetooth Core. Proprietary and confidential.
10**
11*****************************************************************************/
12
13#include "bt_target.h"
14#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
15
16#include <string.h>
17#include "bta_av_api.h"
18#include "bta_av_int.h"
19#include "avdt_api.h"
20#include "bd.h"
21#include "utl.h"
22#include "l2c_api.h"
23#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
24#include "bta_ar_api.h"
25#endif
26
27/*****************************************************************************
28**  Constants
29*****************************************************************************/
30/* the timer in milliseconds to wait for open req after setconfig for incoming connections */
31#ifndef BTA_AV_SIG_TIME_VAL
32#define BTA_AV_SIG_TIME_VAL 8000
33#endif
34
35/* In millisec to wait for signalling from SNK when it is initiated from SNK.   */
36/* If not, we will start signalling from SRC.                                   */
37#ifndef BTA_AV_ACP_SIG_TIME_VAL
38#define BTA_AV_ACP_SIG_TIME_VAL 2000
39#endif
40
41static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle);
42
43/*******************************************************************************
44**
45** Function         bta_av_get_rcb_by_shdl
46**
47** Description      find the RCB associated with the given SCB handle.
48**
49** Returns          tBTA_AV_RCB
50**
51*******************************************************************************/
52tBTA_AV_RCB * bta_av_get_rcb_by_shdl(UINT8 shdl)
53{
54    tBTA_AV_RCB *p_rcb = NULL;
55    int         i;
56
57    for (i=0; i<BTA_AV_NUM_RCB; i++)
58    {
59        if (bta_av_cb.rcb[i].shdl == shdl && bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE)
60        {
61            p_rcb = &bta_av_cb.rcb[i];
62            break;
63        }
64    }
65    return p_rcb;
66}
67#define BTA_AV_STS_NO_RSP       0xFF    /* a number not used by tAVRC_STS */
68
69/*******************************************************************************
70**
71** Function         bta_av_del_rc
72**
73** Description      delete the given AVRC handle.
74**
75** Returns          void
76**
77*******************************************************************************/
78void bta_av_del_rc(tBTA_AV_RCB *p_rcb)
79{
80    tBTA_AV_SCB  *p_scb;
81    UINT8        rc_handle;      /* connected AVRCP handle */
82
83    if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
84    {
85        if(p_rcb->shdl)
86        {
87            p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
88            if(p_scb)
89            {
90                APPL_TRACE_DEBUG3("bta_av_del_rc shdl:%d, srch:%d rc_handle:%d", p_rcb->shdl,
91                                  p_scb->rc_handle, p_rcb->handle);
92                if(p_scb->rc_handle == p_rcb->handle)
93                    p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
94                /* just in case the RC timer is active
95                if(bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl == BTA_AV_CHNL_AUDIO) */
96                    bta_sys_stop_timer(&p_scb->timer);
97            }
98        }
99
100        APPL_TRACE_EVENT4("bta_av_del_rc  handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
101            p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
102        rc_handle = p_rcb->handle;
103        if(!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
104            ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) )
105        {
106            p_rcb->status = 0;
107            p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
108            p_rcb->shdl = 0;
109            p_rcb->lidx = 0;
110        }
111        /* else ACP && connected. do not clear the handle yet */
112        AVRC_Close(rc_handle);
113        if (rc_handle == bta_av_cb.rc_acp_handle)
114            bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
115        APPL_TRACE_EVENT4("end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
116            p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, p_rcb->lidx);
117    }
118}
119
120
121/*******************************************************************************
122**
123** Function         bta_av_close_all_rc
124**
125** Description      close the all AVRC handle.
126**
127** Returns          void
128**
129*******************************************************************************/
130static void bta_av_close_all_rc(tBTA_AV_CB *p_cb)
131{
132    int i;
133
134    for(i=0; i<BTA_AV_NUM_RCB; i++)
135    {
136        if ((p_cb->disabling == TRUE) || (bta_av_cb.rcb[i].shdl != 0))
137            bta_av_del_rc(&bta_av_cb.rcb[i]);
138    }
139}
140
141/*******************************************************************************
142**
143** Function         bta_av_del_sdp_rec
144**
145** Description      delete the given SDP record handle.
146**
147** Returns          void
148**
149*******************************************************************************/
150static void bta_av_del_sdp_rec(UINT32 *p_sdp_handle)
151{
152    if(*p_sdp_handle != 0)
153    {
154        SDP_DeleteRecord(*p_sdp_handle);
155        *p_sdp_handle = 0;
156    }
157}
158
159/*******************************************************************************
160**
161** Function         bta_av_avrc_sdp_cback
162**
163** Description      AVRCP service discovery callback.
164**
165** Returns          void
166**
167*******************************************************************************/
168static void bta_av_avrc_sdp_cback(UINT16 status)
169{
170    BT_HDR *p_msg;
171
172    if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
173    {
174        p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
175        bta_sys_sendmsg(p_msg);
176    }
177}
178
179/*******************************************************************************
180**
181** Function         bta_av_rc_ctrl_cback
182**
183** Description      AVRCP control callback.
184**
185** Returns          void
186**
187*******************************************************************************/
188static void bta_av_rc_ctrl_cback(UINT8 handle, UINT8 event, UINT16 result, BD_ADDR peer_addr)
189{
190    tBTA_AV_RC_CONN_CHG *p_msg;
191    UINT16 msg_event = 0;
192
193#if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
194    APPL_TRACE_EVENT2("rc_ctrl handle: %d event=0x%x", handle, event);
195#else
196    APPL_TRACE_EVENT2("bta_av_rc_ctrl_cback handle: %d event=0x%x", handle, event);
197#endif
198    if (event == AVRC_OPEN_IND_EVT)
199    {
200        /* save handle of opened connection
201        bta_av_cb.rc_handle = handle;*/
202
203        msg_event = BTA_AV_AVRC_OPEN_EVT;
204    }
205    else if (event == AVRC_CLOSE_IND_EVT)
206    {
207        msg_event = BTA_AV_AVRC_CLOSE_EVT;
208    }
209
210    if (msg_event)
211    {
212        if ((p_msg = (tBTA_AV_RC_CONN_CHG *) GKI_getbuf(sizeof(tBTA_AV_RC_CONN_CHG))) != NULL)
213        {
214            p_msg->hdr.event = msg_event;
215            p_msg->handle    = handle;
216            if(peer_addr)
217                bdcpy(p_msg->peer_addr, peer_addr);
218            bta_sys_sendmsg(p_msg);
219        }
220    }
221}
222
223/*******************************************************************************
224**
225** Function         bta_av_rc_msg_cback
226**
227** Description      AVRCP message callback.
228**
229** Returns          void
230**
231*******************************************************************************/
232static void bta_av_rc_msg_cback(UINT8 handle, UINT8 label, UINT8 opcode, tAVRC_MSG *p_msg)
233{
234    tBTA_AV_RC_MSG  *p_buf;
235    UINT8           *p_data = NULL;
236    UINT8           **p_p_data = NULL;
237    UINT16          data_len = 0;
238
239#if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
240    APPL_TRACE_ERROR2("rc_msg handle: %d opcode=0x%x", handle, opcode);
241#else
242    APPL_TRACE_EVENT2("bta_av_rc_msg_cback handle: %d opcode=0x%x", handle, opcode);
243#endif
244    /* determine size of buffer we need */
245    if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL)
246    {
247        p_data = p_msg->vendor.p_vendor_data;
248        p_p_data = &p_msg->vendor.p_vendor_data;
249        data_len = (UINT16) p_msg->vendor.vendor_len;
250    }
251    else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL)
252    {
253        p_data = p_msg->pass.p_pass_data;
254        p_p_data = &p_msg->pass.p_pass_data;
255        data_len = (UINT16) p_msg->pass.pass_len;
256    }
257
258    if ((p_buf = (tBTA_AV_RC_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_RC_MSG) + data_len))) != NULL)
259    {
260        p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
261        p_buf->handle = handle;
262        p_buf->label = label;
263        p_buf->opcode = opcode;
264        memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
265        if (p_data != NULL)
266        {
267            memcpy((UINT8 *)(p_buf + 1), p_data, data_len);
268            *p_p_data = (UINT8 *)(p_buf + 1);
269        }
270#if (AVRC_ADV_CTRL_INCLUDED == TRUE)
271#if (AVCT_BROWSE_INCLUDED == TRUE)
272        if (opcode == AVRC_OP_BROWSE)
273        {
274            /* set p_pkt to NULL, so avrc would not free the GKI buffer */
275            p_msg->browse.p_browse_pkt = NULL;
276        }
277#endif
278#endif
279        bta_sys_sendmsg(p_buf);
280    }
281}
282
283/*******************************************************************************
284**
285** Function         bta_av_rc_create
286**
287** Description      alloc RCB and call AVRC_Open
288**
289** Returns          the created rc handle
290**
291*******************************************************************************/
292UINT8 bta_av_rc_create(tBTA_AV_CB *p_cb, UINT8 role, UINT8 shdl, UINT8 lidx)
293{
294    tAVRC_CONN_CB ccb;
295    BD_ADDR_PTR   bda = (BD_ADDR_PTR)bd_addr_any;
296    UINT8         status = BTA_AV_RC_ROLE_ACP;
297    tBTA_AV_SCB  *p_scb = p_cb->p_scb[shdl - 1];
298    int i;
299    UINT8   rc_handle;
300    tBTA_AV_RCB *p_rcb;
301
302    if(role == AVCT_INT)
303    {
304        bda = p_scb->peer_addr;
305        status = BTA_AV_RC_ROLE_INT;
306    }
307    else
308    {
309        if ((p_rcb = bta_av_get_rcb_by_shdl(shdl)) != NULL )
310        {
311            APPL_TRACE_ERROR1("bta_av_rc_create ACP handle exist for shdl:%d", shdl);
312            return p_rcb->handle;
313        }
314    }
315
316    ccb.p_ctrl_cback = bta_av_rc_ctrl_cback;
317    ccb.p_msg_cback = bta_av_rc_msg_cback;
318    ccb.company_id = p_bta_av_cfg->company_id;
319    ccb.conn = role;
320    /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL */
321    ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT | AVRC_CT_PASSIVE);
322
323
324    if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
325        return BTA_AV_RC_HANDLE_NONE;
326
327    i = rc_handle;
328    p_rcb = &p_cb->rcb[i];
329
330    if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
331    {
332        APPL_TRACE_ERROR1("bta_av_rc_create found duplicated handle:%d", rc_handle);
333    }
334
335    p_rcb->handle = rc_handle;
336    p_rcb->status = status;
337    p_rcb->shdl = shdl;
338    p_rcb->lidx = lidx;
339    p_rcb->peer_features = 0;
340    if(lidx == (BTA_AV_NUM_LINKS + 1))
341    {
342        /* this LIDX is reserved for the AVRCP ACP connection */
343        p_cb->rc_acp_handle = p_rcb->handle;
344        p_cb->rc_acp_idx = (i + 1);
345        APPL_TRACE_DEBUG2("rc_acp_handle:%d idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
346    }
347    APPL_TRACE_DEBUG6("create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
348        i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
349
350    return rc_handle;
351}
352
353/*******************************************************************************
354**
355** Function         bta_av_valid_group_navi_msg
356**
357** Description      Check if it is Group Navigation Msg for Metadata
358**
359** Returns          BTA_AV_RSP_ACCEPT or BTA_AV_RSP_NOT_IMPL.
360**
361*******************************************************************************/
362static tBTA_AV_CODE bta_av_group_navi_supported(UINT8 len, UINT8 *p_data)
363{
364    tBTA_AV_CODE ret=BTA_AV_RSP_NOT_IMPL;
365    UINT8 *p_ptr = p_data;
366    UINT16 u16;
367    UINT32 u32;
368
369    if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN)
370    {
371        BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
372        BE_STREAM_TO_UINT16(u16, p_ptr);
373
374        if (u32 == AVRC_CO_METADATA)
375        {
376            if (u16 <= AVRC_PDU_PREV_GROUP)
377                ret = BTA_AV_RSP_ACCEPT;
378            else
379                ret = BTA_AV_RSP_REJ;
380        }
381    }
382
383    return ret;
384}
385
386/*******************************************************************************
387**
388** Function         bta_av_op_supported
389**
390** Description      Check if remote control operation is supported.
391**
392** Returns          BTA_AV_RSP_ACCEPT of supported, BTA_AV_RSP_NOT_IMPL if not.
393**
394*******************************************************************************/
395static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id)
396{
397    tBTA_AV_CODE ret_code = BTA_AV_RSP_NOT_IMPL;
398
399    if (p_bta_av_rc_id)
400    {
401        if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
402        {
403            ret_code = BTA_AV_RSP_ACCEPT;
404        }
405        else if ((p_bta_av_cfg->rc_pass_rsp == BTA_AV_RSP_INTERIM) && p_bta_av_rc_id_ac)
406        {
407            if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F)))
408            {
409                ret_code = BTA_AV_RSP_INTERIM;
410            }
411        }
412    }
413    return ret_code;
414}
415
416/*******************************************************************************
417**
418** Function         bta_av_find_lcb
419**
420** Description      Given BD_addr, find the associated LCB.
421**
422** Returns          NULL, if not found.
423**
424*******************************************************************************/
425tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op)
426{
427    tBTA_AV_CB   *p_cb = &bta_av_cb;
428    int     xx;
429    UINT8   mask;
430    tBTA_AV_LCB *p_lcb = NULL;
431
432    for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
433    {
434        mask = 1 << xx; /* the used mask for this lcb */
435        if((mask & p_cb->conn_lcb) && 0 ==( bdcmp(p_cb->lcb[xx].addr, addr)))
436        {
437            p_lcb = &p_cb->lcb[xx];
438            if(op == BTA_AV_LCB_FREE)
439            {
440                p_cb->conn_lcb &= ~mask; /* clear the connect mask */
441                APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
442            }
443            break;
444        }
445    }
446    return p_lcb;
447}
448
449/*******************************************************************************
450**
451** Function         bta_av_rc_opened
452**
453** Description      Set AVRCP state to opened.
454**
455** Returns          void
456**
457*******************************************************************************/
458void bta_av_rc_opened(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
459{
460    tBTA_AV_RC_OPEN rc_open;
461    tBTA_AV_SCB     *p_scb;
462    int         i;
463    UINT8       shdl = 0;
464    tBTA_AV_LCB *p_lcb;
465    tBTA_AV_RCB *p_rcb;
466    UINT8       tmp;
467    UINT8       disc = 0;
468
469    /* find the SCB & stop the timer */
470    for(i=0; i<BTA_AV_NUM_STRS; i++)
471    {
472        p_scb = p_cb->p_scb[i];
473        if(p_scb && bdcmp(p_scb->peer_addr, p_data->rc_conn_chg.peer_addr) == 0)
474        {
475            p_scb->rc_handle = p_data->rc_conn_chg.handle;
476            APPL_TRACE_DEBUG2("bta_av_rc_opened shdl:%d, srch %d", i + 1, p_scb->rc_handle);
477            shdl = i+1;
478            APPL_TRACE_ERROR1("use_rc:%d", p_scb->use_rc);
479            bta_sys_stop_timer(&p_scb->timer);
480            disc = p_scb->hndl;
481            break;
482        }
483    }
484
485    i = p_data->rc_conn_chg.handle;
486    if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE)
487    {
488        APPL_TRACE_ERROR1("not a valid handle:%d any more", i);
489        return;
490    }
491
492#if (AVRC_ADV_CTRL_INCLUDED == TRUE && AVCT_BROWSE_INCLUDED == TRUE)
493    /* listen to browsing channel when the connection is open,
494     - if peer initiated AVRCP connection and local device supports browsing channel */
495    if ((p_cb->features & BTA_AV_FEAT_BROWSE) && (p_cb->rcb[i].peer_features == 0))
496        AVRC_OpenBrowse (p_data->rc_conn_chg.handle, AVCT_ACP);
497#endif
498
499    if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0)
500    {
501        /* rc is opened on the RC only ACP channel, but is for a specific
502         * SCB -> need to switch RCBs */
503        p_rcb = bta_av_get_rcb_by_shdl(shdl);
504        if (p_rcb)
505        {
506            p_rcb->shdl = p_cb->rcb[i].shdl;
507            tmp         = p_rcb->lidx;
508            p_rcb->lidx = p_cb->rcb[i].lidx;
509            p_cb->rcb[i].lidx = tmp;
510            p_cb->rc_acp_handle = p_rcb->handle;
511            p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
512            APPL_TRACE_DEBUG2("switching RCB rc_acp_handle:%d idx:%d",
513                               p_cb->rc_acp_handle, p_cb->rc_acp_idx);
514        }
515    }
516
517    p_cb->rcb[i].shdl = shdl;
518    rc_open.rc_handle = i;
519    APPL_TRACE_ERROR4("bta_av_rc_opened rcb[%d] shdl:%d lidx:%d/%d",
520            i, shdl, p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
521    p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
522
523    if(!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx)
524    {
525        /* no associated SCB -> connected to an RC only device
526         * update the index to the extra LCB */
527        p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
528        bdcpy(p_lcb->addr, p_data->rc_conn_chg.peer_addr);
529        APPL_TRACE_DEBUG6("rc_only bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
530                      p_lcb->addr[0], p_lcb->addr[1],
531                      p_lcb->addr[2], p_lcb->addr[3],
532                      p_lcb->addr[4], p_lcb->addr[5]);
533        p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
534            p_cb->rcb[i].lidx = p_lcb->lidx;
535        p_lcb->conn_msk = 1;
536        APPL_TRACE_ERROR3("rcb[%d].lidx=%d, lcb.conn_msk=x%x",
537            i, p_cb->rcb[i].lidx, p_lcb->conn_msk);
538        disc = p_data->rc_conn_chg.handle|BTA_AV_CHNL_MSK;
539    }
540
541    bdcpy(rc_open.peer_addr, p_data->rc_conn_chg.peer_addr);
542    rc_open.peer_features = p_cb->rcb[i].peer_features;
543    rc_open.status = BTA_AV_SUCCESS;
544    APPL_TRACE_DEBUG2("local features:x%x peer_features:x%x", p_cb->features,
545                      rc_open.peer_features);
546    if(rc_open.peer_features == 0)
547    {
548        /* we have not done SDP on peer RC capabilities.
549         * peer must have initiated the RC connection */
550        rc_open.peer_features = BTA_AV_FEAT_RCCT;
551        bta_av_rc_disc(disc);
552    }
553    (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
554
555#if (AVRC_ADV_CTRL_INCLUDED == TRUE && AVCT_BROWSE_INCLUDED == TRUE)
556    /* if local initiated AVRCP connection and both peer and locals device support
557     * browsing channel, open the browsing channel now */
558    if ((p_cb->features & BTA_AV_FEAT_BROWSE) && (rc_open.peer_features & BTA_AV_FEAT_BROWSE) &&
559        ((p_cb->rcb[i].status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT))
560    {
561        AVRC_OpenBrowse (p_data->rc_conn_chg.handle, AVCT_INT);
562    }
563#endif
564}
565
566
567/*******************************************************************************
568**
569** Function         bta_av_rc_remote_cmd
570**
571** Description      Send an AVRCP remote control command.
572**
573** Returns          void
574**
575*******************************************************************************/
576void bta_av_rc_remote_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
577{
578    tBTA_AV_RCB    *p_rcb;
579    if (p_cb->features & BTA_AV_FEAT_RCCT)
580    {
581        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
582        {
583            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
584            if(p_rcb->status & BTA_AV_RC_CONN_MASK)
585            {
586                AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
587                     &p_data->api_remote_cmd.msg);
588            }
589        }
590    }
591}
592
593/*******************************************************************************
594**
595** Function         bta_av_rc_vendor_cmd
596**
597** Description      Send an AVRCP vendor specific command.
598**
599** Returns          void
600**
601*******************************************************************************/
602void bta_av_rc_vendor_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
603{
604    tBTA_AV_RCB    *p_rcb;
605    if ( (p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
606         (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR))
607    {
608        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
609        {
610            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
611            AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
612        }
613    }
614}
615
616/*******************************************************************************
617**
618** Function         bta_av_rc_vendor_rsp
619**
620** Description      Send an AVRCP vendor specific response.
621**
622** Returns          void
623**
624*******************************************************************************/
625void bta_av_rc_vendor_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
626{
627    tBTA_AV_RCB    *p_rcb;
628    if ( (p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
629         (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR))
630    {
631        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
632        {
633            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
634            AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
635        }
636    }
637}
638
639/*******************************************************************************
640**
641** Function         bta_av_rc_meta_rsp
642**
643** Description      Send an AVRCP metadata/advanced control command/response.
644**
645** Returns          void
646**
647*******************************************************************************/
648void bta_av_rc_meta_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
649{
650    tBTA_AV_RCB *p_rcb;
651    BOOLEAN         free = TRUE;
652
653    if ((p_cb->features & BTA_AV_FEAT_METADATA) && (p_data->hdr.layer_specific < BTA_AV_NUM_RCB))
654    {
655        if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
656            (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT)) )
657        {
658            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
659            AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label, p_data->api_meta_rsp.rsp_code,
660                                      p_data->api_meta_rsp.p_pkt);
661            free = FALSE;
662        }
663    }
664
665    if (free)
666        GKI_freebuf (p_data->api_meta_rsp.p_pkt);
667}
668
669/*******************************************************************************
670**
671** Function         bta_av_rc_free_rsp
672**
673** Description      free an AVRCP metadata command buffer.
674**
675** Returns          void
676**
677*******************************************************************************/
678void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
679{
680    GKI_freebuf (p_data->api_meta_rsp.p_pkt);
681}
682
683/*******************************************************************************
684**
685** Function         bta_av_rc_meta_req
686**
687** Description      Send an AVRCP metadata command.
688**
689** Returns          void
690**
691*******************************************************************************/
692void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
693{
694#if (AVRC_ADV_CTRL_INCLUDED == TRUE)
695#if (AVCT_BROWSE_INCLUDED == TRUE)
696    if (p_data->rc_msg.opcode == AVRC_OP_BROWSE)
697    {
698        utl_freebuf ((void **)&p_data->rc_msg.msg.browse.p_browse_pkt);
699    }
700#endif
701#endif
702}
703
704
705
706/*******************************************************************************
707**
708** Function         bta_av_chk_notif_evt_id
709**
710** Description      make sure the requested player id is valid.
711**
712** Returns          BTA_AV_STS_NO_RSP, if no error
713**
714*******************************************************************************/
715static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR *p_vendor)
716{
717    tAVRC_STS   status = BTA_AV_STS_NO_RSP;
718    UINT8       xx;
719    UINT16      u16;
720    UINT8       *p = p_vendor->p_vendor_data + 2;
721
722    BE_STREAM_TO_UINT16 (u16, p);
723    /* double check the fixed length */
724    if ((u16 != 5) || (p_vendor->vendor_len != 9))
725    {
726        status = AVRC_STS_INTERNAL_ERR;
727    }
728    else
729    {
730        /* make sure the player_id is valid */
731        for (xx=0; xx<p_bta_av_cfg->num_evt_ids; xx++)
732        {
733            if (*p == p_bta_av_cfg->p_meta_evt_ids[xx])
734            {
735                break;
736            }
737        }
738        if (xx == p_bta_av_cfg->num_evt_ids)
739        {
740            status = AVRC_STS_BAD_PARAM;
741        }
742    }
743
744    return status;
745}
746
747/*******************************************************************************
748**
749** Function         bta_av_proc_meta_cmd
750**
751** Description      Process an AVRCP metadata command from the peer.
752**
753** Returns          TRUE to respond immediately
754**
755*******************************************************************************/
756tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE  *p_rc_rsp, tBTA_AV_RC_MSG *p_msg, UINT8 *p_ctype)
757{
758    tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
759    UINT8       u8, pdu, *p;
760    UINT16      u16;
761    tAVRC_MSG_VENDOR    *p_vendor = &p_msg->msg.vendor;
762
763    pdu = *(p_vendor->p_vendor_data);
764    p_rc_rsp->pdu = pdu;
765    *p_ctype = AVRC_RSP_REJ;
766    /* Metadata messages only use PANEL sub-unit type */
767    if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL)
768    {
769        APPL_TRACE_DEBUG0("SUBUNIT must be PANEL");
770        /* reject it */
771        evt=0;
772        p_vendor->hdr.ctype = BTA_AV_RSP_NOT_IMPL;
773        AVRC_VendorRsp(p_msg->handle, p_msg->label, &p_msg->msg.vendor);
774    }
775#if (AVRC_METADATA_INCLUDED == TRUE)
776    else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype) )
777    {
778        APPL_TRACE_DEBUG2("Invalid pdu/ctype: 0x%x, %d", pdu, p_vendor->hdr.ctype);
779        /* reject invalid message without reporting to app */
780        evt = 0;
781        p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
782    }
783#endif
784    else
785    {
786        switch (pdu)
787        {
788        case AVRC_PDU_GET_CAPABILITIES:
789            /* process GetCapabilities command without reporting the event to app */
790            evt = 0;
791            u8 = *(p_vendor->p_vendor_data + 4);
792            p = p_vendor->p_vendor_data + 2;
793            p_rc_rsp->get_caps.capability_id = u8;
794            BE_STREAM_TO_UINT16 (u16, p);
795            if ((u16 != 1) || (p_vendor->vendor_len != 5))
796            {
797                p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
798            }
799            else
800            {
801                p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
802                if (u8 == AVRC_CAP_COMPANY_ID)
803                {
804                    *p_ctype = AVRC_RSP_IMPL_STBL;
805                    p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
806                    memcpy(p_rc_rsp->get_caps.param.company_id, p_bta_av_cfg->p_meta_co_ids,
807                           (p_bta_av_cfg->num_co_ids << 2));
808                }
809                else if (u8 == AVRC_CAP_EVENTS_SUPPORTED)
810                {
811                    *p_ctype = AVRC_RSP_IMPL_STBL;
812                    p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
813                    memcpy(p_rc_rsp->get_caps.param.event_id, p_bta_av_cfg->p_meta_evt_ids,
814                           p_bta_av_cfg->num_evt_ids);
815                }
816                else
817                {
818                    APPL_TRACE_DEBUG1("Invalid capability ID: 0x%x", u8);
819                    /* reject - unknown capability ID */
820                    p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
821                }
822            }
823            break;
824
825
826        case AVRC_PDU_REGISTER_NOTIFICATION:
827            /* make sure the event_id is implemented */
828            p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id (p_vendor);
829            if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP)
830                evt = 0;
831            break;
832
833        }
834    }
835
836    return evt;
837}
838
839
840/*******************************************************************************
841**
842** Function         bta_av_rc_msg
843**
844** Description      Process an AVRCP message from the peer.
845**
846** Returns          void
847**
848*******************************************************************************/
849void bta_av_rc_msg(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
850{
851    tBTA_AV_EVT evt = 0;
852    tBTA_AV     av;
853    BT_HDR      *p_pkt = NULL;
854    tAVRC_MSG_VENDOR    *p_vendor = &p_data->rc_msg.msg.vendor;
855#if (AVRC_METADATA_INCLUDED == TRUE)
856    tAVRC_STS   res;
857    UINT8       ctype;
858    tAVRC_RESPONSE  rc_rsp;
859
860    rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
861#endif
862
863    if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU)
864    {
865    /* if this is a pass thru command */
866        if (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL)
867        {
868        /* check if operation is supported */
869            if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR)
870            {
871                p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
872#if (AVRC_METADATA_INCLUDED == TRUE)
873                if (p_cb->features & BTA_AV_FEAT_METADATA)
874                    p_data->rc_msg.msg.hdr.ctype =
875                       bta_av_group_navi_supported(p_data->rc_msg.msg.pass.pass_len,
876                       p_data->rc_msg.msg.pass.p_pass_data);
877#endif
878            }
879            else
880            {
881                p_data->rc_msg.msg.hdr.ctype = bta_av_op_supported(p_data->rc_msg.msg.pass.op_id);
882            }
883
884            /* send response */
885            if (p_data->rc_msg.msg.hdr.ctype != BTA_AV_RSP_INTERIM)
886                AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
887
888            /* set up for callback if supported */
889            if (p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_ACCEPT || p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_INTERIM)
890            {
891                evt = BTA_AV_REMOTE_CMD_EVT;
892                av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
893                av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
894                av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
895                av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
896                memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof (tAVRC_HDR));
897                av.remote_cmd.label = p_data->rc_msg.label;
898            }
899        }
900        /* else if this is a pass thru response */
901        else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
902        {
903            /* set up for callback */
904            evt = BTA_AV_REMOTE_RSP_EVT;
905            av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
906            av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
907            av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
908            av.remote_rsp.label = p_data->rc_msg.label;
909        }
910        /* must be a bad ctype -> reject*/
911        else
912        {
913            p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
914            AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
915        }
916    }
917    /* else if this is a vendor specific command or response */
918    else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR)
919    {
920        /* set up for callback */
921        av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
922        av.vendor_cmd.company_id = p_vendor->company_id;
923        av.vendor_cmd.label = p_data->rc_msg.label;
924        av.vendor_cmd.p_data = p_vendor->p_vendor_data;
925        av.vendor_cmd.len = p_vendor->vendor_len;
926
927        /* if configured to support vendor specific and it's a command */
928        if ((p_cb->features & BTA_AV_FEAT_VENDOR)  &&
929            p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
930        {
931#if (AVRC_METADATA_INCLUDED == TRUE)
932            if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
933               (p_vendor->company_id == AVRC_CO_METADATA))
934            {
935                av.meta_msg.p_msg = &p_data->rc_msg.msg;
936                evt = bta_av_proc_meta_cmd (&rc_rsp, &p_data->rc_msg, &ctype);
937            }
938            else
939#endif
940                evt = BTA_AV_VENDOR_CMD_EVT;
941        }
942        /* else if configured to support vendor specific and it's a response */
943        else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
944                 p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
945        {
946#if (AVRC_METADATA_INCLUDED == TRUE)
947            if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
948               (p_vendor->company_id == AVRC_CO_METADATA))
949            {
950                av.meta_msg.p_msg = &p_data->rc_msg.msg;
951                evt = BTA_AV_META_MSG_EVT;
952            }
953            else
954#endif
955                evt = BTA_AV_VENDOR_RSP_EVT;
956
957        }
958        /* else if not configured to support vendor specific and it's a command */
959        else if (!(p_cb->features & BTA_AV_FEAT_VENDOR)  &&
960            p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
961        {
962            /* reject it */
963            p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
964            AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.vendor);
965        }
966    }
967#if (AVRC_METADATA_INCLUDED == TRUE)
968#if (AVRC_ADV_CTRL_INCLUDED == TRUE)
969#if (AVCT_BROWSE_INCLUDED == TRUE)
970    else if (p_data->rc_msg.opcode == AVRC_OP_BROWSE)
971    {
972        /* set up for callback */
973        av.meta_msg.code = p_data->rc_msg.msg.hdr.ctype;
974        av.meta_msg.company_id = p_vendor->company_id;
975        av.meta_msg.label = p_data->rc_msg.label;
976        av.meta_msg.p_data = p_data->rc_msg.msg.browse.p_browse_data;
977        av.meta_msg.len = p_data->rc_msg.msg.browse.browse_len;
978        av.meta_msg.p_msg = &p_data->rc_msg.msg;
979        evt = BTA_AV_META_MSG_EVT;
980    }
981#endif
982#endif
983
984    if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP)
985    {
986        if (!p_pkt)
987        {
988            rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
989            res = AVRC_BldResponse (0, &rc_rsp, &p_pkt);
990        }
991        if (p_pkt)
992            AVRC_MsgReq (p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
993    }
994#endif
995
996    /* call callback */
997    if (evt != 0)
998    {
999        av.remote_cmd.rc_handle = p_data->rc_msg.handle;
1000        (*p_cb->p_cback)(evt, &av);
1001#if (AVRC_METADATA_INCLUDED == TRUE)
1002#if (AVRC_ADV_CTRL_INCLUDED == TRUE)
1003#if (AVCT_BROWSE_INCLUDED == TRUE)
1004        /* If the application does not free the buffer or claim the ownership, free the buffer now */
1005        if (p_data->rc_msg.opcode == AVRC_OP_BROWSE)
1006            utl_freebuf((void **)&p_data->rc_msg.msg.browse.p_browse_pkt);
1007#endif
1008#endif
1009#endif
1010    }
1011}
1012
1013/*******************************************************************************
1014**
1015** Function         bta_av_rc_close
1016**
1017** Description      close the specified AVRC handle.
1018**
1019** Returns          void
1020**
1021*******************************************************************************/
1022void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
1023{
1024    UINT16 handle = p_data->hdr.layer_specific;
1025    tBTA_AV_SCB  *p_scb;
1026    tBTA_AV_RCB *p_rcb;
1027
1028    if(handle < BTA_AV_NUM_RCB)
1029    {
1030        p_rcb = &p_cb->rcb[handle];
1031
1032        APPL_TRACE_DEBUG2("bta_av_rc_close handle: %d, status=0x%x", p_rcb->handle, p_rcb->status);
1033        if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
1034        {
1035            if(p_rcb->shdl)
1036            {
1037                p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1038                if(p_scb)
1039                {
1040                    /* just in case the RC timer is active
1041                    if(bta_av_cb.features & BTA_AV_FEAT_RCCT &&
1042                       p_scb->chnl == BTA_AV_CHNL_AUDIO) */
1043                        bta_sys_stop_timer(&p_scb->timer);
1044                }
1045            }
1046
1047            AVRC_Close(p_rcb->handle);
1048        }
1049    }
1050}
1051
1052/*******************************************************************************
1053**
1054** Function         bta_av_get_shdl
1055**
1056** Returns          The index to p_scb[]
1057**
1058*******************************************************************************/
1059static UINT8 bta_av_get_shdl(tBTA_AV_SCB *p_scb)
1060{
1061    int     i;
1062    UINT8   shdl = 0;
1063    /* find the SCB & stop the timer */
1064    for(i=0; i<BTA_AV_NUM_STRS; i++)
1065    {
1066        if(p_scb == bta_av_cb.p_scb[i])
1067        {
1068            shdl = i+1;
1069            break;
1070        }
1071    }
1072    return shdl;
1073}
1074
1075/*******************************************************************************
1076**
1077** Function         bta_av_stream_chg
1078**
1079** Description      audio streaming status changed.
1080**
1081** Returns          void
1082**
1083*******************************************************************************/
1084void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started)
1085{
1086    UINT8   started_msk;
1087    int     i;
1088    UINT8   *p_streams;
1089    BOOLEAN no_streams = FALSE;
1090    tBTA_AV_SCB *p_scbi;
1091
1092    started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1093    APPL_TRACE_DEBUG3 ("bta_av_stream_chg started:%d started_msk:x%x chnl:x%x", started,
1094                                                  started_msk, p_scb->chnl);
1095    if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
1096        p_streams = &bta_av_cb.audio_streams;
1097    else
1098        p_streams = &bta_av_cb.video_streams;
1099
1100    if (started)
1101    {
1102        /* Let L2CAP know this channel is processed with high priority */
1103        L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_HIGH);
1104        (*p_streams) |= started_msk;
1105    }
1106    else
1107    {
1108        (*p_streams) &= ~started_msk;
1109    }
1110
1111    if (!started)
1112    {
1113        i=0;
1114        if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
1115        {
1116            if (bta_av_cb.video_streams == 0)
1117                no_streams = TRUE;
1118        }
1119        else
1120        {
1121            no_streams = TRUE;
1122            if ( bta_av_cb.audio_streams )
1123            {
1124                for (; i<BTA_AV_NUM_STRS; i++)
1125                {
1126                    p_scbi = bta_av_cb.p_scb[i];
1127                    /* scb is used and started */
1128                    if ( p_scbi && (bta_av_cb.audio_streams & BTA_AV_HNDL_TO_MSK(i))
1129                        && bdcmp(p_scbi->peer_addr, p_scb->peer_addr) == 0)
1130                    {
1131                        no_streams = FALSE;
1132                        break;
1133                    }
1134                }
1135
1136            }
1137        }
1138
1139        APPL_TRACE_DEBUG4 ("no_streams:%d i:%d, audio_streams:x%x, video_streams:x%x", no_streams, i,
1140                           bta_av_cb.audio_streams, bta_av_cb.video_streams);
1141        if (no_streams)
1142        {
1143            /* Let L2CAP know this channel is processed with low priority */
1144            L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_NORMAL);
1145        }
1146    }
1147}
1148
1149
1150/*******************************************************************************
1151**
1152** Function         bta_av_conn_chg
1153**
1154** Description      connetion status changed.
1155**                  Open an AVRCP acceptor channel, if new conn.
1156**
1157** Returns          void
1158**
1159*******************************************************************************/
1160void bta_av_conn_chg(tBTA_AV_DATA *p_data)
1161{
1162    tBTA_AV_CB   *p_cb = &bta_av_cb;
1163    tBTA_AV_SCB     *p_scb;
1164    tBTA_AV_SCB     *p_scbi;
1165    UINT8   mask;
1166    UINT8   conn_msk;
1167    UINT8   old_msk;
1168    int i;
1169    int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
1170    tBTA_AV_LCB *p_lcb;
1171    tBTA_AV_LCB *p_lcb_rc;
1172    tBTA_AV_RCB *p_rcb, *p_rcb2;
1173    BOOLEAN     chk_restore = FALSE;
1174
1175    p_scb = p_cb->p_scb[index];
1176
1177    mask = BTA_AV_HNDL_TO_MSK(index);
1178    p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
1179    conn_msk = 1 << (index + 1);
1180    if(p_data->conn_chg.is_up)
1181    {
1182        /* set the conned mask for this channel */
1183        if(p_scb)
1184        {
1185            if(p_lcb)
1186            {
1187                p_lcb->conn_msk |= conn_msk;
1188                for (i=0; i<BTA_AV_NUM_RCB; i++)
1189                {
1190                    if (bta_av_cb.rcb[i].lidx == p_lcb->lidx)
1191                    {
1192                        bta_av_cb.rcb[i].shdl = index + 1;
1193                        APPL_TRACE_DEBUG5("conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
1194                                          bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1195                                          bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1196                        break;
1197                    }
1198                }
1199            }
1200            if (p_scb->chnl == BTA_AV_CHNL_AUDIO)
1201            {
1202                old_msk = p_cb->conn_audio;
1203                p_cb->conn_audio |= mask;
1204            }
1205            else
1206            {
1207                old_msk = p_cb->conn_video;
1208                p_cb->conn_video |= mask;
1209            }
1210
1211            if ((old_msk & mask) == 0)
1212            {
1213                /* increase the audio open count, if not set yet */
1214                bta_av_cb.audio_open_cnt++;
1215            }
1216
1217
1218            APPL_TRACE_DEBUG2("rc_acp_handle:%d rc_acp_idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1219            /* check if the AVRCP ACP channel is already connected */
1220            if(p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE && p_cb->rc_acp_idx)
1221            {
1222                p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
1223                APPL_TRACE_DEBUG1("rc_acp is connected && conn_chg on same addr p_lcb_rc->conn_msk:x%x",
1224                                  p_lcb_rc->conn_msk);
1225                /* check if the RC is connected to the scb addr */
1226                APPL_TRACE_DEBUG6 ("p_lcb_rc->addr: %02x:%02x:%02x:%02x:%02x:%02x",
1227                       p_lcb_rc->addr[0], p_lcb_rc->addr[1], p_lcb_rc->addr[2], p_lcb_rc->addr[3],
1228                       p_lcb_rc->addr[4], p_lcb_rc->addr[5]);
1229                APPL_TRACE_DEBUG6 ("conn_chg.peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
1230                       p_data->conn_chg.peer_addr[0], p_data->conn_chg.peer_addr[1],
1231                       p_data->conn_chg.peer_addr[2],
1232                       p_data->conn_chg.peer_addr[3], p_data->conn_chg.peer_addr[4],
1233                       p_data->conn_chg.peer_addr[5]);
1234                if (p_lcb_rc->conn_msk && bdcmp(p_lcb_rc->addr, p_data->conn_chg.peer_addr) == 0)
1235                {
1236                    /* AVRCP is already connected.
1237                     * need to update the association betwen SCB and RCB */
1238                    p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
1239                    p_lcb_rc->lidx = 0;
1240                    p_scb->rc_handle = p_cb->rc_acp_handle;
1241                    p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
1242                    p_rcb->shdl = bta_av_get_shdl(p_scb);
1243                    APPL_TRACE_DEBUG3("update rc_acp shdl:%d/%d srch:%d", index + 1, p_rcb->shdl,
1244                                      p_scb->rc_handle );
1245
1246                    p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
1247                    if (p_rcb2)
1248                    {
1249                        /* found the RCB that was created to associated with this SCB */
1250                        p_cb->rc_acp_handle = p_rcb2->handle;
1251                        p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
1252                        APPL_TRACE_DEBUG2("new rc_acp_handle:%d, idx:%d", p_cb->rc_acp_handle,
1253                                           p_cb->rc_acp_idx);
1254                        p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
1255                        APPL_TRACE_DEBUG3("rc2 handle:%d lidx:%d/%d",p_rcb2->handle, p_rcb2->lidx,
1256                                          p_cb->lcb[p_rcb2->lidx-1].lidx);
1257                    }
1258                    p_rcb->lidx = p_lcb->lidx;
1259                    APPL_TRACE_DEBUG3("rc handle:%d lidx:%d/%d",p_rcb->handle, p_rcb->lidx,
1260                                      p_cb->lcb[p_rcb->lidx-1].lidx);
1261                }
1262            }
1263        }
1264    }
1265    else
1266    {
1267        if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
1268        {
1269            /* this channel is still marked as open. decrease the count */
1270            bta_av_cb.audio_open_cnt--;
1271        }
1272
1273        /* clear the conned mask for this channel */
1274        p_cb->conn_audio &= ~mask;
1275        p_cb->conn_video &= ~mask;
1276        if(p_scb)
1277        {
1278            /* the stream is closed.
1279             * clear the peer address, so it would not mess up the AVRCP for the next round of operation */
1280            bdcpy(p_scb->peer_addr, bd_addr_null);
1281            if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
1282            {
1283                if(p_lcb)
1284                {
1285                    p_lcb->conn_msk &= ~conn_msk;
1286                }
1287                /* audio channel is down. make sure the INT channel is down */
1288                /* just in case the RC timer is active
1289                if(p_cb->features & BTA_AV_FEAT_RCCT) */
1290                {
1291                    bta_sys_stop_timer(&p_scb->timer);
1292                }
1293                /* one audio channel goes down. check if we need to restore high priority */
1294                chk_restore = TRUE;
1295            }
1296        }
1297
1298        APPL_TRACE_DEBUG1("bta_av_conn_chg shdl:%d", index + 1);
1299        for (i=0; i<BTA_AV_NUM_RCB; i++)
1300        {
1301            APPL_TRACE_DEBUG5("conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
1302                              bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1303                              bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1304            if(bta_av_cb.rcb[i].shdl == index + 1)
1305            {
1306                bta_av_del_rc(&bta_av_cb.rcb[i]);
1307                break;
1308            }
1309        }
1310
1311        if(p_cb->conn_audio == 0 && p_cb->conn_video == 0)
1312        {
1313            /* if both channels are not connected,
1314             * close all RC channels */
1315            bta_av_close_all_rc(p_cb);
1316        }
1317
1318        /* if the AVRCP is no longer listening, create the listening channel */
1319        if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
1320            bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1321    }
1322
1323    APPL_TRACE_DEBUG6("bta_av_conn_chg audio:%x video:%x up:%d conn_msk:0x%x chk_restore:%d audio_open_cnt:%d",
1324        p_cb->conn_audio, p_cb->conn_video, p_data->conn_chg.is_up, conn_msk, chk_restore, p_cb->audio_open_cnt);
1325
1326    if (chk_restore)
1327    {
1328        if (p_cb->audio_open_cnt == 1)
1329        {
1330            /* one audio channel goes down and there's one audio channel remains open.
1331             * restore the switch role in default link policy */
1332            bta_sys_set_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
1333            /* allow role switch, if this is the last connection */
1334            bta_av_restore_switch();
1335        }
1336        if (p_cb->audio_open_cnt)
1337        {
1338            /* adjust flush timeout settings to longer period */
1339            for (i=0; i<BTA_AV_NUM_STRS; i++)
1340            {
1341                p_scbi = bta_av_cb.p_scb[i];
1342                if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
1343                {
1344                    /* may need to update the flush timeout of this already started stream */
1345                    if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
1346                    {
1347                        p_scbi->co_started = bta_av_cb.audio_open_cnt;
1348                        L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
1349                    }
1350                }
1351            }
1352        }
1353    }
1354}
1355
1356/*******************************************************************************
1357**
1358** Function         bta_av_disable
1359**
1360** Description      disable AV.
1361**
1362** Returns          void
1363**
1364*******************************************************************************/
1365void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
1366{
1367    BT_HDR  hdr;
1368    UINT16  xx;
1369
1370    p_cb->disabling = TRUE;
1371
1372    bta_av_close_all_rc(p_cb);
1373
1374    utl_freebuf((void **) &p_cb->p_disc_db);
1375
1376    /* disable audio/video - de-register all channels,
1377     * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
1378    for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
1379    {
1380        hdr.layer_specific = xx + 1;
1381        bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
1382    }
1383}
1384
1385/*******************************************************************************
1386**
1387** Function         bta_av_api_disconnect
1388**
1389** Description      .
1390**
1391** Returns          void
1392**
1393*******************************************************************************/
1394void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
1395{
1396    AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
1397    bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1398}
1399
1400/*******************************************************************************
1401**
1402** Function         bta_av_sig_chg
1403**
1404** Description      process AVDT signal channel up/down.
1405**
1406** Returns          void
1407**
1408*******************************************************************************/
1409void bta_av_sig_chg(tBTA_AV_DATA *p_data)
1410{
1411    UINT16 event = p_data->str_msg.hdr.layer_specific;
1412    tBTA_AV_CB   *p_cb = &bta_av_cb;
1413    int     xx;
1414    UINT8   mask;
1415    tBTA_AV_LCB *p_lcb = NULL;
1416
1417    APPL_TRACE_DEBUG1("bta_av_sig_chg event: %d", event);
1418    if(event == AVDT_CONNECT_IND_EVT)
1419    {
1420        p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1421        if(!p_lcb)
1422        {
1423            /* if the address does not have an LCB yet, alloc one */
1424            for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
1425            {
1426                mask = 1 << xx;
1427                APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
1428                if(!(mask & p_cb->conn_lcb))
1429                {
1430                    if (!p_cb->p_scb[xx])
1431                    {
1432                        /* We do not have scb for this avdt connection.     */
1433                        /* Silently close the connection.                   */
1434                        APPL_TRACE_ERROR0("av scb not available for avdt connection");
1435                        AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
1436                        return;
1437                    }
1438
1439                    p_lcb = &p_cb->lcb[xx];
1440                    p_lcb->lidx = xx + 1;
1441                    bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
1442                    p_lcb->conn_msk = 0; /* clear the connect mask */
1443                    /* start listening when the signal channel is open */
1444                    if (p_cb->features & BTA_AV_FEAT_RCTG)
1445                    {
1446                        bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1447                    }
1448                    /* this entry is not used yet. */
1449                    p_cb->conn_lcb |= mask;     /* mark it as used */
1450                    APPL_TRACE_DEBUG1("start sig timer %d", p_data->hdr.offset);
1451                    if (p_data->hdr.offset == AVDT_ACP)
1452                    {
1453                        APPL_TRACE_DEBUG1("Incoming L2CAP acquired, set state as incoming", NULL);
1454                        bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
1455                        p_cb->p_scb[xx]->use_rc = TRUE;     /* allowing RC for incoming connection */
1456                        bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
1457
1458                        /* The Pending Event should be sent as soon as the L2CAP signalling channel
1459                         * is set up, which is NOW. Earlier this was done only after
1460                         * BTA_AV_SIG_TIME_VAL milliseconds.
1461                         * The following function shall send the event and start the recurring timer
1462                         */
1463                        bta_av_sig_timer(NULL);
1464
1465                        /* Possible collision : need to avoid outgoing processing while the timer is running */
1466                        p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
1467
1468                        p_cb->acp_sig_tmr.param = (UINT32)xx;
1469                        p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK*)&bta_av_acp_sig_timer_cback;
1470                        bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1471                    }
1472                    break;
1473                }
1474            }
1475        }
1476    }
1477#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1478    else if (event == BTA_AR_AVDT_CONN_EVT)
1479    {
1480        bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1481    }
1482#endif
1483    else
1484    {
1485        /* disconnected. */
1486        p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1487        if(p_lcb && p_lcb->conn_msk)
1488        {
1489            APPL_TRACE_DEBUG1("conn_msk: 0x%x", p_lcb->conn_msk);
1490            /* clean up ssm  */
1491            for(xx=0; xx < BTA_AV_NUM_STRS; xx++)
1492            {
1493                mask = 1 << (xx + 1);
1494                if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
1495                    (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0))
1496                {
1497                    bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1498                }
1499            }
1500        }
1501    }
1502    APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
1503}
1504
1505/*******************************************************************************
1506**
1507** Function         bta_av_sig_timer
1508**
1509** Description      process the signal channel timer. This timer is started
1510**                  when the AVDTP signal channel is connected. If no profile
1511**                  is connected, the timer goes off every BTA_AV_SIG_TIME_VAL
1512**
1513** Returns          void
1514**
1515*******************************************************************************/
1516void bta_av_sig_timer(tBTA_AV_DATA *p_data)
1517{
1518    tBTA_AV_CB   *p_cb = &bta_av_cb;
1519    int     xx;
1520    UINT8   mask;
1521    tBTA_AV_LCB *p_lcb = NULL;
1522    tBTA_AV_PEND pend;
1523
1524    APPL_TRACE_DEBUG0("bta_av_sig_timer");
1525    for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
1526    {
1527        mask = 1 << xx;
1528        if(mask & p_cb->conn_lcb)
1529        {
1530            /* this entry is used. check if it is connected */
1531            p_lcb = &p_cb->lcb[xx];
1532            if(!p_lcb->conn_msk)
1533            {
1534                bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
1535                bdcpy(pend.bd_addr, p_lcb->addr);
1536                (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
1537            }
1538        }
1539    }
1540}
1541
1542/*******************************************************************************
1543**
1544** Function         bta_av_acp_sig_timer_cback
1545**
1546** Description      Process the timeout when SRC is accepting connection
1547**                  and SNK did not start signalling.
1548**
1549** Returns          void
1550**
1551*******************************************************************************/
1552static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
1553{
1554    UINT8   inx = (UINT8)p_tle->param;
1555    tBTA_AV_CB  *p_cb = &bta_av_cb;
1556    tBTA_AV_SCB *p_scb = p_cb->p_scb[inx];
1557    tBTA_AV_API_OPEN  *p_buf;
1558
1559    if (p_scb)
1560    {
1561        APPL_TRACE_DEBUG1("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
1562
1563        if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
1564        {
1565            p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1566
1567            if (bta_av_is_scb_opening(p_scb))
1568            {
1569                if (p_scb->p_disc_db)
1570                {
1571                    /* We are still doing SDP. Run the timer again. */
1572                    p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1573
1574                    p_cb->acp_sig_tmr.param = (UINT32)inx;
1575                    p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
1576                    bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1577                }
1578                else
1579                {
1580                    /* SNK did not start signalling, resume signalling process. */
1581                    bta_av_discover_req (p_scb, NULL);
1582                }
1583            }
1584            else if (bta_av_is_scb_incoming(p_scb))
1585            {
1586                /* Stay in incoming state if SNK does not start signalling */
1587
1588                /* API open was called right after SNK opened L2C connection. */
1589                if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
1590                {
1591                    p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1592
1593                    /* BTA_AV_API_OPEN_EVT */
1594                    if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
1595                    {
1596                        memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1597                        bta_sys_sendmsg(p_buf);
1598                    }
1599                }
1600            }
1601        }
1602    }
1603}
1604
1605/*******************************************************************************
1606**
1607** Function         bta_av_check_peer_features
1608**
1609** Description      checks
1610**
1611** Returns          void
1612**
1613*******************************************************************************/
1614tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
1615{
1616    tBTA_AV_FEAT peer_features = 0;
1617    tBTA_AV_CB   *p_cb = &bta_av_cb;
1618    tSDP_DISC_REC       *p_rec = NULL;
1619    tSDP_DISC_ATTR      *p_attr;
1620    UINT16              peer_rc_version=0;
1621    UINT16              categories = 0;
1622
1623    APPL_TRACE_DEBUG1("bta_av_check_peer_features service_uuid:x%x", service_uuid);
1624    /* loop through all records we found */
1625    while (TRUE)
1626    {
1627        /* get next record; if none found, we're done */
1628        if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL)
1629        {
1630            break;
1631        }
1632
1633        if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL)
1634        {
1635            /* find peer features */
1636            if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL))
1637            {
1638                peer_features |= BTA_AV_FEAT_RCCT;
1639            }
1640            if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL))
1641            {
1642                peer_features |= BTA_AV_FEAT_RCTG;
1643            }
1644        }
1645
1646        if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL)
1647        {
1648            /* get profile version (if failure, version parameter is not updated) */
1649            SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1650            APPL_TRACE_DEBUG1("peer_rc_version 0x%x", peer_rc_version);
1651
1652            if (peer_rc_version >= AVRC_REV_1_3)
1653                peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1654
1655            if (peer_rc_version >= AVRC_REV_1_4)
1656            {
1657                peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1658                /* get supported categories */
1659                if ((p_attr = SDP_FindAttributeInRec(p_rec,
1660                                ATTR_ID_SUPPORTED_FEATURES)) != NULL)
1661                {
1662                    categories = p_attr->attr_value.v.u16;
1663                    if (categories & AVRC_SUPF_CT_BROWSE)
1664                        peer_features |= (BTA_AV_FEAT_BROWSE);
1665                }
1666            }
1667        }
1668    }
1669    APPL_TRACE_DEBUG1("peer_features:x%x", peer_features);
1670    return peer_features;
1671}
1672
1673/*******************************************************************************
1674**
1675** Function         bta_av_rc_disc_done
1676**
1677** Description      Handle AVRCP service discovery results.  If matching
1678**                  service found, open AVRCP connection.
1679**
1680** Returns          void
1681**
1682*******************************************************************************/
1683void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
1684{
1685    tBTA_AV_CB   *p_cb = &bta_av_cb;
1686    tBTA_AV_SCB  *p_scb = NULL;
1687    tBTA_AV_LCB  *p_lcb;
1688    tBTA_AV_RC_OPEN rc_open;
1689    tBTA_AV_RC_FEAT rc_feat;
1690    UINT8               rc_handle;
1691    tBTA_AV_FEAT        peer_features;  /* peer features mask */
1692
1693    APPL_TRACE_DEBUG1("bta_av_rc_disc_done disc:x%x", p_cb->disc);
1694    if (!p_cb->disc)
1695    {
1696        return;
1697    }
1698
1699    if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
1700    {
1701        /* this is the rc handle/index to tBTA_AV_RCB */
1702        rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1703    }
1704    else
1705    {
1706        p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1707        if (p_scb)
1708            rc_handle = p_scb->rc_handle;
1709        else
1710        {
1711            p_cb->disc = 0;
1712            return;
1713        }
1714    }
1715
1716    APPL_TRACE_DEBUG1("rc_handle %d", rc_handle);
1717    peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
1718    if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features&BTA_AV_FEAT_ADV_CTRL) == 0))
1719    {
1720        /* if we support advance control and peer does not, check their support on TG role
1721         * some implementation uses 1.3 on CT ans 1.4 on TG */
1722        peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1723    }
1724
1725    p_cb->disc = 0;
1726    utl_freebuf((void **) &p_cb->p_disc_db);
1727
1728    APPL_TRACE_DEBUG2("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
1729
1730    /* if we have no rc connection */
1731    if (rc_handle == BTA_AV_RC_HANDLE_NONE)
1732    {
1733        if (p_scb)
1734        {
1735            /* if peer remote control service matches ours and USE_RC is TRUE */
1736            if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
1737                 ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) )
1738            {
1739                p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
1740                if(p_lcb)
1741                {
1742                    rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
1743                    p_cb->rcb[rc_handle].peer_features = peer_features;
1744                }
1745#if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
1746                else
1747                {
1748                    APPL_TRACE_ERROR0("can not find LCB!!");
1749                }
1750#endif
1751            }
1752            else if(p_scb->use_rc)
1753            {
1754                /* can not find AVRC on peer device. report failure */
1755                p_scb->use_rc = FALSE;
1756                bdcpy(rc_open.peer_addr, p_scb->peer_addr);
1757                rc_open.peer_features = 0;
1758                rc_open.status = BTA_AV_FAIL_SDP;
1759                (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
1760            }
1761        }
1762    }
1763    else
1764    {
1765        p_cb->rcb[rc_handle].peer_features = peer_features;
1766        rc_feat.rc_handle =  rc_handle;
1767        rc_feat.peer_features = peer_features;
1768        (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
1769    }
1770}
1771
1772/*******************************************************************************
1773**
1774** Function         bta_av_rc_closed
1775**
1776** Description      Set AVRCP state to closed.
1777**
1778** Returns          void
1779**
1780*******************************************************************************/
1781void bta_av_rc_closed(tBTA_AV_DATA *p_data)
1782{
1783    tBTA_AV_CB   *p_cb = &bta_av_cb;
1784    tBTA_AV_RC_CLOSE rc_close;
1785    tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
1786    tBTA_AV_RCB    *p_rcb;
1787    tBTA_AV_SCB    *p_scb;
1788    int i;
1789    BOOLEAN conn = FALSE;
1790    tBTA_AV_LCB *p_lcb;
1791
1792    rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
1793    APPL_TRACE_DEBUG1("bta_av_rc_closed rc_handle:%d", p_msg->handle);
1794    for(i=0; i<BTA_AV_NUM_RCB; i++)
1795    {
1796        p_rcb = &p_cb->rcb[i];
1797        APPL_TRACE_DEBUG3("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
1798        if(p_rcb->handle == p_msg->handle)
1799        {
1800            rc_close.rc_handle = i;
1801            p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
1802            p_rcb->peer_features = 0;
1803            APPL_TRACE_DEBUG2("       shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
1804            if(p_rcb->shdl)
1805            {
1806                p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1807                if(p_scb)
1808                {
1809                    bdcpy(rc_close.peer_addr, p_scb->peer_addr);
1810                    if(p_scb->rc_handle == p_rcb->handle)
1811                        p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
1812                    APPL_TRACE_DEBUG2("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
1813                }
1814                p_rcb->shdl = 0;
1815            }
1816            else if(p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) )
1817            {
1818                /* if the RCB uses the extra LCB, use the addr for event and clean it */
1819                p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
1820                bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1821                APPL_TRACE_DEBUG6("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
1822                              p_msg->peer_addr[0], p_msg->peer_addr[1],
1823                              p_msg->peer_addr[2], p_msg->peer_addr[3],
1824                              p_msg->peer_addr[4], p_msg->peer_addr[5]);
1825                p_lcb->conn_msk = 0;
1826                p_lcb->lidx = 0;
1827            }
1828            p_rcb->lidx = 0;
1829
1830            if((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)
1831            {
1832                /* AVCT CCB is deallocated */
1833                p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
1834                p_rcb->status = 0;
1835            }
1836            else
1837            {
1838                /* AVCT CCB is still there. dealloc */
1839                bta_av_del_rc(p_rcb);
1840
1841                /* if the AVRCP is no longer listening, create the listening channel */
1842                if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
1843                    bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1844            }
1845        }
1846        else if((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK))
1847        {
1848            /* at least one channel is still connected */
1849            conn = TRUE;
1850        }
1851    }
1852
1853    if(!conn)
1854    {
1855        /* no AVRC channels are connected, go back to INIT state */
1856        bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
1857    }
1858
1859    if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE)
1860    {
1861        rc_close.rc_handle = p_msg->handle;
1862        bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1863    }
1864    (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
1865}
1866
1867/*******************************************************************************
1868**
1869** Function         bta_av_rc_disc
1870**
1871** Description      start AVRC SDP discovery.
1872**
1873** Returns          void
1874**
1875*******************************************************************************/
1876void bta_av_rc_disc(UINT8 disc)
1877{
1878    tBTA_AV_CB   *p_cb = &bta_av_cb;
1879    tAVRC_SDP_DB_PARAMS db_params;
1880      UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
1881                                       ATTR_ID_BT_PROFILE_DESC_LIST,
1882                                       ATTR_ID_SUPPORTED_FEATURES};
1883    UINT8       hdi;
1884    tBTA_AV_SCB *p_scb;
1885    UINT8       *p_addr = NULL;
1886    UINT8       rc_handle;
1887
1888    APPL_TRACE_DEBUG2("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
1889    if ((bta_av_cb.disc != 0) || (disc == 0))
1890        return;
1891
1892    if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
1893    {
1894        /* this is the rc handle/index to tBTA_AV_RCB */
1895        rc_handle = disc & (~BTA_AV_CHNL_MSK);
1896        if (p_cb->rcb[rc_handle].lidx)
1897        {
1898            p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx-1].addr;
1899        }
1900    }
1901    else
1902    {
1903        hdi = (disc & BTA_AV_HNDL_MSK) - 1;
1904        p_scb = p_cb->p_scb[hdi];
1905
1906        if (p_scb)
1907        {
1908            APPL_TRACE_DEBUG1("rc_handle %d", p_scb->rc_handle);
1909            p_addr = p_scb->peer_addr;
1910        }
1911    }
1912
1913    if (p_addr)
1914    {
1915        /* allocate discovery database */
1916        if (p_cb->p_disc_db == NULL)
1917        {
1918            p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
1919        }
1920
1921        if (p_cb->p_disc_db)
1922        {
1923            /* set up parameters */
1924            db_params.db_len = BTA_AV_DISC_BUF_SIZE;
1925            db_params.num_attr = 3;
1926            db_params.p_db = p_cb->p_disc_db;
1927            db_params.p_attrs = attr_list;
1928
1929            /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
1930            if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
1931                            bta_av_avrc_sdp_cback) == AVRC_SUCCESS)
1932            {
1933                p_cb->disc = disc;
1934                APPL_TRACE_DEBUG1("disc %d", p_cb->disc);
1935            }
1936        }
1937    }
1938}
1939
1940/*******************************************************************************
1941**
1942** Function         bta_av_dereg_comp
1943**
1944** Description      deregister complete. free the stream control block.
1945**
1946** Returns          void
1947**
1948*******************************************************************************/
1949void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
1950{
1951    tBTA_AV_CB   *p_cb = &bta_av_cb;
1952    tBTA_AV_SCB  *p_scb;
1953    tBTA_UTL_COD    cod;
1954    UINT8   mask;
1955    BT_HDR  *p_buf;
1956
1957    /* find the stream control block */
1958    p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
1959
1960    if(p_scb)
1961    {
1962        APPL_TRACE_DEBUG2("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
1963        mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1964        if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
1965        {
1966            p_cb->reg_audio  &= ~mask;
1967            if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
1968            {
1969                /* this channel is still marked as open. decrease the count */
1970                bta_av_cb.audio_open_cnt--;
1971            }
1972            p_cb->conn_audio &= ~mask;
1973
1974            if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM)
1975            {
1976            /* make sure no buffers are in q_info.a2d */
1977            while((p_buf = (BT_HDR*)GKI_dequeue (&p_scb->q_info.a2d)) != NULL)
1978                GKI_freebuf(p_buf);
1979            }
1980
1981            /* remove the A2DP SDP record, if no more audio stream is left */
1982            if(!p_cb->reg_audio)
1983            {
1984#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1985                bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
1986#endif
1987                bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
1988                bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
1989            }
1990        }
1991        else
1992        {
1993            p_cb->reg_video  &= ~mask;
1994            /* make sure that this channel is not connected */
1995            p_cb->conn_video &= ~mask;
1996            /* remove the VDP SDP record, (only one video stream at most) */
1997            bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
1998            bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
1999        }
2000
2001        /* make sure that the timer is not active */
2002        bta_sys_stop_timer(&p_scb->timer);
2003        utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
2004    }
2005
2006    APPL_TRACE_DEBUG3("audio 0x%x, video: 0x%x, disable:%d",
2007        p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
2008    /* if no stream control block is active */
2009    if((p_cb->reg_audio + p_cb->reg_video) == 0)
2010    {
2011#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
2012        /* deregister from AVDT */
2013        bta_ar_dereg_avdt(BTA_ID_AV);
2014
2015        /* deregister from AVCT */
2016        bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
2017        bta_ar_dereg_avct(BTA_ID_AV);
2018#endif
2019
2020        if(p_cb->disabling)
2021        {
2022            p_cb->disabling     = FALSE;
2023            bta_av_cb.features  = 0;
2024        }
2025
2026        /* Clear the Capturing service class bit */
2027        cod.service = BTM_COD_SERVICE_CAPTURING;
2028        utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
2029    }
2030}
2031#endif /* BTA_AV_INCLUDED */
2032