OSDN Git Service

Replace BT_HDR => BT_HDR_RIGID
[android-x86/system-bt.git] / bta / av / bta_av_act.cc
1 /******************************************************************************
2  *
3  *  Copyright 2004-2016 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 #define LOG_TAG "bt_bta_av"
27
28 #include <cstdint>
29
30 #include "bt_target.h"  // Must be first to define build configuration
31
32 #include "bta/av/bta_av_int.h"
33 #include "bta/include/bta_ar_api.h"
34 #include "bta/include/utl.h"
35 #include "btif/avrcp/avrcp_service.h"
36 #include "osi/include/allocator.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h"  // UNUSED_ATTR
39 #include "osi/include/properties.h"
40 #include "stack/include/acl_api.h"
41 #include "stack/include/l2c_api.h"
42 #include "types/raw_address.h"
43
44 /*****************************************************************************
45  *  Constants
46  ****************************************************************************/
47 /* the timeout to wait for open req after setconfig for incoming connections */
48 #ifndef BTA_AV_SIGNALLING_TIMEOUT_MS
49 #define BTA_AV_SIGNALLING_TIMEOUT_MS (8 * 1000) /* 8 seconds */
50 #endif
51
52 /* Time to wait for signalling from SNK when it is initiated from SNK. */
53 /* If not, we will start signalling from SRC. */
54 #ifndef BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS
55 #define BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS (2 * 1000) /* 2 seconds */
56 #endif
57
58 static void bta_av_accept_signalling_timer_cback(void* data);
59
60 #ifndef AVRC_MIN_META_CMD_LEN
61 #define AVRC_MIN_META_CMD_LEN 20
62 #endif
63
64 /*******************************************************************************
65  *
66  * Function         bta_av_get_rcb_by_shdl
67  *
68  * Description      find the RCB associated with the given SCB handle.
69  *
70  * Returns          tBTA_AV_RCB
71  *
72  ******************************************************************************/
73 tBTA_AV_RCB* bta_av_get_rcb_by_shdl(uint8_t shdl) {
74   tBTA_AV_RCB* p_rcb = NULL;
75   int i;
76
77   for (i = 0; i < BTA_AV_NUM_RCB; i++) {
78     if (bta_av_cb.rcb[i].shdl == shdl &&
79         bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE) {
80       p_rcb = &bta_av_cb.rcb[i];
81       break;
82     }
83   }
84   return p_rcb;
85 }
86 #define BTA_AV_STS_NO_RSP 0xFF /* a number not used by tAVRC_STS */
87
88 /*******************************************************************************
89  *
90  * Function         bta_av_del_rc
91  *
92  * Description      delete the given AVRC handle.
93  *
94  * Returns          void
95  *
96  ******************************************************************************/
97 void bta_av_del_rc(tBTA_AV_RCB* p_rcb) {
98   tBTA_AV_SCB* p_scb;
99   uint8_t rc_handle; /* connected AVRCP handle */
100
101   p_scb = NULL;
102   if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
103     if (p_rcb->shdl) {
104       /* Validate array index*/
105       if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS) {
106         p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
107       }
108       if (p_scb) {
109         APPL_TRACE_DEBUG("%s: shdl:%d, srch:%d rc_handle:%d", __func__,
110                          p_rcb->shdl, p_scb->rc_handle, p_rcb->handle);
111         if (p_scb->rc_handle == p_rcb->handle)
112           p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
113         /* just in case the RC timer is active
114         if (bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl ==
115         BTA_AV_CHNL_AUDIO) */
116         alarm_cancel(p_scb->avrc_ct_timer);
117       }
118     }
119
120     APPL_TRACE_EVENT("%s: handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
121                      __func__, p_rcb->handle, p_rcb->status,
122                      bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
123     rc_handle = p_rcb->handle;
124     if (!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
125         ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)) {
126       p_rcb->status = 0;
127       p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
128       p_rcb->shdl = 0;
129       p_rcb->lidx = 0;
130     }
131     /* else ACP && connected. do not clear the handle yet */
132     AVRC_Close(rc_handle);
133     if (rc_handle == bta_av_cb.rc_acp_handle)
134       bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
135     APPL_TRACE_EVENT(
136         "%s: end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
137         __func__, p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle,
138         p_rcb->lidx);
139   }
140 }
141
142 /*******************************************************************************
143  *
144  * Function         bta_av_close_all_rc
145  *
146  * Description      close the all AVRC handle.
147  *
148  * Returns          void
149  *
150  ******************************************************************************/
151 static void bta_av_close_all_rc(tBTA_AV_CB* p_cb) {
152   int i;
153
154   for (i = 0; i < BTA_AV_NUM_RCB; i++) {
155     if ((p_cb->disabling) || (bta_av_cb.rcb[i].shdl != 0))
156       bta_av_del_rc(&bta_av_cb.rcb[i]);
157   }
158 }
159
160 /*******************************************************************************
161  *
162  * Function         bta_av_del_sdp_rec
163  *
164  * Description      delete the given SDP record handle.
165  *
166  * Returns          void
167  *
168  ******************************************************************************/
169 static void bta_av_del_sdp_rec(uint32_t* p_sdp_handle) {
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(UNUSED_ATTR uint16_t status) {
186   BT_HDR_RIGID* p_msg = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
187
188   p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
189
190   bta_sys_sendmsg(p_msg);
191 }
192
193 /*******************************************************************************
194  *
195  * Function         bta_av_rc_ctrl_cback
196  *
197  * Description      AVRCP control callback.
198  *
199  * Returns          void
200  *
201  ******************************************************************************/
202 static void bta_av_rc_ctrl_cback(uint8_t handle, uint8_t event,
203                                  UNUSED_ATTR uint16_t result,
204                                  const RawAddress* peer_addr) {
205   uint16_t msg_event = 0;
206
207   APPL_TRACE_EVENT("%s: handle: %d event=0x%x", __func__, handle, event);
208   if (event == AVRC_OPEN_IND_EVT) {
209     /* save handle of opened connection
210     bta_av_cb.rc_handle = handle;*/
211
212     msg_event = BTA_AV_AVRC_OPEN_EVT;
213   } else if (event == AVRC_CLOSE_IND_EVT) {
214     msg_event = BTA_AV_AVRC_CLOSE_EVT;
215   } else if (event == AVRC_BROWSE_OPEN_IND_EVT) {
216     msg_event = BTA_AV_AVRC_BROWSE_OPEN_EVT;
217   } else if (event == AVRC_BROWSE_CLOSE_IND_EVT) {
218     msg_event = BTA_AV_AVRC_BROWSE_CLOSE_EVT;
219   }
220
221   if (msg_event) {
222     tBTA_AV_RC_CONN_CHG* p_msg =
223         (tBTA_AV_RC_CONN_CHG*)osi_malloc(sizeof(tBTA_AV_RC_CONN_CHG));
224     p_msg->hdr.event = msg_event;
225     p_msg->handle = handle;
226     if (peer_addr) p_msg->peer_addr = *peer_addr;
227     bta_sys_sendmsg(p_msg);
228   }
229 }
230
231 /*******************************************************************************
232  *
233  * Function         bta_av_rc_msg_cback
234  *
235  * Description      AVRCP message callback.
236  *
237  * Returns          void
238  *
239  ******************************************************************************/
240 static void bta_av_rc_msg_cback(uint8_t handle, uint8_t label, uint8_t opcode,
241                                 tAVRC_MSG* p_msg) {
242   uint8_t* p_data_src = NULL;
243   uint16_t data_len = 0;
244
245   APPL_TRACE_DEBUG("%s: handle: %u opcode=0x%x", __func__, handle, opcode);
246
247   /* Copy avrc packet into BTA message buffer (for sending to BTA state machine)
248    */
249
250   /* Get size of payload data  (for vendor and passthrough messages only; for
251    * browsing
252    * messages, use zero-copy) */
253   if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL) {
254     p_data_src = p_msg->vendor.p_vendor_data;
255     data_len = (uint16_t)p_msg->vendor.vendor_len;
256   } else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL) {
257     p_data_src = p_msg->pass.p_pass_data;
258     data_len = (uint16_t)p_msg->pass.pass_len;
259   }
260
261   /* Create a copy of the message */
262   tBTA_AV_RC_MSG* p_buf =
263       (tBTA_AV_RC_MSG*)osi_malloc(sizeof(tBTA_AV_RC_MSG) + data_len);
264
265   p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
266   p_buf->handle = handle;
267   p_buf->label = label;
268   p_buf->opcode = opcode;
269   memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
270   /* Copy the data payload, and set the pointer to it */
271   if (p_data_src != NULL) {
272     uint8_t* p_data_dst = (uint8_t*)(p_buf + 1);
273     memcpy(p_data_dst, p_data_src, data_len);
274
275     /* Update bta message buffer to point to payload data */
276     /* (Note AVRC_OP_BROWSING uses zero-copy: p_buf->msg.browse.p_browse_data
277      * already points to original avrc buffer) */
278     if (opcode == AVRC_OP_VENDOR)
279       p_buf->msg.vendor.p_vendor_data = p_data_dst;
280     else if (opcode == AVRC_OP_PASS_THRU)
281       p_buf->msg.pass.p_pass_data = p_data_dst;
282   }
283
284   if (opcode == AVRC_OP_BROWSE) {
285     /* set p_pkt to NULL, so avrc would not free the buffer */
286     p_msg->browse.p_browse_pkt = NULL;
287   }
288
289   bta_sys_sendmsg(p_buf);
290 }
291
292 /*******************************************************************************
293  *
294  * Function         bta_av_rc_create
295  *
296  * Description      alloc RCB and call AVRC_Open
297  *
298  * Returns          the created rc handle
299  *
300  ******************************************************************************/
301 uint8_t bta_av_rc_create(tBTA_AV_CB* p_cb, uint8_t role, uint8_t shdl,
302                          uint8_t lidx) {
303   if (is_new_avrcp_enabled()) {
304     LOG_INFO("Skipping RC creation for the old AVRCP profile");
305     return BTA_AV_RC_HANDLE_NONE;
306   }
307
308   tAVRC_CONN_CB ccb;
309   RawAddress bda = RawAddress::kAny;
310   uint8_t status = BTA_AV_RC_ROLE_ACP;
311   int i;
312   uint8_t rc_handle;
313   tBTA_AV_RCB* p_rcb;
314
315   if (role == AVCT_INT) {
316     // Can't grab a stream control block that doesn't have a valid handle
317     if (!shdl) {
318       APPL_TRACE_ERROR(
319           "%s: Can't grab stream control block for shdl = %d -> index = %d",
320           __func__, shdl, shdl - 1);
321       return BTA_AV_RC_HANDLE_NONE;
322     }
323     tBTA_AV_SCB* p_scb = p_cb->p_scb[shdl - 1];
324     bda = p_scb->PeerAddress();
325     status = BTA_AV_RC_ROLE_INT;
326   } else {
327     p_rcb = bta_av_get_rcb_by_shdl(shdl);
328     if (p_rcb != NULL) {
329       APPL_TRACE_ERROR("%s: ACP handle exist for shdl:%d", __func__, shdl);
330       p_rcb->lidx = lidx;
331       return p_rcb->handle;
332     }
333   }
334
335   ccb.ctrl_cback = base::Bind(bta_av_rc_ctrl_cback);
336   ccb.msg_cback = base::Bind(bta_av_rc_msg_cback);
337   ccb.company_id = p_bta_av_cfg->company_id;
338   ccb.conn = role;
339   /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL
340    */
341   ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT |
342                                   BTA_AV_FEAT_METADATA | AVRC_CT_PASSIVE);
343
344   if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
345     return BTA_AV_RC_HANDLE_NONE;
346
347   i = rc_handle;
348   p_rcb = &p_cb->rcb[i];
349
350   if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
351     APPL_TRACE_ERROR("%s: found duplicated handle:%d", __func__, rc_handle);
352   }
353
354   p_rcb->handle = rc_handle;
355   p_rcb->status = status;
356   p_rcb->shdl = shdl;
357   p_rcb->lidx = lidx;
358   p_rcb->peer_features = 0;
359   p_rcb->cover_art_psm = 0;
360   if (lidx == (BTA_AV_NUM_LINKS + 1)) {
361     /* this LIDX is reserved for the AVRCP ACP connection */
362     p_cb->rc_acp_handle = p_rcb->handle;
363     p_cb->rc_acp_idx = (i + 1);
364     APPL_TRACE_DEBUG("%s: rc_acp_handle:%d idx:%d", __func__,
365                      p_cb->rc_acp_handle, p_cb->rc_acp_idx);
366   }
367   APPL_TRACE_DEBUG(
368       "%s: create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
369       __func__, i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
370
371   return rc_handle;
372 }
373
374 /*******************************************************************************
375  *
376  * Function         bta_av_valid_group_navi_msg
377  *
378  * Description      Check if it is Group Navigation Msg for Metadata
379  *
380  * Returns          AVRC_RSP_ACCEPT or AVRC_RSP_NOT_IMPL
381  *
382  ******************************************************************************/
383 static tBTA_AV_CODE bta_av_group_navi_supported(uint8_t len, uint8_t* p_data,
384                                                 bool is_inquiry) {
385   tBTA_AV_CODE ret = AVRC_RSP_NOT_IMPL;
386   uint8_t* p_ptr = p_data;
387   uint16_t u16;
388   uint32_t u32;
389
390   if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN) {
391     BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
392     BE_STREAM_TO_UINT16(u16, p_ptr);
393
394     if (u32 == AVRC_CO_METADATA) {
395       if (is_inquiry) {
396         if (u16 <= AVRC_PDU_PREV_GROUP) ret = AVRC_RSP_IMPL_STBL;
397       } else {
398         if (u16 <= AVRC_PDU_PREV_GROUP)
399           ret = AVRC_RSP_ACCEPT;
400         else
401           ret = AVRC_RSP_REJ;
402       }
403     }
404   }
405
406   return ret;
407 }
408
409 /*******************************************************************************
410  *
411  * Function         bta_av_op_supported
412  *
413  * Description      Check if remote control operation is supported.
414  *
415  * Returns          AVRC_RSP_ACCEPT of supported, AVRC_RSP_NOT_IMPL if not.
416  *
417  ******************************************************************************/
418 static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id, bool is_inquiry) {
419   tBTA_AV_CODE ret_code = AVRC_RSP_NOT_IMPL;
420
421   if (p_bta_av_rc_id) {
422     if (is_inquiry) {
423       if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F))) {
424         ret_code = AVRC_RSP_IMPL_STBL;
425       }
426     } else {
427       if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F))) {
428         ret_code = AVRC_RSP_ACCEPT;
429       } else if ((p_bta_av_cfg->rc_pass_rsp == AVRC_RSP_INTERIM) &&
430                  p_bta_av_rc_id_ac) {
431         if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F))) {
432           ret_code = AVRC_RSP_INTERIM;
433         }
434       }
435     }
436   }
437   return ret_code;
438 }
439
440 /*******************************************************************************
441  *
442  * Function         bta_av_find_lcb
443  *
444  * Description      Given BD_addr, find the associated LCB.
445  *
446  * Returns          NULL, if not found.
447  *
448  ******************************************************************************/
449 tBTA_AV_LCB* bta_av_find_lcb(const RawAddress& addr, uint8_t op) {
450   tBTA_AV_CB* p_cb = &bta_av_cb;
451   int xx;
452   uint8_t mask;
453   tBTA_AV_LCB* p_lcb = NULL;
454
455   APPL_TRACE_DEBUG("%s: address: %s op:%d", __func__, addr.ToString().c_str(),
456                    op);
457   for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
458     mask = 1 << xx; /* the used mask for this lcb */
459     if ((mask & p_cb->conn_lcb) && p_cb->lcb[xx].addr == addr) {
460       p_lcb = &p_cb->lcb[xx];
461       if (op == BTA_AV_LCB_FREE) {
462         p_cb->conn_lcb &= ~mask; /* clear the connect mask */
463         APPL_TRACE_DEBUG("%s: conn_lcb: 0x%x", __func__, p_cb->conn_lcb);
464       }
465       break;
466     }
467   }
468   return p_lcb;
469 }
470
471 /*******************************************************************************
472  *
473  * Function         bta_av_rc_opened
474  *
475  * Description      Set AVRCP state to opened.
476  *
477  * Returns          void
478  *
479  ******************************************************************************/
480 void bta_av_rc_opened(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
481   tBTA_AV_RC_OPEN rc_open;
482   tBTA_AV_SCB* p_scb;
483   int i;
484   uint8_t shdl = 0;
485   tBTA_AV_LCB* p_lcb;
486   tBTA_AV_RCB* p_rcb;
487   uint8_t tmp;
488   uint8_t disc = 0;
489
490   /* find the SCB & stop the timer */
491   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
492     p_scb = p_cb->p_scb[i];
493     if (p_scb && p_scb->PeerAddress() == p_data->rc_conn_chg.peer_addr) {
494       p_scb->rc_handle = p_data->rc_conn_chg.handle;
495       APPL_TRACE_DEBUG("%s: shdl:%d, srch %d", __func__, i + 1,
496                        p_scb->rc_handle);
497       shdl = i + 1;
498       LOG_INFO("%s: allow incoming AVRCP connections:%d", __func__,
499                p_scb->use_rc);
500       alarm_cancel(p_scb->avrc_ct_timer);
501       disc = p_scb->hndl;
502       break;
503     }
504   }
505
506   i = p_data->rc_conn_chg.handle;
507   if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE) {
508     APPL_TRACE_ERROR("%s: not a valid handle:%d any more", __func__, i);
509     return;
510   }
511
512   APPL_TRACE_DEBUG("%s: local features %d peer features %d", __func__,
513                    p_cb->features, p_cb->rcb[i].peer_features);
514
515   /* listen to browsing channel when the connection is open,
516    * if peer initiated AVRCP connection and local device supports browsing
517    * channel */
518   AVRC_OpenBrowse(p_data->rc_conn_chg.handle, AVCT_ACP);
519
520   if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0) {
521     /* rc is opened on the RC only ACP channel, but is for a specific
522      * SCB -> need to switch RCBs */
523     p_rcb = bta_av_get_rcb_by_shdl(shdl);
524     if (p_rcb) {
525       p_rcb->shdl = p_cb->rcb[i].shdl;
526       tmp = p_rcb->lidx;
527       p_rcb->lidx = p_cb->rcb[i].lidx;
528       p_cb->rcb[i].lidx = tmp;
529       p_cb->rc_acp_handle = p_rcb->handle;
530       p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
531       APPL_TRACE_DEBUG("%s: switching RCB rc_acp_handle:%d idx:%d", __func__,
532                        p_cb->rc_acp_handle, p_cb->rc_acp_idx);
533     }
534   }
535
536   p_cb->rcb[i].shdl = shdl;
537   rc_open.rc_handle = i;
538   APPL_TRACE_ERROR("%s: rcb[%d] shdl:%d lidx:%d/%d", __func__, i, shdl,
539                    p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
540   p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
541
542   if (!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx) {
543     /* no associated SCB -> connected to an RC only device
544      * update the index to the extra LCB */
545     p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
546     p_lcb->addr = p_data->rc_conn_chg.peer_addr;
547     p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
548     p_cb->rcb[i].lidx = p_lcb->lidx;
549     p_lcb->conn_msk = 1;
550     APPL_TRACE_ERROR("%s: bd_addr: %s rcb[%d].lidx=%d, lcb.conn_msk=x%x",
551                      __func__, p_lcb->addr.ToString().c_str(), i,
552                      p_cb->rcb[i].lidx, p_lcb->conn_msk);
553     disc = p_data->rc_conn_chg.handle | BTA_AV_CHNL_MSK;
554   }
555
556   rc_open.peer_addr = p_data->rc_conn_chg.peer_addr;
557   rc_open.peer_features = p_cb->rcb[i].peer_features;
558   rc_open.cover_art_psm = p_cb->rcb[i].cover_art_psm;
559   rc_open.status = BTA_AV_SUCCESS;
560   APPL_TRACE_DEBUG("%s: local features:x%x peer_features:x%x", __func__,
561                    p_cb->features, rc_open.peer_features);
562   APPL_TRACE_DEBUG("%s: cover art psm:x%x", __func__, rc_open.cover_art_psm);
563   if (rc_open.peer_features == 0) {
564     /* we have not done SDP on peer RC capabilities.
565      * peer must have initiated the RC connection */
566     if (p_cb->features & BTA_AV_FEAT_RCCT)
567       rc_open.peer_features |= BTA_AV_FEAT_RCTG;
568     if (p_cb->features & BTA_AV_FEAT_RCTG)
569       rc_open.peer_features |= BTA_AV_FEAT_RCCT;
570
571     bta_av_rc_disc(disc);
572   }
573   tBTA_AV bta_av_data;
574   bta_av_data.rc_open = rc_open;
575   (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, &bta_av_data);
576
577   /* if local initiated AVRCP connection and both peer and locals device support
578    * browsing channel, open the browsing channel now
579    * TODO (sanketa): Some TG would not broadcast browse feature hence check
580    * inter-op. */
581   if ((p_cb->features & BTA_AV_FEAT_BROWSE) &&
582       (rc_open.peer_features & BTA_AV_FEAT_BROWSE) &&
583       ((p_cb->rcb[i].status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)) {
584     APPL_TRACE_DEBUG("%s: opening AVRC Browse channel", __func__);
585     AVRC_OpenBrowse(p_data->rc_conn_chg.handle, AVCT_INT);
586   }
587 }
588
589 /*******************************************************************************
590  *
591  * Function         bta_av_rc_remote_cmd
592  *
593  * Description      Send an AVRCP remote control command.
594  *
595  * Returns          void
596  *
597  ******************************************************************************/
598 void bta_av_rc_remote_cmd(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
599   tBTA_AV_RCB* p_rcb;
600   if (p_cb->features & BTA_AV_FEAT_RCCT) {
601     if (p_data->hdr.layer_specific < BTA_AV_NUM_RCB) {
602       p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
603       if (p_rcb->status & BTA_AV_RC_CONN_MASK) {
604         AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
605                      &p_data->api_remote_cmd.msg);
606       }
607     }
608   }
609 }
610
611 /*******************************************************************************
612  *
613  * Function         bta_av_rc_vendor_cmd
614  *
615  * Description      Send an AVRCP vendor specific command.
616  *
617  * Returns          void
618  *
619  ******************************************************************************/
620 void bta_av_rc_vendor_cmd(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
621   tBTA_AV_RCB* p_rcb;
622   if ((p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
623       (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) {
624     if (p_data->hdr.layer_specific < BTA_AV_NUM_RCB) {
625       p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
626       AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label,
627                      &p_data->api_vendor.msg);
628     }
629   }
630 }
631
632 /*******************************************************************************
633  *
634  * Function         bta_av_rc_vendor_rsp
635  *
636  * Description      Send an AVRCP vendor specific response.
637  *
638  * Returns          void
639  *
640  ******************************************************************************/
641 void bta_av_rc_vendor_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
642   tBTA_AV_RCB* p_rcb;
643   if ((p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
644       (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) {
645     if (p_data->hdr.layer_specific < BTA_AV_NUM_RCB) {
646       p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
647       AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label,
648                      &p_data->api_vendor.msg);
649     }
650   }
651 }
652
653 /*******************************************************************************
654  *
655  * Function         bta_av_rc_meta_rsp
656  *
657  * Description      Send an AVRCP metadata/advanced control command/response.
658  *
659  * Returns          void
660  *
661  ******************************************************************************/
662 void bta_av_rc_meta_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
663   tBTA_AV_RCB* p_rcb;
664   bool do_free = true;
665
666   if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
667       (p_data->hdr.layer_specific < BTA_AV_NUM_RCB)) {
668     if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
669         (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT))) {
670       p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
671       if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
672         AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label,
673                     p_data->api_meta_rsp.rsp_code, p_data->api_meta_rsp.p_pkt);
674         do_free = false;
675       }
676     }
677   }
678
679   if (do_free) osi_free_and_reset((void**)&p_data->api_meta_rsp.p_pkt);
680 }
681
682 /*******************************************************************************
683  *
684  * Function         bta_av_rc_free_rsp
685  *
686  * Description      free an AVRCP metadata command buffer.
687  *
688  * Returns          void
689  *
690  ******************************************************************************/
691 void bta_av_rc_free_rsp(UNUSED_ATTR tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
692   osi_free_and_reset((void**)&p_data->api_meta_rsp.p_pkt);
693 }
694
695 /*******************************************************************************
696  *
697  * Function         bta_av_rc_free_browse_msg
698  *
699  * Description      free an AVRCP browse message buffer.
700  *
701  * Returns          void
702  *
703  ******************************************************************************/
704 void bta_av_rc_free_browse_msg(UNUSED_ATTR tBTA_AV_CB* p_cb,
705                                tBTA_AV_DATA* p_data) {
706   if (p_data->rc_msg.opcode == AVRC_OP_BROWSE) {
707     osi_free_and_reset((void**)&p_data->rc_msg.msg.browse.p_browse_pkt);
708   }
709 }
710
711 /*******************************************************************************
712  *
713  * Function         bta_av_chk_notif_evt_id
714  *
715  * Description      make sure the requested player id is valid.
716  *
717  * Returns          BTA_AV_STS_NO_RSP, if no error
718  *
719  ******************************************************************************/
720 static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR* p_vendor) {
721   tAVRC_STS status = BTA_AV_STS_NO_RSP;
722   uint8_t xx;
723   uint16_t u16;
724   uint8_t* p = p_vendor->p_vendor_data + 2;
725
726   BE_STREAM_TO_UINT16(u16, p);
727   /* double check the fixed length */
728   if ((u16 != 5) || (p_vendor->vendor_len != 9)) {
729     status = AVRC_STS_INTERNAL_ERR;
730   } else {
731     /* make sure the player_id is valid */
732     for (xx = 0; xx < p_bta_av_cfg->num_evt_ids; xx++) {
733       if (*p == p_bta_av_cfg->p_meta_evt_ids[xx]) {
734         break;
735       }
736     }
737     if (xx == p_bta_av_cfg->num_evt_ids) {
738       status = AVRC_STS_BAD_PARAM;
739     }
740   }
741
742   return status;
743 }
744
745 /*******************************************************************************
746  *
747  * Function         bta_av_proc_meta_cmd
748  *
749  * Description      Process an AVRCP metadata command from the peer.
750  *
751  * Returns          true to respond immediately
752  *
753  ******************************************************************************/
754 tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE* p_rc_rsp,
755                                  tBTA_AV_RC_MSG* p_msg, uint8_t* p_ctype) {
756   tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
757   uint8_t u8, pdu, *p;
758   uint16_t u16;
759   tAVRC_MSG_VENDOR* p_vendor = &p_msg->msg.vendor;
760
761   pdu = *(p_vendor->p_vendor_data);
762   p_rc_rsp->pdu = pdu;
763   *p_ctype = AVRC_RSP_REJ;
764
765   /* Check to ansure a  valid minimum meta data length */
766   if ((AVRC_MIN_META_CMD_LEN + p_vendor->vendor_len) > AVRC_META_CMD_BUF_SIZE) {
767     /* reject it */
768     p_rc_rsp->rsp.status = AVRC_STS_BAD_PARAM;
769     APPL_TRACE_ERROR("%s: Invalid meta-command length: %d", __func__,
770                      p_vendor->vendor_len);
771     return 0;
772   }
773
774   /* Metadata messages only use PANEL sub-unit type */
775   if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL) {
776     APPL_TRACE_DEBUG("%s: SUBUNIT must be PANEL", __func__);
777     /* reject it */
778     evt = 0;
779     p_vendor->hdr.ctype = AVRC_RSP_NOT_IMPL;
780     p_vendor->vendor_len = 0;
781     p_rc_rsp->rsp.status = AVRC_STS_BAD_PARAM;
782   } else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype)) {
783     APPL_TRACE_DEBUG("%s: Invalid pdu/ctype: 0x%x, %d", __func__, pdu,
784                      p_vendor->hdr.ctype);
785     /* reject invalid message without reporting to app */
786     evt = 0;
787     p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
788   } else {
789     switch (pdu) {
790       case AVRC_PDU_GET_CAPABILITIES:
791         /* process GetCapabilities command without reporting the event to app */
792         evt = 0;
793         if (p_vendor->vendor_len != 5) {
794           android_errorWriteLog(0x534e4554, "111893951");
795           p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
796           break;
797         }
798         u8 = *(p_vendor->p_vendor_data + 4);
799         p = p_vendor->p_vendor_data + 2;
800         p_rc_rsp->get_caps.capability_id = u8;
801         BE_STREAM_TO_UINT16(u16, p);
802         if (u16 != 1) {
803           p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
804         } else {
805           p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
806           if (u8 == AVRC_CAP_COMPANY_ID) {
807             *p_ctype = AVRC_RSP_IMPL_STBL;
808             p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
809             memcpy(p_rc_rsp->get_caps.param.company_id,
810                    p_bta_av_cfg->p_meta_co_ids,
811                    (p_bta_av_cfg->num_co_ids << 2));
812           } else if (u8 == AVRC_CAP_EVENTS_SUPPORTED) {
813             *p_ctype = AVRC_RSP_IMPL_STBL;
814             p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
815             memcpy(p_rc_rsp->get_caps.param.event_id,
816                    p_bta_av_cfg->p_meta_evt_ids, p_bta_av_cfg->num_evt_ids);
817           } else {
818             APPL_TRACE_DEBUG("%s: Invalid capability ID: 0x%x", __func__, u8);
819             /* reject - unknown capability ID */
820             p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
821           }
822         }
823         break;
824
825       case AVRC_PDU_REGISTER_NOTIFICATION:
826         /* make sure the event_id is implemented */
827         p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id(p_vendor);
828         if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP) evt = 0;
829         break;
830     }
831   }
832
833   return evt;
834 }
835
836 /*******************************************************************************
837  *
838  * Function         bta_av_rc_msg
839  *
840  * Description      Process an AVRCP message from the peer.
841  *
842  * Returns          void
843  *
844  ******************************************************************************/
845 void bta_av_rc_msg(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
846   tBTA_AV_EVT evt = 0;
847   tBTA_AV av;
848   BT_HDR* p_pkt = NULL;
849   tAVRC_MSG_VENDOR* p_vendor = &p_data->rc_msg.msg.vendor;
850   bool is_inquiry = ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
851                      p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ);
852   uint8_t ctype = 0;
853   tAVRC_RESPONSE rc_rsp;
854
855   rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
856
857   if (NULL == p_data) {
858     APPL_TRACE_ERROR("%s: Message from peer with no data", __func__);
859     return;
860   }
861
862   APPL_TRACE_DEBUG("%s: opcode=%x, ctype=%x", __func__, p_data->rc_msg.opcode,
863                    p_data->rc_msg.msg.hdr.ctype);
864
865   if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU) {
866     /* if this is a pass thru command */
867     if ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL) ||
868         (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
869         (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ)) {
870       /* check if operation is supported */
871       char avrcp_ct_support[PROPERTY_VALUE_MAX];
872       osi_property_get("bluetooth.pts.avrcp_ct.support", avrcp_ct_support,
873                        "false");
874       if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR) {
875         p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_NOT_IMPL;
876         if (p_cb->features & BTA_AV_FEAT_METADATA)
877           p_data->rc_msg.msg.hdr.ctype = bta_av_group_navi_supported(
878               p_data->rc_msg.msg.pass.pass_len,
879               p_data->rc_msg.msg.pass.p_pass_data, is_inquiry);
880       } else if (((p_data->rc_msg.msg.pass.op_id == AVRC_ID_VOL_UP) ||
881                   (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VOL_DOWN)) &&
882                  !strcmp(avrcp_ct_support, "true")) {
883         p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_ACCEPT;
884       } else {
885         p_data->rc_msg.msg.hdr.ctype =
886             bta_av_op_supported(p_data->rc_msg.msg.pass.op_id, is_inquiry);
887       }
888
889       APPL_TRACE_DEBUG("%s: ctype %d", __func__, p_data->rc_msg.msg.hdr.ctype)
890
891       /* send response */
892       if (p_data->rc_msg.msg.hdr.ctype != AVRC_RSP_INTERIM)
893         AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label,
894                      &p_data->rc_msg.msg.pass);
895
896       /* set up for callback if supported */
897       if (p_data->rc_msg.msg.hdr.ctype == AVRC_RSP_ACCEPT ||
898           p_data->rc_msg.msg.hdr.ctype == AVRC_RSP_INTERIM) {
899         evt = BTA_AV_REMOTE_CMD_EVT;
900         av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
901         av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
902         av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
903         av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
904         memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof(tAVRC_HDR));
905         av.remote_cmd.label = p_data->rc_msg.label;
906       }
907     }
908     /* else if this is a pass thru response */
909     /* id response type is not impl, we have to release label */
910     else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_NOT_IMPL) {
911       /* set up for callback */
912       evt = BTA_AV_REMOTE_RSP_EVT;
913       av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
914       av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
915       av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
916       av.remote_rsp.label = p_data->rc_msg.label;
917
918       /* If this response is for vendor unique command  */
919       if ((p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR) &&
920           (p_data->rc_msg.msg.pass.pass_len > 0)) {
921         av.remote_rsp.p_data =
922             (uint8_t*)osi_malloc(p_data->rc_msg.msg.pass.pass_len);
923         APPL_TRACE_DEBUG("%s: Vendor Unique data len = %d", __func__,
924                          p_data->rc_msg.msg.pass.pass_len);
925         memcpy(av.remote_rsp.p_data, p_data->rc_msg.msg.pass.p_pass_data,
926                p_data->rc_msg.msg.pass.pass_len);
927       }
928     }
929     /* must be a bad ctype -> reject*/
930     else {
931       p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_REJ;
932       AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label,
933                    &p_data->rc_msg.msg.pass);
934     }
935   }
936   /* else if this is a vendor specific command or response */
937   else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR) {
938     /* set up for callback */
939     av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
940     av.vendor_cmd.company_id = p_vendor->company_id;
941     av.vendor_cmd.label = p_data->rc_msg.label;
942     av.vendor_cmd.p_data = p_vendor->p_vendor_data;
943     av.vendor_cmd.len = p_vendor->vendor_len;
944
945     /* if configured to support vendor specific and it's a command */
946     if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
947         p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ) {
948       if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
949           (p_vendor->company_id == AVRC_CO_METADATA)) {
950         av.meta_msg.p_msg = &p_data->rc_msg.msg;
951         rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
952         evt = bta_av_proc_meta_cmd(&rc_rsp, &p_data->rc_msg, &ctype);
953       } else {
954         evt = BTA_AV_VENDOR_CMD_EVT;
955       }
956     } else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
957                p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_NOT_IMPL) {
958       /* else if configured to support vendor specific and it's a response */
959       if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
960           (p_vendor->company_id == AVRC_CO_METADATA)) {
961         av.meta_msg.p_msg = &p_data->rc_msg.msg;
962         evt = BTA_AV_META_MSG_EVT;
963       } else {
964         evt = BTA_AV_VENDOR_RSP_EVT;
965       }
966     } else if (!(p_cb->features & BTA_AV_FEAT_VENDOR) &&
967                p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ) {
968       /* else if not configured to support vendor specific and it's a command */
969       if (p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID) {
970         /* reject it */
971         p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_REJ;
972         p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
973       } else {
974         p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_NOT_IMPL;
975       }
976       AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label,
977                      &p_data->rc_msg.msg.vendor);
978     }
979   } else if (p_data->rc_msg.opcode == AVRC_OP_BROWSE) {
980     /* set up for callback */
981     av.meta_msg.rc_handle = p_data->rc_msg.handle;
982     av.meta_msg.company_id = p_vendor->company_id;
983     av.meta_msg.code = p_data->rc_msg.msg.hdr.ctype;
984     av.meta_msg.label = p_data->rc_msg.label;
985     av.meta_msg.p_msg = &p_data->rc_msg.msg;
986     av.meta_msg.p_data = p_data->rc_msg.msg.browse.p_browse_data;
987     av.meta_msg.len = p_data->rc_msg.msg.browse.browse_len;
988     evt = BTA_AV_META_MSG_EVT;
989   }
990
991   if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP) {
992     if (!p_pkt) {
993       rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
994       AVRC_BldResponse(0, &rc_rsp, &p_pkt);
995     }
996     if (p_pkt)
997       AVRC_MsgReq(p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
998   }
999
1000   /* call callback */
1001   if (evt != 0) {
1002     av.remote_cmd.rc_handle = p_data->rc_msg.handle;
1003     (*p_cb->p_cback)(evt, &av);
1004     /* If browsing message, then free the browse message buffer */
1005     bta_av_rc_free_browse_msg(p_cb, p_data);
1006   }
1007 }
1008
1009 /*******************************************************************************
1010  *
1011  * Function         bta_av_rc_close
1012  *
1013  * Description      close the specified AVRC handle.
1014  *
1015  * Returns          void
1016  *
1017  ******************************************************************************/
1018 void bta_av_rc_close(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
1019   uint16_t handle = p_data->hdr.layer_specific;
1020   tBTA_AV_SCB* p_scb;
1021   tBTA_AV_RCB* p_rcb;
1022
1023   if (handle < BTA_AV_NUM_RCB) {
1024     p_rcb = &p_cb->rcb[handle];
1025
1026     APPL_TRACE_DEBUG("%s: handle: %d, status=0x%x", __func__, p_rcb->handle,
1027                      p_rcb->status);
1028     if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
1029       if (p_rcb->shdl) {
1030         p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1031         if (p_scb) {
1032           /* just in case the RC timer is active
1033           if (bta_av_cb.features & BTA_AV_FEAT_RCCT &&
1034              p_scb->chnl == BTA_AV_CHNL_AUDIO) */
1035           alarm_cancel(p_scb->avrc_ct_timer);
1036         }
1037       }
1038
1039       AVRC_Close(p_rcb->handle);
1040     }
1041   }
1042 }
1043
1044 /*******************************************************************************
1045  *
1046  * Function         bta_av_rc_browse_close
1047  *
1048  * Description      Empty placeholder.
1049  *
1050  * Returns          void
1051  *
1052  ******************************************************************************/
1053 void bta_av_rc_browse_close(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
1054   APPL_TRACE_WARNING("%s: empty placeholder does nothing!", __func__);
1055 }
1056
1057 /*******************************************************************************
1058  *
1059  * Function         bta_av_get_shdl
1060  *
1061  * Returns          The index to p_scb[]
1062  *
1063  ******************************************************************************/
1064 static uint8_t bta_av_get_shdl(tBTA_AV_SCB* p_scb) {
1065   int i;
1066   uint8_t shdl = 0;
1067   /* find the SCB & stop the timer */
1068   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1069     if (p_scb == bta_av_cb.p_scb[i]) {
1070       shdl = i + 1;
1071       break;
1072     }
1073   }
1074   return shdl;
1075 }
1076
1077 /*******************************************************************************
1078  *
1079  * Function         bta_av_stream_chg
1080  *
1081  * Description      audio streaming status changed.
1082  *
1083  * Returns          void
1084  *
1085  ******************************************************************************/
1086 void bta_av_stream_chg(tBTA_AV_SCB* p_scb, bool started) {
1087   uint8_t started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1088
1089   APPL_TRACE_DEBUG("%s: peer %s started:%s started_msk:0x%x", __func__,
1090                    p_scb->PeerAddress().ToString().c_str(),
1091                    logbool(started).c_str(), started_msk);
1092
1093   if (started) {
1094     /* Let L2CAP know this channel is processed with high priority */
1095     L2CA_SetAclPriority(p_scb->PeerAddress(), L2CAP_PRIORITY_HIGH);
1096   } else {
1097     /* Let L2CAP know this channel is processed with low priority */
1098     L2CA_SetAclPriority(p_scb->PeerAddress(), L2CAP_PRIORITY_NORMAL);
1099   }
1100 }
1101
1102 /*******************************************************************************
1103  *
1104  * Function         bta_av_conn_chg
1105  *
1106  * Description      connetion status changed.
1107  *                  Open an AVRCP acceptor channel, if new conn.
1108  *
1109  * Returns          void
1110  *
1111  ******************************************************************************/
1112 void bta_av_conn_chg(tBTA_AV_DATA* p_data) {
1113   tBTA_AV_CB* p_cb = &bta_av_cb;
1114   tBTA_AV_SCB* p_scb = NULL;
1115   tBTA_AV_SCB* p_scbi;
1116   uint8_t mask;
1117   uint8_t conn_msk;
1118   uint8_t old_msk;
1119   int i;
1120   int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
1121   tBTA_AV_LCB* p_lcb;
1122   tBTA_AV_LCB* p_lcb_rc;
1123   tBTA_AV_RCB *p_rcb, *p_rcb2;
1124   bool chk_restore = false;
1125
1126   /* Validate array index*/
1127   if (index < BTA_AV_NUM_STRS) {
1128     p_scb = p_cb->p_scb[index];
1129   }
1130   mask = BTA_AV_HNDL_TO_MSK(index);
1131   p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
1132   conn_msk = 1 << (index + 1);
1133   if (p_data->conn_chg.is_up) {
1134     /* set the conned mask for this channel */
1135     if (p_scb) {
1136       if (p_lcb) {
1137         p_lcb->conn_msk |= conn_msk;
1138         for (i = 0; i < BTA_AV_NUM_RCB; i++) {
1139           if (bta_av_cb.rcb[i].lidx == p_lcb->lidx) {
1140             bta_av_cb.rcb[i].shdl = index + 1;
1141             APPL_TRACE_DEBUG(
1142                 "%s: conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d",
1143                 __func__, i, bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1144                 bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1145             break;
1146           }
1147         }
1148       }
1149       old_msk = p_cb->conn_audio;
1150       p_cb->conn_audio |= mask;
1151
1152       if ((old_msk & mask) == 0) {
1153         /* increase the audio open count, if not set yet */
1154         bta_av_cb.audio_open_cnt++;
1155       }
1156
1157       APPL_TRACE_DEBUG("%s: rc_acp_handle:%d rc_acp_idx:%d", __func__,
1158                        p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1159       /* check if the AVRCP ACP channel is already connected */
1160       if (p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE &&
1161           p_cb->rc_acp_idx) {
1162         p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
1163         APPL_TRACE_DEBUG(
1164             "%s: rc_acp is connected && conn_chg on same addr "
1165             "p_lcb_rc->conn_msk:x%x",
1166             __func__, p_lcb_rc->conn_msk);
1167         /* check if the RC is connected to the scb addr */
1168         LOG_INFO("%s: p_lcb_rc->addr: %s conn_chg.peer_addr: %s", __func__,
1169                  p_lcb_rc->addr.ToString().c_str(),
1170                  p_data->conn_chg.peer_addr.ToString().c_str());
1171
1172         if (p_lcb_rc->conn_msk &&
1173             p_lcb_rc->addr == p_data->conn_chg.peer_addr) {
1174           /* AVRCP is already connected.
1175            * need to update the association betwen SCB and RCB */
1176           p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
1177           p_lcb_rc->lidx = 0;
1178           p_scb->rc_handle = p_cb->rc_acp_handle;
1179           p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
1180           p_rcb->shdl = bta_av_get_shdl(p_scb);
1181           APPL_TRACE_DEBUG("%s: update rc_acp shdl:%d/%d srch:%d", __func__,
1182                            index + 1, p_rcb->shdl, p_scb->rc_handle);
1183
1184           p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
1185           if (p_rcb2) {
1186             /* found the RCB that was created to associated with this SCB */
1187             p_cb->rc_acp_handle = p_rcb2->handle;
1188             p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
1189             APPL_TRACE_DEBUG("%s: new rc_acp_handle:%d, idx:%d", __func__,
1190                              p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1191             p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
1192             APPL_TRACE_DEBUG("%s: rc2 handle:%d lidx:%d/%d", __func__,
1193                              p_rcb2->handle, p_rcb2->lidx,
1194                              p_cb->lcb[p_rcb2->lidx - 1].lidx);
1195           }
1196           p_rcb->lidx = p_lcb->lidx;
1197           APPL_TRACE_DEBUG("%s: rc handle:%d lidx:%d/%d", __func__,
1198                            p_rcb->handle, p_rcb->lidx,
1199                            p_cb->lcb[p_rcb->lidx - 1].lidx);
1200         }
1201       }
1202     }
1203   } else {
1204     if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt) {
1205       /* this channel is still marked as open. decrease the count */
1206       bta_av_cb.audio_open_cnt--;
1207     }
1208
1209     /* clear the conned mask for this channel */
1210     p_cb->conn_audio &= ~mask;
1211     if (p_scb) {
1212       // The stream is closed. Clear the state.
1213       p_scb->OnDisconnected();
1214       if (p_scb->chnl == BTA_AV_CHNL_AUDIO) {
1215         if (p_lcb) {
1216           p_lcb->conn_msk &= ~conn_msk;
1217         }
1218         /* audio channel is down. make sure the INT channel is down */
1219         /* just in case the RC timer is active
1220         if (p_cb->features & BTA_AV_FEAT_RCCT) */
1221         { alarm_cancel(p_scb->avrc_ct_timer); }
1222         /* one audio channel goes down. check if we need to restore high
1223          * priority */
1224         chk_restore = true;
1225       }
1226     }
1227
1228     APPL_TRACE_DEBUG("%s: shdl:%d", __func__, index + 1);
1229     for (i = 0; i < BTA_AV_NUM_RCB; i++) {
1230       APPL_TRACE_DEBUG("%s: conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d",
1231                        __func__, i, bta_av_cb.rcb[i].handle,
1232                        bta_av_cb.rcb[i].status, bta_av_cb.rcb[i].shdl,
1233                        bta_av_cb.rcb[i].lidx);
1234       if (bta_av_cb.rcb[i].shdl == index + 1) {
1235         bta_av_del_rc(&bta_av_cb.rcb[i]);
1236         /* since the connection is already down and info was removed, clean
1237          * reference */
1238         bta_av_cb.rcb[i].shdl = 0;
1239         break;
1240       }
1241     }
1242
1243     if (p_cb->conn_audio == 0) {
1244       /* if both channels are not connected,
1245        * close all RC channels */
1246       bta_av_close_all_rc(p_cb);
1247     }
1248
1249     /* if the AVRCP is no longer listening, create the listening channel */
1250     if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE &&
1251         bta_av_cb.features & BTA_AV_FEAT_RCTG)
1252       bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1253   }
1254
1255   APPL_TRACE_DEBUG(
1256       "%s: audio:%x up:%d conn_msk:0x%x chk_restore:%d "
1257       "audio_open_cnt:%d",
1258       __func__, p_cb->conn_audio, p_data->conn_chg.is_up, conn_msk, chk_restore,
1259       p_cb->audio_open_cnt);
1260
1261   if (chk_restore) {
1262     if (p_cb->audio_open_cnt == 1) {
1263       /* one audio channel goes down and there's one audio channel remains open.
1264        * restore the switch role in default link policy */
1265       BTM_default_unblock_role_switch();
1266       bta_av_restore_switch();
1267     }
1268     if (p_cb->audio_open_cnt) {
1269       /* adjust flush timeout settings to longer period */
1270       for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1271         p_scbi = bta_av_cb.p_scb[i];
1272         if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
1273           /* may need to update the flush timeout of this already started stream
1274            */
1275           if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
1276             p_scbi->co_started = bta_av_cb.audio_open_cnt;
1277           }
1278         }
1279       }
1280     }
1281   }
1282 }
1283
1284 /*******************************************************************************
1285  *
1286  * Function         bta_av_disable
1287  *
1288  * Description      disable AV.
1289  *
1290  * Returns          void
1291  *
1292  ******************************************************************************/
1293 void bta_av_disable(tBTA_AV_CB* p_cb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1294   BT_HDR_RIGID hdr;
1295   bool disabling_in_progress = false;
1296   uint16_t xx;
1297
1298   p_cb->disabling = true;
1299
1300   bta_av_close_all_rc(p_cb);
1301
1302   osi_free_and_reset((void**)&p_cb->p_disc_db);
1303
1304   /* disable audio/video - de-register all channels,
1305    * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
1306   for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
1307     if (p_cb->p_scb[xx] != NULL) {
1308       hdr.layer_specific = xx + 1;
1309       bta_av_api_deregister((tBTA_AV_DATA*)&hdr);
1310       disabling_in_progress = true;
1311     }
1312   }
1313   // Since All channels are deregistering by API_DEREGISTER, the DEREG_COMP_EVT
1314   // would come first before API_DISABLE if there is no connections, and it is
1315   // no needed to setup this disabling flag.
1316   p_cb->disabling = disabling_in_progress;
1317
1318   alarm_free(p_cb->link_signalling_timer);
1319   p_cb->link_signalling_timer = NULL;
1320   alarm_free(p_cb->accept_signalling_timer);
1321   p_cb->accept_signalling_timer = NULL;
1322 }
1323
1324 /*******************************************************************************
1325  *
1326  * Function         bta_av_api_disconnect
1327  *
1328  * Description      .
1329  *
1330  * Returns          void
1331  *
1332  ******************************************************************************/
1333 void bta_av_api_disconnect(tBTA_AV_DATA* p_data) {
1334   AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
1335   alarm_cancel(bta_av_cb.link_signalling_timer);
1336 }
1337
1338 /**
1339  * Find the index for the free LCB entry to use.
1340  *
1341  * The selection order is:
1342  * (1) Find the index if there is already SCB entry for the peer address
1343  * (2) If there is no SCB entry for the peer address, find the first
1344  * SCB entry that is not assigned.
1345  *
1346  * @param peer_address the peer address to use
1347  * @return the index for the free LCB entry to use or BTA_AV_NUM_LINKS
1348  * if no entry is found
1349  */
1350 static uint8_t bta_av_find_lcb_index_by_scb_and_address(
1351     const RawAddress& peer_address) {
1352   APPL_TRACE_DEBUG("%s: peer_address: %s conn_lcb: 0x%x", __func__,
1353                    peer_address.ToString().c_str(), bta_av_cb.conn_lcb);
1354
1355   // Find the index if there is already SCB entry for the peer address
1356   for (uint8_t index = 0; index < BTA_AV_NUM_LINKS; index++) {
1357     uint8_t mask = 1 << index;
1358     if (mask & bta_av_cb.conn_lcb) {
1359       continue;
1360     }
1361     tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
1362     if (p_scb == nullptr) {
1363       continue;
1364     }
1365     if (p_scb->PeerAddress() == peer_address) {
1366       return index;
1367     }
1368   }
1369
1370   // Find the first SCB entry that is not assigned.
1371   for (uint8_t index = 0; index < BTA_AV_NUM_LINKS; index++) {
1372     uint8_t mask = 1 << index;
1373     if (mask & bta_av_cb.conn_lcb) {
1374       continue;
1375     }
1376     tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
1377     if (p_scb == nullptr) {
1378       continue;
1379     }
1380     if (!p_scb->IsAssigned()) {
1381       return index;
1382     }
1383   }
1384
1385   return BTA_AV_NUM_LINKS;
1386 }
1387
1388 /*******************************************************************************
1389  *
1390  * Function         bta_av_sig_chg
1391  *
1392  * Description      process AVDT signal channel up/down.
1393  *
1394  * Returns          void
1395  *
1396  ******************************************************************************/
1397 void bta_av_sig_chg(tBTA_AV_DATA* p_data) {
1398   uint16_t event = p_data->str_msg.hdr.layer_specific;
1399   tBTA_AV_CB* p_cb = &bta_av_cb;
1400   uint32_t xx;
1401   uint8_t mask;
1402   tBTA_AV_LCB* p_lcb = NULL;
1403
1404   APPL_TRACE_DEBUG("%s: event: %d", __func__, event);
1405   if (event == AVDT_CONNECT_IND_EVT) {
1406     APPL_TRACE_DEBUG("%s: AVDT_CONNECT_IND_EVT: peer %s", __func__,
1407                      p_data->str_msg.bd_addr.ToString().c_str());
1408
1409     p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1410     if (!p_lcb) {
1411       /* if the address does not have an LCB yet, alloc one */
1412       xx = bta_av_find_lcb_index_by_scb_and_address(p_data->str_msg.bd_addr);
1413
1414       /* check if we found something */
1415       if (xx >= BTA_AV_NUM_LINKS) {
1416         /* We do not have scb for this avdt connection.     */
1417         /* Silently close the connection.                   */
1418         APPL_TRACE_ERROR("%s: av scb not available for avdt connection for %s",
1419                          __func__, p_data->str_msg.bd_addr.ToString().c_str());
1420         AVDT_DisconnectReq(p_data->str_msg.bd_addr, NULL);
1421         return;
1422       }
1423       LOG_INFO("%s: AVDT_CONNECT_IND_EVT: peer %s selected lcb_index %d",
1424                __func__, p_data->str_msg.bd_addr.ToString().c_str(), xx);
1425
1426       tBTA_AV_SCB* p_scb = p_cb->p_scb[xx];
1427       mask = 1 << xx;
1428       p_lcb = &p_cb->lcb[xx];
1429       p_lcb->lidx = xx + 1;
1430       p_lcb->addr = p_data->str_msg.bd_addr;
1431       p_lcb->conn_msk = 0; /* clear the connect mask */
1432       /* start listening when the signal channel is open */
1433       if (p_cb->features & BTA_AV_FEAT_RCTG) {
1434         bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1435       }
1436       /* this entry is not used yet. */
1437       p_cb->conn_lcb |= mask; /* mark it as used */
1438       APPL_TRACE_DEBUG("%s: start sig timer %d", __func__, p_data->hdr.offset);
1439       if (p_data->hdr.offset == AVDT_ACP) {
1440         APPL_TRACE_DEBUG("%s: Incoming L2CAP acquired, set state as incoming",
1441                          __func__);
1442         p_scb->OnConnected(p_data->str_msg.bd_addr);
1443         p_scb->use_rc = true; /* allowing RC for incoming connection */
1444         bta_av_ssm_execute(p_scb, BTA_AV_ACP_CONNECT_EVT, p_data);
1445
1446         /* The Pending Event should be sent as soon as the L2CAP signalling
1447          * channel
1448          * is set up, which is NOW. Earlier this was done only after
1449          * BTA_AV_SIGNALLING_TIMEOUT_MS.
1450          * The following function shall send the event and start the
1451          * recurring timer
1452          */
1453         bta_av_signalling_timer(NULL);
1454
1455         APPL_TRACE_DEBUG("%s: Re-start timer for AVDTP service", __func__);
1456         bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
1457         /* Possible collision : need to avoid outgoing processing while the
1458          * timer is running */
1459         p_scb->coll_mask = BTA_AV_COLL_INC_TMR;
1460         alarm_set_on_mloop(
1461             p_cb->accept_signalling_timer, BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS,
1462             bta_av_accept_signalling_timer_cback, UINT_TO_PTR(xx));
1463       }
1464     }
1465   }
1466   else if (event == BTA_AR_AVDT_CONN_EVT) {
1467     alarm_cancel(bta_av_cb.link_signalling_timer);
1468   }
1469   else {
1470     /* disconnected. */
1471     APPL_TRACE_DEBUG("%s: bta_av_cb.conn_lcb=0x%x", __func__,
1472                      bta_av_cb.conn_lcb);
1473
1474     p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1475     if (p_lcb && (p_lcb->conn_msk || bta_av_cb.conn_lcb)) {
1476       APPL_TRACE_DEBUG("%s: conn_msk: 0x%x", __func__, p_lcb->conn_msk);
1477       /* clean up ssm  */
1478       for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
1479         if (p_cb->p_scb[xx] &&
1480             p_cb->p_scb[xx]->PeerAddress() == p_data->str_msg.bd_addr) {
1481           APPL_TRACE_DEBUG("%s: Closing timer for AVDTP service", __func__);
1482           bta_sys_conn_close(BTA_ID_AV, p_cb->p_scb[xx]->app_id,
1483                              p_cb->p_scb[xx]->PeerAddress());
1484         }
1485         mask = 1 << (xx + 1);
1486         if (((mask & p_lcb->conn_msk) || bta_av_cb.conn_lcb) &&
1487             p_cb->p_scb[xx] &&
1488             p_cb->p_scb[xx]->PeerAddress() == p_data->str_msg.bd_addr) {
1489           APPL_TRACE_WARNING("%s: Sending AVDT_DISCONNECT_EVT peer_addr=%s",
1490                              __func__,
1491                              p_cb->p_scb[xx]->PeerAddress().ToString().c_str());
1492           bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1493         }
1494       }
1495     }
1496   }
1497   APPL_TRACE_DEBUG("%s: bta_av_cb.conn_lcb=0x%x after sig_chg", __func__,
1498                    p_cb->conn_lcb);
1499 }
1500
1501 /*******************************************************************************
1502  *
1503  * Function         bta_av_signalling_timer
1504  *
1505  * Description      process the signal channel timer. This timer is started
1506  *                  when the AVDTP signal channel is connected. If no profile
1507  *                  is connected, the timer goes off every
1508  *                  BTA_AV_SIGNALLING_TIMEOUT_MS.
1509  *
1510  * Returns          void
1511  *
1512  ******************************************************************************/
1513 void bta_av_signalling_timer(UNUSED_ATTR tBTA_AV_DATA* p_data) {
1514   tBTA_AV_CB* p_cb = &bta_av_cb;
1515   int xx;
1516   uint8_t mask;
1517   tBTA_AV_LCB* p_lcb = NULL;
1518
1519   APPL_TRACE_DEBUG("%s: conn_lcb=0x%x", __func__, p_cb->conn_lcb);
1520   for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
1521     p_lcb = &p_cb->lcb[xx];
1522     mask = 1 << xx;
1523     APPL_TRACE_DEBUG(
1524         "%s: index=%d conn_lcb=0x%x peer=%s conn_mask=0x%x lidx=%d", __func__,
1525         xx, p_cb->conn_lcb, p_lcb->addr.ToString().c_str(), p_lcb->conn_msk,
1526         p_lcb->lidx);
1527     if (mask & p_cb->conn_lcb) {
1528       /* this entry is used. check if it is connected */
1529       if (!p_lcb->conn_msk) {
1530         bta_sys_start_timer(p_cb->link_signalling_timer,
1531                             BTA_AV_SIGNALLING_TIMEOUT_MS,
1532                             BTA_AV_SIGNALLING_TIMER_EVT, 0);
1533         tBTA_AV_PEND pend;
1534         pend.bd_addr = p_lcb->addr;
1535         tBTA_AV bta_av_data;
1536         bta_av_data.pend = pend;
1537         APPL_TRACE_DEBUG(
1538             "%s: BTA_AV_PENDING_EVT for %s index=%d conn_mask=0x%x lidx=%d",
1539             __func__, pend.bd_addr.ToString().c_str(), xx, p_lcb->conn_msk,
1540             p_lcb->lidx);
1541         (*p_cb->p_cback)(BTA_AV_PENDING_EVT, &bta_av_data);
1542       }
1543     }
1544   }
1545 }
1546
1547 /*******************************************************************************
1548  *
1549  * Function         bta_av_accept_signalling_timer_cback
1550  *
1551  * Description      Process the timeout when SRC is accepting connection
1552  *                  and SNK did not start signalling.
1553  *
1554  * Returns          void
1555  *
1556  ******************************************************************************/
1557 static void bta_av_accept_signalling_timer_cback(void* data) {
1558   uint32_t inx = PTR_TO_UINT(data);
1559   tBTA_AV_CB* p_cb = &bta_av_cb;
1560   tBTA_AV_SCB* p_scb = NULL;
1561   if (inx < BTA_AV_NUM_STRS) {
1562     p_scb = p_cb->p_scb[inx];
1563   }
1564   if (p_scb) {
1565     APPL_TRACE_DEBUG("%s: coll_mask=0x%02x", __func__, p_scb->coll_mask);
1566
1567     if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1568       p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1569
1570       if (bta_av_is_scb_opening(p_scb)) {
1571         APPL_TRACE_DEBUG("%s: stream state opening: SDP started = %d", __func__,
1572                          p_scb->sdp_discovery_started);
1573         if (p_scb->sdp_discovery_started) {
1574           /* We are still doing SDP. Run the timer again. */
1575           p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1576
1577           alarm_set_on_mloop(p_cb->accept_signalling_timer,
1578                              BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS,
1579                              bta_av_accept_signalling_timer_cback,
1580                              UINT_TO_PTR(inx));
1581         } else {
1582           /* SNK did not start signalling, resume signalling process. */
1583           bta_av_discover_req(p_scb, NULL);
1584         }
1585       } else if (bta_av_is_scb_incoming(p_scb)) {
1586         /* Stay in incoming state if SNK does not start signalling */
1587
1588         APPL_TRACE_DEBUG("%s: stream state incoming", __func__);
1589         /* API open was called right after SNK opened L2C connection. */
1590         if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED) {
1591           p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1592
1593           /* BTA_AV_API_OPEN_EVT */
1594           tBTA_AV_API_OPEN* p_buf =
1595               (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
1596           memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1597           bta_sys_sendmsg(p_buf);
1598         }
1599       }
1600     }
1601   }
1602 }
1603
1604 /*******************************************************************************
1605  *
1606  * Function         bta_av_check_peer_features
1607  *
1608  * Description      check supported features on the peer device from the SDP
1609  *                  record and return the feature mask
1610  *
1611  * Returns          tBTA_AV_FEAT peer device feature mask
1612  *
1613  ******************************************************************************/
1614 tBTA_AV_FEAT bta_av_check_peer_features(uint16_t service_uuid) {
1615   tBTA_AV_FEAT peer_features = 0;
1616   tBTA_AV_CB* p_cb = &bta_av_cb;
1617   tSDP_DISC_REC* p_rec = NULL;
1618   tSDP_DISC_ATTR* p_attr;
1619   uint16_t peer_rc_version = 0;
1620   uint16_t categories = 0;
1621
1622   APPL_TRACE_DEBUG("%s: service_uuid:x%x", __func__, service_uuid);
1623   /* loop through all records we found */
1624   while (true) {
1625     /* get next record; if none found, we're done */
1626     p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec);
1627     if (p_rec == NULL) {
1628       break;
1629     }
1630
1631     if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) !=
1632         NULL) {
1633       /* find peer features */
1634       if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1635                               NULL)) {
1636         peer_features |= BTA_AV_FEAT_RCCT;
1637       }
1638       if (SDP_FindServiceInDb(p_cb->p_disc_db,
1639                               UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1640         peer_features |= BTA_AV_FEAT_RCTG;
1641       }
1642     }
1643
1644     if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1645       /* get profile version (if failure, version parameter is not updated) */
1646       SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1647                                   &peer_rc_version);
1648       APPL_TRACE_DEBUG("%s: peer_rc_version 0x%x", __func__, peer_rc_version);
1649
1650       if (peer_rc_version >= AVRC_REV_1_3)
1651         peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1652
1653       if (peer_rc_version >= AVRC_REV_1_4) {
1654         /* get supported categories */
1655         p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
1656         if (p_attr != NULL) {
1657           categories = p_attr->attr_value.v.u16;
1658           if (categories & AVRC_SUPF_CT_CAT2)
1659             peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1660           if (categories & AVRC_SUPF_CT_BROWSE)
1661             peer_features |= (BTA_AV_FEAT_BROWSE);
1662         }
1663       }
1664     }
1665   }
1666   APPL_TRACE_DEBUG("%s: peer_features:x%x", __func__, peer_features);
1667   return peer_features;
1668 }
1669
1670 /*******************************************************************************
1671  *
1672  * Function         bta_avk_check_peer_features
1673  *
1674  * Description      check supported features on the peer device from the SDP
1675  *                  record and return the feature mask
1676  *
1677  * Returns          tBTA_AV_FEAT peer device feature mask
1678  *
1679  ******************************************************************************/
1680 tBTA_AV_FEAT bta_avk_check_peer_features(uint16_t service_uuid) {
1681   tBTA_AV_FEAT peer_features = 0;
1682   tBTA_AV_CB* p_cb = &bta_av_cb;
1683
1684   APPL_TRACE_DEBUG("%s: service_uuid:x%x", __func__, service_uuid);
1685
1686   /* loop through all records we found */
1687   tSDP_DISC_REC* p_rec =
1688       SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, NULL);
1689   while (p_rec) {
1690     APPL_TRACE_DEBUG("%s: found Service record for x%x", __func__,
1691                      service_uuid);
1692
1693     if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) !=
1694         NULL) {
1695       /* find peer features */
1696       if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1697                               NULL)) {
1698         peer_features |= BTA_AV_FEAT_RCCT;
1699       }
1700       if (SDP_FindServiceInDb(p_cb->p_disc_db,
1701                               UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1702         peer_features |= BTA_AV_FEAT_RCTG;
1703       }
1704     }
1705
1706     if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1707       /* get profile version (if failure, version parameter is not updated) */
1708       uint16_t peer_rc_version = 0;
1709       bool val = SDP_FindProfileVersionInRec(
1710           p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1711       APPL_TRACE_DEBUG("%s: peer_rc_version for TG 0x%x, profile_found %d",
1712                        __func__, peer_rc_version, val);
1713
1714       if (peer_rc_version >= AVRC_REV_1_3)
1715         peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1716
1717       /* Get supported features */
1718       tSDP_DISC_ATTR* p_attr =
1719           SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
1720       if (p_attr != NULL) {
1721         uint16_t categories = p_attr->attr_value.v.u16;
1722         /*
1723          * Though Absolute Volume came after in 1.4 and above, but there are
1724          * few devices in market which supports absolute Volume and they are
1725          * still 1.3. To avoid IOP issuses with those devices, we check for
1726          * 1.3 as minimum version
1727          */
1728         if (peer_rc_version >= AVRC_REV_1_3) {
1729           if (categories & AVRC_SUPF_TG_CAT2)
1730             peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1731           if (categories & AVRC_SUPF_TG_APP_SETTINGS)
1732             peer_features |= (BTA_AV_FEAT_APP_SETTING);
1733           if (categories & AVRC_SUPF_TG_BROWSE)
1734             peer_features |= (BTA_AV_FEAT_BROWSE);
1735         }
1736
1737         /* AVRCP Cover Artwork over BIP */
1738         if (peer_rc_version >= AVRC_REV_1_6) {
1739           if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
1740               categories & AVRC_SUPF_TG_PLAYER_COVER_ART)
1741             peer_features |= (BTA_AV_FEAT_COVER_ARTWORK);
1742         }
1743       }
1744     }
1745     /* get next record; if none found, we're done */
1746     p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec);
1747   }
1748   APPL_TRACE_DEBUG("%s: peer_features:x%x", __func__, peer_features);
1749   return peer_features;
1750 }
1751
1752 /******************************************************************************
1753  *
1754  * Function         bta_avk_get_cover_art_psm
1755  *
1756  * Description      Get the PSM associated with the AVRCP Target cover art
1757  *                  feature
1758  *
1759  * Returns          uint16_t PSM value used to get cover artwork, or 0x0000 if
1760  *                  one does not exist.
1761  *
1762  *****************************************************************************/
1763 uint16_t bta_avk_get_cover_art_psm() {
1764   APPL_TRACE_DEBUG("%s: searching for cover art psm", __func__);
1765   /* Cover Art L2CAP PSM is only available on a target device */
1766   tBTA_AV_CB* p_cb = &bta_av_cb;
1767   tSDP_DISC_REC* p_rec =
1768       SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET,
1769           NULL);
1770   while (p_rec) {
1771     tSDP_DISC_ATTR* p_attr =
1772         (SDP_FindAttributeInRec(p_rec, ATTR_ID_ADDITION_PROTO_DESC_LISTS));
1773     /*
1774      * If we have the Additional Protocol Description Lists attribute then we
1775      * specifically want the list that is an L2CAP protocol leading to OBEX.
1776      * Because the is a case where cover art is supported and browsing isn't
1777      * we need to check each list for the one we want.
1778      *
1779      * This means we need to do drop down into the protocol list and do a
1780      * "for each protocol, for each protocol element, for each protocol element
1781      * list parameter, if the parameter is L2CAP then find the PSM associated
1782      * with it, then make sure we see OBEX in that same protocol"
1783      */
1784     if (p_attr != NULL && SDP_DISC_ATTR_TYPE(p_attr->attr_len_type)
1785         == DATA_ELE_SEQ_DESC_TYPE) {
1786       // Point to first in List of protocols (i.e [(L2CAP -> AVCTP),
1787       // (L2CAP -> OBEX)])
1788       tSDP_DISC_ATTR* p_protocol_list = p_attr->attr_value.v.p_sub_attr;
1789       while (p_protocol_list != NULL) {
1790         if (SDP_DISC_ATTR_TYPE(p_protocol_list->attr_len_type)
1791             == DATA_ELE_SEQ_DESC_TYPE) {
1792           // Point to fist in list of protocol elements (i.e. [L2CAP, AVCTP])
1793           tSDP_DISC_ATTR* p_protocol =
1794               p_protocol_list->attr_value.v.p_sub_attr;
1795           bool protocol_has_obex = false;
1796           bool protocol_has_l2cap = false;
1797           uint16_t psm = 0x0000;
1798           while (p_protocol) {
1799             if (SDP_DISC_ATTR_TYPE(p_protocol->attr_len_type)
1800                 == DATA_ELE_SEQ_DESC_TYPE) {
1801               // Point to first item protocol parameters list (i.e [UUID=L2CAP,
1802               // PSM=0x1234])
1803               tSDP_DISC_ATTR* p_protocol_param =
1804                   p_protocol->attr_value.v.p_sub_attr;
1805               /*
1806                * Currently there's only ever one UUID and one parameter. L2cap
1807                * has a single PSM, AVCTP has a version and OBEX has nothing.
1808                * Change this if that ever changes.
1809                */
1810               uint16_t protocol_uuid = 0;
1811               uint16_t protocol_param = 0;
1812               while (p_protocol_param) {
1813                 uint16_t param_type =
1814                     SDP_DISC_ATTR_TYPE(p_protocol_param->attr_len_type);
1815                 uint16_t param_len =
1816                     SDP_DISC_ATTR_LEN(p_protocol_param->attr_len_type);
1817                 if (param_type == UUID_DESC_TYPE) {
1818                   protocol_uuid = p_protocol_param->attr_value.v.u16;
1819                 } else if (param_type == UINT_DESC_TYPE) {
1820                     protocol_param = (param_len == 2)
1821                       ? p_protocol_param->attr_value.v.u16
1822                       : p_protocol_param->attr_value.v.u8;
1823                 } /* else dont care */
1824                 p_protocol_param = p_protocol_param->p_next_attr;  // next
1825               }
1826               // If we've found L2CAP then the parameter is a PSM
1827               if (protocol_uuid == UUID_PROTOCOL_L2CAP) {
1828                 protocol_has_l2cap = true;
1829                 psm = protocol_param;
1830               } else if (protocol_uuid == UUID_PROTOCOL_OBEX) {
1831                 protocol_has_obex = true;
1832               }
1833             }
1834             // If this protocol has l2cap and obex then we're found the BIP PSM
1835             if (protocol_has_l2cap && protocol_has_obex) {
1836               APPL_TRACE_DEBUG("%s: found psm 0x%x", __func__, psm);
1837               return psm;
1838             }
1839             p_protocol = p_protocol->p_next_attr;  // next protocol element
1840           }
1841         }
1842         p_protocol_list = p_protocol_list->p_next_attr;  // next protocol
1843       }
1844     }
1845     /* get next record; if none found, we're done */
1846     p_rec = SDP_FindServiceInDb(p_cb->p_disc_db,
1847         UUID_SERVCLASS_AV_REM_CTRL_TARGET, p_rec);
1848   }
1849   /* L2CAP PSM range is 0x1000-0xFFFF so 0x0000 is safe default invalid */
1850   APPL_TRACE_DEBUG("%s: could not find a BIP psm", __func__);
1851   return 0x0000;
1852 }
1853
1854 /*******************************************************************************
1855  *
1856  * Function         bta_av_rc_disc_done
1857  *
1858  * Description      Handle AVRCP service discovery results.  If matching
1859  *                  service found, open AVRCP connection.
1860  *
1861  * Returns          void
1862  *
1863  ******************************************************************************/
1864 void bta_av_rc_disc_done(UNUSED_ATTR tBTA_AV_DATA* p_data) {
1865   tBTA_AV_CB* p_cb = &bta_av_cb;
1866   tBTA_AV_SCB* p_scb = NULL;
1867   tBTA_AV_LCB* p_lcb;
1868   uint8_t rc_handle;
1869   tBTA_AV_FEAT peer_features = 0; /* peer features mask */
1870   uint16_t cover_art_psm = 0x0000;
1871
1872   APPL_TRACE_DEBUG("%s: bta_av_rc_disc_done disc:x%x", __func__, p_cb->disc);
1873   if (!p_cb->disc) {
1874     return;
1875   }
1876
1877   if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
1878     /* this is the rc handle/index to tBTA_AV_RCB */
1879     rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1880   } else {
1881     /* Validate array index*/
1882     if (((p_cb->disc & BTA_AV_HNDL_MSK) - 1) < BTA_AV_NUM_STRS) {
1883       p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1884     }
1885     if (p_scb) {
1886       rc_handle = p_scb->rc_handle;
1887     } else {
1888       p_cb->disc = 0;
1889       return;
1890     }
1891   }
1892
1893   APPL_TRACE_DEBUG("%s: rc_handle %d", __func__, rc_handle);
1894 #if (BTA_AV_SINK_INCLUDED == TRUE)
1895   if (p_cb->sdp_a2dp_snk_handle) {
1896     /* This is Sink + CT + TG(Abs Vol) */
1897     peer_features =
1898         bta_avk_check_peer_features(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1899     APPL_TRACE_DEBUG("%s: populating rem ctrl target features %d", __func__,
1900                      peer_features);
1901     if (BTA_AV_FEAT_ADV_CTRL &
1902         bta_avk_check_peer_features(UUID_SERVCLASS_AV_REMOTE_CONTROL))
1903       peer_features |= (BTA_AV_FEAT_ADV_CTRL | BTA_AV_FEAT_RCCT);
1904
1905     if (peer_features & BTA_AV_FEAT_COVER_ARTWORK)
1906       cover_art_psm = bta_avk_get_cover_art_psm();
1907
1908     APPL_TRACE_DEBUG("%s: populating rem ctrl target bip psm 0x%x", __func__,
1909                      cover_art_psm);
1910   } else
1911 #endif
1912       if (p_cb->sdp_a2dp_handle) {
1913     /* check peer version and whether support CT and TG role */
1914     peer_features =
1915         bta_av_check_peer_features(UUID_SERVCLASS_AV_REMOTE_CONTROL);
1916     if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) &&
1917         ((peer_features & BTA_AV_FEAT_ADV_CTRL) == 0)) {
1918       /* if we support advance control and peer does not, check their support on
1919        * TG role
1920        * some implementation uses 1.3 on CT ans 1.4 on TG */
1921       peer_features |=
1922           bta_av_check_peer_features(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1923     }
1924
1925     /* Change our features if the remote AVRCP version is 1.3 or less */
1926     tSDP_DISC_REC* p_rec = nullptr;
1927     p_rec = SDP_FindServiceInDb(p_cb->p_disc_db,
1928                                 UUID_SERVCLASS_AV_REMOTE_CONTROL, p_rec);
1929     if (p_rec != NULL &&
1930         SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST) != NULL) {
1931       /* get profile version (if failure, version parameter is not updated) */
1932       uint16_t peer_rc_version = 0xFFFF;  // Don't change the AVRCP version
1933       SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1934                                   &peer_rc_version);
1935       if (peer_rc_version <= AVRC_REV_1_3) {
1936         APPL_TRACE_DEBUG("%s: Using AVRCP 1.3 Capabilities with remote device",
1937                          __func__);
1938         p_bta_av_cfg = &bta_av_cfg_compatibility;
1939       }
1940     }
1941   }
1942
1943   p_cb->disc = 0;
1944   osi_free_and_reset((void**)&p_cb->p_disc_db);
1945
1946   APPL_TRACE_DEBUG("%s: peer_features 0x%x, features 0x%x", __func__,
1947                    peer_features, p_cb->features);
1948
1949   /* if we have no rc connection */
1950   if (rc_handle == BTA_AV_RC_HANDLE_NONE) {
1951     if (p_scb) {
1952       /* if peer remote control service matches ours and USE_RC is true */
1953       if ((((p_cb->features & BTA_AV_FEAT_RCCT) &&
1954             (peer_features & BTA_AV_FEAT_RCTG)) ||
1955            ((p_cb->features & BTA_AV_FEAT_RCTG) &&
1956             (peer_features & BTA_AV_FEAT_RCCT)))) {
1957         p_lcb = bta_av_find_lcb(p_scb->PeerAddress(), BTA_AV_LCB_FIND);
1958         if (p_lcb) {
1959           rc_handle = bta_av_rc_create(p_cb, AVCT_INT,
1960                                        (uint8_t)(p_scb->hdi + 1), p_lcb->lidx);
1961           p_cb->rcb[rc_handle].peer_features = peer_features;
1962           p_cb->rcb[rc_handle].cover_art_psm = cover_art_psm;
1963         } else {
1964           APPL_TRACE_ERROR("%s: can not find LCB!!", __func__);
1965         }
1966       } else if (p_scb->use_rc) {
1967         /* can not find AVRC on peer device. report failure */
1968         p_scb->use_rc = false;
1969         tBTA_AV_RC_OPEN rc_open;
1970         rc_open.peer_addr = p_scb->PeerAddress();
1971         rc_open.peer_features = 0;
1972         rc_open.cover_art_psm = 0;
1973         rc_open.status = BTA_AV_FAIL_SDP;
1974         tBTA_AV bta_av_data;
1975         bta_av_data.rc_open = rc_open;
1976         (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, &bta_av_data);
1977       }
1978     }
1979   } else {
1980     tBTA_AV_RC_FEAT rc_feat;
1981     p_cb->rcb[rc_handle].peer_features = peer_features;
1982     rc_feat.rc_handle = rc_handle;
1983     rc_feat.peer_features = peer_features;
1984     if (p_scb == NULL) {
1985       /*
1986        * In case scb is not created by the time we are done with SDP
1987        * we still need to send RC feature event. So we need to get BD
1988        * from Message.  Note that lidx is 1 based not 0 based
1989        */
1990       rc_feat.peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
1991     } else {
1992       rc_feat.peer_addr = p_scb->PeerAddress();
1993     }
1994
1995     tBTA_AV bta_av_feat;
1996     bta_av_feat.rc_feat = rc_feat;
1997     (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, &bta_av_feat);
1998
1999     // Send PSM data
2000     APPL_TRACE_DEBUG("%s: Send PSM data", __func__);
2001     tBTA_AV_RC_PSM rc_psm;
2002     p_cb->rcb[rc_handle].cover_art_psm = cover_art_psm;
2003     rc_psm.rc_handle = rc_handle;
2004     rc_psm.cover_art_psm = cover_art_psm;
2005     if (p_scb == NULL) {
2006       rc_psm.peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2007     } else {
2008       rc_psm.peer_addr = p_scb->PeerAddress();
2009     }
2010
2011     APPL_TRACE_DEBUG("%s: rc_psm = 0x%x", __func__, rc_psm.cover_art_psm);
2012
2013     tBTA_AV bta_av_psm;
2014     bta_av_psm.rc_cover_art_psm = rc_psm;
2015     (*p_cb->p_cback)(BTA_AV_RC_PSM_EVT, &bta_av_psm);
2016   }
2017 }
2018
2019 /*******************************************************************************
2020  *
2021  * Function         bta_av_rc_closed
2022  *
2023  * Description      Set AVRCP state to closed.
2024  *
2025  * Returns          void
2026  *
2027  ******************************************************************************/
2028 void bta_av_rc_closed(tBTA_AV_DATA* p_data) {
2029   tBTA_AV_CB* p_cb = &bta_av_cb;
2030   tBTA_AV_RC_CLOSE rc_close;
2031   tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2032   tBTA_AV_RCB* p_rcb;
2033   tBTA_AV_SCB* p_scb;
2034   int i;
2035   bool conn = false;
2036   tBTA_AV_LCB* p_lcb;
2037
2038   rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
2039   p_scb = NULL;
2040   APPL_TRACE_DEBUG("%s: rc_handle:%d", __func__, p_msg->handle);
2041   for (i = 0; i < BTA_AV_NUM_RCB; i++) {
2042     p_rcb = &p_cb->rcb[i];
2043     APPL_TRACE_DEBUG("%s: rcb[%d] rc_handle:%d, status=0x%x", __func__, i,
2044                      p_rcb->handle, p_rcb->status);
2045     if (p_rcb->handle == p_msg->handle) {
2046       rc_close.rc_handle = i;
2047       p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
2048       p_rcb->peer_features = 0;
2049       p_rcb->cover_art_psm = 0;
2050       APPL_TRACE_DEBUG("%s: shdl:%d, lidx:%d", __func__, p_rcb->shdl,
2051                        p_rcb->lidx);
2052       if (p_rcb->shdl) {
2053         if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS) {
2054           p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
2055         }
2056         if (p_scb) {
2057           rc_close.peer_addr = p_scb->PeerAddress();
2058           if (p_scb->rc_handle == p_rcb->handle)
2059             p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
2060           APPL_TRACE_DEBUG("%s: shdl:%d, srch:%d", __func__, p_rcb->shdl,
2061                            p_scb->rc_handle);
2062         }
2063         p_rcb->shdl = 0;
2064       } else if (p_rcb->lidx == (BTA_AV_NUM_LINKS + 1)) {
2065         /* if the RCB uses the extra LCB, use the addr for event and clean it */
2066         p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
2067         rc_close.peer_addr = p_msg->peer_addr;
2068         LOG_INFO("%s: rc_only closed bd_addr: %s", __func__,
2069                  p_msg->peer_addr.ToString().c_str());
2070         p_lcb->conn_msk = 0;
2071         p_lcb->lidx = 0;
2072       }
2073       p_rcb->lidx = 0;
2074
2075       if ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) {
2076         /* AVCT CCB is deallocated */
2077         p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
2078         p_rcb->status = 0;
2079       } else {
2080         /* AVCT CCB is still there. dealloc */
2081         bta_av_del_rc(p_rcb);
2082       }
2083     } else if ((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) &&
2084                (p_rcb->status & BTA_AV_RC_CONN_MASK)) {
2085       /* at least one channel is still connected */
2086       conn = true;
2087     }
2088   }
2089
2090   if (!conn) {
2091     /* no AVRC channels are connected, go back to INIT state */
2092     bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
2093   }
2094
2095   if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE) {
2096     rc_close.rc_handle = p_msg->handle;
2097     rc_close.peer_addr = p_msg->peer_addr;
2098   }
2099   tBTA_AV bta_av_data;
2100   bta_av_data.rc_close = rc_close;
2101   (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, &bta_av_data);
2102   if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE
2103                   && bta_av_cb.features & BTA_AV_FEAT_RCTG)
2104       bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
2105 }
2106
2107 /*******************************************************************************
2108  *
2109  * Function         bta_av_rc_browse_opened
2110  *
2111  * Description      AVRC browsing channel is opened
2112  *
2113  * Returns          void
2114  *
2115  ******************************************************************************/
2116 void bta_av_rc_browse_opened(tBTA_AV_DATA* p_data) {
2117   tBTA_AV_CB* p_cb = &bta_av_cb;
2118   tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2119   tBTA_AV_RC_BROWSE_OPEN rc_browse_open;
2120
2121   LOG_INFO("%s: peer_addr: %s rc_handle:%d", __func__,
2122            p_msg->peer_addr.ToString().c_str(), p_msg->handle);
2123
2124   rc_browse_open.status = BTA_AV_SUCCESS;
2125   rc_browse_open.rc_handle = p_msg->handle;
2126   rc_browse_open.peer_addr = p_msg->peer_addr;
2127
2128   tBTA_AV bta_av_data;
2129   bta_av_data.rc_browse_open = rc_browse_open;
2130   (*p_cb->p_cback)(BTA_AV_RC_BROWSE_OPEN_EVT, &bta_av_data);
2131 }
2132
2133 /*******************************************************************************
2134  *
2135  * Function         bta_av_rc_browse_closed
2136  *
2137  * Description      AVRC browsing channel is closed
2138  *
2139  * Returns          void
2140  *
2141  ******************************************************************************/
2142 void bta_av_rc_browse_closed(tBTA_AV_DATA* p_data) {
2143   tBTA_AV_CB* p_cb = &bta_av_cb;
2144   tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2145   tBTA_AV_RC_BROWSE_CLOSE rc_browse_close;
2146
2147   LOG_INFO("%s: peer_addr: %s rc_handle:%d", __func__,
2148            p_msg->peer_addr.ToString().c_str(), p_msg->handle);
2149
2150   rc_browse_close.rc_handle = p_msg->handle;
2151   rc_browse_close.peer_addr = p_msg->peer_addr;
2152
2153   tBTA_AV bta_av_data;
2154   bta_av_data.rc_browse_close = rc_browse_close;
2155   (*p_cb->p_cback)(BTA_AV_RC_BROWSE_CLOSE_EVT, &bta_av_data);
2156 }
2157
2158 /*******************************************************************************
2159  *
2160  * Function         bta_av_rc_disc
2161  *
2162  * Description      start AVRC SDP discovery.
2163  *
2164  * Returns          void
2165  *
2166  ******************************************************************************/
2167 void bta_av_rc_disc(uint8_t disc) {
2168   tBTA_AV_CB* p_cb = &bta_av_cb;
2169   tAVRC_SDP_DB_PARAMS db_params;
2170   uint16_t attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
2171                           ATTR_ID_BT_PROFILE_DESC_LIST,
2172                           ATTR_ID_SUPPORTED_FEATURES,
2173                           ATTR_ID_ADDITION_PROTO_DESC_LISTS};
2174   uint8_t hdi;
2175   tBTA_AV_SCB* p_scb;
2176   RawAddress peer_addr = RawAddress::kEmpty;
2177   uint8_t rc_handle;
2178
2179   APPL_TRACE_DEBUG("%s: disc: 0x%x, bta_av_cb.disc: 0x%x", __func__, disc,
2180                    bta_av_cb.disc);
2181   if ((bta_av_cb.disc != 0) || (disc == 0)) return;
2182
2183   if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
2184     /* this is the rc handle/index to tBTA_AV_RCB */
2185     rc_handle = disc & (~BTA_AV_CHNL_MSK);
2186     if (p_cb->rcb[rc_handle].lidx) {
2187       peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2188     }
2189   } else {
2190     hdi = (disc & BTA_AV_HNDL_MSK) - 1;
2191     p_scb = p_cb->p_scb[hdi];
2192
2193     if (p_scb) {
2194       APPL_TRACE_DEBUG("%s: rc_handle %d", __func__, p_scb->rc_handle);
2195       peer_addr = p_scb->PeerAddress();
2196     }
2197   }
2198
2199   if (!peer_addr.IsEmpty()) {
2200     /* allocate discovery database */
2201     if (p_cb->p_disc_db == NULL)
2202       p_cb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BTA_AV_DISC_BUF_SIZE);
2203
2204     /* set up parameters */
2205     db_params.db_len = BTA_AV_DISC_BUF_SIZE;
2206     db_params.num_attr = sizeof(attr_list) / sizeof(uint16_t);
2207     db_params.p_db = p_cb->p_disc_db;
2208     db_params.p_attrs = attr_list;
2209
2210     /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
2211     if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, peer_addr,
2212                          &db_params,
2213                          base::Bind(bta_av_avrc_sdp_cback)) == AVRC_SUCCESS) {
2214       p_cb->disc = disc;
2215       APPL_TRACE_DEBUG("%s: disc 0x%x", __func__, p_cb->disc);
2216     }
2217   }
2218 }
2219
2220 /*******************************************************************************
2221  *
2222  * Function         bta_av_dereg_comp
2223  *
2224  * Description      deregister complete. free the stream control block.
2225  *
2226  * Returns          void
2227  *
2228  ******************************************************************************/
2229 void bta_av_dereg_comp(tBTA_AV_DATA* p_data) {
2230   tBTA_AV_CB* p_cb = &bta_av_cb;
2231   tBTA_AV_SCB* p_scb;
2232   tBTA_UTL_COD cod;
2233   uint8_t mask;
2234   BT_HDR* p_buf;
2235
2236   /* find the stream control block */
2237   p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
2238
2239   if (p_scb) {
2240     APPL_TRACE_DEBUG("%s: deregistered %d(h%d)", __func__, p_scb->chnl,
2241                      p_scb->hndl);
2242     mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
2243     p_cb->reg_audio &= ~mask;
2244     if ((p_cb->conn_audio & mask) && p_cb->audio_open_cnt) {
2245       /* this channel is still marked as open. decrease the count */
2246       p_cb->audio_open_cnt--;
2247     }
2248     p_cb->conn_audio &= ~mask;
2249
2250     if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM && p_scb->a2dp_list) {
2251       /* make sure no buffers are in a2dp_list */
2252       while (!list_is_empty(p_scb->a2dp_list)) {
2253         p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
2254         list_remove(p_scb->a2dp_list, p_buf);
2255         osi_free(p_buf);
2256       }
2257     }
2258
2259     /* remove the A2DP SDP record, if no more audio stream is left */
2260     if (!p_cb->reg_audio) {
2261
2262       /* Only remove the SDP record if we're the ones that created it */
2263       if (is_new_avrcp_enabled()) {
2264         APPL_TRACE_DEBUG("%s: newavrcp is the owner of the AVRCP Target SDP "
2265             "record. Don't dereg the SDP record", __func__);
2266       } else {
2267         APPL_TRACE_DEBUG("%s: newavrcp is not enabled. Remove SDP record",
2268             __func__);
2269         bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL);
2270       }
2271
2272       if (p_cb->sdp_a2dp_handle) {
2273         bta_av_del_sdp_rec(&p_cb->sdp_a2dp_handle);
2274         p_cb->sdp_a2dp_handle = 0;
2275         bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
2276       }
2277
2278 #if (BTA_AV_SINK_INCLUDED == TRUE)
2279       if (p_cb->sdp_a2dp_snk_handle) {
2280         bta_av_del_sdp_rec(&p_cb->sdp_a2dp_snk_handle);
2281         p_cb->sdp_a2dp_snk_handle = 0;
2282         bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
2283       }
2284 #endif
2285     }
2286
2287     bta_av_free_scb(p_scb);
2288   }
2289
2290   APPL_TRACE_DEBUG("%s: audio 0x%x, disable:%d", __func__, p_cb->reg_audio,
2291                    p_cb->disabling);
2292   /* if no stream control block is active */
2293   if (p_cb->reg_audio == 0) {
2294     /* deregister from AVDT */
2295     bta_ar_dereg_avdt();
2296
2297     /* deregister from AVCT */
2298     bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
2299     bta_ar_dereg_avct();
2300
2301     if (p_cb->disabling) {
2302       p_cb->disabling = false;
2303       // reset enabling parameters
2304       p_cb->features = 0;
2305       p_cb->sec_mask = 0;
2306     }
2307
2308     /* Clear the Capturing service class bit */
2309     cod.service = BTM_COD_SERVICE_CAPTURING;
2310     utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
2311   }
2312 }