/************************************************************************/
/* Copyright 2009 MBARI													*/
/************************************************************************/
/* Summary	: Remote serial and power ports for Respiromters			*/
/* Filename : remote.c													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Benthic/Midwater Respirometers (BRS/MRS)					*/
/* Revision: 1.0														*/
/* Created	: 04/07/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:												*/
/* 07apr2009 rah - created												*/
/************************************************************************/

#include <mbariTypes.h>					/* MBARI type definitions		*/
#include <mbariConst.h>					/* MBARI constants				*/
#include <oasis.h>						/* OASIS controller definitions */
#include <custom.h>						/* Custom def's for deployment	*/
#include <list.h>						/* OASIS Linked List library	*/
#include <sem.h>						/* OASIS Semaphore library		*/
#include <task.h>						/* OASIS task dispatcher		*/
#include <serial.h>						/* OASIS Serial I/O				*/		
#include <utils.h>						/* OASIS Utility routines		*/
#include <modbus.h>						/* Modbus protocol library		*/
#include <remote.h>						/* Remote I/O library			*/
#include <parse.h>						/* Generic parser declarations	*/
#include <variable.h>					/* Variable definitions			*/
#include <debug.h>
#include <swdiag.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


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

//Extern Errno	modErrno;
//Extern MBool	blinky;


/********************************************************/
/*				Global Data								*/
/* (Global only so we can share with usrRemoteCmds.c)	*/
/********************************************************/

Global RemSerPort		*serPorts[VIRT_SER_PORTS];
Global RemIOPort		*pwrPorts[VIRT_PWR_PORTS];
Global RemIOPort		*outPorts[VIRT_OUT_PORTS];
Global RemID			remIDs[REMOTE_IDS];


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

MLocal Semaphore remSem;


/************************************************************************/
/* Function	   : initRemoteLib											*/
/* Purpose	   : Initialize this module									*/
/* Inputs	   : None													*/
/* Outputs	   : None													*/
/************************************************************************/
void initRemoteLib(void)
{
	int			i;

	memset(serPorts, 0, sizeof(serPorts));
	memset(pwrPorts, 0, sizeof(pwrPorts));
	memset(outPorts, 0, sizeof(outPorts));

#if 0
	for (i = 0; i < NumberOf(pwrPorts); i++) {
		pwrPorts[i].multiplier = DFLT_MULT;
		pwrPorts[i].surgeDelayMs = DFLT_SURGE;
	}

	for (i = 0; i < NumberOf(outPorts); i++) {
		outPorts[i].multiplier = DFLT_MULT;
		outPorts[i].surgeDelayMs = DFLT_SURGE;
	}

	for (i = 0; i < NumberOf(remIDs); i++) {
		remIDs[i].assigned = FALSE;
	}
#endif

	semCreateMutex(&remSem);
	semName(&remSem, "remoteSem");

} /* initRemoteLib() */


/************************************************************************/
/* Function	   : takeRemoteSem											*/
/* Purpose	   : Take the remSem semaphore								*/
/* Inputs	   : None													*/
/* Outputs	   : None													*/
/************************************************************************/
void takeRemoteSem(void)
{
} /* takeRemoteSem() */


/************************************************************************/
/* Function	   : giveRemoteSem											*/
/* Purpose	   : Give back the remSem semaphore							*/
/* Inputs	   : Delay ms before giving sem								*/
/* Outputs	   : None													*/
/************************************************************************/
Void giveRemoteSem(Nat32 delay_ms)
{
} /* giveRemoteSem() */


/************************************************************************/
/* Function	   : findRemPwrPort											*/
/* Purpose	   : Find remote power port number from string input		*/
/* Inputs	   : String to look up										*/
/* Outputs	   : Port number or ERROR									*/
/************************************************************************/
Int16 findRemPwrPort(char *s)
{
	return(ERROR);

} /* findRemPwrPort() */


/************************************************************************/
/* Function	   : findRemOutputPort										*/
/* Purpose	   : Find remote Output port number from string input		*/
/* Inputs	   : String to look up										*/
/* Outputs	   : Port number or ERROR									*/
/************************************************************************/
Int16 findRemOutputPort(char *s)
{
	return(ERROR);

} /* findRemOutputPort() */


/************************************************************************/
/* Function	   : getVirtSerPort											*/
/* Purpose	   : Get ptr to RemSerPort, or NULL if port invalid			*/
/* Inputs	   : Virtual serial port number								*/
/* Outputs	   : RemSerHandle or NULL									*/
/************************************************************************/
RemSerHandle getVirtSerPort(Nat32 virtPort)
{
	return(NULL);

} /* getVirtSerPort() */


