Index: device/bluetooth/device.cc |
diff --git a/device/bluetooth/device.cc b/device/bluetooth/device.cc |
index ca1e6149dbf2370efbbb3caea1dd7d202d18fb56..a5842aa004f5a31d75a711bfd0bbc58642c705a2 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,52 @@ 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); |
+ |
+ if (device->IsGattServicesDiscoveryComplete()) { |
+ ReadValueForCharacteristicImpl(service_id, characteristic_id, callback); |
+ return; |
+ } |
+ |
+ // pending_services_requests_ is owned by Device, so base::Unretained is |
+ // safe. |
+ pending_services_requests_.push_back(base::Bind( |
ortuno
2017/01/20 03:45:22
q: Why do we queue instead of just returning an er
|
+ &Device::ReadValueForCharacteristicImpl, base::Unretained(this), |
+ service_id, characteristic_id, callback)); |
+} |
+ |
+void Device::WriteValueForCharacteristic( |
+ const std::vector<uint8_t>& value, |
+ const std::string& service_id, |
+ const std::string& characteristic_id, |
+ const WriteValueForCharacteristicCallback& callback) { |
+ device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
+ DCHECK(device); |
+ |
+ if (device->IsGattServicesDiscoveryComplete()) { |
+ WriteValueForCharacteristicImpl(value, service_id, characteristic_id, |
+ callback); |
+ return; |
+ } |
+ |
+ // pending_services_requests_ is owned by Device, so base::Unretained is |
+ // safe. |
+ pending_services_requests_.push_back(base::Bind( |
+ &Device::WriteValueForCharacteristicImpl, base::Unretained(this), value, |
+ service_id, characteristic_id, callback)); |
+} |
+ |
void Device::GetServicesImpl(const GetServicesCallback& callback) { |
device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
DCHECK(device); |
@@ -161,6 +204,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->value = characteristic->GetValue(); |
characteristics.push_back(std::move(characteristic_info)); |
} |
@@ -168,6 +213,71 @@ void Device::GetCharacteristicsImpl( |
callback.Run(std::move(characteristics)); |
} |
+void Device::ReadValueForCharacteristicImpl( |
+ 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); |
+ DCHECK(service); |
ortuno
2017/01/20 03:45:22
There are no guarantees, in code or docs, that the
mbrunson
2017/01/21 01:49:00
Ok. Done.
|
+ device::BluetoothRemoteGattCharacteristic* characteristic = |
+ service->GetCharacteristic(characteristic_id); |
+ DCHECK(characteristic); |
+ |
+ characteristic->ReadRemoteCharacteristic( |
+ base::Bind(&Device::OnReadRemoteCharacteristic, |
+ weak_ptr_factory_.GetWeakPtr(), callback), |
+ base::Bind(&Device::OnReadRemoteCharacteristicError, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+} |
+ |
+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), |
+ std::vector<uint8_t>()); |
+} |
+ |
+void Device::WriteValueForCharacteristicImpl( |
+ const std::vector<uint8_t>& value, |
+ const std::string& service_id, |
+ const std::string& characteristic_id, |
+ const WriteValueForCharacteristicCallback& callback) { |
+ device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
+ DCHECK(device); |
+ device::BluetoothRemoteGattService* service = |
+ device->GetGattService(service_id); |
+ DCHECK(service); |
+ device::BluetoothRemoteGattCharacteristic* characteristic = |
+ service->GetCharacteristic(characteristic_id); |
+ DCHECK(characteristic); |
+ |
+ characteristic->WriteRemoteCharacteristic( |
+ value, base::Bind(&Device::OnWriteRemoteCharacteristic, |
+ weak_ptr_factory_.GetWeakPtr(), callback), |
+ base::Bind(&Device::OnWriteRemoteCharacteristicError, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+} |
+ |
+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(); |
} |