xref: /original-bsd/usr.bin/uucp/libacu/hys.c (revision f91c1509)
1 #ifndef lint
2 static char sccsid[] = "@(#)hys.c	4.8 (Berkeley) 03/02/91";
3 #endif
4 
5 #include "condevs.h"
6 
7 #ifdef USR2400
8 #define DROPDTR
9 /*
10  * The "correct" switch settings for a USR Courier 2400 are
11  * 	Dialin/out:	0 0 1 1 0 0 0 1 0 0
12  *	Dialout only:	0 0 1 1 1 1 0 1 0 0
13  * where 0 = off and 1 = on
14  */
15 #endif USR2400
16 
17 /*
18  *	hyspopn(telno, flds, dev) connect to hayes smartmodem (pulse call)
19  *	hystopn(telno, flds, dev) connect to hayes smartmodem (tone call)
20  *	char *flds[], *dev[];
21  *
22  *	return codes:
23  *		>0  -  file number  -  ok
24  *		CF_DIAL,CF_DEVICE  -  failed
25  */
26 
27 hyspopn(telno, flds, dev)
28 char *telno, *flds[];
29 struct Devices *dev;
30 {
31 	return hysopn(telno, flds, dev, 0);
32 }
33 
34 hystopn(telno, flds, dev)
35 char *telno, *flds[];
36 struct Devices *dev;
37 {
38 	return hysopn(telno, flds, dev, 1);
39 }
40 
41 /* ARGSUSED */
42 hysopn(telno, flds, dev, toneflag)
43 char *telno;
44 char *flds[];
45 struct Devices *dev;
46 int toneflag;
47 {
48 	extern errno;
49 	char dcname[20];
50 	char cbuf[MAXPH];
51 	register char *cp;
52 	register int i;
53 	int dh = -1, nrings = 0;
54 
55 	sprintf(dcname, "/dev/%s", dev->D_line);
56 	DEBUG(4, "dc - %s\n", dcname);
57 	if (setjmp(Sjbuf)) {
58 		logent(dcname, "TIMEOUT");
59 		if (dh >= 0)
60 			hyscls(dh);
61 		return CF_DIAL;
62 	}
63 	signal(SIGALRM, alarmtr);
64 	getnextfd();
65 	alarm(10);
66 	dh = open(dcname, 2); /* read/write */
67 	alarm(0);
68 
69 	/* modem is open */
70 	next_fd = -1;
71 	if (dh >= 0) {
72 		fixline(dh, dev->D_speed);
73 		if (dochat(dev, flds, dh)) {
74 			logent(dcname, "CHAT FAILED");
75 			hyscls(dh);
76 			return CF_DIAL;
77 		}
78 		write(dh, "ATV1E0H\r", 8);
79 		if (expect("OK\r\n", dh) != 0) {
80 			logent(dcname, "HSM seems dead");
81 			hyscls(dh);
82 			return CF_DIAL;
83 		}
84 #ifdef USR2400
85 		write(dh, "ATX6S7=44\r", 10);
86 		if (expect("OK\r\n", dh) != 0) {
87 			logent(dcname, "HSM seems dead");
88 			hyscls(dh);
89 			return CF_DIAL;
90 		}
91 #endif USR2400
92 		if (toneflag)
93 			write(dh, "\rATDT", 5);
94 		else
95 #ifdef USR2400
96 			write(dh, "\rATD", 4);
97 #else HAYES
98 			write(dh, "\rATDP", 5);
99 #endif HAYES
100 		write(dh, telno, strlen(telno));
101 		write(dh, "\r", 1);
102 
103 		if (setjmp(Sjbuf)) {
104 			logent(dcname, "TIMEOUT");
105 			strcpy(devSel, dev->D_line);
106 			hyscls(dh);
107 			return CF_DIAL;
108 		}
109 		signal(SIGALRM, alarmtr);
110 		alarm(2*MAXMSGTIME);
111 		do {
112 			cp = cbuf;
113 			while (read(dh, cp ,1) == 1)
114 				if (*cp >= ' ')
115 					break;
116 			while (++cp < &cbuf[MAXPH] && read(dh, cp, 1) == 1 && *cp != '\n')
117 				;
118 			alarm(0);
119 			*cp-- = '\0';
120 			if (*cp == '\r')
121 				*cp = '\0';
122 			DEBUG(4,"\nGOT: %s", cbuf);
123 			alarm(MAXMSGTIME);
124 		} while ((strncmp(cbuf, "RING", 4) == 0 ||
125 			 strncmp(cbuf, "RRING", 5) == 0) && nrings++ < 5);
126 		if (strncmp(cbuf, "CONNECT", 7) != 0) {
127 			logent(cbuf, _FAILED);
128 			strcpy(devSel, dev->D_line);
129 			hyscls(dh);
130 			return CF_DIAL;
131 		}
132 #undef DONTRESETBAUDRATE
133 #ifndef DONTRESETBAUDRATE
134 		i = atoi(&cbuf[8]);
135 		if (i > 0 && i != dev->D_speed) {
136 			DEBUG(4,"Baudrate reset to %d\n", i);
137 			fixline(dh, i);
138 		}
139 #endif /* DONTRESETBAUDRATE */
140 
141 	}
142 	if (dh < 0) {
143 		logent(dcname, "CAN'T OPEN");
144 		return dh;
145 	}
146 	DEBUG(4, "hayes ok\n", CNULL);
147 	return dh;
148 }
149 
150 hyscls(fd)
151 int fd;
152 {
153 	char dcname[20];
154 #ifdef DROPDTR
155 	struct sgttyb hup, sav;
156 #endif
157 
158 	if (fd > 0) {
159 		sprintf(dcname, "/dev/%s", devSel);
160 		DEBUG(4, "Hanging up fd = %d\n", fd);
161 #ifdef DROPDTR
162 		/*
163 		 * code to drop DTR -- change to 0 baud then back to default.
164 		 */
165 		gtty(fd, &hup);
166 		gtty(fd, &sav);
167 		hup.sg_ispeed = B0;
168 		hup.sg_ospeed = B0;
169 		stty(fd, &hup);
170 		sleep(2);
171 		stty(fd, &sav);
172 		/*
173 		 * now raise DTR -- close the device & open it again.
174 		 */
175 		sleep(2);
176 		close(fd);
177 		sleep(2);
178 		fd = open(dcname, 2);
179 		stty(fd, &sav);
180 #else
181 		sleep(3);
182 		write(fd, "+++", 3);
183 #endif
184 		sleep(3);
185 		write(fd, "ATH\r", 4);
186 /*
187 		if (expect("OK",fd) != 0)
188 			logent(devSel, "HSM did not respond to ATZ");
189 */
190 		sleep(1);
191 		write(fd, "ATZ\r", 4);
192 		sleep(1);
193 		close(fd);
194 		delock(devSel);
195 	}
196 }
197