/****************************************************************************/
/* Copyright 2004 MBARI														*/
/****************************************************************************/
/* Summary	: Diagnostic routines to debug OASIS3 software					*/
/* Filename : swdiag.c														*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: OASIS3 Mooring												*/
/* $Revision: 1.2 $															*/
/* Created	: 08/11/2004 rah												*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/****************************************************************************/
/* Modification History:													*/
/* 11aug2004 rah - created from OASIS usrcmds.c								*/
/* $Log: swdiag.c,v $
/* Revision 1.2	 2015/01/22 18:10:50  bobh
/* Changes due to 2015 BRS GOC cruise
/*
/* Revision 1.1	 2011/10/25 22:39:22  bobh
/* Initial oasis4 revision
/*
/* Revision 1.9	 2010/12/08 18:57:03  bobh
/* Added flags to ModBusStruct, global debug flag
/*
/* Revision 1.8	 2010/10/26 17:15:06  bobh
/* Moved ModBus errors to file
/*
/* Revision 1.7	 2010/10/25 16:42:50  bobh
/* Changes to support scripting
/*
/* Revision 1.6	 2010/05/06 22:25:46  bobh
/* Added user functions to manipulate remote I/O, changed function parser to parse and pass 4 parameters
/*
/* Revision 1.5	 2009/11/19 19:05:58  bobh
/* Changes to support con to remote ports and fixes to file I/O due to testing
/*
/* Revision 1.4	 2009/04/29 20:35:28  bobh
/* Added remSerChown, minor other mods
/*
/* Revision 1.3	 2009/04/27 22:49:43  bobh
/* First working version of remote serial ports
/*
/* Revision 1.2	 2009/04/17 18:05:04  bobh
/* Changes leading up to CDR
/*
/* Revision 1.1	 2009/03/25 20:34:41  bobh
/* Initial checkin
/*
/* Revision 1.1	 2004/08/18 21:07:25  bobh
/* Software diagnostic routines
/*
 */
/****************************************************************************/

#include <mbariTypes.h>					/* MBARI type definitions			*/
#include <mbariConst.h>					/* MBARI constants					*/
#include <oasis.h>						/* OASIS controller definitions		*/
#include <custom.h>						/* PWR_PERM definition				*/
#include <debug.h>
#include <olist.h>						/* OASIS linked list definitions	*/
#include <serial.h>						/* OASIS serial I/O definitions		*/
#include <sem.h>						/* OASIS semaphore definitions		*/
#include <otask.h>						/* OASIS task dispatcher			*/
#include <malloc.h>						/* OASIS malloc routines			*/
#include <log.h>						/* OASIS data logging definitions	*/
#include <drvr.h>						/* OASIS driver functions			*/
#include <modbus.h>						/* For Modbus testing				*/
#include <remote.h>						/* Remote I/O library			*/
#include <swdiag.h>						/* Software diagnostic func protos	*/
#include <utils.h>						/* OASIS Utility routines			*/
#include <timer.h>						/* OASIS Clock module				*/
#include <usrcmds.h>

#include <istdio.h>						/* Standard I/O						*/
#include <stdio.h>						/* Standard I/O						*/
#include <string.h>						/* String library functions			*/
#include <ctype.h>						/* Standard ctype.h defs			*/


/********************************/
/*		External Data			*/
/********************************/

Extern Nat32	modbusDebug;
Extern Errno	modErrno;
Extern Parm_t	con_dsc;				/* Disconnect character for CONNECT */
Extern Parm_t	con_tmout;				/* Timeout during CONNECT			*/
Extern Parm_t	brkchr;					/* Character that sends break		*/

#define REM_BREAK_TIME	1000			/* Length of serial break in ms		*/


/********************************/
/*		Module Local Data		*/
/********************************/

MLocal	testThreadNum = 0;

typedef struct
{
	Parm_t		serport;
	Parm_t		parm2;
	Parm_t		loops;
} TaskTestStruct;


#ifdef DEBUG_FREEZE_CMD

/************************************************************************/
/* Function	   : usr_freeze												*/
/* Purpose	   : Get/Set task dispatcher freeze state					*/
/* Inputs	   : Parm Mask, New freeze state							*/
/* Outputs	   : OK														*/
/************************************************************************/
CmdRtn usr_freeze(ParmMask_t pmask, Parm_t newState)
{
  return(OK);

} /* freeze() */

#endif

#ifdef DEBUG_SYSTEM_CMD

/************************************************************************/
/* Function	   : showSystem												*/
/* Purpose	   : Show state of system: dispatcher, etc					*/
/* Inputs	   : None													*/
/* Outputs	   : OK														*/
/************************************************************************/
CmdRtn showSystem(ParmMask_t pmask, Parm_t skipSem)
{
  return(OK);
 
} /* showSystem() */

#endif /* DEBUG_SYSTEM_CMD */


