mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
Refactor FastPairHandshake to avoid locally store fast pair device
PiperOrigin-RevId: 526691078
This commit is contained in:
committed by
Copybara-Service
parent
e4c1f9aa24
commit
b68f5324ff
@@ -49,11 +49,10 @@ class FastPairHandshake {
|
||||
FastPairDevice& device, std::optional<PairFailure> failure)>;
|
||||
|
||||
FastPairHandshake(
|
||||
FastPairDevice& device, OnCompleteCallback on_complete_cb,
|
||||
OnCompleteCallback on_complete_cb,
|
||||
std::unique_ptr<FastPairDataEncryptor> data_encryptor,
|
||||
std::unique_ptr<FastPairGattServiceClient> gatt_service_client)
|
||||
: device_(&device),
|
||||
on_complete_callback_(std::move(on_complete_cb)),
|
||||
: on_complete_callback_(std::move(on_complete_cb)),
|
||||
fast_pair_data_encryptor_(std::move(data_encryptor)),
|
||||
fast_pair_gatt_service_client_(std::move(gatt_service_client)) {}
|
||||
|
||||
@@ -73,7 +72,6 @@ class FastPairHandshake {
|
||||
|
||||
protected:
|
||||
bool completed_successfully_ = false;
|
||||
FastPairDevice* device_;
|
||||
OnCompleteCallback on_complete_callback_;
|
||||
std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor_;
|
||||
std::unique_ptr<FastPairGattServiceClient> fast_pair_gatt_service_client_;
|
||||
|
||||
@@ -32,22 +32,22 @@ namespace fastpair {
|
||||
|
||||
FastPairHandshakeImpl::FastPairHandshakeImpl(FastPairDevice& device,
|
||||
OnCompleteCallback on_complete)
|
||||
: FastPairHandshake(device, std::move(on_complete), nullptr, nullptr) {
|
||||
: FastPairHandshake(std::move(on_complete), nullptr, nullptr) {
|
||||
fast_pair_gatt_service_client_ =
|
||||
FastPairGattServiceClientImpl::Factory::Create(device);
|
||||
fast_pair_gatt_service_client_->InitializeGattConnection(
|
||||
[this](std::optional<PairFailure> failure) {
|
||||
OnGattClientInitializedCallback(failure);
|
||||
[&](std::optional<PairFailure> failure) {
|
||||
OnGattClientInitializedCallback(device, failure);
|
||||
});
|
||||
}
|
||||
|
||||
void FastPairHandshakeImpl::OnGattClientInitializedCallback(
|
||||
std::optional<PairFailure> failure) {
|
||||
FastPairDevice& device, std::optional<PairFailure> failure) {
|
||||
if (failure.has_value()) {
|
||||
NEARBY_LOGS(WARNING) << __func__
|
||||
<< ": Failed to init gatt client with failure = "
|
||||
<< failure.value();
|
||||
std::move(on_complete_callback_)(*device_, failure.value());
|
||||
std::move(on_complete_callback_)(device, failure.value());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,16 +55,19 @@ void FastPairHandshakeImpl::OnGattClientInitializedCallback(
|
||||
<< __func__
|
||||
<< ": Fast Pair GATT service client initialization successful.";
|
||||
FastPairDataEncryptorImpl::Factory::CreateAsync(
|
||||
*device_, absl::bind_front(
|
||||
&FastPairHandshakeImpl::OnDataEncryptorCreateAsync, this));
|
||||
device,
|
||||
[&](std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor) {
|
||||
OnDataEncryptorCreateAsync(device, std::move(fast_pair_data_encryptor));
|
||||
});
|
||||
}
|
||||
|
||||
void FastPairHandshakeImpl::OnDataEncryptorCreateAsync(
|
||||
FastPairDevice& device,
|
||||
std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor) {
|
||||
if (!fast_pair_data_encryptor) {
|
||||
NEARBY_LOGS(WARNING) << __func__
|
||||
<< ": Failed to create Fast Pair Data Encryptor.";
|
||||
std::move(on_complete_callback_)(*device_,
|
||||
std::move(on_complete_callback_)(device,
|
||||
PairFailure::kDataEncryptorRetrieval);
|
||||
return;
|
||||
}
|
||||
@@ -74,21 +77,22 @@ void FastPairHandshakeImpl::OnDataEncryptorCreateAsync(
|
||||
fast_pair_gatt_service_client_->WriteRequestAsync(
|
||||
/*message_type=*/kKeyBasedPairingType,
|
||||
/*flags=*/kInitialOrSubsequentFlags,
|
||||
/*provider_address=*/device_->GetBleAddress(),
|
||||
/*provider_address=*/device.GetBleAddress(),
|
||||
/*seekers_address=*/"", *fast_pair_data_encryptor_,
|
||||
[this](absl::string_view response, std::optional<PairFailure> failure) {
|
||||
OnWriteResponse(response, failure);
|
||||
[&](absl::string_view response, std::optional<PairFailure> failure) {
|
||||
OnWriteResponse(device, response, failure);
|
||||
});
|
||||
}
|
||||
|
||||
void FastPairHandshakeImpl::OnWriteResponse(
|
||||
absl::string_view response, std::optional<PairFailure> failure) {
|
||||
FastPairDevice& device, absl::string_view response,
|
||||
std::optional<PairFailure> failure) {
|
||||
if (failure.has_value()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< __func__
|
||||
<< ": Failed during key-based pairing protocol with failure = "
|
||||
<< failure.value();
|
||||
std::move(on_complete_callback_)(*device_, failure.value());
|
||||
std::move(on_complete_callback_)(device, failure.value());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -98,33 +102,33 @@ void FastPairHandshakeImpl::OnWriteResponse(
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< __func__ << ": Handshake failed because of incorrect response size.";
|
||||
std::move(on_complete_callback_)(
|
||||
*device_, PairFailure::kKeybasedPairingResponseDecryptFailure);
|
||||
device, PairFailure::kKeybasedPairingResponseDecryptFailure);
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> response_bytes(response.begin(), response.end());
|
||||
fast_pair_data_encryptor_->ParseDecryptResponse(
|
||||
response_bytes, [this](std::optional<DecryptedResponse> response) {
|
||||
OnParseDecryptedResponse(response);
|
||||
response_bytes, [&](std::optional<DecryptedResponse> response) {
|
||||
OnParseDecryptedResponse(device, response);
|
||||
});
|
||||
}
|
||||
|
||||
void FastPairHandshakeImpl::OnParseDecryptedResponse(
|
||||
std::optional<DecryptedResponse>& response) {
|
||||
FastPairDevice& device, std::optional<DecryptedResponse>& response) {
|
||||
if (!response.has_value()) {
|
||||
NEARBY_LOGS(WARNING) << __func__
|
||||
<< ": Missing decrypted response from parse.";
|
||||
std::move(on_complete_callback_)(
|
||||
*device_, PairFailure::kKeybasedPairingResponseDecryptFailure);
|
||||
device, PairFailure::kKeybasedPairingResponseDecryptFailure);
|
||||
return;
|
||||
}
|
||||
NEARBY_LOGS(INFO) << __func__
|
||||
<< ": Successfully decrypted and parsed response.";
|
||||
|
||||
device_->set_public_address(
|
||||
device.set_public_address(
|
||||
device::CanonicalizeBluetoothAddress(response->address_bytes));
|
||||
completed_successfully_ = true;
|
||||
std::move(on_complete_callback_)(*device_, absl::nullopt);
|
||||
std::move(on_complete_callback_)(device, absl::nullopt);
|
||||
}
|
||||
|
||||
} // namespace fastpair
|
||||
|
||||
@@ -33,12 +33,15 @@ class FastPairHandshakeImpl : public FastPairHandshake {
|
||||
FastPairHandshakeImpl& operator=(const FastPairHandshakeImpl&) = delete;
|
||||
|
||||
private:
|
||||
void OnGattClientInitializedCallback(std::optional<PairFailure> failure);
|
||||
void OnGattClientInitializedCallback(FastPairDevice& device,
|
||||
std::optional<PairFailure> failure);
|
||||
void OnDataEncryptorCreateAsync(
|
||||
FastPairDevice& device,
|
||||
std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor);
|
||||
void OnWriteResponse(absl::string_view response,
|
||||
void OnWriteResponse(FastPairDevice& device, absl::string_view response,
|
||||
std::optional<PairFailure> failure);
|
||||
void OnParseDecryptedResponse(std::optional<DecryptedResponse>& response);
|
||||
void OnParseDecryptedResponse(FastPairDevice& device,
|
||||
std::optional<DecryptedResponse>& response);
|
||||
};
|
||||
|
||||
} // namespace fastpair
|
||||
|
||||
Reference in New Issue
Block a user