/************************************************************************/
/* Function	   : assignRemoteID											*/
/* Purpose	   : Allocate a remID for a chamber controller				*/
/* Inputs	   : Modbus bus, slave ID									*/
/* Outputs	   : None													*/
/************************************************************************/
RemID *assignRemoteID(ModBusType busType, Port_t bus, Nat16 slave)
{
} /* assignRemoteID() */


/************************************************************************/
/* Function	   : assignVirtSerial										*/
/* Purpose	   : Assign a virtual serial port							*/
/* Inputs	   : Virtual port num, identifiers for remote port			*/
/* Outputs	   : OK or PORT_CONFLICT									*/
/************************************************************************/
Errno assignVirtSerial(char *name, Nat32 virtPort, ModBusType busType, Nat16 bus,
					   Nat16 slave, Nat16 port, Flt32 mult, Nat32 surgeDelay)
{
	return(OK);
}

/************************************************************************/
/* Function	   : assignVirtPwr											*/
/* Purpose	   : Assign a virtual power port							*/
/* Inputs	   : Virtual port num, identifiers for remote port			*/
/* Outputs	   : OK or PORT_CONFLICT									*/
/************************************************************************/
Errno assignVirtPwr(char *name, Nat32 virtPort, ModBusType busType, Nat16 bus,
					Nat16 slave, Nat16 coil, Flt32 mult, Nat32 surgeDelay)
{
	return(OK);
}

/************************************************************************/
/* Function	   : assignVirtOutput										*/
/* Purpose	   : Assign a virtual output port							*/
/* Inputs	   : Virtual port num, identifiers for remote port			*/
/* Outputs	   : OK or PORT_CONFLICT									*/
/************************************************************************/
Errno assignVirtOutput(char *name, Nat32 virtPort, ModBusType busType, Nat16 bus,
					   Nat16 slave, Nat16 coil, Flt32 mult, Nat32 surgeDelay)
{
	return(OK);
}

/************************************************************************/
/* Function	   : openRemSerHandle										*/
/* Purpose	   : Local func to manipulate RemSerPort for an open		*/
/* Inputs	   : RemSerPort, boolean to open async						*/
/* Outputs	   : OK or ERROR											*/
/* Comment	   : If async==TRUE and semaphore not avail, return PORT_IN_USE*/
/************************************************************************/
Errno openRemSerHandle(RemSerHandle rsh, MBool async)
{
	return(ERROR);

} /* openRemSerHandle() */


/************************************************************************/
/* Function	   : findRemSerHandle										*/
/* Purpose	   : Find a remote serial port handle						*/
/* Inputs	   : bus, slave, port										*/
/* Outputs	   : RemSerHandle, or NULL if error							*/
/************************************************************************/
RemSerHandle findRemSerHandle(ModBusType busType, Nat16 bus, Nat16 slave, Nat16 port)
{
	return(NULL);

} /* findRemSerHandle() */


/************************************************************************/
/* Function	   : remSerOpen												*/
/* Purpose	   : Open a remote serial port								*/
/* Inputs	   : bus, slave, port, boolean TRUE to open async			*/
/* Outputs	   : RemSerHandle, or NULL if error							*/
/* Comment	   : If async==TRUE and semaphore not avail, return NULL	*/
/************************************************************************/
RemSerHandle remSerOpen(ModBusType busType, Nat16 bus, Nat16 slave, Nat16 port, MBool async)
{
	return(NULL);

} /* remSerOpen() */


/************************************************************************/
/* Function	   : remOpenVirtSer											*/
/* Purpose	   : Open a virtual serial port								*/
/* Inputs	   : Virtual serial port number, boolean to open async		*/
/* Outputs	   : Errno													*/
/* Comment	   : If async==TRUE and semaphore not avail, return PORT_IN_USE*/
/************************************************************************/
Errno remOpenVirtSer(Nat32 virtPort, MBool async)
{
	return(PORT_CONFLICT);

} /* remOpenVirtSer() */


/************************************************************************/
/* Function	   : remSerClose											*/
/* Purpose	   : Close a remote serial port								*/
/* Inputs	   : RemSerHandle											*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remSerClose(RemSerHandle rsh)
{
	return(ERROR);

} /* remSerClose() */


/************************************************************************/
/* Function	   : remCloseVirtSer										*/
/* Purpose	   : Close a virtual serial port							*/
/* Inputs	   : RemSerHandle											*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remCloseVirtSer(Nat32 virtPort)
{
	return(ERROR);

} /* remCloseVirtSer() */


/************************************************************************/
/* Function	   : remSerInit												*/
/* Purpose	   : Set baud rate and mode for a remote serial port		*/
/* Inputs	   : RemSerHandle, baud rate, mode							*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remSerInit(RemSerHandle rsh, Nat32 baud, Nat32 mode)
{
	return(ERROR);

} /* remSerInit() */


