Module_Dispenser.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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_REFRESH_CHARGING_INFO 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. * Upgrade firmware
  612. * @return
  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. }
  857. if(ShmDispenser->gun_info.legacyRequest.isLegacyRequest)
  858. {
  859. ShmDispenser->gun_info.isGunPlugged = NO;
  860. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  861. if(ShmDispenser->isCcsEnable)
  862. {
  863. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  864. }
  865. else
  866. {
  867. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = (ShmDispenser->gun_info.targetCurrent==0?CCS_PWM_DUTY_100:ShmDispenser->gun_info.targetCurrent);
  868. }
  869. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  870. setChargerMode(SYS_MODE_AUTHORIZING);
  871. }
  872. else
  873. {}
  874. break;
  875. case SYS_MODE_AUTHORIZING:
  876. if(isModeChange())
  877. {}
  878. setChargerMode(SYS_MODE_PREPARING);
  879. break;
  880. case SYS_MODE_PREPARING:
  881. if(isModeChange())
  882. {
  883. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  884. if(ShmDispenser->isCcsEnable)
  885. {
  886. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_DUTY_5;
  887. }
  888. else
  889. {
  890. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  891. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  892. }
  893. }
  894. // If control pilot detect Bx, skip watch dog time out.
  895. if((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_B) ||
  896. (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_C))
  897. {
  898. ShmDispenser->gun_info.isGunPlugged = YES;
  899. switch(ShmDispenser->gun_info.ccsHandshakeState)
  900. {
  901. case HANDSHAKE_DUTY_5:
  902. //Let CCS task start to negotiate
  903. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = ON;
  904. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  905. // Set CCS 5% PWM duty
  906. if(ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)
  907. {
  908. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  909. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  910. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  911. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK\n");
  912. }
  913. break;
  914. case HANDSHAKE_DUTY_5_CHECK:
  915. if((ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF))
  916. {
  917. //2 secs timeout
  918. refreshStartTimer(&startTime[TMR_IDX_BS_HLC_HANDSHAKE]);
  919. DEBUG_INFO("HLC slac handshake start.\n");
  920. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CCS;
  921. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS\n");
  922. }
  923. break;
  924. case HANDSHAKE_CCS:
  925. //CCS handshake timeout
  926. if(getDiffSecNow(startTime[TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  927. {
  928. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  929. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  930. DEBUG_INFO("HLC %d secs slac handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  931. if(ShmDispenser->isCcsEnable)
  932. {
  933. DEBUG_INFO("TIMEOUT_SPEC_BS_HLC_HANDSHAKE.\n");
  934. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  935. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  936. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  937. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  938. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  939. break;
  940. }
  941. else
  942. {
  943. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  944. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  945. DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  946. }
  947. }
  948. if((ShmDispenser->gun_info.acCcsInfo.ChargingPermission == OFF) && (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  949. {
  950. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  951. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  952. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  953. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  954. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  955. }
  956. //CCS status check
  957. if((16 <= ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus) && (ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus < 253))
  958. {
  959. //change PWM duty to BS
  960. if((ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)&&(ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current!=CCS_PWM_DUTY_5))
  961. {
  962. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current =CCS_PWM_DUTY_5;
  963. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  964. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS (set 5% duty)\n");
  965. }
  966. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  967. }
  968. if((37 < ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus) && (ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus < 49))
  969. {
  970. //chang PWM duty to BS
  971. if((ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)&&(ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current!=CCS_PWM_DUTY_5))
  972. {
  973. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current =CCS_PWM_DUTY_5;
  974. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  975. }
  976. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_HLC;
  977. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_HLC_MODE;
  978. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_HLC_MODE\n");
  979. }
  980. else if((54 < ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus) && (ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus <= 255))
  981. {
  982. DEBUG_INFO("ccsHandshakeState = CHARGING_MODE_BS, CCS terminated\n");
  983. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  984. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  985. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  986. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  987. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  988. }
  989. break;
  990. case HANDSHAKE_CP_STATE_E:
  991. if(ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)
  992. {
  993. if(ShmDispenser->isCcsEnable)
  994. {
  995. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  996. //CP STATE E for 4 secs
  997. sleep(4);
  998. }
  999. //restore normal CP PWM duty
  1000. // Determine max charging current to MCU
  1001. DEBUG_INFO("Determine max charging current to MCU.\n");
  1002. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = (ShmDispenser->gun_info.targetCurrent==0?CCS_PWM_DUTY_100:ShmDispenser->gun_info.targetCurrent);
  1003. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  1004. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  1005. refreshStartTimer(&startTime[TMR_IDX_BS_HLC_HANDSHAKE]);
  1006. }
  1007. break;
  1008. case HANDSHAKE_SET_MAX_CURRENT:
  1009. if(ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)
  1010. {
  1011. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_BS_MODE;
  1012. DEBUG_INFO("Enter BS Mode charging.\n");
  1013. //for EV READY 30 secs didn't start charging to STATE E
  1014. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  1015. }
  1016. break;
  1017. case HANDSHAKE_BS_MODE:
  1018. refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
  1019. presentChargedEnergyClear();
  1020. getDateTimeString((char*)ShmDispenser->gun_info.StartDateTime);
  1021. ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start = ShmDispenser->gun_info.powerConsumptionTotal.power_consumption;
  1022. ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[0].power_consumption;
  1023. ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[1].power_consumption;
  1024. ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[2].power_consumption;
  1025. refreshStartTimer(&startChargingTime);
  1026. setChargerMode(SYS_MODE_CHARGING);
  1027. //EV READY CHECK
  1028. /*
  1029. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  1030. {
  1031. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  1032. {
  1033. DEBUG_INFO("EV READY STATE E 4sec.\n");
  1034. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  1035. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  1036. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1037. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  1038. }
  1039. }
  1040. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  1041. {
  1042. switch(ShmCharger->gun_info[gun_index].evReadyState)
  1043. {
  1044. case EV_READY_STAT_E:
  1045. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  1046. {
  1047. sleep(4);
  1048. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1049. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1050. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  1051. }
  1052. break;
  1053. case EV_READY_STAT_C:
  1054. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  1055. {
  1056. usleep(500000);
  1057. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  1058. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  1059. {
  1060. 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);
  1061. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  1062. {
  1063. 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);
  1064. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1065. }
  1066. }
  1067. else
  1068. {
  1069. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  1070. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  1071. {
  1072. 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);
  1073. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1074. }
  1075. }
  1076. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1077. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  1078. }
  1079. break;
  1080. case EV_READY_SET_MAX_CURRENT:
  1081. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  1082. {
  1083. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  1084. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  1085. }
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. }*/
  1091. break;
  1092. case HANDSHAKE_HLC_MODE:
  1093. if(ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_START_MODE)//powerDelivery
  1094. {
  1095. ShmDispenser->gun_info.isCCSStartTransation = ON;
  1096. }
  1097. if((ShmDispenser->gun_info.isCCSStartTransation == ON))
  1098. {
  1099. presentChargedEnergyClear();
  1100. getDateTimeString((char*)ShmDispenser->gun_info.StartDateTime);
  1101. ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start = ShmDispenser->gun_info.powerConsumptionTotal.power_consumption;
  1102. ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[0].power_consumption;
  1103. ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[1].power_consumption;
  1104. ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[2].power_consumption;
  1105. ShmDispenser->gun_info.isCCSStartTransation = OFF;
  1106. setChargerMode(SYS_MODE_CHARGING);
  1107. refreshStartTimer(&startChargingTime);
  1108. refreshStartTimer(&startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  1109. }
  1110. //120 sec timeout
  1111. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  1112. {
  1113. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5)
  1114. {
  1115. DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
  1116. //Cancel CCS task negotiating
  1117. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1118. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1119. //ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1120. if((ShmDispenser->gun_info.acCcsInfo.ChargingPermission == OFF) && (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  1121. {
  1122. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  1123. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  1124. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1125. ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  1126. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
  1127. }
  1128. }
  1129. }
  1130. break;
  1131. default:
  1132. break;
  1133. }
  1134. }
  1135. else if(ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEMode)
  1136. {
  1137. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  1138. ShmDispenser->gun_info.chargingMode = CHARGING_MODE_SOCKETE;
  1139. refreshStartTimer(&startChargingTime);
  1140. setChargerMode(SYS_MODE_CHARGING);
  1141. }
  1142. // Unplug charging gun to Idle mode
  1143. if((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A) && (ShmDispenser->gun_info.isGunPlugged == YES))
  1144. {
  1145. DEBUG_INFO("Charging gun is plugged before.\n");
  1146. //Cancel CCS task negotiating
  1147. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1148. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1149. ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1150. }
  1151. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > ShmDispenser->timeoutSpec.Present_Timeout_Spec)
  1152. {
  1153. if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > (ShmDispenser->timeoutSpec.Present_Timeout_Spec+5))
  1154. {
  1155. DEBUG_INFO("Handshaking timeout...\n");
  1156. //Cancel CCS task negotiating
  1157. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1158. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1159. ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1160. }
  1161. }
  1162. if(((ShmDispenser->gun_info.isCCSWaitChangeDuty == ON) && ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5) ||
  1163. !ShmDispenser->gun_info.legacyRequest.isLegacyRequest)
  1164. {
  1165. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1166. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1167. ShmDispenser->gun_info.isCCSWaitChangeDuty = OFF;
  1168. setChargerMode(SYS_MODE_IDLE);
  1169. }
  1170. break;
  1171. case SYS_MODE_CHARGING:
  1172. if(isModeChange())
  1173. {
  1174. refreshStartTimer(&startTime[TMR_IDX_REFRESH_CHARGING_INFO]);
  1175. refreshStartTimer(&startTime[TMR_IDX_PROFILE_PREPARE]);
  1176. refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
  1177. ShmDispenser->gun_info.isChargerStopByCondition = NO;
  1178. ShmDispenser->gun_info.resultAuthorization = DEFAULT_RFID;
  1179. }
  1180. //if time up, clear CCS MSG count
  1181. if((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) &&
  1182. (getDiffSecNow(startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  1183. {
  1184. refreshStartTimer(&startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  1185. }
  1186. if(!ShmDispenser->gun_info.legacyRequest.isLegacyRequest ||
  1187. ((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && ShmDispenser->gun_info.primaryMcuState.cp_state != CP_STATE_C) ||
  1188. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && !ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn) ||
  1189. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  1190. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  1191. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE) ||
  1192. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ((49 <= ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus) && (ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus <= 255))))
  1193. {
  1194. setChargerMode(SYS_MODE_TERMINATING);
  1195. //setRelay(gun_index, OFF);
  1196. }
  1197. else
  1198. {
  1199. // Charging session info calculation
  1200. refreshStartTimer(&endChargingTime);
  1201. ShmDispenser->gun_info.PresentChargedDuration = getDiffSecBetween(startChargingTime, endChargingTime);
  1202. presentChargedEnergyUpdate();
  1203. // Determine max charging current to MCU
  1204. if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS)
  1205. {
  1206. if(getDiffSecNow(startTime[TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  1207. {
  1208. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = (ShmDispenser->gun_info.targetCurrent==0?CCS_PWM_DUTY_100:ShmDispenser->gun_info.targetCurrent);
  1209. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  1210. refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
  1211. }
  1212. }
  1213. else if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC)
  1214. {
  1215. 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);
  1216. }
  1217. // Debug information
  1218. if(getDiffSecNow(startTime[TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_REFRESH_CHARGING_INFO)
  1219. {
  1220. DEBUG_INFO("==================================================\n");
  1221. DEBUG_INFO("gun_info.primaryMcuCp_Pwn_Duty.max_current: %d\n", ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current);
  1222. DEBUG_INFO("gun_info.targetCurrent: %d\n", ShmDispenser->gun_info.targetCurrent);
  1223. DEBUG_INFO("==================================================\n");
  1224. refreshStartTimer(&startTime[TMR_IDX_REFRESH_CHARGING_INFO]);
  1225. getDateTimeString((char*)ShmDispenser->gun_info.StopDateTime);
  1226. }
  1227. }
  1228. break;
  1229. case SYS_MODE_TERMINATING:
  1230. if(isModeChange())
  1231. {
  1232. getDateTimeString((char*)ShmDispenser->gun_info.StopDateTime);
  1233. }
  1234. refreshStartTimer(&endChargingTime);
  1235. if(ShmDispenser->gun_info.PresentChargedDuration != 0)
  1236. {
  1237. ShmDispenser->gun_info.PresentChargedDuration = getDiffSecBetween(startChargingTime, endChargingTime);
  1238. }
  1239. // End authorize pass
  1240. if(!ShmDispenser->gun_info.legacyRequest.isLegacyRequest ||
  1241. ((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A)) ||
  1242. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && !ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn) ||
  1243. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  1244. {
  1245. if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS)
  1246. {
  1247. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1248. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1249. sleep(1);
  1250. setChargerMode(SYS_MODE_COMPLETE);
  1251. }
  1252. else if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC)
  1253. {
  1254. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  1255. //Cancel CCS task negotiating
  1256. ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
  1257. ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  1258. ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
  1259. }
  1260. else
  1261. {
  1262. setChargerMode(SYS_MODE_COMPLETE);
  1263. }
  1264. }
  1265. else
  1266. {
  1267. // Debug information
  1268. if(getDiffSecNow(startTime[TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_REFRESH_CHARGING_INFO)
  1269. {
  1270. DEBUG_INFO("==================================================\n");
  1271. DEBUG_INFO("gun_info.primaryMcuCp_Pwn_Duty.max_current: %d\n", ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current);
  1272. DEBUG_INFO("gun_info.targetCurrent: %d\n", ShmDispenser->gun_info.targetCurrent);
  1273. DEBUG_INFO("==================================================\n");
  1274. refreshStartTimer(&startTime[TMR_IDX_REFRESH_CHARGING_INFO]);
  1275. }
  1276. if(getDiffSecNow(startTime[TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  1277. {
  1278. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = (ShmDispenser->gun_info.targetCurrent==0?CCS_PWM_DUTY_100:ShmDispenser->gun_info.targetCurrent);
  1279. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
  1280. refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
  1281. }
  1282. if(ShmDispenser->gun_info.legacyRequest.isLegacyRequest &&
  1283. (((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)) &&
  1284. (((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_START_MODE)) || (ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS))
  1285. )
  1286. {
  1287. setChargerMode(SYS_MODE_CHARGING);
  1288. }
  1289. }
  1290. if((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) &&
  1291. (ShmDispenser->gun_info.isCCSWaitChangeDuty == ON) &&
  1292. (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  1293. {
  1294. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  1295. ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1296. ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
  1297. ShmDispenser->gun_info.isCCSWaitChangeDuty = OFF;
  1298. setChargerMode( SYS_MODE_COMPLETE);
  1299. }
  1300. break;
  1301. case SYS_MODE_COMPLETE:
  1302. if(isModeChange())
  1303. {}
  1304. if(((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A)) ||
  1305. ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && (!ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn)))
  1306. {
  1307. setChargerMode(SYS_MODE_IDLE);
  1308. }
  1309. break;
  1310. case SYS_MODE_ALARM:
  1311. if((ShmDispenser->gun_info.systemAlarmCode.SystemAlarmCode == 0))
  1312. {
  1313. if((ShmDispenser->gun_info.PreviousSystemStatus == SYS_MODE_CHARGING) ||
  1314. (ShmDispenser->gun_info.PreviousSystemStatus == SYS_MODE_TERMINATING))
  1315. {
  1316. setChargerMode(ShmDispenser->gun_info.PreviousSystemStatus);
  1317. }
  1318. else
  1319. {
  1320. setChargerMode(SYS_MODE_IDLE);
  1321. }
  1322. }
  1323. break;
  1324. case SYS_MODE_FAULT:
  1325. if(isModeChange())
  1326. {}
  1327. break;
  1328. case SYS_MODE_MAINTAIN:
  1329. if(isModeChange())
  1330. {}
  1331. if(ShmDispenser->gun_info.isOperactive)
  1332. {
  1333. DEBUG_INFO("Connector change to operactive.\n");
  1334. setChargerMode(SYS_MODE_IDLE);
  1335. }
  1336. break;
  1337. case SYS_MODE_UPDATE:
  1338. if(isModeChange())
  1339. {}
  1340. break;
  1341. case SYS_MODE_DEBUG:
  1342. if(isModeChange())
  1343. {}
  1344. break;
  1345. }
  1346. usleep(100000);
  1347. }
  1348. return -1;
  1349. }