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