UpgradeFW.c 14 KB

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