OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_gatt_descriptor_service_provider.h" | 5 #include "chromeos/dbus/bluetooth_gatt_descriptor_service_provider.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/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 dbus::MessageReader reader(method_call); | 135 dbus::MessageReader reader(method_call); |
136 | 136 |
137 std::string interface_name; | 137 std::string interface_name; |
138 std::string property_name; | 138 std::string property_name; |
139 if (!reader.PopString(&interface_name) || | 139 if (!reader.PopString(&interface_name) || |
140 !reader.PopString(&property_name) || | 140 !reader.PopString(&property_name) || |
141 reader.HasMoreData()) { | 141 reader.HasMoreData()) { |
142 scoped_ptr<dbus::ErrorResponse> error_response = | 142 scoped_ptr<dbus::ErrorResponse> error_response = |
143 dbus::ErrorResponse::FromMethodCall( | 143 dbus::ErrorResponse::FromMethodCall( |
144 method_call, kErrorInvalidArgs, "Expected 'ss'."); | 144 method_call, kErrorInvalidArgs, "Expected 'ss'."); |
145 response_sender.Run(error_response.PassAs<dbus::Response>()); | 145 response_sender.Run(error_response.Pass()); |
146 return; | 146 return; |
147 } | 147 } |
148 | 148 |
149 // Only the GATT descriptor interface is supported. | 149 // Only the GATT descriptor interface is supported. |
150 if (interface_name != | 150 if (interface_name != |
151 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) { | 151 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) { |
152 scoped_ptr<dbus::ErrorResponse> error_response = | 152 scoped_ptr<dbus::ErrorResponse> error_response = |
153 dbus::ErrorResponse::FromMethodCall( | 153 dbus::ErrorResponse::FromMethodCall( |
154 method_call, kErrorInvalidArgs, | 154 method_call, kErrorInvalidArgs, |
155 "No such interface: '" + interface_name + "'."); | 155 "No such interface: '" + interface_name + "'."); |
156 response_sender.Run(error_response.PassAs<dbus::Response>()); | 156 response_sender.Run(error_response.Pass()); |
157 return; | 157 return; |
158 } | 158 } |
159 | 159 |
160 // If getting the "Value" property, obtain the value from the delegate. | 160 // If getting the "Value" property, obtain the value from the delegate. |
161 if (property_name == bluetooth_gatt_descriptor::kValueProperty) { | 161 if (property_name == bluetooth_gatt_descriptor::kValueProperty) { |
162 DCHECK(delegate_); | 162 DCHECK(delegate_); |
163 delegate_->GetDescriptorValue( | 163 delegate_->GetDescriptorValue( |
164 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGet, | 164 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGet, |
165 weak_ptr_factory_.GetWeakPtr(), | 165 weak_ptr_factory_.GetWeakPtr(), |
166 method_call, response_sender), | 166 method_call, response_sender), |
(...skipping 13 matching lines...) Expand all Loading... |
180 writer.OpenVariant("s", &variant_writer); | 180 writer.OpenVariant("s", &variant_writer); |
181 variant_writer.AppendString(uuid_); | 181 variant_writer.AppendString(uuid_); |
182 writer.CloseContainer(&variant_writer); | 182 writer.CloseContainer(&variant_writer); |
183 } else if (property_name == | 183 } else if (property_name == |
184 bluetooth_gatt_descriptor::kCharacteristicProperty) { | 184 bluetooth_gatt_descriptor::kCharacteristicProperty) { |
185 writer.OpenVariant("o", &variant_writer); | 185 writer.OpenVariant("o", &variant_writer); |
186 variant_writer.AppendObjectPath(characteristic_path_); | 186 variant_writer.AppendObjectPath(characteristic_path_); |
187 writer.CloseContainer(&variant_writer); | 187 writer.CloseContainer(&variant_writer); |
188 } else { | 188 } else { |
189 response = dbus::ErrorResponse::FromMethodCall( | 189 response = dbus::ErrorResponse::FromMethodCall( |
190 method_call, kErrorInvalidArgs, | 190 method_call, |
191 "No such property: '" + property_name + "'.") | 191 kErrorInvalidArgs, |
192 .PassAs<dbus::Response>(); | 192 "No such property: '" + property_name + "'."); |
193 } | 193 } |
194 | 194 |
195 response_sender.Run(response.Pass()); | 195 response_sender.Run(response.Pass()); |
196 } | 196 } |
197 | 197 |
198 // Called by dbus:: when the Bluetooth daemon sets a single property of the | 198 // Called by dbus:: when the Bluetooth daemon sets a single property of the |
199 // descriptor. | 199 // descriptor. |
200 void Set(dbus::MethodCall* method_call, | 200 void Set(dbus::MethodCall* method_call, |
201 dbus::ExportedObject::ResponseSender response_sender) { | 201 dbus::ExportedObject::ResponseSender response_sender) { |
202 VLOG(2) << "BluetoothGattDescriptorServiceProvider::Set: " | 202 VLOG(2) << "BluetoothGattDescriptorServiceProvider::Set: " |
203 << object_path_.value(); | 203 << object_path_.value(); |
204 DCHECK(OnOriginThread()); | 204 DCHECK(OnOriginThread()); |
205 | 205 |
206 dbus::MessageReader reader(method_call); | 206 dbus::MessageReader reader(method_call); |
207 | 207 |
208 std::string interface_name; | 208 std::string interface_name; |
209 std::string property_name; | 209 std::string property_name; |
210 dbus::MessageReader variant_reader(NULL); | 210 dbus::MessageReader variant_reader(NULL); |
211 if (!reader.PopString(&interface_name) || | 211 if (!reader.PopString(&interface_name) || |
212 !reader.PopString(&property_name) || | 212 !reader.PopString(&property_name) || |
213 !reader.PopVariant(&variant_reader) || | 213 !reader.PopVariant(&variant_reader) || |
214 reader.HasMoreData()) { | 214 reader.HasMoreData()) { |
215 scoped_ptr<dbus::ErrorResponse> error_response = | 215 scoped_ptr<dbus::ErrorResponse> error_response = |
216 dbus::ErrorResponse::FromMethodCall( | 216 dbus::ErrorResponse::FromMethodCall( |
217 method_call, kErrorInvalidArgs, "Expected 'ssv'."); | 217 method_call, kErrorInvalidArgs, "Expected 'ssv'."); |
218 response_sender.Run(error_response.PassAs<dbus::Response>()); | 218 response_sender.Run(error_response.Pass()); |
219 return; | 219 return; |
220 } | 220 } |
221 | 221 |
222 // Only the GATT descriptor interface is allowed. | 222 // Only the GATT descriptor interface is allowed. |
223 if (interface_name != | 223 if (interface_name != |
224 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) { | 224 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) { |
225 scoped_ptr<dbus::ErrorResponse> error_response = | 225 scoped_ptr<dbus::ErrorResponse> error_response = |
226 dbus::ErrorResponse::FromMethodCall( | 226 dbus::ErrorResponse::FromMethodCall( |
227 method_call, kErrorInvalidArgs, | 227 method_call, kErrorInvalidArgs, |
228 "No such interface: '" + interface_name + "'."); | 228 "No such interface: '" + interface_name + "'."); |
229 response_sender.Run(error_response.PassAs<dbus::Response>()); | 229 response_sender.Run(error_response.Pass()); |
230 return; | 230 return; |
231 } | 231 } |
232 | 232 |
233 // Only the "Value" property is writeable. | 233 // Only the "Value" property is writeable. |
234 if (property_name != bluetooth_gatt_descriptor::kValueProperty) { | 234 if (property_name != bluetooth_gatt_descriptor::kValueProperty) { |
235 std::string error_name; | 235 std::string error_name; |
236 std::string error_message; | 236 std::string error_message; |
237 if (property_name == bluetooth_gatt_descriptor::kUUIDProperty || | 237 if (property_name == bluetooth_gatt_descriptor::kUUIDProperty || |
238 property_name == bluetooth_gatt_descriptor::kCharacteristicProperty) { | 238 property_name == bluetooth_gatt_descriptor::kCharacteristicProperty) { |
239 error_name = kErrorPropertyReadOnly; | 239 error_name = kErrorPropertyReadOnly; |
240 error_message = "Read-only property: '" + property_name + "'."; | 240 error_message = "Read-only property: '" + property_name + "'."; |
241 } else { | 241 } else { |
242 error_name = kErrorInvalidArgs; | 242 error_name = kErrorInvalidArgs; |
243 error_message = "No such property: '" + property_name + "'."; | 243 error_message = "No such property: '" + property_name + "'."; |
244 } | 244 } |
245 scoped_ptr<dbus::ErrorResponse> error_response = | 245 scoped_ptr<dbus::ErrorResponse> error_response = |
246 dbus::ErrorResponse::FromMethodCall( | 246 dbus::ErrorResponse::FromMethodCall( |
247 method_call, error_name, error_message); | 247 method_call, error_name, error_message); |
248 response_sender.Run(error_response.PassAs<dbus::Response>()); | 248 response_sender.Run(error_response.Pass()); |
249 return; | 249 return; |
250 } | 250 } |
251 | 251 |
252 // Obtain the value. | 252 // Obtain the value. |
253 const uint8* bytes = NULL; | 253 const uint8* bytes = NULL; |
254 size_t length = 0; | 254 size_t length = 0; |
255 if (!variant_reader.PopArrayOfBytes(&bytes, &length)) { | 255 if (!variant_reader.PopArrayOfBytes(&bytes, &length)) { |
256 scoped_ptr<dbus::ErrorResponse> error_response = | 256 scoped_ptr<dbus::ErrorResponse> error_response = |
257 dbus::ErrorResponse::FromMethodCall( | 257 dbus::ErrorResponse::FromMethodCall( |
258 method_call, kErrorInvalidArgs, | 258 method_call, kErrorInvalidArgs, |
259 "Property '" + property_name + "' has type 'ay'."); | 259 "Property '" + property_name + "' has type 'ay'."); |
260 response_sender.Run(error_response.PassAs<dbus::Response>()); | 260 response_sender.Run(error_response.Pass()); |
261 return; | 261 return; |
262 } | 262 } |
263 | 263 |
264 // Pass the set request onto the delegate. | 264 // Pass the set request onto the delegate. |
265 std::vector<uint8> value(bytes, bytes + length); | 265 std::vector<uint8> value(bytes, bytes + length); |
266 DCHECK(delegate_); | 266 DCHECK(delegate_); |
267 delegate_->SetDescriptorValue( | 267 delegate_->SetDescriptorValue( |
268 value, | 268 value, |
269 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnSet, | 269 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnSet, |
270 weak_ptr_factory_.GetWeakPtr(), | 270 weak_ptr_factory_.GetWeakPtr(), |
(...skipping 11 matching lines...) Expand all Loading... |
282 << object_path_.value(); | 282 << object_path_.value(); |
283 DCHECK(OnOriginThread()); | 283 DCHECK(OnOriginThread()); |
284 | 284 |
285 dbus::MessageReader reader(method_call); | 285 dbus::MessageReader reader(method_call); |
286 | 286 |
287 std::string interface_name; | 287 std::string interface_name; |
288 if (!reader.PopString(&interface_name) || reader.HasMoreData()) { | 288 if (!reader.PopString(&interface_name) || reader.HasMoreData()) { |
289 scoped_ptr<dbus::ErrorResponse> error_response = | 289 scoped_ptr<dbus::ErrorResponse> error_response = |
290 dbus::ErrorResponse::FromMethodCall( | 290 dbus::ErrorResponse::FromMethodCall( |
291 method_call, kErrorInvalidArgs, "Expected 's'."); | 291 method_call, kErrorInvalidArgs, "Expected 's'."); |
292 response_sender.Run(error_response.PassAs<dbus::Response>()); | 292 response_sender.Run(error_response.Pass()); |
293 return; | 293 return; |
294 } | 294 } |
295 | 295 |
296 // Only the GATT descriptor interface is supported. | 296 // Only the GATT descriptor interface is supported. |
297 if (interface_name != | 297 if (interface_name != |
298 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) { | 298 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) { |
299 scoped_ptr<dbus::ErrorResponse> error_response = | 299 scoped_ptr<dbus::ErrorResponse> error_response = |
300 dbus::ErrorResponse::FromMethodCall( | 300 dbus::ErrorResponse::FromMethodCall( |
301 method_call, kErrorInvalidArgs, | 301 method_call, kErrorInvalidArgs, |
302 "No such interface: '" + interface_name + "'."); | 302 "No such interface: '" + interface_name + "'."); |
303 response_sender.Run(error_response.PassAs<dbus::Response>()); | 303 response_sender.Run(error_response.Pass()); |
304 return; | 304 return; |
305 } | 305 } |
306 | 306 |
307 // Try to obtain the value from the delegate. We will construct the | 307 // Try to obtain the value from the delegate. We will construct the |
308 // response in the success callback. | 308 // response in the success callback. |
309 DCHECK(delegate_); | 309 DCHECK(delegate_); |
310 delegate_->GetDescriptorValue( | 310 delegate_->GetDescriptorValue( |
311 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGetAll, | 311 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGetAll, |
312 weak_ptr_factory_.GetWeakPtr(), | 312 weak_ptr_factory_.GetWeakPtr(), |
313 method_call, response_sender), | 313 method_call, response_sender), |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 | 397 |
398 // Called by the Delegate in response to a failed method call to get or set | 398 // Called by the Delegate in response to a failed method call to get or set |
399 // the descriptor value. | 399 // the descriptor value. |
400 void OnFailure(dbus::MethodCall* method_call, | 400 void OnFailure(dbus::MethodCall* method_call, |
401 dbus::ExportedObject::ResponseSender response_sender) { | 401 dbus::ExportedObject::ResponseSender response_sender) { |
402 VLOG(2) << "Failed to get/set descriptor value. Report error."; | 402 VLOG(2) << "Failed to get/set descriptor value. Report error."; |
403 scoped_ptr<dbus::ErrorResponse> error_response = | 403 scoped_ptr<dbus::ErrorResponse> error_response = |
404 dbus::ErrorResponse::FromMethodCall( | 404 dbus::ErrorResponse::FromMethodCall( |
405 method_call, kErrorFailed, | 405 method_call, kErrorFailed, |
406 "Failed to get/set descriptor value."); | 406 "Failed to get/set descriptor value."); |
407 response_sender.Run(error_response.PassAs<dbus::Response>()); | 407 response_sender.Run(error_response.Pass()); |
408 } | 408 } |
409 | 409 |
410 // Origin thread (i.e. the UI thread in production). | 410 // Origin thread (i.e. the UI thread in production). |
411 base::PlatformThreadId origin_thread_id_; | 411 base::PlatformThreadId origin_thread_id_; |
412 | 412 |
413 // 128-bit descriptor UUID of this object. | 413 // 128-bit descriptor UUID of this object. |
414 std::string uuid_; | 414 std::string uuid_; |
415 | 415 |
416 // D-Bus bus object is exported on, not owned by this object and must | 416 // D-Bus bus object is exported on, not owned by this object and must |
417 // outlive it. | 417 // outlive it. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 const dbus::ObjectPath& characteristic_path) { | 462 const dbus::ObjectPath& characteristic_path) { |
463 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { | 463 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { |
464 return new BluetoothGattDescriptorServiceProviderImpl( | 464 return new BluetoothGattDescriptorServiceProviderImpl( |
465 bus, object_path, delegate, uuid, permissions, characteristic_path); | 465 bus, object_path, delegate, uuid, permissions, characteristic_path); |
466 } | 466 } |
467 return new FakeBluetoothGattDescriptorServiceProvider( | 467 return new FakeBluetoothGattDescriptorServiceProvider( |
468 object_path, delegate, uuid, permissions, characteristic_path); | 468 object_path, delegate, uuid, permissions, characteristic_path); |
469 } | 469 } |
470 | 470 |
471 } // namespace chromeos | 471 } // namespace chromeos |
OLD | NEW |