Commit 449dc8e0 authored by Ryan Hansberry's avatar Ryan Hansberry Committed by Commit Bot

[Nearby] Create concrete BlePeripheral and skeleton BleMedium.

Subsequent CLs will use Nearby's bluetooth::mojom::Adapter to
scan for and register BLE advertisements.

Note that incoming and outgoing BLE GATT connections are not
supported by Nearby Chrome at the moment and the respective
methods on BleMedium are left as no-ops.

See go/nearby-chrome-bt for more details.

Bug: b:154845685
Change-Id: I31537b957129a7cac6272b5f9eed3157a2e6dfa6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2364032Reviewed-by: default avatarJames Vecore <vecore@google.com>
Commit-Queue: Ryan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#801134}
parent b6d8edac
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/services/sharing/nearby/nearby_connections.h" #include "chrome/services/sharing/nearby/nearby_connections.h"
#include "chrome/services/sharing/nearby/platform_v2/atomic_boolean.h" #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/atomic_uint32.h"
#include "chrome/services/sharing/nearby/platform_v2/ble_medium.h"
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_adapter.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/bluetooth_classic_medium.h"
#include "chrome/services/sharing/nearby/platform_v2/condition_variable.h" #include "chrome/services/sharing/nearby/platform_v2/condition_variable.h"
...@@ -136,11 +137,13 @@ ImplementationPlatform::CreateBluetoothClassicMedium( ...@@ -136,11 +137,13 @@ ImplementationPlatform::CreateBluetoothClassicMedium(
std::unique_ptr<BleMedium> ImplementationPlatform::CreateBleMedium( std::unique_ptr<BleMedium> ImplementationPlatform::CreateBleMedium(
api::BluetoothAdapter& adapter) { api::BluetoothAdapter& adapter) {
return nullptr; // TODO (hansberry): Inject bluetooth::mojom::Adapter into BleMedium.
return std::make_unique<chrome::BleMedium>();
} }
std::unique_ptr<ble_v2::BleMedium> ImplementationPlatform::CreateBleV2Medium( std::unique_ptr<ble_v2::BleMedium> ImplementationPlatform::CreateBleV2Medium(
api::BluetoothAdapter& adapter) { api::BluetoothAdapter& adapter) {
// Do nothing. ble_v2::BleMedium is not yet supported in Chrome Nearby.
return nullptr; return nullptr;
} }
......
...@@ -8,6 +8,10 @@ source_set("platform_v2") { ...@@ -8,6 +8,10 @@ source_set("platform_v2") {
"atomic_boolean.h", "atomic_boolean.h",
"atomic_uint32.cc", "atomic_uint32.cc",
"atomic_uint32.h", "atomic_uint32.h",
"ble_medium.cc",
"ble_medium.h",
"ble_peripheral.cc",
"ble_peripheral.h",
"bluetooth_adapter.cc", "bluetooth_adapter.cc",
"bluetooth_adapter.h", "bluetooth_adapter.h",
"bluetooth_classic_medium.cc", "bluetooth_classic_medium.cc",
...@@ -63,6 +67,7 @@ source_set("unit_tests") { ...@@ -63,6 +67,7 @@ source_set("unit_tests") {
sources = [ sources = [
"atomic_boolean_unittest.cc", "atomic_boolean_unittest.cc",
"atomic_uint32_unittest.cc", "atomic_uint32_unittest.cc",
"ble_medium_unittest.cc",
"bluetooth_adapter_unittest.cc", "bluetooth_adapter_unittest.cc",
"bluetooth_classic_medium_unittest.cc", "bluetooth_classic_medium_unittest.cc",
"bluetooth_server_socket_unittest.cc", "bluetooth_server_socket_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/ble_medium.h"
namespace location {
namespace nearby {
namespace chrome {
BleMedium::BleMedium() = default;
BleMedium::~BleMedium() = default;
bool BleMedium::StartAdvertising(absl::string_view service_id,
const ByteArray& advertisement) {
// TODO(b/154845685): Implement this method.
NOTIMPLEMENTED();
return false;
}
void BleMedium::StopAdvertising(absl::string_view service_id) {
// TODO(b/154845685): Implement this method.
NOTIMPLEMENTED();
}
bool BleMedium::StartScanning(
absl::string_view service_id,
const api::BleMedium::DiscoveredPeripheralCallback&
discovered_peripheral_callback) {
// TODO(b/154848193): Implement this method.
NOTIMPLEMENTED();
return true;
}
void BleMedium::StopScanning(absl::string_view service_id) {
// TODO(b/154848193): Implement this method.
NOTIMPLEMENTED();
}
bool BleMedium::StartAcceptingConnections(
absl::string_view service_id,
const api::BleMedium::AcceptedConnectionCallback&
accepted_connection_callback) {
// Do not actually start a GATT server, because BLE connections are not yet
// supported in Chrome Nearby. However, return true in order to allow
// BLE advertising to continue.
// TODO(hansberry): Verify if this is still required in NCv2.
return true;
}
void BleMedium::StopAcceptingConnections(const std::string& service_id) {
// Do nothing. BLE connections are not yet supported in Chrome Nearby.
}
std::unique_ptr<api::BleSocket> BleMedium::Connect(
api::BlePeripheral* ble_peripheral,
absl::string_view service_id) {
// Do nothing. BLE connections are not yet supported in Chrome Nearby.
return nullptr;
}
} // 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_BLE_MEDIUM_H_
#define CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLE_MEDIUM_H_
#include <string>
#include "device/bluetooth/public/mojom/adapter.mojom.h"
#include "third_party/nearby/src/cpp/platform_v2/api/ble.h"
namespace location {
namespace nearby {
namespace chrome {
// Concrete BleMedium implementation.
class BleMedium : public api::BleMedium {
public:
BleMedium();
~BleMedium() override;
BleMedium(const BleMedium&) = delete;
BleMedium& operator=(const BleMedium&) = delete;
// api::BleMedium:
bool StartAdvertising(absl::string_view service_id,
const ByteArray& advertisement) override;
void StopAdvertising(absl::string_view service_id) override;
bool StartScanning(absl::string_view service_id,
const DiscoveredPeripheralCallback&
discovered_peripheral_callback) override;
void StopScanning(absl::string_view service_id) override;
bool StartAcceptingConnections(
absl::string_view service_id,
const AcceptedConnectionCallback& accepted_connection_callback) override;
void StopAcceptingConnections(const std::string& service_id) override;
std::unique_ptr<api::BleSocket> Connect(
api::BlePeripheral* ble_peripheral,
absl::string_view service_id) override;
};
} // namespace chrome
} // namespace nearby
} // namespace location
#endif // CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLE_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/ble_medium.h"
#include <memory>
#include "base/test/task_environment.h"
#include "chrome/services/sharing/nearby/platform_v2/ble_peripheral.h"
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_device.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace location {
namespace nearby {
namespace chrome {
namespace {
const char kServiceName[] = "NearbySharing";
class FakeAcceptedConnectionCallback
: public BleMedium::AcceptedConnectionCallback {
public:
~FakeAcceptedConnectionCallback() override = default;
// BleMedium::AcceptedConnectionCallback:
void OnConnectionAccepted(std::unique_ptr<api::BleSocket> socket,
absl::string_view service_id) override {}
};
} // namespace
class BleMediumTest : public testing::Test {
public:
BleMediumTest() = default;
~BleMediumTest() override = default;
BleMediumTest(const BleMediumTest&) = delete;
BleMediumTest& operator=(const BleMediumTest&) = delete;
void SetUp() override { ble_medium_ = std::make_unique<BleMedium>(); }
protected:
std::unique_ptr<BleMedium> ble_medium_;
FakeAcceptedConnectionCallback fake_accepted_connection_callback_;
base::test::TaskEnvironment task_environment_;
};
TEST_F(BleMediumTest, TestAdvertising) {
// TODO(b/154845685): Write test.
}
TEST_F(BleMediumTest, TestScanning) {
// TODO(b/154848193): Write test.
}
TEST_F(BleMediumTest, TestStartAcceptingConnections) {
// StartAcceptingConnections() should do nothing but still return true.
EXPECT_TRUE(ble_medium_->StartAcceptingConnections(
kServiceName, fake_accepted_connection_callback_));
}
TEST_F(BleMediumTest, TestConnect) {
BluetoothDevice bluetooth_device(bluetooth::mojom::DeviceInfo::New());
BlePeripheral ble_peripheral(bluetooth_device);
// Connect() should do nothing and not return a valid api::BleSocket.
EXPECT_FALSE(ble_medium_->Connect(&ble_peripheral, kServiceName));
}
} // 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/ble_peripheral.h"
namespace location {
namespace nearby {
namespace chrome {
BlePeripheral::BlePeripheral(api::BluetoothDevice& bluetooth_device)
: bluetooth_device_(bluetooth_device) {}
BlePeripheral::~BlePeripheral() = default;
api::BluetoothDevice& BlePeripheral::GetBluetoothDevice() {
return bluetooth_device_;
}
} // 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_BLE_PERIPHERAL_H_
#define CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLE_PERIPHERAL_H_
#include "third_party/nearby/src/cpp/platform_v2/api/ble.h"
namespace location {
namespace nearby {
namespace chrome {
// Concrete BlePeripheral implementation.
class BlePeripheral : public api::BlePeripheral {
public:
explicit BlePeripheral(api::BluetoothDevice& bluetooth_device);
~BlePeripheral() override;
BlePeripheral(const BlePeripheral&) = delete;
BlePeripheral& operator=(const BlePeripheral&) = delete;
// api::BlePeripheral:
api::BluetoothDevice& GetBluetoothDevice() override;
private:
api::BluetoothDevice& bluetooth_device_;
};
} // namespace chrome
} // namespace nearby
} // namespace location
#endif // CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_BLE_PERIPHERAL_H_
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