Module_EventLogging.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "../Log/log.h"
  29. #include "../ShareMemory/shmMem.h"
  30. #include "../Define/define.h"
  31. #include "../Config.h"
  32. //------------------------------------------------------------------------------
  33. //struct SysConfigAndInfo *ShmSysConfigAndInfo;
  34. //struct StatusCodeData *ShmStatusCodeData;
  35. static struct SysConfigData *pSysConfig = NULL;
  36. static struct SysInfoData *pSysInfo = NULL;
  37. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  38. static struct AlarmCodeData *pAlarmCode = NULL;
  39. static struct InfoCodeData *pInfoCode = NULL;
  40. static struct FaultCodeData *pFaultCode = NULL;
  41. //sqlite3 *localDb; //DS60-120 add
  42. //------------------------------------------------------------------------------
  43. #define WRITE_FLASH_TIME (128)
  44. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db" //DS60-120 add
  45. //#define log_info(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  46. //#define log_warn(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  47. //#define log_error(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  48. #define event_info(format, args...) StoreEventLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args) //DS60-120 add
  49. /*
  50. int StoreLogMsg(const char *fmt, ...)
  51. {
  52. char Buf[4096 + 256];
  53. char buffer[4096];
  54. va_list args;
  55. struct timeb SeqEndTime;
  56. struct tm *tm;
  57. va_start(args, fmt);
  58. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  59. va_end(args);
  60. memset(Buf, 0, sizeof(Buf));
  61. ftime(&SeqEndTime);
  62. SeqEndTime.time = time(NULL);
  63. tm = localtime(&SeqEndTime.time);
  64. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES) {
  65. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  66. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  67. printf("%s \n", Buf);
  68. } else {
  69. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  70. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  71. buffer,
  72. tm->tm_year + 1900, tm->tm_mon + 1);
  73. system(Buf);
  74. }
  75. return rc;
  76. }
  77. int StoreEventLogMsg(const char *fmt, ...)
  78. {
  79. char Buf[4096 + 256];
  80. char buffer[4096];
  81. time_t CurrentTime;
  82. struct tm *tm;
  83. struct timeval tv;
  84. va_list args;
  85. va_start(args, fmt);
  86. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  87. va_end(args);
  88. memset(Buf, 0, sizeof(Buf));
  89. CurrentTime = time(NULL);
  90. tm = localtime(&CurrentTime);
  91. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  92. if ((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)) {
  93. sprintf(Buf, "echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]%s_%s_EventLog",
  94. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
  95. buffer,
  96. tm->tm_year + 1900, tm->tm_mon + 1,
  97. ShmSysConfigAndInfo->SysConfig.ModelName,
  98. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  99. } else {
  100. sprintf(Buf, "echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/EventLog/[%04d.%02d]EventLog",
  101. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
  102. buffer,
  103. tm->tm_year + 1900, tm->tm_mon + 1);
  104. }
  105. #ifdef SystemLogMessage
  106. system(Buf);
  107. #endif
  108. #ifdef ConsloePrintLog
  109. 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);
  110. #endif
  111. return rc;
  112. }
  113. int DiffTimeb(struct timeb ST, struct timeb ET)
  114. {
  115. //return milli-second
  116. unsigned int StartTime, StopTime;
  117. StartTime = (unsigned int)ST.time;
  118. StopTime = (unsigned int)ET.time;
  119. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  120. }
  121. //=================================
  122. // Common routine
  123. //=================================
  124. char *getTimeString(void)
  125. {
  126. char *result = malloc(21);
  127. time_t timep;
  128. struct tm *p;
  129. time(&timep);
  130. p = gmtime(&timep);
  131. 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);
  132. return result;
  133. }
  134. //==========================================
  135. // Init all share memory
  136. //==========================================
  137. int InitShareMemory()
  138. {
  139. int result = PASS;
  140. int MeterSMId;
  141. //creat ShmSysConfigAndInfo
  142. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  143. result = FAIL;
  144. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  145. result = FAIL;
  146. }
  147. //creat ShmStatusCodeData
  148. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  149. result = FAIL;
  150. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  151. result = FAIL;
  152. }
  153. return result;
  154. }
  155. */
  156. //================================================
  157. // Main process
  158. //================================================
  159. void AddFaultCodeToBuf(uint8_t *Code)
  160. {
  161. if (pSysWarning->WarningCount < 10) {
  162. memcpy(&pSysWarning->WarningCode[pSysWarning->WarningCount][0], Code, 7);
  163. pSysWarning->WarningCount++;
  164. }
  165. }
  166. void RemoveFaultCodeToBuf(uint8_t *Code)
  167. {
  168. uint8_t find = 0x01;
  169. char _code[7];
  170. sprintf(_code, "%s", Code);
  171. // 把相關的錯誤碼一次移除,避免重複顯示
  172. while (find) {
  173. find = 0x00;
  174. for (uint8_t i = 0; i < pSysWarning->WarningCount; i++) {
  175. if (find == 0x00) {
  176. if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
  177. find = 0x01;
  178. }
  179. } else {
  180. memcpy(&pSysWarning->WarningCode[i - 1][0],
  181. &pSysWarning->WarningCode[i][0], 7);
  182. }
  183. }
  184. if (find) {
  185. pSysWarning->WarningCount--;
  186. }
  187. }
  188. }
  189. #if 0
  190. //==========================================
  191. // SQLite routne
  192. //==========================================
  193. int DB_Open(sqlite3 *db) //DS60-120 add
  194. {
  195. int result = PASS;
  196. char *errMsg = NULL;
  197. char *createRecordSql = "CREATE TABLE IF NOT EXISTS event_record("
  198. "idx integer primary key AUTOINCREMENT, "
  199. "occurDatetime text NOT NULL, "
  200. "statusCode text NOT NULL"
  201. ");";
  202. if (sqlite3_open(DB_FILE, &db)) {
  203. result = FAIL;
  204. log_error( "Can't open database: %s\n", sqlite3_errmsg(db));
  205. sqlite3_close(db);
  206. } else {
  207. log_info( "Local event record database open successfully.\n");
  208. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  209. result = FAIL;
  210. log_error( "Create local event record table error message: %s\n", errMsg);
  211. } else {
  212. log_info( "Opened local event record table successfully\n");
  213. }
  214. sqlite3_close(db);
  215. }
  216. return result;
  217. }
  218. int DB_Insert_Record(sqlite3 *db, uint8_t *statusCode) //DS60-120 add
  219. {
  220. int result = PASS;
  221. char *errMsg = NULL;
  222. char sqlStr[1024];
  223. sprintf(sqlStr, "insert into event_record(occurDatetime, statusCode) values(CURRENT_TIMESTAMP, '%s');", statusCode);
  224. if (sqlite3_open(DB_FILE, &db)) {
  225. result = FAIL;
  226. log_info( "Can't open database: %s\n", sqlite3_errmsg(db));
  227. sqlite3_close(db);
  228. } else {
  229. log_info( "Local event record database open successfully.\n");
  230. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  231. result = FAIL;
  232. log_info( "Insert local event record error message: %s\n", errMsg);
  233. } else {
  234. log_info( "Insert local event record successfully\n");
  235. }
  236. sprintf(sqlStr, "delete from event_record where idx < (select idx from event_record order by idx desc limit 1)-2000;");
  237. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  238. result = FAIL;
  239. log_info( "delete local event_record error message: %s\n", errMsg);
  240. } else {
  241. log_info( "delete local event record successfully\n");
  242. }
  243. sqlite3_close(db);
  244. }
  245. return result;
  246. }
  247. #endif //0
  248. int main(void)
  249. {
  250. int ByteCounter, BitCounter;
  251. uint8_t tmp, EventCodeTmp[7] = {0};
  252. //if (InitShareMemory() == FAIL) {
  253. // log_error("InitShareMemory NG\n");
  254. // if (ShmStatusCodeData != NULL) {
  255. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  256. // }
  257. // sleep(5);
  258. // return 0;
  259. //}
  260. if (CreateAllCsuShareMemory() == FAIL) {
  261. log_error("create share memory error\r\n");
  262. return FAIL;
  263. }
  264. MappingGunChargingInfo("EventLog Task");
  265. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  266. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  267. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  268. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  269. pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  270. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  271. for (;;) {
  272. //check Fault Status
  273. for (ByteCounter = 0; ByteCounter < sizeof(pFaultCode->PreviousFaultVal); ByteCounter++) {
  274. usleep(WRITE_FLASH_TIME);
  275. if (pFaultCode->FaultEvents.FaultVal[ByteCounter] == pFaultCode->PreviousFaultVal[ByteCounter]) {
  276. continue;
  277. }
  278. tmp = pFaultCode->FaultEvents.FaultVal[ByteCounter]; //prevent be modified during following process
  279. for (BitCounter = 0; BitCounter < 8; BitCounter++) {
  280. usleep(WRITE_FLASH_TIME);
  281. if (((tmp >> BitCounter) & 0x01) == ((pFaultCode->PreviousFaultVal[ByteCounter] >> BitCounter) & 0x01)) {
  282. continue;
  283. }
  284. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  285. memcpy(EventCodeTmp, GetFaultStatusCode(ByteCounter * 8 + BitCounter), sizeof(EventCodeTmp) - 1);
  286. if (((tmp >> BitCounter) & 0x01) == 0) { //Recovered
  287. //EventCodeTmp[0]=1;
  288. log_info("Recovery Fault Code = %s\n", EventCodeTmp);
  289. pFaultCode->PreviousFaultVal[ByteCounter] &= ~(1 << BitCounter);
  290. RemoveFaultCodeToBuf(EventCodeTmp);
  291. EventCodeTmp[0] = '1';//DS60-120 add
  292. } else {
  293. log_info("Fault Code = %s\n", EventCodeTmp);
  294. pFaultCode->PreviousFaultVal[ByteCounter] |= (1 << BitCounter);
  295. AddFaultCodeToBuf(EventCodeTmp);
  296. }
  297. event_info("%s\n", EventCodeTmp); //DS60-120 add
  298. InsertEventRecord(EventCodeTmp); //DS60-120 add
  299. }
  300. }
  301. //check Alarm Status
  302. for (ByteCounter = 0; ByteCounter < sizeof(pAlarmCode->PreviousAlarmVal); ByteCounter++) {
  303. usleep(WRITE_FLASH_TIME);
  304. if (pAlarmCode->AlarmEvents.AlarmVal[ByteCounter] == pAlarmCode->PreviousAlarmVal[ByteCounter]) {
  305. continue;
  306. }
  307. tmp = pAlarmCode->AlarmEvents.AlarmVal[ByteCounter]; //prevent be modified during following process
  308. for (BitCounter = 0; BitCounter < 8; BitCounter++) {
  309. usleep(WRITE_FLASH_TIME);
  310. if (((tmp >> BitCounter) & 0x01) == ((pAlarmCode->PreviousAlarmVal[ByteCounter] >> BitCounter) & 0x01)) {
  311. continue;
  312. }
  313. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  314. memcpy(EventCodeTmp, GetAlarmStatusCode(ByteCounter * 8 + BitCounter), sizeof(EventCodeTmp) - 1);
  315. if (((tmp >> BitCounter) & 0x01) == 0) { //Recovered
  316. //EventCodeTmp[0]=1;
  317. log_info("Recovery Alarm Code = %s\n", EventCodeTmp);
  318. pAlarmCode->PreviousAlarmVal[ByteCounter] &= ~(1 << BitCounter);
  319. RemoveFaultCodeToBuf(EventCodeTmp);
  320. EventCodeTmp[0] = '1';//DS60-120 add
  321. } else {
  322. log_info("Alarm Code = %s\n", EventCodeTmp);
  323. pAlarmCode->PreviousAlarmVal[ByteCounter] |= (1 << BitCounter);
  324. AddFaultCodeToBuf(EventCodeTmp);
  325. }
  326. event_info("%s\n", EventCodeTmp); //DS60-120 add
  327. InsertEventRecord(EventCodeTmp); //DS60-120 add
  328. }
  329. }
  330. //check Info Status
  331. for (ByteCounter = 0; ByteCounter < sizeof(pInfoCode->PreviousInfoVal); ByteCounter++) {
  332. usleep(WRITE_FLASH_TIME);
  333. if (pInfoCode->InfoEvents.InfoVal[ByteCounter] == pInfoCode->PreviousInfoVal[ByteCounter]) {
  334. continue;
  335. }
  336. tmp = pInfoCode->InfoEvents.InfoVal[ByteCounter]; //prevent be modified during following process
  337. for (BitCounter = 0; BitCounter < 8; BitCounter++) {
  338. usleep(WRITE_FLASH_TIME);
  339. if (((tmp >> BitCounter) & 0x01) == ((pInfoCode->PreviousInfoVal[ByteCounter] >> BitCounter) & 0x01)) {
  340. continue;
  341. }
  342. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  343. memcpy(EventCodeTmp, GetInfoStatusCode(ByteCounter * 8 + BitCounter), sizeof(EventCodeTmp) - 1);
  344. if (((tmp >> BitCounter) & 0x01) == 0) { //Recovered
  345. //EventCodeTmp[0]=1;
  346. log_info("Recovery Info Code = %s\n", EventCodeTmp);
  347. pInfoCode->PreviousInfoVal[ByteCounter] &= ~(1 << BitCounter);
  348. RemoveFaultCodeToBuf(EventCodeTmp);
  349. EventCodeTmp[0] = '1';//DS60-120 add
  350. } else {
  351. log_info("Info Code = %s\n", EventCodeTmp);
  352. pInfoCode->PreviousInfoVal[ByteCounter] |= (1 << BitCounter);
  353. AddFaultCodeToBuf(EventCodeTmp);
  354. }
  355. event_info("%s\n", EventCodeTmp); //DS60-120 add
  356. InsertEventRecord(EventCodeTmp); //DS60-120 add
  357. }
  358. }
  359. usleep(500000);
  360. }
  361. return FAIL;
  362. }