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