Prechádzať zdrojové kódy

2020-07-20 / Eason Yang
1.Change: main.c
Reason :
a) Fixed : isReachableInternet();
b) Fixed : Remote start should be Remote stop
c) Added : 5 seconds for change pwn duty
d) Fixed : If clean ChargingProfile, current duty should changed to rating current
e) Added : Authorization into Terminating mode
f) Added : LED_ACTION_STOP into Terminating mode
g) Rename : Version
2.Change: main.h
Reason :
a) Added : isChargerStopByCondition flag.
3.Change: Module_InternalComm.c
Reason :
a) Added : Simulation function
Version : B0.51.XX.XXXX.XX

8009 4 rokov pred
rodič
commit
fff0f762b8

+ 23 - 1
EVSE/Projects/AW-Regular/Apps/Module_InternalComm.c

@@ -31,6 +31,8 @@
 #include	"main.h"
 #include	"Module_InternalComm.h"
 
+//#define SIMULATION
+
 #define FAIL_SPEC_COMM		1000
 #define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
 #define PASS				1
@@ -50,6 +52,8 @@ struct OCPP16Data 				*ShmOCPP16Data;
 struct Charger					*ShmCharger;
 
 uint16_t						stepIndex;
+long long						tsLast, tsNow;
+double							tmpPowerConsumption;
 
 void trim(char *s);
 int mystrcmp(char *p1,char *p2);
@@ -111,6 +115,14 @@ int DiffTimeb(struct timeb ST, struct timeb ET)
 	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
 }
 
+long long current_timestamp()
+{
+    struct timeval te;
+    gettimeofday(&te, NULL); // get current time
+    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
+    return milliseconds;
+}
+
 //=================================
 // Common routine
 //=================================
@@ -959,7 +971,14 @@ unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr
 		   (rx[1] == tx[2]) &&
 		   (rx[3] == tx[3]))
 		{
+#ifndef SIMULATION
 			Ret_Buf-> power_consumption = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
+#else
+			tsNow = current_timestamp();
+			tmpPowerConsumption += (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent)*((tsNow-tsLast)/36000000.0);
+			Ret_Buf-> power_consumption = (uint32_t)tmpPowerConsumption;
+			tsLast = tsNow;
+#endif
 			result = PASS;
 		}
 	}
@@ -2178,8 +2197,11 @@ int main(void)
 						//===============================
 						if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
 						{
+#ifndef SIMULATION
 							ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
-
+#else
+							ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0) ;
+#endif
 							failCount[gun_index] = 0;
 						}
 						else

+ 247 - 60
EVSE/Projects/AW-Regular/Apps/main.c

@@ -1,7 +1,6 @@
 #include	"define.h"
 #include 	"main.h"
 
-
 //==========================
 // System basic sample constant
 //==========================
@@ -21,7 +20,7 @@
 #define TMR_IDX_AUTH					1
 #define TMR_IDX_LOGPPRINTOUT			2
 #define TMR_IDX_PROFILE_PREPARE			3
-#define TMR_IDX_4						4
+#define TMR_IDX_PWN_CHANGE				4
 #define TMR_IDX_5						5
 #define TMR_IDX_6						6
 #define TMR_IDX_7						7
@@ -33,6 +32,7 @@
 #define TIMEOUT_SPEC_HANDSHAKING_LED	185000
 #define TIMEOUT_SPEC_LOGPPRINTOUT		30000
 #define TIMEOUT_SPEC_PROFILE_PREPARE	5000
+#define TIMEOUT_SPEC_PWN_CHANGE			5000
 
 #define MtdBlockSize 					0x600000
 
