/* 
**
**  HOST ETHERNET INTERFACE (HEI) - HEADER FILE
**	
**	Copyright 1996-1997 - Host Automation Products, Inc.
*
*/
#if !defined(__HEI_H)
#define __HEI_H


/* Force structure alignment on single byte boundary */
#pragma pack(1)

typedef struct
	{
	WORD SizeofEthernetStats;
	DWORD MissedFrameCount;
	DWORD TransmitCollisionCount;
	DWORD DiscardedPackets;
	DWORD BadCRCCount;                             
	
	DWORD UnknownTypeCount;
	DWORD SendErrorCount;
	} EthernetStats;     

typedef struct
	{
	BYTE Version;
	BYTE Bytes2Follow;
	
#if defined (ANSI_C)
	BYTE UnusedBytess[16];							/* Support Info */
#else	
	BYTE SUP_FUN_POLLING 				: 1;			/* Polling one base */
	BYTE SUP_FUN_READ_VER_INFO			: 1; 			/* Version Info */
	BYTE SUP_FUN_READ_SUPPORT_INFO		: 1;			/* Support Info */
	BYTE SUP_FUN_READ_DEVICE_INFO		: 1;			/* Device Info  */
	BYTE SUP_FUN_POLLING_ALL			: 1; 			/* Polling all bases (returns ethernet address) */
	BYTE SUP_FUN_WRITE_IO				: 1;			/* Write IO base */
	BYTE SUP_FUN_READ_IO				: 1;			/* Read IO Base */
	BYTE SUP_FUN_READ_BASE_DEF			: 1; 			/* ReadBaseDef */
	
	BYTE SUP_FUN_ENUM_SETUP_DATA		: 1;			/* Enumerate setup data */
	BYTE SUP_FUN_READ_SETUP_DATA   		: 1;			/* Read setup data */
	BYTE SUP_FUN_WRITE_SETUP_DATA		: 1;			/* Write setup data */
	BYTE SUP_FUN_DELETE_SETUP_DATA		: 1; 			/* Delete setup data */
	BYTE SUP_FUN_READ_ETHERNET_STATS	: 1; 			/* Read ethernet statistics */
	BYTE SUP_FUN_PET_LINK				: 1; 			/* Used to keep the link sense timer from firing in the absense of ReadIO or WriteIO messages */
	BYTE SUP_FUN_ADDRESSED_BROADCAST	: 1; 			/* Used to broadcast to a particular ethernet address. */
	BYTE SUP_FUN_READ_MODULE_STATUS		: 1;			/* Read module status short BYTEs */
	
	BYTE SUP_FUN_EXTENDED				: 1;			/* Extended function */
	BYTE SUP_FUN_QUERY_SETUP_DATA		: 1;			/* Query for particular data type/value */
	BYTE SUP_FUN_INIT_BASE_DEF 			: 1;			/* Initialize base def from backplane. */
	BYTE SUP_FUN_DATA_BROADCAST			: 1;			/* Broadcast to a particular data type */
	BYTE SUP_FUN_CCM_REQUEST			: 1;			/* Perform CCM Request */
	BYTE SUP_FUN_KSEQ_REQUEST			: 1;			/* Perform KSEQ Request */
	BYTE SUP_FUN_BACKPLANE_REQUEST		: 1;			/* Perform backplane request */
	BYTE SUP_FUN_WRITE_BASE_DEF			: 1;			/* Write Base Def */
	
	BYTE SUP_FUN_EXTEND_RESPONSE		: 1;			/* Extends the response packet. */
	BYTE SUP_FUN_ACK					: 1;			/* Acknowledge */
	BYTE SUP_FUN_NAK					: 1;			/* NOT Acknowledge */
	BYTE SUP_FUN_RESPONSE			 	: 1;			/* Response */
	BYTE SUP_FUN_SERIAL_PORT			: 1;			/* Execute serial port function (see below) */
	BYTE SUP_FUN_WRITE_MEMORY			: 1;			/* Write a particular memory type */
	BYTE SUP_FUN_READ_MEMORY			: 1;			/* Read a particular memory type */
	BYTE SUP_FUN_ENUM_MEMORY			: 1;			/* Get list of all memory types */

	BYTE SUP_FUN_READ_SHARED_RAM		: 1;			/* Read shared ram */
	BYTE SUP_FUN_WRITE_SHARED_RAM		: 1;			/* Write shared ram */
	BYTE SUP_FUN_ACCESS_MEMORY			: 1;			/* Access (read/write) multiple memory types */
	BYTE Unused5 						: 1;			/* Unused */
	BYTE Unused4						: 1;			/* Unused */
	BYTE Unused3						: 1;			/* Unused */
	BYTE Unused2						: 1;			/* Unused */
	BYTE Unused1 						: 1;			/* Unused */
	
	BYTE UnusedBytess[11];							/* Unused */
#endif	
	} SupportDef;
	

/* Serial port defines */
#define SERIAL_1_STOP_BIT	0
#define SERIAL_2_STOP_BITS  1

#define SERIAL_7_DATA_BITS	0
#define SERIAL_8_DATA_BITS	1

