4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.13 2008/02/23 11:48:37 james
14 * *** empty log message ***
16 * Revision 1.12 2008/02/22 23:39:27 james
17 * *** empty log message ***
19 * Revision 1.11 2008/02/20 18:31:53 james
20 * *** empty log message ***
22 * Revision 1.10 2008/02/15 23:52:12 james
23 * *** empty log message ***
25 * Revision 1.9 2008/02/15 03:32:07 james
26 * *** empty log message ***
28 * Revision 1.8 2008/02/14 10:36:18 james
29 * *** empty log message ***
31 * Revision 1.7 2008/02/14 10:34:30 james
32 * *** empty log message ***
34 * Revision 1.6 2008/02/13 16:59:34 james
35 * *** empty log message ***
37 * Revision 1.5 2008/02/13 16:57:29 james
38 * *** empty log message ***
40 * Revision 1.4 2008/02/12 22:36:46 james
41 * *** empty log message ***
43 * Revision 1.3 2008/02/09 15:47:28 james
44 * *** empty log message ***
52 speed_t_to_baud (speed_t s)
138 baud_to_speed_t (int baud)
223 tty_pre_select (TTY * t, fd_set * rfds, fd_set * wfds)
226 struct timeval now, dif;
231 gettimeofday (&now, NULL);
232 timersub (&now, &t->hangup_clock, &dif);
236 ioctl (t->rfd, TIOCMBIS, &line);
242 FD_SET (t->rfd, rfds);
246 tty_get_status (TTY * t, TTY_Status * s)
250 ioctl (t->rfd, TIOCMGET, &s->lines);
252 if (tcgetattr (t->rfd, &s->termios))
255 s->baud = speed_t_to_baud (cfgetispeed (&s->termios));
256 s->blocked = t->blocked;
262 tty_set_baud (TTY * t, int rate)
264 struct termios tios = { 0 };
266 speed_t s = baud_to_speed_t (rate);
268 if (s == (speed_t) - 1)
271 if (tcgetattr (t->rfd, &tios))
274 cfsetispeed (&tios, s);
275 cfsetospeed (&tios, s);
277 tcsetattr (t->rfd, TCSANOW, &tios);
281 tty_send_break (TTY * t)
283 tcsendbreak (t->wfd, 0);
287 tty_set_flow (TTY * t, int flow)
289 struct termios tios = { 0 };
291 if (tcgetattr (t->rfd, &tios))
295 tios.c_cflag |= CRTSCTS;
297 tios.c_cflag &= ~CRTSCTS;
299 tcsetattr (t->rfd, TCSANOW, &tios);
309 ioctl (t->rfd, TIOCMBIC, &line);
312 gettimeofday (&t->hangup_clock, NULL);
323 int bit_edge_frequency[8];
330 #define bit(p,b,z,o) \
351 tty_bit_analyse (TTY_Parser * p, int err, int ch)
359 gettimeofday (&p->lasterr, NULL);
366 bit (p, ch & c, zc, oc);
373 tty_parse_reset (Context * c)
375 TTY_Parser *p = c->tp;
376 memset (p->bitfreq, 0, sizeof (p->bitfreq));
382 analyse (Context * c)
384 TTY_Parser *p = c->tp;
385 struct timeval now, dif;
394 gettimeofday (&now, NULL);
396 timersub (&now, &p->lasterr, &dif);
404 #define TTY_BITFREQ_LEN 10
406 for (i = 0; i < TTY_BITFREQ_LEN && (!p->bitfreq[i]); ++i);
410 /*Closest bit edge is one bit, so the baud rate is too low */
411 p->guessed_baud = -1;
420 tty_parser_new (void)
424 p = (TTY_Parser *) malloc (sizeof (TTY_Parser));
426 memset (p, 0, sizeof (TTY_Parser));
432 tty_parse (Context * c, uint8_t * buf, int len)
447 tty_bit_analyse (p, 0, *buf);
448 utf8_parse (c, *buf);
454 log_f (c->l, "%s:%d DLE parsing error: \\377 \\%03o", __FILE__,
458 else if (p->in_errmark)
461 log_f (c->l, "%s:%d tty reports error: \\377 \\000 \\%03o",
462 __FILE__, __LINE__, *buf);
465 tty_bit_analyse (p, 1, *buf);
469 utf8_parse (c, *buf);
471 utf8_parse (c, SYM_CHAR_RESET);
474 else if (*buf == DLE)
481 tty_bit_analyse (p, 0, *buf);
485 utf8_parse (c, *buf);