Module_EventLogging.c 9.9 KB

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