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