summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_pairing_bluez.h
blob: 85113bf7116db66f454fa053c3e4a1f1446f514f (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_BLUEZ_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_BLUEZ_H_

#include <stdint.h>

#include "base/macros.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/dbus/bluetooth_agent_service_provider.h"

namespace bluez {

class BluetoothDeviceBlueZ;

// The BluetoothPairingBlueZ class encapsulates the logic for an individual
// device pairing, acting as a bridge between BluetoothAdapterBlueZ which
// communicates with the underlying Controller and Host Subsystem, and
// BluetoothDeviceBlueZ which presents the pairing logic to the application.
class BluetoothPairingBlueZ {
 public:
  BluetoothPairingBlueZ(
      BluetoothDeviceBlueZ* device,
      device::BluetoothDevice::PairingDelegate* pairing_delegate);
  ~BluetoothPairingBlueZ();

  // Indicates whether the device is currently pairing and expecting a
  // Passkey to be returned.
  bool ExpectingPasskey() const;

  // Indicates whether the device is currently pairing and expecting
  // confirmation of a displayed passkey.
  bool ExpectingConfirmation() const;

  // Requests a PIN code for the current device from the current pairing
  // delegate, the SetPinCode(), RejectPairing() and CancelPairing() method
  // calls on this object are translated into the appropriate response to
  // |callback|.
  void RequestPinCode(
      const bluez::BluetoothAgentServiceProvider::Delegate::PinCodeCallback&
          callback);

  // Indicates whether the device is currently pairing and expecting a
  // PIN Code to be returned.
  bool ExpectingPinCode() const;

  // Sends the PIN code |pincode| to the remote device during pairing.
  //
  // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
  // for which there is no automatic pairing or special handling.
  void SetPinCode(const std::string& pincode);

  // Requests a PIN code for the current device be displayed by the current
  // pairing delegate. No response is expected from the delegate.
  void DisplayPinCode(const std::string& pincode);

  // Requests a Passkey for the current device from the current pairing
  // delegate, the SetPasskey(), RejectPairing() and CancelPairing() method
  // calls on this object are translated into the appropriate response to
  // |callback|.
  void RequestPasskey(
      const bluez::BluetoothAgentServiceProvider::Delegate::PasskeyCallback&
          callback);

  // Sends the Passkey |passkey| to the remote device during pairing.
  //
  // Passkeys are generally required for Bluetooth 2.1 and later devices
  // which cannot provide input or display on their own, and don't accept
  // passkey-less pairing, and are a numeric in the range 0-999999.
  void SetPasskey(uint32_t passkey);

  // Requests a Passkey for the current device be displayed by the current
  // pairing delegate. No response is expected from the delegate.
  void DisplayPasskey(uint32_t passkey);

  // Informs the current pairing delegate that |entered| keys have been
  // provided to the remote device since the DisplayPasskey() call. No
  // response is expected from the delegate.
  void KeysEntered(uint16_t entered);

  // Requests confirmation that |passkey| is displayed on the current device
  // from the current pairing delegate. The ConfirmPairing(), RejectPairing()
  // and CancelPairing() method calls on this object are translated into the
  // appropriate response to |callback|.
  void RequestConfirmation(uint32_t passkey,
                           const bluez::BluetoothAgentServiceProvider::
                               Delegate::ConfirmationCallback& callback);

  // Requests authorization that the current device be allowed to pair with
  // this device from the current pairing delegate. The ConfirmPairing(),
  // RejectPairing() and CancelPairing() method calls on this object are
  // translated into the appropriate response to |callback|.
  void RequestAuthorization(const bluez::BluetoothAgentServiceProvider::
                                Delegate::ConfirmationCallback& callback);

  // Confirms to the remote device during pairing that a passkey provided by
  // the ConfirmPasskey() delegate call is displayed on both devices.
  void ConfirmPairing();

  // Rejects a pairing or connection request from a remote device, returns
  // false if there was no way to reject the pairing.
  bool RejectPairing();

  // Cancels a pairing or connection attempt to a remote device, returns
  // false if there was no way to cancel the pairing.
  bool CancelPairing();

  // Returns the pairing delegate being used by this pairing object.
  device::BluetoothDevice::PairingDelegate* GetPairingDelegate() const;

 private:
  // Internal method to reset the current set of callbacks because a new
  // request has arrived that supersedes them.
  void ResetCallbacks();

  // Internal method to respond to the relevant callback for a RejectPairing
  // or CancelPairing call.
  bool RunPairingCallbacks(
      bluez::BluetoothAgentServiceProvider::Delegate::Status status);

  // The underlying BluetoothDeviceBlueZ that owns this pairing context.
  BluetoothDeviceBlueZ* device_;

  // UI Pairing Delegate to make method calls on, this must live as long as
  // the object capturing the PairingContext.
  device::BluetoothDevice::PairingDelegate* pairing_delegate_;

  // Flag to indicate whether any pairing delegate method has been called
  // during pairing. Used to determine whether we need to log the
  // "no pairing interaction" metric.
  bool pairing_delegate_used_;

  // During pairing these callbacks are set to those provided by method calls
  // made on the BluetoothAdapterBlueZ instance by its respective
  // bluez::BluetoothAgentServiceProvider instance, and are called by our own
  // method calls such as SetPinCode() and SetPasskey().
  bluez::BluetoothAgentServiceProvider::Delegate::PinCodeCallback
      pincode_callback_;
  bluez::BluetoothAgentServiceProvider::Delegate::PasskeyCallback
      passkey_callback_;
  bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback
      confirmation_callback_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothPairingBlueZ);
};

}  // namespace bluez

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_BLUEZ_H_