UpgradeFW.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 "main.h"
  12. //WatchDog.c
  13. extern void CreateWatchdog(void);
  14. extern void TryCloseWatchdog(void);
  15. extern void TryFeedWatchdog(void);
  16. //------------------------------------------------------------------------------
  17. static char *_priPortName = "/dev/ttyS1";
  18. static char *_485PortName = "/dev/ttyS5";
  19. //------------------------------------------------------------------------------
  20. static int InitComPort(uint8_t target)
  21. {
  22. int fd;
  23. struct termios tios;
  24. if (target == UPGRADE_PRI) {
  25. fd = open(_priPortName, O_RDWR);
  26. } else if (target == UPGRADE_FAN ||
  27. target == UPGRADE_RB ||
  28. target == UPGRADE_AC ||
  29. target == UPGRADE_LED
  30. ) {
  31. fd = open(_485PortName, O_RDWR);
  32. }
  33. if (fd <= 0) {
  34. log_error("open 407 Communication port NG \n");
  35. return -1;
  36. }
  37. ioctl (fd, TCGETS, &tios);
  38. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  39. tios.c_lflag = 0;
  40. tios.c_iflag = 0;
  41. tios.c_oflag = 0;
  42. tios.c_cc[VMIN] = 0;
  43. tios.c_cc[VTIME] = (uint8_t)1;
  44. tios.c_lflag = 0;
  45. tcflush(fd, TCIFLUSH);
  46. ioctl (fd, TCSETS, &tios);
  47. return fd;
  48. }
  49. static int InitCanBus(void)
  50. {
  51. int fd = -1;
  52. int nbytes;
  53. struct timeval tv;
  54. struct ifreq ifr0;
  55. struct sockaddr_can addr0;
  56. system("/sbin/ip link set can0 down");
  57. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  58. system("/sbin/ip link set can0 up");
  59. fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  60. tv.tv_sec = 0;
  61. tv.tv_usec = 10000;
  62. if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
  63. log_error("Set SO_RCVTIMEO NG");
  64. }
  65. nbytes = 40960;
  66. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0) {
  67. log_error("Set SO_RCVBUF NG");
  68. }
  69. nbytes = 40960;
  70. if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0) {
  71. log_error("Set SO_SNDBUF NG");
  72. }
  73. strcpy(ifr0.ifr_name, "can0");
  74. ioctl(fd, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  75. addr0.can_family = AF_CAN;
  76. addr0.can_ifindex = ifr0.ifr_ifindex;
  77. bind(fd, (struct sockaddr *)&addr0, sizeof(addr0));
  78. return fd;
  79. }
  80. static int CheckUpdateProcess(void)
  81. {
  82. //bool isPass = true;
  83. uint8_t retSucc = 0;
  84. uint8_t retFail = 0;
  85. uint8_t index = 0;
  86. uint8_t target = 0;
  87. char Buf[256];
  88. char *new_str = NULL;
  89. uint8_t *ptr = NULL;
  90. int fd = 0;
  91. int CanFd = 0;
  92. int uartFd = 0;
  93. unsigned int Type = 0;
  94. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  95. DIR *d;
  96. struct dirent *dir;
  97. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  98. DcCommonInfo *ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  99. struct ChargingInfoData *pDcChargingInfo = NULL;
  100. d = opendir("/mnt/");
  101. if (d) {
  102. while ((dir = readdir(d)) != NULL) {
  103. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  104. continue;
  105. }
  106. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  107. //new_str[0] = '\0';
  108. strcat(new_str, "/mnt/");
  109. strcat(new_str, dir->d_name);
  110. log_info("%s%s\r\n", "/mnt/", dir->d_name);
  111. fd = open(new_str, O_RDONLY);
  112. if (fd < 0) {
  113. return FAIL;
  114. }
  115. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  116. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  117. //get the image length
  118. ImageLen = read(fd, ptr, MaxLen);
  119. for (uint8_t i = 0; i < 16; i++) {
  120. if (pSysConfig->ModelName[i] != ptr[i]) {
  121. return FAIL;
  122. }
  123. }
  124. log_info("model name check pass. \n");
  125. if (ImageLen > 20) {
  126. Type = (((unsigned int)ptr[16]) << 24 |
  127. ((unsigned int)ptr[17]) << 16 |
  128. ((unsigned int)ptr[18]) << 8 |
  129. ((unsigned int)ptr[19]));
  130. log_info("Typed...%x \r\n", Type);
  131. switch (Type) {
  132. case 0x10000001:
  133. case 0x10000002:
  134. case 0x10000003:
  135. case 0x10000004:
  136. case 0x10000005:
  137. if (Upgrade_Flash(Type, new_str, (char *)pSysConfig->ModelName) == PASS) {
  138. //return PASS;
  139. retSucc++;
  140. } else {
  141. log_info("Upgrade %x Failed\r\n", Type);
  142. //return FAIL;
  143. retFail++;
  144. }
  145. break;
  146. case 0x10000007:
  147. case 0x10000008:
  148. case 0x10000009:
  149. case 0x1000000A:
  150. CanFd = InitCanBus();
  151. if (CanFd > 0) {
  152. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  153. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  154. if (pDcChargingInfo->Type == _Type_CCS_2) {
  155. uint8_t targetID = pDcChargingInfo->Evboard_id;
  156. if (pSysConfig->TotalConnectorCount == 1 &&
  157. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  158. //targetID += 1;
  159. }
  160. system("echo 3 > /proc/sys/vm/drop_caches");
  161. sleep(2);
  162. log_info("Upgrade CCS Processing..target id = %d \n", targetID);
  163. if (Upgrade_CCS(CanFd,
  164. Type,
  165. targetID,
  166. new_str,
  167. (char *)pSysConfig->ModelName) == FAIL) {
  168. log_info("Upgrade CCS Failed \n");
  169. retFail++;
  170. } else {
  171. retSucc++;
  172. }
  173. }
  174. }
  175. close(CanFd);
  176. }
  177. memset(Buf, 0, sizeof(Buf));
  178. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  179. system(Buf);
  180. //isPass = true;
  181. #if 0
  182. CanFd = InitCanBus();
  183. if (CanFd > 0) {
  184. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  185. //if (!isPass) {
  186. // break;
  187. //}
  188. if (chargingInfo[index]->Type == _Type_CCS_2) {
  189. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)pSysConfig->ModelName) == FAIL) {
  190. //isPass = false;
  191. log_info("Upgrade %x Failed\r\n", Type);
  192. retFail++;
  193. }
  194. }
  195. }
  196. } else {
  197. log_error("Upgrade CCS open CAN FD fail.\n");
  198. //isPass = false;
  199. return FAIL;
  200. }
  201. if (retFail != 0) {
  202. break;
  203. } else {
  204. retSucc++;
  205. }
  206. //return isPass;
  207. #endif //0
  208. break;
  209. case 0x10000006:
  210. case 0x1000000D:
  211. case 0x1000000E:
  212. case 0x20000002:
  213. case 0x10000014:
  214. // CSU_PRIMARY_CONTROLLER : 0x10000006
  215. target = 0x00;
  216. if (Type == 0x10000006) {
  217. target = UPGRADE_PRI;
  218. } else if (Type == 0x1000000D) {
  219. target = UPGRADE_RB;
  220. } else if (Type == 0x1000000E) {
  221. target = UPGRADE_FAN;
  222. } else if (Type == 0x20000002) {
  223. target = UPGRADE_AC;
  224. } else if (Type == 0x10000014) {
  225. target = UPGRADE_LED;
  226. }
  227. uartFd = InitComPort(target);
  228. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)pSysConfig->ModelName) == PASS) {
  229. //return PASS;
  230. retSucc++;
  231. } else {
  232. log_info("Upgrade %x Failed\r\n", Type);
  233. //return FAIL;
  234. return FAIL;
  235. }
  236. if (uartFd > 0) {
  237. close(uartFd);
  238. }
  239. break;
  240. case 0x1000000B:
  241. case 0x1000000C:
  242. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  243. //bool isPass = true;
  244. CanFd = InitCanBus();
  245. if (CanFd > 0) {
  246. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  247. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  248. //if (!isPass) {
  249. // break;
  250. //}
  251. if ((Type == 0x1000000B && pDcChargingInfo->Type == _Type_Chademo) ||
  252. (Type == 0x1000000C && pDcChargingInfo->Type == _Type_GB)) {
  253. if (Upgrade_CAN(CanFd, Type, pDcChargingInfo->Evboard_id, new_str, (char *)pSysConfig->ModelName) == PASS) {
  254. //isPass = PASS;
  255. retSucc++;
  256. } else {
  257. log_info("Upgrade %x Failed\r\n", Type);
  258. //isPass = FAIL;
  259. retFail++;
  260. }
  261. }
  262. }
  263. } else {
  264. log_info("Upgrad FD fail. \n");
  265. //isPass = false;
  266. return FAIL;
  267. }
  268. //return isPass;
  269. break;
  270. }
  271. }
  272. free(new_str);
  273. free(ptr);
  274. }
  275. }
  276. free(dir);
  277. closedir(d);
  278. if (retFail != 0) {
  279. return FAIL;
  280. }
  281. return PASS;
  282. }
  283. void CheckFwUpdateFunction(void)
  284. {
  285. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  286. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  287. struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  288. struct ChargingInfoData *pAcChargingInfo = NULL;
  289. //log_info("pSysInfo->FirmwareUpdate = %d \n", pSysInfo->FirmwareUpdate);
  290. if (pSysInfo->FirmwareUpdate == YES) {
  291. log_info("ftp : update start. \n");
  292. TryCloseWatchdog();
  293. for (uint8_t gun_index = 0; gun_index < pSysConfig->TotalConnectorCount; gun_index++) {
  294. setChargerMode(gun_index, MODE_UPDATE);
  295. }
  296. sleep(1);
  297. uint8_t updateResult = CheckUpdateProcess();
  298. if (updateResult == PASS) {
  299. log_info("ftp : update complete. \n");
  300. } else if (updateResult == MODELNAME_FAIL) {
  301. log_info("ftp : model name is none match. \n");
  302. KillAllTask();
  303. pSysInfo->FirmwareUpdate = NO;
  304. sleep(5);
  305. system("/usr/bin/run_evse_restart.sh");
  306. return;
  307. } else {
  308. log_info("ftp : update fail. \n");
  309. }
  310. pSysInfo->FirmwareUpdate = NO;
  311. sleep(5);
  312. system("reboot -f");
  313. } else if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES) {
  314. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  315. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL) {
  316. log_info("Backend : update start. \n");
  317. TryCloseWatchdog();
  318. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  319. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  320. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  321. KillTask();
  322. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  323. setChargerMode(_index, MODE_UPDATE);
  324. }
  325. for (uint8_t _index = 0; _index < pSysConfig->AcConnectorCount; _index++) {
  326. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(_index);
  327. pAcChargingInfo->SystemStatus = MODE_UPDATE;
  328. }
  329. sleep(1);
  330. uint8_t updateResult = CheckUpdateProcess();
  331. if (updateResult == PASS) {
  332. log_info("Backend : update complete. \n");
  333. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  334. } else if (updateResult == MODELNAME_FAIL) {
  335. log_info("Backend : model name is none match. \n");
  336. KillAllTask();
  337. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  338. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  339. sleep(5);
  340. system("/usr/bin/run_evse_restart.sh");
  341. return;
  342. } else {
  343. log_info("Backend : update fail. \n");
  344. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  345. }
  346. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  347. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  348. sleep(5);
  349. system("reboot -f");
  350. }
  351. }
  352. }