/*
 * Module_ConfigTools.c
 *
 *  Created on: 2020�~6��22��
 *      Author: foluswen
 */

#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include 	<sys/types.h>
#include 	<sys/ioctl.h>
#include 	<sys/socket.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<linux/wireless.h>
#include 	<arpa/inet.h>
#include 	<netinet/in.h>
#include	<dirent.h>

#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <unistd.h>
#include    <fcntl.h>
#include    <termios.h>
#include    <errno.h>
#include 	<errno.h>
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include	<stdbool.h>
#include	<stddef.h>
#include	<stdint.h>
#include 	"define.h"
#include 	"main.h"

//=================================
// System basic sample constant
//=================================
#define ARRAY_SIZE(A)					(sizeof(A) / sizeof(A[0]))
#define PASS							1
#define FAIL							-1
#define YES								1
#define NO								0
#define ON								1
#define OFF								0
#define MtdBlockSize 					0x300000

struct SysConfigAndInfo			*ShmSysConfigAndInfo;
struct StatusCodeData			*ShmStatusCodeData;
struct OCPP16Data 				*ShmOCPP16Data;
struct Charger					*ShmCharger;

int StoreLogMsg(const char *fmt, ...)
{
	char Buf[4096+256];
	char buffer[4096];
	time_t CurrentTime;
	struct tm *tm;
	struct timeval tv;
	va_list args;

	va_start(args, fmt);
	int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
	va_end(args);

	memset(Buf,0,sizeof(Buf));
	CurrentTime = time(NULL);
	tm=localtime(&CurrentTime);
	gettimeofday(&tv, NULL); // get microseconds, 10^-6

	sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]ConfigToolsLog",
				tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
				buffer,
				tm->tm_year+1900,tm->tm_mon+1);


#ifdef SystemLogMessage
	system(Buf);
#endif

#ifdef ConsloePrintLog
	printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
#endif

	return rc;
}

int runShellCmd(const char*cmd)
{
	int result = FAIL;
	char buf[256];
	FILE *fp;

	fp = popen(cmd, "r");
	if(fp != NULL)
	{
		while(fgets(buf, sizeof(buf), fp) != NULL)
		{
			DEBUG_INFO("%s\n", buf);
		}

		result = PASS;
	}
	pclose(fp);

	return result;
}

int StoreUsrConfigData(struct SysConfigData *UsrData)
{
	int result = PASS;
	int fd,wrd;
	unsigned int i,Chk;
	unsigned char *ptr, *BufTmp;

	Chk=0;
	ptr=(unsigned char *)UsrData;
	if((BufTmp=malloc(MtdBlockSize))!=NULL)
	{
		memset(BufTmp,0,MtdBlockSize);
		memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
		for(i=ARRAY_SIZE(UsrData->CsuBootLoadFwRev);i<MtdBlockSize-4;i++)
			Chk+=*(BufTmp+i);
		memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);

		// Output configuration to file.
		fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
		if (fd < 0)
		{
			DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");

			free(BufTmp);
			return 0;
		}
		wrd=write(fd, BufTmp, MtdBlockSize);
		close(fd);
		if(wrd<MtdBlockSize)
		{
			DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");

			free(BufTmp);
			return 0;
		}
		DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");


		DEBUG_INFO("Erase /dev/mtd10.\n");
		runShellCmd("flash_erase /dev/mtd10 0 0");
		DEBUG_INFO("Write /dev/mtd10.\n");
		runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");

		DEBUG_INFO("Erase /dev/mtd11.\n");
		runShellCmd("flash_erase /dev/mtd11 0 0");
		DEBUG_INFO("Write /dev/mtd11.\n");
		runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");


		system("rm -f /mnt/EvseConfig.bin");
		DEBUG_INFO("EvseConfig write to flash OK\n");
	}
	else
	{
		DEBUG_ERROR("alloc BlockSize NG\r\n");
    		result = FAIL;
	}

	if(BufTmp!=NULL)
		free(BufTmp);

	return result;
}

