1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19
20 /*****************************************************************************
21 *
22 * Filename: btif_rc.c
23 *
24 * Description: Bluetooth AVRC implementation
25 *
26 *****************************************************************************/
27 #include <hardware/bluetooth.h>
28 #include <fcntl.h>
29 #include "bta_api.h"
30 #include "bta_av_api.h"
31 #include "avrc_defs.h"
32 #include "bd.h"
33 #include "gki.h"
34
35 #define LOG_TAG "BTIF_RC"
36 #include "btif_common.h"
37
38 /*****************************************************************************
39 ** Constants & Macros
40 ******************************************************************************/
41 #define BTIF_RC_USE_UINPUT TRUE
42 #include "uinput.h"
43
44 /* cod value for Headsets */
45 #define COD_AV_HEADSETS 0x0404
46
47
48 /*****************************************************************************
49 ** Local type definitions
50 ******************************************************************************/
51 typedef struct {
52 BOOLEAN rc_connected;
53 UINT8 rc_handle;
54 BD_ADDR rc_addr;
55 UINT16 rc_pending_play;
56 } btif_rc_cb_t;
57
58 #ifdef BTIF_RC_USE_UINPUT
59 #define MAX_UINPUT_PATHS 3
60 static const char* uinput_dev_path[] =
61 {"/dev/uinput", "/dev/input/uinput", "/dev/misc/uinput" };
62 static int uinput_fd = -1;
63
64 static int send_event (int fd, uint16_t type, uint16_t code, int32_t value);
65 static void send_key (int fd, uint16_t key, int pressed);
66 static int uinput_driver_check();
67 static int uinput_create(char *name);
68 static int init_uinput (void);
69 static void close_uinput (void);
70
71 static struct {
72 const char *name;
73 uint8_t avrcp;
74 uint16_t mapped_id;
75 uint8_t release_quirk;
76 } key_map[] = {
77 { "PLAY", AVRC_ID_PLAY, KEY_PLAYCD, 1 },
78 { "STOP", AVRC_ID_STOP, KEY_STOPCD, 0 },
79 { "PAUSE", AVRC_ID_PAUSE, KEY_PAUSECD, 1 },
80 { "FORWARD", AVRC_ID_FORWARD, KEY_NEXTSONG, 0 },
81 { "BACKWARD", AVRC_ID_BACKWARD, KEY_PREVIOUSSONG, 0 },
82 { "REWIND", AVRC_ID_REWIND, KEY_REWIND, 0 },
83 { "FAST FORWARD", AVRC_ID_FAST_FOR, KEY_FORWARD, 0 },
84 { NULL, 0, 0, 0 }
85 };
86 #endif /* BTIF_RC_USE_UINPUT */
87
88
89 /*****************************************************************************
90 ** Static variables
91 ******************************************************************************/
92 static btif_rc_cb_t btif_rc_cb;
93
94 /*****************************************************************************
95 ** Static functions
96 ******************************************************************************/
97
98 /*****************************************************************************
99 ** Externs
100 ******************************************************************************/
101 extern BOOLEAN btif_hf_call_terminated_recently();
102 extern BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod);
103 extern BOOLEAN btif_av_is_connected(void);
104
105 /*****************************************************************************
106 ** Functions
107 ******************************************************************************/
108
109
110 #ifdef BTIF_RC_USE_UINPUT
111 /*****************************************************************************
112 ** Local uinput helper functions
113 ******************************************************************************/
send_event(int fd,uint16_t type,uint16_t code,int32_t value)114 int send_event (int fd, uint16_t type, uint16_t code, int32_t value)
115 {
116 struct uinput_event event;
117
118 memset(&event, 0, sizeof(event));
119 event.type = type;
120 event.code = code;
121 event.value = value;
122
123 return write(fd, &event, sizeof(event));
124 }
125
send_key(int fd,uint16_t key,int pressed)126 void send_key (int fd, uint16_t key, int pressed)
127 {
128 if (fd < 0) {
129 return;
130 }
131
132 BTIF_TRACE_DEBUG3("AVRCP: Send key %d (%d) fd=%d", key, pressed, fd);
133 send_event(fd, EV_KEY, key, pressed);
134 send_event(fd, EV_SYN, SYN_REPORT, 0);
135 }
136
137 /************** uinput related functions **************/
uinput_driver_check()138 int uinput_driver_check()
139 {
140 uint32_t i;
141 for (i=0; i < MAX_UINPUT_PATHS; i++)
142 {
143 if (access(uinput_dev_path[i], O_RDWR) == 0) {
144 return 0;
145 }
146 }
147 BTIF_TRACE_ERROR1("%s ERROR: uinput device is not in the system", __FUNCTION__);
148 return -1;
149 }
150
uinput_create(char * name)151 int uinput_create(char *name)
152 {
153 struct uinput_dev dev;
154 int fd, err, x = 0;
155
156 for(x=0; x < MAX_UINPUT_PATHS; x++)
157 {
158 fd = open(uinput_dev_path[x], O_RDWR);
159 if (fd < 0)
160 continue;
161 break;
162 }
163 if (x == MAX_UINPUT_PATHS) {
164 BTIF_TRACE_ERROR1("%s ERROR: uinput device open failed", __FUNCTION__);
165 return -1;
166 }
167 memset(&dev, 0, sizeof(dev));
168 if (name)
169 strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
170
171 dev.id.bustype = BUS_BLUETOOTH;
172 dev.id.vendor = 0x0000;
173 dev.id.product = 0x0000;
174 dev.id.version = 0x0000;
175
176 if (write(fd, &dev, sizeof(dev)) < 0) {
177 BTIF_TRACE_ERROR1("%s Unable to write device information", __FUNCTION__);
178 close(fd);
179 return -1;
180 }
181
182 ioctl(fd, UI_SET_EVBIT, EV_KEY);
183 ioctl(fd, UI_SET_EVBIT, EV_REL);
184 ioctl(fd, UI_SET_EVBIT, EV_SYN);
185
186 for (x = 0; key_map[x].name != NULL; x++)
187 ioctl(fd, UI_SET_KEYBIT, key_map[x].mapped_id);
188
189 for(x = 0; x < KEY_MAX; x++)
190 ioctl(fd, UI_SET_KEYBIT, x);
191
192 if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
193 BTIF_TRACE_ERROR1("%s Unable to create uinput device", __FUNCTION__);
194 close(fd);
195 return -1;
196 }
197 return fd;
198 }
199
init_uinput(void)200 int init_uinput (void)
201 {
202 char *name = "AVRCP";
203
204 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
205 uinput_fd = uinput_create(name);
206 if (uinput_fd < 0) {
207 BTIF_TRACE_ERROR3("%s AVRCP: Failed to initialize uinput for %s (%d)",
208 __FUNCTION__, name, uinput_fd);
209 } else {
210 BTIF_TRACE_DEBUG3("%s AVRCP: Initialized uinput for %s (fd=%d)",
211 __FUNCTION__, name, uinput_fd);
212 }
213 return uinput_fd;
214 }
215
close_uinput(void)216 void close_uinput (void)
217 {
218 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
219 if (uinput_fd > 0) {
220 ioctl(uinput_fd, UI_DEV_DESTROY);
221
222 close(uinput_fd);
223 uinput_fd = -1;
224 }
225 }
226 #endif // BTA_AVRCP_FORCE_USE_UINPUT
227
dump_rc_event_name(tBTA_AV_EVT event)228 const char *dump_rc_event_name(tBTA_AV_EVT event)
229 {
230 switch(event) {
231 case BTA_AV_RC_OPEN_EVT: return "BTA_AV_RC_OPEN_EVT";
232 case BTA_AV_RC_CLOSE_EVT: return "BTA_AV_RC_CLOSE_EVT";
233 case BTA_AV_REMOTE_CMD_EVT: return "BTA_AV_REMOTE_CMD_EVT";
234 case BTA_AV_REMOTE_RSP_EVT: return "BTA_AV_REMOTE_RSP_EVT";
235 case BTA_AV_VENDOR_CMD_EVT: return "BTA_AV_VENDOR_CMD_EVT";
236 case BTA_AV_VENDOR_RSP_EVT: return "BTA_AV_VENDOR_RSP_EVT";
237 default: return "UNKNOWN_EVENT";
238 }
239 }
240
241 /***************************************************************************
242 * Function handle_rc_connect
243 *
244 * - Argument: tBTA_AV_RC_OPEN RC open data structure
245 *
246 * - Description: RC connection event handler
247 *
248 ***************************************************************************/
handle_rc_connect(tBTA_AV_RC_OPEN * p_rc_open)249 void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open)
250 {
251 BTIF_TRACE_DEBUG2("%s: rc_handle: %d", __FUNCTION__, p_rc_open->rc_handle);
252
253 #ifdef BTIF_RC_USE_UINPUT
254 init_uinput();
255 #endif
256
257 memcpy(btif_rc_cb.rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR));
258 btif_rc_cb.rc_connected = TRUE;
259 btif_rc_cb.rc_handle = p_rc_open->rc_handle;
260 }
261
262 /***************************************************************************
263 * Function handle_rc_disconnect
264 *
265 * - Argument: tBTA_AV_RC_CLOSE RC close data structure
266 *
267 * - Description: RC disconnection event handler
268 *
269 ***************************************************************************/
handle_rc_disconnect(tBTA_AV_RC_CLOSE * p_rc_close)270 void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close)
271 {
272 BTIF_TRACE_DEBUG2("%s: rc_handle: %d", __FUNCTION__, p_rc_close->rc_handle);
273
274 btif_rc_cb.rc_handle = 0;
275 btif_rc_cb.rc_connected = FALSE;
276 memset(btif_rc_cb.rc_addr, 0, sizeof(BD_ADDR));
277 #ifdef BTIF_RC_USE_UINPUT
278 close_uinput();
279 #endif /* BTIF_RC_USE_UINPUT */
280 }
281
282 /***************************************************************************
283 * Function handle_rc_passthrough_cmd
284 *
285 * - Argument: tBTA_AV_RC rc_id remote control command ID
286 * tBTA_AV_STATE key_state status of key press
287 *
288 * - Description: Remote control command handler
289 *
290 ***************************************************************************/
handle_rc_passthrough_cmd(tBTA_AV_REMOTE_CMD * p_remote_cmd)291 void handle_rc_passthrough_cmd ( tBTA_AV_REMOTE_CMD *p_remote_cmd)
292 {
293 const char *status;
294 int pressed, i;
295
296 btif_rc_cb.rc_handle = p_remote_cmd->rc_handle;
297
298 /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up this PLAY */
299 if (p_remote_cmd)
300 {
301 /* queue AVRC PLAY if GAVDTP Open notification to app is pending (2 second timer) */
302 if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected()))
303 {
304 if (p_remote_cmd->key_state == AVRC_STATE_PRESS)
305 {
306 APPL_TRACE_WARNING1("%s: AVDT not open, queuing the PLAY command", __FUNCTION__);
307 btif_rc_cb.rc_pending_play = TRUE;
308 }
309 return;
310 }
311
312 if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (btif_rc_cb.rc_pending_play))
313 {
314 APPL_TRACE_WARNING1("%s: Clear the pending PLAY on PAUSE received", __FUNCTION__);
315 btif_rc_cb.rc_pending_play = FALSE;
316 return;
317 }
318 }
319 if (p_remote_cmd->key_state == AVRC_STATE_RELEASE) {
320 status = "released";
321 pressed = 0;
322 } else {
323 status = "pressed";
324 pressed = 1;
325 }
326
327 /* If this is Play/Pause command (press or release) before processing, check the following
328 * a voice call has ended recently
329 * the remote device is not of type headset
330 * If the above conditions meet, drop the Play/Pause command
331 * This fix is to interop with certain carkits which sends an automatic PLAY or PAUSE
332 * commands right after call ends
333 */
334 if((p_remote_cmd->rc_id == BTA_AV_RC_PLAY || p_remote_cmd->rc_id == BTA_AV_RC_PAUSE)&&
335 (btif_hf_call_terminated_recently() == TRUE) &&
336 (check_cod( (const bt_bdaddr_t*)&(btif_rc_cb.rc_addr), COD_AV_HEADSETS) != TRUE))
337 {
338 BTIF_TRACE_DEBUG2("%s:Dropping the play/Pause command received right after call end cmd:%d",
339 __FUNCTION__,p_remote_cmd->rc_id);
340 return;
341 }
342
343 for (i = 0; key_map[i].name != NULL; i++) {
344 if (p_remote_cmd->rc_id == key_map[i].avrcp) {
345 BTIF_TRACE_DEBUG3("%s: %s %s", __FUNCTION__, key_map[i].name, status);
346
347 /* MusicPlayer uses a long_press_timeout of 1 second for PLAYPAUSE button
348 * and maps that to autoshuffle. So if for some reason release for PLAY/PAUSE
349 * comes 1 second after the press, the MediaPlayer UI goes into a bad state.
350 * The reason for the delay could be sniff mode exit or some AVDTP procedure etc.
351 * The fix is to generate a release right after the press and drown the 'actual'
352 * release.
353 */
354 if ((key_map[i].release_quirk == 1) && (pressed == 0))
355 {
356 BTIF_TRACE_DEBUG2("%s: AVRC %s Release Faked earlier, drowned now",
357 __FUNCTION__, key_map[i].name);
358 return;
359 }
360 #ifdef BTIF_RC_USE_UINPUT
361 send_key(uinput_fd, key_map[i].mapped_id, pressed);
362 #endif
363 if ((key_map[i].release_quirk == 1) && (pressed == 1))
364 {
365 GKI_delay(30); // 30ms
366 BTIF_TRACE_DEBUG2("%s: AVRC %s Release quirk enabled, send release now",
367 __FUNCTION__, key_map[i].name);
368 #ifdef BTIF_RC_USE_UINPUT
369 send_key(uinput_fd, key_map[i].mapped_id, 0);
370 #endif
371 }
372 break;
373 }
374 }
375
376 if (key_map[i].name == NULL)
377 BTIF_TRACE_ERROR3("%s AVRCP: unknown button 0x%02X %s", __FUNCTION__,
378 p_remote_cmd->rc_id, status);
379 }
380
381 /*****************************************************************************
382 **
383 ** Function btif_rc_init
384 **
385 ** Description Initialize RC
386 **
387 ** Returns Returns 0 on success, -1 otherwise
388 **
389 *******************************************************************************/
btif_rc_init()390 int btif_rc_init()
391 {
392 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
393 memset (&btif_rc_cb, 0, sizeof(btif_rc_cb));
394
395 #ifdef BTIF_RC_USE_UINPUT
396 return uinput_driver_check();
397 #endif /* BTIF_RC_USE_UINPUT */
398 }
399
400 /***************************************************************************
401 **
402 ** Function btif_rc_handler
403 **
404 ** Description RC event handler
405 **
406 ***************************************************************************/
btif_rc_handler(tBTA_AV_EVT event,tBTA_AV * p_data)407 void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data)
408 {
409 BTIF_TRACE_DEBUG2 ("%s event:%s", __FUNCTION__, dump_rc_event_name(event));
410 switch (event)
411 {
412 case BTA_AV_RC_OPEN_EVT:
413 {
414 BTIF_TRACE_DEBUG1("Peer_features:%x", p_data->rc_open.peer_features);
415 handle_rc_connect( &(p_data->rc_open) );
416 }break;
417
418 case BTA_AV_RC_CLOSE_EVT:
419 {
420 handle_rc_disconnect( &(p_data->rc_close) );
421 }break;
422
423 case BTA_AV_REMOTE_CMD_EVT:
424 {
425 BTIF_TRACE_DEBUG2("rc_id:0x%x key_state:%d", p_data->remote_cmd.rc_id,
426 p_data->remote_cmd.key_state);
427 handle_rc_passthrough_cmd( (&p_data->remote_cmd) );
428 }break;
429 default:
430 BTIF_TRACE_DEBUG0("Unhandled RC event");
431 }
432 }
433
434 /***************************************************************************
435 **
436 ** Function btif_rc_get_connected_peer
437 **
438 ** Description Fetches the connected headset's BD_ADDR if any
439 **
440 ***************************************************************************/
btif_rc_get_connected_peer(BD_ADDR peer_addr)441 BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr)
442 {
443 if (btif_rc_cb.rc_connected == TRUE) {
444 bdcpy(peer_addr, btif_rc_cb.rc_addr);
445 return TRUE;
446 }
447 return FALSE;
448 }
449
450 /***************************************************************************
451 **
452 ** Function btif_rc_check_handle_pending_play
453 **
454 ** Description Clears the queued PLAY command. if bSend is TRUE, forwards to app
455 **
456 ***************************************************************************/
457
458 /* clear the queued PLAY command. if bSend is TRUE, forward to app */
btif_rc_check_handle_pending_play(BD_ADDR peer_addr,BOOLEAN bSendToApp)459 void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp)
460 {
461 ALOGV("btapp_rc_check_handle_pending_play: bSendToApp=%d", bSendToApp);
462 if (btif_rc_cb.rc_pending_play)
463 {
464 if (bSendToApp)
465 {
466 tBTA_AV_REMOTE_CMD remote_cmd;
467 APPL_TRACE_DEBUG1("%s: Sending queued PLAYED event to app", __FUNCTION__);
468
469 memset (&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD));
470 remote_cmd.rc_handle = btif_rc_cb.rc_handle;
471 remote_cmd.rc_id = AVRC_ID_PLAY;
472 remote_cmd.hdr.ctype = AVRC_CMD_CTRL;
473 remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU;
474
475 /* delay sending to app, else there is a timing issue in the framework,
476 ** which causes the audio to be on th device's speaker. Delay between
477 ** OPEN & RC_PLAYs
478 */
479 GKI_delay (200);
480 /* send to app - both PRESSED & RELEASED */
481 remote_cmd.key_state = AVRC_STATE_PRESS;
482 handle_rc_passthrough_cmd( &remote_cmd );
483
484 GKI_delay (100);
485
486 remote_cmd.key_state = AVRC_STATE_RELEASE;
487 handle_rc_passthrough_cmd( &remote_cmd );
488 }
489 btif_rc_cb.rc_pending_play = FALSE;
490 }
491 }
492
493