]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/bta/av/bta_av_act.c
02d420c31d400f2e0740d62e6fad55fbcb365131
[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
1273 /*******************************************************************************
1274 **
1275 ** Function         bta_av_api_disconnect
1276 **
1277 ** Description      .
1278 **
1279 ** Returns          void
1280 **
1281 *******************************************************************************/
1282 void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
1283 {
1284     AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
1285     bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1286 }
1287
1288 /*******************************************************************************
1289 **
1290 ** Function         bta_av_sig_chg
1291 **
1292 ** Description      process AVDT signal channel up/down.
1293 **
1294 ** Returns          void
1295 **
1296 *******************************************************************************/
1297 void bta_av_sig_chg(tBTA_AV_DATA *p_data)
1298 {
1299     UINT16 event = p_data->str_msg.hdr.layer_specific;
1300     tBTA_AV_CB   *p_cb = &bta_av_cb;
1301     int     xx;
1302     UINT8   mask;
1303     tBTA_AV_LCB *p_lcb = NULL;
1304
1305     APPL_TRACE_DEBUG("bta_av_sig_chg event: %d", event);
1306     if (event == AVDT_CONNECT_IND_EVT) {
1307         p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1308         if (!p_lcb) {
1309             /* if the address does not have an LCB yet, alloc one */
1310             for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
1311                 mask = 1 << xx;
1312                 APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
1313
1314                 /* look for a p_lcb with its p_scb registered */
1315                 if ((!(mask & p_cb->conn_lcb)) && (p_cb->p_scb[xx] != NULL)) {
1316                     p_lcb = &p_cb->lcb[xx];
1317                     p_lcb->lidx = xx + 1;
1318                     bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
1319                     p_lcb->conn_msk = 0; /* clear the connect mask */
1320                     /* start listening when the signal channel is open */
1321                     if (p_cb->features & BTA_AV_FEAT_RCTG) {
1322                         bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1323                     }
1324                     /* this entry is not used yet. */
1325                     p_cb->conn_lcb |= mask;     /* mark it as used */
1326                     APPL_TRACE_DEBUG("start sig timer %d", p_data->hdr.offset);
1327                     if (p_data->hdr.offset == AVDT_ACP) {
1328                         APPL_TRACE_DEBUG("Incoming L2CAP acquired, set state as incoming");
1329                         bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
1330                         p_cb->p_scb[xx]->use_rc = TRUE;     /* allowing RC for incoming connection */
1331                         bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
1332
1333                         /* The Pending Event should be sent as soon as the L2CAP signalling channel
1334                          * is set up, which is NOW. Earlier this was done only after
1335                          * BTA_AV_SIG_TIME_VAL milliseconds.
1336                          * The following function shall send the event and start the recurring timer
1337                          */
1338                         bta_av_sig_timer(NULL);
1339
1340                         /* Possible collision : need to avoid outgoing processing while the timer is running */
1341                         p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
1342
1343                         p_cb->acp_sig_tmr.param = (UINT32)xx;
1344                         p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
1345                         bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1346                     }
1347                     break;
1348                 }
1349             }
1350
1351             /* check if we found something */
1352             if (xx == BTA_AV_NUM_LINKS) {
1353                 /* We do not have scb for this avdt connection.     */
1354                 /* Silently close the connection.                   */
1355                 APPL_TRACE_ERROR("av scb not available for avdt connection");
1356                 AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
1357                 return;
1358             }
1359         }
1360     }
1361 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1362     else if (event == BTA_AR_AVDT_CONN_EVT) {
1363         bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1364     }
1365 #endif
1366     else {
1367         /* disconnected. */
1368         p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1369         if (p_lcb && p_lcb->conn_msk) {
1370             APPL_TRACE_DEBUG("conn_msk: 0x%x", p_lcb->conn_msk);
1371             /* clean up ssm  */
1372             for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
1373                 mask = 1 << (xx + 1);
1374                 if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
1375                         (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0)) {
1376                     p_cb->p_scb[xx]->disc_rsn = p_data->str_msg.hdr.offset;
1377                     bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1378                 }
1379             }
1380         }
1381     }
1382     APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
1383 }
1384
1385 /*******************************************************************************
1386 **
1387 ** Function         bta_av_sig_timer
1388 **
1389 ** Description      process the signal channel timer. This timer is started
1390 **                  when the AVDTP signal channel is connected. If no profile
1391 **                  is connected, the timer goes off every BTA_AV_SIG_TIME_VAL
1392 **
1393 ** Returns          void
1394 **
1395 *******************************************************************************/
1396 void bta_av_sig_timer(tBTA_AV_DATA *p_data)
1397 {
1398     tBTA_AV_CB   *p_cb = &bta_av_cb;
1399     int     xx;
1400     UINT8   mask;
1401     tBTA_AV_LCB *p_lcb = NULL;
1402     tBTA_AV_PEND pend;
1403     UNUSED(p_data);
1404
1405     APPL_TRACE_DEBUG("bta_av_sig_timer");
1406     for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
1407         mask = 1 << xx;
1408         if (mask & p_cb->conn_lcb) {
1409             /* this entry is used. check if it is connected */
1410             p_lcb = &p_cb->lcb[xx];
1411             if (!p_lcb->conn_msk) {
1412                 bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
1413                 bdcpy(pend.bd_addr, p_lcb->addr);
1414                 (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
1415             }
1416         }
1417     }
1418 }
1419
1420 /*******************************************************************************
1421 **
1422 ** Function         bta_av_acp_sig_timer_cback
1423 **
1424 ** Description      Process the timeout when SRC is accepting connection
1425 **                  and SNK did not start signalling.
1426 **
1427 ** Returns          void
1428 **
1429 *******************************************************************************/
1430 static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
1431 {
1432     UINT8   inx = (UINT8)p_tle->param;
1433     tBTA_AV_CB  *p_cb = &bta_av_cb;
1434     tBTA_AV_SCB *p_scb = NULL;
1435     tBTA_AV_API_OPEN  *p_buf;
1436     if (inx < BTA_AV_NUM_STRS) {
1437         p_scb = p_cb->p_scb[inx];
1438     }
1439     if (p_scb) {
1440         APPL_TRACE_DEBUG("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
1441
1442         if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1443             p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1444
1445             if (bta_av_is_scb_opening(p_scb)) {
1446                 if (p_scb->p_disc_db) {
1447                     /* We are still doing SDP. Run the timer again. */
1448                     p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1449
1450                     p_cb->acp_sig_tmr.param = (UINT32)inx;
1451                     p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
1452                     bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1453                 } else {
1454                     /* SNK did not start signalling, resume signalling process. */
1455                     bta_av_discover_req (p_scb, NULL);
1456                 }
1457             } else if (bta_av_is_scb_incoming(p_scb)) {
1458                 /* Stay in incoming state if SNK does not start signalling */
1459
1460                 /* API open was called right after SNK opened L2C connection. */
1461                 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED) {
1462                     p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1463
1464                     /* BTA_AV_API_OPEN_EVT */
1465                     if ((p_buf = (tBTA_AV_API_OPEN *) osi_malloc(sizeof(tBTA_AV_API_OPEN))) != NULL) {
1466                         memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1467                         bta_sys_sendmsg(p_buf);
1468                     }
1469                 }
1470             }
1471         }
1472     }
1473 }
1474
1475 /*******************************************************************************
1476 **
1477 ** Function         bta_av_check_peer_features
1478 **
1479 ** Description      check supported features on the peer device from the SDP record
1480 **                  and return the feature mask
1481 **
1482 ** Returns          tBTA_AV_FEAT peer device feature mask
1483 **
1484 *******************************************************************************/
1485 tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
1486 {
1487     tBTA_AV_FEAT peer_features = 0;
1488     tBTA_AV_CB   *p_cb = &bta_av_cb;
1489     tSDP_DISC_REC       *p_rec = NULL;
1490     tSDP_DISC_ATTR      *p_attr;
1491     UINT16              peer_rc_version = 0;
1492     UINT16              categories = 0;
1493
1494     APPL_TRACE_DEBUG("bta_av_check_peer_features service_uuid:x%x", service_uuid);
1495     /* loop through all records we found */
1496     while (TRUE) {
1497         /* get next record; if none found, we're done */
1498         if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL) {
1499             break;
1500         }
1501
1502         if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL) {
1503             /* find peer features */
1504             if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL)) {
1505                 peer_features |= BTA_AV_FEAT_RCCT;
1506             }
1507             if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1508                 peer_features |= BTA_AV_FEAT_RCTG;
1509             }
1510         }
1511
1512         if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1513             /* get profile version (if failure, version parameter is not updated) */
1514             SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1515             APPL_TRACE_DEBUG("peer_rc_version 0x%x", peer_rc_version);
1516
1517             if (peer_rc_version >= AVRC_REV_1_3) {
1518                 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1519             }
1520
1521             if (peer_rc_version >= AVRC_REV_1_4) {
1522                 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1523                 /* get supported categories */
1524                 if ((p_attr = SDP_FindAttributeInRec(p_rec,
1525                                                      ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
1526                     categories = p_attr->attr_value.v.u16;
1527                     if (categories & AVRC_SUPF_CT_BROWSE) {
1528                         peer_features |= (BTA_AV_FEAT_BROWSE);
1529                     }
1530                 }
1531             }
1532         }
1533     }
1534     APPL_TRACE_DEBUG("peer_features:x%x", peer_features);
1535     return peer_features;
1536 }
1537
1538 /*******************************************************************************
1539 **
1540 ** Function         bta_av_rc_disc_done
1541 **
1542 ** Description      Handle AVRCP service discovery results.  If matching
1543 **                  service found, open AVRCP connection.
1544 **
1545 ** Returns          void
1546 **
1547 *******************************************************************************/
1548 void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
1549 {
1550     tBTA_AV_CB   *p_cb = &bta_av_cb;
1551     tBTA_AV_SCB  *p_scb = NULL;
1552     tBTA_AV_LCB  *p_lcb;
1553     tBTA_AV_RC_OPEN rc_open;
1554     tBTA_AV_RC_FEAT rc_feat;
1555     UINT8               rc_handle;
1556     tBTA_AV_FEAT        peer_features;  /* peer features mask */
1557     UNUSED(p_data);
1558
1559     APPL_TRACE_DEBUG("bta_av_rc_disc_done disc:x%x", p_cb->disc);
1560     if (!p_cb->disc) {
1561         return;
1562     }
1563
1564     if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
1565         /* this is the rc handle/index to tBTA_AV_RCB */
1566         rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1567     } else {
1568         /* Validate array index*/
1569         if (((p_cb->disc & BTA_AV_HNDL_MSK) - 1) < BTA_AV_NUM_STRS) {
1570             p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1571         }
1572         if (p_scb) {
1573             rc_handle = p_scb->rc_handle;
1574         } else {
1575             p_cb->disc = 0;
1576             return;
1577         }
1578     }
1579
1580     APPL_TRACE_DEBUG("rc_handle %d", rc_handle);
1581     /* check peer version and whether support CT and TG role */
1582     peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
1583     if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features & BTA_AV_FEAT_ADV_CTRL) == 0)) {
1584         /* if we support advance control and peer does not, check their support on TG role
1585          * some implementation uses 1.3 on CT ans 1.4 on TG */
1586         peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1587     }
1588
1589     p_cb->disc = 0;
1590     utl_freebuf((void **) &p_cb->p_disc_db);
1591
1592     APPL_TRACE_DEBUG("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
1593
1594     /* if we have no rc connection */
1595     if (rc_handle == BTA_AV_RC_HANDLE_NONE) {
1596         if (p_scb) {
1597             /* if peer remote control service matches ours and USE_RC is TRUE */
1598             if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
1599                     ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) ) {
1600                 p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
1601                 if (p_lcb) {
1602                     rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
1603                     p_cb->rcb[rc_handle].peer_features = peer_features;
1604                 }
1605 #if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
1606                 else {
1607                     APPL_TRACE_ERROR("can not find LCB!!");
1608                 }
1609 #endif
1610             } else if (p_scb->use_rc) {
1611                 /* can not find AVRC on peer device. report failure */
1612                 p_scb->use_rc = FALSE;
1613                 bdcpy(rc_open.peer_addr, p_scb->peer_addr);
1614                 rc_open.peer_features = 0;
1615                 rc_open.sdp_disc_done = FALSE;
1616                 rc_open.status = BTA_AV_FAIL_SDP;
1617                 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
1618             }
1619         }
1620     } else {
1621         p_cb->rcb[rc_handle].peer_features = peer_features;
1622         rc_feat.rc_handle =  rc_handle;
1623         rc_feat.peer_features = peer_features;
1624         (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
1625     }
1626 }
1627
1628 /*******************************************************************************
1629 **
1630 ** Function         bta_av_rc_closed
1631 **
1632 ** Description      Set AVRCP state to closed.
1633 **
1634 ** Returns          void
1635 **
1636 *******************************************************************************/
1637 void bta_av_rc_closed(tBTA_AV_DATA *p_data)
1638 {
1639     tBTA_AV_CB   *p_cb = &bta_av_cb;
1640     tBTA_AV_RC_CLOSE rc_close;
1641     tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
1642     tBTA_AV_RCB    *p_rcb;
1643     tBTA_AV_SCB    *p_scb;
1644     int i;
1645     BOOLEAN conn = FALSE;
1646     tBTA_AV_LCB *p_lcb;
1647
1648     rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
1649     p_scb = NULL;
1650     APPL_TRACE_DEBUG("bta_av_rc_closed rc_handle:%d", p_msg->handle);
1651     for (i = 0; i < BTA_AV_NUM_RCB; i++) {
1652         p_rcb = &p_cb->rcb[i];
1653         APPL_TRACE_DEBUG("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
1654         if (p_rcb->handle == p_msg->handle) {
1655             rc_close.rc_handle = i;
1656             p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
1657             p_rcb->peer_features = 0;
1658             APPL_TRACE_DEBUG("       shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
1659             if (p_rcb->shdl) {
1660                 if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS) {
1661                     p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1662                 }
1663                 if (p_scb) {
1664                     bdcpy(rc_close.peer_addr, p_scb->peer_addr);
1665                     if (p_scb->rc_handle == p_rcb->handle) {
1666                         p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
1667                     }
1668                     APPL_TRACE_DEBUG("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
1669                 }
1670                 p_rcb->shdl = 0;
1671             } else if (p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) ) {
1672                 /* if the RCB uses the extra LCB, use the addr for event and clean it */
1673                 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
1674                 bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1675                 APPL_TRACE_DEBUG("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
1676                                  p_msg->peer_addr[0], p_msg->peer_addr[1],
1677                                  p_msg->peer_addr[2], p_msg->peer_addr[3],
1678                                  p_msg->peer_addr[4], p_msg->peer_addr[5]);
1679                 p_lcb->conn_msk = 0;
1680                 p_lcb->lidx = 0;
1681             }
1682             p_rcb->lidx = 0;
1683
1684             if ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) {
1685                 /* AVCT CCB is deallocated */
1686                 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
1687                 p_rcb->status = 0;
1688             } else {
1689                 /* AVCT CCB is still there. dealloc */
1690                 bta_av_del_rc(p_rcb);
1691
1692                 /* if the AVRCP is no longer listening, create the listening channel */
1693                 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG) {
1694                     bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1695                 }
1696             }
1697         } else if ((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK)) {
1698             /* at least one channel is still connected */
1699             conn = TRUE;
1700         }
1701     }
1702
1703     if (!conn) {
1704         /* no AVRC channels are connected, go back to INIT state */
1705         bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
1706     }
1707
1708     if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE) {
1709         rc_close.rc_handle = p_msg->handle;
1710         bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1711     }
1712     (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
1713 }
1714
1715 /*******************************************************************************
1716 **
1717 ** Function         bta_av_rc_disc
1718 **
1719 ** Description      start AVRC SDP discovery.
1720 **
1721 ** Returns          void
1722 **
1723 *******************************************************************************/
1724 void bta_av_rc_disc(UINT8 disc)
1725 {
1726     tBTA_AV_CB   *p_cb = &bta_av_cb;
1727     tAVRC_SDP_DB_PARAMS db_params;
1728     UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
1729                                        ATTR_ID_BT_PROFILE_DESC_LIST,
1730                                        ATTR_ID_SUPPORTED_FEATURES
1731                                       };
1732     UINT8       hdi;
1733     tBTA_AV_SCB *p_scb;
1734     UINT8       *p_addr = NULL;
1735     UINT8       rc_handle;
1736
1737     APPL_TRACE_DEBUG("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
1738     if ((bta_av_cb.disc != 0) || (disc == 0)) {
1739         return;
1740     }
1741
1742     if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
1743         /* this is the rc handle/index to tBTA_AV_RCB */
1744         rc_handle = disc & (~BTA_AV_CHNL_MSK);
1745         if (p_cb->rcb[rc_handle].lidx) {
1746             p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
1747         }
1748     } else {
1749         hdi = (disc & BTA_AV_HNDL_MSK) - 1;
1750         p_scb = p_cb->p_scb[hdi];
1751
1752         if (p_scb) {
1753             APPL_TRACE_DEBUG("rc_handle %d", p_scb->rc_handle);
1754             p_addr = p_scb->peer_addr;
1755         }
1756     }
1757
1758     if (p_addr) {
1759         /* allocate discovery database */
1760         if (p_cb->p_disc_db == NULL) {
1761             p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) osi_malloc(BTA_AV_DISC_BUF_SIZE);
1762         }
1763
1764         if (p_cb->p_disc_db) {
1765             /* set up parameters */
1766             db_params.db_len = BTA_AV_DISC_BUF_SIZE;
1767             db_params.num_attr = 3;
1768             db_params.p_db = p_cb->p_disc_db;
1769             db_params.p_attrs = attr_list;
1770
1771             /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
1772             if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
1773                                  bta_av_avrc_sdp_cback) == AVRC_SUCCESS) {
1774                 p_cb->disc = disc;
1775                 APPL_TRACE_DEBUG("disc %d", p_cb->disc);
1776             }
1777         }
1778     }
1779 }
1780
1781 /*******************************************************************************
1782 **
1783 ** Function         bta_av_dereg_comp
1784 **
1785 ** Description      deregister complete. free the stream control block.
1786 **
1787 ** Returns          void
1788 **
1789 *******************************************************************************/
1790 void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
1791 {
1792     tBTA_AV_CB   *p_cb = &bta_av_cb;
1793     tBTA_AV_SCB  *p_scb;
1794     tBTA_UTL_COD    cod;
1795     UINT8   mask;
1796     BT_HDR  *p_buf;
1797
1798     /* find the stream control block */
1799     p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
1800
1801     if (p_scb) {
1802         APPL_TRACE_DEBUG("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
1803         mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1804         if (p_scb->chnl == BTA_AV_CHNL_AUDIO) {
1805             p_cb->reg_audio  &= ~mask;
1806             if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt) {
1807                 /* this channel is still marked as open. decrease the count */
1808                 bta_av_cb.audio_open_cnt--;
1809             }
1810             p_cb->conn_audio &= ~mask;
1811
1812             if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM && p_scb->a2d_list) {
1813                 /* make sure no buffers are in a2d_list */
1814                 while (!list_is_empty(p_scb->a2d_list)) {
1815                     p_buf = (BT_HDR *)list_front(p_scb->a2d_list);
1816                     list_remove(p_scb->a2d_list, p_buf);
1817                     osi_free(p_buf);
1818                 }
1819             }
1820
1821             /* remove the A2DP SDP record, if no more audio stream is left */
1822             if (!p_cb->reg_audio) {
1823 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1824                 bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
1825 #endif
1826                 bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
1827                 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
1828
1829 #if (BTA_AV_SINK_INCLUDED == TRUE)
1830                 bta_av_del_sdp_rec(&p_cb->sdp_a2d_snk_handle);
1831                 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
1832 #endif
1833             }
1834         } else {
1835             p_cb->reg_video  &= ~mask;
1836             /* make sure that this channel is not connected */
1837             p_cb->conn_video &= ~mask;
1838             /* remove the VDP SDP record, (only one video stream at most) */
1839             bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
1840             bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
1841         }
1842
1843         /* make sure that the timer is not active */
1844         bta_sys_stop_timer(&p_scb->timer);
1845         utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
1846     }
1847
1848     APPL_TRACE_DEBUG("audio 0x%x, video: 0x%x, disable:%d",
1849                      p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
1850     /* if no stream control block is active */
1851     if ((p_cb->reg_audio + p_cb->reg_video) == 0) {
1852 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1853         /* deregister from AVDT */
1854         bta_ar_dereg_avdt(BTA_ID_AV);
1855
1856         /* deregister from AVCT */
1857         bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
1858         bta_ar_dereg_avct(BTA_ID_AV);
1859 #endif
1860
1861         if (p_cb->disabling) {
1862             p_cb->disabling     = FALSE;
1863             bta_av_cb.features  = 0;
1864         }
1865
1866         /* Clear the Capturing service class bit */
1867         cod.service = BTM_COD_SERVICE_CAPTURING;
1868         utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
1869     }
1870 }
1871 #endif /* BTA_AV_INCLUDED */