ReadCmdline.c 24 KB

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