Chromium Code Reviews| Index: device/bluetooth/adapter.cc |
| diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc |
| index 639e7a2fd501b984dcbe1fc63486200e7523ef1c..ba95ea3f85d26414fe79000366f406a3a149c2d5 100644 |
| --- a/device/bluetooth/adapter.cc |
| +++ b/device/bluetooth/adapter.cc |
| @@ -34,12 +34,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::ConnectErrorCode::DEVICE_NO_LONGER_IN_RANGE, |
| + nullptr /* Device */); |
|
ortuno
2016/10/26 02:52:21
nit: s/Device/device/
mbrunson
2016/10/28 21:06:47
Done.
|
| + 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 +91,62 @@ void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
| } |
| } |
| +void Adapter::OnGattConnected( |
| + const ConnectToDeviceCallback& callback, |
| + std::unique_ptr<device::BluetoothGattConnection> connection) { |
| + mojom::DevicePtr device_ptr; |
| + mojo::MakeStrongBinding( |
| + base::MakeUnique<Device>(adapter_, std::move(connection)), |
| + mojo::GetProxy(&device_ptr)); |
| + callback.Run(mojom::ConnectErrorCode::SUCCESS, std::move(device_ptr)); |
| +} |
| + |
| +void Adapter::OnConnectError( |
| + const ConnectToDeviceCallback& callback, |
| + device::BluetoothDevice::ConnectErrorCode error_code) { |
| + mojom::ConnectErrorCode code = BluetoothErrorCodeToMojomErrorCode(error_code); |
| + callback.Run(code, nullptr /* Device */); |
| +} |
| + |
| +mojom::ConnectErrorCode Adapter::BluetoothErrorCodeToMojomErrorCode( |
| + device::BluetoothDevice::ConnectErrorCode error_code) { |
| + switch (error_code) { |
| + case device::BluetoothDevice::ConnectErrorCode:: |
| + ERROR_ATTRIBUTE_LENGTH_INVALID: |
| + return mojom::ConnectErrorCode::ATTRIBUTE_LENGTH_INVALID; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: |
| + return mojom::ConnectErrorCode::AUTH_CANCELED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: |
| + return mojom::ConnectErrorCode::AUTH_FAILED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: |
| + return mojom::ConnectErrorCode::AUTH_REJECTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: |
| + return mojom::ConnectErrorCode::AUTH_TIMEOUT; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: |
| + return mojom::ConnectErrorCode::CONNECTION_CONGESTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: |
| + return mojom::ConnectErrorCode::FAILED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: |
| + return mojom::ConnectErrorCode::INPROGRESS; |
| + case device::BluetoothDevice::ConnectErrorCode:: |
| + ERROR_INSUFFICIENT_ENCRYPTION: |
| + return mojom::ConnectErrorCode::INSUFFICIENT_ENCRYPTION; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: |
| + return mojom::ConnectErrorCode::OFFSET_INVALID; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: |
| + return mojom::ConnectErrorCode::READ_NOT_PERMITTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: |
| + return mojom::ConnectErrorCode::REQUEST_NOT_SUPPORTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: |
| + return mojom::ConnectErrorCode::UNKNOWN; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: |
| + return mojom::ConnectErrorCode::UNSUPPORTED_DEVICE; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: |
| + return mojom::ConnectErrorCode::WRITE_NOT_PERMITTED; |
| + case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: |
|
ortuno
2016/10/26 02:52:21
This is not an actual error :) add a NOTREACHED an
mbrunson
2016/10/28 21:06:47
Done.
|
| + return mojom::ConnectErrorCode::NUM_CONNECT_ERROR_CODES; |
| + default: |
|
ortuno
2016/10/26 02:52:21
When a new value is added to an enum the compiler
mbrunson
2016/10/28 21:06:47
Done.
|
| + return mojom::ConnectErrorCode::UNKNOWN; |
| + } |
| +} |
| } // namespace bluetooth |