/*
 * 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>      /*�зǿ�J��X�w�q*/
#include    <stdlib.h>     /*�зǨ�Ʈw�w�q*/
#include    <unistd.h>     /*Unix �зǨ�Ʃw�q*/
#include    <fcntl.h>      /*�ɱ���w�q*/
#include    <termios.h>    /*PPSIX �׺ݱ���w�q*/
#include    <errno.h>      /*���~���w�q*/
#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"

//=================================
// System basic sample constant
//=================================
#define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)

#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;

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 12");
		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 12");
		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)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
		#endif
		result = FAIL;
	}
	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
		#endif
		result = FAIL;
	}

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

	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.\r\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  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 *************************************************");
			printf("\n  Please input operation item: ");
			scanf("%s", &cmd[0]);

			if(strcmp(cmd, "modelname") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
			else if(strcmp(cmd, "serialnumber") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
		}
		else if(strcmp(cmd, "ocpp") == 0)
		{
			memset(cmd, 0x00, ARRAY_SIZE(cmd));
			printf("\n *************************************************");
			printf("\n  ocppurl: OCPP backend server url.");
			printf("\n  cboxid: Charger box id.");
			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 *************************************************");
				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]);
				}
			}
			else if(strcmp(cmd, "cboxid") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
		}
		else if(strcmp(cmd, "network") == 0)
		{
			memset(cmd, 0x00, ARRAY_SIZE(cmd));
			printf("\n *************************************************");
			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 *************************************************");
				printf("\n  0: Disable.");
				printf("\n  1: Enable.");
				printf("\n *************************************************");

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

				ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
			}
			else if(strcmp(cmd, "wifimode") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				printf("\n  0: Disable.");
				printf("\n  1: Station.");
				printf("\n  2: Access point.");
				printf("\n *************************************************");

				printf("\n  Current WiFi mode: %d", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
				printf("\n  Please input WiFi mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode = ((0<=atoi(cmd))&&(atoi(cmd)<=2)?atoi(cmd):0);
			}
			else if(strcmp(cmd, "wifidhcp") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				printf("\n  0: Disable.");
				printf("\n  1: Enable.");
				printf("\n *************************************************");

				printf("\n  Current WiFi dhcp client mode: %d", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1);
				printf("\n  Please input WiFi mode: ");
				scanf("%s", &cmd[0]);

				ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
			}
			else if(strcmp(cmd, "wificssid") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
			else if(strcmp(cmd, "wificpasswd") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
			else if(strcmp(cmd, "telemode") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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);
			}
			else if(strcmp(cmd, "teleapn") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
			else if(strcmp(cmd, "teleid") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
			else if(strcmp(cmd, "telepwd") == 0)
			{
				memset(cmd, 0x00, ARRAY_SIZE(cmd));
				printf("\n *************************************************");
				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]);
				}
			}
		}
		else if(strcmp(cmd, "upgrade") == 0)
		{
			printf("\n  Firmware upgrade trigger.");

			ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = ON;

			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");

			sleep(2);
		}
		else if(strcmp(cmd, "exit") == 0)
		{
			printf("\n  exit program.\n\n");
			return FAIL;
		}
	}



	return -1;
}