From c2fc33038cb9c047f3f43aa6f494212496a73b09 Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Tue, 23 May 2023 13:52:51 -0700 Subject: [PATCH] Define FastPairDeviceRepository Move onwership of Fast Par Devices to a single point, FastPairDeviceRepository, which will live inside a singleton FastPairService. PiperOrigin-RevId: 534547186 --- fastpair/common/fast_pair_device.h | 10 ++- fastpair/dart/BUILD | 2 +- fastpair/dart/fast_pair_wrapper_impl.cc | 5 +- fastpair/dart/fast_pair_wrapper_impl.h | 2 + fastpair/repository/BUILD | 32 ++++++- .../repository/fast_pair_device_repository.cc | 64 ++++++++++++++ .../repository/fast_pair_device_repository.h | 51 +++++++++++ .../fast_pair_device_repository_test.cc | 85 +++++++++++++++++++ fastpair/scanning/BUILD | 4 +- fastpair/scanning/fastpair/BUILD | 5 +- .../fast_pair_discoverable_scanner_impl.cc | 34 ++++---- .../fast_pair_discoverable_scanner_impl.h | 13 +-- ...ast_pair_discoverable_scanner_impl_test.cc | 41 ++++++--- fastpair/scanning/scanner_broker_impl.cc | 7 +- fastpair/scanning/scanner_broker_impl.h | 5 +- fastpair/scanning/scanner_broker_impl_test.cc | 4 +- 16 files changed, 315 insertions(+), 49 deletions(-) create mode 100644 fastpair/repository/fast_pair_device_repository.cc create mode 100644 fastpair/repository/fast_pair_device_repository.h create mode 100644 fastpair/repository/fast_pair_device_repository_test.cc diff --git a/fastpair/common/fast_pair_device.h b/fastpair/common/fast_pair_device.h index ea11347a..f6323968 100644 --- a/fastpair/common/fast_pair_device.h +++ b/fastpair/common/fast_pair_device.h @@ -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 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_; diff --git a/fastpair/dart/BUILD b/fastpair/dart/BUILD index e9dc578c..d3a2c424 100644 --- a/fastpair/dart/BUILD +++ b/fastpair/dart/BUILD @@ -30,9 +30,9 @@ cc_library( ], deps = [ "//fastpair/common", + "//fastpair/repository:device_repository", "//fastpair/scanning:scanner", "//internal/platform:logging", - "//internal/platform/implementation:types", ], ) diff --git a/fastpair/dart/fast_pair_wrapper_impl.cc b/fastpair/dart/fast_pair_wrapper_impl.cc index c29935e9..8e162691 100644 --- a/fastpair/dart/fast_pair_wrapper_impl.cc +++ b/fastpair/dart/fast_pair_wrapper_impl.cc @@ -16,10 +16,9 @@ #include - +#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(mediums); + scanner_broker_ = std::make_unique(mediums, &devices_); if (is_scanning_) { NEARBY_LOGS(VERBOSE) << __func__ << ": We're currently scanning. "; return; diff --git a/fastpair/dart/fast_pair_wrapper_impl.h b/fastpair/dart/fast_pair_wrapper_impl.h index 9589303b..ffb1f044 100644 --- a/fastpair/dart/fast_pair_wrapper_impl.h +++ b/fastpair/dart/fast_pair_wrapper_impl.h @@ -19,6 +19,7 @@ #include #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 scanner_broker_; + FastPairDeviceRepository devices_; // True if we are currently scanning for remote devices. bool is_scanning_ = false; diff --git a/fastpair/repository/BUILD b/fastpair/repository/BUILD index 6bfb3386..0a83d741 100644 --- a/fastpair/repository/BUILD +++ b/fastpair/repository/BUILD @@ -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", diff --git a/fastpair/repository/fast_pair_device_repository.cc b/fastpair/repository/fast_pair_device_repository.cc new file mode 100644 index 00000000..0f703ba0 --- /dev/null +++ b/fastpair/repository/fast_pair_device_repository.cc @@ -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 +#include +#include +#include + +namespace nearby { +namespace fastpair { + +FastPairDevice* FastPairDeviceRepository::AddDevice( + std::unique_ptr 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& item) { + return item.get() == device; + }), + devices_.end()); +} + +std::optional FastPairDeviceRepository::FindDevice( + absl::string_view mac_address) { + auto it = std::find_if(devices_.begin(), devices_.end(), + [&](const std::unique_ptr& 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 diff --git a/fastpair/repository/fast_pair_device_repository.h b/fastpair/repository/fast_pair_device_repository.h new file mode 100644 index 00000000..d97f77dc --- /dev/null +++ b/fastpair/repository/fast_pair_device_repository.h @@ -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 +#include +#include + +#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 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 FindDevice(absl::string_view mac_address); + + private: + std::vector> devices_; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_DEVICE_REPOSITORY_H_ diff --git a/fastpair/repository/fast_pair_device_repository_test.cc b/fastpair/repository/fast_pair_device_repository_test.cc new file mode 100644 index 00000000..9c996955 --- /dev/null +++ b/fastpair/repository/fast_pair_device_repository_test.cc @@ -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 +#include + +#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( + kModelId, kBleAddress, Protocol::kFastPairInitialPairing)); + + ASSERT_NE(device, nullptr); + EXPECT_EQ(device->GetModelId(), kModelId); +} + +TEST(FastPairDeviceRepositoryTest, FindDeviceByBleAddress) { + FastPairDeviceRepository repo; + repo.AddDevice(std::make_unique( + 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(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( + kModelId, kBleAddress, Protocol::kFastPairInitialPairing)); + + repo.RemoveDevice(device); + + EXPECT_FALSE(repo.FindDevice(kBleAddress).has_value()); +} + +} // namespace + +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/scanning/BUILD b/fastpair/scanning/BUILD index e1dc5370..5b1b0158 100644 --- a/fastpair/scanning/BUILD +++ b/fastpair/scanning/BUILD @@ -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", ], ) diff --git a/fastpair/scanning/fastpair/BUILD b/fastpair/scanning/fastpair/BUILD index 2f3bd825..dc01f55a 100644 --- a/fastpair/scanning/fastpair/BUILD +++ b/fastpair/scanning/fastpair/BUILD @@ -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", diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc index 2236856f..e6618374 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc @@ -66,16 +66,18 @@ FastPairDiscoverableScannerImpl::Factory* FastPairDiscoverableScannerImpl::Factory::g_test_factory_ = nullptr; std::unique_ptr -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( - 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( - model_id, address, Protocol::kFastPairInitialPairing)); - NotifyDeviceFound(*notified_devices_[address]); + FastPairDevice* device = + device_repository_->AddDevice(std::make_unique( + 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 diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h index c172e09a..1280a674 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h @@ -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 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 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> - notified_devices_ ABSL_GUARDED_BY(mutex_); + FastPairDeviceRepository* device_repository_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map model_id_parse_attempts_ ABSL_GUARDED_BY(mutex_); ObserverList observer_list_; diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc index 024a85d1..f7e05bf9 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc @@ -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(); auto repository = std::make_unique(); + 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(kTestBleDeviceAddress, kValidModelId); @@ -98,6 +101,7 @@ TEST(FastPairDiscoverableScannerImplTest, ValidModelId) { TEST(FastPairDiscoverableScannerImplTest, InvalidModelId) { auto scanner = std::make_unique(); auto repository = std::make_unique(); + 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( kTestBleDeviceAddress, kInvalidModelId); @@ -122,6 +127,7 @@ TEST(FastPairDiscoverableScannerImplTest, InvalidModelId) { TEST(FastPairDiscoverableScannerImplTest, NoServiceData) { auto scanner = std::make_unique(); auto repository = std::make_unique(); + 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(kTestBleDeviceAddress, ""); @@ -146,6 +153,7 @@ TEST(FastPairDiscoverableScannerImplTest, NoServiceData) { TEST(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) { auto scanner = std::make_unique(); auto repository = std::make_unique(); + 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(kTestBleDeviceAddress, kValidModelId); @@ -170,6 +179,7 @@ TEST(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) { TEST(FastPairDiscoverableScannerImplTest, UnsupportedNotifictionType) { auto scanner = std::make_unique(); auto repository = std::make_unique(); + 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(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(); + 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(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(); + 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(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(); + 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(kTestBleDeviceAddress, kValidModelId); @@ -278,6 +295,7 @@ TEST(FastPairDiscoverableScannerImplTest, V2NotificationType) { TEST(FastPairDiscoverableScannerImplTest, NearbyShareModelId) { auto scanner = std::make_unique(); auto repository = std::make_unique(); + 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( kTestBleDeviceAddress, kNearbyShareModelId); @@ -302,6 +321,7 @@ TEST(FastPairDiscoverableScannerImplTest, DoesntInvokeLostCallbackIfDidntInvokeFound) { auto scanner = std::make_unique(); auto repository = std::make_unique(); + 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(kTestBleDeviceAddress, kValidModelId); diff --git a/fastpair/scanning/scanner_broker_impl.cc b/fastpair/scanning/scanner_broker_impl.cc index 372caddf..1f458c40 100644 --- a/fastpair/scanning/scanner_broker_impl.cc +++ b/fastpair/scanning/scanner_broker_impl.cc @@ -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(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(); } diff --git a/fastpair/scanning/scanner_broker_impl.h b/fastpair/scanning/scanner_broker_impl.h index 36774597..85d2b394 100644 --- a/fastpair/scanning/scanner_broker_impl.h +++ b/fastpair/scanning/scanner_broker_impl.h @@ -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 scanner_; std::unique_ptr fast_pair_discoverable_scanner_; ObserverList observers_; + FastPairDeviceRepository* device_repository_; }; } // namespace fastpair diff --git a/fastpair/scanning/scanner_broker_impl_test.cc b/fastpair/scanning/scanner_broker_impl_test.cc index eceddd8c..8d0d5666 100644 --- a/fastpair/scanning/scanner_broker_impl_test.cc +++ b/fastpair/scanning/scanner_broker_impl_test.cc @@ -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(); 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(mediums_1); + auto scanner_broker = + std::make_unique(mediums_1, &devices); CountDownLatch accept_latch(1); CountDownLatch lost_latch(1); ScannerBrokerObserver observer(scanner_broker.get(), &accept_latch,