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