btif_rc.c revision 68c53ded6523113ddcc1932fa5bcb7d378b12209
1/****************************************************************************** 2 * 3 * Copyright (C) 2009-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 20/***************************************************************************** 21 * 22 * Filename: btif_rc.c 23 * 24 * Description: Bluetooth AVRC implementation 25 * 26 *****************************************************************************/ 27#include <hardware/bluetooth.h> 28#include <fcntl.h> 29#include "bta_api.h" 30#include "bta_av_api.h" 31#include "avrc_defs.h" 32#include "bd.h" 33#include "gki.h" 34 35#define LOG_TAG "BTIF_RC" 36#include "btif_common.h" 37#include "btif_util.h" 38#include "btif_av.h" 39#include "hardware/bt_rc.h" 40#include "uinput.h" 41 42/***************************************************************************** 43** Constants & Macros 44******************************************************************************/ 45 46/* cod value for Headsets */ 47#define COD_AV_HEADSETS 0x0404 48/* for AVRC 1.4 need to change this */ 49#define MAX_RC_NOTIFICATIONS AVRC_EVT_APP_SETTING_CHANGE 50 51#define IDX_GET_PLAY_STATUS_RSP 0 52#define IDX_LIST_APP_ATTR_RSP 1 53#define IDX_LIST_APP_VALUE_RSP 2 54#define IDX_GET_CURR_APP_VAL_RSP 3 55#define IDX_SET_APP_VAL_RSP 4 56#define IDX_GET_APP_ATTR_TXT_RSP 5 57#define IDX_GET_APP_VAL_TXT_RSP 6 58#define IDX_GET_ELEMENT_ATTR_RSP 7 59#define MAX_VOLUME 128 60#define MAX_LABEL 16 61#define MAX_TRANSACTIONS_PER_SESSION 16 62#define MAX_CMD_QUEUE_LEN 8 63 64#define CHECK_RC_CONNECTED \ 65 BTIF_TRACE_DEBUG1("## %s ##", __FUNCTION__); \ 66 if(btif_rc_cb.rc_connected == FALSE) \ 67 { \ 68 BTIF_TRACE_WARNING1("Function %s() called when RC is not connected", __FUNCTION__); \ 69 return BT_STATUS_NOT_READY; \ 70 } 71 72#define FILL_PDU_QUEUE(index, ctype, label, pending) \ 73{ \ 74 btif_rc_cb.rc_pdu_info[index].ctype = ctype; \ 75 btif_rc_cb.rc_pdu_info[index].label = label; \ 76 btif_rc_cb.rc_pdu_info[index].is_rsp_pending = pending; \ 77} 78 79#define SEND_METAMSG_RSP(index, avrc_rsp) \ 80{ \ 81 if(btif_rc_cb.rc_pdu_info[index].is_rsp_pending == FALSE) \ 82 { \ 83 BTIF_TRACE_WARNING1("%s Not sending response as no PDU was registered", __FUNCTION__); \ 84 return BT_STATUS_UNHANDLED; \ 85 } \ 86 send_metamsg_rsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_pdu_info[index].label, \ 87 btif_rc_cb.rc_pdu_info[index].ctype, avrc_rsp); \ 88 btif_rc_cb.rc_pdu_info[index].ctype = 0; \ 89 btif_rc_cb.rc_pdu_info[index].label = 0; \ 90 btif_rc_cb.rc_pdu_info[index].is_rsp_pending = FALSE; \ 91} 92 93/***************************************************************************** 94** Local type definitions 95******************************************************************************/ 96typedef struct { 97 UINT8 bNotify; 98 UINT8 label; 99} btif_rc_reg_notifications_t; 100 101typedef struct 102{ 103 UINT8 label; 104 UINT8 ctype; 105 BOOLEAN is_rsp_pending; 106} btif_rc_cmd_ctxt_t; 107 108/* TODO : Merge btif_rc_reg_notifications_t and btif_rc_cmd_ctxt_t to a single struct */ 109typedef struct { 110 BOOLEAN rc_connected; 111 UINT8 rc_handle; 112 tBTA_AV_FEAT rc_features; 113 BD_ADDR rc_addr; 114 UINT16 rc_pending_play; 115 btif_rc_cmd_ctxt_t rc_pdu_info[MAX_CMD_QUEUE_LEN]; 116 btif_rc_reg_notifications_t rc_notif[MAX_RC_NOTIFICATIONS]; 117 unsigned int rc_volume; 118 uint8_t rc_vol_label; 119} btif_rc_cb_t; 120 121typedef struct { 122 BOOLEAN in_use; 123 UINT8 lbl; 124 UINT8 handle; 125} rc_transaction_t; 126 127typedef struct 128{ 129 pthread_mutex_t lbllock; 130 rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION]; 131} rc_device_t; 132 133 134rc_device_t device; 135 136#define MAX_UINPUT_PATHS 3 137static const char* uinput_dev_path[] = 138 {"/dev/uinput", "/dev/input/uinput", "/dev/misc/uinput" }; 139static int uinput_fd = -1; 140 141static int send_event (int fd, uint16_t type, uint16_t code, int32_t value); 142static void send_key (int fd, uint16_t key, int pressed); 143static int uinput_driver_check(); 144static int uinput_create(char *name); 145static int init_uinput (void); 146static void close_uinput (void); 147static BOOLEAN dev_blacklisted_for_absolute_volume(BD_ADDR peer_dev); 148 149static const struct { 150 const char *name; 151 uint8_t avrcp; 152 uint16_t mapped_id; 153 uint8_t release_quirk; 154} key_map[] = { 155 { "PLAY", AVRC_ID_PLAY, KEY_PLAYCD, 1 }, 156 { "STOP", AVRC_ID_STOP, KEY_STOPCD, 0 }, 157 { "PAUSE", AVRC_ID_PAUSE, KEY_PAUSECD, 1 }, 158 { "FORWARD", AVRC_ID_FORWARD, KEY_NEXTSONG, 0 }, 159 { "BACKWARD", AVRC_ID_BACKWARD, KEY_PREVIOUSSONG, 0 }, 160 { "REWIND", AVRC_ID_REWIND, KEY_REWIND, 0 }, 161 { "FAST FORWARD", AVRC_ID_FAST_FOR, KEY_FAST_FORWARD, 0 }, 162 { NULL, 0, 0, 0 } 163}; 164 165/* the rc_black_addr_prefix and rc_white_addr_prefix are used to correct 166 * IOP issues of absolute volume feature 167 * We encoutered A2DP headsets/carkits advertising absolute volume but buggy. 168 * We would like to blacklist those devices. 169 * But we donot have a full list of the bad devices. So as a temp fix, we 170 * are blacklisting all the devices except the devices we have well tested, 171 * the ones in the whitelist. 172 * 173 * For now, only the rc_white_addr_prefix is used in the code while 174 * rc_black_addr_prefix is kept here for future long term solution. 175 */ 176static const UINT8 rc_black_addr_prefix[][3] = { 177 {0x0, 0x18, 0x6B}, // LG HBS-730 178 {0x0, 0x26, 0x7E} // VW Passat 179}; 180 181static const UINT8 rc_white_addr_prefix[][3] = { 182 {0x94, 0xCE, 0x2C}, // Sony SBH50 183 {0x30, 0x17, 0xC8} // Sony wm600 184}; 185 186static const char* rc_white_name[] = { 187 "SBH50", 188 "MW600" 189}; 190 191static void send_reject_response (UINT8 rc_handle, UINT8 label, 192 UINT8 pdu, UINT8 status); 193static UINT8 opcode_from_pdu(UINT8 pdu); 194static void send_metamsg_rsp (UINT8 rc_handle, UINT8 label, 195 tBTA_AV_CODE code, tAVRC_RESPONSE *pmetamsg_resp); 196static void register_volumechange(UINT8 label); 197static void lbl_init(); 198static void lbl_destroy(); 199static void init_all_transactions(); 200static bt_status_t get_transaction(rc_transaction_t **ptransaction); 201static void release_transaction(UINT8 label); 202static rc_transaction_t* get_transaction_by_lbl(UINT8 label); 203static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg); 204static void btif_rc_upstreams_evt(UINT16 event, tAVRC_COMMAND* p_param, UINT8 ctype, UINT8 label); 205static void btif_rc_upstreams_rsp_evt(UINT16 event, tAVRC_RESPONSE *pavrc_resp, UINT8 ctype, UINT8 label); 206 207/***************************************************************************** 208** Static variables 209******************************************************************************/ 210static btif_rc_cb_t btif_rc_cb; 211static btrc_callbacks_t *bt_rc_callbacks = NULL; 212 213/***************************************************************************** 214** Static functions 215******************************************************************************/ 216 217/***************************************************************************** 218** Externs 219******************************************************************************/ 220extern BOOLEAN btif_hf_call_terminated_recently(); 221extern BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod); 222 223 224/***************************************************************************** 225** Functions 226******************************************************************************/ 227 228/***************************************************************************** 229** Local uinput helper functions 230******************************************************************************/ 231int send_event (int fd, uint16_t type, uint16_t code, int32_t value) 232{ 233 struct uinput_event event; 234 BTIF_TRACE_DEBUG4("%s type:%u code:%u value:%d", __FUNCTION__, 235 type, code, value); 236 memset(&event, 0, sizeof(event)); 237 event.type = type; 238 event.code = code; 239 event.value = value; 240 241 return write(fd, &event, sizeof(event)); 242} 243 244void send_key (int fd, uint16_t key, int pressed) 245{ 246 BTIF_TRACE_DEBUG4("%s fd:%d key:%u pressed:%d", __FUNCTION__, 247 fd, key, pressed); 248 249 if (fd < 0) 250 { 251 return; 252 } 253 254 BTIF_TRACE_DEBUG3("AVRCP: Send key %d (%d) fd=%d", key, pressed, fd); 255 send_event(fd, EV_KEY, key, pressed); 256 send_event(fd, EV_SYN, SYN_REPORT, 0); 257} 258 259/************** uinput related functions **************/ 260int uinput_driver_check() 261{ 262 uint32_t i; 263 for (i=0; i < MAX_UINPUT_PATHS; i++) 264 { 265 if (access(uinput_dev_path[i], O_RDWR) == 0) { 266 return 0; 267 } 268 } 269 BTIF_TRACE_ERROR1("%s ERROR: uinput device is not in the system", __FUNCTION__); 270 return -1; 271} 272 273int uinput_create(char *name) 274{ 275 struct uinput_dev dev; 276 int fd, err, x = 0; 277 278 for(x=0; x < MAX_UINPUT_PATHS; x++) 279 { 280 fd = open(uinput_dev_path[x], O_RDWR); 281 if (fd < 0) 282 continue; 283 break; 284 } 285 if (x == MAX_UINPUT_PATHS) { 286 BTIF_TRACE_ERROR1("%s ERROR: uinput device open failed", __FUNCTION__); 287 return -1; 288 } 289 memset(&dev, 0, sizeof(dev)); 290 if (name) 291 strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE); 292 293 dev.id.bustype = BUS_BLUETOOTH; 294 dev.id.vendor = 0x0000; 295 dev.id.product = 0x0000; 296 dev.id.version = 0x0000; 297 298 if (write(fd, &dev, sizeof(dev)) < 0) { 299 BTIF_TRACE_ERROR1("%s Unable to write device information", __FUNCTION__); 300 close(fd); 301 return -1; 302 } 303 304 ioctl(fd, UI_SET_EVBIT, EV_KEY); 305 ioctl(fd, UI_SET_EVBIT, EV_REL); 306 ioctl(fd, UI_SET_EVBIT, EV_SYN); 307 308 for (x = 0; key_map[x].name != NULL; x++) 309 ioctl(fd, UI_SET_KEYBIT, key_map[x].mapped_id); 310 311 for(x = 0; x < KEY_MAX; x++) 312 ioctl(fd, UI_SET_KEYBIT, x); 313 314 if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) { 315 BTIF_TRACE_ERROR1("%s Unable to create uinput device", __FUNCTION__); 316 close(fd); 317 return -1; 318 } 319 return fd; 320} 321 322int init_uinput (void) 323{ 324 char *name = "AVRCP"; 325 326 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 327 uinput_fd = uinput_create(name); 328 if (uinput_fd < 0) { 329 BTIF_TRACE_ERROR3("%s AVRCP: Failed to initialize uinput for %s (%d)", 330 __FUNCTION__, name, uinput_fd); 331 } else { 332 BTIF_TRACE_DEBUG3("%s AVRCP: Initialized uinput for %s (fd=%d)", 333 __FUNCTION__, name, uinput_fd); 334 } 335 return uinput_fd; 336} 337 338void close_uinput (void) 339{ 340 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 341 if (uinput_fd > 0) { 342 ioctl(uinput_fd, UI_DEV_DESTROY); 343 344 close(uinput_fd); 345 uinput_fd = -1; 346 } 347} 348 349void handle_rc_features() 350{ 351 btrc_remote_features_t rc_features = BTRC_FEAT_NONE; 352 bt_bdaddr_t rc_addr; 353 bdcpy(rc_addr.address, btif_rc_cb.rc_addr); 354 355 if (dev_blacklisted_for_absolute_volume(btif_rc_cb.rc_addr)) 356 { 357 btif_rc_cb.rc_features &= ~BTA_AV_FEAT_ADV_CTRL; 358 } 359 360 if (btif_rc_cb.rc_features & BTA_AV_FEAT_BROWSE) 361 { 362 rc_features |= BTRC_FEAT_BROWSE; 363 } 364 if ( (btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL) && 365 (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)) 366 { 367 rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME; 368 } 369 if (btif_rc_cb.rc_features & BTA_AV_FEAT_METADATA) 370 { 371 rc_features |= BTRC_FEAT_METADATA; 372 } 373 BTIF_TRACE_DEBUG2("%s: rc_features=0x%x", __FUNCTION__, rc_features); 374 HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features) 375 376#if (AVRC_ADV_CTRL_INCLUDED == TRUE) 377 BTIF_TRACE_DEBUG1("Checking for feature flags in btif_rc_handler with label %d", 378 btif_rc_cb.rc_vol_label); 379 // Register for volume change on connect 380 if(btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL && 381 btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG) 382 { 383 rc_transaction_t *p_transaction=NULL; 384 bt_status_t status = BT_STATUS_NOT_READY; 385 if(MAX_LABEL==btif_rc_cb.rc_vol_label) 386 { 387 status=get_transaction(&p_transaction); 388 } 389 else 390 { 391 p_transaction=get_transaction_by_lbl(btif_rc_cb.rc_vol_label); 392 if(NULL!=p_transaction) 393 { 394 BTIF_TRACE_DEBUG1("register_volumechange already in progress for label %d", 395 btif_rc_cb.rc_vol_label); 396 return; 397 } 398 else 399 status=get_transaction(&p_transaction); 400 } 401 402 if(BT_STATUS_SUCCESS == status && NULL!=p_transaction) 403 { 404 btif_rc_cb.rc_vol_label=p_transaction->lbl; 405 register_volumechange(btif_rc_cb.rc_vol_label); 406 } 407 } 408#endif 409} 410 411 412/*************************************************************************** 413 * Function handle_rc_connect 414 * 415 * - Argument: tBTA_AV_RC_OPEN RC open data structure 416 * 417 * - Description: RC connection event handler 418 * 419 ***************************************************************************/ 420void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open) 421{ 422 BTIF_TRACE_DEBUG2("%s: rc_handle: %d", __FUNCTION__, p_rc_open->rc_handle); 423 bt_status_t result = BT_STATUS_SUCCESS; 424 int i; 425 char bd_str[18]; 426 427 if(p_rc_open->status == BTA_AV_SUCCESS) 428 { 429 memcpy(btif_rc_cb.rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR)); 430 btif_rc_cb.rc_features = p_rc_open->peer_features; 431 btif_rc_cb.rc_vol_label=MAX_LABEL; 432 btif_rc_cb.rc_volume=MAX_VOLUME; 433 434 btif_rc_cb.rc_connected = TRUE; 435 btif_rc_cb.rc_handle = p_rc_open->rc_handle; 436 437 /* on locally initiated connection we will get remote features as part of connect */ 438 if (btif_rc_cb.rc_features != 0) 439 handle_rc_features(); 440 441 result = uinput_driver_check(); 442 if(result == BT_STATUS_SUCCESS) 443 { 444 init_uinput(); 445 } 446 } 447 else 448 { 449 BTIF_TRACE_ERROR2("%s Connect failed with error code: %d", 450 __FUNCTION__, p_rc_open->status); 451 btif_rc_cb.rc_connected = FALSE; 452 } 453} 454 455/*************************************************************************** 456 * Function handle_rc_disconnect 457 * 458 * - Argument: tBTA_AV_RC_CLOSE RC close data structure 459 * 460 * - Description: RC disconnection event handler 461 * 462 ***************************************************************************/ 463void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close) 464{ 465 BTIF_TRACE_DEBUG2("%s: rc_handle: %d", __FUNCTION__, p_rc_close->rc_handle); 466 467 btif_rc_cb.rc_handle = 0; 468 btif_rc_cb.rc_connected = FALSE; 469 memset(btif_rc_cb.rc_addr, 0, sizeof(BD_ADDR)); 470 memset(btif_rc_cb.rc_notif, 0, sizeof(btif_rc_cb.rc_notif)); 471 btif_rc_cb.rc_features = 0; 472 btif_rc_cb.rc_vol_label=MAX_LABEL; 473 btif_rc_cb.rc_volume=MAX_VOLUME; 474 init_all_transactions(); 475 close_uinput(); 476} 477 478/*************************************************************************** 479 * Function handle_rc_passthrough_cmd 480 * 481 * - Argument: tBTA_AV_RC rc_id remote control command ID 482 * tBTA_AV_STATE key_state status of key press 483 * 484 * - Description: Remote control command handler 485 * 486 ***************************************************************************/ 487void handle_rc_passthrough_cmd ( tBTA_AV_REMOTE_CMD *p_remote_cmd) 488{ 489 const char *status; 490 int pressed, i; 491 492 BTIF_TRACE_DEBUG2("%s: p_remote_cmd->rc_id=%d", __FUNCTION__, p_remote_cmd->rc_id); 493 494 /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up this PLAY */ 495 if (p_remote_cmd) 496 { 497 /* queue AVRC PLAY if GAVDTP Open notification to app is pending (2 second timer) */ 498 if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected())) 499 { 500 if (p_remote_cmd->key_state == AVRC_STATE_PRESS) 501 { 502 APPL_TRACE_WARNING1("%s: AVDT not open, queuing the PLAY command", __FUNCTION__); 503 btif_rc_cb.rc_pending_play = TRUE; 504 } 505 return; 506 } 507 508 if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (btif_rc_cb.rc_pending_play)) 509 { 510 APPL_TRACE_WARNING1("%s: Clear the pending PLAY on PAUSE received", __FUNCTION__); 511 btif_rc_cb.rc_pending_play = FALSE; 512 return; 513 } 514 } 515 if (p_remote_cmd->key_state == AVRC_STATE_RELEASE) { 516 status = "released"; 517 pressed = 0; 518 } else { 519 status = "pressed"; 520 pressed = 1; 521 } 522 523 /* If this is Play/Pause command (press or release) before processing, check the following 524 * a voice call has ended recently 525 * the remote device is not of type headset 526 * If the above conditions meet, drop the Play/Pause command 527 * This fix is to interop with certain carkits which sends an automatic PLAY or PAUSE 528 * commands right after call ends 529 */ 530 if((p_remote_cmd->rc_id == BTA_AV_RC_PLAY || p_remote_cmd->rc_id == BTA_AV_RC_PAUSE)&& 531 (btif_hf_call_terminated_recently() == TRUE) && 532 (check_cod( (const bt_bdaddr_t*)&(btif_rc_cb.rc_addr), COD_AV_HEADSETS) != TRUE)) 533 { 534 BTIF_TRACE_DEBUG2("%s:Dropping the play/Pause command received right after call end cmd:%d", 535 __FUNCTION__,p_remote_cmd->rc_id); 536 return; 537 } 538 539 if (p_remote_cmd->rc_id == BTA_AV_RC_FAST_FOR || p_remote_cmd->rc_id == BTA_AV_RC_REWIND) { 540 HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed); 541 return; 542 } 543 544 for (i = 0; key_map[i].name != NULL; i++) { 545 if (p_remote_cmd->rc_id == key_map[i].avrcp) { 546 BTIF_TRACE_DEBUG3("%s: %s %s", __FUNCTION__, key_map[i].name, status); 547 548 /* MusicPlayer uses a long_press_timeout of 1 second for PLAYPAUSE button 549 * and maps that to autoshuffle. So if for some reason release for PLAY/PAUSE 550 * comes 1 second after the press, the MediaPlayer UI goes into a bad state. 551 * The reason for the delay could be sniff mode exit or some AVDTP procedure etc. 552 * The fix is to generate a release right after the press and drown the 'actual' 553 * release. 554 */ 555 if ((key_map[i].release_quirk == 1) && (pressed == 0)) 556 { 557 BTIF_TRACE_DEBUG2("%s: AVRC %s Release Faked earlier, drowned now", 558 __FUNCTION__, key_map[i].name); 559 return; 560 } 561 send_key(uinput_fd, key_map[i].mapped_id, pressed); 562 if ((key_map[i].release_quirk == 1) && (pressed == 1)) 563 { 564 GKI_delay(30); // 30ms 565 BTIF_TRACE_DEBUG2("%s: AVRC %s Release quirk enabled, send release now", 566 __FUNCTION__, key_map[i].name); 567 send_key(uinput_fd, key_map[i].mapped_id, 0); 568 } 569 break; 570 } 571 } 572 573 if (key_map[i].name == NULL) 574 BTIF_TRACE_ERROR3("%s AVRCP: unknown button 0x%02X %s", __FUNCTION__, 575 p_remote_cmd->rc_id, status); 576} 577 578void handle_uid_changed_notification(tBTA_AV_META_MSG *pmeta_msg, tAVRC_COMMAND *pavrc_command) 579{ 580 tAVRC_RESPONSE avrc_rsp = {0}; 581 avrc_rsp.rsp.pdu = pavrc_command->pdu; 582 avrc_rsp.rsp.status = AVRC_STS_NO_ERROR; 583 avrc_rsp.rsp.opcode = pavrc_command->cmd.opcode; 584 585 avrc_rsp.reg_notif.event_id = pavrc_command->reg_notif.event_id; 586 avrc_rsp.reg_notif.param.uid_counter = 0; 587 588 send_metamsg_rsp(pmeta_msg->rc_handle, pmeta_msg->label, AVRC_RSP_INTERIM, &avrc_rsp); 589 send_metamsg_rsp(pmeta_msg->rc_handle, pmeta_msg->label, AVRC_RSP_CHANGED, &avrc_rsp); 590 591} 592 593 594/*************************************************************************** 595 * Function handle_rc_metamsg_cmd 596 * 597 * - Argument: tBTA_AV_VENDOR Structure containing the received 598 * metamsg command 599 * 600 * - Description: Remote control metamsg command handler (AVRCP 1.3) 601 * 602 ***************************************************************************/ 603void handle_rc_metamsg_cmd (tBTA_AV_META_MSG *pmeta_msg) 604{ 605 /* Parse the metamsg command and pass it on to BTL-IFS */ 606 UINT8 scratch_buf[512] = {0}; 607 tAVRC_COMMAND avrc_command = {0}; 608 tAVRC_STS status; 609 int param_len; 610 611 BTIF_TRACE_EVENT1("+ %s", __FUNCTION__); 612 613 if (pmeta_msg->p_msg->hdr.opcode != AVRC_OP_VENDOR) 614 { 615 BTIF_TRACE_WARNING1("Invalid opcode: %x", pmeta_msg->p_msg->hdr.opcode); 616 return; 617 } 618 if (pmeta_msg->len < 3) 619 { 620 BTIF_TRACE_WARNING2("Invalid length.Opcode: 0x%x, len: 0x%x", pmeta_msg->p_msg->hdr.opcode, 621 pmeta_msg->len); 622 return; 623 } 624 625 if (pmeta_msg->code >= AVRC_RSP_NOT_IMPL) 626 { 627#if (AVRC_ADV_CTRL_INCLUDED == TRUE) 628{ 629 rc_transaction_t *transaction=NULL; 630 transaction=get_transaction_by_lbl(pmeta_msg->label); 631 if(NULL!=transaction) 632 { 633 handle_rc_metamsg_rsp(pmeta_msg); 634 } 635 else 636 { 637 BTIF_TRACE_DEBUG3("%s:Discard vendor dependent rsp. code: %d label:%d.", 638 __FUNCTION__, pmeta_msg->code, pmeta_msg->label); 639 } 640 return; 641} 642#else 643{ 644 BTIF_TRACE_DEBUG3("%s:Received vendor dependent rsp. code: %d len: %d. Not processing it.", 645 __FUNCTION__, pmeta_msg->code, pmeta_msg->len); 646 return; 647} 648#endif 649 } 650 651 status=AVRC_ParsCommand(pmeta_msg->p_msg, &avrc_command, scratch_buf, sizeof(scratch_buf)); 652 BTIF_TRACE_DEBUG3("Received vendor command.code,PDU and label: %d, %d,%d",pmeta_msg->code, 653 avrc_command.cmd.pdu, pmeta_msg->label); 654 655 if (status != AVRC_STS_NO_ERROR) 656 { 657 /* return error */ 658 BTIF_TRACE_WARNING2("%s: Error in parsing received metamsg command. status: 0x%02x", 659 __FUNCTION__, status); 660 send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_command.pdu, status); 661 } 662 else 663 { 664 /* if RegisterNotification, add it to our registered queue */ 665 666 if (avrc_command.cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) 667 { 668 UINT8 event_id = avrc_command.reg_notif.event_id; 669 param_len = sizeof(tAVRC_REG_NOTIF_CMD); 670 BTIF_TRACE_EVENT4("%s:New register notification received.event_id:%s,label:0x%x,code:%x", 671 __FUNCTION__,dump_rc_notification_event_id(event_id), pmeta_msg->label,pmeta_msg->code); 672 btif_rc_cb.rc_notif[event_id-1].bNotify = TRUE; 673 btif_rc_cb.rc_notif[event_id-1].label = pmeta_msg->label; 674 675 if(event_id == AVRC_EVT_UIDS_CHANGE) 676 { 677 handle_uid_changed_notification(pmeta_msg, &avrc_command); 678 return; 679 } 680 681 } 682 683 BTIF_TRACE_EVENT2("%s: Passing received metamsg command to app. pdu: %s", 684 __FUNCTION__, dump_rc_pdu(avrc_command.cmd.pdu)); 685 686 /* Since handle_rc_metamsg_cmd() itself is called from 687 *btif context, no context switching is required. Invoke 688 * btif_rc_upstreams_evt directly from here. */ 689 btif_rc_upstreams_evt((uint16_t)avrc_command.cmd.pdu, &avrc_command, pmeta_msg->code, 690 pmeta_msg->label); 691 } 692} 693 694/*************************************************************************** 695 ** 696 ** Function btif_rc_handler 697 ** 698 ** Description RC event handler 699 ** 700 ***************************************************************************/ 701void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data) 702{ 703 BTIF_TRACE_DEBUG2 ("%s event:%s", __FUNCTION__, dump_rc_event(event)); 704 switch (event) 705 { 706 case BTA_AV_RC_OPEN_EVT: 707 { 708 BTIF_TRACE_DEBUG1("Peer_features:%x", p_data->rc_open.peer_features); 709 handle_rc_connect( &(p_data->rc_open) ); 710 }break; 711 712 case BTA_AV_RC_CLOSE_EVT: 713 { 714 handle_rc_disconnect( &(p_data->rc_close) ); 715 }break; 716 717 case BTA_AV_REMOTE_CMD_EVT: 718 { 719 BTIF_TRACE_DEBUG2("rc_id:0x%x key_state:%d", p_data->remote_cmd.rc_id, 720 p_data->remote_cmd.key_state); 721 handle_rc_passthrough_cmd( (&p_data->remote_cmd) ); 722 } 723 break; 724 case BTA_AV_RC_FEAT_EVT: 725 { 726 BTIF_TRACE_DEBUG1("Peer_features:%x", p_data->rc_feat.peer_features); 727 btif_rc_cb.rc_features = p_data->rc_feat.peer_features; 728 handle_rc_features(); 729 } 730 break; 731 case BTA_AV_META_MSG_EVT: 732 { 733 BTIF_TRACE_DEBUG2("BTA_AV_META_MSG_EVT code:%d label:%d", p_data->meta_msg.code, 734 p_data->meta_msg.label); 735 BTIF_TRACE_DEBUG3(" company_id:0x%x len:%d handle:%d", p_data->meta_msg.company_id, 736 p_data->meta_msg.len, p_data->meta_msg.rc_handle); 737 /* handle the metamsg command */ 738 handle_rc_metamsg_cmd(&(p_data->meta_msg)); 739 } 740 break; 741 default: 742 BTIF_TRACE_DEBUG1("Unhandled RC event : 0x%x", event); 743 } 744} 745 746/*************************************************************************** 747 ** 748 ** Function btif_rc_get_connected_peer 749 ** 750 ** Description Fetches the connected headset's BD_ADDR if any 751 ** 752 ***************************************************************************/ 753BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr) 754{ 755 if (btif_rc_cb.rc_connected == TRUE) { 756 bdcpy(peer_addr, btif_rc_cb.rc_addr); 757 return TRUE; 758 } 759 return FALSE; 760} 761 762/*************************************************************************** 763 ** 764 ** Function btif_rc_check_handle_pending_play 765 ** 766 ** Description Clears the queued PLAY command. if bSend is TRUE, forwards to app 767 ** 768 ***************************************************************************/ 769 770/* clear the queued PLAY command. if bSend is TRUE, forward to app */ 771void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp) 772{ 773 UNUSED(peer_addr); 774 775 BTIF_TRACE_DEBUG2("%s: bSendToApp=%d", __FUNCTION__, bSendToApp); 776 if (btif_rc_cb.rc_pending_play) 777 { 778 if (bSendToApp) 779 { 780 tBTA_AV_REMOTE_CMD remote_cmd; 781 APPL_TRACE_DEBUG1("%s: Sending queued PLAYED event to app", __FUNCTION__); 782 783 memset (&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD)); 784 remote_cmd.rc_handle = btif_rc_cb.rc_handle; 785 remote_cmd.rc_id = AVRC_ID_PLAY; 786 remote_cmd.hdr.ctype = AVRC_CMD_CTRL; 787 remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU; 788 789 /* delay sending to app, else there is a timing issue in the framework, 790 ** which causes the audio to be on th device's speaker. Delay between 791 ** OPEN & RC_PLAYs 792 */ 793 GKI_delay (200); 794 /* send to app - both PRESSED & RELEASED */ 795 remote_cmd.key_state = AVRC_STATE_PRESS; 796 handle_rc_passthrough_cmd( &remote_cmd ); 797 798 GKI_delay (100); 799 800 remote_cmd.key_state = AVRC_STATE_RELEASE; 801 handle_rc_passthrough_cmd( &remote_cmd ); 802 } 803 btif_rc_cb.rc_pending_play = FALSE; 804 } 805} 806 807/* Generic reject response */ 808static void send_reject_response (UINT8 rc_handle, UINT8 label, UINT8 pdu, UINT8 status) 809{ 810 UINT8 ctype = AVRC_RSP_REJ; 811 tAVRC_RESPONSE avrc_rsp; 812 BT_HDR *p_msg = NULL; 813 memset (&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); 814 815 avrc_rsp.rsp.opcode = opcode_from_pdu(pdu); 816 avrc_rsp.rsp.pdu = pdu; 817 avrc_rsp.rsp.status = status; 818 819 if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(rc_handle, &avrc_rsp, &p_msg)) ) 820 { 821 BTIF_TRACE_DEBUG4("%s:Sending error notification to handle:%d. pdu:%s,status:0x%02x", 822 __FUNCTION__, rc_handle, dump_rc_pdu(pdu), status); 823 BTA_AvMetaRsp(rc_handle, label, ctype, p_msg); 824 } 825} 826 827/*************************************************************************** 828 * Function send_metamsg_rsp 829 * 830 * - Argument: 831 * rc_handle RC handle corresponding to the connected RC 832 * label Label of the RC response 833 * code Response type 834 * pmetamsg_resp Vendor response 835 * 836 * - Description: Remote control metamsg response handler (AVRCP 1.3) 837 * 838 ***************************************************************************/ 839static void send_metamsg_rsp (UINT8 rc_handle, UINT8 label, tBTA_AV_CODE code, 840 tAVRC_RESPONSE *pmetamsg_resp) 841{ 842 UINT8 ctype; 843 tAVRC_STS status; 844 845 if (!pmetamsg_resp) 846 { 847 BTIF_TRACE_WARNING1("%s: Invalid response received from application", __FUNCTION__); 848 return; 849 } 850 851 BTIF_TRACE_EVENT5("+%s: rc_handle: %d, label: %d, code: 0x%02x, pdu: %s", __FUNCTION__, 852 rc_handle, label, code, dump_rc_pdu(pmetamsg_resp->rsp.pdu)); 853 854 if (pmetamsg_resp->rsp.status != AVRC_STS_NO_ERROR) 855 { 856 ctype = AVRC_RSP_REJ; 857 } 858 else 859 { 860 if ( code < AVRC_RSP_NOT_IMPL) 861 { 862 if (code == AVRC_CMD_NOTIF) 863 { 864 ctype = AVRC_RSP_INTERIM; 865 } 866 else if (code == AVRC_CMD_STATUS) 867 { 868 ctype = AVRC_RSP_IMPL_STBL; 869 } 870 else 871 { 872 ctype = AVRC_RSP_ACCEPT; 873 } 874 } 875 else 876 { 877 ctype = code; 878 } 879 } 880 /* if response is for register_notification, make sure the rc has 881 actually registered for this */ 882 if((pmetamsg_resp->rsp.pdu == AVRC_PDU_REGISTER_NOTIFICATION) && (code == AVRC_RSP_CHANGED)) 883 { 884 BOOLEAN bSent = FALSE; 885 UINT8 event_id = pmetamsg_resp->reg_notif.event_id; 886 BOOLEAN bNotify = (btif_rc_cb.rc_connected) && (btif_rc_cb.rc_notif[event_id-1].bNotify); 887 888 /* de-register this notification for a CHANGED response */ 889 btif_rc_cb.rc_notif[event_id-1].bNotify = FALSE; 890 BTIF_TRACE_DEBUG4("%s rc_handle: %d. event_id: 0x%02d bNotify:%u", __FUNCTION__, 891 btif_rc_cb.rc_handle, event_id, bNotify); 892 if (bNotify) 893 { 894 BT_HDR *p_msg = NULL; 895 tAVRC_STS status; 896 897 if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(btif_rc_cb.rc_handle, 898 pmetamsg_resp, &p_msg)) ) 899 { 900 BTIF_TRACE_DEBUG3("%s Sending notification to rc_handle: %d. event_id: 0x%02d", 901 __FUNCTION__, btif_rc_cb.rc_handle, event_id); 902 bSent = TRUE; 903 BTA_AvMetaRsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_notif[event_id-1].label, 904 ctype, p_msg); 905 } 906 else 907 { 908 BTIF_TRACE_WARNING2("%s failed to build metamsg response. status: 0x%02x", 909 __FUNCTION__, status); 910 } 911 912 } 913 914 if (!bSent) 915 { 916 BTIF_TRACE_DEBUG2("%s: Notification not sent, as there are no RC connections or the \ 917 CT has not subscribed for event_id: %s", __FUNCTION__, dump_rc_notification_event_id(event_id)); 918 } 919 } 920 else 921 { 922 /* All other commands go here */ 923 924 BT_HDR *p_msg = NULL; 925 tAVRC_STS status; 926 927 status = AVRC_BldResponse(rc_handle, pmetamsg_resp, &p_msg); 928 929 if (status == AVRC_STS_NO_ERROR) 930 { 931 BTA_AvMetaRsp(rc_handle, label, ctype, p_msg); 932 } 933 else 934 { 935 BTIF_TRACE_ERROR2("%s: failed to build metamsg response. status: 0x%02x", 936 __FUNCTION__, status); 937 } 938 } 939} 940 941static UINT8 opcode_from_pdu(UINT8 pdu) 942{ 943 UINT8 opcode = 0; 944 945 switch (pdu) 946 { 947 case AVRC_PDU_NEXT_GROUP: 948 case AVRC_PDU_PREV_GROUP: /* pass thru */ 949 opcode = AVRC_OP_PASS_THRU; 950 break; 951 952 default: /* vendor */ 953 opcode = AVRC_OP_VENDOR; 954 break; 955 } 956 957 return opcode; 958} 959 960/******************************************************************************* 961** 962** Function btif_rc_upstreams_evt 963** 964** Description Executes AVRC UPSTREAMS events in btif context. 965** 966** Returns void 967** 968*******************************************************************************/ 969static void btif_rc_upstreams_evt(UINT16 event, tAVRC_COMMAND *pavrc_cmd, UINT8 ctype, UINT8 label) 970{ 971 BTIF_TRACE_EVENT5("%s pdu: %s handle: 0x%x ctype:%x label:%x", __FUNCTION__, 972 dump_rc_pdu(pavrc_cmd->pdu), btif_rc_cb.rc_handle, ctype, label); 973 974 switch (event) 975 { 976 case AVRC_PDU_GET_PLAY_STATUS: 977 { 978 FILL_PDU_QUEUE(IDX_GET_PLAY_STATUS_RSP, ctype, label, TRUE) 979 HAL_CBACK(bt_rc_callbacks, get_play_status_cb); 980 } 981 break; 982 case AVRC_PDU_LIST_PLAYER_APP_ATTR: 983 case AVRC_PDU_LIST_PLAYER_APP_VALUES: 984 case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: 985 case AVRC_PDU_SET_PLAYER_APP_VALUE: 986 case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: 987 case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: 988 { 989 /* TODO: Add support for Application Settings */ 990 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_CMD); 991 } 992 break; 993 case AVRC_PDU_GET_ELEMENT_ATTR: 994 { 995 btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; 996 UINT8 num_attr; 997 memset(&element_attrs, 0, sizeof(element_attrs)); 998 if (pavrc_cmd->get_elem_attrs.num_attr == 0) 999 { 1000 /* CT requests for all attributes */ 1001 int attr_cnt; 1002 num_attr = BTRC_MAX_ELEM_ATTR_SIZE; 1003 for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++) 1004 { 1005 element_attrs[attr_cnt] = attr_cnt + 1; 1006 } 1007 } 1008 else if (pavrc_cmd->get_elem_attrs.num_attr == 0xFF) 1009 { 1010 /* 0xff indicates, no attributes requested - reject */ 1011 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, 1012 AVRC_STS_BAD_PARAM); 1013 return; 1014 } 1015 else 1016 { 1017 num_attr = pavrc_cmd->get_elem_attrs.num_attr; 1018 memcpy(element_attrs, pavrc_cmd->get_elem_attrs.attrs, sizeof(UINT32) 1019 *pavrc_cmd->get_elem_attrs.num_attr); 1020 } 1021 FILL_PDU_QUEUE(IDX_GET_ELEMENT_ATTR_RSP, ctype, label, TRUE); 1022 HAL_CBACK(bt_rc_callbacks, get_element_attr_cb, num_attr, element_attrs); 1023 } 1024 break; 1025 case AVRC_PDU_REGISTER_NOTIFICATION: 1026 { 1027 if(pavrc_cmd->reg_notif.event_id == BTRC_EVT_PLAY_POS_CHANGED && 1028 pavrc_cmd->reg_notif.param == 0) 1029 { 1030 BTIF_TRACE_WARNING1("%s Device registering position changed with illegal param 0.", 1031 __FUNCTION__); 1032 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_PARAM); 1033 /* de-register this notification for a rejected response */ 1034 btif_rc_cb.rc_notif[BTRC_EVT_PLAY_POS_CHANGED - 1].bNotify = FALSE; 1035 return; 1036 } 1037 HAL_CBACK(bt_rc_callbacks, register_notification_cb, pavrc_cmd->reg_notif.event_id, 1038 pavrc_cmd->reg_notif.param); 1039 } 1040 break; 1041 case AVRC_PDU_INFORM_DISPLAY_CHARSET: 1042 { 1043 tAVRC_RESPONSE avrc_rsp; 1044 BTIF_TRACE_EVENT1("%s() AVRC_PDU_INFORM_DISPLAY_CHARSET", __FUNCTION__); 1045 if(btif_rc_cb.rc_connected == TRUE) 1046 { 1047 memset(&(avrc_rsp.inform_charset), 0, sizeof(tAVRC_RSP)); 1048 avrc_rsp.inform_charset.opcode=opcode_from_pdu(AVRC_PDU_INFORM_DISPLAY_CHARSET); 1049 avrc_rsp.inform_charset.pdu=AVRC_PDU_INFORM_DISPLAY_CHARSET; 1050 avrc_rsp.inform_charset.status=AVRC_STS_NO_ERROR; 1051 send_metamsg_rsp(btif_rc_cb.rc_handle, label, ctype, &avrc_rsp); 1052 } 1053 } 1054 break; 1055 default: 1056 { 1057 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, 1058 (pavrc_cmd->pdu == AVRC_PDU_SEARCH)?AVRC_STS_SEARCH_NOT_SUP:AVRC_STS_BAD_CMD); 1059 return; 1060 } 1061 break; 1062 } 1063 1064} 1065 1066 1067/******************************************************************************* 1068** 1069** Function btif_rc_upstreams_rsp_evt 1070** 1071** Description Executes AVRC UPSTREAMS response events in btif context. 1072** 1073** Returns void 1074** 1075*******************************************************************************/ 1076static void btif_rc_upstreams_rsp_evt(UINT16 event, tAVRC_RESPONSE *pavrc_resp, UINT8 ctype, UINT8 label) 1077{ 1078 BTIF_TRACE_EVENT5("%s pdu: %s handle: 0x%x ctype:%x label:%x", __FUNCTION__, 1079 dump_rc_pdu(pavrc_resp->pdu), btif_rc_cb.rc_handle, ctype, label); 1080 1081#if (AVRC_ADV_CTRL_INCLUDED == TRUE) 1082 switch (event) 1083 { 1084 case AVRC_PDU_REGISTER_NOTIFICATION: 1085 { 1086 if(AVRC_RSP_CHANGED==ctype) 1087 btif_rc_cb.rc_volume=pavrc_resp->reg_notif.param.volume; 1088 HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->reg_notif.param.volume,ctype) 1089 } 1090 break; 1091 1092 case AVRC_PDU_SET_ABSOLUTE_VOLUME: 1093 { 1094 BTIF_TRACE_DEBUG2("Set absolute volume change event received: volume %d,ctype %d", 1095 pavrc_resp->volume.volume,ctype); 1096 if(AVRC_RSP_ACCEPT==ctype) 1097 btif_rc_cb.rc_volume=pavrc_resp->volume.volume; 1098 HAL_CBACK(bt_rc_callbacks,volume_change_cb,pavrc_resp->volume.volume,ctype) 1099 } 1100 break; 1101 1102 default: 1103 return; 1104 } 1105#endif 1106} 1107 1108/************************************************************************************ 1109** AVRCP API Functions 1110************************************************************************************/ 1111 1112/******************************************************************************* 1113** 1114** Function init 1115** 1116** Description Initializes the AVRC interface 1117** 1118** Returns bt_status_t 1119** 1120*******************************************************************************/ 1121static bt_status_t init(btrc_callbacks_t* callbacks ) 1122{ 1123 BTIF_TRACE_EVENT1("## %s ##", __FUNCTION__); 1124 bt_status_t result = BT_STATUS_SUCCESS; 1125 1126 if (bt_rc_callbacks) 1127 return BT_STATUS_DONE; 1128 1129 bt_rc_callbacks = callbacks; 1130 memset (&btif_rc_cb, 0, sizeof(btif_rc_cb)); 1131 btif_rc_cb.rc_vol_label=MAX_LABEL; 1132 btif_rc_cb.rc_volume=MAX_VOLUME; 1133 lbl_init(); 1134 1135 return result; 1136} 1137 1138/*************************************************************************** 1139** 1140** Function get_play_status_rsp 1141** 1142** Description Returns the current play status. 1143** This method is called in response to 1144** GetPlayStatus request. 1145** 1146** Returns bt_status_t 1147** 1148***************************************************************************/ 1149static bt_status_t get_play_status_rsp(btrc_play_status_t play_status, uint32_t song_len, 1150 uint32_t song_pos) 1151{ 1152 tAVRC_RESPONSE avrc_rsp; 1153 UINT32 i; 1154 CHECK_RC_CONNECTED 1155 memset(&(avrc_rsp.get_play_status), 0, sizeof(tAVRC_GET_PLAY_STATUS_RSP)); 1156 avrc_rsp.get_play_status.song_len = song_len; 1157 avrc_rsp.get_play_status.song_pos = song_pos; 1158 avrc_rsp.get_play_status.play_status = play_status; 1159 1160 avrc_rsp.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS; 1161 avrc_rsp.get_play_status.opcode = opcode_from_pdu(AVRC_PDU_GET_PLAY_STATUS); 1162 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR; 1163 /* Send the response */ 1164 SEND_METAMSG_RSP(IDX_GET_PLAY_STATUS_RSP, &avrc_rsp); 1165 return BT_STATUS_SUCCESS; 1166} 1167 1168/*************************************************************************** 1169** 1170** Function get_element_attr_rsp 1171** 1172** Description Returns the current songs' element attributes 1173** in text. 1174** 1175** Returns bt_status_t 1176** 1177***************************************************************************/ 1178static bt_status_t get_element_attr_rsp(uint8_t num_attr, btrc_element_attr_val_t *p_attrs) 1179{ 1180 tAVRC_RESPONSE avrc_rsp; 1181 UINT32 i; 1182 uint8_t j; 1183 tAVRC_ATTR_ENTRY element_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; 1184 CHECK_RC_CONNECTED 1185 memset(element_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr); 1186 1187 if (num_attr == 0) 1188 { 1189 avrc_rsp.get_play_status.status = AVRC_STS_BAD_PARAM; 1190 } 1191 else 1192 { 1193 for (i=0; i<num_attr; i++) { 1194 element_attrs[i].attr_id = p_attrs[i].attr_id; 1195 element_attrs[i].name.charset_id = AVRC_CHARSET_ID_UTF8; 1196 element_attrs[i].name.str_len = (UINT16)strlen((char *)p_attrs[i].text); 1197 element_attrs[i].name.p_str = p_attrs[i].text; 1198 BTIF_TRACE_DEBUG5("%s attr_id:0x%x, charset_id:0x%x, str_len:%d, str:%s", 1199 __FUNCTION__, (unsigned int)element_attrs[i].attr_id, 1200 element_attrs[i].name.charset_id, element_attrs[i].name.str_len, 1201 element_attrs[i].name.p_str); 1202 } 1203 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR; 1204 } 1205 avrc_rsp.get_elem_attrs.num_attr = num_attr; 1206 avrc_rsp.get_elem_attrs.p_attrs = element_attrs; 1207 avrc_rsp.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR; 1208 avrc_rsp.get_elem_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ELEMENT_ATTR); 1209 /* Send the response */ 1210 SEND_METAMSG_RSP(IDX_GET_ELEMENT_ATTR_RSP, &avrc_rsp); 1211 return BT_STATUS_SUCCESS; 1212} 1213 1214/*************************************************************************** 1215** 1216** Function register_notification_rsp 1217** 1218** Description Response to the register notification request. 1219** in text. 1220** 1221** Returns bt_status_t 1222** 1223***************************************************************************/ 1224static bt_status_t register_notification_rsp(btrc_event_id_t event_id, 1225 btrc_notification_type_t type, btrc_register_notification_t *p_param) 1226{ 1227 tAVRC_RESPONSE avrc_rsp; 1228 CHECK_RC_CONNECTED 1229 BTIF_TRACE_EVENT2("## %s ## event_id:%s", __FUNCTION__, dump_rc_notification_event_id(event_id)); 1230 if (btif_rc_cb.rc_notif[event_id-1].bNotify == FALSE) 1231 { 1232 BTIF_TRACE_ERROR1("Avrcp Event id not registered: event_id = %x", event_id); 1233 return BT_STATUS_NOT_READY; 1234 } 1235 memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); 1236 avrc_rsp.reg_notif.event_id = event_id; 1237 1238 switch(event_id) 1239 { 1240 case BTRC_EVT_PLAY_STATUS_CHANGED: 1241 avrc_rsp.reg_notif.param.play_status = p_param->play_status; 1242 break; 1243 case BTRC_EVT_TRACK_CHANGE: 1244 memcpy(&(avrc_rsp.reg_notif.param.track), &(p_param->track), sizeof(btrc_uid_t)); 1245 break; 1246 case BTRC_EVT_PLAY_POS_CHANGED: 1247 avrc_rsp.reg_notif.param.play_pos = p_param->song_pos; 1248 break; 1249 default: 1250 BTIF_TRACE_WARNING2("%s : Unhandled event ID : 0x%x", __FUNCTION__, event_id); 1251 return BT_STATUS_UNHANDLED; 1252 } 1253 1254 avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; 1255 avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION); 1256 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR; 1257 1258 /* Send the response. */ 1259 send_metamsg_rsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_notif[event_id-1].label, 1260 ((type == BTRC_NOTIFICATION_TYPE_INTERIM)?AVRC_CMD_NOTIF:AVRC_RSP_CHANGED), &avrc_rsp); 1261 return BT_STATUS_SUCCESS; 1262} 1263 1264/*************************************************************************** 1265** 1266** Function set_volume 1267** 1268** Description Send current volume setting to remote side. 1269** Support limited to SetAbsoluteVolume 1270** This can be enhanced to support Relative Volume (AVRCP 1.0). 1271** With RelateVolume, we will send VOLUME_UP/VOLUME_DOWN 1272** as opposed to absolute volume level 1273** volume: Should be in the range 0-127. bit7 is reseved and cannot be set 1274** 1275** Returns bt_status_t 1276** 1277***************************************************************************/ 1278static bt_status_t set_volume(uint8_t volume) 1279{ 1280 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 1281 CHECK_RC_CONNECTED 1282 tAVRC_STS status = BT_STATUS_UNSUPPORTED; 1283 rc_transaction_t *p_transaction=NULL; 1284 1285 if(btif_rc_cb.rc_volume==volume) 1286 { 1287 status=BT_STATUS_DONE; 1288 BTIF_TRACE_ERROR2("%s: volume value already set earlier: 0x%02x",__FUNCTION__, volume); 1289 return status; 1290 } 1291 1292 if ((btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG) && 1293 (btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL)) 1294 { 1295 tAVRC_COMMAND avrc_cmd = {0}; 1296 BT_HDR *p_msg = NULL; 1297 1298 BTIF_TRACE_DEBUG2("%s: Peer supports absolute volume. newVolume=%d", __FUNCTION__, volume); 1299 avrc_cmd.volume.opcode = AVRC_OP_VENDOR; 1300 avrc_cmd.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME; 1301 avrc_cmd.volume.status = AVRC_STS_NO_ERROR; 1302 avrc_cmd.volume.volume = volume; 1303 1304 if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) 1305 { 1306 bt_status_t tran_status=get_transaction(&p_transaction); 1307 if(BT_STATUS_SUCCESS == tran_status && NULL!=p_transaction) 1308 { 1309 BTIF_TRACE_DEBUG2("%s msgreq being sent out with label %d", 1310 __FUNCTION__,p_transaction->lbl); 1311 BTA_AvMetaCmd(btif_rc_cb.rc_handle,p_transaction->lbl, AVRC_CMD_CTRL, p_msg); 1312 status = BT_STATUS_SUCCESS; 1313 } 1314 else 1315 { 1316 if(NULL!=p_msg) 1317 GKI_freebuf(p_msg); 1318 BTIF_TRACE_ERROR2("%s: failed to obtain transaction details. status: 0x%02x", 1319 __FUNCTION__, tran_status); 1320 status = BT_STATUS_FAIL; 1321 } 1322 } 1323 else 1324 { 1325 BTIF_TRACE_ERROR2("%s: failed to build absolute volume command. status: 0x%02x", 1326 __FUNCTION__, status); 1327 status = BT_STATUS_FAIL; 1328 } 1329 } 1330 else 1331 status=BT_STATUS_NOT_READY; 1332 return status; 1333} 1334 1335 1336/*************************************************************************** 1337** 1338** Function register_volumechange 1339** 1340** Description Register for volume change notification from remote side. 1341** 1342** Returns void 1343** 1344***************************************************************************/ 1345 1346static void register_volumechange (UINT8 lbl) 1347{ 1348 tAVRC_COMMAND avrc_cmd = {0}; 1349 BT_HDR *p_msg = NULL; 1350 tAVRC_STS BldResp=AVRC_STS_BAD_CMD; 1351 UINT16 rv = 0; 1352 bt_status_t tran_status; 1353 rc_transaction_t *p_transaction=NULL; 1354 1355 BTIF_TRACE_DEBUG2("%s called with label:%d",__FUNCTION__,lbl); 1356 1357 avrc_cmd.cmd.opcode=0x00; 1358 avrc_cmd.pdu = AVRC_PDU_REGISTER_NOTIFICATION; 1359 avrc_cmd.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE; 1360 avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR; 1361 1362 BldResp=AVRC_BldCommand(&avrc_cmd, &p_msg); 1363 if(AVRC_STS_NO_ERROR==BldResp && p_msg) 1364 { 1365 p_transaction=get_transaction_by_lbl(lbl); 1366 if(NULL!=p_transaction) 1367 { 1368 BTA_AvMetaCmd(btif_rc_cb.rc_handle,p_transaction->lbl, AVRC_CMD_NOTIF, p_msg); 1369 BTIF_TRACE_DEBUG1("%s:BTA_AvMetaCmd called",__FUNCTION__); 1370 } 1371 else 1372 { 1373 if(NULL!=p_msg) 1374 GKI_freebuf(p_msg); 1375 BTIF_TRACE_ERROR2("%s transaction not obtained with label: %d",__FUNCTION__,lbl); 1376 } 1377 } 1378 else 1379 BTIF_TRACE_ERROR2("%s failed to build command:%d",__FUNCTION__,BldResp); 1380} 1381 1382 1383/*************************************************************************** 1384** 1385** Function handle_rc_metamsg_rsp 1386** 1387** Description Handle RC metamessage response 1388** 1389** Returns void 1390** 1391***************************************************************************/ 1392static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg) 1393{ 1394 tAVRC_RESPONSE avrc_response = {0}; 1395 UINT8 scratch_buf[512] = {0}; 1396 tAVRC_STS status = BT_STATUS_UNSUPPORTED; 1397 1398 if(AVRC_OP_VENDOR==pmeta_msg->p_msg->hdr.opcode &&(AVRC_RSP_CHANGED==pmeta_msg->code 1399 || AVRC_RSP_INTERIM==pmeta_msg->code || AVRC_RSP_ACCEPT==pmeta_msg->code 1400 || AVRC_RSP_REJ==pmeta_msg->code || AVRC_RSP_NOT_IMPL==pmeta_msg->code)) 1401 { 1402 status=AVRC_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, sizeof(scratch_buf)); 1403 BTIF_TRACE_DEBUG6("%s: code %d,event ID %d,PDU %x,parsing status %d, label:%d", 1404 __FUNCTION__,pmeta_msg->code,avrc_response.reg_notif.event_id,avrc_response.reg_notif.pdu, 1405 status, pmeta_msg->label); 1406 1407 if (status != AVRC_STS_NO_ERROR) 1408 { 1409 if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu 1410 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id 1411 && btif_rc_cb.rc_vol_label==pmeta_msg->label) 1412 { 1413 btif_rc_cb.rc_vol_label=MAX_LABEL; 1414 release_transaction(btif_rc_cb.rc_vol_label); 1415 } 1416 else if(AVRC_PDU_SET_ABSOLUTE_VOLUME==avrc_response.rsp.pdu) 1417 { 1418 release_transaction(pmeta_msg->label); 1419 } 1420 return; 1421 } 1422 else if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu 1423 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id 1424 && btif_rc_cb.rc_vol_label!=pmeta_msg->label) 1425 { 1426 // Just discard the message, if the device sends back with an incorrect label 1427 BTIF_TRACE_DEBUG3("%s:Discarding register notfn in rsp.code: %d and label %d", 1428 __FUNCTION__, pmeta_msg->code, pmeta_msg->label); 1429 return; 1430 } 1431 } 1432 else 1433 { 1434 BTIF_TRACE_DEBUG3("%s:Received vendor dependent in adv ctrl rsp. code: %d len: %d. Not processing it.", 1435 __FUNCTION__, pmeta_msg->code, pmeta_msg->len); 1436 return; 1437 } 1438 1439 if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu 1440 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id 1441 && AVRC_RSP_CHANGED==pmeta_msg->code) 1442 { 1443 /* re-register for volume change notification */ 1444 // Do not re-register for rejected case, as it might get into endless loop 1445 register_volumechange(btif_rc_cb.rc_vol_label); 1446 } 1447 else if(AVRC_PDU_SET_ABSOLUTE_VOLUME==avrc_response.rsp.pdu) 1448 { 1449 /* free up the label here */ 1450 release_transaction(pmeta_msg->label); 1451 } 1452 1453 BTIF_TRACE_EVENT2("%s: Passing received metamsg response to app. pdu: %s", 1454 __FUNCTION__, dump_rc_pdu(avrc_response.pdu)); 1455 btif_rc_upstreams_rsp_evt((uint16_t)avrc_response.rsp.pdu, &avrc_response, pmeta_msg->code, 1456 pmeta_msg->label); 1457} 1458 1459 1460/*************************************************************************** 1461** 1462** Function cleanup 1463** 1464** Description Closes the AVRC interface 1465** 1466** Returns void 1467** 1468***************************************************************************/ 1469static void cleanup() 1470{ 1471 BTIF_TRACE_EVENT1("## %s ##", __FUNCTION__); 1472 close_uinput(); 1473 if (bt_rc_callbacks) 1474 { 1475 bt_rc_callbacks = NULL; 1476 } 1477 memset(&btif_rc_cb, 0, sizeof(btif_rc_cb_t)); 1478 lbl_destroy(); 1479} 1480 1481 1482static const btrc_interface_t bt_rc_interface = { 1483 sizeof(bt_rc_interface), 1484 init, 1485 get_play_status_rsp, 1486 NULL, /* list_player_app_attr_rsp */ 1487 NULL, /* list_player_app_value_rsp */ 1488 NULL, /* get_player_app_value_rsp */ 1489 NULL, /* get_player_app_attr_text_rsp */ 1490 NULL, /* get_player_app_value_text_rsp */ 1491 get_element_attr_rsp, 1492 NULL, /* set_player_app_value_rsp */ 1493 register_notification_rsp, 1494 set_volume, 1495 cleanup, 1496}; 1497 1498/******************************************************************************* 1499** 1500** Function btif_rc_get_interface 1501** 1502** Description Get the AVRCP callback interface 1503** 1504** Returns btav_interface_t 1505** 1506*******************************************************************************/ 1507const btrc_interface_t *btif_rc_get_interface(void) 1508{ 1509 BTIF_TRACE_EVENT1("%s", __FUNCTION__); 1510 return &bt_rc_interface; 1511} 1512 1513/******************************************************************************* 1514** Function initialize_transaction 1515** 1516** Description Initializes fields of the transaction structure 1517** 1518** Returns void 1519*******************************************************************************/ 1520static void initialize_transaction(int lbl) 1521{ 1522 pthread_mutex_lock(&device.lbllock); 1523 if(lbl < MAX_TRANSACTIONS_PER_SESSION) 1524 { 1525 device.transaction[lbl].lbl = lbl; 1526 device.transaction[lbl].in_use=FALSE; 1527 device.transaction[lbl].handle=0; 1528 } 1529 pthread_mutex_unlock(&device.lbllock); 1530} 1531 1532/******************************************************************************* 1533** Function lbl_init 1534** 1535** Description Initializes label structures and mutexes. 1536** 1537** Returns void 1538*******************************************************************************/ 1539void lbl_init() 1540{ 1541 memset(&device,0,sizeof(rc_device_t)); 1542 pthread_mutexattr_t attr; 1543 pthread_mutexattr_init(&attr); 1544 pthread_mutex_init(&(device.lbllock), &attr); 1545 pthread_mutexattr_destroy(&attr); 1546 init_all_transactions(); 1547} 1548 1549/******************************************************************************* 1550** 1551** Function init_all_transactions 1552** 1553** Description Initializes all transactions 1554** 1555** Returns void 1556*******************************************************************************/ 1557void init_all_transactions() 1558{ 1559 UINT8 txn_indx=0; 1560 for(txn_indx=0; txn_indx < MAX_TRANSACTIONS_PER_SESSION; txn_indx++) 1561 { 1562 initialize_transaction(txn_indx); 1563 } 1564} 1565 1566/******************************************************************************* 1567** 1568** Function get_transaction_by_lbl 1569** 1570** Description Will return a transaction based on the label. If not inuse 1571** will return an error. 1572** 1573** Returns bt_status_t 1574*******************************************************************************/ 1575rc_transaction_t *get_transaction_by_lbl(UINT8 lbl) 1576{ 1577 rc_transaction_t *transaction = NULL; 1578 pthread_mutex_lock(&device.lbllock); 1579 1580 /* Determine if this is a valid label */ 1581 if (lbl < MAX_TRANSACTIONS_PER_SESSION) 1582 { 1583 if (FALSE==device.transaction[lbl].in_use) 1584 { 1585 transaction = NULL; 1586 } 1587 else 1588 { 1589 transaction = &(device.transaction[lbl]); 1590 BTIF_TRACE_DEBUG2("%s: Got transaction.label: %d",__FUNCTION__,lbl); 1591 } 1592 } 1593 1594 pthread_mutex_unlock(&device.lbllock); 1595 return transaction; 1596} 1597 1598/******************************************************************************* 1599** 1600** Function get_transaction 1601** 1602** Description Obtains the transaction details. 1603** 1604** Returns bt_status_t 1605*******************************************************************************/ 1606 1607bt_status_t get_transaction(rc_transaction_t **ptransaction) 1608{ 1609 bt_status_t result = BT_STATUS_NOMEM; 1610 UINT8 i=0; 1611 pthread_mutex_lock(&device.lbllock); 1612 1613 // Check for unused transactions 1614 for (i=0; i<MAX_TRANSACTIONS_PER_SESSION; i++) 1615 { 1616 if (FALSE==device.transaction[i].in_use) 1617 { 1618 BTIF_TRACE_DEBUG2("%s:Got transaction.label: %d",__FUNCTION__,device.transaction[i].lbl); 1619 device.transaction[i].in_use = TRUE; 1620 *ptransaction = &(device.transaction[i]); 1621 result = BT_STATUS_SUCCESS; 1622 break; 1623 } 1624 } 1625 1626 pthread_mutex_unlock(&device.lbllock); 1627 return result; 1628} 1629 1630 1631/******************************************************************************* 1632** 1633** Function release_transaction 1634** 1635** Description Will release a transaction for reuse 1636** 1637** Returns bt_status_t 1638*******************************************************************************/ 1639void release_transaction(UINT8 lbl) 1640{ 1641 rc_transaction_t *transaction = get_transaction_by_lbl(lbl); 1642 1643 /* If the transaction is in use... */ 1644 if (transaction != NULL) 1645 { 1646 BTIF_TRACE_DEBUG2("%s: lbl: %d", __FUNCTION__, lbl); 1647 initialize_transaction(lbl); 1648 } 1649} 1650 1651/******************************************************************************* 1652** 1653** Function lbl_destroy 1654** 1655** Description Cleanup of the mutex 1656** 1657** Returns void 1658*******************************************************************************/ 1659void lbl_destroy() 1660{ 1661 pthread_mutex_destroy(&(device.lbllock)); 1662} 1663 1664/******************************************************************************* 1665** Function dev_blacklisted_for_absolute_volume 1666** 1667** Description Blacklist Devices that donot handle absolute volume well 1668** We are blacklisting all the devices that are not in whitelist 1669** 1670** Returns True if the device is in the list 1671*******************************************************************************/ 1672static BOOLEAN dev_blacklisted_for_absolute_volume(BD_ADDR peer_dev) 1673{ 1674 int i; 1675 char *dev_name_str = NULL; 1676 int whitelist_size = sizeof(rc_white_addr_prefix)/sizeof(rc_white_addr_prefix[0]); 1677 1678 for (i = 0; i < whitelist_size; i++) { 1679 if (rc_white_addr_prefix[i][0] == peer_dev[0] && 1680 rc_white_addr_prefix[i][1] == peer_dev[1] && 1681 rc_white_addr_prefix[i][2] == peer_dev[2]) { 1682 BTIF_TRACE_DEBUG3("whitelist absolute volume for %02x:%02x:%02x", 1683 peer_dev[0], peer_dev[1], peer_dev[2]); 1684 return FALSE; 1685 } 1686 } 1687 1688 dev_name_str = BTM_SecReadDevName(peer_dev); 1689 whitelist_size = sizeof(rc_white_name)/sizeof(char*); 1690 if (dev_name_str != NULL) { 1691 for (i = 0; i < whitelist_size; i++) { 1692 if (strcmp(dev_name_str, rc_white_name[i]) == 0) { 1693 BTIF_TRACE_DEBUG1("whitelist absolute volume for %s", dev_name_str); 1694 return FALSE; 1695 } 1696 } 1697 } 1698 1699 BTIF_TRACE_WARNING4("blacklist absolute volume for %02x:%02x:%02x, name = %s", 1700 peer_dev[0], peer_dev[1], peer_dev[2], dev_name_str); 1701 return TRUE; 1702} 1703