119 throw Rexception(
"RlinkPort::Read()",
"Bad state: port not open");
121 throw Rexception(
"RlinkPort::Read()",
"Bad args: buf==nullptr");
123 throw Rexception(
"RlinkPort::Read()",
"Bad args: size==0");
128 if (!rdpoll)
return kTout;
132 irc = ::read(
fFdRead, buf, size);
133 if (irc < 0 && errno != EINTR) {
134 emsg.
InitErrno(
"RlinkPort::Read()",
"read() failed : ", errno);
142 lmsg <<
"port read nchar=" <<
RosPrintf(irc,
"d",4);
143 Rtime now(CLOCK_MONOTONIC);
150 size_t ncol = (80-5-6)/(2+1);
151 for (
int i=0; i<irc; i++) {
152 if ((i%ncol)==0) lmsg <<
"\n " <<
RosPrintf(i,
"d",4) <<
": ";
169 throw Rexception(
"RlinkPort::Write()",
"Bad state: port not open");
171 throw Rexception(
"RlinkPort::Write()",
"Bad args: buf==nullptr");
173 throw Rexception(
"RlinkPort::Write()",
"Bad args: size==0");
179 lmsg <<
"port write nchar=" <<
RosPrintf(size,
"d",4);
180 Rtime now(CLOCK_MONOTONIC);
187 size_t ncol = (80-5-6)/(2+1);
188 for (
size_t i=0; i<size; i++) {
189 if ((i%ncol)==0) lmsg <<
"\n " <<
RosPrintf(i,
"d",4) <<
": ";
196 while (ndone < size) {
199 irc = ::write(
fFdWrite, buf+ndone, size-ndone);
200 if (irc < 0 && errno != EINTR) {
201 emsg.
InitErrno(
"RlinkPort::Write()",
"write() failed : ", errno);
221 throw Rexception(
"RlinkPort::PollRead()",
"Bad state: port not open");
223 throw Rexception(
"RlinkPort::PollRead()",
"Bad args: timeout < 0");
225 int ito = timeout.
ToMSec();
227 struct pollfd fds[1] = {{
fFdRead,
234 irc = ::poll(fds, 1, ito);
235 if (irc < 0 && errno != EINTR)
236 throw Rexception(
"RlinkPort::PollRead()",
"poll() failed: rc<0: ", errno);
239 if (irc == 0)
return false;
241 if (fds[0].revents == POLLERR)
242 throw Rexception(
"RlinkPort::PollRead()",
"poll() failed: POLLERR");
253 throw Rexception(
"RlinkPort::RawRead()",
"Bad args: timeout <= 0.");
255 throw Rexception(
"RlinkPort::RawRead()",
"Bad args: size <= 0");
260 Rtime tnow(CLOCK_MONOTONIC);
261 Rtime tend = tnow + timeout;
265 while (tnow < tend && ndone<size) {
266 int irc =
Read(buf+ndone, size-ndone, tend-tnow, emsg);
269 if (irc <= 0)
return irc;
271 if (!exactsize)
break;
282 return Write(buf, size, emsg);
292 os << bl << (text?text:
"--") <<
"RlinkPort @ " <<
this << endl;
297 os << bl <<
" fFdRead: " <<
fFdRead << endl;
298 os << bl <<
" fFdWrite: " <<
fFdWrite << endl;
299 os << bl <<
" fspLog: " <<
fspLog.get() << endl;
300 os << bl <<
" fTraceLevel: " <<
fTraceLevel << endl;
301 os << bl <<
" fTsLastRead: " <<
fTsLastRead << endl;
std::string Message() const
FIXME_docs.
void InitErrno(const std::string &meth, const std::string &text, int errnum)
FIXME_docs.
int RawRead(uint8_t *buf, size_t size, bool exactsize, const Rtime &timeout, Rtime &tused, RerrMsg &emsg)
FIXME_docs.
int RawWrite(const uint8_t *buf, size_t size, RerrMsg &emsg)
FIXME_docs.
virtual bool PollRead(const Rtime &timeout)
FIXME_docs.
Rtime fTsLastWrite
time stamp last write
static const int kEof
return code: end-of-file
void CloseFd(int &fd)
FIXME_docs.
bool IsOpen() const
FIXME_docs.
virtual ~RlinkPort()
Destructor.
static const int kErr
return code: IO error
RlinkPort()
Default constructor.
std::shared_ptr< RlogFile > fspLog
log file ptr
bool fXon
xon attribute set
static const int kTout
return code: time out
virtual int Read(uint8_t *buf, size_t size, const Rtime &timeout, RerrMsg &emsg)
FIXME_docs.
virtual void Close()
FIXME_docs.
virtual int Write(const uint8_t *buf, size_t size, RerrMsg &emsg)
FIXME_docs.
Rtime fTsLastRead
time stamp last write
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
uint32_t fTraceLevel
trace level
I/O appicator to generate fill characters.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0) const
FIXME_docs.
void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
void Inc(size_t ind, double val=1.)
FIXME_docs.
void Define(size_t ind, const std::string &name, const std::string &text)
FIXME_docs.
bool IsNegative() const
FIXME_docs.
int ToMSec() const
FIXME_docs.
bool IsPositive() const
FIXME_docs.
void GetClock(clockid_t clkid)
FIXME_docs.
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Declaration of class ReventLoop.