Browse Source

[Add feature][Modularization][Module_InitUpgrade]

2021.02.01 / Folus Wen

Actions:
1. Add Module_InitUpgrade for auto upgrade firmware when charger first boot up.

Files:
1. As follow commit history

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

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 4 years ago
parent
commit
1d44cfec65

+ 7 - 1
EVSE/Modularization/Makefile

@@ -4,7 +4,7 @@ export PATH=/bin:/sbin:/usr/bin:$(SDK_PATH_TARGET)/usr/bin:$PATH
 #define library variable
 Lib_SQLite3 = "-L../../../Modularization/ocppfiles" -lsqlite3
 
-all: clean Module_RFIDLib Module_Wifi WebServiceLib Ocpp16 Phihong_PsuCommObj Module_4g Module_UpgradeLib Infypwr_PsuCommObj Module_EventLogging Module_ProduceUtils Module_PhBackend Ocpp20
+all: clean Module_RFIDLib Module_Wifi WebServiceLib Ocpp16 Phihong_PsuCommObj Module_4g Module_UpgradeLib Infypwr_PsuCommObj Module_EventLogging Module_ProduceUtils Module_PhBackend Ocpp20 Module_InitUpgrade
 
 
 clean:
@@ -103,3 +103,9 @@ Module_PhBackend:
 	rm -f Module_PhBackend.o
 	mv -f Module_PhBackend ../rootfs/root
 
+Module_InitUpgrade:
+	rm -f Module_InitUpgrade
+	$(CC) -D $(Project) -I ../Projects -O0 -g3 -Wall -c -fmessage-length=0 -o Module_InitUpgrade.o Module_InitUpgrade.c
+	$(CC) -o Module_InitUpgrade Module_InitUpgrade.o
+	rm -f Module_InitUpgrade.o
+	mv -f Module_InitUpgrade ../rootfs/root

+ 327 - 0
EVSE/Modularization/Module_InitUpgrade.c

