// $Id: RlinkPortTerm.cpp 516 2013-05-05 21:24:52Z mueller $
|
// $Id: RlinkPortTerm.cpp 516 2013-05-05 21:24:52Z mueller $
|
//
|
//
|
// Copyright 2011-2013 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
// Copyright 2011-2013 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
//
|
//
|
// This program is free software; you may redistribute and/or modify it under
|
// This program is free software; you may redistribute and/or modify it under
|
// the terms of the GNU General Public License as published by the Free
|
// the terms of the GNU General Public License as published by the Free
|
// Software Foundation, either version 2, or at your option any later version.
|
// Software Foundation, either version 2, or at your option any later version.
|
//
|
//
|
// This program is distributed in the hope that it will be useful, but
|
// This program is distributed in the hope that it will be useful, but
|
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for complete details.
|
// for complete details.
|
//
|
//
|
// Revision History:
|
// Revision History:
|
// Date Rev Version Comment
|
// Date Rev Version Comment
|
// 2013-02-23 492 1.1 use RparseUrl
|
// 2013-02-23 492 1.1 use RparseUrl
|
// 2011-12-18 440 1.0.4 add kStatNPort stats; Open(): autoadd /dev/tty,
|
// 2011-12-18 440 1.0.4 add kStatNPort stats; Open(): autoadd /dev/tty,
|
// BUGFIX: Open(): set VSTART, VSTOP
|
// BUGFIX: Open(): set VSTART, VSTOP
|
// 2011-12-11 438 1.0.3 Read(),Write(): added for xon handling, tcdrain();
|
// 2011-12-11 438 1.0.3 Read(),Write(): added for xon handling, tcdrain();
|
// Open(): add more baud rates, support xon attribute
|
// Open(): add more baud rates, support xon attribute
|
// 2011-12-04 435 1.0.2 Open(): add cts attr, hw flow control now optional
|
// 2011-12-04 435 1.0.2 Open(): add cts attr, hw flow control now optional
|
// 2011-07-04 388 1.0.1 add termios readback and verification
|
// 2011-07-04 388 1.0.1 add termios readback and verification
|
// 2011-03-27 374 1.0 Initial version
|
// 2011-03-27 374 1.0 Initial version
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
|
|
/*!
|
/*!
|
\file
|
\file
|
\version $Id: RlinkPortTerm.cpp 516 2013-05-05 21:24:52Z mueller $
|
\version $Id: RlinkPortTerm.cpp 516 2013-05-05 21:24:52Z mueller $
|
\brief Implemenation of RlinkPortTerm.
|
\brief Implemenation of RlinkPortTerm.
|
*/
|
*/
|
|
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <sys/stat.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <errno.h>
|
#include <errno.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <termios.h>
|
#include <termios.h>
|
|
|
#include "RlinkPortTerm.hpp"
|
#include "RlinkPortTerm.hpp"
|
|
|
#include "librtools/RosFill.hpp"
|
#include "librtools/RosFill.hpp"
|
#include "librtools/RosPrintf.hpp"
|
#include "librtools/RosPrintf.hpp"
|
|
|
using namespace std;
|
using namespace std;
|
|
|
/*!
|
/*!
|
\class Retro::RlinkPortTerm
|
\class Retro::RlinkPortTerm
|
\brief FIXME_docs
|
\brief FIXME_docs
|
*/
|
*/
|
|
|
// all method definitions in namespace Retro
|
// all method definitions in namespace Retro
|
namespace Retro {
|
namespace Retro {
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
// constants definitions
|
// constants definitions
|
const uint8_t RlinkPortTerm::kc_xon;
|
const uint8_t RlinkPortTerm::kc_xon;
|
const uint8_t RlinkPortTerm::kc_xoff;
|
const uint8_t RlinkPortTerm::kc_xoff;
|
const uint8_t RlinkPortTerm::kc_xesc;
|
const uint8_t RlinkPortTerm::kc_xesc;
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! Default constructor
|
//! Default constructor
|
|
|
RlinkPortTerm::RlinkPortTerm()
|
RlinkPortTerm::RlinkPortTerm()
|
: RlinkPort()
|
: RlinkPort()
|
{
|
{
|
fStats.Define(kStatNPortTxXesc, "NPortTxXesc", "Tx XESC escapes");
|
fStats.Define(kStatNPortTxXesc, "NPortTxXesc", "Tx XESC escapes");
|
fStats.Define(kStatNPortRxXesc, "NPortRxXesc", "Rx XESC escapes");
|
fStats.Define(kStatNPortRxXesc, "NPortRxXesc", "Rx XESC escapes");
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! Destructor
|
//! Destructor
|
|
|
RlinkPortTerm::~RlinkPortTerm()
|
RlinkPortTerm::~RlinkPortTerm()
|
{
|
{
|
RlinkPortTerm::Close();
|
RlinkPortTerm::Close();
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! FIXME_docs
|
//! FIXME_docs
|
|
|
bool RlinkPortTerm::Open(const std::string& url, RerrMsg& emsg)
|
bool RlinkPortTerm::Open(const std::string& url, RerrMsg& emsg)
|
{
|
{
|
Close();
|
Close();
|
|
|
if (!fUrl.Set(url, "|baud=|break|cts|xon|", emsg)) return false;
|
if (!fUrl.Set(url, "|baud=|break|cts|xon|", emsg)) return false;
|
|
|
// if path doesn't start with a '/' prepend a '/dev/tty'
|
// if path doesn't start with a '/' prepend a '/dev/tty'
|
if (fUrl.Path().substr(0,1) != "/") {
|
if (fUrl.Path().substr(0,1) != "/") {
|
fUrl.SetPath(string("/dev/tty" + fUrl.Path()));
|
fUrl.SetPath(string("/dev/tty" + fUrl.Path()));
|
}
|
}
|
|
|
speed_t speed = B115200;
|
speed_t speed = B115200;
|
string baud;
|
string baud;
|
if (fUrl.FindOpt("baud", baud)) {
|
if (fUrl.FindOpt("baud", baud)) {
|
speed = B0;
|
speed = B0;
|
if (baud=="2400") speed = B2400;
|
if (baud=="2400") speed = B2400;
|
if (baud=="4800") speed = B4800;
|
if (baud=="4800") speed = B4800;
|
if (baud=="9600") speed = B9600;
|
if (baud=="9600") speed = B9600;
|
if (baud=="19200" || baud=="19k") speed = B19200;
|
if (baud=="19200" || baud=="19k") speed = B19200;
|
if (baud=="38400" || baud=="38k") speed = B38400;
|
if (baud=="38400" || baud=="38k") speed = B38400;
|
if (baud=="57600" || baud=="57k") speed = B57600;
|
if (baud=="57600" || baud=="57k") speed = B57600;
|
if (baud=="115200" || baud=="115k") speed = B115200;
|
if (baud=="115200" || baud=="115k") speed = B115200;
|
if (baud=="230400" || baud=="230k") speed = B230400;
|
if (baud=="230400" || baud=="230k") speed = B230400;
|
if (baud=="460800" || baud=="460k") speed = B460800;
|
if (baud=="460800" || baud=="460k") speed = B460800;
|
if (baud=="500000" || baud=="500k") speed = B500000;
|
if (baud=="500000" || baud=="500k") speed = B500000;
|
if (baud=="921600" || baud=="921k") speed = B921600;
|
if (baud=="921600" || baud=="921k") speed = B921600;
|
if (baud=="1000000" || baud=="1000k" || baud=="1M") speed = B1000000;
|
if (baud=="1000000" || baud=="1000k" || baud=="1M") speed = B1000000;
|
if (baud=="1152000" || baud=="1152k") speed = B1152000;
|
if (baud=="1152000" || baud=="1152k") speed = B1152000;
|
if (baud=="1500000" || baud=="1500k") speed = B1500000;
|
if (baud=="1500000" || baud=="1500k") speed = B1500000;
|
if (baud=="2000000" || baud=="2000k" || baud=="2M") speed = B2000000;
|
if (baud=="2000000" || baud=="2000k" || baud=="2M") speed = B2000000;
|
if (baud=="2500000" || baud=="2500k") speed = B2500000;
|
if (baud=="2500000" || baud=="2500k") speed = B2500000;
|
if (baud=="3000000" || baud=="3000k" || baud=="3M") speed = B3000000;
|
if (baud=="3000000" || baud=="3000k" || baud=="3M") speed = B3000000;
|
if (baud=="3500000" || baud=="3500k") speed = B3500000;
|
if (baud=="3500000" || baud=="3500k") speed = B3500000;
|
if (baud=="4000000" || baud=="4000k" || baud=="4M") speed = B4000000;
|
if (baud=="4000000" || baud=="4000k" || baud=="4M") speed = B4000000;
|
if (speed == B0) {
|
if (speed == B0) {
|
emsg.Init("RlinkPortTerm::Open()",
|
emsg.Init("RlinkPortTerm::Open()",
|
string("invalid baud rate '") + baud + "' specified");
|
string("invalid baud rate '") + baud + "' specified");
|
return false;
|
return false;
|
}
|
}
|
}
|
}
|
|
|
int fd;
|
int fd;
|
|
|
fd = open(fUrl.Path().c_str(), O_RDWR|O_NOCTTY);
|
fd = open(fUrl.Path().c_str(), O_RDWR|O_NOCTTY);
|
if (fd < 0) {
|
if (fd < 0) {
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
string("open() for '") + fUrl.Path() + "' failed: ",
|
string("open() for '") + fUrl.Path() + "' failed: ",
|
errno);
|
errno);
|
return false;
|
return false;
|
}
|
}
|
|
|
if (!isatty(fd)) {
|
if (!isatty(fd)) {
|
emsg.Init("RlinkPortTerm::Open()",
|
emsg.Init("RlinkPortTerm::Open()",
|
string("isatty() check for '") + fUrl.Path() +
|
string("isatty() check for '") + fUrl.Path() +
|
"' failed: not a TTY");
|
"' failed: not a TTY");
|
close(fd);
|
close(fd);
|
return false;
|
return false;
|
}
|
}
|
|
|
if (tcgetattr(fd, &fTiosOld) != 0) {
|
if (tcgetattr(fd, &fTiosOld) != 0) {
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
string("tcgetattr() for '") + fUrl.Path() + "' failed: ",
|
string("tcgetattr() for '") + fUrl.Path() + "' failed: ",
|
errno);
|
errno);
|
close(fd);
|
close(fd);
|
return false;
|
return false;
|
}
|
}
|
|
|
bool use_cts = fUrl.FindOpt("cts");
|
bool use_cts = fUrl.FindOpt("cts");
|
bool use_xon = fUrl.FindOpt("xon");
|
bool use_xon = fUrl.FindOpt("xon");
|
fUseXon = use_xon;
|
fUseXon = use_xon;
|
fPendXesc = false;
|
fPendXesc = false;
|
|
|
fTiosNew = fTiosOld;
|
fTiosNew = fTiosOld;
|
|
|
fTiosNew.c_iflag = IGNBRK | // ignore breaks on input
|
fTiosNew.c_iflag = IGNBRK | // ignore breaks on input
|
IGNPAR; // ignore parity errors
|
IGNPAR; // ignore parity errors
|
if (use_xon) {
|
if (use_xon) {
|
fTiosNew.c_iflag |= IXON| // XON/XOFF flow control output
|
fTiosNew.c_iflag |= IXON| // XON/XOFF flow control output
|
IXOFF; // XON/XOFF flow control input
|
IXOFF; // XON/XOFF flow control input
|
}
|
}
|
|
|
fTiosNew.c_oflag = 0;
|
fTiosNew.c_oflag = 0;
|
|
|
fTiosNew.c_cflag = CS8 | // 8 bit chars
|
fTiosNew.c_cflag = CS8 | // 8 bit chars
|
CSTOPB | // 2 stop bits
|
CSTOPB | // 2 stop bits
|
CREAD | // enable receiver
|
CREAD | // enable receiver
|
CLOCAL; // ignore modem control
|
CLOCAL; // ignore modem control
|
if (use_cts) {
|
if (use_cts) {
|
fTiosNew.c_cflag |= CRTSCTS; // enable hardware flow control
|
fTiosNew.c_cflag |= CRTSCTS; // enable hardware flow control
|
}
|
}
|
|
|
fTiosNew.c_lflag = 0;
|
fTiosNew.c_lflag = 0;
|
|
|
if (cfsetspeed(&fTiosNew, speed) != 0) {
|
if (cfsetspeed(&fTiosNew, speed) != 0) {
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
string("cfsetspeed() for '") + baud + "' failed: ",
|
string("cfsetspeed() for '") + baud + "' failed: ",
|
errno);
|
errno);
|
close(fd);
|
close(fd);
|
return false;
|
return false;
|
}
|
}
|
|
|
fTiosNew.c_cc[VEOF] = 0; // undef
|
fTiosNew.c_cc[VEOF] = 0; // undef
|
fTiosNew.c_cc[VEOL] = 0; // undef
|
fTiosNew.c_cc[VEOL] = 0; // undef
|
fTiosNew.c_cc[VERASE] = 0; // undef
|
fTiosNew.c_cc[VERASE] = 0; // undef
|
fTiosNew.c_cc[VINTR] = 0; // undef
|
fTiosNew.c_cc[VINTR] = 0; // undef
|
fTiosNew.c_cc[VKILL] = 0; // undef
|
fTiosNew.c_cc[VKILL] = 0; // undef
|
fTiosNew.c_cc[VQUIT] = 0; // undef
|
fTiosNew.c_cc[VQUIT] = 0; // undef
|
fTiosNew.c_cc[VSUSP] = 0; // undef
|
fTiosNew.c_cc[VSUSP] = 0; // undef
|
fTiosNew.c_cc[VSTART] = 0; // undef
|
fTiosNew.c_cc[VSTART] = 0; // undef
|
fTiosNew.c_cc[VSTOP] = 0; // undef
|
fTiosNew.c_cc[VSTOP] = 0; // undef
|
fTiosNew.c_cc[VMIN] = 1; // wait for 1 char
|
fTiosNew.c_cc[VMIN] = 1; // wait for 1 char
|
fTiosNew.c_cc[VTIME] = 0; //
|
fTiosNew.c_cc[VTIME] = 0; //
|
if (use_xon) {
|
if (use_xon) {
|
fTiosNew.c_cc[VSTART] = kc_xon; // setup XON -> ^Q
|
fTiosNew.c_cc[VSTART] = kc_xon; // setup XON -> ^Q
|
fTiosNew.c_cc[VSTOP] = kc_xoff; // setup XOFF -> ^S
|
fTiosNew.c_cc[VSTOP] = kc_xoff; // setup XOFF -> ^S
|
}
|
}
|
|
|
if (tcsetattr(fd, TCSANOW, &fTiosNew) != 0) {
|
if (tcsetattr(fd, TCSANOW, &fTiosNew) != 0) {
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
string("tcsetattr() for '") + fUrl.Path() + "' failed: ",
|
string("tcsetattr() for '") + fUrl.Path() + "' failed: ",
|
errno);
|
errno);
|
close(fd);
|
close(fd);
|
return false;
|
return false;
|
}
|
}
|
|
|
// tcsetattr() returns success if any of the requested changes could be
|
// tcsetattr() returns success if any of the requested changes could be
|
// successfully carried out. Therefore the termios structure is read back
|
// successfully carried out. Therefore the termios structure is read back
|
// and verified.
|
// and verified.
|
|
|
struct termios tios;
|
struct termios tios;
|
if (tcgetattr(fd, &tios) != 0) {
|
if (tcgetattr(fd, &tios) != 0) {
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
string("2nd tcgetattr() for '") + fUrl.Path() +
|
string("2nd tcgetattr() for '") + fUrl.Path() +
|
"' failed: ", errno);
|
"' failed: ", errno);
|
close(fd);
|
close(fd);
|
return false;
|
return false;
|
}
|
}
|
|
|
const char* pmsg = 0;
|
const char* pmsg = 0;
|
if (tios.c_iflag != fTiosNew.c_iflag) pmsg = "c_iflag";
|
if (tios.c_iflag != fTiosNew.c_iflag) pmsg = "c_iflag";
|
if (tios.c_oflag != fTiosNew.c_oflag) pmsg = "c_oflag";
|
if (tios.c_oflag != fTiosNew.c_oflag) pmsg = "c_oflag";
|
if (tios.c_cflag != fTiosNew.c_cflag) pmsg = "c_cflag";
|
if (tios.c_cflag != fTiosNew.c_cflag) pmsg = "c_cflag";
|
if (tios.c_lflag != fTiosNew.c_lflag) pmsg = "c_lflag";
|
if (tios.c_lflag != fTiosNew.c_lflag) pmsg = "c_lflag";
|
if (cfgetispeed(&tios) != speed) pmsg = "ispeed";
|
if (cfgetispeed(&tios) != speed) pmsg = "ispeed";
|
if (cfgetospeed(&tios) != speed) pmsg = "ospeed";
|
if (cfgetospeed(&tios) != speed) pmsg = "ospeed";
|
for (int i=0; i<NCCS; i++) {
|
for (int i=0; i<NCCS; i++) {
|
if (tios.c_cc[i] != fTiosNew.c_cc[i]) pmsg = "c_cc char";
|
if (tios.c_cc[i] != fTiosNew.c_cc[i]) pmsg = "c_cc char";
|
}
|
}
|
|
|
if (pmsg) {
|
if (pmsg) {
|
emsg.Init("RlinkPortTerm::Open()",
|
emsg.Init("RlinkPortTerm::Open()",
|
string("tcsetattr() failed to set") + string(pmsg));
|
string("tcsetattr() failed to set") + string(pmsg));
|
close(fd);
|
close(fd);
|
return false;
|
return false;
|
}
|
}
|
|
|
fFdWrite = fd;
|
fFdWrite = fd;
|
fFdRead = fd;
|
fFdRead = fd;
|
fIsOpen = true;
|
fIsOpen = true;
|
|
|
if (fUrl.FindOpt("break")) {
|
if (fUrl.FindOpt("break")) {
|
if (tcsendbreak(fd, 0) != 0) {
|
if (tcsendbreak(fd, 0) != 0) {
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
emsg.InitErrno("RlinkPortTerm::Open()",
|
string("tcsendbreak() for '") + fUrl.Path() +
|
string("tcsendbreak() for '") + fUrl.Path() +
|
"' failed: ", errno);
|
"' failed: ", errno);
|
Close();
|
Close();
|
return false;
|
return false;
|
}
|
}
|
uint8_t buf[1];
|
uint8_t buf[1];
|
buf[0] = 0x80;
|
buf[0] = 0x80;
|
if (Write(buf, 1, emsg) != 1) {
|
if (Write(buf, 1, emsg) != 1) {
|
Close();
|
Close();
|
return false;
|
return false;
|
}
|
}
|
}
|
}
|
|
|
return true;
|
return true;
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! FIXME_docs
|
//! FIXME_docs
|
|
|
void RlinkPortTerm::Close()
|
void RlinkPortTerm::Close()
|
{
|
{
|
if (!IsOpen()) return;
|
if (!IsOpen()) return;
|
|
|
if (fFdWrite >= 0) {
|
if (fFdWrite >= 0) {
|
tcflush(fFdWrite, TCIOFLUSH);
|
tcflush(fFdWrite, TCIOFLUSH);
|
tcsetattr(fFdWrite, TCSANOW, &fTiosOld);
|
tcsetattr(fFdWrite, TCSANOW, &fTiosOld);
|
}
|
}
|
RlinkPort::Close();
|
RlinkPort::Close();
|
|
|
return;
|
return;
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! FIXME_docs
|
//! FIXME_docs
|
|
|
int RlinkPortTerm::Read(uint8_t* buf, size_t size, double timeout,
|
int RlinkPortTerm::Read(uint8_t* buf, size_t size, double timeout,
|
RerrMsg& emsg)
|
RerrMsg& emsg)
|
{
|
{
|
int irc;
|
int irc;
|
if (fUseXon) {
|
if (fUseXon) {
|
uint8_t* po = buf;
|
uint8_t* po = buf;
|
if (fRxBuf.size() < size) fRxBuf.resize(size);
|
if (fRxBuf.size() < size) fRxBuf.resize(size);
|
|
|
// repeat read until at least one byte returned (or an error occurs)
|
// repeat read until at least one byte returned (or an error occurs)
|
// this avoids that the Read() returns with 0 in case only one byte is
|
// this avoids that the Read() returns with 0 in case only one byte is
|
// seen and this is a kc_xesc. At most two iterations possible because
|
// seen and this is a kc_xesc. At most two iterations possible because
|
// in 2nd iteration fPendXesc must be set and thus po pushed.
|
// in 2nd iteration fPendXesc must be set and thus po pushed.
|
while (po == buf) {
|
while (po == buf) {
|
irc = RlinkPort::Read(fRxBuf.data(), size, timeout, emsg);
|
irc = RlinkPort::Read(fRxBuf.data(), size, timeout, emsg);
|
if (irc <= 0) break;
|
if (irc <= 0) break;
|
uint8_t* pi = fRxBuf.data();
|
uint8_t* pi = fRxBuf.data();
|
for (int i=0; i<irc; i++) {
|
for (int i=0; i<irc; i++) {
|
uint8_t c = *pi++;
|
uint8_t c = *pi++;
|
if (fPendXesc) {
|
if (fPendXesc) {
|
*po++ = ~c;
|
*po++ = ~c;
|
fPendXesc = false;
|
fPendXesc = false;
|
} else if (c == kc_xesc) {
|
} else if (c == kc_xesc) {
|
fStats.Inc(kStatNPortRxXesc);
|
fStats.Inc(kStatNPortRxXesc);
|
fPendXesc = true;
|
fPendXesc = true;
|
} else {
|
} else {
|
*po++ = c;
|
*po++ = c;
|
}
|
}
|
}
|
}
|
irc = po - buf; // set irc to # of unescaped bytes
|
irc = po - buf; // set irc to # of unescaped bytes
|
}
|
}
|
|
|
} else {
|
} else {
|
irc = RlinkPort::Read(buf, size, timeout, emsg);
|
irc = RlinkPort::Read(buf, size, timeout, emsg);
|
}
|
}
|
|
|
return irc;
|
return irc;
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! FIXME_docs
|
//! FIXME_docs
|
|
|
int RlinkPortTerm::Write(const uint8_t* buf, size_t size, RerrMsg& emsg)
|
int RlinkPortTerm::Write(const uint8_t* buf, size_t size, RerrMsg& emsg)
|
{
|
{
|
int irc = 0;
|
int irc = 0;
|
|
|
if (fUseXon) {
|
if (fUseXon) {
|
fTxBuf.clear();
|
fTxBuf.clear();
|
const uint8_t* pc = buf;
|
const uint8_t* pc = buf;
|
|
|
for (size_t i=0; i<size; i++) {
|
for (size_t i=0; i<size; i++) {
|
uint8_t c = *pc++;
|
uint8_t c = *pc++;
|
if (c==kc_xon || c==kc_xoff || c==kc_xesc) {
|
if (c==kc_xon || c==kc_xoff || c==kc_xesc) {
|
fStats.Inc(kStatNPortTxXesc);
|
fStats.Inc(kStatNPortTxXesc);
|
fTxBuf.push_back(kc_xesc);
|
fTxBuf.push_back(kc_xesc);
|
fTxBuf.push_back(~c);
|
fTxBuf.push_back(~c);
|
} else {
|
} else {
|
fTxBuf.push_back(c);
|
fTxBuf.push_back(c);
|
}
|
}
|
}
|
}
|
int irce = RlinkPort::Write(fTxBuf.data(), fTxBuf.size(), emsg);
|
int irce = RlinkPort::Write(fTxBuf.data(), fTxBuf.size(), emsg);
|
if (irce == (int)fTxBuf.size()) irc = size;
|
if (irce == (int)fTxBuf.size()) irc = size;
|
} else {
|
} else {
|
irc = RlinkPort::Write(buf, size, emsg);
|
irc = RlinkPort::Write(buf, size, emsg);
|
}
|
}
|
|
|
/* tcdrain(fFdWrite);*/
|
/* tcdrain(fFdWrite);*/
|
return irc;
|
return irc;
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! FIXME_docs
|
//! FIXME_docs
|
|
|
void RlinkPortTerm::Dump(std::ostream& os, int ind, const char* text) const
|
void RlinkPortTerm::Dump(std::ostream& os, int ind, const char* text) const
|
{
|
{
|
RosFill bl(ind);
|
RosFill bl(ind);
|
os << bl << (text?text:"--") << "RlinkPortTerm @ " << this << endl;
|
os << bl << (text?text:"--") << "RlinkPortTerm @ " << this << endl;
|
DumpTios(os, ind, "fTiosOld", fTiosOld);
|
DumpTios(os, ind, "fTiosOld", fTiosOld);
|
DumpTios(os, ind, "fTiosNew", fTiosNew);
|
DumpTios(os, ind, "fTiosNew", fTiosNew);
|
RlinkPort::Dump(os, ind, " ^");
|
RlinkPort::Dump(os, ind, " ^");
|
return;
|
return;
|
}
|
}
|
|
|
//------------------------------------------+-----------------------------------
|
//------------------------------------------+-----------------------------------
|
//! FIXME_docs
|
//! FIXME_docs
|
|
|
void RlinkPortTerm::DumpTios(std::ostream& os, int ind, const std::string& name,
|
void RlinkPortTerm::DumpTios(std::ostream& os, int ind, const std::string& name,
|
const struct termios& tios) const
|
const struct termios& tios) const
|
{
|
{
|
RosFill bl(ind+2);
|
RosFill bl(ind+2);
|
os << bl << name << ":" << endl;
|
os << bl << name << ":" << endl;
|
os << bl << " c_iflag : " << RosPrintf(tios.c_iflag,"x0",8);
|
os << bl << " c_iflag : " << RosPrintf(tios.c_iflag,"x0",8);
|
if (tios.c_iflag & BRKINT) os << " BRKINT";
|
if (tios.c_iflag & BRKINT) os << " BRKINT";
|
if (tios.c_iflag & ICRNL) os << " ICRNL ";
|
if (tios.c_iflag & ICRNL) os << " ICRNL ";
|
if (tios.c_iflag & IGNBRK) os << " IGNBRK";
|
if (tios.c_iflag & IGNBRK) os << " IGNBRK";
|
if (tios.c_iflag & IGNCR) os << " IGNCR ";
|
if (tios.c_iflag & IGNCR) os << " IGNCR ";
|
if (tios.c_iflag & IGNPAR) os << " IGNPAR";
|
if (tios.c_iflag & IGNPAR) os << " IGNPAR";
|
if (tios.c_iflag & INLCR) os << " INLCR ";
|
if (tios.c_iflag & INLCR) os << " INLCR ";
|
if (tios.c_iflag & INPCK) os << " INPCK ";
|
if (tios.c_iflag & INPCK) os << " INPCK ";
|
if (tios.c_iflag & ISTRIP) os << " ISTRIP";
|
if (tios.c_iflag & ISTRIP) os << " ISTRIP";
|
if (tios.c_iflag & IXOFF) os << " IXOFF ";
|
if (tios.c_iflag & IXOFF) os << " IXOFF ";
|
if (tios.c_iflag & IXON) os << " IXON ";
|
if (tios.c_iflag & IXON) os << " IXON ";
|
if (tios.c_iflag & PARMRK) os << " PARMRK";
|
if (tios.c_iflag & PARMRK) os << " PARMRK";
|
os << endl;
|
os << endl;
|
|
|
os << bl << " c_oflag : " << RosPrintf(tios.c_oflag,"x0",8);
|
os << bl << " c_oflag : " << RosPrintf(tios.c_oflag,"x0",8);
|
if (tios.c_oflag & OPOST) os << " OPOST ";
|
if (tios.c_oflag & OPOST) os << " OPOST ";
|
os << endl;
|
os << endl;
|
|
|
os << bl << " c_cflag : " << RosPrintf(tios.c_cflag,"x0",8);
|
os << bl << " c_cflag : " << RosPrintf(tios.c_cflag,"x0",8);
|
if (tios.c_cflag & CLOCAL) os << " CLOCAL";
|
if (tios.c_cflag & CLOCAL) os << " CLOCAL";
|
if (tios.c_cflag & CREAD) os << " CREAD ";
|
if (tios.c_cflag & CREAD) os << " CREAD ";
|
if ((tios.c_cflag & CSIZE) == CS5) os << " CS5 ";
|
if ((tios.c_cflag & CSIZE) == CS5) os << " CS5 ";
|
if ((tios.c_cflag & CSIZE) == CS6) os << " CS6 ";
|
if ((tios.c_cflag & CSIZE) == CS6) os << " CS6 ";
|
if ((tios.c_cflag & CSIZE) == CS7) os << " CS7 ";
|
if ((tios.c_cflag & CSIZE) == CS7) os << " CS7 ";
|
if ((tios.c_cflag & CSIZE) == CS8) os << " CS8 ";
|
if ((tios.c_cflag & CSIZE) == CS8) os << " CS8 ";
|
if (tios.c_cflag & CSTOPB) os << " CSTOPB";
|
if (tios.c_cflag & CSTOPB) os << " CSTOPB";
|
if (tios.c_cflag & HUPCL) os << " HUPCL ";
|
if (tios.c_cflag & HUPCL) os << " HUPCL ";
|
if (tios.c_cflag & PARENB) os << " PARENB";
|
if (tios.c_cflag & PARENB) os << " PARENB";
|
if (tios.c_cflag & PARODD) os << " PARODD";
|
if (tios.c_cflag & PARODD) os << " PARODD";
|
speed_t speed = cfgetispeed(&tios);
|
speed_t speed = cfgetispeed(&tios);
|
int baud = 0;
|
int baud = 0;
|
if (speed == B2400) baud = 2400;
|
if (speed == B2400) baud = 2400;
|
if (speed == B4800) baud = 4800;
|
if (speed == B4800) baud = 4800;
|
if (speed == B9600) baud = 9600;
|
if (speed == B9600) baud = 9600;
|
if (speed == B19200) baud = 19200;
|
if (speed == B19200) baud = 19200;
|
if (speed == B38400) baud = 38400;
|
if (speed == B38400) baud = 38400;
|
if (speed == B57600) baud = 57600;
|
if (speed == B57600) baud = 57600;
|
if (speed == B115200) baud = 115200;
|
if (speed == B115200) baud = 115200;
|
if (speed == B230400) baud = 230400;
|
if (speed == B230400) baud = 230400;
|
if (speed == B460800) baud = 460800;
|
if (speed == B460800) baud = 460800;
|
if (speed == B500000) baud = 500000;
|
if (speed == B500000) baud = 500000;
|
if (speed == B921600) baud = 921600;
|
if (speed == B921600) baud = 921600;
|
if (speed == B1000000) baud = 1000000;
|
if (speed == B1000000) baud = 1000000;
|
if (speed == B1152000) baud = 1152000;
|
if (speed == B1152000) baud = 1152000;
|
if (speed == B1500000) baud = 1500000;
|
if (speed == B1500000) baud = 1500000;
|
if (speed == B2000000) baud = 2000000;
|
if (speed == B2000000) baud = 2000000;
|
if (speed == B2500000) baud = 2500000;
|
if (speed == B2500000) baud = 2500000;
|
if (speed == B3000000) baud = 3000000;
|
if (speed == B3000000) baud = 3000000;
|
if (speed == B3500000) baud = 3500000;
|
if (speed == B3500000) baud = 3500000;
|
if (speed == B4000000) baud = 4000000;
|
if (speed == B4000000) baud = 4000000;
|
os << " speed: " << RosPrintf(baud, "d", 7);
|
os << " speed: " << RosPrintf(baud, "d", 7);
|
os << endl;
|
os << endl;
|
|
|
os << bl << " c_lflag : " << RosPrintf(tios.c_lflag,"x0",8);
|
os << bl << " c_lflag : " << RosPrintf(tios.c_lflag,"x0",8);
|
if (tios.c_lflag & ECHO) os << " ECHO ";
|
if (tios.c_lflag & ECHO) os << " ECHO ";
|
if (tios.c_lflag & ECHOE) os << " ECHOE ";
|
if (tios.c_lflag & ECHOE) os << " ECHOE ";
|
if (tios.c_lflag & ECHOK) os << " ECHOK ";
|
if (tios.c_lflag & ECHOK) os << " ECHOK ";
|
if (tios.c_lflag & ECHONL) os << " ECHONL";
|
if (tios.c_lflag & ECHONL) os << " ECHONL";
|
if (tios.c_lflag & ICANON) os << " ICANON";
|
if (tios.c_lflag & ICANON) os << " ICANON";
|
if (tios.c_lflag & IEXTEN) os << " IEXTEN";
|
if (tios.c_lflag & IEXTEN) os << " IEXTEN";
|
if (tios.c_lflag & ISIG) os << " ISIG ";
|
if (tios.c_lflag & ISIG) os << " ISIG ";
|
if (tios.c_lflag & NOFLSH) os << " NOFLSH";
|
if (tios.c_lflag & NOFLSH) os << " NOFLSH";
|
if (tios.c_lflag & TOSTOP) os << " TOSTOP";
|
if (tios.c_lflag & TOSTOP) os << " TOSTOP";
|
os << endl;
|
os << endl;
|
|
|
os << bl << " c_cc : " << endl;
|
os << bl << " c_cc : " << endl;
|
os << bl << " [VEOF] : " << RosPrintf(tios.c_cc[VEOF],"o",3);
|
os << bl << " [VEOF] : " << RosPrintf(tios.c_cc[VEOF],"o",3);
|
os << " [VEOL] : " << RosPrintf(tios.c_cc[VEOL],"o",3);
|
os << " [VEOL] : " << RosPrintf(tios.c_cc[VEOL],"o",3);
|
os << " [VERASE]: " << RosPrintf(tios.c_cc[VERASE],"o",3);
|
os << " [VERASE]: " << RosPrintf(tios.c_cc[VERASE],"o",3);
|
os << " [VINTR] : " << RosPrintf(tios.c_cc[VINTR],"o",3) << endl;
|
os << " [VINTR] : " << RosPrintf(tios.c_cc[VINTR],"o",3) << endl;
|
os << bl << " [VKILL] : " << RosPrintf(tios.c_cc[VKILL],"o",3);
|
os << bl << " [VKILL] : " << RosPrintf(tios.c_cc[VKILL],"o",3);
|
os << " [VQUIT] : " << RosPrintf(tios.c_cc[VQUIT],"o",3);
|
os << " [VQUIT] : " << RosPrintf(tios.c_cc[VQUIT],"o",3);
|
os << " [VSUSP] : " << RosPrintf(tios.c_cc[VSUSP],"o",3);
|
os << " [VSUSP] : " << RosPrintf(tios.c_cc[VSUSP],"o",3);
|
os << " [VSTART]: " << RosPrintf(tios.c_cc[VSTART],"o",3) << endl;
|
os << " [VSTART]: " << RosPrintf(tios.c_cc[VSTART],"o",3) << endl;
|
os << bl << " [VSTOP] : " << RosPrintf(tios.c_cc[VSTOP],"o",3);
|
os << bl << " [VSTOP] : " << RosPrintf(tios.c_cc[VSTOP],"o",3);
|
os << " [VMIN] : " << RosPrintf(tios.c_cc[VMIN],"o",3);
|
os << " [VMIN] : " << RosPrintf(tios.c_cc[VMIN],"o",3);
|
os << " [VTIME] : " << RosPrintf(tios.c_cc[VTIME],"o",3) << endl;
|
os << " [VTIME] : " << RosPrintf(tios.c_cc[VTIME],"o",3) << endl;
|
|
|
return;
|
return;
|
}
|
}
|
|
|
} // end namespace Retro
|
} // end namespace Retro
|
|
|