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 "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" | 10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 const char kErrorAdapterNotInitialized[] = | 22 const char kErrorAdapterNotInitialized[] = |
23 "Could not initialize Bluetooth adapter."; | 23 "Could not initialize Bluetooth adapter."; |
24 const char kErrorCharacteristicNotFoundFormat[] = | 24 const char kErrorCharacteristicNotFoundFormat[] = |
25 "Characteristic with ID \"%s\" not found."; | 25 "Characteristic with ID \"%s\" not found."; |
26 const char kErrorDeviceNotFoundFormat[] = | 26 const char kErrorDeviceNotFoundFormat[] = |
27 "Device with address \"%s\" not found."; | 27 "Device with address \"%s\" not found."; |
28 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; | 28 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
29 const char kErrorPlatformNotSupported[] = | 29 const char kErrorPlatformNotSupported[] = |
30 "This operation is not supported on the current platform"; | 30 "This operation is not supported on the current platform"; |
| 31 const char kErrorWriteCharacteristicValueFailedFormat[] = |
| 32 "Failed to write value of characteristic with ID \"%s\"."; |
31 | 33 |
32 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 34 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
33 BrowserContext* context) { | 35 BrowserContext* context) { |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
35 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 37 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
36 } | 38 } |
37 | 39 |
38 void DoWorkCallback(const base::Callback<bool()>& callback) { | 40 void DoWorkCallback(const base::Callback<bool()>& callback) { |
39 DCHECK(!callback.is_null()); | 41 DCHECK(!callback.is_null()); |
40 callback.Run(); | 42 callback.Run(); |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 } | 331 } |
330 | 332 |
331 bool BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { | 333 bool BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { |
332 // TODO(armansito): Implement. | 334 // TODO(armansito): Implement. |
333 SetError("Call not supported."); | 335 SetError("Call not supported."); |
334 SendResponse(false); | 336 SendResponse(false); |
335 return false; | 337 return false; |
336 } | 338 } |
337 | 339 |
338 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { | 340 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { |
339 // TODO(armansito): Implement. | 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
340 SetError("Call not supported."); | 342 |
| 343 BluetoothLowEnergyEventRouter* event_router = |
| 344 GetEventRouter(browser_context()); |
| 345 |
| 346 // The adapter must be initialized at this point, but return an error instead |
| 347 // of asserting. |
| 348 if (!event_router->HasAdapter()) { |
| 349 SetError(kErrorAdapterNotInitialized); |
| 350 SendResponse(false); |
| 351 return false; |
| 352 } |
| 353 |
| 354 scoped_ptr<apibtle::WriteCharacteristicValue::Params> params( |
| 355 apibtle::WriteCharacteristicValue::Params::Create(*args_)); |
| 356 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 357 |
| 358 instance_id_ = params->characteristic_id; |
| 359 std::vector<uint8> value(params->value.begin(), params->value.end()); |
| 360 |
| 361 if (!event_router->WriteCharacteristicValue( |
| 362 instance_id_, |
| 363 value, |
| 364 base::Bind(&BluetoothLowEnergyWriteCharacteristicValueFunction:: |
| 365 SuccessCallback, |
| 366 this), |
| 367 base::Bind(&BluetoothLowEnergyWriteCharacteristicValueFunction:: |
| 368 ErrorCallback, |
| 369 this))) { |
| 370 SetError(base::StringPrintf(kErrorCharacteristicNotFoundFormat, |
| 371 instance_id_.c_str())); |
| 372 SendResponse(false); |
| 373 return false; |
| 374 } |
| 375 |
| 376 return true; |
| 377 } |
| 378 |
| 379 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { |
| 380 results_ = apibtle::WriteCharacteristicValue::Results::Create(); |
| 381 SendResponse(true); |
| 382 } |
| 383 |
| 384 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback() { |
| 385 SetError(base::StringPrintf(kErrorWriteCharacteristicValueFailedFormat, |
| 386 instance_id_.c_str())); |
341 SendResponse(false); | 387 SendResponse(false); |
342 return false; | |
343 } | 388 } |
344 | 389 |
345 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { | 390 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
346 // TODO(armansito): Implement. | 391 // TODO(armansito): Implement. |
347 SetError("Call not supported."); | 392 SetError("Call not supported."); |
348 SendResponse(false); | 393 SendResponse(false); |
349 return false; | 394 return false; |
350 } | 395 } |
351 | 396 |
352 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 397 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
353 // TODO(armansito): Implement. | 398 // TODO(armansito): Implement. |
354 SetError("Call not supported."); | 399 SetError("Call not supported."); |
355 SendResponse(false); | 400 SendResponse(false); |
356 return false; | 401 return false; |
357 } | 402 } |
358 | 403 |
359 } // namespace api | 404 } // namespace api |
360 } // namespace extensions | 405 } // namespace extensions |
OLD | NEW |