Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / tip / libacu / ventel.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)ventel.c    8.1 (Berkeley) 6/6/93";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.bin/tip/libacu/ventel.c,v 1.3 1999/08/28 01:06:31 peter Exp $";
40 #endif /* not lint */
41
42 /*
43  * Routines for calling up on a Ventel Modem
44  * The Ventel is expected to be strapped for local echo (just like uucp)
45  */
46 #include "tipconf.h"
47 #include "tip.h"
48 #include <err.h>
49
50 #define MAXRETRY        5
51
52 static  void sigALRM();
53 static  int timeout = 0;
54 static  jmp_buf timeoutbuf;
55
56 /*
57  * some sleep calls have been replaced by this macro
58  * because some ventel modems require two <cr>s in less than
59  * a second in order to 'wake up'... yes, it is dirty...
60  */
61 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
62 #define CPUSPEED 1000000        /* VAX 780 is 1MIPS */
63 #define DELAY(n)        { register long N = (n); while (--N > 0); }
64 busyloop(n) { DELAY(n); }
65
66 ven_dialer(num, acu)
67         register char *num;
68         char *acu;
69 {
70         register char *cp;
71         register int connected = 0;
72         char *msg, *index(), line[80];
73         static int gobble(), vensync();
74         static void echo();
75
76         /*
77          * Get in synch with a couple of carriage returns
78          */
79         if (!vensync(FD)) {
80                 printf("can't synchronize with ventel\n");
81 #if ACULOG
82                 logent(value(HOST), num, "ventel", "can't synch up");
83 #endif
84                 return (0);
85         }
86         if (boolean(value(VERBOSE)))
87                 printf("\ndialing...");
88         fflush(stdout);
89         acu_hupcl ();
90         echo("#k$\r$\n$D$I$A$L$:$ ");
91         for (cp = num; *cp; cp++) {
92                 delay(1, 10);
93                 write(FD, cp, 1);
94         }
95         delay(1, 10);
96         write(FD, "\r", 1);
97         gobble('\n', line);
98         if (gobble('\n', line))
99                 connected = gobble('!', line);
100         acu_flush ();
101 #if ACULOG
102         if (timeout) {
103                 sprintf(line, "%d second dial timeout",
104                         number(value(DIALTIMEOUT)));
105                 logent(value(HOST), num, "ventel", line);
106         }
107 #endif
108         if (timeout)
109                 ven_disconnect();       /* insurance */
110         if (connected || timeout || !boolean(value(VERBOSE)))
111                 return (connected);
112         /* call failed, parse response for user */
113         cp = index(line, '\r');
114         if (cp)
115                 *cp = '\0';
116         for (cp = line; cp = index(cp, ' '); cp++)
117                 if (cp[1] == ' ')
118                         break;
119         if (cp) {
120                 while (*cp == ' ')
121                         cp++;
122                 msg = cp;
123                 while (*cp) {
124                         if (isupper(*cp))
125                                 *cp = tolower(*cp);
126                         cp++;
127                 }
128                 printf("%s...", msg);
129         }
130         return (connected);
131 }
132
133 ven_disconnect()
134 {
135
136         close(FD);
137 }
138
139 ven_abort()
140 {
141
142         write(FD, "\03", 1);
143         close(FD);
144 }
145
146 static void
147 echo(s)
148         register char *s;
149 {
150         char c;
151
152         while (c = *s++) switch (c) {
153
154         case '$':
155                 read(FD, &c, 1);
156                 s++;
157                 break;
158
159         case '#':
160                 c = *s++;
161                 write(FD, &c, 1);
162                 break;
163
164         default:
165                 write(FD, &c, 1);
166                 read(FD, &c, 1);
167         }
168 }
169
170 static void
171 sigALRM()
172 {
173         printf("\07timeout waiting for reply\n");
174         timeout = 1;
175         longjmp(timeoutbuf, 1);
176 }
177
178 static int
179 gobble(match, response)
180         register char match;
181         char response[];
182 {
183         register char *cp = response;
184         sig_t f;
185         char c;
186
187         f = signal(SIGALRM, sigALRM);
188         timeout = 0;
189         do {
190                 if (setjmp(timeoutbuf)) {
191                         signal(SIGALRM, f);
192                         *cp = '\0';
193                         return (0);
194                 }
195                 alarm(number(value(DIALTIMEOUT)));
196                 read(FD, cp, 1);
197                 alarm(0);
198                 c = (*cp++ &= 0177);
199 #ifdef notdef
200                 if (boolean(value(VERBOSE)))
201                         putchar(c);
202 #endif
203         } while (c != '\n' && c != match);
204         signal(SIGALRM, SIG_DFL);
205         *cp = '\0';
206         return (c == match);
207 }
208
209 #define min(a,b)        ((a)>(b)?(b):(a))
210 /*
211  * This convoluted piece of code attempts to get
212  * the ventel in sync.  If you don't have FIONREAD
213  * there are gory ways to simulate this.
214  */
215 static int
216 vensync(fd)
217 {
218         int already = 0, nread;
219         char buf[60];
220
221         /*
222          * Toggle DTR to force anyone off that might have left
223          * the modem connected, and insure a consistent state
224          * to start from.
225          *
226          * If you don't have the ioctl calls to diddle directly
227          * with DTR, you can always try setting the baud rate to 0.
228          */
229         ioctl(FD, TIOCCDTR, 0);
230         sleep(1);
231         ioctl(FD, TIOCSDTR, 0);
232         while (already < MAXRETRY) {
233                 /*
234                  * After reseting the modem, send it two \r's to
235                  * autobaud on. Make sure to delay between them
236                  * so the modem can frame the incoming characters.
237                  */
238                 write(fd, "\r", 1);
239                 delay(1,10);
240                 write(fd, "\r", 1);
241                 sleep(2);
242                 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
243                         warn("ioctl");
244                         continue;
245                 }
246                 while (nread > 0) {
247                         read(fd, buf, min(nread, 60));
248                         if ((buf[nread - 1] & 0177) == '$')
249                                 return (1);
250                         nread -= min(nread, 60);
251                 }
252                 sleep(1);
253                 already++;
254         }
255         return (0);
256 }
257