Module_Dispenser.c 48 KB

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