Module_UpdateFW.c 17 KB

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