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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_socket_win.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "device/bluetooth/bluetooth_init_win.h"
18 #include "device/bluetooth/bluetooth_service_record_win.h"
19 #include "device/bluetooth/bluetooth_socket_thread.h"
20 #include "net/base/io_buffer.h"
21 #include "net/base/ip_endpoint.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/winsock_init.h"
27 const char kL2CAPNotSupported[] = "Bluetooth L2CAP protocal is not supported";
28 const char kSocketAlreadyConnected[] = "Socket is already connected.";
29 const char kInvalidRfcommPort[] = "Invalid RFCCOMM port.";
30 const char kFailedToCreateSocket[] = "Failed to create socket.";
31 const char kFailedToBindSocket[] = "Failed to bind socket.";
32 const char kFailedToListenOnSocket[] = "Failed to listen on socket.";
33 const char kFailedToGetSockNameForSocket[] = "Failed to getsockname.";
34 const char kBadUuid[] = "Bad uuid.";
35 const char kWsaSetServiceError[] = "WSASetService error.";
41 struct BluetoothSocketWin::ServiceRegData {
43 ZeroMemory(&address, sizeof(address));
44 ZeroMemory(&address_info, sizeof(address_info));
45 ZeroMemory(&uuid, sizeof(uuid));
46 ZeroMemory(&service, sizeof(service));
50 CSADDR_INFO address_info;
57 scoped_refptr<BluetoothSocketWin>
58 BluetoothSocketWin::CreateBluetoothSocket(
59 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
60 scoped_refptr<device::BluetoothSocketThread> socket_thread,
62 const net::NetLog::Source& source) {
63 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
65 return make_scoped_refptr(
66 new BluetoothSocketWin(ui_task_runner, socket_thread, net_log, source));
69 BluetoothSocketWin::BluetoothSocketWin(
70 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
71 scoped_refptr<BluetoothSocketThread> socket_thread,
73 const net::NetLog::Source& source)
74 : BluetoothSocketNet(ui_task_runner, socket_thread, net_log, source),
75 supports_rfcomm_(false),
77 bth_addr_(BTH_ADDR_NULL) {
80 BluetoothSocketWin::~BluetoothSocketWin() {
83 void BluetoothSocketWin::StartService(
84 const BluetoothUUID& uuid,
85 const std::string& name,
87 const base::Closure& success_callback,
88 const ErrorCompletionCallback& error_callback,
89 const OnNewConnectionCallback& new_connection_callback) {
90 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread());
92 socket_thread()->task_runner()->PostTask(
94 base::Bind(&BluetoothSocketWin::DoStartService,
101 new_connection_callback));
104 void BluetoothSocketWin::Connect(
105 const BluetoothServiceRecord& service_record,
106 const base::Closure& success_callback,
107 const ErrorCompletionCallback& error_callback) {
108 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread());
110 const BluetoothServiceRecordWin* service_record_win =
111 static_cast<const BluetoothServiceRecordWin*>(&service_record);
112 device_address_ = service_record_win->address();
113 if (service_record.SupportsRfcomm()) {
114 supports_rfcomm_ = true;
115 rfcomm_channel_ = service_record_win->rfcomm_channel();
116 bth_addr_ = service_record_win->bth_addr();
119 socket_thread()->task_runner()->PostTask(
122 &BluetoothSocketWin::DoConnect,
124 base::Bind(&BluetoothSocketWin::PostSuccess, this, success_callback),
126 &BluetoothSocketWin::PostErrorCompletion, this, error_callback)));
130 void BluetoothSocketWin::ResetData() {
131 if (service_reg_data_) {
132 if (WSASetService(&service_reg_data_->service,RNRSERVICE_DELETE, 0) ==
134 LOG(WARNING) << "Failed to unregister service.";
136 service_reg_data_.reset();
140 void BluetoothSocketWin::DoConnect(
141 const base::Closure& success_callback,
142 const ErrorCompletionCallback& error_callback) {
143 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread());
144 base::ThreadRestrictions::AssertIOAllowed();
147 error_callback.Run(kSocketAlreadyConnected);
151 if (!supports_rfcomm_) {
152 // TODO(youngki) add support for L2CAP sockets as well.
153 error_callback.Run(kL2CAPNotSupported);
158 net::EnsureWinsockInit();
159 SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
161 ZeroMemory(&sa, sizeof(sa));
162 sa.addressFamily = AF_BTH;
163 sa.port = rfcomm_channel_;
164 sa.btAddr = bth_addr_;
166 // TODO(rpaquay): Condider making this call non-blocking.
167 int status = connect(socket_fd, reinterpret_cast<SOCKADDR*>(&sa), sizeof(sa));
168 DWORD error_code = WSAGetLastError();
169 if (!(status == 0 || error_code == WSAEINPROGRESS)) {
170 LOG(ERROR) << "Failed to connect bluetooth socket "
171 << "(" << device_address_ << "): "
172 << logging::SystemErrorCodeToString(error_code);
173 error_callback.Run("Error connecting to socket: " +
174 logging::SystemErrorCodeToString(error_code));
175 closesocket(socket_fd);
179 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the
180 // TCPSocket implementation does not actually require one.
182 tcp_socket()->AdoptConnectedSocket(socket_fd, net::IPEndPoint());
183 if (net_result != net::OK) {
184 error_callback.Run("Error connecting to socket: " +
185 std::string(net::ErrorToString(net_result)));
186 closesocket(socket_fd);
190 success_callback.Run();
193 void BluetoothSocketWin::DoStartService(
194 const BluetoothUUID& uuid,
195 const std::string& name,
197 const base::Closure& success_callback,
198 const ErrorCompletionCallback& error_callback,
199 const OnNewConnectionCallback& new_connection_callback) {
200 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread());
201 DCHECK(!tcp_socket() &&
202 !service_reg_data_ &&
203 on_new_connection_callback_.is_null());
205 // The valid range is 0-30. 0 means BT_PORT_ANY and 1-30 are the
206 // valid RFCOMM port numbers of SOCKADDR_BTH.
207 if (rfcomm_channel < 0 || rfcomm_channel > 30) {
208 LOG(WARNING) << "Failed to start service: "
209 << "Invalid RFCCOMM port " << rfcomm_channel
210 << ", uuid=" << uuid.value();
211 PostErrorCompletion(error_callback, kInvalidRfcommPort);
215 SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
216 if (socket_fd == INVALID_SOCKET) {
217 LOG(WARNING) << "Failed to start service: create socket, "
218 << "winsock err=" << WSAGetLastError();
219 PostErrorCompletion(error_callback, kFailedToCreateSocket);
223 // Note that |socket_fd| belongs to a non-TCP address family (i.e. AF_BTH),
224 // TCPSocket methods that involve address could not be called. So bind()
225 // is called on |socket_fd| directly.
226 scoped_ptr<net::TCPSocket> scoped_socket(
227 new net::TCPSocket(NULL, net::NetLog::Source()));
228 scoped_socket->AdoptListenSocket(socket_fd);
231 struct sockaddr* sock_addr = reinterpret_cast<struct sockaddr*>(&sa);
232 int sock_addr_len = sizeof(sa);
233 ZeroMemory(&sa, sock_addr_len);
234 sa.addressFamily = AF_BTH;
235 sa.port = rfcomm_channel ? rfcomm_channel : BT_PORT_ANY;
236 if (bind(socket_fd, sock_addr, sock_addr_len) < 0) {
237 LOG(WARNING) << "Failed to start service: create socket, "
238 << "winsock err=" << WSAGetLastError();
239 PostErrorCompletion(error_callback, kFailedToBindSocket);
243 const int kListenBacklog = 5;
244 if (scoped_socket->Listen(kListenBacklog) < 0) {
245 LOG(WARNING) << "Failed to start service: Listen"
246 << "winsock err=" << WSAGetLastError();
247 PostErrorCompletion(error_callback, kFailedToListenOnSocket);
251 scoped_ptr<ServiceRegData> reg_data(new ServiceRegData);
252 reg_data->name = base::UTF8ToUTF16(name);
254 if (getsockname(socket_fd, sock_addr, &sock_addr_len)) {
255 LOG(WARNING) << "Failed to start service: getsockname, "
256 << "winsock err=" << WSAGetLastError();
257 PostErrorCompletion(error_callback, kFailedToGetSockNameForSocket);
260 reg_data->address = sa;
262 reg_data->address_info.LocalAddr.iSockaddrLength = sizeof(sa);
263 reg_data->address_info.LocalAddr.lpSockaddr =
264 reinterpret_cast<struct sockaddr*>(®_data->address);
265 reg_data->address_info.iSocketType = SOCK_STREAM;
266 reg_data->address_info.iProtocol = BTHPROTO_RFCOMM;
268 base::string16 cannonical_uuid = L"{" + base::ASCIIToUTF16(
269 uuid.canonical_value()) + L"}";
270 if (!SUCCEEDED(CLSIDFromString(cannonical_uuid.c_str(), ®_data->uuid))) {
271 LOG(WARNING) << "Failed to start service: "
272 << ", bad uuid=" << cannonical_uuid;
273 PostErrorCompletion(error_callback, kBadUuid);
277 reg_data->service.dwSize = sizeof(WSAQUERYSET);
278 reg_data->service.lpszServiceInstanceName =
279 const_cast<LPWSTR>(reg_data->name.c_str());
280 reg_data->service.lpServiceClassId = ®_data->uuid;
281 reg_data->service.dwNameSpace = NS_BTH;
282 reg_data->service.dwNumberOfCsAddrs = 1;
283 reg_data->service.lpcsaBuffer = ®_data->address_info;
285 if (WSASetService(®_data->service,
286 RNRSERVICE_REGISTER, 0) == SOCKET_ERROR) {
287 LOG(WARNING) << "Failed to register profile: WSASetService"
288 << "winsock err=" << WSAGetLastError();
289 PostErrorCompletion(error_callback, kWsaSetServiceError);
293 SetTCPSocket(scoped_socket.Pass());
294 service_reg_data_ = reg_data.Pass();
295 on_new_connection_callback_ = new_connection_callback;
298 PostSuccess(success_callback);
301 void BluetoothSocketWin::DoAccept() {
302 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread());
303 int result = tcp_socket()->Accept(
306 base::Bind(&BluetoothSocketWin::OnAcceptOnSocketThread, this));
307 if (result != net::OK && result != net::ERR_IO_PENDING)
308 LOG(WARNING) << "Failed to accept, net err=" << result;
311 void BluetoothSocketWin::OnAcceptOnSocketThread(int accept_result) {
312 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread());
313 if (accept_result != net::OK) {
314 LOG(WARNING) << "OnAccept error, net err=" << accept_result;
318 ui_task_runner()->PostTask(
320 base::Bind(&BluetoothSocketWin::OnAcceptOnUI,
322 base::Passed(&accept_socket_),
327 void BluetoothSocketWin::OnAcceptOnUI(
328 scoped_ptr<net::TCPSocket> accept_socket,
329 const net::IPEndPoint& peer_address) {
330 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread());
332 scoped_refptr<BluetoothSocketWin> peer = CreateBluetoothSocket(
337 peer->SetTCPSocket(accept_socket.Pass());
339 on_new_connection_callback_.Run(peer, peer_address);
342 } // namespace device