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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
// 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 "chromeos/dbus/bluetooth_device_client.h"
#include <map>
#include <utility>
#include "base/bind.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "chromeos/dbus/bluetooth_adapter_client.h"
#include "chromeos/dbus/bluetooth_property.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 chromeos {
BluetoothDeviceClient::Properties::Properties(
dbus::ObjectProxy* object_proxy,
const PropertyChangedCallback& callback)
: BluetoothPropertySet(object_proxy,
bluetooth_device::kBluetoothDeviceInterface,
callback) {
RegisterProperty(bluetooth_device::kAddressProperty, &address);
RegisterProperty(bluetooth_device::kNameProperty, &name);
RegisterProperty(bluetooth_device::kVendorProperty, &vendor);
RegisterProperty(bluetooth_device::kProductProperty, &product);
RegisterProperty(bluetooth_device::kVersionProperty, &version);
RegisterProperty(bluetooth_device::kIconProperty, &icon);
RegisterProperty(bluetooth_device::kClassProperty, &bluetooth_class);
RegisterProperty(bluetooth_device::kUUIDsProperty, &uuids);
RegisterProperty(bluetooth_device::kServicesProperty, &services);
RegisterProperty(bluetooth_device::kPairedProperty, &paired);
RegisterProperty(bluetooth_device::kConnectedProperty, &connected);
RegisterProperty(bluetooth_device::kTrustedProperty, &trusted);
RegisterProperty(bluetooth_device::kBlockedProperty, &blocked);
RegisterProperty(bluetooth_device::kAliasProperty, &alias);
RegisterProperty(bluetooth_device::kNodesProperty, &nodes);
RegisterProperty(bluetooth_device::kAdapterProperty, &adapter);
RegisterProperty(bluetooth_device::kLegacyPairingProperty, &legacy_pairing);
}
BluetoothDeviceClient::Properties::~Properties() {
}
// The BluetoothDeviceClient implementation used in production.
class BluetoothDeviceClientImpl: public BluetoothDeviceClient,
private BluetoothAdapterClient::Observer {
public:
BluetoothDeviceClientImpl(dbus::Bus* bus,
BluetoothAdapterClient* adapter_client)
: bus_(bus),
weak_ptr_factory_(this) {
VLOG(1) << "Creating BluetoothDeviceClientImpl";
DCHECK(adapter_client);
adapter_client->AddObserver(this);
}
virtual ~BluetoothDeviceClientImpl() {
// Clean up Properties structures
for (ObjectMap::iterator iter = object_map_.begin();
iter != object_map_.end(); ++iter) {
Object object = iter->second;
Properties* properties = object.second;
delete properties;
}
}
// BluetoothDeviceClient override.
virtual void AddObserver(BluetoothDeviceClient::Observer* observer)
OVERRIDE {
DCHECK(observer);
observers_.AddObserver(observer);
}
// BluetoothDeviceClient override.
virtual void RemoveObserver(BluetoothDeviceClient::Observer* observer)
OVERRIDE {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
// BluetoothDeviceClient override.
virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
OVERRIDE {
return GetObject(object_path).second;
}
// BluetoothDeviceClient override.
virtual void DiscoverServices(const dbus::ObjectPath& object_path,
const std::string& pattern,
const ServicesCallback& callback) OVERRIDE {
dbus::MethodCall method_call(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kDiscoverServices);
dbus::MessageWriter writer(&method_call);
writer.AppendString(pattern);
dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
object_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothDeviceClientImpl::OnDiscoverServices,
weak_ptr_factory_.GetWeakPtr(), object_path, callback));
}
// BluetoothDeviceClient override.
virtual void CancelDiscovery(const dbus::ObjectPath& object_path,
const DeviceCallback& callback) OVERRIDE {
dbus::MethodCall method_call(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kCancelDiscovery);
dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
object_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothDeviceClientImpl::OnCancelDiscovery,
weak_ptr_factory_.GetWeakPtr(), object_path, callback));
}
// BluetoothDeviceClient override.
virtual void Disconnect(const dbus::ObjectPath& object_path,
const DeviceCallback& callback) OVERRIDE {
dbus::MethodCall method_call(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kDisconnect);
dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
object_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothDeviceClientImpl::OnDisconnect,
weak_ptr_factory_.GetWeakPtr(), object_path, callback));
}
// BluetoothDeviceClient override.
virtual void CreateNode(const dbus::ObjectPath& object_path,
const std::string& uuid,
const NodeCallback& callback) OVERRIDE {
dbus::MethodCall method_call(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kCreateNode);
dbus::MessageWriter writer(&method_call);
writer.AppendString(uuid);
dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
object_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothDeviceClientImpl::OnCreateNode,
weak_ptr_factory_.GetWeakPtr(), object_path, callback));
}
// BluetoothDeviceClient override.
virtual void RemoveNode(const dbus::ObjectPath& object_path,
const dbus::ObjectPath& node_path,
const DeviceCallback& callback) OVERRIDE {
dbus::MethodCall method_call(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kRemoveNode);
dbus::MessageWriter writer(&method_call);
writer.AppendObjectPath(node_path);
dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
object_proxy->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothDeviceClientImpl::OnRemoveNode,
weak_ptr_factory_.GetWeakPtr(), object_path, callback));
}
private:
// We maintain a collection of dbus object proxies and properties structures
// for each device.
typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
ObjectMap object_map_;
// BluetoothAdapterClient::Observer override.
virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
const dbus::ObjectPath& object_path) OVERRIDE {
}
// BluetoothAdapterClient::Observer override.
virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
const dbus::ObjectPath& object_path) OVERRIDE {
RemoveObject(object_path);
}
// Ensures that we have an object proxy and properties structure for
// a device with object path |object_path|, creating it if not and
// storing it in our |object_map_| map.
Object GetObject(const dbus::ObjectPath& object_path) {
ObjectMap::iterator iter = object_map_.find(object_path);
if (iter != object_map_.end())
return iter->second;
// Create the object proxy.
DCHECK(bus_);
dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
bluetooth_device::kBluetoothDeviceServiceName, object_path);
object_proxy->ConnectToSignal(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kDisconnectRequestedSignal,
base::Bind(&BluetoothDeviceClientImpl::DisconnectRequestedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothDeviceClientImpl::DisconnectRequestedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
object_proxy->ConnectToSignal(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kNodeCreatedSignal,
base::Bind(&BluetoothDeviceClientImpl::NodeCreatedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothDeviceClientImpl::NodeCreatedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
object_proxy->ConnectToSignal(
bluetooth_device::kBluetoothDeviceInterface,
bluetooth_device::kNodeRemovedSignal,
base::Bind(&BluetoothDeviceClientImpl::NodeRemovedReceived,
weak_ptr_factory_.GetWeakPtr(), object_path),
base::Bind(&BluetoothDeviceClientImpl::NodeRemovedConnected,
weak_ptr_factory_.GetWeakPtr(), object_path));
// Create the properties structure.
Properties* properties = new Properties(
object_proxy,
base::Bind(&BluetoothDeviceClientImpl::OnPropertyChanged,
weak_ptr_factory_.GetWeakPtr(), object_path));
properties->ConnectSignals();
properties->GetAll();
Object object = std::make_pair(object_proxy, properties);
object_map_[object_path] = object;
return object;
}
// Removes the dbus object proxy and properties for the device with
// dbus object path |object_path| from our |object_map_| map.
void RemoveObject(const dbus::ObjectPath& object_path) {
ObjectMap::iterator iter = object_map_.find(object_path);
if (iter != object_map_.end()) {
// Clean up the Properties structure.
Object object = iter->second;
Properties* properties = object.second;
delete properties;
object_map_.erase(iter);
}
}
// Returns a pointer to the object proxy for |object_path|, creating
// it if necessary.
dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
return GetObject(object_path).first;
}
// Called by BluetoothPropertySet when a property value is changed,
// either by result of a signal or response to a GetAll() or Get()
// call. Informs observers.
void OnPropertyChanged(const dbus::ObjectPath& object_path,
const std::string& property_name) {
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DevicePropertyChanged(object_path, property_name));
}
// Called by dbus:: when a DisconnectRequested signal is received.
void DisconnectRequestedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
VLOG(1) << object_path.value() << ": Disconnect requested.";
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DisconnectRequested(object_path));
}
// Called by dbus:: when the DisconnectRequested signal is initially
// connected.
void DisconnectRequestedConnected(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 "
"DisconnectRequested signal.";
}
// Called by dbus:: when a NodeCreated signal is received.
void NodeCreatedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
dbus::ObjectPath node_path;
if (!reader.PopObjectPath(&node_path)) {
LOG(WARNING) << object_path.value()
<< ": NodeCreated signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": Node created: "
<< node_path.value();
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
NodeCreated(object_path, node_path));
}
// Called by dbus:: when the NodeCreated signal is initially connected.
void NodeCreatedConnected(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 NodeCreated signal.";
}
// Called by dbus:: when a NodeRemoved signal is received.
void NodeRemovedReceived(const dbus::ObjectPath& object_path,
dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
dbus::ObjectPath node_path;
if (!reader.PopObjectPath(&node_path)) {
LOG(WARNING) << object_path.value()
<< ": NodeRemoved signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << object_path.value() << ": Node removed: "
<< node_path.value();
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
NodeRemoved(object_path, node_path));
}
// Called by dbus:: when the NodeRemoved signal is initially connected.
void NodeRemovedConnected(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 NodeRemoved signal.";
}
// Called when a response for DiscoverServices() is received.
void OnDiscoverServices(const dbus::ObjectPath& object_path,
const ServicesCallback& callback,
dbus::Response* response) {
// Parse response.
bool success = false;
ServiceMap services;
if (response != NULL) {
dbus::MessageReader reader(response);
dbus::MessageReader array_reader(NULL);
if (!reader.PopArray(&array_reader)) {
LOG(WARNING) << "DiscoverServices response has incorrect parameters: "
<< response->ToString();
} else {
while (array_reader.HasMoreData()) {
dbus::MessageReader dict_entry_reader(NULL);
uint32 key = 0;
std::string value;
if (!array_reader.PopDictEntry(&dict_entry_reader)
|| !dict_entry_reader.PopUint32(&key)
|| !dict_entry_reader.PopString(&value)) {
LOG(WARNING) << "DiscoverServices response has "
"incorrect parameters: " << response->ToString();
} else {
services[key] = value;
}
}
success = true;
}
} else {
LOG(WARNING) << "Failed to discover services.";
}
// Notify client.
callback.Run(object_path, services, success);
}
// Called when a response for CancelDiscovery() is received.
void OnCancelDiscovery(const dbus::ObjectPath& object_path,
const DeviceCallback& callback,
dbus::Response* response) {
LOG_IF(WARNING, !response) << object_path.value()
<< ": OnCancelDiscovery: failed.";
callback.Run(object_path, response);
}
// Called when a response for Disconnect() is received.
void OnDisconnect(const dbus::ObjectPath& object_path,
const DeviceCallback& callback,
dbus::Response* response) {
LOG_IF(WARNING, !response) << object_path.value()
<< ": OnDisconnect: failed.";
callback.Run(object_path, response);
}
// Called when a response for CreateNode() is received.
void OnCreateNode(const dbus::ObjectPath& object_path,
const NodeCallback& callback,
dbus::Response* response) {
// Parse response.
bool success = false;
dbus::ObjectPath node_path;
if (response != NULL) {
dbus::MessageReader reader(response);
if (!reader.PopObjectPath(&node_path)) {
LOG(WARNING) << "CreateNode response has incorrect parameters: "
<< response->ToString();
} else {
success = true;
}
} else {
LOG(WARNING) << "Failed to create node.";
}
// Notify client.
callback.Run(node_path, success);
}
// Called when a response for RemoveNode() is received.
void OnRemoveNode(const dbus::ObjectPath& object_path,
const DeviceCallback& callback,
dbus::Response* response) {
LOG_IF(WARNING, !response) << object_path.value()
<< ": OnRemoveNode: failed.";
callback.Run(object_path, response);
}
dbus::Bus* bus_;
// List of observers interested in event notifications from us.
ObserverList<BluetoothDeviceClient::Observer> observers_;
// Weak pointer factory for generating 'this' pointers that might live longer
// than we do.
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<BluetoothDeviceClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceClientImpl);
};
// The BluetoothDeviceClient implementation used on Linux desktop, which does
// nothing.
class BluetoothDeviceClientStubImpl : public BluetoothDeviceClient {
public:
struct Properties : public BluetoothDeviceClient::Properties {
explicit Properties(const PropertyChangedCallback& callback)
: BluetoothDeviceClient::Properties(NULL, callback) {
}
virtual ~Properties() {
}
virtual void Get(dbus::PropertyBase* property,
dbus::PropertySet::GetCallback callback) OVERRIDE {
VLOG(1) << "Get " << property->name();
callback.Run(false);
}
virtual void GetAll() OVERRIDE {
VLOG(1) << "GetAll";
}
virtual void Set(dbus::PropertyBase *property,
dbus::PropertySet::SetCallback callback) OVERRIDE {
VLOG(1) << "Set " << property->name();
callback.Run(false);
}
};
BluetoothDeviceClientStubImpl() {
dbus::ObjectPath dev0("/fake/hci0/dev0");
Properties* properties = new Properties(base::Bind(
&BluetoothDeviceClientStubImpl::OnPropertyChanged,
base::Unretained(this),
dev0));
properties->address.ReplaceValue("00:11:22:33:44:55");
properties->name.ReplaceValue("Fake Device");
properties->paired.ReplaceValue(true);
properties->trusted.ReplaceValue(true);
properties_map_[dev0] = properties;
}
virtual ~BluetoothDeviceClientStubImpl() {
// Clean up Properties structures
STLDeleteValues(&properties_map_);
}
// BluetoothDeviceClient override.
virtual void AddObserver(Observer* observer) OVERRIDE {
observers_.AddObserver(observer);
}
// BluetoothDeviceClient override.
virtual void RemoveObserver(Observer* observer) OVERRIDE {
observers_.RemoveObserver(observer);
}
// BluetoothDeviceClient override.
virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
OVERRIDE {
VLOG(1) << "GetProperties: " << object_path.value();
PropertiesMap::iterator iter = properties_map_.find(object_path);
if (iter != properties_map_.end())
return iter->second;
return NULL;
}
// BluetoothDeviceClient override.
virtual void DiscoverServices(const dbus::ObjectPath& object_path,
const std::string& pattern,
const ServicesCallback& callback) OVERRIDE {
VLOG(1) << "DiscoverServices: " << object_path.value() << " " << pattern;
ServiceMap services;
callback.Run(object_path, services, false);
}
// BluetoothDeviceClient override.
virtual void CancelDiscovery(const dbus::ObjectPath& object_path,
const DeviceCallback& callback) OVERRIDE {
VLOG(1) << "CancelDiscovery: " << object_path.value();
callback.Run(object_path, false);
}
// BluetoothDeviceClient override.
virtual void Disconnect(const dbus::ObjectPath& object_path,
const DeviceCallback& callback) OVERRIDE {
VLOG(1) << "Disconnect: " << object_path.value();
callback.Run(object_path, false);
}
// BluetoothDeviceClient override.
virtual void CreateNode(const dbus::ObjectPath& object_path,
const std::string& uuid,
const NodeCallback& callback) OVERRIDE {
VLOG(1) << "CreateNode: " << object_path.value() << " " << uuid;
callback.Run(dbus::ObjectPath(), false);
}
// BluetoothDeviceClient override.
virtual void RemoveNode(const dbus::ObjectPath& object_path,
const dbus::ObjectPath& node_path,
const DeviceCallback& callback) OVERRIDE {
VLOG(1) << "RemoveNode: " << object_path.value()
<< " " << node_path.value();
callback.Run(object_path, false);
}
private:
void OnPropertyChanged(dbus::ObjectPath object_path,
const std::string& property_name) {
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DevicePropertyChanged(object_path, property_name));
}
// List of observers interested in event notifications from us.
ObserverList<Observer> observers_;
// Static properties we typedef.
typedef std::map<const dbus::ObjectPath, Properties *> PropertiesMap;
PropertiesMap properties_map_;
};
BluetoothDeviceClient::BluetoothDeviceClient() {
}
BluetoothDeviceClient::~BluetoothDeviceClient() {
}
BluetoothDeviceClient* BluetoothDeviceClient::Create(
DBusClientImplementationType type,
dbus::Bus* bus,
BluetoothAdapterClient* adapter_client) {
if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
return new BluetoothDeviceClientImpl(bus, adapter_client);
DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
return new BluetoothDeviceClientStubImpl();
}
} // namespace chromeos
|