Home | History | Annotate | Download | only in dbus
      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:
     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.
     68     virtual void Release() = 0;
     69 
     70     // This method will be called when the Bluetooth daemon requires a
     71     // PIN Code for authentication of the device with object path |device_path|,
     72     // the agent should obtain the code from the user and call |callback|
     73     // to provide it, or indicate rejection or cancellation of the request.
     74     //
     75     // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
     76     // for which there is no automatic pairing or special handling.
     77     virtual void RequestPinCode(const dbus::ObjectPath& device_path,
     78                                 const PinCodeCallback& callback) = 0;
     79 
     80     // This method will be called when the Bluetooth daemon requires that the
     81     // user enter the PIN code |pincode| into the device with object path
     82     // |device_path| so that it may be authenticated. The Cancel() method
     83     // will be called to dismiss the display once pairing is complete or
     84     // cancelled.
     85     //
     86     // This is used for Bluetooth 2.0 and earlier keyboard devices, the
     87     // |pincode| will always be a six-digit numeric in the range 000000-999999
     88     // for compatibilty with later specifications.
     89     virtual void DisplayPinCode(const dbus::ObjectPath& device_path,
     90                                 const std::string& pincode) = 0;
     91 
     92     // This method will be called when the Bluetooth daemon requires a
     93     // Passkey for authentication of the device with object path |device_path|,
     94     // the agent should obtain the passkey from the user (a numeric in the
     95     // range 0-999999) and call |callback| to provide it, or indicate
     96     // rejection or cancellation of the request.
     97     //
     98     // Passkeys are generally required for Bluetooth 2.1 and later devices
     99     // which cannot provide input or display on their own, and don't accept
    100     // passkey-less pairing.
    101     virtual void RequestPasskey(const dbus::ObjectPath& device_path,
    102                                 const PasskeyCallback& callback) = 0;
    103 
    104     // This method will be called when the Bluetooth daemon requires that the
    105     // user enter the Passkey |passkey| into the device with object path
    106     // |device_path| so that it may be authenticated. The Cancel() method
    107     // will be called to dismiss the display once pairing is complete or
    108     // cancelled.
    109     //
    110     // This is used for Bluetooth 2.1 and later devices that support input
    111     // but not display, such as keyboards. The Passkey is a numeric in the
    112     // range 0-999999 and should be always presented zero-padded to six
    113     // digits.
    114     //
    115     // As the user enters the passkey onto the device, |entered| will be
    116     // updated to reflect the number of digits entered so far.
    117     virtual void DisplayPasskey(const dbus::ObjectPath& device_path,
    118                                 uint32 passkey, uint16 entered) = 0;
    119 
    120     // This method will be called when the Bluetooth daemon requires that the
    121     // user confirm that the Passkey |passkey| is displayed on the screen
    122     // of the device with object path |object_path| so that it may be
    123     // authenticated. The agent should display to the user and ask for
    124     // confirmation, then call |callback| to provide their response (success,
    125     // rejected or cancelled).
    126     //
    127     // This is used for Bluetooth 2.1 and later devices that support display,
    128     // such as other computers or phones. The Passkey is a numeric in the
    129     // range 0-999999 and should be always present zero-padded to six
    130     // digits.
    131     virtual void RequestConfirmation(const dbus::ObjectPath& device_path,
    132                                      uint32 passkey,
    133                                      const ConfirmationCallback& callback) = 0;
    134 
    135     // This method will be called when the Bluetooth daemon requires
    136     // authorization of an incoming pairing attempt from the device with object
    137     // path |device_path| that would have otherwised triggered the just-works
    138     // pairing model.
    139     //
    140     // The agent should confirm the incoming pairing with the user and call
    141     // |callback| to provide their response (success, rejected or cancelled).
    142     virtual void RequestAuthorization(const dbus::ObjectPath& device_path,
    143                                       const ConfirmationCallback& callback) = 0;
    144 
    145     // This method will be called when the Bluetooth daemon requires that the
    146     // user confirm that the device with object path |object_path| is
    147     // authorized to connect to the service with UUID |uuid|. The agent should
    148     // confirm with the user and call |callback| to provide their response
    149     // (success, rejected or cancelled).
    150     virtual void AuthorizeService(const dbus::ObjectPath& device_path,
    151                                   const std::string& uuid,
    152                                   const ConfirmationCallback& callback) = 0;
    153 
    154     // This method will be called by the Bluetooth daemon to indicate that
    155     // the request failed before a reply was returned from the device.
    156     virtual void Cancel() = 0;
    157   };
    158 
    159   virtual ~BluetoothAgentServiceProvider();
    160 
    161   // Creates the instance where |bus| is the D-Bus bus connection to export
    162   // the object onto, |object_path| is the object path that it should have
    163   // and |delegate| is the object to which all method calls will be passed
    164   // and responses generated from.
    165   static BluetoothAgentServiceProvider* Create(
    166       dbus::Bus* bus,
    167       const dbus::ObjectPath& object_path,
    168       Delegate* delegate);
    169 
    170  protected:
    171   BluetoothAgentServiceProvider();
    172 
    173  private:
    174   DISALLOW_COPY_AND_ASSIGN(BluetoothAgentServiceProvider);
    175 };
    176 
    177 }  // namespace chromeos
    178 
    179 #endif  // CHROMEOS_DBUS_BLUETOOTH_AGENT_SERVICE_PROVIDER_H_
    180