Chromium Code Reviews| Index: device/bluetooth/adapter.cc |
| diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc |
| index 639e7a2fd501b984dcbe1fc63486200e7523ef1c..301423cca2f8ea9fd5cf72a062d589089c312a24 100644 |
| --- a/device/bluetooth/adapter.cc |
| +++ b/device/bluetooth/adapter.cc |
| @@ -6,11 +6,56 @@ |
| #include <utility> |
| #include <vector> |
| +#include "base/memory/ptr_util.h" |
| #include "device/bluetooth/adapter.h" |
| #include "device/bluetooth/device.h" |
| #include "mojo/public/cpp/bindings/strong_binding.h" |
| namespace bluetooth { |
| +namespace { |
| +mojom::ConnectResult BluetoothErrorCodeToMojomResult( |
| + device::BluetoothDevice::ConnectErrorCode error_code) { |
| + switch (error_code) { |
| + case device::BluetoothDevice::ConnectErrorCode:: |
| + ERROR_ATTRIBUTE_LENGTH_INVALID: |
| + return mojom::ConnectResult::ATTRIBUTE_LENGTH_INVALID; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: |
| + return mojom::ConnectResult::AUTH_CANCELED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: |
| + return mojom::ConnectResult::AUTH_FAILED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: |
| + return mojom::ConnectResult::AUTH_REJECTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: |
| + return mojom::ConnectResult::AUTH_TIMEOUT; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: |
| + return mojom::ConnectResult::CONNECTION_CONGESTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: |
| + return mojom::ConnectResult::FAILED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: |
| + return mojom::ConnectResult::INPROGRESS; |
| + case device::BluetoothDevice::ConnectErrorCode:: |
| + ERROR_INSUFFICIENT_ENCRYPTION: |
| + return mojom::ConnectResult::INSUFFICIENT_ENCRYPTION; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: |
| + return mojom::ConnectResult::OFFSET_INVALID; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: |
| + return mojom::ConnectResult::READ_NOT_PERMITTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: |
| + return mojom::ConnectResult::REQUEST_NOT_SUPPORTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: |
| + return mojom::ConnectResult::UNKNOWN; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: |
| + return mojom::ConnectResult::UNSUPPORTED_DEVICE; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: |
| + return mojom::ConnectResult::WRITE_NOT_PERMITTED; |
| + case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: |
| + NOTREACHED(); |
| + return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; |
| + } |
| + NOTREACHED(); |
| + return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; |
| +} |
| +} // namespace |
| Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) |
| : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { |
| @@ -34,12 +79,21 @@ void Adapter::GetInfo(const GetInfoCallback& callback) { |
| callback.Run(std::move(adapter_info)); |
| } |
| -void Adapter::GetDevice(const std::string& address, |
| - const GetDeviceCallback& callback) { |
| - mojom::DevicePtr device_ptr; |
| - mojo::MakeStrongBinding(base::MakeUnique<Device>(address, adapter_), |
| - mojo::GetProxy(&device_ptr)); |
| - callback.Run(std::move(device_ptr)); |
| +void Adapter::ConnectToDevice(const std::string& address, |
| + const ConnectToDeviceCallback& callback) { |
| + device::BluetoothDevice* device = adapter_->GetDevice(address); |
| + |
| + if (!device) { |
| + callback.Run(mojom::ConnectResult::DEVICE_NO_LONGER_IN_RANGE, |
| + nullptr /* device */); |
| + return; |
| + } |
| + |
| + device->CreateGattConnection( |
| + base::Bind(&Adapter::OnGattConnected, weak_ptr_factory_.GetWeakPtr(), |
| + callback), |
| + base::Bind(&Adapter::OnConnectError, weak_ptr_factory_.GetWeakPtr(), |
| + callback)); |
| } |
| void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| @@ -82,4 +136,23 @@ void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
| } |
| } |
| +void Adapter::OnGattConnected( |
| + const ConnectToDeviceCallback& callback, |
| + std::unique_ptr<device::BluetoothGattConnection> connection) { |
| + mojom::DevicePtr device_ptr; |
| + |
| + auto* device = new Device(adapter_, std::move(connection)); |
| + auto binding = mojo::MakeStrongBinding(base::WrapUnique(device), |
| + mojo::GetProxy(&device_ptr)); |
| + device->SetStrongBindingPtr(binding); |
|
ortuno
2016/11/16 04:53:17
Can this be done in Device? Similar to BatteryMoni
mbrunson
2016/11/16 22:17:04
Done.
|
| + |
| + callback.Run(mojom::ConnectResult::SUCCESS, std::move(device_ptr)); |
| +} |
| + |
| +void Adapter::OnConnectError( |
| + const ConnectToDeviceCallback& callback, |
| + device::BluetoothDevice::ConnectErrorCode error_code) { |
| + mojom::ConnectResult code = BluetoothErrorCodeToMojomResult(error_code); |
| + callback.Run(code, nullptr /* Device */); |
| +} |
| } // namespace bluetooth |