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