// EBCIO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "IOBase.h"

#pragma warning(disable:4103)

#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_IPX;
	DWORD ID = 0;
	char Name[256] = "";
	int QueryType = QT_ID;
	int Error = 0;

	printf("EBCIO - Copyright(C) 2001, Host Engineering, Inc.\n");
	printf("Executes IO transaction on specified Hx-EBC device\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 'N':
				case 'n':
					QueryType = QT_NAME;
					memset(Name, 0, 256);
					strcpy(Name, argv[Parm]+2);
					break;

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

				case 'h':
				case 'H':
				case '?':
					printf("Usage - EtherIO [Options]\n");
					printf("Options:\n");
					printf("  /Pn  Set protocol\n");
					printf("       n = I for IP\n");
					printf("           X for IPX\n");
					printf("  /Nv  Connect to device named v\n");
					printf("  /Iv  Connect to device number v\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_WINSOCK; // Under Win32 this is the only valid transport
	TP.Protocol = Protocol;
	if(!HEIOpenTransport(&TP, HEIAPIVERSION, 0))
		{
		/*
		** Step 3, resolve the logical address to a device
		** This uses a data query to find all matching devices. There may
		** be more than 1 match.
		*/
		DeviceCount = MAX_DEVICES;

		switch(QueryType)
			{
			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)
			{
			/*
			** Step 4, open the device. We'll use only the first device and
			** ignore the others.
			*/
			if(!HEIOpenDevice(&TP, &aDevices[0], HEIAPIVERSION, 25, 3, FALSE))
				{
				/*
				** Step 5, confirm that it is an Hx-EBC. A more correct way to do this
				** would be to read the module caps (HEIReadSupportInfo) and test 
				** for the functions we wish to use; I'm lazy.
				*/
				DeviceDef DD;
				memset(&DD, 0, sizeof(DD));
				if(!HEIReadDeviceDef(&aDevices[0], (BYTE *)&DD, sizeof(DD)))
					{
					if(DD.ModuleType != MT_EBC)
						{
						if(QueryType == QT_ID)
							printf("Module number %u is not an Hx-EBC. Cannot continue...\n", ID);
						else
							printf("Module %s is not an Hx-EBC. Cannot continue...\n", Name);
						}
					else
						{
						/*
						** Step 6, having confirmed this to be an IO device, we can now create the
						** IO base class and talk to this baby.
						*/
						CEbcIO IOB(&aDevices[0], TRUE);
		
						// To read the IO...
						IOB.ReadIO();

						// To read points with slot:type:point addressing
						// Slot 1, discrete input 0 and 1
						BOOL I00 = IOB.GetDI(1, 0);
						BOOL I01 = IOB.GetDI(1, 1);

						// To read points with type:offset addressing
						BOOL X2 = IOB.GetX(2);
						BOOL X3 = IOB.GetX(3);

						// To write points with slot:type:point addressing
						// Slot 3, discrete output 1 and 2
						IOB.SetDO(3, 1, I00 /* From input */);
						IOB.SetDO(3, 1, I00 /* From input */);

						// To write points with type:offset addressing
						IOB.SetY(3, X2);
						IOB.SetY(4, X3);

						// After updating  CEbcIO's copy it must be written to the EBC...
						IOB.WriteIO();

						/*
						** NOTE: Writing to the EBC returns the input state so an explicit
						** read isn't really needed. IOW, call WriteIO at the bottom of
						** each scan and you're done...
						*/
						}
					}
				else
					{
					printf("Error! Unable to read device def.\n");
					}
				
				/*
				** Step 8, close the device
				*/
				HEICloseDevice(&aDevices[0]);
				}
			else
				{
				printf("Error! Unable to open device %02X%02X%02X%02X%02X%02X\n", 
					aDevices[0].ENetAddress[0], aDevices[0].ENetAddress[1], aDevices[0].ENetAddress[2], 
					aDevices[0].ENetAddress[3], aDevices[0].ENetAddress[4], aDevices[0].ENetAddress[5]);
				}
			}
		else
			{
			printf("Sorry! No ethernet devices found\n");
			}

		/*
		** Step 9, terminate the transport
		*/
		HEICloseTransport(&TP);
		}
	else
		{
		printf("Error! Unable to initialize Transport\n");
		}

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