ReadCmdline.c 32 KB

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