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 "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
| 13 #include "device/serial/buffer.h" |
13 #include "device/serial/serial.mojom.h" | 14 #include "device/serial/serial.mojom.h" |
14 #include "net/base/io_buffer.h" | |
15 | 15 |
16 namespace device { | 16 namespace device { |
17 | 17 |
18 // Provides a simplified interface for performing asynchronous I/O on serial | 18 // Provides a simplified interface for performing asynchronous I/O on serial |
19 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O | 19 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O |
20 // operations hold a reference to this object until completion so that memory | 20 // operations hold a reference to this object until completion so that memory |
21 // doesn't disappear out from under the OS. | 21 // doesn't disappear out from under the OS. |
22 class SerialIoHandler : public base::NonThreadSafe, | 22 class SerialIoHandler : public base::NonThreadSafe, |
23 public base::RefCounted<SerialIoHandler> { | 23 public base::RefCounted<SerialIoHandler> { |
24 public: | 24 public: |
25 // Constructs an instance of some platform-specific subclass. | 25 // Constructs an instance of some platform-specific subclass. |
26 static scoped_refptr<SerialIoHandler> Create( | 26 static scoped_refptr<SerialIoHandler> Create( |
27 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop); | 27 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop); |
28 | 28 |
29 typedef base::Callback<void(bool success)> OpenCompleteCallback; | 29 typedef base::Callback<void(bool success)> OpenCompleteCallback; |
30 | 30 |
31 // Called with a string of bytes read, and a result code. Note that an error | |
32 // result does not necessarily imply 0 bytes read. | |
33 typedef base::Callback<void(const std::string& data, | |
34 serial::ReceiveError error)> ReadCompleteCallback; | |
35 | |
36 // Called with the number of bytes written and a result code. Note that an | |
37 // error result does not necessarily imply 0 bytes written. | |
38 typedef base::Callback<void(int bytes_written, serial::SendError error)> | |
39 WriteCompleteCallback; | |
40 | |
41 // Initializes the handler on the current message loop. Must be called exactly | |
42 // once before performing any I/O through the handler. | |
43 virtual void Initialize(const ReadCompleteCallback& read_callback, | |
44 const WriteCompleteCallback& write_callback); | |
45 | |
46 // Initiates an asynchronous Open of the device. | 31 // Initiates an asynchronous Open of the device. |
47 virtual void Open(const std::string& port, | 32 virtual void Open(const std::string& port, |
48 const OpenCompleteCallback& callback); | 33 const OpenCompleteCallback& callback); |
49 | 34 |
50 // Performs an async Read operation. Behavior is undefined if this is called | 35 // Performs an async Read operation. Behavior is undefined if this is called |
51 // while a Read is already pending. Otherwise, the ReadCompleteCallback | 36 // while a Read is already pending. Otherwise, the Done or DoneWithError |
52 // (see above) will eventually be called with a result. | 37 // method on |buffer| will eventually be called with a result. |
53 void Read(int max_bytes); | 38 void Read(scoped_ptr<WritableBuffer> buffer); |
54 | 39 |
55 // Performs an async Write operation. Behavior is undefined if this is called | 40 // Performs an async Write operation. Behavior is undefined if this is called |
56 // while a Write is already pending. Otherwise, the WriteCompleteCallback | 41 // while a Write is already pending. Otherwise, the Done or DoneWithError |
57 // (see above) will eventually be called with a result. | 42 // method on |buffer| will eventually be called with a result. |
58 void Write(const std::string& data); | 43 void Write(scoped_ptr<ReadOnlyBuffer> buffer); |
59 | 44 |
60 // Indicates whether or not a read is currently pending. | 45 // Indicates whether or not a read is currently pending. |
61 bool IsReadPending() const; | 46 bool IsReadPending() const; |
62 | 47 |
63 // Indicates whether or not a write is currently pending. | 48 // Indicates whether or not a write is currently pending. |
64 bool IsWritePending() const; | 49 bool IsWritePending() const; |
65 | 50 |
66 // Attempts to cancel a pending read operation. | 51 // Attempts to cancel a pending read operation. |
67 void CancelRead(serial::ReceiveError reason); | 52 void CancelRead(serial::ReceiveError reason); |
68 | 53 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 // without being reentrant. | 120 // without being reentrant. |
136 void QueueReadCompleted(int bytes_read, serial::ReceiveError error); | 121 void QueueReadCompleted(int bytes_read, serial::ReceiveError error); |
137 | 122 |
138 // Queues a WriteCompleted call on the current thread. This is used to allow | 123 // Queues a WriteCompleted call on the current thread. This is used to allow |
139 // WriteImpl to immediately signal completion with 0 bytes and an error, | 124 // WriteImpl to immediately signal completion with 0 bytes and an error, |
140 // without being reentrant. | 125 // without being reentrant. |
141 void QueueWriteCompleted(int bytes_written, serial::SendError error); | 126 void QueueWriteCompleted(int bytes_written, serial::SendError error); |
142 | 127 |
143 const base::File& file() const { return file_; } | 128 const base::File& file() const { return file_; } |
144 | 129 |
145 net::IOBuffer* pending_read_buffer() const { | 130 char* pending_read_buffer() const { |
146 return pending_read_buffer_.get(); | 131 return pending_read_buffer_ ? pending_read_buffer_->GetData() : NULL; |
147 } | 132 } |
148 | 133 |
149 int pending_read_buffer_len() const { return pending_read_buffer_len_; } | 134 uint32_t pending_read_buffer_len() const { |
| 135 return pending_read_buffer_ ? pending_read_buffer_->GetSize() : 0; |
| 136 } |
150 | 137 |
151 serial::ReceiveError read_cancel_reason() const { | 138 serial::ReceiveError read_cancel_reason() const { |
152 return read_cancel_reason_; | 139 return read_cancel_reason_; |
153 } | 140 } |
154 | 141 |
155 bool read_canceled() const { return read_canceled_; } | 142 bool read_canceled() const { return read_canceled_; } |
156 | 143 |
157 net::IOBuffer* pending_write_buffer() const { | 144 const char* pending_write_buffer() const { |
158 return pending_write_buffer_.get(); | 145 return pending_write_buffer_ ? pending_write_buffer_->GetData() : NULL; |
159 } | 146 } |
160 | 147 |
161 int pending_write_buffer_len() const { return pending_write_buffer_len_; } | 148 uint32_t pending_write_buffer_len() const { |
| 149 return pending_write_buffer_ ? pending_write_buffer_->GetSize() : 0; |
| 150 } |
162 | 151 |
163 serial::SendError write_cancel_reason() const { return write_cancel_reason_; } | 152 serial::SendError write_cancel_reason() const { return write_cancel_reason_; } |
164 | 153 |
165 bool write_canceled() const { return write_canceled_; } | 154 bool write_canceled() const { return write_canceled_; } |
166 | 155 |
167 // Possibly fixes up a serial port path name in a platform-specific manner. | 156 // Possibly fixes up a serial port path name in a platform-specific manner. |
168 static std::string MaybeFixUpPortName(const std::string& port_name); | 157 static std::string MaybeFixUpPortName(const std::string& port_name); |
169 | 158 |
170 private: | 159 private: |
171 friend class base::RefCounted<SerialIoHandler>; | 160 friend class base::RefCounted<SerialIoHandler>; |
172 | 161 |
173 // Continues an Open operation on the FILE thread. | 162 // Continues an Open operation on the FILE thread. |
174 void StartOpen(const std::string& port, | 163 void StartOpen(const std::string& port, |
175 scoped_refptr<base::MessageLoopProxy> io_message_loop); | 164 scoped_refptr<base::MessageLoopProxy> io_message_loop); |
176 | 165 |
177 // Finalizes an Open operation (continued from StartOpen) on the IO thread. | 166 // Finalizes an Open operation (continued from StartOpen) on the IO thread. |
178 void FinishOpen(base::File file); | 167 void FinishOpen(base::File file); |
179 | 168 |
180 void Close(); | 169 void Close(); |
181 | 170 |
182 // Continues a Close operation on the FILE thread. | 171 // Continues a Close operation on the FILE thread. |
183 static void DoClose(base::File port); | 172 static void DoClose(base::File port); |
184 | 173 |
185 // File for the opened serial device. This value is only modified from the IO | 174 // File for the opened serial device. This value is only modified from the IO |
186 // thread. | 175 // thread. |
187 base::File file_; | 176 base::File file_; |
188 | 177 |
189 scoped_refptr<net::IOBuffer> pending_read_buffer_; | 178 scoped_ptr<WritableBuffer> pending_read_buffer_; |
190 int pending_read_buffer_len_; | |
191 serial::ReceiveError read_cancel_reason_; | 179 serial::ReceiveError read_cancel_reason_; |
192 bool read_canceled_; | 180 bool read_canceled_; |
193 | 181 |
194 scoped_refptr<net::IOBuffer> pending_write_buffer_; | 182 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_; |
195 int pending_write_buffer_len_; | |
196 serial::SendError write_cancel_reason_; | 183 serial::SendError write_cancel_reason_; |
197 bool write_canceled_; | 184 bool write_canceled_; |
198 | 185 |
199 ReadCompleteCallback read_complete_; | |
200 WriteCompleteCallback write_complete_; | |
201 | |
202 // Callback to handle the completion of a pending Open() request. | 186 // Callback to handle the completion of a pending Open() request. |
203 OpenCompleteCallback open_complete_; | 187 OpenCompleteCallback open_complete_; |
204 | 188 |
205 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; | 189 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; |
206 | 190 |
207 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); | 191 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); |
208 }; | 192 }; |
209 | 193 |
210 } // namespace device | 194 } // namespace device |
211 | 195 |
212 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 196 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
OLD | NEW |