Browse Source

2020.02.20 / Alston

Actions:
1. DW30 synchronize from Alston DS60 repository.
2. Apps Makefile add MACRO NAME to argument -D.

Files:
1. As follow commit history

Image version: D0.00.XX.XXXX.XX
Image checksum: XXXXXXXX

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 5 năm trước cách đây
mục cha
commit
070e4353b9
61 tập tin đã thay đổi với 8932 bổ sung6942 xóa
  1. BIN
      EVSE/Modularization/Module_4g
  2. BIN
      EVSE/Modularization/Module_ProduceUtils
  3. BIN
      EVSE/Modularization/Module_Wifi
  4. BIN
      EVSE/Modularization/OcppBackend
  5. BIN
      EVSE/Modularization/WebService
  6. BIN
      EVSE/Modularization/libInfypwr_PsuCommObj.a
  7. BIN
      EVSE/Modularization/libModule_RFID.a
  8. BIN
      EVSE/Modularization/libModule_Upgrade.a
  9. BIN
      EVSE/Modularization/libPhihong_PsuCommObj.a
  10. BIN
      EVSE/Modularization/logPackTools
  11. 34 12
      EVSE/Projects/DW30/Apps/Config.h
  12. BIN
      EVSE/Projects/DW30/Apps/Ev_Comm.o
  13. BIN
      EVSE/Projects/DW30/Apps/FactoryConfig
  14. 239 89
      EVSE/Projects/DW30/Apps/FactoryConfig.c
  15. BIN
      EVSE/Projects/DW30/Apps/FactoryConfig.o
  16. 35 20
      EVSE/Projects/DW30/Apps/Makefile
  17. BIN
      EVSE/Projects/DW30/Apps/Module_EvComm
  18. 1384 1117
      EVSE/Projects/DW30/Apps/Module_EvComm.c
  19. BIN
      EVSE/Projects/DW30/Apps/Module_EvComm.o
  20. BIN
      EVSE/Projects/DW30/Apps/Module_EventLogging
  21. 18 3
      EVSE/Projects/DW30/Apps/Module_EventLogging.c
  22. BIN
      EVSE/Projects/DW30/Apps/Module_EventLogging.o
  23. BIN
      EVSE/Projects/DW30/Apps/Module_InternalComm
  24. 1498 1358
      EVSE/Projects/DW30/Apps/Module_InternalComm.c
  25. BIN
      EVSE/Projects/DW30/Apps/Module_InternalComm.o
  26. 3 2
      EVSE/Projects/DW30/Apps/Module_LcmContro.h
  27. BIN
      EVSE/Projects/DW30/Apps/Module_LcmControl
  28. 77 48
      EVSE/Projects/DW30/Apps/Module_LcmControl.c
  29. BIN
      EVSE/Projects/DW30/Apps/Module_LcmControl.o
  30. BIN
      EVSE/Projects/DW30/Apps/Module_PrimaryComm
  31. 70 12
      EVSE/Projects/DW30/Apps/Module_PrimaryComm.c
  32. BIN
      EVSE/Projects/DW30/Apps/Module_PrimaryComm.o
  33. BIN
      EVSE/Projects/DW30/Apps/Module_PsuComm
  34. 452 300
      EVSE/Projects/DW30/Apps/Module_PsuComm.c
  35. 7 0
      EVSE/Projects/DW30/Apps/Module_PsuComm.h
  36. BIN
      EVSE/Projects/DW30/Apps/Module_PsuComm.o
  37. 35 4
      EVSE/Projects/DW30/Apps/PrimaryComm.c
  38. 3 4
      EVSE/Projects/DW30/Apps/PrimaryComm.h
  39. BIN
      EVSE/Projects/DW30/Apps/PrimaryComm.o
  40. BIN
      EVSE/Projects/DW30/Apps/ReadCmdline
  41. 401 315
      EVSE/Projects/DW30/Apps/ReadCmdline.c
  42. BIN
      EVSE/Projects/DW30/Apps/ReadCmdline.o
  43. 1 0
      EVSE/Projects/DW30/Apps/init.sh
  44. 835 793
      EVSE/Projects/DW30/Apps/internalComm.c
  45. 8 1
      EVSE/Projects/DW30/Apps/internalComm.h
  46. BIN
      EVSE/Projects/DW30/Apps/internalComm.o
  47. 11 7
      EVSE/Projects/DW30/Apps/kill.sh
  48. BIN
      EVSE/Projects/DW30/Apps/main
  49. 3769 2800
      EVSE/Projects/DW30/Apps/main.c
  50. BIN
      EVSE/Projects/DW30/Apps/main.o
  51. 12 11
      EVSE/Projects/DW30/Apps/timeout.h
  52. BIN
      EVSE/Projects/DW30/Apps/timeout.o
  53. BIN
      EVSE/Projects/DW30/Images/FactoryDefaultConfig.bin
  54. BIN
      EVSE/Projects/DW30/Images/ramdisk.gz
  55. 40 46
      EVSE/Projects/define.h
  56. BIN
      EVSE/rootfs/root/Module_4g
  57. BIN
      EVSE/rootfs/root/Module_ProduceUtils
  58. BIN
      EVSE/rootfs/root/Module_Wifi
  59. BIN
      EVSE/rootfs/root/OcppBackend
  60. BIN
      EVSE/rootfs/root/WebService
  61. BIN
      EVSE/rootfs/root/logPackTools

BIN
EVSE/Modularization/Module_4g


BIN
EVSE/Modularization/Module_ProduceUtils


BIN
EVSE/Modularization/Module_Wifi


BIN
EVSE/Modularization/OcppBackend


BIN
EVSE/Modularization/WebService


BIN
EVSE/Modularization/libInfypwr_PsuCommObj.a


BIN
EVSE/Modularization/libModule_RFID.a


BIN
EVSE/Modularization/libModule_Upgrade.a


BIN
EVSE/Modularization/libPhihong_PsuCommObj.a


BIN
EVSE/Modularization/logPackTools


+ 34 - 12
EVSE/Projects/DW30/Apps/Config.h

@@ -4,13 +4,14 @@
  *  Created on: 2019年4月23日
  *      Author: foluswen
  */
-
 #ifndef CONFIG_H_
 #define CONFIG_H_
 
 typedef unsigned char			byte;
 
 #define TOTAL_QUANTITY_GUN			4				//Max Count
+#define DEBUG						0
+
 #define MODE_BOOT					0
 #define MODE_IDLE					1
 #define MODE_AUTHORIZING			2
@@ -23,17 +24,19 @@ typedef unsigned char			byte;
 #define MODE_TERMINATING			9
 #define MODE_COMPLETE				10
 #define MODE_ALARM					11
-#define MODE_RESERVATION			12
-#define MODE_BOOKING				13
-#define MODE_MAINTAIN				14
-#define MODE_DEBUG					15
-#define MODE_CCS_PRECHARGE_STEP0	16 	// ready for ccs precharge processing, For D+ relay to precharge relay
-#define MODE_CCS_PRECHARGE_STEP1	17	// waitting for ev board inform to enter to charging, For precharge relay to D+ relay
-#define MODE_SINGLE_RUN				18
+#define MODE_FAULT					12
+#define MODE_RESERVATION			13
+#define MODE_BOOKING				14
+#define MODE_MAINTAIN				15
+#define MODE_DEBUG					16
+#define MODE_CCS_PRECHARGE_STEP0	17 	// ready for ccs precharge processing, For D+ relay to precharge relay
+#define MODE_CCS_PRECHARGE_STEP1	18	// waitting for ev board inform to enter to charging, For precharge relay to D+ relay
+#define MODE_SINGLE_RUN				19
 
 #define GFD_WAIT			0
 #define GFD_PASS			1
 #define GFD_FAIL			2
+#define GFD_WARNING			3
 
 #define PRECHARGE_WAIT				0
 #define PRECHARGE_READY				1
@@ -57,6 +60,7 @@ enum _SYSTEM_STATUS
 	S_TERMINATING,
 	S_COMPLETE,
 	S_ALARM,
+	S_FAULT,
 	S_RESERVATION,
 	S_BOOKING,
 	S_MAINTAIN,
@@ -64,13 +68,15 @@ enum _SYSTEM_STATUS
 	S_CCS_PRECHARGE_ST0,
 	S_CCS_PRECHARGE_ST1,
 	S_SINGLE_RUN,
+	S_NONE,
 };
 
 enum _GUN_TYPE
 {
 	_Type_Chademo = 		0,
-	_Type_CCS,
+	_Type_CCS_2,
 	_Type_GB,
+
 };
 
 enum _LCM_INDEX
@@ -110,6 +116,7 @@ enum _MODULE_PSU_WORK_STEP
 	_WORK_CHARGING 	= 			10,
 
 	_NO_WORKING			= 		254,
+	_INIT_PSU_STATUS	= 		255
 };
 
 enum _OFFLINE_POLICY
@@ -120,15 +127,30 @@ enum _OFFLINE_POLICY
 	_OFFLINE_POLICY_NO_CHARGING = 0x03,
 };
 
+enum _SYS_AUTHORIZE_MODE
+{
+	_SYS_AUTHORIZE_OCPP = 0x00,
+	_SYS_AUTHORIZE_FREE = 0x01
+};
+
 enum _REASSIGNED_RESOURCE_STEP
 {
 	_REASSIGNED_NONE = 			0,	//
 	_REASSIGNED_PREPARE = 		1,	// 系統收到需要降載需求 (輸出總電流降低),PSU Task 收到將狀態切換至下個狀態
 	_REASSIGNED_GET_NEW_CAP = 	2,	// 充電中的重新取得屬於自己火線上的總能量並透過小板通知車端 - 超過10秒直接跳下一步
-	_REASSIGNED_ADJUST = 		3,	// 車端降載完成,模塊能量重新調整
-	_REASSIGNED_RELAY = 		4,	// 切斷橋接的 Relay
-	_REASSIGNED_MAIN = 			5,  // 重新分配 PSU
+	_REASSIGNED_MAIN = 			3,  // 重新分配 PSU
+	_REASSIGNED_ADJUST = 		4,	// 模塊重新分配完成
+	_REASSIGNED_RELAY = 		5,	// 切斷橋接的 Relay
 	_REASSIGNED_COMP = 			6,	// 完成
+	_REASSIGNED_M_RELAY = 		7,	// 搭接橋接的 Relay
+	_REASSIGNED_M_MAIN = 		8,	// 重新分配 PSU (最大充)
+	_REASSIGNED_M_GET_NEW_CAP = 9	// 取得新的 CAP 並通知車端
+};
+
+enum _MAIN_CHARGING_MODE
+{
+	_MAIN_CHARGING_MODE_MAX = 0,
+	_MAIN_CHARGING_MODE_AVER = 1,
 };
 
 #endif /* CONFIG_H_ */

BIN
EVSE/Projects/DW30/Apps/Ev_Comm.o


BIN
EVSE/Projects/DW30/Apps/FactoryConfig


+ 239 - 89
EVSE/Projects/DW30/Apps/FactoryConfig.c

@@ -31,19 +31,63 @@
 #include	"../../define.h"
 #include 	"Config.h"
 
+#define Debug
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				-1
+#define OUTPUT_FLASH		0x01
+#define OUTPUT_FILE			0x02
+
+struct SysConfigData 			SysConfig;
+
+int StoreLogMsg(const char *fmt, ...);
+
+int StoreLogMsg(const char *fmt, ...)
+{
+	char Buf[4096+256];
+	char buffer[4096];
+	time_t CurrentTime;
+	struct tm *tm;
+	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);
+	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
+			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
+			buffer,
+			tm->tm_year+1900,tm->tm_mon+1);
+	system(Buf);
+
+	return rc;
+}
+
+void helpOutput(void)
+{
+	printf("Usage: Module_FactoryConfig [OPTION]...\r\n\r\n");
+	printf("Generate factory default configuration value\r\n\r\n");
+	printf("OPTION:\r\n");
+	printf("	-a Write to file(/mnt) & flash\r\n");
+	printf("	-f Write to file(/mnt)\r\n");
+	printf("	-m Write to flash\r\n");
+}
+
 /**************************************************************************************/
 /************This task will create Factory default confgiuration file *****************/
  /***********and store it into mtdblock 10,11,12                               ****************/
 /**************************************************************************************/
-
 int main(int argc,char *argv[])
 {
-	struct SysConfigData 	SysConfig;
-	unsigned int i,Chk;
+	unsigned char outType=0;
+	unsigned int i,Chk, MtdBlockSize=0x600000;
 	unsigned char *ptr;
 	int fd,wrd;
 
-	ptr=malloc(sizeof(struct SysConfigData));
+	ptr=malloc(MtdBlockSize);
 	if(ptr==NULL)
 	{
 		#ifdef SystemLogMessage
@@ -51,113 +95,219 @@ int main(int argc,char *argv[])
 		#endif
 		return 0;
 	}
-	memset(ptr,0,sizeof(struct SysConfigData));
-	memset(&SysConfig,0,sizeof(struct SysConfigData));
-
-	//Set default configuration
-	strcpy((char *)SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
-	strcpy((char *)SysConfig.Eth0Interface.EthSubmaskAddress,"255.255.255.0");
-	strcpy((char *)SysConfig.Eth0Interface.EthGatewayAddress,"192.168.1.254");
-	strcpy((char *)SysConfig.Eth1Interface.EthIpAddress,"192.168.0.10");
-	strcpy((char *)SysConfig.Eth1Interface.EthSubmaskAddress,"255.255.255.0");
-	strcpy((char *)SysConfig.Eth1Interface.EthGatewayAddress,"192.168.0.254");
-	SysConfig.BackendConnTimeout=300; //300 seconds
+	memset(ptr, 0, MtdBlockSize);
+	memset(&SysConfig, 0, sizeof(struct SysConfigData));
+
+	/*
+	 * TODO: Set factory default configuration
+	*/
+	//********** System **********// udhcpc -i eth1 -s ./dhcp_script/eth1.script
+	//
+	strcpy((char *)SysConfig.ModelName, "DSYE601J0EW2PH");
+	strcpy((char *)SysConfig.SerialNumber, "");
+
+	memset(SysConfig.SystemId, 0x00, sizeof(SysConfig.SystemId));
+	char Dash = '-';
+
+	strcat((char *)SysConfig.SystemId, (char *)SysConfig.ModelName);
+	strncat((char *)SysConfig.SystemId, &Dash, 1);
+	strcat((char *)SysConfig.SystemId, (char *)SysConfig.SerialNumber);
+
+	strcpy((char *)SysConfig.SystemDateTime, "");
+	SysConfig.AuthorisationMode = _SYS_AUTHORIZE_OCPP;
+	SysConfig.DefaultLanguage = 0;
+	SysConfig.RfidCardNumEndian = 0;
+	SysConfig.AcPlugInTimes = 0;
+	SysConfig.GbPlugInTimes = 0;
+	SysConfig.Ccs1PlugInTime = 0;
+	SysConfig.Ccs2PlugInTimes = 0;
+	SysConfig.ChademoPlugInTimes = 0;
+	//********** Charging **********//
+	SysConfig.RatingCurrent = 1200;			// 最大可輸出電流 120 A
+	SysConfig.MaxChargingEnergy = 0;
+	SysConfig.MaxChargingPower = 600; 		// 最大功率 : 跟著 model name 跑
+	SysConfig.MaxChargingCurrent = 1200;		// 最大可輸出電流 120 A
+	SysConfig.MaxChargingDuration = 0;
+	SysConfig.PhaseLossPolicy = 0;
+	for(unsigned char i = 0; i < 10; i++)
+		strcpy((char *)SysConfig.LocalWhiteCard, "");
+
+	strcpy((char *)SysConfig.UserId, "");
+	//********** Network **********//
+	strcpy((char *)SysConfig.FtpServer, "");
+	SysConfig.Eth0Interface.EthDhcpClient = 0;
+	strcpy((char *) SysConfig.Eth0Interface.EthMacAddress, "AA:BB:CC:DD:EE:FF");
+	strcpy((char *) SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
+	strcpy((char *) SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
+	strcpy((char *) SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
+	SysConfig.Eth1Interface.EthDhcpClient = 0;
+	strcpy((char *) SysConfig.Eth1Interface.EthMacAddress, "AA:BB:CC:DD:EE:FF");
+	strcpy((char *) SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
+	strcpy((char *) SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
+	strcpy((char *) SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
+	SysConfig.AthInterface.WifiMode = 0;
+	strcpy((char *) SysConfig.AthInterface.WifiSsid, "");
+	strcpy((char *) SysConfig.AthInterface.WifiPassword, "");
+	SysConfig.AthInterface.WifiRssi = 0;
+	SysConfig.AthInterface.WifiDhcpServer = 0;
+	SysConfig.AthInterface.WifiDhcpClient = 0;
+	strcpy((char *) SysConfig.AthInterface.WifiMacAddress, "");
+	strcpy((char *) SysConfig.AthInterface.WifiIpAddress, "");
+	strcpy((char *) SysConfig.AthInterface.WifiSubmaskAddress, "");
+	strcpy((char *) SysConfig.AthInterface.WifiGatewayAddress, "");
+	SysConfig.AthInterface.WifiNetworkConn = 0;
+	strcpy((char *) SysConfig.TelecomInterface.TelcomModelName, "");
+	strcpy((char *) SysConfig.TelecomInterface.TelcomSoftwareVer, "");
+	strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "Internet");
+	SysConfig.TelecomInterface.TelcomRssi = 0;
+	strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapId, " ");
+	strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
+	strcpy((char *) SysConfig.TelecomInterface.TelcomModemImei, "");
+	strcpy((char *) SysConfig.TelecomInterface.TelcomSimImsi, "");
+	strcpy((char *) SysConfig.TelecomInterface.TelcomSimIccid, "");
+	SysConfig.TelecomInterface.TelcomSimStatus = 0;
+	SysConfig.TelecomInterface.TelcomModemMode = 0;
+	strcpy((char *) SysConfig.TelecomInterface.TelcomIpAddress, "");
+	SysConfig.TelecomInterface.TelcomNetworkConn = 0;
+	strcpy((char *)SysConfig.chargePointVendor, "Phihong Technology");
+	//********** Backend **********//
+	SysConfig.BackendConnTimeout = 300; //300 seconds
+	SysConfig.OfflinePolicy = 0;
+	SysConfig.OfflineMaxChargeEnergy = 0;
+	SysConfig.OfflineMaxChargeDuration = 0;
+	strcpy((char *) SysConfig.OcppServerURL, "ws://test.evsocket.phihong.com.cn:2012/ocpp/");
+	strcpy((char *) SysConfig.ChargeBoxId, "DemoDC_Alston");
 
 	//copy default configuration to pointer
 	memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
 
 	//calculate CRC
 	Chk=0;
-	for(i=0;i<(sizeof(struct SysConfigData)-4);i++)
+	for(i=0;i<(MtdBlockSize-4);i++)
 	{
 		Chk+=*(ptr+i);
 	}
-	SysConfig.Checksum=Chk;
+	memcpy(	ptr+MtdBlockSize-4,&Chk,4);
 
-	fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR|O_CREAT);
-	if (fd < 0)
+	/*
+	* Parameter process
+	*/
+	if (argc > 1)
 	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
-		#endif
-		free(ptr);
-		return 0;
+		char *arg = argv[1];
+		switch (arg[0])
+		{
+		case '-':
+			switch (arg[1])
+			{
+				case 'a':
+					outType |= OUTPUT_FILE;
+					outType |= OUTPUT_FLASH;
+					break;
+				case 'f':
+					outType |= OUTPUT_FILE;
+					break;
+				case 'm':
+					outType |= OUTPUT_FLASH;
+					break;
+				default:
+					helpOutput();
+					break;
+			}
+				break;
+			default:
+				helpOutput();
+				break;
+		}
 	}
-
-	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
-	close(fd);
-	if(wrd!=(sizeof(struct SysConfigData)))
+	else
 	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: write /mnt/FactoryDefaultConfig.bin NG");
-		#endif
-		free(ptr);
-		return 0;
+		helpOutput();
 	}
 
-	fd = open("/dev/mtdblock12", O_RDWR);
-	if (fd < 0)
+	/*
+	 * Configuration bin file generate
+	*/
+	if((outType&OUTPUT_FILE)>0)
 	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock12 NG");
-		#endif
-		free(ptr);
-		return 0;
-   	 }
-    	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
-    	close(fd);
-    	if(wrd!=(sizeof(struct SysConfigData)))
-	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock12 NG");
-		#endif
-		free(ptr);
-		return 0;
+		fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
+		if (fd < 0)
+		{
+			StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
+			free(ptr);
+			return 0;
+		}
+		wrd=write(fd, ptr, MtdBlockSize);
+		close(fd);
+		if(wrd<MtdBlockSize)
+		{
+			StoreLogMsg("write /mnt/FactoryDefaultConfig.bin NG\r\n");
+			free(ptr);
+			return 0;
+		}
+		StoreLogMsg("FactoryConfig write to file in /mnt OK.\r\n");
 	}
 
-	fd = open("/dev/mtdblock11", O_RDWR);
-	if (fd < 0)
+	/*
+	* Flash memory write
+	*/
+	if((outType&OUTPUT_FLASH)>0)
 	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock11 NG");
-		#endif
-		free(ptr);
-		return 0;
-   	 }
-    	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
-    	close(fd);
-    	if(wrd!=(sizeof(struct SysConfigData)))
-	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock11 NG");
-		#endif
-		free(ptr);
-		return 0;
-	}
+		// Save factory default setting value to flash factory default setting block
+		fd = open("/dev/mtdblock12", O_RDWR);
+		if (fd < 0)
+		{
+			StoreLogMsg("open /dev/mtdblock12 NG\r\n");
+			free(ptr);
+			return 0;
+		}
+		wrd=write(fd, ptr, MtdBlockSize);
+		close(fd);
+		if(wrd<MtdBlockSize)
+		{
+			StoreLogMsg("write /dev/mtdblock12 NG\r\n");
+			free(ptr);
+			return 0;
+		}
 
-	fd = open("/dev/mtdblock10", O_RDWR);
-	if (fd < 0)
-	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock10 NG");
-		#endif
-		free(ptr);
-		return 0;
-   	 }
-    	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
-    	close(fd);
-    	if(wrd!=(sizeof(struct SysConfigData)))
-	{
-		#ifdef SystemLogMessage
-		StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock10 NG");
-		#endif
-		free(ptr);
-		return 0;
+		// Save factory default setting value to flash backup setting block
+		fd = open("/dev/mtdblock11", O_RDWR);
+		if (fd < 0)
+		{
+			StoreLogMsg("open /dev/mtdblock11 NG\r\n");
+			free(ptr);
+			return 0;
+		}
+		wrd=write(fd, ptr, MtdBlockSize);
+		close(fd);
+		if(wrd<MtdBlockSize)
+		{
+			StoreLogMsg("write /dev/mtdblock11 NG\r\n");
+			free(ptr);
+			return 0;
+		}
+
+		// Save factory default setting value to flash setting block
+		fd = open("/dev/mtdblock10", O_RDWR);
+		if (fd < 0)
+		{
+			StoreLogMsg("open /dev/mtdblock10 NG\r\n");
+			free(ptr);
+			return 0;
+		}
+		wrd=write(fd, ptr, MtdBlockSize);
+		close(fd);
+		if(wrd<MtdBlockSize)
+		{
+			StoreLogMsg("write /dev/mtdblock10 NG\r\n");
+			free(ptr);
+			return 0;
+		}
+		StoreLogMsg("FactoryConfig write to flash OK\r\n");
 	}
 
 	free(ptr);
-	#ifdef SystemLogMessage
-	StoreLogMsg("[FactoryConfig]main: FactoryConfig OK");
-	#endif
+
+	return FAIL;
 }
 

BIN
EVSE/Projects/DW30/Apps/FactoryConfig.o


+ 35 - 20
EVSE/Projects/DW30/Apps/Makefile

@@ -8,64 +8,79 @@ Internal485ProtocolLib = -L ../../../Modularization/Internal485Protocol -lIntern
 all: CopyFile apps
 #apps: Module_CSU Module_EvComm Module_EventLogging Module_InternalComm Module_LcmControl Module_PrimaryComm Module_PsuComm 
 # ReadCmdline kill.sh
-apps: MainTask EvCommTask EventLoggingTask InternalCommTask LcmControlTask PrimaryCommTask PsuCommTask ReadCmdlineTask WebService OtherTools
+apps: MainTask EvCommTask EventLoggingTask InternalCommTask LcmControlTask PrimaryCommTask PsuCommTask ReadCmdlineTask WebService 4GTask FactoryConfigApp OtherTools
 
 MainTask:
 	rm -f *.o
 	rm -f main;
-	$(CC) -O0 -g3 -Wall -c -fmessage-length=0 -o timeout.o timeout.c
-	$(CC) -O0 -g3 -Wall -c -fmessage-length=0 -o main.o main.c
-	$(CC) -o main main.o timeout.o ../../../Modularization/libModule_RFID.a ../../../Modularization/libModule_Upgrade.a
+	$(CC) -D $(Project) -include../../../Modularization/Module_RFID.h -O0 -g3 -Wall -c -fmessage-length=0 -o main.o main.c
+	$(CC) -D $(Project) -include../../../Modularization/Module_RFID.h -O0 -g3 -Wall -c -fmessage-length=0 -o timeout.o timeout.c
+	$(CC) -o main main.o timeout.o ../../../Modularization/libModule_RFID.a ../../../Modularization/libModule_Upgrade.a	
 	cp -f main ../Images/root
 
 EvCommTask:
 	rm -f Module_EvComm;
-	$(CC) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Ev_Comm.o Ev_Comm.c
-	$(CC) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_EvComm.o Module_EvComm.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Ev_Comm.o Ev_Comm.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_EvComm.o Module_EvComm.c
 	$(CC) -o Module_EvComm Ev_Comm.o Module_EvComm.o
 	cp -f Module_EvComm ../Images/root	
-
+	
 EventLoggingTask:
 	rm -f Module_EventLogging;
-	$(CC) -O0 -g3 -Wall -c -fmessage-length=0 -o Module_EventLogging.o Module_EventLogging.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_EventLogging.o Module_EventLogging.c
 	$(CC) -o Module_EventLogging Module_EventLogging.o 	
-	cp -f Module_EventLogging ../Images/root
-
+	cp -f Module_EventLogging ../Images/root	
+	
 InternalCommTask:
 	rm -f Module_InternalComm; 
-	$(CC) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o internalComm.o internalComm.c
-	$(CC) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_InternalComm.o Module_InternalComm.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o internalComm.o internalComm.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_InternalComm.o Module_InternalComm.c
 	$(CC) -o Module_InternalComm Module_InternalComm.o internalComm.o 	
 	cp -f Module_InternalComm ../Images/root
-
+	
 LcmControlTask:
 	rm -f Module_LcmControl; 
-	$(CC) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_LcmControl.o Module_LcmControl.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_LcmControl.o Module_LcmControl.c
 	$(CC) -o Module_LcmControl Module_LcmControl.o
-	cp -f Module_LcmControl ../Images/root	
+	cp -f Module_LcmControl ../Images/root			
 
 PrimaryCommTask:
 	rm -f Module_PrimaryComm; 
-	$(CC) -O0 -g3 -Wall -c -fmessage-length=0 -o Module_PrimaryComm.o Module_PrimaryComm.c
-	$(CC) -O0 -g3 -Wall -c -fmessage-length=0 -o PrimaryComm.o PrimaryComm.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_PrimaryComm.o Module_PrimaryComm.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o PrimaryComm.o PrimaryComm.c
 	$(CC) -o Module_PrimaryComm Module_PrimaryComm.o PrimaryComm.o
 	cp -f Module_PrimaryComm ../Images/root	
 
 PsuCommTask:
 	rm -f Module_PsuComm; 
-	$(CC) -include../../../Modularization/Infypwr_PsuCommObj.h -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_PsuComm.o Module_PsuComm.c
+	$(CC) -D $(Project) -include../../../Modularization/Infypwr_PsuCommObj.h -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o Module_PsuComm.o Module_PsuComm.c
 	$(CC) -o Module_PsuComm Module_PsuComm.o ../../../Modularization/libInfypwr_PsuCommObj.a
-	cp -f Module_PsuComm ../Images/root
+	cp -f Module_PsuComm ../Images/root	
 	
 ReadCmdlineTask:
 	rm -f ReadCmdline; 
-	$(CC) -O0 -g3 -Wall -c -fmessage-length=0 -o ReadCmdline.o ReadCmdline.c
+	$(CC) -D $(Project) -includeConfig.h -O0 -g3 -Wall -c -fmessage-length=0 -o ReadCmdline.o ReadCmdline.c
 	$(CC) -o ReadCmdline ReadCmdline.o
 	cp -f ReadCmdline ../Images/root
 
+FactoryConfigApp:
+	@echo "===== Module_FactoryConfig_Task =================================="
+	rm -f FactoryConfig
+	gcc -D $(Project) "-I../../" -o FactoryConfig "./FactoryConfig.c"
+	mkdir -p /Storage/SystemLog	
+	./FactoryConfig -f;true
+	cp /mnt/FactoryDefaultConfig.bin ../Images
+	rm -f FactoryConfig; 
+	$(CC) -D $(Project) -O0 -g3 -Wall -c -fmessage-length=0 -o FactoryConfig.o FactoryConfig.c 
+	$(CC) -o FactoryConfig FactoryConfig.o 
+	cp -f FactoryConfig ../Images/root
+
 WebService:
 	cp -f ../../../Modularization/WebService ../Images/root
 
+4GTask:
+	cp -f ../../../Modularization/Module_4g ../Images/root
+
 OtherTools:
 	cp -f init.sh ../Images/root
 	cp -f kill.sh ../Images/root

BIN
EVSE/Projects/DW30/Apps/Module_EvComm


+ 1384 - 1117
EVSE/Projects/DW30/Apps/Module_EvComm.c

@@ -1,1117 +1,1384 @@
-#include 	<sys/time.h>
-#include 	<sys/timeb.h>
-#include    <sys/types.h>
-#include    <sys/stat.h>
-#include 	<sys/types.h>
-#include 	<sys/ioctl.h>
-#include 	<sys/socket.h>
-#include 	<sys/ipc.h>
-#include 	<sys/shm.h>
-#include 	<sys/shm.h>
-#include 	<sys/mman.h>
-#include 	<linux/can.h>
-#include 	<linux/can/raw.h>
-#include 	<linux/wireless.h>
-#include 	<arpa/inet.h>
-#include 	<netinet/in.h>
-
-#include 	<unistd.h>
-#include 	<stdarg.h>
-#include    <stdio.h>      /*標準輸入輸出定義*/
-#include    <stdlib.h>     /*標準函數庫定義*/
-#include    <unistd.h>     /*Unix 標準函數定義*/
-#include    <fcntl.h>      /*檔控制定義*/
-#include    <termios.h>    /*PPSIX 終端控制定義*/
-#include    <errno.h>      /*錯誤號定義*/
-#include 	<errno.h>
-#include 	<string.h>
-#include	<time.h>
-#include	<ctype.h>
-#include 	<ifaddrs.h>
-#include	"../../define.h"
-#include 	"Module_EvComm.h"
-
-#define Debug
-#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
-#define PASS				1
-#define FAIL				-1
-#define START				1
-#define STOP				0
-#define YES					1
-#define NO					0
-#define DEMO 				0
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct FanModuleData			*ShmFanModuleData;
-struct CHAdeMOData				*ShmCHAdeMOData;
-struct CcsData					*ShmCcsData;
-
-byte gun_count = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
-int chargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-
-float maxChargingVol = 6000;			// 限制最大充電電壓,如依照模塊則填上 0
-float maxChargingCur = 500;				// 限制最大充電電流,如依照模塊則填上 0
-
-// 槍資訊
-struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-
-struct Ev_Board_Cmd Ev_Cmd={
-		0,
-		0x00000200,
-		0x00000400,
-		0x00000500,
-		0x00000600,
-		0x00000700,
-		0x00000800,
-		0x00000900,
-		0x00000A00,
-		0x00000C00,
-		0x00000D00,
-
-		0x00000E00,
-		0x00000F00,
-		0x00001000,
-		0x00001100,
-
-		0x00001200,
-		0x00001500,
-};
-
-unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
-
-void GetMaxVolAndCurMethod(byte index, float *vol, float *cur);
-unsigned long GetTimeoutValue(struct timeval _sour_time);
-unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
-void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
-
-#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)
-
-// 檢查 Byte 中某個 Bit 的值
-// _byte : 欲改變的 byte
-// _bit : 該 byte 的第幾個 bit
-unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
-{
-	return ( _byte & mask_table[_bit] ) != 0x00;
-}
-
-// 設定 Byte 中某個 Bit的值
-// _byte : 欲改變的 byte
-// _bit : 該 byte 的第幾個 bit
-// value : 修改的值為 0 or 1
-void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
-{
-	if(value == 1)
-		*_byte |= (1 << _bit);
-	else if (value == 0)
-		*_byte ^= (1 << _bit);
-}
-
-unsigned long GetTimeoutValue(struct timeval _sour_time)
-{
-	struct timeval _end_time;
-	gettimeofday(&_end_time, NULL);
-
-	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
-}
-
-int StoreLogMsg(const char *fmt, ...)
-{
-	char Buf[4096+256];
-	char buffer[4096];
-	time_t CurrentTime;
-	struct tm *tm;
-	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);
-	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
-			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
-			buffer,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-
-	return rc;
-}
-
-int DiffTimeb(struct timeb ST, struct timeb ET)
-{
-	//return milli-second
-	unsigned int StartTime,StopTime;
-
-	StartTime=(unsigned int)ST.time;
-	StopTime=(unsigned int)ET.time;
-	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
-}
-
-//=================================
-// Common routine
-//=================================
-void getTimeString(char *buff)
-{
-	time_t timep;
-	struct tm *p;
-	time(&timep);
-	p=gmtime(&timep);
-
-	sprintf(buff, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
-}
-
-bool CheckUniqNumber(byte value)
-{
-	for (byte index = 0; index < gun_count; index++)
-	{
-		if (_chargingData[index]->Evboard_id == value)
-		{
-			struct timeval _end_time;
-			gettimeofday(&_end_time, NULL);
-			unsigned long diff = 1000000 *	(_end_time.tv_sec - _id_assign_time.tv_sec) + _end_time.tv_usec - _id_assign_time.tv_usec;
-			if (diff >= 3000000)
-			{
-				gettimeofday(&_id_assign_time, NULL);
-				return true;
-			}
-			else
-			{
-				return false;
-			}
-		}
-	}
-
-	gettimeofday(&_id_assign_time, NULL);
-	return true;
-}
-
-//==========================================
-// Init all share memory
-//==========================================
-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;
-   	 }
-    else
-    {}
-
-   	 //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;
-   	}
-    else
-    {}
-
-   	if(CHAdeMO_QUANTITY > 0)
-   	{
-   		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
-   		{
-   			#ifdef SystemLogMessage
-   		   	DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
-   			#endif
-   			return FAIL;
-   		}
-   		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-   		{
-   			#ifdef SystemLogMessage
-   		   	DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
-   			#endif
-   			return FAIL;
-   		}
-   		else
-   		{}
-   	}
-
-   	if(CCS_QUANTITY > 0)
-   	{
-   		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
-   		{
-   			#ifdef SystemLogMessage
-   			DEBUG_ERROR("shmget ShmCcsData NG \n");
-   			#endif
-   			return FAIL;
-   		}
-   		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
-   			#ifdef SystemLogMessage
-   		   	DEBUG_ERROR("shmat ShmCcsData NG \n");
-   			#endif
-   			return FAIL;
-   		}
-   		else
-   		{}
-   	}
-
-    return result;
-}
-
-//================================================
-// initial can-bus
-//================================================
-int InitCanBus()
-{
-	int 					s0,nbytes;
-	struct timeval			tv;
-	struct ifreq 			ifr0;
-	struct sockaddr_can		addr0;
-
-	system("/sbin/ip link set can0 down");
-	system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
-	system("/sbin/ip link set can0 up");
-
-	s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
-
-	tv.tv_sec = 0;
-	tv.tv_usec = 10000;
-   	if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct	timeval)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("Set SO_RCVTIMEO NG");
-		#endif
-	}
-	nbytes=40960;
-	if (setsockopt(s0, SOL_SOCKET,  SO_RCVBUF, &nbytes, sizeof(int)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("Set SO_RCVBUF NG");
-		#endif
-	}
-	nbytes=40960;
-	if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("Set SO_SNDBUF NG");
-		#endif
-	}
-
-   	strcpy(ifr0.ifr_name, "can0" );
-	ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
-	addr0.can_family = AF_CAN;
-	addr0.can_ifindex = ifr0.ifr_ifindex;
-	bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
-	return s0;
-}
-
-//================================================
-//================================================
-// CANBUS receive task
-//================================================
-//================================================
-bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
-{
-	for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
-	{
-		if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
-		{
-			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
-			return true;
-		}
-	}
-
-	for (byte index = 0; index < CCS_QUANTITY; index++)
-	{
-		if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
-		{
-			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
-			return true;
-		}
-	}
-
-	for (byte index = 0; index < GB_QUANTITY; index++)
-	{
-		if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
-		{
-			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
-			return true;
-		}
-	}
-
-	return false;
-}
-
-void AddrAssignment(byte *data)
-{
-	byte target_number[8];
-	byte index = 0x00;
-
-	memcpy(target_number, data, sizeof(target_number));
-	index = *(data + 4);
-
-	if (CheckUniqNumber(index))
-	{
-		printf("EV board id = %x \n", index);
-//		printf("target_number[0] = %x \n", target_number[0]);
-//		printf("target_number[1] = %x \n", target_number[1]);
-//		printf("target_number[2] = %x \n", target_number[2]);
-//		printf("target_number[3] = %x \n", target_number[3]);
-//		printf("target_number[4] = %x \n", target_number[4]);
-
-		printf("SetTargetAddr = %d \n", index);
-		SetTargetAddr(target_number, index);
-	}
-}
-
-void ClearAbnormalStatus_Chadmoe(byte gun_index)
-{
-	int codeValue = 0;
-
-	if (strlen((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index]) == 6)
-	{
-		codeValue = atoi((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index]);
-
-		if (codeValue >= 23700 && codeValue <= 23736)
-		{
-			memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], "", 7);
-			ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = 0;
-		}
-	}
-
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvCommFail = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryMalfun = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoNoPermission = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryIncompatibility = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOVP = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryUVP = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOTP = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryCurrentDiff = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryVoltageDiff = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoShiftPosition = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOtherFault = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargingSystemError = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvNormalStop = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoTempSensorBroken = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoConnectorLockFail = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoD1OnNoReceive = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeAllowTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoWaitGfdTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsReqCurrentTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJOffTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayOffTimeout = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan10V = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan20V = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeBeforeStop = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetNormalStop = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoIsolationResultFail = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoMissLinkWithMotherBoard = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoOutputVolMoreThanLimit = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReqCurrentMoreThanLimit = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReCapBmsEqrCurrentExceed = 0x00;
-	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargeRemainCountDown = 0x00;
-}
-
-void AbnormalStopAnalysis(byte gun_index, byte *errCode)
-{
-	char string[7];
-	sprintf(string, "%d%d%d%d%d%d", *(errCode + 5), *(errCode + 4), *(errCode + 3), *(errCode + 2), *(errCode + 1), *(errCode + 0));
-
-	if (gun_index < gun_count)
-	{
-		if (strlen((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index]) <= 0)
-		{
-			memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], string, 7);
-			ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = 0x00;
-		}
-	}
-	printf("NOTIFICATION_EV_STOP : Err Code = %s \n", string);
-
-	if (strcmp(string, "023700") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvCommFail = 0x01;
-	if (strcmp(string, "023701") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail = 0x01;
-	if (strcmp(string, "023702") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.GbEvCommFail = 0x01;
-	if (strcmp(string, "023703") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = 0x01;
-	if (strcmp(string, "023704") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryMalfun = 0x01;
-	if (strcmp(string, "023705") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoNoPermission = 0x01;
-	if (strcmp(string, "023706") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryIncompatibility = 0x01;
-	if (strcmp(string, "023707") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOVP = 0x01;
-	if (strcmp(string, "023708") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryUVP = 0x01;
-	if (strcmp(string, "023709") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOTP = 0x01;
-	if (strcmp(string, "023710") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryCurrentDiff = 0x01;
-	if (strcmp(string, "023711") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryVoltageDiff = 0x01;
-	if (strcmp(string, "023712") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoShiftPosition = 0x01;
-	if (strcmp(string, "023713") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOtherFault = 0x01;
-	if (strcmp(string, "023714") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargingSystemError = 0x01;
-	if (strcmp(string, "023715") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvNormalStop = 0x01;
-	if (strcmp(string, "023716") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoTempSensorBroken = 0x01;
-	if (strcmp(string, "023717") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoConnectorLockFail = 0x01;
-	if (strcmp(string, "023718") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoD1OnNoReceive = 0x01;
-	if (strcmp(string, "023719") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJTimeout = 0x01;
-	if (strcmp(string, "023720") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeAllowTimeout = 0x01;
-	if (strcmp(string, "023721") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoWaitGfdTimeout = 0x01;
-	if (strcmp(string, "023722") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayTimeout = 0x01;
-	if (strcmp(string, "023723") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsReqCurrentTimeout = 0x01;
-	if (strcmp(string, "023724") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJOffTimeout = 0x01;
-	if (strcmp(string, "023725") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayOffTimeout = 0x01;
-	if (strcmp(string, "023726") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan10V = 0x01;
-	if (strcmp(string, "023727") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan20V = 0x01;
-	if (strcmp(string, "023728") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeBeforeStop = 0x01;
-	if (strcmp(string, "023729") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetNormalStop = 0x01;
-	if (strcmp(string, "023730") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = 0x01;
-	if (strcmp(string, "023731") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoIsolationResultFail = 0x01;
-	if (strcmp(string, "023732") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoMissLinkWithMotherBoard = 0x01;
-	if (strcmp(string, "023733") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoOutputVolMoreThanLimit = 0x01;
-	if (strcmp(string, "023734") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReqCurrentMoreThanLimit = 0x01;
-	if (strcmp(string, "023735") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReCapBmsEqrCurrentExceed = 0x01;
-	if (strcmp(string, "023736") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargeRemainCountDown = 0x01;
-}
-
-void CANReceiver()
-{
-	pid_t canRecPid;
-
-	canRecPid = fork();
-
-	if(canRecPid > 0)
-	{
-		int nbytes;
-		struct can_frame frame;
-		int intCmd;
-		// 槍資訊
-		struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-		struct timeval _cmd_ack_timeout[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-
-		bool isPass = false;
-		while(!isPass)
-		{
-			isPass = true;
-			for (byte _index = 0; _index < gun_count; _index++)
-			{
-				if (!FindChargingInfoData(_index, &_chargingData[0]))
-				{
-					DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
-					isPass = false;
-					break;
-				}
-			}
-		}
-
-		for (byte _index = 0; _index < gun_count; _index++)
-			gettimeofday(&_cmd_ack_timeout[_index], NULL);
-
-		while (1)
-		{
-			memset(&frame, 0, sizeof(struct can_frame));
-			nbytes = read(CanFd, &frame, sizeof(struct can_frame));
-
-			for (byte _index = 0; _index < gun_count; _index++)
-			{
-				if (GetTimeoutValue(_cmd_ack_timeout[_index]) >= 5000000)
-				{
-					// ACK timeout
-					//printf("gun = %x, ack timeout \n", _index);
-				}
-			}
-
-			if (nbytes > 0)
-			{
-				byte target;
-				byte targetGun = 0x00;
-				intCmd = (int) (frame.can_id & CAN_EFF_MASK);
-
-				if (intCmd == ADDRESS_REQ)
-				{
-					AddrAssignment(frame.data);
-					continue;
-				}
-
-				intCmd = (int) (frame.can_id & CAN_EFF_MASK & 0xFFFFFF00);
-				target = ((byte) (frame.can_id & 0x000000FF));		// 0x01 or 0x02
-
-				for (byte _index = 0; _index < gun_count; _index++)
-				{
-					if (_chargingData[_index]->Evboard_id == target)
-					{
-						targetGun = _index;
-						break;
-					}
-				}
-
-				if(targetGun < 0 || targetGun >= CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY)
-				{
-					printf("EvComm (CANReceiver) : Target index = %x is < 0 or > QUANTITY \n", targetGun);
-					continue;
-				}
-				if(intCmd == 256)
-				{
-					continue;
-				}
-
-				gettimeofday(&_cmd_ack_timeout[targetGun], NULL);
-				switch (intCmd)
-				{
-					case NOTIFICATION_EV_STATUS:
-					{
-						_chargingData[targetGun]->ConnectorPlugIn = frame.data[0];
-						_chargingData[targetGun]->PilotVoltage = frame.data[1];
-
-						//printf("index = %d, ConnectorPlugIn = %x, data[0] = %x \n", targetGun, _chargingData[targetGun]->ConnectorPlugIn, frame.data[0]);
-						//printf("ConnectorPlugIn = %x \n", (-120 + frame.data[1]) / 10);
-					}
-						break;
-					case ACK_EV_FW_VERSION:
-					{
-						if (_chargingData[targetGun]->Type == _Type_Chademo)
-						{
-							memcpy(ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
-							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
-						}
-						else if (_chargingData[targetGun]->Type == _Type_CCS)
-						{
-							printf("Get FW HW = %s \n", frame.data);
-							if (ShmCcsData->CommProtocol == 0x01)
-							{
-								memcpy(&ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
-								ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
-							}
-						}
-					}
-						break;
-					case ACK_EV_HW_VERSION:
-					{
-						//printf("Get EV HW = %s \n", frame.data);
-					}
-						break;
-					case ACK_GET_OUTPUT_REQ:
-					{
-						_chargingData[targetGun]->EvBatterySoc = frame.data[1];
-						_chargingData[targetGun]->EvBatterytargetVoltage = ((short) frame.data[3] << 8) + (short) frame.data[2];
-						_chargingData[targetGun]->EvBatterytargetCurrent = ((short) frame.data[5] << 8) + (short) frame.data[4];
-						_chargingData[targetGun]->PresentChargedDuration = ((short) frame.data[7] << 8) + (short) frame.data[6];
-
-						if (_chargingData[targetGun]->Type == _Type_Chademo)
-						{
-							if (ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].EvDetection != frame.data[0])
-							{
-								ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
-							}
-
-							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].EvDetection = frame.data[0];
-							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].SOC = _chargingData[targetGun]->EvBatterySoc;
-							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].TargetBatteryVoltage = _chargingData[targetGun]->EvBatterytargetVoltage;
-							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].ChargingCurrentRequest = _chargingData[targetGun]->EvBatterytargetCurrent;
-						}
-						else if (_chargingData[targetGun]->Type == _Type_CCS)
-						{
-							if(ShmCcsData->CommProtocol == 0x01)
-							{
-								ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
-							}
-						}
-
-						//printf("(EvComm) EvBatterytargetVoltage = %f \n", _chargingData[targetGun]->EvBatterytargetVoltage);
-						//printf("(EvComm) EvBatterytargetCurrent = %f \n", _chargingData[targetGun]->EvBatterytargetCurrent);
-						//printf("BatteryVoltage = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TargetBatteryVoltage);
-						//printf("CurrentRequest = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].ChargingCurrentRequest);
-					}
-						break;
-					case ACK_GET_EV_BATTERY_INFO:
-					{
-						//_chargingData[target].EvACorDCcharging = frame.data[0];
-						//_chargingData[target]->TotalBatteryCap = ((float) frame.data[4] << 8) + (short) frame.data[3];
-						_chargingData[targetGun]->EvBatteryMaxVoltage = ((short) frame.data[4] << 8) + (short) frame.data[3];
-						//_chargingData[target]->EvBatteryMaxCurrent = ((float) frame.data[4] << 8) + (short) frame.data[3];
-						//_chargingData[target].MaxiBatteryCurrent = ((short) frame.data[6] << 8) + (short) frame.data[5];
-						if (_chargingData[targetGun]->Type == _Type_Chademo)
-						{
-							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].TotalBatteryCapacity = ((short) frame.data[2] << 8) + (short) frame.data[1];
-							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].MaxiBatteryVoltage = _chargingData[targetGun]->EvBatteryMaxVoltage;
-
-							//printf("EvBatteryMaxVoltage = %f \n", _chargingData[target]->EvBatteryMaxVoltage);
-							//printf("TotalBatteryCapacity = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TotalBatteryCapacity);
-							//printf("MaxiBatteryVoltage = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].MaxiBatteryVoltage);
-						}
-						else if (_chargingData[targetGun]->Type == _Type_CCS)
-						{
-
-						}
-					}
-						break;
-					case ACK_GET_MISCELLANEOUS_INFO:
-					{
-						if (_chargingData[targetGun]->Type == _Type_Chademo)
-						{
-							_chargingData[targetGun]->GunLocked = frame.data[0];
-							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureP = frame.data[1];
-							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureN = frame.data[2];
-							_chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
-							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].EvboardStatus = frame.data[7];
-						}
-						else if (_chargingData[targetGun]->Type == _Type_CCS)
-						{
-							if (ShmCcsData->CommProtocol == 0x01)
-							{
-								_chargingData[targetGun]->GunLocked = frame.data[0];
-								//ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index]. .ConnectorTemperatureP = frame.data[1];
-								//ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index]. .ConnectorTemperatureN = frame.data[2];
-								_chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
-							}
-						}
-
-						//printf("EvboardStatus = %x \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].EvboardStatus);
-						//printf("ConnectorPlug locked = %x \n", frame.data[0]);
-						//printf("ConnectorTemp 0= %d \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].ConnectorTemperatureP);
-						//printf("ConnectorTemp 1= %d \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].ConnectorTemperatureN);
-						//printf("PilotVoltage = %x \n", (-120 + frame.data[3]) / 10);
-					}
-						break;
-					case ACK_EVSE_ISOLATION_STATUS:	{}
-						break;
-					case ACK_EVSE_PRECHAGE_INFO:
-					{
-						_chargingData[targetGun]->PrechargeStatus = frame.data[0];
-					}
-						break;
-					case NOTIFICATION_EV_STOP:
-					{
-						// 車端要求停止
-						// frame.data[0] : 0x01 => normal stop, 0x02 => ev emergency stop
-						if (frame.data[0] == 0x02)
-						{
-							//printf("NOTIFICATION_EV_STOP -----------------------------\n");
-							AbnormalStopAnalysis(targetGun, frame.data + 1);
-						}
-						_chargingData[targetGun]->StopChargeFlag = YES;
-					}
-						break;
-					default:
-						printf("EV board = %d, Ack none defined. intCmd = %d  \n", targetGun, intCmd);
-						break;
-				}
-			}
-			usleep(10000);
-		}
-	}
-}
-
-//================================================
-// Main process
-//================================================
-// 檢查 Byte 中某個 Bit 的值
-// _byte : 欲改變的 byte
-// _bit : 該 byte 的第幾個 bit
-unsigned char EvDetectionStatus(unsigned char _byte, unsigned char _bit)
-{
-	return ( _byte & mask_table[_bit] ) != 0x00;
-}
-
-bool IsConnectorPlugIn(struct ChargingInfoData *chargingData)
-{
-	return (chargingData->ConnectorPlugIn == 0x01) ? true : false;
-}
-
-void SetPresentChargingOutputPower(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
-{
-	float vol1 = 0, cur1 = 0;
-	float vol2 = 0, cur2 = 0;
-
-	//printf("f vol - 0 = %f \n", chargingData_1->FireChargingVoltage);
-	//printf("f cur - 0 = %f \n", chargingData_1->PresentChargingCurrent);
-	//printf("f vol - 1 = %f \n", chargingData_2->FireChargingVoltage);
-	//printf("f cur - 1 = %f \n", chargingData_2->PresentChargingCurrent);
-
-	vol1 = chargingData_1->FireChargingVoltage;
-	cur1 = chargingData_1->PresentChargingCurrent;
-
-	vol2 = chargingData_2->FireChargingVoltage;
-	cur2 = chargingData_2->PresentChargingCurrent;
-
-	SetPresentOutputPower(vol1, cur1, vol2, cur2);
-}
-
-void SetPresentChargingOutputCap(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
-{
-	float pow1 = 0, cur1 = 0;
-	float pow2 = 0, cur2 = 0;
-	float vol = 0;
-
-	if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
-	{
-		pow1 = chargingData_1->DeratingChargingPower;
-		cur1 = chargingData_1->DeratingChargingCurrent;
-		printf("Left : To EV D_Power_1 = %f, D_Cur_1 = %f \n", pow1, cur1);
-	}
-	else
-	{
-		pow1 = chargingData_1->AvailableChargingPower;
-		cur1 = chargingData_1->AvailableChargingCurrent;
-	}
-	vol = chargingData_1->MaximumChargingVoltage;
-	GetMaxVolAndCurMethod(chargingData_1->Index, &vol, &cur1);
-
-	if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
-	{
-		pow2 = chargingData_2->DeratingChargingPower;
-		cur2 = chargingData_2->DeratingChargingCurrent;
-		printf("Right : To EV D_Power_1 = %f, D_Cur_1 = %f \n", pow2, cur2);
-	}
-	else
-	{
-		pow2 = chargingData_2->AvailableChargingPower;
-		cur2 = chargingData_2->AvailableChargingCurrent;
-	}
-	vol = chargingData_2->MaximumChargingVoltage;
-	GetMaxVolAndCurMethod(chargingData_2->Index, &vol, &cur2);
-
-	printf("To EV Power_1 = %f, Cur_1 = %f, Power_2 = %f, Cur_2 = %f \n", pow1, cur1, pow2, cur2);
-	SetPresentOutputCapacity(pow1, cur1, pow2, cur2);
-}
-
-void Initialization()
-{
-	bool isPass = false;
-	while(!isPass)
-	{
-		isPass = true;
-		for (byte _index = 0; _index < gun_count; _index++)
-		{
-			if (!FindChargingInfoData(_index, &_chargingData[0]))
-			{
-				DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
-				isPass = false;
-				break;
-			}
-		}
-	}
-}
-
-void GetMaxVolAndCurMethod(byte index, float *vol, float *cur)
-{
-	if (maxChargingVol != 0 && maxChargingVol <= *vol)
-		*vol = maxChargingVol;
-
-	if (maxChargingCur != 0 && maxChargingCur <= *cur)
-		*cur = maxChargingCur;
-}
-
-void GetStopChargingReasonByEvse(byte gunIndex, byte *reason)
-{
-	if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == 0x01)
-	{
-		// 012251
-		*(reason + 5) = 0;
-		*(reason + 4)  = 1;
-		*(reason + 3)  = 2;
-		*(reason + 2)  = 2;
-		*(reason + 1)  = 5;
-		*(reason + 0)  = 1;
-	}
-	else if (_chargingData[gunIndex]->Type == _Type_Chademo &&
-			ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == 0x01)
-	{
-		// 011012
-		*(reason + 5) = 0;
-		*(reason + 4) = 1;
-		*(reason + 3) = 1;
-		*(reason + 2) = 0;
-		*(reason + 1) = 1;
-		*(reason + 0) = 2;
-	}
-	else if (_chargingData[gunIndex]->Type == _Type_CCS &&
-			ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == 0x01)
-	{
-		// 011014
-		*(reason + 5) = 0;
-		*(reason + 4) = 1;
-		*(reason + 3) = 1;
-		*(reason + 2) = 0;
-		*(reason + 1) = 1;
-		*(reason + 0) = 4;
-	}
-}
-
-int main(int argc, char *argv[])
-{
-	if(InitShareMemory() == FAIL)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("InitShareMemory NG\n");
-		#endif
-		if(ShmStatusCodeData != NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}
-		sleep(5);
-		return 0;
-	}
-
-	Initialization();
-	CanFd = InitCanBus();
-	CANReceiver();
-
-	byte priorityLow = 1;
-#if (!DEMO)
-	while(CanFd)
-	{
-		for(byte _index = 0; _index < gun_count; _index++)
-		{
-			if (priorityLow == 1)
-			{
-				// 優先權較低 - 只要有回應即不會再詢問
-				if (_chargingData[_index]->Type == _Type_Chademo &&
-						ShmCHAdeMOData->evse[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
-				{
-					GetFirmwareVersion(_index, _chargingData[_index]->Evboard_id);
-					GetHardwareVersion(_index, _chargingData[_index]->Evboard_id);
-				}
-				else if (_chargingData[_index]->Type == _Type_CCS)
-				{
-					if (ShmCcsData->CommProtocol == 0x01 &&
-						ShmCcsData->V2GMessage_DIN70121[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
-					{
-						GetFirmwareVersion(_index, _chargingData[_index]->Evboard_id);
-						GetHardwareVersion(_index, _chargingData[_index]->Evboard_id);
-					}
-				}
-
-				// 固定要取得的資訊 : 1.槍鎖狀態, 2."Connector 1" 溫度, 3."Connector 2" 溫度, 4.Pilot Voltage
-				//printf("GetMiscellaneousInfo. index = %d, Eid = %d \n", _index, _chargingData[_index]->Evboard_id);
-				GetMiscellaneousInfo(_index, _chargingData[_index]->Evboard_id);
-			}
-
-			switch (_chargingData[_index]->SystemStatus)
-			{
-				case S_IDLE:
-					_chargingData[_index]->PresentChargedEnergy = 0;
-					_chargingData[_index]->GroundFaultStatus = GFD_WAIT;
-					_chargingData[_index]->StopChargeFlag = NO;
-					chargingTime[_index] = 0;
-
-					if (_chargingData[_index]->Type == _Type_Chademo)
-					{
-						ClearAbnormalStatus_Chadmoe(_index);
-					}
-					else if (_chargingData[_index]->Type == _Type_CCS)
-					{
-
-					}
-					break;
-				case S_PREPARING_FOR_EV:
-				{
-					// 開始確認車端是否同意開始充電 : 1.SOC, 2.Target Vol, 3.Target Cur, 4.Charging remaining time
-					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
-
-					//printf("PresentChargingVoltage = %f \n", _chargingData[_index]->PresentChargingVoltage);
-					//printf("PresentChargingCurrent = %f \n", _chargingData[_index]->PresentChargingCurrent);
-					//printf("AvailableChargingPower = %f \n", _chargingData[_index]->AvailableChargingPower);
-					//printf("AvailableChargingCurrent = %f \n", _chargingData[_index]->AvailableChargingCurrent);
-					//printf("MaximumChargingVoltage = %f \n", _chargingData[_index]->MaximumChargingVoltage);
-
-					// 設定當前輸出
-					if (gun_count == 1)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
-					else if (gun_count == 2)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
-
-					if (priorityLow == 1)
-					{
-						float maxVol, maxCur;
-						// 樁端輸出能力
-						if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
-						{
-							maxVol = _chargingData[_index]->MaximumChargingVoltage;
-							maxCur = _chargingData[_index]->DeratingChargingCurrent;
-						}
-						else
-						{
-							maxVol = _chargingData[_index]->MaximumChargingVoltage;
-							maxCur = _chargingData[_index]->AvailableChargingCurrent;
-						}
-						GetMaxVolAndCurMethod(_index, &maxVol, &maxCur);
-
-						printf("To EV Max_Vol = %f, Cap_Cur = %f \n", maxVol, maxCur);
-						SetChargingPermission(_index, START,
-						_chargingData[_index]->AvailableChargingPower,
-								maxCur,
-								maxVol,
-								_chargingData[_index]->Evboard_id);
-
-						// 取得車端電池資訊 : 1.AC or DC ? 2.Total battery cap, 3.Max battery vol, 4.Max battery cur
-						GetEvBatteryInfo(_index, _chargingData[_index]->Evboard_id);
-					}
-				}
-					break;
-				case S_PREPARING_FOR_EVSE:
-				case S_CCS_PRECHARGE_ST0:
-				case S_CCS_PRECHARGE_ST1:
-				{
-					// 開始確認車端是否同意開始充電
-					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
-
-					// 設定當前輸出
-					if (gun_count == 1)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
-					else if (gun_count == 2)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
-
-					if (priorityLow % 5 == 1)
-					{
-						// 樁端輸出能力改變
-						if (gun_count == 1)
-							SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
-						else if (gun_count == 2)
-							SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
-					}
-
-					// 持續通知 Isolation 測試狀態
-					if (priorityLow == 1)
-					{
-						// 拉 500 V 如果在一秒鐘內 GFD 都符合則 PASS
-						if(_chargingData[_index]->GroundFaultStatus != GFD_WAIT)
-						{
-							SetIsolationStatus(_index, _chargingData[_index]->GroundFaultStatus, _chargingData[_index]->Evboard_id);
-						}
-
-						if(_chargingData[_index]->SystemStatus == S_CCS_PRECHARGE_ST0 &&
-							_chargingData[_index]->PrechargeStatus == PRECHARGE_READY)
-						{
-							SetEvsePrechargeInfo(_index, PRECHARGE_PRERELAY_PASS, _chargingData[_index]->Evboard_id);
-						}
-					}
-				}
-					break;
-				case S_CHARGING:
-				{
-					// 計算 Power
-					_chargingData[_index]->PresentChargingPower = ((float)((_chargingData[_index]->PresentChargingVoltage / 10) * (_chargingData[_index]->PresentChargingCurrent / 10)) / 1000);
-
-					if (chargingTime[_index] == 0)
-					{
-						chargingTime[_index] = _chargingData[_index]->RemainChargingDuration;
-					}
-					else
-					{
-						int passTime = _chargingData[_index]->RemainChargingDuration - chargingTime[_index];
-
-						if (passTime > 0)
-						{
-							_chargingData[_index]->PresentChargedEnergy += (_chargingData[_index]->PresentChargingPower) * passTime / 3600;
-							chargingTime[_index] = _chargingData[_index]->RemainChargingDuration;
-						}
-					}
-
-					// 開始確認車端是否同意開始充電
-					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
-
-					// 設定當前輸出
-					if (gun_count == 1)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
-					else if (gun_count == 2)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
-
-					// for test end
-					if (priorityLow % 5 == 0)
-					{
-						// 樁端輸出能力改變
-						if (gun_count == 1)
-							SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
-						else if (gun_count == 2)
-							SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
-					}
-
-					// GFD 失敗再通知
-					if (priorityLow == 1)
-					{
-						if(_chargingData[_index]->GroundFaultStatus == GFD_FAIL)
-						{
-							SetIsolationStatus(_index, _chargingData[_index]->GroundFaultStatus, _chargingData[_index]->Evboard_id);
-						}
-
-						if(_chargingData[_index]->Type == _Type_CCS &&
-							_chargingData[_index]->PrechargeStatus == PRECHARGE_READY)
-						{
-							SetEvsePrechargeInfo(_index, PRECHARGE_CHARELAY_PASS, _chargingData[_index]->Evboard_id);
-						}
-					}
-				}
-					break;
-				case S_TERMINATING:
-				{
-					// 設定當前輸出
-					if (gun_count == 1)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
-					else if (gun_count == 2)
-						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
-
-					// 槍鎖還在,則代表是樁端要求的停止
-					if (_chargingData[_index]->GunLocked == START)
-					{
-						byte normalStop = 0x01;
-						byte stopReason[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-
-						if (strlen((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[_index]) > 0)
-						{
-							normalStop = 0x02;
-							GetStopChargingReasonByEvse(_index, stopReason);
-						}
-
-						EvseStopChargingEvent(normalStop, stopReason, _chargingData[_index]->Evboard_id);
-					}
-					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
-				}
-					break;
-				case S_COMPLETE:
-				{
-					if (priorityLow == 1)
-					{
-						// 樁端輸出能力
-						if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
-						{
-							SetChargingPermission(_index, STOP,
-									_chargingData[_index]->DeratingChargingPower,
-									_chargingData[_index]->DeratingChargingCurrent,
-									_chargingData[_index]->MaximumChargingVoltage,
-									_chargingData[_index]->Evboard_id);
-						}
-						else
-						{
-							SetChargingPermission(_index, STOP,
-									_chargingData[_index]->AvailableChargingPower,
-									_chargingData[_index]->AvailableChargingCurrent,
-									_chargingData[_index]->MaximumChargingVoltage,
-									_chargingData[_index]->Evboard_id);
-						}
-					}
-				}
-					break;
-			}
-		}
-		priorityLow >= 20 ? priorityLow = 1 : priorityLow++;
-		usleep(45000); //EV 小板通訊 (50 ms)
-	}
-#else
-#endif
-	DEBUG_INFO("Module_EvComm : Can-bus port = %d \n", CanFd);
-	return FAIL;
-}
+#include 	<sys/time.h>
+#include 	<sys/timeb.h>
+#include    <sys/types.h>
+#include    <sys/stat.h>
+#include 	<sys/types.h>
+#include 	<sys/ioctl.h>
+#include 	<sys/socket.h>
+#include 	<sys/ipc.h>
+#include 	<sys/shm.h>
+#include 	<sys/shm.h>
+#include 	<sys/mman.h>
+#include 	<linux/can.h>
+#include 	<linux/can/raw.h>
+#include 	<linux/wireless.h>
+#include 	<arpa/inet.h>
+#include 	<netinet/in.h>
+
+#include 	<unistd.h>
+#include 	<stdarg.h>
+#include    <stdio.h>      /*標準輸入輸出定義*/
+#include    <stdlib.h>     /*標準函數庫定義*/
+#include    <unistd.h>     /*Unix 標準函數定義*/
+#include    <fcntl.h>      /*檔控制定義*/
+#include    <termios.h>    /*PPSIX 終端控制定義*/
+#include    <errno.h>      /*錯誤號定義*/
+#include 	<errno.h>
+#include 	<string.h>
+#include	<time.h>
+#include	<ctype.h>
+#include 	<ifaddrs.h>
+#include	"../../define.h"
+#include 	"Module_EvComm.h"
+
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				-1
+#define START				1
+#define STOP				0
+#define YES					1
+#define NO					0
+
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+struct FanModuleData			*ShmFanModuleData;
+struct CHAdeMOData				*ShmCHAdeMOData;
+struct CcsData					*ShmCcsData;
+
+byte gun_count = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
+int chargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+
+// 限制最大充電電壓,因應不同 type 槍線來限制
+// Chademo : 500V, CCS : 950V
+float maxChargingVol[2] = { 5000, 9500 };			// 限制最大充電電壓,如依照模塊則填上 0
+// 限制最大充電電流與能量透過 Web
+float maxChargingCur[2] = { 0, 0 };					// 限制最大充電電流,如依照模塊則填上 0
+float maxChargingPow = 0;							// 限制最大充電能量,如依照模塊則填上 0
+
+// 槍資訊
+struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+
+struct Ev_Board_Cmd Ev_Cmd={
+		0,
+		0x00000200,
+		0x00000400,
+		0x00000500,
+		0x00000600,
+		0x00000700,
+		0x00000800,
+		0x00000900,
+		0x00000A00,
+		0x00000C00,
+		0x00000D00,
+
+		0x00000E00,
+		0x00000F00,
+		0x00001000,
+		0x00001100,
+
+		0x00001200,
+		0x00001500,
+};
+
+unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
+
+void PRINTF_FUNC(char *string, ...);
+
+void GetMaxVolAndCurMethod(byte index, float *vol, float *cur);
+void GetMaxPowerMethod(float *pow);
+unsigned long GetTimeoutValue(struct timeval _sour_time);
+
+#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)
+
+unsigned long GetTimeoutValue(struct timeval _sour_time)
+{
+	struct timeval _end_time;
+	gettimeofday(&_end_time, NULL);
+
+	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
+}
+
+int StoreLogMsg(const char *fmt, ...)
+{
+	char Buf[4096+256];
+	char buffer[4096];
+	time_t CurrentTime;
+	struct tm *tm;
+	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);
+	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
+			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
+			buffer,
+			tm->tm_year+1900,tm->tm_mon+1);
+	system(Buf);
+
+	return rc;
+}
+
+int DiffTimeb(struct timeb ST, struct timeb ET)
+{
+	//return milli-second
+	unsigned int StartTime,StopTime;
+
+	StartTime=(unsigned int)ST.time;
+	StopTime=(unsigned int)ET.time;
+	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
+}
+
+void PRINTF_FUNC(char *string, ...)
+{
+	if (DEBUG)
+	{
+		va_list args;
+		char buffer[4096];
+
+		va_start(args, string);
+		vsnprintf(buffer, sizeof(buffer), string, args);
+		va_end(args);
+		printf("%s \n", buffer);
+	}
+}
+
+//=================================
+// Common routine
+//=================================
+void getTimeString(char *buff)
+{
+	time_t timep;
+	struct tm *p;
+	time(&timep);
+	p=gmtime(&timep);
+
+	sprintf(buff, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
+}
+
+bool CheckUniqNumber(byte value)
+{
+	for (byte index = 0; index < gun_count; index++)
+	{
+		if (_chargingData[index]->Evboard_id == value)
+		{
+			struct timeval _end_time;
+			gettimeofday(&_end_time, NULL);
+			unsigned long diff = 1000000 *	(_end_time.tv_sec - _id_assign_time.tv_sec) + _end_time.tv_usec - _id_assign_time.tv_usec;
+			if (diff >= 3000000)
+			{
+				gettimeofday(&_id_assign_time, NULL);
+				return true;
+			}
+			else
+			{
+				return false;
+			}
+		}
+	}
+
+	gettimeofday(&_id_assign_time, NULL);
+	return true;
+}
+
+//==========================================
+// Init all share memory
+//==========================================
+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;
+   	 }
+    else
+    {}
+
+   	 //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;
+   	}
+    else
+    {}
+
+   	if(CHAdeMO_QUANTITY > 0)
+   	{
+   		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
+   		{
+   			#ifdef SystemLogMessage
+   		   	DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
+   			#endif
+   			return FAIL;
+   		}
+   		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+   		{
+   			#ifdef SystemLogMessage
+   		   	DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
+   			#endif
+   			return FAIL;
+   		}
+   		else
+   		{}
+   	}
+
+   	if(CCS_QUANTITY > 0)
+   	{
+   		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
+   		{
+   			#ifdef SystemLogMessage
+   			DEBUG_ERROR("shmget ShmCcsData NG \n");
+   			#endif
+   			return FAIL;
+   		}
+   		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
+   			#ifdef SystemLogMessage
+   		   	DEBUG_ERROR("shmat ShmCcsData NG \n");
+   			#endif
+   			return FAIL;
+   		}
+   		else
+   		{}
+   	}
+
+    return result;
+}
+
+//================================================
+// initial can-bus
+//================================================
+int InitCanBus()
+{
+	int 					s0,nbytes;
+	struct timeval			tv;
+	struct ifreq 			ifr0;
+	struct sockaddr_can		addr0;
+
+	system("/sbin/ip link set can0 down");
+	system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
+	system("/sbin/ip link set can0 up");
+
+	s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+
+	tv.tv_sec = 0;
+	tv.tv_usec = 10000;
+   	if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct	timeval)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("Set SO_RCVTIMEO NG");
+		#endif
+	}
+	nbytes=40960;
+	if (setsockopt(s0, SOL_SOCKET,  SO_RCVBUF, &nbytes, sizeof(int)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("Set SO_RCVBUF NG");
+		#endif
+	}
+	nbytes=40960;
+	if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("Set SO_SNDBUF NG");
+		#endif
+	}
+
+   	strcpy(ifr0.ifr_name, "can0" );
+	ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
+	addr0.can_family = AF_CAN;
+	addr0.can_ifindex = ifr0.ifr_ifindex;
+	bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
+	return s0;
+}
+
+//================================================
+//================================================
+// CANBUS receive task
+//================================================
+//================================================
+bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
+{
+	for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
+	{
+		if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
+		{
+			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
+			return true;
+		}
+	}
+
+	for (byte index = 0; index < CCS_QUANTITY; index++)
+	{
+		if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
+		{
+			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
+			return true;
+		}
+	}
+
+	for (byte index = 0; index < GB_QUANTITY; index++)
+	{
+		if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
+		{
+			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
+			return true;
+		}
+	}
+
+	return false;
+}
+
+void AddrAssignment(byte *data)
+{
+	byte target_number[8];
+	byte index = 0x00;
+
+	memcpy(target_number, data, sizeof(target_number));
+	index = *(data + 4);
+
+	if (CheckUniqNumber(index))
+	{
+		PRINTF_FUNC("EV board id = %x \n", index);
+//		PRINTF_FUNC("target_number[0] = %x \n", target_number[0]);
+//		PRINTF_FUNC("target_number[1] = %x \n", target_number[1]);
+//		PRINTF_FUNC("target_number[2] = %x \n", target_number[2]);
+//		PRINTF_FUNC("target_number[3] = %x \n", target_number[3]);
+//		PRINTF_FUNC("target_number[4] = %x \n", target_number[4]);
+
+		PRINTF_FUNC("SetTargetAddr = %d, type = %d \n", index, _chargingData[index - 1]->Type);
+		SetTargetAddr(target_number, index);
+	}
+}
+
+void ClearAbnormalStatus_Chademo(byte gun_index)
+{
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvCommFail = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryMalfun = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoNoPermission = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryIncompatibility = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOVP = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryUVP = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOTP = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryCurrentDiff = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryVoltageDiff = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoShiftPosition = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOtherFault = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargingSystemError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvNormalStop = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoTempSensorBroken = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoConnectorLockFail = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoD1OnNoReceive = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeAllowTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoWaitGfdTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsReqCurrentTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJOffTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayOffTimeout = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan10V = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan20V = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeBeforeStop = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetNormalStop = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoIsolationResultFail = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoMissLinkWithMotherBoard = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoOutputVolMoreThanLimit = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReqCurrentMoreThanLimit = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReCapBmsEqrCurrentExceed = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargeRemainCountDown = 0x00;
+}
+
+void ClearAbnormalStatus_CCS(byte gun_index)
+{
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsRESTemperatureInhibit = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVShiftPosition = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargerConnectorLockFault = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVRESSMalfunction = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingCurrentdifferential = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingVoltageOutOfRange = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingSystemIncompatibility = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEmergencyEvent = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsBreaker = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoData = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_A = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_B = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_C = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_1 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_2 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_3 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_1 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_2 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_3 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_4 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_5 = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequenceError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSignatureError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUnknownSession = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceIDInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPaymentSelectionInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsIdentificationSelectionInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceSelectionInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateExpired = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotYetValid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoCertificateAvailable = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertChainError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertValidationError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertVerificationError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractCanceled = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChallengeInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongEnergyTransferMode = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongChargeParameter = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingProfileInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTariffSelectionInvalid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEPresentVoltageToLow = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryNotApplied = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMeteringSignatureNotValid = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoChargeServiceSelected = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContactorError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotAllowedAtThisEVSE = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsGAChargeStop = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAlignmentError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsACDError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAssociationError = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEChargeAbort = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoSupportedAppProtocol = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractNotAccepted = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMOUnknown = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_Prov_CertificateRevoke = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA1_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA2_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_RootCA_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_Prov_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA1_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA2_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_RootCA_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_Prov_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA1_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA2_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_RootCA_CertificateRevoked = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_SLAC_init = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_match_response = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_sequence = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_match_MNBC = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_avg_atten_calc = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_response = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_match_session = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_assoc_session = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_vald_toggle = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUDP_TT_match_join = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTCP_TT_match_join = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_amp_map_exchange = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_link_ready_notification = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSupportedAppProtocolRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionSetupRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceDiscoveryRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServicePaymentSelectionRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractAuthenticationRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargeParameterDiscoveryRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheckRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPreChargeRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCurrentDemandRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWeldingDetectionRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionStopRes = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequence_Time = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsReadyToCharge_Performance_Time = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCommunicationSetup_Performance_Time = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheck_Performance_Time = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPState_Detection_Time = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPOscillator_Retain_Time = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_TARGET_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_TARGET_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_BATTERY_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_BATTERY_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EV_STOP_EVENT = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EV_STOP_EVENT = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_STOP_EVENT = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_STOP_EVENT = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_MISC_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_MISC_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_REQUEST = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_REQUEST = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_START_BLOCK_TRANSFER = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_START_BLOCK_TRANSFER = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DATA_TRANSFER = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DATA_TRANSFER = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_FINISH = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_FINISH = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_ISOLATION_STATUS = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_ISOLATION_STATUS = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_CONNECTOR_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_CONNECTOR_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_RTC_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_RTC_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_PRECHARGE_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_PRECHARGE_INFO = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMSG_Sequence = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCAN_MSG_Unrecognized_CMD_ID = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Decode_Error = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Encode_Error = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Decode_Error = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Encode_Error = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Decode_Error = 0x00;
+	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Encode_Error = 0x00;
+
+}
+
+void AbnormalStopAnalysis(byte gun_index, byte *errCode)
+{
+	char string[7];
+	sprintf(string, "%d%d%d%d%d%d", *(errCode + 5), *(errCode + 4), *(errCode + 3), *(errCode + 2), *(errCode + 1), *(errCode + 0));
+
+	PRINTF_FUNC("NOTIFICATION_EV_STOP : Err Code = %s \n", string);
+
+	if (strcmp(string, "023700") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvCommFail = 0x01;
+	if (strcmp(string, "023701") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail = 0x01;
+	if (strcmp(string, "023702") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.GbEvCommFail = 0x01;
+	if (strcmp(string, "023703") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = 0x01;
+	if (strcmp(string, "023704") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryMalfun = 0x01;
+	if (strcmp(string, "023705") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoNoPermission = 0x01;
+	if (strcmp(string, "023706") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryIncompatibility = 0x01;
+	if (strcmp(string, "023707") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOVP = 0x01;
+	if (strcmp(string, "023708") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryUVP = 0x01;
+	if (strcmp(string, "023709") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOTP = 0x01;
+	if (strcmp(string, "023710") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryCurrentDiff = 0x01;
+	if (strcmp(string, "023711") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryVoltageDiff = 0x01;
+	if (strcmp(string, "023712") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoShiftPosition = 0x01;
+	if (strcmp(string, "023713") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOtherFault = 0x01;
+	if (strcmp(string, "023714") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargingSystemError = 0x01;
+	if (strcmp(string, "023715") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvNormalStop = 0x01;
+	if (strcmp(string, "023716") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoTempSensorBroken = 0x01;
+	if (strcmp(string, "023717") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoConnectorLockFail = 0x01;
+	if (strcmp(string, "023718") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoD1OnNoReceive = 0x01;
+	if (strcmp(string, "023719") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJTimeout = 0x01;
+	if (strcmp(string, "023720") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeAllowTimeout = 0x01;
+	if (strcmp(string, "023721") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoWaitGfdTimeout = 0x01;
+	if (strcmp(string, "023722") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayTimeout = 0x01;
+	if (strcmp(string, "023723") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsReqCurrentTimeout = 0x01;
+	if (strcmp(string, "023724") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJOffTimeout = 0x01;
+	if (strcmp(string, "023725") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayOffTimeout = 0x01;
+	if (strcmp(string, "023726") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan10V = 0x01;
+	if (strcmp(string, "023727") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan20V = 0x01;
+	if (strcmp(string, "023728") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeBeforeStop = 0x01;
+	if (strcmp(string, "023729") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetNormalStop = 0x01;
+	if (strcmp(string, "023730") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = 0x01;
+	if (strcmp(string, "023731") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoIsolationResultFail = 0x01;
+	if (strcmp(string, "023732") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoMissLinkWithMotherBoard = 0x01;
+	if (strcmp(string, "023733") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoOutputVolMoreThanLimit = 0x01;
+	if (strcmp(string, "023734") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReqCurrentMoreThanLimit = 0x01;
+	if (strcmp(string, "023735") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReCapBmsEqrCurrentExceed = 0x01;
+	if (strcmp(string, "023736") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargeRemainCountDown = 0x01;
+
+	if (strcmp(string, "23737") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsRESTemperatureInhibit = 0x01;
+	if (strcmp(string, "23738") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVShiftPosition = 0x01;
+	if (strcmp(string, "23739") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargerConnectorLockFault = 0x01;
+	if (strcmp(string, "23740") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVRESSMalfunction = 0x01;
+	if (strcmp(string, "23741") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingCurrentdifferential = 0x01;
+	if (strcmp(string, "23742") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingVoltageOutOfRange = 0x01;
+	if (strcmp(string, "23743") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingSystemIncompatibility = 0x01;
+	if (strcmp(string, "23744") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEmergencyEvent = 0x01;
+	if (strcmp(string, "23745") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsBreaker = 0x01;
+	if (strcmp(string, "23746") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoData = 0x01;
+	if (strcmp(string, "23747") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_A = 0x01;
+	if (strcmp(string, "23748") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_B = 0x01;
+	if (strcmp(string, "23749") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_C = 0x01;
+	if (strcmp(string, "23750") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_1 = 0x01;
+	if (strcmp(string, "23751") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_2 = 0x01;
+	if (strcmp(string, "23752") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_3 = 0x01;
+	if (strcmp(string, "23753") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_1 = 0x01;
+	if (strcmp(string, "23754") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_2 = 0x01;
+	if (strcmp(string, "23755") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_3 = 0x01;
+	if (strcmp(string, "23756") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_4 = 0x01;
+	if (strcmp(string, "23757") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_5 = 0x01;
+	if (strcmp(string, "23758") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequenceError = 0x01;
+	if (strcmp(string, "23759") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSignatureError = 0x01;
+	if (strcmp(string, "23760") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUnknownSession = 0x01;
+	if (strcmp(string, "23761") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceIDInvalid = 0x01;
+	if (strcmp(string, "23762") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPaymentSelectionInvalid = 0x01;
+	if (strcmp(string, "23763") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsIdentificationSelectionInvalid = 0x01;
+	if (strcmp(string, "23764") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceSelectionInvalid = 0x01;
+	if (strcmp(string, "23765") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateExpired = 0x01;
+	if (strcmp(string, "23766") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotYetValid = 0x01;
+	if (strcmp(string, "23767") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateRevoked = 0x01;
+	if (strcmp(string, "23768") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoCertificateAvailable = 0x01;
+	if (strcmp(string, "23769") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertChainError = 0x01;
+	if (strcmp(string, "23770") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertValidationError = 0x01;
+	if (strcmp(string, "23771") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertVerificationError = 0x01;
+	if (strcmp(string, "23772") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractCanceled = 0x01;
+	if (strcmp(string, "23773") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChallengeInvalid = 0x01;
+	if (strcmp(string, "23774") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongEnergyTransferMode = 0x01;
+	if (strcmp(string, "23775") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongChargeParameter = 0x01;
+	if (strcmp(string, "23776") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingProfileInvalid = 0x01;
+	if (strcmp(string, "23777") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTariffSelectionInvalid = 0x01;
+	if (strcmp(string, "23778") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEPresentVoltageToLow = 0x01;
+	if (strcmp(string, "23779") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryNotApplied = 0x01;
+	if (strcmp(string, "23780") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMeteringSignatureNotValid = 0x01;
+	if (strcmp(string, "23781") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoChargeServiceSelected = 0x01;
+	if (strcmp(string, "23782") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContactorError = 0x01;
+	if (strcmp(string, "23783") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotAllowedAtThisEVSE = 0x01;
+	if (strcmp(string, "23784") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsGAChargeStop = 0x01;
+	if (strcmp(string, "23785") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAlignmentError = 0x01;
+	if (strcmp(string, "23786") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsACDError = 0x01;
+	if (strcmp(string, "23787") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAssociationError = 0x01;
+	if (strcmp(string, "23788") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEChargeAbort = 0x01;
+	if (strcmp(string, "23789") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoSupportedAppProtocol = 0x01;
+	if (strcmp(string, "23790") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractNotAccepted = 0x01;
+	if (strcmp(string, "23791") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMOUnknown = 0x01;
+	if (strcmp(string, "23792") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_Prov_CertificateRevoke = 0x01;
+	if (strcmp(string, "23793") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA1_CertificateRevoked = 0x01;
+	if (strcmp(string, "23794") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA2_CertificateRevoked = 0x01;
+	if (strcmp(string, "23795") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_RootCA_CertificateRevoked = 0x01;
+	if (strcmp(string, "23796") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_Prov_CertificateRevoked = 0x01;
+	if (strcmp(string, "23797") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA1_CertificateRevoked = 0x01;
+	if (strcmp(string, "23798") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA2_CertificateRevoked = 0x01;
+	if (strcmp(string, "23799") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_RootCA_CertificateRevoked = 0x01;
+	if (strcmp(string, "23800") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_Prov_CertificateRevoked = 0x01;
+	if (strcmp(string, "23801") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA1_CertificateRevoked = 0x01;
+	if (strcmp(string, "23802") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA2_CertificateRevoked = 0x01;
+	if (strcmp(string, "23803") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_RootCA_CertificateRevoked = 0x01;
+	if (strcmp(string, "23809") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_SLAC_init = 0x01;
+	if (strcmp(string, "23810") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_match_response = 0x01;
+	if (strcmp(string, "23811") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_sequence = 0x01;
+	if (strcmp(string, "23812") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_match_MNBC = 0x01;
+	if (strcmp(string, "23813") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_avg_atten_calc = 0x01;
+	if (strcmp(string, "23814") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_response = 0x01;
+	if (strcmp(string, "23815") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_match_session = 0x01;
+	if (strcmp(string, "23816") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_assoc_session = 0x01;
+	if (strcmp(string, "23817") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_vald_toggle = 0x01;
+	if (strcmp(string, "23823") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUDP_TT_match_join = 0x01;
+	if (strcmp(string, "23824") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTCP_TT_match_join = 0x01;
+	if (strcmp(string, "23825") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_amp_map_exchange = 0x01;
+	if (strcmp(string, "23826") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_link_ready_notification = 0x01;
+	if (strcmp(string, "23832") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSupportedAppProtocolRes = 0x01;
+	if (strcmp(string, "23833") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionSetupRes = 0x01;
+	if (strcmp(string, "23834") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceDiscoveryRes = 0x01;
+	if (strcmp(string, "23835") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServicePaymentSelectionRes = 0x01;
+	if (strcmp(string, "23836") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractAuthenticationRes = 0x01;
+	if (strcmp(string, "23837") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargeParameterDiscoveryRes = 0x01;
+	if (strcmp(string, "23838") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryRes = 0x01;
+	if (strcmp(string, "23839") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheckRes = 0x01;
+	if (strcmp(string, "23840") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPreChargeRes = 0x01;
+	if (strcmp(string, "23841") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCurrentDemandRes = 0x01;
+	if (strcmp(string, "23842") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWeldingDetectionRes = 0x01;
+	if (strcmp(string, "23843") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionStopRes = 0x01;
+	if (strcmp(string, "23844") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequence_Time = 0x01;
+	if (strcmp(string, "23845") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsReadyToCharge_Performance_Time = 0x01;
+	if (strcmp(string, "23846") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCommunicationSetup_Performance_Time = 0x01;
+	if (strcmp(string, "23847") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheck_Performance_Time = 0x01;
+	if (strcmp(string, "23848") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPState_Detection_Time = 0x01;
+	if (strcmp(string, "23849") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPOscillator_Retain_Time = 0x01;
+	if (strcmp(string, "23855") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_TARGET_INFO = 0x01;
+	if (strcmp(string, "23856") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_TARGET_INFO = 0x01;
+	if (strcmp(string, "23857") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_BATTERY_INFO = 0x01;
+	if (strcmp(string, "23858") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_BATTERY_INFO = 0x01;
+	if (strcmp(string, "23859") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EV_STOP_EVENT = 0x01;
+	if (strcmp(string, "23860") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EV_STOP_EVENT = 0x01;
+	if (strcmp(string, "23861") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_STOP_EVENT = 0x01;
+	if (strcmp(string, "23862") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_STOP_EVENT = 0x01;
+	if (strcmp(string, "23863") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_MISC_INFO = 0x01;
+	if (strcmp(string, "23864") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_MISC_INFO = 0x01;
+	if (strcmp(string, "23865") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_REQUEST = 0x01;
+	if (strcmp(string, "23866") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_REQUEST = 0x01;
+	if (strcmp(string, "23867") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_START_BLOCK_TRANSFER = 0x01;
+	if (strcmp(string, "23868") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_START_BLOCK_TRANSFER = 0x01;
+	if (strcmp(string, "23869") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DATA_TRANSFER = 0x01;
+	if (strcmp(string, "23870") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DATA_TRANSFER = 0x01;
+	if (strcmp(string, "23871") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_FINISH = 0x01;
+	if (strcmp(string, "23872") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_FINISH = 0x01;
+	if (strcmp(string, "23873") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_ISOLATION_STATUS = 0x01;
+	if (strcmp(string, "23874") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_ISOLATION_STATUS = 0x01;
+	if (strcmp(string, "23875") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_CONNECTOR_INFO = 0x01;
+	if (strcmp(string, "23876") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_CONNECTOR_INFO = 0x01;
+	if (strcmp(string, "23877") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_RTC_INFO = 0x01;
+	if (strcmp(string, "23878") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_RTC_INFO = 0x01;
+	if (strcmp(string, "23879") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_PRECHARGE_INFO = 0x01;
+	if (strcmp(string, "23880") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_PRECHARGE_INFO = 0x01;
+	if (strcmp(string, "23881") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMSG_Sequence = 0x01;
+	if (strcmp(string, "23882") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCAN_MSG_Unrecognized_CMD_ID = 0x01;
+	if (strcmp(string, "23883") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Decode_Error = 0x01;
+	if (strcmp(string, "23884") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Encode_Error = 0x01;
+	if (strcmp(string, "23885") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Decode_Error = 0x01;
+	if (strcmp(string, "23886") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Encode_Error = 0x01;
+	if (strcmp(string, "23887") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Decode_Error = 0x01;
+	if (strcmp(string, "23888") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Encode_Error = 0x01;
+}
+
+void CANReceiver()
+{
+	pid_t canRecPid;
+
+	canRecPid = fork();
+
+	if(canRecPid > 0)
+	{
+		int nbytes;
+		struct can_frame frame;
+		int intCmd;
+		// 槍資訊
+		struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+		struct timeval _cmd_ack_timeout[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+
+		bool isPass = false;
+		while(!isPass)
+		{
+			isPass = true;
+			for (byte _index = 0; _index < gun_count; _index++)
+			{
+				if (!FindChargingInfoData(_index, &_chargingData[0]))
+				{
+					DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
+					isPass = false;
+					break;
+				}
+			}
+		}
+
+		for (byte _index = 0; _index < gun_count; _index++)
+			gettimeofday(&_cmd_ack_timeout[_index], NULL);
+
+		while (1)
+		{
+			memset(&frame, 0, sizeof(struct can_frame));
+			nbytes = read(CanFd, &frame, sizeof(struct can_frame));
+
+			for (byte _index = 0; _index < gun_count; _index++)
+			{
+				if (GetTimeoutValue(_cmd_ack_timeout[_index]) >= 5000000)
+				{
+					// ACK timeout
+					//PRINTF_FUNC("gun = %x, ack timeout \n", _index);
+				}
+			}
+
+			if (nbytes > 0)
+			{
+				byte target;
+				byte targetGun = 0x00;
+				intCmd = (int) (frame.can_id & CAN_EFF_MASK);
+
+				if (intCmd == ADDRESS_REQ)
+				{
+					AddrAssignment(frame.data);
+					continue;
+				}
+
+				intCmd = (int) (frame.can_id & CAN_EFF_MASK & 0xFFFFFF00);
+				target = ((byte) (frame.can_id & 0x000000FF));		// 0x01 or 0x02
+
+				for (byte _index = 0; _index < gun_count; _index++)
+				{
+					if (_chargingData[_index]->Evboard_id == target)
+					{
+						targetGun = _index;
+						break;
+					}
+				}
+
+				if(targetGun < 0 || targetGun >= CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY)
+				{
+					PRINTF_FUNC("EvComm (CANReceiver) : Target index = %x is < 0 or > QUANTITY \n", targetGun);
+					continue;
+				}
+				if(intCmd == 256)
+				{
+					continue;
+				}
+
+				gettimeofday(&_cmd_ack_timeout[targetGun], NULL);
+				switch (intCmd)
+				{
+					case NOTIFICATION_EV_STATUS:
+					{
+						_chargingData[targetGun]->ConnectorPlugIn = frame.data[0];
+						_chargingData[targetGun]->PilotVoltage = frame.data[1];
+
+						//PRINTF_FUNC("index = %d, ConnectorPlugIn = %x, data[0] = %x \n", targetGun, _chargingData[targetGun]->ConnectorPlugIn, frame.data[0]);
+						//PRINTF_FUNC("ConnectorPlugIn = %x \n", (-120 + frame.data[1]) / 10);
+					}
+						break;
+					case ACK_EV_FW_VERSION:
+					{
+						if (_chargingData[targetGun]->Type == _Type_Chademo)
+						{
+							memcpy(ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
+							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
+							PRINTF_FUNC("chademo ver. : %s\n", ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].version);
+						}
+						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
+						{
+							if (ShmCcsData->CommProtocol == 0x01)
+							{
+								memcpy(&ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
+								ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
+								PRINTF_FUNC("CCS FW = %s \n", ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].version);
+							}
+						}
+					}
+						break;
+					case ACK_EV_HW_VERSION:
+					{
+						//PRINTF_FUNC("Get EV HW = %s \n", frame.data);
+					}
+						break;
+					case ACK_GET_OUTPUT_REQ:
+					{
+						_chargingData[targetGun]->EvBatterySoc = frame.data[1];
+						_chargingData[targetGun]->EvBatterytargetVoltage = ((short) frame.data[3] << 8) + (short) frame.data[2];
+						_chargingData[targetGun]->EvBatterytargetCurrent = ((short) frame.data[5] << 8) + (short) frame.data[4];
+						_chargingData[targetGun]->PresentChargedDuration = ((short) frame.data[7] << 8) + (short) frame.data[6];
+
+						if (_chargingData[targetGun]->Type == _Type_Chademo)
+						{
+							if (ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].EvDetection != frame.data[0])
+							{
+								ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
+							}
+
+							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].EvDetection = frame.data[0];
+							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].SOC = _chargingData[targetGun]->EvBatterySoc;
+							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].TargetBatteryVoltage = _chargingData[targetGun]->EvBatterytargetVoltage;
+							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].ChargingCurrentRequest = _chargingData[targetGun]->EvBatterytargetCurrent;
+						}
+						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
+						{
+							if(ShmCcsData->CommProtocol == 0x01)
+							{
+								ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
+							}
+						}
+
+						//PRINTF_FUNC("EvBatterytargetVoltage = %f \n", _chargingData[targetGun]->EvBatterytargetVoltage);
+						//PRINTF_FUNC("EvBatterytargetCurrent = %f \n", _chargingData[targetGun]->EvBatterytargetCurrent);
+						//PRINTF_FUNC("BatteryVoltage = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TargetBatteryVoltage);
+						//PRINTF_FUNC("CurrentRequest = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].ChargingCurrentRequest);
+					}
+						break;
+					case ACK_GET_EV_BATTERY_INFO:
+					{
+						//_chargingData[target].EvACorDCcharging = frame.data[0];
+						//_chargingData[target]->TotalBatteryCap = ((float) frame.data[4] << 8) + (short) frame.data[3];
+						_chargingData[targetGun]->EvBatteryMaxVoltage = ((short) frame.data[4] << 8) + (short) frame.data[3];
+						//_chargingData[target]->EvBatteryMaxCurrent = ((float) frame.data[4] << 8) + (short) frame.data[3];
+						//_chargingData[target].MaxiBatteryCurrent = ((short) frame.data[6] << 8) + (short) frame.data[5];
+						if (_chargingData[targetGun]->Type == _Type_Chademo)
+						{
+							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].TotalBatteryCapacity = ((short) frame.data[2] << 8) + (short) frame.data[1];
+							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].MaxiBatteryVoltage = _chargingData[targetGun]->EvBatteryMaxVoltage;
+
+							//PRINTF_FUNC("EvBatteryMaxVoltage = %f \n", _chargingData[target]->EvBatteryMaxVoltage);
+							//PRINTF_FUNC("TotalBatteryCapacity = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TotalBatteryCapacity);
+							//PRINTF_FUNC("MaxiBatteryVoltage = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].MaxiBatteryVoltage);
+						}
+						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
+						{
+
+						}
+					}
+						break;
+					case ACK_GET_MISCELLANEOUS_INFO:
+					{
+						if (_chargingData[targetGun]->Type == _Type_Chademo)
+						{
+							_chargingData[targetGun]->GunLocked = frame.data[0];
+							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureP = frame.data[1];
+							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureN = frame.data[2];
+							_chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
+							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].EvboardStatus = frame.data[7];
+						}
+						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
+						{
+							if (ShmCcsData->CommProtocol == 0x01)
+							{
+								_chargingData[targetGun]->GunLocked = frame.data[0];
+								//ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index]. .ConnectorTemperatureP = frame.data[1];
+								//ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index]. .ConnectorTemperatureN = frame.data[2];
+								_chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
+							}
+						}
+
+						//PRINTF_FUNC("EvboardStatus = %x \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].EvboardStatus);
+						//PRINTF_FUNC("ConnectorPlug locked = %x \n", frame.data[0]);
+						//PRINTF_FUNC("ConnectorTemp 0= %d \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].ConnectorTemperatureP);
+						//PRINTF_FUNC("ConnectorTemp 1= %d \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].ConnectorTemperatureN);
+						//PRINTF_FUNC("PilotVoltage = %x \n", (-120 + frame.data[3]) / 10);
+					}
+						break;
+					case ACK_EVSE_ISOLATION_STATUS:	{}
+						break;
+					case ACK_EVSE_PRECHAGE_INFO:
+					{
+						_chargingData[targetGun]->PrechargeStatus = frame.data[0];
+					}
+						break;
+					case NOTIFICATION_EV_STOP:
+					{
+						// 車端要求停止
+						// frame.data[0] : 0x01 => normal stop, 0x02 => ev emergency stop
+						PRINTF_FUNC("NOTIFICATION_EV_STOP -----------------------------\n");
+						if (frame.data[0] == 0x02)
+						{
+							AbnormalStopAnalysis(targetGun, frame.data + 1);
+						}
+						_chargingData[targetGun]->StopChargeFlag = YES;
+					}
+						break;
+					default:
+						PRINTF_FUNC("EV board = %d, Ack none defined. intCmd = %d  \n", targetGun, intCmd);
+						break;
+				}
+			}
+			usleep(10000);
+		}
+	}
+}
+
+//================================================
+// Main process
+//================================================
+// 檢查 Byte 中某個 Bit 的值
+// _byte : 欲改變的 byte
+// _bit : 該 byte 的第幾個 bit
+unsigned char EvDetectionStatus(unsigned char _byte, unsigned char _bit)
+{
+	return ( _byte & mask_table[_bit] ) != 0x00;
+}
+
+bool IsConnectorPlugIn(struct ChargingInfoData *chargingData)
+{
+	return (chargingData->ConnectorPlugIn == 0x01) ? true : false;
+}
+
+void SetPresentChargingOutputPower(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
+{
+	float vol1 = 0, cur1 = 0;
+	float vol2 = 0, cur2 = 0;
+
+	//PRINTF_FUNC("f vol - 0 = %f \n", chargingData_1->FireChargingVoltage);
+	//PRINTF_FUNC("f cur - 0 = %f \n", chargingData_1->PresentChargingCurrent);
+	//PRINTF_FUNC("***********************f vol - 1 = %f \n", chargingData_2->FireChargingVoltage);
+	//PRINTF_FUNC("***********************f cur - 1 = %f \n", chargingData_2->PresentChargingCurrent);
+
+	vol1 = chargingData_1->FireChargingVoltage;
+	cur1 = chargingData_1->PresentChargingCurrent;
+
+	vol2 = chargingData_2->FireChargingVoltage;
+	cur2 = chargingData_2->PresentChargingCurrent;
+
+	SetPresentOutputPower(vol1, cur1, vol2, cur2);
+}
+
+void SetPresentChargingOutputCap(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
+{
+	float pow1 = 0, cur1 = 0;
+	float pow2 = 0, cur2 = 0;
+	float vol = 0;
+
+	if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
+	{
+		pow1 = chargingData_1->DeratingChargingPower;
+		cur1 = chargingData_1->DeratingChargingCurrent;
+	}
+	else
+	{
+		pow1 = chargingData_1->AvailableChargingPower;
+		cur1 = chargingData_1->AvailableChargingCurrent;
+	}
+	vol = chargingData_1->MaximumChargingVoltage;
+	GetMaxVolAndCurMethod(chargingData_1->Index, &vol, &cur1);
+	GetMaxPowerMethod(&pow1);
+
+	if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
+	{
+		pow2 = chargingData_2->DeratingChargingPower;
+		cur2 = chargingData_2->DeratingChargingCurrent;
+	}
+	else
+	{
+		pow2 = chargingData_2->AvailableChargingPower;
+		cur2 = chargingData_2->AvailableChargingCurrent;
+	}
+	vol = chargingData_2->MaximumChargingVoltage;
+	GetMaxVolAndCurMethod(chargingData_2->Index, &vol, &cur2);
+	GetMaxPowerMethod(&pow2);
+
+	PRINTF_FUNC("To EV Power_1 = %f, Cur_1 = %f, Power_2 = %f, Cur_2 = %f \n", pow1, cur1, pow2, cur2);
+	SetPresentOutputCapacity(pow1, cur1, pow2, cur2);
+}
+
+void Initialization()
+{
+	bool isPass = false;
+	while(!isPass)
+	{
+		isPass = true;
+		for (byte _index = 0; _index < gun_count; _index++)
+		{
+			if (!FindChargingInfoData(_index, &_chargingData[0]))
+			{
+				DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
+				isPass = false;
+				break;
+			}
+		}
+	}
+}
+
+void GetMaxVolAndCurMethod(byte index, float *vol, float *cur)
+{
+	if (maxChargingVol[index] != 0 && maxChargingVol[index] <= *vol)
+		*vol = maxChargingVol[index];
+
+	if (maxChargingCur[index] != 0 && maxChargingCur[index] <= *cur)
+		*cur = maxChargingCur[index];
+}
+
+void GetMaxPowerMethod(float *pow)
+{
+	if (maxChargingPow != 0 && maxChargingPow <= *pow)
+		*pow = maxChargingPow;
+}
+
+byte GetStopChargingReasonByEvse(byte gunIndex, byte *reason)
+{
+	byte result = NO;
+
+	if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == 0x01)
+	{
+		// 012251
+		*(reason + 5) = 0;
+		*(reason + 4)  = 1;
+		*(reason + 3)  = 2;
+		*(reason + 2)  = 2;
+		*(reason + 1)  = 5;
+		*(reason + 0)  = 1;
+		result = YES;
+	}
+	else if (_chargingData[gunIndex]->Type == _Type_Chademo &&
+			ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == 0x01)
+	{
+		// 011012
+		*(reason + 5) = 0;
+		*(reason + 4) = 1;
+		*(reason + 3) = 1;
+		*(reason + 2) = 0;
+		*(reason + 1) = 1;
+		*(reason + 0) = 2;
+		result = YES;
+	}
+	else if (_chargingData[gunIndex]->Type == _Type_CCS_2 &&
+			ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == 0x01)
+	{
+		// 011014
+		*(reason + 5) = 0;
+		*(reason + 4) = 1;
+		*(reason + 3) = 1;
+		*(reason + 2) = 0;
+		*(reason + 1) = 1;
+		*(reason + 0) = 4;
+		result = YES;
+	}
+
+	return result;
+}
+
+int main(int argc, char *argv[])
+{
+	if(InitShareMemory() == FAIL)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("InitShareMemory NG\n");
+		#endif
+		if(ShmStatusCodeData != NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		sleep(5);
+		return 0;
+	}
+
+	Initialization();
+	CanFd = InitCanBus();
+	CANReceiver();
+
+	byte priorityLow = 1;
+	while(CanFd)
+	{
+		for(byte _index = 0; _index < gun_count; _index++)
+		{
+			if (priorityLow == 1)
+			{
+				// 優先權較低 - 只要有回應即不會再詢問
+				if (_chargingData[_index]->Type == _Type_Chademo &&
+						ShmCHAdeMOData->evse[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
+				{
+					GetFirmwareVersion(_index, _chargingData[_index]->Evboard_id);
+					GetHardwareVersion(_index, _chargingData[_index]->Evboard_id);
+				}
+				else if (_chargingData[_index]->Type == _Type_CCS_2)
+				{
+					if (ShmCcsData->CommProtocol == 0x01 &&
+						ShmCcsData->V2GMessage_DIN70121[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
+					{
+						GetFirmwareVersion(_index, _chargingData[_index]->Evboard_id);
+						GetHardwareVersion(_index, _chargingData[_index]->Evboard_id);
+					}
+				}
+
+				// 固定要取得的資訊 : 1.槍鎖狀態, 2."Connector 1" 溫度, 3."Connector 2" 溫度, 4.Pilot Voltage
+				//PRINTF_FUNC("GetMiscellaneousInfo. index = %d, Eid = %d \n", _index, _chargingData[_index]->Evboard_id);
+				GetMiscellaneousInfo(_index, _chargingData[_index]->Evboard_id);
+			}
+
+			switch (_chargingData[_index]->SystemStatus)
+			{
+				case S_IDLE:
+				case S_RESERVATION:
+					_chargingData[_index]->PresentChargedEnergy = 0;
+					_chargingData[_index]->PresentChargingPower = 0;
+					_chargingData[_index]->GroundFaultStatus = GFD_WAIT;
+					_chargingData[_index]->StopChargeFlag = NO;
+					chargingTime[_index] = 0;
+
+					if (_chargingData[_index]->Type == _Type_Chademo)
+					{
+						ClearAbnormalStatus_Chademo(_index);
+					}
+					else if (_chargingData[_index]->Type == _Type_CCS_2)
+					{
+						ClearAbnormalStatus_CCS(_index);
+					}
+
+					if (priorityLow == 1)
+					{
+						maxChargingCur[_index] = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
+						maxChargingPow = ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
+					}
+					break;
+				case S_PREPARING_FOR_EV:
+				{
+					// 開始確認車端是否同意開始充電 : 1.SOC, 2.Target Vol, 3.Target Cur, 4.Charging remaining time
+					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
+
+					//PRINTF_FUNC("PresentChargingVoltage = %f \n", _chargingData[_index]->PresentChargingVoltage);
+					//PRINTF_FUNC("PresentChargingCurrent = %f \n", _chargingData[_index]->PresentChargingCurrent);
+					//PRINTF_FUNC("AvailableChargingPower = %f \n", _chargingData[_index]->AvailableChargingPower);
+					//PRINTF_FUNC("AvailableChargingCurrent = %f \n", _chargingData[_index]->AvailableChargingCurrent);
+					//PRINTF_FUNC("MaximumChargingVoltage = %f \n", _chargingData[_index]->MaximumChargingVoltage);
+
+					// 設定當前輸出
+					if (gun_count == 1)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
+					else if (gun_count == 2)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
+
+					if (priorityLow == 1)
+					{
+						float maxVol, maxCur;
+						// 樁端輸出能力
+						if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
+						{
+							maxVol = _chargingData[_index]->MaximumChargingVoltage;
+							maxCur = _chargingData[_index]->DeratingChargingCurrent;
+						}
+						else
+						{
+							maxVol = _chargingData[_index]->MaximumChargingVoltage;
+							maxCur = _chargingData[_index]->AvailableChargingCurrent;
+						}
+						GetMaxVolAndCurMethod(_index, &maxVol, &maxCur);
+
+						PRINTF_FUNC("To EV Max_Vol = %f, Cap_Cur = %f \n", maxVol, maxCur);
+						SetChargingPermission(_index, START,
+						_chargingData[_index]->AvailableChargingPower,
+								maxCur,
+								maxVol,
+								_chargingData[_index]->Evboard_id);
+
+						// 取得車端電池資訊 : 1.AC or DC ? 2.Total battery cap, 3.Max battery vol, 4.Max battery cur
+						GetEvBatteryInfo(_index, _chargingData[_index]->Evboard_id);
+					}
+				}
+					break;
+				case S_PREPARING_FOR_EVSE:
+				case S_CCS_PRECHARGE_ST0:
+				case S_CCS_PRECHARGE_ST1:
+				{
+					// 開始確認車端是否同意開始充電
+					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
+
+					// 設定當前輸出
+					if (gun_count == 1)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
+					else if (gun_count == 2)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
+
+					if (priorityLow % 5 == 1)
+					{
+						// 樁端輸出能力改變
+						if (gun_count == 1)
+							SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
+						else if (gun_count == 2)
+							SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
+					}
+
+					// 持續通知 Isolation 測試狀態
+					if (priorityLow == 1)
+					{
+						// 拉 500 V 如果在一秒鐘內 GFD 都符合則 PASS
+						if(_chargingData[_index]->GroundFaultStatus != GFD_WAIT)
+						{
+							SetIsolationStatus(_index, _chargingData[_index]->GroundFaultStatus, _chargingData[_index]->Evboard_id);
+						}
+
+						if(_chargingData[_index]->SystemStatus == S_CCS_PRECHARGE_ST0 &&
+							_chargingData[_index]->PrechargeStatus == PRECHARGE_READY)
+						{
+							SetEvsePrechargeInfo(_index, PRECHARGE_PRERELAY_PASS, _chargingData[_index]->Evboard_id);
+						}
+					}
+				}
+					break;
+				case S_CHARGING:
+				{
+					// 計算 Power
+					_chargingData[_index]->PresentChargingPower = ((float)((_chargingData[_index]->PresentChargingVoltage / 10) * (_chargingData[_index]->PresentChargingCurrent / 10)) / 1000);
+
+					if (chargingTime[_index] == 0)
+					{
+						chargingTime[_index] = _chargingData[_index]->RemainChargingDuration;
+					}
+					else
+					{
+						int passTime = _chargingData[_index]->RemainChargingDuration - chargingTime[_index];
+
+						if (passTime > 0)
+						{
+							_chargingData[_index]->PresentChargedEnergy += (_chargingData[_index]->PresentChargingPower) * passTime / 3600;
+							chargingTime[_index] = _chargingData[_index]->RemainChargingDuration;
+						}
+					}
+
+					// 開始確認車端是否同意開始充電
+					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
+
+					// 設定當前輸出
+					if (gun_count == 1)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
+					else if (gun_count == 2)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
+
+					// for test end
+					if (priorityLow % 5 == 0)
+					{
+						// 樁端輸出能力改變
+						if (gun_count == 1)
+							SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
+						else if (gun_count == 2)
+							SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
+					}
+
+					// GFD 失敗再通知
+					if (priorityLow == 1)
+					{
+						if(_chargingData[_index]->GroundFaultStatus == GFD_FAIL)
+						{
+							SetIsolationStatus(_index, _chargingData[_index]->GroundFaultStatus, _chargingData[_index]->Evboard_id);
+						}
+
+						if(_chargingData[_index]->Type == _Type_CCS_2 &&
+							_chargingData[_index]->PrechargeStatus == PRECHARGE_READY)
+						{
+							SetEvsePrechargeInfo(_index, PRECHARGE_CHARELAY_PASS, _chargingData[_index]->Evboard_id);
+						}
+					}
+				}
+					break;
+				case S_TERMINATING:
+				{
+					// 設定當前輸出
+					if (gun_count == 1)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
+					else if (gun_count == 2)
+						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
+
+					// 槍鎖還在,則代表是樁端要求的停止
+					if (_chargingData[_index]->GunLocked == START)
+					{
+						byte normalStop = 0x01;
+						byte stopReason[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+						if (GetStopChargingReasonByEvse(_index, stopReason))
+						{
+							normalStop = 0x02;
+						}
+
+						EvseStopChargingEvent(normalStop, stopReason, _chargingData[_index]->Evboard_id);
+					}
+					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
+				}
+					break;
+				case S_COMPLETE:
+				{
+					if (priorityLow == 1)
+					{
+						float maxVol, maxCur;
+
+						// 樁端輸出能力
+						if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
+						{
+							maxVol = _chargingData[_index]->MaximumChargingVoltage;
+							maxCur = _chargingData[_index]->DeratingChargingCurrent;
+						}
+						else
+						{
+							maxVol = _chargingData[_index]->MaximumChargingVoltage;
+							maxCur = _chargingData[_index]->AvailableChargingCurrent;
+						}
+
+						GetMaxVolAndCurMethod(_index, &maxVol, &maxCur);
+						SetChargingPermission(_index, STOP,
+									_chargingData[_index]->AvailableChargingPower,
+									maxCur,
+									maxVol,
+									_chargingData[_index]->Evboard_id);
+					}
+				}
+					break;
+			}
+		}
+		priorityLow >= 20 ? priorityLow = 1 : priorityLow++;
+		usleep(45000); //EV 小板通訊 (50 ms)
+	}
+	DEBUG_INFO("Module_EvComm : Can-bus port = %d \n", CanFd);
+	return FAIL;
+}

BIN
EVSE/Projects/DW30/Apps/Module_EvComm.o


BIN
EVSE/Projects/DW30/Apps/Module_EventLogging


+ 18 - 3
EVSE/Projects/DW30/Apps/Module_EventLogging.c

@@ -28,7 +28,6 @@
 #include 	<ifaddrs.h>
 #include	"../../define.h"
 
-#define Debug
 #define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
 #define PASS				1
 #define FAIL				-1
@@ -36,6 +35,8 @@
 struct SysConfigAndInfo			*ShmSysConfigAndInfo;
 struct StatusCodeData 			*ShmStatusCodeData;
 
+void PRINTF_FUNC(char *string, ...);
+
 int StoreLogMsg(const char *fmt, ...);
 #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)
@@ -75,6 +76,20 @@ int DiffTimeb(struct timeb ST, struct timeb ET)
 	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
 }
 
+void PRINTF_FUNC(char *string, ...)
+{
+	if (DEBUG)
+	{
+		va_list args;
+		char buffer[4096];
+
+		va_start(args, string);
+		vsnprintf(buffer, sizeof(buffer), string, args);
+		va_end(args);
+		printf("%s \n", buffer);
+	}
+}
+
 //=================================
 // Common routine
 //=================================
@@ -233,7 +248,7 @@ int main(void)
 		}
 
 		//check Alarm Status
-		for(ByteCount=0;ByteCount<8;ByteCount++)
+		for(ByteCount=0;ByteCount<11;ByteCount++)
 		{
 			if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
 			{
@@ -263,7 +278,7 @@ int main(void)
 		}
 
 		//check Info Status
-		for(ByteCount=0;ByteCount<8;ByteCount++)
+		for(ByteCount=0;ByteCount<29;ByteCount++)
 		{
 			if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
 			{

BIN
EVSE/Projects/DW30/Apps/Module_EventLogging.o


BIN
EVSE/Projects/DW30/Apps/Module_InternalComm


+ 1498 - 1358
EVSE/Projects/DW30/Apps/Module_InternalComm.c

@@ -1,1358 +1,1498 @@
-#include 	<sys/time.h>
-#include 	<sys/timeb.h>
-#include    <sys/types.h>
-#include    <sys/stat.h>
-#include 	<sys/types.h>
-#include 	<sys/ioctl.h>
-#include 	<sys/socket.h>
-#include 	<sys/ipc.h>
-#include 	<sys/shm.h>
-#include 	<sys/shm.h>
-#include 	<sys/mman.h>
-#include 	<linux/wireless.h>
-#include 	<arpa/inet.h>
-#include 	<netinet/in.h>
-
-#include 	<unistd.h>
-#include 	<stdarg.h>
-#include    <stdio.h>      /*標準輸入輸出定義*/
-#include    <stdlib.h>     /*標準函數庫定義*/
-#include    <unistd.h>     /*Unix 標準函數定義*/
-#include    <fcntl.h>      /*檔控制定義*/
-#include    <termios.h>    /*PPSIX 終端控制定義*/
-#include    <errno.h>      /*錯誤號定義*/
-#include 	<errno.h>
-#include 	<string.h>
-#include	<time.h>
-#include	<ctype.h>
-#include 	<ifaddrs.h>
-#include 	<math.h>
-#include	"../../define.h"
-#include	"internalComm.h"
-#include 	<stdbool.h>
-
-#define Debug
-#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
-#define PASS				1
-#define FAIL				-1
-#define YES					1
-#define NO					0
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct FanModuleData			*ShmFanModuleData;
-struct RelayModuleData			*ShmRelayModuleData;
-struct CHAdeMOData				*ShmCHAdeMOData;
-struct CcsData					*ShmCcsData;
-
-#define VIN_MAX_VOLTAGE		250	// 大於該值 : OVP
-#define VIN_MIN_VOLTAGE		170	// 小於該值 : UVP
-#define VIN_DROP_VOLTAGE	150	// 小於該值 : ac drop
-
-#define VOUT_MAX_VOLTAGE	750
-#define VOUT_MIN_VOLTAGE	150
-#define IOUT_MAX_CURRENT	50
-
-#define MAX_FAN_SPEED		6000
-#define MIN_FAN_SPEED		300
-
-// GFD Status
-#define GFD_IDLE			0
-#define GFD_CABLECHK		1
-#define GFD_PRECHARGE		2
-#define GFD_CHARGING		3
-
-//Warning : 150, PreWarning : 500
-#define GFD_VALUE			500
-
-// 最小切換 Relay 電壓
-#define SELF_TO_CHANGE_RELAY_STATUS			600
-// 透過電壓確認 Relay 是否搭上的依據電壓
-#define CHECK_RELAY_STATUS					300
-#define CHECK_RELAY_STATUS_GAP				100
-// 安全在停止充電程序中斷開 Relay 的電流
-#define SEFETY_SWITCH_RELAY_CUR				20
-// 確認 Relay Welding 電壓
-#define RELAY_WELDING_DET					300
-
-byte gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
-// 槍資訊
-struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-byte gfdChkFailCount[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-
-bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
-
-int Uart5Fd;
-char *relayRs485PortName = "/dev/ttyS5";
-unsigned short fanSpeedSmoothValue = 100;
-
-struct timeval _priority_time;
-
-Ver ver;
-PresentInputVoltage inputVoltage;
-PresentOutputVoltage outputVoltage;
-FanSpeed fanSpeed;
-Temperature temperature;
-AuxPower auxPower;
-Gfd gfd_adc;
-Gfd_config gfd_config;
-Gpio_in gpio_in;
-Gpio_out gpio_out;
-Relay outputRelay;
-Relay regRelay;
-
-int StoreLogMsg(const char *fmt, ...);
-#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)
-
-unsigned long GetTimeoutValue(struct timeval _sour_time)
-{
-	struct timeval _end_time;
-	gettimeofday(&_end_time, NULL);
-
-	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
-}
-
-int StoreLogMsg(const char *fmt, ...)
-{
-	char Buf[4096+256];
-	char buffer[4096];
-	time_t CurrentTime;
-	struct tm *tm;
-	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);
-	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
-			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
-			buffer,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-
-	return rc;
-}
-
-int DiffTimeb(struct timeb ST, struct timeb ET)
-{
-	//return milli-second
-	unsigned int StartTime,StopTime;
-
-	StartTime=(unsigned int)ST.time;
-	StopTime=(unsigned int)ET.time;
-	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
-}
-
-unsigned short MaxValue(unsigned short value1, unsigned short value2)
-{
-	return value1 >= value2 ? value1 : value2;
-}
-
-//==========================================
-// Communication Function
-//==========================================
-void GetFwAndHwVersion_Aux()
-{
-	if (Query_FW_Ver(Uart5Fd, Addr.Aux, &ver) == PASS)
-	{
-		// SystemInfo
-		strcpy((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, ver.Version_FW);
-		printf("s1 = %s \n", ver.Version_FW);
-	}
-
-	if (Query_HW_Ver(Uart5Fd, Addr.Aux, &ver) == PASS)
-	{
-		// SystemInfo
-		strcpy((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, ver.Version_HW);
-		printf("s2 = %s \n", ver.Version_HW);
-	}
-}
-
-void GetFwAndHwVersion_Fan()
-{
-	if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
-	{
-		// FanModuleData
-		strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
-		// SystemInfo
-		strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
-		printf("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
-	}
-
-	if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
-	{
-		// SystemInfo
-		strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
-		printf("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
-	}
-}
-
-void GetFwAndHwVersion_Relay()
-{
-	if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
-	{
-		// FanModuleData
-		strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
-		// SystemInfo
-		strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
-		printf("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
-	}
-
-	if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
-	{
-		// SystemInfo
-		strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
-		printf("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
-	}
-}
-
-void SetModelName_Fan()
-{
-	if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
-	{
-		printf("Set Model name PASS \n");
-	}
-}
-
-void GetTemperature_Aux()
-{
-	memset(temperature.temperature, 0, ARRAY_SIZE(temperature.temperature));
-	if (Query_Temperature(Uart5Fd, Addr.Aux, &temperature) == PASS)
-	{
-		// aux temp
-		// UI 實際顯示溫度轉換
-		//char s[4];
-		//sprintf(s,"%d",(-60 + temperature[2].temperature[4])),
-		//printf("s = %s \n",s);
-
-		printf("Aux temp = %d,%d,%d,%d,%d,%d,%d,%d \n",
-				(-60 + temperature.temperature[0]),
-				(-60 + temperature.temperature[1]),
-				(-60 + temperature.temperature[2]),
-				(-60 + temperature.temperature[3]),
-				(-60 + temperature.temperature[4]),
-				(-60 + temperature.temperature[5]),
-				(-60 + temperature.temperature[6]),
-				(-60 + temperature.temperature[7]));
-	}
-}
-
-void GetTemperature_Relay()
-{
-	memset(temperature.temperature, 0, ARRAY_SIZE(temperature.temperature));
-	if (Query_Temperature(Uart5Fd, Addr.Relay, &temperature) == PASS)
-	{
-		// relay temp
-		printf("Relay temp = %d,%d,%d,%d,%d,%d,%d,%d \n",
-				(-60 + temperature.temperature[0]),
-				(-60 + temperature.temperature[1]),
-				(-60 + temperature.temperature[2]),
-				(-60 + temperature.temperature[3]),
-				(-60 + temperature.temperature[4]),
-				(-60 + temperature.temperature[5]),
-				(-60 + temperature.temperature[6]),
-				(-60 + temperature.temperature[7]));
-	}
-}
-
-// AC 三相輸入電壓
-void GetPresentInputVol()
-{
-	if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
-	{
-		// resolution : 0.1
-		//printf("InputVoltageR = %f \n", inputVoltage.L1N_L12);
-		//printf("InputVoltageS = %f \n", inputVoltage.L2N_L23);
-		//printf("InputVoltageT = %f \n", inputVoltage.L3N_L31);
-
-		ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
-		ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
-		ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
-
-		//********************************************************************************************************//
-		// VIN < 170
-		if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x01;
-		}
-		if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x01;
-		}
-		if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x01;
-		}
-
-		//********************************************************************************************************//
-		// VIN > 250
-		if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x01;
-		}
-		if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x01;
-		}
-		if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x01;
-		}
-		//********************************************************************************************************//
-		// VIN < 150
-		if (inputVoltage.L1N_L12 < VIN_DROP_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x01;
-		}
-		if (inputVoltage.L2N_L23 < VIN_DROP_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x01;
-		}
-		if (inputVoltage.L3N_L31 < VIN_DROP_VOLTAGE)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x01;
-		}
-
-		//********************************************************************************************************//
-		// 150 <= VIN < 160
-//		if (inputVoltage.L1N_L12 >= VIN_MIN_VOLTAGE && inputVoltage.L1N_L12 <= VIN_LOW_VOLTAGE)
-//		{
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x00;
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x00;
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x00;
-//		}
-//		if (inputVoltage.L2N_L23 >= VIN_MIN_VOLTAGE && inputVoltage.L2N_L23 <= VIN_LOW_VOLTAGE)
-//		{
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x00;
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x00;
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x00;
-//		}
-//		if (inputVoltage.L3N_L31 >= VIN_MIN_VOLTAGE && inputVoltage.L3N_L31 <= VIN_LOW_VOLTAGE)
-//		{
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x00;
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x00;
-//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x00;
-//		}
-	}
-}
-
-// 左右槍的 Relay 前後的輸出電壓
-void GetPersentOutputVol()
-{
-	if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
-	{
-//		printf("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
-//		printf("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
-//		printf("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
-//		printf("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
-
-		//printf("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
-		//printf("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
-
-		ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
-		ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
-		ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
-		ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
-
-		for (int index = 0; index < gunCount; index++)
-		{
-			if (index == 0)
-			{
-				if (_chargingData[index]->Evboard_id == 0x01)
-				{
-					_chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
-					_chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
-				}
-				else if (_chargingData[index]->Evboard_id == 0x02)
-				{
-					_chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
-					_chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
-				}
-			}
-			else if (index == 1)
-			{
-				_chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
-				_chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
-			}
-
-			unsigned short Ovp = 0;
-			unsigned short Ocp = 0;
-			//Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] 	// 最大輸出電壓與電池電壓最大值
-			//Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ]		// 最大輸出電流與需求電流最小值
-			if (_chargingData[index]->Type == _Type_Chademo)
-			{
-				Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
-				Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
-				if (_chargingData[index]->PresentChargingVoltage >= Ovp)
-				{
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = 0x01;
-				}
-				if (_chargingData[index]->PresentChargingCurrent >= Ocp)
-				{
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOCP = 0x01;
-				}
-			}
-			else if (_chargingData[index]->Type == _Type_CCS)
-			{
-
-			}
-		}
-	}
-}
-
-// 風扇速度
-void GetFanSpeed()
-{
-	//printf("Get fan board speed \n");
-	if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
-	{
-		ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
-		ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
-		ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
-		ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
-		//printf("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
-		//printf("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
-		//printf("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
-		//printf("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
-		// Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
-		//SysInfoData (SystemFanRotaSpeed)
-	}
-}
-
-// 讀取 Relay 狀態
-void GetRelayOutputStatus()
-{
-	if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
-	{
-		regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
-	}
-}
-
-// 確認 K1 K2 relay 的狀態
-void CheckK1K2RelayOutput(byte index)
-{
-	if (index == 0)
-	{
-		if (_chargingData[index]->Evboard_id == 0x01)
-		{
-			if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
-				_chargingData[index]->RelayK1K2Status = YES;
-			else
-				_chargingData[index]->RelayK1K2Status = NO;
-
-			if(_chargingData[index]->Type == _Type_CCS)
-			{
-				if (regRelay.relay_event.bits.Gun1_N == YES	&& regRelay.relay_event.bits.CCS_Precharge == YES)
-					_chargingData[index]->RelayKPK2Status = YES;
-				else
-					_chargingData[index]->RelayKPK2Status = NO;
-			}
-		}
-		else if (_chargingData[index]->Evboard_id == 0x02)
-		{
-			if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
-				_chargingData[index]->RelayK1K2Status = YES;
-			else
-				_chargingData[index]->RelayK1K2Status = NO;
-
-			if(_chargingData[index]->Type == _Type_CCS)
-			{
-				if (regRelay.relay_event.bits.Gun2_N == YES	&& regRelay.relay_event.bits.CCS_Precharge == YES)
-					_chargingData[index]->RelayKPK2Status = YES;
-				else
-					_chargingData[index]->RelayKPK2Status = NO;
-			}
-
-		}
-	}
-	else if (index == 1)
-	{
-		if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
-			_chargingData[index]->RelayK1K2Status = YES;
-		else
-			_chargingData[index]->RelayK1K2Status = NO;
-
-		if(_chargingData[index]->Type == _Type_CCS)
-		{
-			if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
-				_chargingData[index]->RelayKPK2Status = YES;
-			else
-				_chargingData[index]->RelayKPK2Status = NO;
-		}
-	}
-
-	if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
-		ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
-	else
-		ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
-}
-
-void GetGfdAdc()
-{
-	// define : 每 0.2 ~ 1 秒一次
-		// occur : <= 75k 歐姆 @ 150 - 750 Vdc
-		// warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
-		if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
-		{
-			for (int i = 0; i < gunCount; i++)
-			{
-				if (i == 0)
-				{
-					_chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
-					if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
-					{
-						DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
-								i, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
-					}
-				}
-				else if (i == 1)
-				{
-					_chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
-					if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
-					{
-						DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
-								i, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
-					}
-				}
-			}
-
-			//if (gfd_adc.result_conn1 != 0)
-//			{
-//				printf("******************Resister_conn1 = %d, voltage_conn1 = %d, result_conn1 = %d, step = %d \n",
-//					gfd_adc.Resister_conn1,
-//					gfd_adc.voltage_conn1,
-//					gfd_adc.result_conn1,
-//					gfd_adc.rb_step_1);
-//			}
-		}
-}
-
-void GetGpioInput()
-{
-	if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
-	{
-		// AC Contactor Status
-		//ShmSysConfigAndInfo->SysInfo.AcContactorStatus = gpio_in.AC_Connector;
-
-		if (gpio_in.AC_MainBreaker == 1)
-		{
-			// AC Main Breaker ON
-			printf("RB AC Main Breaker. \n");
-		}
-
-		if (gpio_in.SPD == 1)
-		{
-			// SPD (雷擊保護) ON
-			printf("RB SPD. \n");
-		}
-
-		if (gpio_in.Door_Open == 1)
-		{
-			// Door Open
-			printf("RB Door Open. \n");
-		}
-
-		if (gpio_in.GFD[0] == 1)
-		{
-			// GFD_1 Trigger
-		}
-
-		if (gpio_in.GFD[1] == 1)
-		{
-			// GFD_2 Trigger
-		}
-
-		if (gpio_in.AC_Drop == 1)
-		{
-			// AC Drop
-			printf("RB AC Drop. \n");
-		}
-
-		if (gpio_in.Emergency_IO == 1)
-		{
-			// Emergency IO ON
-			printf("RB Emergency IO ON. \n");
-		}
-
-		if (gpio_in.Button_Emergency_Press == 1)
-		{
-			// Emergency button Press
-		}
-
-		if (gpio_in.Button_On_Press == 1)
-		{
-			// On button Press
-		}
-
-		if (gpio_in.Button_Off_Press == 1)
-		{
-			// Off button Press
-		}
-
-		if (gpio_in.Key_1_Press == 1)
-		{
-			// key 1 press
-		}
-
-		if (gpio_in.Key_2_Press == 1)
-		{
-			// key 2 press
-		}
-
-		if (gpio_in.Key_3_Press == 1)
-		{
-			// key 3 press
-		}
-
-		if (gpio_in.Key_4_Press == 1)
-		{
-			// key 4 press
-		}
-	}
-}
-
-// 5V 12V 24V 48V
-void GetAuxPower()
-{
-	if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
-	{
-		ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
-		ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
-		//ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
-		//ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
-		// aux power voltage
-		//printf("aux1 = %x, \n", auxPower.voltage[0]);
-		//printf("aux2 = %x, \n", auxPower.voltage[1]);
-	}
-}
-
-void SetFanModuleSpeed()
-{
-	// 調整風扇速度要漸進式 : 500 rpm/p
-	if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed ||
-			ShmFanModuleData->PresentFan2Speed != ShmFanModuleData->SetFan2Speed ||
-			ShmFanModuleData->PresentFan3Speed != ShmFanModuleData->SetFan3Speed ||
-			ShmFanModuleData->PresentFan4Speed != ShmFanModuleData->SetFan4Speed)
-	{
-		FanSpeed _fanSpeed;
-
-		unsigned short speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
-		if (speed >= ShmFanModuleData->SetFan1Speed)
-			speed = ShmFanModuleData->SetFan1Speed;
-		_fanSpeed.speed[0] = speed & 0xff;
-		_fanSpeed.speed[1] = (speed >> 8) & 0xff;
-
-		speed = ShmFanModuleData->PresentFan2Speed + fanSpeedSmoothValue;
-		if (speed >= ShmFanModuleData->SetFan2Speed)
-			speed = ShmFanModuleData->SetFan2Speed;
-		_fanSpeed.speed[2] = speed & 0xff;
-		_fanSpeed.speed[3] = (speed >> 8) & 0xff;
-
-		speed = ShmFanModuleData->PresentFan3Speed + fanSpeedSmoothValue;
-		if (speed >= ShmFanModuleData->SetFan3Speed)
-			speed = ShmFanModuleData->SetFan3Speed;
-		_fanSpeed.speed[4] = speed & 0xff;
-		_fanSpeed.speed[5] = (speed >> 8) & 0xff;
-
-		speed = ShmFanModuleData->PresentFan4Speed + fanSpeedSmoothValue;
-		if (speed >= ShmFanModuleData->SetFan4Speed)
-			speed = ShmFanModuleData->SetFan4Speed;
-		_fanSpeed.speed[6] = speed & 0xff;
-		_fanSpeed.speed[7] = (speed >> 8) & 0xff;
-
-		if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
-		{
-			//printf("successfully Fan\n");
-		}
-		//else
-			//printf("fail Fan\n");
-	}
-}
-
-void SetRelayModuleFanSpeed()
-{
-	// 調整風扇速度要漸進式 : 100 rpm/p
-	if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed)
-	{
-		FanSpeed _fanSpeed;
-		unsigned short speed = 0;
-
-		if (ShmFanModuleData->SetFan1Speed > ShmFanModuleData->PresentFan1Speed)
-		{
-			speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
-			if (speed >= ShmFanModuleData->SetFan1Speed)
-				speed = ShmFanModuleData->SetFan1Speed;
-		}
-		else
-		{
-			speed = ShmFanModuleData->PresentFan1Speed - fanSpeedSmoothValue;
-			if (speed <= 0)
-				speed = ShmFanModuleData->SetFan1Speed;
-		}
-
-		_fanSpeed.speed[0] = speed & 0xff;
-		_fanSpeed.speed[1] = (speed >> 8) & 0xff;
-		ShmFanModuleData->PresentFan1Speed = speed;
-
-		Config_Fan_Speed(Uart5Fd, Addr.Relay, &_fanSpeed);
-	}
-}
-
-void GetRelayModuleFanSpeed()
-{
-	printf("Get fan board speed \n");
-	if (Query_Fan_Speed(Uart5Fd, Addr.Relay, &fanSpeed) == PASS)
-	{
-		ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0] + (fanSpeed.speed[1] >> 8);
-		//printf("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
-	}
-}
-
-//==========================================
-// Common Function
-//==========================================
-void SetK1K2RelayStatus(byte index)
-{
-	if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
-	{
-		if (_chargingData[index]->Evboard_id == 0x01)
-		{
-			if(regRelay.relay_event.bits.Gun1_P == YES)
-				outputRelay.relay_event.bits.Gun1_P = NO;
-			else if (regRelay.relay_event.bits.Gun1_N == YES)
-				outputRelay.relay_event.bits.Gun1_N = NO;
-
-			if (_chargingData[index]->Type == _Type_CCS)
-			{
-				if(regRelay.relay_event.bits.CCS_Precharge == YES)
-					outputRelay.relay_event.bits.CCS_Precharge = NO;
-			}
-		}
-		else if (_chargingData[index]->Evboard_id == 0x02)
-		{
-			if(regRelay.relay_event.bits.Gun2_P == YES)
-				outputRelay.relay_event.bits.Gun2_P = NO;
-			else if (regRelay.relay_event.bits.Gun2_N == YES)
-				outputRelay.relay_event.bits.Gun2_N = NO;
-
-			if (_chargingData[index]->Type == _Type_CCS)
-			{
-				if(regRelay.relay_event.bits.CCS_Precharge == YES)
-					outputRelay.relay_event.bits.CCS_Precharge = NO;
-			}
-		}
-	}
-	else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING))
-	{
-		if (_chargingData[index]->Evboard_id == 0x01)
-		{
-			if(regRelay.relay_event.bits.Gun1_N == NO)
-				outputRelay.relay_event.bits.Gun1_N = YES;
-			else if (regRelay.relay_event.bits.Gun1_P == NO)
-				outputRelay.relay_event.bits.Gun1_P = YES;
-		}
-		else if (_chargingData[index]->Evboard_id == 0x02)
-		{
-			if(regRelay.relay_event.bits.Gun2_N == NO)
-				outputRelay.relay_event.bits.Gun2_N = YES;
-			else if (regRelay.relay_event.bits.Gun2_P == NO)
-				outputRelay.relay_event.bits.Gun2_P = YES;
-		}
-	}
-	else if (_chargingData[index]->SystemStatus == S_COMPLETE)
-	{
-		if (_chargingData[index]->PresentChargingCurrent <= SEFETY_SWITCH_RELAY_CUR)
-		{
-			if (_chargingData[index]->Evboard_id == 0x01)
-			{
-				if(regRelay.relay_event.bits.Gun1_P == YES)
-					outputRelay.relay_event.bits.Gun1_P = NO;
-				else if (regRelay.relay_event.bits.Gun1_N == YES)
-					outputRelay.relay_event.bits.Gun1_N = NO;
-			}
-			else if (_chargingData[index]->Evboard_id == 0x02)
-			{
-				if(regRelay.relay_event.bits.Gun2_P == YES)
-					outputRelay.relay_event.bits.Gun2_P = NO;
-				else if (regRelay.relay_event.bits.Gun2_N == YES)
-					outputRelay.relay_event.bits.Gun2_N = NO;
-			}
-		}
-	}
-	else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
-	{
-		if (_chargingData[index]->Evboard_id == 0x01)
-		{
-			if (_chargingData[index]->Type == _Type_CCS)
-			{
-				if (regRelay.relay_event.bits.CCS_Precharge == NO)
-					outputRelay.relay_event.bits.CCS_Precharge = YES;
-				else if (regRelay.relay_event.bits.CCS_Precharge == YES)
-					outputRelay.relay_event.bits.Gun1_P = NO;
-			}
-		}
-		else if (_chargingData[index]->Evboard_id == 0x02)
-		{
-			if (_chargingData[index]->Type == _Type_CCS)
-			{
-				if (regRelay.relay_event.bits.CCS_Precharge == NO)
-					outputRelay.relay_event.bits.CCS_Precharge = YES;
-				else if (regRelay.relay_event.bits.CCS_Precharge == YES)
-					outputRelay.relay_event.bits.Gun2_P = NO;
-			}
-		}
-	}
-	else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
-	{
-		if (_chargingData[index]->Evboard_id == 0x01)
-		{
-			if (_chargingData[index]->Type == _Type_CCS)
-			{
-				if (regRelay.relay_event.bits.Gun1_P == NO)
-					outputRelay.relay_event.bits.Gun1_P = YES;
-				else if(regRelay.relay_event.bits.Gun1_P == YES)
-					outputRelay.relay_event.bits.CCS_Precharge = NO;
-			}
-		}
-		else if (_chargingData[index]->Evboard_id == 0x02)
-		{
-			if (_chargingData[index]->Type == _Type_CCS)
-			{
-				if (regRelay.relay_event.bits.Gun2_P == NO)
-					outputRelay.relay_event.bits.Gun2_P = YES;
-				else if(regRelay.relay_event.bits.Gun2_P == YES)
-					outputRelay.relay_event.bits.CCS_Precharge = NO;
-			}
-		}
-	}
-}
-
-void SetParalleRelayStatus()
-{
-	if (gunCount >= 2)
-	{
-		if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING)
-		{
-			// 初始化~ 不搭橋接
-			if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
-				outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
-			else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
-				outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
-		}
-		else
-		{
-			// 單槍充電中 - 搭上橋接
-			if((_chargingData[0]->IsReadyToCharging == YES && _chargingData[1]->IsReadyToCharging == NO) ||
-					(_chargingData[0]->IsReadyToCharging == NO && _chargingData[1]->IsReadyToCharging == YES))
-			{
-				if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY)
-				{
-					if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
-						outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
-					else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
-						outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
-				}
-				else
-				{
-					if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
-						outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
-					else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
-						outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
-				}
-			}
-			else
-			{
-				// 雙槍充電中~ 不搭橋接
-				if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
-					outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
-				else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
-					outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
-			}
-		}
-	}
-}
-
-//==========================================
-// Init all share memory
-//==========================================
-int InitShareMemory()
-{
-	int result = PASS;
-	int MeterSMId;
-
-	//creat 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;
-	 }
-	 //creat 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;
-	}
-
-	//creat ShmFanModuleData
-	if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData),  0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("shmget ShmFanModuleData NG\n");
-		#endif
-		result = FAIL;
-	}
-	else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("shmat ShmFanModuleData NG\n");
-		#endif
-		result = FAIL;
-	 }
-	 memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
-	 //creat ShmRelayModuleData
-	if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData),  0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
-		#endif
-		result = FAIL;
-	}
-	else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
-		#endif
-		result = FAIL;
-	}
-
-	if(CHAdeMO_QUANTITY > 0)
-	{
-		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
-			#endif
-			return FAIL;
-		}
-		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
-			#ifdef SystemLogMessage
-			DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
-			#endif
-			return FAIL;
-		}
-	}
-
-	if(CCS_QUANTITY > 0)
-	{
-		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR("shmget ShmCcsData NG \n");
-			#endif
-			return FAIL;
-		}
-		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-		{
-			#ifdef SystemLogMessage
-		   	DEBUG_ERROR("shmat ShmCcsData NG \n");
-			#endif
-			return FAIL;
-		}
-	}
-
-
-	return result;
-}
-
-int InitComPort()
-{
-	int fd;
-	struct termios tios;
-
-	fd = open(relayRs485PortName, O_RDWR);
-	if(fd <= 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
-		#endif
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
-		}
-		sleep(5);
-		return -1;
-	}
-	ioctl (fd, TCGETS, &tios);
-	tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
-	tios.c_lflag = 0;
-	tios.c_iflag = 0;
-	tios.c_oflag = 0;
-	tios.c_cc[VMIN]=0;
-	tios.c_cc[VTIME]=(byte)0;		// timeout 0.5 second
-	tios.c_lflag=0;
-	tcflush(fd, TCIFLUSH);
-	ioctl (fd, TCSETS, &tios);
-
-	return fd;
-}
-
-//================================================
-// Main process
-//================================================
-bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
-{
-	for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
-		if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
-				== target) {
-			chargingData[target] =
-					&ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
-			return true;
-		}
-	}
-
-	for (byte index = 0; index < CCS_QUANTITY; index++) {
-		if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
-				== target) {
-			chargingData[target] =
-					&ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
-			return true;
-		}
-	}
-
-	for (byte index = 0; index < GB_QUANTITY; index++) {
-		if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
-				== target) {
-			chargingData[target] =
-					&ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
-			return true;
-		}
-	}
-
-	return false;
-}
-
-void Initialization()
-{
-	bool isPass = false;
-
-	for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
-	{
-		outputRelay.relay_event.relay_status[index] = 0x00;
-	}
-
-	while(!isPass)
-	{
-		isPass = true;
-		for (byte _index = 0; _index < gunCount; _index++)
-		{
-			if (!FindChargingInfoData(_index, &_chargingData[0]))
-			{
-				DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
-				isPass = false;
-				break;
-			}
-		}
-	}
-}
-
-bool IsNoneMatchRelayStatus()
-{
-	bool result = false;
-
-//	printf("Real Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
-//			regRelay.relay_event.bits.AC_Contactor,
-//			regRelay.relay_event.bits.Gun1_P,
-//			regRelay.relay_event.bits.Gun1_N,
-//			regRelay.relay_event.bits.Gun2_P,
-//			regRelay.relay_event.bits.Gun2_N,
-//			regRelay.relay_event.bits.CCS_Precharge,
-//			regRelay.relay_event.bits.Gun1_Parallel_P,
-//			regRelay.relay_event.bits.Gun1_Parallel_N);
-
-	if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
-		(regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
-		(regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
-		(regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
-		(regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
-		(regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
-		(regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
-		(regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
-	{
-		result = true;
-	}
-
-	return result;
-}
-
-void MatchRelayStatus()
-{
-	// 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
-	//regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
-	ShmSysConfigAndInfo->SysInfo.AcContactorStatus  = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
-	regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
-	regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
-	regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
-	regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
-	regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
-	regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
-	regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
-}
-
-void CheckRelayStatusByADC()
-{
-	if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
-			(ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
-	{
-		// Relay 前後電壓一致
-		_chargingData[0]->RelayK1K2Status = 0x01;
-	}
-	else
-		_chargingData[0]->RelayK1K2Status = 0x00;
-
-	if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
-				(ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
-	{
-		// Relay 前後電壓一致
-		_chargingData[1]->RelayK1K2Status = 0x01;
-	}
-	else
-		_chargingData[1]->RelayK1K2Status = 0x00;
-}
-
-void SetGfdConfig(byte index, short resister)
-{
-	gfd_config.index = index;
-		gfd_config.state = resister;
-
-	//printf("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
-	if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
-	{
-//		printf("Set reqVol = %f, resister = %d \n",
-//				gfd_config.reqVol,
-//				gfd_config.resister);
-	}
-}
-
-void CableCheckDetected(byte index)
-{
-	// Cable Check
-	// 當火線上的電壓 = 車端要求的電壓電流
-	// _chargingData[targetGun]->EvBatterytargetVoltage
-	// 才可以開始偵測 1s
-	// Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
-	// Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
-	// SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
-	if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE
-			&& _chargingData[index]->SystemStatus <= S_CHARGING)
-			|| (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0
-					&& _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
-		if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE) {
-			SetGfdConfig(index, GFD_CABLECHK);
-		} else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0
-				&& _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1) {
-			SetGfdConfig(index, GFD_PRECHARGE);
-		} else if (_chargingData[index]->SystemStatus <= S_CHARGING) {
-			SetGfdConfig(index, GFD_CHARGING);
-		}
-	} else if (_chargingData[index]->SystemStatus == S_COMPLETE
-			|| _chargingData[index]->SystemStatus == S_PREPARNING
-			|| _chargingData[index]->SystemStatus == S_IDLE) {
-		SetGfdConfig(index, GFD_IDLE);
-	}
-}
-
-void CheckOutputPowerOverCarReq(byte index)
-{
-	float fireV = _chargingData[index]->FireChargingVoltage;
-	float carV = _chargingData[index]->EvBatterytargetVoltage;
-
-	if (fireV >= (carV + (carV * 0.1)))
-	{
-		printf("[Module_InternalComm]CheckOutputPowerOverCarReq NG \n");
-		_chargingData[index]->StopChargeFlag = YES;
-	}
-}
-
-int main(void)
-{
-	if(InitShareMemory() == FAIL)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("InitShareMemory NG\n");
-		#endif
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}
-		sleep(5);
-		return 0;
-	}
-	// Open Uart5 for RB
-	Uart5Fd = InitComPort();
-	Initialization();
-	sleep(1);
-
-	if(Uart5Fd < 0)
-	{
-		printf ("open port error. \n");
-		return 0;
-	}
-
-	outputRelay.relay_event.bits.AC_Contactor = 0x00;
-	//outputRelay.relay_event.bits.CCS_Precharge = 0x01;
-	//outputRelay.relay_event.bits.Gun1_Parallel_P = 0x01;
-	//outputRelay.relay_event.bits.Gun1_Parallel_N = 0x01;
-	//outputRelay.relay_event.bits.Gun1_P = 0x00;
-	//outputRelay.relay_event.bits.Gun1_N = 0x00;
-	//outputRelay.relay_event.bits.Gun2_P = 0x01;
-	//outputRelay.relay_event.bits.Gun2_N = 0x01;
-	Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay);
-	
-	
-	ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
-	ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
-	ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
-	ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
-	
-	//char *moduleName = "DSYE601E00T2PH";
-	//	memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, moduleName, strlen(moduleName));
-    //SetModelName_Fan();
-    //SetFanModuleSpeed();
-    //return 0;
-	gettimeofday(&_priority_time, NULL);
-	for(;;)
-	{
-		// 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
-		if (ShmRelayModuleData->SelfTest_Comp == NO)
-		{
-			printf("(RB) Get Fw and Hw Ver. \n");
-			GetFwAndHwVersion_Relay();
-			GetFwAndHwVersion_Fan();
-			SetModelName_Fan();
-			sleep(1);
-		}
-		else if (ShmRelayModuleData->SelfTest_Comp == YES)
-		{
-			// ==============優先權最高 10 ms ==============
-			// 輸出電壓
-			GetPersentOutputVol();
-
-			// 三相輸入電壓
-			GetPresentInputVol();
-
-			// 讀取當前 relay 狀態
-			GetRelayOutputStatus();
-
-			bool isCharging = false;
-			for (int i = 0; i < gunCount; i++)
-			{
-				// Cable check (Set)
-				CableCheckDetected(i);
-
-				// check k1 k2 relay 狀態
-				CheckK1K2RelayOutput(i);
-
-				// 依據當前各槍的狀態選擇 搭上/放開 Relay
-				SetK1K2RelayStatus(i);
-
-				if (_chargingData[i]->SystemStatus == S_IDLE)
-					gfdChkFailCount[i] = 0;
-
-				if (_chargingData[i]->SystemStatus == S_BOOTING	||
-					(_chargingData[i]->SystemStatus >= S_PREPARNING && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
-					(_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1))
-				{
-					_chargingData[i]->IsReadyToCharging = YES;
-					isCharging = true;
-
-					if (_chargingData[i]->SystemStatus == S_CHARGING)
-						CheckOutputPowerOverCarReq(i);
-				}
-				else
-					_chargingData[i]->IsReadyToCharging = NO;
-			}
-			// Cable check (Get)
-			GetGfdAdc();
-
-			SetParalleRelayStatus();
-
-			// 搭上 AC Contactor
-			if (isCharging)
-				outputRelay.relay_event.bits.AC_Contactor = YES;
-			else
-				outputRelay.relay_event.bits.AC_Contactor = NO;
-
-			// 搭上/鬆開 Relay
-			// 放開 Relay 之前要先確認輸出的電壓電流是否已經降到某個值
-			if(IsNoneMatchRelayStatus())
-			{
-				if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
-				{
-					printf("Match Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
-							regRelay.relay_event.bits.AC_Contactor,
-							regRelay.relay_event.bits.Gun1_P,
-							regRelay.relay_event.bits.Gun1_N,
-							regRelay.relay_event.bits.Gun2_P,
-							regRelay.relay_event.bits.Gun2_N,
-							regRelay.relay_event.bits.CCS_Precharge,
-							regRelay.relay_event.bits.Gun1_Parallel_P,
-							regRelay.relay_event.bits.Gun1_Parallel_N);
-				}
-			}
-			// 風扇控制
-		    GetFanSpeed();
-
-			if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
-			{
-				gettimeofday(&_priority_time, NULL);
-				if (isCharging)
-				{
-					if (ShmFanModuleData->PresentFan1Speed < MAX_FAN_SPEED)
-					{
-						ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
-						ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
-						ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
-						ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
-					}
-				}
-				else
-				{
-					if (ShmFanModuleData->PresentFan1Speed > MIN_FAN_SPEED)
-					{
-						ShmFanModuleData->SetFan1Speed = MIN_FAN_SPEED;
-						ShmFanModuleData->SetFan2Speed = MIN_FAN_SPEED;
-						ShmFanModuleData->SetFan3Speed = MIN_FAN_SPEED;
-						ShmFanModuleData->SetFan4Speed = MIN_FAN_SPEED;
-					}
-				}
-				SetFanModuleSpeed();
-			}
-		}
-		usleep(10000);
-	}
-
-	return FAIL;
-}
+#include 	<sys/time.h>
+#include 	<sys/timeb.h>
+#include    <sys/types.h>
+#include    <sys/stat.h>
+#include 	<sys/types.h>
+#include 	<sys/ioctl.h>
+#include 	<sys/socket.h>
+#include 	<sys/ipc.h>
+#include 	<sys/shm.h>
+#include 	<sys/shm.h>
+#include 	<sys/mman.h>
+#include 	<linux/wireless.h>
+#include 	<arpa/inet.h>
+#include 	<netinet/in.h>
+
+#include 	<unistd.h>
+#include 	<stdarg.h>
+#include    <stdio.h>      /*標準輸入輸出定義*/
+#include    <stdlib.h>     /*標準函數庫定義*/
+#include    <unistd.h>     /*Unix 標準函數定義*/
+#include    <fcntl.h>      /*檔控制定義*/
+#include    <termios.h>    /*PPSIX 終端控制定義*/
+#include    <errno.h>      /*錯誤號定義*/
+#include 	<errno.h>
+#include 	<string.h>
+#include	<time.h>
+#include	<ctype.h>
+#include 	<ifaddrs.h>
+#include 	<math.h>
+#include	"../../define.h"
+#include	"internalComm.h"
+#include 	<stdbool.h>
+
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				-1
+#define YES					1
+#define NO					0
+#define TEN_MINUTES			600
+
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+struct FanModuleData			*ShmFanModuleData;
+struct RelayModuleData			*ShmRelayModuleData;
+struct CHAdeMOData				*ShmCHAdeMOData;
+struct CcsData					*ShmCcsData;
+
+#define VIN_MAX_VOLTAGE		250	// 大於該值 : OVP
+#define VIN_MIN_VOLTAGE		170	// 小於該值 : UVP
+#define VIN_DROP_VOLTAGE	150	// 小於該值 : ac drop
+
+#define VOUT_MAX_VOLTAGE	750
+#define VOUT_MIN_VOLTAGE	150
+#define IOUT_MAX_CURRENT	50
+
+#define MAX_FAN_SPEED		13500
+#define MIN_FAN_SPEED		2800
+
+// GFD Status
+#define GFD_IDLE			0
+#define GFD_CABLECHK		1
+#define GFD_PRECHARGE		2
+#define GFD_CHARGING		3
+
+// 最小切換 Relay 電壓
+#define SELF_TO_CHANGE_RELAY_STATUS			600
+// 透過電壓確認 Relay 是否搭上的依據電壓
+#define CHECK_RELAY_STATUS					300
+#define CHECK_RELAY_STATUS_GAP				100
+// 安全在停止充電程序中斷開 Relay 的電流
+#define SEFETY_SWITCH_RELAY_CUR				20
+// 確認 Relay Welding 電壓
+#define RELAY_WELDING_DET					300
+
+byte gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
+// 槍資訊
+struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+byte gfdChkFailCount[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+
+bool _isOutputNoneMatch[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+struct timeval _checkOutputNoneMatchTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+
+bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
+
+int Uart5Fd;
+char *relayRs485PortName = "/dev/ttyS5";
+unsigned short fanSpeedSmoothValue = 1000;
+
+bool isStopChargingCount = false;
+bool isSystemBooting = false;
+struct timeval _close_ac_contactor;
+
+struct timeval _priority_time;
+
+Ver ver;
+PresentInputVoltage inputVoltage;
+PresentOutputVoltage outputVoltage;
+FanSpeed fanSpeed;
+Temperature temperature;
+AuxPower auxPower;
+Gfd gfd_adc;
+Gfd_config gfd_config;
+Gpio_in gpio_in;
+Gpio_out gpio_out;
+Relay outputRelay;
+Relay regRelay;
+Rtc rtc;
+
+void PRINTF_FUNC(char *string, ...);
+
+int StoreLogMsg(const char *fmt, ...);
+unsigned long GetTimeoutValue(struct timeval _sour_time);
+
+#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)
+
+unsigned long GetTimeoutValue(struct timeval _sour_time)
+{
+	struct timeval _end_time;
+	gettimeofday(&_end_time, NULL);
+
+	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
+}
+
+int StoreLogMsg(const char *fmt, ...)
+{
+	char Buf[4096+256];
+	char buffer[4096];
+	time_t CurrentTime;
+	struct tm *tm;
+	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);
+	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
+			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
+			buffer,
+			tm->tm_year+1900,tm->tm_mon+1);
+	system(Buf);
+
+	return rc;
+}
+
+int DiffTimeb(struct timeb ST, struct timeb ET)
+{
+	//return milli-second
+	unsigned int StartTime,StopTime;
+
+	StartTime=(unsigned int)ST.time;
+	StopTime=(unsigned int)ET.time;
+	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
+}
+
+unsigned short MaxValue(unsigned short value1, unsigned short value2)
+{
+	return value1 >= value2 ? value1 : value2;
+}
+
+void PRINTF_FUNC(char *string, ...)
+{
+	if (DEBUG)
+	{
+		va_list args;
+		char buffer[4096];
+
+		va_start(args, string);
+		vsnprintf(buffer, sizeof(buffer), string, args);
+		va_end(args);
+		printf("%s \n", buffer);
+	}
+}
+
+//==========================================
+// Communication Function
+//==========================================
+void GetFwAndHwVersion_Fan()
+{
+	if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
+	{
+		// FanModuleData
+		strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
+		// SystemInfo
+		strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
+		//PRINTF_FUNC("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
+	}
+
+	if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
+	{
+		// SystemInfo
+		strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
+		//PRINTF_FUNC("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
+	}
+}
+
+void GetFwAndHwVersion_Relay()
+{
+	if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
+	{
+		// FanModuleData
+		strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
+		// SystemInfo
+		strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
+		//PRINTF_FUNC("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
+	}
+
+	if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
+	{
+		// SystemInfo
+		strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
+		//PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
+	}
+}
+
+void SetRtcData_Relay()
+{
+	struct timeb csuTime;
+	struct tm *tmCSU;
+
+	ftime(&csuTime);
+	tmCSU = localtime(&csuTime.time);
+	//	PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
+	//			tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
+	//			tmCSU->tm_sec);
+
+	rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
+	rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
+	rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
+	rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
+
+	rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
+	rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
+
+	rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
+	rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
+
+	rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
+	rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
+
+	rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
+	rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
+
+	rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
+	rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
+
+	if (Config_Rtc_Data(Uart5Fd, Addr.Relay, &rtc) == PASS)
+	{
+		//PRINTF_FUNC("SetRtc (RB) sucessfully. \n");
+	}
+}
+
+void SetRtcData_Fan()
+{
+	struct timeb csuTime;
+	struct tm *tmCSU;
+
+	ftime(&csuTime);
+	tmCSU = localtime(&csuTime.time);
+	//	PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
+	//			tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
+	//			tmCSU->tm_sec);
+
+	rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
+	rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
+	rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
+	rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
+
+	rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
+	rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
+
+	rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
+	rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
+
+	rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
+	rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
+
+	rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
+	rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
+
+	rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
+	rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
+
+	if (Config_Rtc_Data(Uart5Fd, Addr.Fan, &rtc) == PASS)
+	{
+		//PRINTF_FUNC("SetRtc (FB) sucessfully. \n");
+	}
+}
+
+void SetModelName_Fan()
+{
+	if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
+	{
+		PRINTF_FUNC("Set Model name PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
+	}
+}
+
+// AC 三相輸入電壓
+void GetPresentInputVol()
+{
+	if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
+	{
+		// resolution : 0.1
+		//PRINTF_FUNC("InputVoltageR = %f \n", inputVoltage.L1N_L12);
+		//PRINTF_FUNC("InputVoltageS = %f \n", inputVoltage.L2N_L23);
+		//PRINTF_FUNC("InputVoltageT = %f \n", inputVoltage.L3N_L31);
+
+		ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
+		ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
+		ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
+
+		//********************************************************************************************************//
+		// VIN < 170
+		if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x01;
+		}
+		if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x01;
+		}
+		if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x01;
+		}
+
+		//********************************************************************************************************//
+		// VIN > 250
+		if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE)
+		{
+			//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x01;
+		}
+		if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE)
+		{
+			//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x01;
+		}
+		if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE)
+		{
+			//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x01;
+		}
+		//********************************************************************************************************//
+		// VIN < 150
+		if (inputVoltage.L1N_L12 < VIN_DROP_VOLTAGE)
+		{
+			//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x01;
+		}
+		if (inputVoltage.L2N_L23 < VIN_DROP_VOLTAGE)
+		{
+			//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x01;
+		}
+		if (inputVoltage.L3N_L31 < VIN_DROP_VOLTAGE)
+		{
+			//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x01;
+		}
+
+		//********************************************************************************************************//
+		// 150 <= VIN < 160
+//		if (inputVoltage.L1N_L12 >= VIN_MIN_VOLTAGE && inputVoltage.L1N_L12 <= VIN_LOW_VOLTAGE)
+//		{
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x00;
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x00;
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x00;
+//		}
+//		if (inputVoltage.L2N_L23 >= VIN_MIN_VOLTAGE && inputVoltage.L2N_L23 <= VIN_LOW_VOLTAGE)
+//		{
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x00;
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x00;
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x00;
+//		}
+//		if (inputVoltage.L3N_L31 >= VIN_MIN_VOLTAGE && inputVoltage.L3N_L31 <= VIN_LOW_VOLTAGE)
+//		{
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x00;
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x00;
+//			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x00;
+//		}
+	}
+}
+
+// 左右槍的 Relay 前後的輸出電壓
+void GetPersentOutputVol()
+{
+	if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
+	{
+//		PRINTF_FUNC("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
+//		PRINTF_FUNC("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
+//		PRINTF_FUNC("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
+//		PRINTF_FUNC("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
+
+		//PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
+		//PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
+
+		ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
+		ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
+		ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
+		ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
+
+		for (int index = 0; index < gunCount; index++)
+		{
+			if (index == 0)
+			{
+				if (_chargingData[index]->Evboard_id == 0x01)
+				{
+					_chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
+					_chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
+				}
+				else if (_chargingData[index]->Evboard_id == 0x02)
+				{
+					_chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
+					_chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
+				}
+			}
+			else if (index == 1)
+			{
+				_chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
+				_chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
+			}
+
+			unsigned short Ovp = 0;
+			unsigned short Ocp = 0;
+			//Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] 	// 最大輸出電壓與電池電壓最大值
+			//Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ]		// 最大輸出電流與需求電流最小值
+			if (_chargingData[index]->Type == _Type_Chademo)
+			{
+				Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
+				Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
+				if (_chargingData[index]->PresentChargingVoltage >= Ovp)
+				{
+					//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = 0x01;
+				}
+				if (_chargingData[index]->PresentChargingCurrent >= Ocp)
+				{
+					//ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOCP = 0x01;
+				}
+			}
+			else if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+
+			}
+		}
+	}
+}
+
+// 風扇速度
+void GetFanSpeed()
+{
+	//PRINTF_FUNC("Get fan board speed \n");
+	if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
+	{
+		ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
+		ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
+		ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
+		ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
+//		PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
+//		PRINTF_FUNC("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
+//		PRINTF_FUNC("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
+//		PRINTF_FUNC("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
+		// Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
+		//SysInfoData (SystemFanRotaSpeed)
+	}
+}
+
+// 讀取 Relay 狀態
+void GetRelayOutputStatus()
+{
+	if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
+	{
+		regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
+	}
+}
+
+// 確認 K1 K2 relay 的狀態
+void CheckK1K2RelayOutput(byte index)
+{
+	if (index == 0)
+	{
+		if (_chargingData[index]->Evboard_id == 0x01)
+		{
+			if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
+				_chargingData[index]->RelayK1K2Status = YES;
+			else
+				_chargingData[index]->RelayK1K2Status = NO;
+
+			if(_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if (regRelay.relay_event.bits.Gun1_N == YES	&& regRelay.relay_event.bits.CCS_Precharge == YES)
+					_chargingData[index]->RelayKPK2Status = YES;
+				else
+					_chargingData[index]->RelayKPK2Status = NO;
+			}
+		}
+		else if (_chargingData[index]->Evboard_id == 0x02)
+		{
+			if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
+				_chargingData[index]->RelayK1K2Status = YES;
+			else
+				_chargingData[index]->RelayK1K2Status = NO;
+
+			if(_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if (regRelay.relay_event.bits.Gun2_N == YES	&& regRelay.relay_event.bits.CCS_Precharge == YES)
+					_chargingData[index]->RelayKPK2Status = YES;
+				else
+					_chargingData[index]->RelayKPK2Status = NO;
+			}
+
+		}
+	}
+	else if (index == 1)
+	{
+		if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
+			_chargingData[index]->RelayK1K2Status = YES;
+		else
+			_chargingData[index]->RelayK1K2Status = NO;
+
+		if(_chargingData[index]->Type == _Type_CCS_2)
+		{
+			if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
+				_chargingData[index]->RelayKPK2Status = YES;
+			else
+				_chargingData[index]->RelayKPK2Status = NO;
+		}
+	}
+
+	if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
+		ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
+	else
+		ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
+}
+
+void GetGfdAdc()
+{
+	// define : 每 0.2 ~ 1 秒一次
+	// occur : <= 75k 歐姆 @ 150 - 750 Vdc
+	// warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
+	if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
+	{
+		for (int i = 0; i < gunCount; i++)
+		{
+			if (i == 0)
+			{
+				_chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
+				if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
+				{
+					DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
+							i, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
+				}
+			}
+			else if (i == 1)
+			{
+				_chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
+				if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
+				{
+					DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
+							i, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
+				}
+			}
+		}
+
+		//if (gfd_adc.result_conn1 != 0)
+		{
+//			PRINTF_FUNC("******************Resister_conn1 = %d, voltage_conn1 = %d, result_conn1 = %d, step = %d \n",
+//				gfd_adc.Resister_conn1,
+//				gfd_adc.voltage_conn1,
+//				gfd_adc.result_conn1,
+//				gfd_adc.rb_step_1);
+		}
+	}
+}
+
+void GetGpioInput()
+{
+	if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
+	{
+		// AC Contactor Status
+		if (gpio_in.AC_MainBreaker == 1)
+		{
+			// AC Main Breaker ON
+			PRINTF_FUNC("RB AC Main Breaker. \n");
+		}
+
+		if (gpio_in.SPD == 1)
+		{
+			// SPD (雷擊保護) ON
+			PRINTF_FUNC("RB SPD. \n");
+		}
+
+		if (gpio_in.Door_Open == 1)
+		{
+			// Door Open
+			PRINTF_FUNC("RB Door Open. \n");
+		}
+
+		if (gpio_in.GFD[0] == 1)
+		{
+			// GFD_1 Trigger
+		}
+
+		if (gpio_in.GFD[1] == 1)
+		{
+			// GFD_2 Trigger
+		}
+
+		if (gpio_in.AC_Drop == 1)
+		{
+			// AC Drop
+			PRINTF_FUNC("RB AC Drop. \n");
+		}
+
+		if (gpio_in.Emergency_IO == 1)
+		{
+			// Emergency IO ON
+			PRINTF_FUNC("RB Emergency IO ON. \n");
+		}
+
+		if (gpio_in.Button_Emergency_Press == 1)
+		{
+			// Emergency button Press
+		}
+
+		if (gpio_in.Button_On_Press == 1)
+		{
+			// On button Press
+		}
+
+		if (gpio_in.Button_Off_Press == 1)
+		{
+			// Off button Press
+		}
+
+		if (gpio_in.Key_1_Press == 1)
+		{
+			// key 1 press
+		}
+
+		if (gpio_in.Key_2_Press == 1)
+		{
+			// key 2 press
+		}
+
+		if (gpio_in.Key_3_Press == 1)
+		{
+			// key 3 press
+		}
+
+		if (gpio_in.Key_4_Press == 1)
+		{
+			// key 4 press
+		}
+	}
+}
+
+// 5V 12V 24V 48V
+void GetAuxPower()
+{
+	if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
+	{
+		ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
+		ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
+		//ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
+		//ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
+		// aux power voltage
+		//PRINTF_FUNC("aux1 = %x, \n", auxPower.voltage[0]);
+		//PRINTF_FUNC("aux2 = %x, \n", auxPower.voltage[1]);
+	}
+}
+
+void SetFanModuleSpeed()
+{
+	// 調整風扇速度要漸進式 : 500 rpm/p
+	if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed ||
+			ShmFanModuleData->PresentFan2Speed != ShmFanModuleData->SetFan2Speed ||
+			ShmFanModuleData->PresentFan3Speed != ShmFanModuleData->SetFan3Speed ||
+			ShmFanModuleData->PresentFan4Speed != ShmFanModuleData->SetFan4Speed)
+	{
+		FanSpeed _fanSpeed;
+
+		unsigned short speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
+		if (speed >= ShmFanModuleData->SetFan1Speed)
+			speed = ShmFanModuleData->SetFan1Speed;
+		_fanSpeed.speed[0] = speed;
+
+		speed = ShmFanModuleData->PresentFan2Speed + fanSpeedSmoothValue;
+		if (speed >= ShmFanModuleData->SetFan2Speed)
+			speed = ShmFanModuleData->SetFan2Speed;
+		_fanSpeed.speed[1] = speed;
+
+		speed = ShmFanModuleData->PresentFan3Speed + fanSpeedSmoothValue;
+		if (speed >= ShmFanModuleData->SetFan3Speed)
+			speed = ShmFanModuleData->SetFan3Speed;
+		_fanSpeed.speed[2] = speed;
+
+		speed = ShmFanModuleData->PresentFan4Speed + fanSpeedSmoothValue;
+		if (speed >= ShmFanModuleData->SetFan4Speed)
+			speed = ShmFanModuleData->SetFan4Speed;
+		_fanSpeed.speed[3] = speed;
+
+		if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
+		{
+			//PRINTF_FUNC("successfully Fan\n");
+		}
+	}
+}
+
+void SetRelayModuleFanSpeed()
+{
+	// 調整風扇速度要漸進式 : 100 rpm/p
+	if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed)
+	{
+		FanSpeed _fanSpeed;
+		unsigned short speed = 0;
+
+		if (ShmFanModuleData->SetFan1Speed > ShmFanModuleData->PresentFan1Speed)
+		{
+			speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
+			if (speed >= ShmFanModuleData->SetFan1Speed)
+				speed = ShmFanModuleData->SetFan1Speed;
+		}
+		else
+		{
+			speed = ShmFanModuleData->PresentFan1Speed - fanSpeedSmoothValue;
+			if (speed <= 0)
+				speed = ShmFanModuleData->SetFan1Speed;
+		}
+
+		_fanSpeed.speed[0] = speed & 0xff;
+		_fanSpeed.speed[1] = (speed >> 8) & 0xff;
+		ShmFanModuleData->PresentFan1Speed = speed;
+
+		Config_Fan_Speed(Uart5Fd, Addr.Relay, &_fanSpeed);
+	}
+}
+
+void GetRelayModuleFanSpeed()
+{
+	PRINTF_FUNC("Get fan board speed \n");
+	if (Query_Fan_Speed(Uart5Fd, Addr.Relay, &fanSpeed) == PASS)
+	{
+		ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0] + (fanSpeed.speed[1] >> 8);
+		PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
+	}
+}
+
+//==========================================
+// Common Function
+//==========================================
+void SetK1K2RelayStatus(byte index)
+{
+	if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
+	{
+		if (_chargingData[index]->Evboard_id == 0x01)
+		{
+			if(regRelay.relay_event.bits.Gun1_P == YES)
+				outputRelay.relay_event.bits.Gun1_P = NO;
+			else if (regRelay.relay_event.bits.Gun1_N == YES)
+				outputRelay.relay_event.bits.Gun1_N = NO;
+
+			if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if(regRelay.relay_event.bits.CCS_Precharge == YES)
+					outputRelay.relay_event.bits.CCS_Precharge = NO;
+			}
+		}
+		else if (_chargingData[index]->Evboard_id == 0x02)
+		{
+			if(regRelay.relay_event.bits.Gun2_P == YES)
+				outputRelay.relay_event.bits.Gun2_P = NO;
+			else if (regRelay.relay_event.bits.Gun2_N == YES)
+				outputRelay.relay_event.bits.Gun2_N = NO;
+
+			if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if(regRelay.relay_event.bits.CCS_Precharge == YES)
+					outputRelay.relay_event.bits.CCS_Precharge = NO;
+			}
+		}
+	}
+	else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING))
+	{
+		if (_chargingData[index]->Evboard_id == 0x01)
+		{
+			if(regRelay.relay_event.bits.Gun1_N == NO)
+				outputRelay.relay_event.bits.Gun1_N = YES;
+			else if (regRelay.relay_event.bits.Gun1_P == NO)
+				outputRelay.relay_event.bits.Gun1_P = YES;
+		}
+		else if (_chargingData[index]->Evboard_id == 0x02)
+		{
+			if(regRelay.relay_event.bits.Gun2_N == NO)
+				outputRelay.relay_event.bits.Gun2_N = YES;
+			else if (regRelay.relay_event.bits.Gun2_P == NO)
+				outputRelay.relay_event.bits.Gun2_P = YES;
+		}
+	}
+	else if (_chargingData[index]->SystemStatus == S_COMPLETE)
+	{
+		if (_chargingData[index]->PresentChargingCurrent <= SEFETY_SWITCH_RELAY_CUR)
+		{
+			if (_chargingData[index]->Evboard_id == 0x01)
+			{
+				if(regRelay.relay_event.bits.Gun1_P == YES)
+					outputRelay.relay_event.bits.Gun1_P = NO;
+				else if (regRelay.relay_event.bits.Gun1_N == YES)
+					outputRelay.relay_event.bits.Gun1_N = NO;
+			}
+			else if (_chargingData[index]->Evboard_id == 0x02)
+			{
+				if(regRelay.relay_event.bits.Gun2_P == YES)
+					outputRelay.relay_event.bits.Gun2_P = NO;
+				else if (regRelay.relay_event.bits.Gun2_N == YES)
+					outputRelay.relay_event.bits.Gun2_N = NO;
+			}
+		}
+	}
+	else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
+	{
+		if (_chargingData[index]->Evboard_id == 0x01)
+		{
+			if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if (regRelay.relay_event.bits.CCS_Precharge == NO)
+					outputRelay.relay_event.bits.CCS_Precharge = YES;
+				else if (regRelay.relay_event.bits.CCS_Precharge == YES)
+					outputRelay.relay_event.bits.Gun1_P = NO;
+			}
+		}
+		else if (_chargingData[index]->Evboard_id == 0x02)
+		{
+			if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if (regRelay.relay_event.bits.CCS_Precharge == NO)
+					outputRelay.relay_event.bits.CCS_Precharge = YES;
+				else if (regRelay.relay_event.bits.CCS_Precharge == YES)
+					outputRelay.relay_event.bits.Gun2_P = NO;
+			}
+		}
+	}
+	else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
+	{
+		if (_chargingData[index]->Evboard_id == 0x01)
+		{
+			if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if (regRelay.relay_event.bits.Gun1_P == NO)
+					outputRelay.relay_event.bits.Gun1_P = YES;
+				else if(regRelay.relay_event.bits.Gun1_P == YES)
+					outputRelay.relay_event.bits.CCS_Precharge = NO;
+			}
+		}
+		else if (_chargingData[index]->Evboard_id == 0x02)
+		{
+			if (_chargingData[index]->Type == _Type_CCS_2)
+			{
+				if (regRelay.relay_event.bits.Gun2_P == NO)
+					outputRelay.relay_event.bits.Gun2_P = YES;
+				else if(regRelay.relay_event.bits.Gun2_P == YES)
+					outputRelay.relay_event.bits.CCS_Precharge = NO;
+			}
+		}
+	}
+}
+
+void SetParalleRelayStatus()
+{
+	if (gunCount >= 2 && ONE_CONNECTOR_USE == NO)
+	{
+		if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING)
+		{
+			// 初始化~ 不搭橋接
+			if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
+				outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
+			else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
+				outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
+		}
+		else
+		{
+			// 單槍充電中 - 搭上橋接
+			if((_chargingData[0]->IsReadyToCharging == YES && _chargingData[1]->IsReadyToCharging == NO) ||
+					(_chargingData[0]->IsReadyToCharging == NO && _chargingData[1]->IsReadyToCharging == YES))
+			{
+				if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY)
+				{
+					if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
+						outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
+					else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
+						outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
+				}
+				else
+				{
+					if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
+						outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
+					else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
+						outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
+				}
+			}
+			else
+			{
+				// 雙槍充電中~ 不搭橋接
+				if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
+					outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
+				else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
+					outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
+			}
+		}
+	}
+}
+
+//==========================================
+// Init all share memory
+//==========================================
+int InitShareMemory()
+{
+	int result = PASS;
+	int MeterSMId;
+
+	//creat 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;
+	 }
+	 //creat 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;
+	}
+
+	//creat ShmFanModuleData
+	if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData),  0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmget ShmFanModuleData NG\n");
+		#endif
+		result = FAIL;
+	}
+	else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmat ShmFanModuleData NG\n");
+		#endif
+		result = FAIL;
+	 }
+	 memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
+	 //creat ShmRelayModuleData
+	if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData),  0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
+		#endif
+		result = FAIL;
+	}
+	else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
+		#endif
+		result = FAIL;
+	}
+
+	if(CHAdeMO_QUANTITY > 0)
+	{
+		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
+		{
+			#ifdef SystemLogMessage
+			DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
+			#endif
+			return FAIL;
+		}
+		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
+			#ifdef SystemLogMessage
+			DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
+			#endif
+			return FAIL;
+		}
+	}
+
+	if(CCS_QUANTITY > 0)
+	{
+		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
+		{
+			#ifdef SystemLogMessage
+			DEBUG_ERROR("shmget ShmCcsData NG \n");
+			#endif
+			return FAIL;
+		}
+		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+		{
+			#ifdef SystemLogMessage
+		   	DEBUG_ERROR("shmat ShmCcsData NG \n");
+			#endif
+			return FAIL;
+		}
+	}
+
+
+	return result;
+}
+
+int InitComPort()
+{
+	int fd;
+	struct termios tios;
+
+	fd = open(relayRs485PortName, O_RDWR);
+	if(fd <= 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
+		#endif
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+		}
+		sleep(5);
+		return -1;
+	}
+	ioctl (fd, TCGETS, &tios);
+	tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
+	tios.c_lflag = 0;
+	tios.c_iflag = 0;
+	tios.c_oflag = 0;
+	tios.c_cc[VMIN]=0;
+	tios.c_cc[VTIME]=(byte)0;		// timeout 0.5 second
+	tios.c_lflag=0;
+	tcflush(fd, TCIFLUSH);
+	ioctl (fd, TCSETS, &tios);
+
+	return fd;
+}
+
+//================================================
+// Main process
+//================================================
+bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
+{
+	for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
+		if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
+				== target) {
+			chargingData[target] =
+					&ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
+			return true;
+		}
+	}
+
+	for (byte index = 0; index < CCS_QUANTITY; index++) {
+		if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
+				== target) {
+			chargingData[target] =
+					&ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
+			return true;
+		}
+	}
+
+	for (byte index = 0; index < GB_QUANTITY; index++) {
+		if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
+				== target) {
+			chargingData[target] =
+					&ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
+			return true;
+		}
+	}
+
+	return false;
+}
+
+void Initialization()
+{
+	bool isPass = false;
+
+	for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
+	{
+		outputRelay.relay_event.relay_status[index] = 0x00;
+	}
+
+	while(!isPass)
+	{
+		isPass = true;
+		for (byte _index = 0; _index < gunCount; _index++)
+		{
+			if (!FindChargingInfoData(_index, &_chargingData[0]))
+			{
+				DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
+				isPass = false;
+				break;
+			}
+		}
+	}
+}
+
+bool IsNoneMatchRelayStatus()
+{
+	bool result = false;
+
+//	PRINTF_FUNC("Real Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
+//			regRelay.relay_event.bits.AC_Contactor,
+//			regRelay.relay_event.bits.Gun1_P,
+//			regRelay.relay_event.bits.Gun1_N,
+//			regRelay.relay_event.bits.Gun2_P,
+//			regRelay.relay_event.bits.Gun2_N,
+//			regRelay.relay_event.bits.CCS_Precharge,
+//			regRelay.relay_event.bits.Gun1_Parallel_P,
+//			regRelay.relay_event.bits.Gun1_Parallel_N);
+
+	if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
+		(regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
+		(regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
+		(regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
+		(regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
+		(regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
+		(regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
+		(regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
+	{
+		result = true;
+	}
+
+	return result;
+}
+
+void MatchRelayStatus()
+{
+	// 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
+	//regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
+	ShmSysConfigAndInfo->SysInfo.AcContactorStatus  = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
+	regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
+	regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
+	regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
+	regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
+	regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
+	regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
+	regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
+}
+
+void CheckRelayStatusByADC()
+{
+	if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
+			(ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
+	{
+		// Relay 前後電壓一致
+		_chargingData[0]->RelayK1K2Status = 0x01;
+	}
+	else
+		_chargingData[0]->RelayK1K2Status = 0x00;
+
+	if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
+				(ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
+	{
+		// Relay 前後電壓一致
+		_chargingData[1]->RelayK1K2Status = 0x01;
+	}
+	else
+		_chargingData[1]->RelayK1K2Status = 0x00;
+}
+
+void SetGfdConfig(byte index, byte resister)
+{
+	gfd_config.index = index;
+	gfd_config.state = resister;
+
+	//PRINTF_FUNC("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
+	if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
+	{
+//		PRINTF_FUNC("Set reqVol = %f, resister = %d \n",
+//				gfd_config.reqVol,
+//				gfd_config.resister);
+	}
+}
+
+void CableCheckDetected(byte index)
+{
+	// Cable Check
+	// 當火線上的電壓 = 車端要求的電壓電流
+	// _chargingData[targetGun]->EvBatterytargetVoltage
+	// 才可以開始偵測 1s
+	// Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
+	// Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
+	// SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
+	if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING) ||
+			(_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
+	{
+		if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE)
+		{
+			SetGfdConfig(index, GFD_CABLECHK);
+		}
+		else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
+				_chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)
+		{
+			SetGfdConfig(index, GFD_PRECHARGE);
+		}
+		else if (_chargingData[index]->SystemStatus <= S_CHARGING)
+		{
+			SetGfdConfig(index, GFD_CHARGING);
+		}
+	}
+	else if(_chargingData[index]->SystemStatus == S_COMPLETE || _chargingData[index]->SystemStatus == S_PREPARNING
+			|| _chargingData[index]->SystemStatus == S_IDLE)
+	{
+		SetGfdConfig(index, GFD_IDLE);
+	}
+}
+
+void CheckOutputPowerOverCarReq(byte index)
+{
+	float fireV = _chargingData[index]->FireChargingVoltage;
+	float carV = _chargingData[index]->EvBatterytargetVoltage;
+
+	if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
+			(_chargingData[index]->Type == _Type_Chademo || _chargingData[index]->Type == _Type_CCS_2 || _chargingData[index]->Type == _Type_GB))
+	{
+		if (fireV >= (carV + (carV * 0.1)))
+		{
+			PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
+					_chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
+			DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
+					_chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
+			_chargingData[index]->StopChargeFlag = YES;
+		}
+	}
+}
+
+void CheckOutputVolNoneMatchFire(byte index)
+{
+	if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
+			(_chargingData[index]->Type == _Type_Chademo || _chargingData[index]->Type == _Type_CCS_2 || _chargingData[index]->Type == _Type_GB))
+	{
+		if ((_chargingData[index]->PresentChargingVoltage < _chargingData[index]->FireChargingVoltage - 300) ||
+				(_chargingData[index]->PresentChargingVoltage > _chargingData[index]->FireChargingVoltage + 300))
+		{
+			if (!_isOutputNoneMatch[index])
+			{
+				_isOutputNoneMatch[index] = YES;
+				gettimeofday(&_checkOutputNoneMatchTimer[index], NULL);
+			}
+			else
+			{
+				if ((GetTimeoutValue(_checkOutputNoneMatchTimer[index]) / 1000) >= 5000)
+				{
+					PRINTF_FUNC("[Module_InternalComm]CheckOutputVolNoneMatchFire NG : pre = %f, fire = %f \n",
+					_chargingData[index]->PresentChargingVoltage, _chargingData[index]->FireChargingVoltage);
+					DEBUG_ERROR("[Module_InternalComm]CheckOutputVolNoneMatchFire NG : pre = %f, fire = %f \n",
+					_chargingData[index]->PresentChargingVoltage, _chargingData[index]->FireChargingVoltage);
+					_chargingData[index]->StopChargeFlag = YES;
+				}
+			}
+		}
+	}
+}
+
+int main(void)
+{
+	if(InitShareMemory() == FAIL)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("InitShareMemory NG\n");
+		#endif
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		sleep(5);
+		return 0;
+	}
+	// Open Uart5 for RB
+	Uart5Fd = InitComPort();
+	Initialization();
+	sleep(1);
+
+	if(Uart5Fd < 0)
+	{
+		printf ("open port error. \n");
+		return 0;
+	}
+
+	outputRelay.relay_event.bits.AC_Contactor = 0x00;
+	//outputRelay.relay_event.bits.CCS_Precharge = 0x00;
+	//outputRelay.relay_event.bits.Gun1_Parallel_P = 0x01;
+	//outputRelay.relay_event.bits.Gun1_Parallel_N = 0x01;
+	//outputRelay.relay_event.bits.Gun1_P = 0x01;
+	//outputRelay.relay_event.bits.Gun1_N = 0x01;
+	//outputRelay.relay_event.bits.Gun2_N = 0x01;
+	//outputRelay.relay_event.bits.Gun2_P = 0x01;
+	if(Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay) != PASS)
+		PRINTF_FUNC("Config_Relay_Output fail \n");
+
+	bool printRelayStatus = false;
+	//return 0;
+	for(;;)
+	{
+		bool isCharging = false;
+		// 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
+		if (ShmRelayModuleData->SelfTest_Comp == NO)
+		{
+			GetFwAndHwVersion_Relay();
+			SetRtcData_Relay();
+			sleep(1);
+		}
+
+		if (ShmFanModuleData->SelfTest_Comp == NO)
+		{
+			GetFwAndHwVersion_Fan();
+			SetModelName_Fan();
+			SetRtcData_Fan();
+			sleep(1);
+			gettimeofday(&_priority_time, NULL);
+		}
+
+		if (ShmRelayModuleData->SelfTest_Comp == YES)
+		{
+			// ==============優先權最高 10 ms ==============
+			// 輸出電壓
+			GetPersentOutputVol();
+
+			// 三相輸入電壓
+			GetPresentInputVol();
+
+			// 讀取當前 relay 狀態
+			GetRelayOutputStatus();
+
+			for (int i = 0; i < gunCount; i++)
+			{
+				// Cable check (Set)
+				CableCheckDetected(i);
+
+				// check k1 k2 relay 狀態
+				CheckK1K2RelayOutput(i);
+
+				// 依據當前各槍的狀態選擇 搭上/放開 Relay
+				SetK1K2RelayStatus(i);
+
+				if (_chargingData[i]->SystemStatus == S_IDLE)
+					gfdChkFailCount[i] = 0;
+
+				if (_chargingData[i]->SystemStatus == S_BOOTING	||
+					(_chargingData[i]->SystemStatus >= S_PREPARNING && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
+					(_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
+					(ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_WAIT_FOR_PLUG))
+				{
+					_chargingData[i]->IsReadyToCharging = YES;
+					isCharging = true;
+
+					if (_chargingData[i]->SystemStatus == S_CHARGING)
+					{
+						CheckOutputPowerOverCarReq(i);
+						CheckOutputVolNoneMatchFire(i);
+					}
+					else
+					{
+						_isOutputNoneMatch[i] = NO;
+					}
+				}
+				else
+					_chargingData[i]->IsReadyToCharging = NO;
+			}
+			// Cable check (Get)
+			GetGfdAdc();
+
+			// 橋接 relay
+			SetParalleRelayStatus();
+
+			// 搭上 AC Contactor
+			if (isCharging)
+				outputRelay.relay_event.bits.AC_Contactor = YES;
+			else
+				outputRelay.relay_event.bits.AC_Contactor = NO;
+
+//			if (isCharging)
+//			{
+//				isStopChargingCount = false;
+//				outputRelay.relay_event.bits.AC_Contactor = YES;
+//			}
+//			else
+//			{
+//				if (!isStopChargingCount)
+//				{
+//					gettimeofday(&_close_ac_contactor, NULL);
+//					isStopChargingCount = true;
+//				}
+//				else
+//				{
+//					if (!isSystemBooting ||
+//						(outputRelay.relay_event.bits.AC_Contactor == YES && GetTimeoutValue(_close_ac_contactor) / 1000 >= (TEN_MINUTES * 1000)))
+//						outputRelay.relay_event.bits.AC_Contactor = NO;
+//				}
+//			}
+
+			// 搭上/鬆開 Relay
+			// 放開 Relay 之前要先確認輸出的電壓電流是否已經降到某個值
+			if(IsNoneMatchRelayStatus())
+			{
+				if (!printRelayStatus)
+				{
+					PRINTF_FUNC("Match Relay Target, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
+							outputRelay.relay_event.bits.AC_Contactor,
+							outputRelay.relay_event.bits.Gun1_P,
+							outputRelay.relay_event.bits.Gun1_N,
+							outputRelay.relay_event.bits.Gun2_P,
+							outputRelay.relay_event.bits.Gun2_N,
+							outputRelay.relay_event.bits.CCS_Precharge,
+							outputRelay.relay_event.bits.Gun1_Parallel_P,
+							outputRelay.relay_event.bits.Gun1_Parallel_N);
+				}
+				printRelayStatus = false;
+				if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
+				{}
+			}
+			else
+			{
+				if (!printRelayStatus)
+				{
+					PRINTF_FUNC("Match Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
+							regRelay.relay_event.bits.AC_Contactor,
+							regRelay.relay_event.bits.Gun1_P,
+							regRelay.relay_event.bits.Gun1_N,
+							regRelay.relay_event.bits.Gun2_P,
+							regRelay.relay_event.bits.Gun2_N,
+							regRelay.relay_event.bits.CCS_Precharge,
+							regRelay.relay_event.bits.Gun1_Parallel_P,
+							regRelay.relay_event.bits.Gun1_Parallel_N);
+				}
+				printRelayStatus = true;
+			}
+		}
+
+		if (ShmFanModuleData->SelfTest_Comp == YES)
+		{
+			if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
+			{
+				GetFanSpeed();
+				gettimeofday(&_priority_time, NULL);
+//				printf("ShmFanModuleData->PresentFan1Speed = %d \n", ShmFanModuleData->PresentFan1Speed);
+//				printf("ShmFanModuleData->PresentFan2Speed = %d \n", ShmFanModuleData->PresentFan2Speed);
+//				printf("ShmFanModuleData->PresentFan3Speed = %d \n", ShmFanModuleData->PresentFan3Speed);
+//				printf("ShmFanModuleData->PresentFan4Speed = %d \n", ShmFanModuleData->PresentFan4Speed);
+				if (isCharging)
+				{
+					if (ShmFanModuleData->PresentFan1Speed < MAX_FAN_SPEED ||
+						ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
+						ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
+						ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
+					{
+						ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
+						ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
+						ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
+						ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
+					}
+				}
+				else
+				{
+					if (ShmFanModuleData->PresentFan1Speed > MIN_FAN_SPEED ||
+						ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
+						ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
+						ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
+					{
+						ShmFanModuleData->SetFan1Speed = MIN_FAN_SPEED;
+						ShmFanModuleData->SetFan2Speed = MIN_FAN_SPEED;
+						ShmFanModuleData->SetFan3Speed = MIN_FAN_SPEED;
+						ShmFanModuleData->SetFan4Speed = MIN_FAN_SPEED;
+					}
+				}
+
+				if (ShmFanModuleData->TestFanSpeed > 0)
+				{
+					ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
+					ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
+					ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
+					ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
+				}
+				//PRINTF_FUNC("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
+				SetFanModuleSpeed();
+			}
+		}
+
+		usleep(10000);
+	}
+
+	return FAIL;
+}

BIN
EVSE/Projects/DW30/Apps/Module_InternalComm.o


+ 3 - 2
EVSE/Projects/DW30/Apps/Module_LcmContro.h

@@ -29,10 +29,11 @@
 #include 	<stdbool.h>
 #include	"../../define.h"
 
-#define Debug
 #define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
 #define PASS				1
 #define FAIL				-1
+#define YES					1
+#define NO					0
 
 typedef unsigned char 			byte;
 
@@ -63,7 +64,6 @@ byte _gunIndex = 0;
 byte _idlePageRotate = 1;
 bool _backend_conn_status = false;
 bool _wifi_conn_status = false;
-bool _net_conn_status = false;
 
 // LCM - HW
 byte _everyPageRollChange = 0;
@@ -73,6 +73,7 @@ short __wifi_status = 0x003C;
 
 short __sel_gun_btn = 0x0040;
 short __ret_home_btn = 0x0042;
+short __stop_method_btn = 0x0044;
 
 short __qr_code = 0x0050;
 

BIN
EVSE/Projects/DW30/Apps/Module_LcmControl


+ 77 - 48
EVSE/Projects/DW30/Apps/Module_LcmControl.c

@@ -1,5 +1,7 @@
 #include "Module_LcmContro.h"
 
+void PRINTF_FUNC(char *string, ...);
+
 int StoreLogMsg(const char *fmt, ...);
 #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)
@@ -47,6 +49,20 @@ char* getTimeString(void)
 	return result;
 }
 
+void PRINTF_FUNC(char *string, ...)
+{
+	if (DEBUG)
+	{
+		va_list args;
+		char buffer[4096];
+
+		va_start(args, string);
+		vsnprintf(buffer, sizeof(buffer), string, args);
+		va_end(args);
+		printf("%s \n", buffer);
+	}
+}
+
 //==========================================
 // Init all share memory
 //==========================================
@@ -133,7 +149,7 @@ void WriteCmdToLcm(byte *cmd, byte cmdLen)
 	int len = write(_port, cmd, cmdLen);
 	if(len >= sizeof(cmd))
 	{
-		//printf("Write cmd to LCM successfully. \n");
+		//PRINTF_FUNC("Write cmd to LCM successfully. \n");
 	}
 }
 
@@ -170,7 +186,7 @@ void ReadMsgFromLcm(byte *msg, byte readLen)
 	}
 
 //	for (byte idx = 0; idx < len; idx++)
-//		printf("[system_command]-RX: %X\n", *(msg + idx));
+//		PRINTF_FUNC("[system_command]-RX: %X\n", *(msg + idx));
 }
 
 //================================================
@@ -305,10 +321,10 @@ void ChangeWarningFunc()
 {
 	byte cmd[7];
 	byte i = 0;
-	//printf("ChangeWarningFunc \n");
+	//PRINTF_FUNC("ChangeWarningFunc \n");
 	// 最多一次五筆
-	//printf("LCM PageIndex = %d \n", ShmSysConfigAndInfo->SysWarningInfo.PageIndex);
-	//printf("WarningCount = %d \n", ShmSysConfigAndInfo->SysWarningInfo.WarningCount);
+	//PRINTF_FUNC("LCM PageIndex = %d \n", ShmSysConfigAndInfo->SysWarningInfo.PageIndex);
+	//PRINTF_FUNC("WarningCount = %d \n", ShmSysConfigAndInfo->SysWarningInfo.WarningCount);
 	for(i = 0; (i + ShmSysConfigAndInfo->SysWarningInfo.PageIndex * 5) < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
 	{
 		memset(cmd, 0x00, sizeof(cmd));
@@ -375,7 +391,7 @@ void ChangeQrCode_Charge(char *input)
 //================================================
 void ChangeCurPage()
 {
-	//printf("cur = %d, new = %d \n", _currentPage, ShmSysConfigAndInfo->SysInfo.PageIndex);
+	//PRINTF_FUNC("cur = %d, new = %d \n", _currentPage, ShmSysConfigAndInfo->SysInfo.PageIndex);
 	if (_currentPage != ShmSysConfigAndInfo->SysInfo.PageIndex)
 	{
 		_currentPage = ShmSysConfigAndInfo->SysInfo.PageIndex;
@@ -512,9 +528,6 @@ void ChangeRemainTime(int sec)
 //	int max = 65536;
 //	sec = rand() % (max - min + 1) + min;
 
-	if (sec < 0)
-		return;
-
 	h = (sec / 3600);
 	m = (sec - (3600 * h)) / 60;
 	s = (sec - (3600 * h) - (m * 60));
@@ -523,6 +536,18 @@ void ChangeRemainTime(int sec)
 	DisplayValueToLcm(__remain_time_tx, cmd, sizeof(cmd));
 }
 
+void ChangeChargingEnergyValue(float energy)
+{
+	byte cmd[10];
+	byte value[10];
+
+	memset(cmd, 0x00, sizeof(cmd));
+
+	sprintf((char *) value, "%.1f kWh", energy);
+	string2ByteArray(value, cmd);
+	DisplayValueToLcm(__total_out_eng_tx, cmd, sizeof(cmd));
+}
+
 void ChangeChargingPowerValue(float pow)
 {
 	byte cmd[10];
@@ -539,6 +564,11 @@ void ChangeChargingPowerValue(float pow)
 	DisplayValueToLcm(__output_eng_tx, cmd, sizeof(cmd));
 }
 
+void ChangeStopMap(byte value)
+{
+
+}
+
 void RefreshPageAnimation(byte value)
 {
 	switch(_currentPage)
@@ -605,9 +635,10 @@ void RefreshPageAnimation(byte value)
 					}
 				}
 
-				if (_chargingInfoData[index]->SystemStatus == S_IDLE)
+				ChangeDisplay2Value(__sel_gun_btn, _sel_gun_btn);
+				if (_chargingInfoData[index]->SystemStatus == S_IDLE ||
+						_chargingInfoData[index]->SystemStatus == S_RESERVATION)
 				{
-					ChangeDisplay2Value(__sel_gun_btn, _disappear);
 					if(value == 0)
 					{
 						ChangeDisplay2Value(__side_top, _side_rfid_1);
@@ -626,7 +657,6 @@ void RefreshPageAnimation(byte value)
 				}
 				else
 				{
-					ChangeDisplay2Value(__sel_gun_btn, _sel_gun_btn);
 					ChangeDisplay2Value(__side_top, _disappear);
 					ChangeDisplay2Value(__side_down, _disappear);
 					ChangeDisplay2Value(__qr_code_pre, _disappear);
@@ -649,47 +679,35 @@ void RefreshPageAnimation(byte value)
 void RefreshConnStatus()
 {
 	// Wifi priority is higher than Ethernet
-	if (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn == 0x01)
+	if (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn == YES)
 	{
-		if (!_wifi_conn_status)
-		{
-			_wifi_conn_status = true;
-			ChangeDisplay2Value(__wifi_status, _wifi_connect);
-			ChangeDisplay2Value(__ethernet_status, _disappear);
-		}
+		_wifi_conn_status = true;
+		ChangeDisplay2Value(__wifi_status, _wifi_connect);
+		ChangeDisplay2Value(__ethernet_status, _disappear);
 	}
 	else
 	{
-		if(_wifi_conn_status)
-		{
-			_wifi_conn_status = false;
-			ChangeDisplay2Value(__wifi_status, _disappear);
-		}
+		_wifi_conn_status = false;
+		ChangeDisplay2Value(__wifi_status, _disappear);
 	}
 
-
 	if (!_wifi_conn_status)
 	{
-		if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == PASS)
+		if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES)
 		{
-			if(!_net_conn_status)
-			{
-				_net_conn_status = true;
-				ChangeDisplay2Value(__ethernet_status, _ethernet_connect);
-			}
+			ChangeDisplay2Value(__ethernet_status, _ethernet_connect);
 		}
 		else
 		{
-			if(_net_conn_status)
-			{
-				_net_conn_status = false;
-				ChangeDisplay2Value(__ethernet_status, _ethernet_disconnect);
-			}
+			ChangeDisplay2Value(__ethernet_status, _ethernet_disconnect);
 		}
 	}
 
 	// 連線到後台
-
+	if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == YES)
+		ChangeDisplay2Value(__conn_status, _connect);
+	else
+		ChangeDisplay2Value(__conn_status, _disconnect);
 }
 
 void ProcessPageInfo()
@@ -723,7 +741,7 @@ void ProcessPageInfo()
 						}
 					}
 						break;
-					case _Type_CCS:
+					case _Type_CCS_2:
 					{
 						if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == i)
 						{
@@ -742,9 +760,17 @@ void ProcessPageInfo()
 					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == i)
 					{
 						ChangeBattMapAndValue(_LCM_CHARGING, _chargingInfoData[i]->EvBatterySoc);
-						if (_chargingInfoData[i]->RemainChargingDuration < 0)
+						if (_chargingInfoData[i]->RemainChargingDuration >= 0)
 							ChangeRemainTime(_chargingInfoData[i]->RemainChargingDuration);
-						ChangeChargingPowerValue(_chargingInfoData[i]->PresentChargingPower);
+						if (_chargingInfoData[i]->PresentChargingPower >= 0)
+							ChangeChargingPowerValue(_chargingInfoData[i]->PresentChargingPower);
+						if (_chargingInfoData[i]->PresentChargedEnergy >= 0)
+							ChangeChargingEnergyValue(_chargingInfoData[i]->PresentChargedEnergy);
+
+						if (strcmp((char *)_chargingInfoData[i]->StartUserId, "") == 0)
+							ChangeDisplay2Value(__stop_method_btn, _stop_charging_btn);
+						else
+							ChangeDisplay2Value(__stop_method_btn, _stop_charging_btn_scan);
 					}
 				}
 				else if (_currentPage == _LCM_COMPLETE)
@@ -752,9 +778,12 @@ void ProcessPageInfo()
 					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == i)
 					{
 						ChangeBattMapAndValue(_LCM_COMPLETE, _chargingInfoData[i]->EvBatterySoc);
-						if (_chargingInfoData[i]->RemainChargingDuration < 0)
+						if (_chargingInfoData[i]->RemainChargingDuration >= 0)
 							ChangeRemainTime(_chargingInfoData[i]->RemainChargingDuration);
-						ChangeChargingPowerValue(_chargingInfoData[i]->PresentChargingPower);
+						if (_chargingInfoData[i]->PresentChargingPower >= 0)
+							ChangeChargingPowerValue(_chargingInfoData[i]->PresentChargingPower);
+						if (_chargingInfoData[i]->PresentChargedEnergy >= 0)
+							ChangeChargingEnergyValue(_chargingInfoData[i]->PresentChargedEnergy);
 					}
 				}
 			}
@@ -771,15 +800,13 @@ void ProcessPageInfo()
 					}
 				}
 
-				if (_chargingInfoData[index]->SystemStatus == S_IDLE || _chargingInfoData[index]->SystemStatus == S_BOOTING)
+				if (_chargingInfoData[index]->SystemStatus == S_IDLE ||
+						_chargingInfoData[index]->SystemStatus == S_RESERVATION ||
+						_chargingInfoData[index]->SystemStatus == S_BOOTING)
 				{
 					// QR Code 處理
 					ChangeQrCode_Charge("http://google.com.tw");
 				}
-				else
-				{
-					ChangeDisplay2Value(__sel_gun_btn, _disappear);
-				}
 			}
 		}
 			break;
@@ -808,7 +835,7 @@ void Initialization()
 	}
 
 	if (count == 0)
-		printf("LCM Initialization Gun Fail.............\n");
+		PRINTF_FUNC("LCM Initialization Gun Fail.............\n");
 }
 
 int main(void)
@@ -832,6 +859,8 @@ int main(void)
 	//ChangeBackLight(true);
 	Initialization();
 
+//	ChangeToOtherPage(_LCM_FIX);
+//	return 0;
 	while(_port != -1)
 	{
 		//DemoFunction();

BIN
EVSE/Projects/DW30/Apps/Module_LcmControl.o


BIN
EVSE/Projects/DW30/Apps/Module_PrimaryComm


+ 70 - 12
EVSE/Projects/DW30/Apps/Module_PrimaryComm.c

@@ -31,10 +31,11 @@
 #include	"PrimaryComm.h"
 #include 	<stdbool.h>
 
-#define Debug
 #define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
 #define PASS				1
 #define FAIL				-1
+#define YES					1
+#define NO					0
 
 typedef unsigned char 		byte;
 
@@ -51,6 +52,9 @@ int Uart1Fd;
 char *priPortName = "/dev/ttyS1";
 Ver ver;
 Gpio_in gpio_in;
+Rtc rtc;
+
+void PRINTF_FUNC(char *string, ...);
 
 int StoreLogMsg(const char *fmt, ...);
 #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
@@ -91,6 +95,19 @@ int DiffTimeb(struct timeb ST, struct timeb ET)
 	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
 }
 
+void PRINTF_FUNC(char *string, ...)
+{
+	if (DEBUG)
+	{
+		va_list args;
+		char buffer[4096];
+
+		va_start(args, string);
+		vsnprintf(buffer, sizeof(buffer), string, args);
+		va_end(args);
+		printf("%s \n", buffer);
+	}
+}
 //=================================
 // Common routine
 //=================================
@@ -224,17 +241,18 @@ void GetFwAndHwVersion()
 {
 	if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
 	{
-		printf("s1 = %s \n", ver.Version_FW);
+		PRINTF_FUNC("s1 = %s \n", ver.Version_FW);
 		strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
+		strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
 	}
 
 	if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
-		printf("s2 = %s \n", ver.Version_HW);
+		PRINTF_FUNC("s2 = %s \n", ver.Version_HW);
 }
 
 void GetInputGpioStatus()
 {
-	//printf("GetInputGpioStatus \n");
+	//PRINTF_FUNC("GetInputGpioStatus \n");
 	if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
 	{
 		ShmSysConfigAndInfo->SysInfo.AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
@@ -246,9 +264,11 @@ void GetInputGpioStatus()
 		ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
 		ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
 
-		//printf("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
-		//printf("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
-		//printf("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
+		//PRINTF_FUNC("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
+		//PRINTF_FUNC("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
+		//PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
+		if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
+			DEBUG_ERROR("AC Mainbreaker occur. \n");
 	}
 }
 
@@ -267,9 +287,46 @@ void SetOutputGpio()
 	gpio.AC_Breaker = 0x00;
 
 	if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
-		printf("SetOutputGpio sucessfully. \n");
+		PRINTF_FUNC("SetOutputGpio sucessfully. \n");
+	else
+		PRINTF_FUNC("SetOutputGpio fail. \n");
+}
+
+void SetRtcData()
+{
+	struct timeb csuTime;
+	struct tm *tmCSU;
+
+	ftime(&csuTime);
+	tmCSU = localtime(&csuTime.time);
+//	PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
+//			tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
+//			tmCSU->tm_sec);
+
+	rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
+	rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
+	rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
+	rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
+
+	rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
+	rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
+
+	rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
+	rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
+
+	rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
+	rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
+
+	rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
+	rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
+
+	rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
+	rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
+
+	if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
+		PRINTF_FUNC("SetRtc sucessfully. \n");
 	else
-		printf("SetOutputGpio fail. \n");
+		PRINTF_FUNC("SetRtc fail. \n");
 }
 
 //================================================
@@ -311,13 +368,13 @@ int main(void)
 		#endif
 		if(ShmStatusCodeData!=NULL)
 		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
 		}
 		sleep(5);
 		return 0;
 	}
 	Uart1Fd = InitComPort();
-	printf("407 Port id = %d \n", Uart1Fd);
+	PRINTF_FUNC("407 Port id = %d \n", Uart1Fd);
 
 	if(Uart1Fd < 0)
 	{
@@ -334,13 +391,14 @@ int main(void)
 	}
 
 	SetOutputGpio();
+	SetRtcData();
 	for(;;)
 	{
 		// 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
 		// 模組更新 FW 後,需重新做
 		if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
 		{
-			printf("(407) Get Fw and Hw Ver. \n");
+			PRINTF_FUNC("(407) Get Fw and Hw Ver. \n");
 			GetFwAndHwVersion();
 			usleep(1000000);
 			ShmPrimaryMcuData->SelfTest_Comp = PASS;

BIN
EVSE/Projects/DW30/Apps/Module_PrimaryComm.o


BIN
EVSE/Projects/DW30/Apps/Module_PsuComm


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 452 - 300
EVSE/Projects/DW30/Apps/Module_PsuComm.c


+ 7 - 0
EVSE/Projects/DW30/Apps/Module_PsuComm.h

@@ -82,3 +82,10 @@ bool _chkTotalCapStart = false;
 bool isUpgradeFlag = false;
 
 struct timeval _workModePriority_time;
+struct timeval _derating_time;
+int deratingTime = 0;
+
+int connector_1[] = {0};
+int connector_2[] = {1};
+int conn_1_count = 0;
+int conn_2_count = 0;

BIN
EVSE/Projects/DW30/Apps/Module_PsuComm.o


+ 35 - 4
EVSE/Projects/DW30/Apps/PrimaryComm.c

@@ -33,7 +33,7 @@
 #define FAIL				-1
 
 struct Address Addr={0x01,0x02,0x03,0x04,0xFF};
-struct Command Cmd={0x01,0x02,0x0a,0x86,0xe0,0xe1,0xe2,0xe3};
+struct Command Cmd={0x01,0x02,0x0a,0x86,0x87,0xe0,0xe1,0xe2,0xe3};
 
 int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
 {
@@ -197,6 +197,40 @@ unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpi
 	return result;
 }
 
+unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
+			Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
+			Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
+		chksum ^= tx[6 + idx];
+	tx[20] = chksum;
+
+	if (tranceive(fd, tx, sizeof(tx), rx) > 0)
+	{
+		chksum = 0x00;
+		for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
+		{
+			chksum ^= rx[6 + idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+				(rx[2] == tx[1]) &&
+				(rx[1] == tx[2]) &&
+				(rx[3] == tx[3]) &&
+				(rx[6] == tx[6]))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
 unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
 {
 	unsigned char result = FAIL;
@@ -211,9 +245,6 @@ unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned
 
 	if(tranceive(fd, tx, sizeof(tx), rx) > 0)
 	{
-		for(int rr = 0; rr < 8; rr++)
-			printf("rx = %x \n", rx[rr]);
-
 		chksum = 0x00;
 		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
 		{

+ 3 - 4
EVSE/Projects/DW30/Apps/PrimaryComm.h

@@ -17,6 +17,7 @@ extern struct Command
 	unsigned char query_Gpio_In;		//0x0a
 
 	unsigned char config_Gpio_Output;	//0x86
+	unsigned char config_Rtc_Data;		//0x87
 
 	unsigned char update_Start;		//0xe0
 	unsigned char update_Abort;		//0xe1
@@ -56,10 +57,7 @@ typedef struct GPIO_OUT
 
 typedef struct RTC
 {
-	unsigned char AC_Connector;
-	unsigned char Button_LED[2];
-	unsigned char System_LED[4];
-	unsigned char AC_Breaker;
+	unsigned char RtcData[14];
 }Rtc;
 
 extern unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf);
@@ -67,6 +65,7 @@ extern unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ve
 extern unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf);
 
 extern unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf);
+extern unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf);
 // 13 bytes
 // year : 4, month : 2, day : 2, hour : 2, min : 2, sec : 2
 //extern unsigned char Config_RTC();

BIN
EVSE/Projects/DW30/Apps/PrimaryComm.o


BIN
EVSE/Projects/DW30/Apps/ReadCmdline


+ 401 - 315
EVSE/Projects/DW30/Apps/ReadCmdline.c

@@ -43,6 +43,10 @@ typedef unsigned char			byte;
 #define FAIL				-1
 #define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
 
+#define TTY_PATH            "/dev/tty"
+#define STTY_US             "stty raw -echo -F "
+#define STTY_DEF            "stty -raw echo -F "
+
 struct SysConfigAndInfo			*ShmSysConfigAndInfo;
 struct StatusCodeData 			*ShmStatusCodeData;
 struct PrimaryMcuData			*ShmPrimaryMcuData;
@@ -50,11 +54,10 @@ struct CHAdeMOData				*ShmCHAdeMOData;
 struct CcsData					*ShmCcsData;
 struct FanModuleData			*ShmFanModuleData;
 struct RelayModuleData			*ShmRelayModuleData;
+struct PsuData 					*ShmPsuData;
 
 struct ChargingInfoData 		*_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-
-
-struct timeval _id_assign_time;
+int gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
 
 char *msg = "state : get gun state (index) \n"
 		"card : scanning card (x) : \n"
@@ -64,35 +67,6 @@ char *msg = "state : get gun state (index) \n"
 		"ver : ver of board (407 or index or rb or fan) \n"
 		"ac : get ac relay state (x) \n";
 
-
-
-
-#define TTY_PATH            "/dev/tty"
-#define STTY_US             "stty raw -echo -F "
-#define STTY_DEF            "stty -raw echo -F "
-
-
-static int get_char()
-{
-    fd_set rfds;
-    struct timeval tv;
-    int ch = 0;
-
-    FD_ZERO(&rfds);
-    FD_SET(0, &rfds);
-    tv.tv_sec = 0;
-    tv.tv_usec = 10; //wait input timout time
-
-    //if input
-    if (select(1, &rfds, NULL, NULL, &tv) > 0)
-    {
-        ch = getchar(); 
-    }
-
-    return ch;
-}
-
-
 bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
 {
 	for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
@@ -204,160 +178,382 @@ int InitShareMemory()
    		result = FAIL;
    	}
 
+   	if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData),	IPC_CREAT | 0777)) < 0)
+   	{
+   		result = FAIL;
+   	}
+   	else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+   	{
+   		result = FAIL;
+   	}
+
     return result;
 }
 
-enum _SYSTEM_STATUS
+void RunStatusProc(char *v1, char *v2)
+{
+	int _index = atoi(v1);
+	if (!FindChargingInfoData(_index, &_chargingData[0]))
+	{
+		printf ("FindChargingInfoData error\n");
+		return;
+	}
+
+	if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
+	{
+		// get
+		printf ("index = %x, status = %x (%d)\n", _index, _chargingData[_index]->SystemStatus, _chargingData[_index]->IsAvailable);
+	}
+	else
+	{
+		// set
+		_chargingData[_index]->SystemStatus = atoi(v2);
+	}
+}
+
+void RunCardProc(char *v1, char *v2)
+{
+	if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
+	{
+		ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
+		printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
+	}
+	else
+	{
+		ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
+		printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
+	}
+}
+
+void RunGunPlugitProc(char *v1, char *v2)
+{
+	int _index = atoi(v1);
+	if (!FindChargingInfoData(_index, &_chargingData[0]))
+	{
+		printf("FindChargingInfoData error\n");
+		return;
+	}
+
+	if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
+	{
+		// get
+		printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
+	}
+	else
+	{
+		// set
+		_chargingData[_index]->ConnectorPlugIn = atoi(v2);
+	}
+}
+
+void GetGunLockStatusProc(char *v1, char *v2)
+{
+	if (strcmp(v1, "0") == 0)
+	{
+		printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked);
+	}
+
+	if (strcmp(v1, "1") == 0)
+	{
+		printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked);
+	}
+}
+
+void SetSystemIDProc()
+{
+	char *systemId = "Alston_Test";
+	memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, systemId, strlen(systemId));
+}
+
+void RunSelfProc()
+{
+	printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
+}
+
+void GetFwVerProc(char *v1)
+{
+	if (strcmp(v1, "407") == 0)
+	{
+		printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
+	}
+	else if (strcmp(v1, "0") == 0)
+	{
+		printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version);
+		printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
+	}
+	else if (strcmp(v1, "1") == 0)
+	{
+		printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version);
+		printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
+	}
+	else if (strcmp(v1, "rb") == 0)
+	{
+		printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
+	}
+	else if (strcmp(v1, "fan") == 0)
+	{
+		printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
+	}
+	else if (strcmp(v1, "dc") == 0)
+	{
+		printf("DC Main Version = %s \n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
+	}
+}
+
+void FwUpdateFlagProc()
+{
+	ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
+}
+
+void CheckAcStatus(char *v1)
+{
+	if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
+	{
+		printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
+	}
+}
+
+void SetCableChkStatus(char *v1, char *v2)
+{
+	int _index = atoi(v1);
+	if (!FindChargingInfoData(_index, &_chargingData[0]))
+	{
+		printf ("FindChargingInfoData error\n");
+		return;
+	}
+
+	_chargingData[_index]->GroundFaultStatus = atoi(v2);
+}
+
+void SetPowerValue(char *v1, char *v2)
+{
+//	int _index = atoi(v1);
+//	if (!FindChargingInfoData(_index, &_chargingData[0])) {
+//		printf("FindChargingInfoData error\n");
+//		return;
+//	}
+//
+//	if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0) {
+//		// get
+//		printf("index = %x, max cur = %f\n", _index,
+//				_chargingData[_index]->MaxChargingCurrentForTest);
+//	} else {
+//		// set
+//		_chargingData[_index]->MaxChargingCurrentForTest = atoi(v2);
+//	}
+}
+
+void GetGunSelectedNum(char *v1)
+{
+	if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
+		printf("connector selected = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
+	else
+	{
+		int _index = atoi(v1);
+		ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
+		printf("connector select changed = %d \n", _index);
+	}
+}
+
+void SetFanSpeed(char *v1)
 {
-	S_BOOTING = 						0,
-    S_IDLE,
-	S_AUTHORIZING,
-	S_REASSIGN_CHECK,
-	S_REASSIGN,
-	S_PREPARNING,
-	S_PREPARING_FOR_EV,
-	S_PREPARING_FOR_EVSE,
-    S_CHARGING,
-	S_TERMINATING,
-	S_COMPLETE,
-	S_ALARM,
-	S_RESERVATION,
-	S_BOOKING,
-	S_MAINTAIN,
-	S_DEBUG,
-	S_CCS_PRECHARGE_ST0,
-	S_CCS_PRECHARGE_ST1,
-	S_SINGLE_RUN,
-};
-
-void RunUnconditionalChargeIndex1(char *v1, char *v2)
+	int speed = atoi(v1);
+
+	ShmFanModuleData->TestFanSpeed = speed;
+}
+
+void GetFanSpeed()
 {
-	int _Voltage = atoi(v1);
-	int _Current = atoi(v2);
+	printf("ShmFanModuleData->PresentFan1Speed = %d \n", ShmFanModuleData->PresentFan1Speed);
+	printf("ShmFanModuleData->PresentFan2Speed = %d \n", ShmFanModuleData->PresentFan2Speed);
+	printf("ShmFanModuleData->PresentFan3Speed = %d \n", ShmFanModuleData->PresentFan3Speed);
+	printf("ShmFanModuleData->PresentFan4Speed = %d \n", ShmFanModuleData->PresentFan4Speed);
+}
+
+void GetPsuInformation(char *v1)
+{
+	printf("*************************************************\n");
+	if(strcmp(v1, "count") == 0)
+	{
+		for (int i = 0; i < 4; i++)
+		{
+			printf("Group Index = %d, Module Count = %d \n", i, ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity);
+		}
+	}
+	else if(strcmp(v1, "ver") == 0)
+	{
+		for (int i = 0; i < ShmPsuData->GroupCount; i++)
+		{
+			for (int j = 0; j < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; j++)
+			{
+				printf("Group Index = %d, Psu Index = %d, Version = %s \n",
+					i, j, ShmPsuData->PsuGroup[i].PsuModule[j].FwVersion);
+			}
+		}
+	}
+	else if(strcmp(v1, "cap") == 0)
+	{
+		for (int i = 0; i < ShmPsuData->GroupCount; i++)
+		{
+			printf("Group Index = %d, MaxCur = %d, Power = %d \n",
+					i, ShmPsuData->PsuGroup[i].GroupAvailableCurrent, ShmPsuData->PsuGroup[i].GroupAvailablePower);
+		}
+	}
+	else if (strcmp(v1, "output") == 0)
+	{
+		for (int i = 0; i < ShmPsuData->GroupCount; i++)
+		{
+			printf("Group Index = %d, OutputV = %d, OutputC = %d \n",
+					i, ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage, ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent);
+		}
+	}
+	printf("*************************************************\n");
+}
+
+static int get_char()
+{
+    fd_set rfds;
+    struct timeval tv;
+    int ch = 0;
+
+    FD_ZERO(&rfds);
+    FD_SET(0, &rfds);
+    tv.tv_sec = 0;
+    tv.tv_usec = 10; //wait input timout time
+
+    //if input
+    if (select(1, &rfds, NULL, NULL, &tv) > 0)
+    {
+        ch = getchar();
+    }
+
+    return ch;
+}
+
+void RunUnconditionalChargeIndex1(char *v1, char *v2, char *v3)
+{
+	int _GunIndex = atoi(v1);
+	float _Voltage = atof(v2);
+	int _Current = atoi(v3);
 	unsigned char PreviousSystemStatus = 0xff;
-	int ch = 0,chcount = 0;;
-	
-	char InputChar[128];
-	
-	if (!FindChargingInfoData(0, &_chargingData[0]))
+	int ch = 0;
+
+	if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
 	{
 		printf ("FindChargingInfoData error\n");
 		return;
 	}
 
-    printf ("ReqVoltage = %d, ReqCurrent = %d\n", _Voltage,_Current);
-    
+    printf ("ReqVoltage = %f, ReqCurrent = %d\n", _Voltage,_Current);
+
     if(_Voltage > 1000 || _Voltage < 150){
-        
+
         printf ("Input Voltage over range\n");
         return;
     }
 
-    if(_Current > 100 || _Current < 2){
-        
-        printf ("Input Current over range\n");
-        return;
-    }
-            
+//    if(_Current > 100 || _Current < 2){
+//
+//        printf ("Input Current over range\n");
+//        return;
+//    }
+
     //測試期間先跳過自我測試 _STEST_COMPLETE = 0xfe
     //ShmSysConfigAndInfo->SysInfo.SelfTestSeq = 0xfe;
-    
+
     //kill ev task
     system("killall Module_EvComm");
-    
+
     _Voltage = (_Voltage * 10);
     _Current = (_Current * 10);
-    
-    
+
     //system(STTY_US TTY_PATH);
-    
-    
+
     while(true)
     {
         //fix gun 1
-        ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
-        
+        ShmSysConfigAndInfo->SysInfo.CurGunSelected = _GunIndex;
+
     	switch(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
-    	{    	    
+    	{
             case S_IDLE:
-    		{    		    
+    		{
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
-        	        
+
         	        printf ("[UnconditionalCharge - S_IDLE]\n");
-        	        
+
         	    }
-        	    
-        	        		    
+
                 _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARNING;
     		}
     		break;
-    		    		
-    		
+
+
     		case S_PREPARNING:
     		{
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
-        	        
+
         	        printf ("[UnconditionalCharge - S_PREPARNIN]\n");
-        	        
+
         	        //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
         	        printf ("wait find module\n");
-        	        
+
         	    }
     		    //main 會在此階段判斷以下資料跳到下一個 state
     		    //用來得知 AC 是否有搭上 (搭上模組的資訊才會出來) 因為每次  AC_Contactor
-    		    
-    		    
+
+
     		    //ShmPsuData->SystemPresentPsuQuantity;
     		    //ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity;
     		    //ShmPsuData->PsuGroup[gun_index].GroupAvailablePower;
-    		    //_chargingData[gun_index]->AvailableChargingPower;	
-    		    
+    		    //_chargingData[gun_index]->AvailableChargingPower;
+
     		    //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
     		    //sleep(10);
-    		    
-    		    //清除 main timeout 機制 
-    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;	   
-    		    //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step 
-    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;	
-    	    
+
+    		    //清除 main timeout 機制
+    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
+    		    //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
+    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
+
     		}
     		break;
-    		
+
     		case S_PREPARING_FOR_EV:
     		{
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
-        	        
+
         	        printf ("[UnconditionalCharge - S_PREPARING_FOR_EV]\n");
-        	        printf ("ReqVoltage = %d, ReqCurrent = %d\n", _Voltage,_Current);
-        	               	        
-        	    }    		    
-    		    //清除 main timeout 機制 
-    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;	   
-    		    //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step 
+        	        printf ("ReqVoltage = %f, ReqCurrent = %d\n", _Voltage,_Current);
+
+        	    }
+    		    //清除 main timeout 機制
+    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
+    		    //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
-    		    
+
     		    //充電電壓電流
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
    		        _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 5000;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 20;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
-    		    
+
     		    //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
     		    //確定模組己升壓完成
-    		    //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage <=  (3000+500) && 
+    		    //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage <=  (3000+500) &&
     		    //  _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage >=  (3000-500) )
     		    {
-    		        printf ("Precharge Done = %d\n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
-    		        //EV done 
+    		        printf ("Precharge Done = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
+    		        //EV done
     		        _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARING_FOR_EVSE;
     		    }
-    		    		    
+
     		}
     		break;
 
@@ -367,116 +563,116 @@ void RunUnconditionalChargeIndex1(char *v1, char *v2)
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
-        	        
+
         	        printf ("[UnconditionalCharge - S_PREPARING_FOR_EVSE]\n");
-        	               	        
-        	    }    	
+
+        	    }
         	    //printf ("tar vol = %d \n", _Voltage);
         	    //printf ("tar cur = %d \n", _Current);
-        	    	    
-    		    //清除 main timeout 機制 
-    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;	   
-    		    //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step 
+
+    		    //清除 main timeout 機制
+    		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
+    		    //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
-    		    
+
     		    //充電電壓電流
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 5000;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 20;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
-    		    
+
     		    //printf ("tar vol_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage);
         	   // printf ("tar cur_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent);
-        	    
+
     		    //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
     		    //確定模組己升壓完成
-    		    if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x01 || 
+    		    if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x01 ||
     		        _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03)
     		    {
     		        printf ("First Ground Fault State (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
-    		        printf ("Wait K1K2 = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
-    		        sleep(3);
-    		        //EV done 
-    		        _chargingData[0]->SystemStatus = S_CHARGING;
+    		        printf ("Wait K1K2 = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
+    		        sleep(5);
+    		        //EV done
+    		        _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_CHARGING;
     		    }
     		    else if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus > 0x02)
     		    {
     		             printf ("First Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
     		            _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
-    		    } 
-    		    		    
+    		    }
+
     		}
     		break;
-    		    		
+
     		case S_CHARGING:
     		{
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
-        	        
+
         	        printf ("[UnconditionalCharge - S_CHARGING]\n");
-        	               	        
+
         	    }
     		    //充電電壓電流
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _Voltage;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _Current;
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
-    		    
+
     		    //ev task do this
     		    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingPower = ((float)((_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage / 10) * (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingCurrent / 10)) / 1000);
-    		    
+
     		    if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x02){
     		         printf ("Charging Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
     		        _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
     		    }
-    		    
+
     		    if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03){
     		         printf ("Charging Ground Fault Warning (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
     		        _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
     		    }
-    		        
+
     		}
     		break;
- 
+
      		case S_TERMINATING:
     		{
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
         	        system("/root/Module_EvComm &");
-        	        
+
         	        printf ("[UnconditionalCharge - S_TERMINATING]\n");
         	        //無阻塞偵測 keybaord 結束
         	        system(STTY_DEF TTY_PATH);
-        	               	        
+
         	    }
-        	    
+
         	    sleep(3);
         	    _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_COMPLETE;
         	    return;
     		}
     		break;
-    		   		
+
     		case S_COMPLETE:
     		{
         	    if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
         	    {
         	        PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
-        	        
+
         	        printf ("[UnconditionalCharge - S_COMPLETE]\n");
-        	               	        
+
         	    }
         	    sleep(3);
         	    return;
     		}
     		break;
     	}
-    	
+
     	/*
     	//使用 Keyboard input 阻塞方法
         fgets(InputChar, sizeof(InputChar), stdin);
-        
+
         if (InputChar[0] == 's' && InputChar[1] == 't' && InputChar[2] == 'o' && InputChar[3] == 'p')
         {
            printf ("Precharge Done\n");
@@ -484,19 +680,19 @@ void RunUnconditionalChargeIndex1(char *v1, char *v2)
 
         }
         */
-        
+
         //使用 keybaord input 非阻塞方法
-        ch = get_char();       
+        ch = get_char();
         if (ch)
         {
             printf("%c \n\r", ch);
-            
+
             switch (ch)
             {
                 //Ctrl + C
                 case 3:
                     //system(STTY_DEF TTY_PATH);
-                        return 0;
+                        return;
                 //input c or C
                 case 'c':
                 case 'C':
@@ -506,134 +702,9 @@ void RunUnconditionalChargeIndex1(char *v1, char *v2)
                     break;
             }
         }
-               
-    }		
-
-}
-
-void RunStatusProc(char *v1, char *v2)
-{
-	int _index = atoi(v1);
-	if (!FindChargingInfoData(_index, &_chargingData[0]))
-	{
-		printf ("FindChargingInfoData error\n");
-		return;
-	}
-
-	if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
-	{
-		// get
-		printf ("index = %x, status = %x\n", _index, _chargingData[_index]->SystemStatus);
-	}
-	else
-	{
-		// set
-		_chargingData[_index]->SystemStatus = atoi(v2);
-	}
-}
-
-void RunCardProc(char *v1, char *v2)
-{
-	if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
-	{
-		ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
-		printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
-	}
-	else
-	{
-		ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
-		printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
-	}
-}
-
-void RunGunPlugitProc(char *v1, char *v2)
-{
-	int _index = atoi(v1);
-	if (!FindChargingInfoData(_index, &_chargingData[0]))
-	{
-		printf("FindChargingInfoData error\n");
-		return;
-	}
 
-	if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
-	{
-		// get
-		printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
-	}
-	else
-	{
-		// set
-		_chargingData[_index]->ConnectorPlugIn = atoi(v2);
-	}
-}
-
-void GetGunLockStatusProc(char *v1, char *v2)
-{
-	if (strcmp(v1, "0") == 0)
-	{
-		printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked);
-	}
-
-	if (strcmp(v1, "1") == 0)
-	{
-		printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked);
-	}
-}
-
-void RunSelfProc()
-{
-	printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
-}
-
-void GetFwVerProc(char *v1)
-{
-	if (strcmp(v1, "407") == 0)
-	{
-		printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
-	}
-	else if (strcmp(v1, "0") == 0)
-	{
-		printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version);
-		printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
-	}
-	else if (strcmp(v1, "1") == 0)
-	{
-		printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version);
-		printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
-	}
-	else if (strcmp(v1, "rb") == 0)
-	{
-		printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
-	}
-	else if (strcmp(v1, "fan") == 0)
-	{
-		printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
-	}
-}
-
-void FwUpdateFlagProc()
-{
-	ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
-}
-
-void CheckAcStatus(char *v1)
-{
-	if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
-	{
-		printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
-	}
-}
-
-void SetCableChkStatus(char *v1, char *v2)
-{
-	int _index = atoi(v1);
-	if (!FindChargingInfoData(_index, &_chargingData[0]))
-	{
-		printf ("FindChargingInfoData error\n");
-		return;
-	}
+    }
 
-	_chargingData[_index]->GroundFaultStatus = atoi(v2);
 }
 
 int main(void)
@@ -652,7 +723,7 @@ int main(void)
 	for(;;)
 	{
 		char word[128];
-		char newString[3][10];
+		char newString[7][10];
 		int i,j,ctr;
 
 		fgets(word, sizeof(word), stdin);
@@ -675,33 +746,18 @@ int main(void)
 			}
 		}
 
-		if(strcmp(newString[0], "strchg") == 0)
-		{    
-		    //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
-			if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 || strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
-			{
-			    printf ("Input cmd fail ------  strchg [vol 150-1000] [cru 2-100]\n");
-				continue;
-			}
-
-			// 槍狀態
-			RunUnconditionalChargeIndex1(newString[1], newString[2]);
-			continue;
-		}		
-		else if(strcmp(newString[0], "state") == 0)
+		if(strcmp(newString[0], "state") == 0)
 		{
 			if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
 				continue;
 
 			// 槍狀態
 			RunStatusProc(newString[1], newString[2]);
-			continue;
 		}
 		else if(strcmp(newString[0], "card") == 0)
 		{
 			// 刷卡狀態
 			RunCardProc(newString[1], newString[2]);
-			continue;
 		}
 		else if(strcmp(newString[0], "gun") == 0)
 		{
@@ -710,7 +766,6 @@ int main(void)
 
 			// 插槍狀態
 			RunGunPlugitProc(newString[1], newString[2]);
-			continue;
 		}
 		else if(strcmp(newString[0], "lock") == 0)
 		{
@@ -719,13 +774,16 @@ int main(void)
 
 			// 插槍狀態
 			GetGunLockStatusProc(newString[1], newString[2]);
-			continue;
+		}
+		else if(strcmp(newString[0], "sysid") == 0)
+		{
+			// 測試 sys id
+			SetSystemIDProc();
 		}
 		else if(strcmp(newString[0], "self") == 0)
 		{
 			// CSU 自我檢測狀態
 			RunSelfProc(newString[1]);
-			continue;
 		}
 		else if(strcmp(newString[0], "ver") == 0)
 		{
@@ -733,19 +791,16 @@ int main(void)
 				continue;
 			// 取 FW 版號
 			GetFwVerProc(newString[1]);
-			continue;
 		}
 		else if (strcmp(newString[0], "update") == 0)
 		{
 			// 更新
 			FwUpdateFlagProc(newString[1]);
-			continue;
 		}
 		else if (strcmp(newString[0], "ac") == 0)
 		{
 			// AC contactor 狀態
 			CheckAcStatus(newString[1]);
-			continue;
 		}
 		else if (strcmp(newString[0], "cable") == 0)
 		{
@@ -753,26 +808,57 @@ int main(void)
 				continue;
 			// cable check pass
 			SetCableChkStatus(newString[1], newString[2]);
-			continue;
 		}
-		else if (strcmp(newString[0], "?") == 0)
+		else if (strcmp(newString[0], "pow") == 0)
+		{
+			if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
+				continue;
+			// cable check pass
+			SetPowerValue(newString[1], newString[2]);
+		}
+		else if(strcmp(newString[0], "select") == 0)
 		{
-		    
-            printf ("\r\n\r\n%-12s : %-20s\n", "state","get gun state (index)");
-            printf ("%-12s : %-20s\n", "card","scanning card (x)");
-            printf ("%-12s : %-20s\n", "gun","get gun plugit state (index)");
-            printf ("%-12s : %-20s\n", "lock","get gun locked state (index)");
-            printf ("%-12s : %-20s\n", "self","self test state (x)");
-            printf ("%-12s : %-20s\n", "ver","ver of board (407 or index or rb or fan)");
-            printf ("%-12s : %-20s \r\n\r\n", "ac","get ac relay state (x)");
-			continue;
+			// 取得當前選的槍號
+			GetGunSelectedNum(newString[1]);
 		}
-		else{
-		    printf ("Dbg->", msg);
-		    continue;
+		else if(strcmp(newString[0], "fan") == 0)
+		{
+			// 設定風扇速度
+			SetFanSpeed(newString[1]);
 		}
+		else if(strcmp(newString[0], "speed") == 0)
+		{
+			// 取得風扇速度
+			GetFanSpeed();
+		}
+		else if(strcmp(newString[0], "psu") == 0)
+		{
+			//如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
+			if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
+			{
+				printf ("PSU : Param fail..Please retry again......\n");
+				continue;
+			}
+
+			// 取得 PSU 資訊
+			GetPsuInformation(newString[1]);
+		}
+		else if(strcmp(newString[0], "strchg") == 0)
+		{
+			//如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
+			if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 ||
+					strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
+			{
+				printf ("Input cmd fail ------  strchg [vol 150-1000] [cru 2-100]\n");
+				continue;
+			}
+
+			// 槍狀態
+			RunUnconditionalChargeIndex1(newString[1], newString[2], newString[3]);
+		}
+
+		printf ("%s\n", msg);
 		usleep(100000);
-		
 	}
 
 	return 0;

BIN
EVSE/Projects/DW30/Apps/ReadCmdline.o


+ 1 - 0
EVSE/Projects/DW30/Apps/init.sh

@@ -5,6 +5,7 @@ chmod 777 Module_InternalComm
 chmod 777 Module_EventLogging
 chmod 777 Module_EvComm
 chmod 777 Module_PsuComm
+chmod 777 OcppBackend
 chmod 777 kill.sh
 chmod 777 ReadCmdline
 

+ 835 - 793
EVSE/Projects/DW30/Apps/internalComm.c

@@ -1,793 +1,835 @@
-/*
- * internalComm.c
- *
- *  Created on: 201957
- *      Author: foluswen
- */
-#include 	<sys/time.h>
-#include 	<sys/timeb.h>
-#include    <sys/types.h>
-#include    <sys/stat.h>
-#include 	<sys/types.h>
-#include 	<sys/ioctl.h>
-#include 	<sys/socket.h>
-#include 	<sys/ipc.h>
-#include 	<sys/shm.h>
-#include 	<sys/shm.h>
-#include 	<sys/mman.h>
-#include 	<linux/wireless.h>
-#include 	<arpa/inet.h>
-#include 	<netinet/in.h>
-
-#include 	<unistd.h>
-#include 	<stdarg.h>
-#include    <stdio.h>      /*標準輸入輸出定義*/
-#include    <stdlib.h>     /*標準函數庫定義*/
-#include    <unistd.h>     /*Unix 標準函數定義*/
-#include    <fcntl.h>      /*檔控制定義*/
-#include    <termios.h>    /*PPSIX 終端控制定義*/
-#include    <errno.h>      /*錯誤號定義*/
-#include 	<errno.h>
-#include 	<string.h>
-#include	<time.h>
-#include	<ctype.h>
-#include 	<ifaddrs.h>
-#include 	<math.h>
-#include 	"internalComm.h"
-
-#define PASS				1
-#define FAIL				-1
-
-struct Address Addr={0x01,0x02,0x03,0xFF};
-struct Command Cmd={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x81,0x83,0x85,0x86,0x87,0xe0,0xe1,0xe2,0xe3};
-
-int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
-{
-	int len;
-	//sleep(2); //required to make flush work, for some reason
-	tcflush(fd,TCIOFLUSH);
-	if(write(fd, cmd, cmd_len) >= cmd_len)
-	{
-		usleep(10000);
-		len = read(fd, rx, 512);
-	}
-	else
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR("Serial command %s response fail.\n", cmd);
-		#endif
-	}
-
-	return len;
-}
-
-unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
-			*(Ret_Buf->Version_FW + 8) = 0x00;
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
-			*(Ret_Buf->Version_HW + 8) = 0x00;
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_InputVoltage, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			Ret_Buf->inputType = rx[6];
-			Ret_Buf->L1N_L12 =(rx[7] | (rx[8]<<8))/10.0;
-			Ret_Buf->L2N_L23 =(rx[9] | (rx[10]<<8))/10.0;
-			Ret_Buf->L3N_L31 =(rx[11] | (rx[12]<<8))/10.0;
-
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_OutputVoltage, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-
-			Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8));
-			Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8));
-			if((rx[4] | rx[5]<<8) > 4)
-			{
-				Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8));
-				Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8));
-			}
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Fan_Speed, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-//		for (int i = 0; i < 7; i++)
-//			printf("tx = %x \n", tx[i]);
-//		for (int i = 0; i < len; i++)
-//			printf("rx = %x \n", rx[i]);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			for(int idx=0;idx < 4;idx++)
-				Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
-
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			for(int idx=0;idx < 4;idx++)
-				Ret_Buf->temperature[idx] = rx[6+idx] - 60;
-
-			result = PASS;
-		}
-	}
-
-
-	return result;
-}
-
-unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
-				Ret_Buf->voltage[idx] = rx[6+idx];
-
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-//	for (int i = 0; i < 7; i++)
-//		printf("tx = %x \n", tx[i]);
-//	for (int i = 0; i < len; i++)
-//		printf("rx = %x \n", rx[i]);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
-			Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
-
-			Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
-			Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
-			Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
-			Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
-
-			Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
-			Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-//	for(int i = 0; i < 7; i++)
-//		printf ("tx = %d \n", tx[i]);
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-		{
-			printf("Query_Gfd_Adc fail \n");
-			return result;
-		}
-
-//		for(int i = 0; i < len; i++)
-//			printf ("rx = %d \n", rx[i]);
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
-			Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
-			Ret_Buf->result_conn1 = rx[10];
-			Ret_Buf->rb_step_1 = rx[11];
-
-			Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
-			Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
-			Ret_Buf->result_conn2 = rx[16];
-			Ret_Buf->rb_step_2 = rx[17];
-
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			Ret_Buf->AC_Connector 		= (rx[6] >> 0) & 0x01;
-			Ret_Buf->AC_MainBreaker 	= (rx[6] >> 1) & 0x01;
-			Ret_Buf->SPD 				= (rx[6] >> 2) & 0x01;
-			Ret_Buf->Door_Open 			= (rx[6] >> 3) & 0x01;
-			Ret_Buf->GFD[0] 			= (rx[6] >> 4) & 0x01;
-			Ret_Buf->GFD[1] 			= (rx[6] >> 5) & 0x01;
-			Ret_Buf->AC_Drop 			= (rx[6] >> 6) & 0x01;
-
-			Ret_Buf->Emergency_IO		= (rx[7] >> 0) & 0x01;
-
-			Ret_Buf->Button_Emergency_Press	= (rx[8] >> 0) & 0x01;
-			Ret_Buf->Button_On_Press 	= (rx[8] >> 1) & 0x01;
-			Ret_Buf->Button_Off_Press	= (rx[8] >> 2) & 0x01;
-			Ret_Buf->Key_1_Press 		= (rx[8] >> 3) & 0x01;
-			Ret_Buf->Key_2_Press 		= (rx[8] >> 4) & 0x01;
-			Ret_Buf->Key_3_Press 		= (rx[8] >> 5) & 0x01;
-			Ret_Buf->Key_4_Press 		= (rx[8] >> 6) & 0x01;
-
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[15] = {0xaa, 0x00, targetAddr, Cmd.config_Fan_Speed, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
-		chksum ^= tx[6+idx];
-	tx[14] = chksum;
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		chksum = 0x00;
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[10] = {0xaa, 0x00, targetAddr, Cmd.config_Relay_Output, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	for(int idx = 6;idx<8;idx++)
-		chksum ^= tx[idx];
-	tx[9] = chksum;
-
-//	for (int i = 0; i < 10; i++)
-//		printf("set relay cmd : tx = %x \n", tx[i]);
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-//		for (int i = 0; i < len; i++)
-//			printf("set relay cmd : rx = %x \n", rx[i]);
-
-		chksum = 0x00;
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == 0x01))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
-
-	for(int idx = 0;idx<2;idx++)
-		tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
-
-	for(int idx = 0;idx<4;idx++)
-			tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
-
-	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
-		chksum ^= tx[6+idx];
-	tx[14] = chksum;
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		chksum = 0x00;
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == tx[6]))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	tx[6] = Set_Buf->index;
-	tx[7] = Set_Buf->state;
-
-	for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
-		chksum ^= tx[6+idx];
-	tx[8] = chksum;
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		chksum = 0x00;
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == tx[6]))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
-			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	memcpy(tx + 6, modelname, 14);
-
-	for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
-		chksum ^= tx[6+idx];
-	tx[20] = chksum;
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		chksum = 0x00;
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-			  (rx[2] == tx[1]) &&
-			  (rx[1] == tx[2]) &&
-			  (rx[3] == tx[3]) &&
-			  (rx[6] == tx[6]))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[11] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
-		chksum ^= tx[6+idx];
-	tx[10] = chksum;
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		chksum = 0x00;
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == 0x00))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == 0x00))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[11 + length];
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-
-	tx[0] = 0xaa;
-	tx[1] = 0x00;
-	tx[2] = targetAddr;
-	tx[3] = Cmd.update_Transfer;
-	tx[4] = (4 + length) & 0xff;
-	tx[5] = ((4 + length)>>8) & 0xff;
-	tx[6] = (startAddr>>0) & 0xff;
-	tx[7] = (startAddr>>8) & 0xff;
-	tx[8] = (startAddr>>16) & 0xff;
-	tx[9] = (startAddr>>24) & 0xff;
-	memcpy(tx+10, data, length);
-
-	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
-		chksum ^= tx[6+idx];
-	tx[sizeof(tx)-1] = chksum;
-
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == 0x00))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
-
-unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
-{
-	unsigned char result = FAIL;
-	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
-	unsigned char rx[512];
-	unsigned char chksum = 0x00;
-	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
-
-	if(len > 6)
-	{
-		if (len < 6+(rx[4] | rx[5]<<8))
-			return result;
-
-		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
-		   (rx[2] == tx[1]) &&
-		   (rx[1] == tx[2]) &&
-		   (rx[3] == tx[3]) &&
-		   (rx[6] == 0x00))
-		{
-			result = PASS;
-		}
-	}
-
-	return result;
-}
+/*
+ * internalComm.c
+ *
+ *  Created on: 201957
+ *      Author: foluswen
+ */
+#include 	<sys/time.h>
+#include 	<sys/timeb.h>
+#include    <sys/types.h>
+#include    <sys/stat.h>
+#include 	<sys/types.h>
+#include 	<sys/ioctl.h>
+#include 	<sys/socket.h>
+#include 	<sys/ipc.h>
+#include 	<sys/shm.h>
+#include 	<sys/shm.h>
+#include 	<sys/mman.h>
+#include 	<linux/wireless.h>
+#include 	<arpa/inet.h>
+#include 	<netinet/in.h>
+
+#include 	<unistd.h>
+#include 	<stdarg.h>
+#include    <stdio.h>      /*標準輸入輸出定義*/
+#include    <stdlib.h>     /*標準函數庫定義*/
+#include    <unistd.h>     /*Unix 標準函數定義*/
+#include    <fcntl.h>      /*檔控制定義*/
+#include    <termios.h>    /*PPSIX 終端控制定義*/
+#include    <errno.h>      /*錯誤號定義*/
+#include 	<errno.h>
+#include 	<string.h>
+#include	<time.h>
+#include	<ctype.h>
+#include 	<ifaddrs.h>
+#include 	<math.h>
+#include 	"internalComm.h"
+
+#define PASS				1
+#define FAIL				-1
+
+struct Address Addr={0x01,0x02,0x03,0xFF};
+struct Command Cmd={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x81,0x83,0x85,0x86, 0x87,0x8B,0xe0,0xe1,0xe2,0xe3};
+
+int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
+{
+	int len;
+	//sleep(2); //required to make flush work, for some reason
+	tcflush(fd,TCIOFLUSH);
+	if(write(fd, cmd, cmd_len) >= cmd_len)
+	{
+		usleep(10000);
+		len = read(fd, rx, 512);
+	}
+	else
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("Serial command %s response fail.\n", cmd);
+		#endif
+	}
+
+	return len;
+}
+
+unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
+			*(Ret_Buf->Version_FW + 8) = 0x00;
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
+			*(Ret_Buf->Version_HW + 8) = 0x00;
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_InputVoltage, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			Ret_Buf->inputType = rx[6];
+			Ret_Buf->L1N_L12 =(rx[7] | (rx[8]<<8))/10.0;
+			Ret_Buf->L2N_L23 =(rx[9] | (rx[10]<<8))/10.0;
+			Ret_Buf->L3N_L31 =(rx[11] | (rx[12]<<8))/10.0;
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_OutputVoltage, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+
+			Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8));
+			Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8));
+			if((rx[4] | rx[5]<<8) > 4)
+			{
+				Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8));
+				Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8));
+			}
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Fan_Speed, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+//		for (int i = 0; i < 7; i++)
+//			printf("tx = %x \n", tx[i]);
+//		for (int i = 0; i < len; i++)
+//			printf("rx = %x \n", rx[i]);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			for(int idx=0;idx < 4;idx++)
+				Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			for(int idx=0;idx < 4;idx++)
+				Ret_Buf->temperature[idx] = rx[6+idx] - 60;
+
+			result = PASS;
+		}
+	}
+
+
+	return result;
+}
+
+unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
+				Ret_Buf->voltage[idx] = rx[6+idx];
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+//	for (int i = 0; i < 7; i++)
+//		printf("tx = %x \n", tx[i]);
+//	for (int i = 0; i < len; i++)
+//		printf("rx = %x \n", rx[i]);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
+			Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
+
+			Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
+			Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
+			Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
+			Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
+
+			Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
+			Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+//	for(int i = 0; i < 7; i++)
+//		printf ("tx = %d \n", tx[i]);
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+		{
+			//printf("Query_Gfd_Adc fail \n");
+			return result;
+		}
+
+//		for(int i = 0; i < len; i++)
+//			printf ("rx = %d \n", rx[i]);
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
+			Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
+			Ret_Buf->result_conn1 = rx[10];
+			Ret_Buf->rb_step_1 = rx[11];
+
+			Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
+			Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
+			Ret_Buf->result_conn2 = rx[16];
+			Ret_Buf->rb_step_2 = rx[17];
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			Ret_Buf->AC_Connector 		= (rx[6] >> 0) & 0x01;
+			Ret_Buf->AC_MainBreaker 	= (rx[6] >> 1) & 0x01;
+			Ret_Buf->SPD 				= (rx[6] >> 2) & 0x01;
+			Ret_Buf->Door_Open 			= (rx[6] >> 3) & 0x01;
+			Ret_Buf->GFD[0] 			= (rx[6] >> 4) & 0x01;
+			Ret_Buf->GFD[1] 			= (rx[6] >> 5) & 0x01;
+			Ret_Buf->AC_Drop 			= (rx[6] >> 6) & 0x01;
+
+			Ret_Buf->Emergency_IO		= (rx[7] >> 0) & 0x01;
+
+			Ret_Buf->Button_Emergency_Press	= (rx[8] >> 0) & 0x01;
+			Ret_Buf->Button_On_Press 	= (rx[8] >> 1) & 0x01;
+			Ret_Buf->Button_Off_Press	= (rx[8] >> 2) & 0x01;
+			Ret_Buf->Key_1_Press 		= (rx[8] >> 3) & 0x01;
+			Ret_Buf->Key_2_Press 		= (rx[8] >> 4) & 0x01;
+			Ret_Buf->Key_3_Press 		= (rx[8] >> 5) & 0x01;
+			Ret_Buf->Key_4_Press 		= (rx[8] >> 6) & 0x01;
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[15] = {0xaa, 0x00, targetAddr, Cmd.config_Fan_Speed, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[14] = chksum;
+
+//		for(int i = 0; i < 15; i++)
+//			printf ("tx = %x \n", tx[i]);
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+//	for(int i = 0; i < len; i++)
+//				printf ("rx = %x \n", rx[i]);
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		chksum = 0x00;
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[10] = {0xaa, 0x00, targetAddr, Cmd.config_Relay_Output, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6 + idx];
+	tx[9] = chksum;
+
+//	for (int i = 0; i < 10; i++)
+//		printf("set relay cmd : tx = %x \n", tx[i]);
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+//		for (int i = 0; i < len; i++)
+//			printf("set relay cmd : rx = %x \n", rx[i]);
+
+		chksum = 0x00;
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == 0x01))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
+
+	for(int idx = 0;idx<2;idx++)
+		tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
+
+	for(int idx = 0;idx<4;idx++)
+			tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
+
+	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[14] = chksum;
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		chksum = 0x00;
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == tx[6]))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	tx[6] = Set_Buf->index;
+	tx[7] = Set_Buf->state;
+
+	for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[8] = chksum;
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		chksum = 0x00;
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == tx[6]))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	memcpy(tx + 6, modelname, 14);
+
+	for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[20] = chksum;
+
+//	for(int i = 0; i < 21; i++)
+//				printf ("tx = %x \n", tx[i]);
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+//	for(int i = 0; i < len; i++)
+//					printf ("rx = %x \n", rx[i]);
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		chksum = 0x00;
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+			  (rx[2] == tx[1]) &&
+			  (rx[1] == tx[2]) &&
+			  (rx[3] == tx[3]) &&
+			  (rx[6] == tx[6]))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
+			Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
+			Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
+		chksum ^= tx[6 + idx];
+	tx[20] = chksum;
+
+	if (tranceive(fd, tx, sizeof(tx), rx) > 0)
+	{
+		chksum = 0x00;
+		for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
+		{
+			chksum ^= rx[6 + idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+				(rx[2] == tx[1]) &&
+				(rx[1] == tx[2]) &&
+				(rx[3] == tx[3]) &&
+				(rx[6] == tx[6]))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[11] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[10] = chksum;
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		chksum = 0x00;
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == 0x00))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == 0x00))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[11 + length];
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	tx[0] = 0xaa;
+	tx[1] = 0x00;
+	tx[2] = targetAddr;
+	tx[3] = Cmd.update_Transfer;
+	tx[4] = (4 + length) & 0xff;
+	tx[5] = ((4 + length)>>8) & 0xff;
+	tx[6] = (startAddr>>0) & 0xff;
+	tx[7] = (startAddr>>8) & 0xff;
+	tx[8] = (startAddr>>16) & 0xff;
+	tx[9] = (startAddr>>24) & 0xff;
+	memcpy(tx+10, data, length);
+
+	for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[sizeof(tx)-1] = chksum;
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == 0x00))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		if (len < 6+(rx[4] | rx[5]<<8))
+			return result;
+
+		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
+		{
+			chksum ^= rx[6+idx];
+		}
+
+		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
+		   (rx[2] == tx[1]) &&
+		   (rx[1] == tx[2]) &&
+		   (rx[3] == tx[3]) &&
+		   (rx[6] == 0x00))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}

+ 8 - 1
EVSE/Projects/DW30/Apps/internalComm.h

@@ -33,7 +33,8 @@ extern struct Command
 	unsigned char config_Model_Name;			//0x83
 	unsigned char config_Relay_Output;			//0x85
 	unsigned char config_Gpio_Output;			//0x86
-	unsigned char config_Gfd_Value;			//0x87
+	unsigned char config_Rtc_Data;				//0x87
+	unsigned char config_Gfd_Value;			//0x8B
 
 	unsigned char update_Start;				//0xe0
 	unsigned char update_Abort;				//0xe1
@@ -162,6 +163,11 @@ typedef struct GPIO_OUT
 	unsigned char System_LED[4];
 }Gpio_out;
 
+typedef struct RTC
+{
+	unsigned char RtcData[14];
+}Rtc;
+
 extern unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf);
 extern unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf);
 extern unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf);
@@ -178,6 +184,7 @@ extern unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetA
 extern unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf);
 extern unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf);
 extern unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname);
+extern unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf);
 
 extern unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32);
 extern unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr);

BIN
EVSE/Projects/DW30/Apps/internalComm.o


+ 11 - 7
EVSE/Projects/DW30/Apps/kill.sh

@@ -1,8 +1,12 @@
-killall Module_CSU
-killall Module_PrimaryComm
-killall Module_LcmControl
-killall Module_InternalComm
-killall Module_EventLogging
-killall Module_EvComm
-killall Module_PsuComm
+pkill Module_CSU
+pkill Module_PrimaryComm
+pkill Module_LcmControl
+pkill Module_InternalComm
+pkill Module_EventLogging
+pkill Module_EvComm
+pkill Module_PsuComm
+pkill Module_4g
+pkill Module_Wifi
+pkill OcppBackend
+pkill main
 

BIN
EVSE/Projects/DW30/Apps/main


+ 3769 - 2800
EVSE/Projects/DW30/Apps/main.c

@@ -1,2800 +1,3769 @@
-#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 	<unistd.h>
-#include 	<stdarg.h>
-#include    <stdio.h>      /*標準輸入輸出定義*/
-#include    <stdlib.h>     /*標準函數庫定義*/
-#include    <unistd.h>     /*Unix 標準函數定義*/
-#include    <fcntl.h>      /*檔控制定義*/
-#include    <termios.h>    /*PPSIX 終端控制定義*/
-#include    <errno.h>      /*錯誤號定義*/
-#include 	<errno.h>
-#include 	<string.h>
-#include	<time.h>
-#include	<ctype.h>
-#include 	<ifaddrs.h>
-#include 	<math.h>
-#include 	"../../define.h"
-#include 	"Config.h"
-#include 	<stdbool.h>
-#include 	<dirent.h>
-#include	"timeout.h"
-
-#define 	ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
-#define 	PASS				1
-#define 	FAIL				-1
-#define 	BUFFER_SIZE			128
-#define 	YES					1
-#define 	NO					0
-#define 	NORMAL				0
-#define		ABNORMAL			1
-#define 	EQUAL				0
-#define 	BTN_RELEASE			0
-#define 	BTN_PRESS			1
-#define 	MAX_BUF 			64
-#define 	SYSFS_GPIO_DIR 		"/sys/class/gpio"
-#define		UPGRADE_FAN			0x02
-#define		UPGRADE_RB			0x03
-#define		UPGRADE_PRI			0x04
-
-bool IsAuthorizingMode();
-void ClearAuthorizedFlag();
-bool isDetectPlugin();
-void ClearDetectPluginFlag();
-int mystrcmp(unsigned char *p1, unsigned char *p2);
-
-unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
-void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
-void ChargingTerminalProcess(byte gunIndex);
-void ChkPrimaryStatus();
-void StartSystemTimeoutDet(unsigned char flag);
-void StopSystemTimeoutDet();
-void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
-void StopGunInfoTimeoutDet(unsigned char gunIndex);
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct PsuData 					*ShmPsuData;
-struct CHAdeMOData				*ShmCHAdeMOData;
-struct CcsData					*ShmCcsData;
-struct PrimaryMcuData			*ShmPrimaryMcuData;
-struct FanModuleData			*ShmFanModuleData;
-struct RelayModuleData			*ShmRelayModuleData;
-struct OCPP16Data				*ShmOCPP16Data;
-
-struct ChargingInfoData			*chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-struct timeb 					startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-struct timeb 					endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
-
-byte _gunCount = 0;
-// for initial index to check EV board type is correct
-byte _gunIndex = 0;
-byte _chademoIndex = 0;
-byte _ccsIndex = 0;
-byte _gb_Index = 0;
-byte bd0_1_status = 0;
-byte bd0_2_status = 0;
-byte bd1_1_status = 0;
-byte bd1_2_status = 0;
-
-bool isCardScan = false;
-
-int rfidFd = -1;
-char* rfidPortName = "/dev/ttyS2";
-byte autoReturnTimeoutFlag = 0x00;
-int whileLoopTime = 10000; // 10 ms
-unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
-
-int StoreLogMsg_1(const char *fmt, ...);
-
-#define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
-#define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
-#define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
-
-//================================================
-// initial can-bus
-//================================================
-int InitCanBus()
-{
-	int 					s0,nbytes;
-	struct timeval			tv;
-	struct ifreq 			ifr0;
-	struct sockaddr_can		addr0;
-
-	system("/sbin/ip link set can0 down");
-	system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
-	system("/sbin/ip link set can0 up");
-
-	s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
-
-	tv.tv_sec = 0;
-	tv.tv_usec = 10000;
-   	if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct	timeval)) < 0)
-	{
-		#ifdef SystemLogMessage
-   		DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
-		#endif
-	}
-	nbytes=40960;
-	if (setsockopt(s0, SOL_SOCKET,  SO_RCVBUF, &nbytes, sizeof(int)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
-		#endif
-	}
-	nbytes=40960;
-	if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
-		#endif
-	}
-
-   	strcpy(ifr0.ifr_name, "can0" );
-	ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
-	addr0.can_family = AF_CAN;
-	addr0.can_ifindex = ifr0.ifr_ifindex;
-	bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
-	return s0;
-}
-
-//================================================
-// initial uart port
-//================================================
-char *_priPortName = "/dev/ttyS1";
-char *_485PortName = "/dev/ttyS5";
-
-int InitComPort(byte target)
-{
-	int fd;
-	struct termios tios;
-
-	if (target == UPGRADE_PRI)
-		fd = open(_priPortName, O_RDWR);
-	else if (target == UPGRADE_FAN || target == UPGRADE_RB)
-		fd = open(_485PortName, O_RDWR);
-
-	if(fd<=0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("open 407 Communication port NG \n");
-		#endif
-		return -1;
-	}
-	ioctl (fd, TCGETS, &tios);
-	tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
-	tios.c_lflag = 0;
-	tios.c_iflag = 0;
-	tios.c_oflag = 0;
-	tios.c_cc[VMIN]=0;
-	tios.c_cc[VTIME]=(unsigned char)1;
-	tios.c_lflag=0;
-	tcflush(fd, TCIFLUSH);
-	ioctl (fd, TCSETS, &tios);
-
-	return fd;
-}
-
-//=================================
-// Common routine
-//=================================
-int mystrcmp(unsigned char *p1, unsigned char *p2)
-{
-    while(*p1==*p2)
-    {
-        if(*p1=='\0' || *p2=='\0')
-            break;
-        p1++;
-        p2++;
-    }
-    if(*p1=='\0' && *p2=='\0')
-        return(PASS);
-    else
-        return(FAIL);
-}
-
-int DiffTimeb(struct timeb ST, struct timeb ET)
-{
-	//return milli-second
-	unsigned int StartTime,StopTime;
-
-	StartTime=(unsigned int)ST.time;
-	StopTime=(unsigned int)ET.time;
-	//return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
-	return (StopTime-StartTime);
-}
-
-int StoreLogMsg_1(const char *fmt, ...)
-{
-	char Buf[4096+256];
-	char buffer[4096];
-	time_t CurrentTime;
-	struct tm *tm;
-	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);
-	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
-			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
-			buffer,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-
-	return rc;
-}
-
-unsigned long GetTimeoutValue(struct timeval _sour_time)
-{
-	struct timeval _end_time;
-	gettimeofday(&_end_time, NULL);
-
-	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
-}
-
-void setChargerMode(byte gun_index, byte mode)
-{
-	chargingInfo[gun_index]->SystemStatus = mode;
-}
-
-//==========================================
-// Check interface status
-//==========================================
-int isInterfaceUp(const char *interface)
-{
-	int result = FAIL;
-
-	FILE *fp;
-	char cmd[256];
-	char buf[512];
-
-	strcpy(cmd, "ifconfig");
-	fp = popen(cmd, "r");
-	if(fp != NULL)
-	{
-		while(fgets(buf, sizeof(buf), fp) != NULL)
-		{
-			if(strstr(buf, interface) > 0)
-			{
-				result = PASS;
-			}
-		}
-	}
-	pclose(fp);
-
-	return result;
-}
-
-//=================================
-// Create all share memory
-//=================================
-int CreateShareMemory()
-{
-	int MeterSMId;
-
-	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey,	sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0))	== (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
-		#endif
-		return 0;
-	}
-	memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
-
-	if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
-		#endif
-		return 0;
-	}
-
-	memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
-
-	//creat ShmPsuData
-	if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
-		#endif
-		return 0;
-	}
-	memset(ShmPsuData, 0, sizeof(struct PsuData));
-
-	if(CHAdeMO_QUANTITY > 0)
-	{
-		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
-			#endif
-			return 0;
-		}
-		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
-			#endif
-			return 0;
-		}
-		memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
-	}
-
-	//creat ShmCcsData
-	if(CCS_QUANTITY > 0)
-	{
-		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
-			#endif
-			return 0;
-		}
-		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
-			#endif
-			return 0;
-		}
-		memset(ShmCcsData, 0, sizeof(struct CcsData));
-	}
-
-	//creat ShmPrimaryMcuData
-	if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
-		#endif
-		return 0;
-	}
-	memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
-
-	//creat ShmFanModuleData
-	if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData),	IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
-		#endif
-		return 0;
-	}
-	memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
-
-	//creat ShmRelayModuleData
-	if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData),	IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
-		#endif
-		return 0;
-	}
-	memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
-
-	//creat ShmOCPP16Data
-	if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
-		#endif
-		return 0;
-	}
-	else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
-		#endif
-		return 0;
-	}
-	// memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
-	return 1;
-}
-
-//=================================
-// LCM Page
-//=================================
-void ChangeLcmByIndex(byte page_index)
-{
-	if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level != 2 ||
-			page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
-	{
-		ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
-	}
-}
-
-//======================================================
-// Peripheral initial
-//======================================================
-void InitGPIO()
-{
-	/*****************0~3, 4 bank, bank x 32+ num*********************/
-	/***************************************************************/
-	/*************** GPIO 0 ***************************************/
-	/***************************************************************/
-	/* GPMC_AD8			=> 	GPIO0_22 *//*ID BD1_1*/
-	system("echo 22 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio22/direction");
-	/* GPMC_AD9			=>	GPIO0_23 *//*ID BD1_2*/
-	system("echo 23 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio23/direction");
-	/* GPMC_AD10		=>	GPIO0_26 *//*IO BD1_1*/
-	system("echo 26 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio26/direction");
-	system("echo 1 > /sys/class/gpio/gpio26/value");
-	/* GPMC_AD11		=>	GPIO0_27 *//*IO BD1_2*/
-	system("echo 27 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio27/direction");
-	/* RMII1_REF_CLK		=>	GPIO0_29 *//*USB 0 OCP detection*/
-	system("echo 29 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio29/direction");
-	/*XDMA_EVENT_INTR0	=>	GPIO0_19 *//*AM_RFID_RST*/
-	system("echo 19 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio19/direction");
-	system("echo 0 > /sys/class/gpio/gpio19/value");
-	/*XDMA_EVENT_INTR1	=>	GPIO0_20 *//*AM_RFID_ICC*/
-	system("echo 20 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio20/direction");
-	/***************************************************************/
-	/*************** GPIO 1 ***************************************/
-	/***************************************************************/
-	/* GPMC_AD12	=> 	GPIO1_12 *//*ID BD2_1*/
-	system("echo 44 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio44/direction");
-	/* GPMC_AD13	=>	GPIO1_13 *//*ID BD2_2*/
-	system("echo 45 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio45/direction");
-	/* GPMC_AD14	=>	GPIO1_14 *//*IO BD2_1*/
-	system("echo 46 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio46/direction");
-	system("echo 0 > /sys/class/gpio/gpio46/value");
-	/* GPMC_AD15	=>	GPIO1_15 *//*IO BD2_2*/
-	system("echo 47 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio47/direction");
-	/***************************************************************/
-	/*************** GPIO 2 ***************************************/
-	/***************************************************************/
-	/*LCD_AC_BIAS_EN	=>	GPIO2_25*//*RS-485 for module DE control*/
-	system("echo 89 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio89/direction");
-	system("echo 1 > /sys/class/gpio/gpio89/value");
-	/*LCD_HSYNC		=>	GPIO2_23*//*RS-485 for module RE control*/
-	system("echo 87 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio87/direction");
-	system("echo 0 > /sys/class/gpio/gpio87/value");
-	/*LCD_PCLK		=>	GPIO2_24*//*CCS communication board 1 proximity*/
-	system("echo 88 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio88/direction");
-	/*LCD_VSYNC		=>	GPIO2_22*//*CCS communication board 2 proximity*/
-	system("echo 86 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio86/direction");
-	/***************************************************************/
-	/*************** GPIO 3 ***************************************/
-	/***************************************************************/
-	/*MCASP0_FSX		=>	GPIO3_15*//*Emergency Stop button detect*/
-	system("echo 111 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio111/direction");
-	/*MCASP0_ACLKR	=>	GPIO3_18*//*USB1 OCP detect*/
-	system("echo 114 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio114/direction");
-	/*MCASP0_AHCLKR	=>	GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
-	system("echo 113 > /sys/class/gpio/export");
-	system("echo \"in\" > /sys/class/gpio/gpio113/direction");
-	/*MCASP0_ACLKX	=>	GPIO3_14*//*Ethernet PHY reset*/
-	system("echo 110 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio110/direction");
-	system("echo 0 > /sys/class/gpio/gpio110/value");
-	/* MCASP0_FSR		=>	GPIO3_19 *//*SMR Enable control_1*/
-	system("echo 115 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio115/direction");
-	system("echo 0 > /sys/class/gpio/gpio115/value");
-	/* MCASP0_AXR0	=>	GPIO3_16 *//*CSU board function OK indicator.*/
-	system("echo 112 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio112/direction");
-	system("echo 0 > /sys/class/gpio/gpio112/value");
-	/* MCASP0_AXR1	=>	GPIO3_20 *//*SMR Enable control_2*/
-	system("echo 116 > /sys/class/gpio/export");
-	system("echo \"out\" > /sys/class/gpio/gpio116/direction");
-	system("echo 0 > /sys/class/gpio/gpio116/value");
-#ifdef SystemLogMessage
-	DEBUG_ERROR_MSG("[main]InitGPIO: Initial GPIO OK");
-#endif
-}
-
-int LoadSysConfigAndInfo(struct SysConfigData *ptr)
-{
-	int fd,wrd;
-	struct SysConfigData *buf;
-	byte *PtrBuf;
-	unsigned int ChkSum, ChkSumOrg;
-
-	if ((buf = malloc(sizeof(struct SysConfigData))) == NULL)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo:malloc buffer NG,rebooting..");
-		#endif
-		if (ShmStatusCodeData != NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-		}
-		sleep(5);
-		system("reboot -f");
-		sleep(5);
-		system("reboot -f");
-	}
-	memset(buf, 0, sizeof(struct SysConfigData));
-
-	fd = open("/dev/mtdblock10", O_RDWR);
-	if (fd < 0)
-	{
-		free(buf);
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo:open mtdblock10 NG,rebooting..");
-		#endif
-		if (ShmStatusCodeData != NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-		}
-		sleep(5);
-		system("reboot -f");
-		sleep(5);
-		system("reboot -f");
-	}
-	wrd = read(fd, buf, sizeof(struct SysConfigData));
-	close(fd);
-	if (wrd != (sizeof(struct SysConfigData)))
-	{
-		free(buf);
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: read SysConfigData data NG,rebooting..");
-		#endif
-		if (ShmStatusCodeData != NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-		}
-		sleep(5);
-		system("reboot -f");
-		sleep(5);
-		system("reboot -f");
-	}
-	PtrBuf = (byte *) buf;
-	ChkSum = 0;
-	for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
-	{
-		ChkSum += PtrBuf[wrd];
-	}
-	ChkSumOrg = buf->Checksum;
-
-	if (ChkSum != ChkSumOrg)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: Primary SysConfigData checksum NG, read backup");
-		#endif
-		fd = open("/dev/mtdblock11", O_RDWR);
-		if (fd < 0)
-		{
-			free(buf);
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: open mtdblock11 (backup) NG,rebooting..");
-			#endif
-			if (ShmStatusCodeData != NULL)
-			{
-				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-			}
-			sleep(5);
-			system("reboot -f");
-			sleep(5);
-			system("reboot -f");
-		}
-		memset(buf, 0, sizeof(struct SysConfigData));
-		wrd = read(fd, buf, sizeof(struct SysConfigData));
-		close(fd);
-		if (wrd != sizeof(struct SysConfigData))
-		{
-			free(buf);
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: read backup SysConfigData data NG,rebooting..");
-			#endif
-			if (ShmStatusCodeData != NULL)
-			{
-				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-			}
-			sleep(5);
-			system("reboot -f");
-			sleep(5);
-			system("reboot -f");
-		}
-		PtrBuf = (byte *) buf;
-		ChkSum = 0;
-		for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
-		{
-			ChkSum += PtrBuf[wrd];
-		}
-		ChkSumOrg = buf->Checksum;
-		if (ChkSum != ChkSumOrg)
-		{
-			#ifdef SystemLogMessage
-			DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: backup SysConfigData checksum NG, read Factory default");
-			#endif
-			fd = open("/dev/mtdblock12", O_RDWR);
-			if (fd < 0)
-			{
-				free(buf);
-				#ifdef SystemLogMessage
-				DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: open mtdblock12 (Factory default) NG,rebooting..");
-				#endif
-				if (ShmStatusCodeData != NULL)
-				{
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-				}
-				sleep(5);
-				system("reboot -f");
-				sleep(5);
-				system("reboot -f");
-			}
-			memset(buf, 0, sizeof(struct SysConfigData));
-			wrd = read(fd, buf, sizeof(struct SysConfigData));
-			close(fd);
-			if (wrd != sizeof(struct SysConfigData))
-			{
-				free(buf);
-				#ifdef SystemLogMessage
-				DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: read factory default  SysConfigData data NG,rebooting..");
-				#endif
-				if (ShmStatusCodeData != NULL)
-				{
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
-				}
-				sleep(5);
-				system("reboot -f");
-				sleep(5);
-				system("reboot -f");
-			}
-			PtrBuf = (byte *) buf;
-			ChkSum = 0;
-			for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
-			{
-				ChkSum += PtrBuf[wrd];
-			}
-			ChkSumOrg = buf->Checksum;
-			if (ChkSum != ChkSumOrg)
-			{
-				#ifdef SystemLogMessage
-				DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: factory default  SysConfigData checksum NG, restore factory default");
-				#endif
-				goto DefaultShm;
-			}
-		}
-	}
-	//load OK
-	memcpy((struct SysConfigData *) ptr, (struct SysConfigData *) buf,	sizeof(struct SysConfigData));
-	free(buf);
-	#ifdef SystemLogMessage
-	DEBUG_ERROR_MSG("[main]LoadSysConfigAndInfo: Load SysConfigData OK");
-	#endif
-	return 1;
-
-	DefaultShm: system("cd /root;./FactoryConfig");
-	system("sync");
-	sleep(5);
-	system("reboot -f");
-	sleep(5);
-	system("reboot -f");
-	return FAIL;
-}
-
-void InitEthernet()
-{
-	char tmpbuf[256];
-	// /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
-	system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
-	sleep(2);
-	//Init Eth0 for internet
-	return;
-	memset(tmpbuf,0,256);
-	sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
-	ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
-	ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
-	system(tmpbuf);
-	memset(tmpbuf,0,256);
-	sprintf(tmpbuf,"route add default gw %s eth0 ",
-	ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
-    system(tmpbuf);
-
-    //Init Eth1 for administrator tool
-	memset(tmpbuf,0,256);
-	sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
-	ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
-	ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
-	system(tmpbuf);
-
-    //Run DHCP client if enabled
-	system("killall udhcpc");
-	system("rm -rf /etc/resolv.conf");
-	system("echo nameserver 8.8.8.8 > /etc/resolv.conf");		//Google DNS server
-	system("echo nameserver 180.76.76.76 > /etc/resolv.conf");	//Baidu DNS server
-	if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
-		system("/sbin/udhcpc -i eth0 -s /root/simple.script > /dev/null &");
-
-	#ifdef SystemLogMessage
-	DEBUG_ERROR_MSG("[main]InitEthernet: Initial Ethernet OK");
-	#endif
-}
-
-int InitialRfidPort()
-{
-	int uartO2 = open(rfidPortName, O_RDWR);
-	struct termios tios;
-
-	if (uartO2 != FAIL)
-	{
-		ioctl (uartO2, TCGETS, &tios);
-		tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
-		tios.c_lflag = 0;
-		tios.c_iflag = 0;
-		tios.c_oflag = 0;
-		tios.c_cc[VMIN] = 0;
-		tios.c_cc[VTIME] = (unsigned char) 1;
-		tios.c_lflag = 0;
-		tcflush(uartO2, TCIFLUSH);
-		ioctl(uartO2, TCSETS, &tios);
-	}
-
-	if (uartO2 < 0)
-	{
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
-	}
-
-	return uartO2;
-}
-
-int Initialization()
-{
-	//InitGPIO();
-	//LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
-	//InitEthernet();
-
-	ShmSysConfigAndInfo->SysConfig.OfflinePolicy = _OFFLINE_POLICY_FREE_CHARGING;
-	sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
-	sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
-	sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
-
-	// 初始化卡號驗證的 Flag
-	ClearAuthorizedFlag();
-	// 初始化插槍驗證的 Flag
-	ClearDetectPluginFlag();
-
-	// UART 2 for Rfid
-	rfidFd = InitialRfidPort();
-
-	memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
-	memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
-	ShmPrimaryMcuData->SelfTest_Comp = NO;
-	ShmRelayModuleData->SelfTest_Comp = NO;
-	ShmFanModuleData->SelfTest_Comp = NO;
-	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
-
-	for (byte count = 0; count < _gunCount; count++)
-	{
-		if (chargingInfo[count]->Type == _Type_Chademo)
-		{
-			ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
-		}
-		else if (chargingInfo[count]->Type == _Type_CCS)
-		{
-			if (ShmCcsData->CommProtocol == 0x01)
-			{
-				ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
-			}
-		}
-	}
-
-	#ifdef SystemLogMessage
-	printf("Initialization OK \n");
-	#endif
-
-	return PASS;
-}
-
-void SelfTestRun()
-{
-	bool evInitFlag = false;
-
-	StartSystemTimeoutDet(Timeout_SelftestChk);
-	ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
-	while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
-	{
-		ChkPrimaryStatus();
-		if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 2)
-		{
-			ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
-			return;
-		}
-
-		if (_gunCount > 0)
-		{
-			if (ShmPsuData->Work_Step == _NO_WORKING)
-			{
-				ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
-				break;
-			}
-
-			switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
-			{
-				case _STEST_VERSION:
-				{
-					// RB Version
-					if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
-							ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
-					{
-						//printf("RB pass \n");
-						ShmRelayModuleData->SelfTest_Comp = YES;
-					}
-
-					// Fan Version
-					if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
-							ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
-					{
-						//printf("RB pass \n");
-						ShmFanModuleData->SelfTest_Comp = YES;
-					}
-					
-					// 407 Version
-					if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
-							ShmPrimaryMcuData->version[0] != '\0')
-					{
-						//printf("407 pass \n");
-						ShmPrimaryMcuData->SelfTest_Comp = YES;
-					}
-					// EV 小板
-					if (!evInitFlag)
-					{
-						evInitFlag = YES;
-						for (byte index = 0; index < _gunCount; index++)
-						{
-							if (chargingInfo[index]->Type == _Type_Chademo)
-							{
-								if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
-										ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
-								{
-									//printf("chademo pass \n");
-									ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
-								}
-								else
-								{
-									printf("chademo fw lose...... \n");
-									evInitFlag = NO;
-								}
-							}
-							else if (chargingInfo[index]->Type == _Type_CCS)
-							{
-								if (ShmCcsData->CommProtocol == 0x01)
-								{
-									if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
-										ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
-									{
-										ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
-									}
-									else
-									{
-										printf("ccs fw lose \n");
-										evInitFlag = NO;
-									}
-								}
-							}
-						}
-					}
-
-					if (ShmRelayModuleData->SelfTest_Comp && ShmFanModuleData->SelfTest_Comp && ShmPrimaryMcuData->SelfTest_Comp && evInitFlag)
-					{
-						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
-					}
-				}
-					break;
-				case _STEST_AC_CONTACTOR:
-				{
-					//ShmPsuData->Work_Step = _TEST_COMPLETE;
-					// 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
-					if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
-					{
-						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
-						printf("Communication board pass. \n");
-					}
-				}
-					break;
-				case _STEST_PSU_DETECT:
-				{
-					// 此測試主要測試 PSU 對應是否為正確的火線上電壓
-					// 如果沒有 PSU 模組請 bypass
-					if (ShmPsuData->Work_Step == _TEST_POWER_STEP || ShmPsuData->Work_Step == _TEST_COMPLETE)
-					{
-						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
-					}
-				}
-					break;
-				case _STEST_PSU_CAP:
-				{
-					// 此測試是要確認當前總輸出能力
-					// 如果沒有 PSU 模組請 bypass
-					if (ShmPsuData->Work_Step == _TEST_COMPLETE)
-					{
-						sleep(1);
-						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
-						ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
-					}
-				}
-					break;
-			}
-		}
-		else
-			break;
-
-		usleep(100000);
-	}
-}
-
-int SpawnTask()
-{
-	system("/root/Module_EventLogging &");
-	system("/root/Module_PrimaryComm &");
-	system("/root/Module_EvComm &");
-	system("/root/Module_LcmControl &");
-	system("/root/Module_InternalComm &");
-	system("/root/Module_PsuComm &");
-
-	//system("/root/OcppBackend &");
-	//system("/root/Module_4g &");
-	//system("/root/Module_Wifi &");
-	//system("/root/Module_PsuComm &");
-	//system("/root/InfyPowerPsu_Comm &");
-
-	// 加入參數
-//	char str[64];
-//	memset(str, '\0', sizeof(65));
-//	sprintf(str, "/root/Module_EvComm %x &", (CHAdeMO_QUANTITY + CCS_QUANTITY));
-//	printf("%s \n", str);
-//	system(str);
-
-	return PASS;
-}
-
-int StoreUsrConfigData(struct SysConfigData *UsrData)
-{
-	int fd,wrd;
-	unsigned int i, Chk;
-	byte *ptr;
-
-	Chk = 0;
-	ptr = (byte *) UsrData;
-	for (i = 0; i < sizeof(struct SysConfigData) - 4; i++)
-	{
-		Chk += *(ptr + i);
-	}
-	UsrData->Checksum = Chk;
-
-	fd = open("/dev/mtdblock10", O_RDWR);
-	if (fd < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]StoreUsrConfigData: open /dev/mtdblock10 NG");
-		#endif
-		return 0;
-	}
-	wrd = write(fd, UsrData, sizeof(struct SysConfigData));
-	close(fd);
-	if (wrd != (sizeof(struct SysConfigData)))
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]StoreUsrConfigData: write /dev/mtdblock10 NG");
-		#endif
-		return 0;
-	}
-
-	fd = open("/dev/mtdblock11", O_RDWR);
-	if (fd < 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
-		#endif
-		return 0;
-	}
-	wrd = write(fd, UsrData, sizeof(struct SysConfigData));
-	close(fd);
-	if (wrd != (sizeof(struct SysConfigData)))
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("[main]StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
-		#endif
-		return 0;
-	}
-	return 1;
-}
-
-//===============================================
-// Common Detect Chk - Stop Charging ?
-//===============================================
-bool isEvBoardStopChargeFlag(byte gunIndex)
-{
-	return chargingInfo[gunIndex]->StopChargeFlag;
-}
-
-//===============================================
-// Common Detect Chk - Chademo
-//===============================================
-bool isEvGunLocked_chademo(byte gunIndex)
-{
-	return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
-}
-
-bool isEvContactorWelding_chademo(byte gunIndex)
-{
-	return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
-}
-
-bool isEvStopReq_chademo(byte gunIndex)
-{
-	return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
-}
-
-bool isEvStopCharging_chademo(byte gunIndex)
-{
-	if (isEvGunLocked_chademo(gunIndex) == NO)
-	{
-		// 無鎖槍 = 停止
-		printf("gun locked none. \n");
-		return YES;
-	}
-
-	return NO;
-}
-
-byte isPrechargeStatus_chademo(byte gunIndex)
-{
-	byte result = 0x00;
-
-	result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
-
-	return result;
-}
-//===============================================
-// Common Detect Chk - CCS
-//===============================================
-bool isEvGunLocked_ccs(byte gunIndex)
-{
-	return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
-}
-
-byte isPrechargeStatus_ccs(byte gunIndex)
-{
-	byte result = 0x00;
-
-	if (ShmCcsData->CommProtocol == 0x01)
-	{
-		result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
-	}
-
-	return result;
-}
-
-bool isEvStopCharging_ccs(byte gunIndex)
-{
-	if (isEvGunLocked_ccs(gunIndex) == NO)
-	{
-		// 無鎖槍 = 停止
-		printf("gun locked none. \n");
-		return YES;
-	}
-
-	return NO;
-}
-
-//===============================================
-// Callback
-//===============================================
-void _AutoReturnTimeout()
-{
-	if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
-	{
-		ClearDetectPluginFlag();
-	}
-
-	strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
-	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
-//
-//	for (byte i = 0; i < CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY; i++)
-//	{
-//		if (chargingInfo[i]->SystemStatus > S_IDLE)
-//		{
-//			switch(chargingInfo[i]->SystemStatus)
-//			{
-//				case S_PREPARNING:
-//				case S_PREPARING_FOR_EV:
-//				case S_PREPARING_FOR_EVSE:
-//				{
-//					ChangeLcmByIndex(i, _LCM_PRE_CHARGE);
-//				}
-//					break;
-//				case S_CHARGING:
-//				{
-//					ChangeLcmByIndex(i, _LCM_CHARGING);
-//				}
-//					break;
-//				case S_TERMINATING:
-//				{
-//					ChangeLcmByIndex(i, _LCM_COMPLETE);
-//				}
-//					break;
-//			}
-//			return;
-//		}
-//	}
-
-//	if (!IsAuthorizingMode())
-//		ChangeLcmByIndex(255, _LCM_IDLE);
-}
-
-void _SelfTestTimeout()
-{
-	if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
-	{
-		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
-		{
-			setChargerMode(gun_index, MODE_ALARM);
-		}
-		ShmPsuData->Work_Step = _NO_WORKING;
-	}
-}
-
-void _AuthorizedTimeout()
-{
-	if(IsAuthorizingMode())
-	{
-		printf("_AuthorizedTimeout \n");
-		ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
-		//ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
-		strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
-		ClearAuthorizedFlag();
-	}
-}
-
-void _DetectPlugInTimeout()
-{
-	if(isDetectPlugin())
-	{
-		printf("_DetectPlugInTimeout \n");
-		ClearDetectPluginFlag();
-	}
-	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
-}
-
-void _DetectEvChargingEnableTimeout(byte gunIndex)
-{
-	if (chargingInfo[gunIndex]->Type == _Type_Chademo)
-	{
-		if(!isEvGunLocked_chademo(gunIndex))
-		{
-			printf("_DetectEvChargingEnableTimeout (chademo) \n");
-			ChargingTerminalProcess(gunIndex);
-			_AutoReturnTimeout();
-		}
-	}
-	else if (chargingInfo[gunIndex]->Type == _Type_CCS)
-	{
-		if(!isEvGunLocked_ccs(gunIndex))
-		{
-			printf("_DetectEvChargingEnableTimeout (ccs) \n");
-			ChargingTerminalProcess(gunIndex);
-			_AutoReturnTimeout();
-		}
-	}
-}
-
-void _DetectEvseChargingEnableTimeout(byte gunIndex)
-{
-	printf("_DetectEvseChargingEnableTimeout (GFD timeout) \n");
-	if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
-	{
-		setChargerMode(gunIndex, MODE_IDLE);
-		_AutoReturnTimeout();
-	}
-}
-
-void _PrepareTimeout(byte gunIndex)
-{
-	printf("_PrechargeTimeout \n");
-	setChargerMode(gunIndex, MODE_IDLE);
-	_AutoReturnTimeout();
-}
-
-void _CompleteTimeout(byte gunIndex)
-{
-	printf("_CompleteTimeout ====> %d \n", gunIndex);
-	setChargerMode(gunIndex, MODE_IDLE);
-}
-
-void _CcsPrechargeTimeout(byte gunIndex)
-{
-	printf("_CcsPrechargeTimeout \n");
-	setChargerMode(gunIndex, MODE_IDLE);
-}
-
-//===============================================
-// 取得卡號與卡號驗證
-//===============================================
-bool canStartCharging()
-{
-	char buf2[16] = "";
-	memset(buf2, 0, ARRAY_SIZE(buf2));
-
-	for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
-	{
-		sprintf(buf2 + (index - 1) * 2, "%02X",	ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
-	}
-	sprintf(buf2, "%s",	ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
-
-	// 因為無法得知實際的長度,所以只能用搜尋的方式
-	if(strstr(buf2, "Accepted") != 0)
-		return true;
-	else
-	{
-
-	}
-
-	return false;
-}
-
-void AuthorizingStart()
-{
-	ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
-}
-
-void ClearAuthorizedFlag()
-{
-	ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
-	ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
-}
-
-bool isAuthorizedComplete()
-{
-	if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
-		return false;
-
-	return true;
-}
-
-bool IsAuthorizingMode()
-{
-	if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq == NO)
-		return false;
-
-	return true;
-}
-
-byte GetCardNumber()
-{
-	byte card_number[16];
-
-		if(getRequestCardSN(rfidFd, 0, card_number))
-		{
-			printf("get card number \n");
-			if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
-			{
-				//Get Card Number
-							byte len = card_number[0];
-							char buf2[32] = "";
-
-							memcpy(buf2, (card_number + 1), len);
-							memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x0, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
-							for (byte index = 0; index < len; index++)
-							{
-								sprintf((char *)ShmSysConfigAndInfo->SysConfig.UserId + (index * 2), "%02X", buf2[index]);
-							}
-
-							printf("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
-							return PASS;
-			}
-	}
-
-	return FAIL;
-}
-
-//===============================================
-// 掃描插槍狀況
-//===============================================
-void ClearDetectPluginFlag()
-{
-	ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
-}
-
-void DetectPluginStart()
-{
-	ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
-}
-
-bool isDetectPlugin()
-{
-	if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
-		return YES;
-
-	return NO;
-}
-
-//===============================================
-// EmergencyStop and Charging Stop
-//===============================================
-void ChargingTerminalProcess(byte gunIndex)
-{
-	setChargerMode(gunIndex, MODE_TERMINATING);
-}
-
-void StopChargingProcessByString(byte level, byte gun_index, char *string)
-{
-	if (strlen((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index]) == 0 ||
-		level > ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level)
-	{
-		memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], string, 7);
-		ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = level;
-	}
-}
-
-void ReleaseChargingProcessByString(byte gun_index, char *code)
-{
-	memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], "", 7);
-	ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = 0;
-}
-
-// 各小板的停止充電處理函式
-void EmcOccureByString(byte index, char *code)
-{
-	bool isStopCharger = false;
-	if (strncmp(code, "012251", 6) == 0 || strncmp(code, "012252", 6) == 0 ||
-			strncmp(code, "012237", 6) == 0 || strncmp(code, "012238", 6) == 0)
-	{
-		isStopCharger = true;
-	}
-
-	if (isStopCharger)
-	{
-		for (byte gun = 0; gun < _gunCount; gun++)
-		{
-			if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
-					(chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
-			{
-				ChargingTerminalProcess(gun);
-			}
-
-			StopChargingProcessByString(2, gun, code);
-		}
-	}
-	else
-	{
-		if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
-				(chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
-		{
-			ChargingTerminalProcess(index);
-		}
-
-		StopChargingProcessByString(2, index, code);
-	}
-}
-
-void ReleaseEmsOccureByString(byte index, char *code)
-{
-	bool isReleaseCharger = false;
-	bool isTrigger = false;
-	if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
-	{
-		isTrigger = true;
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
-		isReleaseCharger = true;
-	}
-	else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
-	{
-		isTrigger = true;
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
-		isReleaseCharger = true;
-	}
-	else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
-	{
-		isTrigger = true;
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
-		isReleaseCharger = true;
-	}
-	else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
-	{
-		isTrigger = true;
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
-		isReleaseCharger = true;
-	}
-	else if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
-	{
-		isTrigger = true;
-		ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
-	}
-
-	if (isTrigger)
-	{
-		if (isReleaseCharger)
-		{
-			for (byte gun = 0; gun < _gunCount; gun++)
-			{
-				ReleaseChargingProcessByString(gun, code);
-			}
-		}
-		else
-		{
-			ReleaseChargingProcessByString(index, code);
-		}
-	}
-}
-
-//===============================================
-// 確認硬體 (按鈕) 狀態
-//===============================================
-bool leftBtnPush = false;
-bool rightBtnPush = false;
-
-void ChkPrimaryStatus()
-{
-	if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
-	{
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
-		EmcOccureByString(0, "012251");
-	}
-	else
-		ReleaseEmsOccureByString(0, "012251");
-
-	if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
-	{
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
-		EmcOccureByString(0, "012238");
-	}
-	else
-		ReleaseEmsOccureByString(0, "012238");
-
-	if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
-	{
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
-		EmcOccureByString(0, "012237");
-	}
-	else
-		ReleaseEmsOccureByString(0, "012237");
-
-	if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
-	{
-		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
-		EmcOccureByString(0, "012252");
-	}
-	else
-		ReleaseEmsOccureByString(0, "012252");
-
-	if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
-	{
-		if(!leftBtnPush)
-		{
-			printf("left btn down............................... \n");
-			ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
-			switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
-			{
-				case S_IDLE:
-				{
-					ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
-				}
-				break;
-				case S_CHARGING:
-				{
-					// 停止充電
-					printf("To Stop = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-					chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 9;
-					//ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-				}
-					break;
-				case S_COMPLETE:
-				{
-					// 回 IDLE
-					printf("Back to IDLE = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-					chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 1;
-				}
-					break;
-			}
-		}
-
-		leftBtnPush = true;
-		// 左邊的選槍按鈕,只有在雙槍都在充電時候才有用 : 30KW 以下該按鈕無作用
-	}
-	else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
-	{
-		if(leftBtnPush)
-			printf("left btn up............................... \n");
-		leftBtnPush = false;
-	}
-
-	if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
-	{
-		if(!rightBtnPush)
-		{
-			printf("right btn down............................... \n");
-			if (_gunCount > 1)
-			{
-				ShmSysConfigAndInfo->SysInfo.CurGunSelected = 1;
-				switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
-				{
-					case S_IDLE:
-					{
-						ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
-					}
-					break;
-					case S_CHARGING:
-					{
-						// 停止充電
-						printf("To Stop = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-						chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 9;
-						//ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-					}
-						break;
-					case S_COMPLETE:
-					{
-						// 回 IDLE
-						printf("Back to IDLE = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-						chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 1;
-					}
-						break;
-				}
-			}
-		}
-
-		rightBtnPush = true;
-		// 右邊的按鈕,只作用在當前頁面,當前頁面如果在可以回 Home 與 停止充電的狀態為可用
-//		switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
-//		{
-//			case S_IDLE: {}break;
-//			case S_AUTHORIZING: {}break;
-//			case S_PREPARING_FOR_EV: {}break;
-//			case S_PREPARING_FOR_EVSE: {}break;
-//			case S_CCS_PRECHARGE_ST0: {}break;
-//			case S_CCS_PRECHARGE_ST1: {}break;
-//			case S_CHARGING:
-//			{
-//				// 停止充電
-//				printf("Stop --------------------------------------------------- \n");
-//				//ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
-//			}
-//				break;
-//			case S_COMPLETE:
-//			{
-//				//setChargerMode(ShmSysConfigAndInfo->SysInfo.CurGunSelected, MODE_IDLE);
-//			}
-//				break;
-//		}
-	}
-	else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
-	{
-		if(rightBtnPush)
-			printf("right btn up............................... \n");
-		rightBtnPush = false;
-	}
-}
-
-//===============================================
-// 確認各小板偵測的錯誤狀況
-//===============================================
-void CheckErrorOccurStatus(byte index)
-{
-	// 小板
-	if (chargingInfo[index]->Type == _Type_Chademo)
-	{
-		if (ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
-			EmcOccureByString(index, "023730");
-		else if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
-			EmcOccureByString(index, "011012");
-		else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
-			EmcOccureByString(index, "012234");
-	}
-	else if (chargingInfo[index]->Type == _Type_CCS)
-	{
-		if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
-			EmcOccureByString(index, "011014");
-		else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
-			EmcOccureByString(index, "012235");
-	}
-}
-
-//===============================================
-// 確認 GPIO 狀態
-//===============================================
-int gpio_get_value(unsigned int gpio, unsigned int *value)
-{
-    int fd;
-    char buf[MAX_BUF];
-    char ch;
-
-    snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
-
-    fd = open(buf, O_RDONLY);
-    if (fd < 0) {
-        perror("gpio/get-value");
-        return fd;
-    }
-
-    read(fd, &ch, 1);
-
-    if (ch != '0') {
-        *value = 1;
-    } else {
-        *value = 0;
-    }
-
-    close(fd);
-    return 0;
-}
-
-void CheckGunTypeFromHw()
-{
-	int pinIn[4] = { 22, 23, 44, 45 };
-	unsigned int gpioValue = 0;
-
-	for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
-		gpio_get_value(pinIn[i], &gpioValue);
-		{
-			switch (pinIn[i])
-			{
-			case 22:
-				bd1_1_status = gpioValue;
-				break;
-			case 23:
-				bd1_2_status = gpioValue;
-				break;
-			case 44:
-				bd0_1_status = gpioValue;
-				break;
-			case 45:
-				bd0_2_status = gpioValue;
-				break;
-			}
-		}
-	}
-}
-
-void CheckGpioInStatus()
-{
-	int pinIn[2] = { 27, 47 };
-	unsigned int gpioValue = 0;
-
-	for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
-	{
-		gpio_get_value(pinIn[i], &gpioValue);
-		if (gpioValue == 0x01)
-		{
-			switch(pinIn[i])
-			{
-				// 小板緊急停止
-				case 27:
-				{
-					for(int i = 0; i < _gunCount; i++)
-					{
-						if (chargingInfo[i]->slotsIndex == 1)
-						{
-							if (chargingInfo[i]->Type == _Type_Chademo)
-								EmcOccureByString(i, "023730");
-							else if (chargingInfo[i]->Type == _Type_CCS)
-								EmcOccureByString(i, "013627");
-							break;
-						}
-					}
-				}
-					break;
-				case 47:
-				{
-					for(int i = 0; i < _gunCount; i++)
-					{
-						if (chargingInfo[i]->slotsIndex == 3)
-						{
-							if (chargingInfo[i]->Type == _Type_Chademo)
-								EmcOccureByString(i, "023730");
-							else if (chargingInfo[i]->Type == _Type_CCS)
-								EmcOccureByString(i, "013627");
-							break;
-						}
-					}
-				}
-					break;
-			}
-		}
-		else
-		{
-			switch (pinIn[i])
-			{
-				// 小板解除緊急停止
-				case 27:
-				{
-					for(int i = 0; i < _gunCount; i++)
-					{
-						if (chargingInfo[i]->slotsIndex == 1)
-						{
-							if (chargingInfo[i]->Type == _Type_Chademo)
-								ReleaseEmsOccureByString(i, "023730");
-							else if (chargingInfo[i]->Type == _Type_CCS)
-								ReleaseEmsOccureByString(i, "013627");
-							break;
-						}
-					}
-				}
-					break;
-				case 47:
-				{
-					// 右槍
-					for (int i = 0; i < _gunCount; i++)
-					{
-						if (chargingInfo[i]->slotsIndex == 3)
-						{
-							if (chargingInfo[i]->Type == _Type_Chademo)
-								ReleaseEmsOccureByString(i, "023730");
-							else if (chargingInfo[i]->Type == _Type_CCS)
-								ReleaseEmsOccureByString(i, "013627");
-							break;
-						}
-					}
-				}
-				break;
-			}
-		}
-	}
-}
-
-//===============================================
-// Main process
-//===============================================
-// 檢查 Byte 中某個 Bit 的值
-// _byte : 欲改變的 byte
-// _bit : 該 byte 的第幾個 bit
-unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
-{
-	return ( _byte & mask_table[_bit] ) != 0x00;
-}
-
-// 設定 Byte 中某個 Bit的值
-// _byte : 欲改變的 byte
-// _bit : 該 byte 的第幾個 bit
-// value : 修改的值為 0 or 1
-void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
-{
-	if(value == 1)
-		*_byte |= (1 << _bit);
-	else if (value == 0)
-		*_byte ^= (1 << _bit);
-}
-
-void UserScanFunction()
-{
-	bool idleReq = false;
-	unsigned char stopReq = 255;
-
-	// 當前非驗證的狀態
-	if(!IsAuthorizingMode())
-	{
-		// 先判斷現在是否可以提供刷卡
-		// 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
-		// 2. 停止充電
-		for (byte i = 0; i < _gunCount; i++)
-		{
-			// 二擇一
-			if (chargingInfo[i]->SystemStatus == S_CHARGING)
-			{
-				stopReq = i;
-			}
-			else if (chargingInfo[i]->SystemStatus == S_IDLE)
-			{
-				idleReq = true;
-			}
-		}
-
-		//printf("idleReq = %x, stopReq = %d \n", idleReq, stopReq);
-		// 有閒置的槍號,即可接受刷卡
-		if (idleReq || stopReq < _gunCount)
-		{
-			// 取卡號,假設 : 刷卡過了
-			if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
-			{
-				if (stopReq < _gunCount)
-				{
-					char value[32];
-
-					memcpy(value, (unsigned char *)chargingInfo[stopReq]->CardNumber, ARRAY_SIZE(chargingInfo[stopReq]->CardNumber));
-					if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
-					{
-						ChargingTerminalProcess(stopReq);
-						strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
-						return;
-					}
-				}
-
-				if (idleReq)
-				{
-					// LCM => Authorizing
-					ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
-					// 進入確認卡號狀態
-					AuthorizingStart();
-					// authorizing timer
-					StartSystemTimeoutDet(Timeout_Authorizing);
-					autoReturnTimeoutFlag = NO;
-				}
-			}
-		}
-	}
-	else
-	{
-		// 確認驗證卡號完成沒
-		if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
-		{
-			StopSystemTimeoutDet();
-			// 判斷後台回覆狀態
-			if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
-			{
-				// LCM => Authorize complete
-				ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
-				// 通過認證,開始確認當前要進入充電的槍號
-				DetectPluginStart();
-			}
-			else
-			{
-				// LCM => Authorize fail
-				ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
-			}
-			ClearAuthorizedFlag();
-		}
-	}
-}
-
-unsigned char isModeChange(unsigned char gun_index)
-{
-	unsigned char result = NO;
-
-	if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
-	{
-		result = YES;
-		chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
-	}
-
-	return result;
-}
-
-void ScannerCardProcess()
-{
-	if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 0)
-	{
-		isCardScan = true;
-		// 處理刷卡及驗證卡號的動作
-		UserScanFunction();
-	}
-
-	if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
-	{
-		StartSystemTimeoutDet(Timeout_VerifyFail);
-		isCardScan = false;
-	}
-	else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
-	{
-		StartSystemTimeoutDet(Timeout_VerifyComp);
-	}
-	else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
-	{
-		StartSystemTimeoutDet(Timeout_WaitPlug);
-	}
-	else
-		isCardScan = false;
-}
-
-void AddGunInfoByConnector(byte typeValue, byte slots)
-{
-	switch (typeValue)
-	{
-		case '0': // none
-			break;
-		case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
-			break;
-		case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
-			break;
-		case '3': // IEC 62196-2 Type 2 Plug
-			break;
-		case '4': // IEC 62196-2 Type 2 Socket
-			break;
-		case '5': // GB/T AC Plug
-			break;
-		case '6': // GB/T AC Socket
-			break;
-		case 'J': // CHAdeMO
-		{
-			if (CHAdeMO_QUANTITY > _chademoIndex)
-			{
-				chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
-
-				chargingInfo[_gunIndex]->Index = _gunIndex;
-				chargingInfo[_gunIndex]->slotsIndex = slots;
-				chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
-				chargingInfo[_gunIndex]->Type = _Type_Chademo;
-				chargingInfo[_gunIndex]->type_index = _chademoIndex;
-				_chademoIndex++;
-				_gunIndex++;
-			}
-		}
-			break;
-		case 'U': // CCS1 combo
-			break;
-		case 'E': // CCS2 combo
-		{
-			if (CCS_QUANTITY > _ccsIndex)
-			{
-				chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
-
-				chargingInfo[_gunIndex]->Index =	_gunIndex;
-				chargingInfo[_gunIndex]->slotsIndex = slots;
-				chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
-				chargingInfo[_gunIndex]->Type = _Type_CCS;
-				chargingInfo[_gunIndex]->type_index = _ccsIndex;
-				// 現階段預設為走 DIN70121
-				ShmCcsData->CommProtocol = 0x01;
-				_ccsIndex++;
-				_gunIndex++;
-			}
-		}
-			break;
-		case 'G': // GBT DC
-			break;
-		case 'D': // GBT DC x 2
-			break;
-	}
-}
-
-bool CheckConnectorTypeStatus()
-{
-	bool result = true;
-
-	printf("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
-			bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
-	if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
-	{
-		byte slots = 1;
-		for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
-		{
-			AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots);
-			slots++;
-		}
-
-		_gunCount = _gunIndex;
-		printf("_gunCount = %d \n", _gunCount);
-		if (_gunCount == 0)
-			result = false;
-
-		// 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
-		for (byte gunIndex = 0; gunIndex < _gunCount; gunIndex++)
-		{
-			if (bd0_1_status == 0 && bd0_2_status == 1)
-			{
-				// 與硬體相同 type : Chademo
-				if (chargingInfo[gunIndex]->Type == _Type_Chademo)
-				{
-					chargingInfo[gunIndex]->Evboard_id = 0x01;
-				}
-			}
-			else if (bd0_1_status == 1 && bd0_2_status == 0)
-			{
-				// 與硬體相同 type : CCS
-				if (chargingInfo[gunIndex]->Type == _Type_CCS)
-				{
-					chargingInfo[gunIndex]->Evboard_id = 0x01;
-				}
-			}
-
-			if (bd1_1_status == 0 && bd1_2_status == 1)
-			{
-				// 與硬體相同 type : Chademo
-				if (chargingInfo[gunIndex]->Type == _Type_Chademo)
-				{
-					chargingInfo[gunIndex]->Evboard_id = 0x02;
-				}
-
-				if (_gunCount == 1)
-					chargingInfo[gunIndex]->Evboard_id = 0x01;
-			}
-			else if (bd1_1_status == 1 && bd1_2_status == 0)
-			{
-				// 與硬體相同 type : CCS
-				if (chargingInfo[gunIndex]->Type == _Type_CCS)
-				{
-					chargingInfo[gunIndex]->Evboard_id = 0x02;
-				}
-
-				if (_gunCount == 1)
-					chargingInfo[gunIndex]->Evboard_id = 0x01;
-			}
-
-			printf("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
-			if (chargingInfo[gunIndex]->Evboard_id == 0x00)
-				result = false;
-		}
-	}
-	else
-	{
-		// Module Name 不正確 - 告警
-		result = false;
-	}
-
-	return result;
-}
-
-void KillTask()
-{
-	ChangeLcmByIndex(_LCM_FIX);
-	system("killall Module_EventLogging");
-	system("killall Module_PrimaryComm");
-	system("killall Module_EvComm");
-	system("killall Module_LcmControl");
-	system("killall Module_InternalComm");
-	system("killall Module_PsuComm");
-}
-
-char CheckUpdateProcess()
-{
-	DIR *d;
-	struct dirent *dir;
-	d = opendir("/mnt/");
-
-	if (d)
-	{
-		long int MaxLen=48*1024*1024, ImageLen = 0;
-		while ((dir = readdir(d)) != NULL)
-		{
-			char *new_str;
-			new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
-			new_str[0] = '\0';
-			strcat(new_str, "/mnt/");
-			strcat(new_str, dir->d_name);
-			int fd = open(new_str, O_RDONLY);
-			if (fd < 0)
-			{
-				return FAIL;
-			}
-
-			unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
-			memset(ptr, 0xFF, MaxLen);  //-48 is take out the header
-			//get the image length
-			ImageLen = read(fd, ptr, MaxLen);
-
-			if (ImageLen > 20)
-			{
-				unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
-			    printf("Typed...%x \r\n", Type);
-
-			    switch (Type)
-			    {
-			    	case 0x10000001:
-			    	case 0x10000002:
-			    	case 0x10000003:
-			    	case 0x10000004:
-			    	case 0x10000005:
-			    	{
-			    		if (Upgrade_Flash(Type, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
-			    			return PASS;
-			    		else
-			    			return FAIL;
-			    	}
-			    	break;
-			    	case 0x10000006:
-			    	case 0x1000000D:
-			    	case 0x1000000E:
-			    	{
-			    		// CSU_PRIMARY_CONTROLLER : 0x10000006
-						byte target = 0x00;
-
-						if (Type == 0x10000006)
-							target = UPGRADE_PRI;
-						else if (Type == 0x1000000D)
-							target = UPGRADE_RB;
-						else if (Type == 0x1000000E)
-							target = UPGRADE_FAN;
-
-						int fd = InitComPort(target);
-
-			    		if (Upgrade_UART(fd, Type, target, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
-			    			return PASS;
-			    		else
-			    			return FAIL;
-
-			    		close(fd);
-			    	}
-			    	break;
-			    	case 0x10000007:
-			    	case 0x10000008:
-			    	case 0x10000009:
-			    	case 0x1000000A:
-			    	case 0x1000000B:
-			    	case 0x1000000C:
-			    	{
-			    		// CHAdeMO_BOARD : 0x1000000B
-			    		for(byte index = 0; index < _gunCount; index++)
-			    		{
-			    			if (chargingInfo[index]->Type == _Type_Chademo)
-			    			{
-			    				int CanFd = InitCanBus();
-			    				if (CanFd > 0)
-			    				{
-			    					if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
-			    					{
-			    						printf("Upgrad OK. \n");
-			    						return PASS;
-			    					}
-			    					else
-			    					{
-			    						printf("Upgrad Fail. \n");
-			    						return FAIL;
-			    					}
-			    				}
-			    				else
-			    				{
-			    					printf("Upgrad FD fail. \n");
-			    					return FAIL;
-			    				}
-			    			}
-			    		}
-			    		break;
-			    	}
-			    }
-			}
-			free(new_str);
-			free(ptr);
-		}
-	}
-	free(dir);
-	closedir(d);
-	return FAIL;
-}
-
-void CreateRfidFork()
-{
-	pid_t rfidRecPid;
-
-	rfidRecPid = fork();
-	if (rfidRecPid == 0)
-	{
-		while(true)
-		{
-			// 刷卡判斷
-			GetCardNumber();
-			usleep(100000);
-		}
-	}
-}
-
-void StartSystemTimeoutDet(unsigned char flag)
-{
-	if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
-	{
-		gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
-	}
-	ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
-}
-
-void StopSystemTimeoutDet()
-{
-	ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
-}
-
-void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
-{
-	if (gunIndex < _gunCount)
-	{
-		if (chargingInfo[gunIndex]->TimeoutFlag != flag)
-		{
-			gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
-		}
-		chargingInfo[gunIndex]->TimeoutFlag = flag;
-	}
-}
-
-void StopGunInfoTimeoutDet(unsigned char gunIndex)
-{
-	if (gunIndex < _gunCount)
-	{
-		chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
-	}
-}
-
-void CreateTimeoutFork()
-{
-	pid_t timeoutPid;
-
-	timeoutPid = fork();
-	if (timeoutPid == 0)
-	{
-		while(true)
-		{
-			// 系統
-			switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
-			{
-				case Timeout_SelftestChk:
-					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 60000000)
-					{
-						_SelfTestTimeout();
-						StopSystemTimeoutDet();
-					}
-					break;
-				case Timeout_Authorizing:
-					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 60000000)
-					{
-						_AuthorizedTimeout();
-						StopSystemTimeoutDet();
-					}
-					break;
-				case Timeout_VerifyFail:
-					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
-					{
-						_AutoReturnTimeout();
-						StopSystemTimeoutDet();
-					}
-					break;
-				case Timeout_VerifyComp:
-					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
-					{
-						_AutoReturnTimeout();
-						StopSystemTimeoutDet();
-					}
-					break;
-				case Timeout_WaitPlug:
-					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
-					{
-						_DetectPlugInTimeout();
-						StopSystemTimeoutDet();
-					}
-					break;
-			}
-			// 各槍
-			for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
-			{
-				switch(chargingInfo[gun_index]->TimeoutFlag)
-				{
-					case Timeout_Preparing:
-						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 30000000)
-						{
-							_PrepareTimeout(gun_index);
-							StopGunInfoTimeoutDet(gun_index);
-						}
-						break;
-					case Timeout_EvChargingDet:
-						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
-						{
-							_DetectEvChargingEnableTimeout(gun_index);
-							StopGunInfoTimeoutDet(gun_index);
-						}
-						break;
-					case Timeout_EvseChargingDet:
-						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
-						{
-							_DetectEvseChargingEnableTimeout(gun_index);
-							StopGunInfoTimeoutDet(gun_index);
-						}
-						break;
-					case Timeout_WaitforCompleteDet:
-						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
-						{
-							_CompleteTimeout(gun_index);
-							StopGunInfoTimeoutDet(gun_index);
-						}
-						break;
-					case Timeout_ForCcsPrechargeDet:
-						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
-						{
-							_CcsPrechargeTimeout(gun_index);
-							StopGunInfoTimeoutDet(gun_index);
-						}
-						break;
-				}
-			}
-			usleep(100000);
-		}
-	}
-}
-
-int main(void)
-{
-	InitGPIO();
-	InitEthernet();
-	sleep(1);
-	system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
-	sleep(1);
-	system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
-
-	//echo 1 > /sys/class/gpio/gpio110/value
-	//return 0;
-	if(CreateShareMemory() == 0)
-	{
-		#ifdef SystemLogMessage
-		DEBUG_ERROR_MSG("CreatShareMemory NG \n");
-		#endif
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
-		}
-		return 0;
-		sleep(5);
-		system("reboot -f");
-		sleep(5);
-		system("reboot -f");
-	}
-
-	printf("\n");
-	printf("CheckGunTypeFromHw....... \n");
-	CheckGunTypeFromHw();
-	char *moduleName = "DSYE601E00T2PH";
-	memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, moduleName, strlen(moduleName));
-
-	if (!CheckConnectorTypeStatus())
-	{
-		// Module Name 與硬體對應不正確
-		printf("Module Name & HW info none match. \n");
-		DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
-		sleep(5);
-		return 0;
-	}
-
-	printf("Module Name & HW info correct. Initialize.......\n");
-	Initialization();
-	printf("Spawn all Task. \n");
-	SpawnTask();
-
-	ChangeLcmByIndex(_LCM_INIT);
-	CreateTimeoutFork();
-	printf("Self test. \n");
-	SelfTestRun();
-	StopSystemTimeoutDet();
-
-	if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
-	{
-		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
-		{
-			setChargerMode(gun_index, MODE_ALARM);
-		}
-		ChangeLcmByIndex(_LCM_FIX);
-		return FAIL;
-	}
-	else
-	{
-		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
-		{
-			setChargerMode(gun_index, MODE_IDLE);
-		}
-	}
-	ChangeLcmByIndex(_LCM_IDLE);
-	sleep(1);
-	//***** 須新增的偵測 *****//
-	// 1. Thernal - 控制風扇轉速
-	// 2. ouput fuse - 控制風扇轉速
-
-	CreateRfidFork();
-	// Main loop
-	printf("Main Loop. \n");
-	for (;;)
-	{
-		ChkPrimaryStatus();
-		if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_IDLE)
-		{
-			//printf("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
-			if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
-			{
-				KillTask();
-				if (CheckUpdateProcess() == PASS)
-				{
-					printf("update complete. \n");
-				}
-				else
-				{
-					printf("update fail. \n");
-				}
-				ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
-				sleep(5);
-				system("reboot -f");
-			}
-		}
-//		usleep(whileLoopTime * 100);
-//		continue;
-		// 讀卡邏輯
-		ScannerCardProcess();
-
-		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
-		{
-			CheckGpioInStatus();
-			CheckErrorOccurStatus(gun_index);
-
-			switch(chargingInfo[gun_index]->SystemStatus)
-			{
-				case S_IDLE:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_IDLE================================== %x \n", gun_index);
-						chargingInfo[gun_index]->RemainChargingDuration = 0;
-						chargingInfo[gun_index]->PresentChargedEnergy = 0;
-					}
-
-					if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 2)
-					{
-						ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
-						ClearDetectPluginFlag();
-					}
-					else
-					{
-						// 判斷是否有啟用檢查插槍
-						if(isDetectPlugin())
-						{
-							// 卡號驗證成功後,等待充電槍插入充電車
-							if (chargingInfo[gun_index]->ConnectorPlugIn == YES)
-							{
-								ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
-								strcpy((char *)chargingInfo[gun_index]->CardNumber, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
-								// 當前操作的槍號,進入 Preparing
-								setChargerMode(gun_index, MODE_REASSIGN_CHECK);
-								ClearDetectPluginFlag();
-								ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
-							}
-							else if (!isCardScan)
-							{
-								// LCM => Waiting for plugging
-								ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
-							}
-						}
-						else
-						{
-							if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-								ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
-						}
-					}
-				}
-					break;
-				case S_REASSIGN_CHECK:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_REASSIGN_CHECK================================== %x \n", gun_index);
-						StopSystemTimeoutDet();
-					}
-
-					bool isRessign = false;
-					if (_gunCount > 1)
-					{
-						for (byte index = 0; index < _gunCount; index++)
-						{
-							// 有其他槍已經分配好 psu 模塊
-							if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
-									chargingInfo[index]->SystemStatus >= S_PREPARNING)
-							{
-								printf("=============Smart Charging============= Step 1 \n");
-								ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE;
-								isRessign = true;
-								break;
-							}
-						}
-					}
-
-					if (isRessign)
-						setChargerMode(gun_index, MODE_REASSIGN);
-					else
-						setChargerMode(gun_index, MODE_PRECHARGE);
-				}
-					break;
-				case S_REASSIGN:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_REASSIGN================================== %x \n", gun_index);
-					}
-
-					// 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
-					// 此狀態下~ 控制權在於 PSU 及 EV小板 Process
-					if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
-							ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_MAIN)
-						setChargerMode(gun_index, MODE_PRECHARGE);
-					else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY &&
-						ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
-							ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_MAIN;
-				}
-					break;
-				case S_PREPARNING:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_PREPARNING================================== %x \n", gun_index);
-						StopGunInfoTimeoutDet(gun_index);
-						StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
-					}
-
-					if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
-						ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
-
-					// Precharge 三個流程 : 1 Precharge, 2 Preparing for ev, 3 Preparing for evse
-					// Precharge : AC Contactor <Relay board>, Relay k1 k2 <Relay board>, PSU AddressAssignment, PSU GroupAvailablePower
-					// Preparing for ev : 車端通訊流程
-					// Preparing for evse : PSU (output 500V, 2A), GFD Test <Relay board>
-					//ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES &&
-					if (((ShmPsuData->SystemPresentPsuQuantity > 0 &&
-							ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 &&
-							ShmPsuData->PsuGroup[gun_index].GroupAvailablePower > 10) &&
-							chargingInfo[gun_index]->AvailableChargingPower > 10))
-					{
-						setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
-					}
-
-					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
-				}
-					break;
-				case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_PREPARING_FOR_EV================================== %x \n", gun_index);
-						strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
-						StopGunInfoTimeoutDet(gun_index);
-						StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
-					}
-
-					if (chargingInfo[gun_index]->Type == _Type_Chademo)
-					{
-						// 檢查車端的槍鎖是否為鎖上
-						if (isEvGunLocked_chademo(gun_index) == YES)
-						{
-							setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
-						}
-					}
-					else if (chargingInfo[gun_index]->Type == _Type_CCS)
-					{
-						// 檢查車端的 charging enable 是否為 1
-						if (isEvGunLocked_ccs(gun_index) == YES)
-						{
-							setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
-						}
-					}
-
-					if (isEvBoardStopChargeFlag(gun_index) == YES)
-					{
-						// 板端要求停止
-						ChargingTerminalProcess(gun_index);
-					}
-
-					// LCM => Pre-charging
-					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
-				}
-					break;
-				case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
-						StopGunInfoTimeoutDet(gun_index);
-						StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
-					}
-
-					if (chargingInfo[gun_index]->Type == _Type_Chademo)
-					{
-						// 檢查樁端的 GFD 結果
-						if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
-						{
-							// 當前操作的槍號,進入 Charging
-							setChargerMode(gun_index, MODE_CHARGING);
-						}
-					}
-					else if (chargingInfo[gun_index]->Type == _Type_CCS)
-					{
-						// 檢查樁端的 GFD 結果
-						if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS)
-						{
-							setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
-						}
-					}
-
-					if (isEvBoardStopChargeFlag(gun_index) == YES)
-					{
-						// 板端要求停止
-						ChargingTerminalProcess(gun_index);
-					}
-
-					// LCM => Pre-charging
-					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
-				}
-					break;
-				case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("S_CHARGING================================== %x \n", gun_index);
-						StopGunInfoTimeoutDet(gun_index);
-						ftime(&startChargingTime[gun_index]);
-					}
-
-					ftime(&endChargingTime[gun_index]);
-					chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
-
-					if (isEvBoardStopChargeFlag(gun_index) == YES ||
-							chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
-					{
-						// 板端要求停止
-						ChargingTerminalProcess(gun_index);
-					}
-
-					// LCM => Charging
-					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
-				}
-					break;
-				case S_TERMINATING:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf ("terminating......................... %x \n", gun_index);
-						StopGunInfoTimeoutDet(gun_index);
-					}
-
-					if (chargingInfo[gun_index]->Type == _Type_Chademo)
-					{
-						// 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
-						if (isEvStopCharging_chademo(gun_index) == YES)
-						{
-							setChargerMode(gun_index, MODE_COMPLETE);
-						}
-					}
-					else if (chargingInfo[gun_index]->Type == _Type_CCS)
-					{
-						// 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
-						if (isEvStopCharging_ccs(gun_index) == YES)
-						{
-							setChargerMode(gun_index, MODE_COMPLETE);
-						}
-					}
-					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
-					//ChangeLcmByIndex(gun_index, _LCM_COMPLETE);
-				}
-					break;
-				case S_COMPLETE:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf ("complete......................... %x \n", gun_index);
-						ftime(&endChargingTime[gun_index]);
-						if (chargingInfo[gun_index]->RemainChargingDuration != 0)
-							chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
-
-						strcpy((char *)chargingInfo[gun_index]->CardNumber, "");
-						strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
-						StopGunInfoTimeoutDet(gun_index);
-						StartGunInfoTimeoutDet(gun_index, Timeout_WaitforCompleteDet);
-					}
-
-					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
-						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
-				}
-					break;
-				case S_CCS_PRECHARGE_ST0:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("CCS Precharge Processing 1....................%x \n", gun_index);
-						StopGunInfoTimeoutDet(gun_index);
-						StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
-					}
-
-					if (isEvBoardStopChargeFlag(gun_index) == YES)
-					{
-						// 板端要求停止
-						ChargingTerminalProcess(gun_index);
-					}
-
-					// 等待 EV 小板 (CCS) 通知可以開始 Precharge
-					// 切換 D+ Relay to Precharge Relay
-					if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
-					{
-						if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
-						//if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
-						{
-							printf("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
-							chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
-						}
-					}
-					else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
-					{
-						setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
-					}
-
-					break;
-				}
-				case S_CCS_PRECHARGE_ST1:
-				{
-					if (isModeChange(gun_index))
-					{
-						printf("CCS Precharge Processing 2....................%x \n", gun_index);
-					}
-
-					if (isEvBoardStopChargeFlag(gun_index) == YES)
-					{
-						// 板端要求停止
-						ChargingTerminalProcess(gun_index);
-					}
-
-					// 等待小板通知進入充電
-					// 切換 D+ Relay to Precharge Relay
-					if (chargingInfo[gun_index]->RelayK1K2Status == YES)
-					{
-						chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
-						setChargerMode(gun_index, MODE_CHARGING);
-					}
-					break;
-				}
-			}
-		}
-
-		if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
-			ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
-		else
-			ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
-
-		usleep(whileLoopTime);
-	}
-
-	return FAIL;
-}
+#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 	<unistd.h>
+#include 	<stdarg.h>
+#include    <stdio.h>      /*標準輸入輸出定義*/
+#include    <stdlib.h>     /*標準函數庫定義*/
+#include    <unistd.h>     /*Unix 標準函數定義*/
+#include    <fcntl.h>      /*檔控制定義*/
+#include    <termios.h>    /*PPSIX 終端控制定義*/
+#include    <errno.h>      /*錯誤號定義*/
+#include 	<errno.h>
+#include 	<string.h>
+#include 	<stdint.h>
+#include	<time.h>
+#include	<ctype.h>
+#include 	<ifaddrs.h>
+#include 	<math.h>
+#include 	"../../define.h"
+#include 	"Config.h"
+#include 	<stdbool.h>
+#include 	<dirent.h>
+#include	"timeout.h"
+
+#define 	ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define 	PASS				1
+#define 	FAIL				-1
+#define 	BUFFER_SIZE			128
+#define 	YES					1
+#define 	NO					0
+#define 	NORMAL				0
+#define		ABNORMAL			1
+#define 	EQUAL				0
+#define 	BTN_RELEASE			0
+#define 	BTN_PRESS			1
+#define 	MAX_BUF 			64
+#define 	MtdBlockSize 		0x600000
+#define 	SYSFS_GPIO_DIR 		"/sys/class/gpio"
+#define		UPGRADE_FAN			0x02
+#define		UPGRADE_RB			0x03
+#define		UPGRADE_PRI			0x04
+
+char 	*valid_Internet[2] 	  = {"8.8.8.8", "180.76.76.76"};
+unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
+int whileLoopTime = 10000; // 10 ms
+int	wtdFd = -1;
+
+bool IsAuthorizingMode();
+void ClearAuthorizedFlag();
+bool isDetectPlugin();
+void ClearDetectPluginFlag();
+int mystrcmp(unsigned char *p1, unsigned char *p2);
+
+unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
+void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
+void ChargingTerminalProcess(byte gunIndex);
+void ChkPrimaryStatus();
+void StartSystemTimeoutDet(unsigned char flag);
+void StopSystemTimeoutDet();
+void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
+void StopGunInfoTimeoutDet(unsigned char gunIndex);
+int StoreLogMsg_1(const char *fmt, ...);
+unsigned long GetTimeoutValue(struct timeval _sour_time);
+void gpio_set_value(unsigned int gpio, unsigned int value);
+void PRINTF_FUNC(char *string, ...);
+
+#define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+struct PsuData 					*ShmPsuData;
+struct CHAdeMOData				*ShmCHAdeMOData;
+struct CcsData					*ShmCcsData;
+struct PrimaryMcuData			*ShmPrimaryMcuData;
+struct FanModuleData			*ShmFanModuleData;
+struct RelayModuleData			*ShmRelayModuleData;
+struct OCPP16Data				*ShmOCPP16Data;
+
+struct ChargingInfoData			*chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+struct timeb 					startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+struct timeb 					endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
+
+byte _gunCount = 0;
+// for initial index to check EV board type is correct
+byte _gunIndex = 0;
+byte _chademoIndex = 0;
+byte _ccsIndex = 0;
+byte _gb_Index = 0;
+byte bd0_1_status = 0;
+byte bd0_2_status = 0;
+byte bd1_1_status = 0;
+byte bd1_2_status = 0;
+
+bool isCardScan = false;
+
+int rfidFd = -1;
+char* rfidPortName = "/dev/ttyS2";
+
+//================================================
+// initial can-bus
+//================================================
+int InitCanBus()
+{
+	int 					s0,nbytes;
+	struct timeval			tv;
+	struct ifreq 			ifr0;
+	struct sockaddr_can		addr0;
+
+	system("/sbin/ip link set can0 down");
+	system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
+	system("/sbin/ip link set can0 up");
+
+	s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+
+	tv.tv_sec = 0;
+	tv.tv_usec = 10000;
+   	if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct	timeval)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
+		#endif
+	}
+	nbytes=40960;
+	if (setsockopt(s0, SOL_SOCKET,  SO_RCVBUF, &nbytes, sizeof(int)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
+		#endif
+	}
+	nbytes=40960;
+	if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
+		#endif
+	}
+
+   	strcpy(ifr0.ifr_name, "can0" );
+	ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
+	addr0.can_family = AF_CAN;
+	addr0.can_ifindex = ifr0.ifr_ifindex;
+	bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
+	return s0;
+}
+
+//================================================
+// initial uart port
+//================================================
+char *_priPortName = "/dev/ttyS1";
+char *_485PortName = "/dev/ttyS5";
+
+int InitComPort(byte target)
+{
+	int fd;
+	struct termios tios;
+
+	if(target == UPGRADE_PRI)
+		fd = open(_priPortName, O_RDWR);
+	else if (target == UPGRADE_FAN ||  target == UPGRADE_RB)
+		fd = open(_485PortName, O_RDWR);
+
+	if(fd<=0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("open 407 Communication port NG \n");
+		#endif
+		return -1;
+	}
+	ioctl (fd, TCGETS, &tios);
+	tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
+	tios.c_lflag = 0;
+	tios.c_iflag = 0;
+	tios.c_oflag = 0;
+	tios.c_cc[VMIN]=0;
+	tios.c_cc[VTIME]=(unsigned char)1;
+	tios.c_lflag=0;
+	tcflush(fd, TCIFLUSH);
+	ioctl (fd, TCSETS, &tios);
+
+	return fd;
+}
+
+//=================================
+// Common routine
+//=================================
+int InitWatchDog()
+{
+	int fd;
+	system("/usr/bin/fuser -k /dev/watchdog");
+	sleep(1);
+	system("echo V > /dev/watchdog");
+	sleep(1);
+	fd=open("/dev/watchdog", O_RDWR);
+
+	if(fd<=0)
+	{
+		DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
+	}
+	return fd;
+}
+
+int StoreLogMsg_1(const char *fmt, ...)
+{
+	char Buf[4096+256];
+	char buffer[4096];
+	time_t CurrentTime;
+	struct tm *tm;
+	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);
+	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
+			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
+			buffer,
+			tm->tm_year+1900,tm->tm_mon+1);
+	system(Buf);
+
+	return rc;
+}
+
+unsigned long GetTimeoutValue(struct timeval _sour_time)
+{
+	struct timeval _end_time;
+	gettimeofday(&_end_time, NULL);
+
+	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
+}
+
+int mystrcmp(unsigned char *p1, unsigned char *p2)
+{
+    while(*p1==*p2)
+    {
+        if(*p1=='\0' || *p2=='\0')
+            break;
+        p1++;
+        p2++;
+    }
+    if(*p1=='\0' && *p2=='\0')
+        return(PASS);
+    else
+        return(FAIL);
+}
+
+int DiffTimeb(struct timeb ST, struct timeb ET)
+{
+	//return milli-second
+	unsigned int StartTime,StopTime;
+
+	StartTime=(unsigned int)ST.time;
+	StopTime=(unsigned int)ET.time;
+	//return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
+	return (StopTime-StartTime);
+}
+
+bool CheckTimeOut(struct timeb ST)
+{
+	struct timeb ET;
+	unsigned int StartTime, StopTime;
+
+	ftime(&ET);
+	StartTime = (unsigned int) ST.time;
+	StopTime = (unsigned int) ET.time;
+	return (StopTime > StartTime)? YES : NO;
+}
+
+void setChargerMode(byte gun_index, byte mode)
+{
+	chargingInfo[gun_index]->SystemStatus = mode;
+}
+
+void PRINTF_FUNC(char *string, ...)
+{
+	if (DEBUG)
+	{
+		va_list args;
+		char buffer[4096];
+
+		va_start(args, string);
+		vsnprintf(buffer, sizeof(buffer), string, args);
+		va_end(args);
+		printf("%s \n", buffer);
+	}
+}
+//==========================================
+// Check interface status
+//==========================================
+int isInterfaceUp(const char *interface)
+{
+	int result = FAIL;
+
+	FILE *fp;
+	char cmd[256];
+	char buf[512];
+
+	strcpy(cmd, "ifconfig");
+	fp = popen(cmd, "r");
+	if(fp != NULL)
+	{
+		while(fgets(buf, sizeof(buf), fp) != NULL)
+		{
+			if(strstr(buf, interface) > 0)
+			{
+				result = PASS;
+			}
+		}
+	}
+	pclose(fp);
+
+	return result;
+}
+
+//=================================
+// Create all share memory
+//=================================
+int CreateShareMemory()
+{
+	int MeterSMId;
+
+	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey,	sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0))	== (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
+		#endif
+		return 0;
+	}
+	memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
+
+	if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
+		#endif
+		return 0;
+	}
+
+	memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
+
+	//creat ShmPsuData
+	if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
+		#endif
+		return 0;
+	}
+	memset(ShmPsuData, 0, sizeof(struct PsuData));
+
+	if(CHAdeMO_QUANTITY > 0)
+	{
+		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
+		{
+			#ifdef SystemLogMessage
+			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
+			#endif
+			return 0;
+		}
+		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+		{
+			#ifdef SystemLogMessage
+			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
+			#endif
+			return 0;
+		}
+		memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
+	}
+
+	//creat ShmCcsData
+	if(CCS_QUANTITY > 0)
+	{
+		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
+		{
+			#ifdef SystemLogMessage
+			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
+			#endif
+			return 0;
+		}
+		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+		{
+			#ifdef SystemLogMessage
+			DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
+			#endif
+			return 0;
+		}
+		memset(ShmCcsData, 0, sizeof(struct CcsData));
+	}
+
+	//creat ShmPrimaryMcuData
+	if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
+		#endif
+		return 0;
+	}
+	memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
+
+	//creat ShmFanModuleData
+	if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData),	IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
+		#endif
+		return 0;
+	}
+	memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
+
+	//creat ShmRelayModuleData
+	if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData),	IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
+		#endif
+		return 0;
+	}
+	memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
+
+	//creat ShmOCPP16Data
+	if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
+		#endif
+		return 0;
+	}
+	else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
+		#endif
+		return 0;
+	}
+	// memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
+	return 1;
+}
+
+//=================================
+// LCM Page
+//=================================
+void ChangeLcmByIndex(byte page_index)
+{
+	if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
+			page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
+	{
+		ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
+	}
+}
+
+//======================================================
+// Peripheral initial
+//======================================================
+void InitGPIO()
+{
+	/*****************0~3, 4 bank, bank x 32+ num*********************/
+	/***************************************************************/
+	/*************** GPIO 0 ***************************************/
+	/***************************************************************/
+	/* GPMC_AD8			=> 	GPIO0_22 *//*ID BD1_1*/
+	system("echo 22 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio22/direction");
+	/* GPMC_AD9			=>	GPIO0_23 *//*ID BD1_2*/
+	system("echo 23 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio23/direction");
+	/* GPMC_AD10		=>	GPIO0_26 *//*IO BD1_1*/
+	system("echo 26 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio26/direction");
+	system("echo 1 > /sys/class/gpio/gpio26/value");
+	/* GPMC_AD11		=>	GPIO0_27 *//*IO BD1_2*/
+	system("echo 27 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio27/direction");
+	/* RMII1_REF_CLK		=>	GPIO0_29 *//*USB 0 OCP detection*/
+	system("echo 29 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio29/direction");
+	/*XDMA_EVENT_INTR0	=>	GPIO0_19 *//*AM_RFID_RST*/
+	system("echo 19 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio19/direction");
+	system("echo 1 > /sys/class/gpio/gpio19/value");
+	/*XDMA_EVENT_INTR1	=>	GPIO0_20 *//*AM_RFID_ICC*/
+	system("echo 20 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio20/direction");
+	/***************************************************************/
+	/*************** GPIO 1 ***************************************/
+	/***************************************************************/
+	/* GPMC_AD12	=> 	GPIO1_12 *//*ID BD2_1*/
+	system("echo 44 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio44/direction");
+	/* GPMC_AD13	=>	GPIO1_13 *//*ID BD2_2*/
+	system("echo 45 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio45/direction");
+	/* GPMC_AD14	=>	GPIO1_14 *//*IO BD2_1*/
+	system("echo 46 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio46/direction");
+	system("echo 0 > /sys/class/gpio/gpio46/value");
+	/* GPMC_AD15	=>	GPIO1_15 *//*IO BD2_2*/
+	system("echo 47 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio47/direction");
+	/***************************************************************/
+	/*************** GPIO 2 ***************************************/
+	/***************************************************************/
+	/*LCD_AC_BIAS_EN	=>	GPIO2_25*//*RS-485 for module DE control*/
+	system("echo 89 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio89/direction");
+	system("echo 1 > /sys/class/gpio/gpio89/value");
+	/*LCD_HSYNC		=>	GPIO2_23*//*RS-485 for module RE control*/
+	system("echo 87 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio87/direction");
+	system("echo 0 > /sys/class/gpio/gpio87/value");
+	/*LCD_PCLK		=>	GPIO2_24*//*CCS communication board 1 proximity*/
+	system("echo 88 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio88/direction");
+	/*LCD_VSYNC		=>	GPIO2_22*//*CCS communication board 2 proximity*/
+	system("echo 86 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio86/direction");
+	/***************************************************************/
+	/*************** GPIO 3 ***************************************/
+	/***************************************************************/
+	/*MCASP0_FSX		=>	GPIO3_15*//*Emergency Stop button detect*/
+	system("echo 111 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio111/direction");
+	/*MCASP0_ACLKR	=>	GPIO3_18*//*USB1 OCP detect*/
+	system("echo 114 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio114/direction");
+	/*MCASP0_AHCLKR	=>	GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
+	system("echo 113 > /sys/class/gpio/export");
+	system("echo \"in\" > /sys/class/gpio/gpio113/direction");
+	/*MCASP0_ACLKX	=>	GPIO3_14*//*Ethernet PHY reset*/
+	system("echo 110 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio110/direction");
+	system("echo 0 > /sys/class/gpio/gpio110/value");
+	/* MCASP0_FSR		=>	GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
+	system("echo 115 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio115/direction");
+	system("echo 0 > /sys/class/gpio/gpio115/value");
+	/* MCASP0_AXR0	=>	GPIO3_16 *//*CSU board function OK indicator.*/
+	system("echo 112 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio112/direction");
+	system("echo 1 > /sys/class/gpio/gpio112/value");
+	/* MCASP0_AXR1	=>	GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
+	system("echo 116 > /sys/class/gpio/export");
+	system("echo \"out\" > /sys/class/gpio/gpio116/direction");
+	system("echo 0 > /sys/class/gpio/gpio116/value");
+#ifdef SystemLogMessage
+	DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
+#endif
+}
+
+int LoadSysConfigAndInfo(struct SysConfigData *ptr)
+{
+	int fd,wrd;
+	unsigned char *buf;
+	unsigned int ChkSum,ChkSumOrg;
+
+	if((buf=malloc(MtdBlockSize))==NULL)
+	{
+		DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+		}
+		sleep(5);
+		system("reboot -f");
+		sleep(5);
+		system("reboot -f");
+	}
+	memset(buf, 0, MtdBlockSize);
+
+	//================================================
+	// Load configuration from mtdblock10
+	//================================================
+	fd = open("/dev/mtdblock10", O_RDWR);
+	if (fd < 0)
+	{
+		free(buf);
+		DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+		}
+		sleep(5);
+		system("reboot -f");
+		sleep(5);
+		system("reboot -f");
+	}
+
+	wrd=read(fd, buf, MtdBlockSize);
+	close(fd);
+	if(wrd<MtdBlockSize)
+	{
+		free(buf);
+		DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+		}
+		sleep(5);
+		system("reboot -f");
+		sleep(5);
+		system("reboot -f");
+	}
+	ChkSum=0;
+	for(wrd=0;wrd<MtdBlockSize-4;wrd++)
+	{
+		ChkSum+=buf[wrd];
+	}
+	memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
+
+	//================================================
+	// Load configuration from mtdblock11
+	//================================================
+	if(ChkSum!=ChkSumOrg)
+	{
+		DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
+		fd = open("/dev/mtdblock11", O_RDWR);
+		if (fd < 0)
+		{
+			free(buf);
+			DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
+			if(ShmStatusCodeData!=NULL)
+			{
+				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+			}
+			sleep(5);
+			system("reboot -f");
+			sleep(5);
+			system("reboot -f");
+		}
+
+		memset(buf, 0, MtdBlockSize);
+	    wrd=read(fd, buf,MtdBlockSize);
+	    close(fd);
+		if(wrd<MtdBlockSize)
+		{
+			free(buf);
+			DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
+			if(ShmStatusCodeData!=NULL)
+			{
+				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+			}
+			sleep(5);
+			system("reboot -f");
+			sleep(5);
+			system("reboot -f");
+		}
+		ChkSum=0;
+		for(wrd=0;wrd<MtdBlockSize-4;wrd++)
+		{
+			ChkSum+=buf[wrd];
+		}
+		memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
+
+		//================================================
+		// Load configuration from mtdblock12 (Factory default)
+		//================================================
+		if(ChkSum!=ChkSumOrg)
+		{
+			DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
+			fd = open("/dev/mtdblock12", O_RDWR);
+			if (fd < 0)
+			{
+				free(buf);
+				DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
+				if(ShmStatusCodeData!=NULL)
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+				}
+				sleep(5);
+				system("reboot -f");
+				sleep(5);
+				system("reboot -f");
+		    }
+		    memset(buf, 0, MtdBlockSize);
+	   		wrd=read(fd, buf,MtdBlockSize);
+	    	close(fd);
+			if(wrd<MtdBlockSize)
+			{
+				free(buf);
+				DEBUG_ERROR_MSG("read factory default  SysConfigData data NG,rebooting..\r\n");
+				if(ShmStatusCodeData!=NULL)
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+				}
+				sleep(5);
+				system("reboot -f");
+				sleep(5);
+				system("reboot -f");
+			}
+			ChkSum=0;
+			for(wrd=0;wrd<MtdBlockSize-4;wrd++)
+			{
+				ChkSum+=buf[wrd];
+			}
+			memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
+			if(ChkSum!=ChkSumOrg)
+			{
+				DEBUG_ERROR_MSG("factory default  SysConfigData checksum NG, restore factory default\r\n");
+				free(buf);
+				system("cd /root;./FactoryConfig -m");
+				system("sync");
+				sleep(5);
+				system("reboot -f");
+				sleep(5);
+				system("reboot -f");
+
+				return FAIL;
+			}
+		}
+	}
+
+	//load OK
+	memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
+	free(buf);
+	DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
+	return PASS;
+}
+
+int isReachableInternet()
+{
+	int result = FAIL;
+	FILE *fp;
+	char cmd[256];
+	char buf[512];
+	//char tmp[512];
+
+	for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
+	{
+		strcpy(cmd, "ping -c 1 -w 3 ");
+		strcat(cmd, valid_Internet[idx]);
+		fp = popen(cmd, "r");
+		if(fp != NULL)
+		{
+			while(fgets(buf, sizeof(buf), fp) != NULL)
+			{
+				if(strstr(buf, "transmitted") > 0)
+				{
+					//sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
+
+					if(strstr(buf,"100%") != NULL)
+					{
+
+					}
+					else
+					{
+						result = PASS;
+					}
+					//DEBUG_INFO("%s",buf);
+					//DEBUG_INFO("%s\n",tmp);
+				}
+			}
+		}
+		pclose(fp);
+	}
+
+	return result;
+}
+
+void InitEthernet()
+{
+	char tmpbuf[256];
+	// /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
+	system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
+	sleep(2);
+	//Init Eth0 for internet
+	memset(tmpbuf,0,256);
+	sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
+	ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
+	ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
+	system(tmpbuf);
+	memset(tmpbuf,0,256);
+	sprintf(tmpbuf,"route add default gw %s eth0 ",
+	ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
+	system(tmpbuf);
+
+    //Init Eth1 for administrator tool
+	memset(tmpbuf,0,256);
+	sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
+	ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
+	ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
+	system(tmpbuf);
+
+    //Run DHCP client if enabled
+	system("killall udhcpc");
+	system("rm -rf /etc/resolv.conf");
+	system("echo nameserver 8.8.8.8 > /etc/resolv.conf");		//Google DNS server
+	system("echo nameserver 180.76.76.76 > /etc/resolv.conf");	//Baidu DNS server
+
+	if(ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient == 0)
+		system("/sbin/udhcpc -i eth0 -s /root/dhcp_script/eth0.script > /dev/null &");
+
+	pid_t pid = fork();
+	uint8_t cnt_pingDNS_Fail;
+
+	if(pid == 0)
+	{
+		for(;;)
+		{
+			if(isReachableInternet() == PASS)
+			{
+				ShmSysConfigAndInfo->SysInfo.InternetConn = YES;
+				ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= NO;
+				cnt_pingDNS_Fail = 0;
+			}
+			else
+			{
+				if(++cnt_pingDNS_Fail > 3)
+				{
+					ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
+					ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= YES;
+				}
+			}
+
+			sleep(5);
+		}
+	}
+
+	#ifdef SystemLogMessage
+	DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK");
+	#endif
+}
+
+int InitialRfidPort()
+{
+	int uartO2 = open(rfidPortName, O_RDWR);
+	struct termios tios;
+
+	if (uartO2 != FAIL)
+	{
+		ioctl (uartO2, TCGETS, &tios);
+		tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
+		tios.c_lflag = 0;
+		tios.c_iflag = 0;
+		tios.c_oflag = 0;
+		tios.c_cc[VMIN] = 0;
+		tios.c_cc[VTIME] = (unsigned char) 1;
+		tios.c_lflag = 0;
+		tcflush(uartO2, TCIFLUSH);
+		ioctl(uartO2, TCSETS, &tios);
+	}
+
+	if (uartO2 < 0)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
+	}
+
+	return uartO2;
+}
+
+void InitialShareMemoryInfo()
+{
+	FILE *fp;
+	char cmd[512];
+	char buf[512];
+
+	sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.01.04.20.01.00");
+
+	sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
+	sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
+	sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
+
+	ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
+	ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
+	ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
+	ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
+	ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
+	ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
+	ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
+	ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
+	ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
+	ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
+
+	sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
+	memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
+
+	sprintf(cmd, "/bin/uname -r");
+	fp = popen(cmd, "r");
+	if(fp == NULL)
+		sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
+	else
+	{
+		while(fgets(buf, sizeof(buf), fp) != NULL)
+		{
+			strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
+		}
+	}
+
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.01.44.0030");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
+
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
+	sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
+	ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
+	ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
+	ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
+	ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
+	ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
+	ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
+	ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
+
+	strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
+
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
+
+	memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
+	memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
+	ShmPrimaryMcuData->SelfTest_Comp = NO;
+	ShmRelayModuleData->SelfTest_Comp = NO;
+	ShmFanModuleData->SelfTest_Comp = NO;
+	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
+	ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
+
+	ShmFanModuleData->TestFanSpeed = 0;
+
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
+}
+
+int Initialization()
+{
+	// 初始化卡號驗證的 Flag
+	ClearAuthorizedFlag();
+	// 初始化插槍驗證的 Flag
+	ClearDetectPluginFlag();
+
+	// UART 2 for Rfid
+	rfidFd = InitialRfidPort();
+
+	int pinOut[2] = { 115, 116 };
+	for (byte count = 0; count < _gunCount; count++)
+	{
+		if (chargingInfo[count]->Type == _Type_Chademo)
+		{
+			gpio_set_value(pinOut[count], 0x01);
+			ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
+		}
+		else if (chargingInfo[count]->Type == _Type_CCS_2)
+		{
+			if (ShmCcsData->CommProtocol == 0x01)
+			{
+				if (_gunCount == 1)
+					gpio_set_value(pinOut[1], 0x00);
+				else
+					gpio_set_value(pinOut[count], 0x00);
+				ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
+			}
+		}
+
+
+		strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
+	}
+
+	PRINTF_FUNC("Initialization OK \n");
+	return PASS;
+}
+
+bool InitialSystemDefaultConfig()
+{
+	bool result = true;
+
+	LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
+	InitGPIO();
+	//InitEthernet();
+	if (DEBUG == NO)
+	{
+		wtdFd = InitWatchDog();
+
+		if (wtdFd < 0)
+			result = false;
+	}
+
+	system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
+	sleep(3);
+	system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
+	sleep(1);
+	system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
+
+	return result;
+}
+
+void DisplaySelfTestFailReason()
+{
+	// RB、FB、407、EV 小板中有些板子無回應
+	if (ShmRelayModuleData->SelfTest_Comp == NO)
+	{ ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; }
+	if (ShmFanModuleData->SelfTest_Comp == NO)
+	{ ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; }
+	if (ShmPrimaryMcuData->SelfTest_Comp == NO)
+	{ ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; }
+	for (byte index = 0; index < _gunCount; index++)
+	{
+		if (chargingInfo[index]->Type == _Type_Chademo)
+		{
+			if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
+			{ ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; }
+		}
+		else if (chargingInfo[index]->Type == _Type_CCS_2)
+		{
+			if (ShmCcsData->CommProtocol == 0x01)
+			{
+				if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
+				{ ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; }
+			}
+		}
+	}
+
+	if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
+	{
+		// AC Contact 未搭上
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
+	}
+	else
+	{
+		// PSU 通訊問題
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
+	}
+}
+
+void SelfTestRun()
+{
+	bool evInitFlag = false;
+
+	StartSystemTimeoutDet(Timeout_SelftestChk);
+	ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
+	while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
+	{
+		ChkPrimaryStatus();
+		if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
+		{
+			ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
+			return;
+		}
+
+		if (_gunCount > 0)
+		{
+			if (ShmPsuData->Work_Step == _NO_WORKING ||
+					ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
+			{
+				ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
+				return;
+			}
+
+			switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
+			{
+				case _STEST_VERSION:
+				{
+					if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
+							ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
+					{
+						//PRINTF_FUNC("RB pass \n");
+						ShmRelayModuleData->SelfTest_Comp = YES;
+					}
+
+					if  (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
+							ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
+					{
+						//PRINTF_FUNC("Fan pass \n");
+						ShmFanModuleData->SelfTest_Comp = YES;
+					}
+
+					if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
+							ShmPrimaryMcuData->version[0] != '\0')
+					{
+						//PRINTF_FUNC("407 pass \n");
+						ShmPrimaryMcuData->SelfTest_Comp = YES;
+					}
+					// EV 小板
+					if (!evInitFlag)
+					{
+						evInitFlag = YES;
+						for (byte index = 0; index < _gunCount; index++)
+						{
+							if (chargingInfo[index]->Type == _Type_Chademo)
+							{
+								if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
+										ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
+								{
+									//PRINTF_FUNC("chademo pass \n");
+									ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
+								}
+								else
+								{
+									//PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
+									evInitFlag = NO;
+								}
+							}
+							else if (chargingInfo[index]->Type == _Type_CCS_2)
+							{
+								if (ShmCcsData->CommProtocol == 0x01)
+								{
+									if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
+										ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
+									{
+										//PRINTF_FUNC("ccs fw =  %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
+										ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
+									}
+									else
+									{
+										//PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
+										evInitFlag = NO;
+									}
+								}
+							}
+						}
+					}
+
+					if (ShmFanModuleData->SelfTest_Comp &&
+							ShmRelayModuleData->SelfTest_Comp &&
+							ShmPrimaryMcuData->SelfTest_Comp &&
+							evInitFlag)
+					{
+						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
+					}
+				}
+					break;
+				case _STEST_AC_CONTACTOR:
+				{
+					//ShmPsuData->Work_Step = _TEST_COMPLETE;
+					// 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
+					if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
+					{
+						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
+						PRINTF_FUNC("Communication board pass. \n");
+					}
+				}
+					break;
+				case _STEST_PSU_DETECT:
+				{
+					// 此測試主要測試 PSU 對應是否為正確的火線上電壓
+					// 如果沒有 PSU 模組請 bypass
+					if (ShmPsuData->Work_Step == _TEST_POWER_STEP || ShmPsuData->Work_Step == _TEST_COMPLETE)
+					{
+						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
+					}
+				}
+					break;
+				case _STEST_PSU_CAP:
+				{
+					// 此測試是要確認當前總輸出能力
+					// 如果沒有 PSU 模組請 bypass
+					if (ShmPsuData->Work_Step == _TEST_COMPLETE)
+					{
+						sleep(1);
+						ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
+						ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
+					}
+				}
+					break;
+			}
+		}
+		else
+			break;
+
+		usleep(100000);
+	}
+}
+
+int SpawnTask()
+{
+	system("/root/Module_EventLogging &");
+	system("/root/Module_PrimaryComm &");
+	system("/root/Module_EvComm &");
+	system("/root/Module_LcmControl &");
+	system("/root/Module_InternalComm &");
+	system("/root/Module_PsuComm &");
+	system("/root/OcppBackend &");
+
+	if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
+	{
+		system("/root/Module_4g &");
+	}
+	else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
+	{
+		system("/root/Module_Wifi &");
+	}
+
+	// 加入參數
+//	char str[64];
+//	memset(str, '\0', sizeof(65));
+//	sprintf(str, "/root/Module_EvComm %x &", (CHAdeMO_QUANTITY + CCS_QUANTITY));
+//	PRINTF_FUNC("%s \n", str);
+//	system(str);
+
+	return PASS;
+}
+
+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=0; i<MtdBlockSize-4; i++)
+			Chk+=*(ptr+i);
+
+		memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
+		fd = open("/dev/mtdblock10", O_RDWR);
+		if (fd > 0)
+		{
+			wrd=write(fd, BufTmp, MtdBlockSize);
+			close(fd);
+			if(wrd >= MtdBlockSize)
+			{
+				fd = open("/dev/mtdblock11", O_RDWR);
+				if (fd > 0)
+				{
+					wrd=write(fd, BufTmp, MtdBlockSize);
+	    			close(fd);
+					if(wrd < MtdBlockSize)
+					{
+						DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
+					   	result = FAIL;
+					}
+				}
+				else
+				{
+					DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
+					result = FAIL;
+				}
+			}
+			else
+			{
+				DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
+			    result = FAIL;
+			}
+
+		}
+		else
+		{
+			DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
+			result = FAIL;
+		}
+	}
+	else
+	{
+		DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
+	    result = FAIL;
+	}
+
+	if(BufTmp != NULL)
+		free(BufTmp);
+
+	return result;
+}
+
+//===============================================
+// Common Detect Chk - Stop Charging ?
+//===============================================
+bool isEvBoardStopChargeFlag(byte gunIndex)
+{
+	return chargingInfo[gunIndex]->StopChargeFlag;
+}
+
+//===============================================
+// 掃描插槍狀況
+//===============================================
+void ClearDetectPluginFlag()
+{
+	ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
+}
+
+void DetectPluginStart()
+{
+	ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
+}
+
+bool isDetectPlugin()
+{
+	if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
+		return YES;
+
+	return NO;
+}
+
+//===============================================
+// Common Detect Chk - Chademo
+//===============================================
+bool isEvGunLocked_chademo(byte gunIndex)
+{
+	return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
+}
+
+bool isEvContactorWelding_chademo(byte gunIndex)
+{
+	return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
+}
+
+bool isEvStopReq_chademo(byte gunIndex)
+{
+	return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
+}
+
+bool isEvStopCharging_chademo(byte gunIndex)
+{
+	if (isEvGunLocked_chademo(gunIndex) == NO)
+	{
+		// 無鎖槍 = 停止
+		PRINTF_FUNC("gun locked none. \n");
+		return YES;
+	}
+
+	return NO;
+}
+
+byte isPrechargeStatus_chademo(byte gunIndex)
+{
+	byte result = 0x00;
+
+	result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
+
+	return result;
+}
+//===============================================
+// Common Detect Chk - CCS
+//===============================================
+bool isEvGunLocked_ccs(byte gunIndex)
+{
+	return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
+}
+
+byte isPrechargeStatus_ccs(byte gunIndex)
+{
+	byte result = 0x00;
+
+	if (ShmCcsData->CommProtocol == 0x01)
+	{
+		result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
+	}
+
+	return result;
+}
+
+bool isEvStopCharging_ccs(byte gunIndex)
+{
+	if (isEvGunLocked_ccs(gunIndex) == NO)
+	{
+		// 無鎖槍 = 停止
+		PRINTF_FUNC("gun locked none. \n");
+		return YES;
+	}
+
+	return NO;
+}
+
+//===============================================
+// Callback
+//===============================================
+void DisplayChargingInfo()
+{
+	for (byte i = _gunCount - 1; i >= 0; i--)
+	{
+		if (chargingInfo[i]->SystemStatus != S_IDLE)
+		{
+			ShmSysConfigAndInfo->SysInfo.CurGunSelected = i;
+			break;
+		}
+	}
+	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
+}
+
+void _AutoReturnTimeout()
+{
+	if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
+	{
+		ClearDetectPluginFlag();
+	}
+	else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
+	{
+		DetectPluginStart();
+	}
+
+	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
+//
+//	for (byte i = 0; i < CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY; i++)
+//	{
+//		if (chargingInfo[i]->SystemStatus > S_IDLE)
+//		{
+//			switch(chargingInfo[i]->SystemStatus)
+//			{
+//				case S_PREPARNING:
+//				case S_PREPARING_FOR_EV:
+//				case S_PREPARING_FOR_EVSE:
+//				{
+//					ChangeLcmByIndex(i, _LCM_PRE_CHARGE);
+//				}
+//					break;
+//				case S_CHARGING:
+//				{
+//					ChangeLcmByIndex(i, _LCM_CHARGING);
+//				}
+//					break;
+//				case S_TERMINATING:
+//				{
+//					ChangeLcmByIndex(i, _LCM_COMPLETE);
+//				}
+//					break;
+//			}
+//			return;
+//		}
+//	}
+
+//	if (!IsAuthorizingMode())
+//		ChangeLcmByIndex(255, _LCM_IDLE);
+}
+
+void _SelfTestTimeout()
+{
+	if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
+	{
+		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
+		{
+			setChargerMode(gun_index, MODE_ALARM);
+		}
+	}
+	ShmPsuData->Work_Step = _NO_WORKING;
+	ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
+	PRINTF_FUNC("Self test timeout. \n");
+}
+
+void _AuthorizedTimeout()
+{
+	if(IsAuthorizingMode())
+	{
+		PRINTF_FUNC("_AuthorizedTimeout \n");
+		isCardScan = false;
+		ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
+		//ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
+		strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+		ClearAuthorizedFlag();
+	}
+}
+
+void _DetectPlugInTimeout()
+{
+	strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+	PRINTF_FUNC("_DetectPlugInTimeout \n");
+	ClearDetectPluginFlag();
+	ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
+}
+
+void _DetectEvChargingEnableTimeout(byte gunIndex)
+{
+	if (chargingInfo[gunIndex]->Type == _Type_Chademo)
+	{
+		if(!isEvGunLocked_chademo(gunIndex))
+		{
+			PRINTF_FUNC("_DetectEvChargingEnableTimeout (chademo) \n");
+			ChargingTerminalProcess(gunIndex);
+			_AutoReturnTimeout();
+		}
+	}
+	else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
+	{
+		if(!isEvGunLocked_ccs(gunIndex))
+		{
+			PRINTF_FUNC("_DetectEvChargingEnableTimeout (ccs) \n");
+			ChargingTerminalProcess(gunIndex);
+			_AutoReturnTimeout();
+		}
+	}
+}
+
+void _DetectEvseChargingEnableTimeout(byte gunIndex)
+{
+	PRINTF_FUNC("_DetectEvseChargingEnableTimeout (GFD timeout) \n");
+	//if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
+	{
+		setChargerMode(gunIndex, MODE_IDLE);
+		_AutoReturnTimeout();
+	}
+}
+
+void _PrepareTimeout(byte gunIndex)
+{
+	PRINTF_FUNC("_PrechargeTimeout \n");
+	setChargerMode(gunIndex, MODE_IDLE);
+	ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
+	_AutoReturnTimeout();
+}
+
+void _CompleteTimeout(byte gunIndex)
+{
+	PRINTF_FUNC("_CompleteTimeout ====> %d \n", gunIndex);
+	setChargerMode(gunIndex, MODE_IDLE);
+}
+
+void _CcsPrechargeTimeout(byte gunIndex)
+{
+	PRINTF_FUNC("_CcsPrechargeTimeout \n");
+	setChargerMode(gunIndex, MODE_IDLE);
+}
+
+//===============================================
+// 取得卡號與卡號驗證
+//===============================================
+bool canStartCharging()
+{
+	char buf2[16] = "";
+	memset(buf2, 0, ARRAY_SIZE(buf2));
+
+	for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
+	{
+		sprintf(buf2 + (index - 1) * 2, "%02X",	ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
+	}
+	sprintf(buf2, "%s",	ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
+
+	// 因為無法得知實際的長度,所以只能用搜尋的方式
+	if(strcmp(buf2, "Accepted") == EQUAL)
+		return true;
+	else
+	{
+
+	}
+
+	return false;
+}
+
+void AuthorizingStart()
+{
+	ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
+	ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
+}
+
+void ClearAuthorizedFlag()
+{
+	ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
+	ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
+	ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
+}
+
+bool isAuthorizedComplete()
+{
+	if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
+		return false;
+
+	return true;
+}
+
+bool IsAuthorizingMode()
+{
+	if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
+		return false;
+
+	return true;
+}
+
+//===============================================
+// 紀錄 Alarm Code
+//===============================================
+void RecordAlarmCode(char *code)
+{
+	if (strcmp(code, "012234") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
+	if (strcmp(code, "012235") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
+}
+
+void ReleaseAlarmCode(byte gunIndex)
+{
+	if (chargingInfo[gunIndex]->Type == _Type_Chademo)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
+	}
+	else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
+	}
+}
+
+//===============================================
+// EmergencyStop and Charging Stop
+//===============================================
+void ChargingTerminalProcess(byte gunIndex)
+{
+	setChargerMode(gunIndex, MODE_TERMINATING);
+}
+
+void StopChargingProcessByString(byte level, byte gun_index, char *string)
+{
+	if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
+	{
+		ShmSysConfigAndInfo->SysWarningInfo.Level = level;
+	}
+}
+
+void ReleaseChargingProcessByString(byte level, char *code)
+{
+	if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
+		ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
+}
+
+// 一般錯誤停止充電處理函式
+void BoardErrOccurByString(byte index, char *code)
+{
+	byte level = 1;
+	if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
+			(chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
+	{
+		ChargingTerminalProcess(index);
+	}
+
+	StopChargingProcessByString(level, index, code);
+}
+
+// 急停狀況的停止充電處理函式
+void EmcOccureByString(char *code)
+{
+	byte level = 2;
+	// 嚴重的急停有四種 : EMC 按鈕、Mainbreak、Dooropen、SPD Trip
+	// 其錯誤等級為 2
+	if (strncmp(code, "012251", 6) == 0 || strncmp(code, "012252", 6) == 0 ||
+			strncmp(code, "012237", 6) == 0 || strncmp(code, "012238", 6) == 0)
+	{
+		for (byte gun = 0; gun < _gunCount; gun++)
+		{
+			if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
+					(chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
+			{
+				ChargingTerminalProcess(gun);
+			}
+
+			StopChargingProcessByString(level, gun, code);
+		}
+	}
+}
+
+void ReleaseBoardErrOccurByString(byte index, char *code)
+{
+	bool isTrigger = false;
+	byte level = 1;
+
+	if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
+	{
+		isTrigger = true;
+		ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
+	}
+
+	if (isTrigger)
+	{
+		ReleaseChargingProcessByString(level, code);
+	}
+}
+
+void ReleaseEmsOccureByString(byte index, char *code)
+{
+	bool isTrigger = false;
+	byte level = 2;
+
+	if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
+	{
+		isTrigger = true;
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
+	}
+	else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
+	{
+		isTrigger = true;
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
+	}
+	else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
+	{
+		isTrigger = true;
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
+	}
+	else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
+	{
+		isTrigger = true;
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
+	}
+
+	if (isTrigger)
+	{
+		ReleaseChargingProcessByString(level, code);
+	}
+}
+
+//===============================================
+// 確認硬體 (按鈕) 狀態
+//===============================================
+bool leftBtnPush = false;
+bool rightBtnPush = false;
+
+void ChkPrimaryStatus()
+{
+	if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
+		EmcOccureByString("012251");
+	}
+	else
+		ReleaseEmsOccureByString(0, "012251");
+
+	if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
+		EmcOccureByString("012238");
+	}
+	else
+		ReleaseEmsOccureByString(0, "012238");
+
+	if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
+		EmcOccureByString("012237");
+	}
+	else
+		ReleaseEmsOccureByString(0, "012237");
+
+	if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
+		EmcOccureByString("012252");
+	}
+	else
+		ReleaseEmsOccureByString(0, "012252");
+
+	if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
+	{
+		if(!leftBtnPush)
+		{
+			leftBtnPush = true;
+			PRINTF_FUNC("left btn down............................... \n");
+			if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < _gunCount)
+				ShmSysConfigAndInfo->SysInfo.CurGunSelected += 1;
+			else
+				ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
+		}
+		// 左邊的選槍按鈕,只有在雙槍都在充電時候才有用 : 30KW 以下該按鈕無作用
+	}
+	else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
+	{
+		if(leftBtnPush)
+		{
+			leftBtnPush = false;
+			PRINTF_FUNC("left btn up............................... \n");
+		}
+	}
+
+	if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
+	{
+		if(!rightBtnPush)
+		{
+			rightBtnPush = true;
+			PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
+			if (_gunCount > 1)
+			{
+				switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
+				{
+					case S_IDLE:
+					{
+						//ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
+					}
+					break;
+					case S_PREPARNING:
+					case S_PREPARING_FOR_EV:
+					case S_PREPARING_FOR_EVSE:
+					case S_CCS_PRECHARGE_ST0:
+					case S_CCS_PRECHARGE_ST1:
+					case S_CHARGING:
+					{
+						// 停止充電
+						ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
+					}
+						break;
+					case S_COMPLETE:
+					{
+						// 回 IDLE
+						PRINTF_FUNC("right btn down.................S_COMPLETE \n");
+						chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
+					}
+						break;
+				}
+			}
+		}
+
+		// 右邊的按鈕,只作用在當前頁面,當前頁面如果在可以回 Home 與 停止充電的狀態為可用
+//		switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
+//		{
+//			case S_IDLE: {}break;
+//			case S_AUTHORIZING: {}break;
+//			case S_PREPARING_FOR_EV: {}break;
+//			case S_PREPARING_FOR_EVSE: {}break;
+//			case S_CCS_PRECHARGE_ST0: {}break;
+//			case S_CCS_PRECHARGE_ST1: {}break;
+//			case S_CHARGING:
+//			{
+//				// 停止充電
+//				PRINTF_FUNC("Stop --------------------------------------------------- \n");
+//				//ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
+//			}
+//				break;
+//			case S_COMPLETE:
+//			{
+//				//setChargerMode(ShmSysConfigAndInfo->SysInfo.CurGunSelected, MODE_IDLE);
+//			}
+//				break;
+//		}
+	}
+	else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
+	{
+		if(rightBtnPush)
+		{
+			rightBtnPush = false;
+			PRINTF_FUNC("right btn up............................... \n");
+		}
+	}
+}
+
+//===============================================
+// 確認各小板偵測的錯誤狀況
+//===============================================
+void CheckErrorOccurStatus(byte index)
+{
+	// 小板
+	if (chargingInfo[index]->Type == _Type_Chademo)
+	{
+		if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
+			BoardErrOccurByString(index, "011012");
+		else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
+			BoardErrOccurByString(index, "012234");
+	}
+	else if (chargingInfo[index]->Type == _Type_CCS_2)
+	{
+		if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
+			BoardErrOccurByString(index, "011014");
+		else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
+			BoardErrOccurByString(index, "012235");
+	}
+}
+
+//===============================================
+// 確認 GPIO 狀態
+//===============================================
+void gpio_set_value(unsigned int gpio, unsigned int value)
+{
+	int fd;
+	char buf[MAX_BUF];
+
+	snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
+	fd = open(buf, O_WRONLY);
+	if (fd < 0)
+	{
+	    perror("gpio/set-value");
+	    return;
+	}
+
+	if (value)
+		write(fd, "1", 2);
+	else
+	    write(fd, "0", 2);
+
+	close(fd);
+}
+
+int gpio_get_value(unsigned int gpio, unsigned int *value)
+{
+    int fd;
+    char buf[MAX_BUF];
+    char ch;
+
+    snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
+
+    fd = open(buf, O_RDONLY);
+    if (fd < 0) {
+        perror("gpio/get-value");
+        return fd;
+    }
+
+    read(fd, &ch, 1);
+
+    if (ch != '0') {
+        *value = 1;
+    } else {
+        *value = 0;
+    }
+
+    close(fd);
+    return 0;
+}
+
+void CheckGunTypeFromHw()
+{
+	int pinIn[4] = { 22, 23, 44, 45 };
+	unsigned int gpioValue = 0;
+
+	for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
+		gpio_get_value(pinIn[i], &gpioValue);
+		{
+			switch (pinIn[i])
+			{
+			case 22:
+				bd1_1_status = gpioValue;
+				break;
+			case 23:
+				bd1_2_status = gpioValue;
+				break;
+			case 44:
+				bd0_1_status = gpioValue;
+				break;
+			case 45:
+				bd0_2_status = gpioValue;
+				break;
+			}
+		}
+	}
+}
+
+void CheckGpioInStatus()
+{
+	int pinIn[2] = { 27, 47 };
+	unsigned int gpioValue = 0;
+
+	for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
+	{
+		gpio_get_value(pinIn[i], &gpioValue);
+		if (gpioValue == 0x01)
+		{
+			switch(pinIn[i])
+			{
+				// 小板緊急停止
+				case 47:
+				{
+					for(int i = 0; i < _gunCount; i++)
+					{
+						if (chargingInfo[i]->slotsIndex == 1)
+						{
+							if (chargingInfo[i]->Type == _Type_Chademo)
+								BoardErrOccurByString(i, "023730");
+							else if (chargingInfo[i]->Type == _Type_CCS_2)
+								BoardErrOccurByString(i, "013627");
+							break;
+						}
+					}
+				}
+					break;
+				case 27:
+				{
+					for(int i = 0; i < _gunCount; i++)
+					{
+						if (chargingInfo[i]->slotsIndex == 3)
+						{
+							if (chargingInfo[i]->Type == _Type_Chademo)
+								BoardErrOccurByString(i, "023730");
+							else if (chargingInfo[i]->Type == _Type_CCS_2)
+								BoardErrOccurByString(i, "013627");
+							break;
+						}
+					}
+				}
+					break;
+			}
+		}
+		else
+		{
+			switch (pinIn[i])
+			{
+				// 小板解除緊急停止
+				case 47:
+				{
+					for(int i = 0; i < _gunCount; i++)
+					{
+						if (chargingInfo[i]->slotsIndex == 1)
+						{
+							if (chargingInfo[i]->Type == _Type_Chademo)
+								ReleaseEmsOccureByString(i, "023730");
+							else if (chargingInfo[i]->Type == _Type_CCS_2)
+								ReleaseEmsOccureByString(i, "013627");
+							break;
+						}
+					}
+				}
+					break;
+				case 27:
+				{
+					for (int i = 0; i < _gunCount; i++)
+					{
+						if (chargingInfo[i]->slotsIndex == 3)
+						{
+							if (chargingInfo[i]->Type == _Type_Chademo)
+								ReleaseEmsOccureByString(i, "023730");
+							else if (chargingInfo[i]->Type == _Type_CCS_2)
+								ReleaseEmsOccureByString(i, "013627");
+							break;
+						}
+					}
+				}
+				break;
+			}
+		}
+	}
+}
+
+//===============================================
+// Main process
+//===============================================
+// 檢查 Byte 中某個 Bit 的值
+// _byte : 欲改變的 byte
+// _bit : 該 byte 的第幾個 bit
+unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
+{
+	return ( _byte & mask_table[_bit] ) != 0x00;
+}
+
+// 設定 Byte 中某個 Bit的值
+// _byte : 欲改變的 byte
+// _bit : 該 byte 的第幾個 bit
+// value : 修改的值為 0 or 1
+void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
+{
+	if(value == 1)
+		*_byte |= (1 << _bit);
+	else if (value == 0)
+		*_byte ^= (1 << _bit);
+}
+
+void UserScanFunction()
+{
+	bool idleReq = false;
+	unsigned char stopReq = 255;
+
+	// 當前非驗證的狀態
+	if(!IsAuthorizingMode())
+	{
+		// 先判斷現在是否可以提供刷卡
+		// 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
+		// 2. 停止充電
+		for (byte i = 0; i < _gunCount; i++)
+		{
+			// 二擇一
+			if ((chargingInfo[i]->SystemStatus >= S_AUTHORIZING && chargingInfo[i]->SystemStatus <= S_PREPARNING) ||
+				(chargingInfo[i]->SystemStatus >= S_TERMINATING && chargingInfo[i]->SystemStatus < S_COMPLETE))
+			{
+				strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+				return;
+			}
+			if (chargingInfo[i]->SystemStatus == S_CHARGING)
+			{
+				stopReq = i;
+			}
+			else if (chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable == YES)
+			{
+				idleReq = true;
+			}
+		}
+
+		if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
+		{
+			if (stopReq < _gunCount &&
+					chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING)
+			{
+				char value[32];
+
+				PRINTF_FUNC("stop charging \n");
+				PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
+						chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
+				memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
+						ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
+				if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
+				{
+					ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
+				}
+				strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+			}
+			else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_COMPLETE)
+			{
+				strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+			}
+			else if (idleReq)
+			{
+				if (_gunCount > 1 && stopReq != 255 && ONE_CONNECTOR_USE == YES)
+				{
+					idleReq = false;
+					strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+				}
+				else
+				{
+					PRINTF_FUNC("// LCM => Authorizing \n");
+					// LCM => Authorizing
+					ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
+					// 進入確認卡號狀態
+					AuthorizingStart();
+				}
+			}
+			else
+			{
+				strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+			}
+		}
+	}
+}
+
+unsigned char isModeChange(unsigned char gun_index)
+{
+	unsigned char result = NO;
+
+	if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
+	{
+		result = YES;
+		chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
+	}
+
+	return result;
+}
+
+void ScannerCardProcess()
+{
+	if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level == 0 &&
+			ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_OCPP)
+	{
+		isCardScan = true;
+		// 處理刷卡及驗證卡號的動作
+		UserScanFunction();
+	}
+
+	if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
+	{
+		StartSystemTimeoutDet(Timeout_Authorizing);
+
+		// 確認驗證卡號完成沒
+		if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
+		{
+			StopSystemTimeoutDet();
+			// 判斷後台回覆狀態
+			if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
+			{
+				// LCM => Authorize complete
+				ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
+			}
+			else
+			{
+				// LCM => Authorize fail
+				ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
+			}
+			ClearAuthorizedFlag();
+		}
+		else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
+		{
+			// 白名單驗證
+			for (int i = 0; i < 10; i++)
+			{
+				if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
+				{
+					if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
+					{
+						ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
+						ClearAuthorizedFlag();
+						break;
+					}
+				}
+			}
+		}
+	}
+	else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
+	{
+		StartSystemTimeoutDet(Timeout_VerifyFail);
+		isCardScan = false;
+	}
+	else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
+	{
+		StartSystemTimeoutDet(Timeout_VerifyComp);
+	}
+	else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
+	{
+		StartSystemTimeoutDet(Timeout_WaitPlug);
+	}
+	else
+		isCardScan = false;
+}
+
+void AddGunInfoByConnector(byte typeValue, byte slots)
+{
+	switch (typeValue)
+	{
+		case '0': // none
+			break;
+		case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
+			break;
+		case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
+			break;
+		case '3': // IEC 62196-2 Type 2 Plug
+			break;
+		case '4': // IEC 62196-2 Type 2 Socket
+			break;
+		case '5': // GB/T AC Plug
+			break;
+		case '6': // GB/T AC Socket
+			break;
+		case 'J': // CHAdeMO
+		{
+			if (CHAdeMO_QUANTITY > _chademoIndex)
+			{
+				chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
+
+				chargingInfo[_gunIndex]->Index = _gunIndex;
+				chargingInfo[_gunIndex]->slotsIndex = slots;
+				chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
+				chargingInfo[_gunIndex]->Type = _Type_Chademo;
+				chargingInfo[_gunIndex]->type_index = _chademoIndex;
+				chargingInfo[_gunIndex]->IsAvailable = YES;
+				_chademoIndex++;
+				_gunIndex++;
+			}
+		}
+			break;
+		case 'U': // CCS1 combo
+			break;
+		case 'E': // CCS2 combo
+		{
+			if (CCS_QUANTITY > _ccsIndex)
+			{
+				chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
+
+				chargingInfo[_gunIndex]->Index =	_gunIndex;
+				chargingInfo[_gunIndex]->slotsIndex = slots;
+				chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
+				chargingInfo[_gunIndex]->Type = _Type_CCS_2;
+				chargingInfo[_gunIndex]->type_index = _ccsIndex;
+				chargingInfo[_gunIndex]->IsAvailable = YES;
+				// 現階段預設為走 DIN70121
+				ShmCcsData->CommProtocol = 0x01;
+				_ccsIndex++;
+				_gunIndex++;
+			}
+		}
+			break;
+		case 'G': // GBT DC
+			break;
+		case 'D': // GBT DC x 2
+			break;
+	}
+}
+
+bool CheckConnectorTypeStatus()
+{
+	bool result = true;
+
+	PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
+			bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
+	if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
+	{
+		byte slots = 1;
+		for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
+		{
+			AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots);
+			slots++;
+		}
+
+		_gunCount = _gunIndex;
+		PRINTF_FUNC("_gunCount = %d \n", _gunCount);
+		if (_gunCount == 0)
+			result = false;
+
+		// 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
+		for (byte gunIndex = 0; gunIndex < _gunCount; gunIndex++)
+		{
+			if (bd0_1_status == 0 && bd0_2_status == 1)
+			{
+				// 與硬體相同 type : Chademo
+				if (chargingInfo[gunIndex]->Type == _Type_Chademo)
+				{
+					chargingInfo[gunIndex]->Evboard_id = 0x01;
+				}
+			}
+			else if (bd0_1_status == 1 && bd0_2_status == 0)
+			{
+				// 與硬體相同 type : CCS
+				if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
+				{
+					chargingInfo[gunIndex]->Evboard_id = 0x01;
+				}
+			}
+
+			if (bd1_1_status == 0 && bd1_2_status == 1)
+			{
+				// 與硬體相同 type : Chademo
+				if (chargingInfo[gunIndex]->Type == _Type_Chademo)
+				{
+					chargingInfo[gunIndex]->Evboard_id = 0x02;
+				}
+
+				if (_gunCount == 1)
+					chargingInfo[gunIndex]->Evboard_id = 0x01;
+			}
+			else if (bd1_1_status == 1 && bd1_2_status == 0)
+			{
+				// 與硬體相同 type : CCS
+				if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
+				{
+					chargingInfo[gunIndex]->Evboard_id = 0x02;
+				}
+
+				if (_gunCount == 1)
+					chargingInfo[gunIndex]->Evboard_id = 0x01;
+			}
+
+			PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
+			if (chargingInfo[gunIndex]->Evboard_id == 0x00)
+				result = false;
+		}
+	}
+	else
+	{
+		// Module Name 不正確 - 告警
+		result = false;
+	}
+
+	return result;
+}
+
+void KillTask()
+{
+	ChangeLcmByIndex(_LCM_FIX);
+	system("killall Module_EventLogging");
+	system("killall Module_PrimaryComm");
+	system("killall Module_EvComm");
+	system("killall Module_LcmControl");
+	system("killall Module_InternalComm");
+	system("killall Module_PsuComm");
+}
+
+char CheckUpdateProcess()
+{
+	DIR *d;
+	struct dirent *dir;
+	d = opendir("/mnt/");
+
+	if (d)
+	{
+		long int MaxLen=48*1024*1024, ImageLen = 0;
+		while ((dir = readdir(d)) != NULL)
+		{
+			char *new_str;
+			new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
+			new_str[0] = '\0';
+			strcat(new_str, "/mnt/");
+			strcat(new_str, dir->d_name);
+			int fd = open(new_str, O_RDONLY);
+			if (fd < 0)
+			{
+				return FAIL;
+			}
+
+			unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
+			memset(ptr, 0xFF, MaxLen);  //-48 is take out the header
+			//get the image length
+			ImageLen = read(fd, ptr, MaxLen);
+
+			if (ImageLen > 20)
+			{
+				unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
+			    PRINTF_FUNC("Typed...%x \r\n", Type);
+
+			    switch (Type)
+			    {
+			    	case 0x10000001:
+			    	case 0x10000002:
+			    	case 0x10000003:
+			    	case 0x10000004:
+			    	case 0x10000005:
+			    	{
+			    		if (Upgrade_Flash(Type, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
+			    			return PASS;
+			    		else
+			    			return FAIL;
+			    	}
+			    	break;
+			    	case 0x10000006:
+			    	case 0x1000000D:
+			    	case 0x1000000E:
+			    	{
+			    		// CSU_PRIMARY_CONTROLLER : 0x10000006
+			    		byte target = 0x00;
+
+			    		if (Type == 0x10000006)
+			    			target = UPGRADE_PRI;
+			    		else if (Type == 0x1000000D)
+			    			target = UPGRADE_RB;
+			    		else if (Type == 0x1000000E)
+			    			target = UPGRADE_FAN;
+
+			    		int fd = InitComPort(target);
+
+			    		if (Upgrade_UART(fd, Type, target, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
+			    			return PASS;
+			    		else
+			    			return FAIL;
+
+			    		close(fd);
+			    	}
+			    	break;
+			    	case 0x10000007:
+			    	case 0x10000008:
+			    	case 0x10000009:
+			    	case 0x1000000A:
+			    	case 0x1000000B:
+			    	case 0x1000000C:
+			    	{
+			    		// CHAdeMO_BOARD : 0x1000000B
+			    		bool isPass = true;
+			    		int CanFd = InitCanBus();
+
+			    		if (CanFd > 0)
+			    		{
+			    			for(byte index = 0; index < _gunCount; index++)
+			    			{
+			    				if (!isPass)
+			    					break;
+
+			    				if (Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo)
+			    				{
+			    					if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) != PASS)
+			    					{
+			    						PRINTF_FUNC("Upgrad Fail. \n");
+			    						isPass = false;
+			    					}
+			    				}
+			    				else if ((Type == 0x10000007 || Type == 0x10000008 || Type == 0x10000009 || Type == 0x1000000A) &&
+			    						chargingInfo[index]->Type == _Type_CCS_2)
+			    				{
+			    					if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) != PASS)
+			    					{
+			    						PRINTF_FUNC("Upgrad Fail. \n");
+			    						isPass = false;
+			    					}
+			    				}
+			    			}
+			    		}
+			    		else
+			    		{
+			    			PRINTF_FUNC("Upgrad FD fail. \n");
+			    			isPass = false;
+			    		}
+
+			    		return isPass;
+			    		break;
+			    	}
+			    }
+			}
+			free(new_str);
+			free(ptr);
+		}
+	}
+	free(dir);
+	closedir(d);
+	return FAIL;
+}
+
+void CreateRfidFork()
+{
+	pid_t rfidRecPid;
+
+	rfidRecPid = fork();
+	if (rfidRecPid == 0)
+	{
+		while(true)
+		{
+			// 刷卡判斷
+			RFID rfid;
+			if(getRequestCardSN(rfidFd, 0, &rfid))
+			{
+				PRINTF_FUNC("Get Card.. \n");
+				if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
+				{
+					if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
+					{
+						switch (rfid.snType)
+						{
+						case RFID_SN_TYPE_6BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X%02X%02X",
+									rfid.currentCard[0], rfid.currentCard[1],
+									rfid.currentCard[2], rfid.currentCard[3],
+									rfid.currentCard[4], rfid.currentCard[5]);
+							break;
+						case RFID_SN_TYPE_7BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X%02X%02X%02X",
+									rfid.currentCard[0], rfid.currentCard[1],
+									rfid.currentCard[2], rfid.currentCard[3],
+									rfid.currentCard[4], rfid.currentCard[5],
+									rfid.currentCard[6]);
+							break;
+						case RFID_SN_TYPE_10BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
+									rfid.currentCard[0], rfid.currentCard[1],
+									rfid.currentCard[2], rfid.currentCard[3],
+									rfid.currentCard[4], rfid.currentCard[5],
+									rfid.currentCard[6], rfid.currentCard[7],
+									rfid.currentCard[8], rfid.currentCard[9]);
+							break;
+						case RFID_SN_TYPE_4BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X",
+									rfid.currentCard[0], rfid.currentCard[1],
+									rfid.currentCard[2], rfid.currentCard[3]);
+							break;
+						}
+					}
+					else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
+					{
+						switch (rfid.snType)
+						{
+						case RFID_SN_TYPE_6BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X%02X%02X",
+									rfid.currentCard[5], rfid.currentCard[4],
+									rfid.currentCard[3], rfid.currentCard[2],
+									rfid.currentCard[1], rfid.currentCard[0]);
+							break;
+						case RFID_SN_TYPE_7BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X%02X%02X%02X",
+									rfid.currentCard[6], rfid.currentCard[5],
+									rfid.currentCard[4], rfid.currentCard[3],
+									rfid.currentCard[2], rfid.currentCard[1],
+									rfid.currentCard[0]);
+							break;
+						case RFID_SN_TYPE_10BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
+									rfid.currentCard[9], rfid.currentCard[8],
+									rfid.currentCard[7], rfid.currentCard[6],
+									rfid.currentCard[5], rfid.currentCard[4],
+									rfid.currentCard[3], rfid.currentCard[2],
+									rfid.currentCard[1], rfid.currentCard[0]);
+							break;
+						case RFID_SN_TYPE_4BYTE:
+							sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
+									"%02X%02X%02X%02X",
+									rfid.currentCard[3], rfid.currentCard[2],
+									rfid.currentCard[1], rfid.currentCard[0]);
+							break;
+						}
+					}
+
+					PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
+				}
+			}
+			sleep(1);
+		}
+	}
+}
+
+void StartSystemTimeoutDet(unsigned char flag)
+{
+	if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
+	{
+		gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
+	}
+	ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
+}
+
+void StopSystemTimeoutDet()
+{
+	gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
+	ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
+}
+
+void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
+{
+	if (gunIndex < _gunCount)
+	{
+		if (chargingInfo[gunIndex]->TimeoutFlag != flag)
+		{
+			gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
+		}
+		chargingInfo[gunIndex]->TimeoutFlag = flag;
+	}
+}
+
+void StopGunInfoTimeoutDet(unsigned char gunIndex)
+{
+	if (gunIndex < _gunCount)
+	{
+		chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
+	}
+}
+
+void CreateTimeoutFork()
+{
+	pid_t timeoutPid;
+
+	timeoutPid = fork();
+	if (timeoutPid == 0)
+	{
+		while(true)
+		{
+			// 系統
+			switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
+			{
+				case Timeout_SelftestChk:
+					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
+					{
+						_SelfTestTimeout();
+						StopSystemTimeoutDet();
+					}
+					break;
+				case Timeout_Authorizing:
+					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 50000000)
+					{
+						_AuthorizedTimeout();
+						StopSystemTimeoutDet();
+					}
+					break;
+				case Timeout_VerifyFail:
+					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
+					{
+						_AutoReturnTimeout();
+						StopSystemTimeoutDet();
+					}
+					break;
+				case Timeout_VerifyComp:
+					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
+					{
+						_AutoReturnTimeout();
+						StopSystemTimeoutDet();
+					}
+					break;
+				case Timeout_WaitPlug:
+					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
+					{
+						_DetectPlugInTimeout();
+						StopSystemTimeoutDet();
+					}
+					break;
+				case Timeout_ReturnToChargingGunDet:
+				{
+					if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 10000000)
+					{
+						DisplayChargingInfo();
+						StopSystemTimeoutDet();
+					}
+				}
+					break;
+			}
+			// 各槍
+			for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
+			{
+				switch(chargingInfo[gun_index]->TimeoutFlag)
+				{
+					case Timeout_Preparing:
+						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 40000000)
+						{
+							_PrepareTimeout(gun_index);
+							StopGunInfoTimeoutDet(gun_index);
+						}
+						break;
+					case Timeout_EvChargingDet:
+						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
+						{
+							_DetectEvChargingEnableTimeout(gun_index);
+							StopGunInfoTimeoutDet(gun_index);
+						}
+						break;
+					case Timeout_EvseChargingDet:
+						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
+						{
+							_DetectEvseChargingEnableTimeout(gun_index);
+							StopGunInfoTimeoutDet(gun_index);
+						}
+						break;
+					case Timeout_WaitforCompleteDet:
+						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
+						{
+							_CompleteTimeout(gun_index);
+							StopGunInfoTimeoutDet(gun_index);
+						}
+						break;
+					case Timeout_ForCcsPrechargeDet:
+						if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
+						{
+							_CcsPrechargeTimeout(gun_index);
+							StopGunInfoTimeoutDet(gun_index);
+						}
+						break;
+				}
+			}
+			usleep(100000);
+		}
+	}
+}
+
+void GetSystemTime()
+{
+	struct timeb csuTime;
+	struct tm *tmCSU;
+
+	ftime(&csuTime);
+	tmCSU = localtime(&csuTime.time);
+	PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
+			tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
+			tmCSU->tm_sec);
+
+//	byte date[14];
+//
+//
+//		 //sprintf(&date, "%d", );
+//
+//		 date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
+
+//	date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
+//	date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
+//	date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
+//	date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
+//
+//	date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
+//	date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
+//
+//	date[6] = (tmCSU->tm_mday) / 10 % 10;
+//	date[7] = (tmCSU->tm_mday) / 1 % 10;
+//
+//	date[8] = (tmCSU->tm_hour) / 10 % 10;
+//	date[9] = (tmCSU->tm_hour) / 1 % 10;
+//
+//	date[10] = (tmCSU->tm_min) / 10 % 10;
+//	date[11] = (tmCSU->tm_min) / 1 % 10;
+//
+//	date[12] = (tmCSU->tm_sec) / 10 % 10;
+//	date[13] = (tmCSU->tm_sec) / 1 % 10;
+
+//	PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
+//			date[4], date[5], date[6], date[7],
+//			date[8], date[9], date[10], date[11],
+//			date[12], date[13]);
+}
+
+void CheckFactoryConfigFunction()
+{
+	if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
+	{
+		system("cd /root;./FactoryConfig -m");
+		system("sync");
+		sleep(5);
+		system("reboot -f");
+		sleep(5);
+		system("reboot -f");
+	}
+}
+
+void CheckFwUpdateFunction()
+{
+	//PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
+	if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
+	{
+		DEBUG_INFO_MSG("ftp : update start. \n");
+		KillTask();
+		if (CheckUpdateProcess() == PASS)
+			DEBUG_INFO_MSG("ftp : update complete. \n");
+		else
+			DEBUG_INFO_MSG("ftp : update fail. \n");
+
+		ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
+		sleep(5);
+		system("reboot -f");
+	}
+	else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
+	{
+		ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
+	}
+	else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
+	{
+		DEBUG_INFO_MSG("Backend : update start. \n");
+		strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
+		strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
+		KillTask();
+
+		if (CheckUpdateProcess() == PASS)
+		{
+			DEBUG_INFO_MSG("Backend : update complete. \n");
+			strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
+			strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
+		}
+		else
+		{
+			DEBUG_INFO_MSG("Backend : update fail. \n");
+			strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
+			strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
+		}
+
+		ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
+		sleep(5);
+		system("reboot -f");
+	}
+	else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == EQUAL)
+	{
+		DEBUG_ERROR_MSG("Backend to download file fail. \n");
+		strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
+		strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
+		ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
+	}
+}
+
+//===============================================
+// Check reservation date is expired
+//===============================================
+int isReservationExpired(unsigned char gun_index)
+{
+	int result = NO;
+	struct tm expiredDate;
+	struct timeb expiredTime;
+
+	if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
+			"%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
+			&expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
+			&expiredDate.tm_min, &expiredDate.tm_sec) == 6)
+	{
+		expiredDate.tm_year -= 1900;
+		expiredDate.tm_mon -= 1;
+
+		expiredTime.time = mktime(&expiredDate);
+		if (!CheckTimeOut(expiredTime))
+		{
+			result = YES;
+		}
+	}
+
+	return result;
+}
+
+//===============================================
+// OCPP
+//===============================================
+void CheckOcppStatus()
+{
+	if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
+	{
+		ShmSysConfigAndInfo->SysInfo.OcppConnStatus = YES;
+		ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
+	}
+
+	if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
+	{
+		ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
+		if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
+		{
+			DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
+			sleep(3);
+			system("reboot -f");
+		}
+		else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
+		{
+			DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
+			sleep(3);
+			KillTask();
+			system("/usr/bin/run_evse_restart.sh");
+		}
+	}
+}
+
+void OcppStartTransation(byte gunIndex)
+{
+	if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
+		strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
+	else
+		strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
+
+	PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
+	ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionReq = YES;
+}
+
+void OcppStopTransation(byte gunIndex)
+{
+	if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
+		strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
+	else
+		strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
+
+	PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
+	ShmOCPP16Data->CpMsg.bits[gunIndex].StopTransactionReq = YES;
+}
+
+bool OcppRemoteStop(byte gunIndex)
+{
+	bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
+	if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
+		ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
+
+	return result;
+}
+
+void OcppRemoteStartChk()
+{
+	if(!isDetectPlugin())
+	{
+		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
+		{
+			if ((chargingInfo[gun_index]->SystemStatus == S_IDLE || chargingInfo[gun_index]->SystemStatus == S_RESERVATION)&&
+					ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES)
+			{
+				ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
+				ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = NO;
+				DetectPluginStart();
+				break;
+			}
+		}
+	}
+}
+
+void ChkOcppStatus(byte gunIndex)
+{
+	if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
+			ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
+	{
+		ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
+		if (isReservationExpired(gunIndex))
+		{
+			PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
+			DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
+			chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
+			chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
+		}
+	}
+
+	if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
+			ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
+	{
+		ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
+		if (isReservationExpired(gunIndex))
+		{
+			PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
+			DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
+			chargingInfo[gunIndex]->ReservationId = 0;
+			chargingInfo[gunIndex]->SystemStatus = S_IDLE;
+		}
+	}
+
+	if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
+	{
+		PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
+		DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
+		ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
+		if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
+			chargingInfo[gunIndex]->IsAvailable = YES;
+		else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
+			chargingInfo[gunIndex]->IsAvailable = NO;
+	}
+}
+
+bool CheckBackendChargingTimeout(byte gunIndex)
+{
+	bool result = false;
+
+	if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_OCPP)
+	{
+		if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
+		{
+			if (chargingInfo[gunIndex]->RemainChargingDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
+				result = true;
+		}
+	}
+	else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_FREE)
+	{
+		// 隨插即充電的要看 offline
+		if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
+		{
+			if (chargingInfo[gunIndex]->RemainChargingDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
+				result = true;
+		}
+	}
+
+	return result;
+}
+
+bool CheckBackendChargingEnergy(byte gunIndex)
+{
+	bool result = false;
+
+	if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_OCPP)
+	{
+		if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
+		{
+			if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
+				result = true;
+		}
+	}
+	else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_FREE)
+	{
+		// 隨插即充電的要看 offline
+		if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
+		{
+			if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
+				result = true;
+		}
+	}
+
+	return result;
+}
+
+//===============================================
+// Config process
+//===============================================
+void AddPlugInTimes(byte gunIndex)
+{
+	if (chargingInfo[gunIndex]->Type == _Type_Chademo)
+		ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
+	else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
+		ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
+	else if(chargingInfo[gunIndex]->Type == _Type_GB)
+		ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
+}
+
+void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
+{
+	char cmdBuf[32];
+	struct timeb csuTime;
+	struct tm *tmCSU;
+
+	ftime(&csuTime);
+	tmCSU = localtime(&csuTime.time);
+
+	sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
+			tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
+			tmCSU->tm_sec);
+	if (isStart)
+		strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
+	else
+		strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
+}
+
+void zipLogFiles()
+{
+	const char* logPath = "/Storage/SystemLog";
+	// 獲取目錄
+	DIR* pDir = opendir(logPath);
+	if (pDir != NULL)
+	{
+		struct timeb csuTime;
+		struct tm *tmCSU;
+
+		ftime(&csuTime);
+		tmCSU = localtime(&csuTime.time);
+//		PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
+//			tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
+//			tmCSU->tm_sec);
+
+		// Read items inside the folder
+		struct dirent* pEntry = NULL;
+		while ((pEntry = readdir(pDir)) != NULL)
+		{
+			if (strcmp(pEntry->d_name, ".") != 0 &&
+					strcmp(pEntry->d_name, "..") != 0 &&
+					strncmp(pEntry->d_name, "[", 1) == 0 &&
+					strstr(pEntry->d_name, "tar") < 0)
+			{
+				char yearC[5];
+				unsigned short year = 0;
+				char monthC[3];
+				unsigned short month = 0;
+
+				yearC[4] = '\0';
+				strncpy(yearC, pEntry->d_name + 1, 4);
+				monthC[2] = '\0';
+				strncpy(monthC, pEntry->d_name + 6, 2);
+
+				year = atoi(yearC);
+				month = atoi(monthC);
+
+				if (year != 0)
+				{
+					if (year < tmCSU->tm_year + 1900 ||
+							(year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
+					{
+						DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
+						char file[256];
+
+						memset(file, 0x00, sizeof(file));
+						strcat(file, "tar zcvf ");
+						strcat(file, logPath);
+						strncat(file, "/", 1);
+						strcat(file, pEntry->d_name);
+						strcat(file, ".tar");
+						strncat(file, " ", 1);
+						strcat(file, logPath);
+						strncat(file, "/", 1);
+						strcat(file, pEntry->d_name);
+						PRINTF_FUNC("zip = %s \n", file);
+						system(file);
+					}
+				}
+			}
+		}
+	}
+	// Close folder
+	closedir(pDir);
+}
+
+void StopProcessing()
+{
+	for (;;)
+		sleep(5);
+}
+
+int main(void)
+{
+	if(CreateShareMemory() == 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR_MSG("CreatShareMemory NG \n");
+		#endif
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		return 0;
+		sleep(5);
+		system("reboot -f");
+		sleep(5);
+		system("reboot -f");
+	}
+
+	PRINTF_FUNC("\n"); PRINTF_FUNC("Initial SystemConfig and Info.......\n");
+	if (!InitialSystemDefaultConfig())
+	{
+		DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
+		StopProcessing();
+	}
+
+	PRINTF_FUNC("CheckConnectorTypeStatus. \n");
+	CheckGunTypeFromHw();
+	PRINTF_FUNC("InitialShareMemoryInfo & Initialization. \n");
+	InitialShareMemoryInfo();
+	PRINTF_FUNC("CheckConnectorTypeStatus. \n");
+	ChangeLcmByIndex(_LCM_INIT);
+	if (!CheckConnectorTypeStatus())
+	{
+		ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
+		ChangeLcmByIndex(_LCM_FIX);
+		// Module Name 與硬體對應不正確
+		PRINTF_FUNC("Module Name & HW info none match. \n");
+		DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
+		sleep(3);
+		KillTask();
+		StopProcessing();
+	}
+	Initialization();
+	ShmSysConfigAndInfo->SysInfo.InternetConn = 1;
+	PRINTF_FUNC("Spawn all Task. \n");
+	SpawnTask();
+	PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
+	CreateTimeoutFork();
+	PRINTF_FUNC("Self test. \n");
+	SelfTestRun();
+	StopSystemTimeoutDet();
+
+	PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
+	if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
+			ShmPsuData->Work_Step == _NO_WORKING)
+	{
+		DisplaySelfTestFailReason();
+		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
+		{
+			setChargerMode(gun_index, MODE_ALARM);
+		}
+		ChangeLcmByIndex(_LCM_FIX);
+		StopProcessing();
+	}
+	else
+	{
+		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
+		{
+			setChargerMode(gun_index, MODE_IDLE);
+		}
+	}
+
+	ChangeLcmByIndex(_LCM_IDLE);
+	sleep(1);
+	//***** 須新增的偵測 *****//
+	// 1. Thernal - 控制風扇轉速
+	// 2. ouput fuse - 控制風扇轉速
+	CreateRfidFork();
+	// Main loop
+	PRINTF_FUNC("Main Loop. \n");
+	for (;;)
+	{
+		CheckOcppStatus();
+		ChkPrimaryStatus();
+		if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_IDLE &&
+				ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
+		{
+			CheckFactoryConfigFunction();
+
+			CheckFwUpdateFunction();
+		}
+
+		// OCPP 邏輯
+		OcppRemoteStartChk();
+		// 讀卡邏輯
+		ScannerCardProcess();
+		for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
+		{
+			CheckGpioInStatus();
+			CheckErrorOccurStatus(gun_index);
+			ChkOcppStatus(gun_index);
+
+			//PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
+			switch(chargingInfo[gun_index]->SystemStatus)
+			{
+				case S_IDLE:
+				case S_RESERVATION:
+				{
+					if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
+							isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
+						chargingInfo[gun_index]->RemainChargingDuration = 0;
+						chargingInfo[gun_index]->PresentChargedEnergy = 0;
+					}
+					else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
+							isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
+						ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
+					}
+
+					if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
+					{
+						if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
+						{
+							ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
+						}
+						ClearDetectPluginFlag();
+					}
+					else
+					{
+						if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
+						{
+							ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
+						}
+
+						if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
+						{
+							// 均充 -> 最大充
+						}
+						else
+						{
+							// 判斷是否有啟用檢查插槍
+							if(isDetectPlugin())
+							{
+								// 卡號驗證成功後,等待充電槍插入充電車
+								if ((ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL &&
+										ShmSysConfigAndInfo->SysInfo.OrderCharging < _gunCount))
+								{
+									if (chargingInfo[(unsigned char)ShmSysConfigAndInfo->SysInfo.OrderCharging]->ConnectorPlugIn == YES &&
+											chargingInfo[gun_index]->IsAvailable)
+									{
+										PRINTF_FUNC("-----------------1----------------- %d \n", ShmSysConfigAndInfo->SysInfo.OrderCharging);
+										ShmSysConfigAndInfo->SysInfo.CurGunSelected = ShmSysConfigAndInfo->SysInfo.OrderCharging;
+										AddPlugInTimes(ShmSysConfigAndInfo->SysInfo.OrderCharging);
+										setChargerMode(ShmSysConfigAndInfo->SysInfo.OrderCharging, MODE_REASSIGN_CHECK);
+										ClearDetectPluginFlag();
+										continue;
+									}
+								}
+								else
+								{
+									if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
+											chargingInfo[gun_index]->SystemStatus == S_IDLE)
+									{
+										PRINTF_FUNC("-----------------2----------------- \n");
+										ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
+										AddPlugInTimes(gun_index);
+										strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
+										PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
+										strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+										// 當前操作的槍號,進入 Preparing
+										setChargerMode(gun_index, MODE_REASSIGN_CHECK);
+										ClearDetectPluginFlag();
+										continue;
+									}
+								}
+
+								if (!isCardScan)
+								{
+									// LCM => Waiting for plugging
+									ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
+								}
+							}
+							else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
+							{
+								if (!isReservationExpired(gun_index))
+								{
+									chargingInfo[gun_index]->SystemStatus = S_IDLE;
+								}
+							}
+							else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_FREE &&
+									(chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
+							{
+								PRINTF_FUNC("-----------------3----------------- \n");
+								ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
+								AddPlugInTimes(gun_index);
+								setChargerMode(gun_index, MODE_REASSIGN_CHECK);
+								ClearDetectPluginFlag();
+								continue;
+							}
+							else
+							{
+								if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+									ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
+
+								if (_gunCount > 1 &&
+										ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
+										ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
+										ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
+										ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
+								{
+									for (byte count = 0; count < _gunCount; count++)
+									{
+										if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected &&
+											((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
+											(chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1)))
+										{
+											StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
+										}
+									}
+								}
+							}
+						}
+					}
+
+					ReleaseAlarmCode(gun_index);
+				}
+					break;
+				case S_REASSIGN_CHECK:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
+						ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
+						if (ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL)
+							ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
+						StopSystemTimeoutDet();
+					}
+
+					bool isRessign = false;
+					if (_gunCount > 1)
+					{
+						for (byte index = 0; index < _gunCount; index++)
+						{
+							// 有其他槍已經分配好 psu 模塊
+							if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
+									chargingInfo[index]->SystemStatus >= S_PREPARNING)
+							{
+								PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
+								ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE;
+								isRessign = true;
+								break;
+							}
+						}
+					}
+
+					if (isRessign)
+						setChargerMode(gun_index, MODE_REASSIGN);
+					else
+						setChargerMode(gun_index, MODE_PRECHARGE);
+				}
+					break;
+				case S_REASSIGN:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
+					}
+
+					// 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
+					// 此狀態下~ 控制權在於 PSU 及 EV小板 Process
+					if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
+							ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
+						setChargerMode(gun_index, MODE_PRECHARGE);
+					else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY &&
+						ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
+					{
+						PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
+						ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
+						ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
+					}
+
+					PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
+				}
+					break;
+				case S_PREPARNING:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
+						StopGunInfoTimeoutDet(gun_index);
+						StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
+					}
+
+					if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
+						ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
+
+					// Precharge 三個流程 : 1 Precharge, 2 Preparing for ev, 3 Preparing for evse
+					// Precharge : AC Contactor <Relay board>, Relay k1 k2 <Relay board>, PSU AddressAssignment, PSU GroupAvailablePower
+					// Preparing for ev : 車端通訊流程
+					// Preparing for evse : PSU (output 500V, 2A), GFD Test <Relay board>
+					//ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES &&
+					if (((ShmPsuData->SystemPresentPsuQuantity > 0 &&
+							ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 &&
+							ShmPsuData->PsuGroup[gun_index].GroupAvailablePower > 10) &&
+							chargingInfo[gun_index]->AvailableChargingPower > 10))
+					{
+						setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
+					}
+
+					if (isEvBoardStopChargeFlag(gun_index) == YES ||
+							OcppRemoteStop(gun_index) == YES)
+					{
+						// 板端或後臺要求停止
+						ChargingTerminalProcess(gun_index);
+					}
+
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
+				}
+					break;
+				case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
+						strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+						StopGunInfoTimeoutDet(gun_index);
+						StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
+					}
+
+					if (chargingInfo[gun_index]->Type == _Type_Chademo)
+					{
+						// 檢查車端的槍鎖是否為鎖上
+						if (isEvGunLocked_chademo(gun_index) == YES)
+						{
+							setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
+						}
+					}
+					else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
+					{
+						// 檢查車端的 charging enable 是否為 1
+						if (isEvGunLocked_ccs(gun_index) == YES)
+						{
+							setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
+						}
+					}
+
+					if (isEvBoardStopChargeFlag(gun_index) == YES  ||
+							OcppRemoteStop(gun_index) == YES)
+					{
+						// 板端或後臺要求停止
+						ChargingTerminalProcess(gun_index);
+					}
+
+					// LCM => Pre-charging
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
+				}
+					break;
+				case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
+						StopGunInfoTimeoutDet(gun_index);
+						StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
+					}
+
+					if (chargingInfo[gun_index]->Type == _Type_Chademo)
+					{
+						// 檢查樁端的 GFD 結果
+						if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
+						{
+							// 當前操作的槍號,進入 Charging
+							setChargerMode(gun_index, MODE_CHARGING);
+						}
+
+						if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+						{
+							// GFD 錯誤停止
+							RecordAlarmCode("012234");
+						}
+					}
+					else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
+					{
+						// 檢查樁端的 GFD 結果
+						if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS)
+						{
+							setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
+						}
+
+						if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+						{
+							// GFD 錯誤停止
+							RecordAlarmCode("012235");
+						}
+					}
+
+					if (isEvBoardStopChargeFlag(gun_index) == YES ||
+							chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
+							OcppRemoteStop(gun_index) == YES)
+					{
+						// 板端或後臺要求停止
+						ChargingTerminalProcess(gun_index);
+					}
+
+					// LCM => Pre-charging
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
+				}
+					break;
+				case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
+						StopGunInfoTimeoutDet(gun_index);
+						ftime(&startChargingTime[gun_index]);
+						ChangeStartOrStopDateTime(YES, gun_index);
+						OcppStartTransation(gun_index);
+					}
+					ftime(&endChargingTime[gun_index]);
+					chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
+
+					if (chargingInfo[gun_index]->Type == _Type_Chademo)
+					{
+						if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+						{
+							// GFD 錯誤停止
+							RecordAlarmCode("012234");
+						}
+					}
+					else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
+					{
+						if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+						{
+							// GFD 錯誤停止
+							RecordAlarmCode("012235");
+						}
+					}
+
+					if (isEvBoardStopChargeFlag(gun_index) ||
+							chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
+							OcppRemoteStop(gun_index) ||
+							CheckBackendChargingTimeout(gun_index) ||
+							CheckBackendChargingEnergy(gun_index))
+					{
+						// 板端或後臺要求停止
+						ChargingTerminalProcess(gun_index);
+					}
+
+					// LCM => Charging
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
+				}
+					break;
+				case S_TERMINATING:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC ("terminating......................... %x \n", gun_index);
+						StopGunInfoTimeoutDet(gun_index);
+					}
+
+					if (chargingInfo[gun_index]->Type == _Type_Chademo)
+					{
+						// 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
+						if (isEvStopCharging_chademo(gun_index) == YES ||
+								isPrechargeStatus_chademo(gun_index) <= 0)
+						{
+							setChargerMode(gun_index, MODE_COMPLETE);
+						}
+					}
+					else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
+					{
+						// 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
+						if (isEvStopCharging_ccs(gun_index) == YES)
+						{
+							setChargerMode(gun_index, MODE_COMPLETE);
+						}
+					}
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
+				}
+					break;
+				case S_COMPLETE:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC ("complete......................... %x \n", gun_index);
+						OcppStopTransation(gun_index);
+						ftime(&endChargingTime[gun_index]);
+						if (chargingInfo[gun_index]->RemainChargingDuration != 0)
+							chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
+
+						//strcpy((char *)chargingInfo[gun_index]->CardNumber, "");
+						//strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
+						StopGunInfoTimeoutDet(gun_index);
+						ChangeStartOrStopDateTime(NO, gun_index);
+						// 單槍或者雙槍單充自動回 idle
+						if (_gunCount == 1 || (_gunCount > 1 && ONE_CONNECTOR_USE == YES))
+							StartGunInfoTimeoutDet(gun_index, Timeout_WaitforCompleteDet);
+					}
+
+					if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
+						ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
+				}
+					break;
+				case S_CCS_PRECHARGE_ST0:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
+						StopGunInfoTimeoutDet(gun_index);
+						StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
+					}
+
+					if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+					{
+						// GFD 錯誤停止
+						RecordAlarmCode("012235");
+					}
+
+					if (isEvBoardStopChargeFlag(gun_index) == YES ||
+							chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+					{
+						// 板端要求停止
+						ChargingTerminalProcess(gun_index);
+					}
+
+					// 等待 EV 小板 (CCS) 通知可以開始 Precharge
+					// 切換 D+ Relay to Precharge Relay
+					if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
+					{
+						if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
+						//if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
+						{
+							PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
+							chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
+						}
+					}
+					else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
+					{
+						setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
+					}
+
+					break;
+				}
+				case S_CCS_PRECHARGE_ST1:
+				{
+					if (isModeChange(gun_index))
+					{
+						PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
+					}
+
+					if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+					{
+						// GFD 錯誤停止
+						RecordAlarmCode("012235");
+					}
+
+					if (isEvBoardStopChargeFlag(gun_index) == YES ||
+							chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
+					{
+						// 板端要求停止
+						ChargingTerminalProcess(gun_index);
+					}
+
+					// 等待小板通知進入充電
+					// 切換 D+ Relay to Precharge Relay
+					if (chargingInfo[gun_index]->RelayK1K2Status == YES)
+					{
+						chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
+						setChargerMode(gun_index, MODE_CHARGING);
+					}
+					break;
+				}
+			}
+		}
+
+		if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
+		{
+			ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
+		}
+		else
+		{
+			ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
+		}
+
+		write(wtdFd, "a", 1);
+		usleep(whileLoopTime);
+	}
+
+	return FAIL;
+}

BIN
EVSE/Projects/DW30/Apps/main.o


+ 12 - 11
EVSE/Projects/DW30/Apps/timeout.h

@@ -34,19 +34,20 @@
 
 enum Timeout_flag
 {
-	Timeout_None =					0,
-	Timeout_SelftestChk = 			1,
-	Timeout_Authorizing = 			2,
-	Timeout_VerifyFail = 			3,
-	Timeout_VerifyComp = 			4,
-	Timeout_WaitPlug = 				5,
+	Timeout_None =						0,
+	Timeout_SelftestChk = 				1,
+	Timeout_Authorizing = 				2,
+	Timeout_VerifyFail = 				3,
+	Timeout_VerifyComp = 				4,
+	Timeout_WaitPlug = 					5,
 
-	Timeout_Preparing = 			6,
-	Timeout_EvChargingDet = 		7,
-	Timeout_EvseChargingDet = 		8,
-	Timeout_WaitforCompleteDet = 	9,
+	Timeout_Preparing = 				6,
+	Timeout_EvChargingDet = 			7,
+	Timeout_EvseChargingDet = 			8,
+	Timeout_WaitforCompleteDet = 		9,
 
-	Timeout_ForCcsPrechargeDet = 	10,
+	Timeout_ForCcsPrechargeDet = 		10,
+	Timeout_ReturnToChargingGunDet = 	11
 };
 
 #endif /* TIMEOUT_H_ */

BIN
EVSE/Projects/DW30/Apps/timeout.o


BIN
EVSE/Projects/DW30/Images/FactoryDefaultConfig.bin


BIN
EVSE/Projects/DW30/Images/ramdisk.gz


+ 40 - 46
EVSE/Projects/define.h

@@ -46,52 +46,52 @@ Storage							0x0A200000-0x7FFFFFFF		1886 MB
 	#define ONE_CONNECTOR_USE	0
 #elif BYTONGB
 	#define MAX_PSU_QUANTITY        62
-        #define CHAdeMO_QUANTITY        1
-        #define CCS_QUANTITY            1
-        #define GB_QUANTITY             0
-        #define AC_QUANTITY             1
-        #define PSU_QUANTITY            2
-        #define ONE_CONNECTOR_USE       0
+	#define CHAdeMO_QUANTITY        1
+	#define CCS_QUANTITY            1
+	#define GB_QUANTITY             0
+	#define AC_QUANTITY             1
+	#define PSU_QUANTITY            2
+	#define ONE_CONNECTOR_USE       0
 #elif DW30
-        #define MAX_PSU_QUANTITY        62
-        #define CHAdeMO_QUANTITY        1
-        #define CCS_QUANTITY            1
-        #define GB_QUANTITY             0
-        #define AC_QUANTITY             0
-        #define PSU_QUANTITY            2
-        #define ONE_CONNECTOR_USE       0
+	#define MAX_PSU_QUANTITY        62
+	#define CHAdeMO_QUANTITY        1
+	#define CCS_QUANTITY            1
+	#define GB_QUANTITY             0
+	#define AC_QUANTITY             0
+	#define PSU_QUANTITY            2
+	#define ONE_CONNECTOR_USE       1
 #elif DM30
-        #define MAX_PSU_QUANTITY        62
-        #define CHAdeMO_QUANTITY        1
-        #define CCS_QUANTITY            1
-        #define GB_QUANTITY             0
-        #define AC_QUANTITY             0
-        #define PSU_QUANTITY            2
-        #define ONE_CONNECTOR_USE       0
+	#define MAX_PSU_QUANTITY        62
+	#define CHAdeMO_QUANTITY        0
+	#define CCS_QUANTITY            1
+	#define GB_QUANTITY             0
+	#define AC_QUANTITY             0
+	#define PSU_QUANTITY            2
+	#define ONE_CONNECTOR_USE       0
 #elif DS60120
-        #define MAX_PSU_QUANTITY        62
-        #define CHAdeMO_QUANTITY        1
-        #define CCS_QUANTITY            1
-        #define GB_QUANTITY             0
-        #define AC_QUANTITY             0
-        #define PSU_QUANTITY            2
-        #define ONE_CONNECTOR_USE       0
+	#define MAX_PSU_QUANTITY        62
+	#define CHAdeMO_QUANTITY        1
+	#define CCS_QUANTITY            1
+	#define GB_QUANTITY             0
+	#define AC_QUANTITY             0
+	#define PSU_QUANTITY            2
+	#define ONE_CONNECTOR_USE       0
 #elif DS60210
-        #define MAX_PSU_QUANTITY        62
-        #define CHAdeMO_QUANTITY        1
-        #define CCS_QUANTITY            1
-        #define GB_QUANTITY             0
-        #define AC_QUANTITY             0
-        #define PSU_QUANTITY            2
-        #define ONE_CONNECTOR_USE       0
+	#define MAX_PSU_QUANTITY        62
+	#define CHAdeMO_QUANTITY        1
+	#define CCS_QUANTITY            1
+	#define GB_QUANTITY             0
+	#define AC_QUANTITY             0
+	#define PSU_QUANTITY            2
+	#define ONE_CONNECTOR_USE       0
 #else
 	#define MAX_PSU_QUANTITY        62
-        #define CHAdeMO_QUANTITY        1
-        #define CCS_QUANTITY            1
-        #define GB_QUANTITY             0
-        #define AC_QUANTITY             1
-        #define PSU_QUANTITY            2
-        #define ONE_CONNECTOR_USE       0
+	#define CHAdeMO_QUANTITY        1
+	#define CCS_QUANTITY            1
+	#define GB_QUANTITY             0
+	#define AC_QUANTITY             1
+	#define PSU_QUANTITY            2
+	#define ONE_CONNECTOR_USE       0
 #endif
 
 
@@ -423,12 +423,7 @@ struct WARNING_CODE_INFO
 	unsigned char WarningCount;
 	unsigned char PageIndex;
 	unsigned char WarningCode[10][7];
-};
-
-struct STOP_CHARGING_ALARM_CODE
-{
 	unsigned char Level;
-	unsigned char StopCode[4][7];
 };
 
 struct SysConfigAndInfo
@@ -436,7 +431,6 @@ struct SysConfigAndInfo
 	struct SysConfigData				SysConfig;
 	struct SysInfoData					SysInfo;
 	struct WARNING_CODE_INFO			SysWarningInfo;
-	struct STOP_CHARGING_ALARM_CODE		SysStopChargingAlarmCode;
 };
 
 /**************************************************************************************/

BIN
EVSE/rootfs/root/Module_4g


BIN
EVSE/rootfs/root/Module_ProduceUtils


BIN
EVSE/rootfs/root/Module_Wifi


BIN
EVSE/rootfs/root/OcppBackend


BIN
EVSE/rootfs/root/WebService


BIN
EVSE/rootfs/root/logPackTools


Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác