changeset 928:65953c172f24

rvinterf/lowlevel: new hex dump format
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 May 2023 05:23:19 +0000
parents 4e243402f453
children ab771ce34fac
files rvinterf/libprint/Makefile rvinterf/libprint/hexdump.c rvinterf/lowlevel/format.c rvinterf/lowlevel/format_fc.c rvinterf/lowlevel/logsent.c rvinterf/lowlevel/output.c
diffstat 6 files changed, 90 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/libprint/Makefile	Tue May 23 03:59:42 2023 +0000
+++ b/rvinterf/libprint/Makefile	Tue May 23 05:23:19 2023 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	back_esc.o
+OBJS=	back_esc.o hexdump.o
 LIB=	libprint.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/libprint/hexdump.c	Tue May 23 05:23:19 2023 +0000
@@ -0,0 +1,52 @@
+/*
+ * The function contained in this module implements hex dump functionality:
+ * an arbitrary binary packet is dumped in hex, line by line, calling an output
+ * function for each line.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+
+void
+packet_hex_dump(src, srclen, outfunc)
+	u_char *src;
+	unsigned srclen;
+	void (*outfunc)();
+{
+	u_char *sp = src;
+	unsigned remain = srclen;
+	unsigned offset = 0;
+	unsigned chunk;
+	char line[80], *dp;
+	int i, c;
+
+	while (remain) {
+		sprintf(line, "%04X:  ", offset);
+		dp = line + 7;
+		chunk = remain;
+		if (chunk > 16)
+			chunk = 16;
+		for (i = 0; i < 16; i++) {
+			if (i < chunk)
+				sprintf(dp, "%02X ", sp[i]);
+			else
+				strcpy(dp, "   ");
+			dp += 3;
+			if (i == 7 || i == 15)
+				*dp++ = ' ';
+		}
+		for (i = 0; i < chunk; i++) {
+			c = sp[i];
+			if (c < ' ' || c > '~')
+				c = '.';
+			*dp++ = c;
+		}
+		*dp = '\0';
+		outfunc(line);
+		sp += chunk;
+		remain -= chunk;
+		offset += chunk;
+	}
+}
--- a/rvinterf/lowlevel/format.c	Tue May 23 03:59:42 2023 +0000
+++ b/rvinterf/lowlevel/format.c	Tue May 23 05:23:19 2023 +0000
@@ -14,6 +14,8 @@
 extern u_char rxpkt[];
 extern size_t rxpkt_len;
 
+extern void output_cont();
+
 static char fmtbuf[MAX_PKT_FROM_TARGET*8];	/* size it generously */
 
 void
@@ -33,7 +35,7 @@
 	/* severity level */
 	sprintf(dp, " %d ", rxpkt[5]);
 	dp = index(dp, '\0');
-	safe_print_trace(rxpkt + 6, rxpkt_len - 6, dp);
+	safe_print_trace(rxpkt + 6, (int)rxpkt_len - 6, dp);
 	output_line(fmtbuf);
 }
 
@@ -101,33 +103,13 @@
 void
 print_tm_output_raw()
 {
-	int i;
-	char *dp;
-
-	dp = fmtbuf;
-	strcpy(dp, "TM:");
-	dp += 3;
-	for (i = 1; i < rxpkt_len; i++) {
-		sprintf(dp, " %02X", rxpkt[i]);
-		dp += 3;
-	}
-	*dp = '\0';
-	output_line(fmtbuf);
+	output_line("Rx Test Mode packet");
+	packet_hex_dump(rxpkt, (unsigned) rxpkt_len, output_cont);
 }
 
 void
 print_unknown_packet()
 {
-	int i;
-	char *dp;
-
-	dp = fmtbuf;
-	strcpy(dp, "UNK:");
-	dp += 4;
-	for (i = 0; i < rxpkt_len; i++) {
-		sprintf(dp, " %02X", rxpkt[i]);
-		dp += 3;
-	}
-	*dp = '\0';
-	output_line(fmtbuf);
+	output_line("Rx unknown packet format");
+	packet_hex_dump(rxpkt, (unsigned) rxpkt_len, output_cont);
 }
