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

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: use static_cast Created 4 years, 8 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 target_characteristic = remembered_characteristic_.get();
194 DCHECK(target_characteristic);
195 DCHECK(target_characteristic->characteristic_info->AttributeHandle ==
196 characteristic->AttributeHandle);
197 }
191 198
192 // Return error simulated by SimulateGattCharacteristicReadError. 199 // Return error simulated by SimulateGattCharacteristicReadError.
193 if (target_characteristic->read_errors.size()) { 200 if (target_characteristic->read_errors.size()) {
194 HRESULT hr = target_characteristic->read_errors[0]; 201 HRESULT hr = target_characteristic->read_errors[0];
195 target_characteristic->read_errors.erase( 202 target_characteristic->read_errors.erase(
196 target_characteristic->read_errors.begin()); 203 target_characteristic->read_errors.begin());
197 return hr; 204 return hr;
198 } 205 }
199 206
200 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value = 207 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value =
201 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 208 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
202 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]); 209 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]);
203 ret_value->DataSize = target_characteristic->value->DataSize; 210 ret_value->DataSize = target_characteristic->value->DataSize;
204 for (ULONG i = 0; i < ret_value->DataSize; i++) 211 for (ULONG i = 0; i < ret_value->DataSize; i++)
205 ret_value->Data[i] = target_characteristic->value->Data[i]; 212 ret_value->Data[i] = target_characteristic->value->Data[i];
206 out_value->reset(ret_value); 213 out_value->reset(ret_value);
214 if (observer_)
215 observer_->OnReadGattCharacteristicValue();
207 return S_OK; 216 return S_OK;
208 } 217 }
209 218
210 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue( 219 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue(
211 base::FilePath& service_path, 220 base::FilePath& service_path,
212 const PBTH_LE_GATT_CHARACTERISTIC characteristic, 221 const PBTH_LE_GATT_CHARACTERISTIC characteristic,
213 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) { 222 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) {
214 GattCharacteristic* target_characteristic = 223 GattCharacteristic* target_characteristic =
215 GetSimulatedGattCharacteristic(service_path, characteristic); 224 GetSimulatedGattCharacteristic(service_path, characteristic);
216 if (target_characteristic == nullptr) 225 if (target_characteristic == nullptr) {
217 return ERROR_NOT_FOUND; 226 target_characteristic = remembered_characteristic_.get();
227 DCHECK(target_characteristic);
228 DCHECK(target_characteristic->characteristic_info->AttributeHandle ==
229 characteristic->AttributeHandle);
230 }
218 231
219 // Return error simulated by SimulateGattCharacteristicWriteError. 232 // Return error simulated by SimulateGattCharacteristicWriteError.
220 if (target_characteristic->write_errors.size()) { 233 if (target_characteristic->write_errors.size()) {
221 HRESULT hr = *(target_characteristic->write_errors.begin()); 234 HRESULT hr = *(target_characteristic->write_errors.begin());
222 target_characteristic->write_errors.erase( 235 target_characteristic->write_errors.erase(
223 target_characteristic->write_errors.begin()); 236 target_characteristic->write_errors.begin());
224 return hr; 237 return hr;
225 } 238 }
226 239
227 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 240 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
228 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 241 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
229 new UCHAR[new_value->DataSize + sizeof(ULONG)]); 242 new UCHAR[new_value->DataSize + sizeof(ULONG)]);
230 for (ULONG i = 0; i < new_value->DataSize; i++) 243 for (ULONG i = 0; i < new_value->DataSize; i++)
231 win_value->Data[i] = new_value->Data[i]; 244 win_value->Data[i] = new_value->Data[i];
232 win_value->DataSize = new_value->DataSize; 245 win_value->DataSize = new_value->DataSize;
233 target_characteristic->value.reset(win_value); 246 target_characteristic->value.reset(win_value);
234 if (observer_) 247 if (observer_)
235 observer_->onWriteGattCharacteristicValue(win_value); 248 observer_->OnWriteGattCharacteristicValue(win_value);
236 return S_OK; 249 return S_OK;
237 } 250 }
238 251
252 HRESULT BluetoothLowEnergyWrapperFake::RegisterGattEvents(
253 base::FilePath& service_path,
254 BTH_LE_GATT_EVENT_TYPE type,
255 PVOID event_parameter,
256 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback,
257 PVOID context,
258 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) {
259 // Right now, only CharacteristicValueChangedEvent is supported.
260 CHECK(CharacteristicValueChangedEvent == type);
261
262 scoped_ptr<GattCharacteristicObserver> observer(
263 new GattCharacteristicObserver());
264 observer->callback = callback;
265 observer->context = context;
266 *out_handle = (BLUETOOTH_GATT_EVENT_HANDLE)observer.get();
267 gatt_characteristic_observers_[*out_handle] = std::move(observer);
268
269 PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION parameter =
270 (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION)event_parameter;
271 for (USHORT i = 0; i < parameter->NumCharacteristics; i++) {
272 GattCharacteristic* target_characteristic = GetSimulatedGattCharacteristic(
273 service_path, &parameter->Characteristics[i]);
274 CHECK(target_characteristic);
275 target_characteristic->observers.push_back(*out_handle);
276 }
277
278 if (observer_)
279 observer_->OnStartCharacteristicNotification();
280
281 return S_OK;
282 }
283
284 HRESULT BluetoothLowEnergyWrapperFake::UnregisterGattEvent(
285 BLUETOOTH_GATT_EVENT_HANDLE event_handle) {
286 gatt_characteristic_observers_.erase(event_handle);
287 return S_OK;
288 }
289
290 HRESULT BluetoothLowEnergyWrapperFake::WriteDescriptorValue(
291 base::FilePath& service_path,
292 const PBTH_LE_GATT_DESCRIPTOR descriptor,
293 PBTH_LE_GATT_DESCRIPTOR_VALUE new_value) {
294 if (new_value->DescriptorType == ClientCharacteristicConfiguration) {
295 // Simulate the value the OS will write.
296 std::vector<UCHAR> write_value;
297 if (new_value->ClientCharacteristicConfiguration
298 .IsSubscribeToNotification) {
299 write_value.push_back(1);
300 } else if (new_value->ClientCharacteristicConfiguration
301 .IsSubscribeToIndication) {
302 write_value.push_back(2);
303 }
304 write_value.push_back(0);
305 if (observer_)
306 observer_->OnWriteGattDescriptorValue(write_value);
307 }
308 return S_OK;
309 }
310
239 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( 311 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice(
240 std::string device_name, 312 std::string device_name,
241 BLUETOOTH_ADDRESS device_address) { 313 BLUETOOTH_ADDRESS device_address) {
242 BLEDevice* device = new BLEDevice(); 314 BLEDevice* device = new BLEDevice();
243 BluetoothLowEnergyDeviceInfo* device_info = 315 BluetoothLowEnergyDeviceInfo* device_info =
244 new BluetoothLowEnergyDeviceInfo(); 316 new BluetoothLowEnergyDeviceInfo();
245 std::string string_device_address = 317 std::string string_device_address =
246 BluetoothAddressToCanonicalString(device_address); 318 BluetoothAddressToCanonicalString(device_address);
247 device_info->path = 319 device_info->path =
248 base::FilePath(GenerateBLEDevicePath(string_device_address)); 320 base::FilePath(GenerateBLEDevicePath(string_device_address));
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 GattCharacteristic* win_characteristic = new GattCharacteristic(); 407 GattCharacteristic* win_characteristic = new GattCharacteristic();
336 PBTH_LE_GATT_CHARACTERISTIC win_characteristic_info = 408 PBTH_LE_GATT_CHARACTERISTIC win_characteristic_info =
337 new BTH_LE_GATT_CHARACTERISTIC[1]; 409 new BTH_LE_GATT_CHARACTERISTIC[1];
338 *win_characteristic_info = characteristic; 410 *win_characteristic_info = characteristic;
339 (win_characteristic->characteristic_info).reset(win_characteristic_info); 411 (win_characteristic->characteristic_info).reset(win_characteristic_info);
340 win_characteristic->characteristic_info->AttributeHandle = 412 win_characteristic->characteristic_info->AttributeHandle =
341 GenerateAUniqueAttributeHandle(device_address); 413 GenerateAUniqueAttributeHandle(device_address);
342 parent_service->included_characteristics[std::to_string( 414 parent_service->included_characteristics[std::to_string(
343 win_characteristic->characteristic_info->AttributeHandle)] = 415 win_characteristic->characteristic_info->AttributeHandle)] =
344 make_scoped_ptr(win_characteristic); 416 make_scoped_ptr(win_characteristic);
417 // Set default empty value.
418 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
419 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(new UCHAR[sizeof(ULONG)]);
scheib 2016/04/05 22:44:02 sizeof(BTH_LE_GATT_CHARACTERISTIC_VALUE)
gogerald1 2016/04/06 22:54:48 Done.
420 win_value->DataSize = 0;
421 win_characteristic->value.reset(win_value);
345 return win_characteristic; 422 return win_characteristic;
346 } 423 }
347 424
348 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteriscRemove( 425 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteriscRemove(
349 GattService* parent_service, 426 GattService* parent_service,
350 std::string attribute_handle) { 427 std::string attribute_handle) {
351 CHECK(parent_service); 428 CHECK(parent_service);
352 parent_service->included_characteristics.erase(attribute_handle); 429 parent_service->included_characteristics.erase(attribute_handle);
353 } 430 }
354 431
355 GattCharacteristic* 432 GattCharacteristic*
356 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( 433 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic(
357 GattService* parent_service, 434 GattService* parent_service,
358 std::string attribute_handle) { 435 std::string attribute_handle) {
359 CHECK(parent_service); 436 CHECK(parent_service);
360 GattCharacteristicsMap::iterator it = 437 GattCharacteristicsMap::iterator it =
361 parent_service->included_characteristics.find(attribute_handle); 438 parent_service->included_characteristics.find(attribute_handle);
362 if (it != parent_service->included_characteristics.end()) 439 if (it != parent_service->included_characteristics.end())
363 return it->second.get(); 440 return it->second.get();
364 return nullptr; 441 return nullptr;
365 } 442 }
366 443
367 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue( 444 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue(
368 GattCharacteristic* characteristic, 445 GattCharacteristic* characteristic,
369 const std::vector<uint8_t>& value) { 446 const std::vector<uint8_t>& value) {
370 CHECK(characteristic); 447 GattCharacteristic* target_characteristic = characteristic;
448 if (target_characteristic == nullptr)
449 target_characteristic = remembered_characteristic_.get();
450 CHECK(target_characteristic);
451
371 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 452 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
372 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 453 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
373 new UCHAR[value.size() + sizeof(ULONG)]); 454 new UCHAR[value.size() + sizeof(ULONG)]);
374 win_value->DataSize = (ULONG)value.size(); 455 win_value->DataSize = (ULONG)value.size();
375 for (std::size_t i = 0; i < value.size(); i++) 456 for (std::size_t i = 0; i < value.size(); i++)
376 win_value->Data[i] = value[i]; 457 win_value->Data[i] = value[i];
377 characteristic->value.reset(win_value); 458 target_characteristic->value.reset(win_value);
459 }
460
461 void BluetoothLowEnergyWrapperFake::
462 SimulateCharacteristicValueChangeNotification(
463 GattCharacteristic* characteristic) {
464 GattCharacteristic* target_characteristic = characteristic;
465 if (target_characteristic == nullptr)
466 target_characteristic = remembered_characteristic_.get();
467 CHECK(target_characteristic);
468 for (const auto& observer : target_characteristic->observers) {
469 GattCharacteristicObserverTable::const_iterator it =
470 gatt_characteristic_observers_.find(observer);
471 // Check if |observer| has been unregistered by UnregisterGattEvent.
472 if (it != gatt_characteristic_observers_.end()) {
473 BLUETOOTH_GATT_VALUE_CHANGED_EVENT event;
474 event.ChangedAttributeHandle =
475 target_characteristic->characteristic_info->AttributeHandle;
476 event.CharacteristicValueDataSize =
477 target_characteristic->value->DataSize + sizeof(ULONG);
478 event.CharacteristicValue = target_characteristic->value.get();
479 it->second->callback(CharacteristicValueChangedEvent, &event,
480 it->second->context);
481 }
482 }
378 } 483 }
379 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::RememberCharacteristicForSubsequentAction(
500 GattService* parent_service,
501 std::string attribute_handle) {
502 CHECK(parent_service);
503 GattCharacteristicsMap::iterator it =
504 parent_service->included_characteristics.find(attribute_handle);
505 DCHECK(it != parent_service->included_characteristics.end());
506 remembered_characteristic_ = std::move(it->second);
scheib 2016/04/05 22:44:02 Are you intending to move the characteristic out o
gogerald1 2016/04/06 22:54:48 Done.
507 }
508
394 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( 509 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor(
395 std::string device_address, 510 std::string device_address,
396 GattCharacteristic* characteristic, 511 GattCharacteristic* characteristic,
397 const BTH_LE_UUID& uuid) { 512 const BTH_LE_UUID& uuid) {
398 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor()); 513 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor());
399 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); 514 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]);
400 descriptor->descriptor_info->DescriptorUuid = uuid; 515 descriptor->descriptor_info->DescriptorUuid = uuid;
401 descriptor->descriptor_info->AttributeHandle = 516 descriptor->descriptor_info->AttributeHandle =
402 GenerateAUniqueAttributeHandle(device_address); 517 GenerateAUniqueAttributeHandle(device_address);
403 characteristic->included_descriptors[std::to_string( 518 characteristic->included_descriptors[std::to_string(
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( 623 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString(
509 const BLUETOOTH_ADDRESS& btha) { 624 const BLUETOOTH_ADDRESS& btha) {
510 std::string result = base::StringPrintf( 625 std::string result = base::StringPrintf(
511 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], 626 "%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]); 627 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]);
513 return result; 628 return result;
514 } 629 }
515 630
516 } // namespace win 631 } // namespace win
517 } // namespace device 632 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698