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