| 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_socket_win.h" | 5 #include "device/bluetooth/bluetooth_socket_win.h" |
| 6 | 6 |
| 7 #include <objbase.h> | 7 #include <objbase.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 GUID uuid; | 71 GUID uuid; |
| 72 base::string16 name; | 72 base::string16 name; |
| 73 WSAQUERYSET service; | 73 WSAQUERYSET service; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 // static | 76 // static |
| 77 scoped_refptr<BluetoothSocketWin> | 77 scoped_refptr<BluetoothSocketWin> |
| 78 BluetoothSocketWin::CreateBluetoothSocket( | 78 BluetoothSocketWin::CreateBluetoothSocket( |
| 79 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 79 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 80 scoped_refptr<device::BluetoothSocketThread> socket_thread) { | 80 scoped_refptr<device::BluetoothSocketThread> socket_thread) { |
| 81 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | 81 DCHECK(ui_task_runner->RunsTasksInCurrentSequence()); |
| 82 | 82 |
| 83 return make_scoped_refptr( | 83 return make_scoped_refptr( |
| 84 new BluetoothSocketWin(ui_task_runner, socket_thread)); | 84 new BluetoothSocketWin(ui_task_runner, socket_thread)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 BluetoothSocketWin::BluetoothSocketWin( | 87 BluetoothSocketWin::BluetoothSocketWin( |
| 88 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 88 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 89 scoped_refptr<BluetoothSocketThread> socket_thread) | 89 scoped_refptr<BluetoothSocketThread> socket_thread) |
| 90 : BluetoothSocketNet(ui_task_runner, socket_thread), | 90 : BluetoothSocketNet(ui_task_runner, socket_thread), |
| 91 supports_rfcomm_(false), | 91 supports_rfcomm_(false), |
| 92 rfcomm_channel_(0xFF), | 92 rfcomm_channel_(0xFF), |
| 93 bth_addr_(BTH_ADDR_NULL) { | 93 bth_addr_(BTH_ADDR_NULL) { |
| 94 } | 94 } |
| 95 | 95 |
| 96 BluetoothSocketWin::~BluetoothSocketWin() { | 96 BluetoothSocketWin::~BluetoothSocketWin() { |
| 97 } | 97 } |
| 98 | 98 |
| 99 void BluetoothSocketWin::Connect( | 99 void BluetoothSocketWin::Connect( |
| 100 const BluetoothDeviceWin* device, | 100 const BluetoothDeviceWin* device, |
| 101 const BluetoothUUID& uuid, | 101 const BluetoothUUID& uuid, |
| 102 const base::Closure& success_callback, | 102 const base::Closure& success_callback, |
| 103 const ErrorCompletionCallback& error_callback) { | 103 const ErrorCompletionCallback& error_callback) { |
| 104 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 104 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 105 DCHECK(device); | 105 DCHECK(device); |
| 106 | 106 |
| 107 if (!uuid.IsValid()) { | 107 if (!uuid.IsValid()) { |
| 108 error_callback.Run(kInvalidUUID); | 108 error_callback.Run(kInvalidUUID); |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 | 111 |
| 112 const BluetoothServiceRecordWin* service_record_win = | 112 const BluetoothServiceRecordWin* service_record_win = |
| 113 device->GetServiceRecord(uuid); | 113 device->GetServiceRecord(uuid); |
| 114 if (!service_record_win) { | 114 if (!service_record_win) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 131 base::Bind(&BluetoothSocketWin::PostSuccess, this, success_callback), | 131 base::Bind(&BluetoothSocketWin::PostSuccess, this, success_callback), |
| 132 base::Bind( | 132 base::Bind( |
| 133 &BluetoothSocketWin::PostErrorCompletion, this, error_callback))); | 133 &BluetoothSocketWin::PostErrorCompletion, this, error_callback))); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void BluetoothSocketWin::Listen(scoped_refptr<BluetoothAdapter> adapter, | 136 void BluetoothSocketWin::Listen(scoped_refptr<BluetoothAdapter> adapter, |
| 137 const BluetoothUUID& uuid, | 137 const BluetoothUUID& uuid, |
| 138 const BluetoothAdapter::ServiceOptions& options, | 138 const BluetoothAdapter::ServiceOptions& options, |
| 139 const base::Closure& success_callback, | 139 const base::Closure& success_callback, |
| 140 const ErrorCompletionCallback& error_callback) { | 140 const ErrorCompletionCallback& error_callback) { |
| 141 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 141 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 142 | 142 |
| 143 adapter_ = adapter; | 143 adapter_ = adapter; |
| 144 int rfcomm_channel = options.channel ? *options.channel : 0; | 144 int rfcomm_channel = options.channel ? *options.channel : 0; |
| 145 | 145 |
| 146 // TODO(xiyuan): Use |options.name|. | 146 // TODO(xiyuan): Use |options.name|. |
| 147 socket_thread()->task_runner()->PostTask( | 147 socket_thread()->task_runner()->PostTask( |
| 148 FROM_HERE, | 148 FROM_HERE, |
| 149 base::Bind(&BluetoothSocketWin::DoListen, | 149 base::Bind(&BluetoothSocketWin::DoListen, |
| 150 this, | 150 this, |
| 151 uuid, | 151 uuid, |
| 152 rfcomm_channel, | 152 rfcomm_channel, |
| 153 success_callback, | 153 success_callback, |
| 154 error_callback)); | 154 error_callback)); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void BluetoothSocketWin::ResetData() { | 157 void BluetoothSocketWin::ResetData() { |
| 158 if (service_reg_data_) { | 158 if (service_reg_data_) { |
| 159 if (WSASetService(&service_reg_data_->service,RNRSERVICE_DELETE, 0) == | 159 if (WSASetService(&service_reg_data_->service,RNRSERVICE_DELETE, 0) == |
| 160 SOCKET_ERROR) { | 160 SOCKET_ERROR) { |
| 161 LOG(WARNING) << "Failed to unregister service."; | 161 LOG(WARNING) << "Failed to unregister service."; |
| 162 } | 162 } |
| 163 service_reg_data_.reset(); | 163 service_reg_data_.reset(); |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 | 166 |
| 167 void BluetoothSocketWin::Accept( | 167 void BluetoothSocketWin::Accept( |
| 168 const AcceptCompletionCallback& success_callback, | 168 const AcceptCompletionCallback& success_callback, |
| 169 const ErrorCompletionCallback& error_callback) { | 169 const ErrorCompletionCallback& error_callback) { |
| 170 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 170 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 171 | 171 |
| 172 socket_thread()->task_runner()->PostTask( | 172 socket_thread()->task_runner()->PostTask( |
| 173 FROM_HERE, | 173 FROM_HERE, |
| 174 base::Bind(&BluetoothSocketWin::DoAccept, | 174 base::Bind(&BluetoothSocketWin::DoAccept, |
| 175 this, | 175 this, |
| 176 success_callback, | 176 success_callback, |
| 177 error_callback)); | 177 error_callback)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void BluetoothSocketWin::DoConnect( | 180 void BluetoothSocketWin::DoConnect( |
| 181 const base::Closure& success_callback, | 181 const base::Closure& success_callback, |
| 182 const ErrorCompletionCallback& error_callback) { | 182 const ErrorCompletionCallback& error_callback) { |
| 183 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 183 DCHECK(socket_thread()->task_runner()->RunsTasksInCurrentSequence()); |
| 184 base::ThreadRestrictions::AssertIOAllowed(); | 184 base::ThreadRestrictions::AssertIOAllowed(); |
| 185 | 185 |
| 186 if (tcp_socket()) { | 186 if (tcp_socket()) { |
| 187 error_callback.Run(kSocketAlreadyConnected); | 187 error_callback.Run(kSocketAlreadyConnected); |
| 188 return; | 188 return; |
| 189 } | 189 } |
| 190 | 190 |
| 191 if (!supports_rfcomm_) { | 191 if (!supports_rfcomm_) { |
| 192 // TODO(youngki) add support for L2CAP sockets as well. | 192 // TODO(youngki) add support for L2CAP sockets as well. |
| 193 error_callback.Run(kL2CAPNotSupported); | 193 error_callback.Run(kL2CAPNotSupported); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 230 |
| 231 SetTCPSocket(std::move(scoped_socket)); | 231 SetTCPSocket(std::move(scoped_socket)); |
| 232 success_callback.Run(); | 232 success_callback.Run(); |
| 233 } | 233 } |
| 234 | 234 |
| 235 void BluetoothSocketWin::DoListen( | 235 void BluetoothSocketWin::DoListen( |
| 236 const BluetoothUUID& uuid, | 236 const BluetoothUUID& uuid, |
| 237 int rfcomm_channel, | 237 int rfcomm_channel, |
| 238 const base::Closure& success_callback, | 238 const base::Closure& success_callback, |
| 239 const ErrorCompletionCallback& error_callback) { | 239 const ErrorCompletionCallback& error_callback) { |
| 240 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 240 DCHECK(socket_thread()->task_runner()->RunsTasksInCurrentSequence()); |
| 241 DCHECK(!tcp_socket() && !service_reg_data_); | 241 DCHECK(!tcp_socket() && !service_reg_data_); |
| 242 | 242 |
| 243 // The valid range is 0-30. 0 means BT_PORT_ANY and 1-30 are the | 243 // The valid range is 0-30. 0 means BT_PORT_ANY and 1-30 are the |
| 244 // valid RFCOMM port numbers of SOCKADDR_BTH. | 244 // valid RFCOMM port numbers of SOCKADDR_BTH. |
| 245 if (rfcomm_channel < 0 || rfcomm_channel > 30) { | 245 if (rfcomm_channel < 0 || rfcomm_channel > 30) { |
| 246 LOG(WARNING) << "Failed to start service: " | 246 LOG(WARNING) << "Failed to start service: " |
| 247 << "Invalid RFCCOMM port " << rfcomm_channel | 247 << "Invalid RFCCOMM port " << rfcomm_channel |
| 248 << ", uuid=" << uuid.value(); | 248 << ", uuid=" << uuid.value(); |
| 249 PostErrorCompletion(error_callback, kInvalidRfcommPort); | 249 PostErrorCompletion(error_callback, kInvalidRfcommPort); |
| 250 return; | 250 return; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 330 |
| 331 SetTCPSocket(std::move(scoped_socket)); | 331 SetTCPSocket(std::move(scoped_socket)); |
| 332 service_reg_data_ = std::move(reg_data); | 332 service_reg_data_ = std::move(reg_data); |
| 333 | 333 |
| 334 PostSuccess(success_callback); | 334 PostSuccess(success_callback); |
| 335 } | 335 } |
| 336 | 336 |
| 337 void BluetoothSocketWin::DoAccept( | 337 void BluetoothSocketWin::DoAccept( |
| 338 const AcceptCompletionCallback& success_callback, | 338 const AcceptCompletionCallback& success_callback, |
| 339 const ErrorCompletionCallback& error_callback) { | 339 const ErrorCompletionCallback& error_callback) { |
| 340 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 340 DCHECK(socket_thread()->task_runner()->RunsTasksInCurrentSequence()); |
| 341 int result = tcp_socket()->Accept( | 341 int result = tcp_socket()->Accept( |
| 342 &accept_socket_, | 342 &accept_socket_, |
| 343 &accept_address_, | 343 &accept_address_, |
| 344 base::Bind(&BluetoothSocketWin::OnAcceptOnSocketThread, | 344 base::Bind(&BluetoothSocketWin::OnAcceptOnSocketThread, |
| 345 this, | 345 this, |
| 346 success_callback, | 346 success_callback, |
| 347 error_callback)); | 347 error_callback)); |
| 348 if (result != net::OK && result != net::ERR_IO_PENDING) { | 348 if (result != net::OK && result != net::ERR_IO_PENDING) { |
| 349 LOG(WARNING) << "Failed to accept, net err=" << result; | 349 LOG(WARNING) << "Failed to accept, net err=" << result; |
| 350 PostErrorCompletion(error_callback, kFailedToAccept); | 350 PostErrorCompletion(error_callback, kFailedToAccept); |
| 351 } | 351 } |
| 352 } | 352 } |
| 353 | 353 |
| 354 void BluetoothSocketWin::OnAcceptOnSocketThread( | 354 void BluetoothSocketWin::OnAcceptOnSocketThread( |
| 355 const AcceptCompletionCallback& success_callback, | 355 const AcceptCompletionCallback& success_callback, |
| 356 const ErrorCompletionCallback& error_callback, | 356 const ErrorCompletionCallback& error_callback, |
| 357 int accept_result) { | 357 int accept_result) { |
| 358 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 358 DCHECK(socket_thread()->task_runner()->RunsTasksInCurrentSequence()); |
| 359 if (accept_result != net::OK) { | 359 if (accept_result != net::OK) { |
| 360 LOG(WARNING) << "OnAccept error, net err=" << accept_result; | 360 LOG(WARNING) << "OnAccept error, net err=" << accept_result; |
| 361 PostErrorCompletion(error_callback, kFailedToAccept); | 361 PostErrorCompletion(error_callback, kFailedToAccept); |
| 362 return; | 362 return; |
| 363 } | 363 } |
| 364 | 364 |
| 365 ui_task_runner()->PostTask( | 365 ui_task_runner()->PostTask( |
| 366 FROM_HERE, | 366 FROM_HERE, |
| 367 base::Bind(&BluetoothSocketWin::OnAcceptOnUI, | 367 base::Bind(&BluetoothSocketWin::OnAcceptOnUI, |
| 368 this, | 368 this, |
| 369 base::Passed(&accept_socket_), | 369 base::Passed(&accept_socket_), |
| 370 accept_address_, | 370 accept_address_, |
| 371 success_callback, | 371 success_callback, |
| 372 error_callback)); | 372 error_callback)); |
| 373 } | 373 } |
| 374 | 374 |
| 375 void BluetoothSocketWin::OnAcceptOnUI( | 375 void BluetoothSocketWin::OnAcceptOnUI( |
| 376 std::unique_ptr<net::TCPSocket> accept_socket, | 376 std::unique_ptr<net::TCPSocket> accept_socket, |
| 377 const net::IPEndPoint& peer_address, | 377 const net::IPEndPoint& peer_address, |
| 378 const AcceptCompletionCallback& success_callback, | 378 const AcceptCompletionCallback& success_callback, |
| 379 const ErrorCompletionCallback& error_callback) { | 379 const ErrorCompletionCallback& error_callback) { |
| 380 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 380 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 381 | 381 |
| 382 const std::string peer_device_address = | 382 const std::string peer_device_address = |
| 383 IPEndPointToBluetoothAddress(peer_address); | 383 IPEndPointToBluetoothAddress(peer_address); |
| 384 const BluetoothDevice* peer_device = adapter_->GetDevice(peer_device_address); | 384 const BluetoothDevice* peer_device = adapter_->GetDevice(peer_device_address); |
| 385 if (!peer_device) { | 385 if (!peer_device) { |
| 386 LOG(WARNING) << "OnAccept failed with unknown device, addr=" | 386 LOG(WARNING) << "OnAccept failed with unknown device, addr=" |
| 387 << peer_device_address; | 387 << peer_device_address; |
| 388 error_callback.Run(kFailedToAccept); | 388 error_callback.Run(kFailedToAccept); |
| 389 return; | 389 return; |
| 390 } | 390 } |
| 391 | 391 |
| 392 scoped_refptr<BluetoothSocketWin> peer_socket = | 392 scoped_refptr<BluetoothSocketWin> peer_socket = |
| 393 CreateBluetoothSocket(ui_task_runner(), socket_thread()); | 393 CreateBluetoothSocket(ui_task_runner(), socket_thread()); |
| 394 peer_socket->SetTCPSocket(std::move(accept_socket)); | 394 peer_socket->SetTCPSocket(std::move(accept_socket)); |
| 395 success_callback.Run(peer_device, peer_socket); | 395 success_callback.Run(peer_device, peer_socket); |
| 396 } | 396 } |
| 397 | 397 |
| 398 } // namespace device | 398 } // namespace device |
| OLD | NEW |