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.
31 class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceChromeOS
32 : public device::BluetoothDevice
,
33 public BluetoothGattServiceClient::Observer
{
35 // BluetoothDevice override
36 uint32
GetBluetoothClass() const override
;
37 std::string
GetAddress() const override
;
38 VendorIDSource
GetVendorIDSource() const override
;
39 uint16
GetVendorID() const override
;
40 uint16
GetProductID() const override
;
41 uint16
GetDeviceID() const override
;
42 bool IsPaired() const override
;
43 bool IsConnected() const override
;
44 bool IsConnectable() const override
;
45 bool IsConnecting() const override
;
46 UUIDList
GetUUIDs() const override
;
47 bool ExpectingPinCode() const override
;
48 bool ExpectingPasskey() const override
;
49 bool ExpectingConfirmation() const override
;
50 void GetConnectionInfo(
51 const ConnectionInfoCallback
& callback
) override
;
52 void Connect(device::BluetoothDevice::PairingDelegate
* pairing_delegate
,
53 const base::Closure
& callback
,
54 const ConnectErrorCallback
& error_callback
) override
;
55 void SetPinCode(const std::string
& pincode
) override
;
56 void SetPasskey(uint32 passkey
) override
;
57 void ConfirmPairing() override
;
58 void RejectPairing() override
;
59 void CancelPairing() override
;
60 void Disconnect(const base::Closure
& callback
,
61 const ErrorCallback
& error_callback
) override
;
62 void Forget(const ErrorCallback
& error_callback
) override
;
63 void ConnectToService(
64 const device::BluetoothUUID
& uuid
,
65 const ConnectToServiceCallback
& callback
,
66 const ConnectToServiceErrorCallback
& error_callback
) override
;
67 void ConnectToServiceInsecurely(
68 const device::BluetoothUUID
& uuid
,
69 const ConnectToServiceCallback
& callback
,
70 const ConnectToServiceErrorCallback
& error_callback
) override
;
71 void CreateGattConnection(
72 const GattConnectionCallback
& callback
,
73 const ConnectErrorCallback
& error_callback
) override
;
75 // Creates a pairing object with the given delegate |pairing_delegate| and
76 // establishes it as the pairing context for this device. All pairing-related
77 // method calls will be forwarded to this object until it is released.
78 BluetoothPairingChromeOS
* BeginPairing(
79 BluetoothDevice::PairingDelegate
* pairing_delegate
);
81 // Releases the current pairing object, any pairing-related method calls will
85 // Returns the current pairing object or NULL if no pairing is in progress.
86 BluetoothPairingChromeOS
* GetPairing() const;
88 // Returns the object path of the device.
89 const dbus::ObjectPath
& object_path() const { return object_path_
; }
91 // Returns the adapter which owns this device instance.
92 BluetoothAdapterChromeOS
* adapter() const { return adapter_
; }
95 // BluetoothDevice override
96 std::string
GetDeviceName() const override
;
99 friend class BluetoothAdapterChromeOS
;
101 BluetoothDeviceChromeOS(
102 BluetoothAdapterChromeOS
* adapter
,
103 const dbus::ObjectPath
& object_path
,
104 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
105 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
106 ~BluetoothDeviceChromeOS() override
;
108 // BluetoothGattServiceClient::Observer overrides.
109 void GattServiceAdded(const dbus::ObjectPath
& object_path
) override
;
110 void GattServiceRemoved(const dbus::ObjectPath
& object_path
) override
;
112 // Called by dbus:: on completion of the D-Bus method call to get the
113 // connection attributes of the current connection to the device.
114 void OnGetConnInfo(const ConnectionInfoCallback
& callback
,
116 int16 transmit_power
,
117 int16 max_transmit_power
);
118 void OnGetConnInfoError(const ConnectionInfoCallback
& callback
,
119 const std::string
& error_name
,
120 const std::string
& error_message
);
122 // Internal method to initiate a connection to this device, and methods called
123 // by dbus:: on completion of the D-Bus method call.
124 void ConnectInternal(bool after_pairing
,
125 const base::Closure
& callback
,
126 const ConnectErrorCallback
& error_callback
);
127 void OnConnect(bool after_pairing
,
128 const base::Closure
& callback
);
129 void OnCreateGattConnection(const GattConnectionCallback
& 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 // The adapter that owns this device instance.
170 BluetoothAdapterChromeOS
* adapter_
;
172 // The dbus object path of the device object.
173 dbus::ObjectPath object_path_
;
175 // Number of ongoing calls to Connect().
176 int num_connecting_calls_
;
178 // True if the connection monitor has been started, tracking the connection
179 // RSSI and TX power.
180 bool connection_monitor_started_
;
182 // UI thread task runner and socket thread object used to create sockets.
183 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
184 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
186 // During pairing this is set to an object that we don't own, but on which
187 // we can make method calls to request, display or confirm PIN Codes and
188 // Passkeys. Generally it is the object that owns this one.
189 scoped_ptr
<BluetoothPairingChromeOS
> pairing_
;
191 // Note: This should remain the last member so it'll be destroyed and
192 // invalidate its weak pointers before any other members are destroyed.
193 base::WeakPtrFactory
<BluetoothDeviceChromeOS
> weak_ptr_factory_
;
195 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS
);
198 } // namespace chromeos
200 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H