diff options
author | Ganesh Ganapathi Batta <ganeshg@broadcom.com> | 2012-05-04 14:30:30 -0700 |
---|---|---|
committer | Matthew Xie <mattx@google.com> | 2012-07-14 11:19:20 -0700 |
commit | 78b0ba7aef165ed0e6a6d2b343bbc44a886c2dff (patch) | |
tree | ade5df25f87d56f31404ddb454f3f91cb964ad99 /btif | |
parent | 5ea31910ef79943ef321465e10c7707c68a49abd (diff) | |
download | external_bluetooth_bluedroid-78b0ba7aef165ed0e6a6d2b343bbc44a886c2dff.zip external_bluetooth_bluedroid-78b0ba7aef165ed0e6a6d2b343bbc44a886c2dff.tar.gz external_bluetooth_bluedroid-78b0ba7aef165ed0e6a6d2b343bbc44a886c2dff.tar.bz2 |
Implementation of auto pair logic (Automatically pair with legacy headsets and pointing devices with fixed pin key .0000.) in BT IF layer.
Change-Id: Iac4a141669f9a3766a72da09886c58cac67eae86
Diffstat (limited to 'btif')
-rwxr-xr-x[-rw-r--r--] | btif/include/btif_storage.h | 62 | ||||
-rwxr-xr-x[-rw-r--r--] | btif/src/btif_dm.c | 87 | ||||
-rwxr-xr-x | btif/src/btif_storage.c | 266 |
3 files changed, 407 insertions, 8 deletions
diff --git a/btif/include/btif_storage.h b/btif/include/btif_storage.h index 59cad1f..f4b2ca3 100644..100755 --- a/btif/include/btif_storage.h +++ b/btif/include/btif_storage.h @@ -269,7 +269,6 @@ bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr, UINT16 product_id, UINT16 version, UINT8 ctry_code, UINT16 dl_len, UINT8 *dsc_list); - /******************************************************************************* ** ** Function btif_storage_load_bonded_hid_info @@ -282,8 +281,69 @@ bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr, *******************************************************************************/ bt_status_t btif_storage_load_bonded_hid_info(void); +/******************************************************************************* +** +** Function btif_storage_remove_hid_info +** +** Description BTIF storage API - Deletes the bonded hid device info from NVRAM +** +** Returns BT_STATUS_SUCCESS if the deletion was successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ + bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr); +/******************************************************************************* +** +** Function btif_storage_load_autopair_device_list +** +** Description BTIF storage API - Populates auto pair device list +** +** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +bt_status_t btif_storage_load_autopair_device_list(); + +/******************************************************************************* +** +** Function btif_storage_is_device_autopair_blacklisted +** +** Description BTIF storage API Checks if the given device is blacklisted for auto pairing +** +** Returns TRUE if the device is found in the auto pair blacklist +** FALSE otherwise +** +*******************************************************************************/ + +BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_dev_addr); + +/******************************************************************************* +** +** Function btif_storage_add_device_to_autopair_blacklist +** +** Description BTIF storage API - Add a remote device to the auto pairing blacklist +** +** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ + +bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_dev_addr); + +/******************************************************************************* +** +** Function btif_storage_is_fixed_pin_zeros_keyboard +** +** Description BTIF storage API - checks if this device has fixed PIN key device list +** +** Returns TRUE if the device is found in the fixed pin keyboard device list +** FALSE otherwise +** +*******************************************************************************/ +BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_dev_addr); + #endif /* BTIF_STORAGE_H */ diff --git a/btif/src/btif_dm.c b/btif/src/btif_dm.c index 65de159..e7d672f 100644..100755 --- a/btif/src/btif_dm.c +++ b/btif/src/btif_dm.c @@ -75,9 +75,15 @@ ******************************************************************************/ #define COD_UNCLASSIFIED ((0x1F) << 8) -#define COD_HID_KEYBOARD 0x0540 -#define COD_HID_POINTING 0x0580 -#define COD_HID_COMBO 0x05C0 +#define COD_HID_KEYBOARD 0x0540 +#define COD_HID_POINTING 0x0580 +#define COD_HID_COMBO 0x05C0 +#define COD_AV_HEADSETS 0x0404 +#define COD_AV_HANDSFREE 0x0408 +#define COD_AV_HEADPHONES 0x0418 +#define COD_AV_PORTABLE_AUDIO 0x041C +#define COD_AV_HIFI_AUDIO 0x0428 + #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10 @@ -88,6 +94,7 @@ typedef struct { UINT8 is_temp; UINT8 pin_code_len; UINT8 is_ssp; + UINT8 autopair_attempts; } btif_dm_pairing_cb_t; typedef struct { @@ -496,6 +503,7 @@ static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req) bt_bdname_t bd_name; UINT32 cod; btif_dm_remote_name_t remote_param; + bt_pin_code_t pin_code; /* Remote name update */ bdcpy(remote_param.bd_addr , p_pin_req->bd_addr); @@ -510,10 +518,51 @@ static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req) cod = devclass2uint(p_pin_req->dev_class); if ( cod == 0) { - LOGD("cod is 0, set as unclassified"); + BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__); cod = COD_UNCLASSIFIED; } + if (check_cod(&bd_addr, COD_AV_HEADSETS) || + check_cod(&bd_addr, COD_AV_HANDSFREE) || + check_cod(&bd_addr, COD_AV_HEADPHONES) || + check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || + check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || + check_cod(&bd_addr, COD_HID_POINTING)) + { + BTIF_TRACE_DEBUG1("%s()cod matches for auto pair", __FUNCTION__); + /* Check if this device can be auto paired */ + if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) && + (pairing_cb.autopair_attempts == 0)) + { + BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__); + pin_code.pin[0] = 0x30; + pin_code.pin[1] = 0x30; + pin_code.pin[2] = 0x30; + pin_code.pin[3] = 0x30; + + pairing_cb.autopair_attempts++; + BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin); + return; + } + } + else if (check_cod(&bd_addr, COD_HID_KEYBOARD) || + check_cod(&bd_addr, COD_HID_COMBO)) + { + if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) && + (pairing_cb.autopair_attempts == 0)) + { + BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__); + pin_code.pin[0] = 0x30; + pin_code.pin[1] = 0x30; + pin_code.pin[2] = 0x30; + pin_code.pin[3] = 0x30; + + pairing_cb.autopair_attempts++; + BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin); + return; + } + } + HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod); } @@ -676,13 +725,37 @@ static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) break; case HCI_ERR_AUTH_FAILURE: - status = BT_STATUS_AUTH_FAILURE; + BTIF_TRACE_DEBUG1(" %s() Authentication fail ", __FUNCTION__); + if (pairing_cb.autopair_attempts == 1) + { + BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__); + + /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */ + if (check_cod(&bd_addr, COD_AV_HEADSETS) || + check_cod(&bd_addr, COD_AV_HANDSFREE) || + check_cod(&bd_addr, COD_AV_HEADPHONES) || + check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || + check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || + check_cod(&bd_addr, COD_HID_POINTING)) + { + btif_storage_add_device_to_autopair_blacklist (&bd_addr); + } + pairing_cb.autopair_attempts++; + + /* Create the Bond once again */ + BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__); + btif_dm_cb_create_bond (&bd_addr); + return; + } + else if (pairing_cb.autopair_attempts > 1) + { + status = BT_STATUS_AUTH_FAILURE; + } break; default: status = BT_STATUS_FAIL; } - } bond_state_changed(status, &bd_addr, state); } @@ -1023,6 +1096,8 @@ static void btif_dm_upstreams_evt(UINT16 event, char* p_param) */ btif_storage_load_bonded_devices(); + btif_storage_load_autopair_device_list(); + btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr); } break; diff --git a/btif/src/btif_storage.c b/btif/src/btif_storage.c index 764821b..856df6e 100755 --- a/btif/src/btif_storage.c +++ b/btif/src/btif_storage.c @@ -98,6 +98,7 @@ ***********************************************************************************/ #include <stdlib.h> #include <time.h> +#include <string.h> #include <hardware/bluetooth.h> @@ -128,11 +129,27 @@ #define BTIF_STORAGE_PATH_REMOTE_ALIASES "remote_aliases" #define BTIF_STORAGE_PATH_REMOTE_SERVICES "remote_services" #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "hid_info" - +#define BTIF_STORAGE_PATH_DYNAMIC_AUTOPAIR_BLACKLIST "" #define BTIF_STORAGE_KEY_ADAPTER_NAME "name" #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "scan_mode" #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "discovery_timeout" + +#define BTIF_AUTO_PAIR_CONF_FILE "/etc/bluetooth/auto_pairing.conf" +#define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "auto_pair_blacklist" +#define BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_ADDR "AddressBlacklist" +#define BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_EXACTNAME "ExactNameBlacklist" +#define BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_PARTIALNAME "PartialNameBlacklist" +#define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist" +#define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist" +#define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLIACKLIST_ADDR "DynamicAddressBlacklist" + +#define BTIF_AUTO_PAIR_CONF_VALUE_SEPERATOR "," +#define BTIF_AUTO_PAIR_CONF_SPACE ' ' +#define BTIF_AUTO_PAIR_CONF_COMMENT '#' +#define BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER "=" + + /* This is a local property to add a device found */ #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF @@ -1688,3 +1705,250 @@ bt_status_t btif_storage_write_hl_mdl_data(UINT8 app_idx, char *value, int value return bt_status; } + +/******************************************************************************* +** +** Function btif_storage_load_autopair_device_list +** +** Description BTIF storage API - Populates auto pair device list +** +** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +bt_status_t btif_storage_load_autopair_device_list() +{ + char *fname, *key_name, *key_value; + int ret , i=0; + char linebuf[BTIF_STORAGE_MAX_LINE_SZ]; + char *line; + FILE *fp; + + /* check if the auto pair device list is already present in the NV memory */ + fname = btif_in_make_filename(NULL, BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST); + + if (fname == NULL) + { + return BT_STATUS_FAIL; + } + + key_value = unv_read_key(fname,BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_ADDR,linebuf, BTIF_STORAGE_MAX_LINE_SZ); + + if (key_value == NULL) + { + /* first time loading of auto pair blacklist configuration */ + ret = unv_create_file(fname); + + if (ret < 0) + { + LOGE("%s: Failed to create dynamic auto pair blacklist", __FUNCTION__); + return BT_STATUS_FAIL; + } + fp = fopen (BTIF_AUTO_PAIR_CONF_FILE, "r"); + + if (fp == NULL) + { + LOGE("%s: Failed to open auto pair blacklist conf file at %s", __FUNCTION__,BTIF_AUTO_PAIR_CONF_FILE ); + return BT_STATUS_FAIL; + } + + /* read through auto_pairing.conf file and create the key value pairs specific to auto pair blacklist devices */ + while (fgets(linebuf, BTIF_STORAGE_MAX_LINE_SZ, fp) != NULL) + { + /* trip leading white spaces */ + while (linebuf[i] == BTIF_AUTO_PAIR_CONF_SPACE) + i++; + + /* skip commented lines */ + if (linebuf[i] == BTIF_AUTO_PAIR_CONF_COMMENT) + continue; + + line = (char*)&(linebuf[i]); + + if (line == NULL) + continue; + + key_name = strtok(line, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER); + + if (key_name == NULL) + continue; + else if((strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_ADDR) == 0) || + (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_EXACTNAME) ==0) || + (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST) ==0 ) || + (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_PARTIALNAME) == 0) || + (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLIACKLIST_ADDR) == 0)) + { + key_value = strtok(NULL, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER); + unv_write_key (fname, key_name, key_value); + } + } + fclose(fp); + } + return BT_STATUS_SUCCESS; +} + +/******************************************************************************* +** +** Function btif_storage_is_device_autopair_blacklisted +** +** Description BTIF storage API Checks if the given device is blacklisted for auto pairing +** +** Returns TRUE if the device is found in the auto pair blacklist +** FALSE otherwise +** +*******************************************************************************/ +BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_dev_addr) +{ + char *fname; + char *value, *token; + int ret; + bdstr_t bdstr; + char bd_addr_lap[9]; + char *dev_name_str; + char linebuf[BTIF_STORAGE_MAX_LINE_SZ]; + + bd2str(remote_dev_addr, &bdstr); + + /* create a string with Lower Address Part from BD Address */ + snprintf(bd_addr_lap, 9, "%s", (char*)bdstr); + + /* create filepath */ + fname = btif_in_make_filename(NULL, BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST); + + /* check if this device address LAP is same as one of the auto pair blackliseted LAP */ + value = unv_read_key( fname, + BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_ADDR, + linebuf, BTIF_STORAGE_MAX_LINE_SZ); + if (value != NULL) + { + if (strstr(value,bd_addr_lap) != NULL) + return TRUE; + } + + dev_name_str = BTM_SecReadDevName((remote_dev_addr->address)); + + if (dev_name_str != NULL) + { + value = unv_read_key( fname, + BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_EXACTNAME, + linebuf, BTIF_STORAGE_MAX_LINE_SZ); + if (value != NULL) + { + if (strstr(value,dev_name_str) != NULL) + return TRUE; + } + + value = unv_read_key( fname, + BTIF_STORAGE_KEY_AUTOPAIR_BLIACKLIST_PARTIALNAME, + linebuf, BTIF_STORAGE_MAX_LINE_SZ); + if (value != NULL) + { + token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPERATOR); + while (token != NULL) + { + if (strstr(dev_name_str, token) != NULL) + return TRUE; + + token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPERATOR); + } + } + } + + value = unv_read_key( fname, + BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLIACKLIST_ADDR, + linebuf, BTIF_STORAGE_MAX_LINE_SZ); + if (value != NULL) + { + if (strstr(value,bdstr) != NULL) + return TRUE; + } + return FALSE; +} + +/******************************************************************************* +** +** Function btif_storage_add_device_to_autopair_blacklist +** +** Description BTIF storage API - Add a remote device to the auto pairing blacklist +** +** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_dev_addr) +{ + char *fname; + char *value; + int ret; + bdstr_t bdstr; + char linebuf[BTIF_STORAGE_MAX_LINE_SZ]; + char input_value [20]; + + bd2str(remote_dev_addr, &bdstr); + strncpy(input_value, (char*)bdstr, 20); + strncat (input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPERATOR, 20); + + /* create filepath */ + fname = btif_in_make_filename(NULL, BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST); + + if (fname == NULL) + { + return BT_STATUS_FAIL; + } + + value = unv_read_key( fname, + BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLIACKLIST_ADDR, + linebuf, BTIF_STORAGE_MAX_LINE_SZ); + if (value != NULL) + { + /* Append this address to the dynamic List of BD address */ + strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ); + } + else + { + strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ); + } + + /* Write back the key value */ + ret = unv_write_key (fname, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLIACKLIST_ADDR,( const char *)linebuf); + + return (ret == 0 ? BT_STATUS_SUCCESS:BT_STATUS_FAIL); +} + +/******************************************************************************* +** +** Function btif_storage_is_fixed_pin_zeros_keyboard +** +** Description BTIF storage API - checks if this device has fixed PIN key device list +** +** Returns TRUE if the device is found in the fixed pin keyboard device list +** FALSE otherwise +** +*******************************************************************************/ +BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_dev_addr) +{ + char *fname; + char *value; + int ret; + bdstr_t bdstr; + char bd_addr_lap[9]; + char *dev_name_str; + char linebuf[BTIF_STORAGE_MAX_LINE_SZ]; + + bd2str(remote_dev_addr, &bdstr); + snprintf(bd_addr_lap, 9, "%s", (char*)bdstr); + + /* create filepath */ + fname = btif_in_make_filename(NULL, BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST); + + value = unv_read_key( fname, + BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, + linebuf, BTIF_STORAGE_MAX_LINE_SZ); + + if (value != NULL) + { + if (strstr(bd_addr_lap, value) != NULL) + return TRUE; + } + return FALSE; +} |