/**********************************************************************
 *
 *  canfd.h
 *
 *  Header of I-7565M-FD Linux Library
 *
 *  v 0.0.0 2024.11.29 by Winson Chen
 *
 *    create
 *
 **********************************************************************/

#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <math.h>
#include <stdint.h>
#include <sys/select.h>
#include <sys/eventfd.h>

#ifndef SUCCESS
#define SUCCESS 0
#endif

#ifndef FAILURE
#define FAILURE -1
#endif

typedef unsigned int BOOL;
typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef uint32_t DWORD;

#define	USBDEV_MAXNUM   8
#define HIDRAW_MAXNUM   128

#define HID_DATA_BYTE	512
#define HID_MAX_BYTE	HID_DATA_BYTE+1	//include Report ID

//#Error Code
#define DEV_RETURN_ERR_CODE_BASE  0		//Device
#define DEVLIB_ERR_CODE_BASE      0x10000	//USB Device Library
#define CANFDLIB_ERR_CODE_BASE    0x10100	//CANFD Library

/*
//Device Reture Error code
#define	ERR_DEV_OPError			(DEV_RETURN_ERR_CODE_BASE + 1)
#define	ERR_DEV_FCError			(DEV_RETURN_ERR_CODE_BASE + 2)
#define	ERR_DEV_DLError			(DEV_RETURN_ERR_CODE_BASE + 3)
#define	ERR_DEV_WriteDataError		(DEV_RETURN_ERR_CODE_BASE + 4)
*/

//USB Device Library Error Code
#define _USBDEV_ERROR_NO_Err			0
#define	_USBDEV_ERROR_Invalid_Device		  (DEVLIB_ERR_CODE_BASE + 1)
#define _USBDEV_ERROR_Device_Already_Used	(DEVLIB_ERR_CODE_BASE + 2)
#define	_USBDEV_ERROR_DevNotExists		    (DEVLIB_ERR_CODE_BASE + 3)
#define	_USBDEV_ERROR_GetDevInfo		      (DEVLIB_ERR_CODE_BASE + 4)
#define	_USBDEV_ERROR_Invalid_PktSize		  (DEVLIB_ERR_CODE_BASE + 5)
#define	_USBDEV_ERROR_WriteFileFailed		  (DEVLIB_ERR_CODE_BASE + 6)
#define	_USBDEV_ERROR_WriteBufferOverflow	(DEVLIB_ERR_CODE_BASE + 7)

#define _USBDEV_ERROR_ExceedMaxSuppDevNum	(DEVLIB_ERR_CODE_BASE + 10)
#define _USBDEV_ERROR_DevNotOpen		      (DEVLIB_ERR_CODE_BASE + 11)

#define ERR_NO_ERR                	0
/* CANFD Library Error Codes */
#define ERR_CANFD_COMM_TIMEOUT		  (CANFDLIB_ERR_CODE_BASE + 0)
#define ERR_CANFD_INVALID_Port		  (CANFDLIB_ERR_CODE_BASE + 1)
#define ERR_CANFD_CANRxNoData		    (CANFDLIB_ERR_CODE_BASE + 2)
#define ERR_CANFD_CANTxBufOverflow	(CANFDLIB_ERR_CODE_BASE + 3)
#define ERR_CANFD_CANADBaudrateErr	(CANFDLIB_ERR_CODE_BASE + 4)
#define ERR_CANFD_ListSizeErr		    (CANFDLIB_ERR_CODE_BASE + 5)

#define ERR_OpenFile		      1
#define ERR_CreateRxTxThread	2
#define ERR_RestartRxThread	  3
#define ERR_RestartTxThread	  4
#define ERR_EventFd		        5

