OSDN Git Service

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