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