/**
    F&eIT protocol header
*/


/*--------------------------------------------------------------*/
/*  Communications server                                                */
/*--------------------------------------------------------------*/
/* Common header */
typedef struct  _SVHEAD {
        WORD    wSVHId;             /* Identifier */
        BYTE    bSVHVer;            /* Header structure version */
        BYTE    bSVHCommand;        /* Command */
        WORD    wSVHSeqNo;          /* Sequence number */
        WORD    wSVHReplyId;        /* Response identifier */
        DWORD   dwSVHVAddr;         /* Virtual address */
        WORD    wSVHAccessSize;     /* Access size */
        WORD    wSVHStatus;         /* Status */
        BYTE    bSVHAccInd[8];      /* Access Identifier */
        BYTE    bSVHRemMacAddr[6];  /* The other MAC address */
        BYTE    bSVHResv[6];        /* Reservation */
                                    /* Data */
}SVHEAD,*PSVHEAD;

#define SV_ID                       ( 0x5653 )      /* Identifier('SV') */
#define SV_VER                      ( 1 )           /* Header structure version */
#define SV_DATA_SIZE                ( 1436 )        /* Data size */

/* Trap command */
typedef struct _SVHTRAPDATA {
        WORD    wTrapCommand;       /* Command number */
        WORD    wTrapDataLen;       /* Data length */
        DWORD   dwTrapFragment;     /* Fragment data */
                                    /* Data */
}SVHTRAPDATA, *PSVHTRAPDATA;

/* Coomand number */
#define SV_READ                     ( 1 )           /* Read virtual address */
#define SV_WRITE                    ( 2 )           /* Write virtual address */
#define SV_MESSAGE                  ( 3 )           /* Send Message */
#define SV_TRAP                     ( 4 )           /* Trap */
#define SV_RESET                    ( 5 )           /* Reset */
#define SV_DATA_STREAM              ( 6 )           /* Data Stream */
#define SV_ACK_FLAG                 ( 0x80 )        /* ACK flag */
#define SV_NO_ACK_FLAG              ( 0x40 )        /* No ACK flag */
#define SV_COMMAND_MASK             ( 0x3f )        /* Command bit */

/* ð */
#define SV_OK                       ( 0x0000 )      /* Success */
#define SV_AS_NO                    ( 0x0001 )      /* Access violation: Write into Read Only Area */
#define SV_AREA_ERR                 ( 0x0002 )      /* Area Error: Access to undefined area */
#define SV_AS_SIZE_ERR              ( 0x0003 )      /* Access size Error: Access to more than 1452byte */
#define SV_PARM_ERR                 ( 0x0004 )      /* Parameters Error: Parameters is incorrect ... */
#define SV_LENGTH_ERR               ( 0x0005 )      /* Length Error: Send data length is incorrect. */
#define SV_RES_FL                   ( 0x0006 )      /* Insufficient Resource: Too much ACK is waitting or insufficient resources */
#define SV_WRITE_ERR                ( 0x0007 )      /* Write Error: Can not write properly (send stream data...) */
#define SV_TIMEOUT_ERR              ( 0x0008 )      /* Timeout Error: Timeout occurred (send stream data...) */


/*--------------------------------------------------------------*/
/*  Command Stream                                              */
/*--------------------------------------------------------------*/
/* Stream header */
typedef struct _STRMHEAD {
        BYTE    bSTRMVer;           /* Header structure version */
        BYTE    bSTRMCommand;       /* Command */
        WORD    bSTRMHandle;        /* Handle number */
        DWORD   dwSTRMOffset;       /* Offset */
        WORD    wSTRMSize;          /* Data size */
        WORD    wSVHResv;           /* Reservation */
                                    /* Data */
}STRMHEAD, *PSTRMHEAD;

#define STRM_VER                    ( 1 )           /* Header structure version */
#define STRM_DATA_SIZE              ( 1424 )        /* Data size */


