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