QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_socket_mac.h
blob768b85023b94523a6ed84198f9f764d9923fcd0e
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_
8 #include <queue>
9 #include <string>
11 #import <IOBluetooth/IOBluetooth.h>
12 #import <IOKit/IOReturn.h>
14 #include "base/mac/scoped_nsobject.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/threading/thread_checker.h"
19 #include "device/bluetooth/bluetooth_adapter.h"
20 #include "device/bluetooth/bluetooth_socket.h"
21 #include "device/bluetooth/bluetooth_uuid.h"
23 @class BluetoothRfcommConnectionListener;
24 @class BluetoothL2capConnectionListener;
26 namespace net {
27 class IOBuffer;
28 class IOBufferWithSize;
31 namespace device {
33 class BluetoothAdapterMac;
34 class BluetoothChannelMac;
36 // Implements the BluetoothSocket class for the Mac OS X platform.
37 class BluetoothSocketMac : public BluetoothSocket {
38 public:
39 static scoped_refptr<BluetoothSocketMac> CreateSocket();
41 // Connects this socket to the service on |device| published as UUID |uuid|.
42 // The underlying protocol and PSM or Channel is obtained through service
43 // discovery. On a successful connection, the socket properties will be
44 // updated and |success_callback| called. On failure, |error_callback| will be
45 // called with a message explaining the cause of failure.
46 void Connect(IOBluetoothDevice* device,
47 const BluetoothUUID& uuid,
48 const base::Closure& success_callback,
49 const ErrorCompletionCallback& error_callback);
51 // Listens for incoming RFCOMM connections using this socket: Publishes an
52 // RFCOMM service on the |adapter| as UUID |uuid| with Channel
53 // |options.channel|, or an automatically allocated Channel if
54 // |options.channel| is left null. The service is published with English name
55 // |options.name| if that is non-null. |success_callback| will be called if
56 // the service is successfully registered, |error_callback| on failure with a
57 // message explaining the cause.
58 void ListenUsingRfcomm(scoped_refptr<BluetoothAdapterMac> adapter,
59 const BluetoothUUID& uuid,
60 const BluetoothAdapter::ServiceOptions& options,
61 const base::Closure& success_callback,
62 const ErrorCompletionCallback& error_callback);
64 // Listens for incoming L2CAP connections using this socket: Publishes an
65 // L2CAP service on the |adapter| as UUID |uuid| with PSM |options.psm|, or an
66 // automatically allocated PSM if |options.psm| is left null. The service is
67 // published with English name |options.name| if that is non-null.
68 // |success_callback| will be called if the service is successfully
69 // registered, |error_callback| on failure with a message explaining the
70 // cause.
71 void ListenUsingL2cap(scoped_refptr<BluetoothAdapterMac> adapter,
72 const BluetoothUUID& uuid,
73 const BluetoothAdapter::ServiceOptions& options,
74 const base::Closure& success_callback,
75 const ErrorCompletionCallback& error_callback);
77 // BluetoothSocket:
78 void Close() override;
79 void Disconnect(const base::Closure& callback) override;
80 void Receive(int /* buffer_size */,
81 const ReceiveCompletionCallback& success_callback,
82 const ReceiveErrorCompletionCallback& error_callback) override;
83 void Send(scoped_refptr<net::IOBuffer> buffer,
84 int buffer_size,
85 const SendCompletionCallback& success_callback,
86 const ErrorCompletionCallback& error_callback) override;
87 void Accept(const AcceptCompletionCallback& success_callback,
88 const ErrorCompletionCallback& error_callback) override;
90 // Callback that is invoked when the OS completes an SDP query.
91 // |status| is the returned status from the SDP query, |device| is the
92 // IOBluetoothDevice for which the query was made. The remaining
93 // parameters are those from |Connect()|.
94 void OnSDPQueryComplete(
95 IOReturn status,
96 IOBluetoothDevice* device,
97 const base::Closure& success_callback,
98 const ErrorCompletionCallback& error_callback);
100 // Called by BluetoothRfcommConnectionListener and
101 // BluetoothL2capConnectionListener.
102 void OnChannelOpened(scoped_ptr<BluetoothChannelMac> channel);
104 // Called by |channel_|.
105 // Note: OnChannelOpenComplete might be called before the |channel_| is set.
106 void OnChannelOpenComplete(const std::string& device_address,
107 IOReturn status);
108 void OnChannelClosed();
109 void OnChannelDataReceived(void* data, size_t length);
110 void OnChannelWriteComplete(void* refcon, IOReturn status);
112 private:
113 struct AcceptRequest {
114 AcceptRequest();
115 ~AcceptRequest();
117 AcceptCompletionCallback success_callback;
118 ErrorCompletionCallback error_callback;
121 struct SendRequest {
122 SendRequest();
123 ~SendRequest();
124 int buffer_size;
125 SendCompletionCallback success_callback;
126 ErrorCompletionCallback error_callback;
127 IOReturn status;
128 int active_async_writes;
129 bool error_signaled;
132 struct ReceiveCallbacks {
133 ReceiveCallbacks();
134 ~ReceiveCallbacks();
135 ReceiveCompletionCallback success_callback;
136 ReceiveErrorCompletionCallback error_callback;
139 struct ConnectCallbacks {
140 ConnectCallbacks();
141 ~ConnectCallbacks();
142 base::Closure success_callback;
143 ErrorCompletionCallback error_callback;
146 BluetoothSocketMac();
147 ~BluetoothSocketMac() override;
149 // Accepts a single incoming connection.
150 void AcceptConnectionRequest();
152 void ReleaseChannel();
153 void ReleaseListener();
155 bool is_connecting() const { return connect_callbacks_; }
157 // Used to verify that all methods are called on the same thread.
158 base::ThreadChecker thread_checker_;
160 // Adapter the socket is registered against. This is only present when the
161 // socket is listening.
162 scoped_refptr<BluetoothAdapterMac> adapter_;
164 // UUID of the profile being connected to, or that the socket is listening on.
165 device::BluetoothUUID uuid_;
167 // Simple helpers that register for OS notifications and forward them to
168 // |this| profile.
169 base::scoped_nsobject<BluetoothRfcommConnectionListener>
170 rfcomm_connection_listener_;
171 base::scoped_nsobject<BluetoothL2capConnectionListener>
172 l2cap_connection_listener_;
174 // A handle to the service record registered in the system SDP server.
175 // Used to eventually unregister the service.
176 BluetoothSDPServiceRecordHandle service_record_handle_;
178 // The channel used to issue commands.
179 scoped_ptr<BluetoothChannelMac> channel_;
181 // Connection callbacks -- when a pending async connection is active.
182 scoped_ptr<ConnectCallbacks> connect_callbacks_;
184 // Packets received while there is no pending "receive" callback.
185 std::queue<scoped_refptr<net::IOBufferWithSize> > receive_queue_;
187 // Receive callbacks -- when a receive call is active.
188 scoped_ptr<ReceiveCallbacks> receive_callbacks_;
190 // Send queue -- one entry per pending send operation.
191 std::queue<linked_ptr<SendRequest>> send_queue_;
193 // The pending request to an Accept() call, or null if there is no pending
194 // request.
195 scoped_ptr<AcceptRequest> accept_request_;
197 // Queue of incoming connections.
198 std::queue<linked_ptr<BluetoothChannelMac>> accept_queue_;
200 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac);
203 } // namespace device
205 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_