/*
 * *************************************************************************
 *
 * Trimble Navigation, Ltd.
 * OEM Products Development Group
 * P.O. Box 3642
 * 645 North Mary Avenue
 * Sunnyvale, California 94088-3642
 *
 * Corporate Headquarter:
 *    Telephone:  (408) 481-8000
 *    Fax:        (408) 481-6005
 *
 * Technical Support Center:
 *    Telephone:  (800) 767-4822	(U.S. and Canada)
 *                (408) 481-6940    (outside U.S. and Canada)
 *    Fax:        (408) 481-6020
 *    BBS:        (408) 481-7800
 *    e-mail:     trimble_support@trimble.com
 *
 * *************************************************************************
 *
 * Vers Date               Changes                                                                                       Author
 * ---- ---------       ----------------------------------------
 * 1.10 21 Jun 93       Initial version                     pvwl
 * 1.12 14 Feb 94       Changes to byte reader              pvwl
 * 1.40 17 Jul 97		TSIP_IFC.C changes					pvwl
 * *************************************************************************
 *
 * The following files are used in compilation:
 *
 *    TSIP_IFC.H:  protoypes
 *    TSIPINCL.H:  protoypes
 *    TSIPPRNT.C:  main program
 *    TSIP_RPT.C:  report interpreter
 *    TSIP_IFC.C:  standard command/report interface for TSIP
 *
 * TSIPPRNT translates a TSIP binary data stream that has been captured.
 * by TSIPCHAT (^F or ^G options) or any serial byte recorder.
 */

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "tsip_ifc.h"
#include "tsipincl.h"

FILE *tsipstream;    /* file for reading report data */

/**/
/* Dummy routines required by TSIP_RPT.C, TSIP_IFC.C  */
void GPS_time_to_PC (double rtime)
{
	rtime = 1.0-rtime;	/* dummy statement to avoid compiler warnings */
}

short sendb (unsigned char dummy)
{
	dummy = !dummy;
	return 0;
}

short getb (void)
{
	short inbyte;

	inbyte = fgetc (tsipstream);
	return (inbyte & 0xFF00) ? -1 : inbyte;
}

void show_crlf (void) {
	printf("\n");
}

short main (short argc, char *argv[])
{
	static TSIPPKT
		rpt;                            /* structure for TSIP report  */
	unsigned long
		numchars;

	printf ("TSIPPRNT -- TSIP Packet Translator -- Ver. %s(%s)\r\n",
		TPRNT_VERNUM, TSIP_VERNUM);
	printf ("   (c) Copyright 1993, 1994, 1995 Trimble Navigation\r\n");

	/* initialize binary storage file if desired */
	if (argc != 2) {
		printf ("\n usage: tsipprnt infilename");
		exit (0);
	}
	tsipstream = fopen (argv[1], "rb");
	if (tsipstream == NULL) {
		printf ("\n usage: tsipprnt infilename");
		exit (0);
	}

	rpt.status = TSIP_PARSED_EMPTY;
	/* clear report buffer - initialize to rpt.cnt of zero */
    numchars = 0;
	do {
		numchars++;
		/* if FALSE, end-of-packet sequence not received, continue */
		tsip_input_proc (&rpt, getb());
		if (rpt.status == TSIP_PARSED_FULL) {
			rpt_packet (&rpt);
			rpt.status = TSIP_PARSED_EMPTY;
		}
		if (!(numchars & 0xFF))
		/* check every 256 characters for escape */
		{
			if (kbhit()) if (getch() == 0x1B) break;
		}
	} while (!feof (tsipstream) && !ferror (tsipstream));
	return TRUE;
}
