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

Commit 1b7e462c authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

leaudio: Disconnect profile when link key is not found

Bug: 312691809
Test: atest bluetooth_le_audio_client_test
Tag: #feature
Change-Id: I6d31a0c0c49a51af74974473b648817d1605c10b
parent 721be9e4
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -1951,19 +1951,22 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
    }

    if (BTM_IsLinkKeyKnown(address, BT_TRANSPORT_LE)) {
    int result = BTM_SetEncryption(address, BT_TRANSPORT_LE, nullptr, nullptr,
                                   BTM_BLE_SEC_ENCRYPT);

      LOG(INFO) << __func__
                << "Encryption required. Request result: " << result;
      return;
    }
    LOG_INFO("Encryption required for %s. Request result: 0x%02x",
             ADDRESS_TO_LOGGABLE_CSTR(address), result);

    LOG(ERROR) << __func__ << " Encryption error";
    if (result == BTM_ERR_KEY_MISSING) {
      LOG_ERROR("Link key unknown for %s, disconnect profile",
                ADDRESS_TO_LOGGABLE_CSTR(address));
      le_audio::MetricsCollector::Get()->OnConnectionStateChanged(
          leAudioDevice->group_id_, address, ConnectionState::CONNECTED,
          le_audio::ConnectionStatus::FAILED);

      /* If link cannot be enctypted, disconnect profile */
      BTA_GATTC_Close(conn_id);
    }
  }

  void RegisterKnownNotifications(LeAudioDevice* leAudioDevice,
+30 −0
Original line number Diff line number Diff line
@@ -1656,6 +1656,9 @@ class UnicastTestNoInit : public Test {
    ON_CALL(mock_btm_interface_, BTM_IsEncrypted(address, _))
        .WillByDefault(DoAll(Return(isEncrypted)));

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

    EXPECT_CALL(mock_gatt_interface_,
                Open(gatt_if, address, BTM_BLE_DIRECT_CONNECTION, _))
        .Times(1);
@@ -3213,6 +3216,33 @@ TEST_F(UnicastTest, ConnectRemoteServiceDiscoveryCompleteBeforeEncryption) {
  Mock::VerifyAndClearExpectations(&mock_audio_hal_client_callbacks_);
}

TEST_F(UnicastTest, DisconnectWhenLinkKeyIsGone) {
  const RawAddress test_address0 = GetTestAddress(0);
  uint16_t conn_id = 1;
  SetSampleDatabaseEarbudsValid(conn_id, test_address0,
                                codec_spec_conf::kLeAudioLocationStereo,
                                codec_spec_conf::kLeAudioLocationStereo);
  EXPECT_CALL(mock_audio_hal_client_callbacks_,
              OnConnectionState(ConnectionState::DISCONNECTED, test_address0))
      .Times(1);

  ON_CALL(mock_btm_interface_, BTM_IsEncrypted(test_address0, _))
      .WillByDefault(DoAll(Return(false)));

  ON_CALL(mock_btm_interface_, SetEncryption(test_address0, _, _, _, _))
      .WillByDefault(Return(BTM_ERR_KEY_MISSING));

  EXPECT_CALL(mock_gatt_interface_, Close(conn_id)).Times(1);
  do_in_main_thread(
      FROM_HERE,
      base::BindOnce(&LeAudioClient::Connect,
                     base::Unretained(LeAudioClient::Get()), test_address0));

  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_btm_interface_);
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
}

/* same as above case except the disconnect is initiated by remote */
TEST_F(UnicastTest, ConnectRemoteDisconnectOneEarbud) {
  const RawAddress test_address0 = GetTestAddress(0);