Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Unified Diff: device/bluetooth/adapter.cc

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Major reorganization of Mojo interface code and DeviceCollection Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/bluetooth/adapter.h ('k') | device/bluetooth/device.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/bluetooth/adapter.cc
diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc
index 639e7a2fd501b984dcbe1fc63486200e7523ef1c..0a6489ebe6fb205cfc2f3caa318312bf1032909e 100644
--- a/device/bluetooth/adapter.cc
+++ b/device/bluetooth/adapter.cc
@@ -6,9 +6,9 @@
#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 {
@@ -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 */);
+ 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,64 @@ 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::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:
+ NOTREACHED();
+ return mojom::ConnectErrorCode::UNTRANSLATED_CONNECT_ERROR_CODE;
+ }
+ NOTREACHED();
+ return mojom::ConnectErrorCode::UNTRANSLATED_CONNECT_ERROR_CODE;
+}
} // namespace bluetooth
« no previous file with comments | « device/bluetooth/adapter.h ('k') | device/bluetooth/device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698