changeset 183:85222710dc92

rvinterf/etm: implemented everything except the packet sending command
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 24 Nov 2013 08:03:27 +0000
parents 13a0348ffce4
children 4714fdfca39c
files rvinterf/etm/Makefile rvinterf/etm/etmbasic.c rvinterf/etm/interf.c rvinterf/etm/localtypes.h rvinterf/etm/pktsort.c rvinterf/etm/ttymagic.c
diffstat 6 files changed, 215 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etm/Makefile	Sun Nov 24 08:03:27 2013 +0000
@@ -0,0 +1,18 @@
+CC=	gcc
+CFLAGS=	-O2
+PROGS=	fc-tmsh
+INSTBIN=/usr/local/bin
+
+TMSH_OBJS=	etmbasic.o init.o interf.o main.o pktsort.o ttymagic.o usercmd.o
+
+all:	${PROGS}
+
+fc-tmsh:	${TMSH_OBJS}
+	${CC} ${CFLAGS} -o $@ ${TMSH_OBJS}
+
+install:	${PROGS}
+	mkdir -p ${INSTBIN}
+	install -c ${PROGS} ${INSTBIN}
+
+clean:
+	rm -f *.o *.out *errs ${PROGS}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etm/etmbasic.c	Sun Nov 24 08:03:27 2013 +0000
@@ -0,0 +1,35 @@
+/*
+ * Basic ETM interaction
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "../limits.h"
+
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+
+void
+etm_packet_rx()
+{
+	char buf[MAX_PKT_FROM_TARGET*3+80], *dp;
+	int i, c;
+
+	if (rvi_msg_len < 4) {
+		async_msg_output("!!! Short ETM Rx packet !!!");
+		return;
+	}
+	strcpy(buf, "Pkt from ETM:");
+	dp = index(buf, '\0');
+	c = 0;
+	for (i = 2; i < rvi_msg_len; i++) {
+		sprintf(dp, " %02X", rvi_msg[i]);
+		dp += 3;
+		c ^= rvi_msg[i];
+	}
+	sprintf(dp, " chksum %s", c ? "BAD" : "OK");
+	async_msg_output(buf);
+}
--- a/rvinterf/etm/interf.c	Sun Nov 24 06:59:09 2013 +0000
+++ b/rvinterf/etm/interf.c	Sun Nov 24 08:03:27 2013 +0000
@@ -35,8 +35,27 @@
 void
 process_msg_from_rvinterf()
 {
-
-
+	switch (rvi_msg[0]) {
+	case RVI2CLI_PKT_FROM_TARGET:
+		process_pkt_from_target();
+		return;
+	case RVI2CLI_LOCAL_CMD_RESP:
+		if (rvi_msg_len < 2)
+			goto bad;
+		if (rvi_msg[1] == '+')
+			return;
+		tty_cleanup();
+		fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
+			rvi_msg + 1);
+		exit(1);
+	default:
+	bad:
+		tty_cleanup();
+		fprintf(stderr,
+			"Error: unexpected message type %02X from rvinterf\n",
+			rvi_msg[0]);
+		exit(1);
+	}
 }
 
 void
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etm/localtypes.h	Sun Nov 24 08:03:27 2013 +0000
@@ -0,0 +1,7 @@
+/*
+ * Our own definition of u8/u16/u32
+ */
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned int u32;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etm/pktsort.c	Sun Nov 24 08:03:27 2013 +0000
@@ -0,0 +1,111 @@
+/*
+ * Here we sort out incoming packets from the target relayed via rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "../pktmux.h"
+#include "../limits.h"
+#include "../localsock.h"
+#include "localtypes.h"
+#include "etm.h"
+
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+
+void
+safe_print_trace(src, srclen, dest)
+	u_char *src;
+	char *dest;
+{
+	int i, c;
+	char *dp;
+
+	dp = dest;
+	for (i = 0; i < srclen; i++) {
+		c = src[i];
+		if (c & 0x80) {
+			*dp++ = 'M';
+			*dp++ = '-';
+			c &= 0x7F;
+		}
+		if (c < 0x20) {
+			*dp++ = '^';
+			*dp++ = c + '@';
+		} else if (c == 0x7F) {
+			*dp++ = '^';
+			*dp++ = '?';
+		} else
+			*dp++ = c;
+	}
+	*dp = '\0';
+}
+
+static void
+handle_useid_0()
+{
+	char buf[MAX_PKT_FROM_TARGET*4];
+
+	if (strncmp(rvi_msg + 7, "RVT: Lost Message", 17))
+		return;
+	safe_print_trace(rvi_msg + 7, rvi_msg_len - 7, buf);
+	async_msg_output(buf);
+}
+
+static void
+print_etm_trace()
+{
+	char buf[MAX_PKT_FROM_TARGET*4];
+
+	strcpy(buf, "ETM Tr: ");
+	safe_print_trace(rvi_msg + 7, rvi_msg_len - 7, buf + 9);
+	async_msg_output(buf);
+}
+
+static void
+process_rvt()
+{
+	u32 useid;
+
+	if (rvi_msg_len < 7) {
+		tty_cleanup();
+		fprintf(stderr, "Error: rvinterf sent us an invalid RVT msg\n");
+		exit(1);
+	}
+	useid = rvi_msg[2] << 24 | rvi_msg[3] << 16 | rvi_msg[4] << 8
+		| rvi_msg[5];
+	switch (useid) {
+	case 0:
+		handle_useid_0();
+		return;
+	case ETM_USE_ID:
+		print_etm_trace();
+		return;
+	default:
+		tty_cleanup();
+		fprintf(stderr, "unexpected fwd of USEID %08X from rvinterf\n",
+			useid);
+		exit(1);
+	}
+}
+
+void
+process_pkt_from_target()
+{
+	switch (rvi_msg[1]) {
+	case RVT_RV_HEADER:
+		process_rvt();
+		return;
+	case RVT_TM_HEADER:
+		etm_packet_rx();
+		return;
+	default:
+		tty_cleanup();
+		fprintf(stderr, "unexpected fwd of MUX %02X from rvinterf\n",
+			rvi_msg[1]);
+		exit(1);
+	}
+}
--- a/rvinterf/etm/ttymagic.c	Sun Nov 24 06:59:09 2013 +0000
+++ b/rvinterf/etm/ttymagic.c	Sun Nov 24 08:03:27 2013 +0000
@@ -87,3 +87,26 @@
 		}
 	}
 }
+
+void
+async_msg_output(msg)
+	char *msg;
+{
+	int msglen, i;
+
+	msglen = strlen(msg);
+	if (ttyhacks)
+		putchar('\r');
+	fputs(msg, stdout);
+	if (ttyhacks)
+		for (i = msglen; i < usercmd_len + 1; i++)
+			putchar(' ');
+	putchar('\n');
+	if (!ttyhacks)
+		return;
+	/* reprint the input line */
+	putchar('>');
+	if (!usercmd_len)
+		return;
+	fwrite(usercmd, 1, usercmd_len, stdout);
+}