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