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 // 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
);
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 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
);
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
);
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
146 void OnCancelPairingError(const std::string
& error_name
,
147 const std::string
& error_message
);
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.
154 void OnSetTrusted(bool success
);
156 // Called by dbus:: on completion of the D-Bus method call to disconnect the
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
);
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
);
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
);
177 // The adapter that owns this device instance.
178 BluetoothAdapterChromeOS
* adapter_
;
180 // The dbus object path of the device object.
181 dbus::ObjectPath object_path_
;
183 // Number of ongoing calls to Connect().
184 int num_connecting_calls_
;
186 // True if the connection monitor has been started, tracking the connection
187 // RSSI and TX power.
188 bool connection_monitor_started_
;
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_
;
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_
;
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_
;
203 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS
);
206 } // namespace chromeos
208 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H