//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma hdrstop

#ifndef linux
#include "..\..\core\api.h"
#else
#include "../../core/api.h"
#endif // linux
//---------------------------------------------------------------------------



int DoInfo(int argc, char* argv[]);
int DoHelp(int argc, char* argv[]);
int DoBKLControl(int argc, char* argv[]);
int DoGetMac(int argc, char* argv[]);
int DoReadGPIO(int argc, char* argv[]);
int DoSetGPIODir(int argc, char* argv[]);
int DoSetClrGPIO(int argc, char* argv[]);       
int DoReadBoardHeader(int argc, char* argv[]);       

int DoReadMonitor(int argc, char* argv[]);       
int DoSetFanSpeed(int argc, char* argv[]);       
int DoFanSpeedCruiseMode(int argc, char* argv[]);       
int DoThermalCruiseMode(int argc, char* argv[]);       

int DoEnableWD(int argc, char* argv[]);       
int DoDisableWD(int argc, char* argv[]);       
int DoSetWDTimer(int argc, char* argv[]);       

int DoGetIntruderStatus(int argc, char* argv[]);       
int DoClrIntruderStatus(int argc, char* argv[]);       



typedef struct
{
	char * Cmd;
	char * Usage;
	int (*Execute)(int argc, char* argv[]);
}tCommands;

tCommands  Commands[]=
{
	{"-?","This text",&DoHelp},
	{"Info","Display info from KTAPI",&DoInfo},
	{"SetBKLcontrol","<Freq> <Duty> : Backlight Control PWM",&DoBKLControl},
	{"GetMac","<Index> : Get MAC addresse",&DoGetMac},
	{"ReadGPIO","<GPIO Mask> : Read GPIO",&DoReadGPIO},
	{"SetGPIODir","<GPIO Mask> : Setgpio direction ",&DoSetGPIODir},
	{"SetClrGPIO","<GPIO Mask> <0/1>: Setgpio direction ",&DoSetClrGPIO},
	{"ReadBoardHeader",": Display Integrated Info Header",&DoReadBoardHeader},
	{"ReadMonitor",": Display Monitor Info",&DoReadMonitor},
	{"SetFanSpeed","<Value(0-127)> : Set Fan Speed ",&DoSetFanSpeed},
	{"FanSpeedCruiseMode","<Speed> : ",&DoFanSpeedCruiseMode},
	{"ThermalCruiseMode","<Temp> : ",&DoThermalCruiseMode},
	       
	{"EnableWD",": ",&DoEnableWD},
	{"DisableWD",": ",&DoDisableWD},
	{"SetWDTimer",": ",&DoSetWDTimer},

	{"GetIntruderStatus",": ",&DoGetIntruderStatus},
	{"ClrIntruderStatus",": ",&DoClrIntruderStatus},

	
};

void __cdecl DebugOut(char *Text)
{
	printf("%s",Text);
}

int main(int argc, char* argv[])
{
int i;
int Status;
	
	Proc_DebugOut=DebugOut;
	Status=1;
	if (KT_API_Open(sfDISK))
	{

		Status=0;
		
		if (argc >=2)
		{
			for(i=0;i<(sizeof(Commands)/sizeof(tCommands));i++)
			{
				if (stricmp(Commands[i].Cmd,argv[1])==0)
				{
					if ((Status=Commands[i].Execute(argc,argv))==2)
					{
						printf("Usage %s %s\n",Commands[i].Cmd,Commands[i].Usage);
					}
					break;
				}
			}
			if (i==(sizeof(Commands)/sizeof(tCommands)))
			{
				printf("Unknown command\n");
			}
		}
		else
		{
			printf("API Test Ex V0.01\n-? for usage\n");
		}
		

		KT_API_Close();
	}


	return Status;
}
//---------------------------------------------------------------------------
int DoHelp(int argc, char* argv[])
{
int i;

	printf("Usage :\n");
	for(i=0;i<(sizeof(Commands)/sizeof(tCommands));i++)
	{
		printf("%-20s %s\n",Commands[i].Cmd,Commands[i].Usage);
	}

	return 0;
}
//---------------------------------------------------------------------------

int DoInfo(int argc, char* argv[])
{
	printf("KTAPI Verion %d.%03d.%d.%d (%s)",KT_API->VersionMajor,KT_API->VersionMinor,KT_API->Build,KT_API->Type,OS_String);

	return 0;	
}


