Module_Dispenser.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /*
  2. * Module_Dispenser.c
  3. *
  4. * Created on: 2021/10/26
  5. * Author: folus
  6. */
  7. #include "Module_Dispenser.h"
  8. //==========================
  9. // Timeout constant define
  10. //==========================
  11. #define TIMEOUT_SPEC_HANDSHAKING 180
  12. #define TIMEOUT_SPEC_AUTH 15
  13. #define TIMEOUT_SPEC_HANDSHAKING_LED 185
  14. #define TIMEOUT_SPEC_LOGPPRINTOUT 30
  15. #define TIMEOUT_SPEC_PROFILE_PREPARE 60
  16. #define TIMEOUT_SPEC_BS_HLC_HANDSHAKE 60
  17. #define TIMEOUT_SPEC_EV_READY 30
  18. #define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET 10
  19. #define TIMEOUT_SPEC_CCS_HANDSHAKE 120
  20. #define TIMEOUT_SPEC_PWN_CHANGE 5
  21. #define TIMEOUT_SPEC_POWERSAVING_LCD 120
  22. #define TIMEOUT_SPEC_POWERSAVING_RFID 120
  23. #define TIMEOUT_SPEC_POWERSAVING_METER 120
  24. #define TIMEOUT_SPEC_POWERSAVING_LED_STATUS 120
  25. #define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION 15
  26. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  27. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  28. #define PASS 1
  29. #define FAIL -1
  30. #define YES 1
  31. #define NO 0
  32. #define ON 1
  33. #define OFF 0
  34. #define MtdBlockSize 0x300000
  35. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  36. struct StatusCodeData *ShmStatusCodeData;
  37. struct PrimaryMcuData *ShmPrimaryMcuData;
  38. struct DISPENSER *ShmDispenser;
  39. struct timespec startTime[TMR_IDX_CNT];
  40. struct timespec startChargingTime;
  41. struct timespec endChargingTime;
  42. struct SysConfigData SysConfigOrg;
  43. /*
  44. *
  45. * @param dest
  46. * @param src
  47. * @param start
  48. * @param cnt
  49. */
  50. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  51. {
  52. strncpy(dest, src + start, cnt);
  53. dest[cnt] = 0;
  54. }
  55. /*
  56. *
  57. * @param fmt
  58. * @return
  59. */
  60. int StoreLogMsg(const char *fmt, ...)
  61. {
  62. char Buf[4096+256];
  63. char buffer[4096];
  64. time_t CurrentTime;
  65. struct tm *tm;
  66. struct timeval tv;
  67. va_list args;
  68. va_start(args, fmt);
  69. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  70. va_end(args);
  71. memset(Buf,0,sizeof(Buf));
  72. CurrentTime = time(NULL);
  73. tm=localtime(&CurrentTime);
  74. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  75. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]DispenserLog",
  76. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  77. buffer,
  78. tm->tm_year+1900,tm->tm_mon+1);
  79. #ifdef SystemLogMessage
  80. system(Buf);
  81. #endif
  82. #ifdef ConsloePrintLog
  83. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  84. #endif
  85. return rc;
  86. }
  87. /*
  88. *
  89. * @param timer
  90. */
  91. void refreshStartTimer(struct timespec *timer)
  92. {
  93. clock_gettime(CLOCK_MONOTONIC, timer);
  94. }
  95. /*
  96. *
  97. * @param clk
  98. * @return
  99. */
  100. int getDiffSecNow(struct timespec timer)
  101. {
  102. struct timespec timerNow;
  103. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  104. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  105. }
  106. /*
  107. *
  108. * @param start
  109. * @param end
  110. * @return
  111. */
  112. int getDiffSecBetween(struct timespec start, struct timespec end)
  113. {
  114. return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
  115. }
  116. /*
  117. *
  118. * @param result
  119. */
  120. void getDateTimeString(char* result)
  121. {
  122. time_t CurrentTime;
  123. struct tm *tm;
  124. CurrentTime = time(NULL);
  125. tm=localtime(&CurrentTime);
  126. sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  127. }
  128. /*
  129. *
  130. * @param mode
  131. * @return
  132. */
  133. char* getSystemModeName(unsigned char mode)
  134. {
  135. char* result;
  136. switch(mode)
  137. {
  138. case SYS_MODE_BOOTING:
  139. result = "booting";
  140. break;
  141. case SYS_MODE_IDLE:
  142. result = "idle";
  143. break;
  144. case SYS_MODE_AUTHORIZING:
  145. result = "authorizing";
  146. break;
  147. case SYS_MODE_PREPARING:
  148. result = "preparing";
  149. break;
  150. case SYS_MODE_CHARGING:
  151. result = "charging";
  152. break;
  153. case SYS_MODE_TERMINATING:
  154. result = "terminating";
  155. break;
  156. case SYS_MODE_COMPLETE:
  157. result = "complete";
  158. break;
  159. case SYS_MODE_ALARM:
  160. result = "alarm";
  161. break;
  162. case SYS_MODE_FAULT:
  163. result = "fault";
  164. break;
  165. case SYS_MODE_MAINTAIN:
  166. result = "maintain";
  167. break;
  168. case SYS_MODE_RESERVATION:
  169. result = "reservation";
  170. break;
  171. case SYS_MODE_BOOKING:
  172. result = "booking";
  173. break;
  174. case SYS_MODE_DEBUG:
  175. result = "debug";
  176. break;
  177. case SYS_MODE_UPDATE:
  178. result = "upgrade";
  179. break;
  180. default:
  181. result = "unknown";
  182. break;
  183. }
  184. return result;
  185. }
  186. /*
  187. *
  188. * @param ptr
  189. * @return
  190. */
  191. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  192. {
  193. int fd,wrd;
  194. unsigned char *buf;
  195. unsigned int ChkSum,ChkSumOrg;
  196. if((buf=malloc(MtdBlockSize))==NULL)
  197. {
  198. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  199. if(ShmStatusCodeData!=NULL)
  200. {
  201. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  202. }
  203. sleep(5);
  204. system("reboot -f");
  205. sleep(5);
  206. system("reboot -f");
  207. }
  208. memset(buf, 0, MtdBlockSize);
  209. //================================================
  210. // Load configuration from mtdblock10
  211. //================================================
  212. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  213. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  214. if (fd < 0)
  215. {
  216. free(buf);
  217. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  218. if(ShmStatusCodeData!=NULL)
  219. {
  220. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  221. }
  222. sleep(5);
  223. system("reboot -f");
  224. sleep(5);
  225. system("reboot -f");
  226. }
  227. wrd=read(fd, buf, MtdBlockSize);
  228. close(fd);
  229. if(wrd<MtdBlockSize)
  230. {
  231. free(buf);
  232. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  233. if(ShmStatusCodeData!=NULL)
  234. {
  235. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  236. }
  237. sleep(5);
  238. system("reboot -f");
  239. sleep(5);
  240. system("reboot -f");
  241. }
  242. ChkSum=0;
  243. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  244. {
  245. ChkSum+=buf[wrd];
  246. }
  247. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  248. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  249. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  250. //================================================
  251. // Load configuration from mtdblock11
  252. //================================================
  253. if(ChkSum!=ChkSumOrg)
  254. {
  255. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  256. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  257. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  258. if (fd < 0)
  259. {
  260. free(buf);
  261. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  262. if(ShmStatusCodeData!=NULL)
  263. {
  264. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  265. }
  266. sleep(5);
  267. system("reboot -f");
  268. sleep(5);
  269. system("reboot -f");
  270. }
  271. memset(buf, 0, MtdBlockSize);
  272. wrd=read(fd, buf,MtdBlockSize);
  273. close(fd);
  274. if(wrd<MtdBlockSize)
  275. {
  276. free(buf);
  277. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  278. if(ShmStatusCodeData!=NULL)
  279. {
  280. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  281. }
  282. sleep(5);
  283. system("reboot -f");
  284. sleep(5);
  285. system("reboot -f");
  286. }
  287. ChkSum=0;
  288. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  289. {
  290. ChkSum+=buf[wrd];
  291. }
  292. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  293. //================================================
  294. // Load configuration from mtdblock12 (Factory default)
  295. //================================================
  296. if(ChkSum!=ChkSumOrg)
  297. {
  298. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  299. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  300. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  301. if (fd < 0)
  302. {
  303. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  304. free(buf);
  305. if(ShmStatusCodeData!=NULL)
  306. {
  307. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  308. }
  309. sleep(5);
  310. system("reboot -f");
  311. sleep(5);
  312. system("reboot -f");
  313. }
  314. memset(buf, 0, MtdBlockSize);
  315. wrd=read(fd, buf,MtdBlockSize);
  316. close(fd);
  317. if(wrd<MtdBlockSize)
  318. {
  319. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  320. free(buf);
  321. if(ShmStatusCodeData!=NULL)
  322. {
  323. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  324. }
  325. sleep(5);
  326. system("reboot -f");
  327. sleep(5);
  328. system("reboot -f");
  329. }
  330. ChkSum=0;
  331. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  332. {
  333. ChkSum+=buf[wrd];
  334. }
  335. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  336. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  337. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  338. if(ChkSum!=ChkSumOrg)
  339. {
  340. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  341. free(buf);
  342. system("cd /root;./Module_FactoryConfig -m");
  343. system("rm -f /Storage/OCPP/OCPPConfiguration");
  344. system("sync");
  345. sleep(5);
  346. system("reboot -f");
  347. sleep(5);
  348. system("reboot -f");
  349. return FAIL;
  350. }
  351. }
  352. }
  353. //load OK
  354. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  355. free(buf);
  356. system("rm -f /mnt/EvseConfig.bin");
  357. // SysConfig in flash is empty (0xffffffff)
  358. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  359. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  360. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  361. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  362. {
  363. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  364. {
  365. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  366. }
  367. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  368. {
  369. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  370. }
  371. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  372. {
  373. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  374. }
  375. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  376. {
  377. DEBUG_INFO("Ethernet dhcp config is null.\n");
  378. }
  379. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  380. {
  381. DEBUG_INFO("Model name over length.\n");
  382. }
  383. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  384. {
  385. DEBUG_INFO("Model serial number over length.\n");
  386. }
  387. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  388. {
  389. DEBUG_INFO("SystemId over length.\n");
  390. }
  391. system("cd /root;./Module_FactoryConfig -m");
  392. sleep(3);
  393. system("/usr/bin/run_evse_restart.sh");
  394. }
  395. DEBUG_INFO("Load SysConfigData OK\n");
  396. return PASS;
  397. }
  398. /*
  399. *
  400. * @param UsrData
  401. * @return
  402. */
  403. int StoreUsrConfigData(struct SysConfigData *UsrData)
  404. {
  405. int result = PASS;
  406. int fd,wrd;
  407. unsigned int i,Chk;
  408. unsigned char *ptr, *BufTmp;
  409. Chk=0;
  410. ptr=(unsigned char *)UsrData;
  411. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  412. {
  413. memset(BufTmp,0,MtdBlockSize);
  414. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  415. for(i=0;i<MtdBlockSize-4;i++)
  416. Chk+=*(BufTmp+i);
  417. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  418. // Output configuration to file.
  419. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  420. if (fd < 0)
  421. {
  422. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  423. free(BufTmp);
  424. return 0;
  425. }
  426. wrd=write(fd, BufTmp, MtdBlockSize);
  427. close(fd);
  428. if(wrd<MtdBlockSize)
  429. {
  430. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  431. free(BufTmp);
  432. return 0;
  433. }
  434. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  435. DEBUG_INFO("Erase /dev/mtd10.\n");
  436. runShellCmd("flash_erase /dev/mtd10 0 0");
  437. DEBUG_INFO("Write /dev/mtd10.\n");
  438. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  439. DEBUG_INFO("Erase /dev/mtd11.\n");
  440. runShellCmd("flash_erase /dev/mtd11 0 0");
  441. DEBUG_INFO("Write /dev/mtd11.\n");
  442. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  443. system("rm -f /mnt/EvseConfig.bin");
  444. DEBUG_INFO("EvseConfig write to flash OK\n");
  445. }
  446. else
  447. {
  448. DEBUG_ERROR("alloc BlockSize NG\r\n");
  449. result = FAIL;
  450. }
  451. if(BufTmp!=NULL)
  452. free(BufTmp);
  453. return result;
  454. }
  455. /*
  456. *
  457. * @param gun_index
  458. * @param mode
  459. * @return
  460. */
  461. unsigned char isMode(unsigned char mode)
  462. {
  463. return ((ShmDispenser->gun_info.SystemStatus == mode)?YES:NO);
  464. }
  465. /*
  466. *
  467. * @param gun_index
  468. * @return
  469. */
  470. unsigned char isModeChange()
  471. {
  472. unsigned char result = NO;
  473. if(!isMode(ShmDispenser->gun_info.PreviousSystemStatus))
  474. {
  475. result = YES;
  476. ShmDispenser->gun_info.PreviousSystemStatus = ShmDispenser->gun_info.SystemStatus;
  477. }
  478. return result;
  479. }
  480. /*
  481. *
  482. * @param gun_index
  483. * @param mode
  484. */
  485. void setChargerMode(unsigned char mode)
  486. {
  487. ShmDispenser->gun_info.PreviousSystemStatus = ShmDispenser->gun_info.SystemStatus;
  488. ShmDispenser->gun_info.SystemStatus = mode;
  489. DEBUG_INFO("System mode switch from %s to %s\n", getSystemModeName(ShmDispenser->gun_info.PreviousSystemStatus), getSystemModeName(ShmDispenser->gun_info.SystemStatus));
  490. }
  491. /*
  492. *
  493. * @return
  494. */
  495. int getRequest()
  496. {
  497. return ShmDispenser->gun_info.legacyRequest.isLegacyRequest;
  498. }
  499. /*
  500. *
  501. * @param gun_index
  502. * @return
  503. */
  504. int getRelay()
  505. {
  506. return ShmDispenser->gun_info.legacyRequest.isRelayOn;
  507. }
  508. /*
  509. *
  510. * @param gun_index
  511. * @return
  512. */
  513. int presentChargedEnergyClear()
  514. {
  515. int result = FAIL;
  516. ShmDispenser->gun_info.PresentChargedEnergy = 0;
  517. memset(ShmDispenser->gun_info.presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmDispenser->gun_info.presentChargedEnergyPeriod)*sizeof(float));
  518. result = PASS;
  519. return result;
  520. }
  521. /*
  522. *
  523. * @param gun_index
  524. * @return
  525. */
  526. float presentChargedEnergyTotal()
  527. {
  528. float result = 0.0f;
  529. for(int idx=0;idx<ARRAY_SIZE(ShmDispenser->gun_info.presentChargedEnergyPeriod);idx++)
  530. {
  531. result += ShmDispenser->gun_info.presentChargedEnergyPeriod[idx];
  532. }
  533. return result;
  534. }
  535. /*
  536. *
  537. * @param gun_index
  538. * @return
  539. */
  540. int presentChargedEnergyUpdate()
  541. {
  542. int result = FAIL;
  543. time_t CurrentTime;
  544. struct tm *tm;
  545. CurrentTime = time(NULL);
  546. tm=localtime(&CurrentTime);
  547. if(ShmDispenser->ConfigData.AcPhaseCount==1)
  548. {
  549. // Resolution: 0.0001 kwh
  550. ShmDispenser->gun_info.presentChargedEnergyPeriod[tm->tm_hour] += (((float)(ShmDispenser->gun_info.powerConsumptionTotal.power_consumption - ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start))/10000.0) - presentChargedEnergyTotal();
  551. }
  552. else
  553. {
  554. // Resolution: 0.0001 kwh
  555. ShmDispenser->gun_info.presentChargedEnergyPeriod[tm->tm_hour] += ((((float)(ShmDispenser->gun_info.powerConsumption[0].power_consumption - ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start))/10000.0) +
  556. (((float)(ShmDispenser->gun_info.powerConsumption[1].power_consumption - ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start))/10000.0) +
  557. (((float)(ShmDispenser->gun_info.powerConsumption[2].power_consumption - ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start))/10000.0))
  558. - presentChargedEnergyTotal();
  559. }
  560. ShmDispenser->gun_info.PresentChargedEnergy = presentChargedEnergyTotal();
  561. return result;
  562. }
  563. /*
  564. * Init all share memory
  565. * @return
  566. */
  567. int InitShareMemory()
  568. {
  569. int result = PASS;
  570. int MeterSMId;
  571. //Initial ShmSysConfigAndInfo
  572. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  573. {
  574. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  575. result = FAIL;
  576. }
  577. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  578. {
  579. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  580. result = FAIL;
  581. }
  582. else
  583. {}
  584. //Initial ShmStatusCodeData
  585. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  586. {
  587. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  588. result = FAIL;
  589. }
  590. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  591. {
  592. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  593. result = FAIL;
  594. }
  595. else
  596. {}
  597. //Initial ShmSDispenser
  598. if ((MeterSMId = shmget(ShmDispenserKey, sizeof(struct DISPENSER), 0777)) < 0)
  599. {
  600. DEBUG_ERROR("shmget ShmSDispenser NG\n");
  601. result = FAIL;
  602. }
  603. else if ((ShmDispenser = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  604. {
  605. DEBUG_ERROR("shmat ShmSDispenser NG\n");
  606. result = FAIL;
  607. }
  608. return result;
  609. }
  610. /*
  611. * TODO:
  612. * 1. Upgrade firmware
  613. */
  614. int upgrade_check()
  615. {
  616. int result = PASS;
  617. int fd;
  618. DIR *dir;
  619. struct dirent *file;
  620. char cmd[512];
  621. long int MaxLen=48*1024*1024;
  622. if ((dir = opendir ("/mnt")) != NULL)
  623. {
  624. /* print all the files and directories within directory */
  625. while ((file = readdir (dir)) != NULL)
  626. {
  627. if((strlen(file->d_name)>2))
  628. {
  629. // Wait for MCU upgrade finish.
  630. while(ShmDispenser->gun_info.mcuFlag.isMcuUpgradeReq)sleep(1);
  631. memset(&ShmDispenser->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  632. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  633. sprintf(ShmDispenser->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  634. if((fd=open(ShmDispenser->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  635. {
  636. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  637. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  638. read(fd, ptr, MaxLen);
  639. close(fd);
  640. ShmDispenser->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  641. substr(ShmDispenser->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  642. DEBUG_INFO("New firmware type: %X\n", ShmDispenser->fwUpgradeInfo.fwType);
  643. DEBUG_INFO("New firmware model name: %s, %s\n", ShmDispenser->fwUpgradeInfo.modelName, ShmDispenser->ConfigData.ModelName);
  644. if((ShmDispenser->fwUpgradeInfo.modelName[0] == ShmDispenser->ConfigData.ModelName[0]) &&
  645. (ShmDispenser->fwUpgradeInfo.modelName[1] == ShmDispenser->ConfigData.ModelName[1]) &&
  646. (ShmDispenser->fwUpgradeInfo.modelName[7] == ShmDispenser->ConfigData.ModelName[7]) &&
  647. (ShmDispenser->fwUpgradeInfo.modelName[8] == ShmDispenser->ConfigData.ModelName[8]) &&
  648. (ShmDispenser->fwUpgradeInfo.modelName[9] == ShmDispenser->ConfigData.ModelName[9]) &&
  649. (ShmDispenser->fwUpgradeInfo.modelName[11] == ShmDispenser->ConfigData.ModelName[11]) &&
  650. (ShmDispenser->fwUpgradeInfo.modelName[12] == ShmDispenser->ConfigData.ModelName[12]) &&
  651. (ShmDispenser->fwUpgradeInfo.modelName[13] == ShmDispenser->ConfigData.ModelName[13]) &&
  652. (ShmDispenser->fwUpgradeInfo.fwType>0))
  653. {
  654. switch(ShmDispenser->fwUpgradeInfo.fwType)
  655. {
  656. case CSU_MLO:
  657. case CSU_BOOTLOADER:
  658. case CSU_KERNEL_CONFIGURATION:
  659. case CSU_KERNEL_IMAGE:
  660. case CSU_ROOT_FILE_SYSTEM:
  661. case CSU_USER_CONFIGURATION:
  662. case CSU_PRIMARY_CONTROLLER:
  663. if(Upgrade_Flash(ShmDispenser->fwUpgradeInfo.fwType, ShmDispenser->fwUpgradeInfo.location, ShmDispenser->fwUpgradeInfo.modelName) != PASS)
  664. {
  665. result = FAIL;
  666. }
  667. else
  668. {
  669. if(ShmDispenser->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  670. {
  671. DEBUG_INFO("Restore model name & serial number.\n");
  672. memcpy(&SysConfigOrg, &ShmDispenser->ConfigData, sizeof(struct SysConfigData));
  673. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  674. {
  675. DEBUG_INFO("Re-load configuration fail.\n");
  676. result = FAIL;
  677. }
  678. else
  679. {
  680. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  681. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  682. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  683. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  684. {
  685. DEBUG_INFO("Re-write configuration fail.\n");
  686. result = FAIL;
  687. }
  688. else
  689. DEBUG_INFO("Re-write configuration OK.\n");
  690. }
  691. }
  692. }
  693. sprintf(cmd, "yes|rm %s", ShmDispenser->fwUpgradeInfo.location);
  694. system(cmd);
  695. break;
  696. case AC_WALLMOUNT_CONTROLLER:
  697. ShmDispenser->gun_info.mcuFlag.isMcuUpgradeReq = ON;
  698. sleep(10);
  699. break;
  700. default:
  701. result = FAIL;
  702. DEBUG_WARN("Image file is unknown type.\n");
  703. sprintf(cmd, "yes|rm %s", ShmDispenser->fwUpgradeInfo.location);
  704. system(cmd);
  705. break;
  706. }
  707. }
  708. else
  709. {
  710. result = FAIL;
  711. DEBUG_ERROR("Model name and Firmware type error.\n");
  712. sprintf(cmd, "yes|rm %s", ShmDispenser->fwUpgradeInfo.location);
  713. system(cmd);
  714. }
  715. free(ptr);
  716. }
  717. else
  718. {
  719. result = FAIL;
  720. DEBUG_ERROR("New firmware open error.\n");
  721. }
  722. }
  723. else
  724. {
  725. if(strlen(file->d_name) >= 3)
  726. {
  727. result = FAIL;
  728. DEBUG_ERROR("File name error.\n");
  729. }
  730. else
  731. {
  732. DEBUG_ERROR("Searching file.\n");
  733. }
  734. }
  735. }
  736. closedir (dir);
  737. }
  738. else
  739. {
  740. result = FAIL;
  741. DEBUG_ERROR("/mnt does not valid.\n");
  742. }
  743. return result;
  744. }
  745. /*
  746. *
  747. * @return
  748. */
  749. int main(void)
  750. {
  751. // Initial share memory
  752. if(InitShareMemory() == FAIL)
  753. {
  754. DEBUG_ERROR("InitShareMemory NG\n");
  755. if(ShmStatusCodeData!=NULL)
  756. {
  757. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  758. }
  759. sleep(5);
  760. return 0;
  761. }
  762. ShmDispenser->gun_info.PreviousSystemStatus = 0xff;
  763. ShmDispenser->gun_info.primaryMcuState.rotatory_switch = 0xff;
  764. ShmDispenser->gun_info.mcuResetRequest.isMcuResetRequest = ON;
  765. ShmDispenser->gun_info.isSetBreatheLedTiming = OFF;
  766. ShmDispenser->gun_info.isSetLedBrightness = OFF;
  767. DEBUG_INFO("Module_Dispenser initialized...\n");
  768. for(;;)
  769. {
  770. //==========================================
  771. // Check initialization "PASS" or "FAIL"
  772. //==========================================
  773. if(ShmDispenser->gun_info.SystemStatus != SYS_MODE_BOOTING)
  774. {
  775. // Alarm event check
  776. if((ShmDispenser->gun_info.primaryMcuAlarm.InputAlarmCode > 0))
  777. {
  778. if(ShmDispenser->gun_info.SystemStatus != SYS_MODE_ALARM)
  779. {
  780. if(ShmDispenser->gun_info.SystemStatus != SYS_MODE_UPDATE)
  781. {
  782. setChargerMode(SYS_MODE_ALARM);
  783. }
  784. }
  785. }
  786. }
  787. //==========================================
  788. // Switch operative
  789. //==========================================
  790. if(ShmDispenser->gun_info.isOperactive)
  791. {
  792. if(isMode(SYS_MODE_MAINTAIN))
  793. {
  794. setChargerMode(SYS_MODE_IDLE);
  795. }
  796. }
  797. else
  798. {
  799. if(isMode(SYS_MODE_IDLE))
  800. {
  801. setChargerMode(SYS_MODE_MAINTAIN);
  802. }
  803. }
  804. //==========================================
  805. // Upgrade check
  806. //==========================================
  807. if(ShmDispenser->gun_info.isUpgradeReq && !ShmDispenser->gun_info.isUpgradeEnd)
  808. {
  809. if((ShmDispenser->gun_info.SystemStatus == SYS_MODE_IDLE) || (ShmDispenser->gun_info.SystemStatus == SYS_MODE_ALARM))
  810. {
  811. if((ShmDispenser->gun_info.isUpgradePASS = upgrade_check()) == PASS)
  812. {
  813. DEBUG_INFO("Upgrade process pass.\n");
  814. }
  815. else
  816. {
  817. DEBUG_ERROR("Upgrade process fail.\n");
  818. }
  819. ShmDispenser->gun_info.isUpgradeEnd = ON;
  820. }
  821. }
  822. //==========================================
  823. // Gun process
  824. //==========================================
  825. switch(ShmDispenser->gun_info.SystemStatus)
  826. {
  827. case SYS_MODE_BOOTING:
  828. if(isModeChange())
  829. {}
  830. if(ShmDispenser->gun_info.mcuFlag.isReadFwVerPass &&
  831. ShmDispenser->gun_info.mcuFlag.isSetModelNamePass &&
  832. ShmDispenser->gun_info.mcuFlag.isSetSerialNumberPass)
  833. {
  834. // Set max current to rating current
  835. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ShmDispenser->gun_info.primaryMcuState.rating_current;
  836. // If rotate switch equal zero, the system needs to change Debug mode
  837. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch == 0)
  838. setChargerMode(SYS_MODE_DEBUG);
  839. else
  840. setChargerMode(SYS_MODE_IDLE);
  841. }
  842. break;
  843. case SYS_MODE_IDLE:
  844. if(isModeChange())
  845. {
  846. ShmDispenser->gun_info.isGunPlugged = NO;
  847. ShmDispenser->gun_info.systemAlarmCode.SystemAlarmCode = 0x00;
  848. ShmDispenser->gun_info.PresentChargedDuration = 0;
  849. presentChargedEnergyClear();
  850. ShmDispenser->gun_info.targetCurrent = 0xFF;
  851. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_IDLE;
  852. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  853. ShmDispenser->gun_info.isDoEvReadyOnce = OFF;
  854. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  855. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  856. if(ShmDispenser->isCcsEnable)system("pkill Module_CCS");
  857. }
  858. if(ShmDispenser->gun_info.legacyRequest.isLegacyRequest)
  859. {
  860. ShmDispenser->gun_info.isGunPlugged = NO;
  861. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  862. if(ShmDispenser->isCcsEnable)
  863. {
  864. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  865. }
  866. else
  867. {
  868. if((6 <= ShmDispenser->gun_info.targetCurrent))
  869. {
  870. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
  871. }
  872. else if((1 <= ShmDispenser->gun_info.targetCurrent) && (ShmDispenser->gun_info.targetCurrent < 6))
  873. {
  874. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 6;
  875. }
  876. else
  877. {
  878. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 100;
  879. }
  880. }
  881. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  882. setChargerMode(SYS_MODE_AUTHORIZING);
  883. }
  884. else
  885. {}
  886. break;
  887. case SYS_MODE_AUTHORIZING:
  888. if(isModeChange())
  889. {}
  890. if(ShmDispenser->isCcsEnable)
  891. {
  892. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  893. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  894. sleep(4);
  895. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  896. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  897. system("/root/Module_CCS &");
  898. }
  899. setChargerMode(SYS_MODE_PREPARING);
  900. break;
  901. case SYS_MODE_PREPARING:
  902. if(isModeChange())
  903. {
  904. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  905. if(ShmDispenser->isCcsEnable)
  906. {
  907. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_DUTY_5;
  908. }
  909. else
  910. {
  911. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  912. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  913. }
  914. }
  915. // If control pilot detect Bx, skip watch dog time out.
  916. if((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_B) ||
  917. (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_C))
  918. {
  919. ShmDispenser->gun_info.isGunPlugged = YES;
  920. switch(ShmDispenser->gun_info.ccsHandshakeState)
  921. {
  922. case HANDSHAKE_DUTY_5:
  923. //Let CCS task start to negotiate
  924. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = ON;
  925. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  926. // Set CCS 5% PWM duty
  927. if(ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)
  928. {
  929. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  930. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  931. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  932. }
  933. break;
  934. case HANDSHAKE_DUTY_5_CHECK:
  935. if((ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF))
  936. {
  937. //2 secs timeout
  938. refreshStartTimer(&startTime[TMR_IDX_BS_HLC_HANDSHAKE]);
  939. DEBUG_INFO("HLC slac handshake start.\n");
  940. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CCS;
  941. }
  942. break;
  943. case HANDSHAKE_CCS:
  944. //CCS handshake timeout
  945. if(getDiffSecNow(startTime[TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  946. {
  947. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  948. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  949. DEBUG_INFO("HLC %d secs slac handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  950. }
  951. if((ShmDispenser->gun_info.acCcsInfo.ChargingPermission == OFF) && (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  952. {
  953. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  954. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  955. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  956. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  957. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  958. }
  959. //CCS status check
  960. if((16 < ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus) && (ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus < 254))
  961. {
  962. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_HLC;
  963. DEBUG_INFO("Enter HLC Mode charging.\n");
  964. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_HLC_MODE;
  965. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  966. }
  967. break;
  968. case HANDSHAKE_CP_STATE_E:
  969. if(ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)
  970. {
  971. if(ShmDispenser->isCcsEnable)
  972. {
  973. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  974. //CP STATE E for 4 secs
  975. sleep(4);
  976. }
  977. //restore normal CP PWM duty
  978. // Determine max charging current to MCU
  979. DEBUG_INFO("Determine max charging current to MCU.\n");
  980. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
  981. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  982. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  983. refreshStartTimer(&startTime[TMR_IDX_BS_HLC_HANDSHAKE]);
  984. }
  985. break;
  986. case HANDSHAKE_SET_MAX_CURRENT:
  987. if(ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)
  988. {
  989. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_BS_MODE;
  990. DEBUG_INFO("Enter BS Mode charging.\n");
  991. //for EV READY 30 secs didn't start charging to STATE E
  992. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  993. }
  994. break;
  995. case HANDSHAKE_BS_MODE:
  996. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  997. presentChargedEnergyClear();
  998. getDateTimeString((char*)ShmDispenser->gun_info.StartDateTime);
  999. ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start = ShmDispenser->gun_info.powerConsumptionTotal.power_consumption;
  1000. ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[0].power_consumption;
  1001. ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[1].power_consumption;
  1002. ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[2].power_consumption;
  1003. refreshStartTimer(&startChargingTime);
  1004. setChargerMode(SYS_MODE_CHARGING);
  1005. //EV READY CHECK
  1006. /*
  1007. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  1008. {
  1009. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  1010. {
  1011. DEBUG_INFO("EV READY STATE E 4sec.\n");
  1012. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  1013. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  1014. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1015. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  1016. }
  1017. }
  1018. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  1019. {
  1020. switch(ShmCharger->gun_info[gun_index].evReadyState)
  1021. {
  1022. case EV_READY_STAT_E:
  1023. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  1024. {
  1025. sleep(4);
  1026. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1027. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1028. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  1029. }
  1030. break;
  1031. case EV_READY_STAT_C:
  1032. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  1033. {
  1034. usleep(500000);
  1035. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  1036. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  1037. {
  1038. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  1039. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  1040. {
  1041. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  1042. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  1048. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  1049. {
  1050. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  1051. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1052. }
  1053. }
  1054. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1055. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  1056. }
  1057. break;
  1058. case EV_READY_SET_MAX_CURRENT:
  1059. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  1060. {
  1061. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  1062. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  1063. }
  1064. break;
  1065. default:
  1066. break;
  1067. }
  1068. }*/
  1069. break;
  1070. case HANDSHAKE_HLC_MODE:
  1071. if(ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_START_MODE)
  1072. {
  1073. ShmDispenser->gun_info.isCCSStartTransation = ON;
  1074. }
  1075. if((ShmDispenser->gun_info.isCCSStartTransation == ON))
  1076. {
  1077. presentChargedEnergyClear();
  1078. getDateTimeString((char*)ShmDispenser->gun_info.StartDateTime);
  1079. ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start = ShmDispenser->gun_info.powerConsumptionTotal.power_consumption;
  1080. ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[0].power_consumption;
  1081. ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[1].power_consumption;
  1082. ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[2].power_consumption;
  1083. ShmDispenser->gun_info.isCCSStartTransation = OFF;
  1084. setChargerMode(SYS_MODE_CHARGING);
  1085. refreshStartTimer(&startChargingTime);
  1086. refreshStartTimer(&startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  1087. }
  1088. //120 sec timeout
  1089. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  1090. {
  1091. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5)
  1092. {
  1093. DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
  1094. //Cancel CCS task negotiating
  1095. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1096. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1097. //ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1098. if((ShmDispenser->gun_info.acCcsInfo.ChargingPermission == OFF) && (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  1099. {
  1100. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  1101. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  1102. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1103. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  1104. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  1105. }
  1106. }
  1107. }
  1108. break;
  1109. default:
  1110. break;
  1111. }
  1112. }
  1113. else if(ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEMode)
  1114. {
  1115. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  1116. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_SOCKETE;
  1117. refreshStartTimer(&startChargingTime);
  1118. setChargerMode(SYS_MODE_CHARGING);
  1119. }
  1120. // Unplug charging gun to Idle mode
  1121. if((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A) && (ShmDispenser->gun_info.isGunPlugged == YES))
  1122. {
  1123. DEBUG_INFO("Charging gun is plugged before.\n");
  1124. //Cancel CCS task negotiating
  1125. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1126. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1127. ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1128. }
  1129. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > ShmDispenser->timeoutSpec.Present_Timeout_Spec)
  1130. {
  1131. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > (ShmDispenser->timeoutSpec.Present_Timeout_Spec+5))
  1132. {
  1133. DEBUG_INFO("Handshaking timeout...\n");
  1134. //Cancel CCS task negotiating
  1135. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1136. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1137. ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1138. }
  1139. }
  1140. if(((ShmDispenser->gun_info.isCCSWaitChangeDuty == ON) && ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5) ||
  1141. !ShmDispenser->gun_info.legacyRequest.isLegacyRequest)
  1142. {
  1143. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1144. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1145. ShmDispenser->gun_info.isCCSWaitChangeDuty = OFF;
  1146. setChargerMode(SYS_MODE_IDLE);
  1147. }
  1148. break;
  1149. case SYS_MODE_CHARGING:
  1150. if(isModeChange())
  1151. {
  1152. refreshStartTimer(&startTime[TMR_IDX_LOGPPRINTOUT]);
  1153. refreshStartTimer(&startTime[TMR_IDX_PROFILE_PREPARE]);
  1154. refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
  1155. ShmDispenser->gun_info.isChargerStopByCondition = NO;
  1156. ShmDispenser->gun_info.resultAuthorization = DEFAULT_RFID;
  1157. }
  1158. //if time up, clear CCS MSG count
  1159. if((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) &&
  1160. (getDiffSecNow(startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  1161. {
  1162. refreshStartTimer(&startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  1163. if(ShmDispenser->gun_info.acCcsInfo.CcsHeartBeat > 0)
  1164. {
  1165. ShmDispenser->gun_info.acCcsInfo.CcsHeartBeat = 0;
  1166. }
  1167. else
  1168. {
  1169. DEBUG_INFO("CCS could not get MSG from car.\n");
  1170. //setChargerMode(SYS_MODE_TERMINATING);
  1171. //setRelay(OFF);
  1172. }
  1173. }
  1174. if(!ShmDispenser->gun_info.legacyRequest.isLegacyRequest ||
  1175. ((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && ShmDispenser->gun_info.primaryMcuState.cp_state != CP_STATE_C) ||
  1176. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && !ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn) ||
  1177. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  1178. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  1179. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  1180. {
  1181. setChargerMode(SYS_MODE_TERMINATING);
  1182. //setRelay(gun_index, OFF);
  1183. }
  1184. else
  1185. {
  1186. // Charging session info calculation
  1187. refreshStartTimer(&endChargingTime);
  1188. ShmDispenser->gun_info.PresentChargedDuration = getDiffSecBetween(startChargingTime, endChargingTime);
  1189. presentChargedEnergyUpdate();
  1190. // Determine max charging current to MCU
  1191. if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS)
  1192. {
  1193. if(getDiffSecNow(startTime[TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  1194. {
  1195. if((6 <= ShmDispenser->gun_info.targetCurrent))
  1196. {
  1197. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
  1198. }
  1199. else if((1 <= ShmDispenser->gun_info.targetCurrent) && (ShmDispenser->gun_info.targetCurrent < 6))
  1200. {
  1201. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 6;
  1202. }
  1203. else
  1204. {
  1205. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 100;
  1206. }
  1207. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  1208. refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
  1209. }
  1210. }
  1211. else if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC)
  1212. {
  1213. ShmDispenser->gun_info.acCcsInfo.EVSEMaxCurrent = (float)((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
  1214. }
  1215. // Debug information
  1216. if(getDiffSecNow(startTime[TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  1217. {
  1218. DEBUG_INFO("================================================\n");
  1219. DEBUG_INFO("gun_info.primaryMcuCp_Pwn_Duty.max_current: %d\n", ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current);
  1220. DEBUG_INFO("gun_info.targetCurrent: %d\n", ShmDispenser->gun_info.targetCurrent);
  1221. DEBUG_INFO("================================================\n");
  1222. refreshStartTimer(&startTime[TMR_IDX_LOGPPRINTOUT]);
  1223. getDateTimeString((char*)ShmDispenser->gun_info.StopDateTime);
  1224. }
  1225. }
  1226. break;
  1227. case SYS_MODE_TERMINATING:
  1228. if(isModeChange())
  1229. {
  1230. getDateTimeString((char*)ShmDispenser->gun_info.StopDateTime);
  1231. }
  1232. refreshStartTimer(&endChargingTime);
  1233. if(ShmDispenser->gun_info.PresentChargedDuration != 0)
  1234. {
  1235. ShmDispenser->gun_info.PresentChargedDuration = getDiffSecBetween(startChargingTime, endChargingTime);
  1236. }
  1237. // End authorize pass
  1238. if(!ShmDispenser->gun_info.legacyRequest.isLegacyRequest ||
  1239. ((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A)) ||
  1240. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && !ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn) ||
  1241. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  1242. {
  1243. if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS)
  1244. {
  1245. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1246. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1247. sleep(1);
  1248. setChargerMode(SYS_MODE_COMPLETE);
  1249. }
  1250. else if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC)
  1251. {
  1252. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  1253. //Cancel CCS task negotiating
  1254. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1255. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1256. ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1257. }
  1258. else
  1259. {
  1260. setChargerMode(SYS_MODE_COMPLETE);
  1261. }
  1262. }
  1263. else
  1264. {
  1265. // Debug information
  1266. if(getDiffSecNow(startTime[TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  1267. {
  1268. DEBUG_INFO("=============================================================\n");
  1269. DEBUG_INFO("ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current: %d\n", ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current);
  1270. DEBUG_INFO("ShmDispenser->gun_info.targetCurrent: %d\n", ShmDispenser->gun_info.targetCurrent);
  1271. DEBUG_INFO("=============================================================\n");
  1272. refreshStartTimer(&startTime[TMR_IDX_LOGPPRINTOUT]);
  1273. }
  1274. if(ShmDispenser->gun_info.legacyRequest.isLegacyRequest &&
  1275. (((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_C)) || ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn)) &&
  1276. (((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_START_MODE)) || (ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS))
  1277. )
  1278. {
  1279. setChargerMode(SYS_MODE_CHARGING);
  1280. }
  1281. }
  1282. if((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) &&
  1283. (ShmDispenser->gun_info.isCCSWaitChangeDuty == ON) &&
  1284. (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  1285. {
  1286. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  1287. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1288. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1289. ShmDispenser->gun_info.isCCSWaitChangeDuty = OFF;
  1290. setChargerMode( SYS_MODE_COMPLETE);
  1291. }
  1292. break;
  1293. case SYS_MODE_COMPLETE:
  1294. if(isModeChange())
  1295. {}
  1296. if(((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A)) ||
  1297. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && (!ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn)))
  1298. {
  1299. setChargerMode(SYS_MODE_IDLE);
  1300. }
  1301. break;
  1302. case SYS_MODE_ALARM:
  1303. if((ShmDispenser->gun_info.systemAlarmCode.SystemAlarmCode == 0))
  1304. {
  1305. if((ShmDispenser->gun_info.PreviousSystemStatus == SYS_MODE_CHARGING) ||
  1306. (ShmDispenser->gun_info.PreviousSystemStatus == SYS_MODE_TERMINATING))
  1307. {
  1308. setChargerMode(ShmDispenser->gun_info.PreviousSystemStatus);
  1309. }
  1310. else
  1311. {
  1312. setChargerMode(SYS_MODE_IDLE);
  1313. }
  1314. }
  1315. break;
  1316. case SYS_MODE_FAULT:
  1317. if(isModeChange())
  1318. {}
  1319. break;
  1320. case SYS_MODE_MAINTAIN:
  1321. if(isModeChange())
  1322. {}
  1323. if(ShmDispenser->gun_info.isOperactive)
  1324. {
  1325. DEBUG_INFO("Connector change to operactive.\n");
  1326. setChargerMode(SYS_MODE_IDLE);
  1327. }
  1328. break;
  1329. case SYS_MODE_UPDATE:
  1330. if(isModeChange())
  1331. {}
  1332. break;
  1333. case SYS_MODE_DEBUG:
  1334. if(isModeChange())
  1335. {}
  1336. break;
  1337. }
  1338. usleep(100000);
  1339. }
  1340. return -1;
  1341. }