blob: fce46b1f9307bf67e6b1f2579b0a5d04fea5e2aa [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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_H_
6#define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
7
8#include <string>
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009
10#include "base/callback.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "base/memory/ref_counted.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "base/memory/scoped_vector.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010013#include "base/strings/string16.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014
15namespace device {
16
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017class BluetoothProfile;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018class BluetoothSocket;
19
20struct BluetoothOutOfBandPairingData;
21
22// BluetoothDevice represents a remote Bluetooth device, both its properties and
23// capabilities as discovered by a local adapter and actions that may be
24// performed on the remove device such as pairing, connection and disconnection.
25//
26// The class is instantiated and managed by the BluetoothAdapter class
27// and pointers should only be obtained from that class and not cached,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010028// instead use the GetAddress() method as a unique key for a device.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029//
30// Since the lifecycle of BluetoothDevice instances is managed by
31// BluetoothAdapter, that class rather than this provides observer methods
32// for devices coming and going, as well as properties being updated.
33class BluetoothDevice {
34 public:
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000035 // Possible values that may be returned by GetVendorIDSource(),
36 // indicating different organisations that allocate the identifiers returned
37 // by GetVendorID().
38 enum VendorIDSource {
39 VENDOR_ID_UNKNOWN,
40 VENDOR_ID_BLUETOOTH,
41 VENDOR_ID_USB
42 };
43
Torne (Richard Coles)58218062012-11-14 11:43:16 +000044 // Possible values that may be returned by GetDeviceType(), representing
45 // different types of bluetooth device that we support or are aware of
46 // decoded from the bluetooth class information.
47 enum DeviceType {
48 DEVICE_UNKNOWN,
49 DEVICE_COMPUTER,
50 DEVICE_PHONE,
51 DEVICE_MODEM,
52 DEVICE_AUDIO,
53 DEVICE_CAR_AUDIO,
54 DEVICE_VIDEO,
55 DEVICE_PERIPHERAL,
56 DEVICE_JOYSTICK,
57 DEVICE_GAMEPAD,
58 DEVICE_KEYBOARD,
59 DEVICE_MOUSE,
60 DEVICE_TABLET,
61 DEVICE_KEYBOARD_MOUSE_COMBO
62 };
63
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064 // Possible errors passed back to an error callback function in case of a
65 // failed call to Connect().
66 enum ConnectErrorCode {
67 ERROR_UNKNOWN,
68 ERROR_INPROGRESS,
69 ERROR_FAILED,
70 ERROR_AUTH_FAILED,
71 ERROR_AUTH_CANCELED,
72 ERROR_AUTH_REJECTED,
73 ERROR_AUTH_TIMEOUT,
74 ERROR_UNSUPPORTED_DEVICE
75 };
76
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077 // Interface for observing changes from bluetooth devices.
78 class Observer {
79 public:
80 virtual ~Observer() {}
81
82 // TODO(keybuk): add observers for pairing and connection.
83 };
84
85 // Interface for negotiating pairing of bluetooth devices.
86 class PairingDelegate {
87 public:
88 virtual ~PairingDelegate() {}
89
90 // This method will be called when the Bluetooth daemon requires a
91 // PIN Code for authentication of the device |device|, the delegate should
92 // obtain the code from the user and call SetPinCode() on the device to
93 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
94 // the request.
95 //
96 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
97 // for which there is no automatic pairing or special handling.
98 virtual void RequestPinCode(BluetoothDevice* device) = 0;
99
100 // This method will be called when the Bluetooth daemon requires a
101 // Passkey for authentication of the device |device|, the delegate should
102 // obtain the passkey from the user (a numeric in the range 0-999999) and
103 // call SetPasskey() on the device to provide it, or RejectPairing() or
104 // CancelPairing() to reject or cancel the request.
105 //
106 // Passkeys are generally required for Bluetooth 2.1 and later devices
107 // which cannot provide input or display on their own, and don't accept
108 // passkey-less pairing.
109 virtual void RequestPasskey(BluetoothDevice* device) = 0;
110
111 // This method will be called when the Bluetooth daemon requires that the
112 // user enter the PIN code |pincode| into the device |device| so that it
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000113 // may be authenticated.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000114 //
115 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
116 // |pincode| will always be a six-digit numeric in the range 000000-999999
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100117 // for compatibility with later specifications.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000118 virtual void DisplayPinCode(BluetoothDevice* device,
119 const std::string& pincode) = 0;
120
121 // This method will be called when the Bluetooth daemon requires that the
122 // user enter the Passkey |passkey| into the device |device| so that it
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000123 // may be authenticated.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000124 //
125 // This is used for Bluetooth 2.1 and later devices that support input
126 // but not display, such as keyboards. The Passkey is a numeric in the
127 // range 0-999999 and should be always presented zero-padded to six
128 // digits.
129 virtual void DisplayPasskey(BluetoothDevice* device,
130 uint32 passkey) = 0;
131
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100132 // This method will be called when the Bluetooth daemon gets a notification
133 // of a key entered on the device |device| while pairing with the device
134 // using a PIN code or a Passkey.
135 //
136 // This method will be called only after DisplayPinCode() or
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000137 // DisplayPasskey() method is called, but is not warranted to be called
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100138 // on every pairing process that requires a PIN code or a Passkey because
139 // some device may not support this feature.
140 //
141 // The |entered| value describes the number of keys entered so far,
142 // including the last [enter] key. A first call to KeysEntered() with
143 // |entered| as 0 will be sent when the device supports this feature.
144 virtual void KeysEntered(BluetoothDevice* device,
145 uint32 entered) = 0;
146
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000147 // This method will be called when the Bluetooth daemon requires that the
148 // user confirm that the Passkey |passkey| is displayed on the screen
149 // of the device |device| so that it may be authenticated. The delegate
150 // should display to the user and ask for confirmation, then call
151 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
152 // to reject or CancelPairing() on the device to cancel authentication
153 // for any other reason.
154 //
155 // This is used for Bluetooth 2.1 and later devices that support display,
156 // such as other computers or phones. The Passkey is a numeric in the
157 // range 0-999999 and should be always present zero-padded to six
158 // digits.
159 virtual void ConfirmPasskey(BluetoothDevice* device,
160 uint32 passkey) = 0;
161
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000162 // This method will be called when the Bluetooth daemon requires that a
163 // pairing request, usually only incoming, using the just-works model is
164 // authorized. The delegate should decide whether the user should confirm
165 // or not, then call ConfirmPairing() on the device to confirm the pairing
166 // (whether by user action or by default), RejectPairing() on the device to
167 // reject or CancelPairing() on the device to cancel authorization for
168 // any other reason.
169 virtual void AuthorizePairing(BluetoothDevice* device) = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000170 };
171
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000172 // Returns true if uuid is in a a valid canonical format
173 // (see utils::CanonicalUuid).
174 static bool IsUUIDValid(const std::string& uuid);
175
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000176 virtual ~BluetoothDevice();
177
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100178 // Returns the Bluetooth class of the device, used by GetDeviceType()
179 // and metrics logging,
180 virtual uint32 GetBluetoothClass() const = 0;
181
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000182 // Returns the Bluetooth of address the device. This should be used as
183 // a unique key to identify the device and copied where needed.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100184 virtual std::string GetAddress() const = 0;
185
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000186 // Returns the allocation source of the identifier returned by GetVendorID(),
187 // where available, or VENDOR_ID_UNKNOWN where not.
188 virtual VendorIDSource GetVendorIDSource() const = 0;
189
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100190 // Returns the Vendor ID of the device, where available.
191 virtual uint16 GetVendorID() const = 0;
192
193 // Returns the Product ID of the device, where available.
194 virtual uint16 GetProductID() const = 0;
195
196 // Returns the Device ID of the device, typically the release or version
197 // number in BCD format, where available.
198 virtual uint16 GetDeviceID() const = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000199
200 // Returns the name of the device suitable for displaying, this may
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100201 // be a synthesized string containing the address and localized type name
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000202 // if the device has no obtained name.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000203 virtual base::string16 GetName() const;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000204
205 // Returns the type of the device, limited to those we support or are
206 // aware of, by decoding the bluetooth class information. The returned
207 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
208 // DEVICE_PERIPHERAL.
209 DeviceType GetDeviceType() const;
210
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100211 // Indicates whether the device is known to support pairing based on its
212 // device class and address.
213 bool IsPairable() const;
214
215 // Indicates whether the device is paired with the adapter.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000216 virtual bool IsPaired() const = 0;
217
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100218 // Indicates whether the device is currently connected to the adapter.
219 // Note that if IsConnected() is true, does not imply that the device is
220 // connected to any application or service. If the device is not paired, it
221 // could be still connected to the adapter for other reason, for example, to
222 // request the adapter's SDP records. The same holds for paired devices, since
223 // they could be connected to the adapter but not to an application.
224 virtual bool IsConnected() const = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000225
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100226 // Indicates whether the paired device accepts connections initiated from the
227 // adapter. This value is undefined for unpaired devices.
228 virtual bool IsConnectable() const = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000229
230 // Indicates whether there is a call to Connect() ongoing. For this attribute,
231 // we consider a call is ongoing if none of the callbacks passed to Connect()
232 // were called after the corresponding call to Connect().
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100233 virtual bool IsConnecting() const = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000234
Torne (Richard Coles)23730a62014-03-21 14:25:57 +0000235 // Returns the set of UUIDs that this device supports. For classic Bluetooth
236 // devices this data is collected from both the EIR data and SDP tables,
237 // for Low Energy devices this data is collected from AD and GATT primary
238 // services, for dual mode devices this may be collected from both./
239 //
240 // All UUIDs are returned in the canonical 128-bit format.
241 typedef std::vector<std::string> UUIDList;
242 virtual UUIDList GetUUIDs() const = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000243
244 // The ErrorCallback is used for methods that can fail in which case it
245 // is called, in the success case the callback is simply not called.
246 typedef base::Callback<void()> ErrorCallback;
247
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000248 // The ConnectErrorCallback is used for methods that can fail with an error,
249 // passed back as an error code argument to this callback.
250 // In the success case this callback is not called.
251 typedef base::Callback<void(enum ConnectErrorCode)> ConnectErrorCallback;
252
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000253 // Indicates whether the device is currently pairing and expecting a
254 // PIN Code to be returned.
255 virtual bool ExpectingPinCode() const = 0;
256
257 // Indicates whether the device is currently pairing and expecting a
258 // Passkey to be returned.
259 virtual bool ExpectingPasskey() const = 0;
260
261 // Indicates whether the device is currently pairing and expecting
262 // confirmation of a displayed passkey.
263 virtual bool ExpectingConfirmation() const = 0;
264
265 // SocketCallback is used by ConnectToService to return a BluetoothSocket to
266 // the caller, or NULL if there was an error. The socket will remain open
267 // until the last reference to the returned BluetoothSocket is released.
268 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
269 SocketCallback;
270
271 // Initiates a connection to the device, pairing first if necessary.
272 //
273 // Method calls will be made on the supplied object |pairing_delegate|
274 // to indicate what display, and in response should make method calls
275 // back to the device object. Not all devices require user responses
276 // during pairing, so it is normal for |pairing_delegate| to receive no
277 // calls. To explicitly force a low-security connection without bonding,
278 // pass NULL, though this is ignored if the device is already paired.
279 //
280 // If the request fails, |error_callback| will be called; otherwise,
281 // |callback| is called when the request is complete.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000282 // After calling Connect, CancelPairing should be called to cancel the pairing
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000283 // process and release the pairing delegate if user cancels the pairing and
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000284 // closes the pairing UI.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000285 virtual void Connect(PairingDelegate* pairing_delegate,
286 const base::Closure& callback,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000287 const ConnectErrorCallback& error_callback) = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000288
289 // Sends the PIN code |pincode| to the remote device during pairing.
290 //
291 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
292 // for which there is no automatic pairing or special handling.
293 virtual void SetPinCode(const std::string& pincode) = 0;
294
295 // Sends the Passkey |passkey| to the remote device during pairing.
296 //
297 // Passkeys are generally required for Bluetooth 2.1 and later devices
298 // which cannot provide input or display on their own, and don't accept
299 // passkey-less pairing, and are a numeric in the range 0-999999.
300 virtual void SetPasskey(uint32 passkey) = 0;
301
302 // Confirms to the remote device during pairing that a passkey provided by
303 // the ConfirmPasskey() delegate call is displayed on both devices.
304 virtual void ConfirmPairing() = 0;
305
306 // Rejects a pairing or connection request from a remote device.
307 virtual void RejectPairing() = 0;
308
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000309 // Cancels a pairing or connection attempt to a remote device, releasing
310 // the pairing delegate.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000311 virtual void CancelPairing() = 0;
312
313 // Disconnects the device, terminating the low-level ACL connection
314 // and any application connections using it. Link keys and other pairing
315 // information are not discarded, and the device object is not deleted.
316 // If the request fails, |error_callback| will be called; otherwise,
317 // |callback| is called when the request is complete.
318 virtual void Disconnect(const base::Closure& callback,
319 const ErrorCallback& error_callback) = 0;
320
321 // Disconnects the device, terminating the low-level ACL connection
322 // and any application connections using it, and then discards link keys
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100323 // and other pairing information. The device object remains valid until
324 // returning from the calling function, after which it should be assumed to
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000325 // have been deleted. If the request fails, |error_callback| will be called.
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100326 // There is no callback for success because this object is often deleted
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000327 // before that callback would be called.
328 virtual void Forget(const ErrorCallback& error_callback) = 0;
329
330 // Attempts to open a socket to a service matching |uuid| on this device. If
331 // the connection is successful, |callback| is called with a BluetoothSocket.
332 // Otherwise |callback| is called with NULL. The socket is closed as soon as
333 // all references to the BluetoothSocket are released. Note that the
334 // BluetoothSocket object can outlive both this BluetoothDevice and the
335 // BluetoothAdapter for this device.
336 virtual void ConnectToService(const std::string& service_uuid,
337 const SocketCallback& callback) = 0;
338
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100339 // Attempts to initiate an outgoing connection to this device for the profile
340 // identified by |profile|, on success the profile's connection callback
341 // will be called as well as |callback|; on failure |error_callback| will be
342 // called.
343 virtual void ConnectToProfile(BluetoothProfile* profile,
344 const base::Closure& callback,
345 const ErrorCallback& error_callback) = 0;
346
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000347 // Sets the Out Of Band pairing data for this device to |data|. Exactly one
348 // of |callback| or |error_callback| will be run.
349 virtual void SetOutOfBandPairingData(
350 const BluetoothOutOfBandPairingData& data,
351 const base::Closure& callback,
352 const ErrorCallback& error_callback) = 0;
353
354 // Clears the Out Of Band pairing data for this device. Exactly one of
355 // |callback| or |error_callback| will be run.
356 virtual void ClearOutOfBandPairingData(
357 const base::Closure& callback,
358 const ErrorCallback& error_callback) = 0;
359
360 protected:
361 BluetoothDevice();
362
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100363 // Returns the internal name of the Bluetooth device, used by GetName().
364 virtual std::string GetDeviceName() const = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000365
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000366 private:
367 // Returns a localized string containing the device's bluetooth address and
368 // a device type for display when |name_| is empty.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000369 base::string16 GetAddressWithLocalizedDeviceTypeName() const;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000370};
371
372} // namespace device
373
374#endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_