Revert 273817 "Record RenderViewContextMenu.Used histogram befor..."
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device.h
bloba495b209cc55d33003863db713e182e9c18791a3
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_uuid.h"
17 #include "net/base/net_log.h"
19 namespace device {
21 class BluetoothGattService;
22 class BluetoothProfile;
23 class BluetoothSocket;
24 class BluetoothUUID;
26 // BluetoothDevice represents a remote Bluetooth device, both its properties and
27 // capabilities as discovered by a local adapter and actions that may be
28 // performed on the remove device such as pairing, connection and disconnection.
30 // The class is instantiated and managed by the BluetoothAdapter class
31 // and pointers should only be obtained from that class and not cached,
32 // instead use the GetAddress() method as a unique key for a device.
34 // Since the lifecycle of BluetoothDevice instances is managed by
35 // BluetoothAdapter, that class rather than this provides observer methods
36 // for devices coming and going, as well as properties being updated.
37 class BluetoothDevice {
38 public:
39 // Possible values that may be returned by GetVendorIDSource(),
40 // indicating different organisations that allocate the identifiers returned
41 // by GetVendorID().
42 enum VendorIDSource {
43 VENDOR_ID_UNKNOWN,
44 VENDOR_ID_BLUETOOTH,
45 VENDOR_ID_USB
48 // Possible values that may be returned by GetDeviceType(), representing
49 // different types of bluetooth device that we support or are aware of
50 // decoded from the bluetooth class information.
51 enum DeviceType {
52 DEVICE_UNKNOWN,
53 DEVICE_COMPUTER,
54 DEVICE_PHONE,
55 DEVICE_MODEM,
56 DEVICE_AUDIO,
57 DEVICE_CAR_AUDIO,
58 DEVICE_VIDEO,
59 DEVICE_PERIPHERAL,
60 DEVICE_JOYSTICK,
61 DEVICE_GAMEPAD,
62 DEVICE_KEYBOARD,
63 DEVICE_MOUSE,
64 DEVICE_TABLET,
65 DEVICE_KEYBOARD_MOUSE_COMBO
68 // The value returned if the RSSI or transmit power cannot be read.
69 static const int kUnknownPower = 127;
71 // Possible errors passed back to an error callback function in case of a
72 // failed call to Connect().
73 enum ConnectErrorCode {
74 ERROR_UNKNOWN,
75 ERROR_INPROGRESS,
76 ERROR_FAILED,
77 ERROR_AUTH_FAILED,
78 ERROR_AUTH_CANCELED,
79 ERROR_AUTH_REJECTED,
80 ERROR_AUTH_TIMEOUT,
81 ERROR_UNSUPPORTED_DEVICE
84 // Interface for observing changes from bluetooth devices.
85 class Observer {
86 public:
87 virtual ~Observer() {}
89 // Called when a new GATT service |service| is added to the device |device|,
90 // as the service is received from the device. Don't cache |service|. Store
91 // its identifier instead (i.e. BluetoothGattService::GetIdentifier).
92 virtual void GattServiceAdded(BluetoothDevice* device,
93 BluetoothGattService* service) {}
95 // Called when the GATT service |service| is removed from the device
96 // |device|. This can happen if the attribute database of the remote device
97 // changes or when |device| gets removed.
98 virtual void GattServiceRemoved(BluetoothDevice* device,
99 BluetoothGattService* service) {}
101 // TODO(keybuk): add observers for pairing and connection.
104 // Interface for negotiating pairing of bluetooth devices.
105 class PairingDelegate {
106 public:
107 virtual ~PairingDelegate() {}
109 // This method will be called when the Bluetooth daemon requires a
110 // PIN Code for authentication of the device |device|, the delegate should
111 // obtain the code from the user and call SetPinCode() on the device to
112 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
113 // the request.
115 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
116 // for which there is no automatic pairing or special handling.
117 virtual void RequestPinCode(BluetoothDevice* device) = 0;
119 // This method will be called when the Bluetooth daemon requires a
120 // Passkey for authentication of the device |device|, the delegate should
121 // obtain the passkey from the user (a numeric in the range 0-999999) and
122 // call SetPasskey() on the device to provide it, or RejectPairing() or
123 // CancelPairing() to reject or cancel the request.
125 // Passkeys are generally required for Bluetooth 2.1 and later devices
126 // which cannot provide input or display on their own, and don't accept
127 // passkey-less pairing.
128 virtual void RequestPasskey(BluetoothDevice* device) = 0;
130 // This method will be called when the Bluetooth daemon requires that the
131 // user enter the PIN code |pincode| into the device |device| so that it
132 // may be authenticated.
134 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
135 // |pincode| will always be a six-digit numeric in the range 000000-999999
136 // for compatibility with later specifications.
137 virtual void DisplayPinCode(BluetoothDevice* device,
138 const std::string& pincode) = 0;
140 // This method will be called when the Bluetooth daemon requires that the
141 // user enter the Passkey |passkey| into the device |device| so that it
142 // may be authenticated.
144 // This is used for Bluetooth 2.1 and later devices that support input
145 // but not display, such as keyboards. The Passkey is a numeric in the
146 // range 0-999999 and should be always presented zero-padded to six
147 // digits.
148 virtual void DisplayPasskey(BluetoothDevice* device,
149 uint32 passkey) = 0;
151 // This method will be called when the Bluetooth daemon gets a notification
152 // of a key entered on the device |device| while pairing with the device
153 // using a PIN code or a Passkey.
155 // This method will be called only after DisplayPinCode() or
156 // DisplayPasskey() method is called, but is not warranted to be called
157 // on every pairing process that requires a PIN code or a Passkey because
158 // some device may not support this feature.
160 // The |entered| value describes the number of keys entered so far,
161 // including the last [enter] key. A first call to KeysEntered() with
162 // |entered| as 0 will be sent when the device supports this feature.
163 virtual void KeysEntered(BluetoothDevice* device,
164 uint32 entered) = 0;
166 // This method will be called when the Bluetooth daemon requires that the
167 // user confirm that the Passkey |passkey| is displayed on the screen
168 // of the device |device| so that it may be authenticated. The delegate
169 // should display to the user and ask for confirmation, then call
170 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
171 // to reject or CancelPairing() on the device to cancel authentication
172 // for any other reason.
174 // This is used for Bluetooth 2.1 and later devices that support display,
175 // such as other computers or phones. The Passkey is a numeric in the
176 // range 0-999999 and should be always present zero-padded to six
177 // digits.
178 virtual void ConfirmPasskey(BluetoothDevice* device,
179 uint32 passkey) = 0;
181 // This method will be called when the Bluetooth daemon requires that a
182 // pairing request, usually only incoming, using the just-works model is
183 // authorized. The delegate should decide whether the user should confirm
184 // or not, then call ConfirmPairing() on the device to confirm the pairing
185 // (whether by user action or by default), RejectPairing() on the device to
186 // reject or CancelPairing() on the device to cancel authorization for
187 // any other reason.
188 virtual void AuthorizePairing(BluetoothDevice* device) = 0;
191 virtual ~BluetoothDevice();
193 // Adds and removes observers for events on this Bluetooth device. If
194 // monitoring multiple devices, check the |device| parameter of the observer
195 // methods to determine which device is issuing the event.
196 virtual void AddObserver(Observer* observer) = 0;
197 virtual void RemoveObserver(Observer* observer) = 0;
199 // Returns the Bluetooth class of the device, used by GetDeviceType()
200 // and metrics logging,
201 virtual uint32 GetBluetoothClass() const = 0;
203 // Returns the Bluetooth of address the device. This should be used as
204 // a unique key to identify the device and copied where needed.
205 virtual std::string GetAddress() const = 0;
207 // Returns the allocation source of the identifier returned by GetVendorID(),
208 // where available, or VENDOR_ID_UNKNOWN where not.
209 virtual VendorIDSource GetVendorIDSource() const = 0;
211 // Returns the Vendor ID of the device, where available.
212 virtual uint16 GetVendorID() const = 0;
214 // Returns the Product ID of the device, where available.
215 virtual uint16 GetProductID() const = 0;
217 // Returns the Device ID of the device, typically the release or version
218 // number in BCD format, where available.
219 virtual uint16 GetDeviceID() const = 0;
221 // Returns the name of the device suitable for displaying, this may
222 // be a synthesized string containing the address and localized type name
223 // if the device has no obtained name.
224 virtual base::string16 GetName() const;
226 // Returns the type of the device, limited to those we support or are
227 // aware of, by decoding the bluetooth class information. The returned
228 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
229 // DEVICE_PERIPHERAL.
230 DeviceType GetDeviceType() const;
232 // Gets the "received signal strength indication" (RSSI) of the current
233 // connection to the device. The RSSI indicates the power present in the
234 // received radio signal, measured in dBm, to a resolution of 1dBm. Larger
235 // (typically, less negative) values indicate a stronger signal.
236 // If the device is not currently connected, then returns the RSSI read from
237 // the last inquiry that returned the device, where available. In case of an
238 // error, returns |kUnknownPower|. Otherwise, returns the connection's RSSI.
239 virtual int GetRSSI() const = 0;
241 // These two methods are used to read the current or maximum transmit power
242 // ("Tx power") of the current connection to the device. The transmit power
243 // indicates the strength of the signal broadcast from the host's Bluetooth
244 // antenna when communicating with the device, measured in dBm, to a
245 // resolution of 1dBm. Larger (typically, less negative) values
246 // indicate a stronger signal.
247 // It is only meaningful to call this method when there is a connection
248 // established to the device. If there is no connection, or in case of an
249 // error, returns |kUnknownPower|. Otherwise, returns the connection's
250 // transmit power.
251 virtual int GetCurrentHostTransmitPower() const = 0;
252 virtual int GetMaximumHostTransmitPower() const = 0;
254 // Indicates whether the device is known to support pairing based on its
255 // device class and address.
256 bool IsPairable() const;
258 // Indicates whether the device is paired with the adapter.
259 virtual bool IsPaired() const = 0;
261 // Indicates whether the device is currently connected to the adapter.
262 // Note that if IsConnected() is true, does not imply that the device is
263 // connected to any application or service. If the device is not paired, it
264 // could be still connected to the adapter for other reason, for example, to
265 // request the adapter's SDP records. The same holds for paired devices, since
266 // they could be connected to the adapter but not to an application.
267 virtual bool IsConnected() const = 0;
269 // Indicates whether the paired device accepts connections initiated from the
270 // adapter. This value is undefined for unpaired devices.
271 virtual bool IsConnectable() const = 0;
273 // Indicates whether there is a call to Connect() ongoing. For this attribute,
274 // we consider a call is ongoing if none of the callbacks passed to Connect()
275 // were called after the corresponding call to Connect().
276 virtual bool IsConnecting() const = 0;
278 // Indicates whether the device can be trusted, based on device properties,
279 // such as vendor and product id.
280 bool IsTrustable() const;
282 // Returns the set of UUIDs that this device supports. For classic Bluetooth
283 // devices this data is collected from both the EIR data and SDP tables,
284 // for Low Energy devices this data is collected from AD and GATT primary
285 // services, for dual mode devices this may be collected from both./
286 typedef std::vector<BluetoothUUID> UUIDList;
287 virtual UUIDList GetUUIDs() const = 0;
289 // The ErrorCallback is used for methods that can fail in which case it
290 // is called, in the success case the callback is simply not called.
291 typedef base::Callback<void()> ErrorCallback;
293 // The ConnectErrorCallback is used for methods that can fail with an error,
294 // passed back as an error code argument to this callback.
295 // In the success case this callback is not called.
296 typedef base::Callback<void(enum ConnectErrorCode)> ConnectErrorCallback;
298 // Indicates whether the device is currently pairing and expecting a
299 // PIN Code to be returned.
300 virtual bool ExpectingPinCode() const = 0;
302 // Indicates whether the device is currently pairing and expecting a
303 // Passkey to be returned.
304 virtual bool ExpectingPasskey() const = 0;
306 // Indicates whether the device is currently pairing and expecting
307 // confirmation of a displayed passkey.
308 virtual bool ExpectingConfirmation() const = 0;
310 // Initiates a connection to the device, pairing first if necessary.
312 // Method calls will be made on the supplied object |pairing_delegate|
313 // to indicate what display, and in response should make method calls
314 // back to the device object. Not all devices require user responses
315 // during pairing, so it is normal for |pairing_delegate| to receive no
316 // calls. To explicitly force a low-security connection without bonding,
317 // pass NULL, though this is ignored if the device is already paired.
319 // If the request fails, |error_callback| will be called; otherwise,
320 // |callback| is called when the request is complete.
321 // After calling Connect, CancelPairing should be called to cancel the pairing
322 // process and release the pairing delegate if user cancels the pairing and
323 // closes the pairing UI.
324 virtual void Connect(PairingDelegate* pairing_delegate,
325 const base::Closure& callback,
326 const ConnectErrorCallback& error_callback) = 0;
328 // Sends the PIN code |pincode| to the remote device during pairing.
330 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
331 // for which there is no automatic pairing or special handling.
332 virtual void SetPinCode(const std::string& pincode) = 0;
334 // Sends the Passkey |passkey| to the remote device during pairing.
336 // Passkeys are generally required for Bluetooth 2.1 and later devices
337 // which cannot provide input or display on their own, and don't accept
338 // passkey-less pairing, and are a numeric in the range 0-999999.
339 virtual void SetPasskey(uint32 passkey) = 0;
341 // Confirms to the remote device during pairing that a passkey provided by
342 // the ConfirmPasskey() delegate call is displayed on both devices.
343 virtual void ConfirmPairing() = 0;
345 // Rejects a pairing or connection request from a remote device.
346 virtual void RejectPairing() = 0;
348 // Cancels a pairing or connection attempt to a remote device, releasing
349 // the pairing delegate.
350 virtual void CancelPairing() = 0;
352 // Disconnects the device, terminating the low-level ACL connection
353 // and any application connections using it. Link keys and other pairing
354 // information are not discarded, and the device object is not deleted.
355 // If the request fails, |error_callback| will be called; otherwise,
356 // |callback| is called when the request is complete.
357 virtual void Disconnect(const base::Closure& callback,
358 const ErrorCallback& error_callback) = 0;
360 // Disconnects the device, terminating the low-level ACL connection
361 // and any application connections using it, and then discards link keys
362 // and other pairing information. The device object remains valid until
363 // returning from the calling function, after which it should be assumed to
364 // have been deleted. If the request fails, |error_callback| will be called.
365 // There is no callback for success because this object is often deleted
366 // before that callback would be called.
367 virtual void Forget(const ErrorCallback& error_callback) = 0;
369 // Attempts to initiate an outgoing connection to this device for the profile
370 // identified by |profile|, on success the profile's connection callback
371 // will be called as well as |callback|; on failure |error_callback| will be
372 // called.
373 typedef base::Callback<void(const std::string&)>
374 ConnectToProfileErrorCallback;
375 virtual void ConnectToProfile(
376 BluetoothProfile* profile,
377 const base::Closure& callback,
378 const ConnectToProfileErrorCallback& error_callback) = 0;
380 // Attempts to initiate an outgoing L2CAP or RFCOMM connection to the
381 // advertised service on this device matching |uuid|, performing an SDP lookup
382 // if necessary to determine the correct protocol and channel for the
383 // connection. |callback| will be called on a successful connection with a
384 // BluetoothSocket instance that is to be owned by the receiver.
385 // |error_callback| will be called on failure with a message indicating the
386 // cause.
387 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
388 ConnectToServiceCallback;
389 typedef base::Callback<void(const std::string& message)>
390 ConnectToServiceErrorCallback;
391 virtual void ConnectToService(
392 const BluetoothUUID& uuid,
393 const ConnectToServiceCallback& callback,
394 const ConnectToServiceErrorCallback& error_callback) = 0;
396 // Starts monitoring the connection properties, RSSI and TX power. These
397 // properties will be tracked, and updated when their values change. Exactly
398 // one of |callback| or |error_callback| will be run.
399 virtual void StartConnectionMonitor(const base::Closure& callback,
400 const ErrorCallback& 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 the |address| in the canoncial format: XX:XX:XX:XX:XX:XX, where
411 // each 'X' is a hex digit. If the input |address| is invalid, returns an
412 // empty string.
413 static std::string CanonicalizeAddress(const std::string& address);
415 protected:
416 BluetoothDevice();
418 // Returns the internal name of the Bluetooth device, used by GetName().
419 virtual std::string GetDeviceName() const = 0;
421 // Mapping from the platform-specific GATT service identifiers to
422 // BluetoothGattService objects.
423 typedef std::map<std::string, BluetoothGattService*> GattServiceMap;
424 GattServiceMap gatt_services_;
426 private:
427 // Returns a localized string containing the device's bluetooth address and
428 // a device type for display when |name_| is empty.
429 base::string16 GetAddressWithLocalizedDeviceTypeName() const;
432 } // namespace device
434 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_