/******************************************************************************
 * $Id: go-nogo.c,v 1.2 2010/07/21 09:28:30 tw Exp $
 * go-nogo - Read a GDS-8x0 GO/NO-GO template data from the instrument.
 *
 * Copyright (C) 2010 Thomas Weidenfeller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/ 

#include <stdint.h>	// C99
#include <stdio.h>

#include "gds-read-bin.h"
#include "read.h"
#include "tty.h"
#include "error.h"


#define GO_NOGO_DATA_SIZE	(548)
#define GO_NOGO_PACKAGE_TYPE	(0x03)

/*
 * Read and convert the GO/NO-GO data.
 */
void
go_nogo(char *fieldsep) {
	int size = read_header();
	if(size != GO_NOGO_DATA_SIZE) {
		die("Expected a data size of %d, got %d instead\n",
						GO_NOGO_DATA_SIZE, size);
	}

	//
	// Package type.
	// 
	int c = next();
	if(c != GO_NOGO_PACKAGE_TYPE) {
		die("Expected package type ID 0x%X, got 0x%X instead\n",
				GO_NOGO_PACKAGE_TYPE, c);
	}

	printf("Package Type%s0x%02X\n", fieldsep, c);

	//
	// Template Number
	//
	c = next();
	if(c < 1 || c > 100) {
		die("Expected template number between 1 and 100, got %d instead\n", c);
	}
	printf("Template Number%s%d\n", fieldsep, c);

	//
	// Position
	//
	int16_t pos;
#if __BYTE_ORDER == __LITTLE_ENDIAN
	pos = next() << 8;	
	pos |= next();
#else
	pos = next();
	pos |= next() << 8;
#endif
	if(pos < -300 || pos > 300) {
		die("Expected position between -300 and 300, got %d instead\n", pos);
	}
	printf("Position%s%d\n", fieldsep, pos);

	//
	// Text string
	// 39 bytes including trailing \0.
	//
	char text[39];
	int i;
	for(i = 0; i < 39; i++) {
		text[i] = next();
	}
	text[38] = '\0';	// just in case
	// TODO: Check for fieldsep in text, and use some escape mechanism
	printf("Text%s%s\n", fieldsep, text);

	//
	// 1 byte reserved
	//
	next();

	uint32_t checksum = 0;
	for(i = 0; i < 4; i++) {
		checksum <<= 8;
		checksum |= (unsigned)next() & 0xFF;
	}
	printf("Checksum%s0x%08X\n", fieldsep, checksum);

	//
	// The template data, 250 int16_t
	//
	printf("Template Data\n");
	int16_t data;
	uint32_t checksum2 = 0;
	for(i = 0; i < 250; i++) {
		c = next();
		checksum2 += c;
		data = c << 8;

		c = next();
		checksum2 += c;
		data |= c;
		
		// TODO: Check range. For now trust the checksum.
		printf("%d\n", data);
	}

	if(checksum != checksum2) {
		die("Checksum error. Expected 0x%08X, got 0x%08X instead\n",
				checksum, checksum2);
	}
	// printf("Checksum%s0X%08X\n", fieldsep, checksum2);
}
