OLD | NEW |
| (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 #include "chromeos/dbus/bluetooth_adapter_client.h" | |
6 | |
7 #include <map> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/stl_util.h" | |
14 #include "chromeos/dbus/bluetooth_device_client.h" | |
15 #include "chromeos/dbus/bluetooth_manager_client.h" | |
16 #include "chromeos/dbus/bluetooth_property.h" | |
17 #include "chromeos/dbus/fake_old_bluetooth_adapter_client.h" | |
18 #include "dbus/bus.h" | |
19 #include "dbus/message.h" | |
20 #include "dbus/object_path.h" | |
21 #include "dbus/object_proxy.h" | |
22 #include "third_party/cros_system_api/dbus/service_constants.h" | |
23 | |
24 namespace { | |
25 | |
26 // The |CreatePairedDevice| DBus call needs a longer timeout than the default | |
27 // in order to allow BlueZ to timeout this call first. See crosbug.com/37387. | |
28 const int kCreatePairedDeviceTimeoutMs = 120 * 1000; | |
29 | |
30 } // namespace | |
31 | |
32 namespace chromeos { | |
33 | |
34 const char BluetoothAdapterClient::kNoResponseError[] = | |
35 "org.chromium.Error.NoResponse"; | |
36 const char BluetoothAdapterClient::kBadResponseError[] = | |
37 "org.chromium.Error.BadResponse"; | |
38 | |
39 BluetoothAdapterClient::Properties::Properties( | |
40 dbus::ObjectProxy* object_proxy, | |
41 const PropertyChangedCallback& callback) | |
42 : BluetoothPropertySet(object_proxy, | |
43 bluetooth_adapter::kBluetoothAdapterInterface, | |
44 callback) { | |
45 RegisterProperty(bluetooth_adapter::kAddressProperty, &address); | |
46 RegisterProperty(bluetooth_adapter::kNameProperty, &name); | |
47 RegisterProperty(bluetooth_adapter::kClassProperty, &bluetooth_class); | |
48 RegisterProperty(bluetooth_adapter::kPoweredProperty, &powered); | |
49 RegisterProperty(bluetooth_adapter::kDiscoverableProperty, &discoverable); | |
50 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable); | |
51 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty, | |
52 &pairable_timeout); | |
53 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty, | |
54 &discoverable_timeout); | |
55 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering); | |
56 RegisterProperty(bluetooth_adapter::kDevicesProperty, &devices); | |
57 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids); | |
58 } | |
59 | |
60 BluetoothAdapterClient::Properties::~Properties() { | |
61 } | |
62 | |
63 | |
64 // The BluetoothAdapterClient implementation used in production. | |
65 class BluetoothAdapterClientImpl: public BluetoothAdapterClient, | |
66 private BluetoothManagerClient::Observer { | |
67 public: | |
68 explicit BluetoothAdapterClientImpl(dbus::Bus* bus, | |
69 BluetoothManagerClient* manager_client) | |
70 : bus_(bus), | |
71 weak_ptr_factory_(this) { | |
72 DCHECK(manager_client); | |
73 manager_client->AddObserver(this); | |
74 } | |
75 | |
76 virtual ~BluetoothAdapterClientImpl() { | |
77 // Clean up Properties structures | |
78 for (ObjectMap::iterator iter = object_map_.begin(); | |
79 iter != object_map_.end(); ++iter) { | |
80 Object object = iter->second; | |
81 Properties* properties = object.second; | |
82 delete properties; | |
83 } | |
84 } | |
85 | |
86 // BluetoothAdapterClient override. | |
87 virtual void AddObserver(BluetoothAdapterClient::Observer* observer) | |
88 OVERRIDE { | |
89 DCHECK(observer); | |
90 observers_.AddObserver(observer); | |
91 } | |
92 | |
93 // BluetoothAdapterClient override. | |
94 virtual void RemoveObserver(BluetoothAdapterClient::Observer* observer) | |
95 OVERRIDE { | |
96 DCHECK(observer); | |
97 observers_.RemoveObserver(observer); | |
98 } | |
99 | |
100 // BluetoothAdapterClient override. | |
101 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
102 OVERRIDE { | |
103 return GetObject(object_path).second; | |
104 } | |
105 | |
106 // BluetoothAdapterClient override. | |
107 virtual void RequestSession(const dbus::ObjectPath& object_path, | |
108 const AdapterCallback& callback) OVERRIDE { | |
109 dbus::MethodCall method_call( | |
110 bluetooth_adapter::kBluetoothAdapterInterface, | |
111 bluetooth_adapter::kRequestSession); | |
112 | |
113 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
114 | |
115 object_proxy->CallMethod( | |
116 &method_call, | |
117 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
118 base::Bind(&BluetoothAdapterClientImpl::OnRequestSession, | |
119 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
120 } | |
121 | |
122 // BluetoothAdapterClient override. | |
123 virtual void ReleaseSession(const dbus::ObjectPath& object_path, | |
124 const AdapterCallback& callback) OVERRIDE { | |
125 dbus::MethodCall method_call( | |
126 bluetooth_adapter::kBluetoothAdapterInterface, | |
127 bluetooth_adapter::kReleaseSession); | |
128 | |
129 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
130 | |
131 object_proxy->CallMethod( | |
132 &method_call, | |
133 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
134 base::Bind(&BluetoothAdapterClientImpl::OnReleaseSession, | |
135 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
136 } | |
137 | |
138 // BluetoothAdapterClient override. | |
139 virtual void StartDiscovery(const dbus::ObjectPath& object_path, | |
140 const AdapterCallback& callback) OVERRIDE { | |
141 dbus::MethodCall method_call( | |
142 bluetooth_adapter::kBluetoothAdapterInterface, | |
143 bluetooth_adapter::kStartDiscovery); | |
144 | |
145 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
146 | |
147 object_proxy->CallMethod( | |
148 &method_call, | |
149 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
150 base::Bind(&BluetoothAdapterClientImpl::OnStartDiscovery, | |
151 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
152 } | |
153 | |
154 // BluetoothAdapterClient override. | |
155 virtual void StopDiscovery(const dbus::ObjectPath& object_path, | |
156 const AdapterCallback& callback) OVERRIDE { | |
157 dbus::MethodCall method_call( | |
158 bluetooth_adapter::kBluetoothAdapterInterface, | |
159 bluetooth_adapter::kStopDiscovery); | |
160 | |
161 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
162 | |
163 object_proxy->CallMethod( | |
164 &method_call, | |
165 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
166 base::Bind(&BluetoothAdapterClientImpl::OnStopDiscovery, | |
167 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
168 } | |
169 | |
170 // BluetoothAdapterClient override. | |
171 virtual void FindDevice(const dbus::ObjectPath& object_path, | |
172 const std::string& address, | |
173 const DeviceCallback& callback) OVERRIDE { | |
174 dbus::MethodCall method_call( | |
175 bluetooth_adapter::kBluetoothAdapterInterface, | |
176 bluetooth_adapter::kFindDevice); | |
177 | |
178 dbus::MessageWriter writer(&method_call); | |
179 writer.AppendString(address); | |
180 | |
181 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
182 | |
183 object_proxy->CallMethod( | |
184 &method_call, | |
185 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
186 base::Bind(&BluetoothAdapterClientImpl::OnFindDevice, | |
187 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
188 } | |
189 | |
190 // BluetoothAdapterClient override. | |
191 virtual void CreateDevice(const dbus::ObjectPath& object_path, | |
192 const std::string& address, | |
193 const CreateDeviceCallback& callback, | |
194 const CreateDeviceErrorCallback& error_callback) | |
195 OVERRIDE { | |
196 dbus::MethodCall method_call( | |
197 bluetooth_adapter::kBluetoothAdapterInterface, | |
198 bluetooth_adapter::kCreateDevice); | |
199 | |
200 dbus::MessageWriter writer(&method_call); | |
201 writer.AppendString(address); | |
202 | |
203 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
204 | |
205 object_proxy->CallMethodWithErrorCallback( | |
206 &method_call, | |
207 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
208 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice, | |
209 weak_ptr_factory_.GetWeakPtr(), object_path, | |
210 callback, error_callback), | |
211 base::Bind(&BluetoothAdapterClientImpl::OnCreateDeviceError, | |
212 weak_ptr_factory_.GetWeakPtr(), object_path, | |
213 error_callback)); | |
214 } | |
215 | |
216 // BluetoothAdapterClient override. | |
217 virtual void CreatePairedDevice( | |
218 const dbus::ObjectPath& object_path, const std::string& address, | |
219 const dbus::ObjectPath& agent_path, const std::string& capability, | |
220 const CreateDeviceCallback& callback, | |
221 const CreateDeviceErrorCallback& error_callback) OVERRIDE { | |
222 dbus::MethodCall method_call( | |
223 bluetooth_adapter::kBluetoothAdapterInterface, | |
224 bluetooth_adapter::kCreatePairedDevice); | |
225 | |
226 dbus::MessageWriter writer(&method_call); | |
227 writer.AppendString(address); | |
228 writer.AppendObjectPath(agent_path); | |
229 writer.AppendString(capability); | |
230 | |
231 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
232 | |
233 object_proxy->CallMethodWithErrorCallback( | |
234 &method_call, | |
235 kCreatePairedDeviceTimeoutMs, | |
236 base::Bind(&BluetoothAdapterClientImpl::OnCreatePairedDevice, | |
237 weak_ptr_factory_.GetWeakPtr(), object_path, | |
238 callback, error_callback), | |
239 base::Bind(&BluetoothAdapterClientImpl::OnCreatePairedDeviceError, | |
240 weak_ptr_factory_.GetWeakPtr(), object_path, | |
241 error_callback)); | |
242 } | |
243 | |
244 // BluetoothAdapterClient override. | |
245 virtual void CancelDeviceCreation(const dbus::ObjectPath& object_path, | |
246 const std::string& address, | |
247 const AdapterCallback& callback) OVERRIDE { | |
248 dbus::MethodCall method_call( | |
249 bluetooth_adapter::kBluetoothAdapterInterface, | |
250 bluetooth_adapter::kCancelDeviceCreation); | |
251 | |
252 dbus::MessageWriter writer(&method_call); | |
253 writer.AppendString(address); | |
254 | |
255 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
256 | |
257 object_proxy->CallMethod( | |
258 &method_call, | |
259 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
260 base::Bind(&BluetoothAdapterClientImpl::OnCancelDeviceCreation, | |
261 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
262 } | |
263 | |
264 // BluetoothAdapterClient override. | |
265 virtual void RemoveDevice(const dbus::ObjectPath& object_path, | |
266 const dbus::ObjectPath& device_path, | |
267 const AdapterCallback& callback) OVERRIDE { | |
268 dbus::MethodCall method_call( | |
269 bluetooth_adapter::kBluetoothAdapterInterface, | |
270 bluetooth_adapter::kRemoveDevice); | |
271 | |
272 dbus::MessageWriter writer(&method_call); | |
273 writer.AppendObjectPath(device_path); | |
274 | |
275 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
276 | |
277 object_proxy->CallMethod( | |
278 &method_call, | |
279 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
280 base::Bind(&BluetoothAdapterClientImpl::OnRemoveDevice, | |
281 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
282 } | |
283 | |
284 // BluetoothAdapterClient override. | |
285 virtual void RegisterAgent(const dbus::ObjectPath& object_path, | |
286 const dbus::ObjectPath& agent_path, | |
287 const std::string& capability, | |
288 const AdapterCallback& callback) OVERRIDE { | |
289 dbus::MethodCall method_call( | |
290 bluetooth_adapter::kBluetoothAdapterInterface, | |
291 bluetooth_adapter::kRegisterAgent); | |
292 | |
293 dbus::MessageWriter writer(&method_call); | |
294 writer.AppendObjectPath(agent_path); | |
295 writer.AppendString(capability); | |
296 | |
297 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
298 | |
299 object_proxy->CallMethod( | |
300 &method_call, | |
301 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
302 base::Bind(&BluetoothAdapterClientImpl::OnRegisterAgent, | |
303 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
304 } | |
305 | |
306 // BluetoothAdapterClient override. | |
307 virtual void UnregisterAgent(const dbus::ObjectPath& object_path, | |
308 const dbus::ObjectPath& agent_path, | |
309 const AdapterCallback& callback) OVERRIDE { | |
310 dbus::MethodCall method_call( | |
311 bluetooth_adapter::kBluetoothAdapterInterface, | |
312 bluetooth_adapter::kUnregisterAgent); | |
313 | |
314 dbus::MessageWriter writer(&method_call); | |
315 writer.AppendObjectPath(agent_path); | |
316 | |
317 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
318 | |
319 object_proxy->CallMethod( | |
320 &method_call, | |
321 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
322 base::Bind(&BluetoothAdapterClientImpl::OnUnregisterAgent, | |
323 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
324 } | |
325 | |
326 private: | |
327 // We maintain a collection of dbus object proxies and properties structures | |
328 // for each adapter. | |
329 typedef std::pair<dbus::ObjectProxy*, Properties*> Object; | |
330 typedef std::map<const dbus::ObjectPath, Object> ObjectMap; | |
331 ObjectMap object_map_; | |
332 | |
333 // BluetoothManagerClient::Observer override. | |
334 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE { | |
335 } | |
336 | |
337 // BluetoothManagerClient::Observer override. | |
338 virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE { | |
339 RemoveObject(object_path); | |
340 } | |
341 | |
342 // Ensures that we have an object proxy and properties structure for | |
343 // an adapter with object path |object_path|, creating it if not and | |
344 // storing in our |object_map_| map. | |
345 Object GetObject(const dbus::ObjectPath& object_path) { | |
346 ObjectMap::iterator iter = object_map_.find(object_path); | |
347 if (iter != object_map_.end()) | |
348 return iter->second; | |
349 | |
350 // Create the object proxy. | |
351 DCHECK(bus_); | |
352 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy( | |
353 bluetooth_adapter::kBluetoothAdapterServiceName, object_path); | |
354 | |
355 object_proxy->ConnectToSignal( | |
356 bluetooth_adapter::kBluetoothAdapterInterface, | |
357 bluetooth_adapter::kDeviceCreatedSignal, | |
358 base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedReceived, | |
359 weak_ptr_factory_.GetWeakPtr(), object_path), | |
360 base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedConnected, | |
361 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
362 | |
363 object_proxy->ConnectToSignal( | |
364 bluetooth_adapter::kBluetoothAdapterInterface, | |
365 bluetooth_adapter::kDeviceRemovedSignal, | |
366 base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedReceived, | |
367 weak_ptr_factory_.GetWeakPtr(), object_path), | |
368 base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedConnected, | |
369 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
370 | |
371 object_proxy->ConnectToSignal( | |
372 bluetooth_adapter::kBluetoothAdapterInterface, | |
373 bluetooth_adapter::kDeviceFoundSignal, | |
374 base::Bind(&BluetoothAdapterClientImpl::DeviceFoundReceived, | |
375 weak_ptr_factory_.GetWeakPtr(), object_path), | |
376 base::Bind(&BluetoothAdapterClientImpl::DeviceFoundConnected, | |
377 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
378 | |
379 object_proxy->ConnectToSignal( | |
380 bluetooth_adapter::kBluetoothAdapterInterface, | |
381 bluetooth_adapter::kDeviceDisappearedSignal, | |
382 base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedReceived, | |
383 weak_ptr_factory_.GetWeakPtr(), object_path), | |
384 base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedConnected, | |
385 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
386 | |
387 // Create the properties structure. | |
388 Properties* properties = new Properties( | |
389 object_proxy, | |
390 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged, | |
391 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
392 | |
393 properties->ConnectSignals(); | |
394 properties->GetAll(); | |
395 | |
396 Object object = std::make_pair(object_proxy, properties); | |
397 object_map_[object_path] = object; | |
398 return object; | |
399 } | |
400 | |
401 // Removes the dbus object proxy and properties for the adapter with | |
402 // dbus object path |object_path| from our |object_map_| map. | |
403 void RemoveObject(const dbus::ObjectPath& object_path) { | |
404 ObjectMap::iterator iter = object_map_.find(object_path); | |
405 if (iter != object_map_.end()) { | |
406 // Clean up the Properties structure. | |
407 Object object = iter->second; | |
408 Properties* properties = object.second; | |
409 delete properties; | |
410 | |
411 object_map_.erase(iter); | |
412 } | |
413 } | |
414 | |
415 // Returns a pointer to the object proxy for |object_path|, creating | |
416 // it if necessary. | |
417 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) { | |
418 return GetObject(object_path).first; | |
419 } | |
420 | |
421 // Called by BluetoothPropertySet when a property value is changed, | |
422 // either by result of a signal or response to a GetAll() or Get() | |
423 // call. Informs observers. | |
424 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
425 const std::string& property_name) { | |
426 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
427 AdapterPropertyChanged(object_path, property_name)); | |
428 } | |
429 | |
430 // Called by dbus:: when a DeviceCreated signal is received. | |
431 void DeviceCreatedReceived(const dbus::ObjectPath& object_path, | |
432 dbus::Signal* signal) { | |
433 DCHECK(signal); | |
434 dbus::MessageReader reader(signal); | |
435 dbus::ObjectPath device_path; | |
436 if (!reader.PopObjectPath(&device_path)) { | |
437 LOG(WARNING) << object_path.value() | |
438 << ": DeviceCreated signal has incorrect parameters: " | |
439 << signal->ToString(); | |
440 return; | |
441 } | |
442 | |
443 VLOG(1) << object_path.value() << ": Device created: " | |
444 << device_path.value(); | |
445 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
446 DeviceCreated(object_path, device_path)); | |
447 } | |
448 | |
449 // Called by dbus:: when the DeviceCreated signal is initially connected. | |
450 void DeviceCreatedConnected(const dbus::ObjectPath& object_path, | |
451 const std::string& interface_name, | |
452 const std::string& signal_name, | |
453 bool success) { | |
454 LOG_IF(WARNING, !success) << object_path.value() | |
455 << ": Failed to connect to DeviceCreated signal."; | |
456 } | |
457 | |
458 // Called by dbus:: when a DeviceRemoved signal is received. | |
459 void DeviceRemovedReceived(const dbus::ObjectPath& object_path, | |
460 dbus::Signal* signal) { | |
461 DCHECK(signal); | |
462 dbus::MessageReader reader(signal); | |
463 dbus::ObjectPath device_path; | |
464 if (!reader.PopObjectPath(&device_path)) { | |
465 LOG(WARNING) << object_path.value() | |
466 << ": DeviceRemoved signal has incorrect parameters: " | |
467 << signal->ToString(); | |
468 return; | |
469 } | |
470 | |
471 VLOG(1) << object_path.value() << ": Device removed: " | |
472 << device_path.value(); | |
473 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
474 DeviceRemoved(object_path, device_path)); | |
475 } | |
476 | |
477 // Called by dbus:: when the DeviceRemoved signal is initially connected. | |
478 void DeviceRemovedConnected(const dbus::ObjectPath& object_path, | |
479 const std::string& interface_name, | |
480 const std::string& signal_name, | |
481 bool success) { | |
482 LOG_IF(WARNING, !success) << object_path.value() | |
483 << ": Failed to connect to DeviceRemoved signal."; | |
484 } | |
485 | |
486 // Called by dbus:: when a DeviceFound signal is received. | |
487 void DeviceFoundReceived(const dbus::ObjectPath& object_path, | |
488 dbus::Signal* signal) { | |
489 DCHECK(signal); | |
490 dbus::MessageReader reader(signal); | |
491 std::string address; | |
492 if (!reader.PopString(&address)) { | |
493 LOG(WARNING) << object_path.value() | |
494 << ": DeviceFound signal has incorrect parameters: " | |
495 << signal->ToString(); | |
496 return; | |
497 } | |
498 | |
499 // Create device properties structure without an attached object_proxy | |
500 // and a NULL callback; value() functions will work on this, but not | |
501 // Get() or Set() calls. | |
502 BluetoothDeviceClient::Properties device_properties( | |
503 NULL, BluetoothDeviceClient::Properties::PropertyChangedCallback()); | |
504 if (!device_properties.UpdatePropertiesFromReader(&reader)) { | |
505 LOG(WARNING) << object_path.value() | |
506 << ": DeviceFound signal has incorrect parameters: " | |
507 << signal->ToString(); | |
508 return; | |
509 } | |
510 | |
511 VLOG(1) << object_path.value() << ": Device found: " << address; | |
512 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
513 DeviceFound(object_path, address, device_properties)); | |
514 } | |
515 | |
516 // Called by dbus:: when the DeviceFound signal is initially connected. | |
517 void DeviceFoundConnected(const dbus::ObjectPath& object_path, | |
518 const std::string& interface_name, | |
519 const std::string& signal_name, | |
520 bool success) { | |
521 LOG_IF(WARNING, !success) << object_path.value() | |
522 << ": Failed to connect to DeviceFound signal."; | |
523 } | |
524 | |
525 // Called by dbus:: when a DeviceDisappeared signal is received. | |
526 void DeviceDisappearedReceived(const dbus::ObjectPath& object_path, | |
527 dbus::Signal* signal) { | |
528 DCHECK(signal); | |
529 dbus::MessageReader reader(signal); | |
530 std::string address; | |
531 if (!reader.PopString(&address)) { | |
532 LOG(WARNING) << object_path.value() | |
533 << ": DeviceDisappeared signal has incorrect parameters: " | |
534 << signal->ToString(); | |
535 return; | |
536 } | |
537 | |
538 VLOG(1) << object_path.value() << ": Device disappeared: " << address; | |
539 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
540 DeviceDisappeared(object_path, address)); | |
541 } | |
542 | |
543 // Called by dbus:: when the DeviceDisappeared signal is initially connected. | |
544 void DeviceDisappearedConnected(const dbus::ObjectPath& object_path, | |
545 const std::string& interface_name, | |
546 const std::string& signal_name, | |
547 bool success) { | |
548 LOG_IF(WARNING, !success) | |
549 << object_path.value() | |
550 << ": Failed to connect to DeviceDisappeared signal."; | |
551 } | |
552 | |
553 // Called when a response for RequestSession() is received. | |
554 void OnRequestSession(const dbus::ObjectPath& object_path, | |
555 const AdapterCallback& callback, | |
556 dbus::Response* response) { | |
557 LOG_IF(WARNING, !response) << object_path.value() | |
558 << ": OnRequestSession: failed."; | |
559 callback.Run(object_path, response); | |
560 } | |
561 | |
562 // Called when a response for ReleaseSession() is received. | |
563 void OnReleaseSession(const dbus::ObjectPath& object_path, | |
564 const AdapterCallback& callback, | |
565 dbus::Response* response) { | |
566 LOG_IF(WARNING, !response) << object_path.value() | |
567 << ": OnReleaseSession: failed."; | |
568 callback.Run(object_path, response); | |
569 } | |
570 | |
571 // Called when a response for StartDiscovery() is received. | |
572 void OnStartDiscovery(const dbus::ObjectPath& object_path, | |
573 const AdapterCallback& callback, | |
574 dbus::Response* response) { | |
575 LOG_IF(WARNING, !response) << object_path.value() | |
576 << ": OnStartDiscovery: failed."; | |
577 callback.Run(object_path, response); | |
578 } | |
579 | |
580 // Called when a response for StopDiscovery() is received. | |
581 void OnStopDiscovery(const dbus::ObjectPath& object_path, | |
582 const AdapterCallback& callback, | |
583 dbus::Response* response) { | |
584 LOG_IF(WARNING, !response) << object_path.value() | |
585 << ": OnStopDiscovery: failed."; | |
586 callback.Run(object_path, response); | |
587 } | |
588 | |
589 // Called when a response for FindDevice() is received. | |
590 void OnFindDevice(const dbus::ObjectPath& object_path, | |
591 const DeviceCallback& callback, | |
592 dbus::Response* response) { | |
593 // Parse response. | |
594 bool success = false; | |
595 dbus::ObjectPath device_path; | |
596 if (response != NULL) { | |
597 dbus::MessageReader reader(response); | |
598 if (!reader.PopObjectPath(&device_path)) { | |
599 LOG(WARNING) << "FindDevice response has incorrect parameters: " | |
600 << response->ToString(); | |
601 } else { | |
602 success = true; | |
603 } | |
604 } else { | |
605 LOG(WARNING) << "Failed to find device."; | |
606 } | |
607 | |
608 // Notify client. | |
609 callback.Run(device_path, success); | |
610 } | |
611 | |
612 // Called when a response for CreateDevice() is received. | |
613 void OnCreateDevice(const dbus::ObjectPath& object_path, | |
614 const CreateDeviceCallback& callback, | |
615 const CreateDeviceErrorCallback& error_callback, | |
616 dbus::Response* response) { | |
617 // Parse response. | |
618 DCHECK(response); | |
619 dbus::ObjectPath device_path; | |
620 dbus::MessageReader reader(response); | |
621 if (!reader.PopObjectPath(&device_path)) { | |
622 LOG(WARNING) << "CreateDevice response has incorrect parameters: " | |
623 << response->ToString(); | |
624 error_callback.Run(kBadResponseError, ""); | |
625 return; | |
626 } | |
627 | |
628 // Notify client. | |
629 callback.Run(device_path); | |
630 } | |
631 | |
632 // Called when an error for CreateDevice() is received. | |
633 void OnCreateDeviceError(const dbus::ObjectPath& object_path, | |
634 const CreateDeviceErrorCallback& error_callback, | |
635 dbus::ErrorResponse* response) { | |
636 // Error response has optional error message argument. | |
637 std::string error_name; | |
638 std::string error_message; | |
639 if (response) { | |
640 dbus::MessageReader reader(response); | |
641 error_name = response->GetErrorName(); | |
642 reader.PopString(&error_message); | |
643 } else { | |
644 error_name = kNoResponseError; | |
645 error_message = ""; | |
646 } | |
647 error_callback.Run(error_name, error_message); | |
648 } | |
649 | |
650 // Called when a response for CreatePairedDevice() is received. | |
651 void OnCreatePairedDevice(const dbus::ObjectPath& object_path, | |
652 const CreateDeviceCallback& callback, | |
653 const CreateDeviceErrorCallback& error_callback, | |
654 dbus::Response* response) { | |
655 // Parse response. | |
656 DCHECK(response); | |
657 dbus::ObjectPath device_path; | |
658 dbus::MessageReader reader(response); | |
659 if (!reader.PopObjectPath(&device_path)) { | |
660 LOG(WARNING) << "CreatePairedDevice response has incorrect parameters: " | |
661 << response->ToString(); | |
662 error_callback.Run(kBadResponseError, ""); | |
663 return; | |
664 } | |
665 | |
666 // Notify client. | |
667 callback.Run(device_path); | |
668 } | |
669 | |
670 // Called when an error for CreatePairedDevice() is received. | |
671 void OnCreatePairedDeviceError( | |
672 const dbus::ObjectPath& object_path, | |
673 const CreateDeviceErrorCallback& error_callback, | |
674 dbus::ErrorResponse* response) { | |
675 // Error response has optional error message argument. | |
676 std::string error_name; | |
677 std::string error_message; | |
678 if (response) { | |
679 dbus::MessageReader reader(response); | |
680 error_name = response->GetErrorName(); | |
681 reader.PopString(&error_message); | |
682 } else { | |
683 error_name = kNoResponseError; | |
684 error_message = ""; | |
685 } | |
686 error_callback.Run(error_name, error_message); | |
687 } | |
688 | |
689 // Called when a response for CancelDeviceCreation() is received. | |
690 void OnCancelDeviceCreation(const dbus::ObjectPath& object_path, | |
691 const AdapterCallback& callback, | |
692 dbus::Response* response) { | |
693 LOG_IF(WARNING, !response) << object_path.value() | |
694 << ": OnCancelDeviceCreation: failed."; | |
695 callback.Run(object_path, response); | |
696 } | |
697 | |
698 // Called when a response for RemoveDevice() is received. | |
699 void OnRemoveDevice(const dbus::ObjectPath& object_path, | |
700 const AdapterCallback& callback, | |
701 dbus::Response* response) { | |
702 LOG_IF(WARNING, !response) << object_path.value() | |
703 << ": OnRemoveDevice: failed."; | |
704 callback.Run(object_path, response); | |
705 } | |
706 | |
707 // Called when a response for RegisterAgent() is received. | |
708 void OnRegisterAgent(const dbus::ObjectPath& object_path, | |
709 const AdapterCallback& callback, | |
710 dbus::Response* response) { | |
711 LOG_IF(WARNING, !response) << object_path.value() | |
712 << ": OnRegisterAgent: failed."; | |
713 callback.Run(object_path, response); | |
714 } | |
715 | |
716 // Called when a response for UnregisterAgent() is received. | |
717 void OnUnregisterAgent(const dbus::ObjectPath& object_path, | |
718 const AdapterCallback& callback, | |
719 dbus::Response* response) { | |
720 LOG_IF(WARNING, !response) << object_path.value() | |
721 << ": OnUnregisterAgent: failed."; | |
722 callback.Run(object_path, response); | |
723 } | |
724 | |
725 dbus::Bus* bus_; | |
726 | |
727 // List of observers interested in event notifications from us. | |
728 ObserverList<BluetoothAdapterClient::Observer> observers_; | |
729 | |
730 // Weak pointer factory for generating 'this' pointers that might live longer | |
731 // than we do. | |
732 // Note: This should remain the last member so it'll be destroyed and | |
733 // invalidate its weak pointers before any other members are destroyed. | |
734 base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_; | |
735 | |
736 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl); | |
737 }; | |
738 | |
739 BluetoothAdapterClient::BluetoothAdapterClient() { | |
740 } | |
741 | |
742 BluetoothAdapterClient::~BluetoothAdapterClient() { | |
743 } | |
744 | |
745 BluetoothAdapterClient* BluetoothAdapterClient::Create( | |
746 DBusClientImplementationType type, | |
747 dbus::Bus* bus, | |
748 BluetoothManagerClient* manager_client) { | |
749 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
750 return new BluetoothAdapterClientImpl(bus, manager_client); | |
751 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
752 return new FakeOldBluetoothAdapterClient(); | |
753 } | |
754 | |
755 } // namespace chromeos | |
OLD | NEW |