mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Merge branch 'google3'
Change-Id: I08c677177d69e257e6fbe6aa32a5854e0eae8c52
This commit is contained in:
@@ -28,11 +28,13 @@ cc_library(
|
||||
"//platform_v2/public:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":logging",
|
||||
"//platform_v2/api:platform",
|
||||
"//platform_v2/api:types",
|
||||
"//platform_v2/base",
|
||||
"//platform_v2/base:util",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/container:flat_hash_map",
|
||||
"//absl/time",
|
||||
"//absl/types:any",
|
||||
],
|
||||
@@ -40,8 +42,12 @@ cc_library(
|
||||
|
||||
cc_library(
|
||||
name = "comm",
|
||||
srcs = [
|
||||
"bluetooth_classic.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"bluetooth_adapter.h",
|
||||
"bluetooth_classic.h",
|
||||
"webrtc.h",
|
||||
],
|
||||
visibility = [
|
||||
@@ -49,8 +55,12 @@ cc_library(
|
||||
"//platform_v2/public:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":logging",
|
||||
":types",
|
||||
"//platform_v2/api:comm",
|
||||
"//platform_v2/api:platform",
|
||||
"//platform_v2/base",
|
||||
"//absl/container:flat_hash_map",
|
||||
"//absl/strings",
|
||||
"//webrtc/api:libjingle_peerconnection_api",
|
||||
],
|
||||
@@ -73,10 +83,12 @@ cc_library(
|
||||
|
||||
cc_test(
|
||||
name = "public_test",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"atomic_boolean_test.cc",
|
||||
"atomic_reference_test.cc",
|
||||
"bluetooth_adapter_test.cc",
|
||||
"bluetooth_classic_test.cc",
|
||||
"count_down_latch_test.cc",
|
||||
"crypto_test.cc",
|
||||
"future_test.cc",
|
||||
@@ -93,6 +105,7 @@ cc_test(
|
||||
":logging",
|
||||
":types",
|
||||
"//platform_v2/base",
|
||||
"//platform_v2/base:test_util",
|
||||
"//platform_v2/impl/g3", # build_cleaner: keep
|
||||
"//testing/base/public:gunit_main",
|
||||
"//absl/synchronization",
|
||||
|
||||
@@ -4,55 +4,81 @@
|
||||
#include <string>
|
||||
|
||||
#include "platform_v2/api/bluetooth_adapter.h"
|
||||
#include "platform_v2/api/bluetooth_classic.h"
|
||||
#include "platform_v2/api/platform.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html.
|
||||
class BluetoothDevice final {
|
||||
public:
|
||||
BluetoothDevice() = default;
|
||||
BluetoothDevice(const BluetoothDevice&) = default;
|
||||
BluetoothDevice& operator=(const BluetoothDevice&) = default;
|
||||
explicit BluetoothDevice(api::BluetoothDevice* device) : impl_(device) {}
|
||||
~BluetoothDevice() = default;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName()
|
||||
std::string GetName() const { return impl_->GetName(); }
|
||||
|
||||
api::BluetoothDevice& GetImpl() { return *impl_; }
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
private:
|
||||
api::BluetoothDevice* impl_;
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
|
||||
class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
class BluetoothAdapter final {
|
||||
public:
|
||||
using Status = api::BluetoothAdapter::Status;
|
||||
using ScanMode = api::BluetoothAdapter::ScanMode;
|
||||
|
||||
BluetoothAdapter()
|
||||
: impl_(api::ImplementationPlatform::CreateBluetoothAdapter()) {}
|
||||
~BluetoothAdapter() override = default;
|
||||
~BluetoothAdapter() = default;
|
||||
BluetoothAdapter(BluetoothAdapter&&) = default;
|
||||
BluetoothAdapter& operator=(BluetoothAdapter&&) = default;
|
||||
|
||||
// Synchronously sets the status of the BluetoothAdapter to 'status', and
|
||||
// returns true if the operation was a success.
|
||||
bool SetStatus(Status status) override { return impl_->SetStatus(status); }
|
||||
bool SetStatus(Status status) { return impl_->SetStatus(status); }
|
||||
Status GetStatus() const {
|
||||
return IsEnabled() ? Status::kEnabled : Status::kDisabled;
|
||||
}
|
||||
|
||||
// Returns true if the BluetoothAdapter's current status is
|
||||
// Status::Value::kEnabled.
|
||||
bool IsEnabled() const override { return impl_->IsEnabled(); }
|
||||
bool IsEnabled() const { return impl_->IsEnabled(); }
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getScanMode()
|
||||
//
|
||||
// Returns ScanMode::kUnknown on error.
|
||||
ScanMode GetScanMode() const override { return impl_->GetScanMode(); }
|
||||
ScanMode GetScanMode() const { return impl_->GetScanMode(); }
|
||||
|
||||
// Synchronously sets the scan mode of the adapter, and returns true if the
|
||||
// operation was a success.
|
||||
bool SetScanMode(ScanMode scan_mode) override {
|
||||
bool SetScanMode(ScanMode scan_mode) {
|
||||
return impl_->SetScanMode(scan_mode);
|
||||
}
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getName()
|
||||
// Returns an empty string on error
|
||||
std::string GetName() const override { return impl_->GetName(); }
|
||||
std::string GetName() const { return impl_->GetName(); }
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName(java.lang.String)
|
||||
bool SetName(absl::string_view name) override { return impl_->SetName(name); }
|
||||
bool SetName(absl::string_view name) { return impl_->SetName(name); }
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
// Returns reference to platform implementation.
|
||||
// This is used to communicate with platform code, and for debugging purposes.
|
||||
// Returned reference will remain valid for while BluetoothAdapter object is
|
||||
// itself valid. It matches Core() object lifetime.
|
||||
api::BluetoothAdapter& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<api::BluetoothAdapter> impl_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "platform_v2/public/bluetooth_classic.h"
|
||||
|
||||
#include "platform_v2/public/logging.h"
|
||||
#include "platform_v2/public/mutex_lock.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
BluetoothClassicMedium::~BluetoothClassicMedium() { StopDiscovery(); }
|
||||
|
||||
BluetoothSocket BluetoothClassicMedium::ConnectToService(
|
||||
BluetoothDevice& remote_device, const std::string& service_uuid) {
|
||||
NEARBY_LOG(INFO,
|
||||
"BluetoothClassicMedium::ConnectToService: device=%p [impl=%p]",
|
||||
&remote_device, &remote_device.GetImpl());
|
||||
return BluetoothSocket(
|
||||
impl_->ConnectToService(remote_device.GetImpl(), service_uuid));
|
||||
}
|
||||
|
||||
bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (discovery_enabled_) {
|
||||
NEARBY_LOG(INFO, "BT Discovery already enabled; impl=%p", &GetImpl());
|
||||
return false;
|
||||
}
|
||||
discovery_callback_ = std::move(callback);
|
||||
devices_.clear();
|
||||
discovery_enabled_ = true;
|
||||
NEARBY_LOG(INFO, "BT Discovery enabled; impl=%p", &GetImpl());
|
||||
}
|
||||
return impl_->StartDiscovery({
|
||||
.device_discovered_cb =
|
||||
[this](api::BluetoothDevice& device) {
|
||||
MutexLock lock(&mutex_);
|
||||
auto pair = devices_.emplace(
|
||||
&device, absl::make_unique<DeviceDiscoveryInfo>());
|
||||
auto& context = *pair.first->second;
|
||||
if (!pair.second) {
|
||||
NEARBY_LOG(INFO, "Adding (again) device=%p, impl=%p",
|
||||
&context.device, &device);
|
||||
return;
|
||||
}
|
||||
context.device = BluetoothDevice(&device);
|
||||
NEARBY_LOG(INFO, "Adding device=%p, impl=%p", &context.device,
|
||||
&device);
|
||||
if (!discovery_enabled_) return;
|
||||
discovery_callback_.device_discovered_cb(context.device);
|
||||
},
|
||||
.device_name_changed_cb =
|
||||
[this](api::BluetoothDevice& device) {
|
||||
MutexLock lock(&mutex_);
|
||||
auto& context = *devices_[&device];
|
||||
NEARBY_LOG(INFO, "Renaming device=%p, impl=%p", &context.device,
|
||||
&device);
|
||||
if (!discovery_enabled_) return;
|
||||
discovery_callback_.device_name_changed_cb(context.device);
|
||||
},
|
||||
.device_lost_cb =
|
||||
[this](api::BluetoothDevice& device) {
|
||||
MutexLock lock(&mutex_);
|
||||
auto item = devices_.extract(&device);
|
||||
auto& context = *item.mapped();
|
||||
NEARBY_LOG(INFO, "Removing device=%p, impl=%p", &context.device,
|
||||
&device);
|
||||
if (!discovery_enabled_) return;
|
||||
discovery_callback_.device_lost_cb(context.device);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
bool BluetoothClassicMedium::StopDiscovery() {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (!discovery_enabled_) return true;
|
||||
discovery_enabled_ = false;
|
||||
discovery_callback_ = {};
|
||||
devices_.clear();
|
||||
NEARBY_LOG(INFO, "BT Discovery disabled: impl=%p", &GetImpl());
|
||||
}
|
||||
return impl_->StopDiscovery();
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,205 @@
|
||||
#ifndef PLATFORM_V2_PUBLIC_BLUETOOTH_CLASSIC_H_
|
||||
#define PLATFORM_V2_PUBLIC_BLUETOOTH_CLASSIC_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "platform_v2/api/bluetooth_classic.h"
|
||||
#include "platform_v2/api/platform.h"
|
||||
#include "platform_v2/base/byte_array.h"
|
||||
#include "platform_v2/base/exception.h"
|
||||
#include "platform_v2/base/input_stream.h"
|
||||
#include "platform_v2/base/listeners.h"
|
||||
#include "platform_v2/base/output_stream.h"
|
||||
#include "platform_v2/public/bluetooth_adapter.h"
|
||||
#include "platform_v2/public/mutex.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html.
|
||||
class BluetoothSocket final {
|
||||
public:
|
||||
BluetoothSocket() = default;
|
||||
BluetoothSocket(const BluetoothSocket&) = default;
|
||||
BluetoothSocket& operator=(const BluetoothSocket&) = default;
|
||||
explicit BluetoothSocket(std::unique_ptr<api::BluetoothSocket> socket)
|
||||
: impl_(socket.release()) {}
|
||||
~BluetoothSocket() = default;
|
||||
|
||||
// Returns the InputStream of this connected BluetoothSocket.
|
||||
InputStream& GetInputStream() { return impl_->GetInputStream(); }
|
||||
|
||||
// Returns the OutputStream of this connected BluetoothSocket.
|
||||
OutputStream& GetOutputStream() { return impl_->GetOutputStream(); }
|
||||
|
||||
// Closes both input and output streams, marks Socket as closed.
|
||||
// After this call object should be treated as not connected.
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() { return impl_->Close(); }
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#getRemoteDevice()
|
||||
BluetoothDevice GetRemoteDevice() {
|
||||
return BluetoothDevice(impl_->GetRemoteDevice());
|
||||
}
|
||||
|
||||
// Returns true if a socket is usable. If this method returns false,
|
||||
// it is not safe to call any other method.
|
||||
// NOTE(socket validity):
|
||||
// Socket created by a default public constructor is not valid, because
|
||||
// it is missing platform implementation.
|
||||
// The only way to obtain a valid socket is through connection, such as
|
||||
// an object returned by either BluetoothClassicMedium::ConnectTotService or
|
||||
// BluetoothServerSocket::Accept().
|
||||
// These methods may also return an invalid socket if connection failed for
|
||||
// any reason.
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
// Returns reference to platform implementation.
|
||||
// This is used to communicate with platform code, and for debugging purposes.
|
||||
// Returned reference will remain valid for while BluetoothSocket object is
|
||||
// itself valid. Typically BluetoothSocket lifetime matches duration of the
|
||||
// connection, and is controlled by end user, since they hold the instance.
|
||||
api::BluetoothSocket& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<api::BluetoothSocket> impl_;
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html.
|
||||
class BluetoothServerSocket final {
|
||||
public:
|
||||
BluetoothServerSocket() = default;
|
||||
BluetoothServerSocket(const BluetoothServerSocket&) = default;
|
||||
BluetoothServerSocket& operator=(const BluetoothServerSocket&) = default;
|
||||
~BluetoothServerSocket() = default;
|
||||
explicit BluetoothServerSocket(
|
||||
std::unique_ptr<api::BluetoothServerSocket> socket)
|
||||
: impl_(std::move(socket)) {}
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#accept()
|
||||
//
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
BluetoothSocket Accept() { return BluetoothSocket(impl_->Accept()); }
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#close()
|
||||
//
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() { return impl_->Close(); }
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
api::BluetoothServerSocket& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<api::BluetoothServerSocket> impl_;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the Bluetooth Classic
|
||||
// medium.
|
||||
class BluetoothClassicMedium final {
|
||||
public:
|
||||
using Platform = api::ImplementationPlatform;
|
||||
struct DiscoveryCallback {
|
||||
// BluetoothDevice is a proxy object created as a result of BT discovery.
|
||||
// Its lifetime spans between calls to device_discovered_cb and
|
||||
// device_lost_cb.
|
||||
// It is safe to use BluetoothDevice in device_discovered_cb() callback
|
||||
// and at any time afterwards, until device_lost_cb() is called.
|
||||
// It is not safe to use BluetoothDevice after returning from
|
||||
// device_lost_cb() callback.
|
||||
std::function<void(BluetoothDevice& device)> device_discovered_cb =
|
||||
DefaultCallback<BluetoothDevice&>();
|
||||
std::function<void(BluetoothDevice& device)> device_name_changed_cb =
|
||||
DefaultCallback<BluetoothDevice&>();
|
||||
std::function<void(BluetoothDevice& device)> device_lost_cb =
|
||||
DefaultCallback<BluetoothDevice&>();
|
||||
};
|
||||
struct DeviceDiscoveryInfo {
|
||||
BluetoothDevice device;
|
||||
};
|
||||
|
||||
explicit BluetoothClassicMedium(BluetoothAdapter& adapter)
|
||||
: impl_(Platform::CreateBluetoothClassicMedium(adapter.GetImpl())),
|
||||
adapter_(adapter) {}
|
||||
|
||||
~BluetoothClassicMedium();
|
||||
|
||||
// NOTE(DiscoveryCallback):
|
||||
// BluetoothDevice is a proxy object created as a result of BT discovery.
|
||||
// Its lifetime spans between calls to device_discovered_cb and
|
||||
// device_lost_cb.
|
||||
// It is safe to use BluetoothDevice in device_discovered_cb() callback
|
||||
// and at any time afterwards, until device_lost_cb() is called.
|
||||
// It is not safe to use BluetoothDevice after returning from
|
||||
// device_lost_cb() callback.
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()
|
||||
//
|
||||
// Returns true once the process of discovery has been initiated.
|
||||
bool StartDiscovery(DiscoveryCallback callback);
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#cancelDiscovery()
|
||||
//
|
||||
// Returns true once discovery is well and truly stopped; after this returns,
|
||||
// there must be no more invocations of the DiscoveryCallback passed in to
|
||||
// StartDiscovery().
|
||||
bool StopDiscovery();
|
||||
|
||||
// A combination of
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createInsecureRfcommSocketToServiceRecord
|
||||
// followed by
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect().
|
||||
//
|
||||
// service_uuid is the canonical textual representation
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a
|
||||
// type 3 name-based
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based))
|
||||
// UUID.
|
||||
//
|
||||
// Returns a new BluetoothSocket. On Success, BluetoothSocket::IsValid()
|
||||
// returns true.
|
||||
BluetoothSocket ConnectToService(BluetoothDevice& remote_device,
|
||||
const std::string& service_uuid);
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#listenUsingInsecureRfcommWithServiceRecord
|
||||
//
|
||||
// service_uuid is the canonical textual representation
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a
|
||||
// type 3 name-based
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based))
|
||||
// UUID.
|
||||
//
|
||||
// Returns a new BluetoothServerSocket.
|
||||
// On Success, BluetoothServerSocket::IsValid() returns true.
|
||||
BluetoothServerSocket ListenForService(const std::string& service_name,
|
||||
const std::string& service_uuid) {
|
||||
return BluetoothServerSocket(
|
||||
impl_->ListenForService(service_name, service_uuid));
|
||||
}
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
api::BluetoothClassicMedium& GetImpl() { return *impl_; }
|
||||
BluetoothAdapter& GetAdapter() { return adapter_; }
|
||||
|
||||
private:
|
||||
Mutex mutex_;
|
||||
std::unique_ptr<api::BluetoothClassicMedium> impl_;
|
||||
BluetoothAdapter& adapter_;
|
||||
absl::flat_hash_map<api::BluetoothDevice*,
|
||||
std::unique_ptr<DeviceDiscoveryInfo>>
|
||||
devices_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveryCallback discovery_callback_ ABSL_GUARDED_BY(mutex_);
|
||||
bool discovery_enabled_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_V2_PUBLIC_BLUETOOTH_CLASSIC_H_
|
||||
@@ -0,0 +1,197 @@
|
||||
#include "platform_v2/public/bluetooth_classic.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/public/bluetooth_adapter.h"
|
||||
#include "platform_v2/public/count_down_latch.h"
|
||||
#include "platform_v2/public/logging.h"
|
||||
#include "platform_v2/public/single_thread_executor.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/time/time.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace {
|
||||
|
||||
class BluetoothClassicMediumTest : public ::testing::Test {
|
||||
protected:
|
||||
using DiscoveryCallback = BluetoothClassicMedium::DiscoveryCallback;
|
||||
BluetoothClassicMediumTest() {
|
||||
env_.Reset();
|
||||
adapter_a_ = std::make_unique<BluetoothAdapter>();
|
||||
adapter_b_ = std::make_unique<BluetoothAdapter>();
|
||||
bt_a_ = std::make_unique<BluetoothClassicMedium>(*adapter_a_);
|
||||
bt_b_ = std::make_unique<BluetoothClassicMedium>(*adapter_b_);
|
||||
adapter_a_->SetName("Device-A");
|
||||
adapter_b_->SetName("Device-B");
|
||||
adapter_a_->SetStatus(BluetoothAdapter::Status::kEnabled);
|
||||
adapter_b_->SetStatus(BluetoothAdapter::Status::kEnabled);
|
||||
env_.Sync();
|
||||
}
|
||||
~BluetoothClassicMediumTest() override {
|
||||
env_.Sync(false);
|
||||
adapter_a_->SetStatus(BluetoothAdapter::Status::kDisabled);
|
||||
adapter_b_->SetStatus(BluetoothAdapter::Status::kDisabled);
|
||||
bt_a_.reset();
|
||||
bt_b_.reset();
|
||||
env_.Sync(false);
|
||||
adapter_a_.reset();
|
||||
adapter_b_.reset();
|
||||
env_.Reset();
|
||||
}
|
||||
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
|
||||
std::unique_ptr<BluetoothAdapter> adapter_a_;
|
||||
std::unique_ptr<BluetoothAdapter> adapter_b_;
|
||||
std::unique_ptr<BluetoothClassicMedium> bt_a_;
|
||||
std::unique_ptr<BluetoothClassicMedium> bt_b_;
|
||||
};
|
||||
|
||||
TEST_F(BluetoothClassicMediumTest, ConstructorDestructorWorks) {
|
||||
// Make sure we can create functional adapters.
|
||||
ASSERT_TRUE(adapter_a_->IsValid());
|
||||
ASSERT_TRUE(adapter_b_->IsValid());
|
||||
|
||||
// Make sure we can create 2 distinct adapters.
|
||||
// NOTE: multiple adapters are supported on a test platform, but not
|
||||
// necessarily on every available HW platform.
|
||||
// Often, HW platform supports only one BT adapter.
|
||||
EXPECT_NE(&adapter_a_->GetImpl(), &adapter_b_->GetImpl());
|
||||
|
||||
// Make sure we can create functional mediums.
|
||||
ASSERT_TRUE(bt_a_->IsValid());
|
||||
ASSERT_TRUE(bt_b_->IsValid());
|
||||
|
||||
// Make sure we can create 2 distinct mediums.
|
||||
EXPECT_NE(&bt_a_->GetImpl(), &bt_b_->GetImpl());
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicMediumTest, CanStartDiscovery) {
|
||||
adapter_a_->SetScanMode(BluetoothAdapter::ScanMode::kConnectable);
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
bt_a_->StartDiscovery(DiscoveryCallback{
|
||||
.device_discovered_cb =
|
||||
[this, &found_latch](BluetoothDevice& device) {
|
||||
NEARBY_LOG(INFO, "Device discovered: %s", device.GetName().c_str());
|
||||
EXPECT_EQ(device.GetName(), adapter_b_->GetName());
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.device_lost_cb =
|
||||
[this, &lost_latch](BluetoothDevice& device) {
|
||||
NEARBY_LOG(INFO, "Device lost: %s", device.GetName().c_str());
|
||||
EXPECT_EQ(device.GetName(), adapter_b_->GetName());
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
adapter_b_->SetScanMode(BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_EQ(adapter_b_->GetScanMode(),
|
||||
BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
adapter_b_->SetStatus(BluetoothAdapter::Status::kDisabled);
|
||||
EXPECT_FALSE(adapter_b_->IsEnabled());
|
||||
EXPECT_TRUE(lost_latch.Await(absl::Milliseconds(1000)).result());
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicMediumTest, CanStopDiscovery) {
|
||||
adapter_a_->SetScanMode(BluetoothAdapter::ScanMode::kConnectable);
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
bt_a_->StartDiscovery(DiscoveryCallback{
|
||||
.device_discovered_cb =
|
||||
[this, &found_latch](BluetoothDevice& device) {
|
||||
NEARBY_LOG(INFO, "Device discovered: %s", device.GetName().c_str());
|
||||
EXPECT_EQ(device.GetName(), adapter_b_->GetName());
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.device_lost_cb =
|
||||
[this, &lost_latch](BluetoothDevice& device) {
|
||||
NEARBY_LOG(INFO, "Device lost: %s", device.GetName().c_str());
|
||||
EXPECT_EQ(device.GetName(), adapter_b_->GetName());
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
adapter_b_->SetScanMode(BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_EQ(adapter_b_->GetScanMode(),
|
||||
BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
bt_a_->StopDiscovery();
|
||||
adapter_b_->SetStatus(BluetoothAdapter::Status::kDisabled);
|
||||
EXPECT_FALSE(adapter_b_->IsEnabled());
|
||||
EXPECT_FALSE(lost_latch.Await(absl::Milliseconds(1000)).result());
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicMediumTest, CanListenForService) {
|
||||
adapter_a_->SetScanMode(BluetoothAdapter::ScanMode::kConnectable);
|
||||
CountDownLatch found_latch(1);
|
||||
bt_a_->StartDiscovery(DiscoveryCallback{
|
||||
.device_discovered_cb =
|
||||
[this, &found_latch](BluetoothDevice& device) {
|
||||
NEARBY_LOG(INFO, "Device discovered: %s", device.GetName().c_str());
|
||||
EXPECT_EQ(device.GetName(), adapter_b_->GetName());
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
adapter_b_->SetScanMode(BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_EQ(adapter_b_->GetScanMode(),
|
||||
BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
std::string service_name{"service"};
|
||||
std::string service_uuid("service-uuid");
|
||||
BluetoothServerSocket server_socket =
|
||||
bt_b_->ListenForService(service_name, service_uuid);
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
server_socket.Close();
|
||||
}
|
||||
|
||||
TEST_F(BluetoothClassicMediumTest, CanConnectToService) {
|
||||
adapter_a_->SetScanMode(BluetoothAdapter::ScanMode::kConnectable);
|
||||
CountDownLatch found_latch(1);
|
||||
BluetoothDevice* discovered_device = nullptr;
|
||||
bt_a_->StartDiscovery(DiscoveryCallback{
|
||||
.device_discovered_cb =
|
||||
[this, &found_latch, &discovered_device](BluetoothDevice& device) {
|
||||
NEARBY_LOG(INFO, "Device discovered: %s", device.GetName().c_str());
|
||||
EXPECT_EQ(device.GetName(), adapter_b_->GetName());
|
||||
discovered_device = &device;
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
adapter_b_->SetScanMode(BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_EQ(adapter_b_->GetScanMode(),
|
||||
BluetoothAdapter::ScanMode::kConnectableDiscoverable);
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
std::string service_name{"service"};
|
||||
std::string service_uuid("service-uuid");
|
||||
BluetoothServerSocket server_socket =
|
||||
bt_b_->ListenForService(service_name, service_uuid);
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
BluetoothSocket socket_a;
|
||||
BluetoothSocket socket_b;
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
{
|
||||
SingleThreadExecutor server_executor;
|
||||
SingleThreadExecutor client_executor;
|
||||
client_executor.Execute(
|
||||
[this, &socket_a, discovered_device, &service_uuid, &server_socket]() {
|
||||
socket_a = bt_a_->ConnectToService(*discovered_device, service_uuid);
|
||||
if (!socket_a.IsValid()) server_socket.Close();
|
||||
});
|
||||
server_executor.Execute(
|
||||
[&socket_b, &server_socket]() {
|
||||
socket_b = server_socket.Accept();
|
||||
if (!socket_b.IsValid()) server_socket.Close();
|
||||
});
|
||||
}
|
||||
EXPECT_TRUE(socket_a.IsValid());
|
||||
EXPECT_TRUE(socket_b.IsValid());
|
||||
server_socket.Close();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
Reference in New Issue
Block a user