Add pair_broker to FastPairMediator/FastPairSeeker

PiperOrigin-RevId: 538592398
This commit is contained in:
Qin Wang
2023-06-07 14:27:29 -07:00
committed by Copybara-Service
parent f5e67ed034
commit c786b07fcf
10 changed files with 111 additions and 17 deletions
+1
View File
@@ -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",
@@ -15,16 +15,26 @@
#include "fastpair/internal/fast_pair_seeker_impl.h"
#include <memory>
#include <optional>
#include <utility>
#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<PairerBrokerImpl>(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<PairFailure> 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
+14 -7
View File
@@ -16,11 +16,13 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_DEFAULT_FAST_PAIR_SEEKER_H_
#include <memory>
#include <optional>
#include <utility>
#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<void(const FastPairDevice&, InitialDiscoveryEvent)>
@@ -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<PairFailure> 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<ScannerBrokerImpl> scanner_;
std::unique_ptr<ScannerBrokerImpl::ScanningSession> scanning_session_;
std::unique_ptr<PairerBroker> pairer_broker_;
FastPairDevice* test_device_ = nullptr;
};
+1
View File
@@ -29,6 +29,7 @@ cc_library(
deps = [
"//fastpair/common",
"//fastpair/internal/mediums",
"//fastpair/pairing",
"//fastpair/repository:device_repository",
"//fastpair/scanning:scanner",
"//fastpair/server_access",
+35 -2
View File
@@ -15,10 +15,12 @@
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include <memory>
#include <optional>
#include <utility>
#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<FastPairDeviceRepository>(executor_.get());
scanner_broker_ = std::make_unique<ScannerBrokerImpl>(
*mediums_, executor_.get(), devices_.get());
pairer_broker_ =
std::make_unique<PairerBrokerImpl>(*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<PairFailure> 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_ =
+14 -2
View File
@@ -16,9 +16,12 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
#include <memory>
#include <optional>
#include <utility>
#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> mediums, std::unique_ptr<UIBroker> 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<PairFailure> 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<ScannerBroker> scanner_broker_;
std::unique_ptr<ScannerBroker::ScanningSession> scanning_session_;
std::unique_ptr<UIBroker> ui_broker_;
std::unique_ptr<PairerBroker> pairer_broker_;
std::unique_ptr<FastPairNotificationController> notification_controller_;
std::unique_ptr<FastPairRepository> fast_pair_repository_;
std::unique_ptr<SingleThreadExecutor> executor_;
@@ -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,
+1 -2
View File
@@ -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);
}
+1 -1
View File
@@ -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;
};
+1 -1
View File
@@ -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;