Implement NearbyDevice::ToProto() for Connections device

PiperOrigin-RevId: 546376155
This commit is contained in:
Anay Wadhera
2023-07-07 13:55:31 -07:00
committed by Copybara-Service
parent 6624dd003e
commit 507935f506
4 changed files with 85 additions and 4 deletions
+7 -2
View File
@@ -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",
+63
View File
@@ -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 <string>
#include <variant>
#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<absl::monostate>(connection_info)) {
continue;
}
if (std::holds_alternative<BleConnectionInfo>(connection_info)) {
absl::StrAppend(
&connection_info_string,
std::get<BleConnectionInfo>(connection_info).ToDataElementBytes());
}
if (std::holds_alternative<WifiLanConnectionInfo>(connection_info)) {
absl::StrAppend(&connection_info_string,
std::get<WifiLanConnectionInfo>(connection_info)
.ToDataElementBytes());
}
if (std::holds_alternative<BluetoothConnectionInfo>(connection_info)) {
absl::StrAppend(&connection_info_string,
std::get<BluetoothConnectionInfo>(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
+1 -2
View File
@@ -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() {
+14
View File
@@ -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