#define SERIAL_NO_PARITY	0
#define SERIAL_ODD_PARITY	2
#define SERIAL_EVEN_PARITY	3

#define SERIAL_SLAVE		0
#define SERIAL_MASTER		1
#define SERIAL_PROXY		1

#define SERIAL_NO_RTS		0
#define SERIAL_USE_RTS		1
	
typedef struct
	{
	DWORD BaudRate;				/* Baud rate to use i.e. 9600 */
	
#if defined (ANSI_C)
	BYTE 	ConfigData;			
#else
	BYTE	StopBits 	: 1;    /* 0 == 1 Stop bit;  1 == 2 Stop bits */
	BYTE 	DataBits	: 1;    /* 0 == 7 Data bits;  1 == 8 Data bits */
	BYTE	Parity		: 2;    /* 0 == 1 == None; 2 == Odd;  3 == Even */
	BYTE	Mode		: 1;    /* 0 == Slave;  1 == Master/Proxy */
	BYTE	UseRTS		: 1;	/* 0 == Don't use RTS line;  1 == Use RTS line */
	BYTE	Reserved	: 2;    /* Reserved locations */
#endif	
	BYTE	PreTransmitDelay;	/* If UseRTS == 1 delay this many ms (times 2) before starting transmit */
	BYTE	PostTransmitDelay;  /* If UseRTS == 1 delay this many ms (times 2) after ending transmit */
	BYTE 	Unused[1];
	} SerialSetup;

typedef struct
	{
	BYTE	MajorVersion;
	BYTE	MinorVersion;
	WORD	BuildVersion;
	} VersionDef;
	
typedef struct
	{
	BYTE SizeofVersionInfo;
	VersionDef BootVersion;
	VersionDef OSVersion;
	BYTE	NumOSExtensions;
	VersionDef OSExt[10];
	}	VersionInfoDef;
	
typedef struct
	{
	BYTE PLCFamily;				/* 2 == 205 */
								/* 3 == 305 */
								/* 4 == 405 */
	BYTE Unused1;
	BYTE ModuleType;			/* 0 == EBC */
								/* 1 == ECom */
								/* &0x80 == Fiber */
								
	BYTE StatusCode;				
	BYTE EthernetAddress[6];	/* Hardware ethernet address */
	WORD RamSize;  				/* In K-Byte increments */
	WORD FlashSize; 			/* In K-Byte increments */
	BYTE DIPSettings;			/* Settings of the 8 dip switches. */
	BYTE MediaType;				/* 0 == 10-Base T */
								/* 1 == 10-Base F */
	DWORD EPFCount;				/* Early power fail count (405 EBC)*/

#if defined (ANSI_C)
	BYTE 	Status;			
#else
	BYTE RunRelay	: 1;		/* 405 EBC Run Relay Status */
	BYTE BattLow	: 1;			/* 405 EBC Battery Low indicator */
	BYTE UnusedBits	: 6;		/* Unused status bits */
#endif

	BYTE Unused[21];
	} DeviceDef;

typedef struct
	{
	DWORD Timeout;		/* Timeout 0 == Don't use link monitor */
	BYTE Mode;			/* Mode: */
							/* 		0 == Clear outputs */
							/*			1 == Set outputs to given I/O data pattern */
	BYTE Data[251];	/* Pattern:  Used with set outputs,  same format */
							/* 			 as data for HEIWriteIO call. */
	} LinkMonitor;

typedef struct
	{ 
	BYTE Algorithm;  	/* Algorithm to use for encryption */
							/*		0 == No encryption */
							/*		1 == Private key encryption */
							/* */
	BYTE Unused[3];		/* Reserved for later */
	BYTE Key[60];		/* Encryption key (null terminated) */
	}  Encryption;
	

typedef struct
	{
	WORD Type;			/* Type of memory */
	DWORD Size;			/* Size of memory */
	DWORD Unused[4];	/* Unused */
	} MemoryTypeDef;
	
typedef struct
	{
	WORD SizeofSettings		;				/* sizeof(HEISettings) */
	
	/* Action items. */			
	DWORD Flags;        						/* Flags used to control things. */
													/* Bit:   	Function:																				*/
													/*		0-31 		Unused																				*/
	
	/* RXWX Config items. */
	WORD RXWXACKTimeout 			;			/* Timeout for receiving ACK / NAK */
	WORD RXWXResponseTimeout 	;			/* Timeout for receiving response. */
	WORD RXWXMaxRetrys			;			/* Number of times to retry a transaction. */
	
	/* RXWX Stat Items. */
	WORD RXWXMaxACKTime			;			/* STAT: Max number of ms we have waited for an ack. */
	WORD RXWXMaxRSPTime			;			/* STAT: Max number of ms we have waited for a response. */
	DWORD RXWXACKRetrys			;      	/* STAT: Number of retrys for an ack. */
	DWORD RXWXRSPRetrys			;			/* STAT: Number of retrys for a response. */
	DWORD RXWXCompleted			;			/* STAT: Number of successfully completed transactions */
	DWORD RXWXTimeouts			;			/* STAT: Number of timeouts on transactions (after retrys) */
	DWORD RXWXOverruns			;			/* STAT: Number of times the PLC requested a transaction while */
													/* one was being processed. */
	DWORD RXWXErrors				;			/* STAT: Number of times an invalid code was found or a transaction */
													/* was NAKed. */
													
	/* Other stuff */													
	BYTE Version;								/* Version of this structure.  Currently 0 */
	
	/* K-Sequence Retrys */
	WORD KSeqMaxRetrys;						/* Max number of times to retry a K-Sequence request 	*/
	WORD KSeqRetrys;							/* STAT: Number of K-Sequence retrys.								*/
	WORD KSeqTimeouts;						/* STAT: Number of K-Sequence timeouts. */
	
	BYTE Unused[81];							/* Reserved for future use.  (Configure protocol, etc. */
	} HEISettings;

	
