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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_
11 #include <IOKit/IOReturn.h>
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"
19 @class BluetoothRFCOMMChannelDelegate;
20 @class IOBluetoothRFCOMMChannel;
21 @class IOBluetoothSDPServiceRecord;
25 class IOBufferWithSize;
30 class BluetoothServiceRecord;
32 // Implements the BluetoothSocket class for the Mac OS X platform.
33 class BluetoothSocketMac : public BluetoothSocket {
35 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)>
36 ConnectSuccessCallback;
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);
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);
54 virtual void Close() OVERRIDE;
55 virtual void Disconnect(const base::Closure& callback) OVERRIDE;
57 int /* buffer_size */,
58 const ReceiveCompletionCallback& success_callback,
59 const ReceiveErrorCompletionCallback& error_callback) OVERRIDE;
60 virtual void Send(scoped_refptr<net::IOBuffer> buffer,
62 const SendCompletionCallback& success_callback,
63 const ErrorCompletionCallback& error_callback) OVERRIDE;
65 // Called by BluetoothRFCOMMChannelDelegate.
66 void OnChannelOpened(IOBluetoothRFCOMMChannel* rfcomm_channel,
68 void OnChannelClosed(IOBluetoothRFCOMMChannel* rfcomm_channel);
69 void OnChannelDataReceived(IOBluetoothRFCOMMChannel* rfcomm_channel,
72 void OnChannelWriteComplete(IOBluetoothRFCOMMChannel* rfcomm_channel,
81 SendCompletionCallback success_callback;
82 ErrorCompletionCallback error_callback;
84 int active_async_writes;
88 struct ReceiveCallbacks {
91 ReceiveCompletionCallback success_callback;
92 ReceiveErrorCompletionCallback error_callback;
95 struct ConnectCallbacks {
98 base::Closure success_callback;
99 ErrorCompletionCallback error_callback;
102 BluetoothSocketMac();
103 virtual ~BluetoothSocketMac();
105 void ReleaseChannel();
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
111 void ConnectImpl(IOBluetoothSDPServiceRecord* record,
112 const ConnectSuccessCallback& success_callback,
113 const ErrorCompletionCallback& error_callback);
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);
123 bool connecting() const { return connect_callbacks_; }
125 // Used to verify that all methods are called on the same thread.
126 base::ThreadChecker thread_checker_;
128 // The RFCOMM channel delegate.
129 base::scoped_nsobject<BluetoothRFCOMMChannelDelegate> delegate_;
131 // The IOBluetooth RFCOMM channel used to issue commands.
132 base::scoped_nsobject<IOBluetoothRFCOMMChannel> rfcomm_channel_;
134 // Connection callbacks -- when a pending async connection is active.
135 scoped_ptr<ConnectCallbacks> connect_callbacks_;
137 // Packets received while there is no pending "receive" callback.
138 std::queue<scoped_refptr<net::IOBufferWithSize> > receive_queue_;
140 // Receive callbacks -- when a receive call is active.
141 scoped_ptr<ReceiveCallbacks> receive_callbacks_;
143 // Send queue -- one entry per pending send operation.
144 std::queue<linked_ptr<SendRequest> > send_queue_;
146 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac);
149 } // namespace device
151 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_