Module_UpdateFW.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. #include "../Config.h"
  8. #include "../Log/log.h"
  9. #include "../Define/define.h"
  10. #include "../ShareMemory/shmMem.h"
  11. #include "../CSU/main.h"
  12. struct SysConfigData *pSysConfig = NULL;
  13. struct SysInfoData *pSysInfo = NULL;
  14. struct OCPP16Data *ShmOCPP16Data = NULL;
  15. struct ChargingInfoData *pAcChargingInfo = NULL;
  16. static struct ChargingInfoData *pDcChargingInfo = NULL;
  17. #define MODELNAME_FAIL (0)
  18. #define UPGRADE_FAN (0x02)
  19. #define UPGRADE_RB (0x09) //0x09 for DD360 dispenser
  20. #define UPGRADE_PRI (0x04)
  21. #define UPGRADE_AC (0x05)
  22. #define UPGRADE_LED (0x06)
  23. //------------------------------------------------------------------------------
  24. static char *_priPortName = "/dev/ttyS1";
  25. static char *_485PortName = "/dev/ttyS5";
  26. //------------------------------------------------------------------------------
  27. void KillAllTask(void)
  28. {
  29. pSysInfo->PageIndex = _LCM_MAINTAIN;
  30. system("killall Module_EventLogging");
  31. system("killall Module_PrimaryComm");
  32. system("killall Module_EvComm");
  33. system("killall Module_LcmControl");
  34. system("killall Module_InternalComm");
  35. system("killall Module_DoComm");
  36. return ;
  37. }
  38. void KillTask(void)
  39. {
  40. pSysInfo->PageIndex = _LCM_MAINTAIN;
  41. system("killall Module_EventLogging");
  42. system("killall Module_PrimaryComm");
  43. system("killall Module_EvComm");
  44. system("killall Module_LcmControl");
  45. system("killall Module_InternalComm");
  46. //system("killall Module_DoComm");
  47. return ;
  48. }
  49. void setChargerMode(uint8_t gunIndex, uint8_t mode)
  50. {
  51. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  52. pDcChargingInfo->SystemStatus = mode;
  53. log_info(" ====== System Status:%d ======",mode);
  54. }
  55. void TryCloseWatchdog()
  56. {
  57. system("echo V > /dev/watchdog");
  58. }
  59. static int InitComPort(uint8_t target)
  60. {
  61. int fd;
  62. struct termios tios;
  63. if (target == UPGRADE_PRI) {
  64. fd = open(_priPortName, O_RDWR);
  65. } else if (target == UPGRADE_FAN ||
  66. target == UPGRADE_RB ||
  67. target == UPGRADE_AC ||
  68. target == UPGRADE_LED
  69. ) {
  70. fd = open(_485PortName, O_RDWR);
  71. }
  72. if (fd <= 0) {
  73. log_error("open 407 Communication port NG ");
  74. return -1;
  75. }
  76. ioctl (fd, TCGETS, &tios);
  77. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  78. tios.c_lflag = 0;
  79. tios.c_iflag = 0;
  80. tios.c_oflag = 0;
  81. tios.c_cc[VMIN] = 0;
  82. tios.c_cc[VTIME] = (uint8_t)1;
  83. tios.c_lflag = 0;
  84. tcflush(fd, TCIFLUSH);
  85. ioctl (fd, TCSETS, &tios);
  86. return fd;
  87. }
  88. bool IsConnectorWholeIdle()
  89. {
  90. bool result = true;
  91. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  92. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  93. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  94. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  95. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  96. result = false;
  97. break;
  98. }
  99. }
  100. /*
  101. for (uint8_t count = 0; count < pSysConfig->AcConnectorCount; count++) {
  102. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  103. if (pAcChargingInfo->SystemStatus != S_IDLE &&
  104. pAcChargingInfo->IsErrorOccur == NO) {
  105. result = false;
  106. break;
  107. }
  108. }
  109. */
  110. return result;
  111. }
  112. static int InitCanBus(void)
  113. {
  114. int fd = -1;
  115. int nbytes;
  116. struct timeval tv;
  117. struct ifreq ifr0;
  118. struct sockaddr_can addr0;
  119. system("/sbin/ip link set can0 down");
  120. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  121. system("/sbin/ip link set can0 up");
  122. fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  123. tv.tv_sec = 0;
  124. tv.tv_usec = 10000;
  125. if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
  126. log_error("Set SO_RCVTIMEO NG");
  127. }
  128. nbytes = 40960;
  129. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0) {
  130. log_error("Set SO_RCVBUF NG");
  131. }
  132. nbytes = 40960;
  133. if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0) {
  134. log_error("Set SO_SNDBUF NG");
  135. }
  136. strcpy(ifr0.ifr_name, "can0");
  137. ioctl(fd, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  138. addr0.can_family = AF_CAN;
  139. addr0.can_ifindex = ifr0.ifr_ifindex;
  140. bind(fd, (struct sockaddr *)&addr0, sizeof(addr0));
  141. return fd;
  142. }
  143. unsigned long long getAvailableMemory()
  144. {
  145. system("pkill ntpd");
  146. sleep(1);
  147. long pages = sysconf(_SC_AVPHYS_PAGES);
  148. long page_size = sysconf(_SC_PAGE_SIZE);
  149. log_info("Avaiable Memory size:%.1f MB",pages*page_size/(1024*1024));
  150. return pages * page_size;
  151. }
  152. static int CheckUpdateProcess(void)
  153. {
  154. //bool isPass = true;
  155. uint8_t retSucc = 0;
  156. uint8_t retFail = 0;
  157. uint8_t index = 0;
  158. uint8_t target = 0;
  159. char Buf[256];
  160. char *new_str = NULL;
  161. uint8_t *ptr = NULL;
  162. int fd = 0;
  163. int CanFd = 0;
  164. int uartFd = 0;
  165. unsigned int Type = 0;
  166. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  167. DIR *d;
  168. struct dirent *dir;
  169. DcCommonInfo *ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  170. struct ChargingInfoData *pDcChargingInfo = NULL;
  171. if (getAvailableMemory() < (200 * 1024 * 1024))
  172. {
  173. log_info("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory() / (1024 * 1024.0));
  174. system("echo 3 > /proc/sys/vm/drop_caches");
  175. sleep(1);
  176. }
  177. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  178. d = opendir("/mnt/");
  179. if (d) {
  180. while ((dir = readdir(d)) != NULL) {
  181. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  182. continue;
  183. }
  184. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  185. //new_str[0] = '\0';
  186. strcat(new_str, "/mnt/");
  187. strcat(new_str, dir->d_name);
  188. log_info("%s%s", "/mnt/", dir->d_name);
  189. fd = open(new_str, O_RDONLY);
  190. if (fd < 0) {
  191. return FAIL;
  192. }
  193. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  194. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  195. //get the image length
  196. ImageLen = read(fd, ptr, MaxLen);
  197. for (uint8_t i = 0; i < 16; i++) {
  198. if (pSysConfig->ModelName[i] != ptr[i]) {
  199. return FAIL;
  200. }
  201. }
  202. log_info("model name check pass. ");
  203. if (ImageLen > 20) {
  204. Type = (((unsigned int)ptr[16]) << 24 |
  205. ((unsigned int)ptr[17]) << 16 |
  206. ((unsigned int)ptr[18]) << 8 |
  207. ((unsigned int)ptr[19]));
  208. log_info("Typed...%x ", Type);
  209. free(ptr);
  210. switch (Type) {
  211. case 0x10000001:
  212. case 0x10000002:
  213. case 0x10000003:
  214. case 0x10000004:
  215. case 0x10000005:
  216. if (Upgrade_Flash(Type, new_str, (char *)pSysConfig->ModelName) == PASS) {
  217. //return PASS;
  218. retSucc++;
  219. } else {
  220. log_info("Upgrade %x Failed", Type);
  221. //return FAIL;
  222. retFail++;
  223. }
  224. break;
  225. case 0x10000007:
  226. case 0x10000008:
  227. case 0x10000009:
  228. case 0x1000000A:
  229. CanFd = InitCanBus();
  230. if (CanFd > 0) {
  231. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  232. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  233. if (pDcChargingInfo->Type == _Type_CCS_2) {
  234. uint8_t targetID = pDcChargingInfo->Evboard_id;
  235. if (pSysConfig->TotalConnectorCount == 1 &&
  236. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  237. //targetID += 1;
  238. }
  239. system("echo 3 > /proc/sys/vm/drop_caches");
  240. sleep(2);
  241. log_info("Upgrade CCS Processing..target id = %d ", targetID);
  242. if (Upgrade_CCS(CanFd,
  243. Type,
  244. targetID,
  245. new_str,
  246. (char *)pSysConfig->ModelName) == FAIL) {
  247. log_info("Upgrade CCS Failed ");
  248. retFail++;
  249. } else {
  250. retSucc++;
  251. }
  252. }
  253. }
  254. close(CanFd);
  255. }
  256. memset(Buf, 0, sizeof(Buf));
  257. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  258. system(Buf);
  259. //isPass = true;
  260. #if 0
  261. CanFd = InitCanBus();
  262. if (CanFd > 0) {
  263. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  264. //if (!isPass) {
  265. // break;
  266. //}
  267. if (chargingInfo[index]->Type == _Type_CCS_2) {
  268. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)pSysConfig->ModelName) == FAIL) {
  269. //isPass = false;
  270. log_info("Upgrade %x Failed", Type);
  271. retFail++;
  272. }
  273. }
  274. }
  275. } else {
  276. log_error("Upgrade CCS open CAN FD fail.");
  277. //isPass = false;
  278. return FAIL;
  279. }
  280. if (retFail != 0) {
  281. break;
  282. } else {
  283. retSucc++;
  284. }
  285. //return isPass;
  286. #endif //0
  287. break;
  288. case 0x10000006:
  289. case 0x1000000D:
  290. case 0x1000000E:
  291. case 0x20000002:
  292. case 0x10000014:
  293. // CSU_PRIMARY_CONTROLLER : 0x10000006
  294. target = 0x00;
  295. if (Type == 0x10000006) {
  296. target = UPGRADE_PRI;
  297. } else if (Type == 0x1000000D) {
  298. target = UPGRADE_RB;
  299. } else if (Type == 0x1000000E) {
  300. target = UPGRADE_FAN;
  301. } else if (Type == 0x20000002) {
  302. target = UPGRADE_AC;
  303. } else if (Type == 0x10000014) {
  304. target = UPGRADE_LED;
  305. }
  306. uartFd = InitComPort(target);
  307. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)pSysConfig->ModelName) == PASS) {
  308. //return PASS;
  309. retSucc++;
  310. } else {
  311. log_info("Upgrade %x Failed", Type);
  312. //return FAIL;
  313. return FAIL;
  314. }
  315. if (uartFd > 0) {
  316. close(uartFd);
  317. }
  318. break;
  319. case 0x1000000B:
  320. case 0x1000000C:
  321. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  322. //bool isPass = true;
  323. CanFd = InitCanBus();
  324. if (CanFd > 0) {
  325. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  326. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  327. //if (!isPass) {
  328. // break;
  329. //}
  330. if ((Type == 0x1000000B && pDcChargingInfo->Type == _Type_Chademo) ||
  331. (Type == 0x1000000C && pDcChargingInfo->Type == _Type_GB)) {
  332. if (Upgrade_CAN(CanFd, Type, pDcChargingInfo->Evboard_id, new_str, (char *)pSysConfig->ModelName) == PASS) {
  333. //isPass = PASS;
  334. retSucc++;
  335. } else {
  336. log_info("Upgrade %x Failed", Type);
  337. //isPass = FAIL;
  338. retFail++;
  339. }
  340. }
  341. }
  342. } else {
  343. log_info("Upgrad FD fail. ");
  344. //isPass = false;
  345. return FAIL;
  346. }
  347. //return isPass;
  348. break;
  349. }
  350. } else {
  351. free(ptr);
  352. }
  353. free(new_str);
  354. }
  355. }
  356. free(dir);
  357. closedir(d);
  358. if (retFail != 0) {
  359. return FAIL;
  360. }
  361. return PASS;
  362. }
  363. void CheckFwUpdateFunction(void)
  364. {
  365. //log_info("pSysInfo->FirmwareUpdate = %d ", pSysInfo->FirmwareUpdate);
  366. if (pSysInfo->FirmwareUpdate == YES) {
  367. log_info("ftp : update start. ");
  368. TryCloseWatchdog();
  369. pSysInfo->SystemPage = _LCM_MAINTAIN;
  370. for (uint8_t gun_index = 0; gun_index < pSysConfig->TotalConnectorCount; gun_index++) {
  371. setChargerMode(gun_index, MODE_UPDATE);
  372. }
  373. sleep(1);
  374. uint8_t updateResult = CheckUpdateProcess();
  375. if (updateResult == PASS) {
  376. log_info("ftp : update complete. ");
  377. } else if (updateResult == MODELNAME_FAIL) {
  378. log_info("ftp : model name is none match. ");
  379. KillAllTask();
  380. pSysInfo->FirmwareUpdate = NO;
  381. sleep(5);
  382. system("/usr/bin/run_evse_restart.sh");
  383. return;
  384. } else {
  385. log_info("ftp : update fail. ");
  386. }
  387. pSysInfo->FirmwareUpdate = NO;
  388. sleep(5);
  389. system("reboot -f");
  390. } else if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES) {
  391. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  392. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL) {
  393. log_info("Backend : update start. ");
  394. TryCloseWatchdog();
  395. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  396. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  397. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  398. KillTask();
  399. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  400. setChargerMode(_index, MODE_UPDATE);
  401. }
  402. for (uint8_t _index = 0; _index < pSysConfig->AcConnectorCount; _index++) {
  403. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(_index);
  404. pAcChargingInfo->SystemStatus = MODE_UPDATE;
  405. }
  406. sleep(1);
  407. uint8_t updateResult = CheckUpdateProcess();
  408. if (updateResult == PASS) {
  409. log_info("Backend : update complete. ");
  410. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  411. } else if (updateResult == MODELNAME_FAIL) {
  412. log_info("Backend : model name is none match. ");
  413. KillAllTask();
  414. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  415. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  416. sleep(5);
  417. system("/usr/bin/run_evse_restart.sh");
  418. return;
  419. } else {
  420. log_info("Backend : update fail. ");
  421. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  422. }
  423. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  424. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  425. sleep(5);
  426. system("reboot -f");
  427. }
  428. }
  429. }
  430. int main(int argc, char *argv[])
  431. {
  432. if (CreateAllCsuShareMemory() == FAIL) {
  433. log_error("create share memory error");
  434. return FAIL;
  435. }
  436. MappingGunChargingInfo("Upgrade Task");
  437. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  438. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  439. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  440. while (1) {
  441. if (IsConnectorWholeIdle())
  442. CheckFwUpdateFunction();
  443. sleep(3);
  444. } //while
  445. return 0;
  446. }