Chromium Code Reviews| 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 <winsock2.h> | 7 #include <winsock2.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 530 } | 530 } |
| 531 } | 531 } |
| 532 return true; | 532 return true; |
| 533 } | 533 } |
| 534 | 534 |
| 535 bool BluetoothTaskManagerWin::DiscoverClassicDeviceServices( | 535 bool BluetoothTaskManagerWin::DiscoverClassicDeviceServices( |
| 536 const std::string& device_address, | 536 const std::string& device_address, |
| 537 const GUID& protocol_uuid, | 537 const GUID& protocol_uuid, |
| 538 bool search_cached_services_only, | 538 bool search_cached_services_only, |
| 539 ScopedVector<ServiceRecordState>* service_record_states) { | 539 ScopedVector<ServiceRecordState>* service_record_states) { |
| 540 TryAgain: | |
| 540 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. | 541 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. |
| 541 WSAQUERYSET sdp_query; | 542 WSAQUERYSET sdp_query; |
| 542 ZeroMemory(&sdp_query, sizeof(sdp_query)); | 543 ZeroMemory(&sdp_query, sizeof(sdp_query)); |
| 543 sdp_query.dwSize = sizeof(sdp_query); | 544 sdp_query.dwSize = sizeof(sdp_query); |
| 544 GUID protocol = protocol_uuid; | 545 GUID protocol = protocol_uuid; |
| 545 sdp_query.lpServiceClassId = &protocol; | 546 sdp_query.lpServiceClassId = &protocol; |
| 546 sdp_query.dwNameSpace = NS_BTH; | 547 sdp_query.dwNameSpace = NS_BTH; |
| 547 wchar_t device_address_context[kMaxNumDeviceAddressChar]; | 548 wchar_t device_address_context[kMaxNumDeviceAddressChar]; |
| 548 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( | 549 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( |
| 549 device_address_context, kMaxNumDeviceAddressChar); | 550 device_address_context, kMaxNumDeviceAddressChar); |
| 550 device_address_context[length] = NULL; | 551 device_address_context[length] = NULL; |
| 551 sdp_query.lpszContext = device_address_context; | 552 sdp_query.lpszContext = device_address_context; |
| 552 DWORD control_flags = LUP_RETURN_ALL; | 553 DWORD control_flags = LUP_RETURN_ALL; |
| 553 // See http://goo.gl/t1Hulo: "Applications should generally specify | 554 // See http://goo.gl/t1Hulo: "Applications should generally specify |
| 554 // LUP_FLUSHCACHE. This flag instructs the system to ignore any cached | 555 // LUP_FLUSHCACHE. This flag instructs the system to ignore any cached |
| 555 // information and establish an over-the-air SDP connection to the specified | 556 // information and establish an over-the-air SDP connection to the specified |
| 556 // device to perform the SDP search. This non-cached operation may take | 557 // device to perform the SDP search. This non-cached operation may take |
| 557 // several seconds (whereas a cached search returns quickly)." | 558 // several seconds (whereas a cached search returns quickly)." |
| 558 // In summary, we need to specify LUP_FLUSHCACHE if we want to obtain the list | 559 // In summary, we need to specify LUP_FLUSHCACHE if we want to obtain the list |
| 559 // of services for devices which have not been discovered before. | 560 // of services for devices which have not been discovered before. |
| 560 if (!search_cached_services_only) | 561 if (!search_cached_services_only) |
| 561 control_flags |= LUP_FLUSHCACHE; | 562 control_flags |= LUP_FLUSHCACHE; |
| 562 HANDLE sdp_handle; | 563 HANDLE sdp_handle; |
| 563 if (ERROR_SUCCESS != | 564 if (ERROR_SUCCESS != |
| 564 WSALookupServiceBegin(&sdp_query, control_flags, &sdp_handle)) { | 565 WSALookupServiceBegin(&sdp_query, control_flags, &sdp_handle)) { |
| 565 LogPollingError("Error calling WSALookupServiceBegin", WSAGetLastError()); | 566 int last_error = WSAGetLastError(); |
| 567 // If the device is "offline", no services are returned when specifying | |
| 568 // "LUP_FLUSHCACHE". Try again without flushing the cache so that the list | |
| 569 // of previously known services is returned. | |
| 570 if (!search_cached_services_only && | |
| 571 (last_error == WSASERVICE_NOT_FOUND || last_error == WSANO_DATA)) { | |
| 572 search_cached_services_only = true; | |
| 573 goto TryAgain; | |
|
xiyuan
2014/08/14 19:35:29
Can we get rid of the "goto"?
rpaquay
2014/08/15 19:55:33
Done.
| |
| 574 } | |
| 575 LogPollingError("Error calling WSALookupServiceBegin", last_error); | |
| 566 return false; | 576 return false; |
| 567 } | 577 } |
| 568 char sdp_buffer[kServiceDiscoveryResultBufferSize]; | 578 char sdp_buffer[kServiceDiscoveryResultBufferSize]; |
| 569 LPWSAQUERYSET sdp_result_data = reinterpret_cast<LPWSAQUERYSET>(sdp_buffer); | 579 LPWSAQUERYSET sdp_result_data = reinterpret_cast<LPWSAQUERYSET>(sdp_buffer); |
| 570 while (true) { | 580 while (true) { |
| 571 DWORD sdp_buffer_size = sizeof(sdp_buffer); | 581 DWORD sdp_buffer_size = sizeof(sdp_buffer); |
| 572 if (ERROR_SUCCESS != | 582 if (ERROR_SUCCESS != |
| 573 WSALookupServiceNext( | 583 WSALookupServiceNext( |
| 574 sdp_handle, control_flags, &sdp_buffer_size, sdp_result_data)) { | 584 sdp_handle, control_flags, &sdp_buffer_size, sdp_result_data)) { |
| 575 int last_error = WSAGetLastError(); | 585 int last_error = WSAGetLastError(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 618 ++iter2) { | 628 ++iter2) { |
| 619 ServiceRecordState* service_state = new ServiceRecordState(); | 629 ServiceRecordState* service_state = new ServiceRecordState(); |
| 620 service_state->gatt_uuid = | 630 service_state->gatt_uuid = |
| 621 BluetoothLowEnergyUuidToBluetoothUuid((*iter2)->uuid); | 631 BluetoothLowEnergyUuidToBluetoothUuid((*iter2)->uuid); |
| 622 service_record_states->push_back(service_state); | 632 service_record_states->push_back(service_state); |
| 623 } | 633 } |
| 624 return true; | 634 return true; |
| 625 } | 635 } |
| 626 | 636 |
| 627 } // namespace device | 637 } // namespace device |
| OLD | NEW |