xref: /titanic_52/usr/src/cmd/tip/aculib/hayes.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #include "tip.h"
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate static void sigALRM();
15*7c478bd9Sstevel@tonic-gate static sigjmp_buf timeoutbuf;
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate /*
18*7c478bd9Sstevel@tonic-gate  * Dial up on a Hayes Smart Modem 1200 or 2400
19*7c478bd9Sstevel@tonic-gate  */
20*7c478bd9Sstevel@tonic-gate int
21*7c478bd9Sstevel@tonic-gate hayes_dialer(num, acu)
22*7c478bd9Sstevel@tonic-gate 	char *num, *acu;
23*7c478bd9Sstevel@tonic-gate {
24*7c478bd9Sstevel@tonic-gate 	char code = 0, cr = 0;
25*7c478bd9Sstevel@tonic-gate 	void (*f)();
26*7c478bd9Sstevel@tonic-gate 	struct termios buf;
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate 	f = signal(SIGALRM, sigALRM);
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 	if (!hayes_sync(FD)) {
31*7c478bd9Sstevel@tonic-gate 		printf("can't synchronize with hayes\n");
32*7c478bd9Sstevel@tonic-gate #ifdef ACULOG
33*7c478bd9Sstevel@tonic-gate 		logent(value(HOST), num, "hayes", "can't synch up");
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate 		signal(SIGALRM, f);
36*7c478bd9Sstevel@tonic-gate 		return (0);
37*7c478bd9Sstevel@tonic-gate 	}
38*7c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
39*7c478bd9Sstevel@tonic-gate 		printf("\ndialing...");
40*7c478bd9Sstevel@tonic-gate 	fflush(stdout);
41*7c478bd9Sstevel@tonic-gate 	ioctl(FD, TCGETS, &buf);
42*7c478bd9Sstevel@tonic-gate 	buf.c_cflag |= HUPCL;
43*7c478bd9Sstevel@tonic-gate 	ioctl(FD, TCSETS, &buf);
44*7c478bd9Sstevel@tonic-gate 	ioctl(FD, TCFLSH, TCIOFLUSH);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	if (sigsetjmp(timeoutbuf, 1)) {
47*7c478bd9Sstevel@tonic-gate #ifdef ACULOG
48*7c478bd9Sstevel@tonic-gate 		char line[80];
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 		sprintf(line, "%d second dial timeout",
51*7c478bd9Sstevel@tonic-gate 			number(value(DIALTIMEOUT)));
52*7c478bd9Sstevel@tonic-gate 		logent(value(HOST), num, "hayes", line);
53*7c478bd9Sstevel@tonic-gate #endif
54*7c478bd9Sstevel@tonic-gate 		hayes_disconnect();
55*7c478bd9Sstevel@tonic-gate 		signal(SIGALRM, f);
56*7c478bd9Sstevel@tonic-gate 		return (0);
57*7c478bd9Sstevel@tonic-gate 	}
58*7c478bd9Sstevel@tonic-gate 	alarm(number(value(DIALTIMEOUT)));
59*7c478bd9Sstevel@tonic-gate 	ioctl(FD, TCFLSH, TCIOFLUSH);
60*7c478bd9Sstevel@tonic-gate 	if (*num == 'S')
61*7c478bd9Sstevel@tonic-gate 		write(FD, "AT", 2);
62*7c478bd9Sstevel@tonic-gate 	else
63*7c478bd9Sstevel@tonic-gate 		write(FD, "ATDT", 4);	/* use tone dialing */
64*7c478bd9Sstevel@tonic-gate 	write(FD, num, strlen(num));
65*7c478bd9Sstevel@tonic-gate 	write(FD, "\r", 1);
66*7c478bd9Sstevel@tonic-gate 	read(FD, &code, 1);
67*7c478bd9Sstevel@tonic-gate 	read(FD, &cr, 1);
68*7c478bd9Sstevel@tonic-gate 	if (code == '1' && cr == '0')
69*7c478bd9Sstevel@tonic-gate 		read(FD, &cr, 1);
70*7c478bd9Sstevel@tonic-gate 	alarm(0);
71*7c478bd9Sstevel@tonic-gate 	signal(SIGALRM, f);
72*7c478bd9Sstevel@tonic-gate 	if ((code == '1' || code == '5') && cr == '\r')
73*7c478bd9Sstevel@tonic-gate 		return (1);
74*7c478bd9Sstevel@tonic-gate 	return (0);
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate hayes_disconnect()
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	close(FD);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate hayes_abort()
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	int dtr = TIOCM_DTR;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	alarm(0);
87*7c478bd9Sstevel@tonic-gate 	ioctl(FD, TIOCMBIC, &dtr);
88*7c478bd9Sstevel@tonic-gate 	sleep(2);
89*7c478bd9Sstevel@tonic-gate 	ioctl(FD, TCFLSH, TCIOFLUSH);
90*7c478bd9Sstevel@tonic-gate 	close(FD);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate static void
94*7c478bd9Sstevel@tonic-gate sigALRM()
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	siglongjmp(timeoutbuf, 1);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * This piece of code attempts to get the hayes in sync.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate static int
103*7c478bd9Sstevel@tonic-gate hayes_sync(fd)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	register int tries;
106*7c478bd9Sstevel@tonic-gate 	char code = 0, cr = 0;
107*7c478bd9Sstevel@tonic-gate 	int dtr = TIOCM_DTR;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	/*
110*7c478bd9Sstevel@tonic-gate 	 * Toggle DTR to force anyone off that might have left
111*7c478bd9Sstevel@tonic-gate 	 * the modem connected, and insure a consistent state
112*7c478bd9Sstevel@tonic-gate 	 * to start from.
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 	ioctl(fd, TIOCMBIC, &dtr);
115*7c478bd9Sstevel@tonic-gate 	sleep(1);
116*7c478bd9Sstevel@tonic-gate 	ioctl(fd, TIOCMBIS, &dtr);
117*7c478bd9Sstevel@tonic-gate 	for (tries = 0; tries < 3; tries++) {
118*7c478bd9Sstevel@tonic-gate 		/*
119*7c478bd9Sstevel@tonic-gate 		 * After reseting the modem, initialize all
120*7c478bd9Sstevel@tonic-gate 		 * parameters to required vaules:
121*7c478bd9Sstevel@tonic-gate 		 *
122*7c478bd9Sstevel@tonic-gate 		 *	V0	- result codes are single digits
123*7c478bd9Sstevel@tonic-gate 		 *	Q0	- result codes ARE sent
124*7c478bd9Sstevel@tonic-gate 		 *	E0	- do not echo
125*7c478bd9Sstevel@tonic-gate 		 *	S0=1	- automatically answer phone
126*7c478bd9Sstevel@tonic-gate 		 *	S2=255	- disable escape character
127*7c478bd9Sstevel@tonic-gate 		 *	S12=255	- longest possible escape guard time
128*7c478bd9Sstevel@tonic-gate 		 */
129*7c478bd9Sstevel@tonic-gate 		write(fd, "ATV0Q0E0S0=1S2=255S12=255\r", 26);
130*7c478bd9Sstevel@tonic-gate 		sleep(1);
131*7c478bd9Sstevel@tonic-gate 		/* flush any echoes or return codes */
132*7c478bd9Sstevel@tonic-gate 		ioctl(fd, TCFLSH, TCIOFLUSH);
133*7c478bd9Sstevel@tonic-gate 		/* now see if the modem is talking to us properly */
134*7c478bd9Sstevel@tonic-gate 		write(fd, "AT\r", 3);
135*7c478bd9Sstevel@tonic-gate 		if (sigsetjmp(timeoutbuf, 1) == 0) {
136*7c478bd9Sstevel@tonic-gate 			alarm(2);
137*7c478bd9Sstevel@tonic-gate 			read(FD, &code, 1);
138*7c478bd9Sstevel@tonic-gate 			read(FD, &cr, 1);
139*7c478bd9Sstevel@tonic-gate 			if (code == '0' && cr == '\r')
140*7c478bd9Sstevel@tonic-gate 				return (1);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 	return (0);
144*7c478bd9Sstevel@tonic-gate }
145