diff --git a/fastpair/handshake/fast_pair_handshake.h b/fastpair/handshake/fast_pair_handshake.h index c42da161..73b1e730 100644 --- a/fastpair/handshake/fast_pair_handshake.h +++ b/fastpair/handshake/fast_pair_handshake.h @@ -49,11 +49,10 @@ class FastPairHandshake { FastPairDevice& device, std::optional failure)>; FastPairHandshake( - FastPairDevice& device, OnCompleteCallback on_complete_cb, + OnCompleteCallback on_complete_cb, std::unique_ptr data_encryptor, std::unique_ptr 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 fast_pair_data_encryptor_; std::unique_ptr fast_pair_gatt_service_client_; diff --git a/fastpair/handshake/fast_pair_handshake_impl.cc b/fastpair/handshake/fast_pair_handshake_impl.cc index a2d6a2dc..16317315 100644 --- a/fastpair/handshake/fast_pair_handshake_impl.cc +++ b/fastpair/handshake/fast_pair_handshake_impl.cc @@ -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 failure) { - OnGattClientInitializedCallback(failure); + [&](std::optional failure) { + OnGattClientInitializedCallback(device, failure); }); } void FastPairHandshakeImpl::OnGattClientInitializedCallback( - std::optional failure) { + FastPairDevice& device, std::optional 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 fast_pair_data_encryptor) { + OnDataEncryptorCreateAsync(device, std::move(fast_pair_data_encryptor)); + }); } void FastPairHandshakeImpl::OnDataEncryptorCreateAsync( + FastPairDevice& device, std::unique_ptr 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 failure) { - OnWriteResponse(response, failure); + [&](absl::string_view response, std::optional failure) { + OnWriteResponse(device, response, failure); }); } void FastPairHandshakeImpl::OnWriteResponse( - absl::string_view response, std::optional failure) { + FastPairDevice& device, absl::string_view response, + std::optional 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 response_bytes(response.begin(), response.end()); fast_pair_data_encryptor_->ParseDecryptResponse( - response_bytes, [this](std::optional response) { - OnParseDecryptedResponse(response); + response_bytes, [&](std::optional response) { + OnParseDecryptedResponse(device, response); }); } void FastPairHandshakeImpl::OnParseDecryptedResponse( - std::optional& response) { + FastPairDevice& device, std::optional& 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 diff --git a/fastpair/handshake/fast_pair_handshake_impl.h b/fastpair/handshake/fast_pair_handshake_impl.h index c41bbc30..e38fc688 100644 --- a/fastpair/handshake/fast_pair_handshake_impl.h +++ b/fastpair/handshake/fast_pair_handshake_impl.h @@ -33,12 +33,15 @@ class FastPairHandshakeImpl : public FastPairHandshake { FastPairHandshakeImpl& operator=(const FastPairHandshakeImpl&) = delete; private: - void OnGattClientInitializedCallback(std::optional failure); + void OnGattClientInitializedCallback(FastPairDevice& device, + std::optional failure); void OnDataEncryptorCreateAsync( + FastPairDevice& device, std::unique_ptr fast_pair_data_encryptor); - void OnWriteResponse(absl::string_view response, + void OnWriteResponse(FastPairDevice& device, absl::string_view response, std::optional failure); - void OnParseDecryptedResponse(std::optional& response); + void OnParseDecryptedResponse(FastPairDevice& device, + std::optional& response); }; } // namespace fastpair