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 IsGattConnected() const override
;
50 bool IsConnectable() const override
;
51 bool IsConnecting() const override
;
52 UUIDList
GetUUIDs() const override
;
53 int16
GetInquiryRSSI() const override
;
54 int16
GetInquiryTxPower() const override
;
55 bool ExpectingPinCode() const override
;
56 bool ExpectingPasskey() const override
;
57 bool ExpectingConfirmation() const override
;
58 void GetConnectionInfo(
59 const ConnectionInfoCallback
& callback
) override
;
60 void Connect(device::BluetoothDevice::PairingDelegate
* pairing_delegate
,
61 const base::Closure
& callback
,
62 const ConnectErrorCallback
& error_callback
) override
;
63 void SetPinCode(const std::string
& pincode
) override
;
64 void SetPasskey(uint32 passkey
) override
;
65 void ConfirmPairing() override
;
66 void RejectPairing() override
;
67 void CancelPairing() override
;
68 void Disconnect(const base::Closure
& callback
,
69 const ErrorCallback
& error_callback
) override
;
70 void Forget(const ErrorCallback
& error_callback
) override
;
71 void ConnectToService(
72 const device::BluetoothUUID
& uuid
,
73 const ConnectToServiceCallback
& callback
,
74 const ConnectToServiceErrorCallback
& error_callback
) override
;
75 void ConnectToServiceInsecurely(
76 const device::BluetoothUUID
& uuid
,
77 const ConnectToServiceCallback
& callback
,
78 const ConnectToServiceErrorCallback
& error_callback
) override
;
79 void CreateGattConnection(
80 const GattConnectionCallback
& callback
,
81 const ConnectErrorCallback
& error_callback
) override
;
83 // Creates a pairing object with the given delegate |pairing_delegate| and
84 // establishes it as the pairing context for this device. All pairing-related
85 // method calls will be forwarded to this object until it is released.
86 BluetoothPairingChromeOS
* BeginPairing(
87 BluetoothDevice::PairingDelegate
* pairing_delegate
);
89 // Releases the current pairing object, any pairing-related method calls will
93 // Returns the current pairing object or NULL if no pairing is in progress.
94 BluetoothPairingChromeOS
* GetPairing() const;
96 // Returns the object path of the device.
97 const dbus::ObjectPath
& object_path() const { return object_path_
; }
99 // Returns the adapter which owns this device instance.
100 BluetoothAdapterChromeOS
* adapter() const;
103 // BluetoothDevice override
104 std::string
GetDeviceName() const override
;
105 void CreateGattConnectionImpl() override
;
106 void DisconnectGatt() override
;
109 friend class BluetoothAdapterChromeOS
;
111 BluetoothDeviceChromeOS(
112 BluetoothAdapterChromeOS
* adapter
,
113 const dbus::ObjectPath
& object_path
,
114 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
115 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
116 ~BluetoothDeviceChromeOS() override
;
118 // BluetoothGattServiceClient::Observer overrides.
119 void GattServiceAdded(const dbus::ObjectPath
& object_path
) override
;
120 void GattServiceRemoved(const dbus::ObjectPath
& object_path
) override
;
122 // Called by dbus:: on completion of the D-Bus method call to get the
123 // connection attributes of the current connection to the device.
124 void OnGetConnInfo(const ConnectionInfoCallback
& callback
,
126 int16 transmit_power
,
127 int16 max_transmit_power
);
128 void OnGetConnInfoError(const ConnectionInfoCallback
& callback
,
129 const std::string
& error_name
,
130 const std::string
& error_message
);
132 // Internal method to initiate a connection to this device, and methods called
133 // by dbus:: on completion of the D-Bus method call.
134 void ConnectInternal(bool after_pairing
,
135 const base::Closure
& callback
,
136 const ConnectErrorCallback
& error_callback
);
137 void OnConnect(bool after_pairing
,
138 const base::Closure
& callback
);
139 void OnCreateGattConnection(const GattConnectionCallback
& callback
);
140 void OnConnectError(bool after_pairing
,
141 const ConnectErrorCallback
& error_callback
,
142 const std::string
& error_name
,
143 const std::string
& error_message
);
145 // Called by dbus:: on completion of the D-Bus method call to pair the device.
146 void OnPair(const base::Closure
& callback
,
147 const ConnectErrorCallback
& error_callback
);
148 void OnPairError(const ConnectErrorCallback
& error_callback
,
149 const std::string
& error_name
,
150 const std::string
& error_message
);
152 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
153 // there is no matching completion call since we don't do anything special
155 void OnCancelPairingError(const std::string
& error_name
,
156 const std::string
& error_message
);
158 // Internal method to set the device as trusted. Trusted devices can connect
159 // to us automatically, and we can connect to them after rebooting; it also
160 // causes the device to be remembered by the stack even if not paired.
161 // |success| to the callback indicates whether or not the request succeeded.
163 void OnSetTrusted(bool success
);
165 // Called by dbus:: on completion of the D-Bus method call to disconnect the
167 void OnDisconnect(const base::Closure
& callback
);
168 void OnDisconnectError(const ErrorCallback
& error_callback
,
169 const std::string
& error_name
,
170 const std::string
& error_message
);
172 // Called by dbus:: on failure of the D-Bus method call to unpair the device;
173 // there is no matching completion call since this object is deleted in the
174 // process of unpairing.
175 void OnForgetError(const ErrorCallback
& error_callback
,
176 const std::string
& error_name
,
177 const std::string
& error_message
);
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