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 ConnectToServiceInsecurely(
70 const device::BluetoothUUID
& uuid
,
71 const ConnectToServiceCallback
& callback
,
72 const ConnectToServiceErrorCallback
& error_callback
) override
;
73 virtual void CreateGattConnection(
74 const GattConnectionCallback
& callback
,
75 const ConnectErrorCallback
& error_callback
) override
;
76 virtual void StartConnectionMonitor(
77 const base::Closure
& callback
,
78 const ErrorCallback
& error_callback
) override
;
80 // Creates a pairing object with the given delegate |pairing_delegate| and
81 // establishes it as the pairing context for this device. All pairing-related
82 // method calls will be forwarded to this object until it is released.
83 BluetoothPairingChromeOS
* BeginPairing(
84 BluetoothDevice::PairingDelegate
* pairing_delegate
);
86 // Releases the current pairing object, any pairing-related method calls will
90 // Returns the current pairing object or NULL if no pairing is in progress.
91 BluetoothPairingChromeOS
* GetPairing() const;
93 // Returns the object path of the device.
94 const dbus::ObjectPath
& object_path() const { return object_path_
; }
97 // BluetoothDevice override
98 virtual 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 virtual ~BluetoothDeviceChromeOS();
110 // BluetoothGattServiceClient::Observer overrides.
111 virtual void GattServiceAdded(const dbus::ObjectPath
& object_path
) override
;
112 virtual void GattServiceRemoved(const dbus::ObjectPath
& object_path
) override
;
114 // Internal method to initiate a connection to this device, and methods called
115 // by dbus:: on completion of the D-Bus method call.
116 void ConnectInternal(bool after_pairing
,
117 const base::Closure
& callback
,
118 const ConnectErrorCallback
& error_callback
);
119 void OnConnect(bool after_pairing
,
120 const base::Closure
& callback
);
121 void OnCreateGattConnection(const GattConnectionCallback
& callback
);
122 void OnConnectError(bool after_pairing
,
123 const ConnectErrorCallback
& error_callback
,
124 const std::string
& error_name
,
125 const std::string
& error_message
);
127 // Called by dbus:: on completion of the D-Bus method call to pair the device.
128 void OnPair(const base::Closure
& callback
,
129 const ConnectErrorCallback
& error_callback
);
130 void OnPairError(const ConnectErrorCallback
& error_callback
,
131 const std::string
& error_name
,
132 const std::string
& error_message
);
134 // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
135 // there is no matching completion call since we don't do anything special
137 void OnCancelPairingError(const std::string
& error_name
,
138 const std::string
& error_message
);
140 // Internal method to set the device as trusted. Trusted devices can connect
141 // to us automatically, and we can connect to them after rebooting; it also
142 // causes the device to be remembered by the stack even if not paired.
143 // |success| to the callback indicates whether or not the request succeeded.
145 void OnSetTrusted(bool success
);
147 // Called by dbus:: on completion of the D-Bus method call to disconnect the
149 void OnDisconnect(const base::Closure
& callback
);
150 void OnDisconnectError(const ErrorCallback
& error_callback
,
151 const std::string
& error_name
,
152 const std::string
& error_message
);
154 // Called by dbus:: on failure of the D-Bus method call to unpair the device;
155 // there is no matching completion call since this object is deleted in the
156 // process of unpairing.
157 void OnForgetError(const ErrorCallback
& error_callback
,
158 const std::string
& error_name
,
159 const std::string
& error_message
);
161 // Called by dbus:: on completion of the D-Bus method call to start the
162 // connection monitor.
163 void OnStartConnectionMonitor(const base::Closure
& callback
);
164 void OnStartConnectionMonitorError(const ErrorCallback
& error_callback
,
165 const std::string
& error_name
,
166 const std::string
& error_message
);
168 // The adapter that owns this device instance.
169 BluetoothAdapterChromeOS
* adapter_
;
171 // The dbus object path of the device object.
172 dbus::ObjectPath object_path_
;
174 // Number of ongoing calls to Connect().
175 int num_connecting_calls_
;
177 // True if the connection monitor has been started, tracking the connection
178 // RSSI and TX power.
179 bool connection_monitor_started_
;
181 // UI thread task runner and socket thread object used to create sockets.
182 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
183 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
185 // During pairing this is set to an object that we don't own, but on which
186 // we can make method calls to request, display or confirm PIN Codes and
187 // Passkeys. Generally it is the object that owns this one.
188 scoped_ptr
<BluetoothPairingChromeOS
> pairing_
;
190 // Note: This should remain the last member so it'll be destroyed and
191 // invalidate its weak pointers before any other members are destroyed.
192 base::WeakPtrFactory
<BluetoothDeviceChromeOS
> weak_ptr_factory_
;
194 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS
);
197 } // namespace chromeos
199 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H