From 507935f50697a976594a511fefc5f0621502d494 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Fri, 7 Jul 2023 13:54:25 -0700 Subject: [PATCH] Implement NearbyDevice::ToProto() for Connections device PiperOrigin-RevId: 546376155 --- connections/v3/BUILD | 9 +++- connections/v3/connections_device.cc | 63 +++++++++++++++++++++++ connections/v3/connections_device.h | 3 +- connections/v3/connections_device_test.cc | 14 +++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 connections/v3/connections_device.cc diff --git a/connections/v3/BUILD b/connections/v3/BUILD index db0050bc..9cdc47b4 100644 --- a/connections/v3/BUILD +++ b/connections/v3/BUILD @@ -1,5 +1,8 @@ cc_library( name = "v3_types", + srcs = [ + "connections_device.cc", + ], hdrs = [ "bandwidth_info.h", "connection_listening_options.h", @@ -15,11 +18,13 @@ cc_library( ], deps = [ "//connections:core_types", + "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/crypto", "//internal/interop:device", "//internal/platform:connection_info", "//proto:connections_enums_cc_proto", "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/strings", ], ) @@ -30,8 +35,8 @@ cc_test( "connections_device_test.cc", ], deps = [ - "//connections/implementation:internal_test", - "//connections/v3:v3_types", + ":v3_types", + "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/platform:connection_info", "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", diff --git a/connections/v3/connections_device.cc b/connections/v3/connections_device.cc new file mode 100644 index 00000000..c2d3ab67 --- /dev/null +++ b/connections/v3/connections_device.cc @@ -0,0 +1,63 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "connections/v3/connections_device.h" + +#include +#include + +#include "absl/strings/str_cat.h" +#include "connections/implementation/proto/offline_wire_formats.pb.h" +#include "internal/platform/ble_connection_info.h" +#include "internal/platform/bluetooth_connection_info.h" +#include "internal/platform/wifi_lan_connection_info.h" + +namespace nearby { +namespace connections { +namespace v3 { + +std::string ConnectionsDevice::ToProtoBytes() const { + // Bytes holding the connection info data elements. + std::string connection_info_string; + for (const auto& connection_info : connection_infos_) { + if (std::holds_alternative(connection_info)) { + continue; + } + if (std::holds_alternative(connection_info)) { + absl::StrAppend( + &connection_info_string, + std::get(connection_info).ToDataElementBytes()); + } + if (std::holds_alternative(connection_info)) { + absl::StrAppend(&connection_info_string, + std::get(connection_info) + .ToDataElementBytes()); + } + if (std::holds_alternative(connection_info)) { + absl::StrAppend(&connection_info_string, + std::get(connection_info) + .ToDataElementBytes()); + } + } + location::nearby::connections::ConnectionsDevice device_frame; + device_frame.set_endpoint_id(endpoint_id_); + device_frame.set_endpoint_info(endpoint_info_); + device_frame.set_connectivity_info_list(connection_info_string); + device_frame.set_endpoint_type( + location::nearby::connections::CONNECTIONS_ENDPOINT); + return device_frame.SerializeAsString(); +} +} // namespace v3 +} // namespace connections +} // namespace nearby diff --git a/connections/v3/connections_device.h b/connections/v3/connections_device.h index 5f49fa29..1c6158ac 100644 --- a/connections/v3/connections_device.h +++ b/connections/v3/connections_device.h @@ -54,8 +54,7 @@ class ConnectionsDevice : public nearby::NearbyDevice { Type GetType() const override { return Type::kConnectionsDevice; } std::string GetEndpointInfo() const { return endpoint_info_; } - // TODO(b/289368652): Implement. - std::string ToProtoBytes() const override { return ""; } + std::string ToProtoBytes() const override; private: std::string GenerateRandomEndpointId() { diff --git a/connections/v3/connections_device_test.cc b/connections/v3/connections_device_test.cc index b157d54d..3a0b9b3b 100644 --- a/connections/v3/connections_device_test.cc +++ b/connections/v3/connections_device_test.cc @@ -19,6 +19,7 @@ #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "connections/implementation/proto/offline_wire_formats.pb.h" #include "internal/platform/ble_connection_info.h" #include "internal/platform/connection_info.h" #include "internal/platform/wifi_lan_connection_info.h" @@ -64,6 +65,19 @@ TEST(ConnectionsDeviceTest, TestLongEndpointId) { EXPECT_EQ(device.GetEndpointInfo(), "connections endpoint"); } +TEST(ConnectionsDeviceTest, TestToProtoBytes) { + auto connection_infos = CreateDefaultConnectionInfos(); + ConnectionsDevice device("ABCD", "connections endpoint", connection_infos); + auto proto_bytes = device.ToProtoBytes(); + location::nearby::connections::ConnectionsDevice device_frame; + ASSERT_TRUE(device_frame.ParseFromString(proto_bytes)); + EXPECT_EQ(device_frame.endpoint_id(), "ABCD"); + EXPECT_EQ(device_frame.endpoint_info(), "connections endpoint"); + EXPECT_TRUE(device_frame.has_connectivity_info_list()); + EXPECT_EQ(device_frame.endpoint_type(), + location::nearby::connections::CONNECTIONS_ENDPOINT); +} + } // namespace } // namespace v3 } // namespace connections