#================================================================
# ccan.py
# module file for CONTEC API-CAN(WDM)
#                                                CONTEC.Co., Ltd.
#================================================================
import ctypes
import ctypes.wintypes

ccan_dll = ctypes.windll.LoadLibrary('ccan.dll')


#----------------------------------------
# Types for callback function.
#----------------------------------------
PCAN_EVENT_CALLBACK = ctypes.WINFUNCTYPE(None,
                                       ctypes.c_short, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM,
                                       ctypes.c_void_p)


#================================================================
# Prototype definition
#================================================================
#----------------------------------------
# Common function
#----------------------------------------
# C Prototype: long WINAPI CanInit(char *DeviceName, short *Id);
CanInit = ccan_dll.CanInit
CanInit.restype = ctypes.c_long
CanInit.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_short)]

# C Prototype: long WINAPI CanExit(short Id);
CanExit = ccan_dll.CanExit
CanExit.restype = ctypes.c_long
CanExit.argtypes = [ctypes.c_short]

# C Prototype: long WINAPI CanResetDevice(short Id);
CanResetDevice = ccan_dll.CanResetDevice
CanResetDevice.restype = ctypes.c_long
CanResetDevice.argtypes = [ctypes.c_short]

# C Prototype: long WINAPI CanGetErrorString(int ErrorCode , char *ErrorString);
CanGetErrorString = ccan_dll.CanGetErrorString
CanGetErrorString.restype = ctypes.c_long
CanGetErrorString.argtypes = [ctypes.c_int, ctypes.c_char_p]


#----------------------------------------
# Information acquisition function
#----------------------------------------
# C Prototype: long WINAPI CanQueryDeviceName(short Index, char *DeviceName, char *Device);
CanQueryDeviceName = ccan_dll.CanQueryDeviceName
CanQueryDeviceName.restype = ctypes.c_long
CanQueryDeviceName.argtypes = [ctypes.c_short, ctypes.c_char_p, ctypes.c_char_p]

# C Prototype: long WINAPI CanGetMaxChannels(short Id, unsigned short *CanChannelNum, unsigned short *CanFdChannelNum);
CanGetMaxChannels = ccan_dll.CanGetMaxChannels
CanGetMaxChannels.restype = ctypes.c_long
CanGetMaxChannels.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort)]


#----------------------------------------
# Setup and get setup functions
#----------------------------------------
# C Prototype: long WINAPI CanSetChannelAcceptance(short Id, unsigned short ChannelNo, unsigned int MessageTypeMask, unsigned int CanIdMask, unsigned int CanIdCode);
CanSetChannelAcceptance = ccan_dll.CanSetChannelAcceptance
CanSetChannelAcceptance.restype = ctypes.c_long
CanSetChannelAcceptance.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint]

# C Prototype: long WINAPI CanGetChannelAcceptance(short Id, unsigned short ChannelNo, unsigned int *MessageTypeMask, unsigned int *CanIdMask, unsigned int *CanIdCode);
CanGetChannelAcceptance = ccan_dll.CanGetChannelAcceptance
CanGetChannelAcceptance.restype = ctypes.c_long
CanGetChannelAcceptance.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint)]

