changeset 41:5ee00413b8af

serial: beginning of fcsim-serial-be
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Mar 2021 20:53:51 +0000
parents 8f505d413815
children 6cc3eea720cb
files .hgignore serial/Makefile serial/main.c serial/xmit.c
diffstat 4 files changed, 98 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sat Mar 20 20:23:24 2021 +0000
+++ b/.hgignore	Sat Mar 20 20:53:51 2021 +0000
@@ -10,6 +10,7 @@
 ^pcsc/fc-pcsc-list$
 
 ^serial/fcsim-serial-atr$
+^serial/fcsim-serial-be$
 
 ^simtool/fc-simtool$
 
--- a/serial/Makefile	Sat Mar 20 20:23:24 2021 +0000
+++ b/serial/Makefile	Sat Mar 20 20:53:51 2021 +0000
@@ -1,18 +1,22 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	fcsim-serial-atr
+PROGS=	fcsim-serial-atr fcsim-serial-be
 
 INSTALL_PREFIX=	/opt/freecalypso
 
 INSTBIN=${INSTALL_PREFIX}/bin
 
 ATR_OBJS=	atrmain.o baud_parse.o collect_atr.o invtable.o serport.o
+MAIN_OBJS=	baud_parse.o collect_atr.o invtable.o main.o serport.o xmit.o
 
 all:	${PROGS}
 
 fcsim-serial-atr:	${ATR_OBJS}
 	${CC} ${CFLAGS} -o $@ ${ATR_OBJS}
 
+fcsim-serial-be:	${MAIN_OBJS}
+	${CC} ${CFLAGS} -o $@ ${MAIN_OBJS}
+
 install:
 	mkdir -p ${INSTBIN}
 	install -c ${PROGS} ${INSTBIN}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial/main.c	Sat Mar 20 20:53:51 2021 +0000
@@ -0,0 +1,31 @@
+/*
+ * This module implements the main() function for fcsim-serial-be.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+extern unsigned baud_base;
+
+main(argc, argv)
+	char **argv;
+{
+	int rc;
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s ttyport baud\n", argv[0]);
+		exit(1);
+	}
+	open_serial_port(argv[1]);
+	parse_baud_spenh_arg(argv[2]);
+	set_serial_params(baud_base);
+	set_serial_nonblock(0);
+	serial_card_reset();
+	rc = collect_atr();
+	if (rc < 0)
+		exit(1);
+	print_atr("A");
+
+	/* remaining logic to be implemented */
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial/xmit.c	Sat Mar 20 20:53:51 2021 +0000
@@ -0,0 +1,61 @@
+/*
+ * This module implements the function for sending byte strings
+ * to the SIM and collecting the UART echo.
+ */
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+extern int target_fd;
+extern int inverse_coding;
+
+send_bytes_to_sim(data, nbytes)
+	u_char *data;
+	unsigned nbytes;
+{
+	u_char buf1[255], buf2[255];
+	fd_set fds;
+	struct timeval tv;
+	unsigned rcvd;
+	int cc;
+
+	bcopy(data, buf1, nbytes);
+	if (inverse_coding)
+		invert_bytes(buf1, nbytes);
+	write(target_fd, buf1, nbytes);
+	for (rcvd = 0; rcvd < nbytes; ) {
+		FD_ZERO(&fds);
+		FD_SET(target_fd, &fds);
+		tv.tv_sec = 1;
+		tv.tv_usec = 0;
+		cc = select(target_fd+1, &fds, NULL, NULL, &tv);
+		if (cc < 0) {
+			if (errno == EINTR)
+				continue;
+			perror("select");
+			return(-1);
+		}
+		if (cc < 1) {
+			fprintf(stderr,
+			"error: timeout waiting for echo of Tx bytes\n");
+			return(-1);
+		}
+		cc = read(target_fd, buf2 + rcvd, nbytes - rcvd);
+		if (cc <= 0) {
+			perror("read after successful select");
+			return(-1);
+		}
+		rcvd += cc;
+	}
+	if (bcmp(buf1, buf2, nbytes)) {
+		fprintf(stderr, "error: UART echo mismatch\n");
+		return(-1);
+	} else
+		return(0);
+}