diff serial/xmit.c @ 41:5ee00413b8af

serial: beginning of fcsim-serial-be
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Mar 2021 20:53:51 +0000
parents
children
line wrap: on
line diff
--- /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);
+}