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