@@ -675,7 +675,6 @@ int LoadSysConfigAndInfo(struct SysConfigData *ptr)
 
 	if((buf=malloc(MtdBlockSize))==NULL)
 	{
-
 		DEBUG_ERROR("malloc buffer NG,rebooting..\r\n");
 
 		if(ShmStatusCodeData!=NULL)
@@ -737,7 +736,6 @@ int LoadSysConfigAndInfo(struct SysConfigData *ptr)
 	//================================================
 	if(ChkSum!=ChkSumOrg)
 	{
-
 		DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\r\n");
 
 		fd = open("/dev/mtdblock11", O_RDWR);
@@ -791,10 +789,9 @@ int LoadSysConfigAndInfo(struct SysConfigData *ptr)
 			fd = open("/dev/mtdblock12", O_RDWR);
 			if (fd < 0)
 			{
-				free(buf);
-
 				DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\r\n");
-
+				
+				free(buf);
 				if(ShmStatusCodeData!=NULL)
 				{
 					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
@@ -809,10 +806,9 @@ int LoadSysConfigAndInfo(struct SysConfigData *ptr)
     		close(fd);
 			if(wrd<MtdBlockSize)
 			{
-				free(buf);
-
 				DEBUG_ERROR("read factory default  SysConfigData data NG,rebooting..\r\n");
-
+				
+				free(buf);
 				if(ShmStatusCodeData!=NULL)
 				{
 					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
@@ -895,8 +891,8 @@ int StoreUsrConfigData(struct SysConfigData *UsrData)
 			}
 			else
 			{
-		    		DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
-		    		result = FAIL;
+		    	DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
+		    	result = FAIL;
 			}
 		}
 		else
@@ -908,8 +904,9 @@ int StoreUsrConfigData(struct SysConfigData *UsrData)
 	else
 	{
 		DEBUG_ERROR("alloc BlockSize NG\r\n");
-    		result = FAIL;
+    	result = FAIL;
 	}
+	
 	if(BufTmp!=NULL)
 		free(BufTmp);
 
@@ -1190,7 +1187,7 @@ void get_firmware_version(unsigned char gun_index)
 	strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
 
 	// Get CSU root file system version
-	sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.50.00.0000.00");
+	sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.51.00.0000.00");
 
 	// Get AC connector type from model name
 	for(uint8_t idx=0;idx<3;idx++)
@@ -1642,33 +1639,40 @@ int isReachableInternet()
 	char cmd[256];
 	char buf[512];
 	//char tmp[512];
-
-	for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
+	
+	if(ShmOCPP16Data->OcppConnStatus == PASS)
+	{
+		result = PASS;
+	}
+	else
 	{
-		sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
-		fp = popen(cmd, "r");
-		if(fp != NULL)
+		for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
 		{
-			while(fgets(buf, sizeof(buf), fp) != NULL)
+			sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
+			fp = popen(cmd, "r");
+			if(fp != NULL)
 			{
-				if(strstr(buf, "transmitted") > 0)
+				while(fgets(buf, sizeof(buf), fp) != NULL)
 				{
-					//sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
-
-					if(strstr(buf,"100%") != NULL)
+					if(strstr(buf, "transmitted") > 0)
 					{
+						//sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
 
+						if(strstr(buf,"100%") != NULL)
+						{
+
+						}
+						else
+						{
+							result = PASS;
+						}
+						//DEBUG_INFO("%s",buf);
+						//DEBUG_INFO("%s\n",tmp);
 					}
-					else
-					{
-						result = PASS;
-					}
-					//DEBUG_INFO("%s",buf);
-					//DEBUG_INFO("%s\n",tmp);
 				}
 			}
+			pclose(fp);
 		}
-		pclose(fp);
 	}
 
 	return result;
@@ -1745,7 +1749,6 @@ int getScheduleStart(int gun_index)
 	struct tm tmScheduleStart;;
 	struct timeb tbScheduleStart;
 
-
 	if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
 	{
 		tmScheduleStart.tm_year -= 1900;
@@ -2380,7 +2383,7 @@ int main(void)
 						{
 							ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
 							memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
-							DEBUG_INFO("Start Method : FREE..\r\n");
+							DEBUG_INFO("Start Method : FREE...\r\n");
 						}
 
 						ShmCharger->gun_info[gun_index].rfidReq = OFF;
@@ -2433,7 +2436,7 @@ int main(void)
 						}
 						ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
 					}
-
+					
 					if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
 					{
 						// Authorization timeout process.
@@ -2537,7 +2540,7 @@ int main(void)
 						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
 						getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
 						ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
-						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
 						memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
 
 						memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
@@ -2554,8 +2557,11 @@ int main(void)
 						ftime(&startChargingTime[gun_index]);
 						ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
 						ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
+						ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
 						startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
 						ShmOCPP16Data->SpMsg.bits.AuthorizeReq = OFF;
+						ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
+						ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
 					}
 
 					if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
@@ -2565,9 +2571,10 @@ int main(void)
 					   (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
 					   (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
 					{
-
 						if(ShmCharger->gun_info[gun_index].rfidReq)
 						{
+							DEBUG_INFO("Certified in charging mode... \r\n");
+							
 							// If RFID SN different with start user, it need to authorize ID
 							if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
 							{
@@ -2616,11 +2623,12 @@ int main(void)
 												break;
 										}
 									}
+									
 									DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
-									DEBUG_INFO("Start method : %d...\r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
+									DEBUG_INFO("Start method : %d\r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
 
 									if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
-									   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
+									   //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
 									   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
 									{
 										switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
@@ -2654,6 +2662,11 @@ int main(void)
 												break;
 										}
 									}
+									else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
+									{
+										DEBUG_INFO("Should be remote stop charger... \r\n");
+										ShmCharger->gun_info[gun_index].rfidReq = OFF;
+									} 
 								}
 								else
 								{
@@ -2695,10 +2708,10 @@ int main(void)
 					else
 					{
 						setLedMotion(gun_index,LED_ACTION_CHARGING);
-						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
 						ftime(&endChargingTime[gun_index]);
 						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
-						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start))/100;
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start)/100.0;
 
 						// Response StartTransactionConf
 						if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
@@ -2721,7 +2734,7 @@ int main(void)
 						}
 
 						// Checking profile id > 0 and current time is between charging profile validFrom & validTo
-						if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
+						if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId > 0) &&
 						   (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON) &&
 						   (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
 						{
@@ -2819,24 +2832,41 @@ int main(void)
 								}
 							}
 						}
-
+						else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
+						{
+							ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
+						}
+						
 						// Determine max charging current to MCU
 						if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
 						{
 							ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
 							if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
 							{
-								ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
-								ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
+								if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
+								{
+									ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
+									ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
+									ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
+								}
+								else
+								{}
 							}
 						}
 						else
 						{
 							ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
+							
 							if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
 							{
-								ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
-								ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
+								if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
+								{
+									ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
+									ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
+									ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
+								}
+								else
+								{}
 							}
 						}
 
@@ -2844,7 +2874,7 @@ int main(void)
 						if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
 						{
 							DEBUG_INFO("=======================================================================\r\n");
-							DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
+							DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
 							DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
 							DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
 							DEBUG_INFO("=======================================================================\r\n");
@@ -2856,16 +2886,21 @@ int main(void)
 							// On-line max condition check
 							if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
 							{
+								setRelay(gun_index, OFF);
+								ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
 								setChargerMode(gun_index, SYS_MODE_TERMINATING);
 								DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
 							}
 							else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
 							{
+								setRelay(gun_index, OFF);
+								ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
 								setChargerMode(gun_index, SYS_MODE_TERMINATING);
 								DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
 							}
 							else
 							{
+								ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
 								setRelay(gun_index, ON);
 							}
 						}
@@ -2877,16 +2912,21 @@ int main(void)
 							{
 								if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
 								{
+									setRelay(gun_index, OFF);
+									ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
 									setChargerMode(gun_index, SYS_MODE_TERMINATING);
 									DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
 								}
 								else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
 								{
+									setRelay(gun_index, OFF);
+									ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
 									setChargerMode(gun_index, SYS_MODE_TERMINATING);
 									DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
 								}
 								else
 								{
+									ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
 									setRelay(gun_index, ON);
 								}
 							}
@@ -2907,14 +2947,8 @@ int main(void)
 							ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
 						}
 
-						if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
-						{
-							setLedMotion(gun_index, LED_ACTION_STOP);
-						}
-						else
-						{}
-
 						getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
+						startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
 					}
 
 					// End authorize pass
