Module_UpdateFW.c 20 KB

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