Module_EventLogging.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 "define.h"
  29. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  30. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define EVENT_INFO(format, args...) StoreEventLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define Debug
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  38. struct StatusCodeData *ShmStatusCodeData;
  39. void trim(char *s);
  40. int mystrcmp(char *p1,char *p2);
  41. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  42. void split(char **arr, char *str, const char *del);
  43. #ifdef SystemLogMessage
  44. int StoreLogMsg(const char *fmt, ...)
  45. {
  46. char Buf[4096+256];
  47. char buffer[4096];
  48. time_t CurrentTime;
  49. struct tm *tm;
  50. struct timeval tv;
  51. va_list args;
  52. va_start(args, fmt);
  53. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  54. va_end(args);
  55. memset(Buf,0,sizeof(Buf));
  56. CurrentTime = time(NULL);
  57. tm=localtime(&CurrentTime);
  58. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  59. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/Eventlog/[%04d.%02d]EventLog",
  60. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  61. buffer,
  62. tm->tm_year+1900,tm->tm_mon+1);
  63. #ifdef SystemLogMessage
  64. system(Buf);
  65. #endif
  66. 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);
  67. return rc;
  68. }
  69. #endif
  70. int StoreEventLogMsg(const char *fmt, ...)
  71. {
  72. char Buf[4096+256];
  73. char buffer[4096];
  74. time_t CurrentTime;
  75. struct tm *tm;
  76. struct timeval tv;
  77. va_list args;
  78. va_start(args, fmt);
  79. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  80. va_end(args);
  81. memset(Buf,0,sizeof(Buf));
  82. CurrentTime = time(NULL);
  83. tm=localtime(&CurrentTime);
  84. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  85. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  86. {
  87. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]%s_%s_EventLog",
  88. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  89. buffer,
  90. tm->tm_year+1900,tm->tm_mon+1,
  91. ShmSysConfigAndInfo->SysConfig.ModelName,
  92. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  93. }
  94. else
  95. {
  96. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  97. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  98. buffer,
  99. tm->tm_year+1900,tm->tm_mon+1);
  100. }
  101. #ifdef SystemLogMessage
  102. system(Buf);
  103. #endif
  104. #ifdef ConsloePrintLog
  105. 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);
  106. #endif
  107. return rc;
  108. }
  109. int DiffTimeb(struct timeb ST, struct timeb ET)
  110. {
  111. //return milli-second
  112. unsigned int StartTime,StopTime;
  113. StartTime=(unsigned int)ST.time;
  114. StopTime=(unsigned int)ET.time;
  115. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  116. }
  117. //=================================
  118. // Common routine
  119. //=================================
  120. void trim(char *s)
  121. {
  122. int i=0, j, k, l=0;
  123. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  124. i++;
  125. j = strlen(s)-1;
  126. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  127. j--;
  128. if(i==0 && j==strlen(s)-1) { }
  129. else if(i==0) s[j+1] = '\0';
  130. else {
  131. for(k=i; k<=j; k++) s[l++] = s[k];
  132. s[l] = '\0';
  133. }
  134. }
  135. int mystrcmp(char *p1,char *p2)
  136. {
  137. while(*p1==*p2)
  138. {
  139. if(*p1=='\0' || *p2=='\0')
  140. break;
  141. p1++;
  142. p2++;
  143. }
  144. if(*p1=='\0' && *p2=='\0')
  145. return(PASS);
  146. else
  147. return(FAIL);
  148. }
  149. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  150. {
  151. strncpy(dest, src + start, cnt);
  152. dest[cnt] = 0;
  153. }
  154. void split(char **arr, char *str, const char *del)
  155. {
  156. char *s = strtok(str, del);
  157. while(s != NULL)
  158. {
  159. *arr++ = s;
  160. s = strtok(NULL, del);
  161. }
  162. }
  163. //==========================================
  164. // Init all share memory
  165. //==========================================
  166. int InitShareMemory()
  167. {
  168. int result = PASS;
  169. int MeterSMId;
  170. //creat ShmSysConfigAndInfo
  171. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  172. {
  173. #ifdef SystemLogMessage
  174. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  175. #endif
  176. result = FAIL;
  177. }
  178. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  179. {
  180. #ifdef SystemLogMessage
  181. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  182. #endif
  183. result = FAIL;
  184. }
  185. else
  186. {}
  187. //creat ShmStatusCodeData
  188. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  189. {
  190. #ifdef SystemLogMessage
  191. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  192. #endif
  193. result = FAIL;
  194. }
  195. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  196. {
  197. #ifdef SystemLogMessage
  198. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  199. #endif
  200. result = FAIL;
  201. }
  202. else
  203. {}
  204. //creat ShmStatusCodeData
  205. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  206. {
  207. #ifdef SystemLogMessage
  208. DEBUG_ERROR("shmget ShmStatusCodeData NG");
  209. #endif
  210. result = FAIL;
  211. }
  212. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  213. {
  214. #ifdef SystemLogMessage
  215. DEBUG_ERROR("shmat ShmStatusCodeData NG");
  216. #endif
  217. result = FAIL;
  218. }
  219. else
  220. {}
  221. memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
  222. return result;
  223. }
  224. //================================================
  225. // Main process
  226. //================================================
  227. int main(void)
  228. {
  229. int ByteCount,BitCount;
  230. unsigned char tmp, EventCodeTmp[7];
  231. if(InitShareMemory() == FAIL)
  232. {
  233. #ifdef SystemLogMessage
  234. DEBUG_ERROR("InitShareMemory NG\n");
  235. #endif
  236. if(ShmStatusCodeData!=NULL)
  237. {
  238. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  239. }
  240. sleep(5);
  241. return 0;
  242. }
  243. for(;;)
  244. {
  245. //check Fault Status
  246. for(ByteCount=0;ByteCount<5;ByteCount++)
  247. {
  248. if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
  249. {
  250. tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
  251. for(BitCount=0;BitCount<8;BitCount++)
  252. {
  253. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
  254. {
  255. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  256. memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  257. if(((tmp>>BitCount)&0x01)==0)//Recovered
  258. {
  259. EventCodeTmp[0]='1';
  260. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]&=~(1<<BitCount);
  261. }
  262. else
  263. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]|=(1<<BitCount);
  264. EVENT_INFO("%s\n", EventCodeTmp);
  265. }
  266. }
  267. }
  268. }
  269. //check Alarm Status
  270. for(ByteCount=0;ByteCount<12;ByteCount++)
  271. {
  272. if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
  273. {
  274. tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
  275. for(BitCount=0;BitCount<8;BitCount++)
  276. {
  277. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
  278. {
  279. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  280. memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  281. if(((tmp>>BitCount)&0x01)==0)//Recovered
  282. {
  283. EventCodeTmp[0]='1';
  284. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]&=(0<<BitCount);
  285. }
  286. else
  287. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]|=(1<<BitCount);
  288. EVENT_INFO("%s\n", EventCodeTmp);
  289. }
  290. }
  291. }
  292. }
  293. //check Info Status
  294. for(ByteCount=0;ByteCount<40;ByteCount++)
  295. {
  296. if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
  297. {
  298. tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
  299. for(BitCount=0;BitCount<8;BitCount++)
  300. {
  301. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
  302. {
  303. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  304. memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  305. if(((tmp>>BitCount)&0x01)==0)//Recovered
  306. {
  307. EventCodeTmp[0]='1';
  308. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]&=(0<<BitCount);
  309. }
  310. else
  311. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]|=(1<<BitCount);
  312. EVENT_INFO("%s\n", EventCodeTmp);
  313. }
  314. }
  315. }
  316. }
  317. usleep(100000);
  318. }
  319. return FAIL;
  320. }