/************************************************************************/
/* Copyright 2009 MBARI													*/
/************************************************************************/
/* Summary	: CTD simulator for testing remote serial ports for Respirometers*/
/* Filename : ctdsim.c													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Benthic/Midwater Respirometers (BRS/MRS)					*/
/* Revision: 1.0														*/
/* Created	: 04/27/2009												*/
/*																			*/
/* 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:												*/
/* 27apr2009 rah - created												*/
/* 05nov2009 rah - rewritten as an Oasis3 Driver rather than remote port*/
/************************************************************************/

#include <mbariTypes.h>					/* MBARI type definitions		*/
#include <mbariConst.h>					/* MBARI constants				*/
#include <oasis.h>						/* OASIS controller definitions */
#include <io.h>							/* OASIS I/O definitions		*/
#include <log.h>						/* Log record definitions		*/
#include <drvr.h>						/* OASIS driver functions		*/
#include <list.h>						/* OASIS Linked List library	*/
#include <sem.h>						/* OASIS Semaphore library		*/
#include <task.h>						/* OASIS task dispatcher		*/
#include <utils.h>						/* OASIS Utility routines		*/
#include <parse.h>						/* Generic parser declarations	*/
#include <debug.h>						/* OASIS debug definitions		*/
#include <clock.h>						/* OASIS time functions			*/

#include <cfxpico.h>					/* Persistor PicoDOS definitions*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define BUFLEN			256
#define SER_POLL		(TICKS_PER_SECOND/4)


/********************************/
/*		Function Prototypes		*/
/********************************/

Void	ctdsim_drv(Driver *dp);


/************************************************************************/
/* Function	   : ctdsim_drv												*/
/* Purpose	   : Driver for CTD simulator								*/
/* Inputs	   : Driver Pointer											*/
/* Outputs	   : None													*/
/* Comments	   : To use this driver, put a null modem cable between two */
/*				 serial ports -- one assigned to this driver, one		*/
/*				 assigned to the CTD									*/
/************************************************************************/
		Void
ctdsim_drv( Driver *dp )
{
	Int16		len;
	MBool		done = FALSE;
	time_t		now;
	struct tm	*nowtm;
	char		buffer[BUFLEN];

	drv_ser_port( dp );					/* Get serial port				*/

	while(!done)
	{
		len = xgets_tmout(buffer, BUFLEN, dp->drv_parms[TIMEOUT]);

		if (len < 0)
			done = TRUE;

		else if (len == 0)
			xputs("S>");

		else if ((strncasecmp(buffer, "SL", 2) == 0) ||
				 (strncasecmp(buffer, "TS", 2) == 0))
		{
			RTCtime(&now);
			nowtm = gmtime(&now);
			strftime(buffer, BUFLEN, "%e %b %Y, %X\r", nowtm);
			xprintf("4.3594, 3.26081, 887.042, %s", buffer);
		}

		else if (strncasecmp(buffer, "QS", 2) == 0)
			done = TRUE;

		task_delay(SER_POLL);
	}

	drv_ser_release( dp );				/* Release serial port			*/
	drv_pwroff( dp );					/* Turn off power				*/

} /* ctdsim_drv() */
