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

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: adjust comments 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 GattServiceObserver::GattServiceObserver() {}
31 GattServiceObserver::~GattServiceObserver() {}
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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 } 228 }
226 229
227 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 230 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
228 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 231 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
229 new UCHAR[new_value->DataSize + sizeof(ULONG)]); 232 new UCHAR[new_value->DataSize + sizeof(ULONG)]);
230 for (ULONG i = 0; i < new_value->DataSize; i++) 233 for (ULONG i = 0; i < new_value->DataSize; i++)
231 win_value->Data[i] = new_value->Data[i]; 234 win_value->Data[i] = new_value->Data[i];
232 win_value->DataSize = new_value->DataSize; 235 win_value->DataSize = new_value->DataSize;
233 target_characteristic->value.reset(win_value); 236 target_characteristic->value.reset(win_value);
234 if (observer_) 237 if (observer_)
235 observer_->onWriteGattCharacteristicValue(win_value); 238 observer_->OnWriteGattCharacteristicValue(win_value);
236 return S_OK; 239 return S_OK;
237 } 240 }
238 241
242 HRESULT BluetoothLowEnergyWrapperFake::RegisterGattEvents(
243 base::FilePath& service_path,
244 BTH_LE_GATT_EVENT_TYPE type,
245 PVOID event_parameter,
246 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback,
247 PVOID context,
248 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) {
249 base::string16 device_address =
250 ExtractDeviceAddressFromDevicePath(service_path.value());
251 BLEDevice* target_device = GetSimulatedBLEDevice(
252 std::string(device_address.begin(), device_address.end()));
253 if (target_device == nullptr)
254 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
255 const std::vector<std::string> service_att_handles =
256 ExtractServiceAttributeHandlesFromDevicePath(service_path.value());
257 GattService* target_service =
258 GetSimulatedGattService(target_device, service_att_handles);
259 if (target_service == nullptr)
260 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
261
262 PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION parameter =
263 (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION)event_parameter;
264 for (USHORT i = 0; i < parameter->NumCharacteristics; i++) {
265 GattCharacteristic* target_characteristic = GetSimulatedGattCharacteristic(
266 target_service,
267 std::to_string(parameter->Characteristics[i].AttributeHandle));
268 CHECK(target_characteristic);
269
270 // Return error simulated by SimulateGattCharacteristicSetNotifyError.
271 if (target_characteristic->notify_errors.size()) {
272 HRESULT error = target_characteristic->notify_errors[0];
273 target_characteristic->notify_errors.erase(
274 target_characteristic->notify_errors.begin());
275 return error;
276 }
277
278 // Get CCC descriptor.
279 GattDescriptor* target_descriptor = GetCCCDescriptor(target_characteristic);
280 if (target_descriptor == nullptr)
281 return E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED;
282 if (observer_)
283 observer_->OnStartCharacteristicNotification();
284
285 // Return error simualted by SimulateGattDescriptorWriteError.
286 if (target_descriptor->write_errors.size()) {
287 HRESULT error = target_descriptor->write_errors[0];
288 target_descriptor->write_errors.erase(
289 target_descriptor->write_errors.begin());
290 return error;
291 }
292
293 // Write CCC descriptor.
294 std::vector<uint8_t> new_value;
295 if (target_characteristic->characteristic_info->IsNotifiable)
296 new_value.push_back(1);
297 else if (target_characteristic->characteristic_info->IsIndicatable)
298 new_value.push_back(2);
299 else
300 return E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED;
301 new_value.push_back(0);
302 SimulateGattDescriptorWrite(target_descriptor, new_value);
303 }
304
305 scoped_ptr<GattServiceObserver> observer(new GattServiceObserver());
306 observer->callback = callback;
307 observer->context = context;
308 BLUETOOTH_GATT_EVENT_HANDLE handle =
309 (BLUETOOTH_GATT_EVENT_HANDLE)observer.get();
310 gatt_service_observers_[handle] = std::move(observer);
311 target_service->observers.push_back(handle);
312
313 return S_OK;
314 }
315
316 HRESULT BluetoothLowEnergyWrapperFake::UnregisterGattEvent(
317 BLUETOOTH_GATT_EVENT_HANDLE event_handle) {
318 gatt_service_observers_.erase(event_handle);
319 return S_OK;
320 }
321
239 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( 322 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice(
240 std::string device_name, 323 std::string device_name,
241 BLUETOOTH_ADDRESS device_address) { 324 BLUETOOTH_ADDRESS device_address) {
242 BLEDevice* device = new BLEDevice(); 325 BLEDevice* device = new BLEDevice();
243 BluetoothLowEnergyDeviceInfo* device_info = 326 BluetoothLowEnergyDeviceInfo* device_info =
244 new BluetoothLowEnergyDeviceInfo(); 327 new BluetoothLowEnergyDeviceInfo();
245 std::string string_device_address = 328 std::string string_device_address =
246 BluetoothAddressToCanonicalString(device_address); 329 BluetoothAddressToCanonicalString(device_address);
247 device_info->path = 330 device_info->path =
248 base::FilePath(GenerateBLEDevicePath(string_device_address)); 331 base::FilePath(GenerateBLEDevicePath(string_device_address));
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 CHECK(characteristic); 453 CHECK(characteristic);
371 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 454 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
372 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 455 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
373 new UCHAR[value.size() + sizeof(ULONG)]); 456 new UCHAR[value.size() + sizeof(ULONG)]);
374 win_value->DataSize = (ULONG)value.size(); 457 win_value->DataSize = (ULONG)value.size();
375 for (std::size_t i = 0; i < value.size(); i++) 458 for (std::size_t i = 0; i < value.size(); i++)
376 win_value->Data[i] = value[i]; 459 win_value->Data[i] = value[i];
377 characteristic->value.reset(win_value); 460 characteristic->value.reset(win_value);
378 } 461 }
379 462
463 void BluetoothLowEnergyWrapperFake::
464 SimulateCharacteristicValueChangeNotification(
465 GattService* parent_service,
466 GattCharacteristic* characteristic) {
467 for (auto observer : parent_service->observers) {
468 GattServiceObserverTable::iterator it =
469 gatt_service_observers_.find(observer);
470 // Check if |observer| has been unregistered by UnregisterGattEvent.
471 if (it != gatt_service_observers_.end()) {
472 BLUETOOTH_GATT_VALUE_CHANGED_EVENT event;
473 event.ChangedAttributeHandle =
474 characteristic->characteristic_info->AttributeHandle;
475 event.CharacteristicValueDataSize =
476 characteristic->value->DataSize + (ULONG)sizeof(ULONG);
477 event.CharacteristicValue = characteristic->value.get();
478 it->second->callback(CharacteristicValueChangedEvent, &event,
479 it->second->context);
480 }
481 }
482 }
483
380 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( 484 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError(
381 GattCharacteristic* characteristic, 485 GattCharacteristic* characteristic,
382 HRESULT error) { 486 HRESULT error) {
383 CHECK(characteristic); 487 CHECK(characteristic);
384 characteristic->read_errors.push_back(error); 488 characteristic->read_errors.push_back(error);
385 } 489 }
386 490
387 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( 491 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError(
388 GattCharacteristic* characteristic, 492 GattCharacteristic* characteristic,
389 HRESULT error) { 493 HRESULT error) {
390 CHECK(characteristic); 494 CHECK(characteristic);
391 characteristic->write_errors.push_back(error); 495 characteristic->write_errors.push_back(error);
392 } 496 }
393 497
498 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicSetNotifyError(
499 GattCharacteristic* characteristic,
500 HRESULT error) {
501 CHECK(characteristic);
502 characteristic->notify_errors.push_back(error);
503 }
504
394 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( 505 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor(
395 std::string device_address, 506 std::string device_address,
396 GattCharacteristic* characteristic, 507 GattCharacteristic* characteristic,
397 const BTH_LE_UUID& uuid) { 508 const BTH_LE_UUID& uuid) {
398 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor()); 509 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor());
399 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); 510 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]);
400 descriptor->descriptor_info->DescriptorUuid = uuid; 511 descriptor->descriptor_info->DescriptorUuid = uuid;
401 descriptor->descriptor_info->AttributeHandle = 512 descriptor->descriptor_info->AttributeHandle =
402 GenerateAUniqueAttributeHandle(device_address); 513 GenerateAUniqueAttributeHandle(device_address);
403 characteristic->included_descriptors[std::to_string( 514 characteristic->included_descriptors[std::to_string(
404 descriptor->descriptor_info->AttributeHandle)] = std::move(descriptor); 515 descriptor->descriptor_info->AttributeHandle)] = std::move(descriptor);
405 } 516 }
406 517
518 GattDescriptor* BluetoothLowEnergyWrapperFake::GetSimulatedDescriptor(
519 GattCharacteristic* parent_characteristic,
520 std::string attribute_handle) {
521 GattDescriptorsMap::const_iterator it =
522 parent_characteristic->included_descriptors.find(attribute_handle);
523 if (it != parent_characteristic->included_descriptors.end())
524 return it->second.get();
525 return nullptr;
526 }
527
528 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptorWrite(
529 GattDescriptor* descriptor,
530 const std::vector<uint8_t>& value) {
531 ULONG length = (ULONG)(sizeof(BTH_LE_GATT_DESCRIPTOR_VALUE) + value.size());
532 descriptor->value.reset((BTH_LE_GATT_DESCRIPTOR_VALUE*)(new UCHAR[length]));
533 descriptor->value->DataSize = (ULONG)value.size();
534 for (size_t i = 0; i < value.size(); i++)
535 descriptor->value->Data[i] = value[i];
536 if (observer_)
537 observer_->OnWriteGattDescriptorValue(descriptor->value.get());
538 }
539
540 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptorWriteError(
541 GattDescriptor* descriptor,
542 HRESULT error) {
543 descriptor->write_errors.push_back(error);
544 }
545
407 void BluetoothLowEnergyWrapperFake::AddObserver(Observer* observer) { 546 void BluetoothLowEnergyWrapperFake::AddObserver(Observer* observer) {
408 observer_ = observer; 547 observer_ = observer;
409 } 548 }
410 549
411 GattCharacteristic* 550 GattCharacteristic*
412 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( 551 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic(
413 base::FilePath& service_path, 552 base::FilePath& service_path,
414 const PBTH_LE_GATT_CHARACTERISTIC characteristic) { 553 const PBTH_LE_GATT_CHARACTERISTIC characteristic) {
415 base::string16 device_address = 554 base::string16 device_address =
416 ExtractDeviceAddressFromDevicePath(service_path.value()); 555 ExtractDeviceAddressFromDevicePath(service_path.value());
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 } 645 }
507 646
508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( 647 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString(
509 const BLUETOOTH_ADDRESS& btha) { 648 const BLUETOOTH_ADDRESS& btha) {
510 std::string result = base::StringPrintf( 649 std::string result = base::StringPrintf(
511 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], 650 "%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]); 651 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]);
513 return result; 652 return result;
514 } 653 }
515 654
655 GattDescriptor* BluetoothLowEnergyWrapperFake::GetCCCDescriptor(
656 GattCharacteristic* characteristic) {
657 for (const auto& d : characteristic->included_descriptors) {
658 const BTH_LE_UUID* uuid = &(d.second->descriptor_info->DescriptorUuid);
659 if (uuid->IsShortUuid)
660 continue;
661
662 std::string uuid_string = base::StringPrintf(
663 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
664 uuid->Value.LongUuid.Data1, uuid->Value.LongUuid.Data2,
665 uuid->Value.LongUuid.Data3, uuid->Value.LongUuid.Data4[0],
666 uuid->Value.LongUuid.Data4[1], uuid->Value.LongUuid.Data4[2],
667 uuid->Value.LongUuid.Data4[3], uuid->Value.LongUuid.Data4[4],
668 uuid->Value.LongUuid.Data4[5], uuid->Value.LongUuid.Data4[6],
669 uuid->Value.LongUuid.Data4[7]);
670 if (uuid_string != "00002902-0000-1000-8000-00805f9b34fb")
671 continue;
672
673 return d.second.get();
674 }
675
676 return nullptr;
677 }
678
516 } // namespace win 679 } // namespace win
517 } // namespace device 680 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698