/************************************************************************/
/* Function	   : startTestThread										*/
/* Purpose	   : UserIF routine to start a test thread to put msgs to ser*/
/* Inputs	   : Parm Mask, serial port, delay, number of loops			*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn startTestThread(ParmMask_t pmask, Parm_t serPort, Parm_t delay, Parm_t loops)
{
	return(OK);

} /* startTestThread() */


/************************************************************************/
/* Function	   : testDelay												*/
/* Purpose	   : Test task_delay()										*/
/* Inputs	   : O3 ticks												*/
/* Outputs	   : None													*/
/************************************************************************/
CmdRtn testDelay(ParmMask_t pmask, Parm_t ticks)
{
	return(OK);

} /* testDelay() */


/************************************************************************/
/* Function	   : testDelay1												*/
/* Purpose	   : Test task_delay_rtcTicks()								*/
/* Inputs	   : Seconds, RTC ticks										*/
/* Outputs	   : None													*/
/************************************************************************/
CmdRtn testDelay1(ParmMask_t pmask, Parm_t seconds, Parm_t rtcTicks)
{
	return(OK);

} /* testDelay1() */


/************************************************************************/
/* Function	   : modbusTest												*/
/* Purpose	   : Modbus Test											*/
/* Inputs	   : Parm Mask, serial port, slave ID, number of slaves, mode*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn modbusTest(ParmMask_t pmask, Parm_t testPort, Parm_t testSlave, Parm_t numSlaves)
{
	return(OK);
}


/************************************************************************/
/* Function	   : remSerConnect											*/
/* Purpose	   : Open a connection to remote serial port				*/
/* Inputs	   : Remote Serial Handle									*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn remSerConnect(RemSerHandle rsh)
{
	return(OK);
}


/************************************************************************/
/* Function	   : remConnect												*/
/* Purpose	   : Send test string to remote serial port					*/
/* Inputs	   : Parm Mask, serial port, slave ID, number of slaves, mode*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn remConnect(ParmMask_t pmask, Parm_t bus, Parm_t slave, Parm_t port)
{
	return(OK);

} /* remConnect() */


#ifdef DEBUG_KILL

/************************************************************************/
/* Function	   : taskKillTest											*/
/* Purpose	   : Test task creation and killing task that owns semaphore*/
/* Inputs	   : None													*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn taskKillTest(Void)
{
	return(OK);

} /* taskKilltest() */

#endif /* DEBUG_KILL */


/************************************************************************/
/* Function	   : testNum												*/
/* Purpose	   : Check output of CmdIsNumber
/* Inputs	   : Parm Mask, string										*/
/* Outputs	   : OK														*/
/************************************************************************/
CmdRtn testNum(ParmMask_t pmask, char *str)
{
	int			rtn, cnvlen;
	Nat32		lval;
	float		fval;
	char		buf[32];

	if ((pmask & 1) == 0)
		return(ERROR);
	
	rtn = sscanf(str, " %d%n", &lval, &cnvlen);
	xprintf("sscanf(%%d) returns %d (len %d), parsed value is %d\n", rtn, cnvlen, lval);

	rtn = sscanf(str, " %x%n", &lval, &cnvlen);
	xprintf("sscanf(%%x) returns %d (len %d), parsed value is %d\n", rtn, cnvlen, lval);

	rtn = sscanf(str, " %f%n", &fval, &cnvlen);
	xprintf("sscanf(%%f) returns %d (len %d), parsed value is %d.%03u  ",
			rtn, cnvlen, (int)fval, (Nat32)(1000*(fval - (int)fval)));

	sprintf(buf, "%.3f\n", fval);
	xputs(buf);

	return(OK);

} /* testNum() */


/************************************************************************/
/* Function	   : usrEcho												*/
/* Purpose	   : Test command-line args									*/
/* Inputs	   : Parm Mask, string, string, string, string				*/
/* Outputs	   : OK														*/
/************************************************************************/
CmdRtn usrEcho(ParmMask_t pmask, Parm_t s1, Parm_t s2, char *s3, char *s4)
{
	if (pmask & 1)
		xprintf("Arg 1: %u\n", s1);

	if (pmask & 2)
		xprintf("Arg 2: %u\n", s2);

	if (pmask & 4)
		xprintf("Arg 3: %s\n", s3);

	if (pmask & 8)
		xprintf("Arg 4: %s\n", s4);

	return(OK);

} /* usrEcho() */


/************************************************************************/
/* Function	   : echoAll												*/
/* Purpose	   : Test command-line args									*/
/* Inputs	   : Parm Mask, string, string, string, string				*/
/* Outputs	   : OK														*/
/************************************************************************/
CmdRtn echoAll(ParmMask_t pmask, char *s1, char *s2, char *s3, char *s4)
{
	if (pmask & 1)
		xprintf("Arg 1: %s\n", s1);

	if (pmask & 2)
		xprintf("Arg 2: %s\n", s2);

	if (pmask & 4)
		xprintf("Arg 3: %s\n", s3);

	if (pmask & 8)
		xprintf("Arg 4: %s\n", s4);

	return(OK);

} /* echoAll() */

