ReadCmdline.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Main.c
  3. *
  4. * Created on: 2019年8月6日
  5. * Author: 7564
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h> /*標準輸入輸出定義*/
  24. #include <stdlib.h> /*標準函數庫定義*/
  25. #include <unistd.h> /*Unix 標準函數定義*/
  26. #include <fcntl.h> /*檔控制定義*/
  27. #include <termios.h> /*PPSIX 終端控制定義*/
  28. #include <errno.h> /*錯誤號定義*/
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include <stdbool.h>
  36. #include "../../define.h"
  37. typedef unsigned char byte;
  38. #define PASS 1
  39. #define FAIL -1
  40. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  41. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  42. struct StatusCodeData *ShmStatusCodeData;
  43. struct PrimaryMcuData *ShmPrimaryMcuData;
  44. struct CHAdeMOData *ShmCHAdeMOData;
  45. struct CcsData *ShmCcsData;
  46. struct FanModuleData *ShmFanModuleData;
  47. struct RelayModuleData *ShmRelayModuleData;
  48. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  49. char *msg = "state : get gun state (index) \n"
  50. "card : scanning card (x) : \n"
  51. "gun : get gun plugit state (index) \n"
  52. "lock : get gun locked state (index) \n"
  53. "self : self test state (x) \n"
  54. "ver : ver of board (407 or index or rb or fan) \n"
  55. "ac : get ac relay state (x) \n";
  56. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  57. {
  58. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  59. {
  60. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  61. {
  62. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  63. return true;
  64. }
  65. }
  66. for (byte index = 0; index < CCS_QUANTITY; index++)
  67. {
  68. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  69. {
  70. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  71. return true;
  72. }
  73. }
  74. for (byte index = 0; index < GB_QUANTITY; index++)
  75. {
  76. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  77. {
  78. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. int InitShareMemory()
  85. {
  86. int result = PASS;
  87. int MeterSMId;
  88. //initial ShmSysConfigAndInfo
  89. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  90. {
  91. result = FAIL;
  92. }
  93. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  94. {
  95. result = FAIL;
  96. }
  97. else
  98. {}
  99. //initial ShmStatusCodeData
  100. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  101. {
  102. result = FAIL;
  103. }
  104. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  105. {
  106. result = FAIL;
  107. }
  108. else
  109. {}
  110. if(CHAdeMO_QUANTITY > 0)
  111. {
  112. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),
  113. IPC_CREAT | 0777)) < 0) {
  114. result = FAIL;
  115. } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0))
  116. == (void *) -1) {
  117. result = FAIL;
  118. } else {
  119. }
  120. }
  121. if(CCS_QUANTITY > 0)
  122. {
  123. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),
  124. IPC_CREAT | 0777)) < 0) {
  125. result = FAIL;
  126. } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  127. result = FAIL;
  128. } else {
  129. }
  130. }
  131. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  132. {
  133. result = FAIL;
  134. }
  135. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  136. {
  137. result = FAIL;
  138. }
  139. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  140. {
  141. result = FAIL;
  142. }
  143. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  144. {
  145. result = FAIL;
  146. }
  147. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  148. {
  149. result = FAIL;
  150. }
  151. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  152. {
  153. result = FAIL;
  154. }
  155. return result;
  156. }
  157. void RunStatusProc(char *v1, char *v2)
  158. {
  159. int _index = atoi(v1);
  160. if (!FindChargingInfoData(_index, &_chargingData[0]))
  161. {
  162. printf ("FindChargingInfoData error\n");
  163. return;
  164. }
  165. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  166. {
  167. // get
  168. printf ("index = %x, status = %x\n", _index, _chargingData[_index]->SystemStatus);
  169. }
  170. else
  171. {
  172. // set
  173. _chargingData[_index]->SystemStatus = atoi(v2);
  174. }
  175. }
  176. void RunCardProc(char *v1, char *v2)
  177. {
  178. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
  179. {
  180. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
  181. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  182. }
  183. else
  184. {
  185. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  186. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  187. }
  188. }
  189. void RunGunPlugitProc(char *v1, char *v2)
  190. {
  191. int _index = atoi(v1);
  192. if (!FindChargingInfoData(_index, &_chargingData[0]))
  193. {
  194. printf("FindChargingInfoData error\n");
  195. return;
  196. }
  197. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  198. {
  199. // get
  200. printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
  201. }
  202. else
  203. {
  204. // set
  205. _chargingData[_index]->ConnectorPlugIn = atoi(v2);
  206. }
  207. }
  208. void GetGunLockStatusProc(char *v1, char *v2)
  209. {
  210. if (strcmp(v1, "0") == 0)
  211. {
  212. printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked);
  213. }
  214. if (strcmp(v1, "1") == 0)
  215. {
  216. printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked);
  217. }
  218. }
  219. void RunSelfProc()
  220. {
  221. printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
  222. }
  223. void GetFwVerProc(char *v1)
  224. {
  225. if (strcmp(v1, "407") == 0)
  226. {
  227. printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
  228. }
  229. else if (strcmp(v1, "0") == 0)
  230. {
  231. printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version);
  232. printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  233. }
  234. else if (strcmp(v1, "1") == 0)
  235. {
  236. printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version);
  237. printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  238. }
  239. else if (strcmp(v1, "rb") == 0)
  240. {
  241. printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  242. }
  243. else if (strcmp(v1, "fan") == 0)
  244. {
  245. printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  246. }
  247. }
  248. void FwUpdateFlagProc()
  249. {
  250. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
  251. }
  252. void CheckAcStatus(char *v1)
  253. {
  254. if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
  255. {
  256. printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  257. }
  258. }
  259. void SetCableChkStatus(char *v1, char *v2)
  260. {
  261. int _index = atoi(v1);
  262. if (!FindChargingInfoData(_index, &_chargingData[0]))
  263. {
  264. printf ("FindChargingInfoData error\n");
  265. return;
  266. }
  267. _chargingData[_index]->GroundFaultStatus = atoi(v2);
  268. }
  269. int main(void)
  270. {
  271. if(InitShareMemory() == FAIL)
  272. {
  273. printf ("InitShareMemory = FAIL \n");
  274. if(ShmStatusCodeData != NULL)
  275. {
  276. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  277. }
  278. sleep(5);
  279. return 0;
  280. }
  281. for(;;)
  282. {
  283. char word[128];
  284. char newString[3][10];
  285. int i,j,ctr;
  286. fgets(word, sizeof(word), stdin);
  287. j=0; ctr=0;
  288. strcpy(newString[1], "-1");
  289. strcpy(newString[2], "-1");
  290. for (i = 0; i <= (strlen(word)); i++)
  291. {
  292. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  293. {
  294. newString[ctr][j] = '\0';
  295. ctr++;
  296. j = 0;
  297. }
  298. else
  299. {
  300. newString[ctr][j] = word[i];
  301. j++;
  302. }
  303. }
  304. if(strcmp(newString[0], "state") == 0)
  305. {
  306. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  307. continue;
  308. // 槍狀態
  309. RunStatusProc(newString[1], newString[2]);
  310. continue;
  311. }
  312. else if(strcmp(newString[0], "card") == 0)
  313. {
  314. // 刷卡狀態
  315. RunCardProc(newString[1], newString[2]);
  316. continue;
  317. }
  318. else if(strcmp(newString[0], "gun") == 0)
  319. {
  320. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  321. continue;
  322. // 插槍狀態
  323. RunGunPlugitProc(newString[1], newString[2]);
  324. continue;
  325. }
  326. else if(strcmp(newString[0], "lock") == 0)
  327. {
  328. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  329. continue;
  330. // 插槍狀態
  331. GetGunLockStatusProc(newString[1], newString[2]);
  332. continue;
  333. }
  334. else if(strcmp(newString[0], "self") == 0)
  335. {
  336. // CSU 自我檢測狀態
  337. RunSelfProc(newString[1]);
  338. continue;
  339. }
  340. else if(strcmp(newString[0], "ver") == 0)
  341. {
  342. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  343. continue;
  344. // 取 FW 版號
  345. GetFwVerProc(newString[1]);
  346. continue;
  347. }
  348. else if (strcmp(newString[0], "update") == 0)
  349. {
  350. // 更新
  351. FwUpdateFlagProc(newString[1]);
  352. continue;
  353. }
  354. else if (strcmp(newString[0], "ac") == 0)
  355. {
  356. // AC contactor 狀態
  357. CheckAcStatus(newString[1]);
  358. continue;
  359. }
  360. else if (strcmp(newString[0], "cable") == 0)
  361. {
  362. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  363. continue;
  364. // cable check pass
  365. SetCableChkStatus(newString[1], newString[2]);
  366. continue;
  367. }
  368. printf ("%s\n", msg);
  369. usleep(100000);
  370. }
  371. return 0;
  372. }