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

Side by Side 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, 1 month 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 unified diff | Download patch
« no previous file with comments | « device/bluetooth/adapter.h ('k') | device/bluetooth/device.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/logging.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 12
13 namespace bluetooth { 13 namespace bluetooth {
14 14
15 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) 15 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter)
16 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { 16 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) {
17 adapter_->AddObserver(this); 17 adapter_->AddObserver(this);
18 } 18 }
19 19
20 Adapter::~Adapter() { 20 Adapter::~Adapter() {
21 adapter_->RemoveObserver(this); 21 adapter_->RemoveObserver(this);
22 adapter_ = nullptr; 22 adapter_ = nullptr;
23 } 23 }
24 24
25 void Adapter::GetInfo(const GetInfoCallback& callback) { 25 void Adapter::GetInfo(const GetInfoCallback& callback) {
26 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); 26 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New();
27 adapter_info->address = adapter_->GetAddress(); 27 adapter_info->address = adapter_->GetAddress();
28 adapter_info->name = adapter_->GetName(); 28 adapter_info->name = adapter_->GetName();
29 adapter_info->initialized = adapter_->IsInitialized(); 29 adapter_info->initialized = adapter_->IsInitialized();
30 adapter_info->present = adapter_->IsPresent(); 30 adapter_info->present = adapter_->IsPresent();
31 adapter_info->powered = adapter_->IsPowered(); 31 adapter_info->powered = adapter_->IsPowered();
32 adapter_info->discoverable = adapter_->IsDiscoverable(); 32 adapter_info->discoverable = adapter_->IsDiscoverable();
33 adapter_info->discovering = adapter_->IsDiscovering(); 33 adapter_info->discovering = adapter_->IsDiscovering();
34 callback.Run(std::move(adapter_info)); 34 callback.Run(std::move(adapter_info));
35 } 35 }
36 36
37 void Adapter::GetDevice(const std::string& address, 37 void Adapter::ConnectToDevice(const std::string& address,
38 const GetDeviceCallback& callback) { 38 const ConnectToDeviceCallback& callback) {
39 mojom::DevicePtr device_ptr; 39 device::BluetoothDevice* device = adapter_->GetDevice(address);
40 mojo::MakeStrongBinding(base::MakeUnique<Device>(address, adapter_), 40
41 mojo::GetProxy(&device_ptr)); 41 if (!device) {
42 callback.Run(std::move(device_ptr)); 42 callback.Run(mojom::ConnectErrorCode::DEVICE_NO_LONGER_IN_RANGE,
43 nullptr /* device */);
44 return;
45 }
46
47 device->CreateGattConnection(
48 base::Bind(&Adapter::OnGattConnected, weak_ptr_factory_.GetWeakPtr(),
49 callback),
50 base::Bind(&Adapter::OnConnectError, weak_ptr_factory_.GetWeakPtr(),
51 callback));
43 } 52 }
44 53
45 void Adapter::GetDevices(const GetDevicesCallback& callback) { 54 void Adapter::GetDevices(const GetDevicesCallback& callback) {
46 std::vector<mojom::DeviceInfoPtr> devices; 55 std::vector<mojom::DeviceInfoPtr> devices;
47 56
48 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { 57 for (const device::BluetoothDevice* device : adapter_->GetDevices()) {
49 mojom::DeviceInfoPtr device_info = 58 mojom::DeviceInfoPtr device_info =
50 Device::ConstructDeviceInfoStruct(device); 59 Device::ConstructDeviceInfoStruct(device);
51 devices.push_back(std::move(device_info)); 60 devices.push_back(std::move(device_info));
52 } 61 }
(...skipping 22 matching lines...) Expand all
75 } 84 }
76 85
77 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, 86 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter,
78 device::BluetoothDevice* device) { 87 device::BluetoothDevice* device) {
79 if (client_) { 88 if (client_) {
80 auto device_info = Device::ConstructDeviceInfoStruct(device); 89 auto device_info = Device::ConstructDeviceInfoStruct(device);
81 client_->DeviceChanged(std::move(device_info)); 90 client_->DeviceChanged(std::move(device_info));
82 } 91 }
83 } 92 }
84 93
94 void Adapter::OnGattConnected(
95 const ConnectToDeviceCallback& callback,
96 std::unique_ptr<device::BluetoothGattConnection> connection) {
97 mojom::DevicePtr device_ptr;
98
99 // Owns itself.
100 new Device(adapter_, std::move(connection), mojo::GetProxy(&device_ptr));
101
102 callback.Run(mojom::ConnectErrorCode::SUCCESS, std::move(device_ptr));
103 }
104
105 void Adapter::OnConnectError(
106 const ConnectToDeviceCallback& callback,
107 device::BluetoothDevice::ConnectErrorCode error_code) {
108 mojom::ConnectErrorCode code = BluetoothErrorCodeToMojomErrorCode(error_code);
109 callback.Run(code, nullptr /* Device */);
110 }
111
112 mojom::ConnectErrorCode Adapter::BluetoothErrorCodeToMojomErrorCode(
113 device::BluetoothDevice::ConnectErrorCode error_code) {
114 switch (error_code) {
115 case device::BluetoothDevice::ConnectErrorCode::
116 ERROR_ATTRIBUTE_LENGTH_INVALID:
117 return mojom::ConnectErrorCode::ATTRIBUTE_LENGTH_INVALID;
118 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED:
119 return mojom::ConnectErrorCode::AUTH_CANCELED;
120 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED:
121 return mojom::ConnectErrorCode::AUTH_FAILED;
122 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED:
123 return mojom::ConnectErrorCode::AUTH_REJECTED;
124 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT:
125 return mojom::ConnectErrorCode::AUTH_TIMEOUT;
126 case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED:
127 return mojom::ConnectErrorCode::CONNECTION_CONGESTED;
128 case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED:
129 return mojom::ConnectErrorCode::FAILED;
130 case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS:
131 return mojom::ConnectErrorCode::INPROGRESS;
132 case device::BluetoothDevice::ConnectErrorCode::
133 ERROR_INSUFFICIENT_ENCRYPTION:
134 return mojom::ConnectErrorCode::INSUFFICIENT_ENCRYPTION;
135 case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID:
136 return mojom::ConnectErrorCode::OFFSET_INVALID;
137 case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED:
138 return mojom::ConnectErrorCode::READ_NOT_PERMITTED;
139 case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED:
140 return mojom::ConnectErrorCode::REQUEST_NOT_SUPPORTED;
141 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN:
142 return mojom::ConnectErrorCode::UNKNOWN;
143 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE:
144 return mojom::ConnectErrorCode::UNSUPPORTED_DEVICE;
145 case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED:
146 return mojom::ConnectErrorCode::WRITE_NOT_PERMITTED;
147 case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES:
148 NOTREACHED();
149 return mojom::ConnectErrorCode::UNTRANSLATED_CONNECT_ERROR_CODE;
150 }
151 NOTREACHED();
152 return mojom::ConnectErrorCode::UNTRANSLATED_CONNECT_ERROR_CODE;
153 }
85 } // namespace bluetooth 154 } // namespace bluetooth
OLDNEW
« 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