Module_UpdateFW.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. unsigned long long getAvailableMemory()
  192. {
  193. long pages = sysconf(_SC_AVPHYS_PAGES);
  194. long page_size = sysconf(_SC_PAGE_SIZE);
  195. return pages * page_size;
  196. }
  197. static int CheckUpdateProcess(void)
  198. {
  199. //bool isPass = true;
  200. uint8_t retSucc = 0;
  201. uint8_t retFail = 0;
  202. uint8_t index = 0;
  203. uint8_t target = 0;
  204. char Buf[256];
  205. char *new_str = NULL;
  206. uint8_t *ptr = NULL;
  207. int fd = 0;
  208. int CanFd = 0;
  209. int uartFd = 0;
  210. unsigned int Type = 0;
  211. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  212. DIR *d;
  213. struct dirent *dir;
  214. struct ChargingInfoData *pDcChargingInfo = NULL;
  215. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  216. if (getAvailableMemory() < (200 * 1024 * 1024))
  217. {
  218. log_info("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory() / (1024 * 1024.0));
  219. system("echo 3 > /proc/sys/vm/drop_caches");
  220. }
  221. d = opendir("/mnt/");
  222. if (d) {
  223. log_info("Open /mnt success");
  224. while ((dir = readdir(d)) != NULL) {
  225. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  226. continue;
  227. }
  228. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  229. //new_str[0] = '\0';
  230. strcat(new_str, "/mnt/");
  231. strcat(new_str, dir->d_name);
  232. log_info("%s%s", "/mnt/", dir->d_name);
  233. fd = open(new_str, O_RDONLY);
  234. if (fd < 0) {
  235. return FAIL;
  236. }
  237. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  238. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  239. //get the image length
  240. ImageLen = read(fd, ptr, MaxLen);
  241. for (uint8_t i = 0; i < 16; i++) {
  242. if (pSysConfig->ModelName[i] != ptr[i]) {
  243. log_info("model name check fail. ");
  244. return FAIL;
  245. }
  246. }
  247. log_info("model name check pass. ");
  248. if (ImageLen > 20) {
  249. Type = (((unsigned int)ptr[16]) << 24 |
  250. ((unsigned int)ptr[17]) << 16 |
  251. ((unsigned int)ptr[18]) << 8 |
  252. ((unsigned int)ptr[19]));
  253. log_info("Typed...%x ", Type);
  254. free(ptr);
  255. switch (Type) {
  256. case 0x10000001:
  257. case 0x10000002:
  258. case 0x10000003:
  259. case 0x10000004:
  260. case 0x10000005:
  261. if (Upgrade_Flash(Type, new_str, (char *)pSysConfig->ModelName) == PASS) {
  262. //return PASS;
  263. retSucc++;
  264. } else {
  265. log_info("Upgrade %x Failed", Type);
  266. //return FAIL;
  267. retFail++;
  268. }
  269. break;
  270. case 0x10000007:
  271. case 0x10000008:
  272. case 0x10000009:
  273. case 0x1000000A:
  274. CanFd = InitCanBus();
  275. if (CanFd > 0) {
  276. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  277. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  278. if (pDcChargingInfo->Type == _Type_CCS_2) {
  279. uint8_t targetID = pDcChargingInfo->Evboard_id;
  280. /*
  281. if (pSysConfig->TotalConnectorCount == 1 &&
  282. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  283. //targetID += 1;
  284. }*/
  285. system("echo 3 > /proc/sys/vm/drop_caches");
  286. sleep(2);
  287. log_info("Upgrade CCS Processing..target id = %d ", targetID);
  288. if (Upgrade_CCS(CanFd,
  289. Type,
  290. targetID,
  291. new_str,
  292. (char *)pSysConfig->ModelName) == FAIL) {
  293. log_info("Upgrade CCS Failed ");
  294. retFail++;
  295. } else {
  296. retSucc++;
  297. }
  298. }
  299. }
  300. close(CanFd);
  301. }
  302. memset(Buf, 0, sizeof(Buf));
  303. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  304. system(Buf);
  305. //isPass = true;
  306. #if 0
  307. CanFd = InitCanBus();
  308. if (CanFd > 0) {
  309. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  310. //if (!isPass) {
  311. // break;
  312. //}
  313. if (chargingInfo[index]->Type == _Type_CCS_2) {
  314. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)pSysConfig->ModelName) == FAIL) {
  315. //isPass = false;
  316. log_info("Upgrade %x Failed", Type);
  317. retFail++;
  318. }
  319. }
  320. }
  321. } else {
  322. log_error("Upgrade CCS open CAN FD fail.");
  323. //isPass = false;
  324. return FAIL;
  325. }
  326. if (retFail != 0) {
  327. break;
  328. } else {
  329. retSucc++;
  330. }
  331. //return isPass;
  332. #endif //0
  333. break;
  334. case 0x10000006:
  335. case 0x1000000D:
  336. case 0x1000000E:
  337. case 0x20000002:
  338. case 0x10000014:
  339. // CSU_PRIMARY_CONTROLLER : 0x10000006
  340. target = 0x00;
  341. if (Type == 0x10000006) {
  342. target = UPGRADE_PRI;
  343. } else if (Type == 0x1000000D) {
  344. target = UPGRADE_RB;
  345. } else if (Type == 0x1000000E) {
  346. target = UPGRADE_FAN;
  347. } else if (Type == 0x20000002) {
  348. target = UPGRADE_AC;
  349. } else if (Type == 0x10000014) {
  350. target = UPGRADE_LED;
  351. }
  352. uartFd = InitComPort(target);
  353. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)pSysConfig->ModelName) == PASS) {
  354. //return PASS;
  355. retSucc++;
  356. } else {
  357. log_info("Upgrade %x Failed", Type);
  358. //return FAIL;
  359. return FAIL;
  360. }
  361. if (uartFd > 0) {
  362. close(uartFd);
  363. }
  364. break;
  365. case 0x1000000B:
  366. case 0x1000000C:
  367. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  368. //bool isPass = true;
  369. CanFd = InitCanBus();
  370. if (CanFd > 0) {
  371. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  372. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  373. //if (!isPass) {
  374. // break;
  375. //}
  376. if ((Type == 0x1000000B && pDcChargingInfo->Type == _Type_Chademo) ||
  377. (Type == 0x1000000C && pDcChargingInfo->Type == _Type_GB)) {
  378. if (Upgrade_CAN(CanFd, Type, pDcChargingInfo->Evboard_id, new_str, (char *)pSysConfig->ModelName) == PASS) {
  379. //isPass = PASS;
  380. retSucc++;
  381. } else {
  382. log_info("Upgrade %x Failed", Type);
  383. //isPass = FAIL;
  384. retFail++;
  385. }
  386. }
  387. }
  388. } else {
  389. log_info("Upgrad FD fail. ");
  390. //isPass = false;
  391. return FAIL;
  392. }
  393. //return isPass;
  394. break;
  395. case 0x1000000F:
  396. // Update LCM
  397. if (CreateBufferForLcmFile(new_str)) {
  398. system("mkdir -p /mnt/lcd");
  399. system("unzip /mnt/BUFFER.zip -d /mnt/lcd");
  400. log_info("Start Upgrade LCM Firmware.");
  401. ShmDcCommonData->_upgrade_lcm_flag = YES;
  402. while(ShmDcCommonData->_upgrade_lcm_result == _LCM_UPGRADE_RESULT_WAIT ) {
  403. sleep(5);
  404. }
  405. if (ShmDcCommonData->_upgrade_lcm_result == _LCM_UPGRADE_RESULT_PASS) {
  406. retSucc++;
  407. } else
  408. retFail++;
  409. } else {
  410. log_info("CreateBufferForLcmFile fail");
  411. }
  412. break;
  413. }
  414. } else {
  415. free(ptr);
  416. }
  417. free(new_str);
  418. }
  419. }
  420. free(dir);
  421. closedir(d);
  422. if (retFail != 0) {
  423. return FAIL;
  424. }
  425. return PASS;
  426. }
  427. void CheckFwUpdateFunction(void)
  428. {
  429. //log_info("pSysInfo->FirmwareUpdate = %d ", pSysInfo->FirmwareUpdate);
  430. if (pSysInfo->FirmwareUpdate == YES) {
  431. log_info("ftp : update start. ");
  432. TryCloseWatchdog();
  433. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  434. for (uint8_t gun_index = 0; gun_index < pSysConfig->TotalConnectorCount; gun_index++) {
  435. setChargerMode(gun_index, MODE_UPDATE);
  436. }
  437. sleep(1);
  438. uint8_t updateResult = CheckUpdateProcess();
  439. if (updateResult == PASS) {
  440. log_info("ftp : update complete. ");
  441. } else if (updateResult == MODELNAME_FAIL) {
  442. log_info("ftp : model name is none match. ");
  443. KillAllTask();
  444. pSysInfo->FirmwareUpdate = NO;
  445. sleep(5);
  446. system("/usr/bin/run_evse_restart.sh");
  447. return;
  448. } else {
  449. log_info("ftp : update fail. ");
  450. }
  451. pSysInfo->FirmwareUpdate = NO;
  452. sleep(5);
  453. system("reboot -f");
  454. } else if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES) {
  455. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  456. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL) {
  457. log_info("Backend : update start. ");
  458. TryCloseWatchdog();
  459. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  460. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  461. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  462. KillTask();
  463. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  464. setChargerMode(_index, MODE_UPDATE);
  465. }
  466. for (uint8_t _index = 0; _index < pSysConfig->AcConnectorCount; _index++) {
  467. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(_index);
  468. pAcChargingInfo->SystemStatus = MODE_UPDATE;
  469. }
  470. sleep(1);
  471. uint8_t updateResult = CheckUpdateProcess();
  472. if (updateResult == PASS) {
  473. log_info("Backend : update complete. ");
  474. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  475. } else if (updateResult == MODELNAME_FAIL) {
  476. log_info("Backend : model name is none match. ");
  477. KillAllTask();
  478. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  479. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  480. sleep(5);
  481. system("/usr/bin/run_evse_restart.sh");
  482. return;
  483. } else {
  484. log_info("Backend : update fail. ");
  485. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  486. }
  487. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  488. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  489. sleep(5);
  490. system("reboot -f");
  491. }
  492. }
  493. }
  494. int main(int argc, char *argv[])
  495. {
  496. if (CreateAllCsuShareMemory() == FAIL) {
  497. log_error("create share memory error");
  498. return FAIL;
  499. }
  500. MappingGunChargingInfo("Upgrade Task");
  501. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  502. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  503. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  504. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  505. ShmDcCommonData->_upgrade_lcm_flag = NO;
  506. ShmDcCommonData->_upgrade_lcm_result = _LCM_UPGRADE_RESULT_WAIT;
  507. while (1) {
  508. //if (IsConnectorWholeIdle() || (pSysInfo->PageIndex == _PAGE_MAINTAIN) )
  509. CheckFwUpdateFunction();
  510. sleep(3);
  511. } //while
  512. return 0;
  513. }