/* Stream Command number */
#define STRM_RESV                   ( 0 )           /* Reservation */
#define STRM_OPEN                   ( 1 )           /* Open */
#define STRM_CLOSE                  ( 2 )           /* Close */
#define STRM_SEND                   ( 3 )           /* Send */
#define STRM_BST_SEND               ( 4 )           /* BST Send */
#define STRM_TRAP                   ( 5 )           /* Trap */
#define STRM_COMMAND_MASK           ( 0x0f )        /* Command bit */

/* Stream Command Event number */
#define STRM_EVENT_CANSEL           ( 1 )           /* Send Cancel */
#define STRM_EVENT_PAUSE            ( 2 )           /* Send Pause */
#define STRM_EVENT_RESUME           ( 3 )           /* Send Resume */
#define STRM_EVENT_STA_CHANGE       ( 4 )           /* Status Change */


/*--------------------------------------------------------------*/
/*  Virtual MAP                                                 */
/*--------------------------------------------------------------*/
#define SV_VA_IO_SPACE1     ( 0x00300000L )         /* I/O space 1(64K bytes) */
#define SV_VA_IO_CTRL       ( SV_VA_IO_SPACE1 )     /* I/O operations */

#define SV_VA_SETCTRL_OFFS  ( 0x00000020 )          /* Device Control Offs */
#define SV_VA_SETCTRL_SIZE  ( 4 )                   /* Device Control Size */

#define SV_VA_SETSTATE_OFFS ( 0x00000024 )          /* Baud rate offs */
#define SV_VA_SETSTATE_SIZE ( 20 )                  /* Baud rate - Signal control size */

#define SV_VA_GETMSTAT_OFFS ( 0x000000a8 )          /* Modem Status offs */
#define SV_VA_GETMSTAT_SIZE ( 4  )                  /* Modem Status size */

#define SV_VA_SET_XON_OFFS  ( 0x00000030 )          /* XON Offs */
#define SV_VA_SET_XON_SIZE  ( 2  )                  /* XON Size */

#define SV_VA_SET_XOFF_OFFS ( 0x00000032 )          /* XOFF Offs */
#define SV_VA_SET_XOFF_SIZE ( 2  )                  /* XOFF Size */

#define SV_VA_SET_BRK_OFFS  ( 0x00000034 )          /* Break bit offs */
#define SV_VA_SET_BRK_SIZE  ( 2  )                  /* Break bit size */


#define SV_VA_SET_SIG_OFFS  ( 0x00000036 )          /* Signal control offs */
#define SV_VA_SET_SIG_SIZE  ( 2  )                  /* Signal control size */

#define SV_VA_GETCNT_OFFS   ( 0x000000c0 )          /* Counter offs */
#define SV_VA_GETCNT_SIZE   ( 4 + 4 + 4  )          /* Counter size */


