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