4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.13 2008/02/15 23:52:12 james
15 * *** empty log message ***
17 * Revision 1.12 2008/02/15 03:32:07 james
18 * *** empty log message ***
20 * Revision 1.11 2008/02/14 16:21:17 james
21 * *** empty log message ***
23 * Revision 1.10 2008/02/14 10:39:14 james
24 * *** empty log message ***
26 * Revision 1.9 2008/02/14 10:34:47 james
27 * *** empty log message ***
29 * Revision 1.8 2008/02/14 10:34:30 james
30 * *** empty log message ***
32 * Revision 1.7 2008/02/14 02:46:44 james
33 * *** empty log message ***
35 * Revision 1.6 2008/02/14 00:57:58 james
36 * *** empty log message ***
38 * Revision 1.5 2008/02/13 18:05:06 james
39 * *** empty log message ***
41 * Revision 1.4 2008/02/13 17:21:55 james
42 * *** empty log message ***
44 * Revision 1.3 2008/02/08 15:06:52 james
45 * *** empty log message ***
47 * Revision 1.2 2008/02/07 15:42:49 james
48 * *** empty log message ***
50 * Revision 1.1 2008/02/05 14:25:49 james
51 * *** empty log message ***
73 get_status (TTY * t, Clients * cs)
75 static struct timeval last_cd_edge = { 0 };
76 static int last_cd_state = -1;
78 struct timeval now, dif;
80 TTY_Status tty_status = { 0 };
83 tty_get_status (t, &tty_status);
86 status.nclients = cs->n;
87 status.lines = tty_status.lines;
88 status.baud = tty_status.baud;
89 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
90 status.blocked = tty_status.blocked;
92 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
94 if (cd != last_cd_state)
96 gettimeofday (&last_cd_edge, NULL);
100 gettimeofday (&now, NULL);
101 timersub (&now, &last_cd_edge, &dif);
102 status.cd_edge_sec = dif.tv_sec;
154 log_line_changes (Context * ctx, int old, int new)
158 char buf[1024], *ptr = buf;
166 n = "<Modem lines changed:";
177 *(ptr++) = (new & c) ? '+' : '-';
178 n = line_to_name (c);
189 ctx->l->log (ctx->l, buf);
194 do_line (char *ptr, int lines, int line)
200 lname = line_to_name (line);
204 *(ptr++) = *(lname++);
212 check_status (Context * c, Clients * cs)
214 static Status old_status = { 0 };
220 status = get_status (c->t, cs);
221 if (!memcmp (&status, &old_status, sizeof (status)))
226 log_line_changes (c, old_status.lines, status.lines);
228 ptr += sprintf (ptr, "CTRL-B ");
231 if (!strncmp (t, "/dev/", 5))
236 ptr += sprintf (ptr, " %db", status.baud);
238 ptr = do_line (ptr, status.lines, TIOCM_RTS);
239 ptr = do_line (ptr, status.lines, TIOCM_CTS);
240 ptr = do_line (ptr, status.lines, TIOCM_DTR);
241 ptr = do_line (ptr, status.lines, TIOCM_DSR);
242 ptr = do_line (ptr, status.lines, TIOCM_RI);
243 ptr = do_line (ptr, status.lines, TIOCM_CD);
260 if (status.lines & TIOCM_CD)
263 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
264 status.cd_edge_sec % 60);
269 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
270 status.cd_edge_sec % 60);
275 sprintf (ptr, ", %d client%s", status.nclients,
276 (status.nclients == 1) ? "" : "s");
280 send_status (cs, buf);
284 main (int argc, char *argv[])
293 construct_possible_lock_files ("/dev/modem");
297 s = socket_listen ("socket");
299 // c.t = ptty_open (NULL, NULL);
300 c.t = serial_open ("/dev/cellmodem", 0);
302 c.h = history_new (200);
303 c.l = file_log_new ("log");
304 c.k = keydis_vt102_new ();
308 clients = clients_new ();
312 struct timeval tv = { 1, 0 };
314 check_status (&c, clients);
319 tty_pre_select (c.t, &rfds, &wfds);
321 FD_SET (s->fd, &rfds);
323 socket_pre_select (s, &rfds, &wfds);
325 clients_pre_select (clients, &rfds, &wfds);
327 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
329 if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
333 /*New client connexion */
334 cl = clients_new_client (clients, cs, &c);
336 send_history (c.h, cl);
337 send_vt102 (c.v, cl);
343 clients_post_select (clients, &c, &rfds, &wfds);
345 if (FD_ISSET (c.t->rfd, &rfds))
347 char buf[IPC_MAX_BUF];
350 red = c.t->recv (c.t, buf, sizeof (buf));
357 send_output (clients, buf, red);
358 vt102_parse (&c, buf, red);
366 clients_shutdown (clients);