Define FastPairDeviceRepository

Move onwership of Fast Par Devices to a single point, FastPairDeviceRepository,
which will live inside a singleton FastPairService.

PiperOrigin-RevId: 534547186
This commit is contained in:
Janusz Sobczak
2023-05-23 13:54:05 -07:00
committed by Copybara-Service
parent 27267f57b8
commit c2fc33038c
16 changed files with 315 additions and 49 deletions
+9 -1
View File
@@ -45,8 +45,9 @@ class FastPairDevice {
: model_id_(model_id), ble_address_(ble_address), protocol_(protocol) {}
FastPairDevice(const FastPairDevice&) = delete;
FastPairDevice(FastPairDevice&&) = default;
FastPairDevice& operator=(const FastPairDevice&) = delete;
FastPairDevice& operator=(FastPairDevice&&) = delete;
FastPairDevice& operator=(FastPairDevice&&) = default;
~FastPairDevice() = default;
std::optional<std::string> GetPublicAddress() const {
@@ -87,6 +88,13 @@ class FastPairDevice {
Protocol GetProtocol() const { return protocol_; }
const std::string& GetUniqueId() const {
if (public_address_.has_value()) {
return *public_address_;
}
return ble_address_;
}
private:
std::string model_id_;
+1 -1
View File
@@ -30,9 +30,9 @@ cc_library(
],
deps = [
"//fastpair/common",
"//fastpair/repository:device_repository",
"//fastpair/scanning:scanner",
"//internal/platform:logging",
"//internal/platform/implementation:types",
],
)
+2 -3
View File
@@ -16,10 +16,9 @@
#include <memory>
#include "fastpair/common/protocol.h"
#include "fastpair/scanning/scanner_broker_impl.h"
#include "internal/platform/logging.h"
#include "fastpair/common/protocol.h"
namespace nearby {
namespace fastpair {
@@ -31,7 +30,7 @@ FastPairWrapperImpl::~FastPairWrapperImpl() = default;
void FastPairWrapperImpl::StartScan() {
Mediums mediums;
scanner_broker_ = std::make_unique<ScannerBrokerImpl>(mediums);
scanner_broker_ = std::make_unique<ScannerBrokerImpl>(mediums, &devices_);
if (is_scanning_) {
NEARBY_LOGS(VERBOSE) << __func__ << ": We're currently scanning. ";
return;
+2
View File
@@ -19,6 +19,7 @@
#include <memory>
#include "fastpair/dart/fast_pair_wrapper.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/scanner_broker.h"
namespace nearby {
@@ -38,6 +39,7 @@ class FastPairWrapperImpl : public FastPairWrapper {
private:
std::unique_ptr<ScannerBroker> scanner_broker_;
FastPairDeviceRepository devices_;
// True if we are currently scanning for remote devices.
bool is_scanning_ = false;
+28 -4
View File
@@ -29,6 +29,33 @@ cc_library(
],
)
cc_library(
name = "device_repository",
srcs = [
"fast_pair_device_repository.cc",
],
hdrs = [
"fast_pair_device_repository.h",
],
compatible_with = ["//buildenv/target:non_prod"],
visibility = ["//fastpair:__subpackages__"],
deps = ["//fastpair/common"],
)
cc_test(
name = "device_repository_test",
srcs = [
"fast_pair_device_repository_test.cc",
],
deps = [
":device_repository",
"//fastpair/common",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "fake_fast_pair_metadata_repository",
hdrs = ["fake_fast_pair_metadata_repository.h"],
@@ -38,10 +65,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":repository",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
],
)
@@ -60,7 +84,7 @@ cc_test(
"//fastpair/internal/test:nearby_fastpair_test",
"//fastpair/proto:fastpair_cc_proto",
"//internal/network:types",
"//internal/platform/implementation/g3",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
@@ -0,0 +1,64 @@
// 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.
// 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/repository/fast_pair_device_repository.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <utility>
namespace nearby {
namespace fastpair {
FastPairDevice* FastPairDeviceRepository::AddDevice(
std::unique_ptr<FastPairDevice> device) {
const auto& id = device->GetUniqueId();
for (auto& item : devices_) {
if (item->GetUniqueId() == id) {
// Overwrite the existing object.
*item = std::move(*device);
return item.get();
}
}
FastPairDevice* ptr = device.get();
devices_.push_back(std::move(device));
return ptr;
}
void FastPairDeviceRepository::RemoveDevice(const FastPairDevice* device) {
devices_.erase(
std::remove_if(devices_.begin(), devices_.end(),
[&](const std::unique_ptr<FastPairDevice>& item) {
return item.get() == device;
}),
devices_.end());
}
std::optional<FastPairDevice*> FastPairDeviceRepository::FindDevice(
absl::string_view mac_address) {
auto it = std::find_if(devices_.begin(), devices_.end(),
[&](const std::unique_ptr<FastPairDevice>& device) {
return device->GetBleAddress() == mac_address ||
device->GetPublicAddress() == mac_address;
});
if (it != devices_.end()) {
return it->get();
} else {
return std::nullopt;
}
}
} // namespace fastpair
} // namespace nearby
@@ -0,0 +1,51 @@
// 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.
// 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_REPOSITORY_FAST_PAIR_DEVICE_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_DEVICE_REPOSITORY_H_
#include <memory>
#include <optional>
#include <vector>
#include "fastpair/common/fast_pair_device.h"
namespace nearby {
namespace fastpair {
// Owner of `FastPairDevice` instances.
class FastPairDeviceRepository {
public:
// Adds device to the repository and takes over ownership.
// If a device with the same MAC address is already in the repository, it is
// replaced.
// Returns a stable, non-null pointer to the inserted element. The pointer is
// valid until `RemoveDevice()`.
FastPairDevice* AddDevice(std::unique_ptr<FastPairDevice> device);
// Removes the device and frees resources.
void RemoveDevice(const FastPairDevice* device);
// Finds a device matching the mac address. The mac address can be either BT
// or BLE.
std::optional<FastPairDevice*> FindDevice(absl::string_view mac_address);
private:
std::vector<std::unique_ptr<FastPairDevice>> devices_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_DEVICE_REPOSITORY_H_
@@ -0,0 +1,85 @@
// 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.
// 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/repository/fast_pair_device_repository.h"
#include <memory>
#include <utility>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/common/protocol.h"
namespace nearby {
namespace fastpair {
namespace {
constexpr absl::string_view kModelId = "123456";
constexpr absl::string_view kBleAddress = "AA:BB:CC:DD:EE:FF";
constexpr absl::string_view kBtAddress = "12:34:56:78:90:AB";
TEST(FastPairDeviceRepositoryTest, AddDevice) {
FastPairDeviceRepository repo;
FastPairDevice* device = repo.AddDevice(std::make_unique<FastPairDevice>(
kModelId, kBleAddress, Protocol::kFastPairInitialPairing));
ASSERT_NE(device, nullptr);
EXPECT_EQ(device->GetModelId(), kModelId);
}
TEST(FastPairDeviceRepositoryTest, FindDeviceByBleAddress) {
FastPairDeviceRepository repo;
repo.AddDevice(std::make_unique<FastPairDevice>(
kModelId, kBleAddress, Protocol::kFastPairInitialPairing));
auto opt_device = repo.FindDevice(kBleAddress);
ASSERT_TRUE(opt_device.has_value());
FastPairDevice* device = opt_device.value();
ASSERT_NE(device, nullptr);
EXPECT_EQ(device->GetModelId(), kModelId);
}
TEST(FastPairDeviceRepositoryTest, FindDeviceByBtAddress) {
FastPairDeviceRepository repo;
auto fast_pair_device =
std::make_unique<FastPairDevice>(Protocol::kFastPairInitialPairing);
fast_pair_device->SetPublicAddress(kBtAddress);
repo.AddDevice(std::move(fast_pair_device));
auto opt_device = repo.FindDevice(kBtAddress);
ASSERT_TRUE(opt_device.has_value());
FastPairDevice* device = opt_device.value();
ASSERT_NE(device, nullptr);
EXPECT_EQ(device->GetPublicAddress(), kBtAddress);
}
TEST(FastPairDeviceRepositoryTest, RemoveDevice) {
FastPairDeviceRepository repo;
FastPairDevice* device = repo.AddDevice(std::make_unique<FastPairDevice>(
kModelId, kBleAddress, Protocol::kFastPairInitialPairing));
repo.RemoveDevice(device);
EXPECT_FALSE(repo.FindDevice(kBleAddress).has_value());
}
} // namespace
} // namespace fastpair
} // namespace nearby
+1 -3
View File
@@ -30,14 +30,12 @@ cc_library(
deps = [
"//fastpair/common",
"//fastpair/internal/mediums",
"//fastpair/repository:device_repository",
"//fastpair/scanning/fastpair:scanning",
"//internal/base",
"//internal/platform:base",
"//internal/platform:comm",
"//internal/platform:logging",
"//internal/platform:types",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/strings",
],
)
+2 -3
View File
@@ -36,6 +36,7 @@ cc_library(
"//fastpair/internal/mediums",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/server_access",
"//internal/base",
"//internal/platform:base",
@@ -45,7 +46,6 @@ cc_library(
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
],
)
@@ -103,11 +103,10 @@ cc_test(
deps = [
":scanning",
":test_support",
"//fastpair/dataparser",
"//fastpair/repository:device_repository",
"//fastpair/server_access:test_support",
"//fastpair/testing",
"//internal/platform:comm",
"//internal/platform:test_util",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/synchronization",
@@ -66,16 +66,18 @@ FastPairDiscoverableScannerImpl::Factory*
FastPairDiscoverableScannerImpl::Factory::g_test_factory_ = nullptr;
std::unique_ptr<FastPairDiscoverableScanner>
FastPairDiscoverableScannerImpl::Factory::Create(FastPairScanner& scanner,
DeviceCallback found_callback,
DeviceCallback lost_callback) {
FastPairDiscoverableScannerImpl::Factory::Create(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback, FastPairDeviceRepository* device_repository) {
if (g_test_factory_) {
return g_test_factory_->CreateInstance(scanner, std::move(found_callback),
std::move(lost_callback));
std::move(lost_callback),
device_repository);
}
return std::make_unique<FastPairDiscoverableScannerImpl>(
scanner, std::move(found_callback), std::move(lost_callback));
scanner, std::move(found_callback), std::move(lost_callback),
device_repository);
}
void FastPairDiscoverableScannerImpl::Factory::SetFactoryForTesting(
@@ -88,10 +90,11 @@ FastPairDiscoverableScannerImpl::Factory::~Factory() = default;
// FastPairScannerImpl
FastPairDiscoverableScannerImpl::FastPairDiscoverableScannerImpl(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback)
DeviceCallback lost_callback, FastPairDeviceRepository* device_repository)
: scanner_(scanner),
found_callback_(std::move(found_callback)),
lost_callback_(std::move(lost_callback)) {
lost_callback_(std::move(lost_callback)),
device_repository_(device_repository) {
scanner_.AddObserver(this);
}
@@ -183,10 +186,10 @@ void FastPairDiscoverableScannerImpl::OnDeviceMetadataRetrieved(
return;
}
MutexLock lock(&mutex_);
notified_devices_.insert_or_assign(
address, std::make_unique<FastPairDevice>(
model_id, address, Protocol::kFastPairInitialPairing));
NotifyDeviceFound(*notified_devices_[address]);
FastPairDevice* device =
device_repository_->AddDevice(std::make_unique<FastPairDevice>(
model_id, address, Protocol::kFastPairInitialPairing));
NotifyDeviceFound(*device);
}
void FastPairDiscoverableScannerImpl::NotifyDeviceFound(
@@ -203,12 +206,13 @@ void FastPairDiscoverableScannerImpl::OnDeviceLost(
MutexLock lock(&mutex_);
model_id_parse_attempts_.erase(peripheral.GetName());
auto it = notified_devices_.find(peripheral.GetName());
auto opt_device = device_repository_->FindDevice(peripheral.GetName());
// Don't invoke callback if we didn't notify this device.
if (it == notified_devices_.end()) return;
lost_callback_(*it->second);
notified_devices_.erase(it);
if (!opt_device.has_value()) return;
FastPairDevice* device = opt_device.value();
lost_callback_(*device);
device_repository_->RemoveDevice(device);
}
} // namespace fastpair
@@ -23,6 +23,7 @@
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "internal/base/observer_list.h"
@@ -39,7 +40,8 @@ class FastPairDiscoverableScannerImpl : public FastPairDiscoverableScanner,
public:
static std::unique_ptr<FastPairDiscoverableScanner> Create(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback);
DeviceCallback lost_callback,
FastPairDeviceRepository* device_repository);
static void SetFactoryForTesting(Factory* g_test_factory);
@@ -47,7 +49,8 @@ class FastPairDiscoverableScannerImpl : public FastPairDiscoverableScanner,
virtual ~Factory();
virtual std::unique_ptr<FastPairDiscoverableScanner> CreateInstance(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback) = 0;
DeviceCallback lost_callback,
FastPairDeviceRepository* device_repository) = 0;
private:
static Factory* g_test_factory_;
@@ -55,7 +58,8 @@ class FastPairDiscoverableScannerImpl : public FastPairDiscoverableScanner,
FastPairDiscoverableScannerImpl(FastPairScanner& scanner,
DeviceCallback found_callback,
DeviceCallback lost_callback);
DeviceCallback lost_callback,
FastPairDeviceRepository* device_repository);
FastPairDiscoverableScannerImpl(const FastPairDiscoverableScannerImpl&) =
delete;
FastPairDiscoverableScannerImpl& operator=(
@@ -78,8 +82,7 @@ class FastPairDiscoverableScannerImpl : public FastPairDiscoverableScanner,
FastPairScanner& scanner_;
DeviceCallback found_callback_;
DeviceCallback lost_callback_;
absl::flat_hash_map<std::string, std::unique_ptr<FastPairDevice>>
notified_devices_ ABSL_GUARDED_BY(mutex_);
FastPairDeviceRepository* device_repository_ ABSL_GUARDED_BY(mutex_);
absl::flat_hash_map<std::string, int> model_id_parse_attempts_
ABSL_GUARDED_BY(mutex_);
ObserverList<FastPairScanner::Observer> observer_list_;
@@ -20,6 +20,7 @@
#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fake_fast_pair_scanner.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/testing/fast_pair_service_data_creator.h"
@@ -74,6 +75,7 @@ class FakeBlePeripheral : public api::BlePeripheral {
TEST(FastPairDiscoverableScannerImplTest, ValidModelId) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
@@ -85,7 +87,8 @@ TEST(FastPairDiscoverableScannerImplTest, ValidModelId) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
@@ -98,6 +101,7 @@ TEST(FastPairDiscoverableScannerImplTest, ValidModelId) {
TEST(FastPairDiscoverableScannerImplTest, InvalidModelId) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
@@ -109,7 +113,8 @@ TEST(FastPairDiscoverableScannerImplTest, InvalidModelId) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(
kTestBleDeviceAddress, kInvalidModelId);
@@ -122,6 +127,7 @@ TEST(FastPairDiscoverableScannerImplTest, InvalidModelId) {
TEST(FastPairDiscoverableScannerImplTest, NoServiceData) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
@@ -133,7 +139,8 @@ TEST(FastPairDiscoverableScannerImplTest, NoServiceData) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, "");
@@ -146,6 +153,7 @@ TEST(FastPairDiscoverableScannerImplTest, NoServiceData) {
TEST(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::AUTOMOTIVE);
repository->SetFakeMetadata(kValidModelId, metadata);
@@ -157,7 +165,8 @@ TEST(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
@@ -170,6 +179,7 @@ TEST(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) {
TEST(FastPairDiscoverableScannerImplTest, UnsupportedNotifictionType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::HEADPHONES);
metadata.set_notification_type(proto::NotificationType::APP_LAUNCH);
@@ -182,7 +192,8 @@ TEST(FastPairDiscoverableScannerImplTest, UnsupportedNotifictionType) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
@@ -198,6 +209,7 @@ TEST(FastPairDiscoverableScannerImplTest, UnspecifiedNotificationType) {
// or device type. Since we aren't sure what this device is, we'll show
// the notification to be safe.
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::DEVICE_TYPE_UNSPECIFIED);
metadata.set_notification_type(
@@ -211,7 +223,8 @@ TEST(FastPairDiscoverableScannerImplTest, UnspecifiedNotificationType) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
@@ -226,6 +239,7 @@ TEST(FastPairDiscoverableScannerImplTest, V1NotificationType) {
// Set metadata to mimic a V1 device which advertises with no device
// type and a notification type of FAST_PAIR_ONE.
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::DEVICE_TYPE_UNSPECIFIED);
metadata.set_notification_type(proto::NotificationType::FAST_PAIR_ONE);
@@ -238,7 +252,8 @@ TEST(FastPairDiscoverableScannerImplTest, V1NotificationType) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
@@ -253,6 +268,7 @@ TEST(FastPairDiscoverableScannerImplTest, V2NotificationType) {
// Set metadata to mimic a V2 device which advertises with a device
// type of TRUE_WIRELESS_HEADPHONES and a notification type of FAST_PAIR.
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
metadata.set_notification_type(proto::NotificationType::FAST_PAIR);
@@ -265,7 +281,8 @@ TEST(FastPairDiscoverableScannerImplTest, V2NotificationType) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
@@ -278,6 +295,7 @@ TEST(FastPairDiscoverableScannerImplTest, V2NotificationType) {
TEST(FastPairDiscoverableScannerImplTest, NearbyShareModelId) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
@@ -288,7 +306,8 @@ TEST(FastPairDiscoverableScannerImplTest, NearbyShareModelId) {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(
kTestBleDeviceAddress, kNearbyShareModelId);
@@ -302,6 +321,7 @@ TEST(FastPairDiscoverableScannerImplTest,
DoesntInvokeLostCallbackIfDidntInvokeFound) {
auto scanner = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
FastPairDeviceRepository devices;
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
@@ -312,7 +332,8 @@ TEST(FastPairDiscoverableScannerImplTest,
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner,
[&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); });
[&](FastPairDevice& device) { lost_notification.Notify(); },
&devices);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
+5 -2
View File
@@ -25,7 +25,9 @@
namespace nearby {
namespace fastpair {
ScannerBrokerImpl::ScannerBrokerImpl(Mediums& mediums) : mediums_(mediums) {
ScannerBrokerImpl::ScannerBrokerImpl(
Mediums& mediums, FastPairDeviceRepository* device_repository)
: mediums_(mediums), device_repository_(device_repository) {
task_runner_ = std::make_unique<TaskRunnerImpl>(1);
}
@@ -55,7 +57,8 @@ void ScannerBrokerImpl::StartFastPairScanning() {
FastPairDiscoverableScannerImpl::Factory::Create(
*scanner_,
absl::bind_front(&ScannerBrokerImpl::NotifyDeviceFound, this),
absl::bind_front(&ScannerBrokerImpl::NotifyDeviceLost, this));
absl::bind_front(&ScannerBrokerImpl::NotifyDeviceLost, this),
device_repository_);
scanner_->StartScanning();
}
+4 -1
View File
@@ -19,6 +19,7 @@
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "fastpair/scanning/scanner_broker.h"
@@ -30,7 +31,8 @@ namespace fastpair {
class ScannerBrokerImpl : public ScannerBroker {
public:
explicit ScannerBrokerImpl(Mediums& mediums);
ScannerBrokerImpl(Mediums& mediums,
FastPairDeviceRepository* device_repository);
~ScannerBrokerImpl() override = default;
// ScannerBroker:
@@ -50,6 +52,7 @@ class ScannerBrokerImpl : public ScannerBroker {
std::unique_ptr<FastPairScanner> scanner_;
std::unique_ptr<FastPairDiscoverableScanner> fast_pair_discoverable_scanner_;
ObserverList<Observer> observers_;
FastPairDeviceRepository* device_repository_;
};
} // namespace fastpair
@@ -74,6 +74,7 @@ TEST_F(ScannerBrokerImplTest, CanStartScanning) {
// Setup FakeFastPairRepository
std::string decoded_key;
absl::Base64Unescape(kPublicAntiSpoof, &decoded_key);
FastPairDeviceRepository devices;
proto::Device metadata;
auto repository_ = std::make_unique<FakeFastPairRepository>();
metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
@@ -81,7 +82,8 @@ TEST_F(ScannerBrokerImplTest, CanStartScanning) {
// Create Fast Pair Scanner and add its observer
Mediums mediums_1;
auto scanner_broker = std::make_unique<ScannerBrokerImpl>(mediums_1);
auto scanner_broker =
std::make_unique<ScannerBrokerImpl>(mediums_1, &devices);
CountDownLatch accept_latch(1);
CountDownLatch lost_latch(1);
ScannerBrokerObserver observer(scanner_broker.get(), &accept_latch,