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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
20 #include "device/bluetooth/bluetooth_device_win.h" | 20 #include "device/bluetooth/bluetooth_device_win.h" |
21 #include "device/bluetooth/bluetooth_init_win.h" | 21 #include "device/bluetooth/bluetooth_init_win.h" |
22 #include "device/bluetooth/bluetooth_service_record_win.h" | 22 #include "device/bluetooth/bluetooth_service_record_win.h" |
23 #include "device/bluetooth/bluetooth_socket_thread.h" | 23 #include "device/bluetooth/bluetooth_socket_thread.h" |
24 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
25 #include "net/base/ip_endpoint.h" | 25 #include "net/base/ip_endpoint.h" |
26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
27 #include "net/base/winsock_init.h" | 27 #include "net/base/winsock_init.h" |
28 #include "net/base/winsock_util.h" | 28 #include "net/base/winsock_util.h" |
| 29 #include "net/log/net_log_source.h" |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 const char kL2CAPNotSupported[] = "Bluetooth L2CAP protocal is not supported"; | 33 const char kL2CAPNotSupported[] = "Bluetooth L2CAP protocal is not supported"; |
33 const char kSocketAlreadyConnected[] = "Socket is already connected."; | 34 const char kSocketAlreadyConnected[] = "Socket is already connected."; |
34 const char kInvalidRfcommPort[] = "Invalid RFCCOMM port."; | 35 const char kInvalidRfcommPort[] = "Invalid RFCCOMM port."; |
35 const char kFailedToCreateSocket[] = "Failed to create socket."; | 36 const char kFailedToCreateSocket[] = "Failed to create socket."; |
36 const char kFailedToBindSocket[] = "Failed to bind socket."; | 37 const char kFailedToBindSocket[] = "Failed to bind socket."; |
37 const char kFailedToListenOnSocket[] = "Failed to listen on socket."; | 38 const char kFailedToListenOnSocket[] = "Failed to listen on socket."; |
38 const char kFailedToGetSockNameForSocket[] = "Failed to getsockname."; | 39 const char kFailedToGetSockNameForSocket[] = "Failed to getsockname."; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 return; | 188 return; |
188 } | 189 } |
189 | 190 |
190 if (!supports_rfcomm_) { | 191 if (!supports_rfcomm_) { |
191 // TODO(youngki) add support for L2CAP sockets as well. | 192 // TODO(youngki) add support for L2CAP sockets as well. |
192 error_callback.Run(kL2CAPNotSupported); | 193 error_callback.Run(kL2CAPNotSupported); |
193 return; | 194 return; |
194 } | 195 } |
195 | 196 |
196 std::unique_ptr<net::TCPSocket> scoped_socket( | 197 std::unique_ptr<net::TCPSocket> scoped_socket( |
197 new net::TCPSocket(NULL, NULL, net::NetLog::Source())); | 198 new net::TCPSocket(NULL, NULL, net::NetLogSource())); |
198 net::EnsureWinsockInit(); | 199 net::EnsureWinsockInit(); |
199 SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); | 200 SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); |
200 SOCKADDR_BTH sa; | 201 SOCKADDR_BTH sa; |
201 ZeroMemory(&sa, sizeof(sa)); | 202 ZeroMemory(&sa, sizeof(sa)); |
202 sa.addressFamily = AF_BTH; | 203 sa.addressFamily = AF_BTH; |
203 sa.port = rfcomm_channel_; | 204 sa.port = rfcomm_channel_; |
204 sa.btAddr = bth_addr_; | 205 sa.btAddr = bth_addr_; |
205 | 206 |
206 // TODO(rpaquay): Condider making this call non-blocking. | 207 // TODO(rpaquay): Condider making this call non-blocking. |
207 int status = connect(socket_fd, reinterpret_cast<SOCKADDR*>(&sa), sizeof(sa)); | 208 int status = connect(socket_fd, reinterpret_cast<SOCKADDR*>(&sa), sizeof(sa)); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 LOG(WARNING) << "Failed to start service: create socket, " | 255 LOG(WARNING) << "Failed to start service: create socket, " |
255 << "winsock err=" << WSAGetLastError(); | 256 << "winsock err=" << WSAGetLastError(); |
256 PostErrorCompletion(error_callback, kFailedToCreateSocket); | 257 PostErrorCompletion(error_callback, kFailedToCreateSocket); |
257 return; | 258 return; |
258 } | 259 } |
259 | 260 |
260 // Note that |socket_fd| belongs to a non-TCP address family (i.e. AF_BTH), | 261 // Note that |socket_fd| belongs to a non-TCP address family (i.e. AF_BTH), |
261 // TCPSocket methods that involve address could not be called. So bind() | 262 // TCPSocket methods that involve address could not be called. So bind() |
262 // is called on |socket_fd| directly. | 263 // is called on |socket_fd| directly. |
263 std::unique_ptr<net::TCPSocket> scoped_socket( | 264 std::unique_ptr<net::TCPSocket> scoped_socket( |
264 new net::TCPSocket(NULL, NULL, net::NetLog::Source())); | 265 new net::TCPSocket(NULL, NULL, net::NetLogSource())); |
265 scoped_socket->AdoptListenSocket(socket_fd); | 266 scoped_socket->AdoptListenSocket(socket_fd); |
266 | 267 |
267 SOCKADDR_BTH sa; | 268 SOCKADDR_BTH sa; |
268 struct sockaddr* sock_addr = reinterpret_cast<struct sockaddr*>(&sa); | 269 struct sockaddr* sock_addr = reinterpret_cast<struct sockaddr*>(&sa); |
269 int sock_addr_len = sizeof(sa); | 270 int sock_addr_len = sizeof(sa); |
270 ZeroMemory(&sa, sock_addr_len); | 271 ZeroMemory(&sa, sock_addr_len); |
271 sa.addressFamily = AF_BTH; | 272 sa.addressFamily = AF_BTH; |
272 sa.port = rfcomm_channel ? rfcomm_channel : BT_PORT_ANY; | 273 sa.port = rfcomm_channel ? rfcomm_channel : BT_PORT_ANY; |
273 if (bind(socket_fd, sock_addr, sock_addr_len) < 0) { | 274 if (bind(socket_fd, sock_addr, sock_addr_len) < 0) { |
274 LOG(WARNING) << "Failed to start service: create socket, " | 275 LOG(WARNING) << "Failed to start service: create socket, " |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 return; | 389 return; |
389 } | 390 } |
390 | 391 |
391 scoped_refptr<BluetoothSocketWin> peer_socket = | 392 scoped_refptr<BluetoothSocketWin> peer_socket = |
392 CreateBluetoothSocket(ui_task_runner(), socket_thread()); | 393 CreateBluetoothSocket(ui_task_runner(), socket_thread()); |
393 peer_socket->SetTCPSocket(std::move(accept_socket)); | 394 peer_socket->SetTCPSocket(std::move(accept_socket)); |
394 success_callback.Run(peer_device, peer_socket); | 395 success_callback.Run(peer_device, peer_socket); |
395 } | 396 } |
396 | 397 |
397 } // namespace device | 398 } // namespace device |
OLD | NEW |