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"
18 #include "device/bluetooth/bluetooth_export.h"
21 class BluetoothSocketThread
;
26 class BluetoothAdapterChromeOS
;
27 class BluetoothPairingChromeOS
;
29 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
30 // Chrome OS platform.
32 // This class is not thread-safe, but is only called from the UI thread.
34 // A socket thread is used to create sockets but posts all callbacks on the UI
36 class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceChromeOS
37 : public device::BluetoothDevice
,
38 public BluetoothGattServiceClient::Observer
{
40 // BluetoothDevice override
41 uint32
GetBluetoothClass() const override
;
42 std::string
GetAddress() const override
;
43 VendorIDSource
GetVendorIDSource() const override
;
44 uint16
GetVendorID() const override
;
45 uint16
GetProductID() const override
;
46 uint16
GetDeviceID() const override
;
47 bool IsPaired() const override
;
48 bool IsConnected() const override
;
49 bool IsConnectable() const override
;
50 bool IsConnecting() const override
;
51 UUIDList
GetUUIDs() const override
;
52 int16
GetInquiryRSSI() const override
;
53 int16
GetInquiryTxPower() const override
;
54 bool ExpectingPinCode() const override
;
55 bool ExpectingPasskey() const override
;
56 bool ExpectingConfirmation() const override
;
57 void GetConnectionInfo(
58 const ConnectionInfoCallback
& callback
) override
;
59 void Connect(device::BluetoothDevice::PairingDelegate
* pairing_delegate
,
60 const base::Closure
& callback
,
61 const ConnectErrorCallback
& error_callback
) override
;
62 void SetPinCode(const std::string
& pincode
) override
;
63 void SetPasskey(uint32 passkey
) override
;
64 void ConfirmPairing() override
;
65 void RejectPairing() override
;
66 void CancelPairing() override
;
67 void Disconnect(const base::Closure
& callback
,
68 const ErrorCallback
& error_callback
) override
;
69 void Forget(const ErrorCallback
& error_callback
) override
;
70 void ConnectToService(
71 const device::BluetoothUUID
& uuid
,
72 const ConnectToServiceCallback
& callback
,
73 const ConnectToServiceErrorCallback
& error_callback
) override
;
74 void ConnectToServiceInsecurely(
75 const device::BluetoothUUID
& uuid
,
76 const ConnectToServiceCallback
& callback
,
77 const ConnectToServiceErrorCallback
& error_callback
) override
;
78 void CreateGattConnection(
79 const GattConnectionCallback
& callback
,
80 const ConnectErrorCallback
& error_callback
) override
;
82 // Creates a pairing object with the given delegate |pairing_delegate| and
83 // establishes it as the pairing context for this device. All pairing-related
84 // method calls will be forwarded to this object until it is released.
85 BluetoothPairingChromeOS
* BeginPairing(
86 BluetoothDevice::PairingDelegate
* pairing_delegate
);
88 // Releases the current pairing object, any pairing-related method calls will
92 // Returns the current pairing object or NULL if no pairing is in progress.
93 BluetoothPairingChromeOS
* GetPairing() const;
95 // Returns the object path of the device.
96 const dbus::ObjectPath
& object_path() const { return object_path_
; }
98 // Returns the adapter which owns this device instance.
99 BluetoothAdapterChromeOS
* adapter() const { return adapter_
; }
102 // BluetoothDevice override
103 std::string
GetDeviceName() const override
;
106 friend class BluetoothAdapterChromeOS
;
108 BluetoothDeviceChromeOS(
109 BluetoothAdapterChromeOS
* adapter
,
110 const dbus::ObjectPath
& object_path
,
111 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
112 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
113 ~BluetoothDeviceChromeOS() override
;
115 // BluetoothGattServiceClient::Observer overrides.
116 void GattServiceAdded(const dbus::ObjectPath
& object_path
) override
;
117 void GattServiceRemoved(const dbus::ObjectPath
& object_path
) override
;
119 // Called by dbus:: on completion of the D-Bus method call to get the
120 // connection attributes of the current connection to the device.
121 void OnGetConnInfo(const ConnectionInfoCallback
& callback
,
123 int16 transmit_power
,
124 int16 max_transmit_power
);
125 void OnGetConnInfoError(const ConnectionInfoCallback
& callback
,
126 const std::string
& error_name
,
127 const std::string
& error_message
);
129 // Internal method to initiate a connection to this device, and methods called
130 // by dbus:: on completion of the D-Bus method call.
131 void ConnectInternal(bool after_pairing
,
132 const base::Closure
& callback
,
133 const ConnectErrorCallback
& error_callback
);
134 void OnConnect(bool after_pairing
,
135 const base::Closure
& callback
);
136 void OnCreateGattConnection(const GattConnectionCallback
& callback
);
137 void OnConnectError(bool after_pairing
,
138 const ConnectErrorCallback
& error_callback
,
139 const std::string
& error_name
,
140 const std::string
& error_message
);
142 // Called by dbus:: on completion of the D-Bus method call to pair the device.
143 void OnPair(const base::Closure
& callback
,
144 const ConnectErrorCallback
& error_callback
);
145 void OnPairError(const ConnectErrorCallback
& error_callback
,
146 const std::string
& error_name
,
147 const std::string
& error_message
);
149 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
150 // there is no matching completion call since we don't do anything special
152 void OnCancelPairingError(const std::string
& error_name
,
153 const std::string
& error_message
);
155 // Internal method to set the device as trusted. Trusted devices can connect
156 // to us automatically, and we can connect to them after rebooting; it also
157 // causes the device to be remembered by the stack even if not paired.
158 // |success| to the callback indicates whether or not the request succeeded.
160 void OnSetTrusted(bool success
);
162 // Called by dbus:: on completion of the D-Bus method call to disconnect the
164 void OnDisconnect(const base::Closure
& callback
);
165 void OnDisconnectError(const ErrorCallback
& error_callback
,
166 const std::string
& error_name
,
167 const std::string
& error_message
);
169 // Called by dbus:: on failure of the D-Bus method call to unpair the device;
170 // there is no matching completion call since this object is deleted in the
171 // process of unpairing.
172 void OnForgetError(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 // Number of ongoing calls to Connect().
183 int num_connecting_calls_
;
185 // True if the connection monitor has been started, tracking the connection
186 // RSSI and TX power.
187 bool connection_monitor_started_
;
189 // UI thread task runner and socket thread object used to create sockets.
190 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
191 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
193 // During pairing this is set to an object that we don't own, but on which
194 // we can make method calls to request, display or confirm PIN Codes and
195 // Passkeys. Generally it is the object that owns this one.
196 scoped_ptr
<BluetoothPairingChromeOS
> pairing_
;
198 // Note: This should remain the last member so it'll be destroyed and
199 // invalidate its weak pointers before any other members are destroyed.
200 base::WeakPtrFactory
<BluetoothDeviceChromeOS
> weak_ptr_factory_
;
202 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS
);
205 } // namespace chromeos
207 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H