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 "chromeos/dbus/bluetooth_adapter_client.h"
16 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
17 #include "chromeos/dbus/bluetooth_device_client.h"
18 #include "chromeos/dbus/bluetooth_input_client.h"
19 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
20 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
21 #include "dbus/object_path.h"
22 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_audio_sink.h"
24 #include "device/bluetooth/bluetooth_device.h"
25 #include "device/bluetooth/bluetooth_discovery_session.h"
26 #include "device/bluetooth/bluetooth_export.h"
29 class SequencedTaskRunner
;
33 class BluetoothSocketThread
;
38 class BluetoothChromeOSTest
;
39 class BluetoothAdapterProfileChromeOS
;
40 class BluetoothDeviceChromeOS
;
41 class BluetoothPairingChromeOS
;
42 class BluetoothRemoteGattCharacteristicChromeOS
;
43 class BluetoothRemoteGattDescriptorChromeOS
;
44 class BluetoothRemoteGattServiceChromeOS
;
46 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
47 // Chrome OS platform.
49 // All methods are called from the dbus origin / UI thread and are generally
50 // not assumed to be thread-safe.
52 // This class interacts with sockets using the BluetoothSocketThread to ensure
53 // single-threaded calls, and posts tasks to the UI thread.
55 // Methods tolerate a shutdown scenario where BluetoothAdapterChromeOS::Shutdown
56 // causes IsPresent to return false just before the dbus system is shutdown but
57 // while references to the BluetoothAdapterChromeOS object still exists.
59 // When adding methods to this class verify shutdown behavior in
60 // BluetoothChromeOSTest, Shutdown.
61 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterChromeOS
62 : public device::BluetoothAdapter
,
63 public chromeos::BluetoothAdapterClient::Observer
,
64 public chromeos::BluetoothDeviceClient::Observer
,
65 public chromeos::BluetoothInputClient::Observer
,
66 public chromeos::BluetoothAgentServiceProvider::Delegate
{
68 typedef base::Callback
<void(const std::string
& error_message
)>
69 ErrorCompletionCallback
;
70 typedef base::Callback
<void(BluetoothAdapterProfileChromeOS
* profile
)>
71 ProfileRegisteredCallback
;
73 static base::WeakPtr
<BluetoothAdapter
> CreateAdapter();
76 void Shutdown() override
;
77 std::string
GetAddress() const override
;
78 std::string
GetName() const override
;
79 void SetName(const std::string
& name
,
80 const base::Closure
& callback
,
81 const ErrorCallback
& error_callback
) override
;
82 bool IsInitialized() const override
;
83 bool IsPresent() const override
;
84 bool IsPowered() const override
;
85 void SetPowered(bool powered
,
86 const base::Closure
& callback
,
87 const ErrorCallback
& error_callback
) override
;
88 bool IsDiscoverable() const override
;
89 void SetDiscoverable(bool discoverable
,
90 const base::Closure
& callback
,
91 const ErrorCallback
& error_callback
) override
;
92 bool IsDiscovering() const override
;
93 void CreateRfcommService(
94 const device::BluetoothUUID
& uuid
,
95 const ServiceOptions
& options
,
96 const CreateServiceCallback
& callback
,
97 const CreateServiceErrorCallback
& error_callback
) override
;
98 void CreateL2capService(
99 const device::BluetoothUUID
& uuid
,
100 const ServiceOptions
& options
,
101 const CreateServiceCallback
& callback
,
102 const CreateServiceErrorCallback
& error_callback
) override
;
103 void RegisterAudioSink(
104 const device::BluetoothAudioSink::Options
& options
,
105 const device::BluetoothAdapter::AcquiredCallback
& callback
,
106 const device::BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
108 void RegisterAdvertisement(
109 scoped_ptr
<device::BluetoothAdvertisement::Data
> advertisement_data
,
110 const CreateAdvertisementCallback
& callback
,
111 const CreateAdvertisementErrorCallback
& error_callback
) override
;
113 // Locates the device object by object path (the devices map and
114 // BluetoothDevice methods are by address).
115 BluetoothDeviceChromeOS
* GetDeviceWithPath(
116 const dbus::ObjectPath
& object_path
);
118 // Announces to observers a change in device state that is not reflected by
119 // its D-Bus properties. |device| is owned by the caller and cannot be NULL.
120 void NotifyDeviceChanged(BluetoothDeviceChromeOS
* device
);
122 // The following methods are used to send various GATT observer events to
124 void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS
* service
);
125 void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS
* service
);
126 void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS
* service
);
127 void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS
* service
);
128 void NotifyGattCharacteristicAdded(
129 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
);
130 void NotifyGattCharacteristicRemoved(
131 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
);
132 void NotifyGattDescriptorAdded(
133 BluetoothRemoteGattDescriptorChromeOS
* descriptor
);
134 void NotifyGattDescriptorRemoved(
135 BluetoothRemoteGattDescriptorChromeOS
* descriptor
);
136 void NotifyGattCharacteristicValueChanged(
137 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
,
138 const std::vector
<uint8
>& value
);
139 void NotifyGattDescriptorValueChanged(
140 BluetoothRemoteGattDescriptorChromeOS
* descriptor
,
141 const std::vector
<uint8
>& value
);
143 // Returns the object path of the adapter.
144 const dbus::ObjectPath
& object_path() const { return object_path_
; }
146 // Request a profile on the adapter for a custom service with a
147 // specific UUID for the device at |device_path| to be sent to |delegate|.
148 // If |device_path| is the empty string, incoming connections will be
149 // assigned to |delegate|. When the profile is
150 // successfully registered, |success_callback| will be called with a pointer
151 // to the profile which is managed by BluetoothAdapterChromeOS. On failure,
152 // |error_callback| will be called.
153 void UseProfile(const device::BluetoothUUID
& uuid
,
154 const dbus::ObjectPath
& device_path
,
155 const BluetoothProfileManagerClient::Options
& options
,
156 BluetoothProfileServiceProvider::Delegate
* delegate
,
157 const ProfileRegisteredCallback
& success_callback
,
158 const ErrorCompletionCallback
& error_callback
);
160 // Release use of a profile by a device.
161 void ReleaseProfile(const dbus::ObjectPath
& device_path
,
162 BluetoothAdapterProfileChromeOS
* profile
);
166 void RemovePairingDelegateInternal(
167 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
170 friend class BluetoothChromeOSTest
;
171 friend class BluetoothChromeOSTest_Shutdown_Test
;
172 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscovery_Test
;
173 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscoveryError_Test
;
174 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscovery_Test
;
175 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscoveryError_Test
;
177 // typedef for callback parameters that are passed to AddDiscoverySession
178 // and RemoveDiscoverySession. This is used to queue incoming requests while
179 // a call to BlueZ is pending.
180 typedef std::tuple
<device::BluetoothDiscoveryFilter
*,
182 DiscoverySessionErrorCallback
> DiscoveryParamTuple
;
183 typedef std::queue
<DiscoveryParamTuple
> DiscoveryCallbackQueue
;
185 // Callback pair for the profile registration queue.
186 typedef std::pair
<base::Closure
, ErrorCompletionCallback
>
187 RegisterProfileCompletionPair
;
189 BluetoothAdapterChromeOS();
190 ~BluetoothAdapterChromeOS() override
;
192 // BluetoothAdapterClient::Observer override.
193 void AdapterAdded(const dbus::ObjectPath
& object_path
) override
;
194 void AdapterRemoved(const dbus::ObjectPath
& object_path
) override
;
195 void AdapterPropertyChanged(const dbus::ObjectPath
& object_path
,
196 const std::string
& property_name
) override
;
198 // BluetoothDeviceClient::Observer override.
199 void DeviceAdded(const dbus::ObjectPath
& object_path
) override
;
200 void DeviceRemoved(const dbus::ObjectPath
& object_path
) override
;
201 void DevicePropertyChanged(const dbus::ObjectPath
& object_path
,
202 const std::string
& property_name
) override
;
204 // BluetoothInputClient::Observer override.
205 void InputPropertyChanged(const dbus::ObjectPath
& object_path
,
206 const std::string
& property_name
) override
;
208 // BluetoothAgentServiceProvider::Delegate override.
209 void Released() override
;
210 void RequestPinCode(const dbus::ObjectPath
& device_path
,
211 const PinCodeCallback
& callback
) override
;
212 void DisplayPinCode(const dbus::ObjectPath
& device_path
,
213 const std::string
& pincode
) override
;
214 void RequestPasskey(const dbus::ObjectPath
& device_path
,
215 const PasskeyCallback
& callback
) override
;
216 void DisplayPasskey(const dbus::ObjectPath
& device_path
,
218 uint16 entered
) override
;
219 void RequestConfirmation(const dbus::ObjectPath
& device_path
,
221 const ConfirmationCallback
& callback
) override
;
222 void RequestAuthorization(const dbus::ObjectPath
& device_path
,
223 const ConfirmationCallback
& callback
) override
;
224 void AuthorizeService(const dbus::ObjectPath
& device_path
,
225 const std::string
& uuid
,
226 const ConfirmationCallback
& callback
) override
;
227 void Cancel() override
;
229 // Called by dbus:: on completion of the D-Bus method call to register the
231 void OnRegisterAgent();
232 void OnRegisterAgentError(const std::string
& error_name
,
233 const std::string
& error_message
);
235 // Called by dbus:: on completion of the D-Bus method call to request that
236 // the pairing agent be made the default.
237 void OnRequestDefaultAgent();
238 void OnRequestDefaultAgentError(const std::string
& error_name
,
239 const std::string
& error_message
);
241 // Called by BluetoothAudioSinkChromeOS on completion of registering an audio
243 void OnRegisterAudioSink(
244 const device::BluetoothAdapter::AcquiredCallback
& callback
,
245 const device::BluetoothAudioSink::ErrorCallback
& error_callback
,
246 scoped_refptr
<device::BluetoothAudioSink
> audio_sink
);
248 // Internal method to obtain a BluetoothPairingChromeOS object for the device
249 // with path |object_path|. Returns the existing pairing object if the device
250 // already has one (usually an outgoing connection in progress) or a new
251 // pairing object with the default pairing delegate if not. If no default
252 // pairing object exists, NULL will be returned.
253 BluetoothPairingChromeOS
* GetPairing(const dbus::ObjectPath
& object_path
);
255 // Set the tracked adapter to the one in |object_path|, this object will
256 // subsequently operate on that adapter until it is removed.
257 void SetAdapter(const dbus::ObjectPath
& object_path
);
259 // Set the adapter name to one chosen from the system information.
260 void SetDefaultAdapterName();
262 // Remove the currently tracked adapter. IsPresent() will return false after
264 void RemoveAdapter();
266 // Announce to observers a change in the adapter state.
267 void PoweredChanged(bool powered
);
268 void DiscoverableChanged(bool discoverable
);
269 void DiscoveringChanged(bool discovering
);
270 void PresentChanged(bool present
);
272 // Called by dbus:: on completion of the discoverable property change.
273 void OnSetDiscoverable(const base::Closure
& callback
,
274 const ErrorCallback
& error_callback
,
277 // Called by dbus:: on completion of an adapter property change.
278 void OnPropertyChangeCompleted(const base::Closure
& callback
,
279 const ErrorCallback
& error_callback
,
283 void AddDiscoverySession(
284 device::BluetoothDiscoveryFilter
* discovery_filter
,
285 const base::Closure
& callback
,
286 const DiscoverySessionErrorCallback
& error_callback
) override
;
287 void RemoveDiscoverySession(
288 device::BluetoothDiscoveryFilter
* discovery_filter
,
289 const base::Closure
& callback
,
290 const DiscoverySessionErrorCallback
& error_callback
) override
;
291 void SetDiscoveryFilter(
292 scoped_ptr
<device::BluetoothDiscoveryFilter
> discovery_filter
,
293 const base::Closure
& callback
,
294 const DiscoverySessionErrorCallback
& error_callback
) override
;
296 // Called by dbus:: on completion of the D-Bus method call to start discovery.
297 void OnStartDiscovery(const base::Closure
& callback
,
298 const DiscoverySessionErrorCallback
& error_callback
);
299 void OnStartDiscoveryError(
300 const base::Closure
& callback
,
301 const DiscoverySessionErrorCallback
& error_callback
,
302 const std::string
& error_name
,
303 const std::string
& error_message
);
305 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
306 void OnStopDiscovery(const base::Closure
& callback
);
307 void OnStopDiscoveryError(const DiscoverySessionErrorCallback
& error_callback
,
308 const std::string
& error_name
,
309 const std::string
& error_message
);
311 void OnPreSetDiscoveryFilter(
312 const base::Closure
& callback
,
313 const DiscoverySessionErrorCallback
& error_callback
);
314 void OnPreSetDiscoveryFilterError(
315 const base::Closure
& callback
,
316 const DiscoverySessionErrorCallback
& error_callback
,
317 device::UMABluetoothDiscoverySessionOutcome outcome
);
318 void OnSetDiscoveryFilter(
319 const base::Closure
& callback
,
320 const DiscoverySessionErrorCallback
& error_callback
);
321 void OnSetDiscoveryFilterError(
322 const base::Closure
& callback
,
323 const DiscoverySessionErrorCallback
& error_callback
,
324 const std::string
& error_name
,
325 const std::string
& error_message
);
327 // Called by dbus:: on completion of the D-Bus method to register a profile.
328 void OnRegisterProfile(const device::BluetoothUUID
& uuid
,
329 scoped_ptr
<BluetoothAdapterProfileChromeOS
> profile
);
331 void SetProfileDelegate(const device::BluetoothUUID
& uuid
,
332 const dbus::ObjectPath
& device_path
,
333 BluetoothProfileServiceProvider::Delegate
* delegate
,
334 const ProfileRegisteredCallback
& success_callback
,
335 const ErrorCompletionCallback
& error_callback
);
336 void OnRegisterProfileError(const device::BluetoothUUID
& uuid
,
337 const std::string
& error_name
,
338 const std::string
& error_message
);
340 // Called by BluetoothAdapterProfileChromeOS when no users of a profile
342 void RemoveProfile(const device::BluetoothUUID
& uuid
);
344 // Processes the queued discovery requests. For each DiscoveryParamTuple in
345 // the queue, this method will try to add a new discovery session. This method
346 // is called whenever a pending D-Bus call to start or stop discovery has
347 // ended (with either success or failure).
348 void ProcessQueuedDiscoveryRequests();
350 // Set in |Shutdown()|, makes IsPresent()| return false.
351 bool dbus_is_shutdown_
;
353 // Number of discovery sessions that have been added.
354 int num_discovery_sessions_
;
356 // True, if there is a pending request to start or stop discovery.
357 bool discovery_request_pending_
;
359 // List of queued requests to add new discovery sessions. While there is a
360 // pending request to BlueZ to start or stop discovery, many requests from
361 // within Chrome to start or stop discovery sessions may occur. We only
362 // queue requests to add new sessions to be processed later. All requests to
363 // remove a session while a call is pending immediately return failure. Note
364 // that since BlueZ keeps its own reference count of applications that have
365 // requested discovery, dropping our count to 0 won't necessarily result in
366 // the controller actually stopping discovery if, for example, an application
367 // other than Chrome, such as bt_console, was also used to start discovery.
368 DiscoveryCallbackQueue discovery_request_queue_
;
370 // Object path of the adapter we track.
371 dbus::ObjectPath object_path_
;
373 // Instance of the D-Bus agent object used for pairing, initialized with
374 // our own class as its delegate.
375 scoped_ptr
<BluetoothAgentServiceProvider
> agent_
;
377 // UI thread task runner and socket thread object used to create sockets.
378 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
379 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
381 // The profiles we have registered with the bluetooth daemon.
382 std::map
<device::BluetoothUUID
, BluetoothAdapterProfileChromeOS
*> profiles_
;
384 // Queue of delegates waiting for a profile to register.
385 std::map
<device::BluetoothUUID
, std::vector
<RegisterProfileCompletionPair
>*>
388 scoped_ptr
<device::BluetoothDiscoveryFilter
> current_filter_
;
390 // Note: This should remain the last member so it'll be destroyed and
391 // invalidate its weak pointers before any other members are destroyed.
392 base::WeakPtrFactory
<BluetoothAdapterChromeOS
> weak_ptr_factory_
;
394 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS
);
397 } // namespace chromeos
399 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_