#include "base/define.h"
#include "stdlib.h"
#include "hardware/gpio_cfg.h"
#include "base/delay.h"
#include "hardware/adc_cfg.h"
#include "hardware/power.h"
#include "base/utility.h"
//#include "usartapp.h"
//#include "database.h"
#include "driver/XTDriver.h"
#include "interface/BaseChecker.h"
#include "interface/XTChecker.h"
#include "elec_io.h"

//volatile uint16_t jq_adc_test_buf[2048];
static void XTCheckerTest(int argc, char**argv)
{
	uint32_t h_power,m_power;
	uint16_t us_array[20];
	uint8_t* puc_buf;
	uint16_t us_temp;
	rt_memset(&checker_runcfg.params,0,sizeof(checker_runcfg.params));
	rt_memset(&checker_runcfg.Task_Result,0,sizeof(checker_runcfg.Task_Result));
	checker_runcfg.excue_rtv = 0;
	checker_runcfg.param_count = 0;
	checker_runcfg.rtv_count = 0;
	checker_runcfg.rtv_index = 0;
	checker_runcfg.task_info.runindex = 0;
	
	if(argc > 2)
	{
		checker_runcfg.param_count = argc-2;
		us_temp = 0;
		while(us_temp < checker_runcfg.param_count)
		{
			checker_runcfg.params[us_temp] = atoi(argv[us_temp+2]);
			us_temp++;
		}
	}
	if (argc < 2)
	{
		 goto CH_help_label_;
	}else if(!rt_strcmp(argv[1], "ON")){
		XTBUS_ON
	}else if(!rt_strcmp(argv[1], "OFF")){
		XTBUS_OFF
	}
	else if(!rt_strcmp(argv[1], "H")){
			XTBUS_W_1
		
	}else if(!rt_strcmp(argv[1], "L")){
			XTBUS_W_0
	}else if(!rt_strcmp(argv[1], "CURRENT")){
			rt_kprintf("Bus Cur %d 0.1uA \n",Power_GetCurrent());
	
	}else if(!rt_strcmp(argv[1], "PowerPrapare")){

		Checker_PowerPrapare();
		rt_kprintf("PowerPrapare  Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "PowerOn")){
		checker_runcfg.rtv_count = 2;
		XT_Test_PowerOn();		
		rt_kprintf("PowerOn Excute %d BusV %d Energy %d \n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0],checker_runcfg.Test_Rtv[1] );
	}else if(!rt_strcmp(argv[1], "SetBusV")){
		checker_runcfg.rtv_count = 1;
		XT_Test_SetBusV();		
		rt_kprintf("SetBusV Excute %d  BusV %d \n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0] );
	}else if(!rt_strcmp(argv[1], "BaseCur")){
		checker_runcfg.rtv_count = 2;
		XT_Test_BaseCur();		
		rt_kprintf("BaseCur Excute %d  BusCur1 %d  BusCur2 %d \n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0],checker_runcfg.Test_Rtv[1] );
	}	
	else if(!rt_strcmp(argv[1], "ScanUID")){
		checker_runcfg.rtv_count = (checker_runcfg.params[0]+8+1)/2;
		XT_Test_ScanUID();		
		rt_kprintf("ScanUID excute %d \t",checker_runcfg.Task_Result[0]);
		us_temp = 0;
		while(us_temp < checker_runcfg.rtv_count)
		{
			printf("%02x ",(checker_runcfg.Test_Rtv[us_temp]) & 0x0FF);
			printf("%02x ",(checker_runcfg.Test_Rtv[us_temp]>>8) & 0x0FF);
			us_temp++;
		}
		printf("\n");
	}else if(!rt_strcmp(argv[1], "WriteChipCfg")){
		XT_Test_WriteChipCfg();		
		printf("WriteChipCfg Excute %d  \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "CheckChipCfg")){
		XT_Test_CheckChipCfg();		
		printf("CheckChipCfg Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "BindTestCode")){
		XT_Test_BindTestCode();		
		printf("BindTestCode Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "ChgEnergy")){
		checker_runcfg.rtv_count = 4;
		XT_Test_ChgEnergy();		
		printf("ReadState Excute %d  Energy_0.1ms %d ",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
		printf("EndCur_0.1uA %d ",checker_runcfg.Test_Rtv[1]);
		printf("MaxCur_0.1mA %d ",checker_runcfg.Test_Rtv[2]);
		printf("ShakeADV %d \n",checker_runcfg.Test_Rtv[3]);
	}	else if(!rt_strcmp(argv[1], "WriteRunField")){
		XT_Test_WriteRunField();		
		printf("WriteRunField Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "CheckRunField")){
		checker_runcfg.rtv_count = 4;
		XT_Test_CheckRunField();		
		printf("ReadState Excute %d  MaxCur %d   MaxTime %d MinCur %d  MinTime %d\n",checker_runcfg.Task_Result[0],\
		checker_runcfg.Test_Rtv[0],checker_runcfg.Test_Rtv[1],checker_runcfg.Test_Rtv[2],checker_runcfg.Test_Rtv[3]);
	}else if(!rt_strcmp(argv[1], "Resister")){
		checker_runcfg.rtv_count = 1;
		XT_Test_Resister();		
		printf("Resister Excute %d R_adc %d \n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
	}else if(!rt_strcmp(argv[1], "Cap")){
		checker_runcfg.rtv_count = 1;
		XT_Test_Cap();		
		printf("Cap Excute %d  Cap %d 0.1uf\n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
	}else if(!rt_strcmp(argv[1], "WaitDelay")){
		Checker_WaitDelay();		
		printf("Cap Excute %d  C\n",checker_runcfg.Task_Result[0]);
	}
	else if(!rt_strcmp(argv[1], "WriteShell")){
		XT_Test_WriteShell();		
		printf("WriteShell Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "WriteUID")){
		XT_Test_WriteUID();		
		printf("WriteUID Excute %d  \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "WritePWD")){
		XT_Test_WritePWD();		
		printf("WritePWD Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "CheckBackFlag")){
		XT_Test_CheckBackFlag();		
		printf("CheckBackFlag Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "ReadBackFlag")){
		checker_runcfg.rtv_count = 2;
		XT_Test_ReadBackFlag();		
		printf("Discharge Excute %d  BackBytes %04X %04X\n ",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0],checker_runcfg.Test_Rtv[1]);
	}else if(!rt_strcmp(argv[1], "Trim")){
		XT_Test_Trim();		
		printf("Trim Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "Discharge")){
		XT_Test_Discharge();		
		printf("Discharge Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "EnCommEndCur")){
		XT_Test_EnCommEndCur();		
		printf("EnCommEndCur Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "GetCommEndCur")){
		checker_runcfg.rtv_count = 1;
		XT_Test_GetCommEndCur();		
		printf("GetCommEndCur Excute %d  Cur_ADC %d ",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
			
	}else if(!rt_strcmp(argv[1], "OneLineCheck")){
		XT_Test_OneLineCheck();		
		printf("OneLineCheck Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "CheckState")){
		checker_runcfg.rtv_count = 1;
		XT_Test_CheckState();		
		printf("CheckState Excute %d  CommCur %d\n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
	}else if(!rt_strcmp(argv[1], "Boom")){
		checker_runcfg.rtv_count = 2;
		XT_Test_Boom();		
		printf("Boom Excute %d  delay %d 0.1ms net_id %d \n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[1],checker_runcfg.Test_Rtv[0]);
	}else if(!rt_strcmp(argv[1], "Reset")){
		XT_Test_Reset();		
		printf("Reset Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "PowerOFF")){
		XT_Test_PowerOFF();
		printf("PowerOFF Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "LockCmdC")){
		XT_Test_LockCmdC();
		printf("LockCmdC Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "CodeBindEn")){
		XT_Test_CodeBindEn();
		printf("LockCmdC Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "SetWriteBuf")){
		Checker_FacBuf[0] = strlen(argv[2]);
		memcpy(Checker_FacBuf+2,argv[2],Checker_FacBuf[0]);
		Checker_FacBuf[1] = CheckCRC_8(Checker_FacBuf+2,Checker_FacBuf[0]);
		printf("SetWriteBuf Excute 0 \n");
	}else if(!rt_strcmp(argv[1], "SetWriteBufHex")){
		HexStrings2Byte(argv+2,argc-2,Checker_FacBuf+2,48);
		Checker_FacBuf[0] = argc-2;
		Checker_FacBuf[1] = CheckCRC_8(Checker_FacBuf+2,Checker_FacBuf[0]);
		printf("SetWriteBufHex Excute 0 \n");
	}else if(!rt_strcmp(argv[1], "InputCheck")){
		checker_runcfg.rtv_count = 1;
		XT_Test_InputCheck();
		printf("InputCheck Excute %d  BusVoltage %d \n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
	}else if(!rt_strcmp(argv[1], "CheckPWD")){
		XT_Test_CheckPWD();
		printf("CheckPWD Excute %d \n",checker_runcfg.Task_Result[0]);
	
	}else if(!rt_strcmp(argv[1], "UID_PWD_Bind")){
		
		printf("UID_PWD_Bind Excute %d \n",XT_UID_PWD_Bind());
	}else if(!rt_strcmp(argv[1], "ResistorSample")){
		checker_runcfg.rtv_count = 1;
		Checker_ResistorSample();
		printf("ResistorSample Excute %d Res(0.01) %d\n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
	}
	else if(!rt_strcmp(argv[1], "LoadChipCfg")){
		checker_runcfg.rtv_count = 4;
		XT_Test_LoadChipCfg();
		printf("LoadChipCfg Excute %d UID_LEN %d PWD_LEN %d  CUR_CLASS %d Ver %d\n",\
		checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0],checker_runcfg.Test_Rtv[1],checker_runcfg.Test_Rtv[3],checker_runcfg.Test_Rtv[2]);
	}
	else if(!rt_strcmp(argv[1], "CapVoltage")){
		checker_runcfg.rtv_count = 1;
		XT_Test_CapVoltage();
		printf("CheckPWD Excute %d Res(0.01) %d\n",checker_runcfg.Task_Result[0],checker_runcfg.Test_Rtv[0]);
	}else if(!rt_strcmp(argv[1], "BindUIDPWD")){
		
		memcpy(us_array,argv[2],strlen(argv[2]));
		UidCode_YM2JQ(checker_runcfg.writeuid,(uint8_t*)us_array);
		
		m_power = GetPasswordByUidCode((uint8_t*)us_array);
		memcpy(checker_runcfg.writepwd,(uint8_t*)&m_power ,4);
		
		checker_runcfg.pwd_len = 4;
		checker_runcfg.uid_len = 8;
		XT_Test_UID_PWD_Bind();
		printf("BindUIDPWD Excute %d \n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "VerifyUIDPWD")){
		XT_Test_UID_PWD_Verify();
		printf("VerifyUIDPWD Excute %d \n",checker_runcfg.Task_Result[0]);
	}
	
	
	else if(!rt_strcmp(argv[1], "SetHardVer")){
		HexStrings2Byte(argv+2,argc-2,(uint8_t*)(&boardinfo_un.boardinfo.hard_v),2);
		SaveBoardInfo();
		printf("SetHardVer  %04x\n",boardinfo_un.boardinfo.hard_v);
	}else if(!rt_strcmp(argv[1], "SetRsisDiff")){
		boardinfo_un.boardinfo.resistor_diff = checker_runcfg.params[0];
		SaveBoardInfo();
		printf("SetRsisDiff  %d\n",boardinfo_un.boardinfo.resistor_diff);
	}else if(!rt_strcmp(argv[1], "ReadBoard")){
		LoadBoardInfo();
		printf("BootFlag %08x HardVersion %02x RsistorDiff %d\n PlanID " ,\
		boardinfo_un.boardinfo.bootflag,boardinfo_un.boardinfo.hard_v,boardinfo_un.boardinfo.resistor_diff);
	}else if(!rt_strcmp(argv[1], "ExcuePlan")){
		Ye_RunPlanCheckTest();
	}else if(!rt_strcmp(argv[1], "CurMonitor")){
		XT_CurMonitor();
		printf("CurMonitor Excute %d\n",checker_runcfg.Task_Result[0]);
	}else if(!rt_strcmp(argv[1], "VerfyShell")){
		checker_runcfg.rtv_count = 7;
		XT_Test_VerfyShell();
		printf("VerfyShell Excute %d Shell Data \n",checker_runcfg.Task_Result[0]);
		us_temp = 0;
		while(us_temp < 7)
		{
			printf("%02x ",(checker_runcfg.Test_Rtv[us_temp]) & 0x0FF);
			printf("%02x ",(checker_runcfg.Test_Rtv[us_temp]>>8) & 0x0FF);
			if(us_temp == 4)
			{
				printf("\n");
			}
			us_temp++;
		}
		printf("\n");
	}
	
	else{
		CH_help_label_:
		rt_kprintf("CH             [ON | OFF | H | L ] \n");
		rt_kprintf("PowerPrapare   [V1,V2,V3...V10] \n");
		rt_kprintf("PowerOn        [Voltage] [TimeOut] [End_ADC_V] \n");
		rt_kprintf("SetBusV        [Voltage] \n");
		rt_kprintf("BaseCur        \n");
		rt_kprintf("ScanUID        [UID_LEN] [E_UID_Equal_0]\n");
		rt_kprintf("WriteChipCfg   [UID_LEN] [PWD_LEN] [FreeBack_Class] [Ver] \n");
		rt_kprintf("CheckChipCfg   [UID_LEN] [PWD_LEN] [FreeBack_Class] [Ver] \n");
		rt_kprintf("BindTestCode   \n");
		rt_kprintf("ChgEnergy      [Vol_Class] [End_ADV] [End_Cur_0.1] [Wait_Timeout] [Chg_Hold]\n");
		rt_kprintf("WriteRunField  [Net_ID] [Delay] [Local_Num]\n");
		rt_kprintf("CheckRunField  [Net_ID] [Delay] [Local_Num] [En_Freebakc_0]\n");
		rt_kprintf("Resister       \n");
		rt_kprintf("Cap   				 [Chg_Class][Measure_Time]\n");
		rt_kprintf("WriteShell     [Buf_Bg]  [Len]  \n");
		rt_kprintf("WaitDelay      [Delay_0.1S] [En_Monitor] \n");
		rt_kprintf("WriteUID       [Buf_Bg]  [Len]  \n");
		rt_kprintf("WritePWD       [Buf_Bg]  [Len]\n");
		rt_kprintf("CheckBackFlag  [EN_1Checke_0Write] [2BytesMask] [2BytesData]\n");
		rt_kprintf("ReadBackFlag   \n");
		rt_kprintf("Trim        	 [Trim_Mode][Cycle][Count]\n");
		rt_kprintf("Discharge   	 \n");
		rt_kprintf("EnCommEndCur   \n");
		rt_kprintf("GetCommEndCur  \n");
		rt_kprintf("OneLineCheck   [NetID] [MaxNetId]\n");
		rt_kprintf("CheckState		 [StateMask]  \n");
		rt_kprintf("Boom       		 [Monitor_time]\n");
		rt_kprintf("Reset           \n");
		rt_kprintf("PowerOFF        \n");
		rt_kprintf("LockCmdC        \n");
		rt_kprintf("CodeBindEn      \n");
		rt_kprintf("InputCheck      [BusVoltage][TimeOut_1ms]\n");
		rt_kprintf("CheckPWD 				\n");
		rt_kprintf("SetHardVer     [HardVersion 2Bytes] \n");
		rt_kprintf("SetRsisDiff    [RsistorDiff *100] \n");
		rt_kprintf("ReadBoardInfo  \n");
		rt_kprintf("SetWriteBuf    [HexData...]\n");
		rt_kprintf("ResistorSample [Test_Modle]\n");
		rt_kprintf("CapVoltage 		 [WaitTime 0.1S]\n");
		rt_kprintf("BindUIDPWD   	 [uid_str] \n");
		rt_kprintf("VerifyUIDPWD   \n");
		rt_kprintf("OnlineScan     [BusV][TimeOUt][UIDLen] \n");
		rt_kprintf("VerfyShell     [buf_bg][len] \n");
		rt_kprintf("ExcuePlan  \n");
		
		
	}
	
}
MSH_CMD_EXPORT_ALIAS(XTCheckerTest ,XCH,  XT CHecker );


