bluedroid key miss问题

来源:互联网 发布:western结果分析软件 编辑:程序博客网 时间:2024/06/05 23:43

蓝牙ble设备由于与多个host配对时,host所在平台不能马上解除绑定,需要手动解绑,在做自动配对功能时,只有一个蓝牙设备可操作,因此需要添加从host中清除ble设备的操作
主要作法时是在encryption change出现key miss的反馈后,加上
1:从控制器中清除白名单(问题设备)
2:将设备从协议栈中解除绑定
3:删除gatt等上层信息etc

diff --git a/stack/btm/btm_sec.c b/stack/btm/btm_sec.cindex c8b1de7..a9d8cce 100644--- a/stack/btm/btm_sec.c+++ b/stack/btm/btm_sec.c@@ -36,6 +36,7 @@ #include "bt_utils.h" #include "osi/include/log.h"+#include "bta/gatt/bta_gattc_int.h" #if (BT_USE_TRACES == TRUE && BT_TRACE_VERBOSE == FALSE) /* needed for sprintf() */ #include <stdio.h>@@ -4278,11 +4279,25 @@ void btm_sec_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)         if (status == HCI_ERR_KEY_MISSING || status == HCI_ERR_AUTH_FAILURE ||             status == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE)         {+           tBTA_GATTC_SERV *p_srcb = NULL;+                       p_dev_rec->sec_flags &= ~ (BTM_SEC_LE_LINK_KEY_KNOWN);-            p_dev_rec->ble.key_type = BTM_LE_KEY_NONE;-        }-        btm_ble_link_encrypted(p_dev_rec->ble.pseudo_addr, encr_enable);-        return;+           btm_add_dev_to_controller(FALSE,p_dev_rec->bd_addr,0);+       btm_ble_link_encrypted(p_dev_rec->bd_addr, encr_enable);+       btif_dm_remove_bond(p_dev_rec->bd_addr);    ++        if( (p_srcb= bta_gattc_find_srcb(p_dev_rec->bd_addr))!= NULL)    +       {+          BTM_TRACE_DEBUG ("Reset this p_clrcb", __func__);             +          memset(p_srcb ,0,sizeof(tBTA_GATTC_SERV));       +         }+       +          }+      else+      {+       +           btm_ble_link_encrypted(p_dev_rec->bd_addr, encr_enable);+      }     }     else     {

Index: stack/btm/btm_sec.c===========================================================--- stack/btm/btm_sec.c (revision 423)+++ stack/btm/btm_sec.c (revision 424)@@ -50,6 +50,7 @@extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);extern BOOLEAN btm_add_dev_to_controller (BOOLEAN to_add, BD_ADDR bd_addr);** Function         btm_sec_encrypt_change**** Description      This function is when encryption of the connection is**                  completed by the LM**** Returns          void*********************************************************************************/void btm_sec_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable){    tBTM_SEC_DEV_REC  *p_dev_rec = btm_find_dev_by_handle (handle);#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE    tACL_CONN       *p_acl = NULL;    UINT8           acl_idx = btm_handle_to_acl_index(handle);#endif    BTM_TRACE_EVENT ("Security Manager: encrypt_change status:%d State:%d, encr_enable = %d",                      status, (p_dev_rec) ? p_dev_rec->sec_state : 0, encr_enable);    BTM_TRACE_DEBUG ("before update p_dev_rec->sec_flags=0x%x", (p_dev_rec) ? p_dev_rec->sec_flags : 0 );    /* For transaction collision we need to wait and repeat.  There is no need */    /* for random timeout because only slave should receive the result */    if ((status == HCI_ERR_LMP_ERR_TRANS_COLLISION) ||        (status == HCI_ERR_DIFF_TRANSACTION_COLLISION))    {        btm_sec_auth_collision(handle);        return;    }    btm_cb.collision_start_time = 0;    if (!p_dev_rec)        return;    if ((status == HCI_SUCCESS) && encr_enable)    {        if (p_dev_rec->hci_handle == handle) {            p_dev_rec->sec_flags |= (BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED);            if (p_dev_rec->pin_code_length >= 16 ||                p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||                p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {                p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;            }        }        else        {            p_dev_rec->sec_flags |= (BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED);        }    }    /* It is possible that we decrypted the link to perform role switch */    /* mark link not to be encrypted, so that when we execute security next time it will kick in again */    if ((status == HCI_SUCCESS) && !encr_enable)    {        if (p_dev_rec->hci_handle == handle)            p_dev_rec->sec_flags &= ~BTM_SEC_ENCRYPTED;        else            p_dev_rec->sec_flags &= ~BTM_SEC_LE_ENCRYPTED;    }    BTM_TRACE_DEBUG ("after update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags );#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE    if (acl_idx != MAX_L2CAP_LINKS)        p_acl = &btm_cb.acl_db[acl_idx];    if (p_acl != NULL)        btm_sec_check_pending_enc_req(p_dev_rec, p_acl->transport, encr_enable);    if (p_acl && p_acl->transport == BT_TRANSPORT_LE)    {        if (status == HCI_ERR_KEY_MISSING || status == HCI_ERR_AUTH_FAILURE ||            status == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE)        {            p_dev_rec->sec_flags &= ~ (BTM_SEC_LE_LINK_KEY_KNOWN);            p_dev_rec->ble.key_type = BTM_LE_KEY_NONE;        +btm_add_dev_to_controller(FALSE,p_dev_rec->bd_addr);        +btif_dm_remove_bond((const bt_bdaddr_t *)p_dev_rec->bd_addr);/*modify by toby*/        }        btm_ble_link_encrypted(p_dev_rec->ble.pseudo_addr, encr_enable);        return;    }    else    {        /* BR/EDR connection, update the encryption key size to be 16 as always */        p_dev_rec->enc_key_size = 16;    }     BTM_TRACE_DEBUG ("in %s new_encr_key_256 is %d",                       __func__, p_dev_rec->new_encryption_key_is_p256);
原创粉丝点击