ReadCmdline.c 30 KB

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