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

Commit 9ad9854d authored by Chris Manton's avatar Chris Manton
Browse files

Present name request during service discovery

Bug: 316383772
Bug: 321809163
Test: atest net_test_btif
Flag: rnr_present_during_service_discovery

Change-Id: Ida3da6087df142e944aec51a5769e93dfa74d535
parent 5825472d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -322,6 +322,7 @@ cc_test {
        "libchrome",
        "libcom.android.sysprop.bluetooth.wrapped",
        "libevent",
        "libflagtest",
        "libflatbuffers-cpp",
        "libg722codec",
        "libgmock",
@@ -347,6 +348,7 @@ cc_test {
                "android.hardware.bluetooth@1.1",
                "android.hardware.common-V2-ndk",
                "android.hardware.common.fmq-V1-ndk",
                "android.system.suspend-V1-ndk",
                "android.system.suspend.control-V1-ndk",
                "libaaudio",
            ],
+37 −1
Original line number Diff line number Diff line
@@ -2070,7 +2070,38 @@ static void btif_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,
    } break;

    case BTA_DM_NAME_READ_EVT: {
      if (IS_FLAG_ENABLED(rnr_present_during_service_discovery)) {
        const tBTA_DM_DISC_RES& disc_res = p_data->disc_res;
        if (disc_res.hci_status != HCI_SUCCESS) {
          LOG_WARN("Received RNR event with bad status addr:%s hci_status:%s",
                   ADDRESS_TO_LOGGABLE_CSTR(disc_res.bd_addr),
                   hci_error_code_text(disc_res.hci_status).c_str());
          break;
        }
        if (disc_res.bd_name[0] == '\0') {
          LOG_WARN("Received RNR event without valid name addr:%s",
                   ADDRESS_TO_LOGGABLE_CSTR(disc_res.bd_addr));
          break;
        }
        bt_property_t properties[] = {{
            .type = BT_PROPERTY_BDNAME,
            .len = (int)strlen((char*)disc_res.bd_name),
            .val = (void*)disc_res.bd_name,
        }};
        const bt_status_t status = btif_storage_set_remote_device_property(
            &disc_res.bd_addr, properties);
        ASSERT_LOG(status == BT_STATUS_SUCCESS,
                   "Failed to save remote device property status:%s",
                   bt_status_text(status).c_str());
        const size_t num_props = sizeof(properties) / sizeof(bt_property_t);
        GetInterfaceToProfiles()->events->invoke_remote_device_properties_cb(
            status, disc_res.bd_addr, (int)num_props, properties);
        LOG_INFO("Callback for read name event addr:%s name:%s",
                 ADDRESS_TO_LOGGABLE_CSTR(disc_res.bd_addr),
                 PRIVATE_NAME(disc_res.bd_name));
      } else {
        LOG_INFO("Skipping name read event - called on bad callback.");
      }
    } break;

    default: {
@@ -4205,6 +4236,11 @@ void bta_energy_info_cb(tBTM_BLE_TX_TIME_MS tx_time,
                       status);
}

void btif_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,
                                 tBTA_DM_SEARCH* p_data) {
  ::btif_dm_search_services_evt(event, p_data);
}

}  // namespace testing
}  // namespace legacy
}  // namespace bluetooth
+93 −1
Original line number Diff line number Diff line
@@ -16,17 +16,26 @@

#include "btif/include/btif_dm.h"

#include <android_bluetooth_flags.h>
#include <flag_macros.h>
#include <gtest/gtest.h>

#include <memory>

#include "bta/include/bta_api_data_types.h"
#include "btif/include/mock_core_callbacks.h"
#include "stack/include/btm_api_types.h"
#include "gd/module.h"
#include "main/shim/stack.h"
#include "stack/include/btm_ble_api_types.h"
#include "storage/storage_module.h"

using bluetooth::core::testing::MockCoreInterface;

namespace {
const RawAddress kRawAddress = {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}};
constexpr char kBdName[] = {'k', 'B', 'd', 'N', 'a', 'm', 'e', '\0'};
}  // namespace

namespace bluetooth {
namespace legacy {
namespace testing {
@@ -40,6 +49,9 @@ void bta_energy_info_cb(tBTM_BLE_TX_TIME_MS tx_time,
                        tBTM_BLE_ENERGY_USED energy_used,
                        tBTM_CONTRL_STATE ctrl_state, tBTA_STATUS status);

void btif_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,
                                 tBTA_DM_SEARCH* p_data);

}  // namespace testing
}  // namespace legacy
}  // namespace bluetooth
@@ -105,3 +117,83 @@ TEST_F(BtifDmWithUidTest, bta_energy_info_cb__with_uid) {

  ASSERT_TRUE(invoke_energy_info_cb_entered);
}

