fast_pair_handshake.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_H_
  5. #define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_H_
  6. #include <memory>
  7. #include "ash/quick_pair/common/pair_failure.h"
  8. #include "base/callback.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace device {
  12. class BluetoothAdapter;
  13. } // namespace device
  14. namespace ash {
  15. namespace quick_pair {
  16. struct Device;
  17. class FastPairDataEncryptor;
  18. class FastPairGattServiceClient;
  19. // This class performs the Fast Pair handshake procedure upon creation and
  20. // calls |on_complete| once finished. It also exposes the
  21. // |FastPairDataEncryptor| and |FastPairGattServiceClient| instances that were
  22. // used during the handshake.
  23. //
  24. // The procedure steps are as follows:
  25. // 1. Create a GATT connection to the device.
  26. // 2. Create a data encryptor instance with the appropriate keys.
  27. // 3. Write the Key-Based Pairing Request to the characteristic
  28. // (https://developers.google.com/nearby/fast-pair/spec#table1.1)
  29. // 4. Decrypt the response.
  30. // 5. Validate response.
  31. // 6. Set classic address field on |Device| instance.
  32. // 7. Complete.
  33. class FastPairHandshake {
  34. public:
  35. using OnCompleteCallback =
  36. base::OnceCallback<void(scoped_refptr<Device>,
  37. absl::optional<PairFailure>)>;
  38. FastPairHandshake(
  39. scoped_refptr<device::BluetoothAdapter> adapter,
  40. scoped_refptr<Device> device,
  41. OnCompleteCallback on_complete,
  42. std::unique_ptr<FastPairDataEncryptor> data_encryptor,
  43. std::unique_ptr<FastPairGattServiceClient> gatt_service_client);
  44. FastPairHandshake(const FastPairHandshake&) = delete;
  45. FastPairHandshake& operator=(const FastPairHandshake&) = delete;
  46. virtual ~FastPairHandshake();
  47. bool completed_successfully() { return completed_successfully_; }
  48. FastPairDataEncryptor* fast_pair_data_encryptor() {
  49. return fast_pair_data_encryptor_.get();
  50. }
  51. FastPairGattServiceClient* fast_pair_gatt_service_client() {
  52. return fast_pair_gatt_service_client_.get();
  53. }
  54. protected:
  55. bool completed_successfully_ = false;
  56. scoped_refptr<device::BluetoothAdapter> adapter_;
  57. scoped_refptr<Device> device_;
  58. OnCompleteCallback on_complete_callback_;
  59. std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor_;
  60. std::unique_ptr<FastPairGattServiceClient> fast_pair_gatt_service_client_;
  61. };
  62. } // namespace quick_pair
  63. } // namespace ash
  64. #endif // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_H_