Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chrome/browser/chromeos/bluetooth/bluetooth_device.h

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

Powered by Google App Engine
This is Rietveld 408576698