mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Allow multiple clients to discover on BTC
PiperOrigin-RevId: 648735657
This commit is contained in:
committed by
Copybara-Service
parent
22b1cda657
commit
c5869cf5fc
@@ -130,9 +130,11 @@ cc_test(
|
||||
"//connections/implementation/mediums/ble_v2",
|
||||
"//internal/flags:nearby_flags",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:cancellation_flag",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/strings",
|
||||
|
||||
@@ -56,7 +56,7 @@ BluetoothClassic::BluetoothClassic(
|
||||
|
||||
BluetoothClassic::~BluetoothClassic() {
|
||||
// Destructor is not taking locks, but methods it is calling are.
|
||||
StopDiscovery();
|
||||
StopAllDiscovery();
|
||||
while (!server_sockets_.empty()) {
|
||||
StopAcceptingConnections(server_sockets_.begin()->first);
|
||||
}
|
||||
@@ -197,9 +197,15 @@ bool BluetoothClassic::RestoreDeviceName() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BluetoothClassic::StartDiscovery(DiscoveredDeviceCallback callback) {
|
||||
bool BluetoothClassic::StartDiscovery(const std::string& serviceId,
|
||||
DiscoveredDeviceCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (serviceId.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Refusing to start discovery; service ID is empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!radio_.IsEnabled()) {
|
||||
NEARBY_LOGS(INFO) << "Can't discover BT devices because BT isn't enabled.";
|
||||
return false;
|
||||
@@ -211,43 +217,94 @@ bool BluetoothClassic::StartDiscovery(DiscoveredDeviceCallback callback) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsDiscoveringLocked()) {
|
||||
if (IsDiscoveringLocked(serviceId)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start discovery of BT devices because another "
|
||||
"discovery is already in-progress.";
|
||||
"discovery is already in-progress for service_id="
|
||||
<< serviceId;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_->StartDiscovery(std::move(callback))) {
|
||||
NEARBY_LOGS(INFO) << "Failed to start discovery of BT devices.";
|
||||
return false;
|
||||
if (!HasDiscoveryCallbacks()) {
|
||||
BluetoothClassicMedium::DiscoveryCallback medium_callback{
|
||||
.device_discovered_cb =
|
||||
[this](BluetoothDevice& device) {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
for (auto& [service_id, callback] : discovery_callbacks_) {
|
||||
if (callback.device_discovered_cb) {
|
||||
callback.device_discovered_cb(device);
|
||||
}
|
||||
}
|
||||
},
|
||||
.device_name_changed_cb =
|
||||
[this](BluetoothDevice& device) {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
for (auto& [service_id, callback] : discovery_callbacks_) {
|
||||
if (callback.device_name_changed_cb) {
|
||||
callback.device_name_changed_cb(device);
|
||||
}
|
||||
}
|
||||
},
|
||||
.device_lost_cb =
|
||||
[this](BluetoothDevice& device) {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
for (auto& [service_id, callback] : discovery_callbacks_) {
|
||||
if (callback.device_lost_cb) {
|
||||
callback.device_lost_cb(device);
|
||||
}
|
||||
}
|
||||
}};
|
||||
|
||||
if (!medium_->StartDiscovery(std::move(medium_callback))) {
|
||||
NEARBY_LOGS(INFO) << "Failed to start discovery of BT devices.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
AddDiscoveryCallback(serviceId, std::move(callback));
|
||||
|
||||
// Mark the fact that we're currently performing a Bluetooth scan.
|
||||
scan_info_.valid = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BluetoothClassic::StopDiscovery() {
|
||||
bool BluetoothClassic::StopDiscovery(const std::string& serviceId) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsDiscoveringLocked()) {
|
||||
if (!IsDiscoveringLocked(serviceId)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't stop discovery of BT devices because it never started.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_->StopDiscovery()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to stop discovery of Bluetooth devices.";
|
||||
return false;
|
||||
}
|
||||
RemoveDiscoveryCallback(serviceId);
|
||||
|
||||
scan_info_.valid = false;
|
||||
if (!HasDiscoveryCallbacks()) {
|
||||
if (!medium_->StopDiscovery()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to stop discovery of Bluetooth devices.";
|
||||
return false;
|
||||
}
|
||||
|
||||
scan_info_.valid = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BluetoothClassic::IsDiscoveringLocked() const { return scan_info_.valid; }
|
||||
bool BluetoothClassic::IsDiscoveringLocked(const std::string& serviceId) const {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
return scan_info_.valid && discovery_callbacks_.contains(serviceId);
|
||||
}
|
||||
|
||||
void BluetoothClassic::StopAllDiscovery() {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!medium_->StopDiscovery()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to stop discovery of Bluetooth devices.";
|
||||
}
|
||||
|
||||
RemoveAllDiscoveryCallbacks();
|
||||
scan_info_.valid = false;
|
||||
}
|
||||
|
||||
bool BluetoothClassic::StartAcceptingConnections(
|
||||
const std::string& service_id, AcceptedConnectionCallback callback) {
|
||||
@@ -292,8 +349,8 @@ bool BluetoothClassic::StartAcceptingConnections(
|
||||
server_sockets_.emplace(service_id, std::move(socket)).first->second;
|
||||
|
||||
// Start the accept loop on a dedicated thread - this stays alive and
|
||||
// listening for new incoming connections until StopAcceptingConnections() is
|
||||
// invoked.
|
||||
// listening for new incoming connections until StopAcceptingConnections()
|
||||
// is invoked.
|
||||
accept_loops_runner_.Execute(
|
||||
"bt-accept",
|
||||
[callback = std::move(callback), server_socket = std::move(owned_socket),
|
||||
@@ -341,9 +398,9 @@ bool BluetoothClassic::StopAcceptingConnections(const std::string& service_id) {
|
||||
}
|
||||
|
||||
// Closing the BluetoothServerSocket will kick off the suicide of the thread
|
||||
// in accept_loops_thread_pool_ that blocks on BluetoothServerSocket.accept().
|
||||
// That may take some time to complete, but there's no particular reason to
|
||||
// wait around for it.
|
||||
// in accept_loops_thread_pool_ that blocks on
|
||||
// BluetoothServerSocket.accept(). That may take some time to complete, but
|
||||
// there's no particular reason to wait around for it.
|
||||
auto item = server_sockets_.extract(it);
|
||||
|
||||
// Store a handle to the BluetoothServerSocket, so we can use it after
|
||||
@@ -401,7 +458,8 @@ BluetoothSocket BluetoothClassic::AttemptToConnect(
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << "BluetoothClassic::Connect: service_id=" << service_id
|
||||
<< ", device=" << &bluetooth_device;
|
||||
// Socket to return. To allow for NRVO to work, it has to be a single object.
|
||||
// Socket to return. To allow for NRVO to work, it has to be a single
|
||||
// object.
|
||||
BluetoothSocket socket;
|
||||
|
||||
if (service_id.empty()) {
|
||||
@@ -437,6 +495,28 @@ BluetoothSocket BluetoothClassic::AttemptToConnect(
|
||||
return socket;
|
||||
}
|
||||
|
||||
bool BluetoothClassic::HasDiscoveryCallbacks() const {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
return !discovery_callbacks_.empty();
|
||||
}
|
||||
|
||||
void BluetoothClassic::RemoveDiscoveryCallback(const std::string& service_id) {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
if (discovery_callbacks_.contains(service_id)) {
|
||||
discovery_callbacks_.erase(service_id);
|
||||
}
|
||||
}
|
||||
void BluetoothClassic::AddDiscoveryCallback(const std::string& service_id,
|
||||
DiscoveredDeviceCallback callback) {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
discovery_callbacks_.insert({service_id, std::move(callback)});
|
||||
}
|
||||
|
||||
void BluetoothClassic::RemoveAllDiscoveryCallbacks() {
|
||||
MutexLock lock(&discovery_callbacks_mutex_);
|
||||
discovery_callbacks_.clear();
|
||||
}
|
||||
|
||||
BluetoothDevice BluetoothClassic::GetRemoteDevice(
|
||||
const std::string& mac_address) {
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -448,9 +528,9 @@ BluetoothDevice BluetoothClassic::GetRemoteDevice(
|
||||
return medium_->GetRemoteDevice(mac_address);
|
||||
}
|
||||
|
||||
bool BluetoothClassic::IsDiscovering() const {
|
||||
bool BluetoothClassic::IsDiscovering(const std::string& serviceId) const {
|
||||
MutexLock lock(&mutex_);
|
||||
return IsDiscoveringLocked();
|
||||
return IsDiscoveringLocked(serviceId);
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,17 +61,16 @@ class BluetoothClassic {
|
||||
// Called by server.
|
||||
bool TurnOffDiscoverability() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Enables BT discovery mode. Will report any discoverable devices in range
|
||||
// through a callback.
|
||||
// Returns true, if discovery mode was enabled, false otherwise.
|
||||
// Called by client.
|
||||
bool StartDiscovery(DiscoveredDeviceCallback callback)
|
||||
// Enables BT discovery for serviceId. If it is the first call to start
|
||||
// discovery, will enable BT discovery mode.
|
||||
// Returns true, if discovery enabled for serviceId, false otherwise.
|
||||
bool StartDiscovery(const std::string& serviceId,
|
||||
DiscoveredDeviceCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables BT discovery mode.
|
||||
// Returns true, if discovery mode was previously enabled, false otherwise.
|
||||
// Called by client.
|
||||
bool StopDiscovery() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
// Disables BT discovery for serviceId.
|
||||
// if it is the last call to stop discovery, will disable BT discovery mode.
|
||||
bool StopDiscovery(const std::string& serviceId) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Starts a worker thread, creates a BT server socket, associates it with a
|
||||
// service ID; in a worker thread repeatedly calls ServerSocket::Accept().
|
||||
@@ -119,7 +118,8 @@ class BluetoothClassic {
|
||||
BluetoothDevice GetRemoteDevice(const std::string& mac_address)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsDiscovering() const ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool IsDiscovering(const std::string& serviceId) const
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
protected:
|
||||
// Use for unit tests only to inject a BluetoothClassicMedium.
|
||||
@@ -171,7 +171,10 @@ class BluetoothClassic {
|
||||
bool RestoreDeviceName() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Returns true if device is currently in discovery mode.
|
||||
bool IsDiscoveringLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
bool IsDiscoveringLocked(const std::string& serviceId) const
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
void StopAllDiscovery() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Establishes connection to BT service that was might be started on another
|
||||
// device with StartAcceptingConnections() using the same service_id.
|
||||
@@ -182,6 +185,17 @@ class BluetoothClassic {
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag);
|
||||
|
||||
// Accesses to discovery callbacks.
|
||||
bool HasDiscoveryCallbacks() const
|
||||
ABSL_LOCKS_EXCLUDED(discovery_callbacks_mutex_);
|
||||
void RemoveDiscoveryCallback(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(discovery_callbacks_mutex_);
|
||||
void AddDiscoveryCallback(const std::string& service_id,
|
||||
DiscoveredDeviceCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(discovery_callbacks_mutex_);
|
||||
void RemoveAllDiscoveryCallbacks()
|
||||
ABSL_LOCKS_EXCLUDED(discovery_callbacks_mutex_);
|
||||
|
||||
mutable Mutex mutex_;
|
||||
BluetoothRadio& radio_ ABSL_GUARDED_BY(mutex_);
|
||||
BluetoothAdapter& adapter_ ABSL_GUARDED_BY(mutex_);
|
||||
@@ -209,6 +223,11 @@ class BluetoothClassic {
|
||||
// and thus require pointer stability.
|
||||
absl::flat_hash_map<std::string, BluetoothServerSocket> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
// A map of service ID to discovery callback.
|
||||
mutable Mutex discovery_callbacks_mutex_;
|
||||
absl::flat_hash_map<std::string, DiscoveredDeviceCallback>
|
||||
discovery_callbacks_ ABSL_GUARDED_BY(discovery_callbacks_mutex_);
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -18,16 +18,18 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "connections/implementation/mediums/bluetooth_radio.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/bluetooth_classic.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#include "internal/platform/feature_flags.h"
|
||||
#include "internal/platform/implementation/system_clock.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "internal/platform/system_clock.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
@@ -45,6 +47,9 @@ constexpr FeatureFlags kTestCases[] = {
|
||||
};
|
||||
|
||||
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
constexpr absl::string_view kServiceId1{"service ID 1"};
|
||||
constexpr absl::string_view kServiceId2{"service ID 2"};
|
||||
|
||||
class FakeBluetoothClassicMedium final : public BluetoothClassicMedium {
|
||||
public:
|
||||
@@ -126,12 +131,123 @@ class BluetoothClassicTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
std::unique_ptr<TestBluetoothClassic> bt_b_;
|
||||
};
|
||||
|
||||
TEST_P(BluetoothClassicTest, CanConnect) {
|
||||
TEST_P(BluetoothClassicTest, CanNotTurnOnDiscoverability) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
constexpr absl::string_view kServiceName1{"service name"};
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
|
||||
// Cannot turn on discoverability to an empty device name.
|
||||
EXPECT_FALSE(bt_client.TurnOnDiscoverability(""));
|
||||
|
||||
// Cannot turn on discoverability when radio is disabled.
|
||||
radio_for_client.Disable();
|
||||
EXPECT_FALSE(bt_client.TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
radio_for_client.Enable();
|
||||
|
||||
// Cannot connect when discovery is running.
|
||||
EXPECT_TRUE(bt_client.TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
env_.Sync();
|
||||
EXPECT_FALSE(bt_client.TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CanNotConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
|
||||
// Cannot connect to an empty service id.
|
||||
CancellationFlag flag;
|
||||
BluetoothDevice discovered_device;
|
||||
BluetoothSocket socket_for_client =
|
||||
bt_client.Connect(discovered_device, "", &flag);
|
||||
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
|
||||
// Cannot connect when radio is disabled.
|
||||
radio_for_client.Disable();
|
||||
socket_for_client =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
radio_for_client.Enable();
|
||||
|
||||
// Cannot connect when adapter is disabled.
|
||||
radio_for_client.GetBluetoothAdapter().SetStatus(
|
||||
BluetoothAdapter::Status::kDisabled);
|
||||
socket_for_client =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CannotStartAcceptingConnections) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
|
||||
// Cannot start accepting connections to an empty service ID.
|
||||
EXPECT_FALSE(bt_client.StartAcceptingConnections(
|
||||
"", [&](const std::string& service_id, BluetoothSocket socket) {}));
|
||||
|
||||
// Cannot start accepting connections when radio is disabled.
|
||||
radio_for_client.Disable();
|
||||
EXPECT_FALSE(bt_client.StartAcceptingConnections(
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {}));
|
||||
radio_for_client.Enable();
|
||||
|
||||
// Cannot start accepting connections when it is already accepting.
|
||||
EXPECT_FALSE(bt_client.IsAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(bt_client.StartAcceptingConnections(
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {}));
|
||||
EXPECT_TRUE(bt_client.IsAcceptingConnections(std::string(kServiceId1)));
|
||||
env_.Sync();
|
||||
EXPECT_FALSE(bt_client.StartAcceptingConnections(
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {}));
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CannotStopAcceptingConnections) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
|
||||
// Cannot stop accepting connections to an empty service ID.
|
||||
EXPECT_FALSE(bt_client.StopAcceptingConnections(""));
|
||||
|
||||
// Cannot stop accepting connections when service ID is not accepting.
|
||||
EXPECT_FALSE(bt_client.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CannotStartDiscovery) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
|
||||
// Cannot start discovery when service ID is empty.
|
||||
EXPECT_FALSE(bt_client.StartDiscovery("", {}));
|
||||
|
||||
// Cannot start discovery when radio is disabled.
|
||||
radio_for_client.Disable();
|
||||
EXPECT_FALSE(bt_client.StartDiscovery(std::string(kServiceId1), {}));
|
||||
radio_for_client.Enable();
|
||||
|
||||
// Cannot start discovery when it is already discovering.
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(std::string(kServiceId1), {}));
|
||||
EXPECT_FALSE(bt_client.StartDiscovery(std::string(kServiceId1), {}));
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CanConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothRadio& radio_for_server = *radio_b_;
|
||||
@@ -146,31 +262,33 @@ TEST_P(BluetoothClassicTest, CanConnect) {
|
||||
std::string(kDeviceName));
|
||||
CountDownLatch latch(1);
|
||||
BluetoothDevice discovered_device;
|
||||
EXPECT_TRUE(bt_client.StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.TurnOffDiscoverability());
|
||||
ASSERT_TRUE(discovered_device.IsValid());
|
||||
BluetoothSocket socket_for_server;
|
||||
CountDownLatch accept_latch(1);
|
||||
EXPECT_TRUE(bt_server.StartAcceptingConnections(
|
||||
std::string(kServiceName1),
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag;
|
||||
BluetoothSocket socket_for_client =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceName1), &flag);
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_server.GetRemoteDevice().IsValid());
|
||||
@@ -181,9 +299,6 @@ TEST_P(BluetoothClassicTest, CanCancelBeforeConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
constexpr absl::string_view kServiceName1{"service name"};
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothRadio& radio_for_server = *radio_b_;
|
||||
TestBluetoothClassic& bt_client = *bt_a_;
|
||||
@@ -197,47 +312,49 @@ TEST_P(BluetoothClassicTest, CanCancelBeforeConnect) {
|
||||
std::string(kDeviceName));
|
||||
CountDownLatch latch(1);
|
||||
BluetoothDevice discovered_device;
|
||||
EXPECT_TRUE(bt_client.StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.TurnOffDiscoverability());
|
||||
ASSERT_TRUE(discovered_device.IsValid());
|
||||
BluetoothSocket socket_for_server;
|
||||
CountDownLatch accept_latch(1);
|
||||
EXPECT_TRUE(bt_server.StartAcceptingConnections(
|
||||
std::string(kServiceName1),
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag(true);
|
||||
BluetoothSocket socket_for_client =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceName1), &flag);
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_server.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client.GetRemoteDevice().IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_FALSE(socket_for_server.IsValid());
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
|
||||
// Expect an invalid socket from stopping during the first attempt to
|
||||
// connect, because `Connect` returned immediatley when it checked for
|
||||
// cancellation.
|
||||
EXPECT_EQ(1, bt_client.connect_attempts_count(std::string(kServiceName1)));
|
||||
EXPECT_EQ(1, bt_client.connect_attempts_count(std::string(kServiceId1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,9 +362,6 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
constexpr absl::string_view kServiceName1{"service name"};
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothRadio& radio_for_server = *radio_b_;
|
||||
TestBluetoothClassic& bt_client = *bt_a_;
|
||||
@@ -264,40 +378,42 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect) {
|
||||
std::string(kDeviceName));
|
||||
CountDownLatch latch(1);
|
||||
BluetoothDevice discovered_device;
|
||||
EXPECT_TRUE(bt_client.StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.TurnOffDiscoverability());
|
||||
ASSERT_TRUE(discovered_device.IsValid());
|
||||
BluetoothSocket socket_for_server;
|
||||
CountDownLatch accept_latch(1);
|
||||
EXPECT_TRUE(bt_server.StartAcceptingConnections(
|
||||
std::string(kServiceName1),
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag;
|
||||
BluetoothSocket socket_for_client =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceName1), &flag);
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_server.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client.GetRemoteDevice().IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_FALSE(socket_for_server.IsValid());
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
|
||||
@@ -307,7 +423,7 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect) {
|
||||
// during shutdown. Because of the way the iteration happens, the check for
|
||||
// is cancelled happens after the counter has already been incremented, but
|
||||
// before the attempt actually occurs.
|
||||
EXPECT_EQ(2, bt_client.connect_attempts_count(std::string(kServiceName1)));
|
||||
EXPECT_EQ(2, bt_client.connect_attempts_count(std::string(kServiceId1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,10 +431,6 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
constexpr absl::string_view kServiceName1{"service name"};
|
||||
constexpr absl::string_view kServiceName2{"anotherservice name"};
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothRadio& radio_for_server = *radio_b_;
|
||||
TestBluetoothClassic& bt_client = *bt_a_;
|
||||
@@ -331,15 +443,17 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
std::string(kDeviceName));
|
||||
CountDownLatch latch(1);
|
||||
BluetoothDevice discovered_device;
|
||||
EXPECT_TRUE(bt_client.StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.TurnOffDiscoverability());
|
||||
ASSERT_TRUE(discovered_device.IsValid());
|
||||
@@ -348,33 +462,33 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
EXPECT_TRUE(bt_server.StartAcceptingConnections(
|
||||
std::string(kServiceName1),
|
||||
std::string(kServiceId1),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {
|
||||
socket_for_server1 = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag;
|
||||
BluetoothSocket socket_for_client1 =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceName1), &flag);
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
|
||||
// Simulate the flag being cancelled during connection attempt to a different
|
||||
// endpoint.
|
||||
medium_a_->CancelDuringConnectToService();
|
||||
EXPECT_TRUE(bt_server.StartAcceptingConnections(
|
||||
std::string(kServiceName2),
|
||||
std::string(kServiceId2),
|
||||
[&](const std::string& service_id, BluetoothSocket socket) {
|
||||
socket_for_server2 = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
|
||||
BluetoothSocket socket_for_client2 =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceName2), &flag);
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId2), &flag);
|
||||
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName2)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId2)));
|
||||
EXPECT_TRUE(socket_for_server1.IsValid());
|
||||
EXPECT_TRUE(socket_for_server2.IsValid());
|
||||
EXPECT_TRUE(socket_for_client1.IsValid());
|
||||
@@ -384,8 +498,8 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
EXPECT_TRUE(socket_for_client1.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client2.GetRemoteDevice().IsValid());
|
||||
} else {
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName2)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId2)));
|
||||
EXPECT_TRUE(socket_for_client1.IsValid());
|
||||
EXPECT_FALSE(socket_for_client2.IsValid());
|
||||
|
||||
@@ -395,11 +509,11 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
// during shutdown. Because of the way the iteration happens, the check for
|
||||
// is cancelled happens after the counter has already been incremented, but
|
||||
// before the attempt actually occurs.
|
||||
EXPECT_EQ(2, bt_client.connect_attempts_count(std::string(kServiceName2)));
|
||||
EXPECT_EQ(2, bt_client.connect_attempts_count(std::string(kServiceId2)));
|
||||
|
||||
// With the first service name, we expect one attempt count since it
|
||||
// succeeded.
|
||||
EXPECT_EQ(1, bt_client.connect_attempts_count(std::string(kServiceName1)));
|
||||
EXPECT_EQ(1, bt_client.connect_attempts_count(std::string(kServiceId1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,45 +531,91 @@ TEST_F(BluetoothClassicTest, CanConstructValidObject) {
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CanStartAdvertising) {
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
EXPECT_TRUE(bt_a_->TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
EXPECT_EQ(radio_a_->GetBluetoothAdapter().GetName(), kDeviceName);
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CanStopAdvertising) {
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
EXPECT_TRUE(bt_a_->TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
EXPECT_EQ(radio_a_->GetBluetoothAdapter().GetName(), kDeviceName);
|
||||
EXPECT_TRUE(bt_a_->TurnOffDiscoverability());
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CanStartDiscovery) {
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
EXPECT_TRUE(bt_a_->TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
EXPECT_EQ(radio_a_->GetBluetoothAdapter().GetName(), kDeviceName);
|
||||
CountDownLatch latch(1);
|
||||
EXPECT_TRUE(bt_b_->StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch](BluetoothDevice& device) { latch.CountDown(); },
|
||||
}));
|
||||
EXPECT_TRUE(bt_b_->StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch](BluetoothDevice& device) { latch.CountDown(); },
|
||||
}));
|
||||
EXPECT_TRUE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_a_->TurnOffDiscoverability());
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CanStopDiscovery) {
|
||||
CountDownLatch latch(1);
|
||||
EXPECT_TRUE(bt_a_->StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch](BluetoothDevice& device) { latch.CountDown(); },
|
||||
}));
|
||||
EXPECT_TRUE(bt_a_->StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch](BluetoothDevice& device) { latch.CountDown(); },
|
||||
}));
|
||||
EXPECT_FALSE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_a_->StopDiscovery());
|
||||
EXPECT_TRUE(bt_a_->StopDiscovery(std::string(kServiceId1)));
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CanDiscoverDeviceChanges) {
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothRadio& radio_for_server = *radio_b_;
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
BluetoothClassic& bt_server = *bt_b_;
|
||||
|
||||
EXPECT_TRUE(radio_for_client.IsEnabled());
|
||||
EXPECT_TRUE(radio_for_server.IsEnabled());
|
||||
|
||||
EXPECT_TRUE(bt_server.TurnOnDiscoverability(std::string(kDeviceName)));
|
||||
EXPECT_EQ(radio_for_server.GetBluetoothAdapter().GetName(), kDeviceName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch rename_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
BluetoothDevice discovered_device;
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&discovered_latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.device_name_changed_cb =
|
||||
[&rename_latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Rename device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
rename_latch.CountDown();
|
||||
},
|
||||
.device_lost_cb =
|
||||
[&lost_latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Lost device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(radio_for_server.GetBluetoothAdapter().SetName("new_name"));
|
||||
EXPECT_TRUE(rename_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(radio_for_server.Disable());
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_client.StopDiscovery(std::string(kServiceId1)));
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CanStartAcceptingConnections) {
|
||||
constexpr absl::string_view kDeviceName{"Simulated BT device #1"};
|
||||
constexpr absl::string_view kServiceName1{"service name"};
|
||||
|
||||
BluetoothRadio& radio_for_client = *radio_a_;
|
||||
BluetoothRadio& radio_for_server = *radio_b_;
|
||||
BluetoothClassic& bt_client = *bt_a_;
|
||||
@@ -468,24 +628,58 @@ TEST_F(BluetoothClassicTest, CanStartAcceptingConnections) {
|
||||
EXPECT_EQ(radio_for_server.GetBluetoothAdapter().GetName(), kDeviceName);
|
||||
CountDownLatch latch(1);
|
||||
BluetoothDevice discovered_device;
|
||||
EXPECT_TRUE(bt_client.StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(bt_client.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb =
|
||||
[&latch, &discovered_device](BluetoothDevice& device) {
|
||||
discovered_device = device;
|
||||
NEARBY_LOG(INFO, "Discovered device=%p [impl=%p]", &device,
|
||||
&device.GetImpl());
|
||||
latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.TurnOffDiscoverability());
|
||||
EXPECT_TRUE(discovered_device.IsValid());
|
||||
EXPECT_TRUE(
|
||||
bt_server.StartAcceptingConnections(std::string(kServiceName1), {}));
|
||||
bt_server.StartAcceptingConnections(std::string(kServiceId1), {}));
|
||||
// Allow StartAcceptingConnections do something, before stopping it.
|
||||
// This is best effort, because no callbacks are invoked in this scenario.
|
||||
SystemClock::Sleep(kWaitDuration);
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceName1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, CheckDiscoveryingStatus) {
|
||||
BluetoothClassic& bluetooth_classic = *bt_a_;
|
||||
|
||||
EXPECT_FALSE(bluetooth_classic.IsDiscovering(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(bluetooth_classic.StartDiscovery(
|
||||
std::string(kServiceId1),
|
||||
{
|
||||
.device_discovered_cb = [](BluetoothDevice& device) {},
|
||||
}));
|
||||
EXPECT_TRUE(bluetooth_classic.IsDiscovering(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(bluetooth_classic.StopDiscovery(std::string(kServiceId1)));
|
||||
EXPECT_FALSE(bluetooth_classic.IsDiscovering(std::string(kServiceId1)));
|
||||
EXPECT_FALSE(bluetooth_classic.StopDiscovery(std::string(kServiceId1)));
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, GetMacAddress) {
|
||||
EXPECT_NE(bt_a_->GetMacAddress(), "");
|
||||
radio_a_->Disable();
|
||||
EXPECT_EQ(bt_a_->GetMacAddress(), "");
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicTest, GetRemoteDevice) {
|
||||
EXPECT_EQ(
|
||||
bt_a_->GetRemoteDevice(radio_b_->GetBluetoothAdapter().GetMacAddress())
|
||||
.GetMacAddress(),
|
||||
radio_b_->GetBluetoothAdapter().GetMacAddress());
|
||||
radio_a_->Disable();
|
||||
EXPECT_FALSE(
|
||||
bt_a_->GetRemoteDevice(radio_b_->GetBluetoothAdapter().GetMacAddress())
|
||||
.IsValid());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1091,7 +1091,8 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
|
||||
location::nearby::proto::connections::UNKNOWN_MEDIUM) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartDiscoveryImpl: BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
bluetooth_classic_discoverer_client_id_ = client->GetClientId();
|
||||
bluetooth_classic_client_id_to_service_id_map_.insert(
|
||||
{client->GetClientId(), service_id});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1115,14 +1116,16 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
|
||||
|
||||
Status P2pClusterPcpHandler::StopDiscoveryImpl(ClientProxy* client) {
|
||||
wifi_lan_medium_.StopDiscovery(client->GetDiscoveryServiceId());
|
||||
if (client->GetClientId() == bluetooth_classic_discoverer_client_id_) {
|
||||
bluetooth_medium_.StopDiscovery();
|
||||
bluetooth_classic_discoverer_client_id_ = 0;
|
||||
if (bluetooth_classic_client_id_to_service_id_map_.contains(
|
||||
client->GetClientId())) {
|
||||
bluetooth_medium_.StopDiscovery(
|
||||
bluetooth_classic_client_id_to_service_id_map_.at(
|
||||
client->GetClientId()));
|
||||
bluetooth_classic_client_id_to_service_id_map_.erase(client->GetClientId());
|
||||
} else {
|
||||
NEARBY_LOGS(INFO) << "Skipped BT StopDiscovery for client="
|
||||
<< client->GetClientId()
|
||||
<< ", client that started discovery is "
|
||||
<< bluetooth_classic_discoverer_client_id_;
|
||||
<< " because it is not in discovery.";
|
||||
}
|
||||
|
||||
if (NearbyFlags::GetInstance().GetBoolFlag(
|
||||
@@ -1539,7 +1542,7 @@ P2pClusterPcpHandler::UpdateDiscoveryOptionsImpl(
|
||||
if (NeedsToTurnOffDiscoveryMedium(Medium::BLUETOOTH, old_options,
|
||||
discovery_options) ||
|
||||
needs_restart) {
|
||||
bluetooth_medium_.StopDiscovery();
|
||||
bluetooth_medium_.StopDiscovery(std::string(service_id));
|
||||
StartEndpointLostByMediumAlarms(client, Medium::BLUETOOTH);
|
||||
}
|
||||
// wifi lan
|
||||
@@ -1742,17 +1745,19 @@ Medium P2pClusterPcpHandler::StartBluetoothAdvertising(
|
||||
Medium P2pClusterPcpHandler::StartBluetoothDiscovery(
|
||||
ClientProxy* client, const std::string& service_id) {
|
||||
if (bluetooth_radio_.Enable() &&
|
||||
bluetooth_medium_.StartDiscovery({
|
||||
.device_discovered_cb = absl::bind_front(
|
||||
&P2pClusterPcpHandler::BluetoothDeviceDiscoveredHandler, this,
|
||||
client, service_id),
|
||||
.device_name_changed_cb = absl::bind_front(
|
||||
&P2pClusterPcpHandler::BluetoothNameChangedHandler, this, client,
|
||||
service_id),
|
||||
.device_lost_cb = absl::bind_front(
|
||||
&P2pClusterPcpHandler::BluetoothDeviceLostHandler, this, client,
|
||||
service_id),
|
||||
})) {
|
||||
bluetooth_medium_.StartDiscovery(
|
||||
service_id,
|
||||
{
|
||||
.device_discovered_cb = absl::bind_front(
|
||||
&P2pClusterPcpHandler::BluetoothDeviceDiscoveredHandler, this,
|
||||
client, service_id),
|
||||
.device_name_changed_cb = absl::bind_front(
|
||||
&P2pClusterPcpHandler::BluetoothNameChangedHandler, this,
|
||||
client, service_id),
|
||||
.device_lost_cb = absl::bind_front(
|
||||
&P2pClusterPcpHandler::BluetoothDeviceLostHandler, this,
|
||||
client, service_id),
|
||||
})) {
|
||||
NEARBY_LOGS(INFO) << "In StartBluetoothDiscovery(), client="
|
||||
<< client->GetClientId()
|
||||
<< " started scanning for Bluetooth for service_id="
|
||||
@@ -1777,7 +1782,8 @@ void P2pClusterPcpHandler::StartBluetoothDiscoveryWithPause(
|
||||
mediums_started_successfully.end(),
|
||||
location::nearby::proto::connections::BLE) !=
|
||||
mediums_started_successfully.end()) {
|
||||
if (bluetooth_medium_.IsDiscovering()) {
|
||||
if (bluetooth_medium_.IsDiscovering(service_id)) {
|
||||
NEARBY_LOGS(INFO) << "xxxxx";
|
||||
// If we are already discovering, we don't need to start again.
|
||||
Medium bluetooth_medium = StartBluetoothDiscovery(client, service_id);
|
||||
if (bluetooth_medium !=
|
||||
@@ -1786,7 +1792,8 @@ void P2pClusterPcpHandler::StartBluetoothDiscoveryWithPause(
|
||||
"P2pClusterPcpHandler::StartBluetoothDiscoveryWithPause: "
|
||||
"BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
bluetooth_classic_discoverer_client_id_ = client->GetClientId();
|
||||
bluetooth_classic_client_id_to_service_id_map_.insert(
|
||||
{client->GetClientId(), service_id});
|
||||
}
|
||||
} else {
|
||||
NEARBY_LOGS(INFO) << "Pause bluetooth discovery for service id : "
|
||||
@@ -1803,7 +1810,8 @@ void P2pClusterPcpHandler::StartBluetoothDiscoveryWithPause(
|
||||
INFO,
|
||||
"P2pClusterPcpHandler::StartBluetoothDiscoveryWithPause: BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
bluetooth_classic_discoverer_client_id_ = client->GetClientId();
|
||||
bluetooth_classic_client_id_to_service_id_map_.insert(
|
||||
{client->GetClientId(), service_id});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -300,7 +300,10 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
WifiDirect& wifi_direct_medium_;
|
||||
mediums::WebRtc& webrtc_medium_;
|
||||
InjectedBluetoothDeviceStore& injected_bluetooth_device_store_;
|
||||
std::int64_t bluetooth_classic_discoverer_client_id_{0};
|
||||
// Maintains a map of client_id to service_id for bluetooth classic
|
||||
// discoverer.
|
||||
absl::flat_hash_map<std::int64_t, std::string>
|
||||
bluetooth_classic_client_id_to_service_id_map_;
|
||||
std::int64_t bluetooth_classic_advertiser_client_id_{0};
|
||||
|
||||
// Maps a BlePeripheral to its corresponding BleEndpointState.
|
||||
|
||||
@@ -483,7 +483,7 @@ TEST_P(P2pClusterPcpHandlerTest, PauseBluetoothClassicDiscovery) {
|
||||
Status{Status::kSuccess});
|
||||
|
||||
EXPECT_TRUE(mediums_a.GetBleV2().IsScanning(service_id_));
|
||||
EXPECT_FALSE(mediums_a.GetBluetoothClassic().IsDiscovering());
|
||||
EXPECT_FALSE(mediums_a.GetBluetoothClassic().IsDiscovering(service_id_));
|
||||
// Before we finish the test, we have to stop discovery for other mediums that
|
||||
// may be still ongoing.
|
||||
handler_a.StopDiscovery(&client_a_);
|
||||
@@ -535,7 +535,7 @@ TEST_P(P2pClusterPcpHandlerTest, ResumeBluetoothClassicDiscovery) {
|
||||
Status{Status::kSuccess});
|
||||
|
||||
EXPECT_TRUE(mediums_a.GetBleV2().IsScanning(service_id_));
|
||||
EXPECT_FALSE(mediums_a.GetBluetoothClassic().IsDiscovering());
|
||||
EXPECT_FALSE(mediums_a.GetBluetoothClassic().IsDiscovering(service_id_));
|
||||
|
||||
EXPECT_EQ(
|
||||
handler_b.StartAdvertising(&client_b_, service_id_, advertising_options_,
|
||||
@@ -546,7 +546,7 @@ TEST_P(P2pClusterPcpHandlerTest, ResumeBluetoothClassicDiscovery) {
|
||||
absl::SleepFor(absl::Milliseconds(100));
|
||||
|
||||
EXPECT_TRUE(mediums_a.GetBleV2().IsScanning(service_id_));
|
||||
EXPECT_TRUE(mediums_a.GetBluetoothClassic().IsDiscovering());
|
||||
EXPECT_TRUE(mediums_a.GetBluetoothClassic().IsDiscovering(service_id_));
|
||||
|
||||
// Before we finish the test, we have to stop discovery for other mediums that
|
||||
// may be still ongoing.
|
||||
@@ -724,7 +724,7 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateDiscoveryOptionsNoLowPower) {
|
||||
EXPECT_EQ(old_enabled.wifi_lan,
|
||||
mediums_a.GetWifiLan().IsDiscovering(service_id_));
|
||||
EXPECT_EQ(old_enabled.bluetooth,
|
||||
mediums_a.GetBluetoothClassic().StopDiscovery());
|
||||
mediums_a.GetBluetoothClassic().StopDiscovery(service_id_));
|
||||
NEARBY_LOGS(INFO) << "started discovery";
|
||||
// Update discovery options
|
||||
EXPECT_TRUE(
|
||||
@@ -739,7 +739,7 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateDiscoveryOptionsNoLowPower) {
|
||||
EXPECT_EQ(new_enabled.wifi_lan,
|
||||
mediums_a.GetWifiLan().IsDiscovering(service_id_));
|
||||
EXPECT_EQ(new_enabled.bluetooth,
|
||||
mediums_a.GetBluetoothClassic().StopDiscovery());
|
||||
mediums_a.GetBluetoothClassic().StopDiscovery(service_id_));
|
||||
handler_a.StopDiscovery(&client_a_);
|
||||
env_.Stop();
|
||||
}
|
||||
@@ -777,7 +777,8 @@ TEST_P(P2pClusterPcpHandlerTest, UpdateDiscoveryOptionsSkipMediumRestart) {
|
||||
}
|
||||
EXPECT_EQ(enabled.wifi_lan,
|
||||
mediums_a.GetWifiLan().IsDiscovering(service_id_));
|
||||
EXPECT_EQ(enabled.bluetooth, mediums_a.GetBluetoothClassic().StopDiscovery());
|
||||
EXPECT_EQ(enabled.bluetooth,
|
||||
mediums_a.GetBluetoothClassic().StopDiscovery(service_id_));
|
||||
// Update discovery options
|
||||
auto result = handler_a.UpdateDiscoveryOptions(&client_a_, service_id_,
|
||||
discovery_options_);
|
||||
@@ -791,7 +792,7 @@ TEST_P(P2pClusterPcpHandlerTest, UpdateDiscoveryOptionsSkipMediumRestart) {
|
||||
EXPECT_EQ(enabled.wifi_lan,
|
||||
mediums_a.GetWifiLan().IsDiscovering(service_id_));
|
||||
// We didn't restart the medium.
|
||||
EXPECT_FALSE(mediums_a.GetBluetoothClassic().StopDiscovery());
|
||||
EXPECT_FALSE(mediums_a.GetBluetoothClassic().StopDiscovery(service_id_));
|
||||
handler_a.StopDiscovery(&client_a_);
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user