Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_socket_mac.h
1 // Copyright 2013 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_MAC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_
7
8 #include <queue>
9 #include <string>
10
11 #include <IOKit/IOReturn.h>
12
13 #include "base/mac/scoped_nsobject.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/threading/thread_checker.h"
17 #include "device/bluetooth/bluetooth_socket.h"
18
19 @class BluetoothRFCOMMChannelDelegate;
20 @class IOBluetoothRFCOMMChannel;
21 @class IOBluetoothSDPServiceRecord;
22
23 namespace net {
24 class IOBuffer;
25 class IOBufferWithSize;
26 }  // namespace net
27
28 namespace device {
29
30 class BluetoothServiceRecord;
31
32 // Implements the BluetoothSocket class for the Mac OS X platform.
33 class BluetoothSocketMac : public BluetoothSocket {
34  public:
35   typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
36       ConnectSuccessCallback;
37
38   // Creates a client socket and connects it to the Bluetooth service |record|.
39   // Calls |success_callback|, passing in the created socket, on success.
40   // Calls |error_callback| on failure.
41   static void Connect(IOBluetoothSDPServiceRecord* record,
42                       const ConnectSuccessCallback& success_callback,
43                       const ErrorCompletionCallback& error_callback);
44
45   // Creates a server socket to wrap the |rfcomm_channel|, which should be an
46   // incoming channel in the process of being opened.
47   // Calls |success_callback|, passing in the created socket, on success.
48   // Calls |error_callback| on failure.
49   static void AcceptConnection(IOBluetoothRFCOMMChannel* rfcomm_channel,
50                                const ConnectSuccessCallback& success_callback,
51                                const ErrorCompletionCallback& error_callback);
52
53   // BluetoothSocket:
54   virtual void Close() OVERRIDE;
55   virtual void Disconnect(const base::Closure& callback) OVERRIDE;
56   virtual void Receive(
57       int /* buffer_size */,
58       const ReceiveCompletionCallback& success_callback,
59       const ReceiveErrorCompletionCallback& error_callback) OVERRIDE;
60   virtual void Send(scoped_refptr<net::IOBuffer> buffer,
61                     int buffer_size,
62                     const SendCompletionCallback& success_callback,
63                     const ErrorCompletionCallback& error_callback) OVERRIDE;
64
65   // Called by BluetoothRFCOMMChannelDelegate.
66   void OnChannelOpened(IOBluetoothRFCOMMChannel* rfcomm_channel,
67                        IOReturn status);
68   void OnChannelClosed(IOBluetoothRFCOMMChannel* rfcomm_channel);
69   void OnChannelDataReceived(IOBluetoothRFCOMMChannel* rfcomm_channel,
70                              void* data,
71                              size_t length);
72   void OnChannelWriteComplete(IOBluetoothRFCOMMChannel* rfcomm_channel,
73                               void* refcon,
74                               IOReturn status);
75
76  private:
77   struct SendRequest {
78     SendRequest();
79     ~SendRequest();
80     int buffer_size;
81     SendCompletionCallback success_callback;
82     ErrorCompletionCallback error_callback;
83     IOReturn status;
84     int active_async_writes;
85     bool error_signaled;
86   };
87
88   struct ReceiveCallbacks {
89     ReceiveCallbacks();
90     ~ReceiveCallbacks();
91     ReceiveCompletionCallback success_callback;
92     ReceiveErrorCompletionCallback error_callback;
93   };
94
95   struct ConnectCallbacks {
96     ConnectCallbacks();
97     ~ConnectCallbacks();
98     base::Closure success_callback;
99     ErrorCompletionCallback error_callback;
100   };
101
102   BluetoothSocketMac();
103   virtual ~BluetoothSocketMac();
104
105   void ReleaseChannel();
106
107   // Connects to the peer device corresponding to |record| and calls
108   // |success_callback| when the connection has been established
109   // successfully. If an error occurs, calls |error_callback| with a system
110   // error message.
111   void ConnectImpl(IOBluetoothSDPServiceRecord* record,
112                    const ConnectSuccessCallback& success_callback,
113                    const ErrorCompletionCallback& error_callback);
114
115   // Accepts a connection from a peer device. The connection is represented as
116   // the |rfcomm_channel|, which should be an incoming channel in the process of
117   // being opened. Calls |success_callback|, passing in |this|, on success.
118   // Calls |error_callback| on failure.
119   void AcceptConnectionImpl(IOBluetoothRFCOMMChannel* rfcomm_channel,
120                             const ConnectSuccessCallback& success_callback,
121                             const ErrorCompletionCallback& error_callback);
122
123   bool connecting() const { return connect_callbacks_; }
124
125   // Used to verify that all methods are called on the same thread.
126   base::ThreadChecker thread_checker_;
127
128   // The RFCOMM channel delegate.
129   base::scoped_nsobject<BluetoothRFCOMMChannelDelegate> delegate_;
130
131   // The IOBluetooth RFCOMM channel used to issue commands.
132   base::scoped_nsobject<IOBluetoothRFCOMMChannel> rfcomm_channel_;
133
134   // Connection callbacks -- when a pending async connection is active.
135   scoped_ptr<ConnectCallbacks> connect_callbacks_;
136
137   // Packets received while there is no pending "receive" callback.
138   std::queue<scoped_refptr<net::IOBufferWithSize> > receive_queue_;
139
140   // Receive callbacks -- when a receive call is active.
141   scoped_ptr<ReceiveCallbacks> receive_callbacks_;
142
143   // Send queue -- one entry per pending send operation.
144   std::queue<linked_ptr<SendRequest> > send_queue_;
145
146   DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac);
147 };
148
149 }  // namespace device
150
151 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_