@@ -2926,7 +2960,7 @@ int main(void)
 					   (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
 					   (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
 					{
-						if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
+						if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
 						{
 							if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
 							   (ShmCharger->gun_info[gun_index].isAuthPassEnd))
@@ -2936,13 +2970,32 @@ int main(void)
 								setLedMotion(gun_index,LED_ACTION_RFID_PASS);
 								sleep(3);
 							}
+							else
+							{
+								/*
+								// If stop reason is "Bluetooth" / "RemoteStop" / "Reset" / "Unplug" should be set speaker on
+								if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
+									DEBUG_INFO("Bluetooth to stop charging... \r\n");
+								else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)
+									DEBUG_INFO("Remote to stop charging... \r\n");
+								else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0))
+									DEBUG_INFO("Unplug to stop charging... \r\n");
+								else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
+									DEBUG_INFO("Reset request to stop charging... \r\n");
+								else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
+									DEBUG_INFO("Unlock connector request to stop charging... \r\n");
+
+								setSpeaker(ON,SPEAKER_SHORT);
+								*/
+							}
 						}
 						else
 						{}
 
 						setRelay(gun_index, OFF);
 						setLedMotion(gun_index, LED_ACTION_STOP);
-
+						
+						// If relay is off, the system should change to complete mode
 						if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
 						{
 							setChargerMode(gun_index, SYS_MODE_COMPLETE);
@@ -2950,6 +3003,140 @@ int main(void)
 					}
 					else
 					{
+						// If the charger stops charging, the led should be stoped blink
+						if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
+						{
+							setLedMotion(gun_index, LED_ACTION_STOP);
+						}
+						
+						if(ShmCharger->gun_info[gun_index].rfidReq)
+						{
+							DEBUG_INFO("Certified in terminating mode... \r\n");
+							
+							// If RFID SN different with start user, it need to authorize ID
+							if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
+							{
+								if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
+								{
+									// Request authorization
+									ftime(&startTime[gun_index][TMR_IDX_AUTH]);
+									memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
+									if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
+									{
+										// Big endian
+										switch(rfid.snType)
+										{
+											case RFID_SN_TYPE_6BYTE:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
+												break;
+											case RFID_SN_TYPE_7BYTE:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
+												break;
+											case RFID_SN_TYPE_10BYTE:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
+												break;
+											case RFID_SN_TYPE_4BYTE:
+											default:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
+												break;
+										}
+									}
+									else
+									{
+										// Little endian
+										switch(rfid.snType)
+										{
+											case RFID_SN_TYPE_6BYTE:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
+												break;
+											case RFID_SN_TYPE_7BYTE:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
+												break;
+											case RFID_SN_TYPE_10BYTE:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
+												break;
+											case RFID_SN_TYPE_4BYTE:
+											default:
+												sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
+												break;
+										}
+									}
+									
+									DEBUG_INFO("End request User Id : %s... \r\n", ShmSysConfigAndInfo->SysConfig.UserId);
+									DEBUG_INFO("Start method : %d... \r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
+
+									if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
+									   //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
+									   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
+									{
+										switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
+										{
+											case AUTH_MODE_ENABLE:
+												if(ShmOCPP16Data->OcppConnStatus)
+												{
+													// On line
+													ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
+												}
+												else
+												{
+													// Off line
+													switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
+													{
+														case OFF_POLICY_LOCALLIST:
+															ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
+															break;
+														case OFF_POLICY_PH_RFID:
+															break;
+														case OFF_POLICY_FREE:
+															break;
+														case OFF_POLICY_NOCHARGE:
+														default:
+															break;
+													}
+												}
+												break;
+											case AUTH_MODE_DISABLE:
+											default:
+												break;
+										}
+									}
+									else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
+									{
+										DEBUG_INFO("Should be remote stop charger... \r\n");
+										ShmCharger->gun_info[gun_index].rfidReq = OFF;
+									} 
+								}
+								else
+								{
+									if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
+									  (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
+									  (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
+									  (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
+									{
+										if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
+										   (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
+										   (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
+										   (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
+										{
+											ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
+										}
+										else
+										{
+											ShmCharger->gun_info[gun_index].rfidReq = OFF;
+											ShmOCPP16Data->SpMsg.bits.AuthorizeReq = OFF;
+											DEBUG_INFO("Authorize fail... \r\n");
+											setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
+											setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
+											sleep(3);
+										}
+
+										startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
+										ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
+									}
+								}
+							}
+						}
+						
 						if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
 						   (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
 						   (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON)  &&
@@ -3001,9 +3188,9 @@ int main(void)
 					DEBUG_INFO("Gun-%d : StopReason [ %s ]...\r\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
 
 					memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
-					ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
+					ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
+					
 					ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
-
 					ShmCharger->gun_info[gun_index].rfidReq = OFF;
 					ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
 					ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
@@ -3067,7 +3254,7 @@ int main(void)
 						ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
 						int value = upgrade_check();
 
-						DEBUG_INFO("Local update Value: %d...\r\n",value);
+						DEBUG_INFO("Local update Value: %s... \r\n",((value == PASS)?"Pass": "Fail"));
 						if(value == PASS)
 						{
 							if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
@@ -3104,7 +3291,7 @@ int main(void)
 							ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
 							int result = upgrade_check();
 
-							DEBUG_INFO("Remote update Result: %d...\r\n",result);
+							DEBUG_INFO("Remote update Result: %s... \r\n",((result == PASS)?"Pass": "Fail"));
 							if(result == PASS)
 							{
 								if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)

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

@@ -419,6 +419,7 @@ typedef struct GUN_INFO
 	uint16_t										isSetLedBrightness:1;
 	uint16_t										isUnlockerConnetor:1;
 	uint16_t										isOperactive:1;
+	uint16_t										isChargerStopByCondition:1;
 }Gun_Info;
 
 struct Charger

BIN
EVSE/Projects/AW-Regular/Images/FactoryDefaultConfig.bin


BIN
EVSE/Projects/AW-Regular/Images/ramdisk.gz