From 4acd3b1a1d88b045e65973d62be655e9ddea674c Mon Sep 17 00:00:00 2001 From: hais Date: Tue, 22 Mar 2022 17:55:29 -0700 Subject: [PATCH] Initial impl for Nearby Presence in Nearby SDk. discovery associated data types. PiperOrigin-RevId: 436615656 --- presence/BUILD | 52 ++++++++++++++++++++++++++ presence/device_motion.cc | 28 ++++++++++++++ presence/device_motion.h | 47 +++++++++++++++++++++++ presence/device_motion_test.cc | 60 ++++++++++++++++++++++++++++++ presence/presence_action.cc | 31 +++++++++++++++ presence/presence_action.h | 42 +++++++++++++++++++++ presence/presence_action_test.cc | 57 ++++++++++++++++++++++++++++ presence/presence_identity.cc | 28 ++++++++++++++ presence/presence_identity.h | 44 ++++++++++++++++++++++ presence/presence_identity_test.cc | 59 +++++++++++++++++++++++++++++ 10 files changed, 448 insertions(+) create mode 100644 presence/BUILD create mode 100644 presence/device_motion.cc create mode 100644 presence/device_motion.h create mode 100644 presence/device_motion_test.cc create mode 100644 presence/presence_action.cc create mode 100644 presence/presence_action.h create mode 100644 presence/presence_action_test.cc create mode 100644 presence/presence_identity.cc create mode 100644 presence/presence_identity.h create mode 100644 presence/presence_identity_test.cc diff --git a/presence/BUILD b/presence/BUILD new file mode 100644 index 00000000..7ccd0290 --- /dev/null +++ b/presence/BUILD @@ -0,0 +1,52 @@ +# 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. +licenses(["notice"]) + +cc_library( + name = "types", + srcs = [ + "device_motion.cc", + "presence_action.cc", + "presence_identity.cc", + ], + hdrs = [ + "device_motion.h", + "presence_action.h", + "presence_identity.h", + ], + visibility = [ + "//third_party/nearby/presence:__subpackages__", + ], + deps = [ + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:variant", + "@com_google_glog//:glog", + ], +) + +cc_test( + name = "types_test", + size = "small", + srcs = [ + "device_motion_test.cc", + "presence_action_test.cc", + "presence_identity_test.cc", + ], + shard_count = 6, + deps = [ + ":types", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/presence/device_motion.cc b/presence/device_motion.cc new file mode 100644 index 00000000..a42afc4b --- /dev/null +++ b/presence/device_motion.cc @@ -0,0 +1,28 @@ +// 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. + +#include "third_party/nearby/presence/device_motion.h" + +namespace nearby { +namespace presence { + +DeviceMotion::DeviceMotion(MotionType motion_type, float confidence) noexcept + : motion_type_(motion_type), confidence_(confidence) {} +DeviceMotion::MotionType DeviceMotion::GetMotionType() const { + return motion_type_; +} +float DeviceMotion::GetConfidence() const { return confidence_; } + +} // namespace presence +} // namespace nearby diff --git a/presence/device_motion.h b/presence/device_motion.h new file mode 100644 index 00000000..2dcd4bc8 --- /dev/null +++ b/presence/device_motion.h @@ -0,0 +1,47 @@ +// 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_DEVICE_MOTION_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_DEVICE_MOTION_H_ + +namespace nearby { +namespace presence { +class DeviceMotion { + public: + enum class MotionType { + kPointAndHold = 0, + }; + DeviceMotion(MotionType motion_type = MotionType::kPointAndHold, + float confidence = 0) noexcept; + ~DeviceMotion() noexcept = default; + MotionType GetMotionType() const; + float GetConfidence() const; + + private: + const MotionType motion_type_; + const float confidence_; +}; + +inline bool operator==(const DeviceMotion& m1, const DeviceMotion& m2) { + return m1.GetMotionType() == m2.GetMotionType() && + m1.GetConfidence() == m2.GetConfidence(); +} +inline bool operator!=(const DeviceMotion& m1, const DeviceMotion& m2) { + return !(m1 == m2); +} + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_DEVICE_MOTION_H_ diff --git a/presence/device_motion_test.cc b/presence/device_motion_test.cc new file mode 100644 index 00000000..6ff71b79 --- /dev/null +++ b/presence/device_motion_test.cc @@ -0,0 +1,60 @@ +// 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. + +#include "third_party/nearby/presence/device_motion.h" + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" + +namespace nearby { +namespace presence { + +static constexpr DeviceMotion::MotionType kDefaultMotionType = + DeviceMotion::MotionType::kPointAndHold; +static constexpr float kDefaultConfidence = 0; +static constexpr float kConfidenceForTest = 0.1; +TEST(DeviceMotionTest, DefaultConstructorWorks) { + DeviceMotion motion; + EXPECT_EQ(motion.GetMotionType(), kDefaultMotionType); + EXPECT_EQ(motion.GetConfidence(), kDefaultConfidence); +} + +TEST(DeviceMotionTest, DefaultEquals) { + DeviceMotion motion1; + DeviceMotion motion2; + EXPECT_EQ(motion1, motion2); +} + +TEST(DeviceMotionTest, ExplicitInitEquals) { + DeviceMotion motion1 = {kDefaultMotionType, kConfidenceForTest}; + DeviceMotion motion2 = {kDefaultMotionType, kConfidenceForTest}; + EXPECT_EQ(motion1, motion2); + EXPECT_EQ(motion1.GetConfidence(), kConfidenceForTest); +} + +TEST(DeviceMotionTest, ExplicitInitNotEquals) { + DeviceMotion motion1 = {kDefaultMotionType, kConfidenceForTest}; + DeviceMotion motion2 = {kDefaultMotionType}; + EXPECT_NE(motion1, motion2); +} + +TEST(DeviceMotionTest, CopyInitEquals) { + DeviceMotion motion1; + DeviceMotion motion2 = {motion1}; + EXPECT_EQ(motion1, motion2); +} + +} // namespace presence +} // namespace nearby diff --git a/presence/presence_action.cc b/presence/presence_action.cc new file mode 100644 index 00000000..606d6f0f --- /dev/null +++ b/presence/presence_action.cc @@ -0,0 +1,31 @@ +// 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. + +#include "third_party/nearby/presence/presence_action.h" + +#include "glog/logging.h" + +namespace nearby { +namespace presence { + +PresenceAction::PresenceAction(int action_identifier) + : action_identifier_(action_identifier) { + CHECK(kMinActionIdentifierValue <= action_identifier_ && + action_identifier_ <= kMaxActionIdentifierValue); +} + +int PresenceAction::GetActionIdentifier() const { return action_identifier_; } + +} // namespace presence +} // namespace nearby diff --git a/presence/presence_action.h b/presence/presence_action.h new file mode 100644 index 00000000..51a50c88 --- /dev/null +++ b/presence/presence_action.h @@ -0,0 +1,42 @@ +// 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_ACTION_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_ACTION_H_ + +namespace nearby { +namespace presence { +class PresenceAction { + public: + PresenceAction(int action_identifier = 1); + ~PresenceAction() noexcept = default; + int GetActionIdentifier() const; + + private: + static constexpr int kMinActionIdentifierValue = 1; + static constexpr int kMaxActionIdentifierValue = 255; + const int action_identifier_; +}; + +inline bool operator==(const PresenceAction& a1, const PresenceAction& a2) { + return a1.GetActionIdentifier() == a2.GetActionIdentifier(); +} + +inline bool operator!=(const PresenceAction& a1, const PresenceAction& a2) { + return !(a1 == a2); +} +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_ACTION_H_ diff --git a/presence/presence_action_test.cc b/presence/presence_action_test.cc new file mode 100644 index 00000000..f9f59227 --- /dev/null +++ b/presence/presence_action_test.cc @@ -0,0 +1,57 @@ +// 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. + +#include "third_party/nearby/presence/presence_action.h" + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" + +namespace nearby { +namespace presence { +static constexpr int kDefaultActionIdentifier = 1; +static constexpr int kTestActionIdentifier = 2; +TEST(PresenceActionTest, DefaultConstructorWorks) { + PresenceAction action; + EXPECT_EQ(action.GetActionIdentifier(), kDefaultActionIdentifier); +} + +TEST(PresenceActionTest, DefaultEquals) { + PresenceAction action1; + PresenceAction action2; + EXPECT_EQ(action1, action2); +} + +TEST(PresenceActionTest, ExplicitInitEquals) { + PresenceAction action1 = {kTestActionIdentifier}; + PresenceAction action2 = {kTestActionIdentifier}; + EXPECT_EQ(action1, action2); + EXPECT_EQ(action1.GetActionIdentifier(), kTestActionIdentifier); +} + +TEST(PresenceActionTest, ExplicitInitNotEquals) { + PresenceAction action1 = {kDefaultActionIdentifier}; + PresenceAction action2 = {kTestActionIdentifier}; + EXPECT_NE(action1, action2); +} + +TEST(PresenceActionTest, CopyInitEquals) { + PresenceAction action1 = {kTestActionIdentifier}; + PresenceAction action2 = {action1}; + + EXPECT_EQ(action1, action2); +} + +} // namespace presence +} // namespace nearby diff --git a/presence/presence_identity.cc b/presence/presence_identity.cc new file mode 100644 index 00000000..a05e2697 --- /dev/null +++ b/presence/presence_identity.cc @@ -0,0 +1,28 @@ +// 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. + +#include "third_party/nearby/presence/presence_identity.h" + +namespace nearby { +namespace presence { + +PresenceIdentity::PresenceIdentity(IdentityType identity_type) noexcept + : identity_type_(identity_type) {} + +PresenceIdentity::IdentityType PresenceIdentity::GetIdentityType() const { + return identity_type_; +} + +} // namespace presence +} // namespace nearby diff --git a/presence/presence_identity.h b/presence/presence_identity.h new file mode 100644 index 00000000..14ccb69f --- /dev/null +++ b/presence/presence_identity.h @@ -0,0 +1,44 @@ +// 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_IDENTITY_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_IDENTITY_H_ + +namespace nearby { +namespace presence { +class PresenceIdentity { + public: + enum class IdentityType { + kPrivate = 0, + kTrusted, + }; + PresenceIdentity(IdentityType type = IdentityType::kPrivate) noexcept; + ~PresenceIdentity() noexcept = default; + IdentityType GetIdentityType() const; + + private: + const IdentityType identity_type_; +}; + +inline bool operator==(const PresenceIdentity& i1, const PresenceIdentity& i2) { + return i1.GetIdentityType() == i2.GetIdentityType(); +} + +inline bool operator!=(const PresenceIdentity& i1, const PresenceIdentity& i2) { + return !(i1 == i2); +} +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_IDENTITY_H_ diff --git a/presence/presence_identity_test.cc b/presence/presence_identity_test.cc new file mode 100644 index 00000000..67794ab2 --- /dev/null +++ b/presence/presence_identity_test.cc @@ -0,0 +1,59 @@ +// 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. + +#include "third_party/nearby/presence/presence_identity.h" + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" + +namespace nearby { +namespace presence { +static constexpr PresenceIdentity::IdentityType kDefaultIdentityType = + PresenceIdentity::IdentityType::kPrivate; +static constexpr PresenceIdentity::IdentityType kTestIdentityType = + PresenceIdentity::IdentityType::kTrusted; + +TEST(PresenceIdentityTest, DefaultIsError) { + PresenceIdentity identity; + EXPECT_EQ(identity.GetIdentityType(), kDefaultIdentityType); +} + +TEST(PresenceIdentityTest, DefaultEquals) { + PresenceIdentity identity1; + PresenceIdentity identity2; + EXPECT_EQ(identity1, identity2); +} + +TEST(PresenceIdentityTest, ExplicitInitEquals) { + PresenceIdentity identity1 = {kTestIdentityType}; + PresenceIdentity identity2 = {kTestIdentityType}; + EXPECT_EQ(identity1, identity2); + EXPECT_EQ(identity1.GetIdentityType(), kTestIdentityType); +} + +TEST(PresenceIdentityTest, ExplicitInitNotEquals) { + PresenceIdentity identity1 = {kTestIdentityType}; + PresenceIdentity identity2; + EXPECT_NE(identity1, identity2); +} + +TEST(PresenceIdentityTest, CopyInitEquals) { + PresenceIdentity identity1 = {kTestIdentityType}; + PresenceIdentity identity2 = {identity1}; + EXPECT_EQ(identity1, identity2); +} + +} // namespace presence +} // namespace nearby