123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/wireless.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix 標準函數定義*/
- #include <fcntl.h> /*檔控制定義*/
- #include <termios.h> /*PPSIX 終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include "../Log/log.h"
- #include "../ShareMemory/shmMem.h"
- #include "../DataBase/DataBase.h"
- #include "../Define/define.h"
- #include "../Config.h"
- static struct SysConfigData *pSysConfig = NULL;
- static struct SysInfoData *pSysInfo = NULL;
- static struct WARNING_CODE_INFO *pSysWarning = NULL;
- static struct AlarmCodeData *pAlarmCode = NULL;
- static struct InfoCodeData *pInfoCode = NULL;
- static struct FaultCodeData *pFaultCode = NULL;
- #define WRITE_FLASH_TIME (128)
- #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
- #define event_info(format, args...) StoreEventLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- void AddFaultCodeToBuf(uint8_t *Code)
- {
- if (pSysWarning->WarningCount < 10) {
- memcpy(&pSysWarning->WarningCode[pSysWarning->WarningCount][0], Code, 7);
- pSysWarning->WarningCount++;
- }
- }
- void RemoveFaultCodeToBuf(uint8_t *Code)
- {
- uint8_t find = 0x01;
- char _code[7];
- sprintf(_code, "%s", Code);
-
- while (find) {
- find = 0x00;
- for (uint8_t i = 0; i < pSysWarning->WarningCount; i++) {
- if (find == 0x00) {
- if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
- find = 0x01;
- }
- } else {
- memcpy(&pSysWarning->WarningCode[i - 1][0],
- &pSysWarning->WarningCode[i][0], 7);
- }
- }
- if (find) {
- pSysWarning->WarningCount--;
- }
- }
- }
- #if 0
- int DB_Open(sqlite3 *db)
- {
- int result = PASS;
- char *errMsg = NULL;
- char *createRecordSql = "CREATE TABLE IF NOT EXISTS event_record("
- "idx integer primary key AUTOINCREMENT, "
- "occurDatetime text NOT NULL, "
- "statusCode text NOT NULL"
- ");";
- if (sqlite3_open(DB_FILE, &db)) {
- result = FAIL;
- log_error( "Can't open database: %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- } else {
- log_info( "Local event record database open successfully.");
- if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK) {
- result = FAIL;
- log_error( "Create local event record table error message: %s", errMsg);
- } else {
- log_info( "Opened local event record table successfully");
- }
- sqlite3_close(db);
- }
- return result;
- }
- int DB_Insert_Record(sqlite3 *db, uint8_t *statusCode)
- {
- int result = PASS;
- char *errMsg = NULL;
- char sqlStr[1024];
- sprintf(sqlStr, "insert into event_record(occurDatetime, statusCode) values(CURRENT_TIMESTAMP, '%s');", statusCode);
- if (sqlite3_open(DB_FILE, &db)) {
- result = FAIL;
- log_info( "Can't open database: %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- } else {
- log_info( "Local event record database open successfully.");
- if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
- result = FAIL;
- log_info( "Insert local event record error message: %s", errMsg);
- } else {
- log_info( "Insert local event record successfully");
- }
- sprintf(sqlStr, "delete from event_record where idx < (select idx from event_record order by idx desc limit 1)-2000;");
- if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
- result = FAIL;
- log_info( "delete local event_record error message: %s", errMsg);
- } else {
- log_info( "delete local event record successfully");
- }
- sqlite3_close(db);
- }
- return result;
- }
- #endif
- int main(void)
- {
- int ByteCounter, BitCounter;
- uint8_t tmp, EventCodeTmp[7] = {0};
-
-
-
-
-
-
-
-
- if (CreateAllCsuShareMemory() == FAIL) {
- log_error("create share memory error");
- return FAIL;
- }
- MappingGunChargingInfo("EventLog Task");
- pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
- pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
- pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
- pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
- for (;;) {
-
- for (ByteCounter = 0; ByteCounter < sizeof(pFaultCode->PreviousFaultVal); ByteCounter++) {
- usleep(WRITE_FLASH_TIME);
- if (pFaultCode->FaultEvents.FaultVal[ByteCounter] == pFaultCode->PreviousFaultVal[ByteCounter]) {
- continue;
- }
- tmp = pFaultCode->FaultEvents.FaultVal[ByteCounter];
- for (BitCounter = 0; BitCounter < 8; BitCounter++) {
- usleep(WRITE_FLASH_TIME);
- if (((tmp >> BitCounter) & 0x01) == ((pFaultCode->PreviousFaultVal[ByteCounter] >> BitCounter) & 0x01)) {
- continue;
- }
- memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
- memcpy(EventCodeTmp, GetFaultStatusCode(ByteCounter * 8 + BitCounter), sizeof(EventCodeTmp) - 1);
- if (((tmp >> BitCounter) & 0x01) == 0) {
-
- log_info("Recovery Fault Code = %s", EventCodeTmp);
- pFaultCode->PreviousFaultVal[ByteCounter] &= ~(1 << BitCounter);
- RemoveFaultCodeToBuf(EventCodeTmp);
- EventCodeTmp[0] = '1';
- } else {
- log_info("Fault Code = %s", EventCodeTmp);
- pFaultCode->PreviousFaultVal[ByteCounter] |= (1 << BitCounter);
- AddFaultCodeToBuf(EventCodeTmp);
- }
- event_info("%s", EventCodeTmp);
- InsertEventRecord(EventCodeTmp);
- }
- }
-
- for (ByteCounter = 0; ByteCounter < sizeof(pAlarmCode->PreviousAlarmVal); ByteCounter++) {
- usleep(WRITE_FLASH_TIME);
- if (pAlarmCode->AlarmEvents.AlarmVal[ByteCounter] == pAlarmCode->PreviousAlarmVal[ByteCounter]) {
- continue;
- }
- tmp = pAlarmCode->AlarmEvents.AlarmVal[ByteCounter];
- for (BitCounter = 0; BitCounter < 8; BitCounter++) {
- usleep(WRITE_FLASH_TIME);
- if (((tmp >> BitCounter) & 0x01) == ((pAlarmCode->PreviousAlarmVal[ByteCounter] >> BitCounter) & 0x01)) {
- continue;
- }
- memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
- memcpy(EventCodeTmp, GetAlarmStatusCode(ByteCounter * 8 + BitCounter), sizeof(EventCodeTmp) - 1);
- if (((tmp >> BitCounter) & 0x01) == 0) {
-
- log_info("Recovery Alarm Code = %s", EventCodeTmp);
- pAlarmCode->PreviousAlarmVal[ByteCounter] &= ~(1 << BitCounter);
- RemoveFaultCodeToBuf(EventCodeTmp);
- EventCodeTmp[0] = '1';
- } else {
- log_info("Alarm Code = %s", EventCodeTmp);
- pAlarmCode->PreviousAlarmVal[ByteCounter] |= (1 << BitCounter);
- AddFaultCodeToBuf(EventCodeTmp);
- }
- event_info("%s", EventCodeTmp);
- InsertEventRecord(EventCodeTmp);
- }
- }
-
- for (ByteCounter = 0; ByteCounter < sizeof(pInfoCode->PreviousInfoVal); ByteCounter++) {
- usleep(WRITE_FLASH_TIME);
- if (pInfoCode->InfoEvents.InfoVal[ByteCounter] == pInfoCode->PreviousInfoVal[ByteCounter]) {
- continue;
- }
- tmp = pInfoCode->InfoEvents.InfoVal[ByteCounter];
- for (BitCounter = 0; BitCounter < 8; BitCounter++) {
- usleep(WRITE_FLASH_TIME);
- if (((tmp >> BitCounter) & 0x01) == ((pInfoCode->PreviousInfoVal[ByteCounter] >> BitCounter) & 0x01)) {
- continue;
- }
- memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
- memcpy(EventCodeTmp, GetInfoStatusCode(ByteCounter * 8 + BitCounter), sizeof(EventCodeTmp) - 1);
- if (((tmp >> BitCounter) & 0x01) == 0) {
-
- log_info("Recovery Info Code = %s", EventCodeTmp);
- pInfoCode->PreviousInfoVal[ByteCounter] &= ~(1 << BitCounter);
- RemoveFaultCodeToBuf(EventCodeTmp);
- EventCodeTmp[0] = '1';
- } else {
- log_info("Info Code = %s", EventCodeTmp);
- pInfoCode->PreviousInfoVal[ByteCounter] |= (1 << BitCounter);
- AddFaultCodeToBuf(EventCodeTmp);
- }
- event_info("%s", EventCodeTmp);
- InsertEventRecord(EventCodeTmp);
- }
- }
- usleep(500000);
- }
- return FAIL;
- }
|