#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 "hei.h"

#define MAX_DEVICES	20

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

void main(int argc, char *argv[])
	{
	WORD Protocol = HEIP_IP;
	DWORD ID = 0;
	char Name[256];
	int QueryType = QT_NORMAL;

	printf("EtherQuery - Copyright(C) 1997, Host Engineering, Inc.\n");
	printf("Queries for PLCDirect Ethernet products and displays configuration information\n\n");

	/*
	** Parse command line
	*/
	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, 256);
							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 to 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;
				}
			}
		}

	/*
	** Initialize required data structures
	*/
	HEITransport TP;
	memset(&TP, 0, sizeof(TP));

	HEIDevice aDevices[MAX_DEVICES];
	memset(aDevices, 0, sizeof(aDevices));

	WORD DeviceCount;
	
	/*
	** Step 1, initialize the driver
	** Called once by app at startup
	*/
	HEIOpen(HEIAPIVERSION);

	/*
	** Step 2, initialize the transport
	*/
	TP.Transport = HEIT_UNIX; // Under Unix this is the only valid transport
	TP.Protocol = Protocol;
	if(!HEIOpenTransport(&TP, HEIAPIVERSION, 0))
		{
		/*
		** Step 3, perform the query
		*/
		DeviceCount = MAX_DEVICES;

		switch(QueryType)
			{
			case QT_NORMAL:
				HEIQueryDevices(&TP, aDevices, &DeviceCount, HEIAPIVERSION);
				break;

			case QT_ID:
				HEIQueryDeviceData(&TP, aDevices, &DeviceCount, HEIAPIVERSION, DT_NODE_NUMBER, (BYTE *)&ID, 4);
				break;

			case QT_NAME:
				HEIQueryDeviceData(&TP, aDevices, &DeviceCount, HEIAPIVERSION, DT_NODE_NAME, (BYTE *)Name, 256);
				break;
			}

		// DeviceCount now contains number of devices found
		if(DeviceCount)
			{
			for(int DeviceNum = 0; DeviceNum < DeviceCount; DeviceNum++)
				{
				/*
				** Step 4, open the device
				*/
				if(!HEIOpenDevice(&TP, &aDevices[DeviceNum], HEIAPIVERSION, 25, 3, FALSE))
					{
					/*
					** Step 5, talk to the device
					*/
					DWORD ModuleID;
					char Name[256];
					char Description[256];
					BYTE IP[4];
					WORD Size = sizeof(ModuleID);
					
					HEIReadSetupData(&aDevices[DeviceNum], DT_NODE_NUMBER, (BYTE *)&ModuleID, &Size);

					Size = sizeof(Name);
					HEIReadSetupData(&aDevices[DeviceNum], DT_NODE_NAME, (BYTE *)Name, &Size);

					Size = sizeof(Description);
					HEIReadSetupData(&aDevices[DeviceNum], DT_DESCRIPTION, (BYTE *)Description, &Size);
					
					Size = sizeof(IP);
					HEIReadSetupData(&aDevices[DeviceNum], DT_IP_ADDRESS, IP, &Size);
					
					DeviceDef DD;
					memset(&DD, 0, sizeof(DD));
					HEIReadDeviceDef(&aDevices[DeviceNum], (BYTE *)&DD, sizeof(DD));

					// Prep for print
					if(strlen(Name) > 10)
						{
						Name[7] = Name[8] = Name[9] = '.';
						Name[10] = 0;
						}

					if(strlen(Description) > 20)
						{
						Description[17] = Description[18] = Description[19] = '.';
						Description[20] = 0;
						}
					
					printf("%02X%02X%02X%02X%02X%02X  Type=H%d-%s  ID=%-4u  Name=%-10s  Desc=%-20s\n", 
						aDevices[DeviceNum].ENetAddress[0], aDevices[DeviceNum].ENetAddress[1], aDevices[DeviceNum].ENetAddress[2], 
						aDevices[DeviceNum].ENetAddress[3], aDevices[DeviceNum].ENetAddress[4], aDevices[DeviceNum].ENetAddress[5],
						DD.PLCFamily, DD.ModuleType ? "ENET" : "EBC ", ModuleID, Name, Description);
					
					/*
					** Step 6, close the device
					*/
					HEICloseDevice(&aDevices[DeviceNum]);
					}
				else
					{
					printf("Error! Unable to open device %02X%02X%02X%02X%02X%02X\n", 
						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, terminate the transport
		*/
		HEICloseTransport(&TP);
		}
	else
		{
		printf("Error! Unable to initialize Transport\n");
		}

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





