/************************************************************************/
/* Copyright 2009-2012 MBARI						*/
/************************************************************************/
/* Summary  : Definitions for remote serial ports using SensorNode	*/
/* Filename : remSerial.h						*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : xFOCE							*/
/* Revision: 1.0							*/
/* Created  : 09/19/2012 from Oasis4 remote.h				*/
/*									    */
/* 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:						*/
/* 19sep2012 rah - created from remote.h in Oasis4			*/
/************************************************************************/

#ifndef INCremSerialh
#define INCremSerialh	1

#include <modbus.h>


/****************************************/
/* Typedefs and definitions		*/
/****************************************/

#define SENSOR_NODES	32		/* Max SensorNodes supported	*/
#define SN_SER_PORTS	3		/* Number ser ports on SensorNode*/
#define SN_PWR_PORTS	3		/* Num pwr ports on SensorNode  */
#define SN_GPIO_PORTS	8		/* Num GPIO bits on SensorNode	*/
#define DEVNAME_LEN	128		/* Length of device name field	*/
#define SERBUF_SIZE	240		/* Size of serial buffer	*/	
#define SERREG_SIZE	(SERBUF_SIZE/2)

typedef struct			/****************************************/
{				/* Modbus Connection to SensorNode	*/
    modbus_t	*ctx;		/* libmodbus context			*/
    Nat16	slaveID;	/* SlaveID on this connection		*/
    Nat16	useCount;	/* Number of times opened		*/
    char	devName[DEVNAME_LEN]; /* Device serial port		*/
} SensorNodeStruct;		/****************************************/

typedef SensorNodeStruct *SensorNodeHandle;

typedef struct			/****************************************/
{				/* Remote Serial Port			*/
    SensorNodeStruct *snp;	/* SensorNodeStruct for this ser port	*/
    modbus_t	*ctx;		/* libmodbus context from snp for convenience*/
    Nat16	slaveID;	/* SlaveID on the SensorConnection	*/
    Nat16	port;		/* Serial port on the slave		*/
    Nat16	bufChars;	/* Number of chars valid in buf		*/
    Nat16	rxQueued;	/* RxQueued reg. MUST be immed b4 buf	*/
    Byte	*bufPtr;	/* Ring buffer output ptr		*/
    Byte	rbuf[SERBUF_SIZE];   /* Read Buffer for serial chars	*/
    Nat16	wbuf[SERREG_SIZE+1]; /* Write Register Buffer		*/
} RemSerPort;			/****************************************/

typedef RemSerPort *RemSerHandle;


/****************************************/
/* Remote Serial I/O Definitions	*/
/****************************************/

#define MIN_READ		10	/* Min number of regs to read	*/

/* Register and Coil Definitions	*/
#define SerStatusReg		0xff
#define TxBusy(val, port)	(val & (1 << (4*port)))
#define RxAvail(val, port)	(val & (1 << (4*port+1)))
#define TxErr(val, port)	(val & (1 << (4*port+2)))

#define BaudReg(port)		(0x100 * (port + 1))
#define ModeReg(port)		((0x100 * port) + 0x101)
#define RxCtlReg(port)		((0x100 * port) + 0x102)
#define RxQReg(port)		((0x100 * port) + 0x107)
#define TxQueuedReg(port)	((0x100 * port) + 0x183)
#define TxCtlReg(port)		((0x100 * port) + 0x184)
#define TxBreakReg(port)	((0x100 * port) + 0x185)
#define TxReqReg(port)		((0x100 * port) + 0x187)
#define OutputBit(port)		(port + 1)
#define PowerBit(port)		(port + 9)
#define TxEnable(port)		(port + 12)
#define BlinkyDebugCoil		(15)
#define BlinkyStateCoil		(16)

#define REM_NO_PTY      0
#define REM_EVEN_PTY    2
#define REM_ODD_PTY     4
#define REM_STOP2       1
#define REM_TX_FLUSH    0x20
#define REM_RX_FLUSH    0x20
#define REM_BREAK       1

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

Void		initRemoteLib(Void);
Void		remLibSetDebug(MBool onoff);
SensorNodeHandle openSensorNode(char *dev, int baud, int slaveID);
RemSerHandle	openRemSerial(SensorNodeHandle snh, int port);
int		closeSensorNode(SensorNodeHandle snh);
int		closeRemSerial(RemSerHandle rsh);
int		remSerInit(RemSerHandle rsh, int baud, char parity, int stop);

Nat32		remSerRead(RemSerHandle rsh, char *buffer, Nat32 len);
Nat32		remSerWrite(RemSerHandle rsh, const char *buffer, Nat32 len);
Errno		remSerDrain(RemSerHandle rsh, Nat32 tmout);
Errno		remSerIFlush(RemSerHandle rsh);
Errno		remSerOFlush(RemSerHandle rsh);
Int16		remSerRxCount(RemSerHandle rsh);
Errno		remSerBreak(RemSerHandle rsh, Nat16 breakTime);

#endif	/* INCremSerial */
