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

Commit 6d42e931 authored by Josh Wu's avatar Josh Wu
Browse files

[HFP] Fix feature comparison for ESCO_S4

Bug: 278781243
Test: atest bt_host_test_bta
Change-Id: I2d0345abccabe66e047b1079af1cb38f5ce56037
parent c229f923
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -333,6 +333,15 @@ cc_test {
        ":TestMockStackMetrics",
        ":TestMockStackPan",
        ":TestMockStackRfcomm",
        "ag/bta_ag_act.cc",
        "ag/bta_ag_api.cc",
        "ag/bta_ag_at.cc",
        "ag/bta_ag_cfg.cc",
        "ag/bta_ag_cmd.cc",
        "ag/bta_ag_main.cc",
        "ag/bta_ag_rfc.cc",
        "ag/bta_ag_sco.cc",
        "ag/bta_ag_sdp.cc",
        "ar/bta_ar.cc",
        "av/bta_av_aact.cc",
        "av/bta_av_act.cc",
@@ -369,6 +378,7 @@ cc_test {
        "sys/bta_sys_conn.cc",
        "sys/bta_sys_main.cc",
        "sys/utl.cc",
        "test/bta_ag_sco_test.cc",
        "test/bta_api_test.cc",
        "test/bta_av_test.cc",
        "test/bta_dm_cust_uuid_test.cc",
@@ -381,6 +391,7 @@ cc_test {
    shared_libs: [
        "libbase",
        "libcrypto",
        "libcutils",
        "liblog",
    ],
    static_libs: [
@@ -400,6 +411,13 @@ cc_test {
        cfi: true,
        misc_undefined: ["bounds"],
    },
    target: {
        android: {
            whole_static_libs: [
                "libPlatformProperties",
            ],
        },
    },
}

// csis unit tests for host
+3 −0
Original line number Diff line number Diff line
@@ -369,6 +369,9 @@ bool bta_ag_sco_is_active_device(const RawAddress& bd_addr);
bool bta_ag_sco_is_open(tBTA_AG_SCB* p_scb);
bool bta_ag_sco_is_opening(tBTA_AG_SCB* p_scb);
void bta_ag_sco_conn_rsp(tBTA_AG_SCB* p_scb, tBTM_ESCO_CONN_REQ_EVT_DATA* data);
// Testonly
void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig);
void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local);

/* AT command functions */
void bta_ag_at_hsp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd, uint8_t arg_type,
+5 −7
Original line number Diff line number Diff line
@@ -123,8 +123,6 @@ bool bta_ag_sco_is_active_device(const RawAddress& bd_addr) {
  return !active_device_addr.IsEmpty() && active_device_addr == bd_addr;
}

static void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local);

/*******************************************************************************
 *
 * Function         bta_ag_sco_conn_cback
@@ -385,7 +383,7 @@ static void bta_ag_cback_sco(tBTA_AG_SCB* p_scb, tBTA_AG_EVT event) {
 * Returns          void
 *
 ******************************************************************************/
