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 <hardware/bluetooth.h>
32 #include <sys/types.h>
37 #include <cutils/properties.h>
39 #define LOG_TAG "BTIF_CORE"
48 #include "btif_storage.h"
49 #include "btif_util.h"
50 #include "btif_sock.h"
53 #include "btif_profile_queue.h"
54 #include "btif_config.h"
55 /************************************************************************************
57 ************************************************************************************/
59 #ifndef BTIF_TASK_STACK_SIZE
60 #define BTIF_TASK_STACK_SIZE 0x2000 /* In bytes */
63 #ifndef BTE_DID_CONF_FILE
64 #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
67 #define BTIF_TASK_STR ((INT8 *) "BTIF")
69 /************************************************************************************
70 ** Local type definitions
71 ************************************************************************************/
73 /* These type definitions are used when passing data from the HAL to BTIF context
74 * in the downstream path for the adapter and remote_device property APIs */
78 bt_property_type_t type;
79 } btif_storage_read_t;
84 } btif_storage_write_t;
87 btif_storage_read_t read_req;
88 btif_storage_write_t write_req;
92 BTIF_CORE_STATE_DISABLED = 0,
93 BTIF_CORE_STATE_ENABLING,
94 BTIF_CORE_STATE_ENABLED,
95 BTIF_CORE_STATE_DISABLING
98 /************************************************************************************
100 ************************************************************************************/
102 bt_bdaddr_t btif_local_bd_addr;
104 static UINT32 btif_task_stack[(BTIF_TASK_STACK_SIZE + 3) / 4];
106 /* holds main adapter state */
107 static btif_core_state_t btif_core_state = BTIF_CORE_STATE_DISABLED;
109 static int btif_shutdown_pending = 0;
110 static tBTA_SERVICE_MASK btif_enabled_services = 0;
113 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
114 * function in DUT mode.
116 * To set this, the btif_init_bluetooth needs to be called with argument as 1
118 static UINT8 btif_dut_mode = 0;
120 /************************************************************************************
122 ************************************************************************************/
123 static bt_status_t btif_associate_evt(void);
124 static bt_status_t btif_disassociate_evt(void);
126 /* sends message to btif task */
127 static void btif_sendmsg(void *p_msg);
129 /************************************************************************************
131 ************************************************************************************/
132 extern void bte_load_did_conf(const char *p_path);
134 /** TODO: Move these to _common.h */
135 void bte_main_boot_entry(void);
136 void bte_main_enable();
137 void bte_main_disable(void);
138 void bte_main_shutdown(void);
139 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
140 void bte_main_enable_lpm(BOOLEAN enable);
142 void bte_main_postload_cfg(void);
143 void btif_dm_execute_service_request(UINT16 event, char *p_param);
144 #ifdef BTIF_DM_OOB_TEST
145 void btif_dm_load_local_oob(void);
147 void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
149 /************************************************************************************
151 ************************************************************************************/
154 /*****************************************************************************
155 ** Context switching functions
156 *****************************************************************************/
159 /*******************************************************************************
161 ** Function btif_context_switched
163 ** Description Callback used to execute transferred context callback
165 ** p_msg : message to be executed in btif context
169 *******************************************************************************/
171 static void btif_context_switched(void *p_msg)
173 tBTIF_CONTEXT_SWITCH_CBACK *p;
175 BTIF_TRACE_VERBOSE("btif_context_switched");
177 p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
179 /* each callback knows how to parse the data */
181 p->p_cb(p->event, p->p_param);
185 /*******************************************************************************
187 ** Function btif_transfer_context
189 ** Description This function switches context to btif task
191 ** p_cback : callback used to process message in btif context
192 ** event : event id of message
193 ** p_params : parameter area passed to callback (copied)
194 ** param_len : length of parameter area
195 ** p_copy_cback : If set this function will be invoked for deep copy
199 *******************************************************************************/
201 bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
203 tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
205 BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len);
207 /* allocate and send message that will be executed in btif context */
208 if ((p_msg = (tBTIF_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
210 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
211 p_msg->p_cb = p_cback;
213 p_msg->event = event; /* callback event */
215 /* check if caller has provided a copy callback to do the deep copy */
218 p_copy_cback(event, p_msg->p_param, p_params);
222 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
226 return BT_STATUS_SUCCESS;
230 /* let caller deal with a failed allocation */
231 return BT_STATUS_NOMEM;
235 /*******************************************************************************
237 ** Function btif_is_dut_mode
239 ** Description checks if BTIF is currently in DUT mode
241 ** Returns 1 if test mode, otherwize 0
243 *******************************************************************************/
245 UINT8 btif_is_dut_mode(void)
247 return (btif_dut_mode == 1);
250 /*******************************************************************************
252 ** Function btif_is_enabled
254 ** Description checks if main adapter is fully enabled
256 ** Returns 1 if fully enabled, otherwize 0
258 *******************************************************************************/
260 int btif_is_enabled(void)
262 return ((!btif_is_dut_mode()) && (btif_core_state == BTIF_CORE_STATE_ENABLED));
265 /*******************************************************************************
267 ** Function btif_task
269 ** Description BTIF task handler managing all messages being passed
270 ** Bluetooth HAL and BTA.
274 *******************************************************************************/
276 static void btif_task(UINT32 params)
282 BTIF_TRACE_DEBUG("btif task starting");
284 btif_associate_evt();
288 /* wait for specified events */
289 event = GKI_wait(0xFFFF, 0);
292 * Wait for the trigger to init chip and stack. This trigger will
293 * be received by btu_task once the UART is opened and ready
295 if (event == BT_EVT_TRIGGER_STACK_INIT)
297 BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
298 #if (BLE_INCLUDED == TRUE)
299 btif_dm_load_ble_local_keys();
301 BTA_EnableBluetooth(bte_dm_evt);
305 * Failed to initialize controller hardware, reset state and bring
308 if (event == BT_EVT_HARDWARE_INIT_FAIL)
310 BTIF_TRACE_DEBUG("btif_task: hardware init failed");
312 btif_queue_release();
313 GKI_task_self_cleanup(BTIF_TASK);
316 btif_core_state = BTIF_CORE_STATE_DISABLED;
317 HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
321 if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
324 if(event & TASK_MBOX_1_EVT_MASK)
326 while((p_msg = GKI_read_mbox(BTU_BTIF_MBOX)) != NULL)
328 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
330 switch (p_msg->event)
332 case BT_EVT_CONTEXT_SWITCH_EVT:
333 btif_context_switched(p_msg);
336 BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
345 btif_disassociate_evt();
347 BTIF_TRACE_DEBUG("btif task exiting");
351 /*******************************************************************************
353 ** Function btif_sendmsg
355 ** Description Sends msg to BTIF task
359 *******************************************************************************/
361 void btif_sendmsg(void *p_msg)
363 GKI_send_msg(BTIF_TASK, BTU_BTIF_MBOX, p_msg);
366 static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
369 uint8_t valid_bda = FALSE;
371 const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
373 /* Get local bdaddr storage path from property */
374 if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
378 BTIF_TRACE_DEBUG("local bdaddr is stored in %s", val);
380 if ((addr_fd = open(val, O_RDONLY)) != -1)
382 memset(val, 0, sizeof(val));
383 read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN);
384 str2bd(val, local_addr);
385 /* If this is not a reserved/special bda, then use it */
386 if (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)
389 BTIF_TRACE_DEBUG("Got Factory BDA %02X:%02X:%02X:%02X:%02X:%02X",
390 local_addr->address[0], local_addr->address[1], local_addr->address[2],
391 local_addr->address[3], local_addr->address[4], local_addr->address[5]);
400 val_size = sizeof(val);
401 if(btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
403 str2bd(val, local_addr);
404 BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val);
409 /* No factory BDADDR found. Look for previously generated random BDA */
410 if ((!valid_bda) && \
411 (property_get(PERSIST_BDADDR_PROPERTY, val, NULL)))
413 str2bd(val, local_addr);
415 BTIF_TRACE_DEBUG("Got prior random BDA %02X:%02X:%02X:%02X:%02X:%02X",
416 local_addr->address[0], local_addr->address[1], local_addr->address[2],
417 local_addr->address[3], local_addr->address[4], local_addr->address[5]);
420 /* Generate new BDA if necessary */
424 /* Seed the random number generator */
425 srand((unsigned int) (time(0)));
427 /* No autogen BDA. Generate one now. */
428 local_addr->address[0] = 0x22;
429 local_addr->address[1] = 0x22;
430 local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
431 local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
432 local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
433 local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
435 /* Convert to ascii, and store as a persistent property */
436 bd2str(local_addr, &bdstr);
438 BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
439 (char*)bdstr, PERSIST_BDADDR_PROPERTY);
441 if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
442 BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
445 //save the bd address to config file
447 bd2str(local_addr, &bdstr);
448 val_size = sizeof(val);
449 if (btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
451 if (strcmp(bdstr, val) ==0)
453 // BDA is already present in the config file.
457 btif_config_set_str("Local", "Adapter", "Address", bdstr);
461 /*****************************************************************************
463 ** btif core api functions
465 *****************************************************************************/
467 /*******************************************************************************
469 ** Function btif_init_bluetooth
471 ** Description Creates BTIF task and prepares BT scheduler for startup
473 ** Returns bt_status_t
475 *******************************************************************************/
477 bt_status_t btif_init_bluetooth()
481 bte_main_boot_entry();
483 /* As part of the init, fetch the local BD ADDR */
484 memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
485 btif_fetch_local_bdaddr(&btif_local_bd_addr);
487 /* start btif task */
488 status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
489 (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
490 sizeof(btif_task_stack));
492 if (status != GKI_SUCCESS)
493 return BT_STATUS_FAIL;
495 return BT_STATUS_SUCCESS;
498 /*******************************************************************************
500 ** Function btif_associate_evt
502 ** Description Event indicating btif_task is up
503 ** Attach btif_task to JVM
507 *******************************************************************************/
509 static bt_status_t btif_associate_evt(void)
511 BTIF_TRACE_DEBUG("%s: notify ASSOCIATE_JVM", __FUNCTION__);
512 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
514 return BT_STATUS_SUCCESS;
518 /*******************************************************************************
520 ** Function btif_enable_bluetooth
522 ** Description Performs chip power on and kickstarts OS scheduler
524 ** Returns bt_status_t
526 *******************************************************************************/
528 bt_status_t btif_enable_bluetooth(void)
530 BTIF_TRACE_DEBUG("BTIF ENABLE BLUETOOTH");
532 if (btif_core_state != BTIF_CORE_STATE_DISABLED)
534 ALOGD("not disabled\n");
535 return BT_STATUS_DONE;
538 btif_core_state = BTIF_CORE_STATE_ENABLING;
540 /* Create the GKI tasks and run them */
543 return BT_STATUS_SUCCESS;
547 /*******************************************************************************
549 ** Function btif_enable_bluetooth_evt
551 ** Description Event indicating bluetooth enable is completed
552 ** Notifies HAL user with updated adapter state
556 *******************************************************************************/
558 void btif_enable_bluetooth_evt(tBTA_STATUS status, BD_ADDR local_bd)
563 bdcpy(bd_addr.address, local_bd);
564 BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status,
565 bd2str(&bd_addr, &bdstr));
567 if (bdcmp(btif_local_bd_addr.address,local_bd))
573 * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
574 * This could be because the factory BDADDR was stored separatley in
575 * the Controller's non-volatile memory rather than in device's file
578 BTIF_TRACE_WARNING("***********************************************");
579 BTIF_TRACE_WARNING("BTIF init BDA was %02X:%02X:%02X:%02X:%02X:%02X",
580 btif_local_bd_addr.address[0], btif_local_bd_addr.address[1],
581 btif_local_bd_addr.address[2], btif_local_bd_addr.address[3],
582 btif_local_bd_addr.address[4], btif_local_bd_addr.address[5]);
583 BTIF_TRACE_WARNING("Controller BDA is %02X:%02X:%02X:%02X:%02X:%02X",
584 local_bd[0], local_bd[1], local_bd[2],
585 local_bd[3], local_bd[4], local_bd[5]);
586 BTIF_TRACE_WARNING("***********************************************");
588 bdcpy(btif_local_bd_addr.address, local_bd);
590 //save the bd address to config file
591 bd2str(&btif_local_bd_addr, &buf);
592 btif_config_set_str("Local", "Adapter", "Address", buf);
595 //fire HAL callback for property change
596 memcpy(buf, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
597 prop.type = BT_PROPERTY_BDADDR;
598 prop.val = (void*)buf;
599 prop.len = sizeof(bt_bdaddr_t);
600 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
603 bte_main_postload_cfg();
604 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
605 bte_main_enable_lpm(TRUE);
607 /* add passing up bd address as well ? */
609 /* callback to HAL */
610 if (status == BTA_SUCCESS)
612 /* initialize a2dp service */
615 /* init rfcomm & l2cap api */
621 /* load did configuration */
622 bte_load_did_conf(BTE_DID_CONF_FILE);
624 #ifdef BTIF_DM_OOB_TEST
625 btif_dm_load_local_oob();
627 /* now fully enabled, update state */
628 btif_core_state = BTIF_CORE_STATE_ENABLED;
630 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON);
634 /* cleanup rfcomm & l2cap api */
639 /* we failed to enable, reset state */
640 btif_core_state = BTIF_CORE_STATE_DISABLED;
642 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
646 /*******************************************************************************
648 ** Function btif_disable_bluetooth
650 ** Description Inititates shutdown of Bluetooth system.
651 ** Any active links will be dropped and device entering
652 ** non connectable/discoverable mode
656 *******************************************************************************/
657 bt_status_t btif_disable_bluetooth(void)
661 if (!btif_is_enabled())
663 BTIF_TRACE_ERROR("btif_disable_bluetooth : not yet enabled");
664 return BT_STATUS_NOT_READY;
667 BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
669 btif_dm_on_disable();
670 btif_core_state = BTIF_CORE_STATE_DISABLING;
672 /* cleanup rfcomm & l2cap api */
677 status = BTA_DisableBluetooth();
681 if (status != BTA_SUCCESS)
683 BTIF_TRACE_ERROR("disable bt failed (%d)", status);
685 /* reset the original state to allow attempting disable again */
686 btif_core_state = BTIF_CORE_STATE_ENABLED;
688 return BT_STATUS_FAIL;
690 return BT_STATUS_SUCCESS;
693 /*******************************************************************************
695 ** Function btif_disable_bluetooth_evt
697 ** Description Event notifying BT disable is now complete.
698 ** Terminates main stack tasks and notifies HAL
699 ** user with updated BT state.
703 *******************************************************************************/
705 void btif_disable_bluetooth_evt(void)
707 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
709 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
710 bte_main_enable_lpm(FALSE);
713 #if (BLE_INCLUDED == TRUE)
719 /* update local state */
720 btif_core_state = BTIF_CORE_STATE_DISABLED;
722 /* callback to HAL */
723 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
725 if (btif_shutdown_pending)
727 BTIF_TRACE_DEBUG("%s: calling btif_shutdown_bluetooth", __FUNCTION__);
728 btif_shutdown_bluetooth();
733 /*******************************************************************************
735 ** Function btif_shutdown_bluetooth
737 ** Description Finalizes BT scheduler shutdown and terminates BTIF
742 *******************************************************************************/
744 bt_status_t btif_shutdown_bluetooth(void)
746 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
748 if (btif_core_state == BTIF_CORE_STATE_DISABLING)
750 BTIF_TRACE_WARNING("shutdown during disabling");
751 /* shutdown called before disabling is done */
752 btif_shutdown_pending = 1;
753 return BT_STATUS_NOT_READY;
756 if (btif_is_enabled())
758 BTIF_TRACE_WARNING("shutdown while still enabled, initiate disable");
760 /* shutdown called prior to disabling, initiate disable */
761 btif_disable_bluetooth();
762 btif_shutdown_pending = 1;
763 return BT_STATUS_NOT_READY;
766 btif_shutdown_pending = 0;
768 if (btif_core_state == BTIF_CORE_STATE_ENABLING)
770 // Java layer abort BT ENABLING, could be due to ENABLE TIMEOUT
771 // Direct call from cleanup()@bluetooth.c
772 // bring down HCI/Vendor lib
774 btif_core_state = BTIF_CORE_STATE_DISABLED;
775 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
778 GKI_destroy_task(BTIF_TASK);
779 btif_queue_release();
786 BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
788 return BT_STATUS_SUCCESS;
792 /*******************************************************************************
794 ** Function btif_disassociate_evt
796 ** Description Event indicating btif_task is going down
797 ** Detach btif_task to JVM
801 *******************************************************************************/
803 static bt_status_t btif_disassociate_evt(void)
805 BTIF_TRACE_DEBUG("%s: notify DISASSOCIATE_JVM", __FUNCTION__);
807 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
809 /* shutdown complete, all events notified and we reset HAL callbacks */
810 bt_hal_cbacks = NULL;
812 return BT_STATUS_SUCCESS;
815 /****************************************************************************
817 ** BTIF Test Mode APIs
819 *****************************************************************************/
820 /*******************************************************************************
822 ** Function btif_dut_mode_cback
824 ** Description Callback invoked on completion of vendor specific test mode command
828 *******************************************************************************/
829 static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
832 /* For now nothing to be done. */
835 /*******************************************************************************
837 ** Function btif_dut_mode_configure
839 ** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
842 ** Returns BT_STATUS_SUCCESS on success
844 *******************************************************************************/
845 bt_status_t btif_dut_mode_configure(uint8_t enable)
847 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
849 if (btif_core_state != BTIF_CORE_STATE_ENABLED) {
850 BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
851 return BT_STATUS_NOT_READY;
854 btif_dut_mode = enable;
856 BTA_EnableTestMode();
858 BTA_DisableTestMode();
860 return BT_STATUS_SUCCESS;
863 /*******************************************************************************
865 ** Function btif_dut_mode_send
867 ** Description Sends a HCI Vendor specific command to the controller
869 ** Returns BT_STATUS_SUCCESS on success
871 *******************************************************************************/
872 bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
874 /* TODO: Check that opcode is a vendor command group */
875 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
876 if (!btif_is_dut_mode()) {
877 BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1.");
878 return BT_STATUS_FAIL;
880 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
881 return BT_STATUS_SUCCESS;
884 /*****************************************************************************
886 ** btif api adapter property functions
888 *****************************************************************************/
890 static bt_status_t btif_in_get_adapter_properties(void)
892 bt_property_t properties[6];
898 uint32_t disc_timeout;
899 bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
900 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
904 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
905 sizeof(addr), &addr);
906 btif_storage_get_adapter_property(&properties[num_props]);
910 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
911 sizeof(name), &name);
912 btif_storage_get_adapter_property(&properties[num_props]);
916 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
917 sizeof(mode), &mode);
918 btif_storage_get_adapter_property(&properties[num_props]);
922 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
923 sizeof(disc_timeout), &disc_timeout);
924 btif_storage_get_adapter_property(&properties[num_props]);
928 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
929 sizeof(bonded_devices), bonded_devices);
930 btif_storage_get_adapter_property(&properties[num_props]);
934 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
935 sizeof(local_uuids), local_uuids);
936 btif_storage_get_adapter_property(&properties[num_props]);
939 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
940 BT_STATUS_SUCCESS, num_props, properties);
942 return BT_STATUS_SUCCESS;
945 static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
947 bt_property_t remote_properties[8];
948 uint32_t num_props = 0;
950 bt_bdname_t name, alias;
951 uint32_t cod, devtype;
952 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
954 memset(remote_properties, 0, sizeof(remote_properties));
955 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
956 sizeof(name), &name);
957 btif_storage_get_remote_device_property(bd_addr,
958 &remote_properties[num_props]);
961 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
962 sizeof(alias), &alias);
963 btif_storage_get_remote_device_property(bd_addr,
964 &remote_properties[num_props]);
967 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
969 btif_storage_get_remote_device_property(bd_addr,
970 &remote_properties[num_props]);
973 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
974 sizeof(devtype), &devtype);
975 btif_storage_get_remote_device_property(bd_addr,
976 &remote_properties[num_props]);
979 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
980 sizeof(remote_uuids), remote_uuids);
981 btif_storage_get_remote_device_property(bd_addr,
982 &remote_properties[num_props]);
985 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
986 BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
988 return BT_STATUS_SUCCESS;
992 /*******************************************************************************
994 ** Function execute_storage_request
996 ** Description Executes adapter storage request in BTIF context
998 ** Returns bt_status_t
1000 *******************************************************************************/
1002 static void execute_storage_request(UINT16 event, char *p_param)
1005 int num_entries = 0;
1006 bt_status_t status = BT_STATUS_SUCCESS;
1008 BTIF_TRACE_EVENT("execute storage request event : %d", event);
1012 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1014 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1015 bt_property_t *p_prop = &(p_req->write_req.prop);
1016 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
1017 p_prop->len, p_prop->val);
1019 status = btif_storage_set_adapter_property(p_prop);
1020 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
1023 case BTIF_CORE_STORAGE_ADAPTER_READ:
1025 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1028 prop.type = p_req->read_req.type;
1029 prop.val = (void*)buf;
1030 prop.len = sizeof(buf);
1031 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
1033 #if (BLE_INCLUDED == TRUE)
1034 tBTM_BLE_VSC_CB cmn_vsc_cb;
1035 bt_local_le_features_t local_le_features;
1037 /* LE features are not stored in storage. Should be retrived from stack */
1038 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1039 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1041 prop.len = sizeof (bt_local_le_features_t);
1042 if (cmn_vsc_cb.filter_support == 1)
1043 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1045 local_le_features.max_adv_filter_supported = 0;
1046 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1047 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1048 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1049 local_le_features.scan_result_storage_size_hibyte =
1050 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1051 local_le_features.scan_result_storage_size_lobyte =
1052 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
1053 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1054 memcpy(prop.val, &local_le_features, prop.len);
1059 status = btif_storage_get_adapter_property(&prop);
1061 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
1064 case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
1066 status = btif_in_get_adapter_properties();
1069 case BTIF_CORE_STORAGE_NOTIFY_STATUS:
1071 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
1075 BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
1080 static void execute_storage_remote_request(UINT16 event, char *p_param)
1082 bt_status_t status = BT_STATUS_FAIL;
1085 BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
1089 case BTIF_CORE_STORAGE_REMOTE_READ:
1092 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1093 prop.type = p_req->read_req.type;
1094 prop.val = (void*) buf;
1095 prop.len = sizeof(buf);
1097 status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
1099 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1100 status, &(p_req->read_req.bd_addr), 1, &prop);
1102 case BTIF_CORE_STORAGE_REMOTE_WRITE:
1104 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1105 status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
1106 &(p_req->write_req.prop));
1108 case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
1110 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1111 btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
1116 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
1117 bt_property_t *p_props)
1119 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1120 status, num_props, p_props);
1123 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
1124 uint32_t num_props, bt_property_t *p_props)
1126 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1127 status, remote_addr, num_props, p_props);
1130 /*******************************************************************************
1132 ** Function btif_in_storage_request_copy_cb
1134 ** Description Switch context callback function to perform the deep copy for
1135 ** both the adapter and remote_device property API
1139 *******************************************************************************/
1140 static void btif_in_storage_request_copy_cb(UINT16 event,
1141 char *p_new_buf, char *p_old_buf)
1143 btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
1144 btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
1146 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1149 case BTIF_CORE_STORAGE_REMOTE_WRITE:
1150 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1152 bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
1153 /* Copy the member variables one at a time */
1154 new_req->write_req.prop.type = old_req->write_req.prop.type;
1155 new_req->write_req.prop.len = old_req->write_req.prop.len;
1157 new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
1158 memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
1159 old_req->write_req.prop.len);
1164 /*******************************************************************************
1166 ** Function btif_get_adapter_properties
1168 ** Description Fetch all available properties (local & remote)
1170 ** Returns bt_status_t
1172 *******************************************************************************/
1174 bt_status_t btif_get_adapter_properties(void)
1176 BTIF_TRACE_EVENT("%s", __FUNCTION__);
1178 if (!btif_is_enabled())
1179 return BT_STATUS_NOT_READY;
1181 return btif_transfer_context(execute_storage_request,
1182 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1186 /*******************************************************************************
1188 ** Function btif_get_adapter_property
1190 ** Description Fetches property value from local cache
1192 ** Returns bt_status_t
1194 *******************************************************************************/
1196 bt_status_t btif_get_adapter_property(bt_property_type_t type)
1198 btif_storage_req_t req;
1200 BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
1202 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1203 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1204 return BT_STATUS_NOT_READY;
1206 memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1207 req.read_req.type = type;
1209 return btif_transfer_context(execute_storage_request,
1210 BTIF_CORE_STORAGE_ADAPTER_READ,
1211 (char*)&req, sizeof(btif_storage_req_t), NULL);
1214 /*******************************************************************************
1216 ** Function btif_set_adapter_property
1218 ** Description Updates core stack with property value and stores it in
1221 ** Returns bt_status_t
1223 *******************************************************************************/
1225 bt_status_t btif_set_adapter_property(const bt_property_t *property)
1227 btif_storage_req_t req;
1228 bt_status_t status = BT_STATUS_SUCCESS;
1229 int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1230 char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1231 UINT16 name_len = 0;
1233 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
1234 property->type, property->len, property->val);
1236 if (!btif_is_enabled())
1237 return BT_STATUS_NOT_READY;
1239 switch(property->type)
1241 case BT_PROPERTY_BDNAME:
1243 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1245 memcpy(bd_name,property->val, name_len);
1246 bd_name[name_len] = '\0';
1248 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
1250 BTA_DmSetDeviceName((char *)bd_name);
1252 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1256 case BT_PROPERTY_ADAPTER_SCAN_MODE:
1258 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1259 tBTA_DM_DISC disc_mode;
1260 tBTA_DM_CONN conn_mode;
1264 case BT_SCAN_MODE_NONE:
1265 disc_mode = BTA_DM_NON_DISC;
1266 conn_mode = BTA_DM_NON_CONN;
1269 case BT_SCAN_MODE_CONNECTABLE:
1270 disc_mode = BTA_DM_NON_DISC;
1271 conn_mode = BTA_DM_CONN;
1274 case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1275 disc_mode = BTA_DM_GENERAL_DISC;
1276 conn_mode = BTA_DM_CONN;
1280 BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
1281 return BT_STATUS_PARM_INVALID;
1284 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
1286 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1288 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1291 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1293 /* Nothing to do beside store the value in NV. Java
1294 will change the SCAN_MODE property after setting timeout,
1296 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1299 case BT_PROPERTY_BDADDR:
1300 case BT_PROPERTY_UUIDS:
1301 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1302 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1303 /* no write support through HAL, these properties are only populated from BTA events */
1304 status = BT_STATUS_FAIL;
1307 BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
1309 status = BT_STATUS_FAIL;
1313 if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1316 /* pass on to storage for updating local database */
1318 memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1319 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1321 return btif_transfer_context(execute_storage_request,
1324 sizeof(btif_storage_req_t)+property->len,
1325 btif_in_storage_request_copy_cb);
1332 /*******************************************************************************
1334 ** Function btif_get_remote_device_property
1336 ** Description Fetches the remote device property from the NVRAM
1338 ** Returns bt_status_t
1340 *******************************************************************************/
1341 bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1342 bt_property_type_t type)
1344 btif_storage_req_t req;
1346 if (!btif_is_enabled())
1347 return BT_STATUS_NOT_READY;
1349 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1350 req.read_req.type = type;
1351 return btif_transfer_context(execute_storage_remote_request,
1352 BTIF_CORE_STORAGE_REMOTE_READ,
1353 (char*)&req, sizeof(btif_storage_req_t),
1357 /*******************************************************************************
1359 ** Function btif_get_remote_device_properties
1361 ** Description Fetches all the remote device properties from NVRAM
1363 ** Returns bt_status_t
1365 *******************************************************************************/
1366 bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1368 btif_storage_req_t req;
1370 if (!btif_is_enabled())
1371 return BT_STATUS_NOT_READY;
1373 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1374 return btif_transfer_context(execute_storage_remote_request,
1375 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1376 (char*)&req, sizeof(btif_storage_req_t),
1380 /*******************************************************************************
1382 ** Function btif_set_remote_device_property
1384 ** Description Writes the remote device property to NVRAM.
1385 ** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1386 ** remote device property that can be set
1388 ** Returns bt_status_t
1390 *******************************************************************************/
1391 bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1392 const bt_property_t *property)
1394 btif_storage_req_t req;
1396 if (!btif_is_enabled())
1397 return BT_STATUS_NOT_READY;
1399 memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1400 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1402 return btif_transfer_context(execute_storage_remote_request,
1403 BTIF_CORE_STORAGE_REMOTE_WRITE,
1405 sizeof(btif_storage_req_t)+property->len,
1406 btif_in_storage_request_copy_cb);
1410 /*******************************************************************************
1412 ** Function btif_get_remote_service_record
1414 ** Description Looks up the service matching uuid on the remote device
1415 ** and fetches the SCN and service_name if the UUID is found
1417 ** Returns bt_status_t
1419 *******************************************************************************/
1420 bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1423 if (!btif_is_enabled())
1424 return BT_STATUS_NOT_READY;
1426 return btif_dm_get_remote_service_record(remote_addr, uuid);
1430 /*******************************************************************************
1432 ** Function btif_get_enabled_services_mask
1434 ** Description Fetches currently enabled services
1436 ** Returns tBTA_SERVICE_MASK
1438 *******************************************************************************/
1440 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1442 return btif_enabled_services;
1445 /*******************************************************************************
1447 ** Function btif_enable_service
1449 ** Description Enables the service 'service_ID' to the service_mask.
1450 ** Upon BT enable, BTIF core shall invoke the BTA APIs to
1451 ** enable the profiles
1453 ** Returns bt_status_t
1455 *******************************************************************************/
1456 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1458 tBTA_SERVICE_ID *p_id = &service_id;
1460 /* If BT is enabled, we need to switch to BTIF context and trigger the
1461 * enable for that profile
1463 * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1464 * enable for the profiles that have been enabled */
1466 btif_enabled_services |= (1 << service_id);
1468 BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1470 if (btif_is_enabled())
1472 btif_transfer_context(btif_dm_execute_service_request,
1473 BTIF_DM_ENABLE_SERVICE,
1474 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1477 return BT_STATUS_SUCCESS;
1479 /*******************************************************************************
1481 ** Function btif_disable_service
1483 ** Description Disables the service 'service_ID' to the service_mask.
1484 ** Upon BT disable, BTIF core shall invoke the BTA APIs to
1485 ** disable the profiles
1487 ** Returns bt_status_t
1489 *******************************************************************************/
1490 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1492 tBTA_SERVICE_ID *p_id = &service_id;
1494 /* If BT is enabled, we need to switch to BTIF context and trigger the
1495 * disable for that profile so that the appropriate uuid_property_changed will
1496 * be triggerred. Otherwise, we just need to clear the service_id in the mask
1499 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
1501 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1503 if (btif_is_enabled())
1505 btif_transfer_context(btif_dm_execute_service_request,
1506 BTIF_DM_DISABLE_SERVICE,
1507 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1510 return BT_STATUS_SUCCESS;
1513 /*******************************************************************************
1515 ** Function btif_config_hci_snoop_log
1517 ** Description enable or disable HCI snoop log
1519 ** Returns bt_status_t
1521 *******************************************************************************/
1522 bt_status_t btif_config_hci_snoop_log(uint8_t enable)
1524 bte_main_config_hci_logging(enable != 0,
1525 btif_core_state == BTIF_CORE_STATE_DISABLED);
1526 return BT_STATUS_SUCCESS;