forked from google/nearby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence_device.h
129 lines (117 loc) · 5.08 KB
/
presence_device.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2020 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.
#ifndef THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_H_
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_H_
#include <optional>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "internal/interop/device.h"
#include "internal/proto/credential.pb.h"
#include "internal/proto/metadata.pb.h"
#include "presence/data_element.h"
#include "presence/device_motion.h"
#include "presence/presence_action.h"
namespace nearby {
namespace presence {
inline constexpr int kEndpointIdLength = 4;
class PresenceDevice : public nearby::NearbyDevice {
using Metadata = ::nearby::internal::Metadata;
using DeviceIdentityMetaData = ::nearby::internal::DeviceIdentityMetaData;
public:
explicit PresenceDevice(absl::string_view endpoint_id) noexcept;
explicit PresenceDevice(
DeviceIdentityMetaData device_identity_metadata) noexcept;
explicit PresenceDevice(
DeviceMotion device_motion,
DeviceIdentityMetaData device_identity_metadata) noexcept;
explicit PresenceDevice(
DeviceMotion device_motion,
DeviceIdentityMetaData device_identity_metadata,
nearby::internal::IdentityType identity_type) noexcept;
std::string GetEndpointId() const override { return endpoint_id_; }
std::vector<nearby::ConnectionInfoVariant> GetConnectionInfos()
const override;
std::string ToProtoBytes() const override;
void AddExtendedProperty(const DataElement& data_element) {
extended_properties_.push_back(data_element);
}
void AddExtendedProperties(const std::vector<DataElement>& properties) {
extended_properties_.insert(extended_properties_.end(), properties.begin(),
properties.end());
}
std::vector<DataElement> GetExtendedProperties() const {
return extended_properties_;
}
void AddAction(const PresenceAction& action) { actions_.push_back(action); }
std::vector<PresenceAction> GetActions() const { return actions_; }
NearbyDevice::Type GetType() const override {
return NearbyDevice::Type::kPresenceDevice;
}
DeviceMotion GetDeviceMotion() const { return device_motion_; }
DeviceIdentityMetaData GetDeviceIdentityMetadata() const {
return device_identity_metadata_;
}
void SetDeviceIdentityMetaData(
const DeviceIdentityMetaData& device_identity_metadata) {
device_identity_metadata_ = device_identity_metadata;
}
void SetDecryptSharedCredential(
const internal::SharedCredential& decrypt_shared_credential) {
decrypt_shared_credential_ = decrypt_shared_credential;
}
const std::optional<internal::SharedCredential>& GetDecryptSharedCredential()
const {
return decrypt_shared_credential_;
}
absl::Time GetDiscoveryTimestamp() const { return discovery_timestamp_; }
internal::IdentityType GetIdentityType() const { return identity_type_; }
private:
const absl::Time discovery_timestamp_;
const DeviceMotion device_motion_;
DeviceIdentityMetaData device_identity_metadata_;
std::vector<DataElement> extended_properties_;
std::vector<PresenceAction> actions_;
std::string endpoint_id_;
internal::IdentityType identity_type_ = internal::IDENTITY_TYPE_UNSPECIFIED;
std::optional<internal::SharedCredential> decrypt_shared_credential_;
};
// Timestamp is not used for equality since if the same device is discovered
// twice, they will have different timestamps and thus will show up as two
// different devices when they are the same device.
inline bool operator==(const PresenceDevice& d1, const PresenceDevice& d2) {
bool shared_credential_equality = true;
shared_credential_equality &= d1.GetDecryptSharedCredential().has_value() ==
d2.GetDecryptSharedCredential().has_value();
if (shared_credential_equality &&
d1.GetDecryptSharedCredential().has_value()) {
shared_credential_equality &=
d1.GetDecryptSharedCredential()->SerializeAsString() ==
d2.GetDecryptSharedCredential()->SerializeAsString();
}
return d1.GetDeviceMotion() == d2.GetDeviceMotion() &&
d1.GetDeviceIdentityMetadata().SerializeAsString() ==
d2.GetDeviceIdentityMetadata().SerializeAsString() &&
d1.GetActions() == d2.GetActions() &&
d1.GetExtendedProperties() == d2.GetExtendedProperties() &&
d1.GetIdentityType() == d2.GetIdentityType() &&
shared_credential_equality;
}
inline bool operator!=(const PresenceDevice& d1, const PresenceDevice& d2) {
return !(d1 == d2);
}
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_H_