view sw/sniff-dec/pps.c @ 41:118a12e9483b

simtrace3-sniff-dec started
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 08:46:23 +0000
parents
children 2855330ab96f
line wrap: on
line source

/*
 * Here we implement PPS request/response message decoding.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "state.h"

extern char linebuf[];
extern int lineno;
extern unsigned rx_byte;
extern int state;

#define	SUBST_PPS0	0
#define	SUBST_PPS1	1
#define	SUBST_PPS2	2
#define	SUBST_PPS3	3
#define	SUBST_PCK	4

static char pps_start_timestamp[18];
static int pps_start_line;
static int substate;
static u_char pps_bytes[6];
static unsigned byte_count;

void
start_pps_msg()
{
	strcpy(pps_start_timestamp, linebuf);
	pps_start_line = lineno;
	pps_bytes[0] = rx_byte;
	byte_count = 1;
	state = STATE_PPS_MSG;
	substate = SUBST_PPS0;
}

static void
advance_state()
{
	if (substate == SUBST_PPS1) {
		if (pps_bytes[1] & 0x10)
			return;
		substate = SUBST_PPS2;
	}
	if (substate == SUBST_PPS2) {
		if (pps_bytes[1] & 0x20)
			return;
		substate = SUBST_PPS3;
	}
	if (substate == SUBST_PPS3) {
		if (pps_bytes[2] & 0x40)
			return;
		substate = SUBST_PCK;
	}
	if (substate == SUBST_PCK)
		return;
	fprintf(stderr, "BUG in PPS decoder: bad state in advance_state()\n");
	abort();
}

static void
check_pck()
{
	unsigned n, xor;

	xor = 0;
	for (n = 0; n < byte_count; n++)
		xor ^= pps_bytes[n];
	printf(" PCK is %s\n", xor ? "bad!" : "correct");
}

static void
pps_finish()
{
	unsigned n;

	printf("%s line %d: PPS", pps_start_timestamp, pps_start_line);
	for (n = 0; n < byte_count; n++)
		printf(" %02X", pps_bytes[n]);
	putchar('\n');
	check_pck();
	state = STATE_READY_FOR_CMD;
}

void
pps_byte_in()
{
	pps_bytes[byte_count++] = rx_byte;
	switch (substate) {
	case SUBST_PPS0:
		substate = SUBST_PPS1;
		advance_state();
		return;
	case SUBST_PPS1:
		substate = SUBST_PPS2;
		advance_state();
		return;
	case SUBST_PPS2:
		substate = SUBST_PPS3;
		advance_state();
		return;
	case SUBST_PPS3:
		substate = SUBST_PCK;
		return;
	case SUBST_PCK:
		pps_finish();
		return;
	default:
		fprintf(stderr,
			"BUG in PPS decoder: bad state in pps_byte_in()\n");
		abort();
	}
}