4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.12 2008/02/15 03:32:07 james
15 * *** empty log message ***
17 * Revision 1.11 2008/02/14 16:21:17 james
18 * *** empty log message ***
20 * Revision 1.10 2008/02/14 10:39:14 james
21 * *** empty log message ***
23 * Revision 1.9 2008/02/14 10:34:47 james
24 * *** empty log message ***
26 * Revision 1.8 2008/02/14 10:34:30 james
27 * *** empty log message ***
29 * Revision 1.7 2008/02/14 02:46:44 james
30 * *** empty log message ***
32 * Revision 1.6 2008/02/14 00:57:58 james
33 * *** empty log message ***
35 * Revision 1.5 2008/02/13 18:05:06 james
36 * *** empty log message ***
38 * Revision 1.4 2008/02/13 17:21:55 james
39 * *** empty log message ***
41 * Revision 1.3 2008/02/08 15:06:52 james
42 * *** empty log message ***
44 * Revision 1.2 2008/02/07 15:42:49 james
45 * *** empty log message ***
47 * Revision 1.1 2008/02/05 14:25:49 james
48 * *** empty log message ***
70 get_status (TTY * t, Clients * cs)
72 static struct timeval last_cd_edge = { 0 };
73 static int last_cd_state = -1;
75 struct timeval now, dif;
77 TTY_Status tty_status = { 0 };
80 tty_get_status (t, &tty_status);
83 status.nclients = cs->n;
84 status.lines = tty_status.lines;
85 status.baud = tty_status.baud;
86 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
87 status.blocked = tty_status.blocked;
89 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
91 if (cd != last_cd_state)
93 gettimeofday (&last_cd_edge, NULL);
97 gettimeofday (&now, NULL);
98 timersub (&now, &last_cd_edge, &dif);
99 status.cd_edge_sec = dif.tv_sec;
151 log_line_changes (Context * ctx, int old, int new)
155 char buf[1024], *ptr = buf;
163 n = "<Modem lines changed:";
174 *(ptr++) = (new & c) ? '+' : '-';
175 n = line_to_name (c);
186 ctx->l->log (ctx->l, buf);
191 do_line (char *ptr, int lines, int line)
197 lname = line_to_name (line);
200 *(ptr++) = *(lname++);
208 check_status (Context * c, Clients * cs)
210 static Status old_status = { 0 };
216 status = get_status (c->t, cs);
217 if (!memcmp (&status, &old_status, sizeof (status)))
222 log_line_changes (c, old_status.lines, status.lines);
224 ptr += sprintf (ptr, "CTRL-B ");
227 if (!strncmp (t, "/dev/", 5))
232 ptr += sprintf (ptr, " %db", status.baud);
234 ptr = do_line (ptr, status.lines, TIOCM_RTS);
235 ptr = do_line (ptr, status.lines, TIOCM_CTS);
236 ptr = do_line (ptr, status.lines, TIOCM_DTR);
237 ptr = do_line (ptr, status.lines, TIOCM_DSR);
238 ptr = do_line (ptr, status.lines, TIOCM_RI);
239 ptr = do_line (ptr, status.lines, TIOCM_CD);
256 if (status.lines & TIOCM_CD)
259 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
260 status.cd_edge_sec % 60);
265 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
266 status.cd_edge_sec % 60);
271 sprintf (ptr, ", %d client%s", status.nclients,
272 (status.nclients == 1) ? "" : "s");
276 send_status (cs, buf);
280 main (int argc, char *argv[])
289 construct_possible_lock_files ("/dev/modem");
293 s = socket_listen ("socket");
295 c.t = ptty_open (NULL, NULL);
297 c.h = history_new (200);
298 c.l = file_log_new ("log");
299 c.k = keydis_vt102_new ();
303 clients = clients_new ();
307 struct timeval tv = { 1, 0 };
309 check_status (&c, clients);
314 tty_pre_select (c.t, &rfds, &wfds);
316 FD_SET (s->fd, &rfds);
318 socket_pre_select (s, &rfds, &wfds);
320 clients_pre_select (clients, &rfds, &wfds);
322 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
324 if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
328 /*New client connexion */
329 cl = clients_new_client (clients, cs, &c);
331 send_history (c.h, cl);
332 send_vt102 (c.v, cl);
338 clients_post_select (clients, &c, &rfds, &wfds);
340 if (FD_ISSET (c.t->rfd, &rfds))
342 char buf[IPC_MAX_BUF];
345 red = c.t->recv (c.t, buf, sizeof (buf));
352 send_output (clients, buf, red);
353 vt102_parse (&c, buf, red);
361 clients_shutdown (clients);