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 "hardware/bt_rc.h"
40 #include "device/include/interop.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 (AVRC_ADV_CTRL_INCLUDED == TRUE)
337 if ( (btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL) &&
338 (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG))
340 rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME;
344 if (btif_rc_cb.rc_features & BTA_AV_FEAT_METADATA)
346 rc_features |= BTRC_FEAT_METADATA;
349 BTIF_TRACE_DEBUG("%s: rc_features=0x%x", __FUNCTION__, rc_features);
350 HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features)
352 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
353 BTIF_TRACE_DEBUG("Checking for feature flags in btif_rc_handler with label %d",
354 btif_rc_cb.rc_vol_label);
355 // Register for volume change on connect
356 if(btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL &&
357 btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)
359 rc_transaction_t *p_transaction=NULL;
360 bt_status_t status = BT_STATUS_NOT_READY;
361 if(MAX_LABEL==btif_rc_cb.rc_vol_label)
363 status=get_transaction(&p_transaction);
367 p_transaction=get_transaction_by_lbl(btif_rc_cb.rc_vol_label);
368 if(NULL!=p_transaction)
370 BTIF_TRACE_DEBUG("register_volumechange already in progress for label %d",
371 btif_rc_cb.rc_vol_label);
375 status=get_transaction(&p_transaction);
378 if(BT_STATUS_SUCCESS == status && NULL!=p_transaction)
380 btif_rc_cb.rc_vol_label=p_transaction->lbl;
381 register_volumechange(btif_rc_cb.rc_vol_label);
388 /***************************************************************************
389 * Function handle_rc_connect
391 * - Argument: tBTA_AV_RC_OPEN RC open data structure
393 * - Description: RC connection event handler
395 ***************************************************************************/
396 void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open)
398 BTIF_TRACE_DEBUG("%s: rc_handle: %d", __FUNCTION__, p_rc_open->rc_handle);
399 bt_status_t result = BT_STATUS_SUCCESS;
400 #if (AVRC_CTLR_INCLUDED == TRUE)
404 if(p_rc_open->status == BTA_AV_SUCCESS)
406 //check if already some RC is connected
407 if (btif_rc_cb.rc_connected)
409 BTIF_TRACE_ERROR("Got RC OPEN in connected state, Connected RC: %d \
410 and Current RC: %d", btif_rc_cb.rc_handle,p_rc_open->rc_handle );
411 if ((btif_rc_cb.rc_handle != p_rc_open->rc_handle)
412 && (bdcmp(btif_rc_cb.rc_addr, p_rc_open->peer_addr)))
414 BTIF_TRACE_DEBUG("Got RC connected for some other handle");
415 BTA_AvCloseRc(p_rc_open->rc_handle);
419 memcpy(btif_rc_cb.rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR));
420 btif_rc_cb.rc_features = p_rc_open->peer_features;
421 btif_rc_cb.rc_vol_label=MAX_LABEL;
422 btif_rc_cb.rc_volume=MAX_VOLUME;
424 btif_rc_cb.rc_connected = TRUE;
425 btif_rc_cb.rc_handle = p_rc_open->rc_handle;
427 /* on locally initiated connection we will get remote features as part of connect */
428 if (btif_rc_cb.rc_features != 0)
429 handle_rc_features();
431 result = uinput_driver_check();
432 if(result == BT_STATUS_SUCCESS)
436 #if (AVRC_CTLR_INCLUDED == TRUE)
437 bdcpy(rc_addr.address, btif_rc_cb.rc_addr);
438 /* report connection state if device is AVRCP target */
439 if (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG) {
440 if (bt_rc_ctrl_callbacks != NULL) {
441 HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, TRUE, &rc_addr);
448 BTIF_TRACE_ERROR("%s Connect failed with error code: %d",
449 __FUNCTION__, p_rc_open->status);
450 btif_rc_cb.rc_connected = FALSE;
454 /***************************************************************************
455 * Function handle_rc_disconnect
457 * - Argument: tBTA_AV_RC_CLOSE RC close data structure
459 * - Description: RC disconnection event handler
461 ***************************************************************************/
462 void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close)
464 #if (AVRC_CTLR_INCLUDED == TRUE)
466 tBTA_AV_FEAT features;
468 BTIF_TRACE_DEBUG("%s: rc_handle: %d", __FUNCTION__, p_rc_close->rc_handle);
469 if ((p_rc_close->rc_handle != btif_rc_cb.rc_handle)
470 && (bdcmp(btif_rc_cb.rc_addr, p_rc_close->peer_addr)))
472 BTIF_TRACE_ERROR("Got disconnect of unknown device");
476 btif_rc_cb.rc_handle = 0;
477 btif_rc_cb.rc_connected = FALSE;
478 memset(btif_rc_cb.rc_addr, 0, sizeof(BD_ADDR));
479 memset(btif_rc_cb.rc_notif, 0, sizeof(btif_rc_cb.rc_notif));
480 #if (AVRC_CTLR_INCLUDED == TRUE)
481 features = btif_rc_cb.rc_features;
483 btif_rc_cb.rc_features = 0;
484 btif_rc_cb.rc_vol_label=MAX_LABEL;
485 btif_rc_cb.rc_volume=MAX_VOLUME;
486 init_all_transactions();
488 #if (AVRC_CTLR_INCLUDED == TRUE)
489 bdcpy(rc_addr.address, btif_rc_cb.rc_addr);
491 memset(btif_rc_cb.rc_addr, 0, sizeof(BD_ADDR));
492 #if (AVRC_CTLR_INCLUDED == TRUE)
493 /* report connection state if device is AVRCP target */
494 if (features & BTA_AV_FEAT_RCTG) {
495 if (bt_rc_ctrl_callbacks != NULL) {
496 HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, FALSE, &rc_addr);
502 /***************************************************************************
503 * Function handle_rc_passthrough_cmd
505 * - Argument: tBTA_AV_RC rc_id remote control command ID
506 * tBTA_AV_STATE key_state status of key press
508 * - Description: Remote control command handler
510 ***************************************************************************/
511 void handle_rc_passthrough_cmd ( tBTA_AV_REMOTE_CMD *p_remote_cmd)
516 BTIF_TRACE_DEBUG("%s: p_remote_cmd->rc_id=%d", __FUNCTION__, p_remote_cmd->rc_id);
518 /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up this PLAY */
521 /* queue AVRC PLAY if GAVDTP Open notification to app is pending (2 second timer) */
522 if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected()))
524 if (p_remote_cmd->key_state == AVRC_STATE_PRESS)
526 APPL_TRACE_WARNING("%s: AVDT not open, queuing the PLAY command", __FUNCTION__);
527 btif_rc_cb.rc_pending_play = TRUE;
532 if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (btif_rc_cb.rc_pending_play))
534 APPL_TRACE_WARNING("%s: Clear the pending PLAY on PAUSE received", __FUNCTION__);
535 btif_rc_cb.rc_pending_play = FALSE;
540 if ((p_remote_cmd->rc_id == BTA_AV_RC_STOP) && (!btif_av_stream_started_ready()))
542 APPL_TRACE_WARNING("%s: Stream suspended, ignore STOP cmd",__FUNCTION__);
546 if (p_remote_cmd->key_state == AVRC_STATE_RELEASE) {
554 /* If this is Play/Pause command (press or release) before processing, check the following
555 * a voice call has ended recently
556 * the remote device is not of type headset
557 * If the above conditions meet, drop the Play/Pause command
558 * This fix is to interop with certain carkits which sends an automatic PLAY or PAUSE
559 * commands right after call ends
561 if((p_remote_cmd->rc_id == BTA_AV_RC_PLAY || p_remote_cmd->rc_id == BTA_AV_RC_PAUSE)&&
562 (btif_hf_call_terminated_recently() == TRUE) &&
563 (check_cod( (const bt_bdaddr_t*)&(btif_rc_cb.rc_addr), COD_AV_HEADSETS) != TRUE))
565 BTIF_TRACE_DEBUG("%s:Dropping the play/Pause command received right after call end cmd:%d",
566 __FUNCTION__,p_remote_cmd->rc_id);
570 if (p_remote_cmd->rc_id == BTA_AV_RC_FAST_FOR || p_remote_cmd->rc_id == BTA_AV_RC_REWIND) {
571 HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed);
575 for (i = 0; key_map[i].name != NULL; i++) {
576 if (p_remote_cmd->rc_id == key_map[i].avrcp) {
577 BTIF_TRACE_DEBUG("%s: %s %s", __FUNCTION__, key_map[i].name, status);
579 /* MusicPlayer uses a long_press_timeout of 1 second for PLAYPAUSE button
580 * and maps that to autoshuffle. So if for some reason release for PLAY/PAUSE
581 * comes 1 second after the press, the MediaPlayer UI goes into a bad state.
582 * The reason for the delay could be sniff mode exit or some AVDTP procedure etc.
583 * The fix is to generate a release right after the press and drown the 'actual'
586 if ((key_map[i].release_quirk == 1) && (pressed == 0))
588 BTIF_TRACE_DEBUG("%s: AVRC %s Release Faked earlier, drowned now",
589 __FUNCTION__, key_map[i].name);
592 send_key(uinput_fd, key_map[i].mapped_id, pressed);
593 if ((key_map[i].release_quirk == 1) && (pressed == 1))
595 GKI_delay(30); // 30ms
596 BTIF_TRACE_DEBUG("%s: AVRC %s Release quirk enabled, send release now",
597 __FUNCTION__, key_map[i].name);
598 send_key(uinput_fd, key_map[i].mapped_id, 0);
604 if (key_map[i].name == NULL)
605 BTIF_TRACE_ERROR("%s AVRCP: unknown button 0x%02X %s", __FUNCTION__,
606 p_remote_cmd->rc_id, status);
609 /***************************************************************************
610 * Function handle_rc_passthrough_rsp
612 * - Argument: tBTA_AV_REMOTE_RSP passthrough command response
614 * - Description: Remote control passthrough response handler
616 ***************************************************************************/
617 void handle_rc_passthrough_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp)
619 #if (AVRC_CTLR_INCLUDED == TRUE)
621 if (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)
624 if (p_remote_rsp->key_state == AVRC_STATE_RELEASE)
635 BTIF_TRACE_DEBUG("%s: rc_id=%d status=%s", __FUNCTION__, p_remote_rsp->rc_id, status);
637 release_transaction(p_remote_rsp->label);
638 if (bt_rc_ctrl_callbacks != NULL) {
639 HAL_CBACK(bt_rc_ctrl_callbacks, passthrough_rsp_cb, p_remote_rsp->rc_id, key_state);
644 BTIF_TRACE_ERROR("%s DUT does not support AVRCP controller role", __FUNCTION__);
647 BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __FUNCTION__);
651 void handle_uid_changed_notification(tBTA_AV_META_MSG *pmeta_msg, tAVRC_COMMAND *pavrc_command)
653 tAVRC_RESPONSE avrc_rsp = {0};
654 avrc_rsp.rsp.pdu = pavrc_command->pdu;
655 avrc_rsp.rsp.status = AVRC_STS_NO_ERROR;
656 avrc_rsp.rsp.opcode = pavrc_command->cmd.opcode;
658 avrc_rsp.reg_notif.event_id = pavrc_command->reg_notif.event_id;
659 avrc_rsp.reg_notif.param.uid_counter = 0;
661 send_metamsg_rsp(pmeta_msg->rc_handle, pmeta_msg->label, AVRC_RSP_INTERIM, &avrc_rsp);
662 send_metamsg_rsp(pmeta_msg->rc_handle, pmeta_msg->label, AVRC_RSP_CHANGED, &avrc_rsp);
667 /***************************************************************************
668 * Function handle_rc_metamsg_cmd
670 * - Argument: tBTA_AV_VENDOR Structure containing the received
673 * - Description: Remote control metamsg command handler (AVRCP 1.3)
675 ***************************************************************************/
676 void handle_rc_metamsg_cmd (tBTA_AV_META_MSG *pmeta_msg)
678 /* Parse the metamsg command and pass it on to BTL-IFS */
679 UINT8 scratch_buf[512] = {0};
680 tAVRC_COMMAND avrc_command = {0};
683 BTIF_TRACE_EVENT("+ %s", __FUNCTION__);
685 if (pmeta_msg->p_msg->hdr.opcode != AVRC_OP_VENDOR)
687 BTIF_TRACE_WARNING("Invalid opcode: %x", pmeta_msg->p_msg->hdr.opcode);
690 if (pmeta_msg->len < 3)
692 BTIF_TRACE_WARNING("Invalid length.Opcode: 0x%x, len: 0x%x", pmeta_msg->p_msg->hdr.opcode,
697 if (pmeta_msg->code >= AVRC_RSP_NOT_IMPL)
699 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
701 rc_transaction_t *transaction=NULL;
702 transaction=get_transaction_by_lbl(pmeta_msg->label);
703 if(NULL!=transaction)
705 handle_rc_metamsg_rsp(pmeta_msg);
709 BTIF_TRACE_DEBUG("%s:Discard vendor dependent rsp. code: %d label:%d.",
710 __FUNCTION__, pmeta_msg->code, pmeta_msg->label);
716 BTIF_TRACE_DEBUG("%s:Received vendor dependent rsp. code: %d len: %d. Not processing it.",
717 __FUNCTION__, pmeta_msg->code, pmeta_msg->len);
723 status=AVRC_ParsCommand(pmeta_msg->p_msg, &avrc_command, scratch_buf, sizeof(scratch_buf));
724 BTIF_TRACE_DEBUG("Received vendor command.code,PDU and label: %d, %d,%d",pmeta_msg->code,
725 avrc_command.cmd.pdu, pmeta_msg->label);
727 if (status != AVRC_STS_NO_ERROR)
730 BTIF_TRACE_WARNING("%s: Error in parsing received metamsg command. status: 0x%02x",
731 __FUNCTION__, status);
732 send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_command.pdu, status);
736 /* if RegisterNotification, add it to our registered queue */
738 if (avrc_command.cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION)
740 UINT8 event_id = avrc_command.reg_notif.event_id;
741 BTIF_TRACE_EVENT("%s:New register notification received.event_id:%s,label:0x%x,code:%x",
742 __FUNCTION__,dump_rc_notification_event_id(event_id), pmeta_msg->label,pmeta_msg->code);
743 btif_rc_cb.rc_notif[event_id-1].bNotify = TRUE;
744 btif_rc_cb.rc_notif[event_id-1].label = pmeta_msg->label;
746 if(event_id == AVRC_EVT_UIDS_CHANGE)
748 handle_uid_changed_notification(pmeta_msg, &avrc_command);
754 BTIF_TRACE_EVENT("%s: Passing received metamsg command to app. pdu: %s",
755 __FUNCTION__, dump_rc_pdu(avrc_command.cmd.pdu));
757 /* Since handle_rc_metamsg_cmd() itself is called from
758 *btif context, no context switching is required. Invoke
759 * btif_rc_upstreams_evt directly from here. */
760 btif_rc_upstreams_evt((uint16_t)avrc_command.cmd.pdu, &avrc_command, pmeta_msg->code,
765 /***************************************************************************
767 ** Function btif_rc_handler
769 ** Description RC event handler
771 ***************************************************************************/
772 void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data)
774 BTIF_TRACE_DEBUG ("%s event:%s", __FUNCTION__, dump_rc_event(event));
777 case BTA_AV_RC_OPEN_EVT:
779 BTIF_TRACE_DEBUG("Peer_features:%x", p_data->rc_open.peer_features);
780 handle_rc_connect( &(p_data->rc_open) );
783 case BTA_AV_RC_CLOSE_EVT:
785 handle_rc_disconnect( &(p_data->rc_close) );
788 case BTA_AV_REMOTE_CMD_EVT:
790 BTIF_TRACE_DEBUG("rc_id:0x%x key_state:%d", p_data->remote_cmd.rc_id,
791 p_data->remote_cmd.key_state);
792 handle_rc_passthrough_cmd( (&p_data->remote_cmd) );
795 #if (AVRC_CTLR_INCLUDED == TRUE)
796 case BTA_AV_REMOTE_RSP_EVT:
798 BTIF_TRACE_DEBUG("RSP: rc_id:0x%x key_state:%d", p_data->remote_rsp.rc_id,
799 p_data->remote_rsp.key_state);
800 handle_rc_passthrough_rsp( (&p_data->remote_rsp) );
804 case BTA_AV_RC_FEAT_EVT:
806 BTIF_TRACE_DEBUG("Peer_features:%x", p_data->rc_feat.peer_features);
807 btif_rc_cb.rc_features = p_data->rc_feat.peer_features;
808 handle_rc_features();
811 case BTA_AV_META_MSG_EVT:
813 BTIF_TRACE_DEBUG("BTA_AV_META_MSG_EVT code:%d label:%d", p_data->meta_msg.code,
814 p_data->meta_msg.label);
815 BTIF_TRACE_DEBUG(" company_id:0x%x len:%d handle:%d", p_data->meta_msg.company_id,
816 p_data->meta_msg.len, p_data->meta_msg.rc_handle);
817 /* handle the metamsg command */
818 handle_rc_metamsg_cmd(&(p_data->meta_msg));
822 BTIF_TRACE_DEBUG("Unhandled RC event : 0x%x", event);
826 /***************************************************************************
828 ** Function btif_rc_get_connected_peer
830 ** Description Fetches the connected headset's BD_ADDR if any
832 ***************************************************************************/
833 BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr)
835 if (btif_rc_cb.rc_connected == TRUE) {
836 bdcpy(peer_addr, btif_rc_cb.rc_addr);
842 /***************************************************************************
844 ** Function btif_rc_check_handle_pending_play
846 ** Description Clears the queued PLAY command. if bSend is TRUE, forwards to app
848 ***************************************************************************/
850 /* clear the queued PLAY command. if bSend is TRUE, forward to app */
851 void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp)
855 BTIF_TRACE_DEBUG("%s: bSendToApp=%d", __FUNCTION__, bSendToApp);
856 if (btif_rc_cb.rc_pending_play)
860 tBTA_AV_REMOTE_CMD remote_cmd;
861 APPL_TRACE_DEBUG("%s: Sending queued PLAYED event to app", __FUNCTION__);
863 memset (&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD));
864 remote_cmd.rc_handle = btif_rc_cb.rc_handle;
865 remote_cmd.rc_id = AVRC_ID_PLAY;
866 remote_cmd.hdr.ctype = AVRC_CMD_CTRL;
867 remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU;
869 /* delay sending to app, else there is a timing issue in the framework,
870 ** which causes the audio to be on th device's speaker. Delay between
874 /* send to app - both PRESSED & RELEASED */
875 remote_cmd.key_state = AVRC_STATE_PRESS;
876 handle_rc_passthrough_cmd( &remote_cmd );
880 remote_cmd.key_state = AVRC_STATE_RELEASE;
881 handle_rc_passthrough_cmd( &remote_cmd );
883 btif_rc_cb.rc_pending_play = FALSE;
887 /* Generic reject response */
888 static void send_reject_response (UINT8 rc_handle, UINT8 label, UINT8 pdu, UINT8 status)
890 UINT8 ctype = AVRC_RSP_REJ;
891 tAVRC_RESPONSE avrc_rsp;
892 BT_HDR *p_msg = NULL;
893 memset (&avrc_rsp, 0, sizeof(tAVRC_RESPONSE));
895 avrc_rsp.rsp.opcode = opcode_from_pdu(pdu);
896 avrc_rsp.rsp.pdu = pdu;
897 avrc_rsp.rsp.status = status;
899 if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(rc_handle, &avrc_rsp, &p_msg)) )
901 BTIF_TRACE_DEBUG("%s:Sending error notification to handle:%d. pdu:%s,status:0x%02x",
902 __FUNCTION__, rc_handle, dump_rc_pdu(pdu), status);
903 BTA_AvMetaRsp(rc_handle, label, ctype, p_msg);
907 /***************************************************************************
908 * Function send_metamsg_rsp
911 * rc_handle RC handle corresponding to the connected RC
912 * label Label of the RC response
914 * pmetamsg_resp Vendor response
916 * - Description: Remote control metamsg response handler (AVRCP 1.3)
918 ***************************************************************************/
919 static void send_metamsg_rsp (UINT8 rc_handle, UINT8 label, tBTA_AV_CODE code,
920 tAVRC_RESPONSE *pmetamsg_resp)
926 BTIF_TRACE_WARNING("%s: Invalid response received from application", __FUNCTION__);
930 BTIF_TRACE_EVENT("+%s: rc_handle: %d, label: %d, code: 0x%02x, pdu: %s", __FUNCTION__,
931 rc_handle, label, code, dump_rc_pdu(pmetamsg_resp->rsp.pdu));
933 if (pmetamsg_resp->rsp.status != AVRC_STS_NO_ERROR)
935 ctype = AVRC_RSP_REJ;
939 if ( code < AVRC_RSP_NOT_IMPL)
941 if (code == AVRC_CMD_NOTIF)
943 ctype = AVRC_RSP_INTERIM;
945 else if (code == AVRC_CMD_STATUS)
947 ctype = AVRC_RSP_IMPL_STBL;
951 ctype = AVRC_RSP_ACCEPT;
959 /* if response is for register_notification, make sure the rc has
960 actually registered for this */
961 if((pmetamsg_resp->rsp.pdu == AVRC_PDU_REGISTER_NOTIFICATION) && (code == AVRC_RSP_CHANGED))
963 BOOLEAN bSent = FALSE;
964 UINT8 event_id = pmetamsg_resp->reg_notif.event_id;
965 BOOLEAN bNotify = (btif_rc_cb.rc_connected) && (btif_rc_cb.rc_notif[event_id-1].bNotify);
967 /* de-register this notification for a CHANGED response */
968 btif_rc_cb.rc_notif[event_id-1].bNotify = FALSE;
969 BTIF_TRACE_DEBUG("%s rc_handle: %d. event_id: 0x%02d bNotify:%u", __FUNCTION__,
970 btif_rc_cb.rc_handle, event_id, bNotify);
973 BT_HDR *p_msg = NULL;
976 if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(btif_rc_cb.rc_handle,
977 pmetamsg_resp, &p_msg)) )
979 BTIF_TRACE_DEBUG("%s Sending notification to rc_handle: %d. event_id: 0x%02d",
980 __FUNCTION__, btif_rc_cb.rc_handle, event_id);
982 BTA_AvMetaRsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_notif[event_id-1].label,
987 BTIF_TRACE_WARNING("%s failed to build metamsg response. status: 0x%02x",
988 __FUNCTION__, status);
995 BTIF_TRACE_DEBUG("%s: Notification not sent, as there are no RC connections or the \
996 CT has not subscribed for event_id: %s", __FUNCTION__, dump_rc_notification_event_id(event_id));
1001 /* All other commands go here */
1003 BT_HDR *p_msg = NULL;
1006 status = AVRC_BldResponse(rc_handle, pmetamsg_resp, &p_msg);
1008 if (status == AVRC_STS_NO_ERROR)
1010 BTA_AvMetaRsp(rc_handle, label, ctype, p_msg);
1014 BTIF_TRACE_ERROR("%s: failed to build metamsg response. status: 0x%02x",
1015 __FUNCTION__, status);
1020 static UINT8 opcode_from_pdu(UINT8 pdu)
1026 case AVRC_PDU_NEXT_GROUP:
1027 case AVRC_PDU_PREV_GROUP: /* pass thru */
1028 opcode = AVRC_OP_PASS_THRU;
1031 default: /* vendor */
1032 opcode = AVRC_OP_VENDOR;
1039 /*******************************************************************************
1041 ** Function btif_rc_upstreams_evt
1043 ** Description Executes AVRC UPSTREAMS events in btif context.
1047 *******************************************************************************/
1048 static void btif_rc_upstreams_evt(UINT16 event, tAVRC_COMMAND *pavrc_cmd, UINT8 ctype, UINT8 label)
1050 BTIF_TRACE_EVENT("%s pdu: %s handle: 0x%x ctype:%x label:%x", __FUNCTION__,
1051 dump_rc_pdu(pavrc_cmd->pdu), btif_rc_cb.rc_handle, ctype, label);
1055 case AVRC_PDU_GET_PLAY_STATUS:
1057 FILL_PDU_QUEUE(IDX_GET_PLAY_STATUS_RSP, ctype, label, TRUE)
1058 HAL_CBACK(bt_rc_callbacks, get_play_status_cb);
1061 case AVRC_PDU_LIST_PLAYER_APP_ATTR:
1062 case AVRC_PDU_LIST_PLAYER_APP_VALUES:
1063 case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:
1064 case AVRC_PDU_SET_PLAYER_APP_VALUE:
1065 case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:
1066 case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:
1068 /* TODO: Add support for Application Settings */
1069 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_CMD);
1072 case AVRC_PDU_GET_ELEMENT_ATTR:
1074 btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
1076 memset(&element_attrs, 0, sizeof(element_attrs));
1077 if (pavrc_cmd->get_elem_attrs.num_attr == 0)
1079 /* CT requests for all attributes */
1081 num_attr = BTRC_MAX_ELEM_ATTR_SIZE;
1082 for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++)
1084 element_attrs[attr_cnt] = attr_cnt + 1;
1087 else if (pavrc_cmd->get_elem_attrs.num_attr == 0xFF)
1089 /* 0xff indicates, no attributes requested - reject */
1090 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu,
1091 AVRC_STS_BAD_PARAM);
1096 int attr_cnt, filled_attr_count;
1099 /* Attribute IDs from 1 to AVRC_MAX_NUM_MEDIA_ATTR_ID are only valid,
1100 * hence HAL definition limits the attributes to AVRC_MAX_NUM_MEDIA_ATTR_ID.
1101 * Fill only valid entries.
1103 for (attr_cnt = 0; (attr_cnt < pavrc_cmd->get_elem_attrs.num_attr) &&
1104 (num_attr < AVRC_MAX_NUM_MEDIA_ATTR_ID); attr_cnt++)
1106 if ((pavrc_cmd->get_elem_attrs.attrs[attr_cnt] > 0) &&
1107 (pavrc_cmd->get_elem_attrs.attrs[attr_cnt] <= AVRC_MAX_NUM_MEDIA_ATTR_ID))
1109 /* Skip the duplicate entries : PTS sends duplicate entries for Fragment cases
1111 for (filled_attr_count = 0; filled_attr_count < num_attr; filled_attr_count++)
1113 if (element_attrs[filled_attr_count] == pavrc_cmd->get_elem_attrs.attrs[attr_cnt])
1116 if (filled_attr_count == num_attr)
1118 element_attrs[num_attr] = pavrc_cmd->get_elem_attrs.attrs[attr_cnt];
1124 FILL_PDU_QUEUE(IDX_GET_ELEMENT_ATTR_RSP, ctype, label, TRUE);
1125 HAL_CBACK(bt_rc_callbacks, get_element_attr_cb, num_attr, element_attrs);
1128 case AVRC_PDU_REGISTER_NOTIFICATION:
1130 if(pavrc_cmd->reg_notif.event_id == BTRC_EVT_PLAY_POS_CHANGED &&
1131 pavrc_cmd->reg_notif.param == 0)
1133 BTIF_TRACE_WARNING("%s Device registering position changed with illegal param 0.",
1135 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_PARAM);
1136 /* de-register this notification for a rejected response */
1137 btif_rc_cb.rc_notif[BTRC_EVT_PLAY_POS_CHANGED - 1].bNotify = FALSE;
1140 HAL_CBACK(bt_rc_callbacks, register_notification_cb, pavrc_cmd->reg_notif.event_id,
1141 pavrc_cmd->reg_notif.param);
1144 case AVRC_PDU_INFORM_DISPLAY_CHARSET:
1146 tAVRC_RESPONSE avrc_rsp;
1147 BTIF_TRACE_EVENT("%s() AVRC_PDU_INFORM_DISPLAY_CHARSET", __FUNCTION__);
1148 if(btif_rc_cb.rc_connected == TRUE)
1150 memset(&(avrc_rsp.inform_charset), 0, sizeof(tAVRC_RSP));
1151 avrc_rsp.inform_charset.opcode=opcode_from_pdu(AVRC_PDU_INFORM_DISPLAY_CHARSET);
1152 avrc_rsp.inform_charset.pdu=AVRC_PDU_INFORM_DISPLAY_CHARSET;
1153 avrc_rsp.inform_charset.status=AVRC_STS_NO_ERROR;
1154 send_metamsg_rsp(btif_rc_cb.rc_handle, label, ctype, &avrc_rsp);
1160 send_reject_response (btif_rc_cb.rc_handle, label, pavrc_cmd->pdu,
1161 (pavrc_cmd->pdu == AVRC_PDU_SEARCH)?AVRC_STS_SEARCH_NOT_SUP:AVRC_STS_BAD_CMD);
1170 /*******************************************************************************
1172 ** Function btif_rc_upstreams_rsp_evt
1174 ** Description Executes AVRC UPSTREAMS response events in btif context.
1178 *******************************************************************************/
1179 static void btif_rc_upstreams_rsp_evt(UINT16 event, tAVRC_RESPONSE *pavrc_resp, UINT8 ctype, UINT8 label)
1181 BTIF_TRACE_EVENT("%s pdu: %s handle: 0x%x ctype:%x label:%x", __FUNCTION__,
1182 dump_rc_pdu(pavrc_resp->pdu), btif_rc_cb.rc_handle, ctype, label);
1184 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
1187 case AVRC_PDU_REGISTER_NOTIFICATION:
1189 if(AVRC_RSP_CHANGED==ctype)
1190 btif_rc_cb.rc_volume=pavrc_resp->reg_notif.param.volume;
1191 HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->reg_notif.param.volume,ctype)
1195 case AVRC_PDU_SET_ABSOLUTE_VOLUME:
1197 BTIF_TRACE_DEBUG("Set absolute volume change event received: volume %d,ctype %d",
1198 pavrc_resp->volume.volume,ctype);
1199 if(AVRC_RSP_ACCEPT==ctype)
1200 btif_rc_cb.rc_volume=pavrc_resp->volume.volume;
1201 HAL_CBACK(bt_rc_callbacks,volume_change_cb,pavrc_resp->volume.volume,ctype)
1211 /************************************************************************************
1212 ** AVRCP API Functions
1213 ************************************************************************************/
1215 /*******************************************************************************
1219 ** Description Initializes the AVRC interface
1221 ** Returns bt_status_t
1223 *******************************************************************************/
1224 static bt_status_t init(btrc_callbacks_t* callbacks )
1226 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1227 bt_status_t result = BT_STATUS_SUCCESS;
1229 if (bt_rc_callbacks)
1230 return BT_STATUS_DONE;
1232 bt_rc_callbacks = callbacks;
1233 memset (&btif_rc_cb, 0, sizeof(btif_rc_cb));
1234 btif_rc_cb.rc_vol_label=MAX_LABEL;
1235 btif_rc_cb.rc_volume=MAX_VOLUME;
1241 /*******************************************************************************
1243 ** Function init_ctrl
1245 ** Description Initializes the AVRC interface
1247 ** Returns bt_status_t
1249 *******************************************************************************/
1250 static bt_status_t init_ctrl(btrc_ctrl_callbacks_t* callbacks )
1252 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1253 bt_status_t result = BT_STATUS_SUCCESS;
1255 if (bt_rc_ctrl_callbacks)
1256 return BT_STATUS_DONE;
1258 bt_rc_ctrl_callbacks = callbacks;
1259 memset (&btif_rc_cb, 0, sizeof(btif_rc_cb));
1260 btif_rc_cb.rc_vol_label=MAX_LABEL;
1261 btif_rc_cb.rc_volume=MAX_VOLUME;
1267 /***************************************************************************
1269 ** Function get_play_status_rsp
1271 ** Description Returns the current play status.
1272 ** This method is called in response to
1273 ** GetPlayStatus request.
1275 ** Returns bt_status_t
1277 ***************************************************************************/
1278 static bt_status_t get_play_status_rsp(btrc_play_status_t play_status, uint32_t song_len,
1281 tAVRC_RESPONSE avrc_rsp;
1283 memset(&(avrc_rsp.get_play_status), 0, sizeof(tAVRC_GET_PLAY_STATUS_RSP));
1284 avrc_rsp.get_play_status.song_len = song_len;
1285 avrc_rsp.get_play_status.song_pos = song_pos;
1286 avrc_rsp.get_play_status.play_status = play_status;
1288 avrc_rsp.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS;
1289 avrc_rsp.get_play_status.opcode = opcode_from_pdu(AVRC_PDU_GET_PLAY_STATUS);
1290 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
1291 /* Send the response */
1292 SEND_METAMSG_RSP(IDX_GET_PLAY_STATUS_RSP, &avrc_rsp);
1293 return BT_STATUS_SUCCESS;
1296 /***************************************************************************
1298 ** Function get_element_attr_rsp
1300 ** Description Returns the current songs' element attributes
1303 ** Returns bt_status_t
1305 ***************************************************************************/
1306 static bt_status_t get_element_attr_rsp(uint8_t num_attr, btrc_element_attr_val_t *p_attrs)
1308 tAVRC_RESPONSE avrc_rsp;
1310 tAVRC_ATTR_ENTRY element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
1312 memset(element_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr);
1316 avrc_rsp.get_play_status.status = AVRC_STS_BAD_PARAM;
1320 for (i=0; i<num_attr; i++) {
1321 element_attrs[i].attr_id = p_attrs[i].attr_id;
1322 element_attrs[i].name.charset_id = AVRC_CHARSET_ID_UTF8;
1323 element_attrs[i].name.str_len = (UINT16)strlen((char *)p_attrs[i].text);
1324 element_attrs[i].name.p_str = p_attrs[i].text;
1325 BTIF_TRACE_DEBUG("%s attr_id:0x%x, charset_id:0x%x, str_len:%d, str:%s",
1326 __FUNCTION__, (unsigned int)element_attrs[i].attr_id,
1327 element_attrs[i].name.charset_id, element_attrs[i].name.str_len,
1328 element_attrs[i].name.p_str);
1330 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
1332 avrc_rsp.get_elem_attrs.num_attr = num_attr;
1333 avrc_rsp.get_elem_attrs.p_attrs = element_attrs;
1334 avrc_rsp.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR;
1335 avrc_rsp.get_elem_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ELEMENT_ATTR);
1336 /* Send the response */
1337 SEND_METAMSG_RSP(IDX_GET_ELEMENT_ATTR_RSP, &avrc_rsp);
1338 return BT_STATUS_SUCCESS;
1341 /***************************************************************************
1343 ** Function register_notification_rsp
1345 ** Description Response to the register notification request.
1348 ** Returns bt_status_t
1350 ***************************************************************************/
1351 static bt_status_t register_notification_rsp(btrc_event_id_t event_id,
1352 btrc_notification_type_t type, btrc_register_notification_t *p_param)
1354 tAVRC_RESPONSE avrc_rsp;
1356 BTIF_TRACE_EVENT("## %s ## event_id:%s", __FUNCTION__, dump_rc_notification_event_id(event_id));
1357 if (btif_rc_cb.rc_notif[event_id-1].bNotify == FALSE)
1359 BTIF_TRACE_ERROR("Avrcp Event id not registered: event_id = %x", event_id);
1360 return BT_STATUS_NOT_READY;
1362 memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP));
1363 avrc_rsp.reg_notif.event_id = event_id;
1367 case BTRC_EVT_PLAY_STATUS_CHANGED:
1368 avrc_rsp.reg_notif.param.play_status = p_param->play_status;
1369 if (avrc_rsp.reg_notif.param.play_status == PLAY_STATUS_PLAYING)
1370 btif_av_clear_remote_suspend_flag();
1372 case BTRC_EVT_TRACK_CHANGE:
1373 memcpy(&(avrc_rsp.reg_notif.param.track), &(p_param->track), sizeof(btrc_uid_t));
1375 case BTRC_EVT_PLAY_POS_CHANGED:
1376 avrc_rsp.reg_notif.param.play_pos = p_param->song_pos;
1379 BTIF_TRACE_WARNING("%s : Unhandled event ID : 0x%x", __FUNCTION__, event_id);
1380 return BT_STATUS_UNHANDLED;
1383 avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
1384 avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION);
1385 avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
1387 /* Send the response. */
1388 send_metamsg_rsp(btif_rc_cb.rc_handle, btif_rc_cb.rc_notif[event_id-1].label,
1389 ((type == BTRC_NOTIFICATION_TYPE_INTERIM)?AVRC_CMD_NOTIF:AVRC_RSP_CHANGED), &avrc_rsp);
1390 return BT_STATUS_SUCCESS;
1393 /***************************************************************************
1395 ** Function set_volume
1397 ** Description Send current volume setting to remote side.
1398 ** Support limited to SetAbsoluteVolume
1399 ** This can be enhanced to support Relative Volume (AVRCP 1.0).
1400 ** With RelateVolume, we will send VOLUME_UP/VOLUME_DOWN
1401 ** as opposed to absolute volume level
1402 ** volume: Should be in the range 0-127. bit7 is reseved and cannot be set
1404 ** Returns bt_status_t
1406 ***************************************************************************/
1407 static bt_status_t set_volume(uint8_t volume)
1409 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1411 tAVRC_STS status = BT_STATUS_UNSUPPORTED;
1412 rc_transaction_t *p_transaction=NULL;
1414 if(btif_rc_cb.rc_volume==volume)
1416 status=BT_STATUS_DONE;
1417 BTIF_TRACE_ERROR("%s: volume value already set earlier: 0x%02x",__FUNCTION__, volume);
1421 if ((btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG) &&
1422 (btif_rc_cb.rc_features & BTA_AV_FEAT_ADV_CTRL))
1424 tAVRC_COMMAND avrc_cmd = {0};
1425 BT_HDR *p_msg = NULL;
1427 BTIF_TRACE_DEBUG("%s: Peer supports absolute volume. newVolume=%d", __FUNCTION__, volume);
1428 avrc_cmd.volume.opcode = AVRC_OP_VENDOR;
1429 avrc_cmd.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME;
1430 avrc_cmd.volume.status = AVRC_STS_NO_ERROR;
1431 avrc_cmd.volume.volume = volume;
1433 if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR)
1435 bt_status_t tran_status=get_transaction(&p_transaction);
1436 if(BT_STATUS_SUCCESS == tran_status && NULL!=p_transaction)
1438 BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d",
1439 __FUNCTION__,p_transaction->lbl);
1440 BTA_AvMetaCmd(btif_rc_cb.rc_handle,p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
1441 status = BT_STATUS_SUCCESS;
1447 BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x",
1448 __FUNCTION__, tran_status);
1449 status = BT_STATUS_FAIL;
1454 BTIF_TRACE_ERROR("%s: failed to build absolute volume command. status: 0x%02x",
1455 __FUNCTION__, status);
1456 status = BT_STATUS_FAIL;
1460 status=BT_STATUS_NOT_READY;
1465 /***************************************************************************
1467 ** Function register_volumechange
1469 ** Description Register for volume change notification from remote side.
1473 ***************************************************************************/
1475 static void register_volumechange (UINT8 lbl)
1477 tAVRC_COMMAND avrc_cmd = {0};
1478 BT_HDR *p_msg = NULL;
1479 tAVRC_STS BldResp=AVRC_STS_BAD_CMD;
1480 rc_transaction_t *p_transaction=NULL;
1482 BTIF_TRACE_DEBUG("%s called with label:%d",__FUNCTION__,lbl);
1484 avrc_cmd.cmd.opcode=0x00;
1485 avrc_cmd.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
1486 avrc_cmd.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE;
1487 avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR;
1489 BldResp=AVRC_BldCommand(&avrc_cmd, &p_msg);
1490 if(AVRC_STS_NO_ERROR==BldResp && p_msg)
1492 p_transaction=get_transaction_by_lbl(lbl);
1493 if(NULL!=p_transaction)
1495 BTA_AvMetaCmd(btif_rc_cb.rc_handle,p_transaction->lbl, AVRC_CMD_NOTIF, p_msg);
1496 BTIF_TRACE_DEBUG("%s:BTA_AvMetaCmd called",__FUNCTION__);
1502 BTIF_TRACE_ERROR("%s transaction not obtained with label: %d",__FUNCTION__,lbl);
1506 BTIF_TRACE_ERROR("%s failed to build command:%d",__FUNCTION__,BldResp);
1510 /***************************************************************************
1512 ** Function handle_rc_metamsg_rsp
1514 ** Description Handle RC metamessage response
1518 ***************************************************************************/
1519 static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg)
1521 tAVRC_RESPONSE avrc_response = {0};
1522 UINT8 scratch_buf[512] = {0};
1523 tAVRC_STS status = BT_STATUS_UNSUPPORTED;
1525 if(AVRC_OP_VENDOR==pmeta_msg->p_msg->hdr.opcode &&(AVRC_RSP_CHANGED==pmeta_msg->code
1526 || AVRC_RSP_INTERIM==pmeta_msg->code || AVRC_RSP_ACCEPT==pmeta_msg->code
1527 || AVRC_RSP_REJ==pmeta_msg->code || AVRC_RSP_NOT_IMPL==pmeta_msg->code))
1529 status=AVRC_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, sizeof(scratch_buf));
1530 BTIF_TRACE_DEBUG("%s: code %d,event ID %d,PDU %x,parsing status %d, label:%d",
1531 __FUNCTION__,pmeta_msg->code,avrc_response.reg_notif.event_id,avrc_response.reg_notif.pdu,
1532 status, pmeta_msg->label);
1534 if (status != AVRC_STS_NO_ERROR)
1536 if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu
1537 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id
1538 && btif_rc_cb.rc_vol_label==pmeta_msg->label)
1540 btif_rc_cb.rc_vol_label=MAX_LABEL;
1541 release_transaction(btif_rc_cb.rc_vol_label);
1543 else if(AVRC_PDU_SET_ABSOLUTE_VOLUME==avrc_response.rsp.pdu)
1545 release_transaction(pmeta_msg->label);
1549 else if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu
1550 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id
1551 && btif_rc_cb.rc_vol_label!=pmeta_msg->label)
1553 // Just discard the message, if the device sends back with an incorrect label
1554 BTIF_TRACE_DEBUG("%s:Discarding register notfn in rsp.code: %d and label %d",
1555 __FUNCTION__, pmeta_msg->code, pmeta_msg->label);
1561 BTIF_TRACE_DEBUG("%s:Received vendor dependent in adv ctrl rsp. code: %d len: %d. Not processing it.",
1562 __FUNCTION__, pmeta_msg->code, pmeta_msg->len);
1566 if(AVRC_PDU_REGISTER_NOTIFICATION==avrc_response.rsp.pdu
1567 && AVRC_EVT_VOLUME_CHANGE==avrc_response.reg_notif.event_id
1568 && AVRC_RSP_CHANGED==pmeta_msg->code)
1570 /* re-register for volume change notification */
1571 // Do not re-register for rejected case, as it might get into endless loop
1572 register_volumechange(btif_rc_cb.rc_vol_label);
1574 else if(AVRC_PDU_SET_ABSOLUTE_VOLUME==avrc_response.rsp.pdu)
1576 /* free up the label here */
1577 release_transaction(pmeta_msg->label);
1580 BTIF_TRACE_EVENT("%s: Passing received metamsg response to app. pdu: %s",
1581 __FUNCTION__, dump_rc_pdu(avrc_response.pdu));
1582 btif_rc_upstreams_rsp_evt((uint16_t)avrc_response.rsp.pdu, &avrc_response, pmeta_msg->code,
1587 /***************************************************************************
1591 ** Description Closes the AVRC interface
1595 ***************************************************************************/
1596 static void cleanup()
1598 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1600 if (bt_rc_callbacks)
1602 bt_rc_callbacks = NULL;
1604 memset(&btif_rc_cb, 0, sizeof(btif_rc_cb_t));
1606 BTIF_TRACE_EVENT("## %s ## completed", __FUNCTION__);
1609 /***************************************************************************
1611 ** Function cleanup_ctrl
1613 ** Description Closes the AVRC Controller interface
1617 ***************************************************************************/
1618 static void cleanup_ctrl()
1620 BTIF_TRACE_EVENT("## %s ##", __FUNCTION__);
1622 if (bt_rc_ctrl_callbacks)
1624 bt_rc_ctrl_callbacks = NULL;
1626 memset(&btif_rc_cb, 0, sizeof(btif_rc_cb_t));
1628 BTIF_TRACE_EVENT("## %s ## completed", __FUNCTION__);
1631 static bt_status_t send_passthrough_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code, uint8_t key_state)
1633 tAVRC_STS status = BT_STATUS_UNSUPPORTED;
1634 #if (AVRC_CTLR_INCLUDED == TRUE)
1636 rc_transaction_t *p_transaction=NULL;
1637 BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __FUNCTION__,
1638 key_code, key_state);
1639 if (btif_rc_cb.rc_features & BTA_AV_FEAT_RCTG)
1641 bt_status_t tran_status = get_transaction(&p_transaction);
1642 if(BT_STATUS_SUCCESS == tran_status && NULL != p_transaction)
1644 BTA_AvRemoteCmd(btif_rc_cb.rc_handle, p_transaction->lbl,
1645 (tBTA_AV_RC)key_code, (tBTA_AV_STATE)key_state);
1646 status = BT_STATUS_SUCCESS;
1647 BTIF_TRACE_DEBUG("%s: succesfully sent passthrough command to BTA", __FUNCTION__);
1651 status = BT_STATUS_FAIL;
1652 BTIF_TRACE_DEBUG("%s: error in fetching transaction", __FUNCTION__);
1657 status = BT_STATUS_FAIL;
1658 BTIF_TRACE_DEBUG("%s: feature not supported", __FUNCTION__);
1661 BTIF_TRACE_DEBUG("%s: feature not enabled", __FUNCTION__);
1666 static const btrc_interface_t bt_rc_interface = {
1667 sizeof(bt_rc_interface),
1669 get_play_status_rsp,
1670 NULL, /* list_player_app_attr_rsp */
1671 NULL, /* list_player_app_value_rsp */
1672 NULL, /* get_player_app_value_rsp */
1673 NULL, /* get_player_app_attr_text_rsp */
1674 NULL, /* get_player_app_value_text_rsp */
1675 get_element_attr_rsp,
1676 NULL, /* set_player_app_value_rsp */
1677 register_notification_rsp,
1682 static const btrc_ctrl_interface_t bt_rc_ctrl_interface = {
1683 sizeof(bt_rc_ctrl_interface),
1685 send_passthrough_cmd,
1689 /*******************************************************************************
1691 ** Function btif_rc_get_interface
1693 ** Description Get the AVRCP Target callback interface
1695 ** Returns btav_interface_t
1697 *******************************************************************************/
1698 const btrc_interface_t *btif_rc_get_interface(void)
1700 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1701 return &bt_rc_interface;
1704 /*******************************************************************************
1706 ** Function btif_rc_ctrl_get_interface
1708 ** Description Get the AVRCP Controller callback interface
1710 ** Returns btav_interface_t
1712 *******************************************************************************/
1713 const btrc_ctrl_interface_t *btif_rc_ctrl_get_interface(void)
1715 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1716 return &bt_rc_ctrl_interface;
1719 /*******************************************************************************
1720 ** Function initialize_transaction
1722 ** Description Initializes fields of the transaction structure
1725 *******************************************************************************/
1726 static void initialize_transaction(int lbl)
1728 pthread_mutex_lock(&device.lbllock);
1729 if(lbl < MAX_TRANSACTIONS_PER_SESSION)
1731 device.transaction[lbl].lbl = lbl;
1732 device.transaction[lbl].in_use=FALSE;
1733 device.transaction[lbl].handle=0;
1735 pthread_mutex_unlock(&device.lbllock);
1738 /*******************************************************************************
1739 ** Function lbl_init
1741 ** Description Initializes label structures and mutexes.
1744 *******************************************************************************/
1747 memset(&device,0,sizeof(rc_device_t));
1748 pthread_mutexattr_t attr;
1749 pthread_mutexattr_init(&attr);
1750 pthread_mutex_init(&(device.lbllock), &attr);
1751 pthread_mutexattr_destroy(&attr);
1752 init_all_transactions();
1755 /*******************************************************************************
1757 ** Function init_all_transactions
1759 ** Description Initializes all transactions
1762 *******************************************************************************/
1763 void init_all_transactions()
1766 for(txn_indx=0; txn_indx < MAX_TRANSACTIONS_PER_SESSION; txn_indx++)
1768 initialize_transaction(txn_indx);
1772 /*******************************************************************************
1774 ** Function get_transaction_by_lbl
1776 ** Description Will return a transaction based on the label. If not inuse
1777 ** will return an error.
1779 ** Returns bt_status_t
1780 *******************************************************************************/
1781 rc_transaction_t *get_transaction_by_lbl(UINT8 lbl)
1783 rc_transaction_t *transaction = NULL;
1784 pthread_mutex_lock(&device.lbllock);
1786 /* Determine if this is a valid label */
1787 if (lbl < MAX_TRANSACTIONS_PER_SESSION)
1789 if (FALSE==device.transaction[lbl].in_use)
1795 transaction = &(device.transaction[lbl]);
1796 BTIF_TRACE_DEBUG("%s: Got transaction.label: %d",__FUNCTION__,lbl);
1800 pthread_mutex_unlock(&device.lbllock);
1804 /*******************************************************************************
1806 ** Function get_transaction
1808 ** Description Obtains the transaction details.
1810 ** Returns bt_status_t
1811 *******************************************************************************/
1813 bt_status_t get_transaction(rc_transaction_t **ptransaction)
1815 bt_status_t result = BT_STATUS_NOMEM;
1817 pthread_mutex_lock(&device.lbllock);
1819 // Check for unused transactions
1820 for (i=0; i<MAX_TRANSACTIONS_PER_SESSION; i++)
1822 if (FALSE==device.transaction[i].in_use)
1824 BTIF_TRACE_DEBUG("%s:Got transaction.label: %d",__FUNCTION__,device.transaction[i].lbl);
1825 device.transaction[i].in_use = TRUE;
1826 *ptransaction = &(device.transaction[i]);
1827 result = BT_STATUS_SUCCESS;
1832 pthread_mutex_unlock(&device.lbllock);
1837 /*******************************************************************************
1839 ** Function release_transaction
1841 ** Description Will release a transaction for reuse
1843 ** Returns bt_status_t
1844 *******************************************************************************/
1845 void release_transaction(UINT8 lbl)
1847 rc_transaction_t *transaction = get_transaction_by_lbl(lbl);
1849 /* If the transaction is in use... */
1850 if (transaction != NULL)
1852 BTIF_TRACE_DEBUG("%s: lbl: %d", __FUNCTION__, lbl);
1853 initialize_transaction(lbl);
1857 /*******************************************************************************
1859 ** Function lbl_destroy
1861 ** Description Cleanup of the mutex
1864 *******************************************************************************/
1867 pthread_mutex_destroy(&(device.lbllock));