@@ -0,0 +1,327 @@
+/*
+ * Module_InitUpgrade.c
+ *
+ *  Created on: 2021/01/29
+ *      Author: foluswen
+ */
+
+#include 	<sys/time.h>
+#include 	<sys/timeb.h>
+#include    <sys/types.h>
+#include    <sys/stat.h>
+#include 	<sys/types.h>
+#include 	<sys/ioctl.h>
+#include 	<sys/socket.h>
+#include 	<sys/ipc.h>
+#include 	<sys/shm.h>
+#include 	<sys/shm.h>
+#include 	<sys/mman.h>
+#include 	<linux/wireless.h>
+#include 	<arpa/inet.h>
+#include 	<netinet/in.h>
+
+#include 	<unistd.h>
+#include 	<stdarg.h>
+#include    <stdio.h>
+#include    <stdlib.h>
+#include    <unistd.h>
+#include    <fcntl.h>
+#include    <termios.h>
+#include    <errno.h>
+#include 	<errno.h>
+#include 	<string.h>
+#include	<time.h>
+#include	<ctype.h>
+#include 	<ifaddrs.h>
+#include 	<sys/types.h>
+#include 	<sys/socket.h>
+#include 	<netinet/in.h>
+#include 	<netdb.h>
+#include 	<error.h>
+#include 	<signal.h>
+#include	"define.h"
+
+#define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
+#define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
+#define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
+
+#define is_error(ptr) 				((unsigned long)ptr > (unsigned long)-4000L)
+#define ARRAY_SIZE(A)				(sizeof(A) / sizeof(A[0]))
+#define PASS						1
+#define FAIL			   			-1
+#define ON							1
+#define OFF							0
+#define YES							1
+#define NO							0
+#define true			    		1
+#define false						0
+#define MtdBlockSize				0x600000
+
+struct SysConfigAndInfo		*ShmSysConfigAndInfo;
+struct StatusCodeData 		*ShmStatusCodeData;
+
+
+//==========================================
+// Common routine
+//==========================================
+int StoreLogMsg(const char *fmt, ...)
+{
+	char Buf[4096+256];
+	char buffer[4096];
+	time_t CurrentTime;
+	struct tm *tm;
+	struct timeval tv;
+	va_list args;
+
+	va_start(args, fmt);
+	int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
+	va_end(args);
+
+	memset(Buf,0,sizeof(Buf));
+	CurrentTime = time((time_t*)NULL);
+	tm=localtime(&CurrentTime);
+	gettimeofday(&tv, NULL); // get microseconds, 10^-6
+
+
+	sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_InitUpgradeLog",
+				tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
+				buffer,
+				tm->tm_year+1900,tm->tm_mon+1);
+
+#ifdef SystemLogMessage
+	system(Buf);
+#endif
+
+#ifdef ConsloePrintLog
+	printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
+#endif
+
+	return rc;
+}
+
+int 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;
+}
+
+int runShellCmd(const char*cmd)
+{
+	int result = FAIL;
+	char buf[256];
+	FILE *fp;
+
+	fp = popen(cmd, "r");
+	if(fp != NULL)
+	{
+		while(fgets(buf, sizeof(buf), fp) != NULL)
+		{
+			DEBUG_INFO("%s\n", buf);
+		}
+
+		result = PASS;
+	}
+	pclose(fp);
+
+	return result;
+}
+
+int StoreUsrConfigData(struct SysConfigData *UsrData)
+{
+	int result = PASS;
+	int fd,wrd;
+	unsigned int i,Chk;
+	unsigned char *ptr, *BufTmp;
+
+	Chk=0;
+	ptr=(unsigned char *)UsrData;
+	if((BufTmp=malloc(MtdBlockSize))!=NULL)
+	{
+		memset(BufTmp,0,MtdBlockSize);
+		memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
+		for(i=0;i<MtdBlockSize-4;i++)
+			Chk+=*(BufTmp+i);
+		memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
+
+		// Output configuration to file.
+		fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
+		if (fd < 0)
+		{
+			DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
+
+			free(BufTmp);
+			return 0;
+		}
+		wrd=write(fd, BufTmp, MtdBlockSize);
+		close(fd);
+		if(wrd<MtdBlockSize)
+		{
+			DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
+
+			free(BufTmp);
+			return 0;
+		}
+		DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
+
+
+		DEBUG_INFO("Erase /dev/mtd10.\n");
+		runShellCmd("flash_erase /dev/mtd10 0 12");
+		DEBUG_INFO("Write /dev/mtd10.\n");
+		runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
+
+		DEBUG_INFO("Erase /dev/mtd11.\n");
+		runShellCmd("flash_erase /dev/mtd11 0 12");
+		DEBUG_INFO("Write /dev/mtd11.\n");
+		runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
+
+
+		system("rm -f /mnt/EvseConfig.bin");
+		DEBUG_INFO("EvseConfig write to flash OK\n");
+	}
+	else
+	{
+		DEBUG_ERROR("alloc BlockSize NG\r\n");
+			result = FAIL;
+	}
+
+	if(BufTmp!=NULL)
+		free(BufTmp);
+
+	return result;
+}
+
+//==========================================
+// Init all share memory
+//==========================================
+int InitShareMemory()
+{
+	int result = PASS;
+	int MeterSMId;
+
+	//Initial ShmSysConfigAndInfo
+	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0)
+    {
+		DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
+		result = FAIL;
+	}
+    else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+    {
+    	DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
+    	result = FAIL;
+   	 }
+    else
+    {}
+
+   	//Initial ShmStatusCodeData
+   	if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0)
+    {
+   		DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
+   		result = FAIL;
+	}
+    else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
+    {
+    	DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
+    	result = FAIL;
+   	}
+    else
+    {}
+
+
+    return result;
+}
+
+//==========================================
+// Main process
+//==========================================
+int main(void)
+{
+	char cmd[512] = {0};
+	uint8_t retryCnt = 0;
+
+	// Initial share memory
+	if(InitShareMemory() == FAIL)
+	{
+		DEBUG_ERROR("InitShareMemory NG\n");
+
+		if(ShmStatusCodeData!=NULL)
+		{
+			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
+		}
+		sleep(5);
+		return 0;
+	}
+
+	for(;;)
+	{
+		if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14) &&
+		   ShmSysConfigAndInfo->SysInfo.InternetConn &&
+		   ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
+		{
+			system("ping 8.8.8.8 &");
+
+			// Delete old file if exist
+			runShellCmd("rm -f /mnt/LatestImage.zip");
+
+			// Download file by model name
+			DEBUG_INFO("Download %s latest image file.\n", ShmSysConfigAndInfo->SysConfig.ModelName);
+			memset(cmd, 0x00, ARRAY_SIZE(cmd));
+			sprintf(cmd, "wget --tries=3 -O /mnt/LatestImage.zip -c ftp://ui:ph123456@ftp.phihong.com.tw/Upgrade/%s/LatestImage.zip -T 120", ShmSysConfigAndInfo->SysConfig.ModelName);
+			//DEBUG_INFO("%s\n", cmd);
+
+			if(system(cmd) == 0)
+			{
+				DEBUG_INFO("File download success\n");
+
+				DEBUG_INFO("Unzip download file\n");
+				runShellCmd("unzip -oq /mnt/LatestImage.zip -d /mnt");
+
+				DEBUG_INFO("OFF first upgrade request\n");
+				ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade = OFF;
+				if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig))
+				{
+					DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update success\n");
+				}
+				else
+				{
+					DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update fail\n");
+				}
+
+				DEBUG_INFO("Trigger upgrade request\n");
+				ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = ON;
+			}
+			else
+			{
+				DEBUG_ERROR("Image download fail\n");
+				if(retryCnt > 10)
+				{
+					DEBUG_INFO("File download retry over 10 times, disable upgrade request.\n");
+					ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade = OFF;
+					if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig))
+					{
+						DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update success\n");
+					}
+					else
+					{
+						DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update fail\n");
+					}
+				}
+				else
+				{
+					retryCnt += 1;
+				}
+			}
+
+			// Delete download file
+			runShellCmd("rm -f /mnt/LatestImage.zip");
+			system("pkill ping");
+		}
+
+		sleep(10);
+	}
+
+	return FAIL;
+}

