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

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

Powered by Google App Engine
This is Rietveld 408576698