15 #include <mx/sys/timeUtils.hpp>
27 const std::string & inputStr
30 telnetStr.resize(inputStr.size());
32 size_t N = inputStr.size();
34 for(
size_t i=0;i<N; ++i)
36 if(inputStr[i] !=
'\r' && inputStr[i] !=
'\n')
38 telnetStr[j] = inputStr[i];
40 else if(inputStr[i] ==
'\r')
46 if(inputStr[i+1] ==
'\n')
55 telnetStr.push_back(
' ');
59 else if(inputStr[i] ==
'\n')
62 telnetStr.push_back(
' ');
73 std::string & deviceName,
79 fileDescrip = ::open( deviceName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
82 struct termios termopt;
83 if( tcgetattr(fileDescrip, &termopt) < 0 )
90 if( cfsetispeed(&termopt, speed) < 0 )
97 if( cfsetospeed(&termopt, speed) < 0 )
106 if( tcsetattr(fileDescrip, TCSANOW, &termopt) < 0 )
117 const std::string & eot
121 if(eot.size() > strRead.size())
return false;
124 for(
size_t i=0; i < eot.size(); ++i)
126 if( strRead[strRead.size()-1-i] != eot[eot.size()-1-i] )
return false;
142 pfd.events = POLLOUT;
144 t0 = mx::sys::get_curr_time();
146 size_t totWritten = 0;
147 while( totWritten < buffWrite.size())
149 int timeoutCurrent = timeoutWrite - (mx::sys::get_curr_time()-t0)*1000;
152 int rv = poll( &pfd, 1, timeoutCurrent);
156 rv = write(fd, buffWrite.c_str()+totWritten, buffWrite.size()-totWritten);
161 std::cerr <<
"Wrote " << rv <<
" chars of " << buffWrite.size() <<
"\n";
188 rv = poll( &pfd, 1, timeoutRead);
194 rv = read(fd, vecRead.data(), vecRead.size());
227 t0 = mx::sys::get_curr_time();
228 timeoutCurrent = timeoutRead;
233 rv = poll( &pfd, 1, timeoutCurrent);
240 strRead.append( buffRead, rv);
244 while( totBytes < bytes )
246 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
249 rv = poll( &pfd, 1, timeoutCurrent);
257 strRead.append( buffRead, rv);
261 std::cerr <<
"ttyRead: read " << rv <<
" bytes. buffRead=" << buffRead <<
"\n";
272 const std::string & eot,
292 t0 = mx::sys::get_curr_time();
293 timeoutCurrent = timeoutRead;
298 rv = poll( &pfd, 1, timeoutCurrent);
305 strRead.append( buffRead, rv);
309 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
312 rv = poll( &pfd, 1, timeoutCurrent);
320 strRead.append( buffRead, rv);
322 std::cerr <<
"ttyRead: read " << rv <<
" bytes. buffRead=" << buffRead <<
"\n";
333 const std::string & strWrite,
334 const std::string & eot,
346 rv =
ttyWrite( strWrite, fd, timeoutWrite);
357 t0 = mx::sys::get_curr_time();;
369 while( totrv <= strWrite.size() )
371 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
374 rv = poll( &pfd, 1, timeoutCurrent);
385 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
389 return ttyRead(strRead, eot, fd, timeoutCurrent);
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.
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.
int ttyWrite(const std::string &buffWrite, int fd, int timeoutWrite)
Write to the tty console indicated by a file descriptor.
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.
int ttyOpenRaw(int &fileDescrip, std::string &deviceName, speed_t speed)
Open a file as a raw-mode tty device.
bool isEndOfTrans(const std::string &strRead, const std::string &eot)
Check if the end of the buffer contains the end-of-transmission string.
int telnetCRLF(std::string &telnetStr, const std::string &inputStr)
Replace lone \r and \n with \r\n for telnet-ness.
Error numbers for the tty utilities.
#define TTY_E_TIMEOUTONREAD
#define TTY_E_TIMEOUTONREADPOLL
#define TTY_E_ERRORONWRITE
#define TTY_E_ERRORONWRITEPOLL
#define TTY_E_ERRORONREADPOLL
#define TTY_E_ERRORONREAD
#define TTY_E_TIMEOUTONWRITEPOLL
#define TTY_E_TIMEOUTONWRITE
Utilities for i/o on a file descriptor pointing to a tty device.