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

Commit 4ba175bf authored by Chris Manton's avatar Chris Manton Committed by Android (Google) Code Review
Browse files

Merge changes Ib97a515e,I69c866b0,I9110bf52,I5ada8642,I0042d9c9 into tm-qpr-dev

* changes:
  rnr: Add bta_dm_remname_cback tests
  rnr: Condition upon hci status
  rnr: Add hci status to remote name read callback data
  sdp: If known skip read remote name request
  Introduce BTM_IsRemoteNameKnown
parents 610b89e0 b8d12240
Loading
Loading
Loading
Loading
+33 −5
Original line number Diff line number Diff line
@@ -1715,6 +1715,14 @@ static void bta_dm_discover_device(const RawAddress& remote_bd_addr) {
    /* Do not perform RNR for LE devices at inquiry complete*/
    bta_dm_search_cb.name_discover_done = true;
  }
  // If we already have the name we can skip getting the name
  if (BTM_IsRemoteNameKnown(remote_bd_addr, transport) &&
      bluetooth::common::init_flags::sdp_skip_rnr_if_known_is_enabled()) {
    LOG_DEBUG("Security record already known skipping read remote name peer:%s",
              PRIVATE_ADDRESS(remote_bd_addr));
    bta_dm_search_cb.name_discover_done = true;
  }

  /* if name discovery is not done and application needs remote name */
  if ((!bta_dm_search_cb.name_discover_done) &&
      ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
@@ -1898,7 +1906,7 @@ static void bta_dm_inq_cmpl_cb(void* p_result) {
static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
                                                UNUSED_ATTR DEV_CLASS dc,
                                                tBTM_BD_NAME bd_name) {
  tBTM_REMOTE_DEV_NAME rem_name;
  tBTM_REMOTE_DEV_NAME rem_name = {};
  tBTM_STATUS btm_status;

  APPL_TRACE_DEBUG("%s name=<%s>", __func__, bd_name);
@@ -1912,7 +1920,7 @@ static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
      rem_name.length = BD_NAME_LEN;
    }
    rem_name.status = BTM_SUCCESS;

    rem_name.hci_status = HCI_SUCCESS;
    bta_dm_remname_cback(&rem_name);
  } else {
    /* get name of device */
@@ -1933,6 +1941,7 @@ static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
      rem_name.length = 0;
      rem_name.remote_bd_name[0] = 0;
      rem_name.status = btm_status;
      rem_name.hci_status = HCI_SUCCESS;
      bta_dm_remname_cback(&rem_name);
    }
  }
@@ -1960,12 +1969,29 @@ static void bta_dm_remname_cback(void* p) {
      BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
    }
  } else {
    // if we got a different response, ignore it
    // if we got a different response, maybe ignore it
    // we will have made a request directly from BTM_ReadRemoteDeviceName so we
    // expect a dedicated response for us
    LOG_INFO("ignoring remote name response in DM callback since it's for the wrong bd_addr");
    if (p_remote_name->hci_status == HCI_ERR_CONNECTION_EXISTS) {
      if (bluetooth::shim::is_gd_security_enabled()) {
        bluetooth::shim::BTM_SecDeleteRmtNameNotifyCallback(
            &bta_dm_service_search_remname_cback);
      } else {
        BTM_SecDeleteRmtNameNotifyCallback(
            &bta_dm_service_search_remname_cback);
      }
      LOG_INFO(
          "Assume command failed due to disconnection hci_status:%s peer:%s",
          hci_error_code_text(p_remote_name->hci_status).c_str(),
          PRIVATE_ADDRESS(p_remote_name->bd_addr));
    } else {
      LOG_INFO(
          "Ignored remote name response for the wrong address exp:%s act:%s",
          PRIVATE_ADDRESS(bta_dm_search_cb.peer_bdaddr),
          PRIVATE_ADDRESS(p_remote_name->bd_addr));
      return;
    }
  }

  /* remote name discovery is done but it could be failed */
  bta_dm_search_cb.name_discover_done = true;
@@ -4164,6 +4190,8 @@ tBTA_DM_PEER_DEVICE* allocate_device_for(const RawAddress& bd_addr,
  return ::allocate_device_for(bd_addr, transport);
}

void bta_dm_remname_cback(void* p) { ::bta_dm_remname_cback(p); }

}  // namespace testing
}  // namespace legacy
}  // namespace bluetooth
+110 −0
Original line number Diff line number Diff line
@@ -27,8 +27,11 @@
#include "bta/include/bta_hf_client_api.h"
#include "btif/include/stack_manager.h"
#include "common/message_loop_thread.h"
#include "osi/include/compat.h"
#include "stack/include/btm_status.h"
#include "test/common/main_handler.h"
#include "test/mock/mock_osi_alarm.h"
#include "test/mock/mock_osi_allocator.h"
#include "test/mock/mock_stack_acl.h"
#include "test/mock/mock_stack_btm_sec.h"

@@ -46,12 +49,21 @@ class MessageLoop;

namespace {
constexpr uint8_t kUnusedTimer = BTA_ID_MAX;
const RawAddress kRawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
const RawAddress kRawAddress2({0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc});
constexpr char kRemoteName[] = "TheRemoteName";

const char* test_flags[] = {
    "INIT_logging_debug_enabled_for_all=true",
    nullptr,
};

bool bta_dm_search_sm_execute(BT_HDR_RIGID* p_msg) { return true; }
void bta_dm_search_sm_disable() { bta_sys_deregister(BTA_ID_DM_SEARCH); }

const tBTA_SYS_REG bta_dm_search_reg = {bta_dm_search_sm_execute,
                                        bta_dm_search_sm_disable};

}  // namespace