/************************************************************************/
/* Function	   : remVirtSerInit											*/
/* Purpose	   : Set baud rate for a virtual serial port				*/
/* Inputs	   : Virtual ser port number, baud rate, mode				*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remVirtSerInit(Nat32 virtPort, Nat32 baud, Nat32 mode)
{
	return(ERROR);

} /* remVirtSerInit() */


/************************************************************************/
/* Function	   : remPower												*/
/* Purpose	   : Turn on/off a remote power bit							*/
/* Inputs	   : Remote Power number, boolean							*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remPower(Nat32 pwrnum, MBool onoff)
{
	return(ERROR);

} /* remPower() */


/************************************************************************/
/* Function	   : remOutput												*/
/* Purpose	   : Turn on/off a remote output bit						*/
/* Inputs	   : Remote Output number, boolean							*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remOutput(Nat32 outnum, MBool onoff)
{
	return(ERROR);

} /* remOutput() */


/************************************************************************/
/* Function	   : remIsPowerOn											*/
/* Purpose	   : See if any power bits set on a given ModBus			*/
/* Inputs	   : ModBus number											*/
/* Outputs	   : TRUE if any power bits set, else FALSE					*/
/************************************************************************/
Errno remIsPowerOn(Port_t bus)
{
	return(FALSE);

} /* remIsPowerOn() */


/************************************************************************/
/* Function	   : remIsOutputOn											*/
/* Purpose	   : See if any output bits set on a given ModBus			*/
/* Inputs	   : ModBus number											*/
/* Outputs	   : TRUE if any output bits set, else FALSE				*/
/************************************************************************/
Errno remIsOutputOn(Port_t bus)
{
	return(FALSE);

} /* remIsOutputOn() */


/************************************************************************/
/* Function	   : remOff													*/
/* Purpose	   : Turn off ALL remote outputs and power bits				*/
/* Inputs	   : None													*/
/* Outputs	   : OK														*/
/************************************************************************/
Errno remOff(Void)
{
	return(OK);

} /* remOff() */


/************************************************************************/
/* Function	   : remOutPulseOn											*/
/* Purpose	   : Pulse on remote output bit								*/
/* Inputs	   : Remote Output number, Number of ms to pulse on			*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remOutPulseOn(Nat32 bitNum, Flt32 sec)
{
	return(ERROR);

} /* remOutPulseOn() */


/************************************************************************/
/* Function	   : remOutPulseOnByVolume									*/
/* Purpose	   : Pulse on remote output bit								*/
/* Inputs	   : Remote Output number, volume to pulse on				*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remOutPulseOnByVolume(Nat32 bitNum, Flt32 vol)
{
	return(ERROR);

} /* remOutPulseOnByVolume() */


/************************************************************************/
/* Function	   : remOutPulseOff											*/
/* Purpose	   : Pulse off remote output bit							*/
/* Inputs	   : Remote Output number, Number of ms to pulse off		*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno remOutPulseOff(Nat32 bitNum, Flt32 sec)
{
	return(ERROR);

} /* remOutPulseOff() */


/************************************************************************/
/* Function	   : remSerRead												*/
/* Purpose	   : Read a remote serial port								*/
/* Inputs	   : Remote serial handle, buffer ptr, length				*/
/* Outputs	   : Number of chars read									*/
/************************************************************************/
Nat32 remSerRead(RemSerHandle rsh, char *buffer, Nat32 len)
{
	return(0);

} /* remSerRead() */


/************************************************************************/
/* Function	   : remSerWrite											*/
/* Purpose	   : Write a remote serial port								*/
/* Inputs	   : Remote serial handle, buffer ptr, length				*/
/* Outputs	   : Number of chars written								*/
/************************************************************************/
Nat32 remSerWrite(RemSerHandle rsh, const char *buffer, Nat32 len)
{
	return(0);

} /* remSerWrite() */


/************************************************************************/
/* Function	   : remSerDrain											*/
/* Purpose	   : Wait for serial output stream to finish sending		*/
/* Inputs	   : RemSerHandle, ticks to wait							*/
/* Outputs	   : Errno													*/
/************************************************************************/
Errno remSerDrain(RemSerHandle rsh, Nat32 tmout)
{
	return(ERROR);

} /* remSerDrain() */


/************************************************************************/
/* Function	   : remSerIFlush											*/
/* Purpose	   : Flush remote serial input stream						*/
/* Inputs	   : RemSerHandle											*/
/* Outputs	   : Errno													*/
/************************************************************************/
Errno remSerIFlush(RemSerHandle rsh)
{
	return(ERROR);

} /* remSerIFlush() */


/************************************************************************/
/* Function	   : remSerOFlush											*/
/* Purpose	   : Flush remote serial output stream						*/
/* Inputs	   : RemSerHandle											*/
/* Outputs	   : Errno													*/
/************************************************************************/
Errno remSerOFlush(RemSerHandle rsh)
{
	return(ERROR);

} /* remSerOFlush() */


