xref: /onnv-gate/usr/src/cmd/ttymon/tmautobaud.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <fcntl.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <termio.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <signal.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <sys/types.h>
42*0Sstevel@tonic-gate #include <sys/stropts.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include "tmextern.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	NTRY	5
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * At this time, we only recognize certain speeds.
50*0Sstevel@tonic-gate  * This table can be expanded if new patterns are found
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate static struct	autobaud {
53*0Sstevel@tonic-gate 	char	*a_speed;
54*0Sstevel@tonic-gate 	char	*a_pattern;	/* first byte is length */
55*0Sstevel@tonic-gate } autob2400[] = {
56*0Sstevel@tonic-gate #ifdef i386
57*0Sstevel@tonic-gate 	/*
58*0Sstevel@tonic-gate 	 * These are the bit patterns returned on x86 boxes
59*0Sstevel@tonic-gate 	 */
60*0Sstevel@tonic-gate 	"110",		"\1\000",
61*0Sstevel@tonic-gate #else
62*0Sstevel@tonic-gate 	"110",		"\3\000\000\000",
63*0Sstevel@tonic-gate #endif
64*0Sstevel@tonic-gate 	"1200",		"\2\346\200",
65*0Sstevel@tonic-gate 	"2400",		"\1\15",
66*0Sstevel@tonic-gate 	"4800",		"\1\371",
67*0Sstevel@tonic-gate 	"4800",		"\1\362",
68*0Sstevel@tonic-gate 	"9600",		"\1\377",
69*0Sstevel@tonic-gate 	0,		0
70*0Sstevel@tonic-gate };
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate extern	struct	strbuf *peek_ptr;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  *	auto_termio - set termio to allow autobaud
76*0Sstevel@tonic-gate  *		    - the line is set to raw mode, with VMIN = 5, VTIME = 1
77*0Sstevel@tonic-gate  *		    - baud rate is set to 2400
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate int
auto_termio(int fd)80*0Sstevel@tonic-gate auto_termio(int fd)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	struct termio termio;
83*0Sstevel@tonic-gate 	struct termios termios;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	if (ioctl(fd, TCGETS, &termios) == -1) {
86*0Sstevel@tonic-gate 		if (ioctl(fd, TCGETA, &termio) == -1) {
87*0Sstevel@tonic-gate 			log("auto_termio: ioctl TCGETA failed, fd = %d: %s", fd,
88*0Sstevel@tonic-gate 			    strerror(errno));
89*0Sstevel@tonic-gate 			return(-1);
90*0Sstevel@tonic-gate 		}
91*0Sstevel@tonic-gate 		termio.c_iflag = 0;
92*0Sstevel@tonic-gate 		termio.c_cflag &= ~(CBAUD|CSIZE|PARENB);
93*0Sstevel@tonic-gate 		termio.c_cflag |= CREAD|HUPCL|(CS8&CSIZE)|(B2400&CBAUD);
94*0Sstevel@tonic-gate 		termio.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK);
95*0Sstevel@tonic-gate 		termio.c_oflag = 0;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 		termio.c_cc[VMIN] = 5;
98*0Sstevel@tonic-gate 		termio.c_cc[VTIME] = 1;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 		if (ioctl(fd, TCSETAF, &termio) == -1) {
101*0Sstevel@tonic-gate 			log("auto_termio: ioctl TCSETAF failed, fd = %d: %s",
102*0Sstevel@tonic-gate 			    fd, strerror(errno));
103*0Sstevel@tonic-gate 			return(-1);
104*0Sstevel@tonic-gate 		}
105*0Sstevel@tonic-gate 	} else {
106*0Sstevel@tonic-gate 		termios.c_iflag &= 0xffff0000;
107*0Sstevel@tonic-gate 		termios.c_cflag &= ~(CSIZE|PARENB);
108*0Sstevel@tonic-gate 		termios.c_cflag |= CREAD|HUPCL|(CS8&CSIZE);
109*0Sstevel@tonic-gate 		termios.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK);
110*0Sstevel@tonic-gate 		termios.c_oflag &= 0xffff0000;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		termios.c_cc[VMIN] = 5;
113*0Sstevel@tonic-gate 		termios.c_cc[VTIME] = 1;
114*0Sstevel@tonic-gate 		cfsetospeed(&termios, B2400);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		if (ioctl(fd, TCSETSF, &termios) == -1) {
117*0Sstevel@tonic-gate 			log("auto_termio: ioctl TCSETSF failed, fd = %d: %s",
118*0Sstevel@tonic-gate 			      fd, strerror(errno));
119*0Sstevel@tonic-gate 			return(-1);
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	return(0);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  *	autobaud - determine the baudrate by reading data at 2400 baud rate
127*0Sstevel@tonic-gate  *		 - the program is anticipating <CR>
128*0Sstevel@tonic-gate  *		 - the bit pattern is matched again an autobaud table
129*0Sstevel@tonic-gate  *		 - if a match is found, the matched speed is returned
130*0Sstevel@tonic-gate  *		 - otherwise, NULL is returned
131*0Sstevel@tonic-gate  */
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate char *
autobaud(int fd,int timeout)134*0Sstevel@tonic-gate autobaud(int fd, int timeout)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	int i, k, count;
137*0Sstevel@tonic-gate 	static char	buf[5];
138*0Sstevel@tonic-gate 	register char *cp = buf;
139*0Sstevel@tonic-gate 	struct	autobaud *tp;
140*0Sstevel@tonic-gate 	struct	sigaction sigact;
141*0Sstevel@tonic-gate 	extern	void	timedout();
142*0Sstevel@tonic-gate 	extern	void	flush_input();
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate #ifdef	DEBUG
145*0Sstevel@tonic-gate 	debug("in autobaud");
146*0Sstevel@tonic-gate #endif
147*0Sstevel@tonic-gate 	auto_termio(fd);
148*0Sstevel@tonic-gate 	sigact.sa_flags = 0;
149*0Sstevel@tonic-gate 	sigact.sa_handler = SIG_IGN;
150*0Sstevel@tonic-gate 	(void)sigemptyset(&sigact.sa_mask);
151*0Sstevel@tonic-gate 	(void)sigaction(SIGINT, &sigact, NULL);
152*0Sstevel@tonic-gate 	count = NTRY;
153*0Sstevel@tonic-gate 	while (count--) {
154*0Sstevel@tonic-gate 		if (timeout) {
155*0Sstevel@tonic-gate 			sigact.sa_flags = 0;
156*0Sstevel@tonic-gate 			sigact.sa_handler = timedout;
157*0Sstevel@tonic-gate 			(void)sigemptyset(&sigact.sa_mask);
158*0Sstevel@tonic-gate 			(void)sigaction(SIGALRM, &sigact, NULL);
159*0Sstevel@tonic-gate 			(void)alarm((unsigned)timeout);
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 		cp = &buf[1];
162*0Sstevel@tonic-gate 		if ( peek_ptr ) {
163*0Sstevel@tonic-gate 			strncpy(cp, peek_ptr->buf, k=peek_ptr->len);
164*0Sstevel@tonic-gate 			peek_ptr = NULL;
165*0Sstevel@tonic-gate 		} else if ((k=read(fd, cp, 5)) < 0) {
166*0Sstevel@tonic-gate 			fatal("autobaud: read failed: %s", strerror(errno));
167*0Sstevel@tonic-gate 		}
168*0Sstevel@tonic-gate 		if (timeout)
169*0Sstevel@tonic-gate 			(void)alarm((unsigned)0);
170*0Sstevel@tonic-gate 		buf[0] = (char)k;
171*0Sstevel@tonic-gate 		for (tp = autob2400; tp->a_speed; tp++) {
172*0Sstevel@tonic-gate 			for (i = 0;; i++) {
173*0Sstevel@tonic-gate 				if (buf[i] != tp->a_pattern[i])
174*0Sstevel@tonic-gate 					break;
175*0Sstevel@tonic-gate 				if (i == buf[0]) {
176*0Sstevel@tonic-gate 					return(tp->a_speed);
177*0Sstevel@tonic-gate 				}
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 		flush_input(fd);
181*0Sstevel@tonic-gate 	} /* end while */
182*0Sstevel@tonic-gate 	return(NULL);		/* autobaud failed */
183*0Sstevel@tonic-gate }
184