third_party: Add OWNERS for re2 library.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device.h
blobccf71dadfd2cadd13ee57be4ee5289d07c03f3ae
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 received signal strength, in dBm. This field is avaliable and valid
262 // only during discovery. If not during discovery, or RSSI wasn't reported,
263 // this method will return |kUnknownPower|.
264 virtual int16 GetInquiryRSSI() const = 0;
266 // The transmitted power level. This field is avaliable only for LE devices
267 // that include this field in AD. It is avaliable and valid only during
268 // discovery. If not during discovery, or TxPower wasn't reported, this
269 // method will return |kUnknownPower|.
270 virtual int16 GetInquiryTxPower() const = 0;
272 // The ErrorCallback is used for methods that can fail in which case it
273 // is called, in the success case the callback is simply not called.
274 typedef base::Callback<void()> ErrorCallback;
276 // The ConnectErrorCallback is used for methods that can fail with an error,
277 // passed back as an error code argument to this callback.
278 // In the success case this callback is not called.
279 typedef base::Callback<void(enum ConnectErrorCode)> ConnectErrorCallback;
281 typedef base::Callback<void(const ConnectionInfo&)> ConnectionInfoCallback;
283 // Indicates whether the device is currently pairing and expecting a
284 // PIN Code to be returned.
285 virtual bool ExpectingPinCode() const = 0;
287 // Indicates whether the device is currently pairing and expecting a
288 // Passkey to be returned.
289 virtual bool ExpectingPasskey() const = 0;
291 // Indicates whether the device is currently pairing and expecting
292 // confirmation of a displayed passkey.
293 virtual bool ExpectingConfirmation() const = 0;
295 // Returns the RSSI and TX power of the active connection to the device:
297 // The RSSI indicates the power present in the received radio signal, measured
298 // in dBm, to a resolution of 1dBm. Larger (typically, less negative) values
299 // indicate a stronger signal.
301 // The transmit power indicates the strength of the signal broadcast from the
302 // host's Bluetooth antenna when communicating with the device, measured in
303 // dBm, to a resolution of 1dBm. Larger (typically, less negative) values
304 // indicate a stronger signal.
306 // If the device isn't connected, then the ConnectionInfo struct passed into
307 // the callback will be populated with |kUnknownPower|.
308 virtual void GetConnectionInfo(const ConnectionInfoCallback& callback) = 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 L2CAP or RFCOMM connection to the
370 // advertised service on this device matching |uuid|, performing an SDP lookup
371 // if necessary to determine the correct protocol and channel for the
372 // connection. |callback| will be called on a successful connection with a
373 // BluetoothSocket instance that is to be owned by the receiver.
374 // |error_callback| will be called on failure with a message indicating the
375 // cause.
376 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
377 ConnectToServiceCallback;
378 typedef base::Callback<void(const std::string& message)>
379 ConnectToServiceErrorCallback;
380 virtual void ConnectToService(
381 const BluetoothUUID& uuid,
382 const ConnectToServiceCallback& callback,
383 const ConnectToServiceErrorCallback& error_callback) = 0;
385 // Attempts to initiate an insecure outgoing L2CAP or RFCOMM connection to the
386 // advertised service on this device matching |uuid|, performing an SDP lookup
387 // if necessary to determine the correct protocol and channel for the
388 // connection. Unlike ConnectToService, the outgoing connection will request
389 // no bonding rather than general bonding. |callback| will be called on a
390 // successful connection with a BluetoothSocket instance that is to be owned
391 // by the receiver. |error_callback| will be called on failure with a message
392 // indicating the cause.
393 virtual void ConnectToServiceInsecurely(
394 const device::BluetoothUUID& uuid,
395 const ConnectToServiceCallback& callback,
396 const ConnectToServiceErrorCallback& error_callback) = 0;
398 // Opens a new GATT connection to this device. On success, a new
399 // BluetoothGattConnection will be handed to the caller via |callback|. On
400 // error, |error_callback| will be called. The connection will be kept alive,
401 // as long as there is at least one active GATT connection. In the case that
402 // the underlying connection gets terminated, either due to a call to
403 // BluetoothDevice::Disconnect or other unexpected circumstances, the
404 // returned BluetoothGattConnection will be automatically marked as inactive.
405 // To monitor the state of the connection, observe the
406 // BluetoothAdapter::Observer::DeviceChanged method.
407 typedef base::Callback<void(scoped_ptr<BluetoothGattConnection>)>
408 GattConnectionCallback;
409 virtual void CreateGattConnection(
410 const GattConnectionCallback& callback,
411 const ConnectErrorCallback& error_callback) = 0;
413 // Returns the list of discovered GATT services.
414 virtual std::vector<BluetoothGattService*> GetGattServices() const;
416 // Returns the GATT service with device-specific identifier |identifier|.
417 // Returns NULL, if no such service exists.
418 virtual BluetoothGattService* GetGattService(
419 const std::string& identifier) const;
421 // Returns service data of a service given its UUID.
422 virtual base::BinaryValue* GetServiceData(BluetoothUUID serviceUUID) const;
424 // Returns the list UUIDs of services that have service data.
425 virtual UUIDList GetServiceDataUUIDs() const;
427 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where
428 // each 'X' is a hex digit. If the input |address| is invalid, returns an
429 // empty string.
430 static std::string CanonicalizeAddress(const std::string& address);
432 protected:
433 BluetoothDevice();
435 // Returns the internal name of the Bluetooth device, used by GetName().
436 virtual std::string GetDeviceName() const = 0;
438 // Clears the list of service data.
439 void ClearServiceData();
441 // Set the data of a given service designated by its UUID.
442 void SetServiceData(BluetoothUUID serviceUUID, const char* buffer,
443 size_t size);
445 // Mapping from the platform-specific GATT service identifiers to
446 // BluetoothGattService objects.
447 typedef std::map<std::string, BluetoothGattService*> GattServiceMap;
448 GattServiceMap gatt_services_;
450 // Mapping from service UUID represented as a std::string of a bluetooth
451 // service to
452 // the specific data. The data is stored as BinaryValue.
453 scoped_ptr<base::DictionaryValue> services_data_;
455 private:
456 // Returns a localized string containing the device's bluetooth address and
457 // a device type for display when |name_| is empty.
458 base::string16 GetAddressWithLocalizedDeviceTypeName() const;
461 } // namespace device
463 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_