Chromium Code Reviews| Index: device/bluetooth/bluetooth_socket_win.cc |
| diff --git a/device/bluetooth/bluetooth_socket_win.cc b/device/bluetooth/bluetooth_socket_win.cc |
| index 9b1e953fb6ddb709705726698625c3232bccd08a..f362d74d83da654c20e760a6ef50e7b98f8ad153 100644 |
| --- a/device/bluetooth/bluetooth_socket_win.cc |
| +++ b/device/bluetooth/bluetooth_socket_win.cc |
| @@ -4,12 +4,15 @@ |
| #include "device/bluetooth/bluetooth_socket_win.h" |
| +#include <objbase.h> |
| + |
| #include <string> |
| #include "base/logging.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/sequenced_task_runner.h" |
| #include "base/strings/sys_string_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| #include "base/threading/thread_restrictions.h" |
| #include "device/bluetooth/bluetooth_init_win.h" |
| #include "device/bluetooth/bluetooth_service_record_win.h" |
| @@ -24,6 +27,13 @@ namespace { |
| const char kL2CAPNotSupported[] = "Bluetooth L2CAP protocal is not supported"; |
| const char kSocketAlreadyConnected[] = "Socket is already connected."; |
| const char kSocketNotConnected[] = "Socket is not connected."; |
| +const char kInvalidRfcommPort[] = "Invalid RFCCOMM port."; |
| +const char kFailedToCreateSocket[] = "Failed to create socket."; |
| +const char kFailedToBindSocket[] = "Failed to bind socket."; |
| +const char kFailedToListenOnSocket[] = "Failed to listen on socket."; |
| +const char kFailedToGetSockNameForSocket[] = "Failed to getsockname."; |
| +const char kBadUuid[] = "Bad uuid."; |
| +const char kWsaSetServiceError[] = "WSASetService error."; |
| using device::BluetoothSocketWin; |
| @@ -48,27 +58,28 @@ static void DeactivateSocket( |
| namespace device { |
| +struct BluetoothSocketWin::ServiceRegData { |
| + ServiceRegData() { |
| + ZeroMemory(this, sizeof(ServiceRegData)); |
|
rpaquay
2014/04/23 22:52:06
Is it safe to ZeroMemory a struct that contains a
xiyuan
2014/04/23 23:19:42
Nope. Replaced with ZeroMemory on individual membe
|
| + } |
| + |
| + SOCKADDR_BTH address; |
| + CSADDR_INFO address_info; |
| + GUID uuid; |
| + base::string16 name; |
| + WSAQUERYSET service; |
| +}; |
| + |
| // static |
| scoped_refptr<BluetoothSocketWin> BluetoothSocketWin::CreateBluetoothSocket( |
| - const BluetoothServiceRecord& service_record, |
| scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| scoped_refptr<BluetoothSocketThreadWin> socket_thread, |
| net::NetLog* net_log, |
| const net::NetLog::Source& source) { |
| DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| - const BluetoothServiceRecordWin* service_record_win = |
| - static_cast<const BluetoothServiceRecordWin*>(&service_record); |
| - scoped_refptr<BluetoothSocketWin> result( |
| + return make_scoped_refptr( |
| new BluetoothSocketWin(ui_task_runner, socket_thread, net_log, source)); |
| - result->device_address_ = service_record_win->address(); |
| - if (service_record.SupportsRfcomm()) { |
| - result->supports_rfcomm_ = true; |
| - result->rfcomm_channel_ = service_record_win->rfcomm_channel(); |
| - result->bth_addr_ = service_record_win->bth_addr(); |
| - } |
| - |
| - return result; |
| } |
| BluetoothSocketWin::BluetoothSocketWin( |
| @@ -92,6 +103,27 @@ BluetoothSocketWin::~BluetoothSocketWin() { |
| base::Bind(&DeactivateSocket, socket_thread_)); |
| } |
| +void BluetoothSocketWin::StartService( |
| + const BluetoothUUID& uuid, |
| + const std::string& name, |
| + int rfcomm_channel, |
| + const base::Closure& success_callback, |
| + const ErrorCompletionCallback& error_callback, |
| + const OnNewConnectionCallback& new_connection_callback) { |
| + DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + socket_thread_->task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BluetoothSocketWin::DoStartService, |
| + this, |
| + uuid, |
| + name, |
| + rfcomm_channel, |
| + success_callback, |
| + error_callback, |
| + new_connection_callback)); |
| +} |
| + |
| void BluetoothSocketWin::Close() { |
| DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| socket_thread_->task_runner()->PostTask( |
| @@ -99,9 +131,20 @@ void BluetoothSocketWin::Close() { |
| } |
| void BluetoothSocketWin::Connect( |
| + const BluetoothServiceRecord& service_record, |
| const base::Closure& success_callback, |
| const ErrorCompletionCallback& error_callback) { |
| DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + const BluetoothServiceRecordWin* service_record_win = |
| + static_cast<const BluetoothServiceRecordWin*>(&service_record); |
| + device_address_ = service_record_win->address(); |
| + if (service_record.SupportsRfcomm()) { |
| + supports_rfcomm_ = true; |
| + rfcomm_channel_ = service_record_win->rfcomm_channel(); |
| + bth_addr_ = service_record_win->bth_addr(); |
| + } |
| + |
| socket_thread_->task_runner()->PostTask( |
| FROM_HERE, |
| base::Bind( |
| @@ -174,6 +217,14 @@ void BluetoothSocketWin::DoClose() { |
| read_buffer_ = NULL; |
| std::queue<linked_ptr<WriteRequest> > empty; |
| write_queue_.swap(empty); |
| + |
| + if (service_reg_data_) { |
| + if (WSASetService(&service_reg_data_->service,RNRSERVICE_DELETE, 0) == |
| + SOCKET_ERROR) { |
| + LOG(WARNING) << "Failed to unregister service."; |
| + } |
| + service_reg_data_.reset(); |
| + } |
| } |
| void BluetoothSocketWin::DoConnect( |
| @@ -399,4 +450,150 @@ void BluetoothSocketWin::PostSendCompletion( |
| ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, bytes_written)); |
| } |
| +void BluetoothSocketWin::DoStartService( |
| + const BluetoothUUID& uuid, |
| + const std::string& name, |
| + int rfcomm_channel, |
| + const base::Closure& success_callback, |
| + const ErrorCompletionCallback& error_callback, |
| + const OnNewConnectionCallback& new_connection_callback) { |
| + DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); |
| + DCHECK(!tcp_socket_ && |
| + !service_reg_data_ && |
| + on_new_connection_callback_.is_null()); |
| + |
| + // The valid range is 0-30. 0 means BT_PORT_ANY and 1-30 are the |
| + // valid RFCOMM port numbers of SOCKADDR_BTH. |
| + if (rfcomm_channel < 0 || rfcomm_channel > 30) { |
| + LOG(WARNING) << "Failed to start service: " |
| + << "Invalid RFCCOMM port " << rfcomm_channel |
| + << ", uuid=" << uuid.value(); |
| + PostErrorCompletion(error_callback, kInvalidRfcommPort); |
| + return; |
| + } |
| + |
| + SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); |
| + if (socket_fd == INVALID_SOCKET) { |
| + LOG(WARNING) << "Failed to start service: create socket, " |
| + << "winsock err=" << WSAGetLastError(); |
| + PostErrorCompletion(error_callback, kFailedToCreateSocket); |
| + return; |
| + } |
| + |
| + scoped_ptr<net::TCPSocket> scoped_socket( |
| + new net::TCPSocket(NULL, net::NetLog::Source())); |
| + scoped_socket->AdoptBoundSocket(socket_fd); |
| + |
| + SOCKADDR_BTH sa; |
| + struct sockaddr* sock_addr = reinterpret_cast<struct sockaddr*>(&sa); |
| + int sock_addr_len = sizeof(sa); |
| + ZeroMemory(&sa, sock_addr_len); |
| + sa.addressFamily = AF_BTH; |
| + sa.port = rfcomm_channel ? rfcomm_channel : BT_PORT_ANY; |
| + if (bind(socket_fd, sock_addr, sock_addr_len) < 0) { |
| + LOG(WARNING) << "Failed to start service: create socket, " |
| + << "winsock err=" << WSAGetLastError(); |
| + PostErrorCompletion(error_callback, kFailedToBindSocket); |
| + return; |
| + } |
| + |
| + const int kListenBacklog = 5; |
| + if (scoped_socket->Listen(kListenBacklog) < 0) { |
| + LOG(WARNING) << "Failed to start service: Listen" |
| + << "winsock err=" << WSAGetLastError(); |
| + PostErrorCompletion(error_callback, kFailedToListenOnSocket); |
| + return; |
| + } |
| + |
| + scoped_ptr<ServiceRegData> reg_data(new ServiceRegData); |
| + reg_data->name = base::UTF8ToUTF16(name); |
| + |
| + if (getsockname(socket_fd, sock_addr, &sock_addr_len)) { |
| + LOG(WARNING) << "Failed to start service: getsockname, " |
| + << "winsock err=" << WSAGetLastError(); |
| + PostErrorCompletion(error_callback, kFailedToGetSockNameForSocket); |
| + return; |
| + } |
| + reg_data->address = sa; |
| + |
| + reg_data->address_info.LocalAddr.iSockaddrLength = sizeof(sa); |
| + reg_data->address_info.LocalAddr.lpSockaddr = |
| + reinterpret_cast<struct sockaddr*>(®_data->address); |
| + reg_data->address_info.iSocketType = SOCK_STREAM; |
| + reg_data->address_info.iProtocol = BTHPROTO_RFCOMM; |
| + |
| + base::string16 cannonical_uuid = L"{" + base::ASCIIToUTF16( |
| + uuid.canonical_value()) + L"}"; |
| + if (!SUCCEEDED(CLSIDFromString(cannonical_uuid.c_str(), ®_data->uuid))) { |
| + LOG(WARNING) << "Failed to start service: " |
| + << ", bad uuid=" << cannonical_uuid; |
| + PostErrorCompletion(error_callback, kBadUuid); |
| + return; |
| + } |
| + |
| + reg_data->service.dwSize = sizeof(WSAQUERYSET); |
| + reg_data->service.lpszServiceInstanceName = |
| + const_cast<LPWSTR>(reg_data->name.c_str()); |
| + reg_data->service.lpServiceClassId = ®_data->uuid; |
| + reg_data->service.dwNameSpace = NS_BTH; |
| + reg_data->service.dwNumberOfCsAddrs = 1; |
| + reg_data->service.lpcsaBuffer = ®_data->address_info; |
| + |
| + if (WSASetService(®_data->service, |
| + RNRSERVICE_REGISTER, 0) == SOCKET_ERROR) { |
| + LOG(WARNING) << "Failed to register profile: WSASetService" |
| + << "winsock err=" << WSAGetLastError(); |
| + PostErrorCompletion(error_callback, kWsaSetServiceError); |
| + return; |
| + } |
| + |
| + tcp_socket_ = scoped_socket.Pass(); |
| + service_reg_data_ = reg_data.Pass(); |
| + on_new_connection_callback_ = new_connection_callback; |
| + DoAccept(); |
| + |
| + PostSuccess(success_callback); |
| +} |
| + |
| +void BluetoothSocketWin::DoAccept() { |
| + DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); |
| + int result = tcp_socket_->Accept( |
| + &accept_socket_, |
| + &accept_address_, |
| + base::Bind(&BluetoothSocketWin::OnAcceptOnSocketThread, this)); |
| + if (result != net::OK && result != net::ERR_IO_PENDING) |
| + LOG(WARNING) << "Failed to accept, net err=" << result; |
| +} |
| + |
| +void BluetoothSocketWin::OnAcceptOnSocketThread(int accept_result) { |
| + DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); |
| + if (accept_result != net::OK) { |
| + LOG(WARNING) << "OnAccept error, net err=" << accept_result; |
| + return; |
| + } |
| + |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BluetoothSocketWin::OnAcceptOnUI, |
| + this, |
| + base::Passed(&accept_socket_), |
| + accept_address_)); |
| + DoAccept(); |
| +} |
| + |
| +void BluetoothSocketWin::OnAcceptOnUI( |
| + scoped_ptr<net::TCPSocket> accept_socket, |
| + const net::IPEndPoint& peer_address) { |
| + DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + scoped_refptr<BluetoothSocketWin> peer = CreateBluetoothSocket( |
| + ui_task_runner_, |
| + socket_thread_, |
| + net_log_, |
| + source_); |
| + peer->tcp_socket_ = accept_socket.Pass(); |
| + |
| + on_new_connection_callback_.Run(peer, peer_address); |
| +} |
| + |
| } // namespace device |