// ClrConsoleTest.cpp : main project file.

#include "stdafx.h"
#include "ClrConsoleTest.h"


using namespace System;
using namespace wwio_USB2LPT;

int main(array<System::String ^> ^args)
{
	#define title "Demo library wwio.dll"
	int nLenghtString =  80;

	Console::Clear();
	DateTime thisDate = DateTime::Now;
	Console :: WriteLine ("Dnes\t {0:F}",thisDate);
	Console::ForegroundColor = ConsoleColor::Green;
    Console::WriteLine( title);
	Console::WriteLine( ((String^)"")->PadRight( nLenghtString, '-' ) );
	Console::WriteLine(" ");
	Console::ResetColor();
	DemoKnihovna_Dll(nLenghtString);
	Console::ReadLine();
    return 0;
}

//Demo Knihovny wwio.dll
// 25.08.2013
// hacesoft
// Pavel Prochazka.zde.cz
//
//***********************************************************************************************//

// ErrorKod = 0 -- Vse je ok.
// ErrorKod = 1 -- Paramet range mimo rozsah.
// ErrorKod = 2 -- Pozadovaný port NEEXISTUJE.
// ErrorKod = 3 -- Neplatne oznaceni portu.
// ErrorKod = 4 -- Je indikovana chyba z predchozi cinnosti funkce OpenPort.
// ErrorKod = 5 -- Paramet range mimo rozsah.
// ErrorKod = 6 -- Pozadovaný port NEEXISTUJE.
// ErrorKod = 7 -- Neplatne oznaceni portu.
// ErrorKod = 8 -- Je indikovana chyba z predchozi cinnosti funkce OpenPort.
// ErrorKod = 9 -- Neni provedena funkce UnLock.
//ErrorKod = 10 -- nelze rezervovat pamet pro rozsirujici strukturu USBxLPT potru

//Funkce UnLock vrati v ErrorKod promenne nasledujici parametry:
//ErrorKod = 0 -- Vše je OK.
//ErrorKod = -1 -- Funkce UnLock selhala.
//ErrorKod = 1 -- Nelze najit soubor GiveIO.sys.
//ErrorKod = 3 -- Nejde spustit giveio.sys ovladač.
//ErrorKod = 4 -- Nejde otevrit OpenSCManager. Aplikace potrebuje prava Administratora.
//ErrorKod = 12 -- ERROR_FILE_NOT_FOUND, toto je chyba funkce DriverStart, provedte restart pc a smazte v registrech retezec giveio, provedte DriverStop(); a DriverRemove a znovu Unlock
//ErrorKod = 13 -- ERROR_PATH_NOT_FOUND, toto je chyba funkce DriverStart, provedte restart pc a smazte v registrech retezec giveio, provedte DriverStop(); a DriverRemove a znovu Unlock
//ErrorKod = 183 – Nelze vytvořit soubor, který již existuje. Nejedna se o chybu ale o upozorněni, program muže pokračovat dále.
//ErrorKod = 1072 – Zadané zařízení je určeno na odstranění. Zkuste to znovu.
//ErrorKod = 1073 – Zadaná služba již existuje. Nejedna se o chybu ale o upozorněni, program muže pokračovat dále.
//ErrorKod = 1056 - Instance této služby je již spuštěna. Nejedna se o chybu ale o upozorněni, program muže pokračovat dále.
//ErrorKod = 1060 -- Nejde nainstalovat giveio.sys ovladač.


void DemoKnihovna_Dll(int nLenghtString){
	#define lpt1 "Lpt1"
	#define lpt2 "LPT2"
	#define com1 "CoM1"
	#define USB_ "USB_LPT"
	#define LenghtsBuffer 20
	
	CONFIG_PORT Config_Port;
	int rnt = 0;
	int Data = 0;
	int adresa = 0;
	int k = 0;

	EXISTING_DRIVER ExistingDriver;
	RETURN_CODE ReturnCode;
	Wwio_RwData WwioRwData; 

	rnt = Existing_Driver(&ExistingDriver);
	WriteConsoleExistingDriver(rnt, ExistingDriver, nLenghtString);
	rnt = Error_();
	WriteConsoleWwio(rnt,TEXT("Error_"), nLenghtString);

	Config_Port.range = 0;
	Config_Port.port = lpt1;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	rnt = Error_();
	WriteConsoleWwio(rnt,TEXT("Error_"), nLenghtString);

	Config_Port.range = 0;
	Config_Port.port = USB_;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	rnt = Error_();
	WriteConsoleWwio(rnt,TEXT("Error_"), nLenghtString);

	Config_Port.range = 1;
	Config_Port.port = USB_;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	Config_Port.range = 1;
	Config_Port.port = lpt1;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	Config_Port.range = 3;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	Config_Port.range = 1;
	Config_Port.port = "CoMs";
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);


	Config_Port.port = "x";
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	Config_Port.range = 0;
	Config_Port.port = USB_;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);


	Config_Port.range = 0;
	Config_Port.port = lpt1;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);
