Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: device/bluetooth/bluetooth_adapter_chromeos.h

Issue 1367663002: Add Linux support for the Bluetooth API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor_dbus
Patch Set: rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
7
8 #include <map>
9 #include <queue>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "base/memory/weak_ptr.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_audio_sink.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 #include "device/bluetooth/bluetooth_discovery_session.h"
20 #include "device/bluetooth/bluetooth_export.h"
21 #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
22 #include "device/bluetooth/dbus/bluetooth_agent_service_provider.h"
23 #include "device/bluetooth/dbus/bluetooth_device_client.h"
24 #include "device/bluetooth/dbus/bluetooth_input_client.h"
25 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h"
26 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h"
27
28 namespace base {
29 class SequencedTaskRunner;
30 } // namespace base
31
32 namespace device {
33 class BluetoothSocketThread;
34 } // namespace device
35
36 namespace chromeos {
37
38 class BluetoothChromeOSTest;
39 class BluetoothAdapterProfileChromeOS;
40 class BluetoothDeviceChromeOS;
41 class BluetoothPairingChromeOS;
42 class BluetoothRemoteGattCharacteristicChromeOS;
43 class BluetoothRemoteGattDescriptorChromeOS;
44 class BluetoothRemoteGattServiceChromeOS;
45
46 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
47 // Chrome OS platform.
48 //
49 // All methods are called from the dbus origin / UI thread and are generally
50 // not assumed to be thread-safe.
51 //
52 // This class interacts with sockets using the BluetoothSocketThread to ensure
53 // single-threaded calls, and posts tasks to the UI thread.
54 //
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.
58 //
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 bluez::BluetoothAdapterClient::Observer,
64 public bluez::BluetoothDeviceClient::Observer,
65 public bluez::BluetoothInputClient::Observer,
66 public bluez::BluetoothAgentServiceProvider::Delegate {
67 public:
68 typedef base::Callback<void(const std::string& error_message)>
69 ErrorCompletionCallback;
70 typedef base::Callback<void(BluetoothAdapterProfileChromeOS* profile)>
71 ProfileRegisteredCallback;
72
73 static base::WeakPtr<BluetoothAdapter> CreateAdapter();
74
75 // BluetoothAdapter:
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;
107
108 void RegisterAdvertisement(
109 scoped_ptr<device::BluetoothAdvertisement::Data> advertisement_data,
110 const CreateAdvertisementCallback& callback,
111 const CreateAdvertisementErrorCallback& error_callback) override;
112
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);
117
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);
121
122 // Announce to observers a device address change.
123 void NotifyDeviceAddressChanged(BluetoothDeviceChromeOS* device,
124 const std::string& old_address);
125
126 // The following methods are used to send various GATT observer events to
127 // observers.
128 void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS* service);
129 void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS* service);
130 void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS* service);
131 void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS* service);
132 void NotifyGattCharacteristicAdded(
133 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
134 void NotifyGattCharacteristicRemoved(
135 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
136 void NotifyGattDescriptorAdded(
137 BluetoothRemoteGattDescriptorChromeOS* descriptor);
138 void NotifyGattDescriptorRemoved(
139 BluetoothRemoteGattDescriptorChromeOS* descriptor);
140 void NotifyGattCharacteristicValueChanged(
141 BluetoothRemoteGattCharacteristicChromeOS* characteristic,
142 const std::vector<uint8>& value);
143 void NotifyGattDescriptorValueChanged(
144 BluetoothRemoteGattDescriptorChromeOS* descriptor,
145 const std::vector<uint8>& value);
146
147 // Returns the object path of the adapter.
148 const dbus::ObjectPath& object_path() const { return object_path_; }
149
150 // Request a profile on the adapter for a custom service with a
151 // specific UUID for the device at |device_path| to be sent to |delegate|.
152 // If |device_path| is the empty string, incoming connections will be
153 // assigned to |delegate|. When the profile is
154 // successfully registered, |success_callback| will be called with a pointer
155 // to the profile which is managed by BluetoothAdapterChromeOS. On failure,
156 // |error_callback| will be called.
157 void UseProfile(const device::BluetoothUUID& uuid,
158 const dbus::ObjectPath& device_path,
159 const bluez::BluetoothProfileManagerClient::Options& options,
160 bluez::BluetoothProfileServiceProvider::Delegate* delegate,
161 const ProfileRegisteredCallback& success_callback,
162 const ErrorCompletionCallback& error_callback);
163
164 // Release use of a profile by a device.
165 void ReleaseProfile(const dbus::ObjectPath& device_path,
166 BluetoothAdapterProfileChromeOS* profile);
167
168 protected:
169 // BluetoothAdapter:
170 void RemovePairingDelegateInternal(
171 device::BluetoothDevice::PairingDelegate* pairing_delegate) override;
172
173 private:
174 friend class BluetoothChromeOSTest;
175 friend class BluetoothChromeOSTest_Shutdown_Test;
176 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscovery_Test;
177 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscoveryError_Test;
178 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscovery_Test;
179 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscoveryError_Test;
180
181 // typedef for callback parameters that are passed to AddDiscoverySession
182 // and RemoveDiscoverySession. This is used to queue incoming requests while
183 // a call to BlueZ is pending.
184 typedef std::tuple<device::BluetoothDiscoveryFilter*,
185 base::Closure,
186 DiscoverySessionErrorCallback> DiscoveryParamTuple;
187 typedef std::queue<DiscoveryParamTuple> DiscoveryCallbackQueue;
188
189 // Callback pair for the profile registration queue.
190 typedef std::pair<base::Closure, ErrorCompletionCallback>
191 RegisterProfileCompletionPair;
192
193 BluetoothAdapterChromeOS();
194 ~BluetoothAdapterChromeOS() override;
195
196 // bluez::BluetoothAdapterClient::Observer override.
197 void AdapterAdded(const dbus::ObjectPath& object_path) override;
198 void AdapterRemoved(const dbus::ObjectPath& object_path) override;
199 void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
200 const std::string& property_name) override;
201
202 // bluez::BluetoothDeviceClient::Observer override.
203 void DeviceAdded(const dbus::ObjectPath& object_path) override;
204 void DeviceRemoved(const dbus::ObjectPath& object_path) override;
205 void DevicePropertyChanged(const dbus::ObjectPath& object_path,
206 const std::string& property_name) override;
207
208 // bluez::BluetoothInputClient::Observer override.
209 void InputPropertyChanged(const dbus::ObjectPath& object_path,
210 const std::string& property_name) override;
211
212 // bluez::BluetoothAgentServiceProvider::Delegate override.
213 void Released() override;
214 void RequestPinCode(const dbus::ObjectPath& device_path,
215 const PinCodeCallback& callback) override;
216 void DisplayPinCode(const dbus::ObjectPath& device_path,
217 const std::string& pincode) override;
218 void RequestPasskey(const dbus::ObjectPath& device_path,
219 const PasskeyCallback& callback) override;
220 void DisplayPasskey(const dbus::ObjectPath& device_path,
221 uint32 passkey,
222 uint16 entered) override;
223 void RequestConfirmation(const dbus::ObjectPath& device_path,
224 uint32 passkey,
225 const ConfirmationCallback& callback) override;
226 void RequestAuthorization(const dbus::ObjectPath& device_path,
227 const ConfirmationCallback& callback) override;
228 void AuthorizeService(const dbus::ObjectPath& device_path,
229 const std::string& uuid,
230 const ConfirmationCallback& callback) override;
231 void Cancel() override;
232
233 // Called by dbus:: on completion of the D-Bus method call to register the
234 // pairing agent.
235 void OnRegisterAgent();
236 void OnRegisterAgentError(const std::string& error_name,
237 const std::string& error_message);
238
239 // Called by dbus:: on completion of the D-Bus method call to request that
240 // the pairing agent be made the default.
241 void OnRequestDefaultAgent();
242 void OnRequestDefaultAgentError(const std::string& error_name,
243 const std::string& error_message);
244
245 // Called by BluetoothAudioSinkChromeOS on completion of registering an audio
246 // sink.
247 void OnRegisterAudioSink(
248 const device::BluetoothAdapter::AcquiredCallback& callback,
249 const device::BluetoothAudioSink::ErrorCallback& error_callback,
250 scoped_refptr<device::BluetoothAudioSink> audio_sink);
251
252 // Internal method to obtain a BluetoothPairingChromeOS object for the device
253 // with path |object_path|. Returns the existing pairing object if the device
254 // already has one (usually an outgoing connection in progress) or a new
255 // pairing object with the default pairing delegate if not. If no default
256 // pairing object exists, NULL will be returned.
257 BluetoothPairingChromeOS* GetPairing(const dbus::ObjectPath& object_path);
258
259 // Set the tracked adapter to the one in |object_path|, this object will
260 // subsequently operate on that adapter until it is removed.
261 void SetAdapter(const dbus::ObjectPath& object_path);
262
263 // Set the adapter name to one chosen from the system information.
264 void SetDefaultAdapterName();
265
266 // Remove the currently tracked adapter. IsPresent() will return false after
267 // this is called.
268 void RemoveAdapter();
269
270 // Announce to observers a change in the adapter state.
271 void PoweredChanged(bool powered);
272 void DiscoverableChanged(bool discoverable);
273 void DiscoveringChanged(bool discovering);
274 void PresentChanged(bool present);
275
276 // Called by dbus:: on completion of the discoverable property change.
277 void OnSetDiscoverable(const base::Closure& callback,
278 const ErrorCallback& error_callback,
279 bool success);
280
281 // Called by dbus:: on completion of an adapter property change.
282 void OnPropertyChangeCompleted(const base::Closure& callback,
283 const ErrorCallback& error_callback,
284 bool success);
285
286 // BluetoothAdapter:
287 void AddDiscoverySession(
288 device::BluetoothDiscoveryFilter* discovery_filter,
289 const base::Closure& callback,
290 const DiscoverySessionErrorCallback& error_callback) override;
291 void RemoveDiscoverySession(
292 device::BluetoothDiscoveryFilter* discovery_filter,
293 const base::Closure& callback,
294 const DiscoverySessionErrorCallback& error_callback) override;
295 void SetDiscoveryFilter(
296 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
297 const base::Closure& callback,
298 const DiscoverySessionErrorCallback& error_callback) override;
299
300 // Called by dbus:: on completion of the D-Bus method call to start discovery.
301 void OnStartDiscovery(const base::Closure& callback,
302 const DiscoverySessionErrorCallback& error_callback);
303 void OnStartDiscoveryError(
304 const base::Closure& callback,
305 const DiscoverySessionErrorCallback& error_callback,
306 const std::string& error_name,
307 const std::string& error_message);
308
309 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
310 void OnStopDiscovery(const base::Closure& callback);
311 void OnStopDiscoveryError(const DiscoverySessionErrorCallback& error_callback,
312 const std::string& error_name,
313 const std::string& error_message);
314
315 void OnPreSetDiscoveryFilter(
316 const base::Closure& callback,
317 const DiscoverySessionErrorCallback& error_callback);
318 void OnPreSetDiscoveryFilterError(
319 const base::Closure& callback,
320 const DiscoverySessionErrorCallback& error_callback,
321 device::UMABluetoothDiscoverySessionOutcome outcome);
322 void OnSetDiscoveryFilter(
323 const base::Closure& callback,
324 const DiscoverySessionErrorCallback& error_callback);
325 void OnSetDiscoveryFilterError(
326 const base::Closure& callback,
327 const DiscoverySessionErrorCallback& error_callback,
328 const std::string& error_name,
329 const std::string& error_message);
330
331 // Called by dbus:: on completion of the D-Bus method to register a profile.
332 void OnRegisterProfile(const device::BluetoothUUID& uuid,
333 scoped_ptr<BluetoothAdapterProfileChromeOS> profile);
334
335 void SetProfileDelegate(
336 const device::BluetoothUUID& uuid,
337 const dbus::ObjectPath& device_path,
338 bluez::BluetoothProfileServiceProvider::Delegate* delegate,
339 const ProfileRegisteredCallback& success_callback,
340 const ErrorCompletionCallback& error_callback);
341 void OnRegisterProfileError(const device::BluetoothUUID& uuid,
342 const std::string& error_name,
343 const std::string& error_message);
344
345 // Called by BluetoothAdapterProfileChromeOS when no users of a profile
346 // remain.
347 void RemoveProfile(const device::BluetoothUUID& uuid);
348
349 // Processes the queued discovery requests. For each DiscoveryParamTuple in
350 // the queue, this method will try to add a new discovery session. This method
351 // is called whenever a pending D-Bus call to start or stop discovery has
352 // ended (with either success or failure).
353 void ProcessQueuedDiscoveryRequests();
354
355 // Set in |Shutdown()|, makes IsPresent()| return false.
356 bool dbus_is_shutdown_;
357
358 // Number of discovery sessions that have been added.
359 int num_discovery_sessions_;
360
361 // True, if there is a pending request to start or stop discovery.
362 bool discovery_request_pending_;
363
364 // List of queued requests to add new discovery sessions. While there is a
365 // pending request to BlueZ to start or stop discovery, many requests from
366 // within Chrome to start or stop discovery sessions may occur. We only
367 // queue requests to add new sessions to be processed later. All requests to
368 // remove a session while a call is pending immediately return failure. Note
369 // that since BlueZ keeps its own reference count of applications that have
370 // requested discovery, dropping our count to 0 won't necessarily result in
371 // the controller actually stopping discovery if, for example, an application
372 // other than Chrome, such as bt_console, was also used to start discovery.
373 DiscoveryCallbackQueue discovery_request_queue_;
374
375 // Object path of the adapter we track.
376 dbus::ObjectPath object_path_;
377
378 // Instance of the D-Bus agent object used for pairing, initialized with
379 // our own class as its delegate.
380 scoped_ptr<bluez::BluetoothAgentServiceProvider> agent_;
381
382 // UI thread task runner and socket thread object used to create sockets.
383 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
384 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
385
386 // The profiles we have registered with the bluetooth daemon.
387 std::map<device::BluetoothUUID, BluetoothAdapterProfileChromeOS*> profiles_;
388
389 // Queue of delegates waiting for a profile to register.
390 std::map<device::BluetoothUUID, std::vector<RegisterProfileCompletionPair>*>
391 profile_queues_;
392
393 scoped_ptr<device::BluetoothDiscoveryFilter> current_filter_;
394
395 // Note: This should remain the last member so it'll be destroyed and
396 // invalidate its weak pointers before any other members are destroyed.
397 base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_;
398
399 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS);
400 };
401
402 } // namespace chromeos
403
404 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_bluez.cc ('k') | device/bluetooth/bluetooth_adapter_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698