OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librlink/] [RlinkPortTerm.cpp] - Blame information for rev 17

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 wfjm
// $Id: RlinkPortTerm.cpp 466 2012-12-30 13:26:55Z mueller $
2 10 wfjm
//
3
// Copyright 2011- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 16 wfjm
// 2011-12-18   440   1.0.4  add kStatNPort stats; Open(): autoadd /dev/tty,
17
//                           BUGFIX: Open(): set VSTART, VSTOP
18
// 2011-12-11   438   1.0.3  Read(),Write(): added for xon handling, tcdrain();
19
//                           Open(): add more baud rates, support xon attribute
20 15 wfjm
// 2011-12-04   435   1.0.2  Open(): add cts attr, hw flow control now optional
21 12 wfjm
// 2011-07-04   388   1.0.1  add termios readback and verification
22 10 wfjm
// 2011-03-27   374   1.0    Initial version
23
// ---------------------------------------------------------------------------
24
 
25
/*!
26
  \file
27 17 wfjm
  \version $Id: RlinkPortTerm.cpp 466 2012-12-30 13:26:55Z mueller $
28 10 wfjm
  \brief   Implemenation of RlinkPortTerm.
29
*/
30
 
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
34
#include <errno.h>
35
#include <unistd.h>
36
#include <termios.h>
37
 
38
#include "RlinkPortTerm.hpp"
39
 
40
#include "librtools/RosFill.hpp"
41
#include "librtools/RosPrintf.hpp"
42
 
43
using namespace std;
44
using namespace Retro;
45
 
46
/*!
47
  \class Retro::RlinkPortTerm
48
  \brief FIXME_text
49
*/
50
 
51
//------------------------------------------+-----------------------------------
52 16 wfjm
// constants definitions
53
const uint8_t RlinkPortTerm::kc_xon;
54
const uint8_t RlinkPortTerm::kc_xoff;
55
const uint8_t RlinkPortTerm::kc_xesc;
56
 
57
//------------------------------------------+-----------------------------------
58 10 wfjm
//! Default constructor
59
 
60
RlinkPortTerm::RlinkPortTerm()
61
  : RlinkPort()
62 16 wfjm
{
63
  fStats.Define(kStatNPortTxXesc, "NPortTxXesc", "Tx XESC escapes");
64
  fStats.Define(kStatNPortRxXesc, "NPortRxXesc", "Rx XESC escapes");
65
}
66 10 wfjm
 
67
//------------------------------------------+-----------------------------------
68
//! Destructor
69
 
70
RlinkPortTerm::~RlinkPortTerm()
71
{
72
  if (IsOpen()) RlinkPortTerm::Close();
73
}
74
 
75
//------------------------------------------+-----------------------------------
76
//! FIXME_text
77
 
