123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- #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>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include <math.h>
- #include "define.h"
- #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define Debug
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define SystemLogMessage
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- void trim(char *s);
- int mystrcmp(char *p1, char *p2);
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
- void split(char **arr, char *str, const char *del);
- int StoreLogMsg(const char *fmt, ...) {
- char Buf[4096 + 256];
- char buffer[4096];
- time_t CurrentTime;
- struct tm *tm;
- va_list args;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf, 0, sizeof(Buf));
- CurrentTime = time(NULL);
- tm = localtime(&CurrentTime);
- sprintf(Buf,
- "echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]logPackTools_SystemLog",
- tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
- tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
- system(Buf);
- #ifdef ConsloePrintLog
- printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
- tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
- buffer);
- #endif
- return rc;
- }
- int DiffTimeb(struct timeb ST, struct timeb ET) {
- //return milli-second
- unsigned int StartTime, StopTime;
- StartTime = (unsigned int) ST.time;
- StopTime = (unsigned int) ET.time;
- return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
- }
- //=================================
- // Common routine
- //=================================
- void trim(char *s) {
- int i = 0, j, k, l = 0;
- while ((s[i] == ' ') || (s[i] == '\t') || (s[i] == '\n'))
- i++;
- j = strlen(s) - 1;
- while ((s[j] == ' ') || (s[j] == '\t') || (s[j] == '\n'))
- j--;
- if (i == 0 && j == strlen(s) - 1) {
- } else if (i == 0)
- s[j + 1] = '\0';
- else {
- for (k = i; k <= j; k++)
- s[l++] = s[k];
- s[l] = '\0';
- }
- }
- int mystrcmp(char *p1, char *p2) {
- while (*p1 == *p2) {
- if (*p1 == '\0' || *p2 == '\0')
- break;
- p1++;
- p2++;
- }
- if (*p1 == '\0' && *p2 == '\0')
- return (PASS);
- else
- return (FAIL);
- }
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt) {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- void split(char **arr, char *str, const char *del) {
- char *s = strtok(str, del);
- while (s != NULL) {
- *arr++ = s;
- s = strtok(NULL, del);
- }
- }
- int ConnectorType(char* connector){
- int result;
- if(strcmp(connector, "0") == 0){
- result= 0;
- }
- else if(strcmp(connector, "1") == 0 || strcmp(connector, "2") == 0 || strcmp(connector, "3") == 0 || strcmp(connector, "4") == 0 || strcmp(connector, "U") == 0 || strcmp(connector, "E") == 0){
- result= 1;//CCS
- }
- else if(strcmp(connector, "5") == 0 || strcmp(connector, "6") == 0 || strcmp(connector, "G") == 0){
- result= 2;//GB
- }
- else if(strcmp(connector, "J") == 0){
- result= 3;//CHAdeMO
- }
- else{
- }
- return result;
- }
- int ModelType(char* type,char* network){
- int result=0;
- if(strcmp(type, "A") == 0){
- if(strcmp(network, "0") == 0 || strcmp(network, "R") == 0 || strcmp(network, "B") == 0){
- result=0;
- }
- else if(strcmp(network, "E") == 0 || strcmp(network, "W") == 0 || strcmp(network, "T") == 0 || strcmp(network, "U") == 0){
- result=1;
- }
- }
- else if(strcmp(type, "D") == 0){
- result=2;
- }
- return result;
- }
- int isDirectory(const char *path) {
- struct stat statbuf;
- if (stat(path, &statbuf) != 0)
- return 0;
- return S_ISDIR(statbuf.st_mode);
- }
- //==========================================
- // Init all share memory
- //==========================================
- int InitShareMemory() {
- int result = PASS;
- int MeterSMId;
- //creat ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey,
- sizeof(struct SysConfigAndInfo), 0777)) < 0) {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0))
- == (void *) -1) {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- } else {
- }
- //creat ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),
- 0777)) < 0) {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- } else {
- }
- return result;
- }
- //================================================
- // Main process
- //================================================
- int main(int argc, char *argv[])
- {
- int CCSID=0;
- int isCCS=isDirectory("/Storage/root");
- if(isCCS==0)
- {
- if (InitShareMemory() == FAIL)
- {
- DEBUG_ERROR("InitShareMemory NG\n");
- if (ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
- }
- sleep(5);
- return 0;
- }
- }
- if(strcmp(argv[1], "log") == 0)
- {
- if(isCCS==1)
- {
- // get ip to distinguish main or ccs
- char MyIpBuf[32]={0};
- FILE *fpRead;
- // Get IP address by shell command
- char* command=(char*)"ifconfig eth0|grep 'inet addr' |awk -F \":\" '{print $2}' |awk '{print $1}'";
- char* renewCh;
- fpRead = popen(command, "r");
- fgets(MyIpBuf, 32 , fpRead);
- // Have to pclose()
- if(fpRead != NULL)
- pclose(fpRead);
- // Replace \n to '0'
- renewCh=strstr(MyIpBuf,"\n");
- if(renewCh)
- *renewCh= '\0';
- printf("===ip: %s ===\n", MyIpBuf);
- if(strcmp(MyIpBuf, "192.168.0.21") == 0){
- CCSID=1;
- }
- if(strcmp(MyIpBuf, "192.168.0.22") == 0){
- CCSID=2;
- }
- }
- unsigned char ModelName[64];;
- unsigned char SerialNo[64];;
- if(isCCS==0)
- {
- memcpy(ModelName,ShmSysConfigAndInfo->SysConfig.ModelName,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
- printf("%s", ModelName);
- memcpy(SerialNo,ShmSysConfigAndInfo->SysConfig.SerialNumber,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
- printf("%s", SerialNo);
- }
- if(CCSID!=0)
- {
- sprintf((char*)ModelName,"CCS%d",CCSID);
- printf("%s", ModelName);
- }
- int year,month,i,ty,tm,cnt;
- char pwd[70];
- char cmd[2048];
- time_t tt = time(0); // Get current time
- struct tm *pst = localtime(&tt); // Transfer time_t to struct tm
- year = pst->tm_year + 1900;
- month = pst->tm_mon + 1;
- system("rm -f /mnt/*.zip");
- if((argc == 3) && isdigit(*argv[2]))
- {
- cnt=atoi(argv[2]);
- }
- else
- {
- cnt=6;
- }
- if(isCCS==0)
- {
- sprintf(pwd," --password %04d%02d%s",year,month,SerialNo);
- }
- else
- {}
- // Pack system log
- sprintf(cmd,"zip -9 %s \t /mnt/system.zip",pwd);
- for(i=0;i<cnt;i++)
- {
- if(month-i<1)
- {
- tm=month-i+12;
- ty=year-1;
- }
- else
- {
- tm=month-i;
- ty=year;
- }
- sprintf(cmd,"%s \t /Storage/SystemLog/*%04d*%02d*",cmd,ty,tm);
- sprintf(cmd,"%s \t /Storage/OCPP/*%04d*%02d*SystemLog*",cmd,ty,tm);
- }
- sprintf(cmd,"%s \t /Storage/OCPP/*.db",cmd);
- if(isCCS==0){
- sprintf(cmd,"%s \t /Storage/CCS*.zip",cmd);
- }
- system(cmd);
-
- // Pack Event & Charging Log
- sprintf(cmd,"zip -9 \t /mnt/charging_event.zip");
- for(i=0;i<cnt;i++)
- {
- if(month-i<1)
- {
- tm=month-i+12;
- ty=year-1;
- }
- else
- {
- tm=month-i;
- ty=year;
- }
- sprintf(cmd,"%s \t /Storage/EventLog/*%04d*%02d*",cmd,ty,tm);
- sprintf(cmd,"%s \t /Storage/OCPP/*%04d*%02d*Ocpp*MessageLog*",cmd,ty,tm);
- }
- sprintf(cmd,"%s \t /Storage/ChargeLog/*.db",cmd);
- sprintf(cmd,"%s \t /Storage/EventLog/*.db",cmd);
- system(cmd);
- // Combine all zip file to one
- system("exec zip -9 /mnt/log.zip /mnt/system.zip /mnt/charging_event.zip");
- system("exec rm -f /mnt/system.zip /mnt/charging_event.zip");
- if(isCCS==1)
- {
- const char* server="192.168.0.10";
- const char* username="vern";
- const char* password="vern@delta";
- //sprintf(cmd,"/usr/bin/ftpput -u %s -p %s %s /Storage/CCS/CCS%d-%04d%02d%02d%02d%02d%02d.zip /mnt/%s-%s-%04d%02d%02d%02d%02d%02d.zip",username,password,server,CCSID,year,month,day,hour,min,sec,ModelName,SerialNo,year,month,day,hour,min,sec);
- sprintf(cmd,"/usr/bin/ftpput -u %s -p %s %s /Storage/CCS%d.zip /mnt/log.zip",username,password,server,CCSID);
- system(cmd);
- }
-
- printf("Log packing is done!\n");
- }
- }
|