xref: /titanic_41/usr/src/cmd/tip/aculib/v3451.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
1 /*
2  * Copyright 2000 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
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.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 /*
15  * Routines for calling up on a Vadic 3451 Modem
16  */
17 #include "tip.h"
18 
19 static int	expect(char *);
20 static int	notin(char *, char *);
21 static int	prefix(char *, char *);
22 static void	vawrite(char *, int);
23 static void	alarmtr(void);
24 
25 static sigjmp_buf	Sjbuf;
26 
27 /* ARGSUSED */
28 int
29 v3451_dialer(char *num, char *acu)
30 {
31 	int ok;
32 	sig_handler_t	func;
33 	struct termios buf;
34 	int slow = number(value(BAUDRATE)) < 1200;
35 	char phone[50];
36 #ifdef ACULOG
37 	char line[80];
38 #endif
39 
40 	/*
41 	 * Get in synch
42 	 */
43 	vawrite("I\r", 1 + slow);
44 	vawrite("I\r", 1 + slow);
45 	vawrite("I\r", 1 + slow);
46 	vawrite("\005\r", 2 + slow);
47 	if (!expect("READY")) {
48 		(void) printf("can't synchronize with vadic 3451\n");
49 #ifdef ACULOG
50 		logent(value(HOST), num, "vadic", "can't synch up");
51 #endif
52 		return (0);
53 	}
54 	(void) ioctl(FD, TCGETS, &buf);
55 	buf.c_cflag |= HUPCL;
56 	(void) ioctl(FD, TCSETSF, &buf);
57 	(void) sleep(1);
58 	vawrite("D\r", 2 + slow);
59 	if (!expect("NUMBER?")) {
60 		(void) printf("Vadic will not accept dial command\n");
61 #ifdef ACULOG
62 		logent(value(HOST), num, "vadic", "will not accept dial");
63 #endif
64 		return (0);
65 	}
66 	(void) strlcpy(phone, num, sizeof (phone));
67 	(void) strlcat(phone, "\r", sizeof (phone));
68 	vawrite(phone, 1 + slow);
69 	if (!expect(phone)) {
70 		(void) printf("Vadic will not accept phone number\n");
71 #ifdef ACULOG
72 		logent(value(HOST), num, "vadic", "will not accept number");
73 #endif
74 		return (0);
75 	}
76 	func = signal(SIGINT, SIG_IGN);
77 	/*
78 	 * You cannot interrupt the Vadic when its dialing;
79 	 * even dropping DTR does not work (definitely a
80 	 * brain damaged design).
81 	 */
82 	vawrite("\r", 1 + slow);
83 	vawrite("\r", 1 + slow);
84 	if (!expect("DIALING:")) {
85 		(void) printf("Vadic failed to dial\n");
86 #ifdef ACULOG
87 		logent(value(HOST), num, "vadic", "failed to dial");
88 #endif
89 		return (0);
90 	}
91 	if (boolean(value(VERBOSE)))
92 		(void) printf("\ndialing...");
93 	ok = expect("ON LINE");
94 	(void) signal(SIGINT, func);
95 	if (!ok) {
96 		(void) printf("call failed\n");
97 #ifdef ACULOG
98 		logent(value(HOST), num, "vadic", "call failed");
99 #endif
100 		return (0);
101 	}
102 	(void) ioctl(FD, TCFLSH, TCOFLUSH);
103 	return (1);
104 }
105 
106 void
107 v3451_disconnect(void)
108 {
109 
110 	(void) close(FD);
111 }
112 
113 void
114 v3451_abort(void)
115 {
116 
117 	(void) close(FD);
118 }
119 
120 static void
121 vawrite(char *cp, int delay)
122 {
123 
124 	for (; *cp; (void) sleep(delay), cp++)
125 		(void) write(FD, cp, 1);
126 }
127 
128 static int
129 expect(char *cp)
130 {
131 	char buf[300];
132 	char *rp = buf;
133 	int timeout = 30, online = 0;
134 
135 	if (strcmp(cp, "\"\"") == 0)
136 		return (1);
137 	*rp = 0;
138 	/*
139 	 * If we are waiting for the Vadic to complete
140 	 * dialing and get a connection, allow more time
141 	 * Unfortunately, the Vadic times out 24 seconds after
142 	 * the last digit is dialed
143 	 */
144 	online = strcmp(cp, "ON LINE") == 0;
145 	if (online)
146 		timeout = number(value(DIALTIMEOUT));
147 	(void) signal(SIGALRM, (sig_handler_t)alarmtr);
148 	if (sigsetjmp(Sjbuf, 1))
149 		return (0);
150 	(void) alarm(timeout);
151 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
152 		if (online && notin("FAILED CALL", buf) == 0)
153 			return (0);
154 		if (read(FD, rp, 1) < 0) {
155 			(void) alarm(0);
156 			return (0);
157 		}
158 		if (*rp &= 0177)
159 			rp++;
160 		*rp = '\0';
161 	}
162 	(void) alarm(0);
163 	return (1);
164 }
165 
166 static void
167 alarmtr(void)
168 {
169 
170 	siglongjmp(Sjbuf, 1);
171 }
172 
173 static int
174 notin(char *sh, char *lg)
175 {
176 
177 	for (; *lg; lg++)
178 		if (prefix(sh, lg))
179 			return (0);
180 	return (1);
181 }
182 
183 static int
184 prefix(char *s1, char *s2)
185 {
186 	char c;
187 
188 	while ((c = *s1++) == *s2++)
189 		if (c == '\0')
190 			return (1);
191 	return (c == '\0');
192 }
193