blob: c50560cbe009d162dec2c8120c2cfdf2b011e179 [file] [log] [blame]
[email protected]6cc9b8d2013-03-18 21:07:031// 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
[email protected]2b38b732014-06-10 20:56:468#import <IOBluetooth/IOBluetooth.h>
9#import <IOKit/IOReturn.h>
avi176e2692015-12-22 19:26:5210#include <stddef.h>
[email protected]3b017562014-04-10 17:09:5511
thakis08c9d69b02016-04-11 04:37:2512#include <memory>
thakis08c9d69b02016-04-11 04:37:2513#include <string>
14
Brett Wilsona62d9c02017-09-20 20:53:2015#include "base/containers/queue.h"
[email protected]99c8c9b92014-04-17 04:14:3016#include "base/mac/scoped_nsobject.h"
avi176e2692015-12-22 19:26:5217#include "base/macros.h"
[email protected]6cc9b8d2013-03-18 21:07:0318#include "base/memory/ref_counted.h"
[email protected]3b017562014-04-10 17:09:5519#include "base/threading/thread_checker.h"
[email protected]dbedd5022014-07-03 06:08:2020#include "device/bluetooth/bluetooth_adapter.h"
[email protected]6cc9b8d2013-03-18 21:07:0321#include "device/bluetooth/bluetooth_socket.h"
Jason Majors47659c42019-07-08 18:23:2822#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
[email protected]6cc9b8d2013-03-18 21:07:0323
[email protected]2b38b732014-06-10 20:56:4624@class BluetoothRfcommConnectionListener;
[email protected]286b0de2014-06-16 22:07:1625@class BluetoothL2capConnectionListener;
[email protected]6cc9b8d2013-03-18 21:07:0326
27namespace net {
[email protected]73131fc12014-04-08 01:02:2728class IOBuffer;
[email protected]3b017562014-04-10 17:09:5529class IOBufferWithSize;
[email protected]2b38b732014-06-10 20:56:4630}
[email protected]6cc9b8d2013-03-18 21:07:0331
32namespace device {
33
[email protected]5902a7f2014-06-18 13:08:5034class BluetoothAdapterMac;
[email protected]8696f572014-06-12 12:59:2035class BluetoothChannelMac;
[email protected]6cc9b8d2013-03-18 21:07:0336
[email protected]99c8c9b92014-04-17 04:14:3037// Implements the BluetoothSocket class for the Mac OS X platform.
[email protected]6cc9b8d2013-03-18 21:07:0338class BluetoothSocketMac : public BluetoothSocket {
39 public:
[email protected]2b38b732014-06-10 20:56:4640 static scoped_refptr<BluetoothSocketMac> CreateSocket();
[email protected]476f29d2013-04-24 09:06:5141
[email protected]2b38b732014-06-10 20:56:4642 // Connects this socket to the service on |device| published as UUID |uuid|.
43 // The underlying protocol and PSM or Channel is obtained through service
44 // discovery. On a successful connection, the socket properties will be
45 // updated and |success_callback| called. On failure, |error_callback| will be
46 // called with a message explaining the cause of failure.
47 void Connect(IOBluetoothDevice* device,
48 const BluetoothUUID& uuid,
49 const base::Closure& success_callback,
50 const ErrorCompletionCallback& error_callback);
[email protected]649726b92014-04-28 23:54:1951
[email protected]2b38b732014-06-10 20:56:4652 // Listens for incoming RFCOMM connections using this socket: Publishes an
[email protected]dbedd5022014-07-03 06:08:2053 // RFCOMM service on the |adapter| as UUID |uuid| with Channel
54 // |options.channel|, or an automatically allocated Channel if
55 // |options.channel| is left null. The service is published with English name
56 // |options.name| if that is non-null. |success_callback| will be called if
57 // the service is successfully registered, |error_callback| on failure with a
58 // message explaining the cause.
[email protected]5902a7f2014-06-18 13:08:5059 void ListenUsingRfcomm(scoped_refptr<BluetoothAdapterMac> adapter,
[email protected]2b38b732014-06-10 20:56:4660 const BluetoothUUID& uuid,
[email protected]dbedd5022014-07-03 06:08:2061 const BluetoothAdapter::ServiceOptions& options,
[email protected]2b38b732014-06-10 20:56:4662 const base::Closure& success_callback,
63 const ErrorCompletionCallback& error_callback);
64
65 // Listens for incoming L2CAP connections using this socket: Publishes an
[email protected]dbedd5022014-07-03 06:08:2066 // L2CAP service on the |adapter| as UUID |uuid| with PSM |options.psm|, or an
67 // automatically allocated PSM if |options.psm| is left null. The service is
68 // published with English name |options.name| if that is non-null.
[email protected]2b38b732014-06-10 20:56:4669 // |success_callback| will be called if the service is successfully
70 // registered, |error_callback| on failure with a message explaining the
71 // cause.
[email protected]5902a7f2014-06-18 13:08:5072 void ListenUsingL2cap(scoped_refptr<BluetoothAdapterMac> adapter,
[email protected]2b38b732014-06-10 20:56:4673 const BluetoothUUID& uuid,
[email protected]dbedd5022014-07-03 06:08:2074 const BluetoothAdapter::ServiceOptions& options,
[email protected]2b38b732014-06-10 20:56:4675 const base::Closure& success_callback,
76 const ErrorCompletionCallback& error_callback);
[email protected]3b017562014-04-10 17:09:5577
[email protected]99c8c9b92014-04-17 04:14:3078 // BluetoothSocket:
dchengb5aaf402014-10-22 23:07:5279 void Close() override;
80 void Disconnect(const base::Closure& callback) override;
81 void Receive(int /* buffer_size */,
82 const ReceiveCompletionCallback& success_callback,
83 const ReceiveErrorCompletionCallback& error_callback) override;
84 void Send(scoped_refptr<net::IOBuffer> buffer,
85 int buffer_size,
86 const SendCompletionCallback& success_callback,
87 const ErrorCompletionCallback& error_callback) override;
88 void Accept(const AcceptCompletionCallback& success_callback,
89 const ErrorCompletionCallback& error_callback) override;
[email protected]37f0b7f2014-05-12 18:09:5190
[email protected]2b38b732014-06-10 20:56:4691 // Callback that is invoked when the OS completes an SDP query.
92 // |status| is the returned status from the SDP query, |device| is the
93 // IOBluetoothDevice for which the query was made. The remaining
94 // parameters are those from |Connect()|.
95 void OnSDPQueryComplete(
96 IOReturn status,
97 IOBluetoothDevice* device,
98 const base::Closure& success_callback,
99 const ErrorCompletionCallback& error_callback);
[email protected]6cc9b8d2013-03-18 21:07:03100
[email protected]286b0de2014-06-16 22:07:16101 // Called by BluetoothRfcommConnectionListener and
102 // BluetoothL2capConnectionListener.
thakis08c9d69b02016-04-11 04:37:25103 void OnChannelOpened(std::unique_ptr<BluetoothChannelMac> channel);
[email protected]2b38b732014-06-10 20:56:46104
[email protected]8696f572014-06-12 12:59:20105 // Called by |channel_|.
106 // Note: OnChannelOpenComplete might be called before the |channel_| is set.
107 void OnChannelOpenComplete(const std::string& device_address,
108 IOReturn status);
109 void OnChannelClosed();
110 void OnChannelDataReceived(void* data, size_t length);
111 void OnChannelWriteComplete(void* refcon, IOReturn status);
[email protected]6cc9b8d2013-03-18 21:07:03112
[email protected]6cc9b8d2013-03-18 21:07:03113 private:
[email protected]2b38b732014-06-10 20:56:46114 struct AcceptRequest {
115 AcceptRequest();
116 ~AcceptRequest();
117
118 AcceptCompletionCallback success_callback;
119 ErrorCompletionCallback error_callback;
120 };
121
[email protected]3b017562014-04-10 17:09:55122 struct SendRequest {
123 SendRequest();
124 ~SendRequest();
125 int buffer_size;
126 SendCompletionCallback success_callback;
127 ErrorCompletionCallback error_callback;
128 IOReturn status;
129 int active_async_writes;
130 bool error_signaled;
131 };
[email protected]6cc9b8d2013-03-18 21:07:03132
[email protected]3b017562014-04-10 17:09:55133 struct ReceiveCallbacks {
134 ReceiveCallbacks();
135 ~ReceiveCallbacks();
136 ReceiveCompletionCallback success_callback;
137 ReceiveErrorCompletionCallback error_callback;
138 };
139
140 struct ConnectCallbacks {
141 ConnectCallbacks();
142 ~ConnectCallbacks();
143 base::Closure success_callback;
144 ErrorCompletionCallback error_callback;
145 };
146
[email protected]649726b92014-04-28 23:54:19147 BluetoothSocketMac();
dchengb5aaf402014-10-22 23:07:52148 ~BluetoothSocketMac() override;
[email protected]649726b92014-04-28 23:54:19149
[email protected]2b38b732014-06-10 20:56:46150 // Accepts a single incoming connection.
151 void AcceptConnectionRequest();
152
[email protected]3b017562014-04-10 17:09:55153 void ReleaseChannel();
[email protected]2b38b732014-06-10 20:56:46154 void ReleaseListener();
[email protected]3b017562014-04-10 17:09:55155
dchengbd780bf2016-03-12 04:56:00156 bool is_connecting() const { return !!connect_callbacks_; }
[email protected]3b017562014-04-10 17:09:55157
[email protected]99c8c9b92014-04-17 04:14:30158 // Used to verify that all methods are called on the same thread.
[email protected]3b017562014-04-10 17:09:55159 base::ThreadChecker thread_checker_;
[email protected]99c8c9b92014-04-17 04:14:30160
[email protected]2b38b732014-06-10 20:56:46161 // Adapter the socket is registered against. This is only present when the
162 // socket is listening.
[email protected]5902a7f2014-06-18 13:08:50163 scoped_refptr<BluetoothAdapterMac> adapter_;
[email protected]2b38b732014-06-10 20:56:46164
165 // UUID of the profile being connected to, or that the socket is listening on.
166 device::BluetoothUUID uuid_;
167
[email protected]286b0de2014-06-16 22:07:16168 // Simple helpers that register for OS notifications and forward them to
[email protected]2b38b732014-06-10 20:56:46169 // |this| profile.
170 base::scoped_nsobject<BluetoothRfcommConnectionListener>
171 rfcomm_connection_listener_;
[email protected]286b0de2014-06-16 22:07:16172 base::scoped_nsobject<BluetoothL2capConnectionListener>
173 l2cap_connection_listener_;
[email protected]2b38b732014-06-10 20:56:46174
shrike07aa6dca2016-10-25 00:08:24175 // The service record registered in the system SDP server, used to
176 // eventually unregister the service.
177 base::scoped_nsobject<IOBluetoothSDPServiceRecord> service_record_;
[email protected]99c8c9b92014-04-17 04:14:30178
[email protected]8696f572014-06-12 12:59:20179 // The channel used to issue commands.
thakis08c9d69b02016-04-11 04:37:25180 std::unique_ptr<BluetoothChannelMac> channel_;
[email protected]99c8c9b92014-04-17 04:14:30181
[email protected]3b017562014-04-10 17:09:55182 // Connection callbacks -- when a pending async connection is active.
thakis08c9d69b02016-04-11 04:37:25183 std::unique_ptr<ConnectCallbacks> connect_callbacks_;
[email protected]99c8c9b92014-04-17 04:14:30184
[email protected]3b017562014-04-10 17:09:55185 // Packets received while there is no pending "receive" callback.
Brett Wilsona62d9c02017-09-20 20:53:20186 base::queue<scoped_refptr<net::IOBufferWithSize>> receive_queue_;
[email protected]99c8c9b92014-04-17 04:14:30187
[email protected]3b017562014-04-10 17:09:55188 // Receive callbacks -- when a receive call is active.
thakis08c9d69b02016-04-11 04:37:25189 std::unique_ptr<ReceiveCallbacks> receive_callbacks_;
[email protected]99c8c9b92014-04-17 04:14:30190
[email protected]3b017562014-04-10 17:09:55191 // Send queue -- one entry per pending send operation.
Avi Drissmand3c4eab2018-12-05 02:42:09192 base::queue<std::unique_ptr<SendRequest>> send_queue_;
[email protected]2b38b732014-06-10 20:56:46193
194 // The pending request to an Accept() call, or null if there is no pending
195 // request.
thakis08c9d69b02016-04-11 04:37:25196 std::unique_ptr<AcceptRequest> accept_request_;
[email protected]2b38b732014-06-10 20:56:46197
[email protected]8696f572014-06-12 12:59:20198 // Queue of incoming connections.
Avi Drissmand3c4eab2018-12-05 02:42:09199 base::queue<std::unique_ptr<BluetoothChannelMac>> accept_queue_;
[email protected]6cc9b8d2013-03-18 21:07:03200
201 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac);
202};
203
204} // namespace device
205
206#endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_