| 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 PVOID context) { | 160 PVOID context) { |
| 160 if (type != CharacteristicValueChangedEvent) { | 161 if (type != CharacteristicValueChangedEvent) { |
| 161 // Right now, only characteristic value changed event is supported. | 162 // Right now, only characteristic value changed event is supported. |
| 162 NOTREACHED(); | 163 NOTREACHED(); |
| 163 return; | 164 return; |
| 164 } | 165 } |
| 165 | 166 |
| 166 BLUETOOTH_GATT_VALUE_CHANGED_EVENT* event = | 167 BLUETOOTH_GATT_VALUE_CHANGED_EVENT* event = |
| 167 (BLUETOOTH_GATT_VALUE_CHANGED_EVENT*)event_parameter; | 168 (BLUETOOTH_GATT_VALUE_CHANGED_EVENT*)event_parameter; |
| 168 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value_win = event->CharacteristicValue; | 169 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value_win = event->CharacteristicValue; |
| 169 std::unique_ptr<std::vector<uint8_t>> new_value( | 170 auto new_value = |
| 170 new std::vector<uint8_t>(new_value_win->DataSize)); | 171 base::MakeUnique<std::vector<uint8_t>>(new_value_win->DataSize); |
| 171 for (ULONG i = 0; i < new_value_win->DataSize; i++) | 172 for (ULONG i = 0; i < new_value_win->DataSize; i++) |
| 172 (*new_value)[i] = new_value_win->Data[i]; | 173 (*new_value)[i] = new_value_win->Data[i]; |
| 173 | 174 |
| 174 base::AutoLock auto_lock(g_characteristic_value_changed_registrations_lock); | 175 base::AutoLock auto_lock(g_characteristic_value_changed_registrations_lock); |
| 175 CharacteristicValueChangedRegistrationMap::const_iterator it = | 176 CharacteristicValueChangedRegistrationMap::const_iterator it = |
| 176 g_characteristic_value_changed_registrations.find(context); | 177 g_characteristic_value_changed_registrations.find(context); |
| 177 if (it == g_characteristic_value_changed_registrations.end()) | 178 if (it == g_characteristic_value_changed_registrations.end()) |
| 178 return; | 179 return; |
| 179 | 180 |
| 180 it->second->callback_task_runner->PostTask( | 181 it->second->callback_task_runner->PostTask( |
| (...skipping 190 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 const 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 (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 (auto& device : *device_list) { |
| 618 iter != device_list->end(); | 609 auto& state = device->service_record_states; |
| 619 ++iter) { | |
| 620 DeviceState* device = (*iter); | |
| 621 ScopedVector<ServiceRecordState>* service_record_states = | |
| 622 &(*iter)->service_record_states; | |
| 623 | 610 |
| 624 if ((*iter)->is_bluetooth_classic()) { | 611 if (device->is_bluetooth_classic()) { |
| 625 if (!DiscoverClassicDeviceServices(device->address, | 612 if (!DiscoverClassicDeviceServices(device->address, L2CAP_PROTOCOL_UUID, |
| 626 L2CAP_PROTOCOL_UUID, | 613 search_cached_services_only, &state)) { |
| 627 search_cached_services_only, | |
| 628 service_record_states)) { | |
| 629 return false; | 614 return false; |
| 630 } | 615 } |
| 631 } else { | 616 } else { |
| 632 if (!DiscoverLowEnergyDeviceServices(device->path, | 617 if (!DiscoverLowEnergyDeviceServices(device->path, &state)) { |
| 633 service_record_states)) { | |
| 634 return false; | 618 return false; |
| 635 } | 619 } |
| 636 if (!SearchForGattServiceDevicePaths(device->address, | 620 if (!SearchForGattServiceDevicePaths(device->address, &state)) { |
| 637 service_record_states)) { | |
| 638 return false; | 621 return false; |
| 639 } | 622 } |
| 640 } | 623 } |
| 641 } | 624 } |
| 642 return true; | 625 return true; |
| 643 } | 626 } |
| 644 | 627 |
| 645 bool BluetoothTaskManagerWin::DiscoverClassicDeviceServices( | 628 bool BluetoothTaskManagerWin::DiscoverClassicDeviceServices( |
| 646 const std::string& device_address, | 629 const std::string& device_address, |
| 647 const GUID& protocol_uuid, | 630 const GUID& protocol_uuid, |
| 648 bool search_cached_services_only, | 631 bool search_cached_services_only, |
| 649 ScopedVector<ServiceRecordState>* service_record_states) { | 632 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
| 650 int error_code = | 633 int error_code = |
| 651 DiscoverClassicDeviceServicesWorker(device_address, | 634 DiscoverClassicDeviceServicesWorker(device_address, |
| 652 protocol_uuid, | 635 protocol_uuid, |
| 653 search_cached_services_only, | 636 search_cached_services_only, |
| 654 service_record_states); | 637 service_record_states); |
| 655 // If the device is "offline", no services are returned when specifying | 638 // If the device is "offline", no services are returned when specifying |
| 656 // "LUP_FLUSHCACHE". Try again without flushing the cache so that the list | 639 // "LUP_FLUSHCACHE". Try again without flushing the cache so that the list |
| 657 // of previously known services is returned. | 640 // of previously known services is returned. |
| 658 if (!search_cached_services_only && | 641 if (!search_cached_services_only && |
| 659 (error_code == WSASERVICE_NOT_FOUND || error_code == WSANO_DATA)) { | 642 (error_code == WSASERVICE_NOT_FOUND || error_code == WSANO_DATA)) { |
| 660 error_code = DiscoverClassicDeviceServicesWorker( | 643 error_code = DiscoverClassicDeviceServicesWorker( |
| 661 device_address, protocol_uuid, true, service_record_states); | 644 device_address, protocol_uuid, true, service_record_states); |
| 662 } | 645 } |
| 663 | 646 |
| 664 return (error_code == ERROR_SUCCESS); | 647 return (error_code == ERROR_SUCCESS); |
| 665 } | 648 } |
| 666 | 649 |
| 667 int BluetoothTaskManagerWin::DiscoverClassicDeviceServicesWorker( | 650 int BluetoothTaskManagerWin::DiscoverClassicDeviceServicesWorker( |
| 668 const std::string& device_address, | 651 const std::string& device_address, |
| 669 const GUID& protocol_uuid, | 652 const GUID& protocol_uuid, |
| 670 bool search_cached_services_only, | 653 bool search_cached_services_only, |
| 671 ScopedVector<ServiceRecordState>* service_record_states) { | 654 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
| 672 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. | 655 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. |
| 673 WSAQUERYSET sdp_query; | 656 WSAQUERYSET sdp_query; |
| 674 ZeroMemory(&sdp_query, sizeof(sdp_query)); | 657 ZeroMemory(&sdp_query, sizeof(sdp_query)); |
| 675 sdp_query.dwSize = sizeof(sdp_query); | 658 sdp_query.dwSize = sizeof(sdp_query); |
| 676 GUID protocol = protocol_uuid; | 659 GUID protocol = protocol_uuid; |
| 677 sdp_query.lpServiceClassId = &protocol; | 660 sdp_query.lpServiceClassId = &protocol; |
| 678 sdp_query.dwNameSpace = NS_BTH; | 661 sdp_query.dwNameSpace = NS_BTH; |
| 679 wchar_t device_address_context[kMaxNumDeviceAddressChar]; | 662 wchar_t device_address_context[kMaxNumDeviceAddressChar]; |
| 680 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( | 663 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( |
| 681 device_address_context, kMaxNumDeviceAddressChar); | 664 device_address_context, kMaxNumDeviceAddressChar); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 712 WSALookupServiceNext( | 695 WSALookupServiceNext( |
| 713 sdp_handle, control_flags, &sdp_buffer_size, sdp_result_data)) { | 696 sdp_handle, control_flags, &sdp_buffer_size, sdp_result_data)) { |
| 714 int last_error = WSAGetLastError(); | 697 int last_error = WSAGetLastError(); |
| 715 if (last_error == WSA_E_NO_MORE || last_error == WSAENOMORE) { | 698 if (last_error == WSA_E_NO_MORE || last_error == WSAENOMORE) { |
| 716 break; | 699 break; |
| 717 } | 700 } |
| 718 LogPollingError("Error calling WSALookupServiceNext", last_error); | 701 LogPollingError("Error calling WSALookupServiceNext", last_error); |
| 719 WSALookupServiceEnd(sdp_handle); | 702 WSALookupServiceEnd(sdp_handle); |
| 720 return last_error; | 703 return last_error; |
| 721 } | 704 } |
| 722 ServiceRecordState* service_record_state = new ServiceRecordState(); | 705 auto service_record_state = base::MakeUnique<ServiceRecordState>(); |
| 723 service_record_state->name = | 706 service_record_state->name = |
| 724 base::SysWideToUTF8(sdp_result_data->lpszServiceInstanceName); | 707 base::SysWideToUTF8(sdp_result_data->lpszServiceInstanceName); |
| 725 for (uint64_t i = 0; i < sdp_result_data->lpBlob->cbSize; i++) { | 708 for (uint64_t i = 0; i < sdp_result_data->lpBlob->cbSize; i++) { |
| 726 service_record_state->sdp_bytes.push_back( | 709 service_record_state->sdp_bytes.push_back( |
| 727 sdp_result_data->lpBlob->pBlobData[i]); | 710 sdp_result_data->lpBlob->pBlobData[i]); |
| 728 } | 711 } |
| 729 service_record_states->push_back(service_record_state); | 712 service_record_states->push_back(std::move(service_record_state)); |
| 730 } | 713 } |
| 731 if (ERROR_SUCCESS != WSALookupServiceEnd(sdp_handle)) { | 714 if (ERROR_SUCCESS != WSALookupServiceEnd(sdp_handle)) { |
| 732 int last_error = WSAGetLastError(); | 715 int last_error = WSAGetLastError(); |
| 733 LogPollingError("Error calling WSALookupServiceEnd", last_error); | 716 LogPollingError("Error calling WSALookupServiceEnd", last_error); |
| 734 return last_error; | 717 return last_error; |
| 735 } | 718 } |
| 736 | 719 |
| 737 return ERROR_SUCCESS; | 720 return ERROR_SUCCESS; |
| 738 } | 721 } |
| 739 | 722 |
| 740 bool BluetoothTaskManagerWin::DiscoverLowEnergyDeviceServices( | 723 bool BluetoothTaskManagerWin::DiscoverLowEnergyDeviceServices( |
| 741 const base::FilePath& device_path, | 724 const base::FilePath& device_path, |
| 742 ScopedVector<ServiceRecordState>* service_record_states) { | 725 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
| 743 if (!win::BluetoothLowEnergyWrapper::GetInstance() | 726 if (!win::BluetoothLowEnergyWrapper::GetInstance() |
| 744 ->IsBluetoothLowEnergySupported()) { | 727 ->IsBluetoothLowEnergySupported()) { |
| 745 return true; // Bluetooth LE not supported is not an error. | 728 return true; // Bluetooth LE not supported is not an error. |
| 746 } | 729 } |
| 747 | 730 |
| 748 std::string error; | 731 std::string error; |
| 749 ScopedVector<win::BluetoothLowEnergyServiceInfo> services; | 732 std::vector<std::unique_ptr<win::BluetoothLowEnergyServiceInfo>> services; |
| 750 bool success = win::BluetoothLowEnergyWrapper::GetInstance() | 733 bool success = win::BluetoothLowEnergyWrapper::GetInstance() |
| 751 ->EnumerateKnownBluetoothLowEnergyServices( | 734 ->EnumerateKnownBluetoothLowEnergyServices( |
| 752 device_path, &services, &error); | 735 device_path, &services, &error); |
| 753 if (!success) { | 736 if (!success) { |
| 754 LogPollingError(error.c_str(), 0); | 737 LogPollingError(error.c_str(), 0); |
| 755 return false; | 738 return false; |
| 756 } | 739 } |
| 757 | 740 |
| 758 for (ScopedVector<win::BluetoothLowEnergyServiceInfo>::iterator iter2 = | 741 for (auto& service : services) { |
| 759 services.begin(); | 742 auto service_state = base::MakeUnique<ServiceRecordState>(); |
| 760 iter2 != services.end(); | |
| 761 ++iter2) { | |
| 762 ServiceRecordState* service_state = new ServiceRecordState(); | |
| 763 service_state->gatt_uuid = | 743 service_state->gatt_uuid = |
| 764 BluetoothLowEnergyUuidToBluetoothUuid((*iter2)->uuid); | 744 BluetoothLowEnergyUuidToBluetoothUuid(service->uuid); |
| 765 service_state->attribute_handle = (*iter2)->attribute_handle; | 745 service_state->attribute_handle = service->attribute_handle; |
| 766 service_record_states->push_back(service_state); | 746 service_record_states->push_back(std::move(service_state)); |
| 767 } | 747 } |
| 768 return true; | 748 return true; |
| 769 } | 749 } |
| 770 | 750 |
| 771 // Each GATT service of a BLE device will be listed on the machine as a BLE | 751 // 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 | 752 // device interface with a matching service attribute handle. This interface |
| 773 // lists all GATT service devices and matches them back to correspond GATT | 753 // 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 | 754 // 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. | 755 // attribute handles, as we did not find a more neat way to bond them. |
| 776 bool BluetoothTaskManagerWin::SearchForGattServiceDevicePaths( | 756 bool BluetoothTaskManagerWin::SearchForGattServiceDevicePaths( |
| 777 const std::string device_address, | 757 const std::string device_address, |
| 778 ScopedVector<ServiceRecordState>* service_record_states) { | 758 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states) { |
| 779 std::string error; | 759 std::string error; |
| 780 | 760 |
| 781 // List all known GATT service devices on the machine. | 761 // List all known GATT service devices on the machine. |
| 782 ScopedVector<win::BluetoothLowEnergyDeviceInfo> gatt_service_devices; | 762 std::vector<std::unique_ptr<win::BluetoothLowEnergyDeviceInfo>> |
| 763 gatt_service_devices; |
| 783 bool success = win::BluetoothLowEnergyWrapper::GetInstance() | 764 bool success = win::BluetoothLowEnergyWrapper::GetInstance() |
| 784 ->EnumerateKnownBluetoothLowEnergyGattServiceDevices( | 765 ->EnumerateKnownBluetoothLowEnergyGattServiceDevices( |
| 785 &gatt_service_devices, &error); | 766 &gatt_service_devices, &error); |
| 786 if (!success) { | 767 if (!success) { |
| 787 LogPollingError(error.c_str(), 0); | 768 LogPollingError(error.c_str(), 0); |
| 788 return false; | 769 return false; |
| 789 } | 770 } |
| 790 | 771 |
| 791 for (auto* gatt_service_device : gatt_service_devices) { | 772 for (auto& gatt_service_device : gatt_service_devices) { |
| 792 // Only care about the service devices with |device_address|. | 773 // Only care about the service devices with |device_address|. |
| 793 if (BluetoothAddressToCanonicalString(gatt_service_device->address) != | 774 if (BluetoothAddressToCanonicalString(gatt_service_device->address) != |
| 794 device_address) { | 775 device_address) { |
| 795 continue; | 776 continue; |
| 796 } | 777 } |
| 797 | 778 |
| 798 // Discover this service device's contained services. | 779 // Discover this service device's contained services. |
| 799 ScopedVector<win::BluetoothLowEnergyServiceInfo> gatt_services; | 780 std::vector<std::unique_ptr<win::BluetoothLowEnergyServiceInfo>> |
| 781 gatt_services; |
| 800 if (!win::BluetoothLowEnergyWrapper::GetInstance() | 782 if (!win::BluetoothLowEnergyWrapper::GetInstance() |
| 801 ->EnumerateKnownBluetoothLowEnergyServices( | 783 ->EnumerateKnownBluetoothLowEnergyServices( |
| 802 gatt_service_device->path, &gatt_services, &error)) { | 784 gatt_service_device->path, &gatt_services, &error)) { |
| 803 LogPollingError(error.c_str(), 0); | 785 LogPollingError(error.c_str(), 0); |
| 804 continue; | 786 continue; |
| 805 } | 787 } |
| 806 | 788 |
| 807 // Usually each service device correspond to one Gatt service. | 789 // Usually each service device correspond to one Gatt service. |
| 808 if (gatt_services.size() > 1) { | 790 if (gatt_services.size() > 1) { |
| 809 LOG(WARNING) << "This GATT service device contains more than one (" | 791 LOG(WARNING) << "This GATT service device contains more than one (" |
| 810 << gatt_services.size() << ") services"; | 792 << gatt_services.size() << ") services"; |
| 811 } | 793 } |
| 812 | 794 |
| 813 // Associate service device to corresponding service record. Attribute | 795 // Associate service device to corresponding service record. Attribute |
| 814 // handle is unique on one device. | 796 // handle is unique on one device. |
| 815 for (auto* gatt_service : gatt_services) { | 797 for (auto& gatt_service : gatt_services) { |
| 816 for (auto* service_record_state : *service_record_states) { | 798 for (auto& service_record_state : *service_record_states) { |
| 817 if (service_record_state->attribute_handle == | 799 if (service_record_state->attribute_handle == |
| 818 gatt_service->attribute_handle) { | 800 gatt_service->attribute_handle) { |
| 819 service_record_state->path = gatt_service_device->path; | 801 service_record_state->path = gatt_service_device->path; |
| 820 break; | 802 break; |
| 821 } | 803 } |
| 822 } | 804 } |
| 823 } | 805 } |
| 824 } | 806 } |
| 825 | 807 |
| 826 return true; | 808 return true; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 935 new_cccd_value.ClientCharacteristicConfiguration.IsSubscribeToIndication = | 917 new_cccd_value.ClientCharacteristicConfiguration.IsSubscribeToIndication = |
| 936 TRUE; | 918 TRUE; |
| 937 } | 919 } |
| 938 | 920 |
| 939 hr = win::BluetoothLowEnergyWrapper::GetInstance()->WriteDescriptorValue( | 921 hr = win::BluetoothLowEnergyWrapper::GetInstance()->WriteDescriptorValue( |
| 940 service_path, (PBTH_LE_GATT_DESCRIPTOR)(&ccc_descriptor), | 922 service_path, (PBTH_LE_GATT_DESCRIPTOR)(&ccc_descriptor), |
| 941 &new_cccd_value); | 923 &new_cccd_value); |
| 942 } | 924 } |
| 943 | 925 |
| 944 if (SUCCEEDED(hr)) { | 926 if (SUCCEEDED(hr)) { |
| 945 std::unique_ptr<CharacteristicValueChangedRegistration> registration( | 927 auto registration = |
| 946 new CharacteristicValueChangedRegistration()); | 928 base::MakeUnique<CharacteristicValueChangedRegistration>(); |
| 929 |
| 947 registration->win_event_handle = win_event_handle; | 930 registration->win_event_handle = win_event_handle; |
| 948 registration->callback = registered_callback; | 931 registration->callback = registered_callback; |
| 949 registration->callback_task_runner = ui_task_runner_; | 932 registration->callback_task_runner = ui_task_runner_; |
| 950 base::AutoLock auto_lock(g_characteristic_value_changed_registrations_lock); | 933 base::AutoLock auto_lock(g_characteristic_value_changed_registrations_lock); |
| 951 g_characteristic_value_changed_registrations[user_event_handle] = | 934 g_characteristic_value_changed_registrations[user_event_handle] = |
| 952 std::move(registration); | 935 std::move(registration); |
| 953 } | 936 } |
| 954 | 937 |
| 955 ui_task_runner_->PostTask(FROM_HERE, | 938 ui_task_runner_->PostTask(FROM_HERE, |
| 956 base::Bind(callback, user_event_handle, hr)); | 939 base::Bind(callback, user_event_handle, hr)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1034 void BluetoothTaskManagerWin::PostUnregisterGattCharacteristicValueChangedEvent( | 1017 void BluetoothTaskManagerWin::PostUnregisterGattCharacteristicValueChangedEvent( |
| 1035 PVOID event_handle) { | 1018 PVOID event_handle) { |
| 1036 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 1019 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 1037 bluetooth_task_runner_->PostTask( | 1020 bluetooth_task_runner_->PostTask( |
| 1038 FROM_HERE, base::Bind(&BluetoothTaskManagerWin:: | 1021 FROM_HERE, base::Bind(&BluetoothTaskManagerWin:: |
| 1039 UnregisterGattCharacteristicValueChangedEvent, | 1022 UnregisterGattCharacteristicValueChangedEvent, |
| 1040 this, event_handle)); | 1023 this, event_handle)); |
| 1041 } | 1024 } |
| 1042 | 1025 |
| 1043 } // namespace device | 1026 } // namespace device |
| OLD | NEW |