瀏覽代碼

2019-12-26 / Folus Wen

Actions:
1. Initial apps source code.

Files
1. As follow commit history.
FolusWen 5 年之前
父節點
當前提交
2d5a127f9e

二進制
EVSE/Projects/AW-Regular/Apps/4gModem


+ 0 - 467
EVSE/Projects/AW-Regular/Apps/4gModem.c

@@ -1,467 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <netinet/in.h>
-#include <unistd.h>
-#include "define.h"
-
-//#define debug		
-#define CheckModemInterval		30	//sec
-#define CheckSimInterval			10 	//sec
-#define CheckConnectionInterval		5 	//sec
-#define DisconnInterval				60 	//sec
-
-
-
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct FanModuleData			*ShmFanModuleData;
-
-
-#ifdef SystemLogMessage
-int StoreLogMsg(unsigned char *DataString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	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,
-			DataString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",DataString);
-	#endif
-}		
-#endif
-
-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;
-}	
-	
-/**************************************************************************************/
-/**************************Init all share memory *********************************/
-/**************************************************************************************/
-int InitShareMemory()
-{
-	int MeterSMId;
-	
-	//creat ShmSysConfigAndInfo
-	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]InitShareMemory:shmget ShmSysConfigAndInfo NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]InitShareMemory:shmat ShmSysConfigAndInfo NG");
-		#endif		
-		return 0;
-   	 }
-   	 //creat ShmStatusCodeData
-   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]InitShareMemory:shmget ShmStatusCodeData NG");
-		#endif		
-		return 0;	
-	}
-    	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]InitShareMemory:shmat ShmStatusCodeData NG");
-		#endif		
-		return 0;
-   	 }
-   	
-    	return 1;
-}
-
-
-int Check4GModem()
-{
-	//return 0: No 4G modem equipped
-	//return 1: Qutel 4G modem equipped
-	//return 2: ublox 4G modem equipped
-	
-	int Rtn1,Rtn2;
-	
-	//check Qutel 4G modem
-	Rtn1=Rtn2=-1;
-	Rtn1=access("/dev/ttyUSB0",R_OK);
-	if(Rtn1==0)
-	{
-		Rtn2=access("/dev/ttyUSB2",R_OK);
-		if(Rtn2==0)
-		{
-			#ifdef SystemLogMessage	
-			StoreLogMsg("[4gModem]Check4GModem: Qutel 4G modem be found");
-			#endif	
-			return 1;
-		}
-	}	
-	//check ublox 4G modem
-	Rtn1=Rtn2=-1;
-	Rtn1=access("/dev/ttyACM0",R_OK);
-	if(Rtn1==0)
-	{
-		Rtn2=access("/dev/ttyACM2",R_OK);
-		if(Rtn2==0)
-		{
-			#ifdef SystemLogMessage	
-			StoreLogMsg("[4gModem]Check4GModem: ublox 4G modem be found");
-			#endif	
-			return 2;
-		}
-	}	
-	#ifdef SystemLogMessage	
-	StoreLogMsg("[4gModem]Check4GModem: No 4G modem be found");
-	#endif	
-	return 0;	
-}
-
-int InitComPort(unsigned char ModemModel)
-{
-	int UsbFd;
-	struct termios tios;
-	
-	if(ModemModel==1)	//Qutel 4G modem equipped
-		UsbFd = open("/dev/ttyUSB2", O_RDWR|O_NOCTTY);
-	else if(ModemModel==2)	//ublox 4G modem equipped
-		UsbFd = open("/dev/ttyACM2", O_RDWR|O_NOCTTY);
-	else
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]InitComPort: Unknow ModemModel ");
-		#endif		
-		return -1;
-	}	
-	
-	if(UsbFd<0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]InitComPort:  UsbFd open failed");
-		#endif	
-		return -1;
-	}	
-	ioctl (UsbFd, 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]=100; 
-	tios.c_lflag=0;  
-	tcflush(UsbFd, TCIFLUSH);
-	ioctl (UsbFd, TCSETS, &tios);
-	return UsbFd;
-	
-}
-
-void GetModelName(int Fd, unsigned char *ModelName)
-{
-	//AT command processing
-}
-
-void GetModelVersion(int Fd, unsigned char *ModelVersion)
-{
-	//AT command processing
-}
-
-void GetModelImei(int Fd, unsigned char *ModelImei)
-{
-	//AT command processing
-}
-
-void GetSimImsi(int Fd, unsigned char *SimImsi)
-{
-	//AT command processing
-}
-
-unsigned char GetModemMode(int Fd)
-{
-	int Mode;
-	//AT command processing
-	return Mode;
-}
-
-int GetSignalRssi(int Fd)
-{
-	int RssiValue;
-	//AT command processing
-	return RssiValue;
-}
-
-int GetPPP0Info(unsigned char *IPaddress)
-{
-    	int fd,rd=0;
-	unsigned int address;
-	char buf[128],addr[32],*sptr,*eptr;
-	
-
-    	system("ifconfig ppp0 | grep \"inet addr:\" > /mnt/GetPPP0Info");
-	fd = open("/mnt/GetPPP0Info", O_RDONLY);
-    	if(fd<0)
-   	{
-    		system("rm -f /mnt/GetPPP0Info");
-	        return 0;
-    	}
-    
-    	memset(addr,0,sizeof(addr));
-    	memset(buf,0,sizeof(buf));
-    	rd=read(fd,buf,sizeof(buf));	
-    	if(rd<=0)
-    	{
-    		close(fd);
-    		system("rm -f /mnt/GetPPP0Info");
-        	return 0;
-   	}
-    	close(fd);
-    	if((sptr=strstr(buf,"inet addr:"))==NULL)
-    	{
-    		close(fd);
-    		system("rm -f /mnt/GetPPP0Info");
-        	return 0;
-    	}
-    	sptr+=strlen("inet addr:");
-    	if((eptr=strstr(buf,"  P-t-P:"))==NULL)
-    	{
-    		close(fd);
-    		system("rm -f /mnt/GetPPP0Info");
-        	return 0;
-    	}
-    	memset(IPaddress,0,strlen(IPaddress));
-    	strncpy(IPaddress,sptr,eptr-sptr);   
-    	#ifdef SystemLogMessage	
-	{
-		unsigned char Buffer[128];
-		memset(Buffer,0,sizeof(Buffer));
-		sprintf(Buffer,"[4gModem]GetPPP0Info: 4G IP Address = %s",IPaddress);
-		StoreLogMsg(Buffer);
-	}
-	#endif		
-    	return 1;
-}
-int  Load4gConfiguration()
-{
-	unsigned char CopyTmp[128];
-	
-	if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn)<=0)
-		return 0;
-	system("cat /dev/null > /root/ppp/auto-apn.conf");
-	system("chmod 777 /root/ppp/auto-apn.conf");  
-	system("cat /dev/null > /etc/ppp/options"); 
-	system("chmod 777 /etc/ppp/options"); 
-	system("cat /dev/null > /etc/ppp/pap-secrets"); 
-	system("chmod 777 /etc/ppp/pap-secrets"); 
-	system("cat /dev/null > /etc/ppp/chap-secrets"); 
-	system("chmod 777 /etc/ppp/chap-secrets"); 
-	memset(CopyTmp,0,sizeof(CopyTmp));
-	sprintf(CopyTmp,"echo \"APN=\"%s\"\" > /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
-	system(CopyTmp);
-	system("echo \"ACCOUNT=\" >> /root/ppp/auto-apn.conf");
-	system("echo \"PASSWORD=\" >> /root/ppp/auto-apn.conf");
-	
-	if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId)>0)
-	{	
-		memset(CopyTmp,0,sizeof(CopyTmp));
-		sprintf(CopyTmp,"echo \"name %s \" > /etc/ppp/options",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
-		system(CopyTmp);
-		
-		memset(CopyTmp,0,sizeof(CopyTmp));
-		sprintf(CopyTmp,"echo \"%s * %s \" > /etc/ppp/pap-secrets",
-		ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId,
-		((strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)?ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd : " "));
-		system(CopyTmp);
-
-		memset(CopyTmp,0,sizeof(CopyTmp));
-		sprintf(CopyTmp,"echo \"%s * %s \" > /etc/ppp/chap-secrets",
-		ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId,
-		((strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)?ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd : " "));
-		system(CopyTmp);
-	}	
-	return 1;
-}
-void ResetModem()
-{
-	//power cycle modem by GPIO
-}
-/**************************************************************/
-/************** main function********************************/
-/*************************************************************/
-int main(int argc,char *argv[])
-{
-	unsigned char ModuleModel=0;//0: None, 1: Qutel, 2: ublox SARA-U201
-	int UsbFd=-1;
-	unsigned int StartTime,EndTime,Tmp; 
-	unsigned char TmpIpAddr[16];
-	
-	//Initialization
-	if(InitShareMemory()==0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[4gModem]main:InitShareMemory NG");
-		#endif		
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}	
-		sleep(5);
-		return 0;
-	}	
-	ResetModem();
-	
-ReCheckModem:    
-	//ResetModem();
-	UsbFd=-1;
-	ModuleModel=0;
-	Load4gConfiguration();
-
-	while(1)
-	{
-		ModuleModel=Check4GModem();
-		if(ModuleModel<=0)
-		{
-			sleep(CheckModemInterval);
-			continue;
-		}	
-		if(UsbFd>0)
-			close(UsbFd);
-		UsbFd=InitComPort(ModuleModel);
-		if(UsbFd<0)
-		{
-			sleep(CheckModemInterval);
-			continue;
-		}
-		/**** Record some modem information and dial up if SIM card ready ****/
-		memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface,0,sizeof(struct TeleConfigData));
-		while(1)
-		{
-			if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName)<=0)
-				GetModelName(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
-			if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer)<=0)
-				GetModelVersion(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);	
-			if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei)<=0)
-				GetModelImei(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);		
-			GetSimImsi(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);		
-			if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi)<=0)
-			{	
-				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus=0;
-				sleep(CheckSimInterval);	
-				if(Check4GModem()<=0)
-					goto ReCheckModem;
-			}
-			else
-			{
-				ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus=1;
-				if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn)<=0)
-				{
-					sleep(DisconnInterval);
-					goto ReCheckModem;		
-				}
-				system("killall 4GDetection");	
-				sleep(2);
-				if(ModuleModel==1)
-					system("/root/ppp/4GDetection /dev/ttyUSB0 &");	
-				else if(ModuleModel==2)
-					system("/root/ppp/4GDetection /dev/ttyACM0 &");
-				break;	
-			}			
-		}
-		/***** Periodically check RSSI and connection*****/
-		StartTime=time((time_t*)NULL);
-		EndTime=time((time_t*)NULL);
-		while(1)
-		{
-			if((time((time_t*)NULL)-StartTime)>=CheckConnectionInterval)
-			{	
-				//check IP address 
-				memset(TmpIpAddr,0,sizeof(TmpIpAddr));
-				if(GetPPP0Info(TmpIpAddr)<=0)
-				{
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn=0;
-					if((time((time_t*)NULL)-EndTime)>=DisconnInterval)
-						goto ReCheckModem; 
-				}	
-				else
-				{
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn=1;
-					if(strstr(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,TmpIpAddr)==NULL)
-					{
-						#ifdef SystemLogMessage	
-						{
-							unsigned char Buffer[128];
-							memset(Buffer,0,sizeof(Buffer));
-							sprintf(Buffer,"[4gModem]main:  4G IP Address changed from %s to %s",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,TmpIpAddr);
-							StoreLogMsg(Buffer);
-						}
-						#endif		
-						memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,0,sizeof(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress));
-						strcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,TmpIpAddr);
-						system("route del default");
-						system("route add default dev ppp0");
-					}
-					EndTime=time((time_t*)NULL);
-				}	
-				//check RSSI
-				Tmp=GetSignalRssi(UsbFd);
-				if(abs(Tmp-ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi)>=5)
-				{
-					#ifdef SystemLogMessage	
-					{
-						unsigned char Buffer[128];
-						memset(Buffer,0,sizeof(Buffer));
-						sprintf(Buffer,"[4gModem]main: RSSI changed from %d to %d",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi,Tmp);
-						StoreLogMsg(Buffer);
-					}
-					#endif		
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi=Tmp;
-				}	
-				//check Mode
-				Tmp=GetModemMode(UsbFd);
-				if(Tmp!=ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode)
-				{
-					#ifdef SystemLogMessage	
-					{
-						unsigned char Buffer[128];
-						memset(Buffer,0,sizeof(Buffer));
-						sprintf(Buffer,"[4gModem]main: Modem mode changed from %d to %d",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode,Tmp);
-						StoreLogMsg(Buffer);
-					}
-					#endif
-					ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode=Tmp;
-				}	
-				StartTime=time((time_t*)NULL);
-			}
-		}
-	}//main while loop
-}
-
-

二進制
EVSE/Projects/AW-Regular/Apps/EventLogging


+ 0 - 218
EVSE/Projects/AW-Regular/Apps/EventLogging.c

@@ -1,218 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <math.h>//for pow
-#include <unistd.h>
-#include "../../define.h"
-
-//#define Debug
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-
-
-#ifdef SystemLogMessage
-int StoreLogMsg(unsigned char *DataString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	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,
-			DataString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",DataString);
-	#endif
-}		
-#endif
-
-int StoreEventLogMsg(unsigned char *EventCodeString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	memset(Buf,0,sizeof(Buf));
-	CurrentTime = time(NULL);
-	tm=localtime(&CurrentTime);
-	sprintf(Buf,"echo \"%04d.%02d.%02d %02d:%02d:%02d - %s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
-			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
-			EventCodeString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",Buf);
-	#endif
-}		
-
-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;
-}	
-	
-/**************************************************************************************/
-/**************************Init all share memory *********************************/
-/**************************************************************************************/
-int InitShareMemory()
-{
-	int MeterSMId;
-	
-	//creat ShmSysConfigAndInfo
-	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[EventLogging]InitShareMemory:shmget ShmSysConfigAndInfo NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[EventLogging]InitShareMemory:shmat ShmSysConfigAndInfo NG");
-		#endif		
-		return 0;
-   	 }
-   	 //creat ShmStatusCodeData
-   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[EventLogging]InitShareMemory:shmget ShmStatusCodeData NG");
-		#endif		
-		return 0;	
-	}
-    	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[EventLogging]InitShareMemory:shmat ShmStatusCodeData NG");
-		#endif		
-		return 0;
-   	 }
-
-    	return 1;
-}
-
-int main(int argc,char *argv[])
-{
-	int ByteCount,BitCount;
-	unsigned char tmp, EventCodeTmp[7];
-	
-	//Initialization
-	if(InitShareMemory()==0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[EventLogging]main:InitShareMemory NG");
-		#endif		
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}	
-		sleep(5);
-		return 0;
-	}	
-	
-	while(1)
-	{
-		//check Fault Status
-		for(ByteCount=0;ByteCount<4;ByteCount++)
-		{
-			if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
-			{
-				tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
-				for(BitCount=0;BitCount<8;BitCount++)
-				{
-					if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
-					{
-						memset(EventCodeTmp,0,sizeof(EventCodeTmp));
-						memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
-						if(((tmp>>BitCount)&0x01)==0)//Recovered
-						{	
-							EventCodeTmp[0]=1;
-							ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]&=(0<<BitCount);
-						}
-						else
-							ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]|=(1<<BitCount);
-						StoreEventLogMsg(EventCodeTmp);
-					}
-				}
-			}
-		}
-		
-		//check Alarm Status
-		for(ByteCount=0;ByteCount<8;ByteCount++)
-		{
-			if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
-			{
-				tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
-				for(BitCount=0;BitCount<8;BitCount++)
-				{
-					if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
-					{
-						memset(EventCodeTmp,0,sizeof(EventCodeTmp));
-						memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
-						if(((tmp>>BitCount)&0x01)==0)//Recovered
-						{	
-							EventCodeTmp[0]=1;
-							ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]&=(0<<BitCount);
-						}
-						else
-							ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]|=(1<<BitCount);
-						StoreEventLogMsg(EventCodeTmp);
-					}
-				}
-			}
-		}
-		
-		//check Info Status
-		for(ByteCount=0;ByteCount<8;ByteCount++)
-		{
-			if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
-			{
-				tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
-				for(BitCount=0;BitCount<8;BitCount++)
-				{
-					if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
-					{
-						memset(EventCodeTmp,0,sizeof(EventCodeTmp));
-						memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
-						if(((tmp>>BitCount)&0x01)==0)//Recovered
-						{	
-							EventCodeTmp[0]=1;
-							ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]&=(0<<BitCount);
-						}
-						else
-							ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]|=(1<<BitCount);
-						StoreEventLogMsg(EventCodeTmp);
-					}
-				}
-			}
-		}
-		
-	}//main while loop 
-}
-
-

