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