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 int16
GetInquiryRSSI() const override
;
48 int16
GetInquiryTxPower() const override
;
49 bool ExpectingPinCode() const override
;
50 bool ExpectingPasskey() const override
;
51 bool ExpectingConfirmation() const override
;
52 void GetConnectionInfo(
53 const ConnectionInfoCallback
& callback
) override
;
54 void Connect(device::BluetoothDevice::PairingDelegate
* pairing_delegate
,
55 const base::Closure
& callback
,
56 const ConnectErrorCallback
& error_callback
) override
;
57 void SetPinCode(const std::string
& pincode
) override
;
58 void SetPasskey(uint32 passkey
) override
;
59 void ConfirmPairing() override
;
60 void RejectPairing() override
;
61 void CancelPairing() override
;
62 void Disconnect(const base::Closure
& callback
,
63 const ErrorCallback
& error_callback
) override
;
64 void Forget(const ErrorCallback
& error_callback
) override
;
65 void ConnectToService(
66 const device::BluetoothUUID
& uuid
,
67 const ConnectToServiceCallback
& callback
,
68 const ConnectToServiceErrorCallback
& error_callback
) override
;
69 void ConnectToServiceInsecurely(
70 const device::BluetoothUUID
& uuid
,
71 const ConnectToServiceCallback
& callback
,
72 const ConnectToServiceErrorCallback
& error_callback
) override
;
73 void CreateGattConnection(
74 const GattConnectionCallback
& callback
,
75 const ConnectErrorCallback
& error_callback
) override
;
77 // Creates a pairing object with the given delegate |pairing_delegate| and
78 // establishes it as the pairing context for this device. All pairing-related
79 // method calls will be forwarded to this object until it is released.
80 BluetoothPairingChromeOS
* BeginPairing(
81 BluetoothDevice::PairingDelegate
* pairing_delegate
);
83 // Releases the current pairing object, any pairing-related method calls will
87 // Returns the current pairing object or NULL if no pairing is in progress.
88 BluetoothPairingChromeOS
* GetPairing() const;
90 // Returns the object path of the device.
91 const dbus::ObjectPath
& object_path() const { return object_path_
; }
93 // Returns the adapter which owns this device instance.
94 BluetoothAdapterChromeOS
* adapter() const { return adapter_
; }
97 // BluetoothDevice override
98 std::string
GetDeviceName() const override
;
101 friend class BluetoothAdapterChromeOS
;
103 BluetoothDeviceChromeOS(
104 BluetoothAdapterChromeOS
* adapter
,
105 const dbus::ObjectPath
& object_path
,
106 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
107 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
108 ~BluetoothDeviceChromeOS() override
;
110 // BluetoothGattServiceClient::Observer overrides.
111 void GattServiceAdded(const dbus::ObjectPath
& object_path
) override
;
112 void GattServiceRemoved(const dbus::ObjectPath
& object_path
) override
;
114 // Called by dbus:: on completion of the D-Bus method call to get the
115 // connection attributes of the current connection to the device.
116 void OnGetConnInfo(const ConnectionInfoCallback
& callback
,
118 int16 transmit_power
,
119 int16 max_transmit_power
);
120 void OnGetConnInfoError(const ConnectionInfoCallback
& callback
,
121 const std::string
& error_name
,
122 const std::string
& error_message
);
124 // Internal method to initiate a connection to this device, and methods called
125 // by dbus:: on completion of the D-Bus method call.
126 void ConnectInternal(bool after_pairing
,
127 const base::Closure
& callback
,
128 const ConnectErrorCallback
& error_callback
);
129 void OnConnect(bool after_pairing
,
130 const base::Closure
& callback
);
131 void OnCreateGattConnection(const GattConnectionCallback
& callback
);
132 void OnConnectError(bool after_pairing
,
133 const ConnectErrorCallback
& error_callback
,
134 const std::string
& error_name
,
135 const std::string
& error_message
);
137 // Called by dbus:: on completion of the D-Bus method call to pair the device.
138 void OnPair(const base::Closure
& callback
,
139 const ConnectErrorCallback
& error_callback
);
140 void OnPairError(const ConnectErrorCallback
& error_callback
,
141 const std::string
& error_name
,
142 const std::string
& error_message
);
144 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
145 // there is no matching completion call since we don't do anything special
147 void OnCancelPairingError(const std::string
& error_name
,
148 const std::string
& error_message
);
150 // Internal method to set the device as trusted. Trusted devices can connect
151 // to us automatically, and we can connect to them after rebooting; it also
152 // causes the device to be remembered by the stack even if not paired.
153 // |success| to the callback indicates whether or not the request succeeded.
155 void OnSetTrusted(bool success
);
157 // Called by dbus:: on completion of the D-Bus method call to disconnect the
159 void OnDisconnect(const base::Closure
& callback
);
160 void OnDisconnectError(const ErrorCallback
& error_callback
,
161 const std::string
& error_name
,
162 const std::string
& error_message
);
164 // Called by dbus:: on failure of the D-Bus method call to unpair the device;
165 // there is no matching completion call since this object is deleted in the
166 // process of unpairing.
167 void OnForgetError(const ErrorCallback
& error_callback
,
168 const std::string
& error_name
,
169 const std::string
& error_message
);
171 // The adapter that owns this device instance.
172 BluetoothAdapterChromeOS
* adapter_
;
174 // The dbus object path of the device object.
175 dbus::ObjectPath object_path_
;
177 // Number of ongoing calls to Connect().
178 int num_connecting_calls_
;
180 // True if the connection monitor has been started, tracking the connection
181 // RSSI and TX power.
182 bool connection_monitor_started_
;
184 // UI thread task runner and socket thread object used to create sockets.
185 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
186 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
188 // During pairing this is set to an object that we don't own, but on which
189 // we can make method calls to request, display or confirm PIN Codes and
190 // Passkeys. Generally it is the object that owns this one.
191 scoped_ptr
<BluetoothPairingChromeOS
> pairing_
;
193 // Note: This should remain the last member so it'll be destroyed and
194 // invalidate its weak pointers before any other members are destroyed.
195 base::WeakPtrFactory
<BluetoothDeviceChromeOS
> weak_ptr_factory_
;
197 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS
);
200 } // namespace chromeos
202 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H