+ 0 - 109
EVSE/Projects/AW-Regular/Apps/FWMaker.c

@@ -1,109 +0,0 @@
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <string.h>
-#include <net/if.h> /*struct ifreq*/
-#include <linux/sockios.h> /*SIOCGIFINDEX*/
-#include <linux/socket.h>
-#include <errno.h> 
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <fcntl.h>
-#include <sys/mman.h>
-
-//./FWMaker 172.16.24.126
-int main(int argc,char *argv[])
-{
-	unsigned char *MemBuf,*MemBuf2,buf[64];
-    int fd,rd=0,wrd=0,tmp=0,rd2=0;
-    unsigned int CheckSum=0;
-
-
-
-	memset(buf,0,64);
-	sprintf(buf,"tftp -gr uImage -l /mnt/uImage %s",argv[1]);   
-	system(buf);
-	memset(buf,0,64);
-	sprintf(buf,"tftp -gr rootfs_nor.img -l /mnt/rootfs_nor.img %s",argv[1]);   
-	system(buf);
-
-	if((MemBuf=malloc(0x100000D))==NULL)
-	{
-		printf("Allocate MemBuf memory error\n");
-		return 0;
-	}	
-	memset(MemBuf, 0xff, 0x100000D);
-	fd = open("/mnt/uImage", O_RDWR);
-	if (fd > 0) 
-	{
-	    if((rd=read(fd,MemBuf,0x100000D))<=0)
-	    {
-			printf("/mnt/uImage read Error\n");
-			free(MemBuf);
-			close(fd);
-			return 0;
-	    }   
-	    close(fd);
-	    for(tmp=0;tmp<rd;tmp++)
-	    	CheckSum+=MemBuf[tmp];
-	    strncpy(MemBuf+rd,"DELTADCOK",9);	
-	   *(MemBuf+rd+9)=CheckSum>>24;
-	    *(MemBuf+rd+10)=CheckSum>>16;
-	    *(MemBuf+rd+11)=CheckSum>>8;
-	     *(MemBuf+rd+12)=CheckSum;
-	   // memcpy(MemBuf+rd+9,&CheckSum,4);	
-	    fd = open("/mnt/DcoKImage", O_CREAT|O_RDWR);
-	    wrd=write(fd, MemBuf, rd+13);
-	    if(wrd!=(rd+13))
-	    	printf("write error wrd=0x%x, rd=0x%x\n",wrd,rd+13);
-	   else 
-	   		printf("/mnt/DcoKImage OK\n"); 	
-	    
-	}
-	else	
-	{	
-		free(MemBuf);
-		printf("/mnt/uImage open Error\n");
-		return 0;
-	}
-	
-	
-	memset(MemBuf, 0xff, 0x100000D);
-	CheckSum=rd=0;
-	fd = open("/mnt/rootfs_nor.img", O_RDWR);
-	if (fd > 0) 
-	{
-	    if((rd=read(fd,MemBuf,0x100000D))<=0)
-	    {
-			printf("/mnt/rootfs_nor.img read Error\n");
-			free(MemBuf);
-			close(fd);
-			return 0;
-	    }   
-	    close(fd);
-	    for(tmp=0;tmp<rd;tmp++)
-	    	CheckSum+=MemBuf[tmp];
-	    strncpy(MemBuf+rd,"DELTADCOF",9);	
-	    *(MemBuf+rd+9)=CheckSum>>24;
-	    *(MemBuf+rd+10)=CheckSum>>16;
-	    *(MemBuf+rd+11)=CheckSum>>8;
-	     *(MemBuf+rd+12)=CheckSum;
-	    //memcpy(MemBuf+rd+9,&CheckSum,4);	
-	    fd = open("/mnt/DcoFImage", O_CREAT|O_RDWR);
-	    wrd=write(fd, MemBuf, rd+13);
-	    if(wrd!=(rd+13))
-	    	printf("write error wrd=0x%x, rd=0x%x\n",wrd,rd+13);
-	   else 
-	   		printf("/mnt/DcoFImage OK\n"); 	
-	    
-	}
-	else	
-	{	
-		free(MemBuf);
-		printf("/mnt/rootfs_nor.img open Error\n");
-		return 0;
-	}
-	
-}

二進制
EVSE/Projects/AW-Regular/Apps/FactoryConfig


+ 0 - 173
EVSE/Projects/AW-Regular/Apps/FactoryConfig.c

@@ -1,173 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <math.h>//for pow
-#include <unistd.h>
-#include "../../define.h"
-
-
-#ifdef SystemLogMessage
-int StoreLogMsg(unsigned char *DataString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	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,
-			DataString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",DataString);
-	#endif
-}		
-#endif
-
-
-/**************************************************************************************/
-/************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 *ptr; 
-	int fd,wrd;
-	
-	ptr=malloc(sizeof(struct SysConfigData));
-	if(ptr==NULL)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
-		#endif			
-		return 0;
-	}	
-	memset(ptr,0,sizeof(struct SysConfigData));
-	memset(&SysConfig,0,sizeof(struct SysConfigData));
-	
-	//Set default configuration
-	strcpy(SysConfig.Eth0Interface.EthIpAddress,"192.168.0.10");
-	strcpy(SysConfig.Eth0Interface.EthSubmaskAddress,"255.255.255.0");
-	strcpy(SysConfig.Eth0Interface.EthGatewayAddress,"192.168.0.254");
-	strcpy(SysConfig.Eth1Interface.EthIpAddress,"192.168.1.10");
-	strcpy(SysConfig.Eth1Interface.EthSubmaskAddress,"255.255.255.0");
-	strcpy(SysConfig.Eth1Interface.EthGatewayAddress,"192.168.1.254");
-	SysConfig.BackendConnTimeout=300; //300 seconds
-	
-	//copy default configuration to pointer
-	memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
-	
-	//calculate CRC
-	Chk=0;
-	for(i=0;i<(sizeof(struct SysConfigData)-4);i++)
-	{
-		Chk+=*(ptr+i);
-	}
-	SysConfig.Checksum=Chk;
-	
-	fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR|O_CREAT);
-	if (fd < 0) 
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin 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 /mnt/FactoryDefaultConfig.bin NG");
-		#endif	
-		free(ptr);
-		return 0;
-	}
-	
-	fd = open("/dev/mtdblock12", O_RDWR);
-	if (fd < 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("/dev/mtdblock11", O_RDWR);
-	if (fd < 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;
-	}	
-	
-	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;
-	}	
-	
-	free(ptr);
-	#ifdef SystemLogMessage	
-	StoreLogMsg("[FactoryConfig]main: FactoryConfig OK");
-	#endif	
-}

二進制
EVSE/Projects/AW-Regular/Apps/InternalComm


+ 0 - 200
EVSE/Projects/AW-Regular/Apps/InternalComm.c

@@ -1,200 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <math.h>//for pow
-#include <unistd.h>
-#include "../../define.h"
-
-
-//#define Debug
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct FanModuleData			*ShmFanModuleData;
-struct RelayModuleData			*ShmRelayModuleData;
-
-
-#ifdef SystemLogMessage
-int StoreLogMsg(unsigned char *DataString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	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,
-			DataString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",DataString);
-	#endif
-}		
-#endif
-
-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;
-}	
-	
-/**************************************************************************************/
-/**************************Init all share memory *********************************/
-/**************************************************************************************/
-int InitShareMemory()
-{
-	int MeterSMId;
-	
-	//creat ShmSysConfigAndInfo
-	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmget ShmSysConfigAndInfo NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmat ShmSysConfigAndInfo NG");
-		#endif		
-		return 0;
-   	 }
-   	 //creat ShmStatusCodeData
-   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmget ShmStatusCodeData NG");
-		#endif		
-		return 0;	
-	}
-    	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmat ShmStatusCodeData NG");
-		#endif		
-		return 0;
-   	 }
-
-   	//creat ShmFanModuleData
-   	 if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmget ShmFanModuleData NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmat ShmFanModuleData NG");
-		#endif		
-		return 0;
-   	 }
-   	 memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
-   	 //creat ShmRelayModuleData
-   	 if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmget ShmRelayModuleData NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitShareMemory:shmat ShmRelayModuleData NG");
-		#endif		
-		return 0;
-   	 }
-    	return 1;
-}
-
-int InitComPort()
-{
-	int fd;
-	struct termios tios;
-	
-	fd = open("/dev/ttyS4", O_RDWR);
-	if(fd<=0)
-	{ 
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]InitComPort: open /dev/ttyS4 NG");
-		#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]=1000; 
-	tios.c_lflag=0;  
-	tcflush(fd, TCIFLUSH);
-	ioctl (fd, TCSETS, &tios);
-	return fd;
-}
-
-/**************************************************************/
-/************** main function********************************/
-/*************************************************************/
-int main(int argc,char *argv[])
-{
-	int Uart4Fd;
-
-	//Initialization
-	if(InitShareMemory()==0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]main:InitShareMemory NG");
-		#endif		
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}	
-		sleep(5);
-		return 0;
-	}	
-	Uart4Fd=InitComPort();
-	if(Uart4Fd<0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[InternalComm]main:InitComPort NG");
-		#endif		
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
-		}	
-		sleep(5);
-		return 0;
-	}	
-	
-	while(1)
-	{
-		//processing
-	}//main while loop
-}
-
-

+ 44 - 54
EVSE/Projects/AW-Regular/Apps/Makefile

@@ -2,68 +2,58 @@
 export PATH=/bin:/sbin:/usr/bin:$(SDK_PATH_TARGET)/usr/bin:$PATH
 
 #define library variable
-Internal485ProtocolLib = -L ../../../Modularization/Internal485Protocol -lInternal485Protocol
-PsuCommProtocolLib = -L ../../../Modularization/PsuCommProtocol -lPsuCommProtocol
+Lib_Module_RFID = -L../../../Modularization -lModule_RFID
+Lib_Module_Upgrade = "-L../../../Modularization" -lModule_Upgrade
+
 
 all: CopyFile apps
-#apps: MainTask PsuCommTask InternalCommTask 4gModemTask FactoryConfigTask EvCommTask UpdateRootfsTask LcmControlTask FWMakerTask PrimaryCommTask OcppBackendTask ErrorHandle
-apps: MainTask FactoryConfigTask
+apps: Module_InternalComm_Task Module_FactoryConfig_Task Module_EventLogging_Task Module_AlarmDetect_Task Module_CSU_Task
 
-MainTask:
-	rm -f main; $(CC) main.c -lm -o main
-#	cp -f main ../Images/root
-	
-PsuCommTask:
-	rm -f PsuComm; $(CC) PsuComm.c ${PsuCommProtocolLib} -lm -o PsuComm
-	cp -f PsuComm ../Images/root	
-	
-InternalCommTask:
-	rm -f InternalComm; $(CC) InternalComm.c ${Internal485ProtocolLib} -lm -o InternalComm	
-	cp -f InternalComm ../Images/root
 
-WiFiModemTask:
-	rm -f WiFiModem; $(CC) WiFiModem.c -o WiFiModem	
-	cp -f WiFiModem ../Images/root
-		
-4gModemTask:
-	rm -f 4gModem; $(CC) 4gModem.c -o 4gModem		
-	cp -f 4gModem ../Images/root
-	
-FactoryConfigTask:
-	rm -f FactoryConfig; $(CC) FactoryConfig.c -o FactoryConfig	
-	cp -f FactoryConfig ../Images/root		
+Module_InternalComm_Task:
+	@echo "===== Module_InternalComm_Task ==================================="
+	rm -f Module_InternalComm 
+	$(CC) "-I../../" "-include../../../Modularization/Module_Upgrade.h" -O0 -g3 -Wall -c -fmessage-length=0 -o Module_InternalComm.o "./Module_InternalComm.c"
+	$(CC) -o Module_InternalComm Module_InternalComm.o ${Lib_Module_Upgrade}
+	rm -f *.o
+	mv -f Module_InternalComm ../Images/root
+	@echo \ 
 	
-EvCommTask:
-	rm -f EvComm; $(CC) EvComm.c -lm -o EvComm	
-	cp -f EvComm ../Images/root
+Module_FactoryConfig_Task:
+	@echo "===== Module_FactoryConfig_Task =================================="
+	rm -f Module_FactoryConfig 
+	$(CC) "-I../../" -O0 -g3 -Wall -c -fmessage-length=0 -o Module_FactoryConfig.o "./Module_FactoryConfig.c"
+	$(CC) -o Module_FactoryConfig Module_FactoryConfig.o 
+	rm -f *.o
+	mv -f Module_FactoryConfig ../Images/root
+	@echo \ 
 	
-UpdateRootfsTask:
-	rm -f UpdateRootfs; $(CC) UpdateRootfs.c -o UpdateRootfs		 
-	cp -f UpdateRootfs ../Images/root	
-
-LcmControlTask:
-	rm -f LcmControl; $(CC) LcmControl.c -o LcmControl
-	cp -f LcmControl ../Images/root			
-
-PrimaryCommTask:
-	rm -f PrimaryComm; $(CC) PrimaryComm.c -o PrimaryComm
-	cp -f PrimaryComm ../Images/root	
-
-Ocpp16Task:
-	rm -f Ocpp16; $(CC) Ocpp16.c -I ../../../GPL/libwebsockets-v2.1-stable/release/include -L ../../../GPL/libwebsockets-v2.1-stable/release/lib -lwebsockets -o Ocpp16
-	cp -f Ocpp16 ../Images/root
+Module_EventLogging_Task:
+	@echo "===== Module_EventLogging_Task ==================================="
+	rm -f Module_EventLogging 
+	$(CC) "-I../../" -O0 -g3 -Wall -c -fmessage-length=0 -o Module_EventLogging.o "./Module_EventLogging.c"
+	$(CC) -o Module_EventLogging Module_EventLogging.o 
+	rm -f *.o
+	mv -f Module_EventLogging ../Images/root
+	@echo \ 
 	
-EventLoggingTask:
-	rm -f EventLogging; $(CC) EventLogging.c -o EventLogging	
-	cp -f EventLogging ../Images/root	
-		
-FWMakerTask:
-	rm -f FWMaker; $(CC) FWMaker.c -o FWMaker	
-	cp -f FWMaker ../Images/root
+Module_AlarmDetect_Task:
+	@echo "===== Module_AlarmDetect_Task ===================================="
+	rm -f Module_AlarmDetect 
+	$(CC) "-I../../" -O0 -g3 -Wall -c -fmessage-length=0 -o Module_AlarmDetect.o "./Module_AlarmDetect.c"
+	$(CC) -o Module_AlarmDetect Module_AlarmDetect.o 
+	rm -f *.o
+	mv -f Module_AlarmDetect ../Images/root	
+	@echo \ 
 	
-ccsTask:
-	rm -f ccs; $(CC) ccs.c -o ccs	
-	cp -f ccs ../Images/root	
+Module_CSU_Task:
+	@echo "===== Module_CSU_Task ============================================"
+	rm -f main 
+	$(CC) "-I../../" "-include../../../Modularization/Module_Upgrade.h" "-include../../../Modularization/Module_RFID.h" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "./main.c"
+	$(CC) -o main main.o ${Lib_Module_RFID} ${Lib_Module_Upgrade}
+	rm -f *.o
+	mv -f main ../Images/root		
+	@echo \ 
 	
 CopyFile: 
 	rm -rfv ../Images/root

+ 968 - 0
EVSE/Projects/AW-Regular/Apps/Module_AlarmDetect.c

