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 "base/memory/ptr_util.h" | |
| 9 #include "device/bluetooth/adapter.h" | 10 #include "device/bluetooth/adapter.h" |
| 10 #include "device/bluetooth/device.h" | 11 #include "device/bluetooth/device.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 | 13 |
| 13 namespace bluetooth { | 14 namespace bluetooth { |
| 15 namespace { | |
| 16 mojom::ConnectResult BluetoothErrorCodeToMojomResult( | |
| 17 device::BluetoothDevice::ConnectErrorCode error_code) { | |
| 18 switch (error_code) { | |
| 19 case device::BluetoothDevice::ConnectErrorCode:: | |
| 20 ERROR_ATTRIBUTE_LENGTH_INVALID: | |
| 21 return mojom::ConnectResult::ATTRIBUTE_LENGTH_INVALID; | |
| 22 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: | |
| 23 return mojom::ConnectResult::AUTH_CANCELED; | |
| 24 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: | |
| 25 return mojom::ConnectResult::AUTH_FAILED; | |
| 26 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: | |
| 27 return mojom::ConnectResult::AUTH_REJECTED; | |
| 28 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: | |
| 29 return mojom::ConnectResult::AUTH_TIMEOUT; | |
| 30 case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: | |
| 31 return mojom::ConnectResult::CONNECTION_CONGESTED; | |
| 32 case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: | |
| 33 return mojom::ConnectResult::FAILED; | |
| 34 case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: | |
| 35 return mojom::ConnectResult::INPROGRESS; | |
| 36 case device::BluetoothDevice::ConnectErrorCode:: | |
| 37 ERROR_INSUFFICIENT_ENCRYPTION: | |
| 38 return mojom::ConnectResult::INSUFFICIENT_ENCRYPTION; | |
| 39 case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: | |
| 40 return mojom::ConnectResult::OFFSET_INVALID; | |
| 41 case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: | |
| 42 return mojom::ConnectResult::READ_NOT_PERMITTED; | |
| 43 case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: | |
| 44 return mojom::ConnectResult::REQUEST_NOT_SUPPORTED; | |
| 45 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: | |
| 46 return mojom::ConnectResult::UNKNOWN; | |
| 47 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: | |
| 48 return mojom::ConnectResult::UNSUPPORTED_DEVICE; | |
| 49 case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: | |
| 50 return mojom::ConnectResult::WRITE_NOT_PERMITTED; | |
| 51 case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: | |
| 52 NOTREACHED(); | |
| 53 return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; | |
| 54 } | |
| 55 NOTREACHED(); | |
| 56 return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; | |
| 57 } | |
| 58 } // namespace | |
| 14 | 59 |
| 15 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) | 60 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) |
| 16 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { | 61 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { |
| 17 adapter_->AddObserver(this); | 62 adapter_->AddObserver(this); |
| 18 } | 63 } |
| 19 | 64 |
| 20 Adapter::~Adapter() { | 65 Adapter::~Adapter() { |
| 21 adapter_->RemoveObserver(this); | 66 adapter_->RemoveObserver(this); |
| 22 adapter_ = nullptr; | 67 adapter_ = nullptr; |
| 23 } | 68 } |
| 24 | 69 |
| 25 void Adapter::GetInfo(const GetInfoCallback& callback) { | 70 void Adapter::GetInfo(const GetInfoCallback& callback) { |
| 26 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); | 71 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); |
| 27 adapter_info->address = adapter_->GetAddress(); | 72 adapter_info->address = adapter_->GetAddress(); |
| 28 adapter_info->name = adapter_->GetName(); | 73 adapter_info->name = adapter_->GetName(); |
| 29 adapter_info->initialized = adapter_->IsInitialized(); | 74 adapter_info->initialized = adapter_->IsInitialized(); |
| 30 adapter_info->present = adapter_->IsPresent(); | 75 adapter_info->present = adapter_->IsPresent(); |
| 31 adapter_info->powered = adapter_->IsPowered(); | 76 adapter_info->powered = adapter_->IsPowered(); |
| 32 adapter_info->discoverable = adapter_->IsDiscoverable(); | 77 adapter_info->discoverable = adapter_->IsDiscoverable(); |
| 33 adapter_info->discovering = adapter_->IsDiscovering(); | 78 adapter_info->discovering = adapter_->IsDiscovering(); |
| 34 callback.Run(std::move(adapter_info)); | 79 callback.Run(std::move(adapter_info)); |
| 35 } | 80 } |
| 36 | 81 |
| 37 void Adapter::GetDevice(const std::string& address, | 82 void Adapter::ConnectToDevice(const std::string& address, |
| 38 const GetDeviceCallback& callback) { | 83 const ConnectToDeviceCallback& callback) { |
| 39 mojom::DevicePtr device_ptr; | 84 device::BluetoothDevice* device = adapter_->GetDevice(address); |
| 40 mojo::MakeStrongBinding(base::MakeUnique<Device>(address, adapter_), | 85 |
| 41 mojo::GetProxy(&device_ptr)); | 86 if (!device) { |
| 42 callback.Run(std::move(device_ptr)); | 87 callback.Run(mojom::ConnectResult::DEVICE_NO_LONGER_IN_RANGE, |
| 88 nullptr /* device */); | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 device->CreateGattConnection( | |
| 93 base::Bind(&Adapter::OnGattConnected, weak_ptr_factory_.GetWeakPtr(), | |
| 94 callback), | |
| 95 base::Bind(&Adapter::OnConnectError, weak_ptr_factory_.GetWeakPtr(), | |
| 96 callback)); | |
| 43 } | 97 } |
| 44 | 98 |
| 45 void Adapter::GetDevices(const GetDevicesCallback& callback) { | 99 void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| 46 std::vector<mojom::DeviceInfoPtr> devices; | 100 std::vector<mojom::DeviceInfoPtr> devices; |
| 47 | 101 |
| 48 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 102 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
| 49 mojom::DeviceInfoPtr device_info = | 103 mojom::DeviceInfoPtr device_info = |
| 50 Device::ConstructDeviceInfoStruct(device); | 104 Device::ConstructDeviceInfoStruct(device); |
| 51 devices.push_back(std::move(device_info)); | 105 devices.push_back(std::move(device_info)); |
| 52 } | 106 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 75 } | 129 } |
| 76 | 130 |
| 77 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, | 131 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
| 78 device::BluetoothDevice* device) { | 132 device::BluetoothDevice* device) { |
| 79 if (client_) { | 133 if (client_) { |
| 80 auto device_info = Device::ConstructDeviceInfoStruct(device); | 134 auto device_info = Device::ConstructDeviceInfoStruct(device); |
| 81 client_->DeviceChanged(std::move(device_info)); | 135 client_->DeviceChanged(std::move(device_info)); |
| 82 } | 136 } |
| 83 } | 137 } |
| 84 | 138 |
| 139 void Adapter::OnGattConnected( | |
| 140 const ConnectToDeviceCallback& callback, | |
| 141 std::unique_ptr<device::BluetoothGattConnection> connection) { | |
| 142 mojom::DevicePtr device_ptr; | |
| 143 | |
| 144 auto* device = new Device(adapter_, std::move(connection)); | |
| 145 auto binding = mojo::MakeStrongBinding(base::WrapUnique(device), | |
| 146 mojo::GetProxy(&device_ptr)); | |
| 147 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.
| |
| 148 | |
| 149 callback.Run(mojom::ConnectResult::SUCCESS, std::move(device_ptr)); | |
| 150 } | |
| 151 | |
| 152 void Adapter::OnConnectError( | |
| 153 const ConnectToDeviceCallback& callback, | |
| 154 device::BluetoothDevice::ConnectErrorCode error_code) { | |
| 155 mojom::ConnectResult code = BluetoothErrorCodeToMojomResult(error_code); | |
| 156 callback.Run(code, nullptr /* Device */); | |
| 157 } | |
| 85 } // namespace bluetooth | 158 } // namespace bluetooth |
| OLD | NEW |