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_device_client.h" | 5 #include "device/bluetooth/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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "dbus/bus.h" | 12 #include "dbus/bus.h" |
12 #include "dbus/message.h" | 13 #include "dbus/message.h" |
13 #include "dbus/object_manager.h" | 14 #include "dbus/object_manager.h" |
14 #include "dbus/object_proxy.h" | 15 #include "dbus/object_proxy.h" |
| 16 #include "device/bluetooth/bluez/bluetooth_service_attribute_value_bluez.h" |
15 #include "third_party/cros_system_api/dbus/service_constants.h" | 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
16 | 18 |
17 namespace bluez { | 19 namespace bluez { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 // Value returned for the the RSSI or TX power if it cannot be read. | 23 // Value returned for the the RSSI or TX power if it cannot be read. |
22 const int kUnknownPower = 127; | 24 const int kUnknownPower = 127; |
23 | 25 |
| 26 std::unique_ptr<BluetoothServiceAttributeValueBlueZ> ReadAttributeValue( |
| 27 dbus::MessageReader* struct_reader) { |
| 28 uint8_t type_val; |
| 29 if (!struct_reader->PopByte(&type_val)) |
| 30 return nullptr; |
| 31 BluetoothServiceAttributeValueBlueZ::Type type = |
| 32 static_cast<BluetoothServiceAttributeValueBlueZ::Type>(type_val); |
| 33 |
| 34 uint32_t size; |
| 35 if (!struct_reader->PopUint32(&size)) |
| 36 return nullptr; |
| 37 |
| 38 std::unique_ptr<base::Value> value = nullptr; |
| 39 switch (type) { |
| 40 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: { |
| 41 break; |
| 42 } |
| 43 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| 44 // Fall through. |
| 45 case bluez::BluetoothServiceAttributeValueBlueZ::INT: { |
| 46 switch (size) { |
| 47 // It doesn't matter what 'sign' the number is, only size. |
| 48 // Whenever we unpack this value, we will take the raw bits and |
| 49 // cast it back to the correct sign anyway. |
| 50 case 1: |
| 51 uint8_t byte; |
| 52 if (!struct_reader->PopVariantOfByte(&byte)) |
| 53 return nullptr; |
| 54 value = base::MakeUnique<base::FundamentalValue>(byte); |
| 55 break; |
| 56 case 2: |
| 57 uint16_t short_val; |
| 58 if (!struct_reader->PopVariantOfUint16(&short_val)) |
| 59 return nullptr; |
| 60 value = base::MakeUnique<base::FundamentalValue>(short_val); |
| 61 break; |
| 62 case 4: |
| 63 uint32_t val; |
| 64 if (!struct_reader->PopVariantOfUint32(&val)) |
| 65 return nullptr; |
| 66 value = base::MakeUnique<base::FundamentalValue>( |
| 67 static_cast<int32_t>(val)); |
| 68 break; |
| 69 case 8: |
| 70 // Fall through. |
| 71 // BlueZ should never be sending us this size at the moment since |
| 72 // the Android SDP records we will create from these raw records |
| 73 // don't have any fields which use this size. If we ever decide to |
| 74 // change this, this needs to get fixed. |
| 75 default: |
| 76 NOTREACHED(); |
| 77 } |
| 78 break; |
| 79 } |
| 80 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| 81 // Fall through. |
| 82 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| 83 // Fall through. |
| 84 case bluez::BluetoothServiceAttributeValueBlueZ::URL: { |
| 85 std::string str; |
| 86 if (!struct_reader->PopVariantOfString(&str)) |
| 87 return nullptr; |
| 88 value = base::MakeUnique<base::StringValue>(str); |
| 89 break; |
| 90 } |
| 91 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { |
| 92 bool b; |
| 93 if (!struct_reader->PopVariantOfBool(&b)) |
| 94 return nullptr; |
| 95 value = base::MakeUnique<base::FundamentalValue>(b); |
| 96 break; |
| 97 } |
| 98 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { |
| 99 dbus::MessageReader variant_reader(nullptr); |
| 100 if (!struct_reader->PopVariant(&variant_reader)) |
| 101 return nullptr; |
| 102 dbus::MessageReader array_reader(nullptr); |
| 103 if (!variant_reader.PopArray(&array_reader)) |
| 104 return nullptr; |
| 105 std::unique_ptr<BluetoothServiceAttributeValueBlueZ::Sequence> sequence = |
| 106 base::MakeUnique<BluetoothServiceAttributeValueBlueZ::Sequence>(); |
| 107 while (array_reader.HasMoreData()) { |
| 108 dbus::MessageReader sequence_element_struct_reader(nullptr); |
| 109 if (!array_reader.PopStruct(&sequence_element_struct_reader)) |
| 110 return nullptr; |
| 111 std::unique_ptr<BluetoothServiceAttributeValueBlueZ> attribute_value = |
| 112 ReadAttributeValue(&sequence_element_struct_reader); |
| 113 if (!attribute_value) |
| 114 return nullptr; |
| 115 sequence->emplace_back(*attribute_value); |
| 116 } |
| 117 return base::MakeUnique<BluetoothServiceAttributeValueBlueZ>( |
| 118 std::move(sequence)); |
| 119 } |
| 120 } |
| 121 return base::MakeUnique<BluetoothServiceAttributeValueBlueZ>( |
| 122 type, size, std::move(value)); |
| 123 } |
| 124 |
| 125 std::unique_ptr<BluetoothServiceRecordBlueZ> ReadRecord( |
| 126 dbus::MessageReader* array_reader) { |
| 127 std::unique_ptr<BluetoothServiceRecordBlueZ> record = |
| 128 base::MakeUnique<BluetoothServiceRecordBlueZ>(); |
| 129 while (array_reader->HasMoreData()) { |
| 130 dbus::MessageReader dict_entry_reader(nullptr); |
| 131 if (!array_reader->PopDictEntry(&dict_entry_reader)) |
| 132 return nullptr; |
| 133 uint16_t id; |
| 134 if (!dict_entry_reader.PopUint16(&id)) |
| 135 return nullptr; |
| 136 dbus::MessageReader struct_reader(nullptr); |
| 137 if (!dict_entry_reader.PopStruct(&struct_reader)) |
| 138 return nullptr; |
| 139 std::unique_ptr<BluetoothServiceAttributeValueBlueZ> attribute_value = |
| 140 ReadAttributeValue(&struct_reader); |
| 141 if (!attribute_value) |
| 142 return nullptr; |
| 143 record->AddRecordEntry(id, *attribute_value); |
| 144 } |
| 145 // return std::move(record); |
| 146 return record; |
| 147 } |
| 148 |
| 149 bool ReadRecordsFromMessage(dbus::MessageReader* reader, |
| 150 BluetoothDeviceClient::ServiceRecordList* records) { |
| 151 dbus::MessageReader array_reader(nullptr); |
| 152 if (!reader->PopArray(&array_reader)) { |
| 153 return false; |
| 154 LOG(ERROR) << "Arguments for GetConnInfo invalid."; |
| 155 } |
| 156 while (array_reader.HasMoreData()) { |
| 157 dbus::MessageReader nested_array_reader(nullptr); |
| 158 if (!array_reader.PopArray(&nested_array_reader)) |
| 159 return false; |
| 160 std::unique_ptr<BluetoothServiceRecordBlueZ> record = |
| 161 ReadRecord(&nested_array_reader); |
| 162 if (!record) |
| 163 return false; |
| 164 records->emplace_back(*record); |
| 165 } |
| 166 return true; |
| 167 } |
| 168 |
24 } // namespace | 169 } // namespace |
25 | 170 |
26 const char BluetoothDeviceClient::kNoResponseError[] = | 171 const char BluetoothDeviceClient::kNoResponseError[] = |
27 "org.chromium.Error.NoResponse"; | 172 "org.chromium.Error.NoResponse"; |
28 const char BluetoothDeviceClient::kUnknownDeviceError[] = | 173 const char BluetoothDeviceClient::kUnknownDeviceError[] = |
29 "org.chromium.Error.UnknownDevice"; | 174 "org.chromium.Error.UnknownDevice"; |
30 | 175 |
31 const char BluetoothDeviceClient::kTypeBredr[] = "BR/EDR"; | 176 const char BluetoothDeviceClient::kTypeBredr[] = "BR/EDR"; |
32 const char BluetoothDeviceClient::kTypeLe[] = "LE"; | 177 const char BluetoothDeviceClient::kTypeLe[] = "LE"; |
33 const char BluetoothDeviceClient::kTypeDual[] = "DUAL"; | 178 const char BluetoothDeviceClient::kTypeDual[] = "DUAL"; |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 return; | 419 return; |
275 } | 420 } |
276 object_proxy->CallMethodWithErrorCallback( | 421 object_proxy->CallMethodWithErrorCallback( |
277 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 422 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
278 base::Bind(&BluetoothDeviceClientImpl::OnGetConnInfoSuccess, | 423 base::Bind(&BluetoothDeviceClientImpl::OnGetConnInfoSuccess, |
279 weak_ptr_factory_.GetWeakPtr(), callback), | 424 weak_ptr_factory_.GetWeakPtr(), callback), |
280 base::Bind(&BluetoothDeviceClientImpl::OnError, | 425 base::Bind(&BluetoothDeviceClientImpl::OnError, |
281 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 426 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
282 } | 427 } |
283 | 428 |
| 429 void GetServiceRecords(const dbus::ObjectPath& object_path, |
| 430 const ServiceRecordsCallback& callback, |
| 431 const ErrorCallback& error_callback) override { |
| 432 dbus::MethodCall method_call(bluetooth_device::kBluetoothDeviceInterface, |
| 433 bluetooth_device::kGetServiceRecords); |
| 434 |
| 435 dbus::ObjectProxy* object_proxy = |
| 436 object_manager_->GetObjectProxy(object_path); |
| 437 if (!object_proxy) { |
| 438 error_callback.Run(kUnknownDeviceError, ""); |
| 439 return; |
| 440 } |
| 441 object_proxy->CallMethodWithErrorCallback( |
| 442 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 443 base::Bind(&BluetoothDeviceClientImpl::OnGetServiceRecordsSuccess, |
| 444 weak_ptr_factory_.GetWeakPtr(), callback), |
| 445 base::Bind(&BluetoothDeviceClientImpl::OnError, |
| 446 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 447 } |
| 448 |
284 protected: | 449 protected: |
285 void Init(dbus::Bus* bus) override { | 450 void Init(dbus::Bus* bus) override { |
286 object_manager_ = bus->GetObjectManager( | 451 object_manager_ = bus->GetObjectManager( |
287 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | 452 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
288 dbus::ObjectPath( | 453 dbus::ObjectPath( |
289 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | 454 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
290 object_manager_->RegisterInterface( | 455 object_manager_->RegisterInterface( |
291 bluetooth_device::kBluetoothDeviceInterface, this); | 456 bluetooth_device::kBluetoothDeviceInterface, this); |
292 } | 457 } |
293 | 458 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 } | 502 } |
338 | 503 |
339 dbus::MessageReader reader(response); | 504 dbus::MessageReader reader(response); |
340 if (!reader.PopInt16(&rssi) || !reader.PopInt16(&transmit_power) || | 505 if (!reader.PopInt16(&rssi) || !reader.PopInt16(&transmit_power) || |
341 !reader.PopInt16(&max_transmit_power)) { | 506 !reader.PopInt16(&max_transmit_power)) { |
342 LOG(ERROR) << "Arguments for GetConnInfo invalid."; | 507 LOG(ERROR) << "Arguments for GetConnInfo invalid."; |
343 } | 508 } |
344 callback.Run(rssi, transmit_power, max_transmit_power); | 509 callback.Run(rssi, transmit_power, max_transmit_power); |
345 } | 510 } |
346 | 511 |
| 512 void OnGetServiceRecordsSuccess(const ServiceRecordsCallback& callback, |
| 513 dbus::Response* response) { |
| 514 ServiceRecordList records; |
| 515 if (!response) { |
| 516 LOG(ERROR) << "GetServiceRecords succeeded, but no response received."; |
| 517 callback.Run(records); |
| 518 return; |
| 519 } |
| 520 |
| 521 dbus::MessageReader reader(response); |
| 522 if (!ReadRecordsFromMessage(&reader, &records)) { |
| 523 callback.Run(ServiceRecordList()); |
| 524 } |
| 525 |
| 526 callback.Run(records); |
| 527 } |
| 528 |
347 // Called when a response for a failed method call is received. | 529 // Called when a response for a failed method call is received. |
348 void OnError(const ErrorCallback& error_callback, | 530 void OnError(const ErrorCallback& error_callback, |
349 dbus::ErrorResponse* response) { | 531 dbus::ErrorResponse* response) { |
350 // Error response has optional error message argument. | 532 // Error response has optional error message argument. |
351 std::string error_name; | 533 std::string error_name; |
352 std::string error_message; | 534 std::string error_message; |
353 if (response) { | 535 if (response) { |
354 dbus::MessageReader reader(response); | 536 dbus::MessageReader reader(response); |
355 error_name = response->GetErrorName(); | 537 error_name = response->GetErrorName(); |
356 reader.PopString(&error_message); | 538 reader.PopString(&error_message); |
(...skipping 20 matching lines...) Expand all Loading... |
377 | 559 |
378 BluetoothDeviceClient::BluetoothDeviceClient() {} | 560 BluetoothDeviceClient::BluetoothDeviceClient() {} |
379 | 561 |
380 BluetoothDeviceClient::~BluetoothDeviceClient() {} | 562 BluetoothDeviceClient::~BluetoothDeviceClient() {} |
381 | 563 |
382 BluetoothDeviceClient* BluetoothDeviceClient::Create() { | 564 BluetoothDeviceClient* BluetoothDeviceClient::Create() { |
383 return new BluetoothDeviceClientImpl(); | 565 return new BluetoothDeviceClientImpl(); |
384 } | 566 } |
385 | 567 |
386 } // namespace bluez | 568 } // namespace bluez |
OLD | NEW |