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_ADAPTER_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
14 #include "base/memory/weak_ptr.h"
15 #include "base/sequenced_task_runner.h"
16 #include "chromeos/dbus/bluetooth_adapter_client.h"
17 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
18 #include "chromeos/dbus/bluetooth_device_client.h"
19 #include "chromeos/dbus/bluetooth_input_client.h"
20 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
21 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
22 #include "dbus/object_path.h"
23 #include "device/bluetooth/bluetooth_adapter.h"
24 #include "device/bluetooth/bluetooth_audio_sink.h"
25 #include "device/bluetooth/bluetooth_device.h"
26 #include "device/bluetooth/bluetooth_export.h"
29 class BluetoothSocketThread
;
34 class BluetoothChromeOSTest
;
35 class BluetoothAdapterProfileChromeOS
;
36 class BluetoothDeviceChromeOS
;
37 class BluetoothPairingChromeOS
;
38 class BluetoothRemoteGattCharacteristicChromeOS
;
39 class BluetoothRemoteGattDescriptorChromeOS
;
40 class BluetoothRemoteGattServiceChromeOS
;
42 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
43 // Chrome OS platform.
45 // Methods tolerate a shutdown scenario where BluetoothAdapterChromeOS::Shutdown
46 // causes IsPresent to return false just before the dbus system is shutdown but
47 // while references to the BluetoothAdapterChromeOS object still exists.
49 // When adding methods to this class verify shutdown behavior in
50 // BluetoothChromeOSTest, Shutdown.
51 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterChromeOS
52 : public device::BluetoothAdapter
,
53 public chromeos::BluetoothAdapterClient::Observer
,
54 public chromeos::BluetoothDeviceClient::Observer
,
55 public chromeos::BluetoothInputClient::Observer
,
56 public chromeos::BluetoothAgentServiceProvider::Delegate
{
58 typedef base::Callback
<void(const std::string
& error_message
)>
59 ErrorCompletionCallback
;
60 typedef base::Callback
<void(BluetoothAdapterProfileChromeOS
* profile
)>
61 ProfileRegisteredCallback
;
63 static base::WeakPtr
<BluetoothAdapter
> CreateAdapter();
66 void Shutdown() override
;
67 void DeleteOnCorrectThread() const override
;
68 void AddObserver(device::BluetoothAdapter::Observer
* observer
) override
;
69 void RemoveObserver(device::BluetoothAdapter::Observer
* observer
) override
;
70 std::string
GetAddress() const override
;
71 std::string
GetName() const override
;
72 void SetName(const std::string
& name
,
73 const base::Closure
& callback
,
74 const ErrorCallback
& error_callback
) override
;
75 bool IsInitialized() const override
;
76 bool IsPresent() const override
;
77 bool IsPowered() const override
;
78 void SetPowered(bool powered
,
79 const base::Closure
& callback
,
80 const ErrorCallback
& error_callback
) override
;
81 bool IsDiscoverable() const override
;
82 void SetDiscoverable(bool discoverable
,
83 const base::Closure
& callback
,
84 const ErrorCallback
& error_callback
) override
;
85 bool IsDiscovering() const override
;
86 void CreateRfcommService(
87 const device::BluetoothUUID
& uuid
,
88 const ServiceOptions
& options
,
89 const CreateServiceCallback
& callback
,
90 const CreateServiceErrorCallback
& error_callback
) override
;
91 void CreateL2capService(
92 const device::BluetoothUUID
& uuid
,
93 const ServiceOptions
& options
,
94 const CreateServiceCallback
& callback
,
95 const CreateServiceErrorCallback
& error_callback
) override
;
96 void RegisterAudioSink(
97 const device::BluetoothAudioSink::Options
& options
,
98 const device::BluetoothAdapter::AcquiredCallback
& callback
,
99 const device::BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
101 // Locates the device object by object path (the devices map and
102 // BluetoothDevice methods are by address).
103 BluetoothDeviceChromeOS
* GetDeviceWithPath(
104 const dbus::ObjectPath
& object_path
);
106 // Announce to observers a change in device state that is not reflected by
107 // its D-Bus properties.
108 void NotifyDeviceChanged(BluetoothDeviceChromeOS
* device
);
110 // The following methods are used to send various GATT observer events to
112 void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS
* service
);
113 void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS
* service
);
114 void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS
* service
);
115 void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS
* service
);
116 void NotifyGattCharacteristicAdded(
117 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
);
118 void NotifyGattCharacteristicRemoved(
119 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
);
120 void NotifyGattDescriptorAdded(
121 BluetoothRemoteGattDescriptorChromeOS
* descriptor
);
122 void NotifyGattDescriptorRemoved(
123 BluetoothRemoteGattDescriptorChromeOS
* descriptor
);
124 void NotifyGattCharacteristicValueChanged(
125 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
,
126 const std::vector
<uint8
>& value
);
127 void NotifyGattDescriptorValueChanged(
128 BluetoothRemoteGattDescriptorChromeOS
* descriptor
,
129 const std::vector
<uint8
>& value
);
131 // Returns the object path of the adapter.
132 const dbus::ObjectPath
& object_path() const { return object_path_
; }
134 // Request a profile on the adapter for a custom service with a
135 // specific UUID for the device at |device_path| to be sent to |delegate|.
136 // If |device_path| is the empty string, incoming connections will be
137 // assigned to |delegate|. When the profile is
138 // successfully registered, |success_callback| will be called with a pointer
139 // to the profile which is managed by BluetoothAdapterChromeOS. On failure,
140 // |error_callback| will be called.
141 void UseProfile(const device::BluetoothUUID
& uuid
,
142 const dbus::ObjectPath
& device_path
,
143 const BluetoothProfileManagerClient::Options
& options
,
144 BluetoothProfileServiceProvider::Delegate
* delegate
,
145 const ProfileRegisteredCallback
& success_callback
,
146 const ErrorCompletionCallback
& error_callback
);
148 // Release use of a profile by a device.
149 void ReleaseProfile(const dbus::ObjectPath
& device_path
,
150 BluetoothAdapterProfileChromeOS
* profile
);
154 void RemovePairingDelegateInternal(
155 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
158 friend class base::DeleteHelper
<BluetoothAdapterChromeOS
>;
159 friend class BluetoothChromeOSTest
;
160 friend class BluetoothChromeOSTest_Shutdown_Test
;
161 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscovery_Test
;
162 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscoveryError_Test
;
163 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscovery_Test
;
164 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscoveryError_Test
;
166 // typedef for callback parameters that are passed to AddDiscoverySession
167 // and RemoveDiscoverySession. This is used to queue incoming requests while
168 // a call to BlueZ is pending.
169 typedef std::pair
<base::Closure
, ErrorCallback
> DiscoveryCallbackPair
;
170 typedef std::queue
<DiscoveryCallbackPair
> DiscoveryCallbackQueue
;
172 BluetoothAdapterChromeOS();
173 ~BluetoothAdapterChromeOS() override
;
175 // BluetoothAdapterClient::Observer override.
176 void AdapterAdded(const dbus::ObjectPath
& object_path
) override
;
177 void AdapterRemoved(const dbus::ObjectPath
& object_path
) override
;
178 void AdapterPropertyChanged(const dbus::ObjectPath
& object_path
,
179 const std::string
& property_name
) override
;
181 // BluetoothDeviceClient::Observer override.
182 void DeviceAdded(const dbus::ObjectPath
& object_path
) override
;
183 void DeviceRemoved(const dbus::ObjectPath
& object_path
) override
;
184 void DevicePropertyChanged(const dbus::ObjectPath
& object_path
,
185 const std::string
& property_name
) override
;
187 // BluetoothInputClient::Observer override.
188 void InputPropertyChanged(const dbus::ObjectPath
& object_path
,
189 const std::string
& property_name
) override
;
191 // BluetoothAgentServiceProvider::Delegate override.
192 void Released() override
;
193 void RequestPinCode(const dbus::ObjectPath
& device_path
,
194 const PinCodeCallback
& callback
) override
;
195 void DisplayPinCode(const dbus::ObjectPath
& device_path
,
196 const std::string
& pincode
) override
;
197 void RequestPasskey(const dbus::ObjectPath
& device_path
,
198 const PasskeyCallback
& callback
) override
;
199 void DisplayPasskey(const dbus::ObjectPath
& device_path
,
201 uint16 entered
) override
;
202 void RequestConfirmation(const dbus::ObjectPath
& device_path
,
204 const ConfirmationCallback
& callback
) override
;
205 void RequestAuthorization(const dbus::ObjectPath
& device_path
,
206 const ConfirmationCallback
& callback
) override
;
207 void AuthorizeService(const dbus::ObjectPath
& device_path
,
208 const std::string
& uuid
,
209 const ConfirmationCallback
& callback
) override
;
210 void Cancel() override
;
212 // Called by dbus:: on completion of the D-Bus method call to register the
214 void OnRegisterAgent();
215 void OnRegisterAgentError(const std::string
& error_name
,
216 const std::string
& error_message
);
218 // Called by dbus:: on completion of the D-Bus method call to request that
219 // the pairing agent be made the default.
220 void OnRequestDefaultAgent();
221 void OnRequestDefaultAgentError(const std::string
& error_name
,
222 const std::string
& error_message
);
224 // Called by BluetoothAudioSinkChromeOS on completion of registering an audio
226 void OnRegisterAudioSink(
227 const device::BluetoothAdapter::AcquiredCallback
& callback
,
228 const device::BluetoothAudioSink::ErrorCallback
& error_callback
,
229 scoped_refptr
<device::BluetoothAudioSink
> audio_sink
);
231 // Internal method to obtain a BluetoothPairingChromeOS object for the device
232 // with path |object_path|. Returns the existing pairing object if the device
233 // already has one (usually an outgoing connection in progress) or a new
234 // pairing object with the default pairing delegate if not. If no default
235 // pairing object exists, NULL will be returned.
236 BluetoothPairingChromeOS
* GetPairing(const dbus::ObjectPath
& object_path
);
238 // Set the tracked adapter to the one in |object_path|, this object will
239 // subsequently operate on that adapter until it is removed.
240 void SetAdapter(const dbus::ObjectPath
& object_path
);
242 // Set the adapter name to one chosen from the system information.
243 void SetDefaultAdapterName();
245 // Remove the currently tracked adapter. IsPresent() will return false after
247 void RemoveAdapter();
249 // Announce to observers a change in the adapter state.
250 void PoweredChanged(bool powered
);
251 void DiscoverableChanged(bool discoverable
);
252 void DiscoveringChanged(bool discovering
);
253 void PresentChanged(bool present
);
255 // Called by dbus:: on completion of the discoverable property change.
256 void OnSetDiscoverable(const base::Closure
& callback
,
257 const ErrorCallback
& error_callback
,
260 // Called by dbus:: on completion of an adapter property change.
261 void OnPropertyChangeCompleted(const base::Closure
& callback
,
262 const ErrorCallback
& error_callback
,
266 void AddDiscoverySession(const base::Closure
& callback
,
267 const ErrorCallback
& error_callback
) override
;
268 void RemoveDiscoverySession(const base::Closure
& callback
,
269 const ErrorCallback
& error_callback
) override
;
271 // Called by dbus:: on completion of the D-Bus method call to start discovery.
272 void OnStartDiscovery(const base::Closure
& callback
,
273 const ErrorCallback
& error_callback
);
274 void OnStartDiscoveryError(const base::Closure
& callback
,
275 const ErrorCallback
& error_callback
,
276 const std::string
& error_name
,
277 const std::string
& error_message
);
279 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
280 void OnStopDiscovery(const base::Closure
& callback
);
281 void OnStopDiscoveryError(const ErrorCallback
& error_callback
,
282 const std::string
& error_name
,
283 const std::string
& error_message
);
285 // Called by dbus:: on completion of the D-Bus method to register a profile.
286 void OnRegisterProfile(const device::BluetoothUUID
& uuid
,
287 scoped_ptr
<BluetoothAdapterProfileChromeOS
> profile
);
289 void SetProfileDelegate(const device::BluetoothUUID
& uuid
,
290 const dbus::ObjectPath
& device_path
,
291 BluetoothProfileServiceProvider::Delegate
* delegate
,
292 const ProfileRegisteredCallback
& success_callback
,
293 const ErrorCompletionCallback
& error_callback
);
294 void OnRegisterProfileError(const device::BluetoothUUID
& uuid
,
295 const std::string
& error_name
,
296 const std::string
& error_message
);
298 // Called by BluetoothAdapterProfileChromeOS when no users of a profile
300 void RemoveProfile(const device::BluetoothUUID
& uuid
);
302 // Processes the queued discovery requests. For each DiscoveryCallbackPair in
303 // the queue, this method will try to add a new discovery session. This method
304 // is called whenever a pending D-Bus call to start or stop discovery has
305 // ended (with either success or failure).
306 void ProcessQueuedDiscoveryRequests();
308 // Set in |Shutdown()|, makes IsPresent()| return false.
309 bool dbus_is_shutdown_
;
311 // Number of discovery sessions that have been added.
312 int num_discovery_sessions_
;
314 // True, if there is a pending request to start or stop discovery.
315 bool discovery_request_pending_
;
317 // List of queued requests to add new discovery sessions. While there is a
318 // pending request to BlueZ to start or stop discovery, many requests from
319 // within Chrome to start or stop discovery sessions may occur. We only
320 // queue requests to add new sessions to be processed later. All requests to
321 // remove a session while a call is pending immediately return failure. Note
322 // that since BlueZ keeps its own reference count of applications that have
323 // requested discovery, dropping our count to 0 won't necessarily result in
324 // the controller actually stopping discovery if, for example, an application
325 // other than Chrome, such as bt_console, was also used to start discovery.
326 DiscoveryCallbackQueue discovery_request_queue_
;
328 // Object path of the adapter we track.
329 dbus::ObjectPath object_path_
;
331 // List of observers interested in event notifications from us.
332 ObserverList
<device::BluetoothAdapter::Observer
> observers_
;
334 // Instance of the D-Bus agent object used for pairing, initialized with
335 // our own class as its delegate.
336 scoped_ptr
<BluetoothAgentServiceProvider
> agent_
;
338 // UI thread task runner and socket thread object used to create sockets.
339 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
340 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
342 // The profiles we have registered with the bluetooth daemon.
343 std::map
<device::BluetoothUUID
, BluetoothAdapterProfileChromeOS
*> profiles_
;
345 // Callback pair for the profile registration queue.
346 typedef std::pair
<base::Closure
, ErrorCompletionCallback
>
347 RegisterProfileCompletionPair
;
349 // Queue of delegates waiting for a profile to register.
350 std::map
<device::BluetoothUUID
, std::vector
<RegisterProfileCompletionPair
>*>
353 // Note: This should remain the last member so it'll be destroyed and
354 // invalidate its weak pointers before any other members are destroyed.
355 base::WeakPtrFactory
<BluetoothAdapterChromeOS
> weak_ptr_factory_
;
357 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS
);
360 } // namespace chromeos
362 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_