#ifdef  WIN32   /* Windows */
#pragma pack(1)
#endif
typedef struct  _SER_DEVCTL {           /* Device control */
        WORD    version;                /* Version (R) */
        WORD    resv0[ 7 ];             /* Reservation (R/W) */
        BYTE    name[ 16 ];             /* Device name (R) */
        DWORD   ctrl_reg;               /* Control register (R/W) */
        DWORD   baudrate;               /* Baudrate (R/W) */
        WORD    data_bit;               /* Data bit (R/W) */
        WORD    parity_bit;             /* Parity bit (R/W) */
        WORD    stop_bit;               /* Stop bit (R/W) */
        WORD    flow_ctrl;              /* Flow Control (R/W) */
        WORD    xon_char;               /* XON Char (R/W) */
        WORD    xoff_char;              /* XOFF Char (R/W) */
        WORD    break_bit;              /* Break bit (R/W) */
        WORD    signal_ctrl;            /* Signal contrl (R/W) */
        DWORD   mode;                   /* mode (R/W) */
        DWORD   tx_wait;                /* Wait time (R/W) */
        DWORD   sampl_time;             /* Sanbururingu Time (R/W) */
        DWORD   age_time;               /* Age time (R/W) */

        /* Resend for Control command */
        DWORD   ctrl_cmd_retx_intval;   /* Intval (R/W) */
        WORD    ctrl_cmd_retx_cnt;      /* Count (R/W) */
        WORD    resv1[ 1 ];             /* Reservation */

        /* Resend for Data */
        DWORD   data_cmd_retx_intval;   /* Intval (R/W) */
        WORD    data_cmd_retx_cnt;      /* Count (R/W) */
        WORD    resv2[ 1 ];             /* Reservation */

        /* Address for administrative */
        DWORD   ip_addr;                /* IP Address (R/W) */
        WORD    port_no;                /* Port (R/W) */
        WORD    resv3[ 1 ];             /* Reservation */

        /* Address for stream command */
        DWORD   stm_ip_addr;            /* IP Address (R/W) */
        WORD    stm_port_no;            /* Port (R/W) */
        WORD    resv4[ 1 ];             /* Reservation */

        WORD    host_tx_resum;          /* Resume rate (data from host) (R/W) */
        WORD    host_tx_pause;          /* Pause rate (data from host) (R/W) */
        WORD    rs232c_rx_resum;        /* Resume rate (data from RS-232C) (R/W) */
        WORD    re232c_rx_pause;        /* Pause rate (data from RS-232C) (R/W) */
        WORD    resv5[ 24 ];            /* Reservation */

        DWORD   status_reg;             /* Status Register (R) */
        WORD    handle;                 /* Handle number (R) */
        WORD    lsr;                    /* LSR Register (R) */
        WORD    msr;                    /* Modem Status (R) */
        WORD    resv6[ 11 ];            /* Reservation */

                                        /* Error Counter (R/W) */
        DWORD   parity_errors;          /* Parity Error */
        DWORD   frame_errors;           /* Frame Error */
        DWORD   overrun_errors;         /* Overrun Error */
        WORD    resv7[ 26 ];            /* Reservation */
}SER_DEVCTL, *PSER_DEVCTL;
#ifdef  WIN32   /* Windows */
#pragma pack()
#endif

#define SER_FITDEV_VERSION      1                   /* Version */
#define SER_FITDRV_NAME         "COM1"              /* Device Name */
                                                    /* Control register (R/W) */
#define SER_CR_INIT             0x00000000          /* Initial */
#define SER_CR_START            0x00000001          /* Start */
#define SER_CR_HOST_RXBUFFULL   0x00000002          /* PC hosts the receive buffer is full */
                                                    /* Status register (R) */
#define SER_SR_NOTUSE           0x00000000          /* Notuse */
#define SER_SR_ACTIVE           0x00000001          /* Active */
#define SER_SR_STREAM           0x00000002          /* Stream is Opening */
#define SER_SR_USE              0x00000004          /* Use */
#define SER_SR_TXENABLE         0x00000008          /* Send enable */
#define SER_SR_TXBUFEMPTY       0x00000010          /* Send buffer is Empty */
#define SER_SR_RXDATA           0x00000020          /* Have Receive data */
#define SER_SR_STATUSCHANGE     0x00000040          /* Status Change */

#define COM_DEV_HANDLE          0x0001              /* Default handle number */

/*--------------------------------------------------------------*/
/*  Subroutine Program                                            */
/*--------------------------------------------------------------*/
typedef struct {
	char	ip_addr[80];
	WORD	port_no;
	WORD	handle;
	struct sockaddr_in  SocketIn;
	struct sockaddr_in  SocketOut;
	int		Socket;
	BYTE	Buffer[1500];
	WORD	SeqNo;
	WORD	ReplyId;
	DWORD	StrmOffs;
} FIT_COMM, *PFIT_COMM;

PFIT_COMM FitCommOpen(char *ip_addr, WORD port_no,
					  DWORD baudrate, WORD data_bit, WORD parity_bit,
					  WORD stop_bit, WORD flow_ctrl);
WORD FitCommSend(PFIT_COMM pfc, BYTE *buff, WORD buff_size);
WORD FitCommReceive(PFIT_COMM pfc, BYTE *buff, WORD *buff_size, WORD time_out);
void FitCommClose(PFIT_COMM pfc);
WORD FitCommGetMap(PFIT_COMM pfc);
WORD FitCommSetMap(PFIT_COMM pfc);
