123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /*
- * Module_Diagnostics.c
- *
- * Created on: 2022年12月12日
- * Author: 7978
- */
- #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 <math.h>
- #include <stdbool.h>
- #include "../../define.h"
- #include "Config.h"
- #include "Common.h"
- #include "Module_EvComm.h"
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct PrimaryMcuData *ShmPrimaryMcuData;
- struct PsuData *ShmPsuData;
- struct RelayModuleData *ShmRelayModuleData[2];
- struct FanModuleData *ShmFanModuleData;
- struct LedModuleData *ShmLedModuleData;
- ChargerInfoData *ShmChargerInfo;
- struct OCPP16Data *ShmOCPP16Data;
- struct OCPP20Data *ShmOCPP20Data;
- struct OCPP16Data *ShmOCPP16DataPH;
- int InitSysConfigAndInfoShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmSysConfigAndInfo
- if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitStatusCodeShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmStatusCodeData
- if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitPrimaryMcuShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmPrimaryMcuData
- if((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitPsuShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmPsuData
- if((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitRelayModuleShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmRelayModuleData
- if((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitRelay2ModuleShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmRelayModuleData
- if((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitFanModuleShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmFanModuleData
- if((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitLedModuleShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmLedModuleData
- if((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitChargerInfoShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmChargerInfo
- if((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitOCPP16ShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmOCPP16Data
- if((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitOCPP16PHShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmOCPP16Data
- if((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitOCPP20ShareMemory(void)
- {
- int result = PASS;
- int MeterSMId;
- //initial ShmOCPP20Data
- if((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0)
- {
- result = FAIL;
- }
- else if((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- result = FAIL;
- }
- return result;
- }
- int InitShareMemory(void)
- {
- int result = PASS;
- //initial ShmSysConfigAndInfo
- if(InitSysConfigAndInfoShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmStatusCodeData
- if(InitStatusCodeShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmPrimaryMcuData
- if(InitPrimaryMcuShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmPsuData
- if(InitPsuShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmRelayModuleData
- if(InitRelayModuleShareMemory() == FAIL)
- {
- result = FAIL;
- }
- if(InitRelay2ModuleShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmFanModuleData
- if(InitFanModuleShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmLedModuleData
- if(InitLedModuleShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmChargerInfo
- if(InitChargerInfoShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmOCPP16Data
- if(InitOCPP16ShareMemory() == FAIL)
- {
- result = FAIL;
- }
- if(InitOCPP16PHShareMemory() == FAIL)
- {
- result = FAIL;
- }
- //initial ShmOCPP20Data
- if(InitOCPP20ShareMemory() == FAIL)
- {
- result = FAIL;
- }
- return result;
- }
- // ./Module_Diagnostics X AAA.AAA.AAA.AAA
- // Dispenser ID: X, start from 1 ~ 4
- // Dispenser IP: AAA.AAA.AAA.AAA
- int main(int argc, char *argv[])
- {
- int success = FAIL;
- bool _find = false;
- int _dispenserId = 0;
- char _address[128];
- char dispenserStr[64];
- memset(_address, 0x00, sizeof(_address));
- memset(dispenserStr, 0x00, sizeof(dispenserStr));
- if(InitShareMemory() == FAIL)
- {
- LOG_ERROR("InitShareMemory NG");
- return FAIL;
- }
- if(argc == 3)
- {
- _dispenserId = atoi(argv[1]);
- strcpy(_address, argv[2]);
- if(_dispenserId > 0)
- {
- for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
- {
- if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
- {
- if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == inet_addr(_address) &&
- ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex == (_dispenserId - 1))
- {
- _find = true;
- }
- }
- }
- }
- if(_find)
- {
- sprintf(dispenserStr, "Dispenser %d ", _dispenserId);
- ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Confirm = ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Request;
- }
- LOG_INFO("Set Diagnostics Request to %s[%s]", dispenserStr, _address);
- char cmdBuf[512] = {0};
- sprintf(cmdBuf, "curl -k -X POST --data \"logCnt=2\" https://%s/log_download_action.php", _address);
- if(system(cmdBuf) == 0)
- {
- success = PASS;
- }
- LOG_INFO("%s[%s] Latest log file is %s", dispenserStr, _address, success == PASS ? "OK" : "NG");
- if(success == PASS)
- {
- struct timeb SeqEndTime;
- struct tm *tm;
- ftime(&SeqEndTime);
- SeqEndTime.time = time(NULL);
- tm=localtime(&SeqEndTime.time);
- char _fileName[64];
- if(_find)
- {
- sprintf(_fileName, "%d", _dispenserId);
- }
- else
- {
- sprintf(_fileName, "[%02d.%02d]", tm->tm_mon + 1, tm->tm_mday);
- }
- sprintf((char*)cmdBuf,"ftpget -u root -p y42j/4cj84 %s \"/Storage/SystemLog/[%04d.%02d]Dispenser%sLog.zip\" /mnt/log.zip",
- _address, tm->tm_year + 1900, tm->tm_mon + 1, _fileName);
- if(system(cmdBuf) != 0)
- {
- success = FAIL;
- }
- LOG_INFO("Diagnostics %s[%s] %s", dispenserStr, _address, success == PASS ? "OK" : "NG");
- if(success)
- {
- ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Completed = ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_dispenserId - 1].bits.Confirm;
- }
- }
- }
- else
- {
- LOG_INFO("Diagnostics input parameter error!");
- }
- return success;
- }
|