/************************************************************************/
/* Copyright 2009-2015 MBARI                                            */
/************************************************************************/
/* Summary  : Definitions for interfacing to libO5Exp                   */
/* Filename : o5expander.h                                              */
/* Author   : Robert Herlien (rah)                                      */
/* Project  : OASIS5                                                    */
/* Revision: 1.0                                                        */
/* Created  : 11/5/2015 from sensorNode.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:                                                */
/* 5nov2015, rah, created from sensorNode.h                             */
/************************************************************************/

#ifndef INC_o5eh
#define INC_o5eh  1

#include <sys/types.h>
#include <sys/time.h>
#include <stdint.h>


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

#define O5_EXPANDERS   16               /* Max O5 Expanders supported    */
#define O5E_SER_PORTS 6                 /* Max Num ser ports on O5Exp or daughter*/
//#define O5E_PWR_PORTS   5               /* Num pwr ports on O5Expander  */
#define O5E_AD_CHANS    0               /* Num A/D channels on O5Expander*/

#ifndef INCo5eLibh
typedef void    *O5ExpHandle;
typedef void    *RemSerHandle;
#endif

typedef short int       MBool;
typedef int             Errno;

#ifndef FALSE
#define FALSE   0
#endif
#ifndef TRUE
#define TRUE    ~FALSE
#endif
#ifndef ON
#define ON      TRUE
#endif
#ifndef OFF
#define OFF     FALSE
#endif

#define OK       0
#define SUCCESS  0
#define ERROR   -1
#define FOREVER while(TRUE)

    
/****************************************/
/* Serial Error Bits                    */
/****************************************/
#define PERR_BIT        0x10
#define FERR_BIT        0x08
#define OERR_BIT        0x04
#define XERR_BIT        0x02
#define YERR_BIT        0x01

/****************************************/
/* Board Configuration                  */
/****************************************/

typedef enum                        /* Board Types        */
{ BOARD_TYPE_CONFIGURED=0, BOARD_TYPE_CHAMBER_CONTROLLER,
  BOARD_TYPE_SENSOR_NODE, BOARD_TYPE_O5_EXPANDER, BOARD_TYPE_O5_DAUGHTER
} BoardType;

typedef struct                  /********************************/
{                               /* BoardConfig struct           */
    uint16_t    boardType;      /* See BoardType above          */
    uint16_t    numSerPorts;    /* Number of serial ports       */
    uint16_t    numRelays;      /* Num pwr/serial relays        */
    uint16_t    numDIO;         /* Num Dig I/O bits (Chmbr Ctrlr)*/
    uint16_t    numAnalog;      /* Num analog ports (SensorNode)*/
    uint16_t    serSwitch;      /* Non-zero if serial switched with power*/
} BoardConfig;                  /********************************/

typedef enum
{ BLINKY_OFF = 0, BLINKY_ON, BLINKY_DEBUG24, BLINKY_DEBUG, BLINKY_ERROR, BLINKY50 }
BlinkyState;


#define openO5Expander(dev, baud, slaveID)      openO5ExpanderDebug(dev, baud, slaveID, FALSE)


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

/* Library-wide functions       */
void            initO5ExpanderLib(void);
void            o5eSetDebug(MBool onoff);
O5ExpHandle     openO5ExpanderDebug(char *dev, int baud, int slaveID, MBool dbg);
Errno           closeO5Expander(O5ExpHandle o5eh);

/* libmodbus functions made available to sensorNode library */
void            o5eModbusSetDebug(O5ExpHandle o5eh, MBool onoff);
void            o5eModbusGetResponseTimeout(O5ExpHandle o5eh, struct timeval *timeout);
void            o5eModbusSetResponseTimeout(O5ExpHandle o5eh, struct timeval *timeout);
void            o5eModbusGetByteTimeout(O5ExpHandle o5eh, struct timeval *timeout);
void            o5eModbusSetByteTimeout(O5ExpHandle o5eh, struct timeval *timeout);

/* Functions to get/set Modbus timing   */
Errno           setO5ExpModbusTimeout(O5ExpHandle o5eh, uint16_t tmout50us);
Errno           getO5ExpModbusTimeout(O5ExpHandle o5eh, uint16_t *tmout50us);

/* Remote Serial Functions      */
RemSerHandle    o5eOpenSerial(O5ExpHandle o5eh, uint16_t port);
Errno           o5eSerInit(RemSerHandle rsh, int baud, char parity, int stop);
Errno           o5eCloseSerial(RemSerHandle rsh);

uint32_t        o5eSerRead(RemSerHandle rsh, char *buffer, uint32_t len);
uint32_t        o5eSerWrite(RemSerHandle rsh, const char *buffer, uint32_t len);
Errno           o5eSerDrain(RemSerHandle rsh, uint32_t tmout);
Errno           o5eSerIFlush(RemSerHandle rsh);
Errno           o5eSerOFlush(RemSerHandle rsh);
int16_t         o5eSerRxCount(RemSerHandle rsh);
Errno           o5eSerBreak(RemSerHandle rsh, uint16_t breakTime);
Errno           o5eSerSetTxEnbl(RemSerHandle rsh, MBool enbl);
Errno           o5eSerGetErrFlags(RemSerHandle rsh, uint16_t *flags);
Errno           o5eSerClearErrFlags(RemSerHandle rsh, uint16_t flags);

/* Remote Power Functions       */
Errno           o5ePower(O5ExpHandle o5eh, uint16_t port, MBool onoff);
int16_t         o5ePowerIsOn(O5ExpHandle o5eh, uint16_t port);
Errno           o5eGetPowerVector(O5ExpHandle o5eh, uint16_t *vect);

/* Misc         */
Errno           o5eGetSerialNum(O5ExpHandle o5eh, uint16_t *num);
Errno           o5eGetSoftwareRev(O5ExpHandle o5eh, uint16_t *num);
Errno           o5eGetBoardConfig(O5ExpHandle o5eh, BoardConfig *cfgp);
Errno           o5eSetBlinky(O5ExpHandle o5eh, BlinkyState state);
Errno           o5eGetBlinky(O5ExpHandle o5eh, BlinkyState *statep);
Errno           o5eResetConfigMode(O5ExpHandle o5eh);

#endif  /* INC_o5eh */