@@ -0,0 +1,968 @@
+/*
+ * Module_AlarmDetect.c
+ *
+ *  Created on: 2019年8月17日
+ *      Author: foluswen
+ */
+#include    <sys/types.h>
+#include    <sys/stat.h>
+#include 	<sys/time.h>
+#include 	<sys/timeb.h>
+#include 	<sys/ipc.h>
+#include 	<sys/shm.h>
+#include 	<sys/mman.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	"define.h"
+#include	"main.h"
+
+#define FILTER_SPEC			50
+
+#define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+
+#define Debug
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				0
+#define ON					1
+#define OFF					0
+
+#define SPEC_OV				275
+#define SPEC_UV				160
+#define SPEC_OC				(32*1.1)
+#define SPEC_OT				85
+
+#define HYSTERETIC_OUV		10
+#define HYSTERETIC_OT		10
+#define HYSTERETIC_OC		10
+
+struct{
+	unsigned short int	OV[3];
+	unsigned short int	UV[3];
+	unsigned short int	OC;
+	unsigned short int	OT_AMB;
+	unsigned short int	GMI;
+	unsigned short int	Short;
+	unsigned short int	Ac_Leak;
+	unsigned short int	Dc_Leak;
+	unsigned short int	HandShakingTimeout;
+	unsigned short int	EmrgencyBTN;
+	unsigned short int	Relay_Welding;
+	unsigned short int	Relay_DrivingFault;
+	unsigned short int	CP_LevelFail;
+	unsigned short int	MCU_SelfTestFail;
+}Alarm_Counter[2];
+
+void trim(char *s);
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
+
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+struct Charger					*ShmCharger;
+
+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_%s_%s_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,
+			ShmSysConfigAndInfo->SysConfig.ModelName,
+			ShmSysConfigAndInfo->SysConfig.SerialNumber);
+#ifdef SystemLogMessage
+	system(Buf);
+#endif
+
+	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
+
+	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;
+}
+
+//==========================================
+// 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;
+   	 }
+    else
+    {}
+
+   	//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;
+   	}
+    else
+    {}
+
+   	//creat ShmStatusCodeData
+   	if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
+	{
+
+		DEBUG_ERROR("shmget ShmCharger NG\r\n");
+
+		result = FAIL;
+	}
+	else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+
+		DEBUG_ERROR("shmat ShmCharger NG\r\n");
+
+		result = FAIL;
+	}
+	else
+	{}
+
+    return result;
+}
+
+//==========================================
+// Common routine
+//==========================================
+void trim(char *s)
+{
+    int i=0, j, k, l=0;
+
+    while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
+        i++;
+
+    j = strlen(s)-1;
+    while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
+        j--;
+
+    if(i==0 && j==strlen(s)-1) { }
+    else if(i==0) s[j+1] = '\0';
+    else {
+        for(k=i; k<=j; k++) s[l++] = s[k];
+        s[l] = '\0';
+    }
+}
+
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
+{
+	strncpy(dest, src + start, cnt);
+	dest[cnt] = 0;
+}
+
+//==========================================
+// Main process
+//==========================================
+int main(void)
+{
+
+	if(InitShareMemory() == FAIL)
+	{
+		DEBUG_ERROR("InitShareMemory NG\n");
+
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		sleep(5);
+		return FAIL;
+	}
+
+	for(;;)
+	{
+		for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
+		{
+			//=====================================
+			// Over voltage detection
+			//=====================================
+			if((ShmSysConfigAndInfo->SysInfo.InputVoltageR > SPEC_OV) &&
+				(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_VOLTAGE))
+			{
+				if(Alarm_Counter[gun_index].OV[0] > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_VOLTAGE;
+						DEBUG_INFO("ALARM_OVER_VOLTAGE : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].OV[0]++;
+				}
+			}
+			else if((ShmSysConfigAndInfo->SysInfo.InputVoltageR < (SPEC_OV-HYSTERETIC_OUV)) &&
+					(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_VOLTAGE)))
+			{
+				Alarm_Counter[gun_index].OV[0] = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_VOLTAGE;
+					DEBUG_INFO("ALARM_OVER_VOLTAGE : recover \r\n");
+				}
+			}
+
+			if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
+			{
+				if((ShmSysConfigAndInfo->SysInfo.InputVoltageS > SPEC_OV) &&
+				   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_VOLTAGE))
+				{
+					if(Alarm_Counter[gun_index].OV[1] > FILTER_SPEC)
+					{
+						if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == OFF))
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = ON;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_VOLTAGE;
+							DEBUG_INFO("ALARM_OVER_VOLTAGE : alarm \r\n");
+						}
+					}
+					else
+					{
+						Alarm_Counter[gun_index].OV[1]++;
+					}
+				}
+				else if((ShmSysConfigAndInfo->SysInfo.InputVoltageS < (SPEC_OV-HYSTERETIC_OUV)) &&
+						(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_VOLTAGE)))
+				{
+					Alarm_Counter[gun_index].OV[1] = 0;
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == ON))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_VOLTAGE;
+						DEBUG_INFO("ALARM_OVER_VOLTAGE : recover \r\n");
+					}
+				}
+
+				if((ShmSysConfigAndInfo->SysInfo.InputVoltageT > SPEC_OV) &&
+				   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_VOLTAGE))
+				{
+					if(Alarm_Counter[gun_index].OV[2] > FILTER_SPEC)
+					{
+						if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == OFF))
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = ON;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_VOLTAGE;
+							DEBUG_INFO("ALARM_OVER_VOLTAGE : alarm \r\n");
+						}
+					}
+					else
+					{
+						Alarm_Counter[gun_index].OV[2]++;
+					}
+				}
+				else if((ShmSysConfigAndInfo->SysInfo.InputVoltageT < (SPEC_OV-HYSTERETIC_OUV)) &&
+						(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_VOLTAGE)))
+				{
+					Alarm_Counter[gun_index].OV[2] = 0;
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == ON))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_VOLTAGE;
+						DEBUG_INFO("ALARM_OVER_VOLTAGE : recover \r\n");
+					}
+				}
+			}
+
+			//=====================================
+			// Under voltage detection
+			//=====================================
+			if((ShmSysConfigAndInfo->SysInfo.InputVoltageR  < SPEC_UV) &&
+				(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_UNDER_VOLTAGE))
+			{
+				if(Alarm_Counter[gun_index].UV[0] > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_UNDER_VOLTAGE;
+						DEBUG_INFO("ALARM_UNDER_VOLTAGE : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].UV[0]++;
+				}
+			}
+			else if((ShmSysConfigAndInfo->SysInfo.InputVoltageR  > (SPEC_UV+HYSTERETIC_OUV)) &&
+					(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_UNDER_VOLTAGE)))
+			{
+				Alarm_Counter[gun_index].UV[0] = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_UNDER_VOLTAGE;
+					DEBUG_INFO("ALARM_UNDER_VOLTAGE : recover \r\n");
+				}
+			}
+
+			if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
+			{
+				if((ShmSysConfigAndInfo->SysInfo.InputVoltageS < SPEC_UV) &&
+				   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_UNDER_VOLTAGE))
+				{
+					if(Alarm_Counter[gun_index].UV[1] > FILTER_SPEC)
+					{
+						if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == OFF))
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = ON;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_UNDER_VOLTAGE;
+							DEBUG_INFO("ALARM_UNDER_VOLTAGE : alarm \r\n");
+						}
+					}
+					else
+					{
+						Alarm_Counter[gun_index].UV[1]++;
+					}
+				}
+				else if((ShmSysConfigAndInfo->SysInfo.InputVoltageS > (SPEC_UV+HYSTERETIC_OUV)) &&
+						(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_UNDER_VOLTAGE)))
+				{
+					Alarm_Counter[gun_index].UV[1] = 0;
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == ON))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_UNDER_VOLTAGE;
+						DEBUG_INFO("ALARM_UNDER_VOLTAGE : recover \r\n");
+					}
+				}
+
+				if((ShmSysConfigAndInfo->SysInfo.InputVoltageT < SPEC_UV) &&
+				   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_UNDER_VOLTAGE))
+				{
+					if(Alarm_Counter[gun_index].UV[2] > FILTER_SPEC)
+					{
+						if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == OFF))
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = ON;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_UNDER_VOLTAGE;
+							DEBUG_INFO("ALARM_UNDER_VOLTAGE : alarm \r\n");
+						}
+					}
+					else
+					{
+						Alarm_Counter[gun_index].UV[2]++;
+					}
+				}
+				else if((ShmSysConfigAndInfo->SysInfo.InputVoltageT > (SPEC_OV+HYSTERETIC_OUV)) &&
+						(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_UNDER_VOLTAGE)))
+				{
+					Alarm_Counter[gun_index].UV[2] = 0;
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == ON))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_UNDER_VOLTAGE;
+						DEBUG_INFO("ALARM_UNDER_VOLTAGE : recover \r\n");
+					}
+				}
+			}
+
+			//=====================================
+			// Over current detection
+			//=====================================
+			if((ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0] > SPEC_OC) &&
+			   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_CURRENT))
+			{
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == OFF))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = ON;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_CURRENT;
+					DEBUG_INFO("ALARM_OVER_CURRENT : alarm \r\n");
+				}
+			}
+			else if ((ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0] < (SPEC_OC-HYSTERETIC_OC)) &&
+					 (!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_CURRENT)))
+			{
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_CURRENT;
+					DEBUG_INFO("ALARM_OVER_CURRENT : recover \r\n");
+				}
+			}
+
+			if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
+			{
+				if((ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0] > SPEC_OC) &&
+				   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_CURRENT))
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_CURRENT;
+						DEBUG_INFO("ALARM_OVER_CURRENT : alarm \r\n");
+					}
+				}
+				else if ((ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0] < (SPEC_OC-HYSTERETIC_OC)) &&
+						 (!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_CURRENT)))
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_CURRENT;
+						DEBUG_INFO("ALARM_OVER_CURRENT : recover \r\n");
+					}
+				}
+
+				if((ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0] > SPEC_OC) &&
+				   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_CURRENT))
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_CURRENT;
+						DEBUG_INFO("ALARM_OVER_CURRENT : alarm \r\n");
+					}
+				}
+				else if ((ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0] < (SPEC_OC-HYSTERETIC_OC)) &&
+						 (!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_CURRENT)))
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_CURRENT;
+						DEBUG_INFO("ALARM_OVER_CURRENT : recover \r\n");
+					}
+				}
+			}
+
+			//=====================================
+			// Over temperature detection
+			//=====================================
+			if(//(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp > SPEC_OT) &&
+			   (ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_TEMPERATURE))
+			{
+				if(Alarm_Counter[gun_index].OT_AMB > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_OVER_TEMPERATURE;
+						DEBUG_INFO("ALARM_OVER_TEMPERATURE : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].OT_AMB++;
+				}
+			}
+			else if(//(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp < (SPEC_OT-10)) &&
+					(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_OVER_TEMPERATURE)))
+			{
+				Alarm_Counter[gun_index].OT_AMB = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_OVER_TEMPERATURE;
+					DEBUG_INFO("ALARM_OVER_TEMPERATURE : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Ground fault detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_GROUND_FAIL)
+			{
+				if(Alarm_Counter[gun_index].GMI > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_GROUND_FAIL;
+						DEBUG_INFO("ALARM_GROUND_FAIL : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].GMI++;
+				}
+			}
+			else if (!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_GROUND_FAIL))
+			{
+				Alarm_Counter[gun_index].GMI = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == ON ))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_GROUND_FAIL;
+					DEBUG_INFO("ALARM_GROUND_FAIL : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// CP level fail detection
+			//====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CP_ERROR)
+			{
+				if(Alarm_Counter[gun_index].CP_LevelFail > FILTER_SPEC)
+				{
+					if(ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault == OFF)
+					{
+						ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_CP_ERROR;
+						DEBUG_INFO("ALARM_CP_ERROR : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].CP_LevelFail++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CP_ERROR))
+			{
+				Alarm_Counter[gun_index].CP_LevelFail= 0;
+				if(ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault == ON)
+				{
+					ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_CP_ERROR;
+					DEBUG_INFO("ALARM_CP_ERROR : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Current AC leak detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CURRENT_LEAK_AC)
+			{
+				if(Alarm_Counter[gun_index].Ac_Leak > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_CURRENT_LEAK_AC;
+						DEBUG_INFO("ALARM_CURRENT_LEAK_AC : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].Ac_Leak++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CURRENT_LEAK_AC))
+			{
+				Alarm_Counter[gun_index].Ac_Leak = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_CURRENT_LEAK_AC;
+					DEBUG_INFO("ALARM_CURRENT_LEAK_AC : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Current DC leak detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CURRENT_LEAK_DC)
+			{
+				if(Alarm_Counter[gun_index].Dc_Leak > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_CURRENT_LEAK_DC;
+						DEBUG_INFO("ALARM_CURRENT_LEAK_DC : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].Dc_Leak++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CURRENT_LEAK_DC))
+			{
+				Alarm_Counter[gun_index].Dc_Leak = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_CURRENT_LEAK_DC;
+					DEBUG_INFO("ALARM_CURRENT_LEAK_DC : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// MCU self test fail detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_MCU_TESTFAIL)
+			{
+				if(Alarm_Counter[gun_index].MCU_SelfTestFail > FILTER_SPEC)
+				{
+					if(ShmCharger->gun_info[gun_index].otherAlarmCode.isMcuSelfTest == OFF)
+					{
+						ShmCharger->gun_info[gun_index].otherAlarmCode.isMcuSelfTest = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_MCU_TESTFAIL;
+						DEBUG_INFO("ALARM_MCU_TESTFAIL : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].MCU_SelfTestFail++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_MCU_TESTFAIL))
+			{
+				Alarm_Counter[gun_index].MCU_SelfTestFail = 0;
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isMcuSelfTest == ON)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isMcuSelfTest = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_MCU_TESTFAIL;
+					DEBUG_INFO("ALARM_MCU_TESTFAIL : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Hand shaking timeout detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_HANDSHAKE_TIMEOUT)
+			{
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isHandshakingTimeOut == OFF)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isHandshakingTimeOut  = ON;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_HANDSHAKE_TIMEOUT;
+					DEBUG_INFO("ALARM_HANDSHAKE_TIMEOUT : alarm \r\n");
+				}
+
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_HANDSHAKE_TIMEOUT))
+			{
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isHandshakingTimeOut  == ON)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isHandshakingTimeOut  = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_HANDSHAKE_TIMEOUT;
+					DEBUG_INFO("ALARM_HANDSHAKE_TIMEOUT : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Emergency stop detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_EMERGENCY_STOP)
+			{
+				if(Alarm_Counter[gun_index].EmrgencyBTN > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == OFF))
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_EMERGENCY_STOP;
+						DEBUG_INFO("ALARM_EMERGENCY_STOP : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].EmrgencyBTN++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_EMERGENCY_STOP))
+			{
+				Alarm_Counter[gun_index].EmrgencyBTN = 0;
+				if((ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == ON))
+				{
+					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_EMERGENCY_STOP;
+					DEBUG_INFO("ALARM_EMERGENCY_STOP : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Relay welding detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_RELAY_STATUS)
+			{
+				if(Alarm_Counter[gun_index].Relay_Welding > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding == OFF))
+					{
+						ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_RELAY_STATUS;
+						DEBUG_INFO("ALARM_RELAY_STATUS : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].Relay_Welding++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_RELAY_STATUS))
+			{
+				Alarm_Counter[gun_index].Relay_Welding = 0;
+				if((ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding == ON))
+				{
+					ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_RELAY_STATUS;
+					DEBUG_INFO("ALARM_RELAY_STATUS : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Relay driving fault detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_RELAY_DRIVE_FAULT)
+			{
+				if(Alarm_Counter[gun_index].Relay_DrivingFault > FILTER_SPEC)
+				{
+					if((ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault == OFF))
+					{
+						ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_RELAY_DRIVE_FAULT;
+						DEBUG_INFO("ALARM_RELAY_DRIVE_FAULT : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].Relay_DrivingFault++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_RELAY_DRIVE_FAULT))
+			{
+				Alarm_Counter[gun_index].Relay_DrivingFault = 0;
+				if((ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault == ON))
+				{
+					ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_RELAY_DRIVE_FAULT;
+					DEBUG_INFO("ALARM_RELAY_DRIVE_FAULT : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Current short detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CIRCUIT_SHORT)
+			{
+				if(Alarm_Counter[gun_index].Short > FILTER_SPEC)
+				{
+					if(ShmCharger->gun_info[gun_index].otherAlarmCode.isCurrentShort == OFF)
+					{
+						ShmCharger->gun_info[gun_index].otherAlarmCode.isCurrentShort = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_CIRCUIT_SHORT;
+						DEBUG_INFO("ALARM_CIRCUIT_SHORT : alarm \r\n");
+					}
+				}
+				else
+				{
+					Alarm_Counter[gun_index].Short++;
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_CIRCUIT_SHORT))
+			{
+				Alarm_Counter[gun_index].Short = 0;
+
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isCurrentShort == ON)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isCurrentShort = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_CIRCUIT_SHORT;
+					DEBUG_INFO("ALARM_CIRCUIT_SHORT : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Rotatory switch detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_ROTATORY_SWITCH_FAULT)
+			{
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isShutter == OFF)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isShutter = ON;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_ROTATORY_SWITCH_FAULT;
+					DEBUG_INFO("ALARM_ROTATORY_SWITCH_FAULT : alarm \r\n");
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_ROTATORY_SWITCH_FAULT))
+			{
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isShutter == ON)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isShutter = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_ROTATORY_SWITCH_FAULT;
+					DEBUG_INFO("ALARM_ROTATORY_SWITCH_FAULT : recover \r\n");
+				}
+			}
+
+			//=====================================
+			// Leakage module detection
+			//=====================================
+			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_LEAK_MODULE_FAIL)
+			{
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isLeakageModule == OFF)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isLeakageModule = ON;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_LEAK_MODULE_FAIL;
+
+					DEBUG_INFO("ALARM_LEAK_MODULE_FAIL : alarm \r\n");
+				}
+			}
+			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_LEAK_MODULE_FAIL))
+			{
+				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isLeakageModule == ON)
+				{
+					ShmCharger->gun_info[gun_index].otherAlarmCode.isLeakageModule = OFF;
+					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_LEAK_MODULE_FAIL;
+
+					DEBUG_INFO("ALARM_LEAK_MODULE_FAIL : recover \r\n");
+				}
+			}
+//
+//			//=====================================
+//			// Shutter detection
+//			//=====================================
+//			if(ShmCsuInputInfo->AcChargerInfoData[gun_index].InputAlarmCode & ALARM_SHUTTER_FAULT)
+//			{
+//				if(ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.SHUTTER_FAULT == OFF)
+//				{
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.SHUTTER_FAULT = ON;
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].SystemAlarmCode |= ALARM_SHUTTER_FAULT;
+//
+//					DEBUG_INFO("ALARM_SHUTTER_FAULT : alarm \r\n");
+//				}
+//			}
+//			else if(!(ShmCsuInputInfo->AcChargerInfoData[gun_index].InputAlarmCode & ALARM_SHUTTER_FAULT))
+//			{
+//				if(ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.SHUTTER_FAULT == ON)
+//				{
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.SHUTTER_FAULT = OFF;
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].SystemAlarmCode &= ~ALARM_SHUTTER_FAULT;
+//
+//					DEBUG_INFO("ALARM_SHUTTER_FAULT : recover \r\n");
+//				}
+//			}
+//
+//			//=====================================
+//			// Locker detection
+//			//=====================================
+//			if(ShmCsuInputInfo->AcChargerInfoData[gun_index].InputAlarmCode & ALARM_LOCKER_FAULT)
+//			{
+//				if(ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.LOCKER_FAULT == OFF)
+//				{
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.LOCKER_FAULT = ON;
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].SystemAlarmCode |= ALARM_LOCKER_FAULT;
+//
+//					DEBUG_INFO("ALARM_LOCKER_FAULT : alarm \r\n");
+//				}
+//			}
+//			else if(!(ShmCsuInputInfo->AcChargerInfoData[gun_index].InputAlarmCode & ALARM_LOCKER_FAULT))
+//			{
+//				if(ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.LOCKER_FAULT == ON)
+//				{
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].AlarmCode.bits.LOCKER_FAULT = OFF;
+//					ShmCsuInputInfo->AcChargerInfoData[gun_index].SystemAlarmCode &= ~ALARM_LOCKER_FAULT;
+//
+//					DEBUG_INFO("ALARM_LOCKER_FAULT : recover \r\n");
+//				}
+//			}
+//
+//			//=====================================
+//			// Power drop detection
+//			//=====================================
+//			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_POWER_DROP)
+//			{
+//				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isPowerDrop == OFF)
+//				{
+//					ShmCharger->gun_info[gun_index].otherAlarmCode.isPowerDrop = ON;
+//					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_POWER_DROP;
+//
+//					DEBUG_INFO("ALARM_POWER_DROP : alarm \r\n");
+//				}
+//			}
+//			else if(!(ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode & ALARM_POWER_DROP))
+//			{
+//				if(ShmCharger->gun_info[gun_index].otherAlarmCode.isPowerDrop == ON)
+//				{
+//					ShmCharger->gun_info[gun_index].otherAlarmCode.isPowerDrop = OFF;
+//					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_POWER_DROP;
+//
+//					DEBUG_INFO("ALARM_POWER_DROP : recover \r\n");
+//				}
+//			}
+//
+
+
+
+			//=====================================
+			// Latch alarm recover in state A
+			//=====================================
+			if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == 1))
+			{
+				/*
+				  TODO: Recover latch alarm here
+				*/
+			}
+
+			//=====================================
+			// Latch alarm recover in state B1 and B2
+			//=====================================
+			if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == 2) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == 3))
+			{
+				/*
+				TODO: Recover latch alarm here
+				*/
+			}
+
+			//=====================================
+			// Latch alarm recover in state C
+			//=====================================
+			if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == 4))
+			{
+				/*
+				  TODO: Recover latch alarm here
+				*/
+			}
+		}
+
+		//sleep(1);
+	}
+
+	return FAIL;
+}

+ 13 - 0
EVSE/Projects/AW-Regular/Apps/Module_AlarmDetect.h

@@ -0,0 +1,13 @@
+/*
+ * Module_AlarmDetect.h
+ *
+ *  Created on: 2019¦~8¤ë17¤é
+ *      Author: foluswen
+ */
+
+#ifndef MODULE_ALARMDETECT_H_
+#define MODULE_ALARMDETECT_H_
+
+
+
+#endif /* MODULE_ALARMDETECT_H_ */

+ 342 - 0
EVSE/Projects/AW-Regular/Apps/Module_EventLogging.c

@@ -0,0 +1,342 @@
+#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	"define.h"
+
+#define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define EVENT_INFO(format, args...) StoreEventLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+
+
+#define Debug
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				-1
+
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+
+void trim(char *s);
+int mystrcmp(char *p1,char *p2);
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
+void split(char **arr, char *str, const char *del);
+
+
+#ifdef SystemLogMessage
+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_%s_%s_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,
+			ShmSysConfigAndInfo->SysConfig.ModelName,
+			ShmSysConfigAndInfo->SysConfig.SerialNumber);
+#ifdef SystemLogMessage
+	system(Buf);
+#endif
+
+	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
+
+	return rc;
+}
+#endif
+
+
+int StoreEventLogMsg(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/EventLog/[%04d.%02d]EventLog",
+			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);
+	#ifdef Debug
+	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
+	#endif
+
+	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 trim(char *s)
+{
+    int i=0, j, k, l=0;
+
+    while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
+        i++;
+
+    j = strlen(s)-1;
+    while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
+        j--;
+
+    if(i==0 && j==strlen(s)-1) { }
+    else if(i==0) s[j+1] = '\0';
+    else {
+        for(k=i; k<=j; k++) s[l++] = s[k];
+        s[l] = '\0';
+    }
+}
+
+int mystrcmp(char *p1,char *p2)
+{
+    while(*p1==*p2)
+    {
+        if(*p1=='\0' || *p2=='\0')
+            break;
+        p1++;
+        p2++;
+    }
+    if(*p1=='\0' && *p2=='\0')
+        return(PASS);
+    else
+        return(FAIL);
+}
+
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
+{
+	strncpy(dest, src + start, cnt);
+	dest[cnt] = 0;
+}
+
+void split(char **arr, char *str, const char *del)
+{
+	char *s = strtok(str, del);
+
+	while(s != NULL)
+	{
+		*arr++ = s;
+		s = strtok(NULL, del);
+	}
+}
+
+
+//==========================================
+// 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;
+   	 }
+    else
+    {}
+
+   	//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;
+   	}
+    else
+    {}
+
+   	//creat ShmStatusCodeData
+	if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmget ShmStatusCodeData NG");
+		#endif
+		result = FAIL;
+	}
+	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmat ShmStatusCodeData NG");
+		#endif
+		result = FAIL;
+	}
+	else
+	{}
+	memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
+
+    return result;
+}
+
+//================================================
+// Main process
+//================================================
+int main(void)
+{
+	int ByteCount,BitCount;
+	unsigned char tmp, EventCodeTmp[7];
+
+	if(InitShareMemory() == FAIL)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("InitShareMemory NG\n");
+		#endif
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		sleep(5);
+		return 0;
+	}
+
+	for(;;)
+	{
+		//check Fault Status
+		for(ByteCount=0;ByteCount<4;ByteCount++)
+		{
+			if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
+			{
+				tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
+				for(BitCount=0;BitCount<8;BitCount++)
+				{
+					if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
+					{
+						memset(EventCodeTmp,0,sizeof(EventCodeTmp));
+						memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
+						if(((tmp>>BitCount)&0x01)==0)//Recovered
+						{
+							EventCodeTmp[0]=1;
+							ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]&=~(1<<BitCount);
+						}
+						else
+							ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]|=(1<<BitCount);
+						EVENT_INFO("%s\n", EventCodeTmp);
+					}
+				}
+			}
+		}
+
+		//check Alarm Status
+		for(ByteCount=0;ByteCount<8;ByteCount++)
+		{
+			if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
+			{
+				tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
+				for(BitCount=0;BitCount<8;BitCount++)
+				{
+					if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
+					{
+						memset(EventCodeTmp,0,sizeof(EventCodeTmp));
+						memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
+						if(((tmp>>BitCount)&0x01)==0)//Recovered
+						{
+							EventCodeTmp[0]=1;
+							ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]&=(0<<BitCount);
+						}
+						else
+							ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]|=(1<<BitCount);
+						EVENT_INFO("%s\n", EventCodeTmp);
+					}
+				}
+			}
+		}
+
+		//check Info Status
+		for(ByteCount=0;ByteCount<8;ByteCount++)
+		{
+			if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
+			{
+				tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
+				for(BitCount=0;BitCount<8;BitCount++)
+				{
+					if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
+					{
+						memset(EventCodeTmp,0,sizeof(EventCodeTmp));
+						memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
+						if(((tmp>>BitCount)&0x01)==0)//Recovered
+						{
+							EventCodeTmp[0]=1;
+							ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]&=(0<<BitCount);
+						}
+						else
+							ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]|=(1<<BitCount);
+						EVENT_INFO("%s\n", EventCodeTmp);
+					}
+				}
+			}
+		}
+	}
+
+	return FAIL;
+}

+ 356 - 0
EVSE/Projects/AW-Regular/Apps/Module_FactoryConfig.c

@@ -0,0 +1,356 @@
+#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	"define.h"
+
+#define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+#define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
+
+#define Debug
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				-1
+
+struct SysConfigData 			SysConfig;
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+struct FanModuleData			*ShmFanModuleData;
+void trim(char *s);
+int mystrcmp(char *p1,char *p2);
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
+void split(char **arr, char *str, const char *del);
+
+
+
+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_%s_%s_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,
+			SysConfig.ModelName,
+			SysConfig.SerialNumber);
+#ifdef SystemLogMessage
+	system(Buf);
+#endif
+
+	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
+
+	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 trim(char *s)
+{
+    int i=0, j, k, l=0;
+
+    while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
+        i++;
+
+    j = strlen(s)-1;
+    while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
+        j--;
+
+    if(i==0 && j==strlen(s)-1) { }
+    else if(i==0) s[j+1] = '\0';
+    else {
+        for(k=i; k<=j; k++) s[l++] = s[k];
+        s[l] = '\0';
+    }
+}
+
+int mystrcmp(char *p1,char *p2)
+{
+    while(*p1==*p2)
+    {
+        if(*p1=='\0' || *p2=='\0')
+            break;
+        p1++;
+        p2++;
+    }
+    if(*p1=='\0' && *p2=='\0')
+        return(PASS);
+    else
+        return(FAIL);
+}
+
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
+{
+	strncpy(dest, src + start, cnt);
+	dest[cnt] = 0;
+}
+
+void split(char **arr, char *str, const char *del)
+{
+	char *s = strtok(str, del);
+
+	while(s != NULL)
+	{
+		*arr++ = s;
+		s = strtok(NULL, del);
+	}
+}
+
+
+//==========================================
+// Init all share memory
+//==========================================
+int InitShareMemory()
+{
+	int result = PASS;
+	int MeterSMId;
+
+	//creat ShmSysConfigAndInfo
+	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0)
+    {
+		DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
+
+		result = FAIL;
+	}
+    else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+    {
+    	DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
+
+    	result = FAIL;
+   	 }
+    else
+    {}
+
+   	 //creat ShmStatusCodeData
+   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0)
+    {
+   		DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
+
+   		result = FAIL;
+	}
+    else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+    {
+    	DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
+
+    	result = FAIL;
+   	}
+    else
+    {}
+
+    return result;
+}
+
+//================================================
+// Main process
+//================================================
+int main(void)
+{
+
+	unsigned int i,Chk;
+	unsigned char *ptr;
+	int fd,wrd;
+
+	ptr=malloc(sizeof(struct SysConfigData));
+	if(ptr==NULL)
+	{
+		DEBUG_ERROR("malloc for SysConfigData NG\r\n");
+
+		return 0;
+	}
+	memset(ptr,0,sizeof(struct SysConfigData));
+	memset(&SysConfig,0,sizeof(struct SysConfigData));
+
+	/*
+	 * TODO: Set factory default configuration
+	 */
+	// System configuration
+	time_t t = time(NULL);
+	struct tm tm = *localtime(&t);
+	strcpy((char*)SysConfig.ModelName, "AWLU770100W1P0");
+	strcpy((char*)SysConfig.SerialNumber, "D195200001A0");
+	sprintf((char*)SysConfig.SystemId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
+	sprintf((char*)SysConfig.SystemDateTime, "%d-%d-%d %d:%d:%d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+	SysConfig.AuthorisationMode = 0;	// 0:PH card	1: OCPP backend		2: PH backend	3: Free Mode
+	SysConfig.DefaultLanguage  = 0;		// 0:English	1:Big5				2: GB			3: JN		4: Français 	5: Italiano		6: Español		7: Deutsch		8: Nederland	9: Norsk	10: Suomalainen		11: Svenska		12: Pусский		13: ไทย
+	SysConfig.RfidCardNumEndian = 0;	// 0: Little endian		1: Big endian
+
+	// Charging configuration
+	SysConfig.MaxChargingEnergy = 0;	// 0: No limit	Other: 1~65536 KWH
+	SysConfig.MaxChargingPower = 0;		// 0: No limit	Other: 1~65536 KW
+	SysConfig.MaxChargingCurrent = 0;	// 0: Rating value	Other: 1~Rating A
+	SysConfig.MaxChargingDuration = 0;	// 0: No limit	Other: 1~65536 Minute
+	SysConfig.PhaseLossPolicy = 0;		// 0: Charging	1: Stop charging
+	SysConfig.AcPhaseCount = 1;			// 1: One phase	3: Three phase
+
+	// Network configuration
+	strcpy((char*)SysConfig.FtpServer, "ftp://ipc_ui:pht2016@ftp.phihong.com.tw/");
+	SysConfig.Eth0Interface.EthDhcpClient = 0;
+	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.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 = 2;		// 0: Disable	1: Infrastructure client	2: Infrastructure server	3: Ad-Hoc
+	SysConfig.AthInterface.WifiRssi = 0;		// Wifi rssi value
+	SysConfig.AthInterface.WifiDhcpServer = 0;	// 0: Enable	1: Disable
+	SysConfig.AthInterface.WifiDhcpClient = 0;	// 0: Enable	1: Disable
+
+	strcpy((char*)SysConfig.TelecomInterface.TelcomApn, "internet");
+	SysConfig.TelecomInterface.TelcomSimStatus = 0;	// SIM card status
+	SysConfig.TelecomInterface.TelcomModemMode = 0;	//0: No services	1: CDMA		2: GSM/GPRS	3: WCDMA	4: GSM/WCDMA	5: TD_SCDMA		6: Unknown
+
+	// Backend configuration
+	SysConfig.BackendConnTimeout=300; 		// 300 seconds
+	SysConfig.OfflinePolicy = 0;			// 0: Local list	1: PH RFID		2: Free		3: Deny
+	SysConfig.OfflineMaxChargeEnergy = 0;	// 0: Same as MaxChargeEnergy	Other: 1~65535KWH
+	SysConfig.OfflineMaxChargeDuration = 0; // 0: Same as MaxChargeDuration Other: 1~65535 minutes
+	strcpy((char*)SysConfig.OcppServerURL, "ws://evsocket.phihong.com.tw/");
+	sprintf((char*)SysConfig.ChargeBoxId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
+
+
+	// Copy default configuration to pointer
+	memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
+
+	// Calculate CRC
+	Chk=0;
+	for(i=0;i<(sizeof(struct SysConfigData)-4);i++)
+	{
+		Chk+=*(ptr+i);
+	}
+	SysConfig.Checksum=Chk;
+
+	// Save factory default setting value to file
+	fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR|O_CREAT);
+	if (fd < 0)
+	{
+
+		DEBUG_ERROR("open /mnt/FactoryDefaultConfig.bin NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
+	close(fd);
+	if(wrd!=(sizeof(struct SysConfigData)))
+	{
+		DEBUG_ERROR("write /mnt/FactoryDefaultConfig.bin NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+
+	// Save factory default setting value to flash factory default setting block
+	fd = open("/dev/mtdblock12", O_RDWR);
+	if (fd < 0)
+	{
+
+		DEBUG_ERROR("open /dev/mtdblock12 NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
+	close(fd);
+	if(wrd!=(sizeof(struct SysConfigData)))
+	{
+		DEBUG_ERROR("write /dev/mtdblock12 NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+
+	// Save factory default setting value to flash backup setting block
+	fd = open("/dev/mtdblock11", O_RDWR);
+	if (fd < 0)
+	{
+		DEBUG_ERROR("open /dev/mtdblock11 NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
+	close(fd);
+	if(wrd!=(sizeof(struct SysConfigData)))
+	{
+		DEBUG_ERROR("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)
+	{
+		DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+	wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
+	close(fd);
+	if(wrd!=(sizeof(struct SysConfigData)))
+	{
+		DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
+
+		free(ptr);
+		return 0;
+	}
+
+	free(ptr);
+
+	DEBUG_INFO("FactoryConfig OK\r\n");
+
+
+	return FAIL;
+}

+ 2050 - 0
EVSE/Projects/AW-Regular/Apps/Module_InternalComm.c

@@ -0,0 +1,2050 @@
+#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	"main.h"
+#include	"Module_InternalComm.h"
+
+#define FAIL_SPEC_COMM		1000
+#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
+#define PASS				1
+#define FAIL				0
+#define ON					1
+#define OFF					0
+
+struct SysConfigAndInfo			*ShmSysConfigAndInfo;
+struct StatusCodeData 			*ShmStatusCodeData;
+struct PrimaryMcuData			*ShmPrimaryMcuData;
+struct OCPP16Data 				*ShmOCPP16Data;
+struct Charger					*ShmCharger;
+
+void trim(char *s);
+int mystrcmp(char *p1,char *p2);
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
+void split(char **arr, char *str, const char *del);
+
+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);
+
+#ifdef SystemLogMessage
+	system(Buf);
+#endif
+
+	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
+
+	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 trim(char *s)
+{
+    int i=0, j, k, l=0;
+
+    while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
+        i++;
+
+    j = strlen(s)-1;
+    while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
+        j--;
+
+    if(i==0 && j==strlen(s)-1) { }
+    else if(i==0) s[j+1] = '\0';
+    else {
+        for(k=i; k<=j; k++) s[l++] = s[k];
+        s[l] = '\0';
+    }
+}
+
+int mystrcmp(char *p1,char *p2)
+{
+    while(*p1==*p2)
+    {
+        if(*p1=='\0' || *p2=='\0')
+            break;
+        p1++;
+        p2++;
+    }
+    if(*p1=='\0' && *p2=='\0')
+        return(PASS);
+    else
+        return(FAIL);
+}
+
+void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
+{
+	strncpy(dest, src + start, cnt);
+	dest[cnt] = 0;
+}
+
+void split(char **arr, char *str, const char *del)
+{
+	char *s = strtok(str, del);
+
+	while(s != NULL)
+	{
+		*arr++ = s;
+		s = strtok(NULL, del);
+	}
+}
+
+//==========================================
+// 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;
+	}
+
+	//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;
+	}
+
+	//Initial ShmPrimaryMcuKey
+	if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmget ShmPrimaryMcuData NG\r\n");
+		#endif
+		result = FAIL;
+	}
+	else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmat ShmPrimaryMcuData NG\r\n");
+		#endif
+		result = FAIL;
+	}
+
+	//Initial ShmOcppModuleKey
+	if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmget ShmChargerKey NG\r\n");
+		#endif
+		result = FAIL;
+	}
+	else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("shmat ShmChargerKey NG\r\n");
+		#endif
+		result = FAIL;
+	}
+
+	return result;
+}
+
+int InitComPort()
+{
+	int fd;
+	struct termios tios;
+
+	fd = open("/dev/ttyS1", O_RDWR);
+	if(fd<=0)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("open /dev/ttyS1 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)5;		// timeout 0.5 secod
+	tios.c_lflag=0;
+	tcflush(fd, TCIFLUSH);
+	ioctl (fd, TCSETS, &tios);
+
+	return fd;
+}
+
+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(300000);
+		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]))
+		{
+			strncpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
+			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]))
+		{
+			strncpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
+			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))/10.0;
+			Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8))/10.0;
+			if((rx[4] | rx[5]<<8) > 4)
+			{
+				Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8))/10.0;
+				Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8))/10.0;
+			}
+			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);
+
+	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)>>1);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<(rx[4] | rx[5]<<8);idx++)
+				Ret_Buf->point[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);
+
+	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_connector=0;idx_connector<(rx[4] | rx[5]<<8);idx_connector++)
+			{
+				for(int idx=0;idx<8;idx++)
+					Ret_Buf->relay_status[idx_connector][idx] = (rx[6+idx_connector]>>idx) & 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);
+
+	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_connector=0;idx_connector<((rx[4] | rx[5]<<8)>>2);idx_connector++)
+			{
+				Ret_Buf->adc_value_positive[idx_connector] = (rx[6+(4*idx_connector)] | rx[6+(4*idx_connector)+1]<<8);
+				Ret_Buf->adc_value_negative[idx_connector] = (rx[6+(4*idx_connector)+2] | rx[6+(4*idx_connector)+3]<<8);;
+			}
+
+			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_INPUT, 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->Button[0] 			= (rx[6] >> 6) & 0x01;
+			Ret_Buf->Button[1] 			= (rx[6] >> 7) & 0x01;
+			Ret_Buf->Button_Emergency	= (rx[7] >> 0) & 0x01;
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Alarm_Log(unsigned char fd, unsigned char targetAddr, Alarm_Log *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[10] = {0xaa, 0x00, targetAddr, CMD_QUERY_ALARM_LOG, 0x03, 0x00, Ret_Buf->logArea, Ret_Buf->alarmIndex&0xff, ((Ret_Buf->alarmIndex>>8)&0xff), 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = Ret_Buf->logArea ^ (Ret_Buf->alarmIndex&0xff) ^ ((Ret_Buf->alarmIndex>>8)&0xff);
+	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->log[0], &rx[8], 8);
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RTC, 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->year = ((rx[6]-'0')*1000) + ((rx[7]-'0')*100) + ((rx[8]-'0')*10) + ((rx[9]-'0')*1);
+			Ret_Buf->month = ((rx[10]-'0')*10) + ((rx[11]-'0')*1);
+			Ret_Buf->day = ((rx[12]-'0')*10) + ((rx[13]-'0')*1);
+			Ret_Buf->hour = ((rx[14]-'0')*10) + ((rx[15]-'0')*1);
+			Ret_Buf->min = ((rx[16]-'0')*10) + ((rx[17]-'0')*1);
+			Ret_Buf->sec = ((rx[18]-'0')*10) + ((rx[19]-'0')*1);
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_AC_MCU_Status(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 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->cp_state = rx[6];
+			Ret_Buf->current_limit = rx[7] | (rx[8]<<0x08);
+			Ret_Buf->cp_voltage_positive = (rx[9] | (rx[10]<<0x08))/100.0;
+			Ret_Buf->cp_voltage_negtive = (rx[11] | (rx[12]<<0x08))/100.0;
+			Ret_Buf->locker_state = rx[13];
+			Ret_Buf->relay_state = rx[14];
+			Ret_Buf->shutter_state = rx[15];
+			Ret_Buf->meter_state = rx[16];
+			Ret_Buf->pp_state = rx[17];
+			Ret_Buf->rating_current = rx[18];
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_AC_MCU_Alarm(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Alarm *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM, 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]))
+		{
+			//rx[6]
+			Ret_Buf->bits.OVP = (((rx[6]>>0)&0x01)?1:0);
+			Ret_Buf->bits.UVP = (((rx[6]>>1)&0x01)?1:0);
+			Ret_Buf->bits.OCP = (((rx[6]>>2)&0x01)?1:0);
+			Ret_Buf->bits.OTP = (((rx[6]>>3)&0x01)?1:0);
+			Ret_Buf->bits.gmi_fault = (((rx[6]>>4)&0x01)?1:0);
+			Ret_Buf->bits.cp_fault = (((rx[6]>>5)&0x01)?1:0);
+			Ret_Buf->bits.ac_leak = (((rx[6]>>6)&0x01)?1:0);
+			Ret_Buf->bits.dc_leak = (((rx[6]>>7)&0x01)?1:0);
+
+			//rx[7]
+			Ret_Buf->bits.mcu_selftest_fail = (((rx[7]>>0)&0x01)?1:0);
+			Ret_Buf->bits.handshaking_timeout = (((rx[7]>>1)&0x01)?1:0);
+			Ret_Buf->bits.emergency_stop = (((rx[7]>>2)&0x01)?1:0);
+			Ret_Buf->bits.relay_welding = (((rx[7]>>3)&0x01)?1:0);
+			Ret_Buf->bits.leak_module_fail = (((rx[7]>>4)&0x01)?1:0);
+			Ret_Buf->bits.shutter_fault = (((rx[7]>>5)&0x01)?1:0);
+			Ret_Buf->bits.locker_fault = (((rx[7]>>6)&0x01)?1:0);
+			Ret_Buf->bits.power_drop = (((rx[7]>>7)&0x01)?1:0);
+
+			//rx[8] 3~7bits reserved
+			Ret_Buf->bits.circuit_short = (((rx[8]>>0)&0x01)?1:0);
+			Ret_Buf->bits.set_circuit = (((rx[8]>>1)&0x01)?1:0);
+			Ret_Buf->bits.relay_drive_fault = (((rx[8]>>2)&0x01)?1:0);
+
+			//rx[9] 0~7bits reserved
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Present_OutputCurrent(unsigned char fd, unsigned char targetAddr, Presentoutputcurrent *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTCURRENT, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 0)
+	{
+		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->L1N_L12[0] = (rx[6] | (rx[7]<<8))/10.0;
+			Ret_Buf->L2N_L23[0] = (rx[8] | (rx[9]<<8))/10.0;
+			Ret_Buf->L3N_L31[0] = (rx[10] | (rx[11]<<8))/10.0;
+			Ret_Buf->L1N_L12[1] = (rx[12] | (rx[13]<<8))/10.0;
+			Ret_Buf->L2N_L23[1] = (rx[14] | (rx[15]<<8))/10.0;
+			Ret_Buf->L3N_L31[1] = (rx[16] | (rx[17]<<8))/10.0;
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Ble_Config(unsigned char fd, unsigned char targetAddr, Ble_Config_Data *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CONFIG_DATA, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 0)
+	{
+		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->isLogin = (rx[6]?0x01:0x00);
+			Ret_Buf->isRequestStart = (rx[7]?0x01:0x00);
+			Ret_Buf->isRequestStop = (rx[8]?0x01:0x00);
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Ble_Central_ID(unsigned char fd, unsigned char targetAddr, Ble_Login_Central_Id *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CENTRAL_ID, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 0)
+	{
+		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]))
+		{
+			memset(Ret_Buf->id, 0x00, ARRAY_SIZE(Ret_Buf->id));
+			memcpy(&Ret_Buf->id[0], &rx[6], (rx[4] | (rx[5]<<8)));
+
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr, Power_Consumption *Ret_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_POWER_CONSUMPTION, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 0)
+	{
+		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-> power_consumption = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
+			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]) &&
+		   (rx[6] == 0x01))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_Serial_Number(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[27] = {0xaa, 0x00, targetAddr, CMD_CONFIG_SERIAL_NUMBER, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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[14], &Set_Buf->serial_number[0], ARRAY_SIZE(Set_Buf->serial_number));
+
+	for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[26] = chksum;
+
+	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
+
+	if(len > 6)
+	{
+		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_Model_Name(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
+{
+	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], &Set_Buf->model_name[0], ARRAY_SIZE(Set_Buf->model_name));
+
+	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)
+	{
+		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_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x02, 0x00, 0x00, 0x00, 0x00};
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	for(int idx_connector=0;idx_connector<2;idx_connector++)
+		for(int idx = 0;idx<8;idx++)
+			tx[6+idx_connector] |= ((Set_Buf->relay_status[idx_connector][idx]?0x01:0x00)<<idx);
+
+	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] == 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_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[21];
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+
+	tx[0] = 0xaa;
+	tx[1] = 0x00;
+	tx[2] = targetAddr;
+	tx[3] = CMD_CONFIG_RTC;
+	tx[4] = 0x0e;
+	tx[5] = 0x00;
+	tx[6] = ((Set_Buf->year)/1000)+'0';
+	tx[7] = ((Set_Buf->year)/100%10)+'0';
+	tx[8] = ((Set_Buf->year)/10%10)+'0';
+	tx[9] = (Set_Buf->year)%1000+'0';
+	tx[10] = ((Set_Buf->month)/10)+'0';
+	tx[11] = ((Set_Buf->month)%10)+'0';
+	tx[12] = ((Set_Buf->day)/10)+'0';
+	tx[13] = ((Set_Buf->day)%10)+'0';
+	tx[14] = ((Set_Buf->hour)/10)+'0';
+	tx[15] = ((Set_Buf->hour)%10)+'0';
+	tx[16] = ((Set_Buf->min)/10)+'0';
+	tx[17] = ((Set_Buf->min)%10)+'0';
+	tx[18] = ((Set_Buf->sec)/10)+'0';
+	tx[19] = ((Set_Buf->sec)%10)+'0';
+
+	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] == 0x01))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_AC_MCU_LED(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Led *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[12] ;
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	tx[0] = 0xaa;
+	tx[1] = 0x00;
+	tx[2] = targetAddr;
+	tx[3] = CMD_CONFIG_AC_LED;
+	tx[4] = 0x05;
+	tx[5] = 0x00;
+	tx[6] = Set_Buf->mode;
+	tx[7] = ((Set_Buf->alarm_code>>0)&0xff);
+	tx[8] = ((Set_Buf->alarm_code>>8)&0xff);
+	tx[9] = ((Set_Buf->alarm_code>>16)&0xff);
+	tx[10] = ((Set_Buf->alarm_code>>24)&0xff);
+
+	for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
+		chksum ^= tx[6+idx];
+	tx[11] = 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] == 0x01))
+		{
+			result = PASS;
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_AC_MCU_LEGACY_REQUEST(unsigned char fd, unsigned char targetAddr, Legacy_Request *Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[9];
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	tx[0] = 0xaa;
+	tx[1] = 0x00;
+	tx[2] = targetAddr;
+	tx[3] = CMD_CONFIG_MCU_MODE;
+	tx[4] = 0x02;
+	tx[5] = 0x00;
+	tx[6] = Set_Buf->isLegacyRequest;
+	tx[7] = 0x00;
+
+	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] == 0x01))
+		{
+			result = PASS;
+
+		}
+	}
+
+	return result;
+}
+
+unsigned char Config_AC_MaxCurrent_And_CpPwmDuty(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Cp_Pwm_Duty*Set_Buf)
+{
+	unsigned char result = FAIL;
+	unsigned char tx[9];
+	unsigned char rx[512];
+	unsigned char chksum = 0x00;
+
+	tx[0] = 0xaa;
+	tx[1] = 0x00;
+	tx[2] = targetAddr;
+	tx[3] = CMD_CONFIG_CURRENT_LINIT;
+	tx[4] = 0x05;
+	tx[5] = 0x00;
+	tx[6] = Set_Buf->max_current;
+
+	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)
+	{
+		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 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_ABOARD, 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_END, 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] == 0x01))
+		{
+			result = PASS;
+		}
+	}
+	return result;
+}
+
+
+//================================================
+// Main process
+//================================================
+int main(void)
+{
+	int Uart1Fd;
+
+	unsigned short int failCount[2] = {0,0};
+
+	if(InitShareMemory() == FAIL)
+	{
+		#ifdef SystemLogMessage
+		DEBUG_ERROR("InitShareMemory NG\n");
+		#endif
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
+		}
+		sleep(5);
+		return 0;
+	}
+	else
+	{
+		DEBUG_INFO("InitShareMemory OK.\r\n");
+	}
+
+	Uart1Fd=InitComPort();
+	if(Uart1Fd<0)
+	{
+		DEBUG_ERROR("InitComPort NG\n");
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
+		}
+		sleep(5);
+		return 0;
+	}
+	else
+	{
+		DEBUG_INFO("ttyS1 port open success.\r\n");
+	}
+
+	for(;;)
+	{
+		for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
+		{
+			//===============================
+			// Query firmware version
+			//===============================
+			if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass != PASS)
+			{
+				if(Query_FW_Ver(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].ver) == PASS)
+				{
+					DEBUG_INFO("MCU-%d get firmware version : %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
+					memcpy(ShmPrimaryMcuData->version, ShmCharger->gun_info[gun_index].ver.Version_FW, sizeof(ShmPrimaryMcuData->version));
+					ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = PASS;
+
+					failCount[gun_index] = 0;
+				}
+				else
+				{
+					DEBUG_WARN("MCU-%d get firmware version fail...%d\r\n", gun_index, failCount[gun_index]);
+					if(failCount[gun_index]<1000)
+					{
+						failCount[gun_index]++;
+					}
+				}
+			}
+
+			//===============================
+			// Config primary MCU serial number
+			//===============================
+			if(ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass != PASS)
+			{
+				memcpy(ShmCharger->evseId.serial_number, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmCharger->evseId.serial_number));
+				if(Config_Serial_Number(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
+				{
+					DEBUG_INFO("MCU-%d set serial number OK...\r\n");
+					ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = PASS;
+
+					failCount[gun_index] = 0;
+				}
+				else
+				{
+					DEBUG_WARN("MCU-%d set serial number fail...%d\r\n", gun_index, failCount[gun_index]);
+					if(failCount[gun_index]<1000)
+					{
+						failCount[gun_index]++;
+					}
+				}
+			}
+
+			//===============================
+			// Config primary MCU model name
+			//===============================
+			if(ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass != PASS)
+			{
+				memcpy(ShmCharger->evseId.model_name, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmCharger->evseId.model_name));
+				if(Config_Model_Name(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
+				{
+					DEBUG_INFO("MCU-%d set model name OK...\r\n");
+					ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = PASS;
+
+					failCount[gun_index] = 0;
+				}
+				else
+				{
+					DEBUG_WARN("MCU-%d set model name fail...%d\r\n", gun_index, failCount[gun_index]);
+					if(failCount[gun_index]<1000)
+					{
+						failCount[gun_index]++;
+					}
+				}
+			}
+
+			//===============================
+			// Query present input voltage
+			//===============================
+			if(Query_Present_InputVoltage(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].inputVoltage) == PASS)
+			{
+				DEBUG_INFO("MCU-%d get input voltage : %f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
+				ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
+				ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
+				ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get input voltage fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Query present output current
+			//===============================
+			if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
+			{
+				DEBUG_INFO("MCU-%d get output current : %f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get output current fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+					failCount[gun_index]++;
+			}
+
+			//===============================
+			// Query temperature
+			//===============================
+			if(Query_Temperature(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].temperature) == PASS)
+			{
+				DEBUG_INFO("MCU-%d get temperature : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].temperature.point[0]);
+				ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get temperature fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Query primary MCU status
+			//===============================
+			if(Query_AC_MCU_Status(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuState))
+			{
+				DEBUG_INFO("MCU-%d get Pilot State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_state);
+				DEBUG_INFO("MCU-%d get Pilot Duty : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
+				DEBUG_INFO("MCU-%d get Pilot Voltage Positive : %.2f\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive);
+				DEBUG_INFO("MCU-%d get Pilot Voltage Negative : %.2f\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive);
+				DEBUG_INFO("MCU-%d get Locker State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.locker_state);
+				DEBUG_INFO("MCU-%d get Relay State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relay_state);
+				DEBUG_INFO("MCU-%d get Shutter State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state);
+				DEBUG_INFO("MCU-%d get Meter State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.meter_state);
+				DEBUG_INFO("MCU-%d get PP State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.pp_state);
+				DEBUG_INFO("MCU-%d get Rating Current : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current);
+
+				ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
+				ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotDuty = (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit>51?(unsigned char)((ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/2.5)+64):(unsigned char)(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/0.6));
+				ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
+				ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
+				ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive;
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get status fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Query primary MCU Alarm code
+			//===============================
+			if(Query_AC_MCU_Alarm(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuAlarm))
+			{
+				DEBUG_INFO("MCU-%d get alarm OK...\r\n");
+				/*
+				// Byte[6]
+				// Byte[7]
+				// Byte[8] Alarm code 3~7 bits Reserved
+				// Byte[9] Alarm code 0~7 bits Reserved
+				 */
+
+				//================================================
+				// Byte[6]
+				//================================================
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<0;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<0);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<1;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<1);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<2;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<2);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<3;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<3);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<4;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<4);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<5;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<5);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<6;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<6);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<7;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<7);
+
+				//================================================
+				// Byte[7]
+				//================================================
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<8;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<8);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<9;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<9);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<10;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<10);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<11;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<11);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<12;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<12);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<13;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<13);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<14;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<14);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<15;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<15);
+
+				//================================================
+				// Byte[8]
+				//================================================
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<16;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<16);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<17;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<17);
+
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault == 0x01)
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<18;
+				else
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<18);
+
+				//================================================
+				// Byte[9]
+				//================================================
+				// Debug print out
+				DEBUG_INFO("MCU-%d get OVP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP);
+				DEBUG_INFO("MCU-%d get UVP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP);
+				DEBUG_INFO("MCU-%d get OCP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP);
+				DEBUG_INFO("MCU-%d get OTP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP);
+				DEBUG_INFO("MCU-%d get gmi_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault);
+				DEBUG_INFO("MCU-%d get cp_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault);
+				DEBUG_INFO("MCU-%d get ac_leak : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak);
+				DEBUG_INFO("MCU-%d get dc_leak : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak);
+				DEBUG_INFO("MCU-%d get mcu_selftest_fail : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail);
+				DEBUG_INFO("MCU-%d get handshaking_timeout : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout);
+				DEBUG_INFO("MCU-%d get emergency_stop : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop);
+				DEBUG_INFO("MCU-%d get relay_welding : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding);
+				DEBUG_INFO("MCU-%d get leak_module_fail : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail);
+				DEBUG_INFO("MCU-%d get shutter_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault);
+				DEBUG_INFO("MCU-%d get locker_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault);
+				DEBUG_INFO("MCU-%d get power_drop : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop);
+				DEBUG_INFO("MCU-%d get circuit_short : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short);
+				DEBUG_INFO("MCU-%d get set_circuit : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit);
+				DEBUG_INFO("MCU-%d get relay_drive_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault);
+				DEBUG_INFO("MCU-%d get InputAlarmCode : %x\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode);
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get alarm fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Query primary MCU BLE config
+			//===============================
+			if(Query_Ble_Config(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleConfigData))
+			{
+				DEBUG_INFO("MCU-%d get isUserLogin : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isLogin);
+				DEBUG_INFO("MCU-%d get isRequestStartCharger : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart);
+				DEBUG_INFO("MCU-%d get isRequestStopCharger : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop);
+
+				ShmSysConfigAndInfo->SysConfig.Bluetooth.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
+				ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
+				ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get ble config fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Query primary MCU ble login id
+			//===============================
+			if(Query_Ble_Central_ID(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleLoginCentralId))
+			{
+				DEBUG_INFO("MCU-%d get ble central id : %s\r\n", gun_index, ShmCharger->gun_info[gun_index].bleLoginCentralId.id);
+				memcpy(ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, sizeof ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID);
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get ble login central id fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Config primary MCU LED
+			//===============================
+			ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode;
+			if(Config_AC_MCU_LED(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuLed))
+			{
+				DEBUG_INFO("MCU-%d set Led mode : %d\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.mode);
+				DEBUG_INFO("MCU-%d set Alarm code : %x\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code);
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d set led fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+				{
+					failCount[gun_index]++;
+				}
+			}
+
+			//===============================
+			// Query RTC
+			//===============================
+			if(Query_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc))
+			{
+				struct timeb csuTime, mcuTime;
+				struct tm *tmCSU;
+				struct tm tmMcu;
+				ftime(&csuTime);
+				tmCSU = localtime(&csuTime.time);
+				tmMcu.tm_year = ShmCharger->gun_info[gun_index].rtc.year-1900;
+				tmMcu.tm_mon = ShmCharger->gun_info[gun_index].rtc.month-1;
+				tmMcu.tm_mday = ShmCharger->gun_info[gun_index].rtc.day;
+				tmMcu.tm_hour = ShmCharger->gun_info[gun_index].rtc.hour;
+				tmMcu.tm_min = ShmCharger->gun_info[gun_index].rtc.min;
+				tmMcu.tm_sec = ShmCharger->gun_info[gun_index].rtc.sec;
+				mcuTime.time = mktime(&tmMcu);
+
+
+				if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
+				{
+					if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
+					{
+						char cmdBuf[128];
+						sprintf(cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", ShmCharger->gun_info[gun_index].rtc.year,
+																						ShmCharger->gun_info[gun_index].rtc.month,
+																						ShmCharger->gun_info[gun_index].rtc.day,
+																						ShmCharger->gun_info[gun_index].rtc.hour,
+																						ShmCharger->gun_info[gun_index].rtc.min,
+																						ShmCharger->gun_info[gun_index].rtc.sec);
+						system(cmdBuf);
+						system("hwclock -w -u");
+						system("hwclock -s");
+
+						DEBUG_INFO("Sync from MCU-%d rtc OK...%04d-%02d-%02d %02d:%02d:%02d\r\n", gun_index,
+																								  ShmCharger->gun_info[gun_index].rtc.year,
+																								  ShmCharger->gun_info[gun_index].rtc.month,
+																								  ShmCharger->gun_info[gun_index].rtc.day,
+																								  ShmCharger->gun_info[gun_index].rtc.hour,
+																								  ShmCharger->gun_info[gun_index].rtc.min,
+																								  ShmCharger->gun_info[gun_index].rtc.sec);
+					}
+				}
+				else
+				{
+					if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
+					{
+						ShmCharger->gun_info[gun_index].rtc.year = tmCSU->tm_year+1900;
+						ShmCharger->gun_info[gun_index].rtc.month = tmCSU->tm_mon+1;
+						ShmCharger->gun_info[gun_index].rtc.day = tmCSU->tm_mday;
+						ShmCharger->gun_info[gun_index].rtc.hour = tmCSU->tm_hour;
+						ShmCharger->gun_info[gun_index].rtc.min = tmCSU->tm_hour;
+						ShmCharger->gun_info[gun_index].rtc.sec = tmCSU->tm_sec;
+						Config_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc);
+
+						DEBUG_INFO("MCU-%d set rtc OK...%04d-%02d-%02d %02d:%02d:%02d\r\n", gun_index,
+																							ShmCharger->gun_info[gun_index].rtc.year,
+																							ShmCharger->gun_info[gun_index].rtc.month,
+																							ShmCharger->gun_info[gun_index].rtc.day,
+																							ShmCharger->gun_info[gun_index].rtc.hour,
+																							ShmCharger->gun_info[gun_index].rtc.min,
+																							ShmCharger->gun_info[gun_index].rtc.sec);
+					}
+				}
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get rtc fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+					failCount[gun_index]++;
+			}
+
+			//===============================
+			// Query primary MCU power consumption
+			//===============================
+			if(Query_Power_Consumption(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].powerConsumption))
+			{
+				DEBUG_INFO("MCU-%d get power consumption : %f\r\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0));
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d get power consumption fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+					failCount[gun_index]++;
+			}
+
+			//===============================
+			// Config primary Legacy request
+			//===============================
+			if(Config_AC_MCU_LEGACY_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].legacyRequest))
+			{
+				DEBUG_INFO("MCU-%d set relay request : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
+				DEBUG_INFO("========================================\n");
+
+				failCount[gun_index] = 0;
+			}
+			else
+			{
+				DEBUG_WARN("MCU-%d set relay request fail...%d\r\n", gun_index, failCount[gun_index]);
+				if(failCount[gun_index]<1000)
+					failCount[gun_index]++;
+			}
+
+			//===============================
+			// Upgrade MCU
+			//===============================
+			if(ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq)
+			{
+				unsigned char cmd[512];
+				if(Upgrade_UART(Uart1Fd, AC_WALLMOUNT_CONTROLLER, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName))
+				{
+					DEBUG_INFO("MCU-%d upgrade firmware OK...%s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
+					sleep(20);
+					ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = OFF;
+					failCount[gun_index] = 0;
+				}
+				else
+				{
+					DEBUG_WARN("MCU-%d upgrade firmware fail...%d\r\n", gun_index, failCount[gun_index]);
+					if(failCount[gun_index]<1000)
+						failCount[gun_index]++;
+				}
+
+				sprintf((char*)cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
+				system((char*)cmd);
+			}
+
+			//===============================
+			// Config primary set CP PWN duty
+			//===============================
+//			ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 32;
+//			if(Config_AC_MaxCurrent_And_CpPwmDuty(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty))
+//			{
+//				DEBUG_INFO("MCU-%d cp pwn duty : %d\r\n",gun_index, &ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
+//			}
+//			else
+//			{
+//				DEBUG_WARN("MCU-%d set cp pwn duty fail...%d\r\n", gun_index, failCount[gun_index]);
+//				if(failCount[gun_index]<1000)
+//				{
+//					failCount[gun_index]++;
+//				}
+//			}
+//
+//			if(failCount[gun_index]>=1000)
+//			{
+//				DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
+//			}
+
+
+			//===============================
+			// Communication fail check
+			//===============================
+			if(failCount[gun_index] >= FAIL_SPEC_COMM)
+			{
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == OFF)
+				{
+					DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = ON;
+				}
+			}
+			else
+			{
+				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == ON)
+				{
+					DEBUG_ERROR("Primary MCU-%d communication recover", gun_index);
+					ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = OFF;
+				}
+			}
+		}
+		sleep(3);
+	}
+
+	return FAIL;
+}

+ 65 - 0
EVSE/Projects/AW-Regular/Apps/Module_InternalComm.h

@@ -0,0 +1,65 @@
+/*
+ * Module_InternalComm.h
+ *
+ *  Created on: 2019¦~8¤ë28¤é
+ *      Author: USER
+ */
+
+#ifndef MODULE_INTERNALCOMM_H_
+#define MODULE_INTERNALCOMM_H_
+
+#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)
+
+extern int StoreLogMsg(const char *fmt, ...);
+
+enum MESSAGE_COMMAND
+{
+	CMD_QUERY_FW_VER = 0x01,
+	CMD_QUERY_HW_VER = 0x02,
+	CMD_QUERY_PRESENT_INPUTVOLTAGE = 0x03,
+	CMD_QUERY_PRESENT_OUTPUTVOLTAGE = 0x04,
+	CMD_QUERY_FAN_SPEED = 0x05,
+	CMD_QUERY_TEMPERATURE = 0x06,
+	CMD_QUERY_AUX_POWERVOLTAGE = 0x07,
+	CMD_QUERY_RELAY_OUTPUT = 0x08,
+	CMD_QUERY_GFD_ADC = 0x09,
+	CMD_QUERY_GPIO_INPUT = 0x0a,
+	CMD_QUERY_ALARM_LOG = 0x22,
+	CMD_QUERY_RTC = 0x26,
+	CMD_QUERY_PRESENT_OUTPUTCURRENT = 0x27,
+	CMD_QUERY_AC_STATUS = 0x28,
+	CMD_QUERY_AC_ALARM = 0x29,
+	CMD_QUERY_BLE_CONFIG_DATA = 0x2A,
+	CMD_QUERY_BLE_CENTRAL_ID = 0x2B,
+	CMD_QUERY_POWER_CONSUMPTION = 0x2C,
+
+	CMD_CONFIG_FAN_SPEED = 0x81,
+	CMD_CONFIG_SERIAL_NUMBER = 0x82,
+	CMD_CONFIG_MODEL_NAME = 0x83,
+	CMD_CONFIG_RELAY_OUTPUT = 0x85,
+	CMD_CONFIG_GPIO_OUTPUT = 0x86,
+	CMD_CONFIG_RTC = 0x87,
+	CMD_CONFIG_AC_LED = 0x88,
+	CMD_CONFIG_CURRENT_LINIT = 0x89,
+	CMD_CONFIG_MCU_MODE = 0x8A,
+
+	CMD_UPDATE_START = 0xe0,
+	CMD_UPDATE_ABOARD = 0xe1,
+	CMD_UPDATE_TRANSFER = 0xe2,
+	CMD_UPDATE_END = 0xe3
+};
+
+enum MESSAGE_ADDRESS
+{
+	ADDR_AUX = 0x01,
+	ADDR_FAN = 0x02,
+	ADDR_RELAY = 0x03,
+	ADDR_DC_PRIMARY = 0x04,
+	ADDR_AC_PRIMARY_1 = 0xff,
+	ADDR_AC_PRIMARY_2 = 0xff
+};
+
+
+#endif /* MODULE_INTERNALCOMM_H_ */

二進制
EVSE/Projects/AW-Regular/Apps/Ocpp16


+ 0 - 333
EVSE/Projects/AW-Regular/Apps/Ocpp16.c

@@ -1,333 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <libwebsockets.h>
-#include <lws_config.h>
-#include "../../define.h"
-
-#define Debug
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct OCPP16Data 			*ShmOCPP16Data;
-
-
-struct lws *wsi;
-struct lws_context *context;
-
-unsigned char *SendBuffer;
-int SendBufLen=(1024*3);
-int ConnectionEstablished=0;
-
-
-#ifdef SystemLogMessage
-int StoreLogMsg(unsigned char *DataString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	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,
-			DataString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",DataString);
-	#endif
-}		
-#endif
-
-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;
-}	
-
-/**************************************************************************************/
-/**************************Init all share memory *********************************/
-/**************************************************************************************/
-int InitShareMemory()
-{
-	int MeterSMId;
-	//creat ShmSysConfigAndInfo
-	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]InitShareMemory:shmget ShmSysConfigAndInfo NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]InitShareMemory:shmat ShmSysConfigAndInfo NG");
-		#endif		
-		return 0;
-   	 }
-   	 //creat ShmStatusCodeData
-   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]InitShareMemory:shmget ShmStatusCodeData NG");
-		#endif		
-		return 0;	
-	}
-    	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]InitShareMemory:shmat ShmStatusCodeData NG");
-		#endif		
-		return 0;
-   	 }
-   	 //creat ShmOCPP16Data
-   	 if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]InitShareMemory:shmget ShmOCPP16Data NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]InitShareMemory:shmat ShmOCPP16Data NG");
-		#endif		
-		return 0;
-   	 }
-   	memset(ShmOCPP16Data,0,sizeof(struct PsuData));
-    	return 1;
-}
-
-
-int SendData(struct lws *wsi) 
-{
-    int n;
-    int len;
-    unsigned char *out = NULL;
-
-    len = strlen(SendBuffer);
-    out = (char *)malloc(sizeof(char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));
-    memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
-   
-    n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
-   
-    free(out);
-    return n;
-}
-
-static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
-{	
-
-	#ifdef Debug
-	printf("OCPP16Callback:reason=%d\n",reason);
-	#endif
-	switch (reason) 
-	{
-		case LWS_CALLBACK_CLIENT_ESTABLISHED://3
-			#ifdef SystemLogMessage	
-			StoreLogMsg("[Ocpp16]OCPP16Callback:LWS_CALLBACK_CLIENT_ESTABLISHED");
-			#endif		
-			//connected
-			ConnectionEstablished=1;
-			break;
-		case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
-			#ifdef Debug
-			printf("OCPP16Callback:LWS_CALLBACK_CLIENT_CONNECTION_ERROR:%s\n",in);
-			#endif
-			#ifdef SystemLogMessage	
-			StoreLogMsg("[Ocpp16]OCPP16Callback:LWS_CALLBACK_CLIENT_CONNECTION_ERROR");
-			#endif		
-			//disconnected
-			ConnectionEstablished=0;
-			break;
-		case LWS_CALLBACK_CLOSED://4
-			#ifdef SystemLogMessage	
-			StoreLogMsg("[Ocpp16]OCPP16Callback:LWS_CALLBACK_CLOSED");
-			#endif
-			//disconnected
-			ConnectionEstablished=0;
-			break;
-		case LWS_CALLBACK_CLIENT_WRITEABLE://10
-			//sif(need to send message and its relevant data already store into SendBuffer)
-				SendData(wsi);
-			break;
-		case LWS_CALLBACK_CLIENT_RECEIVE://8
-			((char *)in)[len] = '\0';
-			#ifdef Debug
-			printf("OCPP16Callback:LWS_CALLBACK_CLIENT_RECEIVE : rx %d '%s'\n", (int)len, (char *)in);
-			#endif
-			//parsing received message and do something
-			break;
-		default:
-			break;
-	}
-
-	return 0;
-}
-
-static struct lws_protocols protocols[] = {
-
-	{
-		"ocpp1.6",		
-		OCPP16Callback,
-		10240,	
-		10240,
-	},
-	{
-		"ocpp1.6",		
-		OCPP16Callback,
-		10240,	
-		10240,
-	},
-	{
-		NULL, NULL, 0		/* End of list */
-	}
-};
-
-int ConnectWsServer()
-{	
-	struct lws_context_creation_info ContextInfo;
-	struct lws_client_connect_info ConnInfo;
-	int use_ssl=0;
-	
-	if(context!=NULL)
-		lws_context_destroy(context);
-	memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
-	ContextInfo.port=CONTEXT_PORT_NO_LISTEN;
-	ContextInfo.iface=NULL;
-	ContextInfo.ssl_private_key_password=NULL; 
-	ContextInfo.ssl_cert_filepath=NULL; 
-	ContextInfo.ssl_private_key_filepath=NULL; 
-	ContextInfo.ssl_ca_filepath="/root/cacert.pem"; 
-	ContextInfo.ssl_cipher_list=NULL; //use default one
-	ContextInfo.gid=-1;
-	ContextInfo.uid=-1;
-	//if(security connection)
-	//	ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
-	ContextInfo.protocols=protocols;
-	ContextInfo.timeout_secs= 30;
-	//if(ping pong enabled)
-		//ContextInfo.ws_ping_pong_interval= 15;//0 for none, else interval in seconds 
-	ContextInfo.ws_ping_pong_interval= 0;	
-	context = lws_create_context(&ContextInfo);
-	if (context == NULL) 
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]ConnectWsServer: lws_create_context NG");
-		#endif	
-		return 0;
-	}
-	memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
-	ConnInfo.context = context;
-
-	// fill up below information
-	//ConnInfo.address=ServerIpAddress;
-	//ConnInfo.port = ServerSrcPort;
-	//ConnInfo.path=ServerPath;
-	//ConnInfo.host=ServerHost;
-	//ConnInfo.origin=ServerOrigin;
-
-	/* ws://192.168.1.1:9091/greenlots/ocpp*/
-	ConnInfo.address = "192.168.1.1";
-	ConnInfo.port = 9091;
-	ConnInfo.path="/ocpp/T123456789";
-	ConnInfo.host = "192.168.1.1:9091";
-	ConnInfo.origin = "192.168.1.1:9091";	
-
-	use_ssl = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
-	//if(security connection)
-	//	ConnInfo.ssl_connection = use_ssl;
-	//else
-		ConnInfo.ssl_connection=0;
-
-	ConnInfo.protocol = protocols[1].name;
-	ConnInfo.ietf_version_or_minus_one=-1;
-	wsi=lws_client_connect_via_info(&ConnInfo);						 
-	if (!wsi) 
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]ConnectWsServer: lws_client_connect_via_info NG");
-		#endif	
-		return 0;
-	}
-	return 1;
-}
-
-int main(int argc,char *argv[])
-{
-	unsigned int StartTime=0;
-	/**************** Initialization **********/		
-	/*if(InitShareMemory()==0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]main:InitShareMemory NG");
-		#endif		
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}	
-		sleep(5);
-		return 0;
-	}*/	
-  	if((SendBuffer=malloc(SendBufLen))==NULL)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[Ocpp16]main: malloc(SendBufLen) NG");
-		#endif	
-		sleep(5);
-		return 0;	
-	}	
-	while(ConnectionEstablished==0)
-	{
-		if((time((time_t*)NULL)-StartTime)>=60)
-		{
-			#ifdef debug
-			printf("[OCPP16:]main:Execute ConnectWsServer1\n");
-			#endif
-			ConnectWsServer();
-			StartTime=time((time_t*)NULL);
-		}	
-		lws_service(context, 10000);//timeout_ms	
-	}
-	memset(SendBuffer,0,SendBufLen);
-	sprintf(SendBuffer,"[2,\"%s\",\"%s\",{\"chargePointVendor\":\"%s\",\"chargePointModel\":\"%s\",\"chargePointSerialNumber\":\"%s\",\"chargeBoxSerialNumber\":\"%s\",\"firmwareVersion\":\"%s\",\"imsi\":\"%s\"}]",
-					"11112222",
-					"BootNotification",
-					"CpVendor",
-					"CpModel",
-					"CpSN",
-					"CbSN",
-					"CpFwVersion",
-					"CpImsi");
-	lws_callback_on_writable(wsi);
-	while(1)
-	{
-		//prcessing
-		lws_service(context, 500);//500 timeout_ms 	
-	};
-	
-
-	free(SendBuffer);
-}

+ 0 - 248
EVSE/Projects/AW-Regular/Apps/PsuComm.c

@@ -1,248 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <math.h>//for pow
-#include <net/if.h>
-#include <linux/can.h>
-#include <linux/can/raw.h>
-#include <unistd.h>
-#include "../../define.h"
-
-//#define Debug
-
-
-struct SysConfigAndInfo			*ShmSysConfigAndInfo;
-struct StatusCodeData 			*ShmStatusCodeData;
-struct PsuData 				*ShmPsuData ;
-pid_t						CANReceiverPid;
-int 							CanFd;
-
-
-#ifdef SystemLogMessage
-int StoreLogMsg(unsigned char *DataString)
-{
-	unsigned char Buf[256];
-	time_t CurrentTime;
-	struct tm *tm;
-			
-	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,
-			DataString,
-			tm->tm_year+1900,tm->tm_mon+1);
-	system(Buf);
-	#ifdef Debug
-	printf("%s \n",DataString);
-	#endif
-}		
-#endif
-
-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;
-}	
-	
-/**************************************************************************************/
-/**************************Init all share memory *********************************/
-/**************************************************************************************/
-int InitShareMemory()
-{
-	int MeterSMId;
-	
-	//creat ShmSysConfigAndInfo
-	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitShareMemory:shmget ShmSysConfigAndInfo NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitShareMemory:shmat ShmSysConfigAndInfo NG");
-		#endif		
-		return 0;
-   	 }
-   	 //creat ShmStatusCodeData
-   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitShareMemory:shmget ShmStatusCodeData NG");
-		#endif		
-		return 0;	
-	}
-    	else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitShareMemory:shmat ShmStatusCodeData NG");
-		#endif		
-		return 0;
-   	 }
-
-   	 //creat ShmPsuData
-   	 if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData),  0777)) < 0) 
-    	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitShareMemory:shmget ShmPsuData NG");
-		#endif			
-		return 0;
-	}
-    	else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1) 
-    	{
-    		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitShareMemory:shmat ShmPsuData NG");
-		#endif		
-		return 0;
-   	 }
-   	memset(ShmPsuData,0,sizeof(struct PsuData));
-    	return 1;
-}
-
-int InitCanBus()
-{
-	int 					s0,nbytes;
-	struct timeval			tv;
-	struct ifreq 			ifr0;
-	struct sockaddr_can	addr0;
-	
-	system("/sbin/ip link set can1 type can bitrate 500000 restart-ms 100");				  
-	system("/sbin/ip link set can1 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	
-		StoreLogMsg("[PsuCOmm]InitCanBus:Set SO_RCVTIMEO NG");
-		#endif		
-    	}
-    	nbytes=40960;
-    	if (setsockopt(s0, SOL_SOCKET,  SO_RCVBUF, &nbytes, sizeof(int)) < 0) 
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitCanBus:Set SO_RCVBUF NG");
-		#endif	
-	}
-    	nbytes=40960;
-    	if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0) 
-    	{
-       		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitCanBus:Set SO_SNDBUF NG");
-		#endif	
-    	}
-    
-   	strcpy(ifr0.ifr_name, "can1" );
-	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;
-}
-
-
-/**************************************************************/
-/************** Receiving Task*******************************/
-/*************************************************************/
-void CANReceiver(int fd)
-{
-	pid_t tmp=0;
-	struct can_frame frame;
-	struct timeb StartTime,EndTime;
-	int nbytes;
-	unsigned char FanspeedGetTime=0;
-	
-	if(CANReceiverPid==0)
-	{
-		tmp=fork();
-		if(tmp>0)
-		{
-			CANReceiverPid=tmp;	
-			{
-				unsigned char buf[64];
-				memset(buf,0,sizeof(buf));
-				sprintf(buf,"renice -20 -p %d",tmp);
-				system(buf);
-			}
-			return;
-		}
-	}
-	
-	while(1)
-	{
-		memset(&frame,0,sizeof(struct can_frame));
-		nbytes = read(fd, &frame, sizeof(struct can_frame));
-		//handle received packet
-	}
-}
-
-/**************************************************************/
-/************** main function***********************************/
-/*************************************************************/
-int main(int argc,char *argv[])
-{
-	int CanFd;
-	struct can_frame frame;
-
-	
-	//Initialization
-	if(InitShareMemory()==0)
-	{
-		#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]main:InitShareMemory NG");
-		#endif		
-		if(ShmStatusCodeData!=NULL)
-		{
-			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
-		}	
-		sleep(5);
-		return 0;
-	}	
-	CanFd=InitCanBus();
-	CANReceiverPid=0;
-	CANReceiver(CanFd);
-
-	//main loop
-	while(1)
-	{
-		//processing
-	}
-	
-EndProcess:	
-	if(CANReceiverPid>0)
-	{
-		char Buf[32];
-		memset(Buf,0,32);
-		sprintf(Buf,"kill %d",CANReceiverPid);
-		system(Buf);
-	}
-	close(CanFd); 
-	system("/sbin/ip link set can1 down");
-	system("/sbin/ip link set can1 type can bitrate 500000 restart-ms 100");
-	system("/sbin/ip link set can1 up");
-	system("/sbin/ip link set can1 down");
-	system("killall PsuComm");
-}

