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

Commit 3fbf4126 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

leaudio: Fix reconnect after disconnected by Java

Disconnect from Java can happen in two cases
1.Profile connection timeout - this can happen on single device while
  other is connected
2. Intentional user disconnection - this will end up with all members
   being disconnected.

With this patch, if 1) happens, we want to be sure that device will get
back on allow list if other set members are connected

Bug: 281937978
Test: atest bluetooth_le_audio_client_test
Test: atest BluetoothInstrumentationtTests
Tag: #feature
Change-Id: I1e446e611400659fc58a5a14ec13ba1e53f7eb65
parent fe1322e6
Loading
Loading
Loading
Loading
+4 −27
Original line number Diff line number Diff line
@@ -1433,30 +1433,12 @@ class LeAudioClientImpl : public LeAudioClient {
  }

  void BackgroundConnectIfNeeded(LeAudioDevice* leAudioDevice) {
    auto group = GetGroupIfEnabled(leAudioDevice->group_id_);
    if (group == nullptr) {
      LOG_INFO(" Device %s is not yet part of the group %d. ",
               ADDRESS_TO_LOGGABLE_CSTR(leAudioDevice->address_),
               leAudioDevice->group_id_);
      return;
    }

    if (!leAudioDevice->autoconnect_flag_ && !group->IsAnyDeviceConnected()) {
    if (!leAudioDevice->autoconnect_flag_) {
      LOG_DEBUG("Device %s not in the background connect",
                ADDRESS_TO_LOGGABLE_CSTR(leAudioDevice->address_));
      return;
    }

    LOG_INFO(
        "Add %s added to background connect. autoconnect flag: %d "
        "group_connected: %d",
        ADDRESS_TO_LOGGABLE_CSTR(leAudioDevice->address_),
        leAudioDevice->group_id_, group->IsAnyDeviceConnected());

    leAudioDevice->SetConnectionState(
        DeviceConnectState::CONNECTING_AUTOCONNECT);
    BTA_GATTC_Open(gatt_if_, leAudioDevice->address_, reconnection_mode_,
                   false);
    AddToBackgroundConnectCheckGroupConnected(leAudioDevice);
  }

  void Disconnect(const RawAddress& address) override {
@@ -2259,6 +2241,8 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
    }

    leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);

    /* Attempt background re-connect if disconnect was not initiated locally
     * or if autoconnect is set and device got disconnected because of some
     * issues
@@ -2266,7 +2250,6 @@ class LeAudioClientImpl : public LeAudioClient {
    if (group == nullptr || !group->IsEnabled()) {
      LOG_ERROR("Group id %d (%p) disabled or null", leAudioDevice->group_id_,
                group);
      leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);
      return;
    }

@@ -2274,9 +2257,6 @@ class LeAudioClientImpl : public LeAudioClient {
      if (leAudioDevice->autoconnect_flag_) {
        /* In this case ACL might not yet been disconnected */
        scheduleAutoConnect(address);
      } else {
        /* Just acknowledge disconnected state*/
        leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);
      }
      return;
    }
@@ -2305,9 +2285,6 @@ class LeAudioClientImpl : public LeAudioClient {
         */
        scheduleGroupConnectedCheck(leAudioDevice->group_id_);
      }
    } else {
      /* Just acknowledge disconnected state*/
      leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);
    }
  }

+2 −1
Original line number Diff line number Diff line
@@ -3114,7 +3114,8 @@ void LeAudioDevices::Dump(int fd, int group_id) {
void LeAudioDevices::Cleanup(tGATT_IF client_if) {
  for (auto const& device : leAudioDevices_) {
    auto connection_state = device->GetConnectionState();
    if (connection_state == DeviceConnectState::DISCONNECTED) {
    if (connection_state == DeviceConnectState::DISCONNECTED ||
        connection_state == DeviceConnectState::DISCONNECTING) {
      continue;
    }

+10 −9
Original line number Diff line number Diff line
@@ -4204,21 +4204,22 @@ TEST_F(UnicastTest, TwoEarbudsStreamingProfileDisconnect) {

  EXPECT_CALL(mock_gatt_interface_,
              Open(_, _, BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
      .Times(1);
      .Times(2);
  EXPECT_CALL(mock_state_machine_, StopStream(_)).Times(1);

  DisconnectLeAudio(test_address0, 1);
  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
  Mock::VerifyAndClearExpectations(&mock_state_machine_);

  EXPECT_CALL(mock_gatt_interface_,
              Open(_, _, BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
      .Times(1);
  /* Do not inject OPEN_EVENT by default */
  ON_CALL(mock_gatt_interface_, Open(_, _, _, _))
      .WillByDefault(DoAll(Return()));
  ON_CALL(mock_gatt_interface_, Close(_)).WillByDefault(DoAll(Return()));

  DisconnectLeAudio(test_address0, 1);
  DisconnectLeAudio(test_address1, 2);

  InjectDisconnectedEvent(1);
  InjectDisconnectedEvent(2);

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