struct alarm_t {
@@ -70,7 +82,22 @@ class BtaDmTest : public testing::Test {
    test::mock::osi_alarm::alarm_free.body = [](alarm_t* alarm) {
      delete alarm;
    };
    test::mock::osi_allocator::osi_malloc.body = [](size_t size) {
      return malloc(size);
    };
    test::mock::osi_allocator::osi_calloc.body = [](size_t size) {
      return calloc(1UL, size);
    };
    test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
    test::mock::osi_allocator::osi_free_and_reset.body = [](void** ptr) {
      free(*ptr);
      *ptr = nullptr;
    };

    main_thread_start_up();
    post_on_bt_main([]() { LOG_INFO("Main thread started up"); });

    bta_sys_register(BTA_ID_DM_SEARCH, &bta_dm_search_reg);
    bta_dm_init_cb();

    for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
@@ -80,9 +107,17 @@ class BtaDmTest : public testing::Test {
    }
  }
  void TearDown() override {
    bta_sys_deregister(BTA_ID_DM_SEARCH);
    bta_dm_deinit_cb();
    post_on_bt_main([]() { LOG_INFO("Main thread shutting down"); });
    main_thread_shut_down();

    test::mock::osi_alarm::alarm_new = {};
    test::mock::osi_alarm::alarm_free = {};
    test::mock::osi_allocator::osi_malloc = {};
    test::mock::osi_allocator::osi_calloc = {};
    test::mock::osi_allocator::osi_free = {};
    test::mock::osi_allocator::osi_free_and_reset = {};
  }
};

@@ -212,6 +247,9 @@ namespace legacy {
namespace testing {
tBTA_DM_PEER_DEVICE* allocate_device_for(const RawAddress& bd_addr,
                                         tBT_TRANSPORT transport);

void bta_dm_remname_cback(void* p);

}  // namespace testing
}  // namespace legacy
}  // namespace bluetooth
@@ -364,3 +402,75 @@ TEST_F(BtaDmTest, bta_dm_state_text) {
                   static_cast<tBTA_DM_STATE>(std::numeric_limits<int>::max()))
                   .c_str());
}

TEST_F(BtaDmTest, bta_dm_remname_cback__typical) {
  bta_dm_search_cb = {
      .name_discover_done = false,
      .peer_bdaddr = kRawAddress,
  };

  tBTM_REMOTE_DEV_NAME name = {
      .status = BTM_SUCCESS,
      .bd_addr = kRawAddress,
      .length = static_cast<uint16_t>(strlen(kRemoteName)),
      .remote_bd_name = {},
      .hci_status = HCI_SUCCESS,
  };
  strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
          strlen(kRemoteName));

  bluetooth::legacy::testing::bta_dm_remname_cback(static_cast<void*>(&name));

  sync_main_handler();

  ASSERT_EQ(1, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
  ASSERT_TRUE(bta_dm_search_cb.name_discover_done);
}

TEST_F(BtaDmTest, bta_dm_remname_cback__wrong_address) {
  bta_dm_search_cb = {
      .name_discover_done = false,
      .peer_bdaddr = kRawAddress,
  };

  tBTM_REMOTE_DEV_NAME name = {
      .status = BTM_SUCCESS,
      .bd_addr = kRawAddress2,
      .length = static_cast<uint16_t>(strlen(kRemoteName)),
      .remote_bd_name = {},
      .hci_status = HCI_SUCCESS,
  };
  strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
          strlen(kRemoteName));

  bluetooth::legacy::testing::bta_dm_remname_cback(static_cast<void*>(&name));

  sync_main_handler();

  ASSERT_EQ(0, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
  ASSERT_FALSE(bta_dm_search_cb.name_discover_done);
}

TEST_F(BtaDmTest, bta_dm_remname_cback__HCI_ERR_CONNECTION_EXISTS) {
  bta_dm_search_cb = {
      .name_discover_done = false,
      .peer_bdaddr = kRawAddress,
  };

  tBTM_REMOTE_DEV_NAME name = {
      .status = BTM_SUCCESS,
      .bd_addr = RawAddress::kEmpty,
      .length = static_cast<uint16_t>(strlen(kRemoteName)),
      .remote_bd_name = {},
      .hci_status = HCI_ERR_CONNECTION_EXISTS,
  };
  strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
          strlen(kRemoteName));

  bluetooth::legacy::testing::bta_dm_remname_cback(static_cast<void*>(&name));

  sync_main_handler();

  ASSERT_EQ(1, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
  ASSERT_TRUE(bta_dm_search_cb.name_discover_done);
}
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ table InitFlagsData {
    gd_core_enabled:bool (privacy:"Any");
    btaa_hci_log_enabled:bool (privacy:"Any");
    btm_dm_flush_discovery_queue_on_search_cancel:bool (privacy:"Any");
    sdp_skip_rnr_if_known_is_enabled:bool (privacy:"Any");
}

root_type InitFlagsData;
+2 −0
Original line number Diff line number Diff line
@@ -31,5 +31,7 @@ flatbuffers::Offset<bluetooth::common::InitFlagsData> bluetooth::dumpsys::InitFl
  builder.add_gd_controller_enabled(true);
  builder.add_gd_core_enabled(bluetooth::common::init_flags::gd_core_is_enabled());
  builder.add_btaa_hci_log_enabled(bluetooth::common::init_flags::btaa_hci_is_enabled());
  builder.add_sdp_skip_rnr_if_known_is_enabled(bluetooth::common::init_flags::sdp_skip_rnr_if_known_is_enabled());

  return builder.Finish();
}
+1 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ init_flags!(
        gd_rust,
        gd_link_policy,
        irk_rotation,
        sdp_skip_rnr_if_known,
        pass_phy_update_callback
    },
    dependencies: {
Loading