Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <utility> | 6 #include <utility> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "device/bluetooth/adapter.h" | 9 #include "device/bluetooth/adapter.h" |
| 10 #include "device/bluetooth/device.h" | 10 #include "device/bluetooth/device.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 12 | 11 |
| 13 namespace bluetooth { | 12 namespace bluetooth { |
| 13 namespace { | |
| 14 mojom::ConnectResult BluetoothErrorCodeToMojomResult( | |
| 15 device::BluetoothDevice::ConnectErrorCode error_code) { | |
|
dcheng
2016/11/11 01:25:32
Let's use EnumTraits for this: https://www.chromiu
mbrunson
2016/11/11 21:26:15
Is it possible to use EnumTraits when the C++ clas
dcheng
2016/11/15 07:24:02
Yes, that's not a problem.
(If the conversion is
mbrunson
2016/11/16 03:32:03
I talked to yzshen about this. It's not possible.
dcheng
2016/11/17 01:31:47
Why would it need to do that? We only need to hand
ortuno
2016/11/17 03:54:44
I think what mbrunson meant is that the mojo enum
mbrunson
2016/11/17 21:28:01
Following our chat discussion: Has a simpler alter
dcheng
2016/11/17 21:35:36
Sorry for the slow responses. I really don't think
mbrunson
2016/11/17 23:00:43
Hmm. It seems really awkward to start an action in
mbrunson
2016/11/18 01:53:06
TypeConverter added. Done.
| |
| 16 switch (error_code) { | |
| 17 case device::BluetoothDevice::ConnectErrorCode:: | |
| 18 ERROR_ATTRIBUTE_LENGTH_INVALID: | |
| 19 return mojom::ConnectResult::ATTRIBUTE_LENGTH_INVALID; | |
| 20 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: | |
| 21 return mojom::ConnectResult::AUTH_CANCELED; | |
| 22 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: | |
| 23 return mojom::ConnectResult::AUTH_FAILED; | |
| 24 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: | |
| 25 return mojom::ConnectResult::AUTH_REJECTED; | |
| 26 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: | |
| 27 return mojom::ConnectResult::AUTH_TIMEOUT; | |
| 28 case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: | |
| 29 return mojom::ConnectResult::CONNECTION_CONGESTED; | |
| 30 case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: | |
| 31 return mojom::ConnectResult::FAILED; | |
| 32 case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: | |
| 33 return mojom::ConnectResult::INPROGRESS; | |
| 34 case device::BluetoothDevice::ConnectErrorCode:: | |
| 35 ERROR_INSUFFICIENT_ENCRYPTION: | |
| 36 return mojom::ConnectResult::INSUFFICIENT_ENCRYPTION; | |
| 37 case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: | |
| 38 return mojom::ConnectResult::OFFSET_INVALID; | |
| 39 case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: | |
| 40 return mojom::ConnectResult::READ_NOT_PERMITTED; | |
| 41 case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: | |
| 42 return mojom::ConnectResult::REQUEST_NOT_SUPPORTED; | |
| 43 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: | |
| 44 return mojom::ConnectResult::UNKNOWN; | |
| 45 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: | |
| 46 return mojom::ConnectResult::UNSUPPORTED_DEVICE; | |
| 47 case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: | |
| 48 return mojom::ConnectResult::WRITE_NOT_PERMITTED; | |
| 49 case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: | |
| 50 NOTREACHED(); | |
| 51 return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; | |
| 52 } | |
| 53 NOTREACHED(); | |
| 54 return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; | |
| 55 } | |
| 56 } // namespace | |
| 14 | 57 |
| 15 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) | 58 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) |
| 16 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { | 59 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { |
| 17 adapter_->AddObserver(this); | 60 adapter_->AddObserver(this); |
| 18 } | 61 } |
| 19 | 62 |
| 20 Adapter::~Adapter() { | 63 Adapter::~Adapter() { |
| 21 adapter_->RemoveObserver(this); | 64 adapter_->RemoveObserver(this); |
| 22 adapter_ = nullptr; | 65 adapter_ = nullptr; |
| 23 } | 66 } |
| 24 | 67 |
| 25 void Adapter::GetInfo(const GetInfoCallback& callback) { | 68 void Adapter::GetInfo(const GetInfoCallback& callback) { |
| 26 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); | 69 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); |
| 27 adapter_info->address = adapter_->GetAddress(); | 70 adapter_info->address = adapter_->GetAddress(); |
| 28 adapter_info->name = adapter_->GetName(); | 71 adapter_info->name = adapter_->GetName(); |
| 29 adapter_info->initialized = adapter_->IsInitialized(); | 72 adapter_info->initialized = adapter_->IsInitialized(); |
| 30 adapter_info->present = adapter_->IsPresent(); | 73 adapter_info->present = adapter_->IsPresent(); |
| 31 adapter_info->powered = adapter_->IsPowered(); | 74 adapter_info->powered = adapter_->IsPowered(); |
| 32 adapter_info->discoverable = adapter_->IsDiscoverable(); | 75 adapter_info->discoverable = adapter_->IsDiscoverable(); |
| 33 adapter_info->discovering = adapter_->IsDiscovering(); | 76 adapter_info->discovering = adapter_->IsDiscovering(); |
| 34 callback.Run(std::move(adapter_info)); | 77 callback.Run(std::move(adapter_info)); |
| 35 } | 78 } |
| 36 | 79 |
| 37 void Adapter::GetDevice(const std::string& address, | 80 void Adapter::ConnectToDevice(const std::string& address, |
| 38 const GetDeviceCallback& callback) { | 81 const ConnectToDeviceCallback& callback) { |
| 39 mojom::DevicePtr device_ptr; | 82 device::BluetoothDevice* device = adapter_->GetDevice(address); |
| 40 mojo::MakeStrongBinding(base::MakeUnique<Device>(address, adapter_), | 83 |
| 41 mojo::GetProxy(&device_ptr)); | 84 if (!device) { |
| 42 callback.Run(std::move(device_ptr)); | 85 callback.Run(mojom::ConnectResult::DEVICE_NO_LONGER_IN_RANGE, |
| 86 nullptr /* device */); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 device->CreateGattConnection( | |
| 91 base::Bind(&Adapter::OnGattConnected, weak_ptr_factory_.GetWeakPtr(), | |
| 92 callback), | |
| 93 base::Bind(&Adapter::OnConnectError, weak_ptr_factory_.GetWeakPtr(), | |
| 94 callback)); | |
| 43 } | 95 } |
| 44 | 96 |
| 45 void Adapter::GetDevices(const GetDevicesCallback& callback) { | 97 void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| 46 std::vector<mojom::DeviceInfoPtr> devices; | 98 std::vector<mojom::DeviceInfoPtr> devices; |
| 47 | 99 |
| 48 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 100 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
| 49 mojom::DeviceInfoPtr device_info = | 101 mojom::DeviceInfoPtr device_info = |
| 50 Device::ConstructDeviceInfoStruct(device); | 102 Device::ConstructDeviceInfoStruct(device); |
| 51 devices.push_back(std::move(device_info)); | 103 devices.push_back(std::move(device_info)); |
| 52 } | 104 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 75 } | 127 } |
| 76 | 128 |
| 77 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, | 129 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
| 78 device::BluetoothDevice* device) { | 130 device::BluetoothDevice* device) { |
| 79 if (client_) { | 131 if (client_) { |
| 80 auto device_info = Device::ConstructDeviceInfoStruct(device); | 132 auto device_info = Device::ConstructDeviceInfoStruct(device); |
| 81 client_->DeviceChanged(std::move(device_info)); | 133 client_->DeviceChanged(std::move(device_info)); |
| 82 } | 134 } |
| 83 } | 135 } |
| 84 | 136 |
| 137 void Adapter::OnGattConnected( | |
| 138 const ConnectToDeviceCallback& callback, | |
| 139 std::unique_ptr<device::BluetoothGattConnection> connection) { | |
| 140 mojom::DevicePtr device_ptr; | |
| 141 | |
| 142 // Owns itself. | |
| 143 new Device(adapter_, std::move(connection), mojo::GetProxy(&device_ptr)); | |
| 144 | |
| 145 callback.Run(mojom::ConnectResult::SUCCESS, std::move(device_ptr)); | |
| 146 } | |
| 147 | |
| 148 void Adapter::OnConnectError( | |
| 149 const ConnectToDeviceCallback& callback, | |
| 150 device::BluetoothDevice::ConnectErrorCode error_code) { | |
| 151 mojom::ConnectResult code = BluetoothErrorCodeToMojomResult(error_code); | |
| 152 callback.Run(code, nullptr /* Device */); | |
| 153 } | |
| 85 } // namespace bluetooth | 154 } // namespace bluetooth |
| OLD | NEW |