OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/callback.h" | |
11 #include "base/memory/scoped_ptr.h" | |
10 #include "base/string16.h" | 12 #include "base/string16.h" |
13 #include "chrome/browser/chromeos/dbus/bluetooth_agent_service_provider.h" | |
11 #include "chrome/browser/chromeos/dbus/bluetooth_device_client.h" | 14 #include "chrome/browser/chromeos/dbus/bluetooth_device_client.h" |
12 #include "dbus/object_path.h" | 15 #include "dbus/object_path.h" |
13 | 16 |
14 namespace chromeos { | 17 namespace chromeos { |
15 | 18 |
16 class BluetoothAdapter; | 19 class BluetoothAdapter; |
17 | 20 |
18 // The BluetoothDevice class represents a remote Bluetooth device, both | 21 // The BluetoothDevice class represents a remote Bluetooth device, both |
19 // its properties and capabilities as discovered by a local adapter and | 22 // its properties and capabilities as discovered by a local adapter and |
20 // actions that may be performed on the remove device such as pairing, | 23 // actions that may be performed on the remove device such as pairing, |
21 // connection and disconnection. | 24 // connection and disconnection. |
22 // | 25 // |
23 // The class is instantiated and managed by the BluetoothAdapter class | 26 // The class is instantiated and managed by the BluetoothAdapter class |
24 // and pointers should only be obtained from that class and not cached, | 27 // and pointers should only be obtained from that class and not cached, |
25 // instead use the address() method as a unique key for a device. | 28 // instead use the address() method as a unique key for a device. |
26 // | 29 // |
27 // Since the lifecycle of BluetoothDevice instances is managed by | 30 // Since the lifecycle of BluetoothDevice instances is managed by |
28 // BluetoothAdapter, that class rather than this provides observer methods | 31 // BluetoothAdapter, that class rather than this provides observer methods |
29 // for devices coming and going, as well as properties being updated. | 32 // for devices coming and going, as well as properties being updated. |
30 class BluetoothDevice : public BluetoothDeviceClient::Observer { | 33 class BluetoothDevice : private BluetoothDeviceClient::Observer, |
34 private BluetoothAgentServiceProvider::Delegate { | |
31 public: | 35 public: |
32 // Possible values that may be returned by GetDeviceType(), representing | 36 // Possible values that may be returned by GetDeviceType(), representing |
33 // different types of bluetooth device that we support or are aware of | 37 // different types of bluetooth device that we support or are aware of |
34 // decoded from the bluetooth class information. | 38 // decoded from the bluetooth class information. |
35 enum DeviceType { | 39 enum DeviceType { |
36 DEVICE_UNKNOWN, | 40 DEVICE_UNKNOWN, |
37 DEVICE_COMPUTER, | 41 DEVICE_COMPUTER, |
38 DEVICE_PHONE, | 42 DEVICE_PHONE, |
39 DEVICE_MODEM, | 43 DEVICE_MODEM, |
40 DEVICE_PERIPHERAL, | 44 DEVICE_PERIPHERAL, |
41 DEVICE_KEYBOARD, | 45 DEVICE_KEYBOARD, |
42 DEVICE_MOUSE, | 46 DEVICE_MOUSE, |
43 DEVICE_TABLET, | 47 DEVICE_TABLET, |
44 DEVICE_KEYBOARD_MOUSE_COMBO | 48 DEVICE_KEYBOARD_MOUSE_COMBO |
45 }; | 49 }; |
46 | 50 |
47 // Interface for observing changes from bluetooth devices. | 51 // Interface for observing changes from bluetooth devices. |
48 class Observer { | 52 class Observer { |
49 public: | 53 public: |
50 virtual ~Observer() {} | 54 virtual ~Observer() {} |
51 | 55 |
52 // TODO(keybuk): add observers for pairing and connection. | 56 // TODO(keybuk): add observers for pairing and connection. |
53 }; | 57 }; |
54 | 58 |
59 // Interface for negotiating pairing of bluetooth devices. | |
60 class PairingDelegate { | |
61 public: | |
62 virtual ~PairingDelegate() {} | |
63 | |
64 // This method will be called when the Bluetooth daemon requires a | |
65 // PIN Code for authentication of the device |device|, the delegate should | |
66 // obtain the code from the user and call SetPinCode() on the device to | |
67 // provide it, or RejectPairing() or CancelPairing() to reject or cancel | |
68 // the request. | |
69 // | |
70 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices | |
71 // for which there is no automatic pairing or special handling. | |
72 virtual void RequestPinCode(BluetoothDevice* device) = 0; | |
73 | |
74 // This method will be called when the Bluetooth daemon requires a | |
75 // Passkey for authentication of the device |device|, the delegate should | |
76 // obtain the passkey from the user (a numeric in the range 0-999999) and | |
77 // call SetPasskey() on the device to provide it, or RejectPairing() or | |
78 // CancelPairing() to reject or cancel the request. | |
79 // | |
80 // Passkeys are generally required for Bluetooth 2.1 and later devices | |
81 // which cannot provide input or display on their own, and don't accept | |
82 // passkey-less pairing. | |
83 virtual void RequestPasskey(BluetoothDevice* device) = 0; | |
84 | |
85 // This method will be called when the Bluetooth daemon requires that the | |
86 // user enter the PIN code |pincode| into the device |device| so that it | |
87 // may be authenticated. The DismissDisplayOrConfirm() method | |
88 // will be called to dismiss the display once pairing is complete or | |
89 // cancelled. | |
90 // | |
91 // This is used for Bluetooth 2.0 and earlier keyboard devices, the | |
92 // |pincode| will always be a six-digit numeric in the range 000000-999999 | |
93 // for compatibilty with later specifications. | |
94 virtual void DisplayPinCode(BluetoothDevice* device, | |
95 const std::string& pincode) = 0; | |
96 | |
97 // This method will be called when the Bluetooth daemon requires that the | |
98 // user enter the Passkey |passkey| into the device |device| so that it | |
99 // may be authenticated. The DismissDisplayOrConfirm() method will be | |
100 // called to dismiss the display once pairing is complete or cancelled. | |
101 // | |
102 // This is used for Bluetooth 2.1 and later devices that support input | |
103 // but not display, such as keyboards. The Passkey is a numeric in the | |
104 // range 0-999999 and should be always presented zero-padded to six | |
105 // digits. | |
106 virtual void DisplayPasskey(BluetoothDevice* device, | |
107 uint32 passkey) = 0; | |
108 | |
109 // This method will be called when the Bluetooth daemon requires that the | |
110 // user confirm that the Passkey |passkey| is displayed on the screen | |
111 // of the device |device| so that it may be authenticated. The delegate | |
112 // should display to the user and ask for confirmation, then call | |
113 // ConfirmPairing() on the device to confirm, RejectPairing() on the device | |
114 // to reject or CancelPairing() on the device to cancel authentication | |
115 // for any other reason. | |
116 // | |
117 // This is used for Bluetooth 2.1 and later devices that support display, | |
118 // such as other computers or phones. The Passkey is a numeric in the | |
119 // range 0-999999 and should be always present zero-padded to six | |
120 // digits. | |
121 virtual void ConfirmPasskey(BluetoothDevice* device, | |
122 uint32 passkey) = 0; | |
123 | |
124 // This method will be called when any previous DisplayPinCode(), | |
125 // DisplayPasskey() or ConfirmPasskey() request should be concluded | |
126 // and removed from the user. | |
127 virtual void DismissDisplayOrConfirm() = 0; | |
128 }; | |
129 | |
55 virtual ~BluetoothDevice(); | 130 virtual ~BluetoothDevice(); |
56 | 131 |
57 // Returns the Bluetooth of address the device. This should be used as | 132 // Returns the Bluetooth of address the device. This should be used as |
58 // a unique key to identify the device and copied where needed. | 133 // a unique key to identify the device and copied where needed. |
59 const std::string& address() const { return address_; } | 134 const std::string& address() const { return address_; } |
60 | 135 |
61 // Returns the name of the device suitable for displaying, this may | 136 // Returns the name of the device suitable for displaying, this may |
62 // be a synthesied string containing the address and localized type name | 137 // be a synthesied string containing the address and localized type name |
63 // if the device has no obtained name. | 138 // if the device has no obtained name. |
64 string16 GetName() const; | 139 string16 GetName() const; |
(...skipping 18 matching lines...) Expand all Loading... | |
83 // Indicates whether the device is paired to the adapter, note that some | 158 // Indicates whether the device is paired to the adapter, note that some |
84 // devices can be connected to and used without pairing so use | 159 // devices can be connected to and used without pairing so use |
85 // WasDiscovered() rather than this method to determine whether or not | 160 // WasDiscovered() rather than this method to determine whether or not |
86 // to display in a list of devices. | 161 // to display in a list of devices. |
87 bool IsPaired() const { return paired_; } | 162 bool IsPaired() const { return paired_; } |
88 | 163 |
89 // Indicates whether the device is currently connected to the adapter | 164 // Indicates whether the device is currently connected to the adapter |
90 // and at least one service available for use. | 165 // and at least one service available for use. |
91 bool IsConnected() const; | 166 bool IsConnected() const; |
92 | 167 |
168 // The ErrorCallback is used for methods that can fail in which case it | |
169 // is called, in the success case the callback is simply not called. | |
170 typedef base::Callback<void()> ErrorCallback; | |
171 | |
172 // Initiates a low-security connection to the device, without requiring | |
173 // pairing. If the request fails, |error_callback| will be called. | |
174 void Connect(ErrorCallback error_callback); | |
175 | |
176 // Initiates a high-security connection to the device pairing first if | |
177 // necessary. Method calls will be made on the supplied object | |
178 // |pairing_delegate| to indicate what display, and in response should | |
179 // make method calls back to the device object. If the request fails, | |
180 // |error_callback| will be called. | |
181 void PairAndConnect(PairingDelegate* pairing_delegate, | |
182 ErrorCallback error_callback); | |
183 | |
184 // Sends the PIN code |pincode| to the remote device during pairing. | |
185 // | |
186 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices | |
187 // for which there is no automatic pairing or special handling. | |
188 void SetPinCode(const std::string& pincode); | |
189 | |
190 // Sends the Passkey |passkey| to the remote device during pairing. | |
191 // | |
192 // Passkeys are generally required for Bluetooth 2.1 and later devices | |
193 // which cannot provide input or display on their own, and don't accept | |
194 // passkey-less pairing, and are a numeric in the range 0-999999. | |
195 void SetPasskey(uint32 passkey); | |
196 | |
197 // Confirms to the remote device during pairing that a passkey provided by | |
198 // the ConfirmPasskey() delegate call is displayed on both devices. | |
199 void ConfirmPairing(); | |
200 | |
201 // Rejects a pairing or connection request from a remote device. | |
202 void RejectPairing(); | |
203 | |
204 // Cancels a pairing or connection attempt to a rmeote device. | |
205 void CancelPairing(); | |
206 | |
93 private: | 207 private: |
94 friend class BluetoothAdapter; | 208 friend class BluetoothAdapter; |
95 | 209 |
96 BluetoothDevice(); | 210 explicit BluetoothDevice(BluetoothAdapter* adapter); |
97 | 211 |
98 // Sets the dbus object path for the device to |object_path|, indicating | 212 // Sets the dbus object path for the device to |object_path|, indicating |
99 // that the device has gone from being discovered to paired or connected. | 213 // that the device has gone from being discovered to paired or connected. |
100 void SetObjectPath(const dbus::ObjectPath& object_path); | 214 void SetObjectPath(const dbus::ObjectPath& object_path); |
101 | 215 |
102 // Updates device information from the properties in |properties|, device | 216 // Updates device information from the properties in |properties|, device |
103 // state properties such as |paired_| and |connected_| are ignored unless | 217 // state properties such as |paired_| and |connected_| are ignored unless |
104 // |update_state| is true. | 218 // |update_state| is true. |
105 void Update(const BluetoothDeviceClient::Properties* properties, | 219 void Update(const BluetoothDeviceClient::Properties* properties, |
106 bool update_state); | 220 bool update_state); |
107 | 221 |
222 // Called by BluetoothAdapterClient when a call to CreateDevice() or | |
223 // CreatePairedDevice() to provide the new object path for the remote | |
224 // device in |device_path| and |success| which indicates whether or not | |
225 // the request succeeded. |error_callback| is the callback provided to | |
226 // our own Connect() and PairAndConnect() calls. | |
227 void ConnectCallback(ErrorCallback error_callback, | |
228 const dbus::ObjectPath& device_path, bool success); | |
229 | |
230 // BluetoothDeviceClient::Observer override. | |
231 // | |
232 // Called when the device with object path |object_path| is about | |
233 // to be disconnected, giving a chance for application layers to | |
234 // shut down cleanly. | |
235 virtual void DisconnectRequested( | |
236 const dbus::ObjectPath& object_path) OVERRIDE; | |
237 | |
238 // BluetoothAgentServiceProvider::Delegate override. | |
239 // | |
240 // This method will be called when the agent is unregistered from the | |
241 // Bluetooth daemon, generally at the end of a pairing request. It may be | |
242 // used to perform cleanup tasks. | |
satorux1
2012/03/14 16:55:57
nit: don't repeat the same comment. we have this c
keybuk
2012/03/14 18:07:55
oh, I've generally consistently copied the delegat
| |
243 virtual void Release() OVERRIDE; | |
244 | |
245 // BluetoothAgentServiceProvider::Delegate override. | |
246 // | |
247 // This method will be called when the Bluetooth daemon requires a | |
248 // PIN Code for authentication of the device with object path |device_path|, | |
249 // the agent should obtain the code from the user and call |callback| | |
250 // to provide it, or indicate rejection or cancellation of the request. | |
251 // | |
252 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices | |
253 // for which there is no automatic pairing or special handling. | |
254 virtual void RequestPinCode(const dbus::ObjectPath& device_path, | |
255 const PinCodeCallback& callback) OVERRIDE; | |
256 | |
257 // BluetoothAgentServiceProvider::Delegate override. | |
258 // | |
259 // This method will be called when the Bluetooth daemon requires a | |
260 // Passkey for authentication of the device with object path |device_path|, | |
261 // the agent should obtain the passkey from the user (a numeric in the | |
262 // range 0-999999) and call |callback| to provide it, or indicate | |
263 // rejection or cancellation of the request. | |
264 // | |
265 // Passkeys are generally required for Bluetooth 2.1 and later devices | |
266 // which cannot provide input or display on their own, and don't accept | |
267 // passkey-less pairing. | |
268 virtual void RequestPasskey(const dbus::ObjectPath& device_path, | |
269 const PasskeyCallback& callback) OVERRIDE; | |
270 | |
271 // BluetoothAgentServiceProvider::Delegate override. | |
272 // | |
273 // This method will be called when the Bluetooth daemon requires that the | |
274 // user enter the PIN code |pincode| into the device with object path | |
275 // |device_path| so that it may be authenticated. The Cancel() method | |
276 // will be called to dismiss the display once pairing is complete or | |
277 // cancelled. | |
278 // | |
279 // This is used for Bluetooth 2.0 and earlier keyboard devices, the | |
280 // |pincode| will always be a six-digit numeric in the range 000000-999999 | |
281 // for compatibilty with later specifications. | |
282 virtual void DisplayPinCode(const dbus::ObjectPath& device_path, | |
283 const std::string& pincode) OVERRIDE; | |
284 | |
285 // BluetoothAgentServiceProvider::Delegate override. | |
286 // | |
287 // This method will be called when the Bluetooth daemon requires that the | |
288 // user enter the Passkey |passkey| into the device with object path | |
289 // |device_path| so that it may be authenticated. The Cancel() method | |
290 // will be called to dismiss the display once pairing is complete or | |
291 // cancelled. | |
292 // | |
293 // This is used for Bluetooth 2.1 and later devices that support input | |
294 // but not display, such as keyboards. The Passkey is a numeric in the | |
295 // range 0-999999 and should be always presented zero-padded to six | |
296 // digits. | |
297 virtual void DisplayPasskey(const dbus::ObjectPath& device_path, | |
298 uint32 passkey) OVERRIDE; | |
299 | |
300 // BluetoothAgentServiceProvider::Delegate override. | |
301 // | |
302 // This method will be called when the Bluetooth daemon requires that the | |
303 // user confirm that the Passkey |passkey| is displayed on the screen | |
304 // of the device with object path |object_path| so that it may be | |
305 // authentication. The agent should display to the user and ask for | |
306 // confirmation, then call |callback| to provide their response (success, | |
307 // rejected or cancelled). | |
308 // | |
309 // This is used for Bluetooth 2.1 and later devices that support display, | |
310 // such as other computers or phones. The Passkey is a numeric in the | |
311 // range 0-999999 and should be always present zero-padded to six | |
312 // digits. | |
313 virtual void RequestConfirmation( | |
314 const dbus::ObjectPath& device_path, | |
315 uint32 passkey, | |
316 const ConfirmationCallback& callback) OVERRIDE; | |
317 | |
318 // BluetoothAgentServiceProvider::Delegate override. | |
319 // | |
320 // This method will be called when the Bluetooth daemon requires that the | |
321 // user confirm that the device with object path |object_path| is | |
322 // authorized to connect to the service with UUID |uuid|. The agent should | |
323 // confirm with the user and call |callback| to provide their response | |
324 // (success, rejected or cancelled). | |
325 virtual void Authorize(const dbus::ObjectPath& device_path, | |
326 const std::string& uuid, | |
327 const ConfirmationCallback& callback) OVERRIDE; | |
328 | |
329 // BluetoothAgentServiceProvider::Delegate override. | |
330 // | |
331 // This method will be called when the Bluetooth daemon requires that the | |
332 // user confirm that the device adapter may switch to mode |mode|. The | |
333 // agent should confirm with the user and call |callback| to provide | |
334 // their response (success, rejected or cancelled). | |
335 virtual void ConfirmModeChange(Mode mode, | |
336 const ConfirmationCallback& callback) OVERRIDE; | |
337 | |
338 // BluetoothAgentServiceProvider::Delegate override. | |
339 // | |
340 // This method will be called by the Bluetooth daemon to indicate that | |
341 // the request failed before a reply was returned from the device. | |
342 virtual void Cancel() OVERRIDE; | |
343 | |
108 // Creates a new BluetoothDevice object bound to the information of the | 344 // Creates a new BluetoothDevice object bound to the information of the |
109 // dbus object path |object_path|, representing a paired device or one | 345 // dbus object path |object_path| and the adapter |adapter|, representing |
110 // that is currently or previously connected, with initial properties set | 346 // a paired device or one that is currently or previously connected, with |
111 // from |properties|. | 347 // initial properties set from |properties|. |
112 static BluetoothDevice* CreateBound( | 348 static BluetoothDevice* CreateBound( |
349 BluetoothAdapter* adapter, | |
113 const dbus::ObjectPath& object_path, | 350 const dbus::ObjectPath& object_path, |
114 const BluetoothDeviceClient::Properties* properties); | 351 const BluetoothDeviceClient::Properties* properties); |
115 | 352 |
116 // Creates a new BluetoothDevice object not bound to a dbus object path, | 353 // Creates a new BluetoothDevice object not bound to a dbus object path, |
117 // representing a discovered device that has not yet been connected to, | 354 // but bound to the adapter |adapter|, representing a discovered device that |
118 // with initial properties set from |properties|. | 355 // has not yet been connected to, with initial properties set |
356 // from |properties|. | |
119 static BluetoothDevice* CreateUnbound( | 357 static BluetoothDevice* CreateUnbound( |
358 BluetoothAdapter* adapter, | |
120 const BluetoothDeviceClient::Properties* properties); | 359 const BluetoothDeviceClient::Properties* properties); |
121 | 360 |
361 // Weak pointer factory for generating 'this' pointers that might live longer | |
362 // than we do. | |
363 base::WeakPtrFactory<BluetoothDevice> weak_ptr_factory_; | |
364 | |
365 // The adapter that owns this device instance. | |
366 BluetoothAdapter* adapter_; | |
367 | |
122 // The dbus object path of the device, will be empty if the device has only | 368 // The dbus object path of the device, will be empty if the device has only |
123 // been discovered and not yet paired with or connected to. | 369 // been discovered and not yet paired with or connected to. |
124 dbus::ObjectPath object_path_; | 370 dbus::ObjectPath object_path_; |
125 | 371 |
126 // The Bluetooth address of the device. | 372 // The Bluetooth address of the device. |
127 std::string address_; | 373 std::string address_; |
128 | 374 |
129 // The name of the device, as supplied by the remote device. | 375 // The name of the device, as supplied by the remote device. |
130 std::string name_; | 376 std::string name_; |
131 | 377 |
132 // The Bluetooth class of the device, a bitmask that may be decoded using | 378 // The Bluetooth class of the device, a bitmask that may be decoded using |
133 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm | 379 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm |
134 uint32 bluetooth_class_; | 380 uint32 bluetooth_class_; |
135 | 381 |
136 // Tracked device state, updated by the adapter managing the lifecyle of | 382 // Tracked device state, updated by the adapter managing the lifecyle of |
137 // the device. | 383 // the device. |
138 bool paired_; | 384 bool paired_; |
139 bool connected_; | 385 bool connected_; |
140 | 386 |
387 // During pairing this is set to an object that we don't own, but on which | |
388 // we can make method calls to request, display or confirm PIN Codes and | |
389 // Passkeys. Generally it is the object that owns this one. | |
390 PairingDelegate* pairing_delegate_; | |
391 | |
392 // During pairing this is set to an instance of a D-Bus agent object | |
393 // intialized with our own class as its delegate. | |
394 scoped_ptr<BluetoothAgentServiceProvider> agent_; | |
395 | |
396 // During pairing these callbacks are set to those provided by method calls | |
397 // made on us by |agent_| and are called by our own method calls such as | |
398 // SetPinCode() and SetPasskey(). | |
399 PinCodeCallback pincode_callback_; | |
400 PasskeyCallback passkey_callback_; | |
401 ConfirmationCallback confirmation_callback_; | |
402 | |
141 DISALLOW_COPY_AND_ASSIGN(BluetoothDevice); | 403 DISALLOW_COPY_AND_ASSIGN(BluetoothDevice); |
142 }; | 404 }; |
143 | 405 |
144 } // namespace chromeos | 406 } // namespace chromeos |
145 | 407 |
146 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 408 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
OLD | NEW |