Index: device/bluetooth/adapter.cc |
diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc |
index 639e7a2fd501b984dcbe1fc63486200e7523ef1c..dcab17ef66fef30ade3d9bdf7906bee183e51647 100644 |
--- a/device/bluetooth/adapter.cc |
+++ b/device/bluetooth/adapter.cc |
@@ -6,11 +6,55 @@ |
#include <utility> |
#include <vector> |
+#include "base/logging.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 +78,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 +135,21 @@ void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
} |
} |
+void Adapter::OnGattConnected( |
+ const ConnectToDeviceCallback& callback, |
+ std::unique_ptr<device::BluetoothGattConnection> connection) { |
+ mojom::DevicePtr device_ptr; |
+ |
+ // Owns itself. |
+ new Device(adapter_, std::move(connection), mojo::GetProxy(&device_ptr)); |
+ |
+ 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 |