diff options
author | Kausik Sinnaswamy <kausik@broadcom.com> | 2012-04-11 17:56:17 +0530 |
---|---|---|
committer | Matthew Xie <mattx@google.com> | 2012-07-14 11:19:17 -0700 |
commit | 35207a7fe091c5ade3ac3a80acf8366beb6e21da (patch) | |
tree | 64124a91e7d597677b16962c528d7f7b495d1150 /btif | |
parent | 2f9c0a7966a08936e8ae7a03ab8fbf1de3b22e0e (diff) | |
download | external_bluetooth_bluedroid-35207a7fe091c5ade3ac3a80acf8366beb6e21da.zip external_bluetooth_bluedroid-35207a7fe091c5ade3ac3a80acf8366beb6e21da.tar.gz external_bluetooth_bluedroid-35207a7fe091c5ade3ac3a80acf8366beb6e21da.tar.bz2 |
Added support to init/shutdown Bluetooth as part of
the HAL init/cleanup. This allows Java/JNI to query BD_ADDR and BD_NAME
prior to HAL BT_enable.
Change-Id: Ic56d859d75d6adaa134d42bb31da4f2c45e349d9
Diffstat (limited to 'btif')
-rwxr-xr-x | btif/include/btif_common.h | 18 | ||||
-rw-r--r-- | btif/src/btif_core.c | 143 | ||||
-rw-r--r-- | btif/src/btif_storage.c | 8 |
3 files changed, 133 insertions, 36 deletions
diff --git a/btif/include/btif_common.h b/btif/include/btif_common.h index 635caeb..3ecea9f 100755 --- a/btif/include/btif_common.h +++ b/btif/include/btif_common.h @@ -140,6 +140,24 @@ enum BTIF_DM_CB_HID_REMOTE_NAME, /* Remote name callback for HID device */ }; +/* Macro definitions for BD ADDR persistence */ +/* PROPERTY_BT_BDADDR_PATH + The property key stores the storage location of Bluetooth Device Address +*/ +#ifndef PROPERTY_BT_BDADDR_PATH +#define PROPERTY_BT_BDADDR_PATH "ro.bt.bdaddr_path" +#endif + +/* PERSIST_BDADDR_PROPERTY + If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH, + generating a random BDADDR and keeping it in the PERSIST_BDADDR_DROP. +*/ +#ifndef PERSIST_BDADDR_PROPERTY +#define PERSIST_BDADDR_PROPERTY "persist.service.bdroid.bdaddr" +#endif + +#define FACTORY_BT_BDADDR_STORAGE_LEN 17 + /************************************************************************************ ** Type definitions for callback functions ************************************************************************************/ diff --git a/btif/src/btif_core.c b/btif/src/btif_core.c index ebe2f8b..51328a8 100644 --- a/btif/src/btif_core.c +++ b/btif/src/btif_core.c @@ -54,10 +54,18 @@ * ***********************************************************************************/ +#include <stdlib.h> #include <hardware/bluetooth.h> #include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <dirent.h> +#include <ctype.h> +#include <cutils/properties.h> + #define LOG_TAG "BTIF_CORE" #include "btif_api.h" @@ -113,7 +121,7 @@ typedef union { /************************************************************************************ ** Static variables ************************************************************************************/ - +bt_bdaddr_t btif_local_bd_addr; /************************************************************************************ ** Static functions ************************************************************************************/ @@ -130,7 +138,7 @@ extern void bte_load_did_conf(const char *p_path); /** TODO: Move these to _common.h */ void bte_main_boot_entry(void); -void bte_main_enable(void); +void bte_main_enable(uint8_t *local_addr); void bte_main_disable(void); void bte_main_shutdown(void); #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) @@ -287,18 +295,6 @@ static void btif_task(UINT32 params) btif_disassociate_evt(); - GKI_task_self_cleanup(BTIF_TASK); - - bte_main_shutdown(); - - if (btif_shutdown_pending) - { - btif_shutdown_pending = 0; - - /* shutdown complete, all events notified and we reset HAL callbacks */ - bt_hal_cbacks = NULL; - } - BTIF_TRACE_DEBUG0("btif task exiting"); } @@ -318,6 +314,76 @@ void btif_sendmsg(void *p_msg) GKI_send_msg(BTIF_TASK, BTU_BTIF_MBOX, p_msg); } +static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr) +{ + char val[256]; + uint8_t valid_bda = FALSE; + const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0}; + + BTIF_TRACE_DEBUG1("Look for bdaddr storage path in prop %s", PROPERTY_BT_BDADDR_PATH); + + /* Get local bdaddr storage path from property */ + if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL)) + { + int addr_fd; + + BTIF_TRACE_DEBUG1("local bdaddr is stored in %s", val); + + if ((addr_fd = open(val, O_RDONLY)) != -1) + { + memset(val, 0, sizeof(val)); + read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN); + str2bd(val, local_addr); + /* If this is not a reserved/special bda, then use it */ + if (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0) + { + valid_bda = TRUE; + BTIF_TRACE_DEBUG6("Got Factory BDA %02X:%02X:%02X:%02X:%02X:%02X", + local_addr->address[0], local_addr->address[1], local_addr->address[2], + local_addr->address[3], local_addr->address[4], local_addr->address[5]); + } + + close(addr_fd); + } + } + + /* No factory BDADDR found. Look for previously generated random BDA */ + if ((!valid_bda) && \ + (property_get(PERSIST_BDADDR_PROPERTY, val, NULL))) + { + str2bd(val, local_addr); + valid_bda = TRUE; + BTIF_TRACE_DEBUG6("Got prior random BDA %02X:%02X:%02X:%02X:%02X:%02X", + local_addr->address[0], local_addr->address[1], local_addr->address[2], + local_addr->address[3], local_addr->address[4], local_addr->address[5]); + } + + /* Generate new BDA if necessary */ + if (!valid_bda) + { + bdstr_t bdstr; + /* Seed the random number generator */ + srand((unsigned int) (time(0))); + + /* No autogen BDA. Generate one now. */ + local_addr->address[0] = 0x22; + local_addr->address[1] = 0x22; + local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF); + local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF); + local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF); + local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF); + + /* Convert to ascii, and store as a persistent property */ + bd2str(local_addr, &bdstr); + + BTIF_TRACE_DEBUG2("No preset BDA. Generating BDA: %s for prop %s", + (char*)bdstr, PERSIST_BDADDR_PROPERTY); + + if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) + BTIF_TRACE_ERROR1("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY); + } +} + /***************************************************************************** ** ** btif core api functions @@ -336,8 +402,22 @@ void btif_sendmsg(void *p_msg) bt_status_t btif_init_bluetooth(void) { + UINT8 status; + bte_main_boot_entry(); + /* As part of the init, fetch the local BD ADDR */ + memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t)); + btif_fetch_local_bdaddr(&btif_local_bd_addr); + + /* start btif task */ + status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR, + (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE), + sizeof(btif_task_stack)); + + if (status != GKI_SUCCESS) + return BT_STATUS_FAIL; + return BT_STATUS_SUCCESS; } @@ -377,27 +457,19 @@ bt_status_t btif_enable_bluetooth(void) LOGI("btif_enable_bluetooth"); - /* initialize OS */ - GKI_init(); - if (btif_enabled == 1) { LOGD("already enabled\n"); return BT_STATUS_DONE; } - /* Start the BTIF task */ - status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR, - (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE), - sizeof(btif_task_stack)); - - if (status != BTA_SUCCESS) - return BT_STATUS_FAIL; - /* add return status for create tasks functions ? */ /* Create the GKI tasks and run them */ - bte_main_enable(); + bte_main_enable(btif_local_bd_addr.address); + + if (status != BTA_SUCCESS) + return BT_STATUS_FAIL; return BT_STATUS_SUCCESS; } @@ -525,7 +597,10 @@ void btif_disable_bluetooth_evt(void) /* update local state */ btif_enabled = 0; - GKI_send_event(BTIF_TASK, EVENT_MASK(GKI_SHUTDOWN_EVT)); + if (btif_shutdown_pending) + { + btif_shutdown_bluetooth(); + } } @@ -554,10 +629,11 @@ bt_status_t btif_shutdown_bluetooth(void) return BT_STATUS_SUCCESS; } - bte_main_shutdown(); + btif_shutdown_pending = 0; - /* shutdown complete, all events notified and we reset HAL callbacks */ - bt_hal_cbacks = NULL; + GKI_destroy_task(BTIF_TASK); + + bte_main_shutdown(); return BT_STATUS_SUCCESS; } @@ -580,6 +656,9 @@ static bt_status_t btif_disassociate_evt(void) HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM); + /* shutdown complete, all events notified and we reset HAL callbacks */ + bt_hal_cbacks = NULL; + return BT_STATUS_SUCCESS; } @@ -732,7 +811,6 @@ static void execute_storage_request(UINT16 event, char *p_param) prop.type = p_req->read_req.type; prop.val = (void*)buf; prop.len = sizeof(buf); - status = btif_storage_get_adapter_property(&prop); HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop); } break; @@ -875,7 +953,8 @@ bt_status_t btif_get_adapter_property(bt_property_type_t type) BTIF_TRACE_EVENT2("%s %d", __FUNCTION__, type); - if (btif_enabled == 0) + /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */ + if ((btif_enabled == 0) && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME)) return BT_STATUS_FAIL; memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t)); diff --git a/btif/src/btif_storage.c b/btif/src/btif_storage.c index 2c3ed69..e289634 100644 --- a/btif/src/btif_storage.c +++ b/btif/src/btif_storage.c @@ -112,6 +112,7 @@ #include "bta_hh_api.h" #include "btif_hh.h" + /************************************************************************************ ** Constants & Macros ************************************************************************************/ @@ -202,7 +203,7 @@ typedef struct { ** Extern variables ************************************************************************************/ extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID]; - +extern bt_bdaddr_t btif_local_bd_addr; /************************************************************************************ ** Static variables ************************************************************************************/ @@ -760,10 +761,9 @@ bt_status_t btif_storage_get_adapter_property(bt_property_t *property) { BD_ADDR addr; bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val; - BTM_GetLocalDeviceAddr(addr); - bdcpy(bd_addr->address, addr); + /* This has been cached in btif. Just fetch it from there */ + memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t)); property->len = sizeof(bt_bdaddr_t); - return BT_STATUS_SUCCESS; } else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES) |