# C Prototype: long WINAPI CanSetCanTiming(short Id, unsigned short ChannelNo, unsigned int Bitrate, unsigned short TSeg1, unsigned short SJW, unsigned short TSeg2);
CanSetCanTiming = ccan_dll.CanSetCanTiming
CanSetCanTiming.restype = ctypes.c_long
CanSetCanTiming.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_uint, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI CanGetCanTiming(short Id, unsigned short ChannelNo, unsigned int *Bitrate, unsigned short *TSeg1, unsigned short *SJW, unsigned short *TSeg2);
CanGetCanTiming = ccan_dll.CanGetCanTiming
CanGetCanTiming.restype = ctypes.c_long
CanGetCanTiming.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI CanSetCanFdTiming(short Id, unsigned short ChannelNo, unsigned int ArbBitrate, unsigned short ArbTSeg1, unsigned short ArbSJW, unsigned short ArbTSeg2, unsigned int DataBitrate, unsigned short DataTSeg1, unsigned short DataSJW, unsigned short DataTSeg2);
CanSetCanFdTiming = ccan_dll.CanSetCanFdTiming
CanSetCanFdTiming.restype = ctypes.c_long
CanSetCanFdTiming.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_uint, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_uint, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI CanGetCanFdTiming(short Id, unsigned short ChannelNo, unsigned int *ArbBitrate, unsigned short *ArbTSeg1, unsigned short *ArbSJW, unsigned short *ArbTSeg2, unsigned int *DataBitrate, unsigned short *DataTSeg1, unsigned short *DataSJW, unsigned short *DataTSeg2);
CanGetCanFdTiming = ccan_dll.CanGetCanFdTiming
CanGetCanFdTiming.restype = ctypes.c_long
CanGetCanFdTiming.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI CanSetMonitorMode(short Id, unsigned short ChannelNo, unsigned short MonitorMode);
CanSetMonitorMode = ccan_dll.CanSetMonitorMode
CanSetMonitorMode.restype = ctypes.c_long
CanSetMonitorMode.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI CanSetTimeout(short Id, unsigned int Timeout);
CanSetTimeout = ccan_dll.CanSetTimeout
CanSetTimeout.restype = ctypes.c_long
CanSetTimeout.argtypes = [ctypes.c_short, ctypes.c_uint]

# C Prototype: long WINAPI CanGetTimeout(short Id, unsigned int Timeout);
CanGetTimeout = ccan_dll.CanGetTimeout
CanGetTimeout.restype = ctypes.c_long
CanGetTimeout.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_uint)]


#----------------------------------------
# Bus control function
#----------------------------------------
# C Prototype: long WINAPI CanBusConnect(short Id, unsigned short ChannelNo);
CanBusConnect = ccan_dll.CanBusConnect
CanBusConnect.restype = ctypes.c_long
CanBusConnect.argtypes = [ctypes.c_short, ctypes.c_ushort]

# C Prototype: long WINAPI CanBusDisconnect(short Id, unsigned short ChannelNo);
CanBusDisconnect = ccan_dll.CanBusDisconnect
CanBusDisconnect.restype = ctypes.c_long
CanBusDisconnect.argtypes = [ctypes.c_short, ctypes.c_ushort]


#----------------------------------------
# Communication function
#----------------------------------------
# C Prototype: long WINAPI CanTransmit(short Id, unsigned short ChannelNo, unsigned int MessageNum, unsigned int *MessageType, unsigned int *CanId, unsigned short *DataLength, unsigned char *Data);
CanTransmit = ccan_dll.CanTransmit
CanTransmit.restype = ctypes.c_long
CanTransmit.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_uint, ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ubyte)]

# C Prototype: long WINAPI CanReceive(short Id, unsigned short ChannelNo, unsigned int *MessageNum, unsigned int *MessageType, unsigned int *CanId, unsigned short *DataLength, unsigned char *Data, unsigned int *ErrorCode, unsigned long long *TickCount);
CanReceive = ccan_dll.CanReceive
CanReceive.restype = ctypes.c_long
CanReceive.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ubyte), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_ulonglong)]

# C Prototype: long WINAPI CanGetChannelStatus(short Id, unsigned short ChannelNo, unsigned short *ChannelStatus, unsigned short *TxErrorCount, unsigned short *RxErrorCount);
CanGetChannelStatus = ccan_dll.CanGetChannelStatus
CanGetChannelStatus.restype = ctypes.c_long
CanGetChannelStatus.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort)]


#----------------------------------------
# Event function
#----------------------------------------
# C Prototype: long WINAPI CanNotifyEvent(short Id, unsigned short ChannelNo, unsigned short EventType, PCAN_EVENT_CALLBACK CallBackProc, void *Param);
CanNotifyEvent = ccan_dll.CanNotifyEvent
CanNotifyEvent.restype = ctypes.c_long
CanNotifyEvent.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, PCAN_EVENT_CALLBACK, ctypes.c_void_p]

