diff --git a/fastpair/common/BUILD b/fastpair/common/BUILD index f53566da..10fc7f3a 100644 --- a/fastpair/common/BUILD +++ b/fastpair/common/BUILD @@ -3,10 +3,12 @@ licenses(["notice"]) cc_library( name = "common", srcs = [ + "fast_pair_device.cc", "protocol.cc", ], hdrs = [ "constant.h", + "fast_pair_device.h", "protocol.h", ], visibility = [ @@ -16,3 +18,18 @@ cc_library( "@com_google_absl//absl/container:flat_hash_map", ], ) + +cc_test( + name = "unit_test", + size = "small", + srcs = [ + "fast_pair_device_test.cc", + ], + shard_count = 16, + deps = [ + ":common", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/fastpair/common/fast_pair_device.cc b/fastpair/common/fast_pair_device.cc new file mode 100644 index 00000000..46ad0a0c --- /dev/null +++ b/fastpair/common/fast_pair_device.cc @@ -0,0 +1,47 @@ +// Copyright 2022 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 "fastpair/common/fast_pair_device.h" + +#include +#include +#include + +#include "fastpair/common/protocol.h" + +namespace location { +namespace nearby { +namespace fastpair { + +FastPairDevice::FastPairDevice(std::string model_id, std::string ble_address, + Protocol protocol) + : model_id(std::move(model_id)), + ble_address(std::move(ble_address)), + protocol(protocol) {} + +FastPairDevice::~FastPairDevice() = default; + +std::ostream& operator<<(std::ostream& stream, const FastPairDevice& device) { + stream << "[Device: model_id = " << device.model_id + << ", ble_address = " << device.ble_address + << ", piblic_address = " << device.public_address().value_or("null") + << ", display_name = " << device.display_name().value_or("null") + << ", protocol = " << device.protocol << "]"; + + return stream; +} + +} // namespace fastpair +} // namespace nearby +} // namespace location diff --git a/fastpair/common/fast_pair_device.h b/fastpair/common/fast_pair_device.h new file mode 100644 index 00000000..c05061b4 --- /dev/null +++ b/fastpair/common/fast_pair_device.h @@ -0,0 +1,110 @@ +// Copyright 2022 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_FASTPAIR_COMMON_FAST_PAIR_DEVICE_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FAST_PAIR_DEVICE_H_ + +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "fastpair/common/protocol.h" + +namespace location { +namespace nearby { +namespace fastpair { + +enum class DeviceFastPairVersion { + kV1, + kHigherThanV1, +}; + +// Thin class which is used by the higher level components of the Fast Pair +// system to represent a device. +struct FastPairDevice { + FastPairDevice(std::string model_id, std::string ble_address, + Protocol protocol); + FastPairDevice(const FastPairDevice&) = delete; + FastPairDevice& operator=(const FastPairDevice&) = delete; + FastPairDevice& operator=(FastPairDevice&&) = delete; + ~FastPairDevice(); + + const absl::optional& public_address() const { + return public_address_; + } + + void set_public_address(const std::string& address) { + public_address_ = address; + } + + const absl::optional& display_name() const { + return display_name_; + } + + void set_display_name(const absl::optional& display_name) { + display_name_ = display_name; + } + + absl::optional version() { return version_; } + + void set_version(absl::optional version) { + version_ = version; + } + + absl::optional> account_key() const { + return account_key_; + } + + void set_account_key(std::vector account_key) { + account_key_ = account_key; + } + + const std::string model_id; + + // Bluetooth LE address of the device. + const std::string ble_address; + + // The Quick Pair protocol implementation that this device belongs to. + const Protocol protocol; + + private: + // Bluetooth public classic address of the device. + absl::optional public_address_; + + // Display name for the device + // Similar to Bluetooth classic address field, this will be null when a + // device is found from a discoverable advertisement due to the fact that + // initial pair notifications show the OEM default name from the device + // metadata instead of the display name. + absl::optional display_name_; + + // Fast Pair version number, possible versions numbers are defined at the top + // of this file. + absl::optional version_; + + // Account key which will be saved to the user's account during Fast Pairing + // for eligible devices (V2 or higher) and used for detecting subsequent + // pairing scenarios. + absl::optional> account_key_; +}; + +std::ostream& operator<<(std::ostream& stream, const FastPairDevice& device); + +} // namespace fastpair +} // namespace nearby +} // namespace location + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FAST_PAIR_DEVICE_H_ diff --git a/fastpair/common/fast_pair_device_test.cc b/fastpair/common/fast_pair_device_test.cc new file mode 100644 index 00000000..c0f440ba --- /dev/null +++ b/fastpair/common/fast_pair_device_test.cc @@ -0,0 +1,94 @@ +// Copyright 2022 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 "fastpair/common/fast_pair_device.h" + +#include +#include +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "fastpair/common/protocol.h" + +namespace location { +namespace nearby { +namespace fastpair { +namespace { + +TEST(FastPairDevice, GetAndSetAccountKey) { + FastPairDevice device_("model_id", "ble_address", + Protocol::kFastPairInitialPairing); + absl::optional> accountKey; + std::vector data = {0}; + device_.set_account_key(data); + accountKey = device_.account_key(); + EXPECT_EQ(accountKey, data); + + // Test that overriding works. + std::vector more_data = {1}; + device_.set_account_key(more_data); + accountKey = device_.account_key(); + EXPECT_EQ(accountKey, more_data); +} + +TEST(FastPairDevice, GetAndSetName) { + FastPairDevice device_("model_id", "ble_address", + Protocol::kFastPairInitialPairing); + // Test that name returns null before any sets. + absl::optional name = device_.display_name(); + EXPECT_FALSE(name.has_value()); + + // Test that name returns the set value. + std::string test_name = "test_name"; + device_.set_display_name(test_name); + name = device_.display_name(); + EXPECT_TRUE(name.has_value()); + EXPECT_EQ(name.value(), test_name); + + // Test that overriding works. + std::string new_test_name = "new_test_name"; + device_.set_display_name(new_test_name); + name = device_.display_name(); + EXPECT_TRUE(name.has_value()); + EXPECT_EQ(name.value(), new_test_name); +} + +TEST(FastPairDevice, GetAndPublicAddress) { + FastPairDevice device_("model_id", "ble_address", + Protocol::kFastPairInitialPairing); + // Test that public address returns null before any sets. + absl::optional public_address = device_.public_address(); + EXPECT_FALSE(public_address.has_value()); + + // Test that name returns the set value. + std::string test_public_address = "test_public_address "; + device_.set_public_address(test_public_address); + public_address = device_.public_address(); + EXPECT_TRUE(public_address.has_value()); + EXPECT_EQ(public_address.value(), test_public_address); + + // Test that overriding works. + std::string new_test_public_address = "new_test_public_address "; + device_.set_public_address(new_test_public_address); + public_address = device_.public_address(); + EXPECT_TRUE(public_address.has_value()); + EXPECT_EQ(public_address.value(), new_test_public_address); +} + +} // namespace +} // namespace fastpair +} // namespace nearby +} // namespace location