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(const dbus::ObjectPath& object_path); |
| 236 |
| 237 // BluetoothAgentServiceProvider::Delegate override. |
| 238 // |
| 239 // This method will be called when the agent is unregistered from the |
| 240 // Bluetooth daemon, generally at the end of a pairing request. It may be |
| 241 // used to perform cleanup tasks. |
| 242 virtual void Release() OVERRIDE; |
| 243 |
| 244 // BluetoothAgentServiceProvider::Delegate override. |
| 245 // |
| 246 // This method will be called when the Bluetooth daemon requires a |
| 247 // PIN Code for authentication of the device with object path |device_path|, |
| 248 // the agent should obtain the code from the user and call |callback| |
| 249 // to provide it, or indicate rejection or cancellation of the request. |
| 250 // |
| 251 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices |
| 252 // for which there is no automatic pairing or special handling. |
| 253 virtual void RequestPinCode(const dbus::ObjectPath& device_path, |
| 254 const PinCodeCallback& callback) OVERRIDE; |
| 255 |
| 256 // BluetoothAgentServiceProvider::Delegate override. |
| 257 // |
| 258 // This method will be called when the Bluetooth daemon requires a |
| 259 // Passkey for authentication of the device with object path |device_path|, |
| 260 // the agent should obtain the passkey from the user (a numeric in the |
| 261 // range 0-999999) and call |callback| to provide it, or indicate |
| 262 // rejection or cancellation of the request. |
| 263 // |
| 264 // Passkeys are generally required for Bluetooth 2.1 and later devices |
| 265 // which cannot provide input or display on their own, and don't accept |
| 266 // passkey-less pairing. |
| 267 virtual void RequestPasskey(const dbus::ObjectPath& device_path, |
| 268 const PasskeyCallback& callback) OVERRIDE; |
| 269 |
| 270 // BluetoothAgentServiceProvider::Delegate override. |
| 271 // |
| 272 // This method will be called when the Bluetooth daemon requires that the |
| 273 // user enter the PIN code |pincode| into the device with object path |
| 274 // |device_path| so that it may be authenticated. The Cancel() method |
| 275 // will be called to dismiss the display once pairing is complete or |
| 276 // cancelled. |
| 277 // |
| 278 // This is used for Bluetooth 2.0 and earlier keyboard devices, the |
| 279 // |pincode| will always be a six-digit numeric in the range 000000-999999 |
| 280 // for compatibilty with later specifications. |
| 281 virtual void DisplayPinCode(const dbus::ObjectPath& device_path, |
| 282 const std::string& pincode) OVERRIDE; |
| 283 |
| 284 // BluetoothAgentServiceProvider::Delegate override. |
| 285 // |
| 286 // This method will be called when the Bluetooth daemon requires that the |
| 287 // user enter the Passkey |passkey| into the device with object path |
| 288 // |device_path| so that it may be authenticated. The Cancel() method |
| 289 // will be called to dismiss the display once pairing is complete or |
| 290 // cancelled. |
| 291 // |
| 292 // This is used for Bluetooth 2.1 and later devices that support input |
| 293 // but not display, such as keyboards. The Passkey is a numeric in the |
| 294 // range 0-999999 and should be always presented zero-padded to six |
| 295 // digits. |
| 296 virtual void DisplayPasskey(const dbus::ObjectPath& device_path, |
| 297 uint32 passkey) OVERRIDE; |
| 298 |
| 299 // BluetoothAgentServiceProvider::Delegate override. |
| 300 // |
| 301 // This method will be called when the Bluetooth daemon requires that the |
| 302 // user confirm that the Passkey |passkey| is displayed on the screen |
| 303 // of the device with object path |object_path| so that it may be |
| 304 // authentication. The agent should display to the user and ask for |
| 305 // confirmation, then call |callback| to provide their response (success, |
| 306 // rejected or cancelled). |
| 307 // |
| 308 // This is used for Bluetooth 2.1 and later devices that support display, |
| 309 // such as other computers or phones. The Passkey is a numeric in the |
| 310 // range 0-999999 and should be always present zero-padded to six |
| 311 // digits. |
| 312 virtual void RequestConfirmation( |
| 313 const dbus::ObjectPath& device_path, |
| 314 uint32 passkey, |
| 315 const ConfirmationCallback& callback) OVERRIDE; |
| 316 |
| 317 // BluetoothAgentServiceProvider::Delegate override. |
| 318 // |
| 319 // This method will be called when the Bluetooth daemon requires that the |
| 320 // user confirm that the device with object path |object_path| is |
| 321 // authorized to connect to the service with UUID |uuid|. The agent should |
| 322 // confirm with the user and call |callback| to provide their response |
| 323 // (success, rejected or cancelled). |
| 324 virtual void Authorize(const dbus::ObjectPath& device_path, |
| 325 const std::string& uuid, |
| 326 const ConfirmationCallback& callback) OVERRIDE; |
| 327 |
| 328 // BluetoothAgentServiceProvider::Delegate override. |
| 329 // |
| 330 // This method will be called when the Bluetooth daemon requires that the |
| 331 // user confirm that the device adapter may switch to mode |mode|. The |
| 332 // agent should confirm with the user and call |callback| to provide |
| 333 // their response (success, rejected or cancelled). |
| 334 virtual void ConfirmModeChange(Mode mode, |
| 335 const ConfirmationCallback& callback) OVERRIDE; |
| 336 |
| 337 // BluetoothAgentServiceProvider::Delegate override. |
| 338 // |
| 339 // This method will be called by the Bluetooth daemon to indicate that |
| 340 // the request failed before a reply was returned from the device. |
| 341 virtual void Cancel() OVERRIDE; |
| 342 |
108 // Creates a new BluetoothDevice object bound to the information of the | 343 // Creates a new BluetoothDevice object bound to the information of the |
109 // dbus object path |object_path|, representing a paired device or one | 344 // dbus object path |object_path| and the adapter |adapter|, representing |
110 // that is currently or previously connected, with initial properties set | 345 // a paired device or one that is currently or previously connected, with |
111 // from |properties|. | 346 // initial properties set from |properties|. |
112 static BluetoothDevice* CreateBound( | 347 static BluetoothDevice* CreateBound( |
| 348 BluetoothAdapter* adapter, |
113 const dbus::ObjectPath& object_path, | 349 const dbus::ObjectPath& object_path, |
114 const BluetoothDeviceClient::Properties* properties); | 350 const BluetoothDeviceClient::Properties* properties); |
115 | 351 |
116 // Creates a new BluetoothDevice object not bound to a dbus object path, | 352 // Creates a new BluetoothDevice object not bound to a dbus object path, |
117 // representing a discovered device that has not yet been connected to, | 353 // but bound to the adapter |adapter|, representing a discovered device that |
118 // with initial properties set from |properties|. | 354 // has not yet been connected to, with initial properties set |
| 355 // from |properties|. |
119 static BluetoothDevice* CreateUnbound( | 356 static BluetoothDevice* CreateUnbound( |
| 357 BluetoothAdapter* adapter, |
120 const BluetoothDeviceClient::Properties* properties); | 358 const BluetoothDeviceClient::Properties* properties); |
121 | 359 |
| 360 // Weak pointer factory for generating 'this' pointers that might live longer |
| 361 // than we do. |
| 362 base::WeakPtrFactory<BluetoothDevice> weak_ptr_factory_; |
| 363 |
| 364 // The adapter that owns this device instance. |
| 365 BluetoothAdapter* adapter_; |
| 366 |
122 // The dbus object path of the device, will be empty if the device has only | 367 // 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. | 368 // been discovered and not yet paired with or connected to. |
124 dbus::ObjectPath object_path_; | 369 dbus::ObjectPath object_path_; |
125 | 370 |
126 // The Bluetooth address of the device. | 371 // The Bluetooth address of the device. |
127 std::string address_; | 372 std::string address_; |
128 | 373 |
129 // The name of the device, as supplied by the remote device. | 374 // The name of the device, as supplied by the remote device. |
130 std::string name_; | 375 std::string name_; |
131 | 376 |
132 // The Bluetooth class of the device, a bitmask that may be decoded using | 377 // The Bluetooth class of the device, a bitmask that may be decoded using |
133 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm | 378 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm |
134 uint32 bluetooth_class_; | 379 uint32 bluetooth_class_; |
135 | 380 |
136 // Tracked device state, updated by the adapter managing the lifecyle of | 381 // Tracked device state, updated by the adapter managing the lifecyle of |
137 // the device. | 382 // the device. |
138 bool paired_; | 383 bool paired_; |
139 bool connected_; | 384 bool connected_; |
140 | 385 |
| 386 // During pairing this is set to an object that we don't own, but on which |
| 387 // we can make method calls to request, display or confirm PIN Codes and |
| 388 // Passkeys. Generally it is the object that owns this one. |
| 389 PairingDelegate* pairing_delegate_; |
| 390 |
| 391 // During pairing this is set to an instance of a D-Bus agent object |
| 392 // intialized with our own class as its delegate. |
| 393 scoped_ptr<BluetoothAgentServiceProvider> agent_; |
| 394 |
| 395 // During pairing these callbacks are set to those provided by method calls |
| 396 // made on us by |agent_| and are called by our own method calls such as |
| 397 // SetPinCode() and SetPasskey(). |
| 398 PinCodeCallback pincode_callback_; |
| 399 PasskeyCallback passkey_callback_; |
| 400 ConfirmationCallback confirmation_callback_; |
| 401 |
141 DISALLOW_COPY_AND_ASSIGN(BluetoothDevice); | 402 DISALLOW_COPY_AND_ASSIGN(BluetoothDevice); |
142 }; | 403 }; |
143 | 404 |
144 } // namespace chromeos | 405 } // namespace chromeos |
145 | 406 |
146 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 407 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
OLD | NEW |