1// Copyright 2014 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
5#ifndef DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_CHROMEOS_H_
6#define DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_CHROMEOS_H_
7
8#include "chromeos/dbus/bluetooth_agent_service_provider.h"
9#include "device/bluetooth/bluetooth_device.h"
10
11namespace chromeos {
12
13class BluetoothDeviceChromeOS;
14
15// The BluetoothPairingChromeOS class encapsulates the logic for an individual
16// device pairing, acting as a bridge between BluetoothAdapterChromeOS which
17// communicates with the underlying Controller and Host Subsystem, and
18// BluetoothDeviceChromeOS which presents the pairing logic to the application.
19class BluetoothPairingChromeOS {
20 public:
21  BluetoothPairingChromeOS(
22      BluetoothDeviceChromeOS* device,
23      device::BluetoothDevice::PairingDelegate* pairing_delegate);
24  ~BluetoothPairingChromeOS();
25
26  // Indicates whether the device is currently pairing and expecting a
27  // Passkey to be returned.
28  bool ExpectingPasskey() const;
29
30  // Indicates whether the device is currently pairing and expecting
31  // confirmation of a displayed passkey.
32  bool ExpectingConfirmation() const;
33
34  // Requests a PIN code for the current device from the current pairing
35  // delegate, the SetPinCode(), RejectPairing() and CancelPairing() method
36  // calls on this object are translated into the appropriate response to
37  // |callback|.
38  void RequestPinCode(
39      const BluetoothAgentServiceProvider::Delegate::PinCodeCallback& callback);
40
41  // Indicates whether the device is currently pairing and expecting a
42  // PIN Code to be returned.
43  bool ExpectingPinCode() const;
44
45  // Sends the PIN code |pincode| to the remote device during pairing.
46  //
47  // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
48  // for which there is no automatic pairing or special handling.
49  void SetPinCode(const std::string& pincode);
50
51  // Requests a PIN code for the current device be displayed by the current
52  // pairing delegate. No response is expected from the delegate.
53  void DisplayPinCode(const std::string& pincode);
54
55  // Requests a Passkey for the current device from the current pairing
56  // delegate, the SetPasskey(), RejectPairing() and CancelPairing() method
57  // calls on this object are translated into the appropriate response to
58  // |callback|.
59  void RequestPasskey(
60      const BluetoothAgentServiceProvider::Delegate::PasskeyCallback& callback);
61
62  // Sends the Passkey |passkey| to the remote device during pairing.
63  //
64  // Passkeys are generally required for Bluetooth 2.1 and later devices
65  // which cannot provide input or display on their own, and don't accept
66  // passkey-less pairing, and are a numeric in the range 0-999999.
67  void SetPasskey(uint32 passkey);
68
69  // Requests a Passkey for the current device be displayed by the current
70  // pairing delegate. No response is expected from the delegate.
71  void DisplayPasskey(uint32 passkey);
72
73  // Informs the current pairing delegate that |entered| keys have been
74  // provided to the remote device since the DisplayPasskey() call. No
75  // response is expected from the delegate.
76  void KeysEntered(uint16 entered);
77
78  // Requests confirmation that |passkey| is displayed on the current device
79  // from the current pairing delegate. The ConfirmPairing(), RejectPairing()
80  // and CancelPairing() method calls on this object are translated into the
81  // appropriate response to |callback|.
82  void RequestConfirmation(
83      uint32 passkey,
84      const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
85          callback);
86
87  // Requests authorization that the current device be allowed to pair with
88  // this device from the current pairing delegate. The ConfirmPairing(),
89  // RejectPairing() and CancelPairing() method calls on this object are
90  // translated into the appropriate response to |callback|.
91  void RequestAuthorization(
92      const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
93          callback);
94
95  // Confirms to the remote device during pairing that a passkey provided by
96  // the ConfirmPasskey() delegate call is displayed on both devices.
97  void ConfirmPairing();
98
99  // Rejects a pairing or connection request from a remote device, returns
100  // false if there was no way to reject the pairing.
101  bool RejectPairing();
102
103  // Cancels a pairing or connection attempt to a remote device, returns
104  // false if there was no way to cancel the pairing.
105  bool CancelPairing();
106
107  // Returns the pairing delegate being used by this pairing object.
108  device::BluetoothDevice::PairingDelegate* GetPairingDelegate() const;
109
110 private:
111  // Internal method to reset the current set of callbacks because a new
112  // request has arrived that supercedes them.
113  void ResetCallbacks();
114
115  // Internal method to respond to the relevant callback for a RejectPairing
116  // or CancelPairing call.
117  bool RunPairingCallbacks(
118      BluetoothAgentServiceProvider::Delegate::Status status);
119
120  // The underlying BluetoothDeviceChromeOS that owns this pairing context.
121  BluetoothDeviceChromeOS* device_;
122
123  // UI Pairing Delegate to make method calls on, this must live as long as
124  // the object capturing the PairingContext.
125  device::BluetoothDevice::PairingDelegate* pairing_delegate_;
126
127  // Flag to indicate whether any pairing delegate method has been called
128  // during pairing. Used to determine whether we need to log the
129  // "no pairing interaction" metric.
130  bool pairing_delegate_used_;
131
132  // During pairing these callbacks are set to those provided by method calls
133  // made on the BluetoothAdapterChromeOS instance by its respective
134  // BluetoothAgentServiceProvider instance, and are called by our own
135  // method calls such as SetPinCode() and SetPasskey().
136  BluetoothAgentServiceProvider::Delegate::PinCodeCallback pincode_callback_;
137  BluetoothAgentServiceProvider::Delegate::PasskeyCallback passkey_callback_;
138  BluetoothAgentServiceProvider::Delegate::ConfirmationCallback
139      confirmation_callback_;
140
141  DISALLOW_COPY_AND_ASSIGN(BluetoothPairingChromeOS);
142};
143
144}  // namespace chromeos
145
146#endif  // DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_CHROMEOS_H_
147