/************************************************************************/
/* Function	   : remSerRxCount											*/
/* Purpose	   : Get number of queued Rx characters						*/
/* Inputs	   : RemSerHandle											*/
/* Output	   : Number of queued Rx characters							*/
/************************************************************************/
Int16 remSerRxCount(RemSerHandle rsh)
{
	return(0);

} /* remSerRxCount() */


/************************************************************************/
/* Function	   : remSerBreak											*/
/* Purpose	   : Send a break character to remote output stream			*/
/* Inputs	   : RemSerHandle, break time in ms							*/
/* Outputs	   : Errno													*/
/************************************************************************/
Errno remSerBreak(RemSerHandle rsh, Nat16 breakTime)
{
	return(ERROR);

} /* remSerBreak() */


/************************************************************************/
/* Function	   : remTaskExit											*/
/* Purpose	   : Close remote ports owned by exiting task				*/
/* Inputs	   : Task Descriptor ptr of exiting task					*/
/* Outputs	   : None													*/
/************************************************************************/
		Void
remTaskExit(TaskHandle_t *td)
{
} /* remTaskExit() */


/************************************************************************/
/* Function	   : setAllSlaves											*/
/* Purpose	   : Set coil in all chamber controllers					*/
/* Inputs	   : Coil number, coil state								*/
/* Outputs	   : None													*/
/************************************************************************/
MLocal Void setAllSlaves(Nat16 coil, MBool state)
{

} /* setAllSlaves() */


/************************************************************************/
/* Function	   : remErrorThread											*/
/* Purpose	   : Thread that blinks remote LEDs to indicate error		*/
/* Inputs	   : None													*/
/* Outputs	   : None													*/
/************************************************************************/
MLocal Void remErrorThread()
{

} /* remErrorThread() */


/************************************************************************/
/* Function	   : remCreateErrorThread									*/
/* Purpose	   : Create thread that blinks remote LEDs to indicate error*/
/* Inputs	   : None													*/
/* Outputs	   : None													*/
/************************************************************************/
Void remSetError()
{

} /* remSetError() */


/* From usrRemoteCmds.c	*/

/************************************************************************/
/* Function	   : usrRemPower											*/
/* Purpose	   : User I/F function to turn on/off a (remote) power bit	*/
/* Inputs	   : pmask, Power bit, bool for on/off						*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn usrRemPower(Nat16 pmask, char *bitStr, char *onOffStr)
{
	return(OK);

} /* usrRemPower() */


/************************************************************************/
/* Function	   : usrRemOutput											*/
/* Purpose	   : User I/F function to turn on/off a remote output bit	*/
/* Inputs	   : pmask, Output bit, bool for on/off						*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn usrRemOutput(Nat16 pmask, char *bitStr, char *onOffStr)
{
	return(OK);

} /* usrRemOutput() */


/************************************************************************/
/* Function	   : usrRemOutPulse											*/
/* Purpose	   : User I/F function to pulse a remote output bit			*/
/* Inputs	   : pmask, Output bit, time in ms							*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn usrRemOutPulse(Nat16 pmask, char *bitStr, char *timeStr)
{
	return(OK);

} /* usrRemOutPulse() */


/************************************************************************/
/* Function	   : usrAssignSerial										*/
/* Purpose	   : User I/F function to assign a virtual serial port		*/
/* Inputs	   : pmask, virt serport number, bus, slave, port			*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn usrAssignSerial(Nat16 pmask, Parm_t virtPort, Parm_t bus, Parm_t slave, Parm_t port)
{
	return(OK);

} /* usrAssignSerial() */


/************************************************************************/
/* Function	   : usrAssignPower											*/
/* Purpose	   : User I/F function to assign a virtual power port		*/
/* Inputs	   : pmask, virt power number, bus, slave, port				*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn usrAssignPower(Nat16 pmask, Parm_t virtPort, Parm_t bus, Parm_t slave, Parm_t port)
{
	return(OK);

} /* usrAssignPower() */


/************************************************************************/
/* Function	   : usrAssignOutput										*/
/* Purpose	   : User I/F function to assign a virtual output port		*/
/* Inputs	   : pmask, virt output number, bus, slave, port			*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
CmdRtn usrAssignOutput(Nat16 pmask, Parm_t virtPort, Parm_t bus, Parm_t slave, Parm_t port)
{
	return(OK);

} /* usrAssignOutput() */


/************************************************************************/
/* Function	   : showRemote												*/
/* Purpose	   : Show status of remote/virtual ports					*/
/* Inputs	   : None													*/
/* Outputs	   : OK														*/
/************************************************************************/
CmdRtn showRemote(void)
{
	return(OK);
}
