| 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_adapter_client.h" | 5 #include "device/bluetooth/dbus/bluetooth_adapter_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 "dbus/bus.h" | 9 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
| 11 #include "dbus/object_manager.h" | 11 #include "dbus/object_manager.h" |
| 12 #include "dbus/object_proxy.h" | 12 #include "dbus/object_proxy.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 | 14 |
| 15 namespace chromeos { | 15 namespace bluez { |
| 16 | 16 |
| 17 BluetoothAdapterClient::DiscoveryFilter::DiscoveryFilter() { | 17 BluetoothAdapterClient::DiscoveryFilter::DiscoveryFilter() {} |
| 18 } | |
| 19 | 18 |
| 20 BluetoothAdapterClient::DiscoveryFilter::~DiscoveryFilter() { | 19 BluetoothAdapterClient::DiscoveryFilter::~DiscoveryFilter() {} |
| 21 } | |
| 22 | 20 |
| 23 void BluetoothAdapterClient::DiscoveryFilter::CopyFrom( | 21 void BluetoothAdapterClient::DiscoveryFilter::CopyFrom( |
| 24 const DiscoveryFilter& filter) { | 22 const DiscoveryFilter& filter) { |
| 25 if (filter.rssi.get()) | 23 if (filter.rssi.get()) |
| 26 rssi.reset(new int16_t(*filter.rssi)); | 24 rssi.reset(new int16_t(*filter.rssi)); |
| 27 else | 25 else |
| 28 rssi.reset(); | 26 rssi.reset(); |
| 29 | 27 |
| 30 if (filter.pathloss.get()) | 28 if (filter.pathloss.get()) |
| 31 pathloss.reset(new uint16_t(*filter.pathloss)); | 29 pathloss.reset(new uint16_t(*filter.pathloss)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 62 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable); | 60 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable); |
| 63 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty, | 61 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty, |
| 64 &pairable_timeout); | 62 &pairable_timeout); |
| 65 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty, | 63 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty, |
| 66 &discoverable_timeout); | 64 &discoverable_timeout); |
| 67 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering); | 65 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering); |
| 68 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids); | 66 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids); |
| 69 RegisterProperty(bluetooth_adapter::kModaliasProperty, &modalias); | 67 RegisterProperty(bluetooth_adapter::kModaliasProperty, &modalias); |
| 70 } | 68 } |
| 71 | 69 |
| 72 BluetoothAdapterClient::Properties::~Properties() { | 70 BluetoothAdapterClient::Properties::~Properties() {} |
| 73 } | |
| 74 | |
| 75 | 71 |
| 76 // The BluetoothAdapterClient implementation used in production. | 72 // The BluetoothAdapterClient implementation used in production. |
| 77 class BluetoothAdapterClientImpl | 73 class BluetoothAdapterClientImpl : public BluetoothAdapterClient, |
| 78 : public BluetoothAdapterClient, | 74 public dbus::ObjectManager::Interface { |
| 79 public dbus::ObjectManager::Interface { | |
| 80 public: | 75 public: |
| 81 BluetoothAdapterClientImpl() | 76 BluetoothAdapterClientImpl() |
| 82 : object_manager_(NULL), weak_ptr_factory_(this) {} | 77 : object_manager_(NULL), weak_ptr_factory_(this) {} |
| 83 | 78 |
| 84 ~BluetoothAdapterClientImpl() override { | 79 ~BluetoothAdapterClientImpl() override { |
| 85 object_manager_->UnregisterInterface( | 80 object_manager_->UnregisterInterface( |
| 86 bluetooth_adapter::kBluetoothAdapterInterface); | 81 bluetooth_adapter::kBluetoothAdapterInterface); |
| 87 } | 82 } |
| 88 | 83 |
| 89 // BluetoothAdapterClient override. | 84 // BluetoothAdapterClient override. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 103 return object_manager_->GetObjectsWithInterface( | 98 return object_manager_->GetObjectsWithInterface( |
| 104 bluetooth_adapter::kBluetoothAdapterInterface); | 99 bluetooth_adapter::kBluetoothAdapterInterface); |
| 105 } | 100 } |
| 106 | 101 |
| 107 // dbus::ObjectManager::Interface override. | 102 // dbus::ObjectManager::Interface override. |
| 108 dbus::PropertySet* CreateProperties( | 103 dbus::PropertySet* CreateProperties( |
| 109 dbus::ObjectProxy* object_proxy, | 104 dbus::ObjectProxy* object_proxy, |
| 110 const dbus::ObjectPath& object_path, | 105 const dbus::ObjectPath& object_path, |
| 111 const std::string& interface_name) override { | 106 const std::string& interface_name) override { |
| 112 Properties* properties = new Properties( | 107 Properties* properties = new Properties( |
| 113 object_proxy, | 108 object_proxy, interface_name, |
| 114 interface_name, | |
| 115 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged, | 109 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged, |
| 116 weak_ptr_factory_.GetWeakPtr(), | 110 weak_ptr_factory_.GetWeakPtr(), object_path)); |
| 117 object_path)); | |
| 118 return static_cast<dbus::PropertySet*>(properties); | 111 return static_cast<dbus::PropertySet*>(properties); |
| 119 } | 112 } |
| 120 | 113 |
| 121 // BluetoothAdapterClient override. | 114 // BluetoothAdapterClient override. |
| 122 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | 115 Properties* GetProperties(const dbus::ObjectPath& object_path) override { |
| 123 return static_cast<Properties*>( | 116 return static_cast<Properties*>(object_manager_->GetProperties( |
| 124 object_manager_->GetProperties( | 117 object_path, bluetooth_adapter::kBluetoothAdapterInterface)); |
| 125 object_path, | |
| 126 bluetooth_adapter::kBluetoothAdapterInterface)); | |
| 127 } | 118 } |
| 128 | 119 |
| 129 // BluetoothAdapterClient override. | 120 // BluetoothAdapterClient override. |
| 130 void StartDiscovery(const dbus::ObjectPath& object_path, | 121 void StartDiscovery(const dbus::ObjectPath& object_path, |
| 131 const base::Closure& callback, | 122 const base::Closure& callback, |
| 132 const ErrorCallback& error_callback) override { | 123 const ErrorCallback& error_callback) override { |
| 133 dbus::MethodCall method_call( | 124 dbus::MethodCall method_call(bluetooth_adapter::kBluetoothAdapterInterface, |
| 134 bluetooth_adapter::kBluetoothAdapterInterface, | 125 bluetooth_adapter::kStartDiscovery); |
| 135 bluetooth_adapter::kStartDiscovery); | |
| 136 | 126 |
| 137 dbus::ObjectProxy* object_proxy = | 127 dbus::ObjectProxy* object_proxy = |
| 138 object_manager_->GetObjectProxy(object_path); | 128 object_manager_->GetObjectProxy(object_path); |
| 139 if (!object_proxy) { | 129 if (!object_proxy) { |
| 140 error_callback.Run(kUnknownAdapterError, ""); | 130 error_callback.Run(kUnknownAdapterError, ""); |
| 141 return; | 131 return; |
| 142 } | 132 } |
| 143 | 133 |
| 144 object_proxy->CallMethodWithErrorCallback( | 134 object_proxy->CallMethodWithErrorCallback( |
| 145 &method_call, | 135 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 146 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 147 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, | 136 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, |
| 148 weak_ptr_factory_.GetWeakPtr(), callback), | 137 weak_ptr_factory_.GetWeakPtr(), callback), |
| 149 base::Bind(&BluetoothAdapterClientImpl::OnError, | 138 base::Bind(&BluetoothAdapterClientImpl::OnError, |
| 150 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 139 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 151 } | 140 } |
| 152 | 141 |
| 153 // BluetoothAdapterClient override. | 142 // BluetoothAdapterClient override. |
| 154 void StopDiscovery(const dbus::ObjectPath& object_path, | 143 void StopDiscovery(const dbus::ObjectPath& object_path, |
| 155 const base::Closure& callback, | 144 const base::Closure& callback, |
| 156 const ErrorCallback& error_callback) override { | 145 const ErrorCallback& error_callback) override { |
| 157 dbus::MethodCall method_call( | 146 dbus::MethodCall method_call(bluetooth_adapter::kBluetoothAdapterInterface, |
| 158 bluetooth_adapter::kBluetoothAdapterInterface, | 147 bluetooth_adapter::kStopDiscovery); |
| 159 bluetooth_adapter::kStopDiscovery); | |
| 160 | 148 |
| 161 dbus::ObjectProxy* object_proxy = | 149 dbus::ObjectProxy* object_proxy = |
| 162 object_manager_->GetObjectProxy(object_path); | 150 object_manager_->GetObjectProxy(object_path); |
| 163 if (!object_proxy) { | 151 if (!object_proxy) { |
| 164 error_callback.Run(kUnknownAdapterError, ""); | 152 error_callback.Run(kUnknownAdapterError, ""); |
| 165 return; | 153 return; |
| 166 } | 154 } |
| 167 | 155 |
| 168 object_proxy->CallMethodWithErrorCallback( | 156 object_proxy->CallMethodWithErrorCallback( |
| 169 &method_call, | 157 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 170 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 171 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, | 158 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, |
| 172 weak_ptr_factory_.GetWeakPtr(), callback), | 159 weak_ptr_factory_.GetWeakPtr(), callback), |
| 173 base::Bind(&BluetoothAdapterClientImpl::OnError, | 160 base::Bind(&BluetoothAdapterClientImpl::OnError, |
| 174 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 161 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 175 } | 162 } |
| 176 | 163 |
| 177 // BluetoothAdapterClient override. | 164 // BluetoothAdapterClient override. |
| 178 void RemoveDevice(const dbus::ObjectPath& object_path, | 165 void RemoveDevice(const dbus::ObjectPath& object_path, |
| 179 const dbus::ObjectPath& device_path, | 166 const dbus::ObjectPath& device_path, |
| 180 const base::Closure& callback, | 167 const base::Closure& callback, |
| 181 const ErrorCallback& error_callback) override { | 168 const ErrorCallback& error_callback) override { |
| 182 dbus::MethodCall method_call( | 169 dbus::MethodCall method_call(bluetooth_adapter::kBluetoothAdapterInterface, |
| 183 bluetooth_adapter::kBluetoothAdapterInterface, | 170 bluetooth_adapter::kRemoveDevice); |
| 184 bluetooth_adapter::kRemoveDevice); | |
| 185 | 171 |
| 186 dbus::MessageWriter writer(&method_call); | 172 dbus::MessageWriter writer(&method_call); |
| 187 writer.AppendObjectPath(device_path); | 173 writer.AppendObjectPath(device_path); |
| 188 | 174 |
| 189 dbus::ObjectProxy* object_proxy = | 175 dbus::ObjectProxy* object_proxy = |
| 190 object_manager_->GetObjectProxy(object_path); | 176 object_manager_->GetObjectProxy(object_path); |
| 191 if (!object_proxy) { | 177 if (!object_proxy) { |
| 192 error_callback.Run(kUnknownAdapterError, ""); | 178 error_callback.Run(kUnknownAdapterError, ""); |
| 193 return; | 179 return; |
| 194 } | 180 } |
| 195 | 181 |
| 196 object_proxy->CallMethodWithErrorCallback( | 182 object_proxy->CallMethodWithErrorCallback( |
| 197 &method_call, | 183 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 198 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 199 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, | 184 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, |
| 200 weak_ptr_factory_.GetWeakPtr(), callback), | 185 weak_ptr_factory_.GetWeakPtr(), callback), |
| 201 base::Bind(&BluetoothAdapterClientImpl::OnError, | 186 base::Bind(&BluetoothAdapterClientImpl::OnError, |
| 202 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 187 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 203 } | 188 } |
| 204 | 189 |
| 205 // BluetoothAdapterClient override. | 190 // BluetoothAdapterClient override. |
| 206 void SetDiscoveryFilter(const dbus::ObjectPath& object_path, | 191 void SetDiscoveryFilter(const dbus::ObjectPath& object_path, |
| 207 const DiscoveryFilter& discovery_filter, | 192 const DiscoveryFilter& discovery_filter, |
| 208 const base::Closure& callback, | 193 const base::Closure& callback, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 // Called by dbus::PropertySet when a property value is changed, | 296 // Called by dbus::PropertySet when a property value is changed, |
| 312 // either by result of a signal or response to a GetAll() or Get() | 297 // either by result of a signal or response to a GetAll() or Get() |
| 313 // call. Informs observers. | 298 // call. Informs observers. |
| 314 void OnPropertyChanged(const dbus::ObjectPath& object_path, | 299 void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 315 const std::string& property_name) { | 300 const std::string& property_name) { |
| 316 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 301 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, |
| 317 AdapterPropertyChanged(object_path, property_name)); | 302 AdapterPropertyChanged(object_path, property_name)); |
| 318 } | 303 } |
| 319 | 304 |
| 320 // Called when a response for successful method call is received. | 305 // Called when a response for successful method call is received. |
| 321 void OnSuccess(const base::Closure& callback, | 306 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
| 322 dbus::Response* response) { | |
| 323 DCHECK(response); | 307 DCHECK(response); |
| 324 callback.Run(); | 308 callback.Run(); |
| 325 } | 309 } |
| 326 | 310 |
| 327 // Called when a response for a failed method call is received. | 311 // Called when a response for a failed method call is received. |
| 328 void OnError(const ErrorCallback& error_callback, | 312 void OnError(const ErrorCallback& error_callback, |
| 329 dbus::ErrorResponse* response) { | 313 dbus::ErrorResponse* response) { |
| 330 // Error response has optional error message argument. | 314 // Error response has optional error message argument. |
| 331 std::string error_name; | 315 std::string error_name; |
| 332 std::string error_message; | 316 std::string error_message; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 343 | 327 |
| 344 dbus::ObjectManager* object_manager_; | 328 dbus::ObjectManager* object_manager_; |
| 345 | 329 |
| 346 // List of observers interested in event notifications from us. | 330 // List of observers interested in event notifications from us. |
| 347 base::ObserverList<BluetoothAdapterClient::Observer> observers_; | 331 base::ObserverList<BluetoothAdapterClient::Observer> observers_; |
| 348 | 332 |
| 349 // Weak pointer factory for generating 'this' pointers that might live longer | 333 // Weak pointer factory for generating 'this' pointers that might live longer |
| 350 // than we do. | 334 // than we do. |
| 351 // Note: This should remain the last member so it'll be destroyed and | 335 // Note: This should remain the last member so it'll be destroyed and |
| 352 // invalidate its weak pointers before any other members are destroyed. | 336 // invalidate its weak pointers before any other members are destroyed. |
| 353 base::WeakPtrFactory<BluetoothAdapterClientImpl> | 337 base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_; |
| 354 weak_ptr_factory_; | |
| 355 | 338 |
| 356 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl); | 339 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl); |
| 357 }; | 340 }; |
| 358 | 341 |
| 359 BluetoothAdapterClient::BluetoothAdapterClient() { | 342 BluetoothAdapterClient::BluetoothAdapterClient() {} |
| 360 } | |
| 361 | 343 |
| 362 BluetoothAdapterClient::~BluetoothAdapterClient() { | 344 BluetoothAdapterClient::~BluetoothAdapterClient() {} |
| 363 } | |
| 364 | 345 |
| 365 BluetoothAdapterClient* BluetoothAdapterClient::Create() { | 346 BluetoothAdapterClient* BluetoothAdapterClient::Create() { |
| 366 return new BluetoothAdapterClientImpl; | 347 return new BluetoothAdapterClientImpl; |
| 367 } | 348 } |
| 368 | 349 |
| 369 } // namespace chromeos | 350 } // namespace bluez |
| OLD | NEW |