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 ******************************************************************************/
bta_av_get_rcb_by_shdl(uint8_t shdl)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 ******************************************************************************/
bta_av_del_rc(tBTA_AV_RCB * p_rcb)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 ******************************************************************************/
bta_av_close_all_rc(tBTA_AV_CB * p_cb)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 ******************************************************************************/
bta_av_del_sdp_rec(uint32_t * p_sdp_handle)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 ******************************************************************************/
bta_av_avrc_sdp_cback(UNUSED_ATTR uint16_t status)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 ******************************************************************************/
bta_av_rc_ctrl_cback(uint8_t handle,uint8_t event,UNUSED_ATTR uint16_t result,const RawAddress * peer_addr)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 ******************************************************************************/
bta_av_rc_msg_cback(uint8_t handle,uint8_t label,uint8_t opcode,tAVRC_MSG * p_msg)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 ******************************************************************************/
bta_av_rc_create(tBTA_AV_CB * p_cb,uint8_t role,uint8_t shdl,uint8_t lidx)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 ******************************************************************************/
bta_av_group_navi_supported(uint8_t len,uint8_t * p_data,bool is_inquiry)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 ******************************************************************************/
bta_av_op_supported(tBTA_AV_RC rc_id,bool is_inquiry)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 ******************************************************************************/
bta_av_find_lcb(const RawAddress & addr,uint8_t op)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 ******************************************************************************/
bta_av_rc_opened(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_remote_cmd(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_vendor_cmd(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_vendor_rsp(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_meta_rsp(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_free_rsp(UNUSED_ATTR tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_free_browse_msg(UNUSED_ATTR tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR * p_vendor)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 ******************************************************************************/
bta_av_proc_meta_cmd(tAVRC_RESPONSE * p_rc_rsp,tBTA_AV_RC_MSG * p_msg,uint8_t * p_ctype)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 ******************************************************************************/
bta_av_rc_msg(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_close(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_rc_browse_close(tBTA_AV_CB * p_cb,tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_get_shdl(tBTA_AV_SCB * p_scb)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 ******************************************************************************/
bta_av_stream_chg(tBTA_AV_SCB * p_scb,bool started)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 ******************************************************************************/
bta_av_conn_chg(tBTA_AV_DATA * p_data)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 ******************************************************************************/
bta_av_disable(tBTA_AV_CB * p_cb,UNUSED_ATTR tBTA_AV_DATA * p_data)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 // Free signalling timers
1309 alarm_free(p_cb->p_scb[xx]->link_signalling_timer);
1310 p_cb->p_scb[xx]->link_signalling_timer = NULL;
1311 alarm_free(p_cb->p_scb[xx]->accept_signalling_timer);
1312 p_cb->p_scb[xx]->accept_signalling_timer = NULL;
1313
1314 hdr.layer_specific = xx + 1;
1315 bta_av_api_deregister((tBTA_AV_DATA*)&hdr);
1316 disabling_in_progress = true;
1317 }
1318 }
1319 // Since All channels are deregistering by API_DEREGISTER, the DEREG_COMP_EVT
1320 // would come first before API_DISABLE if there is no connections, and it is
1321 // no needed to setup this disabling flag.
1322 p_cb->disabling = disabling_in_progress;
1323
1324 }
1325
1326 /*******************************************************************************
1327 *
1328 * Function bta_av_api_disconnect
1329 *
1330 * Description .
1331 *
1332 * Returns void
1333 *
1334 ******************************************************************************/
bta_av_api_disconnect(tBTA_AV_DATA * p_data)1335 void bta_av_api_disconnect(tBTA_AV_DATA* p_data) {
1336 tBTA_AV_SCB* p_scb =
1337 bta_av_hndl_to_scb(p_data->api_discnt.hdr.layer_specific);
1338 AVDT_DisconnectReq(p_scb->PeerAddress(), bta_av_conn_cback);
1339 alarm_cancel(p_scb->link_signalling_timer);
1340 }
1341
1342 /**
1343 * Find the index for the free LCB entry to use.
1344 *
1345 * The selection order is:
1346 * (1) Find the index if there is already SCB entry for the peer address
1347 * (2) If there is no SCB entry for the peer address, find the first
1348 * SCB entry that is not assigned.
1349 *
1350 * @param peer_address the peer address to use
1351 * @return the index for the free LCB entry to use or BTA_AV_NUM_LINKS
1352 * if no entry is found
1353 */
bta_av_find_lcb_index_by_scb_and_address(const RawAddress & peer_address)1354 static uint8_t bta_av_find_lcb_index_by_scb_and_address(
1355 const RawAddress& peer_address) {
1356 APPL_TRACE_DEBUG("%s: peer_address: %s conn_lcb: 0x%x", __func__,
1357 peer_address.ToString().c_str(), bta_av_cb.conn_lcb);
1358
1359 // Find the index if there is already SCB entry for the peer address
1360 for (uint8_t index = 0; index < BTA_AV_NUM_LINKS; index++) {
1361 uint8_t mask = 1 << index;
1362 if (mask & bta_av_cb.conn_lcb) {
1363 continue;
1364 }
1365 tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
1366 if (p_scb == nullptr) {
1367 continue;
1368 }
1369 if (p_scb->PeerAddress() == peer_address) {
1370 return index;
1371 }
1372 }
1373
1374 // Find the first SCB entry that is not assigned.
1375 for (uint8_t index = 0; index < BTA_AV_NUM_LINKS; index++) {
1376 uint8_t mask = 1 << index;
1377 if (mask & bta_av_cb.conn_lcb) {
1378 continue;
1379 }
1380 tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
1381 if (p_scb == nullptr) {
1382 continue;
1383 }
1384 if (!p_scb->IsAssigned()) {
1385 return index;
1386 }
1387 }
1388
1389 return BTA_AV_NUM_LINKS;
1390 }
1391
1392 /*******************************************************************************
1393 *
1394 * Function bta_av_sig_chg
1395 *
1396 * Description process AVDT signal channel up/down.
1397 *
1398 * Returns void
1399 *
1400 ******************************************************************************/
bta_av_sig_chg(tBTA_AV_DATA * p_data)1401 void bta_av_sig_chg(tBTA_AV_DATA* p_data) {
1402 uint16_t event = p_data->str_msg.hdr.layer_specific;
1403 tBTA_AV_CB* p_cb = &bta_av_cb;
1404 uint32_t xx;
1405 uint8_t mask;
1406 tBTA_AV_LCB* p_lcb = NULL;
1407
1408 APPL_TRACE_DEBUG("%s: event: %d", __func__, event);
1409 if (event == AVDT_CONNECT_IND_EVT) {
1410 APPL_TRACE_DEBUG("%s: AVDT_CONNECT_IND_EVT: peer %s", __func__,
1411 p_data->str_msg.bd_addr.ToString().c_str());
1412
1413 p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1414 if (!p_lcb) {
1415 /* if the address does not have an LCB yet, alloc one */
1416 xx = bta_av_find_lcb_index_by_scb_and_address(p_data->str_msg.bd_addr);
1417
1418 /* check if we found something */
1419 if (xx >= BTA_AV_NUM_LINKS) {
1420 /* We do not have scb for this avdt connection. */
1421 /* Silently close the connection. */
1422 APPL_TRACE_ERROR("%s: av scb not available for avdt connection for %s",
1423 __func__, p_data->str_msg.bd_addr.ToString().c_str());
1424 AVDT_DisconnectReq(p_data->str_msg.bd_addr, NULL);
1425 return;
1426 }
1427 LOG_INFO("%s: AVDT_CONNECT_IND_EVT: peer %s selected lcb_index %d",
1428 __func__, p_data->str_msg.bd_addr.ToString().c_str(), xx);
1429
1430 tBTA_AV_SCB* p_scb = p_cb->p_scb[xx];
1431 mask = 1 << xx;
1432 p_lcb = &p_cb->lcb[xx];
1433 p_lcb->lidx = xx + 1;
1434 p_lcb->addr = p_data->str_msg.bd_addr;
1435 p_lcb->conn_msk = 0; /* clear the connect mask */
1436 /* start listening when the signal channel is open */
1437 if (p_cb->features & BTA_AV_FEAT_RCTG) {
1438 bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1439 }
1440 /* this entry is not used yet. */
1441 p_cb->conn_lcb |= mask; /* mark it as used */
1442 APPL_TRACE_DEBUG("%s: start sig timer %d", __func__, p_data->hdr.offset);
1443 if (p_data->hdr.offset == AVDT_ACP) {
1444 APPL_TRACE_DEBUG("%s: Incoming L2CAP acquired, set state as incoming",
1445 __func__);
1446 p_scb->OnConnected(p_data->str_msg.bd_addr);
1447 p_scb->use_rc = true; /* allowing RC for incoming connection */
1448 bta_av_ssm_execute(p_scb, BTA_AV_ACP_CONNECT_EVT, p_data);
1449
1450 /* The Pending Event should be sent as soon as the L2CAP signalling
1451 * channel
1452 * is set up, which is NOW. Earlier this was done only after
1453 * BTA_AV_SIGNALLING_TIMEOUT_MS.
1454 * The following function shall send the event and start the
1455 * recurring timer
1456 */
1457 if (!p_scb->link_signalling_timer) {
1458 p_scb->link_signalling_timer = alarm_new("link_signalling_timer");
1459 }
1460 BT_HDR hdr;
1461 hdr.layer_specific = p_scb->hndl;
1462 bta_av_signalling_timer((tBTA_AV_DATA*)&hdr);
1463
1464 APPL_TRACE_DEBUG("%s: Re-start timer for AVDTP service", __func__);
1465 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
1466 /* Possible collision : need to avoid outgoing processing while the
1467 * timer is running */
1468 p_scb->coll_mask = BTA_AV_COLL_INC_TMR;
1469 if (!p_scb->accept_signalling_timer) {
1470 p_scb->accept_signalling_timer = alarm_new("accept_signalling_timer");
1471 }
1472 alarm_set_on_mloop(
1473 p_scb->accept_signalling_timer, BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS,
1474 bta_av_accept_signalling_timer_cback, UINT_TO_PTR(xx));
1475 }
1476 }
1477 }
1478 else if (event == BTA_AR_AVDT_CONN_EVT) {
1479 uint8_t scb_index = p_data->str_msg.scb_index;
1480 alarm_cancel(p_cb->p_scb[scb_index]->link_signalling_timer);
1481 }
1482 else {
1483 /* disconnected. */
1484 APPL_TRACE_DEBUG("%s: bta_av_cb.conn_lcb=0x%x", __func__,
1485 bta_av_cb.conn_lcb);
1486
1487 p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1488 if (p_lcb && (p_lcb->conn_msk || bta_av_cb.conn_lcb)) {
1489 APPL_TRACE_DEBUG("%s: conn_msk: 0x%x", __func__, p_lcb->conn_msk);
1490 /* clean up ssm */
1491 for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
1492 if (p_cb->p_scb[xx] &&
1493 p_cb->p_scb[xx]->PeerAddress() == p_data->str_msg.bd_addr) {
1494 APPL_TRACE_DEBUG("%s: Closing timer for AVDTP service", __func__);
1495 bta_sys_conn_close(BTA_ID_AV, p_cb->p_scb[xx]->app_id,
1496 p_cb->p_scb[xx]->PeerAddress());
1497 }
1498 mask = 1 << (xx + 1);
1499 if (((mask & p_lcb->conn_msk) || bta_av_cb.conn_lcb) &&
1500 p_cb->p_scb[xx] &&
1501 p_cb->p_scb[xx]->PeerAddress() == p_data->str_msg.bd_addr) {
1502 APPL_TRACE_WARNING("%s: Sending AVDT_DISCONNECT_EVT peer_addr=%s",
1503 __func__,
1504 p_cb->p_scb[xx]->PeerAddress().ToString().c_str());
1505 bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1506 }
1507 }
1508 }
1509 }
1510 APPL_TRACE_DEBUG("%s: bta_av_cb.conn_lcb=0x%x after sig_chg", __func__,
1511 p_cb->conn_lcb);
1512 }
1513
1514 /*******************************************************************************
1515 *
1516 * Function bta_av_signalling_timer
1517 *
1518 * Description process the signal channel timer. This timer is started
1519 * when the AVDTP signal channel is connected. If no profile
1520 * is connected, the timer goes off every
1521 * BTA_AV_SIGNALLING_TIMEOUT_MS.
1522 *
1523 * Returns void
1524 *
1525 ******************************************************************************/
bta_av_signalling_timer(UNUSED_ATTR tBTA_AV_DATA * p_data)1526 void bta_av_signalling_timer(UNUSED_ATTR tBTA_AV_DATA* p_data) {
1527 tBTA_AV_HNDL hndl = p_data->hdr.layer_specific;
1528 tBTA_AV_SCB* p_scb = bta_av_hndl_to_scb(hndl);
1529
1530 tBTA_AV_CB* p_cb = &bta_av_cb;
1531 int xx;
1532 uint8_t mask;
1533 tBTA_AV_LCB* p_lcb = NULL;
1534
1535 APPL_TRACE_DEBUG("%s: conn_lcb=0x%x", __func__, p_cb->conn_lcb);
1536 for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
1537 p_lcb = &p_cb->lcb[xx];
1538 mask = 1 << xx;
1539 APPL_TRACE_DEBUG(
1540 "%s: index=%d conn_lcb=0x%x peer=%s conn_mask=0x%x lidx=%d", __func__,
1541 xx, p_cb->conn_lcb, p_lcb->addr.ToString().c_str(), p_lcb->conn_msk,
1542 p_lcb->lidx);
1543 if (mask & p_cb->conn_lcb) {
1544 /* this entry is used. check if it is connected */
1545 if (!p_lcb->conn_msk) {
1546 APPL_TRACE_DEBUG("%s hndl 0x%x", __func__, p_scb->hndl);
1547 bta_sys_start_timer(p_scb->link_signalling_timer,
1548 BTA_AV_SIGNALLING_TIMEOUT_MS,
1549 BTA_AV_SIGNALLING_TIMER_EVT, hndl);
1550 tBTA_AV_PEND pend;
1551 pend.bd_addr = p_lcb->addr;
1552 tBTA_AV bta_av_data;
1553 bta_av_data.pend = pend;
1554 APPL_TRACE_DEBUG(
1555 "%s: BTA_AV_PENDING_EVT for %s index=%d conn_mask=0x%x lidx=%d",
1556 __func__, pend.bd_addr.ToString().c_str(), xx, p_lcb->conn_msk,
1557 p_lcb->lidx);
1558 (*p_cb->p_cback)(BTA_AV_PENDING_EVT, &bta_av_data);
1559 }
1560 }
1561 }
1562 }
1563
1564 /*******************************************************************************
1565 *
1566 * Function bta_av_accept_signalling_timer_cback
1567 *
1568 * Description Process the timeout when SRC is accepting connection
1569 * and SNK did not start signalling.
1570 *
1571 * Returns void
1572 *
1573 ******************************************************************************/
bta_av_accept_signalling_timer_cback(void * data)1574 static void bta_av_accept_signalling_timer_cback(void* data) {
1575 uint32_t inx = PTR_TO_UINT(data);
1576 tBTA_AV_CB* p_cb = &bta_av_cb;
1577 tBTA_AV_SCB* p_scb = NULL;
1578 if (inx < BTA_AV_NUM_STRS) {
1579 p_scb = p_cb->p_scb[inx];
1580 }
1581 if (p_scb) {
1582 APPL_TRACE_DEBUG("%s: coll_mask=0x%02x", __func__, p_scb->coll_mask);
1583
1584 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1585 p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1586
1587 if (bta_av_is_scb_opening(p_scb)) {
1588 APPL_TRACE_DEBUG("%s: stream state opening: SDP started = %d", __func__,
1589 p_scb->sdp_discovery_started);
1590 if (p_scb->sdp_discovery_started) {
1591 /* We are still doing SDP. Run the timer again. */
1592 p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1593
1594 alarm_set_on_mloop(p_scb->accept_signalling_timer,
1595 BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS,
1596 bta_av_accept_signalling_timer_cback,
1597 UINT_TO_PTR(inx));
1598 } else {
1599 /* SNK did not start signalling, resume signalling process. */
1600 bta_av_discover_req(p_scb, NULL);
1601 }
1602 } else if (bta_av_is_scb_incoming(p_scb)) {
1603 /* Stay in incoming state if SNK does not start signalling */
1604
1605 APPL_TRACE_DEBUG("%s: stream state incoming", __func__);
1606 /* API open was called right after SNK opened L2C connection. */
1607 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED) {
1608 p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1609
1610 /* BTA_AV_API_OPEN_EVT */
1611 tBTA_AV_API_OPEN* p_buf =
1612 (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
1613 memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1614 bta_sys_sendmsg(p_buf);
1615 }
1616 }
1617 }
1618 }
1619 }
1620
1621 /*******************************************************************************
1622 *
1623 * Function bta_av_check_peer_features
1624 *
1625 * Description check supported features on the peer device from the SDP
1626 * record and return the feature mask
1627 *
1628 * Returns tBTA_AV_FEAT peer device feature mask
1629 *
1630 ******************************************************************************/
bta_av_check_peer_features(uint16_t service_uuid)1631 tBTA_AV_FEAT bta_av_check_peer_features(uint16_t service_uuid) {
1632 tBTA_AV_FEAT peer_features = 0;
1633 tBTA_AV_CB* p_cb = &bta_av_cb;
1634 tSDP_DISC_REC* p_rec = NULL;
1635 tSDP_DISC_ATTR* p_attr;
1636 uint16_t peer_rc_version = 0;
1637 uint16_t categories = 0;
1638
1639 APPL_TRACE_DEBUG("%s: service_uuid:x%x", __func__, service_uuid);
1640 /* loop through all records we found */
1641 while (true) {
1642 /* get next record; if none found, we're done */
1643 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec);
1644 if (p_rec == NULL) {
1645 break;
1646 }
1647
1648 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) !=
1649 NULL) {
1650 /* find peer features */
1651 if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1652 NULL)) {
1653 peer_features |= BTA_AV_FEAT_RCCT;
1654 }
1655 if (SDP_FindServiceInDb(p_cb->p_disc_db,
1656 UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1657 peer_features |= BTA_AV_FEAT_RCTG;
1658 }
1659 }
1660
1661 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1662 /* get profile version (if failure, version parameter is not updated) */
1663 SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1664 &peer_rc_version);
1665 APPL_TRACE_DEBUG("%s: peer_rc_version 0x%x", __func__, peer_rc_version);
1666
1667 if (peer_rc_version >= AVRC_REV_1_3)
1668 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1669
1670 if (peer_rc_version >= AVRC_REV_1_4) {
1671 /* get supported categories */
1672 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
1673 if (p_attr != NULL) {
1674 categories = p_attr->attr_value.v.u16;
1675 if (categories & AVRC_SUPF_CT_CAT2)
1676 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1677 if (categories & AVRC_SUPF_CT_BROWSE)
1678 peer_features |= (BTA_AV_FEAT_BROWSE);
1679 }
1680 }
1681 }
1682 }
1683 APPL_TRACE_DEBUG("%s: peer_features:x%x", __func__, peer_features);
1684 return peer_features;
1685 }
1686
1687 /*******************************************************************************
1688 *
1689 * Function bta_avk_check_peer_features
1690 *
1691 * Description check supported features on the peer device from the SDP
1692 * record and return the feature mask
1693 *
1694 * Returns tBTA_AV_FEAT peer device feature mask
1695 *
1696 ******************************************************************************/
bta_avk_check_peer_features(uint16_t service_uuid)1697 tBTA_AV_FEAT bta_avk_check_peer_features(uint16_t service_uuid) {
1698 tBTA_AV_FEAT peer_features = 0;
1699 tBTA_AV_CB* p_cb = &bta_av_cb;
1700
1701 APPL_TRACE_DEBUG("%s: service_uuid:x%x", __func__, service_uuid);
1702
1703 /* loop through all records we found */
1704 tSDP_DISC_REC* p_rec =
1705 SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, NULL);
1706 while (p_rec) {
1707 APPL_TRACE_DEBUG("%s: found Service record for x%x", __func__,
1708 service_uuid);
1709
1710 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) !=
1711 NULL) {
1712 /* find peer features */
1713 if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1714 NULL)) {
1715 peer_features |= BTA_AV_FEAT_RCCT;
1716 }
1717 if (SDP_FindServiceInDb(p_cb->p_disc_db,
1718 UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1719 peer_features |= BTA_AV_FEAT_RCTG;
1720 }
1721 }
1722
1723 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1724 /* get profile version (if failure, version parameter is not updated) */
1725 uint16_t peer_rc_version = 0;
1726 bool val = SDP_FindProfileVersionInRec(
1727 p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1728 APPL_TRACE_DEBUG("%s: peer_rc_version for TG 0x%x, profile_found %d",
1729 __func__, peer_rc_version, val);
1730
1731 if (peer_rc_version >= AVRC_REV_1_3)
1732 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1733
1734 /* Get supported features */
1735 tSDP_DISC_ATTR* p_attr =
1736 SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
1737 if (p_attr != NULL) {
1738 uint16_t categories = p_attr->attr_value.v.u16;
1739 /*
1740 * Though Absolute Volume came after in 1.4 and above, but there are
1741 * few devices in market which supports absolute Volume and they are
1742 * still 1.3. To avoid IOP issuses with those devices, we check for
1743 * 1.3 as minimum version
1744 */
1745 if (peer_rc_version >= AVRC_REV_1_3) {
1746 if (categories & AVRC_SUPF_TG_CAT2)
1747 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1748 if (categories & AVRC_SUPF_TG_APP_SETTINGS)
1749 peer_features |= (BTA_AV_FEAT_APP_SETTING);
1750 if (categories & AVRC_SUPF_TG_BROWSE)
1751 peer_features |= (BTA_AV_FEAT_BROWSE);
1752 }
1753
1754 /* AVRCP Cover Artwork over BIP */
1755 if (peer_rc_version >= AVRC_REV_1_6) {
1756 if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
1757 categories & AVRC_SUPF_TG_PLAYER_COVER_ART)
1758 peer_features |= (BTA_AV_FEAT_COVER_ARTWORK);
1759 }
1760 }
1761 }
1762 /* get next record; if none found, we're done */
1763 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec);
1764 }
1765 APPL_TRACE_DEBUG("%s: peer_features:x%x", __func__, peer_features);
1766 return peer_features;
1767 }
1768
1769 /******************************************************************************
1770 *
1771 * Function bta_avk_get_cover_art_psm
1772 *
1773 * Description Get the PSM associated with the AVRCP Target cover art
1774 * feature
1775 *
1776 * Returns uint16_t PSM value used to get cover artwork, or 0x0000 if
1777 * one does not exist.
1778 *
1779 *****************************************************************************/
bta_avk_get_cover_art_psm()1780 uint16_t bta_avk_get_cover_art_psm() {
1781 APPL_TRACE_DEBUG("%s: searching for cover art psm", __func__);
1782 /* Cover Art L2CAP PSM is only available on a target device */
1783 tBTA_AV_CB* p_cb = &bta_av_cb;
1784 tSDP_DISC_REC* p_rec =
1785 SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET,
1786 NULL);
1787 while (p_rec) {
1788 tSDP_DISC_ATTR* p_attr =
1789 (SDP_FindAttributeInRec(p_rec, ATTR_ID_ADDITION_PROTO_DESC_LISTS));
1790 /*
1791 * If we have the Additional Protocol Description Lists attribute then we
1792 * specifically want the list that is an L2CAP protocol leading to OBEX.
1793 * Because the is a case where cover art is supported and browsing isn't
1794 * we need to check each list for the one we want.
1795 *
1796 * This means we need to do drop down into the protocol list and do a
1797 * "for each protocol, for each protocol element, for each protocol element
1798 * list parameter, if the parameter is L2CAP then find the PSM associated
1799 * with it, then make sure we see OBEX in that same protocol"
1800 */
1801 if (p_attr != NULL && SDP_DISC_ATTR_TYPE(p_attr->attr_len_type)
1802 == DATA_ELE_SEQ_DESC_TYPE) {
1803 // Point to first in List of protocols (i.e [(L2CAP -> AVCTP),
1804 // (L2CAP -> OBEX)])
1805 tSDP_DISC_ATTR* p_protocol_list = p_attr->attr_value.v.p_sub_attr;
1806 while (p_protocol_list != NULL) {
1807 if (SDP_DISC_ATTR_TYPE(p_protocol_list->attr_len_type)
1808 == DATA_ELE_SEQ_DESC_TYPE) {
1809 // Point to fist in list of protocol elements (i.e. [L2CAP, AVCTP])
1810 tSDP_DISC_ATTR* p_protocol =
1811 p_protocol_list->attr_value.v.p_sub_attr;
1812 bool protocol_has_obex = false;
1813 bool protocol_has_l2cap = false;
1814 uint16_t psm = 0x0000;
1815 while (p_protocol) {
1816 if (SDP_DISC_ATTR_TYPE(p_protocol->attr_len_type)
1817 == DATA_ELE_SEQ_DESC_TYPE) {
1818 // Point to first item protocol parameters list (i.e [UUID=L2CAP,
1819 // PSM=0x1234])
1820 tSDP_DISC_ATTR* p_protocol_param =
1821 p_protocol->attr_value.v.p_sub_attr;
1822 /*
1823 * Currently there's only ever one UUID and one parameter. L2cap
1824 * has a single PSM, AVCTP has a version and OBEX has nothing.
1825 * Change this if that ever changes.
1826 */
1827 uint16_t protocol_uuid = 0;
1828 uint16_t protocol_param = 0;
1829 while (p_protocol_param) {
1830 uint16_t param_type =
1831 SDP_DISC_ATTR_TYPE(p_protocol_param->attr_len_type);
1832 uint16_t param_len =
1833 SDP_DISC_ATTR_LEN(p_protocol_param->attr_len_type);
1834 if (param_type == UUID_DESC_TYPE) {
1835 protocol_uuid = p_protocol_param->attr_value.v.u16;
1836 } else if (param_type == UINT_DESC_TYPE) {
1837 protocol_param = (param_len == 2)
1838 ? p_protocol_param->attr_value.v.u16
1839 : p_protocol_param->attr_value.v.u8;
1840 } /* else dont care */
1841 p_protocol_param = p_protocol_param->p_next_attr; // next
1842 }
1843 // If we've found L2CAP then the parameter is a PSM
1844 if (protocol_uuid == UUID_PROTOCOL_L2CAP) {
1845 protocol_has_l2cap = true;
1846 psm = protocol_param;
1847 } else if (protocol_uuid == UUID_PROTOCOL_OBEX) {
1848 protocol_has_obex = true;
1849 }
1850 }
1851 // If this protocol has l2cap and obex then we're found the BIP PSM
1852 if (protocol_has_l2cap && protocol_has_obex) {
1853 APPL_TRACE_DEBUG("%s: found psm 0x%x", __func__, psm);
1854 return psm;
1855 }
1856 p_protocol = p_protocol->p_next_attr; // next protocol element
1857 }
1858 }
1859 p_protocol_list = p_protocol_list->p_next_attr; // next protocol
1860 }
1861 }
1862 /* get next record; if none found, we're done */
1863 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db,
1864 UUID_SERVCLASS_AV_REM_CTRL_TARGET, p_rec);
1865 }
1866 /* L2CAP PSM range is 0x1000-0xFFFF so 0x0000 is safe default invalid */
1867 APPL_TRACE_DEBUG("%s: could not find a BIP psm", __func__);
1868 return 0x0000;
1869 }
1870
1871 /*******************************************************************************
1872 *
1873 * Function bta_av_rc_disc_done
1874 *
1875 * Description Handle AVRCP service discovery results. If matching
1876 * service found, open AVRCP connection.
1877 *
1878 * Returns void
1879 *
1880 ******************************************************************************/
bta_av_rc_disc_done(UNUSED_ATTR tBTA_AV_DATA * p_data)1881 void bta_av_rc_disc_done(UNUSED_ATTR tBTA_AV_DATA* p_data) {
1882 tBTA_AV_CB* p_cb = &bta_av_cb;
1883 tBTA_AV_SCB* p_scb = NULL;
1884 tBTA_AV_LCB* p_lcb;
1885 uint8_t rc_handle;
1886 tBTA_AV_FEAT peer_features = 0; /* peer features mask */
1887 uint16_t cover_art_psm = 0x0000;
1888
1889 APPL_TRACE_DEBUG("%s: bta_av_rc_disc_done disc:x%x", __func__, p_cb->disc);
1890 if (!p_cb->disc) {
1891 return;
1892 }
1893
1894 if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
1895 /* this is the rc handle/index to tBTA_AV_RCB */
1896 rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1897 } else {
1898 /* Validate array index*/
1899 if (((p_cb->disc & BTA_AV_HNDL_MSK) - 1) < BTA_AV_NUM_STRS) {
1900 p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1901 }
1902 if (p_scb) {
1903 rc_handle = p_scb->rc_handle;
1904 } else {
1905 p_cb->disc = 0;
1906 return;
1907 }
1908 }
1909
1910 APPL_TRACE_DEBUG("%s: rc_handle %d", __func__, rc_handle);
1911 #if (BTA_AV_SINK_INCLUDED == TRUE)
1912 if (p_cb->sdp_a2dp_snk_handle) {
1913 /* This is Sink + CT + TG(Abs Vol) */
1914 peer_features =
1915 bta_avk_check_peer_features(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1916 APPL_TRACE_DEBUG("%s: populating rem ctrl target features %d", __func__,
1917 peer_features);
1918 if (BTA_AV_FEAT_ADV_CTRL &
1919 bta_avk_check_peer_features(UUID_SERVCLASS_AV_REMOTE_CONTROL))
1920 peer_features |= (BTA_AV_FEAT_ADV_CTRL | BTA_AV_FEAT_RCCT);
1921
1922 if (peer_features & BTA_AV_FEAT_COVER_ARTWORK)
1923 cover_art_psm = bta_avk_get_cover_art_psm();
1924
1925 APPL_TRACE_DEBUG("%s: populating rem ctrl target bip psm 0x%x", __func__,
1926 cover_art_psm);
1927 } else
1928 #endif
1929 if (p_cb->sdp_a2dp_handle) {
1930 /* check peer version and whether support CT and TG role */
1931 peer_features =
1932 bta_av_check_peer_features(UUID_SERVCLASS_AV_REMOTE_CONTROL);
1933 if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) &&
1934 ((peer_features & BTA_AV_FEAT_ADV_CTRL) == 0)) {
1935 /* if we support advance control and peer does not, check their support on
1936 * TG role
1937 * some implementation uses 1.3 on CT ans 1.4 on TG */
1938 peer_features |=
1939 bta_av_check_peer_features(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1940 }
1941
1942 /* Change our features if the remote AVRCP version is 1.3 or less */
1943 tSDP_DISC_REC* p_rec = nullptr;
1944 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db,
1945 UUID_SERVCLASS_AV_REMOTE_CONTROL, p_rec);
1946 if (p_rec != NULL &&
1947 SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST) != NULL) {
1948 /* get profile version (if failure, version parameter is not updated) */
1949 uint16_t peer_rc_version = 0xFFFF; // Don't change the AVRCP version
1950 SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1951 &peer_rc_version);
1952 if (peer_rc_version <= AVRC_REV_1_3) {
1953 APPL_TRACE_DEBUG("%s: Using AVRCP 1.3 Capabilities with remote device",
1954 __func__);
1955 p_bta_av_cfg = &bta_av_cfg_compatibility;
1956 }
1957 }
1958 }
1959
1960 p_cb->disc = 0;
1961 osi_free_and_reset((void**)&p_cb->p_disc_db);
1962
1963 APPL_TRACE_DEBUG("%s: peer_features 0x%x, features 0x%x", __func__,
1964 peer_features, p_cb->features);
1965
1966 /* if we have no rc connection */
1967 if (rc_handle == BTA_AV_RC_HANDLE_NONE) {
1968 if (p_scb) {
1969 /* if peer remote control service matches ours and USE_RC is true */
1970 if ((((p_cb->features & BTA_AV_FEAT_RCCT) &&
1971 (peer_features & BTA_AV_FEAT_RCTG)) ||
1972 ((p_cb->features & BTA_AV_FEAT_RCTG) &&
1973 (peer_features & BTA_AV_FEAT_RCCT)))) {
1974 p_lcb = bta_av_find_lcb(p_scb->PeerAddress(), BTA_AV_LCB_FIND);
1975 if (p_lcb) {
1976 rc_handle = bta_av_rc_create(p_cb, AVCT_INT,
1977 (uint8_t)(p_scb->hdi + 1), p_lcb->lidx);
1978 p_cb->rcb[rc_handle].peer_features = peer_features;
1979 p_cb->rcb[rc_handle].cover_art_psm = cover_art_psm;
1980 } else {
1981 APPL_TRACE_ERROR("%s: can not find LCB!!", __func__);
1982 }
1983 } else if (p_scb->use_rc) {
1984 /* can not find AVRC on peer device. report failure */
1985 p_scb->use_rc = false;
1986 tBTA_AV_RC_OPEN rc_open;
1987 rc_open.peer_addr = p_scb->PeerAddress();
1988 rc_open.peer_features = 0;
1989 rc_open.cover_art_psm = 0;
1990 rc_open.status = BTA_AV_FAIL_SDP;
1991 tBTA_AV bta_av_data;
1992 bta_av_data.rc_open = rc_open;
1993 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, &bta_av_data);
1994 }
1995 }
1996 } else {
1997 tBTA_AV_RC_FEAT rc_feat;
1998 p_cb->rcb[rc_handle].peer_features = peer_features;
1999 rc_feat.rc_handle = rc_handle;
2000 rc_feat.peer_features = peer_features;
2001 if (p_scb == NULL) {
2002 /*
2003 * In case scb is not created by the time we are done with SDP
2004 * we still need to send RC feature event. So we need to get BD
2005 * from Message. Note that lidx is 1 based not 0 based
2006 */
2007 rc_feat.peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2008 } else {
2009 rc_feat.peer_addr = p_scb->PeerAddress();
2010 }
2011
2012 tBTA_AV bta_av_feat;
2013 bta_av_feat.rc_feat = rc_feat;
2014 (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, &bta_av_feat);
2015
2016 // Send PSM data
2017 APPL_TRACE_DEBUG("%s: Send PSM data", __func__);
2018 tBTA_AV_RC_PSM rc_psm;
2019 p_cb->rcb[rc_handle].cover_art_psm = cover_art_psm;
2020 rc_psm.rc_handle = rc_handle;
2021 rc_psm.cover_art_psm = cover_art_psm;
2022 if (p_scb == NULL) {
2023 rc_psm.peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2024 } else {
2025 rc_psm.peer_addr = p_scb->PeerAddress();
2026 }
2027
2028 APPL_TRACE_DEBUG("%s: rc_psm = 0x%x", __func__, rc_psm.cover_art_psm);
2029
2030 tBTA_AV bta_av_psm;
2031 bta_av_psm.rc_cover_art_psm = rc_psm;
2032 (*p_cb->p_cback)(BTA_AV_RC_PSM_EVT, &bta_av_psm);
2033 }
2034 }
2035
2036 /*******************************************************************************
2037 *
2038 * Function bta_av_rc_closed
2039 *
2040 * Description Set AVRCP state to closed.
2041 *
2042 * Returns void
2043 *
2044 ******************************************************************************/
bta_av_rc_closed(tBTA_AV_DATA * p_data)2045 void bta_av_rc_closed(tBTA_AV_DATA* p_data) {
2046 tBTA_AV_CB* p_cb = &bta_av_cb;
2047 tBTA_AV_RC_CLOSE rc_close;
2048 tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2049 tBTA_AV_RCB* p_rcb;
2050 tBTA_AV_SCB* p_scb;
2051 int i;
2052 bool conn = false;
2053 tBTA_AV_LCB* p_lcb;
2054
2055 rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
2056 p_scb = NULL;
2057 APPL_TRACE_DEBUG("%s: rc_handle:%d", __func__, p_msg->handle);
2058 for (i = 0; i < BTA_AV_NUM_RCB; i++) {
2059 p_rcb = &p_cb->rcb[i];
2060 APPL_TRACE_DEBUG("%s: rcb[%d] rc_handle:%d, status=0x%x", __func__, i,
2061 p_rcb->handle, p_rcb->status);
2062 if (p_rcb->handle == p_msg->handle) {
2063 rc_close.rc_handle = i;
2064 p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
2065 p_rcb->peer_features = 0;
2066 p_rcb->cover_art_psm = 0;
2067 APPL_TRACE_DEBUG("%s: shdl:%d, lidx:%d", __func__, p_rcb->shdl,
2068 p_rcb->lidx);
2069 if (p_rcb->shdl) {
2070 if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS) {
2071 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
2072 }
2073 if (p_scb) {
2074 rc_close.peer_addr = p_scb->PeerAddress();
2075 if (p_scb->rc_handle == p_rcb->handle)
2076 p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
2077 APPL_TRACE_DEBUG("%s: shdl:%d, srch:%d", __func__, p_rcb->shdl,
2078 p_scb->rc_handle);
2079 }
2080 p_rcb->shdl = 0;
2081 } else if (p_rcb->lidx == (BTA_AV_NUM_LINKS + 1)) {
2082 /* if the RCB uses the extra LCB, use the addr for event and clean it */
2083 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
2084 rc_close.peer_addr = p_msg->peer_addr;
2085 LOG_INFO("%s: rc_only closed bd_addr: %s", __func__,
2086 p_msg->peer_addr.ToString().c_str());
2087 p_lcb->conn_msk = 0;
2088 p_lcb->lidx = 0;
2089 }
2090 p_rcb->lidx = 0;
2091
2092 if ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) {
2093 /* AVCT CCB is deallocated */
2094 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
2095 p_rcb->status = 0;
2096 } else {
2097 /* AVCT CCB is still there. dealloc */
2098 bta_av_del_rc(p_rcb);
2099 }
2100 } else if ((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) &&
2101 (p_rcb->status & BTA_AV_RC_CONN_MASK)) {
2102 /* at least one channel is still connected */
2103 conn = true;
2104 }
2105 }
2106
2107 if (!conn) {
2108 /* no AVRC channels are connected, go back to INIT state */
2109 bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
2110 }
2111
2112 if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE) {
2113 rc_close.rc_handle = p_msg->handle;
2114 rc_close.peer_addr = p_msg->peer_addr;
2115 }
2116 tBTA_AV bta_av_data;
2117 bta_av_data.rc_close = rc_close;
2118 (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, &bta_av_data);
2119 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE
2120 && bta_av_cb.features & BTA_AV_FEAT_RCTG)
2121 bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
2122 }
2123
2124 /*******************************************************************************
2125 *
2126 * Function bta_av_rc_browse_opened
2127 *
2128 * Description AVRC browsing channel is opened
2129 *
2130 * Returns void
2131 *
2132 ******************************************************************************/
bta_av_rc_browse_opened(tBTA_AV_DATA * p_data)2133 void bta_av_rc_browse_opened(tBTA_AV_DATA* p_data) {
2134 tBTA_AV_CB* p_cb = &bta_av_cb;
2135 tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2136 tBTA_AV_RC_BROWSE_OPEN rc_browse_open;
2137
2138 LOG_INFO("%s: peer_addr: %s rc_handle:%d", __func__,
2139 p_msg->peer_addr.ToString().c_str(), p_msg->handle);
2140
2141 rc_browse_open.status = BTA_AV_SUCCESS;
2142 rc_browse_open.rc_handle = p_msg->handle;
2143 rc_browse_open.peer_addr = p_msg->peer_addr;
2144
2145 tBTA_AV bta_av_data;
2146 bta_av_data.rc_browse_open = rc_browse_open;
2147 (*p_cb->p_cback)(BTA_AV_RC_BROWSE_OPEN_EVT, &bta_av_data);
2148 }
2149
2150 /*******************************************************************************
2151 *
2152 * Function bta_av_rc_browse_closed
2153 *
2154 * Description AVRC browsing channel is closed
2155 *
2156 * Returns void
2157 *
2158 ******************************************************************************/
bta_av_rc_browse_closed(tBTA_AV_DATA * p_data)2159 void bta_av_rc_browse_closed(tBTA_AV_DATA* p_data) {
2160 tBTA_AV_CB* p_cb = &bta_av_cb;
2161 tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2162 tBTA_AV_RC_BROWSE_CLOSE rc_browse_close;
2163
2164 LOG_INFO("%s: peer_addr: %s rc_handle:%d", __func__,
2165 p_msg->peer_addr.ToString().c_str(), p_msg->handle);
2166
2167 rc_browse_close.rc_handle = p_msg->handle;
2168 rc_browse_close.peer_addr = p_msg->peer_addr;
2169
2170 tBTA_AV bta_av_data;
2171 bta_av_data.rc_browse_close = rc_browse_close;
2172 (*p_cb->p_cback)(BTA_AV_RC_BROWSE_CLOSE_EVT, &bta_av_data);
2173 }
2174
2175 /*******************************************************************************
2176 *
2177 * Function bta_av_rc_disc
2178 *
2179 * Description start AVRC SDP discovery.
2180 *
2181 * Returns void
2182 *
2183 ******************************************************************************/
bta_av_rc_disc(uint8_t disc)2184 void bta_av_rc_disc(uint8_t disc) {
2185 tBTA_AV_CB* p_cb = &bta_av_cb;
2186 tAVRC_SDP_DB_PARAMS db_params;
2187 uint16_t attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
2188 ATTR_ID_BT_PROFILE_DESC_LIST,
2189 ATTR_ID_SUPPORTED_FEATURES,
2190 ATTR_ID_ADDITION_PROTO_DESC_LISTS};
2191 uint8_t hdi;
2192 tBTA_AV_SCB* p_scb;
2193 RawAddress peer_addr = RawAddress::kEmpty;
2194 uint8_t rc_handle;
2195
2196 APPL_TRACE_DEBUG("%s: disc: 0x%x, bta_av_cb.disc: 0x%x", __func__, disc,
2197 bta_av_cb.disc);
2198 if ((bta_av_cb.disc != 0) || (disc == 0)) return;
2199
2200 if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
2201 /* this is the rc handle/index to tBTA_AV_RCB */
2202 rc_handle = disc & (~BTA_AV_CHNL_MSK);
2203 if (p_cb->rcb[rc_handle].lidx) {
2204 peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2205 }
2206 } else {
2207 hdi = (disc & BTA_AV_HNDL_MSK) - 1;
2208 p_scb = p_cb->p_scb[hdi];
2209
2210 if (p_scb) {
2211 APPL_TRACE_DEBUG("%s: rc_handle %d", __func__, p_scb->rc_handle);
2212 peer_addr = p_scb->PeerAddress();
2213 }
2214 }
2215
2216 if (!peer_addr.IsEmpty()) {
2217 /* allocate discovery database */
2218 if (p_cb->p_disc_db == NULL)
2219 p_cb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BTA_AV_DISC_BUF_SIZE);
2220
2221 /* set up parameters */
2222 db_params.db_len = BTA_AV_DISC_BUF_SIZE;
2223 db_params.num_attr = sizeof(attr_list) / sizeof(uint16_t);
2224 db_params.p_db = p_cb->p_disc_db;
2225 db_params.p_attrs = attr_list;
2226
2227 /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
2228 if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, peer_addr,
2229 &db_params,
2230 base::Bind(bta_av_avrc_sdp_cback)) == AVRC_SUCCESS) {
2231 p_cb->disc = disc;
2232 APPL_TRACE_DEBUG("%s: disc 0x%x", __func__, p_cb->disc);
2233 }
2234 }
2235 }
2236
2237 /*******************************************************************************
2238 *
2239 * Function bta_av_dereg_comp
2240 *
2241 * Description deregister complete. free the stream control block.
2242 *
2243 * Returns void
2244 *
2245 ******************************************************************************/
bta_av_dereg_comp(tBTA_AV_DATA * p_data)2246 void bta_av_dereg_comp(tBTA_AV_DATA* p_data) {
2247 tBTA_AV_CB* p_cb = &bta_av_cb;
2248 tBTA_AV_SCB* p_scb;
2249 tBTA_UTL_COD cod;
2250 uint8_t mask;
2251 BT_HDR* p_buf;
2252
2253 /* find the stream control block */
2254 p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
2255
2256 if (p_scb) {
2257 APPL_TRACE_DEBUG("%s: deregistered %d(h%d)", __func__, p_scb->chnl,
2258 p_scb->hndl);
2259 mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
2260 p_cb->reg_audio &= ~mask;
2261 if ((p_cb->conn_audio & mask) && p_cb->audio_open_cnt) {
2262 /* this channel is still marked as open. decrease the count */
2263 p_cb->audio_open_cnt--;
2264 }
2265 p_cb->conn_audio &= ~mask;
2266
2267 if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM && p_scb->a2dp_list) {
2268 /* make sure no buffers are in a2dp_list */
2269 while (!list_is_empty(p_scb->a2dp_list)) {
2270 p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
2271 list_remove(p_scb->a2dp_list, p_buf);
2272 osi_free(p_buf);
2273 }
2274 }
2275
2276 /* remove the A2DP SDP record, if no more audio stream is left */
2277 if (!p_cb->reg_audio) {
2278
2279 /* Only remove the SDP record if we're the ones that created it */
2280 if (is_new_avrcp_enabled()) {
2281 APPL_TRACE_DEBUG("%s: newavrcp is the owner of the AVRCP Target SDP "
2282 "record. Don't dereg the SDP record", __func__);
2283 } else {
2284 APPL_TRACE_DEBUG("%s: newavrcp is not enabled. Remove SDP record",
2285 __func__);
2286 bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL);
2287 }
2288
2289 if (p_cb->sdp_a2dp_handle) {
2290 bta_av_del_sdp_rec(&p_cb->sdp_a2dp_handle);
2291 p_cb->sdp_a2dp_handle = 0;
2292 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
2293 }
2294
2295 #if (BTA_AV_SINK_INCLUDED == TRUE)
2296 if (p_cb->sdp_a2dp_snk_handle) {
2297 bta_av_del_sdp_rec(&p_cb->sdp_a2dp_snk_handle);
2298 p_cb->sdp_a2dp_snk_handle = 0;
2299 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
2300 }
2301 #endif
2302 }
2303
2304 bta_av_free_scb(p_scb);
2305 }
2306
2307 APPL_TRACE_DEBUG("%s: audio 0x%x, disable:%d", __func__, p_cb->reg_audio,
2308 p_cb->disabling);
2309 /* if no stream control block is active */
2310 if (p_cb->reg_audio == 0) {
2311 /* deregister from AVDT */
2312 bta_ar_dereg_avdt();
2313
2314 /* deregister from AVCT */
2315 bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
2316 bta_ar_dereg_avct();
2317
2318 if (p_cb->disabling) {
2319 p_cb->disabling = false;
2320 // reset enabling parameters
2321 p_cb->features = 0;
2322 p_cb->sec_mask = 0;
2323 }
2324
2325 /* Clear the Capturing service class bit */
2326 cod.service = BTM_COD_SERVICE_CAPTURING;
2327 utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
2328 }
2329 }
2330