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

Commit 7f638311 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "leaudio: Improve cleanup function" into main

parents 847a24cc e71691b8
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -1387,9 +1387,14 @@ void LeAudioDevices::Cleanup(tGATT_IF client_if) {
      continue;
    }

    if (connection_state == DeviceConnectState::CONNECTING_AUTOCONNECT) {
    // For connecting or connected device always remove background connect
    BTA_GATTC_CancelOpen(client_if, device->address_, false);
    } else {

    if (connection_state == DeviceConnectState::CONNECTING_BY_USER) {
      // When connecting by user, remove direct connect
      BTA_GATTC_CancelOpen(client_if, device->address_, true);
    } else if (connection_state != DeviceConnectState::CONNECTING_AUTOCONNECT) {
      // If connected, close the connection
      BtaGattQueue::Clean(device->conn_id_);
      BTA_GATTC_Close(device->conn_id_);
      device->DisconnectAcl();
+96 −0
Original line number Diff line number Diff line
@@ -2970,6 +2970,102 @@ TEST_F(UnicastTestNoInit, InitializeNoHal_2_1) {
          "disable LE Audio Profile, or update your HAL");
}
TEST_F(UnicastTest, CleanupWhenUserConnecting) {
  const RawAddress test_address0 = GetTestAddress(0);
  uint16_t conn_id = 1;
  SetSampleDatabaseEarbudsValid(1, test_address0, codec_spec_conf::kLeAudioLocationStereo,
                                codec_spec_conf::kLeAudioLocationStereo, default_channel_cnt,
                                default_channel_cnt, 0x0004,
                                /* source sample freq 16khz */ true, /*add_csis*/
                                true,                                /*add_cas*/
                                true,                                /*add_pacs*/
                                default_ase_cnt /*add_ascs*/);
  /* Remove default action on the direct connect */
  ON_CALL(mock_gatt_interface_, Open(_, _, BTM_BLE_DIRECT_CONNECTION, _)).WillByDefault(Return());
  ConnectLeAudio(test_address0, false, false);
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false)).Times(1);
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, true)).Times(1);
  EXPECT_CALL(mock_btm_interface_, AclDisconnectFromHandle(conn_id, _)).Times(0);
  EXPECT_CALL(mock_gatt_interface_, Close(_)).Times(0);
  LeAudioClient::Cleanup();
  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
}
TEST_F(UnicastTest, CleanupWhenAutoConnecting) {
  const RawAddress test_address0 = GetTestAddress(0);
  uint16_t conn_id = 1;
  SetSampleDatabaseEarbudsValid(1, test_address0, codec_spec_conf::kLeAudioLocationStereo,
                                codec_spec_conf::kLeAudioLocationStereo, default_channel_cnt,
                                default_channel_cnt, 0x0004,
                                /* source sample freq 16khz */ true, /*add_csis*/
                                true,                                /*add_cas*/
                                true,                                /*add_pacs*/
                                default_ase_cnt /*add_ascs*/);
  log::info("Connect device");
  ConnectLeAudio(test_address0);
  /* Remove default action on the autoconnect */
  ON_CALL(mock_gatt_interface_, Open(_, _, BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
          .WillByDefault(Return());
  EXPECT_CALL(mock_audio_hal_client_callbacks_,
              OnConnectionState(ConnectionState::DISCONNECTED, test_address0))
          .Times(1);
  /* Make sure when remote device disconnects us, TA is used */
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, _)).Times(1);
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address0, BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
          .Times(1);
  InjectDisconnectedEvent(1, GATT_CONN_TERMINATE_PEER_USER);
  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
  log::info("Device is in auto connect");
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false)).Times(1);
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, true)).Times(0);
  EXPECT_CALL(mock_btm_interface_, AclDisconnectFromHandle(conn_id, _)).Times(0);
  EXPECT_CALL(mock_gatt_interface_, Close(_)).Times(0);
  LeAudioClient::Cleanup();
  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
}
TEST_F(UnicastTest, CleanupWhenConnected) {
  const RawAddress test_address0 = GetTestAddress(0);
  uint16_t conn_id = 1;
  SetSampleDatabaseEarbudsValid(1, test_address0, codec_spec_conf::kLeAudioLocationStereo,
                                codec_spec_conf::kLeAudioLocationStereo, default_channel_cnt,
                                default_channel_cnt, 0x0004,
                                /* source sample freq 16khz */ true, /*add_csis*/
                                true,                                /*add_cas*/
                                true,                                /*add_pacs*/
                                default_ase_cnt /*add_ascs*/);
  log::info("Connect device");
  ConnectLeAudio(test_address0);
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false)).Times(1);
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, true)).Times(0);
  EXPECT_CALL(mock_btm_interface_, AclDisconnectFromHandle(conn_id, _)).Times(1);
  EXPECT_CALL(mock_gatt_interface_, Close(conn_id)).Times(1);
  LeAudioClient::Cleanup();
  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
}
TEST_F(UnicastTest, ConnectAndSetupPhy) {
  const RawAddress test_address0 = GetTestAddress(0);
  uint16_t conn_id = 1;