/****************************************************************************/
/* Copyright 2001 MBARI														*/
/****************************************************************************/
/* $Header: /home/cvs/oasis4/src/controller/drivers/emeter.c,v 1.1 2015/01/14 18:58:51 bobh Exp $					*/
/* Summary	: Driver Routines for xantrex emeter battery monitor			*/
/* Filename : emeter.c														*/
/* Author	: Kent Headley (klh)											*/
/* Project	: OASIS Mooring													*/
/* $Revision: 1.1 $															*/
/* Created	: 06/1/01														*/
/*																			*/
/* 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:													*/
/* 1jun01 klh - created												*/
/* $Log: emeter.c,v $
/* Revision 1.1	 2015/01/14 18:58:51  bobh
/* Added to act as proxy for pH driver on respirometers
/*
/* Revision 1.1	 2003/08/19 18:57:35  headley
/* no message
/*
 * Revision 4.4	 2001/06/19	 12:13:31  12:13:31	 oasisa (Oasis users)
 * New Repository; 6/19/2001 (klh)
 * 
 * Revision 1.1	 2001/06/19	 11:43:25  11:43:25	 oasisa (Oasis users)
 * Initial revision
 * 
*/
/****************************************************************************/

#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 <list.h>						/* OASIS Linked List library		*/
#include <sem.h>						/* OASIS Semaphore library			*/
#include <task.h>						/* OASIS Multitasking definitions	*/
#include <drvr.h>						/* OASIS driver functions			*/
#include <utils.h>						/* OASIS Utility routines			*/

#include <string.h>						/* String library functions			*/

#define EMETER_BUFSIZE	128				/* Specified max is 1340			*/
#define EMETER_WAKETIME 3
#define EMETER_FLUSHTIME		(TICKS_PER_SECOND/2)


/********************************/
/*		Function Prototype		*/
/********************************/

Void	emeter_drv(Driver *dp);


/************************************************************************/
/* Function	   : wakeup_emeter											*/
/* Purpose	   : Wakeup emeter									*/
/* Inputs	   : Driver ptr, buffer										*/
/* Outputs	   : TRUE if woke, else FALSE								*/
/************************************************************************/
/*
		MLocal MBool
wakeup_emeter( Driver *dp, char *buffer )
{
	Int16		i;
	Nat16		st_tick;
	
	for ( i = dp->drv_parms[PARM0]; i; i-- )
	{
	  if ( xgets_tmout(buffer, EMETER_BUFSIZE, EMETER_WAKETIME) > 0 )
		{
		  return( TRUE );
		}

		xflush_ser( TICKS_PER_SECOND - (tick - st_tick) );
	}
	
	return( FALSE );

} *//* wakeup_emeter() */


/************************************************************************/
/* Function	   : emeter_drv												*/
/* Purpose	   : Driver for xantrex emeter battery monitor				*/
/* Inputs	   : Driver Pointer											*/
/* PARMs	   : 
		   TIMEOUT: seconds to wait on serial read
				 0: wakeup retries
				 1: read retries
				 2: unused (Horrible Hack: used as on-time for NMC freewave)(klh)
 */
/* Outputs	   : None													*/
/************************************************************************/
		Void
emeter_drv( Driver *dp )
{
	Reg Int16	emeterLen=0;
	char		*emeterBuf, *p;
	Nat16		tmout,i;
	Int16		c;

	if ( (emeterBuf = drvSerPortAndMalloc(dp, EMETER_BUFSIZE)) == NULL )
	{
		drvLogError( dp, MALLOC_ERR );			/* No malloc space		*/
		return;
	}
	
	/*		 if ( wakeup_emeter(dp, emeterBuf)==TRUE )
			 {*/
												/* Got mem and wakeup	*/
		tmout = dp->drv_parms[TIMEOUT];
		for ( i = dp->drv_parms[PARM1]; (i>0 && emeterLen<=0) ; i-- )
		{
		  xgets_tmout(emeterBuf, EMETER_BUFSIZE, tmout);
								/* get to end of (possibly partial) line */
		  while (((c = xgetc_tmout(tmout)) == '\n') || (c == '\r'))
			;					/* Throw away following LF/CR's			*/
		  
		  p = emeterBuf;
		  if (c != ERROR)		/* If got another char, save it			*/
			*p++ = c;

		  if (xgets_tmout(p, EMETER_BUFSIZE-1, tmout) >= 0)
		  {						/* now get the next full line */
			  emeterBuf[EMETER_BUFSIZE-1]='\0';
			  emeterLen=strlen(emeterBuf);
			  drvLog( dp,(Byte *)emeterBuf,emeterLen );
			  break;
			}else{
			  drvLogError(dp,NO_DATA);
			}
		}				/* No wakeup			*/
		/*}else{
		drvLogError( dp, SYNC_ERR );
	  }*/

		drvSerReleaseAndFree( dp, emeterBuf );	/* Release serial port	*/

} /* emeter_drv() */