//---------------------------------------------------------------------------
int DoBKLControl(int argc, char* argv[])
{
unsigned int Freq;
unsigned int Duty;
int Status;
tSetBKLControl SetBKLControl;
	
	if (argc!=4)
	{
		return 2;
	}
		
	Freq = strtoul(argv[2],NULL,10);
	Duty = strtoul(argv[3],NULL,10);
	
	Status=1;

	printf("Freq %d Duty %d\n",Freq,Duty);
	
	if ( KT_API_GetFunction("SetBKLControl",&SetBKLControl))
	{
		printf("Calling SetBKLControl\n");
		if (SetBKLControl(Freq,Duty)==_API_se_OK)
		{
			Status=0;
			printf("Return OK\n");
		}
		else
		{
			printf("Return Error\n");
		}
	}
	else
	{
		printf("Error locating SetBKLControl\n");
	}

	return Status;	
}
//---------------------------------------------------------------------------
int DoGetMac(int argc, char* argv[])
{
int Status;
int i;
int nMac;
unsigned char Mac[6];
tGetMac GetMac;
	
	if (argc!=3)
	{
		return 2;
	}
		
	nMac = strtoul(argv[2],NULL,10);
	
	Status=1;

	if ( KT_API_GetFunction("GetMac",&GetMac))
	{
	
		Status=0;
		
		if (GetMac(nMac,Mac)==_API_se_OK)
		{
			printf("MAC : ");
			for(i=0;i<6;i++)
				printf("%02X ",Mac[i]);
			printf("\n");				
			
		}			
	}

	return Status;	
}
//---------------------------------------------------------------------------

int DoReadGPIO(int argc, char* argv[])
{
int Status;
int GPIO_Mask;
tReadGPIO ReadGPIO;
unsigned char Data;
int i;


	
	if (argc!=3)
	{
		return 2;
	}
		
	GPIO_Mask = strtoul(argv[2],NULL,16);
	
	Status=1;

	if ( KT_API_GetFunction("ReadGPIO",&ReadGPIO))
	{
	
		if (ReadGPIO(GPIO_Mask,&Data)==_API_se_OK)
		{
			Status=0;
			
			printf("GPIO : ");
			
			for(i=0;i<8;i++)
				printf("%d ",((Data>>(7-i))&0x01));
				
			printf("\n");
			
		}
			
	}

	return Status;	
}
//---------------------------------------------------------------------------
int DoSetGPIODir(int argc, char* argv[])
{
int Status;
int GPIO_Mask;
tSetGPIODir SetGPIODir;
	
	if (argc!=3)
	{
		return 2;
	}
		
	GPIO_Mask = strtoul(argv[2],NULL,16);
	
	Status=1;

	if ( KT_API_GetFunction("SetGPIODir",&SetGPIODir))
	{
	
		if (SetGPIODir(GPIO_Mask)==_API_se_OK)
		{
			Status=0;
		}
			
	}

	return Status;	
}
//---------------------------------------------------------------------------
int DoSetClrGPIO(int argc, char* argv[])
{
int Status;
int GPIO_Mask;
tSetClrGPIO SetClrGPIO;
unsigned char Val;

	
	if (argc!=4)
	{
		return 2;
	}
		
	GPIO_Mask = strtoul(argv[2],NULL,16);
	Val = strtoul(argv[3],NULL,16);
	
	Status=1;

	if ( KT_API_GetFunction("SetClrGPIO",&SetClrGPIO))
	{
	
		if (SetClrGPIO(GPIO_Mask,Val)==_API_se_OK)
		{
			Status=0;
		}
			
	}

	return Status;	
}
//---------------------------------------------------------------------------

