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