4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.17 2008/02/28 15:37:06 james
14 * *** empty log message ***
16 * Revision 1.16 2008/02/28 12:12:25 james
17 * *** empty log message ***
19 * Revision 1.15 2008/02/28 00:10:44 james
20 * *** empty log message ***
22 * Revision 1.14 2008/02/23 13:05:58 staffcvs
23 * *** empty log message ***
25 * Revision 1.13 2008/02/23 11:48:37 james
26 * *** empty log message ***
28 * Revision 1.12 2008/02/22 23:39:27 james
29 * *** empty log message ***
31 * Revision 1.11 2008/02/20 18:31:53 james
32 * *** empty log message ***
34 * Revision 1.10 2008/02/15 23:52:12 james
35 * *** empty log message ***
37 * Revision 1.9 2008/02/15 03:32:07 james
38 * *** empty log message ***
40 * Revision 1.8 2008/02/14 10:36:18 james
41 * *** empty log message ***
43 * Revision 1.7 2008/02/14 10:34:30 james
44 * *** empty log message ***
46 * Revision 1.6 2008/02/13 16:59:34 james
47 * *** empty log message ***
49 * Revision 1.5 2008/02/13 16:57:29 james
50 * *** empty log message ***
52 * Revision 1.4 2008/02/12 22:36:46 james
53 * *** empty log message ***
55 * Revision 1.3 2008/02/09 15:47:28 james
56 * *** empty log message ***
64 speed_t_to_baud (speed_t s)
150 baud_to_speed_t (int baud)
235 tty_pre_select (TTY * t, fd_set * rfds, fd_set * wfds)
238 struct timeval now, dif;
243 gettimeofday (&now, NULL);
244 timersub (&now, &t->hangup_clock, &dif);
248 ioctl (t->rfd, TIOCMBIS, &line);
254 FD_SET (t->rfd, rfds);
258 tty_get_status (TTY * t, TTY_Status * s)
262 ioctl (t->rfd, TIOCMGET, &s->lines);
264 if (tcgetattr (t->rfd, &s->termios))
267 s->baud = speed_t_to_baud (cfgetispeed (&s->termios));
268 s->blocked = t->blocked;
274 tty_get_baud (TTY * t)
276 struct termios tios = { 0 };
278 if (tcgetattr (t->rfd, &tios))
281 return speed_t_to_baud (cfgetispeed (&tios));
286 tty_set_baud (TTY * t, int rate)
288 struct termios tios = { 0 };
290 speed_t s = baud_to_speed_t (rate);
292 if (s == (speed_t) - 1)
295 if (tcgetattr (t->rfd, &tios))
298 cfsetispeed (&tios, s);
299 cfsetospeed (&tios, s);
301 tcsetattr (t->rfd, TCSANOW, &tios);
305 tty_send_break (TTY * t)
307 tcsendbreak (t->wfd, 0);
311 tty_set_flow (TTY * t, int flow)
313 struct termios tios = { 0 };
315 if (tcgetattr (t->rfd, &tios))
319 tios.c_cflag |= CRTSCTS;
321 tios.c_cflag &= ~CRTSCTS;
323 tcsetattr (t->rfd, TCSANOW, &tios);
327 void tty_winch(TTY *y,CRT_Pos p)
339 ioctl (t->rfd, TIOCMBIC, &line);
342 gettimeofday (&t->hangup_clock, NULL);
347 void tty_winch(TTY * t,CRT_Pos size)
349 struct winsize sz = { 0 };
354 ioctl (t->wfd, TIOCGWINSZ, &sz);
365 int bit_edge_frequency[8];
372 #define bit(p,b,z,o) \
393 tty_bit_analyse (Context * c, int err, int ch)
397 TTY_Parser *p = c->tp;
402 for (d = 1; d < 0x100; d <<= 1)
404 bit (p, ch & d, zc, oc);
413 gettimeofday (&p->lasterr, NULL);
419 "<tty_bit_analyse: 0%d%d%d%d%d%d%d%d1 [%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]>",
420 ch & 0x01 ? 1 : 0, ch & 0x02 ? 1 : 0, ch & 0x04 ? 1 : 0,
421 ch & 0x08 ? 1 : 0, ch & 0x10 ? 1 : 0, ch & 0x20 ? 1 : 0,
422 ch & 0x40 ? 1 : 0, ch & 0x80 ? 1 : 0, p->bitfreq[0],
423 p->bitfreq[1], p->bitfreq[2], p->bitfreq[3], p->bitfreq[4],
424 p->bitfreq[5], p->bitfreq[6], p->bitfreq[7], p->bitfreq[8],
431 tty_parse_reset (Context * c)
433 TTY_Parser *p = c->tp;
434 memset (p->bitfreq, 0, sizeof (p->bitfreq));
440 tty_analyse (Context * c)
442 TTY_Parser *p = c->tp;
443 struct timeval now, dif;
452 gettimeofday (&now, NULL);
454 timersub (&now, &p->lasterr, &dif);
465 for (i = 0; i < TTY_BITFREQ_LEN; ++i)
467 if (p->bitfreq[i] > max)
475 i = tty_get_baud (c->t);
481 /*Closest bit edge is one bit, so the baud rate is too low */
482 p->guessed_baud = -1;
488 p->guessed_baud = i / j;
494 if (p->guessed_baud == -1)
496 log_f (c->l, "<tty_analyse: %6d errors, current rate %db is too low>",
501 log_f (c->l, "<tty_analyse: %6d errors, current rate %db, suggest %db>",
502 p->biterrs, i, p->guessed_baud);
508 tty_parser_new (void)
512 p = (TTY_Parser *) malloc (sizeof (TTY_Parser));
514 memset (p, 0, sizeof (TTY_Parser));
520 tty_parse (Context * c, uint8_t * buf, int len)
535 tty_bit_analyse (c, 0, *buf);
536 utf8_parse (c, *buf);
542 log_f (c->l, "%s:%d DLE parsing error: \\377 \\%03o", __FILE__,
546 else if (p->in_errmark)
550 log_f (c->l, "<tty reports error: \\377 \\000 \\%03o>",
551 __FILE__, __LINE__, *buf);
553 tty_bit_analyse (c, 1, *buf);
557 utf8_parse (c, *buf);
559 utf8_parse (c, SYM_CHAR_RESET);
562 else if (*buf == DLE)
569 tty_bit_analyse (c, 0, *buf);
573 utf8_parse (c, *buf);