Module_Debug.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #include "main.h"
  31. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  32. #define PASS 1
  33. #define FAIL 0
  34. #define ON 1
  35. #define OFF 0
  36. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  37. struct StatusCodeData *ShmStatusCodeData;
  38. struct OCPP16Data *ShmOCPP16Data;
  39. struct Charger *ShmCharger;
  40. int StoreLogMsg(const char *fmt, ...)
  41. {
  42. char Buf[4096+256];
  43. char buffer[4096];
  44. time_t CurrentTime;
  45. struct tm *tm;
  46. struct timeval tv;
  47. va_list args;
  48. va_start(args, fmt);
  49. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  50. va_end(args);
  51. memset(Buf,0,sizeof(Buf));
  52. CurrentTime = time(NULL);
  53. tm=localtime(&CurrentTime);
  54. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  55. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_DebugLog",
  56. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  57. buffer,
  58. tm->tm_year+1900,tm->tm_mon+1);
  59. #ifdef SystemLogMessage
  60. system(Buf);
  61. #endif
  62. #ifdef ConsloePrintLog
  63. 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);
  64. #endif
  65. return rc;
  66. }
  67. int InitShareMemory()
  68. {
  69. int result = PASS;
  70. int MeterSMId;
  71. //Initial ShmSysConfigAndInfo
  72. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  73. {
  74. #ifdef SystemLogMessage
  75. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  76. #endif
  77. result = FAIL;
  78. }
  79. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  80. {
  81. #ifdef SystemLogMessage
  82. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  83. #endif
  84. result = FAIL;
  85. }
  86. //Initial ShmStatusCodeData
  87. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  88. {
  89. #ifdef SystemLogMessage
  90. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  91. #endif
  92. result = FAIL;
  93. }
  94. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  95. {
  96. #ifdef SystemLogMessage
  97. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  98. #endif
  99. result = FAIL;
  100. }
  101. // Initial ShmOCPP16Data
  102. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  103. {
  104. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  105. result = FAIL;
  106. }
  107. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  108. {
  109. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  110. result = FAIL;
  111. }
  112. //Initial ShmCharger
  113. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  114. {
  115. #ifdef SystemLogMessage
  116. DEBUG_ERROR("shmget ShmChargerKey NG\r\n");
  117. #endif
  118. result = FAIL;
  119. }
  120. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  121. {
  122. #ifdef SystemLogMessage
  123. DEBUG_ERROR("shmat ShmChargerKey NG\r\n");
  124. #endif
  125. result = FAIL;
  126. }
  127. return result;
  128. }
  129. void wait()
  130. {
  131. printf("\n Please press any key then \"Enter\" to continue.");
  132. while(getchar() =='\n') usleep(100000);
  133. }
  134. int main(void)
  135. {
  136. char cmd[128];
  137. if(InitShareMemory() == FAIL)
  138. {
  139. #ifdef SystemLogMessage
  140. DEBUG_ERROR("InitShareMemory NG\n");
  141. #endif
  142. if(ShmStatusCodeData!=NULL)
  143. {
  144. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  145. }
  146. sleep(5);
  147. return 0;
  148. }
  149. else
  150. {
  151. DEBUG_INFO("InitShareMemory OK.\r\n");
  152. }
  153. for(;;)
  154. {
  155. system("clear");
  156. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  157. printf("\n ============== Debug main menu ==================");
  158. printf("\n info: List charger status info.");
  159. printf("\n test: Charger test command.");
  160. printf("\n exit: Exit Module_Debug_Test.");
  161. printf("\n =================================================");
  162. printf("\n Please input debug command: ");
  163. scanf("%s", &cmd[0]);
  164. if(strcmp(cmd, "info") == 0)
  165. {
  166. uint8_t isExit = FALSE;
  167. int gun_index;
  168. do
  169. {
  170. system("clear");
  171. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  172. printf("\n ***************** Info menu *********************");
  173. printf("\n csu: charger info");
  174. printf("\n mcu: mcu request state");
  175. printf("\n exit: exit to previous menu.");
  176. printf("\n *************************************************");
  177. printf("\n Please input info need to query: ");
  178. scanf("%s", &cmd[0]);
  179. if(strcmp(cmd, "csu") == 0)
  180. {
  181. printf("\n Please input gun index: ");
  182. scanf("%d", &gun_index);
  183. printf("\n CSU info.\n\n");
  184. gun_index = gun_index<AC_QUANTITY?gun_index:0;
  185. printf("\n CSU u-boot version: %s", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  186. printf("\n CSU kernel version: %s", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  187. printf("\n CSU rootfs version: %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  188. printf("\n --------------------------------------------------");
  189. printf("\n Charger connector plug times: %d", ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  190. printf("\n --------------------------------------------------");
  191. printf("\n CP positive voltage: %.2f", ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive);
  192. printf("\n CP negative voltage: %.2f", ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative);
  193. printf("\n --------------------------------------------------");
  194. printf("\n CSU to MCU legacyRequest: %d", ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  195. printf("\n CSU to MCU relay on request: %d", ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn);
  196. printf("\n --------------------------------------------------");
  197. printf("\n Charger charging mode BS/HLC: %d", ShmCharger->gun_info[gun_index].chargingMode);
  198. printf("\n --------------------------------------------------");
  199. printf("\n Charger input voltage L1: %.2f", ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  200. printf("\n --------------------------------------------------");
  201. printf("\n CSU output current L1: %.2f", ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  202. printf("\n --------------------------------------------------");
  203. printf("\n CSU power total consumption: %.2f", (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0));
  204. printf("\n --------------------------------------------------");
  205. printf("\n CSU temperature: %d", ShmCharger->gun_info[gun_index].temperature.point[0]);
  206. wait();
  207. }
  208. else if(strcmp(cmd, "mcu") == 0)
  209. {
  210. printf("\n Please input gun index: ");
  211. scanf("%d", &gun_index);
  212. printf("\n MCU info.\n\n");
  213. gun_index = gun_index<AC_QUANTITY?gun_index:0;
  214. printf("\n Firmware version: %s", ShmCharger->gun_info[gun_index].ver.Version_FW);
  215. wait();
  216. }
  217. else if(strcmp(cmd, "exit") == 0)
  218. {
  219. printf("\n Exit to previous.\n\n");
  220. isExit = TRUE;
  221. }
  222. }while(!isExit);
  223. }
  224. else if(strcmp(cmd, "test") == 0)
  225. {
  226. int gun_index;
  227. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  228. printf("\n ***************** Info menu *********************");
  229. printf("\n start: start charging session");
  230. printf("\n stop: stop charging session");
  231. printf("\n operative: enable charger gun");
  232. printf("\n inoperative: disable charger gun");
  233. printf("\n exit: exit to previous menu.");
  234. printf("\n *************************************************");
  235. printf("\n Please input command: ");
  236. scanf("%s", &cmd[0]);
  237. if(strcmp(cmd, "start") == 0)
  238. {
  239. printf("\n Please input gun index: ");
  240. scanf("%d", &gun_index);
  241. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = ON;
  242. }
  243. else if(strcmp(cmd, "stop") == 0)
  244. {
  245. printf("\n Please input gun index: ");
  246. scanf("%d", &gun_index);
  247. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = ON;
  248. }
  249. else if(strcmp(cmd, "operative") == 0)
  250. {
  251. printf("\n Please input gun index: ");
  252. scanf("%d", &gun_index);
  253. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  254. {
  255. sprintf((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative");
  256. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = ON;
  257. }
  258. else
  259. {
  260. }
  261. }
  262. else if(strcmp(cmd, "inoperative") == 0)
  263. {
  264. printf("\n Please input gun index: ");
  265. scanf("%d", &gun_index);
  266. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  267. {
  268. sprintf((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Inoperative");
  269. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = ON;
  270. }
  271. else
  272. {
  273. }
  274. }
  275. else if(strcmp(cmd, "exit") == 0)
  276. {
  277. printf("\n Exit to previous.\n\n");
  278. }
  279. }
  280. else if(strcmp(cmd, "exit") == 0)
  281. {
  282. printf("\n exit program.\n\n");
  283. return FAIL;
  284. }
  285. }
  286. return FAIL;
  287. }