+ 0 - 69
EVSE/Projects/AW-Regular/Apps/UpdateRootfs.c

@@ -1,69 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <linux/termios.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdlib.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/sockios.h> 
-#include <linux/socket.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h> 
-#include <sys/timeb.h> 
-#include <math.h>//for pow
-#include "../../define.h"
-
-
-int main(int argc,char *argv[])
-{
-	unsigned int MaxLen=25*1024*1024, ImageLen=0;
-	unsigned char *ptr; 
-	int fd,wrd;
-	
-	ptr=malloc(MaxLen);
-	if(ptr==NULL)
-	{
-		printf("UpdateRootfs NG - can not malloc\n");
-		return 0;
-	}	
-	memset(ptr,0xFF,MaxLen);
-	
-	fd = open(argv[1], O_RDONLY);
-	if(fd < 0)
-	{
-		printf("UpdateRootfs NG - can not open rootfs\n");
-		free(ptr);
-		return 0;
-	}
-    	ImageLen=read(fd,ptr,MaxLen);
-	close(fd);	
-	printf("ImageLen=0x%x\n",ImageLen);
-	if(ImageLen<(24*1024*1024))
-	{
-		printf("ImageLen size mismatch\n");
-		free(ptr);
-		return 0;
-	}	
-	fd = open("/dev/mtdblock8", O_RDWR);
-	if (fd < 0) 
-	{
-		printf("UpdateRootfs NG - can not open mtdblock8\n");
-		free(ptr);
-		return 0;
-    	}
-    	wrd=write(fd, ptr, ImageLen);   
-    	close(fd);
-   	if(wrd!=ImageLen)
-	{	
-		printf("UpdateRootfs NG - wrd(0x%x) != ImageLen(0x%x)\n",wrd,ImageLen);
-		free(ptr);
-		return 0;
-	}	
-	free(ptr);
-	printf("UpdateRootfs OK\n");
-}

