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 ***********************************************************************************/
29 #define LOG_TAG "bt_btif_core"
34 #include <hardware/bluetooth.h>
38 #include <sys/types.h>
42 * TODO(armansito): On OSs other than Android, the sys/properties.h system
43 * does not exist. Remove this conditional include once we have a generic way
44 * to obtain system properties.
46 #if !defined(OS_GENERIC)
47 #include <cutils/properties.h>
48 #endif /* !defined(OS_GENERIC) */
56 #include "btif_config.h"
57 #include "btif_config.h"
59 #include "btif_profile_queue.h"
60 #include "btif_sock.h"
61 #include "btif_storage.h"
62 #include "btif_util.h"
64 #include "device/include/controller.h"
66 #include "osi/include/fixed_queue.h"
67 #include "osi/include/future.h"
68 #include "osi/include/log.h"
69 #include "osi/include/osi.h"
70 #include "osi/include/thread.h"
71 #include "stack_manager.h"
73 /************************************************************************************
75 ************************************************************************************/
77 #ifndef BTE_DID_CONF_FILE
78 // TODO(armansito): Find a better way than searching by a hardcoded path.
79 #if defined(OS_GENERIC)
80 #define BTE_DID_CONF_FILE "bt_did.conf"
81 #else // !defined(OS_GENERIC)
82 #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
83 #endif // defined(OS_GENERIC)
84 #endif // BTE_DID_CONF_FILE
86 /************************************************************************************
87 ** Local type definitions
88 ************************************************************************************/
90 /* These type definitions are used when passing data from the HAL to BTIF context
91 * in the downstream path for the adapter and remote_device property APIs */
95 bt_property_type_t type;
96 } btif_storage_read_t;
101 } btif_storage_write_t;
104 btif_storage_read_t read_req;
105 btif_storage_write_t write_req;
106 } btif_storage_req_t;
109 BTIF_CORE_STATE_DISABLED = 0,
110 BTIF_CORE_STATE_ENABLING,
111 BTIF_CORE_STATE_ENABLED,
112 BTIF_CORE_STATE_DISABLING
115 /************************************************************************************
117 ************************************************************************************/
119 bt_bdaddr_t btif_local_bd_addr;
121 static tBTA_SERVICE_MASK btif_enabled_services = 0;
124 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
125 * function in DUT mode.
127 * To set this, the btif_init_bluetooth needs to be called with argument as 1
129 static UINT8 btif_dut_mode = 0;
131 static thread_t *bt_jni_workqueue_thread;
132 static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue";
134 /************************************************************************************
136 ************************************************************************************/
137 static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
138 static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
139 static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr);
141 /* sends message to btif task */
142 static void btif_sendmsg(void *p_msg);
144 /************************************************************************************
146 ************************************************************************************/
147 extern fixed_queue_t *btu_hci_msg_queue;
149 extern void bte_load_did_conf(const char *p_path);
151 /** TODO: Move these to _common.h */
152 void bte_main_boot_entry(void);
153 void bte_main_disable(void);
154 void bte_main_shutdown(void);
155 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
156 void bte_main_enable_lpm(BOOLEAN enable);
158 void bte_main_postload_cfg(void);
159 void btif_dm_execute_service_request(UINT16 event, char *p_param);
160 #ifdef BTIF_DM_OOB_TEST
161 void btif_dm_load_local_oob(void);
163 void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
165 /*******************************************************************************
167 ** Function btif_context_switched
169 ** Description Callback used to execute transferred context callback
171 ** p_msg : message to be executed in btif context
175 *******************************************************************************/
177 static void btif_context_switched(void *p_msg)
180 BTIF_TRACE_VERBOSE("btif_context_switched");
182 tBTIF_CONTEXT_SWITCH_CBACK *p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
184 /* each callback knows how to parse the data */
186 p->p_cb(p->event, p->p_param);
190 /*******************************************************************************
192 ** Function btif_transfer_context
194 ** Description This function switches context to btif task
196 ** p_cback : callback used to process message in btif context
197 ** event : event id of message
198 ** p_params : parameter area passed to callback (copied)
199 ** param_len : length of parameter area
200 ** p_copy_cback : If set this function will be invoked for deep copy
204 *******************************************************************************/
206 bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
208 tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
210 BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len);
212 /* allocate and send message that will be executed in btif context */
213 if ((p_msg = (tBTIF_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
215 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
216 p_msg->p_cb = p_cback;
218 p_msg->event = event; /* callback event */
220 /* check if caller has provided a copy callback to do the deep copy */
223 p_copy_cback(event, p_msg->p_param, p_params);
227 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
231 return BT_STATUS_SUCCESS;
235 /* let caller deal with a failed allocation */
236 return BT_STATUS_NOMEM;
240 /*******************************************************************************
242 ** Function btif_is_dut_mode
244 ** Description checks if BTIF is currently in DUT mode
246 ** Returns 1 if test mode, otherwize 0
248 *******************************************************************************/
250 UINT8 btif_is_dut_mode(void)
252 return (btif_dut_mode == 1);
255 /*******************************************************************************
257 ** Function btif_is_enabled
259 ** Description checks if main adapter is fully enabled
261 ** Returns 1 if fully enabled, otherwize 0
263 *******************************************************************************/
265 int btif_is_enabled(void)
267 return ((!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running()));
270 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
271 BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
272 #if (BLE_INCLUDED == TRUE)
273 btif_dm_load_ble_local_keys();
275 BTA_EnableBluetooth(bte_dm_evt);
278 void btif_init_fail(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
279 BTIF_TRACE_DEBUG("btif_task: hardware init failed");
281 btif_queue_release();
285 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
288 /*******************************************************************************
290 ** Function btif_task
292 ** Description BTIF task handler managing all messages being passed
293 ** Bluetooth HAL and BTA.
297 *******************************************************************************/
298 static void bt_jni_msg_ready(void *context) {
299 BT_HDR *p_msg = (BT_HDR *)context;
301 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
303 switch (p_msg->event) {
304 case BT_EVT_CONTEXT_SWITCH_EVT:
305 btif_context_switched(p_msg);
308 BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
314 /*******************************************************************************
316 ** Function btif_sendmsg
318 ** Description Sends msg to BTIF task
322 *******************************************************************************/
324 void btif_sendmsg(void *p_msg)
326 thread_post(bt_jni_workqueue_thread, bt_jni_msg_ready, p_msg);
329 void btif_thread_post(thread_fn func, void *context) {
330 thread_post(bt_jni_workqueue_thread, func, context);
333 static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr) {
334 char val[PROPERTY_VALUE_MAX] = {0};
336 if (property_get(key, val, NULL)) {
337 if (string_to_bdaddr(val, addr)) {
338 BTIF_TRACE_DEBUG("%s: Got BDA %s", __func__, val);
341 BTIF_TRACE_DEBUG("%s: System Property did not contain valid bdaddr", __func__);
346 static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
348 char val[PROPERTY_VALUE_MAX] = {0};
349 uint8_t valid_bda = FALSE;
353 * TODO(armansito): On OSs other than Android, the sys/properties.h system
354 * does not exist. Remove this conditional include once we have a generic way
355 * to obtain system properties.
357 #if !defined(OS_GENERIC)
358 const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
360 /* Get local bdaddr storage path from property */
361 if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
365 BTIF_TRACE_DEBUG("%s, local bdaddr is stored in %s", __func__, val);
367 if ((addr_fd = open(val, O_RDONLY)) != -1)
369 memset(val, 0, sizeof(val));
370 read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN);
371 /* If this is not a reserved/special bda, then use it */
372 if ((string_to_bdaddr(val, local_addr)) &&
373 (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0))
376 BTIF_TRACE_DEBUG("%s: Got Factory BDA %s", __func__, val);
384 val_size = sizeof(val);
385 if(btif_config_get_str("Adapter", "Address", val, &val_size))
387 string_to_bdaddr(val, local_addr);
388 BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val);
393 /* No factory BDADDR found. Look for previously generated random BDA */
395 valid_bda = btif_fetch_property(PERSIST_BDADDR_PROPERTY, local_addr);
398 /* No BDADDR found in file. Look for BDA in factory property */
400 valid_bda = btif_fetch_property(FACTORY_BT_ADDR_PROPERTY, local_addr);
402 #endif /* !defined(OS_GENERIC) */
404 /* Generate new BDA if necessary */
408 /* Seed the random number generator */
409 srand((unsigned int) (time(0)));
411 /* No autogen BDA. Generate one now. */
412 local_addr->address[0] = 0x22;
413 local_addr->address[1] = 0x22;
414 local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
415 local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
416 local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
417 local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
419 /* Convert to ascii, and store as a persistent property */
420 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
422 BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
423 (char*)bdstr, PERSIST_BDADDR_PROPERTY);
427 * TODO(armansito): On OSs other than Android, the sys/properties.h system
428 * does not exist. Remove this conditional include once we have a generic way
429 * to obtain system properties.
431 #if !defined(OS_GENERIC)
432 if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
433 BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
434 #endif /* !defined(OS_GENERIC) */
437 //save the bd address to config file
439 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
440 val_size = sizeof(val);
441 if (btif_config_get_str("Adapter", "Address", val, &val_size))
443 if (strcmp(bdstr, val) ==0)
445 // BDA is already present in the config file.
449 btif_config_set_str("Adapter", "Address", bdstr);
452 /*******************************************************************************
454 ** Function btif_init_bluetooth
456 ** Description Creates BTIF task and prepares BT scheduler for startup
458 ** Returns bt_status_t
460 *******************************************************************************/
461 bt_status_t btif_init_bluetooth() {
462 bte_main_boot_entry();
464 /* As part of the init, fetch the local BD ADDR */
465 memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
466 btif_fetch_local_bdaddr(&btif_local_bd_addr);
468 bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME);
469 if (bt_jni_workqueue_thread == NULL) {
470 LOG_ERROR(LOG_TAG, "%s Unable to create thread %s", __func__, BT_JNI_WORKQUEUE_NAME);
474 // Associate this workqueue thread with jni.
475 btif_transfer_context(btif_jni_associate, 0, NULL, 0, NULL);
477 return BT_STATUS_SUCCESS;
480 thread_free(bt_jni_workqueue_thread);
482 bt_jni_workqueue_thread = NULL;
484 return BT_STATUS_FAIL;
487 /*******************************************************************************
489 ** Function btif_enable_bluetooth_evt
491 ** Description Event indicating bluetooth enable is completed
492 ** Notifies HAL user with updated adapter state
496 *******************************************************************************/
498 void btif_enable_bluetooth_evt(tBTA_STATUS status)
500 const controller_t *controller = controller_get_interface();
502 bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
504 BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status, bdstr);
506 if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address))
508 // TODO(zachoverflow): this whole code path seems like a bad time waiting to happen
509 // We open the vendor library using the old address.
513 bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address));
516 * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
517 * This could be because the factory BDADDR was stored separately in
518 * the Controller's non-volatile memory rather than in device's file
521 BTIF_TRACE_WARNING("***********************************************");
522 BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address);
523 BTIF_TRACE_WARNING("Controller BDA is %s", bdstr);
524 BTIF_TRACE_WARNING("***********************************************");
526 btif_local_bd_addr = *controller->get_address();
528 //save the bd address to config file
529 btif_config_set_str("Adapter", "Address", bdstr);
532 //fire HAL callback for property change
533 prop.type = BT_PROPERTY_BDADDR;
534 prop.val = (void*)&btif_local_bd_addr;
535 prop.len = sizeof(bt_bdaddr_t);
536 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
539 bte_main_postload_cfg();
540 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
541 bte_main_enable_lpm(TRUE);
543 /* add passing up bd address as well ? */
545 /* callback to HAL */
546 if (status == BTA_SUCCESS)
548 /* init rfcomm & l2cap api */
554 /* load did configuration */
555 bte_load_did_conf(BTE_DID_CONF_FILE);
557 #ifdef BTIF_DM_OOB_TEST
558 btif_dm_load_local_oob();
561 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
565 /* cleanup rfcomm & l2cap api */
570 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
574 /*******************************************************************************
576 ** Function btif_disable_bluetooth
578 ** Description Inititates shutdown of Bluetooth system.
579 ** Any active links will be dropped and device entering
580 ** non connectable/discoverable mode
584 *******************************************************************************/
585 bt_status_t btif_disable_bluetooth(void)
587 BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
589 btif_dm_on_disable();
590 /* cleanup rfcomm & l2cap api */
593 BTA_DisableBluetooth();
595 return BT_STATUS_SUCCESS;
598 /*******************************************************************************
600 ** Function btif_disable_bluetooth_evt
602 ** Description Event notifying BT disable is now complete.
603 ** Terminates main stack tasks and notifies HAL
604 ** user with updated BT state.
608 *******************************************************************************/
610 void btif_disable_bluetooth_evt(void)
612 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
614 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
615 bte_main_enable_lpm(FALSE);
618 #if (BLE_INCLUDED == TRUE)
624 /* callback to HAL */
625 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
628 /*******************************************************************************
630 ** Function btif_shutdown_bluetooth
632 ** Description Finalizes BT scheduler shutdown and terminates BTIF
637 *******************************************************************************/
639 bt_status_t btif_shutdown_bluetooth(void)
641 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
643 btif_transfer_context(btif_jni_disassociate, 0, NULL, 0, NULL);
645 btif_queue_release();
647 thread_free(bt_jni_workqueue_thread);
648 bt_jni_workqueue_thread = NULL;
654 BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
656 return BT_STATUS_SUCCESS;
659 /*******************************************************************************
661 ** Function btif_dut_mode_cback
663 ** Description Callback invoked on completion of vendor specific test mode command
667 *******************************************************************************/
668 static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
671 /* For now nothing to be done. */
674 /*******************************************************************************
676 ** Function btif_dut_mode_configure
678 ** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
681 ** Returns BT_STATUS_SUCCESS on success
683 *******************************************************************************/
684 bt_status_t btif_dut_mode_configure(uint8_t enable)
686 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
688 if (!stack_manager_get_interface()->get_stack_is_running()) {
689 BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
690 return BT_STATUS_NOT_READY;
693 btif_dut_mode = enable;
695 BTA_EnableTestMode();
697 BTA_DisableTestMode();
699 return BT_STATUS_SUCCESS;
702 /*******************************************************************************
704 ** Function btif_dut_mode_send
706 ** Description Sends a HCI Vendor specific command to the controller
708 ** Returns BT_STATUS_SUCCESS on success
710 *******************************************************************************/
711 bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
713 /* TODO: Check that opcode is a vendor command group */
714 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
715 if (!btif_is_dut_mode()) {
716 BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1.");
717 return BT_STATUS_FAIL;
719 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
720 return BT_STATUS_SUCCESS;
723 /*****************************************************************************
725 ** btif api adapter property functions
727 *****************************************************************************/
729 static bt_status_t btif_in_get_adapter_properties(void)
731 bt_property_t properties[6];
737 uint32_t disc_timeout;
738 bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
739 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
743 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
744 sizeof(addr), &addr);
745 btif_storage_get_adapter_property(&properties[num_props]);
749 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
750 sizeof(name), &name);
751 btif_storage_get_adapter_property(&properties[num_props]);
755 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
756 sizeof(mode), &mode);
757 btif_storage_get_adapter_property(&properties[num_props]);
761 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
762 sizeof(disc_timeout), &disc_timeout);
763 btif_storage_get_adapter_property(&properties[num_props]);
767 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
768 sizeof(bonded_devices), bonded_devices);
769 btif_storage_get_adapter_property(&properties[num_props]);
773 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
774 sizeof(local_uuids), local_uuids);
775 btif_storage_get_adapter_property(&properties[num_props]);
778 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
779 BT_STATUS_SUCCESS, num_props, properties);
781 return BT_STATUS_SUCCESS;
784 static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
786 bt_property_t remote_properties[8];
787 uint32_t num_props = 0;
789 bt_bdname_t name, alias;
790 uint32_t cod, devtype;
791 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
793 memset(remote_properties, 0, sizeof(remote_properties));
794 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
795 sizeof(name), &name);
796 btif_storage_get_remote_device_property(bd_addr,
797 &remote_properties[num_props]);
800 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
801 sizeof(alias), &alias);
802 btif_storage_get_remote_device_property(bd_addr,
803 &remote_properties[num_props]);
806 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
808 btif_storage_get_remote_device_property(bd_addr,
809 &remote_properties[num_props]);
812 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
813 sizeof(devtype), &devtype);
814 btif_storage_get_remote_device_property(bd_addr,
815 &remote_properties[num_props]);
818 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
819 sizeof(remote_uuids), remote_uuids);
820 btif_storage_get_remote_device_property(bd_addr,
821 &remote_properties[num_props]);
824 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
825 BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
827 return BT_STATUS_SUCCESS;
831 /*******************************************************************************
833 ** Function execute_storage_request
835 ** Description Executes adapter storage request in BTIF context
837 ** Returns bt_status_t
839 *******************************************************************************/
841 static void execute_storage_request(UINT16 event, char *p_param)
843 bt_status_t status = BT_STATUS_SUCCESS;
845 BTIF_TRACE_EVENT("execute storage request event : %d", event);
849 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
851 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
852 bt_property_t *p_prop = &(p_req->write_req.prop);
853 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
854 p_prop->len, p_prop->val);
856 status = btif_storage_set_adapter_property(p_prop);
857 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
860 case BTIF_CORE_STORAGE_ADAPTER_READ:
862 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
865 prop.type = p_req->read_req.type;
866 prop.val = (void*)buf;
867 prop.len = sizeof(buf);
868 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
870 #if (BLE_INCLUDED == TRUE)
871 tBTM_BLE_VSC_CB cmn_vsc_cb;
872 bt_local_le_features_t local_le_features;
874 /* LE features are not stored in storage. Should be retrived from stack */
875 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
876 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
878 prop.len = sizeof (bt_local_le_features_t);
879 if (cmn_vsc_cb.filter_support == 1)
880 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
882 local_le_features.max_adv_filter_supported = 0;
883 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
884 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
885 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
886 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
887 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
888 local_le_features.version_supported = cmn_vsc_cb.version_supported;
889 local_le_features.total_trackable_advertisers =
890 cmn_vsc_cb.total_trackable_advertisers;
891 memcpy(prop.val, &local_le_features, prop.len);
896 status = btif_storage_get_adapter_property(&prop);
898 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
901 case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
903 status = btif_in_get_adapter_properties();
906 case BTIF_CORE_STORAGE_NOTIFY_STATUS:
908 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
912 BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
917 static void execute_storage_remote_request(UINT16 event, char *p_param)
919 bt_status_t status = BT_STATUS_FAIL;
922 BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
926 case BTIF_CORE_STORAGE_REMOTE_READ:
929 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
930 prop.type = p_req->read_req.type;
931 prop.val = (void*) buf;
932 prop.len = sizeof(buf);
934 status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
936 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
937 status, &(p_req->read_req.bd_addr), 1, &prop);
939 case BTIF_CORE_STORAGE_REMOTE_WRITE:
941 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
942 status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
943 &(p_req->write_req.prop));
945 case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
947 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
948 btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
953 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
954 bt_property_t *p_props)
956 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
957 status, num_props, p_props);
960 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
961 uint32_t num_props, bt_property_t *p_props)
963 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
964 status, remote_addr, num_props, p_props);
967 /*******************************************************************************
969 ** Function btif_in_storage_request_copy_cb
971 ** Description Switch context callback function to perform the deep copy for
972 ** both the adapter and remote_device property API
976 *******************************************************************************/
977 static void btif_in_storage_request_copy_cb(UINT16 event,
978 char *p_new_buf, char *p_old_buf)
980 btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
981 btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
983 BTIF_TRACE_EVENT("%s", __FUNCTION__);
986 case BTIF_CORE_STORAGE_REMOTE_WRITE:
987 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
989 bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
990 /* Copy the member variables one at a time */
991 new_req->write_req.prop.type = old_req->write_req.prop.type;
992 new_req->write_req.prop.len = old_req->write_req.prop.len;
994 new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
995 memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
996 old_req->write_req.prop.len);
1001 /*******************************************************************************
1003 ** Function btif_get_adapter_properties
1005 ** Description Fetch all available properties (local & remote)
1007 ** Returns bt_status_t
1009 *******************************************************************************/
1011 bt_status_t btif_get_adapter_properties(void)
1013 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1015 if (!btif_is_enabled())
1016 return BT_STATUS_NOT_READY;
1018 return btif_transfer_context(execute_storage_request,
1019 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1023 /*******************************************************************************
1025 ** Function btif_get_adapter_property
1027 ** Description Fetches property value from local cache
1029 ** Returns bt_status_t
1031 *******************************************************************************/
1033 bt_status_t btif_get_adapter_property(bt_property_type_t type)
1035 btif_storage_req_t req;
1037 BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
1039 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1040 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1041 return BT_STATUS_NOT_READY;
1043 memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1044 req.read_req.type = type;
1046 return btif_transfer_context(execute_storage_request,
1047 BTIF_CORE_STORAGE_ADAPTER_READ,
1048 (char*)&req, sizeof(btif_storage_req_t), NULL);
1051 /*******************************************************************************
1053 ** Function btif_set_adapter_property
1055 ** Description Updates core stack with property value and stores it in
1058 ** Returns bt_status_t
1060 *******************************************************************************/
1062 bt_status_t btif_set_adapter_property(const bt_property_t *property)
1064 btif_storage_req_t req;
1065 bt_status_t status = BT_STATUS_SUCCESS;
1066 int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1067 char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1068 UINT16 name_len = 0;
1070 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
1071 property->type, property->len, property->val);
1073 if (!btif_is_enabled())
1074 return BT_STATUS_NOT_READY;
1076 switch(property->type)
1078 case BT_PROPERTY_BDNAME:
1080 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1082 memcpy(bd_name,property->val, name_len);
1083 bd_name[name_len] = '\0';
1085 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
1087 BTA_DmSetDeviceName((char *)bd_name);
1089 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1093 case BT_PROPERTY_ADAPTER_SCAN_MODE:
1095 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1096 tBTA_DM_DISC disc_mode;
1097 tBTA_DM_CONN conn_mode;
1101 case BT_SCAN_MODE_NONE:
1102 disc_mode = BTA_DM_NON_DISC;
1103 conn_mode = BTA_DM_NON_CONN;
1106 case BT_SCAN_MODE_CONNECTABLE:
1107 disc_mode = BTA_DM_NON_DISC;
1108 conn_mode = BTA_DM_CONN;
1111 case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1112 disc_mode = BTA_DM_GENERAL_DISC;
1113 conn_mode = BTA_DM_CONN;
1117 BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
1118 return BT_STATUS_PARM_INVALID;
1121 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
1123 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1125 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1128 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1130 /* Nothing to do beside store the value in NV. Java
1131 will change the SCAN_MODE property after setting timeout,
1133 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1136 case BT_PROPERTY_BDADDR:
1137 case BT_PROPERTY_UUIDS:
1138 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1139 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1140 /* no write support through HAL, these properties are only populated from BTA events */
1141 status = BT_STATUS_FAIL;
1144 BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
1146 status = BT_STATUS_FAIL;
1150 if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1152 /* pass on to storage for updating local database */
1154 memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1155 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1157 return btif_transfer_context(execute_storage_request,
1160 sizeof(btif_storage_req_t)+property->len,
1161 btif_in_storage_request_copy_cb);
1168 /*******************************************************************************
1170 ** Function btif_get_remote_device_property
1172 ** Description Fetches the remote device property from the NVRAM
1174 ** Returns bt_status_t
1176 *******************************************************************************/
1177 bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1178 bt_property_type_t type)
1180 btif_storage_req_t req;
1182 if (!btif_is_enabled())
1183 return BT_STATUS_NOT_READY;
1185 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1186 req.read_req.type = type;
1187 return btif_transfer_context(execute_storage_remote_request,
1188 BTIF_CORE_STORAGE_REMOTE_READ,
1189 (char*)&req, sizeof(btif_storage_req_t),
1193 /*******************************************************************************
1195 ** Function btif_get_remote_device_properties
1197 ** Description Fetches all the remote device properties from NVRAM
1199 ** Returns bt_status_t
1201 *******************************************************************************/
1202 bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1204 btif_storage_req_t req;
1206 if (!btif_is_enabled())
1207 return BT_STATUS_NOT_READY;
1209 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1210 return btif_transfer_context(execute_storage_remote_request,
1211 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1212 (char*)&req, sizeof(btif_storage_req_t),
1216 /*******************************************************************************
1218 ** Function btif_set_remote_device_property
1220 ** Description Writes the remote device property to NVRAM.
1221 ** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1222 ** remote device property that can be set
1224 ** Returns bt_status_t
1226 *******************************************************************************/
1227 bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1228 const bt_property_t *property)
1230 btif_storage_req_t req;
1232 if (!btif_is_enabled())
1233 return BT_STATUS_NOT_READY;
1235 memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1236 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1238 return btif_transfer_context(execute_storage_remote_request,
1239 BTIF_CORE_STORAGE_REMOTE_WRITE,
1241 sizeof(btif_storage_req_t)+property->len,
1242 btif_in_storage_request_copy_cb);
1246 /*******************************************************************************
1248 ** Function btif_get_remote_service_record
1250 ** Description Looks up the service matching uuid on the remote device
1251 ** and fetches the SCN and service_name if the UUID is found
1253 ** Returns bt_status_t
1255 *******************************************************************************/
1256 bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1259 if (!btif_is_enabled())
1260 return BT_STATUS_NOT_READY;
1262 return btif_dm_get_remote_service_record(remote_addr, uuid);
1266 /*******************************************************************************
1268 ** Function btif_get_enabled_services_mask
1270 ** Description Fetches currently enabled services
1272 ** Returns tBTA_SERVICE_MASK
1274 *******************************************************************************/
1276 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1278 return btif_enabled_services;
1281 /*******************************************************************************
1283 ** Function btif_enable_service
1285 ** Description Enables the service 'service_ID' to the service_mask.
1286 ** Upon BT enable, BTIF core shall invoke the BTA APIs to
1287 ** enable the profiles
1289 ** Returns bt_status_t
1291 *******************************************************************************/
1292 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1294 tBTA_SERVICE_ID *p_id = &service_id;
1296 /* If BT is enabled, we need to switch to BTIF context and trigger the
1297 * enable for that profile
1299 * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1300 * enable for the profiles that have been enabled */
1302 btif_enabled_services |= (1 << service_id);
1304 BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1306 if (btif_is_enabled())
1308 btif_transfer_context(btif_dm_execute_service_request,
1309 BTIF_DM_ENABLE_SERVICE,
1310 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1313 return BT_STATUS_SUCCESS;
1315 /*******************************************************************************
1317 ** Function btif_disable_service
1319 ** Description Disables the service 'service_ID' to the service_mask.
1320 ** Upon BT disable, BTIF core shall invoke the BTA APIs to
1321 ** disable the profiles
1323 ** Returns bt_status_t
1325 *******************************************************************************/
1326 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1328 tBTA_SERVICE_ID *p_id = &service_id;
1330 /* If BT is enabled, we need to switch to BTIF context and trigger the
1331 * disable for that profile so that the appropriate uuid_property_changed will
1332 * be triggerred. Otherwise, we just need to clear the service_id in the mask
1335 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
1337 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1339 if (btif_is_enabled())
1341 btif_transfer_context(btif_dm_execute_service_request,
1342 BTIF_DM_DISABLE_SERVICE,
1343 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1346 return BT_STATUS_SUCCESS;
1349 static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
1350 BTIF_TRACE_DEBUG("%s Associating thread to JVM", __func__);
1351 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
1354 static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
1355 BTIF_TRACE_DEBUG("%s Disassociating thread from JVM", __func__);
1356 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
1357 bt_hal_cbacks = NULL;
1358 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);