OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SERIAL_SERIAL_IO_HANDLER_H_ | 5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 12 matching lines...) Expand all Loading... |
23 namespace device { | 23 namespace device { |
24 | 24 |
25 // Provides a simplified interface for performing asynchronous I/O on serial | 25 // Provides a simplified interface for performing asynchronous I/O on serial |
26 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O | 26 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O |
27 // operations hold a reference to this object until completion so that memory | 27 // operations hold a reference to this object until completion so that memory |
28 // doesn't disappear out from under the OS. | 28 // doesn't disappear out from under the OS. |
29 class SerialIoHandler : public base::RefCounted<SerialIoHandler> { | 29 class SerialIoHandler : public base::RefCounted<SerialIoHandler> { |
30 public: | 30 public: |
31 // Constructs an instance of some platform-specific subclass. | 31 // Constructs an instance of some platform-specific subclass. |
32 static scoped_refptr<SerialIoHandler> Create( | 32 static scoped_refptr<SerialIoHandler> Create( |
33 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
34 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | 33 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
35 | 34 |
36 typedef base::Callback<void(bool success)> OpenCompleteCallback; | 35 typedef base::Callback<void(bool success)> OpenCompleteCallback; |
37 | 36 |
38 // Initiates an asynchronous Open of the device. | 37 // Initiates an asynchronous Open of the device. |
39 virtual void Open(const std::string& port, | 38 virtual void Open(const std::string& port, |
40 const serial::ConnectionOptions& options, | 39 const serial::ConnectionOptions& options, |
41 const OpenCompleteCallback& callback); | 40 const OpenCompleteCallback& callback); |
42 | 41 |
43 #if defined(OS_CHROMEOS) | 42 #if defined(OS_CHROMEOS) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // Initiates a BREAK signal. Places the transmission line in a break state | 104 // Initiates a BREAK signal. Places the transmission line in a break state |
106 // until the |ClearBreak| is called. | 105 // until the |ClearBreak| is called. |
107 virtual bool SetBreak() = 0; | 106 virtual bool SetBreak() = 0; |
108 | 107 |
109 // Terminates the BREAK signal. Places the transmission line in a nonbreak | 108 // Terminates the BREAK signal. Places the transmission line in a nonbreak |
110 // state. | 109 // state. |
111 virtual bool ClearBreak() = 0; | 110 virtual bool ClearBreak() = 0; |
112 | 111 |
113 protected: | 112 protected: |
114 explicit SerialIoHandler( | 113 explicit SerialIoHandler( |
115 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
116 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | 114 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
117 virtual ~SerialIoHandler(); | 115 virtual ~SerialIoHandler(); |
118 | 116 |
119 // Performs a platform-specific read operation. This must guarantee that | 117 // Performs a platform-specific read operation. This must guarantee that |
120 // ReadCompleted is called when the underlying async operation is completed | 118 // ReadCompleted is called when the underlying async operation is completed |
121 // or the SerialIoHandler instance will leak. | 119 // or the SerialIoHandler instance will leak. |
122 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. | 120 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. |
123 // Use QueueReadCompleted instead to avoid reentrancy. | 121 // Use QueueReadCompleted instead to avoid reentrancy. |
124 virtual void ReadImpl() = 0; | 122 virtual void ReadImpl() = 0; |
125 | 123 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 186 |
189 serial::SendError write_cancel_reason() const { return write_cancel_reason_; } | 187 serial::SendError write_cancel_reason() const { return write_cancel_reason_; } |
190 | 188 |
191 bool write_canceled() const { return write_canceled_; } | 189 bool write_canceled() const { return write_canceled_; } |
192 | 190 |
193 const serial::ConnectionOptions& options() const { return options_; } | 191 const serial::ConnectionOptions& options() const { return options_; } |
194 | 192 |
195 // Possibly fixes up a serial port path name in a platform-specific manner. | 193 // Possibly fixes up a serial port path name in a platform-specific manner. |
196 static std::string MaybeFixUpPortName(const std::string& port_name); | 194 static std::string MaybeFixUpPortName(const std::string& port_name); |
197 | 195 |
198 base::SingleThreadTaskRunner* file_thread_task_runner() const { | |
199 return file_thread_task_runner_.get(); | |
200 } | |
201 | |
202 base::SingleThreadTaskRunner* ui_thread_task_runner() const { | 196 base::SingleThreadTaskRunner* ui_thread_task_runner() const { |
203 return ui_thread_task_runner_.get(); | 197 return ui_thread_task_runner_.get(); |
204 } | 198 } |
205 | 199 |
206 const std::string& port() const { return port_; } | 200 const std::string& port() const { return port_; } |
207 | 201 |
208 SEQUENCE_CHECKER(sequence_checker_); | 202 SEQUENCE_CHECKER(sequence_checker_); |
209 | 203 |
210 private: | 204 private: |
211 friend class base::RefCounted<SerialIoHandler>; | 205 friend class base::RefCounted<SerialIoHandler>; |
212 | 206 |
213 void MergeConnectionOptions(const serial::ConnectionOptions& options); | 207 void MergeConnectionOptions(const serial::ConnectionOptions& options); |
214 | 208 |
215 // Continues an Open operation on the FILE thread. | 209 // Continues an Open operation on the FILE thread. |
216 void StartOpen(const std::string& port, | 210 void StartOpen(const std::string& port, |
217 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | 211 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
218 | 212 |
219 // Finalizes an Open operation (continued from StartOpen) on the IO thread. | 213 // Finalizes an Open operation (continued from StartOpen) on the IO thread. |
220 void FinishOpen(base::File file); | 214 void FinishOpen(base::File file); |
221 | 215 |
222 void Close(); | |
223 | |
224 // Continues a Close operation on the FILE thread. | 216 // Continues a Close operation on the FILE thread. |
225 static void DoClose(base::File port); | 217 static void DoClose(base::File port); |
226 | 218 |
227 // File for the opened serial device. This value is only modified from the IO | 219 // File for the opened serial device. This value is only modified from the IO |
228 // thread. | 220 // thread. |
229 base::File file_; | 221 base::File file_; |
230 | 222 |
231 // Currently applied connection options. | 223 // Currently applied connection options. |
232 serial::ConnectionOptions options_; | 224 serial::ConnectionOptions options_; |
233 | 225 |
234 std::unique_ptr<WritableBuffer> pending_read_buffer_; | 226 std::unique_ptr<WritableBuffer> pending_read_buffer_; |
235 serial::ReceiveError read_cancel_reason_; | 227 serial::ReceiveError read_cancel_reason_; |
236 bool read_canceled_; | 228 bool read_canceled_; |
237 | 229 |
238 std::unique_ptr<ReadOnlyBuffer> pending_write_buffer_; | 230 std::unique_ptr<ReadOnlyBuffer> pending_write_buffer_; |
239 serial::SendError write_cancel_reason_; | 231 serial::SendError write_cancel_reason_; |
240 bool write_canceled_; | 232 bool write_canceled_; |
241 | 233 |
242 // Callback to handle the completion of a pending Open() request. | 234 // Callback to handle the completion of a pending Open() request. |
243 OpenCompleteCallback open_complete_; | 235 OpenCompleteCallback open_complete_; |
244 | 236 |
245 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; | |
246 // On Chrome OS, PermissionBrokerClient should be called on the UI thread. | 237 // On Chrome OS, PermissionBrokerClient should be called on the UI thread. |
247 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | 238 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
248 | 239 |
249 std::string port_; | 240 std::string port_; |
250 | 241 |
251 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); | 242 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); |
252 }; | 243 }; |
253 | 244 |
254 } // namespace device | 245 } // namespace device |
255 | 246 |
256 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 247 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
OLD | NEW |