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

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

Issue 1749403002: Implement BluetoothRemoteGattCharacteristicWin::StartNotifySession and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Enable RememberCharacteristicForSubsequentAction and related tests Created 4 years, 9 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 "device/bluetooth/bluetooth_low_energy_win_fake.h" 5 #include "device/bluetooth/bluetooth_low_energy_win_fake.h"
6 6
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "device/bluetooth/bluetooth_low_energy_defs_win.h" 8 #include "device/bluetooth/bluetooth_low_energy_defs_win.h"
9 9
10 namespace { 10 namespace {
11 const char kPlatformNotSupported[] = 11 const char kPlatformNotSupported[] =
12 "Bluetooth Low energy is only supported on Windows 8 and later."; 12 "Bluetooth Low energy is only supported on Windows 8 and later.";
13 } // namespace 13 } // namespace
14 14
15 namespace device { 15 namespace device {
16 namespace win { 16 namespace win {
17 17
18 BLEDevice::BLEDevice() {} 18 BLEDevice::BLEDevice() {}
19 BLEDevice::~BLEDevice() {} 19 BLEDevice::~BLEDevice() {}
20 20
21 GattService::GattService() {} 21 GattService::GattService() {}
22 GattService::~GattService() {} 22 GattService::~GattService() {}
23 23
24 GattCharacteristic::GattCharacteristic() {} 24 GattCharacteristic::GattCharacteristic() {}
25 GattCharacteristic::~GattCharacteristic() {} 25 GattCharacteristic::~GattCharacteristic() {}
26 26
27 GattDescriptor::GattDescriptor() {} 27 GattDescriptor::GattDescriptor() {}
28 GattDescriptor::~GattDescriptor() {} 28 GattDescriptor::~GattDescriptor() {}
29 29
30 GattCharacteristicObserver::GattCharacteristicObserver() {}
31 GattCharacteristicObserver::~GattCharacteristicObserver() {}
32
30 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() 33 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake()
31 : observer_(nullptr) {} 34 : observer_(nullptr) {}
32 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} 35 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {}
33 36
34 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { 37 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() {
35 return true; 38 return true;
36 } 39 }
37 40
38 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( 41 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices(
39 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, 42 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices,
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 out_included_descriptors->reset(win_descriptors_info); 182 out_included_descriptors->reset(win_descriptors_info);
180 return S_OK; 183 return S_OK;
181 } 184 }
182 185
183 HRESULT BluetoothLowEnergyWrapperFake::ReadCharacteristicValue( 186 HRESULT BluetoothLowEnergyWrapperFake::ReadCharacteristicValue(
184 base::FilePath& service_path, 187 base::FilePath& service_path,
185 const PBTH_LE_GATT_CHARACTERISTIC characteristic, 188 const PBTH_LE_GATT_CHARACTERISTIC characteristic,
186 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) { 189 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) {
187 GattCharacteristic* target_characteristic = 190 GattCharacteristic* target_characteristic =
188 GetSimulatedGattCharacteristic(service_path, characteristic); 191 GetSimulatedGattCharacteristic(service_path, characteristic);
189 if (target_characteristic == nullptr) 192 if (target_characteristic == nullptr) {
190 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); 193 if (remembered_characteristic_ &&
ortuno 2016/03/21 17:00:53 I don't think you need all this logic and the erro
gogerald1 2016/03/21 22:09:16 Done.
194 remembered_characteristic_->characteristic_info->AttributeHandle ==
195 characteristic->AttributeHandle) {
196 target_characteristic = remembered_characteristic_.get();
197 } else {
198 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
199 }
200 }
191 201
192 // Return error simulated by SimulateGattCharacteristicReadError. 202 // Return error simulated by SimulateGattCharacteristicReadError.
193 if (target_characteristic->read_errors.size()) { 203 if (target_characteristic->read_errors.size()) {
194 HRESULT hr = target_characteristic->read_errors[0]; 204 HRESULT hr = target_characteristic->read_errors[0];
195 target_characteristic->read_errors.erase( 205 target_characteristic->read_errors.erase(
196 target_characteristic->read_errors.begin()); 206 target_characteristic->read_errors.begin());
197 return hr; 207 return hr;
198 } 208 }
199 209
200 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value = 210 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value =
201 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 211 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
202 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]); 212 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]);
203 ret_value->DataSize = target_characteristic->value->DataSize; 213 ret_value->DataSize = target_characteristic->value->DataSize;
204 for (ULONG i = 0; i < ret_value->DataSize; i++) 214 for (ULONG i = 0; i < ret_value->DataSize; i++)
205 ret_value->Data[i] = target_characteristic->value->Data[i]; 215 ret_value->Data[i] = target_characteristic->value->Data[i];
206 out_value->reset(ret_value); 216 out_value->reset(ret_value);
217 if (observer_)
218 observer_->OnReadGattCharacteristicValue();
207 return S_OK; 219 return S_OK;
208 } 220 }
209 221
210 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue( 222 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue(
211 base::FilePath& service_path, 223 base::FilePath& service_path,
212 const PBTH_LE_GATT_CHARACTERISTIC characteristic, 224 const PBTH_LE_GATT_CHARACTERISTIC characteristic,
213 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) { 225 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) {
214 GattCharacteristic* target_characteristic = 226 GattCharacteristic* target_characteristic =
215 GetSimulatedGattCharacteristic(service_path, characteristic); 227 GetSimulatedGattCharacteristic(service_path, characteristic);
216 if (target_characteristic == nullptr) 228 if (target_characteristic == nullptr) {
217 return ERROR_NOT_FOUND; 229 if (remembered_characteristic_ &&
ortuno 2016/03/21 17:00:53 Same as above. No need for the logic and the error
gogerald1 2016/03/21 22:09:16 Done.
230 remembered_characteristic_->characteristic_info->AttributeHandle ==
231 characteristic->AttributeHandle) {
232 target_characteristic = remembered_characteristic_.get();
233 } else {
234 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
235 }
236 }
218 237
219 // Return error simulated by SimulateGattCharacteristicWriteError. 238 // Return error simulated by SimulateGattCharacteristicWriteError.
220 if (target_characteristic->write_errors.size()) { 239 if (target_characteristic->write_errors.size()) {
221 HRESULT hr = *(target_characteristic->write_errors.begin()); 240 HRESULT hr = *(target_characteristic->write_errors.begin());
222 target_characteristic->write_errors.erase( 241 target_characteristic->write_errors.erase(
223 target_characteristic->write_errors.begin()); 242 target_characteristic->write_errors.begin());
224 return hr; 243 return hr;
225 } 244 }
226 245
227 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 246 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
228 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 247 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
229 new UCHAR[new_value->DataSize + sizeof(ULONG)]); 248 new UCHAR[new_value->DataSize + sizeof(ULONG)]);
230 for (ULONG i = 0; i < new_value->DataSize; i++) 249 for (ULONG i = 0; i < new_value->DataSize; i++)
231 win_value->Data[i] = new_value->Data[i]; 250 win_value->Data[i] = new_value->Data[i];
232 win_value->DataSize = new_value->DataSize; 251 win_value->DataSize = new_value->DataSize;
233 target_characteristic->value.reset(win_value); 252 target_characteristic->value.reset(win_value);
234 if (observer_) 253 if (observer_)
235 observer_->onWriteGattCharacteristicValue(win_value); 254 observer_->OnWriteGattCharacteristicValue(win_value);
236 return S_OK; 255 return S_OK;
237 } 256 }
238 257
258 HRESULT BluetoothLowEnergyWrapperFake::RegisterGattEvents(
259 base::FilePath& service_path,
260 BTH_LE_GATT_EVENT_TYPE type,
261 PVOID event_parameter,
262 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback,
263 PVOID context,
264 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) {
265 // Right now, only CharacteristicValueChangedEvent is supported.
266 CHECK(CharacteristicValueChangedEvent == type);
267
268 scoped_ptr<GattCharacteristicObserver> observer(
269 new GattCharacteristicObserver());
270 observer->callback = callback;
271 observer->context = context;
272 *out_handle = (BLUETOOTH_GATT_EVENT_HANDLE)observer.get();
273 gatt_characteristic_observers_[*out_handle] = std::move(observer);
274
275 PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION parameter =
276 (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION)event_parameter;
277 for (USHORT i = 0; i < parameter->NumCharacteristics; i++) {
278 GattCharacteristic* target_characteristic = GetSimulatedGattCharacteristic(
279 service_path, &parameter->Characteristics[i]);
280 CHECK(target_characteristic);
281 target_characteristic->observers.push_back(*out_handle);
282 }
283
284 if (observer_)
285 observer_->OnStartCharacteristicNotification();
286
287 return S_OK;
288 }
289
290 HRESULT BluetoothLowEnergyWrapperFake::UnregisterGattEvent(
291 BLUETOOTH_GATT_EVENT_HANDLE event_handle) {
292 gatt_characteristic_observers_.erase(event_handle);
293 return S_OK;
294 }
295
239 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( 296 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice(
240 std::string device_name, 297 std::string device_name,
241 BLUETOOTH_ADDRESS device_address) { 298 BLUETOOTH_ADDRESS device_address) {
242 BLEDevice* device = new BLEDevice(); 299 BLEDevice* device = new BLEDevice();
243 BluetoothLowEnergyDeviceInfo* device_info = 300 BluetoothLowEnergyDeviceInfo* device_info =
244 new BluetoothLowEnergyDeviceInfo(); 301 new BluetoothLowEnergyDeviceInfo();
245 std::string string_device_address = 302 std::string string_device_address =
246 BluetoothAddressToCanonicalString(device_address); 303 BluetoothAddressToCanonicalString(device_address);
247 device_info->path = 304 device_info->path =
248 base::FilePath(GenerateBLEDevicePath(string_device_address)); 305 base::FilePath(GenerateBLEDevicePath(string_device_address));
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 GattCharacteristicsMap::iterator it = 417 GattCharacteristicsMap::iterator it =
361 parent_service->included_characteristics.find(attribute_handle); 418 parent_service->included_characteristics.find(attribute_handle);
362 if (it != parent_service->included_characteristics.end()) 419 if (it != parent_service->included_characteristics.end())
363 return it->second.get(); 420 return it->second.get();
364 return nullptr; 421 return nullptr;
365 } 422 }
366 423
367 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue( 424 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue(
368 GattCharacteristic* characteristic, 425 GattCharacteristic* characteristic,
369 const std::vector<uint8_t>& value) { 426 const std::vector<uint8_t>& value) {
370 CHECK(characteristic); 427 GattCharacteristic* target_characteristic = characteristic;
428 if (target_characteristic == nullptr)
429 target_characteristic = remembered_characteristic_.get();
430 CHECK(target_characteristic);
431
371 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 432 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
372 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 433 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
373 new UCHAR[value.size() + sizeof(ULONG)]); 434 new UCHAR[value.size() + sizeof(ULONG)]);
374 win_value->DataSize = (ULONG)value.size(); 435 win_value->DataSize = (ULONG)value.size();
375 for (std::size_t i = 0; i < value.size(); i++) 436 for (std::size_t i = 0; i < value.size(); i++)
376 win_value->Data[i] = value[i]; 437 win_value->Data[i] = value[i];
377 characteristic->value.reset(win_value); 438 target_characteristic->value.reset(win_value);
439 }
440
441 void BluetoothLowEnergyWrapperFake::
442 SimulateCharacteristicValueChangeNotification(
443 GattCharacteristic* characteristic) {
444 GattCharacteristic* target_characteristic = characteristic;
445 if (target_characteristic == nullptr)
446 target_characteristic = remembered_characteristic_.get();
447 CHECK(target_characteristic);
448 for (const auto& observer : target_characteristic->observers) {
449 GattCharacteristicObserverTable::const_iterator it =
450 gatt_characteristic_observers_.find(observer);
451 // Check if |observer| has been unregistered by UnregisterGattEvent.
452 if (it != gatt_characteristic_observers_.end()) {
453 BLUETOOTH_GATT_VALUE_CHANGED_EVENT event;
454 event.ChangedAttributeHandle =
455 target_characteristic->characteristic_info->AttributeHandle;
456 event.CharacteristicValueDataSize =
457 target_characteristic->value->DataSize + sizeof(ULONG);
458 event.CharacteristicValue = target_characteristic->value.get();
459 it->second->callback(CharacteristicValueChangedEvent, &event,
460 it->second->context);
461 }
462 }
378 } 463 }
379 464
380 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( 465 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError(
381 GattCharacteristic* characteristic, 466 GattCharacteristic* characteristic,
382 HRESULT error) { 467 HRESULT error) {
383 CHECK(characteristic); 468 CHECK(characteristic);
384 characteristic->read_errors.push_back(error); 469 characteristic->read_errors.push_back(error);
385 } 470 }
386 471
387 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( 472 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError(
388 GattCharacteristic* characteristic, 473 GattCharacteristic* characteristic,
389 HRESULT error) { 474 HRESULT error) {
390 CHECK(characteristic); 475 CHECK(characteristic);
391 characteristic->write_errors.push_back(error); 476 characteristic->write_errors.push_back(error);
392 } 477 }
393 478
479 void BluetoothLowEnergyWrapperFake::RememberCharacteristicForSubsequentAction(
480 GattService* parent_service,
481 std::string attribute_handle) {
482 CHECK(parent_service);
483 GattCharacteristicsMap::iterator it =
484 parent_service->included_characteristics.find(attribute_handle);
485 if (it != parent_service->included_characteristics.end())
ortuno 2016/03/21 17:00:53 DCHECK this. If someone passes in an non existing
gogerald1 2016/03/21 22:09:16 Done.
486 remembered_characteristic_ = std::move(it->second);
487 }
488
394 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( 489 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor(
395 std::string device_address, 490 std::string device_address,
396 GattCharacteristic* characteristic, 491 GattCharacteristic* characteristic,
397 const BTH_LE_UUID& uuid) { 492 const BTH_LE_UUID& uuid) {
398 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor()); 493 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor());
399 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); 494 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]);
400 descriptor->descriptor_info->DescriptorUuid = uuid; 495 descriptor->descriptor_info->DescriptorUuid = uuid;
401 descriptor->descriptor_info->AttributeHandle = 496 descriptor->descriptor_info->AttributeHandle =
402 GenerateAUniqueAttributeHandle(device_address); 497 GenerateAUniqueAttributeHandle(device_address);
403 characteristic->included_descriptors[std::to_string( 498 characteristic->included_descriptors[std::to_string(
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( 603 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString(
509 const BLUETOOTH_ADDRESS& btha) { 604 const BLUETOOTH_ADDRESS& btha) {
510 std::string result = base::StringPrintf( 605 std::string result = base::StringPrintf(
511 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], 606 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4],
512 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); 607 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]);
513 return result; 608 return result;
514 } 609 }
515 610
516 } // namespace win 611 } // namespace win
517 } // namespace device 612 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698