| 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 12 matching lines...) Expand all Loading... |
| 23 #include "net/base/winsock_init.h" | 23 #include "net/base/winsock_init.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const int kNumThreadsInWorkerPool = 3; | 27 const int kNumThreadsInWorkerPool = 3; |
| 28 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; | 28 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; |
| 29 const int kMaxNumDeviceAddressChar = 127; | 29 const int kMaxNumDeviceAddressChar = 127; |
| 30 const int kServiceDiscoveryResultBufferSize = 5000; | 30 const int kServiceDiscoveryResultBufferSize = 5000; |
| 31 const int kMaxDeviceDiscoveryTimeout = 48; | 31 const int kMaxDeviceDiscoveryTimeout = 48; |
| 32 | 32 |
| 33 typedef device::BluetoothTaskManagerWin::ServiceRecordState ServiceRecordState; | |
| 34 | |
| 35 // Populates bluetooth adapter state using adapter_handle. | 33 // Populates bluetooth adapter state using adapter_handle. |
| 36 void GetAdapterState(HANDLE adapter_handle, | 34 void GetAdapterState(HANDLE adapter_handle, |
| 37 device::BluetoothTaskManagerWin::AdapterState* state) { | 35 device::BluetoothTaskManagerWin::AdapterState* state) { |
| 38 std::string name; | 36 std::string name; |
| 39 std::string address; | 37 std::string address; |
| 40 bool powered = false; | 38 bool powered = false; |
| 41 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | 39 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; |
| 42 if (adapter_handle && | 40 if (adapter_handle && |
| 43 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, | 41 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, |
| 44 &adapter_info)) { | 42 &adapter_info)) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 66 device_info.Address.rgBytes[3], | 64 device_info.Address.rgBytes[3], |
| 67 device_info.Address.rgBytes[2], | 65 device_info.Address.rgBytes[2], |
| 68 device_info.Address.rgBytes[1], | 66 device_info.Address.rgBytes[1], |
| 69 device_info.Address.rgBytes[0]); | 67 device_info.Address.rgBytes[0]); |
| 70 state->bluetooth_class = device_info.ulClassofDevice; | 68 state->bluetooth_class = device_info.ulClassofDevice; |
| 71 state->visible = true; | 69 state->visible = true; |
| 72 state->connected = !!device_info.fConnected; | 70 state->connected = !!device_info.fConnected; |
| 73 state->authenticated = !!device_info.fAuthenticated; | 71 state->authenticated = !!device_info.fAuthenticated; |
| 74 } | 72 } |
| 75 | 73 |
| 76 void DiscoverDeviceServices( | |
| 77 const std::string& device_address, | |
| 78 const GUID& protocol_uuid, | |
| 79 ScopedVector<ServiceRecordState>* service_record_states) { | |
| 80 // Bluetooth and WSAQUERYSET for Service Inquiry. See http://goo.gl/2v9pyt. | |
| 81 WSAQUERYSET sdp_query; | |
| 82 ZeroMemory(&sdp_query, sizeof(sdp_query)); | |
| 83 sdp_query.dwSize = sizeof(sdp_query); | |
| 84 GUID protocol = protocol_uuid; | |
| 85 sdp_query.lpServiceClassId = &protocol; | |
| 86 sdp_query.dwNameSpace = NS_BTH; | |
| 87 wchar_t device_address_context[kMaxNumDeviceAddressChar]; | |
| 88 std::size_t length = base::SysUTF8ToWide("(" + device_address + ")").copy( | |
| 89 device_address_context, kMaxNumDeviceAddressChar); | |
| 90 device_address_context[length] = NULL; | |
| 91 sdp_query.lpszContext = device_address_context; | |
| 92 HANDLE sdp_handle; | |
| 93 if (ERROR_SUCCESS != | |
| 94 WSALookupServiceBegin(&sdp_query, LUP_RETURN_ALL, &sdp_handle)) { | |
| 95 return; | |
| 96 } | |
| 97 char sdp_buffer[kServiceDiscoveryResultBufferSize]; | |
| 98 LPWSAQUERYSET sdp_result_data = reinterpret_cast<LPWSAQUERYSET>(sdp_buffer); | |
| 99 while (true) { | |
| 100 DWORD sdp_buffer_size = sizeof(sdp_buffer); | |
| 101 if (ERROR_SUCCESS != | |
| 102 WSALookupServiceNext( | |
| 103 sdp_handle, LUP_RETURN_ALL, &sdp_buffer_size, sdp_result_data)) { | |
| 104 break; | |
| 105 } | |
| 106 ServiceRecordState* service_record_state = new ServiceRecordState(); | |
| 107 service_record_state->name = | |
| 108 base::SysWideToUTF8(sdp_result_data->lpszServiceInstanceName); | |
| 109 service_record_state->address = device_address; | |
| 110 for (uint64 i = 0; i < sdp_result_data->lpBlob->cbSize; i++) { | |
| 111 service_record_state->sdp_bytes.push_back( | |
| 112 sdp_result_data->lpBlob->pBlobData[i]); | |
| 113 } | |
| 114 service_record_states->push_back(service_record_state); | |
| 115 } | |
| 116 WSALookupServiceEnd(sdp_handle); | |
| 117 } | |
| 118 | |
| 119 } // namespace | 74 } // namespace |
| 120 | 75 |
| 121 namespace device { | 76 namespace device { |
| 122 | 77 |
| 123 // static | 78 // static |
| 124 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; | 79 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; |
| 125 | 80 |
| 126 BluetoothTaskManagerWin::BluetoothTaskManagerWin( | 81 BluetoothTaskManagerWin::BluetoothTaskManagerWin( |
| 127 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | 82 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 128 : ui_task_runner_(ui_task_runner), | 83 : ui_task_runner_(ui_task_runner), |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 void BluetoothTaskManagerWin::DiscoverServices( | 368 void BluetoothTaskManagerWin::DiscoverServices( |
| 414 ScopedVector<DeviceState>* device_list) { | 369 ScopedVector<DeviceState>* device_list) { |
| 415 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 370 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 416 net::EnsureWinsockInit(); | 371 net::EnsureWinsockInit(); |
| 417 for (ScopedVector<DeviceState>::iterator iter = device_list->begin(); | 372 for (ScopedVector<DeviceState>::iterator iter = device_list->begin(); |
| 418 iter != device_list->end(); | 373 iter != device_list->end(); |
| 419 ++iter) { | 374 ++iter) { |
| 420 const std::string device_address = (*iter)->address; | 375 const std::string device_address = (*iter)->address; |
| 421 ScopedVector<ServiceRecordState>* service_record_states = | 376 ScopedVector<ServiceRecordState>* service_record_states = |
| 422 &(*iter)->service_record_states; | 377 &(*iter)->service_record_states; |
| 423 | 378 WSAQUERYSET sdp_query; |
| 424 DiscoverDeviceServices( | 379 ZeroMemory(&sdp_query, sizeof(sdp_query)); |
| 425 device_address, L2CAP_PROTOCOL_UUID, service_record_states); | 380 sdp_query.dwSize = sizeof(sdp_query); |
| 381 GUID protocol = L2CAP_PROTOCOL_UUID; |
| 382 sdp_query.lpServiceClassId = &protocol; |
| 383 sdp_query.dwNameSpace = NS_BTH; |
| 384 wchar_t device_address_context[kMaxNumDeviceAddressChar]; |
| 385 std::size_t length = |
| 386 base::SysUTF8ToWide("(" + device_address + ")").copy( |
| 387 device_address_context, kMaxNumDeviceAddressChar); |
| 388 device_address_context[length] = NULL; |
| 389 sdp_query.lpszContext = device_address_context; |
| 390 HANDLE sdp_handle; |
| 391 if (ERROR_SUCCESS != |
| 392 WSALookupServiceBegin(&sdp_query, LUP_RETURN_ALL, &sdp_handle)) { |
| 393 return; |
| 394 } |
| 395 char sdp_buffer[kServiceDiscoveryResultBufferSize]; |
| 396 LPWSAQUERYSET sdp_result_data = reinterpret_cast<LPWSAQUERYSET>(sdp_buffer); |
| 397 DWORD sdp_buffer_size = sizeof(sdp_buffer); |
| 398 while (ERROR_SUCCESS == WSALookupServiceNext(sdp_handle, |
| 399 LUP_RETURN_ALL, |
| 400 &sdp_buffer_size, |
| 401 sdp_result_data)) { |
| 402 ServiceRecordState* service_record_state = new ServiceRecordState(); |
| 403 service_record_state->name = |
| 404 base::SysWideToUTF8(sdp_result_data->lpszServiceInstanceName); |
| 405 service_record_state->address = device_address; |
| 406 for (uint64 i = 0; i < sdp_result_data->lpBlob->cbSize; i++) { |
| 407 service_record_state->sdp_bytes.push_back( |
| 408 sdp_result_data->lpBlob->pBlobData[i]); |
| 409 } |
| 410 service_record_states->push_back(service_record_state); |
| 411 } |
| 412 WSALookupServiceEnd(sdp_handle); |
| 426 } | 413 } |
| 427 } | 414 } |
| 428 | 415 |
| 429 } // namespace device | 416 } // namespace device |
| OLD | NEW |