changeset 133:56b53c289785

rvtdump: some restructuring
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 02 Nov 2013 22:24:14 +0000
parents 2c5160a9d652
children e0d56e9be8a2
files rvinterf/packetrx.c rvinterf/rvtdump.c rvinterf/trdump.c
diffstat 3 files changed, 125 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/packetrx.c	Sat Nov 02 01:48:11 2013 +0000
+++ b/rvinterf/packetrx.c	Sat Nov 02 22:24:14 2013 +0000
@@ -14,6 +14,8 @@
 u_char rxpkt[MAXPKT];
 size_t rxpkt_len;
 
+extern char pr_item[];
+
 static int in_pkt, dle_state, toobig;
 
 static void
@@ -21,19 +23,29 @@
 {
 	if (!in_pkt) {
 		if (inb != STX || dle_state) {
+			rxpkt_len++;
 			dle_state = (inb == DLE);
 			return;
 		}
+		if (rxpkt_len) {
+			sprintf(pr_item,
+				"Warning: Rx %u byte%s outside of a packet",
+				(unsigned)rxpkt_len, rxpkt_len != 1 ? "s" : "");
+			print_item();
+			rxpkt_len = 0;
+		}
 		in_pkt = 1;
-		rxpkt_len = 0;
 		toobig = 0;
 		return;
 	}
 	if (dle_state) {
 		dle_state = 0;
 		if (inb != STX && inb != DLE) {
-			printf("Rx framing error: %02X after DLE\n", inb);
+			sprintf(pr_item,
+				"Rx framing error: %02X after DLE\n", inb);
+			print_item();
 			in_pkt = 0;
+			rxpkt_len = 0;
 			return;
 		}
 		goto data;
@@ -46,11 +58,13 @@
 			return;
 		in_pkt = 0;
 		handle_rx_packet();
+		rxpkt_len = 0;
 		return;
 	}
 data:	if (rxpkt_len >= MAXPKT) {
 		if (!toobig) {
-			printf("Rx packet too big!\n");
+			sprintf(pr_item, "Error: Rx packet too big!\n");
+			print_item();
 			toobig = 1;
 		}
 		return;
--- a/rvinterf/rvtdump.c	Sat Nov 02 01:48:11 2013 +0000
+++ b/rvinterf/rvtdump.c	Sat Nov 02 22:24:14 2013 +0000
@@ -12,6 +12,8 @@
 extern int target_fd;
 extern char *baudrate_name;
 
+extern char pr_item[];
+
 main(argc, argv)
 	char **argv;
 {
@@ -61,3 +63,8 @@
 {
 	print_rx_packet();
 }
+
+print_item()
+{
+	printf("%s\n", pr_item);
+}
--- a/rvinterf/trdump.c	Sat Nov 02 01:48:11 2013 +0000
+++ b/rvinterf/trdump.c	Sat Nov 02 22:24:14 2013 +0000
@@ -4,83 +4,136 @@
 
 #include <sys/types.h>
 #include <stdio.h>
+#include <string.h>
+#include <strings.h>
 #include "pktmux.h"
 
 extern u_char rxpkt[];
 extern size_t rxpkt_len;
 
-void
-print_presumed_string(str, len)
-	u_char *str;
-	size_t len;
-{
-	int i, c;
-
-	if (len >= 3 &&
-	    (str[len-2] == '\r' && str[len-1] == '\n' ||
-	     str[len-2] == '\n' && str[len-1] == '\r'))
-		len -= 2;
-	for (i = 0; i < len; i++) {
-		c = str[i];
-		if (c & 0x80) {
-			putchar('M');
-			putchar('-');
-			c &= 0x7F;
-		}
-		if (c < 0x20) {
-			putchar('^');
-			putchar(c + '@');
-		} else if (c == 0x7F) {
-			putchar('^');
-			putchar('?');
-		} else
-			putchar(c);
-	}
-}
+char pr_item[4096];
 
 void
 print_rv_trace()
 {
-	int i;
+	int i, c;
+	char *dp;
 
-	fputs("RV ", stdout);
+	dp = pr_item;
+	strcpy(dp, "RV ");
+	dp += 3;
 	/* the SWE static ID is sent MSB first */
-	for (i = 1; i <= 4; i++)
-		printf("%02X", rxpkt[i]);
+	for (i = 1; i <= 4; i++) {
+		sprintf(dp, "%02X", rxpkt[i]);
+		dp += 2;
+	}
 	/* severity level */
-	printf(" %d ", rxpkt[5]);
-	print_presumed_string(rxpkt + 6, rxpkt_len - 6);
-	putchar('\n');
+	sprintf(dp, " %d ", rxpkt[5]);
+	dp = index(dp, '\0');
+	for (i = 6; i < rxpkt_len; i++) {
+		c = rxpkt[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';
+	print_item();
 }
 
 void
 print_l1_trace()
 {
-	fputs("L1: ", stdout);
-	print_presumed_string(rxpkt + 1, rxpkt_len - 1);
-	putchar('\n');
+	int i, c;
+	char *dp;
+
+	dp = pr_item;
+	strcpy(dp, "L1: ");
+	dp += 4;
+	for (i = 1; i < rxpkt_len; i++) {
+		if ((i+1 < rxpkt_len) &&
+		    (rxpkt[i] == '\r' && rxpkt[i+1] == '\n' ||
+		     rxpkt[i] == '\n' && rxpkt[i+1] == '\r')) {
+			*dp = '\0';
+			print_item();
+			if (i+2 == rxpkt_len)
+				return;
+			dp = pr_item;
+			*dp++ = '+';
+			*dp++ = ' ';
+			i++;
+			continue;
+		}
+		c = rxpkt[i];
+		if (c & 0x80) {
+			*dp++ = 'M';
+			*dp++ = '-';
+			c &= 0x7F;
+		}
+		if (c < 0x20) {
+			*dp++ = '^';
+			*dp++ = c + '@';
+		} else if (c == 0x7F) {
+			*dp++ = '^';
+			*dp++ = '?';
+		} else
+			*dp++ = c;
+	}
+	/* will get here only if no newline sequence at the end */
+	*dp = '\0';
+	print_item();
 }
 
 void
 print_g23_trace()
 {
 	int i;
+	char *dp;
 
-	fputs("G23:", stdout);
-	for (i = 1; i < rxpkt_len; i++)
-		printf(" %02X", rxpkt[i]);
-	putchar('\n');
+	dp = pr_item;
+	strcpy(dp, "G23:");
+	dp += 4;
+	for (i = 1; i < rxpkt_len; i++) {
+		sprintf(dp, " %02X", rxpkt[i]);
+		dp += 3;
+	}
+	*dp = '\0';
+	print_item();
+}
+
+void
+print_unknown_packet()
+{
+	int i;
+	char *dp;
+
+	dp = pr_item;
+	strcpy(dp, "UNK:");
+	dp += 4;
+	for (i = 0; i < rxpkt_len; i++) {
+		sprintf(dp, " %02X", rxpkt[i]);
+		dp += 3;
+	}
+	*dp = '\0';
+	print_item();
 }
 
 void
 print_rx_packet()
 {
-	int i;
-
 	switch (rxpkt[0]) {
 	case RVT_RV_HEADER:
 		if (rxpkt_len < 6)
-			break;
+			goto unknown;
 		print_rv_trace();
 		return;
 	case RVT_L1_HEADER:
@@ -89,10 +142,8 @@
 	case RVT_L23_HEADER:
 		print_g23_trace();
 		return;
+	default:
+	unknown:
+		print_unknown_packet();
 	}
-	/* default case: print the whole packet in hex as an unknown */
-	fputs("UNK:", stdout);
-	for (i = 0; i < rxpkt_len; i++)
-		printf(" %02X", rxpkt[i]);
-	putchar('\n');
 }