typedef struct
	{
	BYTE RunRelayMode;	/* 405 EBC Run Relay Mode */
						/* See Run relay modes below. */
	BYTE Unused[63];
	} ModuleSetup;



/* Run relay Modes */
#define RRM_LINK_GOOD		0	/* Run relay on when link is good (default) */
#define RRM_LINK_NOT_GOOD	1	/* Run relay on when link is not good */
#define RRM_POWERUP_ON		2	/* EBC turns run relay mode on on powerup			*/
#define RRM_MANUAL_ON		3	/* Run relay mode is controlled by control software */


#define ACCESS_READ		0
#define ACCESS_WRITE	1

typedef struct sMemRefDetail
	{
	BYTE Direction;		// ACCESS_READ == Read, ACCESS_WRITE == Write
	WORD Type;			// Memory type
	DWORD Offset;		// Memory Offset
	WORD NumBytes;		// Number of bytes
	} MemRefDetail;

typedef struct
	{
	MemRefDetail Detail;
	BYTE *pBuffer;		// Data buffer
	} MemRef;


#if !defined(FIRMWARE)

#if defined(HEIDEFS)
typedef int		BOOL;
typedef unsigned char	BYTE;
typedef unsigned short	WORD;
typedef unsigned long	DWORD;
#define FALSE 0
#define TRUE 1
#endif /* #if defined(HEIDEFS) */


#define HEIAPIVERSION 2
#define HEIAPIVERSIONSTRING "2"

/* Protocol Defines */
#define HEIP_HOST   		1
#define HEIP_IPX	  		2
#define HEIP_IP			3

#define HEIT_HOST			1
#define HEIT_IPX			2	
#define HEIT_WINSOCK		4
#define HEIT_OTHER_TRANSPORT 8
#define HEIT_UNIX			16

/* Encryption Algorithms */
#define HEIEN_NONE			0	
#define HEIEN_A1			1  

#define PACKET_HEADER_SIZE 9

#if defined(HEIDOS)
#define __declspec(dllexport)
#endif	/* #if defined(HEIDOS) */

#if defined(HEI16)
#define __declspec(dllexport)
#endif	/* #if defined(HEI16) */

#if defined(HEIUNIX)
#define __declspec(dllexport)
#endif	/* #if defined(HEIT_UNIX) */


/* Default values for HEIOpenDevice */
#define DefDevTimeout 10
#define DefDevRetrys  3
#define DefDevUseAddressedBroadcast FALSE


typedef struct 
	{
	union
		{
		/* Use this for HEIP_HOST protocol addressing. */
		struct 
			{
			short Family;			/* AF_UNSPEC == 0 */
			char Nodenum[6];		/* Ethernet network address */
			unsigned short LANNum;	/* Lana number */
			} AddressHost;

		/* Use this for HEIP_IPX protocol addressing. */
		struct 
			{
			short Family;			/* AF_IPX == 6 */
			char Netnum[4];			/* Network number */
			char Nodenum[6];		/* Ethernet network address */
			unsigned short Socket;	/* Socket number == 0x7070 */
			} AddressIPX;

		/* Use this for HEIP_IP protocol addressing. */
		struct 
			{
			short Family;			/* AF_INET == 2 */
			unsigned short Port;	/* Port number == 0x7070 */
			union					/* Internet address */
				{
				struct { unsigned char b1,b2,b3,b4; } bAddr;/* Byte addressing */
				struct { unsigned short w1,w2; } wAddr;		/* Word addressing */
				unsigned long lAddr;						/* DWord addressing */
				} AddressingType;
			char  Zero[8];			/* Initialize to zeros */
			} AddressIP;

		/* This is the generic address buffer. */
		BYTE Raw[20];
#ifdef ANSI_C	/*  ANSI C cannot have anonymous structures */
		} Address;
#else
		};
#endif
	} ENetAddress;