#ifdef __cplusplus
extern "C" {
#endif
// SYSTEM
DWORD CANFD_GetDllVersion(void);
int CANFD_ScanDevice(void);
BYTE CANFD_ListDevice(WORD* o_wPID, DWORD* o_dwBID);
int CANFD_OpenDevice(WORD *o_wDevice_id, WORD i_wpid, DWORD i_wbid);
int CANFD_CloseDevice(WORD i_wDevice_id);

// DEVICE
//GET API
int CANFD_GetFwVer(WORD i_wDevice_id, WORD* o_wFwVer);
int CANFD_GetINITPinStatus(WORD i_wDevice_id, BYTE* o_byStatus);

int CANFD_GetCANOPMode(WORD i_wDevice_id, BYTE i_byCANPort, WORD* o_wEnable, WORD* o_wBusMode, WORD* o_wISOMode);
int CANFD_GetCANStatus(WORD i_wDevice_id, BYTE i_byCANPort, DWORD* o_dwCANStatus, DWORD* o_dwErrCnt, DWORD* o_dwBufStatus);
int CANFD_GetCANADBaudRate(WORD i_wDevice_id, BYTE i_byCANPort, DWORD* o_dwArbitrBR, DWORD* o_dwDataBR, WORD* o_wArbitrBRSP, WORD* o_wDataBRSP);
int CANFD_GetCANGlobalFilter(WORD i_wDevice_id, BYTE i_byCANPort, BYTE *o_byRejectRFS, BYTE *o_byRejectRFE, WORD* o_wSTDFIDListSize, WORD* o_wEXTFIDListSize);
int CANFD_GetCANSTDIDFilter(WORD i_wDevice_id, BYTE i_byCANPort, WORD* o_wSTDFID1, WORD* o_wSTDFID2);
int CANFD_GetCANEXTIDFilter(WORD i_wDevice_id, BYTE i_byCANPort, DWORD* o_dwEXTFID1, DWORD* o_dwEXTFID2);

int CANFD_GetCANRxFramePerSec(WORD i_wDevice_id, BYTE i_byCANPort, WORD *o_wRxFPS);
int CANFD_GetCANHWSendMode(WORD i_wDevice_id, BYTE i_byCANPort, BYTE *o_byMode); 

int CANFD_GetCANRxMsgCount(WORD i_wDevice_id, BYTE i_byCANPort, DWORD* o_dwCount);
int CANFD_GetCANRxMsg(WORD i_wDevice_id, BYTE i_byCANPort, BYTE* o_byType, BYTE* o_byMode, DWORD* o_dwID, BYTE* o_byRTR, BYTE* o_byFDF, BYTE* o_byDlen, BYTE *o_byData, DWORD *o_dw_TimeStamp_s, DWORD *o_dw_TimeStamp_us);

//SET API
int CANFD_SetSN(WORD i_wDevice_id, DWORD i_dwSN);
//QC LED used
int CANFD_SetLEDMode(WORD i_wDevice_id, BYTE i_byLEDMode);
int CANFD_SetLEDState(WORD i_wDevice_id, BYTE i_byLEDState);

int CANFD_SetCANReInitialize(WORD i_wDevice_id, BYTE i_byCANPort);
int CANFD_SetCANOPMode(WORD i_wDevice_id, BYTE i_byCANPort, WORD i_wEnable, WORD i_wBusMode, WORD i_wISOMode);
int CANFD_SetCANADBaudRate(WORD i_wDevice_id, BYTE i_byCANPort, DWORD i_dwArbitrBR, DWORD i_dwDataBR, WORD i_wArbitrBRSP, WORD i_wDataBRSP);
int CANFD_SetCANGlobalFilter(WORD i_wDevice_id, BYTE i_byCANPort, BYTE i_byRejectRFS, BYTE i_byRejectRFE);
int CANFD_SetCANSTDIDFilter(WORD i_wDevice_id, BYTE i_byCANPort, WORD i_wSTDFIDListSize, WORD* i_wSTDFID1, WORD* i_wSTDFID2);
int CANFD_SetCANEXTIDFilter(WORD i_wDevice_id, BYTE i_byCANPort, WORD i_wEXTFIDListSize, DWORD* i_dwEXTFID1, DWORD* i_dwEXTFID2);

int CANFD_SetCANHWSendMode(WORD i_wDevice_id, BYTE i_byCANPort, BYTE i_byMode);
int CANFD_SetCANHWSendMsg(WORD i_wDevice_id, BYTE i_byCANPort, BYTE i_byMode, DWORD i_dwID, BYTE i_byRTR, BYTE i_byFDF, BYTE i_byDlen, BYTE *i_byData, DWORD i_dwPeriod, DWORD i_dwCounter);

int CANFD_SetCANTxMsg(WORD i_wDevice_id, BYTE i_byCANPort, BYTE i_byMode, DWORD i_dwID, BYTE i_byRTR, BYTE i_byFDF, BYTE i_byDlen, BYTE *i_byData);

int CANFD_ResetModule(WORD i_wDevice_id);
int CANFD_ResetModuleWDT(WORD i_wDevice_id);
int CANFD_ClearCANRxBuf(WORD i_wDevice_id, BYTE i_byCANPort);
int CANFD_ClearCANTxBuf(WORD i_wDevice_id, BYTE i_byCANPort);

// OTHERS
#ifdef __cplusplus
}
#endif