二進制
EVSE/Projects/AW-Regular/Apps/ccs


+ 0 - 59
EVSE/Projects/AW-Regular/Apps/ccs.c

@@ -1,59 +0,0 @@
-// raw_sock.c
-#include<stdio.h>
-#include<stdlib.h>
-#include<string.h>
-#include<netinet/ip.h>
-#include<sys/socket.h>
-#include<arpa/inet.h>
-#include <unistd.h>
-
-int main() {
-    // Structs that contain source IP addresses
-    struct sockaddr_in source_socket_address, dest_socket_address;
-
-    int packet_size;
-	struct timeval			tv;
-    // Allocate string buffer to hold incoming packet data
-    unsigned char *buffer = (unsigned char *)malloc(65536);
-    // Open the raw socket
-    int sock = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
-    tv.tv_sec = 0; 
-    	tv.tv_usec = 10000; 
-   	 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct	timeval)) < 0) 
-    	{
-	       	#ifdef SystemLogMessage	
-		StoreLogMsg("[PsuCOmm]InitCanBus:Set SO_RCVTIMEO NG");
-		#endif		
-    	}
-    if(sock == -1)
-    {
-        //socket creation failed, may be because of non-root privileges
-        printf("Failed to create socket\n");
-        return 1;
-    }
-    while(1) {
-      // recvfrom is used to read data from a socket
-      packet_size = recvfrom(sock , buffer , 65536 , 0 , NULL, NULL);
-      if (packet_size == -1) 
-      {
-		printf("Failed to get packets\n");
-		sleep(5);
-        	continue;
-      }
- 	printf("get packets, packet_size=%d\n",packet_size);
-      struct iphdr *ip_packet = (struct iphdr *)buffer;
-
-      memset(&source_socket_address, 0, sizeof(source_socket_address));
-      source_socket_address.sin_addr.s_addr = ip_packet->saddr;
-      memset(&dest_socket_address, 0, sizeof(dest_socket_address));
-      dest_socket_address.sin_addr.s_addr = ip_packet->daddr;
-
-      printf("Incoming Packet: \n");
-      printf("Packet Size (bytes): %d\n",ntohs(ip_packet->tot_len));
-      printf("Source Address: %s\n", (char *)inet_ntoa(source_socket_address.sin_addr));
-      printf("Destination Address: %s\n", (char *)inet_ntoa(dest_socket_address.sin_addr));
-      printf("Identification: %d\n\n", ntohs(ip_packet->id));
-    }
-
-    return 0;
-}

