1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
20 /*****************************************************************************
24 * Description: Bluetooth AVRC implementation
26 *****************************************************************************/
27 #include <hardware/bluetooth.h>
31 #include "bta_av_api.h"
32 #include "avrc_defs.h"
35 #define LOG_TAG "bt_btif_avrc"
36 #include "btif_common.h"
37 #include "btif_util.h"
39 #include "device/include/interop.h"
40 #include "hardware/bt_rc.h"
43 /*****************************************************************************
45 ******************************************************************************/
47 /* cod value for Headsets */
48 #define COD_AV_HEADSETS 0x0404
49 /* for AVRC 1.4 need to change this */
50 #define MAX_RC_NOTIFICATIONS AVRC_EVT_APP_SETTING_CHANGE
52 #define IDX_GET_PLAY_STATUS_RSP 0
53 #define IDX_LIST_APP_ATTR_RSP 1
54 #define IDX_LIST_APP_VALUE_RSP 2
55 #define IDX_GET_CURR_APP_VAL_RSP 3
56 #define IDX_SET_APP_VAL_RSP 4
57 #define IDX_GET_APP_ATTR_TXT_RSP 5
58 #define IDX_GET_APP_VAL_TXT_RSP 6
59 #define IDX_GET_ELEMENT_ATTR_RSP 7
60 #define MAX_VOLUME 128
62 #define MAX_TRANSACTIONS_PER_SESSION 16
63 #define MAX_CMD_QUEUE_LEN 8
64 #define PLAY_STATUS_PLAYING 1
66 #define CHECK_RC_CONNECTED \
67 BTIF_TRACE_DEBUG("## %s ##", __FUNCTION__); \
68 if(btif_rc_cb.rc_connected == FALSE) \
70 BTIF_TRACE_WARNING("Function %s() called when RC is not connected", __FUNCTION__); \
71 return BT_STATUS_NOT_READY; \
74 #define FILL_PDU_QUEUE(index, ctype, label, pending) \
76 btif_rc_cb.rc_pdu_info[index].ctype = ctype; \
77 btif_rc_cb.rc_pdu_info[index].label = label; \
78 btif_rc_cb.rc_pdu_info[index].is_rsp_pending = pending; \
81 #define SEND_METAMSG_RSP(index, avrc_rsp) \
83 if(btif_rc_cb.rc_pdu_info[index].is_rsp_pending == FALSE) \
85 BTIF_TRACE_WARNING("%s Not sending response as no PDU was registered", __FUNCTION__); \
86 return BT_STATUS_UNHANDLED; \
88 send_metamsg_rsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_pdu_info[index].label, \
89 btif_rc_cb.rc_pdu_info[index].ctype, avrc_rsp); \
90 btif_rc_cb.rc_pdu_info[index].ctype = 0; \
91 btif_rc_cb.rc_pdu_info[index].label = 0; \
92 btif_rc_cb.rc_pdu_info[index].is_rsp_pending = FALSE; \
95 /*****************************************************************************
96 ** Local type definitions
97 ******************************************************************************/
101 } btif_rc_reg_notifications_t;
107 BOOLEAN is_rsp_pending;
108 } btif_rc_cmd_ctxt_t;
110 /* TODO : Merge btif_rc_reg_notifications_t and btif_rc_cmd_ctxt_t to a single struct */
112 BOOLEAN rc_connected;
114 tBTA_AV_FEAT rc_features;
116 UINT16 rc_pending_play;
117 btif_rc_cmd_ctxt_t rc_pdu_info[MAX_CMD_QUEUE_LEN];
118 btif_rc_reg_notifications_t rc_notif[MAX_RC_NOTIFICATIONS];
119 unsigned int rc_volume;
120 uint8_t rc_vol_label;
131 pthread_mutex_t lbllock;
132 rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION];
138 #define MAX_UINPUT_PATHS 3
139 static const char* uinput_dev_path[] =
140 {"/dev/uinput", "/dev/input/uinput", "/dev/misc/uinput" };
141 static int uinput_fd = -1;
143 static int send_event (int fd, uint16_t type, uint16_t code, int32_t value);
144 static void send_key (int fd, uint16_t key, int pressed);
145 static int uinput_driver_check();
146 static int uinput_create(char *name);
147 static int init_uinput (void);
148 static void close_uinput (void);
150 static const struct {
154 uint8_t release_quirk;
156 { "PLAY", AVRC_ID_PLAY, KEY_PLAYCD, 1 },
157 { "STOP", AVRC_ID_STOP, KEY_STOPCD, 0 },
158 { "PAUSE", AVRC_ID_PAUSE, KEY_PAUSECD, 1 },
159 { "FORWARD", AVRC_ID_FORWARD, KEY_NEXTSONG, 0 },
160 { "BACKWARD", AVRC_ID_BACKWARD, KEY_PREVIOUSSONG, 0 },
161 { "REWIND", AVRC_ID_REWIND, KEY_REWIND, 0 },
162 { "FAST FORWARD", AVRC_ID_FAST_FOR, KEY_FAST_FORWARD, 0 },
166 static void send_reject_response (UINT8 rc_handle, UINT8 label,
167 UINT8 pdu, UINT8 status);
168 static UINT8 opcode_from_pdu(UINT8 pdu);
169 static void send_metamsg_rsp (UINT8 rc_handle, UINT8 label,
170 tBTA_AV_CODE code, tAVRC_RESPONSE *pmetamsg_resp);
171 static void register_volumechange(UINT8 label);
172 static void lbl_init();
173 static void lbl_destroy();
174 static void init_all_transactions();
175 static bt_status_t get_transaction(rc_transaction_t **ptransaction);
176 static void release_transaction(UINT8 label);
177 static rc_transaction_t* get_transaction_by_lbl(UINT8 label);
178 static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg);
179 static void btif_rc_upstreams_evt(UINT16 event, tAVRC_COMMAND* p_param, UINT8 ctype, UINT8 label);
180 static void btif_rc_upstreams_rsp_evt(UINT16 event, tAVRC_RESPONSE *pavrc_resp, UINT8 ctype, UINT8 label);
182 /*****************************************************************************
184 ******************************************************************************/
185 static btif_rc_cb_t btif_rc_cb;
186 static btrc_callbacks_t *bt_rc_callbacks = NULL;
187 static btrc_ctrl_callbacks_t *bt_rc_ctrl_callbacks = NULL;
189 /*****************************************************************************
191 ******************************************************************************/
193 /*****************************************************************************
195 ******************************************************************************/
196 extern BOOLEAN btif_hf_call_terminated_recently();
197 extern BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod);
200 /*****************************************************************************
202 ******************************************************************************/
204 /*****************************************************************************
205 ** Local uinput helper functions
206 ******************************************************************************/
207 int send_event (int fd, uint16_t type, uint16_t code, int32_t value)
209 struct uinput_event event;
210 BTIF_TRACE_DEBUG("%s type:%u code:%u value:%d", __FUNCTION__,
212 memset(&event, 0, sizeof(event));
217 return write(fd, &event, sizeof(event));
220 void send_key (int fd, uint16_t key, int pressed)
222 BTIF_TRACE_DEBUG("%s fd:%d key:%u pressed:%d", __FUNCTION__,
230 BTIF_TRACE_DEBUG("AVRCP: Send key %d (%d) fd=%d", key, pressed, fd);
231 send_event(fd, EV_KEY, key, pressed);
232 send_event(fd, EV_SYN, SYN_REPORT, 0);
235 /************** uinput related functions **************/
236 int uinput_driver_check()
239 for (i=0; i < MAX_UINPUT_PATHS; i++)
241 if (access(uinput_dev_path[i], O_RDWR) == 0) {
245 BTIF_TRACE_ERROR("%s ERROR: uinput device is not in the system", __FUNCTION__);
249 int uinput_create(char *name)
251 struct uinput_dev dev;
254 for(x=0; x < MAX_UINPUT_PATHS; x++)
256 fd = open(uinput_dev_path[x], O_RDWR);
261 if (x == MAX_UINPUT_PATHS) {
262 BTIF_TRACE_ERROR("%s ERROR: uinput device open failed", __FUNCTION__);
265 memset(&dev, 0, sizeof(dev));
267 strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE-1);
269 dev.id.bustype = BUS_BLUETOOTH;
270 dev.id.vendor = 0x0000;
271 dev.id.product = 0x0000;
272 dev.id.version = 0x0000;
274 if (write(fd, &dev, sizeof(dev)) < 0) {
275 BTIF_TRACE_ERROR("%s Unable to write device information", __FUNCTION__);
280 ioctl(fd, UI_SET_EVBIT, EV_KEY);
281 ioctl(fd, UI_SET_EVBIT, EV_REL);
282 ioctl(fd, UI_SET_EVBIT, EV_SYN);
284 for (x = 0; key_map[x].name != NULL; x++)
285 ioctl(fd, UI_SET_KEYBIT, key_map[x].mapped_id);
287 if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
288 BTIF_TRACE_ERROR("%s Unable to create uinput device", __FUNCTION__);
295 int init_uinput (void)
297 char *name = "AVRCP";
299 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
300 uinput_fd = uinput_create(name);
302 BTIF_TRACE_ERROR("%s AVRCP: Failed to initialize uinput for %s (%d)",
303 __FUNCTION__, name, uinput_fd);
305 BTIF_TRACE_DEBUG("%s AVRCP: Initialized uinput for %s (fd=%d)",
306 __FUNCTION__, name, uinput_fd);
311 void close_uinput (void)
313 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
315 ioctl(uinput_fd, UI_DEV_DESTROY);
322 void handle_rc_features()
324 btrc_remote_features_t rc_features = BTRC_FEAT_NONE;
326 bdcpy(rc_addr.address, btif_rc_cb.rc_addr);
328 if (interop_match(INTEROP_DISABLE_ABSOLUTE_VOLUME, &rc_addr))
329 btif_rc_cb.rc_features &= ~BTA_AV_FEAT_ADV_CTRL;
331 if (btif_rc_cb.rc_features & BTA_AV_FEAT_BROWSE)
333 rc_features |= BTRC_FEAT_BROWSE;
336 if ( (btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL) &&
337 (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG))
339 rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME;
342 if (btif_rc_cb.rc_features & BTA_AV_FEAT_METADATA)
344 rc_features |= BTRC_FEAT_METADATA;
347 BTIF_TRACE_DEBUG("%s: rc_features=0x%x", __FUNCTION__, rc_features);
348 HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features)
350 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
351 BTIF_TRACE_DEBUG("Checking for feature flags in btif_rc_handler with label %d",
352 btif_rc_cb.rc_vol_label);
353 // Register for volume change on connect
354 if(btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL &&
355 btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)
357 rc_transaction_t *p_transaction=NULL;
358 bt_status_t status = BT_STATUS_NOT_READY;
359 if(MAX_LABEL==btif_rc_cb.rc_vol_label)
361 status=get_transaction(&p_transaction);
365 p_transaction=get_transaction_by_lbl(btif_rc_cb.rc_vol_label);
366 if(NULL!=p_transaction)
368 BTIF_TRACE_DEBUG("register_volumechange already in progress for label %d",
369 btif_rc_cb.rc_vol_label);
373 status=get_transaction(&p_transaction);
376 if(BT_STATUS_SUCCESS == status && NULL!=p_transaction)
378 btif_rc_cb.rc_vol_label=p_transaction->lbl;
379 register_volumechange(btif_rc_cb.rc_vol_label);
386 /***************************************************************************
387 * Function handle_rc_connect
389 * - Argument: tBTA_AV_RC_OPEN RC open data structure
391 * - Description: RC connection event handler
393 ***************************************************************************/
394 void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open)
396 BTIF_TRACE_DEBUG("%s: rc_handle: %d", __FUNCTION__, p_rc_open->rc_handle);
397 bt_status_t result = BT_STATUS_SUCCESS;
398 #if (AVRC_CTLR_INCLUDED == TRUE)
402 if(p_rc_open->status == BTA_AV_SUCCESS)
404 //check if already some RC is connected
405 if (btif_rc_cb.rc_connected)
407 BTIF_TRACE_ERROR("Got RC OPEN in connected state, Connected RC: %d \
408 and Current RC: %d", btif_rc_cb.rc_handle,p_rc_open->rc_handle );
409 if ((btif_rc_cb.rc_handle != p_rc_open->rc_handle)
410 && (bdcmp(btif_rc_cb.rc_addr, p_rc_open->peer_addr)))
412 BTIF_TRACE_DEBUG("Got RC connected for some other handle");
413 BTA_AvCloseRc(p_rc_open->rc_handle);
417 memcpy(btif_rc_cb.rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR));
418 btif_rc_cb.rc_features = p_rc_open->peer_features;
419 btif_rc_cb.rc_vol_label=MAX_LABEL;
420 btif_rc_cb.rc_volume=MAX_VOLUME;
422 btif_rc_cb.rc_connected = TRUE;
423 btif_rc_cb.rc_handle = p_rc_open->rc_handle;
425 /* on locally initiated connection we will get remote features as part of connect */
426 if (btif_rc_cb.rc_features != 0)
427 handle_rc_features();
429 result = uinput_driver_check();
430 if(result == BT_STATUS_SUCCESS)
434 #if (AVRC_CTLR_INCLUDED == TRUE)
435 bdcpy(rc_addr.address, btif_rc_cb.rc_addr);
436 /* report connection state if device is AVRCP target */
437 if (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG) {
438 if (bt_rc_ctrl_callbacks != NULL) {
439 HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, TRUE, &rc_addr);
446 BTIF_TRACE_ERROR("%s Connect failed with error code: %d",
447 __FUNCTION__, p_rc_open->status);
448 btif_rc_cb.rc_connected = FALSE;
452 /***************************************************************************
453 * Function handle_rc_disconnect
455 * - Argument: tBTA_AV_RC_CLOSE RC close data structure
457 * - Description: RC disconnection event handler
459 ***************************************************************************/
460 void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close)
462 #if (AVRC_CTLR_INCLUDED == TRUE)
464 tBTA_AV_FEAT features;
466 BTIF_TRACE_DEBUG("%s: rc_handle: %d", __FUNCTION__, p_rc_close->rc_handle);
467 if ((p_rc_close->rc_handle != btif_rc_cb.rc_handle)
468 && (bdcmp(btif_rc_cb.rc_addr, p_rc_close->peer_addr)))
470 BTIF_TRACE_ERROR("Got disconnect of unknown device");
474 btif_rc_cb.rc_handle = 0;
475 btif_rc_cb.rc_connected = FALSE;
476 memset(btif_rc_cb.rc_addr, 0, sizeof(BD_ADDR));
477 memset(btif_rc_cb.rc_notif, 0, sizeof(btif_rc_cb.rc_notif));
478 #if (AVRC_CTLR_INCLUDED == TRUE)
479 features = btif_rc_cb.rc_features;
481 btif_rc_cb.rc_features = 0;
482 btif_rc_cb.rc_vol_label=MAX_LABEL;
483 btif_rc_cb.rc_volume=MAX_VOLUME;
484 init_all_transactions();
486 #if (AVRC_CTLR_INCLUDED == TRUE)
487 bdcpy(rc_addr.address, btif_rc_cb.rc_addr);
489 memset(btif_rc_cb.rc_addr, 0, sizeof(BD_ADDR));
490 #if (AVRC_CTLR_INCLUDED == TRUE)
491 /* report connection state if device is AVRCP target */
492 if (features & BTA_AV_FEAT_RCTG) {
493 if (bt_rc_ctrl_callbacks != NULL) {
494 HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, FALSE, &rc_addr);
500 /***************************************************************************
501 * Function handle_rc_passthrough_cmd
503 * - Argument: tBTA_AV_RC rc_id remote control command ID
504 * tBTA_AV_STATE key_state status of key press
506 * - Description: Remote control command handler
508 ***************************************************************************/
509 void handle_rc_passthrough_cmd ( tBTA_AV_REMOTE_CMD *p_remote_cmd)
514 BTIF_TRACE_DEBUG("%s: p_remote_cmd->rc_id=%d", __FUNCTION__, p_remote_cmd->rc_id);
516 /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up this PLAY */
519 /* queue AVRC PLAY if GAVDTP Open notification to app is pending (2 second timer) */
520 if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected()))
522 if (p_remote_cmd->key_state == AVRC_STATE_PRESS)
524 APPL_TRACE_WARNING("%s: AVDT not open, queuing the PLAY command", __FUNCTION__);
525 btif_rc_cb.rc_pending_play = TRUE;
530 if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (btif_rc_cb.rc_pending_play))
532 APPL_TRACE_WARNING("%s: Clear the pending PLAY on PAUSE received", __FUNCTION__);
533 btif_rc_cb.rc_pending_play = FALSE;
538 if ((p_remote_cmd->rc_id == BTA_AV_RC_STOP) && (!btif_av_stream_started_ready()))
540 APPL_TRACE_WARNING("%s: Stream suspended, ignore STOP cmd",__FUNCTION__);
544 if (p_remote_cmd->key_state == AVRC_STATE_RELEASE) {
552 /* If this is Play/Pause command (press or release) before processing, check the following
553 * a voice call has ended recently
554 * the remote device is not of type headset
555 * If the above conditions meet, drop the Play/Pause command
556 * This fix is to interop with certain carkits which sends an automatic PLAY or PAUSE
557 * commands right after call ends
559 if((p_remote_cmd->rc_id == BTA_AV_RC_PLAY || p_remote_cmd->rc_id == BTA_AV_RC_PAUSE)&&
560 (btif_hf_call_terminated_recently() == TRUE) &&
561 (check_cod( (const bt_bdaddr_t*)&(btif_rc_cb.rc_addr), COD_AV_HEADSETS) != TRUE))
563 BTIF_TRACE_DEBUG("%s:Dropping the play/Pause command received right after call end cmd:%d",
564 __FUNCTION__,p_remote_cmd->rc_id);
568 if (p_remote_cmd->rc_id == BTA_AV_RC_FAST_FOR || p_remote_cmd->rc_id == BTA_AV_RC_REWIND) {
569 HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed);
573 for (i = 0; key_map[i].name != NULL; i++) {
574 if (p_remote_cmd->rc_id == key_map[i].avrcp) {
575 BTIF_TRACE_DEBUG("%s: %s %s", __FUNCTION__, key_map[i].name, status);
577 /* MusicPlayer uses a long_press_timeout of 1 second for PLAYPAUSE button
578 * and maps that to autoshuffle. So if for some reason release for PLAY/PAUSE
579 * comes 1 second after the press, the MediaPlayer UI goes into a bad state.
580 * The reason for the delay could be sniff mode exit or some AVDTP procedure etc.
581 * The fix is to generate a release right after the press and drown the 'actual'
584 if ((key_map[i].release_quirk == 1) && (pressed == 0))
586 BTIF_TRACE_DEBUG("%s: AVRC %s Release Faked earlier, drowned now",
587 __FUNCTION__, key_map[i].name);
590 send_key(uinput_fd, key_map[i].mapped_id, pressed);
591 if ((key_map[i].release_quirk == 1) && (pressed == 1))
593 GKI_delay(30); // 30ms
594 BTIF_TRACE_DEBUG("%s: AVRC %s Release quirk enabled, send release now",
595 __FUNCTION__, key_map[i].name);
596 send_key(uinput_fd, key_map[i].mapped_id, 0);
602 if (key_map[i].name == NULL)
603 BTIF_TRACE_ERROR("%s AVRCP: unknown button 0x%02X %s", __FUNCTION__,
604 p_remote_cmd->rc_id, status);
607 /***************************************************************************
608 * Function handle_rc_passthrough_rsp
610 * - Argument: tBTA_AV_REMOTE_RSP passthrough command response
612 * - Description: Remote control passthrough response handler
614 ***************************************************************************/
615 void handle_rc_passthrough_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp)
617 #if (AVRC_CTLR_INCLUDED == TRUE)
619 if (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)
622 if (p_remote_rsp->key_state == AVRC_STATE_RELEASE)
633 BTIF_TRACE_DEBUG("%s: rc_id=%d status=%s", __FUNCTION__, p_remote_rsp->rc_id, status);
635 release_transaction(p_remote_rsp->label);
636 if (bt_rc_ctrl_callbacks != NULL) {
637 HAL_CBACK(bt_rc_ctrl_callbacks, passthrough_rsp_cb, p_remote_rsp->rc_id, key_state);
642 BTIF_TRACE_ERROR("%s DUT does not support AVRCP controller role", __FUNCTION__);
645 BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __FUNCTION__);
649 void handle_uid_changed_notification(tBTA_AV_META_MSG *pmeta_msg, tAVRC_COMMAND *pavrc_command)
651 tAVRC_RESPONSE avrc_rsp = {0};
652 avrc_rsp.rsp.pdu = pavrc_command->pdu;
653 avrc_rsp.rsp.status = AVRC_STS_NO_ERROR;
654 avrc_rsp.rsp.opcode = pavrc_command->cmd.opcode;
656 avrc_rsp.reg_notif.event_id = pavrc_command->reg_notif.event_id;
657 avrc_rsp.reg_notif.param.uid_counter = 0;
659 send_metamsg_rsp(pmeta_msg->rc_handle, pmeta_msg->label, AVRC_RSP_INTERIM, &avrc_rsp);
660 send_metamsg_rsp(pmeta_msg->rc_handle, pmeta_msg->label, AVRC_RSP_CHANGED, &avrc_rsp);
665 /***************************************************************************
666 * Function handle_rc_metamsg_cmd
668 * - Argument: tBTA_AV_VENDOR Structure containing the received
671 * - Description: Remote control metamsg command handler (AVRCP 1.3)
673 ***************************************************************************/
674 void handle_rc_metamsg_cmd (tBTA_AV_META_MSG *pmeta_msg)
676 /* Parse the metamsg command and pass it on to BTL-IFS */
677 UINT8 scratch_buf[512] = {0};
678 tAVRC_COMMAND avrc_command = {0};
681 BTIF_TRACE_EVENT("+ %s", __FUNCTION__);
683 if (pmeta_msg->p_msg->hdr.opcode != AVRC_OP_VENDOR)
685 BTIF_TRACE_WARNING("Invalid opcode: %x", pmeta_msg->p_msg->hdr.opcode);
688 if (pmeta_msg->len < 3)
690 BTIF_TRACE_WARNING("Invalid length.Opcode: 0x%x, len: 0x%x", pmeta_msg->p_msg->hdr.opcode,
695 if (pmeta_msg->code >= AVRC_RSP_NOT_IMPL)
697 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
699 rc_transaction_t *transaction=NULL;
700 transaction=get_transaction_by_lbl(pmeta_msg->label);
701 if(NULL!=transaction)
703 handle_rc_metamsg_rsp(pmeta_msg);
707 BTIF_TRACE_DEBUG("%s:Discard vendor dependent rsp. code: %d label:%d.",
708 __FUNCTION__, pmeta_msg->code, pmeta_msg->label);
714 BTIF_TRACE_DEBUG("%s:Received vendor dependent rsp. code: %d len: %d. Not processing it.",
715 __FUNCTION__, pmeta_msg->code, pmeta_msg->len);
721 status=AVRC_ParsCommand(pmeta_msg->p_msg, &avrc_command, scratch_buf, sizeof(scratch_buf));
722 BTIF_TRACE_DEBUG("Received vendor command.code,PDU and label: %d, %d,%d",pmeta_msg->code,
723 avrc_command.cmd.pdu, pmeta_msg->label);
725 if (status != AVRC_STS_NO_ERROR)
728 BTIF_TRACE_WARNING("%s: Error in parsing received metamsg command. status: 0x%02x",
729 __FUNCTION__, status);
730 send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_command.pdu, status);
734 /* if RegisterNotification, add it to our registered queue */
736 if (avrc_command.cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION)
738 UINT8 event_id = avrc_command.reg_notif.event_id;
739 BTIF_TRACE_EVENT("%s:New register notification received.event_id:%s,label:0x%x,code:%x",
740 __FUNCTION__,dump_rc_notification_event_id(event_id), pmeta_msg->label,pmeta_msg->code);
741 btif_rc_cb.rc_notif[event_id-1].bNotify = TRUE;
742 btif_rc_cb.rc_notif[event_id-1].label = pmeta_msg->label;
744 if(event_id == AVRC_EVT_UIDS_CHANGE)
746 handle_uid_changed_notification(pmeta_msg, &avrc_command);
752 BTIF_TRACE_EVENT("%s: Passing received metamsg command to app. pdu: %s",
753 __FUNCTION__, dump_rc_pdu(avrc_command.cmd.pdu));
755 /* Since handle_rc_metamsg_cmd() itself is called from
756 *btif context, no context switching is required. Invoke
757 * btif_rc_upstreams_evt directly from here. */
758 btif_rc_upstreams_evt((uint16_t)avrc_command.cmd.pdu, &avrc_command, pmeta_msg->code,
763 /***************************************************************************
765 ** Function btif_rc_handler
767 ** Description RC event handler
769 ***************************************************************************/
770 void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data)
772 BTIF_TRACE_DEBUG ("%s event:%s", __FUNCTION__, dump_rc_event(event));
775 case BTA_AV_RC_OPEN_EVT:
777 BTIF_TRACE_DEBUG("Peer_features:%x", p_data->rc_open.peer_features);
778 handle_rc_connect( &(p_data->rc_open) );
781 case BTA_AV_RC_CLOSE_EVT:
783 handle_rc_disconnect( &(p_data->rc_close) );
786 case BTA_AV_REMOTE_CMD_EVT:
788 BTIF_TRACE_DEBUG("rc_id:0x%x key_state:%d", p_data->remote_cmd.rc_id,
789 p_data->remote_cmd.key_state);
790 handle_rc_passthrough_cmd( (&p_data->remote_cmd) );
793 #if (AVRC_CTLR_INCLUDED == TRUE)
794 case BTA_AV_REMOTE_RSP_EVT:
796 BTIF_TRACE_DEBUG("RSP: rc_id:0x%x key_state:%d", p_data->remote_rsp.rc_id,
797 p_data->remote_rsp.key_state);
798 handle_rc_passthrough_rsp( (&p_data->remote_rsp) );
802 case BTA_AV_RC_FEAT_EVT:
804 BTIF_TRACE_DEBUG("Peer_features:%x", p_data->rc_feat.peer_features);
805 btif_rc_cb.rc_features = p_data->rc_feat.peer_features;
806 handle_rc_features();
809 case BTA_AV_META_MSG_EVT:
811 BTIF_TRACE_DEBUG("BTA_AV_META_MSG_EVT code:%d label:%d", p_data->meta_msg.code,
812 p_data->meta_msg.label);
813 BTIF_TRACE_DEBUG(" company_id:0x%x len:%d handle:%d", p_data->meta_msg.company_id,
814 p_data->meta_msg.len, p_data->meta_msg.rc_handle);
815 /* handle the metamsg command */
816 handle_rc_metamsg_cmd(&(p_data->meta_msg));
820 BTIF_TRACE_DEBUG("Unhandled RC event : 0x%x", event);
824 /***************************************************************************
826 ** Function btif_rc_get_connected_peer
828 ** Description Fetches the connected headset's BD_ADDR if any
830 ***************************************************************************/
831 BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr)
833 if (btif_rc_cb.rc_connected == TRUE) {
834 bdcpy(peer_addr, btif_rc_cb.rc_addr);
840 /***************************************************************************
842 ** Function btif_rc_check_handle_pending_play
844 ** Description Clears the queued PLAY command. if bSend is TRUE, forwards to app
846 ***************************************************************************/
848 /* clear the queued PLAY command. if bSend is TRUE, forward to app */
849 void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp)
853 BTIF_TRACE_DEBUG("%s: bSendToApp=%d", __FUNCTION__, bSendToApp);
854 if (btif_rc_cb.rc_pending_play)
858 tBTA_AV_REMOTE_CMD remote_cmd;
859 APPL_TRACE_DEBUG("%s: Sending queued PLAYED event to app", __FUNCTION__);
861 memset (&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD));
862 remote_cmd.rc_handle = btif_rc_cb.rc_handle;
863 remote_cmd.rc_id = AVRC_ID_PLAY;
864 remote_cmd.hdr.ctype = AVRC_CMD_CTRL;
865 remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU;
867 /* delay sending to app, else there is a timing issue in the framework,
868 ** which causes the audio to be on th device's speaker. Delay between
872 /* send to app - both PRESSED & RELEASED */
873 remote_cmd.key_state = AVRC_STATE_PRESS;
874 handle_rc_passthrough_cmd( &remote_cmd );
878 remote_cmd.key_state = AVRC_STATE_RELEASE;
879 handle_rc_passthrough_cmd( &remote_cmd );
881 btif_rc_cb.rc_pending_play = FALSE;
885 /* Generic reject response */
886 static void send_reject_response (UINT8 rc_handle, UINT8 label, UINT8 pdu, UINT8 status)
888 UINT8 ctype = AVRC_RSP_REJ;
889 tAVRC_RESPONSE avrc_rsp;
890 BT_HDR *p_msg = NULL;
891 memset (&avrc_rsp, 0, sizeof(tAVRC_RESPONSE));
893 avrc_rsp.rsp.opcode = opcode_from_pdu(pdu);
894 avrc_rsp.rsp.pdu = pdu;
895 avrc_rsp.rsp.status = status;
897 if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(rc_handle, &avrc_rsp, &p_msg)) )
899 BTIF_TRACE_DEBUG("%s:Sending error notification to handle:%d. pdu:%s,status:0x%02x",
900 __FUNCTION__, rc_handle, dump_rc_pdu(pdu), status);
901 BTA_AvMetaRsp(rc_handle, label, ctype, p_msg);
905 /***************************************************************************
906 * Function send_metamsg_rsp
909 * rc_handle RC handle corresponding to the connected RC
910 * label Label of the RC response
912 * pmetamsg_resp Vendor response
914 * - Description: Remote control metamsg response handler (AVRCP 1.3)
916 ***************************************************************************/
917 static void send_metamsg_rsp (UINT8 rc_handle, UINT8 label, tBTA_AV_CODE code,
918 tAVRC_RESPONSE *pmetamsg_resp)
924 BTIF_TRACE_WARNING("%s: Invalid response received from application", __FUNCTION__);
928 BTIF_TRACE_EVENT("+%s: rc_handle: %d, label: %d, code: 0x%02x, pdu: %s", __FUNCTION__,
929 rc_handle, label, code, dump_rc_pdu(pmetamsg_resp->rsp.pdu));
931 if (pmetamsg_resp->rsp.status != AVRC_STS_NO_ERROR)
933 ctype = AVRC_RSP_REJ;
937 if ( code < AVRC_RSP_NOT_IMPL)
939 if (code == AVRC_CMD_NOTIF)
941 ctype = AVRC_RSP_INTERIM;
943 else if (code == AVRC_CMD_STATUS)
945 ctype = AVRC_RSP_IMPL_STBL;
949 ctype = AVRC_RSP_ACCEPT;
957 /* if response is for register_notification, make sure the rc has
958 actually registered for this */
959 if((pmetamsg_resp->rsp.pdu == AVRC_PDU_REGISTER_NOTIFICATION) && (code == AVRC_RSP_CHANGED))
961 BOOLEAN bSent = FALSE;
962 UINT8 event_id = pmetamsg_resp->reg_notif.event_id;
963 BOOLEAN bNotify = (btif_rc_cb.rc_connected) && (btif_rc_cb.rc_notif[event_id-1].bNotify);
965 /* de-register this notification for a CHANGED response */
966 btif_rc_cb.rc_notif[event_id-1].bNotify = FALSE;
967 BTIF_TRACE_DEBUG("%s rc_handle: %d. event_id: 0x%02d bNotify:%u", __FUNCTION__,
968 btif_rc_cb.rc_handle, event_id, bNotify);
971 BT_HDR *p_msg = NULL;
974 if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(btif_rc_cb.rc_handle,
975 pmetamsg_resp, &p_msg)) )
977 BTIF_TRACE_DEBUG("%s Sending notification to rc_handle: %d. event_id: 0x%02d",
978 __FUNCTION__, btif_rc_cb.rc_handle, event_id);
980 BTA_AvMetaRsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_notif[event_id-1].label,
985 BTIF_TRACE_WARNING("%s failed to build metamsg response. status: 0x%02x",
986 __FUNCTION__, status);
993 BTIF_TRACE_DEBUG("%s: Notification not sent, as there are no RC connections or the \
994 CT has not subscribed for event_id: %s", __FUNCTION__, dump_rc_notification_event_id(event_id));
999 /* All other commands go here */
1001 BT_HDR *p_msg = NULL;
1004 status = AVRC_BldResponse(rc_handle, pmetamsg_resp, &p_msg);
1006 if (status == AVRC_STS_NO_ERROR)
1008 BTA_AvMetaRsp(rc_handle, label, ctype, p_msg);
1012 BTIF_TRACE_ERROR("%s: failed to build metamsg response. status: 0x%02x",
1013 __FUNCTION__, status);
1018 static UINT8 opcode_from_pdu(UINT8 pdu)
1024 case AVRC_PDU_NEXT_GROUP:
1025 case AVRC_PDU_PREV_GROUP: /* pass thru */
1026 opcode = AVRC_OP_PASS_THRU;
1029 default: /* vendor */
1030 opcode = AVRC_OP_VENDOR;
1037 /*******************************************************************************
1039 ** Function btif_rc_upstreams_evt
1041 ** Description Executes AVRC UPSTREAMS events in btif context.
1045 *******************************************************************************/
1046 static void btif_rc_upstreams_evt(UINT16 event, tAVRC_COMMAND *pavrc_cmd, UINT8 ctype, UINT8 label)
1048 BTIF_TRACE_EVENT("%s pdu: %s handle: 0x%x ctype:%x label:%x", __FUNCTION__,
1049 dump_rc_pdu(pavrc_cmd->pdu), btif_rc_cb.rc_handle, ctype, label);
1053 case AVRC_PDU_GET_PLAY_STATUS:
1055 FILL_PDU_QUEUE(IDX_GET_PLAY_STATUS_RSP, ctype, label, TRUE)
1056 HAL_CBACK(bt_rc_callbacks, get_play_status_cb);
1059 case AVRC_PDU_LIST_PLAYER_APP_ATTR:
1060 case AVRC_PDU_LIST_PLAYER_APP_VALUES:
1061 case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:
1062 case AVRC_PDU_SET_PLAYER_APP_VALUE:
1063 case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:
1064 case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:
1066 /* TODO: Add support for Application Settings */
1067 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_CMD);
1070 case AVRC_PDU_GET_ELEMENT_ATTR:
1072 btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
1074 memset(&element_attrs, 0, sizeof(element_attrs));
1075 if (pavrc_cmd->get_elem_attrs.num_attr == 0)
1077 /* CT requests for all attributes */
1079 num_attr = BTRC_MAX_ELEM_ATTR_SIZE;
1080 for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++)
1082 element_attrs[attr_cnt] = attr_cnt + 1;
1085 else if (pavrc_cmd->get_elem_attrs.num_attr == 0xFF)
1087 /* 0xff indicates, no attributes requested - reject */
1088 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu,
1089 AVRC_STS_BAD_PARAM);
1094 int attr_cnt, filled_attr_count;
1097 /* Attribute IDs from 1 to AVRC_MAX_NUM_MEDIA_ATTR_ID are only valid,
1098 * hence HAL definition limits the attributes to AVRC_MAX_NUM_MEDIA_ATTR_ID.
1099 * Fill only valid entries.
1101 for (attr_cnt = 0; (attr_cnt < pavrc_cmd->get_elem_attrs.num_attr) &&
1102 (num_attr < AVRC_MAX_NUM_MEDIA_ATTR_ID); attr_cnt++)
1104 if ((pavrc_cmd->get_elem_attrs.attrs[attr_cnt] > 0) &&
1105 (pavrc_cmd->get_elem_attrs.attrs[attr_cnt] <= AVRC_MAX_NUM_MEDIA_ATTR_ID))
1107 /* Skip the duplicate entries : PTS sends duplicate entries for Fragment cases
1109 for (filled_attr_count = 0; filled_attr_count < num_attr; filled_attr_count++)
1111 if (element_attrs[filled_attr_count] == pavrc_cmd->get_elem_attrs.attrs[attr_cnt])
1114 if (filled_attr_count == num_attr)
1116 element_attrs[num_attr] = pavrc_cmd->get_elem_attrs.attrs[attr_cnt];
1122 FILL_PDU_QUEUE(IDX_GET_ELEMENT_ATTR_RSP, ctype, label, TRUE);
1123 HAL_CBACK(bt_rc_callbacks, get_element_attr_cb, num_attr, element_attrs);
1126 case AVRC_PDU_REGISTER_NOTIFICATION:
1128 if(pavrc_cmd->reg_notif.event_id == BTRC_EVT_PLAY_POS_CHANGED &&
1129 pavrc_cmd->reg_notif.param == 0)
1131 BTIF_TRACE_WARNING("%s Device registering position changed with illegal param 0.",
1133 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_PARAM);
1134 /* de-register this notification for a rejected response */
1135 btif_rc_cb.rc_notif[BTRC_EVT_PLAY_POS_CHANGED - 1].bNotify = FALSE;
1138 HAL_CBACK(bt_rc_callbacks, register_notification_cb, pavrc_cmd->reg_notif.event_id,
1139 pavrc_cmd->reg_notif.param);
1142 case AVRC_PDU_INFORM_DISPLAY_CHARSET:
1144 tAVRC_RESPONSE avrc_rsp;
1145 BTIF_TRACE_EVENT("%s() AVRC_PDU_INFORM_DISPLAY_CHARSET", __FUNCTION__);
1146 if(btif_rc_cb.rc_connected == TRUE)
1148 memset(&(avrc_rsp.inform_charset), 0, sizeof(tAVRC_RSP));
1149 avrc_rsp.inform_charset.opcode=opcode_from_pdu(AVRC_PDU_INFORM_DISPLAY_CHARSET);
1150 avrc_rsp.inform_charset.pdu=AVRC_PDU_INFORM_DISPLAY_CHARSET;
1151 avrc_rsp.inform_charset.status=AVRC_STS_NO_ERROR;
1152 send_metamsg_rsp(btif_rc_cb.rc_handle, label, ctype, &avrc_rsp);
1158 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu,
1159 (pavrc_cmd->pdu == AVRC_PDU_SEARCH)?AVRC_STS_SEARCH_NOT_SUP:AVRC_STS_BAD_CMD);
1168 /*******************************************************************************
1170 ** Function btif_rc_upstreams_rsp_evt
1172 ** Description Executes AVRC UPSTREAMS response events in btif context.
1176 *******************************************************************************/
1177 static void btif_rc_upstreams_rsp_evt(UINT16 event, tAVRC_RESPONSE *pavrc_resp, UINT8 ctype, UINT8 label)
1179 BTIF_TRACE_EVENT("%s pdu: %s handle: 0x%x ctype:%x label:%x", __FUNCTION__,
1180 dump_rc_pdu(pavrc_resp->pdu), btif_rc_cb.rc_handle, ctype, label);
1182 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
1185 case AVRC_PDU_REGISTER_NOTIFICATION:
1187 if(AVRC_RSP_CHANGED==ctype)
1188 btif_rc_cb.rc_volume=pavrc_resp->reg_notif.param.volume;
1189 HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->reg_notif.param.volume,ctype)
1193 case AVRC_PDU_SET_ABSOLUTE_VOLUME:
1195 BTIF_TRACE_DEBUG("Set absolute volume change event received: volume %d,ctype %d",
1196 pavrc_resp->volume.volume,ctype);
1197 if(AVRC_RSP_ACCEPT==ctype)
1198 btif_rc_cb.rc_volume=pavrc_resp->volume.volume;
1199 HAL_CBACK(bt_rc_callbacks,volume_change_cb,pavrc_resp->volume.volume,ctype)
1209 /************************************************************************************
1210 ** AVRCP API Functions
1211 ************************************************************************************/
1213 /*******************************************************************************
1217 ** Description Initializes the AVRC interface
1219 ** Returns bt_status_t
1221 *******************************************************************************/
1222 static bt_status_t init(btrc_callbacks_t* callbacks )
1224 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1225 bt_status_t result = BT_STATUS_SUCCESS;
1227 if (bt_rc_callbacks)
1228 return BT_STATUS_DONE;
1230 bt_rc_callbacks = callbacks;
1231 memset (&btif_rc_cb, 0, sizeof(btif_rc_cb));
1232 btif_rc_cb.rc_vol_label=MAX_LABEL;
1233 btif_rc_cb.rc_volume=MAX_VOLUME;
1239 /*******************************************************************************
1241 ** Function init_ctrl
1243 ** Description Initializes the AVRC interface
1245 ** Returns bt_status_t
1247 *******************************************************************************/
1248 static bt_status_t init_ctrl(btrc_ctrl_callbacks_t* callbacks )
1250 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1251 bt_status_t result = BT_STATUS_SUCCESS;
1253 if (bt_rc_ctrl_callbacks)
1254 return BT_STATUS_DONE;
1256 bt_rc_ctrl_callbacks = callbacks;
1257 memset (&btif_rc_cb, 0, sizeof(btif_rc_cb));
1258 btif_rc_cb.rc_vol_label=MAX_LABEL;
1259 btif_rc_cb.rc_volume=MAX_VOLUME;
1265 /***************************************************************************
1267 ** Function get_play_status_rsp
1269 ** Description Returns the current play status.
1270 ** This method is called in response to
1271 ** GetPlayStatus request.
1273 ** Returns bt_status_t
1275 ***************************************************************************/
1276 static bt_status_t get_play_status_rsp(btrc_play_status_t play_status, uint32_t song_len,
1279 tAVRC_RESPONSE avrc_rsp;
1281 memset(&(avrc_rsp.get_play_status), 0, sizeof(tAVRC_GET_PLAY_STATUS_RSP));
1282 avrc_rsp.get_play_status.song_len = song_len;
1283 avrc_rsp.get_play_status.song_pos = song_pos;
1284 avrc_rsp.get_play_status.play_status = play_status;
1286 avrc_rsp.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS;
1287 avrc_rsp.get_play_status.opcode = opcode_from_pdu(AVRC_PDU_GET_PLAY_STATUS);
1288 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
1289 /* Send the response */
1290 SEND_METAMSG_RSP(IDX_GET_PLAY_STATUS_RSP, &avrc_rsp);
1291 return BT_STATUS_SUCCESS;
1294 /***************************************************************************
1296 ** Function get_element_attr_rsp
1298 ** Description Returns the current songs' element attributes
1301 ** Returns bt_status_t
1303 ***************************************************************************/
1304 static bt_status_t get_element_attr_rsp(uint8_t num_attr, btrc_element_attr_val_t *p_attrs)
1306 tAVRC_RESPONSE avrc_rsp;
1308 tAVRC_ATTR_ENTRY element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
1310 memset(element_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr);
1314 avrc_rsp.get_play_status.status = AVRC_STS_BAD_PARAM;
1318 for (i=0; i<num_attr; i++) {
1319 element_attrs[i].attr_id = p_attrs[i].attr_id;
1320 element_attrs[i].name.charset_id = AVRC_CHARSET_ID_UTF8;
1321 element_attrs[i].name.str_len = (UINT16)strlen((char *)p_attrs[i].text);
1322 element_attrs[i].name.p_str = p_attrs[i].text;
1323 BTIF_TRACE_DEBUG("%s attr_id:0x%x, charset_id:0x%x, str_len:%d, str:%s",
1324 __FUNCTION__, (unsigned int)element_attrs[i].attr_id,
1325 element_attrs[i].name.charset_id, element_attrs[i].name.str_len,
1326 element_attrs[i].name.p_str);
1328 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
1330 avrc_rsp.get_elem_attrs.num_attr = num_attr;
1331 avrc_rsp.get_elem_attrs.p_attrs = element_attrs;
1332 avrc_rsp.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR;
1333 avrc_rsp.get_elem_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ELEMENT_ATTR);
1334 /* Send the response */
1335 SEND_METAMSG_RSP(IDX_GET_ELEMENT_ATTR_RSP, &avrc_rsp);
1336 return BT_STATUS_SUCCESS;
1339 /***************************************************************************
1341 ** Function register_notification_rsp
1343 ** Description Response to the register notification request.
1346 ** Returns bt_status_t
1348 ***************************************************************************/
1349 static bt_status_t register_notification_rsp(btrc_event_id_t event_id,
1350 btrc_notification_type_t type, btrc_register_notification_t *p_param)
1352 tAVRC_RESPONSE avrc_rsp;
1354 BTIF_TRACE_EVENT("## %s ## event_id:%s", __FUNCTION__, dump_rc_notification_event_id(event_id));
1355 if (btif_rc_cb.rc_notif[event_id-1].bNotify == FALSE)
1357 BTIF_TRACE_ERROR("Avrcp Event id not registered: event_id = %x", event_id);
1358 return BT_STATUS_NOT_READY;
1360 memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP));
1361 avrc_rsp.reg_notif.event_id = event_id;
1365 case BTRC_EVT_PLAY_STATUS_CHANGED:
1366 avrc_rsp.reg_notif.param.play_status = p_param->play_status;
1367 if (avrc_rsp.reg_notif.param.play_status == PLAY_STATUS_PLAYING)
1368 btif_av_clear_remote_suspend_flag();
1370 case BTRC_EVT_TRACK_CHANGE:
1371 memcpy(&(avrc_rsp.reg_notif.param.track), &(p_param->track), sizeof(btrc_uid_t));
1373 case BTRC_EVT_PLAY_POS_CHANGED:
1374 avrc_rsp.reg_notif.param.play_pos = p_param->song_pos;
1377 BTIF_TRACE_WARNING("%s : Unhandled event ID : 0x%x", __FUNCTION__, event_id);
1378 return BT_STATUS_UNHANDLED;
1381 avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
1382 avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION);
1383 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
1385 /* Send the response. */
1386 send_metamsg_rsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_notif[event_id-1].label,
1387 ((type == BTRC_NOTIFICATION_TYPE_INTERIM)?AVRC_CMD_NOTIF:AVRC_RSP_CHANGED), &avrc_rsp);
1388 return BT_STATUS_SUCCESS;
1391 /***************************************************************************
1393 ** Function set_volume
1395 ** Description Send current volume setting to remote side.
1396 ** Support limited to SetAbsoluteVolume
1397 ** This can be enhanced to support Relative Volume (AVRCP 1.0).
1398 ** With RelateVolume, we will send VOLUME_UP/VOLUME_DOWN
1399 ** as opposed to absolute volume level
1400 ** volume: Should be in the range 0-127. bit7 is reseved and cannot be set
1402 ** Returns bt_status_t
1404 ***************************************************************************/
1405 static bt_status_t set_volume(uint8_t volume)
1407 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1409 tAVRC_STS status = BT_STATUS_UNSUPPORTED;
1410 rc_transaction_t *p_transaction=NULL;
1412 if(btif_rc_cb.rc_volume==volume)
1414 status=BT_STATUS_DONE;
1415 BTIF_TRACE_ERROR("%s: volume value already set earlier: 0x%02x",__FUNCTION__, volume);
1419 if ((btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG) &&
1420 (btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL))
1422 tAVRC_COMMAND avrc_cmd = {0};
1423 BT_HDR *p_msg = NULL;
1425 BTIF_TRACE_DEBUG("%s: Peer supports absolute volume. newVolume=%d", __FUNCTION__, volume);
1426 avrc_cmd.volume.opcode = AVRC_OP_VENDOR;
1427 avrc_cmd.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME;
1428 avrc_cmd.volume.status = AVRC_STS_NO_ERROR;
1429 avrc_cmd.volume.volume = volume;
1431 if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR)
1433 bt_status_t tran_status=get_transaction(&p_transaction);
1434 if(BT_STATUS_SUCCESS == tran_status && NULL!=p_transaction)
1436 BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d",
1437 __FUNCTION__,p_transaction->lbl);
1438 BTA_AvMetaCmd(btif_rc_cb.rc_handle,p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
1439 status = BT_STATUS_SUCCESS;
1445 BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x",
1446 __FUNCTION__, tran_status);
1447 status = BT_STATUS_FAIL;
1452 BTIF_TRACE_ERROR("%s: failed to build absolute volume command. status: 0x%02x",
1453 __FUNCTION__, status);
1454 status = BT_STATUS_FAIL;
1458 status=BT_STATUS_NOT_READY;
1463 /***************************************************************************
1465 ** Function register_volumechange
1467 ** Description Register for volume change notification from remote side.
1471 ***************************************************************************/
1473 static void register_volumechange (UINT8 lbl)
1475 tAVRC_COMMAND avrc_cmd = {0};
1476 BT_HDR *p_msg = NULL;
1477 tAVRC_STS BldResp=AVRC_STS_BAD_CMD;
1478 rc_transaction_t *p_transaction=NULL;
1480 BTIF_TRACE_DEBUG("%s called with label:%d",__FUNCTION__,lbl);
1482 avrc_cmd.cmd.opcode=0x00;
1483 avrc_cmd.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
1484 avrc_cmd.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE;
1485 avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR;
1487 BldResp=AVRC_BldCommand(&avrc_cmd, &p_msg);
1488 if(AVRC_STS_NO_ERROR==BldResp && p_msg)
1490 p_transaction=get_transaction_by_lbl(lbl);
1491 if(NULL!=p_transaction)
1493 BTA_AvMetaCmd(btif_rc_cb.rc_handle,p_transaction->lbl, AVRC_CMD_NOTIF, p_msg);
1494 BTIF_TRACE_DEBUG("%s:BTA_AvMetaCmd called",__FUNCTION__);
1500 BTIF_TRACE_ERROR("%s transaction not obtained with label: %d",__FUNCTION__,lbl);
1504 BTIF_TRACE_ERROR("%s failed to build command:%d",__FUNCTION__,BldResp);
1508 /***************************************************************************
1510 ** Function handle_rc_metamsg_rsp
1512 ** Description Handle RC metamessage response
1516 ***************************************************************************/
1517 static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg)
1519 tAVRC_RESPONSE avrc_response = {0};
1520 UINT8 scratch_buf[512] = {0};
1521 tAVRC_STS status = BT_STATUS_UNSUPPORTED;
1523 if(AVRC_OP_VENDOR==pmeta_msg->p_msg->hdr.opcode &&(AVRC_RSP_CHANGED==pmeta_msg->code
1524 || AVRC_RSP_INTERIM==pmeta_msg->code || AVRC_RSP_ACCEPT==pmeta_msg->code
1525 || AVRC_RSP_REJ==pmeta_msg->code || AVRC_RSP_NOT_IMPL==pmeta_msg->code))
1527 status=AVRC_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, sizeof(scratch_buf));
1528 BTIF_TRACE_DEBUG("%s: code %d,event ID %d,PDU %x,parsing status %d, label:%d",
1529 __FUNCTION__,pmeta_msg->code,avrc_response.reg_notif.event_id,avrc_response.reg_notif.pdu,
1530 status, pmeta_msg->label);
1532 if (status != AVRC_STS_NO_ERROR)
1534 if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu
1535 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id
1536 && btif_rc_cb.rc_vol_label==pmeta_msg->label)
1538 btif_rc_cb.rc_vol_label=MAX_LABEL;
1539 release_transaction(btif_rc_cb.rc_vol_label);
1541 else if(AVRC_PDU_SET_ABSOLUTE_VOLUME==avrc_response.rsp.pdu)
1543 release_transaction(pmeta_msg->label);
1547 else if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu
1548 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id
1549 && btif_rc_cb.rc_vol_label!=pmeta_msg->label)
1551 // Just discard the message, if the device sends back with an incorrect label
1552 BTIF_TRACE_DEBUG("%s:Discarding register notfn in rsp.code: %d and label %d",
1553 __FUNCTION__, pmeta_msg->code, pmeta_msg->label);
1559 BTIF_TRACE_DEBUG("%s:Received vendor dependent in adv ctrl rsp. code: %d len: %d. Not processing it.",
1560 __FUNCTION__, pmeta_msg->code, pmeta_msg->len);
1564 if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu
1565 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id
1566 && AVRC_RSP_CHANGED==pmeta_msg->code)
1568 /* re-register for volume change notification */
1569 // Do not re-register for rejected case, as it might get into endless loop
1570 register_volumechange(btif_rc_cb.rc_vol_label);
1572 else if(AVRC_PDU_SET_ABSOLUTE_VOLUME==avrc_response.rsp.pdu)
1574 /* free up the label here */
1575 release_transaction(pmeta_msg->label);
1578 BTIF_TRACE_EVENT("%s: Passing received metamsg response to app. pdu: %s",
1579 __FUNCTION__, dump_rc_pdu(avrc_response.pdu));
1580 btif_rc_upstreams_rsp_evt((uint16_t)avrc_response.rsp.pdu, &avrc_response, pmeta_msg->code,
1585 /***************************************************************************
1589 ** Description Closes the AVRC interface
1593 ***************************************************************************/
1594 static void cleanup()
1596 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1598 if (bt_rc_callbacks)
1600 bt_rc_callbacks = NULL;
1602 memset(&btif_rc_cb, 0, sizeof(btif_rc_cb_t));
1604 BTIF_TRACE_EVENT("## %s ## completed", __FUNCTION__);
1607 /***************************************************************************
1609 ** Function cleanup_ctrl
1611 ** Description Closes the AVRC Controller interface
1615 ***************************************************************************/
1616 static void cleanup_ctrl()
1618 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1620 if (bt_rc_ctrl_callbacks)
1622 bt_rc_ctrl_callbacks = NULL;
1624 memset(&btif_rc_cb, 0, sizeof(btif_rc_cb_t));
1626 BTIF_TRACE_EVENT("## %s ## completed", __FUNCTION__);
1629 static bt_status_t send_passthrough_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code, uint8_t key_state)
1631 tAVRC_STS status = BT_STATUS_UNSUPPORTED;
1632 #if (AVRC_CTLR_INCLUDED == TRUE)
1634 rc_transaction_t *p_transaction=NULL;
1635 BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __FUNCTION__,
1636 key_code, key_state);
1637 if (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)
1639 bt_status_t tran_status = get_transaction(&p_transaction);
1640 if(BT_STATUS_SUCCESS == tran_status && NULL != p_transaction)
1642 BTA_AvRemoteCmd(btif_rc_cb.rc_handle, p_transaction->lbl,
1643 (tBTA_AV_RC)key_code, (tBTA_AV_STATE)key_state);
1644 status = BT_STATUS_SUCCESS;
1645 BTIF_TRACE_DEBUG("%s: succesfully sent passthrough command to BTA", __FUNCTION__);
1649 status = BT_STATUS_FAIL;
1650 BTIF_TRACE_DEBUG("%s: error in fetching transaction", __FUNCTION__);
1655 status = BT_STATUS_FAIL;
1656 BTIF_TRACE_DEBUG("%s: feature not supported", __FUNCTION__);
1659 BTIF_TRACE_DEBUG("%s: feature not enabled", __FUNCTION__);
1664 static const btrc_interface_t bt_rc_interface = {
1665 sizeof(bt_rc_interface),
1667 get_play_status_rsp,
1668 NULL, /* list_player_app_attr_rsp */
1669 NULL, /* list_player_app_value_rsp */
1670 NULL, /* get_player_app_value_rsp */
1671 NULL, /* get_player_app_attr_text_rsp */
1672 NULL, /* get_player_app_value_text_rsp */
1673 get_element_attr_rsp,
1674 NULL, /* set_player_app_value_rsp */
1675 register_notification_rsp,
1680 static const btrc_ctrl_interface_t bt_rc_ctrl_interface = {
1681 sizeof(bt_rc_ctrl_interface),
1683 send_passthrough_cmd,
1687 /*******************************************************************************
1689 ** Function btif_rc_get_interface
1691 ** Description Get the AVRCP Target callback interface
1693 ** Returns btav_interface_t
1695 *******************************************************************************/
1696 const btrc_interface_t *btif_rc_get_interface(void)
1698 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1699 return &bt_rc_interface;
1702 /*******************************************************************************
1704 ** Function btif_rc_ctrl_get_interface
1706 ** Description Get the AVRCP Controller callback interface
1708 ** Returns btav_interface_t
1710 *******************************************************************************/
1711 const btrc_ctrl_interface_t *btif_rc_ctrl_get_interface(void)
1713 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1714 return &bt_rc_ctrl_interface;
1717 /*******************************************************************************
1718 ** Function initialize_transaction
1720 ** Description Initializes fields of the transaction structure
1723 *******************************************************************************/
1724 static void initialize_transaction(int lbl)
1726 pthread_mutex_lock(&device.lbllock);
1727 if(lbl < MAX_TRANSACTIONS_PER_SESSION)
1729 device.transaction[lbl].lbl = lbl;
1730 device.transaction[lbl].in_use=FALSE;
1731 device.transaction[lbl].handle=0;
1733 pthread_mutex_unlock(&device.lbllock);
1736 /*******************************************************************************
1737 ** Function lbl_init
1739 ** Description Initializes label structures and mutexes.
1742 *******************************************************************************/
1745 memset(&device,0,sizeof(rc_device_t));
1746 pthread_mutexattr_t attr;
1747 pthread_mutexattr_init(&attr);
1748 pthread_mutex_init(&(device.lbllock), &attr);
1749 pthread_mutexattr_destroy(&attr);
1750 init_all_transactions();
1753 /*******************************************************************************
1755 ** Function init_all_transactions
1757 ** Description Initializes all transactions
1760 *******************************************************************************/
1761 void init_all_transactions()
1764 for(txn_indx=0; txn_indx < MAX_TRANSACTIONS_PER_SESSION; txn_indx++)
1766 initialize_transaction(txn_indx);
1770 /*******************************************************************************
1772 ** Function get_transaction_by_lbl
1774 ** Description Will return a transaction based on the label. If not inuse
1775 ** will return an error.
1777 ** Returns bt_status_t
1778 *******************************************************************************/
1779 rc_transaction_t *get_transaction_by_lbl(UINT8 lbl)
1781 rc_transaction_t *transaction = NULL;
1782 pthread_mutex_lock(&device.lbllock);
1784 /* Determine if this is a valid label */
1785 if (lbl < MAX_TRANSACTIONS_PER_SESSION)
1787 if (FALSE==device.transaction[lbl].in_use)
1793 transaction = &(device.transaction[lbl]);
1794 BTIF_TRACE_DEBUG("%s: Got transaction.label: %d",__FUNCTION__,lbl);
1798 pthread_mutex_unlock(&device.lbllock);
1802 /*******************************************************************************
1804 ** Function get_transaction
1806 ** Description Obtains the transaction details.
1808 ** Returns bt_status_t
1809 *******************************************************************************/
1811 bt_status_t get_transaction(rc_transaction_t **ptransaction)
1813 bt_status_t result = BT_STATUS_NOMEM;
1815 pthread_mutex_lock(&device.lbllock);
1817 // Check for unused transactions
1818 for (i=0; i<MAX_TRANSACTIONS_PER_SESSION; i++)
1820 if (FALSE==device.transaction[i].in_use)
1822 BTIF_TRACE_DEBUG("%s:Got transaction.label: %d",__FUNCTION__,device.transaction[i].lbl);
1823 device.transaction[i].in_use = TRUE;
1824 *ptransaction = &(device.transaction[i]);
1825 result = BT_STATUS_SUCCESS;
1830 pthread_mutex_unlock(&device.lbllock);
1835 /*******************************************************************************
1837 ** Function release_transaction
1839 ** Description Will release a transaction for reuse
1841 ** Returns bt_status_t
1842 *******************************************************************************/
1843 void release_transaction(UINT8 lbl)
1845 rc_transaction_t *transaction = get_transaction_by_lbl(lbl);
1847 /* If the transaction is in use... */
1848 if (transaction != NULL)
1850 BTIF_TRACE_DEBUG("%s: lbl: %d", __FUNCTION__, lbl);
1851 initialize_transaction(lbl);
1855 /*******************************************************************************
1857 ** Function lbl_destroy
1859 ** Description Cleanup of the mutex
1862 *******************************************************************************/
1865 pthread_mutex_destroy(&(device.lbllock));