1 #ident "$Id: ltest.c,v 1.6 2002/11/23 20:35:50 gert Exp $ Copyright (c) Gert Doering"
2
3 /* ltest.c
4 *
5 * show status of all the RS232 lines (RTS, CTS, ...)
6 * Calls routines in io.c, tio.c
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <time.h>
14
15 #include "mgetty.h"
16 #include "tio.h"
17
18 char * Device;
19 int delay_time = 0; /* in milliseconds, 0 = one-shot */
20
21 /* we don't want logging here */
22 #ifdef USE_VARARGS
lprintf()23 int lprintf() { return 0; }
24 #else
lprintf(int level,const char * format,...)25 int lprintf(int level, const char *format, ...) { return 0; }
26 #endif
lputs(int level,char * string)27 int lputs( int level, char * string ) { return 0; }
28
main(int argc,char ** argv)29 int main( int argc, char ** argv )
30 {
31 int opt, fd, f, last_f;
32 time_t ti;
33 struct tm * tm;
34 boolean opt_delta = FALSE; /* show only deltas */
35 boolean opt_keyboard = FALSE; /* read commands from keyboard */
36 TIO tio, save_tio; /* for stdin */
37
38 while ((opt = getopt(argc, argv, "i:m:dk")) != EOF)
39 {
40 switch( opt )
41 {
42 case 'i': delay_time = 1000 * atoi(optarg); break; /* secs */
43 case 'm': delay_time = atoi(optarg); break; /* msecs */
44 case 'd': opt_delta = TRUE; break;
45 case 'k': opt_keyboard = TRUE; break;
46 default:
47 fprintf( stderr, "Valid options: -i <seconds-delay>, -m <msec-delay>, -d, -k\n" ); exit(7);
48 }
49 }
50
51 if ( optind < argc ) /* argument == tty to use */
52 {
53 Device = argv[optind++];
54 fd = open( Device, O_RDONLY | O_NDELAY );
55
56 if ( fd < 0 )
57 { perror( "Opening device failed" ); exit(17); }
58
59 fcntl( fd, F_SETFL, O_RDONLY );
60 }
61 else /* default: use stdin */
62 {
63 Device = "stdin"; fd = fileno(stdin);
64 }
65
66 /* if input from keyboard allowed, set stdin to "raw"
67 */
68 if ( opt_keyboard )
69 {
70 if ( fd == fileno(stdin) )
71 {
72 fprintf( stderr, "can't read modem + keyboard data from stdin\n");
73 exit(7);
74 }
75 if ( tio_get( 0, &tio ) == ERROR )
76 {
77 fprintf( stderr, "can't read termios settings for keyboard\n" );
78 exit(1);
79 }
80 save_tio = tio;
81 tio_mode_raw( &tio );
82 if ( tio_set( 0, &tio ) == ERROR )
83 {
84 fprintf( stderr, "can't set termios settings for keyboard\n" );
85 exit(2);
86 }
87 printf( "keyboard active. Press 'D' to change DTR, 'R' to change RTS.\r\n" );
88 }
89
90 last_f = -1;
91 do
92 {
93 f = tio_get_rs232_lines( fd );
94
95 if ( f == -1 )
96 {
97 printf( "%s: can't read RS232 line status (-1)\n", Device );
98 exit(17);
99 }
100
101 /* display data only if something changed *or* if (not opt_delta)
102 */
103 if ( ! opt_delta || f != last_f )
104 {
105 ti = time(NULL);
106 tm = localtime(&ti);
107 printf( "%s, %02d:%02d:%02d: active lines:", Device,
108 tm->tm_hour, tm->tm_min, tm->tm_sec );
109
110 if ( f & TIO_F_DTR ) printf( " DTR" );
111 if ( f & TIO_F_DSR ) printf( " DSR" );
112 if ( f & TIO_F_RTS ) printf( " RTS" );
113 if ( f & TIO_F_CTS ) printf( " CTS" );
114 if ( f & TIO_F_DCD ) printf( " DCD" );
115 if ( f & TIO_F_RI ) printf( " RI" );
116
117 if ( f == 0 ) printf( " <none>" );
118
119 printf( "\r\n" );
120
121 last_f = f;
122 }
123
124 if ( delay_time ) delay( delay_time );
125
126 /* check keyboard */
127 while ( opt_keyboard && check_for_input(0) )
128 {
129 char ch;
130 if ( read( 0, &ch, 1 ) != 1 )
131 {
132 fprintf( stderr, "error reading from keyboard, abort.\n");
133 delay_time=0; break;
134 }
135 switch(ch)
136 {
137 case 'd': /* toggle DTR */
138 case 'D':
139 tio_set_rs232_lines( fd, last_f & TIO_F_DTR? 0: 1, -1 );
140 break;
141 case 'r': /* toggle RTS */
142 case 'R':
143 tio_set_rs232_lines( fd, -1, last_f & TIO_F_RTS? 0: 1 );
144 break;
145 case 3: /* exit */
146 case 'q':
147 case 'x':
148 delay_time = 0; break;
149 }
150 }
151 }
152 while( delay_time );
153
154 if ( opt_keyboard ) /* reset termios settings */
155 tio_set( 0, &save_tio );
156
157 return 0;
158 }
159
160