view rvinterf/trdump.c @ 127:f4f0c8738dcb

rvtdump: satisfactory handling of output from Pirelli's fw
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Thu, 31 Oct 2013 23:08:59 +0000
parents 811b138f1bed
children 56b53c289785
line wrap: on
line source

/*
 * This module implements the basic dump of any incoming packets
 */

#include <sys/types.h>
#include <stdio.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);
	}
}

void
print_rv_trace()
{
	int i;

	fputs("RV ", stdout);
	/* the SWE static ID is sent MSB first */
	for (i = 1; i <= 4; i++)
		printf("%02X", rxpkt[i]);
	/* severity level */
	printf(" %d ", rxpkt[5]);
	print_presumed_string(rxpkt + 6, rxpkt_len - 6);
	putchar('\n');
}

void
print_l1_trace()
{
	fputs("L1: ", stdout);
	print_presumed_string(rxpkt + 1, rxpkt_len - 1);
	putchar('\n');
}

void
print_g23_trace()
{
	int i;

	fputs("G23:", stdout);
	for (i = 1; i < rxpkt_len; i++)
		printf(" %02X", rxpkt[i]);
	putchar('\n');
}

void
print_rx_packet()
{
	int i;

	switch (rxpkt[0]) {
	case RVT_RV_HEADER:
		if (rxpkt_len < 6)
			break;
		print_rv_trace();
		return;
	case RVT_L1_HEADER:
		print_l1_trace();
		return;
	case RVT_L23_HEADER:
		print_g23_trace();
		return;
	}
	/* 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');
}