Module_EventLogging.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  30. #define PASS 1
  31. #define FAIL -1
  32. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  33. struct StatusCodeData *ShmStatusCodeData;
  34. void PRINTF_FUNC(char *string, ...);
  35. int StoreLogMsg(const char *fmt, ...);
  36. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  37. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  38. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  39. int StoreLogMsg(const char *fmt, ...)
  40. {
  41. char Buf[4096+256];
  42. char buffer[4096];
  43. time_t CurrentTime;
  44. struct tm *tm;
  45. va_list args;
  46. va_start(args, fmt);
  47. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  48. va_end(args);
  49. memset(Buf,0,sizeof(Buf));
  50. CurrentTime = time(NULL);
  51. tm=localtime(&CurrentTime);
  52. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  53. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  54. buffer,
  55. tm->tm_year+1900,tm->tm_mon+1);
  56. system(Buf);
  57. return rc;
  58. }
  59. int DiffTimeb(struct timeb ST, struct timeb ET)
  60. {
  61. //return milli-second
  62. unsigned int StartTime,StopTime;
  63. StartTime=(unsigned int)ST.time;
  64. StopTime=(unsigned int)ET.time;
  65. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  66. }
  67. void PRINTF_FUNC(char *string, ...)
  68. {
  69. if (DEBUG)
  70. {
  71. va_list args;
  72. char buffer[4096];
  73. va_start(args, string);
  74. vsnprintf(buffer, sizeof(buffer), string, args);
  75. va_end(args);
  76. printf("%s \n", buffer);
  77. }
  78. }
  79. //=================================
  80. // Common routine
  81. //=================================
  82. char* getTimeString(void)
  83. {
  84. char *result=malloc(21);
  85. time_t timep;
  86. struct tm *p;
  87. time(&timep);
  88. p=gmtime(&timep);
  89. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  90. return result;
  91. }
  92. //==========================================
  93. // Init all share memory
  94. //==========================================
  95. int InitShareMemory()
  96. {
  97. int result = PASS;
  98. int MeterSMId;
  99. //creat ShmSysConfigAndInfo
  100. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  101. {
  102. #ifdef SystemLogMessage
  103. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  104. #endif
  105. result = FAIL;
  106. }
  107. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  108. {
  109. #ifdef SystemLogMessage
  110. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  111. #endif
  112. result = FAIL;
  113. }
  114. else
  115. {}
  116. //creat ShmStatusCodeData
  117. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  118. {
  119. #ifdef SystemLogMessage
  120. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  121. #endif
  122. result = FAIL;
  123. }
  124. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  125. {
  126. #ifdef SystemLogMessage
  127. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  128. #endif
  129. result = FAIL;
  130. }
  131. else
  132. {}
  133. return result;
  134. }
  135. //================================================
  136. // Main process
  137. //================================================
  138. void AddFaultCodeToBuf(unsigned char *Code)
  139. {
  140. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount < 10)
  141. {
  142. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[ShmSysConfigAndInfo->SysWarningInfo.WarningCount][0], Code, 7);
  143. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  144. }
  145. }
  146. void RemoveFaultCodeToBuf(unsigned char *Code)
  147. {
  148. unsigned char find = 0x00;
  149. char _code[7];
  150. sprintf(_code,"%s", Code);
  151. for(unsigned char i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  152. {
  153. if (find == 0x00)
  154. {
  155. if(memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) != 0)
  156. {
  157. find = 0x01;
  158. }
  159. }
  160. else
  161. {
  162. if(i == ShmSysConfigAndInfo->SysWarningInfo.WarningCount - 1)
  163. {
  164. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "", 7);
  165. }
  166. else
  167. {
  168. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  169. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i + 1][0], 7);
  170. }
  171. }
  172. }
  173. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  174. }
  175. int main(void)
  176. {
  177. int ByteCount,BitCount;
  178. unsigned char tmp, EventCodeTmp[7];
  179. if(InitShareMemory() == FAIL)
  180. {
  181. #ifdef SystemLogMessage
  182. DEBUG_ERROR("InitShareMemory NG\n");
  183. #endif
  184. if(ShmStatusCodeData!=NULL)
  185. {
  186. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  187. }
  188. sleep(5);
  189. return 0;
  190. }
  191. for(;;)
  192. {
  193. //check Fault Status
  194. for(ByteCount=0;ByteCount<4;ByteCount++)
  195. {
  196. if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
  197. {
  198. tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
  199. for(BitCount=0;BitCount<8;BitCount++)
  200. {
  201. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
  202. {
  203. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  204. memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  205. if(((tmp>>BitCount)&0x01)==0)//Recovered
  206. {
  207. //EventCodeTmp[0]=1;
  208. DEBUG_INFO("Recovery Fault Code = %s\n", EventCodeTmp);
  209. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] &= ~(1<<BitCount);
  210. RemoveFaultCodeToBuf(EventCodeTmp);
  211. }
  212. else
  213. {
  214. DEBUG_INFO("Fault Code = %s\n", EventCodeTmp);
  215. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] |= (1<<BitCount);
  216. AddFaultCodeToBuf(EventCodeTmp);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. //check Alarm Status
  223. for(ByteCount=0;ByteCount<11;ByteCount++)
  224. {
  225. if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
  226. {
  227. tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
  228. for(BitCount=0;BitCount<8;BitCount++)
  229. {
  230. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
  231. {
  232. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  233. memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  234. if(((tmp>>BitCount)&0x01)==0)//Recovered
  235. {
  236. //EventCodeTmp[0]=1;
  237. DEBUG_INFO("Recovery Alarm Code = %s\n", EventCodeTmp);
  238. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] &= ~(1<<BitCount);
  239. RemoveFaultCodeToBuf(EventCodeTmp);
  240. }
  241. else
  242. {
  243. DEBUG_INFO("Alarm Code = %s\n", EventCodeTmp);
  244. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] |= (1<<BitCount);
  245. AddFaultCodeToBuf(EventCodeTmp);
  246. }
  247. }
  248. }
  249. }
  250. }
  251. //check Info Status
  252. for(ByteCount=0;ByteCount<8;ByteCount++)
  253. {
  254. if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
  255. {
  256. tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
  257. for(BitCount=0;BitCount<8;BitCount++)
  258. {
  259. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
  260. {
  261. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  262. memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  263. if(((tmp>>BitCount)&0x01)==0)//Recovered
  264. {
  265. //EventCodeTmp[0]=1;
  266. DEBUG_INFO("Recovery Info Code = %s\n", EventCodeTmp);
  267. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] &= ~(1<<BitCount);
  268. RemoveFaultCodeToBuf(EventCodeTmp);
  269. }
  270. else
  271. {
  272. DEBUG_INFO("Info Code = %s\n", EventCodeTmp);
  273. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] |= (1<<BitCount);
  274. AddFaultCodeToBuf(EventCodeTmp);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. usleep(500000);
  281. }
  282. return FAIL;
  283. }