Index: device/bluetooth/device.cc |
diff --git a/device/bluetooth/device.cc b/device/bluetooth/device.cc |
index 0e018a2ee93790e3cf15053fd2f93ede4befaa52..c8561600d272a368f903316a015cc4024802128b 100644 |
--- a/device/bluetooth/device.cc |
+++ b/device/bluetooth/device.cc |
@@ -8,8 +8,13 @@ |
#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_enum_traits.h" |
#include "mojo/public/cpp/bindings/strong_binding.h" |
+using GattResultTraits = |
+ mojo::EnumTraits<bluetooth::mojom::GattResult, |
+ device::BluetoothGattService::GattErrorCode>; |
+ |
namespace bluetooth { |
Device::~Device() { |
adapter_->RemoveObserver(this); |
@@ -123,9 +128,106 @@ void Device::GetCharacteristics(const std::string& service_id, |
callback.Run(std::move(characteristics)); |
} |
+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::SERVICE_NOT_FOUND, |
+ base::nullopt /* value */); |
+ return; |
+ } |
+ |
+ device::BluetoothRemoteGattCharacteristic* characteristic = |
+ service->GetCharacteristic(characteristic_id); |
+ if (characteristic == nullptr) { |
+ callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND, |
+ 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::SERVICE_NOT_FOUND); |
+ return; |
+ } |
+ |
+ device::BluetoothRemoteGattCharacteristic* characteristic = |
+ service->GetCharacteristic(characteristic_id); |
+ if (characteristic == nullptr) { |
+ callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND); |
+ return; |
+ } |
+ |
+ characteristic->WriteRemoteCharacteristic( |
+ value, base::Bind(&Device::OnWriteRemoteCharacteristic, |
+ weak_ptr_factory_.GetWeakPtr(), callback), |
+ base::Bind(&Device::OnWriteRemoteCharacteristicError, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+} |
+ |
+void Device::GetDescriptors(const std::string& service_id, |
+ const std::string& characteristic_id, |
+ const GetDescriptorsCallback& callback) { |
+ device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
+ if (!device) { |
+ callback.Run(base::nullopt); |
+ return; |
+ } |
+ |
+ device::BluetoothRemoteGattService* service = |
+ device->GetGattService(service_id); |
+ if (!service) { |
+ callback.Run(base::nullopt); |
+ return; |
+ } |
+ |
+ device::BluetoothRemoteGattCharacteristic* characteristic = |
+ service->GetCharacteristic(characteristic_id); |
+ if (!characteristic) { |
+ callback.Run(base::nullopt); |
+ return; |
+ } |
+ |
+ std::vector<mojom::DescriptorInfoPtr> descriptors; |
+ |
+ for (const auto* descriptor : characteristic->GetDescriptors()) { |
+ mojom::DescriptorInfoPtr descriptor_info = mojom::DescriptorInfo::New(); |
+ |
+ descriptor_info->id = descriptor->GetIdentifier(); |
+ descriptor_info->uuid = descriptor->GetUUID(); |
+ descriptors.push_back(std::move(descriptor_info)); |
+ } |
+ |
+ callback.Run(std::move(descriptors)); |
+} |
+ |
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); |
} |
@@ -154,6 +256,30 @@ mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( |
return service_info; |
} |
+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(GattResultTraits::ToMojom(error_code), |
dcheng
2017/01/26 19:32:25
I think you need a typemap file as well (see the C
mbrunson
2017/01/26 21:27:40
Ah...yes. The issue with that is I can't do things
|
+ 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(GattResultTraits::ToMojom(error_code)); |
+} |
+ |
const std::string& Device::GetAddress() { |
return connection_->GetDeviceAddress(); |
} |