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