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. 4 5#ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ 6#define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ 7 8#include <WinSock2.h> 9 10#include <string> 11 12#include "base/memory/ref_counted.h" 13#include "device/bluetooth/bluetooth_adapter.h" 14#include "device/bluetooth/bluetooth_service_record_win.h" 15#include "device/bluetooth/bluetooth_socket.h" 16#include "device/bluetooth/bluetooth_socket_net.h" 17#include "device/bluetooth/bluetooth_uuid.h" 18#include "net/base/ip_endpoint.h" 19#include "net/socket/tcp_socket.h" 20 21namespace device { 22 23class BluetoothAdapter; 24class BluetoothDeviceWin; 25 26// The BluetoothSocketWin class implements BluetoothSocket for the Microsoft 27// Windows platform. 28class BluetoothSocketWin : public BluetoothSocketNet { 29 public: 30 static scoped_refptr<BluetoothSocketWin> CreateBluetoothSocket( 31 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 32 scoped_refptr<BluetoothSocketThread> socket_thread); 33 34 // Connect to the peer device and calls |success_callback| when the 35 // connection has been established successfully. If an error occurs, calls 36 // |error_callback| with a system error message. 37 void Connect(const BluetoothDeviceWin* device, 38 const BluetoothUUID& uuid, 39 const base::Closure& success_callback, 40 const ErrorCompletionCallback& error_callback); 41 42 // Listens using this socket using an RFCOMM service published as UUID |uuid| 43 // with Channel |options.channel|, or an automatically allocated Channel if 44 // |options.channel| is null. |success_callback| will be called if the service 45 // is successfully registered, |error_callback| on failure with a message 46 // explaining the cause. 47 void Listen(scoped_refptr<BluetoothAdapter> adapter, 48 const BluetoothUUID& uuid, 49 const BluetoothAdapter::ServiceOptions& options, 50 const base::Closure& success_callback, 51 const ErrorCompletionCallback& error_callback); 52 53 // BluetoothSocketNet: 54 void ResetData(); 55 56 // BluetoothSocket: 57 virtual void Accept(const AcceptCompletionCallback& success_callback, 58 const ErrorCompletionCallback& error_callback) OVERRIDE; 59 60 protected: 61 virtual ~BluetoothSocketWin(); 62 63 private: 64 struct ServiceRegData; 65 66 BluetoothSocketWin(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 67 scoped_refptr<BluetoothSocketThread> socket_thread); 68 69 void DoConnect(const base::Closure& success_callback, 70 const ErrorCompletionCallback& error_callback); 71 void DoListen(const BluetoothUUID& uuid, 72 int rfcomm_channel, 73 const base::Closure& success_callback, 74 const ErrorCompletionCallback& error_callback); 75 void DoAccept(const AcceptCompletionCallback& success_callback, 76 const ErrorCompletionCallback& error_callback); 77 void OnAcceptOnSocketThread(const AcceptCompletionCallback& success_callback, 78 const ErrorCompletionCallback& error_callback, 79 int accept_result); 80 void OnAcceptOnUI(scoped_ptr<net::TCPSocket> accept_socket, 81 const net::IPEndPoint& peer_address, 82 const AcceptCompletionCallback& success_callback, 83 const ErrorCompletionCallback& error_callback); 84 85 std::string device_address_; 86 bool supports_rfcomm_; 87 uint8 rfcomm_channel_; 88 BTH_ADDR bth_addr_; 89 90 // Data members below are only used when listening. 91 scoped_refptr<device::BluetoothAdapter> adapter_; 92 scoped_ptr<ServiceRegData> service_reg_data_; 93 scoped_ptr<net::TCPSocket> accept_socket_; 94 net::IPEndPoint accept_address_; 95 96 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin); 97}; 98 99} // namespace device 100 101#endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ 102