class BtifDmWithStackTest : public BtifDmTest {
 protected:
  void SetUp() override {
    BtifDmTest::SetUp();
    modules_.add<bluetooth::storage::StorageModule>();
    bluetooth::shim::Stack::GetInstance()->StartModuleStack(
        &modules_,
        new bluetooth::os::Thread("gd_stack_thread",
                                  bluetooth::os::Thread::Priority::NORMAL));
  }

  void TearDown() override {
    bluetooth::shim::Stack::GetInstance()->Stop();
    BtifDmTest::TearDown();
  }
  bluetooth::ModuleList modules_;
};

#define MY_PACKAGE com::android::bluetooth::flags

TEST_F_WITH_FLAGS(BtifDmWithStackTest,
                  btif_dm_search_services_evt__BTA_DM_NAME_READ_EVT,
                  REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(
                      MY_PACKAGE, rnr_present_during_service_discovery))) {
  static struct {
    bt_status_t status;
    RawAddress bd_addr;
    int num_properties;
    std::vector<bt_property_t> properties;
  } invoke_remote_device_properties_cb{
      .status = BT_STATUS_NOT_READY,
      .bd_addr = RawAddress::kEmpty,
      .num_properties = -1,
      .properties = {},
  };

  bluetooth::core::testing::mock_event_callbacks
      .invoke_remote_device_properties_cb =
      [](bt_status_t status, RawAddress bd_addr, int num_properties,
         bt_property_t* properties) {
        invoke_remote_device_properties_cb = {
            .status = status,
            .bd_addr = bd_addr,
            .num_properties = num_properties,
            .properties = std::vector<bt_property_t>(
                properties, properties + (size_t)num_properties),
        };
      };

  tBTA_DM_SEARCH data = {
      .disc_res =
          {
              // tBTA_DM_DISC_RES
              .bd_addr = kRawAddress,
              .bd_name = {},
              .services = 0,
              .device_type = BT_DEVICE_TYPE_UNKNOWN,
              .num_uuids = 0,
              .p_uuid_list = nullptr,
              .result = BTA_SUCCESS,
              .hci_status = HCI_SUCCESS,
          },
  };
  bd_name_copy(data.disc_res.bd_name, kBdName);

  bluetooth::legacy::testing::btif_dm_search_services_evt(BTA_DM_NAME_READ_EVT,
                                                          &data);

  ASSERT_EQ(BT_STATUS_SUCCESS, invoke_remote_device_properties_cb.status);
  ASSERT_EQ(kRawAddress, invoke_remote_device_properties_cb.bd_addr);
  ASSERT_EQ(1, invoke_remote_device_properties_cb.num_properties);
  ASSERT_EQ(BT_PROPERTY_BDNAME,
            invoke_remote_device_properties_cb.properties[0].type);
  ASSERT_EQ((int)strlen(kBdName),
            invoke_remote_device_properties_cb.properties[0].len);
  ASSERT_STREQ(
      kBdName,
      (const char*)invoke_remote_device_properties_cb.properties[0].val);
}
+5 −0
Original line number Diff line number Diff line
@@ -59,6 +59,11 @@ public:
   list_.push_back(&T::Factory);
 }

 // Return the number of modules in this list
 size_t NumModules() const {
   return list_.size();
 }

 private:
  std::vector<const ModuleFactory*> list_;
};
+14 −0
Original line number Diff line number Diff line
@@ -102,6 +102,20 @@ void Stack::StartEverything() {
  bluetooth::shim::init_distance_measurement_manager();
}

void Stack::StartModuleStack(const ModuleList* modules,
                             const os::Thread* thread) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
  stack_thread_ = const_cast<os::Thread*>(thread);
  LOG_INFO("Starting Gd stack");

  stack_manager_.StartUp(const_cast<ModuleList*>(modules), stack_thread_);
  stack_handler_ = new os::Handler(stack_thread_);

  Start(const_cast<ModuleList*>(modules));
  is_running_ = true;
}

void Stack::Start(ModuleList* modules) {
  ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
  LOG_INFO("%s Starting Gd stack", __func__);
Loading