1 /******************************************************************************
3 * Copyright (C) 2014 The Android Open Source Project
4 * Copyright (C) 2009-2012 Broadcom Corporation
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ******************************************************************************/
20 /************************************************************************************
22 * Filename: btif_core.c
24 * Description: Contains core functionality related to interfacing between
25 * Bluetooth HAL and BTE core stack.
27 ***********************************************************************************/
30 #include <cutils/properties.h>
33 #include <hardware/bluetooth.h>
37 #include <sys/types.h>
39 #define LOG_TAG "bt_btif_core"
40 #include "btcore/include/bdaddr.h"
48 #include "btif_config.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"
56 #include "device/include/controller.h"
57 #include "osi/include/fixed_queue.h"
58 #include "osi/include/future.h"
60 #include "osi/include/osi.h"
61 #include "osi/include/log.h"
62 #include "stack_manager.h"
63 #include "osi/include/thread.h"
65 /************************************************************************************
67 ************************************************************************************/
69 #ifndef BTE_DID_CONF_FILE
70 #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
73 /************************************************************************************
74 ** Local type definitions
75 ************************************************************************************/
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 */
82 bt_property_type_t type;
83 } btif_storage_read_t;
88 } btif_storage_write_t;
91 btif_storage_read_t read_req;
92 btif_storage_write_t write_req;
96 BTIF_CORE_STATE_DISABLED = 0,
97 BTIF_CORE_STATE_ENABLING,
98 BTIF_CORE_STATE_ENABLED,
99 BTIF_CORE_STATE_DISABLING
102 /************************************************************************************
104 ************************************************************************************/
106 bt_bdaddr_t btif_local_bd_addr;
108 static tBTA_SERVICE_MASK btif_enabled_services = 0;
111 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
112 * function in DUT mode.
114 * To set this, the btif_init_bluetooth needs to be called with argument as 1
116 static UINT8 btif_dut_mode = 0;
118 static thread_t *bt_jni_workqueue_thread;
119 static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue";
121 /************************************************************************************
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);
127 /* sends message to btif task */
128 static void btif_sendmsg(void *p_msg);
130 /************************************************************************************
132 ************************************************************************************/
133 extern fixed_queue_t *btu_hci_msg_queue;
135 extern void bte_load_did_conf(const char *p_path);
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);
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);
149 void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
151 /*******************************************************************************
153 ** Function btif_context_switched
155 ** Description Callback used to execute transferred context callback
157 ** p_msg : message to be executed in btif context
161 *******************************************************************************/
163 static void btif_context_switched(void *p_msg)
166 BTIF_TRACE_VERBOSE("btif_context_switched");
168 tBTIF_CONTEXT_SWITCH_CBACK *p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
170 /* each callback knows how to parse the data */
172 p->p_cb(p->event, p->p_param);
176 /*******************************************************************************
178 ** Function btif_transfer_context
180 ** Description This function switches context to btif task
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
190 *******************************************************************************/
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)
194 tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
196 BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len);
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)
201 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
202 p_msg->p_cb = p_cback;
204 p_msg->event = event; /* callback event */
206 /* check if caller has provided a copy callback to do the deep copy */
209 p_copy_cback(event, p_msg->p_param, p_params);
213 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
217 return BT_STATUS_SUCCESS;
221 /* let caller deal with a failed allocation */
222 return BT_STATUS_NOMEM;
226 /*******************************************************************************
228 ** Function btif_is_dut_mode
230 ** Description checks if BTIF is currently in DUT mode
232 ** Returns 1 if test mode, otherwize 0
234 *******************************************************************************/
236 UINT8 btif_is_dut_mode(void)
238 return (btif_dut_mode == 1);
241 /*******************************************************************************
243 ** Function btif_is_enabled
245 ** Description checks if main adapter is fully enabled
247 ** Returns 1 if fully enabled, otherwize 0
249 *******************************************************************************/
251 int btif_is_enabled(void)
253 return ((!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running()));
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();
261 BTA_EnableBluetooth(bte_dm_evt);
264 void btif_init_fail(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
265 BTIF_TRACE_DEBUG("btif_task: hardware init failed");
267 btif_queue_release();
271 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
274 /*******************************************************************************
276 ** Function btif_task
278 ** Description BTIF task handler managing all messages being passed
279 ** Bluetooth HAL and BTA.
283 *******************************************************************************/
284 static void bt_jni_msg_ready(void *context) {
285 BT_HDR *p_msg = (BT_HDR *)context;
287 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
289 switch (p_msg->event) {
290 case BT_EVT_CONTEXT_SWITCH_EVT:
291 btif_context_switched(p_msg);
294 BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
300 /*******************************************************************************
302 ** Function btif_sendmsg
304 ** Description Sends msg to BTIF task
308 *******************************************************************************/
310 void btif_sendmsg(void *p_msg)
312 thread_post(bt_jni_workqueue_thread, bt_jni_msg_ready, p_msg);
315 void btif_thread_post(thread_fn func, void *context) {
316 thread_post(bt_jni_workqueue_thread, func, context);
319 static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
322 uint8_t valid_bda = FALSE;
324 const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
326 /* Get local bdaddr storage path from property */
327 if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
331 BTIF_TRACE_DEBUG("local bdaddr is stored in %s", val);
333 if ((addr_fd = open(val, O_RDONLY)) != -1)
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)
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]);
353 val_size = sizeof(val);
354 if(btif_config_get_str("Adapter", "Address", val, &val_size))
356 string_to_bdaddr(val, local_addr);
357 BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val);
362 /* No factory BDADDR found. Look for previously generated random BDA */
363 if ((!valid_bda) && \
364 (property_get(PERSIST_BDADDR_PROPERTY, val, NULL)))
366 string_to_bdaddr(val, local_addr);
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]);
373 /* Generate new BDA if necessary */
377 /* Seed the random number generator */
378 srand((unsigned int) (time(0)));
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);
388 /* Convert to ascii, and store as a persistent property */
389 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
391 BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
392 (char*)bdstr, PERSIST_BDADDR_PROPERTY);
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);
398 //save the bd address to config file
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))
404 if (strcmp(bdstr, val) ==0)
406 // BDA is already present in the config file.
410 btif_config_set_str("Adapter", "Address", bdstr);
413 /*******************************************************************************
415 ** Function btif_init_bluetooth
417 ** Description Creates BTIF task and prepares BT scheduler for startup
419 ** Returns bt_status_t
421 *******************************************************************************/
422 bt_status_t btif_init_bluetooth() {
423 bte_main_boot_entry();
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);
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);
435 // Associate this workqueue thread with jni.
436 btif_transfer_context(btif_jni_associate, 0, NULL, 0, NULL);
438 return BT_STATUS_SUCCESS;
441 thread_free(bt_jni_workqueue_thread);
443 bt_jni_workqueue_thread = NULL;
445 return BT_STATUS_FAIL;
448 /*******************************************************************************
450 ** Function btif_enable_bluetooth_evt
452 ** Description Event indicating bluetooth enable is completed
453 ** Notifies HAL user with updated adapter state
457 *******************************************************************************/
459 void btif_enable_bluetooth_evt(tBTA_STATUS status)
461 const controller_t *controller = controller_get_interface();
463 bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
465 BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status, bdstr);
467 if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address))
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.
474 bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address));
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
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("***********************************************");
487 btif_local_bd_addr = *controller->get_address();
489 //save the bd address to config file
490 btif_config_set_str("Adapter", "Address", bdstr);
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);
500 bte_main_postload_cfg();
501 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
502 bte_main_enable_lpm(TRUE);
504 /* add passing up bd address as well ? */
506 /* callback to HAL */
507 if (status == BTA_SUCCESS)
509 /* initialize a2dp service */
512 /* init rfcomm & l2cap api */
518 /* load did configuration */
519 bte_load_did_conf(BTE_DID_CONF_FILE);
521 #ifdef BTIF_DM_OOB_TEST
522 btif_dm_load_local_oob();
525 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
529 /* cleanup rfcomm & l2cap api */
534 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
538 /*******************************************************************************
540 ** Function btif_disable_bluetooth
542 ** Description Inititates shutdown of Bluetooth system.
543 ** Any active links will be dropped and device entering
544 ** non connectable/discoverable mode
548 *******************************************************************************/
549 bt_status_t btif_disable_bluetooth(void)
551 BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
553 btif_dm_on_disable();
554 /* cleanup rfcomm & l2cap api */
557 BTA_DisableBluetooth();
559 return BT_STATUS_SUCCESS;
562 /*******************************************************************************
564 ** Function btif_disable_bluetooth_evt
566 ** Description Event notifying BT disable is now complete.
567 ** Terminates main stack tasks and notifies HAL
568 ** user with updated BT state.
572 *******************************************************************************/
574 void btif_disable_bluetooth_evt(void)
576 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
578 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
579 bte_main_enable_lpm(FALSE);
582 #if (BLE_INCLUDED == TRUE)
588 /* callback to HAL */
589 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
592 /*******************************************************************************
594 ** Function btif_shutdown_bluetooth
596 ** Description Finalizes BT scheduler shutdown and terminates BTIF
601 *******************************************************************************/
603 bt_status_t btif_shutdown_bluetooth(void)
605 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
607 btif_transfer_context(btif_jni_disassociate, 0, NULL, 0, NULL);
609 btif_queue_release();
611 thread_free(bt_jni_workqueue_thread);
612 bt_jni_workqueue_thread = NULL;
618 BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
620 return BT_STATUS_SUCCESS;
623 /*******************************************************************************
625 ** Function btif_dut_mode_cback
627 ** Description Callback invoked on completion of vendor specific test mode command
631 *******************************************************************************/
632 static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
635 /* For now nothing to be done. */
638 /*******************************************************************************
640 ** Function btif_dut_mode_configure
642 ** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
645 ** Returns BT_STATUS_SUCCESS on success
647 *******************************************************************************/
648 bt_status_t btif_dut_mode_configure(uint8_t enable)
650 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
652 if (!stack_manager_get_interface()->get_stack_is_running()) {
653 BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
654 return BT_STATUS_NOT_READY;
657 btif_dut_mode = enable;
659 BTA_EnableTestMode();
661 BTA_DisableTestMode();
663 return BT_STATUS_SUCCESS;
666 /*******************************************************************************
668 ** Function btif_dut_mode_send
670 ** Description Sends a HCI Vendor specific command to the controller
672 ** Returns BT_STATUS_SUCCESS on success
674 *******************************************************************************/
675 bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
677 /* TODO: Check that opcode is a vendor command group */
678 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
679 if (!btif_is_dut_mode()) {
680 BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1.");
681 return BT_STATUS_FAIL;
683 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
684 return BT_STATUS_SUCCESS;
687 /*****************************************************************************
689 ** btif api adapter property functions
691 *****************************************************************************/
693 static bt_status_t btif_in_get_adapter_properties(void)
695 bt_property_t properties[6];
701 uint32_t disc_timeout;
702 bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
703 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
707 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
708 sizeof(addr), &addr);
709 btif_storage_get_adapter_property(&properties[num_props]);
713 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
714 sizeof(name), &name);
715 btif_storage_get_adapter_property(&properties[num_props]);
719 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
720 sizeof(mode), &mode);
721 btif_storage_get_adapter_property(&properties[num_props]);
725 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
726 sizeof(disc_timeout), &disc_timeout);
727 btif_storage_get_adapter_property(&properties[num_props]);
731 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
732 sizeof(bonded_devices), bonded_devices);
733 btif_storage_get_adapter_property(&properties[num_props]);
737 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
738 sizeof(local_uuids), local_uuids);
739 btif_storage_get_adapter_property(&properties[num_props]);
742 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
743 BT_STATUS_SUCCESS, num_props, properties);
745 return BT_STATUS_SUCCESS;
748 static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
750 bt_property_t remote_properties[8];
751 uint32_t num_props = 0;
753 bt_bdname_t name, alias;
754 uint32_t cod, devtype;
755 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
757 memset(remote_properties, 0, sizeof(remote_properties));
758 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
759 sizeof(name), &name);
760 btif_storage_get_remote_device_property(bd_addr,
761 &remote_properties[num_props]);
764 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
765 sizeof(alias), &alias);
766 btif_storage_get_remote_device_property(bd_addr,
767 &remote_properties[num_props]);
770 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
772 btif_storage_get_remote_device_property(bd_addr,
773 &remote_properties[num_props]);
776 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
777 sizeof(devtype), &devtype);
778 btif_storage_get_remote_device_property(bd_addr,
779 &remote_properties[num_props]);
782 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
783 sizeof(remote_uuids), remote_uuids);
784 btif_storage_get_remote_device_property(bd_addr,
785 &remote_properties[num_props]);
788 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
789 BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
791 return BT_STATUS_SUCCESS;
795 /*******************************************************************************
797 ** Function execute_storage_request
799 ** Description Executes adapter storage request in BTIF context
801 ** Returns bt_status_t
803 *******************************************************************************/
805 static void execute_storage_request(UINT16 event, char *p_param)
807 bt_status_t status = BT_STATUS_SUCCESS;
809 BTIF_TRACE_EVENT("execute storage request event : %d", event);
813 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
815 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
816 bt_property_t *p_prop = &(p_req->write_req.prop);
817 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
818 p_prop->len, p_prop->val);
820 status = btif_storage_set_adapter_property(p_prop);
821 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
824 case BTIF_CORE_STORAGE_ADAPTER_READ:
826 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
829 prop.type = p_req->read_req.type;
830 prop.val = (void*)buf;
831 prop.len = sizeof(buf);
832 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
834 #if (BLE_INCLUDED == TRUE)
835 tBTM_BLE_VSC_CB cmn_vsc_cb;
836 bt_local_le_features_t local_le_features;
838 /* LE features are not stored in storage. Should be retrived from stack */
839 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
840 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
842 prop.len = sizeof (bt_local_le_features_t);
843 if (cmn_vsc_cb.filter_support == 1)
844 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
846 local_le_features.max_adv_filter_supported = 0;
847 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
848 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
849 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
850 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
851 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
852 local_le_features.version_supported = cmn_vsc_cb.version_supported;
853 local_le_features.total_trackable_advertisers =
854 cmn_vsc_cb.total_trackable_advertisers;
855 memcpy(prop.val, &local_le_features, prop.len);
860 status = btif_storage_get_adapter_property(&prop);
862 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
865 case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
867 status = btif_in_get_adapter_properties();
870 case BTIF_CORE_STORAGE_NOTIFY_STATUS:
872 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
876 BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
881 static void execute_storage_remote_request(UINT16 event, char *p_param)
883 bt_status_t status = BT_STATUS_FAIL;
886 BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
890 case BTIF_CORE_STORAGE_REMOTE_READ:
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);
898 status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
900 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
901 status, &(p_req->read_req.bd_addr), 1, &prop);
903 case BTIF_CORE_STORAGE_REMOTE_WRITE:
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));
909 case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
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);
917 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
918 bt_property_t *p_props)
920 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
921 status, num_props, p_props);
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)
927 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
928 status, remote_addr, num_props, p_props);
931 /*******************************************************************************
933 ** Function btif_in_storage_request_copy_cb
935 ** Description Switch context callback function to perform the deep copy for
936 ** both the adapter and remote_device property API
940 *******************************************************************************/
941 static void btif_in_storage_request_copy_cb(UINT16 event,
942 char *p_new_buf, char *p_old_buf)
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;
947 BTIF_TRACE_EVENT("%s", __FUNCTION__);
950 case BTIF_CORE_STORAGE_REMOTE_WRITE:
951 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
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;
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);
965 /*******************************************************************************
967 ** Function btif_get_adapter_properties
969 ** Description Fetch all available properties (local & remote)
971 ** Returns bt_status_t
973 *******************************************************************************/
975 bt_status_t btif_get_adapter_properties(void)
977 BTIF_TRACE_EVENT("%s", __FUNCTION__);
979 if (!btif_is_enabled())
980 return BT_STATUS_NOT_READY;
982 return btif_transfer_context(execute_storage_request,
983 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
987 /*******************************************************************************
989 ** Function btif_get_adapter_property
991 ** Description Fetches property value from local cache
993 ** Returns bt_status_t
995 *******************************************************************************/
997 bt_status_t btif_get_adapter_property(bt_property_type_t type)
999 btif_storage_req_t req;
1001 BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
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;
1007 memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1008 req.read_req.type = type;
1010 return btif_transfer_context(execute_storage_request,
1011 BTIF_CORE_STORAGE_ADAPTER_READ,
1012 (char*)&req, sizeof(btif_storage_req_t), NULL);
1015 /*******************************************************************************
1017 ** Function btif_set_adapter_property
1019 ** Description Updates core stack with property value and stores it in
1022 ** Returns bt_status_t
1024 *******************************************************************************/
1026 bt_status_t btif_set_adapter_property(const bt_property_t *property)
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;
1034 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
1035 property->type, property->len, property->val);
1037 if (!btif_is_enabled())
1038 return BT_STATUS_NOT_READY;
1040 switch(property->type)
1042 case BT_PROPERTY_BDNAME:
1044 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1046 memcpy(bd_name,property->val, name_len);
1047 bd_name[name_len] = '\0';
1049 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
1051 BTA_DmSetDeviceName((char *)bd_name);
1053 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1057 case BT_PROPERTY_ADAPTER_SCAN_MODE:
1059 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1060 tBTA_DM_DISC disc_mode;
1061 tBTA_DM_CONN conn_mode;
1065 case BT_SCAN_MODE_NONE:
1066 disc_mode = BTA_DM_NON_DISC;
1067 conn_mode = BTA_DM_NON_CONN;
1070 case BT_SCAN_MODE_CONNECTABLE:
1071 disc_mode = BTA_DM_NON_DISC;
1072 conn_mode = BTA_DM_CONN;
1075 case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1076 disc_mode = BTA_DM_GENERAL_DISC;
1077 conn_mode = BTA_DM_CONN;
1081 BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
1082 return BT_STATUS_PARM_INVALID;
1085 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
1087 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1089 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1092 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1094 /* Nothing to do beside store the value in NV. Java
1095 will change the SCAN_MODE property after setting timeout,
1097 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
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;
1108 BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
1110 status = BT_STATUS_FAIL;
1114 if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1116 /* pass on to storage for updating local database */
1118 memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1119 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1121 return btif_transfer_context(execute_storage_request,
1124 sizeof(btif_storage_req_t)+property->len,
1125 btif_in_storage_request_copy_cb);
1132 /*******************************************************************************
1134 ** Function btif_get_remote_device_property
1136 ** Description Fetches the remote device property from the NVRAM
1138 ** Returns bt_status_t
1140 *******************************************************************************/
1141 bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1142 bt_property_type_t type)
1144 btif_storage_req_t req;
1146 if (!btif_is_enabled())
1147 return BT_STATUS_NOT_READY;
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),
1157 /*******************************************************************************
1159 ** Function btif_get_remote_device_properties
1161 ** Description Fetches all the remote device properties from NVRAM
1163 ** Returns bt_status_t
1165 *******************************************************************************/
1166 bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1168 btif_storage_req_t req;
1170 if (!btif_is_enabled())
1171 return BT_STATUS_NOT_READY;
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),
1180 /*******************************************************************************
1182 ** Function btif_set_remote_device_property
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
1188 ** Returns bt_status_t
1190 *******************************************************************************/
1191 bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1192 const bt_property_t *property)
1194 btif_storage_req_t req;
1196 if (!btif_is_enabled())
1197 return BT_STATUS_NOT_READY;
1199 memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1200 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1202 return btif_transfer_context(execute_storage_remote_request,
1203 BTIF_CORE_STORAGE_REMOTE_WRITE,
1205 sizeof(btif_storage_req_t)+property->len,
1206 btif_in_storage_request_copy_cb);
1210 /*******************************************************************************
1212 ** Function btif_get_remote_service_record
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
1217 ** Returns bt_status_t
1219 *******************************************************************************/
1220 bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1223 if (!btif_is_enabled())
1224 return BT_STATUS_NOT_READY;
1226 return btif_dm_get_remote_service_record(remote_addr, uuid);
1230 /*******************************************************************************
1232 ** Function btif_get_enabled_services_mask
1234 ** Description Fetches currently enabled services
1236 ** Returns tBTA_SERVICE_MASK
1238 *******************************************************************************/
1240 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1242 return btif_enabled_services;
1245 /*******************************************************************************
1247 ** Function btif_enable_service
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
1253 ** Returns bt_status_t
1255 *******************************************************************************/
1256 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1258 tBTA_SERVICE_ID *p_id = &service_id;
1260 /* If BT is enabled, we need to switch to BTIF context and trigger the
1261 * enable for that profile
1263 * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1264 * enable for the profiles that have been enabled */
1266 btif_enabled_services |= (1 << service_id);
1268 BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1270 if (btif_is_enabled())
1272 btif_transfer_context(btif_dm_execute_service_request,
1273 BTIF_DM_ENABLE_SERVICE,
1274 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1277 return BT_STATUS_SUCCESS;
1279 /*******************************************************************************
1281 ** Function btif_disable_service
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
1287 ** Returns bt_status_t
1289 *******************************************************************************/
1290 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1292 tBTA_SERVICE_ID *p_id = &service_id;
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
1299 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
1301 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1303 if (btif_is_enabled())
1305 btif_transfer_context(btif_dm_execute_service_request,
1306 BTIF_DM_DISABLE_SERVICE,
1307 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1310 return BT_STATUS_SUCCESS;
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);
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);