• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 CHROMEOS_DBUS_BLUETOOTH_AGENT_SERVICE_PROVIDER_H_
6 #define CHROMEOS_DBUS_BLUETOOTH_AGENT_SERVICE_PROVIDER_H_
7 
8 #include <string>
9 
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "chromeos/chromeos_export.h"
13 #include "dbus/bus.h"
14 #include "dbus/object_path.h"
15 
16 namespace chromeos {
17 
18 // BluetoothAgentServiceProvider is used to provide a D-Bus object that
19 // the bluetooth daemon can communicate with during a remote device pairing
20 // request.
21 //
22 // Instantiate with a chosen D-Bus object path and delegate object, and pass
23 // the D-Bus object path as the |agent_path| argument to the
24 // chromeos::BluetoothAgentManagerClient::RegisterAgent() method.
25 //
26 // After initiating the pairing process with a device, using the
27 // chromeos::BluetoothDeviceClient::Pair() method, the Bluetooth daemon will
28 // make calls to this agent object and they will be passed on to your Delegate
29 // object for handling. Responses should be returned using the callbacks
30 // supplied to those methods.
31 class CHROMEOS_EXPORT BluetoothAgentServiceProvider {
32  public:
33   // Interface for reacting to agent requests.
34   class Delegate {
35    public:
~Delegate()36     virtual ~Delegate() {}
37 
38     // Possible status values that may be returned to callbacks. Success
39     // indicates that a pincode or passkey has been obtained, or permission
40     // granted; rejected indicates the user rejected the request or denied
41     // permission; cancelled indicates the user cancelled the request
42     // without confirming either way.
43     enum Status {
44       SUCCESS,
45       REJECTED,
46       CANCELLED
47     };
48 
49     // The PinCodeCallback is used for the RequestPinCode() method, it should
50     // be called with two arguments, the |status| of the request (success,
51     // rejected or cancelled) and the |pincode| requested.
52     typedef base::Callback<void(Status, const std::string&)> PinCodeCallback;
53 
54     // The PasskeyCallback is used for the RequestPasskey() method, it should
55     // be called with two arguments, the |status| of the request (success,
56     // rejected or cancelled) and the |passkey| requested, a numeric in the
57     // range 0-999999,
58     typedef base::Callback<void(Status, uint32)> PasskeyCallback;
59 
60     // The ConfirmationCallback is used for methods which request confirmation
61     // or authorization, it should be called with one argument, the |status|
62     // of the request (success, rejected or cancelled).
63     typedef base::Callback<void(Status)> ConfirmationCallback;
64 
65     // This method will be called when the agent is unregistered from the
66     // Bluetooth daemon, generally at the end of a pairing request. It may be
67     // used to perform cleanup tasks. This corresponds to the
68     // org.bluez.Agent1.Release method and is renamed to avoid a conflict
69     // with base::Refcounted<T>.
70     virtual void Released() = 0;
71 
72     // This method will be called when the Bluetooth daemon requires a
73     // PIN Code for authentication of the device with object path |device_path|,
74     // the agent should obtain the code from the user and call |callback|
75     // to provide it, or indicate rejection or cancellation of the request.
76     //
77     // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
78     // for which there is no automatic pairing or special handling.
79     virtual void RequestPinCode(const dbus::ObjectPath& device_path,
80                                 const PinCodeCallback& callback) = 0;
81 
82     // This method will be called when the Bluetooth daemon requires that the
83     // user enter the PIN code |pincode| into the device with object path
84     // |device_path| so that it may be authenticated. The Cancel() method
85     // will be called to dismiss the display once pairing is complete or
86     // cancelled.
87     //
88     // This is used for Bluetooth 2.0 and earlier keyboard devices, the
89     // |pincode| will always be a six-digit numeric in the range 000000-999999
90     // for compatibilty with later specifications.
91     virtual void DisplayPinCode(const dbus::ObjectPath& device_path,
92                                 const std::string& pincode) = 0;
93 
94     // This method will be called when the Bluetooth daemon requires a
95     // Passkey for authentication of the device with object path |device_path|,
96     // the agent should obtain the passkey from the user (a numeric in the
97     // range 0-999999) and call |callback| to provide it, or indicate
98     // rejection or cancellation of the request.
99     //
100     // Passkeys are generally required for Bluetooth 2.1 and later devices
101     // which cannot provide input or display on their own, and don't accept
102     // passkey-less pairing.
103     virtual void RequestPasskey(const dbus::ObjectPath& device_path,
104                                 const PasskeyCallback& callback) = 0;
105 
106     // This method will be called when the Bluetooth daemon requires that the
107     // user enter the Passkey |passkey| into the device with object path
108     // |device_path| so that it may be authenticated. The Cancel() method
109     // will be called to dismiss the display once pairing is complete or
110     // cancelled.
111     //
112     // This is used for Bluetooth 2.1 and later devices that support input
113     // but not display, such as keyboards. The Passkey is a numeric in the
114     // range 0-999999 and should be always presented zero-padded to six
115     // digits.
116     //
117     // As the user enters the passkey onto the device, |entered| will be
118     // updated to reflect the number of digits entered so far.
119     virtual void DisplayPasskey(const dbus::ObjectPath& device_path,
120                                 uint32 passkey, uint16 entered) = 0;
121 
122     // This method will be called when the Bluetooth daemon requires that the
123     // user confirm that the Passkey |passkey| is displayed on the screen
124     // of the device with object path |object_path| so that it may be
125     // authenticated. The agent should display to the user and ask for
126     // confirmation, then call |callback| to provide their response (success,
127     // rejected or cancelled).
128     //
129     // This is used for Bluetooth 2.1 and later devices that support display,
130     // such as other computers or phones. The Passkey is a numeric in the
131     // range 0-999999 and should be always present zero-padded to six
132     // digits.
133     virtual void RequestConfirmation(const dbus::ObjectPath& device_path,
134                                      uint32 passkey,
135                                      const ConfirmationCallback& callback) = 0;
136 
137     // This method will be called when the Bluetooth daemon requires
138     // authorization of an incoming pairing attempt from the device with object
139     // path |device_path| that would have otherwised triggered the just-works
140     // pairing model.
141     //
142     // The agent should confirm the incoming pairing with the user and call
143     // |callback| to provide their response (success, rejected or cancelled).
144     virtual void RequestAuthorization(const dbus::ObjectPath& device_path,
145                                       const ConfirmationCallback& callback) = 0;
146 
147     // This method will be called when the Bluetooth daemon requires that the
148     // user confirm that the device with object path |object_path| is
149     // authorized to connect to the service with UUID |uuid|. The agent should
150     // confirm with the user and call |callback| to provide their response
151     // (success, rejected or cancelled).
152     virtual void AuthorizeService(const dbus::ObjectPath& device_path,
153                                   const std::string& uuid,
154                                   const ConfirmationCallback& callback) = 0;
155 
156     // This method will be called by the Bluetooth daemon to indicate that
157     // the request failed before a reply was returned from the device.
158     virtual void Cancel() = 0;
159   };
160 
161   virtual ~BluetoothAgentServiceProvider();
162 
163   // Creates the instance where |bus| is the D-Bus bus connection to export
164   // the object onto, |object_path| is the object path that it should have
165   // and |delegate| is the object to which all method calls will be passed
166   // and responses generated from.
167   static BluetoothAgentServiceProvider* Create(
168       dbus::Bus* bus,
169       const dbus::ObjectPath& object_path,
170       Delegate* delegate);
171 
172  protected:
173   BluetoothAgentServiceProvider();
174 
175  private:
176   DISALLOW_COPY_AND_ASSIGN(BluetoothAgentServiceProvider);
177 };
178 
179 }  // namespace chromeos
180 
181 #endif  // CHROMEOS_DBUS_BLUETOOTH_AGENT_SERVICE_PROVIDER_H_
182