#if defined(__cplusplus)
extern "C"
	{
#endif	/* #if defined(__cplusplus) */

	/* INITIALIZATION / SHUTDOWN */
	__declspec(dllexport) int HEIOpen(WORD HEIAPIVersion);
	__declspec(dllexport) int HEIClose();
	
	/* Protocol Stuff */
	typedef struct 
		{
		WORD Transport;		/* HEIT_HOST */
							/* HEIT_IPX */
							/* HEIT_NETBIOS */
							/* HEIT_WINSOCK */

		WORD Protocol;		/* HEIP_HOST*/
							/* HEIP_IPX */
							/* HEIP_IP */
							/* HEIP_NETBIOS */
		/* Encryption stuff. */
		Encryption Encrypt;	/* Set this up before calling HEIOpenTransport  */
							/* .Algorithm == HEIEN_NONE OR HEIEN_A1 */
							/* .Key == Encryption key */
									
		//DWORD NetworkAddress;
		ENetAddress *pSourceAddress;	/* Used for multiple ethernet cards.  This address is the address of the card to use. */

		/*int (*pSendPacket)(HEIDevice *pDevice, BYTE *pPacket, WORD PacketSize);*/
		/*int (*pReceivePacket)(HEIDevice *pDevice, BYTE *pResponse, int *pResponseSize);*/

		} HEITransport;
	
	__declspec(dllexport) int HEIOpenTransport(HEITransport *pTransport, WORD Version,  ENetAddress *pSourceAddress);
	/* __declspec(dllexport) int HEIOpenTransport(HEITransport *pTransport, WORD Version, DWORD NetworkAddress );*/
	/* __declspec(dllexport) int HEIOpenTransport(HEITransport *pTransport, WORD Version, DWORD NetworkAddress = 0); */
	__declspec(dllexport) int HEICloseTransport(HEITransport *pTransport);
	
	
	/* DEVICE Stuff */
	typedef struct 
		{
		union
			{
			/* Use this for HEIP_HOST protocol addressing. */
			struct 
				{
				short Family;			/* AF_UNSPEC == 0 */
				char Nodenum[6];		/* Ethernet network address */
				unsigned short LANNum;	/* Lana number */
				} AddressHost;

			/* Use this for HEIP_IPX protocol addressing. */
			struct 
				{
				short Family;			/* AF_IPX == 6 */
				char Netnum[4];			/* Network number */
				char Nodenum[6];		/* Ethernet network address */
				unsigned short Socket;	/* Socket number == 0x7070 */
				} AddressIPX;

			/* Use this for HEIP_IP protocol addressing. */
			struct 
				{
				short Family;			/* AF_INET == 2 */
				unsigned short Port;	/* Port number == 0x7070 */
				union					/* Internet address */
					{
					struct { unsigned char b1,b2,b3,b4; } bAddr;/* Byte addressing */
					struct { unsigned short w1,w2; } wAddr;		/* Word addressing */
					unsigned long lAddr;						/* DWord addressing */
					} AddressingType;
				char  Zero[8];			/* Initialize to zeros */
				} AddressIP;

			/* This is the generic address buffer. */
			BYTE Raw[20];
			} Address;

		WORD wParam;				/* Application can use this. */
		DWORD dwParam;				/* Application can use this. */

		WORD Timeout;				/* Timeout value in ms (can be changed without closing the device). */
		WORD Retrys;				/* Number of times to retry (can be changed without closing the device). */

		BYTE ENetAddress[6];		/* The ethernet address is placed here in the HEIQueryDevices call. */

		WORD RetryCount;			/* Number of retrys which have occured. */
		WORD BadCRCCount;		 	/* Number of packets received with bad CRC */
		WORD LatePacketCount; 		/* Number of packets received, but after a timeout */

		BOOL ParallelPackets;		/* Setting this to TRUE (after HEIOpenDevice) will enable an application */
									/* to send multiple HEIReadIO, HEIWriteIO, HEICCMRequest or  HEIKSEQRequest requests  */
									/* (to different) modules before waiting for any responses.   */
									/* The application will then need to implement its own retry / timeout mechanism  */
									/* while waiting for the responses. */
									/* The application uses HEIGetResponse to see if a response for a module has arrived. */
									/* NOTE: The application should not send multiple requests to a single module */
									/*       without waiting for the response in between. */

		/* Internal - Do not touch!! */
		BOOL UseAddressedBroadcast;	/* Need to close the device and reopen it to change this! */
		BOOL UseBroadcast;				
		DWORD _dwParam;	
		WORD DataOffset;
		HEITransport *_pTransport;	/* Need to close the device and reopen it to change this! */
		int SizeOfData;
		BYTE *pData;
		void *pBuffer;
		unsigned short LastAppVal;
		} HEIDevice;
	
	__declspec(dllexport) int HEIQueryDevices(HEITransport *pTransport, HEIDevice *pDevices, WORD *pNumDevices, WORD HEIAPIVersion);
	
	__declspec(dllexport) int HEIOpenDevice(HEITransport *pTransport, HEIDevice *pDevice, WORD HEIAPIVersion, WORD Timeout, WORD Retrys, BOOL UseAddressedBroadcast);
	/*	__declspec(dllexport) int HEIOpenDevice(HEITransport *pTransport, HEIDevice *pDevice, WORD HEIAPIVersion, WORD Timeout=DefDevTimeout, WORD Retrys=DefDevRetrys, BOOL UseAddressedBroadcast=DefDevUseAddressedBroadcast); */
	__declspec(dllexport) int HEICloseDevice(HEIDevice *pDevice);
	
	__declspec(dllexport) int HEIQueryDeviceData(HEITransport *pTransport, HEIDevice *pDevices, WORD *pNumDevices, WORD HEIAPIVersion, WORD DataType, BYTE *pData, WORD SizeofData);
	
	/* SUPPORT INFORMATION */
	__declspec(dllexport) int HEIReadSupportInfo(HEIDevice *pDevice, BYTE *pSupportInfo, WORD SizeOfSupportInfo);
	
	
	/* VERSION INFORMATION */
	__declspec(dllexport) int HEIReadVersionInfo(HEIDevice *pDevice, BYTE *pVerInfo, WORD SizeVerInfo);
	
	
	/* BASE DEFINITION */
	__declspec(dllexport) int HEIReadBaseDef(HEIDevice *pDevice, BYTE *pBaseDefInfo, WORD *pSizeOfBaseDefInfo);
	__declspec(dllexport) int HEIWriteBaseDef(HEIDevice *pDevice, BYTE *pInputBaseDef, WORD SizeOfInputBaseDef, BYTE *pOutputBaseDef, WORD *pSizeOfOutputBaseDef);
	
	
	/* DEVICE DEFINITION */
	__declspec(dllexport) int HEIReadDeviceDef(HEIDevice *pDevice, BYTE *pModuleDefInfo, WORD SizeOfModuleDefInfo);

	/* IO ACCESS */
	__declspec(dllexport) int HEIReadIO(HEIDevice *pDevice, BYTE *pData, WORD *DataSize);
	__declspec(dllexport) int HEIWriteIO(HEIDevice *pDevice, BYTE *pData, WORD SizeofData, BYTE *pReturnData, WORD *pSizeofReturnData);
	/* __declspec(dllexport) int HEIWriteIO(HEIDevice *pDevice, BYTE *pData, WORD SizeofData, BYTE *pReturnData=(BYTE *)NULL, WORD *pSizeofReturnData=(WORD *) NULL); */

	__declspec(dllexport) int HEIReadIOEx(HEIDevice *apDevice[], BYTE *apData[], WORD aSizeofData[], int aErrorCode[], int DeviceCount);
	__declspec(dllexport) int HEIWriteIOEx(HEIDevice *apDevice[], BYTE *apData[], WORD aSizeofData[], BYTE *apReturnData[], WORD aSizeofReturnData[], int aErrorCode[], int DeviceCount);

	/* SETUP */
	__declspec(dllexport) int HEIReadSetupData(HEIDevice *pDevice, WORD SetupType, BYTE *pData, WORD *pSizeofData);
	__declspec(dllexport) int HEIWriteSetupData(HEIDevice *pDevice, WORD SetupType, BYTE *pData, WORD SizeofData);
	__declspec(dllexport) int HEIDeleteSetupData(HEIDevice *pDevice, WORD SetupType);
	__declspec(dllexport) int HEIEnumSetupData(HEIDevice *pDevice, WORD *pData, WORD *pSizeofDataInWords);

	/* WATCHDOG */
	__declspec(dllexport) int HEIPetDevice(HEIDevice *pDevice);

	/* STATISTICS */
	__declspec(dllexport) int HEIReadEthernetStats(HEIDevice *pDevice, BYTE *pData, WORD *DataSize, BOOL Clear);
	/* __declspec(dllexport) int HEIReadEthernetStats(HEIDevice *pDevice, BYTE *pData, WORD *DataSize, BOOL Clear=FALSE); */

	/* MODULES STATUS */
	__declspec(dllexport) int HEIReadModuleStatus(HEIDevice *pDevice, BYTE *pData, WORD *DataSize, BOOL Reset);
	/* __declspec(dllexport) int HEIReadModuleStatus(HEIDevice *pDevice, BYTE *pData, WORD *DataSize, BOOL Reset=FALSE); */

	/* Multiple Packet Stuff. */
	__declspec(dllexport) int HEIGetResponse(HEIDevice *pDevice, BYTE *pResponse, int *pResponseSize, BOOL CheckAppVal);
	/* __declspec(dllexport) int HEIGetResponse(HEIDevice *pDevice, BYTE *pResponse, int *pResponseSize, BOOL CheckAppVal=TRUE); */

   /* ECOM STUFF */
	__declspec(dllexport) int HEICCMRequest(HEIDevice *pDevice, BOOL bWrite, BYTE DataType, WORD Address, WORD DataLen, BYTE *pData);
	__declspec(dllexport) int HEIKSEQRequest(HEIDevice *pDevice, WORD DataLenIn, BYTE *pData, WORD *pDataLen);

	/* SERIAL PORT STUFF */
	__declspec(dllexport) int HEIWriteComm(HEIDevice *pDevice, WORD Num2Write, BYTE *pData);
	__declspec(dllexport) int HEIReadComm(HEIDevice *pDevice, WORD *pNum2Read, BYTE *pData);
	__declspec(dllexport) int HEIGetRXAvailable(HEIDevice *pDevice, WORD *pAvailable);
	__declspec(dllexport) int HEIFlushRXQueue(HEIDevice *pDevice);
	__declspec(dllexport) int HEIGetTXLeft(HEIDevice *pDevice, WORD *pLeft);
	__declspec(dllexport) int HEISetupSerialPort(HEIDevice *pDevice, SerialSetup *pSetup, BOOL WriteToFlash);
	__declspec(dllexport) int HEIReadSerialPortSetup(HEIDevice *pDevice, SerialSetup *pSetup);
	
	/* MEMORY STUFF (see below for types) */
	__declspec(dllexport) int HEIReadMemory(HEIDevice *pDevice, WORD Type, DWORD Offset, WORD NumBytes, BYTE *pBuffer);
	__declspec(dllexport) int HEIWriteMemory(HEIDevice *pDevice, WORD Type, DWORD Offset, WORD NumBytes, BYTE *pBuffer);
	__declspec(dllexport) int HEIENumMemory(HEIDevice *pDevice, WORD *pNumWords, MemoryTypeDef *pBuffer);
	__declspec(dllexport) int HEIAccessMemory(HEIDevice *pDevice, MemRef MemRefs[], WORD NumRefs);


	/* SHARED RAM STUFF (for Intelligent modules) */
	__declspec(dllexport) int HEIReadSharedRAM(HEIDevice *pDevice, WORD Base, WORD Slot, WORD Address, WORD Bytes2Read, BYTE *pBuffer);
	__declspec(dllexport) int HEIWriteSharedRAM(HEIDevice *pDevice, WORD Base, WORD Slot, WORD Address, WORD Bytes2Write, BYTE *pBuffer);

#if defined(__cplusplus)
	}
#endif	/* #if defined(__cplusplus) */

#endif	/* #if !defined(FIRMWARE) */

/* Errors */
#define HEIE_NULL								0	/* No Error */
#define HEIE_FIRST_ERROR					0x8000	/* Number for First Error */
#define HEIE_LAST_ERROR						0xFFFF	/* Number for Last Error */
#define HEIE_NOT_IMPLEMENTED				0x8000	/* Function not implemented */
#define HEIE_VER_MISMATCH					0x8001	/* Version passed to function not correct for library */
#define HEIE_UNSUPPORTED_TRANSPORT			0x8002	/* Supplied transport not supported */
#define HEIE_INVALID_DEVICE					0x8003	/* Supplied device is not valid */
#define HEIE_BUFFER_TOO_SMALL				0x8004	/* Supplied buffer is too small */
#define HEIE_ZERO_BYTES_RECEIVED			0x8005	/* Zero bytes were returned in the packet */
#define HEIE_TIMEOUT						0x8006	/* Timeout error */
#define HEIE_UNSUPPORTED_PROTOCOL			0x8007	/* Supplied protocol not supported */
#define HEIE_IP_ADDR_NOT_INITIALIZED		0x8008	/* The devices IP address has not be set. NOTE: Need to use */
													/* addressed broadcast to talk to the module (with IP) to setup */
													/* the IP address. */
