/******************************************************************************

$Id: test_fcc_api.c,v 1.1 2000/06/09 16:19:13 jrieffel Exp $

$Log: test_fcc_api.c,v $
Revision 1.1  2000/06/09 16:19:13  jrieffel
Fuel Cell Test programs

Revision 1.1  2000/05/09 12:53:02  root
Initial revision



******************************************************************************/ 

// Test program for fcc_api3r.lib

#ifdef __USAGE

%C
	-d	debug flag	
		enable debug output
	-T node
		target node number
	
#endif

#include <stdio.h>			// fprintf etc......
#include <sys/types.h>		// for nid_t
#include <stdlib.h>			// for EXIT_FAILURE		
#include <unistd.h>			// for getopt,getnid

#include <mvc_fcc_api.h>

BOOLEAN debug_flag=FALSE;	// enable debug statements in this program and the libraries
char *ProgramName;			// our executable name
nid_t target_node;			// node on which the Fuel Cell control software is running

void main(int argc, char **argv)
{
// argument processing stuff
extern char		*optarg;
extern int		opterr, optopt;
int i;

int reply_code;
FCC_STATUS fcc_status;
FCC_DATA fcc_data;
	
	// save our program name for display in error messages
	ProgramName = argv[0];

	// assume that we're running on the same node as the target unless the user specifies
	// a command line option
	target_node = getnid();

	// command line argument processing
	while( (i = getopt( argc, argv, "dT:" )) != -1 )
		switch( i )
		{
			case 'd':	debug_flag = TRUE;
			break;
			case 'T':	target_node = atoi(optarg);
			break;

			default:
				if ( debug_flag )
				{
					fprintf( stderr, "%s: Invalid Option '%c'\n", ProgramName, optopt ); 
				}
				exit( EXIT_FAILURE );
		}
	
	if ( debug_flag )
		fprintf(stderr,"Sending start command to Fuel Cell Controller ...\n");
		
	// Send a START command to the Fuel Cell Controller
	if( ( reply_code=send_FCC_command(FCC_CMD_START)) != FCC_ERR_OK )
	{
		switch(reply_code)
		{
			case FCC_ERR_INVALID_PARAM:
				// Invalid command passed to the API
			break;
			
			case FCC_ERR_COMM:
				// API encountered communication problems
			break;
			
		}
		if( debug_flag )
		{
			fprintf(stderr, "%s : Start command failed ...\n", ProgramName);
			fprintf(stderr, "reply_code: %d\n",reply_code);
			exit( EXIT_FAILURE );
		}
	}

	if ( debug_flag )
		fprintf(stderr,"Sending get_FCC_data() request to Fuel Cell Controller ...\n");
	
	// Send a data request to the fuel cell controller
	if ((reply_code = get_FCC_data(&fcc_data)) != FCC_ERR_OK)
	{
		switch(reply_code)
		{
			case FCC_ERR_INVALID_PARAM:
				// Invalid parameter passed to the API

			break;
			
			case FCC_ERR_COMM:
				// API encountered communication problems
			break;
		}
		if( debug_flag )
		{
			fprintf(stderr, "%s : get_FCC_data() request failed ...\n", ProgramName);
			fprintf(stderr, "reply_code: %d\n",reply_code);
			exit( EXIT_FAILURE );
		}
	}
	else
	{
		if( debug_flag )
		{
			fprintf(stderr, "%s : Sent data message...\n", ProgramName );
			fprintf(stderr, "Data Reply:\n");
			fprintf(stderr, "elapsed_time %d\n",fcc_data.elapsed_time);
			fprintf(stderr, "anolyte_pump_a_current %d\n",fcc_data.anolyte_pump_a_current);
			fprintf(stderr, "anolyte_pump_b_current %d\n",fcc_data.anolyte_pump_b_current);
			fprintf(stderr, "catholyte_pump_current %d\n",fcc_data.catholyte_pump_current);
			fprintf(stderr, "peroxide_pump_current %d\n",fcc_data.peroxide_pump_current);
			fprintf(stderr, "anolyte_temp %d\n",fcc_data.anolyte_temp);
			fprintf(stderr, "catholyte_temp %d\n",fcc_data.catholyte_temp);
			fprintf(stderr, "fwd_peroxide_temp %d\n",fcc_data.fwd_peroxide_temp);
			fprintf(stderr, "aft_peroxide_temp %d\n",fcc_data.aft_peroxide_temp);
			fprintf(stderr, "stack_voltage %d\n",fcc_data.stack_voltage);
			fprintf(stderr, "stack_current %d\n",fcc_data.stack_current);
			fprintf(stderr, "battery_voltage %d\n",fcc_data.battery_voltage);
			fprintf(stderr, "battery_current %d\n",fcc_data.battery_current);
			fprintf(stderr, "peroxide_used %d\n",fcc_data.peroxide_used);
		}

		// Save the FCC data on disk for subsequent transmission to 
		// the satellite
	}
	
	if ( debug_flag )
		fprintf(stderr,"Sending get_FCC_status() request to Fuel Cell Controller ...\n");
	
	// Send a status request to the fuel cell controller
	if ( (reply_code = get_FCC_status(&fcc_status) ) != FCC_ERR_OK)
	{
		switch(reply_code)
		{
			case FCC_ERR_INVALID_PARAM:
				// Invalid parameter passed to the API

			break;
		
			case FCC_ERR_COMM:
				// API encountered communication problems
			break;
		}
		if( debug_flag )
		{
			fprintf(stderr, "%s : get_FCC_status() request failed ...\n", ProgramName);
			fprintf(stderr, "reply_code: %d\n",reply_code);
			exit( EXIT_FAILURE );
		}
	}
	else
	{
		if( debug_flag )
		{
			fprintf(stderr, "%s : Sent status request..\n", ProgramName);
			fprintf(stderr, "Status Reply:\n");
			fprintf(stderr, "fc_control_state: %d\n",fcc_status.fc_control_state);
			fprintf(stderr, "health: %lX\n",fcc_status.health);
			fprintf(stderr, "FC_remaining_kwh: %d\n",fcc_status.FC_remaining_kwh);
			fprintf(stderr, "RB_remaining_wh: %d\n",fcc_status.RB_remaining_wh);
			fprintf(stderr, "BIT_status: %X\n",fcc_status.BIT_status);
			fprintf(stderr, "BIT_result: %X\n",fcc_status.BIT_result);
		}
		
		// Check health status of the Fuel Cell
	}
	
	if ( debug_flag )
		fprintf(stderr,"Sending offline command to Fuel Cell Controller ...\n");
	
	// Send an OFFLINE command to the Fuel Cell Controller, this needs to be
	// sent before sending a STOP command.
	if( ( reply_code=send_FCC_command(FCC_CMD_OFFLINE)) != FCC_ERR_OK )
	{
		switch(reply_code)
		{
			case FCC_ERR_INVALID_PARAM:
				// Invalid parameter passed to the API
			break;
			
			case FCC_ERR_COMM:
				// API encountered communication problems
			break;
			
		}
		if( debug_flag )
		{
			fprintf(stderr, "%s : Offline command failed ...\n", ProgramName);
			fprintf(stderr, "reply_code: %d\n",reply_code);
			exit( EXIT_FAILURE );
		}
	}
	
	if ( debug_flag )
		fprintf(stderr,"Sending stop command to Fuel Cell Controller ...\n");

	// Send a STOP command to the Fuel Cell Controller
	if( ( reply_code=send_FCC_command(FCC_CMD_STOP)) != FCC_ERR_OK )
	{
		switch(reply_code)
		{
			case FCC_ERR_INVALID_PARAM:
				// Invalid parameter passed to the API
			break;
			
			case FCC_ERR_COMM:
				// API encountered communication problems
			break;
			
		}
		if( debug_flag )
		{
			fprintf(stderr, "%s : Stop command failed ...\n", ProgramName);
			fprintf(stderr, "reply_code: %d\n",reply_code);
			exit( EXIT_FAILURE );
		}
	}

}	// end main()