static void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig) {
void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig) {
  LOG_DEBUG("BEFORE %s", p_scb->ToString().c_str());
  tBTA_AG_PEER_CODEC esco_codec = UUID_CODEC_CVSD;

@@ -451,7 +449,7 @@ static void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig) {
      params = esco_parameters_for_codec(ESCO_CODEC_MSBC_T1, offload);
    }
  } else {
    if (p_scb->features & BTA_AG_PEER_FEAT_ESCO_S4 &&
    if ((p_scb->features & BTA_AG_FEAT_ESCO_S4) &&
        (p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
      // HFP >=1.7 eSCO
      params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S4, offload);
@@ -508,7 +506,7 @@ static void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig) {
 * Returns          void
 *
 ******************************************************************************/
static void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local) {
void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local) {
  tBTA_AG_PEER_CODEC esco_codec = p_scb->inuse_codec;
  enh_esco_params_t params = {};
  bool offload = hfp_hal_interface::get_offload_enabled();
@@ -530,7 +528,7 @@ static void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local) {
        params = esco_parameters_for_codec(ESCO_CODEC_MSBC_T1, offload);
      }
    } else {
      if (p_scb->features & BTA_AG_PEER_FEAT_ESCO_S4 &&
      if ((p_scb->features & BTA_AG_FEAT_ESCO_S4) &&
          (p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
        // HFP >=1.7 eSCO
        params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S4, offload);
@@ -565,7 +563,7 @@ static void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local) {
    // and there is no plan to implement corresponding command handlers,
    // so we only accept CVSD connection from HF no matter what's
    // requested.
    if (p_scb->features & BTA_AG_PEER_FEAT_ESCO_S4 &&
    if ((p_scb->features & BTA_AG_FEAT_ESCO_S4) &&
        (p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
      // HFP >=1.7 eSCO
      params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S4, offload);
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include <tuple>
#include <vector>

#include "bta/ag/bta_ag_int.h"
#include "bta/include/bta_le_audio_api.h"
#include "stack/btm/btm_int_types.h"
#include "test/mock/mock_device_esco_parameters.h"

bool sco_peer_supports_esco_ev3(const RawAddress& remote_bda) { return true; }
tBTM_CB btm_cb;
LeAudioClient* LeAudioClient::Get() { return nullptr; }
namespace hfp_hal_interface {
void set_codec_datapath(int codec_uuid) {}
}  // namespace hfp_hal_interface

const RawAddress kRawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});

class BtaAgScoParameterSelectionTest
    : public ::testing::TestWithParam<
          std::tuple<tBTA_AG_FEAT, tBTA_AG_PEER_FEAT, bool>> {
 protected:
  void SetUp() override {
    test::mock::device_esco_parameters::esco_parameters_for_codec.body =
        [this](esco_codec_t codec) {
          this->codec = codec;
          return enh_esco_params_t{};
        };
  }
  void TearDown() override {
    test::mock::device_esco_parameters::esco_parameters_for_codec = {};
  }
  esco_codec_t codec;
};

TEST_P(BtaAgScoParameterSelectionTest, create_sco_cvsd) {
  bta_ag_api_set_active_device(kRawAddress);

  const auto [feature, peer_feature, is_local] = GetParam();
  tBTA_AG_SCB scb{
      .peer_addr = kRawAddress,
      .inuse_codec = UUID_CODEC_CVSD,
      .features = feature,
      .peer_features = peer_feature,
      .sco_idx = BTM_INVALID_SCO_INDEX,
  };

  this->codec = ESCO_CODEC_UNKNOWN;
  bta_ag_create_sco(&scb, is_local);
  if ((scb.features & BTA_AG_FEAT_ESCO_S4) &&
      (scb.peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
    ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S4);
  } else {
    ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S3);
  }
}

TEST_P(BtaAgScoParameterSelectionTest, create_pending_sco_cvsd) {
  bta_ag_api_set_active_device(kRawAddress);

  const auto [feature, peer_feature, is_local] = GetParam();
  tBTA_AG_SCB scb{
      .peer_addr = kRawAddress,
      .inuse_codec = UUID_CODEC_CVSD,
      .features = feature,
      .peer_features = peer_feature,
      .sco_idx = BTM_INVALID_SCO_INDEX,
  };

  this->codec = ESCO_CODEC_UNKNOWN;
  bta_ag_create_pending_sco(&scb, is_local);
  if ((scb.features & BTA_AG_FEAT_ESCO_S4) &&
      (scb.peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
    ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S4);
  } else {
    ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S3);
  }
}

std::vector<std::tuple<tBTA_AG_FEAT, tBTA_AG_PEER_FEAT, bool>>
BtaAgScoParameterSelectionTestParameters() {
  tBTA_AG_FEAT features[] = {0, BTA_AG_FEAT_ESCO_S4};
  tBTA_AG_PEER_FEAT peer_features[] = {0, BTA_AG_PEER_FEAT_ESCO_S4};
  bool is_local_or_orig[] = {false, true};
  std::vector<std::tuple<tBTA_AG_FEAT, tBTA_AG_PEER_FEAT, bool>> params;

  for (auto i : features) {
    for (auto j : peer_features) {
      for (auto k : is_local_or_orig) {
        params.push_back({i, j, k});
      }
    }
  }
  return params;
}

INSTANTIATE_TEST_SUITE_P(
    BtaAgScoParameterSelectionTests, BtaAgScoParameterSelectionTest,
    ::testing::ValuesIn(BtaAgScoParameterSelectionTestParameters()));
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ typedef enum {
  ESCO_CODEC_MSBC_T2,
  ESCO_CODEC_LC3_T1,
  ESCO_CODEC_LC3_T2,
  ESCO_CODEC_UNKNOWN,  // For testing
} esco_codec_t;

#define ESCO_NUM_CODECS 7