OSDN Git Service

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