#define HEIE_NULL_TRANSPORT	 				0x8009	/* No transport specified. */
#define HEIE_IPX_NOT_INSTALLED				0x800A	/* IPX Transport not installed. */
#define HEIE_IPX_OPEN_SOCKET			 	0x800B	/* Error opening IPX Socket. */
#define HEIE_NO_PACKET_DRIVER				0x800C	/* No packet driver found. */
#define HEIE_CRC_MISMATCH					0x800D	/* CRC did not match. */
#define HEIE_ALLOCATION_ERROR				0x800E	/* Memory allocation error failed. */
#define HEIE_NO_IPX_CACHE					0x800F	/* No cache has been allocated for IPX */
#define HEIE_INVALID_REQUEST				0x8010	/* Invalid request */
#define HEIE_NO_RESPONSE					0x8011	/* No response was available./ requested */
#define HEIE_INVALID_RESPONSE				0x8012	/* Invalid format response was received. */
#define HEIE_DATA_TOO_LARGE					0x8013	/* Given data is too large */
#define HEIE_LOAD_PROC_ERROR				0x8014  /* Error loading procedures */
#define HEIE_NOT_LOADED						0x8015	/* Attempted command before successfull OpenTransport */

#define HEIE_LOAD_ERROR						0x8100	/* Mask for WinSock load Error see below (from HEIOpenTransport) */
#define HEIE_LAST_LOAD_ERROR				0x811F	/* Last in the range of WinSock load Errors */

#define HEIE_IO_ERROR						0x9000	/* Mask for IO Error condition (from HEIReadModuleStatus, HEIReadIO, or HEIWriteIO) */
#define HEIE_IO_WARNING						0xA000	/* Mask for IO Warning condition (from HEIReadModuleStatus, HEIReadIO, or HEIWriteIO) */
#define HEIE_IO_INFO						0xB000	/* Mask for IO Info condition (from HEIReadModuleStatus, HEIReadIO, or HEIWriteIO) */


