Commit f1d5879f authored by Ryan Hansberry's avatar Ryan Hansberry Committed by Commit Bot

[Nearby] Create concrete BluetoothClassicMedium and BluetoothDevice.

Partially implement a concrete
location::nearby::api::BluetoothClassicMedium, using the shared
bluetooth::mojom::Adapter reference in Chrome's Nearby Connections.

Also fully implement a concrete location::nearby::api::BluetoothDevice,
constructed using the device info returned during device discovery in
BluetoothClassicMedium.

Only device discovery methods are implemented in this CL. Subsequent
CLs will implement methods to create outgoing, or listen for incoming,
BluetoothSocket connections.

api::BluetoothClassicMedium is a synchronous interface, so this CL
converts the following bluetooth::mojom::Adapter methods it consumes
to [Sync]: SetClient() and StartDiscoverySession().

Please see design doc go/nearby-chrome-bt for more details.

Bug: b:154849033, b:158848873
Change-Id: If603fb716421fdabd756f3ff34b58a69f64f6352
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2314024Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Commit-Queue: Ryan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796557}
parent e63f403b
......@@ -10,6 +10,10 @@ source_set("platform_v2") {
"atomic_uint32.h",
"bluetooth_adapter.cc",
"bluetooth_adapter.h",
"bluetooth_classic_medium.cc",
"bluetooth_classic_medium.h",
"bluetooth_device.cc",
"bluetooth_device.h",
"condition_variable.cc",
"condition_variable.h",
"count_down_latch.cc",
......@@ -54,6 +58,7 @@ source_set("unit_tests") {
"atomic_boolean_unittest.cc",
"atomic_uint32_unittest.cc",
"bluetooth_adapter_unittest.cc",
"bluetooth_classic_medium_unittest.cc",
"condition_variable_unittest.cc",
"count_down_latch_unittest.cc",
"crypto_unittest.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_classic_medium.h"
namespace location {
namespace nearby {
namespace chrome {
BluetoothClassicMedium::BluetoothClassicMedium(
bluetooth::mojom::Adapter* adapter)
: adapter_(adapter) {}
BluetoothClassicMedium::~BluetoothClassicMedium() = default;
bool BluetoothClassicMedium::StartDiscovery(
DiscoveryCallback discovery_callback) {
if (adapter_client_.is_bound() && discovery_callback_ &&
discovery_session_.is_bound()) {
return true;
}
// TODO(hansberry): Verify with Nearby team if this is correct behavior.
discovered_bluetooth_devices_map_.clear();
bool success =
adapter_->SetClient(adapter_client_.BindNewPipeAndPassRemote());
if (!success) {
adapter_client_.reset();
return false;
}
mojo::PendingRemote<bluetooth::mojom::DiscoverySession> discovery_session;
success = adapter_->StartDiscoverySession(&discovery_session);
if (!success || !discovery_session.is_valid()) {
adapter_client_.reset();
return false;
}
discovery_session_.Bind(std::move(discovery_session));
discovery_session_.set_disconnect_handler(
base::BindOnce(&BluetoothClassicMedium::DiscoveringChanged,
base::Unretained(this), /*discovering=*/false));
discovery_callback_ = std::move(discovery_callback);
return true;
}
bool BluetoothClassicMedium::StopDiscovery() {
// TODO(hansberry): Verify with Nearby team if this is correct behavior:
// Do not clear |discovered_bluetooth_devices_map_| because the caller still
// needs references to BluetoothDevices to remain valid.
bool stop_discovery_success = true;
if (discovery_session_) {
bool message_success = discovery_session_->Stop(&stop_discovery_success);
stop_discovery_success = stop_discovery_success && message_success;
}
adapter_client_.reset();
discovery_callback_.reset();
discovery_session_.reset();
return stop_discovery_success;
}
std::unique_ptr<api::BluetoothSocket> BluetoothClassicMedium::ConnectToService(
api::BluetoothDevice& remote_device,
const std::string& service_uuid) {
// TODO(b/154849933): Implement this in a subsequent CL.
NOTIMPLEMENTED();
return nullptr;
}
std::unique_ptr<api::BluetoothServerSocket>
BluetoothClassicMedium::ListenForService(const std::string& service_name,
const std::string& service_uuid) {
// TODO(b/154849933): Implement this in a subsequent CL.
NOTIMPLEMENTED();
return nullptr;
}
void BluetoothClassicMedium::PresentChanged(bool present) {
// TODO(hansberry): It is unclear to me how the API implementation can signal
// to Core that |present| has become unexpectedly false. Need to ask
// Nearby team.
if (!present)
StopDiscovery();
}
void BluetoothClassicMedium::PoweredChanged(bool powered) {
// TODO(hansberry): It is unclear to me how the API implementation can signal
// to Core that |powered| has become unexpectedly false. Need to ask
// Nearby team.
if (!powered)
StopDiscovery();
}
void BluetoothClassicMedium::DiscoverableChanged(bool discoverable) {
// Do nothing. BluetoothClassicMedium is not responsible for managing
// discoverable state.
}
void BluetoothClassicMedium::DiscoveringChanged(bool discovering) {
// TODO(hansberry): It is unclear to me how the API implementation can signal
// to Core that |discovering| has become unexpectedly false. Need to ask
// Nearby team.
if (!discovering)
StopDiscovery();
}
void BluetoothClassicMedium::DeviceAdded(
bluetooth::mojom::DeviceInfoPtr device) {
if (!adapter_client_.is_bound() || !discovery_callback_ ||
!discovery_session_.is_bound()) {
return;
}
const std::string& address = device->address;
if (base::Contains(discovered_bluetooth_devices_map_, address)) {
auto& bluetooth_device = discovered_bluetooth_devices_map_.at(address);
bluetooth_device.UpdateDeviceInfo(std::move(device));
discovery_callback_->device_name_changed_cb(bluetooth_device);
} else {
discovered_bluetooth_devices_map_.emplace(address, std::move(device));
discovery_callback_->device_discovered_cb(
discovered_bluetooth_devices_map_.at(address));
}
}
void BluetoothClassicMedium::DeviceChanged(
bluetooth::mojom::DeviceInfoPtr device) {
DeviceAdded(std::move(device));
}
void BluetoothClassicMedium::DeviceRemoved(
bluetooth::mojom::DeviceInfoPtr device) {
if (!adapter_client_.is_bound() || !discovery_callback_ ||
!discovery_session_.is_bound()) {
return;
}
const std::string& address = device->address;
if (!base::Contains(discovered_bluetooth_devices_map_, address))
return;
discovery_callback_->device_lost_cb(
discovered_bluetooth_devices_map_.at(address));
discovered_bluetooth_devices_map_.erase(address);
}
} // namespace chrome
} // namespace nearby
} // namespace location
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLUETOOTH_CLASSIC_MEDIUM_H_
#define CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLUETOOTH_CLASSIC_MEDIUM_H_
#include <memory>
#include <string>
#include "base/optional.h"
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_device.h"
#include "device/bluetooth/public/mojom/adapter.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/nearby/src/cpp/platform_v2/api/bluetooth_classic.h"
namespace location {
namespace nearby {
namespace chrome {
// Concrete BluetoothClassicMedium implementation.
// api::BluetoothClassicMedium is a synchronous interface, so this
// implementation consumes the synchronous signatures of
// bluetooth::mojom::Adapter methods.
class BluetoothClassicMedium : public api::BluetoothClassicMedium,
public bluetooth::mojom::AdapterClient {
public:
explicit BluetoothClassicMedium(bluetooth::mojom::Adapter* adapter);
~BluetoothClassicMedium() override;
BluetoothClassicMedium(const BluetoothClassicMedium&) = delete;
BluetoothClassicMedium& operator=(const BluetoothClassicMedium&) = delete;
// api::BluetoothClassicMedium:
bool StartDiscovery(DiscoveryCallback discovery_callback) override;
bool StopDiscovery() override;
std::unique_ptr<api::BluetoothSocket> ConnectToService(
api::BluetoothDevice& remote_device,
const std::string& service_uuid) override;
std::unique_ptr<api::BluetoothServerSocket> ListenForService(
const std::string& service_name,
const std::string& service_uuid) override;
private:
// bluetooth::mojom::AdapterClient:
void PresentChanged(bool present) override;
void PoweredChanged(bool powered) override;
void DiscoverableChanged(bool discoverable) override;
void DiscoveringChanged(bool discovering) override;
void DeviceAdded(bluetooth::mojom::DeviceInfoPtr device) override;
void DeviceChanged(bluetooth::mojom::DeviceInfoPtr device) override;
void DeviceRemoved(bluetooth::mojom::DeviceInfoPtr device) override;
// This reference is owned by the top-level Nearby Connections interface and
// will always outlive this object.
bluetooth::mojom::Adapter* adapter_ = nullptr;
// |adapter_client_| is only set and bound during active discovery so that
// events we don't care about outside of discovery don't pile up.
mojo::Receiver<bluetooth::mojom::AdapterClient> adapter_client_{this};
// These properties are only set while discovery is active.
base::Optional<DiscoveryCallback> discovery_callback_;
mojo::Remote<bluetooth::mojom::DiscoverySession> discovery_session_;
std::map<std::string, chrome::BluetoothDevice>
discovered_bluetooth_devices_map_;
};
} // namespace chrome
} // namespace nearby
} // namespace location
#endif // CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLUETOOTH_CLASSIC_MEDIUM_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_classic_medium.h"
#include <memory>
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/test/task_environment.h"
#include "chrome/services/sharing/nearby/test_support/fake_adapter.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/bindings/shared_remote.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace location {
namespace nearby {
namespace chrome {
namespace {
const char kDeviceAddress1[] = "DeviceAddress1";
const char kDeviceAddress2[] = "DeviceAddress2";
const char kDeviceName1[] = "DeviceName1";
const char kDeviceName2[] = "DeviceName2";
} // namespace
class BluetoothClassicMediumTest : public testing::Test {
public:
BluetoothClassicMediumTest() = default;
~BluetoothClassicMediumTest() override = default;
BluetoothClassicMediumTest(const BluetoothClassicMediumTest&) = delete;
BluetoothClassicMediumTest& operator=(const BluetoothClassicMediumTest&) =
delete;
void SetUp() override {
auto fake_adapter = std::make_unique<bluetooth::FakeAdapter>();
fake_adapter_ = fake_adapter.get();
mojo::PendingRemote<bluetooth::mojom::Adapter> pending_adapter;
mojo::MakeSelfOwnedReceiver(
std::move(fake_adapter),
pending_adapter.InitWithNewPipeAndPassReceiver());
remote_adapter_.Bind(std::move(pending_adapter),
/*bind_task_runner=*/nullptr);
bluetooth_classic_medium_ =
std::make_unique<BluetoothClassicMedium>(remote_adapter_.get());
discovery_callback_ = {
.device_discovered_cb =
[this](api::BluetoothDevice& device) {
last_device_discovered_ = &device;
std::move(on_device_discovered_callback_).Run();
},
.device_name_changed_cb =
[this](api::BluetoothDevice& device) {
last_device_name_changed_ = &device;
std::move(on_device_name_changed_callback_).Run();
},
.device_lost_cb =
[this](api::BluetoothDevice& device) {
DCHECK_EQ(expected_last_device_lost_, &device);
std::move(on_device_lost_callback_).Run();
}};
}
protected:
void StartDiscovery() {
EXPECT_FALSE(fake_adapter_->IsDiscoverySessionActive());
EXPECT_TRUE(bluetooth_classic_medium_->StartDiscovery(discovery_callback_));
EXPECT_TRUE(fake_adapter_->IsDiscoverySessionActive());
}
void StopDiscovery() {
base::RunLoop run_loop;
fake_adapter_->SetDiscoverySessionDestroyedCallback(run_loop.QuitClosure());
EXPECT_TRUE(bluetooth_classic_medium_->StopDiscovery());
run_loop.Run();
EXPECT_FALSE(fake_adapter_->IsDiscoverySessionActive());
}
void NotifyDeviceAdded(const std::string& address, const std::string& name) {
base::RunLoop run_loop;
on_device_discovered_callback_ = run_loop.QuitClosure();
fake_adapter_->NotifyDeviceAdded(CreateDeviceInfo(address, name));
run_loop.Run();
}
void NotifyDeviceChanged(const std::string& address,
const std::string& name) {
base::RunLoop run_loop;
on_device_name_changed_callback_ = run_loop.QuitClosure();
fake_adapter_->NotifyDeviceChanged(CreateDeviceInfo(address, name));
run_loop.Run();
}
void NotifyDeviceRemoved(const std::string& address,
const std::string& name) {
base::RunLoop run_loop;
on_device_lost_callback_ = run_loop.QuitClosure();
fake_adapter_->NotifyDeviceRemoved(CreateDeviceInfo(address, name));
run_loop.Run();
}
bluetooth::FakeAdapter* fake_adapter_;
mojo::SharedRemote<bluetooth::mojom::Adapter> remote_adapter_;
std::unique_ptr<BluetoothClassicMedium> bluetooth_classic_medium_;
BluetoothClassicMedium::DiscoveryCallback discovery_callback_;
api::BluetoothDevice* last_device_discovered_ = nullptr;
api::BluetoothDevice* last_device_name_changed_ = nullptr;
api::BluetoothDevice* expected_last_device_lost_ = nullptr;
base::OnceClosure on_device_discovered_callback_;
base::OnceClosure on_device_name_changed_callback_;
base::OnceClosure on_device_lost_callback_;
private:
bluetooth::mojom::DeviceInfoPtr CreateDeviceInfo(const std::string& address,
const std::string& name) {
auto device_info = bluetooth::mojom::DeviceInfo::New();
device_info->address = address;
device_info->name = name;
device_info->name_for_display = name;
return device_info;
}
base::test::TaskEnvironment task_environment_;
};
TEST_F(BluetoothClassicMediumTest, TestDiscovery_StartDiscoveryIsIdempotent) {
EXPECT_FALSE(fake_adapter_->IsDiscoverySessionActive());
EXPECT_TRUE(bluetooth_classic_medium_->StartDiscovery(discovery_callback_));
EXPECT_TRUE(fake_adapter_->IsDiscoverySessionActive());
EXPECT_TRUE(bluetooth_classic_medium_->StartDiscovery(discovery_callback_));
EXPECT_TRUE(fake_adapter_->IsDiscoverySessionActive());
StopDiscovery();
}
TEST_F(BluetoothClassicMediumTest, TestDiscovery_StartDiscoveryError) {
fake_adapter_->SetShouldDiscoverySucceed(false);
EXPECT_FALSE(fake_adapter_->IsDiscoverySessionActive());
EXPECT_FALSE(bluetooth_classic_medium_->StartDiscovery(discovery_callback_));
EXPECT_FALSE(fake_adapter_->IsDiscoverySessionActive());
}
TEST_F(BluetoothClassicMediumTest, TestDiscovery_DeviceDiscovered) {
StartDiscovery();
NotifyDeviceAdded(kDeviceAddress1, kDeviceName1);
EXPECT_EQ(kDeviceName1, last_device_discovered_->GetName());
auto* first_device_discovered = last_device_discovered_;
NotifyDeviceAdded(kDeviceAddress2, kDeviceName2);
EXPECT_EQ(kDeviceName2, last_device_discovered_->GetName());
EXPECT_NE(first_device_discovered, last_device_discovered_);
StopDiscovery();
}
TEST_F(BluetoothClassicMediumTest, TestDiscovery_DeviceNameChanged) {
StartDiscovery();
NotifyDeviceAdded(kDeviceAddress1, kDeviceName1);
EXPECT_EQ(kDeviceName1, last_device_discovered_->GetName());
NotifyDeviceChanged(kDeviceAddress1, kDeviceName2);
EXPECT_EQ(kDeviceName2, last_device_name_changed_->GetName());
EXPECT_EQ(last_device_name_changed_, last_device_discovered_);
StopDiscovery();
}
TEST_F(BluetoothClassicMediumTest, TestDiscovery_DeviceLost) {
StartDiscovery();
NotifyDeviceAdded(kDeviceAddress1, kDeviceName1);
EXPECT_EQ(kDeviceName1, last_device_discovered_->GetName());
expected_last_device_lost_ = last_device_discovered_;
NotifyDeviceRemoved(kDeviceAddress1, kDeviceName1);
StopDiscovery();
}
} // namespace chrome
} // namespace nearby
} // namespace location
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_device.h"
namespace location {
namespace nearby {
namespace chrome {
BluetoothDevice::BluetoothDevice(bluetooth::mojom::DeviceInfoPtr device_info)
: device_info_(std::move(device_info)) {}
BluetoothDevice::~BluetoothDevice() = default;
std::string BluetoothDevice::GetName() const {
return device_info_->name_for_display;
}
std::string BluetoothDevice::GetAddress() const {
return device_info_->address;
}
void BluetoothDevice::UpdateDeviceInfo(
bluetooth::mojom::DeviceInfoPtr device_info) {
DCHECK_EQ(device_info_->address, device_info->address);
device_info_ = std::move(device_info);
}
} // namespace chrome
} // namespace nearby
} // namespace location
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLUETOOTH_DEVICE_H_
#define CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLUETOOTH_DEVICE_H_
#include <string>
#include "device/bluetooth/public/mojom/adapter.mojom.h"
#include "third_party/nearby/src/cpp/platform_v2/api/bluetooth_classic.h"
namespace location {
namespace nearby {
namespace chrome {
// Concrete BluetoothDevice implementation.
class BluetoothDevice : public api::BluetoothDevice {
public:
explicit BluetoothDevice(bluetooth::mojom::DeviceInfoPtr device_info);
~BluetoothDevice() override;
BluetoothDevice(const BluetoothDevice&) = delete;
BluetoothDevice& operator=(const BluetoothDevice&) = delete;
// api::BluetoothDevice:
std::string GetName() const override;
std::string GetAddress() const;
void UpdateDeviceInfo(bluetooth::mojom::DeviceInfoPtr device_info);
private:
bluetooth::mojom::DeviceInfoPtr device_info_;
};
} // namespace chrome
} // namespace nearby
} // namespace location
#endif // CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLUETOOTH_DEVICE_H_
......@@ -10,6 +10,7 @@
#include "chrome/services/sharing/nearby/platform_v2/atomic_boolean.h"
#include "chrome/services/sharing/nearby/platform_v2/atomic_uint32.h"
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_adapter.h"
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_classic_medium.h"
#include "chrome/services/sharing/nearby/platform_v2/condition_variable.h"
#include "chrome/services/sharing/nearby/platform_v2/count_down_latch.h"
#include "chrome/services/sharing/nearby/platform_v2/log_message.h"
......@@ -122,7 +123,17 @@ std::unique_ptr<LogMessage> ImplementationPlatform::CreateLogMessage(
std::unique_ptr<BluetoothClassicMedium>
ImplementationPlatform::CreateBluetoothClassicMedium(
api::BluetoothAdapter& adapter) {
return nullptr;
// Ignore the provided |adapter| argument. It provides no interface useful
// to implement chrome::BluetoothClassicMedium.
auto& connections = connections::NearbyConnections::GetInstance();
bluetooth::mojom::Adapter* bluetooth_adapter =
connections.GetBluetoothAdapter();
if (!bluetooth_adapter)
return nullptr;
return std::make_unique<chrome::BluetoothClassicMedium>(bluetooth_adapter);
}
std::unique_ptr<BleMedium> ImplementationPlatform::CreateBleMedium(
......
......@@ -4,8 +4,33 @@
#include "chrome/services/sharing/nearby/test_support/fake_adapter.h"
#include <memory>
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace bluetooth {
namespace {
class FakeDiscoverySession : public mojom::DiscoverySession {
public:
explicit FakeDiscoverySession(base::OnceClosure on_destroy_callback)
: on_destroy_callback_(std::move(on_destroy_callback)) {}
~FakeDiscoverySession() override { std::move(on_destroy_callback_).Run(); }
private:
// mojom::FakeDiscoverySession:
void IsActive(IsActiveCallback callback) override {
std::move(callback).Run(true);
}
void Stop(StopCallback callback) override { std::move(callback).Run(true); }
base::OnceClosure on_destroy_callback_;
};
} // namespace
FakeAdapter::FakeAdapter() = default;
FakeAdapter::~FakeAdapter() = default;
......@@ -25,11 +50,64 @@ void FakeAdapter::GetInfo(GetInfoCallback callback) {
std::move(callback).Run(std::move(adapter_info));
}
void FakeAdapter::SetClient(
::mojo::PendingRemote<mojom::AdapterClient> client) {}
void FakeAdapter::SetClient(::mojo::PendingRemote<mojom::AdapterClient> client,
SetClientCallback callback) {
client_.Bind(std::move(client));
std::move(callback).Run();
}
void FakeAdapter::StartDiscoverySession(
StartDiscoverySessionCallback callback) {}
StartDiscoverySessionCallback callback) {
DCHECK(!discovery_session_);
if (!should_discovery_succeed_) {
std::move(callback).Run(std::move(mojo::NullRemote()));
return;
}
auto discovery_session =
std::make_unique<FakeDiscoverySession>(base::BindOnce(
&FakeAdapter::OnDiscoverySessionDestroyed, base::Unretained(this)));
discovery_session_ = discovery_session.get();
mojo::PendingRemote<mojom::DiscoverySession> pending_session;
mojo::MakeSelfOwnedReceiver(std::move(discovery_session),
pending_session.InitWithNewPipeAndPassReceiver());
std::move(callback).Run(std::move(pending_session));
}
void FakeAdapter::SetShouldDiscoverySucceed(bool should_discovery_succeed) {
should_discovery_succeed_ = should_discovery_succeed;
}
void FakeAdapter::SetDiscoverySessionDestroyedCallback(
base::OnceClosure callback) {
on_discovery_session_destroyed_callback_ = std::move(callback);
}
bool FakeAdapter::IsDiscoverySessionActive() {
return discovery_session_;
}
void FakeAdapter::NotifyDeviceAdded(mojom::DeviceInfoPtr device_info) {
client_->DeviceAdded(std::move(device_info));
}
void FakeAdapter::NotifyDeviceChanged(mojom::DeviceInfoPtr device_info) {
client_->DeviceChanged(std::move(device_info));
}
void FakeAdapter::NotifyDeviceRemoved(mojom::DeviceInfoPtr device_info) {
client_->DeviceRemoved(std::move(device_info));
}
void FakeAdapter::OnDiscoverySessionDestroyed() {
DCHECK(discovery_session_);
discovery_session_ = nullptr;
if (on_discovery_session_destroyed_callback_)
std::move(on_discovery_session_destroyed_callback_).Run();
}
void FakeAdapter::ConnectToServiceInsecurely(
const std::string& address,
......
......@@ -7,6 +7,7 @@
#include "device/bluetooth/public/mojom/adapter.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote_set.h"
namespace bluetooth {
......@@ -22,19 +23,36 @@ class FakeAdapter : public mojom::Adapter {
ConnectToDeviceCallback callback) override;
void GetDevices(GetDevicesCallback callback) override;
void GetInfo(GetInfoCallback callback) override;
void SetClient(::mojo::PendingRemote<mojom::AdapterClient> client) override;
void SetClient(::mojo::PendingRemote<mojom::AdapterClient> client,
SetClientCallback callback) override;
void StartDiscoverySession(StartDiscoverySessionCallback callback) override;
void ConnectToServiceInsecurely(
const std::string& address,
const device::BluetoothUUID& service_uuid,
ConnectToServiceInsecurelyCallback callback) override;
void SetShouldDiscoverySucceed(bool should_discovery_succeed);
void SetDiscoverySessionDestroyedCallback(base::OnceClosure callback);
bool IsDiscoverySessionActive();
void NotifyDeviceAdded(mojom::DeviceInfoPtr device_info);
void NotifyDeviceChanged(mojom::DeviceInfoPtr device_info);
void NotifyDeviceRemoved(mojom::DeviceInfoPtr device_info);
mojo::Receiver<mojom::Adapter> adapter_{this};
std::string name_ = "AdapterName";
bool present_ = true;
bool powered_ = true;
bool discoverable_ = false;
bool discovering_ = false;
private:
void OnDiscoverySessionDestroyed();
mojom::DiscoverySession* discovery_session_ = nullptr;
bool should_discovery_succeed_ = true;
base::OnceClosure on_discovery_session_destroyed_callback_;
mojo::Remote<mojom::AdapterClient> client_;
};
} // namespace bluetooth
......
......@@ -78,8 +78,10 @@ void Adapter::GetInfo(GetInfoCallback callback) {
std::move(callback).Run(std::move(adapter_info));
}
void Adapter::SetClient(mojo::PendingRemote<mojom::AdapterClient> client) {
void Adapter::SetClient(mojo::PendingRemote<mojom::AdapterClient> client,
SetClientCallback callback) {
client_.Bind(std::move(client));
std::move(callback).Run();
}
void Adapter::StartDiscoverySession(StartDiscoverySessionCallback callback) {
......
......@@ -35,7 +35,8 @@ class Adapter : public mojom::Adapter,
ConnectToDeviceCallback callback) override;
void GetDevices(GetDevicesCallback callback) override;
void GetInfo(GetInfoCallback callback) override;
void SetClient(mojo::PendingRemote<mojom::AdapterClient> client) override;
void SetClient(mojo::PendingRemote<mojom::AdapterClient> client,
SetClientCallback callback) override;
void StartDiscoverySession(StartDiscoverySessionCallback callback) override;
void ConnectToServiceInsecurely(
const std::string& address,
......
......@@ -48,6 +48,7 @@ interface DiscoverySession {
// whether or not discovery should continue. In this case, a new
// DiscoverySession should be started to make sure that device discovery
// continues.
[Sync]
IsActive() => (bool active);
// Requests this discovery session instance to stop. If this instance is
......@@ -58,6 +59,7 @@ interface DiscoverySession {
// disconnecting the message pipe, so that they can respond to the result.
// Returns true on success. Returns false if this session is inactive or an
// error occurs while stopping the session.
[Sync]
Stop() => (bool success);
};
......@@ -93,10 +95,12 @@ interface Adapter {
GetInfo() => (AdapterInfo info);
// Sets the client that listens for the adapter's events.
SetClient(pending_remote<AdapterClient> client);
[Sync]
SetClient(pending_remote<AdapterClient> client) => ();
// Requests the adapter to start a new discovery session. Returns null if
// session not created successfully.
[Sync]
StartDiscoverySession() => (pending_remote<DiscoverySession>? session);
// Attempts to initiate an insecure outgoing L2CAP or RFCOMM connection to the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment