#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>

/* unmark following line to enable debug message */
//#define DBG

/* debug flags */
#ifdef DBG
#define dbg(fmt, args...) printf(fmt, ##args)
#else
#define dbg(fmt, args...)
#endif

#ifndef SUCCESS
#define SUCCESS 0
#endif

#ifndef FAILURE
#define FAILURE -1
#endif

/* defined PDS700 Library ERROR ID */
#define PDS700_NOERROR 0
#define PDS700_OPENCOMERROR 1
#define PDS700_CMDPORTERROR 2
#define PDS700_COMNUMBERERROR 3
#define PDS700_OPENSOCKETERROR 4
#define PDS700_CONNECTSOCKETERROR 5
#define PDS700_GETHOSTNAMEERROR 6
#define PDS700_SETCOMBAUDERROR 7
#define PDS700_SETCOMDATAFORMATERROR 8
#define PDS700_SETCMDRETURNMODEERROR 9
#define PDS700_CLOSECOMSOCKETERROR 10
#define PDS700_CLOSECMDSOCKETERROR 11
#define PDS700_SENDSTRINGERROR 12
#define PDS700_READSTRINGERROR 13
#define PDS700_SETSERVERIPERROR 14
#define PDS700_SETSERVERGATEWAYERROR 15
#define PDS700_SETSERVERSUBMASKERROR 16
#define PDS700_CLEARBUFFERERROR 17
#define PDS700_NORESPONSE 18

/* define driver and dll version */
#define PDS700_LIBRARY_VERSION "0.2.7"
#define LINE_SIZE 128
#define PDS_COM_MAX 10
#define ASCII_CR 13
#define ASCII_LF 10

typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef void (*HANDLE)(int);
typedef unsigned char BYTE;
typedef int BOOL;

typedef struct pds_com_port
{
        int com_init;
        WORD com_skt;
        int baud;
        int data;
        int parity;
        int stop;
} pds_com_t;

typedef struct pds_module
{
        int mod_init;
        char server_ip[24];
        char server_mac[24];
        char server_gateway[24];
        char server_submask[24];
        WORD cmd_skt;
        pds_com_t com_list[PDS_COM_MAX];
} pds_mod_t;


WORD Open_CAN(pds_mod_t *PDSMod, char *ServerIP);
WORD Open_ComA(pds_mod_t *PDSMod, char *ServerIP, WORD CommandPort, WORD ServerCOM, DWORD Baudrate, BYTE Data, BYTE Parity, BYTE Stop);
WORD Close_ComA(pds_mod_t *PDSMod, WORD ServerCOM);
WORD Close_CAN(pds_mod_t *PDSMod);
WORD Close_Module(pds_mod_t *PDSMod);

char * Get_PDS_Library_Version(void);
WORD Get_PDS_MiniOS_Version(pds_mod_t *PDSMod, char *MiniOSVer);
WORD Get_PDS_Vxcomm_Version(pds_mod_t *PDSMod, char *VxcommVer);
WORD Get_PDS_Network_Info(pds_mod_t *PDSMod);
//WORD Get_PDS_MAC_Addr(pds_mod_t *PDSMod, char *MACAddr);
WORD Get_PDS_Module_Name(pds_mod_t *PDSMod, char *ModuleName);
WORD Get_PDS_COM_Info(pds_mod_t *PDSMod, WORD ServerCOM);
WORD Get_PDS_Server_Network_Info(pds_mod_t *PDSMod);

WORD Reset_PDS_Module(pds_mod_t *PDSMod);
WORD Set_PDS_Server_IP(pds_mod_t *PDSMod, char *NewServerIP);
WORD Set_PDS_Server_Gateway(pds_mod_t *PDSMod, char *NewServerGateway);
WORD Set_PDS_Server_SubMask(pds_mod_t *PDSMod, char *NewServerSubMask);

WORD Clear_PDS_Buffer(pds_mod_t *PDSMod, int Port);

WORD Send_CAN_Msg(pds_mod_t *PDSMod, char *can_msg);
WORD Read_CAN_Msg(pds_mod_t *PDSMod, char *In_String);
WORD Send_Read_StringA(pds_mod_t *PDSMod, WORD ServerCOM, char Out_String[], char In_String[]);
WORD Send_StringA(pds_mod_t *PDSMod, WORD ServerCOM, char Out_String[]);
WORD Read_StringA(pds_mod_t *PDSMod, WORD ServerCOM, char In_String[]);

WORD Send_BinaryA(pds_mod_t *PDSMod, WORD ServerCOM, char Out_String[], int len);
WORD Read_BinaryA(pds_mod_t *PDSMod, WORD ServerCOM, char In_String[], int len);
WORD Send_Read_CmdA(int CMDSfd, char Out_String[], char In_String[]);
WORD Send_CmdA(int CMDSfd, char Out_String[]);

WORD Send_Modbus(pds_mod_t *PDSMod, WORD ServerCOM, char Out_String[], char Out_len);
WORD Read_Modbus(pds_mod_t *PDSMod, WORD ServerCOM, char In_String[], char In_len);
