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