blob: 90e420b2f05218a8050de6b511c0e06f357c2b2f [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>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/string16.h"
13#include "base/memory/ref_counted.h"
14
15namespace device {
16
17class BluetoothServiceRecord;
18class 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,
28// instead use the address() method as a unique key for a device.
29//
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:
35 // Possible values that may be returned by GetDeviceType(), representing
36 // different types of bluetooth device that we support or are aware of
37 // decoded from the bluetooth class information.
38 enum DeviceType {
39 DEVICE_UNKNOWN,
40 DEVICE_COMPUTER,
41 DEVICE_PHONE,
42 DEVICE_MODEM,
43 DEVICE_AUDIO,
44 DEVICE_CAR_AUDIO,
45 DEVICE_VIDEO,
46 DEVICE_PERIPHERAL,
47 DEVICE_JOYSTICK,
48 DEVICE_GAMEPAD,
49 DEVICE_KEYBOARD,
50 DEVICE_MOUSE,
51 DEVICE_TABLET,
52 DEVICE_KEYBOARD_MOUSE_COMBO
53 };
54
55 // Interface for observing changes from bluetooth devices.
56 class Observer {
57 public:
58 virtual ~Observer() {}
59
60 // TODO(keybuk): add observers for pairing and connection.
61 };
62
63 // Interface for negotiating pairing of bluetooth devices.
64 class PairingDelegate {
65 public:
66 virtual ~PairingDelegate() {}
67
68 // This method will be called when the Bluetooth daemon requires a
69 // PIN Code for authentication of the device |device|, the delegate should
70 // obtain the code from the user and call SetPinCode() on the device to
71 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
72 // the request.
73 //
74 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
75 // for which there is no automatic pairing or special handling.
76 virtual void RequestPinCode(BluetoothDevice* device) = 0;
77
78 // This method will be called when the Bluetooth daemon requires a
79 // Passkey for authentication of the device |device|, the delegate should
80 // obtain the passkey from the user (a numeric in the range 0-999999) and
81 // call SetPasskey() on the device to provide it, or RejectPairing() or
82 // CancelPairing() to reject or cancel the request.
83 //
84 // Passkeys are generally required for Bluetooth 2.1 and later devices
85 // which cannot provide input or display on their own, and don't accept
86 // passkey-less pairing.
87 virtual void RequestPasskey(BluetoothDevice* device) = 0;
88
89 // This method will be called when the Bluetooth daemon requires that the
90 // user enter the PIN code |pincode| into the device |device| so that it
91 // may be authenticated. The DismissDisplayOrConfirm() method
92 // will be called to dismiss the display once pairing is complete or
93 // cancelled.
94 //
95 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
96 // |pincode| will always be a six-digit numeric in the range 000000-999999
97 // for compatibilty with later specifications.
98 virtual void DisplayPinCode(BluetoothDevice* device,
99 const std::string& pincode) = 0;
100
101 // This method will be called when the Bluetooth daemon requires that the
102 // user enter the Passkey |passkey| into the device |device| so that it
103 // may be authenticated. The DismissDisplayOrConfirm() method will be
104 // called to dismiss the display once pairing is complete or cancelled.
105 //
106 // This is used for Bluetooth 2.1 and later devices that support input
107 // but not display, such as keyboards. The Passkey is a numeric in the
108 // range 0-999999 and should be always presented zero-padded to six
109 // digits.
110 virtual void DisplayPasskey(BluetoothDevice* device,
111 uint32 passkey) = 0;
112
113 // This method will be called when the Bluetooth daemon requires that the
114 // user confirm that the Passkey |passkey| is displayed on the screen
115 // of the device |device| so that it may be authenticated. The delegate
116 // should display to the user and ask for confirmation, then call
117 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
118 // to reject or CancelPairing() on the device to cancel authentication
119 // for any other reason.
120 //
121 // This is used for Bluetooth 2.1 and later devices that support display,
122 // such as other computers or phones. The Passkey is a numeric in the
123 // range 0-999999 and should be always present zero-padded to six
124 // digits.
125 virtual void ConfirmPasskey(BluetoothDevice* device,
126 uint32 passkey) = 0;
127
128 // This method will be called when any previous DisplayPinCode(),
129 // DisplayPasskey() or ConfirmPasskey() request should be concluded
130 // and removed from the user.
131 virtual void DismissDisplayOrConfirm() = 0;
132 };
133
134 virtual ~BluetoothDevice();
135
136 // Returns the Bluetooth of address the device. This should be used as
137 // a unique key to identify the device and copied where needed.
138 virtual const std::string& address() const;
139
140 // Returns the name of the device suitable for displaying, this may
141 // be a synthesied string containing the address and localized type name
142 // if the device has no obtained name.
143 virtual string16 GetName() const;
144
145 // Returns the type of the device, limited to those we support or are
146 // aware of, by decoding the bluetooth class information. The returned
147 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
148 // DEVICE_PERIPHERAL.
149 DeviceType GetDeviceType() const;
150
151 // Indicates whether the device is paired to the adapter, whether or not
152 // that pairing is permanent or temporary.
153 virtual bool IsPaired() const = 0;
154
155 // Indicates whether the device is visible to the adapter, this is not
156 // mutually exclusive to being paired.
157 virtual bool IsVisible() const;
158
159 // Indicates whether the device is bonded to the adapter, bonding is
160 // formed by pairing and exchanging high-security link keys so that
161 // connections may be encrypted.
162 virtual bool IsBonded() const;
163
164 // Indicates whether the device is currently connected to the adapter
165 // and at least one service available for use.
166 virtual bool IsConnected() const;
167
168 // Returns the services (as UUID strings) that this device provides.
169 typedef std::vector<std::string> ServiceList;
170 virtual const ServiceList& GetServices() const = 0;
171
172 // The ErrorCallback is used for methods that can fail in which case it
173 // is called, in the success case the callback is simply not called.
174 typedef base::Callback<void()> ErrorCallback;
175
176 // Returns the services (as BluetoothServiceRecord objects) that this device
177 // provides.
178 typedef ScopedVector<BluetoothServiceRecord> ServiceRecordList;
179 typedef base::Callback<void(const ServiceRecordList&)> ServiceRecordsCallback;
180 virtual void GetServiceRecords(const ServiceRecordsCallback& callback,
181 const ErrorCallback& error_callback) = 0;
182
183 // Indicates whether this device provides the given service. |uuid| should
184 // be in canonical form (see utils::CanonicalUuid).
185 virtual bool ProvidesServiceWithUUID(const std::string& uuid) const = 0;
186
187 // The ProvidesServiceCallback is used by ProvidesServiceWithName to indicate
188 // whether or not a matching service was found.
189 typedef base::Callback<void(bool)> ProvidesServiceCallback;
190
191 // Indicates whether this device provides the given service.
192 virtual void ProvidesServiceWithName(
193 const std::string& name,
194 const ProvidesServiceCallback& callback) = 0;
195
196 // Indicates whether the device is currently pairing and expecting a
197 // PIN Code to be returned.
198 virtual bool ExpectingPinCode() const = 0;
199
200 // Indicates whether the device is currently pairing and expecting a
201 // Passkey to be returned.
202 virtual bool ExpectingPasskey() const = 0;
203
204 // Indicates whether the device is currently pairing and expecting
205 // confirmation of a displayed passkey.
206 virtual bool ExpectingConfirmation() const = 0;
207
208 // SocketCallback is used by ConnectToService to return a BluetoothSocket to
209 // the caller, or NULL if there was an error. The socket will remain open
210 // until the last reference to the returned BluetoothSocket is released.
211 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
212 SocketCallback;
213
214 // Initiates a connection to the device, pairing first if necessary.
215 //
216 // Method calls will be made on the supplied object |pairing_delegate|
217 // to indicate what display, and in response should make method calls
218 // back to the device object. Not all devices require user responses
219 // during pairing, so it is normal for |pairing_delegate| to receive no
220 // calls. To explicitly force a low-security connection without bonding,
221 // pass NULL, though this is ignored if the device is already paired.
222 //
223 // If the request fails, |error_callback| will be called; otherwise,
224 // |callback| is called when the request is complete.
225 virtual void Connect(PairingDelegate* pairing_delegate,
226 const base::Closure& callback,
227 const ErrorCallback& error_callback) = 0;
228
229 // Sends the PIN code |pincode| to the remote device during pairing.
230 //
231 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
232 // for which there is no automatic pairing or special handling.
233 virtual void SetPinCode(const std::string& pincode) = 0;
234
235 // Sends the Passkey |passkey| to the remote device during pairing.
236 //
237 // Passkeys are generally required for Bluetooth 2.1 and later devices
238 // which cannot provide input or display on their own, and don't accept
239 // passkey-less pairing, and are a numeric in the range 0-999999.
240 virtual void SetPasskey(uint32 passkey) = 0;
241
242 // Confirms to the remote device during pairing that a passkey provided by
243 // the ConfirmPasskey() delegate call is displayed on both devices.
244 virtual void ConfirmPairing() = 0;
245
246 // Rejects a pairing or connection request from a remote device.
247 virtual void RejectPairing() = 0;
248
249 // Cancels a pairing or connection attempt to a remote device.
250 virtual void CancelPairing() = 0;
251
252 // Disconnects the device, terminating the low-level ACL connection
253 // and any application connections using it. Link keys and other pairing
254 // information are not discarded, and the device object is not deleted.
255 // If the request fails, |error_callback| will be called; otherwise,
256 // |callback| is called when the request is complete.
257 virtual void Disconnect(const base::Closure& callback,
258 const ErrorCallback& error_callback) = 0;
259
260 // Disconnects the device, terminating the low-level ACL connection
261 // and any application connections using it, and then discards link keys
262 // and other pairing information. The device object remainds valid until
263 // returing from the calling function, after which it should be assumed to
264 // have been deleted. If the request fails, |error_callback| will be called.
265 // There is no callback for success beause this object is often deleted
266 // before that callback would be called.
267 virtual void Forget(const ErrorCallback& error_callback) = 0;
268
269 // Attempts to open a socket to a service matching |uuid| on this device. If
270 // the connection is successful, |callback| is called with a BluetoothSocket.
271 // Otherwise |callback| is called with NULL. The socket is closed as soon as
272 // all references to the BluetoothSocket are released. Note that the
273 // BluetoothSocket object can outlive both this BluetoothDevice and the
274 // BluetoothAdapter for this device.
275 virtual void ConnectToService(const std::string& service_uuid,
276 const SocketCallback& callback) = 0;
277
278 // Sets the Out Of Band pairing data for this device to |data|. Exactly one
279 // of |callback| or |error_callback| will be run.
280 virtual void SetOutOfBandPairingData(
281 const BluetoothOutOfBandPairingData& data,
282 const base::Closure& callback,
283 const ErrorCallback& error_callback) = 0;
284
285 // Clears the Out Of Band pairing data for this device. Exactly one of
286 // |callback| or |error_callback| will be run.
287 virtual void ClearOutOfBandPairingData(
288 const base::Closure& callback,
289 const ErrorCallback& error_callback) = 0;
290
291 protected:
292 BluetoothDevice();
293
294 // The Bluetooth class of the device, a bitmask that may be decoded using
295 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
296 uint32 bluetooth_class_;
297
298 // The name of the device, as supplied by the remote device.
299 std::string name_;
300
301 // The Bluetooth address of the device.
302 std::string address_;
303
304 // Tracked device state, updated by the adapter managing the lifecyle of
305 // the device.
306 bool visible_;
307 bool bonded_;
308 bool connected_;
309
310 private:
311 // Returns a localized string containing the device's bluetooth address and
312 // a device type for display when |name_| is empty.
313 string16 GetAddressWithLocalizedDeviceTypeName() const;
314};
315
316} // namespace device
317
318#endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_