dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / tip / aculib / ventel.c
blob85fc2c8b85d03a6248d783721eca45c63ec88292
1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved. The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
12 #pragma ident "%Z%%M% %I% %E% SMI"
15 * Routines for calling up on a Ventel Modem
16 * Define VENNOECHO if the Ventel is strapped for "no echo".
18 #include "tip.h"
20 #define MAXRETRY 5
22 static int vensync(int);
23 static int gobble(char);
24 static void echo(char *);
25 static void sigALRM(void);
26 static int timeout = 0;
27 static sigjmp_buf timeoutbuf;
29 void ven_disconnect(void);
31 /* ARGSUSED */
32 int
33 ven_dialer(char *num, char *acu)
35 char *cp;
36 int connected = 0;
37 struct termios buf;
38 #ifdef ACULOG
39 char line[80];
40 #endif
42 * Get in synch with a couple of carriage returns
44 if (!vensync(FD)) {
45 (void) printf("can't synchronize with ventel\n");
46 #ifdef ACULOG
47 logent(value(HOST), num, "ventel", "can't synch up");
48 #endif
49 return (0);
51 if (boolean(value(VERBOSE)))
52 (void) printf("\ndialing...");
53 (void) fflush(stdout);
54 (void) ioctl(FD, TCGETS, &buf);
55 buf.c_cflag |= HUPCL;
56 (void) ioctl(FD, TCSETSF, &buf);
57 #ifdef VENNOECHO
58 echo("#k$\r$\n$D$I$A$L$:$ ");
59 for (cp = num; *cp; cp++) {
60 (void) sleep(1);
61 (void) write(FD, cp, 1);
63 echo("\r$\n");
64 #else
65 echo("k$\r$\n$D$I$A$L$:$ <");
66 for (cp = num; *cp; cp++) {
67 char c;
69 (void) sleep(1);
70 (void) write(FD, cp, 1);
71 (void) read(FD, &c, 1);
73 echo(">\r$\n");
74 #endif
75 if (gobble('\n'))
76 connected = gobble('!');
77 (void) ioctl(FD, TCFLSH, TCIOFLUSH);
78 #ifdef ACULOG
79 if (timeout) {
80 (void) sprintf(line, "%d second dial timeout",
81 number(value(DIALTIMEOUT)));
82 logent(value(HOST), num, "ventel", line);
84 #endif
85 if (timeout)
86 ven_disconnect(); /* insurance */
87 return (connected);
90 void
91 ven_disconnect(void)
94 (void) close(FD);
97 void
98 ven_abort(void)
101 (void) write(FD, "\03", 1);
102 (void) close(FD);
105 static void
106 echo(char *s)
108 char c;
110 while (c = *s++) {
111 switch (c) {
112 case '$':
113 (void) read(FD, &c, 1);
114 s++;
115 break;
117 case '#':
118 c = *s++;
119 (void) write(FD, &c, 1);
120 break;
122 default:
123 (void) write(FD, &c, 1);
124 (void) read(FD, &c, 1);
129 static void
130 sigALRM(void)
133 (void) printf("\07timeout waiting for reply\n");
134 timeout = 1;
135 siglongjmp(timeoutbuf, 1);
138 static int
139 gobble(char match)
141 char c;
142 sig_handler_t f;
144 f = signal(SIGALRM, (sig_handler_t)sigALRM);
145 timeout = 0;
146 do {
147 if (sigsetjmp(timeoutbuf, 1)) {
148 (void) signal(SIGALRM, f);
149 return (0);
151 (void) alarm(number(value(DIALTIMEOUT)));
152 (void) read(FD, &c, 1);
153 (void) alarm(0);
154 c &= 0177;
155 #ifdef notdef
156 if (boolean(value(VERBOSE)))
157 (void) putchar(c);
158 #endif
159 } while (c != '\n' && c != match);
160 (void) signal(SIGALRM, SIG_DFL);
161 return (c == match);
164 #define min(a, b) (((a) > (b)) ? (b) : (a))
166 * This convoluted piece of code attempts to get
167 * the ventel in sync. If you don't have FIONREAD
168 * there are gory ways to simulate this.
170 static int
171 vensync(int fd)
173 int already = 0, nread;
174 char buf[60];
175 int dtr = TIOCM_DTR;
178 * Toggle DTR to force anyone off that might have left
179 * the modem connected, and insure a consistent state
180 * to start from.
182 * If you don't have the ioctl calls to diddle directly
183 * with DTR, you can always try setting the baud rate to 0.
185 (void) ioctl(FD, TIOCMBIC, &dtr);
186 (void) sleep(2);
187 (void) ioctl(FD, TIOCMBIS, &dtr);
188 while (already < MAXRETRY) {
190 * After reseting the modem, send it two \r's to
191 * autobaud on. Make sure to delay between them
192 * so the modem can frame the incoming characters.
194 (void) write(fd, "\r", 1);
195 #ifdef VMUNIX
197 #include <sys/time.h>
198 struct timeval tv = {0, 500000};
200 (void) select(0, 0, 0, 0, &tv);
202 #else
203 (void) sleep(1);
204 #endif
205 (void) write(fd, "\r", 1);
206 (void) sleep(3);
207 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
208 perror("tip: ioctl");
209 continue;
211 while (nread > 0) {
212 (void) read(fd, buf, min(nread, 60));
213 if ((buf[nread - 1] & 0177) == '$')
214 return (1);
215 nread -= min(nread, 60);
217 (void) sleep(1);
218 already++;
220 return (0);