view rvinterf/lowlevel/format_g23.c @ 176:7f727aaf5cd4

rvinterf: beginning of server implementation
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 23 Nov 2013 07:40:13 +0000
parents 2f214bd03119
children 2f285f20d617
line wrap: on
line source

/*
 * This module implements the decoding of G23 trace packets into
 * human-readable form.  Because I have not yet reached the point
 * of integrating the target-side code that generates these packets,
 * I do not yet have the proper understanding of their format.
 * Therefore, the current decoding logic implemented here is based
 * on a heuristic examination of what the packets look like to an
 * uninitiated eye.
 */

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "../pktmux.h"
#include "../limits.h"

extern u_char rxpkt[];
extern size_t rxpkt_len;

static int
is_well_formed()
{
	int i, c;

	if (rxpkt_len < 17)
		return(0);
	for (i = 8; i < 16; i++) {
		c = rxpkt[i];
		if (c < ' ' || c > '~')
			return(0);
	}
	return(1);
}

static void
print_well_formed()
{
	char buf[MAX_PKT_FROM_TARGET*4];
	int i, c;
	char *dp;

	dp = buf;
	strcpy(dp, "G23:");
	dp += 4;
	for (i = 1; i <= 7; i++) {
		sprintf(dp, " %02X", rxpkt[i]);
		dp += 3;
	}
	sprintf(dp, " \"%.4s\" \"%.4s\" ", rxpkt+8, rxpkt+12);
	dp += 15;
	i = 16;
	if (rxpkt[i] < 0x20) {
		sprintf(dp, "%02X ", rxpkt[i]);
		dp += 3;
		i++;
	}
	if (rxpkt_len - i == 5 && rxpkt[i] == '%' &&
	    !rxpkt[i+3] && !rxpkt[i+4]) {
		sprintf(dp, "%d", rxpkt[i+2] << 8 | rxpkt[i+1]);
		output_line(buf);
		return;
	}
	*dp++ = '\"';
	for (; 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++ = '\"';
	*dp = '\0';
	output_line(buf);
}

static void
print_malformed()
{
	char buf[MAX_PKT_FROM_TARGET*3+6];
	int i;
	char *dp;

	dp = buf;
	strcpy(dp, "G23 UNK:");
	dp += 8;
	for (i = 1; i < rxpkt_len; i++) {
		sprintf(dp, " %02X", rxpkt[i]);
		dp += 3;
	}
	*dp = '\0';
	output_line(buf);
}

void
print_g23_trace()
{
	if (is_well_formed())
		print_well_formed();
	else
		print_malformed();
}