4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.11 2008/02/14 16:21:17 james
15 * *** empty log message ***
17 * Revision 1.10 2008/02/14 10:39:14 james
18 * *** empty log message ***
20 * Revision 1.9 2008/02/14 10:34:47 james
21 * *** empty log message ***
23 * Revision 1.8 2008/02/14 10:34:30 james
24 * *** empty log message ***
26 * Revision 1.7 2008/02/14 02:46:44 james
27 * *** empty log message ***
29 * Revision 1.6 2008/02/14 00:57:58 james
30 * *** empty log message ***
32 * Revision 1.5 2008/02/13 18:05:06 james
33 * *** empty log message ***
35 * Revision 1.4 2008/02/13 17:21:55 james
36 * *** empty log message ***
38 * Revision 1.3 2008/02/08 15:06:52 james
39 * *** empty log message ***
41 * Revision 1.2 2008/02/07 15:42:49 james
42 * *** empty log message ***
44 * Revision 1.1 2008/02/05 14:25:49 james
45 * *** empty log message ***
67 get_status (TTY * t, Clients * cs)
69 static struct timeval last_cd_edge = { 0 };
70 static int last_cd_state = -1;
72 struct timeval now, dif;
74 TTY_Status tty_status = { 0 };
77 tty_get_status (t, &tty_status);
80 status.nclients = cs->n;
81 status.lines = tty_status.lines;
82 status.baud = tty_status.baud;
83 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
84 status.blocked=tty_status.blocked;
86 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
88 if (cd != last_cd_state)
90 gettimeofday (&last_cd_edge, NULL);
94 gettimeofday (&now, NULL);
95 timersub (&now, &last_cd_edge, &dif);
96 status.cd_edge_sec = dif.tv_sec;
148 log_line_changes (Context * ctx, int old, int new)
152 char buf[1024], *ptr = buf;
160 n = "<Modem lines changed:";
171 *(ptr++) = (new & c) ? '+' : '-';
172 n = line_to_name (c);
183 ctx->l->log (ctx->l, buf);
188 do_line (char *ptr, int lines, int line)
194 lname = line_to_name (line);
197 *(ptr++) = *(lname++);
205 check_status (Context * c, Clients * cs)
207 static Status old_status = { 0 };
213 status = get_status (c->t, cs);
214 if (!memcmp (&status, &old_status, sizeof (status)))
219 log_line_changes (c, old_status.lines, status.lines);
222 if (!strncmp (t, "/dev/", 5))
227 ptr += sprintf (ptr, " %db", status.baud);
229 ptr = do_line (ptr, status.lines, TIOCM_RTS);
230 ptr = do_line (ptr, status.lines, TIOCM_CTS);
231 ptr = do_line (ptr, status.lines, TIOCM_DTR);
232 ptr = do_line (ptr, status.lines, TIOCM_DSR);
233 ptr = do_line (ptr, status.lines, TIOCM_RI);
249 if (status.lines & TIOCM_CD)
252 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
253 status.cd_edge_sec % 60);
258 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
259 status.cd_edge_sec % 60);
263 sprintf (ptr, ", %d client%s", status.nclients,
264 (status.nclients == 1) ? "" : "s");
268 send_status (cs, buf);
272 main (int argc, char *argv[])
280 construct_possible_lock_files("/dev/modem");
283 s = socket_listen ("socket");
285 c.t = ptty_open (NULL, NULL);
287 c.h = history_new (200);
288 c.l = file_log_new ("log");
289 c.k = keydis_vt102_new (&c);
292 clients = clients_new ();
296 struct timeval tv = { 1, 0 };
298 check_status (&c, clients);
303 tty_pre_select (c.t, &rfds, &wfds);
305 FD_SET (s->fd, &rfds);
307 socket_pre_select (s, &rfds, &wfds);
309 clients_pre_select (clients, &rfds, &wfds);
311 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
313 if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
317 /*New client connexion */
318 cl = clients_new_client (clients, cs, &c);
320 send_history (c.h, cl);
321 send_vt102 (c.v, cl);
327 clients_post_select (clients, &c, &rfds, &wfds);
329 if (FD_ISSET (c.t->rfd, &rfds))
331 char buf[IPC_MAX_BUF];
334 red = c.t->recv (c.t, buf, sizeof (buf));
341 send_output (clients, buf, red);
342 vt102_parse (&c, buf, red);
350 clients_shutdown (clients);