# C Prototype: long WINAPI CanStopNotifyEvent(short Id, unsigned short ChannelNo, unsigned short EventType);
CanStopNotifyEvent = ccan_dll.CanStopNotifyEvent
CanStopNotifyEvent.restype = ctypes.c_long
CanStopNotifyEvent.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]


#================================================================
# Parameters
#================================================================
#----------------------------------------
# Monitor mode
#----------------------------------------
CCAN_MONITOR_OFF = 0                          # Monitor mode OFF
CCAN_MONITOR_ON = 1                           # Monitor mode ON
#----------------------------------------
# Message type
#----------------------------------------
CCAN_MSG_TYPE_DISABLE = 0x00                  # All messages are valid
CCAN_MSG_TYPE_CAN = 0x01                      # CAN message
CCAN_MSG_TYPE_CAN_FD = 0x02                   # CAN FD message
CCAN_MSG_TYPE_REMOTE_FRAME = 0x04             # Remote frame
CCAN_MSG_TYPE_BITRATE_SWITCH = 0x08           # Bitrate switch
CCAN_MSG_TYPE_STANDARD_FORMAT = 0x10          # Standard format
CCAN_MSG_TYPE_EXTENDED_FORMAT = 0x20          # Extended format
#----------------------------------------
# CAN bus error
#----------------------------------------
CCAN_STATUS_ERR_BIT = 0x01                    # Bit error
CCAN_STATUS_ERR_FORM = 0x02                   # Form error
CCAN_STATUS_ERR_STUFF = 0x03                  # Stuff error
CCAN_STATUS_ERR_ACK = 0x04                    # ACK error
CCAN_STATUS_ERR_CRC = 0x05                    # CRC error
CCAN_STATUS_ERR_OTHER = 0x06                  # Other error
#----------------------------------------
# Channel status
#----------------------------------------
CCAN_CH_STATUS_ERR_ACTIVE = 0x0000            # Error active
CCAN_CH_STATUS_ERR_PASSIVE = 0x0001           # Error passive
CCAN_CH_STATUS_BUS_OFF = 0x0002               # Bus off
#----------------------------------------
# Event type
#----------------------------------------
CCAN_EVENT_RECEIVE = 0x01                     # Receive event


