OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_task_manager_win.h" | 5 #include "device/bluetooth/bluetooth_task_manager_win.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 observer.DiscoveryStarted(success); | 371 observer.DiscoveryStarted(success); |
372 } | 372 } |
373 | 373 |
374 void BluetoothTaskManagerWin::OnDiscoveryStopped() { | 374 void BluetoothTaskManagerWin::OnDiscoveryStopped() { |
375 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 375 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
376 for (auto& observer : observers_) | 376 for (auto& observer : observers_) |
377 observer.DiscoveryStopped(); | 377 observer.DiscoveryStopped(); |
378 } | 378 } |
379 | 379 |
380 void BluetoothTaskManagerWin::OnDevicesPolled( | 380 void BluetoothTaskManagerWin::OnDevicesPolled( |
381 const ScopedVector<DeviceState>* devices) { | 381 const std::vector<std::unique_ptr<DeviceState>>* devices) { |
382 DCHECK(devices); | |
382 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 383 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
383 for (auto& observer : observers_) | 384 for (auto& observer : observers_) |
384 observer.DevicesPolled(*devices); | 385 observer.DevicesPolled(*devices); |
385 } | 386 } |
386 | 387 |
387 void BluetoothTaskManagerWin::PollAdapter() { | 388 void BluetoothTaskManagerWin::PollAdapter() { |
388 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 389 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
389 | 390 |
390 // Skips updating the adapter info if the adapter is in discovery mode. | 391 // Skips updating the adapter info if the adapter is in discovery mode. |
391 if (!discovering_) { | 392 if (!discovering_) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 | 470 |
470 void BluetoothTaskManagerWin::DiscoverDevices(int timeout_multiplier) { | 471 void BluetoothTaskManagerWin::DiscoverDevices(int timeout_multiplier) { |
471 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 472 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
472 if (!discovering_ || !adapter_handle_) { | 473 if (!discovering_ || !adapter_handle_) { |
473 ui_task_runner_->PostTask( | 474 ui_task_runner_->PostTask( |
474 FROM_HERE, | 475 FROM_HERE, |
475 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStopped, this)); | 476 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStopped, this)); |
476 return; | 477 return; |
477 } | 478 } |
478 | 479 |
479 std::unique_ptr<ScopedVector<DeviceState>> device_list( | 480 auto device_list(new std::vector<std::unique_ptr<DeviceState>>()); |
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
auto device_list = base::MakeUnique<std::vector<st
dougt
2016/12/22 01:18:03
Done.
| |
480 new ScopedVector<DeviceState>()); | 481 |
481 if (SearchDevices(timeout_multiplier, false, device_list.get())) { | 482 if (SearchDevices(timeout_multiplier, false, device_list)) { |
482 ui_task_runner_->PostTask( | 483 ui_task_runner_->PostTask( |
483 FROM_HERE, | 484 FROM_HERE, base::Bind(&BluetoothTaskManagerWin::OnDevicesPolled, this, |
484 base::Bind(&BluetoothTaskManagerWin::OnDevicesPolled, | 485 base::Passed(&device_list))); |
485 this, | |
486 base::Owned(device_list.release()))); | |
487 } | 486 } |
488 | 487 |
489 if (timeout_multiplier < kMaxDeviceDiscoveryTimeoutMultiplier) | 488 if (timeout_multiplier < kMaxDeviceDiscoveryTimeoutMultiplier) |
490 ++timeout_multiplier; | 489 ++timeout_multiplier; |
491 bluetooth_task_runner_->PostTask( | 490 bluetooth_task_runner_->PostTask( |
492 FROM_HERE, | 491 FROM_HERE, |
493 base::Bind( | 492 base::Bind( |
494 &BluetoothTaskManagerWin::DiscoverDevices, this, timeout_multiplier)); | 493 &BluetoothTaskManagerWin::DiscoverDevices, this, timeout_multiplier)); |
495 } | 494 } |
496 | 495 |
497 void BluetoothTaskManagerWin::GetKnownDevices() { | 496 void BluetoothTaskManagerWin::GetKnownDevices() { |
498 std::unique_ptr<ScopedVector<DeviceState>> device_list( | 497 auto device_list(new std::vector<std::unique_ptr<DeviceState>>()); |
499 new ScopedVector<DeviceState>()); | 498 if (SearchDevices(1, true, device_list)) { |
500 if (SearchDevices(1, true, device_list.get())) { | |
501 ui_task_runner_->PostTask( | 499 ui_task_runner_->PostTask( |
502 FROM_HERE, | 500 FROM_HERE, base::Bind(&BluetoothTaskManagerWin::OnDevicesPolled, this, |
503 base::Bind(&BluetoothTaskManagerWin::OnDevicesPolled, | 501 base::Passed(&device_list))); |
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
std::vector<std::unique_ptr<DeviceState>> device_l
dougt
2016/12/22 01:18:03
Done.
| |
504 this, | |
505 base::Owned(device_list.release()))); | |
506 } | 502 } |
507 } | 503 } |
508 | 504 |
509 bool BluetoothTaskManagerWin::SearchDevices( | 505 bool BluetoothTaskManagerWin::SearchDevices( |
510 int timeout_multiplier, | 506 int timeout_multiplier, |
511 bool search_cached_devices_only, | 507 bool search_cached_devices_only, |
512 ScopedVector<DeviceState>* device_list) { | 508 std::vector<std::unique_ptr<DeviceState>>* device_list) { |
513 return SearchClassicDevices( | 509 return SearchClassicDevices( |
514 timeout_multiplier, search_cached_devices_only, device_list) && | 510 timeout_multiplier, search_cached_devices_only, device_list) && |
515 SearchLowEnergyDevices(device_list) && | 511 SearchLowEnergyDevices(device_list) && |
516 DiscoverServices(device_list, search_cached_devices_only); | 512 DiscoverServices(device_list, search_cached_devices_only); |
517 } | 513 } |
518 | 514 |
519 bool BluetoothTaskManagerWin::SearchClassicDevices( | 515 bool BluetoothTaskManagerWin::SearchClassicDevices( |
520 int timeout_multiplier, | 516 int timeout_multiplier, |
521 bool search_cached_devices_only, | 517 bool search_cached_devices_only, |
522 ScopedVector<DeviceState>* device_list) { | 518 std::vector<std::unique_ptr<DeviceState>>* device_list) { |
523 // Issues a device inquiry and waits for |timeout_multiplier| * 1.28 seconds. | 519 // Issues a device inquiry and waits for |timeout_multiplier| * 1.28 seconds. |
524 BLUETOOTH_DEVICE_SEARCH_PARAMS device_search_params; | 520 BLUETOOTH_DEVICE_SEARCH_PARAMS device_search_params; |
525 ZeroMemory(&device_search_params, sizeof(device_search_params)); | 521 ZeroMemory(&device_search_params, sizeof(device_search_params)); |
526 device_search_params.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS); | 522 device_search_params.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS); |
527 device_search_params.fReturnAuthenticated = 1; | 523 device_search_params.fReturnAuthenticated = 1; |
528 device_search_params.fReturnRemembered = 1; | 524 device_search_params.fReturnRemembered = 1; |
529 device_search_params.fReturnUnknown = (search_cached_devices_only ? 0 : 1); | 525 device_search_params.fReturnUnknown = (search_cached_devices_only ? 0 : 1); |
530 device_search_params.fReturnConnected = 1; | 526 device_search_params.fReturnConnected = 1; |
531 device_search_params.fIssueInquiry = (search_cached_devices_only ? 0 : 1); | 527 device_search_params.fIssueInquiry = (search_cached_devices_only ? 0 : 1); |
532 device_search_params.cTimeoutMultiplier = timeout_multiplier; | 528 device_search_params.cTimeoutMultiplier = timeout_multiplier; |
533 | 529 |
534 BLUETOOTH_DEVICE_INFO device_info; | 530 BLUETOOTH_DEVICE_INFO device_info; |
535 ZeroMemory(&device_info, sizeof(device_info)); | 531 ZeroMemory(&device_info, sizeof(device_info)); |
536 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); | 532 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); |
537 HBLUETOOTH_DEVICE_FIND handle = | 533 HBLUETOOTH_DEVICE_FIND handle = |
538 win::BluetoothClassicWrapper::GetInstance()->FindFirstDevice( | 534 win::BluetoothClassicWrapper::GetInstance()->FindFirstDevice( |
539 &device_search_params, &device_info); | 535 &device_search_params, &device_info); |
540 if (!handle) { | 536 if (!handle) { |
541 int last_error = win::BluetoothClassicWrapper::GetInstance()->LastError(); | 537 int last_error = win::BluetoothClassicWrapper::GetInstance()->LastError(); |
542 if (last_error == ERROR_NO_MORE_ITEMS) { | 538 if (last_error == ERROR_NO_MORE_ITEMS) { |
543 return true; // No devices is not an error. | 539 return true; // No devices is not an error. |
544 } | 540 } |
545 LogPollingError("Error calling BluetoothFindFirstDevice", last_error); | 541 LogPollingError("Error calling BluetoothFindFirstDevice", last_error); |
546 return false; | 542 return false; |
547 } | 543 } |
548 | 544 |
549 while (true) { | 545 while (true) { |
550 DeviceState* device_state = new DeviceState(); | 546 std::unique_ptr<DeviceState> device_state(new DeviceState()); |
551 GetDeviceState(device_info, device_state); | 547 GetDeviceState(device_info, device_state.get()); |
552 device_list->push_back(device_state); | 548 device_list->push_back(std::move(device_state)); |
553 | 549 |
554 // Reset device info before next call (as a safety precaution). | 550 // Reset device info before next call (as a safety precaution). |
555 ZeroMemory(&device_info, sizeof(device_info)); | 551 ZeroMemory(&device_info, sizeof(device_info)); |
556 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); | 552 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); |
557 if (!win::BluetoothClassicWrapper::GetInstance()->FindNextDevice( | 553 if (!win::BluetoothClassicWrapper::GetInstance()->FindNextDevice( |
558 handle, &device_info)) { | 554 handle, &device_info)) { |
559 int last_error = win::BluetoothClassicWrapper::GetInstance()->LastError(); | 555 int last_error = win::BluetoothClassicWrapper::GetInstance()->LastError(); |
560 if (last_error == ERROR_NO_MORE_ITEMS) { | 556 if (last_error == ERROR_NO_MORE_ITEMS) { |
561 break; // No more items is expected error when done enumerating. | 557 break; // No more items is expected error when done enumerating. |
562 } | 558 } |
563 LogPollingError("Error calling BluetoothFindNextDevice", last_error); | 559 LogPollingError("Error calling BluetoothFindNextDevice", last_error); |
564 win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle); | 560 win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle); |
565 return false; | 561 return false; |
566 } | 562 } |
567 } | 563 } |
568 | 564 |
569 if (!win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle)) { | 565 if (!win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle)) { |
570 LogPollingError("Error calling BluetoothFindDeviceClose", | 566 LogPollingError("Error calling BluetoothFindDeviceClose", |
571 win::BluetoothClassicWrapper::GetInstance()->LastError()); | 567 win::BluetoothClassicWrapper::GetInstance()->LastError()); |
572 return false; | 568 return false; |
573 } | 569 } |
574 return true; | 570 return true; |
575 } | 571 } |
576 | 572 |
577 bool BluetoothTaskManagerWin::SearchLowEnergyDevices( | 573 bool BluetoothTaskManagerWin::SearchLowEnergyDevices( |
578 ScopedVector<DeviceState>* device_list) { | 574 std::vector<std::unique_ptr<DeviceState>>* device_list) { |
579 if (!win::BluetoothLowEnergyWrapper::GetInstance() | 575 if (!win::BluetoothLowEnergyWrapper::GetInstance() |
580 ->IsBluetoothLowEnergySupported()) { | 576 ->IsBluetoothLowEnergySupported()) { |
581 return true; // Bluetooth LE not supported is not an error. | 577 return true; // Bluetooth LE not supported is not an error. |
582 } | 578 } |
583 | 579 |
584 ScopedVector<win::BluetoothLowEnergyDeviceInfo> btle_devices; | 580 std::vector<std::unique_ptr<win::BluetoothLowEnergyDeviceInfo>> btle_devices; |
585 std::string error; | 581 std::string error; |
586 bool success = | 582 bool success = |
587 win::BluetoothLowEnergyWrapper::GetInstance() | 583 win::BluetoothLowEnergyWrapper::GetInstance() |
588 ->EnumerateKnownBluetoothLowEnergyDevices(&btle_devices, &error); | 584 ->EnumerateKnownBluetoothLowEnergyDevices(&btle_devices, &error); |
589 if (!success) { | 585 if (!success) { |
590 LogPollingError(error.c_str(), 0); | 586 LogPollingError(error.c_str(), 0); |
591 return false; | 587 return false; |
592 } | 588 } |
593 | 589 |
594 for (ScopedVector<win::BluetoothLowEnergyDeviceInfo>::iterator iter = | 590 for (auto& device_info : btle_devices) { |
595 btle_devices.begin(); | 591 std::unique_ptr<DeviceState> device_state(new DeviceState()); |
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
auto device_state = base::MakeUnique<DeviceState>(
dougt
2016/12/22 01:18:03
Done.
| |
596 iter != btle_devices.end(); | |
597 ++iter) { | |
598 win::BluetoothLowEnergyDeviceInfo* device_info = (*iter); | |
599 DeviceState* device_state = new DeviceState(); | |
600 device_state->name = device_info->friendly_name; | 592 device_state->name = device_info->friendly_name; |
601 device_state->address = | 593 device_state->address = |
602 BluetoothAddressToCanonicalString(device_info->address); | 594 BluetoothAddressToCanonicalString(device_info->address); |
603 device_state->visible = device_info->visible; | 595 device_state->visible = device_info->visible; |
604 device_state->authenticated = device_info->authenticated; | 596 device_state->authenticated = device_info->authenticated; |
605 device_state->connected = device_info->connected; | 597 device_state->connected = device_info->connected; |
606 device_state->path = device_info->path; | 598 device_state->path = device_info->path; |
607 device_list->push_back(device_state); | 599 device_list->push_back(std::move(device_state)); |
608 } | 600 } |
609 return true; | 601 return true; |
610 } | 602 } |
611 | 603 |
612 bool BluetoothTaskManagerWin::DiscoverServices( | 604 bool BluetoothTaskManagerWin::DiscoverServices( |
613 ScopedVector<DeviceState>* device_list, | 605 std::vector<std::unique_ptr<DeviceState>>* device_list, |
614 bool search_cached_services_only) { | 606 bool search_cached_services_only) { |
615 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 607 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
616 net::EnsureWinsockInit(); | 608 net::EnsureWinsockInit(); |
617 for (ScopedVector<DeviceState>::iterator iter = device_list->begin(); | 609 for (auto& device : *device_list) { |
618 iter != device_list->end(); | 610 auto& state = device->service_record_states; |
619 ++iter) { | |
620 DeviceState* device = (*iter); | |
621 ScopedVector<ServiceRecordState>* service_record_states = | |
622 &(*iter)->service_record_states; | |
623 | 611 |
624 if ((*iter)->is_bluetooth_classic()) { | 612 if (device->is_bluetooth_classic()) { |
625 if (!DiscoverClassicDeviceServices(device->address, | 613 if (!DiscoverClassicDeviceServices(device->address, L2CAP_PROTOCOL_UUID, |
626 L2CAP_PROTOCOL_UUID, | 614 search_cached_services_only, &state)) { |
627 search_cached_services_only, | |
628 service_record_states)) { | |
629 return false; | 615 return false; |
630 } | 616 } |
631 } else { | 617 } else { |
632 if (!DiscoverLowEnergyDeviceServices(device->path, | 618 if (!DiscoverLowEnergyDeviceServices(device->path, &state)) { |
633 service_record_states)) { | |
634 return false; | 619 return false; |
635 } | 620 } |
636 if (!SearchForGattServiceDevicePaths(device->address, | 621 if (!SearchForGattServiceDevicePaths(device->address, &state)) { |
637 service_record_states)) { | |
638 return false; | 622 return false; |
639 } | 623 } |
640 } | 624 } |
641 } | 625 } |
642 return true; | 626 return true; |
643 } | 627 } |
644 | 628 |
645 bool BluetoothTaskManagerWin::DiscoverClassicDeviceServices( | 629 bool BluetoothTaskManagerWin::DiscoverClassicDeviceServices( |
646 const std::string& device_address, | 630 const std::string& device_address, |
647 const GUID& protocol_uuid, | 631 const GUID& protocol_uuid, |
648 bool search_cached_services_only, | 632 bool search_cached_services_only, |
649 ScopedVector<ServiceRecordState>* service_record_states) { | 633 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
650 int error_code = | 634 int error_code = |
651 DiscoverClassicDeviceServicesWorker(device_address, | 635 DiscoverClassicDeviceServicesWorker(device_address, |
652 protocol_uuid, | 636 protocol_uuid, |
653 search_cached_services_only, | 637 search_cached_services_only, |
654 service_record_states); | 638 service_record_states); |
655 // If the device is "offline", no services are returned when specifying | 639 // If the device is "offline", no services are returned when specifying |
656 // "LUP_FLUSHCACHE". Try again without flushing the cache so that the list | 640 // "LUP_FLUSHCACHE". Try again without flushing the cache so that the list |
657 // of previously known services is returned. | 641 // of previously known services is returned. |
658 if (!search_cached_services_only && | 642 if (!search_cached_services_only && |
659 (error_code == WSASERVICE_NOT_FOUND || error_code == WSANO_DATA)) { | 643 (error_code == WSASERVICE_NOT_FOUND || error_code == WSANO_DATA)) { |
660 error_code = DiscoverClassicDeviceServicesWorker( | 644 error_code = DiscoverClassicDeviceServicesWorker( |
661 device_address, protocol_uuid, true, service_record_states); | 645 device_address, protocol_uuid, true, service_record_states); |
662 } | 646 } |
663 | 647 |
664 return (error_code == ERROR_SUCCESS); | 648 return (error_code == ERROR_SUCCESS); |
665 } | 649 } |
666 | 650 |
667 int BluetoothTaskManagerWin::DiscoverClassicDeviceServicesWorker( | 651 int BluetoothTaskManagerWin::DiscoverClassicDeviceServicesWorker( |
668 const std::string& device_address, | 652 const std::string& device_address, |
669 const GUID& protocol_uuid, | 653 const GUID& protocol_uuid, |
670 bool search_cached_services_only, | 654 bool search_cached_services_only, |
671 ScopedVector<ServiceRecordState>* service_record_states) { | 655 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
672 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. | 656 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. |
673 WSAQUERYSET sdp_query; | 657 WSAQUERYSET sdp_query; |
674 ZeroMemory(&sdp_query, sizeof(sdp_query)); | 658 ZeroMemory(&sdp_query, sizeof(sdp_query)); |
675 sdp_query.dwSize = sizeof(sdp_query); | 659 sdp_query.dwSize = sizeof(sdp_query); |
676 GUID protocol = protocol_uuid; | 660 GUID protocol = protocol_uuid; |
677 sdp_query.lpServiceClassId = &protocol; | 661 sdp_query.lpServiceClassId = &protocol; |
678 sdp_query.dwNameSpace = NS_BTH; | 662 sdp_query.dwNameSpace = NS_BTH; |
679 wchar_t device_address_context[kMaxNumDeviceAddressChar]; | 663 wchar_t device_address_context[kMaxNumDeviceAddressChar]; |
680 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( | 664 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( |
681 device_address_context, kMaxNumDeviceAddressChar); | 665 device_address_context, kMaxNumDeviceAddressChar); |
(...skipping 30 matching lines...) Expand all Loading... | |
712 WSALookupServiceNext( | 696 WSALookupServiceNext( |
713 sdp_handle, control_flags, &sdp_buffer_size, sdp_result_data)) { | 697 sdp_handle, control_flags, &sdp_buffer_size, sdp_result_data)) { |
714 int last_error = WSAGetLastError(); | 698 int last_error = WSAGetLastError(); |
715 if (last_error == WSA_E_NO_MORE || last_error == WSAENOMORE) { | 699 if (last_error == WSA_E_NO_MORE || last_error == WSAENOMORE) { |
716 break; | 700 break; |
717 } | 701 } |
718 LogPollingError("Error calling WSALookupServiceNext", last_error); | 702 LogPollingError("Error calling WSALookupServiceNext", last_error); |
719 WSALookupServiceEnd(sdp_handle); | 703 WSALookupServiceEnd(sdp_handle); |
720 return last_error; | 704 return last_error; |
721 } | 705 } |
722 ServiceRecordState* service_record_state = new ServiceRecordState(); | 706 std::unique_ptr<ServiceRecordState> service_record_state( |
707 new ServiceRecordState()); | |
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
base::MakeUnique.
dougt
2016/12/22 01:18:03
Done.
| |
723 service_record_state->name = | 708 service_record_state->name = |
724 base::SysWideToUTF8(sdp_result_data->lpszServiceInstanceName); | 709 base::SysWideToUTF8(sdp_result_data->lpszServiceInstanceName); |
725 for (uint64_t i = 0; i < sdp_result_data->lpBlob->cbSize; i++) { | 710 for (uint64_t i = 0; i < sdp_result_data->lpBlob->cbSize; i++) { |
726 service_record_state->sdp_bytes.push_back( | 711 service_record_state->sdp_bytes.push_back( |
727 sdp_result_data->lpBlob->pBlobData[i]); | 712 sdp_result_data->lpBlob->pBlobData[i]); |
728 } | 713 } |
729 service_record_states->push_back(service_record_state); | 714 service_record_states->push_back(std::move(service_record_state)); |
730 } | 715 } |
731 if (ERROR_SUCCESS != WSALookupServiceEnd(sdp_handle)) { | 716 if (ERROR_SUCCESS != WSALookupServiceEnd(sdp_handle)) { |
732 int last_error = WSAGetLastError(); | 717 int last_error = WSAGetLastError(); |
733 LogPollingError("Error calling WSALookupServiceEnd", last_error); | 718 LogPollingError("Error calling WSALookupServiceEnd", last_error); |
734 return last_error; | 719 return last_error; |
735 } | 720 } |
736 | 721 |
737 return ERROR_SUCCESS; | 722 return ERROR_SUCCESS; |
738 } | 723 } |
739 | 724 |
740 bool BluetoothTaskManagerWin::DiscoverLowEnergyDeviceServices( | 725 bool BluetoothTaskManagerWin::DiscoverLowEnergyDeviceServices( |
741 const base::FilePath& device_path, | 726 const base::FilePath& device_path, |
742 ScopedVector<ServiceRecordState>* service_record_states) { | 727 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
743 if (!win::BluetoothLowEnergyWrapper::GetInstance() | 728 if (!win::BluetoothLowEnergyWrapper::GetInstance() |
744 ->IsBluetoothLowEnergySupported()) { | 729 ->IsBluetoothLowEnergySupported()) { |
745 return true; // Bluetooth LE not supported is not an error. | 730 return true; // Bluetooth LE not supported is not an error. |
746 } | 731 } |
747 | 732 |
748 std::string error; | 733 std::string error; |
749 ScopedVector<win::BluetoothLowEnergyServiceInfo> services; | 734 std::vector<std::unique_ptr<win::BluetoothLowEnergyServiceInfo>> services; |
750 bool success = win::BluetoothLowEnergyWrapper::GetInstance() | 735 bool success = win::BluetoothLowEnergyWrapper::GetInstance() |
751 ->EnumerateKnownBluetoothLowEnergyServices( | 736 ->EnumerateKnownBluetoothLowEnergyServices( |
752 device_path, &services, &error); | 737 device_path, &services, &error); |
753 if (!success) { | 738 if (!success) { |
754 LogPollingError(error.c_str(), 0); | 739 LogPollingError(error.c_str(), 0); |
755 return false; | 740 return false; |
756 } | 741 } |
757 | 742 |
758 for (ScopedVector<win::BluetoothLowEnergyServiceInfo>::iterator iter2 = | 743 for (auto& service : services) { |
759 services.begin(); | 744 std::unique_ptr<ServiceRecordState> service_state(new ServiceRecordState()); |
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
base::MakeUnique
dougt
2016/12/22 01:18:03
Done.
| |
760 iter2 != services.end(); | |
761 ++iter2) { | |
762 ServiceRecordState* service_state = new ServiceRecordState(); | |
763 service_state->gatt_uuid = | 745 service_state->gatt_uuid = |
764 BluetoothLowEnergyUuidToBluetoothUuid((*iter2)->uuid); | 746 BluetoothLowEnergyUuidToBluetoothUuid(service->uuid); |
765 service_state->attribute_handle = (*iter2)->attribute_handle; | 747 service_state->attribute_handle = service->attribute_handle; |
766 service_record_states->push_back(service_state); | 748 service_record_states->push_back(std::move(service_state)); |
767 } | 749 } |
768 return true; | 750 return true; |
769 } | 751 } |
770 | 752 |
771 // Each GATT service of a BLE device will be listed on the machine as a BLE | 753 // Each GATT service of a BLE device will be listed on the machine as a BLE |
772 // device interface with a matching service attribute handle. This interface | 754 // device interface with a matching service attribute handle. This interface |
773 // lists all GATT service devices and matches them back to correspond GATT | 755 // lists all GATT service devices and matches them back to correspond GATT |
774 // service of the BLE device according to their address and included service | 756 // service of the BLE device according to their address and included service |
775 // attribute handles, as we did not find a more neat way to bond them. | 757 // attribute handles, as we did not find a more neat way to bond them. |
776 bool BluetoothTaskManagerWin::SearchForGattServiceDevicePaths( | 758 bool BluetoothTaskManagerWin::SearchForGattServiceDevicePaths( |
777 const std::string device_address, | 759 const std::string device_address, |
778 ScopedVector<ServiceRecordState>* service_record_states) { | 760 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
779 std::string error; | 761 std::string error; |
780 | 762 |
781 // List all known GATT service devices on the machine. | 763 // List all known GATT service devices on the machine. |
782 ScopedVector<win::BluetoothLowEnergyDeviceInfo> gatt_service_devices; | 764 std::vector<std::unique_ptr<win::BluetoothLowEnergyDeviceInfo>> |
765 gatt_service_devices; | |
783 bool success = win::BluetoothLowEnergyWrapper::GetInstance() | 766 bool success = win::BluetoothLowEnergyWrapper::GetInstance() |
784 ->EnumerateKnownBluetoothLowEnergyGattServiceDevices( | 767 ->EnumerateKnownBluetoothLowEnergyGattServiceDevices( |
785 &gatt_service_devices, &error); | 768 &gatt_service_devices, &error); |
786 if (!success) { | 769 if (!success) { |
787 LogPollingError(error.c_str(), 0); | 770 LogPollingError(error.c_str(), 0); |
788 return false; | 771 return false; |
789 } | 772 } |
790 | 773 |
791 for (auto* gatt_service_device : gatt_service_devices) { | 774 for (auto& gatt_service_device : gatt_service_devices) { |
792 // Only care about the service devices with |device_address|. | 775 // Only care about the service devices with |device_address|. |
793 if (BluetoothAddressToCanonicalString(gatt_service_device->address) != | 776 if (BluetoothAddressToCanonicalString(gatt_service_device->address) != |
794 device_address) { | 777 device_address) { |
795 continue; | 778 continue; |
796 } | 779 } |
797 | 780 |
798 // Discover this service device's contained services. | 781 // Discover this service device's contained services. |
799 ScopedVector<win::BluetoothLowEnergyServiceInfo> gatt_services; | 782 std::vector<std::unique_ptr<win::BluetoothLowEnergyServiceInfo>> |
783 gatt_services; | |
800 if (!win::BluetoothLowEnergyWrapper::GetInstance() | 784 if (!win::BluetoothLowEnergyWrapper::GetInstance() |
801 ->EnumerateKnownBluetoothLowEnergyServices( | 785 ->EnumerateKnownBluetoothLowEnergyServices( |
802 gatt_service_device->path, &gatt_services, &error)) { | 786 gatt_service_device->path, &gatt_services, &error)) { |
803 LogPollingError(error.c_str(), 0); | 787 LogPollingError(error.c_str(), 0); |
804 continue; | 788 continue; |
805 } | 789 } |
806 | 790 |
807 // Usually each service device correspond to one Gatt service. | 791 // Usually each service device correspond to one Gatt service. |
808 if (gatt_services.size() > 1) { | 792 if (gatt_services.size() > 1) { |
809 LOG(WARNING) << "This GATT service device contains more than one (" | 793 LOG(WARNING) << "This GATT service device contains more than one (" |
810 << gatt_services.size() << ") services"; | 794 << gatt_services.size() << ") services"; |
811 } | 795 } |
812 | 796 |
813 // Associate service device to corresponding service record. Attribute | 797 // Associate service device to corresponding service record. Attribute |
814 // handle is unique on one device. | 798 // handle is unique on one device. |
815 for (auto* gatt_service : gatt_services) { | 799 for (auto& gatt_service : gatt_services) { |
816 for (auto* service_record_state : *service_record_states) { | 800 for (auto& service_record_state : *service_record_states) { |
817 if (service_record_state->attribute_handle == | 801 if (service_record_state->attribute_handle == |
818 gatt_service->attribute_handle) { | 802 gatt_service->attribute_handle) { |
819 service_record_state->path = gatt_service_device->path; | 803 service_record_state->path = gatt_service_device->path; |
820 break; | 804 break; |
821 } | 805 } |
822 } | 806 } |
823 } | 807 } |
824 } | 808 } |
825 | 809 |
826 return true; | 810 return true; |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1034 void BluetoothTaskManagerWin::PostUnregisterGattCharacteristicValueChangedEvent( | 1018 void BluetoothTaskManagerWin::PostUnregisterGattCharacteristicValueChangedEvent( |
1035 PVOID event_handle) { | 1019 PVOID event_handle) { |
1036 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 1020 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
1037 bluetooth_task_runner_->PostTask( | 1021 bluetooth_task_runner_->PostTask( |
1038 FROM_HERE, base::Bind(&BluetoothTaskManagerWin:: | 1022 FROM_HERE, base::Bind(&BluetoothTaskManagerWin:: |
1039 UnregisterGattCharacteristicValueChangedEvent, | 1023 UnregisterGattCharacteristicValueChangedEvent, |
1040 this, event_handle)); | 1024 this, event_handle)); |
1041 } | 1025 } |
1042 | 1026 |
1043 } // namespace device | 1027 } // namespace device |
OLD | NEW |