二進制
EVSE/Projects/AW-Regular/Apps/main


文件差異過大導致無法顯示
+ 524 - 390
EVSE/Projects/AW-Regular/Apps/main.c


+ 393 - 0
EVSE/Projects/AW-Regular/Apps/main.h

@@ -0,0 +1,393 @@
+/*
+ * Config.h
+ *
+ *  Created on: 2019年12月25日
+ *      Author: EasonYang
+ */
+
+#ifndef CONFIG_MAIN_H_
+#define CONFIG_MAIN_H_
+
+#include    <sys/types.h>
+#include    <sys/stat.h>
+#include 	<sys/time.h>
+#include 	<sys/timeb.h>
+#include 	<sys/types.h>
+#include 	<sys/ioctl.h>
+#include 	<sys/socket.h>
+#include 	<sys/ipc.h>
+#include 	<sys/shm.h>
+#include 	<sys/mman.h>
+#include 	<linux/wireless.h>
+#include 	<arpa/inet.h>
+#include 	<netinet/in.h>
+#include	<dirent.h>
+
+#include 	<unistd.h>
+#include 	<stdarg.h>
+#include    <stdio.h>      /*標準輸入輸出定義*/
+#include    <stdlib.h>     /*標準函數庫定義*/
+#include    <unistd.h>     /*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	<stdbool.h>
+#include	<stddef.h>
+#include	<stdint.h>
+
+//==========================
+//	Define system mode constant
+//==========================
+#define MODE_BOOTING				0
+#define MODE_IDLE					1
+#define MODE_AUTHORIZING			2
+#define MODE_PREPARING				3
+#define MODE_CHARGING				4
+#define MODE_TERMINATING			5
+#define MODE_ALARM					6
+#define MODE_FAULT					7
+#define MODE_RESERVATION			8
+#define MODE_BOOKING				9
+#define MODE_MAINTAIN				10
+#define MODE_DEBUG					11
+
+//===================================
+//	Define CP State constant
+//===================================
+#define CP_STATE_UNKNOWN			0
+#define CP_STATE_A					1
+#define CP_STATE_B					2
+#define CP_STATE_C					3
+#define CP_STATE_D					4
+#define CP_STATE_E					5
+#define CP_STATE_F					6
+
+//===================================
+// Define start mode constant
+//===================================
+#define START_METHOD_MANUAL 		0
+#define START_METHOD_RFID	 		1
+#define START_METHOD_BACKEND 		2
+#define START_METHOD_BLE	 		3
+
+//===================================
+// Define Speaker type constant
+//===================================
+#define SPEAKER_STOP				0
+#define SPEAKER_ALWAYS_ON			1
+#define SPEAKER_SHORT				2
+#define SPEAKER_LONG				3
+#define SPEAKER_INTERVAL_SHORT		4
+#define SPEAKER_INTERVAL_LONG		5
+#define SPEAKER_INTERVAL_3COUNT		6
+
+//===================================
+// Define Alarm code constant
+//===================================
+#define ALARM_OVER_VOLTAGE          0x000001
+#define ALARM_UNDER_VOLTAGE         0x000002
+#define ALARM_OVER_CURRENT          0x000004
+#define ALARM_OVER_TEMPERATURE      0x000008
+#define ALARM_GROUND_FAIL           0x000010
+#define ALARM_CP_ERROR              0x000020
+#define ALARM_CURRENT_LEAK_AC       0x000040
+#define ALARM_CURRENT_LEAK_DC       0x000080
+#define ALARM_MCU_TESTFAIL          0x000100
+#define ALARM_HANDSHAKE_TIMEOUT     0x000200
+#define ALARM_EMERGENCY_STOP        0x000400
+#define ALARM_RELAY_STATUS          0x000800
+#define ALARM_LEAK_MODULE_FAIL      0x001000
+#define ALARM_SHUTTER_FAULT         0x002000
+#define ALARM_LOCKER_FAULT          0x004000
+#define ALARM_POWER_DROP            0x008000
+#define ALARM_CIRCUIT_SHORT         0x010000
+#define ALARM_ROTATORY_SWITCH_FAULT 0x020000
+#define ALARM_RELAY_DRIVE_FAULT     0x040000
+
+//===================================
+// Define Led constant
+//===================================
+#define LED_ACTION_INIT             	0
+#define LED_ACTION_IDLE             	1
+#define LED_ACTION_AUTHED           	2
+#define LED_ACTION_CONNECTED        	3
+#define LED_ACTION_CHARGING         	4
+#define LED_ACTION_STOP             	5
+#define LED_ACTION_ALARM            	6
+#define LED_ACTION_MAINTAIN         	7
+#define LED_ACTION_RFID_PASS        	8
+#define LED_ACTION_RFID_FAIL        	9
+#define LED_ACTION_BLE_CONNECT      	10
+#define LED_ACTION_BLE_DISABLE      	11
+#define LED_ACTION_DEBUG            	12
+#define LED_ACTION_ALL_OFF          	13
+
+#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)
+
+extern int StoreLogMsg(const char *fmt, ...);
+
+
+//============================================================
+// Private shared memory key define
+//============================================================
+#define ShmChargerKey		2001
+
+typedef struct Verion
+{
+	char Version_FW[32];
+	char Version_HW[32];
+}Ver;
+
+typedef struct PRESENTINPUTVOLTAGE
+{
+	unsigned char inputType;	// 0x00: Line to Line       0x01: Line to Neutral
+	double L1N_L12;
+	double L2N_L23;
+	double L3N_L31;
+}PresentInputVoltage;
+
+typedef struct PRESENTOUTPUTVOLTAGE
+{
+	double behindFuse_Voltage_C1;
+	double behindRelay_Voltage_C1;
+	double behindFuse_Voltage_C2;
+	double behindRelay_Voltage_C2;
+}PresentOutputVoltage;
+
+typedef struct FANSPEED
+{
+	unsigned short int speed[4];
+}FanSpeed;
+
+typedef struct TEMPERATURE
+{
+	unsigned char point[8];
+}Temperature;
+
+typedef struct AUXPOWER
+{
+	unsigned char voltage[8];
+}AuxPower;
+
+typedef struct RELAY
+{
+	unsigned char relay_status[2][8];
+}Relay;
+
+typedef struct GFD
+{
+	unsigned short int adc_value_positive[2];
+	unsigned short int adc_value_negative[2];
+}Gfd;
+
+typedef struct GPIO_IN
+{
+	unsigned char AC_Connector;
+	unsigned char AC_MainBreaker;
+	unsigned char SPD;
+	unsigned char Door_Open;
+	unsigned char GFD[2];
+	unsigned char Button[2];
+	unsigned char Button_Emergency;
+}Gpio_in;
+
+typedef struct GPIO_OUT
+{
+	unsigned char AC_Connector;
+	unsigned char Button_LED[2];
+	unsigned char System_LED[4];
+}Gpio_out;
+
+typedef struct ALARM_LOG
+{
+	unsigned char logArea;
+	unsigned int alarmIndex;
+	unsigned char log[8];
+}Alarm_Log;
+
+typedef struct BLE_CONFIG_DATA
+{
+	unsigned char isLogin:1;
+	unsigned char isRequestStart:1;
+	unsigned char isRequestStop:1;
+}Ble_Config_Data;
+
+typedef struct BLE_LONGIN_CENTRAL_ID
+{
+	unsigned char id[32];
+}Ble_Login_Central_Id;
+
+typedef struct RTC
+{
+	unsigned short int year;
+	unsigned char month;
+	unsigned char day;
+	unsigned char hour;
+	unsigned char min;
+	unsigned char sec;
+}Rtc;
+
+typedef struct PRESENTOUTPUTCURRENT
+{
+	double L1N_L12[2];
+	double L2N_L23[2];
+	double L3N_L31[2];
+}Presentoutputcurrent;
+
+typedef struct AC_PRIMARY_MCU
+{
+	unsigned char cp_state;
+	unsigned int  current_limit;
+	unsigned int  cp_voltage_positive;
+	unsigned int  cp_voltage_negtive;
+	unsigned char locker_state;
+	unsigned char relay_state;
+	unsigned char shutter_state;
+	unsigned char meter_state;
+	unsigned char pp_state;
+	unsigned char rating_current;
+}Ac_Primary_Mcu;
+
+typedef struct AC_PRIMARY_MCU_ALARM
+{
+	union
+	{
+		unsigned long InputAlarmCode;
+		struct
+		{
+			unsigned long OVP:1;
+			unsigned long UVP:1;
+			unsigned long OCP:1;
+			unsigned long OTP:1;
+			unsigned long gmi_fault:1;
+			unsigned long cp_fault:1;
+			unsigned long ac_leak:1;
+			unsigned long dc_leak:1;
+			unsigned long mcu_selftest_fail:1;
+			unsigned long handshaking_timeout:1;
+			unsigned long emergency_stop:1;
+			unsigned long relay_welding:1;
+			unsigned long leak_module_fail:1;
+			unsigned long shutter_fault:1;
+			unsigned long locker_fault:1;
+			unsigned long power_drop:1;
+			unsigned long circuit_short:1;
+			unsigned long set_circuit:1;
+			unsigned long relay_drive_fault:1;
+			unsigned long comm_timeout:1;
+		}bits;
+	};
+}Ac_Primary_Mcu_Alarm;
+
+typedef struct AC_PRIMARY_MCU_LED
+{
+	unsigned char mode;
+	unsigned long alarm_code;
+}Ac_Primary_Mcu_Led;
+
+typedef struct EVSE_ID
+{
+	unsigned char model_name[14];
+	unsigned char serial_number[12];
+}Evse_Id;
+
+typedef struct AC_PRIMARY_MCU_CP_PWM_DUTY
+{
+	unsigned int max_current;
+}Ac_Primary_Mcu_Cp_Pwm_Duty;
+
+typedef struct LEGACY_REQUEST
+{
+	unsigned char isLegacyRequest:1;
+}Legacy_Request;
+
+typedef struct POWER_CONSUMPTION
+{
+	uint32_t power_consumption;
+
+}Power_Consumption;
+
+typedef struct MCU_OP_FLAG
+{
+	unsigned char isSetModePass:1;
+	unsigned char isSetSerialNumberPass:1;
+	unsigned char isSetModelNamePass:1;
+	unsigned char isReadFwVerPass:1;
+	unsigned char isMcuUpgradeReq:1;
+}Mcu_Op_Flag;
+
+typedef struct SYSTEM_ALARM_CODE
+{
+	unsigned long SystemAlarmCode;
+
+}System_Alarm_Code;
+
+typedef struct OTHER_ALARM_CODE
+{
+	unsigned char isMcuSelfTest:1;
+	unsigned char isCurrentShort:1;
+	unsigned char isLeakageModule:1;
+	unsigned char isShutter:1;
+	unsigned char isLocker:1;
+	unsigned char isRotatorySwitchr:1;
+	unsigned char isPowerDrop:1;
+	unsigned char isOverCurrent:1;
+	unsigned char isHandshakingTimeOut:1;
+	unsigned char HandShakingAlarmRequest:1;
+}Other_Alarm_Code;
+
+typedef struct PILOT_VOLTAGE
+{
+	float PilotVoltagePositive;
+	float PilotVoltageNegative;
+
+}Pilot_Voltage;
+
+typedef struct FW_UPGRADE_INFO
+{
+	int fwType;
+	char modelName[17];
+	char location[384];
+}Fw_Upgrade_Info;
+
+typedef struct GUN_INFO
+{
+	Ver 											ver;
+	PresentInputVoltage 							inputVoltage;
+	Presentoutputcurrent							outputCurrent;
+	Temperature 									temperature;
+	Ble_Config_Data									bleConfigData;
+	Ble_Login_Central_Id							bleLoginCentralId;
+	Rtc												rtc;
+	Ac_Primary_Mcu									primaryMcuState;
+	Ac_Primary_Mcu_Alarm							primaryMcuAlarm;
+	Ac_Primary_Mcu_Led								primaryMcuLed;
+	Mcu_Op_Flag										mcuFlag;
+	Power_Consumption								powerConsumption;
+	Legacy_Request									legacyRequest;
+	System_Alarm_Code								systemAlarmCode;
+	Ac_Primary_Mcu_Cp_Pwm_Duty						primaryMcuCp_Pwn_Duty;
+	Other_Alarm_Code								otherAlarmCode;
+	Pilot_Voltage									PilotVoltage;
+}Gun_Info;
+
+struct Charger
+{
+	Ver 					ver;
+	Evse_Id					evseId;
+	Gun_Info 				gun_info[2];
+	Fw_Upgrade_Info			fwUpgradeInfo;
+	unsigned char			rfidReq:1;
+
+	unsigned char 			speaker_type;
+	unsigned char			isSpeakerOn:1;
+};
+
+#endif /* CONFIG_MAIN_H_ */

二進制
EVSE/Projects/AW-Regular/Images/ramdisk.gz


部分文件因文件數量過多而無法顯示