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 "device/bluetooth/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/callback.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/macros.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/values.h" |
10 #include "dbus/bus.h" | 13 #include "dbus/bus.h" |
11 #include "dbus/message.h" | 14 #include "dbus/message.h" |
12 #include "dbus/object_manager.h" | 15 #include "dbus/object_manager.h" |
13 #include "dbus/object_proxy.h" | 16 #include "dbus/object_proxy.h" |
| 17 #include "dbus/values_util.h" |
| 18 #include "device/bluetooth/bluez/bluetooth_service_attribute_value_bluez.h" |
| 19 #include "device/bluetooth/bluez/bluetooth_service_record_bluez.h" |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
15 | 21 |
16 namespace bluez { | 22 namespace bluez { |
17 | 23 |
| 24 namespace { |
| 25 |
| 26 void WriteAttribute(dbus::MessageWriter* writer, |
| 27 const BluetoothServiceAttributeValueBlueZ& attribute) { |
| 28 dbus::MessageWriter struct_writer(nullptr); |
| 29 writer->OpenStruct(&struct_writer); |
| 30 struct_writer.AppendByte(attribute.type()); |
| 31 struct_writer.AppendUint32(attribute.size()); |
| 32 |
| 33 if (attribute.type() != BluetoothServiceAttributeValueBlueZ::SEQUENCE) { |
| 34 dbus::AppendValueDataAsVariant(&struct_writer, attribute.value()); |
| 35 } else { |
| 36 dbus::MessageWriter array_writer(nullptr); |
| 37 struct_writer.OpenArray("v", &array_writer); |
| 38 for (const auto& v : attribute.sequence()) |
| 39 WriteAttribute(&array_writer, v); |
| 40 struct_writer.CloseContainer(&array_writer); |
| 41 } |
| 42 writer->CloseContainer(&struct_writer); |
| 43 } |
| 44 } |
| 45 |
18 BluetoothAdapterClient::DiscoveryFilter::DiscoveryFilter() {} | 46 BluetoothAdapterClient::DiscoveryFilter::DiscoveryFilter() {} |
19 | 47 |
20 BluetoothAdapterClient::DiscoveryFilter::~DiscoveryFilter() {} | 48 BluetoothAdapterClient::DiscoveryFilter::~DiscoveryFilter() {} |
21 | 49 |
22 void BluetoothAdapterClient::DiscoveryFilter::CopyFrom( | 50 void BluetoothAdapterClient::DiscoveryFilter::CopyFrom( |
23 const DiscoveryFilter& filter) { | 51 const DiscoveryFilter& filter) { |
24 if (filter.rssi.get()) | 52 if (filter.rssi.get()) |
25 rssi.reset(new int16_t(*filter.rssi)); | 53 rssi.reset(new int16_t(*filter.rssi)); |
26 else | 54 else |
27 rssi.reset(); | 55 rssi.reset(); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 writer.CloseContainer(&dict_writer); | 288 writer.CloseContainer(&dict_writer); |
261 | 289 |
262 object_proxy->CallMethodWithErrorCallback( | 290 object_proxy->CallMethodWithErrorCallback( |
263 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 291 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
264 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, | 292 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, |
265 weak_ptr_factory_.GetWeakPtr(), callback), | 293 weak_ptr_factory_.GetWeakPtr(), callback), |
266 base::Bind(&BluetoothAdapterClientImpl::OnError, | 294 base::Bind(&BluetoothAdapterClientImpl::OnError, |
267 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 295 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
268 } | 296 } |
269 | 297 |
| 298 // BluetoothAdapterClient override. |
| 299 void CreateServiceRecord(const dbus::ObjectPath& object_path, |
| 300 const bluez::BluetoothServiceRecordBlueZ& record, |
| 301 const ServiceRecordCallback& callback, |
| 302 const ErrorCallback& error_callback) override { |
| 303 dbus::MethodCall method_call(bluetooth_adapter::kBluetoothAdapterInterface, |
| 304 bluetooth_adapter::kCreateServiceRecord); |
| 305 |
| 306 dbus::MessageWriter writer(&method_call); |
| 307 dbus::MessageWriter dict_entry_writer(nullptr); |
| 308 for (auto attribute_id : record.GetAttributeIds()) { |
| 309 writer.OpenDictEntry(&dict_entry_writer); |
| 310 dict_entry_writer.AppendUint16(attribute_id); |
| 311 const BluetoothServiceAttributeValueBlueZ& attribute_value = |
| 312 record.GetAttributeValue(attribute_id); |
| 313 WriteAttribute(&dict_entry_writer, attribute_value); |
| 314 } |
| 315 |
| 316 dbus::ObjectProxy* object_proxy = |
| 317 object_manager_->GetObjectProxy(object_path); |
| 318 if (!object_proxy) { |
| 319 error_callback.Run(kUnknownAdapterError, ""); |
| 320 return; |
| 321 } |
| 322 |
| 323 object_proxy->CallMethodWithErrorCallback( |
| 324 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 325 base::Bind(&BluetoothAdapterClientImpl::OnCreateServiceRecord, |
| 326 weak_ptr_factory_.GetWeakPtr(), callback), |
| 327 base::Bind(&BluetoothAdapterClientImpl::OnError, |
| 328 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 329 } |
| 330 |
| 331 // BluetoothAdapterClient override. |
| 332 void RemoveServiceRecord(const dbus::ObjectPath& object_path, |
| 333 uint32_t handle, |
| 334 const base::Closure& callback, |
| 335 const ErrorCallback& error_callback) override { |
| 336 dbus::MethodCall method_call(bluetooth_adapter::kBluetoothAdapterInterface, |
| 337 bluetooth_adapter::kRemoveServiceRecord); |
| 338 |
| 339 dbus::MessageWriter writer(&method_call); |
| 340 writer.AppendUint32(handle); |
| 341 dbus::ObjectProxy* object_proxy = |
| 342 object_manager_->GetObjectProxy(object_path); |
| 343 if (!object_proxy) { |
| 344 error_callback.Run(kUnknownAdapterError, ""); |
| 345 return; |
| 346 } |
| 347 |
| 348 object_proxy->CallMethodWithErrorCallback( |
| 349 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 350 base::Bind(&BluetoothAdapterClientImpl::OnSuccess, |
| 351 weak_ptr_factory_.GetWeakPtr(), callback), |
| 352 base::Bind(&BluetoothAdapterClientImpl::OnError, |
| 353 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 354 } |
| 355 |
270 protected: | 356 protected: |
271 void Init(dbus::Bus* bus) override { | 357 void Init(dbus::Bus* bus) override { |
272 object_manager_ = bus->GetObjectManager( | 358 object_manager_ = bus->GetObjectManager( |
273 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | 359 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
274 dbus::ObjectPath( | 360 dbus::ObjectPath( |
275 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | 361 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
276 object_manager_->RegisterInterface( | 362 object_manager_->RegisterInterface( |
277 bluetooth_adapter::kBluetoothAdapterInterface, this); | 363 bluetooth_adapter::kBluetoothAdapterInterface, this); |
278 } | 364 } |
279 | 365 |
(...skipping 17 matching lines...) Expand all Loading... |
297 // Called by dbus::PropertySet when a property value is changed, | 383 // Called by dbus::PropertySet when a property value is changed, |
298 // either by result of a signal or response to a GetAll() or Get() | 384 // either by result of a signal or response to a GetAll() or Get() |
299 // call. Informs observers. | 385 // call. Informs observers. |
300 void OnPropertyChanged(const dbus::ObjectPath& object_path, | 386 void OnPropertyChanged(const dbus::ObjectPath& object_path, |
301 const std::string& property_name) { | 387 const std::string& property_name) { |
302 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 388 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, |
303 AdapterPropertyChanged(object_path, property_name)); | 389 AdapterPropertyChanged(object_path, property_name)); |
304 } | 390 } |
305 | 391 |
306 // Called when a response for successful method call is received. | 392 // Called when a response for successful method call is received. |
| 393 void OnCreateServiceRecord(const ServiceRecordCallback& callback, |
| 394 dbus::Response* response) { |
| 395 DCHECK(response); |
| 396 dbus::MessageReader reader(response); |
| 397 uint32_t handle = 0; |
| 398 if (!reader.PopUint32(&handle)) |
| 399 LOG(ERROR) << "Invalid response from CreateServiceRecord."; |
| 400 callback.Run(handle); |
| 401 } |
| 402 |
| 403 // Called when a response for successful method call is received. |
307 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | 404 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
308 DCHECK(response); | 405 DCHECK(response); |
309 callback.Run(); | 406 callback.Run(); |
310 } | 407 } |
311 | 408 |
312 // Called when a response for a failed method call is received. | 409 // Called when a response for a failed method call is received. |
313 void OnError(const ErrorCallback& error_callback, | 410 void OnError(const ErrorCallback& error_callback, |
314 dbus::ErrorResponse* response) { | 411 dbus::ErrorResponse* response) { |
315 // Error response has optional error message argument. | 412 // Error response has optional error message argument. |
316 std::string error_name; | 413 std::string error_name; |
(...skipping 25 matching lines...) Expand all Loading... |
342 | 439 |
343 BluetoothAdapterClient::BluetoothAdapterClient() {} | 440 BluetoothAdapterClient::BluetoothAdapterClient() {} |
344 | 441 |
345 BluetoothAdapterClient::~BluetoothAdapterClient() {} | 442 BluetoothAdapterClient::~BluetoothAdapterClient() {} |
346 | 443 |
347 BluetoothAdapterClient* BluetoothAdapterClient::Create() { | 444 BluetoothAdapterClient* BluetoothAdapterClient::Create() { |
348 return new BluetoothAdapterClientImpl; | 445 return new BluetoothAdapterClientImpl; |
349 } | 446 } |
350 | 447 |
351 } // namespace bluez | 448 } // namespace bluez |
OLD | NEW |