ReadCmdline.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. int gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  50. char *msg = "state : get gun state (index) \n"
  51. "card : scanning card (x) : \n"
  52. "gun : get gun plugit state (index) \n"
  53. "lock : get gun locked state (index) \n"
  54. "self : self test state (x) \n"
  55. "ver : ver of board (407 or index or rb or fan) \n"
  56. "ac : get ac relay state (x) \n";
  57. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  58. {
  59. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  60. {
  61. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  62. {
  63. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  64. return true;
  65. }
  66. }
  67. for (byte index = 0; index < CCS_QUANTITY; index++)
  68. {
  69. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  70. {
  71. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  72. return true;
  73. }
  74. }
  75. for (byte index = 0; index < GB_QUANTITY; index++)
  76. {
  77. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  78. {
  79. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. int InitShareMemory()
  86. {
  87. int result = PASS;
  88. int MeterSMId;
  89. //initial ShmSysConfigAndInfo
  90. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  91. {
  92. result = FAIL;
  93. }
  94. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  95. {
  96. result = FAIL;
  97. }
  98. else
  99. {}
  100. //initial ShmStatusCodeData
  101. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  102. {
  103. result = FAIL;
  104. }
  105. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  106. {
  107. result = FAIL;
  108. }
  109. else
  110. {}
  111. if(CHAdeMO_QUANTITY > 0)
  112. {
  113. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),
  114. IPC_CREAT | 0777)) < 0) {
  115. result = FAIL;
  116. } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0))
  117. == (void *) -1) {
  118. result = FAIL;
  119. } else {
  120. }
  121. }
  122. if(CCS_QUANTITY > 0)
  123. {
  124. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),
  125. IPC_CREAT | 0777)) < 0) {
  126. result = FAIL;
  127. } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  128. result = FAIL;
  129. } else {
  130. }
  131. }
  132. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  133. {
  134. result = FAIL;
  135. }
  136. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  137. {
  138. result = FAIL;
  139. }
  140. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  141. {
  142. result = FAIL;
  143. }
  144. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  145. {
  146. result = FAIL;
  147. }
  148. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  149. {
  150. result = FAIL;
  151. }
  152. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  153. {
  154. result = FAIL;
  155. }
  156. return result;
  157. }
  158. void RunStatusProc(char *v1, char *v2)
  159. {
  160. int _index = atoi(v1);
  161. if (!FindChargingInfoData(_index, &_chargingData[0]))
  162. {
  163. printf ("FindChargingInfoData error\n");
  164. return;
  165. }
  166. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  167. {
  168. // get
  169. printf ("index = %x, status = %x\n", _index, _chargingData[_index]->SystemStatus);
  170. }
  171. else
  172. {
  173. // set
  174. _chargingData[_index]->SystemStatus = atoi(v2);
  175. }
  176. }
  177. void RunCardProc(char *v1, char *v2)
  178. {
  179. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
  180. {
  181. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
  182. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  183. }
  184. else
  185. {
  186. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  187. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  188. }
  189. }
  190. void RunGunPlugitProc(char *v1, char *v2)
  191. {
  192. int _index = atoi(v1);
  193. if (!FindChargingInfoData(_index, &_chargingData[0]))
  194. {
  195. printf("FindChargingInfoData error\n");
  196. return;
  197. }
  198. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  199. {
  200. // get
  201. printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
  202. }
  203. else
  204. {
  205. // set
  206. _chargingData[_index]->ConnectorPlugIn = atoi(v2);
  207. }
  208. }
  209. void GetGunLockStatusProc(char *v1, char *v2)
  210. {
  211. if (strcmp(v1, "0") == 0)
  212. {
  213. printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked);
  214. }
  215. if (strcmp(v1, "1") == 0)
  216. {
  217. printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked);
  218. }
  219. }
  220. void SetSystemIDProc()
  221. {
  222. char *systemId = "Alston_Test";
  223. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, systemId, strlen(systemId));
  224. }
  225. void RunSelfProc()
  226. {
  227. printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
  228. }
  229. void GetFwVerProc(char *v1)
  230. {
  231. if (strcmp(v1, "407") == 0)
  232. {
  233. printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
  234. }
  235. else if (strcmp(v1, "0") == 0)
  236. {
  237. printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version);
  238. printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  239. }
  240. else if (strcmp(v1, "1") == 0)
  241. {
  242. printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version);
  243. printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  244. }
  245. else if (strcmp(v1, "rb") == 0)
  246. {
  247. printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  248. }
  249. else if (strcmp(v1, "fan") == 0)
  250. {
  251. printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  252. }
  253. }
  254. void FwUpdateFlagProc()
  255. {
  256. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
  257. }
  258. void CheckAcStatus(char *v1)
  259. {
  260. if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
  261. {
  262. printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  263. }
  264. }
  265. void SetCableChkStatus(char *v1, char *v2)
  266. {
  267. int _index = atoi(v1);
  268. if (!FindChargingInfoData(_index, &_chargingData[0]))
  269. {
  270. printf ("FindChargingInfoData error\n");
  271. return;
  272. }
  273. _chargingData[_index]->GroundFaultStatus = atoi(v2);
  274. }
  275. void SetPowerValue(char *v1, char *v2)
  276. {
  277. // int _index = atoi(v1);
  278. // if (!FindChargingInfoData(_index, &_chargingData[0])) {
  279. // printf("FindChargingInfoData error\n");
  280. // return;
  281. // }
  282. //
  283. // if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0) {
  284. // // get
  285. // printf("index = %x, max cur = %f\n", _index,
  286. // _chargingData[_index]->MaxChargingCurrentForTest);
  287. // } else {
  288. // // set
  289. // _chargingData[_index]->MaxChargingCurrentForTest = atoi(v2);
  290. // }
  291. }
  292. void GetGunSelectedNum(char *v1)
  293. {
  294. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  295. printf("connector selected = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  296. else
  297. {
  298. int _index = atoi(v1);
  299. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  300. printf("connector select changed = %d \n", _index);
  301. }
  302. }
  303. void ChangeToSpMode()
  304. {
  305. unsigned char _index = 0;
  306. if (gunCount <= 0)
  307. return;
  308. for (int i = 0; i < gunCount; i++)
  309. {
  310. _chargingData[i]->SystemStatus = 20;
  311. }
  312. if (!FindChargingInfoData(_index, &_chargingData[0]))
  313. {
  314. printf ("FindChargingInfoData error\n");
  315. return;
  316. }
  317. _chargingData[_index]->SystemStatus = 19;
  318. }
  319. int main(void)
  320. {
  321. if(InitShareMemory() == FAIL)
  322. {
  323. printf ("InitShareMemory = FAIL \n");
  324. if(ShmStatusCodeData != NULL)
  325. {
  326. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  327. }
  328. sleep(5);
  329. return 0;
  330. }
  331. for(;;)
  332. {
  333. char word[128];
  334. char newString[7][10];
  335. int i,j,ctr;
  336. fgets(word, sizeof(word), stdin);
  337. j=0; ctr=0;
  338. strcpy(newString[1], "-1");
  339. strcpy(newString[2], "-1");
  340. for (i = 0; i <= (strlen(word)); i++)
  341. {
  342. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  343. {
  344. newString[ctr][j] = '\0';
  345. ctr++;
  346. j = 0;
  347. }
  348. else
  349. {
  350. newString[ctr][j] = word[i];
  351. j++;
  352. }
  353. }
  354. if(strcmp(newString[0], "state") == 0)
  355. {
  356. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  357. continue;
  358. // 槍狀態
  359. RunStatusProc(newString[1], newString[2]);
  360. continue;
  361. }
  362. else if(strcmp(newString[0], "card") == 0)
  363. {
  364. // 刷卡狀態
  365. RunCardProc(newString[1], newString[2]);
  366. continue;
  367. }
  368. else if(strcmp(newString[0], "gun") == 0)
  369. {
  370. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  371. continue;
  372. // 插槍狀態
  373. RunGunPlugitProc(newString[1], newString[2]);
  374. continue;
  375. }
  376. else if(strcmp(newString[0], "lock") == 0)
  377. {
  378. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  379. continue;
  380. // 插槍狀態
  381. GetGunLockStatusProc(newString[1], newString[2]);
  382. continue;
  383. }
  384. else if(strcmp(newString[0], "sysid") == 0)
  385. {
  386. // 測試 sys id
  387. SetSystemIDProc();
  388. continue;
  389. }
  390. else if(strcmp(newString[0], "self") == 0)
  391. {
  392. // CSU 自我檢測狀態
  393. RunSelfProc(newString[1]);
  394. continue;
  395. }
  396. else if(strcmp(newString[0], "ver") == 0)
  397. {
  398. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  399. continue;
  400. // 取 FW 版號
  401. GetFwVerProc(newString[1]);
  402. continue;
  403. }
  404. else if (strcmp(newString[0], "update") == 0)
  405. {
  406. // 更新
  407. FwUpdateFlagProc(newString[1]);
  408. continue;
  409. }
  410. else if (strcmp(newString[0], "ac") == 0)
  411. {
  412. // AC contactor 狀態
  413. CheckAcStatus(newString[1]);
  414. continue;
  415. }
  416. else if (strcmp(newString[0], "cable") == 0)
  417. {
  418. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  419. continue;
  420. // cable check pass
  421. SetCableChkStatus(newString[1], newString[2]);
  422. continue;
  423. }
  424. else if (strcmp(newString[0], "pow") == 0)
  425. {
  426. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  427. continue;
  428. // cable check pass
  429. SetPowerValue(newString[1], newString[2]);
  430. continue;
  431. }
  432. else if(strcmp(newString[0], "select") == 0)
  433. {
  434. // 取得當前選的槍號
  435. GetGunSelectedNum(newString[1]);
  436. continue;
  437. }
  438. else if(strcmp(newString[0], "spmode") == 0)
  439. {
  440. // 改變系統模式到特殊測試模式
  441. //ChangeToSpMode();
  442. continue;
  443. }
  444. printf ("%s\n", msg);
  445. usleep(100000);
  446. }
  447. return 0;
  448. }