/**********************************************************************
 *
 *  i7565DNM.h
 *
 *  Header for i7565DNM.c
 *
 *  Since 2011.12.09 by Golden Wang
 *
 **********************************************************************/
#include <stdio.h>
#include <stdlib.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 <pthread.h>
#include <unistd.h>
#include <math.h>
#include "msw.h"


/* Common Return Code */
#define I7565DNM_NoErr			0       //No Error
#define I7565DNM_MasterNoResp		10001   //Master No Response
#define I7565DNM_SlaveNoResp		10002	//Slave Has No Any Response
#define I7565DNM_DevPortErr		10003   //Device Port Over Range
#define I7565DNM_MACIDErr		10004   //MACID Over Range
#define I7565DNM_BaudRateErr		10005   //Baud Over Range
#define I7565DNM_ConnectionTypeErr	10006   //Connection Type Over Range
#define I7565DNM_ModNotExist		10007   //The Module doesn't exist in this Port
#define I7565DNM_PortNotOpen		10008   //The Port doesn't open
#define I7565DNM_PortCloseErr		10009   //The Port doesn't close
#define I7565DNM_ResetFirmwareErr	10010   //Reset Firmware Error
#define I7565DNM_GetFirmwareVerErr	10011   //Get Firmware Version Error
#define I7565DNM_SetMasterMACIDErr	10012   //Set Master MACID Error
#define I7565DNM_GetMasterMACIDErr	10013   //Get Master MACID Error
#define I7565DNM_SetBaudRateErr  	10014   //Set Master Baud Rate Error
#define I7565DNM_GetBaudRateErr  	10015   //Get Master Baud Rate Error
#define I7565DNM_AddDeviceErr	  	10016   //Add Slave Device Error
#define I7565DNM_RemoveDeviceErr  	10017   //Remove Slave Device Error
#define I7565DNM_AddIOConnectionErr 	10018   //Add IOConnection Error
#define I7565DNM_RemoveIOConnectionErr 	10019   //Remove IOConnection Error
#define I7565DNM_StartDeviceErr         10020   //Start Device Error
#define I7565DNM_StopDeviceErr          10021   //Stop Device Error
#define I7565DNM_StartAllDeviceErr      10022   //Start All Device Error
#define I7565DNM_StopAllDeviceErr       10023   //Stop All Device Error
#define I7565DNM_GetAttributeErr	10024	//Get Attribute Error
#define I7565DNM_GetAttributeValueErr	10025	//Get Attribute's Value Error 
#define I7565DNM_SetAttributeErr	10026	//Set Attribute Error
#define I7565DNM_ReadInputDataErr	10027	//Read Input Data Error
#define I7565DNM_WriteOutputDataErr	10028	//Write Output Data Error
#define I7565DNM_GetSlaveStatusErr	10029	//Get Slave Status Error
#define I7565DNM_SearchAllDevicesErr	10030	//Search All Devices Error
#define I7565DNM_SearchSpecificDeviceErr 10031	//Search Specific Device Error
#define I7565DNM_IsSearchOKErr		10032	//Check the result of Searching
#define I7565DNM_GetSearchedDevicesErr	10033	//Get Searched Device Error
#define I7565DNM_GetMasterStatusErr	10034	//Get Master Status Error
#define I7565DNM_GetDeviceInfoErr	10035	//Get Device Information Error
#define I7565DNM_GetLastDeviceErr	10036	//Get Last Device Error
#define I7565DNM_GetScanListErr		10037	//Get ScanList Error
#define I7565DNM_ImportEEPROMErr	10038	//Import EEPROM Error
#define I7565DNM_ClearAllConfigErr	10039	//Clear All Configuration Error
#define I7565DNM_SendExplicitMSGErr	10040	//Send Explicit Message Error
#define I7565DNM_IsExplicitMSGRespOKErr	10041	//Check the result of Sending ExplicitMSG
#define I7565DNM_GetExplicitMSGRespValueErr 10042 //Get the return value of Explicit Message
#define I7565DNM_ReadbackOutputDataErr 	10043	//Read I/O Data Error

#define I7565DNM_GetFirmDebugWndErr 	10044	//Get Firmware Debug Error
#define I7565DNM_DebugWndEnableErr 	10045	//Enable Firmware Debug Error
#define I7565DNM_IntoDownloadModeErr 	10046	//Into Download Mode Error
#define I7565DNM_Load_InDownloadModeErr 10047	//Into Download Mode Error
#define I7565DNM_Load_GetFirmwareInfoErr 10048	//Get Firmware Info Error
#define I7565DNM_Load_SendFirmwareInfoErr 10049	//Send Firmware info Error
#define I7565DNM_Load_SendFirmwareDataErr 10050	//Send Firmware Data Error


