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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
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"
21 class BluetoothSocketThread;
26 class BluetoothAdapterChromeOS;
27 class BluetoothPairingChromeOS;
29 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
30 // Chrome OS platform.
31 class BluetoothDeviceChromeOS
32 : public device::BluetoothDevice,
33 public BluetoothGattServiceClient::Observer {
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;
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 ConnectToProfile(
71 device::BluetoothProfile* profile,
72 const base::Closure& callback,
73 const ConnectToProfileErrorCallback& error_callback) OVERRIDE;
74 virtual void ConnectToService(
75 const device::BluetoothUUID& uuid,
76 const ConnectToServiceCallback& callback,
77 const ConnectToServiceErrorCallback& error_callback) OVERRIDE;
78 virtual void SetOutOfBandPairingData(
79 const device::BluetoothOutOfBandPairingData& data,
80 const base::Closure& callback,
81 const ErrorCallback& error_callback) OVERRIDE;
82 virtual void ClearOutOfBandPairingData(
83 const base::Closure& callback,
84 const ErrorCallback& error_callback) OVERRIDE;
85 virtual void StartConnectionMonitor(
86 const base::Closure& callback,
87 const ErrorCallback& error_callback) OVERRIDE;
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);
95 // Releases the current pairing object, any pairing-related method calls will
99 // Returns the current pairing object or NULL if no pairing is in progress.
100 BluetoothPairingChromeOS* GetPairing() const;
103 // BluetoothDevice override
104 virtual std::string GetDeviceName() const OVERRIDE;
107 friend class BluetoothAdapterChromeOS;
109 BluetoothDeviceChromeOS(
110 BluetoothAdapterChromeOS* adapter,
111 const dbus::ObjectPath& object_path,
112 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
113 scoped_refptr<device::BluetoothSocketThread> socket_thread);
114 virtual ~BluetoothDeviceChromeOS();
116 // BluetoothGattServiceClient::Observer overrides.
117 virtual void GattServiceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
118 virtual void GattServiceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
120 // Internal method to initiate a connection to this device, and methods called
121 // by dbus:: on completion of the D-Bus method call.
122 void ConnectInternal(bool after_pairing,
123 const base::Closure& callback,
124 const ConnectErrorCallback& error_callback);
125 void OnConnect(bool after_pairing,
126 const base::Closure& callback);
127 void OnConnectError(bool after_pairing,
128 const ConnectErrorCallback& error_callback,
129 const std::string& error_name,
130 const std::string& error_message);
132 // Called by dbus:: on completion of the D-Bus method call to pair the device.
133 void OnPair(const base::Closure& callback,
134 const ConnectErrorCallback& error_callback);
135 void OnPairError(const ConnectErrorCallback& error_callback,
136 const std::string& error_name,
137 const std::string& error_message);
139 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
140 // there is no matching completion call since we don't do anything special
142 void OnCancelPairingError(const std::string& error_name,
143 const std::string& error_message);
145 // Internal method to set the device as trusted. Trusted devices can connect
146 // to us automatically, and we can connect to them after rebooting; it also
147 // causes the device to be remembered by the stack even if not paired.
148 // |success| to the callback indicates whether or not the request succeeded.
150 void OnSetTrusted(bool success);
152 // Called by dbus:: on completion of the D-Bus method call to disconnect the
154 void OnDisconnect(const base::Closure& callback);
155 void OnDisconnectError(const ErrorCallback& error_callback,
156 const std::string& error_name,
157 const std::string& error_message);
159 // Called by dbus:: on failure of the D-Bus method call to unpair the device;
160 // there is no matching completion call since this object is deleted in the
161 // process of unpairing.
162 void OnForgetError(const ErrorCallback& error_callback,
163 const std::string& error_name,
164 const std::string& error_message);
166 // Called by dbus:: on completion of the D-Bus method call to
167 // connect a peofile.
168 void OnConnectProfile(device::BluetoothProfile* profile,
169 const base::Closure& callback);
170 void OnConnectProfileError(
171 device::BluetoothProfile* profile,
172 const ConnectToProfileErrorCallback& error_callback,
173 const std::string& error_name,
174 const std::string& error_message);
176 // Called by dbus:: on completion of the D-Bus method call to start the
177 // connection monitor.
178 void OnStartConnectionMonitor(const base::Closure& callback);
179 void OnStartConnectionMonitorError(const ErrorCallback& error_callback,
180 const std::string& error_name,
181 const std::string& error_message);
183 // Returns the object path of the device; used by BluetoothAdapterChromeOS
184 const dbus::ObjectPath& object_path() const { return object_path_; }
186 // The adapter that owns this device instance.
187 BluetoothAdapterChromeOS* adapter_;
189 // The dbus object path of the device object.
190 dbus::ObjectPath object_path_;
192 // List of observers interested in event notifications from us.
193 ObserverList<device::BluetoothDevice::Observer> observers_;
195 // Number of ongoing calls to Connect().
196 int num_connecting_calls_;
198 // True if the connection monitor has been started, tracking the connection
199 // RSSI and TX power.
200 bool connection_monitor_started_;
202 // UI thread task runner and socket thread object used to create sockets.
203 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
204 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
206 // During pairing this is set to an object that we don't own, but on which
207 // we can make method calls to request, display or confirm PIN Codes and
208 // Passkeys. Generally it is the object that owns this one.
209 scoped_ptr<BluetoothPairingChromeOS> pairing_;
211 // Note: This should remain the last member so it'll be destroyed and
212 // invalidate its weak pointers before any other members are destroyed.
213 base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
215 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
218 } // namespace chromeos
220 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H