Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: device/bluetooth/dbus/bluetooth_device_client.cc

Issue 2102093003: Implement BluetoothDeviceBlueZ::GetServiceRecords. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 break;
xiyuan 2016/06/29 22:43:08 nit: no "break" since we "return" ?
rkc 2016/06/30 20:02:24 Done.
120 }
121 }
122 return base::MakeUnique<BluetoothServiceAttributeValueBlueZ>(
123 type, size, std::move(value));
124 }
125
126 std::unique_ptr<BluetoothServiceRecordBlueZ> ReadRecord(
127 dbus::MessageReader* array_reader) {
128 std::unique_ptr<BluetoothServiceRecordBlueZ> record =
129 base::MakeUnique<BluetoothServiceRecordBlueZ>();
130 while (array_reader->HasMoreData()) {
131 dbus::MessageReader dict_entry_reader(nullptr);
132 if (!array_reader->PopDictEntry(&dict_entry_reader))
133 return nullptr;
134 uint16_t id;
135 if (!dict_entry_reader.PopUint16(&id))
136 return nullptr;
137 dbus::MessageReader struct_reader(nullptr);
138 if (!dict_entry_reader.PopStruct(&struct_reader))
139 return nullptr;
140 std::unique_ptr<BluetoothServiceAttributeValueBlueZ> attribute_value =
141 ReadAttributeValue(&struct_reader);
142 if (!attribute_value)
143 return nullptr;
144 record->AddRecordEntry(id, *attribute_value);
145 }
146 // return std::move(record);
147 return record;
148 }
149
150 bool ReadRecordsFromMessage(dbus::MessageReader* reader,
151 BluetoothDeviceClient::ServiceRecordList* records) {
152 dbus::MessageReader array_reader(nullptr);
153 if (!reader->PopArray(&array_reader)) {
154 return false;
155 LOG(ERROR) << "Arguments for GetConnInfo invalid.";
156 }
157 while (array_reader.HasMoreData()) {
158 dbus::MessageReader nested_array_reader(nullptr);
159 if (!array_reader.PopArray(&nested_array_reader))
160 return false;
161 std::unique_ptr<BluetoothServiceRecordBlueZ> record =
162 ReadRecord(&nested_array_reader);
163 if (!record)
164 return false;
165 records->emplace_back(*record);
166 }
167 return true;
168 }
169
24 } // namespace 170 } // namespace
25 171
26 const char BluetoothDeviceClient::kNoResponseError[] = 172 const char BluetoothDeviceClient::kNoResponseError[] =
27 "org.chromium.Error.NoResponse"; 173 "org.chromium.Error.NoResponse";
28 const char BluetoothDeviceClient::kUnknownDeviceError[] = 174 const char BluetoothDeviceClient::kUnknownDeviceError[] =
29 "org.chromium.Error.UnknownDevice"; 175 "org.chromium.Error.UnknownDevice";
30 176
31 const char BluetoothDeviceClient::kTypeBredr[] = "BR/EDR"; 177 const char BluetoothDeviceClient::kTypeBredr[] = "BR/EDR";
32 const char BluetoothDeviceClient::kTypeLe[] = "LE"; 178 const char BluetoothDeviceClient::kTypeLe[] = "LE";
33 const char BluetoothDeviceClient::kTypeDual[] = "DUAL"; 179 const char BluetoothDeviceClient::kTypeDual[] = "DUAL";
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 return; 420 return;
275 } 421 }
276 object_proxy->CallMethodWithErrorCallback( 422 object_proxy->CallMethodWithErrorCallback(
277 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 423 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
278 base::Bind(&BluetoothDeviceClientImpl::OnGetConnInfoSuccess, 424 base::Bind(&BluetoothDeviceClientImpl::OnGetConnInfoSuccess,
279 weak_ptr_factory_.GetWeakPtr(), callback), 425 weak_ptr_factory_.GetWeakPtr(), callback),
280 base::Bind(&BluetoothDeviceClientImpl::OnError, 426 base::Bind(&BluetoothDeviceClientImpl::OnError,
281 weak_ptr_factory_.GetWeakPtr(), error_callback)); 427 weak_ptr_factory_.GetWeakPtr(), error_callback));
282 } 428 }
283 429
430 void GetServiceRecords(const dbus::ObjectPath& object_path,
431 const ServiceRecordsCallback& callback,
432 const ErrorCallback& error_callback) override {
433 dbus::MethodCall method_call(bluetooth_device::kBluetoothDeviceInterface,
434 bluetooth_device::kGetServiceRecords);
435
436 dbus::ObjectProxy* object_proxy =
437 object_manager_->GetObjectProxy(object_path);
438 if (!object_proxy) {
439 error_callback.Run(kUnknownDeviceError, "");
440 return;
441 }
442 object_proxy->CallMethodWithErrorCallback(
443 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
444 base::Bind(&BluetoothDeviceClientImpl::OnGetServiceRecordsSuccess,
445 weak_ptr_factory_.GetWeakPtr(), callback),
446 base::Bind(&BluetoothDeviceClientImpl::OnError,
447 weak_ptr_factory_.GetWeakPtr(), error_callback));
448 }
449
284 protected: 450 protected:
285 void Init(dbus::Bus* bus) override { 451 void Init(dbus::Bus* bus) override {
286 object_manager_ = bus->GetObjectManager( 452 object_manager_ = bus->GetObjectManager(
287 bluetooth_object_manager::kBluetoothObjectManagerServiceName, 453 bluetooth_object_manager::kBluetoothObjectManagerServiceName,
288 dbus::ObjectPath( 454 dbus::ObjectPath(
289 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); 455 bluetooth_object_manager::kBluetoothObjectManagerServicePath));
290 object_manager_->RegisterInterface( 456 object_manager_->RegisterInterface(
291 bluetooth_device::kBluetoothDeviceInterface, this); 457 bluetooth_device::kBluetoothDeviceInterface, this);
292 } 458 }
293 459
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 } 503 }
338 504
339 dbus::MessageReader reader(response); 505 dbus::MessageReader reader(response);
340 if (!reader.PopInt16(&rssi) || !reader.PopInt16(&transmit_power) || 506 if (!reader.PopInt16(&rssi) || !reader.PopInt16(&transmit_power) ||
341 !reader.PopInt16(&max_transmit_power)) { 507 !reader.PopInt16(&max_transmit_power)) {
342 LOG(ERROR) << "Arguments for GetConnInfo invalid."; 508 LOG(ERROR) << "Arguments for GetConnInfo invalid.";
343 } 509 }
344 callback.Run(rssi, transmit_power, max_transmit_power); 510 callback.Run(rssi, transmit_power, max_transmit_power);
345 } 511 }
346 512
513 void OnGetServiceRecordsSuccess(const ServiceRecordsCallback& callback,
514 dbus::Response* response) {
515 ServiceRecordList records;
516 if (!response) {
517 LOG(ERROR) << "GetServiceRecords succeeded, but no response received.";
518 callback.Run(records);
519 return;
520 }
521
522 dbus::MessageReader reader(response);
523 if (!ReadRecordsFromMessage(&reader, &records)) {
524 callback.Run(ServiceRecordList());
525 }
526
527 callback.Run(records);
528 }
529
347 // Called when a response for a failed method call is received. 530 // Called when a response for a failed method call is received.
348 void OnError(const ErrorCallback& error_callback, 531 void OnError(const ErrorCallback& error_callback,
349 dbus::ErrorResponse* response) { 532 dbus::ErrorResponse* response) {
350 // Error response has optional error message argument. 533 // Error response has optional error message argument.
351 std::string error_name; 534 std::string error_name;
352 std::string error_message; 535 std::string error_message;
353 if (response) { 536 if (response) {
354 dbus::MessageReader reader(response); 537 dbus::MessageReader reader(response);
355 error_name = response->GetErrorName(); 538 error_name = response->GetErrorName();
356 reader.PopString(&error_message); 539 reader.PopString(&error_message);
(...skipping 20 matching lines...) Expand all
377 560
378 BluetoothDeviceClient::BluetoothDeviceClient() {} 561 BluetoothDeviceClient::BluetoothDeviceClient() {}
379 562
380 BluetoothDeviceClient::~BluetoothDeviceClient() {} 563 BluetoothDeviceClient::~BluetoothDeviceClient() {}
381 564
382 BluetoothDeviceClient* BluetoothDeviceClient::Create() { 565 BluetoothDeviceClient* BluetoothDeviceClient::Create() {
383 return new BluetoothDeviceClientImpl(); 566 return new BluetoothDeviceClientImpl();
384 } 567 }
385 568
386 } // namespace bluez 569 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698