mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Implement NearbyDevice::ToProto() for PresenceDevice
PiperOrigin-RevId: 546372521
This commit is contained in:
committed by
Copybara-Service
parent
5bab62fbb1
commit
6624dd003e
@@ -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",
|
||||
|
||||
@@ -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 <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#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<nearby::ConnectionInfoVariant> 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<absl::monostate>(connection_info)) {
|
||||
continue;
|
||||
}
|
||||
if (std::holds_alternative<BleConnectionInfo>(connection_info)) {
|
||||
connection_infos +=
|
||||
std::get<BleConnectionInfo>(connection_info).ToDataElementBytes();
|
||||
}
|
||||
if (std::holds_alternative<WifiLanConnectionInfo>(connection_info)) {
|
||||
connection_infos +=
|
||||
std::get<WifiLanConnectionInfo>(connection_info).ToDataElementBytes();
|
||||
}
|
||||
if (std::holds_alternative<BluetoothConnectionInfo>(connection_info)) {
|
||||
connection_infos += std::get<BluetoothConnectionInfo>(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
|
||||
|
||||
@@ -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<nearby::ConnectionInfoVariant> 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; }
|
||||
|
||||
@@ -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 <string>
|
||||
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user