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

Commit 2699f32d authored by Jakub Pawłowski's avatar Jakub Pawłowski Committed by Automerger Merge Worker
Browse files

Merge "leaudio: Improve targeted annoucements reconnection policy" am: 4c924332

parents c0089fd8 4c924332
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -1664,11 +1664,15 @@ class LeAudioClientImpl : public LeAudioClient {

    if (!leAudioDevice) return;

    LOG_INFO("%s, status 0x%02x", ADDRESS_TO_LOGGABLE_CSTR(address), status);

    if (status != GATT_SUCCESS) {
      /* autoconnect connection failed, that's ok */
      if (leAudioDevice->GetConnectionState() ==
          DeviceConnectState::CONNECTING_AUTOCONNECT) {
        leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);
              DeviceConnectState::CONNECTING_AUTOCONNECT ||
          leAudioDevice->autoconnect_flag_) {
        LOG_INFO("Device not available now, do background connect.");
        BTA_GATTC_Open(gatt_if_, address, reconnection_mode_, false);
        return;
      }

+4 −1
Original line number Diff line number Diff line
@@ -2896,13 +2896,16 @@ void LeAudioDevices::SetInitialGroupAutoconnectState(
    return;
  }

  /* This function is called when bluetooth started, therefore here we will
   * try direct connection, if that failes, we fallback to background connection
   */
  for (auto dev : leAudioDevices_) {
    if ((dev->group_id_ == group_id) &&
        (dev->GetConnectionState() == DeviceConnectState::DISCONNECTED)) {
      dev->SetConnectionState(DeviceConnectState::CONNECTING_AUTOCONNECT);
      dev->autoconnect_flag_ = true;
      btif_storage_set_leaudio_autoconnect(dev->address_, true);
      BTA_GATTC_Open(gatt_if, dev->address_, reconnection_mode, false);
      BTA_GATTC_Open(gatt_if, dev->address_, BTM_BLE_DIRECT_CONNECTION, false);
    }
  }
}
+61 −23
Original line number Diff line number Diff line
@@ -389,8 +389,11 @@ class UnicastTestNoInit : public Test {
        .mtu = 240,
    };

    if (status == GATT_SUCCESS) {
      ASSERT_NE(peer_devices.count(conn_id), 0u);
      peer_devices.at(conn_id)->connected = true;
    }

    do_in_main_thread(
        FROM_HERE,
        base::BindOnce(
@@ -2613,27 +2616,28 @@ TEST_F(UnicastTestNoInit, LoadStoredEarbudsCsisGrouped) {
                   std::move(ases)));
  });

  // Expect stored device0 to connect automatically
  EXPECT_CALL(mock_audio_hal_client_callbacks_,
              OnConnectionState(ConnectionState::CONNECTED, test_address0))
  // Expect stored device0 to connect automatically (first directed connection )
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address0, BTM_BLE_DIRECT_CONNECTION, _))
      .Times(1);

  // Expect stored device1 to connect automatically (first direct connection)
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address1, BTM_BLE_DIRECT_CONNECTION, _))
      .Times(1);

  ON_CALL(mock_btm_interface_, BTM_IsEncrypted(test_address1, _))
      .WillByDefault(DoAll(Return(true)));
  ON_CALL(mock_btm_interface_, BTM_IsEncrypted(test_address0, _))
      .WillByDefault(DoAll(Return(true)));
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address0,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))

  EXPECT_CALL(mock_audio_hal_client_callbacks_,
              OnConnectionState(ConnectionState::CONNECTED, test_address0))
      .Times(1);

  // Expect stored device1 to connect automatically
  EXPECT_CALL(mock_audio_hal_client_callbacks_,
              OnConnectionState(ConnectionState::CONNECTED, test_address1))
      .Times(1);
  ON_CALL(mock_btm_interface_, BTM_IsEncrypted(test_address1, _))
      .WillByDefault(DoAll(Return(true)));
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address1,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
      .Times(1);

  ON_CALL(mock_groups_module_, GetGroupId(_, _))
      .WillByDefault(DoAll(Return(group_id)));
@@ -2660,14 +2664,33 @@ TEST_F(UnicastTestNoInit, LoadStoredEarbudsCsisGrouped) {
      framework_encode_preference);
  if (app_register_callback) app_register_callback.Run(gatt_if, GATT_SUCCESS);

  /* For background connect, test needs to Inject Connected Event */
  InjectConnectedEvent(test_address0, 1);
  InjectConnectedEvent(test_address1, 2);

  // We need to wait for the storage callback before verifying stuff
  SyncOnMainLoop();
  ASSERT_TRUE(LeAudioClient::IsLeAudioClientRunning());

  // Simulate devices are not there and phone fallbacks to targeted
  // announcements
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address0,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
      .Times(1);

  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address1,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
      .Times(1);

  // Devices not found
  InjectConnectedEvent(test_address0, 0, GATT_ERROR);
  InjectConnectedEvent(test_address1, 0, GATT_ERROR);

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

  /* For background connect, test needs to Inject Connected Event */
  InjectConnectedEvent(test_address0, 1);
  InjectConnectedEvent(test_address1, 2);

  // Verify if all went well and we got the proper group
  std::vector<RawAddress> devs =
      LeAudioClient::Get()->GetGroupDevices(group_id);
@@ -2753,8 +2776,7 @@ TEST_F(UnicastTestNoInit, LoadStoredEarbudsCsisGroupedDifferently) {
  ON_CALL(mock_btm_interface_, BTM_IsEncrypted(test_address0, _))
      .WillByDefault(DoAll(Return(true)));
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address0,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
              Open(gatt_if, test_address0, BTM_BLE_DIRECT_CONNECTION, _))
      .Times(1);

  // Expect stored device1 to NOT connect automatically
@@ -2764,8 +2786,7 @@ TEST_F(UnicastTestNoInit, LoadStoredEarbudsCsisGroupedDifferently) {
  ON_CALL(mock_btm_interface_, BTM_IsEncrypted(test_address1, _))
      .WillByDefault(DoAll(Return(true)));
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address1,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
              Open(gatt_if, test_address1, BTM_BLE_DIRECT_CONNECTION, _))
      .Times(0);

  // Initialize
@@ -2784,6 +2805,23 @@ TEST_F(UnicastTestNoInit, LoadStoredEarbudsCsisGroupedDifferently) {
      framework_encode_preference);
  if (app_register_callback) app_register_callback.Run(gatt_if, GATT_SUCCESS);

  // We need to wait for the storage callback before verifying stuff
  SyncOnMainLoop();
  ASSERT_TRUE(LeAudioClient::IsLeAudioClientRunning());
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);

  // Simulate device is not there and phone fallbacks to targeted announcements
  EXPECT_CALL(mock_gatt_interface_,
              Open(gatt_if, test_address0,
                   BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS, _))
      .Times(1);

  // Devices not found
  InjectConnectedEvent(test_address0, 0, GATT_ERROR);

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

  /* For background connect, test needs to Inject Connected Event */
  InjectConnectedEvent(test_address0, 1);