//-----------------------------------------------------------------------------------------------
	Data = 111;//6f +1
	WwioRwData.Data = Data; 
	rnt = Write_Status(&WwioRwData);
	WriteConsoleWrite(Data,rnt, WwioRwData, nLenghtString);

	rnt = Read_Status(&WwioRwData);
	WriteConsoleRead(rnt, WwioRwData, nLenghtString);

	Data = 204; //dd + 2
	WwioRwData.Data = Data; //dd + 2
	rnt = Write_Control(&WwioRwData);
	WriteConsoleWrite(Data,rnt, WwioRwData, nLenghtString);

	rnt = Read_Control(&WwioRwData);
	WriteConsoleRead(rnt, WwioRwData, nLenghtString);

	Data = 0;
    WwioRwData.Data = Data;
    adresa = Write_Data(&WwioRwData);
	WriteConsoleWrite(Data,rnt, WwioRwData, nLenghtString);

	rnt = Read_Data(&WwioRwData);
	WriteConsoleRead(rnt, WwioRwData, nLenghtString);

	Data = 255;
	WwioRwData.Data = Data;
    adresa = Write_Data(&WwioRwData);
	WriteConsoleWrite(Data,rnt, WwioRwData, nLenghtString);

	rnt = Read_Data(&WwioRwData);
	WriteConsoleRead(rnt, WwioRwData, nLenghtString);
//-------------------------------------------------------------------------------------------------
	Config_Port.range = 0;
	Config_Port.port = lpt2;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	Config_Port.port = com1;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);

	rnt = Lock_(&ReturnCode);
	WriteConsoleWwio(rnt, TEXT("Lock_(&ReturnCode)"), nLenghtString);

	rnt = Lock_();
	WriteConsoleWwio(rnt, TEXT("Lock_"), nLenghtString);

	Config_Port.port = com1;
	rnt = OpenPort(&Config_Port);
	WriteConsoleOpenPort(rnt, Config_Port, nLenghtString);
//-------------------------------------------------------------------------------------------------
	Data = 0;
	WwioRwData.Data = Data;
    adresa = Write_Data(&WwioRwData);
	WriteConsoleWrite(Data,rnt, WwioRwData, nLenghtString);

	rnt = Read_Data(&WwioRwData);
	WriteConsoleRead(rnt, WwioRwData, nLenghtString);
}

void WriteConsoleWwio(int nRnt,String ^ sString ,int nLenghtString = 24){
	Console::ResetColor();
	Console::WriteLine("Starting Function -> "+ sString);
	Console::ForegroundColor = ConsoleColor::Red;
	Console::WriteLine("rnt -> \t{0:G}", nRnt);
	Console::ResetColor();
	Console::WriteLine( ((String^)"")->PadRight( nLenghtString, '-' ) );
}
void WriteConsoleOpenPort(int nRnt,CONFIG_PORT Config_Port ,int nLenghtString = 24){
	Console::ResetColor();
	System::String ^sBuffer = gcnew System::String(Config_Port.port);
	Console::WriteLine("Starting Function -> OpenPort: "+ sBuffer);
	Console::ForegroundColor = ConsoleColor::Red;
	Console::WriteLine("rnt -> \t{0:G}", nRnt);
	Console::ForegroundColor = ConsoleColor::Yellow;
	Console::WriteLine("Config_Port.range -> \t\t{0:G}", Config_Port.range);
	Console::WriteLine("Config_Port.adress -> \t\t0x{0:X}", Config_Port.adress);
	Console::WriteLine("Config_Port.DMA -> \t\t{0:G}", Config_Port.DMA);
	Console::WriteLine("Config_Port.IRQ -> \t\t{0:G}", Config_Port.IRQ);
	Console::WriteLine("Config_Port.Type_Port -> \t{0:G}", Config_Port.Type_Port);
	if (Config_Port.s_USB_LPT != NULL){
		Console::ForegroundColor = ConsoleColor::Magenta;
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"NamePort: \t\t"+ gcnew System::String(Config_Port.s_USB_LPT->NamePort));
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"LengthNamePort: \t0x{0:X}", Config_Port.s_USB_LPT->LengthNamePort);
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"DeviceDesc: \t\t"+ gcnew System::String(Config_Port.s_USB_LPT->DeviceDesc));
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"LengthDeviceDesc: \t0x{0:X}", Config_Port.s_USB_LPT->LengthDeviceDesc);
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"ServiceName: \t\t"+ gcnew System::String(Config_Port.s_USB_LPT->Service));
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"LengthService: \t\t0x{0:X}", Config_Port.s_USB_LPT->LengthService);
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"Mfg: \t\t"+ gcnew System::String(Config_Port.s_USB_LPT->Mfg));
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"LengthMfg: \t\t0x{0:X}", Config_Port.s_USB_LPT->LengthMfg);
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"HardwareID: \t\t"+ gcnew System::String(Config_Port.s_USB_LPT->HardwareID));
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"LengthHardwareID: \t0x{0:X}", Config_Port.s_USB_LPT->LengthHardwareID);
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"ClassGUID: \t\t"+ gcnew System::String(Config_Port.s_USB_LPT->ClassGUID));
		Console::WriteLine("Config_Port.s_USB_LPT -> \t"+"LengthClassGUID: \t0x{0:X}", Config_Port.s_USB_LPT->LengthClassGUID);
	}
	Console::ResetColor();
	Console::WriteLine( ((String^)"")->PadRight( nLenghtString, '-' ) );
}