--- a/rvinterf/lowlevel/format_fc.c	Tue May 23 03:59:42 2023 +0000
+++ b/rvinterf/lowlevel/format_fc.c	Tue May 23 05:23:19 2023 +0000
@@ -13,13 +13,15 @@
 extern u_char rxpkt[];
 extern size_t rxpkt_len;
 
+extern void output_cont();
+
 static char fmtbuf[MAX_PKT_FROM_TARGET*8];	/* size it generously */
 
 void
 print_ati_output()
 {
 	strcpy(fmtbuf, "ATI: ");
-	safe_print_trace(rxpkt + 1, rxpkt_len - 1, fmtbuf + 5);
+	safe_print_trace(rxpkt + 1, (int)rxpkt_len - 1, fmtbuf + 5);
 	output_line(fmtbuf);
 }
 
@@ -27,25 +29,15 @@
 print_fc_lld_msg()
 {
 	strcpy(fmtbuf, "LLD: ");
-	safe_print_trace(rxpkt + 1, rxpkt_len - 1, fmtbuf + 5);
+	safe_print_trace(rxpkt + 1, (int)rxpkt_len - 1, fmtbuf + 5);
 	output_line(fmtbuf);
 }
 
 void
 print_tch_output_raw()
 {
-	int i;
-	char *dp;
-
-	dp = fmtbuf;
-	strcpy(dp, "TCH:");
-	dp += 4;
-	for (i = 1; i < rxpkt_len; i++) {
-		sprintf(dp, " %02X", rxpkt[i]);
-		dp += 3;
-	}
-	*dp = '\0';
-	output_line(fmtbuf);
+	output_line("Rx TCH packet");
+	packet_hex_dump(rxpkt, (unsigned) rxpkt_len, output_cont);
 }
 
 void
--- a/rvinterf/lowlevel/logsent.c	Tue May 23 03:59:42 2023 +0000
+++ b/rvinterf/lowlevel/logsent.c	Tue May 23 05:23:19 2023 +0000
@@ -12,6 +12,8 @@
 extern int no_output, verbose;
 extern FILE *logF;
 
+extern void output_cont();
+
 static void
 log_sent_ati(pkt, pktlen)
 	u_char *pkt;
@@ -34,25 +36,6 @@
 	output_line(buf);
 }
 
-static void
-log_sent_other(pkt, pktlen)
-	u_char *pkt;
-{
-	char buf[MAX_PKT_TO_TARGET*3+5];
-	int i;
-	char *dp;
-
-	dp = buf;
-	strcpy(dp, "Sent");
-	dp += 4;
-	for (i = 0; i < pktlen; i++) {
-		sprintf(dp, " %02X", pkt[i]);
-		dp += 3;
-	}
-	*dp = '\0';
-	output_line(buf);
-}
-
 log_sent_packet(pkt, pktlen)
 	u_char *pkt;
 {
@@ -62,16 +45,23 @@
 	case RVT_L23_HEADER:
 		log_sent_gpf(pkt, pktlen);
 		return;
+	case RVT_TM_HEADER:
+		output_line("Sent Test Mode packet");
+		packet_hex_dump(pkt, pktlen, output_cont);
+		return;
 	case RVT_AT_HEADER:
 		log_sent_ati(pkt, pktlen);
 		return;
 	case RVT_TCH_HEADER:
-		if (verbose)
-			goto generic;
-		tch_inc_count_tx();
+		if (verbose) {
+			output_line("Sent TCH packet");
+			packet_hex_dump(pkt, pktlen, output_cont);
+		} else {
+			tch_inc_count_tx();
+		}
 		return;
 	default:
-	generic:
-		log_sent_other(pkt, pktlen);
+		output_line("Sent misc packet");
+		packet_hex_dump(pkt, pktlen, output_cont);
 	}
 }
--- a/rvinterf/lowlevel/output.c	Tue May 23 03:59:42 2023 +0000
+++ b/rvinterf/lowlevel/output.c	Tue May 23 05:23:19 2023 +0000
@@ -40,3 +40,13 @@
 		curtm->tm_sec, item);
 	bcopy(curtm, &last_tm, sizeof(struct tm));
 }
+
+void
+output_cont(item)
+	char *item;
+{
+	if (!no_output)
+		printf("%s\n", item);
+	if (logF)
+		fprintf(logF, "%s\n", item);
+}