| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 class IOBufferWithSize; | 25 class IOBufferWithSize; |
| 26 } // namespace net | 26 } // namespace net |
| 27 | 27 |
| 28 namespace device { | 28 namespace device { |
| 29 | 29 |
| 30 class BluetoothServiceRecord; | 30 class BluetoothServiceRecord; |
| 31 | 31 |
| 32 // Implements the BluetoothSocket class for the Mac OS X platform. | 32 // Implements the BluetoothSocket class for the Mac OS X platform. |
| 33 class BluetoothSocketMac : public BluetoothSocket { | 33 class BluetoothSocketMac : public BluetoothSocket { |
| 34 public: | 34 public: |
| 35 static scoped_refptr<BluetoothSocketMac> CreateBluetoothSocket( | 35 typedef base::Callback<void(scoped_refptr<BluetoothSocket>)> |
| 36 IOBluetoothSDPServiceRecord* record); | 36 ConnectSuccessCallback; |
| 37 | 37 |
| 38 // Connects to the peer device and calls |success_callback| when the | 38 // Creates a client socket and connects it to the Bluetooth service |record|. |
| 39 // connection has been established successfully. If an error occurs, calls | 39 // Calls |success_callback|, passing in the created socket, on success. |
| 40 // |error_callback| with a system error message. | 40 // Calls |error_callback| on failure. |
| 41 void Connect(const base::Closure& success_callback, | 41 static void Connect(IOBluetoothSDPServiceRecord* record, |
| 42 const ErrorCompletionCallback& error_callback); | 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); |
| 43 | 52 |
| 44 // BluetoothSocket: | 53 // BluetoothSocket: |
| 45 virtual void Close() OVERRIDE; | 54 virtual void Close() OVERRIDE; |
| 46 virtual void Disconnect(const base::Closure& callback) OVERRIDE; | 55 virtual void Disconnect(const base::Closure& callback) OVERRIDE; |
| 47 virtual void Receive( | 56 virtual void Receive( |
| 48 int /* buffer_size */, | 57 int /* buffer_size */, |
| 49 const ReceiveCompletionCallback& success_callback, | 58 const ReceiveCompletionCallback& success_callback, |
| 50 const ReceiveErrorCompletionCallback& error_callback) OVERRIDE; | 59 const ReceiveErrorCompletionCallback& error_callback) OVERRIDE; |
| 51 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | 60 virtual void Send(scoped_refptr<net::IOBuffer> buffer, |
| 52 int buffer_size, | 61 int buffer_size, |
| 53 const SendCompletionCallback& success_callback, | 62 const SendCompletionCallback& success_callback, |
| 54 const ErrorCompletionCallback& error_callback) OVERRIDE; | 63 const ErrorCompletionCallback& error_callback) OVERRIDE; |
| 55 | 64 |
| 56 // Called by BluetoothRFCOMMChannelDelegate. | 65 // Called by BluetoothRFCOMMChannelDelegate. |
| 57 void OnChannelOpened(IOBluetoothRFCOMMChannel* rfcomm_channel, | 66 void OnChannelOpened(IOBluetoothRFCOMMChannel* rfcomm_channel, |
| 58 IOReturn status); | 67 IOReturn status); |
| 59 void OnChannelClosed(IOBluetoothRFCOMMChannel* rfcomm_channel); | 68 void OnChannelClosed(IOBluetoothRFCOMMChannel* rfcomm_channel); |
| 60 void OnChannelDataReceived(IOBluetoothRFCOMMChannel* rfcomm_channel, | 69 void OnChannelDataReceived(IOBluetoothRFCOMMChannel* rfcomm_channel, |
| 61 void* data, | 70 void* data, |
| 62 size_t length); | 71 size_t length); |
| 63 void OnChannelWriteComplete(IOBluetoothRFCOMMChannel* rfcomm_channel, | 72 void OnChannelWriteComplete(IOBluetoothRFCOMMChannel* rfcomm_channel, |
| 64 void* refcon, | 73 void* refcon, |
| 65 IOReturn status); | 74 IOReturn status); |
| 66 | 75 |
| 67 protected: | |
| 68 virtual ~BluetoothSocketMac(); | |
| 69 | |
| 70 private: | 76 private: |
| 71 BluetoothSocketMac(IOBluetoothSDPServiceRecord* record); | |
| 72 | |
| 73 struct SendRequest { | 77 struct SendRequest { |
| 74 SendRequest(); | 78 SendRequest(); |
| 75 ~SendRequest(); | 79 ~SendRequest(); |
| 76 int buffer_size; | 80 int buffer_size; |
| 77 SendCompletionCallback success_callback; | 81 SendCompletionCallback success_callback; |
| 78 ErrorCompletionCallback error_callback; | 82 ErrorCompletionCallback error_callback; |
| 79 IOReturn status; | 83 IOReturn status; |
| 80 int active_async_writes; | 84 int active_async_writes; |
| 81 bool error_signaled; | 85 bool error_signaled; |
| 82 }; | 86 }; |
| 83 | 87 |
| 84 struct ReceiveCallbacks { | 88 struct ReceiveCallbacks { |
| 85 ReceiveCallbacks(); | 89 ReceiveCallbacks(); |
| 86 ~ReceiveCallbacks(); | 90 ~ReceiveCallbacks(); |
| 87 ReceiveCompletionCallback success_callback; | 91 ReceiveCompletionCallback success_callback; |
| 88 ReceiveErrorCompletionCallback error_callback; | 92 ReceiveErrorCompletionCallback error_callback; |
| 89 }; | 93 }; |
| 90 | 94 |
| 91 struct ConnectCallbacks { | 95 struct ConnectCallbacks { |
| 92 ConnectCallbacks(); | 96 ConnectCallbacks(); |
| 93 ~ConnectCallbacks(); | 97 ~ConnectCallbacks(); |
| 94 base::Closure success_callback; | 98 base::Closure success_callback; |
| 95 ErrorCompletionCallback error_callback; | 99 ErrorCompletionCallback error_callback; |
| 96 }; | 100 }; |
| 97 | 101 |
| 102 BluetoothSocketMac(); |
| 103 virtual ~BluetoothSocketMac(); |
| 104 |
| 98 void ReleaseChannel(); | 105 void ReleaseChannel(); |
| 99 | 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 |
| 100 bool connecting() const { return connect_callbacks_; } | 123 bool connecting() const { return connect_callbacks_; } |
| 101 | 124 |
| 102 // Used to verify that all methods are called on the same thread. | 125 // Used to verify that all methods are called on the same thread. |
| 103 base::ThreadChecker thread_checker_; | 126 base::ThreadChecker thread_checker_; |
| 104 | 127 |
| 105 // The Bluetooth Service definition. | |
| 106 base::scoped_nsobject<IOBluetoothSDPServiceRecord> record_; | |
| 107 | |
| 108 // The RFCOMM channel delegate. | 128 // The RFCOMM channel delegate. |
| 109 base::scoped_nsobject<BluetoothRFCOMMChannelDelegate> delegate_; | 129 base::scoped_nsobject<BluetoothRFCOMMChannelDelegate> delegate_; |
| 110 | 130 |
| 111 // The IOBluetooth RFCOMM channel used to issue commands. | 131 // The IOBluetooth RFCOMM channel used to issue commands. |
| 112 base::scoped_nsobject<IOBluetoothRFCOMMChannel> rfcomm_channel_; | 132 base::scoped_nsobject<IOBluetoothRFCOMMChannel> rfcomm_channel_; |
| 113 | 133 |
| 114 // Connection callbacks -- when a pending async connection is active. | 134 // Connection callbacks -- when a pending async connection is active. |
| 115 scoped_ptr<ConnectCallbacks> connect_callbacks_; | 135 scoped_ptr<ConnectCallbacks> connect_callbacks_; |
| 116 | 136 |
| 117 // Packets received while there is no pending "receive" callback. | 137 // Packets received while there is no pending "receive" callback. |
| 118 std::queue<scoped_refptr<net::IOBufferWithSize> > receive_queue_; | 138 std::queue<scoped_refptr<net::IOBufferWithSize> > receive_queue_; |
| 119 | 139 |
| 120 // Receive callbacks -- when a receive call is active. | 140 // Receive callbacks -- when a receive call is active. |
| 121 scoped_ptr<ReceiveCallbacks> receive_callbacks_; | 141 scoped_ptr<ReceiveCallbacks> receive_callbacks_; |
| 122 | 142 |
| 123 // Send queue -- one entry per pending send operation. | 143 // Send queue -- one entry per pending send operation. |
| 124 std::queue<linked_ptr<SendRequest> > send_queue_; | 144 std::queue<linked_ptr<SendRequest> > send_queue_; |
| 125 | 145 |
| 126 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac); | 146 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac); |
| 127 }; | 147 }; |
| 128 | 148 |
| 129 } // namespace device | 149 } // namespace device |
| 130 | 150 |
| 131 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ | 151 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ |
| OLD | NEW |