78
bool RlinkPortTerm::Open(const std::string& url, RerrMsg& emsg)
79
{
80
  if (IsOpen()) Close();
81
 
82 16 wfjm
  if (!ParseUrl(url, "|baud=|break|cts|xon", emsg)) return false;
83 10 wfjm
 
84 16 wfjm
  // if path doesn't start with a '/' prepend a '/dev/tty'
85
  if (fPath.substr(0,1) != "/") {
86
    string dev = fPath;
87
    fPath  = "/dev/tty";
88
    fPath += dev;
89
  }
90
 
91 10 wfjm
  speed_t speed = B115200;
92
  string baud;
93
  if (UrlFindOpt("baud", baud)) {
94
    speed = B0;
95 16 wfjm
    if (baud=="2400")                     speed = B2400;
96
    if (baud=="4800")                     speed = B4800;
97 10 wfjm
    if (baud=="9600")                     speed = B9600;
98
    if (baud=="19200"   || baud=="19k")   speed = B19200;
99
    if (baud=="38400"   || baud=="38k")   speed = B38400;
100
    if (baud=="57600"   || baud=="57k")   speed = B57600;
101
    if (baud=="115200"  || baud=="115k")  speed = B115200;
102
    if (baud=="230400"  || baud=="230k")  speed = B230400;
103
    if (baud=="460800"  || baud=="460k")  speed = B460800;
104
    if (baud=="500000"  || baud=="500k")  speed = B500000;
105
    if (baud=="921600"  || baud=="921k")  speed = B921600;
106 16 wfjm
    if (baud=="1000000" || baud=="1000k" || baud=="1M") speed = B1000000;
107
    if (baud=="1152000" || baud=="1152k")               speed = B1152000;
108
    if (baud=="1500000" || baud=="1500k")               speed = B1500000;
109
    if (baud=="2000000" || baud=="2000k" || baud=="2M") speed = B2000000;
110
    if (baud=="2500000" || baud=="2500k")               speed = B2500000;
111
    if (baud=="3000000" || baud=="3000k" || baud=="3M") speed = B3000000;
112
    if (baud=="3500000" || baud=="3500k")               speed = B3500000;
113
    if (baud=="4000000" || baud=="4000k" || baud=="4M") speed = B4000000;
114 10 wfjm
    if (speed == B0) {
115
      emsg.Init("RlinkPortTerm::Open()",
116
                string("invalid baud rate \"") + baud + string("\" specified"));
117
      return false;
118
    }
119
  }
120
 
121
  int fd;
122
 
123
  fd = open(fPath.c_str(), O_RDWR|O_NOCTTY);
124
  if (fd < 0) {
125
    emsg.InitErrno("RlinkPortTerm::Open()",
126
                   string("open() for \"") + fPath + string("\" failed: "),
127
                   errno);
128
    return false;
129
  }
130
 
131
  if (!isatty(fd)) {
132
    emsg.Init("RlinkPortTerm::Open()",
133
              string("isatty() check for \"") + fPath +
134
              string("\" failed: not a TTY"));
135
    close(fd);
136
    return false;
137
  }
138
 
139
  if (tcgetattr(fd, &fTiosOld) != 0) {
140
    emsg.InitErrno("RlinkPortTerm::Open()",
141
                   string("tcgetattr() for \"") + fPath + string("\" failed: "),
142
                   errno);
143
    close(fd);
144
    return false;
145
  }
146
 
147 16 wfjm
  bool use_cts = UrlFindOpt("cts");
148
  bool use_xon = UrlFindOpt("xon");
149
  fUseXon = use_xon;
150
  fPendXesc = false;
151
 
152 10 wfjm
  fTiosNew = fTiosOld;
153
 
154
  fTiosNew.c_iflag = IGNBRK |               // ignore breaks on input
155
                     IGNPAR;                // ignore parity errors
156 16 wfjm
  if (use_xon) {
157
    fTiosNew.c_iflag |= IXON|               // XON/XOFF flow control output
158
                        IXOFF;              // XON/XOFF flow control input
159
  }
160 15 wfjm
 
161 10 wfjm
  fTiosNew.c_oflag = 0;
162 15 wfjm
 
163 10 wfjm
  fTiosNew.c_cflag = CS8 |                  // 8 bit chars
164
                     CSTOPB |               // 2 stop bits
165
                     CREAD |                // enable receiver
166 15 wfjm
                     CLOCAL;                // ignore modem control
167 16 wfjm
  if (use_cts) {
168 15 wfjm
    fTiosNew.c_cflag |= CRTSCTS;            // enable hardware flow control
169
  }
170
 
171 10 wfjm
  fTiosNew.c_lflag = 0;
172
 
173
  if (cfsetspeed(&fTiosNew, speed) != 0) {
174
    emsg.InitErrno("RlinkPortTerm::Open()",
175
                   string("cfsetspeed() for \"") + baud + string("\" failed: "),
176
                   errno);
177
    close(fd);
178
    return false;
179
  }
180
 
181
  fTiosNew.c_cc[VEOF]   = 0;                // undef
182
  fTiosNew.c_cc[VEOL]   = 0;                // undef
183
  fTiosNew.c_cc[VERASE] = 0;                // undef
184
  fTiosNew.c_cc[VINTR]  = 0;                // undef
185
  fTiosNew.c_cc[VKILL]  = 0;                // undef
186
  fTiosNew.c_cc[VQUIT]  = 0;                // undef
187
  fTiosNew.c_cc[VSUSP]  = 0;                // undef
188
  fTiosNew.c_cc[VSTART] = 0;                // undef
189
  fTiosNew.c_cc[VSTOP]  = 0;                // undef
190
  fTiosNew.c_cc[VMIN]   = 1;                // wait for 1 char
191
  fTiosNew.c_cc[VTIME]  = 0;                // 
192 16 wfjm
  if (use_xon) {
193
    fTiosNew.c_cc[VSTART] = kc_xon;         // setup XON  -> ^Q
194
    fTiosNew.c_cc[VSTOP]  = kc_xoff;        // setup XOFF -> ^S   
195
  }
196 10 wfjm
 
197
  if (tcsetattr(fd, TCSANOW, &fTiosNew) != 0) {
198
    emsg.InitErrno("RlinkPortTerm::Open()",
199
                   string("tcsetattr() for \"") + fPath + string("\" failed: "),
200
                   errno);
201
    close(fd);
202
    return false;
203
  }
204
 
205 12 wfjm
  // tcsetattr() returns success if any of the requested changes could be
206
  // successfully carried out. Therefore the termios structure is read back
207
  // and verified.
208
 
209
  struct termios tios;
210
  if (tcgetattr(fd, &tios) != 0) {
211
    emsg.InitErrno("RlinkPortTerm::Open()",
212
                   string("2nd tcgetattr() for \"") + fPath +
213
                     string("\" failed: "),
214
                   errno);
215
    close(fd);
216
    return false;
217
  }
218
 
219
  const char* pmsg = 0;
220
  if (tios.c_iflag != fTiosNew.c_iflag) pmsg = "c_iflag";
221
  if (tios.c_oflag != fTiosNew.c_oflag) pmsg = "c_oflag";
222
  if (tios.c_cflag != fTiosNew.c_cflag) pmsg = "c_cflag";
223
  if (tios.c_lflag != fTiosNew.c_lflag) pmsg = "c_lflag";
224
  if (cfgetispeed(&tios) != speed)      pmsg = "ispeed";
225
  if (cfgetospeed(&tios) != speed)      pmsg = "ospeed";
226
  for (int i=0; i<NCCS; i++) {
227
    if (tios.c_cc[i] != fTiosNew.c_cc[i]) pmsg = "c_cc char";
228
  }
229
 
230
  if (pmsg) {
231
    emsg.Init("RlinkPortTerm::Open()",
232
              string("tcsetattr() failed to set") +
233
                string(pmsg));
234
    close(fd);
235
    return false;
236
  }
237
 
238 10 wfjm
  fFdWrite = fd;
239
  fFdRead  = fd;
240
  fIsOpen  = true;
241
 
242
  if (UrlFindOpt("break")) {
243
    if (tcsendbreak(fd, 0) != 0) {
244
      emsg.InitErrno("RlinkPortTerm::Open()",
245
                     string("tcsendbreak() for \"") + fPath +
246
                     string("\" failed: "), errno);
247
      Close();
248
      return false;
249
    }
250
    uint8_t buf[1];
251
    buf[0] = 0x80;
252
    if (Write(buf, 1, emsg) != 1) {
253
      Close();
254
      return false;
255
    }
256
  }
257
 
258
  return true;
259
}
260
 
