ReadCmdline.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 = ((float)((_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage) * (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingCurrent)) / 1000);
  683. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x02){
  684. printf ("Charging Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  685. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  686. }
  687. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03){
  688. printf ("Charging Ground Fault Warning (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  689. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  690. }
  691. }
  692. break;
  693. case S_TERMINATING:
  694. {
  695. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  696. {
  697. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  698. system("/root/Module_EvComm &");
  699. printf ("[UnconditionalCharge - S_TERMINATING]\n");
  700. //無阻塞偵測 keybaord 結束
  701. system(STTY_DEF TTY_PATH);
  702. }
  703. sleep(3);
  704. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_COMPLETE;
  705. return;
  706. }
  707. break;
  708. case S_COMPLETE:
  709. {
  710. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  711. {
  712. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  713. printf ("[UnconditionalCharge - S_COMPLETE]\n");
  714. }
  715. sleep(3);
  716. return;
  717. }
  718. break;
  719. }
  720. char word[128];
  721. char newString[7][10];
  722. int i,j,ctr;
  723. memset(word, 0x00, sizeof(word));
  724. get_char(word);
  725. if (strlen(word) == 0)
  726. continue;
  727. j=0; ctr=0;
  728. strcpy(newString[1], "-1");
  729. strcpy(newString[2], "-1");
  730. for (i = 0; i <= (strlen(word)); i++)
  731. {
  732. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  733. {
  734. newString[ctr][j] = '\0';
  735. ctr++;
  736. j = 0;
  737. }
  738. else
  739. {
  740. newString[ctr][j] = word[i];
  741. j++;
  742. }
  743. }
  744. if(strcmp(newString[0], "chg") == 0)
  745. {
  746. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  747. continue;
  748. if (strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  749. continue;
  750. float _vol = atof(newString[1]);
  751. float _cur = atof(newString[2]);
  752. if (_cur <= 0 || _cur <= 0)
  753. continue;
  754. printf("vol = %f, cur = %f \n", _vol, _cur);
  755. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _vol * 10;
  756. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _cur * 10;
  757. }
  758. else if (strcmp(newString[0], "c") == 0)
  759. {
  760. printf("stop \n\r");
  761. ShmSysConfigAndInfo->SysInfo.StartToChargingFlag = 0x00;
  762. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  763. }
  764. usleep(100000);
  765. }
  766. }
  767. int main(void)
  768. {
  769. if(InitShareMemory() == FAIL)
  770. {
  771. printf ("InitShareMemory = FAIL \n");
  772. if(ShmStatusCodeData != NULL)
  773. {
  774. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  775. }
  776. sleep(5);
  777. return 0;
  778. }
  779. for(;;)
  780. {
  781. char word[128];
  782. char newString[7][10];
  783. int i,j,ctr;
  784. fgets(word, sizeof(word), stdin);
  785. j=0; ctr=0;
  786. strcpy(newString[1], "-1");
  787. strcpy(newString[2], "-1");
  788. for (i = 0; i <= (strlen(word)); i++)
  789. {
  790. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  791. {
  792. newString[ctr][j] = '\0';
  793. ctr++;
  794. j = 0;
  795. }
  796. else
  797. {
  798. newString[ctr][j] = word[i];
  799. j++;
  800. }
  801. }
  802. if(strcmp(newString[0], "state") == 0)
  803. {
  804. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  805. continue;
  806. // 槍狀態
  807. RunStatusProc(newString[1], newString[2]);
  808. }
  809. else if(strcmp(newString[0], "card") == 0)
  810. {
  811. // 刷卡狀態
  812. RunCardProc(newString[1], newString[2]);
  813. }
  814. else if(strcmp(newString[0], "gun") == 0)
  815. {
  816. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  817. continue;
  818. // 插槍狀態
  819. RunGunPlugitProc(newString[1], newString[2]);
  820. }
  821. else if(strcmp(newString[0], "lock") == 0)
  822. {
  823. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  824. continue;
  825. // 插槍狀態
  826. GetGunLockStatusProc(newString[1], newString[2]);
  827. }
  828. else if(strcmp(newString[0], "sysid") == 0)
  829. {
  830. // 測試 sys id
  831. SetSystemIDProc();
  832. }
  833. else if(strcmp(newString[0], "self") == 0)
  834. {
  835. // CSU 自我檢測狀態
  836. RunSelfProc(newString[1]);
  837. }
  838. else if(strcmp(newString[0], "ver") == 0)
  839. {
  840. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  841. continue;
  842. // 取 FW 版號
  843. GetFwVerProc(newString[1]);
  844. }
  845. else if (strcmp(newString[0], "update") == 0)
  846. {
  847. // 更新
  848. FwUpdateFlagProc(newString[1]);
  849. }
  850. else if (strcmp(newString[0], "ac") == 0)
  851. {
  852. // AC contactor 狀態
  853. CheckAcStatus(newString[1]);
  854. }
  855. else if (strcmp(newString[0], "cable") == 0)
  856. {
  857. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  858. continue;
  859. // cable check pass
  860. SetCableChkStatus(newString[1], newString[2]);
  861. }
  862. else if (strcmp(newString[0], "pow") == 0)
  863. {
  864. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  865. continue;
  866. // cable check pass
  867. SetPowerValue(newString[1], newString[2]);
  868. }
  869. else if (strcmp(newString[0], "model") == 0)
  870. {
  871. GetSystemInfo();
  872. }
  873. else if(strcmp(newString[0], "select") == 0)
  874. {
  875. // 取得 / 設定 當前選的槍號
  876. GetGunSelectedNum(newString[1]);
  877. }
  878. else if(strcmp(newString[0], "change") == 0)
  879. {
  880. // 模擬按鈕改變選槍
  881. ChangeGunNum();
  882. }
  883. else if(strcmp(newString[0], "fan") == 0)
  884. {
  885. // 設定風扇速度
  886. SetFanSpeed(newString[1]);
  887. }
  888. else if(strcmp(newString[0], "speed") == 0)
  889. {
  890. // 取得風扇速度
  891. GetFanSpeed();
  892. }
  893. else if(strcmp(newString[0], "debug") == 0)
  894. {
  895. // 設定 debug mode
  896. SetDebugMode(newString[1]);
  897. }
  898. else if (strcmp(newString[0], "gfd") == 0)
  899. {
  900. // 設定盲沖使用 GFD 功能
  901. SetGFDMode(newString[1]);
  902. }
  903. else if(strcmp(newString[0], "temp") == 0)
  904. {
  905. // 取得 PSU 溫度
  906. GetPsuTemp();
  907. }
  908. else if(strcmp(newString[0], "acin") == 0)
  909. {
  910. // 取得三向輸入電壓
  911. GetAcInputVol();
  912. }
  913. else if(strcmp(newString[0], "psu") == 0)
  914. {
  915. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  916. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  917. {
  918. printf ("PSU : Param fail..Please retry again......\n");
  919. continue;
  920. }
  921. // 取得 PSU 資訊
  922. GetPsuInformation(newString[1]);
  923. }
  924. else if (strcmp(newString[0], "cap") == 0)
  925. {
  926. GetConnectorCapInfo(newString[1]);
  927. }
  928. else if(strcmp(newString[0], "error") == 0)
  929. {
  930. CreateOneError(newString[1]);
  931. }
  932. else if(strcmp(newString[0], "strchg") == 0)
  933. {
  934. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  935. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 ||
  936. strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  937. {
  938. printf ("Input cmd fail ------ strchg [vol 150-1000] [cru 2-100]\n");
  939. continue;
  940. }
  941. // 槍狀態
  942. RunUnconditionalChargeIndex1(newString[1], newString[2], newString[3]);
  943. }
  944. else
  945. printf ("%s\n", msg);
  946. usleep(100000);
  947. }
  948. return 0;
  949. }