Module_UpdateFW.c 18 KB

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