void WriteConsoleWrite(int Data,int nRnt, Wwio_RwData WwioRwData,int nLenghtString = 24){
	Console::ResetColor();
	Console::WriteLine("Starting Function -> Write Data: ");
	Console::ForegroundColor = ConsoleColor::Red;
	Console::WriteLine("rnt -> \t{0:G}", nRnt);
	Console::ForegroundColor = ConsoleColor::Cyan;
	Console::WriteLine("Zadaná hodnota -> \t\t0x{0:X}", Data);
	Console::ForegroundColor = ConsoleColor::Yellow;
	if (WwioRwData.Data == Ha_ERROR){Console::WriteLine("Uložená hodnota -> \t\t{0:G}", (int)WwioRwData.Data);}
	else {Console::WriteLine("Uložená hodnota -> \t\t0x{0:X}", (int)WwioRwData.Data);}
	if (WwioRwData.Adress == Ha_ERROR){Console::WriteLine("Adresa uložení  -> \t\t{0:G}", (int)WwioRwData.Adress);}
	else {Console::WriteLine("Adresa uložení  -> \t\t0x{0:X}", (int)WwioRwData.Adress);}
	Console::ResetColor();
	Console::WriteLine( ((String^)"")->PadRight( nLenghtString, '-' ) );
}

void WriteConsoleRead(int nRnt,Wwio_RwData WwioRwData,int nLenghtString = 24){
	Console::ResetColor();
	Console::WriteLine("Starting Function -> Read Data: ");
	Console::ForegroundColor = ConsoleColor::Red;
	Console::WriteLine("rnt -> \t{0:G}", nRnt);
	Console::ForegroundColor = ConsoleColor::Yellow;
	if (WwioRwData.Data == Ha_ERROR){Console::WriteLine("Z adresy uložení přečteno -> \t{0:G}", (int)WwioRwData.Data);}
	else {Console::WriteLine("Z adresy uložení přečteno -> \t0x{0:X}", (int)WwioRwData.Data);}
	if (WwioRwData.Adress == Ha_ERROR){Console::WriteLine("Adresa přečtení  -> \t\t{0:G}", (int)WwioRwData.Adress);}
	else {Console::WriteLine("Adresa přečtení  -> \t\t0x{0:X}", (int)WwioRwData.Adress);}
	Console::ResetColor();
	Console::WriteLine( ((String^)"")->PadRight( nLenghtString, '-' ) );
}

void WriteConsoleExistingDriver(int nRnt,EXISTING_DRIVER ExistingDriver,int nLenghtString = 24){
	Console::ResetColor();
	Console::WriteLine("Starting Function -> Existing_Driver: ");
	Console::ForegroundColor = ConsoleColor::Red;
	Console::WriteLine("rnt -> \t{0:G}", nRnt);
	Console::ForegroundColor = ConsoleColor::Yellow;
	Console::WriteLine("ExistingDrive.DriverInstall -> \t0x{0:X}", ExistingDriver.DriverInstall);
	Console::WriteLine("ExistingDrive.DriverStart  -> \t0x{0:X}", ExistingDriver.DriverStart);
	Console::ResetColor();
	Console::WriteLine( ((String^)"")->PadRight( nLenghtString, '-' ) );
}
