1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
#include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h"
#include "chrome/browser/chromeos/dbus/bluetooth_agent_service_provider.h"
#include "chrome/browser/chromeos/dbus/bluetooth_device_client.h"
#include "chrome/browser/chromeos/dbus/bluetooth_input_client.h"
#include "chrome/browser/chromeos/dbus/introspectable_client.h"
#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
#include "dbus/bus.h"
#include "dbus/object_path.h"
#include "grit/generated_resources.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "ui/base/l10n/l10n_util.h"
namespace chromeos {
BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter)
: weak_ptr_factory_(this),
adapter_(adapter),
bluetooth_class_(0),
bonded_(false),
connected_(false),
pairing_delegate_(NULL) {
}
BluetoothDevice::~BluetoothDevice() {
}
void BluetoothDevice::SetObjectPath(const dbus::ObjectPath& object_path) {
DCHECK(object_path_ == dbus::ObjectPath(""));
object_path_ = object_path;
}
void BluetoothDevice::Update(
const BluetoothDeviceClient::Properties* properties,
bool update_state) {
address_ = properties->address.value();
name_ = properties->name.value();
bluetooth_class_ = properties->bluetooth_class.value();
if (update_state) {
// BlueZ uses paired to mean link keys exchanged, whereas the Bluetooth
// spec refers to this as bonded. Use the spec name for our interface.
bonded_ = properties->paired.value();
connected_ = properties->connected.value();
}
}
string16 BluetoothDevice::GetName() const {
if (!name_.empty()) {
return UTF8ToUTF16(name_);
} else {
return GetAddressWithLocalizedDeviceTypeName();
}
}
BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const {
// https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
switch ((bluetooth_class_ & 0x1f00) >> 8) {
case 0x01:
// Computer major device class.
return DEVICE_COMPUTER;
case 0x02:
// Phone major device class.
switch ((bluetooth_class_ & 0xfc) >> 2) {
case 0x01:
case 0x02:
case 0x03:
// Cellular, cordless and smart phones.
return DEVICE_PHONE;
case 0x04:
case 0x05:
// Modems: wired or voice gateway and common ISDN access.
return DEVICE_MODEM;
}
break;
case 0x05:
// Peripheral major device class.
switch ((bluetooth_class_ & 0xc0) >> 6) {
case 0x00:
// "Not a keyboard or pointing device."
return DEVICE_PERIPHERAL;
case 0x01:
// Keyboard.
return DEVICE_KEYBOARD;
case 0x02:
// Pointing device.
switch ((bluetooth_class_ & 0x01e) >> 2) {
case 0x05:
// Digitizer tablet.
return DEVICE_TABLET;
default:
// Mouse.
return DEVICE_MOUSE;
}
break;
case 0x03:
// Combo device.
return DEVICE_KEYBOARD_MOUSE_COMBO;
}
break;
}
return DEVICE_UNKNOWN;
}
bool BluetoothDevice::IsSupported() const {
DeviceType device_type = GetDeviceType();
return (device_type == DEVICE_KEYBOARD ||
device_type == DEVICE_MOUSE ||
device_type == DEVICE_TABLET ||
device_type == DEVICE_KEYBOARD_MOUSE_COMBO);
}
string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const {
string16 address = UTF8ToUTF16(address_);
DeviceType device_type = GetDeviceType();
switch (device_type) {
case DEVICE_COMPUTER:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER,
address);
case DEVICE_PHONE:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE,
address);
case DEVICE_MODEM:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM,
address);
case DEVICE_KEYBOARD:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD,
address);
case DEVICE_MOUSE:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE,
address);
case DEVICE_TABLET:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET,
address);
case DEVICE_KEYBOARD_MOUSE_COMBO:
return l10n_util::GetStringFUTF16(
IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address);
default:
return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, address);
}
}
bool BluetoothDevice::IsConnected() const {
// TODO(keybuk): examine protocol-specific connected state, such as Input
return connected_;
}
void BluetoothDevice::Connect(PairingDelegate* pairing_delegate,
ErrorCallback error_callback) {
if (IsPaired() || IsBonded() || IsConnected()) {
// Connection to already paired or connected device.
ConnectApplications(error_callback);
} else if (!pairing_delegate) {
// No pairing delegate supplied, initiate low-security connection only.
DBusThreadManager::Get()->GetBluetoothAdapterClient()->
CreateDevice(adapter_->object_path_,
address_,
base::Bind(&BluetoothDevice::ConnectCallback,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
} else {
// Initiate high-security connection with pairing.
DCHECK(!pairing_delegate_);
pairing_delegate_ = pairing_delegate;
// The agent path is relatively meaningless, we use the device address
// to generate it as we only support one pairing attempt at a time for
// a given bluetooth device.
DCHECK(agent_.get() == NULL);
std::string agent_path_basename;
ReplaceChars(address_, ":", "_", &agent_path_basename);
dbus::ObjectPath agent_path("/org/chromium/bluetooth_agent/" +
agent_path_basename);
dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus();
agent_.reset(BluetoothAgentServiceProvider::Create(system_bus,
agent_path,
this));
DVLOG(1) << "Pairing: " << address_;
DBusThreadManager::Get()->GetBluetoothAdapterClient()->
CreatePairedDevice(adapter_->object_path_,
address_,
agent_path,
bluetooth_agent::kDisplayYesNoCapability,
base::Bind(&BluetoothDevice::ConnectCallback,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
}
}
void BluetoothDevice::ConnectCallback(ErrorCallback error_callback,
const dbus::ObjectPath& device_path,
bool success) {
if (success) {
DVLOG(1) << "Connection successful: " << device_path.value();
if (object_path_.value().empty()) {
object_path_ = device_path;
} else {
LOG_IF(WARNING, object_path_ != device_path)
<< "Conflicting device paths for objects, result gave: "
<< device_path.value() << " but signal gave: "
<< object_path_.value();
}
// Mark the device trusted so it can connect to us automatically, and
// we can connect after rebooting. This information is part of the
// pairing information of the device, and is unique to the combination
// of our bluetooth address and the device's bluetooth address. A
// different host needs a new pairing, so it's not useful to sync.
DBusThreadManager::Get()->GetBluetoothDeviceClient()->
GetProperties(object_path_)->trusted.Set(
true,
base::Bind(&BluetoothDevice::OnSetTrusted,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
// Connect application-layer protocols.
ConnectApplications(error_callback);
} else {
LOG(WARNING) << "Connection failed: " << address_;
error_callback.Run();
}
}
void BluetoothDevice::OnSetTrusted(ErrorCallback error_callback, bool success) {
if (!success) {
LOG(WARNING) << "Failed to set device as trusted: " << address_;
error_callback.Run();
}
}
void BluetoothDevice::ConnectApplications(ErrorCallback error_callback) {
// Introspect the device object to determine supported applications.
DBusThreadManager::Get()->GetIntrospectableClient()->
Introspect(bluetooth_device::kBluetoothDeviceServiceName,
object_path_,
base::Bind(&BluetoothDevice::OnIntrospect,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
}
void BluetoothDevice::OnIntrospect(ErrorCallback error_callback,
const std::string& service_name,
const dbus::ObjectPath& device_path,
const std::string& xml_data,
bool success) {
if (!success) {
LOG(WARNING) << "Failed to determine supported applications: " << address_;
error_callback.Run();
return;
}
// The introspection data for the device object may list one or more
// additional D-Bus interfaces that BlueZ supports for this particular
// device. Send appropraite Connect calls for each of those interfaces
// to connect all of the application protocols for this device.
std::vector<std::string> interfaces =
IntrospectableClient::GetInterfacesFromXmlData(xml_data);
for (std::vector<std::string>::iterator iter = interfaces.begin();
iter != interfaces.end(); ++iter) {
if (*iter == bluetooth_input::kBluetoothInputInterface) {
// Supports Input interface.
DBusThreadManager::Get()->GetBluetoothInputClient()->
Connect(object_path_,
base::Bind(&BluetoothDevice::OnConnect,
weak_ptr_factory_.GetWeakPtr(),
error_callback, *iter));
}
}
}
void BluetoothDevice::OnConnect(ErrorCallback error_callback,
const std::string& interface_name,
const dbus::ObjectPath& device_path,
bool success) {
if (success) {
DVLOG(1) << "Application connection successful: " << device_path.value()
<< ": " << interface_name;
} else {
LOG(WARNING) << "Connection failed: " << address_ << ": " << interface_name;
error_callback.Run();
}
}
void BluetoothDevice::SetPinCode(const std::string& pincode) {
if (!agent_.get() || pincode_callback_.is_null())
return;
pincode_callback_.Run(SUCCESS, pincode);
pincode_callback_.Reset();
}
void BluetoothDevice::SetPasskey(uint32 passkey) {
if (!agent_.get() || passkey_callback_.is_null())
return;
passkey_callback_.Run(SUCCESS, passkey);
passkey_callback_.Reset();
}
void BluetoothDevice::ConfirmPairing() {
if (!agent_.get() || confirmation_callback_.is_null())
return;
confirmation_callback_.Run(SUCCESS);
confirmation_callback_.Reset();
}
void BluetoothDevice::RejectPairing() {
if (!agent_.get())
return;
if (!pincode_callback_.is_null()) {
pincode_callback_.Run(REJECTED, "");
pincode_callback_.Reset();
}
if (!passkey_callback_.is_null()) {
passkey_callback_.Run(REJECTED, 0);
passkey_callback_.Reset();
}
if (!confirmation_callback_.is_null()) {
confirmation_callback_.Run(REJECTED);
confirmation_callback_.Reset();
}
}
void BluetoothDevice::CancelPairing() {
if (!agent_.get())
return;
if (!pincode_callback_.is_null()) {
pincode_callback_.Run(CANCELLED, "");
pincode_callback_.Reset();
}
if (!passkey_callback_.is_null()) {
passkey_callback_.Run(CANCELLED, 0);
passkey_callback_.Reset();
}
if (!confirmation_callback_.is_null()) {
confirmation_callback_.Run(CANCELLED);
confirmation_callback_.Reset();
}
}
void BluetoothDevice::Disconnect(ErrorCallback error_callback) {
DBusThreadManager::Get()->GetBluetoothDeviceClient()->
Disconnect(object_path_,
base::Bind(&BluetoothDevice::DisconnectCallback,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
}
void BluetoothDevice::DisconnectCallback(ErrorCallback error_callback,
const dbus::ObjectPath& device_path,
bool success) {
DCHECK(device_path == object_path_);
if (success) {
DVLOG(1) << "Disconnection successful: " << address_;
} else {
LOG(WARNING) << "Disconnection failed: " << address_;
error_callback.Run();
}
}
void BluetoothDevice::Forget(ErrorCallback error_callback) {
DBusThreadManager::Get()->GetBluetoothAdapterClient()->
RemoveDevice(adapter_->object_path_,
object_path_,
base::Bind(&BluetoothDevice::ForgetCallback,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
}
void BluetoothDevice::ForgetCallback(ErrorCallback error_callback,
const dbus::ObjectPath& adapter_path,
bool success) {
// It's quite normal that this path never gets called on success; we use a
// weak pointer, and bluetoothd might send the DeviceRemoved signal before
// the method reply, in which case this object is deleted and the
// callback never takes place. Therefore don't do anything here for the
// success case.
if (!success) {
LOG(WARNING) << "Forget failed: " << address_;
error_callback.Run();
}
}
void BluetoothDevice::DisconnectRequested(const dbus::ObjectPath& object_path) {
DCHECK(object_path == object_path_);
}
void BluetoothDevice::Release() {
DCHECK(agent_.get());
DVLOG(1) << "Release: " << address_;
DCHECK(pairing_delegate_);
pairing_delegate_->DismissDisplayOrConfirm();
pairing_delegate_ = NULL;
pincode_callback_.Reset();
passkey_callback_.Reset();
confirmation_callback_.Reset();
agent_.reset();
}
void BluetoothDevice::RequestPinCode(const dbus::ObjectPath& device_path,
const PinCodeCallback& callback) {
DCHECK(agent_.get());
DVLOG(1) << "RequestPinCode: " << device_path.value();
DCHECK(pairing_delegate_);
DCHECK(pincode_callback_.is_null());
pincode_callback_ = callback;
pairing_delegate_->RequestPinCode(this);
}
void BluetoothDevice::RequestPasskey(const dbus::ObjectPath& device_path,
const PasskeyCallback& callback) {
DCHECK(agent_.get());
DCHECK(device_path == object_path_);
DVLOG(1) << "RequestPasskey: " << device_path.value();
DCHECK(pairing_delegate_);
DCHECK(passkey_callback_.is_null());
passkey_callback_ = callback;
pairing_delegate_->RequestPasskey(this);
}
void BluetoothDevice::DisplayPinCode(const dbus::ObjectPath& device_path,
const std::string& pincode) {
DCHECK(agent_.get());
DCHECK(device_path == object_path_);
DVLOG(1) << "DisplayPinCode: " << device_path.value() << " " << pincode;
DCHECK(pairing_delegate_);
pairing_delegate_->DisplayPinCode(this, pincode);
}
void BluetoothDevice::DisplayPasskey(const dbus::ObjectPath& device_path,
uint32 passkey) {
DCHECK(agent_.get());
DCHECK(device_path == object_path_);
DVLOG(1) << "DisplayPasskey: " << device_path.value() << " " << passkey;
DCHECK(pairing_delegate_);
pairing_delegate_->DisplayPasskey(this, passkey);
}
void BluetoothDevice::RequestConfirmation(
const dbus::ObjectPath& device_path,
uint32 passkey,
const ConfirmationCallback& callback) {
DCHECK(agent_.get());
DCHECK(device_path == object_path_);
DVLOG(1) << "RequestConfirmation: " << device_path.value() << " " << passkey;
DCHECK(pairing_delegate_);
DCHECK(confirmation_callback_.is_null());
confirmation_callback_ = callback;
pairing_delegate_->ConfirmPasskey(this, passkey);
}
void BluetoothDevice::Authorize(const dbus::ObjectPath& device_path,
const std::string& uuid,
const ConfirmationCallback& callback) {
DCHECK(agent_.get());
DCHECK(device_path == object_path_);
LOG(WARNING) << "Rejected authorization for service: " << uuid
<< " requested from device: " << device_path.value();
callback.Run(REJECTED);
}
void BluetoothDevice::ConfirmModeChange(Mode mode,
const ConfirmationCallback& callback) {
DCHECK(agent_.get());
LOG(WARNING) << "Rejected adapter-level mode change: " << mode
<< " made on agent for device: " << address_;
callback.Run(REJECTED);
}
void BluetoothDevice::Cancel() {
DCHECK(agent_.get());
DVLOG(1) << "Cancel: " << address_;
DCHECK(pairing_delegate_);
pairing_delegate_->DismissDisplayOrConfirm();
}
// static
BluetoothDevice* BluetoothDevice::CreateBound(
BluetoothAdapter* adapter,
const dbus::ObjectPath& object_path,
const BluetoothDeviceClient::Properties* properties) {
BluetoothDevice* device = new BluetoothDevice(adapter);
device->SetObjectPath(object_path);
device->Update(properties, true);
return device;
}
// static
BluetoothDevice* BluetoothDevice::CreateUnbound(
BluetoothAdapter* adapter,
const BluetoothDeviceClient::Properties* properties) {
BluetoothDevice* device = new BluetoothDevice(adapter);
device->Update(properties, false);
return device;
}
} // namespace chromeos
|