Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /************************************************************************************
     20  *
     21  *  Filename:      btif_core.c
     22  *
     23  *  Description:   Contains core functionality related to interfacing between
     24  *                 Bluetooth HAL and BTE core stack.
     25  *
     26  ***********************************************************************************/
     27 
     28 #include <stdlib.h>
     29 #include <hardware/bluetooth.h>
     30 #include <string.h>
     31 #include <sys/types.h>
     32 #include <sys/stat.h>
     33 #include <fcntl.h>
     34 #include <dirent.h>
     35 #include <ctype.h>
     36 #include <cutils/properties.h>
     37 
     38 #define LOG_TAG "BTIF_CORE"
     39 #include "btif_api.h"
     40 #include "bta_api.h"
     41 #include "gki.h"
     42 #include "btu.h"
     43 #include "bte.h"
     44 #include "bd.h"
     45 #include "btif_av.h"
     46 #include "btif_storage.h"
     47 #include "btif_util.h"
     48 #include "btif_sock.h"
     49 #include "btif_pan.h"
     50 #include "btif_profile_queue.h"
     51 #include "btif_config.h"
     52 /************************************************************************************
     53 **  Constants & Macros
     54 ************************************************************************************/
     55 
     56 #ifndef BTIF_TASK_STACK_SIZE
     57 #define BTIF_TASK_STACK_SIZE       0x2000         /* In bytes */
     58 #endif
     59 
     60 #ifndef BTE_DID_CONF_FILE
     61 #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
     62 #endif
     63 
     64 #define BTIF_TASK_STR        ((INT8 *) "BTIF")
     65 
     66 /************************************************************************************
     67 **  Local type definitions
     68 ************************************************************************************/
     69 
     70 /* These type definitions are used when passing data from the HAL to BTIF context
     71 *  in the downstream path for the adapter and remote_device property APIs */
     72 
     73 typedef struct {
     74   bt_bdaddr_t bd_addr;
     75   bt_property_type_t type;
     76 } btif_storage_read_t;
     77 
     78 typedef struct {
     79   bt_bdaddr_t bd_addr;
     80   bt_property_t prop;
     81 } btif_storage_write_t;
     82 
     83 typedef union {
     84   btif_storage_read_t read_req;
     85   btif_storage_write_t write_req;
     86 } btif_storage_req_t;
     87 
     88 typedef enum {
     89     BTIF_CORE_STATE_DISABLED = 0,
     90     BTIF_CORE_STATE_ENABLING,
     91     BTIF_CORE_STATE_ENABLED,
     92     BTIF_CORE_STATE_DISABLING
     93 } btif_core_state_t;
     94 
     95 /************************************************************************************
     96 **  Static variables
     97 ************************************************************************************/
     98 
     99 bt_bdaddr_t btif_local_bd_addr;
    100 
    101 static UINT32 btif_task_stack[(BTIF_TASK_STACK_SIZE + 3) / 4];
    102 
    103 /* holds main adapter state */
    104 static btif_core_state_t btif_core_state = BTIF_CORE_STATE_DISABLED;
    105 
    106 static int btif_shutdown_pending = 0;
    107 static tBTA_SERVICE_MASK btif_enabled_services = 0;
    108 
    109 /*
    110 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
    111 * function in DUT mode.
    112 *
    113 * To set this, the btif_init_bluetooth needs to be called with argument as 1
    114 */
    115 static UINT8 btif_dut_mode = 0;
    116 
    117 /************************************************************************************
    118 **  Static functions
    119 ************************************************************************************/
    120 static bt_status_t btif_associate_evt(void);
    121 static bt_status_t btif_disassociate_evt(void);
    122 
    123 /* sends message to btif task */
    124 static void btif_sendmsg(void *p_msg);
    125 
    126 /************************************************************************************
    127 **  Externs
    128 ************************************************************************************/
    129 extern void bte_load_did_conf(const char *p_path);
    130 
    131 /** TODO: Move these to _common.h */
    132 void bte_main_boot_entry(void);
    133 void bte_main_enable();
    134 void bte_main_disable(void);
    135 void bte_main_shutdown(void);
    136 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
    137 void bte_main_enable_lpm(BOOLEAN enable);
    138 #endif
    139 void bte_main_postload_cfg(void);
    140 void btif_dm_execute_service_request(UINT16 event, char *p_param);
    141 #ifdef BTIF_DM_OOB_TEST
    142 void btif_dm_load_local_oob(void);
    143 #endif
    144 void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
    145 
    146 /************************************************************************************
    147 **  Functions
    148 ************************************************************************************/
    149 
    150 
    151 /*****************************************************************************
    152 **   Context switching functions
    153 *****************************************************************************/
    154 
    155 
    156 /*******************************************************************************
    157 **
    158 ** Function         btif_context_switched
    159 **
    160 ** Description      Callback used to execute transferred context callback
    161 **
    162 **                  p_msg : message to be executed in btif context
    163 **
    164 ** Returns          void
    165 **
    166 *******************************************************************************/
    167 
    168 static void btif_context_switched(void *p_msg)
    169 {
    170     tBTIF_CONTEXT_SWITCH_CBACK *p;
    171 
    172     BTIF_TRACE_VERBOSE0("btif_context_switched");
    173 
    174     p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
    175 
    176     /* each callback knows how to parse the data */
    177     if (p->p_cb)
    178         p->p_cb(p->event, p->p_param);
    179 }
    180 
    181 
    182 /*******************************************************************************
    183 **
    184 ** Function         btif_transfer_context
    185 **
    186 ** Description      This function switches context to btif task
    187 **
    188 **                  p_cback   : callback used to process message in btif context
    189 **                  event     : event id of message
    190 **                  p_params  : parameter area passed to callback (copied)
    191 **                  param_len : length of parameter area
    192 **                  p_copy_cback : If set this function will be invoked for deep copy
    193 **
    194 ** Returns          void
    195 **
    196 *******************************************************************************/
    197 
    198 bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
    199 {
    200     tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
    201 
    202     BTIF_TRACE_VERBOSE2("btif_transfer_context event %d, len %d", event, param_len);
    203 
    204     /* allocate and send message that will be executed in btif context */
    205     if ((p_msg = (tBTIF_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
    206     {
    207         p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
    208         p_msg->p_cb = p_cback;
    209 
    210         p_msg->event = event;                         /* callback event */
    211 
    212         /* check if caller has provided a copy callback to do the deep copy */
    213         if (p_copy_cback)
    214         {
    215             p_copy_cback(event, p_msg->p_param, p_params);
    216         }
    217         else if (p_params)
    218         {
    219             memcpy(p_msg->p_param, p_params, param_len);  /* callback parameter data */
    220         }
    221 
    222         btif_sendmsg(p_msg);
    223         return BT_STATUS_SUCCESS;
    224     }
    225     else
    226     {
    227         /* let caller deal with a failed allocation */
    228         return BT_STATUS_NOMEM;
    229     }
    230 }
    231 
    232 /*******************************************************************************
    233 **
    234 ** Function         btif_is_dut_mode
    235 **
    236 ** Description      checks if BTIF is currently in DUT mode
    237 **
    238 ** Returns          1 if test mode, otherwize 0
    239 **
    240 *******************************************************************************/
    241 
    242 UINT8 btif_is_dut_mode(void)
    243 {
    244     return (btif_dut_mode == 1);
    245 }
    246 
    247 /*******************************************************************************
    248 **
    249 ** Function         btif_is_enabled
    250 **
    251 ** Description      checks if main adapter is fully enabled
    252 **
    253 ** Returns          1 if fully enabled, otherwize 0
    254 **
    255 *******************************************************************************/
    256 
    257 int btif_is_enabled(void)
    258 {
    259     return ((!btif_is_dut_mode()) && (btif_core_state == BTIF_CORE_STATE_ENABLED));
    260 }
    261 
    262 /*******************************************************************************
    263 **
    264 ** Function         btif_task
    265 **
    266 ** Description      BTIF task handler managing all messages being passed
    267 **                  Bluetooth HAL and BTA.
    268 **
    269 ** Returns          void
    270 **
    271 *******************************************************************************/
    272 
    273 static void btif_task(UINT32 params)
    274 {
    275     UINT16   event;
    276     BT_HDR   *p_msg;
    277 
    278     BTIF_TRACE_DEBUG0("btif task starting");
    279 
    280     btif_associate_evt();
    281 
    282     for(;;)
    283     {
    284         /* wait for specified events */
    285         event = GKI_wait(0xFFFF, 0);
    286 
    287         /*
    288          * Wait for the trigger to init chip and stack. This trigger will
    289          * be received by btu_task once the UART is opened and ready
    290          */
    291         if (event == BT_EVT_TRIGGER_STACK_INIT)
    292         {
    293             BTIF_TRACE_DEBUG0("btif_task: received trigger stack init event");
    294             #if (BLE_INCLUDED == TRUE)
    295             btif_dm_load_ble_local_keys();
    296             #endif
    297             BTA_EnableBluetooth(bte_dm_evt);
    298         }
    299 
    300         /*
    301          * Failed to initialize controller hardware, reset state and bring
    302          * down all threads
    303          */
    304         if (event == BT_EVT_HARDWARE_INIT_FAIL)
    305         {
    306             BTIF_TRACE_DEBUG0("btif_task: hardware init failed");
    307             bte_main_disable();
    308             btif_queue_release();
    309             GKI_task_self_cleanup(BTIF_TASK);
    310             bte_main_shutdown();
    311             btif_dut_mode = 0;
    312             btif_core_state = BTIF_CORE_STATE_DISABLED;
    313             HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
    314             break;
    315         }
    316 
    317         if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
    318             break;
    319 
    320         if(event & TASK_MBOX_1_EVT_MASK)
    321         {
    322             while((p_msg = GKI_read_mbox(BTU_BTIF_MBOX)) != NULL)
    323             {
    324                 BTIF_TRACE_VERBOSE1("btif task fetched event %x", p_msg->event);
    325 
    326                 switch (p_msg->event)
    327                 {
    328                     case BT_EVT_CONTEXT_SWITCH_EVT:
    329                         btif_context_switched(p_msg);
    330                         break;
    331                     default:
    332                         BTIF_TRACE_ERROR1("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
    333                         break;
    334                 }
    335 
    336                 GKI_freebuf(p_msg);
    337             }
    338         }
    339     }
    340 
    341     btif_disassociate_evt();
    342 
    343     BTIF_TRACE_DEBUG0("btif task exiting");
    344 }
    345 
    346 
    347 /*******************************************************************************
    348 **
    349 ** Function         btif_sendmsg
    350 **
    351 ** Description      Sends msg to BTIF task
    352 **
    353 ** Returns          void
    354 **
    355 *******************************************************************************/
    356 
    357 void btif_sendmsg(void *p_msg)
    358 {
    359     GKI_send_msg(BTIF_TASK, BTU_BTIF_MBOX, p_msg);
    360 }
    361 
    362 static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
    363 {
    364     char val[256];
    365     uint8_t valid_bda = FALSE;
    366     int val_size = 0;
    367     const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
    368 
    369     /* Get local bdaddr storage path from property */
    370     if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
    371     {
    372         int addr_fd;
    373 
    374         BTIF_TRACE_DEBUG1("local bdaddr is stored in %s", val);
    375 
    376         if ((addr_fd = open(val, O_RDONLY)) != -1)
    377         {
    378             memset(val, 0, sizeof(val));
    379             read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN);
    380             str2bd(val, local_addr);
    381             /* If this is not a reserved/special bda, then use it */
    382             if (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)
    383             {
    384                 valid_bda = TRUE;
    385                 BTIF_TRACE_DEBUG6("Got Factory BDA %02X:%02X:%02X:%02X:%02X:%02X",
    386                     local_addr->address[0], local_addr->address[1], local_addr->address[2],
    387                     local_addr->address[3], local_addr->address[4], local_addr->address[5]);
    388             }
    389 
    390             close(addr_fd);
    391         }
    392     }
    393 
    394     if(!valid_bda)
    395     {
    396         val_size = sizeof(val);
    397         if(btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
    398         {
    399             str2bd(val, local_addr);
    400             BTIF_TRACE_DEBUG1("local bdaddr from bt_config.xml is  %s", val);
    401             return;
    402         }
    403      }
    404 
    405     /* No factory BDADDR found. Look for previously generated random BDA */
    406     if ((!valid_bda) && \
    407         (property_get(PERSIST_BDADDR_PROPERTY, val, NULL)))
    408     {
    409         str2bd(val, local_addr);
    410         valid_bda = TRUE;
    411         BTIF_TRACE_DEBUG6("Got prior random BDA %02X:%02X:%02X:%02X:%02X:%02X",
    412             local_addr->address[0], local_addr->address[1], local_addr->address[2],
    413             local_addr->address[3], local_addr->address[4], local_addr->address[5]);
    414     }
    415 
    416     /* Generate new BDA if necessary */
    417     if (!valid_bda)
    418     {
    419         bdstr_t bdstr;
    420         /* Seed the random number generator */
    421         srand((unsigned int) (time(0)));
    422 
    423         /* No autogen BDA. Generate one now. */
    424         local_addr->address[0] = 0x22;
    425         local_addr->address[1] = 0x22;
    426         local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
    427         local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
    428         local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
    429         local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
    430 
    431         /* Convert to ascii, and store as a persistent property */
    432         bd2str(local_addr, &bdstr);
    433 
    434         BTIF_TRACE_DEBUG2("No preset BDA. Generating BDA: %s for prop %s",
    435              (char*)bdstr, PERSIST_BDADDR_PROPERTY);
    436 
    437         if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
    438             BTIF_TRACE_ERROR1("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
    439     }
    440 
    441     //save the bd address to config file
    442     bdstr_t bdstr;
    443     bd2str(local_addr, &bdstr);
    444     val_size = sizeof(val);
    445     if (btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
    446     {
    447         if (strcmp(bdstr, val) ==0)
    448         {
    449             // BDA is already present in the config file.
    450             return;
    451         }
    452     }
    453     btif_config_set_str("Local", "Adapter", "Address", bdstr);
    454     btif_config_save();
    455 }
    456 
    457 /*****************************************************************************
    458 **
    459 **   btif core api functions
    460 **
    461 *****************************************************************************/
    462 
    463 /*******************************************************************************
    464 **
    465 ** Function         btif_init_bluetooth
    466 **
    467 ** Description      Creates BTIF task and prepares BT scheduler for startup
    468 **
    469 ** Returns          bt_status_t
    470 **
    471 *******************************************************************************/
    472 
    473 bt_status_t btif_init_bluetooth()
    474 {
    475     UINT8 status;
    476     btif_config_init();
    477     bte_main_boot_entry();
    478 
    479     /* As part of the init, fetch the local BD ADDR */
    480     memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
    481     btif_fetch_local_bdaddr(&btif_local_bd_addr);
    482 
    483     /* start btif task */
    484     status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
    485                 (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
    486                 sizeof(btif_task_stack));
    487 
    488     if (status != GKI_SUCCESS)
    489         return BT_STATUS_FAIL;
    490 
    491     return BT_STATUS_SUCCESS;
    492 }
    493 
    494 /*******************************************************************************
    495 **
    496 ** Function         btif_associate_evt
    497 **
    498 ** Description      Event indicating btif_task is up
    499 **                  Attach btif_task to JVM
    500 **
    501 ** Returns          void
    502 **
    503 *******************************************************************************/
    504 
    505 static bt_status_t btif_associate_evt(void)
    506 {
    507     BTIF_TRACE_DEBUG1("%s: notify ASSOCIATE_JVM", __FUNCTION__);
    508     HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
    509 
    510     return BT_STATUS_SUCCESS;
    511 }
    512 
    513 
    514 /*******************************************************************************
    515 **
    516 ** Function         btif_enable_bluetooth
    517 **
    518 ** Description      Performs chip power on and kickstarts OS scheduler
    519 **
    520 ** Returns          bt_status_t
    521 **
    522 *******************************************************************************/
    523 
    524 bt_status_t btif_enable_bluetooth(void)
    525 {
    526     BTIF_TRACE_DEBUG0("BTIF ENABLE BLUETOOTH");
    527 
    528     if (btif_core_state != BTIF_CORE_STATE_DISABLED)
    529     {
    530         ALOGD("not disabled\n");
    531         return BT_STATUS_DONE;
    532     }
    533 
    534     btif_core_state = BTIF_CORE_STATE_ENABLING;
    535 
    536     /* Create the GKI tasks and run them */
    537     bte_main_enable();
    538 
    539     return BT_STATUS_SUCCESS;
    540 }
    541 
    542 
    543 /*******************************************************************************
    544 **
    545 ** Function         btif_enable_bluetooth_evt
    546 **
    547 ** Description      Event indicating bluetooth enable is completed
    548 **                  Notifies HAL user with updated adapter state
    549 **
    550 ** Returns          void
    551 **
    552 *******************************************************************************/
    553 
    554 void btif_enable_bluetooth_evt(tBTA_STATUS status, BD_ADDR local_bd)
    555 {
    556     bt_bdaddr_t bd_addr;
    557     bdstr_t bdstr;
    558 
    559     bdcpy(bd_addr.address, local_bd);
    560     BTIF_TRACE_DEBUG3("%s: status %d, local bd [%s]", __FUNCTION__, status,
    561                                                      bd2str(&bd_addr, &bdstr));
    562 
    563     if (bdcmp(btif_local_bd_addr.address,local_bd))
    564     {
    565         bdstr_t buf;
    566         bt_property_t prop;
    567 
    568         /**
    569          * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
    570          * This could be because the factory BDADDR was stored separatley in
    571          * the Controller's non-volatile memory rather than in device's file
    572          * system.
    573          **/
    574         BTIF_TRACE_WARNING0("***********************************************");
    575         BTIF_TRACE_WARNING6("BTIF init BDA was %02X:%02X:%02X:%02X:%02X:%02X",
    576             btif_local_bd_addr.address[0], btif_local_bd_addr.address[1],
    577             btif_local_bd_addr.address[2], btif_local_bd_addr.address[3],
    578             btif_local_bd_addr.address[4], btif_local_bd_addr.address[5]);
    579         BTIF_TRACE_WARNING6("Controller BDA is %02X:%02X:%02X:%02X:%02X:%02X",
    580             local_bd[0], local_bd[1], local_bd[2],
    581             local_bd[3], local_bd[4], local_bd[5]);
    582         BTIF_TRACE_WARNING0("***********************************************");
    583 
    584         bdcpy(btif_local_bd_addr.address, local_bd);
    585 
    586         //save the bd address to config file
    587         bd2str(&btif_local_bd_addr, &buf);
    588         btif_config_set_str("Local", "Adapter", "Address", buf);
    589         btif_config_save();
    590 
    591         //fire HAL callback for property change
    592         memcpy(buf, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
    593         prop.type = BT_PROPERTY_BDADDR;
    594         prop.val = (void*)buf;
    595         prop.len = sizeof(bt_bdaddr_t);
    596         HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
    597     }
    598 
    599     bte_main_postload_cfg();
    600 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
    601     bte_main_enable_lpm(TRUE);
    602 #endif
    603     /* add passing up bd address as well ? */
    604 
    605     /* callback to HAL */
    606     if (status == BTA_SUCCESS)
    607     {
    608         /* initialize a2dp service */
    609         btif_av_init();
    610 
    611         /* init rfcomm & l2cap api */
    612         btif_sock_init();
    613 
    614         /* init pan */
    615         btif_pan_init();
    616 
    617         /* load did configuration */
    618         bte_load_did_conf(BTE_DID_CONF_FILE);
    619 
    620 #ifdef BTIF_DM_OOB_TEST
    621         btif_dm_load_local_oob();
    622 #endif
    623         /* now fully enabled, update state */
    624         btif_core_state = BTIF_CORE_STATE_ENABLED;
    625 
    626         HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON);
    627     }
    628     else
    629     {
    630         /* cleanup rfcomm & l2cap api */
    631         btif_sock_cleanup();
    632 
    633         btif_pan_cleanup();
    634 
    635         /* we failed to enable, reset state */
    636         btif_core_state = BTIF_CORE_STATE_DISABLED;
    637 
    638         HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
    639     }
    640 }
    641 
    642 /*******************************************************************************
    643 **
    644 ** Function         btif_disable_bluetooth
    645 **
    646 ** Description      Inititates shutdown of Bluetooth system.
    647 **                  Any active links will be dropped and device entering
    648 **                  non connectable/discoverable mode
    649 **
    650 ** Returns          void
    651 **
    652 *******************************************************************************/
    653 bt_status_t btif_disable_bluetooth(void)
    654 {
    655     tBTA_STATUS status;
    656 
    657     if (!btif_is_enabled())
    658     {
    659         BTIF_TRACE_ERROR0("btif_disable_bluetooth : not yet enabled");
    660         return BT_STATUS_NOT_READY;
    661     }
    662 
    663     BTIF_TRACE_DEBUG0("BTIF DISABLE BLUETOOTH");
    664 
    665     btif_dm_on_disable();
    666     btif_core_state = BTIF_CORE_STATE_DISABLING;
    667 
    668     /* cleanup rfcomm & l2cap api */
    669     btif_sock_cleanup();
    670 
    671     btif_pan_cleanup();
    672 
    673     status = BTA_DisableBluetooth();
    674 
    675     btif_config_flush();
    676 
    677     if (status != BTA_SUCCESS)
    678     {
    679         BTIF_TRACE_ERROR1("disable bt failed (%d)", status);
    680 
    681         /* reset the original state to allow attempting disable again */
    682         btif_core_state = BTIF_CORE_STATE_ENABLED;
    683 
    684         return BT_STATUS_FAIL;
    685     }
    686     return BT_STATUS_SUCCESS;
    687 }
    688 
    689 /*******************************************************************************
    690 **
    691 ** Function         btif_disable_bluetooth_evt
    692 **
    693 ** Description      Event notifying BT disable is now complete.
    694 **                  Terminates main stack tasks and notifies HAL
    695 **                  user with updated BT state.
    696 **
    697 ** Returns          void
    698 **
    699 *******************************************************************************/
    700 
    701 void btif_disable_bluetooth_evt(void)
    702 {
    703     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    704 
    705 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
    706     bte_main_enable_lpm(FALSE);
    707 #endif
    708 
    709     bte_main_disable();
    710 
    711     /* update local state */
    712     btif_core_state = BTIF_CORE_STATE_DISABLED;
    713 
    714     /* callback to HAL */
    715     HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
    716 
    717     if (btif_shutdown_pending)
    718     {
    719         BTIF_TRACE_DEBUG1("%s: calling btif_shutdown_bluetooth", __FUNCTION__);
    720         btif_shutdown_bluetooth();
    721     }
    722 }
    723 
    724 
    725 /*******************************************************************************
    726 **
    727 ** Function         btif_shutdown_bluetooth
    728 **
    729 ** Description      Finalizes BT scheduler shutdown and terminates BTIF
    730 **                  task.
    731 **
    732 ** Returns          void
    733 **
    734 *******************************************************************************/
    735 
    736 bt_status_t btif_shutdown_bluetooth(void)
    737 {
    738     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    739 
    740     if (btif_is_enabled())
    741     {
    742         BTIF_TRACE_WARNING0("shutdown while still enabled, initiate disable");
    743 
    744         /* shutdown called prior to disabling, initiate disable */
    745         btif_disable_bluetooth();
    746         btif_shutdown_pending = 1;
    747         return BT_STATUS_NOT_READY;
    748     }
    749 
    750     btif_shutdown_pending = 0;
    751 
    752     if (btif_core_state == BTIF_CORE_STATE_ENABLING)
    753     {
    754         // Java layer abort BT ENABLING, could be due to ENABLE TIMEOUT
    755         // Direct call from cleanup()@bluetooth.c
    756         // bring down HCI/Vendor lib
    757         bte_main_disable();
    758         btif_core_state = BTIF_CORE_STATE_DISABLED;
    759         HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
    760     }
    761 
    762     GKI_destroy_task(BTIF_TASK);
    763     btif_queue_release();
    764     bte_main_shutdown();
    765 
    766     btif_dut_mode = 0;
    767 
    768     BTIF_TRACE_DEBUG1("%s done", __FUNCTION__);
    769 
    770     return BT_STATUS_SUCCESS;
    771 }
    772 
    773 
    774 /*******************************************************************************
    775 **
    776 ** Function         btif_disassociate_evt
    777 **
    778 ** Description      Event indicating btif_task is going down
    779 **                  Detach btif_task to JVM
    780 **
    781 ** Returns          void
    782 **
    783 *******************************************************************************/
    784 
    785 static bt_status_t btif_disassociate_evt(void)
    786 {
    787     BTIF_TRACE_DEBUG1("%s: notify DISASSOCIATE_JVM", __FUNCTION__);
    788 
    789     HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
    790 
    791     /* shutdown complete, all events notified and we reset HAL callbacks */
    792     bt_hal_cbacks = NULL;
    793 
    794     return BT_STATUS_SUCCESS;
    795 }
    796 
    797 /****************************************************************************
    798 **
    799 **   BTIF Test Mode APIs
    800 **
    801 *****************************************************************************/
    802 /*******************************************************************************
    803 **
    804 ** Function         btif_dut_mode_cback
    805 **
    806 ** Description     Callback invoked on completion of vendor specific test mode command
    807 **
    808 ** Returns          None
    809 **
    810 *******************************************************************************/
    811 static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
    812 {
    813     /* For now nothing to be done. */
    814 }
    815 
    816 /*******************************************************************************
    817 **
    818 ** Function         btif_dut_mode_configure
    819 **
    820 ** Description      Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
    821 **                       test mode
    822 **
    823 ** Returns          BT_STATUS_SUCCESS on success
    824 **
    825 *******************************************************************************/
    826 bt_status_t btif_dut_mode_configure(uint8_t enable)
    827 {
    828     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    829 
    830     if (btif_core_state != BTIF_CORE_STATE_ENABLED) {
    831         BTIF_TRACE_ERROR0("btif_dut_mode_configure : Bluetooth not enabled");
    832         return BT_STATUS_NOT_READY;
    833     }
    834 
    835     btif_dut_mode = enable;
    836     if (enable == 1) {
    837         BTA_EnableTestMode();
    838     } else {
    839         BTA_DisableTestMode();
    840     }
    841     return BT_STATUS_SUCCESS;
    842 }
    843 
    844 /*******************************************************************************
    845 **
    846 ** Function         btif_dut_mode_send
    847 **
    848 ** Description     Sends a HCI Vendor specific command to the controller
    849 **
    850 ** Returns          BT_STATUS_SUCCESS on success
    851 **
    852 *******************************************************************************/
    853 bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
    854 {
    855     /* TODO: Check that opcode is a vendor command group */
    856     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    857     if (!btif_is_dut_mode()) {
    858          BTIF_TRACE_ERROR0("Bluedroid HAL needs to be init with test_mode set to 1.");
    859          return BT_STATUS_FAIL;
    860     }
    861     BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
    862     return BT_STATUS_SUCCESS;
    863 }
    864 
    865 /*****************************************************************************
    866 **
    867 **   btif api adapter property functions
    868 **
    869 *****************************************************************************/
    870 
    871 static bt_status_t btif_in_get_adapter_properties(void)
    872 {
    873     bt_property_t properties[6];
    874     uint32_t num_props;
    875 
    876     bt_bdaddr_t addr;
    877     bt_bdname_t name;
    878     bt_scan_mode_t mode;
    879     uint32_t disc_timeout;
    880     bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
    881     bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
    882     num_props = 0;
    883 
    884     /* BD_ADDR */
    885     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
    886                                sizeof(addr), &addr);
    887     btif_storage_get_adapter_property(&properties[num_props]);
    888     num_props++;
    889 
    890     /* BD_NAME */
    891     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
    892                                sizeof(name), &name);
    893     btif_storage_get_adapter_property(&properties[num_props]);
    894     num_props++;
    895 
    896     /* SCAN_MODE */
    897     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
    898                                sizeof(mode), &mode);
    899     btif_storage_get_adapter_property(&properties[num_props]);
    900     num_props++;
    901 
    902     /* DISC_TIMEOUT */
    903     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
    904                                sizeof(disc_timeout), &disc_timeout);
    905     btif_storage_get_adapter_property(&properties[num_props]);
    906     num_props++;
    907 
    908     /* BONDED_DEVICES */
    909     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
    910                                sizeof(bonded_devices), bonded_devices);
    911     btif_storage_get_adapter_property(&properties[num_props]);
    912     num_props++;
    913 
    914     /* LOCAL UUIDs */
    915     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
    916                                sizeof(local_uuids), local_uuids);
    917     btif_storage_get_adapter_property(&properties[num_props]);
    918     num_props++;
    919 
    920     HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
    921                      BT_STATUS_SUCCESS, num_props, properties);
    922 
    923     return BT_STATUS_SUCCESS;
    924 }
    925 
    926 static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
    927 {
    928     bt_property_t remote_properties[8];
    929     uint32_t num_props = 0;
    930 
    931     bt_bdname_t name, alias;
    932     uint32_t cod, devtype;
    933     bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
    934 
    935     memset(remote_properties, 0, sizeof(remote_properties));
    936     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
    937                                sizeof(name), &name);
    938     btif_storage_get_remote_device_property(bd_addr,
    939                                             &remote_properties[num_props]);
    940     num_props++;
    941 
    942     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
    943                                sizeof(alias), &alias);
    944     btif_storage_get_remote_device_property(bd_addr,
    945                                             &remote_properties[num_props]);
    946     num_props++;
    947 
    948     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
    949                                sizeof(cod), &cod);
    950     btif_storage_get_remote_device_property(bd_addr,
    951                                             &remote_properties[num_props]);
    952     num_props++;
    953 
    954     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
    955                                sizeof(devtype), &devtype);
    956     btif_storage_get_remote_device_property(bd_addr,
    957                                             &remote_properties[num_props]);
    958     num_props++;
    959 
    960     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
    961                                sizeof(remote_uuids), remote_uuids);
    962     btif_storage_get_remote_device_property(bd_addr,
    963                                             &remote_properties[num_props]);
    964     num_props++;
    965 
    966     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
    967                      BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
    968 
    969     return BT_STATUS_SUCCESS;
    970 }
    971 
    972 
    973 /*******************************************************************************
    974 **
    975 ** Function         execute_storage_request
    976 **
    977 ** Description      Executes adapter storage request in BTIF context
    978 **
    979 ** Returns          bt_status_t
    980 **
    981 *******************************************************************************/
    982 
    983 static void execute_storage_request(UINT16 event, char *p_param)
    984 {
    985     uint8_t is_local;
    986     int num_entries = 0;
    987     bt_status_t status = BT_STATUS_SUCCESS;
    988 
    989     BTIF_TRACE_EVENT1("execute storage request event : %d", event);
    990 
    991     switch(event)
    992     {
    993         case BTIF_CORE_STORAGE_ADAPTER_WRITE:
    994         {
    995             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
    996             bt_property_t *p_prop = &(p_req->write_req.prop);
    997             BTIF_TRACE_EVENT3("type: %d, len %d, 0x%x", p_prop->type,
    998                                p_prop->len, p_prop->val);
    999 
   1000             status = btif_storage_set_adapter_property(p_prop);
   1001             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
   1002         } break;
   1003 
   1004         case BTIF_CORE_STORAGE_ADAPTER_READ:
   1005         {
   1006             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
   1007             char buf[512];
   1008             bt_property_t prop;
   1009             prop.type = p_req->read_req.type;
   1010             prop.val = (void*)buf;
   1011             prop.len = sizeof(buf);
   1012             status = btif_storage_get_adapter_property(&prop);
   1013             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
   1014         } break;
   1015 
   1016         case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
   1017         {
   1018             status = btif_in_get_adapter_properties();
   1019         } break;
   1020 
   1021         case BTIF_CORE_STORAGE_NOTIFY_STATUS:
   1022         {
   1023             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
   1024         } break;
   1025 
   1026         default:
   1027             BTIF_TRACE_ERROR2("%s invalid event id (%d)", __FUNCTION__, event);
   1028             break;
   1029     }
   1030 }
   1031 
   1032 static void execute_storage_remote_request(UINT16 event, char *p_param)
   1033 {
   1034     bt_status_t status = BT_STATUS_FAIL;
   1035     bt_property_t prop;
   1036 
   1037     BTIF_TRACE_EVENT1("execute storage remote request event : %d", event);
   1038 
   1039     switch (event)
   1040     {
   1041         case BTIF_CORE_STORAGE_REMOTE_READ:
   1042         {
   1043             char buf[1024];
   1044             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
   1045             prop.type = p_req->read_req.type;
   1046             prop.val = (void*) buf;
   1047             prop.len = sizeof(buf);
   1048 
   1049             status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
   1050                                                              &prop);
   1051             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
   1052                             status, &(p_req->read_req.bd_addr), 1, &prop);
   1053         }break;
   1054         case BTIF_CORE_STORAGE_REMOTE_WRITE:
   1055         {
   1056            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
   1057            status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
   1058                                                             &(p_req->write_req.prop));
   1059         }break;
   1060         case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
   1061         {
   1062            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
   1063            btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
   1064         }break;
   1065     }
   1066 }
   1067 
   1068 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
   1069                                     bt_property_t *p_props)
   1070 {
   1071     HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
   1072                      status, num_props, p_props);
   1073 
   1074 }
   1075 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
   1076                                    uint32_t num_props, bt_property_t *p_props)
   1077 {
   1078     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
   1079                      status, remote_addr, num_props, p_props);
   1080 }
   1081 
   1082 /*******************************************************************************
   1083 **
   1084 ** Function         btif_in_storage_request_copy_cb
   1085 **
   1086 ** Description     Switch context callback function to perform the deep copy for
   1087 **                 both the adapter and remote_device property API
   1088 **
   1089 ** Returns          None
   1090 **
   1091 *******************************************************************************/
   1092 static void btif_in_storage_request_copy_cb(UINT16 event,
   1093                                                  char *p_new_buf, char *p_old_buf)
   1094 {
   1095      btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
   1096      btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
   1097 
   1098      BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   1099      switch (event)
   1100      {
   1101          case BTIF_CORE_STORAGE_REMOTE_WRITE:
   1102          case BTIF_CORE_STORAGE_ADAPTER_WRITE:
   1103          {
   1104              bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
   1105              /* Copy the member variables one at a time */
   1106              new_req->write_req.prop.type = old_req->write_req.prop.type;
   1107              new_req->write_req.prop.len = old_req->write_req.prop.len;
   1108 
   1109              new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
   1110              memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
   1111                     old_req->write_req.prop.len);
   1112          }break;
   1113      }
   1114 }
   1115 
   1116 /*******************************************************************************
   1117 **
   1118 ** Function         btif_get_adapter_properties
   1119 **
   1120 ** Description      Fetch all available properties (local & remote)
   1121 **
   1122 ** Returns          bt_status_t
   1123 **
   1124 *******************************************************************************/
   1125 
   1126 bt_status_t btif_get_adapter_properties(void)
   1127 {
   1128     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   1129 
   1130     if (!btif_is_enabled())
   1131         return BT_STATUS_NOT_READY;
   1132 
   1133     return btif_transfer_context(execute_storage_request,
   1134                                  BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
   1135                                  NULL, 0, NULL);
   1136 }
   1137 
   1138 /*******************************************************************************
   1139 **
   1140 ** Function         btif_get_adapter_property
   1141 **
   1142 ** Description      Fetches property value from local cache
   1143 **
   1144 ** Returns          bt_status_t
   1145 **
   1146 *******************************************************************************/
   1147 
   1148 bt_status_t btif_get_adapter_property(bt_property_type_t type)
   1149 {
   1150     btif_storage_req_t req;
   1151 
   1152     BTIF_TRACE_EVENT2("%s %d", __FUNCTION__, type);
   1153 
   1154     /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
   1155     if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
   1156         return BT_STATUS_NOT_READY;
   1157 
   1158     memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
   1159     req.read_req.type = type;
   1160 
   1161     return btif_transfer_context(execute_storage_request,
   1162                                  BTIF_CORE_STORAGE_ADAPTER_READ,
   1163                                 (char*)&req, sizeof(btif_storage_req_t), NULL);
   1164 }
   1165 
   1166 /*******************************************************************************
   1167 **
   1168 ** Function         btif_set_adapter_property
   1169 **
   1170 ** Description      Updates core stack with property value and stores it in
   1171 **                  local cache
   1172 **
   1173 ** Returns          bt_status_t
   1174 **
   1175 *******************************************************************************/
   1176 
   1177 bt_status_t btif_set_adapter_property(const bt_property_t *property)
   1178 {
   1179     btif_storage_req_t req;
   1180     bt_status_t status = BT_STATUS_SUCCESS;
   1181     int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
   1182     char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
   1183     UINT16  name_len = 0;
   1184 
   1185     BTIF_TRACE_EVENT3("btif_set_adapter_property type: %d, len %d, 0x%x",
   1186                       property->type, property->len, property->val);
   1187 
   1188     if (!btif_is_enabled())
   1189         return BT_STATUS_NOT_READY;
   1190 
   1191     switch(property->type)
   1192     {
   1193         case BT_PROPERTY_BDNAME:
   1194             {
   1195                 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
   1196                                                                      property->len;
   1197                 memcpy(bd_name,property->val, name_len);
   1198                 bd_name[name_len] = '\0';
   1199 
   1200                 BTIF_TRACE_EVENT1("set property name : %s", (char *)bd_name);
   1201 
   1202                 BTA_DmSetDeviceName((char *)bd_name);
   1203 
   1204                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
   1205             }
   1206             break;
   1207 
   1208         case BT_PROPERTY_ADAPTER_SCAN_MODE:
   1209             {
   1210                 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
   1211                 tBTA_DM_DISC disc_mode;
   1212                 tBTA_DM_CONN conn_mode;
   1213 
   1214                 switch(mode)
   1215                 {
   1216                     case BT_SCAN_MODE_NONE:
   1217                         disc_mode = BTA_DM_NON_DISC;
   1218                         conn_mode = BTA_DM_NON_CONN;
   1219                         break;
   1220 
   1221                     case BT_SCAN_MODE_CONNECTABLE:
   1222                         disc_mode = BTA_DM_NON_DISC;
   1223                         conn_mode = BTA_DM_CONN;
   1224                         break;
   1225 
   1226                     case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
   1227                         disc_mode = BTA_DM_GENERAL_DISC;
   1228                         conn_mode = BTA_DM_CONN;
   1229                         break;
   1230 
   1231                     default:
   1232                         BTIF_TRACE_ERROR1("invalid scan mode (0x%x)", mode);
   1233                         return BT_STATUS_PARM_INVALID;
   1234                 }
   1235 
   1236                 BTIF_TRACE_EVENT1("set property scan mode : %x", mode);
   1237 
   1238                 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
   1239 
   1240                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
   1241             }
   1242             break;
   1243         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
   1244             {
   1245                 /* Nothing to do beside store the value in NV.  Java
   1246                    will change the SCAN_MODE property after setting timeout,
   1247                    if required */
   1248                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
   1249             }
   1250             break;
   1251         case BT_PROPERTY_BDADDR:
   1252         case BT_PROPERTY_UUIDS:
   1253         case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
   1254         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
   1255             /* no write support through HAL, these properties are only populated from BTA events */
   1256             status = BT_STATUS_FAIL;
   1257             break;
   1258         default:
   1259             BTIF_TRACE_ERROR1("btif_get_adapter_property : invalid type %d",
   1260             property->type);
   1261             status = BT_STATUS_FAIL;
   1262             break;
   1263     }
   1264 
   1265     if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
   1266     {
   1267         int btif_status;
   1268         /* pass on to storage for updating local database */
   1269 
   1270         memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
   1271         memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
   1272 
   1273         return btif_transfer_context(execute_storage_request,
   1274                                      storage_req_id,
   1275                                      (char*)&req,
   1276                                      sizeof(btif_storage_req_t)+property->len,
   1277                                      btif_in_storage_request_copy_cb);
   1278     }
   1279 
   1280     return status;
   1281 
   1282 }
   1283 
   1284 /*******************************************************************************
   1285 **
   1286 ** Function         btif_get_remote_device_property
   1287 **
   1288 ** Description      Fetches the remote device property from the NVRAM
   1289 **
   1290 ** Returns          bt_status_t
   1291 **
   1292 *******************************************************************************/
   1293 bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
   1294                                                  bt_property_type_t type)
   1295 {
   1296     btif_storage_req_t req;
   1297 
   1298     if (!btif_is_enabled())
   1299         return BT_STATUS_NOT_READY;
   1300 
   1301     memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
   1302     req.read_req.type = type;
   1303     return btif_transfer_context(execute_storage_remote_request,
   1304                                  BTIF_CORE_STORAGE_REMOTE_READ,
   1305                                  (char*)&req, sizeof(btif_storage_req_t),
   1306                                  NULL);
   1307 }
   1308 
   1309 /*******************************************************************************
   1310 **
   1311 ** Function         btif_get_remote_device_properties
   1312 **
   1313 ** Description      Fetches all the remote device properties from NVRAM
   1314 **
   1315 ** Returns          bt_status_t
   1316 **
   1317 *******************************************************************************/
   1318 bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
   1319 {
   1320     btif_storage_req_t req;
   1321 
   1322     if (!btif_is_enabled())
   1323         return BT_STATUS_NOT_READY;
   1324 
   1325     memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
   1326     return btif_transfer_context(execute_storage_remote_request,
   1327                                  BTIF_CORE_STORAGE_REMOTE_READ_ALL,
   1328                                  (char*)&req, sizeof(btif_storage_req_t),
   1329                                  NULL);
   1330 }
   1331 
   1332 /*******************************************************************************
   1333 **
   1334 ** Function         btif_set_remote_device_property
   1335 **
   1336 ** Description      Writes the remote device property to NVRAM.
   1337 **                  Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
   1338 **                  remote device property that can be set
   1339 **
   1340 ** Returns          bt_status_t
   1341 **
   1342 *******************************************************************************/
   1343 bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
   1344                                                  const bt_property_t *property)
   1345 {
   1346     btif_storage_req_t req;
   1347 
   1348     if (!btif_is_enabled())
   1349         return BT_STATUS_NOT_READY;
   1350 
   1351     memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
   1352     memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
   1353 
   1354     return btif_transfer_context(execute_storage_remote_request,
   1355                                  BTIF_CORE_STORAGE_REMOTE_WRITE,
   1356                                  (char*)&req,
   1357                                  sizeof(btif_storage_req_t)+property->len,
   1358                                  btif_in_storage_request_copy_cb);
   1359 }
   1360 
   1361 
   1362 /*******************************************************************************
   1363 **
   1364 ** Function         btif_get_remote_service_record
   1365 **
   1366 ** Description      Looks up the service matching uuid on the remote device
   1367 **                  and fetches the SCN and service_name if the UUID is found
   1368 **
   1369 ** Returns          bt_status_t
   1370 **
   1371 *******************************************************************************/
   1372 bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
   1373                                                bt_uuid_t *uuid)
   1374 {
   1375     if (!btif_is_enabled())
   1376         return BT_STATUS_NOT_READY;
   1377 
   1378     return btif_dm_get_remote_service_record(remote_addr, uuid);
   1379 }
   1380 
   1381 
   1382 /*******************************************************************************
   1383 **
   1384 ** Function         btif_get_enabled_services_mask
   1385 **
   1386 ** Description      Fetches currently enabled services
   1387 **
   1388 ** Returns          tBTA_SERVICE_MASK
   1389 **
   1390 *******************************************************************************/
   1391 
   1392 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
   1393 {
   1394     return btif_enabled_services;
   1395 }
   1396 
   1397 /*******************************************************************************
   1398 **
   1399 ** Function         btif_enable_service
   1400 **
   1401 ** Description      Enables the service 'service_ID' to the service_mask.
   1402 **                  Upon BT enable, BTIF core shall invoke the BTA APIs to
   1403 **                  enable the profiles
   1404 **
   1405 ** Returns          bt_status_t
   1406 **
   1407 *******************************************************************************/
   1408 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
   1409 {
   1410     tBTA_SERVICE_ID *p_id = &service_id;
   1411 
   1412     /* If BT is enabled, we need to switch to BTIF context and trigger the
   1413      * enable for that profile
   1414      *
   1415      * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
   1416      * enable for the profiles that have been enabled */
   1417 
   1418     btif_enabled_services |= (1 << service_id);
   1419 
   1420     BTIF_TRACE_DEBUG2("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
   1421 
   1422     if (btif_is_enabled())
   1423     {
   1424         btif_transfer_context(btif_dm_execute_service_request,
   1425                               BTIF_DM_ENABLE_SERVICE,
   1426                               (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
   1427     }
   1428 
   1429     return BT_STATUS_SUCCESS;
   1430 }
   1431 /*******************************************************************************
   1432 **
   1433 ** Function         btif_disable_service
   1434 **
   1435 ** Description      Disables the service 'service_ID' to the service_mask.
   1436 **                  Upon BT disable, BTIF core shall invoke the BTA APIs to
   1437 **                  disable the profiles
   1438 **
   1439 ** Returns          bt_status_t
   1440 **
   1441 *******************************************************************************/
   1442 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
   1443 {
   1444     tBTA_SERVICE_ID *p_id = &service_id;
   1445 
   1446     /* If BT is enabled, we need to switch to BTIF context and trigger the
   1447      * disable for that profile so that the appropriate uuid_property_changed will
   1448      * be triggerred. Otherwise, we just need to clear the service_id in the mask
   1449      */
   1450 
   1451     btif_enabled_services &=  (tBTA_SERVICE_MASK)(~(1<<service_id));
   1452 
   1453     BTIF_TRACE_DEBUG2("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
   1454 
   1455     if (btif_is_enabled())
   1456     {
   1457         btif_transfer_context(btif_dm_execute_service_request,
   1458                               BTIF_DM_DISABLE_SERVICE,
   1459                               (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
   1460     }
   1461 
   1462     return BT_STATUS_SUCCESS;
   1463 }
   1464 
   1465 /*******************************************************************************
   1466 **
   1467 ** Function         btif_config_hci_snoop_log
   1468 **
   1469 ** Description      enable or disable HCI snoop log
   1470 **
   1471 ** Returns          bt_status_t
   1472 **
   1473 *******************************************************************************/
   1474 bt_status_t btif_config_hci_snoop_log(uint8_t enable)
   1475 {
   1476     bte_main_config_hci_logging(enable != 0,
   1477              btif_core_state == BTIF_CORE_STATE_DISABLED);
   1478     return BT_STATUS_SUCCESS;
   1479 }
   1480