File: fast_pair_handshake.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_H_
#define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_H_

#include <memory>
#include <optional>

#include "ash/quick_pair/common/pair_failure.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"

namespace device {
class BluetoothAdapter;
}  // namespace device

namespace ash {
namespace quick_pair {

class Device;
class FastPairDataEncryptor;
class FastPairGattServiceClient;

// This class performs the Fast Pair handshake procedure upon creation and
// calls |on_complete| once finished. It also exposes the
// |FastPairDataEncryptor| and |FastPairGattServiceClient| instances that were
// used during the handshake.
//
// The procedure steps are as follows:
//  1. Create a GATT connection to the device.
//  2. Create a data encryptor instance with the appropriate keys.
//  3. Write the Key-Based Pairing Request to the characteristic
//  (https://developers.google.com/nearby/fast-pair/spec#table1.1)
//  4. Decrypt the response.
//  5. Validate response.
//  6. Set classic address field on |Device| instance.
//  7. Complete.
class FastPairHandshake {
 public:
  using OnCompleteCallback =
      base::OnceCallback<void(scoped_refptr<Device>,
                              std::optional<PairFailure>)>;
  using OnCompleteCallbackNew = base::OnceCallback<void(scoped_refptr<Device>)>;
  using OnFailureCallback =
      base::OnceCallback<void(std::optional<PairFailure>)>;
  using OnBleAddressRotationCallback = base::OnceClosure;

  // TODO(b/265853116): After the Fast Pair Handshake code is refactored remove
  // this constructor in favor of the two argument constructor below.
  FastPairHandshake(
      scoped_refptr<device::BluetoothAdapter> adapter,
      scoped_refptr<Device> device,
      OnCompleteCallback on_complete,
      std::unique_ptr<FastPairDataEncryptor> data_encryptor,
      std::unique_ptr<FastPairGattServiceClient> gatt_service_client);
  FastPairHandshake(scoped_refptr<device::BluetoothAdapter> adapter,
                    scoped_refptr<Device> device);
  FastPairHandshake(const FastPairHandshake&) = delete;
  FastPairHandshake& operator=(const FastPairHandshake&) = delete;
  virtual ~FastPairHandshake();

  virtual void SetUpHandshake(OnFailureCallback on_failure_callback,
                              OnCompleteCallbackNew on_success_callback) = 0;
  virtual void Reset() = 0;

  bool completed_successfully() { return completed_successfully_; }

  void set_completed_successfully() { completed_successfully_ = true; }

  FastPairDataEncryptor* fast_pair_data_encryptor() {
    return fast_pair_data_encryptor_.get();
  }

  void BleAddressRotated(OnBleAddressRotationCallback callback) {
    on_ble_address_rotation_callback_ = std::move(callback);
  }

  bool DidBleAddressRotate() {
    return !on_ble_address_rotation_callback_.is_null();
  }

  void RunBleAddressRotationCallback() {
    return std::move(on_ble_address_rotation_callback_).Run();
  }

 protected:
  bool completed_successfully_ = false;
  scoped_refptr<device::BluetoothAdapter> adapter_;
  scoped_refptr<Device> device_;
  // TODO(b/265853116): Audit FastPairHandshake code once refactor is completed
  // and legacy code is removed.
  OnCompleteCallback on_complete_callback_;
  OnCompleteCallbackNew on_complete_callback_new_;
  OnFailureCallback on_failure_callback_;
  std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor_;

  // This callback will only be set if a BLE Address rotation happens during a
  // retroactive pair. The callback being set signals that a rotation
  // happened, if the callback has no value, a rotation did not occur.
  OnBleAddressRotationCallback on_ble_address_rotation_callback_;
};

}  // namespace quick_pair
}  // namespace ash

#endif  // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_HANDSHAKE_H_