#ifndef _sio32Server_H
#define _sio32Server_H
/****************************************************************************/
/* Copyright 1990 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : Server Declarations for RS485 microcontroller serial protocol */
/* Filename : sio32Server.h                                                 */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV VME to Microcontroller Communications Interface   */
/* Version  : 2.1                                                           */
/* Created  : 10/23/91                                                      */
/* Modified : 03/08/96                                                      */
/* Archived :                                                               */
/****************************************************************************/

#define SIO32_BOARD_NAME "sio32"    /* Name of SIO32 Board SIO32 server     */
#define TELEM_BOARD_NAME "telem"    /* Name of Telemetry Board SIO32 server */

#define SIO32_REQUEST_Q  "requestQ" /* Name of Server Request Queue         */
#define SIO32_STATUS_Q   "statusQ"  /* Name of Status Reply Q               */

typedef enum
{
    GMS_BOARD         = 0x0100,     /* GMS Board Server                     */
    TELEM_BOARD       = 0x0200,     /* Telemetry Board Server               */
    QUAD_SERIAL_BOARD = 0x0400      /* Quad Serial Board Server             */
} sio32ServerId;

#define MAX_CHAN_NAME         20    /* Maximum length of Sio32 channel name */
#define MAX_REQ_MSG           24    /* Message count for Server Request Q   */
#define MAX_REQ_MSG_LEN      100    /* Message length for Server Request Q  */
#define MAX_PKT_MSG           10    /* Message count for Data Packets       */
#define SIO32_MSG_Q_TIMEOUT    1    /* Number of ticks to wait for msg q    */

#define INT_VEC_SIO32        208    /* SIO32 Mezzanine Board Interrupt Vect */
#define INT_LVL_SIO32          5    /* SIO32 Mezzanine Board Interrupt Level*/

#define INT_VEC_TELEM        208    /* Telemetry Board Interrupt Vector     */
#define INT_LVL_TELEM          5    /* Telemetry Board Interrupt Level      */

#define FRAME_RX_TIME          5    /* .05 second timeout frame response    */
#define KEEP_ALIVE_TIME       20    /* Keep Alive time in ticks 333 msec    */
#define MAX_TX_TRYS            5    /* Number of re-transmit attempts       */

#define SIO32_PRIORITY        30    /* Priority for sio32 tasks             */

#define SIO32_TX_PRI    SIO32_PRIORITY            /* Lowest priority task   */
#define SIO32_RX_PRI    SIO32_PRIORITY - 1        /* Medium priority task   */

#define SIO32_DEAMON_STACK  2500    /* Deamon Task stack size & TCB         */
#define SIO32_TX_STACK      2500    /* Transmit task stack size & TCB       */
#define SIO32_RX_STACK      2500    /* Receive task stack size & TCB        */

typedef enum                        /* ******* SIO32 Server Commands ****** */
{
    SIO32_INIT_CHAN   = 0x100,      /* Channel Initializarion Request       */
    SIO32_CLOSE_CHAN  = 0x101,      /* Channel Initializarion Request       */
    SIO32_PS_STATUS   = 0x102,      /* Get Power Supply Status Request      */
    SIO32_GET_STATS   = 0x103,      /* Read data transmission statistics    */
    SIO32_CLR_STATS   = 0x104,      /* Clear data transmission statistics   */
    SIO32_SET_KA_MODE = 0x105       /* Set keep alive packet mode           */
} sio32ServerCmd;

typedef struct                      /* Holds the serial channel statistics  */
{
    Nat32   totalRecvd;             /* Total number of packets recevied     */
    Nat32   totalSent;              /* Total number of packet sent          */
    Nat32   txError;                /* Total number of transmit errors      */
    Nat32   rxError;                /* Total number of receive errors       */
} pktStat;                          /* Packet transmit statistics           */

typedef struct
{
    Int32   ticks;                  /* Keep alive ticks or WAIT_FOREVER       */
    Nat32   count;                  /* Num of consecutive keep alives to send */
    Nat32   numSent;                /* Consecutive keep alive packets sent    */
    SEM_ID  mutexSem;               /* Keep Alive Data Mutual Exclustion Sem  */
} sio32KeepAliveMode;

typedef struct
{
    char     name[MAX_CHAN_NAME];   /* SIO32 Channel name for sio32DevCreate  */
    Nat32    baudRate;              /* See SIO32 manual for supported rates   */
    Nat32    dataBits;              /* Number of data bits. 5 through 8       */
    Nat32    stopBits;              /* Number of stop bits. 1 or 2            */
    char     parity[8];             /* "NONE", "ODD" or "EVEN"                */
    sioProto protocol;              /* Select RS232 or RS485 protocol mode    */

    sio32KeepAliveMode keepAlive;   /* Keep Alive Packet Configuration        */
} sio32InitReq;                     /* Channel initialization request         */

typedef struct
{
    Int32      sio32Fd;             /* Sio32 serial channel file descriptor   */
    Nat16      channel;             /* Sio32 channel number                   */
    sioProto   protocol;            /* RS232/RS485 Protocol Flag              */

    Byte    nextTxSeqNo;            /* Next Transmit Sequence Number          */
    Byte    nextRxSeqNo;            /* Next Receive Sequence Number           */
    Byte    nextRxSrqSeqNo;         /* Next Receive SRQ Sequence Number       */

    sio32KeepAliveMode keepAliveMode;   /* Keep Alive Packet Configuration    */

    SEM_ID  frameRecvdSem;          /* Frame received semaphore               */
    SEM_ID  writeMutexSem;          /* Write mutual exclusion semaphore       */

    pktStat statistics;             /* Serial packet I/O statistics           */
    Nat16   debugLevel;             /* Debug message display level            */
} chanProto;

                                    /* Extract server & channel id from chan  */
#define SIO32_SERVER(chan)       (chan & 0x0700)
#define SIO32_CHANNEL(chan)      (chan & 0x00ff)

#define QUAD_SER_ADDR(chan)      ((chan & 0xE000)>>8)
#define QUAD_SER_CHAN(chan)      ((chan & 0x1800)>>11)

#define QS_BOARD_ADDR_0   0x8000
#define QS_BOARD_ADDR_1   0xA000

#define QS_CHAN_0         0x0000
#define QS_CHAN_1         0x0800
#define QS_CHAN_2         0x1000
#define QS_CHAN_3         0x1800

#ifdef __STDC__                     /* ANSI Function prototypes               */

STATUS sio32DeamonTask( const char *serverName );

#else

STATUS sio32DeamonTask();

#endif /* __STDC__ */
#endif /* _sio32Server_H */
