Module_Debug.c 11 KB

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