4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.2 2008/02/16 10:58:52 james
15 * *** empty log message ***
17 * Revision 1.13 2008/02/15 23:52:12 james
18 * *** empty log message ***
20 * Revision 1.12 2008/02/15 03:32:07 james
21 * *** empty log message ***
23 * Revision 1.11 2008/02/14 16:21:17 james
24 * *** empty log message ***
26 * Revision 1.10 2008/02/14 10:39:14 james
27 * *** empty log message ***
29 * Revision 1.9 2008/02/14 10:34:47 james
30 * *** empty log message ***
32 * Revision 1.8 2008/02/14 10:34:30 james
33 * *** empty log message ***
35 * Revision 1.7 2008/02/14 02:46:44 james
36 * *** empty log message ***
38 * Revision 1.6 2008/02/14 00:57:58 james
39 * *** empty log message ***
41 * Revision 1.5 2008/02/13 18:05:06 james
42 * *** empty log message ***
44 * Revision 1.4 2008/02/13 17:21:55 james
45 * *** empty log message ***
47 * Revision 1.3 2008/02/08 15:06:52 james
48 * *** empty log message ***
50 * Revision 1.2 2008/02/07 15:42:49 james
51 * *** empty log message ***
53 * Revision 1.1 2008/02/05 14:25:49 james
54 * *** empty log message ***
76 get_status (TTY * t, Clients * cs)
78 static struct timeval last_cd_edge = { 0 };
79 static int last_cd_state = -1;
81 struct timeval now, dif;
83 TTY_Status tty_status = { 0 };
86 tty_get_status (t, &tty_status);
89 status.nclients = cs->n;
90 status.lines = tty_status.lines;
91 status.baud = tty_status.baud;
92 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
93 status.blocked = tty_status.blocked;
95 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
97 if (cd != last_cd_state)
99 gettimeofday (&last_cd_edge, NULL);
103 gettimeofday (&now, NULL);
104 timersub (&now, &last_cd_edge, &dif);
105 status.cd_edge_sec = dif.tv_sec;
157 log_line_changes (Context * ctx, int old, int new)
161 char buf[1024], *ptr = buf;
169 n = "<Modem lines changed:";
180 *(ptr++) = (new & c) ? '+' : '-';
181 n = line_to_name (c);
192 ctx->l->log (ctx->l, buf);
197 do_line (char *ptr, int lines, int line)
203 lname = line_to_name (line);
207 *(ptr++) = *(lname++);
215 check_status (Context * c, Clients * cs)
217 static Status old_status = { 0 };
223 status = get_status (c->t, cs);
224 if (!memcmp (&status, &old_status, sizeof (status)))
229 log_line_changes (c, old_status.lines, status.lines);
231 ptr += sprintf (ptr, "CTRL-B ");
234 if (!strncmp (t, "/dev/", 5))
239 ptr += sprintf (ptr, " %db", status.baud);
241 ptr = do_line (ptr, status.lines, TIOCM_RTS);
242 ptr = do_line (ptr, status.lines, TIOCM_CTS);
243 ptr = do_line (ptr, status.lines, TIOCM_DTR);
244 ptr = do_line (ptr, status.lines, TIOCM_DSR);
245 ptr = do_line (ptr, status.lines, TIOCM_RI);
246 ptr = do_line (ptr, status.lines, TIOCM_CD);
263 if (status.lines & TIOCM_CD)
266 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
267 status.cd_edge_sec % 60);
272 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
273 status.cd_edge_sec % 60);
278 sprintf (ptr, ", %d client%s", status.nclients,
279 (status.nclients == 1) ? "" : "s");
283 send_status (cs, buf);
289 t= ptty_open (NULL, NULL);
290 //t = serial_open ("/dev/cellmodem", 0);
291 l=file_log_new ("log");
294 void mainloop (TTY *tty,Socket *server_socket,Socket *client_socket,Ansi *a,Log *log)
301 c.h = history_new (200);
304 /* are we being fed by a tty or a socket */
306 if (server_socket) abort();
308 c.k = keydis_ipc_new (client_socket);
312 c.k = keydis_vt102_new ();
315 /* do we have an upstream terminal to talk to*/
324 if(client_socket) abort();
325 clients = clients_new ();
332 struct timeval tv = { 1, 0 };
334 check_status (&c, clients);
340 tty_pre_select (c.t, &rfds, &wfds);
343 FD_SET (server_socket->fd, &rfds);
344 clients_pre_select (clients, &rfds, &wfds);
348 socket_pre_select (client_socket, &rfds, &wfds);
350 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
352 /*any message from clients, or new connexions*/
355 if (FD_ISSET (server_socket->fd, &rfds) && ((cs = socket_accept (s))))
359 /*New client connexion */
360 cl = clients_new_client (clients, cs, &c);
362 send_history (c.h, cl);
363 send_vt102 (c.v, cl);
368 clients_post_select (clients, &c, &rfds, &wfds);
371 /*any data from the port*/
372 if (c.t && FD_ISSET (c.t->rfd, &rfds))
374 char buf[IPC_MAX_BUF];
377 red = c.t->recv (c.t, buf, sizeof (buf));
384 send_output (clients, buf, red);
385 vt102_parse (&c, buf, red);
393 clients_shutdown (clients);