261
//------------------------------------------+-----------------------------------
262
//! FIXME_text
263
 
264
void RlinkPortTerm::Close()
265
{
266 17 wfjm
  if (!IsOpen()) return;
267
 
268
  if (fFdWrite >= 0) {
269
    tcflush(fFdWrite, TCIOFLUSH);
270
    tcsetattr(fFdWrite, TCSANOW, &fTiosOld);
271 10 wfjm
  }
272 17 wfjm
  RlinkPort::Close();
273
 
274 10 wfjm
  return;
275
}
276
 
277
//------------------------------------------+-----------------------------------
278 16 wfjm
//! FIXME_docs
279
 
280
int RlinkPortTerm::Read(uint8_t* buf, size_t size, double timeout,
281
                        RerrMsg& emsg)
282
{
283
  int irc;
284
  if (fUseXon) {
285
    uint8_t* po = buf;
286
    if (fRxBuf.size() < size) fRxBuf.resize(size);
287
 
288 17 wfjm
    // repeat read until at least one byte returned (or an error occurs)
289 16 wfjm
    // this avoids that the Read() returns with 0 in case only one byte is
290
    // seen and this is a kc_xesc. At most two iterations possible because
291
    // in 2nd iteration fPendXesc must be set and thus po pushed.
292
    while (po == buf) {
293
      irc = RlinkPort::Read(fRxBuf.data(), size, timeout, emsg);
294
      if (irc <= 0) break;
295
      uint8_t* pi = fRxBuf.data();
296
      for (int i=0; i<irc; i++) {
297
        uint8_t c = *pi++;
298
        if (fPendXesc) {
299
          *po++ = ~c;
300
          fPendXesc = false;
301
        } else if (c == kc_xesc) {
302
          fStats.Inc(kStatNPortRxXesc);
303
          fPendXesc = true;
304
        } else {
305
          *po++ = c;
306
        }
307
      }
308
      irc = po - buf;                       // set irc to # of unescaped bytes
309
    }
310
 
311
  } else {
312
    irc = RlinkPort::Read(buf, size, timeout, emsg);
313
  }
314
 
315
  return irc;
316
}
317
 
