OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/bluetooth_device_client.h" | 5 #include "chromeos/dbus/bluetooth_device_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
11 #include "dbus/message.h" | 11 #include "dbus/message.h" |
12 #include "dbus/object_manager.h" | 12 #include "dbus/object_manager.h" |
13 #include "dbus/object_path.h" | 13 #include "dbus/object_path.h" |
14 #include "dbus/object_proxy.h" | 14 #include "dbus/object_proxy.h" |
15 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
16 | 16 |
17 namespace chromeos { | 17 namespace chromeos { |
18 | 18 |
19 namespace { | |
20 | |
21 // Value returned for the the RSSI or TX power if it cannot be read. | |
22 const int kUnknownPower = 127; | |
23 | |
24 } // namespace | |
25 | |
19 const char BluetoothDeviceClient::kNoResponseError[] = | 26 const char BluetoothDeviceClient::kNoResponseError[] = |
20 "org.chromium.Error.NoResponse"; | 27 "org.chromium.Error.NoResponse"; |
21 const char BluetoothDeviceClient::kUnknownDeviceError[] = | 28 const char BluetoothDeviceClient::kUnknownDeviceError[] = |
22 "org.chromium.Error.UnknownDevice"; | 29 "org.chromium.Error.UnknownDevice"; |
23 | 30 |
24 BluetoothDeviceClient::Properties::Properties( | 31 BluetoothDeviceClient::Properties::Properties( |
25 dbus::ObjectProxy* object_proxy, | 32 dbus::ObjectProxy* object_proxy, |
26 const std::string& interface_name, | 33 const std::string& interface_name, |
27 const PropertyChangedCallback& callback) | 34 const PropertyChangedCallback& callback) |
28 : dbus::PropertySet(object_proxy, interface_name, callback) { | 35 : dbus::PropertySet(object_proxy, interface_name, callback) { |
29 RegisterProperty(bluetooth_device::kAddressProperty, &address); | 36 RegisterProperty(bluetooth_device::kAddressProperty, &address); |
30 RegisterProperty(bluetooth_device::kNameProperty, &name); | 37 RegisterProperty(bluetooth_device::kNameProperty, &name); |
31 RegisterProperty(bluetooth_device::kIconProperty, &icon); | 38 RegisterProperty(bluetooth_device::kIconProperty, &icon); |
32 RegisterProperty(bluetooth_device::kClassProperty, &bluetooth_class); | 39 RegisterProperty(bluetooth_device::kClassProperty, &bluetooth_class); |
33 RegisterProperty(bluetooth_device::kAppearanceProperty, &appearance); | 40 RegisterProperty(bluetooth_device::kAppearanceProperty, &appearance); |
34 RegisterProperty(bluetooth_device::kUUIDsProperty, &uuids); | 41 RegisterProperty(bluetooth_device::kUUIDsProperty, &uuids); |
35 RegisterProperty(bluetooth_device::kPairedProperty, &paired); | 42 RegisterProperty(bluetooth_device::kPairedProperty, &paired); |
36 RegisterProperty(bluetooth_device::kConnectedProperty, &connected); | 43 RegisterProperty(bluetooth_device::kConnectedProperty, &connected); |
37 RegisterProperty(bluetooth_device::kTrustedProperty, &trusted); | 44 RegisterProperty(bluetooth_device::kTrustedProperty, &trusted); |
38 RegisterProperty(bluetooth_device::kBlockedProperty, &blocked); | 45 RegisterProperty(bluetooth_device::kBlockedProperty, &blocked); |
39 RegisterProperty(bluetooth_device::kAliasProperty, &alias); | 46 RegisterProperty(bluetooth_device::kAliasProperty, &alias); |
40 RegisterProperty(bluetooth_device::kAdapterProperty, &adapter); | 47 RegisterProperty(bluetooth_device::kAdapterProperty, &adapter); |
41 RegisterProperty(bluetooth_device::kLegacyPairingProperty, &legacy_pairing); | 48 RegisterProperty(bluetooth_device::kLegacyPairingProperty, &legacy_pairing); |
42 RegisterProperty(bluetooth_device::kModaliasProperty, &modalias); | 49 RegisterProperty(bluetooth_device::kModaliasProperty, &modalias); |
43 RegisterProperty(bluetooth_device::kRSSIProperty, &rssi); | 50 RegisterProperty(bluetooth_device::kRSSIProperty, &rssi); |
44 RegisterProperty(bluetooth_device::kConnectionRSSI, &connection_rssi); | |
45 RegisterProperty(bluetooth_device::kConnectionTXPower, &connection_tx_power); | |
46 RegisterProperty(bluetooth_device::kConnectionTXPowerMax, | |
47 &connection_tx_power_max); | |
48 } | 51 } |
49 | 52 |
50 BluetoothDeviceClient::Properties::~Properties() { | 53 BluetoothDeviceClient::Properties::~Properties() { |
51 } | 54 } |
52 | 55 |
53 | 56 |
54 // The BluetoothDeviceClient implementation used in production. | 57 // The BluetoothDeviceClient implementation used in production. |
55 class BluetoothDeviceClientImpl | 58 class BluetoothDeviceClientImpl |
56 : public BluetoothDeviceClient, | 59 : public BluetoothDeviceClient, |
57 public dbus::ObjectManager::Interface { | 60 public dbus::ObjectManager::Interface { |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 object_proxy->CallMethodWithErrorCallback( | 269 object_proxy->CallMethodWithErrorCallback( |
267 &method_call, | 270 &method_call, |
268 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 271 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
269 base::Bind(&BluetoothDeviceClientImpl::OnSuccess, | 272 base::Bind(&BluetoothDeviceClientImpl::OnSuccess, |
270 weak_ptr_factory_.GetWeakPtr(), callback), | 273 weak_ptr_factory_.GetWeakPtr(), callback), |
271 base::Bind(&BluetoothDeviceClientImpl::OnError, | 274 base::Bind(&BluetoothDeviceClientImpl::OnError, |
272 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 275 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
273 } | 276 } |
274 | 277 |
275 // BluetoothDeviceClient override. | 278 // BluetoothDeviceClient override. |
276 virtual void StartConnectionMonitor( | 279 void GetConnInfo(const dbus::ObjectPath& object_path, |
277 const dbus::ObjectPath& object_path, | 280 const ConnInfoCallback& callback, |
278 const base::Closure& callback, | 281 const ErrorCallback& error_callback) override { |
279 const ErrorCallback& error_callback) override { | 282 dbus::MethodCall method_call( |
280 dbus::MethodCall method_call(bluetooth_device::kBluetoothDeviceInterface, | 283 bluetooth_plugin_device::kBluetoothPluginInterface, |
281 bluetooth_device::kStartConnectionMonitor); | 284 bluetooth_plugin_device::kGetConnInfo); |
282 | 285 |
283 dbus::ObjectProxy* object_proxy = | 286 dbus::ObjectProxy* object_proxy = |
284 object_manager_->GetObjectProxy(object_path); | 287 object_manager_->GetObjectProxy(object_path); |
285 if (!object_proxy) { | |
286 error_callback.Run(kUnknownDeviceError, ""); | |
287 return; | |
288 } | |
289 object_proxy->CallMethodWithErrorCallback( | |
290 &method_call, | |
291 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
292 base::Bind(&BluetoothDeviceClientImpl::OnSuccess, | |
293 weak_ptr_factory_.GetWeakPtr(), | |
294 callback), | |
295 base::Bind(&BluetoothDeviceClientImpl::OnError, | |
296 weak_ptr_factory_.GetWeakPtr(), | |
297 error_callback)); | |
298 } | |
299 | |
300 // BluetoothDeviceClient override. | |
301 virtual void StopConnectionMonitor( | |
302 const dbus::ObjectPath& object_path, | |
303 const base::Closure& callback, | |
304 const ErrorCallback& error_callback) override { | |
305 dbus::MethodCall method_call(bluetooth_device::kBluetoothDeviceInterface, | |
306 bluetooth_device::kStopConnectionMonitor); | |
307 | |
308 dbus::ObjectProxy* object_proxy = | |
309 object_manager_->GetObjectProxy(object_path); | |
310 if (!object_proxy) { | 288 if (!object_proxy) { |
311 error_callback.Run(kUnknownDeviceError, ""); | 289 error_callback.Run(kUnknownDeviceError, ""); |
312 return; | 290 return; |
313 } | 291 } |
314 object_proxy->CallMethodWithErrorCallback( | 292 object_proxy->CallMethodWithErrorCallback( |
315 &method_call, | 293 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
316 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 294 base::Bind(&BluetoothDeviceClientImpl::OnGetConnInfoSuccess, |
317 base::Bind(&BluetoothDeviceClientImpl::OnSuccess, | 295 weak_ptr_factory_.GetWeakPtr(), callback), |
318 weak_ptr_factory_.GetWeakPtr(), | |
319 callback), | |
320 base::Bind(&BluetoothDeviceClientImpl::OnError, | 296 base::Bind(&BluetoothDeviceClientImpl::OnError, |
321 weak_ptr_factory_.GetWeakPtr(), | 297 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
322 error_callback)); | |
323 } | 298 } |
324 | 299 |
325 protected: | 300 protected: |
326 virtual void Init(dbus::Bus* bus) override { | 301 virtual void Init(dbus::Bus* bus) override { |
327 object_manager_ = bus->GetObjectManager( | 302 object_manager_ = bus->GetObjectManager( |
328 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | 303 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
329 dbus::ObjectPath( | 304 dbus::ObjectPath( |
330 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | 305 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
331 object_manager_->RegisterInterface( | 306 object_manager_->RegisterInterface( |
332 bluetooth_device::kBluetoothDeviceInterface, this); | 307 bluetooth_device::kBluetoothDeviceInterface, this); |
(...skipping 25 matching lines...) Expand all Loading... | |
358 DevicePropertyChanged(object_path, property_name)); | 333 DevicePropertyChanged(object_path, property_name)); |
359 } | 334 } |
360 | 335 |
361 // Called when a response for successful method call is received. | 336 // Called when a response for successful method call is received. |
362 void OnSuccess(const base::Closure& callback, | 337 void OnSuccess(const base::Closure& callback, |
363 dbus::Response* response) { | 338 dbus::Response* response) { |
364 DCHECK(response); | 339 DCHECK(response); |
365 callback.Run(); | 340 callback.Run(); |
366 } | 341 } |
367 | 342 |
343 // Called when a response for the GetConnInfo method is received. | |
344 void OnGetConnInfoSuccess(const ConnInfoCallback& callback, | |
345 dbus::Response* response) { | |
346 int16 rssi = kUnknownPower; | |
347 int16 transmit_power = kUnknownPower; | |
348 int16 max_transmit_power = kUnknownPower; | |
349 | |
350 if (response) { | |
351 dbus::MessageReader reader(response); | |
352 bool success = reader.PopInt16(&rssi); | |
353 success &= reader.PopInt16(&transmit_power); | |
354 success &= reader.PopInt16(&max_transmit_power); | |
armansito
2015/01/06 20:36:07
I find this a bit difficult to read. It might be a
Tim Song
2015/01/06 22:30:50
Done. I don't think it's defined to create a Messa
armansito
2015/01/06 22:39:29
You can also do an early return if response is NUL
Tim Song
2015/01/06 23:02:17
Done.
| |
355 DCHECK(success) << "Arguments for GetConnInfo invalid."; | |
356 } else { | |
357 LOG(ERROR) << "GetConnInfo suceeded, but no response recieved."; | |
358 } | |
359 | |
360 callback.Run(rssi, transmit_power, max_transmit_power); | |
361 } | |
362 | |
368 // Called when a response for a failed method call is received. | 363 // Called when a response for a failed method call is received. |
369 void OnError(const ErrorCallback& error_callback, | 364 void OnError(const ErrorCallback& error_callback, |
370 dbus::ErrorResponse* response) { | 365 dbus::ErrorResponse* response) { |
371 // Error response has optional error message argument. | 366 // Error response has optional error message argument. |
372 std::string error_name; | 367 std::string error_name; |
373 std::string error_message; | 368 std::string error_message; |
374 if (response) { | 369 if (response) { |
375 dbus::MessageReader reader(response); | 370 dbus::MessageReader reader(response); |
376 error_name = response->GetErrorName(); | 371 error_name = response->GetErrorName(); |
377 reader.PopString(&error_message); | 372 reader.PopString(&error_message); |
(...skipping 22 matching lines...) Expand all Loading... | |
400 } | 395 } |
401 | 396 |
402 BluetoothDeviceClient::~BluetoothDeviceClient() { | 397 BluetoothDeviceClient::~BluetoothDeviceClient() { |
403 } | 398 } |
404 | 399 |
405 BluetoothDeviceClient* BluetoothDeviceClient::Create() { | 400 BluetoothDeviceClient* BluetoothDeviceClient::Create() { |
406 return new BluetoothDeviceClientImpl(); | 401 return new BluetoothDeviceClientImpl(); |
407 } | 402 } |
408 | 403 |
409 } // namespace chromeos | 404 } // namespace chromeos |
OLD | NEW |