#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int				BOOL;
typedef unsigned char	BYTE;
typedef unsigned short	WORD;
typedef unsigned long	DWORD;

#define FALSE 0
#define TRUE 1

#pragma warning(disable:4103)

#include "\ethersdk\include\hei.h"

#define MAX_DEVICES	100
#define QT_NORMAL	0
#define QT_ID		1
#define QT_NAME		2

void main(int argc, char *argv[])
{
	printf("EtherQuery - Copyright(C) 1997-2001, Host Engineering, Inc.\n");
	printf("Queries for AD.com Ethernet products and displays configuration information\n\n");

	/*
	** Parse command line
	*/
	WORD	Protocol=	HEIP_IPX;
	DWORD	ID=			0;
	char	Name[256];
	int		QueryType = QT_NORMAL;

	for( int Parm= 1; Parm < argc; Parm++ )
	{
		if( argv[Parm][0]== '/' || argv[Parm][0]== '-' )
		{
			switch( argv[Parm][1] )
			{
				case 'P':
				case 'p':
					switch( argv[Parm][2] )
					{
						case 'I':
						case 'i':
							Protocol= HEIP_IP;
							break;

						case 'X':
						case 'x':
							Protocol= HEIP_IPX;
							break;

						default:
							printf( "%s - Unknown parameter\n", argv[Parm] );
							break;
					}
					break;

				case 'D':
				case 'd':
					switch( argv[Parm][2] )
					{
						case 'I':
						case 'i':
							QueryType = QT_ID;
							ID = atoi( argv[Parm] + 3 );
							break;

						case 'N':
						case 'n':
							QueryType = QT_NAME;
							memset( Name, 0, sizeof(Name) );
							strcpy( Name, argv[Parm] + 3 );
							break;

						default:
							printf( "%s - Unknown parameter\n", argv[Parm] );
							break;
					}
					break;

				case 'h':
				case 'H':
				case '?':
					printf("Usage - EQ [options]\n");
					printf("Options:\n");
					printf("  /Pn  Set protocol\n");
					printf("       n = I for IP\n");
					printf("           X for IPX\n");
					printf("  /Dtv Use data query\n");
					printf("       t = I for ID,   v is 0 -> 4294967295\n");
					printf("           N for name, v is name string\n");
					printf("  /?   This screen\n");
					return;

				default:
					printf("%s - Unknown parameter\n", argv[Parm]);
					break;
			}
		}
	}


/*
** Step 1, initialize the driver, called once at startup
*/
	HEIOpen( HEIAPIVERSION );

/*
** Step 2, initialize the transport, Under Win32, Winsock is the only valid transport
*/
	HEITransport TP;
	memset( &TP, 0, sizeof(TP) );

	TP.Transport=	HEIT_WINSOCK;
	TP.Protocol=	Protocol;

	int rc;
	if( !( rc= HEIOpenTransport( &TP, HEIAPIVERSION, 0 ) ) )
	{
		if( Protocol== HEIP_IP )
			printf( "Winsock Open, using UDP/IP\n" );
		else
			printf( "Winsock Open, using IPX\n" );

/*
** Step 3, perform the query
*/
		WORD	  DeviceCount= MAX_DEVICES;
		HEIDevice aDevices[MAX_DEVICES];
		memset( aDevices, 0, sizeof(aDevices) );

		switch( QueryType )
		{
			case QT_NORMAL:
				if( (rc= HEIQueryDevices( &TP, aDevices, &DeviceCount, HEIAPIVERSION ) )!= 0 )
				{
					printf("Error %04X trying to query for devices\n", rc);
				}
				break;

			case QT_ID:
				if( (rc= HEIQueryDeviceData( &TP, aDevices, &DeviceCount, HEIAPIVERSION, DT_NODE_NUMBER, (BYTE *)&ID, 4 ) )!= 0 )
				{
					printf("Error %04X trying to query for node number\n", rc);
				}
				break;

			case QT_NAME:
				if( (rc= HEIQueryDeviceData( &TP, aDevices, &DeviceCount, HEIAPIVERSION, DT_NODE_NAME, (BYTE *)Name, 256 ) )!= 0 )
				{
					printf("Error %04X trying to query for node name\n", rc);
				}
				break;
		}

		// DeviceCount now contains number of devices found
		//
		if( DeviceCount > 0 )
		{
			printf("Found %d device(s)\n\n", DeviceCount);

			for( int DeviceNum= 0; DeviceNum < DeviceCount; DeviceNum++ )
			{
/*
** Step 4, open the device
*/
				if( !( rc= HEIOpenDevice( &TP, &aDevices[DeviceNum], HEIAPIVERSION, DefDevTimeout, DefDevRetrys, FALSE ) ) )
				{
/*
** Step 5, interact with the device
*/
					/////////////////////////////////////////////////////////////////////
					// display the type of device
					//
					DeviceDef DD;
					memset(&DD, 0, sizeof(DD));

					char DeviceType[16];
					if( ( rc= HEIReadDeviceDef( &aDevices[DeviceNum], (BYTE *)&DD, sizeof(DD) ) )== 0 )
					{
						switch( DD.PLCFamily )
						{	
							case 1:
								// HA- devices
								strcpy( DeviceType, "HA-");
								break;

							case 2:
								// H2- devices
								strcpy( DeviceType, "H2-");
								break;

							case 3:
								// H3- devices
								strcpy( DeviceType, "H3-");
								break;

							case 4:
								// H4- devices
								strcpy( DeviceType, "H4-");
								break;

							case 10:
								// T1H- devices
								strcpy( DeviceType, "T1H-");
								break;

							default:
								// You should never get here
								strcpy( DeviceType, "??-");
								break;
						}
						
						switch( DD.ModuleType )
						{
							case 0:
								// Ethernet Base Controller
								strcat( DeviceType, "EBC");
								
								if( DD.MediaType ) strcat( DeviceType, "-F");
								break;

							case 1:
								// Ethernet Communication Module
								strcat( DeviceType, "ECOM");
								
								if( DD.MediaType ) strcat( DeviceType, "-F");
								break;

							case 2:
								// WinPLC
								strcat( DeviceType, "WPLC");
								break;

							case 3:
								// Ethernet Drive Interface
								strcat( DeviceType, "EDRV2");
								break;

							case 4:
								// Ethernet Remote Master
								strcat( DeviceType, "ERM");
								
								if( DD.MediaType ) strcat( DeviceType, "-F");
								break;

							case 6:
								// EZ-Touch Panel Ethernet Adapter Card
								strcat( DeviceType, "EZTOUCH");
								break;

							default:
								// You should never get here
								strcat( DeviceType, "???????");
								break;
						}
					
					}else
					{
						// bad read
						printf("Error %04X trying to read the Device Definition\n", rc);
					}
					

					/////////////////////////////////////////////////////////////////////
					// get the Module's IP Address
					// 
					BYTE IP[4];
					WORD Size= sizeof(IP);
					if( (rc= HEIReadSetupData(&aDevices[DeviceNum], DT_IP_ADDRESS, IP, &Size))!= 0)
					{
						// bad read
						printf("Error %04X trying to read the IP Address\n", rc);
					}


					/////////////////////////////////////////////////////////////////////
					// display the device type and addressing information
					//
					printf("Device: %-10s  MAC: %02X %02X %02X %02X %02X %02X    IP Address : %3d.%3d.%3d.%3d\n", 
							DeviceType,
							aDevices[DeviceNum].ENetAddress[0], 
							aDevices[DeviceNum].ENetAddress[1], 
							aDevices[DeviceNum].ENetAddress[2], 
							aDevices[DeviceNum].ENetAddress[3], 
							aDevices[DeviceNum].ENetAddress[4], 
							aDevices[DeviceNum].ENetAddress[5],
							IP[0], 
							IP[1], 
							IP[2], 
							IP[3]
						   );

					
					/////////////////////////////////////////////////////////////////////
					// get the module ID
					//
					DWORD	ModuleID;
					Size=	sizeof(ModuleID);
					if( (rc= HEIReadSetupData(&aDevices[DeviceNum], DT_NODE_NUMBER, (BYTE *)&ModuleID, &Size))!= 0)
					{
						// bad read
						printf("Error %04X trying to read the Module ID\n", rc);
					
					}

					/////////////////////////////////////////////////////////////////////
					// get the Node number
					//
					char	Name[256];
					Size=	sizeof(Name);
					if( ( rc= HEIReadSetupData(&aDevices[DeviceNum], DT_NODE_NAME, (BYTE *)Name, &Size))== 0)
					{
						// only display the first 20 characters of the Name
						//
						if(strlen(Name) > 20)
						{
							Name[17] = Name[18] = Name[19] = '.';
							Name[20] = 0;
						}
					
					}else
					{
						// bad read
						printf("Error %04X trying to read Module Name\n", rc);
					}
					
					/////////////////////////////////////////////////////////////////////
					// get the Module's Description
					//
					char	Description[256];
					Size=	sizeof(Description);
					if( (rc= HEIReadSetupData(&aDevices[DeviceNum], DT_DESCRIPTION, (BYTE *)Description, &Size))== 0)
					{
						// only display the first 20 characters of the Description
						//
						if(strlen(Description) > 20)
						{
							Description[17] = Description[18] = Description[19] = '.';
							Description[20] = 0;
						}

					}else
					{
						// bad read
						//
						printf("Error %04X trying to read the Modules Description\n", rc);
					}


					/////////////////////////////////////////////////////////////////////
					// display the remaining user defined information
					//
					printf("    ID: %-10u Name: %-20s Description: %-20s\n\n", ModuleID, Name, Description );
					
					
/*
** Step 6, close this device
*/
					if( ( rc= HEICloseDevice(&aDevices[DeviceNum] ) )!=0 )
					{
						printf( "Error %4X trying to close this device\n", rc );
					}
	
				}else
				{
					printf("Error %04X trying to open device %02X %02X %02X %02X %02X %02X\n", 
							rc,
							aDevices[DeviceNum].ENetAddress[0], 
							aDevices[DeviceNum].ENetAddress[1], 
							aDevices[DeviceNum].ENetAddress[2], 
							aDevices[DeviceNum].ENetAddress[3], 
							aDevices[DeviceNum].ENetAddress[4], 
							aDevices[DeviceNum].ENetAddress[5]
						  );
				}
			}
		
		}else
		{
			printf("Sorry! No ethernet devices found\n");
		}

/*
** Step 7, close the transport, called once at shutdown
*/
		if( (rc= HEICloseTransport(&TP) )!= 0)
		{
			printf("Error %04X closing the Winsock Transport\n", rc);
		}
	
	}else
	{
		printf("Error %04X trying to initialize Winsock Transport\n", rc );
	}

/*
** Step 8, terminate the driver, called once at shutdown
*/
	HEIClose();
}