17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
5*8d489c7aSmuffin
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate */
11*8d489c7aSmuffin
12*8d489c7aSmuffin #pragma ident "%Z%%M% %I% %E% SMI"
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate #include "tip.h"
157c478bd9Sstevel@tonic-gate
16*8d489c7aSmuffin static int hayes_sync(int);
17*8d489c7aSmuffin static void sigALRM(void);
187c478bd9Sstevel@tonic-gate static sigjmp_buf timeoutbuf;
197c478bd9Sstevel@tonic-gate
20*8d489c7aSmuffin void hayes_disconnect(void);
21*8d489c7aSmuffin
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Dial up on a Hayes Smart Modem 1200 or 2400
247c478bd9Sstevel@tonic-gate */
25*8d489c7aSmuffin /* ARGSUSED */
267c478bd9Sstevel@tonic-gate int
hayes_dialer(char * num,char * acu)27*8d489c7aSmuffin hayes_dialer(char *num, char *acu)
287c478bd9Sstevel@tonic-gate {
297c478bd9Sstevel@tonic-gate char code = 0, cr = 0;
30*8d489c7aSmuffin sig_handler_t f;
317c478bd9Sstevel@tonic-gate struct termios buf;
327c478bd9Sstevel@tonic-gate
33*8d489c7aSmuffin f = signal(SIGALRM, (sig_handler_t)sigALRM);
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate if (!hayes_sync(FD)) {
36*8d489c7aSmuffin (void) printf("can't synchronize with hayes\n");
377c478bd9Sstevel@tonic-gate #ifdef ACULOG
387c478bd9Sstevel@tonic-gate logent(value(HOST), num, "hayes", "can't synch up");
397c478bd9Sstevel@tonic-gate #endif
40*8d489c7aSmuffin (void) signal(SIGALRM, f);
417c478bd9Sstevel@tonic-gate return (0);
427c478bd9Sstevel@tonic-gate }
437c478bd9Sstevel@tonic-gate if (boolean(value(VERBOSE)))
44*8d489c7aSmuffin (void) printf("\ndialing...");
45*8d489c7aSmuffin (void) fflush(stdout);
46*8d489c7aSmuffin (void) ioctl(FD, TCGETS, &buf);
477c478bd9Sstevel@tonic-gate buf.c_cflag |= HUPCL;
48*8d489c7aSmuffin (void) ioctl(FD, TCSETS, &buf);
49*8d489c7aSmuffin (void) ioctl(FD, TCFLSH, TCIOFLUSH);
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (sigsetjmp(timeoutbuf, 1)) {
527c478bd9Sstevel@tonic-gate #ifdef ACULOG
537c478bd9Sstevel@tonic-gate char line[80];
547c478bd9Sstevel@tonic-gate
55*8d489c7aSmuffin (void) sprintf(line, "%d second dial timeout",
567c478bd9Sstevel@tonic-gate number(value(DIALTIMEOUT)));
577c478bd9Sstevel@tonic-gate logent(value(HOST), num, "hayes", line);
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate hayes_disconnect();
60*8d489c7aSmuffin (void) signal(SIGALRM, f);
617c478bd9Sstevel@tonic-gate return (0);
627c478bd9Sstevel@tonic-gate }
63*8d489c7aSmuffin (void) alarm(number(value(DIALTIMEOUT)));
64*8d489c7aSmuffin (void) ioctl(FD, TCFLSH, TCIOFLUSH);
657c478bd9Sstevel@tonic-gate if (*num == 'S')
66*8d489c7aSmuffin (void) write(FD, "AT", 2);
677c478bd9Sstevel@tonic-gate else
68*8d489c7aSmuffin (void) write(FD, "ATDT", 4); /* use tone dialing */
69*8d489c7aSmuffin (void) write(FD, num, strlen(num));
70*8d489c7aSmuffin (void) write(FD, "\r", 1);
71*8d489c7aSmuffin (void) read(FD, &code, 1);
72*8d489c7aSmuffin (void) read(FD, &cr, 1);
737c478bd9Sstevel@tonic-gate if (code == '1' && cr == '0')
74*8d489c7aSmuffin (void) read(FD, &cr, 1);
75*8d489c7aSmuffin (void) alarm(0);
76*8d489c7aSmuffin (void) signal(SIGALRM, f);
777c478bd9Sstevel@tonic-gate if ((code == '1' || code == '5') && cr == '\r')
787c478bd9Sstevel@tonic-gate return (1);
797c478bd9Sstevel@tonic-gate return (0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
82*8d489c7aSmuffin void
hayes_disconnect(void)83*8d489c7aSmuffin hayes_disconnect(void)
847c478bd9Sstevel@tonic-gate {
85*8d489c7aSmuffin (void) close(FD);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
88*8d489c7aSmuffin void
hayes_abort(void)89*8d489c7aSmuffin hayes_abort(void)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate int dtr = TIOCM_DTR;
927c478bd9Sstevel@tonic-gate
93*8d489c7aSmuffin (void) alarm(0);
94*8d489c7aSmuffin (void) ioctl(FD, TIOCMBIC, &dtr);
95*8d489c7aSmuffin (void) sleep(2);
96*8d489c7aSmuffin (void) ioctl(FD, TCFLSH, TCIOFLUSH);
97*8d489c7aSmuffin (void) close(FD);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate static void
sigALRM(void)101*8d489c7aSmuffin sigALRM(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate siglongjmp(timeoutbuf, 1);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * This piece of code attempts to get the hayes in sync.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate static int
hayes_sync(int fd)110*8d489c7aSmuffin hayes_sync(int fd)
1117c478bd9Sstevel@tonic-gate {
112*8d489c7aSmuffin int tries;
1137c478bd9Sstevel@tonic-gate char code = 0, cr = 0;
1147c478bd9Sstevel@tonic-gate int dtr = TIOCM_DTR;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * Toggle DTR to force anyone off that might have left
1187c478bd9Sstevel@tonic-gate * the modem connected, and insure a consistent state
1197c478bd9Sstevel@tonic-gate * to start from.
1207c478bd9Sstevel@tonic-gate */
121*8d489c7aSmuffin (void) ioctl(fd, TIOCMBIC, &dtr);
122*8d489c7aSmuffin (void) sleep(1);
123*8d489c7aSmuffin (void) ioctl(fd, TIOCMBIS, &dtr);
1247c478bd9Sstevel@tonic-gate for (tries = 0; tries < 3; tries++) {
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * After reseting the modem, initialize all
1277c478bd9Sstevel@tonic-gate * parameters to required vaules:
1287c478bd9Sstevel@tonic-gate *
1297c478bd9Sstevel@tonic-gate * V0 - result codes are single digits
1307c478bd9Sstevel@tonic-gate * Q0 - result codes ARE sent
1317c478bd9Sstevel@tonic-gate * E0 - do not echo
1327c478bd9Sstevel@tonic-gate * S0=1 - automatically answer phone
1337c478bd9Sstevel@tonic-gate * S2=255 - disable escape character
1347c478bd9Sstevel@tonic-gate * S12=255 - longest possible escape guard time
1357c478bd9Sstevel@tonic-gate */
136*8d489c7aSmuffin (void) write(fd, "ATV0Q0E0S0=1S2=255S12=255\r", 26);
137*8d489c7aSmuffin (void) sleep(1);
1387c478bd9Sstevel@tonic-gate /* flush any echoes or return codes */
139*8d489c7aSmuffin (void) ioctl(fd, TCFLSH, TCIOFLUSH);
1407c478bd9Sstevel@tonic-gate /* now see if the modem is talking to us properly */
141*8d489c7aSmuffin (void) write(fd, "AT\r", 3);
1427c478bd9Sstevel@tonic-gate if (sigsetjmp(timeoutbuf, 1) == 0) {
143*8d489c7aSmuffin (void) alarm(2);
144*8d489c7aSmuffin (void) read(FD, &code, 1);
145*8d489c7aSmuffin (void) read(FD, &cr, 1);
1467c478bd9Sstevel@tonic-gate if (code == '0' && cr == '\r')
1477c478bd9Sstevel@tonic-gate return (1);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate return (0);
1517c478bd9Sstevel@tonic-gate }
152