
#ifndef __PISO_CANFD_HEADER__
#define __PISO_CANFD_HEADER__


#ifdef PISOCANFD_EXPORTS
	#define EXPORTS __declspec(dllexport)
#else
	#define EXPORTS __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C" {
#endif


// return code
#ifndef DWORD
#define DWORD unsigned long
#endif

#ifndef WORD
#define WORD unsigned short
#endif

#ifndef BYTE
#define BYTE unsigned char
#endif

//#Error Code
#define DLL_ERR_CODE_BASE		0
#define SYS_ERR_CODE_BASE		0x100

#define ERR_NO_ERR                0
//PISO-CANFD DLL Error Codes
#define ERR_INIT_DRIVER_ERROR				(DLL_ERR_CODE_BASE+0x01)
#define ERR_COMM_DRIVER_ERROR				(DLL_ERR_CODE_BASE+0x02)
#define ERR_DRIVER_UNINIT_ERROR		   	    (DLL_ERR_CODE_BASE+0x03)
#define ERR_ACTIVE_BOARD_ERROR				(DLL_ERR_CODE_BASE+0x04)
#define ERR_BOARD_ALREADY_ACTIVE_ERROR      (DLL_ERR_CODE_BASE+0x05)
#define ERR_BOARD_UNACTIVE_ERROR		    (DLL_ERR_CODE_BASE+0x06)
#define ERR_BOARD_NUMBER_ERROR			    (DLL_ERR_CODE_BASE+0x07)
#define ERR_PORT_NUMBER_ERROR			    (DLL_ERR_CODE_BASE+0x08)
#define ERR_PORT_UNINIT_ERROR               (DLL_ERR_CODE_BASE+0x09)

#define ERR_FLT_FRAME_FORMAT_ERROR		    (DLL_ERR_CODE_BASE+0x20)
#define ERR_FLT_ACC_ACM_INCONSISTENCY_ERROR	(DLL_ERR_CODE_BASE+0x21)
#define ERR_FLT_STD_ACC_TOOLARGE_ERROR		(DLL_ERR_CODE_BASE+0x22)
#define ERR_FLT_EXT_ACC_TOOLARGE_ERROR		(DLL_ERR_CODE_BASE+0x23)
#define ERR_FLT_STD_ACM_TOOLARGE_ERROR		(DLL_ERR_CODE_BASE+0x24)
#define ERR_FLT_EXT_ACM_TOOLARGE_ERROR		(DLL_ERR_CODE_BASE+0x25)

#define ERR_INVALID_OPMODE_ERROR		    (DLL_ERR_CODE_BASE+0x30)
#define ERR_SOFTBUFF_IS_EMPTY				(DLL_ERR_CODE_BASE+0x31)
#define ERR_NORM_BITRATE_NOT_SUPP_ERROR		(DLL_ERR_CODE_BASE+0x32)
#define ERR_DATA_BITRATE_NOT_SUPP_ERROR		(DLL_ERR_CODE_BASE+0x33)
#define ERR_DATABR_LESS_THAN_NORMBR_ERROR	(DLL_ERR_CODE_BASE+0x34)
#define ERR_INVALID_NORMAL_SAMPLEPOINT		(DLL_ERR_CODE_BASE+0x35)
#define ERR_INVALID_DATA_SAMPLEPOINT		(DLL_ERR_CODE_BASE+0x36)


typedef struct
{    
  ULONG id;
  UCHAR ext;
  UCHAR type;     //remote, data, fdf
  UCHAR dlc;      //0~F  
  UCHAR len;      //0~64
  union { 
    UCHAR data [64];
    ULONG data32 [16] ;
  } ;
  ULONGLONG timestamp;  //100-nanosecond  
}CANFDRxMessage;

typedef struct
{    
  ULONG id;
  UCHAR ext;
  UCHAR type;     //remote, data, fdf
  UCHAR dlc;      //0~F  
  UCHAR len;      //0~64
  union { 
    UCHAR data [64];
    ULONG data32 [16] ;
  } ;  
}CANFDTxMessage;



///////////////////////////////////////////
//           Common Functions            //
///////////////////////////////////////////
//SYSTEM
EXPORTS int CALLBACK CANFD_GetDllVersion(WORD *DLLVer);
EXPORTS int CALLBACK CANFD_GetBoardInf(BYTE BoardNo, WORD *wVID,WORD *wDID,
									   WORD *wSVID, WORD *wSDID,WORD *wSAuxID,
									   WORD *wIrqNo, BYTE *BoardID, BYTE *BoardSwitchNo);
