Home | History | Annotate | Download | only in bluetooth
      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 DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
      6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
     13 #include "chromeos/dbus/bluetooth_device_client.h"
     14 #include "dbus/object_path.h"
     15 #include "device/bluetooth/bluetooth_device.h"
     16 
     17 namespace chromeos {
     18 
     19 class BluetoothAdapterChromeOS;
     20 
     21 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
     22 // Chrome OS platform.
     23 class BluetoothDeviceChromeOS
     24     : public device::BluetoothDevice,
     25       private chromeos::BluetoothAgentServiceProvider::Delegate {
     26  public:
     27   // BluetoothDevice override
     28   virtual uint32 GetBluetoothClass() const OVERRIDE;
     29   virtual std::string GetAddress() const OVERRIDE;
     30   virtual uint16 GetVendorID() const OVERRIDE;
     31   virtual uint16 GetProductID() const OVERRIDE;
     32   virtual uint16 GetDeviceID() const OVERRIDE;
     33   virtual bool IsPaired() const OVERRIDE;
     34   virtual bool IsConnected() const OVERRIDE;
     35   virtual bool IsConnectable() const OVERRIDE;
     36   virtual bool IsConnecting() const OVERRIDE;
     37   virtual ServiceList GetServices() const OVERRIDE;
     38   virtual void GetServiceRecords(
     39       const ServiceRecordsCallback& callback,
     40       const ErrorCallback& error_callback) OVERRIDE;
     41   virtual void ProvidesServiceWithName(
     42       const std::string& name,
     43       const ProvidesServiceCallback& callback) OVERRIDE;
     44   virtual bool ExpectingPinCode() const OVERRIDE;
     45   virtual bool ExpectingPasskey() const OVERRIDE;
     46   virtual bool ExpectingConfirmation() const OVERRIDE;
     47   virtual void Connect(
     48       device::BluetoothDevice::PairingDelegate* pairing_delegate,
     49       const base::Closure& callback,
     50       const ConnectErrorCallback& error_callback) OVERRIDE;
     51   virtual void SetPinCode(const std::string& pincode) OVERRIDE;
     52   virtual void SetPasskey(uint32 passkey) OVERRIDE;
     53   virtual void ConfirmPairing() OVERRIDE;
     54   virtual void RejectPairing() OVERRIDE;
     55   virtual void CancelPairing() OVERRIDE;
     56   virtual void Disconnect(
     57       const base::Closure& callback,
     58       const ErrorCallback& error_callback) OVERRIDE;
     59   virtual void Forget(const ErrorCallback& error_callback) OVERRIDE;
     60   virtual void ConnectToService(
     61       const std::string& service_uuid,
     62       const SocketCallback& callback) OVERRIDE;
     63   virtual void ConnectToProfile(
     64       device::BluetoothProfile* profile,
     65       const base::Closure& callback,
     66       const ErrorCallback& error_callback) OVERRIDE;
     67   virtual void SetOutOfBandPairingData(
     68       const device::BluetoothOutOfBandPairingData& data,
     69       const base::Closure& callback,
     70       const ErrorCallback& error_callback) OVERRIDE;
     71   virtual void ClearOutOfBandPairingData(
     72       const base::Closure& callback,
     73       const ErrorCallback& error_callback) OVERRIDE;
     74 
     75  protected:
     76    // BluetoothDevice override
     77   virtual std::string GetDeviceName() const OVERRIDE;
     78 
     79  private:
     80   friend class BluetoothAdapterChromeOS;
     81 
     82   BluetoothDeviceChromeOS(BluetoothAdapterChromeOS* adapter,
     83                           const dbus::ObjectPath& object_path);
     84   virtual ~BluetoothDeviceChromeOS();
     85 
     86   // BluetoothAgentServiceProvider::Delegate override.
     87   virtual void Release() OVERRIDE;
     88   virtual void RequestPinCode(const dbus::ObjectPath& device_path,
     89                               const PinCodeCallback& callback) OVERRIDE;
     90   virtual void DisplayPinCode(const dbus::ObjectPath& device_path,
     91                               const std::string& pincode) OVERRIDE;
     92   virtual void RequestPasskey(const dbus::ObjectPath& device_path,
     93                               const PasskeyCallback& callback) OVERRIDE;
     94   virtual void DisplayPasskey(const dbus::ObjectPath& device_path,
     95                               uint32 passkey, uint16 entered) OVERRIDE;
     96   virtual void RequestConfirmation(const dbus::ObjectPath& device_path,
     97                                    uint32 passkey,
     98                                    const ConfirmationCallback& callback)
     99       OVERRIDE;
    100   virtual void RequestAuthorization(const dbus::ObjectPath& device_path,
    101                                     const ConfirmationCallback& callback)
    102       OVERRIDE;
    103   virtual void AuthorizeService(const dbus::ObjectPath& device_path,
    104                                 const std::string& uuid,
    105                                 const ConfirmationCallback& callback) OVERRIDE;
    106   virtual void Cancel() OVERRIDE;
    107 
    108   // Internal method to initiate a connection to this device, and methods called
    109   // by dbus:: on completion of the D-Bus method call.
    110   void ConnectInternal(bool after_pairing,
    111                        const base::Closure& callback,
    112                        const ConnectErrorCallback& error_callback);
    113   void OnConnect(bool after_pairing,
    114                  const base::Closure& callback);
    115   void OnConnectError(bool after_pairing,
    116                       const ConnectErrorCallback& error_callback,
    117                       const std::string& error_name,
    118                       const std::string& error_message);
    119 
    120   // Called by dbus:: on completion of the D-Bus method call to register the
    121   // pairing agent.
    122   void OnRegisterAgent(const base::Closure& callback,
    123                        const ConnectErrorCallback& error_callback);
    124   void OnRegisterAgentError(const ConnectErrorCallback& error_callback,
    125                             const std::string& error_name,
    126                             const std::string& error_message);
    127 
    128   // Called by dbus:: on completion of the D-Bus method call to pair the device.
    129   void OnPair(const base::Closure& callback,
    130               const ConnectErrorCallback& error_callback);
    131   void OnPairError(const ConnectErrorCallback& error_callback,
    132                    const std::string& error_name,
    133                    const std::string& error_message);
    134 
    135   // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
    136   // there is no matching completion call since we don't do anything special
    137   // in that case.
    138   void OnCancelPairingError(const std::string& error_name,
    139                             const std::string& error_message);
    140 
    141   // Internal method to set the device as trusted. Trusted devices can connect
    142   // to us automatically, and we can connect to them after rebooting; it also
    143   // causes the device to be remembered by the stack even if not paired.
    144   // |success| to the callback indicates whether or not the request succeeded.
    145   void SetTrusted();
    146   void OnSetTrusted(bool success);
    147 
    148   // Internal method to unregister the pairing agent and method called by dbus::
    149   // on failure of the D-Bus method call. No completion call as success is
    150   // ignored.
    151   void UnregisterAgent();
    152   void OnUnregisterAgentError(const std::string& error_name,
    153                               const std::string& error_message);
    154 
    155   // Called by dbus:: on completion of the D-Bus method call to disconnect the
    156   // device.
    157   void OnDisconnect(const base::Closure& callback);
    158   void OnDisconnectError(const ErrorCallback& error_callback,
    159                          const std::string& error_name,
    160                          const std::string& error_message);
    161 
    162   // Called by dbus:: on failure of the D-Bus method call to unpair the device;
    163   // there is no matching completion call since this object is deleted in the
    164   // process of unpairing.
    165   void OnForgetError(const ErrorCallback& error_callback,
    166                      const std::string& error_name,
    167                      const std::string& error_message);
    168 
    169   // Run any outstanding pairing callbacks passing |status| as the result of
    170   // pairing. Returns true if any callbacks were run, false if not.
    171   bool RunPairingCallbacks(Status status);
    172 
    173   // Called by dbus:: on completion of the D-Bus method call to
    174   // connect a peofile.
    175   void OnConnectProfile(device::BluetoothProfile* profile,
    176                         const base::Closure& callback);
    177   void OnConnectProfileError(device::BluetoothProfile* profile,
    178                              const ErrorCallback& error_callback,
    179                              const std::string& error_name,
    180                              const std::string& error_message);
    181 
    182   // Return the object path of the device; used by BluetoothAdapterChromeOS
    183   const dbus::ObjectPath& object_path() const { return object_path_; }
    184 
    185   // The adapter that owns this device instance.
    186   BluetoothAdapterChromeOS* adapter_;
    187 
    188   // The dbus object path of the device object.
    189   dbus::ObjectPath object_path_;
    190 
    191   // Number of ongoing calls to Connect().
    192   int num_connecting_calls_;
    193 
    194   // During pairing this is set to an object that we don't own, but on which
    195   // we can make method calls to request, display or confirm PIN Codes and
    196   // Passkeys. Generally it is the object that owns this one.
    197   PairingDelegate* pairing_delegate_;
    198 
    199   // Flag to indicate whether a pairing delegate method has been called during
    200   // pairing.
    201   bool pairing_delegate_used_;
    202 
    203   // During pairing this is set to an instance of a D-Bus agent object
    204   // intialized with our own class as its delegate.
    205   scoped_ptr<BluetoothAgentServiceProvider> agent_;
    206 
    207   // During pairing these callbacks are set to those provided by method calls
    208   // made on us by |agent_| and are called by our own method calls such as
    209   // SetPinCode() and SetPasskey().
    210   PinCodeCallback pincode_callback_;
    211   PasskeyCallback passkey_callback_;
    212   ConfirmationCallback confirmation_callback_;
    213 
    214   // Note: This should remain the last member so it'll be destroyed and
    215   // invalidate its weak pointers before any other members are destroyed.
    216   base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
    217 
    218   DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
    219 };
    220 
    221 }  // namespace chromeos
    222 
    223 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
    224