+ 2 - 1
EVSE/Projects/AW-CCS/Apps/Module_FactoryConfig.c

@@ -265,6 +265,7 @@ int main(int argc, char *argv[])
 	{
 		memcpy((char*)SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
 		memcpy((char*)SysConfig.SerialNumber, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
+		memcpy((char*)SysConfig.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
 
 		DEBUG_INFO("InitShareMemory OK.\n");
 	}
@@ -311,7 +312,7 @@ int main(int argc, char *argv[])
 	SysConfig.OfflinePolicy = 2;			// 0: local list, 1: Phihong RFID tag, 2: free charging, 3: no charging
 	SysConfig.OfflineMaxChargeEnergy = 0;	// 0: Same as MaxChargeEnergy	Other: 1~65535KWH
 	SysConfig.OfflineMaxChargeDuration = 0; // 0: Same as MaxChargeDuration Other: 1~65535 minutes
-	SysConfig.isReqFirstUpgrade = 1;		// 0: Skip first upgrade, 	1: Process firstupgrade
+	SysConfig.isReqFirstUpgrade = 1;		// 0: Skip first upgrade, 	1: Process first upgrade
 
 	// Customization configuration item
 	if(strstr((char*)&SysConfig.ModelName[12], "P0") != NULL)

+ 9 - 0
EVSE/Projects/AW-CCS/Apps/main.c

@@ -1771,6 +1771,8 @@ int SpawnTask()
 	system ("pkill Module_Speaker");
 	system ("pkill Module_ProduceUtils");
 	system ("pkill Module_LcmControl");
+	system ("pkill Module_PowerSharing");
+	system ("pkill Module_InitUpgrade");
 
 	if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
 	{
@@ -2963,6 +2965,13 @@ void checkTask()
 		DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
 		system ("/root/Module_PowerSharing &");
 	}
+
+	if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
+		ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
+	{
+		DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
+		system ("/root/Module_InitUpgrade &");
+	}
 }
 
 void checkConnectionTimeout()

+ 1 - 0
EVSE/rootfs/root/.gitignore

@@ -7,3 +7,4 @@ OcppBackend20
 Module_ProduceUtils
 Module_EventLogging
 Module_PhBackend
+Module_InitUpgrade