1 #ifndef lint 2 static char sccsid[] = "@(#)hys24.c 1.7 (Berkeley) 03/02/91"; 3 #endif !lint 4 5 #include "condevs.h" 6 7 /* 8 * hyspopn24(telno, flds, dev) connect to hayes smartmodem (pulse call) 9 * hystopn24(telno, flds, dev) connect to hayes smartmodem (tone call) 10 * 11 * return codes: >0 - file number - ok CF_DIAL,CF_DEVICE - failed 12 */ 13 14 #include <sys/file.h> 15 #include <sys/ioctl.h> 16 17 hyspopn24(telno, flds, dev) 18 char *telno, *flds[]; 19 struct Devices *dev; 20 { 21 return hysopn24(telno, flds, dev, 0); 22 } 23 24 hystopn24(telno, flds, dev) 25 char *telno, *flds[]; 26 struct Devices *dev; 27 { 28 return hysopn24(telno, flds, dev, 1); 29 } 30 31 /* ARGSUSED */ 32 hysopn24(telno, flds, dev, toneflag) 33 char *telno; 34 char *flds[]; 35 struct Devices *dev; 36 int toneflag; 37 { 38 int dh = -1; 39 int result, ix, speed; 40 char *ii; 41 extern errno; 42 char dcname[20]; 43 char resultbuf[16]; 44 45 sprintf(dcname, "/dev/%s", dev->D_line); 46 DEBUG(4, "dc - %s\n", dcname); 47 if (setjmp(Sjbuf)) { 48 logent(dcname, "TIMEOUT"); 49 if (dh >= 0) 50 hyscls24(dh, 0); 51 return CF_DIAL; 52 } 53 signal(SIGALRM, alarmtr); 54 getnextfd(); 55 alarm(10); 56 dh = open(dcname, 2); /* read/write */ 57 alarm(0); 58 59 for (ii = telno; *ii; ii++) 60 if (*ii == '=') 61 *ii = ','; 62 63 /* modem is open */ 64 next_fd = -1; 65 if (dh >= 0) { 66 ioctl(dh, TIOCHPCL, 0); 67 fixline(dh, dev->D_speed); 68 if (dochat(dev, flds, dh)) { 69 logent(dcname, "CHAT FAILED"); 70 hyscls24(dh, 0); 71 return CF_DIAL; 72 } 73 hyscls24(dh, 1);/* make sure the line is reset */ 74 write(dh, "AT&F&D3&C1E0M0X3QV0Y\r", 21); 75 if (expect("0\r", dh) != 0) { 76 logent(dcname, "HSM not responding OK"); 77 hyscls24(dh, 0); 78 return CF_DIAL; 79 } 80 if (toneflag) 81 write(dh, "\rATDT", 5); 82 else 83 write(dh, "\rATDP", 5); 84 write(dh, telno, strlen(telno)); 85 write(dh, "\r", 1); 86 87 if (setjmp(Sjbuf)) { 88 logent(dcname, "Modem Hung"); 89 if (dh >= 0) 90 hyscls24(dh, 0); 91 return CF_DIAL; 92 } 93 signal(SIGALRM, alarmtr); 94 alarm(120); 95 do { 96 for (ix = 0; ix < 16; ix++) { 97 read(dh, resultbuf + ix, 1); 98 DEBUG(6, "character read = 0x%X \n", resultbuf[ix]); 99 if ((0x7f & resultbuf[ix]) == 0xd) 100 break; 101 } 102 103 result = atol(resultbuf); 104 switch (result) { 105 case 0: 106 logent("HSM Spurious OK response", _FAILED); 107 speed = 0; 108 break; 109 case 1: 110 logent("HSM connected at 300 baud!", _FAILED); 111 speed = -1; 112 break; 113 case 2: 114 speed = 0; 115 DEBUG(4, "Ringing", 0); 116 break; 117 case 3: 118 logent("HSM No Carrier", _FAILED); 119 speed = -1; 120 break; 121 case 4: 122 logent("HSM Error", _FAILED); 123 speed = -1; 124 break; 125 case 5: 126 speed = 1200; 127 break; 128 case 6: 129 logent("HSM No dialtone", _FAILED); 130 speed = -1; 131 break; 132 case 7: 133 logent("HSM detected BUSY", _FAILED); 134 speed = -1; 135 break; 136 case 8: 137 logent("HSM No quiet answer", _FAILED); 138 speed = -1; 139 break; 140 case 10: 141 speed = 2400; 142 break; 143 default: 144 logent("HSM Unknown response", _FAILED); 145 speed = -1; 146 break; 147 } 148 149 } while (speed == 0); 150 151 alarm(0); 152 153 if (speed < 0) { 154 strcpy(devSel, dev->D_line); 155 hyscls24(dh, 0); 156 return CF_DIAL; 157 } else if (speed != dev->D_speed) { 158 DEBUG(4, "changing line speed to %d baud\n", speed); 159 fixline(dh, speed); 160 } 161 } 162 if (dh < 0) { 163 logent(dcname, "CAN'T OPEN"); 164 return dh; 165 } 166 DEBUG(4, "hayes ok\n", CNULL); 167 return dh; 168 } 169 170 hyscls24(fd, flag) 171 int fd, flag; 172 { 173 char dcname[20]; 174 int fff = 1; 175 176 if (fd > 0) { 177 sprintf(dcname, "/dev/%s", devSel); 178 if (flag) 179 DEBUG(4, "Resetting fd = %d\n", fd); 180 else 181 DEBUG(4, "Hanging up fd = %d\n", fd); 182 /* 183 * Since we have a getty sleeping on this line, when it wakes 184 * up it sends all kinds of garbage to the modem. 185 * Unfortunatly, the modem likes to execute the previous 186 * command when it sees the garbage. The previous command 187 * was to dial the phone, so let's make the last command 188 * reset the modem. 189 */ 190 if (!flag) 191 fixline(fd, 2400); 192 write(fd, "\r", 1); 193 sleep(2); 194 write(fd, "+++", 3); 195 sleep(3); 196 write(fd, "\rATH\rATZ\r", 9); 197 sleep(2); 198 ioctl(fd, TIOCFLUSH, &fff); 199 200 if (!flag) { 201 close(fd); 202 delock(devSel); 203 } 204 } 205 } 206