Module_Diagnostics.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Module_Diagnostics.c
  3. *
  4. * Created on: 2022年12月12日
  5. * Author: 7978
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h> /*標準輸入輸出定義*/
  24. #include <stdlib.h> /*標準函數庫定義*/
  25. #include <unistd.h> /*Unix 標準函數定義*/
  26. #include <fcntl.h> /*檔控制定義*/
  27. #include <termios.h> /*PPSIX 終端控制定義*/
  28. #include <errno.h> /*錯誤號定義*/
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include <stdbool.h>
  36. #include "../../define.h"
  37. #include "Config.h"
  38. #include "Common.h"
  39. #include "Module_EvComm.h"
  40. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  41. struct StatusCodeData *ShmStatusCodeData;
  42. struct PrimaryMcuData *ShmPrimaryMcuData;
  43. struct PsuData *ShmPsuData;
  44. struct RelayModuleData *ShmRelayModuleData[2];
  45. struct FanModuleData *ShmFanModuleData;
  46. struct LedModuleData *ShmLedModuleData;
  47. ChargerInfoData *ShmChargerInfo;
  48. struct OCPP16Data *ShmOCPP16Data;
  49. struct OCPP20Data *ShmOCPP20Data;
  50. struct OCPP16Data *ShmOCPP16DataPH;
  51. int InitSysConfigAndInfoShareMemory(void)
  52. {
  53. int result = PASS;
  54. int MeterSMId;
  55. //initial ShmSysConfigAndInfo
  56. if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  57. {
  58. result = FAIL;
  59. }
  60. else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  61. {
  62. result = FAIL;
  63. }
  64. return result;
  65. }
  66. int InitStatusCodeShareMemory(void)
  67. {
  68. int result = PASS;
  69. int MeterSMId;
  70. //initial ShmStatusCodeData
  71. if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  72. {
  73. result = FAIL;
  74. }
  75. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  76. {
  77. result = FAIL;
  78. }
  79. return result;
  80. }
  81. int InitPrimaryMcuShareMemory(void)
  82. {
  83. int result = PASS;
  84. int MeterSMId;
  85. //initial ShmPrimaryMcuData
  86. if((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  87. {
  88. result = FAIL;
  89. }
  90. else if((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  91. {
  92. result = FAIL;
  93. }
  94. return result;
  95. }
  96. int InitPsuShareMemory(void)
  97. {
  98. int result = PASS;
  99. int MeterSMId;
  100. //initial ShmPsuData
  101. if((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  102. {
  103. result = FAIL;
  104. }
  105. else if((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  106. {
  107. result = FAIL;
  108. }
  109. return result;
  110. }
  111. int InitRelayModuleShareMemory(void)
  112. {
  113. int result = PASS;
  114. int MeterSMId;
  115. //initial ShmRelayModuleData
  116. if((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  117. {
  118. result = FAIL;
  119. }
  120. else if((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  121. {
  122. result = FAIL;
  123. }
  124. return result;
  125. }
  126. int InitRelay2ModuleShareMemory(void)
  127. {
  128. int result = PASS;
  129. int MeterSMId;
  130. //initial ShmRelayModuleData
  131. if((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  132. {
  133. result = FAIL;
  134. }
  135. else if((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  136. {
  137. result = FAIL;
  138. }
  139. return result;
  140. }
  141. int InitFanModuleShareMemory(void)
  142. {
  143. int result = PASS;
  144. int MeterSMId;
  145. //initial ShmFanModuleData
  146. if((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  147. {
  148. result = FAIL;
  149. }
  150. else if((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  151. {
  152. result = FAIL;
  153. }
  154. return result;
  155. }
  156. int InitLedModuleShareMemory(void)
  157. {
  158. int result = PASS;
  159. int MeterSMId;
  160. //initial ShmLedModuleData
  161. if((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), 0777)) < 0)
  162. {
  163. result = FAIL;
  164. }
  165. else if((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  166. {
  167. result = FAIL;
  168. }
  169. return result;
  170. }
  171. int InitChargerInfoShareMemory(void)
  172. {
  173. int result = PASS;
  174. int MeterSMId;
  175. //initial ShmChargerInfo
  176. if((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  177. {
  178. result = FAIL;
  179. }
  180. else if((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  181. {
  182. result = FAIL;
  183. }
  184. return result;
  185. }
  186. int InitOCPP16ShareMemory(void)
  187. {
  188. int result = PASS;
  189. int MeterSMId;
  190. //initial ShmOCPP16Data
  191. if((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  192. {
  193. result = FAIL;
  194. }
  195. else if((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  196. {
  197. result = FAIL;
  198. }
  199. return result;
  200. }
  201. int InitOCPP16PHShareMemory(void)
  202. {
  203. int result = PASS;
  204. int MeterSMId;
  205. //initial ShmOCPP16Data
  206. if((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  207. {
  208. result = FAIL;
  209. }
  210. else if((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  211. {
  212. result = FAIL;
  213. }
  214. return result;
  215. }
  216. int InitOCPP20ShareMemory(void)
  217. {
  218. int result = PASS;
  219. int MeterSMId;
  220. //initial ShmOCPP20Data
  221. if((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0)
  222. {
  223. result = FAIL;
  224. }
  225. else if((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  226. {
  227. result = FAIL;
  228. }
  229. return result;
  230. }
  231. int InitShareMemory(void)
  232. {
  233. int result = PASS;
  234. //initial ShmSysConfigAndInfo
  235. if(InitSysConfigAndInfoShareMemory() == FAIL)
  236. {
  237. result = FAIL;
  238. }
  239. //initial ShmStatusCodeData
  240. if(InitStatusCodeShareMemory() == FAIL)
  241. {
  242. result = FAIL;
  243. }
  244. //initial ShmPrimaryMcuData
  245. if(InitPrimaryMcuShareMemory() == FAIL)
  246. {
  247. result = FAIL;
  248. }
  249. //initial ShmPsuData
  250. if(InitPsuShareMemory() == FAIL)
  251. {
  252. result = FAIL;
  253. }
  254. //initial ShmRelayModuleData
  255. if(InitRelayModuleShareMemory() == FAIL)
  256. {
  257. result = FAIL;
  258. }
  259. if(InitRelay2ModuleShareMemory() == FAIL)
  260. {
  261. result = FAIL;
  262. }
  263. //initial ShmFanModuleData
  264. if(InitFanModuleShareMemory() == FAIL)
  265. {
  266. result = FAIL;
  267. }
  268. //initial ShmLedModuleData
  269. if(InitLedModuleShareMemory() == FAIL)
  270. {
  271. result = FAIL;
  272. }
  273. //initial ShmChargerInfo
  274. if(InitChargerInfoShareMemory() == FAIL)
  275. {
  276. result = FAIL;
  277. }
  278. //initial ShmOCPP16Data
  279. if(InitOCPP16ShareMemory() == FAIL)
  280. {
  281. result = FAIL;
  282. }
  283. if(InitOCPP16PHShareMemory() == FAIL)
  284. {
  285. result = FAIL;
  286. }
  287. //initial ShmOCPP20Data
  288. if(InitOCPP20ShareMemory() == FAIL)
  289. {
  290. result = FAIL;
  291. }
  292. return result;
  293. }
  294. // ./Module_Diagnostics X AAA.AAA.AAA.AAA
  295. // Dispenser ID: X, start from 1 ~ 4
  296. // Dispenser IP: AAA.AAA.AAA.AAA
  297. int main(int argc, char *argv[])
  298. {
  299. int success = FAIL;
  300. bool _find = false;
  301. int _dispenserId = 0;
  302. char _address[128];
  303. char dispenserStr[64];
  304. memset(_address, 0x00, sizeof(_address));
  305. memset(dispenserStr, 0x00, sizeof(dispenserStr));
  306. if(InitShareMemory() == FAIL)
  307. {
  308. LOG_ERROR("InitShareMemory NG");
  309. return FAIL;
  310. }
  311. if(argc == 3)
  312. {
  313. _dispenserId = atoi(argv[1]);
  314. strcpy(_address, argv[2]);
  315. if(_dispenserId > 0)
  316. {
  317. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  318. {
  319. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  320. {
  321. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == inet_addr(_address) &&
  322. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex == (_dispenserId - 1))
  323. {
  324. _find = true;
  325. }
  326. }
  327. }
  328. }
  329. if(_find)
  330. {
  331. sprintf(dispenserStr, "Dispenser %d ", _dispenserId);
  332. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Confirm = ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Request;
  333. }
  334. LOG_INFO("Set Diagnostics Request to %s[%s]", dispenserStr, _address);
  335. char cmdBuf[512] = {0};
  336. sprintf(cmdBuf, "curl -k -X POST --data \"logCnt=2\" https://%s/log_download_action.php", _address);
  337. if(system(cmdBuf) == 0)
  338. {
  339. success = PASS;
  340. }
  341. LOG_INFO("%s[%s] Latest log file is %s", dispenserStr, _address, success == PASS ? "OK" : "NG");
  342. if(success == PASS)
  343. {
  344. struct timeb SeqEndTime;
  345. struct tm *tm;
  346. ftime(&SeqEndTime);
  347. SeqEndTime.time = time(NULL);
  348. tm=localtime(&SeqEndTime.time);
  349. char _fileName[64];
  350. if(_find)
  351. {
  352. sprintf(_fileName, "%d", _dispenserId);
  353. }
  354. else
  355. {
  356. sprintf(_fileName, "[%02d.%02d]", tm->tm_mon + 1, tm->tm_mday);
  357. }
  358. sprintf((char*)cmdBuf,"ftpget -u root -p y42j/4cj84 %s \"/Storage/SystemLog/[%04d.%02d]Dispenser%sLog.zip\" /mnt/log.zip",
  359. _address, tm->tm_year + 1900, tm->tm_mon + 1, _fileName);
  360. if(system(cmdBuf) != 0)
  361. {
  362. success = FAIL;
  363. }
  364. LOG_INFO("Diagnostics %s[%s] %s", dispenserStr, _address, success == PASS ? "OK" : "NG");
  365. if(success)
  366. {
  367. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Completed = ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Confirm;
  368. }
  369. }
  370. }
  371. else
  372. {
  373. LOG_INFO("Diagnostics input parameter error!");
  374. }
  375. return success;
  376. }