Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*****************************************************************************
     18  *
     19  *  Filename:      btif_rc.cc
     20  *
     21  *  Description:   Bluetooth AVRC implementation
     22  *
     23  *****************************************************************************/
     24 
     25 #define LOG_TAG "bt_btif_avrc"
     26 
     27 #include <errno.h>
     28 #include <fcntl.h>
     29 #include <pthread.h>
     30 #include <string.h>
     31 #include <time.h>
     32 #include <unistd.h>
     33 
     34 #include <mutex>
     35 
     36 #include <hardware/bluetooth.h>
     37 #include <hardware/bt_rc.h>
     38 
     39 #include "avrc_defs.h"
     40 #include "bt_common.h"
     41 #include "bta_api.h"
     42 #include "bta_av_api.h"
     43 #include "btif_av.h"
     44 #include "btif_common.h"
     45 #include "btif_util.h"
     46 #include "btu.h"
     47 #include "device/include/interop.h"
     48 #include "osi/include/list.h"
     49 #include "osi/include/osi.h"
     50 #include "osi/include/properties.h"
     51 #define RC_INVALID_TRACK_ID (0xFFFFFFFFFFFFFFFFULL)
     52 
     53 /*****************************************************************************
     54  *  Constants & Macros
     55  *****************************************************************************/
     56 
     57 /* cod value for Headsets */
     58 #define COD_AV_HEADSETS 0x0404
     59 /* for AVRC 1.4 need to change this */
     60 #define MAX_RC_NOTIFICATIONS AVRC_EVT_VOLUME_CHANGE
     61 
     62 #define IDX_GET_PLAY_STATUS_RSP 0
     63 #define IDX_LIST_APP_ATTR_RSP 1
     64 #define IDX_LIST_APP_VALUE_RSP 2
     65 #define IDX_GET_CURR_APP_VAL_RSP 3
     66 #define IDX_SET_APP_VAL_RSP 4
     67 #define IDX_GET_APP_ATTR_TXT_RSP 5
     68 #define IDX_GET_APP_VAL_TXT_RSP 6
     69 #define IDX_GET_ELEMENT_ATTR_RSP 7
     70 #define IDX_SET_ADDR_PLAYER_RSP 8
     71 #define IDX_SET_BROWSED_PLAYER_RSP 9
     72 #define IDX_GET_FOLDER_ITEMS_RSP 10
     73 #define IDX_CHG_PATH_RSP 11
     74 #define IDX_GET_ITEM_ATTR_RSP 12
     75 #define IDX_PLAY_ITEM_RSP 13
     76 #define IDX_GET_TOTAL_NUM_OF_ITEMS_RSP 14
     77 #define IDX_SEARCH_RSP 15
     78 #define IDX_ADD_TO_NOW_PLAYING_RSP 16
     79 
     80 /* Update MAX value whenever IDX will be changed */
     81 #define MAX_CMD_QUEUE_LEN 17
     82 
     83 #define MAX_VOLUME 128
     84 #define MAX_LABEL 16
     85 #define MAX_TRANSACTIONS_PER_SESSION 16
     86 #define PLAY_STATUS_PLAYING 1
     87 #define BTIF_RC_NUM_CONN BT_RC_NUM_APP
     88 
     89 #define CHECK_RC_CONNECTED(p_dev)                                          \
     90   do {                                                                     \
     91     if ((p_dev) == NULL || (p_dev)->rc_connected == false) {               \
     92       BTIF_TRACE_WARNING("%s: called when RC is not connected", __func__); \
     93       return BT_STATUS_NOT_READY;                                          \
     94     }                                                                      \
     95   } while (0)
     96 
     97 #define CHECK_BR_CONNECTED(p_dev)                                          \
     98   do {                                                                     \
     99     if ((p_dev) == NULL || (p_dev)->br_connected == false) {               \
    100       BTIF_TRACE_WARNING("%s: called when BR is not connected", __func__); \
    101       return BT_STATUS_NOT_READY;                                          \
    102     }                                                                      \
    103   } while (0)
    104 
    105 /*****************************************************************************
    106  *  Local type definitions
    107  *****************************************************************************/
    108 typedef struct {
    109   uint8_t bNotify;
    110   uint8_t label;
    111 } btif_rc_reg_notifications_t;
    112 
    113 typedef struct {
    114   uint8_t label;
    115   uint8_t ctype;
    116   bool is_rsp_pending;
    117 } btif_rc_cmd_ctxt_t;
    118 
    119 /* 2 second timeout to get interim response */
    120 #define BTIF_TIMEOUT_RC_INTERIM_RSP_MS (2 * 1000)
    121 #define BTIF_TIMEOUT_RC_STATUS_CMD_MS (2 * 1000)
    122 #define BTIF_TIMEOUT_RC_CONTROL_CMD_MS (2 * 1000)
    123 
    124 typedef enum {
    125   eNOT_REGISTERED,
    126   eREGISTERED,
    127   eINTERIM
    128 } btif_rc_nfn_reg_status_t;
    129 
    130 typedef struct {
    131   uint8_t event_id;
    132   uint8_t label;
    133   btif_rc_nfn_reg_status_t status;
    134 } btif_rc_supported_event_t;
    135 
    136 #define BTIF_RC_STS_TIMEOUT 0xFE
    137 typedef struct {
    138   uint8_t label;
    139   uint8_t pdu_id;
    140 } btif_rc_status_cmd_timer_t;
    141 
    142 typedef struct {
    143   uint8_t label;
    144   uint8_t pdu_id;
    145 } btif_rc_control_cmd_timer_t;
    146 
    147 typedef struct {
    148   union {
    149     btif_rc_status_cmd_timer_t rc_status_cmd;
    150     btif_rc_control_cmd_timer_t rc_control_cmd;
    151   };
    152   RawAddress rc_addr;
    153 } btif_rc_timer_context_t;
    154 
    155 typedef struct {
    156   bool query_started;
    157   uint8_t num_attrs;
    158   uint8_t num_ext_attrs;
    159 
    160   uint8_t attr_index;
    161   uint8_t ext_attr_index;
    162   uint8_t ext_val_index;
    163   btrc_player_app_attr_t attrs[AVRC_MAX_APP_ATTR_SIZE];
    164   btrc_player_app_ext_attr_t ext_attrs[AVRC_MAX_APP_ATTR_SIZE];
    165 } btif_rc_player_app_settings_t;
    166 
    167 /* TODO : Merge btif_rc_reg_notifications_t and btif_rc_cmd_ctxt_t to a single
    168  * struct */
    169 typedef struct {
    170   bool rc_connected;
    171   bool br_connected;  // Browsing channel.
    172   uint8_t rc_handle;
    173   tBTA_AV_FEAT rc_features;
    174   btrc_connection_state_t rc_state;
    175   RawAddress rc_addr;
    176   uint16_t rc_pending_play;
    177   btif_rc_cmd_ctxt_t rc_pdu_info[MAX_CMD_QUEUE_LEN];
    178   btif_rc_reg_notifications_t rc_notif[MAX_RC_NOTIFICATIONS];
    179   unsigned int rc_volume;
    180   uint8_t rc_vol_label;
    181   list_t* rc_supported_event_list;
    182   btif_rc_player_app_settings_t rc_app_settings;
    183   alarm_t* rc_play_status_timer;
    184   bool rc_features_processed;
    185   uint64_t rc_playing_uid;
    186   bool rc_procedure_complete;
    187 } btif_rc_device_cb_t;
    188 
    189 typedef struct {
    190   std::mutex lock;
    191   btif_rc_device_cb_t rc_multi_cb[BTIF_RC_NUM_CONN];
    192 } rc_cb_t;
    193 
    194 typedef struct {
    195   bool in_use;
    196   uint8_t lbl;
    197   uint8_t handle;
    198   btif_rc_timer_context_t txn_timer_context;
    199   alarm_t* txn_timer;
    200 } rc_transaction_t;
    201 
    202 typedef struct {
    203   std::recursive_mutex lbllock;
    204   rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION];
    205 } rc_device_t;
    206 
    207 typedef struct {
    208   uint8_t label;
    209   RawAddress rc_addr;
    210 } rc_context_t;
    211 
    212 typedef struct { uint8_t handle; } btif_rc_handle_t;
    213 
    214 rc_device_t device;
    215 
    216 static void sleep_ms(period_ms_t timeout_ms);
    217 
    218 /* Response status code - Unknown Error - this is changed to "reserved" */
    219 #define BTIF_STS_GEN_ERROR 0x06
    220 
    221 /* Utility table to map hal status codes to bta status codes for the response
    222  * status */
    223 static const uint8_t status_code_map[] = {
    224     /* BTA_Status codes        HAL_Status codes */
    225     AVRC_STS_BAD_CMD,         /* BTRC_STS_BAD_CMD */
    226     AVRC_STS_BAD_PARAM,       /* BTRC_STS_BAD_PARAM */
    227     AVRC_STS_NOT_FOUND,       /* BTRC_STS_NOT_FOUND */
    228     AVRC_STS_INTERNAL_ERR,    /* BTRC_STS_INTERNAL_ERR */
    229     AVRC_STS_NO_ERROR,        /* BTRC_STS_NO_ERROR */
    230     AVRC_STS_UID_CHANGED,     /* BTRC_STS_UID_CHANGED */
    231     BTIF_STS_GEN_ERROR,       /* BTRC_STS_RESERVED */
    232     AVRC_STS_BAD_DIR,         /* BTRC_STS_INV_DIRN */
    233     AVRC_STS_NOT_DIR,         /* BTRC_STS_INV_DIRECTORY */
    234     AVRC_STS_NOT_EXIST,       /* BTRC_STS_INV_ITEM */
    235     AVRC_STS_BAD_SCOPE,       /* BTRC_STS_INV_SCOPE */
    236     AVRC_STS_BAD_RANGE,       /* BTRC_STS_INV_RANGE */
    237     AVRC_STS_UID_IS_DIR,      /* BTRC_STS_DIRECTORY */
    238     AVRC_STS_IN_USE,          /* BTRC_STS_MEDIA_IN_USE */
    239     AVRC_STS_NOW_LIST_FULL,   /* BTRC_STS_PLAY_LIST_FULL */
    240     AVRC_STS_SEARCH_NOT_SUP,  /* BTRC_STS_SRCH_NOT_SPRTD */
    241     AVRC_STS_SEARCH_BUSY,     /* BTRC_STS_SRCH_IN_PROG */
    242     AVRC_STS_BAD_PLAYER_ID,   /* BTRC_STS_INV_PLAYER */
    243     AVRC_STS_PLAYER_N_BR,     /* BTRC_STS_PLAY_NOT_BROW */
    244     AVRC_STS_PLAYER_N_ADDR,   /* BTRC_STS_PLAY_NOT_ADDR */
    245     AVRC_STS_BAD_SEARCH_RES,  /* BTRC_STS_INV_RESULTS */
    246     AVRC_STS_NO_AVAL_PLAYER,  /* BTRC_STS_NO_AVBL_PLAY */
    247     AVRC_STS_ADDR_PLAYER_CHG, /* BTRC_STS_ADDR_PLAY_CHGD */
    248 };
    249 
    250 static void send_reject_response(uint8_t rc_handle, uint8_t label, uint8_t pdu,
    251                                  uint8_t status, uint8_t opcode);
    252 static uint8_t opcode_from_pdu(uint8_t pdu);
    253 static void send_metamsg_rsp(btif_rc_device_cb_t* p_dev, int index,
    254                              uint8_t label, tBTA_AV_CODE code,
    255                              tAVRC_RESPONSE* pmetamsg_resp);
    256 static void register_volumechange(uint8_t label, btif_rc_device_cb_t* p_dev);
    257 static void lbl_init();
    258 static void init_all_transactions();
    259 static bt_status_t get_transaction(rc_transaction_t** ptransaction);
    260 static void release_transaction(uint8_t label);
    261 static rc_transaction_t* get_transaction_by_lbl(uint8_t label);
    262 static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg,
    263                                   btif_rc_device_cb_t* p_dev);
    264 
    265 static void handle_avk_rc_metamsg_cmd(tBTA_AV_META_MSG* pmeta_msg);
    266 static void handle_avk_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg);
    267 static void btif_rc_ctrl_upstreams_rsp_cmd(uint8_t event,
    268                                            tAVRC_COMMAND* pavrc_cmd,
    269                                            uint8_t label,
    270                                            btif_rc_device_cb_t* p_dev);
    271 static void rc_ctrl_procedure_complete(btif_rc_device_cb_t* p_dev);
    272 static void rc_stop_play_status_timer(btif_rc_device_cb_t* p_dev);
    273 static void register_for_event_notification(btif_rc_supported_event_t* p_event,
    274                                             btif_rc_device_cb_t* p_dev);
    275 static void handle_get_capability_response(tBTA_AV_META_MSG* pmeta_msg,
    276                                            tAVRC_GET_CAPS_RSP* p_rsp);
    277 static void handle_app_attr_response(tBTA_AV_META_MSG* pmeta_msg,
    278                                      tAVRC_LIST_APP_ATTR_RSP* p_rsp);
    279 static void handle_app_val_response(tBTA_AV_META_MSG* pmeta_msg,
    280                                     tAVRC_LIST_APP_VALUES_RSP* p_rsp);
    281 static void handle_app_cur_val_response(tBTA_AV_META_MSG* pmeta_msg,
    282                                         tAVRC_GET_CUR_APP_VALUE_RSP* p_rsp);
    283 static void handle_app_attr_txt_response(tBTA_AV_META_MSG* pmeta_msg,
    284                                          tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp);
    285 static void handle_app_attr_val_txt_response(tBTA_AV_META_MSG* pmeta_msg,
    286                                              tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp);
    287 static void handle_get_playstatus_response(tBTA_AV_META_MSG* pmeta_msg,
    288                                            tAVRC_GET_PLAY_STATUS_RSP* p_rsp);
    289 static void handle_set_addressed_player_response(tBTA_AV_META_MSG* pmeta_msg,
    290                                                  tAVRC_RSP* p_rsp);
    291 static void handle_get_elem_attr_response(tBTA_AV_META_MSG* pmeta_msg,
    292                                           tAVRC_GET_ATTRS_RSP* p_rsp);
    293 static void handle_set_app_attr_val_response(tBTA_AV_META_MSG* pmeta_msg,
    294                                              tAVRC_RSP* p_rsp);
    295 static bt_status_t get_play_status_cmd(btif_rc_device_cb_t* p_dev);
    296 static bt_status_t get_player_app_setting_attr_text_cmd(
    297     uint8_t* attrs, uint8_t num_attrs, btif_rc_device_cb_t* p_dev);
    298 static bt_status_t get_player_app_setting_value_text_cmd(
    299     uint8_t* vals, uint8_t num_vals, btif_rc_device_cb_t* p_dev);
    300 static bt_status_t register_notification_cmd(uint8_t label, uint8_t event_id,
    301                                              uint32_t event_value,
    302                                              btif_rc_device_cb_t* p_dev);
    303 static bt_status_t get_element_attribute_cmd(uint8_t num_attribute,
    304                                              uint32_t* p_attr_ids,
    305                                              btif_rc_device_cb_t* p_dev);
    306 static bt_status_t getcapabilities_cmd(uint8_t cap_id,
    307                                        btif_rc_device_cb_t* p_dev);
    308 static bt_status_t list_player_app_setting_attrib_cmd(
    309     btif_rc_device_cb_t* p_dev);
    310 static bt_status_t list_player_app_setting_value_cmd(
    311     uint8_t attrib_id, btif_rc_device_cb_t* p_dev);
    312 static bt_status_t get_player_app_setting_cmd(uint8_t num_attrib,
    313                                               uint8_t* attrib_ids,
    314                                               btif_rc_device_cb_t* p_dev);
    315 void get_folder_item_type_media(const tAVRC_ITEM* avrc_item,
    316                                 btrc_folder_items_t* btrc_item);
    317 void get_folder_item_type_folder(const tAVRC_ITEM* avrc_item,
    318                                  btrc_folder_items_t* btrc_item);
    319 void get_folder_item_type_player(const tAVRC_ITEM* avrc_item,
    320                                  btrc_folder_items_t* btrc_item);
    321 static bt_status_t get_folder_items_cmd(RawAddress* bd_addr, uint8_t scope,
    322                                         uint8_t start_item, uint8_t num_items);
    323 
    324 static void btif_rc_upstreams_evt(uint16_t event, tAVRC_COMMAND* p_param,
    325                                   uint8_t ctype, uint8_t label,
    326                                   btif_rc_device_cb_t* p_dev);
    327 
    328 static void btif_rc_upstreams_rsp_evt(uint16_t event,
    329                                       tAVRC_RESPONSE* pavrc_resp, uint8_t ctype,
    330                                       uint8_t label,
    331                                       btif_rc_device_cb_t* p_dev);
    332 
    333 static void rc_start_play_status_timer(btif_rc_device_cb_t* p_dev);
    334 static bool absolute_volume_disabled(void);
    335 
    336 /*****************************************************************************
    337  *  Static variables
    338  *****************************************************************************/
    339 static rc_cb_t btif_rc_cb;
    340 static btrc_callbacks_t* bt_rc_callbacks = NULL;
    341 static btrc_ctrl_callbacks_t* bt_rc_ctrl_callbacks = NULL;
    342 
    343 /*****************************************************************************
    344  *  Static functions
    345  *****************************************************************************/
    346 
    347 /*****************************************************************************
    348  *  Externs
    349  *****************************************************************************/
    350 extern bool btif_hf_call_terminated_recently();
    351 extern bool check_cod(const RawAddress* remote_bdaddr, uint32_t cod);
    352 
    353 /*****************************************************************************
    354  *  Functions
    355  *****************************************************************************/
    356 static btif_rc_device_cb_t* alloc_device() {
    357   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
    358     if (btif_rc_cb.rc_multi_cb[idx].rc_state ==
    359         BTRC_CONNECTION_STATE_DISCONNECTED) {
    360       return (&btif_rc_cb.rc_multi_cb[idx]);
    361     }
    362   }
    363   return NULL;
    364 }
    365 
    366 static btif_rc_device_cb_t* get_connected_device(int index) {
    367   BTIF_TRACE_DEBUG("%s: index: %d", __func__, index);
    368   if (index > BTIF_RC_NUM_CONN) {
    369     BTIF_TRACE_ERROR("%s: can't support more than %d connections", __func__,
    370                      BTIF_RC_NUM_CONN);
    371     return NULL;
    372   }
    373   if (btif_rc_cb.rc_multi_cb[index].rc_state !=
    374       BTRC_CONNECTION_STATE_CONNECTED) {
    375     BTIF_TRACE_ERROR("%s: returning NULL", __func__);
    376     return NULL;
    377   }
    378   return (&btif_rc_cb.rc_multi_cb[index]);
    379 }
    380 
    381 static int get_num_connected_devices() {
    382   int connected_devices = 0;
    383   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
    384     if (btif_rc_cb.rc_multi_cb[idx].rc_state ==
    385         BTRC_CONNECTION_STATE_CONNECTED) {
    386       connected_devices++;
    387     }
    388   }
    389   BTIF_TRACE_DEBUG("%s: returning connected_devices: %d", __func__,
    390                    connected_devices);
    391   return connected_devices;
    392 }
    393 
    394 btif_rc_device_cb_t* btif_rc_get_device_by_bda(const RawAddress* bd_addr) {
    395   VLOG(1) << __func__ << ": bd_addr: " << *bd_addr;
    396 
    397   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
    398     if ((btif_rc_cb.rc_multi_cb[idx].rc_state !=
    399          BTRC_CONNECTION_STATE_DISCONNECTED) &&
    400         btif_rc_cb.rc_multi_cb[idx].rc_addr == *bd_addr) {
    401       return (&btif_rc_cb.rc_multi_cb[idx]);
    402     }
    403   }
    404   BTIF_TRACE_ERROR("%s: device not found, returning NULL!", __func__);
    405   return NULL;
    406 }
    407 
    408 btif_rc_device_cb_t* btif_rc_get_device_by_handle(uint8_t handle) {
    409   BTIF_TRACE_DEBUG("%s: handle: 0x%x", __func__, handle);
    410   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
    411     if ((btif_rc_cb.rc_multi_cb[idx].rc_state !=
    412          BTRC_CONNECTION_STATE_DISCONNECTED) &&
    413         (btif_rc_cb.rc_multi_cb[idx].rc_handle == handle)) {
    414       BTIF_TRACE_DEBUG("%s: btif_rc_cb.rc_multi_cb[idx].rc_handle: 0x%x",
    415                        __func__, btif_rc_cb.rc_multi_cb[idx].rc_handle);
    416       return (&btif_rc_cb.rc_multi_cb[idx]);
    417     }
    418   }
    419   BTIF_TRACE_ERROR("%s: returning NULL", __func__);
    420   return NULL;
    421 }
    422 
    423 void fill_pdu_queue(int index, uint8_t ctype, uint8_t label, bool pending,
    424                     btif_rc_device_cb_t* p_dev) {
    425   p_dev->rc_pdu_info[index].ctype = ctype;
    426   p_dev->rc_pdu_info[index].label = label;
    427   p_dev->rc_pdu_info[index].is_rsp_pending = pending;
    428 }
    429 
    430 void fill_avrc_attr_entry(tAVRC_ATTR_ENTRY* attr_vals, int num_attrs,
    431                           btrc_element_attr_val_t* p_attrs) {
    432   for (int attr_cnt = 0; attr_cnt < num_attrs; attr_cnt++) {
    433     attr_vals[attr_cnt].attr_id = p_attrs[attr_cnt].attr_id;
    434     attr_vals[attr_cnt].name.charset_id = AVRC_CHARSET_ID_UTF8;
    435     attr_vals[attr_cnt].name.str_len =
    436         (uint16_t)strlen((char*)p_attrs[attr_cnt].text);
    437     attr_vals[attr_cnt].name.p_str = p_attrs[attr_cnt].text;
    438     BTIF_TRACE_DEBUG(
    439         "%s: attr_id: 0x%x, charset_id: 0x%x, str_len: %d, str: %s", __func__,
    440         (unsigned int)attr_vals[attr_cnt].attr_id,
    441         attr_vals[attr_cnt].name.charset_id, attr_vals[attr_cnt].name.str_len,
    442         attr_vals[attr_cnt].name.p_str);
    443   }
    444 }
    445 
    446 void rc_cleanup_sent_cmd(void* p_data) { BTIF_TRACE_DEBUG("%s: ", __func__); }
    447 
    448 void handle_rc_ctrl_features(btif_rc_device_cb_t* p_dev) {
    449   if (!(p_dev->rc_features & BTA_AV_FEAT_RCTG) &&
    450       (!(p_dev->rc_features & BTA_AV_FEAT_RCCT) ||
    451        !(p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL))) {
    452     return;
    453   }
    454 
    455   int rc_features = 0;
    456 
    457   if ((p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL) &&
    458       (p_dev->rc_features & BTA_AV_FEAT_RCCT)) {
    459     rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME;
    460   }
    461 
    462   if ((p_dev->rc_features & BTA_AV_FEAT_METADATA) &&
    463       (p_dev->rc_features & BTA_AV_FEAT_VENDOR) &&
    464       (p_dev->rc_features_processed != true)) {
    465     rc_features |= BTRC_FEAT_METADATA;
    466 
    467     /* Mark rc features processed to avoid repeating
    468      * the AVRCP procedure every time on receiving this
    469      * update.
    470      */
    471     p_dev->rc_features_processed = true;
    472     if (btif_av_is_sink_enabled()) {
    473       getcapabilities_cmd(AVRC_CAP_COMPANY_ID, p_dev);
    474     }
    475   }
    476 
    477   /* Add browsing feature capability */
    478   if (p_dev->rc_features & BTA_AV_FEAT_BROWSE) {
    479     rc_features |= BTRC_FEAT_BROWSE;
    480   }
    481 
    482   BTIF_TRACE_DEBUG("%s: Update rc features to CTRL: %d", __func__, rc_features);
    483   RawAddress rc_addr = p_dev->rc_addr;
    484   HAL_CBACK(bt_rc_ctrl_callbacks, getrcfeatures_cb, &rc_addr, rc_features);
    485 }
    486 
    487 void handle_rc_features(btif_rc_device_cb_t* p_dev) {
    488   RawAddress rc_addr = p_dev->rc_addr;
    489 
    490   CHECK(bt_rc_callbacks);
    491 
    492   btrc_remote_features_t rc_features = BTRC_FEAT_NONE;
    493   RawAddress avdtp_addr = btif_av_get_addr();
    494 
    495   BTIF_TRACE_DEBUG("%s: AVDTP Address: %s AVCTP address: %s", __func__,
    496                    avdtp_addr.ToString().c_str(), rc_addr.ToString().c_str());
    497 
    498   if (interop_match_addr(INTEROP_DISABLE_ABSOLUTE_VOLUME, &rc_addr) ||
    499       absolute_volume_disabled() || avdtp_addr != rc_addr) {
    500     p_dev->rc_features &= ~BTA_AV_FEAT_ADV_CTRL;
    501   }
    502 
    503   if (p_dev->rc_features & BTA_AV_FEAT_BROWSE) {
    504     rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_BROWSE);
    505   }
    506 
    507 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
    508   if ((p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL) &&
    509       (p_dev->rc_features & BTA_AV_FEAT_RCTG)) {
    510     rc_features =
    511         (btrc_remote_features_t)(rc_features | BTRC_FEAT_ABSOLUTE_VOLUME);
    512   }
    513 #endif
    514 
    515   if (p_dev->rc_features & BTA_AV_FEAT_METADATA) {
    516     rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_METADATA);
    517   }
    518 
    519   BTIF_TRACE_DEBUG("%s: rc_features: 0x%x", __func__, rc_features);
    520   HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features);
    521 
    522 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
    523   BTIF_TRACE_DEBUG(
    524       "%s: Checking for feature flags in btif_rc_handler with label: %d",
    525       __func__, p_dev->rc_vol_label);
    526   // Register for volume change on connect
    527   if (p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL &&
    528       p_dev->rc_features & BTA_AV_FEAT_RCTG) {
    529     rc_transaction_t* p_transaction = NULL;
    530     bt_status_t status = BT_STATUS_NOT_READY;
    531     if (MAX_LABEL == p_dev->rc_vol_label) {
    532       status = get_transaction(&p_transaction);
    533     } else {
    534       p_transaction = get_transaction_by_lbl(p_dev->rc_vol_label);
    535       if (NULL != p_transaction) {
    536         BTIF_TRACE_DEBUG(
    537             "%s: register_volumechange already in progress for label: %d",
    538             __func__, p_dev->rc_vol_label);
    539         return;
    540       }
    541       status = get_transaction(&p_transaction);
    542     }
    543     if (BT_STATUS_SUCCESS == status && NULL != p_transaction) {
    544       p_dev->rc_vol_label = p_transaction->lbl;
    545       register_volumechange(p_dev->rc_vol_label, p_dev);
    546     }
    547   }
    548 #endif
    549 }
    550 
    551 /***************************************************************************
    552  *  Function       handle_rc_connect
    553  *
    554  *  - Argument:    tBTA_AV_RC_OPEN  browse RC open data structure
    555  *
    556  *  - Description: browse RC connection event handler
    557  *
    558  ***************************************************************************/
    559 void handle_rc_browse_connect(tBTA_AV_RC_BROWSE_OPEN* p_rc_br_open) {
    560   BTIF_TRACE_DEBUG("%s: rc_handle %d status %d", __func__,
    561                    p_rc_br_open->rc_handle, p_rc_br_open->status);
    562   btif_rc_device_cb_t* p_dev =
    563       btif_rc_get_device_by_handle(p_rc_br_open->rc_handle);
    564 
    565   if (!p_dev) {
    566     BTIF_TRACE_ERROR("%s p_dev is null", __func__);
    567     return;
    568   }
    569 
    570   /* check that we are already connected to this address since being connected
    571    * to a browse when not connected to the control channel over AVRCP is
    572    * probably not preferred anyways. */
    573   if (p_rc_br_open->status == BTA_AV_SUCCESS) {
    574     RawAddress rc_addr = p_dev->rc_addr;
    575     p_dev->br_connected = true;
    576     HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, true, true, &rc_addr);
    577   }
    578 }
    579 
    580 /***************************************************************************
    581  *  Function       handle_rc_connect
    582  *
    583  *  - Argument:    tBTA_AV_RC_OPEN  RC open data structure
    584  *
    585  *  - Description: RC connection event handler
    586  *
    587  ***************************************************************************/
    588 void handle_rc_connect(tBTA_AV_RC_OPEN* p_rc_open) {
    589   BTIF_TRACE_DEBUG("%s: rc_handle: %d", __func__, p_rc_open->rc_handle);
    590 
    591   btif_rc_device_cb_t* p_dev = alloc_device();
    592   if (p_dev == NULL) {
    593     BTIF_TRACE_ERROR("%s: p_dev is NULL", __func__);
    594     return;
    595   }
    596 
    597   if (!(p_rc_open->status == BTA_AV_SUCCESS)) {
    598     BTIF_TRACE_ERROR("%s: Connect failed with error code: %d", __func__,
    599                      p_rc_open->status);
    600     p_dev->rc_connected = false;
    601   }
    602 
    603   // check if already some RC is connected
    604   if (p_dev->rc_connected) {
    605     BTIF_TRACE_ERROR(
    606         "%s: Got RC OPEN in connected state, Connected RC: %d \
    607             and Current RC: %d",
    608         __func__, p_dev->rc_handle, p_rc_open->rc_handle);
    609     if (p_dev->rc_handle != p_rc_open->rc_handle &&
    610         p_dev->rc_addr != p_rc_open->peer_addr) {
    611       BTIF_TRACE_DEBUG("%s: Got RC connected for some other handle", __func__);
    612       BTA_AvCloseRc(p_rc_open->rc_handle);
    613       return;
    614     }
    615   }
    616   p_dev->rc_addr = p_rc_open->peer_addr;
    617   p_dev->rc_features = p_rc_open->peer_features;
    618   BTIF_TRACE_DEBUG("%s: handle_rc_connect in features: 0x%x out features 0x%x",
    619                    __func__, p_rc_open->peer_features, p_dev->rc_features);
    620   p_dev->rc_vol_label = MAX_LABEL;
    621   p_dev->rc_volume = MAX_VOLUME;
    622 
    623   p_dev->rc_connected = true;
    624   p_dev->rc_handle = p_rc_open->rc_handle;
    625   p_dev->rc_state = BTRC_CONNECTION_STATE_CONNECTED;
    626   /* on locally initiated connection we will get remote features as part of
    627    * connect */
    628   if (p_dev->rc_features != 0 && bt_rc_callbacks != NULL) {
    629     handle_rc_features(p_dev);
    630   }
    631 
    632   p_dev->rc_playing_uid = RC_INVALID_TRACK_ID;
    633   if (bt_rc_ctrl_callbacks != NULL) {
    634     RawAddress rc_addr = p_dev->rc_addr;
    635     HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, true, false, &rc_addr);
    636   }
    637   /* report connection state if remote device is AVRCP target */
    638   handle_rc_ctrl_features(p_dev);
    639 }
    640 
    641 /***************************************************************************
    642  *  Function       handle_rc_disconnect
    643  *
    644  *  - Argument:    tBTA_AV_RC_CLOSE     RC close data structure
    645  *
    646  *  - Description: RC disconnection event handler
    647  *
    648  ***************************************************************************/
    649 void handle_rc_disconnect(tBTA_AV_RC_CLOSE* p_rc_close) {
    650   btif_rc_device_cb_t* p_dev = NULL;
    651   BTIF_TRACE_DEBUG("%s: rc_handle: %d", __func__, p_rc_close->rc_handle);
    652 
    653   p_dev = btif_rc_get_device_by_handle(p_rc_close->rc_handle);
    654   if (p_dev == NULL) {
    655     BTIF_TRACE_ERROR("%s: Got disconnect from invalid rc handle", __func__);
    656     return;
    657   }
    658 
    659   if (p_rc_close->rc_handle != p_dev->rc_handle &&
    660       p_dev->rc_addr != p_rc_close->peer_addr) {
    661     BTIF_TRACE_ERROR("Got disconnect of unknown device");
    662     return;
    663   }
    664   RawAddress rc_addr = p_dev->rc_addr;
    665   /* Clean up AVRCP procedure flags */
    666   memset(&p_dev->rc_app_settings, 0, sizeof(btif_rc_player_app_settings_t));
    667   p_dev->rc_features_processed = false;
    668   p_dev->rc_procedure_complete = false;
    669   rc_stop_play_status_timer(p_dev);
    670   /* Check and clear the notification event list */
    671   if (p_dev->rc_supported_event_list != NULL) {
    672     list_clear(p_dev->rc_supported_event_list);
    673     p_dev->rc_supported_event_list = NULL;
    674   }
    675 
    676   /* check if there is another device connected */
    677   if (p_dev->rc_state == BTRC_CONNECTION_STATE_CONNECTED) {
    678     p_dev->rc_handle = 0;
    679     p_dev->rc_connected = false;
    680     p_dev->rc_state = BTRC_CONNECTION_STATE_DISCONNECTED;
    681 
    682     memset(p_dev->rc_notif, 0, sizeof(p_dev->rc_notif));
    683 
    684     p_dev->rc_features = 0;
    685     p_dev->rc_vol_label = MAX_LABEL;
    686     p_dev->rc_volume = MAX_VOLUME;
    687 
    688     p_dev->rc_addr = RawAddress::kEmpty;
    689   }
    690   if (get_num_connected_devices() == 0) {
    691     BTIF_TRACE_DEBUG("%s: Closing all handles", __func__);
    692     init_all_transactions();
    693   }
    694 
    695   p_dev->rc_addr = RawAddress::kEmpty;
    696   /* report connection state if device is AVRCP target */
    697   if (bt_rc_ctrl_callbacks != NULL) {
    698     HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, false, false,
    699               &rc_addr);
    700   }
    701 }
    702 
    703 /***************************************************************************
    704  *  Function       handle_rc_passthrough_cmd
    705  *
    706  *  - Argument:    tBTA_AV_RC rc_id   remote control command ID
    707  *                 tBTA_AV_STATE key_state status of key press
    708  *
    709  *  - Description: Remote control command handler
    710  *
    711  ***************************************************************************/
    712 void handle_rc_passthrough_cmd(tBTA_AV_REMOTE_CMD* p_remote_cmd) {
    713   if (p_remote_cmd == NULL) {
    714     BTIF_TRACE_ERROR("%s: No remote command!", __func__);
    715     return;
    716   }
    717 
    718   btif_rc_device_cb_t* p_dev =
    719       btif_rc_get_device_by_handle(p_remote_cmd->rc_handle);
    720   if (p_dev == NULL) {
    721     BTIF_TRACE_ERROR("%s: Got passthrough command from invalid rc handle",
    722                      __func__);
    723     return;
    724   }
    725 
    726   RawAddress rc_addr = p_dev->rc_addr;
    727 
    728   BTIF_TRACE_DEBUG("%s: p_remote_cmd->rc_id: %d", __func__,
    729                    p_remote_cmd->rc_id);
    730 
    731   /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up
    732    * this PLAY */
    733   if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected())) {
    734     if (p_remote_cmd->key_state == AVRC_STATE_PRESS) {
    735       APPL_TRACE_WARNING("%s: AVDT not open, queuing the PLAY command",
    736                          __func__);
    737       p_dev->rc_pending_play = true;
    738     }
    739     return;
    740   }
    741 
    742   /* If we previously queued a play and we get a PAUSE, clear it. */
    743   if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (p_dev->rc_pending_play)) {
    744     APPL_TRACE_WARNING("%s: Clear the pending PLAY on PAUSE received",
    745                        __func__);
    746     p_dev->rc_pending_play = false;
    747     return;
    748   }
    749 
    750   if ((p_remote_cmd->rc_id == BTA_AV_RC_STOP) &&
    751       (!btif_av_stream_started_ready())) {
    752     APPL_TRACE_WARNING("%s: Stream suspended, ignore STOP cmd", __func__);
    753     return;
    754   }
    755 
    756   int pressed = (p_remote_cmd->key_state == AVRC_STATE_PRESS) ? 1 : 0;
    757 
    758   /* pass all commands up */
    759   BTIF_TRACE_DEBUG("%s: rc_features: %d, cmd->rc_id: %d, pressed: %d", __func__,
    760                    p_dev->rc_features, p_remote_cmd->rc_id, pressed);
    761   HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed,
    762             &rc_addr);
    763 }
    764 
    765 /***************************************************************************
    766  *  Function       handle_rc_passthrough_rsp
    767  *
    768  *  - Argument:    tBTA_AV_REMOTE_RSP passthrough command response
    769  *
    770  *  - Description: Remote control passthrough response handler
    771  *
    772  ***************************************************************************/
    773 void handle_rc_passthrough_rsp(tBTA_AV_REMOTE_RSP* p_remote_rsp) {
    774   btif_rc_device_cb_t* p_dev = NULL;
    775 
    776   p_dev = btif_rc_get_device_by_handle(p_remote_rsp->rc_handle);
    777   if (p_dev == NULL) {
    778     BTIF_TRACE_ERROR("%s: passthrough response for Invalid rc handle",
    779                      __func__);
    780     return;
    781   }
    782 
    783   RawAddress rc_addr = p_dev->rc_addr;
    784 
    785   if (!(p_dev->rc_features & BTA_AV_FEAT_RCTG)) {
    786     BTIF_TRACE_ERROR("%s: DUT does not support AVRCP controller role",
    787                      __func__);
    788     return;
    789   }
    790 
    791   const char* status = (p_remote_rsp->key_state == 1) ? "released" : "pressed";
    792   BTIF_TRACE_DEBUG("%s: rc_id: %d state: %s", __func__, p_remote_rsp->rc_id,
    793                    status);
    794 
    795   release_transaction(p_remote_rsp->label);
    796   if (bt_rc_ctrl_callbacks != NULL) {
    797     HAL_CBACK(bt_rc_ctrl_callbacks, passthrough_rsp_cb, &rc_addr,
    798               p_remote_rsp->rc_id, p_remote_rsp->key_state);
    799   }
    800 }
    801 
    802 /***************************************************************************
    803  *  Function       handle_rc_vendorunique_rsp
    804  *
    805  *  - Argument:    tBTA_AV_REMOTE_RSP  command response
    806  *
    807  *  - Description: Remote control vendor unique response handler
    808  *
    809  ***************************************************************************/
    810 void handle_rc_vendorunique_rsp(tBTA_AV_REMOTE_RSP* p_remote_rsp) {
    811   btif_rc_device_cb_t* p_dev = NULL;
    812   const char* status;
    813   uint8_t vendor_id = 0;
    814 
    815   p_dev = btif_rc_get_device_by_handle(p_remote_rsp->rc_handle);
    816   if (p_dev == NULL) {
    817     BTIF_TRACE_ERROR("%s: Got vendorunique rsp from invalid rc handle",
    818                      __func__);
    819     return;
    820   }
    821 
    822   if (p_dev->rc_features & BTA_AV_FEAT_RCTG) {
    823     int key_state;
    824     if (p_remote_rsp->key_state == AVRC_STATE_RELEASE) {
    825       status = "released";
    826       key_state = 1;
    827     } else {
    828       status = "pressed";
    829       key_state = 0;
    830     }
    831 
    832     if (p_remote_rsp->len > 0) {
    833       if (p_remote_rsp->len >= AVRC_PASS_THRU_GROUP_LEN)
    834         vendor_id = p_remote_rsp->p_data[AVRC_PASS_THRU_GROUP_LEN - 1];
    835       osi_free_and_reset((void**)&p_remote_rsp->p_data);
    836     }
    837     BTIF_TRACE_DEBUG("%s: vendor_id: %d status: %s", __func__, vendor_id,
    838                      status);
    839 
    840     release_transaction(p_remote_rsp->label);
    841     HAL_CBACK(bt_rc_ctrl_callbacks, groupnavigation_rsp_cb, vendor_id,
    842               key_state);
    843   } else {
    844     BTIF_TRACE_ERROR("%s: Remote does not support AVRCP TG role", __func__);
    845   }
    846 }
    847 
    848 /***************************************************************************
    849  *  Function       handle_rc_metamsg_cmd
    850  *
    851  *  - Argument:    tBTA_AV_VENDOR Structure containing the received
    852  *                          metamsg command
    853  *
    854  *  - Description: Remote control metamsg command handler (AVRCP 1.3)
    855  *
    856  ***************************************************************************/
    857 void handle_rc_metamsg_cmd(tBTA_AV_META_MSG* pmeta_msg) {
    858   /* Parse the metamsg command and pass it on to BTL-IFS */
    859   uint8_t scratch_buf[512] = {0};
    860   tAVRC_COMMAND avrc_command = {0};
    861   tAVRC_STS status;
    862   btif_rc_device_cb_t* p_dev = NULL;
    863 
    864   if (NULL == pmeta_msg) {
    865     BTIF_TRACE_EVENT("%s: Exiting as pmeta_msg is NULL", __func__);
    866     return;
    867   }
    868 
    869   if (NULL == pmeta_msg->p_msg) {
    870     BTIF_TRACE_EVENT("%s: Exiting as pmeta_msg->p_msg is NULL", __func__);
    871     return;
    872   }
    873 
    874   BTIF_TRACE_EVENT("%s: pmeta_msg: opcode: %x, code: %x", __func__,
    875                    pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code);
    876 
    877   p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
    878   if (p_dev == NULL) {
    879     BTIF_TRACE_ERROR("%s: Meta msg event for Invalid rc handle", __func__);
    880     return;
    881   }
    882 
    883   if (pmeta_msg->p_msg->hdr.opcode != AVRC_OP_VENDOR &&
    884       pmeta_msg->p_msg->hdr.opcode != AVRC_OP_BROWSE) {
    885     BTIF_TRACE_WARNING("Invalid opcode: %x", pmeta_msg->p_msg->hdr.opcode);
    886     return;
    887   }
    888 
    889   if (pmeta_msg->len < 3) {
    890     BTIF_TRACE_WARNING("%s: Invalid length. opcode: 0x%x, len: 0x%x", __func__,
    891                        pmeta_msg->p_msg->hdr.opcode, pmeta_msg->len);
    892     return;
    893   }
    894 
    895   if (pmeta_msg->code >= AVRC_RSP_NOT_IMPL) {
    896     {
    897       rc_transaction_t* transaction = NULL;
    898       transaction = get_transaction_by_lbl(pmeta_msg->label);
    899       if (transaction != NULL) {
    900         handle_rc_metamsg_rsp(pmeta_msg, p_dev);
    901       } else {
    902         BTIF_TRACE_DEBUG(
    903             "%s: Discard vendor dependent rsp. code: %d label: %d.", __func__,
    904             pmeta_msg->code, pmeta_msg->label);
    905       }
    906       return;
    907     }
    908   }
    909 
    910   status = AVRC_ParsCommand(pmeta_msg->p_msg, &avrc_command, scratch_buf,
    911                             sizeof(scratch_buf));
    912   BTIF_TRACE_DEBUG("%s: Received vendor command.code,PDU and label: %d, %d, %d",
    913                    __func__, pmeta_msg->code, avrc_command.cmd.pdu,
    914                    pmeta_msg->label);
    915 
    916   if (status != AVRC_STS_NO_ERROR) {
    917     /* return error */
    918     BTIF_TRACE_WARNING(
    919         "%s: Error in parsing received metamsg command. status: 0x%02x",
    920         __func__, status);
    921     send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label,
    922                          avrc_command.pdu, status,
    923                          pmeta_msg->p_msg->hdr.opcode);
    924   } else {
    925     /* if RegisterNotification, add it to our registered queue */
    926 
    927     if (avrc_command.cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) {
    928       uint8_t event_id = avrc_command.reg_notif.event_id;
    929 
    930       BTIF_TRACE_EVENT(
    931           "%s: New register notification received.event_id: %s, label: 0x%x, "
    932           "code: %x",
    933           __func__, dump_rc_notification_event_id(event_id), pmeta_msg->label,
    934           pmeta_msg->code);
    935       p_dev->rc_notif[event_id - 1].bNotify = true;
    936       p_dev->rc_notif[event_id - 1].label = pmeta_msg->label;
    937     }
    938 
    939     BTIF_TRACE_EVENT("%s: Passing received metamsg command to app. pdu: %s",
    940                      __func__, dump_rc_pdu(avrc_command.cmd.pdu));
    941 
    942     /* Since handle_rc_metamsg_cmd() itself is called from
    943         *btif context, no context switching is required. Invoke
    944         * btif_rc_upstreams_evt directly from here. */
    945     btif_rc_upstreams_evt((uint16_t)avrc_command.cmd.pdu, &avrc_command,
    946                           pmeta_msg->code, pmeta_msg->label, p_dev);
    947   }
    948 }
    949 
    950 /***************************************************************************
    951  **
    952  ** Function       btif_rc_handler
    953  **
    954  ** Description    RC event handler
    955  **
    956  ***************************************************************************/
    957 void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV* p_data) {
    958   BTIF_TRACE_DEBUG("%s: event: %s", __func__, dump_rc_event(event));
    959   btif_rc_device_cb_t* p_dev = NULL;
    960   switch (event) {
    961     case BTA_AV_RC_OPEN_EVT: {
    962       BTIF_TRACE_DEBUG("%s: Peer_features: %x", __func__,
    963                        p_data->rc_open.peer_features);
    964       handle_rc_connect(&(p_data->rc_open));
    965     } break;
    966 
    967     case BTA_AV_RC_BROWSE_OPEN_EVT: {
    968       /* tell the UL that we have connection to browse channel and that
    969        * browse commands can be directed accordingly. */
    970       handle_rc_browse_connect(&p_data->rc_browse_open);
    971     } break;
    972 
    973     case BTA_AV_RC_CLOSE_EVT: {
    974       handle_rc_disconnect(&(p_data->rc_close));
    975     } break;
    976 
    977     case BTA_AV_RC_BROWSE_CLOSE_EVT: {
    978       BTIF_TRACE_DEBUG("%s: BTA_AV_RC_BROWSE_CLOSE_EVT", __func__);
    979     } break;
    980 
    981     case BTA_AV_REMOTE_CMD_EVT: {
    982       if (bt_rc_callbacks != NULL) {
    983         BTIF_TRACE_DEBUG("%s: rc_id: 0x%x key_state: %d", __func__,
    984                          p_data->remote_cmd.rc_id,
    985                          p_data->remote_cmd.key_state);
    986         handle_rc_passthrough_cmd((&p_data->remote_cmd));
    987       } else {
    988         BTIF_TRACE_ERROR("%s: AVRCP TG role not up, drop passthrough commands",
    989                          __func__);
    990       }
    991     } break;
    992 
    993     case BTA_AV_REMOTE_RSP_EVT: {
    994       BTIF_TRACE_DEBUG("%s: RSP: rc_id: 0x%x key_state: %d", __func__,
    995                        p_data->remote_rsp.rc_id, p_data->remote_rsp.key_state);
    996 
    997       if (p_data->remote_rsp.rc_id == AVRC_ID_VENDOR) {
    998         handle_rc_vendorunique_rsp((&p_data->remote_rsp));
    999       } else {
   1000         handle_rc_passthrough_rsp((&p_data->remote_rsp));
   1001       }
   1002     } break;
   1003 
   1004     case BTA_AV_RC_FEAT_EVT: {
   1005       BTIF_TRACE_DEBUG("%s: Peer_features: %x", __func__,
   1006                        p_data->rc_feat.peer_features);
   1007       p_dev = btif_rc_get_device_by_handle(p_data->rc_feat.rc_handle);
   1008       if (p_dev == NULL) {
   1009         BTIF_TRACE_ERROR("%s: RC Feature event for Invalid rc handle",
   1010                          __func__);
   1011         break;
   1012       }
   1013 
   1014       p_dev->rc_features = p_data->rc_feat.peer_features;
   1015       if (bt_rc_callbacks != NULL) {
   1016         handle_rc_features(p_dev);
   1017       }
   1018 
   1019       if ((p_dev->rc_connected) && (bt_rc_ctrl_callbacks != NULL)) {
   1020         handle_rc_ctrl_features(p_dev);
   1021       }
   1022     } break;
   1023 
   1024     case BTA_AV_META_MSG_EVT: {
   1025       if (bt_rc_callbacks != NULL) {
   1026         BTIF_TRACE_DEBUG("%s: BTA_AV_META_MSG_EVT code: %d label: %d", __func__,
   1027                          p_data->meta_msg.code, p_data->meta_msg.label);
   1028         BTIF_TRACE_DEBUG("%s: company_id: 0x%x len: %d handle: %d", __func__,
   1029                          p_data->meta_msg.company_id, p_data->meta_msg.len,
   1030                          p_data->meta_msg.rc_handle);
   1031 
   1032         /* handle the metamsg command */
   1033         handle_rc_metamsg_cmd(&(p_data->meta_msg));
   1034 
   1035         /* Free the Memory allocated for tAVRC_MSG */
   1036       } else if (bt_rc_ctrl_callbacks != NULL) {
   1037         /* This is case of Sink + CT + TG(for abs vol)) */
   1038         BTIF_TRACE_DEBUG(
   1039             "%s BTA_AV_META_MSG_EVT code:%d label:%d opcode %d ctype %d",
   1040             __func__, p_data->meta_msg.code, p_data->meta_msg.label,
   1041             p_data->meta_msg.p_msg->hdr.opcode,
   1042             p_data->meta_msg.p_msg->hdr.ctype);
   1043         BTIF_TRACE_DEBUG("%s company_id:0x%x len:%d handle:%d", __func__,
   1044                          p_data->meta_msg.company_id, p_data->meta_msg.len,
   1045                          p_data->meta_msg.rc_handle);
   1046         switch (p_data->meta_msg.p_msg->hdr.opcode) {
   1047           case AVRC_OP_VENDOR:
   1048             if ((p_data->meta_msg.code >= AVRC_RSP_NOT_IMPL) &&
   1049                 (p_data->meta_msg.code <= AVRC_RSP_INTERIM)) {
   1050               /* Its a response */
   1051               handle_avk_rc_metamsg_rsp(&(p_data->meta_msg));
   1052             } else if (p_data->meta_msg.code <= AVRC_CMD_GEN_INQ) {
   1053               /* Its a command  */
   1054               handle_avk_rc_metamsg_cmd(&(p_data->meta_msg));
   1055             }
   1056             break;
   1057 
   1058           case AVRC_OP_BROWSE:
   1059             if (p_data->meta_msg.p_msg->hdr.ctype == AVRC_CMD) {
   1060               handle_avk_rc_metamsg_cmd(&(p_data->meta_msg));
   1061             } else if (p_data->meta_msg.p_msg->hdr.ctype == AVRC_RSP) {
   1062               handle_avk_rc_metamsg_rsp(&(p_data->meta_msg));
   1063             }
   1064             break;
   1065         }
   1066       } else {
   1067         BTIF_TRACE_ERROR("Neither CTRL, nor TG is up, drop meta commands");
   1068       }
   1069     } break;
   1070 
   1071     default:
   1072       BTIF_TRACE_DEBUG("%s: Unhandled RC event : 0x%x", __func__, event);
   1073   }
   1074 }
   1075 
   1076 /***************************************************************************
   1077  **
   1078  ** Function       btif_rc_get_connected_peer
   1079  **
   1080  ** Description    Fetches the connected headset's address if any
   1081  **
   1082  ***************************************************************************/
   1083 bool btif_rc_get_connected_peer(RawAddress* peer_addr) {
   1084   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   1085     btif_rc_device_cb_t* p_dev = get_connected_device(idx);
   1086     if (p_dev != NULL && (p_dev->rc_connected == TRUE)) {
   1087       *peer_addr = p_dev->rc_addr;
   1088       return true;
   1089     }
   1090   }
   1091   return false;
   1092 }
   1093 
   1094 /***************************************************************************
   1095  **
   1096  ** Function       btif_rc_get_connected_peer_handle
   1097  **
   1098  ** Description    Fetches the connected headset's handle if any
   1099  **
   1100  ***************************************************************************/
   1101 uint8_t btif_rc_get_connected_peer_handle(const RawAddress& peer_addr) {
   1102   btif_rc_device_cb_t* p_dev = NULL;
   1103   p_dev = btif_rc_get_device_by_bda(&peer_addr);
   1104 
   1105   if (p_dev == NULL) {
   1106     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   1107     return BTRC_HANDLE_NONE;
   1108   }
   1109   return p_dev->rc_handle;
   1110 }
   1111 
   1112 /***************************************************************************
   1113  **
   1114  ** Function       btif_rc_check_handle_pending_play
   1115  **
   1116  ** Description    Clears the queued PLAY command. if |bSendToApp| is true,
   1117  **                forwards to app
   1118  **
   1119  ***************************************************************************/
   1120 
   1121 /* clear the queued PLAY command. if |bSendToApp| is true, forward to app */
   1122 void btif_rc_check_handle_pending_play(const RawAddress& peer_addr,
   1123                                        bool bSendToApp) {
   1124   btif_rc_device_cb_t* p_dev = NULL;
   1125   p_dev = btif_rc_get_device_by_bda(&peer_addr);
   1126 
   1127   if (p_dev == NULL) {
   1128     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   1129     return;
   1130   }
   1131 
   1132   BTIF_TRACE_DEBUG("%s: bSendToApp: %d", __func__, bSendToApp);
   1133   if (p_dev->rc_pending_play) {
   1134     if (bSendToApp) {
   1135       tBTA_AV_REMOTE_CMD remote_cmd;
   1136       APPL_TRACE_DEBUG("%s: Sending queued PLAYED event to app", __func__);
   1137 
   1138       memset(&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD));
   1139       remote_cmd.rc_handle = p_dev->rc_handle;
   1140       remote_cmd.rc_id = AVRC_ID_PLAY;
   1141       remote_cmd.hdr.ctype = AVRC_CMD_CTRL;
   1142       remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU;
   1143 
   1144       /* delay sending to app, else there is a timing issue in the framework,
   1145        ** which causes the audio to be on th device's speaker. Delay between
   1146        ** OPEN & RC_PLAYs
   1147       */
   1148       sleep_ms(200);
   1149       /* send to app - both PRESSED & RELEASED */
   1150       remote_cmd.key_state = AVRC_STATE_PRESS;
   1151       handle_rc_passthrough_cmd(&remote_cmd);
   1152 
   1153       sleep_ms(100);
   1154 
   1155       remote_cmd.key_state = AVRC_STATE_RELEASE;
   1156       handle_rc_passthrough_cmd(&remote_cmd);
   1157     }
   1158     p_dev->rc_pending_play = false;
   1159   }
   1160 }
   1161 
   1162 /* Generic reject response */
   1163 static void send_reject_response(uint8_t rc_handle, uint8_t label, uint8_t pdu,
   1164                                  uint8_t status, uint8_t opcode) {
   1165   uint8_t ctype = AVRC_RSP_REJ;
   1166   tAVRC_RESPONSE avrc_rsp;
   1167   BT_HDR* p_msg = NULL;
   1168   memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE));
   1169 
   1170   avrc_rsp.rsp.opcode = opcode;
   1171   avrc_rsp.rsp.pdu = pdu;
   1172   avrc_rsp.rsp.status = status;
   1173 
   1174   status = AVRC_BldResponse(rc_handle, &avrc_rsp, &p_msg);
   1175 
   1176   if (status != AVRC_STS_NO_ERROR) {
   1177     BTIF_TRACE_ERROR("%s: status not AVRC_STS_NO_ERROR", __func__);
   1178     return;
   1179   }
   1180 
   1181   BTIF_TRACE_DEBUG(
   1182       "%s: Sending error notification to handle: %d. pdu: %s,status: 0x%02x",
   1183       __func__, rc_handle, dump_rc_pdu(pdu), status);
   1184   BTA_AvMetaRsp(rc_handle, label, ctype, p_msg);
   1185 }
   1186 
   1187 /***************************************************************************
   1188  *  Function         get_rsp_type_code
   1189  *
   1190  *  - Argument:   status
   1191  *  - Description: Returns response type codes for particular command code and
   1192  *                 status.
   1193  *
   1194  ***************************************************************************/
   1195 static tBTA_AV_CODE get_rsp_type_code(tAVRC_STS status, tBTA_AV_CODE code) {
   1196   if (status != AVRC_STS_NO_ERROR) {
   1197     return AVRC_RSP_REJ;
   1198   }
   1199 
   1200   if (code < AVRC_RSP_NOT_IMPL) {
   1201     if (code == AVRC_CMD_NOTIF) return AVRC_RSP_INTERIM;
   1202 
   1203     if (code == AVRC_CMD_STATUS) return AVRC_RSP_IMPL_STBL;
   1204 
   1205     return AVRC_RSP_ACCEPT;
   1206   }
   1207 
   1208   return code;
   1209 }
   1210 
   1211 /***************************************************************************
   1212  *  Function       send_metamsg_rsp
   1213  *
   1214  *  - Argument:
   1215  *                  p_dev           Dev pointer
   1216  *                  index           Command index (= -1 if not used)
   1217  *                  label           Label of the RC response
   1218  *                  code            Response type
   1219  *                  pmetamsg_resp   Vendor response
   1220  *
   1221  *  - Description: Remote control metamsg response handler
   1222  *
   1223  ***************************************************************************/
   1224 static void send_metamsg_rsp(btif_rc_device_cb_t* p_dev, int index,
   1225                              uint8_t label, tBTA_AV_CODE code,
   1226                              tAVRC_RESPONSE* pmetamsg_resp) {
   1227   uint8_t ctype;
   1228 
   1229   if (p_dev == NULL) {
   1230     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   1231     return;
   1232   }
   1233 
   1234   if (pmetamsg_resp == NULL) {
   1235     BTIF_TRACE_WARNING("%s: Invalid response received from application",
   1236                        __func__);
   1237     return;
   1238   }
   1239 
   1240   BTIF_TRACE_EVENT(
   1241       "%s: rc_handle: %d, index: %d, label: %d, code: 0x%02x, pdu: %s",
   1242       __func__, p_dev->rc_handle, index, label, code,
   1243       dump_rc_pdu(pmetamsg_resp->rsp.pdu));
   1244 
   1245   if (index >= 0 && p_dev->rc_pdu_info[index].is_rsp_pending == false) {
   1246     BTIF_TRACE_ERROR("%s: is_rsp_pending false, returning", __func__);
   1247     return;
   1248   }
   1249 
   1250   ctype = get_rsp_type_code(pmetamsg_resp->rsp.status, code);
   1251 
   1252   /* if response is for register_notification, make sure the rc has
   1253   actually registered for this */
   1254   if ((pmetamsg_resp->rsp.pdu == AVRC_PDU_REGISTER_NOTIFICATION) &&
   1255       ((code == AVRC_RSP_CHANGED) || (code == AVRC_RSP_INTERIM))) {
   1256     bool bSent = false;
   1257     uint8_t event_id = pmetamsg_resp->reg_notif.event_id;
   1258     bool bNotify =
   1259         (p_dev->rc_connected) && (p_dev->rc_notif[event_id - 1].bNotify);
   1260 
   1261     /* de-register this notification for a CHANGED response */
   1262     p_dev->rc_notif[event_id - 1].bNotify = false;
   1263     BTIF_TRACE_DEBUG("%s: rc_handle: %d. event_id: 0x%02d bNotify: %u",
   1264                      __func__, p_dev->rc_handle, event_id, bNotify);
   1265     if (bNotify) {
   1266       BT_HDR* p_msg = NULL;
   1267       tAVRC_STS status;
   1268 
   1269       if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(
   1270                                     p_dev->rc_handle, pmetamsg_resp, &p_msg))) {
   1271         BTIF_TRACE_DEBUG(
   1272             "%s: Sending notification to rc_handle: %d. event_id: 0x%02d",
   1273             __func__, p_dev->rc_handle, event_id);
   1274         bSent = true;
   1275         BTA_AvMetaRsp(p_dev->rc_handle, p_dev->rc_notif[event_id - 1].label,
   1276                       ctype, p_msg);
   1277       } else {
   1278         BTIF_TRACE_WARNING(
   1279             "%s: failed to build metamsg response. status: 0x%02x", __func__,
   1280             status);
   1281       }
   1282     }
   1283 
   1284     if (!bSent) {
   1285       BTIF_TRACE_DEBUG(
   1286           "%s: Notification not sent, as there are no RC connections or the \
   1287                 CT has not subscribed for event_id: %s",
   1288           __func__, dump_rc_notification_event_id(event_id));
   1289     }
   1290   } else {
   1291     /* All other commands go here */
   1292 
   1293     BT_HDR* p_msg = NULL;
   1294     tAVRC_STS status;
   1295 
   1296     status = AVRC_BldResponse(p_dev->rc_handle, pmetamsg_resp, &p_msg);
   1297 
   1298     if (status == AVRC_STS_NO_ERROR) {
   1299       BTA_AvMetaRsp(p_dev->rc_handle, label, ctype, p_msg);
   1300     } else {
   1301       BTIF_TRACE_ERROR("%s: failed to build metamsg response. status: 0x%02x",
   1302                        __func__, status);
   1303     }
   1304   }
   1305 
   1306   if (index >= 0) {
   1307     p_dev->rc_pdu_info[index].ctype = 0;
   1308     p_dev->rc_pdu_info[index].label = 0;
   1309     p_dev->rc_pdu_info[index].is_rsp_pending = false;
   1310   }
   1311 }
   1312 
   1313 static uint8_t opcode_from_pdu(uint8_t pdu) {
   1314   uint8_t opcode = 0;
   1315 
   1316   switch (pdu) {
   1317     case AVRC_PDU_SET_BROWSED_PLAYER:
   1318     case AVRC_PDU_GET_FOLDER_ITEMS:
   1319     case AVRC_PDU_CHANGE_PATH:
   1320     case AVRC_PDU_GET_ITEM_ATTRIBUTES:
   1321     case AVRC_PDU_ADD_TO_NOW_PLAYING:
   1322     case AVRC_PDU_SEARCH:
   1323     case AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS:
   1324     case AVRC_PDU_GENERAL_REJECT:
   1325       opcode = AVRC_OP_BROWSE;
   1326       break;
   1327 
   1328     case AVRC_PDU_NEXT_GROUP:
   1329     case AVRC_PDU_PREV_GROUP: /* pass thru */
   1330       opcode = AVRC_OP_PASS_THRU;
   1331       break;
   1332 
   1333     default: /* vendor */
   1334       opcode = AVRC_OP_VENDOR;
   1335       break;
   1336   }
   1337 
   1338   return opcode;
   1339 }
   1340 
   1341 /***************************************************************************
   1342  * Function:  fill_attribute_id_array
   1343  *
   1344  * - Argument:
   1345  *     cmd_attribute_number         input attribute number from AVRCP command
   1346  *     cmd_attribute_id_array       input attribute list from AVRCP command
   1347  *     out_array_size               allocated size of out attribute id array
   1348  *     out_attribute_id_array       output attribute list resolved here
   1349  *
   1350  * - Description:
   1351  *     Resolve attribute id array as defined by the AVRCP specification.
   1352  *
   1353  * - Returns:
   1354  *     The number of attributes filled in
   1355  *
   1356  ***************************************************************************/
   1357 static uint8_t fill_attribute_id_array(
   1358     uint8_t cmd_attribute_number, btrc_media_attr_t* cmd_attribute_id_array,
   1359     size_t out_array_size, btrc_media_attr_t* out_attribute_id_array) {
   1360   /* Reset attribute array */
   1361   memset(out_attribute_id_array, 0, out_array_size);
   1362   /* Default case for cmd_attribute_number == 0xFF, No attribute */
   1363   uint8_t out_attribute_number = 0;
   1364   if (cmd_attribute_number == 0) {
   1365     /* All attributes */
   1366     out_attribute_number = out_array_size < AVRC_MAX_NUM_MEDIA_ATTR_ID
   1367                                ? out_array_size
   1368                                : AVRC_MAX_NUM_MEDIA_ATTR_ID;
   1369     for (int i = 0; i < out_attribute_number; i++) {
   1370       out_attribute_id_array[i] = (btrc_media_attr_t)(i + 1);
   1371     }
   1372   } else if (cmd_attribute_number != 0xFF) {
   1373     /* Attribute List */
   1374     out_attribute_number = 0;
   1375     int filled_id_count = 0;
   1376     for (int i = 0; (i < cmd_attribute_number) &&
   1377                     (out_attribute_number < out_array_size) &&
   1378                     (out_attribute_number < AVRC_MAX_NUM_MEDIA_ATTR_ID);
   1379          i++) {
   1380       /* Fill only valid entries */
   1381       if (AVRC_IS_VALID_MEDIA_ATTRIBUTE(cmd_attribute_id_array[i])) {
   1382         /* Skip the duplicate entries */
   1383         for (filled_id_count = 0; filled_id_count < out_attribute_number;
   1384              filled_id_count++) {
   1385           if (out_attribute_id_array[filled_id_count] ==
   1386               cmd_attribute_id_array[i])
   1387             break;
   1388         }
   1389         /* New ID */
   1390         if (filled_id_count == out_attribute_number) {
   1391           out_attribute_id_array[out_attribute_number] =
   1392               (btrc_media_attr_t)cmd_attribute_id_array[i];
   1393           out_attribute_number++;
   1394         }
   1395       }
   1396     }
   1397   }
   1398   return out_attribute_number;
   1399 }
   1400 
   1401 /*******************************************************************************
   1402  *
   1403  * Function         btif_rc_upstreams_evt
   1404  *
   1405  * Description      Executes AVRC UPSTREAMS events in btif context.
   1406  *
   1407  * Returns          void
   1408  *
   1409  ******************************************************************************/
   1410 static void btif_rc_upstreams_evt(uint16_t event, tAVRC_COMMAND* pavrc_cmd,
   1411                                   uint8_t ctype, uint8_t label,
   1412                                   btif_rc_device_cb_t* p_dev) {
   1413   BTIF_TRACE_EVENT("%s: pdu: %s handle: 0x%x ctype: %x label: %x event ID: %x",
   1414                    __func__, dump_rc_pdu(pavrc_cmd->pdu), p_dev->rc_handle,
   1415                    ctype, label, pavrc_cmd->reg_notif.event_id);
   1416   RawAddress rc_addr = p_dev->rc_addr;
   1417 
   1418   switch (event) {
   1419     case AVRC_PDU_GET_PLAY_STATUS: {
   1420       fill_pdu_queue(IDX_GET_PLAY_STATUS_RSP, ctype, label, true, p_dev);
   1421       HAL_CBACK(bt_rc_callbacks, get_play_status_cb, &rc_addr);
   1422     } break;
   1423     case AVRC_PDU_LIST_PLAYER_APP_ATTR:
   1424     case AVRC_PDU_LIST_PLAYER_APP_VALUES:
   1425     case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:
   1426     case AVRC_PDU_SET_PLAYER_APP_VALUE:
   1427     case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:
   1428     case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: {
   1429       /* TODO: Add support for Application Settings */
   1430       send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu,
   1431                            AVRC_STS_BAD_CMD, pavrc_cmd->cmd.opcode);
   1432     } break;
   1433     case AVRC_PDU_GET_ELEMENT_ATTR: {
   1434       btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
   1435       uint8_t num_attr = fill_attribute_id_array(
   1436           pavrc_cmd->get_elem_attrs.num_attr,
   1437           (btrc_media_attr_t*)pavrc_cmd->get_elem_attrs.attrs,
   1438           BTRC_MAX_ELEM_ATTR_SIZE, element_attrs);
   1439       if (num_attr == 0) {
   1440         BTIF_TRACE_ERROR(
   1441             "%s: No valid attributes requested in GET_ELEMENT_ATTRIBUTES",
   1442             __func__);
   1443         send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu,
   1444                              AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode);
   1445         return;
   1446       }
   1447       fill_pdu_queue(IDX_GET_ELEMENT_ATTR_RSP, ctype, label, true, p_dev);
   1448       HAL_CBACK(bt_rc_callbacks, get_element_attr_cb, num_attr, element_attrs,
   1449                 &rc_addr);
   1450     } break;
   1451     case AVRC_PDU_REGISTER_NOTIFICATION: {
   1452       if (pavrc_cmd->reg_notif.event_id == BTRC_EVT_PLAY_POS_CHANGED &&
   1453           pavrc_cmd->reg_notif.param == 0) {
   1454         BTIF_TRACE_WARNING(
   1455             "%s: Device registering position changed with illegal param 0.",
   1456             __func__);
   1457         send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu,
   1458                              AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode);
   1459         /* de-register this notification for a rejected response */
   1460         p_dev->rc_notif[BTRC_EVT_PLAY_POS_CHANGED - 1].bNotify = false;
   1461         return;
   1462       }
   1463       HAL_CBACK(bt_rc_callbacks, register_notification_cb,
   1464                 (btrc_event_id_t)pavrc_cmd->reg_notif.event_id,
   1465                 pavrc_cmd->reg_notif.param, &rc_addr);
   1466     } break;
   1467     case AVRC_PDU_INFORM_DISPLAY_CHARSET: {
   1468       tAVRC_RESPONSE avrc_rsp;
   1469       BTIF_TRACE_EVENT("%s: AVRC_PDU_INFORM_DISPLAY_CHARSET", __func__);
   1470       if (p_dev->rc_connected == true) {
   1471         memset(&(avrc_rsp.inform_charset), 0, sizeof(tAVRC_RSP));
   1472         avrc_rsp.inform_charset.opcode =
   1473             opcode_from_pdu(AVRC_PDU_INFORM_DISPLAY_CHARSET);
   1474         avrc_rsp.inform_charset.pdu = AVRC_PDU_INFORM_DISPLAY_CHARSET;
   1475         avrc_rsp.inform_charset.status = AVRC_STS_NO_ERROR;
   1476         send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp);
   1477       }
   1478     } break;
   1479 
   1480     case AVRC_PDU_GET_FOLDER_ITEMS: {
   1481       uint32_t attr_ids[BTRC_MAX_ELEM_ATTR_SIZE];
   1482       uint8_t num_attr;
   1483       num_attr = pavrc_cmd->get_items.attr_count;
   1484 
   1485       BTIF_TRACE_EVENT(
   1486           "%s: AVRC_PDU_GET_FOLDER_ITEMS num_attr: %d, start_item [%d] \
   1487                 end_item [%d]",
   1488           __func__, num_attr, pavrc_cmd->get_items.start_item,
   1489           pavrc_cmd->get_items.end_item);
   1490 
   1491       /* num_attr requested:
   1492        *     0x00: All attributes requested
   1493        *     0xFF: No Attributes requested
   1494        *     0x01 to 0x07: Specified number of attributes
   1495        */
   1496       if ((num_attr != 0xFF && num_attr > BTRC_MAX_ELEM_ATTR_SIZE)) {
   1497         send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu,
   1498                              AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode);
   1499         return;
   1500       }
   1501 
   1502       /* Except num_attr is None(0xff) / All(0x00), request follows with an
   1503        * Attribute List */
   1504       if ((num_attr != 0xFF) && (num_attr != 0x00)) {
   1505         memcpy(attr_ids, pavrc_cmd->get_items.p_attr_list,
   1506                sizeof(uint32_t) * num_attr);
   1507       }
   1508 
   1509       fill_pdu_queue(IDX_GET_FOLDER_ITEMS_RSP, ctype, label, true, p_dev);
   1510       HAL_CBACK(bt_rc_callbacks, get_folder_items_cb,
   1511                 pavrc_cmd->get_items.scope, pavrc_cmd->get_items.start_item,
   1512                 pavrc_cmd->get_items.end_item, num_attr, attr_ids, &rc_addr);
   1513     } break;
   1514 
   1515     case AVRC_PDU_SET_ADDRESSED_PLAYER: {
   1516       fill_pdu_queue(IDX_SET_ADDR_PLAYER_RSP, ctype, label, true, p_dev);
   1517       HAL_CBACK(bt_rc_callbacks, set_addressed_player_cb,
   1518                 pavrc_cmd->addr_player.player_id, &rc_addr);
   1519     } break;
   1520 
   1521     case AVRC_PDU_SET_BROWSED_PLAYER: {
   1522       fill_pdu_queue(IDX_SET_BROWSED_PLAYER_RSP, ctype, label, true, p_dev);
   1523       HAL_CBACK(bt_rc_callbacks, set_browsed_player_cb,
   1524                 pavrc_cmd->br_player.player_id, &rc_addr);
   1525     } break;
   1526 
   1527     case AVRC_PDU_REQUEST_CONTINUATION_RSP: {
   1528       BTIF_TRACE_EVENT("%s() REQUEST CONTINUATION: target_pdu: 0x%02d",
   1529                        __func__, pavrc_cmd->continu.target_pdu);
   1530       tAVRC_RESPONSE avrc_rsp;
   1531       if (p_dev->rc_connected == TRUE) {
   1532         memset(&(avrc_rsp.continu), 0, sizeof(tAVRC_NEXT_RSP));
   1533         avrc_rsp.continu.opcode =
   1534             opcode_from_pdu(AVRC_PDU_REQUEST_CONTINUATION_RSP);
   1535         avrc_rsp.continu.pdu = AVRC_PDU_REQUEST_CONTINUATION_RSP;
   1536         avrc_rsp.continu.status = AVRC_STS_NO_ERROR;
   1537         avrc_rsp.continu.target_pdu = pavrc_cmd->continu.target_pdu;
   1538         send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp);
   1539       }
   1540     } break;
   1541 
   1542     case AVRC_PDU_ABORT_CONTINUATION_RSP: {
   1543       BTIF_TRACE_EVENT("%s() ABORT CONTINUATION: target_pdu: 0x%02d", __func__,
   1544                        pavrc_cmd->abort.target_pdu);
   1545       tAVRC_RESPONSE avrc_rsp;
   1546       if (p_dev->rc_connected == TRUE) {
   1547         memset(&(avrc_rsp.abort), 0, sizeof(tAVRC_NEXT_RSP));
   1548         avrc_rsp.abort.opcode =
   1549             opcode_from_pdu(AVRC_PDU_ABORT_CONTINUATION_RSP);
   1550         avrc_rsp.abort.pdu = AVRC_PDU_ABORT_CONTINUATION_RSP;
   1551         avrc_rsp.abort.status = AVRC_STS_NO_ERROR;
   1552         avrc_rsp.abort.target_pdu = pavrc_cmd->continu.target_pdu;
   1553         send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp);
   1554       }
   1555     } break;
   1556 
   1557     case AVRC_PDU_CHANGE_PATH: {
   1558       fill_pdu_queue(IDX_CHG_PATH_RSP, ctype, label, true, p_dev);
   1559       HAL_CBACK(bt_rc_callbacks, change_path_cb, pavrc_cmd->chg_path.direction,
   1560                 pavrc_cmd->chg_path.folder_uid, &rc_addr);
   1561     } break;
   1562 
   1563     case AVRC_PDU_SEARCH: {
   1564       fill_pdu_queue(IDX_SEARCH_RSP, ctype, label, true, p_dev);
   1565       HAL_CBACK(bt_rc_callbacks, search_cb, pavrc_cmd->search.string.charset_id,
   1566                 pavrc_cmd->search.string.str_len,
   1567                 pavrc_cmd->search.string.p_str, &rc_addr);
   1568     } break;
   1569 
   1570     case AVRC_PDU_GET_ITEM_ATTRIBUTES: {
   1571       btrc_media_attr_t item_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
   1572       uint8_t num_attr = fill_attribute_id_array(
   1573           pavrc_cmd->get_attrs.attr_count,
   1574           (btrc_media_attr_t*)pavrc_cmd->get_attrs.p_attr_list,
   1575           BTRC_MAX_ELEM_ATTR_SIZE, item_attrs);
   1576       if (num_attr == 0) {
   1577         BTIF_TRACE_ERROR(
   1578             "%s: No valid attributes requested in GET_ITEM_ATTRIBUTES",
   1579             __func__);
   1580         send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu,
   1581                              AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode);
   1582         return;
   1583       }
   1584       fill_pdu_queue(IDX_GET_ITEM_ATTR_RSP, ctype, label, true, p_dev);
   1585       BTIF_TRACE_DEBUG("%s: GET_ITEM_ATTRIBUTES: num_attr: %d", __func__,
   1586                        num_attr);
   1587       HAL_CBACK(bt_rc_callbacks, get_item_attr_cb, pavrc_cmd->get_attrs.scope,
   1588                 pavrc_cmd->get_attrs.uid, pavrc_cmd->get_attrs.uid_counter,
   1589                 num_attr, item_attrs, &rc_addr);
   1590     } break;
   1591 
   1592     case AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS: {
   1593       fill_pdu_queue(IDX_GET_TOTAL_NUM_OF_ITEMS_RSP, ctype, label, true, p_dev);
   1594       HAL_CBACK(bt_rc_callbacks, get_total_num_of_items_cb,
   1595                 pavrc_cmd->get_num_of_items.scope, &rc_addr);
   1596     } break;
   1597 
   1598     case AVRC_PDU_ADD_TO_NOW_PLAYING: {
   1599       fill_pdu_queue(IDX_ADD_TO_NOW_PLAYING_RSP, ctype, label, true, p_dev);
   1600       HAL_CBACK(bt_rc_callbacks, add_to_now_playing_cb,
   1601                 pavrc_cmd->add_to_play.scope, pavrc_cmd->add_to_play.uid,
   1602                 pavrc_cmd->add_to_play.uid_counter, &rc_addr);
   1603     } break;
   1604 
   1605     case AVRC_PDU_PLAY_ITEM: {
   1606       fill_pdu_queue(IDX_PLAY_ITEM_RSP, ctype, label, true, p_dev);
   1607       HAL_CBACK(bt_rc_callbacks, play_item_cb, pavrc_cmd->play_item.scope,
   1608                 pavrc_cmd->play_item.uid_counter, pavrc_cmd->play_item.uid,
   1609                 &rc_addr);
   1610     } break;
   1611 
   1612     default: {
   1613       send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu,
   1614                            AVRC_STS_BAD_CMD, pavrc_cmd->cmd.opcode);
   1615       return;
   1616     } break;
   1617   }
   1618 }
   1619 
   1620 /*******************************************************************************
   1621  *
   1622  * Function         btif_rc_ctrl_upstreams_rsp_cmd
   1623  *
   1624  * Description      Executes AVRC UPSTREAMS response events in btif context.
   1625  *
   1626  * Returns          void
   1627  *
   1628  ******************************************************************************/
   1629 static void btif_rc_ctrl_upstreams_rsp_cmd(uint8_t event,
   1630                                            tAVRC_COMMAND* pavrc_cmd,
   1631                                            uint8_t label,
   1632                                            btif_rc_device_cb_t* p_dev) {
   1633   BTIF_TRACE_DEBUG("%s: pdu: %s: handle: 0x%x", __func__,
   1634                    dump_rc_pdu(pavrc_cmd->pdu), p_dev->rc_handle);
   1635   RawAddress rc_addr = p_dev->rc_addr;
   1636   switch (event) {
   1637     case AVRC_PDU_SET_ABSOLUTE_VOLUME:
   1638       HAL_CBACK(bt_rc_ctrl_callbacks, setabsvol_cmd_cb, &rc_addr,
   1639                 pavrc_cmd->volume.volume, label);
   1640       break;
   1641     case AVRC_PDU_REGISTER_NOTIFICATION:
   1642       if (pavrc_cmd->reg_notif.event_id == AVRC_EVT_VOLUME_CHANGE) {
   1643         HAL_CBACK(bt_rc_ctrl_callbacks, registernotification_absvol_cb,
   1644                   &rc_addr, label);
   1645       }
   1646       break;
   1647   }
   1648 }
   1649 
   1650 /*******************************************************************************
   1651  *
   1652  * Function         btif_rc_upstreams_rsp_evt
   1653  *
   1654  * Description      Executes AVRC UPSTREAMS response events in btif context.
   1655  *
   1656  * Returns          void
   1657  *
   1658  ******************************************************************************/
   1659 static void btif_rc_upstreams_rsp_evt(uint16_t event,
   1660                                       tAVRC_RESPONSE* pavrc_resp, uint8_t ctype,
   1661                                       uint8_t label,
   1662                                       btif_rc_device_cb_t* p_dev) {
   1663   BTIF_TRACE_EVENT("%s: pdu: %s: handle: 0x%x ctype: %x label: %x", __func__,
   1664                    dump_rc_pdu(pavrc_resp->pdu), p_dev->rc_handle, ctype,
   1665                    label);
   1666   RawAddress rc_addr = p_dev->rc_addr;
   1667 
   1668   switch (event) {
   1669     case AVRC_PDU_REGISTER_NOTIFICATION: {
   1670       if (AVRC_RSP_CHANGED == ctype)
   1671         p_dev->rc_volume = pavrc_resp->reg_notif.param.volume;
   1672       HAL_CBACK(bt_rc_callbacks, volume_change_cb,
   1673                 pavrc_resp->reg_notif.param.volume, ctype, &rc_addr);
   1674     } break;
   1675 
   1676     case AVRC_PDU_SET_ABSOLUTE_VOLUME: {
   1677       BTIF_TRACE_DEBUG(
   1678           "%s: Set absolute volume change event received: volume: %d, ctype: "
   1679           "%d",
   1680           __func__, pavrc_resp->volume.volume, ctype);
   1681       if (AVRC_RSP_ACCEPT == ctype)
   1682         p_dev->rc_volume = pavrc_resp->volume.volume;
   1683       HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->volume.volume,
   1684                 ctype, &rc_addr);
   1685     } break;
   1686 
   1687     default:
   1688       return;
   1689   }
   1690 }
   1691 
   1692 /*******************************************************************************
   1693  *  AVRCP API Functions
   1694  ******************************************************************************/
   1695 
   1696 /*******************************************************************************
   1697  *
   1698  * Function         init
   1699  *
   1700  * Description      Initializes the AVRC interface
   1701  *
   1702  * Returns          bt_status_t
   1703  *
   1704  ******************************************************************************/
   1705 static bt_status_t init(btrc_callbacks_t* callbacks) {
   1706   BTIF_TRACE_EVENT("%s: ", __func__);
   1707   bt_status_t result = BT_STATUS_SUCCESS;
   1708 
   1709   if (bt_rc_callbacks) return BT_STATUS_DONE;
   1710 
   1711   bt_rc_callbacks = callbacks;
   1712   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   1713     memset(&btif_rc_cb.rc_multi_cb[idx], 0,
   1714            sizeof(btif_rc_cb.rc_multi_cb[idx]));
   1715     btif_rc_cb.rc_multi_cb[idx].rc_vol_label = MAX_LABEL;
   1716     btif_rc_cb.rc_multi_cb[idx].rc_volume = MAX_VOLUME;
   1717     btif_rc_cb.rc_multi_cb[idx].rc_state = BTRC_CONNECTION_STATE_DISCONNECTED;
   1718   }
   1719   lbl_init();
   1720 
   1721   return result;
   1722 }
   1723 
   1724 /*******************************************************************************
   1725  *
   1726  * Function         init_ctrl
   1727  *
   1728  * Description      Initializes the AVRC interface
   1729  *
   1730  * Returns          bt_status_t
   1731  *
   1732  ******************************************************************************/
   1733 static bt_status_t init_ctrl(btrc_ctrl_callbacks_t* callbacks) {
   1734   BTIF_TRACE_EVENT("%s: ", __func__);
   1735   bt_status_t result = BT_STATUS_SUCCESS;
   1736 
   1737   if (bt_rc_ctrl_callbacks) return BT_STATUS_DONE;
   1738 
   1739   bt_rc_ctrl_callbacks = callbacks;
   1740   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   1741     memset(&btif_rc_cb.rc_multi_cb[idx], 0,
   1742            sizeof(btif_rc_cb.rc_multi_cb[idx]));
   1743     btif_rc_cb.rc_multi_cb[idx].rc_vol_label = MAX_LABEL;
   1744     btif_rc_cb.rc_multi_cb[idx].rc_volume = MAX_VOLUME;
   1745   }
   1746   lbl_init();
   1747 
   1748   return result;
   1749 }
   1750 
   1751 static void rc_ctrl_procedure_complete(btif_rc_device_cb_t* p_dev) {
   1752   if (p_dev == NULL) {
   1753     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   1754     return;
   1755   }
   1756 
   1757   if (p_dev->rc_procedure_complete == true) {
   1758     return;
   1759   }
   1760   p_dev->rc_procedure_complete = true;
   1761   uint32_t attr_list[] = {
   1762       AVRC_MEDIA_ATTR_ID_TITLE,       AVRC_MEDIA_ATTR_ID_ARTIST,
   1763       AVRC_MEDIA_ATTR_ID_ALBUM,       AVRC_MEDIA_ATTR_ID_TRACK_NUM,
   1764       AVRC_MEDIA_ATTR_ID_NUM_TRACKS,  AVRC_MEDIA_ATTR_ID_GENRE,
   1765       AVRC_MEDIA_ATTR_ID_PLAYING_TIME};
   1766   get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev);
   1767 }
   1768 
   1769 /***************************************************************************
   1770  *
   1771  * Function         get_play_status_rsp
   1772  *
   1773  * Description      Returns the current play status.
   1774  *                      This method is called in response to
   1775  *                      GetPlayStatus request.
   1776  *
   1777  * Returns          bt_status_t
   1778  *
   1779  **************************************************************************/
   1780 static bt_status_t get_play_status_rsp(RawAddress* bd_addr,
   1781                                        btrc_play_status_t play_status,
   1782                                        uint32_t song_len, uint32_t song_pos) {
   1783   tAVRC_RESPONSE avrc_rsp;
   1784   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   1785 
   1786   BTIF_TRACE_DEBUG("%s: song len %d song pos %d", __func__, song_len, song_pos);
   1787   CHECK_RC_CONNECTED(p_dev);
   1788 
   1789   memset(&(avrc_rsp.get_play_status), 0, sizeof(tAVRC_GET_PLAY_STATUS_RSP));
   1790 
   1791   avrc_rsp.get_play_status.song_len = song_len;
   1792   avrc_rsp.get_play_status.song_pos = song_pos;
   1793   avrc_rsp.get_play_status.play_status = play_status;
   1794 
   1795   avrc_rsp.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS;
   1796   avrc_rsp.get_play_status.opcode = opcode_from_pdu(AVRC_PDU_GET_PLAY_STATUS);
   1797   avrc_rsp.get_play_status.status =
   1798       ((play_status != BTRC_PLAYSTATE_ERROR) ? AVRC_STS_NO_ERROR
   1799                                              : AVRC_STS_BAD_PARAM);
   1800 
   1801   /* Send the response */
   1802   send_metamsg_rsp(p_dev, IDX_GET_PLAY_STATUS_RSP,
   1803                    p_dev->rc_pdu_info[IDX_GET_PLAY_STATUS_RSP].label,
   1804                    p_dev->rc_pdu_info[IDX_GET_PLAY_STATUS_RSP].ctype,
   1805                    &avrc_rsp);
   1806 
   1807   return BT_STATUS_SUCCESS;
   1808 }
   1809 
   1810 /***************************************************************************
   1811  *
   1812  * Function         get_element_attr_rsp
   1813  *
   1814  * Description      Returns the current songs' element attributes
   1815  *                      in text.
   1816  *
   1817  * Returns          bt_status_t
   1818  *
   1819  **************************************************************************/
   1820 static bt_status_t get_element_attr_rsp(RawAddress* bd_addr, uint8_t num_attr,
   1821                                         btrc_element_attr_val_t* p_attrs) {
   1822   tAVRC_RESPONSE avrc_rsp;
   1823   uint32_t i;
   1824   tAVRC_ATTR_ENTRY element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
   1825   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   1826 
   1827   BTIF_TRACE_DEBUG("%s", __func__);
   1828   CHECK_RC_CONNECTED(p_dev);
   1829 
   1830   memset(element_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr);
   1831 
   1832   if (num_attr == 0) {
   1833     avrc_rsp.get_play_status.status = AVRC_STS_BAD_PARAM;
   1834   } else {
   1835     for (i = 0; i < num_attr; i++) {
   1836       element_attrs[i].attr_id = p_attrs[i].attr_id;
   1837       element_attrs[i].name.charset_id = AVRC_CHARSET_ID_UTF8;
   1838       element_attrs[i].name.str_len = (uint16_t)strlen((char*)p_attrs[i].text);
   1839       element_attrs[i].name.p_str = p_attrs[i].text;
   1840       BTIF_TRACE_DEBUG(
   1841           "%s: attr_id: 0x%x, charset_id: 0x%x, str_len: %d, str: %s", __func__,
   1842           (unsigned int)element_attrs[i].attr_id,
   1843           element_attrs[i].name.charset_id, element_attrs[i].name.str_len,
   1844           element_attrs[i].name.p_str);
   1845     }
   1846     avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
   1847   }
   1848   avrc_rsp.get_attrs.num_attrs = num_attr;
   1849   avrc_rsp.get_attrs.p_attrs = element_attrs;
   1850   avrc_rsp.get_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR;
   1851   avrc_rsp.get_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ELEMENT_ATTR);
   1852 
   1853   /* Send the response */
   1854   send_metamsg_rsp(p_dev, IDX_GET_ELEMENT_ATTR_RSP,
   1855                    p_dev->rc_pdu_info[IDX_GET_ELEMENT_ATTR_RSP].label,
   1856                    p_dev->rc_pdu_info[IDX_GET_ELEMENT_ATTR_RSP].ctype,
   1857                    &avrc_rsp);
   1858 
   1859   return BT_STATUS_SUCCESS;
   1860 }
   1861 
   1862 /***************************************************************************
   1863  *
   1864  * Function         register_notification_rsp
   1865  *
   1866  * Description      Response to the register notification request.
   1867  *
   1868  * Returns          bt_status_t
   1869  *
   1870  **************************************************************************/
   1871 static bt_status_t register_notification_rsp(
   1872     btrc_event_id_t event_id, btrc_notification_type_t type,
   1873     btrc_register_notification_t* p_param) {
   1874   tAVRC_RESPONSE avrc_rsp;
   1875   BTIF_TRACE_EVENT("%s: event_id: %s", __func__,
   1876                    dump_rc_notification_event_id(event_id));
   1877   std::unique_lock<std::mutex> lock(btif_rc_cb.lock);
   1878 
   1879   memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP));
   1880 
   1881   avrc_rsp.reg_notif.event_id = event_id;
   1882   avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
   1883   avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION);
   1884   avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR;
   1885 
   1886   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   1887     memset(&(avrc_rsp.reg_notif.param), 0, sizeof(tAVRC_NOTIF_RSP_PARAM));
   1888 
   1889     if (!(btif_rc_cb.rc_multi_cb[idx].rc_connected)) {
   1890       BTIF_TRACE_ERROR("%s: Avrcp device is not connected, handle: 0x%x",
   1891                        __func__, btif_rc_cb.rc_multi_cb[idx].rc_handle);
   1892       continue;
   1893     }
   1894 
   1895     if (btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id - 1].bNotify == false) {
   1896       BTIF_TRACE_WARNING(
   1897           "%s: Avrcp Event id is not registered: event_id: %x, handle: 0x%x",
   1898           __func__, event_id, btif_rc_cb.rc_multi_cb[idx].rc_handle);
   1899       continue;
   1900     }
   1901 
   1902     BTIF_TRACE_DEBUG(
   1903         "%s: Avrcp Event id is registered: event_id: %x handle: 0x%x", __func__,
   1904         event_id, btif_rc_cb.rc_multi_cb[idx].rc_handle);
   1905 
   1906     switch (event_id) {
   1907       case BTRC_EVT_PLAY_STATUS_CHANGED:
   1908         avrc_rsp.reg_notif.param.play_status = p_param->play_status;
   1909         if (avrc_rsp.reg_notif.param.play_status == PLAY_STATUS_PLAYING)
   1910           btif_av_clear_remote_suspend_flag();
   1911         break;
   1912       case BTRC_EVT_TRACK_CHANGE:
   1913         memcpy(&(avrc_rsp.reg_notif.param.track), &(p_param->track),
   1914                sizeof(btrc_uid_t));
   1915         break;
   1916       case BTRC_EVT_PLAY_POS_CHANGED:
   1917         avrc_rsp.reg_notif.param.play_pos = p_param->song_pos;
   1918         break;
   1919       case BTRC_EVT_AVAL_PLAYER_CHANGE:
   1920         break;
   1921       case BTRC_EVT_ADDR_PLAYER_CHANGE:
   1922         avrc_rsp.reg_notif.param.addr_player.player_id =
   1923             p_param->addr_player_changed.player_id;
   1924         avrc_rsp.reg_notif.param.addr_player.uid_counter =
   1925             p_param->addr_player_changed.uid_counter;
   1926         break;
   1927       case BTRC_EVT_UIDS_CHANGED:
   1928         avrc_rsp.reg_notif.param.uid_counter =
   1929             p_param->uids_changed.uid_counter;
   1930         break;
   1931       case BTRC_EVT_NOW_PLAYING_CONTENT_CHANGED:
   1932         break;
   1933 
   1934       default:
   1935         BTIF_TRACE_WARNING("%s: Unhandled event ID: 0x%x", __func__, event_id);
   1936         return BT_STATUS_UNHANDLED;
   1937     }
   1938 
   1939     /* Send the response. */
   1940     send_metamsg_rsp(
   1941         &btif_rc_cb.rc_multi_cb[idx], -1,
   1942         btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id - 1].label,
   1943         ((type == BTRC_NOTIFICATION_TYPE_INTERIM) ? AVRC_CMD_NOTIF
   1944                                                   : AVRC_RSP_CHANGED),
   1945         &avrc_rsp);
   1946   }
   1947   return BT_STATUS_SUCCESS;
   1948 }
   1949 
   1950 /***************************************************************************
   1951  *
   1952  * Function         get_folder_items_list_rsp
   1953  *
   1954  * Description      Returns the list of media items in current folder along with
   1955  *                  requested attributes. This is called in response to
   1956  *                  GetFolderItems request.
   1957  *
   1958  * Returns          bt_status_t
   1959  *                      BT_STATUS_NOT_READY - when RC is not connected.
   1960  *                      BT_STATUS_SUCCESS   - always if RC is connected
   1961  *                      BT_STATUS_UNHANDLED - when rsp is not pending for
   1962  *                                            get_folder_items_list PDU
   1963  *
   1964  **************************************************************************/
   1965 static bt_status_t get_folder_items_list_rsp(RawAddress* bd_addr,
   1966                                              btrc_status_t rsp_status,
   1967                                              uint16_t uid_counter,
   1968                                              uint8_t num_items,
   1969                                              btrc_folder_items_t* p_items) {
   1970   tAVRC_RESPONSE avrc_rsp;
   1971   tAVRC_ITEM item;
   1972   tBTA_AV_CODE code = 0, ctype = 0;
   1973   BT_HDR* p_msg = NULL;
   1974   int item_cnt;
   1975   tAVRC_STS status = AVRC_STS_NO_ERROR;
   1976   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   1977   btrc_folder_items_t* cur_item = NULL;
   1978 
   1979   BTIF_TRACE_DEBUG("%s: uid_counter %d num_items %d", __func__, uid_counter,
   1980                    num_items);
   1981   CHECK_RC_CONNECTED(p_dev);
   1982 
   1983   /* check if rsp to previous cmd was completed */
   1984   if (p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].is_rsp_pending == false) {
   1985     BTIF_TRACE_WARNING("%s: Not sending response as no PDU was registered",
   1986                        __func__);
   1987     return BT_STATUS_UNHANDLED;
   1988   }
   1989 
   1990   memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE));
   1991   memset(&item, 0, sizeof(tAVRC_ITEM));
   1992 
   1993   avrc_rsp.get_items.pdu = AVRC_PDU_GET_FOLDER_ITEMS;
   1994   avrc_rsp.get_items.opcode = opcode_from_pdu(AVRC_PDU_GET_FOLDER_ITEMS);
   1995   avrc_rsp.get_items.status = status_code_map[rsp_status];
   1996 
   1997   if (avrc_rsp.get_items.status != AVRC_STS_NO_ERROR) {
   1998     BTIF_TRACE_WARNING(
   1999         "%s: Error in parsing the received getfolderitems cmd. status: 0x%02x",
   2000         __func__, avrc_rsp.get_items.status);
   2001     status = avrc_rsp.get_items.status;
   2002   } else {
   2003     avrc_rsp.get_items.uid_counter = uid_counter;
   2004     avrc_rsp.get_items.item_count = 1;
   2005 
   2006     /* create single item and build response iteratively for all num_items */
   2007     for (item_cnt = 0; item_cnt < num_items; item_cnt++) {
   2008       cur_item = &p_items[item_cnt];
   2009       item.item_type = p_items->item_type;
   2010       /* build respective item based on item_type. All items should be of same
   2011        * type within
   2012        * a response */
   2013       switch (p_items->item_type) {
   2014         case AVRC_ITEM_PLAYER: {
   2015           item.u.player.name.charset_id = cur_item->player.charset_id;
   2016           memcpy(&(item.u.player.features), &(cur_item->player.features),
   2017                  sizeof(cur_item->player.features));
   2018           item.u.player.major_type = cur_item->player.major_type;
   2019           item.u.player.sub_type = cur_item->player.sub_type;
   2020           item.u.player.play_status = cur_item->player.play_status;
   2021           item.u.player.player_id = cur_item->player.player_id;
   2022           item.u.player.name.p_str = cur_item->player.name;
   2023           item.u.player.name.str_len =
   2024               (uint16_t)strlen((char*)(cur_item->player.name));
   2025         } break;
   2026 
   2027         case AVRC_ITEM_FOLDER: {
   2028           memcpy(item.u.folder.uid, cur_item->folder.uid, sizeof(tAVRC_UID));
   2029           item.u.folder.type = cur_item->folder.type;
   2030           item.u.folder.playable = cur_item->folder.playable;
   2031           item.u.folder.name.charset_id = AVRC_CHARSET_ID_UTF8;
   2032           item.u.folder.name.str_len = strlen((char*)cur_item->folder.name);
   2033           item.u.folder.name.p_str = cur_item->folder.name;
   2034         } break;
   2035 
   2036         case AVRC_ITEM_MEDIA: {
   2037           tAVRC_ATTR_ENTRY attr_vals[BTRC_MAX_ELEM_ATTR_SIZE];
   2038 
   2039           memcpy(item.u.media.uid, cur_item->media.uid, sizeof(tAVRC_UID));
   2040           item.u.media.type = cur_item->media.type;
   2041           item.u.media.name.charset_id = cur_item->media.charset_id;
   2042           item.u.media.name.str_len = strlen((char*)cur_item->media.name);
   2043           item.u.media.name.p_str = cur_item->media.name;
   2044           item.u.media.attr_count = cur_item->media.num_attrs;
   2045 
   2046           /* Handle attributes of given item */
   2047           if (item.u.media.attr_count == 0) {
   2048             item.u.media.p_attr_list = NULL;
   2049           } else {
   2050             memset(&attr_vals, 0,
   2051                    sizeof(tAVRC_ATTR_ENTRY) * BTRC_MAX_ELEM_ATTR_SIZE);
   2052             fill_avrc_attr_entry(attr_vals, item.u.media.attr_count,
   2053                                  cur_item->media.p_attrs);
   2054             item.u.media.p_attr_list = attr_vals;
   2055           }
   2056         } break;
   2057 
   2058         default: {
   2059           BTIF_TRACE_ERROR("%s: Unknown item_type: %d. Internal Error",
   2060                            __func__, p_items->item_type);
   2061           status = AVRC_STS_INTERNAL_ERR;
   2062         } break;
   2063       }
   2064 
   2065       avrc_rsp.get_items.p_item_list = &item;
   2066 
   2067       /* Add current item to buffer and build response if no error in item type
   2068        */
   2069       if (status != AVRC_STS_NO_ERROR) {
   2070         /* Reject response due to error occured for unknown item_type, break the
   2071          * loop */
   2072         break;
   2073       }
   2074 
   2075       int len_before = p_msg ? p_msg->len : 0;
   2076       BTIF_TRACE_DEBUG("%s: item_cnt: %d len: %d", __func__, item_cnt,
   2077                        len_before);
   2078       status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg);
   2079       BTIF_TRACE_DEBUG("%s: Build rsp status: %d len: %d", __func__, status,
   2080                        (p_msg ? p_msg->len : 0));
   2081       int len_after = p_msg ? p_msg->len : 0;
   2082       if (status != AVRC_STS_NO_ERROR || len_before == len_after) {
   2083         /* Error occured in build response or we ran out of buffer so break the
   2084          * loop */
   2085         break;
   2086       }
   2087     }
   2088 
   2089     /* setting the error status */
   2090     avrc_rsp.get_items.status = status;
   2091   }
   2092 
   2093   /* if packet built successfully, send the built items to BTA layer */
   2094   if (status == AVRC_STS_NO_ERROR) {
   2095     code = p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].ctype;
   2096     ctype = get_rsp_type_code(avrc_rsp.get_items.status, code);
   2097     BTA_AvMetaRsp(p_dev->rc_handle,
   2098                   p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label, ctype,
   2099                   p_msg);
   2100   } else /* Error occured, send reject response */
   2101   {
   2102     BTIF_TRACE_ERROR("%s: Error status: 0x%02X. Sending reject rsp", __func__,
   2103                      avrc_rsp.rsp.status);
   2104     send_reject_response(
   2105         p_dev->rc_handle, p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label,
   2106         avrc_rsp.pdu, avrc_rsp.get_items.status, avrc_rsp.get_items.opcode);
   2107   }
   2108 
   2109   /* Reset values for current pdu. */
   2110   p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].ctype = 0;
   2111   p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label = 0;
   2112   p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].is_rsp_pending = false;
   2113 
   2114   return status == AVRC_STS_NO_ERROR ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
   2115 }
   2116 
   2117 /***************************************************************************
   2118  *
   2119  * Function         set_addressed_player_rsp
   2120  *
   2121  * Description      Response to set the addressed player for specified media
   2122  *                  player based on id in the media player list.
   2123  *
   2124  * Returns          bt_status_t
   2125  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2126  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2127  *
   2128  **************************************************************************/
   2129 static bt_status_t set_addressed_player_rsp(RawAddress* bd_addr,
   2130                                             btrc_status_t rsp_status) {
   2131   tAVRC_RESPONSE avrc_rsp;
   2132   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2133 
   2134   BTIF_TRACE_DEBUG("%s", __func__);
   2135   CHECK_RC_CONNECTED(p_dev);
   2136 
   2137   avrc_rsp.addr_player.pdu = AVRC_PDU_SET_ADDRESSED_PLAYER;
   2138   avrc_rsp.addr_player.opcode = opcode_from_pdu(AVRC_PDU_SET_ADDRESSED_PLAYER);
   2139   avrc_rsp.addr_player.status = status_code_map[rsp_status];
   2140 
   2141   /* Send the response. */
   2142   send_metamsg_rsp(p_dev, IDX_SET_ADDR_PLAYER_RSP,
   2143                    p_dev->rc_pdu_info[IDX_SET_ADDR_PLAYER_RSP].label,
   2144                    p_dev->rc_pdu_info[IDX_SET_ADDR_PLAYER_RSP].ctype,
   2145                    &avrc_rsp);
   2146 
   2147   return BT_STATUS_SUCCESS;
   2148 }
   2149 
   2150 /***************************************************************************
   2151  *
   2152  * Function         set_browsed_player_rsp
   2153  *
   2154  * Description      Response to set the browsed player command which contains
   2155  *                  current browsed path of the media player. By default,
   2156  *                  current_path = root and folder_depth = 0 for
   2157  *                  every set_browsed_player request.
   2158  *
   2159  * Returns          bt_status_t
   2160  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2161  *                      BT_STATUS_SUCCESS   - if RC is connected and reponse
   2162  *                                            sent successfully
   2163  *                      BT_STATUS_UNHANDLED - when rsp is not pending for
   2164  *                                            set_browsed_player PDU
   2165  *
   2166  **************************************************************************/
   2167 static bt_status_t set_browsed_player_rsp(RawAddress* bd_addr,
   2168                                           btrc_status_t rsp_status,
   2169                                           uint32_t num_items,
   2170                                           uint16_t charset_id,
   2171                                           uint8_t folder_depth,
   2172                                           btrc_br_folder_name_t* p_folders) {
   2173   tAVRC_RESPONSE avrc_rsp;
   2174   tAVRC_NAME item;
   2175   BT_HDR* p_msg = NULL;
   2176   tBTA_AV_CODE code = 0;
   2177   tBTA_AV_CODE ctype = 0;
   2178   unsigned int item_cnt;
   2179   tAVRC_STS status = AVRC_STS_NO_ERROR;
   2180   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2181 
   2182   CHECK_RC_CONNECTED(p_dev);
   2183 
   2184   memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE));
   2185   memset(&item, 0, sizeof(tAVRC_NAME));
   2186 
   2187   avrc_rsp.br_player.status = status_code_map[rsp_status];
   2188   avrc_rsp.br_player.pdu = AVRC_PDU_SET_BROWSED_PLAYER;
   2189   avrc_rsp.br_player.opcode = opcode_from_pdu(AVRC_PDU_SET_BROWSED_PLAYER);
   2190 
   2191   BTIF_TRACE_DEBUG("%s: rsp_status: 0x%02X avrc_rsp.br_player.status: 0x%02X",
   2192                    __func__, rsp_status, avrc_rsp.br_player.status);
   2193 
   2194   /* check if rsp to previous cmd was completed */
   2195   if (p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].is_rsp_pending == false) {
   2196     BTIF_TRACE_WARNING("%s: Not sending response as no PDU was registered",
   2197                        __func__);
   2198     return BT_STATUS_UNHANDLED;
   2199   }
   2200 
   2201   if (AVRC_STS_NO_ERROR == avrc_rsp.get_items.status) {
   2202     avrc_rsp.br_player.num_items = num_items;
   2203     avrc_rsp.br_player.charset_id = charset_id;
   2204     avrc_rsp.br_player.folder_depth = folder_depth;
   2205     avrc_rsp.br_player.p_folders = (tAVRC_NAME*)p_folders;
   2206 
   2207     BTIF_TRACE_DEBUG("%s: folder_depth: 0x%02X num_items: %d", __func__,
   2208                      folder_depth, num_items);
   2209 
   2210     if (folder_depth > 0) {
   2211       /* Iteratively build response for all folders across folder depth upto
   2212        * current path */
   2213       avrc_rsp.br_player.folder_depth = 1;
   2214       for (item_cnt = 0; item_cnt < folder_depth; item_cnt++) {
   2215         BTIF_TRACE_DEBUG("%s: iteration: %d", __func__, item_cnt);
   2216         item.str_len = p_folders[item_cnt].str_len;
   2217         item.p_str = p_folders[item_cnt].p_str;
   2218         avrc_rsp.br_player.p_folders = &item;
   2219 
   2220         /* Add current item to buffer and build response */
   2221         status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg);
   2222         if (AVRC_STS_NO_ERROR != status) {
   2223           BTIF_TRACE_WARNING("%s: Build rsp status: %d", __func__, status);
   2224           /* if the build fails, it is likely that we ran out of buffer. so if
   2225         * we have
   2226         * some items to send, reset this error to no error for sending what we
   2227         * have */
   2228           if (item_cnt > 0) status = AVRC_STS_NO_ERROR;
   2229 
   2230           /* Error occured in build response so break the loop */
   2231           break;
   2232         }
   2233       }
   2234     } else /* current path is root folder, no folders navigated yet */
   2235     {
   2236       status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg);
   2237     }
   2238 
   2239     /* setting the error status */
   2240     avrc_rsp.br_player.status = status;
   2241   } else /* error received from above layer */
   2242   {
   2243     BTIF_TRACE_WARNING(
   2244         "%s: Error in parsing the received setbrowsed command. status: 0x%02x",
   2245         __func__, avrc_rsp.br_player.status);
   2246     status = avrc_rsp.br_player.status;
   2247   }
   2248 
   2249   /* if packet built successfully, send the built items to BTA layer */
   2250   if (status == AVRC_STS_NO_ERROR) {
   2251     code = p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].ctype;
   2252     ctype = get_rsp_type_code(avrc_rsp.br_player.status, code);
   2253     BTA_AvMetaRsp(p_dev->rc_handle,
   2254                   p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label, ctype,
   2255                   p_msg);
   2256   } else /* Error occured, send reject response */
   2257   {
   2258     BTIF_TRACE_ERROR("%s: Error status: 0x%02X. Sending reject rsp", __func__,
   2259                      avrc_rsp.br_player.status);
   2260     send_reject_response(
   2261         p_dev->rc_handle, p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label,
   2262         avrc_rsp.pdu, avrc_rsp.br_player.status, avrc_rsp.get_items.opcode);
   2263   }
   2264 
   2265   /* Reset values for set_browsed_player pdu.*/
   2266   p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].ctype = 0;
   2267   p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label = 0;
   2268   p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].is_rsp_pending = false;
   2269 
   2270   return status == AVRC_STS_NO_ERROR ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
   2271 }
   2272 
   2273 /*******************************************************************************
   2274  *
   2275  * Function         change_path_rsp
   2276  *
   2277  * Description      Response to the change path command which
   2278  *                  contains number of items in the changed path.
   2279  *
   2280  * Returns          bt_status_t
   2281  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2282  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2283  *
   2284  **************************************************************************/
   2285 static bt_status_t change_path_rsp(RawAddress* bd_addr,
   2286                                    btrc_status_t rsp_status,
   2287                                    uint32_t num_items) {
   2288   tAVRC_RESPONSE avrc_rsp;
   2289   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2290 
   2291   BTIF_TRACE_DEBUG("%s", __func__);
   2292   CHECK_RC_CONNECTED(p_dev);
   2293 
   2294   avrc_rsp.chg_path.pdu = AVRC_PDU_CHANGE_PATH;
   2295   avrc_rsp.chg_path.opcode = opcode_from_pdu(AVRC_PDU_CHANGE_PATH);
   2296   avrc_rsp.chg_path.num_items = num_items;
   2297   avrc_rsp.chg_path.status = status_code_map[rsp_status];
   2298 
   2299   /* Send the response. */
   2300   send_metamsg_rsp(p_dev, IDX_CHG_PATH_RSP,
   2301                    p_dev->rc_pdu_info[IDX_CHG_PATH_RSP].label,
   2302                    p_dev->rc_pdu_info[IDX_CHG_PATH_RSP].ctype, &avrc_rsp);
   2303 
   2304   return BT_STATUS_SUCCESS;
   2305 }
   2306 
   2307 /***************************************************************************
   2308  *
   2309  * Function         search_rsp
   2310  *
   2311  * Description      Response to search a string from media content command.
   2312  *
   2313  * Returns          bt_status_t
   2314  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2315  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2316  *
   2317  **************************************************************************/
   2318 static bt_status_t search_rsp(RawAddress* bd_addr, btrc_status_t rsp_status,
   2319                               uint32_t uid_counter, uint32_t num_items) {
   2320   tAVRC_RESPONSE avrc_rsp;
   2321   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2322 
   2323   BTIF_TRACE_DEBUG("%s", __func__);
   2324   CHECK_RC_CONNECTED(p_dev);
   2325 
   2326   avrc_rsp.search.pdu = AVRC_PDU_SEARCH;
   2327   avrc_rsp.search.opcode = opcode_from_pdu(AVRC_PDU_SEARCH);
   2328   avrc_rsp.search.num_items = num_items;
   2329   avrc_rsp.search.uid_counter = uid_counter;
   2330   avrc_rsp.search.status = status_code_map[rsp_status];
   2331 
   2332   /* Send the response. */
   2333   send_metamsg_rsp(p_dev, IDX_SEARCH_RSP,
   2334                    p_dev->rc_pdu_info[IDX_SEARCH_RSP].label,
   2335                    p_dev->rc_pdu_info[IDX_SEARCH_RSP].ctype, &avrc_rsp);
   2336 
   2337   return BT_STATUS_SUCCESS;
   2338 }
   2339 /***************************************************************************
   2340  *
   2341  * Function         get_item_attr_rsp
   2342  *
   2343  * Description      Response to the get item's attributes command which
   2344  *                  contains number of attributes and values list in text.
   2345  *
   2346  * Returns          bt_status_t
   2347  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2348  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2349  *
   2350  **************************************************************************/
   2351 static bt_status_t get_item_attr_rsp(RawAddress* bd_addr,
   2352                                      btrc_status_t rsp_status, uint8_t num_attr,
   2353                                      btrc_element_attr_val_t* p_attrs) {
   2354   tAVRC_RESPONSE avrc_rsp;
   2355   tAVRC_ATTR_ENTRY item_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
   2356   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2357 
   2358   BTIF_TRACE_DEBUG("%s", __func__);
   2359   CHECK_RC_CONNECTED(p_dev);
   2360 
   2361   memset(item_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr);
   2362 
   2363   avrc_rsp.get_attrs.status = status_code_map[rsp_status];
   2364   if (rsp_status == BTRC_STS_NO_ERROR) {
   2365     fill_avrc_attr_entry(item_attrs, num_attr, p_attrs);
   2366   }
   2367 
   2368   avrc_rsp.get_attrs.num_attrs = num_attr;
   2369   avrc_rsp.get_attrs.p_attrs = item_attrs;
   2370   avrc_rsp.get_attrs.pdu = AVRC_PDU_GET_ITEM_ATTRIBUTES;
   2371   avrc_rsp.get_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ITEM_ATTRIBUTES);
   2372 
   2373   /* Send the response. */
   2374   send_metamsg_rsp(p_dev, IDX_GET_ITEM_ATTR_RSP,
   2375                    p_dev->rc_pdu_info[IDX_GET_ITEM_ATTR_RSP].label,
   2376                    p_dev->rc_pdu_info[IDX_GET_ITEM_ATTR_RSP].ctype, &avrc_rsp);
   2377 
   2378   return BT_STATUS_SUCCESS;
   2379 }
   2380 
   2381 /***************************************************************************
   2382  *
   2383  * Function         add_to_now_playing_rsp
   2384  *
   2385  * Description      Response to command for adding speciafied media item
   2386  *                  to Now Playing queue.
   2387  *
   2388  * Returns          bt_status_t
   2389  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2390  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2391  *
   2392  **************************************************************************/
   2393 static bt_status_t add_to_now_playing_rsp(RawAddress* bd_addr,
   2394                                           btrc_status_t rsp_status) {
   2395   tAVRC_RESPONSE avrc_rsp;
   2396   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2397 
   2398   BTIF_TRACE_DEBUG("%s", __func__);
   2399   CHECK_RC_CONNECTED(p_dev);
   2400 
   2401   avrc_rsp.add_to_play.pdu = AVRC_PDU_ADD_TO_NOW_PLAYING;
   2402   avrc_rsp.add_to_play.opcode = opcode_from_pdu(AVRC_PDU_ADD_TO_NOW_PLAYING);
   2403   avrc_rsp.add_to_play.status = status_code_map[rsp_status];
   2404 
   2405   /* Send the response. */
   2406   send_metamsg_rsp(p_dev, IDX_ADD_TO_NOW_PLAYING_RSP,
   2407                    p_dev->rc_pdu_info[IDX_ADD_TO_NOW_PLAYING_RSP].label,
   2408                    p_dev->rc_pdu_info[IDX_ADD_TO_NOW_PLAYING_RSP].ctype,
   2409                    &avrc_rsp);
   2410 
   2411   return BT_STATUS_SUCCESS;
   2412 }
   2413 
   2414 /***************************************************************************
   2415  *
   2416  * Function         play_item_rsp
   2417  *
   2418  * Description      Response to command for playing the specified media item.
   2419  *
   2420  * Returns          bt_status_t
   2421  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2422  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2423  *
   2424  **************************************************************************/
   2425 static bt_status_t play_item_rsp(RawAddress* bd_addr,
   2426                                  btrc_status_t rsp_status) {
   2427   tAVRC_RESPONSE avrc_rsp;
   2428   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2429 
   2430   BTIF_TRACE_DEBUG("%s", __func__);
   2431   CHECK_RC_CONNECTED(p_dev);
   2432 
   2433   avrc_rsp.play_item.pdu = AVRC_PDU_PLAY_ITEM;
   2434   avrc_rsp.play_item.opcode = opcode_from_pdu(AVRC_PDU_PLAY_ITEM);
   2435   avrc_rsp.play_item.status = status_code_map[rsp_status];
   2436 
   2437   /* Send the response. */
   2438   send_metamsg_rsp(p_dev, IDX_PLAY_ITEM_RSP,
   2439                    p_dev->rc_pdu_info[IDX_PLAY_ITEM_RSP].label,
   2440                    p_dev->rc_pdu_info[IDX_PLAY_ITEM_RSP].ctype, &avrc_rsp);
   2441 
   2442   return BT_STATUS_SUCCESS;
   2443 }
   2444 
   2445 /***************************************************************************
   2446  *
   2447  * Function         get_total_num_of_items_rsp
   2448  *
   2449  * Description      response to command to get the Number of Items
   2450  *                  in the selected folder at the selected scope
   2451  *
   2452  * Returns          bt_status_t
   2453  *                      BT_STATUS_NOT_READY - when RC is not connected.
   2454  *                      BT_STATUS_SUCCESS   - always if RC is connected
   2455  *
   2456  **************************************************************************/
   2457 static bt_status_t get_total_num_of_items_rsp(RawAddress* bd_addr,
   2458                                               btrc_status_t rsp_status,
   2459                                               uint32_t uid_counter,
   2460                                               uint32_t num_items) {
   2461   tAVRC_RESPONSE avrc_rsp;
   2462   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   2463 
   2464   BTIF_TRACE_DEBUG("%s", __func__);
   2465   CHECK_RC_CONNECTED(p_dev);
   2466 
   2467   avrc_rsp.get_num_of_items.pdu = AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS;
   2468   avrc_rsp.get_num_of_items.opcode =
   2469       opcode_from_pdu(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS);
   2470   avrc_rsp.get_num_of_items.num_items = num_items;
   2471   avrc_rsp.get_num_of_items.uid_counter = uid_counter;
   2472   avrc_rsp.get_num_of_items.status = status_code_map[rsp_status];
   2473 
   2474   /* Send the response. */
   2475   send_metamsg_rsp(p_dev, IDX_GET_TOTAL_NUM_OF_ITEMS_RSP,
   2476                    p_dev->rc_pdu_info[IDX_GET_TOTAL_NUM_OF_ITEMS_RSP].label,
   2477                    p_dev->rc_pdu_info[IDX_GET_TOTAL_NUM_OF_ITEMS_RSP].ctype,
   2478                    &avrc_rsp);
   2479 
   2480   return BT_STATUS_SUCCESS;
   2481 }
   2482 
   2483 /***************************************************************************
   2484  *
   2485  * Function         set_volume
   2486  *
   2487  * Description      Send current volume setting to remote side.
   2488  *                  Support limited to SetAbsoluteVolume
   2489  *                  This can be enhanced to support Relative Volume (AVRCP 1.0).
   2490  *                  With RelateVolume, we will send VOLUME_UP/VOLUME_DOWN
   2491  *                  as opposed to absolute volume level
   2492  * volume: Should be in the range 0-127. bit7 is reseved and cannot be set
   2493  *
   2494  * Returns          bt_status_t
   2495  *
   2496  **************************************************************************/
   2497 static bt_status_t set_volume(uint8_t volume) {
   2498   BTIF_TRACE_DEBUG("%s: volume: %d", __func__, volume);
   2499   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   2500   rc_transaction_t* p_transaction = NULL;
   2501 
   2502   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   2503     if (!btif_rc_cb.rc_multi_cb[idx].rc_connected) {
   2504       status = BT_STATUS_NOT_READY;
   2505       BTIF_TRACE_ERROR("%s: RC is not connected for device: 0x%x", __func__,
   2506                        btif_rc_cb.rc_multi_cb[idx].rc_addr);
   2507       continue;
   2508     }
   2509 
   2510     if (btif_rc_cb.rc_multi_cb[idx].rc_volume == volume) {
   2511       status = BT_STATUS_DONE;
   2512       BTIF_TRACE_ERROR("%s: volume value already set earlier: 0x%02x", __func__,
   2513                        volume);
   2514       continue;
   2515     }
   2516 
   2517     if ((btif_rc_cb.rc_multi_cb[idx].rc_volume != volume) &&
   2518         btif_rc_cb.rc_multi_cb[idx].rc_state ==
   2519             BTRC_CONNECTION_STATE_CONNECTED) {
   2520       if ((btif_rc_cb.rc_multi_cb[idx].rc_features & BTA_AV_FEAT_RCTG) == 0) {
   2521         status = BT_STATUS_NOT_READY;
   2522         continue;
   2523       } else {
   2524         tAVRC_COMMAND avrc_cmd = {0};
   2525         BT_HDR* p_msg = NULL;
   2526 
   2527         if (btif_rc_cb.rc_multi_cb[idx].rc_features & BTA_AV_FEAT_ADV_CTRL) {
   2528           BTIF_TRACE_DEBUG("%s: Peer supports absolute volume. newVolume: %d",
   2529                            __func__, volume);
   2530           avrc_cmd.volume.opcode = AVRC_OP_VENDOR;
   2531           avrc_cmd.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME;
   2532           avrc_cmd.volume.status = AVRC_STS_NO_ERROR;
   2533           avrc_cmd.volume.volume = volume;
   2534 
   2535           if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) {
   2536             bt_status_t tran_status = get_transaction(&p_transaction);
   2537 
   2538             if (BT_STATUS_SUCCESS == tran_status && NULL != p_transaction) {
   2539               BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d",
   2540                                __func__, p_transaction->lbl);
   2541               BTA_AvMetaCmd(btif_rc_cb.rc_multi_cb[idx].rc_handle,
   2542                             p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
   2543               status = BT_STATUS_SUCCESS;
   2544             } else {
   2545               osi_free_and_reset((void**)&p_msg);
   2546               BTIF_TRACE_ERROR(
   2547                   "%s: failed to obtain transaction details. status: 0x%02x",
   2548                   __func__, tran_status);
   2549               status = BT_STATUS_FAIL;
   2550             }
   2551           } else {
   2552             BTIF_TRACE_ERROR(
   2553                 "%s: failed to build absolute volume command. status: 0x%02x",
   2554                 __func__, status);
   2555             status = BT_STATUS_FAIL;
   2556           }
   2557         }
   2558       }
   2559     }
   2560   }
   2561   return (bt_status_t)status;
   2562 }
   2563 
   2564 /***************************************************************************
   2565  *
   2566  * Function         register_volumechange
   2567  *
   2568  * Description     Register for volume change notification from remote side.
   2569  *
   2570  * Returns          void
   2571  *
   2572  **************************************************************************/
   2573 
   2574 static void register_volumechange(uint8_t lbl, btif_rc_device_cb_t* p_dev) {
   2575   tAVRC_COMMAND avrc_cmd = {0};
   2576   BT_HDR* p_msg = NULL;
   2577   tAVRC_STS BldResp = AVRC_STS_BAD_CMD;
   2578   rc_transaction_t* p_transaction = NULL;
   2579 
   2580   BTIF_TRACE_DEBUG("%s: label: %d", __func__, lbl);
   2581 
   2582   avrc_cmd.cmd.opcode = 0x00;
   2583   avrc_cmd.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
   2584   avrc_cmd.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE;
   2585   avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR;
   2586   avrc_cmd.reg_notif.param = 0;
   2587 
   2588   BldResp = AVRC_BldCommand(&avrc_cmd, &p_msg);
   2589   if (AVRC_STS_NO_ERROR == BldResp && p_msg) {
   2590     p_transaction = get_transaction_by_lbl(lbl);
   2591     if (p_transaction != NULL) {
   2592       BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_NOTIF,
   2593                     p_msg);
   2594       BTIF_TRACE_DEBUG("%s: BTA_AvMetaCmd called", __func__);
   2595     } else {
   2596       osi_free(p_msg);
   2597       BTIF_TRACE_ERROR("%s: transaction not obtained with label: %d", __func__,
   2598                        lbl);
   2599     }
   2600   } else {
   2601     BTIF_TRACE_ERROR("%s: failed to build command: %d", __func__, BldResp);
   2602   }
   2603 }
   2604 
   2605 /***************************************************************************
   2606  *
   2607  * Function         handle_rc_metamsg_rsp
   2608  *
   2609  * Description      Handle RC metamessage response
   2610  *
   2611  * Returns          void
   2612  *
   2613  **************************************************************************/
   2614 static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg,
   2615                                   btif_rc_device_cb_t* p_dev) {
   2616   tAVRC_RESPONSE avrc_response = {0};
   2617   uint8_t scratch_buf[512] = {0};
   2618   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   2619 
   2620   BTIF_TRACE_DEBUG("%s: ", __func__);
   2621 
   2622   if (AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode &&
   2623       (AVRC_RSP_CHANGED == pmeta_msg->code ||
   2624        AVRC_RSP_INTERIM == pmeta_msg->code ||
   2625        AVRC_RSP_ACCEPT == pmeta_msg->code || AVRC_RSP_REJ == pmeta_msg->code ||
   2626        AVRC_RSP_NOT_IMPL == pmeta_msg->code)) {
   2627     status = AVRC_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf,
   2628                                sizeof(scratch_buf));
   2629     BTIF_TRACE_DEBUG(
   2630         "%s: code:%d, event ID: %d, PDU: %x, parsing status: %d, label: %d",
   2631         __func__, pmeta_msg->code, avrc_response.reg_notif.event_id,
   2632         avrc_response.reg_notif.pdu, status, pmeta_msg->label);
   2633 
   2634     if (status != AVRC_STS_NO_ERROR) {
   2635       if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu &&
   2636           AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id &&
   2637           p_dev->rc_vol_label == pmeta_msg->label) {
   2638         p_dev->rc_vol_label = MAX_LABEL;
   2639         release_transaction(p_dev->rc_vol_label);
   2640       } else if (AVRC_PDU_SET_ABSOLUTE_VOLUME == avrc_response.rsp.pdu) {
   2641         release_transaction(pmeta_msg->label);
   2642       }
   2643       return;
   2644     }
   2645 
   2646     if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu &&
   2647         AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id &&
   2648         p_dev->rc_vol_label != pmeta_msg->label) {
   2649       // Just discard the message, if the device sends back with an incorrect
   2650       // label
   2651       BTIF_TRACE_DEBUG(
   2652           "%s: Discarding register notification in rsp.code: %d and label: %d",
   2653           __func__, pmeta_msg->code, pmeta_msg->label);
   2654       return;
   2655     }
   2656 
   2657     if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu &&
   2658         AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id &&
   2659         (AVRC_RSP_REJ == pmeta_msg->code ||
   2660          AVRC_RSP_NOT_IMPL == pmeta_msg->code)) {
   2661       BTIF_TRACE_DEBUG("%s remove AbsoluteVolume feature flag.", __func__);
   2662       p_dev->rc_features &= ~BTA_AV_FEAT_ADV_CTRL;
   2663       handle_rc_features(p_dev);
   2664       return;
   2665     }
   2666   } else {
   2667     BTIF_TRACE_DEBUG(
   2668         "%s: Received vendor dependent in adv ctrl rsp. code: %d len: %d. Not "
   2669         "processing it.",
   2670         __func__, pmeta_msg->code, pmeta_msg->len);
   2671     return;
   2672   }
   2673 
   2674   if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu &&
   2675       AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id &&
   2676       AVRC_RSP_CHANGED == pmeta_msg->code) {
   2677     /* re-register for volume change notification */
   2678     // Do not re-register for rejected case, as it might get into endless loop
   2679     register_volumechange(p_dev->rc_vol_label, p_dev);
   2680   } else if (AVRC_PDU_SET_ABSOLUTE_VOLUME == avrc_response.rsp.pdu) {
   2681     /* free up the label here */
   2682     release_transaction(pmeta_msg->label);
   2683   }
   2684 
   2685   BTIF_TRACE_EVENT("%s: Passing received metamsg response to app. pdu: %s",
   2686                    __func__, dump_rc_pdu(avrc_response.pdu));
   2687   btif_rc_upstreams_rsp_evt((uint16_t)avrc_response.rsp.pdu, &avrc_response,
   2688                             pmeta_msg->code, pmeta_msg->label, p_dev);
   2689 }
   2690 
   2691 /***************************************************************************
   2692  *
   2693  * Function         iterate_supported_event_list_for_interim_rsp
   2694  *
   2695  * Description      iterator callback function to match the event and handle
   2696  *                  timer cleanup
   2697  * Returns          true to continue iterating, false to stop
   2698  *
   2699  **************************************************************************/
   2700 bool iterate_supported_event_list_for_interim_rsp(void* data, void* cb_data) {
   2701   uint8_t* p_event_id;
   2702   btif_rc_supported_event_t* p_event = (btif_rc_supported_event_t*)data;
   2703 
   2704   p_event_id = (uint8_t*)cb_data;
   2705 
   2706   if (p_event->event_id == *p_event_id) {
   2707     p_event->status = eINTERIM;
   2708     return false;
   2709   }
   2710   return true;
   2711 }
   2712 
   2713 /***************************************************************************
   2714  *
   2715  * Function         iterate_supported_event_list_for_timeout
   2716  *
   2717  * Description      Iterator callback function for timeout handling.
   2718  *                  As part of the failure handling, it releases the
   2719  *                  transaction label and removes the event from list,
   2720  *                  this event will not be requested again during
   2721  *                  the lifetime of the connection.
   2722  * Returns          false to stop iterating, true to continue
   2723  *
   2724  **************************************************************************/
   2725 bool iterate_supported_event_list_for_timeout(void* data, void* cb_data) {
   2726   rc_context_t* cntxt = (rc_context_t*)cb_data;
   2727   uint8_t label = cntxt->label & 0xFF;
   2728   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(&cntxt->rc_addr);
   2729   btif_rc_supported_event_t* p_event = (btif_rc_supported_event_t*)data;
   2730 
   2731   if (p_event->label == label) {
   2732     list_remove(p_dev->rc_supported_event_list, p_event);
   2733     return false;
   2734   }
   2735   return true;
   2736 }
   2737 
   2738 /***************************************************************************
   2739  *
   2740  * Function         rc_notification_interim_timout
   2741  *
   2742  * Description      Interim response timeout handler.
   2743  *                  Runs the iterator to check and clear the timed out event.
   2744  *                  Proceeds to register for the unregistered events.
   2745  * Returns          None
   2746  *
   2747  **************************************************************************/
   2748 static void rc_notification_interim_timout(uint8_t label,
   2749                                            btif_rc_device_cb_t* p_dev) {
   2750   list_node_t* node;
   2751   rc_context_t cntxt;
   2752   memset(&cntxt, 0, sizeof(rc_context_t));
   2753   cntxt.label = label;
   2754   cntxt.rc_addr = p_dev->rc_addr;
   2755 
   2756   list_foreach(p_dev->rc_supported_event_list,
   2757                iterate_supported_event_list_for_timeout, &cntxt);
   2758   /* Timeout happened for interim response for the registered event,
   2759    * check if there are any pending for registration
   2760    */
   2761   node = list_begin(p_dev->rc_supported_event_list);
   2762   while (node != NULL) {
   2763     btif_rc_supported_event_t* p_event;
   2764 
   2765     p_event = (btif_rc_supported_event_t*)list_node(node);
   2766     if ((p_event != NULL) && (p_event->status == eNOT_REGISTERED)) {
   2767       register_for_event_notification(p_event, p_dev);
   2768       break;
   2769     }
   2770     node = list_next(node);
   2771   }
   2772   /* Todo. Need to initiate application settings query if this
   2773    * is the last event registration.
   2774    */
   2775 }
   2776 
   2777 /***************************************************************************
   2778  *
   2779  * Function         btif_rc_status_cmd_timeout_handler
   2780  *
   2781  * Description      RC status command timeout handler (Runs in BTIF context).
   2782  * Returns          None
   2783  *
   2784  **************************************************************************/
   2785 static void btif_rc_status_cmd_timeout_handler(UNUSED_ATTR uint16_t event,
   2786                                                char* data) {
   2787   btif_rc_timer_context_t* p_context;
   2788   tAVRC_RESPONSE avrc_response = {0};
   2789   tBTA_AV_META_MSG meta_msg;
   2790   btif_rc_device_cb_t* p_dev = NULL;
   2791 
   2792   p_context = (btif_rc_timer_context_t*)data;
   2793   memset(&meta_msg, 0, sizeof(tBTA_AV_META_MSG));
   2794   p_dev = btif_rc_get_device_by_bda(&p_context->rc_addr);
   2795   if (p_dev == NULL) {
   2796     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   2797     return;
   2798   }
   2799   meta_msg.rc_handle = p_dev->rc_handle;
   2800 
   2801   switch (p_context->rc_status_cmd.pdu_id) {
   2802     case AVRC_PDU_REGISTER_NOTIFICATION:
   2803       rc_notification_interim_timout(p_context->rc_status_cmd.label, p_dev);
   2804       break;
   2805 
   2806     case AVRC_PDU_GET_CAPABILITIES:
   2807       avrc_response.get_caps.status = BTIF_RC_STS_TIMEOUT;
   2808       handle_get_capability_response(&meta_msg, &avrc_response.get_caps);
   2809       break;
   2810 
   2811     case AVRC_PDU_LIST_PLAYER_APP_ATTR:
   2812       avrc_response.list_app_attr.status = BTIF_RC_STS_TIMEOUT;
   2813       handle_app_attr_response(&meta_msg, &avrc_response.list_app_attr);
   2814       break;
   2815 
   2816     case AVRC_PDU_LIST_PLAYER_APP_VALUES:
   2817       avrc_response.list_app_values.status = BTIF_RC_STS_TIMEOUT;
   2818       handle_app_val_response(&meta_msg, &avrc_response.list_app_values);
   2819       break;
   2820 
   2821     case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:
   2822       avrc_response.get_cur_app_val.status = BTIF_RC_STS_TIMEOUT;
   2823       handle_app_cur_val_response(&meta_msg, &avrc_response.get_cur_app_val);
   2824       break;
   2825 
   2826     case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:
   2827       avrc_response.get_app_attr_txt.status = BTIF_RC_STS_TIMEOUT;
   2828       handle_app_attr_txt_response(&meta_msg, &avrc_response.get_app_attr_txt);
   2829       break;
   2830 
   2831     case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:
   2832       avrc_response.get_app_val_txt.status = BTIF_RC_STS_TIMEOUT;
   2833       handle_app_attr_txt_response(&meta_msg, &avrc_response.get_app_val_txt);
   2834       break;
   2835 
   2836     case AVRC_PDU_GET_ELEMENT_ATTR:
   2837       avrc_response.get_attrs.status = BTIF_RC_STS_TIMEOUT;
   2838       handle_get_elem_attr_response(&meta_msg, &avrc_response.get_attrs);
   2839       break;
   2840 
   2841     case AVRC_PDU_GET_PLAY_STATUS:
   2842       avrc_response.get_play_status.status = BTIF_RC_STS_TIMEOUT;
   2843       handle_get_playstatus_response(&meta_msg, &avrc_response.get_play_status);
   2844       break;
   2845   }
   2846   release_transaction(p_context->rc_status_cmd.label);
   2847 }
   2848 
   2849 /***************************************************************************
   2850  *
   2851  * Function         btif_rc_status_cmd_timer_timeout
   2852  *
   2853  * Description      RC status command timeout callback.
   2854  *                  This is called from BTU context and switches to BTIF
   2855  *                  context to handle the timeout events
   2856  * Returns          None
   2857  *
   2858  **************************************************************************/
   2859 static void btif_rc_status_cmd_timer_timeout(void* data) {
   2860   btif_rc_timer_context_t* p_data = (btif_rc_timer_context_t*)data;
   2861 
   2862   btif_transfer_context(btif_rc_status_cmd_timeout_handler, 0, (char*)p_data,
   2863                         sizeof(btif_rc_timer_context_t), NULL);
   2864 }
   2865 
   2866 /***************************************************************************
   2867  *
   2868  * Function         btif_rc_control_cmd_timeout_handler
   2869  *
   2870  * Description      RC control command timeout handler (Runs in BTIF context).
   2871  * Returns          None
   2872  *
   2873  **************************************************************************/
   2874 static void btif_rc_control_cmd_timeout_handler(UNUSED_ATTR uint16_t event,
   2875                                                 char* data) {
   2876   btif_rc_timer_context_t* p_context = (btif_rc_timer_context_t*)data;
   2877   tAVRC_RESPONSE avrc_response = {0};
   2878   tBTA_AV_META_MSG meta_msg;
   2879   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(&p_context->rc_addr);
   2880   if (p_dev == NULL) {
   2881     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   2882     return;
   2883   }
   2884 
   2885   memset(&meta_msg, 0, sizeof(tBTA_AV_META_MSG));
   2886   meta_msg.rc_handle = p_dev->rc_handle;
   2887 
   2888   switch (p_context->rc_control_cmd.pdu_id) {
   2889     case AVRC_PDU_SET_PLAYER_APP_VALUE:
   2890       avrc_response.set_app_val.status = BTIF_RC_STS_TIMEOUT;
   2891       handle_set_app_attr_val_response(&meta_msg, &avrc_response.set_app_val);
   2892       break;
   2893   }
   2894   release_transaction(p_context->rc_control_cmd.label);
   2895 }
   2896 
   2897 /***************************************************************************
   2898  *
   2899  * Function         btif_rc_control_cmd_timer_timeout
   2900  *
   2901  * Description      RC control command timeout callback.
   2902  *                  This is called from BTU context and switches to BTIF
   2903  *                  context to handle the timeout events
   2904  * Returns          None
   2905  *
   2906  **************************************************************************/
   2907 static void btif_rc_control_cmd_timer_timeout(void* data) {
   2908   btif_rc_timer_context_t* p_data = (btif_rc_timer_context_t*)data;
   2909 
   2910   btif_transfer_context(btif_rc_control_cmd_timeout_handler, 0, (char*)p_data,
   2911                         sizeof(btif_rc_timer_context_t), NULL);
   2912 }
   2913 
   2914 /***************************************************************************
   2915  *
   2916  * Function         btif_rc_play_status_timeout_handler
   2917  *
   2918  * Description      RC play status timeout handler (Runs in BTIF context).
   2919  * Returns          None
   2920  *
   2921  **************************************************************************/
   2922 static void btif_rc_play_status_timeout_handler(UNUSED_ATTR uint16_t event,
   2923                                                 char* p_data) {
   2924   btif_rc_handle_t* rc_handle = (btif_rc_handle_t*)p_data;
   2925   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_handle(rc_handle->handle);
   2926   if (p_dev == NULL) {
   2927     BTIF_TRACE_ERROR("%s timeout handler but no device found for handle %d",
   2928                      __func__, rc_handle->handle);
   2929     return;
   2930   }
   2931   get_play_status_cmd(p_dev);
   2932   rc_start_play_status_timer(p_dev);
   2933 }
   2934 
   2935 /***************************************************************************
   2936  *
   2937  * Function         btif_rc_play_status_timer_timeout
   2938  *
   2939  * Description      RC play status timeout callback.
   2940  *                  This is called from BTU context and switches to BTIF
   2941  *                  context to handle the timeout events
   2942  * Returns          None
   2943  *
   2944  **************************************************************************/
   2945 static void btif_rc_play_status_timer_timeout(void* data) {
   2946   btif_rc_handle_t rc_handle;
   2947   rc_handle.handle = PTR_TO_UINT(data);
   2948   BTIF_TRACE_DEBUG("%s called with handle: %d", __func__, rc_handle);
   2949   btif_transfer_context(btif_rc_play_status_timeout_handler, 0,
   2950                         (char*)(&rc_handle), sizeof(btif_rc_handle_t), NULL);
   2951 }
   2952 
   2953 /***************************************************************************
   2954  *
   2955  * Function         rc_start_play_status_timer
   2956  *
   2957  * Description      Helper function to start the timer to fetch play status.
   2958  * Returns          None
   2959  *
   2960  **************************************************************************/
   2961 static void rc_start_play_status_timer(btif_rc_device_cb_t* p_dev) {
   2962   /* Start the Play status timer only if it is not started */
   2963   if (!alarm_is_scheduled(p_dev->rc_play_status_timer)) {
   2964     if (p_dev->rc_play_status_timer == NULL) {
   2965       p_dev->rc_play_status_timer = alarm_new("p_dev->rc_play_status_timer");
   2966     }
   2967     alarm_set_on_mloop(
   2968         p_dev->rc_play_status_timer, BTIF_TIMEOUT_RC_INTERIM_RSP_MS,
   2969         btif_rc_play_status_timer_timeout, UINT_TO_PTR(p_dev->rc_handle));
   2970   }
   2971 }
   2972 
   2973 /***************************************************************************
   2974  *
   2975  * Function         rc_stop_play_status_timer
   2976  *
   2977  * Description      Helper function to stop the play status timer.
   2978  * Returns          None
   2979  *
   2980  **************************************************************************/
   2981 void rc_stop_play_status_timer(btif_rc_device_cb_t* p_dev) {
   2982   alarm_cancel(p_dev->rc_play_status_timer);
   2983 }
   2984 
   2985 /***************************************************************************
   2986  *
   2987  * Function         register_for_event_notification
   2988  *
   2989  * Description      Helper function registering notification events
   2990  *                  sets an interim response timeout to handle if the remote
   2991  *                  does not respond.
   2992  * Returns          None
   2993  *
   2994  **************************************************************************/
   2995 static void register_for_event_notification(btif_rc_supported_event_t* p_event,
   2996                                             btif_rc_device_cb_t* p_dev) {
   2997   rc_transaction_t* p_transaction = NULL;
   2998   bt_status_t status = get_transaction(&p_transaction);
   2999   if (status != BT_STATUS_SUCCESS) {
   3000     BTIF_TRACE_ERROR("%s: no more transaction labels: %d", __func__, status);
   3001     return;
   3002   }
   3003 
   3004   status = register_notification_cmd(p_transaction->lbl, p_event->event_id, 0,
   3005                                      p_dev);
   3006   if (status != BT_STATUS_SUCCESS) {
   3007     BTIF_TRACE_ERROR("%s: Error in Notification registration: %d", __func__,
   3008                      status);
   3009     release_transaction(p_transaction->lbl);
   3010     return;
   3011   }
   3012 
   3013   btif_rc_timer_context_t* p_context = &p_transaction->txn_timer_context;
   3014   p_event->label = p_transaction->lbl;
   3015   p_event->status = eREGISTERED;
   3016   p_context->rc_status_cmd.label = p_transaction->lbl;
   3017   p_context->rc_status_cmd.pdu_id = AVRC_PDU_REGISTER_NOTIFICATION;
   3018   p_context->rc_addr = p_dev->rc_addr;
   3019 
   3020   alarm_free(p_transaction->txn_timer);
   3021   p_transaction->txn_timer = alarm_new("btif_rc.status_command_txn_timer");
   3022   alarm_set_on_mloop(p_transaction->txn_timer, BTIF_TIMEOUT_RC_INTERIM_RSP_MS,
   3023                      btif_rc_status_cmd_timer_timeout, p_context);
   3024 }
   3025 
   3026 static void start_status_command_timer(uint8_t pdu_id, rc_transaction_t* p_txn,
   3027                                        btif_rc_device_cb_t* p_dev) {
   3028   btif_rc_timer_context_t* p_context = &p_txn->txn_timer_context;
   3029   p_context->rc_status_cmd.label = p_txn->lbl;
   3030   p_context->rc_status_cmd.pdu_id = pdu_id;
   3031   p_context->rc_addr = p_dev->rc_addr;
   3032 
   3033   alarm_free(p_txn->txn_timer);
   3034   p_txn->txn_timer = alarm_new("btif_rc.status_command_txn_timer");
   3035   alarm_set_on_mloop(p_txn->txn_timer, BTIF_TIMEOUT_RC_STATUS_CMD_MS,
   3036                      btif_rc_status_cmd_timer_timeout, p_context);
   3037 }
   3038 
   3039 static void start_control_command_timer(uint8_t pdu_id, rc_transaction_t* p_txn,
   3040                                         btif_rc_device_cb_t* p_dev) {
   3041   btif_rc_timer_context_t* p_context = &p_txn->txn_timer_context;
   3042   p_context->rc_control_cmd.label = p_txn->lbl;
   3043   p_context->rc_control_cmd.pdu_id = pdu_id;
   3044   p_context->rc_addr = p_dev->rc_addr;
   3045 
   3046   alarm_free(p_txn->txn_timer);
   3047   p_txn->txn_timer = alarm_new("btif_rc.control_command_txn_timer");
   3048   alarm_set_on_mloop(p_txn->txn_timer, BTIF_TIMEOUT_RC_CONTROL_CMD_MS,
   3049                      btif_rc_control_cmd_timer_timeout, p_context);
   3050 }
   3051 
   3052 bt_status_t build_and_send_vendor_cmd(tAVRC_COMMAND* avrc_cmd,
   3053                                       tBTA_AV_CODE cmd_code,
   3054                                       btif_rc_device_cb_t* p_dev) {
   3055   rc_transaction_t* p_transaction = NULL;
   3056   bt_status_t tran_status = get_transaction(&p_transaction);
   3057   if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL;
   3058 
   3059   BT_HDR* p_msg = NULL;
   3060   tAVRC_STS status = AVRC_BldCommand(avrc_cmd, &p_msg);
   3061   if (status == AVRC_STS_NO_ERROR && p_msg != NULL) {
   3062     uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset;
   3063     BTIF_TRACE_DEBUG("%s: %s msgreq being sent out with label: %d", __func__,
   3064                      dump_rc_pdu(avrc_cmd->pdu), p_transaction->lbl);
   3065     BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, cmd_code, data_start,
   3066                     p_msg->len);
   3067     status = BT_STATUS_SUCCESS;
   3068     if (cmd_code == AVRC_CMD_STATUS) {
   3069       start_status_command_timer(avrc_cmd->pdu, p_transaction, p_dev);
   3070     } else if (cmd_code == AVRC_CMD_CTRL) {
   3071       start_control_command_timer(avrc_cmd->pdu, p_transaction, p_dev);
   3072     }
   3073   } else {
   3074     BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__,
   3075                      status);
   3076   }
   3077   osi_free(p_msg);
   3078   return (bt_status_t)status;
   3079 }
   3080 
   3081 /***************************************************************************
   3082  *
   3083  * Function         handle_get_capability_response
   3084  *
   3085  * Description      Handles the get_cap_response to populate company id info
   3086  *                  and query the supported events.
   3087  *                  Initiates Notification registration for events supported
   3088  * Returns          None
   3089  *
   3090  **************************************************************************/
   3091 static void handle_get_capability_response(tBTA_AV_META_MSG* pmeta_msg,
   3092                                            tAVRC_GET_CAPS_RSP* p_rsp) {
   3093   int xx = 0;
   3094   btif_rc_device_cb_t* p_dev =
   3095       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3096 
   3097   /* Todo: Do we need to retry on command timeout */
   3098   if (p_rsp->status != AVRC_STS_NO_ERROR) {
   3099     BTIF_TRACE_ERROR("%s: Error capability response: 0x%02X", __func__,
   3100                      p_rsp->status);
   3101     return;
   3102   }
   3103 
   3104   if (p_rsp->capability_id == AVRC_CAP_EVENTS_SUPPORTED) {
   3105     btif_rc_supported_event_t* p_event;
   3106 
   3107     /* Todo: Check if list can be active when we hit here */
   3108     p_dev->rc_supported_event_list = list_new(osi_free);
   3109     for (xx = 0; xx < p_rsp->count; xx++) {
   3110       /* Skip registering for Play position change notification */
   3111       if ((p_rsp->param.event_id[xx] == AVRC_EVT_PLAY_STATUS_CHANGE) ||
   3112           (p_rsp->param.event_id[xx] == AVRC_EVT_TRACK_CHANGE) ||
   3113           (p_rsp->param.event_id[xx] == AVRC_EVT_APP_SETTING_CHANGE) ||
   3114           (p_rsp->param.event_id[xx] == AVRC_EVT_UIDS_CHANGE)) {
   3115         p_event = (btif_rc_supported_event_t*)osi_malloc(
   3116             sizeof(btif_rc_supported_event_t));
   3117         p_event->event_id = p_rsp->param.event_id[xx];
   3118         p_event->status = eNOT_REGISTERED;
   3119         list_append(p_dev->rc_supported_event_list, p_event);
   3120       }
   3121     }
   3122     p_event =
   3123         (btif_rc_supported_event_t*)list_front(p_dev->rc_supported_event_list);
   3124     if (p_event != NULL) {
   3125       register_for_event_notification(p_event, p_dev);
   3126     }
   3127   } else if (p_rsp->capability_id == AVRC_CAP_COMPANY_ID) {
   3128     getcapabilities_cmd(AVRC_CAP_EVENTS_SUPPORTED, p_dev);
   3129     BTIF_TRACE_EVENT("%s: AVRC_CAP_COMPANY_ID: ", __func__);
   3130     for (xx = 0; xx < p_rsp->count; xx++) {
   3131       BTIF_TRACE_EVENT("%s: company_id: %d", __func__,
   3132                        p_rsp->param.company_id[xx]);
   3133     }
   3134   }
   3135 }
   3136 
   3137 bool rc_is_track_id_valid(tAVRC_UID uid) {
   3138   tAVRC_UID invalid_uid = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
   3139 
   3140   if (memcmp(uid, invalid_uid, sizeof(tAVRC_UID)) == 0) {
   3141     return false;
   3142   } else {
   3143     return true;
   3144   }
   3145 }
   3146 
   3147 /***************************************************************************
   3148  *
   3149  * Function         handle_notification_response
   3150  *
   3151  * Description      Main handler for notification responses to registered events
   3152  *                  1. Register for unregistered event(in interim response path)
   3153  *                  2. After registering for all supported events, start
   3154  *                     retrieving application settings and values
   3155  *                  3. Reregister for events on getting changed response
   3156  *                  4. Run play status timer for getting position when the
   3157  *                     status changes to playing
   3158  *                  5. Get the Media details when the track change happens
   3159  *                     or track change interim response is received with
   3160  *                     valid track id
   3161  *                  6. HAL callback for play status change and application
   3162  *                     setting change
   3163  * Returns          None
   3164  *
   3165  **************************************************************************/
   3166 static void handle_notification_response(tBTA_AV_META_MSG* pmeta_msg,
   3167                                          tAVRC_REG_NOTIF_RSP* p_rsp) {
   3168   btif_rc_device_cb_t* p_dev =
   3169       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3170   uint32_t attr_list[] = {
   3171       AVRC_MEDIA_ATTR_ID_TITLE,       AVRC_MEDIA_ATTR_ID_ARTIST,
   3172       AVRC_MEDIA_ATTR_ID_ALBUM,       AVRC_MEDIA_ATTR_ID_TRACK_NUM,
   3173       AVRC_MEDIA_ATTR_ID_NUM_TRACKS,  AVRC_MEDIA_ATTR_ID_GENRE,
   3174       AVRC_MEDIA_ATTR_ID_PLAYING_TIME};
   3175 
   3176   if (p_dev == NULL) {
   3177     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3178     return;
   3179   }
   3180 
   3181   RawAddress rc_addr = p_dev->rc_addr;
   3182 
   3183   if (pmeta_msg->code == AVRC_RSP_INTERIM) {
   3184     btif_rc_supported_event_t* p_event;
   3185     list_node_t* node;
   3186 
   3187     BTIF_TRACE_DEBUG("%s: Interim response: 0x%2X ", __func__, p_rsp->event_id);
   3188     switch (p_rsp->event_id) {
   3189       case AVRC_EVT_PLAY_STATUS_CHANGE:
   3190         /* Start timer to get play status periodically
   3191          * if the play state is playing.
   3192          */
   3193         if (p_rsp->param.play_status == AVRC_PLAYSTATE_PLAYING) {
   3194           rc_start_play_status_timer(p_dev);
   3195         }
   3196         HAL_CBACK(bt_rc_ctrl_callbacks, play_status_changed_cb, &rc_addr,
   3197                   (btrc_play_status_t)p_rsp->param.play_status);
   3198         break;
   3199 
   3200       case AVRC_EVT_TRACK_CHANGE:
   3201         if (rc_is_track_id_valid(p_rsp->param.track) != true) {
   3202           break;
   3203         } else {
   3204           uint8_t* p_data = p_rsp->param.track;
   3205           /* Update the UID for current track
   3206            * Attributes will be fetched after the AVRCP procedure
   3207            */
   3208           BE_STREAM_TO_UINT64(p_dev->rc_playing_uid, p_data);
   3209         }
   3210         break;
   3211 
   3212       case AVRC_EVT_APP_SETTING_CHANGE:
   3213         break;
   3214 
   3215       case AVRC_EVT_NOW_PLAYING_CHANGE:
   3216         break;
   3217 
   3218       case AVRC_EVT_AVAL_PLAYERS_CHANGE:
   3219         break;
   3220 
   3221       case AVRC_EVT_ADDR_PLAYER_CHANGE:
   3222         break;
   3223 
   3224       case AVRC_EVT_UIDS_CHANGE:
   3225         break;
   3226 
   3227       case AVRC_EVT_TRACK_REACHED_END:
   3228       case AVRC_EVT_TRACK_REACHED_START:
   3229       case AVRC_EVT_PLAY_POS_CHANGED:
   3230       case AVRC_EVT_BATTERY_STATUS_CHANGE:
   3231       case AVRC_EVT_SYSTEM_STATUS_CHANGE:
   3232       default:
   3233         BTIF_TRACE_ERROR("%s: Unhandled interim response: 0x%2X", __func__,
   3234                          p_rsp->event_id);
   3235         return;
   3236     }
   3237 
   3238     list_foreach(p_dev->rc_supported_event_list,
   3239                  iterate_supported_event_list_for_interim_rsp,
   3240                  &p_rsp->event_id);
   3241 
   3242     node = list_begin(p_dev->rc_supported_event_list);
   3243 
   3244     while (node != NULL) {
   3245       p_event = (btif_rc_supported_event_t*)list_node(node);
   3246       if ((p_event != NULL) && (p_event->status == eNOT_REGISTERED)) {
   3247         register_for_event_notification(p_event, p_dev);
   3248         break;
   3249       }
   3250       node = list_next(node);
   3251       p_event = NULL;
   3252     }
   3253     /* Registered for all events, we can request application settings */
   3254     if (p_event == NULL && p_dev->rc_app_settings.query_started == false) {
   3255       /* we need to do this only if remote TG supports
   3256        * player application settings
   3257        */
   3258       p_dev->rc_app_settings.query_started = true;
   3259       if (p_dev->rc_features & BTA_AV_FEAT_APP_SETTING) {
   3260         list_player_app_setting_attrib_cmd(p_dev);
   3261       } else {
   3262         BTIF_TRACE_DEBUG("%s: App setting not supported, complete procedure",
   3263                          __func__);
   3264         rc_ctrl_procedure_complete(p_dev);
   3265       }
   3266     }
   3267   } else if (pmeta_msg->code == AVRC_RSP_CHANGED) {
   3268     btif_rc_supported_event_t* p_event;
   3269     list_node_t* node;
   3270 
   3271     BTIF_TRACE_DEBUG("%s: Notification completed: 0x%2X ", __func__,
   3272                      p_rsp->event_id);
   3273 
   3274     node = list_begin(p_dev->rc_supported_event_list);
   3275 
   3276     while (node != NULL) {
   3277       p_event = (btif_rc_supported_event_t*)list_node(node);
   3278       if (p_event != NULL && p_event->event_id == p_rsp->event_id) {
   3279         p_event->status = eNOT_REGISTERED;
   3280         register_for_event_notification(p_event, p_dev);
   3281         break;
   3282       }
   3283       node = list_next(node);
   3284     }
   3285 
   3286     switch (p_rsp->event_id) {
   3287       case AVRC_EVT_PLAY_STATUS_CHANGE:
   3288         /* Start timer to get play status periodically
   3289          * if the play state is playing.
   3290          */
   3291         if (p_rsp->param.play_status == AVRC_PLAYSTATE_PLAYING) {
   3292           rc_start_play_status_timer(p_dev);
   3293           get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list,
   3294                                     p_dev);
   3295         } else {
   3296           rc_stop_play_status_timer(p_dev);
   3297         }
   3298         HAL_CBACK(bt_rc_ctrl_callbacks, play_status_changed_cb, &rc_addr,
   3299                   (btrc_play_status_t)p_rsp->param.play_status);
   3300 
   3301         break;
   3302 
   3303       case AVRC_EVT_TRACK_CHANGE:
   3304         if (rc_is_track_id_valid(p_rsp->param.track) != true) {
   3305           break;
   3306         }
   3307         get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev);
   3308         break;
   3309 
   3310       case AVRC_EVT_APP_SETTING_CHANGE: {
   3311         btrc_player_settings_t app_settings;
   3312         uint16_t xx;
   3313 
   3314         app_settings.num_attr = p_rsp->param.player_setting.num_attr;
   3315         for (xx = 0; xx < app_settings.num_attr; xx++) {
   3316           app_settings.attr_ids[xx] = p_rsp->param.player_setting.attr_id[xx];
   3317           app_settings.attr_values[xx] =
   3318               p_rsp->param.player_setting.attr_value[xx];
   3319         }
   3320         HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_changed_cb,
   3321                   &rc_addr, &app_settings);
   3322       } break;
   3323 
   3324       case AVRC_EVT_NOW_PLAYING_CHANGE:
   3325         break;
   3326 
   3327       case AVRC_EVT_AVAL_PLAYERS_CHANGE:
   3328         break;
   3329 
   3330       case AVRC_EVT_ADDR_PLAYER_CHANGE:
   3331         break;
   3332 
   3333       case AVRC_EVT_UIDS_CHANGE:
   3334         break;
   3335 
   3336       case AVRC_EVT_TRACK_REACHED_END:
   3337       case AVRC_EVT_TRACK_REACHED_START:
   3338       case AVRC_EVT_PLAY_POS_CHANGED:
   3339       case AVRC_EVT_BATTERY_STATUS_CHANGE:
   3340       case AVRC_EVT_SYSTEM_STATUS_CHANGE:
   3341       default:
   3342         BTIF_TRACE_ERROR("%s: Unhandled completion response: 0x%2X", __func__,
   3343                          p_rsp->event_id);
   3344         return;
   3345     }
   3346   }
   3347 }
   3348 
   3349 /***************************************************************************
   3350  *
   3351  * Function         handle_app_attr_response
   3352  *
   3353  * Description      handles the the application attributes response and
   3354  *                  initiates procedure to fetch the attribute values
   3355  * Returns          None
   3356  *
   3357  **************************************************************************/
   3358 static void handle_app_attr_response(tBTA_AV_META_MSG* pmeta_msg,
   3359                                      tAVRC_LIST_APP_ATTR_RSP* p_rsp) {
   3360   uint8_t xx;
   3361   btif_rc_device_cb_t* p_dev =
   3362       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3363 
   3364   if (p_dev == NULL || p_rsp->status != AVRC_STS_NO_ERROR) {
   3365     BTIF_TRACE_ERROR("%s: Error getting Player application settings: 0x%2X",
   3366                      __func__, p_rsp->status);
   3367     rc_ctrl_procedure_complete(p_dev);
   3368     return;
   3369   }
   3370 
   3371   for (xx = 0; xx < p_rsp->num_attr; xx++) {
   3372     uint8_t st_index;
   3373 
   3374     if (p_rsp->attrs[xx] > AVRC_PLAYER_SETTING_LOW_MENU_EXT) {
   3375       st_index = p_dev->rc_app_settings.num_ext_attrs;
   3376       p_dev->rc_app_settings.ext_attrs[st_index].attr_id = p_rsp->attrs[xx];
   3377       p_dev->rc_app_settings.num_ext_attrs++;
   3378     } else {
   3379       st_index = p_dev->rc_app_settings.num_attrs;
   3380       p_dev->rc_app_settings.attrs[st_index].attr_id = p_rsp->attrs[xx];
   3381       p_dev->rc_app_settings.num_attrs++;
   3382     }
   3383   }
   3384   p_dev->rc_app_settings.attr_index = 0;
   3385   p_dev->rc_app_settings.ext_attr_index = 0;
   3386   p_dev->rc_app_settings.ext_val_index = 0;
   3387   if (p_rsp->num_attr) {
   3388     list_player_app_setting_value_cmd(p_dev->rc_app_settings.attrs[0].attr_id,
   3389                                       p_dev);
   3390   } else {
   3391     BTIF_TRACE_ERROR("%s: No Player application settings found", __func__);
   3392   }
   3393 }
   3394 
   3395 /***************************************************************************
   3396  *
   3397  * Function         handle_app_val_response
   3398  *
   3399  * Description      handles the the attributes value response and if extended
   3400  *                  menu is available, it initiates query for the attribute
   3401  *                  text. If not, it initiates procedure to get the current
   3402  *                  attribute values and calls the HAL callback for provding
   3403  *                  application settings information.
   3404  * Returns          None
   3405  *
   3406  **************************************************************************/
   3407 static void handle_app_val_response(tBTA_AV_META_MSG* pmeta_msg,
   3408                                     tAVRC_LIST_APP_VALUES_RSP* p_rsp) {
   3409   uint8_t xx, attr_index;
   3410   uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE];
   3411   btif_rc_player_app_settings_t* p_app_settings;
   3412   btif_rc_device_cb_t* p_dev =
   3413       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3414 
   3415   /* Todo: Do we need to retry on command timeout */
   3416   if (p_dev == NULL || p_rsp->status != AVRC_STS_NO_ERROR) {
   3417     BTIF_TRACE_ERROR("%s: Error fetching attribute values: 0x%02X", __func__,
   3418                      p_rsp->status);
   3419     return;
   3420   }
   3421 
   3422   p_app_settings = &p_dev->rc_app_settings;
   3423   RawAddress rc_addr = p_dev->rc_addr;
   3424 
   3425   if (p_app_settings->attr_index < p_app_settings->num_attrs) {
   3426     attr_index = p_app_settings->attr_index;
   3427     p_app_settings->attrs[attr_index].num_val = p_rsp->num_val;
   3428     for (xx = 0; xx < p_rsp->num_val; xx++) {
   3429       p_app_settings->attrs[attr_index].attr_val[xx] = p_rsp->vals[xx];
   3430     }
   3431     attr_index++;
   3432     p_app_settings->attr_index++;
   3433     if (attr_index < p_app_settings->num_attrs) {
   3434       list_player_app_setting_value_cmd(
   3435           p_app_settings->attrs[p_app_settings->attr_index].attr_id, p_dev);
   3436     } else if (p_app_settings->ext_attr_index < p_app_settings->num_ext_attrs) {
   3437       attr_index = 0;
   3438       p_app_settings->ext_attr_index = 0;
   3439       list_player_app_setting_value_cmd(
   3440           p_app_settings->ext_attrs[attr_index].attr_id, p_dev);
   3441     } else {
   3442       for (xx = 0; xx < p_app_settings->num_attrs; xx++) {
   3443         attrs[xx] = p_app_settings->attrs[xx].attr_id;
   3444       }
   3445       get_player_app_setting_cmd(p_app_settings->num_attrs, attrs, p_dev);
   3446       HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr,
   3447                 p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL);
   3448     }
   3449   } else if (p_app_settings->ext_attr_index < p_app_settings->num_ext_attrs) {
   3450     attr_index = p_app_settings->ext_attr_index;
   3451     p_app_settings->ext_attrs[attr_index].num_val = p_rsp->num_val;
   3452     for (xx = 0; xx < p_rsp->num_val; xx++) {
   3453       p_app_settings->ext_attrs[attr_index].ext_attr_val[xx].val =
   3454           p_rsp->vals[xx];
   3455     }
   3456     attr_index++;
   3457     p_app_settings->ext_attr_index++;
   3458     if (attr_index < p_app_settings->num_ext_attrs) {
   3459       list_player_app_setting_value_cmd(
   3460           p_app_settings->ext_attrs[p_app_settings->ext_attr_index].attr_id,
   3461           p_dev);
   3462     } else {
   3463       uint8_t attr[AVRC_MAX_APP_ATTR_SIZE];
   3464 
   3465       for (uint8_t xx = 0; xx < p_app_settings->num_ext_attrs; xx++) {
   3466         attr[xx] = p_app_settings->ext_attrs[xx].attr_id;
   3467       }
   3468       get_player_app_setting_attr_text_cmd(attr, p_app_settings->num_ext_attrs,
   3469                                            p_dev);
   3470     }
   3471   }
   3472 }
   3473 
   3474 /***************************************************************************
   3475  *
   3476  * Function         handle_app_cur_val_response
   3477  *
   3478  * Description      handles the the get attributes value response.
   3479  *
   3480  * Returns          None
   3481  *
   3482  **************************************************************************/
   3483 static void handle_app_cur_val_response(tBTA_AV_META_MSG* pmeta_msg,
   3484                                         tAVRC_GET_CUR_APP_VALUE_RSP* p_rsp) {
   3485   btrc_player_settings_t app_settings;
   3486   uint16_t xx;
   3487   btif_rc_device_cb_t* p_dev = NULL;
   3488 
   3489   /* Todo: Do we need to retry on command timeout */
   3490   if (p_rsp->status != AVRC_STS_NO_ERROR) {
   3491     BTIF_TRACE_ERROR("%s: Error fetching current settings: 0x%02X", __func__,
   3492                      p_rsp->status);
   3493     return;
   3494   }
   3495   p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3496   if (p_dev == NULL) {
   3497     BTIF_TRACE_ERROR("%s: Error in getting Device Address", __func__);
   3498     osi_free_and_reset((void**)&p_rsp->p_vals);
   3499     return;
   3500   }
   3501 
   3502   RawAddress rc_addr = p_dev->rc_addr;
   3503 
   3504   app_settings.num_attr = p_rsp->num_val;
   3505   for (xx = 0; xx < app_settings.num_attr; xx++) {
   3506     app_settings.attr_ids[xx] = p_rsp->p_vals[xx].attr_id;
   3507     app_settings.attr_values[xx] = p_rsp->p_vals[xx].attr_val;
   3508   }
   3509 
   3510   HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_changed_cb, &rc_addr,
   3511             &app_settings);
   3512   /* Application settings are fetched only once for initial values
   3513    * initiate anything that follows after RC procedure.
   3514    * Defer it if browsing is supported till players query
   3515    */
   3516   rc_ctrl_procedure_complete(p_dev);
   3517   osi_free_and_reset((void**)&p_rsp->p_vals);
   3518 }
   3519 
   3520 /***************************************************************************
   3521  *
   3522  * Function         handle_app_attr_txt_response
   3523  *
   3524  * Description      handles the the get attributes text response, if fails
   3525  *                  calls HAL callback with just normal settings and initiates
   3526  *                  query for current settings else initiates query for value
   3527  *                  text
   3528  * Returns          None
   3529  *
   3530  **************************************************************************/
   3531 static void handle_app_attr_txt_response(tBTA_AV_META_MSG* pmeta_msg,
   3532                                          tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp) {
   3533   uint8_t xx;
   3534   uint8_t vals[AVRC_MAX_APP_ATTR_SIZE];
   3535   btif_rc_player_app_settings_t* p_app_settings;
   3536   btif_rc_device_cb_t* p_dev =
   3537       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3538 
   3539   if (p_dev == NULL) {
   3540     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3541     return;
   3542   }
   3543 
   3544   RawAddress rc_addr = p_dev->rc_addr;
   3545   p_app_settings = &p_dev->rc_app_settings;
   3546 
   3547   /* Todo: Do we need to retry on command timeout */
   3548   if (p_rsp->status != AVRC_STS_NO_ERROR) {
   3549     uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE];
   3550 
   3551     BTIF_TRACE_ERROR("%s: Error fetching attribute text: 0x%02X", __func__,
   3552                      p_rsp->status);
   3553     /* Not able to fetch Text for extended Menu, skip the process
   3554      * and cleanup used memory. Proceed to get the current settings
   3555      * for standard attributes.
   3556      */
   3557     p_app_settings->num_ext_attrs = 0;
   3558     for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) {
   3559       osi_free_and_reset((void**)&p_app_settings->ext_attrs[xx].p_str);
   3560     }
   3561     p_app_settings->ext_attr_index = 0;
   3562 
   3563     if (p_dev) {
   3564       for (xx = 0; xx < p_app_settings->num_attrs; xx++) {
   3565         attrs[xx] = p_app_settings->attrs[xx].attr_id;
   3566       }
   3567 
   3568       HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr,
   3569                 p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL);
   3570       get_player_app_setting_cmd(xx, attrs, p_dev);
   3571     }
   3572     return;
   3573   }
   3574 
   3575   for (xx = 0; xx < p_rsp->num_attr; xx++) {
   3576     uint8_t x;
   3577     for (x = 0; x < p_app_settings->num_ext_attrs; x++) {
   3578       if (p_app_settings->ext_attrs[x].attr_id == p_rsp->p_attrs[xx].attr_id) {
   3579         p_app_settings->ext_attrs[x].charset_id = p_rsp->p_attrs[xx].charset_id;
   3580         p_app_settings->ext_attrs[x].str_len = p_rsp->p_attrs[xx].str_len;
   3581         p_app_settings->ext_attrs[x].p_str = p_rsp->p_attrs[xx].p_str;
   3582         break;
   3583       }
   3584     }
   3585   }
   3586 
   3587   for (xx = 0; xx < p_app_settings->ext_attrs[0].num_val; xx++) {
   3588     vals[xx] = p_app_settings->ext_attrs[0].ext_attr_val[xx].val;
   3589   }
   3590   get_player_app_setting_value_text_cmd(vals, xx, p_dev);
   3591 }
   3592 
   3593 /***************************************************************************
   3594  *
   3595  * Function         handle_app_attr_val_txt_response
   3596  *
   3597  * Description      handles the the get attributes value text response, if fails
   3598  *                  calls HAL callback with just normal settings and initiates
   3599  *                  query for current settings
   3600  * Returns          None
   3601  *
   3602  **************************************************************************/
   3603 static void handle_app_attr_val_txt_response(
   3604     tBTA_AV_META_MSG* pmeta_msg, tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp) {
   3605   uint8_t xx, attr_index;
   3606   uint8_t vals[AVRC_MAX_APP_ATTR_SIZE];
   3607   uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE];
   3608   btif_rc_player_app_settings_t* p_app_settings;
   3609   btif_rc_device_cb_t* p_dev =
   3610       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3611 
   3612   if (p_dev == NULL) {
   3613     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3614     return;
   3615   }
   3616 
   3617   RawAddress rc_addr = p_dev->rc_addr;
   3618   p_app_settings = &p_dev->rc_app_settings;
   3619 
   3620   /* Todo: Do we need to retry on command timeout */
   3621   if (p_rsp->status != AVRC_STS_NO_ERROR) {
   3622     uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE];
   3623 
   3624     BTIF_TRACE_ERROR("%s: Error fetching attribute value text: 0x%02X",
   3625                      __func__, p_rsp->status);
   3626 
   3627     /* Not able to fetch Text for extended Menu, skip the process
   3628      * and cleanup used memory. Proceed to get the current settings
   3629      * for standard attributes.
   3630      */
   3631     p_app_settings->num_ext_attrs = 0;
   3632     for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) {
   3633       int x;
   3634       btrc_player_app_ext_attr_t* p_ext_attr = &p_app_settings->ext_attrs[xx];
   3635 
   3636       for (x = 0; x < p_ext_attr->num_val; x++)
   3637         osi_free_and_reset((void**)&p_ext_attr->ext_attr_val[x].p_str);
   3638       p_ext_attr->num_val = 0;
   3639       osi_free_and_reset((void**)&p_app_settings->ext_attrs[xx].p_str);
   3640     }
   3641     p_app_settings->ext_attr_index = 0;
   3642 
   3643     for (xx = 0; xx < p_app_settings->num_attrs; xx++) {
   3644       attrs[xx] = p_app_settings->attrs[xx].attr_id;
   3645     }
   3646     HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr,
   3647               p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL);
   3648 
   3649     get_player_app_setting_cmd(xx, attrs, p_dev);
   3650     return;
   3651   }
   3652 
   3653   for (xx = 0; xx < p_rsp->num_attr; xx++) {
   3654     uint8_t x;
   3655     btrc_player_app_ext_attr_t* p_ext_attr;
   3656     p_ext_attr = &p_app_settings->ext_attrs[p_app_settings->ext_val_index];
   3657     for (x = 0; x < p_rsp->num_attr; x++) {
   3658       if (p_ext_attr->ext_attr_val[x].val == p_rsp->p_attrs[xx].attr_id) {
   3659         p_ext_attr->ext_attr_val[x].charset_id = p_rsp->p_attrs[xx].charset_id;
   3660         p_ext_attr->ext_attr_val[x].str_len = p_rsp->p_attrs[xx].str_len;
   3661         p_ext_attr->ext_attr_val[x].p_str = p_rsp->p_attrs[xx].p_str;
   3662         break;
   3663       }
   3664     }
   3665   }
   3666   p_app_settings->ext_val_index++;
   3667 
   3668   if (p_app_settings->ext_val_index < p_app_settings->num_ext_attrs) {
   3669     attr_index = p_app_settings->ext_val_index;
   3670     for (xx = 0; xx < p_app_settings->ext_attrs[attr_index].num_val; xx++) {
   3671       vals[xx] = p_app_settings->ext_attrs[attr_index].ext_attr_val[xx].val;
   3672     }
   3673     get_player_app_setting_value_text_cmd(vals, xx, p_dev);
   3674   } else {
   3675     uint8_t x;
   3676 
   3677     for (xx = 0; xx < p_app_settings->num_attrs; xx++) {
   3678       attrs[xx] = p_app_settings->attrs[xx].attr_id;
   3679     }
   3680     for (x = 0; x < p_app_settings->num_ext_attrs; x++) {
   3681       attrs[xx + x] = p_app_settings->ext_attrs[x].attr_id;
   3682     }
   3683     HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr,
   3684               p_app_settings->num_attrs, p_app_settings->attrs,
   3685               p_app_settings->num_ext_attrs, p_app_settings->ext_attrs);
   3686     get_player_app_setting_cmd(xx + x, attrs, p_dev);
   3687 
   3688     /* Free the application settings information after sending to
   3689      * application.
   3690      */
   3691     for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) {
   3692       int x;
   3693       btrc_player_app_ext_attr_t* p_ext_attr = &p_app_settings->ext_attrs[xx];
   3694 
   3695       for (x = 0; x < p_ext_attr->num_val; x++)
   3696         osi_free_and_reset((void**)&p_ext_attr->ext_attr_val[x].p_str);
   3697       p_ext_attr->num_val = 0;
   3698       osi_free_and_reset((void**)&p_app_settings->ext_attrs[xx].p_str);
   3699     }
   3700     p_app_settings->num_attrs = 0;
   3701   }
   3702 }
   3703 
   3704 /***************************************************************************
   3705  *
   3706  * Function         handle_set_app_attr_val_response
   3707  *
   3708  * Description      handles the the set attributes value response, if fails
   3709  *                  calls HAL callback to indicate the failure
   3710  * Returns          None
   3711  *
   3712  **************************************************************************/
   3713 static void handle_set_app_attr_val_response(tBTA_AV_META_MSG* pmeta_msg,
   3714                                              tAVRC_RSP* p_rsp) {
   3715   uint8_t accepted = 0;
   3716   btif_rc_device_cb_t* p_dev =
   3717       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3718 
   3719   if (p_dev == NULL) {
   3720     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3721     return;
   3722   }
   3723 
   3724   RawAddress rc_addr = p_dev->rc_addr;
   3725 
   3726   /* For timeout pmeta_msg will be NULL, else we need to
   3727    * check if this is accepted by TG
   3728    */
   3729   if (pmeta_msg && (pmeta_msg->code == AVRC_RSP_ACCEPT)) {
   3730     accepted = 1;
   3731   }
   3732   HAL_CBACK(bt_rc_ctrl_callbacks, setplayerappsetting_rsp_cb, &rc_addr,
   3733             accepted);
   3734 }
   3735 
   3736 /***************************************************************************
   3737  *
   3738  * Function         handle_get_elem_attr_response
   3739  *
   3740  * Description      handles the the element attributes response, calls
   3741  *                  HAL callback to update track change information.
   3742  * Returns          None
   3743  *
   3744  **************************************************************************/
   3745 static void handle_get_elem_attr_response(tBTA_AV_META_MSG* pmeta_msg,
   3746                                           tAVRC_GET_ATTRS_RSP* p_rsp) {
   3747   btif_rc_device_cb_t* p_dev =
   3748       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3749 
   3750   if (p_rsp->status == AVRC_STS_NO_ERROR) {
   3751     size_t buf_size = p_rsp->num_attrs * sizeof(btrc_element_attr_val_t);
   3752     btrc_element_attr_val_t* p_attr =
   3753         (btrc_element_attr_val_t*)osi_calloc(buf_size);
   3754 
   3755     if (p_dev == NULL) {
   3756       BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3757       return;
   3758     }
   3759 
   3760     RawAddress rc_addr = p_dev->rc_addr;
   3761 
   3762     for (int i = 0; i < p_rsp->num_attrs; i++) {
   3763       p_attr[i].attr_id = p_rsp->p_attrs[i].attr_id;
   3764       /* Todo. Legth limit check to include null */
   3765       if (p_rsp->p_attrs[i].name.str_len && p_rsp->p_attrs[i].name.p_str) {
   3766         memcpy(p_attr[i].text, p_rsp->p_attrs[i].name.p_str,
   3767                p_rsp->p_attrs[i].name.str_len);
   3768         osi_free_and_reset((void**)&p_rsp->p_attrs[i].name.p_str);
   3769       }
   3770     }
   3771     HAL_CBACK(bt_rc_ctrl_callbacks, track_changed_cb, &rc_addr,
   3772               p_rsp->num_attrs, p_attr);
   3773     osi_free(p_attr);
   3774   } else if (p_rsp->status == BTIF_RC_STS_TIMEOUT) {
   3775     /* Retry for timeout case, this covers error handling
   3776      * for continuation failure also.
   3777      */
   3778     uint32_t attr_list[] = {
   3779         AVRC_MEDIA_ATTR_ID_TITLE,       AVRC_MEDIA_ATTR_ID_ARTIST,
   3780         AVRC_MEDIA_ATTR_ID_ALBUM,       AVRC_MEDIA_ATTR_ID_TRACK_NUM,
   3781         AVRC_MEDIA_ATTR_ID_NUM_TRACKS,  AVRC_MEDIA_ATTR_ID_GENRE,
   3782         AVRC_MEDIA_ATTR_ID_PLAYING_TIME};
   3783     get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev);
   3784   } else {
   3785     BTIF_TRACE_ERROR("%s: Error in get element attr procedure: %d", __func__,
   3786                      p_rsp->status);
   3787   }
   3788 }
   3789 
   3790 /***************************************************************************
   3791  *
   3792  * Function         handle_get_playstatus_response
   3793  *
   3794  * Description      handles the the play status response, calls
   3795  *                  HAL callback to update play position.
   3796  * Returns          None
   3797  *
   3798  **************************************************************************/
   3799 static void handle_get_playstatus_response(tBTA_AV_META_MSG* pmeta_msg,
   3800                                            tAVRC_GET_PLAY_STATUS_RSP* p_rsp) {
   3801 
   3802   btif_rc_device_cb_t* p_dev =
   3803       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3804 
   3805   if (p_dev == NULL) {
   3806     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3807     return;
   3808   }
   3809 
   3810   RawAddress rc_addr = p_dev->rc_addr;
   3811 
   3812   if (p_rsp->status == AVRC_STS_NO_ERROR) {
   3813     HAL_CBACK(bt_rc_ctrl_callbacks, play_position_changed_cb, &rc_addr,
   3814               p_rsp->song_len, p_rsp->song_pos);
   3815   } else {
   3816     BTIF_TRACE_ERROR("%s: Error in get play status procedure: %d", __func__,
   3817                      p_rsp->status);
   3818   }
   3819 }
   3820 
   3821 /***************************************************************************
   3822  *
   3823  * Function         handle_set_addressed_player_response
   3824  *
   3825  * Description      handles the the set addressed player response, calls
   3826  *                  HAL callback
   3827  * Returns          None
   3828  *
   3829  **************************************************************************/
   3830 static void handle_set_addressed_player_response(tBTA_AV_META_MSG* pmeta_msg,
   3831                                                  tAVRC_RSP* p_rsp) {
   3832 
   3833   btif_rc_device_cb_t* p_dev =
   3834       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3835 
   3836   if (p_dev == NULL) {
   3837     BTIF_TRACE_ERROR("%s: p_dev NULL", __func__);
   3838     return;
   3839   }
   3840 
   3841   RawAddress rc_addr = p_dev->rc_addr;
   3842 
   3843   if (p_rsp->status == AVRC_STS_NO_ERROR) {
   3844     HAL_CBACK(bt_rc_ctrl_callbacks, set_addressed_player_cb, &rc_addr,
   3845               p_rsp->status);
   3846   } else {
   3847     BTIF_TRACE_ERROR("%s: Error in get play status procedure %d", __func__,
   3848                      p_rsp->status);
   3849   }
   3850 }
   3851 
   3852 /***************************************************************************
   3853  *
   3854  * Function         handle_get_folder_items_response
   3855  *
   3856  * Description      handles the the get folder items response, calls
   3857  *                  HAL callback to send the folder items.
   3858  * Returns          None
   3859  *
   3860  **************************************************************************/
   3861 static void handle_get_folder_items_response(tBTA_AV_META_MSG* pmeta_msg,
   3862                                              tAVRC_GET_ITEMS_RSP* p_rsp) {
   3863   btif_rc_device_cb_t* p_dev =
   3864       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   3865   RawAddress rc_addr = p_dev->rc_addr;
   3866 
   3867   if (p_rsp->status == AVRC_STS_NO_ERROR) {
   3868     /* Convert the internal folder listing into a response that can
   3869      * be passed onto JNI via HAL_CBACK
   3870      */
   3871     uint8_t item_count = p_rsp->item_count;
   3872     btrc_folder_items_t* btrc_items = (btrc_folder_items_t*)osi_malloc(
   3873         sizeof(btrc_folder_items_t) * item_count);
   3874     for (uint8_t i = 0; i < item_count; i++) {
   3875       const tAVRC_ITEM* avrc_item = &(p_rsp->p_item_list[i]);
   3876       btrc_folder_items_t* btrc_item = &(btrc_items[i]);
   3877       BTIF_TRACE_DEBUG("%s folder item type %d", __func__,
   3878                        avrc_item->item_type);
   3879       switch (avrc_item->item_type) {
   3880         case AVRC_ITEM_MEDIA:
   3881           BTIF_TRACE_DEBUG("%s setting type to %d", __func__, BTRC_ITEM_MEDIA);
   3882           get_folder_item_type_media(avrc_item, btrc_item);
   3883           break;
   3884 
   3885         case AVRC_ITEM_FOLDER:
   3886           BTIF_TRACE_DEBUG("%s setting type to BTRC_ITEM_FOLDER", __func__);
   3887           get_folder_item_type_folder(avrc_item, btrc_item);
   3888           break;
   3889 
   3890         case AVRC_ITEM_PLAYER:
   3891           BTIF_TRACE_DEBUG("%s setting type to BTRC_ITEM_PLAYER", __func__);
   3892           get_folder_item_type_player(avrc_item, btrc_item);
   3893           break;
   3894 
   3895         default:
   3896           BTIF_TRACE_ERROR("%s cannot understand folder item type %d", __func__,
   3897                            avrc_item->item_type);
   3898       }
   3899     }
   3900 
   3901     HAL_CBACK(bt_rc_ctrl_callbacks, get_folder_items_cb, &rc_addr,
   3902               BTRC_STS_NO_ERROR,
   3903               /* We want to make the ownership explicit in native */
   3904               (const btrc_folder_items_t*)btrc_items, item_count);
   3905     BTIF_TRACE_DEBUG("%s HAL CBACK get_folder_items_cb finished", __func__);
   3906 
   3907     /* Release the memory block for items since we OWN the object */
   3908     osi_free(btrc_items);
   3909   } else {
   3910     BTIF_TRACE_ERROR("%s: Error %d", __func__, p_rsp->status);
   3911     HAL_CBACK(bt_rc_ctrl_callbacks, get_folder_items_cb, &rc_addr,
   3912               (btrc_status_t)p_rsp->status, NULL, 0);
   3913   }
   3914 }
   3915 
   3916 /***************************************************************************
   3917  *
   3918  * Function         get_folder_item_type_media
   3919  *
   3920  * Description      Converts the AVRC representation of a folder item with
   3921  *                  TYPE media to BTIF representation.
   3922  * Returns          None
   3923  *
   3924  **************************************************************************/
   3925 void get_folder_item_type_media(const tAVRC_ITEM* avrc_item,
   3926                                 btrc_folder_items_t* btrc_item) {
   3927   btrc_item->item_type = BTRC_ITEM_MEDIA;
   3928   const tAVRC_ITEM_MEDIA* avrc_item_media = &(avrc_item->u.media);
   3929   btrc_item_media_t* btrc_item_media = &(btrc_item->media);
   3930   /* UID */
   3931   memset(btrc_item_media->uid, 0, BTRC_UID_SIZE * sizeof(uint8_t));
   3932   memcpy(btrc_item_media->uid, avrc_item_media->uid,
   3933          sizeof(uint8_t) * BTRC_UID_SIZE);
   3934 
   3935   /* Audio/Video type */
   3936   switch (avrc_item_media->type) {
   3937     case AVRC_MEDIA_TYPE_AUDIO:
   3938       btrc_item_media->type = BTRC_MEDIA_TYPE_AUDIO;
   3939       break;
   3940     case AVRC_MEDIA_TYPE_VIDEO:
   3941       btrc_item_media->type = BTRC_MEDIA_TYPE_VIDEO;
   3942       break;
   3943   }
   3944 
   3945   /* Charset ID */
   3946   btrc_item_media->charset_id = avrc_item_media->name.charset_id;
   3947 
   3948   /* Copy the name */
   3949   BTIF_TRACE_DEBUG("%s max len %d str len %d", __func__, BTRC_MAX_ATTR_STR_LEN,
   3950                    avrc_item_media->name.str_len);
   3951   memset(btrc_item_media->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t));
   3952   memcpy(btrc_item_media->name, avrc_item_media->name.p_str,
   3953          sizeof(uint8_t) * (avrc_item_media->name.str_len));
   3954 
   3955   /* Copy the parameters */
   3956   btrc_item_media->num_attrs = avrc_item_media->attr_count;
   3957   btrc_item_media->p_attrs = (btrc_element_attr_val_t*)osi_malloc(
   3958       btrc_item_media->num_attrs * sizeof(btrc_element_attr_val_t));
   3959 
   3960   /* Extract each attribute */
   3961   for (int i = 0; i < avrc_item_media->attr_count; i++) {
   3962     btrc_element_attr_val_t* btrc_attr_pair = &(btrc_item_media->p_attrs[i]);
   3963     tAVRC_ATTR_ENTRY* avrc_attr_pair = &(avrc_item_media->p_attr_list[i]);
   3964 
   3965     BTIF_TRACE_DEBUG("%s media attr id 0x%x", __func__,
   3966                      avrc_attr_pair->attr_id);
   3967 
   3968     switch (avrc_attr_pair->attr_id) {
   3969       case AVRC_MEDIA_ATTR_ID_TITLE:
   3970         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_TITLE;
   3971         break;
   3972       case AVRC_MEDIA_ATTR_ID_ARTIST:
   3973         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_ARTIST;
   3974         break;
   3975       case AVRC_MEDIA_ATTR_ID_ALBUM:
   3976         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_ALBUM;
   3977         break;
   3978       case AVRC_MEDIA_ATTR_ID_TRACK_NUM:
   3979         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_TRACK_NUM;
   3980         break;
   3981       case AVRC_MEDIA_ATTR_ID_NUM_TRACKS:
   3982         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_NUM_TRACKS;
   3983         break;
   3984       case AVRC_MEDIA_ATTR_ID_GENRE:
   3985         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_GENRE;
   3986         break;
   3987       case AVRC_MEDIA_ATTR_ID_PLAYING_TIME:
   3988         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_PLAYING_TIME;
   3989         break;
   3990       default:
   3991         BTIF_TRACE_ERROR("%s invalid media attr id: 0x%x", __func__,
   3992                          avrc_attr_pair->attr_id);
   3993         btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_INVALID;
   3994     }
   3995 
   3996     memset(btrc_attr_pair->text, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t));
   3997     memcpy(btrc_attr_pair->text, avrc_attr_pair->name.p_str,
   3998            avrc_attr_pair->name.str_len);
   3999   }
   4000 }
   4001 
   4002 /***************************************************************************
   4003  *
   4004  * Function         get_folder_item_type_folder
   4005  *
   4006  * Description      Converts the AVRC representation of a folder item with
   4007  *                  TYPE folder to BTIF representation.
   4008  * Returns          None
   4009  *
   4010  **************************************************************************/
   4011 void get_folder_item_type_folder(const tAVRC_ITEM* avrc_item,
   4012                                  btrc_folder_items_t* btrc_item) {
   4013   btrc_item->item_type = BTRC_ITEM_FOLDER;
   4014   const tAVRC_ITEM_FOLDER* avrc_item_folder = &(avrc_item->u.folder);
   4015   btrc_item_folder_t* btrc_item_folder = &(btrc_item->folder);
   4016   /* Copy the UID */
   4017   memset(btrc_item_folder->uid, 0, BTRC_UID_SIZE * sizeof(uint8_t));
   4018   memcpy(btrc_item_folder->uid, avrc_item_folder->uid,
   4019          sizeof(uint8_t) * BTRC_UID_SIZE);
   4020 
   4021   /* Copy the type */
   4022   switch (avrc_item_folder->type) {
   4023     case AVRC_FOLDER_TYPE_MIXED:
   4024       btrc_item_folder->type = BTRC_FOLDER_TYPE_MIXED;
   4025       break;
   4026     case AVRC_FOLDER_TYPE_TITLES:
   4027       btrc_item_folder->type = BTRC_FOLDER_TYPE_TITLES;
   4028       break;
   4029     case AVRC_FOLDER_TYPE_ALNUMS:
   4030       btrc_item_folder->type = BTRC_FOLDER_TYPE_ALBUMS;
   4031       break;
   4032     case AVRC_FOLDER_TYPE_ARTISTS:
   4033       btrc_item_folder->type = BTRC_FOLDER_TYPE_ARTISTS;
   4034       break;
   4035     case AVRC_FOLDER_TYPE_GENRES:
   4036       btrc_item_folder->type = BTRC_FOLDER_TYPE_GENRES;
   4037       break;
   4038     case AVRC_FOLDER_TYPE_PLAYLISTS:
   4039       btrc_item_folder->type = BTRC_FOLDER_TYPE_PLAYLISTS;
   4040       break;
   4041     case AVRC_FOLDER_TYPE_YEARS:
   4042       btrc_item_folder->type = BTRC_FOLDER_TYPE_YEARS;
   4043       break;
   4044   }
   4045 
   4046   /* Copy if playable */
   4047   btrc_item_folder->playable = avrc_item_folder->playable;
   4048 
   4049   /* Copy name */
   4050   BTIF_TRACE_DEBUG("%s max len %d str len %d", __func__, BTRC_MAX_ATTR_STR_LEN,
   4051                    avrc_item_folder->name.str_len);
   4052   memset(btrc_item_folder->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t));
   4053   memcpy(btrc_item_folder->name, avrc_item_folder->name.p_str,
   4054          avrc_item_folder->name.str_len * sizeof(uint8_t));
   4055 
   4056   /* Copy charset */
   4057   btrc_item_folder->charset_id = avrc_item_folder->name.charset_id;
   4058 }
   4059 
   4060 /***************************************************************************
   4061  *
   4062  * Function         get_folder_item_type_player
   4063  *
   4064  * Description      Converts the AVRC representation of a folder item with
   4065  *                  TYPE player to BTIF representation.
   4066  * Returns          None
   4067  *
   4068  **************************************************************************/
   4069 void get_folder_item_type_player(const tAVRC_ITEM* avrc_item,
   4070                                  btrc_folder_items_t* btrc_item) {
   4071   btrc_item->item_type = BTRC_ITEM_PLAYER;
   4072   const tAVRC_ITEM_PLAYER* avrc_item_player = &(avrc_item->u.player);
   4073   btrc_item_player_t* btrc_item_player = &(btrc_item->player);
   4074   /* Player ID */
   4075   btrc_item_player->player_id = avrc_item_player->player_id;
   4076   /* Major type */
   4077   btrc_item_player->major_type = avrc_item_player->major_type;
   4078   /* Sub type */
   4079   btrc_item_player->sub_type = avrc_item_player->sub_type;
   4080   /* Features */
   4081   memcpy(btrc_item_player->features, avrc_item_player->features,
   4082          BTRC_FEATURE_BIT_MASK_SIZE);
   4083 
   4084   memset(btrc_item_player->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t));
   4085   memcpy(btrc_item_player->name, avrc_item_player->name.p_str,
   4086          avrc_item_player->name.str_len);
   4087 }
   4088 
   4089 /***************************************************************************
   4090  *
   4091  * Function         handle_change_path_response
   4092  *
   4093  * Description      handles the the change path response, calls
   4094  *                  HAL callback to send the updated folder
   4095  * Returns          None
   4096  *
   4097  **************************************************************************/
   4098 static void handle_change_path_response(tBTA_AV_META_MSG* pmeta_msg,
   4099                                         tAVRC_CHG_PATH_RSP* p_rsp) {
   4100   btif_rc_device_cb_t* p_dev =
   4101       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   4102   RawAddress rc_addr = p_dev->rc_addr;
   4103 
   4104   if (p_rsp->status == AVRC_STS_NO_ERROR) {
   4105     HAL_CBACK(bt_rc_ctrl_callbacks, change_folder_path_cb, &rc_addr,
   4106               p_rsp->num_items);
   4107   } else {
   4108     BTIF_TRACE_ERROR("%s error in handle_change_path_response %d", __func__,
   4109                      p_rsp->status);
   4110   }
   4111 }
   4112 
   4113 /***************************************************************************
   4114  *
   4115  * Function         handle_set_browsed_player_response
   4116  *
   4117  * Description      handles the the change path response, calls
   4118  *                  HAL callback to send the updated folder
   4119  * Returns          None
   4120  *
   4121  **************************************************************************/
   4122 static void handle_set_browsed_player_response(tBTA_AV_META_MSG* pmeta_msg,
   4123                                                tAVRC_SET_BR_PLAYER_RSP* p_rsp) {
   4124   btif_rc_device_cb_t* p_dev =
   4125       btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   4126   RawAddress rc_addr = p_dev->rc_addr;
   4127 
   4128   if (p_rsp->status == AVRC_STS_NO_ERROR) {
   4129     HAL_CBACK(bt_rc_ctrl_callbacks, set_browsed_player_cb, &rc_addr,
   4130               p_rsp->num_items, p_rsp->folder_depth);
   4131   } else {
   4132     BTIF_TRACE_ERROR("%s error %d", __func__, p_rsp->status);
   4133   }
   4134 }
   4135 
   4136 /***************************************************************************
   4137  *
   4138  * Function         clear_cmd_timeout
   4139  *
   4140  * Description      helper function to stop the command timeout timer
   4141  * Returns          None
   4142  *
   4143  **************************************************************************/
   4144 static void clear_cmd_timeout(uint8_t label) {
   4145   rc_transaction_t* p_txn;
   4146 
   4147   p_txn = get_transaction_by_lbl(label);
   4148   if (p_txn == NULL) {
   4149     BTIF_TRACE_ERROR("%s: Error in transaction label lookup", __func__);
   4150     return;
   4151   }
   4152 
   4153   if (p_txn->txn_timer != NULL) alarm_cancel(p_txn->txn_timer);
   4154 }
   4155 
   4156 /***************************************************************************
   4157  *
   4158  * Function         handle_avk_rc_metamsg_rsp
   4159  *
   4160  * Description      Handle RC metamessage response
   4161  *
   4162  * Returns          void
   4163  *
   4164  **************************************************************************/
   4165 static void handle_avk_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg) {
   4166   tAVRC_RESPONSE avrc_response = {0};
   4167   uint8_t scratch_buf[512] = {0};  // this variable is unused
   4168   uint16_t buf_len;
   4169   tAVRC_STS status;
   4170 
   4171   BTIF_TRACE_DEBUG("%s: opcode: %d rsp_code: %d  ", __func__,
   4172                    pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code);
   4173 
   4174   status = AVRC_Ctrl_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf,
   4175                                   &buf_len);
   4176   if ((AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode) &&
   4177       (pmeta_msg->code >= AVRC_RSP_NOT_IMPL) &&
   4178       (pmeta_msg->code <= AVRC_RSP_INTERIM)) {
   4179     BTIF_TRACE_DEBUG("%s parse status %d pdu = %d rsp_status = %d", __func__,
   4180                      status, avrc_response.pdu,
   4181                      pmeta_msg->p_msg->vendor.hdr.ctype);
   4182 
   4183     switch (avrc_response.pdu) {
   4184       case AVRC_PDU_REGISTER_NOTIFICATION:
   4185         handle_notification_response(pmeta_msg, &avrc_response.reg_notif);
   4186         if (pmeta_msg->code == AVRC_RSP_INTERIM) {
   4187           /* Don't free the transaction Id */
   4188           clear_cmd_timeout(pmeta_msg->label);
   4189           return;
   4190         }
   4191         break;
   4192 
   4193       case AVRC_PDU_GET_CAPABILITIES:
   4194         handle_get_capability_response(pmeta_msg, &avrc_response.get_caps);
   4195         break;
   4196 
   4197       case AVRC_PDU_LIST_PLAYER_APP_ATTR:
   4198         handle_app_attr_response(pmeta_msg, &avrc_response.list_app_attr);
   4199         break;
   4200 
   4201       case AVRC_PDU_LIST_PLAYER_APP_VALUES:
   4202         handle_app_val_response(pmeta_msg, &avrc_response.list_app_values);
   4203         break;
   4204 
   4205       case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:
   4206         handle_app_cur_val_response(pmeta_msg, &avrc_response.get_cur_app_val);
   4207         break;
   4208 
   4209       case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:
   4210         handle_app_attr_txt_response(pmeta_msg,
   4211                                      &avrc_response.get_app_attr_txt);
   4212         break;
   4213 
   4214       case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:
   4215         handle_app_attr_val_txt_response(pmeta_msg,
   4216                                          &avrc_response.get_app_val_txt);
   4217         break;
   4218 
   4219       case AVRC_PDU_SET_PLAYER_APP_VALUE:
   4220         handle_set_app_attr_val_response(pmeta_msg, &avrc_response.set_app_val);
   4221         break;
   4222 
   4223       case AVRC_PDU_GET_ELEMENT_ATTR:
   4224         handle_get_elem_attr_response(pmeta_msg, &avrc_response.get_attrs);
   4225         break;
   4226 
   4227       case AVRC_PDU_GET_PLAY_STATUS:
   4228         handle_get_playstatus_response(pmeta_msg,
   4229                                        &avrc_response.get_play_status);
   4230         break;
   4231 
   4232       case AVRC_PDU_SET_ADDRESSED_PLAYER:
   4233         handle_set_addressed_player_response(pmeta_msg, &avrc_response.rsp);
   4234         break;
   4235     }
   4236   } else if (AVRC_OP_BROWSE == pmeta_msg->p_msg->hdr.opcode) {
   4237     BTIF_TRACE_DEBUG("%s AVRC_OP_BROWSE pdu %d", __func__, avrc_response.pdu);
   4238     /* check what kind of command it is for browsing */
   4239     switch (avrc_response.pdu) {
   4240       case AVRC_PDU_GET_FOLDER_ITEMS:
   4241         handle_get_folder_items_response(pmeta_msg, &avrc_response.get_items);
   4242         break;
   4243       case AVRC_PDU_CHANGE_PATH:
   4244         handle_change_path_response(pmeta_msg, &avrc_response.chg_path);
   4245         break;
   4246       case AVRC_PDU_SET_BROWSED_PLAYER:
   4247         handle_set_browsed_player_response(pmeta_msg, &avrc_response.br_player);
   4248         break;
   4249       default:
   4250         BTIF_TRACE_ERROR("%s cannot handle browse pdu %d", __func__,
   4251                          pmeta_msg->p_msg->hdr.opcode);
   4252     }
   4253   } else {
   4254     BTIF_TRACE_DEBUG(
   4255         "%s: Invalid Vendor Command code: %d len: %d. Not processing it.",
   4256         __func__, pmeta_msg->code, pmeta_msg->len);
   4257     return;
   4258   }
   4259   BTIF_TRACE_DEBUG("XX __func__ release transaction %d", pmeta_msg->label);
   4260   release_transaction(pmeta_msg->label);
   4261 }
   4262 
   4263 /***************************************************************************
   4264  *
   4265  * Function         handle_avk_rc_metamsg_cmd
   4266  *
   4267  * Description      Handle RC metamessage response
   4268  *
   4269  * Returns          void
   4270  *
   4271  **************************************************************************/
   4272 static void handle_avk_rc_metamsg_cmd(tBTA_AV_META_MSG* pmeta_msg) {
   4273   tAVRC_COMMAND avrc_cmd = {0};
   4274   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   4275   btif_rc_device_cb_t* p_dev = NULL;
   4276 
   4277   BTIF_TRACE_DEBUG("%s: opcode: %d rsp_code: %d", __func__,
   4278                    pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code);
   4279   status = AVRC_Ctrl_ParsCommand(pmeta_msg->p_msg, &avrc_cmd);
   4280   if ((AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode) &&
   4281       (pmeta_msg->code <= AVRC_CMD_GEN_INQ)) {
   4282     BTIF_TRACE_DEBUG("%s Received vendor command.code %d, PDU %d label %d",
   4283                      __func__, pmeta_msg->code, avrc_cmd.pdu, pmeta_msg->label);
   4284 
   4285     if (status != AVRC_STS_NO_ERROR) {
   4286       /* return error */
   4287       BTIF_TRACE_WARNING(
   4288           "%s: Error in parsing received metamsg command. status: 0x%02x",
   4289           __func__, status);
   4290       send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_cmd.pdu,
   4291                            status, pmeta_msg->p_msg->hdr.opcode);
   4292     } else {
   4293       p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle);
   4294       if (p_dev == NULL) {
   4295         BTIF_TRACE_ERROR("%s: avk rc meta msg cmd for Invalid rc handle",
   4296                          __func__);
   4297         return;
   4298       }
   4299 
   4300       if (avrc_cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) {
   4301         uint8_t event_id = avrc_cmd.reg_notif.event_id;
   4302         BTIF_TRACE_EVENT("%s: Register notification event_id: %s", __func__,
   4303                          dump_rc_notification_event_id(event_id));
   4304       } else if (avrc_cmd.pdu == AVRC_PDU_SET_ABSOLUTE_VOLUME) {
   4305         BTIF_TRACE_EVENT("%s: Abs Volume Cmd Recvd", __func__);
   4306       }
   4307 
   4308       btif_rc_ctrl_upstreams_rsp_cmd(avrc_cmd.pdu, &avrc_cmd, pmeta_msg->label,
   4309                                      p_dev);
   4310     }
   4311   } else {
   4312     BTIF_TRACE_DEBUG(
   4313         "%s: Invalid Vendor Command  code: %d len: %d. Not processing it.",
   4314         __func__, pmeta_msg->code, pmeta_msg->len);
   4315     return;
   4316   }
   4317 }
   4318 
   4319 /***************************************************************************
   4320  *
   4321  * Function         cleanup
   4322  *
   4323  * Description      Closes the AVRC interface
   4324  *
   4325  * Returns          void
   4326  *
   4327  **************************************************************************/
   4328 static void cleanup() {
   4329   BTIF_TRACE_EVENT("%s: ", __func__);
   4330   if (bt_rc_callbacks) {
   4331     bt_rc_callbacks = NULL;
   4332   }
   4333 
   4334   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   4335     alarm_free(btif_rc_cb.rc_multi_cb[idx].rc_play_status_timer);
   4336     memset(&btif_rc_cb.rc_multi_cb[idx], 0,
   4337            sizeof(btif_rc_cb.rc_multi_cb[idx]));
   4338   }
   4339 
   4340   BTIF_TRACE_EVENT("%s: completed", __func__);
   4341 }
   4342 
   4343 /***************************************************************************
   4344  *
   4345  * Function         cleanup_ctrl
   4346  *
   4347  * Description      Closes the AVRC Controller interface
   4348  *
   4349  * Returns          void
   4350  *
   4351  **************************************************************************/
   4352 static void cleanup_ctrl() {
   4353   BTIF_TRACE_EVENT("%s: ", __func__);
   4354 
   4355   if (bt_rc_ctrl_callbacks) {
   4356     bt_rc_ctrl_callbacks = NULL;
   4357   }
   4358 
   4359   for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) {
   4360     alarm_free(btif_rc_cb.rc_multi_cb[idx].rc_play_status_timer);
   4361     memset(&btif_rc_cb.rc_multi_cb[idx], 0,
   4362            sizeof(btif_rc_cb.rc_multi_cb[idx]));
   4363   }
   4364 
   4365   memset(&btif_rc_cb.rc_multi_cb, 0, sizeof(btif_rc_cb.rc_multi_cb));
   4366   BTIF_TRACE_EVENT("%s: completed", __func__);
   4367 }
   4368 
   4369 /***************************************************************************
   4370  *
   4371  * Function         getcapabilities_cmd
   4372  *
   4373  * Description      GetCapabilties from Remote(Company_ID, Events_Supported)
   4374  *
   4375  * Returns          void
   4376  *
   4377  **************************************************************************/
   4378 static bt_status_t getcapabilities_cmd(uint8_t cap_id,
   4379                                        btif_rc_device_cb_t* p_dev) {
   4380   BTIF_TRACE_DEBUG("%s: cap_id: %d", __func__, cap_id);
   4381   CHECK_RC_CONNECTED(p_dev);
   4382 
   4383   tAVRC_COMMAND avrc_cmd = {0};
   4384   avrc_cmd.get_caps.opcode = AVRC_OP_VENDOR;
   4385   avrc_cmd.get_caps.capability_id = cap_id;
   4386   avrc_cmd.get_caps.pdu = AVRC_PDU_GET_CAPABILITIES;
   4387   avrc_cmd.get_caps.status = AVRC_STS_NO_ERROR;
   4388 
   4389   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4390 }
   4391 
   4392 /***************************************************************************
   4393  *
   4394  * Function         list_player_app_setting_attrib_cmd
   4395  *
   4396  * Description      Get supported List Player Attributes
   4397  *
   4398  * Returns          void
   4399  *
   4400  **************************************************************************/
   4401 static bt_status_t list_player_app_setting_attrib_cmd(
   4402     btif_rc_device_cb_t* p_dev) {
   4403   BTIF_TRACE_DEBUG("%s", __func__);
   4404   CHECK_RC_CONNECTED(p_dev);
   4405 
   4406   tAVRC_COMMAND avrc_cmd = {0};
   4407   avrc_cmd.list_app_attr.opcode = AVRC_OP_VENDOR;
   4408   avrc_cmd.list_app_attr.pdu = AVRC_PDU_LIST_PLAYER_APP_ATTR;
   4409   avrc_cmd.list_app_attr.status = AVRC_STS_NO_ERROR;
   4410 
   4411   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4412 }
   4413 
   4414 /***************************************************************************
   4415  *
   4416  * Function         list_player_app_setting_value_cmd
   4417  *
   4418  * Description      Get values of supported Player Attributes
   4419  *
   4420  * Returns          void
   4421  *
   4422  **************************************************************************/
   4423 static bt_status_t list_player_app_setting_value_cmd(
   4424     uint8_t attrib_id, btif_rc_device_cb_t* p_dev) {
   4425   BTIF_TRACE_DEBUG("%s: attrib_id: %d", __func__, attrib_id);
   4426   CHECK_RC_CONNECTED(p_dev);
   4427 
   4428   tAVRC_COMMAND avrc_cmd = {0};
   4429   avrc_cmd.list_app_values.attr_id = attrib_id;
   4430   avrc_cmd.list_app_values.opcode = AVRC_OP_VENDOR;
   4431   avrc_cmd.list_app_values.pdu = AVRC_PDU_LIST_PLAYER_APP_VALUES;
   4432   avrc_cmd.list_app_values.status = AVRC_STS_NO_ERROR;
   4433 
   4434   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4435 }
   4436 
   4437 /***************************************************************************
   4438  *
   4439  * Function         get_player_app_setting_cmd
   4440  *
   4441  * Description      Get current values of Player Attributes
   4442  *
   4443  * Returns          void
   4444  *
   4445  **************************************************************************/
   4446 static bt_status_t get_player_app_setting_cmd(uint8_t num_attrib,
   4447                                               uint8_t* attrib_ids,
   4448                                               btif_rc_device_cb_t* p_dev) {
   4449   BTIF_TRACE_DEBUG("%s: num_attrib: %d", __func__, num_attrib);
   4450   CHECK_RC_CONNECTED(p_dev);
   4451 
   4452   tAVRC_COMMAND avrc_cmd = {0};
   4453   avrc_cmd.get_cur_app_val.opcode = AVRC_OP_VENDOR;
   4454   avrc_cmd.get_cur_app_val.status = AVRC_STS_NO_ERROR;
   4455   avrc_cmd.get_cur_app_val.num_attr = num_attrib;
   4456   avrc_cmd.get_cur_app_val.pdu = AVRC_PDU_GET_CUR_PLAYER_APP_VALUE;
   4457 
   4458   for (int count = 0; count < num_attrib; count++) {
   4459     avrc_cmd.get_cur_app_val.attrs[count] = attrib_ids[count];
   4460   }
   4461 
   4462   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4463 }
   4464 
   4465 /***************************************************************************
   4466  *
   4467  * Function         get_playback_state_cmd
   4468  *
   4469  * Description      Fetch the current playback state for the device
   4470  *
   4471  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4472  *                  BT_STATUS_FAIL.
   4473  *
   4474  **************************************************************************/
   4475 static bt_status_t get_playback_state_cmd(RawAddress* bd_addr) {
   4476   BTIF_TRACE_DEBUG("%s", __func__);
   4477   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4478   return get_play_status_cmd(p_dev);
   4479 }
   4480 
   4481 /***************************************************************************
   4482  *
   4483  * Function         get_now_playing_list_cmd
   4484  *
   4485  * Description      Fetch the now playing list
   4486  *
   4487  * Paramters        start_item: First item to fetch (0 to fetch from beganning)
   4488  *                  end_item: Last item to fetch (0xff to fetch until end)
   4489  *
   4490  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4491  *                  BT_STATUS_FAIL.
   4492  *
   4493  **************************************************************************/
   4494 static bt_status_t get_now_playing_list_cmd(RawAddress* bd_addr,
   4495                                             uint8_t start_item,
   4496                                             uint8_t num_items) {
   4497   BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items);
   4498   return get_folder_items_cmd(bd_addr, AVRC_SCOPE_NOW_PLAYING, start_item,
   4499                               num_items);
   4500 }
   4501 
   4502 /***************************************************************************
   4503  *
   4504  * Function         get_folder_list_cmd
   4505  *
   4506  * Description      Fetch the currently selected folder list
   4507  *
   4508  * Paramters        start_item: First item to fetch (0 to fetch from beganning)
   4509  *                  end_item: Last item to fetch (0xff to fetch until end)
   4510  *
   4511  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4512  *                  BT_STATUS_FAIL.
   4513  *
   4514  **************************************************************************/
   4515 static bt_status_t get_folder_list_cmd(RawAddress* bd_addr, uint8_t start_item,
   4516                                        uint8_t num_items) {
   4517   BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items);
   4518   return get_folder_items_cmd(bd_addr, AVRC_SCOPE_FILE_SYSTEM, start_item,
   4519                               num_items);
   4520 }
   4521 
   4522 /***************************************************************************
   4523  *
   4524  * Function         get_player_list_cmd
   4525  *
   4526  * Description      Fetch the player list
   4527  *
   4528  * Paramters        start_item: First item to fetch (0 to fetch from beganning)
   4529  *                  end_item: Last item to fetch (0xff to fetch until end)
   4530  *
   4531  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4532  *                  BT_STATUS_FAIL.
   4533  *
   4534  **************************************************************************/
   4535 static bt_status_t get_player_list_cmd(RawAddress* bd_addr, uint8_t start_item,
   4536                                        uint8_t num_items) {
   4537   BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items);
   4538   return get_folder_items_cmd(bd_addr, AVRC_SCOPE_PLAYER_LIST, start_item,
   4539                               num_items);
   4540 }
   4541 
   4542 /***************************************************************************
   4543  *
   4544  * Function         change_folder_path_cmd
   4545  *
   4546  * Description      Change the folder.
   4547  *
   4548  * Paramters        direction: Direction (Up/Down) to change folder
   4549  *                  uid: The UID of folder to move to
   4550  *                  start_item: First item to fetch (0 to fetch from beganning)
   4551  *                  end_item: Last item to fetch (0xff to fetch until end)
   4552  *
   4553  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4554  *                  BT_STATUS_FAIL.
   4555  *
   4556  **************************************************************************/
   4557 static bt_status_t change_folder_path_cmd(RawAddress* bd_addr,
   4558                                           uint8_t direction, uint8_t* uid) {
   4559   BTIF_TRACE_DEBUG("%s: direction %d", __func__, direction);
   4560   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4561   CHECK_RC_CONNECTED(p_dev);
   4562   CHECK_BR_CONNECTED(p_dev);
   4563 
   4564   tAVRC_COMMAND avrc_cmd = {0};
   4565 
   4566   avrc_cmd.chg_path.pdu = AVRC_PDU_CHANGE_PATH;
   4567   avrc_cmd.chg_path.status = AVRC_STS_NO_ERROR;
   4568   // TODO(sanketa): Improve for database aware clients.
   4569   avrc_cmd.chg_path.uid_counter = 0;
   4570   avrc_cmd.chg_path.direction = direction;
   4571 
   4572   memset(avrc_cmd.chg_path.folder_uid, 0, AVRC_UID_SIZE * sizeof(uint8_t));
   4573   memcpy(avrc_cmd.chg_path.folder_uid, uid, AVRC_UID_SIZE * sizeof(uint8_t));
   4574 
   4575   BT_HDR* p_msg = NULL;
   4576   tAVRC_STS status = AVRC_BldCommand(&avrc_cmd, &p_msg);
   4577   if (status != AVRC_STS_NO_ERROR) {
   4578     BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, status);
   4579     return BT_STATUS_FAIL;
   4580   }
   4581 
   4582   rc_transaction_t* p_transaction = NULL;
   4583   bt_status_t tran_status = get_transaction(&p_transaction);
   4584   if (tran_status != BT_STATUS_SUCCESS || p_transaction == NULL) {
   4585     osi_free(p_msg);
   4586     BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x",
   4587                      __func__, tran_status);
   4588     return BT_STATUS_FAIL;
   4589   }
   4590 
   4591   BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__,
   4592                    p_transaction->lbl);
   4593   BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
   4594   return BT_STATUS_SUCCESS;
   4595 }
   4596 
   4597 /***************************************************************************
   4598  *
   4599  * Function         set_browsed_player_cmd
   4600  *
   4601  * Description      Change the browsed player.
   4602  *
   4603  * Paramters        id: The UID of player to move to
   4604  *
   4605  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4606  *                  BT_STATUS_FAIL.
   4607  *
   4608  **************************************************************************/
   4609 static bt_status_t set_browsed_player_cmd(RawAddress* bd_addr, uint16_t id) {
   4610   BTIF_TRACE_DEBUG("%s: id %d", __func__, id);
   4611   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4612   CHECK_RC_CONNECTED(p_dev);
   4613   CHECK_BR_CONNECTED(p_dev);
   4614 
   4615   rc_transaction_t* p_transaction = NULL;
   4616 
   4617   tAVRC_COMMAND avrc_cmd = {0};
   4618   avrc_cmd.br_player.pdu = AVRC_PDU_SET_BROWSED_PLAYER;
   4619   avrc_cmd.br_player.status = AVRC_STS_NO_ERROR;
   4620   // TODO(sanketa): Improve for database aware clients.
   4621   avrc_cmd.br_player.player_id = id;
   4622 
   4623   BT_HDR* p_msg = NULL;
   4624   tAVRC_STS status = AVRC_BldCommand(&avrc_cmd, &p_msg);
   4625   if (status != AVRC_STS_NO_ERROR) {
   4626     BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, status);
   4627     return BT_STATUS_FAIL;
   4628   }
   4629 
   4630   bt_status_t tran_status = get_transaction(&p_transaction);
   4631   if (tran_status != BT_STATUS_SUCCESS || p_transaction == NULL) {
   4632     osi_free(p_msg);
   4633     BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x",
   4634                      __func__, tran_status);
   4635     return BT_STATUS_FAIL;
   4636   }
   4637 
   4638   BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__,
   4639                    p_transaction->lbl);
   4640   BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
   4641   return BT_STATUS_SUCCESS;
   4642 }
   4643 
   4644 /***************************************************************************
   4645  **
   4646  ** Function         set_addressed_player_cmd
   4647  **
   4648  ** Description      Change the addressed player.
   4649  **
   4650  ** Paramters        id: The UID of player to move to
   4651  **
   4652  ** Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4653  **                  BT_STATUS_FAIL.
   4654  **
   4655  ***************************************************************************/
   4656 static bt_status_t set_addressed_player_cmd(RawAddress* bd_addr, uint16_t id) {
   4657   BTIF_TRACE_DEBUG("%s: id %d", __func__, id);
   4658 
   4659   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4660   CHECK_RC_CONNECTED(p_dev);
   4661   CHECK_BR_CONNECTED(p_dev);
   4662 
   4663   tAVRC_COMMAND avrc_cmd = {0};
   4664   BT_HDR* p_msg = NULL;
   4665 
   4666   avrc_cmd.addr_player.pdu = AVRC_PDU_SET_ADDRESSED_PLAYER;
   4667   avrc_cmd.addr_player.status = AVRC_STS_NO_ERROR;
   4668   // TODO(sanketa): Improve for database aware clients.
   4669   avrc_cmd.addr_player.player_id = id;
   4670 
   4671   tAVRC_STS status = AVRC_BldCommand(&avrc_cmd, &p_msg);
   4672   if (status != AVRC_STS_NO_ERROR) {
   4673     BTIF_TRACE_ERROR("%s: failed to build command status %d", __func__, status);
   4674     return BT_STATUS_FAIL;
   4675   }
   4676 
   4677   rc_transaction_t* p_transaction = NULL;
   4678   bt_status_t tran_status = get_transaction(&p_transaction);
   4679 
   4680   if (tran_status != BT_STATUS_SUCCESS || p_transaction == NULL) {
   4681     osi_free(p_msg);
   4682     BTIF_TRACE_ERROR("%s: failed to obtain txn details. status: 0x%02x",
   4683                      __func__, tran_status);
   4684     return BT_STATUS_FAIL;
   4685   }
   4686 
   4687   BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__,
   4688                    p_transaction->lbl);
   4689   BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
   4690   return BT_STATUS_SUCCESS;
   4691 }
   4692 
   4693 /***************************************************************************
   4694  *
   4695  * Function         get_folder_items_cmd
   4696  *
   4697  * Description      Helper function to browse the content hierarchy of the
   4698  *                  TG device.
   4699  *
   4700  * Paramters        scope: AVRC_SCOPE_NOW_PLAYING (etc) for various browseable
   4701  *                  content
   4702  *                  start_item: First item to fetch (0 to fetch from beganning)
   4703  *                  end_item: Last item to fetch (0xff to fetch until end)
   4704  *
   4705  * Returns          BT_STATUS_SUCCESS if command issued successfully otherwise
   4706  *                  BT_STATUS_FAIL.
   4707  *
   4708  **************************************************************************/
   4709 static bt_status_t get_folder_items_cmd(RawAddress* bd_addr, uint8_t scope,
   4710                                         uint8_t start_item, uint8_t end_item) {
   4711   /* Check that both avrcp and browse channel are connected. */
   4712   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4713   BTIF_TRACE_DEBUG("%s", __func__);
   4714   CHECK_RC_CONNECTED(p_dev);
   4715   CHECK_BR_CONNECTED(p_dev);
   4716 
   4717   tAVRC_COMMAND avrc_cmd = {0};
   4718 
   4719   /* Set the layer specific to point to browse although this should really
   4720    * be done by lower layers and looking at the PDU
   4721    */
   4722   avrc_cmd.get_items.pdu = AVRC_PDU_GET_FOLDER_ITEMS;
   4723   avrc_cmd.get_items.status = AVRC_STS_NO_ERROR;
   4724   avrc_cmd.get_items.scope = scope;
   4725   avrc_cmd.get_items.start_item = start_item;
   4726   avrc_cmd.get_items.end_item = end_item;
   4727   avrc_cmd.get_items.attr_count = 0; /* p_attr_list does not matter hence */
   4728 
   4729   BT_HDR* p_msg = NULL;
   4730   tAVRC_STS status = AVRC_BldCommand(&avrc_cmd, &p_msg);
   4731   if (status != AVRC_STS_NO_ERROR) {
   4732     BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, status);
   4733     return BT_STATUS_FAIL;
   4734   }
   4735 
   4736   rc_transaction_t* p_transaction = NULL;
   4737   bt_status_t tran_status = get_transaction(&p_transaction);
   4738   if (tran_status != BT_STATUS_SUCCESS || p_transaction == NULL) {
   4739     osi_free(p_msg);
   4740     BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x",
   4741                      __func__, tran_status);
   4742     return BT_STATUS_FAIL;
   4743   }
   4744 
   4745   BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__,
   4746                    p_transaction->lbl);
   4747   BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg);
   4748   return BT_STATUS_SUCCESS;
   4749 }
   4750 
   4751 /***************************************************************************
   4752  *
   4753  * Function         change_player_app_setting
   4754  *
   4755  * Description      Set current values of Player Attributes
   4756  *
   4757  * Returns          void
   4758  *
   4759  **************************************************************************/
   4760 static bt_status_t change_player_app_setting(RawAddress* bd_addr,
   4761                                              uint8_t num_attrib,
   4762                                              uint8_t* attrib_ids,
   4763                                              uint8_t* attrib_vals) {
   4764   BTIF_TRACE_DEBUG("%s: num_attrib: %d", __func__, num_attrib);
   4765   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4766   CHECK_RC_CONNECTED(p_dev);
   4767 
   4768   tAVRC_COMMAND avrc_cmd = {0};
   4769   avrc_cmd.set_app_val.opcode = AVRC_OP_VENDOR;
   4770   avrc_cmd.set_app_val.status = AVRC_STS_NO_ERROR;
   4771   avrc_cmd.set_app_val.num_val = num_attrib;
   4772   avrc_cmd.set_app_val.pdu = AVRC_PDU_SET_PLAYER_APP_VALUE;
   4773   avrc_cmd.set_app_val.p_vals =
   4774       (tAVRC_APP_SETTING*)osi_malloc(sizeof(tAVRC_APP_SETTING) * num_attrib);
   4775   for (int count = 0; count < num_attrib; count++) {
   4776     avrc_cmd.set_app_val.p_vals[count].attr_id = attrib_ids[count];
   4777     avrc_cmd.set_app_val.p_vals[count].attr_val = attrib_vals[count];
   4778   }
   4779 
   4780   bt_status_t st = build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_CTRL, p_dev);
   4781   osi_free_and_reset((void**)&avrc_cmd.set_app_val.p_vals);
   4782   return st;
   4783 }
   4784 
   4785 /***************************************************************************
   4786  *
   4787  * Function         play_item_cmd
   4788  *
   4789  * Description      Play the item specified by UID & scope
   4790  *
   4791  * Returns          void
   4792  *
   4793  **************************************************************************/
   4794 static bt_status_t play_item_cmd(RawAddress* bd_addr, uint8_t scope,
   4795                                  uint8_t* uid, uint16_t uid_counter) {
   4796   BTIF_TRACE_DEBUG("%s: scope %d uid_counter %d", __func__, scope, uid_counter);
   4797   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4798   CHECK_RC_CONNECTED(p_dev);
   4799   CHECK_BR_CONNECTED(p_dev);
   4800 
   4801   tAVRC_COMMAND avrc_cmd = {0};
   4802   avrc_cmd.pdu = AVRC_PDU_PLAY_ITEM;
   4803   avrc_cmd.play_item.opcode = AVRC_OP_VENDOR;
   4804   avrc_cmd.play_item.status = AVRC_STS_NO_ERROR;
   4805   avrc_cmd.play_item.scope = scope;
   4806   memcpy(avrc_cmd.play_item.uid, uid, AVRC_UID_SIZE);
   4807   avrc_cmd.play_item.uid_counter = uid_counter;
   4808 
   4809   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_CTRL, p_dev);
   4810 }
   4811 
   4812 /***************************************************************************
   4813  *
   4814  * Function         get_player_app_setting_attr_text_cmd
   4815  *
   4816  * Description      Get text description for app attribute
   4817  *
   4818  * Returns          void
   4819  *
   4820  **************************************************************************/
   4821 static bt_status_t get_player_app_setting_attr_text_cmd(
   4822     uint8_t* attrs, uint8_t num_attrs, btif_rc_device_cb_t* p_dev) {
   4823   BTIF_TRACE_DEBUG("%s: num attrs: %d", __func__, num_attrs);
   4824   CHECK_RC_CONNECTED(p_dev);
   4825 
   4826   tAVRC_COMMAND avrc_cmd = {0};
   4827   avrc_cmd.pdu = AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT;
   4828   avrc_cmd.get_app_attr_txt.opcode = AVRC_OP_VENDOR;
   4829   avrc_cmd.get_app_attr_txt.num_attr = num_attrs;
   4830 
   4831   for (int count = 0; count < num_attrs; count++) {
   4832     avrc_cmd.get_app_attr_txt.attrs[count] = attrs[count];
   4833   }
   4834 
   4835   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4836 }
   4837 
   4838 /***************************************************************************
   4839  *
   4840  * Function         get_player_app_setting_val_text_cmd
   4841  *
   4842  * Description      Get text description for app attribute values
   4843  *
   4844  * Returns          void
   4845  *
   4846  **************************************************************************/
   4847 static bt_status_t get_player_app_setting_value_text_cmd(
   4848     uint8_t* vals, uint8_t num_vals, btif_rc_device_cb_t* p_dev) {
   4849   BTIF_TRACE_DEBUG("%s: num_vals: %d", __func__, num_vals);
   4850   CHECK_RC_CONNECTED(p_dev);
   4851 
   4852   tAVRC_COMMAND avrc_cmd = {0};
   4853   avrc_cmd.pdu = AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT;
   4854   avrc_cmd.get_app_val_txt.opcode = AVRC_OP_VENDOR;
   4855   avrc_cmd.get_app_val_txt.num_val = num_vals;
   4856 
   4857   for (int count = 0; count < num_vals; count++) {
   4858     avrc_cmd.get_app_val_txt.vals[count] = vals[count];
   4859   }
   4860 
   4861   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4862 }
   4863 
   4864 /***************************************************************************
   4865  *
   4866  * Function         register_notification_cmd
   4867  *
   4868  * Description      Send Command to register for a Notification ID
   4869  *
   4870  * Returns          void
   4871  *
   4872  **************************************************************************/
   4873 static bt_status_t register_notification_cmd(uint8_t label, uint8_t event_id,
   4874                                              uint32_t event_value,
   4875                                              btif_rc_device_cb_t* p_dev) {
   4876   BTIF_TRACE_DEBUG("%s: event_id: %d event_value %d", __func__, event_id,
   4877                    event_value);
   4878   CHECK_RC_CONNECTED(p_dev);
   4879 
   4880   tAVRC_COMMAND avrc_cmd = {0};
   4881   avrc_cmd.reg_notif.opcode = AVRC_OP_VENDOR;
   4882   avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR;
   4883   avrc_cmd.reg_notif.event_id = event_id;
   4884   avrc_cmd.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
   4885   avrc_cmd.reg_notif.param = event_value;
   4886 
   4887   BT_HDR* p_msg = NULL;
   4888   tAVRC_STS status = AVRC_BldCommand(&avrc_cmd, &p_msg);
   4889   if (status == AVRC_STS_NO_ERROR) {
   4890     uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset;
   4891     BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__,
   4892                      label);
   4893     if (p_msg != NULL) {
   4894       BTA_AvVendorCmd(p_dev->rc_handle, label, AVRC_CMD_NOTIF, data_start,
   4895                       p_msg->len);
   4896       status = BT_STATUS_SUCCESS;
   4897     }
   4898   } else {
   4899     BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__,
   4900                      status);
   4901   }
   4902   osi_free(p_msg);
   4903   return (bt_status_t)status;
   4904 }
   4905 
   4906 /***************************************************************************
   4907  *
   4908  * Function         get_element_attribute_cmd
   4909  *
   4910  * Description      Get Element Attribute for  attributeIds
   4911  *
   4912  * Returns          void
   4913  *
   4914  **************************************************************************/
   4915 static bt_status_t get_element_attribute_cmd(uint8_t num_attribute,
   4916                                              uint32_t* p_attr_ids,
   4917                                              btif_rc_device_cb_t* p_dev) {
   4918   BTIF_TRACE_DEBUG("%s: num_attribute: %d attribute_id: %d", __func__,
   4919                    num_attribute, p_attr_ids[0]);
   4920   CHECK_RC_CONNECTED(p_dev);
   4921 
   4922   tAVRC_COMMAND avrc_cmd = {0};
   4923   avrc_cmd.get_elem_attrs.opcode = AVRC_OP_VENDOR;
   4924   avrc_cmd.get_elem_attrs.status = AVRC_STS_NO_ERROR;
   4925   avrc_cmd.get_elem_attrs.num_attr = num_attribute;
   4926   avrc_cmd.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR;
   4927   for (int count = 0; count < num_attribute; count++) {
   4928     avrc_cmd.get_elem_attrs.attrs[count] = p_attr_ids[count];
   4929   }
   4930 
   4931   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4932 }
   4933 
   4934 /***************************************************************************
   4935  *
   4936  * Function         get_play_status_cmd
   4937  *
   4938  * Description      Get Playing Status of a Device
   4939  *
   4940  * Returns          bt_status_t
   4941  *
   4942  **************************************************************************/
   4943 static bt_status_t get_play_status_cmd(btif_rc_device_cb_t* p_dev) {
   4944   BTIF_TRACE_DEBUG("%s", __func__);
   4945   CHECK_RC_CONNECTED(p_dev);
   4946 
   4947   tAVRC_COMMAND avrc_cmd = {0};
   4948   avrc_cmd.get_play_status.opcode = AVRC_OP_VENDOR;
   4949   avrc_cmd.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS;
   4950   avrc_cmd.get_play_status.status = AVRC_STS_NO_ERROR;
   4951 
   4952   return build_and_send_vendor_cmd(&avrc_cmd, AVRC_CMD_STATUS, p_dev);
   4953 }
   4954 
   4955 /***************************************************************************
   4956  *
   4957  * Function         set_volume_rsp
   4958  *
   4959  * Description      Rsp for SetAbsoluteVolume Command
   4960  *
   4961  * Returns          void
   4962  *
   4963  **************************************************************************/
   4964 static bt_status_t set_volume_rsp(RawAddress* bd_addr, uint8_t abs_vol,
   4965                                   uint8_t label) {
   4966   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   4967   tAVRC_RESPONSE avrc_rsp;
   4968   BT_HDR* p_msg = NULL;
   4969   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   4970 
   4971   CHECK_RC_CONNECTED(p_dev);
   4972 
   4973   BTIF_TRACE_DEBUG("%s: abs_vol: %d", __func__, abs_vol);
   4974 
   4975   avrc_rsp.volume.opcode = AVRC_OP_VENDOR;
   4976   avrc_rsp.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME;
   4977   avrc_rsp.volume.status = AVRC_STS_NO_ERROR;
   4978   avrc_rsp.volume.volume = abs_vol;
   4979   status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg);
   4980   if (status == AVRC_STS_NO_ERROR) {
   4981     uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset;
   4982     BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__,
   4983                      p_dev->rc_vol_label);
   4984     if (p_msg != NULL) {
   4985       BTA_AvVendorRsp(p_dev->rc_handle, label, BTA_AV_RSP_ACCEPT, data_start,
   4986                       p_msg->len, 0);
   4987       status = BT_STATUS_SUCCESS;
   4988     }
   4989   } else {
   4990     BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__,
   4991                      status);
   4992   }
   4993   osi_free(p_msg);
   4994   return (bt_status_t)status;
   4995 }
   4996 
   4997 /***************************************************************************
   4998  *
   4999  * Function         send_register_abs_vol_rsp
   5000  *
   5001  * Description      Rsp for Notification of Absolute Volume
   5002  *
   5003  * Returns          void
   5004  *
   5005  **************************************************************************/
   5006 static bt_status_t volume_change_notification_rsp(
   5007     RawAddress* bd_addr, btrc_notification_type_t rsp_type, uint8_t abs_vol,
   5008     uint8_t label) {
   5009   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   5010   tAVRC_RESPONSE avrc_rsp;
   5011   BT_HDR* p_msg = NULL;
   5012   BTIF_TRACE_DEBUG("%s: rsp_type: %d abs_vol: %d", __func__, rsp_type, abs_vol);
   5013 
   5014   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   5015 
   5016   CHECK_RC_CONNECTED(p_dev);
   5017 
   5018   avrc_rsp.reg_notif.opcode = AVRC_OP_VENDOR;
   5019   avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION;
   5020   avrc_rsp.reg_notif.status = AVRC_STS_NO_ERROR;
   5021   avrc_rsp.reg_notif.param.volume = abs_vol;
   5022   avrc_rsp.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE;
   5023 
   5024   status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg);
   5025   if (status == AVRC_STS_NO_ERROR) {
   5026     BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__,
   5027                      label);
   5028     uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset;
   5029     BTA_AvVendorRsp(p_dev->rc_handle, label,
   5030                     (rsp_type == BTRC_NOTIFICATION_TYPE_INTERIM)
   5031                         ? AVRC_RSP_INTERIM
   5032                         : AVRC_RSP_CHANGED,
   5033                     data_start, p_msg->len, 0);
   5034     status = BT_STATUS_SUCCESS;
   5035   } else {
   5036     BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__,
   5037                      status);
   5038   }
   5039   osi_free(p_msg);
   5040 
   5041   return (bt_status_t)status;
   5042 }
   5043 
   5044 /***************************************************************************
   5045  *
   5046  * Function         send_groupnavigation_cmd
   5047  *
   5048  * Description      Send Pass-Through command
   5049  *
   5050  * Returns          void
   5051  *
   5052  **************************************************************************/
   5053 static bt_status_t send_groupnavigation_cmd(RawAddress* bd_addr,
   5054                                             uint8_t key_code,
   5055                                             uint8_t key_state) {
   5056   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   5057   rc_transaction_t* p_transaction = NULL;
   5058   BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __func__, key_code,
   5059                    key_state);
   5060   btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr);
   5061 
   5062   CHECK_RC_CONNECTED(p_dev);
   5063 
   5064   if (p_dev->rc_features & BTA_AV_FEAT_RCTG) {
   5065     bt_status_t tran_status = get_transaction(&p_transaction);
   5066     if ((BT_STATUS_SUCCESS == tran_status) && (NULL != p_transaction)) {
   5067       uint8_t buffer[AVRC_PASS_THRU_GROUP_LEN] = {0};
   5068       uint8_t* start = buffer;
   5069       UINT24_TO_BE_STREAM(start, AVRC_CO_METADATA);
   5070       *(start)++ = 0;
   5071       UINT8_TO_BE_STREAM(start, key_code);
   5072       BTA_AvRemoteVendorUniqueCmd(p_dev->rc_handle, p_transaction->lbl,
   5073                                   (tBTA_AV_STATE)key_state, buffer,
   5074                                   AVRC_PASS_THRU_GROUP_LEN);
   5075       status = BT_STATUS_SUCCESS;
   5076       BTIF_TRACE_DEBUG("%s: succesfully sent group_navigation command to BTA",
   5077                        __func__);
   5078     } else {
   5079       status = BT_STATUS_FAIL;
   5080       BTIF_TRACE_DEBUG("%s: error in fetching transaction", __func__);
   5081     }
   5082   } else {
   5083     status = BT_STATUS_FAIL;
   5084     BTIF_TRACE_DEBUG("%s: feature not supported", __func__);
   5085   }
   5086   return (bt_status_t)status;
   5087 }
   5088 
   5089 /***************************************************************************
   5090  *
   5091  * Function         send_passthrough_cmd
   5092  *
   5093  * Description      Send Pass-Through command
   5094  *
   5095  * Returns          void
   5096  *
   5097  **************************************************************************/
   5098 static bt_status_t send_passthrough_cmd(RawAddress* bd_addr, uint8_t key_code,
   5099                                         uint8_t key_state) {
   5100   tAVRC_STS status = BT_STATUS_UNSUPPORTED;
   5101   btif_rc_device_cb_t* p_dev = NULL;
   5102   BTIF_TRACE_ERROR("%s: calling btif_rc_get_device_by_bda", __func__);
   5103   p_dev = btif_rc_get_device_by_bda(bd_addr);
   5104 
   5105   CHECK_RC_CONNECTED(p_dev);
   5106 
   5107   rc_transaction_t* p_transaction = NULL;
   5108   BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __func__, key_code,
   5109                    key_state);
   5110   if (p_dev->rc_features & BTA_AV_FEAT_RCTG) {
   5111     bt_status_t tran_status = get_transaction(&p_transaction);
   5112     if (BT_STATUS_SUCCESS == tran_status && NULL != p_transaction) {
   5113       BTA_AvRemoteCmd(p_dev->rc_handle, p_transaction->lbl,
   5114                       (tBTA_AV_RC)key_code, (tBTA_AV_STATE)key_state);
   5115       status = BT_STATUS_SUCCESS;
   5116       BTIF_TRACE_DEBUG("%s: succesfully sent passthrough command to BTA",
   5117                        __func__);
   5118     } else {
   5119       status = BT_STATUS_FAIL;
   5120       BTIF_TRACE_DEBUG("%s: error in fetching transaction", __func__);
   5121     }
   5122   } else {
   5123     status = BT_STATUS_FAIL;
   5124     BTIF_TRACE_DEBUG("%s: feature not supported", __func__);
   5125   }
   5126   return (bt_status_t)status;
   5127 }
   5128 
   5129 static const btrc_interface_t bt_rc_interface = {
   5130     sizeof(bt_rc_interface),
   5131     init,
   5132     get_play_status_rsp,
   5133     NULL, /* list_player_app_attr_rsp */
   5134     NULL, /* list_player_app_value_rsp */
   5135     NULL, /* get_player_app_value_rsp */
   5136     NULL, /* get_player_app_attr_text_rsp */
   5137     NULL, /* get_player_app_value_text_rsp */
   5138     get_element_attr_rsp,
   5139     NULL, /* set_player_app_value_rsp */
   5140     register_notification_rsp,
   5141     set_volume,
   5142     set_addressed_player_rsp,
   5143     set_browsed_player_rsp,
   5144     get_folder_items_list_rsp,
   5145     change_path_rsp,
   5146     get_item_attr_rsp,
   5147     play_item_rsp,
   5148     get_total_num_of_items_rsp,
   5149     search_rsp,
   5150     add_to_now_playing_rsp,
   5151     cleanup,
   5152 };
   5153 
   5154 static const btrc_ctrl_interface_t bt_rc_ctrl_interface = {
   5155     sizeof(bt_rc_ctrl_interface),
   5156     init_ctrl,
   5157     send_passthrough_cmd,
   5158     send_groupnavigation_cmd,
   5159     change_player_app_setting,
   5160     play_item_cmd,
   5161     get_playback_state_cmd,
   5162     get_now_playing_list_cmd,
   5163     get_folder_list_cmd,
   5164     get_player_list_cmd,
   5165     change_folder_path_cmd,
   5166     set_browsed_player_cmd,
   5167     set_addressed_player_cmd,
   5168     set_volume_rsp,
   5169     volume_change_notification_rsp,
   5170     cleanup_ctrl,
   5171 };
   5172 
   5173 /*******************************************************************************
   5174  *
   5175  * Function         btif_rc_get_interface
   5176  *
   5177  * Description      Get the AVRCP Target callback interface
   5178  *
   5179  * Returns          btrc_interface_t
   5180  *
   5181  ******************************************************************************/
   5182 const btrc_interface_t* btif_rc_get_interface(void) {
   5183   BTIF_TRACE_EVENT("%s: ", __func__);
   5184   return &bt_rc_interface;
   5185 }
   5186 
   5187 /*******************************************************************************
   5188  *
   5189  * Function         btif_rc_ctrl_get_interface
   5190  *
   5191  * Description      Get the AVRCP Controller callback interface
   5192  *
   5193  * Returns          btrc_ctrl_interface_t
   5194  *
   5195  ******************************************************************************/
   5196 const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface(void) {
   5197   BTIF_TRACE_EVENT("%s: ", __func__);
   5198   return &bt_rc_ctrl_interface;
   5199 }
   5200 
   5201 /*******************************************************************************
   5202  *      Function         initialize_transaction
   5203  *
   5204  *      Description    Initializes fields of the transaction structure
   5205  *
   5206  *      Returns          void
   5207  ******************************************************************************/
   5208 static void initialize_transaction(int lbl) {
   5209   std::unique_lock<std::recursive_mutex>(device.lbllock);
   5210   if (lbl < MAX_TRANSACTIONS_PER_SESSION) {
   5211     if (alarm_is_scheduled(device.transaction[lbl].txn_timer)) {
   5212       clear_cmd_timeout(lbl);
   5213     }
   5214     device.transaction[lbl].lbl = lbl;
   5215     device.transaction[lbl].in_use = false;
   5216     device.transaction[lbl].handle = 0;
   5217   }
   5218 }
   5219 
   5220 /*******************************************************************************
   5221  *      Function         lbl_init
   5222  *
   5223  *      Description    Initializes label structures and mutexes.
   5224  *
   5225  *      Returns         void
   5226  ******************************************************************************/
   5227 void lbl_init() {
   5228   memset(&device.transaction, 0, sizeof(device.transaction));
   5229   init_all_transactions();
   5230 }
   5231 
   5232 /*******************************************************************************
   5233  *
   5234  * Function         init_all_transactions
   5235  *
   5236  * Description    Initializes all transactions
   5237  *
   5238  * Returns          void
   5239  ******************************************************************************/
   5240 void init_all_transactions() {
   5241   uint8_t txn_indx = 0;
   5242   for (txn_indx = 0; txn_indx < MAX_TRANSACTIONS_PER_SESSION; txn_indx++) {
   5243     initialize_transaction(txn_indx);
   5244   }
   5245 }
   5246 
   5247 /*******************************************************************************
   5248  *
   5249  * Function         get_transaction_by_lbl
   5250  *
   5251  * Description    Will return a transaction based on the label. If not inuse
   5252  *                     will return an error.
   5253  *
   5254  * Returns          bt_status_t
   5255  ******************************************************************************/
   5256 rc_transaction_t* get_transaction_by_lbl(uint8_t lbl) {
   5257   rc_transaction_t* transaction = NULL;
   5258   std::unique_lock<std::recursive_mutex> lock(device.lbllock);
   5259 
   5260   /* Determine if this is a valid label */
   5261   if (lbl < MAX_TRANSACTIONS_PER_SESSION) {
   5262     if (false == device.transaction[lbl].in_use) {
   5263       transaction = NULL;
   5264     } else {
   5265       transaction = &(device.transaction[lbl]);
   5266       BTIF_TRACE_DEBUG("%s: Got transaction.label: %d", __func__, lbl);
   5267     }
   5268   }
   5269 
   5270   return transaction;
   5271 }
   5272 
   5273 /*******************************************************************************
   5274  *
   5275  * Function         get_transaction
   5276  *
   5277  * Description    Obtains the transaction details.
   5278  *
   5279  * Returns          bt_status_t
   5280  ******************************************************************************/
   5281 
   5282 static bt_status_t get_transaction(rc_transaction_t** ptransaction) {
   5283   std::unique_lock<std::recursive_mutex> lock(device.lbllock);
   5284 
   5285   // Check for unused transactions
   5286   for (uint8_t i = 0; i < MAX_TRANSACTIONS_PER_SESSION; i++) {
   5287     if (false == device.transaction[i].in_use) {
   5288       BTIF_TRACE_DEBUG("%s: Got transaction.label: %d", __func__,
   5289                        device.transaction[i].lbl);
   5290       device.transaction[i].in_use = true;
   5291       *ptransaction = &(device.transaction[i]);
   5292       return BT_STATUS_SUCCESS;
   5293     }
   5294   }
   5295   return BT_STATUS_NOMEM;
   5296 }
   5297 
   5298 /*******************************************************************************
   5299  *
   5300  * Function         release_transaction
   5301  *
   5302  * Description    Will release a transaction for reuse
   5303  *
   5304  * Returns          bt_status_t
   5305  ******************************************************************************/
   5306 void release_transaction(uint8_t lbl) {
   5307   BTIF_TRACE_DEBUG("%s %d", __func__, lbl);
   5308   rc_transaction_t* transaction = get_transaction_by_lbl(lbl);
   5309 
   5310   /* If the transaction is in use... */
   5311   if (transaction != NULL) {
   5312     BTIF_TRACE_DEBUG("%s: lbl: %d", __func__, lbl);
   5313     initialize_transaction(lbl);
   5314   }
   5315 }
   5316 
   5317 /*******************************************************************************
   5318  *      Function       sleep_ms
   5319  *
   5320  *      Description    Sleep the calling thread unconditionally for
   5321  *                     |timeout_ms| milliseconds.
   5322  *
   5323  *      Returns        void
   5324  ******************************************************************************/
   5325 static void sleep_ms(period_ms_t timeout_ms) {
   5326   struct timespec delay;
   5327   delay.tv_sec = timeout_ms / 1000;
   5328   delay.tv_nsec = 1000 * 1000 * (timeout_ms % 1000);
   5329 
   5330   OSI_NO_INTR(nanosleep(&delay, &delay));
   5331 }
   5332 
   5333 static bool absolute_volume_disabled() {
   5334   char volume_disabled[PROPERTY_VALUE_MAX] = {0};
   5335   osi_property_get("persist.bluetooth.disableabsvol", volume_disabled, "false");
   5336   if (strncmp(volume_disabled, "true", 4) == 0) {
   5337     BTIF_TRACE_WARNING("%s: Absolute volume disabled by property", __func__);
   5338     return true;
   5339   }
   5340   return false;
   5341 }
   5342