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

Commit 1b08684a authored by Myles Watson's avatar Myles Watson Committed by Automerger Merge Worker
Browse files

Merge changes I61150465,I122c2d47 into main am: b844dd0a am: 39603a71

parents 5bc87eda 39603a71
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -31,10 +31,18 @@ filegroup {
    ],
}

filegroup {
    name: "BluetoothHciFake",
    srcs: [
        "hci_layer_fake.cc",
    ],
}

filegroup {
    name: "BluetoothHciUnitTestSources",
    srcs: [
        ":BluetoothHalFake",
        ":BluetoothHciFake",
        "acl_builder_test.cc",
        "acl_manager/acl_scheduler_test.cc",
        "acl_manager/classic_acl_connection_test.cc",
@@ -49,7 +57,6 @@ filegroup {
        "class_of_device_unittest.cc",
        "controller_test.cc",
        "controller_unittest.cc",
        "hci_layer_fake.cc",
        "hci_layer_test.cc",
        "hci_layer_unittest.cc",
        "hci_packets_test.cc",
+2 −1
Original line number Diff line number Diff line
@@ -20,9 +20,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <algorithm>
#include <chrono>

#include "packet/raw_builder.h"

namespace bluetooth {
namespace hci {

+3 −3
Original line number Diff line number Diff line
@@ -15,13 +15,13 @@
 */

#include <future>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <vector>

#include "common/bind.h"
#include "hci/address.h"
#include "hci/hci_layer.h"
#include "packet/raw_builder.h"

namespace bluetooth {
namespace hci {
+1 −0
Original line number Diff line number Diff line
@@ -1564,6 +1564,7 @@ cc_test {
    srcs: [
        ":BluetoothHalSources_hci_host",
        ":BluetoothHalSources_ranging_host",
        ":BluetoothHciFake",
        ":BluetoothOsSources_host",
        ":OsiCompatSources",
        ":TestCommonMainHandler",
+136 −2
Original line number Diff line number Diff line
@@ -14,22 +14,62 @@
 * limitations under the License.
 */

#include <base/strings/stringprintf.h>
#include <bluetooth/log.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdlib.h>

#include "hci_error_code.h"
#include <future>

#include "common/contextual_callback.h"
#include "hci/address.h"
#include "hci/class_of_device.h"
#include "hci/hci_layer_fake.h"
#include "hci/hci_packets.h"
#include "stack/btm/btm_int_types.h"
#include "stack/include/btm_api.h"
#include "stack/include/hci_error_code.h"
#include "stack/include/inq_hci_link_interface.h"
#include "stack/include/main_thread.h"
#include "stack/test/btm/btm_test_fixtures.h"
#include "test/fake/fake_looper.h"
#include "test/mock/mock_main_shim_entry.h"
#include "test/mock/mock_osi_allocator.h"
#include "test/mock/mock_osi_thread.h"
#include "types/raw_address.h"

extern tBTM_CB btm_cb;

using bluetooth::common::ContextualCallback;
using bluetooth::common::ContextualOnceCallback;
using bluetooth::hci::Address;
using bluetooth::hci::CommandCompleteView;
using bluetooth::hci::CommandStatusView;
using bluetooth::hci::EventView;
using bluetooth::hci::ExtendedInquiryResultBuilder;
using bluetooth::hci::ExtendedInquiryResultView;
using bluetooth::hci::InquiryResponse;
using bluetooth::hci::InquiryResultBuilder;
using bluetooth::hci::InquiryResultView;
using bluetooth::hci::InquiryResultWithRssiBuilder;
using bluetooth::hci::InquiryResultWithRssiView;
using bluetooth::hci::InquiryStatusBuilder;
using bluetooth::hci::InquiryView;
using bluetooth::hci::OpCode;
using bluetooth::hci::EventCode::EXTENDED_INQUIRY_RESULT;
using bluetooth::hci::EventCode::INQUIRY_COMPLETE;
using bluetooth::hci::EventCode::INQUIRY_RESULT;
using bluetooth::hci::EventCode::INQUIRY_RESULT_WITH_RSSI;
using testing::_;
using testing::A;
using testing::Matcher;
using testing::NiceMock;
using testing::Return;
using testing::SaveArg;

namespace {
const Address kAddress = Address({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
const Address kAddress2 = Address({0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc});
const RawAddress kRawAddress = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
const RawAddress kRawAddress2 =
    RawAddress({0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc});
@@ -39,6 +79,8 @@ const BD_NAME kEmptyName = "";
tBTM_REMOTE_DEV_NAME gBTM_REMOTE_DEV_NAME{};
bool gBTM_REMOTE_DEV_NAME_sent{false};

static constexpr uint8_t kNumCommandPackets = 1;

}  // namespace

class BtmInqTest : public BtmWithMocksTest {
@@ -142,3 +184,95 @@ TEST_F(BtmInqActiveTest, btm_process_remote_name__different_address) {

  ASSERT_FALSE(gBTM_REMOTE_DEV_NAME_sent);
}

class BtmInquiryCallbacks {
 public:
  virtual ~BtmInquiryCallbacks() = default;
  virtual void btm_inq_results_cb(tBTM_INQ_RESULTS*, const uint8_t*,
                                  uint16_t) = 0;
  virtual void btm_inq_cmpl_cb(void*) = 0;
};

class MockBtmInquiryCallbacks : public BtmInquiryCallbacks {
 public:
  MOCK_METHOD(void, btm_inq_results_cb,
              (tBTM_INQ_RESULTS * p_inq_results, const uint8_t* p_eir,
               uint16_t eir_len),
              (override));
  MOCK_METHOD(void, btm_inq_cmpl_cb, (void*), (override));
};

MockBtmInquiryCallbacks* inquiry_callback_ptr = nullptr;

void btm_inq_results_cb(tBTM_INQ_RESULTS* p_inq_results, const uint8_t* p_eir,
                        uint16_t eir_len) {
  inquiry_callback_ptr->btm_inq_results_cb(p_inq_results, p_eir, eir_len);
}

void btm_inq_cmpl_cb(void* p1) { inquiry_callback_ptr->btm_inq_cmpl_cb(p1); }

class BtmDeviceInquiryTest : public BtmInqTest {
 protected:
  void SetUp() override {
    BtmInqTest::SetUp();
    main_thread_start_up();
    inquiry_callback_ptr = &callbacks_;
    bluetooth::hci::testing::mock_controller_ = &controller_;
    ON_CALL(controller_, SupportsBle()).WillByDefault(Return(true));
    bluetooth::hci::testing::mock_hci_layer_ = &hci_layer_;

    // Start Inquiry
    EXPECT_EQ(BTM_CMD_STARTED,
              BTM_StartInquiry(btm_inq_results_cb, btm_inq_cmpl_cb));
    auto view = hci_layer_.GetCommand(OpCode::INQUIRY);
    hci_layer_.IncomingEvent(InquiryStatusBuilder::Create(
        bluetooth::hci::ErrorCode::SUCCESS, kNumCommandPackets));

    // Send one response to synchronize
    std::promise<void> first_result_promise;
    auto first_result = first_result_promise.get_future();
    EXPECT_CALL(*inquiry_callback_ptr, btm_inq_results_cb(_, _, _))
        .WillOnce(
            [&first_result_promise]() { first_result_promise.set_value(); })
        .RetiresOnSaturation();

    InquiryResponse one_device(kAddress,
                               bluetooth::hci::PageScanRepetitionMode::R0,
                               bluetooth::hci::ClassOfDevice(), 0x1234);
    hci_layer_.IncomingEvent(InquiryResultBuilder::Create({one_device}));

    EXPECT_EQ(std::future_status::ready,
              first_result.wait_for(std::chrono::seconds(1)));
  }

  void TearDown() override {
    BTM_CancelInquiry();
    inquiry_callback_ptr = nullptr;
    main_thread_shut_down();
    BtmInqTest::TearDown();
  }

  NiceMock<bluetooth::hci::testing::MockControllerInterface> controller_;
  bluetooth::hci::HciLayerFake hci_layer_;
  ContextualCallback<void(EventView)> on_exteneded_inq_result_;
  ContextualCallback<void(EventView)> on_inq_complete_;
  ContextualCallback<void(EventView)> on_inq_result_;
  ContextualCallback<void(EventView)> on_inq_result_with_rssi_;
  MockBtmInquiryCallbacks callbacks_;
};

TEST_F(BtmDeviceInquiryTest, bta_dm_disc_device_discovery_single_result) {
  std::promise<void> one_result_promise;
  auto one_result = one_result_promise.get_future();
  EXPECT_CALL(*inquiry_callback_ptr, btm_inq_results_cb(_, _, _))
      .WillOnce([&one_result_promise]() { one_result_promise.set_value(); })
      .RetiresOnSaturation();

  InquiryResponse one_device(kAddress2,
                             bluetooth::hci::PageScanRepetitionMode::R0,
                             bluetooth::hci::ClassOfDevice(), 0x2345);
  hci_layer_.IncomingEvent(InquiryResultBuilder::Create({one_device}));

  EXPECT_EQ(std::future_status::ready,
            one_result.wait_for(std::chrono::seconds(1)));
}
Loading