• 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 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/observer_list.h"
14 #include "base/sequenced_task_runner.h"
15 #include "chromeos/dbus/bluetooth_device_client.h"
16 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
17 #include "dbus/object_path.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 
20 namespace device {
21 class BluetoothSocketThread;
22 }  // namespace device
23 
24 namespace chromeos {
25 
26 class BluetoothAdapterChromeOS;
27 class BluetoothPairingChromeOS;
28 
29 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
30 // Chrome OS platform.
31 class BluetoothDeviceChromeOS
32     : public device::BluetoothDevice,
33       public BluetoothGattServiceClient::Observer {
34  public:
35   // BluetoothDevice override
36   virtual void AddObserver(
37       device::BluetoothDevice::Observer* observer) OVERRIDE;
38   virtual void RemoveObserver(
39       device::BluetoothDevice::Observer* observer) OVERRIDE;
40   virtual uint32 GetBluetoothClass() const OVERRIDE;
41   virtual std::string GetAddress() const OVERRIDE;
42   virtual VendorIDSource GetVendorIDSource() const OVERRIDE;
43   virtual uint16 GetVendorID() const OVERRIDE;
44   virtual uint16 GetProductID() const OVERRIDE;
45   virtual uint16 GetDeviceID() const OVERRIDE;
46   virtual int GetRSSI() const OVERRIDE;
47   virtual int GetCurrentHostTransmitPower() const OVERRIDE;
48   virtual int GetMaximumHostTransmitPower() const OVERRIDE;
49   virtual bool IsPaired() const OVERRIDE;
50   virtual bool IsConnected() const OVERRIDE;
51   virtual bool IsConnectable() const OVERRIDE;
52   virtual bool IsConnecting() const OVERRIDE;
53   virtual UUIDList GetUUIDs() const OVERRIDE;
54   virtual bool ExpectingPinCode() const OVERRIDE;
55   virtual bool ExpectingPasskey() const OVERRIDE;
56   virtual bool ExpectingConfirmation() const OVERRIDE;
57   virtual void Connect(
58       device::BluetoothDevice::PairingDelegate* pairing_delegate,
59       const base::Closure& callback,
60       const ConnectErrorCallback& error_callback) OVERRIDE;
61   virtual void SetPinCode(const std::string& pincode) OVERRIDE;
62   virtual void SetPasskey(uint32 passkey) OVERRIDE;
63   virtual void ConfirmPairing() OVERRIDE;
64   virtual void RejectPairing() OVERRIDE;
65   virtual void CancelPairing() OVERRIDE;
66   virtual void Disconnect(
67       const base::Closure& callback,
68       const ErrorCallback& error_callback) OVERRIDE;
69   virtual void Forget(const ErrorCallback& error_callback) OVERRIDE;
70   virtual void ConnectToService(
71       const device::BluetoothUUID& uuid,
72       const ConnectToServiceCallback& callback,
73       const ConnectToServiceErrorCallback& error_callback) OVERRIDE;
74   virtual void CreateGattConnection(
75       const GattConnectionCallback& callback,
76       const ConnectErrorCallback& error_callback) OVERRIDE;
77   virtual void StartConnectionMonitor(
78       const base::Closure& callback,
79       const ErrorCallback& error_callback) OVERRIDE;
80 
81   // Creates a pairing object with the given delegate |pairing_delegate| and
82   // establishes it as the pairing context for this device. All pairing-related
83   // method calls will be forwarded to this object until it is released.
84   BluetoothPairingChromeOS* BeginPairing(
85       BluetoothDevice::PairingDelegate* pairing_delegate);
86 
87   // Releases the current pairing object, any pairing-related method calls will
88   // be ignored.
89   void EndPairing();
90 
91   // Returns the current pairing object or NULL if no pairing is in progress.
92   BluetoothPairingChromeOS* GetPairing() const;
93 
94   // Returns the object path of the device.
object_path()95   const dbus::ObjectPath& object_path() const { return object_path_; }
96 
97  protected:
98    // BluetoothDevice override
99   virtual std::string GetDeviceName() const OVERRIDE;
100 
101  private:
102   friend class BluetoothAdapterChromeOS;
103 
104   BluetoothDeviceChromeOS(
105       BluetoothAdapterChromeOS* adapter,
106       const dbus::ObjectPath& object_path,
107       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
108       scoped_refptr<device::BluetoothSocketThread> socket_thread);
109   virtual ~BluetoothDeviceChromeOS();
110 
111   // BluetoothGattServiceClient::Observer overrides.
112   virtual void GattServiceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
113   virtual void GattServiceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
114 
115   // Internal method to initiate a connection to this device, and methods called
116   // by dbus:: on completion of the D-Bus method call.
117   void ConnectInternal(bool after_pairing,
118                        const base::Closure& callback,
119                        const ConnectErrorCallback& error_callback);
120   void OnConnect(bool after_pairing,
121                  const base::Closure& callback);
122   void OnCreateGattConnection(const GattConnectionCallback& callback);
123   void OnConnectError(bool after_pairing,
124                       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   // Called by dbus:: on completion of the D-Bus method call to disconnect the
149   // device.
150   void OnDisconnect(const base::Closure& callback);
151   void OnDisconnectError(const ErrorCallback& error_callback,
152                          const std::string& error_name,
153                          const std::string& error_message);
154 
155   // Called by dbus:: on failure of the D-Bus method call to unpair the device;
156   // there is no matching completion call since this object is deleted in the
157   // process of unpairing.
158   void OnForgetError(const ErrorCallback& error_callback,
159                      const std::string& error_name,
160                      const std::string& error_message);
161 
162   // Called by dbus:: on completion of the D-Bus method call to start the
163   // connection monitor.
164   void OnStartConnectionMonitor(const base::Closure& callback);
165   void OnStartConnectionMonitorError(const ErrorCallback& error_callback,
166                                      const std::string& error_name,
167                                      const std::string& error_message);
168 
169   // The adapter that owns this device instance.
170   BluetoothAdapterChromeOS* adapter_;
171 
172   // The dbus object path of the device object.
173   dbus::ObjectPath object_path_;
174 
175   // List of observers interested in event notifications from us.
176   ObserverList<device::BluetoothDevice::Observer> observers_;
177 
178   // Number of ongoing calls to Connect().
179   int num_connecting_calls_;
180 
181   // True if the connection monitor has been started, tracking the connection
182   // RSSI and TX power.
183   bool connection_monitor_started_;
184 
185   // UI thread task runner and socket thread object used to create sockets.
186   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
187   scoped_refptr<device::BluetoothSocketThread> socket_thread_;
188 
189   // During pairing this is set to an object that we don't own, but on which
190   // we can make method calls to request, display or confirm PIN Codes and
191   // Passkeys. Generally it is the object that owns this one.
192   scoped_ptr<BluetoothPairingChromeOS> pairing_;
193 
194   // Note: This should remain the last member so it'll be destroyed and
195   // invalidate its weak pointers before any other members are destroyed.
196   base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
197 
198   DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
199 };
200 
201 }  // namespace chromeos
202 
203 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
204