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
;
64 virtual void Accept(const AcceptCompletionCallback
& success_callback
,
65 const ErrorCompletionCallback
& error_callback
) OVERRIDE
;
68 // Called by BluetoothRFCOMMChannelDelegate.
69 void OnChannelOpened(IOBluetoothRFCOMMChannel
* rfcomm_channel
,
71 void OnChannelClosed(IOBluetoothRFCOMMChannel
* rfcomm_channel
);
72 void OnChannelDataReceived(IOBluetoothRFCOMMChannel
* rfcomm_channel
,
75 void OnChannelWriteComplete(IOBluetoothRFCOMMChannel
* rfcomm_channel
,
84 SendCompletionCallback success_callback
;
85 ErrorCompletionCallback error_callback
;
87 int active_async_writes
;
91 struct ReceiveCallbacks
{
94 ReceiveCompletionCallback success_callback
;
95 ReceiveErrorCompletionCallback error_callback
;
98 struct ConnectCallbacks
{
101 base::Closure success_callback
;
102 ErrorCompletionCallback error_callback
;
105 BluetoothSocketMac();
106 virtual ~BluetoothSocketMac();
108 void ReleaseChannel();
110 // Connects to the peer device corresponding to |record| and calls
111 // |success_callback| when the connection has been established
112 // successfully. If an error occurs, calls |error_callback| with a system
114 void ConnectImpl(IOBluetoothSDPServiceRecord
* record
,
115 const ConnectSuccessCallback
& success_callback
,
116 const ErrorCompletionCallback
& error_callback
);
118 // Accepts a connection from a peer device. The connection is represented as
119 // the |rfcomm_channel|, which should be an incoming channel in the process of
120 // being opened. Calls |success_callback|, passing in |this|, on success.
121 // Calls |error_callback| on failure.
122 void AcceptConnectionImpl(IOBluetoothRFCOMMChannel
* rfcomm_channel
,
123 const ConnectSuccessCallback
& success_callback
,
124 const ErrorCompletionCallback
& error_callback
);
126 bool connecting() const { return connect_callbacks_
; }
128 // Used to verify that all methods are called on the same thread.
129 base::ThreadChecker thread_checker_
;
131 // The RFCOMM channel delegate.
132 base::scoped_nsobject
<BluetoothRFCOMMChannelDelegate
> delegate_
;
134 // The IOBluetooth RFCOMM channel used to issue commands.
135 base::scoped_nsobject
<IOBluetoothRFCOMMChannel
> rfcomm_channel_
;
137 // Connection callbacks -- when a pending async connection is active.
138 scoped_ptr
<ConnectCallbacks
> connect_callbacks_
;
140 // Packets received while there is no pending "receive" callback.
141 std::queue
<scoped_refptr
<net::IOBufferWithSize
> > receive_queue_
;
143 // Receive callbacks -- when a receive call is active.
144 scoped_ptr
<ReceiveCallbacks
> receive_callbacks_
;
146 // Send queue -- one entry per pending send operation.
147 std::queue
<linked_ptr
<SendRequest
> > send_queue_
;
149 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac
);
152 } // namespace device
154 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_