Module_UpdateFW.c 21 KB

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