/****************************************************************************/
/* Copyright 1995-2016 MBARI												*/
/****************************************************************************/
/* $Header: /home/cvs/oasis4/src/controller/drivers/ctd.c,v 1.1 2011/10/25 22:39:21 bobh Exp $						*/
/* Summary	: Driver Routines for Seabird CTD on OASIS mooring				*/
/* Filename : ctd.c															*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: OASIS Mooring													*/
/* $Revision: 1.1 $															*/
/* Created	: 02/15/95 from sensors.c										*/
/*																			*/
/* 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:													*/
/* 15feb95 rah - created from sensors.c										*/
/* 18aug2016 rah - modified for Oasis5										*/
/* $Log$
*/
/****************************************************************************/

#include <oasis.h>						/* OASIS controller definitions		*/
#include <log.h>						/* Log record definitions			*/
#include <custom.h>						/* CTD_TS definition				*/
#include <drvr.h>						/* OASIS driver functions			*/
#include <utils.h>						/* OASIS Utility routines			*/

#define CTD_BUFSIZE		64				/* Leave a little extra room for CTD*/


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

Extern char		*ctdCmd;
Extern	char	*ctdSync;


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

Void	ctd_drv(Driver *dp);


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

const char *ctdParmDesc[] = 
	{"Min size good rcd", "Retries", NULL, NULL, NULL, NULL, NULL, NULL};

const DrvDesc ctdDrvDesc =
	{ "CTD", ctd_drv, nullWakeFunc, LS_NULL, ctdParmDesc,
	  600, NO_SERIAL, 9600, MODE_N81NF, 0, LOG_BIN, 0, 3, 10, 3, 0};


/************************************************************************/
/* Function	   : getCtdPrompt											*/
/* Purpose	   : Look for CTD prompt									*/
/* Inputs	   : Timeout in seconds										*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
MLocal Int16 getCtdPrompt(Nat16 timeout)
{
	Reg Int16	c;
	Int16		i;
	
	for (i = 10; i && ((c = xgetc_tmout(timeout)) != ERROR); i--)
		if ( c == '>' )					/* Look for prompt				*/
			return( OK );				/* If got it, return OK			*/

	return( ERROR );					/* If timed out, return ERROR	*/

} /* getCtdPrompt() */


/************************************************************************/
/* Function	   : get_ctd												*/
/* Purpose	   : Make one attempt to get CTD data						*/
/* Inputs	   : Buffer, Driver pointer									*/
/* Outputs	   : Number of bytes received from CTD						*/
/************************************************************************/
MLocal Nat16 get_ctd(char *ctd_buf, Driver *dp)
{
	Reg Int16	i, c;					/* Loop cntr & char buffer		*/
	Reg char	*p;						/* Buffer ptr					*/
	Nat16		tmout;					/* Timeout						*/

	xputc('\r');						/* Get CTD's attention			*/
	tmout = dp->drv_parms[TIMEOUT];		/* Get timeout					*/

	if ( getCtdPrompt(tmout) != OK )	/* Look for prompt				*/
		return( 0 );					/* If timed out, return no data */

	xprintf("%s\n", ctdCmd);			/* Take Sample command			*/

	while ( xgets_tmout(ctd_buf, CTD_BUFSIZE, tmout) >= 0 )
	{
		for ( p = ctd_buf, i = 0; *p; p++ )
			if ( (c = getByte(p, 16)) != ERROR )
			{							/* Convert ASCII to hex in place*/
				ctd_buf[i++] = c;		/* If ASCII, increment ptr by 2 */
				p++;					/*	and continue converting		*/
			}							/* If not hex, we might still be*/
										/*	getting CTD prompt			*/
		if ( i > 0 )					/* If got some hex bytes,		*/
			return( i );				/* Return bytes we got			*/
	}

	return( 0 );						/* If timed out, return 0		*/

} /* get_ctd() */


/************************************************************************/
/* Function	   : ctd_drv												*/
/* Purpose	   : CTD driver												*/
/* Inputs	   : Driver Pointer											*/
/* Outputs	   : None													*/
/************************************************************************/
void	ctd_drv(Driver *dp)
{
	Int16		i, size, len;
	char		ctd_buf[CTD_BUFSIZE];	/* Buffer for CTD data			*/
	
	drv_ser_port( dp );					/* Get serial port				*/

	if (ctdSync != NULL)
	  drvSync(ctdSync);					/* Wait for water pump			*/

	size = dp->drv_parms[PARM0];		/* Find number bytes to look for*/
	if ( size > CTD_BUFSIZE )			/* If more than buffer, truncate*/
		size = CTD_BUFSIZE;

	for ( i = dp->drv_parms[PARM1]; 
		  i && (len = get_ctd(ctd_buf, dp)) < size; i-- )
		;

	drv_ser_release( dp );				/* Release serial port			*/

	if (len > 0)
	  drvLog( dp, (Byte *)ctd_buf, len ); /* Log CTD data				*/
	else if (len < 0)
	  drvLogError(dp,BAD_DATA);
	else
	  drvLogError(dp,SYNC_ERR);

	drv_pwroff( dp );					/* Turn off power				*/

} /* ctd_drv() */
