changeset 434:3822f3b198d4

fc-serterm: written
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 22 Jun 2014 01:21:10 +0000
parents 2d8ab1b0df8d
children c6e1c813e7f0
files .hgignore miscutil/Makefile miscutil/fc-serterm.c miscutil/openport.c
diffstat 4 files changed, 101 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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$
--- 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}
 
--- /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 <stdio.h>
+#include <stdlib.h>
+
+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);
+}
--- /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 <sys/types.h>
+#include <sys/file.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+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;
+}