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

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

Issue 2627243002: bluetooth: Add control for reading/writing of characteristics to internals page. (Closed)
Patch Set: Merge upstream, add comment detail for type converter 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
« no previous file with comments | « device/bluetooth/device.h ('k') | device/bluetooth/public/interfaces/device.mojom » ('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 <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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 characteristic_info->id = characteristic->GetIdentifier(); 117 characteristic_info->id = characteristic->GetIdentifier();
117 characteristic_info->uuid = characteristic->GetUUID(); 118 characteristic_info->uuid = characteristic->GetUUID();
118 characteristic_info->properties = characteristic->GetProperties(); 119 characteristic_info->properties = characteristic->GetProperties();
119 120
120 characteristics.push_back(std::move(characteristic_info)); 121 characteristics.push_back(std::move(characteristic_info));
121 } 122 }
122 123
123 callback.Run(std::move(characteristics)); 124 callback.Run(std::move(characteristics));
124 } 125 }
125 126
127 void Device::ReadValueForCharacteristic(
128 const std::string& service_id,
129 const std::string& characteristic_id,
130 const ReadValueForCharacteristicCallback& callback) {
131 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
132 DCHECK(device);
133
134 device::BluetoothRemoteGattService* service =
135 device->GetGattService(service_id);
136 if (service == nullptr) {
137 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND,
138 base::nullopt /* value */);
139 return;
140 }
141
142 device::BluetoothRemoteGattCharacteristic* characteristic =
143 service->GetCharacteristic(characteristic_id);
144 if (characteristic == nullptr) {
145 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND,
146 base::nullopt /* value */);
147 return;
148 }
149
150 characteristic->ReadRemoteCharacteristic(
151 base::Bind(&Device::OnReadRemoteCharacteristic,
152 weak_ptr_factory_.GetWeakPtr(), callback),
153 base::Bind(&Device::OnReadRemoteCharacteristicError,
154 weak_ptr_factory_.GetWeakPtr(), callback));
155 }
156
157 void Device::WriteValueForCharacteristic(
158 const std::string& service_id,
159 const std::string& characteristic_id,
160 const std::vector<uint8_t>& value,
161 const WriteValueForCharacteristicCallback& callback) {
162 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
163 DCHECK(device);
164
165 device::BluetoothRemoteGattService* service =
166 device->GetGattService(service_id);
167 if (service == nullptr) {
168 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND);
169 return;
170 }
171
172 device::BluetoothRemoteGattCharacteristic* characteristic =
173 service->GetCharacteristic(characteristic_id);
174 if (characteristic == nullptr) {
175 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND);
176 return;
177 }
178
179 characteristic->WriteRemoteCharacteristic(
180 value, base::Bind(&Device::OnWriteRemoteCharacteristic,
181 weak_ptr_factory_.GetWeakPtr(), callback),
182 base::Bind(&Device::OnWriteRemoteCharacteristicError,
183 weak_ptr_factory_.GetWeakPtr(), callback));
184 }
185
126 void Device::GetDescriptors(const std::string& service_id, 186 void Device::GetDescriptors(const std::string& service_id,
127 const std::string& characteristic_id, 187 const std::string& characteristic_id,
128 const GetDescriptorsCallback& callback) { 188 const GetDescriptorsCallback& callback) {
129 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); 189 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
130 if (!device) { 190 if (!device) {
131 callback.Run(base::nullopt); 191 callback.Run(base::nullopt);
132 return; 192 return;
133 } 193 }
134 194
135 device::BluetoothRemoteGattService* service = 195 device::BluetoothRemoteGattService* service =
(...skipping 18 matching lines...) Expand all
154 descriptor_info->id = descriptor->GetIdentifier(); 214 descriptor_info->id = descriptor->GetIdentifier();
155 descriptor_info->uuid = descriptor->GetUUID(); 215 descriptor_info->uuid = descriptor->GetUUID();
156 descriptors.push_back(std::move(descriptor_info)); 216 descriptors.push_back(std::move(descriptor_info));
157 } 217 }
158 218
159 callback.Run(std::move(descriptors)); 219 callback.Run(std::move(descriptors));
160 } 220 }
161 221
162 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, 222 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter,
163 std::unique_ptr<device::BluetoothGattConnection> connection) 223 std::unique_ptr<device::BluetoothGattConnection> connection)
164 : adapter_(std::move(adapter)), connection_(std::move(connection)) { 224 : adapter_(std::move(adapter)),
225 connection_(std::move(connection)),
226 weak_ptr_factory_(this) {
165 adapter_->AddObserver(this); 227 adapter_->AddObserver(this);
166 } 228 }
167 229
168 void Device::GetServicesImpl(const GetServicesCallback& callback) { 230 void Device::GetServicesImpl(const GetServicesCallback& callback) {
169 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); 231 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
170 DCHECK(device); 232 DCHECK(device);
171 233
172 std::vector<mojom::ServiceInfoPtr> services; 234 std::vector<mojom::ServiceInfoPtr> services;
173 235
174 for (const device::BluetoothRemoteGattService* service : 236 for (const device::BluetoothRemoteGattService* service :
175 device->GetGattServices()) { 237 device->GetGattServices()) {
176 services.push_back(ConstructServiceInfoStruct(*service)); 238 services.push_back(ConstructServiceInfoStruct(*service));
177 } 239 }
178 240
179 callback.Run(std::move(services)); 241 callback.Run(std::move(services));
180 } 242 }
181 243
182 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( 244 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct(
183 const device::BluetoothRemoteGattService& service) { 245 const device::BluetoothRemoteGattService& service) {
184 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); 246 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New();
185 247
186 service_info->id = service.GetIdentifier(); 248 service_info->id = service.GetIdentifier();
187 service_info->uuid = service.GetUUID(); 249 service_info->uuid = service.GetUUID();
188 service_info->is_primary = service.IsPrimary(); 250 service_info->is_primary = service.IsPrimary();
189 251
190 return service_info; 252 return service_info;
191 } 253 }
192 254
255 void Device::OnReadRemoteCharacteristic(
256 const ReadValueForCharacteristicCallback& callback,
257 const std::vector<uint8_t>& value) {
258 callback.Run(mojom::GattResult::SUCCESS, std::move(value));
259 }
260
261 void Device::OnReadRemoteCharacteristicError(
262 const ReadValueForCharacteristicCallback& callback,
263 device::BluetoothGattService::GattErrorCode error_code) {
264 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code),
265 base::nullopt /* value */);
266 }
267
268 void Device::OnWriteRemoteCharacteristic(
269 const WriteValueForCharacteristicCallback& callback) {
270 callback.Run(mojom::GattResult::SUCCESS);
271 }
272
273 void Device::OnWriteRemoteCharacteristicError(
274 const WriteValueForCharacteristicCallback& callback,
275 device::BluetoothGattService::GattErrorCode error_code) {
276 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code));
277 }
278
193 const std::string& Device::GetAddress() { 279 const std::string& Device::GetAddress() {
194 return connection_->GetDeviceAddress(); 280 return connection_->GetDeviceAddress();
195 } 281 }
196 282
197 } // namespace bluetooth 283 } // namespace bluetooth
OLDNEW
« no previous file with comments | « device/bluetooth/device.h ('k') | device/bluetooth/public/interfaces/device.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698