/************************************************************************/
/* Copyright 2009-2016 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 <log.h>						/* Log record definitions		*/
#include <drvr.h>						/* OASIS driver functions		*/
#include <utils.h>						/* OASIS Utility routines		*/
#include <timer.h>						/* OASIS time functions			*/

#include <string.h>
#include <time.h>

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


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

void	ctdsim_drv(Driver *dp);


/****************************************************/
/*	Drive Descriptors for Driver in this module		*/
/****************************************************/

const DrvDesc ctdSimDrvDesc =
	{ "CTDSim", ctdsim_drv, nullWakeFunc, LS_NULL, NULL,
	  30, NO_SERIAL, 9600, MODE_N81NF, 0, LOG_ASCII, 0, 10, 0, 0, 0};


/************************************************************************/
/* 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))
		{
			now = clkTime();
			nowtm = gmtime(&now);
			strftime(buffer, BUFLEN, "%d %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() */
