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/serial.mojom.h" | 13 #include "device/serial/serial.mojom.h" |
14 #include "net/base/io_buffer.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 | 28 |
28 typedef base::Callback<void(bool success)> OpenCompleteCallback; | 29 typedef base::Callback<void(bool success)> OpenCompleteCallback; |
29 | 30 |
30 // Called with a string of bytes read, and a result code. Note that an error | 31 // Called with a string of bytes read, and a result code. Note that an error |
31 // result does not necessarily imply 0 bytes read. | 32 // result does not necessarily imply 0 bytes read. |
32 typedef base::Callback<void(const std::string& data, | 33 typedef base::Callback<void(const std::string& data, |
33 serial::ReceiveError error)> ReadCompleteCallback; | 34 serial::ReceiveError error)> ReadCompleteCallback; |
34 | 35 |
35 // Called with the number of bytes written and a result code. Note that an | 36 // Called with the number of bytes written and a result code. Note that an |
36 // error result does not necessarily imply 0 bytes written. | 37 // error result does not necessarily imply 0 bytes written. |
37 typedef base::Callback<void(int bytes_written, serial::SendError error)> | 38 typedef base::Callback<void(int bytes_written, serial::SendError error)> |
38 WriteCompleteCallback; | 39 WriteCompleteCallback; |
39 | 40 |
40 // Initializes the handler on the current message loop. Must be called exactly | 41 // Initializes the handler on the current message loop. Must be called exactly |
41 // once before performing any I/O through the handler. | 42 // once before performing any I/O through the handler. |
42 virtual void Initialize( | 43 virtual void Initialize(const ReadCompleteCallback& read_callback, |
43 const ReadCompleteCallback& read_callback, | 44 const WriteCompleteCallback& write_callback); |
44 const WriteCompleteCallback& write_callback, | |
45 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop); | |
46 | 45 |
47 // Initiates an asynchronous Open of the device. | 46 // Initiates an asynchronous Open of the device. |
48 virtual void Open(const std::string& port, | 47 virtual void Open(const std::string& port, |
49 const OpenCompleteCallback& callback); | 48 const OpenCompleteCallback& callback); |
50 | 49 |
51 // Performs an async Read operation. Behavior is undefined if this is called | 50 // Performs an async Read operation. Behavior is undefined if this is called |
52 // while a Read is already pending. Otherwise, the ReadCompleteCallback | 51 // while a Read is already pending. Otherwise, the ReadCompleteCallback |
53 // (see above) will eventually be called with a result. | 52 // (see above) will eventually be called with a result. |
54 void Read(int max_bytes); | 53 void Read(int max_bytes); |
55 | 54 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // Performs platform-specific port configuration. Returns |true| iff | 86 // Performs platform-specific port configuration. Returns |true| iff |
88 // configuration was successful. | 87 // configuration was successful. |
89 virtual bool ConfigurePort(const serial::ConnectionOptions& options) = 0; | 88 virtual bool ConfigurePort(const serial::ConnectionOptions& options) = 0; |
90 | 89 |
91 // Performs a platform-specific port configuration query. Fills values in an | 90 // Performs a platform-specific port configuration query. Fills values in an |
92 // existing ConnectionInfo. Returns |true| iff port configuration was | 91 // existing ConnectionInfo. Returns |true| iff port configuration was |
93 // successfully retrieved. | 92 // successfully retrieved. |
94 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0; | 93 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0; |
95 | 94 |
96 protected: | 95 protected: |
97 SerialIoHandler(); | 96 explicit SerialIoHandler( |
| 97 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop); |
98 virtual ~SerialIoHandler(); | 98 virtual ~SerialIoHandler(); |
99 | 99 |
100 // Performs a platform-specific read operation. This must guarantee that | 100 // Performs a platform-specific read operation. This must guarantee that |
101 // ReadCompleted is called when the underlying async operation is completed | 101 // ReadCompleted is called when the underlying async operation is completed |
102 // or the SerialIoHandler instance will leak. | 102 // or the SerialIoHandler instance will leak. |
103 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. | 103 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. |
104 // Use QueueReadCompleted instead to avoid reentrancy. | 104 // Use QueueReadCompleted instead to avoid reentrancy. |
105 virtual void ReadImpl() = 0; | 105 virtual void ReadImpl() = 0; |
106 | 106 |
107 // Performs a platform-specific write operation. This must guarantee that | 107 // Performs a platform-specific write operation. This must guarantee that |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 OpenCompleteCallback open_complete_; | 203 OpenCompleteCallback open_complete_; |
204 | 204 |
205 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; | 205 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; |
206 | 206 |
207 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); | 207 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); |
208 }; | 208 }; |
209 | 209 |
210 } // namespace device | 210 } // namespace device |
211 | 211 |
212 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 212 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
OLD | NEW |