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