EXPORTS int CALLBACK CANFD_TotalBoard(BYTE *BoardNo);
EXPORTS int CALLBACK CANFD_GetCardBoardSwitchNo(BYTE BoardNo, BYTE *BoardSwitchNo);
EXPORTS int CALLBACK CANFD_GetCardBoardID(BYTE BoardNo, BYTE *BoardID);
EXPORTS int CALLBACK CANFD_GetCardPortNum(BYTE BoardNo, BYTE *PortNum);
EXPORTS int CALLBACK CANFD_GetCardFPGAFWVer(BYTE BoardNo, BYTE *FPGAFWVer);

//CAN active, close
EXPORTS int CALLBACK CANFD_ActiveBoard(BYTE BoardNo);
EXPORTS int CALLBACK CANFD_CloseBoard(BYTE BoardNo);
EXPORTS int CALLBACK CANFD_BoardIsActive(BYTE BoardNo, BYTE *IsActive);

//CAN reset, init, set bitrate, change opmode
EXPORTS int CALLBACK CANFD_Reset(BYTE BoardNo, BYTE Port);	//after reset, port rx irq will be disable
EXPORTS int CALLBACK CANFD_Init(BYTE BoardNo, BYTE Port);
EXPORTS int CALLBACK CANFD_SetBitRate(BYTE BoardNo, BYTE Port, DWORD NormalBitRate, DWORD DataBitRate);
EXPORTS int CALLBACK CANFD_SetBitRateWithSP(BYTE BoardNo, BYTE Port, DWORD NormalBitRate, DWORD DataBitRate, WORD NormalSP, WORD DataSP);
EXPORTS int CALLBACK CANFD_GetBitRate(BYTE BoardNo, BYTE Port, DWORD *NormalBitRate, DWORD *DataBitRate);
EXPORTS int CALLBACK CANFD_GetBitRateWithSP(BYTE BoardNo, BYTE Port, DWORD *NormalBitRate, DWORD *DataBitRate, WORD *NormalSP, WORD *DataSP);
EXPORTS int CALLBACK CANFD_SetFilterAllPass(BYTE BoardNo, BYTE Port);	// Accept any frame
EXPORTS int CALLBACK CANFD_SetFilterFormat(BYTE BoardNo, BYTE Port, BYTE inFormat);	// Accept any std or ext identifier
EXPORTS int CALLBACK CANFD_SetFilter(BYTE BoardNo, BYTE Port, BYTE inFormat, DWORD inAcceptance, DWORD inMask);	//Accept std or ext identifier by Acc/Acm setting
EXPORTS int CALLBACK CANFD_SetOPMode(BYTE BoardNo, BYTE Port, BYTE OPMode);
EXPORTS int CALLBACK CANFD_GetOPMode(BYTE BoardNo, BYTE Port, BYTE *OPMode);
EXPORTS int CALLBACK CANFD_SetISOCRCEn(BYTE BoardNo, BYTE Port, BYTE ISOCRCEn);
EXPORTS int CALLBACK CANFD_GetISOCRCEn(BYTE BoardNo, BYTE Port, BYTE *ISOCRCEn);

//CAN Bus status, FIFO state
EXPORTS int CALLBACK CANFD_SetFIFOStatus(BYTE BoardNo, BYTE Port, BYTE FIFOState);	//bit0:1 -> Clear Rx FIFO and overflow flag, bit1:1 -> Clear Tx FIFO and overflow flag
EXPORTS int CALLBACK CANFD_GetFIFOStatus(BYTE BoardNo, BYTE Port, BYTE *FIFOState);	//Get FIFO overflow flag
EXPORTS int CALLBACK CANFD_GetCANStatus(BYTE BoardNo, BYTE Port, WORD *CANStatus, BYTE *TxErrorCount, BYTE *RxErrorCount);
EXPORTS int CALLBACK CANFD_GetBUSDiagnostic(BYTE BoardNo, BYTE Port, DWORD *BUSDiag0, DWORD *BUSDiag1);

//CAN install irq
EXPORTS int CALLBACK CANFD_InstallIrq(BYTE BoardNo);
EXPORTS int CALLBACK CANFD_RemoveIrq(BYTE BoardNo);
EXPORTS int CALLBACK CANFD_GetIrqStatus(BYTE BoardNo, BYTE *IRQST);

//CAN send, receive
EXPORTS int CALLBACK CANFD_SendCANMsg(BYTE BoardNo, BYTE Port, BYTE Mode, DWORD Id, BYTE Type, BYTE Dlc, BYTE *Data);
EXPORTS int CALLBACK CANFD_RecvCANMsg(BYTE BoardNo, BYTE Port, BYTE *Mode, DWORD *Id, BYTE *Type, BYTE *Dlc, BYTE *Data, DWORD *H_MsgTimeStamps ,DWORD *L_MsgTimeStamps);
EXPORTS int CALLBACK CANFD_RecvCANMsgCnt(BYTE BoardNo, BYTE Port, WORD *MsgCnt);


#ifdef __cplusplus
}
#endif

#endif