Module_Debug.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Charger *ShmCharger;
  39. int StoreLogMsg(const char *fmt, ...)
  40. {
  41. char Buf[4096+256];
  42. char buffer[4096];
  43. time_t CurrentTime;
  44. struct tm *tm;
  45. struct timeval tv;
  46. va_list args;
  47. va_start(args, fmt);
  48. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  49. va_end(args);
  50. memset(Buf,0,sizeof(Buf));
  51. CurrentTime = time(NULL);
  52. tm=localtime(&CurrentTime);
  53. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  54. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_DebugLog",
  55. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  56. buffer,
  57. tm->tm_year+1900,tm->tm_mon+1);
  58. #ifdef SystemLogMessage
  59. system(Buf);
  60. #endif
  61. #ifdef ConsloePrintLog
  62. 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);
  63. #endif
  64. return rc;
  65. }
  66. int InitShareMemory()
  67. {
  68. int result = PASS;
  69. int MeterSMId;
  70. //Initial ShmSysConfigAndInfo
  71. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  72. {
  73. #ifdef SystemLogMessage
  74. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  75. #endif
  76. result = FAIL;
  77. }
  78. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  79. {
  80. #ifdef SystemLogMessage
  81. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  82. #endif
  83. result = FAIL;
  84. }
  85. //Initial ShmStatusCodeData
  86. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  87. {
  88. #ifdef SystemLogMessage
  89. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  90. #endif
  91. result = FAIL;
  92. }
  93. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  94. {
  95. #ifdef SystemLogMessage
  96. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  97. #endif
  98. result = FAIL;
  99. }
  100. //Initial ShmCharger
  101. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  102. {
  103. #ifdef SystemLogMessage
  104. DEBUG_ERROR("shmget ShmChargerKey NG\r\n");
  105. #endif
  106. result = FAIL;
  107. }
  108. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  109. {
  110. #ifdef SystemLogMessage
  111. DEBUG_ERROR("shmat ShmChargerKey NG\r\n");
  112. #endif
  113. result = FAIL;
  114. }
  115. return result;
  116. }
  117. void wait()
  118. {
  119. printf("\n Please press any key then \"Enter\" to continue.");
  120. while(getchar() =='\n') usleep(100000);
  121. }
  122. int main(void)
  123. {
  124. char cmd[128];
  125. if(InitShareMemory() == FAIL)
  126. {
  127. #ifdef SystemLogMessage
  128. DEBUG_ERROR("InitShareMemory NG\n");
  129. #endif
  130. if(ShmStatusCodeData!=NULL)
  131. {
  132. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  133. }
  134. sleep(5);
  135. return 0;
  136. }
  137. else
  138. {
  139. DEBUG_INFO("InitShareMemory OK.\r\n");
  140. }
  141. for(;;)
  142. {
  143. system("clear");
  144. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  145. printf("\n ============== Debug main menu ==================");
  146. printf("\n info: List charger status info.");
  147. printf("\n exit: Exit Module_Debug_Test.");
  148. printf("\n =================================================");
  149. printf("\n Please input debug command: ");
  150. scanf("%s", &cmd[0]);
  151. if(strcmp(cmd, "info") == 0)
  152. {
  153. uint8_t isExit = FALSE;
  154. int gun_index;
  155. do
  156. {
  157. system("clear");
  158. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  159. printf("\n ***************** Info menu *********************");
  160. printf("\n csu: charger info");
  161. printf("\n mcu: mcu request state");
  162. printf("\n exit: exit to previous menu.");
  163. printf("\n *************************************************");
  164. printf("\n Please input info need to query: ");
  165. scanf("%s", &cmd[0]);
  166. if(strcmp(cmd, "csu") == 0)
  167. {
  168. printf("\n Please input gun index: ");
  169. scanf("%d", &gun_index);
  170. printf("\n CSU info.\n\n");
  171. gun_index = gun_index<AC_QUANTITY?gun_index:0;
  172. printf("\n Charger connector plug times: %d", ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  173. printf("\n CP positive voltage: %.2f", ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive);
  174. printf("\n CP negative voltage: %.2f", ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative);
  175. printf("\n CSU to MCU legacyRequest: %d", ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  176. printf("\n CSU to MCU relay on request: %d", ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn);
  177. printf("\n Charger charging mode BS/HLC: %d", ShmCharger->gun_info[gun_index].chargingMode);
  178. printf("\n Charger input voltage L1: %.2f", ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  179. printf("\n CSU output current L1: %.2f", ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  180. printf("\n CSU temperature: %d", ShmCharger->gun_info[gun_index].temperature.point[0]);
  181. wait();
  182. }
  183. else if(strcmp(cmd, "mcu") == 0)
  184. {
  185. printf("\n Please input gun index: ");
  186. scanf("%d", &gun_index);
  187. printf("\n MCU info.\n\n");
  188. wait();
  189. }
  190. else if(strcmp(cmd, "exit") == 0)
  191. {
  192. printf("\n Exit to previous.\n\n");
  193. isExit = TRUE;
  194. }
  195. }while(!isExit);
  196. }
  197. else if(strcmp(cmd, "exit") == 0)
  198. {
  199. printf("\n exit program.\n\n");
  200. return FAIL;
  201. }
  202. }
  203. return FAIL;
  204. }