#================================================================
# Error codes
#================================================================
#----------------------------------------
# Initialize Error
#----------------------------------------
CAN_ERR_SUCCESS = 0                           # Normal completed
CAN_ERR_INI_RESOURCE = 1                      # Invalid resource reference specified
CAN_ERR_INI_INTERRUPT = 2                     # Invalid interrupt routine registered
CAN_ERR_INI_MEMORY = 3                        # Invalid memory allocation
CAN_ERR_INI_REGISTRY = 4                      # Invalid registry access
CAN_ERR_SYS_RECOVERED_FROM_STANDBY = 7        # Execute DioResetDevice function because the device has recovered from standby mode.
CAN_ERR_INI_NOT_FOUND_SYS_FILE = 8            # Because the ccan.sys file is not found, it is not possible to initialize it.
CAN_ERR_INI_DLL_FILE_VERSION = 9              # Because version information on the ccan.dll file cannot be acquired, it is not possible to initialize it.
CAN_ERR_INI_SYS_FILE_VERSION = 10             # Because version information on the ccan.sys file cannot be acquired, it is not possible to initialize it.
CAN_ERR_INI_NO_MATCH_DRV_VERSION = 11         # Because version information on ccan.dll and ccan.sys is different, it is not possible to initialize it.
#----------------------------------------
# DLL Error
#----------------------------------------
CAN_ERR_DLL_DEVICE_NAME = 10000               # Invalid device name specified.
CAN_ERR_DLL_INVALID_ID = 10001                # Invalid ID specified.
CAN_ERR_DLL_CALL_DRIVER = 10002               # Not call the driver.(Invalid device I/O controller)
CAN_ERR_DLL_CREATE_FILE = 10003               # Not create the file.(Invalid CreateFile)
CAN_ERR_DLL_CLOSE_FILE = 10004                # Not close the file.(Invalid CloseFile)
CAN_ERR_DLL_CREATE_THREAD = 10005             # Not create the thread.(Invalid CreateThread)
CAN_ERR_INFO_INVALID_DEVICE = 10050           # Invalid device information specified .Please check the spell.
CAN_ERR_INFO_NOT_FIND_DEVICE = 10051          # Not find the available device
CAN_ERR_INFO_INVALID_INFOTYPE = 10052         # Specified device information type beyond the limit
CAN_ERR_INFO_NOT_FOUND_DEVINFO = 10053        # No device information was found in the information retrieval.
CAN_ERR_DLL_BUFF_ADDRESS = 10100              # Invalid data buffer address
CAN_ERR_DLL_HWND = 10200                      # Window handle beyond the limit
CAN_ERR_DLL_EVENT_TYPE = 10300                # Trigger kind beyond the limit
CAN_ERR_DLL_CALLBACK = 10400                  # Invalid callback function address.
#----------------------------------------
# SYS Error
#----------------------------------------
CAN_ERR_SYS_MEMORY = 20000                    # Not secure memory
CAN_ERR_SYS_NOT_SUPPORTED = 20001             # This board couldn't use this function
CAN_ERR_SYS_BOARD_EXECUTING = 20002           # Board is behaving, not execute
CAN_ERR_SYS_USING_OTHER_PROCESS = 20003       # Other process is using the device, not execute
CAN_ERR_SYS_NOT_FOUND_PROCESS_DATA = 20004    # Process information is not found.
CAN_ERR_SYS_MONITOR_MODE = 20100              # Monitor mode is out of the settable range.
CAN_ERR_SYS_CH_NO = 20101                     # The channel number is out of the settable range.
CAN_ERR_SYS_DATA_FRAME_FORMAT = 20102         # Data frame format is out of the settable range.
CAN_ERR_SYS_BITRATE = 20110                   # Bitrate is out of the settable range.
CAN_ERR_SYS_TOTAL_TQ = 20111                  # Total TQ is out of the settable range.
CAN_ERR_SYS_TSEG1 = 20120                     # Time segment 1 is outside the settable range.
CAN_ERR_SYS_SJW = 20121                       # Sync jump width is out of the settable range.
CAN_ERR_SYS_TSEG2 = 20122                     # Time segment 2 is outside the settable range.
CAN_ERR_SYS_ARB_BITRATE = 20130               # Arbitration bitrate is outside the settable range.
CAN_ERR_SYS_ARB_TSEG1 = 20131                 # Arbitration time segment 1 is outside the settable range.
CAN_ERR_SYS_ARB_SJW = 20132                   # Arbitration synchronous jump width is outside the settable range.
CAN_ERR_SYS_ARB_TSEG2 = 20133                 # Arbitration time segment 2 is outside the settable range.
CAN_ERR_SYS_DATA_BITRATE = 20140              # Bitrate is out of the settable range.
CAN_ERR_SYS_DATA_TSEG1 = 20141                # Data time segment 1 is outside the settable range.
CAN_ERR_SYS_DATA_SJW = 20142                  # Data synchronization jump width is outside the settable range.
CAN_ERR_SYS_DATA_TSEG2 = 20143                # Data time segment 2 is outside the settable range.
CAN_ERR_SYS_MESSAGE_NUM = 20150               # Number of messages is out of the settable range.
CAN_ERR_SYS_MESSAGE_TYPE = 20151              # Message type is outside the settable range.
CAN_ERR_SYS_DATA_LEN = 20160                  # Data length is outside the settable range.
CAN_ERR_SYS_CAN_ID = 20170                    # CAN ID is outside the settable range.
CAN_ERR_SYS_TIMEOUT_TIME = 20180              # Timeout time is outside the settable range.
CAN_ERR_SYS_BUSON = 20200                     # Cannot execute because the bus on.
CAN_ERR_SYS_REC_FIFO_OVERFLOW = 20220         # Receive FIFO overflowed.
CAN_ERR_SYS_DEVICE_ACCESS = 20500             # The device could not be accessed normally.