From c887dd6ed1b54fc00ef1d697a665f9b614de104e Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Thu, 6 Jun 2019 13:54:55 +0200 Subject: [PATCH] Fix for Bluetooth connection being dropped after HCI Read Encryption Key Size If remote device stop the encryption before we call "Read Encryption Key Size", we might receive Insufficient Security, which means that link is no longer encrypted. In such cases we should stay connected, rather than disconnecting the link. Test: Connect to device that stop encryption right after encryption is complete, i.e. to change roles. Bug: 124301137 Bug: 132626699 Change-Id: Iab0fd9f357d18a6b048b971d0393fbb47fd4d793 --- stack/btu/btu_hcif.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/stack/btu/btu_hcif.cc b/stack/btu/btu_hcif.cc index 987d173b7..461242297 100644 --- a/stack/btu/btu_hcif.cc +++ b/stack/btu/btu_hcif.cc @@ -1131,6 +1131,14 @@ constexpr uint8_t MIN_KEY_SIZE = 7; static void read_encryption_key_size_complete_after_encryption_change(uint8_t status, uint16_t handle, uint8_t key_size) { + if (status == HCI_ERR_INSUFFCIENT_SECURITY) { + /* If remote device stop the encryption before we call "Read Encryption Key + * Size", we might receive Insufficient Security, which means that link is + * no longer encrypted. */ + LOG(INFO) << __func__ << ": encryption stopped on link: " << loghex(handle); + return; + } + if (status != HCI_SUCCESS) { LOG(INFO) << __func__ << ": disconnecting, status: " << loghex(status); btsnd_hcic_disconnect(handle, HCI_ERR_PEER_USER); @@ -2051,6 +2059,14 @@ static void btu_hcif_enhanced_flush_complete_evt(void) { **********************************************/ static void read_encryption_key_size_complete_after_key_refresh(uint8_t status, uint16_t handle, uint8_t key_size) { + if (status == HCI_ERR_INSUFFCIENT_SECURITY) { + /* If remote device stop the encryption before we call "Read Encryption Key + * Size", we might receive Insufficient Security, which means that link is + * no longer encrypted. */ + LOG(INFO) << __func__ << ": encryption stopped on link: " << loghex(handle); + return; + } + if (status != HCI_SUCCESS) { LOG(INFO) << __func__ << ": disconnecting, status: " << loghex(status); btsnd_hcic_disconnect(handle, HCI_ERR_PEER_USER); -- 2.11.0