Module_EventLogging.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <termios.h>
  22. #include <errno.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <sqlite3.h>
  29. #include "define.h"
  30. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define EVENT_INFO(format, args...) StoreEventLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  34. #define Debug
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL -1
  38. #define DB_FILE "/Storage/EventLog/Eventlog.db"
  39. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  40. struct StatusCodeData *ShmStatusCodeData;
  41. sqlite3 *localDb;
  42. struct network_previous_status
  43. {
  44. int rssiWifi;
  45. int rssi4g;
  46. uint8_t isInternet:1;
  47. uint8_t isOcppConnected:1;
  48. uint8_t ethIsInternet:1;
  49. uint8_t maln0IsInternet:1;
  50. uint8_t ppp0IsInternet:1;
  51. }netPreviousStatus;
  52. void trim(char *s);
  53. int mystrcmp(char *p1,char *p2);
  54. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  55. void split(char **arr, char *str, const char *del);
  56. int StoreLogMsg(const char *fmt, ...)
  57. {
  58. char Buf[4096+256];
  59. char buffer[4096];
  60. time_t CurrentTime;
  61. struct tm *tm;
  62. struct timeval tv;
  63. va_list args;
  64. va_start(args, fmt);
  65. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  66. va_end(args);
  67. memset(Buf,0,sizeof(Buf));
  68. CurrentTime = time(NULL);
  69. tm=localtime(&CurrentTime);
  70. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  71. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  72. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  73. buffer,
  74. tm->tm_year+1900,tm->tm_mon+1);
  75. #ifdef SystemLogMessage
  76. system(Buf);
  77. #endif
  78. #ifdef ConsloePrintLog
  79. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  80. #endif
  81. return rc;
  82. }
  83. int StoreEventLogMsg(const char *fmt, ...)
  84. {
  85. char Buf[4096+256];
  86. char buffer[4096];
  87. time_t CurrentTime;
  88. struct tm *tm;
  89. struct timeval tv;
  90. va_list args;
  91. va_start(args, fmt);
  92. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  93. va_end(args);
  94. memset(Buf,0,sizeof(Buf));
  95. CurrentTime = time(NULL);
  96. tm=localtime(&CurrentTime);
  97. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  98. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  99. {
  100. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]%s_%s_EventLog",
  101. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  102. buffer,
  103. tm->tm_year+1900,tm->tm_mon+1,
  104. ShmSysConfigAndInfo->SysConfig.ModelName,
  105. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  106. }
  107. else
  108. {
  109. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  110. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  111. buffer,
  112. tm->tm_year+1900,tm->tm_mon+1);
  113. }
  114. #ifdef SystemLogMessage
  115. system(Buf);
  116. #endif
  117. #ifdef ConsloePrintLog
  118. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  119. #endif
  120. return rc;
  121. }
  122. int DiffTimeb(struct timeb ST, struct timeb ET)
  123. {
  124. //return milli-second
  125. unsigned int StartTime,StopTime;
  126. StartTime=(unsigned int)ST.time;
  127. StopTime=(unsigned int)ET.time;
  128. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  129. }
  130. //==========================================
  131. // Common routine
  132. //==========================================
  133. void trim(char *s)
  134. {
  135. int i=0, j, k, l=0;
  136. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  137. i++;
  138. j = strlen(s)-1;
  139. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  140. j--;
  141. if(i==0 && j==strlen(s)-1) { }
  142. else if(i==0) s[j+1] = '\0';
  143. else {
  144. for(k=i; k<=j; k++) s[l++] = s[k];
  145. s[l] = '\0';
  146. }
  147. }
  148. int mystrcmp(char *p1,char *p2)
  149. {
  150. while(*p1==*p2)
  151. {
  152. if(*p1=='\0' || *p2=='\0')
  153. break;
  154. p1++;
  155. p2++;
  156. }
  157. if(*p1=='\0' && *p2=='\0')
  158. return(PASS);
  159. else
  160. return(FAIL);
  161. }
  162. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  163. {
  164. strncpy(dest, src + start, cnt);
  165. dest[cnt] = 0;
  166. }
  167. void split(char **arr, char *str, const char *del)
  168. {
  169. char *s = strtok(str, del);
  170. while(s != NULL)
  171. {
  172. *arr++ = s;
  173. s = strtok(NULL, del);
  174. }
  175. }
  176. void AddFaultCodeToBuf(unsigned char *Code)
  177. {
  178. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount < 10)
  179. {
  180. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[ShmSysConfigAndInfo->SysWarningInfo.WarningCount][0], Code, 7);
  181. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  182. }
  183. }
  184. void RemoveFaultCodeToBuf(unsigned char *Code)
  185. {
  186. unsigned char find = 0x01;
  187. char _code[7];
  188. sprintf(_code,"%s", Code);
  189. while(find)
  190. {
  191. find = 0x00;
  192. for(unsigned char i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  193. {
  194. if (find == 0x00)
  195. {
  196. if(memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0)
  197. {
  198. find = 0x01;
  199. }
  200. }
  201. else
  202. {
  203. if(i == ShmSysConfigAndInfo->SysWarningInfo.WarningCount - 1)
  204. {
  205. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "", 7);
  206. }
  207. else
  208. {
  209. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i + 1][0], 7);
  210. }
  211. }
  212. }
  213. if (find)
  214. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  215. }
  216. }
  217. //==========================================
  218. // Init all share memory
  219. //==========================================
  220. int InitShareMemory()
  221. {
  222. int result = PASS;
  223. int MeterSMId;
  224. //creat ShmSysConfigAndInfo
  225. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  226. {
  227. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  228. result = FAIL;
  229. }
  230. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  231. {
  232. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  233. result = FAIL;
  234. }
  235. else
  236. {}
  237. //creat ShmStatusCodeData
  238. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  239. {
  240. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  241. result = FAIL;
  242. }
  243. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  244. {
  245. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  246. result = FAIL;
  247. }
  248. else
  249. {}
  250. //creat ShmStatusCodeData
  251. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  252. {
  253. DEBUG_ERROR("shmget ShmStatusCodeData NG");
  254. result = FAIL;
  255. }
  256. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  257. {
  258. DEBUG_ERROR("shmat ShmStatusCodeData NG");
  259. result = FAIL;
  260. }
  261. else
  262. {}
  263. memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
  264. return result;
  265. }
  266. //==========================================
  267. // SQLite routne
  268. //==========================================
  269. int DB_Event_Open(sqlite3 *db)
  270. {
  271. int result = PASS;
  272. char* errMsg = NULL;
  273. char* createRecordSql="CREATE TABLE IF NOT EXISTS event_record("
  274. "idx integer primary key AUTOINCREMENT, "
  275. "occurDatetime text NOT NULL, "
  276. "statusCode text NOT NULL"
  277. ");";
  278. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  279. if(sqlite3_open(DB_FILE, &db))
  280. {
  281. result = FAIL;
  282. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  283. sqlite3_close(db);
  284. }
  285. else
  286. {
  287. DEBUG_INFO( "Local event record database open successfully.\n");
  288. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  289. {
  290. result = FAIL;
  291. DEBUG_ERROR( "Create local event record table error message: %s\n", errMsg);
  292. }
  293. else
  294. {
  295. DEBUG_INFO( "Opened local event record table successfully\n");
  296. }
  297. sqlite3_close(db);
  298. }
  299. return result;
  300. }
  301. int DB_Event_Insert_Record(sqlite3 *db, uint8_t *statusCode)
  302. {
  303. int result = PASS;
  304. char* errMsg = NULL;
  305. char sqlStr[1024];
  306. sprintf(sqlStr, "insert into event_record(occurDatetime, statusCode) values(CURRENT_TIMESTAMP, '%s');", statusCode);
  307. if(sqlite3_open(DB_FILE, &db))
  308. {
  309. result = FAIL;
  310. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  311. sqlite3_close(db);
  312. }
  313. else
  314. {
  315. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  316. {
  317. result = FAIL;
  318. DEBUG_INFO( "Insert local event record error message: %s\n", errMsg);
  319. }
  320. sprintf(sqlStr, "delete from event_record where idx < (select idx from event_record order by idx desc limit 1)-2000;");
  321. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  322. {
  323. result = FAIL;
  324. DEBUG_INFO( "delete local event_record error message: %s\n", errMsg);
  325. }
  326. sqlite3_close(db);
  327. }
  328. return result;
  329. }
  330. int DB_Network_Open(sqlite3 *db)
  331. {
  332. int result = PASS;
  333. char* errMsg = NULL;
  334. char* createRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  335. "idx integer primary key AUTOINCREMENT, "
  336. "occurDatetime text NOT NULL, "
  337. "isInternet text NOT NULL, "
  338. "isOcppConnected text NOT NULL, "
  339. "isEth0Internet text NOT NULL, "
  340. "isMlan0Internet text NOT NULL, "
  341. "isPpp0Internet text NOT NULL, "
  342. "rssiMlan0 text NOT NULL, "
  343. "rssiPpp0 text NOT NULL"
  344. ");";
  345. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  346. if(sqlite3_open(DB_FILE, &db))
  347. {
  348. result = FAIL;
  349. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  350. sqlite3_close(db);
  351. }
  352. else
  353. {
  354. DEBUG_INFO( "Local network status record database open successfully.\n");
  355. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  356. {
  357. result = FAIL;
  358. DEBUG_ERROR( "Create local network status record table error message: %s\n", errMsg);
  359. }
  360. else
  361. {
  362. DEBUG_INFO( "Opened local network status record table successfully\n");
  363. }
  364. sqlite3_close(db);
  365. }
  366. return result;
  367. }
  368. int DB_Network_Insert_Record(sqlite3 *db)
  369. {
  370. int result = PASS;
  371. char* errMsg = NULL;
  372. char sqlStr[1024];
  373. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  374. {
  375. sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
  376. ShmSysConfigAndInfo->SysInfo.InternetConn,
  377. ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
  378. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
  379. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi,
  380. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi,
  381. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi,
  382. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  383. }
  384. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  385. {
  386. sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
  387. ShmSysConfigAndInfo->SysInfo.InternetConn,
  388. ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
  389. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
  390. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi,
  391. 0,
  392. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi,
  393. 0);
  394. }
  395. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  396. {
  397. sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
  398. ShmSysConfigAndInfo->SysInfo.InternetConn,
  399. ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
  400. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
  401. 0,
  402. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi,
  403. 0,
  404. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  405. }
  406. else
  407. {
  408. sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
  409. ShmSysConfigAndInfo->SysInfo.InternetConn,
  410. ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
  411. !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
  412. 0,
  413. 0,
  414. 0,
  415. 0);
  416. }
  417. if(sqlite3_open(DB_FILE, &db))
  418. {
  419. result = FAIL;
  420. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  421. sqlite3_close(db);
  422. }
  423. else
  424. {
  425. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  426. {
  427. result = FAIL;
  428. DEBUG_INFO( "Insert local network status record error message: %s\n", errMsg);
  429. }
  430. sprintf(sqlStr, "delete from network_record where idx < (select idx from network_record order by idx desc limit 1)-20000;");
  431. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  432. {
  433. result = FAIL;
  434. DEBUG_INFO( "delete local network_record error message: %s\n", errMsg);
  435. }
  436. sqlite3_close(db);
  437. }
  438. return result;
  439. }
  440. //================================================
  441. // Main process
  442. //================================================
  443. int main(void)
  444. {
  445. int ByteCount,BitCount;
  446. unsigned char tmp, EventCodeTmp[7], EventCodeDisp[7];
  447. if(InitShareMemory() == FAIL)
  448. {
  449. #ifdef SystemLogMessage
  450. DEBUG_ERROR("InitShareMemory NG\n");
  451. #endif
  452. if(ShmStatusCodeData!=NULL)
  453. {
  454. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  455. }
  456. sleep(5);
  457. return 0;
  458. }
  459. if(DB_Event_Open(localDb) != PASS)
  460. {
  461. DEBUG_ERROR("Local event database initial fail.\n");
  462. }
  463. if(DB_Network_Open(localDb) != PASS)
  464. {
  465. DEBUG_ERROR("Local network status database initial fail.\n");
  466. }
  467. for(;;)
  468. {
  469. //check Fault Status
  470. for(ByteCount=0;ByteCount<ARRAY_SIZE(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal);ByteCount++)
  471. {
  472. if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
  473. {
  474. tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
  475. for(BitCount=0;BitCount<8;BitCount++)
  476. {
  477. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
  478. {
  479. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  480. memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  481. memcpy(EventCodeDisp, EventCodeTmp, ARRAY_SIZE(EventCodeTmp));
  482. if(((tmp>>BitCount)&0x01)==0)//Recovered
  483. {
  484. EventCodeTmp[0]='1';
  485. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]&=~(1<<BitCount);
  486. RemoveFaultCodeToBuf(EventCodeDisp);
  487. }
  488. else
  489. {
  490. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]|=(1<<BitCount);
  491. AddFaultCodeToBuf(EventCodeDisp);
  492. }
  493. EVENT_INFO("%s\n", EventCodeTmp);
  494. DB_Event_Insert_Record(localDb, EventCodeTmp);
  495. }
  496. }
  497. }
  498. }
  499. //check Alarm Status
  500. for(ByteCount=0;ByteCount<ARRAY_SIZE(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal);ByteCount++)
  501. {
  502. if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
  503. {
  504. tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
  505. for(BitCount=0;BitCount<8;BitCount++)
  506. {
  507. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
  508. {
  509. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  510. memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  511. memcpy(EventCodeDisp, EventCodeTmp, ARRAY_SIZE(EventCodeTmp));
  512. if(((tmp>>BitCount)&0x01)==0)//Recovered
  513. {
  514. EventCodeTmp[0]='1';
  515. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]&=~(1<<BitCount);
  516. RemoveFaultCodeToBuf(EventCodeDisp);
  517. }
  518. else
  519. {
  520. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]|=(1<<BitCount);
  521. AddFaultCodeToBuf(EventCodeDisp);
  522. }
  523. EVENT_INFO("%s\n", EventCodeTmp);
  524. DB_Event_Insert_Record(localDb, EventCodeTmp);
  525. }
  526. }
  527. }
  528. }
  529. //check Info Status
  530. for(ByteCount=0;ByteCount<ARRAY_SIZE(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal);ByteCount++)
  531. {
  532. if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
  533. {
  534. tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
  535. for(BitCount=0;BitCount<8;BitCount++)
  536. {
  537. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
  538. {
  539. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  540. memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  541. memcpy(EventCodeDisp, EventCodeTmp, ARRAY_SIZE(EventCodeTmp));
  542. if(((tmp>>BitCount)&0x01)==0)//Recovered
  543. {
  544. EventCodeTmp[0]='1';
  545. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]&=~(1<<BitCount);
  546. RemoveFaultCodeToBuf(EventCodeDisp);
  547. }
  548. else
  549. {
  550. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]|=(1<<BitCount);
  551. AddFaultCodeToBuf(EventCodeDisp);
  552. }
  553. EVENT_INFO("%s\n", EventCodeTmp);
  554. DB_Event_Insert_Record(localDb, EventCodeTmp);
  555. }
  556. }
  557. }
  558. }
  559. if((netPreviousStatus.isInternet != ShmSysConfigAndInfo->SysInfo.InternetConn) ||
  560. (netPreviousStatus.isOcppConnected != ShmSysConfigAndInfo->SysInfo.OcppConnStatus) ||
  561. (netPreviousStatus.ethIsInternet != ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) ||
  562. (netPreviousStatus.maln0IsInternet != ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) ||
  563. (netPreviousStatus.ppp0IsInternet != ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) ||
  564. (netPreviousStatus.rssiWifi != ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi) ||
  565. (netPreviousStatus.rssi4g != ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi))
  566. {
  567. DB_Network_Insert_Record(localDb);
  568. netPreviousStatus.isInternet = ShmSysConfigAndInfo->SysInfo.InternetConn;
  569. netPreviousStatus.isOcppConnected = ShmSysConfigAndInfo->SysInfo.OcppConnStatus;
  570. netPreviousStatus.ethIsInternet = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  571. netPreviousStatus.maln0IsInternet = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi;
  572. netPreviousStatus.ppp0IsInternet = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi;
  573. netPreviousStatus.rssiWifi = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi;
  574. netPreviousStatus.rssi4g = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi;
  575. }
  576. usleep(100000);
  577. }
  578. return FAIL;
  579. }