using System;
using System.Runtime.InteropServices;

namespace iTekCAN
{

    [StructLayout(LayoutKind.Sequential)]
    public struct canfd_frame
    {
        public UInt32 canid;
        public byte len;
        public byte flage;
        public byte res;
        public byte can_type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public byte[] data;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct iTek_CANFD_Receive_DATA
    {
        public canfd_frame frame;
        public UInt64 timestamp;
    };

    [StructLayout(LayoutKind.Sequential)]

    public struct iTek_CANAFD_Transmit_Data
    {
        public canfd_frame frame;
        public UInt16 send_type;
    };

    [StructLayout(LayoutKind.Sequential)]

    public struct FilterData
    {
        public byte frametype;
        public byte filtertype;
        public UInt32 ID1;
        public UInt32 ID2;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct FilterDataStandard
    {
        public UInt16 num;
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 128, ArraySubType = UnmanagedType.Struct)]
        public FilterData[] fitlterdatastandard;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    public struct FilterDataExtend
    {
        public UInt16 num;
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 64, ArraySubType = UnmanagedType.Struct)]
        public FilterData[] filterdataextend;
    };

    [StructLayout(LayoutKind.Sequential)]

    public struct Standard_Extend
    {
        public FilterDataStandard standart;
        public FilterDataExtend extend;
    };

    [StructLayout(LayoutKind.Sequential)]

    public struct iTek_CANFD_DEVICE_INFO
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] hw_Version;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] fw_Version;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] product_Vsesion;
        public byte can_Num;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public byte[] str_Serial_Num;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
        public byte[] str_hw_Type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public byte[] reserved;

    };
        
    [StructLayout(LayoutKind.Sequential)]
    public struct iTek_CANFD_CHANNEL_INIT_CONFIG
    {
        public byte can_type;
        public byte CANFDStandad;
        public byte CANFDSpeedup;
        public UInt32 abit_timing;
        public UInt32 dbit_timing;
        public byte workMode;
        public UInt32 res;

        public FilterDataExtend extend;
        public FilterDataStandard standard;
    };

    public struct DeviceInfo
    {
        public uint device_type;  //设备类型
        public uint channel_count;//设备的通道个数
        public DeviceInfo(uint type, uint count)
        {
            device_type = type;
            channel_count = count;
        }
    };
    public struct tag_IP_Info
    {
        public byte model;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
        public byte[] local_ip;
        public UInt16 local_port;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
        public byte[] dest_ip;
        public UInt16 dest_port;
    };
    public class iTekCanDLL
    {
        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr iTek_OpenDevice(uint DeviceType, uint Device_index, UInt32 res);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern bool iTek_CloseDevice(IntPtr usbhandle);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern IntPtr iTek_InitCAN(IntPtr channel_handle, byte channel, IntPtr config);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern bool iTek_GetDeviceInfo(IntPtr usbhandle, IntPtr deviceinfo);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern UInt32 iTek_GetReceiveNum(IntPtr channel_handle);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern void iTek_ClearBuffer(IntPtr channel_handle);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern bool iTek_StartCAN(IntPtr channel_handle);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern bool iTek_ResetCAN(IntPtr channel_handle);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern void iTek_Authenticate(IntPtr usbhandle, int key_type, string pStr, int len, byte[] dStr);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern bool iTek_isConnected(IntPtr usbhandle);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern int iTek_Transmit(IntPtr channel_handle, IntPtr send_data, int len);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern int iTek_Receive(IntPtr channel_handle, IntPtr recv_data, uint len, int wait_time);

        [DllImport("iTekCANFD.dll", CallingConvention = CallingConvention.StdCall)]

        public static extern int iTek_Set_Value(IntPtr channel_handle, char[] cmd, IntPtr setValue);

    }

}