Module_EventLogging.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  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. void trim(char *s);
  43. int mystrcmp(char *p1,char *p2);
  44. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  45. void split(char **arr, char *str, const char *del);
  46. int StoreLogMsg(const char *fmt, ...)
  47. {
  48. char Buf[4096+256];
  49. char buffer[4096];
  50. time_t CurrentTime;
  51. struct tm *tm;
  52. struct timeval tv;
  53. va_list args;
  54. va_start(args, fmt);
  55. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  56. va_end(args);
  57. memset(Buf,0,sizeof(Buf));
  58. CurrentTime = time(NULL);
  59. tm=localtime(&CurrentTime);
  60. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  61. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  62. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  63. buffer,
  64. tm->tm_year+1900,tm->tm_mon+1);
  65. #ifdef SystemLogMessage
  66. system(Buf);
  67. #endif
  68. #ifdef ConsloePrintLog
  69. 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);
  70. #endif
  71. return rc;
  72. }
  73. int StoreEventLogMsg(const char *fmt, ...)
  74. {
  75. char Buf[4096+256];
  76. char buffer[4096];
  77. time_t CurrentTime;
  78. struct tm *tm;
  79. struct timeval tv;
  80. va_list args;
  81. va_start(args, fmt);
  82. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  83. va_end(args);
  84. memset(Buf,0,sizeof(Buf));
  85. CurrentTime = time(NULL);
  86. tm=localtime(&CurrentTime);
  87. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  88. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  89. {
  90. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]%s_%s_EventLog",
  91. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  92. buffer,
  93. tm->tm_year+1900,tm->tm_mon+1,
  94. ShmSysConfigAndInfo->SysConfig.ModelName,
  95. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  96. }
  97. else
  98. {
  99. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  100. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  101. buffer,
  102. tm->tm_year+1900,tm->tm_mon+1);
  103. }
  104. #ifdef SystemLogMessage
  105. system(Buf);
  106. #endif
  107. #ifdef ConsloePrintLog
  108. 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);
  109. #endif
  110. return rc;
  111. }
  112. int DiffTimeb(struct timeb ST, struct timeb ET)
  113. {
  114. //return milli-second
  115. unsigned int StartTime,StopTime;
  116. StartTime=(unsigned int)ST.time;
  117. StopTime=(unsigned int)ET.time;
  118. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  119. }
  120. //==========================================
  121. // Common routine
  122. //==========================================
  123. void trim(char *s)
  124. {
  125. int i=0, j, k, l=0;
  126. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  127. i++;
  128. j = strlen(s)-1;
  129. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  130. j--;
  131. if(i==0 && j==strlen(s)-1) { }
  132. else if(i==0) s[j+1] = '\0';
  133. else {
  134. for(k=i; k<=j; k++) s[l++] = s[k];
  135. s[l] = '\0';
  136. }
  137. }
  138. int mystrcmp(char *p1,char *p2)
  139. {
  140. while(*p1==*p2)
  141. {
  142. if(*p1=='\0' || *p2=='\0')
  143. break;
  144. p1++;
  145. p2++;
  146. }
  147. if(*p1=='\0' && *p2=='\0')
  148. return(PASS);
  149. else
  150. return(FAIL);
  151. }
  152. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  153. {
  154. strncpy(dest, src + start, cnt);
  155. dest[cnt] = 0;
  156. }
  157. void split(char **arr, char *str, const char *del)
  158. {
  159. char *s = strtok(str, del);
  160. while(s != NULL)
  161. {
  162. *arr++ = s;
  163. s = strtok(NULL, del);
  164. }
  165. }
  166. void AddFaultCodeToBuf(unsigned char *Code)
  167. {
  168. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount < 10)
  169. {
  170. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[ShmSysConfigAndInfo->SysWarningInfo.WarningCount][0], Code, 7);
  171. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  172. }
  173. }
  174. void RemoveFaultCodeToBuf(unsigned char *Code)
  175. {
  176. unsigned char find = 0x01;
  177. char _code[7];
  178. sprintf(_code,"%s", Code);
  179. while(find)
  180. {
  181. find = 0x00;
  182. for(unsigned char i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  183. {
  184. if (find == 0x00)
  185. {
  186. if(memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0)
  187. {
  188. find = 0x01;
  189. }
  190. }
  191. else
  192. {
  193. if(i == ShmSysConfigAndInfo->SysWarningInfo.WarningCount - 1)
  194. {
  195. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "", 7);
  196. }
  197. else
  198. {
  199. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i + 1][0], 7);
  200. }
  201. }
  202. }
  203. if (find)
  204. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  205. }
  206. }
  207. //==========================================
  208. // Init all share memory
  209. //==========================================
  210. int InitShareMemory()
  211. {
  212. int result = PASS;
  213. int MeterSMId;
  214. //creat ShmSysConfigAndInfo
  215. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  216. {
  217. #ifdef SystemLogMessage
  218. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  219. #endif
  220. result = FAIL;
  221. }
  222. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  223. {
  224. #ifdef SystemLogMessage
  225. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  226. #endif
  227. result = FAIL;
  228. }
  229. else
  230. {}
  231. //creat ShmStatusCodeData
  232. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  233. {
  234. #ifdef SystemLogMessage
  235. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  236. #endif
  237. result = FAIL;
  238. }
  239. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  240. {
  241. #ifdef SystemLogMessage
  242. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  243. #endif
  244. result = FAIL;
  245. }
  246. else
  247. {}
  248. //creat ShmStatusCodeData
  249. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  250. {
  251. #ifdef SystemLogMessage
  252. DEBUG_ERROR("shmget ShmStatusCodeData NG");
  253. #endif
  254. result = FAIL;
  255. }
  256. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  257. {
  258. #ifdef SystemLogMessage
  259. DEBUG_ERROR("shmat ShmStatusCodeData NG");
  260. #endif
  261. result = FAIL;
  262. }
  263. else
  264. {}
  265. memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
  266. return result;
  267. }
  268. //==========================================
  269. // SQLite routne
  270. //==========================================
  271. int DB_Open(sqlite3 *db)
  272. {
  273. int result = PASS;
  274. char* errMsg = NULL;
  275. char* createRecordSql="CREATE TABLE IF NOT EXISTS event_record("
  276. "idx integer primary key AUTOINCREMENT, "
  277. "occurDatetime text NOT NULL, "
  278. "statusCode text NOT NULL"
  279. ");";
  280. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  281. if(sqlite3_open(DB_FILE, &db))
  282. {
  283. result = FAIL;
  284. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  285. sqlite3_close(db);
  286. }
  287. else
  288. {
  289. DEBUG_INFO( "Local event record database open successfully.\n");
  290. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  291. {
  292. result = FAIL;
  293. DEBUG_ERROR( "Create local event record table error message: %s\n", errMsg);
  294. }
  295. else
  296. {
  297. DEBUG_INFO( "Opened local event record table successfully\n");
  298. }
  299. sqlite3_close(db);
  300. }
  301. return result;
  302. }
  303. int DB_Insert_Record(sqlite3 *db, uint8_t *statusCode)
  304. {
  305. int result = PASS;
  306. char* errMsg = NULL;
  307. char sqlStr[1024];
  308. sprintf(sqlStr, "insert into event_record(occurDatetime, statusCode) values(CURRENT_TIMESTAMP, '%s');", statusCode);
  309. if(sqlite3_open(DB_FILE, &db))
  310. {
  311. result = FAIL;
  312. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  313. sqlite3_close(db);
  314. }
  315. else
  316. {
  317. DEBUG_INFO( "Local event record database open successfully.\n");
  318. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  319. {
  320. result = FAIL;
  321. DEBUG_INFO( "Insert local event record error message: %s\n", errMsg);
  322. }
  323. else
  324. {
  325. DEBUG_INFO( "Insert local event record successfully\n");
  326. }
  327. sprintf(sqlStr, "delete from event_record where idx < (select idx from event_record order by idx desc limit 1)-2000;");
  328. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  329. {
  330. result = FAIL;
  331. DEBUG_INFO( "delete local event_record error message: %s\n", errMsg);
  332. }
  333. else
  334. {
  335. DEBUG_INFO( "delete local event record successfully\n");
  336. }
  337. sqlite3_close(db);
  338. }
  339. return result;
  340. }
  341. //================================================
  342. // Main process
  343. //================================================
  344. int main(void)
  345. {
  346. int ByteCount,BitCount;
  347. unsigned char tmp, EventCodeTmp[7], EventCodeDisp[7];
  348. if(InitShareMemory() == FAIL)
  349. {
  350. #ifdef SystemLogMessage
  351. DEBUG_ERROR("InitShareMemory NG\n");
  352. #endif
  353. if(ShmStatusCodeData!=NULL)
  354. {
  355. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  356. }
  357. sleep(5);
  358. return 0;
  359. }
  360. if(DB_Open(localDb) != PASS)
  361. {
  362. DEBUG_ERROR("Local database initial fail.\n");
  363. }
  364. for(;;)
  365. {
  366. //check Fault Status
  367. for(ByteCount=0;ByteCount<ARRAY_SIZE(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal);ByteCount++)
  368. {
  369. if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
  370. {
  371. tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
  372. for(BitCount=0;BitCount<8;BitCount++)
  373. {
  374. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
  375. {
  376. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  377. memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  378. memcpy(EventCodeDisp, EventCodeTmp, ARRAY_SIZE(EventCodeTmp));
  379. if(((tmp>>BitCount)&0x01)==0)//Recovered
  380. {
  381. EventCodeTmp[0]='1';
  382. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]&=~(1<<BitCount);
  383. RemoveFaultCodeToBuf(EventCodeDisp);
  384. }
  385. else
  386. {
  387. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]|=(1<<BitCount);
  388. AddFaultCodeToBuf(EventCodeDisp);
  389. }
  390. EVENT_INFO("%s\n", EventCodeTmp);
  391. DB_Insert_Record(localDb, EventCodeTmp);
  392. }
  393. }
  394. }
  395. }
  396. //check Alarm Status
  397. for(ByteCount=0;ByteCount<ARRAY_SIZE(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal);ByteCount++)
  398. {
  399. if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
  400. {
  401. tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
  402. for(BitCount=0;BitCount<8;BitCount++)
  403. {
  404. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
  405. {
  406. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  407. memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  408. memcpy(EventCodeDisp, EventCodeTmp, ARRAY_SIZE(EventCodeTmp));
  409. if(((tmp>>BitCount)&0x01)==0)//Recovered
  410. {
  411. EventCodeTmp[0]='1';
  412. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]&=~(1<<BitCount);
  413. RemoveFaultCodeToBuf(EventCodeDisp);
  414. }
  415. else
  416. {
  417. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]|=(1<<BitCount);
  418. AddFaultCodeToBuf(EventCodeDisp);
  419. }
  420. EVENT_INFO("%s\n", EventCodeTmp);
  421. DB_Insert_Record(localDb, EventCodeTmp);
  422. }
  423. }
  424. }
  425. }
  426. //check Info Status
  427. for(ByteCount=0;ByteCount<ARRAY_SIZE(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal);ByteCount++)
  428. {
  429. if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
  430. {
  431. tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
  432. for(BitCount=0;BitCount<8;BitCount++)
  433. {
  434. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
  435. {
  436. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  437. memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  438. memcpy(EventCodeDisp, EventCodeTmp, ARRAY_SIZE(EventCodeTmp));
  439. if(((tmp>>BitCount)&0x01)==0)//Recovered
  440. {
  441. EventCodeTmp[0]='1';
  442. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]&=~(1<<BitCount);
  443. RemoveFaultCodeToBuf(EventCodeDisp);
  444. }
  445. else
  446. {
  447. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]|=(1<<BitCount);
  448. AddFaultCodeToBuf(EventCodeDisp);
  449. }
  450. EVENT_INFO("%s\n", EventCodeTmp);
  451. DB_Insert_Record(localDb, EventCodeTmp);
  452. }
  453. }
  454. }
  455. }
  456. usleep(100000);
  457. }
  458. return FAIL;
  459. }