/* 	Winsock Load Errors
If OpenTransport for WinSock fails, it will return one of the following errors:
0x8014	Error getting addresses from WinSock.DLL
0x8100	System was out of memory, executable file was corrupt, or relocations were invalid. 
0x8101	Unused
0x8102	File was not found. 
0x8103	Path was not found. 
0x8104	Unused
0x8105	Attempt was made to dynamically link to a task, or there was a sharing or network-protection error. 
0x8106	Library required separate data segments for each task. 
0x8107  Unused
0x8108	There was insufficient memory to start the application. 
0x8109  Unused
0x810A	Windows version was incorrect. 
0x810B	Executable file was invalid. Either it was not a Windows application or there was an error in the .EXE image. 
0x810C	Application was designed for a different operating system. 
0x810D	Application was designed for MS-DOS 4.0. 
0x810E	Type of executable file was unknown. 
0x810F	Attempt was made to load a real-mode application (developed for an earlier version of Windows). 
0x8110	Attempt was made to load a second instance of an executable file containing multiple data segments that were not marked read-only. 
0x8111	Unused
0x8112	Unused
0x8113	Attempt was made to load a compressed executable file. The file must be decompressed before it can be loaded. 
0x8114	Dynamic-link library (DLL) file was invalid. One of the DLLs required to run this application was corrupt. 
0x8115	Application requires Microsoft Windows 32-bit extensions. 
0x8116-0x811F	Unused
*/
													

/* Warnings */
#define HEIW_FIRST_WARNING					0x4000	/* Number for First Error */
#define HEIW_LAST_WARNING					0x4FFF	/* Number for Last Error */
#define	HEIW_RETRY							0x4000	/* One or more retrys have occurred. */


/* These are masks for values returned from HEIReadIO and/or HEIWriteIO and indicate that some  */
/* error/warning/info condition exists for some module in the base (it could be an I/O  */
/* module or it could be the ethernet module.  The function HEIReadModuleStatus can then be used  */
/* to retrieve the actual conditions.  Note that more than one of the conditions can exist at any time. */
#define MASK_DEVICE_ERROR			0x1000
#define MASK_DEVICE_WARNING  		0x2000
#define MASK_DEVICE_INFO     		0x4000


/* Data Types */
#define DT_IP_ADDRESS				0x0010	/* 4 Byte IP address */
#define DT_NODE_NUMBER				0x0020	/* 4 Byte Node Number */
#define DT_NODE_NAME				0x0016	/* 256 Byte Node Name */
#define DT_DESCRIPTION				0x0026	/* 256 Byte Node Description */
#define DT_LINK_MONITOR				0x8006	/* 256 Byte Link monitor setup (Ram Based) */
											/* See LinkMonitor structure below */
#define DT_ENCRYPT_KEY_FLASH		0x0014	/* 64 Byte key data from FALSH See Encryption structure */
#define DT_ENCRYPT_KEY_RAM			0x8014	/* 64 Byte key data from RAM - This is the working copy! */
											/* See Encryption structure */
#define DT_RXWX_SETTINGS			0x0015	/* 128 Bytes settings see HEISettings */
#define DT_SETTINGS					0x0015	/* 128 Bytes settings see HEISettings */
#define DT_SERIAL_SETUP				0x0011	/* 8 Byte Serial Setup (see SerialSetup)*/
#define DT_BASE_DEF					0x0017	/* 512 Byte Base Def (405 HEIWriteBaseDef) */
#define DT_MODULE_SETUP				0x0024	/* 64 Byte data from FLASH. See ModuleSetup structure */


/* Common functions. */
#define FUN_POLLING  				0x00 	/* Polling one module */
#define FUN_READ_VER_INFO			0x01 	/* Read Version Info from module */
#define FUN_READ_SUPPORT_INFO		0x02 	/* Read Support Info from module */
											/* 0x03 UNUSED                								 */
#define FUN_READ_DEVICE_INFO		0x04 	/* Read Device Info from module */
#define FUN_POLLING_ALL				0x05 	/* Polling all bases on the network (returns ethernet address) */
											/* Used as a broadcast function to locate devices on the network. */
#define FUN_WRITE_IO				0x06 	/* Write IO data to the base */
#define FUN_READ_IO					0x07 	/* Read IO data from Base */
#define FUN_READ_BASE_DEF			0x08 	/* Read Base Definition */
#define FUN_QUERY_SETUP_DATA		0x09	/* Query the network for a particular type of data with a particular value */
#define FUN_ENUM_SETUP_DATA			0x0A	/* Enumerate the types of data stored in a module */
#define FUN_READ_SETUP_DATA			0x0B	/* Read a particular type of data from a module */
#define FUN_WRITE_SETUP_DATA		0x0C	/* Write a particular type of data to a module */
#define FUN_DELETE_SETUP_DATA		0x0D	/* Delete a particular type of data from a module */
#define FUN_READ_ETHERNET_STATS		0x0E	/* Read ethernet statistics from the module. */
#define FUN_WRITE_BASE_DEF			0x0F	/* Write Base Definition (305/405 EBC) */
											/* 0x10 UNUSED                								 */
											/* 0x11 UNUSED                								 */
											/* 0x12 UNUSED                								 */
											/* 0x13 UNUSED                								 */