/* I7565DNM Function No definition */
#define _GetFirmwareVersion   1
#define _SetMasterMACID       2
#define _GetMasterMACID       3
#define _SetBaudRate          4
#define _GetBaudRate          5
#define _AddDevice            6
#define _RemoveDevice         7
#define _AddIOConnection      8
#define _RemoveIOConnection   9
#define _StartDevice          10
#define _StopDevice           11
#define _StartAllDevice       12
#define _StopAllDevice        13
#define _GetAttribute         14
#define _IsGetAttributeOK     15
#define _GetAttributeValue    16
#define _SetAttribute         17
#define _IsSetAttributeOK     18
#define _ReadInputData        19
#define _WriteOutputData      20
#define _ResetFirmware        21
#define _CheckModule          22
#define _GetMasterStatus      23
#define _GetSlaveStatus       24
#define _SearchAllDevices     25
#define _SearchSpecificDevice 26
#define _ImportEEPROM         27
#define _IsSearchOK           28
#define _GetSearchedDevices   29
#define _GetDeviceInfoFromScanList  30
#define _GetScanList          31
#define _ClearAllConfig       32

#define _SendExplicitMSG      33
#define _IsExplicitMSGRespOK  34
#define _GetExplicitMSGRespValue  35
#define _ReadbackOutputData   36

#define _GetAttributeW        37 //2013/05/22 for UCMM 2-byte  
#define _SetAttributeW        38 //2013/05/22 for UCMM 2-byte  
#define _SendExplicitMSG_W    39 //2013/05/22 for UCMM 2-byte  

#define _PauseIOConnection    40 //2016/06/14 for Pause IO Connection
#define _ResumeIOConnection   41 //2016/06/14 for Resume IO Connection

#define _DisableKeepAliveMsg  42 //2021/08/25 for Disable Keep Explicit Alive Message

#define _GetFirmDebugWnd      50
#define _FirmDebugWndEnable   51
#define _Load_IntoDownloadMode    52



/************************      VCI_CAN API Variable      *************************/ 
//Common Define
#define I7565DNM_LIBRARY_VERSION "0.1.3"
#define MAX_I7565DNM_SUPPORT 256
/*
#define CAN1						1
#define CAN2						2
#define CONFIG_MODE					0
#define COMM_MODE					1
#define CAN_BUF_SIZE            65536
#define CAN_BUF_MASK            0xffff
*/

//CAN Baudrate
#define CANBaud_125K 0
#define CANBaud_250K 1
#define CANBaud_500K 2

//I/O Connection Type
#define ConType_Explicit  0
#define ConType_Poll      1
#define ConType_BitStrobe 2
#define ConType_COS       3
#define ConType_Cyclic    4
#define ConType_MultiPoll 5

typedef  struct  _I7565DNM_DEVICE
{
        BYTE  PortOpen;
        BYTE  DevPort;
	char StrConfigData[2048];
	char szResult[2048];
        BYTE CMD_LSB;
	BYTE CMD_MSB;
#ifdef USE_SPINLOCK
pthread_spinlock_t spinlock;
#else
pthread_mutex_t mutex;
#endif
} _I7565DNM_DEV, *PI7565DNM_DEV;

