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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
// 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/dbus/bluetooth_adapter_client.h"
#include <map>
#include "base/bind.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h"
#include "chrome/browser/chromeos/system/runtime_environment.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
// Utility function to convert an array of dbus dict_entry objects into a
// DictionaryValue object.
//
// The dict_entry objects must have keys that are strings and values that are
// simple variants.
//
// When converting integral types, we use Integer Value objects to represent
// uint8, int16, uint16, int32, and uint32 values and Double Value objects to
// represent int64 and uint64 values.
//
// We intend to move this to the chrome dbus library's MessageReader class when
// it's more fully baked.
//
// TODO(vlaviano):
// - Can we handle integral types better?
// - Add support for nested complex types.
// - Write an equivalent function to convert in the opposite direction.
// - Write unit tests.
bool PopArrayOfDictEntries(dbus::MessageReader* reader,
dbus::Message* message,
DictionaryValue* dictionary) {
DCHECK(reader);
DCHECK(message);
DCHECK(dictionary);
dbus::MessageReader array_reader(message);
if (!reader->PopArray(&array_reader)) {
return false;
}
while (array_reader.HasMoreData()) {
dbus::MessageReader dict_entry_reader(message);
if (!array_reader.PopDictEntry(&dict_entry_reader)) {
return false;
}
std::string key;
if (!dict_entry_reader.PopString(&key)) {
return false;
}
dbus::MessageReader variant_reader(message);
if (!dict_entry_reader.PopVariant(&variant_reader)) {
return false;
}
const dbus::Message::DataType type = variant_reader.GetDataType();
switch (type) {
case dbus::Message::BYTE: {
uint8 value = 0;
if (!variant_reader.PopByte(&value)) {
return false;
}
dictionary->SetInteger(key, value);
break;
}
case dbus::Message::BOOL: {
bool value = false;
if (!variant_reader.PopBool(&value)) {
return false;
}
dictionary->SetBoolean(key, value);
break;
}
case dbus::Message::INT16: {
int16 value = 0;
if (!variant_reader.PopInt16(&value)) {
return false;
}
dictionary->SetInteger(key, value);
break;
}
case dbus::Message::UINT16: {
uint16 value = 0;
if (!variant_reader.PopUint16(&value)) {
return false;
}
dictionary->SetInteger(key, value);
break;
}
case dbus::Message::INT32: {
int32 value = 0;
if (!variant_reader.PopInt32(&value)) {
return false;
}
dictionary->SetInteger(key, value);
break;
}
case dbus::Message::UINT32: {
uint32 value = 0;
if (!variant_reader.PopUint32(&value)) {
return false;
}
dictionary->SetInteger(key, value);
break;
}
case dbus::Message::INT64: {
int64 value = 0;
if (!variant_reader.PopInt64(&value)) {
return false;
}
dictionary->SetDouble(key, value);
break;
}
case dbus::Message::UINT64: {
uint64 value = 0;
if (!variant_reader.PopUint64(&value)) {
return false;
}
dictionary->SetDouble(key, value);
break;
}
case dbus::Message::DOUBLE: {
double value = 0;
if (!variant_reader.PopDouble(&value)) {
return false;
}
dictionary->SetDouble(key, value);
break;
}
case dbus::Message::STRING: {
std::string value;
if (!variant_reader.PopString(&value)) {
return false;
}
dictionary->SetString(key, value);
break;
}
case dbus::Message::OBJECT_PATH: {
dbus::ObjectPath value;
if (!variant_reader.PopObjectPath(&value)) {
return false;
}
dictionary->SetString(key, value.value());
break;
}
case dbus::Message::ARRAY: {
// Not yet supported.
return false;
}
case dbus::Message::STRUCT: {
// Not yet supported.
return false;
}
case dbus::Message::DICT_ENTRY: {
// Not yet supported.
return false;
}
case dbus::Message::VARIANT: {
// Not yet supported.
return false;
}
default:
LOG(FATAL) << "Unknown type: " << type;
}
}
return true;
}
} // namespace
namespace chromeos {
// The BluetoothAdapterClient implementation used in production.
class BluetoothAdapterClientImpl: public BluetoothAdapterClient,
private BluetoothManagerClient::Observer {
public:
explicit BluetoothAdapterClientImpl(dbus::Bus* bus,
BluetoothManagerClient* manager_client)
: weak_ptr_factory_(this),
bus_(bus) {
VLOG(1) << "Creating BluetoothAdapterClientImpl";
DCHECK(manager_client);
manager_client->AddObserver(this);
}
virtual ~BluetoothAdapterClientImpl() {
}
// BluetoothAdapterClient override.
virtual void AddObserver(BluetoothAdapterClient::Observer* observer) {
VLOG(1) << "AddObserver";
DCHECK(observer);
observers_.AddObserver(observer);
}
// BluetoothAdapterClient override.
virtual void RemoveObserver(BluetoothAdapterClient::Observer* observer) {
VLOG(1) << "RemoveObserver";
DCHECK(observer);
observers_.RemoveObserver(observer);
}
// BluetoothAdapterClient override.
virtual void StartDiscovery(const dbus::ObjectPath& object_path) {
VLOG(1) << "StartDiscovery: " << object_path.value();
dbus::MethodCall method_call(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kStartDiscovery);
dbus::ObjectProxy* adapter_proxy = GetObjectProxyForPath(object_path);
adapter_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothAdapterClientImpl::OnStartDiscovery,
weak_ptr_factory_.GetWeakPtr(), object_path));
}
// BluetoothAdapterClient override.
virtual void StopDiscovery(const dbus::ObjectPath& object_path) {
VLOG(1) << "StopDiscovery: " << object_path.value();
dbus::MethodCall method_call(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kStopDiscovery);
dbus::ObjectProxy* adapter_proxy = GetObjectProxyForPath(object_path);
adapter_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothAdapterClientImpl::OnStopDiscovery,
weak_ptr_factory_.GetWeakPtr(), object_path));
}
private:
// BluetoothManagerClient::Observer override.
virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "AdapterAdded: " << object_path.value();
}
// BluetoothManagerClient::Observer override.
virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "AdapterRemoved: " << object_path.value();
RemoveObjectProxyForPath(object_path);
}
// Ensures that we have a dbus object proxy for an adapter with dbus
// object path |object_path|, and if not, creates it and stores it in
// our |proxy_map_| map.
dbus::ObjectProxy* GetObjectProxyForPath(
const dbus::ObjectPath& object_path) {
VLOG(1) << "GetObjectProxyForPath: " << object_path.value();
ProxyMap::iterator it = proxy_map_.find(object_path);
if (it != proxy_map_.end())
return it->second;
DCHECK(bus_);
dbus::ObjectProxy* adapter_proxy = bus_->GetObjectProxy(
bluetooth_adapter::kBluetoothAdapterServiceName, object_path);
proxy_map_[object_path] = adapter_proxy;
adapter_proxy->ConnectToSignal(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kDeviceCreatedSignal,
base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
adapter_proxy->ConnectToSignal(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kDeviceRemovedSignal,
base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
adapter_proxy->ConnectToSignal(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kPropertyChangedSignal,
base::Bind(&BluetoothAdapterClientImpl::PropertyChangedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothAdapterClientImpl::PropertyChangedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
adapter_proxy->ConnectToSignal(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kDeviceFoundSignal,
base::Bind(&BluetoothAdapterClientImpl::DeviceFoundReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothAdapterClientImpl::DeviceFoundConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
adapter_proxy->ConnectToSignal(
bluetooth_adapter::kBluetoothAdapterInterface,
bluetooth_adapter::kDeviceDisappearedSignal,
base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
return adapter_proxy;
}
// Removes the dbus object proxy for the adapter with dbus object path
// |object_path| from our |proxy_map_| map.
void RemoveObjectProxyForPath(const dbus::ObjectPath& object_path) {
VLOG(1) << "RemoveObjectProxyForPath: " << object_path.value();
proxy_map_.erase(object_path);
}
// Called by dbus:: when a DeviceCreated signal is received.
void DeviceCreatedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
dbus::ObjectPath device_path;
if (!reader.PopObjectPath(&device_path)) {
LOG(ERROR) << object_path.value()
<< ": DeviceCreated signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": Device created: "
<< device_path.value();
FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
DeviceCreated(object_path, device_path));
}
// Called by dbus:: when the DeviceCreated signal is initially connected.
void DeviceCreatedConnected(const dbus::ObjectPath& object_path,
const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success) << object_path.value()
<< ": Failed to connect to DeviceCreated signal.";
}
// Called by dbus:: when a DeviceRemoved signal is received.
void DeviceRemovedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
dbus::ObjectPath device_path;
if (!reader.PopObjectPath(&device_path)) {
LOG(ERROR) << object_path.value()
<< ": DeviceRemoved signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": Device removed: "
<< device_path.value();
FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
DeviceRemoved(object_path, device_path));
}
// Called by dbus:: when the DeviceRemoved signal is initially connected.
void DeviceRemovedConnected(const dbus::ObjectPath& object_path,
const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success) << object_path.value()
<< ": Failed to connect to DeviceRemoved signal.";
}
// Called by dbus:: when a PropertyChanged signal is received.
void PropertyChangedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
std::string property_name;
if (!reader.PopString(&property_name)) {
LOG(ERROR) << object_path.value()
<< ": PropertyChanged signal has incorrect parameters: "
<< signal->ToString();
return;
}
if (property_name != bluetooth_adapter::kDiscoveringProperty) {
VLOG(1) << object_path.value() << ": PropertyChanged: " << property_name;
// We don't care.
return;
}
bool discovering = false;
if (!reader.PopVariantOfBool(&discovering)) {
LOG(ERROR) << object_path.value()
<< ": PropertyChanged signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": PropertyChanged: Discovering = "
<< discovering;
FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
DiscoveringPropertyChanged(object_path, discovering));
}
// Called by dbus:: when the PropertyChanged signal is initially connected.
void PropertyChangedConnected(const dbus::ObjectPath& object_path,
const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success)
<< object_path.value()
<< ": Failed to connect to PropertyChanged signal.";
}
// Called by dbus:: when a DeviceFound signal is received.
void DeviceFoundReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
std::string address;
if (!reader.PopString(&address)) {
LOG(ERROR) << object_path.value()
<< ": DeviceFound signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": Device found: " << address;
DictionaryValue device_properties;
if (!PopArrayOfDictEntries(&reader, signal, &device_properties)) {
LOG(ERROR) << object_path.value()
<< ": DeviceFound signal has incorrect parameters: "
<< signal->ToString();
return;
}
FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
DeviceFound(object_path, address, device_properties));
}
// Called by dbus:: when the DeviceFound signal is initially connected.
void DeviceFoundConnected(const dbus::ObjectPath& object_path,
const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success) << object_path.value()
<< ": Failed to connect to DeviceFound signal.";
}
// Called by dbus:: when a DeviceDisappeared signal is received.
void DeviceDisappearedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
std::string address;
if (!reader.PopString(&address)) {
LOG(ERROR) << object_path.value()
<< ": DeviceDisappeared signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": Device disappeared: " << address;
FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
DeviceDisappeared(object_path, address));
}
// Called by dbus:: when the DeviceDisappeared signal is initially connected.
void DeviceDisappearedConnected(const dbus::ObjectPath& object_path,
const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success)
<< object_path.value()
<< ": Failed to connect to DeviceDisappeared signal.";
}
// Called when a response for StartDiscovery() is received.
void OnStartDiscovery(const dbus::ObjectPath& object_path,
dbus::Response* response) {
VLOG(1) << "OnStartDiscovery: " << object_path.value();
LOG_IF(WARNING, !response) << object_path.value()
<< ": OnStartDiscovery: failed.";
}
// Called when a response for StopDiscovery() is received.
void OnStopDiscovery(const dbus::ObjectPath& object_path,
dbus::Response* response) {
VLOG(1) << "OnStopDiscovery: " << object_path.value();
LOG_IF(WARNING, !response) << object_path.value()
<< ": OnStopDiscovery: failed.";
}
// Weak pointer factory for generating 'this' pointers that might live longer
// than we do.
base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_;
dbus::Bus* bus_;
// We maintain a collection of dbus object proxies, one for each adapter.
typedef std::map<const dbus::ObjectPath, dbus::ObjectProxy*> ProxyMap;
ProxyMap proxy_map_;
// List of observers interested in event notifications from us.
ObserverList<BluetoothAdapterClient::Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl);
};
// The BluetoothAdapterClient implementation used on Linux desktop, which does
// nothing.
class BluetoothAdapterClientStubImpl : public BluetoothAdapterClient {
public:
// BluetoothAdapterClient override.
virtual void AddObserver(Observer* observer) {
VLOG(1) << "AddObserver";
}
// BluetoothAdapterClient override.
virtual void RemoveObserver(Observer* observer) {
VLOG(1) << "RemoveObserver";
}
// BluetoothAdapterClient override.
virtual void StartDiscovery(const dbus::ObjectPath& object_path) {
VLOG(1) << "StartDiscovery: " << object_path.value();
}
// BluetoothAdapterClient override.
virtual void StopDiscovery(const dbus::ObjectPath& object_path) {
VLOG(1) << "StopDiscovery: " << object_path.value();
}
};
BluetoothAdapterClient::BluetoothAdapterClient() {
}
BluetoothAdapterClient::~BluetoothAdapterClient() {
}
BluetoothAdapterClient* BluetoothAdapterClient::Create(
dbus::Bus* bus,
BluetoothManagerClient* manager_client) {
if (system::runtime_environment::IsRunningOnChromeOS()) {
return new BluetoothAdapterClientImpl(bus, manager_client);
} else {
return new BluetoothAdapterClientStubImpl();
}
}
} // namespace chromeos
|