/************************************************************************/
/* Copyright 2009-2016 MBARI											*/
/************************************************************************/
/* Summary	: Definitions for ModBus I/O for Oasis5						*/
/* Filename : modbus.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Oasis5 Mooring Controller									*/
/* Revision: 1.0														*/
/* Created	: 08/31/2016												*/
/*																			*/
/* 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 Oasis4 version								*/
/* 31aug2016 rah - created Oasis5 version from Oasis4 version			*/
/************************************************************************/

#ifndef INCmodbusioh
#define INCmodbusioh		1

#include <mbariTypes.h>
#include <oasis.h>
#include <timer.h>
#include <modbus.h>
#include <remote.h>

/************************************************************************/
/* Note - I moved the function declarations, even those for modbus.c,	*/
/* from modbus.h to here, to resolve circular definitions for			*/
/* ModBusHandle (modbus.h) and RemID (remote.h).						*/
/************************************************************************/

/****************************************/
/* Function Declarations				*/
/****************************************/

/* From modbus.c	*/
Errno	readCoils(ModBusHandle mh, RemID *rp, Nat16 startAddr,
				  Nat16 numCoils, Byte *result);
Errno	readInputs(ModBusHandle mh, RemID *rp, Nat16 startAddr,
					Nat16 numInputs, Byte *result);
Errno	readHoldingRegs(ModBusHandle mh, RemID *rp, Nat16 startAddr,
						Nat16 numRegs, Nat16 *result);
Errno	readHoldingRegsOwnSem(ModBusHandle mh, RemID *rp, Nat16 startAddr,
							  Nat16 numRegs, Nat16 *result);
Errno	readInputRegs(ModBusHandle mh, RemID *rp, Nat16 startAddr,
					  Nat16 numRegs, Nat16 *result);
Errno	writeOneCoil(ModBusHandle mh, RemID *rp, Nat16 coilAddr,
					 MBool on);
Errno	writeOneReg(ModBusHandle mh, RemID *rp, Nat16 regAddr,
					Nat16 value);
Errno	writeCoils(ModBusHandle mh, RemID *rp, Nat16 startAddr,
				   Nat16 numCoils, const Byte *data);

Errno	writeRegs(ModBusHandle mh, RemID *rp, Nat16 startAddr,
				  Nat16 numRegs, const Nat16 *data);
Errno	reportSlaveId(ModBusHandle mh, RemID *rp, Byte *result);
Errno	writeReadRegs(ModBusHandle mh, RemID *rp, Nat16 readAddr,
					  Nat16 numReads, Nat16 writeAddr, Nat16 numWrites,
					  const Nat16 *writeData, Nat16 *readData);
Errno	writeReadSpecial(ModBusHandle mh, RemID *rp, Nat16 readAddr,
						 Nat16 numReads, Nat16 writeAddr, Nat16 numWrites1,
						 Byte *writeData1, Nat16 numWrites2, 
						 const Byte *writeData2, Nat16 *readData);

/* From modbusio.c */
void		initModBus(Void);
ModBusStruct *getSerModStruct(void);
Errno		getModbusPort(ModBusHandle mh);
void		releaseModbusPort(ModBusHandle mh);
Errno		modBusConfigureSer(Port_t port, Nat32 baud, Nat32 sermode,
							   Nat32 surgeDelay, Nat32 flags);
MBool		modBusConfigured(Port_t port);
MBool		modBusIsPowered(Port_t port);
Int16		usrModPower(Nat16 pmask, Parm_t port, Parm_t onoff);
ModBusHandle modBusOpen(RemID *rp);
Errno		modBusClose(ModBusHandle mh);
Errno		modBusIO(ModBusHandle mh, RemID *rp, Nat16 sndLen);
Int16		showModBus(Void);
Errno		logModBus(Int32 argc, char **argv, MBool syntaxOnly);


/* From modbuscrc.c */
Nat16		mod_crc16(Byte *buffer, Nat16 buffer_length);
Errno		check_mod_crc16(Byte *msg, const Nat16 msg_length);


/* From modbuserr.c */
const char	*modBusErrString(Errno errno);
Void		logModResult(ModBusHandle mh, RemID *rp, Errno rtnCode, Nat16 sndLen);

#endif	/* INCmodbusioh */