int DoReadBoardHeader(int argc, char* argv[])
{
int Status;   
TIntegratedInfoHeader IIH;
tReadBoardHeader ReadBoardHeader;
tGetBoardName GetBoardName;
char *Board;
char * Name;
	
	if (argc!=2)
	{
		return 2;
	}
	Status=1;
    
	if ( KT_API_GetFunction("ReadBoardHeader",&ReadBoardHeader))
	{
	
		if (ReadBoardHeader(&IIH)==_API_se_OK)
		{
			Status=0;
			printf("BIOS Version %d,%d\n",IIH.BIOSmjr,IIH.BIOSmnr);
			printf("S/N %02x%02x%02x%02x\n",IIH.S_N[0],IIH.S_N[1],IIH.S_N[2],IIH.S_N[3]);
			printf("P/N %02x%02x%02x%02x\n",IIH.P_N[0],IIH.P_N[1],IIH.P_N[2],IIH.P_N[3]);
	
			/*
			switch(IIH.Boardinfo)
			{
			case 40:
				Board="886LCD-M/Flex";
				break;
			case 48:
				Board="886LCD/mITX";
				break;
			case 50:
				Board="886LCD-M/ATX";
				break;
			case 60:
				Board="786LCD/mITX";
				break;
			case 90:
				Board="986LCD-M/mITX";
				break;
			case 91:
				Board="986LCD-M/Flex";
				break;
			case 92:
				Board="986LCD-M/ATXP";
				break;
			case 93:
				Board="986LCD-M/ATXE";
				break;
			case 94:
				Board="KT965/Flex";
				break;
			case 95:
				Board="KT965/ATXE";
				break;
			case 96:
				Board="KT965/ATXP";
				break;
			case 98:
				Board="KT690/mITX";
				break;
			default:
				Board="N/A";
				break;
			}*/
	
			printf("Board : %d",IIH.Boardinfo);



			if ( KT_API_GetFunction("GetBoardName",&GetBoardName))
			{	
				Name="N/A";                 
				GetBoardName(&Name);
			
				printf(" - %s",Name);		
			}
	
			printf("\n");


			
		}
	    


	}           
	return Status;
}
//---------------------------------------------------------------------------
int DoReadMonitor(int argc, char* argv[])
{
int Status;   
tReadMonitor ReadMonitor;   
HWMON HardwareMonitor;

	
	if (argc!=2)
	{
		return 2;
	}        
	
	Status=1;
    
	if ( KT_API_GetFunction("ReadMonitor",&ReadMonitor))
	{
	
		if (ReadMonitor(&HardwareMonitor)==_API_se_OK)
		{    
			Status=0;

			printf("VCORE\t: %8.3f\n",HardwareMonitor.VCORE);
			printf("VCOREb\t: %8.3f\n",HardwareMonitor.VCOREb);
			printf("VCC2_5\t: %8.3f\n",HardwareMonitor.VCC2_5);
			printf("VCC3\t: %8.3f\n",HardwareMonitor.VCC3);
			printf("VCC5\t: %8.3f\n",HardwareMonitor.VCC5);
			printf("V12\t: %8.3f\n",HardwareMonitor.V12);
			printf("SB3\t: %8.3f\n",HardwareMonitor.SB3);
			printf("V -12\t: %8.3f\n",HardwareMonitor.V_12);
			printf("SB5\t: %8.3f\n",HardwareMonitor.SB5);
			printf("VBATT\t: %8.3f\n",HardwareMonitor.VBATT);
			printf("CPUTemp\t: %8.3f\n",HardwareMonitor.CPUTemp);
			printf("FAN\t: %8.3f\n",HardwareMonitor.FAN);
			
		}
	}   
	
	return Status;
	
}
//---------------------------------------------------------------------------

int DoSetFanSpeed(int argc, char* argv[])
{       
int Status;   
tSetFanSpeed SetFanSpeed;   
tSetFanMode SetFanMode;   
unsigned int Speed;

	
	if (argc!=3)
	{
		return 2;
	}        
	
	Status=1;
               
	Speed = strtoul(argv[2],NULL,10);

               

	if (( KT_API_GetFunction("SetFanSpeed",&SetFanSpeed))&&
	    ( KT_API_GetFunction("SetFanMode",&SetFanMode)) )
	{
	
		if (SetFanMode(Manual_Mode)==_API_se_OK)
		{    
			if (SetFanSpeed(Speed)==_API_se_OK)
			{    
				Status=0;
			}		
		}

	}
	
	return Status;
}                                 
//---------------------------------------------------------------------------


