Commit 542dc277 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Add mojom::NearbyConnections::InjectBluetoothEndpoint()

This function invokes the InjectEndpoint() API from the Nearby
Connections library using the Bluetooth medium. It will be used by Phone
Hub to inject a Bluetooth address synced via the DeviceSync v2 protocol.

Bug: 1106937
Change-Id: I6f0cd533d2d0ed87df5d52394a3135b920329de7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2494445
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#824990}
parent 92d11259
......@@ -346,6 +346,19 @@ void NearbyConnections::StopDiscovery(const std::string& service_id,
->StopDiscovery(ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::InjectBluetoothEndpoint(
const std::string& service_id,
const std::vector<uint8_t>& remote_bluetooth_mac_address,
InjectBluetoothEndpointCallback callback) {
OutOfBandConnectionMetadata oob_metadata{
.medium = Medium::BLUETOOTH,
.remote_bluetooth_mac_address =
ByteArrayFromMojom(remote_bluetooth_mac_address)};
GetCore(service_id)
->InjectEndpoint(service_id, oob_metadata,
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::RequestConnection(
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
......
......@@ -101,6 +101,10 @@ class NearbyConnections : public mojom::NearbyConnections {
StartDiscoveryCallback callback) override;
void StopDiscovery(const std::string& service_id,
StopDiscoveryCallback callback) override;
void InjectBluetoothEndpoint(
const std::string& service_id,
const std::vector<uint8_t>& remote_bluetooth_mac_address,
InjectBluetoothEndpointCallback callback) override;
void RequestConnection(
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
......
......@@ -462,12 +462,48 @@ TEST_F(NearbyConnectionsTest, StopDiscovery) {
}
TEST_F(NearbyConnectionsTest, InjectEndpoint) {
const std::vector<uint8_t> bluetooth_mac_address(
std::begin(kBluetoothMacAddress), std::end(kBluetoothMacAddress));
const EndpointData endpoint_data = CreateEndpointData(1);
base::RunLoop discovery_run_loop;
FakeEndpointDiscoveryListener fake_discovery_listener;
StartDiscovery(fake_discovery_listener,
/*is_out_of_band_connection=*/true);
fake_discovery_listener.endpoint_found_cb =
base::BindLambdaForTesting([&](const std::string& endpoint_id,
mojom::DiscoveredEndpointInfoPtr info) {
EXPECT_EQ(endpoint_data.remote_endpoint_id, endpoint_id);
EXPECT_EQ(endpoint_data.remote_endpoint_info, info->endpoint_info);
EXPECT_EQ(kServiceId, info->service_id);
discovery_run_loop.Quit();
});
ClientProxy* client_proxy = StartDiscovery(
fake_discovery_listener, /*is_out_of_band_connection=*/true);
EXPECT_CALL(*service_controller_ptr_, InjectEndpoint)
.WillOnce([&](ClientProxy* client, const std::string& service_id,
const OutOfBandConnectionMetadata& metadata) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(Medium::BLUETOOTH, metadata.medium);
EXPECT_EQ(bluetooth_mac_address,
ByteArrayToMojom(metadata.remote_bluetooth_mac_address));
client_proxy->OnEndpointFound(
kServiceId, endpoint_data.remote_endpoint_id,
ByteArrayFromMojom(endpoint_data.remote_endpoint_info),
/*mediums=*/{});
return Status{Status::kSuccess};
});
base::RunLoop inject_run_loop;
nearby_connections_->InjectBluetoothEndpoint(
kServiceId, bluetooth_mac_address,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
inject_run_loop.Quit();
}));
// TODO(khorimoto): Finish this test when InjectBluetoothEndpoint() Mojo API
// is added.
discovery_run_loop.Run();
inject_run_loop.Run();
}
TEST_F(NearbyConnectionsTest, RequestConnectionInitiated) {
......
......@@ -65,6 +65,12 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
StopDiscovery,
(const std::string& service_id, StopDiscoveryCallback),
(override));
MOCK_METHOD(void,
InjectBluetoothEndpoint,
(const std::string& service_id,
const std::vector<uint8_t>& remote_bluetooth_mac_address,
InjectBluetoothEndpointCallback callback),
(override));
MOCK_METHOD(void,
RequestConnection,
(const std::string& service_id,
......
......@@ -185,6 +185,25 @@ interface NearbyConnections {
// Status::kSuccess returned after discovery got stopped.
StopDiscovery(string service_id) => (Status status);
// Attempts an out-of-band discovery from a previous call to StartDiscovery()
// which included the is_out_of_band_discovery option. If successful, this
// call results in the EndpointDiscoveryListener::OnEndpointFound() callback
// corresponding to the |listener| parameter to StartDiscovery() being
// invoked.
//
// Before invoking this function, StartDiscovery() should have been called
// with DiscoveryOptions that set |is_out_of_band_connection| to true.
// After this call completes, it is safe to invoke StopDiscovery().
//
// service_id - Identifier used in the preceding StartDiscovery() call.
// Possible return values include:
// Status::kSuccess if endpoint injection was attempted.
// Status::kError if |remote_bluetooth_mac_address| is malformed.
// Status::kOutOfOrderApiCall if the app is not discovering.
InjectBluetoothEndpoint(
string service_id,
array<uint8, 6> remote_bluetooth_mac_address) => (Status status);
// Sends a request to connect to a remote endpoint.
//
// service_id - Service ID used to discover the endpoint.
......
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