API
ttyIOUtils.hpp
Go to the documentation of this file.
1 /** \file ttyIOUtils.hpp
2  * \brief Utilities for i/o on a file descriptor pointing to a tty device.
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * \ingroup tty_files
6  * History:
7  * - 2018-01-15 created by JRM, starting with code imported from VisAO
8  */
9 
10 #ifndef tty_ttyIOUtils_hpp
11 #define tty_ttyIOUtils_hpp
12 
13 #include <string>
14 #include <vector>
15 
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <poll.h>
19 #include <termios.h>
20 
21 
22 #ifndef TTY_BUFFSIZE
23  #define TTY_BUFFSIZE (1024)
24 #endif
25 
26 namespace MagAOX
27 {
28 namespace tty
29 {
30 
31 /// Replace lone \\r and \\n with \\r\\n for telnet-ness.
32 /** Do it all at once instead of during char-by-char transmission
33  * cuz some devices (I'm looking at you SDG) get impatient and
34  * stop paying attention.
35  *
36  * \returns 0 on success
37  * \returns -1 on error (nothing yet)
38  *
39  */
40 int telnetCRLF( std::string & telnetStr, ///< [out] the string with \\r an \\n converted to \\r\\n
41  const std::string & inputStr ///< [in] the string to be converted
42  );
43 
44 /// Open a file as a raw-mode tty device
45 /**
46  * \returns TTY_E_NOERROR on success.
47  * \returns TTY_E_TCGETATTR on a error from tcgetattr.
48  * \returns TTY_E_TCSETATTR on an error from tcsetattr.
49  * \returns TTY_E_SETISPEED on a cfsetispeed error.
50  * \returns TTY_E_SETOSPEED on a cfsetospeed error.
51  *
52  * \ingroup tty
53  */
54 int ttyOpenRaw( int & fileDescrip, ///< [out] the file descriptor. Set to 0 on an error.
55  std::string & deviceName, ///< [in] the device path name, e.g. /dev/ttyUSB0
56  speed_t speed ///< [in] indicates the baud rate (see http://pubs.opengroup.org/onlinepubs/7908799/xsh/termios.h.html)
57  );
58 
59 /// Check if the end of the buffer contains the end-of-transmission string
60 /**
61  * \returns true if the last N chars of buffRead are equal to eot, where N is the length of eot.
62  * \returns false otherwise.
63  *
64  * \ingroup tty
65  */
66 bool isEndOfTrans( const std::string & strRead, ///< [in] The read buffer to check
67  const std::string & eot ///< [in] The end-of-transmission string
68  );
69 
70 /// Write to the tty console indicated by a file descriptor.
71 /**
72  *
73  * \returns TTY_E_NOERROR on success
74  * \returns TTY_E_TIMEOUTONWRITEPOLL if the poll times out.
75  * \returns TTY_E_ERRORONWRITEPOLL if an error is returned by poll.
76  * \returns TTY_E_TIMEOUTONWRITE if a timeout occurs during the write.
77  * \returns TTY_E_ERRORONWRITE if an error occurs writing to the file.
78  *
79  * \ingroup tty
80  */
81 int ttyWrite( const std::string & buffWrite, ///< [in] The characters to write to the tty.
82  int fd, ///< [in] The file descriptor of the open tty.
83  int timeoutWrite ///< [in] The timeout in milliseconds.
84  );
85 
86 /// Read from a tty console indicated by a file-descriptor, up to a given number of bytes.
87 /** Polls before attempting to read, but does not wait for all bytes to be ready.
88  *
89  * \returns TTY_E_NOERROR on success
90  * \returns TTY_E_TIMEOUTONREADPOLL if the poll times out.
91  * \returns TTY_E_ERRORONREADPOLL if an error is returned by poll.
92  * \returns TTY_E_ERRORONREAD if an error occurs reading from the file.
93  *
94  * \ingroup tty
95  */
96 int ttyReadRaw( std::vector<unsigned char> & vecRead, ///< [out] The buffer in which to store the output.
97  int & readBytes, ///< [out] The number of bytes read.
98  int fd, ///< [in] The file descriptor of the open tty.
99  int timeoutRead ///< [in] The timeout in milliseconds.
100  );
101 
102 /// Read from a tty console indicated by a file-descriptor, until a given number of bytes are read.
103 /**
104  * \returns TTY_E_NOERROR on success
105  * \returns TTY_E_TIMEOUTONREADPOLL if the poll times out.
106  * \returns TTY_E_ERRORONREADPOLL if an error is returned by poll.
107  * \returns TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
108  * \returns TTY_E_ERRORONREAD if an error occurs reading from the file.
109  *
110  * \ingroup tty
111  */
112 int ttyRead( std::string & strRead, ///< [out] The string in which to store the output.
113  int bytes, ///< [in] the number of bytes to read
114  int fd, ///< [in] The file descriptor of the open tty.
115  int timeoutRead ///< [in] The timeout in milliseconds.
116  );
117 
118 /// Read from a tty console indicated by a file-descriptor, until an end of transmission string is read.
119 /**
120  * \returns TTY_E_NOERROR on success
121  * \returns TTY_E_TIMEOUTONREADPOLL if the poll times out.
122  * \returns TTY_E_ERRORONREADPOLL if an error is returned by poll.
123  * \returns TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
124  * \returns TTY_E_ERRORONREAD if an error occurs reading from the file.
125  *
126  * \ingroup tty
127  */
128 int ttyRead( std::string & strRead, ///< [out] The string in which to store the output.
129  const std::string & eot, ///< [in] A sequence of characters which indicates the end of transmission.
130  int fd, ///< [in] The file descriptor of the open tty.
131  int timeoutRead ///< [in] The timeout in milliseconds.
132  );
133 
134 /// Write to a tty on an open file descriptor, then get the result.
135 /** The read is conducted until an end-of-transmission string is received.
136  * Echo characters are swallowed if desired.
137  *
138  * \returns TTY_E_NOERROR on success
139  * \returns TTY_E_TIMEOUTONWRITEPOLL if the poll times out.
140  * \returns TTY_E_ERRORONWRITEPOLL if an error is returned by poll.
141  * \returns TTY_E_TIMEOUTONWRITE if a timeout occurs during the write.
142  * \returns TTY_E_ERRORONWRITE if an error occurs writing to the file.
143  * \returns TTY_E_TIMEOUTONREADPOLL if the poll times out.
144  * \returns TTY_E_ERRORONREADPOLL if an error is returned by poll.
145  * \returns TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
146  * \returns TTY_E_ERRORONREAD if an error occurs reading from the file.
147  *
148  * \ingroup tty
149  */
150 int ttyWriteRead( std::string & strRead, ///< [out] The string in which to store the output.
151  const std::string & strWrite, ///< [in] The characters to write to the tty.
152  const std::string & eot, ///< [in] A sequence of characters which indicates the end of transmission.
153  bool swallowEcho, ///< [in] If true, strWrite.size() characters are read after the write
154  int fd, ///< [in] The file descriptor of the open tty.
155  int timeoutWrite, ///< [in] The write timeout in milliseconds.
156  int timeoutRead ///< [in] The read timeout in milliseconds.
157  );
158 
159 } //namespace tty
160 } //namespace MagAOX
161 
162 #endif //tty_ttyIOUtils_hpp
int ttyReadRaw(std::vector< unsigned char > &vecRead, int &readBytes, int fd, int timeoutRead)
Read from a tty console indicated by a file-descriptor, up to a given number of bytes.
Definition: ttyIOUtils.cpp:172
int ttyWriteRead(std::string &strRead, const std::string &strWrite, const std::string &eot, bool swallowEcho, int fd, int timeoutWrite, int timeoutRead)
Write to a tty on an open file descriptor, then get the result.
Definition: ttyIOUtils.cpp:332
int ttyWrite(const std::string &buffWrite, int fd, int timeoutWrite)
Write to the tty console indicated by a file descriptor.
Definition: ttyIOUtils.cpp:132
int ttyRead(std::string &strRead, int bytes, int fd, int timeoutRead)
Read from a tty console indicated by a file-descriptor, until a given number of bytes are read.
Definition: ttyIOUtils.cpp:206
int ttyOpenRaw(int &fileDescrip, std::string &deviceName, speed_t speed)
Open a file as a raw-mode tty device.
Definition: ttyIOUtils.cpp:72
bool isEndOfTrans(const std::string &strRead, const std::string &eot)
Check if the end of the buffer contains the end-of-transmission string.
Definition: ttyIOUtils.cpp:116
int telnetCRLF(std::string &telnetStr, const std::string &inputStr)
Replace lone \r and \n with \r\n for telnet-ness.
Definition: ttyIOUtils.cpp:26
Definition: dm.hpp:24