1 /* 2 * Copyright (c) 1986 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)courier.c 5.7 (Berkeley) 03/02/91"; 10 #endif /* not lint */ 11 12 /* 13 * Routines for calling up on a Courier modem. 14 * Derived from Hayes driver. 15 */ 16 #include "tip.h" 17 #include <stdio.h> 18 19 #define MAXRETRY 5 20 21 static void sigALRM(); 22 static int timeout = 0; 23 static int connected = 0; 24 static jmp_buf timeoutbuf, intbuf; 25 static int coursync(); 26 27 cour_dialer(num, acu) 28 register char *num; 29 char *acu; 30 { 31 register char *cp; 32 #ifdef ACULOG 33 char line[80]; 34 #endif 35 static int cour_connect(), cour_swallow(); 36 37 if (boolean(value(VERBOSE))) 38 printf("Using \"%s\"\n", acu); 39 40 ioctl(FD, TIOCHPCL, 0); 41 /* 42 * Get in synch. 43 */ 44 if (!coursync()) { 45 badsynch: 46 printf("can't synchronize with courier\n"); 47 #ifdef ACULOG 48 logent(value(HOST), num, "courier", "can't synch up"); 49 #endif 50 return (0); 51 } 52 cour_write(FD, "AT E0\r", 6); /* turn off echoing */ 53 sleep(1); 54 #ifdef DEBUG 55 if (boolean(value(VERBOSE))) 56 verbose_read(); 57 #endif 58 ioctl(FD, TIOCFLUSH, 0); /* flush any clutter */ 59 cour_write(FD, "AT C1 E0 H0 Q0 X6 V1\r", 21); 60 if (!cour_swallow("\r\nOK\r\n")) 61 goto badsynch; 62 fflush(stdout); 63 cour_write(FD, "AT D", 4); 64 for (cp = num; *cp; cp++) 65 if (*cp == '=') 66 *cp = ','; 67 cour_write(FD, num, strlen(num)); 68 cour_write(FD, "\r", 1); 69 connected = cour_connect(); 70 #ifdef ACULOG 71 if (timeout) { 72 sprintf(line, "%d second dial timeout", 73 number(value(DIALTIMEOUT))); 74 logent(value(HOST), num, "cour", line); 75 } 76 #endif 77 if (timeout) 78 cour_disconnect(); 79 return (connected); 80 } 81 82 cour_disconnect() 83 { 84 /* first hang up the modem*/ 85 ioctl(FD, TIOCCDTR, 0); 86 sleep(1); 87 ioctl(FD, TIOCSDTR, 0); 88 coursync(); /* reset */ 89 close(FD); 90 } 91 92 cour_abort() 93 { 94 cour_write(FD, "\r", 1); /* send anything to abort the call */ 95 cour_disconnect(); 96 } 97 98 static void 99 sigALRM() 100 { 101 printf("\07timeout waiting for reply\n"); 102 timeout = 1; 103 longjmp(timeoutbuf, 1); 104 } 105 106 static int 107 cour_swallow(match) 108 register char *match; 109 { 110 sig_t f; 111 char c; 112 113 f = signal(SIGALRM, sigALRM); 114 timeout = 0; 115 do { 116 if (*match =='\0') { 117 signal(SIGALRM, f); 118 return (1); 119 } 120 if (setjmp(timeoutbuf)) { 121 signal(SIGALRM, f); 122 return (0); 123 } 124 alarm(number(value(DIALTIMEOUT))); 125 read(FD, &c, 1); 126 alarm(0); 127 c &= 0177; 128 #ifdef DEBUG 129 if (boolean(value(VERBOSE))) 130 putchar(c); 131 #endif 132 } while (c == *match++); 133 #ifdef DEBUG 134 if (boolean(value(VERBOSE))) 135 fflush(stdout); 136 #endif 137 signal(SIGALRM, SIG_DFL); 138 return (0); 139 } 140 141 struct baud_msg { 142 char *msg; 143 int baud; 144 } baud_msg[] = { 145 "", B300, 146 " 1200", B1200, 147 " 2400", B2400, 148 0, 0, 149 }; 150 151 static int 152 cour_connect() 153 { 154 char c; 155 int nc, nl, n; 156 struct sgttyb sb; 157 char dialer_buf[64]; 158 struct baud_msg *bm; 159 sig_t f; 160 161 if (cour_swallow("\r\n") == 0) 162 return (0); 163 f = signal(SIGALRM, sigALRM); 164 again: 165 nc = 0; nl = sizeof(dialer_buf)-1; 166 bzero(dialer_buf, sizeof(dialer_buf)); 167 timeout = 0; 168 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) { 169 if (setjmp(timeoutbuf)) 170 break; 171 alarm(number(value(DIALTIMEOUT))); 172 n = read(FD, &c, 1); 173 alarm(0); 174 if (n <= 0) 175 break; 176 c &= 0x7f; 177 if (c == '\r') { 178 if (cour_swallow("\n") == 0) 179 break; 180 if (!dialer_buf[0]) 181 goto again; 182 if (strcmp(dialer_buf, "RINGING") == 0 && 183 boolean(value(VERBOSE))) { 184 #ifdef DEBUG 185 printf("%s\r\n", dialer_buf); 186 #endif 187 goto again; 188 } 189 if (strncmp(dialer_buf, "CONNECT", 190 sizeof("CONNECT")-1) != 0) 191 break; 192 for (bm = baud_msg ; bm ; bm++) 193 if (strcmp(bm->msg, 194 dialer_buf+sizeof("CONNECT")-1) == 0) { 195 if (ioctl(FD, TIOCGETP, &sb) < 0) { 196 perror("TIOCGETP"); 197 goto error; 198 } 199 sb.sg_ispeed = sb.sg_ospeed = bm->baud; 200 if (ioctl(FD, TIOCSETP, &sb) < 0) { 201 perror("TIOCSETP"); 202 goto error; 203 } 204 signal(SIGALRM, f); 205 #ifdef DEBUG 206 if (boolean(value(VERBOSE))) 207 printf("%s\r\n", dialer_buf); 208 #endif 209 return (1); 210 } 211 break; 212 } 213 dialer_buf[nc] = c; 214 #ifdef notdef 215 if (boolean(value(VERBOSE))) 216 putchar(c); 217 #endif 218 } 219 error1: 220 printf("%s\r\n", dialer_buf); 221 error: 222 signal(SIGALRM, f); 223 return (0); 224 } 225 226 /* 227 * This convoluted piece of code attempts to get 228 * the courier in sync. 229 */ 230 static int 231 coursync() 232 { 233 int already = 0; 234 int len; 235 char buf[40]; 236 237 while (already++ < MAXRETRY) { 238 ioctl(FD, TIOCFLUSH, 0); /* flush any clutter */ 239 cour_write(FD, "\rAT Z\r", 6); /* reset modem */ 240 bzero(buf, sizeof(buf)); 241 sleep(1); 242 ioctl(FD, FIONREAD, &len); 243 if (len) { 244 len = read(FD, buf, sizeof(buf)); 245 #ifdef DEBUG 246 buf[len] = '\0'; 247 printf("coursync: (\"%s\")\n\r", buf); 248 #endif 249 if (index(buf, '0') || 250 (index(buf, 'O') && index(buf, 'K'))) 251 return(1); 252 } 253 /* 254 * If not strapped for DTR control, 255 * try to get command mode. 256 */ 257 sleep(1); 258 cour_write(FD, "+++", 3); 259 sleep(1); 260 /* 261 * Toggle DTR to force anyone off that might have left 262 * the modem connected. 263 */ 264 ioctl(FD, TIOCCDTR, 0); 265 sleep(1); 266 ioctl(FD, TIOCSDTR, 0); 267 } 268 cour_write(FD, "\rAT Z\r", 6); 269 return (0); 270 } 271 272 cour_write(fd, cp, n) 273 int fd; 274 char *cp; 275 int n; 276 { 277 struct sgttyb sb; 278 #ifdef notdef 279 if (boolean(value(VERBOSE))) 280 write(1, cp, n); 281 #endif 282 ioctl(fd, TIOCGETP, &sb); 283 ioctl(fd, TIOCSETP, &sb); 284 cour_nap(); 285 for ( ; n-- ; cp++) { 286 write(fd, cp, 1); 287 ioctl(fd, TIOCGETP, &sb); 288 ioctl(fd, TIOCSETP, &sb); 289 cour_nap(); 290 } 291 } 292 293 #ifdef DEBUG 294 verbose_read() 295 { 296 int n = 0; 297 char buf[BUFSIZ]; 298 299 if (ioctl(FD, FIONREAD, &n) < 0) 300 return; 301 if (n <= 0) 302 return; 303 if (read(FD, buf, n) != n) 304 return; 305 write(1, buf, n); 306 } 307 #endif 308 309 /* 310 * Code stolen from /usr/src/lib/libc/gen/sleep.c 311 */ 312 #define mask(s) (1<<((s)-1)) 313 #define setvec(vec, a) \ 314 vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0 315 316 static napms = 50; /* Give the courier 50 milliseconds between characters */ 317 318 static int ringring; 319 320 cour_nap() 321 { 322 323 static void cour_napx(); 324 int omask; 325 struct itimerval itv, oitv; 326 register struct itimerval *itp = &itv; 327 struct sigvec vec, ovec; 328 329 timerclear(&itp->it_interval); 330 timerclear(&itp->it_value); 331 if (setitimer(ITIMER_REAL, itp, &oitv) < 0) 332 return; 333 setvec(ovec, SIG_DFL); 334 omask = sigblock(mask(SIGALRM)); 335 itp->it_value.tv_sec = napms/1000; 336 itp->it_value.tv_usec = ((napms%1000)*1000); 337 setvec(vec, cour_napx); 338 ringring = 0; 339 (void) sigvec(SIGALRM, &vec, &ovec); 340 (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0); 341 while (!ringring) 342 sigpause(omask &~ mask(SIGALRM)); 343 (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0); 344 (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0); 345 (void) sigsetmask(omask); 346 } 347 348 static void 349 cour_napx() 350 { 351 ringring = 1; 352 } 353