ReadCmdline.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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 ForceOff Status: %d\r\n", ShmChargerInfo->Control.RelayCtrl.bits.AcContactorForceOff);
  451. printf("AC Contactor Status: %d\r\n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  452. }
  453. else
  454. {
  455. if(system("pidof -s main > /dev/null") != 0)
  456. {
  457. if(strcmp(v1, "0") == 0)
  458. {
  459. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = false;
  460. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorForceOff = false;
  461. printf("Set AC Contactor Off\r\n");
  462. }
  463. else
  464. {
  465. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = true;
  466. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorForceOff = false;
  467. printf("Set AC Contactor On\r\n");
  468. }
  469. }
  470. else
  471. {
  472. printf("main task is running\r\n");
  473. }
  474. }
  475. }
  476. void SetCableChkStatus(char *v1, char *v2)
  477. {
  478. int _index = atoi(v1);
  479. if (!FindChargingInfoData(_index, &_chargingData[0]))
  480. {
  481. printf ("FindChargingInfoData error\n");
  482. return;
  483. }
  484. _chargingData[_index]->GroundFaultStatus = atoi(v2);
  485. }
  486. void SetPowerValue(char *v1, char *v2)
  487. {
  488. int _index = atoi(v1);
  489. float _Current = atof(v2);
  490. // 盲沖的時候才允許使用~
  491. if (_chargingData[_index]->Type != 9)
  492. return;
  493. if (!FindChargingInfoData(_index, &_chargingData[0]))
  494. {
  495. printf ("FindChargingInfoData error\n");
  496. return;
  497. }
  498. _chargingData[_index]->EvBatterytargetCurrent = _Current;
  499. }
  500. void GetSystemInfo()
  501. {
  502. printf ("ModelName = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  503. printf ("SerialNumber = %s \n", ShmSysConfigAndInfo->SysConfig.SerialNumber);
  504. printf ("InternetConn = %d \n", ShmSysConfigAndInfo->SysInfo.InternetConn);
  505. printf ("MaxChargingPower = %d, MaxChargingCurrent = %d \n",
  506. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  507. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  508. }
  509. void ChangeGunNum()
  510. {
  511. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  512. {
  513. ShmSysConfigAndInfo->SysInfo.CurGunSelected += 1;
  514. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  515. }
  516. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  517. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  518. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  519. else
  520. {
  521. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  522. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  523. }
  524. }
  525. void GetGunSelectedNum(char *v1)
  526. {
  527. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  528. {
  529. if (AC_QUANTITY > 0 &&
  530. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  531. {
  532. printf("connector select changed = AC \n");
  533. }
  534. else
  535. printf("connector selected = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  536. }
  537. else
  538. {
  539. int _index = atoi(v1);
  540. if (_index <= 1)
  541. {
  542. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  543. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  544. printf("connector select changed = %d \n", _index);
  545. }
  546. else if (AC_QUANTITY > 0)
  547. {
  548. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  549. printf("connector select changed = AC \n");
  550. }
  551. }
  552. }
  553. void SetFanSpeed(char *v1)
  554. {
  555. int speed = atoi(v1);
  556. ShmFanModuleData->TestFanSpeed = speed;
  557. }
  558. void GetFanSpeed()
  559. {
  560. printf("ShmFanModuleData->PresentFan1Speed = %d \n", ShmFanModuleData->PresentFan1Speed);
  561. printf("ShmFanModuleData->PresentFan2Speed = %d \n", ShmFanModuleData->PresentFan2Speed);
  562. printf("ShmFanModuleData->PresentFan3Speed = %d \n", ShmFanModuleData->PresentFan3Speed);
  563. printf("ShmFanModuleData->PresentFan4Speed = %d \n", ShmFanModuleData->PresentFan4Speed);
  564. }
  565. void SetDebugMode(char *v1)
  566. {
  567. int mode = atoi(v1);
  568. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = mode;
  569. }
  570. void SetGFDMode(char *v1)
  571. {
  572. int mode = atoi(v1);
  573. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = mode;
  574. }
  575. void GetPsuTemp()
  576. {
  577. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  578. {
  579. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  580. {
  581. printf("PSU Temp = %d \n", ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp);
  582. }
  583. }
  584. }
  585. void GetAcInputVol()
  586. {
  587. printf("L1N_L12 = %f, L2N_L23 = %f, L3N_L31 = %f \n",
  588. ShmSysConfigAndInfo->SysInfo.InputVoltageR,
  589. ShmSysConfigAndInfo->SysInfo.InputVoltageS,
  590. ShmSysConfigAndInfo->SysInfo.InputVoltageT);
  591. }
  592. void GetPsuInformation(char *v1, char *v2, char *v3)
  593. {
  594. printf("**********************AC Contact needed*************************\n");
  595. if(strcmp(v1, "count") == 0)
  596. {
  597. for (int i = 0; i < 4; i++)
  598. {
  599. printf("Group Index = %d, Module Count = %d \n", i, ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity);
  600. }
  601. }
  602. else if(strcmp(v1, "ver") == 0)
  603. {
  604. for (int i = 0; i < ShmPsuData->SystemPresentPsuQuantity; i++)
  605. {
  606. printf("Psu Index = %d, PriVersion = %s, SecVersion = %s \n",
  607. i, ShmPsuData->PsuVersion[i].FwPrimaryVersion, ShmPsuData->PsuVersion[i].FwSecondVersion);
  608. }
  609. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  610. {
  611. for (int j = 0; j < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; j++)
  612. {
  613. printf("Group Index = %d, Psu Index = %d, Version = %s \n",
  614. i, j, ShmPsuData->PsuGroup[i].PsuModule[j].FwVersion);
  615. }
  616. }
  617. }
  618. else if(strcmp(v1, "cap") == 0)
  619. {
  620. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  621. {
  622. printf("Group Index = %d, MaxCur = %d, Power = %d \n",
  623. i, ShmPsuData->PsuGroup[i].GroupAvailableCurrent, ShmPsuData->PsuGroup[i].GroupAvailablePower);
  624. }
  625. }
  626. else if(strcmp(v1, "input") == 0)
  627. {
  628. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  629. {
  630. for (byte count = 0; count < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; count++)
  631. {
  632. printf("gp = %d, Index = %d, volR = %d, volS = %d, volT = %d \n",
  633. i, count,
  634. ShmPsuData->PsuGroup[i].PsuModule[count].InputVoltageL1,
  635. ShmPsuData->PsuGroup[i].PsuModule[count].InputVoltageL2,
  636. ShmPsuData->PsuGroup[i].PsuModule[count].InputVoltageL3);
  637. }
  638. }
  639. }
  640. else if (strcmp(v1, "output") == 0)
  641. {
  642. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  643. {
  644. printf("Group Index = %d, OutputV = %d, OutputC = %d \n",
  645. i, ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage, ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent);
  646. }
  647. }
  648. else if (strcmp(v1, "test") == 0)
  649. {
  650. int mode = atoi(v2);
  651. if (mode >= _TEST_MODE && mode <= _TEST_MODE)
  652. {
  653. ShmPsuData->Work_Step = mode;
  654. }
  655. }
  656. else if (strcmp(v1, "out") == 0)
  657. {
  658. float vol = atof(v2);
  659. float cur = atof(v3);
  660. if (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE)
  661. {
  662. if (!FindChargingInfoData(0, &_chargingData[0]))
  663. {
  664. printf ("FindChargingInfoData error\n");
  665. return;
  666. }
  667. _chargingData[0]->EvBatterytargetVoltage = vol;
  668. _chargingData[0]->EvBatterytargetCurrent = cur;
  669. }
  670. }
  671. printf("*************************************************\n");
  672. }
  673. void GetConnectorCapInfo(char *v1)
  674. {
  675. int _GunIndex = atoi(v1);
  676. if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
  677. {
  678. printf ("FindChargingInfoData error\n");
  679. return;
  680. }
  681. printf ("Charger Max Current = %d, Max Power = %d \n",
  682. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10,
  683. ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10);
  684. printf ("Index = %d, MaxPow = %f, MaxVol = %f, MaxCur = %f\n",
  685. _GunIndex,
  686. _chargingData[_GunIndex]->RealMaxPower,
  687. _chargingData[_GunIndex]->RealMaxVoltage,
  688. _chargingData[_GunIndex]->RealMaxCurrent);
  689. }
  690. static void get_char(char *word)
  691. {
  692. fd_set rfds;
  693. struct timeval tv;
  694. FD_ZERO(&rfds);
  695. FD_SET(0, &rfds);
  696. tv.tv_sec = 0;
  697. tv.tv_usec = 10; //wait input timout time
  698. //if input
  699. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  700. {
  701. fgets(word, 128, stdin);
  702. }
  703. }
  704. void RunUnconditionalChargeIndex1(char *v1, char *v2, char *v3)
  705. {
  706. int _GunIndex = atoi(v1);
  707. float _Voltage = atof(v2);
  708. float _Current = atof(v3);
  709. unsigned char PreviousSystemStatus = 0xff;
  710. if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
  711. {
  712. printf ("FindChargingInfoData error\n");
  713. return;
  714. }
  715. printf ("ReqVoltage = %f, ReqCurrent = %f\n", _Voltage, _Current);
  716. if(_Voltage > 1000 || _Voltage < 50)
  717. {
  718. printf ("Input Voltage over range\n");
  719. return;
  720. }
  721. // if(_Current > 100 || _Current < 2){
  722. //
  723. // printf ("Input Current over range\n");
  724. // return;
  725. // }
  726. //測試期間先跳過自我測試 _STEST_COMPLETE = 0xfe
  727. //ShmSysConfigAndInfo->SysInfo.SelfTestSeq = 0xfe;
  728. //kill ev task
  729. system("killall Module_EvComm");
  730. //_Voltage = (_Voltage * 10);
  731. //_Current = (_Current * 10);
  732. //system(STTY_US TTY_PATH);
  733. while(true)
  734. {
  735. //fix gun 1
  736. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _GunIndex;
  737. switch(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  738. {
  739. case S_IDLE:
  740. {
  741. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  742. {
  743. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  744. printf ("[UnconditionalCharge - S_IDLE]\n");
  745. }
  746. ShmSysConfigAndInfo->SysInfo.StartToChargingFlag = 0x01;
  747. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARNING;
  748. }
  749. break;
  750. case S_PREPARNING:
  751. {
  752. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  753. {
  754. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  755. printf ("[UnconditionalCharge - S_PREPARNIN]\n");
  756. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  757. printf ("wait find module\n");
  758. }
  759. //main 會在此階段判斷以下資料跳到下一個 state
  760. //用來得知 AC 是否有搭上 (搭上模組的資訊才會出來) 因為每次 AC_Contactor
  761. //ShmPsuData->SystemPresentPsuQuantity;
  762. //ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity;
  763. //ShmPsuData->PsuGroup[gun_index].GroupAvailablePower;
  764. //_chargingData[gun_index]->AvailableChargingPower;
  765. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  766. //sleep(10);
  767. //清除 main timeout 機制
  768. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  769. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  770. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  771. }
  772. break;
  773. case S_PREPARING_FOR_EV:
  774. {
  775. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  776. {
  777. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  778. printf ("[UnconditionalCharge - S_PREPARING_FOR_EV]\n");
  779. printf ("ReqVoltage = %f, ReqCurrent = %f \n", _Voltage * 10,_Current * 10);
  780. }
  781. //清除 main timeout 機制
  782. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  783. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  784. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  785. //充電電壓電流
  786. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  787. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 500;
  788. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 2;
  789. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  790. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  791. //確定模組己升壓完成
  792. //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage <= (3000+500) &&
  793. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage >= (3000-500) )
  794. {
  795. printf ("Precharge Done = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  796. //EV done
  797. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARING_FOR_EVSE;
  798. }
  799. }
  800. break;
  801. case S_PREPARING_FOR_EVSE:
  802. {
  803. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  804. {
  805. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  806. printf ("[UnconditionalCharge - S_PREPARING_FOR_EVSE]\n");
  807. }
  808. //printf ("tar vol = %d \n", _Voltage);
  809. //printf ("tar cur = %d \n", _Current);
  810. //清除 main timeout 機制
  811. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  812. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  813. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  814. //充電電壓電流
  815. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  816. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 500;
  817. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 2;
  818. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  819. //printf ("tar vol_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage);
  820. // printf ("tar cur_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent);
  821. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  822. //確定模組己升壓完成
  823. //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x01 ||
  824. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03)
  825. //{
  826. //printf ("First Ground Fault State (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  827. printf ("Wait K1K2 = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  828. sleep(5);
  829. //EV done
  830. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_CHARGING;
  831. //}
  832. //else if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus > 0x02)
  833. //{
  834. // printf ("First Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  835. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  836. //}
  837. }
  838. break;
  839. case S_CHARGING:
  840. {
  841. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  842. {
  843. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  844. //充電電壓電流
  845. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  846. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _Voltage;
  847. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _Current;
  848. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  849. printf ("[UnconditionalCharge - S_CHARGING]\n");
  850. }
  851. //ev task do this
  852. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingPower =
  853. ((float)((_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage) * (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingCurrent)) / 1000);
  854. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x02){
  855. printf ("Charging Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  856. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  857. }
  858. }
  859. break;
  860. case S_TERMINATING:
  861. {
  862. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  863. {
  864. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  865. system("/root/Module_EvComm &");
  866. printf ("[UnconditionalCharge - S_TERMINATING]\n");
  867. //無阻塞偵測 keybaord 結束
  868. system(STTY_DEF TTY_PATH);
  869. }
  870. sleep(3);
  871. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_COMPLETE;
  872. return;
  873. }
  874. break;
  875. case S_COMPLETE:
  876. {
  877. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  878. {
  879. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  880. printf ("[UnconditionalCharge - S_COMPLETE]\n");
  881. }
  882. sleep(3);
  883. return;
  884. }
  885. break;
  886. }
  887. char word[128];
  888. char newString[7][10];
  889. int i,j,ctr;
  890. memset(word, 0x00, sizeof(word));
  891. get_char(word);
  892. if (strlen(word) == 0)
  893. {
  894. continue;
  895. }
  896. j=0; ctr=0;
  897. strcpy(newString[1], "-1");
  898. strcpy(newString[2], "-1");
  899. for (i = 0; i <= (strlen(word)); i++)
  900. {
  901. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  902. {
  903. newString[ctr][j] = '\0';
  904. ctr++;
  905. j = 0;
  906. }
  907. else
  908. {
  909. newString[ctr][j] = word[i];
  910. j++;
  911. }
  912. }
  913. if(strcmp(newString[0], "chg") == 0)
  914. {
  915. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  916. continue;
  917. if (strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  918. continue;
  919. float _vol = atof(newString[1]);
  920. float _cur = atof(newString[2]);
  921. if (_cur <= 0 || _cur <= 0)
  922. continue;
  923. printf("vol = %f, cur = %f \n", _vol, _cur);
  924. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _vol;
  925. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _cur;
  926. }
  927. else if (strcmp(newString[0], "c") == 0)
  928. {
  929. printf("stop \n\r");
  930. ShmSysConfigAndInfo->SysInfo.StartToChargingFlag = 0x00;
  931. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  932. }
  933. usleep(100000);
  934. }
  935. }
  936. void SetGroupRoleIdle(byte group)
  937. {
  938. if(group < GENERAL_GUN_QUANTITY)
  939. {
  940. GroupCollection[group].Role = _GROLE_IDLE;
  941. GroupCollection[group].GroupMemberQuantity = 0;
  942. memset(GroupCollection[group].GroupMember, 0x00, ARRAY_SIZE(GroupCollection[group].GroupMember));
  943. GroupCollection[group].TargetGroup = 0;
  944. //printf("\r\n Reset %02X To Idle", group);
  945. }
  946. }
  947. void SetGroupRoleMaster(byte group)
  948. {
  949. if(group < GENERAL_GUN_QUANTITY)
  950. {
  951. GroupCollection[group].Role = _GROLE_MASTER;
  952. //printf("\r\n Set %02X As Master", group);
  953. }
  954. }
  955. void AddGroupCollection(byte group, byte target)
  956. {
  957. if(group < GENERAL_GUN_QUANTITY && target < GENERAL_GUN_QUANTITY)
  958. {
  959. GroupCollection[target].GroupMember[GroupCollection[target].GroupMemberQuantity++] = group;
  960. //printf("\r\n Add %02X To Group %02X (Quantity %d)", group, target, GroupCollection[target].GroupMemberQuantity);
  961. }
  962. }
  963. void RemoveGroupCollection(byte group, byte target)
  964. {
  965. int location = 0, slave = 0;
  966. bool find = false;
  967. if(group < GENERAL_GUN_QUANTITY && target < GENERAL_GUN_QUANTITY)
  968. {
  969. for(int i = 0; i < GroupCollection[target].GroupMemberQuantity; i++)
  970. {
  971. if(group == GroupCollection[target].GroupMember[i])
  972. {
  973. GroupCollection[target].GroupMember[i] = 0;
  974. location = i;
  975. find = true;
  976. break;
  977. }
  978. }
  979. if(find)
  980. {
  981. for(int i = location + 1; i < GroupCollection[target].GroupMemberQuantity; i++)
  982. {
  983. slave = GroupCollection[target].GroupMember[i];
  984. GroupCollection[target].GroupMember[i] = 0;
  985. GroupCollection[target].GroupMember[i - 1] = slave;
  986. }
  987. GroupCollection[target].GroupMemberQuantity--;
  988. //printf("\r\n Remove %02X From Group %02X (Quantity %d)", group, target, GroupCollection[target].GroupMemberQuantity);
  989. }
  990. }
  991. }
  992. void SetGroupRoleSlave(byte group, byte target)
  993. {
  994. if(group < GENERAL_GUN_QUANTITY && target < GENERAL_GUN_QUANTITY)
  995. {
  996. GroupCollection[group].Role = _GROLE_SLAVE;
  997. GroupCollection[group].TargetGroup = target + 1;
  998. //printf("\r\n Set %02X As Slave", group);
  999. }
  1000. }
  1001. void ShowGroupingInfo(void)
  1002. {
  1003. byte target = 0;
  1004. for(int i = 0; i < 4; i++)
  1005. {
  1006. target = ShmPsuGrouping->Layout[i];
  1007. printf("\r\nGun[%d] Role: %d, Quantity: %d", target + 1, GroupCollection[target].Role, GroupCollection[target].GroupMemberQuantity);
  1008. if(GroupCollection[target].Role == 1)
  1009. {
  1010. printf(",");
  1011. printf(" %02X", target);
  1012. for(int j = 0; j < GroupCollection[target].GroupMemberQuantity; j++)
  1013. {
  1014. printf(" %02X", GroupCollection[target].GroupMember[j]);
  1015. }
  1016. }
  1017. if(GroupCollection[target].Role == 2)
  1018. {
  1019. printf(", TargetGroup: %02X", GroupCollection[target].TargetGroup - 1);
  1020. }
  1021. }
  1022. printf("\r\n\r\n");
  1023. }
  1024. void PsuGroupSwitchToIdle(byte group)
  1025. {
  1026. int master = 0, quantity = 0, location = 0, total = 0;
  1027. if(GroupCollection[group].Role != _GROLE_SLAVE)
  1028. {
  1029. return;
  1030. }
  1031. master = GroupCollection[group].TargetGroup - 1;
  1032. quantity = GroupCollection[master].GroupMemberQuantity;
  1033. //printf("\r\n Search %02X From Group %02X", group, master);
  1034. for(int i = 0; i < quantity; i++)
  1035. {
  1036. if(total == 0)
  1037. {
  1038. if(group == GroupCollection[master].GroupMember[i])
  1039. {
  1040. location = i;
  1041. total++;
  1042. //printf("\r\n Find Switch Idle Start Location %d", location);
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // find other group in the same direction
  1048. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[location]] < ShmPsuGrouping->Location[master])
  1049. {
  1050. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[i]] < ShmPsuGrouping->Location[master])
  1051. {
  1052. total++;
  1053. }
  1054. }
  1055. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[location]] > ShmPsuGrouping->Location[master])
  1056. {
  1057. if(ShmPsuGrouping->Location[GroupCollection[master].GroupMember[i]] > ShmPsuGrouping->Location[master])
  1058. {
  1059. total++;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. if(total > 0)
  1065. {
  1066. unsigned char collection[GENERAL_GUN_QUANTITY];
  1067. //printf("\r\n Total Switch Idle %d:", total);
  1068. memcpy(collection, GroupCollection[master].GroupMember, ARRAY_SIZE(GroupCollection[master].GroupMember));
  1069. for(int i = 0; i < total; i++)
  1070. {
  1071. //printf(" %02X", collection[i + location]);
  1072. }
  1073. for(int i = 0; i < total; i++)
  1074. {
  1075. RemoveGroupCollection(collection[i + location], master);
  1076. SetGroupRoleIdle(collection[i + location]);
  1077. }
  1078. }
  1079. }
  1080. int GetPsuGroupAvailable(byte group)
  1081. {
  1082. int slave = 0, location = 0, available = 0;
  1083. // search from left
  1084. location = GroupCollection[group].Location - 1;
  1085. for(int i = location; i >= 0; i--)
  1086. {
  1087. slave = ShmPsuGrouping->Layout[i];
  1088. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1089. {
  1090. available++;
  1091. }
  1092. else
  1093. {
  1094. break;
  1095. }
  1096. }
  1097. // search from right
  1098. location = GroupCollection[group].Location + 1;
  1099. for(int i = location; i < 4; i++)
  1100. {
  1101. slave = ShmPsuGrouping->Layout[i];
  1102. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1103. {
  1104. available++;
  1105. }
  1106. else
  1107. {
  1108. break;
  1109. }
  1110. }
  1111. return available;
  1112. }
  1113. void PsuGroupShareCheck(byte group)
  1114. {
  1115. int slave = 0, target = 0, location = 0, share = 0, total = 0;
  1116. total = GetPsuGroupAvailable(group) + 1;
  1117. //printf("\r\n Group %02X Available Quantity %d", group, total);
  1118. // search from left
  1119. location = GroupCollection[group].Location - 1;
  1120. for(int i = location; i >= 0; i--)
  1121. {
  1122. slave = ShmPsuGrouping->Layout[i];
  1123. if(GroupCollection[slave].Role == _GROLE_SLAVE)
  1124. {
  1125. target = GroupCollection[slave].TargetGroup - 1;
  1126. if(GroupCollection[target].GroupMemberQuantity > total)
  1127. {
  1128. share = (GroupCollection[target].GroupMemberQuantity + 1 - total) / 2;
  1129. }
  1130. //printf("\r\n Find Group %02X Have %d Resource Can Shared %d From Left", target, GroupCollection[target].GroupMemberQuantity, share);
  1131. break;
  1132. }
  1133. else
  1134. {
  1135. break;
  1136. }
  1137. }
  1138. if(share == 0)
  1139. {
  1140. // search from right
  1141. location = GroupCollection[group].Location + 1;
  1142. for(int i = location; i < 4; i++)
  1143. {
  1144. slave = ShmPsuGrouping->Layout[i];
  1145. if(GroupCollection[slave].Role == _GROLE_SLAVE)
  1146. {
  1147. target = GroupCollection[slave].TargetGroup - 1;
  1148. if(GroupCollection[target].GroupMemberQuantity > total)
  1149. {
  1150. share = (GroupCollection[target].GroupMemberQuantity + 1 - total) / 2;
  1151. }
  1152. //printf("\r\n Find Group %02X Have %d Resource Can Shared %d From Left", target, GroupCollection[target].GroupMemberQuantity, share);
  1153. break;
  1154. }
  1155. else
  1156. {
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. if(share > 0)
  1162. {
  1163. //printf("\r\n Grab %02X", slave);
  1164. PsuGroupSwitchToIdle(slave);
  1165. }
  1166. else
  1167. {
  1168. //printf("\r\n No Need To Grab");
  1169. }
  1170. }
  1171. void PsuGroupStartCharging(byte group)
  1172. {
  1173. int slave = 0, location = 0;
  1174. if(GroupCollection[group].Role != _GROLE_IDLE)
  1175. {
  1176. return;
  1177. }
  1178. PsuGroupShareCheck(group);
  1179. // search from left
  1180. location = GroupCollection[group].Location - 1;
  1181. for(int i = location; i >= 0; i--)
  1182. {
  1183. slave = ShmPsuGrouping->Layout[i];
  1184. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1185. {
  1186. //printf("\r\n Find %02X From Left", slave);
  1187. SetGroupRoleSlave(slave, group);
  1188. AddGroupCollection(slave, group);
  1189. }
  1190. else
  1191. {
  1192. break;
  1193. }
  1194. }
  1195. // search from right
  1196. location = GroupCollection[group].Location + 1;
  1197. for(int i = location; i < 4; i++)
  1198. {
  1199. slave = ShmPsuGrouping->Layout[i];
  1200. if(GroupCollection[slave].Role == _GROLE_IDLE)
  1201. {
  1202. //printf("\r\n Find %02X From Right", slave);
  1203. SetGroupRoleSlave(slave, group);
  1204. AddGroupCollection(slave, group);
  1205. }
  1206. else
  1207. {
  1208. break;
  1209. }
  1210. }
  1211. SetGroupRoleMaster(group);
  1212. }
  1213. void PsuGroupStopCharging(byte group)
  1214. {
  1215. if(GroupCollection[group].Role != _GROLE_MASTER)
  1216. {
  1217. return;
  1218. }
  1219. for(int i = 0; i < GroupCollection[group].GroupMemberQuantity; i++)
  1220. {
  1221. SetGroupRoleIdle(GroupCollection[group].GroupMember[i]);
  1222. }
  1223. SetGroupRoleIdle(group);
  1224. }
  1225. void RunPsuGrouping(char *v1, char *v2)
  1226. {
  1227. int group = 0, charging = 0;
  1228. group = atoi(v1);
  1229. charging = atoi(v2);
  1230. group -= 1;
  1231. if(group >= 0 && group < 4)
  1232. {
  1233. if(charging > 0)
  1234. {
  1235. if(GroupCollection[group].Role == _GROLE_IDLE || GroupCollection[group].Role == _GROLE_SLAVE)
  1236. {
  1237. if(GroupCollection[group].Role == _GROLE_SLAVE)
  1238. {
  1239. PsuGroupSwitchToIdle(group);
  1240. }
  1241. PsuGroupStartCharging(group);
  1242. }
  1243. }
  1244. if(charging == 0)
  1245. {
  1246. if(GroupCollection[group].Role == _GROLE_MASTER)
  1247. {
  1248. PsuGroupStopCharging(group);
  1249. }
  1250. if(GroupCollection[group].Role == _GROLE_SLAVE)
  1251. {
  1252. PsuGroupSwitchToIdle(group);
  1253. }
  1254. }
  1255. ShowGroupingInfo();
  1256. }
  1257. }
  1258. int main(void)
  1259. {
  1260. if(InitShareMemory() == FAIL)
  1261. {
  1262. printf ("InitShareMemory = FAIL \n");
  1263. if(ShmStatusCodeData != NULL)
  1264. {
  1265. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1266. }
  1267. sleep(5);
  1268. return 0;
  1269. }
  1270. memset(&GroupCollection[0], 0x00, sizeof(PsuGroupCollectionData));
  1271. memset(&GroupCollection[1], 0x00, sizeof(PsuGroupCollectionData));
  1272. memset(&GroupCollection[2], 0x00, sizeof(PsuGroupCollectionData));
  1273. memset(&GroupCollection[3], 0x00, sizeof(PsuGroupCollectionData));
  1274. GroupCollection[0].Index = 0;
  1275. GroupCollection[1].Index = 1;
  1276. GroupCollection[2].Index = 2;
  1277. GroupCollection[3].Index = 3;
  1278. GroupCollection[0].Location = 0;
  1279. GroupCollection[1].Location = 3;
  1280. GroupCollection[2].Location = 1;
  1281. GroupCollection[3].Location = 2;
  1282. for(;;)
  1283. {
  1284. char word[128];
  1285. char newString[7][10];
  1286. int i,j,ctr;
  1287. fgets(word, sizeof(word), stdin);
  1288. j=0; ctr=0;
  1289. strcpy(newString[1], "-1");
  1290. strcpy(newString[2], "-1");
  1291. for (i = 0; i <= (strlen(word)); i++)
  1292. {
  1293. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  1294. {
  1295. newString[ctr][j] = '\0';
  1296. ctr++;
  1297. j = 0;
  1298. }
  1299. else
  1300. {
  1301. newString[ctr][j] = word[i];
  1302. j++;
  1303. }
  1304. }
  1305. if(strcmp(newString[0], "state") == 0)
  1306. {
  1307. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1308. continue;
  1309. // 槍狀態
  1310. RunStatusProc(newString[1], newString[2]);
  1311. }
  1312. else if(strcmp(newString[0], "card") == 0)
  1313. {
  1314. // 刷卡狀態
  1315. RunCardProc(newString[1], newString[2]);
  1316. }
  1317. else if(strcmp(newString[0], "gun") == 0)
  1318. {
  1319. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1320. continue;
  1321. // 插槍狀態
  1322. RunGunPlugitProc(newString[1], newString[2]);
  1323. }
  1324. else if(strcmp(newString[0], "lock") == 0)
  1325. {
  1326. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1327. continue;
  1328. // 插槍狀態
  1329. GetGunLockStatusProc(newString[1], newString[2]);
  1330. }
  1331. else if(strcmp(newString[0], "sysid") == 0)
  1332. {
  1333. // 測試 sys id
  1334. SetSystemIDProc();
  1335. }
  1336. else if(strcmp(newString[0], "self") == 0)
  1337. {
  1338. // CSU 自我檢測狀態
  1339. RunSelfProc(newString[1]);
  1340. }
  1341. else if(strcmp(newString[0], "ver") == 0)
  1342. {
  1343. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1344. continue;
  1345. // 取 FW 版號
  1346. GetFwVerProc(newString[1]);
  1347. }
  1348. else if (strcmp(newString[0], "update") == 0)
  1349. {
  1350. // 更新
  1351. FwUpdateFlagProc(newString[1]);
  1352. }
  1353. else if (strcmp(newString[0], "ac") == 0)
  1354. {
  1355. // AC contactor 狀態
  1356. CheckAcStatus(newString[1]);
  1357. }
  1358. else if (strcmp(newString[0], "cable") == 0)
  1359. {
  1360. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1361. continue;
  1362. // cable check pass
  1363. SetCableChkStatus(newString[1], newString[2]);
  1364. }
  1365. else if (strcmp(newString[0], "pow") == 0)
  1366. {
  1367. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1368. continue;
  1369. // cable check pass
  1370. SetPowerValue(newString[1], newString[2]);
  1371. }
  1372. else if (strcmp(newString[0], "model") == 0)
  1373. {
  1374. GetSystemInfo();
  1375. }
  1376. else if(strcmp(newString[0], "select") == 0)
  1377. {
  1378. // 取得 / 設定 當前選的槍號
  1379. GetGunSelectedNum(newString[1]);
  1380. }
  1381. else if(strcmp(newString[0], "change") == 0)
  1382. {
  1383. // 模擬按鈕改變選槍
  1384. ChangeGunNum();
  1385. }
  1386. else if(strcmp(newString[0], "fan") == 0)
  1387. {
  1388. // 設定風扇速度
  1389. SetFanSpeed(newString[1]);
  1390. }
  1391. else if(strcmp(newString[0], "speed") == 0)
  1392. {
  1393. // 取得風扇速度
  1394. GetFanSpeed();
  1395. }
  1396. else if(strcmp(newString[0], "debug") == 0)
  1397. {
  1398. // 設定 debug mode
  1399. SetDebugMode(newString[1]);
  1400. }
  1401. else if (strcmp(newString[0], "gfd") == 0)
  1402. {
  1403. // 設定盲沖使用 GFD 功能
  1404. SetGFDMode(newString[1]);
  1405. }
  1406. else if(strcmp(newString[0], "temp") == 0)
  1407. {
  1408. // 取得 PSU 溫度
  1409. GetPsuTemp();
  1410. }
  1411. else if(strcmp(newString[0], "acin") == 0)
  1412. {
  1413. // 取得三向輸入電壓
  1414. GetAcInputVol();
  1415. }
  1416. else if(strcmp(newString[0], "psu") == 0)
  1417. {
  1418. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  1419. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  1420. {
  1421. printf ("PSU : Param fail..Please retry again......\n");
  1422. continue;
  1423. }
  1424. // 取得 PSU 資訊
  1425. GetPsuInformation(newString[1], newString[2], newString[3]);
  1426. }
  1427. else if (strcmp(newString[0], "cap") == 0)
  1428. {
  1429. GetConnectorCapInfo(newString[1]);
  1430. }
  1431. else if(strcmp(newString[0], "error") == 0)
  1432. {
  1433. CreateOneError(newString[1]);
  1434. }
  1435. else if (strcmp(newString[0], "auth") == 0)
  1436. {
  1437. GetAuthorizeFlag(newString[1]);
  1438. }
  1439. else if (strcmp(newString[0], "id") == 0)
  1440. {
  1441. GetOrClearId(newString[1]);
  1442. }
  1443. else if(strcmp(newString[0], "strchg") == 0)
  1444. {
  1445. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  1446. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 ||
  1447. strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  1448. {
  1449. printf ("Input cmd fail ------ strchg [vol 150-1000] [cru 2-100]\n");
  1450. continue;
  1451. }
  1452. // 槍狀態
  1453. RunUnconditionalChargeIndex1(newString[1], newString[2], newString[3]);
  1454. }
  1455. else if(strcmp(newString[0], "group") == 0)
  1456. {
  1457. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "-1") == 0)
  1458. {
  1459. ShowGroupingInfo();
  1460. continue;
  1461. }
  1462. RunPsuGrouping(newString[1], newString[2]);
  1463. }
  1464. else
  1465. printf ("%s\n", msg);
  1466. usleep(100000);
  1467. }
  1468. return 0;
  1469. }