• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
54 static 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 *******************************************************************************/
bta_av_get_rcb_by_shdl(UINT8 shdl)65 tBTA_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 *******************************************************************************/
bta_av_del_rc(tBTA_AV_RCB * p_rcb)91 void 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 *******************************************************************************/
bta_av_close_all_rc(tBTA_AV_CB * p_cb)143 static 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 *******************************************************************************/
bta_av_del_sdp_rec(UINT32 * p_sdp_handle)163 static 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 *******************************************************************************/
bta_av_avrc_sdp_cback(UINT16 status)181 static 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 *******************************************************************************/
bta_av_rc_ctrl_cback(UINT8 handle,UINT8 event,UINT16 result,BD_ADDR peer_addr)201 static 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 *******************************************************************************/
bta_av_rc_msg_cback(UINT8 handle,UINT8 label,UINT8 opcode,tAVRC_MSG * p_msg)245 static 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 *******************************************************************************/
bta_av_rc_create(tBTA_AV_CB * p_cb,UINT8 role,UINT8 shdl,UINT8 lidx)296 UINT8 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 *******************************************************************************/
bta_av_group_navi_supported(UINT8 len,UINT8 * p_data)366 static tBTA_AV_CODE bta_av_group_navi_supported(UINT8 len, UINT8 *p_data)
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 (u16 <= AVRC_PDU_PREV_GROUP)
381                 ret = BTA_AV_RSP_ACCEPT;
382             else
383                 ret = BTA_AV_RSP_REJ;
384         }
385     }
386 
387     return ret;
388 }
389 
390 /*******************************************************************************
391 **
392 ** Function         bta_av_op_supported
393 **
394 ** Description      Check if remote control operation is supported.
395 **
396 ** Returns          BTA_AV_RSP_ACCEPT of supported, BTA_AV_RSP_NOT_IMPL if not.
397 **
398 *******************************************************************************/
bta_av_op_supported(tBTA_AV_RC rc_id)399 static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id)
400 {
401     tBTA_AV_CODE ret_code = BTA_AV_RSP_NOT_IMPL;
402 
403     if (p_bta_av_rc_id)
404     {
405         if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
406         {
407             ret_code = BTA_AV_RSP_ACCEPT;
408         }
409         else if ((p_bta_av_cfg->rc_pass_rsp == BTA_AV_RSP_INTERIM) && p_bta_av_rc_id_ac)
410         {
411             if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F)))
412             {
413                 ret_code = BTA_AV_RSP_INTERIM;
414             }
415         }
416     }
417     return ret_code;
418 }
419 
420 /*******************************************************************************
421 **
422 ** Function         bta_av_find_lcb
423 **
424 ** Description      Given BD_addr, find the associated LCB.
425 **
426 ** Returns          NULL, if not found.
427 **
428 *******************************************************************************/
bta_av_find_lcb(BD_ADDR addr,UINT8 op)429 tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op)
430 {
431     tBTA_AV_CB   *p_cb = &bta_av_cb;
432     int     xx;
433     UINT8   mask;
434     tBTA_AV_LCB *p_lcb = NULL;
435 
436     for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
437     {
438         mask = 1 << xx; /* the used mask for this lcb */
439         if((mask & p_cb->conn_lcb) && 0 ==( bdcmp(p_cb->lcb[xx].addr, addr)))
440         {
441             p_lcb = &p_cb->lcb[xx];
442             if(op == BTA_AV_LCB_FREE)
443             {
444                 p_cb->conn_lcb &= ~mask; /* clear the connect mask */
445                 APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
446             }
447             break;
448         }
449     }
450     return p_lcb;
451 }
452 
453 /*******************************************************************************
454 **
455 ** Function         bta_av_rc_opened
456 **
457 ** Description      Set AVRCP state to opened.
458 **
459 ** Returns          void
460 **
461 *******************************************************************************/
bta_av_rc_opened(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)462 void bta_av_rc_opened(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
463 {
464     tBTA_AV_RC_OPEN rc_open;
465     tBTA_AV_SCB     *p_scb;
466     int         i;
467     UINT8       shdl = 0;
468     tBTA_AV_LCB *p_lcb;
469     tBTA_AV_RCB *p_rcb;
470     UINT8       tmp;
471     UINT8       disc = 0;
472 
473     /* find the SCB & stop the timer */
474     for(i=0; i<BTA_AV_NUM_STRS; i++)
475     {
476         p_scb = p_cb->p_scb[i];
477         if(p_scb && bdcmp(p_scb->peer_addr, p_data->rc_conn_chg.peer_addr) == 0)
478         {
479             p_scb->rc_handle = p_data->rc_conn_chg.handle;
480             APPL_TRACE_DEBUG2("bta_av_rc_opened shdl:%d, srch %d", i + 1, p_scb->rc_handle);
481             shdl = i+1;
482             APPL_TRACE_ERROR1("use_rc:%d", p_scb->use_rc);
483             bta_sys_stop_timer(&p_scb->timer);
484             disc = p_scb->hndl;
485             break;
486         }
487     }
488 
489     i = p_data->rc_conn_chg.handle;
490     if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE)
491     {
492         APPL_TRACE_ERROR1("not a valid handle:%d any more", i);
493         return;
494     }
495 
496 
497     if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0)
498     {
499         /* rc is opened on the RC only ACP channel, but is for a specific
500          * SCB -> need to switch RCBs */
501         p_rcb = bta_av_get_rcb_by_shdl(shdl);
502         if (p_rcb)
503         {
504             p_rcb->shdl = p_cb->rcb[i].shdl;
505             tmp         = p_rcb->lidx;
506             p_rcb->lidx = p_cb->rcb[i].lidx;
507             p_cb->rcb[i].lidx = tmp;
508             p_cb->rc_acp_handle = p_rcb->handle;
509             p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
510             APPL_TRACE_DEBUG2("switching RCB rc_acp_handle:%d idx:%d",
511                                p_cb->rc_acp_handle, p_cb->rc_acp_idx);
512         }
513     }
514 
515     p_cb->rcb[i].shdl = shdl;
516     rc_open.rc_handle = i;
517     APPL_TRACE_ERROR4("bta_av_rc_opened rcb[%d] shdl:%d lidx:%d/%d",
518             i, shdl, p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
519     p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
520 
521     if(!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx)
522     {
523         /* no associated SCB -> connected to an RC only device
524          * update the index to the extra LCB */
525         p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
526         bdcpy(p_lcb->addr, p_data->rc_conn_chg.peer_addr);
527         APPL_TRACE_DEBUG6("rc_only bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
528                       p_lcb->addr[0], p_lcb->addr[1],
529                       p_lcb->addr[2], p_lcb->addr[3],
530                       p_lcb->addr[4], p_lcb->addr[5]);
531         p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
532             p_cb->rcb[i].lidx = p_lcb->lidx;
533         p_lcb->conn_msk = 1;
534         APPL_TRACE_ERROR3("rcb[%d].lidx=%d, lcb.conn_msk=x%x",
535             i, p_cb->rcb[i].lidx, p_lcb->conn_msk);
536         disc = p_data->rc_conn_chg.handle|BTA_AV_CHNL_MSK;
537     }
538 
539     bdcpy(rc_open.peer_addr, p_data->rc_conn_chg.peer_addr);
540     rc_open.peer_features = p_cb->rcb[i].peer_features;
541     rc_open.status = BTA_AV_SUCCESS;
542     APPL_TRACE_DEBUG2("local features:x%x peer_features:x%x", p_cb->features,
543                       rc_open.peer_features);
544     if(rc_open.peer_features == 0)
545     {
546         /* we have not done SDP on peer RC capabilities.
547          * peer must have initiated the RC connection */
548         rc_open.peer_features = BTA_AV_FEAT_RCCT;
549         bta_av_rc_disc(disc);
550     }
551     (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
552 
553 }
554 
555 
556 /*******************************************************************************
557 **
558 ** Function         bta_av_rc_remote_cmd
559 **
560 ** Description      Send an AVRCP remote control command.
561 **
562 ** Returns          void
563 **
564 *******************************************************************************/
bta_av_rc_remote_cmd(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)565 void bta_av_rc_remote_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
566 {
567     tBTA_AV_RCB    *p_rcb;
568     if (p_cb->features & BTA_AV_FEAT_RCCT)
569     {
570         if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
571         {
572             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
573             if(p_rcb->status & BTA_AV_RC_CONN_MASK)
574             {
575                 AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
576                      &p_data->api_remote_cmd.msg);
577             }
578         }
579     }
580 }
581 
582 /*******************************************************************************
583 **
584 ** Function         bta_av_rc_vendor_cmd
585 **
586 ** Description      Send an AVRCP vendor specific command.
587 **
588 ** Returns          void
589 **
590 *******************************************************************************/
bta_av_rc_vendor_cmd(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)591 void bta_av_rc_vendor_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
592 {
593     tBTA_AV_RCB    *p_rcb;
594     if ( (p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
595          (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR))
596     {
597         if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
598         {
599             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
600             AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
601         }
602     }
603 }
604 
605 /*******************************************************************************
606 **
607 ** Function         bta_av_rc_vendor_rsp
608 **
609 ** Description      Send an AVRCP vendor specific response.
610 **
611 ** Returns          void
612 **
613 *******************************************************************************/
bta_av_rc_vendor_rsp(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)614 void bta_av_rc_vendor_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
615 {
616     tBTA_AV_RCB    *p_rcb;
617     if ( (p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
618          (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR))
619     {
620         if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
621         {
622             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
623             AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
624         }
625     }
626 }
627 
628 /*******************************************************************************
629 **
630 ** Function         bta_av_rc_meta_rsp
631 **
632 ** Description      Send an AVRCP metadata/advanced control command/response.
633 **
634 ** Returns          void
635 **
636 *******************************************************************************/
bta_av_rc_meta_rsp(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)637 void bta_av_rc_meta_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
638 {
639     tBTA_AV_RCB *p_rcb;
640     BOOLEAN         free = TRUE;
641 
642     if ((p_cb->features & BTA_AV_FEAT_METADATA) && (p_data->hdr.layer_specific < BTA_AV_NUM_RCB))
643     {
644         if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
645             (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT)) )
646         {
647             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
648             AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label, p_data->api_meta_rsp.rsp_code,
649                                       p_data->api_meta_rsp.p_pkt);
650             free = FALSE;
651         }
652     }
653 
654     if (free)
655         GKI_freebuf (p_data->api_meta_rsp.p_pkt);
656 }
657 
658 /*******************************************************************************
659 **
660 ** Function         bta_av_rc_free_rsp
661 **
662 ** Description      free an AVRCP metadata command buffer.
663 **
664 ** Returns          void
665 **
666 *******************************************************************************/
bta_av_rc_free_rsp(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)667 void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
668 {
669     GKI_freebuf (p_data->api_meta_rsp.p_pkt);
670 }
671 
672 /*******************************************************************************
673 **
674 ** Function         bta_av_rc_meta_req
675 **
676 ** Description      Send an AVRCP metadata command.
677 **
678 ** Returns          void
679 **
680 *******************************************************************************/
bta_av_rc_free_msg(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)681 void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
682 {
683 }
684 
685 
686 
687 /*******************************************************************************
688 **
689 ** Function         bta_av_chk_notif_evt_id
690 **
691 ** Description      make sure the requested player id is valid.
692 **
693 ** Returns          BTA_AV_STS_NO_RSP, if no error
694 **
695 *******************************************************************************/
bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR * p_vendor)696 static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR *p_vendor)
697 {
698     tAVRC_STS   status = BTA_AV_STS_NO_RSP;
699     UINT8       xx;
700     UINT16      u16;
701     UINT8       *p = p_vendor->p_vendor_data + 2;
702 
703     BE_STREAM_TO_UINT16 (u16, p);
704     /* double check the fixed length */
705     if ((u16 != 5) || (p_vendor->vendor_len != 9))
706     {
707         status = AVRC_STS_INTERNAL_ERR;
708     }
709     else
710     {
711         /* make sure the player_id is valid */
712         for (xx=0; xx<p_bta_av_cfg->num_evt_ids; xx++)
713         {
714             if (*p == p_bta_av_cfg->p_meta_evt_ids[xx])
715             {
716                 break;
717             }
718         }
719         if (xx == p_bta_av_cfg->num_evt_ids)
720         {
721             status = AVRC_STS_BAD_PARAM;
722         }
723     }
724 
725     return status;
726 }
727 
728 /*******************************************************************************
729 **
730 ** Function         bta_av_proc_meta_cmd
731 **
732 ** Description      Process an AVRCP metadata command from the peer.
733 **
734 ** Returns          TRUE to respond immediately
735 **
736 *******************************************************************************/
bta_av_proc_meta_cmd(tAVRC_RESPONSE * p_rc_rsp,tBTA_AV_RC_MSG * p_msg,UINT8 * p_ctype)737 tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE  *p_rc_rsp, tBTA_AV_RC_MSG *p_msg, UINT8 *p_ctype)
738 {
739     tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
740     UINT8       u8, pdu, *p;
741     UINT16      u16;
742     tAVRC_MSG_VENDOR    *p_vendor = &p_msg->msg.vendor;
743 
744     pdu = *(p_vendor->p_vendor_data);
745     p_rc_rsp->pdu = pdu;
746     *p_ctype = AVRC_RSP_REJ;
747     /* Metadata messages only use PANEL sub-unit type */
748     if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL)
749     {
750         APPL_TRACE_DEBUG0("SUBUNIT must be PANEL");
751         /* reject it */
752         evt=0;
753         p_vendor->hdr.ctype = BTA_AV_RSP_NOT_IMPL;
754         AVRC_VendorRsp(p_msg->handle, p_msg->label, &p_msg->msg.vendor);
755     }
756     else
757     {
758         switch (pdu)
759         {
760         case AVRC_PDU_GET_CAPABILITIES:
761             /* process GetCapabilities command without reporting the event to app */
762             evt = 0;
763             u8 = *(p_vendor->p_vendor_data + 4);
764             p = p_vendor->p_vendor_data + 2;
765             p_rc_rsp->get_caps.capability_id = u8;
766             BE_STREAM_TO_UINT16 (u16, p);
767             if ((u16 != 1) || (p_vendor->vendor_len != 5))
768             {
769                 p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
770             }
771             else
772             {
773                 p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
774                 if (u8 == AVRC_CAP_COMPANY_ID)
775                 {
776                     *p_ctype = AVRC_RSP_IMPL_STBL;
777                     p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
778                     memcpy(p_rc_rsp->get_caps.param.company_id, p_bta_av_cfg->p_meta_co_ids,
779                            (p_bta_av_cfg->num_co_ids << 2));
780                 }
781                 else if (u8 == AVRC_CAP_EVENTS_SUPPORTED)
782                 {
783                     *p_ctype = AVRC_RSP_IMPL_STBL;
784                     p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
785                     memcpy(p_rc_rsp->get_caps.param.event_id, p_bta_av_cfg->p_meta_evt_ids,
786                            p_bta_av_cfg->num_evt_ids);
787                 }
788                 else
789                 {
790                     APPL_TRACE_DEBUG1("Invalid capability ID: 0x%x", u8);
791                     /* reject - unknown capability ID */
792                     p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
793                 }
794             }
795             break;
796 
797 
798         case AVRC_PDU_REGISTER_NOTIFICATION:
799             /* make sure the event_id is implemented */
800             p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id (p_vendor);
801             if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP)
802                 evt = 0;
803             break;
804 
805         }
806     }
807 
808     return evt;
809 }
810 
811 
812 /*******************************************************************************
813 **
814 ** Function         bta_av_rc_msg
815 **
816 ** Description      Process an AVRCP message from the peer.
817 **
818 ** Returns          void
819 **
820 *******************************************************************************/
bta_av_rc_msg(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)821 void bta_av_rc_msg(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
822 {
823     tBTA_AV_EVT evt = 0;
824     tBTA_AV     av;
825     BT_HDR      *p_pkt = NULL;
826     tAVRC_MSG_VENDOR    *p_vendor = &p_data->rc_msg.msg.vendor;
827 
828     if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU)
829     {
830     /* if this is a pass thru command */
831         if (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL)
832         {
833         /* check if operation is supported */
834             if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR)
835             {
836                 p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
837             }
838             else
839             {
840                 p_data->rc_msg.msg.hdr.ctype = bta_av_op_supported(p_data->rc_msg.msg.pass.op_id);
841             }
842 
843             /* send response */
844             if (p_data->rc_msg.msg.hdr.ctype != BTA_AV_RSP_INTERIM)
845                 AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
846 
847             /* set up for callback if supported */
848             if (p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_ACCEPT || p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_INTERIM)
849             {
850                 evt = BTA_AV_REMOTE_CMD_EVT;
851                 av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
852                 av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
853                 av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
854                 av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
855                 memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof (tAVRC_HDR));
856                 av.remote_cmd.label = p_data->rc_msg.label;
857             }
858         }
859         /* else if this is a pass thru response */
860         else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
861         {
862             /* set up for callback */
863             evt = BTA_AV_REMOTE_RSP_EVT;
864             av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
865             av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
866             av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
867             av.remote_rsp.label = p_data->rc_msg.label;
868         }
869         /* must be a bad ctype -> reject*/
870         else
871         {
872             p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
873             AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
874         }
875     }
876     /* else if this is a vendor specific command or response */
877     else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR)
878     {
879         /* set up for callback */
880         av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
881         av.vendor_cmd.company_id = p_vendor->company_id;
882         av.vendor_cmd.label = p_data->rc_msg.label;
883         av.vendor_cmd.p_data = p_vendor->p_vendor_data;
884         av.vendor_cmd.len = p_vendor->vendor_len;
885 
886         /* if configured to support vendor specific and it's a command */
887         if ((p_cb->features & BTA_AV_FEAT_VENDOR)  &&
888             p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
889         {
890                 evt = BTA_AV_VENDOR_CMD_EVT;
891         }
892         /* else if configured to support vendor specific and it's a response */
893         else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
894                  p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
895         {
896                 evt = BTA_AV_VENDOR_RSP_EVT;
897 
898         }
899         /* else if not configured to support vendor specific and it's a command */
900         else if (!(p_cb->features & BTA_AV_FEAT_VENDOR)  &&
901             p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
902         {
903            if(p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID)
904            {
905            /* reject it */
906               p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
907               p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
908            }
909            else
910               p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
911            AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.vendor);
912         }
913     }
914 
915     /* call callback */
916     if (evt != 0)
917     {
918         av.remote_cmd.rc_handle = p_data->rc_msg.handle;
919         (*p_cb->p_cback)(evt, &av);
920     }
921 }
922 
923 /*******************************************************************************
924 **
925 ** Function         bta_av_rc_close
926 **
927 ** Description      close the specified AVRC handle.
928 **
929 ** Returns          void
930 **
931 *******************************************************************************/
bta_av_rc_close(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)932 void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
933 {
934     UINT16 handle = p_data->hdr.layer_specific;
935     tBTA_AV_SCB  *p_scb;
936     tBTA_AV_RCB *p_rcb;
937 
938     if(handle < BTA_AV_NUM_RCB)
939     {
940         p_rcb = &p_cb->rcb[handle];
941 
942         APPL_TRACE_DEBUG2("bta_av_rc_close handle: %d, status=0x%x", p_rcb->handle, p_rcb->status);
943         if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
944         {
945             if(p_rcb->shdl)
946             {
947                 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
948                 if(p_scb)
949                 {
950                     /* just in case the RC timer is active
951                     if(bta_av_cb.features & BTA_AV_FEAT_RCCT &&
952                        p_scb->chnl == BTA_AV_CHNL_AUDIO) */
953                         bta_sys_stop_timer(&p_scb->timer);
954                 }
955             }
956 
957             AVRC_Close(p_rcb->handle);
958         }
959     }
960 }
961 
962 /*******************************************************************************
963 **
964 ** Function         bta_av_get_shdl
965 **
966 ** Returns          The index to p_scb[]
967 **
968 *******************************************************************************/
bta_av_get_shdl(tBTA_AV_SCB * p_scb)969 static UINT8 bta_av_get_shdl(tBTA_AV_SCB *p_scb)
970 {
971     int     i;
972     UINT8   shdl = 0;
973     /* find the SCB & stop the timer */
974     for(i=0; i<BTA_AV_NUM_STRS; i++)
975     {
976         if(p_scb == bta_av_cb.p_scb[i])
977         {
978             shdl = i+1;
979             break;
980         }
981     }
982     return shdl;
983 }
984 
985 /*******************************************************************************
986 **
987 ** Function         bta_av_stream_chg
988 **
989 ** Description      audio streaming status changed.
990 **
991 ** Returns          void
992 **
993 *******************************************************************************/
bta_av_stream_chg(tBTA_AV_SCB * p_scb,BOOLEAN started)994 void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started)
995 {
996     UINT8   started_msk;
997     int     i;
998     UINT8   *p_streams;
999     BOOLEAN no_streams = FALSE;
1000     tBTA_AV_SCB *p_scbi;
1001 
1002     started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1003     APPL_TRACE_DEBUG3 ("bta_av_stream_chg started:%d started_msk:x%x chnl:x%x", started,
1004                                                   started_msk, p_scb->chnl);
1005     if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
1006         p_streams = &bta_av_cb.audio_streams;
1007     else
1008         p_streams = &bta_av_cb.video_streams;
1009 
1010     if (started)
1011     {
1012         /* Let L2CAP know this channel is processed with high priority */
1013         L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_HIGH);
1014         (*p_streams) |= started_msk;
1015     }
1016     else
1017     {
1018         (*p_streams) &= ~started_msk;
1019     }
1020 
1021     if (!started)
1022     {
1023         i=0;
1024         if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
1025         {
1026             if (bta_av_cb.video_streams == 0)
1027                 no_streams = TRUE;
1028         }
1029         else
1030         {
1031             no_streams = TRUE;
1032             if ( bta_av_cb.audio_streams )
1033             {
1034                 for (; i<BTA_AV_NUM_STRS; i++)
1035                 {
1036                     p_scbi = bta_av_cb.p_scb[i];
1037                     /* scb is used and started */
1038                     if ( p_scbi && (bta_av_cb.audio_streams & BTA_AV_HNDL_TO_MSK(i))
1039                         && bdcmp(p_scbi->peer_addr, p_scb->peer_addr) == 0)
1040                     {
1041                         no_streams = FALSE;
1042                         break;
1043                     }
1044                 }
1045 
1046             }
1047         }
1048 
1049         APPL_TRACE_DEBUG4 ("no_streams:%d i:%d, audio_streams:x%x, video_streams:x%x", no_streams, i,
1050                            bta_av_cb.audio_streams, bta_av_cb.video_streams);
1051         if (no_streams)
1052         {
1053             /* Let L2CAP know this channel is processed with low priority */
1054             L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_NORMAL);
1055         }
1056     }
1057 }
1058 
1059 
1060 /*******************************************************************************
1061 **
1062 ** Function         bta_av_conn_chg
1063 **
1064 ** Description      connetion status changed.
1065 **                  Open an AVRCP acceptor channel, if new conn.
1066 **
1067 ** Returns          void
1068 **
1069 *******************************************************************************/
bta_av_conn_chg(tBTA_AV_DATA * p_data)1070 void bta_av_conn_chg(tBTA_AV_DATA *p_data)
1071 {
1072     tBTA_AV_CB   *p_cb = &bta_av_cb;
1073     tBTA_AV_SCB     *p_scb;
1074     tBTA_AV_SCB     *p_scbi;
1075     UINT8   mask;
1076     UINT8   conn_msk;
1077     UINT8   old_msk;
1078     int i;
1079     int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
1080     tBTA_AV_LCB *p_lcb;
1081     tBTA_AV_LCB *p_lcb_rc;
1082     tBTA_AV_RCB *p_rcb, *p_rcb2;
1083     BOOLEAN     chk_restore = FALSE;
1084 
1085     p_scb = p_cb->p_scb[index];
1086 
1087     mask = BTA_AV_HNDL_TO_MSK(index);
1088     p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
1089     conn_msk = 1 << (index + 1);
1090     if(p_data->conn_chg.is_up)
1091     {
1092         /* set the conned mask for this channel */
1093         if(p_scb)
1094         {
1095             if(p_lcb)
1096             {
1097                 p_lcb->conn_msk |= conn_msk;
1098                 for (i=0; i<BTA_AV_NUM_RCB; i++)
1099                 {
1100                     if (bta_av_cb.rcb[i].lidx == p_lcb->lidx)
1101                     {
1102                         bta_av_cb.rcb[i].shdl = index + 1;
1103                         APPL_TRACE_DEBUG5("conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
1104                                           bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1105                                           bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1106                         break;
1107                     }
1108                 }
1109             }
1110             if (p_scb->chnl == BTA_AV_CHNL_AUDIO)
1111             {
1112                 old_msk = p_cb->conn_audio;
1113                 p_cb->conn_audio |= mask;
1114             }
1115             else
1116             {
1117                 old_msk = p_cb->conn_video;
1118                 p_cb->conn_video |= mask;
1119             }
1120 
1121             if ((old_msk & mask) == 0)
1122             {
1123                 /* increase the audio open count, if not set yet */
1124                 bta_av_cb.audio_open_cnt++;
1125             }
1126 
1127 
1128             APPL_TRACE_DEBUG2("rc_acp_handle:%d rc_acp_idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1129             /* check if the AVRCP ACP channel is already connected */
1130             if(p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE && p_cb->rc_acp_idx)
1131             {
1132                 p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
1133                 APPL_TRACE_DEBUG1("rc_acp is connected && conn_chg on same addr p_lcb_rc->conn_msk:x%x",
1134                                   p_lcb_rc->conn_msk);
1135                 /* check if the RC is connected to the scb addr */
1136                 APPL_TRACE_DEBUG6 ("p_lcb_rc->addr: %02x:%02x:%02x:%02x:%02x:%02x",
1137                        p_lcb_rc->addr[0], p_lcb_rc->addr[1], p_lcb_rc->addr[2], p_lcb_rc->addr[3],
1138                        p_lcb_rc->addr[4], p_lcb_rc->addr[5]);
1139                 APPL_TRACE_DEBUG6 ("conn_chg.peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
1140                        p_data->conn_chg.peer_addr[0], p_data->conn_chg.peer_addr[1],
1141                        p_data->conn_chg.peer_addr[2],
1142                        p_data->conn_chg.peer_addr[3], p_data->conn_chg.peer_addr[4],
1143                        p_data->conn_chg.peer_addr[5]);
1144                 if (p_lcb_rc->conn_msk && bdcmp(p_lcb_rc->addr, p_data->conn_chg.peer_addr) == 0)
1145                 {
1146                     /* AVRCP is already connected.
1147                      * need to update the association betwen SCB and RCB */
1148                     p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
1149                     p_lcb_rc->lidx = 0;
1150                     p_scb->rc_handle = p_cb->rc_acp_handle;
1151                     p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
1152                     p_rcb->shdl = bta_av_get_shdl(p_scb);
1153                     APPL_TRACE_DEBUG3("update rc_acp shdl:%d/%d srch:%d", index + 1, p_rcb->shdl,
1154                                       p_scb->rc_handle );
1155 
1156                     p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
1157                     if (p_rcb2)
1158                     {
1159                         /* found the RCB that was created to associated with this SCB */
1160                         p_cb->rc_acp_handle = p_rcb2->handle;
1161                         p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
1162                         APPL_TRACE_DEBUG2("new rc_acp_handle:%d, idx:%d", p_cb->rc_acp_handle,
1163                                            p_cb->rc_acp_idx);
1164                         p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
1165                         APPL_TRACE_DEBUG3("rc2 handle:%d lidx:%d/%d",p_rcb2->handle, p_rcb2->lidx,
1166                                           p_cb->lcb[p_rcb2->lidx-1].lidx);
1167                     }
1168                     p_rcb->lidx = p_lcb->lidx;
1169                     APPL_TRACE_DEBUG3("rc handle:%d lidx:%d/%d",p_rcb->handle, p_rcb->lidx,
1170                                       p_cb->lcb[p_rcb->lidx-1].lidx);
1171                 }
1172             }
1173         }
1174     }
1175     else
1176     {
1177         if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
1178         {
1179             /* this channel is still marked as open. decrease the count */
1180             bta_av_cb.audio_open_cnt--;
1181         }
1182 
1183         /* clear the conned mask for this channel */
1184         p_cb->conn_audio &= ~mask;
1185         p_cb->conn_video &= ~mask;
1186         if(p_scb)
1187         {
1188             /* the stream is closed.
1189              * clear the peer address, so it would not mess up the AVRCP for the next round of operation */
1190             bdcpy(p_scb->peer_addr, bd_addr_null);
1191             if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
1192             {
1193                 if(p_lcb)
1194                 {
1195                     p_lcb->conn_msk &= ~conn_msk;
1196                 }
1197                 /* audio channel is down. make sure the INT channel is down */
1198                 /* just in case the RC timer is active
1199                 if(p_cb->features & BTA_AV_FEAT_RCCT) */
1200                 {
1201                     bta_sys_stop_timer(&p_scb->timer);
1202                 }
1203                 /* one audio channel goes down. check if we need to restore high priority */
1204                 chk_restore = TRUE;
1205             }
1206         }
1207 
1208         APPL_TRACE_DEBUG1("bta_av_conn_chg shdl:%d", index + 1);
1209         for (i=0; i<BTA_AV_NUM_RCB; i++)
1210         {
1211             APPL_TRACE_DEBUG5("conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
1212                               bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1213                               bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1214             if(bta_av_cb.rcb[i].shdl == index + 1)
1215             {
1216                 bta_av_del_rc(&bta_av_cb.rcb[i]);
1217                 break;
1218             }
1219         }
1220 
1221         if(p_cb->conn_audio == 0 && p_cb->conn_video == 0)
1222         {
1223             /* if both channels are not connected,
1224              * close all RC channels */
1225             bta_av_close_all_rc(p_cb);
1226         }
1227 
1228         /* if the AVRCP is no longer listening, create the listening channel */
1229         if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
1230             bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1231     }
1232 
1233     APPL_TRACE_DEBUG6("bta_av_conn_chg audio:%x video:%x up:%d conn_msk:0x%x chk_restore:%d audio_open_cnt:%d",
1234         p_cb->conn_audio, p_cb->conn_video, p_data->conn_chg.is_up, conn_msk, chk_restore, p_cb->audio_open_cnt);
1235 
1236     if (chk_restore)
1237     {
1238         if (p_cb->audio_open_cnt == 1)
1239         {
1240             /* one audio channel goes down and there's one audio channel remains open.
1241              * restore the switch role in default link policy */
1242             bta_sys_set_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
1243             /* allow role switch, if this is the last connection */
1244             bta_av_restore_switch();
1245         }
1246         if (p_cb->audio_open_cnt)
1247         {
1248             /* adjust flush timeout settings to longer period */
1249             for (i=0; i<BTA_AV_NUM_STRS; i++)
1250             {
1251                 p_scbi = bta_av_cb.p_scb[i];
1252                 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
1253                 {
1254                     /* may need to update the flush timeout of this already started stream */
1255                     if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
1256                     {
1257                         p_scbi->co_started = bta_av_cb.audio_open_cnt;
1258                         L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
1259                     }
1260                 }
1261             }
1262         }
1263     }
1264 }
1265 
1266 /*******************************************************************************
1267 **
1268 ** Function         bta_av_disable
1269 **
1270 ** Description      disable AV.
1271 **
1272 ** Returns          void
1273 **
1274 *******************************************************************************/
bta_av_disable(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)1275 void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
1276 {
1277     BT_HDR  hdr;
1278     UINT16  xx;
1279 
1280     p_cb->disabling = TRUE;
1281 
1282     bta_av_close_all_rc(p_cb);
1283 
1284     utl_freebuf((void **) &p_cb->p_disc_db);
1285 
1286     /* disable audio/video - de-register all channels,
1287      * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
1288     for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
1289     {
1290         hdr.layer_specific = xx + 1;
1291         bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
1292     }
1293 }
1294 
1295 /*******************************************************************************
1296 **
1297 ** Function         bta_av_api_disconnect
1298 **
1299 ** Description      .
1300 **
1301 ** Returns          void
1302 **
1303 *******************************************************************************/
bta_av_api_disconnect(tBTA_AV_DATA * p_data)1304 void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
1305 {
1306     AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
1307     bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1308 }
1309 
1310 /*******************************************************************************
1311 **
1312 ** Function         bta_av_sig_chg
1313 **
1314 ** Description      process AVDT signal channel up/down.
1315 **
1316 ** Returns          void
1317 **
1318 *******************************************************************************/
bta_av_sig_chg(tBTA_AV_DATA * p_data)1319 void bta_av_sig_chg(tBTA_AV_DATA *p_data)
1320 {
1321     UINT16 event = p_data->str_msg.hdr.layer_specific;
1322     tBTA_AV_CB   *p_cb = &bta_av_cb;
1323     int     xx;
1324     UINT8   mask;
1325     tBTA_AV_LCB *p_lcb = NULL;
1326 
1327     APPL_TRACE_DEBUG1("bta_av_sig_chg event: %d", event);
1328     if(event == AVDT_CONNECT_IND_EVT)
1329     {
1330         p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1331         if(!p_lcb)
1332         {
1333             /* if the address does not have an LCB yet, alloc one */
1334             for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
1335             {
1336                 mask = 1 << xx;
1337                 APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
1338                 if(!(mask & p_cb->conn_lcb))
1339                 {
1340                     if (!p_cb->p_scb[xx])
1341                     {
1342                         /* We do not have scb for this avdt connection.     */
1343                         /* Silently close the connection.                   */
1344                         APPL_TRACE_ERROR0("av scb not available for avdt connection");
1345                         AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
1346                         return;
1347                     }
1348 
1349                     p_lcb = &p_cb->lcb[xx];
1350                     p_lcb->lidx = xx + 1;
1351                     bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
1352                     p_lcb->conn_msk = 0; /* clear the connect mask */
1353                     /* start listening when the signal channel is open */
1354                     if (p_cb->features & BTA_AV_FEAT_RCTG)
1355                     {
1356                         bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1357                     }
1358                     /* this entry is not used yet. */
1359                     p_cb->conn_lcb |= mask;     /* mark it as used */
1360                     APPL_TRACE_DEBUG1("start sig timer %d", p_data->hdr.offset);
1361                     if (p_data->hdr.offset == AVDT_ACP)
1362                     {
1363                         APPL_TRACE_DEBUG1("Incoming L2CAP acquired, set state as incoming", NULL);
1364                         bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
1365                         p_cb->p_scb[xx]->use_rc = TRUE;     /* allowing RC for incoming connection */
1366                         bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
1367 
1368                         /* The Pending Event should be sent as soon as the L2CAP signalling channel
1369                          * is set up, which is NOW. Earlier this was done only after
1370                          * BTA_AV_SIG_TIME_VAL milliseconds.
1371                          * The following function shall send the event and start the recurring timer
1372                          */
1373                         bta_av_sig_timer(NULL);
1374 
1375                         /* Possible collision : need to avoid outgoing processing while the timer is running */
1376                         p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
1377 
1378                         p_cb->acp_sig_tmr.param = (UINT32)xx;
1379                         p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK*)&bta_av_acp_sig_timer_cback;
1380                         bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1381                     }
1382                     break;
1383                 }
1384             }
1385         }
1386     }
1387 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1388     else if (event == BTA_AR_AVDT_CONN_EVT)
1389     {
1390         bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1391     }
1392 #endif
1393     else
1394     {
1395         /* disconnected. */
1396         p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1397         if(p_lcb && p_lcb->conn_msk)
1398         {
1399             APPL_TRACE_DEBUG1("conn_msk: 0x%x", p_lcb->conn_msk);
1400             /* clean up ssm  */
1401             for(xx=0; xx < BTA_AV_NUM_STRS; xx++)
1402             {
1403                 mask = 1 << (xx + 1);
1404                 if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
1405                     (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0))
1406                 {
1407                     bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1408                 }
1409             }
1410         }
1411     }
1412     APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
1413 }
1414 
1415 /*******************************************************************************
1416 **
1417 ** Function         bta_av_sig_timer
1418 **
1419 ** Description      process the signal channel timer. This timer is started
1420 **                  when the AVDTP signal channel is connected. If no profile
1421 **                  is connected, the timer goes off every BTA_AV_SIG_TIME_VAL
1422 **
1423 ** Returns          void
1424 **
1425 *******************************************************************************/
bta_av_sig_timer(tBTA_AV_DATA * p_data)1426 void bta_av_sig_timer(tBTA_AV_DATA *p_data)
1427 {
1428     tBTA_AV_CB   *p_cb = &bta_av_cb;
1429     int     xx;
1430     UINT8   mask;
1431     tBTA_AV_LCB *p_lcb = NULL;
1432     tBTA_AV_PEND pend;
1433 
1434     APPL_TRACE_DEBUG0("bta_av_sig_timer");
1435     for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
1436     {
1437         mask = 1 << xx;
1438         if(mask & p_cb->conn_lcb)
1439         {
1440             /* this entry is used. check if it is connected */
1441             p_lcb = &p_cb->lcb[xx];
1442             if(!p_lcb->conn_msk)
1443             {
1444                 bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
1445                 bdcpy(pend.bd_addr, p_lcb->addr);
1446                 (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
1447             }
1448         }
1449     }
1450 }
1451 
1452 /*******************************************************************************
1453 **
1454 ** Function         bta_av_acp_sig_timer_cback
1455 **
1456 ** Description      Process the timeout when SRC is accepting connection
1457 **                  and SNK did not start signalling.
1458 **
1459 ** Returns          void
1460 **
1461 *******************************************************************************/
bta_av_acp_sig_timer_cback(TIMER_LIST_ENT * p_tle)1462 static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
1463 {
1464     UINT8   inx = (UINT8)p_tle->param;
1465     tBTA_AV_CB  *p_cb = &bta_av_cb;
1466     tBTA_AV_SCB *p_scb = p_cb->p_scb[inx];
1467     tBTA_AV_API_OPEN  *p_buf;
1468 
1469     if (p_scb)
1470     {
1471         APPL_TRACE_DEBUG1("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
1472 
1473         if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
1474         {
1475             p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1476 
1477             if (bta_av_is_scb_opening(p_scb))
1478             {
1479                 if (p_scb->p_disc_db)
1480                 {
1481                     /* We are still doing SDP. Run the timer again. */
1482                     p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1483 
1484                     p_cb->acp_sig_tmr.param = (UINT32)inx;
1485                     p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
1486                     bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1487                 }
1488                 else
1489                 {
1490                     /* SNK did not start signalling, resume signalling process. */
1491                     bta_av_discover_req (p_scb, NULL);
1492                 }
1493             }
1494             else if (bta_av_is_scb_incoming(p_scb))
1495             {
1496                 /* Stay in incoming state if SNK does not start signalling */
1497 
1498                 /* API open was called right after SNK opened L2C connection. */
1499                 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
1500                 {
1501                     p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1502 
1503                     /* BTA_AV_API_OPEN_EVT */
1504                     if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
1505                     {
1506                         memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1507                         bta_sys_sendmsg(p_buf);
1508                     }
1509                 }
1510             }
1511         }
1512     }
1513 }
1514 
1515 /*******************************************************************************
1516 **
1517 ** Function         bta_av_check_peer_features
1518 **
1519 ** Description      checks
1520 **
1521 ** Returns          void
1522 **
1523 *******************************************************************************/
bta_av_check_peer_features(UINT16 service_uuid)1524 tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
1525 {
1526     tBTA_AV_FEAT peer_features = 0;
1527     tBTA_AV_CB   *p_cb = &bta_av_cb;
1528     tSDP_DISC_REC       *p_rec = NULL;
1529     tSDP_DISC_ATTR      *p_attr;
1530     UINT16              peer_rc_version=0;
1531     UINT16              categories = 0;
1532 
1533     APPL_TRACE_DEBUG1("bta_av_check_peer_features service_uuid:x%x", service_uuid);
1534     /* loop through all records we found */
1535     while (TRUE)
1536     {
1537         /* get next record; if none found, we're done */
1538         if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL)
1539         {
1540             break;
1541         }
1542 
1543         if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL)
1544         {
1545             /* find peer features */
1546             if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL))
1547             {
1548                 peer_features |= BTA_AV_FEAT_RCCT;
1549             }
1550             if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL))
1551             {
1552                 peer_features |= BTA_AV_FEAT_RCTG;
1553             }
1554         }
1555 
1556         if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL)
1557         {
1558             /* get profile version (if failure, version parameter is not updated) */
1559             SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1560             APPL_TRACE_DEBUG1("peer_rc_version 0x%x", peer_rc_version);
1561 
1562             if (peer_rc_version >= AVRC_REV_1_3)
1563                 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1564 
1565             if (peer_rc_version >= AVRC_REV_1_4)
1566             {
1567                 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1568                 /* get supported categories */
1569                 if ((p_attr = SDP_FindAttributeInRec(p_rec,
1570                                 ATTR_ID_SUPPORTED_FEATURES)) != NULL)
1571                 {
1572                     categories = p_attr->attr_value.v.u16;
1573                     if (categories & AVRC_SUPF_CT_BROWSE)
1574                         peer_features |= (BTA_AV_FEAT_BROWSE);
1575                 }
1576             }
1577         }
1578     }
1579     APPL_TRACE_DEBUG1("peer_features:x%x", peer_features);
1580     return peer_features;
1581 }
1582 
1583 /*******************************************************************************
1584 **
1585 ** Function         bta_av_rc_disc_done
1586 **
1587 ** Description      Handle AVRCP service discovery results.  If matching
1588 **                  service found, open AVRCP connection.
1589 **
1590 ** Returns          void
1591 **
1592 *******************************************************************************/
bta_av_rc_disc_done(tBTA_AV_DATA * p_data)1593 void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
1594 {
1595     tBTA_AV_CB   *p_cb = &bta_av_cb;
1596     tBTA_AV_SCB  *p_scb = NULL;
1597     tBTA_AV_LCB  *p_lcb;
1598     tBTA_AV_RC_OPEN rc_open;
1599     tBTA_AV_RC_FEAT rc_feat;
1600     UINT8               rc_handle;
1601     tBTA_AV_FEAT        peer_features;  /* peer features mask */
1602 
1603     APPL_TRACE_DEBUG1("bta_av_rc_disc_done disc:x%x", p_cb->disc);
1604     if (!p_cb->disc)
1605     {
1606         return;
1607     }
1608 
1609     if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
1610     {
1611         /* this is the rc handle/index to tBTA_AV_RCB */
1612         rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1613     }
1614     else
1615     {
1616         p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1617         if (p_scb)
1618             rc_handle = p_scb->rc_handle;
1619         else
1620         {
1621             p_cb->disc = 0;
1622             return;
1623         }
1624     }
1625 
1626     APPL_TRACE_DEBUG1("rc_handle %d", rc_handle);
1627     peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
1628     if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features&BTA_AV_FEAT_ADV_CTRL) == 0))
1629     {
1630         /* if we support advance control and peer does not, check their support on TG role
1631          * some implementation uses 1.3 on CT ans 1.4 on TG */
1632         peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1633     }
1634 
1635     p_cb->disc = 0;
1636     utl_freebuf((void **) &p_cb->p_disc_db);
1637 
1638     APPL_TRACE_DEBUG2("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
1639 
1640     /* if we have no rc connection */
1641     if (rc_handle == BTA_AV_RC_HANDLE_NONE)
1642     {
1643         if (p_scb)
1644         {
1645             /* if peer remote control service matches ours and USE_RC is TRUE */
1646             if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
1647                  ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) )
1648             {
1649                 p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
1650                 if(p_lcb)
1651                 {
1652                     rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
1653                     p_cb->rcb[rc_handle].peer_features = peer_features;
1654                 }
1655 #if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
1656                 else
1657                 {
1658                     APPL_TRACE_ERROR0("can not find LCB!!");
1659                 }
1660 #endif
1661             }
1662             else if(p_scb->use_rc)
1663             {
1664                 /* can not find AVRC on peer device. report failure */
1665                 p_scb->use_rc = FALSE;
1666                 bdcpy(rc_open.peer_addr, p_scb->peer_addr);
1667                 rc_open.peer_features = 0;
1668                 rc_open.status = BTA_AV_FAIL_SDP;
1669                 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
1670             }
1671         }
1672     }
1673     else
1674     {
1675         p_cb->rcb[rc_handle].peer_features = peer_features;
1676         rc_feat.rc_handle =  rc_handle;
1677         rc_feat.peer_features = peer_features;
1678         (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
1679     }
1680 }
1681 
1682 /*******************************************************************************
1683 **
1684 ** Function         bta_av_rc_closed
1685 **
1686 ** Description      Set AVRCP state to closed.
1687 **
1688 ** Returns          void
1689 **
1690 *******************************************************************************/
bta_av_rc_closed(tBTA_AV_DATA * p_data)1691 void bta_av_rc_closed(tBTA_AV_DATA *p_data)
1692 {
1693     tBTA_AV_CB   *p_cb = &bta_av_cb;
1694     tBTA_AV_RC_CLOSE rc_close;
1695     tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
1696     tBTA_AV_RCB    *p_rcb;
1697     tBTA_AV_SCB    *p_scb;
1698     int i;
1699     BOOLEAN conn = FALSE;
1700     tBTA_AV_LCB *p_lcb;
1701 
1702     rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
1703     APPL_TRACE_DEBUG1("bta_av_rc_closed rc_handle:%d", p_msg->handle);
1704     for(i=0; i<BTA_AV_NUM_RCB; i++)
1705     {
1706         p_rcb = &p_cb->rcb[i];
1707         APPL_TRACE_DEBUG3("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
1708         if(p_rcb->handle == p_msg->handle)
1709         {
1710             rc_close.rc_handle = i;
1711             p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
1712             p_rcb->peer_features = 0;
1713             APPL_TRACE_DEBUG2("       shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
1714             if(p_rcb->shdl)
1715             {
1716                 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1717                 if(p_scb)
1718                 {
1719                     bdcpy(rc_close.peer_addr, p_scb->peer_addr);
1720                     if(p_scb->rc_handle == p_rcb->handle)
1721                         p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
1722                     APPL_TRACE_DEBUG2("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
1723                 }
1724                 p_rcb->shdl = 0;
1725             }
1726             else if(p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) )
1727             {
1728                 /* if the RCB uses the extra LCB, use the addr for event and clean it */
1729                 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
1730                 bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1731                 APPL_TRACE_DEBUG6("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
1732                               p_msg->peer_addr[0], p_msg->peer_addr[1],
1733                               p_msg->peer_addr[2], p_msg->peer_addr[3],
1734                               p_msg->peer_addr[4], p_msg->peer_addr[5]);
1735                 p_lcb->conn_msk = 0;
1736                 p_lcb->lidx = 0;
1737             }
1738             p_rcb->lidx = 0;
1739 
1740             if((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)
1741             {
1742                 /* AVCT CCB is deallocated */
1743                 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
1744                 p_rcb->status = 0;
1745             }
1746             else
1747             {
1748                 /* AVCT CCB is still there. dealloc */
1749                 bta_av_del_rc(p_rcb);
1750 
1751                 /* if the AVRCP is no longer listening, create the listening channel */
1752                 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
1753                     bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1754             }
1755         }
1756         else if((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK))
1757         {
1758             /* at least one channel is still connected */
1759             conn = TRUE;
1760         }
1761     }
1762 
1763     if(!conn)
1764     {
1765         /* no AVRC channels are connected, go back to INIT state */
1766         bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
1767     }
1768 
1769     if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE)
1770     {
1771         rc_close.rc_handle = p_msg->handle;
1772         bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1773     }
1774     (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
1775 }
1776 
1777 /*******************************************************************************
1778 **
1779 ** Function         bta_av_rc_disc
1780 **
1781 ** Description      start AVRC SDP discovery.
1782 **
1783 ** Returns          void
1784 **
1785 *******************************************************************************/
bta_av_rc_disc(UINT8 disc)1786 void bta_av_rc_disc(UINT8 disc)
1787 {
1788     tBTA_AV_CB   *p_cb = &bta_av_cb;
1789     tAVRC_SDP_DB_PARAMS db_params;
1790       UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
1791                                        ATTR_ID_BT_PROFILE_DESC_LIST,
1792                                        ATTR_ID_SUPPORTED_FEATURES};
1793     UINT8       hdi;
1794     tBTA_AV_SCB *p_scb;
1795     UINT8       *p_addr = NULL;
1796     UINT8       rc_handle;
1797 
1798     APPL_TRACE_DEBUG2("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
1799     if ((bta_av_cb.disc != 0) || (disc == 0))
1800         return;
1801 
1802     if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
1803     {
1804         /* this is the rc handle/index to tBTA_AV_RCB */
1805         rc_handle = disc & (~BTA_AV_CHNL_MSK);
1806         if (p_cb->rcb[rc_handle].lidx)
1807         {
1808             p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx-1].addr;
1809         }
1810     }
1811     else
1812     {
1813         hdi = (disc & BTA_AV_HNDL_MSK) - 1;
1814         p_scb = p_cb->p_scb[hdi];
1815 
1816         if (p_scb)
1817         {
1818             APPL_TRACE_DEBUG1("rc_handle %d", p_scb->rc_handle);
1819             p_addr = p_scb->peer_addr;
1820         }
1821     }
1822 
1823     if (p_addr)
1824     {
1825         /* allocate discovery database */
1826         if (p_cb->p_disc_db == NULL)
1827         {
1828             p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
1829         }
1830 
1831         if (p_cb->p_disc_db)
1832         {
1833             /* set up parameters */
1834             db_params.db_len = BTA_AV_DISC_BUF_SIZE;
1835             db_params.num_attr = 3;
1836             db_params.p_db = p_cb->p_disc_db;
1837             db_params.p_attrs = attr_list;
1838 
1839             /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
1840             if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
1841                             bta_av_avrc_sdp_cback) == AVRC_SUCCESS)
1842             {
1843                 p_cb->disc = disc;
1844                 APPL_TRACE_DEBUG1("disc %d", p_cb->disc);
1845             }
1846         }
1847     }
1848 }
1849 
1850 /*******************************************************************************
1851 **
1852 ** Function         bta_av_dereg_comp
1853 **
1854 ** Description      deregister complete. free the stream control block.
1855 **
1856 ** Returns          void
1857 **
1858 *******************************************************************************/
bta_av_dereg_comp(tBTA_AV_DATA * p_data)1859 void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
1860 {
1861     tBTA_AV_CB   *p_cb = &bta_av_cb;
1862     tBTA_AV_SCB  *p_scb;
1863     tBTA_UTL_COD    cod;
1864     UINT8   mask;
1865     BT_HDR  *p_buf;
1866 
1867     /* find the stream control block */
1868     p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
1869 
1870     if(p_scb)
1871     {
1872         APPL_TRACE_DEBUG2("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
1873         mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1874         if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
1875         {
1876             p_cb->reg_audio  &= ~mask;
1877             if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
1878             {
1879                 /* this channel is still marked as open. decrease the count */
1880                 bta_av_cb.audio_open_cnt--;
1881             }
1882             p_cb->conn_audio &= ~mask;
1883 
1884             if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM)
1885             {
1886             /* make sure no buffers are in q_info.a2d */
1887             while((p_buf = (BT_HDR*)GKI_dequeue (&p_scb->q_info.a2d)) != NULL)
1888                 GKI_freebuf(p_buf);
1889             }
1890 
1891             /* remove the A2DP SDP record, if no more audio stream is left */
1892             if(!p_cb->reg_audio)
1893             {
1894 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1895                 bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
1896 #endif
1897                 bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
1898                 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
1899             }
1900         }
1901         else
1902         {
1903             p_cb->reg_video  &= ~mask;
1904             /* make sure that this channel is not connected */
1905             p_cb->conn_video &= ~mask;
1906             /* remove the VDP SDP record, (only one video stream at most) */
1907             bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
1908             bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
1909         }
1910 
1911         /* make sure that the timer is not active */
1912         bta_sys_stop_timer(&p_scb->timer);
1913         utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
1914     }
1915 
1916     APPL_TRACE_DEBUG3("audio 0x%x, video: 0x%x, disable:%d",
1917         p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
1918     /* if no stream control block is active */
1919     if((p_cb->reg_audio + p_cb->reg_video) == 0)
1920     {
1921 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1922         /* deregister from AVDT */
1923         bta_ar_dereg_avdt(BTA_ID_AV);
1924 
1925         /* deregister from AVCT */
1926         bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
1927         bta_ar_dereg_avct(BTA_ID_AV);
1928 #endif
1929 
1930         if(p_cb->disabling)
1931         {
1932             p_cb->disabling     = FALSE;
1933             bta_av_cb.features  = 0;
1934         }
1935 
1936         /* Clear the Capturing service class bit */
1937         cod.service = BTM_COD_SERVICE_CAPTURING;
1938         utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
1939     }
1940 }
1941 #endif /* BTA_AV_INCLUDED */
1942