Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Commit Details
Date: | 2011-11-21 21:00:32 (8 years 23 days ago) |
---|---|
Author: | Werner Almesberger |
Commit: | 7d0683b33599f467fa5f48f4c02d494c95ce2006 |
Message: | neocon: moved over from svn.openmoko.org/developers/werner/neocon/ |
Files: |
neocon/Makefile (1 diff) neocon/README (1 diff) neocon/neocon.c (1 diff) |
Change Details
neocon/Makefile | ||
---|---|---|
1 | PREFIX=/usr/local | |
2 | ||
3 | CFLAGS=-Wall -g | |
4 | ||
5 | .PHONY: all clean install uninstall | |
6 | ||
7 | all: neocon | |
8 | ||
9 | clean: | |
10 | rm -f neocon | |
11 | ||
12 | install: neocon | |
13 | install -m 555 neocon $(PREFIX)/bin | |
14 | ||
15 | uninstall: | |
16 | rm -f $(PREFIX)/bin/neocon |
neocon/README | ||
---|---|---|
1 | "neocon" is a simple serial console utility that tries to open a | |
2 | ttys that may exist on a system until one such open succeeds. It | |
3 | then passes terminal input and output, until there is a read or | |
4 | write failure on the tty, in which case it disconnects, and the | |
5 | process restarts. | |
6 | ||
7 | This is mainly intended for serial over USB interfaces that | |
8 | disappear when the Neo or debug board is restarted. E.g., | |
9 | neocon /dev/ttyUSB0 /dev/ttyUSB1 | |
10 | ||
11 | The option -t delay_ms throttles keyboard input to a rate of | |
12 | one character every "delay_ms" milliseconds. This can be used to | |
13 | prevent buffer overruns on the remote end. | |
14 | ||
15 | "neocon" can log to a file with the option "-l logfile". Non-ASCII | |
16 | and non-printable characters are converted to hash signs (#). To | |
17 | append to an existing logfile, add the option "-a". To add a | |
18 | timestamp before each line, use the option "-T". | |
19 | ||
20 | To leave neocon, type "~.". The escape character (~) can be changed | |
21 | with the option "-e escape". | |
22 | ||
23 | To manually switch to the next device, enter "~n". | |
24 | ||
25 | ||
26 | Known issues | |
27 | ------------ | |
28 | ||
29 | - the escape character is sent to the target |
neocon/neocon.c | ||
---|---|---|
1 | /* | |
2 | * neocon.c - An interface for changing tty devices | |
3 | * | |
4 | * Copyright (C) 2007, 2008 by OpenMoko, Inc. | |
5 | * Written by Werner Almesberger <werner@openmoko.org> | |
6 | * All Rights Reserved | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
23 | ||
24 | #include <stdlib.h> | |
25 | #include <stdio.h> | |
26 | #include <stdio.h> | |
27 | #include <unistd.h> | |
28 | #include <string.h> | |
29 | #include <termios.h> | |
30 | #include <fcntl.h> | |
31 | #include <assert.h> | |
32 | #include <sys/time.h> | |
33 | #include <sys/select.h> | |
34 | ||
35 | ||
36 | #define MAX_BUF 2048 | |
37 | ||
38 | static char *const *ttys; | |
39 | static int num_ttys; | |
40 | static int curr_tty = -1; /* start with first tty */ | |
41 | static speed_t speed = B115200; | |
42 | static struct termios console, tty; | |
43 | static FILE *log = NULL; | |
44 | static int timestamp = 0; | |
45 | static char escape = '~'; | |
46 | ||
47 | ||
48 | static struct bps { | |
49 | speed_t speed; | |
50 | int bps; | |
51 | } bps_tab[] = { | |
52 | { B300, 300 }, | |
53 | { B1200, 1200 }, | |
54 | { B2400, 2400 }, | |
55 | { B9600, 9600 }, | |
56 | { B19200, 19200 }, | |
57 | { B38400, 38400 }, | |
58 | { B115200, 115200 }, | |
59 | { 0, 0 } | |
60 | }; | |
61 | ||
62 | ||
63 | static speed_t bps_to_speed(int bps) | |
64 | { | |
65 | const struct bps *p; | |
66 | ||
67 | for (p = bps_tab; p->bps; p++) | |
68 | if (p->bps == bps) | |
69 | return p->speed; | |
70 | fprintf(stderr, "no such speed: %d bps\n", bps); | |
71 | exit(1); | |
72 | } | |
73 | ||
74 | ||
75 | static void make_raw(int fd, struct termios *old) | |
76 | { | |
77 | struct termios t; | |
78 | long flags; | |
79 | ||
80 | if (tcgetattr(fd, &t) < 0) { | |
81 | perror("tcgetattr"); | |
82 | exit(1); | |
83 | } | |
84 | if (old) | |
85 | *old = t; | |
86 | cfmakeraw(&t); | |
87 | if (fd) { | |
88 | t.c_iflag &= ~(IXON | IXOFF); | |
89 | t.c_cflag |= CLOCAL; | |
90 | t.c_cflag &= ~CRTSCTS; | |
91 | if (cfsetispeed(&t, speed) < 0) { | |
92 | perror("cfsetispeed"); | |
93 | exit(1); | |
94 | } | |
95 | if (cfsetospeed(&t, speed) < 0) { | |
96 | perror("cfsetospeed"); | |
97 | exit(1); | |
98 | } | |
99 | } | |
100 | if (tcsetattr(fd, TCSANOW, &t) < 0) { | |
101 | perror("tcsetattr"); | |
102 | exit(1); | |
103 | } | |
104 | flags = fcntl(fd,F_GETFL); | |
105 | if (flags < 0) { | |
106 | perror("fcntl F_GETFL"); | |
107 | exit(1); | |
108 | } | |
109 | if (fcntl(fd,F_SETFL,flags & ~O_NONBLOCK) < 0) { | |
110 | perror("fcntl F_GETFL"); | |
111 | exit(1); | |
112 | } | |
113 | } | |
114 | ||
115 | ||
116 | static int open_next_tty(void) | |
117 | { | |
118 | int i, fd = -1; | |
119 | ||
120 | for (i = 0; i != num_ttys; i++) { | |
121 | curr_tty = (curr_tty+1) % num_ttys; | |
122 | fd = open(ttys[curr_tty], O_RDWR | O_NDELAY); | |
123 | if (fd >= 0) | |
124 | break; | |
125 | } | |
126 | if (fd >= 0) | |
127 | make_raw(fd, &tty); | |
128 | return fd; | |
129 | } | |
130 | ||
131 | ||
132 | /* | |
133 | * Return 1 if the user manually forces a device change. | |
134 | */ | |
135 | ||
136 | ||
137 | static int scan(const char *s, size_t len) | |
138 | { | |
139 | static int state = 0; | |
140 | const char *p; | |
141 | int res = 0; | |
142 | ||
143 | for (p = s; p != s+len; p++) | |
144 | switch (state) { | |
145 | case 0: | |
146 | if (*p == escape) | |
147 | state++; | |
148 | else | |
149 | state = 0; | |
150 | break; | |
151 | case 1: | |
152 | if (*p == '.') | |
153 | exit(0); | |
154 | if (*p == 'n') | |
155 | res = 1; | |
156 | state = 0; | |
157 | break; | |
158 | } | |
159 | return res; | |
160 | } | |
161 | ||
162 | ||
163 | static int write_log(const char *buf, ssize_t len) | |
164 | { | |
165 | size_t wrote; | |
166 | ||
167 | wrote = fwrite(buf, 1, len, log); | |
168 | if (wrote == len) | |
169 | return 1; | |
170 | fprintf(stderr, "write failed. closing log file.\n"); | |
171 | fclose(log); | |
172 | log = NULL; | |
173 | return 0; | |
174 | } | |
175 | ||
176 | ||
177 | static int add_timestamp(void) | |
178 | { | |
179 | struct timeval tv; | |
180 | char buf[40]; /* be generous */ | |
181 | int len; | |
182 | ||
183 | if (gettimeofday(&tv, NULL) < 0) { | |
184 | perror("gettimeofday"); | |
185 | exit(1); | |
186 | } | |
187 | len = sprintf(buf, "%lu.%06lu ", | |
188 | (unsigned long) tv.tv_sec, (unsigned long) tv.tv_usec); | |
189 | return write_log(buf, len); | |
190 | } | |
191 | ||
192 | ||
193 | static void do_log(const char *buf, ssize_t len) | |
194 | { | |
195 | static int nl = 1; /* we're at the beginning of a new line */ | |
196 | char tmp[MAX_BUF]; | |
197 | const char *from; | |
198 | char *to; | |
199 | ||
200 | assert(len <= MAX_BUF); | |
201 | from = buf; | |
202 | to = tmp; | |
203 | while (from != buf+len) { | |
204 | if (*from == '\r') { | |
205 | from++; | |
206 | continue; | |
207 | } | |
208 | if (nl && timestamp) | |
209 | if (!add_timestamp()) | |
210 | return; | |
211 | nl = 0; | |
212 | if (*from == '\n') { | |
213 | *to++ = *from++; | |
214 | if (!write_log(tmp, to-tmp)) | |
215 | return; | |
216 | to = tmp; | |
217 | nl = 1; | |
218 | continue; | |
219 | } | |
220 | *to++ = *from < ' ' || *from > '~' ? '#' : *from; | |
221 | from++; | |
222 | } | |
223 | write_log(tmp, to-tmp); | |
224 | } | |
225 | ||
226 | ||
227 | static int copy(int in, int out, int from_user, int single) | |
228 | { | |
229 | char buffer[MAX_BUF]; | |
230 | ssize_t got, wrote, pos; | |
231 | ||
232 | got = read(in, buffer, single ? 1 : sizeof(buffer)); | |
233 | if (got <= 0) | |
234 | return 0; | |
235 | if (from_user) { | |
236 | if (scan(buffer, got)) | |
237 | return 0; | |
238 | } | |
239 | else { | |
240 | if (log) | |
241 | do_log(buffer, got); | |
242 | } | |
243 | for (pos = 0; pos != got; pos += wrote) { | |
244 | wrote = write(out, buffer+pos, got-pos); | |
245 | if (wrote < 0) | |
246 | return 0; | |
247 | } | |
248 | return 1; | |
249 | } | |
250 | ||
251 | ||
252 | static void write_string(const char *s) | |
253 | { | |
254 | int len = strlen(s); | |
255 | ||
256 | while (len) { | |
257 | ssize_t wrote; | |
258 | ||
259 | wrote = write(1, s, len); | |
260 | if (wrote < 0) { | |
261 | perror("write"); | |
262 | exit(1); | |
263 | } | |
264 | s += wrote; | |
265 | len -= wrote; | |
266 | } | |
267 | } | |
268 | ||
269 | ||
270 | static void cleanup(void) | |
271 | { | |
272 | if (tcsetattr(0, TCSANOW, &console) < 0) | |
273 | perror("tcsetattr"); | |
274 | write(1, "\n", 1); | |
275 | } | |
276 | ||
277 | ||
278 | static void usage(const char *name) | |
279 | { | |
280 | fprintf(stderr, | |
281 | "usage: %s [-b bps] [-e escape] [-l logfile [-a] [-T]] [-t delay_ms] tty ...\n\n" | |
282 | " -a append to the log file if it already exists\n" | |
283 | " -b bps set the TTY to the specified bit rate\n" | |
284 | " -e escape set the escape character (default: ~)\n" | |
285 | " -l logfile log all output to the specified file\n" | |
286 | " -t delay_ms wait the specified amount of time between input characters\n" | |
287 | " -T add timestamps to the log file\n" | |
288 | , name); | |
289 | exit(1); | |
290 | } | |
291 | ||
292 | ||
293 | int main(int argc, char *const *argv) | |
294 | { | |
295 | char *end; | |
296 | int c, bps; | |
297 | int fd = -1; | |
298 | int append = 0; | |
299 | const char *logfile = NULL; | |
300 | int throttle_us = 0; | |
301 | int throttle = 0; | |
302 | ||
303 | while ((c = getopt(argc, argv, "ab:e:l:t:T")) != EOF) | |
304 | switch (c) { | |
305 | case 'a': | |
306 | append = 1; | |
307 | break; | |
308 | case 'b': | |
309 | bps = strtoul(optarg, &end, 0); | |
310 | if (*end) | |
311 | usage(*argv); | |
312 | speed = bps_to_speed(bps); | |
313 | break; | |
314 | case 'e': | |
315 | if (strlen(optarg) != 1) | |
316 | usage(*argv); | |
317 | escape = *optarg; | |
318 | break; | |
319 | case 'l': | |
320 | logfile = optarg; | |
321 | break; | |
322 | case 't': | |
323 | throttle_us = strtoul(optarg, &end, 0)*1000; | |
324 | if (*end) | |
325 | usage(*argv); | |
326 | break; | |
327 | case 'T': | |
328 | timestamp = 1; | |
329 | break; | |
330 | default: | |
331 | usage(*argv); | |
332 | } | |
333 | num_ttys = argc-optind; | |
334 | ttys = argv+optind; | |
335 | ||
336 | if (logfile) { | |
337 | log = fopen(logfile, append ? "a" : "w"); | |
338 | if (!log) { | |
339 | perror(logfile); | |
340 | exit(1); | |
341 | } | |
342 | setlinebuf(log); | |
343 | } | |
344 | ||
345 | make_raw(0, &console); | |
346 | atexit(cleanup); | |
347 | while (1) { | |
348 | struct timeval tv; | |
349 | fd_set set; | |
350 | int res; | |
351 | ||
352 | if (fd < 0) { | |
353 | fd = open_next_tty(); | |
354 | if (fd > 0) { | |
355 | char buf[1024]; /* enough :-) */ | |
356 | ||
357 | sprintf(buf, "\r\r[Open %s]\r\n", ttys[curr_tty]); | |
358 | write_string(buf); | |
359 | } | |
360 | } | |
361 | FD_ZERO(&set); | |
362 | if (!throttle) | |
363 | FD_SET(0, &set); | |
364 | if (fd >= 0) | |
365 | FD_SET(fd, &set); | |
366 | tv.tv_sec = 0; | |
367 | tv.tv_usec = throttle ? throttle_us : 100000; | |
368 | res = select(fd < 0 ? 1 : fd+1, &set, NULL, NULL, &tv); | |
369 | if (res < 0) { | |
370 | perror("select"); | |
371 | return 1; | |
372 | } | |
373 | if (!res) | |
374 | throttle = 0; | |
375 | if (FD_ISSET(0, &set)) { | |
376 | if (throttle_us) | |
377 | throttle = 1; | |
378 | if (!copy(0, fd, 1, throttle_us != 0)) | |
379 | goto failed; | |
380 | } | |
381 | if (fd >= 0 && FD_ISSET(fd, &set)) | |
382 | if (!copy(fd, 1, 0, 0)) | |
383 | goto failed; | |
384 | continue; | |
385 | ||
386 | failed: | |
387 | write_string("\r\n[Closed]\r\n"); | |
388 | (void) close(fd); | |
389 | fd = -1; | |
390 | } | |
391 | return 0; | |
392 | } |
Branches:
master