From 6624dd003ea6d6fc5f98ed8199274323fac2e240 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Fri, 7 Jul 2023 13:39:14 -0700 Subject: [PATCH] Implement NearbyDevice::ToProto() for PresenceDevice PiperOrigin-RevId: 546372521 --- presence/BUILD | 3 ++ presence/presence_device.cc | 87 +++++++++++++++++++++++++++++++- presence/presence_device.h | 5 +- presence/presence_device_test.cc | 27 +++++++++- 4 files changed, 116 insertions(+), 6 deletions(-) diff --git a/presence/BUILD b/presence/BUILD index 4b1104a9..6c49a01f 100644 --- a/presence/BUILD +++ b/presence/BUILD @@ -86,6 +86,7 @@ cc_library( "scan_request_builder.h", ], deps = [ + "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/interop:device", "//internal/platform:base", "//internal/platform:connection_info", @@ -118,9 +119,11 @@ cc_test( shard_count = 6, deps = [ ":types", + "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/platform:connection_info", "//internal/platform:logging", "//internal/proto:credential_cc_proto", + "//internal/proto:metadata_cc_proto", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/strings", "@com_google_absl//absl/types:variant", diff --git a/presence/presence_device.cc b/presence/presence_device.cc index da14c17e..175cf382 100644 --- a/presence/presence_device.cc +++ b/presence/presence_device.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -15,14 +15,16 @@ #include "presence/presence_device.h" #include +#include #include +#include "connections/implementation/proto/offline_wire_formats.pb.h" #include "internal/interop/device.h" #include "internal/platform/ble_connection_info.h" -#include "internal/platform/implementation/crypto.h" #include "internal/platform/implementation/system_clock.h" #include "internal/platform/prng.h" #include "internal/proto/credential.pb.h" +#include "internal/proto/metadata.pb.h" #include "presence/device_motion.h" namespace nearby { @@ -34,6 +36,15 @@ constexpr char kEndpointIdChars[] = { 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; +// LINT.IfChange +constexpr int kAndroidIdentityTypeUnknown = -1; +constexpr int kAndroidIdentityTypePrivate = 0; +constexpr int kAndroidIdentityTypeTrusted = 1; +constexpr int kAndroidIdentityTypePublic = 2; +// LINT.ThenChange( +// //depot/google3/java/com/google/android/gmscore/integ/client/nearby/src/com/google/android/gms/nearby/presence/PresenceIdentity.java +// ) + std::string GenerateRandomEndpointId() { std::string result(kEndpointIdLength, 0); nearby::Prng prng; @@ -42,6 +53,42 @@ std::string GenerateRandomEndpointId() { } return result; } + +location::nearby::connections::PresenceDevice::DeviceType +ConvertToConnectionsDeviceType(internal::DeviceType device_type) { + switch (device_type) { + case internal::DEVICE_TYPE_FOLDABLE: + case internal::DEVICE_TYPE_PHONE: + return location::nearby::connections::PresenceDevice::PHONE; + case internal::DEVICE_TYPE_TABLET: + return location::nearby::connections::PresenceDevice::TABLET; + case internal::DEVICE_TYPE_DISPLAY: + return location::nearby::connections::PresenceDevice::DISPLAY; + case internal::DEVICE_TYPE_CHROMEOS: + case internal::DEVICE_TYPE_LAPTOP: + return location::nearby::connections::PresenceDevice::LAPTOP; + case internal::DEVICE_TYPE_TV: + return location::nearby::connections::PresenceDevice::TV; + case internal::DEVICE_TYPE_WATCH: + return location::nearby::connections::PresenceDevice::WATCH; + default: + return location::nearby::connections::PresenceDevice::UNKNOWN; + } +} + +int ConvertToAndroidIdentityType(nearby::internal::IdentityType identity_type) { + switch (identity_type) { + case internal::IDENTITY_TYPE_PRIVATE: + return kAndroidIdentityTypePrivate; + case internal::IDENTITY_TYPE_TRUSTED: + return kAndroidIdentityTypeTrusted; + case internal::IDENTITY_TYPE_PUBLIC: + return kAndroidIdentityTypePublic; + default: + // Unknown identity. + return kAndroidIdentityTypeUnknown; + } +} } // namespace PresenceDevice::PresenceDevice(Metadata metadata) noexcept @@ -79,5 +126,41 @@ std::vector PresenceDevice::GetConnectionInfos() /*gatt_characteristic=*/"", /*psm=*/"", transformed_actions)}; } + +std::string PresenceDevice::ToProtoBytes() const { + location::nearby::connections::PresenceDevice device; + device.set_endpoint_id(endpoint_id_); + device.add_identity_type(ConvertToAndroidIdentityType(identity_type_)); + device.set_endpoint_type( + location::nearby::connections::EndpointType::PRESENCE_ENDPOINT); + auto* actions = device.mutable_actions(); + for (const auto& action : actions_) { + actions->Add(action.GetActionIdentifier()); + } + std::string connection_infos = ""; + for (const auto& connection_info : GetConnectionInfos()) { + if (std::holds_alternative(connection_info)) { + continue; + } + if (std::holds_alternative(connection_info)) { + connection_infos += + std::get(connection_info).ToDataElementBytes(); + } + if (std::holds_alternative(connection_info)) { + connection_infos += + std::get(connection_info).ToDataElementBytes(); + } + if (std::holds_alternative(connection_info)) { + connection_infos += std::get(connection_info) + .ToDataElementBytes(); + } + } + device.set_device_type( + ConvertToConnectionsDeviceType(metadata_.device_type())); + device.set_device_name(metadata_.device_name()); + device.set_connectivity_info_list(connection_infos); + device.set_device_image_url(metadata_.device_profile_url()); + return device.SerializeAsString(); +} } // namespace presence } // namespace nearby diff --git a/presence/presence_device.h b/presence/presence_device.h index e46efaa5..962de1e8 100644 --- a/presence/presence_device.h +++ b/presence/presence_device.h @@ -29,7 +29,7 @@ namespace nearby { namespace presence { -constexpr int kEndpointIdLength = 4; +inline constexpr int kEndpointIdLength = 4; class PresenceDevice : public nearby::NearbyDevice { using Metadata = ::nearby::internal::Metadata; @@ -60,8 +60,7 @@ class PresenceDevice : public nearby::NearbyDevice { // Add more medium ConnectionInfos as we introduce them. std::vector GetConnectionInfos() const override; - // TODO(b/289368652): Implement. - std::string ToProtoBytes() const override { return ""; } + std::string ToProtoBytes() const override; DeviceMotion GetDeviceMotion() const { return device_motion_; } Metadata GetMetadata() const { return metadata_; } void SetMetadata(const Metadata metadata) { metadata_ = metadata; } diff --git a/presence/presence_device_test.cc b/presence/presence_device_test.cc index e8a422bb..6ea57a81 100644 --- a/presence/presence_device_test.cc +++ b/presence/presence_device_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -14,12 +14,17 @@ #include "presence/presence_device.h" +#include + #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "absl/types/variant.h" +#include "connections/implementation/proto/offline_wire_formats.pb.h" +#include "connections/implementation/proto/offline_wire_formats.proto.h" #include "internal/platform/ble_connection_info.h" #include "internal/proto/credential.pb.h" +#include "internal/proto/metadata.pb.h" #include "presence/data_element.h" #include "presence/presence_action.h" @@ -28,6 +33,7 @@ namespace presence { namespace { using ::nearby::internal::Metadata; +using ::testing::Contains; constexpr DeviceMotion::MotionType kDefaultMotionType = DeviceMotion::MotionType::kPointAndHold; @@ -44,6 +50,7 @@ Metadata CreateTestMetadata() { metadata.set_device_name("NP test device"); metadata.set_device_profile_url("test_image.test.com"); metadata.set_bluetooth_mac_address(kMacAddr); + metadata.set_device_type(internal::DEVICE_TYPE_LAPTOP); return metadata; } @@ -134,6 +141,24 @@ TEST(PresenceDeviceTest, TestGetIdentityType) { EXPECT_EQ(device.GetIdentityType(), internal::IDENTITY_TYPE_PUBLIC); } +TEST(PresenceDeviceTest, TestToProtoBytes) { + Metadata metadata = CreateTestMetadata(); + PresenceDevice device = + PresenceDevice(DeviceMotion(), metadata, internal::IDENTITY_TYPE_PUBLIC); + std::string proto_bytes = device.ToProtoBytes(); + location::nearby::connections::PresenceDevice device_frame; + ASSERT_TRUE(device_frame.ParseFromString(proto_bytes)); + // Public identity. + EXPECT_THAT(device_frame.identity_type(), Contains(2)); + EXPECT_EQ(device_frame.endpoint_type(), + location::nearby::connections::PRESENCE_ENDPOINT); + EXPECT_EQ(device_frame.endpoint_id(), device.GetEndpointId()); + EXPECT_EQ(device_frame.device_type(), + location::nearby::connections::PresenceDevice::LAPTOP); + EXPECT_EQ(device_frame.device_name(), "NP test device"); + EXPECT_EQ(device_frame.device_image_url(), "test_image.test.com"); +} + } // namespace } // namespace presence } // namespace nearby