ReadCmdline.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #define TTY_PATH "/dev/tty"
  42. #define STTY_US "stty raw -echo -F "
  43. #define STTY_DEF "stty -raw echo -F "
  44. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  45. struct StatusCodeData *ShmStatusCodeData;
  46. struct PrimaryMcuData *ShmPrimaryMcuData;
  47. struct CHAdeMOData *ShmCHAdeMOData;
  48. struct CcsData *ShmCcsData;
  49. struct FanModuleData *ShmFanModuleData;
  50. struct RelayModuleData *ShmRelayModuleData;
  51. struct PsuData *ShmPsuData;
  52. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  53. int gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  54. char *msg = "state : get gun state (index) \n"
  55. "card : scanning card (x) : \n"
  56. "gun : get gun plugit state (index) \n"
  57. "lock : get gun locked state (index) \n"
  58. "self : self test state (x) \n"
  59. "ver : ver of board (407 or index or rb or fan) \n"
  60. "ac : get ac relay state (x) \n";
  61. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  62. {
  63. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  64. {
  65. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  66. {
  67. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  68. return true;
  69. }
  70. }
  71. for (byte index = 0; index < CCS_QUANTITY; index++)
  72. {
  73. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  74. {
  75. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  76. return true;
  77. }
  78. }
  79. for (byte index = 0; index < GB_QUANTITY; index++)
  80. {
  81. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  82. {
  83. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. int InitShareMemory()
  90. {
  91. int result = PASS;
  92. int MeterSMId;
  93. //initial ShmSysConfigAndInfo
  94. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  95. {
  96. result = FAIL;
  97. }
  98. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  99. {
  100. result = FAIL;
  101. }
  102. else
  103. {}
  104. //initial ShmStatusCodeData
  105. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  106. {
  107. result = FAIL;
  108. }
  109. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  110. {
  111. result = FAIL;
  112. }
  113. else
  114. {}
  115. if(CHAdeMO_QUANTITY > 0)
  116. {
  117. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),
  118. IPC_CREAT | 0777)) < 0) {
  119. result = FAIL;
  120. } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0))
  121. == (void *) -1) {
  122. result = FAIL;
  123. } else {
  124. }
  125. }
  126. if(CCS_QUANTITY > 0)
  127. {
  128. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),
  129. IPC_CREAT | 0777)) < 0) {
  130. result = FAIL;
  131. } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  132. result = FAIL;
  133. } else {
  134. }
  135. }
  136. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  137. {
  138. result = FAIL;
  139. }
  140. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  141. {
  142. result = FAIL;
  143. }
  144. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  145. {
  146. result = FAIL;
  147. }
  148. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  149. {
  150. result = FAIL;
  151. }
  152. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  153. {
  154. result = FAIL;
  155. }
  156. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  157. {
  158. result = FAIL;
  159. }
  160. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  161. {
  162. result = FAIL;
  163. }
  164. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  165. {
  166. result = FAIL;
  167. }
  168. return result;
  169. }
  170. void RunStatusProc(char *v1, char *v2)
  171. {
  172. int _index = atoi(v1);
  173. if (!FindChargingInfoData(_index, &_chargingData[0]))
  174. {
  175. printf ("FindChargingInfoData error\n");
  176. return;
  177. }
  178. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  179. {
  180. // get
  181. printf ("index = %x, status = %x (%d)\n", _index, _chargingData[_index]->SystemStatus, _chargingData[_index]->IsAvailable);
  182. }
  183. else
  184. {
  185. // set
  186. _chargingData[_index]->SystemStatus = atoi(v2);
  187. }
  188. }
  189. void RunCardProc(char *v1, char *v2)
  190. {
  191. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
  192. {
  193. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
  194. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  195. }
  196. else
  197. {
  198. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  199. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  200. }
  201. }
  202. void RunGunPlugitProc(char *v1, char *v2)
  203. {
  204. int _index = atoi(v1);
  205. if (!FindChargingInfoData(_index, &_chargingData[0]))
  206. {
  207. printf("FindChargingInfoData error\n");
  208. return;
  209. }
  210. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  211. {
  212. // get
  213. printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
  214. }
  215. else
  216. {
  217. // set
  218. _chargingData[_index]->ConnectorPlugIn = atoi(v2);
  219. }
  220. }
  221. void GetGunLockStatusProc(char *v1, char *v2)
  222. {
  223. if (strcmp(v1, "0") == 0)
  224. {
  225. printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked);
  226. }
  227. if (strcmp(v1, "1") == 0)
  228. {
  229. printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked);
  230. }
  231. }
  232. void SetSystemIDProc()
  233. {
  234. char *systemId = "Alston_Test";
  235. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, systemId, strlen(systemId));
  236. }
  237. void RunSelfProc()
  238. {
  239. printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
  240. }
  241. void GetFwVerProc(char *v1)
  242. {
  243. if (strcmp(v1, "407") == 0)
  244. {
  245. printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
  246. }
  247. else if (strcmp(v1, "0") == 0)
  248. {
  249. printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version);
  250. printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  251. }
  252. else if (strcmp(v1, "1") == 0)
  253. {
  254. printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version);
  255. printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  256. }
  257. else if (strcmp(v1, "rb") == 0)
  258. {
  259. printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  260. }
  261. else if (strcmp(v1, "fan") == 0)
  262. {
  263. printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  264. }
  265. else if (strcmp(v1, "dc") == 0)
  266. {
  267. printf("DC Main Version = %s \n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  268. }
  269. }
  270. void FwUpdateFlagProc()
  271. {
  272. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
  273. }
  274. void CheckAcStatus(char *v1)
  275. {
  276. if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
  277. {
  278. printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  279. }
  280. }
  281. void SetCableChkStatus(char *v1, char *v2)
  282. {
  283. int _index = atoi(v1);
  284. if (!FindChargingInfoData(_index, &_chargingData[0]))
  285. {
  286. printf ("FindChargingInfoData error\n");
  287. return;
  288. }
  289. _chargingData[_index]->GroundFaultStatus = atoi(v2);
  290. }
  291. void SetPowerValue(char *v1, char *v2)
  292. {
  293. // int _index = atoi(v1);
  294. // if (!FindChargingInfoData(_index, &_chargingData[0])) {
  295. // printf("FindChargingInfoData error\n");
  296. // return;
  297. // }
  298. //
  299. // if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0) {
  300. // // get
  301. // printf("index = %x, max cur = %f\n", _index,
  302. // _chargingData[_index]->MaxChargingCurrentForTest);
  303. // } else {
  304. // // set
  305. // _chargingData[_index]->MaxChargingCurrentForTest = atoi(v2);
  306. // }
  307. }
  308. void GetGunSelectedNum(char *v1)
  309. {
  310. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  311. printf("connector selected = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  312. else
  313. {
  314. int _index = atoi(v1);
  315. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  316. printf("connector select changed = %d \n", _index);
  317. }
  318. }
  319. void SetFanSpeed(char *v1)
  320. {
  321. int speed = atoi(v1);
  322. ShmFanModuleData->TestFanSpeed = speed;
  323. }
  324. void GetFanSpeed()
  325. {
  326. printf("ShmFanModuleData->PresentFan1Speed = %d \n", ShmFanModuleData->PresentFan1Speed);
  327. printf("ShmFanModuleData->PresentFan2Speed = %d \n", ShmFanModuleData->PresentFan2Speed);
  328. printf("ShmFanModuleData->PresentFan3Speed = %d \n", ShmFanModuleData->PresentFan3Speed);
  329. printf("ShmFanModuleData->PresentFan4Speed = %d \n", ShmFanModuleData->PresentFan4Speed);
  330. }
  331. void GetPsuInformation(char *v1)
  332. {
  333. printf("*************************************************\n");
  334. if(strcmp(v1, "count") == 0)
  335. {
  336. for (int i = 0; i < 4; i++)
  337. {
  338. printf("Group Index = %d, Module Count = %d \n", i, ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity);
  339. }
  340. }
  341. else if(strcmp(v1, "ver") == 0)
  342. {
  343. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  344. {
  345. for (int j = 0; j < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; j++)
  346. {
  347. printf("Group Index = %d, Psu Index = %d, Version = %s \n",
  348. i, j, ShmPsuData->PsuGroup[i].PsuModule[j].FwVersion);
  349. }
  350. }
  351. }
  352. else if(strcmp(v1, "cap") == 0)
  353. {
  354. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  355. {
  356. printf("Group Index = %d, MaxCur = %d, Power = %d \n",
  357. i, ShmPsuData->PsuGroup[i].GroupAvailableCurrent, ShmPsuData->PsuGroup[i].GroupAvailablePower);
  358. }
  359. }
  360. else if (strcmp(v1, "output") == 0)
  361. {
  362. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  363. {
  364. printf("Group Index = %d, OutputV = %d, OutputC = %d \n",
  365. i, ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage, ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent);
  366. }
  367. }
  368. printf("*************************************************\n");
  369. }
  370. static int get_char()
  371. {
  372. fd_set rfds;
  373. struct timeval tv;
  374. int ch = 0;
  375. FD_ZERO(&rfds);
  376. FD_SET(0, &rfds);
  377. tv.tv_sec = 0;
  378. tv.tv_usec = 10; //wait input timout time
  379. //if input
  380. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  381. {
  382. ch = getchar();
  383. }
  384. return ch;
  385. }
  386. void RunUnconditionalChargeIndex1(char *v1, char *v2, char *v3)
  387. {
  388. int _GunIndex = atoi(v1);
  389. float _Voltage = atof(v2);
  390. int _Current = atoi(v3);
  391. unsigned char PreviousSystemStatus = 0xff;
  392. int ch = 0;
  393. if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
  394. {
  395. printf ("FindChargingInfoData error\n");
  396. return;
  397. }
  398. printf ("ReqVoltage = %f, ReqCurrent = %d\n", _Voltage,_Current);
  399. if(_Voltage > 1000 || _Voltage < 150){
  400. printf ("Input Voltage over range\n");
  401. return;
  402. }
  403. // if(_Current > 100 || _Current < 2){
  404. //
  405. // printf ("Input Current over range\n");
  406. // return;
  407. // }
  408. //測試期間先跳過自我測試 _STEST_COMPLETE = 0xfe
  409. //ShmSysConfigAndInfo->SysInfo.SelfTestSeq = 0xfe;
  410. //kill ev task
  411. system("killall Module_EvComm");
  412. _Voltage = (_Voltage * 10);
  413. _Current = (_Current * 10);
  414. //system(STTY_US TTY_PATH);
  415. while(true)
  416. {
  417. //fix gun 1
  418. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _GunIndex;
  419. switch(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  420. {
  421. case S_IDLE:
  422. {
  423. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  424. {
  425. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  426. printf ("[UnconditionalCharge - S_IDLE]\n");
  427. }
  428. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARNING;
  429. }
  430. break;
  431. case S_PREPARNING:
  432. {
  433. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  434. {
  435. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  436. printf ("[UnconditionalCharge - S_PREPARNIN]\n");
  437. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  438. printf ("wait find module\n");
  439. }
  440. //main 會在此階段判斷以下資料跳到下一個 state
  441. //用來得知 AC 是否有搭上 (搭上模組的資訊才會出來) 因為每次 AC_Contactor
  442. //ShmPsuData->SystemPresentPsuQuantity;
  443. //ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity;
  444. //ShmPsuData->PsuGroup[gun_index].GroupAvailablePower;
  445. //_chargingData[gun_index]->AvailableChargingPower;
  446. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  447. //sleep(10);
  448. //清除 main timeout 機制
  449. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  450. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  451. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  452. }
  453. break;
  454. case S_PREPARING_FOR_EV:
  455. {
  456. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  457. {
  458. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  459. printf ("[UnconditionalCharge - S_PREPARING_FOR_EV]\n");
  460. printf ("ReqVoltage = %f, ReqCurrent = %d\n", _Voltage,_Current);
  461. }
  462. //清除 main timeout 機制
  463. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  464. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  465. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  466. //充電電壓電流
  467. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  468. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 5000;
  469. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 20;
  470. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  471. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  472. //確定模組己升壓完成
  473. //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage <= (3000+500) &&
  474. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage >= (3000-500) )
  475. {
  476. printf ("Precharge Done = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  477. //EV done
  478. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARING_FOR_EVSE;
  479. }
  480. }
  481. break;
  482. case S_PREPARING_FOR_EVSE:
  483. {
  484. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  485. {
  486. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  487. printf ("[UnconditionalCharge - S_PREPARING_FOR_EVSE]\n");
  488. }
  489. //printf ("tar vol = %d \n", _Voltage);
  490. //printf ("tar cur = %d \n", _Current);
  491. //清除 main timeout 機制
  492. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  493. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  494. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  495. //充電電壓電流
  496. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  497. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 5000;
  498. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 20;
  499. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  500. //printf ("tar vol_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage);
  501. // printf ("tar cur_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent);
  502. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  503. //確定模組己升壓完成
  504. if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x01 ||
  505. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03)
  506. {
  507. printf ("First Ground Fault State (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  508. printf ("Wait K1K2 = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  509. sleep(5);
  510. //EV done
  511. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_CHARGING;
  512. }
  513. else if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus > 0x02)
  514. {
  515. printf ("First Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  516. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  517. }
  518. }
  519. break;
  520. case S_CHARGING:
  521. {
  522. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  523. {
  524. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  525. printf ("[UnconditionalCharge - S_CHARGING]\n");
  526. }
  527. //充電電壓電流
  528. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  529. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _Voltage;
  530. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _Current;
  531. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  532. //ev task do this
  533. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingPower = ((float)((_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage / 10) * (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingCurrent / 10)) / 1000);
  534. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x02){
  535. printf ("Charging Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  536. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  537. }
  538. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03){
  539. printf ("Charging Ground Fault Warning (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  540. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  541. }
  542. }
  543. break;
  544. case S_TERMINATING:
  545. {
  546. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  547. {
  548. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  549. system("/root/Module_EvComm &");
  550. printf ("[UnconditionalCharge - S_TERMINATING]\n");
  551. //無阻塞偵測 keybaord 結束
  552. system(STTY_DEF TTY_PATH);
  553. }
  554. sleep(3);
  555. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_COMPLETE;
  556. return;
  557. }
  558. break;
  559. case S_COMPLETE:
  560. {
  561. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  562. {
  563. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  564. printf ("[UnconditionalCharge - S_COMPLETE]\n");
  565. }
  566. sleep(3);
  567. return;
  568. }
  569. break;
  570. }
  571. /*
  572. //使用 Keyboard input 阻塞方法
  573. fgets(InputChar, sizeof(InputChar), stdin);
  574. if (InputChar[0] == 's' && InputChar[1] == 't' && InputChar[2] == 'o' && InputChar[3] == 'p')
  575. {
  576. printf ("Precharge Done\n");
  577. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  578. }
  579. */
  580. //使用 keybaord input 非阻塞方法
  581. ch = get_char();
  582. if (ch)
  583. {
  584. printf("%c \n\r", ch);
  585. switch (ch)
  586. {
  587. //Ctrl + C
  588. case 3:
  589. //system(STTY_DEF TTY_PATH);
  590. return;
  591. //input c or C
  592. case 'c':
  593. case 'C':
  594. printf("stop \n\r");
  595. //system(STTY_DEF TTY_PATH);
  596. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. int main(void)
  603. {
  604. if(InitShareMemory() == FAIL)
  605. {
  606. printf ("InitShareMemory = FAIL \n");
  607. if(ShmStatusCodeData != NULL)
  608. {
  609. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  610. }
  611. sleep(5);
  612. return 0;
  613. }
  614. for(;;)
  615. {
  616. char word[128];
  617. char newString[7][10];
  618. int i,j,ctr;
  619. fgets(word, sizeof(word), stdin);
  620. j=0; ctr=0;
  621. strcpy(newString[1], "-1");
  622. strcpy(newString[2], "-1");
  623. for (i = 0; i <= (strlen(word)); i++)
  624. {
  625. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  626. {
  627. newString[ctr][j] = '\0';
  628. ctr++;
  629. j = 0;
  630. }
  631. else
  632. {
  633. newString[ctr][j] = word[i];
  634. j++;
  635. }
  636. }
  637. if(strcmp(newString[0], "state") == 0)
  638. {
  639. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  640. continue;
  641. // 槍狀態
  642. RunStatusProc(newString[1], newString[2]);
  643. }
  644. else if(strcmp(newString[0], "card") == 0)
  645. {
  646. // 刷卡狀態
  647. RunCardProc(newString[1], newString[2]);
  648. }
  649. else if(strcmp(newString[0], "gun") == 0)
  650. {
  651. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  652. continue;
  653. // 插槍狀態
  654. RunGunPlugitProc(newString[1], newString[2]);
  655. }
  656. else if(strcmp(newString[0], "lock") == 0)
  657. {
  658. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  659. continue;
  660. // 插槍狀態
  661. GetGunLockStatusProc(newString[1], newString[2]);
  662. }
  663. else if(strcmp(newString[0], "sysid") == 0)
  664. {
  665. // 測試 sys id
  666. SetSystemIDProc();
  667. }
  668. else if(strcmp(newString[0], "self") == 0)
  669. {
  670. // CSU 自我檢測狀態
  671. RunSelfProc(newString[1]);
  672. }
  673. else if(strcmp(newString[0], "ver") == 0)
  674. {
  675. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  676. continue;
  677. // 取 FW 版號
  678. GetFwVerProc(newString[1]);
  679. }
  680. else if (strcmp(newString[0], "update") == 0)
  681. {
  682. // 更新
  683. FwUpdateFlagProc(newString[1]);
  684. }
  685. else if (strcmp(newString[0], "ac") == 0)
  686. {
  687. // AC contactor 狀態
  688. CheckAcStatus(newString[1]);
  689. }
  690. else if (strcmp(newString[0], "cable") == 0)
  691. {
  692. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  693. continue;
  694. // cable check pass
  695. SetCableChkStatus(newString[1], newString[2]);
  696. }
  697. else if (strcmp(newString[0], "pow") == 0)
  698. {
  699. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  700. continue;
  701. // cable check pass
  702. SetPowerValue(newString[1], newString[2]);
  703. }
  704. else if(strcmp(newString[0], "select") == 0)
  705. {
  706. // 取得當前選的槍號
  707. GetGunSelectedNum(newString[1]);
  708. }
  709. else if(strcmp(newString[0], "fan") == 0)
  710. {
  711. // 設定風扇速度
  712. SetFanSpeed(newString[1]);
  713. }
  714. else if(strcmp(newString[0], "speed") == 0)
  715. {
  716. // 取得風扇速度
  717. GetFanSpeed();
  718. }
  719. else if(strcmp(newString[0], "psu") == 0)
  720. {
  721. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  722. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  723. {
  724. printf ("PSU : Param fail..Please retry again......\n");
  725. continue;
  726. }
  727. // 取得 PSU 資訊
  728. GetPsuInformation(newString[1]);
  729. }
  730. else if(strcmp(newString[0], "strchg") == 0)
  731. {
  732. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  733. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 ||
  734. strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  735. {
  736. printf ("Input cmd fail ------ strchg [vol 150-1000] [cru 2-100]\n");
  737. continue;
  738. }
  739. // 槍狀態
  740. RunUnconditionalChargeIndex1(newString[1], newString[2], newString[3]);
  741. }
  742. printf ("%s\n", msg);
  743. usleep(100000);
  744. }
  745. return 0;
  746. }