/********************     [1] Init Function      ********************/
DWORD I7565DNM_ActiveModule(BYTE DevPort);
DWORD I7565DNM_CloseModule(BYTE DevPort);
char * I7565DNM__GetLibraryVersion(void);
DWORD I7565DNM_ResetFirmware(BYTE DevPort);
DWORD I7565DNM_GetFirmwareVersion(BYTE DevPort);
DWORD I7565DNM_SetMasterMACID(BYTE DevPort, BYTE MasterMACID);
DWORD I7565DNM_GetMasterMACID(BYTE DevPort);
DWORD I7565DNM_SetBaudRate(BYTE DevPort, BYTE BaudRate);
DWORD I7565DNM_GetBaudRate(BYTE DevPort);
DWORD I7565DNM_AddDevice(BYTE DevPort, BYTE DesMACID, WORD Explicit_EPR);
DWORD I7565DNM_RemoveDevice(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_AddIOConnection(BYTE DevPort, BYTE DesMACID, BYTE ConType, WORD DeviceInputLen, WORD DeviceOutputLen, WORD EPR);
DWORD I7565DNM_RemoveIOConnection(BYTE DevPort, BYTE DesMACID, BYTE ConType);
DWORD I7565DNM_PauseIOConnection(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_ResumeIOConnection(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_StartDevice(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_StopDevice(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_StartAllDevice(BYTE DevPort);
DWORD I7565DNM_StopAllDevice(BYTE DevPort);
DWORD I7565DNM_GetAttribute(BYTE DevPort, BYTE DesMACID, BYTE ClassID, BYTE InstanceID, BYTE AttributeID);
DWORD I7565DNM_GetAttributeW(BYTE DevPort, BYTE DesMACID, BYTE ClassID, BYTE InstanceID, BYTE AttributeID);
DWORD I7565DNM_IsGetAttributeOK(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_GetAttributeValue(BYTE DevPort, BYTE DesMACID, WORD *DataLen, BYTE *DATA);
DWORD I7565DNM_SetAttribute(BYTE DevPort, BYTE DesMACID, BYTE ClassID, BYTE InstanceID, BYTE AttributeID, WORD DataLen, BYTE *DATA);
DWORD I7565DNM_SetAttributeW(BYTE DevPort, BYTE DesMACID, BYTE ClassID, BYTE InstanceID, BYTE AttributeID, WORD DataLen, BYTE *DATA);
DWORD I7565DNM_IsSetAttributeOK(BYTE DevPort,BYTE DesMACID);
DWORD I7565DNM_ReadInputData(BYTE DevPort, BYTE DesMACID, BYTE ConType, WORD *IOLen, BYTE *IODATA);
DWORD I7565DNM_WriteOutputData(BYTE DevPort, BYTE DesMACID, BYTE ConType, WORD IOLen, BYTE *IODATA);
DWORD I7565DNM_ReadbackOutputData(BYTE DevPort, BYTE DesMACID, BYTE ConType, WORD *IOLen, BYTE *IODATA);
DWORD I7565DNM_GetSlaveStatus(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_SearchAllDevices(BYTE DevPort);
DWORD I7565DNM_SearchSpecificDevice(BYTE DevPort, WORD ListCount, BYTE *DesMACIDList);
DWORD I7565DNM_IsSearchOK(BYTE DevPort);
DWORD I7565DNM_GetSearchedDevices(BYTE DevPort, WORD *TotalDevices, BYTE *DesMACID, BYTE *Type, WORD *DeviceInputLen, WORD *DeviceOutputLen);
DWORD I7565DNM_GetMasterStatus(BYTE DevPort);
DWORD I7565DNM_GetDeviceInfoFromScanList(BYTE DevPort, BYTE DesMACID, WORD *ListCount, BYTE *ConnectionTypeList, WORD *InputDataLenList, WORD *OutputDataLenList, WORD *EPRList);
DWORD I7565DNM_GetLastDeviceError(BYTE DevPort, BYTE *Err_MACID, BYTE *Err_Type, BYTE *Err_Code);
DWORD I7565DNM_GetScanList(BYTE DevPort, WORD *TotalDevices, BYTE *DesMACIDList, BYTE *ConnectionTypeList, WORD *InputDataLenList, WORD *OutputDataLenList, WORD *EPR_List);
DWORD I7565DNM_ImportEEPROM(BYTE DevPort, WORD ListCount, BYTE *DesMACIDList, BYTE *ConnectionTypeList, WORD *InputDataLenList, WORD *OutputDataLenList, WORD *EPR_List);
DWORD I7565DNM_ClearAllConfig(BYTE DevPort);
DWORD I7565DNM_SendExplicitMSG(BYTE DevPort, BYTE DesMACID, BYTE ServiceID, BYTE ClassID, BYTE InstanceID, WORD DataLen, BYTE *DATA);
DWORD I7565DNM_SendExplicitMSG_W(BYTE DevPort, BYTE DesMACID, BYTE ServiceID, BYTE ClassID, BYTE InstanceID, WORD DataLen, BYTE *DATA);
DWORD I7565DNM_IsExplicitMSGRespOK(BYTE DevPort, BYTE DesMACID);
DWORD I7565DNM_GetExplicitMSGRespValue(BYTE DevPort, BYTE DesMACID, WORD *DataLen, BYTE *DATA);
DWORD I7565DNM_DisableKeepAliveMsg(BYTE DevPort, BYTE DesMACID);
