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

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

Issue 2627243002: bluetooth: Add control for reading/writing of characteristics to internals page. (Closed)
Patch Set: DCHECK device 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 device::BluetoothRemoteGattService* service =
132 device->GetGattService(service_id);
133 if (service == nullptr) {
134 callback.Run(mojom::GattResult::FAILED, base::nullopt /* value */);
ortuno 2017/01/22 22:38:01 Would it be too much to add a specific error for t
mbrunson 2017/01/24 00:25:31 Done.
135 return;
136 }
137
138 device::BluetoothRemoteGattCharacteristic* characteristic =
139 service->GetCharacteristic(characteristic_id);
140 if (characteristic == nullptr) {
141 callback.Run(mojom::GattResult::FAILED, base::nullopt /* value */);
142 return;
143 }
144
145 characteristic->ReadRemoteCharacteristic(
146 base::Bind(&Device::OnReadRemoteCharacteristic,
147 weak_ptr_factory_.GetWeakPtr(), callback),
148 base::Bind(&Device::OnReadRemoteCharacteristicError,
149 weak_ptr_factory_.GetWeakPtr(), callback));
150 }
151
152 void Device::WriteValueForCharacteristic(
153 const std::string& service_id,
154 const std::string& characteristic_id,
155 const std::vector<uint8_t>& value,
156 const WriteValueForCharacteristicCallback& callback) {
157 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
158 DCHECK(device);
159
160 device::BluetoothRemoteGattService* service =
161 device->GetGattService(service_id);
162 if (service == nullptr) {
163 callback.Run(mojom::GattResult::FAILED);
164 return;
165 }
166
167 device::BluetoothRemoteGattCharacteristic* characteristic =
168 service->GetCharacteristic(characteristic_id);
169 if (characteristic == nullptr) {
170 callback.Run(mojom::GattResult::FAILED);
171 return;
172 }
173
174 characteristic->WriteRemoteCharacteristic(
175 value, base::Bind(&Device::OnWriteRemoteCharacteristic,
176 weak_ptr_factory_.GetWeakPtr(), callback),
177 base::Bind(&Device::OnWriteRemoteCharacteristicError,
178 weak_ptr_factory_.GetWeakPtr(), callback));
179 }
180
121 void Device::GetServicesImpl(const GetServicesCallback& callback) { 181 void Device::GetServicesImpl(const GetServicesCallback& callback) {
122 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); 182 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
123 DCHECK(device); 183 DCHECK(device);
124 184
125 std::vector<mojom::ServiceInfoPtr> services; 185 std::vector<mojom::ServiceInfoPtr> services;
126 186
127 for (const device::BluetoothRemoteGattService* service : 187 for (const device::BluetoothRemoteGattService* service :
128 device->GetGattServices()) { 188 device->GetGattServices()) {
129 services.push_back(ConstructServiceInfoStruct(*service)); 189 services.push_back(ConstructServiceInfoStruct(*service));
130 } 190 }
(...skipping 23 matching lines...) Expand all
154 214
155 std::vector<mojom::CharacteristicInfoPtr> characteristics; 215 std::vector<mojom::CharacteristicInfoPtr> characteristics;
156 216
157 for (const auto* characteristic : service->GetCharacteristics()) { 217 for (const auto* characteristic : service->GetCharacteristics()) {
158 mojom::CharacteristicInfoPtr characteristic_info = 218 mojom::CharacteristicInfoPtr characteristic_info =
159 mojom::CharacteristicInfo::New(); 219 mojom::CharacteristicInfo::New();
160 220
161 characteristic_info->id = characteristic->GetIdentifier(); 221 characteristic_info->id = characteristic->GetIdentifier();
162 characteristic_info->uuid = characteristic->GetUUID(); 222 characteristic_info->uuid = characteristic->GetUUID();
163 characteristic_info->properties = characteristic->GetProperties(); 223 characteristic_info->properties = characteristic->GetProperties();
224 characteristic_info->permissions = characteristic->GetPermissions();
225 characteristic_info->last_known_value = characteristic->GetValue();
164 226
165 characteristics.push_back(std::move(characteristic_info)); 227 characteristics.push_back(std::move(characteristic_info));
166 } 228 }
167 229
168 callback.Run(std::move(characteristics)); 230 callback.Run(std::move(characteristics));
169 } 231 }
170 232
233 void Device::OnReadRemoteCharacteristic(
234 const ReadValueForCharacteristicCallback& callback,
235 const std::vector<uint8_t>& value) {
236 callback.Run(mojom::GattResult::SUCCESS, std::move(value));
237 }
238
239 void Device::OnReadRemoteCharacteristicError(
240 const ReadValueForCharacteristicCallback& callback,
241 device::BluetoothGattService::GattErrorCode error_code) {
242 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code),
243 base::nullopt /* value */);
244 }
245
246 void Device::OnWriteRemoteCharacteristic(
247 const WriteValueForCharacteristicCallback& callback) {
248 callback.Run(mojom::GattResult::SUCCESS);
249 }
250
251 void Device::OnWriteRemoteCharacteristicError(
252 const WriteValueForCharacteristicCallback& callback,
253 device::BluetoothGattService::GattErrorCode error_code) {
254 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code));
255 }
256
171 const std::string& Device::GetAddress() { 257 const std::string& Device::GetAddress() {
172 return connection_->GetDeviceAddress(); 258 return connection_->GetDeviceAddress();
173 } 259 }
174 260
175 } // namespace bluetooth 261 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698