EventLogging.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. EventLogging.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <linux/termios.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/sockios.h>
  19. #include <linux/socket.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <sys/time.h>
  23. #include <sys/timeb.h>
  24. #include <math.h>//for pow
  25. #include <unistd.h>
  26. #include "define.h"
  27. #include "EventLogging.h"
  28. //#define Debug
  29. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  30. struct StatusCodeData *ShmStatusCodeData;
  31. unsigned char buf_log_eventlogging[SIZE_OF_LOG_BUFFER];
  32. /**************************************************************************************/
  33. /**************************Alarm Share memory**************************************/
  34. /***************************************************************************************
  35. Status Code A B C D E F
  36. 0: Issue 1: From EVSE 1: Fault (unrecoverable) 001 ~ 999 serial number
  37. e.g., hardware broken, system latch
  38. 1: Recovered 2: From EV 2: Alarm (recoverable)
  39. e.g., OTP, OVP
  40. 3: From Backend 3: Information
  41. e.g., swipe card to stop charging
  42. according to XXX.Revxx
  43. ***************************************************************************************/
  44. /**************************************************************************************/
  45. char FaultStatusCode[32][6]=
  46. {
  47. "011001", //CHAdeMO output fuse blew
  48. "011002", //CCS output fuse blew
  49. "011003", //GB output fuse blew
  50. "011004", //RCD/CCID self-test fail
  51. "011005", //AC input contactor 1 welding
  52. "011006", //AC input contactor 1 driving fault
  53. "011007", //AC input contactor 2 welding
  54. "011008", //AC input contactor 2 driving fault
  55. "011009", //AC output relay welding
  56. "011010", //AC output relay driving fault
  57. "011011", //CHAdeMO output relay welding
  58. "011012", //CHAdeMO output relay driving fault
  59. "011013", //CCS output relay welding
  60. "011014", //CCS output relay driving fault
  61. "011015", //GB output relay welding
  62. "011016", //GB output relay driving fault
  63. "011017", //AC connector temperature sensor broken
  64. "011018", //CHAdeMO connector temperature sensor broken
  65. "011019", //CCS connector temperature sensor broken
  66. "011020", //GB connector temperature sensor broken
  67. "011021", //WiFi module broken
  68. "011022", //3G/4G module broken
  69. "011023", //Aux. power module broken
  70. "011024", //Relay control module /smart box broken
  71. "011025", //CHAdeMO connector lock fail
  72. "011026", //GB connector lock fail
  73. "011027", //AC connector lock fail
  74. "011028", //CHAdeMO module broken
  75. "011029", //CCS module broken
  76. "011030", //GBT module broken
  77. "011031", //PSU module broken
  78. "011032" //Reserved
  79. };
  80. char AlarmStatusCode[64][6]=
  81. {
  82. "012200", //System L1 input OVP
  83. "012201", //System L2 input OVP
  84. "012202", //System L3 input OVP
  85. "012203", //System L1 input UVP
  86. "012204", //System L2 input UVP
  87. "012205", //System L3 input UVP
  88. "012206", //PSU L1 input OVP
  89. "012207", //PSU L2 input OVP
  90. "012208", //PSU L3 input OVP
  91. "012209", //PSU L1 input UVP
  92. "012210", //PSU L2 input UVP
  93. "012211", //PSU L3 input UVP
  94. "012212", //System L1 input drop
  95. "012213", //System L2 input drop
  96. "012214", //System L3 input drop
  97. "012215", //System AC output OVP
  98. "012216", //System AC output OCP
  99. "012217", //System CHAdeMO output OVP
  100. "012218", //System CHAdeMO output OCP
  101. "012219", //System CCS output OVP
  102. "012220", //System CCS output OCP
  103. "012221", //System GB output OVP
  104. "012222", //System GB output OCP
  105. "012223", //System ambient/inlet OTP
  106. "012224", //System critical point OTP
  107. "012225", //PSU ambient/inlet OTP
  108. "012226", //PSU critical point OTP
  109. "012227", //Aux. power module OTP
  110. "012228", //Relay board/smart box OTP
  111. "012229", //CHAdeMO connector OTP
  112. "012230", //CCS connector OTP
  113. "012231", //GB connector OTP
  114. "012232", //AC connector OTP
  115. "012233", //RCD/CCID trip
  116. "012234", //CHAdeMO GFD trip
  117. "012235", //CCS GFD trip
  118. "012236", //GB GFD trip
  119. "012237", //SPD trip
  120. "012238", //Main power breaker trip
  121. "012239", //Aux. power breaker trip
  122. "012240", //PSU communication fail
  123. "012241", //WiFi module communication fail
  124. "012242", //3G/4G module communication fail
  125. "012243", //RFID module communication fail
  126. "012244", //Bluetooth module communication fail
  127. "012245", //LCM module communication fail
  128. "012246", //Aux. power module communication fail
  129. "012247", //Relay control boaed/smart box communication fail
  130. "012248", //CCS module communication fail
  131. "012249", //CHAdeMO module communication fail
  132. "012250", //GBT module communication fail
  133. "012251", //Emergency stop
  134. "012252", //Door open
  135. "012253", //System fan decay
  136. "012254", //Fail to create share memory
  137. "012255", //CSU initialization failed
  138. "012256", //Reserved
  139. "012257", //Reserved
  140. "012258", //Reserved
  141. "012259", //Reserved
  142. "012260", //Reserved
  143. "012261", //Reserved
  144. "012262", //Reserved
  145. "012263" //Reserved
  146. };
  147. char InfoStatusCode[64][6]=
  148. {
  149. //Information comes from EVSE
  150. "013600", //Normal stop charging by user
  151. "013601", //Charging Time's up
  152. "013602", //Replace system air filter
  153. "013603", //Reach to CHAdeMO max. plugging times.
  154. "013604", //Reach to CCS max. plugging times.
  155. "013605", //Reach to GB max. plugging times.
  156. "013606", //Reach to AC max. plugging times.
  157. "013607", //CSU fimrware update fail
  158. "013608", //CHAdeMO Module fimrware update fail
  159. "013609", //CCS Module fimrware update fail
  160. "013610", //GB Module fimrware update fail
  161. "013611", //Aux. power module fimrware update fail
  162. "013612", //Relay control module fimrware update fail
  163. "013613", //LCM module fimrware update fail
  164. "013614", //Bluetooth module fimrware update fail
  165. "013615", //WiFi module fimrware update fail
  166. "013616", //3G/4G module fimrware update fail
  167. "013617", //SMR fimrware update fail
  168. "013618", //RFID module fimrware update fail
  169. "013619", //configured by USB flash drive
  170. "013620", //configured by backend
  171. "013621", //configured by webpage
  172. "013622", //disconnected from Internet through Ethernet
  173. "013623", //disconnected from Internet through WiFi
  174. "013624", //disconnected from Internet through 3G/4G
  175. "013625", //disconnected from AP through WiFi
  176. "013626", //disconnected from APN through 3G/4G
  177. "013627", //Reserved
  178. "013628", //Reserved
  179. "013629", //Reserved
  180. "013630", //Reserved
  181. "013631", //Reserved
  182. //Information comes from EV
  183. "023700", //CHAdeMO EV communication Fail
  184. "023701", //CCS EV communication Fail
  185. "023702", //GB EV communication Fail
  186. "023703", //AC: pilot fault
  187. "023704", //CHAdeMO: battery malfunction
  188. "023705", //CHAdeMO: no charging permission
  189. "023706", //CHAdeMO: battery incompatibility
  190. "023707", //CHAdeMO: battery OVP
  191. "023708", //CHAdeMO: battery UVP
  192. "023709", //CHAdeMO: battery OTP
  193. "023710", //CHAdeMO: battery current difference
  194. "023711", //CHAdeMO: battery voltage difference
  195. "023712", //CHAdeMO: shift position
  196. "023713", //CHAdeMO: battery other fault
  197. "023714", //CHAdeMO: charging system error
  198. "023715", //CHAdeMO: EV normal stop
  199. "023716", //Reserved
  200. "023717", //Reserved
  201. "023718", //Reserved
  202. "023719", //Reserved
  203. "023720", //Reserved
  204. "023721", //Reserved
  205. "023722", //Reserved
  206. "023723", //Reserved
  207. //Information comes from Backend
  208. "033900", //disconnected from backend through Ethernet
  209. "033901", //disconnected from backend through WiFi
  210. "033902", //disconnected from backend through 3G/4G
  211. "033903", //Remote start charging by backend
  212. "033904", //Remote stop charging by backend
  213. "033905", //Remote reset by backend
  214. "033906", //Reserved
  215. "033907", //Reserved
  216. };
  217. /*===========================================================================
  218. FUNCTION: StoreLogMsg
  219. DESCRIPTION:
  220. PRE-CONDITION:
  221. INPUT:
  222. OUTPUT:
  223. GLOBAL VARIABLES:
  224. =============================================================================*/
  225. #if SAVE_SYS_LOG_MSG_EVENTLOG_SWITCH == ENABLE
  226. int StoreLogMsg(unsigned char *DataString)
  227. {
  228. static unsigned char Buf[1024];
  229. static time_t CurrentTime;
  230. static struct tm *tm;
  231. static struct timeval tv;
  232. memset(Buf, 0, sizeof(Buf));
  233. CurrentTime = time(NULL);
  234. tm = localtime(&CurrentTime);
  235. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  236. sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][EventLogging]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  237. tm->tm_year + 1900,
  238. tm->tm_mon + 1,
  239. tm->tm_mday,
  240. tm->tm_hour,
  241. tm->tm_min,
  242. tm->tm_sec,
  243. tv.tv_usec,
  244. DataString,
  245. tm->tm_year + 1900,
  246. tm->tm_mon + 1);
  247. system(Buf);
  248. DEBUG_PRINTF_EVENTLOG_SYSTEM_LOG("[%02d:%02d:%02d.%06d][EventLogging]%s \n",
  249. tm->tm_hour,
  250. tm->tm_min,
  251. tm->tm_sec,
  252. tv.tv_usec,
  253. DataString);
  254. //Reset the buf_log_eventlogging Buffer, i.e. DataString
  255. memset(buf_log_eventlogging, 0, SIZE_OF_LOG_BUFFER);
  256. }
  257. #endif
  258. int StoreEventLogMsg(unsigned char *EventCodeString)
  259. {
  260. unsigned char Buf[256];
  261. time_t CurrentTime;
  262. struct tm *tm;
  263. memset(Buf, 0, sizeof(Buf));
  264. CurrentTime = time(NULL);
  265. tm = localtime(&CurrentTime);
  266. sprintf(Buf, "echo \"%04d.%02d.%02d %02d:%02d:%02d - %s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  267. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  268. EventCodeString,
  269. tm->tm_year + 1900, tm->tm_mon + 1);
  270. system(Buf);
  271. DEBUG_PRINTF_EVENTLOG_DETAIL("%s \n", Buf);
  272. }
  273. double DiffTimeb(struct timeb ST, struct timeb ET)
  274. {
  275. //return milli-second
  276. double StartTime, EndTime;
  277. double t_diff;
  278. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  279. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  280. t_diff = EndTime - StartTime;
  281. //printf("%.02lf - %.02lf = %.02lf\n", EndTime, StartTime, t_diff);
  282. if (t_diff < 0)
  283. {
  284. #if 0
  285. if (t_diff < -1000) //1000ms
  286. {
  287. sprintf(buf_log_eventlogging,
  288. "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  289. StartTime,
  290. EndTime,
  291. t_diff);
  292. SAVE_SYS_LOG_MSG_EVENTLOG(buf_log_eventlogging);
  293. }
  294. #endif
  295. return -1;
  296. }
  297. return t_diff;
  298. }
  299. /**************************************************************************************/
  300. /**************************Init all share memory *********************************/
  301. /**************************************************************************************/
  302. int ShareMemory_Init()
  303. {
  304. int MeterSMId;
  305. //create ShmSysConfigAndInfo
  306. if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  307. {
  308. StoreLogMsg("[EventLogging]ShareMemory_Init:shmget ShmSysConfigAndInfo NG");
  309. return 0;
  310. }
  311. else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  312. {
  313. StoreLogMsg("[EventLogging]ShareMemory_Init:shmat ShmSysConfigAndInfo NG");
  314. return 0;
  315. }
  316. //create ShmStatusCodeData
  317. if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  318. {
  319. StoreLogMsg("[EventLogging]ShareMemory_Init:shmget ShmStatusCodeData NG");
  320. return 0;
  321. }
  322. else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  323. {
  324. StoreLogMsg("[EventLogging]ShareMemory_Init:shmat ShmStatusCodeData NG");
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. void FlashPresentStatusCode(unsigned char *NewCode)
  330. {
  331. unsigned char CodeIndex, SaveIndex = 0;
  332. unsigned char StatusCodeTmp[10][6];
  333. memset(StatusCodeTmp, 0, sizeof(StatusCodeTmp));
  334. for(CodeIndex = 0; CodeIndex < (sizeof(ShmStatusCodeData->PresentStatusCode) / 6); CodeIndex++)
  335. {
  336. if(strlen(ShmStatusCodeData->PresentStatusCode[CodeIndex]) <= 0)
  337. {
  338. memcpy(StatusCodeTmp[SaveIndex++], NewCode, 6);
  339. memset(ShmStatusCodeData->PresentStatusCode, 0, sizeof(ShmStatusCodeData->PresentStatusCode));
  340. memcpy(ShmStatusCodeData->PresentStatusCode, StatusCodeTmp, sizeof(StatusCodeTmp));
  341. return;
  342. }
  343. else if(strstr(ShmStatusCodeData->PresentStatusCode[CodeIndex], NewCode + 1) != NULL)
  344. {
  345. if((CodeIndex + 1) < 10)
  346. {
  347. memcpy(StatusCodeTmp[SaveIndex], ShmStatusCodeData->PresentStatusCode[CodeIndex + 1], (9 - CodeIndex) * 6);
  348. }
  349. memset(ShmStatusCodeData->PresentStatusCode, 0, sizeof(ShmStatusCodeData->PresentStatusCode));
  350. memcpy(ShmStatusCodeData->PresentStatusCode, StatusCodeTmp, sizeof(StatusCodeTmp));
  351. return;
  352. }
  353. else
  354. {
  355. memcpy(StatusCodeTmp[SaveIndex++], ShmStatusCodeData->PresentStatusCode[CodeIndex], 6);
  356. }
  357. }
  358. }
  359. int main(int argc, char *argv[])
  360. {
  361. int ByteCount, BitCount;
  362. unsigned char tmp, EventCodeTmp[7];
  363. //Initialization
  364. if(ShareMemory_Init() == 0)
  365. {
  366. StoreLogMsg("[EventLogging]main:ShareMemory_Init NG");
  367. if(ShmStatusCodeData != NULL)
  368. {
  369. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  370. }
  371. sleep(5);
  372. return 0;
  373. }
  374. while(1)
  375. {
  376. //check Fault Status
  377. for(ByteCount = 0; ByteCount < 4; ByteCount++)
  378. {
  379. if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
  380. {
  381. tmp = ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
  382. for(BitCount = 0; BitCount < 8; BitCount++)
  383. {
  384. if(((tmp >> BitCount) & 0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] >> BitCount) & 0x01))
  385. {
  386. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  387. memcpy(EventCodeTmp, FaultStatusCode[ByteCount * 8 + BitCount], sizeof(EventCodeTmp) - 1);
  388. if(((tmp >> BitCount) & 0x01) == 0)//Recovered
  389. {
  390. EventCodeTmp[0] = 1;
  391. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] &= (0 << BitCount);
  392. }
  393. else
  394. {
  395. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] |= (1 << BitCount);
  396. }
  397. FlashPresentStatusCode(EventCodeTmp);
  398. StoreEventLogMsg(EventCodeTmp);
  399. }
  400. }
  401. }
  402. }
  403. //check Alarm Status
  404. for(ByteCount = 0; ByteCount < 8; ByteCount++)
  405. {
  406. if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
  407. {
  408. tmp = ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
  409. for(BitCount = 0; BitCount < 8; BitCount++)
  410. {
  411. if(((tmp >> BitCount) & 0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] >> BitCount) & 0x01))
  412. {
  413. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  414. memcpy(EventCodeTmp, AlarmStatusCode[ByteCount * 8 + BitCount], sizeof(EventCodeTmp) - 1);
  415. if(((tmp >> BitCount) & 0x01) == 0)//Recovered
  416. {
  417. EventCodeTmp[0] = 1;
  418. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] &= (0 << BitCount);
  419. }
  420. else
  421. {
  422. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] |= (1 << BitCount);
  423. }
  424. FlashPresentStatusCode(EventCodeTmp);
  425. StoreEventLogMsg(EventCodeTmp);
  426. }
  427. }
  428. }
  429. }
  430. //check Info Status
  431. for(ByteCount = 0; ByteCount < 8; ByteCount++)
  432. {
  433. if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
  434. {
  435. tmp = ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
  436. for(BitCount = 0; BitCount < 8; BitCount++)
  437. {
  438. if(((tmp >> BitCount) & 0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] >> BitCount) & 0x01))
  439. {
  440. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  441. memcpy(EventCodeTmp, InfoStatusCode[ByteCount * 8 + BitCount], sizeof(EventCodeTmp) - 1);
  442. if(((tmp >> BitCount) & 0x01) == 0)//Recovered
  443. {
  444. EventCodeTmp[0] = 1;
  445. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] &= (0 << BitCount);
  446. }
  447. else
  448. {
  449. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] |= (1 << BitCount);
  450. }
  451. FlashPresentStatusCode(EventCodeTmp);
  452. StoreEventLogMsg(EventCodeTmp);
  453. }
  454. }
  455. }
  456. }
  457. }//main while loop
  458. }