diff --git a/fastpair/internal/BUILD b/fastpair/internal/BUILD index eb236378..95a81e29 100644 --- a/fastpair/internal/BUILD +++ b/fastpair/internal/BUILD @@ -14,6 +14,7 @@ cc_library( "//fastpair:fast_pair_events", "//fastpair:fast_pair_seeker", "//fastpair/internal/mediums", + "//fastpair/pairing", "//fastpair/repository:device_repository", "//fastpair/scanning:scanner", "//internal/platform:types", diff --git a/fastpair/internal/fast_pair_seeker_impl.cc b/fastpair/internal/fast_pair_seeker_impl.cc index 34806e33..ad148453 100644 --- a/fastpair/internal/fast_pair_seeker_impl.cc +++ b/fastpair/internal/fast_pair_seeker_impl.cc @@ -15,16 +15,26 @@ #include "fastpair/internal/fast_pair_seeker_impl.h" #include +#include #include #include "absl/status/status.h" #include "fastpair/fast_pair_events.h" +#include "fastpair/pairing/pairer_broker_impl.h" #include "fastpair/scanning/scanner_broker_impl.h" #include "internal/platform/single_thread_executor.h" namespace nearby { namespace fastpair { +FastPairSeekerImpl::FastPairSeekerImpl(ServiceCallbacks callbacks, + SingleThreadExecutor* executor, + FastPairDeviceRepository* devices) + : callbacks_(std::move(callbacks)), executor_(executor), devices_(devices) { + pairer_broker_ = std::make_unique(mediums_, executor_); + pairer_broker_->AddObserver(this); +} + absl::Status FastPairSeekerImpl::StartInitialPairing( const FastPairDevice& device, const InitialPairingParam& params, PairingCallback callback) { @@ -75,5 +85,37 @@ void FastPairSeekerImpl::OnDeviceLost(FastPairDevice& device) { NEARBY_LOGS(INFO) << "Device lost: " << device; } +// PairerBroker:Observer::OnDevicePaired +void FastPairSeekerImpl::OnDevicePaired(FastPairDevice& device) { + NEARBY_LOGS(INFO) << __func__ << ": " << device; +} + +// PairerBroker:Observer::OnAccountKeyWrite +void FastPairSeekerImpl::OnAccountKeyWrite(FastPairDevice& device, + std::optional error) { + if (error.has_value()) { + NEARBY_LOGS(INFO) << __func__ << ": Device=" << device + << ",Error=" << error.value(); + return; + } + + NEARBY_LOGS(INFO) << __func__ << ": Device=" << device; + if (device.GetProtocol() == Protocol::kFastPairRetroactivePairing) { + // TODO: UI ShowAssociateAccount + } +} + +// PairerBroker:Observer::OnPairingComplete +void FastPairSeekerImpl::OnPairingComplete(FastPairDevice& device) { + NEARBY_LOGS(INFO) << __func__ << ": " << device; +} + +// PairerBroker:Observer::OnPairFailure +void FastPairSeekerImpl::OnPairFailure(FastPairDevice& device, + PairFailure failure) { + NEARBY_LOGS(INFO) << __func__ << ": " << device + << " with PairFailure: " << failure; +} + } // namespace fastpair } // namespace nearby diff --git a/fastpair/internal/fast_pair_seeker_impl.h b/fastpair/internal/fast_pair_seeker_impl.h index e0a3bdfb..c180353d 100644 --- a/fastpair/internal/fast_pair_seeker_impl.h +++ b/fastpair/internal/fast_pair_seeker_impl.h @@ -16,11 +16,13 @@ #define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_DEFAULT_FAST_PAIR_SEEKER_H_ #include +#include #include #include "fastpair/fast_pair_events.h" #include "fastpair/fast_pair_seeker.h" #include "fastpair/internal/mediums/mediums.h" +#include "fastpair/pairing/pairer_broker.h" #include "fastpair/repository/fast_pair_device_repository.h" #include "fastpair/scanning/scanner_broker_impl.h" #include "internal/platform/single_thread_executor.h" @@ -37,7 +39,8 @@ class FastPairSeekerExt : public FastPairSeeker { }; class FastPairSeekerImpl : public FastPairSeekerExt, - ScannerBrokerImpl::Observer { + ScannerBrokerImpl::Observer, + PairerBroker::Observer { public: struct ServiceCallbacks { absl::AnyInvocable @@ -53,10 +56,7 @@ class FastPairSeekerImpl : public FastPairSeekerExt, }; FastPairSeekerImpl(ServiceCallbacks callbacks, SingleThreadExecutor* executor, - FastPairDeviceRepository* devices) - : callbacks_(std::move(callbacks)), - executor_(executor), - devices_(devices) {} + FastPairDeviceRepository* devices); // From FastPairSeeker. absl::Status StartInitialPairing(const FastPairDevice& device, @@ -75,19 +75,26 @@ class FastPairSeekerImpl : public FastPairSeekerExt, absl::Status StartFastPairScan() override; absl::Status StopFastPairScan() override; + // Internal methods, not exported to plugins. + private: // From ScannerBrokerImpl::Observer. void OnDeviceFound(FastPairDevice& device) override; void OnDeviceLost(FastPairDevice& device) override; - // Internal methods, not exported to plugins. + // From PairerBroker:Observer + void OnDevicePaired(FastPairDevice& device) override; + void OnAccountKeyWrite(FastPairDevice& device, + std::optional error) override; + void OnPairingComplete(FastPairDevice& device) override; + void OnPairFailure(FastPairDevice& device, PairFailure failure) override; - private: ServiceCallbacks callbacks_; SingleThreadExecutor* executor_; FastPairDeviceRepository* devices_; Mediums mediums_; std::unique_ptr scanner_; std::unique_ptr scanning_session_; + std::unique_ptr pairer_broker_; FastPairDevice* test_device_ = nullptr; }; diff --git a/fastpair/keyed_service/BUILD b/fastpair/keyed_service/BUILD index 4992e71a..a3964296 100644 --- a/fastpair/keyed_service/BUILD +++ b/fastpair/keyed_service/BUILD @@ -29,6 +29,7 @@ cc_library( deps = [ "//fastpair/common", "//fastpair/internal/mediums", + "//fastpair/pairing", "//fastpair/repository:device_repository", "//fastpair/scanning:scanner", "//fastpair/server_access", diff --git a/fastpair/keyed_service/fast_pair_mediator.cc b/fastpair/keyed_service/fast_pair_mediator.cc index 005b4acb..c90190ab 100644 --- a/fastpair/keyed_service/fast_pair_mediator.cc +++ b/fastpair/keyed_service/fast_pair_mediator.cc @@ -15,10 +15,12 @@ #include "fastpair/keyed_service/fast_pair_mediator.h" #include +#include #include #include "fastpair/common/protocol.h" #include "fastpair/internal/mediums/mediums.h" +#include "fastpair/pairing/pairer_broker_impl.h" #include "fastpair/repository/fast_pair_device_repository.h" #include "fastpair/scanning/scanner_broker_impl.h" #include "fastpair/server_access/fast_pair_repository_impl.h" @@ -44,8 +46,11 @@ Mediator::Mediator( devices_ = std::make_unique(executor_.get()); scanner_broker_ = std::make_unique( *mediums_, executor_.get(), devices_.get()); + pairer_broker_ = + std::make_unique(*mediums_, executor_.get()); scanner_broker_->AddObserver(this); ui_broker_->AddObserver(this); + pairer_broker_->AddObserver(this); } void Mediator::OnDeviceFound(FastPairDevice& device) { @@ -73,14 +78,14 @@ void Mediator::OnDeviceLost(FastPairDevice& device) { NEARBY_LOGS(INFO) << __func__ << ": " << device; } -void Mediator::OnDiscoveryAction(const FastPairDevice& device, +void Mediator::OnDiscoveryAction(FastPairDevice& device, DiscoveryAction action) { switch (action) { case DiscoveryAction::kPairToDevice: NEARBY_LOGS(INFO) << __func__ << ": Action = kPairToDevice"; // TODO(285451051): Adding show pairing for higher than v1 version in ui // broker - // TODO(282022590): Adding pairer broker to pair device + pairer_broker_->PairDevice(device); break; case DiscoveryAction::kDismissedByOs: NEARBY_LOGS(INFO) << __func__ << ": Action = kDismissedByOs"; @@ -104,6 +109,34 @@ void Mediator::OnDiscoveryAction(const FastPairDevice& device, } } +void Mediator::OnDevicePaired(FastPairDevice& device) { + NEARBY_LOGS(INFO) << __func__ << ": " << device; +} + +void Mediator::OnAccountKeyWrite(FastPairDevice& device, + std::optional error) { + if (error.has_value()) { + NEARBY_LOGS(INFO) << __func__ << ": Device=" << device + << ",Error=" << error.value(); + return; + } + + NEARBY_LOGS(INFO) << __func__ << ": Device=" << device; + if (device.GetProtocol() == Protocol::kFastPairRetroactivePairing) { + // TODO: UI ShowAssociateAccount + } +} + +void Mediator::OnPairingComplete(FastPairDevice& device) { + NEARBY_LOGS(INFO) << __func__ << ": " << device; +} + +void Mediator::OnPairFailure(FastPairDevice& device, PairFailure failure) { + NEARBY_LOGS(INFO) << __func__ << ": " << device + << " with PairFailure: " << failure; + // TODO: UI showPairingFailed +} + void Mediator::StartScanning() { if (IsFastPairEnabled()) { scanning_session_ = diff --git a/fastpair/keyed_service/fast_pair_mediator.h b/fastpair/keyed_service/fast_pair_mediator.h index d38f0068..c4886ae7 100644 --- a/fastpair/keyed_service/fast_pair_mediator.h +++ b/fastpair/keyed_service/fast_pair_mediator.h @@ -16,9 +16,12 @@ #define THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_ #include +#include +#include #include "fastpair/common/fast_pair_device.h" #include "fastpair/internal/mediums/mediums.h" +#include "fastpair/pairing/pairer_broker.h" #include "fastpair/repository/fast_pair_device_repository.h" #include "fastpair/scanning/scanner_broker.h" #include "fastpair/server_access/fast_pair_repository.h" @@ -31,7 +34,8 @@ namespace fastpair { // Implements the Mediator design pattern for the components in the Fast Pair class Mediator final : public ScannerBroker::Observer, - public UIBroker::Observer { + public UIBroker::Observer, + public PairerBroker::Observer { public: Mediator( std::unique_ptr mediums, std::unique_ptr ui_broker, @@ -63,9 +67,16 @@ class Mediator final : public ScannerBroker::Observer, void StartScanning(); // UIBroker::Observer - void OnDiscoveryAction(const FastPairDevice& device, + void OnDiscoveryAction(FastPairDevice& device, DiscoveryAction action) override; + // PairBroker:Observer + void OnDevicePaired(FastPairDevice& device) override; + void OnAccountKeyWrite(FastPairDevice& device, + std::optional error) override; + void OnPairingComplete(FastPairDevice& device) override; + void OnPairFailure(FastPairDevice& device, PairFailure failure) override; + private: bool IsFastPairEnabled(); @@ -79,6 +90,7 @@ class Mediator final : public ScannerBroker::Observer, std::unique_ptr scanner_broker_; std::unique_ptr scanning_session_; std::unique_ptr ui_broker_; + std::unique_ptr pairer_broker_; std::unique_ptr notification_controller_; std::unique_ptr fast_pair_repository_; std::unique_ptr executor_; diff --git a/fastpair/keyed_service/fast_pair_mediator_test.cc b/fastpair/keyed_service/fast_pair_mediator_test.cc index 8e8cee0d..22452052 100644 --- a/fastpair/keyed_service/fast_pair_mediator_test.cc +++ b/fastpair/keyed_service/fast_pair_mediator_test.cc @@ -42,7 +42,6 @@ namespace { constexpr absl::string_view kModelId = "718c17"; constexpr absl::string_view kServiceID = "Fast Pair"; -constexpr absl::Duration kTaskWaitTimeout = absl::Milliseconds(1000); constexpr absl::string_view kFastPairServiceUuid = "0000FE2C-0000-1000-8000-00805F9B34FB"; constexpr absl::string_view kPublicAntiSpoof = @@ -252,7 +251,7 @@ TEST_F(MediatorTest, OnDiscoveryActionClicked) { service_id, advertisement_bytes, fast_pair_service_uuid); mediator_->StartScanning(); - done.WaitForNotificationWithTimeout(kTaskWaitTimeout); + done.WaitForNotification(); FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); mock_ui_broker_->NotifyDiscoveryAction(device, diff --git a/fastpair/ui/mock_ui_broker.h b/fastpair/ui/mock_ui_broker.h index 9e0aa60e..f0467eae 100644 --- a/fastpair/ui/mock_ui_broker.h +++ b/fastpair/ui/mock_ui_broker.h @@ -37,8 +37,7 @@ class MockUIBroker : public UIBroker { observers_.RemoveObserver(observer); } - void NotifyDiscoveryAction(const FastPairDevice& device, - DiscoveryAction action) { + void NotifyDiscoveryAction(FastPairDevice& device, DiscoveryAction action) { for (auto& observer : observers_.GetObservers()) observer->OnDiscoveryAction(device, action); } diff --git a/fastpair/ui/ui_broker.h b/fastpair/ui/ui_broker.h index 405a6b2d..6a414234 100644 --- a/fastpair/ui/ui_broker.h +++ b/fastpair/ui/ui_broker.h @@ -31,7 +31,7 @@ class UIBroker { class Observer { public: virtual ~Observer() = default; - virtual void OnDiscoveryAction(const FastPairDevice& device, + virtual void OnDiscoveryAction(FastPairDevice& device, DiscoveryAction action) = 0; }; diff --git a/fastpair/ui/ui_broker_impl_test.cc b/fastpair/ui/ui_broker_impl_test.cc index 35295bef..70600c86 100644 --- a/fastpair/ui/ui_broker_impl_test.cc +++ b/fastpair/ui/ui_broker_impl_test.cc @@ -39,7 +39,7 @@ class UIBrokerImplTest : public ::testing::Test, public UIBroker::Observer { ui_broker_->AddObserver(this); } - void OnDiscoveryAction(const FastPairDevice& device, + void OnDiscoveryAction(FastPairDevice& device, DiscoveryAction action) override { on_discovery_action_notified_ = true; discovery_action_ = action;