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;
102 // Returns the object path of the device.
103 const dbus::ObjectPath
& object_path() const { return object_path_
; }
106 // BluetoothDevice override
107 virtual std::string
GetDeviceName() const OVERRIDE
;
110 friend class BluetoothAdapterChromeOS
;
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();
119 // BluetoothGattServiceClient::Observer overrides.
120 virtual void GattServiceAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
;
121 virtual void GattServiceRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
;
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 OnConnectError(bool after_pairing
,
131 const ConnectErrorCallback
& error_callback
,
132 const std::string
& error_name
,
133 const std::string
& error_message
);
135 // Called by dbus:: on completion of the D-Bus method call to pair the device.
136 void OnPair(const base::Closure
& callback
,
137 const ConnectErrorCallback
& error_callback
);
138 void OnPairError(const ConnectErrorCallback
& error_callback
,
139 const std::string
& error_name
,
140 const std::string
& error_message
);
142 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
143 // there is no matching completion call since we don't do anything special
145 void OnCancelPairingError(const std::string
& error_name
,
146 const std::string
& error_message
);
148 // Internal method to set the device as trusted. Trusted devices can connect
149 // to us automatically, and we can connect to them after rebooting; it also
150 // causes the device to be remembered by the stack even if not paired.
151 // |success| to the callback indicates whether or not the request succeeded.
153 void OnSetTrusted(bool success
);
155 // Called by dbus:: on completion of the D-Bus method call to disconnect the
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
);
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
);
169 // Called by dbus:: on completion of the D-Bus method call to start the
170 // connection monitor.
171 void OnStartConnectionMonitor(const base::Closure
& callback
);
172 void OnStartConnectionMonitorError(const ErrorCallback
& error_callback
,
173 const std::string
& error_name
,
174 const std::string
& error_message
);
176 // The adapter that owns this device instance.
177 BluetoothAdapterChromeOS
* adapter_
;
179 // The dbus object path of the device object.
180 dbus::ObjectPath object_path_
;
182 // List of observers interested in event notifications from us.
183 ObserverList
<device::BluetoothDevice::Observer
> observers_
;
185 // Number of ongoing calls to Connect().
186 int num_connecting_calls_
;
188 // True if the connection monitor has been started, tracking the connection
189 // RSSI and TX power.
190 bool connection_monitor_started_
;
192 // UI thread task runner and socket thread object used to create sockets.
193 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
194 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
196 // During pairing this is set to an object that we don't own, but on which
197 // we can make method calls to request, display or confirm PIN Codes and
198 // Passkeys. Generally it is the object that owns this one.
199 scoped_ptr
<BluetoothPairingChromeOS
> pairing_
;
201 // Note: This should remain the last member so it'll be destroyed and
202 // invalidate its weak pointers before any other members are destroyed.
203 base::WeakPtrFactory
<BluetoothDeviceChromeOS
> weak_ptr_factory_
;
205 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS
);
208 } // namespace chromeos
210 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H