mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Implementation FastPairDevice
PiperOrigin-RevId: 495078613
This commit is contained in:
committed by
Copybara-Service
parent
228e52e979
commit
db0aaf75d2
@@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -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 <ostream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#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
|
||||
@@ -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 <cstdint>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string>& public_address() const {
|
||||
return public_address_;
|
||||
}
|
||||
|
||||
void set_public_address(const std::string& address) {
|
||||
public_address_ = address;
|
||||
}
|
||||
|
||||
const absl::optional<std::string>& display_name() const {
|
||||
return display_name_;
|
||||
}
|
||||
|
||||
void set_display_name(const absl::optional<std::string>& display_name) {
|
||||
display_name_ = display_name;
|
||||
}
|
||||
|
||||
absl::optional<DeviceFastPairVersion> version() { return version_; }
|
||||
|
||||
void set_version(absl::optional<DeviceFastPairVersion> version) {
|
||||
version_ = version;
|
||||
}
|
||||
|
||||
absl::optional<std::vector<uint8_t>> account_key() const {
|
||||
return account_key_;
|
||||
}
|
||||
|
||||
void set_account_key(std::vector<uint8_t> 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<std::string> 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<std::string> display_name_;
|
||||
|
||||
// Fast Pair version number, possible versions numbers are defined at the top
|
||||
// of this file.
|
||||
absl::optional<DeviceFastPairVersion> 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<std::vector<uint8_t>> 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_
|
||||
@@ -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 <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::vector<uint8_t>> accountKey;
|
||||
std::vector<uint8_t> data = {0};
|
||||
device_.set_account_key(data);
|
||||
accountKey = device_.account_key();
|
||||
EXPECT_EQ(accountKey, data);
|
||||
|
||||
// Test that overriding works.
|
||||
std::vector<uint8_t> 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<std::string> 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<std::string> 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
|
||||
Reference in New Issue
Block a user