Update.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include "../Config.h"
  6. #include "../Log/log.h"
  7. #include "../Define/define.h"
  8. #include "../ShareMemory/shmMem.h"
  9. //------------------------------------------------------------------------------
  10. int CheckUpdateProcess(void)
  11. {
  12. //bool isPass = true;
  13. uint8_t retSucc = 0;
  14. uint8_t retFail = 0;
  15. uint8_t index = 0;
  16. uint8_t target = 0;
  17. char *new_str = NULL;
  18. uint8_t *ptr = NULL;
  19. int fd = 0;
  20. int CanFd = 0;
  21. int uartFd = 0;
  22. unsigned int Type = 0;
  23. long int MaxLen = (48 * 1024 * 1024, ImageLen = 0);
  24. DIR *d;
  25. struct dirent *dir;
  26. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  27. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  28. d = opendir("/mnt/");
  29. if (d) {
  30. while ((dir = readdir(d)) != NULL) {
  31. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  32. continue;
  33. }
  34. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  35. //new_str[0] = '\0';
  36. strcat(new_str, "/mnt/");
  37. strcat(new_str, dir->d_name);
  38. log_info("%s%s\r\n", "/mnt/", dir->d_name);
  39. fd = open(new_str, O_RDONLY);
  40. if (fd < 0) {
  41. return FAIL;
  42. }
  43. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  44. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  45. //get the image length
  46. ImageLen = read(fd, ptr, MaxLen);
  47. for (uint8_t i = 0; i < 16; i++) {
  48. if (pSysConfig->ModelName[i] != ptr[i]) {
  49. return FAIL;
  50. }
  51. }
  52. log_info("model name check pass. \n");
  53. if (ImageLen > 20) {
  54. Type = (((unsigned int)ptr[16]) << 24 |
  55. ((unsigned int)ptr[17]) << 16 |
  56. ((unsigned int)ptr[18]) << 8 |
  57. ((unsigned int)ptr[19]));
  58. log_info("Typed...%x \r\n", Type);
  59. switch (Type) {
  60. case 0x10000001:
  61. case 0x10000002:
  62. case 0x10000003:
  63. case 0x10000004:
  64. case 0x10000005:
  65. if (Upgrade_Flash(Type, new_str, (char *)pSysConfig->ModelName) == PASS) {
  66. //return PASS;
  67. retSucc++;
  68. } else {
  69. log_info("Upgrade %x Failed\r\n", Type);
  70. //return FAIL;
  71. retFail++;
  72. }
  73. break;
  74. case 0x10000007:
  75. case 0x10000008:
  76. case 0x10000009:
  77. case 0x1000000A:
  78. //isPass = true;
  79. CanFd = InitCanBus();
  80. if (CanFd > 0) {
  81. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  82. //if (!isPass) {
  83. // break;
  84. //}
  85. if (chargingInfo[index]->Type == _Type_CCS_2) {
  86. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)pSysConfig->ModelName) == FAIL) {
  87. //isPass = false;
  88. log_info("Upgrade %x Failed\r\n", Type);
  89. retFail++;
  90. }
  91. }
  92. }
  93. } else {
  94. log_error("Upgrade CCS open CAN FD fail.\n");
  95. //isPass = false;
  96. return FAIL;
  97. }
  98. if (retFail != 0) {
  99. break;
  100. } else {
  101. retSucc++;
  102. }
  103. //return isPass;
  104. break;
  105. case 0x10000006:
  106. case 0x1000000D:
  107. case 0x1000000E:
  108. case 0x20000002:
  109. case 0x10000014:
  110. // CSU_PRIMARY_CONTROLLER : 0x10000006
  111. target = 0x00;
  112. if (Type == 0x10000006) {
  113. target = UPGRADE_PRI;
  114. } else if (Type == 0x1000000D) {
  115. target = UPGRADE_RB;
  116. } else if (Type == 0x1000000E) {
  117. target = UPGRADE_FAN;
  118. } else if (Type == 0x20000002) {
  119. target = UPGRADE_AC;
  120. } else if (Type == 0x10000014) {
  121. target = UPGRADE_LED;
  122. }
  123. uartFd = InitComPort(target);
  124. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)pSysConfig->ModelName) == PASS) {
  125. //return PASS;
  126. retSucc++;
  127. } else {
  128. log_info("Upgrade %x Failed\r\n", Type);
  129. //return FAIL;
  130. return FAIL;
  131. }
  132. if (uartFd > 0) {
  133. close(uartFd);
  134. }
  135. break;
  136. case 0x1000000B:
  137. case 0x1000000C:
  138. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  139. //bool isPass = true;
  140. CanFd = InitCanBus();
  141. if (CanFd > 0) {
  142. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  143. //if (!isPass) {
  144. // break;
  145. //}
  146. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  147. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB)) {
  148. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)pSysConfig->ModelName) == PASS) {
  149. //isPass = PASS;
  150. retSucc++;
  151. } else {
  152. log_info("Upgrade %x Failed\r\n", Type);
  153. //isPass = FAIL;
  154. retFail++;
  155. }
  156. }
  157. }
  158. } else {
  159. log_info("Upgrad FD fail. \n");
  160. //isPass = false;
  161. return FAIL;
  162. }
  163. //return isPass;
  164. break;
  165. }
  166. }
  167. free(new_str);
  168. free(ptr);
  169. }
  170. }
  171. free(dir);
  172. closedir(d);
  173. if (retFail != 0) {
  174. return FAIL;
  175. }
  176. return PASS;
  177. }
  178. void CheckFwUpdateFunction(void)
  179. {
  180. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  181. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  182. //log_info("pSysInfo->FirmwareUpdate = %d \n", pSysInfo->FirmwareUpdate);
  183. if (pSysInfo->FirmwareUpdate == YES) {
  184. log_info("ftp : update start. \n");
  185. for (uint8_t gun_index = 0; gun_index < pSysConfig->TotalConnectorCount; gun_index++) {
  186. setChargerMode(gun_index, MODE_UPDATE);
  187. }
  188. uint8_t updateResult = CheckUpdateProcess();
  189. if (updateResult == PASS) {
  190. log_info("ftp : update complete. \n");
  191. } else if (updateResult == MODELNAME_FAIL) {
  192. log_info("ftp : model name is none match. \n");
  193. KillAllTask();
  194. pSysInfo->FirmwareUpdate = NO;
  195. sleep(5);
  196. system("/usr/bin/run_evse_restart.sh");
  197. return;
  198. } else {
  199. log_info("ftp : update fail. \n");
  200. }
  201. pSysInfo->FirmwareUpdate = NO;
  202. sleep(5);
  203. system("reboot -f");
  204. } else if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES) {
  205. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  206. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL) {
  207. log_info("Backend : update start. \n");
  208. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  209. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  210. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  211. KillTask();
  212. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  213. setChargerMode(_index, MODE_UPDATE);
  214. }
  215. for (uint8_t _index = 0; _index < pSysConfig->AcConnectorCount; _index++) {
  216. ac_chargingInfo[_index]->SystemStatus = MODE_UPDATE;
  217. }
  218. uint8_t updateResult = CheckUpdateProcess();
  219. if (updateResult == PASS) {
  220. log_info("Backend : update complete. \n");
  221. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  222. } else if (updateResult == MODELNAME_FAIL) {
  223. log_info("Backend : model name is none match. \n");
  224. KillAllTask();
  225. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  226. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  227. sleep(5);
  228. system("/usr/bin/run_evse_restart.sh");
  229. return;
  230. } else {
  231. log_info("Backend : update fail. \n");
  232. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  233. }
  234. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  235. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  236. sleep(5);
  237. system("reboot -f");
  238. }
  239. }
  240. }