bluedroid key miss问题

蓝牙协议栈分析hci日志的工具有:Frontline13.2-17.11.15307.15525_signed
蓝牙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.c
index 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);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值