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

Side by Side Diff: device/bluetooth/device.cc

Issue 2627243002: bluetooth: Add control for reading/writing of characteristics to internals page. (Closed)
Patch Set: Rename last_value, cleanup Created 3 years, 11 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 unified diff | Download patch
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 <utility> 5 #include <utility>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "device/bluetooth/device.h" 10 #include "device/bluetooth/device.h"
11 #include "device/bluetooth/public/interfaces/gatt_result_type_converter.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 {
14 Device::~Device() { 15 Device::~Device() {
15 adapter_->RemoveObserver(this); 16 adapter_->RemoveObserver(this);
16 } 17 }
17 18
18 // static 19 // static
19 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, 20 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter,
20 std::unique_ptr<device::BluetoothGattConnection> connection, 21 std::unique_ptr<device::BluetoothGattConnection> connection,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 108
108 // pending_services_requests_ is owned by Device, so base::Unretained is 109 // pending_services_requests_ is owned by Device, so base::Unretained is
109 // safe. 110 // safe.
110 pending_services_requests_.push_back( 111 pending_services_requests_.push_back(
111 base::Bind(&Device::GetCharacteristicsImpl, base::Unretained(this), 112 base::Bind(&Device::GetCharacteristicsImpl, base::Unretained(this),
112 service_id, callback)); 113 service_id, callback));
113 } 114 }
114 115
115 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, 116 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter,
116 std::unique_ptr<device::BluetoothGattConnection> connection) 117 std::unique_ptr<device::BluetoothGattConnection> connection)
117 : adapter_(std::move(adapter)), connection_(std::move(connection)) { 118 : adapter_(std::move(adapter)),
119 connection_(std::move(connection)),
120 weak_ptr_factory_(this) {
118 adapter_->AddObserver(this); 121 adapter_->AddObserver(this);
119 } 122 }
120 123
124 void Device::ReadValueForCharacteristic(
125 const std::string& service_id,
126 const std::string& characteristic_id,
127 const ReadValueForCharacteristicCallback& callback) {
128 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
129 DCHECK(device);
130
131 if (device->IsGattServicesDiscoveryComplete()) {
132 ReadValueForCharacteristicImpl(service_id, characteristic_id, callback);
133 return;
134 }
135
136 // pending_services_requests_ is owned by Device, so base::Unretained is
137 // safe.
138 pending_services_requests_.push_back(base::Bind(
ortuno 2017/01/20 03:45:22 q: Why do we queue instead of just returning an er
139 &Device::ReadValueForCharacteristicImpl, base::Unretained(this),
140 service_id, characteristic_id, callback));
141 }
142
143 void Device::WriteValueForCharacteristic(
144 const std::vector<uint8_t>& value,
145 const std::string& service_id,
146 const std::string& characteristic_id,
147 const WriteValueForCharacteristicCallback& callback) {
148 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
149 DCHECK(device);
150
151 if (device->IsGattServicesDiscoveryComplete()) {
152 WriteValueForCharacteristicImpl(value, service_id, characteristic_id,
153 callback);
154 return;
155 }
156
157 // pending_services_requests_ is owned by Device, so base::Unretained is
158 // safe.
159 pending_services_requests_.push_back(base::Bind(
160 &Device::WriteValueForCharacteristicImpl, base::Unretained(this), value,
161 service_id, characteristic_id, callback));
162 }
163
121 void Device::GetServicesImpl(const GetServicesCallback& callback) { 164 void Device::GetServicesImpl(const GetServicesCallback& callback) {
122 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); 165 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
123 DCHECK(device); 166 DCHECK(device);
124 167
125 std::vector<mojom::ServiceInfoPtr> services; 168 std::vector<mojom::ServiceInfoPtr> services;
126 169
127 for (const device::BluetoothRemoteGattService* service : 170 for (const device::BluetoothRemoteGattService* service :
128 device->GetGattServices()) { 171 device->GetGattServices()) {
129 services.push_back(ConstructServiceInfoStruct(*service)); 172 services.push_back(ConstructServiceInfoStruct(*service));
130 } 173 }
(...skipping 23 matching lines...) Expand all
154 197
155 std::vector<mojom::CharacteristicInfoPtr> characteristics; 198 std::vector<mojom::CharacteristicInfoPtr> characteristics;
156 199
157 for (const auto* characteristic : service->GetCharacteristics()) { 200 for (const auto* characteristic : service->GetCharacteristics()) {
158 mojom::CharacteristicInfoPtr characteristic_info = 201 mojom::CharacteristicInfoPtr characteristic_info =
159 mojom::CharacteristicInfo::New(); 202 mojom::CharacteristicInfo::New();
160 203
161 characteristic_info->id = characteristic->GetIdentifier(); 204 characteristic_info->id = characteristic->GetIdentifier();
162 characteristic_info->uuid = characteristic->GetUUID(); 205 characteristic_info->uuid = characteristic->GetUUID();
163 characteristic_info->properties = characteristic->GetProperties(); 206 characteristic_info->properties = characteristic->GetProperties();
207 characteristic_info->permissions = characteristic->GetPermissions();
208 characteristic_info->value = characteristic->GetValue();
164 209
165 characteristics.push_back(std::move(characteristic_info)); 210 characteristics.push_back(std::move(characteristic_info));
166 } 211 }
167 212
168 callback.Run(std::move(characteristics)); 213 callback.Run(std::move(characteristics));
169 } 214 }
170 215
216 void Device::ReadValueForCharacteristicImpl(
217 const std::string& service_id,
218 const std::string& characteristic_id,
219 const ReadValueForCharacteristicCallback& callback) {
220 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
221 DCHECK(device);
222 device::BluetoothRemoteGattService* service =
223 device->GetGattService(service_id);
224 DCHECK(service);
ortuno 2017/01/20 03:45:22 There are no guarantees, in code or docs, that the
mbrunson 2017/01/21 01:49:00 Ok. Done.
225 device::BluetoothRemoteGattCharacteristic* characteristic =
226 service->GetCharacteristic(characteristic_id);
227 DCHECK(characteristic);
228
229 characteristic->ReadRemoteCharacteristic(
230 base::Bind(&Device::OnReadRemoteCharacteristic,
231 weak_ptr_factory_.GetWeakPtr(), callback),
232 base::Bind(&Device::OnReadRemoteCharacteristicError,
233 weak_ptr_factory_.GetWeakPtr(), callback));
234 }
235
236 void Device::OnReadRemoteCharacteristic(
237 const ReadValueForCharacteristicCallback& callback,
238 const std::vector<uint8_t>& value) {
239 callback.Run(mojom::GattResult::SUCCESS, std::move(value));
240 }
241
242 void Device::OnReadRemoteCharacteristicError(
243 const ReadValueForCharacteristicCallback& callback,
244 device::BluetoothGattService::GattErrorCode error_code) {
245 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code),
246 std::vector<uint8_t>());
247 }
248
249 void Device::WriteValueForCharacteristicImpl(
250 const std::vector<uint8_t>& value,
251 const std::string& service_id,
252 const std::string& characteristic_id,
253 const WriteValueForCharacteristicCallback& callback) {
254 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
255 DCHECK(device);
256 device::BluetoothRemoteGattService* service =
257 device->GetGattService(service_id);
258 DCHECK(service);
259 device::BluetoothRemoteGattCharacteristic* characteristic =
260 service->GetCharacteristic(characteristic_id);
261 DCHECK(characteristic);
262
263 characteristic->WriteRemoteCharacteristic(
264 value, base::Bind(&Device::OnWriteRemoteCharacteristic,
265 weak_ptr_factory_.GetWeakPtr(), callback),
266 base::Bind(&Device::OnWriteRemoteCharacteristicError,
267 weak_ptr_factory_.GetWeakPtr(), callback));
268 }
269
270 void Device::OnWriteRemoteCharacteristic(
271 const WriteValueForCharacteristicCallback& callback) {
272 callback.Run(mojom::GattResult::SUCCESS);
273 }
274
275 void Device::OnWriteRemoteCharacteristicError(
276 const WriteValueForCharacteristicCallback& callback,
277 device::BluetoothGattService::GattErrorCode error_code) {
278 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code));
279 }
280
171 const std::string& Device::GetAddress() { 281 const std::string& Device::GetAddress() {
172 return connection_->GetDeviceAddress(); 282 return connection_->GetDeviceAddress();
173 } 283 }
174 284
175 } // namespace bluetooth 285 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698