# HG changeset patch # User Michael Spacefalcon # Date 1403400070 0 # Node ID 3822f3b198d48c439e0905bc6a4743ca7c51cef0 # Parent 2d8ab1b0df8d5933c986b0c2131ef6117a77f0e7 fc-serterm: written diff -r 2d8ab1b0df8d -r 3822f3b198d4 .hgignore --- a/.hgignore Sun Jun 22 00:17:44 2014 +0000 +++ b/.hgignore Sun Jun 22 01:21:10 2014 +0000 @@ -20,6 +20,7 @@ ^loadtools/fc-loadtool$ ^loadtools/fc-xram$ +^miscutil/fc-serterm$ ^miscutil/imei-luhn$ ^rvinterf/etmsync/fc-fsio$ diff -r 2d8ab1b0df8d -r 3822f3b198d4 miscutil/Makefile --- a/miscutil/Makefile Sun Jun 22 00:17:44 2014 +0000 +++ b/miscutil/Makefile Sun Jun 22 01:21:10 2014 +0000 @@ -1,16 +1,22 @@ CC= gcc CFLAGS= -O2 -PROGS= imei-luhn +PROGS= fc-serterm imei-luhn INSTBIN=/usr/local/bin all: ${PROGS} -${PROGS}: +SERTERM_OBJS= fc-serterm.o openport.o ttypassthru.o + +fc-serterm: ${SERTERM_OBJS} + ${CC} ${CFLAGS} -o $@ ${SERTERM_OBJS} + +ttypassthru.o: ../loadtools/ttypassthru.c + ${CC} ${CFLAGS} -c -o $@ $< + +imei-luhn: imei-luhn.c ${CC} ${CFLAGS} -o $@ $@.c -imei-luhn: imei-luhn.c - -install: ${PROGS} +install: mkdir -p ${INSTBIN} install -c ${PROGS} ${INSTBIN} diff -r 2d8ab1b0df8d -r 3822f3b198d4 miscutil/fc-serterm.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/miscutil/fc-serterm.c Sun Jun 22 01:21:10 2014 +0000 @@ -0,0 +1,24 @@ +/* + * This hack-utility opens a serial port at the user-specified baud rate + * and drops into a terminal pass-thru mode, except that any binary bytes + * received on this port are turned into cat -v form. The intent is for + * sniffing on and/or talking to targets that emit some ASCII mixed in + * with binary. + */ + +#include +#include + +int target_fd; + +main(argc, argv) + char **argv; +{ + if (argc != 3) { + fprintf(stderr, "usage: %s ttyname baudrate\n", argv[0]); + exit(1); + } + open_target_serial(argv[1], argv[2]); + tty_passthru(); + exit(0); +} diff -r 2d8ab1b0df8d -r 3822f3b198d4 miscutil/openport.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/miscutil/openport.c Sun Jun 22 01:21:10 2014 +0000 @@ -0,0 +1,65 @@ +/* + * Serial port opening code for fc-serterm + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern int target_fd; + +static struct baudrate { + char *name; + speed_t termios_code; +} baud_rate_table[] = { + {"19200", B19200}, + {"38400", B38400}, + {"57600", B57600}, + {"115200", B115200}, + /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */ + {"203125", B230400}, + {"406250", B460800}, + {"812500", B921600}, + /* table search terminator */ + {NULL, B0} +}; + +open_target_serial(ttydev, baudname) + char *ttydev, *baudname; +{ + struct termios target_termios; + struct baudrate *br; + + for (br = baud_rate_table; br->name; br++) + if (!strcmp(br->name, baudname)) + break; + if (!br->name) { + fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n", + baudname); + exit(1); + } + target_fd = open(ttydev, O_RDWR|O_NONBLOCK); + if (target_fd < 0) { + perror(ttydev); + exit(1); + } + target_termios.c_iflag = IGNBRK; + target_termios.c_oflag = 0; + target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8; + target_termios.c_lflag = 0; + target_termios.c_cc[VMIN] = 1; + target_termios.c_cc[VTIME] = 0; + cfsetispeed(&target_termios, br->termios_code); + cfsetospeed(&target_termios, br->termios_code); + if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) { + perror("initial tcsetattr on target"); + exit(1); + } + return 0; +}