int DoFanSpeedCruiseMode(int argc, char* argv[])
{
int Status;   
tSetFanMode SetFanMode;   
tSetFanTarget SetFanTarget;   
unsigned int Speed;

	
	if (argc!=3)
	{
		return 2;
	}        
	
	Status=1;
               
	Speed = strtoul(argv[2],NULL,10);
               
	if (( KT_API_GetFunction("SetFanTarget",&SetFanTarget)) &&
		( KT_API_GetFunction("SetFanMode",&SetFanMode)) )
	{

		if (SetFanMode(Fan_Speed_Cruise_Mode)==_API_se_OK)
		{    
			if (SetFanTarget(Speed)==_API_se_OK)
			{    
				Status=0;
				
			}
		
		}
	
	}
	
	return Status;
}
//---------------------------------------------------------------------------
int DoThermalCruiseMode(int argc, char* argv[])
{
int Status;   
tSetFanMode SetFanMode;   
tSetFanTarget SetFanTarget;  
tStartThermalCruise StartThermalCruise; 
unsigned int Temp;

	
	if (argc!=3)
	{
		return 2;
	}        
	
	Status=1;
               
	Temp = strtoul(argv[2],NULL,10);
               
	if (( KT_API_GetFunction("SetFanTarget",&SetFanTarget)) &&
		( KT_API_GetFunction("SetFanMode",&SetFanMode))  &&
		( KT_API_GetFunction("StartThermalCruise",&StartThermalCruise)) )
	{

		if (StartThermalCruise()==_API_se_OK)
		{
			if (SetFanMode(Thermal_Cruise_Mode)==_API_se_OK)
			{    
				if (SetFanTarget(Temp)==_API_se_OK)
				{    
					Status=0;     
					
				}
				else
					printf("Error : SetFanTarget\n");
			
			} 
			else
				printf("Error : SetFanMode\n");
		}
		else
			printf("Error : StartThermalCruise\n");
	}
	
	return Status;
}
//---------------------------------------------------------------------------


int DoEnableWD(int argc, char* argv[])
{
int Status;   
tEnableWD EnableWD;   

	
	if (argc!=2)
	{
		return 2;
	}        
	
	Status=1;
    
	if ( KT_API_GetFunction("EnableWD",&EnableWD))
	{
	
		if (EnableWD()==_API_se_OK)
		{             
			Status=0;
			                
		}
	}
	return Status;
	
}
//---------------------------------------------------------------------------
int DoDisableWD(int argc, char* argv[])
{
int Status;   
tDisableWD DisableWD;   

	
	if (argc!=2)
	{
		return 2;
	}        
	
	Status=1;
    
	if ( KT_API_GetFunction("DisableWD",&DisableWD))
	{
	
		if (DisableWD()==_API_se_OK)
		{    
			Status=0;
		}
	}
	return Status;
	
}
//---------------------------------------------------------------------------
int DoSetWDTimer(int argc, char* argv[])
{       
int Status;   
tSetWDTimerInterval SetWDTimerInterval;   
tSetWDTimer SetWDTimer;  

unsigned int Interval;
unsigned int WD_Time;

	
	if (argc!=4)
	{
		return 2;
	}        
	
	Status=1;
               
	Interval = strtoul(argv[2],NULL,10);
	WD_Time = strtoul(argv[3],NULL,10);
               
	if (( KT_API_GetFunction("SetWDTimer",&SetWDTimer))&&
		( KT_API_GetFunction("SetWDTimerInterval",&SetWDTimerInterval)))
	{
	
		if (SetWDTimerInterval(Interval)==_API_se_OK)
		{    
			if (SetWDTimer(WD_Time)==_API_se_OK)
			{    
				Status=0;
				
			}
		}
	}
	
	return Status;
}                                 
       
//---------------------------------------------------------------------------

int DoGetIntruderStatus(int argc, char* argv[])
{
unsigned int Status;
tGetIntruderStatus GetIntruderStatus;

	if ( KT_API_GetFunction("GetIntruderStatus",&GetIntruderStatus))
	{
	
		if (GetIntruderStatus(&Status)==_API_se_OK)
		{    
			if (Status==0)
			{
				printf("All OK\n");
			}
			else
			{
				printf("Intruder / Case open detectede\n");
			}
				
			
		}
	}

	return (int)Status;
}

int DoClrIntruderStatus(int argc, char* argv[])
{
	
tClrIntruderStatus ClrIntruderStatus;
	
	if ( KT_API_GetFunction("ClrIntruderStatus",&ClrIntruderStatus))
	{
	
		if (ClrIntruderStatus()==_API_se_OK)
		{    
			printf("Intruder / Case open cleared\n");
		}
	}
	return 0;
}
