Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6775832c authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

has_client: Disconnect profile when link key is not found

Bug: 312691809
Test: atest bluetooth_has_test
Tag: #feature
Change-Id: I5390fd9dbcea8d8675c640850f70d52231b432ed
parent 1b7e462c
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -1888,7 +1888,15 @@ class HasClientImpl : public HasClient {

    int result = BTM_SetEncryption(device->addr, BT_TRANSPORT_LE, nullptr,
                                   nullptr, BTM_BLE_SEC_ENCRYPT);
    LOG_INFO("Encryption required. Request result: 0x%02x", result);

    LOG_INFO("Encryption required for %s. Request result: 0x%02x",
             ADDRESS_TO_LOGGABLE_CSTR(device->addr), result);

    if (result == BTM_ERR_KEY_MISSING) {
      LOG_ERROR("Link key unknown for %s, disconnect profile",
                ADDRESS_TO_LOGGABLE_CSTR(device->addr));
      BTA_GATTC_Close(device->conn_id);
    }
  }

  void OnGattDisconnected(const tBTA_GATTC_CLOSE& evt) {
+27 −0
Original line number Diff line number Diff line
@@ -892,6 +892,9 @@ class HasClientTestBase : public ::testing::Test {

    ON_CALL(btm_interface, BTM_IsEncrypted(address, _))
        .WillByDefault(DoAll(Return(encryption_result)));

    ON_CALL(btm_interface, IsLinkKeyKnown(address, _))
        .WillByDefault(DoAll(Return(true)));
  }

  void InjectNotifyReadPresetResponse(uint16_t conn_id,
@@ -1323,6 +1326,30 @@ TEST_F(HasClientTest, test_service_discovery_complete_before_encryption) {
  Mock::VerifyAndClearExpectations(callbacks.get());
}

TEST_F(HasClientTest, test_disconnect_when_link_key_is_gone) {
  const RawAddress test_address = GetTestAddress(1);
  SetSampleDatabaseHasPresetsNtf(
      test_address, bluetooth::has::kFeatureBitHearingAidTypeBinaural);

  EXPECT_CALL(*callbacks,
              OnConnectionState(ConnectionState::DISCONNECTED, test_address))
      .Times(0);
  EXPECT_CALL(*callbacks,
              OnConnectionState(ConnectionState::CONNECTED, test_address))
      .Times(0);

  ON_CALL(btm_interface, BTM_IsEncrypted(test_address, _))
      .WillByDefault(DoAll(Return(false)));
  ON_CALL(btm_interface, SetEncryption(test_address, _, _, _, _))
      .WillByDefault(Return(BTM_ERR_KEY_MISSING));

  auto test_conn_id = GetTestConnId(test_address);
  EXPECT_CALL(gatt_interface, Close(test_conn_id)).Times(1);
  InjectConnectedEvent(test_address, GetTestConnId(test_address));

  Mock::VerifyAndClearExpectations(callbacks.get());
}

TEST_F(HasClientTest, test_reconnect_after_encryption_failed) {
  const RawAddress test_address = GetTestAddress(1);
  SetSampleDatabaseHasNoPresetChange(