ReadCmdline.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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. #include "Config.h"
  38. #include "Module_EvComm.h"
  39. typedef unsigned char byte;
  40. #define PASS 1
  41. #define FAIL -1
  42. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  43. #define NO_DEFINE 255
  44. #define DEFAULT_AC_INDEX 2
  45. #define TTY_PATH "/dev/tty"
  46. #define STTY_US "stty raw -echo -F "
  47. #define STTY_DEF "stty -raw echo -F "
  48. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  49. struct StatusCodeData *ShmStatusCodeData;
  50. struct PrimaryMcuData *ShmPrimaryMcuData;
  51. struct CHAdeMOData *ShmCHAdeMOData;
  52. struct CcsData *ShmCcsData;
  53. struct GBTData *ShmGBTData;
  54. struct FanModuleData *ShmFanModuleData;
  55. struct RelayModuleData *ShmRelayModuleData;
  56. struct LedModuleData *ShmLedModuleData;
  57. struct PsuData *ShmPsuData;
  58. ChargerInfoData *ShmChargerInfo;
  59. PsuPositionInfoData *ShmPsuPosition;
  60. PsuGroupingInfoData *ShmPsuGrouping;
  61. PsuGroupCollectionData *GroupCollection;
  62. struct ChargingInfoData *_chargingData[CONNECTOR_QUANTITY];
  63. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  64. char *msg = "state : get gun state (index) \n"
  65. "card : scanning card (x) : \n"
  66. "gun : get gun plugit state (index) \n"
  67. "lock : get gun locked state (index) \n"
  68. "self : self test state (x) \n"
  69. "ver : ver of board (407 or index or rb or fan) \n"
  70. "ac : get ac relay state (x) \n";
  71. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  72. {
  73. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  74. {
  75. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  76. {
  77. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  78. return true;
  79. }
  80. }
  81. for (byte index = 0; index < CCS_QUANTITY; index++)
  82. {
  83. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  84. {
  85. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  86. return true;
  87. }
  88. }
  89. for (byte index = 0; index < GB_QUANTITY; index++)
  90. {
  91. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  92. {
  93. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  94. return true;
  95. }
  96. }
  97. if(GENERAL_GUN_QUANTITY > 0 && target < GENERAL_GUN_QUANTITY)
  98. {
  99. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].GeneralChargingData;
  100. return true;
  101. }
  102. return false;
  103. }
  104. bool FindAcChargingInfoData(byte target, struct ChargingInfoData **acChargingData)
  105. {
  106. if (target < AC_QUANTITY)
  107. {
  108. acChargingData[target] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[target];
  109. return true;
  110. }
  111. return false;
  112. }
  113. int InitShareMemory()
  114. {
  115. int result = PASS;
  116. int MeterSMId;
  117. //initial ShmSysConfigAndInfo
  118. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  119. {
  120. result = FAIL;
  121. }
  122. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  123. {
  124. result = FAIL;
  125. }
  126. else
  127. {}
  128. //initial ShmStatusCodeData
  129. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  130. {
  131. result = FAIL;
  132. }
  133. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  134. {
  135. result = FAIL;
  136. }
  137. else
  138. {}
  139. if (CHAdeMO_QUANTITY > 0) {
  140. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),
  141. IPC_CREAT | 0777)) < 0) {
  142. result = FAIL;
  143. } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0))
  144. == (void *) -1) {
  145. result = FAIL;
  146. } else {
  147. }
  148. }
  149. if (CCS_QUANTITY > 0) {
  150. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),
  151. IPC_CREAT | 0777)) < 0) {
  152. result = FAIL;
  153. } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  154. result = FAIL;
  155. } else {
  156. }
  157. }
  158. if (GB_QUANTITY > 0) {
  159. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData),
  160. IPC_CREAT | 0777)) < 0) {
  161. return 0;
  162. } else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  163. return 0;
  164. }
  165. memset(ShmGBTData, 0, sizeof(struct GBTData));
  166. }
  167. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  168. {
  169. result = FAIL;
  170. }
  171. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  172. {
  173. result = FAIL;
  174. }
  175. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  176. {
  177. result = FAIL;
  178. }
  179. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  180. {
  181. result = FAIL;
  182. }
  183. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  184. {
  185. result = FAIL;
  186. }
  187. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  188. {
  189. result = FAIL;
  190. }
  191. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), 0777)) < 0)
  192. {
  193. result = FAIL;
  194. }
  195. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  196. {
  197. result = FAIL;
  198. }
  199. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  200. {
  201. result = FAIL;
  202. }
  203. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  204. {
  205. result = FAIL;
  206. }
  207. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  208. {
  209. result = FAIL;
  210. }
  211. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  212. {
  213. result = FAIL;
  214. }
  215. if(result == PASS)
  216. {
  217. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  218. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  219. GroupCollection = &ShmChargerInfo->PsuGrouping.GroupCollection[0];
  220. }
  221. return result;
  222. }
  223. unsigned long GetTimeoutValue(struct timeval _sour_time)
  224. {
  225. struct timeval _end_time;
  226. gettimeofday(&_end_time, NULL);
  227. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  228. }
  229. void RunStatusProc(char *v1, char *v2)
  230. {
  231. printf("OrderCharging = %d \n", ShmSysConfigAndInfo->SysInfo.OrderCharging);
  232. printf("WaitForPlugit = %d \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  233. if (strcmp(v1, "ac") == 0)
  234. {
  235. if (!FindAcChargingInfoData(0, &ac_chargingInfo[0]))
  236. {
  237. printf("FindChargingInfoData (AC) false \n");
  238. }
  239. printf("AC Status = %d \n", ac_chargingInfo[0]->ConnectorPlugIn);
  240. return;
  241. }
  242. int _index = atoi(v1);
  243. if (_index <= 1)
  244. {
  245. if (!FindChargingInfoData(_index, &_chargingData[0]))
  246. {
  247. printf ("FindChargingInfoData error\n");
  248. return;
  249. }
  250. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  251. {
  252. // get
  253. printf ("index = %x, status = %x (%d)\n", _index, _chargingData[_index]->SystemStatus, _chargingData[_index]->IsAvailable);
  254. printf ("SystemTimeoutFlag = %d, PageIndex = %d\n",
  255. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag, ShmSysConfigAndInfo->SysInfo.PageIndex);
  256. printf("ConnectorAlarmCode = %s \n", _chargingData[_index]->ConnectorAlarmCode);
  257. printf("EvConnAlarmCode = %s \n", _chargingData[_index]->EvConnAlarmCode);
  258. printf("RemotenAlarmCode = %s \n", ShmSysConfigAndInfo->SysInfo.ConnectorInfo[_index].RemotenAlarmCode);
  259. //printf("ConnectorAlarmCode = %s \n", ShmOCPP16Data->StatusNotification[index].ErrorCode);
  260. //printf("ConnectorAlarmCode = %s \n", ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  261. }
  262. else
  263. {
  264. // set
  265. _chargingData[_index]->SystemStatus = atoi(v2);
  266. }
  267. }
  268. else
  269. {
  270. if (!FindAcChargingInfoData(0, &ac_chargingInfo[0]))
  271. {
  272. printf("FindChargingInfoData (AC) false \n");
  273. }
  274. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  275. {
  276. // get
  277. printf ("AC Type, status = %x (%d)\n", ac_chargingInfo[0]->SystemStatus, ac_chargingInfo[0]->IsAvailable);
  278. }
  279. else
  280. {
  281. // set
  282. ac_chargingInfo[0]->SystemStatus = atoi(v2);
  283. }
  284. }
  285. }
  286. void RunCardProc(char *v1, char *v2)
  287. {
  288. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  289. {
  290. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
  291. {
  292. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
  293. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  294. }
  295. else
  296. {
  297. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  298. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  299. }
  300. }
  301. else
  302. {
  303. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  304. memcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, v1, strlen(v1));
  305. ShmSysConfigAndInfo->SysConfig.UserId[strlen(v1)] = '\0';
  306. printf("StartUserId = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  307. }
  308. }
  309. void RunGunPlugitProc(char *v1, char *v2)
  310. {
  311. if (strcmp(v1, "ac") == 0)
  312. {
  313. if (!FindAcChargingInfoData(0, &ac_chargingInfo[0]))
  314. {
  315. printf("FindChargingInfoData (AC) false \n");
  316. }
  317. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  318. {
  319. // get
  320. printf("ConnectorPlugIn = %d \n", ac_chargingInfo[0]->ConnectorPlugIn);
  321. }
  322. else
  323. {
  324. // set
  325. ac_chargingInfo[0]->ConnectorPlugIn = atoi(v2);
  326. }
  327. return;
  328. }
  329. int _index = atoi(v1);
  330. if (!FindChargingInfoData(_index, &_chargingData[0]))
  331. {
  332. printf("FindChargingInfoData error\n");
  333. return;
  334. }
  335. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  336. {
  337. // get
  338. printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
  339. }
  340. else
  341. {
  342. // set
  343. _chargingData[_index]->ConnectorPlugIn = atoi(v2);
  344. }
  345. }
  346. void GetGunLockStatusProc(char *v1, char *v2)
  347. {
  348. int _index = atoi(v1);
  349. if (!FindChargingInfoData(_index, &_chargingData[0]))
  350. {
  351. printf("FindChargingInfoData error\n");
  352. return;
  353. }
  354. if (strcmp(v2, "-1") != 0 && strcmp(v2, "") != 0)
  355. {
  356. _chargingData[_index]->GunLocked = atoi(v2);
  357. }
  358. printf("Gun Locked Status = %d \n", _chargingData[_index]->GunLocked);
  359. }
  360. void SetSystemIDProc()
  361. {
  362. char *systemId = "Alston_Test";
  363. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, systemId, strlen(systemId));
  364. }
  365. void RunSelfProc()
  366. {
  367. printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
  368. }
  369. void GetFwVerProc(char *v1)
  370. {
  371. if (strcmp(v1, "407") == 0)
  372. {
  373. printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
  374. }
  375. else if (strcmp(v1, "0") == 0 || strcmp(v1, "1") == 0 || strcmp(v1, "2") == 0 || strcmp(v1, "3") == 0)
  376. {
  377. int _index = atoi(v1);
  378. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[_index].Enable)
  379. {
  380. int dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[_index].ParentDispensetIndex;
  381. int ParentIndex = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[_index].GeneralChargingData.Index;
  382. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  383. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  384. {
  385. if(ParentIndex == 0)
  386. {
  387. printf("Gun %d FW Version = %s \n", _index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Connector1FwRev);
  388. }
  389. else
  390. {
  391. printf("Gun %d FW Version = %s \n", _index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Connector1FwRev);
  392. }
  393. }
  394. }
  395. else
  396. {
  397. printf("Gun %d is disable\r\n", _index);
  398. }
  399. }
  400. else if (strcmp(v1, "rb") == 0)
  401. {
  402. printf("RB1 Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  403. printf("RB2 Version = %s \n", ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev);
  404. }
  405. else if (strcmp(v1, "fan") == 0)
  406. {
  407. printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  408. }
  409. else if (strcmp(v1, "dc") == 0)
  410. {
  411. printf("DC Main Version = %s \n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  412. }
  413. else if (strcmp(v1, "led") == 0)
  414. {
  415. printf("LED Version = %s \n", ShmSysConfigAndInfo->SysInfo.LedModuleFwRev);
  416. }
  417. }
  418. void CreateOneError(char *v1)
  419. {
  420. int value = atoi(v1);
  421. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = value;
  422. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = value;
  423. }
  424. void GetAuthorizeFlag(char *v1)
  425. {
  426. if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
  427. printf("AuthorizeFlag = %d \n", ShmSysConfigAndInfo->SysInfo.AuthorizeFlag);
  428. else
  429. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = atoi(v1);
  430. }
  431. void GetOrClearId(char *v1)
  432. {
  433. int _index = atoi(v1);
  434. if (!FindChargingInfoData(_index, &_chargingData[0]))
  435. {
  436. printf("FindChargingInfoData error\n");
  437. return;
  438. }
  439. printf("Card Number = %s \n", _chargingData[_index]->StartUserId);
  440. }
  441. void FwUpdateFlagProc()
  442. {
  443. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
  444. }
  445. void CheckAcStatus(char *v1)
  446. {
  447. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  448. {
  449. printf(" AC Contactor Ctrl: %d\r\n", ShmChargerInfo->Control.RelayCtrl.bits.AcContactor);
  450. printf(" AC Off By Psu : %d\r\n", ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu);
  451. printf(" AC Off ByEmergency: %d\r\n", ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency);
  452. printf("AC Contactor Status: %d\r\n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  453. }
  454. else
  455. {
  456. if(system("pidof -s main > /dev/null") != 0)
  457. {
  458. if(strcmp(v1, "0") == 0)
  459. {
  460. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = false;
  461. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = false;
  462. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = false;
  463. printf("Set AC Contactor Off\r\n");
  464. }
  465. else
  466. {
  467. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = true;
  468. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = false;
  469. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = false;
  470. printf("Set AC Contactor On\r\n");
  471. }
  472. }
  473. else
  474. {
  475. printf("main task is running\r\n");
  476. }
  477. }
  478. }
  479. void SetCableChkStatus(char *v1, char *v2)
  480. {
  481. int _index = atoi(v1);
  482. if (!FindChargingInfoData(_index, &_chargingData[0]))
  483. {
  484. printf ("FindChargingInfoData error\n");
  485. return;
  486. }
  487. _chargingData[_index]->GroundFaultStatus = atoi(v2);
  488. }
  489. void SetPowerValue(char *v1, char *v2)
  490. {
  491. int _index = atoi(v1);
  492. float _Current = atof(v2);
  493. // 盲沖的時候才允許使用~
  494. if (_chargingData[_index]->Type != 9)
  495. return;
  496. if (!FindChargingInfoData(_index, &_chargingData[0]))
  497. {
  498. printf ("FindChargingInfoData error\n");
  499. return;
  500. }
  501. _chargingData[_index]->EvBatterytargetCurrent = _Current;
  502. }
  503. void GetSystemInfo()
  504. {
  505. printf ("ModelName = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  506. printf ("SerialNumber = %s \n", ShmSysConfigAndInfo->SysConfig.SerialNumber);
  507. printf ("InternetConn = %d \n", ShmSysConfigAndInfo->SysInfo.InternetConn);
  508. printf ("MaxChargingPower = %d, MaxChargingCurrent = %d \n",
  509. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  510. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  511. }
  512. void ChangeGunNum()
  513. {
  514. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  515. {
  516. ShmSysConfigAndInfo->SysInfo.CurGunSelected += 1;
  517. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  518. }
  519. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  520. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  521. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  522. else
  523. {
  524. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  525. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  526. }
  527. }
  528. void GetGunSelectedNum(char *v1)
  529. {
  530. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  531. {
  532. if (AC_QUANTITY > 0 &&
  533. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  534. {
  535. printf("connector select changed = AC \n");
  536. }
  537. else
  538. printf("connector selected = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  539. }
  540. else
  541. {
  542. int _index = atoi(v1);
  543. if (_index <= 1)
  544. {
  545. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  546. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  547. printf("connector select changed = %d \n", _index);
  548. }
  549. else if (AC_QUANTITY > 0)
  550. {
  551. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  552. printf("connector select changed = AC \n");
  553. }
  554. }
  555. }
  556. void SetFanSpeed(char *v1)
  557. {
  558. int speed = atoi(v1);
  559. ShmFanModuleData->TestFanSpeed = speed;
  560. }
  561. void GetFanSpeed()
  562. {
  563. printf("ShmFanModuleData->PresentFan1Speed = %d \n", ShmFanModuleData->PresentFan1Speed);
  564. printf("ShmFanModuleData->PresentFan2Speed = %d \n", ShmFanModuleData->PresentFan2Speed);
  565. printf("ShmFanModuleData->PresentFan3Speed = %d \n", ShmFanModuleData->PresentFan3Speed);
  566. printf("ShmFanModuleData->PresentFan4Speed = %d \n", ShmFanModuleData->PresentFan4Speed);
  567. }
  568. void SetDebugMode(char *v1)
  569. {
  570. int mode = atoi(v1);
  571. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = mode;
  572. }
  573. void SetGFDMode(char *v1)
  574. {
  575. int mode = atoi(v1);
  576. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = mode;
  577. }
  578. void GetPsuTemp()
  579. {
  580. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  581. {
  582. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  583. {
  584. printf("PSU Temp = %d \n", ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp);
  585. }
  586. }
  587. }
  588. void GetAcInputVol()
  589. {
  590. printf("L1N_L12 = %f, L2N_L23 = %f, L3N_L31 = %f \n",
  591. ShmSysConfigAndInfo->SysInfo.InputVoltageR,
  592. ShmSysConfigAndInfo->SysInfo.InputVoltageS,
  593. ShmSysConfigAndInfo->SysInfo.InputVoltageT);
  594. }
  595. void GetPsuInformation(char *v1, char *v2, char *v3)
  596. {
  597. printf("**********************AC Contact needed*************************\n");
  598. if(strcmp(v1, "count") == 0)
  599. {
  600. for (int i = 0; i < 4; i++)
  601. {
  602. printf("Group Index = %d, Module Count = %d \n", i, ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity);
  603. }
  604. }
  605. else if(strcmp(v1, "ver") == 0)
  606. {
  607. for (int i = 0; i < ShmPsuData->SystemPresentPsuQuantity; i++)
  608. {
  609. printf("Psu Index = %d, PriVersion = %s, SecVersion = %s \n",
  610. i, ShmPsuData->PsuVersion[i].FwPrimaryVersion, ShmPsuData->PsuVersion[i].FwSecondVersion);
  611. }
  612. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  613. {
  614. for (int j = 0; j < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; j++)
  615. {
  616. printf("Group Index = %d, Psu Index = %d, Version = %s \n",
  617. i, j, ShmPsuData->PsuGroup[i].PsuModule[j].FwVersion);
  618. }
  619. }
  620. }
  621. else if(strcmp(v1, "cap") == 0)
  622. {
  623. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  624. {
  625. printf("Group Index = %d, MaxCur = %d, Power = %d \n",
  626. i, ShmPsuData->PsuGroup[i].GroupAvailableCurrent, ShmPsuData->PsuGroup[i].GroupAvailablePower);
  627. }
  628. }
  629. else if(strcmp(v1, "input") == 0)
  630. {
  631. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  632. {
  633. for (byte count = 0; count < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; count++)
  634. {
  635. printf("gp = %d, Index = %d, volR = %d, volS = %d, volT = %d \n",
  636. i, count,
  637. ShmPsuData->PsuGroup[i].PsuModule[count].InputVoltageL1,
  638. ShmPsuData->PsuGroup[i].PsuModule[count].InputVoltageL2,
  639. ShmPsuData->PsuGroup[i].PsuModule[count].InputVoltageL3);
  640. }
  641. }
  642. }
  643. else if (strcmp(v1, "output") == 0)
  644. {
  645. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  646. {
  647. printf("Group Index = %d, OutputV = %d, OutputC = %d \n",
  648. i, ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage, ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent);
  649. }
  650. }
  651. else if (strcmp(v1, "test") == 0)
  652. {
  653. int mode = atoi(v2);
  654. if (mode >= _TEST_MODE && mode <= _TEST_MODE)
  655. {
  656. ShmPsuData->Work_Step = mode;
  657. }
  658. }
  659. else if (strcmp(v1, "out") == 0)
  660. {
  661. float vol = atof(v2);
  662. float cur = atof(v3);
  663. if (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE)
  664. {
  665. if (!FindChargingInfoData(0, &_chargingData[0]))
  666. {
  667. printf ("FindChargingInfoData error\n");
  668. return;
  669. }
  670. _chargingData[0]->EvBatterytargetVoltage = vol;
  671. _chargingData[0]->EvBatterytargetCurrent = cur;
  672. }
  673. }
  674. printf("*************************************************\n");
  675. }
  676. void GetConnectorCapInfo(char *v1)
  677. {
  678. int _GunIndex = atoi(v1);
  679. if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
  680. {
  681. printf ("FindChargingInfoData error\n");
  682. return;
  683. }
  684. printf ("Charger Max Current = %d, Max Power = %d \n",
  685. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10,
  686. ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10);
  687. printf ("Index = %d, MaxPow = %f, MaxVol = %f, MaxCur = %f\n",
  688. _GunIndex,
  689. _chargingData[_GunIndex]->RealMaxPower,
  690. _chargingData[_GunIndex]->RealMaxVoltage,
  691. _chargingData[_GunIndex]->RealMaxCurrent);
  692. }
  693. static void get_char(char *word)
  694. {
  695. fd_set rfds;
  696. struct timeval tv;
  697. FD_ZERO(&rfds);
  698. FD_SET(0, &rfds);
  699. tv.tv_sec = 0;
  700. tv.tv_usec = 10; //wait input timout time
  701. //if input
  702. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  703. {
  704. fgets(word, 128, stdin);
  705. }
  706. }
  707. void RunUnconditionalChargeIndex1(char *v1, char *v2, char *v3)
  708. {
  709. int _GunIndex = atoi(v1);
  710. float _Voltage = atof(v2);
  711. float _Current = atof(v3);
  712. unsigned char PreviousSystemStatus = 0xff;
  713. if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
  714. {
  715. printf ("FindChargingInfoData error\n");
  716. return;
  717. }
  718. printf ("ReqVoltage = %f, ReqCurrent = %f\n", _Voltage, _Current);
  719. if(_Voltage > 1000 || _Voltage < 50)
  720. {
  721. printf ("Input Voltage over range\n");
  722. return;
  723. }
  724. // if(_Current > 100 || _Current < 2){
  725. //
  726. // printf ("Input Current over range\n");
  727. // return;
  728. // }
  729. //測試期間先跳過自我測試 _STEST_COMPLETE = 0xfe
  730. //ShmSysConfigAndInfo->SysInfo.SelfTestSeq = 0xfe;
  731. //kill ev task
  732. system("killall Module_EvComm");
  733. //_Voltage = (_Voltage * 10);
  734. //_Current = (_Current * 10);
  735. //system(STTY_US TTY_PATH);
  736. while(true)
  737. {
  738. //fix gun 1
  739. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _GunIndex;
  740. switch(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  741. {
  742. case S_IDLE:
  743. {
  744. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  745. {
  746. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  747. printf ("[UnconditionalCharge - S_IDLE]\n");
  748. }
  749. ShmSysConfigAndInfo->SysInfo.StartToChargingFlag = 0x01;
  750. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARNING;
  751. }
  752. break;
  753. case S_PREPARNING:
  754. {
  755. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  756. {
  757. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  758. printf ("[UnconditionalCharge - S_PREPARNIN]\n");
  759. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  760. printf ("wait find module\n");
  761. }
  762. //main 會在此階段判斷以下資料跳到下一個 state
  763. //用來得知 AC 是否有搭上 (搭上模組的資訊才會出來) 因為每次 AC_Contactor
  764. //ShmPsuData->SystemPresentPsuQuantity;
  765. //ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity;
  766. //ShmPsuData->PsuGroup[gun_index].GroupAvailablePower;
  767. //_chargingData[gun_index]->AvailableChargingPower;
  768. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  769. //sleep(10);
  770. //清除 main timeout 機制
  771. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  772. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  773. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  774. }
  775. break;
  776. case S_PREPARING_FOR_EV:
  777. {
  778. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  779. {
  780. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  781. printf ("[UnconditionalCharge - S_PREPARING_FOR_EV]\n");
  782. printf ("ReqVoltage = %f, ReqCurrent = %f \n", _Voltage * 10,_Current * 10);
  783. }
  784. //清除 main timeout 機制
  785. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  786. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  787. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  788. //充電電壓電流
  789. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  790. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 500;
  791. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 2;
  792. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  793. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  794. //確定模組己升壓完成
  795. //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage <= (3000+500) &&
  796. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage >= (3000-500) )
  797. {
  798. printf ("Precharge Done = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  799. //EV done
  800. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARING_FOR_EVSE;
  801. }
  802. }
  803. break;
  804. case S_PREPARING_FOR_EVSE:
  805. {
  806. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  807. {
  808. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  809. printf ("[UnconditionalCharge - S_PREPARING_FOR_EVSE]\n");
  810. }
  811. //printf ("tar vol = %d \n", _Voltage);
  812. //printf ("tar cur = %d \n", _Current);
  813. //清除 main timeout 機制
  814. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  815. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  816. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  817. //充電電壓電流
  818. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  819. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 500;
  820. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 2;
  821. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  822. //printf ("tar vol_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage);
  823. // printf ("tar cur_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent);
  824. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  825. //確定模組己升壓完成
  826. //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x01 ||
  827. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03)
  828. //{
  829. //printf ("First Ground Fault State (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  830. printf ("Wait K1K2 = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  831. sleep(5);
  832. //EV done
  833. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_CHARGING;
  834. //}
  835. //else if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus > 0x02)
  836. //{
  837. // printf ("First Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  838. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  839. //}
  840. }
  841. break;
  842. case S_CHARGING:
  843. {
  844. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  845. {
  846. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  847. //充電電壓電流
  848. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  849. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _Voltage;
  850. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _Current;
  851. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  852. printf ("[UnconditionalCharge - S_CHARGING]\n");
  853. }
  854. //ev task do this
  855. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingPower =
  856. ((float)((_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage) * (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingCurrent)) / 1000);
  857. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x02){
  858. printf ("Charging Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  859. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  860. }
  861. }
  862. break;
  863. case S_TERMINATING:
  864. {
  865. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  866. {
  867. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  868. system("/root/Module_EvComm &");
  869. printf ("[UnconditionalCharge - S_TERMINATING]\n");
  870. //無阻塞偵測 keybaord 結束
  871. system(STTY_DEF TTY_PATH);
  872. }
  873. sleep(3);
  874. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_COMPLETE;
  875. return;
  876. }
  877. break;
  878. case S_COMPLETE:
  879. {
  880. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  881. {
  882. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  883. printf ("[UnconditionalCharge - S_COMPLETE]\n");
  884. }
  885. sleep(3);
  886. return;
  887. }
  888. break;
  889. }
  890. char word[128];
  891. char newString[7][10];
  892. int i,j,ctr;
  893. memset(word, 0x00, sizeof(word));
  894. get_char(word);
  895. if (strlen(word) == 0)
  896. {
  897. continue;
  898. }
  899. j=0; ctr=0;
  900. strcpy(newString[1], "-1");
  901. strcpy(newString[2], "-1");
  902. for (i = 0; i <= (strlen(word)); i++)
  903. {
  904. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  905. {
  906. newString[ctr][j] = '\0';
  907. ctr++;
  908. j = 0;
  909. }
  910. else
  911. {
  912. newString[ctr][j] = word[i];
  913. j++;
  914. }
  915. }
  916. if(strcmp(newString[0], "chg") == 0)
  917. {
  918. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  919. continue;
  920. if (strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  921. continue;
  922. float _vol = atof(newString[1]);
  923. float _cur = atof(newString[2]);
  924. if (_cur <= 0 || _cur <= 0)
  925. continue;
  926. printf("vol = %f, cur = %f \n", _vol, _cur);
  927. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _vol;
  928. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _cur;
  929. }
  930. else if (strcmp(newString[0], "c") == 0)
  931. {
  932. printf("stop \n\r");
  933. ShmSysConfigAndInfo->SysInfo.StartToChargingFlag = 0x00;
  934. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  935. }
  936. usleep(100000);
  937. }
  938. }
  939. void SetGroupRoleIdle(byte group)
  940. {
  941. if(group < GENERAL_GUN_QUANTITY)
  942. {
  943. GroupCollection[group].Role = _GROLE_IDLE;
  944. GroupCollection[group].GroupMemberQuantity = 0;
  945. memset(GroupCollection[group].GroupMember, 0x00, ARRAY_SIZE(GroupCollection[group].GroupMember));
  946. GroupCollection[group].TargetGroup = 0;
  947. //printf("\r\n Reset %02X To Idle", group);
  948. }
  949. }
  950. void SetGroupRoleMaster(byte group)
  951. {
  952. if(group < GENERAL_GUN_QUANTITY)
  953. {
  954. GroupCollection[group].Role = _GROLE_MASTER;
  955. //printf("\r\n Set %02X As Master", group);
  956. }
  957. }
  958. void AddGroupCollection(byte group, byte target)
  959. {
  960. if(group < GENERAL_GUN_QUANTITY && target < GENERAL_GUN_QUANTITY)
  961. {
  962. GroupCollection[target].GroupMember[GroupCollection[target].GroupMemberQuantity++] = group;
  963. //printf("\r\n Add %02X To Group %02X (Quantity %d)", group, target, GroupCollection[target].GroupMemberQuantity);
  964. }
  965. }
  966. void RemoveGroupCollection(byte group, byte target)
  967. {
  968. int location = 0, slave = 0;
  969. bool find = false;
  970. if(group < GENERAL_GUN_QUANTITY && target < GENERAL_GUN_QUANTITY)
  971. {
  972. for(int i = 0; i < GroupCollection[target].GroupMemberQuantity; i++)
  973. {
  974. if(group == GroupCollection[target].GroupMember[i])
  975. {
  976. GroupCollection[target].GroupMember[i] = 0;
  977. location = i;
  978. find = true;
  979. break;
  980. }
  981. }
  982. if(find)
  983. {
  984. for(int i = location + 1; i < GroupCollection[target].GroupMemberQuantity; i++)
  985. {
  986. slave = GroupCollection[target].GroupMember[i];
  987. GroupCollection[target].GroupMember[i] = 0;
  988. GroupCollection[target].GroupMember[i - 1] = slave;
  989. }
  990. GroupCollection[target].GroupMemberQuantity--;
  991. //printf("\r\n Remove %02X From Group %02X (Quantity %d)", group, target, GroupCollection[target].GroupMemberQuantity);
  992. }
  993. }
  994. }
  995. void SetGroupRoleSlave(byte group, byte target)
  996. {
  997. if(group < GENERAL_GUN_QUANTITY && target < GENERAL_GUN_QUANTITY)
  998. {
  999. GroupCollection[group].Role = _GROLE_SLAVE;
  1000. GroupCollection[group].TargetGroup = target + 1;
  1001. //printf("\r\n Set %02X As Slave", group);
  1002. }
  1003. }
  1004. void ShowGroupingInfo(void)
  1005. {
  1006. byte target = 0;
  1007. for(int i = 0; i < 4; i++)
  1008. {
  1009. target = ShmPsuGrouping->Layout[i];
  1010. printf("\r\nGun[%d] Role: %d, Quantity: %d", target + 1, GroupCollection[target].Role, GroupCollection[target].GroupMemberQuantity);
  1011. if(GroupCollection[target].Role == 1)
  1012. {
  1013. printf(",");
  1014. printf(" %02X", target);
  1015. for(int j = 0; j < GroupCollection[target].GroupMemberQuantity; j++)
  1016. {
  1017. printf(" %02X", GroupCollection[target].GroupMember[j]);
  1018. }
  1019. }
  1020. if(GroupCollection[target].Role == 2)
  1021. {
  1022. printf(", TargetGroup: %02X", GroupCollection[target].TargetGroup - 1);
  1023. }
  1024. }
  1025. printf("\r\n\r\n");
  1026. }
  1027. void PsuGroupSwitchToIdle(byte group)
  1028. {
  1029. int master = 0, quantity = 0, location = 0, total = 0;
  1030. if(GroupCollection[group].Role != _GROLE_SLAVE)
  1031. {
  1032. return;
  1033. }
  1034. master = GroupCollection[group].TargetGroup - 1;
  1035. quantity = GroupCollection[master].GroupMemberQuantity;
  1036. //printf("\r\n Search %02X From Group %02X", group, master);
  1037. for(int i = 0; i < quantity; i++)
  1038. {
  1039. if(total == 0)
  1040. {
  1041. if(group == GroupCollection[master].GroupMember[i])
  1042. {
  1043. location = i;
  1044. total++;
  1045. //printf("\r\n Find Switch Idle Start Location %d", location);
  1046. }
  1047. }
  1048. else
  1049. {
  1050. // find other group in the same direction
  1051. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[location]] < ShmPsuGrouping->Location[master])
  1052. {
  1053. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[i]] < ShmPsuGrouping->Location[master])
  1054. {
  1055. total++;
  1056. }
  1057. }
  1058. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[location]] > ShmPsuGrouping->Location[master])
  1059. {
  1060. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[i]] > ShmPsuGrouping->Location[master])
  1061. {
  1062. total++;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. if(total > 0)
  1068. {
  1069. unsigned char collection[GENERAL_GUN_QUANTITY];
  1070. //printf("\r\n Total Switch Idle %d:", total);
  1071. memcpy(collection, GroupCollection[master].GroupMember, ARRAY_SIZE(GroupCollection[master].GroupMember));
  1072. for(int i = 0; i < total; i++)
  1073. {
  1074. //printf(" %02X", collection[i + location]);
  1075. }
  1076. for(int i = 0; i < total; i++)
  1077. {
  1078. RemoveGroupCollection(collection[i + location], master);
  1079. SetGroupRoleIdle(collection[i + location]);
  1080. }
  1081. }
  1082. }
  1083. int GetPsuGroupAvailable(byte group)
  1084. {
  1085. int slave = 0, location = 0, available = 0;
  1086. // search from left
  1087. location = GroupCollection[group].Location - 1;
  1088. for(int i = location; i >= 0; i--)
  1089. {
  1090. slave = ShmPsuGrouping->Layout[i];
  1091. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1092. {
  1093. available++;
  1094. }
  1095. else
  1096. {
  1097. break;
  1098. }
  1099. }
  1100. // search from right
  1101. location = GroupCollection[group].Location + 1;
  1102. for(int i = location; i < 4; i++)
  1103. {
  1104. slave = ShmPsuGrouping->Layout[i];
  1105. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1106. {
  1107. available++;
  1108. }
  1109. else
  1110. {
  1111. break;
  1112. }
  1113. }
  1114. return available;
  1115. }
  1116. void PsuGroupShareCheck(byte group)
  1117. {
  1118. int slave = 0, target = 0, location = 0, share = 0, total = 0;
  1119. total = GetPsuGroupAvailable(group) + 1;
  1120. //printf("\r\n Group %02X Available Quantity %d", group, total);
  1121. // search from left
  1122. location = GroupCollection[group].Location - 1;
  1123. for(int i = location; i >= 0; i--)
  1124. {
  1125. slave = ShmPsuGrouping->Layout[i];
  1126. if(GroupCollection[slave].Role == _GROLE_SLAVE)
  1127. {
  1128. target = GroupCollection[slave].TargetGroup - 1;
  1129. if(GroupCollection[target].GroupMemberQuantity > total)
  1130. {
  1131. share = (GroupCollection[target].GroupMemberQuantity + 1 - total) / 2;
  1132. }
  1133. //printf("\r\n Find Group %02X Have %d Resource Can Shared %d From Left", target, GroupCollection[target].GroupMemberQuantity, share);
  1134. break;
  1135. }
  1136. else
  1137. {
  1138. break;
  1139. }
  1140. }
  1141. if(share == 0)
  1142. {
  1143. // search from right
  1144. location = GroupCollection[group].Location + 1;
  1145. for(int i = location; i < 4; i++)
  1146. {
  1147. slave = ShmPsuGrouping->Layout[i];
  1148. if(GroupCollection[slave].Role == _GROLE_SLAVE)
  1149. {
  1150. target = GroupCollection[slave].TargetGroup - 1;
  1151. if(GroupCollection[target].GroupMemberQuantity > total)
  1152. {
  1153. share = (GroupCollection[target].GroupMemberQuantity + 1 - total) / 2;
  1154. }
  1155. //printf("\r\n Find Group %02X Have %d Resource Can Shared %d From Left", target, GroupCollection[target].GroupMemberQuantity, share);
  1156. break;
  1157. }
  1158. else
  1159. {
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. if(share > 0)
  1165. {
  1166. //printf("\r\n Grab %02X", slave);
  1167. PsuGroupSwitchToIdle(slave);
  1168. }
  1169. else
  1170. {
  1171. //printf("\r\n No Need To Grab");
  1172. }
  1173. }
  1174. void PsuGroupStartCharging(byte group)
  1175. {
  1176. int slave = 0, location = 0;
  1177. if(GroupCollection[group].Role != _GROLE_IDLE)
  1178. {
  1179. return;
  1180. }
  1181. PsuGroupShareCheck(group);
  1182. // search from left
  1183. location = GroupCollection[group].Location - 1;
  1184. for(int i = location; i >= 0; i--)
  1185. {
  1186. slave = ShmPsuGrouping->Layout[i];
  1187. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1188. {
  1189. //printf("\r\n Find %02X From Left", slave);
  1190. SetGroupRoleSlave(slave, group);
  1191. AddGroupCollection(slave, group);
  1192. }
  1193. else
  1194. {
  1195. break;
  1196. }
  1197. }
  1198. // search from right
  1199. location = GroupCollection[group].Location + 1;
  1200. for(int i = location; i < 4; i++)
  1201. {
  1202. slave = ShmPsuGrouping->Layout[i];
  1203. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1204. {
  1205. //printf("\r\n Find %02X From Right", slave);
  1206. SetGroupRoleSlave(slave, group);
  1207. AddGroupCollection(slave, group);
  1208. }
  1209. else
  1210. {
  1211. break;
  1212. }
  1213. }
  1214. SetGroupRoleMaster(group);
  1215. }
  1216. void PsuGroupStopCharging(byte group)
  1217. {
  1218. if(GroupCollection[group].Role != _GROLE_MASTER)
  1219. {
  1220. return;
  1221. }
  1222. for(int i = 0; i < GroupCollection[group].GroupMemberQuantity; i++)
  1223. {
  1224. SetGroupRoleIdle(GroupCollection[group].GroupMember[i]);
  1225. }
  1226. SetGroupRoleIdle(group);
  1227. }
  1228. void RunPsuGrouping(char *v1, char *v2)
  1229. {
  1230. int group = 0, charging = 0;
  1231. group = atoi(v1);
  1232. charging = atoi(v2);
  1233. group -= 1;
  1234. if(group >= 0 && group < 4)
  1235. {
  1236. if(charging > 0)
  1237. {
  1238. if(GroupCollection[group].Role == _GROLE_IDLE || GroupCollection[group].Role == _GROLE_SLAVE)
  1239. {
  1240. if(GroupCollection[group].Role == _GROLE_SLAVE)
  1241. {
  1242. PsuGroupSwitchToIdle(group);
  1243. }
  1244. PsuGroupStartCharging(group);
  1245. }
  1246. }
  1247. if(charging == 0)
  1248. {
  1249. if(GroupCollection[group].Role == _GROLE_MASTER)
  1250. {
  1251. PsuGroupStopCharging(group);
  1252. }
  1253. if(GroupCollection[group].Role == _GROLE_SLAVE)
  1254. {
  1255. PsuGroupSwitchToIdle(group);
  1256. }
  1257. }
  1258. ShowGroupingInfo();
  1259. }
  1260. }
  1261. int main(void)
  1262. {
  1263. if(InitShareMemory() == FAIL)
  1264. {
  1265. printf ("InitShareMemory = FAIL \n");
  1266. if(ShmStatusCodeData != NULL)
  1267. {
  1268. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1269. }
  1270. sleep(5);
  1271. return 0;
  1272. }
  1273. memset(&GroupCollection[0], 0x00, sizeof(PsuGroupCollectionData));
  1274. memset(&GroupCollection[1], 0x00, sizeof(PsuGroupCollectionData));
  1275. memset(&GroupCollection[2], 0x00, sizeof(PsuGroupCollectionData));
  1276. memset(&GroupCollection[3], 0x00, sizeof(PsuGroupCollectionData));
  1277. GroupCollection[0].Index = 0;
  1278. GroupCollection[1].Index = 1;
  1279. GroupCollection[2].Index = 2;
  1280. GroupCollection[3].Index = 3;
  1281. GroupCollection[0].Location = 0;
  1282. GroupCollection[1].Location = 3;
  1283. GroupCollection[2].Location = 1;
  1284. GroupCollection[3].Location = 2;
  1285. for(;;)
  1286. {
  1287. char word[128];
  1288. char newString[7][10];
  1289. int i,j,ctr;
  1290. fgets(word, sizeof(word), stdin);
  1291. j=0; ctr=0;
  1292. strcpy(newString[1], "-1");
  1293. strcpy(newString[2], "-1");
  1294. for (i = 0; i <= (strlen(word)); i++)
  1295. {
  1296. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  1297. {
  1298. newString[ctr][j] = '\0';
  1299. ctr++;
  1300. j = 0;
  1301. }
  1302. else
  1303. {
  1304. newString[ctr][j] = word[i];
  1305. j++;
  1306. }
  1307. }
  1308. if(strcmp(newString[0], "state") == 0)
  1309. {
  1310. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1311. continue;
  1312. // 槍狀態
  1313. RunStatusProc(newString[1], newString[2]);
  1314. }
  1315. else if(strcmp(newString[0], "card") == 0)
  1316. {
  1317. // 刷卡狀態
  1318. RunCardProc(newString[1], newString[2]);
  1319. }
  1320. else if(strcmp(newString[0], "gun") == 0)
  1321. {
  1322. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1323. continue;
  1324. // 插槍狀態
  1325. RunGunPlugitProc(newString[1], newString[2]);
  1326. }
  1327. else if(strcmp(newString[0], "lock") == 0)
  1328. {
  1329. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1330. continue;
  1331. // 插槍狀態
  1332. GetGunLockStatusProc(newString[1], newString[2]);
  1333. }
  1334. else if(strcmp(newString[0], "sysid") == 0)
  1335. {
  1336. // 測試 sys id
  1337. SetSystemIDProc();
  1338. }
  1339. else if(strcmp(newString[0], "self") == 0)
  1340. {
  1341. // CSU 自我檢測狀態
  1342. RunSelfProc(newString[1]);
  1343. }
  1344. else if(strcmp(newString[0], "ver") == 0)
  1345. {
  1346. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1347. continue;
  1348. // 取 FW 版號
  1349. GetFwVerProc(newString[1]);
  1350. }
  1351. else if (strcmp(newString[0], "update") == 0)
  1352. {
  1353. // 更新
  1354. FwUpdateFlagProc(newString[1]);
  1355. }
  1356. else if (strcmp(newString[0], "ac") == 0)
  1357. {
  1358. // AC contactor 狀態
  1359. CheckAcStatus(newString[1]);
  1360. }
  1361. else if (strcmp(newString[0], "cable") == 0)
  1362. {
  1363. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1364. continue;
  1365. // cable check pass
  1366. SetCableChkStatus(newString[1], newString[2]);
  1367. }
  1368. else if (strcmp(newString[0], "pow") == 0)
  1369. {
  1370. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1371. continue;
  1372. // cable check pass
  1373. SetPowerValue(newString[1], newString[2]);
  1374. }
  1375. else if (strcmp(newString[0], "model") == 0)
  1376. {
  1377. GetSystemInfo();
  1378. }
  1379. else if(strcmp(newString[0], "select") == 0)
  1380. {
  1381. // 取得 / 設定 當前選的槍號
  1382. GetGunSelectedNum(newString[1]);
  1383. }
  1384. else if(strcmp(newString[0], "change") == 0)
  1385. {
  1386. // 模擬按鈕改變選槍
  1387. ChangeGunNum();
  1388. }
  1389. else if(strcmp(newString[0], "fan") == 0)
  1390. {
  1391. // 設定風扇速度
  1392. SetFanSpeed(newString[1]);
  1393. }
  1394. else if(strcmp(newString[0], "speed") == 0)
  1395. {
  1396. // 取得風扇速度
  1397. GetFanSpeed();
  1398. }
  1399. else if(strcmp(newString[0], "debug") == 0)
  1400. {
  1401. // 設定 debug mode
  1402. SetDebugMode(newString[1]);
  1403. }
  1404. else if (strcmp(newString[0], "gfd") == 0)
  1405. {
  1406. // 設定盲沖使用 GFD 功能
  1407. SetGFDMode(newString[1]);
  1408. }
  1409. else if(strcmp(newString[0], "temp") == 0)
  1410. {
  1411. // 取得 PSU 溫度
  1412. GetPsuTemp();
  1413. }
  1414. else if(strcmp(newString[0], "acin") == 0)
  1415. {
  1416. // 取得三向輸入電壓
  1417. GetAcInputVol();
  1418. }
  1419. else if(strcmp(newString[0], "psu") == 0)
  1420. {
  1421. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  1422. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1423. {
  1424. printf ("PSU : Param fail..Please retry again......\n");
  1425. continue;
  1426. }
  1427. // 取得 PSU 資訊
  1428. GetPsuInformation(newString[1], newString[2], newString[3]);
  1429. }
  1430. else if (strcmp(newString[0], "cap") == 0)
  1431. {
  1432. GetConnectorCapInfo(newString[1]);
  1433. }
  1434. else if(strcmp(newString[0], "error") == 0)
  1435. {
  1436. CreateOneError(newString[1]);
  1437. }
  1438. else if (strcmp(newString[0], "auth") == 0)
  1439. {
  1440. GetAuthorizeFlag(newString[1]);
  1441. }
  1442. else if (strcmp(newString[0], "id") == 0)
  1443. {
  1444. GetOrClearId(newString[1]);
  1445. }
  1446. else if(strcmp(newString[0], "strchg") == 0)
  1447. {
  1448. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  1449. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 ||
  1450. strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  1451. {
  1452. printf ("Input cmd fail ------ strchg [vol 150-1000] [cru 2-100]\n");
  1453. continue;
  1454. }
  1455. // 槍狀態
  1456. RunUnconditionalChargeIndex1(newString[1], newString[2], newString[3]);
  1457. }
  1458. else if(strcmp(newString[0], "group") == 0)
  1459. {
  1460. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "-1") == 0)
  1461. {
  1462. ShowGroupingInfo();
  1463. continue;
  1464. }
  1465. RunPsuGrouping(newString[1], newString[2]);
  1466. }
  1467. else
  1468. printf ("%s\n", msg);
  1469. usleep(100000);
  1470. }
  1471. return 0;
  1472. }