Module_EventLogging.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. va_list args;
  70. char buffer[4096];
  71. va_start(args, string);
  72. vsnprintf(buffer, sizeof(buffer), string, args);
  73. va_end(args);
  74. if (DEBUG)
  75. printf("%s \n", buffer);
  76. else
  77. DEBUG_INFO("%s \n", buffer);
  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 = 0x01;
  149. char _code[7];
  150. sprintf(_code,"%s", Code);
  151. // 把相關的錯誤碼一次移除,避免重複顯示
  152. while(find)
  153. {
  154. find = 0x00;
  155. for(unsigned char i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  156. {
  157. if (find == 0x00)
  158. {
  159. if(memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0)
  160. {
  161. find = 0x01;
  162. }
  163. }
  164. else
  165. {
  166. if(i == ShmSysConfigAndInfo->SysWarningInfo.WarningCount - 1)
  167. {
  168. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "", 7);
  169. }
  170. else
  171. {
  172. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  173. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i + 1][0], 7);
  174. }
  175. }
  176. }
  177. if (find)
  178. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  179. }
  180. }
  181. int main(void)
  182. {
  183. int ByteCount,BitCount;
  184. unsigned char tmp, EventCodeTmp[7];
  185. if(InitShareMemory() == FAIL)
  186. {
  187. #ifdef SystemLogMessage
  188. DEBUG_ERROR("InitShareMemory NG\n");
  189. #endif
  190. if(ShmStatusCodeData!=NULL)
  191. {
  192. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  193. }
  194. sleep(5);
  195. return 0;
  196. }
  197. for(;;)
  198. {
  199. //check Fault Status
  200. for(ByteCount=0;ByteCount<4;ByteCount++)
  201. {
  202. if(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount] != ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount])
  203. {
  204. tmp=ShmStatusCodeData->FaultCode.FaultEvents.FaultVal[ByteCount]; //prevent be modified during following process
  205. for(BitCount=0;BitCount<8;BitCount++)
  206. {
  207. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount]>>BitCount)&0x01))
  208. {
  209. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  210. memcpy(EventCodeTmp,FaultStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  211. if(((tmp>>BitCount)&0x01)==0)//Recovered
  212. {
  213. //EventCodeTmp[0]=1;
  214. DEBUG_INFO("Recovery Fault Code = %s\n", EventCodeTmp);
  215. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] &= ~(1<<BitCount);
  216. RemoveFaultCodeToBuf(EventCodeTmp);
  217. }
  218. else
  219. {
  220. DEBUG_INFO("Fault Code = %s\n", EventCodeTmp);
  221. ShmStatusCodeData->FaultCode.PreviousFaultVal[ByteCount] |= (1<<BitCount);
  222. AddFaultCodeToBuf(EventCodeTmp);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. //check Alarm Status
  229. for(ByteCount=0;ByteCount<12;ByteCount++)
  230. {
  231. if(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount] != ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount])
  232. {
  233. tmp=ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal[ByteCount]; //prevent be modified during following process
  234. for(BitCount=0;BitCount<8;BitCount++)
  235. {
  236. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount]>>BitCount)&0x01))
  237. {
  238. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  239. memcpy(EventCodeTmp,AlarmStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  240. if(((tmp>>BitCount)&0x01)==0)//Recovered
  241. {
  242. //EventCodeTmp[0]=1;
  243. DEBUG_INFO("Recovery Alarm Code = %s\n", EventCodeTmp);
  244. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] &= ~(1<<BitCount);
  245. RemoveFaultCodeToBuf(EventCodeTmp);
  246. }
  247. else
  248. {
  249. DEBUG_INFO("Alarm Code = %s\n", EventCodeTmp);
  250. ShmStatusCodeData->AlarmCode.PreviousAlarmVal[ByteCount] |= (1<<BitCount);
  251. AddFaultCodeToBuf(EventCodeTmp);
  252. }
  253. }
  254. }
  255. }
  256. }
  257. //check Info Status
  258. for(ByteCount=0;ByteCount<40;ByteCount++)
  259. {
  260. if(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount] != ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount])
  261. {
  262. tmp=ShmStatusCodeData->InfoCode.InfoEvents.InfoVal[ByteCount]; //prevent be modified during following process
  263. for(BitCount=0;BitCount<8;BitCount++)
  264. {
  265. if(((tmp>>BitCount)&0x01) != ((ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount]>>BitCount)&0x01))
  266. {
  267. memset(EventCodeTmp,0,sizeof(EventCodeTmp));
  268. memcpy(EventCodeTmp,InfoStatusCode[ByteCount*8+BitCount],sizeof(EventCodeTmp)-1);
  269. if(((tmp>>BitCount)&0x01)==0)//Recovered
  270. {
  271. //EventCodeTmp[0]=1;
  272. DEBUG_INFO("Recovery Info Code = %s\n", EventCodeTmp);
  273. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] &= ~(1<<BitCount);
  274. RemoveFaultCodeToBuf(EventCodeTmp);
  275. }
  276. else
  277. {
  278. DEBUG_INFO("Info Code = %s\n", EventCodeTmp);
  279. ShmStatusCodeData->InfoCode.PreviousInfoVal[ByteCount] |= (1<<BitCount);
  280. AddFaultCodeToBuf(EventCodeTmp);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. usleep(500000);
  287. }
  288. return FAIL;
  289. }