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/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"
20 class BluetoothSocketThread;
25 class BluetoothAdapterChromeOS;
26 class BluetoothPairingChromeOS;
28 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
29 // Chrome OS platform.
30 class BluetoothDeviceChromeOS
31 : public device::BluetoothDevice,
32 public BluetoothGattServiceClient::Observer {
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;
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;
76 // Creates a pairing object with the given delegate |pairing_delegate| and
77 // establishes it as the pairing context for this device. All pairing-related
78 // method calls will be forwarded to this object until it is released.
79 BluetoothPairingChromeOS* BeginPairing(
80 BluetoothDevice::PairingDelegate* pairing_delegate);
82 // Releases the current pairing object, any pairing-related method calls will
86 // Returns the current pairing object or NULL if no pairing is in progress.
87 BluetoothPairingChromeOS* GetPairing() const;
89 // Returns the object path of the device.
90 const dbus::ObjectPath& object_path() const { return object_path_; }
93 // BluetoothDevice override
94 virtual std::string GetDeviceName() const OVERRIDE;
97 friend class BluetoothAdapterChromeOS;
99 BluetoothDeviceChromeOS(
100 BluetoothAdapterChromeOS* adapter,
101 const dbus::ObjectPath& object_path,
102 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
103 scoped_refptr<device::BluetoothSocketThread> socket_thread);
104 virtual ~BluetoothDeviceChromeOS();
106 // BluetoothGattServiceClient::Observer overrides.
107 virtual void GattServiceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
108 virtual void GattServiceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
110 // Internal method to initiate a connection to this device, and methods called
111 // by dbus:: on completion of the D-Bus method call.
112 void ConnectInternal(bool after_pairing,
113 const base::Closure& callback,
114 const ConnectErrorCallback& error_callback);
115 void OnConnect(bool after_pairing,
116 const base::Closure& callback);
117 void OnCreateGattConnection(const GattConnectionCallback& callback);
118 void OnConnectError(bool after_pairing,
119 const ConnectErrorCallback& error_callback,
120 const std::string& error_name,
121 const std::string& error_message);
123 // Called by dbus:: on completion of the D-Bus method call to pair the device.
124 void OnPair(const base::Closure& callback,
125 const ConnectErrorCallback& error_callback);
126 void OnPairError(const ConnectErrorCallback& error_callback,
127 const std::string& error_name,
128 const std::string& error_message);
130 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
131 // there is no matching completion call since we don't do anything special
133 void OnCancelPairingError(const std::string& error_name,
134 const std::string& error_message);
136 // Internal method to set the device as trusted. Trusted devices can connect
137 // to us automatically, and we can connect to them after rebooting; it also
138 // causes the device to be remembered by the stack even if not paired.
139 // |success| to the callback indicates whether or not the request succeeded.
141 void OnSetTrusted(bool success);
143 // Called by dbus:: on completion of the D-Bus method call to disconnect the
145 void OnDisconnect(const base::Closure& callback);
146 void OnDisconnectError(const ErrorCallback& error_callback,
147 const std::string& error_name,
148 const std::string& error_message);
150 // Called by dbus:: on failure of the D-Bus method call to unpair the device;
151 // there is no matching completion call since this object is deleted in the
152 // process of unpairing.
153 void OnForgetError(const ErrorCallback& error_callback,
154 const std::string& error_name,
155 const std::string& error_message);
157 // Called by dbus:: on completion of the D-Bus method call to start the
158 // connection monitor.
159 void OnStartConnectionMonitor(const base::Closure& callback);
160 void OnStartConnectionMonitorError(const ErrorCallback& error_callback,
161 const std::string& error_name,
162 const std::string& error_message);
164 // The adapter that owns this device instance.
165 BluetoothAdapterChromeOS* adapter_;
167 // The dbus object path of the device object.
168 dbus::ObjectPath object_path_;
170 // Number of ongoing calls to Connect().
171 int num_connecting_calls_;
173 // True if the connection monitor has been started, tracking the connection
174 // RSSI and TX power.
175 bool connection_monitor_started_;
177 // UI thread task runner and socket thread object used to create sockets.
178 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
179 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
181 // During pairing this is set to an object that we don't own, but on which
182 // we can make method calls to request, display or confirm PIN Codes and
183 // Passkeys. Generally it is the object that owns this one.
184 scoped_ptr<BluetoothPairingChromeOS> pairing_;
186 // Note: This should remain the last member so it'll be destroyed and
187 // invalidate its weak pointers before any other members are destroyed.
188 base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
190 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
193 } // namespace chromeos
195 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H