318
//------------------------------------------+-----------------------------------
319
//! FIXME_docs
320
 
321
int RlinkPortTerm::Write(const uint8_t* buf, size_t size, RerrMsg& emsg)
322
{
323
  int irc = 0;
324
 
325
  if (fUseXon) {
326
    fTxBuf.clear();
327
    const uint8_t* pc = buf;
328
 
329
    for (size_t i=0; i<size; i++) {
330
      uint8_t c = *pc++;
331
      if (c==kc_xon || c==kc_xoff || c==kc_xesc) {
332
        fStats.Inc(kStatNPortTxXesc);
333
        fTxBuf.push_back(kc_xesc);
334
        fTxBuf.push_back(~c);
335
      } else {
336
        fTxBuf.push_back(c);
337
      }
338
    }
339
    int irce = RlinkPort::Write(fTxBuf.data(), fTxBuf.size(), emsg);
340
    if (irce == (int)fTxBuf.size()) irc = size;
341
  } else {
342
    irc = RlinkPort::Write(buf, size, emsg);
343
  }
344
 
345
  /* tcdrain(fFdWrite);*/
346
  return irc;
347
}
348
 
349
//------------------------------------------+-----------------------------------
350 10 wfjm
//! FIXME_text
351
 
352
void RlinkPortTerm::Dump(std::ostream& os, int ind, const char* text) const
353
{
354
  RosFill bl(ind);
355
  os << bl << (text?text:"--") << "RlinkPortTerm @ " << this << endl;
356
  DumpTios(os, ind, "fTiosOld", fTiosOld);
357
  DumpTios(os, ind, "fTiosNew", fTiosNew);
358
  RlinkPort::Dump(os, ind+2, "");
359
  return;
360
}
361
 
362
 
363
//------------------------------------------+-----------------------------------
364
//! FIXME_text
365
 
366
void RlinkPortTerm::DumpTios(std::ostream& os, int ind, const std::string& name,
367
                             const struct termios& tios) const