int InitShareMemory()
{
	int result = PASS;
	int MeterSMId;

	//Initial ShmSysConfigAndInfo
	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
	{
		DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
		result = FAIL;
	}
	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
	{
		DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
		result = FAIL;
	}
	else
	{}

	//Initial ShmStatusCodeData
	if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
	{
		DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
		result = FAIL;
	}
	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
	{
		DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
		result = FAIL;
	}
	else
	{}

	//Initial ShmOCPP16Data
   	if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
	{
		DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
		result = FAIL;
	}
	else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
	{
		DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
		result = FAIL;
	}
	else
	{}

	//Initial ShmCharger
   	if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
    {
   		DEBUG_ERROR("shmget ShmCharger NG\n");
   		result = FAIL;
	}
    else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
    {
    	DEBUG_ERROR("shmat ShmCharger NG\n");
    	result = FAIL;
   	}
    else
    {}

   	return result;
}

int main(void)
{
	char cmd[128];

	if(InitShareMemory() == FAIL)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("InitShareMemory NG\n");
		#endif
		if(ShmStatusCodeData!=NULL)
		{
			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
		}
		sleep(5);
		return 0;
	}
	else
	{
		DEBUG_INFO("InitShareMemory OK.\n");
	}


	for(;;)
	{
		system("clear");
		memset(cmd, 0x00, ARRAY_SIZE(cmd));
		printf("\n ===== main menu ==================================");
		printf("\n  system: system configuration menu.");
		printf("\n  ocpp: ocpp configuration menu.");
		printf("\n  network: netwok configuration menu.");
		printf("\n  test: charger start/stop test.");
		printf("\n  upgrade: trigger firmware upgrade.");
		printf("\n  save: Save config.");
		printf("\n  exit: Exit config tools.");
		printf("\n ==================================================");
		printf("\n  Please input item name to config: ");
		scanf("%s", &cmd[0]);


		if(strcmp(cmd, "system") == 0)
		{
			memset(cmd, 0x00, ARRAY_SIZE(cmd));
			printf("\n ***** system configuration menu ******************");
			printf("\n  modelname: EVSE model name.");
			printf("\n  serialnumber: EVSE serial number.");
			printf("\n  authentication: Authentication function.");
			printf("\n  authbyevccid: Authorize by EVCCID.");
			printf("\n  rfidendian: RFID read endian.");
			printf("\n **************************************************");
			printf("\n  Please input operation item: ");
			scanf("%s", &cmd[0]);

			if(strcmp(cmd, "modelname") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** modelname **********************************");
				printf("\n  Current model name: %s", ShmSysConfigAndInfo->SysConfig.ModelName);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new model name.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n Please input model name: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.ModelName[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.ModelName[0], (char*)&cmd[0]);
					DEBUG_INFO("Input model name: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
				}
			}
			else if(strcmp(cmd, "serialnumber") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** serialnumber *******************************");
				printf("\n  Current serial number: %s", ShmSysConfigAndInfo->SysConfig.SerialNumber);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new serial number.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n Please input serial number: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.SerialNumber[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.SerialNumber[0], (char*)&cmd[0]);
					DEBUG_INFO("Input serial number: %s\n", ShmSysConfigAndInfo->SysConfig.SerialNumber);
				}
			}
			else if(strcmp(cmd, "authentication") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** authentication *****************************");
				printf("\n  Current mode: %d", ShmSysConfigAndInfo->SysConfig.AuthorisationMode);
				printf("\n  0: Enable.");
				printf("\n  1: Disable.");
				printf("\n **************************************************");
				printf("\n  Please input authentication mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.AuthorisationMode = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd):0);
				if(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
					DEBUG_INFO("Authentication: Disable\n");
				else
					DEBUG_INFO("Authentication: Enable\n");
			}
			else if(strcmp(cmd, "authbyevccid") == 0)
                        {
                                memset(cmd, 0x00, ARRAY_SIZE(cmd));
                                printf("\n ***** authbyevccid *******************************");
                                printf("\n  Current mode: %d", ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID);
                                printf("\n  0: Disable.");
                                printf("\n  1: Enable.");
                                printf("\n **************************************************");
                                printf("\n  Please input authorize by EVCCID configuration value: ");
                                scanf("%s", &cmd[0]);

                                ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd):0);
                                if(!ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID)
                                        DEBUG_INFO("Authotize by EVCCID: Disable\n");
                                else
                                        DEBUG_INFO("Authorize by EVCCID: Enable\n");
                        }
			else if(strcmp(cmd, "rfidendian") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** rfidendian *********************************");
				printf("\n  Current mode: %d", ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian);
				printf("\n  0: Little endian.");
				printf("\n  1: Big endian.");
				printf("\n **************************************************");
				printf("\n  Please input rfid endian mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd):0);
				if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
					DEBUG_INFO("Authentication: Little endian\n");
				else
					DEBUG_INFO("Authentication: Big endian\n");
			}
		}
		else if(strcmp(cmd, "ocpp") == 0)
		{
			memset(cmd, 0x00, ARRAY_SIZE(cmd));
			printf("\n ***** ocpp ***************************************");
			printf("\n  ocppurl: OCPP backend server url.");
			printf("\n  ocppsecurity: OCPP backend server security profile.");
			printf("\n  mtserverurl: Maintain backend server url.");
			printf("\n  cboxid: Charger box id.");
			printf("\n  vender: Charger point vender.");
			printf("\n  offlinepolicy: Charger off line policy.");
			printf("\n  localloadbalance: Charger local load balance.");
			printf("\n  maintainurl: Maintain server url.");
			printf("\n  maintainsecurity: Maintain server security profile.");
			printf("\n **************************************************");
			printf("\n  Please input operation item: ");
			scanf("%s", &cmd[0]);

			if(strcmp(cmd, "ocppurl") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** ocppurl ************************************");
				printf("\n  Current OCPP url: %s", ShmSysConfigAndInfo->SysConfig.OcppServerURL);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new ocpp url.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n Please input ocpp url: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.OcppServerURL[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.OcppServerURL[0], (char*)&cmd[0]);
					DEBUG_INFO("Input ocpp url: %s\n", ShmSysConfigAndInfo->SysConfig.OcppServerURL);
				}
			}
			if(strcmp(cmd, "mtserverurl") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** mtserverurl *********************************");
				printf("\n  Current maintain server url: %s", ShmSysConfigAndInfo->SysConfig.MaintainServerURL);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new ocpp url.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n Please input maintain server url: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.MaintainServerURL[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.MaintainServerURL));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL[0], (char*)&cmd[0]);
					DEBUG_INFO("Input maintain server url: %s\n", ShmSysConfigAndInfo->SysConfig.MaintainServerURL);
				}
			}
			else if(strcmp(cmd, "ocppsecurity") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** ocppsecurity *******************************");
				printf("\n  Current OCPP security profile: %d", ShmSysConfigAndInfo->SysConfig.OcppSecurityProfile);
				printf("\n  0: Profile-0.");
				printf("\n  1: Profile-1.");
				printf("\n  2: Profile-2.");
				printf("\n  3: Profile-3.");
				printf("\n **************************************************");
				printf("\n  Please input OCPP security profile: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.OcppSecurityProfile = ((0<=atoi(cmd))&&(atoi(cmd)<=3)&&(atoi(cmd)!=1)?atoi(cmd):0);

				DEBUG_INFO("OCPP security profile: %d.\n");
			}
			else if(strcmp(cmd, "cboxid") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** cboxid *************************************");
				printf("\n  Current OCPP charger box id: %s", ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new charger box id.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input OCPP charger box id: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.ChargeBoxId[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.ChargeBoxId[0], (char*)&cmd[0]);
					DEBUG_INFO("Input ocpp charger box id: %s\n", ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
				}
			}
			else if(strcmp(cmd, "vender") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** vender *************************************");
				printf("\n  Current OCPP vender: %s", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new charger box id.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input OCPP vender: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.chargePointVendor[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.chargePointVendor));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.chargePointVendor[0], (char*)&cmd[0]);
					DEBUG_INFO("Input ocpp vender: %s\n", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
				}
			}
			else if(strcmp(cmd, "offlinepolicy") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** offlinepolicy ******************************");
				printf("\n  Current off line policy: %d", ShmSysConfigAndInfo->SysConfig.OfflinePolicy);
				printf("\n  0: Local list.");
				printf("\n  2: Free charging.");
				printf("\n  3: Deny charging.");
				printf("\n **************************************************");
				printf("\n  Please input off line policy mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.OfflinePolicy = ((0<=atoi(cmd))&&(atoi(cmd)<=3)&&(atoi(cmd)!=1)?atoi(cmd):0);

				switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
				{
					case 0:
						DEBUG_INFO("Off line policy: Local list.\n");
						break;
					case 2:
						DEBUG_INFO("Off line policy: Free charging.\n");
						break;
					case 3:
						DEBUG_INFO("Off line policy: Deny charging.\n");
						break;
				}
			}
			else if(strcmp(cmd, "localloadbalance") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** localloadbalance ***************************");
				printf("\n  Current local loading balance: %d", ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing);
				printf("\n  0: Disable.");
				printf("\n  1: Enable.");
				printf("\n **************************************************");
				printf("\n  Please input local load balance mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd):0);
				if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
					DEBUG_INFO("Local loading balance: Enable\n");
				else
					DEBUG_INFO("Local loading balance: Disable\n");
			}
			else if(strcmp(cmd, "maintainurl") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** maintainurl *********************************");
				printf("\n  Current maintain url: %s", ShmSysConfigAndInfo->SysConfig.MaintainServerURL);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new maintain url.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n Please input maintain url: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.MaintainServerURL[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.MaintainServerURL));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL[0], (char*)&cmd[0]);
					DEBUG_INFO("Input maintain url: %s\n", ShmSysConfigAndInfo->SysConfig.MaintainServerURL);
				}
			}
			else if(strcmp(cmd, "maintainsecurity") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** maintainsecurity ****************************");
				printf("\n  Current maintain security profile: %d", ShmSysConfigAndInfo->SysConfig.MaintainServerSecurityProfile);
				printf("\n  0: Profile-0.");
				printf("\n  1: Profile-1.");
				printf("\n  2: Profile-2.");
				printf("\n  3: Profile-3.");
				printf("\n **************************************************");
				printf("\n  Please input maintain security profile: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.MaintainServerSecurityProfile = ((0<=atoi(cmd))&&(atoi(cmd)<=3)&&(atoi(cmd)!=1)?atoi(cmd):0);

				DEBUG_INFO("Maintain security profile: %d.\n");
			}
		}
		else if(strcmp(cmd, "network") == 0)
		{
			memset(cmd, 0x00, ARRAY_SIZE(cmd));
			printf("\n ***** network *************************************");
			printf("\n  ethdhcp: Ethernet DHCP client.");
			printf("\n  wifimode: WiFi mode.");
			printf("\n  wifidhcp: WiFi DHCP client.");
			printf("\n  wificssid: WiFi client SSID.");
			printf("\n  wificpasswd: WiFi client password.");
			printf("\n  telemode: Telecomm mode.");
			printf("\n  teleapn: Telecomm APN.");
			printf("\n  teleid: Telecomm login id.");
			printf("\n  telepwd: Telecomm login password.");
			printf("\n **************************************************");
			printf("\n  Please input operation item: ");
			scanf("%s", &cmd[0]);

			if(strcmp(cmd, "ethdhcp") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** ethdhcp ************************************");
				printf("\n  Current ethernet dhcp mode: %d", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient^1);
				printf("\n  0: Disable.");
				printf("\n  1: Enable.");
				printf("\n **************************************************");
				printf("\n  Please input dhcp mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
				if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient)
					DEBUG_INFO("Ethernet dhcp client: Disable\n");
				else
					DEBUG_INFO("Ethernet dhcp client: Enable\n");

			}
			else if(strcmp(cmd, "wifimode") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** wifimode ***********************************");
				printf("\n  Current WiFi mode: %d", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
				printf("\n  0: Disable.");
				printf("\n  1: Station.");
				printf("\n  2: Access point.");
				printf("\n **************************************************");
				printf("\n  Please input WiFi mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode = ((0<=atoi(cmd))&&(atoi(cmd)<=2)?atoi(cmd):0);
				switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
				{
					case 0:
						DEBUG_INFO("Wifi mode: Disable.\n");
						break;
					case 1:
						DEBUG_INFO("Wifi mode: Station.\n");
						break;
					case 2:
						DEBUG_INFO("Wifi mode: AP.\n");
						break;
				}
			}
			else if(strcmp(cmd, "wifidhcp") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** wifidhcp ***********************************");
				printf("\n  Current WiFi dhcp client mode: %d", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1);
				printf("\n  0: Disable.");
				printf("\n  1: Enable.");
				printf("\n **************************************************");
				printf("\n  Please input WiFi mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
				if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient)
					DEBUG_INFO("Wifi dhcp client: Disable\n");
				else
					DEBUG_INFO("Wifi dhcp client: Enable\n");
			}
			else if(strcmp(cmd, "wificssid") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** wificssid **********************************");
				printf("\n  Current WiFi client SSID: %s", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new WiFi client SSID.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input WiFi client SSID: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid[0], (char*)&cmd[0]);
					DEBUG_INFO("Wifi client SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
				}
			}
			else if(strcmp(cmd, "wificpasswd") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** wificpasswd ********************************");
				printf("\n  Current WiFi client password: %s", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new WiFi client password.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input WiFi client password: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword[0], (char*)&cmd[0]);
					DEBUG_INFO("Wifi client password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
				}
			}
			else if(strcmp(cmd, "telemode") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** telemode ***********************************");
				printf("\n  0: Disable.");
				printf("\n  1: Enable.");
				printf("\n **************************************************");

				printf("\n  Current telecomm mode: %d", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode^1);
				printf("\n  Please input telecomm mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
				if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode)
					DEBUG_INFO("Wifi dhcp client: Disable\n");
				else
					DEBUG_INFO("Wifi dhcp client: Enable\n");
			}
			else if(strcmp(cmd, "teleapn") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** teleapn ************************************");
				printf("\n  Current telecomm APN: %s", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new telecomm APN.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input telecomm APN: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn[0], (char*)&cmd[0]);
					DEBUG_INFO("Telecomm APN: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
				}
			}
			else if(strcmp(cmd, "teleid") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** teleid *************************************");
				printf("\n  Current telecomm login id: %s", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new telecomm login id.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input telecomm login id: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId[0], (char*)&cmd[0]);
					DEBUG_INFO("Telecomm CHAP id: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
				}
			}
			else if(strcmp(cmd, "telepwd") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n ***** telepwd ************************************");
				printf("\n  Current telecomm login password: %s", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
				printf("\n  0: Keep current config.");
				printf("\n  1: Input new telecomm login password.");
				printf("\n **************************************************");
				printf("\n  Please input operation item: ");
				scanf("%s", &cmd[0]);

				if(atoi(cmd) == 1)
				{
					printf("\n  Please input telecomm login password: ");
					scanf("%s", &cmd[0]);

					memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd));
					strcpy((char*)&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd[0], (char*)&cmd[0]);
					DEBUG_INFO("Telecomm CHAP password: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
				}
			}
		}
		else if(strcmp(cmd, "test") == 0)
		{
			memset(cmd, 0x00, ARRAY_SIZE(cmd));
			printf("\n ***** test menu **********************************");
			printf("\n  start: EVSE start charging request.");
			printf("\n  stop: EVSE stop charging request.");
			printf("\n  auth: Authorize request.");
			printf("\n  ccs: CCS flow status.");
			printf("\n  alarm: Simulate alarm status.");
			printf("\n  cancel: return to main menu.");
			printf("\n **************************************************");
			printf("\n  Please input operation item: ");
			scanf("%s", &cmd[0]);

			if(strcmp(cmd, "start") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n  Please input gun index(1~2): ");
				scanf("%s", &cmd[0]);

				if((0 < atoi(cmd)) && (atoi(cmd) < 3))
				{
					ShmSysConfigAndInfo->SysInfo.AcChargingData[atoi(cmd)-1].schedule.isTriggerStart = ON;
					DEBUG_INFO("Test start gun-%d, start charging session.\n", atoi(cmd));
				}
				else
				{
					printf("\n  Invalid input gun_index.");
				}
			}
			else if(strcmp(cmd, "stop") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n  Please input gun index(1~2): ");
				scanf("%s", &cmd[0]);

				if((0 < atoi(cmd)) && (atoi(cmd) < 3))
				{
					ShmSysConfigAndInfo->SysInfo.AcChargingData[atoi(cmd)-1].schedule.isTriggerStop = ON;
					DEBUG_INFO("Test start gun-%d, stop charging session.\n", atoi(cmd));
				}
				else
				{
					printf("\n  Invalid input gun_index.");
				}
			}
			else if(strcmp(cmd, "auth") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n  Please input idtag: ");
				scanf("%s", &cmd[0]);

				sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%s", cmd);
				ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
				DEBUG_INFO("Test authentication by %s.\n", ShmSysConfigAndInfo->SysConfig.UserId);
			}
			else if(strcmp(cmd, "ccs") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n  Please input gun index(1~2): ");
				scanf("%s", &cmd[0]);

				if((0 < atoi(cmd)) && (atoi(cmd) < 3))
				{
					uint8_t gun_index = atoi(cmd)-1;
					memset(cmd, 0x00, ARRAY_SIZE(cmd));
					printf("\n ***** ccs ****************************************");
					printf("\n  ccs_hs_status: CCS hand shake status.");
					printf("\n  cancel: return to main menu.");
					printf("\n **************************************************");
					printf("\n  Please input operation item: ");
					scanf("%s", &cmd[0]);

					if(strcmp(cmd, "ccs_hs_status") == 0)
					{
						printf("\n ***** ccs_hs_status *******************************");
						printf("\n  Current ccs_hs_status: %d", ShmCharger->gun_info[gun_index].ccsHandshakeState);
						printf("\n  1: HANDSHAKE_DUTY_5.");
						printf("\n  2: HANDSHAKE_DUTY_5_CHECK.");
						printf("\n  3: HANDSHAKE_CCS.");
						printf("\n  4: HANDSHAKE_CP_STATE_E.");
						printf("\n  5: HANDSHAKE_SET_MAX_CURRENT.");
						printf("\n  6: HANDSHAKE_BS_MODE.");
						printf("\n  7: HANDSHAKE_HLC_MODE.");
						printf("\n **************************************************");
						printf("\n  Please input operation item: ");
						scanf("%s", &cmd[0]);

						if((0 < atoi(cmd)) && (atoi(cmd) < 9))
						{
							ShmCharger->gun_info[gun_index].ccsHandshakeState = atoi(cmd);
							DEBUG_INFO("Gun-%d CCS hand shake state: %d.\n", gun_index, ShmCharger->gun_info[gun_index].ccsHandshakeState);
						}
						else
							printf("\n  Invalid hand shake state.");
					}
				}
				else
				{
					printf("\n  Invalid input gun_index.");
				}
			}
			else if(strcmp(cmd, "alarm") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n  Please input gun index(1~2): ");
				scanf("%s", &cmd[0]);

				if((0 < atoi(cmd)) && (atoi(cmd) < 3))
				{
					DEBUG_INFO("Alarm simulate gun-%d, OVP.\n", atoi(cmd));

					while(1)
					{
						ShmCharger->gun_info[atoi(cmd)-1].systemAlarmCode.SystemAlarmCode |= ALARM_L1_OVER_VOLTAGE;
						ShmCharger->gun_info[atoi(cmd)-1].primaryMcuAlarm.InputAlarmCode |= ALARM_L1_OVER_VOLTAGE;
					}
				}
				else
				{
					printf("\n  Invalid input gun_index.");
					sleep(1);
				}
			}
			else if(strcmp(cmd, "cancel") == 0)
			{}
		}
		else if(strcmp(cmd, "upgrade") == 0)
		{
			printf("\n  Firmware upgrade trigger.");

			ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = ON;
			DEBUG_INFO("Trigger firmware upgrade.\n");
			sleep(2);
		}
		else if(strcmp(cmd, "save") == 0)
		{
			if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
			{
				printf("\n  Write configuration fail.\r\n");
			}
			else
			{
				printf("\n  Write configuration OK.\r\n");
				DEBUG_INFO("Save configuration.\n");
			}
			sleep(2);
		}
		else if(strcmp(cmd, "exit") == 0)
		{
			printf("\n  exit program.\n\n");
			DEBUG_INFO("Exit configuration tools.\n");
			return FAIL;
		}
	}



	return -1;
}