OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_DEVICE_CHROMEOS_H_ | |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/string16.h" | |
16 #include "chromeos/dbus/bluetooth_agent_service_provider.h" | |
17 #include "chromeos/dbus/bluetooth_device_client.h" | |
18 #include "dbus/object_path.h" | |
19 #include "device/bluetooth/bluetooth_device.h" | |
20 | |
21 namespace device { | |
22 | |
23 class BluetoothServiceRecord; | |
24 class MockBluetoothDevice; | |
25 struct BluetoothOutOfBandPairingData; | |
26 | |
27 } // namespace device | |
28 | |
29 namespace chromeos { | |
30 | |
31 class BluetoothAdapterChromeOS; | |
32 | |
33 // The BluetoothDeviceChromeOS class is an implementation of BluetoothDevice | |
34 // for Chrome OS platform. | |
35 class BluetoothDeviceChromeOS | |
36 : public device::BluetoothDevice, | |
37 public BluetoothDeviceClient::Observer, | |
38 public BluetoothAgentServiceProvider::Delegate { | |
39 public: | |
40 virtual ~BluetoothDeviceChromeOS(); | |
41 | |
42 // BluetoothDevice override | |
43 virtual uint32 GetBluetoothClass() const OVERRIDE; | |
44 virtual std::string GetAddress() const OVERRIDE; | |
45 virtual uint16 GetVendorID() const OVERRIDE; | |
46 virtual uint16 GetProductID() const OVERRIDE; | |
47 virtual uint16 GetDeviceID() const OVERRIDE; | |
48 virtual bool IsPaired() const OVERRIDE; | |
49 virtual bool IsConnected() const OVERRIDE; | |
50 virtual bool IsConnectable() const OVERRIDE; | |
51 virtual bool IsConnecting() const OVERRIDE; | |
52 virtual ServiceList GetServices() const OVERRIDE; | |
53 virtual void GetServiceRecords( | |
54 const ServiceRecordsCallback& callback, | |
55 const ErrorCallback& error_callback) OVERRIDE; | |
56 virtual void ProvidesServiceWithName( | |
57 const std::string& name, | |
58 const ProvidesServiceCallback& callback) OVERRIDE; | |
59 virtual bool ExpectingPinCode() const OVERRIDE; | |
60 virtual bool ExpectingPasskey() const OVERRIDE; | |
61 virtual bool ExpectingConfirmation() const OVERRIDE; | |
62 virtual void Connect( | |
63 device::BluetoothDevice::PairingDelegate* pairing_delegate, | |
64 const base::Closure& callback, | |
65 const ConnectErrorCallback& error_callback) OVERRIDE; | |
66 virtual void SetPinCode(const std::string& pincode) OVERRIDE; | |
67 virtual void SetPasskey(uint32 passkey) OVERRIDE; | |
68 virtual void ConfirmPairing() OVERRIDE; | |
69 virtual void RejectPairing() OVERRIDE; | |
70 virtual void CancelPairing() OVERRIDE; | |
71 virtual void Disconnect( | |
72 const base::Closure& callback, | |
73 const ErrorCallback& error_callback) OVERRIDE; | |
74 virtual void Forget(const ErrorCallback& error_callback) OVERRIDE; | |
75 virtual void ConnectToService( | |
76 const std::string& service_uuid, | |
77 const SocketCallback& callback) OVERRIDE; | |
78 virtual void ConnectToProfile( | |
79 device::BluetoothProfile* profile, | |
80 const base::Closure& callback, | |
81 const ErrorCallback& error_callback) OVERRIDE; | |
82 virtual void SetOutOfBandPairingData( | |
83 const device::BluetoothOutOfBandPairingData& data, | |
84 const base::Closure& callback, | |
85 const ErrorCallback& error_callback) OVERRIDE; | |
86 virtual void ClearOutOfBandPairingData( | |
87 const base::Closure& callback, | |
88 const ErrorCallback& error_callback) OVERRIDE; | |
89 | |
90 protected: | |
91 // BluetoothDevice override | |
92 virtual std::string GetDeviceName() const OVERRIDE; | |
93 | |
94 private: | |
95 friend class BluetoothAdapterChromeOS; | |
96 friend class device::MockBluetoothDevice; | |
97 | |
98 explicit BluetoothDeviceChromeOS(BluetoothAdapterChromeOS* adapter); | |
99 | |
100 // Returns whether this device has an object path. | |
101 bool HasObjectPath() const { return !object_path_.value().empty(); } | |
102 | |
103 // Sets the dbus object path for the device to |object_path|, indicating | |
104 // that the device has gone from being discovered to paired or connected. | |
105 void SetObjectPath(const dbus::ObjectPath& object_path); | |
106 | |
107 // Removes the dbus object path from the device, indicating that the | |
108 // device is no longer paired or connected, but perhaps still visible. | |
109 void RemoveObjectPath(); | |
110 | |
111 // Returns whether this was a discovered device. | |
112 bool WasDiscovered() const { return discovered_; } | |
113 | |
114 // Sets whether the device was discovered. | |
115 void SetDiscovered(bool discovered) { discovered_ = discovered; } | |
116 | |
117 // Updates device information from the properties in |properties|, device | |
118 // state properties such as |paired_| and |connected_| are ignored unless | |
119 // |update_state| is true. | |
120 void Update(const BluetoothDeviceClient::Properties* properties, | |
121 bool update_state); | |
122 | |
123 // Called by BluetoothAdapterClient when a call to CreateDevice() or | |
124 // CreatePairedDevice() succeeds, provides the new object path for the remote | |
125 // device in |device_path|. |callback| and |error_callback| are the callbacks | |
126 // provided to Connect(). | |
127 void OnCreateDevice(const base::Closure& callback, | |
128 const ConnectErrorCallback& error_callback, | |
129 const dbus::ObjectPath& device_path); | |
130 | |
131 // Called by BluetoothAdapterClient when a call to CreateDevice() or | |
132 // CreatePairedDevice() fails with the error named |error_name| and | |
133 // optional message |error_message|, |error_callback| is the callback | |
134 // provided to Connect(). | |
135 void OnCreateDeviceError(const ConnectErrorCallback& error_callback, | |
136 const std::string& error_name, | |
137 const std::string& error_message); | |
138 | |
139 // Called by BluetoothAdapterClient when a call to GetServiceRecords() | |
140 // completes. |callback| and |error_callback| are the callbacks provided to | |
141 // GetServiceRecords. | |
142 void CollectServiceRecordsCallback( | |
143 const ServiceRecordsCallback& callback, | |
144 const ErrorCallback& error_callback, | |
145 const dbus::ObjectPath& device_path, | |
146 const BluetoothDeviceClient::ServiceMap& service_map, | |
147 bool success); | |
148 | |
149 // Set the device as trusted. Trusted devices can connect to us automatically, | |
150 // and we can connect to them after rebooting. This also causes the device to | |
151 // be remembered by the stack even if not paired. |success| to the callback | |
152 // indicates whether or not the request succeeded. | |
153 void SetTrusted(); | |
154 void OnSetTrusted(bool success); | |
155 | |
156 // Called by BluetoothAdapterClient when a call to GetServiceRecords() | |
157 // fails. |callback| and |error_callback| are the callbacks provided to | |
158 // GetServiceRecords(). | |
159 void OnGetServiceRecordsError(const ServiceRecordsCallback& callback, | |
160 const ErrorCallback& error_callback); | |
161 | |
162 // Called by BluetoothAdapterClient when the initial call to | |
163 // GetServiceRecords() after pairing completes. |callback| and | |
164 // |error_callback| are the callbacks provided to Connect(). | |
165 void OnInitialGetServiceRecords(const base::Closure& callback, | |
166 const ConnectErrorCallback& error_callback, | |
167 const ServiceRecordList& list); | |
168 | |
169 // Called by BluetoothAdapterClient when the initial call to | |
170 // GetServiceRecords() after pairing fails. |callback| and |error_callback| | |
171 // are the callbacks provided to Connect(). | |
172 void OnInitialGetServiceRecordsError( | |
173 const base::Closure& callback, | |
174 const ConnectErrorCallback& error_callback); | |
175 | |
176 // Called by Connect() when it succeeds. The |callback| is the value passed to | |
177 // the Connect() call. | |
178 void OnConnectCallbackCalled(const base::Closure& callback); | |
179 | |
180 // Called by Connect() when it fails. The |error_callback| is the value passed | |
181 // to the Connect() call. | |
182 void OnConnectErrorCallbackCalled(const ConnectErrorCallback& error_callback, | |
183 enum ConnectErrorCode error_code); | |
184 | |
185 // Connect application-level protocols of the device to the system, called | |
186 // on a successful connection or to reconnect to a device that is already | |
187 // paired or previously connected. |error_callback| is called on failure. | |
188 // Otherwise, |callback| is called when the request is complete. | |
189 void ConnectApplications(const base::Closure& callback, | |
190 const ConnectErrorCallback& error_callback); | |
191 | |
192 // Called by IntrospectableClient when a call to Introspect() completes. | |
193 // |success| indicates whether or not the request succeeded, |callback| and | |
194 // |error_callback| are the callbacks provided to ConnectApplications(), | |
195 // |service_name| and |device_path| specify the remote object being | |
196 // introspected and |xml_data| contains the XML-formatted protocol data. | |
197 void OnIntrospect(const base::Closure& callback, | |
198 const ConnectErrorCallback& error_callback, | |
199 const std::string& service_name, | |
200 const dbus::ObjectPath& device_path, | |
201 const std::string& xml_data, bool success); | |
202 | |
203 // Called by BluetoothInputClient when the call to Connect() succeeds. | |
204 // |error_callback| is the callback provided to ConnectApplications(), | |
205 // |interface_name| specifies the interface being connected and | |
206 // |device_path| the remote object path. | |
207 void OnConnect(const base::Closure& callback, | |
208 const std::string& interface_name, | |
209 const dbus::ObjectPath& device_path); | |
210 | |
211 // Called by BluetoothInputClient when the call to Connect() fails. | |
212 // |error_callback| is the callback provided to ConnectApplications(), | |
213 // |interface_name| specifies the interface being connected, | |
214 // |device_path| the remote object path, | |
215 // |error_name| the error name and |error_message| the optional message. | |
216 void OnConnectError(const ConnectErrorCallback& error_callback, | |
217 const std::string& interface_name, | |
218 const dbus::ObjectPath& device_path, | |
219 const std::string& error_name, | |
220 const std::string& error_message); | |
221 | |
222 // Called by BluetoothDeviceClient when a call to Disconnect() completes, | |
223 // |success| indicates whether or not the request succeeded, |callback| and | |
224 // |error_callback| are the callbacks provided to Disconnect() and | |
225 // |device_path| is the device disconnected. | |
226 void DisconnectCallback(const base::Closure& callback, | |
227 const ErrorCallback& error_callback, | |
228 const dbus::ObjectPath& device_path, bool success); | |
229 | |
230 // Called by BluetoothAdapterClient when a call to RemoveDevice() | |
231 // completes, |success| indicates whether or not the request succeeded, | |
232 // |error_callback| is the callback provided to Forget() and |adapter_path| is | |
233 // the d-bus object path of the adapter that performed the removal. | |
234 void ForgetCallback(const ErrorCallback& error_callback, | |
235 const dbus::ObjectPath& adapter_path, bool success); | |
236 | |
237 // Called by BluetoothAdapterClient when a call to CancelDeviceCreation() | |
238 // completes, |success| indicates whether or not the request succeeded. | |
239 void OnCancelDeviceCreation(const dbus::ObjectPath& adapter_path, | |
240 bool success); | |
241 | |
242 // Called if the call to GetServiceRecords from ProvidesServiceWithName fails. | |
243 void SearchServicesForNameErrorCallback( | |
244 const ProvidesServiceCallback& callback); | |
245 | |
246 // Called by GetServiceRecords with the list of BluetoothServiceRecords to | |
247 // search for |name|. |callback| is the callback from | |
248 // ProvidesServiceWithName. | |
249 void SearchServicesForNameCallback( | |
250 const std::string& name, | |
251 const ProvidesServiceCallback& callback, | |
252 const ServiceRecordList& list); | |
253 | |
254 // Called if the call to GetServiceRecords from Connect fails. | |
255 void GetServiceRecordsForConnectErrorCallback( | |
256 const SocketCallback& callback); | |
257 | |
258 // Called by GetServiceRecords with the list of BluetoothServiceRecords. | |
259 // Connections are attempted to each service in the list matching | |
260 // |service_uuid|, and the socket from the first successful connection is | |
261 // passed to |callback|. | |
262 void GetServiceRecordsForConnectCallback( | |
263 const std::string& service_uuid, | |
264 const SocketCallback& callback, | |
265 const ServiceRecordList& list); | |
266 | |
267 // Called by BlueoothDeviceClient in response to the AddRemoteData and | |
268 // RemoveRemoteData method calls. | |
269 void OnRemoteDataCallback(const base::Closure& callback, | |
270 const ErrorCallback& error_callback, | |
271 bool success); | |
272 | |
273 // BluetoothDeviceClient::Observer override. | |
274 // | |
275 // Called when the device with object path |object_path| is about | |
276 // to be disconnected, giving a chance for application layers to | |
277 // shut down cleanly. | |
278 virtual void DisconnectRequested( | |
279 const dbus::ObjectPath& object_path) OVERRIDE; | |
280 | |
281 // BluetoothAgentServiceProvider::Delegate override. | |
282 // | |
283 // This method will be called when the agent is unregistered from the | |
284 // Bluetooth daemon, generally at the end of a pairing request. It may be | |
285 // used to perform cleanup tasks. | |
286 virtual void Release() OVERRIDE; | |
287 | |
288 // BluetoothAgentServiceProvider::Delegate override. | |
289 // | |
290 // This method will be called when the Bluetooth daemon requires a | |
291 // PIN Code for authentication of the device with object path |device_path|, | |
292 // the agent should obtain the code from the user and call |callback| | |
293 // to provide it, or indicate rejection or cancellation of the request. | |
294 // | |
295 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices | |
296 // for which there is no automatic pairing or special handling. | |
297 virtual void RequestPinCode(const dbus::ObjectPath& device_path, | |
298 const PinCodeCallback& callback) OVERRIDE; | |
299 | |
300 // BluetoothAgentServiceProvider::Delegate override. | |
301 // | |
302 // This method will be called when the Bluetooth daemon requires a | |
303 // Passkey for authentication of the device with object path |device_path|, | |
304 // the agent should obtain the passkey from the user (a numeric in the | |
305 // range 0-999999) and call |callback| to provide it, or indicate | |
306 // rejection or cancellation of the request. | |
307 // | |
308 // Passkeys are generally required for Bluetooth 2.1 and later devices | |
309 // which cannot provide input or display on their own, and don't accept | |
310 // passkey-less pairing. | |
311 virtual void RequestPasskey(const dbus::ObjectPath& device_path, | |
312 const PasskeyCallback& callback) OVERRIDE; | |
313 | |
314 // BluetoothAgentServiceProvider::Delegate override. | |
315 // | |
316 // This method will be called when the Bluetooth daemon requires that the | |
317 // user enter the PIN code |pincode| into the device with object path | |
318 // |device_path| so that it may be authenticated. The Cancel() method | |
319 // will be called to dismiss the display once pairing is complete or | |
320 // cancelled. | |
321 // | |
322 // This is used for Bluetooth 2.0 and earlier keyboard devices, the | |
323 // |pincode| will always be a six-digit numeric in the range 000000-999999 | |
324 // for compatibilty with later specifications. | |
325 virtual void DisplayPinCode(const dbus::ObjectPath& device_path, | |
326 const std::string& pincode) OVERRIDE; | |
327 | |
328 // BluetoothAgentServiceProvider::Delegate override. | |
329 // | |
330 // This method will be called when the Bluetooth daemon requires that the | |
331 // user enter the Passkey |passkey| into the device with object path | |
332 // |device_path| so that it may be authenticated. The Cancel() method | |
333 // will be called to dismiss the display once pairing is complete or | |
334 // cancelled. | |
335 // | |
336 // This is used for Bluetooth 2.1 and later devices that support input | |
337 // but not display, such as keyboards. The Passkey is a numeric in the | |
338 // range 0-999999 and should be always presented zero-padded to six | |
339 // digits. | |
340 virtual void DisplayPasskey(const dbus::ObjectPath& device_path, | |
341 uint32 passkey) OVERRIDE; | |
342 | |
343 // BluetoothAgentServiceProvider::Delegate override. | |
344 // | |
345 // This method will be called when the Bluetooth daemon requires that the | |
346 // user confirm that the Passkey |passkey| is displayed on the screen | |
347 // of the device with object path |object_path| so that it may be | |
348 // authentication. The agent should display to the user and ask for | |
349 // confirmation, then call |callback| to provide their response (success, | |
350 // rejected or cancelled). | |
351 // | |
352 // This is used for Bluetooth 2.1 and later devices that support display, | |
353 // such as other computers or phones. The Passkey is a numeric in the | |
354 // range 0-999999 and should be always present zero-padded to six | |
355 // digits. | |
356 virtual void RequestConfirmation( | |
357 const dbus::ObjectPath& device_path, | |
358 uint32 passkey, | |
359 const ConfirmationCallback& callback) OVERRIDE; | |
360 | |
361 // BluetoothAgentServiceProvider::Delegate override. | |
362 // | |
363 // This method will be called when the Bluetooth daemon requires that the | |
364 // user confirm that the device with object path |object_path| is | |
365 // authorized to connect to the service with UUID |uuid|. The agent should | |
366 // confirm with the user and call |callback| to provide their response | |
367 // (success, rejected or cancelled). | |
368 virtual void Authorize(const dbus::ObjectPath& device_path, | |
369 const std::string& uuid, | |
370 const ConfirmationCallback& callback) OVERRIDE; | |
371 | |
372 // BluetoothAgentServiceProvider::Delegate override. | |
373 // | |
374 // This method will be called when the Bluetooth daemon requires that the | |
375 // user confirm that the device adapter may switch to mode |mode|. The | |
376 // agent should confirm with the user and call |callback| to provide | |
377 // their response (success, rejected or cancelled). | |
378 virtual void ConfirmModeChange(Mode mode, | |
379 const ConfirmationCallback& callback) OVERRIDE; | |
380 | |
381 // BluetoothAgentServiceProvider::Delegate override. | |
382 // | |
383 // This method will be called by the Bluetooth daemon to indicate that | |
384 // the request failed before a reply was returned from the device. | |
385 virtual void Cancel() OVERRIDE; | |
386 | |
387 // Creates a new BluetoothDeviceChromeOS object bound to the adapter | |
388 // |adapter|. | |
389 static BluetoothDeviceChromeOS* Create(BluetoothAdapterChromeOS* adapter); | |
390 | |
391 // The adapter that owns this device instance. | |
392 BluetoothAdapterChromeOS* adapter_; | |
393 | |
394 // The dbus object path of the device, will be empty if the device has only | |
395 // been discovered and not yet paired with. | |
396 dbus::ObjectPath object_path_; | |
397 | |
398 // The Bluetooth class of the device, a bitmask that may be decoded using | |
399 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm | |
400 uint32 bluetooth_class_; | |
401 | |
402 // The name of the device, as supplied by the remote device. | |
403 std::string name_; | |
404 | |
405 // The Bluetooth address of the device. | |
406 std::string address_; | |
407 | |
408 // Tracked device state, updated by the adapter managing the lifecyle of | |
409 // the device. | |
410 bool paired_; | |
411 bool trusted_; | |
412 bool connected_; | |
413 | |
414 // Indicates whether the device normally accepts connections initiated from | |
415 // the adapter once paired. | |
416 bool connectable_; | |
417 | |
418 // Indicated whether the device is in a connecting status. | |
419 bool connecting_; | |
420 | |
421 // Used by BluetoothAdapterChromeOS (a friend) to avoid removing discovered | |
422 // devices when they are unpaired. | |
423 bool discovered_; | |
424 | |
425 // The services (identified by UUIDs) that this device provides. | |
426 ServiceList service_uuids_; | |
427 | |
428 // During pairing this is set to an object that we don't own, but on which | |
429 // we can make method calls to request, display or confirm PIN Codes and | |
430 // Passkeys. Generally it is the object that owns this one. | |
431 device::BluetoothDevice::PairingDelegate* pairing_delegate_; | |
432 | |
433 // During pairing this is set to an instance of a D-Bus agent object | |
434 // intialized with our own class as its delegate. | |
435 scoped_ptr<BluetoothAgentServiceProvider> agent_; | |
436 | |
437 // During pairing these callbacks are set to those provided by method calls | |
438 // made on us by |agent_| and are called by our own method calls such as | |
439 // SetPinCode() and SetPasskey(). | |
440 PinCodeCallback pincode_callback_; | |
441 PasskeyCallback passkey_callback_; | |
442 ConfirmationCallback confirmation_callback_; | |
443 | |
444 // Used to keep track of pending application connection requests. | |
445 int connecting_applications_counter_; | |
446 | |
447 // Used to keep track of ongoing calls to Connect(). | |
448 int connecting_calls_; | |
449 | |
450 // A service records cache. | |
451 ServiceRecordList service_records_; | |
452 | |
453 // This says whether the |service_records_| cache is initialized. Note that an | |
454 // empty |service_records_| list can be a valid list. | |
455 bool service_records_loaded_; | |
456 | |
457 // Note: This should remain the last member so it'll be destroyed and | |
458 // invalidate its weak pointers before any other members are destroyed. | |
459 base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_; | |
460 | |
461 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS); | |
462 }; | |
463 | |
464 } // namespace chromeos | |
465 | |
466 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H_ | |
OLD | NEW |