fast_pair_handshake_impl.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_impl.h"
  5. #include "ash/quick_pair/common/fast_pair/fast_pair_metrics.h"
  6. #include "ash/quick_pair/common/logging.h"
  7. #include "ash/quick_pair/common/pair_failure.h"
  8. #include "ash/quick_pair/common/protocol.h"
  9. #include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor_impl.h"
  10. #include "ash/quick_pair/fast_pair_handshake/fast_pair_gatt_service_client_impl.h"
  11. #include "ash/services/quick_pair/public/cpp/decrypted_response.h"
  12. #include "base/callback.h"
  13. #include "device/bluetooth/bluetooth_adapter.h"
  14. #include "device/bluetooth/public/cpp/bluetooth_address.h"
  15. namespace ash {
  16. namespace quick_pair {
  17. constexpr uint8_t kKeyBasedPairingType = 0x00;
  18. constexpr uint8_t kInitialOrSubsequentFlags = 0x00;
  19. constexpr uint8_t kRetroactiveFlags = 0x10;
  20. FastPairHandshakeImpl::FastPairHandshakeImpl(
  21. scoped_refptr<device::BluetoothAdapter> adapter,
  22. scoped_refptr<Device> device,
  23. OnCompleteCallback on_complete)
  24. : FastPairHandshake(std::move(adapter),
  25. std::move(device),
  26. std::move(on_complete),
  27. nullptr,
  28. nullptr) {
  29. device::BluetoothDevice* bluetooth_device =
  30. adapter_->GetDevice(device_->ble_address);
  31. if (!bluetooth_device) {
  32. QP_LOG(INFO) << __func__ << ": Lost device before starting handshake.";
  33. std::move(on_complete_callback_)
  34. .Run(device_, PairFailure::kPairingDeviceLost);
  35. return;
  36. }
  37. fast_pair_gatt_service_client_ =
  38. FastPairGattServiceClientImpl::Factory::Create(
  39. bluetooth_device, adapter_,
  40. base::BindRepeating(
  41. &FastPairHandshakeImpl::OnGattClientInitializedCallback,
  42. weak_ptr_factory_.GetWeakPtr()));
  43. }
  44. FastPairHandshakeImpl::~FastPairHandshakeImpl() = default;
  45. void FastPairHandshakeImpl::OnGattClientInitializedCallback(
  46. absl::optional<PairFailure> failure) {
  47. if (failure) {
  48. QP_LOG(WARNING) << __func__
  49. << ": Failed to init gatt client with failure = "
  50. << failure.value();
  51. std::move(on_complete_callback_).Run(device_, failure.value());
  52. RecordHandshakeResult(/*success=*/false);
  53. RecordHandshakeFailureReason(HandshakeFailureReason::kFailedGattInit);
  54. return;
  55. }
  56. FastPairDataEncryptorImpl::Factory::CreateAsync(
  57. device_,
  58. base::BindOnce(&FastPairHandshakeImpl::OnDataEncryptorCreateAsync,
  59. weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));
  60. }
  61. void FastPairHandshakeImpl::OnDataEncryptorCreateAsync(
  62. base::TimeTicks encryptor_create_start_time,
  63. std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor) {
  64. bool success = fast_pair_data_encryptor != nullptr;
  65. RecordDataEncryptorCreateResult(/*success=*/success);
  66. if (!fast_pair_data_encryptor) {
  67. QP_LOG(WARNING) << __func__
  68. << ": Fast Pair Data Encryptor failed to be created.";
  69. std::move(on_complete_callback_)
  70. .Run(device_, PairFailure::kDataEncryptorRetrieval);
  71. RecordHandshakeResult(/*success=*/false);
  72. RecordHandshakeFailureReason(
  73. HandshakeFailureReason::kFailedCreateEncryptor);
  74. return;
  75. }
  76. fast_pair_data_encryptor_ = std::move(fast_pair_data_encryptor);
  77. QP_LOG(INFO) << __func__
  78. << ": Fast Pair GATT service client initialization successful.";
  79. RecordTotalDataEncryptorCreateTime(base::TimeTicks::Now() -
  80. encryptor_create_start_time);
  81. bool is_retroactive = device_->protocol == Protocol::kFastPairRetroactive;
  82. fast_pair_gatt_service_client_->WriteRequestAsync(
  83. /*message_type=*/kKeyBasedPairingType,
  84. /*flags=*/is_retroactive ? kRetroactiveFlags : kInitialOrSubsequentFlags,
  85. /*provider_address=*/device_->ble_address,
  86. /*seekers_address=*/is_retroactive ? adapter_->GetAddress() : "",
  87. fast_pair_data_encryptor_.get(),
  88. base::BindOnce(&FastPairHandshakeImpl::OnWriteResponse,
  89. weak_ptr_factory_.GetWeakPtr()));
  90. }
  91. void FastPairHandshakeImpl::OnWriteResponse(
  92. std::vector<uint8_t> response_bytes,
  93. absl::optional<PairFailure> failure) {
  94. RecordWriteKeyBasedCharacteristicResult(/*success=*/!failure.has_value());
  95. if (failure) {
  96. QP_LOG(WARNING) << __func__
  97. << ": Failed to write request: " << failure.value();
  98. RecordWriteKeyBasedCharacteristicPairFailure(failure.value());
  99. RecordHandshakeResult(/*success=*/false);
  100. RecordHandshakeFailureReason(HandshakeFailureReason::kFailedWriteResponse);
  101. std::move(on_complete_callback_).Run(device_, failure.value());
  102. return;
  103. }
  104. QP_LOG(INFO) << __func__ << ": Successfully wrote response.";
  105. fast_pair_data_encryptor_->ParseDecryptedResponse(
  106. response_bytes,
  107. base::BindOnce(&FastPairHandshakeImpl::OnParseDecryptedResponse,
  108. weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));
  109. }
  110. void FastPairHandshakeImpl::OnParseDecryptedResponse(
  111. base::TimeTicks decrypt_start_time,
  112. const absl::optional<DecryptedResponse>& response) {
  113. if (!response) {
  114. QP_LOG(WARNING) << __func__ << ": Missing decrypted response from parse.";
  115. std::move(on_complete_callback_)
  116. .Run(device_, PairFailure::kKeybasedPairingResponseDecryptFailure);
  117. RecordKeyBasedCharacteristicDecryptResult(/*success=*/false);
  118. RecordHandshakeResult(/*success=*/false);
  119. RecordHandshakeFailureReason(
  120. HandshakeFailureReason::kFailedDecryptResponse);
  121. return;
  122. }
  123. if (response->message_type != FastPairMessageType::kKeyBasedPairingResponse) {
  124. QP_LOG(WARNING) << __func__
  125. << ": Incorrect message type from decrypted response.";
  126. std::move(on_complete_callback_)
  127. .Run(device_, PairFailure::kIncorrectKeyBasedPairingResponseType);
  128. RecordKeyBasedCharacteristicDecryptResult(/*success=*/false);
  129. RecordHandshakeResult(/*success=*/false);
  130. RecordHandshakeFailureReason(
  131. HandshakeFailureReason::kFailedIncorrectResponseType);
  132. return;
  133. }
  134. RecordKeyBasedCharacteristicDecryptTime(base::TimeTicks::Now() -
  135. decrypt_start_time);
  136. RecordKeyBasedCharacteristicDecryptResult(/*success=*/true);
  137. std::string device_address =
  138. device::CanonicalizeBluetoothAddress(response->address_bytes);
  139. device_->set_classic_address(device_address);
  140. completed_successfully_ = true;
  141. RecordHandshakeResult(/*success=*/true);
  142. std::move(on_complete_callback_).Run(device_, absl::nullopt);
  143. }
  144. } // namespace quick_pair
  145. } // namespace ash