368
{
369
  RosFill bl(ind+2);
370
  os << bl << name << ":" << endl;
371
  os << bl << "  c_iflag : " << RosPrintf(tios.c_iflag,"x0",8);
372
  if (tios.c_iflag & BRKINT) os << " BRKINT";
373
  if (tios.c_iflag & ICRNL)  os << " ICRNL ";
374
  if (tios.c_iflag & IGNBRK) os << " IGNBRK";
375
  if (tios.c_iflag & IGNCR)  os << " IGNCR ";
376
  if (tios.c_iflag & IGNPAR) os << " IGNPAR";
377
  if (tios.c_iflag & INLCR)  os << " INLCR ";
378
  if (tios.c_iflag & INPCK)  os << " INPCK ";
379
  if (tios.c_iflag & ISTRIP) os << " ISTRIP";
380
  if (tios.c_iflag & IXOFF)  os << " IXOFF ";
381
  if (tios.c_iflag & IXON)   os << " IXON  ";
382
  if (tios.c_iflag & PARMRK) os << " PARMRK";
383
  os << endl;
384
 
385
  os << bl << "  c_oflag : " << RosPrintf(tios.c_oflag,"x0",8);
386
  if (tios.c_oflag & OPOST)  os << " OPOST ";
387
  os << endl;
388
 
389
  os << bl << "  c_cflag : " << RosPrintf(tios.c_cflag,"x0",8);
390
  if (tios.c_cflag & CLOCAL) os << " CLOCAL";
391
  if (tios.c_cflag & CREAD)  os << " CREAD ";
392
  if ((tios.c_cflag & CSIZE) == CS5)  os << " CS5   ";
393
  if ((tios.c_cflag & CSIZE) == CS6)  os << " CS6   ";
394
  if ((tios.c_cflag & CSIZE) == CS7)  os << " CS7   ";
395
  if ((tios.c_cflag & CSIZE) == CS8)  os << " CS8   ";
396
  if (tios.c_cflag & CSTOPB) os << " CSTOPB";
397
  if (tios.c_cflag & HUPCL)  os << " HUPCL ";
398
  if (tios.c_cflag & PARENB) os << " PARENB";
399
  if (tios.c_cflag & PARODD) os << " PARODD";
400
  speed_t speed = cfgetispeed(&tios);
401
  int baud = 0;
402 16 wfjm
  if (speed == B2400)    baud =    2400;
403
  if (speed == B4800)    baud =    4800;
404 10 wfjm
  if (speed == B9600)    baud =    9600;
405
  if (speed == B19200)   baud =   19200;
406
  if (speed == B38400)   baud =   38400;
407
  if (speed == B57600)   baud =   57600;
408
  if (speed == B115200)  baud =  115200;
409
  if (speed == B230400)  baud =  230400;
410
  if (speed == B460800)  baud =  460800;
411
  if (speed == B500000)  baud =  500000;
412
  if (speed == B921600)  baud =  921600;
413
  if (speed == B1000000) baud = 1000000;
414 16 wfjm
  if (speed == B1152000) baud = 1152000;
415
  if (speed == B1500000) baud = 1500000;
416 10 wfjm
  if (speed == B2000000) baud = 2000000;
417 16 wfjm
  if (speed == B2500000) baud = 2500000;
418 10 wfjm
  if (speed == B3000000) baud = 3000000;
419 16 wfjm
  if (speed == B3500000) baud = 3500000;
420
  if (speed == B4000000) baud = 4000000;
421 10 wfjm
  os << " speed: " << RosPrintf(baud, "d", 7);
422
  os << endl;
423
 
424
  os << bl << "  c_lflag : " << RosPrintf(tios.c_lflag,"x0",8);
425
  if (tios.c_lflag & ECHO)   os << " ECHO  ";
426
  if (tios.c_lflag & ECHOE)  os << " ECHOE ";
427
  if (tios.c_lflag & ECHOK)  os << " ECHOK ";
428
  if (tios.c_lflag & ECHONL) os << " ECHONL";
429
  if (tios.c_lflag & ICANON) os << " ICANON";
430
  if (tios.c_lflag & IEXTEN) os << " IEXTEN";
431
  if (tios.c_lflag & ISIG)   os << " ISIG  ";
432
  if (tios.c_lflag & NOFLSH) os << " NOFLSH";
433
  if (tios.c_lflag & TOSTOP) os << " TOSTOP";
434
  os << endl;
435
 
436
  os << bl << "  c_cc    : " << endl;
437
  os << bl << "    [VEOF]  : " << RosPrintf(tios.c_cc[VEOF],"o",3);
438
  os       << "    [VEOL]  : " << RosPrintf(tios.c_cc[VEOL],"o",3);
439
  os       << "    [VERASE]: " << RosPrintf(tios.c_cc[VERASE],"o",3);
440
  os       << "    [VINTR] : " << RosPrintf(tios.c_cc[VINTR],"o",3)  << endl;
441
  os << bl << "    [VKILL] : " << RosPrintf(tios.c_cc[VKILL],"o",3);
442
  os       << "    [VQUIT] : " << RosPrintf(tios.c_cc[VQUIT],"o",3);
443
  os       << "    [VSUSP] : " << RosPrintf(tios.c_cc[VSUSP],"o",3);
444
  os       << "    [VSTART]: " << RosPrintf(tios.c_cc[VSTART],"o",3) << endl;
445
  os << bl << "    [VSTOP] : " << RosPrintf(tios.c_cc[VSTOP],"o",3);
446
  os       << "    [VMIN]  : " << RosPrintf(tios.c_cc[VMIN],"o",3);
447
  os       << "    [VTIME] : " << RosPrintf(tios.c_cc[VTIME],"o",3)  << endl;
448
 
449
  return;
450
}
451
 
452
//------------------------------------------+-----------------------------------
453
#if (defined(Retro_NoInline) || defined(Retro_RlinkPortTerm_NoInline))
454
#define inline
455
//#include "RlinkPortTerm.ipp"
456
#undef  inline
457
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.