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