Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device.h
blob1b9b3a9e3dae688398ac5003b9b6d7b39a40002a
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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/string16.h"
16 #include "device/bluetooth/bluetooth_export.h"
17 #include "device/bluetooth/bluetooth_uuid.h"
18 #include "net/log/net_log.h"
20 namespace base {
21 class BinaryValue;
24 namespace device {
26 class BluetoothGattConnection;
27 class BluetoothGattService;
28 class BluetoothSocket;
29 class BluetoothUUID;
31 // BluetoothDevice represents a remote Bluetooth device, both its properties and
32 // capabilities as discovered by a local adapter and actions that may be
33 // performed on the remove device such as pairing, connection and disconnection.
35 // The class is instantiated and managed by the BluetoothAdapter class
36 // and pointers should only be obtained from that class and not cached,
37 // instead use the GetAddress() method as a unique key for a device.
39 // Since the lifecycle of BluetoothDevice instances is managed by
40 // BluetoothAdapter, that class rather than this provides observer methods
41 // for devices coming and going, as well as properties being updated.
42 class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
43 public:
44 // Possible values that may be returned by GetVendorIDSource(),
45 // indicating different organisations that allocate the identifiers returned
46 // by GetVendorID().
47 enum VendorIDSource {
48 VENDOR_ID_UNKNOWN,
49 VENDOR_ID_BLUETOOTH,
50 VENDOR_ID_USB,
51 VENDOR_ID_MAX_VALUE = VENDOR_ID_USB
54 // Possible values that may be returned by GetDeviceType(), representing
55 // different types of bluetooth device that we support or are aware of
56 // decoded from the bluetooth class information.
57 enum DeviceType {
58 DEVICE_UNKNOWN,
59 DEVICE_COMPUTER,
60 DEVICE_PHONE,
61 DEVICE_MODEM,
62 DEVICE_AUDIO,
63 DEVICE_CAR_AUDIO,
64 DEVICE_VIDEO,
65 DEVICE_PERIPHERAL,
66 DEVICE_JOYSTICK,
67 DEVICE_GAMEPAD,
68 DEVICE_KEYBOARD,
69 DEVICE_MOUSE,
70 DEVICE_TABLET,
71 DEVICE_KEYBOARD_MOUSE_COMBO
74 // The value returned if the RSSI or transmit power cannot be read.
75 static const int kUnknownPower = 127;
77 struct ConnectionInfo {
78 int rssi;
79 int transmit_power;
80 int max_transmit_power;
82 ConnectionInfo();
83 ConnectionInfo(int rssi, int transmit_power, int max_transmit_power);
84 ~ConnectionInfo();
87 // Possible errors passed back to an error callback function in case of a
88 // failed call to Connect().
89 enum ConnectErrorCode {
90 ERROR_UNKNOWN,
91 ERROR_INPROGRESS,
92 ERROR_FAILED,
93 ERROR_AUTH_FAILED,
94 ERROR_AUTH_CANCELED,
95 ERROR_AUTH_REJECTED,
96 ERROR_AUTH_TIMEOUT,
97 ERROR_UNSUPPORTED_DEVICE
100 typedef std::vector<BluetoothUUID> UUIDList;
102 // Interface for negotiating pairing of bluetooth devices.
103 class PairingDelegate {
104 public:
105 virtual ~PairingDelegate() {}
107 // This method will be called when the Bluetooth daemon requires a
108 // PIN Code for authentication of the device |device|, the delegate should
109 // obtain the code from the user and call SetPinCode() on the device to
110 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
111 // the request.
113 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
114 // for which there is no automatic pairing or special handling.
115 virtual void RequestPinCode(BluetoothDevice* device) = 0;
117 // This method will be called when the Bluetooth daemon requires a
118 // Passkey for authentication of the device |device|, the delegate should
119 // obtain the passkey from the user (a numeric in the range 0-999999) and
120 // call SetPasskey() on the device to provide it, or RejectPairing() or
121 // CancelPairing() to reject or cancel the request.
123 // Passkeys are generally required for Bluetooth 2.1 and later devices
124 // which cannot provide input or display on their own, and don't accept
125 // passkey-less pairing.
126 virtual void RequestPasskey(BluetoothDevice* device) = 0;
128 // This method will be called when the Bluetooth daemon requires that the
129 // user enter the PIN code |pincode| into the device |device| so that it
130 // may be authenticated.
132 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
133 // |pincode| will always be a six-digit numeric in the range 000000-999999
134 // for compatibility with later specifications.
135 virtual void DisplayPinCode(BluetoothDevice* device,
136 const std::string& pincode) = 0;
138 // This method will be called when the Bluetooth daemon requires that the
139 // user enter the Passkey |passkey| into the device |device| so that it
140 // may be authenticated.
142 // This is used for Bluetooth 2.1 and later devices that support input
143 // but not display, such as keyboards. The Passkey is a numeric in the
144 // range 0-999999 and should be always presented zero-padded to six
145 // digits.
146 virtual void DisplayPasskey(BluetoothDevice* device,
147 uint32 passkey) = 0;
149 // This method will be called when the Bluetooth daemon gets a notification
150 // of a key entered on the device |device| while pairing with the device
151 // using a PIN code or a Passkey.
153 // This method will be called only after DisplayPinCode() or
154 // DisplayPasskey() method is called, but is not warranted to be called
155 // on every pairing process that requires a PIN code or a Passkey because
156 // some device may not support this feature.
158 // The |entered| value describes the number of keys entered so far,
159 // including the last [enter] key. A first call to KeysEntered() with
160 // |entered| as 0 will be sent when the device supports this feature.
161 virtual void KeysEntered(BluetoothDevice* device,
162 uint32 entered) = 0;
164 // This method will be called when the Bluetooth daemon requires that the
165 // user confirm that the Passkey |passkey| is displayed on the screen
166 // of the device |device| so that it may be authenticated. The delegate
167 // should display to the user and ask for confirmation, then call
168 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
169 // to reject or CancelPairing() on the device to cancel authentication
170 // for any other reason.
172 // This is used for Bluetooth 2.1 and later devices that support display,
173 // such as other computers or phones. The Passkey is a numeric in the
174 // range 0-999999 and should be always present zero-padded to six
175 // digits.
176 virtual void ConfirmPasskey(BluetoothDevice* device,
177 uint32 passkey) = 0;
179 // This method will be called when the Bluetooth daemon requires that a
180 // pairing request, usually only incoming, using the just-works model is
181 // authorized. The delegate should decide whether the user should confirm
182 // or not, then call ConfirmPairing() on the device to confirm the pairing
183 // (whether by user action or by default), RejectPairing() on the device to
184 // reject or CancelPairing() on the device to cancel authorization for
185 // any other reason.
186 virtual void AuthorizePairing(BluetoothDevice* device) = 0;
189 virtual ~BluetoothDevice();
191 // Returns the Bluetooth class of the device, used by GetDeviceType()
192 // and metrics logging,
193 virtual uint32 GetBluetoothClass() const = 0;
195 // Returns the identifier of the bluetooth device.
196 virtual std::string GetIdentifier() const;
198 // Returns the Bluetooth of address the device. This should be used as
199 // a unique key to identify the device and copied where needed.
200 virtual std::string GetAddress() const = 0;
202 // Returns the allocation source of the identifier returned by GetVendorID(),
203 // where available, or VENDOR_ID_UNKNOWN where not.
204 virtual VendorIDSource GetVendorIDSource() const = 0;
206 // Returns the Vendor ID of the device, where available.
207 virtual uint16 GetVendorID() const = 0;
209 // Returns the Product ID of the device, where available.
210 virtual uint16 GetProductID() const = 0;
212 // Returns the Device ID of the device, typically the release or version
213 // number in BCD format, where available.
214 virtual uint16 GetDeviceID() const = 0;
216 // Returns the name of the device suitable for displaying, this may
217 // be a synthesized string containing the address and localized type name
218 // if the device has no obtained name.
219 virtual base::string16 GetName() const;
221 // Returns the type of the device, limited to those we support or are
222 // aware of, by decoding the bluetooth class information. The returned
223 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
224 // DEVICE_PERIPHERAL.
225 DeviceType GetDeviceType() const;
227 // Indicates whether the device is known to support pairing based on its
228 // device class and address.
229 bool IsPairable() const;
231 // Indicates whether the device is paired with the adapter.
232 virtual bool IsPaired() const = 0;
234 // Indicates whether the device is currently connected to the adapter.
235 // Note that if IsConnected() is true, does not imply that the device is
236 // connected to any application or service. If the device is not paired, it
237 // could be still connected to the adapter for other reason, for example, to
238 // request the adapter's SDP records. The same holds for paired devices, since
239 // they could be connected to the adapter but not to an application.
240 virtual bool IsConnected() const = 0;
242 // Indicates whether the paired device accepts connections initiated from the
243 // adapter. This value is undefined for unpaired devices.
244 virtual bool IsConnectable() const = 0;
246 // Indicates whether there is a call to Connect() ongoing. For this attribute,
247 // we consider a call is ongoing if none of the callbacks passed to Connect()
248 // were called after the corresponding call to Connect().
249 virtual bool IsConnecting() const = 0;
251 // Indicates whether the device can be trusted, based on device properties,
252 // such as vendor and product id.
253 bool IsTrustable() const;
255 // Returns the set of UUIDs that this device supports. For classic Bluetooth
256 // devices this data is collected from both the EIR data and SDP tables,
257 // for Low Energy devices this data is collected from AD and GATT primary
258 // services, for dual mode devices this may be collected from both./
259 virtual UUIDList GetUUIDs() const = 0;
261 // The ErrorCallback is used for methods that can fail in which case it
262 // is called, in the success case the callback is simply not called.
263 typedef base::Callback<void()> ErrorCallback;
265 // The ConnectErrorCallback is used for methods that can fail with an error,
266 // passed back as an error code argument to this callback.
267 // In the success case this callback is not called.
268 typedef base::Callback<void(enum ConnectErrorCode)> ConnectErrorCallback;
270 typedef base::Callback<void(const ConnectionInfo&)> ConnectionInfoCallback;
272 // Indicates whether the device is currently pairing and expecting a
273 // PIN Code to be returned.
274 virtual bool ExpectingPinCode() const = 0;
276 // Indicates whether the device is currently pairing and expecting a
277 // Passkey to be returned.
278 virtual bool ExpectingPasskey() const = 0;
280 // Indicates whether the device is currently pairing and expecting
281 // confirmation of a displayed passkey.
282 virtual bool ExpectingConfirmation() const = 0;
284 // Returns the RSSI and TX power of the active connection to the device:
286 // The RSSI indicates the power present in the received radio signal, measured
287 // in dBm, to a resolution of 1dBm. Larger (typically, less negative) values
288 // indicate a stronger signal.
290 // The transmit power indicates the strength of the signal broadcast from the
291 // host's Bluetooth antenna when communicating with the device, measured in
292 // dBm, to a resolution of 1dBm. Larger (typically, less negative) values
293 // indicate a stronger signal.
295 // If the device isn't connected, then the ConnectionInfo struct passed into
296 // the callback will be populated with |kUnknownPower|.
297 virtual void GetConnectionInfo(const ConnectionInfoCallback& callback) = 0;
299 // Initiates a connection to the device, pairing first if necessary.
301 // Method calls will be made on the supplied object |pairing_delegate|
302 // to indicate what display, and in response should make method calls
303 // back to the device object. Not all devices require user responses
304 // during pairing, so it is normal for |pairing_delegate| to receive no
305 // calls. To explicitly force a low-security connection without bonding,
306 // pass NULL, though this is ignored if the device is already paired.
308 // If the request fails, |error_callback| will be called; otherwise,
309 // |callback| is called when the request is complete.
310 // After calling Connect, CancelPairing should be called to cancel the pairing
311 // process and release the pairing delegate if user cancels the pairing and
312 // closes the pairing UI.
313 virtual void Connect(PairingDelegate* pairing_delegate,
314 const base::Closure& callback,
315 const ConnectErrorCallback& error_callback) = 0;
317 // Sends the PIN code |pincode| to the remote device during pairing.
319 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
320 // for which there is no automatic pairing or special handling.
321 virtual void SetPinCode(const std::string& pincode) = 0;
323 // Sends the Passkey |passkey| to the remote device during pairing.
325 // Passkeys are generally required for Bluetooth 2.1 and later devices
326 // which cannot provide input or display on their own, and don't accept
327 // passkey-less pairing, and are a numeric in the range 0-999999.
328 virtual void SetPasskey(uint32 passkey) = 0;
330 // Confirms to the remote device during pairing that a passkey provided by
331 // the ConfirmPasskey() delegate call is displayed on both devices.
332 virtual void ConfirmPairing() = 0;
334 // Rejects a pairing or connection request from a remote device.
335 virtual void RejectPairing() = 0;
337 // Cancels a pairing or connection attempt to a remote device, releasing
338 // the pairing delegate.
339 virtual void CancelPairing() = 0;
341 // Disconnects the device, terminating the low-level ACL connection
342 // and any application connections using it. Link keys and other pairing
343 // information are not discarded, and the device object is not deleted.
344 // If the request fails, |error_callback| will be called; otherwise,
345 // |callback| is called when the request is complete.
346 virtual void Disconnect(const base::Closure& callback,
347 const ErrorCallback& error_callback) = 0;
349 // Disconnects the device, terminating the low-level ACL connection
350 // and any application connections using it, and then discards link keys
351 // and other pairing information. The device object remains valid until
352 // returning from the calling function, after which it should be assumed to
353 // have been deleted. If the request fails, |error_callback| will be called.
354 // There is no callback for success because this object is often deleted
355 // before that callback would be called.
356 virtual void Forget(const ErrorCallback& error_callback) = 0;
358 // Attempts to initiate an outgoing L2CAP or RFCOMM connection to the
359 // advertised service on this device matching |uuid|, performing an SDP lookup
360 // if necessary to determine the correct protocol and channel for the
361 // connection. |callback| will be called on a successful connection with a
362 // BluetoothSocket instance that is to be owned by the receiver.
363 // |error_callback| will be called on failure with a message indicating the
364 // cause.
365 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
366 ConnectToServiceCallback;
367 typedef base::Callback<void(const std::string& message)>
368 ConnectToServiceErrorCallback;
369 virtual void ConnectToService(
370 const BluetoothUUID& uuid,
371 const ConnectToServiceCallback& callback,
372 const ConnectToServiceErrorCallback& error_callback) = 0;
374 // Attempts to initiate an insecure outgoing L2CAP or RFCOMM connection to the
375 // advertised service on this device matching |uuid|, performing an SDP lookup
376 // if necessary to determine the correct protocol and channel for the
377 // connection. Unlike ConnectToService, the outgoing connection will request
378 // no bonding rather than general bonding. |callback| will be called on a
379 // successful connection with a BluetoothSocket instance that is to be owned
380 // by the receiver. |error_callback| will be called on failure with a message
381 // indicating the cause.
382 virtual void ConnectToServiceInsecurely(
383 const device::BluetoothUUID& uuid,
384 const ConnectToServiceCallback& callback,
385 const ConnectToServiceErrorCallback& error_callback) = 0;
387 // Opens a new GATT connection to this device. On success, a new
388 // BluetoothGattConnection will be handed to the caller via |callback|. On
389 // error, |error_callback| will be called. The connection will be kept alive,
390 // as long as there is at least one active GATT connection. In the case that
391 // the underlying connection gets terminated, either due to a call to
392 // BluetoothDevice::Disconnect or other unexpected circumstances, the
393 // returned BluetoothGattConnection will be automatically marked as inactive.
394 // To monitor the state of the connection, observe the
395 // BluetoothAdapter::Observer::DeviceChanged method.
396 typedef base::Callback<void(scoped_ptr<BluetoothGattConnection>)>
397 GattConnectionCallback;
398 virtual void CreateGattConnection(
399 const GattConnectionCallback& callback,
400 const ConnectErrorCallback& error_callback) = 0;
402 // Returns the list of discovered GATT services.
403 virtual std::vector<BluetoothGattService*> GetGattServices() const;
405 // Returns the GATT service with device-specific identifier |identifier|.
406 // Returns NULL, if no such service exists.
407 virtual BluetoothGattService* GetGattService(
408 const std::string& identifier) const;
410 // Returns service data of a service given its UUID.
411 virtual base::BinaryValue* GetServiceData(BluetoothUUID serviceUUID) const;
413 // Returns the list UUIDs of services that have service data.
414 virtual UUIDList GetServiceDataUUIDs() const;
416 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where
417 // each 'X' is a hex digit. If the input |address| is invalid, returns an
418 // empty string.
419 static std::string CanonicalizeAddress(const std::string& address);
421 protected:
422 BluetoothDevice();
424 // Returns the internal name of the Bluetooth device, used by GetName().
425 virtual std::string GetDeviceName() const = 0;
427 // Clears the list of service data.
428 void ClearServiceData();
430 // Set the data of a given service designated by its UUID.
431 void SetServiceData(BluetoothUUID serviceUUID, const char* buffer,
432 size_t size);
434 // Mapping from the platform-specific GATT service identifiers to
435 // BluetoothGattService objects.
436 typedef std::map<std::string, BluetoothGattService*> GattServiceMap;
437 GattServiceMap gatt_services_;
439 // Mapping from service UUID represented as a std::string of a bluetooth
440 // service to
441 // the specific data. The data is stored as BinaryValue.
442 scoped_ptr<base::DictionaryValue> services_data_;
444 private:
445 // Returns a localized string containing the device's bluetooth address and
446 // a device type for display when |name_| is empty.
447 base::string16 GetAddressWithLocalizedDeviceTypeName() const;
450 } // namespace device
452 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_