Module_EventLogging.c 15 KB

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