ReadCmdline.c 26 KB

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