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/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/sequenced_task_runner.h"
     14 #include "chromeos/dbus/bluetooth_device_client.h"
     15 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
     16 #include "dbus/object_path.h"
     17 #include "device/bluetooth/bluetooth_device.h"
     18 
     19 namespace device {
     20 class BluetoothSocketThread;
     21 }  // namespace device
     22 
     23 namespace chromeos {
     24 
     25 class BluetoothAdapterChromeOS;
     26 class BluetoothPairingChromeOS;
     27 
     28 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
     29 // Chrome OS platform.
     30 class BluetoothDeviceChromeOS
     31     : public device::BluetoothDevice,
     32       public BluetoothGattServiceClient::Observer {
     33  public:
     34   // BluetoothDevice override
     35   virtual uint32 GetBluetoothClass() const OVERRIDE;
     36   virtual std::string GetAddress() const OVERRIDE;
     37   virtual VendorIDSource GetVendorIDSource() const OVERRIDE;
     38   virtual uint16 GetVendorID() const OVERRIDE;
     39   virtual uint16 GetProductID() const OVERRIDE;
     40   virtual uint16 GetDeviceID() const OVERRIDE;
     41   virtual int GetRSSI() const OVERRIDE;
     42   virtual int GetCurrentHostTransmitPower() const OVERRIDE;
     43   virtual int GetMaximumHostTransmitPower() const OVERRIDE;
     44   virtual bool IsPaired() const OVERRIDE;
     45   virtual bool IsConnected() const OVERRIDE;
     46   virtual bool IsConnectable() const OVERRIDE;
     47   virtual bool IsConnecting() const OVERRIDE;
     48   virtual UUIDList GetUUIDs() const OVERRIDE;
     49   virtual bool ExpectingPinCode() const OVERRIDE;
     50   virtual bool ExpectingPasskey() const OVERRIDE;
     51   virtual bool ExpectingConfirmation() const OVERRIDE;
     52   virtual void Connect(
     53       device::BluetoothDevice::PairingDelegate* pairing_delegate,
     54       const base::Closure& callback,
     55       const ConnectErrorCallback& error_callback) OVERRIDE;
     56   virtual void SetPinCode(const std::string& pincode) OVERRIDE;
     57   virtual void SetPasskey(uint32 passkey) OVERRIDE;
     58   virtual void ConfirmPairing() OVERRIDE;
     59   virtual void RejectPairing() OVERRIDE;
     60   virtual void CancelPairing() OVERRIDE;
     61   virtual void Disconnect(
     62       const base::Closure& callback,
     63       const ErrorCallback& error_callback) OVERRIDE;
     64   virtual void Forget(const ErrorCallback& error_callback) OVERRIDE;
     65   virtual void ConnectToService(
     66       const device::BluetoothUUID& uuid,
     67       const ConnectToServiceCallback& callback,
     68       const ConnectToServiceErrorCallback& error_callback) OVERRIDE;
     69   virtual void CreateGattConnection(
     70       const GattConnectionCallback& callback,
     71       const ConnectErrorCallback& error_callback) OVERRIDE;
     72   virtual void StartConnectionMonitor(
     73       const base::Closure& callback,
     74       const ErrorCallback& error_callback) OVERRIDE;
     75 
     76   // Attempts to initiate an insecure outgoing L2CAP or RFCOMM connection to the
     77   // advertised service on this device matching |uuid|, performing an SDP lookup
     78   // if necessary to determine the correct protocol and channel for the
     79   // connection. Unlike ConnectToService, the outgoing connection will request
     80   // no bonding rather than general bonding. |callback| will be called on a
     81   // successful connection with a BluetoothSocket instance that is to be owned
     82   // by the receiver. |error_callback| will be called on failure with a message
     83   // indicating the cause.
     84   void ConnectToServiceInsecurely(
     85     const device::BluetoothUUID& uuid,
     86     const ConnectToServiceCallback& callback,
     87     const ConnectToServiceErrorCallback& error_callback);
     88 
     89   // Creates a pairing object with the given delegate |pairing_delegate| and
     90   // establishes it as the pairing context for this device. All pairing-related
     91   // method calls will be forwarded to this object until it is released.
     92   BluetoothPairingChromeOS* BeginPairing(
     93       BluetoothDevice::PairingDelegate* pairing_delegate);
     94 
     95   // Releases the current pairing object, any pairing-related method calls will
     96   // be ignored.
     97   void EndPairing();
     98 
     99   // Returns the current pairing object or NULL if no pairing is in progress.
    100   BluetoothPairingChromeOS* GetPairing() const;
    101 
    102   // Returns the object path of the device.
    103   const dbus::ObjectPath& object_path() const { return object_path_; }
    104 
    105  protected:
    106    // BluetoothDevice override
    107   virtual std::string GetDeviceName() const OVERRIDE;
    108 
    109  private:
    110   friend class BluetoothAdapterChromeOS;
    111 
    112   BluetoothDeviceChromeOS(
    113       BluetoothAdapterChromeOS* adapter,
    114       const dbus::ObjectPath& object_path,
    115       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
    116       scoped_refptr<device::BluetoothSocketThread> socket_thread);
    117   virtual ~BluetoothDeviceChromeOS();
    118 
    119   // BluetoothGattServiceClient::Observer overrides.
    120   virtual void GattServiceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
    121   virtual void GattServiceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
    122 
    123   // Internal method to initiate a connection to this device, and methods called
    124   // by dbus:: on completion of the D-Bus method call.
    125   void ConnectInternal(bool after_pairing,
    126                        const base::Closure& callback,
    127                        const ConnectErrorCallback& error_callback);
    128   void OnConnect(bool after_pairing,
    129                  const base::Closure& callback);
    130   void OnCreateGattConnection(const GattConnectionCallback& callback);
    131   void OnConnectError(bool after_pairing,
    132                       const ConnectErrorCallback& error_callback,
    133                       const std::string& error_name,
    134                       const std::string& error_message);
    135 
    136   // Called by dbus:: on completion of the D-Bus method call to pair the device.
    137   void OnPair(const base::Closure& callback,
    138               const ConnectErrorCallback& error_callback);
    139   void OnPairError(const ConnectErrorCallback& error_callback,
    140                    const std::string& error_name,
    141                    const std::string& error_message);
    142 
    143   // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
    144   // there is no matching completion call since we don't do anything special
    145   // in that case.
    146   void OnCancelPairingError(const std::string& error_name,
    147                             const std::string& error_message);
    148 
    149   // Internal method to set the device as trusted. Trusted devices can connect
    150   // to us automatically, and we can connect to them after rebooting; it also
    151   // causes the device to be remembered by the stack even if not paired.
    152   // |success| to the callback indicates whether or not the request succeeded.
    153   void SetTrusted();
    154   void OnSetTrusted(bool success);
    155 
    156   // Called by dbus:: on completion of the D-Bus method call to disconnect the
    157   // device.
    158   void OnDisconnect(const base::Closure& callback);
    159   void OnDisconnectError(const ErrorCallback& error_callback,
    160                          const std::string& error_name,
    161                          const std::string& error_message);
    162 
    163   // Called by dbus:: on failure of the D-Bus method call to unpair the device;
    164   // there is no matching completion call since this object is deleted in the
    165   // process of unpairing.
    166   void OnForgetError(const ErrorCallback& error_callback,
    167                      const std::string& error_name,
    168                      const std::string& error_message);
    169 
    170   // Called by dbus:: on completion of the D-Bus method call to start the
    171   // connection monitor.
    172   void OnStartConnectionMonitor(const base::Closure& callback);
    173   void OnStartConnectionMonitorError(const ErrorCallback& error_callback,
    174                                      const std::string& error_name,
    175                                      const std::string& error_message);
    176 
    177   // The adapter that owns this device instance.
    178   BluetoothAdapterChromeOS* adapter_;
    179 
    180   // The dbus object path of the device object.
    181   dbus::ObjectPath object_path_;
    182 
    183   // Number of ongoing calls to Connect().
    184   int num_connecting_calls_;
    185 
    186   // True if the connection monitor has been started, tracking the connection
    187   // RSSI and TX power.
    188   bool connection_monitor_started_;
    189 
    190   // UI thread task runner and socket thread object used to create sockets.
    191   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
    192   scoped_refptr<device::BluetoothSocketThread> socket_thread_;
    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   scoped_ptr<BluetoothPairingChromeOS> pairing_;
    198 
    199   // Note: This should remain the last member so it'll be destroyed and
    200   // invalidate its weak pointers before any other members are destroyed.
    201   base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
    202 
    203   DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
    204 };
    205 
    206 }  // namespace chromeos
    207 
    208 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
    209