4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.14 2008/02/23 13:05:58 staffcvs
14 * *** empty log message ***
16 * Revision 1.13 2008/02/23 11:48:37 james
17 * *** empty log message ***
19 * Revision 1.12 2008/02/22 23:39:27 james
20 * *** empty log message ***
22 * Revision 1.11 2008/02/20 18:31:53 james
23 * *** empty log message ***
25 * Revision 1.10 2008/02/15 23:52:12 james
26 * *** empty log message ***
28 * Revision 1.9 2008/02/15 03:32:07 james
29 * *** empty log message ***
31 * Revision 1.8 2008/02/14 10:36:18 james
32 * *** empty log message ***
34 * Revision 1.7 2008/02/14 10:34:30 james
35 * *** empty log message ***
37 * Revision 1.6 2008/02/13 16:59:34 james
38 * *** empty log message ***
40 * Revision 1.5 2008/02/13 16:57:29 james
41 * *** empty log message ***
43 * Revision 1.4 2008/02/12 22:36:46 james
44 * *** empty log message ***
46 * Revision 1.3 2008/02/09 15:47:28 james
47 * *** empty log message ***
55 speed_t_to_baud (speed_t s)
141 baud_to_speed_t (int baud)
226 tty_pre_select (TTY * t, fd_set * rfds, fd_set * wfds)
229 struct timeval now, dif;
234 gettimeofday (&now, NULL);
235 timersub (&now, &t->hangup_clock, &dif);
239 ioctl (t->rfd, TIOCMBIS, &line);
245 FD_SET (t->rfd, rfds);
249 tty_get_status (TTY * t, TTY_Status * s)
253 ioctl (t->rfd, TIOCMGET, &s->lines);
255 if (tcgetattr (t->rfd, &s->termios))
258 s->baud = speed_t_to_baud (cfgetispeed (&s->termios));
259 s->blocked = t->blocked;
265 tty_get_baud (TTY * t)
267 struct termios tios = { 0 };
269 if (tcgetattr (t->rfd, &tios))
272 return speed_t_to_baud (cfgetispeed (&tios));
277 tty_set_baud (TTY * t, int rate)
279 struct termios tios = { 0 };
281 speed_t s = baud_to_speed_t (rate);
283 if (s == (speed_t) - 1)
286 if (tcgetattr (t->rfd, &tios))
289 cfsetispeed (&tios, s);
290 cfsetospeed (&tios, s);
292 tcsetattr (t->rfd, TCSANOW, &tios);
296 tty_send_break (TTY * t)
298 tcsendbreak (t->wfd, 0);
302 tty_set_flow (TTY * t, int flow)
304 struct termios tios = { 0 };
306 if (tcgetattr (t->rfd, &tios))
310 tios.c_cflag |= CRTSCTS;
312 tios.c_cflag &= ~CRTSCTS;
314 tcsetattr (t->rfd, TCSANOW, &tios);
324 ioctl (t->rfd, TIOCMBIC, &line);
327 gettimeofday (&t->hangup_clock, NULL);
338 int bit_edge_frequency[8];
345 #define bit(p,b,z,o) \
366 tty_bit_analyse (Context * c, int err, int ch)
370 TTY_Parser *p = c->tp;
375 for (d = 1; d < 0x100; d <<= 1)
377 bit (p, ch & d, zc, oc);
386 gettimeofday (&p->lasterr, NULL);
392 "<tty_bit_analyse: 0%d%d%d%d%d%d%d%d1 [%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]>",
393 ch & 0x01 ? 1 : 0, ch & 0x02 ? 1 : 0, ch & 0x04 ? 1 : 0,
394 ch & 0x08 ? 1 : 0, ch & 0x10 ? 1 : 0, ch & 0x20 ? 1 : 0,
395 ch & 0x40 ? 1 : 0, ch & 0x80 ? 1 : 0, p->bitfreq[0],
396 p->bitfreq[1], p->bitfreq[2], p->bitfreq[3], p->bitfreq[4],
397 p->bitfreq[5], p->bitfreq[6], p->bitfreq[7], p->bitfreq[8],
404 tty_parse_reset (Context * c)
406 TTY_Parser *p = c->tp;
407 memset (p->bitfreq, 0, sizeof (p->bitfreq));
413 tty_analyse (Context * c)
415 TTY_Parser *p = c->tp;
416 struct timeval now, dif;
425 gettimeofday (&now, NULL);
427 timersub (&now, &p->lasterr, &dif);
438 for (i = 0; i < TTY_BITFREQ_LEN; ++i)
440 if (p->bitfreq[i] > max)
448 i = tty_get_baud (c->t);
454 /*Closest bit edge is one bit, so the baud rate is too low */
455 p->guessed_baud = -1;
461 p->guessed_baud = i / j;
467 if (p->guessed_baud == -1)
469 log_f (c->l, "<tty_analyse: %6d errors, current rate %db is too low>",
474 log_f (c->l, "<tty_analyse: %6d errors, current rate %db, suggest %db>",
475 p->biterrs, i, p->guessed_baud);
481 tty_parser_new (void)
485 p = (TTY_Parser *) malloc (sizeof (TTY_Parser));
487 memset (p, 0, sizeof (TTY_Parser));
493 tty_parse (Context * c, uint8_t * buf, int len)
508 tty_bit_analyse (c, 0, *buf);
509 utf8_parse (c, *buf);
515 log_f (c->l, "%s:%d DLE parsing error: \\377 \\%03o", __FILE__,
519 else if (p->in_errmark)
523 log_f (c->l, "%s:%d tty reports error: \\377 \\000 \\%03o",
524 __FILE__, __LINE__, *buf);
526 tty_bit_analyse (c, 1, *buf);
530 utf8_parse (c, *buf);
532 utf8_parse (c, SYM_CHAR_RESET);
535 else if (*buf == DLE)
542 tty_bit_analyse (c, 0, *buf);
546 utf8_parse (c, *buf);