view rvinterf/lowlevel/format_g23.c @ 267:4c6bee98e002

fc-tmsh: support quoted strings for set-pcm-string
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Thu, 06 Feb 2014 01:40:02 +0000
parents 2f285f20d617
children 05874f1ddacb
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 "../include/pktmux.h"
#include "../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();
}