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

Unified Diff: device/bluetooth/device.cc

Issue 2627243002: bluetooth: Add control for reading/writing of characteristics to internals page. (Closed)
Patch Set: DCHECK device Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: device/bluetooth/device.cc
diff --git a/device/bluetooth/device.cc b/device/bluetooth/device.cc
index ca1e6149dbf2370efbbb3caea1dd7d202d18fb56..bc67cd5f7f35488f4928a7a65bbb6efd740c35d4 100644
--- a/device/bluetooth/device.cc
+++ b/device/bluetooth/device.cc
@@ -8,6 +8,7 @@
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "device/bluetooth/device.h"
+#include "device/bluetooth/public/interfaces/gatt_result_type_converter.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
namespace bluetooth {
@@ -114,10 +115,69 @@ void Device::GetCharacteristics(const std::string& service_id,
Device::Device(scoped_refptr<device::BluetoothAdapter> adapter,
std::unique_ptr<device::BluetoothGattConnection> connection)
- : adapter_(std::move(adapter)), connection_(std::move(connection)) {
+ : adapter_(std::move(adapter)),
+ connection_(std::move(connection)),
+ weak_ptr_factory_(this) {
adapter_->AddObserver(this);
}
+void Device::ReadValueForCharacteristic(
+ const std::string& service_id,
+ const std::string& characteristic_id,
+ const ReadValueForCharacteristicCallback& callback) {
+ device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
+ DCHECK(device);
+
+ device::BluetoothRemoteGattService* service =
+ device->GetGattService(service_id);
+ if (service == nullptr) {
+ callback.Run(mojom::GattResult::FAILED, base::nullopt /* value */);
ortuno 2017/01/22 22:38:01 Would it be too much to add a specific error for t
mbrunson 2017/01/24 00:25:31 Done.
+ return;
+ }
+
+ device::BluetoothRemoteGattCharacteristic* characteristic =
+ service->GetCharacteristic(characteristic_id);
+ if (characteristic == nullptr) {
+ callback.Run(mojom::GattResult::FAILED, base::nullopt /* value */);
+ return;
+ }
+
+ characteristic->ReadRemoteCharacteristic(
+ base::Bind(&Device::OnReadRemoteCharacteristic,
+ weak_ptr_factory_.GetWeakPtr(), callback),
+ base::Bind(&Device::OnReadRemoteCharacteristicError,
+ weak_ptr_factory_.GetWeakPtr(), callback));
+}
+
+void Device::WriteValueForCharacteristic(
+ const std::string& service_id,
+ const std::string& characteristic_id,
+ const std::vector<uint8_t>& value,
+ const WriteValueForCharacteristicCallback& callback) {
+ device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
+ DCHECK(device);
+
+ device::BluetoothRemoteGattService* service =
+ device->GetGattService(service_id);
+ if (service == nullptr) {
+ callback.Run(mojom::GattResult::FAILED);
+ return;
+ }
+
+ device::BluetoothRemoteGattCharacteristic* characteristic =
+ service->GetCharacteristic(characteristic_id);
+ if (characteristic == nullptr) {
+ callback.Run(mojom::GattResult::FAILED);
+ return;
+ }
+
+ characteristic->WriteRemoteCharacteristic(
+ value, base::Bind(&Device::OnWriteRemoteCharacteristic,
+ weak_ptr_factory_.GetWeakPtr(), callback),
+ base::Bind(&Device::OnWriteRemoteCharacteristicError,
+ weak_ptr_factory_.GetWeakPtr(), callback));
+}
+
void Device::GetServicesImpl(const GetServicesCallback& callback) {
device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
DCHECK(device);
@@ -161,6 +221,8 @@ void Device::GetCharacteristicsImpl(
characteristic_info->id = characteristic->GetIdentifier();
characteristic_info->uuid = characteristic->GetUUID();
characteristic_info->properties = characteristic->GetProperties();
+ characteristic_info->permissions = characteristic->GetPermissions();
+ characteristic_info->last_known_value = characteristic->GetValue();
characteristics.push_back(std::move(characteristic_info));
}
@@ -168,6 +230,30 @@ void Device::GetCharacteristicsImpl(
callback.Run(std::move(characteristics));
}
+void Device::OnReadRemoteCharacteristic(
+ const ReadValueForCharacteristicCallback& callback,
+ const std::vector<uint8_t>& value) {
+ callback.Run(mojom::GattResult::SUCCESS, std::move(value));
+}
+
+void Device::OnReadRemoteCharacteristicError(
+ const ReadValueForCharacteristicCallback& callback,
+ device::BluetoothGattService::GattErrorCode error_code) {
+ callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code),
+ base::nullopt /* value */);
+}
+
+void Device::OnWriteRemoteCharacteristic(
+ const WriteValueForCharacteristicCallback& callback) {
+ callback.Run(mojom::GattResult::SUCCESS);
+}
+
+void Device::OnWriteRemoteCharacteristicError(
+ const WriteValueForCharacteristicCallback& callback,
+ device::BluetoothGattService::GattErrorCode error_code) {
+ callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code));
+}
+
const std::string& Device::GetAddress() {
return connection_->GetDeviceAddress();
}

Powered by Google App Engine
This is Rietveld 408576698