logPackTools.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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>
  20. #include <fcntl.h>
  21. #include <termios.h>
  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 <math.h>
  29. #include "define.h"
  30. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define Debug
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define SystemLogMessage
  38. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  39. struct StatusCodeData *ShmStatusCodeData;
  40. void trim(char *s);
  41. int mystrcmp(char *p1, char *p2);
  42. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  43. void split(char **arr, char *str, const char *del);
  44. int StoreLogMsg(const char *fmt, ...) {
  45. char Buf[4096 + 256];
  46. char buffer[4096];
  47. time_t CurrentTime;
  48. struct tm *tm;
  49. va_list args;
  50. va_start(args, fmt);
  51. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  52. va_end(args);
  53. memset(Buf, 0, sizeof(Buf));
  54. CurrentTime = time(NULL);
  55. tm = localtime(&CurrentTime);
  56. sprintf(Buf,
  57. "echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]logPackTools_SystemLog",
  58. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  59. tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
  60. system(Buf);
  61. #ifdef ConsloePrintLog
  62. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
  63. tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  64. buffer);
  65. #endif
  66. return rc;
  67. }
  68. int DiffTimeb(struct timeb ST, struct timeb ET) {
  69. //return milli-second
  70. unsigned int StartTime, StopTime;
  71. StartTime = (unsigned int) ST.time;
  72. StopTime = (unsigned int) ET.time;
  73. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  74. }
  75. //=================================
  76. // Common routine
  77. //=================================
  78. void trim(char *s) {
  79. int i = 0, j, k, l = 0;
  80. while ((s[i] == ' ') || (s[i] == '\t') || (s[i] == '\n'))
  81. i++;
  82. j = strlen(s) - 1;
  83. while ((s[j] == ' ') || (s[j] == '\t') || (s[j] == '\n'))
  84. j--;
  85. if (i == 0 && j == strlen(s) - 1) {
  86. } else if (i == 0)
  87. s[j + 1] = '\0';
  88. else {
  89. for (k = i; k <= j; k++)
  90. s[l++] = s[k];
  91. s[l] = '\0';
  92. }
  93. }
  94. int mystrcmp(char *p1, char *p2) {
  95. while (*p1 == *p2) {
  96. if (*p1 == '\0' || *p2 == '\0')
  97. break;
  98. p1++;
  99. p2++;
  100. }
  101. if (*p1 == '\0' && *p2 == '\0')
  102. return (PASS);
  103. else
  104. return (FAIL);
  105. }
  106. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt) {
  107. strncpy(dest, src + start, cnt);
  108. dest[cnt] = 0;
  109. }
  110. void split(char **arr, char *str, const char *del) {
  111. char *s = strtok(str, del);
  112. while (s != NULL) {
  113. *arr++ = s;
  114. s = strtok(NULL, del);
  115. }
  116. }
  117. int ConnectorType(char* connector){
  118. int result;
  119. if(strcmp(connector, "0") == 0){
  120. result= 0;
  121. }
  122. 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){
  123. result= 1;//CCS
  124. }
  125. else if(strcmp(connector, "5") == 0 || strcmp(connector, "6") == 0 || strcmp(connector, "G") == 0){
  126. result= 2;//GB
  127. }
  128. else if(strcmp(connector, "J") == 0){
  129. result= 3;//CHAdeMO
  130. }
  131. else{
  132. }
  133. return result;
  134. }
  135. int ModelType(char* type,char* network){
  136. int result=0;
  137. if(strcmp(type, "A") == 0){
  138. if(strcmp(network, "0") == 0 || strcmp(network, "R") == 0 || strcmp(network, "B") == 0){
  139. result=0;
  140. }
  141. else if(strcmp(network, "E") == 0 || strcmp(network, "W") == 0 || strcmp(network, "T") == 0 || strcmp(network, "U") == 0){
  142. result=1;
  143. }
  144. }
  145. else if(strcmp(type, "D") == 0){
  146. result=2;
  147. }
  148. return result;
  149. }
  150. int isDirectory(const char *path) {
  151. struct stat statbuf;
  152. if (stat(path, &statbuf) != 0)
  153. return 0;
  154. return S_ISDIR(statbuf.st_mode);
  155. }
  156. //==========================================
  157. // Init all share memory
  158. //==========================================
  159. int InitShareMemory() {
  160. int result = PASS;
  161. int MeterSMId;
  162. //creat ShmSysConfigAndInfo
  163. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey,
  164. sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  165. #ifdef SystemLogMessage
  166. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  167. #endif
  168. result = FAIL;
  169. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0))
  170. == (void *) -1) {
  171. #ifdef SystemLogMessage
  172. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  173. #endif
  174. result = FAIL;
  175. } else {
  176. }
  177. //creat ShmStatusCodeData
  178. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),
  179. 0777)) < 0) {
  180. #ifdef SystemLogMessage
  181. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  182. #endif
  183. result = FAIL;
  184. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  185. #ifdef SystemLogMessage
  186. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  187. #endif
  188. result = FAIL;
  189. } else {
  190. }
  191. return result;
  192. }
  193. //================================================
  194. // Main process
  195. //================================================
  196. int main(int argc, char *argv[])
  197. {
  198. int CCSID=0;
  199. int isCCS=isDirectory("/Storage/root");
  200. if(isCCS==0)
  201. {
  202. if (InitShareMemory() == FAIL)
  203. {
  204. DEBUG_ERROR("InitShareMemory NG\n");
  205. if (ShmStatusCodeData != NULL)
  206. {
  207. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  208. }
  209. sleep(5);
  210. return 0;
  211. }
  212. }
  213. if(strcmp(argv[1], "log") == 0)
  214. {
  215. if(isCCS==1)
  216. {
  217. // get ip to distinguish main or ccs
  218. char MyIpBuf[32]={0};
  219. FILE *fpRead;
  220. // Get IP address by shell command
  221. char* command=(char*)"ifconfig eth0|grep 'inet addr' |awk -F \":\" '{print $2}' |awk '{print $1}'";
  222. char* renewCh;
  223. fpRead = popen(command, "r");
  224. fgets(MyIpBuf, 32 , fpRead);
  225. // Have to pclose()
  226. if(fpRead != NULL)
  227. pclose(fpRead);
  228. // Replace \n to '0'
  229. renewCh=strstr(MyIpBuf,"\n");
  230. if(renewCh)
  231. *renewCh= '\0';
  232. printf("===ip: %s ===\n", MyIpBuf);
  233. if(strcmp(MyIpBuf, "192.168.0.21") == 0){
  234. CCSID=1;
  235. }
  236. if(strcmp(MyIpBuf, "192.168.0.22") == 0){
  237. CCSID=2;
  238. }
  239. }
  240. unsigned char ModelName[64];;
  241. unsigned char SerialNo[64];;
  242. if(isCCS==0)
  243. {
  244. memcpy(ModelName,ShmSysConfigAndInfo->SysConfig.ModelName,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  245. printf("%s", ModelName);
  246. memcpy(SerialNo,ShmSysConfigAndInfo->SysConfig.SerialNumber,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  247. printf("%s", SerialNo);
  248. }
  249. if(CCSID!=0)
  250. {
  251. sprintf((char*)ModelName,"CCS%d",CCSID);
  252. printf("%s", ModelName);
  253. }
  254. int year,month,i,ty,tm,cnt;
  255. char pwd[70];
  256. char cmd[2048];
  257. time_t tt = time(0); // Get current time
  258. struct tm *pst = localtime(&tt); // Transfer time_t to struct tm
  259. year = pst->tm_year + 1900;
  260. month = pst->tm_mon + 1;
  261. system("rm -f /mnt/*.zip");
  262. if((argc == 3) && isdigit(*argv[2]))
  263. {
  264. cnt=atoi(argv[2]);
  265. }
  266. else
  267. {
  268. cnt=6;
  269. }
  270. if(isCCS==0)
  271. {
  272. sprintf(pwd," --password %04d%02d%s",year,month,SerialNo);
  273. }
  274. else
  275. {}
  276. // Pack system log
  277. sprintf(cmd,"zip -9 %s \t /mnt/system.zip",pwd);
  278. for(i=0;i<cnt;i++)
  279. {
  280. if(month-i<1)
  281. {
  282. tm=month-i+12;
  283. ty=year-1;
  284. }
  285. else
  286. {
  287. tm=month-i;
  288. ty=year;
  289. }
  290. sprintf(cmd,"%s \t /Storage/SystemLog/*%04d*%02d*",cmd,ty,tm);
  291. sprintf(cmd,"%s \t /Storage/OCPP/*%04d*%02d*SystemLog*",cmd,ty,tm);
  292. }
  293. sprintf(cmd,"%s \t /Storage/OCPP/*.db",cmd);
  294. if(isCCS==0){
  295. sprintf(cmd,"%s \t /Storage/CCS*.zip",cmd);
  296. }
  297. system(cmd);
  298. // Pack Event & Charging Log
  299. sprintf(cmd,"zip -9 \t /mnt/charging_event.zip");
  300. for(i=0;i<cnt;i++)
  301. {
  302. if(month-i<1)
  303. {
  304. tm=month-i+12;
  305. ty=year-1;
  306. }
  307. else
  308. {
  309. tm=month-i;
  310. ty=year;
  311. }
  312. sprintf(cmd,"%s \t /Storage/EventLog/*%04d*%02d*",cmd,ty,tm);
  313. sprintf(cmd,"%s \t /Storage/OCPP/*%04d*%02d*OcppMessageLog*",cmd,ty,tm);
  314. }
  315. sprintf(cmd,"%s \t /Storage/ChargeLog/*.db",cmd);
  316. sprintf(cmd,"%s \t /Storage/EventLog/*.db",cmd);
  317. system(cmd);
  318. // Combine all zip file to one
  319. system("exec zip -9 /mnt/log.zip /mnt/system.zip /mnt/charging_event.zip");
  320. system("exec rm -f /mnt/system.zip /mnt/charging_event.zip");
  321. if(isCCS==1)
  322. {
  323. const char* server="192.168.0.10";
  324. const char* username="vern";
  325. const char* password="vern@delta";
  326. //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);
  327. sprintf(cmd,"/usr/bin/ftpput -u %s -p %s %s /Storage/CCS%d.zip /mnt/log.zip",username,password,server,CCSID);
  328. system(cmd);
  329. }
  330. printf("Log packing is done!\n");
  331. }
  332. }