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