#define FUN_PET_LINK				0x14 	/* Used to keep the link sense timer from firing in the absense of ReadIO or WriteIO messages */
#define FUN_ADDRESSED_BROADCAST		0x15	/* Used to broadcast to a particular ethernet address. Used to setup IP address in a new module. */
#define FUN_DATA_BROADCAST			0x16	/* Used to broadcast to a particular data type. */
#define FUN_READ_MODULE_STATUS		0x17	/* Read the status bytes from each of the slots of the module. */
#define FUN_INIT_BASE_DEF			0x18	/* Initialize the base def by re-reading from the backplane */
#define FUN_CCM_REQUEST				0x19	/* Perform a CCM Request with an ECom Module */
#define FUN_KSEQ_REQUEST			0x1A	/* Perform a K-Seq Request with an ECom Module */
#define FUN_BACKPLANE_REQUEST		0x1B	/* Perform a backplane request on an ECom Module */
#define FUN_EXTEND_RESPONSE			0x1C 	/* Extends the response packet. */

#define FUN_ACK						0x20	/* Acknowledge */
#define FUN_NAK						0x21	/* Not acknowledge */
#define FUN_RESPONSE				0x22	/* Response */

#define FUN_SERIAL_PORT				0x23	/* Execute serial port function (see below) */
#define FUN_WRITE_MEMORY			0x24	/* Write a particular memory type */
#define FUN_READ_MEMORY				0x25	/* Read a particular memory type */
#define FUN_ENUM_MEMORY				0x26	/* Get list of all memory types */
#define FUN_ACCESS_MEMORY			0x27	/* Access (Read/Write) multiple memory types */

#define FUN_READ_SHARED_RAM			0x28	/* Read shared ram */
#define FUN_WRITE_SHARED_RAM		0x29	/* Write shared ram */


/* Serial port functions */
#define SPF_WRITE_COMM				0x01	/* Writes one or more characters to the serial port */
#define SPF_READ_COMM				0x02    /* Reads zero or more characters from the serial port */
#define SPF_RX_AVAILABLE			0x03    /* Returns number of characters available in the input queue */
#define SPF_RX_FLUSH				0x04    /* Flushs the serial port input queue */
#define SPF_SETUP					0x05	/* Setup serial port (see SerialSetup above) */
#define SPF_TX_LEFT					0x06    /* Returns number of characters left in the output queue */
#define SPF_READ_SETUP				0x07	/* Read Setup serial port (see SerialSetup above) */


/*
	Memory Types.
	Description of memory types.
	A memory type is a 16 bit value that is divided up as follows:
			FEDCBA9876543210
			PKAAFFFFTTTTUNNN
			||| |   |   ||
			||| |   |   ||-- Number: Index number from 0 to 7
			||| |   |   |
			||| |   |   |-- Undefined (set to zero)
			||| |   |
			||| |   |-- Type: 0000 = General Purpose
			||| |             0001 = Program
			||| |             0010 = System
			||| |             0011 = Timer
			||| |             0100 = Counter
			||| |             0101 = Setup
			||| |             0110 = Input
			||| |             0111 = Output
			||| |             1000 = Undefined 
			||| |             1001 = Undefined 
			||| |             1010 = Undefined 
			||| |             1011 = Undefined 
			||| |             1100 = Undefined 
			||| |             1101 = Undefined 
			||| |             1110 = Undefined 
			||| |             1111 = Undefined 
			||| |
			||| |-- Format: 0000 = Bit
			|||             0001 = Byte
			|||             0010 = Word
			|||             0011 = DWord
			|||             0100 = Float
			|||             0101 = Undefined
			|||             0110 = Undefined
			|||             0111 = Undefined
			|||             1000 = Undefined
			|||             1001 = Undefined 
			|||             1010 = Undefined
			|||             1011 = Undefined
			|||             1100 = Undefined
			|||             1101 = Undefined
			|||             1110 = Undefined
			|||             1111 = Undefined
			|||
			|||-- Access:  00 = Read / write
			||             01 = Read Only
			||             10 = Write Only
			||             11 = Undefined
			||
			||-- Kind of memory: 0 = Ram;  1 = Flash
			|
			|-- Protection:  1 = Protected;  0 = Not Protected
			
			
		Example:  
			V-Memory in Koyo boxes (205/405) is General Purpose, WORD format, Read / Write, RAM.
			this would make the type be: 0000 0010 0000 0000 = 0200h = 512
			
			CR Memory in Koyo boxes is General Purpose, Bit format, Read / Write, RAM,
			this would make the type be: 0000 0000 0000 0000 = 0000h = 0
			
			Scratch Pad Memory in Koyo boxes is System, Byte Format, Read / Write, RAM.
			this would make the type be: 0000 0001 0010 0000 = 0120h = 288
			
			SP's in Koyo boxes is System, Bit Format, Read Only, RAM
			this would make the type be: 0001 0000 0010 0000 = 1020h = 4128
			
*/
#define MT_KOYO_V	0x0200		/* V-Memory */
#define MT_KOYO_C	0x0000		/* C-Memory */
#define MT_KOYO_Z   0x0120		/* Scratch Pad Memory */	
#define MT_KOYO_SP	0x1020		/* SP-Memory */

/* Restore structure alignment boundary */
#pragma pack()


#endif /* #if !defined(__HEI_H) */
