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

Commit 13250468 authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

Merge changes Ib97a515e,I69c866b0,I9110bf52 am: d3413067

parents 7faf4d03 d3413067
Loading
Loading
Loading
Loading
+25 −5
Original line number Diff line number Diff line
@@ -1955,7 +1955,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);
@@ -1970,7 +1970,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 */
@@ -1991,6 +1991,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);
    }
  }
@@ -2018,12 +2019,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(),
          ADDRESS_TO_LOGGABLE_CSTR(p_remote_name->bd_addr));
    } else {
      LOG_INFO(
          "Ignored remote name response for the wrong address exp:%s act:%s",
          ADDRESS_TO_LOGGABLE_CSTR(bta_dm_search_cb.peer_bdaddr),
          ADDRESS_TO_LOGGABLE_CSTR(p_remote_name->bd_addr));
      return;
    }
  }

  /* remote name discovery is done but it could be failed */
  bta_dm_search_cb.name_discover_done = true;
@@ -4363,6 +4381,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"

@@ -44,12 +47,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 {
@@ -68,7 +80,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++) {
@@ -78,9 +105,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 = {};
  }
};

@@ -210,6 +245,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
@@ -362,3 +400,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);
}
+7 −6
Original line number Diff line number Diff line
@@ -805,7 +805,7 @@ tBTM_STATUS BTM_ClearInqDb(const RawAddress* p_bda) {
 *
 ******************************************************************************/
void btm_inq_db_reset(void) {
  tBTM_REMOTE_DEV_NAME rem_name;
  tBTM_REMOTE_DEV_NAME rem_name = {};
  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  uint8_t num_responses;
  uint8_t temp_inq_active;
@@ -835,6 +835,7 @@ void btm_inq_db_reset(void) {

    if (p_inq->p_remname_cmpl_cb) {
      rem_name.status = BTM_DEV_RESET;
      rem_name.hci_status = HCI_SUCCESS;

      (*p_inq->p_remname_cmpl_cb)(&rem_name);
      p_inq->p_remname_cmpl_cb = NULL;
@@ -1491,6 +1492,7 @@ void btm_process_remote_name(const RawAddress* bda, const BD_NAME bdn,
      rem_name.length = (evt_len < BD_NAME_LEN) ? evt_len : BD_NAME_LEN;
      rem_name.remote_bd_name[rem_name.length] = 0;
      rem_name.status = BTM_SUCCESS;
      rem_name.hci_status = hci_status;
      temp_evt_len = rem_name.length;

      while (temp_evt_len > 0) {
@@ -1498,12 +1500,11 @@ void btm_process_remote_name(const RawAddress* bda, const BD_NAME bdn,
        temp_evt_len--;
      }
      rem_name.remote_bd_name[rem_name.length] = 0;
    }

    } else {
      /* If processing a stand alone remote name then report the error in the
         callback */
    else {
      rem_name.status = BTM_BAD_VALUE_RET;
      rem_name.hci_status = hci_status;
      rem_name.length = 0;
      rem_name.remote_bd_name[0] = 0;
    }
+2 −1
Original line number Diff line number Diff line
@@ -242,10 +242,11 @@ typedef struct {

/* Structure returned with remote name  request */
typedef struct {
  uint16_t status;
  tBTM_STATUS status;
  RawAddress bd_addr;
  uint16_t length;
  BD_NAME remote_bd_name;
  tHCI_STATUS hci_status;
} tBTM_REMOTE_DEV_NAME;

typedef union /* contains the inquiry filter condition */