#pragma once


#define iTek_USBCANFD_2      1
#define iTek_USBCANFD_1      2
#define iTek_NETCANFD_1      3
#define iTek_NETCANFD_2      4
#define iTek_NETCAN_1        5
#define iTek_NETCAN_2        6
#define iTek_WIFICANFD_1     7
#define iTek_WIFICANFD_2     8
#define iTek_WIFICAN_1       9
#define iTek_WIFICAN_2       10
#define iTek_USBCANFD_4      11
#define iTek_USBCANFD_8      12
#define iTek_NETCANFD_4      13
#define iTek_NETCANFD_8      14

#define CHANNEL_NUM 8
typedef void * DEVICE_HANDLE;
typedef void * CHANNEL_HANDLE;
#pragma comment(lib,"Shlwapi.lib")
#define CANFD_MAX_DLEN 64
typedef unsigned char      uint8_t;
typedef unsigned short     uint16_t;
typedef unsigned int       uint32_t;
typedef unsigned long long uint64_t;
typedef uint32_t canid_t;

typedef struct tag_canfd_frame {
	canid_t    can_id;  /* 32 bit MAKE_CAN_ID + EFF/RTR/ERR flags */
	uint8_t    len;     /* frame payload length in byte */
	uint8_t    flags;   /* additional flags for CAN FD,i.e error code */
	uint8_t    sendType;    /* 发送方式*/
	uint8_t    cantype;    /*CAN类型：CAN,CANFD,CANFD加速*/
	uint8_t    data[CANFD_MAX_DLEN];//数据
    /*
	tag_canfd_frame & operator = (const tag_canfd_frame &obj)   //赋值操作
	{
		can_id = obj.can_id;
		len = obj.len;
		flags = obj.flags;
		sendType = obj.sendType;
		cantype = obj.cantype;

		//memcpy_s(data, 64, obj.data, len);
		for (int i = 0; i < obj.len; ++i)
		{
			data[i] = obj.data[i];
		}

		return *this;
	}
    */
}canfd_frame;

typedef struct tagFilterData
{
	uint8_t     frameType;   //帧类型
	uint8_t     filterType;  //过滤器类型
	uint32_t    ID1;     //开始ID
	uint32_t    ID2;       //结束ID
}FilterData;
typedef struct tagFilterDataStandard
{
	int num;
	FilterData filterDataStandard[128];
}FilterDataStandard;
typedef struct tagFilterDataExtend
{
	int num;
	FilterData filterDataExtend[64];
}FilterDataExtend;
typedef struct tagStandard_Extend
{
	FilterDataStandard Standard;
	FilterDataExtend Extend;
}Standard_Extend;
typedef struct tagiTek_CAN_DEVICE_INFO {
	uint8_t  hw_Version[3];     //硬件版本
	uint8_t  fw_Version[3];     //固件版本
	uint8_t  product_Version[3];//产品版本
	uint8_t  can_Num;           //通道个数
	uint8_t  str_Serial_Num[20];//序列号
	uint8_t  str_hw_Type[40];   //产品描述字符
	uint16_t reserved[6];
}iTek_CANFD_DEVICE_INFO;

typedef struct tagiTek_CAN_CHANNEL_INIT_CONFIG {
	uint8_t    can_type; //type:TYPE_CAN TYPE_CANFD
	uint8_t    CANFDStandard;  //标准/扩展帧
	uint8_t    CANFDSpeedup; //是否加速
	uint32_t   abit_timing;//仲裁波特率
	uint32_t   dbit_timing;//数据波特率
	uint8_t    workMode;  //工作模式
	uint32_t   res;
	//std::vector<FilterData>  vecFilterDatas;  //ID过滤
	tagFilterDataExtend Extend;
	tagFilterDataStandard Standard;

}iTek_CANFD_CHANNEL_INIT_CONFIG;

typedef struct tagiTek_CAN_CHANNEL_ERR_INFO {
	uint8_t  error_code;      //错误码
	uint8_t  error_status;    //错误状态
	uint8_t  nRxCount;       //接收错误计数
	uint8_t  nTxCount;       //发送错误计数
} iTek_CANFD_CHANNEL_ERR_INFO;

typedef struct tagiTek_CAN_CHANNEL_STATUS {
	uint8_t errInterrupt;
	uint8_t regMode;
	uint8_t regStatus;
	uint8_t regALCapture;
	uint8_t regECCapture;
	uint8_t regEWLimit;
	uint8_t regRECounter;
	uint8_t regTECounter;
	uint32_t Reserved;
}iTek_CANFD_CHANNEL_STATUS;

typedef struct tagiTek_CANFD_Receive_Data
{
	canfd_frame           frame;
	uint64_t              timestamp;
	
}iTek_CANFD_Receive_Data;

typedef struct tagiTek_Transmit_CANFD_Data
{
	canfd_frame           frame;
	uint16_t			  send_type;
}iTek_CANFD_Transmit_Data;
typedef struct tag_iTek_Auto_Transmit {
    uint8_t enable;
    uint16_t index;
    uint32_t interval;
    canfd_frame frame;
}iTek_Auto_Transmit, *PiTek_Auto_Transmit;
typedef struct tagProperty
{
    unsigned char Ip[64][25];
    int nNum;
}iTek_Property;

#define EXTERNC	

#ifdef __cplusplus
extern "C" {
#endif
	EXTERNC DEVICE_HANDLE __stdcall iTek_OpenDevice(uint16_t, uint16_t, uint32_t);

	EXTERNC bool __stdcall iTek_CloseDevice(CHANNEL_HANDLE usbhandle);
	EXTERNC void* __stdcall iTek_InitCAN(DEVICE_HANDLE usbhandle, uint8_t, iTek_CANFD_CHANNEL_INIT_CONFIG*);

	EXTERNC bool __stdcall iTek_GetDeviceInfo(DEVICE_HANDLE usbhandle, iTek_CANFD_DEVICE_INFO*);

	EXTERNC uint32_t __stdcall iTek_GetReceiveNum(CHANNEL_HANDLE usbhandle);
	EXTERNC void __stdcall iTek_ClearBuffer(CHANNEL_HANDLE usbhandle);

	EXTERNC bool __stdcall iTek_StartCAN(CHANNEL_HANDLE usbhandle);
	EXTERNC bool __stdcall iTek_ResetCAN (CHANNEL_HANDLE usbhandle);

	EXTERNC uint32_t __stdcall iTek_Transmit(CHANNEL_HANDLE usbhandle, iTek_CANFD_Transmit_Data*, uint32_t);
	EXTERNC uint32_t __stdcall iTek_Receive(CHANNEL_HANDLE usbhandle, iTek_CANFD_Receive_Data*, uint32_t, int);

	EXTERNC void __stdcall iTek_Authenticate(DEVICE_HANDLE usbhandle, uint16_t key_type, uint8_t *pStr, int len,unsigned char *dStr);
	EXTERNC bool __stdcall iTek_isConnected(DEVICE_HANDLE usbhanele);
    EXTERNC uint32_t __stdcall iTek_Set_Value(DEVICE_HANDLE deviceHandle, char *cmd, unsigned char *value);
    EXTERNC uint32_t __stdcall iTek_Get_Value(DEVICE_HANDLE deviceHandle, char *cmd, iTek_Property *value);
#ifdef __cplusplus
}
#endif
