Module_UpdateFW.c 18 KB

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