Module_InitUpgrade.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Module_InitUpgrade.c
  3. *
  4. * Created on: 2021/01/29
  5. * Author: foluswen
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <termios.h>
  28. #include <errno.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <netdb.h>
  38. #include <error.h>
  39. #include <signal.h>
  40. #include "define.h"
  41. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
  42. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
  43. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
  44. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  45. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  46. #define PASS 1
  47. #define FAIL -1
  48. #define ON 1
  49. #define OFF 0
  50. #define YES 1
  51. #define NO 0
  52. #define true 1
  53. #define false 0
  54. #define MtdBlockSize 0x300000
  55. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  56. struct StatusCodeData *ShmStatusCodeData;
  57. //==========================================
  58. // Common routine
  59. //==========================================
  60. int StoreLogMsg(const char *fmt, ...)
  61. {
  62. char Buf[4096+256];
  63. char buffer[4096];
  64. time_t CurrentTime;
  65. struct tm *tm;
  66. struct timeval tv;
  67. va_list args;
  68. va_start(args, fmt);
  69. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  70. va_end(args);
  71. memset(Buf,0,sizeof(Buf));
  72. CurrentTime = time((time_t*)NULL);
  73. tm=localtime(&CurrentTime);
  74. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  75. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_InitUpgradeLog",
  76. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  77. buffer,
  78. tm->tm_year+1900,tm->tm_mon+1);
  79. #ifdef SystemLogMessage
  80. system(Buf);
  81. #endif
  82. #ifdef ConsloePrintLog
  83. 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);
  84. #endif
  85. return rc;
  86. }
  87. int DiffTimeb(struct timeb ST, struct timeb ET)
  88. {
  89. //return milli-second
  90. unsigned int StartTime,StopTime;
  91. StartTime=(unsigned int)ST.time;
  92. StopTime=(unsigned int)ET.time;
  93. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  94. }
  95. int runShellCmd(const char*cmd)
  96. {
  97. int result = FAIL;
  98. char buf[256];
  99. FILE *fp;
  100. fp = popen(cmd, "r");
  101. if(fp != NULL)
  102. {
  103. while(fgets(buf, sizeof(buf), fp) != NULL)
  104. {
  105. DEBUG_INFO("%s\n", buf);
  106. }
  107. result = PASS;
  108. }
  109. pclose(fp);
  110. return result;
  111. }
  112. int StoreUsrConfigData(struct SysConfigData *UsrData)
  113. {
  114. int result = PASS;
  115. int fd,wrd;
  116. unsigned int i,Chk;
  117. unsigned char *ptr, *BufTmp;
  118. Chk=0;
  119. ptr=(unsigned char *)UsrData;
  120. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  121. {
  122. memset(BufTmp,0,MtdBlockSize);
  123. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  124. for(i=ARRAY_SIZE(UsrData->CsuBootLoadFwRev);i<MtdBlockSize-4;i++)
  125. Chk+=*(BufTmp+i);
  126. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  127. // Output configuration to file.
  128. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  129. if (fd < 0)
  130. {
  131. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  132. free(BufTmp);
  133. return 0;
  134. }
  135. wrd=write(fd, BufTmp, MtdBlockSize);
  136. close(fd);
  137. if(wrd<MtdBlockSize)
  138. {
  139. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  140. free(BufTmp);
  141. return 0;
  142. }
  143. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  144. DEBUG_INFO("Erase /dev/mtd10.\n");
  145. runShellCmd("flash_erase /dev/mtd10 0 0");
  146. DEBUG_INFO("Write /dev/mtd10.\n");
  147. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  148. DEBUG_INFO("Erase /dev/mtd11.\n");
  149. runShellCmd("flash_erase /dev/mtd11 0 0");
  150. DEBUG_INFO("Write /dev/mtd11.\n");
  151. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  152. system("rm -f /mnt/EvseConfig.bin");
  153. DEBUG_INFO("EvseConfig write to flash OK\n");
  154. }
  155. else
  156. {
  157. DEBUG_ERROR("alloc BlockSize NG\r\n");
  158. result = FAIL;
  159. }
  160. if(BufTmp!=NULL)
  161. free(BufTmp);
  162. return result;
  163. }
  164. //==========================================
  165. // Init all share memory
  166. //==========================================
  167. int InitShareMemory()
  168. {
  169. int result = PASS;
  170. int MeterSMId;
  171. //Initial ShmSysConfigAndInfo
  172. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  173. {
  174. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  175. result = FAIL;
  176. }
  177. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  178. {
  179. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  180. result = FAIL;
  181. }
  182. else
  183. {}
  184. //Initial ShmStatusCodeData
  185. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  186. {
  187. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  188. result = FAIL;
  189. }
  190. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  191. {
  192. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  193. result = FAIL;
  194. }
  195. else
  196. {}
  197. return result;
  198. }
  199. //==========================================
  200. // Main process
  201. //==========================================
  202. int main(void)
  203. {
  204. char cmd[512] = {0};
  205. uint8_t retryCnt = 0;
  206. // Initial share memory
  207. if(InitShareMemory() == FAIL)
  208. {
  209. DEBUG_ERROR("InitShareMemory NG\n");
  210. if(ShmStatusCodeData!=NULL)
  211. {
  212. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  213. }
  214. sleep(5);
  215. return 0;
  216. }
  217. for(;;)
  218. {
  219. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14) &&
  220. ShmSysConfigAndInfo->SysInfo.InternetConn &&
  221. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  222. {
  223. system("ping 8.8.8.8 &");
  224. // Delete old file if exist
  225. runShellCmd("rm -f /mnt/LatestImage.zip");
  226. // Download file by model name
  227. DEBUG_INFO("Download %s latest image file.\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  228. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  229. 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);
  230. //DEBUG_INFO("%s\n", cmd);
  231. if(system(cmd) == 0)
  232. {
  233. DEBUG_INFO("File download success\n");
  234. DEBUG_INFO("Unzip download file\n");
  235. runShellCmd("unzip -oq /mnt/LatestImage.zip -d /mnt");
  236. DEBUG_INFO("OFF first upgrade request\n");
  237. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade = OFF;
  238. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig))
  239. {
  240. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update success\n");
  241. }
  242. else
  243. {
  244. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update fail\n");
  245. }
  246. DEBUG_INFO("Trigger upgrade request\n");
  247. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = ON;
  248. }
  249. else
  250. {
  251. DEBUG_ERROR("Image download fail\n");
  252. if(retryCnt > 10)
  253. {
  254. DEBUG_INFO("File download retry over 10 times, disable upgrade request.\n");
  255. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade = OFF;
  256. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig))
  257. {
  258. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update success\n");
  259. }
  260. else
  261. {
  262. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig update fail\n");
  263. }
  264. }
  265. else
  266. {
  267. retryCnt += 1;
  268. }
  269. }
  270. // Delete download file
  271. runShellCmd("rm -f /mnt/LatestImage.zip");
  272. system("pkill ping");
  273. }
  274. sleep(10);
  275. }
  276. return FAIL;
  277. }