main.c 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include <stdbool.h>
  30. #include <dirent.h>
  31. #include <signal.h>
  32. #include "../Config.h"
  33. #include "main.h"
  34. #include "../common.h"
  35. #include "../timeout.h"
  36. #include "../Log/log.h"
  37. #include "../DataBase/DataBase.h"
  38. #include "../Define/define.h"
  39. #include "../ShareMemory/shmMem.h"
  40. #include "../SelectGun/SelectGun.h"
  41. //------------------------------------------------------------------------------
  42. static struct SysInfoData *pSysInfo = NULL;
  43. static struct SysConfigData *pSysConfig = NULL;
  44. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  45. static struct AlarmCodeData *pAlarmCode = NULL;
  46. static struct FaultCodeData *pFaultCode = NULL;
  47. static struct InfoCodeData *pInfoCode = NULL;
  48. static struct PsuData *ShmPsuData = NULL;
  49. static struct CHAdeMOData *ShmCHAdeMOData = NULL;
  50. static struct GBTData *ShmGBTData = NULL;
  51. static struct CcsData *ShmCcsData = NULL;
  52. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  53. static struct FanModuleData *ShmFanModuleData = NULL;
  54. static struct RelayModuleData *ShmRelayModuleData = NULL;
  55. static struct LedModuleData *ShmLedModuleData = NULL;
  56. static struct OCPP16Data *ShmOCPP16Data = NULL;
  57. static DcCommonInfo *ShmDcCommonData = NULL;
  58. static struct ChargingInfoData *pDcChargingInfo = NULL;
  59. static struct ChargingInfoData *pAcChargingInfo = NULL;
  60. static struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  61. static struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  62. static SelectGunInfo *ShmSelectGunInfo = NULL; //Jerry add
  63. static EvBoardErrMsg gEvBoardErr = {0};
  64. static ChillerTempErr gChillerTempErr = {0};
  65. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  66. struct StatusCodeData *ShmStatusCodeData;
  67. // for initial index to check EV board type is correct
  68. uint8_t bd0_1_status = 0;
  69. uint8_t bd0_2_status = 0;
  70. uint8_t bd1_1_status = 0;
  71. uint8_t bd1_2_status = 0;
  72. char *fwVersion = "V2.00.00.0000.00"; // Phihong version
  73. char* DebugVersion = "v2.00.00"; // Software debug version
  74. //sqlite3 *localDb;
  75. bool isDb_ready;
  76. //------------------------------------------------------------------------------
  77. void ClearDetectPluginFlag();
  78. long long DiffTimebWithNow(struct timeb ST);
  79. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit);
  80. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value);
  81. unsigned long GetTimeoutValue(struct timeval _sour_time);
  82. void GetClockTime(struct timespec *_now_time, void *null);
  83. unsigned long GetClockTimeoutValue(struct timespec _start_time);
  84. void gpio_set_value(unsigned int gpio, unsigned int value);
  85. void InformOcppErrOccur(uint8_t codeType);
  86. void RecordAlarmCode(uint8_t gunIndex, char *code);
  87. void ReleaseAlarmCode(uint8_t gunIndex);
  88. void ResetChargerAlarmCode(uint8_t gunIndex, char *code);
  89. void AdjustChargerCurrent(void);
  90. void UpdateErrorCodeToOcpp(uint8_t index);
  91. //------------------------------------------------------------------------------
  92. //Primary.c
  93. extern void ChkPrimaryStatus(void);
  94. //RFID.c
  95. extern void CreateRfidFork(void);
  96. extern void ScannerCardProcess(void);
  97. extern bool RfidStopCharging(void);
  98. extern bool GetIsCardScan(void);
  99. extern void SetIsCardScan(bool value);
  100. //SelfTest.c
  101. extern void SelfTestRun(void);
  102. //UpgradeFW.c
  103. /*extern void CheckFwUpdateFunction(void);*/
  104. //Ethernet.c
  105. extern void InitEthernet(void);
  106. extern void GetMacAddress(void);
  107. //WatchDog.c
  108. extern void CreateWatchdog(void);
  109. extern void TryCloseWatchdog(void);
  110. extern void TryFeedWatchdog(void);
  111. //ZipFile.c
  112. extern void zipLogFiles(void);
  113. //------------------------------------------------------------------------------
  114. //--- share memory value ---
  115. //------------------------------------------------------------------------------
  116. static void changeLcmPage(uint8_t index)
  117. {
  118. pSysInfo->SystemPage = index;
  119. }
  120. static uint8_t getCurLcmPage(void)
  121. {
  122. return pSysInfo->SystemPage;
  123. }
  124. static void systemPageRestoreInit(void)
  125. {
  126. pSysInfo->SystemPage = _LCM_VIEW;
  127. }
  128. //------------------------------------------------------------------------------
  129. void destroySelGun(uint8_t curGun)
  130. {
  131. uint8_t i = 0;
  132. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  133. //for status timeout
  134. if (curGun == DESTROY_ALL_SEL) {
  135. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  136. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  137. log_info("destroy all gun = %d, %d",
  138. ShmSelectGunInfo->SelGunInfo.LeftGun,
  139. ShmSelectGunInfo->SelGunInfo.RightGun);
  140. for (i = 0; i < totalGun; i++) {
  141. StopGunInfoTimeoutDet(i);
  142. memset(&ShmSelectGunInfo->PricesInfo[i], 0, sizeof(PricesInfo));
  143. }
  144. pSysInfo->CurGunSelected = 0;
  145. strcpy((char *)pSysConfig->UserId, "");
  146. //changeLcmPage(_LCM_VIEW);
  147. return;
  148. }
  149. //for charging timeout or complete
  150. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  151. if (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  152. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  153. changeLcmPage(_LCM_VIEW);
  154. }
  155. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  156. StopGunInfoTimeoutDet(LEFT_GUN_NUM);
  157. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  158. ClearAuthorizedFlag();
  159. }
  160. log_info("destroy left gun, cur page = %d", getCurLcmPage());
  161. if (getCurLcmPage() == pSysInfo->SystemPage) {
  162. log_info("clear left balance");
  163. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  164. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  165. }
  166. }
  167. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  168. if (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  169. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  170. changeLcmPage(_LCM_VIEW);
  171. }
  172. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  173. StopGunInfoTimeoutDet(RIGHT_GUN_NUM);
  174. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  175. ClearAuthorizedFlag();
  176. }
  177. log_info("destroy right gun, cur page = %d", getCurLcmPage());
  178. if (getCurLcmPage() == pSysInfo->SystemPage) {
  179. log_info("clear right balance");
  180. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  181. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  182. }
  183. }
  184. }
  185. static int waitRightGunPlugIt(uint8_t curGun)
  186. {
  187. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  188. return PASS;
  189. }
  190. return FAIL;
  191. }
  192. static int waitLeftGunPlugIt(uint8_t curGun)
  193. {
  194. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  195. return PASS;
  196. }
  197. return FAIL;
  198. }
  199. void setSelGunWaitToAuthor(uint8_t curSel)
  200. {
  201. if (curSel == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM) {
  202. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_ATHOR;
  203. //printf("setSelGunWaitToAuthor left");
  204. StopGunInfoTimeoutDet(curSel);
  205. } else if (curSel == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM) {
  206. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_ATHOR;
  207. //printf("setSelGunWaitToAuthor right");
  208. StopGunInfoTimeoutDet(curSel);
  209. }
  210. }
  211. int getConfirmSelectedGun(uint8_t curSel)
  212. {
  213. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  214. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  215. return PASS;
  216. }
  217. return FAIL;
  218. }
  219. void confirmSelGun(uint8_t selGun)
  220. {
  221. if (selGun == LEFT_GUN_NUM) {
  222. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  223. //printf("confirmSelGun left");
  224. } else if (selGun == RIGHT_GUN_NUM) {
  225. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  226. //printf("confirmSelGun right");
  227. }
  228. changeLcmPage(_LCM_START_SCAN);
  229. //StartGunInfoTimeoutDet(selGun, Timeout_SelectGun);
  230. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  231. }
  232. static void GetFirmwareVersion(void)
  233. {
  234. // Get CSU root file system version
  235. sprintf((char *)pSysInfo->CsuRootFsFwRev, fwVersion);
  236. uint8_t count = 0, chademo = 0, ccs = 0, gb = 0;
  237. for (uint8_t idx = 0; idx < 3; idx++) {
  238. if (pSysConfig->ModelName[7 + idx] == 'J') {
  239. chademo++;
  240. count++;
  241. } else if (pSysConfig->ModelName[7 + idx] == 'G') {
  242. gb++;
  243. count++;
  244. } else if (pSysConfig->ModelName[7 + idx] == 'U' ||
  245. pSysConfig->ModelName[7 + idx] == 'V' ||
  246. pSysConfig->ModelName[7 + idx] == 'E') {
  247. ccs++;
  248. count++;
  249. }
  250. }
  251. if (count == 1) {
  252. if (chademo > 0) {
  253. pSysInfo->CsuRootFsFwRev[7] = '1';
  254. } else if (ccs > 0) {
  255. pSysInfo->CsuRootFsFwRev[7] = '2';
  256. } else if (gb > 0) {
  257. pSysInfo->CsuRootFsFwRev[7] = '3';
  258. }
  259. } else {
  260. if (chademo > 0 && ccs > 0) {
  261. pSysInfo->CsuRootFsFwRev[7] = '4';
  262. } else if (chademo > 0 && gb > 0) {
  263. pSysInfo->CsuRootFsFwRev[7] = '5';
  264. } else if (ccs > 0 && gb > 0) {
  265. pSysInfo->CsuRootFsFwRev[7] = '6';
  266. }
  267. }
  268. // Get network option from model name
  269. switch (pSysConfig->ModelName[10]) {
  270. case 'B':
  271. case 'U':
  272. //Blue tooth
  273. pSysInfo->CsuRootFsFwRev[9] = '3';
  274. break;
  275. case 'W':
  276. // WIFI
  277. pSysInfo->CsuRootFsFwRev[9] = '1';
  278. break;
  279. case 'T':
  280. // 3G/4G
  281. pSysInfo->CsuRootFsFwRev[9] = '2';
  282. break;
  283. case 'D': //DS60-120 add
  284. pSysInfo->CsuRootFsFwRev[9] = '5';
  285. break;
  286. default:
  287. // LAN
  288. pSysInfo->CsuRootFsFwRev[9] = '0';
  289. break;
  290. }
  291. // Get rating power from model name
  292. memcpy(&pSysInfo->CsuRootFsFwRev[10], &pSysConfig->ModelName[4], 0x03);
  293. // Get IEC or UL
  294. char _buf[3] = {0};
  295. memcpy(_buf, &pSysConfig->ModelName[2], 2);
  296. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL) {
  297. pSysInfo->ChargerType = _CHARGER_TYPE_IEC;
  298. log_info("IEC model");
  299. } else if (strcmp(_buf, "WU") == EQUAL) {
  300. pSysInfo->ChargerType = _CHARGER_TYPE_UL;
  301. log_info("UL model");
  302. }
  303. }
  304. static void checkGunOTPState(uint8_t gunIndex)
  305. {
  306. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  307. //水冷機溫度檢測
  308. if (((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x80) >> 7) == YES) {
  309. if (pDcChargingInfo->ChillerTemp != UNDEFINED_TEMP) {
  310. if (pDcChargingInfo->ChillerTemp >= GUN_OTP_VALUE) {
  311. if (((gunIndex == 0) &&
  312. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  313. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  314. ||
  315. ((gunIndex == 1) &&
  316. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  317. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  318. ) {
  319. RecordAlarmCode(gunIndex, "012323");
  320. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = YES;
  321. }
  322. else
  323. {
  324. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  325. }
  326. } else if (pDcChargingInfo->ChillerTemp != 0 &&
  327. pDcChargingInfo->ChillerTemp < GUN_OTP_RECOVERY) {
  328. //ResetChargerAlarmCode(gunIndex, "012323");
  329. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  330. }
  331. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  332. } else {
  333. // 沒接上 Sensor or 異常
  334. //RecordAlarmCode(gunIndex, "011038");
  335. //ResetChargerAlarmCode(gunIndex, "012323");
  336. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  337. if (((gunIndex == 0) &&
  338. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  339. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  340. ||
  341. ((gunIndex == 1) &&
  342. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  343. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  344. ) {
  345. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = YES;
  346. }
  347. else
  348. {
  349. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  350. }
  351. }
  352. if(pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault == YES)
  353. {
  354. RecordAlarmCode(gunIndex, "011037");
  355. }
  356. }
  357. switch (pDcChargingInfo->Type) {
  358. case _Type_Chademo:
  359. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  360. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  361. RecordAlarmCode(gunIndex, "012229");
  362. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  363. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  364. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  365. //ResetChargerAlarmCode(gunIndex, "012229");
  366. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  367. }
  368. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  369. } else {
  370. // 沒接上 Sensor or 異常
  371. //RecordAlarmCode(gunIndex, "011018");
  372. //ResetChargerAlarmCode(gunIndex, "012229");
  373. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  374. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  375. }
  376. break;
  377. case _Type_CCS_2:
  378. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  379. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  380. //ResetChargerAlarmCode(gunIndex, "011019");
  381. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  382. RecordAlarmCode(gunIndex, "012230");
  383. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  384. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  385. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  386. //ResetChargerAlarmCode(gunIndex, "012230");
  387. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  388. }
  389. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  390. //ResetChargerAlarmCode(gunIndex, "011019");
  391. } else {
  392. // 沒接上 Sensor or 異常
  393. //RecordAlarmCode(gunIndex, "011019");
  394. //ResetChargerAlarmCode(gunIndex, "012230");
  395. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  396. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  397. }
  398. break;
  399. case _Type_GB:
  400. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  401. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  402. RecordAlarmCode(gunIndex, "012231");
  403. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  404. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  405. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  406. //ResetChargerAlarmCode(gunIndex, "012231");
  407. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  408. }
  409. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  410. } else {
  411. // 沒接上 Sensor or 異常
  412. //RecordAlarmCode(gunIndex, "011020");
  413. //ResetChargerAlarmCode(gunIndex, "012231");
  414. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  415. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  416. }
  417. break;
  418. }
  419. }
  420. static void collectError(uint8_t gunIndex)
  421. {
  422. gEvBoardErr.GunErrMessage |= ShmDcCommonData->ConnectErrList[gunIndex].GunErrMessage;
  423. gChillerTempErr.TempErrMsg |= ShmDcCommonData->ChillerTempErr[gunIndex].TempErrMsg;
  424. }
  425. static void checkGBTAlarmState(uint8_t gunType)
  426. {
  427. // GFD Trip
  428. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  429. pAlarmCode->AlarmEvents.bits.GbGfdTrip = YES;
  430. } else {
  431. pAlarmCode->AlarmEvents.bits.GbGfdTrip = NO;
  432. }
  433. // UVP
  434. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  435. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = YES;
  436. } else {
  437. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = NO;
  438. }
  439. // OTP
  440. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  441. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = YES;
  442. } else {
  443. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = NO;
  444. }
  445. // OVP
  446. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  447. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = YES;
  448. } else {
  449. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  450. }
  451. // GFD Warning
  452. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  453. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = YES;
  454. } else {
  455. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = NO;
  456. }
  457. // Relay Welding
  458. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  459. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = YES;
  460. } else {
  461. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = NO;
  462. }
  463. // Relay Driving
  464. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  465. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  466. } else {
  467. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  468. }
  469. // Connect temp Sensor broken
  470. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  471. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  472. } else {
  473. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  474. }
  475. }
  476. static void checkCCSAlarmState(uint8_t gunType)
  477. {
  478. // GFD Trip
  479. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  480. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = YES;
  481. } else {
  482. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = NO;
  483. }
  484. // UVP
  485. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  486. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = YES;
  487. } else {
  488. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = NO;
  489. }
  490. // OTP
  491. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  492. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = YES;
  493. } else {
  494. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = NO;
  495. }
  496. // OVP
  497. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  498. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = YES;
  499. } else {
  500. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  501. }
  502. // GFD Warning
  503. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  504. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = YES;
  505. } else {
  506. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = NO;
  507. }
  508. // Relay Welding
  509. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  510. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = YES;
  511. } else {
  512. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = NO;
  513. }
  514. // Relay Driving
  515. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  516. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  517. } else {
  518. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  519. }
  520. // Connect temp Sensor broken
  521. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  522. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  523. } else {
  524. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  525. }
  526. }
  527. static void checkChaDeMoAlarmState(uint8_t gunType)
  528. {
  529. // GFD Trip
  530. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  531. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = YES;
  532. } else {
  533. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = NO;
  534. }
  535. // UVP
  536. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  537. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = YES;
  538. } else {
  539. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = NO;
  540. }
  541. // OTP
  542. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  543. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = YES;
  544. } else {
  545. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = NO;
  546. }
  547. // OVP
  548. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  549. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = YES;
  550. } else {
  551. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  552. }
  553. // GFD Warning
  554. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  555. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = YES;
  556. } else {
  557. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = NO;
  558. }
  559. // Relay Welding
  560. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  561. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = YES;
  562. } else {
  563. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = NO;
  564. }
  565. // Relay Driving
  566. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  567. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  568. } else {
  569. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  570. }
  571. // Connect temp Sensor broken
  572. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  573. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  574. } else {
  575. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  576. }
  577. }
  578. static void checkChillerAlarmState(void)
  579. {
  580. if (DetectBitValue(gChillerTempErr.TempErrMsg, 0)) {
  581. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = YES;
  582. } else {
  583. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = NO;
  584. }
  585. if (DetectBitValue(gChillerTempErr.TempErrMsg, 1)) {
  586. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = YES;
  587. } else {
  588. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = NO;
  589. }
  590. }
  591. static void checkEvBoardAlarmState(uint8_t gunType)
  592. {
  593. switch (gunType) {
  594. case _Type_Chademo:
  595. checkChaDeMoAlarmState(gunType);
  596. break;
  597. case _Type_CCS_2:
  598. checkCCSAlarmState(gunType);
  599. break;
  600. case _Type_GB:
  601. checkGBTAlarmState(gunType);
  602. break;
  603. }
  604. }
  605. unsigned long GetTimeoutValue(struct timeval _sour_time)
  606. {
  607. struct timeval _end_time;
  608. gettimeofday(&_end_time, NULL);
  609. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  610. }
  611. void GetClockTime(struct timespec *_now_time, void *null)
  612. {
  613. clock_gettime(CLOCK_MONOTONIC, _now_time);
  614. }
  615. // return value unit: 1us
  616. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  617. {
  618. struct timespec ts_end;
  619. unsigned long ret = 0;
  620. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  621. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec/ 1000)));
  622. return ret;
  623. }
  624. int DiffTimeb(struct timeb ST, struct timeb ET)
  625. {
  626. //return milli-second
  627. unsigned int StartTime, StopTime;
  628. StartTime = (unsigned int)ST.time;
  629. StopTime = (unsigned int)ET.time;
  630. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  631. return (StopTime - StartTime);
  632. }
  633. bool CheckTimeOut(struct timeb ST)
  634. {
  635. struct timeb ET;
  636. unsigned int StartTime, StopTime;
  637. ftime(&ET);
  638. StartTime = (unsigned int) ST.time;
  639. StopTime = (unsigned int) ET.time;
  640. return (StopTime > StartTime) ? YES : NO;
  641. }
  642. void setChargerMode(uint8_t gunIndex, uint8_t mode)
  643. {
  644. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  645. pDcChargingInfo->SystemStatus = mode;
  646. }
  647. long long DiffTimebWithNow(struct timeb ST)
  648. {
  649. //return milli-second
  650. struct timeb ET;
  651. long long StartTime, StopTime;
  652. ftime(&ET);
  653. StartTime = (long long)ST.time;
  654. StopTime = (long long)ET.time;
  655. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  656. }
  657. //==========================================
  658. // Log
  659. //==========================================
  660. void CheckFwSlotStatusLog(void)
  661. {
  662. if (bd0_1_status == 0 && bd0_2_status == 1) {
  663. log_info("Connector 1 : Chademo");
  664. } else if (bd0_1_status == 1 && bd0_2_status == 0) {
  665. log_info("Connector 1 : CCS");
  666. } else if (bd0_1_status == 1 && bd0_2_status == 1) {
  667. log_info("Connector 1 : GB");
  668. }
  669. if (bd1_1_status == 0 && bd1_2_status == 1) {
  670. log_info("Connector 2 : Chademo");
  671. } else if (bd1_1_status == 1 && bd1_2_status == 0) {
  672. log_info("Connector 2 : CCS");
  673. } else if (bd1_1_status == 1 && bd1_2_status == 1) {
  674. log_info("Connector 2 : GB");
  675. }
  676. }
  677. void CheckHwSlotStatusLog(uint8_t index)
  678. {
  679. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  680. if (pDcChargingInfo->Type == _Type_Chademo) {
  681. log_info("Hw check : Connector %d, Type : Chademo, Evboard_id = %d ",
  682. index, pDcChargingInfo->Evboard_id);
  683. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  684. log_info("Hw check : Connector %d, Type : CCS, Evboard_id = %d ",
  685. index, pDcChargingInfo->Evboard_id);
  686. } else if (pDcChargingInfo->Type == _Type_GB) {
  687. log_info("Hw check : Connector %d, Type : GB, Evboard_id = %d ",
  688. index, pDcChargingInfo->Evboard_id);
  689. }
  690. }
  691. //=================================
  692. // LCM Page
  693. //=================================
  694. void ChangeLcmByIndex(uint8_t page_index)
  695. {
  696. if (pSysWarning->Level != WARN_LV_ER ||
  697. page_index == _LCM_SUMMARY ||
  698. page_index == _LCM_ERROR
  699. ) {
  700. pSysInfo->PageIndex = page_index;
  701. //log_info("LCM index:%d",pSysInfo->PageIndex);
  702. }
  703. }
  704. //======================================================
  705. // Peripheral initial
  706. //======================================================
  707. void InitGPIO()
  708. {
  709. /*****************0~3, 4 bank, bank x 32+ num*********************/
  710. /***************************************************************/
  711. /*************** GPIO 0 ***************************************/
  712. /***************************************************************/
  713. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  714. system("echo 22 > /sys/class/gpio/export");
  715. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  716. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  717. system("echo 23 > /sys/class/gpio/export");
  718. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  719. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  720. system("echo 26 > /sys/class/gpio/export");
  721. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  722. system("echo 1 > /sys/class/gpio/gpio26/value");
  723. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  724. system("echo 27 > /sys/class/gpio/export");
  725. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  726. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  727. system("echo 29 > /sys/class/gpio/export");
  728. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  729. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  730. system("echo 19 > /sys/class/gpio/export");
  731. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  732. system("echo 1 > /sys/class/gpio/gpio19/value");
  733. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  734. system("echo 20 > /sys/class/gpio/export");
  735. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  736. /***************************************************************/
  737. /*************** GPIO 1 ***************************************/
  738. /***************************************************************/
  739. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  740. system("echo 44 > /sys/class/gpio/export");
  741. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  742. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  743. system("echo 45 > /sys/class/gpio/export");
  744. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  745. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  746. system("echo 46 > /sys/class/gpio/export");
  747. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  748. system("echo 0 > /sys/class/gpio/gpio46/value");
  749. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  750. system("echo 47 > /sys/class/gpio/export");
  751. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  752. /***************************************************************/
  753. /*************** GPIO 2 ***************************************/
  754. /***************************************************************/
  755. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  756. system("echo 89 > /sys/class/gpio/export");
  757. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  758. system("echo 1 > /sys/class/gpio/gpio89/value");
  759. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  760. system("echo 87 > /sys/class/gpio/export");
  761. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  762. system("echo 0 > /sys/class/gpio/gpio87/value");
  763. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  764. system("echo 88 > /sys/class/gpio/export");
  765. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  766. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  767. system("echo 86 > /sys/class/gpio/export");
  768. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  769. /***************************************************************/
  770. /*************** GPIO 3 ***************************************/
  771. /***************************************************************/
  772. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  773. system("echo 111 > /sys/class/gpio/export");
  774. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  775. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  776. system("echo 114 > /sys/class/gpio/export");
  777. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  778. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  779. system("echo 113 > /sys/class/gpio/export");
  780. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  781. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  782. system("echo 110 > /sys/class/gpio/export");
  783. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  784. system("echo 0 > /sys/class/gpio/gpio110/value");
  785. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  786. system("echo 115 > /sys/class/gpio/export");
  787. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  788. system("echo 0 > /sys/class/gpio/gpio115/value");
  789. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  790. system("echo 112 > /sys/class/gpio/export");
  791. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  792. system("echo 1 > /sys/class/gpio/gpio112/value");
  793. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  794. system("echo 116 > /sys/class/gpio/export");
  795. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  796. system("echo 0 > /sys/class/gpio/gpio116/value");
  797. log_info("Initial GPIO OK");
  798. return ;
  799. }
  800. int LoadSysConfigAndInfo()
  801. {
  802. int fd, wrd;
  803. unsigned char *buf;
  804. unsigned int ChkSum, ChkSumOrg;
  805. if ((buf = malloc(MtdBlockSize)) == NULL) {
  806. log_error("malloc buffer NG,rebooting..");
  807. if (pAlarmCode != NULL) {
  808. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  809. }
  810. sleep(5);
  811. system("reboot -f");
  812. sleep(5);
  813. system("reboot -f");
  814. }
  815. memset(buf, 0, MtdBlockSize);
  816. //================================================
  817. // Load configuration from mtdblock10
  818. //================================================
  819. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  820. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  821. if (fd < 0) {
  822. free(buf);
  823. log_error("open mtdblock10 NG,rebooting..");
  824. if (pAlarmCode != NULL) {
  825. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  826. }
  827. sleep(5);
  828. system("reboot -f");
  829. sleep(5);
  830. system("reboot -f");
  831. }
  832. wrd = read(fd, buf, MtdBlockSize);
  833. close(fd);
  834. if (wrd < MtdBlockSize) {
  835. free(buf);
  836. log_error("read SysConfigData data NG,rebooting..");
  837. if (pAlarmCode != NULL) {
  838. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  839. }
  840. sleep(5);
  841. system("reboot -f");
  842. sleep(5);
  843. system("reboot -f");
  844. }
  845. ChkSum = 0;
  846. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  847. ChkSum += buf[wrd];
  848. }
  849. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  850. memcpy(&pSysConfig->ModelName, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), ARRAY_SIZE(pSysConfig->ModelName));
  851. memcpy(&pSysConfig->SerialNumber, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), ARRAY_SIZE(pSysConfig->SerialNumber));
  852. //================================================
  853. // Load configuration from mtdblock11
  854. //================================================
  855. if (ChkSum != ChkSumOrg) {
  856. log_error("Primary SysConfigData checksum NG, read backup");
  857. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  858. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  859. if (fd < 0) {
  860. free(buf);
  861. log_error("open mtdblock11 (backup) NG,rebooting..");
  862. if (pAlarmCode != NULL) {
  863. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  864. }
  865. sleep(5);
  866. system("reboot -f");
  867. sleep(5);
  868. system("reboot -f");
  869. }
  870. memset(buf, 0, MtdBlockSize);
  871. wrd = read(fd, buf, MtdBlockSize);
  872. close(fd);
  873. if (wrd < MtdBlockSize) {
  874. free(buf);
  875. log_error("read backup SysConfigData data NG,rebooting..");
  876. if (pAlarmCode != NULL) {
  877. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  878. }
  879. sleep(5);
  880. system("reboot -f");
  881. sleep(5);
  882. system("reboot -f");
  883. }
  884. ChkSum = 0;
  885. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  886. ChkSum += buf[wrd];
  887. }
  888. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  889. //================================================
  890. // Load configuration from mtdblock12 (Factory default)
  891. //================================================
  892. if (ChkSum != ChkSumOrg) {
  893. log_warn("backup SysConfigData checksum NG, read Factory default");
  894. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  895. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  896. if (fd < 0) {
  897. log_error("open mtdblock12 (Factory default) NG,rebooting..");
  898. free(buf);
  899. if (pAlarmCode != NULL) {
  900. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  901. }
  902. sleep(5);
  903. system("reboot -f");
  904. sleep(5);
  905. system("reboot -f");
  906. }
  907. memset(buf, 0, MtdBlockSize);
  908. wrd = read(fd, buf, MtdBlockSize);
  909. close(fd);
  910. if (wrd < MtdBlockSize) {
  911. log_error("read factory default SysConfigData data NG,rebooting..");
  912. free(buf);
  913. if (pAlarmCode != NULL) {
  914. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  915. }
  916. sleep(5);
  917. system("reboot -f");
  918. sleep(5);
  919. system("reboot -f");
  920. }
  921. ChkSum = 0;
  922. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  923. ChkSum += buf[wrd];
  924. }
  925. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  926. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), &pSysConfig->ModelName, ARRAY_SIZE(pSysConfig->ModelName));
  927. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), &pSysConfig->SerialNumber, ARRAY_SIZE(pSysConfig->SerialNumber));
  928. if (ChkSum != ChkSumOrg) {
  929. log_warn("factory default SysConfigData checksum NG, restore factory default");
  930. free(buf);
  931. system("cd /root;./FactoryConfig -m");
  932. system("rm -f /Storage/OCPP/OCPPConfiguration");
  933. system("sync");
  934. sleep(5);
  935. system("reboot -f");
  936. sleep(5);
  937. system("reboot -f");
  938. return FAIL;
  939. }
  940. }
  941. }
  942. //load OK
  943. memcpy(pSysConfig, buf, sizeof(struct SysConfigData));
  944. free(buf);
  945. system("rm -f /mnt/EvseConfig.bin");
  946. // SysConfig in flash is empty (0xffffffff)
  947. if ((strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) ||
  948. (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) ||
  949. (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) ||
  950. (pSysConfig->Eth0Interface.EthDhcpClient == 0xff)) {
  951. if (strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) {
  952. memset(pSysConfig->ModelName, 0x00, ARRAY_SIZE(pSysConfig->ModelName));
  953. }
  954. if (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) {
  955. memset(pSysConfig->SerialNumber, 0x00, ARRAY_SIZE(pSysConfig->SerialNumber));
  956. }
  957. if (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) {
  958. memset(pSysConfig->SystemId, 0x00, ARRAY_SIZE(pSysConfig->SystemId));
  959. }
  960. if (pSysConfig->Eth0Interface.EthDhcpClient == 0xff) {
  961. log_info("Ethernet dhcp config is null.");
  962. }
  963. if (strlen((char *)pSysConfig->ModelName) == 0x00) {
  964. log_info("Model name over length.");
  965. }
  966. if (strlen((char *)pSysConfig->SerialNumber) == 0x00) {
  967. log_info("Model serial number over length.");
  968. }
  969. if (strlen((char *)pSysConfig->SystemId) == 0x00) {
  970. log_info("SystemId over length.");
  971. }
  972. system("cd /root;./FactoryConfig -m");
  973. sleep(3);
  974. system("/usr/bin/run_evse_restart.sh");
  975. }
  976. log_info("Load SysConfigData OK");
  977. return PASS;
  978. }
  979. int Initialization(void)
  980. {
  981. uint8_t count = 0;
  982. uint8_t pinOut[2] = {116, 115};
  983. // 初始化卡號驗證的 Flag
  984. ClearAuthorizedFlag();
  985. // 初始化插槍驗證的 Flag
  986. ClearDetectPluginFlag();
  987. for (count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  988. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  989. pDcChargingInfo->RemoteStartFlag = NO;
  990. if (pDcChargingInfo->Type == _Type_Chademo) {
  991. gpio_set_value(pinOut[count], 0x00);
  992. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  993. } else if (pDcChargingInfo->Type == _Type_GB) {
  994. gpio_set_value(pinOut[count], 0x00);
  995. ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  996. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  997. //if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) { //DS60-120 remove
  998. if (pSysConfig->TotalConnectorCount == 1) {
  999. gpio_set_value(pinOut[1], 0x01);
  1000. } else {
  1001. gpio_set_value(pinOut[count], 0x01);
  1002. }
  1003. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1004. //}
  1005. }
  1006. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1007. }
  1008. for (count = 0; count < pSysConfig->AcConnectorCount; count++) {
  1009. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  1010. pAcChargingInfo->RemoteStartFlag = NO;
  1011. if (pAcChargingInfo->Type == _Type_AC) {
  1012. pAcChargingInfo->SelfTest_Comp = NO;
  1013. strcpy((char *)ShmOCPP16Data->StatusNotification[count + pSysConfig->TotalConnectorCount].ErrorCode, "NoError");
  1014. }
  1015. }
  1016. return PASS;
  1017. }
  1018. bool InitialSystemDefaultConfig()
  1019. {
  1020. bool result = true;
  1021. LoadSysConfigAndInfo();
  1022. InitGPIO();
  1023. InitEthernet();
  1024. GetMacAddress();
  1025. return result;
  1026. }
  1027. //顯示自檢錯誤原因,觸發對應的flag
  1028. bool DisplaySelfTestFailReason()
  1029. {
  1030. bool result = false;
  1031. uint8_t index = 0;
  1032. // RB、FB、407、EV 小板中有些板子無回應
  1033. if (ShmRelayModuleData->SelfTest_Comp == NO) {
  1034. pAlarmCode->AlarmEvents.bits.RelayboardStestFail = true;
  1035. }
  1036. if (ShmFanModuleData->SelfTest_Comp == NO) {
  1037. pAlarmCode->AlarmEvents.bits.FanboardStestFail = true;
  1038. }
  1039. if (ShmPrimaryMcuData->SelfTest_Comp == NO) {
  1040. pAlarmCode->AlarmEvents.bits.PrimaryStestFail = true;
  1041. }
  1042. if (ShmLedModuleData->SelfTest_Comp == NO) {
  1043. pAlarmCode->AlarmEvents.bits.LedboardStestFail = true;
  1044. }
  1045. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  1046. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1047. if (pDcChargingInfo->Type == _Type_Chademo) {
  1048. if (ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1049. pAlarmCode->AlarmEvents.bits.ChademoboardStestFail = true;
  1050. }
  1051. } else if (pDcChargingInfo->Type == _Type_GB) {
  1052. if (ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1053. pAlarmCode->AlarmEvents.bits.GbtboardStestFail = true;
  1054. }
  1055. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1056. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1057. if (ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1058. pAlarmCode->AlarmEvents.bits.CCSboardStestFail = true;
  1059. }
  1060. }
  1061. }
  1062. }
  1063. for (index = 0; index < pSysConfig->AcConnectorCount; index++) {
  1064. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(index);
  1065. // 先借 GBT 顯示
  1066. if (pAcChargingInfo->SelfTest_Comp == NO) {
  1067. pAlarmCode->AlarmEvents.bits.AcConnectorStestFail = true;
  1068. }
  1069. }
  1070. if (pSysInfo->AcContactorStatus == NO) {
  1071. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox &&!defined DD360UCar
  1072. // AC Contact 未搭上
  1073. pAlarmCode->AlarmEvents.bits.AcContactStestFail = true;
  1074. result = true;
  1075. #endif // !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  1076. }
  1077. //else if (pAlarmCode->AlarmEvents.bits.PsuDipSwitchStestFail == YES) { //DS60-120 add
  1078. // result = true;
  1079. //}
  1080. else if (ShmPsuData->SystemAvailablePower <= 0 &&
  1081. ShmPsuData->SystemAvailableCurrent <= 0) {
  1082. // PSU 通訊問題
  1083. pAlarmCode->AlarmEvents.bits.PsuModuleStestFail = true;
  1084. result = true;
  1085. }
  1086. return result;
  1087. }
  1088. //===============================================
  1089. // Common Detect Chk - Stop Charging ?
  1090. //===============================================
  1091. int isEvBoardStopChargeFlag(uint8_t gunIndex)
  1092. {
  1093. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1094. //printf("StopChargeFlag = %d", chargingInfo[gunIndex]->StopChargeFlag);
  1095. return pDcChargingInfo->StopChargeFlag;
  1096. }
  1097. bool isEvBoardNormalStopChargeFlag(uint8_t gunIndex)
  1098. {
  1099. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1100. return pDcChargingInfo->NormalStopChargeFlag;
  1101. }
  1102. //===============================================
  1103. // 掃描插槍狀況
  1104. //===============================================
  1105. void ClearDetectPluginFlag(void)
  1106. {
  1107. uint8_t gunIndex = 0;
  1108. pSysInfo->WaitForPlugit = NO;
  1109. //DS60-120 add
  1110. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1111. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1112. if (pDcChargingInfo->RemoteStartFlag == YES) {
  1113. pDcChargingInfo->RemoteStartFlag = NO;
  1114. }
  1115. }
  1116. if (pSysInfo->OrderCharging != NO_DEFINE) {
  1117. pSysInfo->OrderCharging = NO_DEFINE;
  1118. }
  1119. }
  1120. void DetectPluginStart()
  1121. {
  1122. pSysInfo->WaitForPlugit = YES;
  1123. }
  1124. bool isDetectPlugin()
  1125. {
  1126. if (pSysInfo->WaitForPlugit == YES) {
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. //===============================================
  1132. // Common Detect Chk - Chademo
  1133. //===============================================
  1134. bool isEvGunLocked_chademo(uint8_t gunIndex)
  1135. {
  1136. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1137. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1138. }
  1139. bool isEvContactorWelding_chademo(uint8_t gunIndex)
  1140. {
  1141. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1142. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 3);
  1143. }
  1144. bool isEvStopReq_chademo(uint8_t gunIndex)
  1145. {
  1146. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1147. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 4);
  1148. }
  1149. bool isEvStopCharging_chademo(uint8_t gunIndex)
  1150. {
  1151. if (isEvGunLocked_chademo(gunIndex) == NO) {
  1152. // 無鎖槍 = 停止
  1153. log_info("gun locked none (%d) ", gunIndex);
  1154. return YES;
  1155. }
  1156. return NO;
  1157. }
  1158. uint8_t isPrechargeStatus_chademo(uint8_t gunIndex)
  1159. {
  1160. uint8_t result = 0x00;
  1161. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1162. result = ShmCHAdeMOData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1163. return result;
  1164. }
  1165. //===============================================
  1166. // Common Detect Chk - GB
  1167. //===============================================
  1168. bool isEvGunLocked_gb(uint8_t gunIndex)
  1169. {
  1170. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1171. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1172. }
  1173. bool isEvStopCharging_gb(uint8_t gunIndex)
  1174. {
  1175. if (isEvGunLocked_gb(gunIndex) == NO) {
  1176. // 無鎖槍 = 停止
  1177. log_info("gun locked none. ");
  1178. return YES;
  1179. }
  1180. return NO;
  1181. }
  1182. uint8_t isPrechargeStatus_gb(uint8_t gunIndex)
  1183. {
  1184. uint8_t result = 0x00;
  1185. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1186. result = ShmGBTData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1187. return result;
  1188. }
  1189. //===============================================
  1190. // Common Detect Chk - CCS
  1191. //===============================================
  1192. bool isEvGunLocked_ccs(uint8_t gunIndex)
  1193. {
  1194. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1195. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1196. }
  1197. uint8_t isPrechargeStatus_ccs(uint8_t gunIndex)
  1198. {
  1199. uint8_t result = 0x00;
  1200. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1201. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1202. result = ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1203. }
  1204. return result;
  1205. }
  1206. bool isEvStopCharging_ccs(uint8_t gunIndex)
  1207. {
  1208. if (isEvGunLocked_ccs(gunIndex) == NO) {
  1209. // 無鎖槍 = 停止
  1210. log_info("gun locked none. ");
  1211. return YES;
  1212. }
  1213. return NO;
  1214. }
  1215. //===============================================
  1216. // Callback
  1217. //===============================================
  1218. void DisplayChargingInfo()
  1219. {
  1220. uint8_t i = 0;
  1221. log_info("*********** DisplayChargingInfo *********** ");
  1222. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1223. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1224. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1225. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  1226. ChangeGunSelectByIndex(i);
  1227. return;
  1228. }
  1229. }
  1230. if (pSysConfig->AcConnectorCount > 0 &&
  1231. pSysInfo->CurGunSelectedByAc == NO_DEFINE)
  1232. {
  1233. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(0);
  1234. if (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  1235. pAcChargingInfo->SystemStatus <= S_COMPLETE)
  1236. {
  1237. pSysInfo->CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1238. }
  1239. }
  1240. usleep(50000);
  1241. systemPageRestoreInit();
  1242. }
  1243. void _AutoReturnTimeout(void)
  1244. {
  1245. log_info("*********** _AutoReturnTimeout(%d) *********** ", pSysInfo->PageIndex);
  1246. if (pSysInfo->PageIndex == _LCM_PRECHARGE) {
  1247. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  1248. pDcChargingInfo->SystemStatus = S_ALARM;
  1249. //pSysInfo->SystemPage = _LCM_ERROR;
  1250. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1251. ClearDetectPluginFlag();
  1252. } else if (pSysInfo->SystemPage ==_LCM_START_AUTHORIZE_FAIL) {
  1253. pSysInfo->SystemPage = _LCM_START_SCAN;
  1254. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1255. strcpy((char *)pSysConfig->UserId, "");
  1256. setChargerMode(pSysInfo->CurGunSelected,MODE_AUTHORIZING);
  1257. }
  1258. }
  1259. void _SelfTestTimeout(void)
  1260. {
  1261. if (pSysInfo->BootingStatus != BOOT_COMPLETE) {
  1262. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1263. setChargerMode(gunIndex, MODE_ALARM);
  1264. }
  1265. }
  1266. ShmPsuData->Work_Step = _NO_WORKING;
  1267. pSysInfo->SelfTestSeq = _STEST_FAIL;
  1268. log_info("Self test timeout. ");
  1269. }
  1270. void _AuthorizedTimeout(void)
  1271. {
  1272. //if (IsAuthorizingMode()) {
  1273. log_info("*********** _AuthorizedTimeout *********** ");
  1274. StopSystemTimeoutDet();
  1275. //isCardScan = false;
  1276. SetIsCardScan(false);
  1277. pSysInfo->SystemPage = _LCM_START_AUTHORIZE_FAIL;
  1278. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1279. strcpy((char *)pSysConfig->UserId, "");
  1280. ClearAuthorizedFlag();
  1281. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1282. //}
  1283. }
  1284. void _DetectPlugInTimeout(void)
  1285. {
  1286. log_info("*********** _DetectPlugInTimeout *********** ");
  1287. strcpy((char *)pSysConfig->UserId, "");
  1288. StopSystemTimeoutDet();
  1289. pSysInfo->SystemPage = _LCM_LINK_ERROR;
  1290. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1291. ClearDetectPluginFlag();
  1292. //setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1293. //systemPageRestoreInit();
  1294. }
  1295. void _DetectEvChargingEnableTimeout(uint8_t gunIndex)
  1296. {
  1297. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1298. if (pDcChargingInfo->Type == _Type_Chademo) {
  1299. if (!isEvGunLocked_chademo(gunIndex)) {
  1300. log_info("*********** _DetectEvChargingEnableTimeout (chademo) ***********");
  1301. }
  1302. } else if (pDcChargingInfo->Type == _Type_GB) {
  1303. if (!isEvGunLocked_ccs(gunIndex)) {
  1304. log_info("*********** _DetectEvChargingEnableTimeout (gb) ***********");
  1305. }
  1306. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1307. if (!isEvGunLocked_ccs(gunIndex)) {
  1308. log_info("*********** _DetectEvChargingEnableTimeout (ccs) ***********");
  1309. }
  1310. }
  1311. //pSysInfo->SystemPage = _LCM_ERROR;
  1312. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1313. ChargingTerminalProcess(gunIndex);
  1314. _AutoReturnTimeout();
  1315. }
  1316. void _DetectEvseChargingEnableTimeout(uint8_t gunIndex)
  1317. {
  1318. log_info("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********");
  1319. setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1320. //pSysInfo->SystemPage = _LCM_ERROR;
  1321. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1322. _AutoReturnTimeout();
  1323. }
  1324. void _PrepareTimeout(uint8_t gunIndex)
  1325. {
  1326. log_info("*********** _PrepareTimeout ***********");
  1327. setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1328. pAlarmCode->AlarmEvents.bits.PsuNoResource = YES;
  1329. //pSysInfo->SystemPage = _LCM_ERROR;
  1330. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1331. _AutoReturnTimeout();
  1332. }
  1333. void _CcsPrechargeTimeout(uint8_t gunIndex)
  1334. {
  1335. log_info("*********** _CcsPrechargeTimeout ***********");
  1336. setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1337. //pSysInfo->SystemPage = _LCM_ERROR;
  1338. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1339. }
  1340. void _LinkErrorTimeout(uint8_t gunIndex)
  1341. {
  1342. log_info("*********** _LinkErrorTimeout ***********");
  1343. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  1344. pSysInfo->SystemPage = _LCM_VIEW;
  1345. }
  1346. //===============================================
  1347. // 取得卡號與卡號驗證
  1348. //===============================================
  1349. void AuthorizingStart(void)
  1350. {
  1351. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1352. pSysInfo->AuthorizeFlag = YES;
  1353. }
  1354. void ClearAuthorizedFlag(void)
  1355. {
  1356. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1357. pSysInfo->AuthorizeFlag = NO;
  1358. }
  1359. bool isAuthorizedComplete(void)
  1360. {
  1361. if (pSysInfo->AuthorizeFlag == YES) {
  1362. return false;
  1363. }
  1364. return true;
  1365. }
  1366. bool IsAuthorizingMode()
  1367. {
  1368. if (pSysInfo->AuthorizeFlag == NO) {
  1369. return false;
  1370. }
  1371. return true;
  1372. }
  1373. //===============================================
  1374. // 紀錄 Alarm Code
  1375. //===============================================
  1376. void ResetChargerAlarmCode(uint8_t gunIndex, char *code)
  1377. {
  1378. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1379. if (strcmp(code, "012234") == EQUAL) {
  1380. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1381. } else if (strcmp(code, "012235") == EQUAL) {
  1382. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1383. } else if (strcmp(code, "012236") == EQUAL) {
  1384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1385. } else if (strcmp(code, "012288") == EQUAL) {
  1386. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1387. } else if (strcmp(code, "012289") == EQUAL) {
  1388. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1389. } else if (strcmp(code, "012290") == EQUAL) {
  1390. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1391. } else if (strcmp(code, "012229") == EQUAL) {
  1392. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  1393. } else if (strcmp(code, "012230") == EQUAL) {
  1394. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  1395. } else if (strcmp(code, "012231") == EQUAL) {
  1396. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  1397. } else if (strcmp(code, "011011") == EQUAL) {
  1398. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1399. } else if (strcmp(code, "011013") == EQUAL) {
  1400. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1401. } else if (strcmp(code, "011015") == EQUAL) {
  1402. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1403. } else if (strcmp(code, "011012") == EQUAL) {
  1404. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1405. } else if (strcmp(code, "011014") == EQUAL) {
  1406. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1407. } else if (strcmp(code, "011016") == EQUAL) {
  1408. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1409. } else if (strcmp(code, "011018") == EQUAL) {
  1410. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  1411. } else if (strcmp(code, "011019") == EQUAL) {
  1412. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  1413. } else if (strcmp(code, "011020") == EQUAL) {
  1414. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  1415. } else if (strcmp(code, "012323") == EQUAL) {
  1416. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  1417. } else if (strcmp(code, "011038") == EQUAL) {
  1418. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  1419. }
  1420. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012229", 6) == EQUAL ||
  1421. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012230", 6) == EQUAL ||
  1422. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012231", 6) == EQUAL ||
  1423. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011011", 6) == EQUAL ||
  1424. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011013", 6) == EQUAL ||
  1425. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011015", 6) == EQUAL ||
  1426. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011012", 6) == EQUAL ||
  1427. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011014", 6) == EQUAL ||
  1428. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011016", 6) == EQUAL ||
  1429. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011018", 6) == EQUAL ||
  1430. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011019", 6) == EQUAL ||
  1431. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011020", 6) == EQUAL ||
  1432. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012323", 6) == EQUAL ||
  1433. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011038", 6) == EQUAL) {
  1434. strncpy((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1435. }
  1436. }
  1437. void RecordAlarmCode(uint8_t gunIndex, char *code)
  1438. {
  1439. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1440. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1441. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1442. {
  1443. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1444. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1445. }
  1446. if (pDcChargingInfo->StopChargeFlag == NO)
  1447. {
  1448. pDcChargingInfo->StopChargeFlag = YES;
  1449. }
  1450. }
  1451. }
  1452. void ReleaseAlarmCode(uint8_t gunIndex)
  1453. {
  1454. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1455. //if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1456. // return;
  1457. //}
  1458. switch (pDcChargingInfo->Type) {
  1459. case _Type_Chademo:
  1460. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1461. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1462. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  1463. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  1464. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1465. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1466. break;
  1467. case _Type_CCS_2:
  1468. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1469. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1470. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  1471. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  1472. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1473. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1474. break;
  1475. case _Type_GB:
  1476. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1477. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1478. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  1479. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  1480. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1481. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1482. break;
  1483. }
  1484. memcpy(pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1485. }
  1486. //===============================================
  1487. // EmergencyStop and Charging Stop
  1488. //===============================================
  1489. void ChargingTerminalProcess(uint8_t gunIndex)
  1490. {
  1491. setChargerMode(gunIndex, MODE_TERMINATING);
  1492. }
  1493. void ChargingAlarmProcess(uint8_t gunIndex)
  1494. {
  1495. UpdateErrorCodeToOcpp(gunIndex);
  1496. setChargerMode(gunIndex, MODE_ALARM);
  1497. }
  1498. void AcChargingTerminalProcess(void)
  1499. {
  1500. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  1501. pAcChargingInfo->SystemStatus = MODE_TERMINATING;
  1502. }
  1503. void StopChargingProcessByString(uint8_t level)
  1504. {
  1505. if (level > pSysWarning->Level) {
  1506. pSysWarning->Level = level;
  1507. }
  1508. }
  1509. void ReleaseChargingProcessByString(uint8_t level)
  1510. {
  1511. if (level >= pSysWarning->Level) {
  1512. pSysWarning->Level = WARN_LV_NL;
  1513. }
  1514. }
  1515. // 一般錯誤停止充電處理函式
  1516. void BoardErrOccurByString(uint8_t index, char *code)
  1517. {
  1518. uint8_t level = 1;
  1519. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1520. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1521. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1522. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1523. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1524. if (strncmp(code, "023730", 6) == EQUAL &&
  1525. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == NO
  1526. ) {
  1527. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1528. }
  1529. ChargingTerminalProcess(index);
  1530. }
  1531. StopChargingProcessByString(level);
  1532. }
  1533. void ReleaseBoardErrOccurByString(uint8_t index, char *code)
  1534. {
  1535. bool isTrigger = false;
  1536. uint8_t level = 1;
  1537. if (strncmp(code, "023730", 6) == 0 &&
  1538. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == YES) {
  1539. isTrigger = true;
  1540. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1541. }
  1542. if (isTrigger) {
  1543. ReleaseChargingProcessByString(level);
  1544. }
  1545. }
  1546. // 急停狀況的停止充電處理函式
  1547. void EmcOccureByString(char *code)
  1548. {
  1549. uint8_t level = 2;
  1550. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  1551. // 其錯誤等級為 2
  1552. //DS60-120 remove
  1553. if (strncmp(code, "012251", 6) == EQUAL ||
  1554. strncmp(code, "012252", 6) == EQUAL ||
  1555. strncmp(code, "012238", 6) == EQUAL ||
  1556. strncmp(code, "042251", 6) == EQUAL ||
  1557. strncmp(code, "042252", 6) == EQUAL ||
  1558. strncmp(code, "012304", 6) == EQUAL ||
  1559. strncmp(code, "042200", 6) == EQUAL ||
  1560. strncmp(code, "042201", 6) == EQUAL ||
  1561. strncmp(code, "042202", 6) == EQUAL ||
  1562. strncmp(code, "042267", 6) == EQUAL) {
  1563. for (uint8_t gun = 0; gun < pSysConfig->TotalConnectorCount; gun++) {
  1564. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  1565. //strncpy((char *)ShmOCPP16Data->StatusNotification[gun].VendorErrorCode, code, 6);
  1566. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1567. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1568. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1569. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1570. //ChargingTerminalProcess(gun);
  1571. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1572. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1573. }
  1574. ChargingAlarmProcess(gun);
  1575. }
  1576. }
  1577. StopChargingProcessByString(level);
  1578. InformOcppErrOccur(4);
  1579. }
  1580. }
  1581. void ReleaseEmsOccureByString(uint8_t index, char *code)
  1582. {
  1583. bool isTrigger = false;
  1584. uint8_t level = 2;
  1585. if (strncmp(code, "042251", 6) == 0 ) {
  1586. isTrigger = true;
  1587. } else if (strncmp(code, "012251", 6) == 0 &&
  1588. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1589. isTrigger = true;
  1590. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip = NO;
  1591. } else if (strncmp(code, "012252", 6) == 0 &&
  1592. pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1593. isTrigger = true;
  1594. pAlarmCode->AlarmEvents.bits.DoorOpen = NO;
  1595. } else if (strncmp(code, "012237", 6) == 0 &&
  1596. pAlarmCode->AlarmEvents.bits.SpdTrip == YES) {
  1597. isTrigger = true;
  1598. pAlarmCode->AlarmEvents.bits.SpdTrip = NO;
  1599. } else if (strncmp(code, "012238", 6) == 0 &&
  1600. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1601. isTrigger = true;
  1602. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1603. }
  1604. if (isTrigger) {
  1605. ReleaseChargingProcessByString(level);
  1606. InformOcppErrOccur(6);
  1607. }
  1608. }
  1609. static void checkOvpState(uint8_t gunIndex, uint8_t gunType)
  1610. {
  1611. switch(gunType) {
  1612. case _Type_Chademo:
  1613. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP == YES)
  1614. RecordAlarmCode(gunIndex, "012217");
  1615. break;
  1616. case _Type_CCS_2:
  1617. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP == YES)
  1618. RecordAlarmCode(gunIndex, "012219");
  1619. break;
  1620. case _Type_GB:
  1621. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP == YES)
  1622. RecordAlarmCode(gunIndex, "012221");
  1623. break;
  1624. }
  1625. }
  1626. //===============================================
  1627. // 確認各小板偵測的錯誤狀況
  1628. //===============================================
  1629. void CheckErrorOccurStatus(uint8_t index)
  1630. {
  1631. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1632. // RB
  1633. if (pSysConfig->PhaseLossPolicy == YES) {
  1634. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES ||
  1635. pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES ||
  1636. pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1637. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1638. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  1639. StopChargingProcessByString(2);
  1640. InformOcppErrOccur(13);
  1641. }
  1642. //DS60-120 add -----
  1643. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1644. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES) {
  1645. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012203", 6);
  1646. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES) {
  1647. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012204", 6);
  1648. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1649. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012205", 6);
  1650. }
  1651. }
  1652. //log_info("1 CheckErrorOccurStatus");
  1653. pDcChargingInfo->StopChargeFlag = YES;
  1654. //------------------------------------------------------------------
  1655. } else {
  1656. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1657. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1658. ReleaseChargingProcessByString(2);
  1659. InformOcppErrOccur(6);
  1660. }
  1661. }
  1662. } else {
  1663. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1664. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1665. ReleaseChargingProcessByString(2);
  1666. InformOcppErrOccur(6);
  1667. }
  1668. }
  1669. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES ||
  1670. pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES ||
  1671. pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1672. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1673. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  1674. StopChargingProcessByString(2);
  1675. InformOcppErrOccur(14);
  1676. }
  1677. //DS60-120 -----
  1678. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1679. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES) {
  1680. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012200", 6);
  1681. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES) {
  1682. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012201", 6);
  1683. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1684. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012202", 6);
  1685. }
  1686. }
  1687. //log_info("2 CheckErrorOccurStatus");
  1688. pDcChargingInfo->StopChargeFlag = YES;
  1689. //----------------------------------------------------------------------
  1690. } else {
  1691. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP) {
  1692. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1693. ReleaseChargingProcessByString(2);
  1694. InformOcppErrOccur(6);
  1695. }
  1696. }
  1697. checkOvpState(index, pDcChargingInfo->Type);
  1698. //--------------------------------------------------------------------------
  1699. if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == 0) {
  1700. //Primary
  1701. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1702. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012251", 6);
  1703. } else if (pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1704. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012252", 6);
  1705. } else if (pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1706. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012238", 6);
  1707. } else if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES) {
  1708. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012304", 6);
  1709. }
  1710. //Chiller temperature
  1711. //else if (pAlarmCode->AlarmEvents.bits.SystemChillerOTP == YES) {
  1712. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "012323", 6);
  1713. //} else if (pFaultCode->FaultEvents.bits.ChillerTempSensorBroken == YES) {
  1714. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "011038", 6);
  1715. //}
  1716. }
  1717. }
  1718. //===============================================
  1719. // 確認 GPIO 狀態
  1720. //===============================================
  1721. void gpio_set_value(unsigned int gpio, unsigned int value)
  1722. {
  1723. int fd;
  1724. char buf[MAX_BUF];
  1725. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1726. fd = open(buf, O_WRONLY);
  1727. if (fd < 0) {
  1728. perror("gpio/set-value");
  1729. return;
  1730. }
  1731. if (value) {
  1732. write(fd, "1", 2);
  1733. } else {
  1734. write(fd, "0", 2);
  1735. }
  1736. close(fd);
  1737. }
  1738. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1739. {
  1740. int fd;
  1741. char buf[MAX_BUF];
  1742. char ch;
  1743. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1744. fd = open(buf, O_RDONLY);
  1745. if (fd < 0) {
  1746. perror("gpio/get-value");
  1747. return fd;
  1748. }
  1749. read(fd, &ch, 1);
  1750. if (ch != '0') {
  1751. *value = 1;
  1752. } else {
  1753. *value = 0;
  1754. }
  1755. close(fd);
  1756. return 0;
  1757. }
  1758. void CheckGunTypeFromHw()
  1759. {
  1760. uint8_t i = 0;
  1761. int pinIn[4] = {22, 23, 44, 45};
  1762. unsigned int gpioValue = 0;
  1763. uint8_t tmp[2] = {0};
  1764. log_info("ModelName = %s", pSysConfig->ModelName);
  1765. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1766. gpio_get_value(pinIn[i], &gpioValue);
  1767. switch (pinIn[i]) {
  1768. //right slot
  1769. case 22:
  1770. bd1_1_status = gpioValue;
  1771. break;
  1772. case 23:
  1773. bd1_2_status = gpioValue;
  1774. break;
  1775. //left slot
  1776. case 44:
  1777. bd0_1_status = gpioValue;
  1778. break;
  1779. case 45:
  1780. bd0_2_status = gpioValue;
  1781. break;
  1782. }
  1783. }
  1784. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  1785. //CcsChargingData [0至1] 分別為 Right至Left
  1786. //model name 槍順序左至右分別為Right至Left
  1787. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  1788. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  1789. for (i = 0; i < 2; i++) {
  1790. switch (tmp[i]) {
  1791. case 0x01:
  1792. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CHAdeMO");
  1793. break;
  1794. case 0x10:
  1795. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CCS");
  1796. break;
  1797. case 0x11:
  1798. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "GBT");
  1799. break;
  1800. case 0x00:
  1801. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "None");
  1802. break;
  1803. }
  1804. }
  1805. }
  1806. void CheckGpioInStatus()
  1807. {
  1808. int i = 0;
  1809. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  1810. unsigned int gpioValue = 0;
  1811. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1812. gpio_get_value(pinIn[i], &gpioValue);
  1813. if (gpioValue == 0x01) {
  1814. switch (pinIn[i]) {
  1815. // 小板緊急停止
  1816. case 47:
  1817. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1818. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1819. if (pDcChargingInfo->slotsIndex == 1) {
  1820. if (pDcChargingInfo->Type == _Type_Chademo) {
  1821. BoardErrOccurByString(i, "023730");
  1822. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1823. BoardErrOccurByString(i, "013627");
  1824. }
  1825. break;
  1826. }
  1827. }
  1828. break;
  1829. case 27:
  1830. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1831. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1832. if (pDcChargingInfo->slotsIndex == 3) {
  1833. if (pDcChargingInfo->Type == _Type_Chademo) {
  1834. BoardErrOccurByString(i, "023730");
  1835. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1836. BoardErrOccurByString(i, "013627");
  1837. }
  1838. break;
  1839. }
  1840. }
  1841. break;
  1842. }
  1843. } else {
  1844. switch (pinIn[i]) {
  1845. // 小板解除緊急停止
  1846. case 47:
  1847. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1848. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1849. if (pDcChargingInfo->slotsIndex == 1) {
  1850. if (pDcChargingInfo->Type == _Type_Chademo) {
  1851. ReleaseBoardErrOccurByString(i, "023730");
  1852. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1853. ReleaseBoardErrOccurByString(i, "013627");
  1854. }
  1855. break;
  1856. }
  1857. }
  1858. break;
  1859. case 27:
  1860. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1861. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1862. if (pDcChargingInfo->slotsIndex == 3) {
  1863. if (pDcChargingInfo->Type == _Type_Chademo) {
  1864. ReleaseBoardErrOccurByString(i, "023730");
  1865. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1866. ReleaseBoardErrOccurByString(i, "013627");
  1867. }
  1868. break;
  1869. }
  1870. }
  1871. break;
  1872. }
  1873. }
  1874. }
  1875. }
  1876. //===============================================
  1877. // Main process
  1878. //===============================================
  1879. // 檢查 uint8_t 中某個 Bit 的值
  1880. // _byte : 欲改變的 byte
  1881. // _bit : 該 byte 的第幾個 bit
  1882. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit)
  1883. {
  1884. uint8_t mask_table[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
  1885. return ( _byte & mask_table[_bit] ) != 0x00;
  1886. }
  1887. // 設定 Byte 中某個 Bit的值
  1888. // _byte : 欲改變的 byte
  1889. // _bit : 該 byte 的第幾個 bit
  1890. // value : 修改的值為 0 or 1
  1891. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value)
  1892. {
  1893. if (value == 1) {
  1894. *_byte |= (1 << _bit);
  1895. } else if (value == 0) {
  1896. *_byte ^= (1 << _bit);
  1897. }
  1898. }
  1899. uint8_t isModeChange(uint8_t gunIndex)
  1900. {
  1901. uint8_t result = NO;
  1902. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1903. if (pDcChargingInfo->SystemStatus == pDcChargingInfo->PreviousSystemStatus) {
  1904. return result;
  1905. }
  1906. pDcChargingInfo->PreviousSystemStatus = pDcChargingInfo->SystemStatus;
  1907. ShmDcCommonData->SystemModeChange[gunIndex] = YES; //for Module_EvRxComm print temperature message
  1908. return YES;
  1909. }
  1910. static int checkSlotGpioMappingGunType(uint8_t gunIndex, uint8_t gunType)
  1911. {
  1912. uint8_t slot1 = 0, slot2 = 0;
  1913. if (gunIndex == 0) {
  1914. slot1 = bd0_1_status;
  1915. slot2 = bd0_2_status;
  1916. } else if (gunIndex == 1) {
  1917. slot1 = bd1_1_status;
  1918. slot2 = bd1_2_status;
  1919. }
  1920. log_info("slot %d gpio1 = %d, gpio2 = %d, type = %d",
  1921. gunIndex,
  1922. slot1,
  1923. slot2,
  1924. gunType);
  1925. switch (gunType) {
  1926. case _Type_Chademo:
  1927. if (slot1 != NO && slot2 != YES) {
  1928. return FAIL;
  1929. }
  1930. break;
  1931. case _Type_CCS_2:
  1932. if (slot1 != YES && slot2 != NO) {
  1933. return FAIL;
  1934. }
  1935. break;
  1936. case _Type_GB:
  1937. if (slot1 != YES && slot2 != YES) {
  1938. return FAIL;
  1939. }
  1940. break;
  1941. }
  1942. return PASS;
  1943. }
  1944. bool CheckConnectorTypeStatus(void)
  1945. {
  1946. bool result = true;
  1947. uint8_t gunIndex = 0;
  1948. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  1949. struct ChargingInfoData *pDcChargingInfo = NULL;
  1950. if (!MappingGunChargingInfo("CSU Task")) {
  1951. log_error("CheckConnectorTypeStatus MappingGunChargingInfo failed");
  1952. return false;
  1953. }
  1954. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  1955. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1956. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1957. if (checkSlotGpioMappingGunType(gunIndex, pDcChargingInfo->Type) == FAIL) {
  1958. return false;
  1959. }
  1960. pDcChargingInfo->SystemStatus = S_BOOTING;
  1961. switch (gunIndex) {
  1962. case 0:
  1963. if (pSysConfig->TotalConnectorCount == 1) {
  1964. if ((bd0_1_status == YES || bd0_2_status == YES) &&
  1965. (bd1_1_status == NO && bd1_2_status == NO)) {
  1966. pDcChargingInfo->Evboard_id = 0x01;
  1967. }
  1968. if ((bd0_1_status == NO && bd0_2_status == NO) &&
  1969. (bd1_1_status == YES || bd1_2_status == YES)
  1970. ) {
  1971. pDcChargingInfo->Evboard_id = 0x02;
  1972. }
  1973. break;
  1974. }
  1975. pDcChargingInfo->Evboard_id = 0x01;
  1976. break;
  1977. case 1:
  1978. pDcChargingInfo->Evboard_id = 0x02;
  1979. break;
  1980. }
  1981. CheckHwSlotStatusLog(gunIndex);
  1982. if (pDcChargingInfo->Evboard_id == 0x00) {
  1983. return false;
  1984. }
  1985. }
  1986. AdjustChargerCurrent();
  1987. return result;
  1988. }
  1989. int SpawnTask(void)
  1990. {
  1991. if(SIMULATION)
  1992. system("/root/simulation &");
  1993. sleep(2);
  1994. system("/root/Module_EventLogging &");
  1995. system("/root/Module_PrimaryComm &");
  1996. system("/root/Module_EvComm &");
  1997. system("/root/Module_LcmControl &");
  1998. system("/root/Module_InternalComm &");
  1999. system("/root/Module_ProduceUtils &");
  2000. system("/root/Module_UpdateFW &");
  2001. system("/root/Module_DoComm &");
  2002. return PASS;
  2003. }
  2004. void KillTask(void)
  2005. {
  2006. //ChangeLcmByIndex(_LCM_ERROR);
  2007. ChangeLcmByIndex(_LCM_MAINTAIN);
  2008. system("killall Module_EventLogging");
  2009. system("killall Module_PrimaryComm");
  2010. system("killall Module_EvComm");
  2011. system("killall Module_LcmControl");
  2012. system("killall Module_InternalComm");
  2013. system("killall Module_UpdateFW");
  2014. system("killall Module_ChkSysTask");
  2015. //system("killall Module_DoComm");
  2016. return ;
  2017. }
  2018. void KillTaskExceptPrimary(void)
  2019. {
  2020. //ChangeLcmByIndex(_LCM_ERROR);
  2021. ChangeLcmByIndex(_LCM_MAINTAIN);
  2022. system("killall Module_EvComm");
  2023. system("killall Module_InternalComm");
  2024. return;
  2025. }
  2026. void KillAllTask(void)
  2027. {
  2028. //ChangeLcmByIndex(_LCM_ERROR);
  2029. ChangeLcmByIndex(_LCM_MAINTAIN);
  2030. system("killall Module_EventLogging");
  2031. system("killall Module_PrimaryComm");
  2032. system("killall Module_EvComm");
  2033. system("killall Module_LcmControl");
  2034. system("killall Module_InternalComm");
  2035. system("killall Module_UpdateFW");
  2036. system("killall Module_ChkSysTask");
  2037. system("killall Module_DoComm");
  2038. return ;
  2039. }
  2040. void StartSystemTimeoutDet(uint8_t flag)
  2041. {
  2042. if (pSysInfo->SystemTimeoutFlag != flag) {
  2043. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2044. }
  2045. pSysInfo->SystemTimeoutFlag = flag;
  2046. }
  2047. void StopSystemTimeoutDet(void)
  2048. {
  2049. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2050. pSysInfo->SystemTimeoutFlag = Timeout_None;
  2051. }
  2052. void StartGunInfoTimeoutDet(uint8_t gunIndex, uint8_t flag)
  2053. {
  2054. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2055. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2056. if (pDcChargingInfo->TimeoutFlag != flag) {
  2057. gettimeofday(&pDcChargingInfo->TimeoutTimer, NULL);
  2058. }
  2059. pDcChargingInfo->TimeoutFlag = flag;
  2060. }
  2061. }
  2062. void StopGunInfoTimeoutDet(uint8_t gunIndex)
  2063. {
  2064. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2065. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2066. pDcChargingInfo->TimeoutFlag = Timeout_None;
  2067. }
  2068. }
  2069. void CheckConnectionTimeout(void)
  2070. {
  2071. if (ShmSelectGunInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  2072. _connectionTimeout = ShmSelectGunInfo->RemoteSetup.ConnectionTimeout;
  2073. } else {
  2074. _connectionTimeout = CONN_PLUG_TIMEOUT;
  2075. }
  2076. return;
  2077. }
  2078. void _evccidlinktimeout(uint8_t gunIndex)
  2079. {
  2080. log_info("Getting EVCCID Timeout");
  2081. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2082. pDcChargingInfo->isEVCCIDVerify = true;
  2083. StopGunInfoTimeoutDet(gunIndex);
  2084. destroySelGun(pSysInfo->CurGunSelected);
  2085. systemPageRestoreInit();
  2086. }
  2087. void CreateTimeoutFork(void)
  2088. {
  2089. pid_t timeoutPid;
  2090. timeoutPid = fork();
  2091. if (timeoutPid == 0) {
  2092. gettimeofday(&_cmdSubPriority_time, NULL);
  2093. CheckConnectionTimeout();
  2094. //log_info("Timeout Fork Child's PID is %d", getpid());
  2095. while (1) {
  2096. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  2097. CheckConnectionTimeout();
  2098. gettimeofday(&_cmdSubPriority_time, NULL);
  2099. }
  2100. //if (pSysInfo->SystemTimeoutFlag != 0)
  2101. //log_info("Timeout ***********SystemTimeoutFlag = %d(%d) ********",pSysInfo->SystemTimeoutFlag,
  2102. // GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL);
  2103. // 系統
  2104. switch (pSysInfo->SystemTimeoutFlag) {
  2105. case Timeout_SelftestChk:
  2106. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  2107. _SelfTestTimeout();
  2108. StopSystemTimeoutDet();
  2109. destroySelGun(DESTROY_ALL_SEL); //jerry add
  2110. }
  2111. break;
  2112. case Timeout_ReturnViewPage:
  2113. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_VIEWPAGE_TIMEOUT) {
  2114. StopSystemTimeoutDet();
  2115. pSysInfo->SystemPage = _LCM_VIEW;
  2116. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2117. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING || pDcChargingInfo->SystemStatus == S_COMPLETE )
  2118. setChargerMode(pSysInfo->CurGunSelected, MODE_IDLE);
  2119. }
  2120. break;
  2121. case Timeout_Authorizing:
  2122. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  2123. log_error("Authorizing Timeout");
  2124. _AuthorizedTimeout();
  2125. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2126. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2127. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2128. log_error("Author timeout restart DoComm");
  2129. system("killall Module_DoComm");
  2130. }
  2131. }
  2132. break;
  2133. case Timeout_WaitBalance:
  2134. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= BALANCE_TIMEOUT) {
  2135. log_error("Wait Balance timeout");
  2136. _AuthorizedTimeout();
  2137. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2138. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2139. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2140. log_error("Author timeout restart DoComm");
  2141. system("killall Module_DoComm");
  2142. }
  2143. }
  2144. break;
  2145. case Timeout_VerifyFail:
  2146. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  2147. if (pSysInfo->SystemPage ==_LCM_START_AUTHORIZE_FAIL ) {
  2148. StopSystemTimeoutDet();
  2149. _AutoReturnTimeout();
  2150. //destroySelGun(pSysInfo->CurGunSelected);
  2151. /*
  2152. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2153. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2154. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2155. log_error("Author timeout restart DoComm");
  2156. system("killall Module_DoComm");
  2157. //sleep(1);
  2158. //system("/root/Module_DoComm &");
  2159. }*/
  2160. } else if (pSysInfo->SystemPage ==_LCM_STOP_RFID_FAIL ) {
  2161. StopSystemTimeoutDet();
  2162. pSysInfo->SystemPage = _LCM_VIEW;
  2163. }
  2164. }
  2165. break;
  2166. case Timeout_WaitPlug:
  2167. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  2168. _DetectPlugInTimeout();
  2169. //destroySelGun(pSysInfo->CurGunSelected);
  2170. }
  2171. break;
  2172. /*
  2173. case Timeout_ReturnToChargingGunDet:
  2174. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  2175. if (getCurLcmPage() != _LCM_VIEW &&
  2176. getCurLcmPage() != _LCM_DETAIL_VIEW ) {
  2177. destroySelGun(pSysInfo->CurGunSelected); //jerry add
  2178. }
  2179. DisplayChargingInfo();
  2180. StopSystemTimeoutDet();
  2181. }
  2182. break;
  2183. */
  2184. case Timeout_AuthorizingForStop:
  2185. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  2186. strcpy((char *)pSysConfig->UserId, "");
  2187. ClearAuthorizedFlag();
  2188. StopSystemTimeoutDet();
  2189. }
  2190. break;
  2191. case Timeout_Terminating:
  2192. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TERMINATING_TIMEOUT) {
  2193. StopSystemTimeoutDet();
  2194. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2195. log_info("Terminating timeout");
  2196. setChargerMode(pSysInfo->CurGunSelected, S_COMPLETE);
  2197. }
  2198. break;
  2199. }
  2200. // 各槍
  2201. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2202. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2203. /*
  2204. if (pDcChargingInfo->TimeoutFlag != 0)
  2205. log_info("Timeout ***********GunTimeoutFlag = %d(%d) ********",pDcChargingInfo->TimeoutFlag,
  2206. GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL);
  2207. */
  2208. switch (pDcChargingInfo->TimeoutFlag) {
  2209. case Timeout_EVCCID_Link:
  2210. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= EVCCID_LINK_TIMEOUT) {
  2211. _evccidlinktimeout(gunIndex);
  2212. }
  2213. break;
  2214. case Timeout_LinkError:
  2215. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= LINKERROR_TIMEOUT) {
  2216. _LinkErrorTimeout(gunIndex);
  2217. StopGunInfoTimeoutDet(gunIndex);
  2218. destroySelGun(gunIndex);
  2219. }
  2220. break;
  2221. case Timeout_Preparing:
  2222. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  2223. _PrepareTimeout(gunIndex);
  2224. StopGunInfoTimeoutDet(gunIndex);
  2225. destroySelGun(gunIndex); //jerry add
  2226. }
  2227. break;
  2228. case Timeout_EvChargingDet:
  2229. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  2230. _DetectEvChargingEnableTimeout(gunIndex);
  2231. StopGunInfoTimeoutDet(gunIndex);
  2232. destroySelGun(gunIndex); //jerry add
  2233. }
  2234. break;
  2235. case Timeout_EvseChargingDet:
  2236. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  2237. _DetectEvseChargingEnableTimeout(gunIndex);
  2238. StopGunInfoTimeoutDet(gunIndex);
  2239. destroySelGun(gunIndex); //jerry add
  2240. }
  2241. break;
  2242. case Timeout_EvseCompleteDet:
  2243. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  2244. StopGunInfoTimeoutDet(gunIndex);
  2245. destroySelGun(gunIndex); //jerry add
  2246. }
  2247. break;
  2248. case Timeout_ForCcsPrechargeDet:
  2249. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  2250. _CcsPrechargeTimeout(gunIndex);
  2251. StopGunInfoTimeoutDet(gunIndex);
  2252. destroySelGun(gunIndex); //jerry add
  2253. }
  2254. break;
  2255. /*
  2256. case Timeout_SelectGun: //Jerry add
  2257. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= SEL_GUN_TIMEOUT) {
  2258. log_info("Timeout_SelectGun");
  2259. StopGunInfoTimeoutDet(gunIndex);
  2260. destroySelGun(gunIndex); //jerry add
  2261. }
  2262. break;
  2263. */
  2264. case Timeout_DetailView:
  2265. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= DETAILVIEW_TIMEOUT) {
  2266. StopGunInfoTimeoutDet(gunIndex);
  2267. pSysInfo->SystemPage = _LCM_VIEW;
  2268. }
  2269. break;
  2270. }
  2271. }
  2272. sleep(1);
  2273. }
  2274. }
  2275. }
  2276. void CheckFactoryConfigFunction(void)
  2277. {
  2278. char Buf[256] = {0};
  2279. if (pSysInfo->FactoryConfiguration) {
  2280. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  2281. pSysConfig->ModelName,
  2282. pSysConfig->SerialNumber);
  2283. system(Buf);
  2284. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2285. system("sync");
  2286. sleep(5);
  2287. system("reboot -f");
  2288. sleep(5);
  2289. system("reboot -f");
  2290. }
  2291. }
  2292. //===============================================
  2293. // Check reservation date is expired
  2294. //===============================================
  2295. int isReservationExpired(uint8_t gunIndex)
  2296. {
  2297. int result = NO;
  2298. struct tm expiredDate;
  2299. struct timeb expiredTime;
  2300. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gunIndex].ExpiryDate,
  2301. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2302. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2303. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  2304. expiredDate.tm_year -= 1900;
  2305. expiredDate.tm_mon -= 1;
  2306. expiredTime.time = mktime(&expiredDate);
  2307. if (!CheckTimeOut(expiredTime)) {
  2308. result = YES;
  2309. }
  2310. }
  2311. return result;
  2312. }
  2313. //===============================================
  2314. // OCPP
  2315. //===============================================
  2316. void CheckOcppStatus(void)
  2317. {
  2318. bool canReset = true;
  2319. //bool canHardReset = true; //DS60-120 add
  2320. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  2321. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  2322. }
  2323. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  2324. if (pSysWarning->Level != WARN_LV_ER) {
  2325. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2326. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2327. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2328. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  2329. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  2330. canReset = false;
  2331. if (pDcChargingInfo->SystemStatus >= S_REASSIGN &&
  2332. pDcChargingInfo->SystemStatus < S_TERMINATING) {
  2333. //canHardReset = false;
  2334. ChargingTerminalProcess(_index);
  2335. //restartFlag = 1;
  2336. }
  2337. }
  2338. }
  2339. }
  2340. if (canReset) {
  2341. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  2342. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2343. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  2344. log_error("****** Hard Reboot ****** ");
  2345. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2346. sleep(3);
  2347. system("reboot -f");
  2348. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  2349. log_error("****** Soft Reboot ****** ");
  2350. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2351. sleep(3);
  2352. system("killall OcppBackend &");
  2353. KillAllTask();
  2354. TryCloseWatchdog();
  2355. system("/usr/bin/run_evse_restart.sh");
  2356. }
  2357. }
  2358. }
  2359. }
  2360. void OcppStopTransation(uint8_t gunIndex)
  2361. {
  2362. uint8_t _OcppGunIndex = gunIndex;
  2363. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2364. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  2365. if (pSysConfig->AcConnectorCount == 1 && gunIndex == 1) {
  2366. _OcppGunIndex = 2;
  2367. }
  2368. if (strcmp((char *)pDcChargingInfo->StartUserId, "") == EQUAL) {
  2369. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2370. } else {
  2371. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)pDcChargingInfo->StartUserId);
  2372. }
  2373. log_info("IdTag = %s ", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2374. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  2375. }
  2376. bool OcppRemoteStop(uint8_t gunIndex)
  2377. {
  2378. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2379. // 有 AC 槍的話
  2380. if (acDirIndex > 0 && gunIndex > 0) {
  2381. gunIndex += acDirIndex;
  2382. }
  2383. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  2384. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  2385. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  2386. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  2387. }
  2388. return result;
  2389. }
  2390. bool WifiScheduleStop(uint8_t gunIndex)
  2391. {
  2392. bool result = false;
  2393. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2394. result = pDcChargingInfo->schedule.isTriggerStop;
  2395. pDcChargingInfo->schedule.isTriggerStop = NO;
  2396. return result;
  2397. }
  2398. void OcppRemoteStartChk()
  2399. {
  2400. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  2401. } else if (!isDetectPlugin()) {
  2402. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  2403. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2404. for (uint8_t ac_index = 0; ac_index < pSysConfig->AcConnectorCount; ac_index++) {
  2405. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(ac_index);
  2406. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  2407. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2408. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2409. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2410. pAcChargingInfo->RemoteStartFlag = YES;
  2411. pSysInfo->OrderCharging = YES;
  2412. //pSysInfo->OrderCharging = DEFAULT_AC_INDEX;
  2413. ShmOCPP16Data->CsMsg.bits[pSysConfig->TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  2414. DetectPluginStart();
  2415. return;
  2416. }
  2417. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2418. }
  2419. }
  2420. uint8_t threeGunIndex = 0;
  2421. uint8_t dcIndex = 0;
  2422. bool isGunUsingStatus = false;
  2423. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2424. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2425. // 如果有 AC 槍,且 DC 槍也有兩把
  2426. if (acDirIndex == 1 && _index == 1) {
  2427. threeGunIndex = 1;
  2428. }
  2429. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  2430. dcIndex = _index;
  2431. }
  2432. if (pDcChargingInfo->SystemStatus != S_IDLE) {
  2433. isGunUsingStatus = true;
  2434. }
  2435. }
  2436. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  2437. if (isGunUsingStatus && pSysInfo->IsAlternatvieConf) {
  2438. if (dcIndex == 0) {
  2439. threeGunIndex = 0;
  2440. }
  2441. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2442. return;
  2443. }
  2444. if (dcIndex == 0) {
  2445. threeGunIndex = 0;
  2446. }
  2447. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(dcIndex);
  2448. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES) {
  2449. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2450. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2451. pDcChargingInfo->RemoteStartFlag = YES;
  2452. pSysInfo->OrderCharging = YES;
  2453. //pSysInfo->OrderCharging = gunIndex;
  2454. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2455. DetectPluginStart();
  2456. setSelGunWaitToAuthor(dcIndex);
  2457. }
  2458. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2459. }
  2460. }
  2461. }
  2462. void ChkOcppStatus(uint8_t gunIndex)
  2463. {
  2464. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2465. if (pDcChargingInfo->SystemStatus == S_IDLE &&
  2466. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  2467. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  2468. if (isReservationExpired(gunIndex)) {
  2469. log_error("***************ChkOcppStatus : OcppReservedStatus******************** ");
  2470. pDcChargingInfo->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  2471. pDcChargingInfo->SystemStatus = S_RESERVATION;
  2472. }
  2473. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  2474. }
  2475. if (pDcChargingInfo->SystemStatus == S_RESERVATION &&
  2476. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  2477. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  2478. if (isReservationExpired(gunIndex)) {
  2479. log_error("***************ChkOcppStatus : Cancel OcppReservedStatus******************** ");
  2480. pDcChargingInfo->ReservationId = 0;
  2481. pDcChargingInfo->SystemStatus = S_IDLE;
  2482. }
  2483. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  2484. }
  2485. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  2486. log_error("***************ChkOcppStatus : OcppChangeAvailability******************** ");
  2487. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  2488. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  2489. if (isDb_ready) {
  2490. DB_Update_Operactive(gunIndex, true);
  2491. }
  2492. pDcChargingInfo->IsAvailable = YES;
  2493. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2494. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2495. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2496. setChargerMode(gunIndex, MODE_IDLE);
  2497. }
  2498. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  2499. if (isDb_ready) {
  2500. DB_Update_Operactive(gunIndex, false);
  2501. }
  2502. pDcChargingInfo->IsAvailable = NO;
  2503. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2504. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2505. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2506. setChargerMode(gunIndex, MODE_MAINTAIN);
  2507. }
  2508. }
  2509. }
  2510. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  2511. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  2512. if (pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK && // DS60-120 add ||
  2513. pDcChargingInfo->SystemStatus <= S_CHARGING) {
  2514. // 充電中,需停止充電
  2515. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  2516. ChargingTerminalProcess(gunIndex);
  2517. }
  2518. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  2519. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  2520. }
  2521. }
  2522. bool CheckBackendChargingTimeout(uint8_t gunIndex)
  2523. {
  2524. bool result = false;
  2525. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2526. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2527. if (pSysConfig->MaxChargingDuration > 0) {
  2528. if (pDcChargingInfo->PresentChargedDuration > (pSysConfig->MaxChargingDuration * 60)) {
  2529. result = true;
  2530. }
  2531. }
  2532. }
  2533. return result;
  2534. }
  2535. bool CheckBackendChargingEnergy(uint8_t gunIndex)
  2536. {
  2537. bool result = false;
  2538. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2539. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2540. if (pSysConfig->MaxChargingEnergy > 0) {
  2541. if (pDcChargingInfo->PresentChargedEnergy > pSysConfig->MaxChargingEnergy) {
  2542. result = true;
  2543. }
  2544. }
  2545. }
  2546. return result;
  2547. }
  2548. void InformOcppErrOccur(uint8_t codeType)
  2549. {
  2550. char _error[25];
  2551. switch (codeType) {
  2552. case 4:
  2553. strcpy(_error, "InternalError");
  2554. break;
  2555. case 6:
  2556. strcpy(_error, "NoError");
  2557. break;
  2558. case 7:
  2559. strcpy(_error, "OtherError");
  2560. break;
  2561. case 13:
  2562. strcpy(_error, "UnderVoltage");
  2563. break;
  2564. case 14:
  2565. strcpy(_error, "OverVoltage");
  2566. break;
  2567. }
  2568. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2569. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, _error);
  2570. }
  2571. }
  2572. //===============================================
  2573. // Config process
  2574. //===============================================
  2575. /*
  2576. void AddPlugInTimes(uint8_t gunIndex)
  2577. {
  2578. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2579. if (pDcChargingInfo->Type == _Type_Chademo) {
  2580. pSysConfig->ChademoPlugInTimes += 1;
  2581. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2582. pSysConfig->Ccs2PlugInTimes += 1;
  2583. } else if (pDcChargingInfo->Type == _Type_GB) {
  2584. pSysConfig->GbPlugInTimes += 1;
  2585. }
  2586. }*/
  2587. void ChangeStartOrStopDateTime(uint8_t isStart, uint8_t gunIndex)
  2588. {
  2589. char cmdBuf[32];
  2590. struct timeb csuTime;
  2591. struct tm *tmCSU;
  2592. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2593. ftime(&csuTime);
  2594. tmCSU = localtime(&csuTime.time);
  2595. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2596. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2597. tmCSU->tm_sec);
  2598. if (isStart) {
  2599. strcpy((char *)pDcChargingInfo->StartDateTime, cmdBuf);
  2600. } else {
  2601. strcpy((char *)pDcChargingInfo->StopDateTime, cmdBuf);
  2602. }
  2603. }
  2604. void ChangeGunSelectByIndex(uint8_t sel)
  2605. {
  2606. pSysInfo->CurGunSelected = sel;
  2607. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  2608. }
  2609. void CheckIsAlternatvieByModelName()
  2610. {
  2611. if (pSysConfig->ModelName[1] == 'W') { //DS60-120
  2612. // 壁掛
  2613. pSysInfo->IsAlternatvieConf = YES;
  2614. } else {
  2615. pSysInfo->IsAlternatvieConf = NO;
  2616. }
  2617. }
  2618. void StopProcessingLoop()
  2619. {
  2620. for (;;) {
  2621. CheckFactoryConfigFunction();
  2622. //CheckFwUpdateFunction();
  2623. if (pSysWarning->Level == WARN_LV_ER) {
  2624. ChkPrimaryStatus();
  2625. if (pSysWarning->Level == WARN_LV_NL) {
  2626. log_info("Soft reboot for retry self-tets (Primary). ");
  2627. KillAllTask();
  2628. sleep(3);
  2629. TryCloseWatchdog();
  2630. system("/usr/bin/run_evse_restart.sh");
  2631. return;
  2632. }
  2633. }
  2634. sleep(1);
  2635. TryFeedWatchdog();
  2636. }
  2637. }
  2638. bool IsConnectorWholeIdle()
  2639. {
  2640. bool result = true;
  2641. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2642. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2643. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2644. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2645. result = false;
  2646. break;
  2647. }
  2648. }
  2649. for (uint8_t count = 0; count < pSysConfig->AcConnectorCount; count++) {
  2650. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  2651. if (pAcChargingInfo->SystemStatus != S_IDLE &&
  2652. pAcChargingInfo->IsErrorOccur == NO) {
  2653. result = false;
  2654. break;
  2655. }
  2656. }
  2657. return result;
  2658. }
  2659. void ClearAlarmCodeWhenAcOff()
  2660. {
  2661. if (!pSysInfo->AcContactorStatus) {
  2662. pAlarmCode->AlarmEvents.bits.PsuNoResource = NO;
  2663. }
  2664. }
  2665. //==========================================
  2666. // Check Smart Charging Profile
  2667. //==========================================
  2668. int GetStartScheduleTime(uint8_t *time)
  2669. {
  2670. int result = -1;
  2671. struct tm tmScheduleStart;
  2672. struct timeb tbScheduleStart;
  2673. if ((sscanf((char *)time, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6)) {
  2674. tmScheduleStart.tm_year -= 1900;
  2675. tmScheduleStart.tm_mon -= 1;
  2676. tbScheduleStart.time = mktime(&tmScheduleStart);
  2677. tbScheduleStart.millitm = 0;
  2678. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  2679. }
  2680. return result;
  2681. }
  2682. void CheckSmartChargeProfile(uint8_t _index)
  2683. {
  2684. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2685. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == YES) {
  2686. // Get Charging Profile
  2687. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  2688. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL &&
  2689. ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileId == YES //DS60-120 add
  2690. ) {
  2691. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  2692. uint8_t _startCount = NO_DEFINE;
  2693. uint8_t _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  2694. for (uint8_t _count = 0; _count < _maxCount; _count++) {
  2695. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  2696. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod) {
  2697. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  2698. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  2699. _startCount = _count;
  2700. }
  2701. }
  2702. }
  2703. log_info("_startCount = %d ", _startCount);
  2704. if (_startCount < _maxCount) {
  2705. //DS60-120 add
  2706. log_info("Profile Limit = %f ", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  2707. pDcChargingInfo->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  2708. if (pDcChargingInfo->EvBatterytargetVoltage > 0 && pDcChargingInfo->PresentChargingVoltage > 0) {
  2709. pDcChargingInfo->ChargingProfileCurrent = (pDcChargingInfo->ChargingProfilePower / pDcChargingInfo->PresentChargingVoltage) * 10;
  2710. } else {
  2711. pDcChargingInfo->ChargingProfileCurrent = 0;
  2712. }
  2713. } else {
  2714. pDcChargingInfo->ChargingProfilePower = -1;
  2715. pDcChargingInfo->ChargingProfileCurrent = -1;
  2716. }
  2717. } else {
  2718. pDcChargingInfo->ChargingProfilePower = -1;
  2719. pDcChargingInfo->ChargingProfileCurrent = -1;
  2720. }
  2721. log_info("ChargingProfilePower = %f ", pDcChargingInfo->ChargingProfilePower);
  2722. log_info("ChargingProfileCurrent = %f ", pDcChargingInfo->ChargingProfileCurrent);
  2723. }
  2724. }
  2725. void ChargingProfileFlat(uint8_t _index)
  2726. {
  2727. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  2728. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  2729. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  2730. }
  2731. }
  2732. }
  2733. /*
  2734. void CheckReturnToChargingConn()
  2735. {
  2736. if ((pSysConfig->TotalConnectorCount + pSysConfig->AcConnectorCount) > 1 &&
  2737. pSysInfo->PageIndex != _LCM_START_AUTHORIZING &&
  2738. pSysInfo->PageIndex != _LCM_START_AUTHORIZE_FAIL &&
  2739. pSysInfo->PageIndex != _LCM_WAIT_PLUGIN) {
  2740. bool isReturnTimeout = false;
  2741. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2742. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2743. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  2744. if (count == pSysInfo->CurGunSelected) {
  2745. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2746. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2747. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2748. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2749. ) {
  2750. isReturnTimeout = false;
  2751. break;
  2752. }
  2753. } else if (count != pSysInfo->CurGunSelected) {
  2754. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2755. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2756. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2757. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2758. ) {
  2759. isReturnTimeout = true;
  2760. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2761. }
  2762. }
  2763. }
  2764. // AC 槍
  2765. if (!isReturnTimeout && pSysConfig->AcConnectorCount > 0) {
  2766. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2767. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  2768. // 沒有選中 AC,且 AC 在充電中
  2769. if (pSysInfo->CurGunSelectedByAc == NO_DEFINE &&
  2770. (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  2771. pAcChargingInfo->SystemStatus <= S_COMPLETE)) {
  2772. // 當前 DC 充電槍在 idle 狀態
  2773. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2774. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2775. isReturnTimeout = true;
  2776. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2777. }
  2778. } else if (pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2779. ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2780. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2781. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2782. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  2783. // 當前 DC 充電槍在 idle 狀態
  2784. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2785. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2786. isReturnTimeout = true;
  2787. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2788. }
  2789. }
  2790. }
  2791. if (!isReturnTimeout) {
  2792. StopSystemTimeoutDet();
  2793. }
  2794. }
  2795. }
  2796. bool GetStartChargingByAlterMode(uint8_t _gun)
  2797. {
  2798. bool result = true;
  2799. if (pSysConfig->TotalConnectorCount == 2 &&
  2800. pSysInfo->IsAlternatvieConf == YES) {
  2801. for (uint8_t _select = 0; _select < pSysConfig->TotalConnectorCount; _select++) {
  2802. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_select);
  2803. if (_select != _gun) {
  2804. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2805. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2806. result = false;
  2807. break;
  2808. }
  2809. }
  2810. }
  2811. }
  2812. return result;
  2813. }
  2814. */
  2815. void TheEndCharging(uint8_t gunIndex)
  2816. {
  2817. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2818. ftime(&endChargingTime[gunIndex]);
  2819. if (pDcChargingInfo->PresentChargedDuration != 0) {
  2820. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  2821. }
  2822. pDcChargingInfo->isRemoteStart = NO;
  2823. StopGunInfoTimeoutDet(gunIndex);
  2824. //StartGunInfoTimeoutDet(gunIndex, Timeout_EvseCompleteDet);
  2825. ChangeStartOrStopDateTime(NO, gunIndex);
  2826. DB_Insert_Record(gunIndex);
  2827. }
  2828. void UpdateErrorCodeToOcpp(uint8_t index)
  2829. {
  2830. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  2831. //log_info("%d = ConnectorAlarmCode = %s", index, pDcChargingInfo->ConnectorAlarmCode);
  2832. //log_info("%d = EvConnAlarmCode = %s", index, pDcChargingInfo->EvConnAlarmCode);
  2833. if (strcmp((char *)pDcChargingInfo->ConnectorAlarmCode, "") != EQUAL) {
  2834. //if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == EQUAL) {
  2835. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  2836. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->ConnectorAlarmCode);
  2837. } else if (strcmp((char *)pDcChargingInfo->EvConnAlarmCode, "") != EQUAL) {
  2838. //} else if (strlen((char *)pDcChargingInfo->EvConnAlarmCode) == EQUAL) {
  2839. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  2840. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->EvConnAlarmCode);
  2841. }
  2842. //log_info("Gun %d = VendorErrorCode = %s", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  2843. }
  2844. void AdjustChargerCurrent()
  2845. {
  2846. pSysConfig->RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  2847. // 設定的電流~ 如超過可輸出的電流,則 bypass
  2848. if (pSysConfig->RatingCurrent < pSysConfig->MaxChargingCurrent ||
  2849. pSysConfig->RatingCurrent == 0) {
  2850. pSysConfig->MaxChargingCurrent = 0;
  2851. }
  2852. log_info("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  2853. ShmPsuData->SystemAvailablePower / 10,
  2854. ShmPsuData->SystemAvailableCurrent / 10
  2855. );
  2856. log_info("Config : ChargingPower = %d, ChargingCurrent = %d",
  2857. pSysConfig->MaxChargingPower,
  2858. pSysConfig->MaxChargingCurrent
  2859. );
  2860. }
  2861. void ResetDetAlarmStatus(uint8_t gun)
  2862. {
  2863. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  2864. if (pDcChargingInfo->Type == _Type_Chademo) {
  2865. if (pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  2866. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  2867. }
  2868. } else if (pDcChargingInfo->Type == _Type_GB) {
  2869. if (pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP == YES) {
  2870. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  2871. }
  2872. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2873. if (pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  2874. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  2875. }
  2876. }
  2877. }
  2878. static void autoStartCharging(uint8_t gunIndex)
  2879. {
  2880. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2881. if ( (pSysInfo->SystemPage >= _LCM_START_AUTHORIZING && pSysInfo->SystemPage <= _LCM_LINK_ERROR) ||
  2882. pDcChargingInfo->isEVCCIDVerify && strcmp( (char *)pSysConfig->UserId , "" ) != EQUAL) {
  2883. return;
  2884. }
  2885. if ((pDcChargingInfo->ConnectorPlugIn == YES) &&
  2886. (pDcChargingInfo->SystemStatus == S_IDLE )
  2887. ) {
  2888. if(!pSysConfig->AuthorisationMode) {
  2889. if (pSysConfig->isAuthrizeByEVCCID && !pDcChargingInfo->isEVCCIDVerify &&
  2890. pDcChargingInfo->Type == _Type_CCS_2) {
  2891. // LCM show linking pic. & timeout 2 min
  2892. if( pSysInfo->CurGunSelected == gunIndex ) {
  2893. systemPageRestoreInit();
  2894. pSysInfo->SystemPage = _LCM_PRECHARGE;
  2895. }
  2896. StartGunInfoTimeoutDet(gunIndex,Timeout_EVCCID_Link);
  2897. // Getting EVCCID
  2898. if( strcmp( (char *)pDcChargingInfo->EVCCID, "" ) != EQUAL ) {
  2899. log_info("Authorizing EVCCID");
  2900. StopSystemTimeoutDet();
  2901. strcpy((char *)pSysConfig->UserId, (char *)pDcChargingInfo->EVCCID);
  2902. ChangeGunSelectByIndex(gunIndex);
  2903. confirmSelGun(gunIndex);
  2904. pDcChargingInfo->isEVCCIDVerify = true;
  2905. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  2906. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  2907. setChargerMode(gunIndex, MODE_AUTHORIZING);
  2908. }
  2909. } else
  2910. return;
  2911. } else {
  2912. strcpy((char *)&pSysConfig->UserId, "AutoStartCharging");
  2913. ChangeGunSelectByIndex(gunIndex);
  2914. confirmSelGun(gunIndex);
  2915. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  2916. setChargerMode(gunIndex, MODE_AUTHORIZING);
  2917. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  2918. }
  2919. }
  2920. }
  2921. static bool PrecheckIsPass(uint8_t gunIndex)
  2922. {
  2923. bool result = true;
  2924. // relay welding or driving 是反向
  2925. result = !ShmDcCommonData->GunRelayWeldingOccur[gunIndex];
  2926. return result;
  2927. }
  2928. static void ReviewCriticalAlarm(void)
  2929. {
  2930. if (
  2931. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  2932. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  2933. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  2934. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  2935. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  2936. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  2937. //Power cabinet alarm status
  2938. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  2939. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  2940. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  2941. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  2942. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  2943. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  2944. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES
  2945. ) {
  2946. pSysWarning->Level = WARN_LV_ER;
  2947. } else {
  2948. pSysWarning->Level = WARN_LV_NL;
  2949. }
  2950. }
  2951. static void CheckRelayWeldingOrDrivingFault(uint8_t gunIndex)
  2952. {
  2953. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2954. // relay welding fault then stop the charging process.
  2955. uint8_t faultCode = RELAY_STATUS_ERROR_NONE;
  2956. //static uint8_t drivingCount = 0;
  2957. if (gunIndex == 0) {
  2958. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  2959. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  2960. faultCode = RELAY_STATUS_ERROR_WELDING;
  2961. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  2962. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  2963. faultCode = RELAY_STATUS_ERROR_DRIVING;
  2964. }
  2965. } else if (gunIndex == 1) {
  2966. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  2967. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  2968. faultCode = RELAY_STATUS_ERROR_WELDING;
  2969. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  2970. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  2971. faultCode = RELAY_STATUS_ERROR_DRIVING;
  2972. }
  2973. }
  2974. if (pSysConfig->TotalConnectorCount >= 2 &&
  2975. !pSysInfo->IsAlternatvieConf) {
  2976. // 橋接
  2977. if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  2978. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  2979. faultCode = RELAY_STATUS_ERROR_WELDING;
  2980. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  2981. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  2982. faultCode = RELAY_STATUS_ERROR_DRIVING;
  2983. }
  2984. }
  2985. if (faultCode == RELAY_STATUS_ERROR_WELDING) {
  2986. // welding
  2987. if (pDcChargingInfo->Type == _Type_Chademo) {
  2988. RecordAlarmCode(gunIndex, "011011");
  2989. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2990. } else if (pDcChargingInfo->Type == _Type_GB) {
  2991. RecordAlarmCode(gunIndex, "011015");
  2992. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2993. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2994. RecordAlarmCode(gunIndex, "011013");
  2995. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2996. }
  2997. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = YES;
  2998. //EmcOccureByString("");
  2999. } else if (faultCode == RELAY_STATUS_ERROR_DRIVING) {
  3000. //if (drivingCount++ != 2) {
  3001. // return;
  3002. //}
  3003. // driving
  3004. if (pDcChargingInfo->Type == _Type_Chademo) {
  3005. RecordAlarmCode(gunIndex, "011012");
  3006. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  3007. } else if (pDcChargingInfo->Type == _Type_GB) {
  3008. RecordAlarmCode(gunIndex, "011016");
  3009. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  3010. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3011. RecordAlarmCode(gunIndex, "011014");
  3012. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  3013. }
  3014. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = YES;
  3015. //EmcOccureByString("");
  3016. } else {
  3017. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = NO;
  3018. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = NO;
  3019. //drivingCount = 0;
  3020. }
  3021. }
  3022. static void isChargingAverageState(void)
  3023. {
  3024. if (pSysInfo->MainChargingMode != _MAIN_CHARGING_MODE_AVER) {
  3025. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3026. return;
  3027. }
  3028. // 均充 -> 最大充
  3029. if (pSysInfo->BridgeRelayStatus == NO) {
  3030. if (pSysInfo->ReAssignedFlag == _REASSIGNED_NONE) {
  3031. log_info("=============Smart Charging============= Step 11 ");
  3032. pSysInfo->ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3033. }
  3034. } else if (pSysInfo->ReAssignedFlag != _REASSIGNED_COMP &&
  3035. pSysInfo->ReAssignedFlag != _REASSIGNED_WAITING) {
  3036. log_info("=============Smart Charging============= Step 14 ");
  3037. pSysInfo->ReAssignedFlag = _REASSIGNED_WAITING;
  3038. } else if (pSysInfo->ReAssignedFlag == _REASSIGNED_COMP) {
  3039. pSysInfo->MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3040. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3041. }
  3042. }
  3043. bool Taskconutstring(char *src, char *taskname)
  3044. {
  3045. bool result = false;
  3046. if (src == NULL || strlen(src) == 0)
  3047. return result;
  3048. if (strstr(src, taskname) != NULL &&
  3049. strstr(src, "grep") == NULL &&
  3050. strstr(src, "[") == NULL)
  3051. {
  3052. result = true;
  3053. }
  3054. return result;
  3055. }
  3056. int GetProcessCount(char *procName)
  3057. {
  3058. int result = 0;
  3059. FILE *fp;
  3060. char cmd[256];
  3061. char buf[256];
  3062. sprintf(cmd, "ps -ef |grep %s", procName);
  3063. fp = popen(cmd, "r");
  3064. if(fp != NULL)
  3065. {
  3066. while(fgets(buf, sizeof(buf), fp) != NULL)
  3067. {
  3068. if (Taskconutstring(buf, procName))
  3069. result++;
  3070. }
  3071. }
  3072. pclose(fp);
  3073. return result;
  3074. }
  3075. void CheckTaskAlive()
  3076. {
  3077. pid_t Pid = fork();
  3078. if ( Pid == 0 ) {
  3079. //log_info("CheckTaskAlive fork Child's PID is %d", getpid());
  3080. while(1) {
  3081. sleep(3);
  3082. unsigned char count = GetProcessCount("Module_ChkSysTask");
  3083. if( count != 1 ) {
  3084. system("pkill Module_ChkSysTask");
  3085. sleep(10);
  3086. log_info("Create Check System Task");
  3087. system("/root/Module_ChkSysTask &");
  3088. }
  3089. }
  3090. return;
  3091. }
  3092. log_info("Create Check Systam fork:%d",Pid);
  3093. return;
  3094. }
  3095. /**
  3096. * [checkPileEndGfdResult 確認充電樁GFD狀態]
  3097. * @Author Jerry
  3098. * @DateTime 2021-07-01
  3099. * @param gunIndex [當前的槍]
  3100. * @param gunType [槍的類型]
  3101. * @param sysStatus [槍的系統狀態]
  3102. */
  3103. static void checkPileEndGfdResult(uint8_t gunIndex, uint8_t gunType, uint8_t sysStatus)
  3104. {
  3105. switch (gunType) {
  3106. case _Type_Chademo:
  3107. // 檢查樁端的 GFD 結果
  3108. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3109. isPrechargeStatus_chademo(gunIndex) > 6 &&
  3110. isPrechargeStatus_chademo(gunIndex) < 8
  3111. ) {
  3112. // 當前操作的槍號,進入 Charging
  3113. setChargerMode(gunIndex, MODE_CHARGING);
  3114. }
  3115. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3116. // GFD 錯誤停止
  3117. RecordAlarmCode(gunIndex, "012234");
  3118. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  3119. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3120. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3121. // GFD 警告
  3122. //RecordAlarmCode(gunIndex, "012296");
  3123. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3124. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  3125. }
  3126. else {
  3127. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3128. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3129. }
  3130. break;
  3131. case _Type_GB:
  3132. // 檢查樁端的 GFD 結果
  3133. //if (isPrechargeStatus_gb(gunIndex) > 5 && isPrechargeStatus_gb(gunIndex) < 9) {
  3134. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3135. isPrechargeStatus_gb(gunIndex) > 6) {
  3136. setChargerMode(gunIndex, MODE_CHARGING);
  3137. }
  3138. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3139. // GFD 錯誤停止
  3140. RecordAlarmCode(gunIndex, "012236");
  3141. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  3142. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3143. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3144. // GFD 警告
  3145. //RecordAlarmCode(gunIndex, "012298");
  3146. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3147. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  3148. }
  3149. else {
  3150. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3151. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3152. }
  3153. break;
  3154. case _Type_CCS_2:
  3155. // 檢查樁端的 GFD 結果
  3156. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3157. (pDcChargingInfo->GroundFaultStatus == GFD_PASS ||
  3158. pDcChargingInfo->GroundFaultStatus == GFD_WARNING)
  3159. ) {
  3160. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP0);
  3161. }
  3162. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3163. // GFD 錯誤停止
  3164. RecordAlarmCode(gunIndex, "012235");
  3165. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3166. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3167. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3168. // GFD 警告
  3169. //RecordAlarmCode(gunIndex, "012297");
  3170. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3171. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  3172. }
  3173. else {
  3174. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3175. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3176. }
  3177. break;
  3178. }
  3179. }
  3180. static void checkEvBoardReqStop(uint8_t sysStatus, uint8_t gunIndex)
  3181. {
  3182. uint8_t evBoardStopState = 0;
  3183. if ((evBoardStopState = isEvBoardStopChargeFlag(gunIndex)) > 0) {
  3184. // 板端要求停止 (錯誤)
  3185. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3186. sysStatus != S_CCS_PRECHARGE_ST1) {
  3187. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3188. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3189. }
  3190. }
  3191. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  3192. log_info("EV Board Stop Charging with some error");
  3193. ChargingAlarmProcess(gunIndex);
  3194. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  3195. log_info("Power Cabient Stop Charging");
  3196. ChargingTerminalProcess(gunIndex);
  3197. }
  3198. } else if (isEvBoardNormalStopChargeFlag(gunIndex) == YES) {
  3199. // 板端要求停止 (正常)
  3200. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3201. sysStatus != S_CCS_PRECHARGE_ST1) {
  3202. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3203. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3204. }
  3205. }
  3206. log_info("EV Board Normal Stop Charging");
  3207. ChargingTerminalProcess(gunIndex);
  3208. } else if (OcppRemoteStop(gunIndex) == YES ||
  3209. WifiScheduleStop(gunIndex)) {
  3210. // 後臺要求停止
  3211. log_info("OCPP Stop Charging");
  3212. ChargingTerminalProcess(gunIndex);
  3213. }
  3214. }
  3215. static void checkOCPPReqStop(uint8_t gunIndex)
  3216. {
  3217. if (OcppRemoteStop(gunIndex) == YES ||
  3218. WifiScheduleStop(gunIndex) ||
  3219. CheckBackendChargingTimeout(gunIndex) ||
  3220. CheckBackendChargingEnergy(gunIndex) ||
  3221. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  3222. // 後臺要求停止
  3223. ChargingTerminalProcess(gunIndex);
  3224. }
  3225. }
  3226. void ResetIdleData(uint8_t gunIndex)
  3227. {
  3228. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3229. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3230. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3231. pDcChargingInfo->PresentChargedDuration = 0;
  3232. pDcChargingInfo->RemainChargingDuration = 0;
  3233. pDcChargingInfo->PresentChargingVoltage = 0;//DS60-120 add
  3234. pDcChargingInfo->PresentChargingCurrent = 0;//DS60-120 add
  3235. strcpy((char *)pDcChargingInfo->StartDateTime, "");
  3236. strcpy((char *)pDcChargingInfo->StopDateTime, "");
  3237. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3238. strcpy((char *)pSysConfig->UserId, "");
  3239. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "");
  3240. ClearDetectPluginFlag();
  3241. //Jerry add
  3242. memset(&ShmSelectGunInfo->PricesInfo[gunIndex], 0, sizeof(PricesInfo));
  3243. ShmSelectGunInfo->PricesInfo[gunIndex].Balance = FAIL_BALANCE_PRICES;
  3244. destroySelGun(gunIndex);
  3245. ResetDetAlarmStatus(gunIndex); //recovery OVP status code
  3246. if (ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] == YES) {
  3247. ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] = NO;
  3248. }
  3249. //strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "");
  3250. ReleaseAlarmCode(gunIndex);
  3251. pSysInfo->SystemPage = _LCM_VIEW;
  3252. }
  3253. void CheckErrorCode(uint8_t gunIndex)
  3254. {
  3255. struct ChargingInfoData *pSelectedDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  3256. if (gunIndex == pSysInfo->CurGunSelected) {
  3257. //pSysInfo->SystemPage = _LCM_ERROR;
  3258. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3259. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton)
  3260. pSysInfo->SystemPage = _LCM_EMERGENCY;
  3261. } else if (pSelectedDcChargingInfo->SystemStatus != S_IDLE &&
  3262. pSelectedDcChargingInfo->SystemStatus != S_RESERVATION &&
  3263. pSelectedDcChargingInfo->SystemStatus != S_MAINTAIN &&
  3264. pSelectedDcChargingInfo->SystemStatus != S_FAULT) {
  3265. if (pSelectedDcChargingInfo->SystemStatus == S_CHARGING) {
  3266. pSysInfo->SystemPage = _LCM_SUMMARY;
  3267. } else {
  3268. systemPageRestoreInit();
  3269. }
  3270. }
  3271. ClearDetectPluginFlag();
  3272. if (pDcChargingInfo->SystemStatus != S_FAULT)
  3273. {
  3274. UpdateErrorCodeToOcpp(gunIndex);
  3275. setChargerMode(gunIndex, MODE_FAULT);
  3276. }
  3277. }
  3278. void AuthorizeStopCharging(uint8_t gunIndex)
  3279. {
  3280. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3281. if ( pSysInfo->SystemPage == _LCM_STOP_APP ) {
  3282. checkOCPPReqStop(gunIndex);
  3283. }
  3284. else if (pSysInfo->SystemPage == _LCM_STOP_RFID ){
  3285. if (!RfidStopCharging() && strlen((char *)pSysConfig->UserId) > 0) {
  3286. // Different Card
  3287. log_info("RFID Authorize fail.");
  3288. strcpy((char *)pSysConfig->UserId, "");
  3289. StopSystemTimeoutDet();
  3290. pSysInfo->SystemPage = _LCM_STOP_RFID_FAIL;
  3291. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  3292. }
  3293. }
  3294. }
  3295. void showversion()
  3296. {
  3297. uint8_t index = 0;
  3298. log_info("\t=========== Dispenser info =========== ");
  3299. log_info("\tSW Version = %s", fwVersion);
  3300. log_info("\tDebug Version = %s",DebugVersion);
  3301. log_info("\tModelName = %s", pSysConfig->ModelName);
  3302. log_info("\tRelay Board FW Rev = %s", pSysInfo->RelayModuleFwRev);
  3303. log_info("\tFan Board FW Rev = %s", pSysInfo->FanModuleFwRev);
  3304. log_info("\tPrimary FW Rev = %s", pSysInfo->CsuPrimFwRev);
  3305. log_info("\tLCM HW Rev = %s", pSysInfo->LcmHwRev);
  3306. log_info("\tLED FW Rev = %s", pSysInfo->LedModuleFwRev);
  3307. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  3308. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3309. if (pDcChargingInfo->Type == _Type_Chademo) {
  3310. log_info("\tCHAdeMO[%d] FW Rev = %s",
  3311. pDcChargingInfo->type_index,
  3312. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].version);
  3313. } else if (pDcChargingInfo->Type == _Type_GB) {
  3314. log_info("\tGBT[%d] FW Rev = %s",
  3315. pDcChargingInfo->type_index,
  3316. ShmGBTData->evse[pDcChargingInfo->type_index].version);
  3317. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3318. log_info("\tCCS[%d] FW Rev = %s",
  3319. pDcChargingInfo->type_index,
  3320. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].version);
  3321. }
  3322. }
  3323. log_info("\t==================================== ");
  3324. }
  3325. int main(void)
  3326. {
  3327. bool isModelNameMatch = true;
  3328. uint8_t _ocppProfileChkFlag;
  3329. uint8_t gunIndex = 0;
  3330. if (CreateAllCsuShareMemory() == FAIL) {
  3331. log_error("create share memory error");
  3332. return FAIL;
  3333. }
  3334. ClearAllShmMemParameter();
  3335. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  3336. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  3337. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  3338. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  3339. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  3340. pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  3341. ShmPsuData = (struct PsuData *)GetShmPsuData();
  3342. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  3343. ShmGBTData = (struct GBTData *)GetShmGBTData();
  3344. ShmCcsData = (struct CcsData *)GetShmCcsData();
  3345. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  3346. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  3347. ShmRelayModuleData = (struct RelayModuleData *)GetShmRelayModuleData();
  3348. ShmLedModuleData = (struct LedModuleData *)GetShmLedModuleData();
  3349. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3350. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  3351. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3352. ShmSysConfigAndInfo = (struct SysConfigAndInfo *)GetShmSysConfigAndInfo();
  3353. ShmStatusCodeData = (struct StatusCodeData *)GetShmStatusCodeData();
  3354. log_info(" ********************************************************");
  3355. log_info(" ****************** FileSystem Boot up *****************");
  3356. log_info(" ********************************************************");
  3357. log_info(" ****************** Project:DD360UCar *****************");
  3358. log_info(" ********************************************************");
  3359. if (!InitialSystemDefaultConfig()) {
  3360. log_error("InitialSystemDefaultConfig NG ");
  3361. //StopProcessingLoop();
  3362. sleep(5);
  3363. system("reboot -f");
  3364. }
  3365. CheckGunTypeFromHw();
  3366. CheckIsAlternatvieByModelName();
  3367. InitialShareMemoryInfo();
  3368. GetFirmwareVersion();
  3369. if (!CheckConnectorTypeStatus()) {
  3370. isModelNameMatch = false;
  3371. }
  3372. Initialization();
  3373. SpawnTask();
  3374. log_info("Spawned all Task");
  3375. if (!isModelNameMatch) {
  3376. pAlarmCode->AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3377. //ChangeLcmByIndex(_LCM_ERROR);
  3378. ChangeLcmByIndex(_LCM_MAINTAIN);
  3379. // Module Name 與硬體對應不正確
  3380. log_error("Module Name & HW info none match. ");
  3381. sleep(3);
  3382. KillAllTask();
  3383. StopProcessingLoop();
  3384. }
  3385. CreateTimeoutFork();
  3386. log_info("Start self test... ");
  3387. SelfTestRun();
  3388. StopSystemTimeoutDet();
  3389. log_info("Self test finished : SelfTestSeq = %d, Work_Step = %d ",
  3390. pSysInfo->SelfTestSeq,
  3391. ShmPsuData->Work_Step);
  3392. if (pSysInfo->SelfTestSeq == _STEST_FAIL ||
  3393. ShmPsuData->Work_Step == _NO_WORKING ||
  3394. pInfoCode->InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES //DS60-120 add
  3395. ) {
  3396. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3397. setChargerMode(gunIndex, MODE_MAINTAIN);
  3398. }
  3399. ChangeLcmByIndex(_LCM_MAINTAIN);
  3400. } else {
  3401. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3402. setChargerMode(gunIndex, MODE_IDLE);
  3403. }
  3404. }
  3405. // Local DB
  3406. if (DB_Open() != PASS) {
  3407. log_info("DB_Open fail. ");
  3408. isDb_ready = false;
  3409. } else {
  3410. isDb_ready = true;
  3411. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3412. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  3413. pDcChargingInfo->IsAvailable = DB_Get_Operactive(_index);
  3414. }
  3415. DB_Reboot_Record();
  3416. }
  3417. log_info("===== Create DB End ===== ");
  3418. ChangeLcmByIndex(_LCM_VIEW);
  3419. sleep(1);
  3420. //***** 須新增的偵測 *****//
  3421. // 1. Thernal - 控制風扇轉速
  3422. // 2. ouput fuse - 控制風扇轉速
  3423. CreateRfidFork();
  3424. // Main loop
  3425. showversion();
  3426. CheckFwSlotStatusLog();
  3427. //AdjustChargerCurrent();
  3428. gettimeofday(&_cmdMainPriority_time, NULL);
  3429. GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  3430. CheckTaskAlive();
  3431. CreateWatchdog();
  3432. for (;;) {
  3433. CheckOcppStatus();
  3434. ChkPrimaryStatus();
  3435. if ((IsConnectorWholeIdle() ||
  3436. //(pSysInfo->PageIndex == _LCM_ERROR)) &&
  3437. (pSysInfo->PageIndex == _LCM_MAINTAIN)) &&
  3438. (pSysInfo->SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3439. ) {
  3440. CheckFactoryConfigFunction();
  3441. //CheckFwUpdateFunction();
  3442. }
  3443. // OCPP 邏輯
  3444. OcppRemoteStartChk();
  3445. // 當 AC 沒有搭上時,清除一些錯誤碼
  3446. ClearAlarmCodeWhenAcOff();
  3447. /*
  3448. if (pGunIndexInfo->AcGunIndex > 0 && isDetectPlugin() && !GetIsCardScan()) {
  3449. // AUDI_LCM_CHANGE
  3450. pSysInfo->SystemPage = _LCM_WAIT_PLUGIN;
  3451. }*/
  3452. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  3453. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3454. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3455. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3456. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3457. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3458. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3459. if (pDcChargingInfo->SystemStatus == S_CHARGING &&
  3460. _ocppProfileChkFlag == 12) {
  3461. ChargingProfileFlat(gunIndex);
  3462. _ocppProfileChkFlag = 0;
  3463. } else if (pDcChargingInfo->SystemStatus != S_CHARGING) {
  3464. ChargingProfileFlat(gunIndex);
  3465. _ocppProfileChkFlag = 0;
  3466. } else {
  3467. _ocppProfileChkFlag++;
  3468. }
  3469. }
  3470. checkGunOTPState(gunIndex); //check gun OTP
  3471. }
  3472. gettimeofday(&_cmdMainPriority_time, NULL);
  3473. }
  3474. gEvBoardErr.GunErrMessage = 0; //清除系統執行中的錯誤訊息
  3475. gChillerTempErr.TempErrMsg = 0;//清除系統執行中的錯誤訊息
  3476. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3477. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3478. // 重新收集各槍的錯誤狀態
  3479. collectError(gunIndex);
  3480. checkEvBoardAlarmState(pDcChargingInfo->Type);
  3481. }
  3482. checkChillerAlarmState();
  3483. // 確認當前錯誤 Level = 2 ?
  3484. ReviewCriticalAlarm();
  3485. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3486. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3487. CheckGpioInStatus();
  3488. CheckErrorOccurStatus(gunIndex);
  3489. // 確認 Relay Welding or Driving Fault
  3490. CheckRelayWeldingOrDrivingFault(gunIndex);
  3491. ChkOcppStatus(gunIndex);
  3492. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3493. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3494. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3495. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3496. CheckSmartChargeProfile(gunIndex);
  3497. }
  3498. switch (pDcChargingInfo->SystemStatus) {
  3499. case S_IDLE:
  3500. if (isModeChange(gunIndex)) {
  3501. log_info("============================= S_IDLE(%x) ============================= ", gunIndex);
  3502. ResetIdleData(gunIndex);
  3503. }
  3504. isChargingAverageState();
  3505. goto CheckStatus;
  3506. break;
  3507. case S_RESERVATION:
  3508. if (isModeChange(gunIndex)) {
  3509. log_info("============================= S_RESERVATION(%x) ============================= ", gunIndex);
  3510. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3511. }
  3512. if (!isReservationExpired(gunIndex)) {
  3513. pDcChargingInfo->SystemStatus = S_IDLE;
  3514. }
  3515. goto CheckStatus;
  3516. break;
  3517. case S_FAULT:
  3518. if (isModeChange(gunIndex)) {
  3519. if(pDcChargingInfo->SystemStatus == S_FAULT) {
  3520. log_info("============================= S_FAULT(%x) ============================= ", gunIndex);
  3521. //pSysInfo->SystemPage = _LCM_ERROR;
  3522. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3523. }
  3524. }
  3525. CheckStatus:
  3526. if (pDcChargingInfo->IsAvailable == NO) {
  3527. setChargerMode(gunIndex, MODE_MAINTAIN);
  3528. break;
  3529. }
  3530. // Refresh Error Status
  3531. if (pSysWarning->Level == WARN_LV_ER) {
  3532. CheckErrorCode(gunIndex);
  3533. break;;
  3534. }
  3535. if (PrecheckIsPass(gunIndex) == false) {
  3536. log_error("Relay welding");
  3537. setChargerMode(gunIndex, MODE_FAULT);
  3538. if (gunIndex == pSysInfo->CurGunSelected) {
  3539. //pSysInfo->SystemPage = _LCM_ERROR;
  3540. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3541. }
  3542. break;
  3543. }
  3544. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3545. systemPageRestoreInit();
  3546. setChargerMode(gunIndex, MODE_IDLE);
  3547. }
  3548. break;
  3549. case S_AUTHORIZING:
  3550. if (isModeChange(gunIndex)) {
  3551. log_info("============================= S_AUTHORIZING(%x) ============================= ", gunIndex);
  3552. }
  3553. // 讀卡邏輯
  3554. ScannerCardProcess();
  3555. // 隨插即充
  3556. autoStartCharging(gunIndex);
  3557. if (isDetectPlugin() && pSysInfo->SystemPage != _LCM_LINK_ERROR) {
  3558. // 卡號驗證成功後,等待充電槍插入充電車
  3559. if (pDcChargingInfo->RemoteStartFlag == YES) {
  3560. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3561. pDcChargingInfo->IsAvailable) {
  3562. log_info("-------- Remote Start(%d) --------", gunIndex);
  3563. pDcChargingInfo->RemoteStartFlag = NO;
  3564. pDcChargingInfo->isRemoteStart = YES; //DS60-120
  3565. ChangeGunSelectByIndex(gunIndex);
  3566. //AddPlugInTimes(gunIndex);
  3567. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3568. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3569. ClearDetectPluginFlag();
  3570. continue;
  3571. }
  3572. } else if (pSysInfo->OrderCharging == NO_DEFINE) {
  3573. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3574. pDcChargingInfo->IsAvailable &&
  3575. pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  3576. //waitSelectGunPlugIt(gunIndex)
  3577. (waitRightGunPlugIt(gunIndex) == PASS ||
  3578. waitLeftGunPlugIt(gunIndex) == PASS)
  3579. ) {
  3580. log_info("-------- RFID/Auto Start(%d) --------", gunIndex);
  3581. ChangeGunSelectByIndex(gunIndex);
  3582. //AddPlugInTimes(gunIndex);
  3583. pDcChargingInfo->isRemoteStart = NO;
  3584. strcpy((char *)pDcChargingInfo->StartUserId, (char *)pSysConfig->UserId);
  3585. log_info("index = %d, CardNumber = %s ",
  3586. gunIndex,
  3587. pDcChargingInfo->StartUserId);
  3588. strcpy((char *)pSysConfig->UserId, "");
  3589. // 當前操作的槍號,進入 Preparing
  3590. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3591. ClearDetectPluginFlag();
  3592. continue;
  3593. }
  3594. }
  3595. if (!GetIsCardScan() && pSysInfo->SystemPage != _LCM_LINK_ERROR) {
  3596. pSysInfo->SystemPage = _LCM_WAIT_PLUGIN;
  3597. }
  3598. }
  3599. break;
  3600. case S_REASSIGN_CHECK: {
  3601. if (isModeChange(gunIndex)) {
  3602. log_info("============================= S_REASSIGN_CHECK(%x) ============================= ", gunIndex);
  3603. if (pSysInfo->OrderCharging != NO_DEFINE) {
  3604. pSysInfo->OrderCharging = NO_DEFINE;
  3605. }
  3606. StopSystemTimeoutDet();
  3607. }
  3608. setChargerMode(gunIndex, S_PREPARNING);
  3609. if (pSysInfo->CurGunSelected == gunIndex) {
  3610. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3611. }
  3612. }
  3613. break;
  3614. case S_PREPARNING:
  3615. if (isModeChange(gunIndex)) {
  3616. log_info("============================= S_PREPARNING(%x) ============================= ", gunIndex);
  3617. StopGunInfoTimeoutDet(gunIndex);
  3618. StartGunInfoTimeoutDet(gunIndex, Timeout_Preparing);
  3619. }
  3620. if (ShmPsuData->SystemPresentPsuQuantity > 0 &&
  3621. ShmPsuData->SystemAvailablePower > 10 &&
  3622. GetTimeoutValue(pDcChargingInfo->TimeoutTimer) >= 5000000) {
  3623. setChargerMode(gunIndex, MODE_PREPARE_FOR_EV);
  3624. }
  3625. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3626. if (pSysInfo->CurGunSelected == gunIndex) {
  3627. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3628. }
  3629. break;
  3630. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  3631. if (isModeChange(gunIndex)) {
  3632. log_info("============================= S_PREPARING_FOR_EV(%x) ============================= ", gunIndex);
  3633. log_info("Waitting for Gun locked.");
  3634. StopGunInfoTimeoutDet(gunIndex);
  3635. StartGunInfoTimeoutDet(gunIndex, Timeout_EvChargingDet);
  3636. }
  3637. if (pDcChargingInfo->Type == _Type_Chademo) {
  3638. if (isEvGunLocked_chademo(gunIndex) == YES) {
  3639. log_info("Gun Locked");
  3640. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  3641. }
  3642. } else if (pDcChargingInfo->Type == _Type_GB) {
  3643. if (isEvGunLocked_gb(gunIndex) == YES) {
  3644. log_info("Gun Locked");
  3645. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  3646. }
  3647. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3648. if (isEvGunLocked_ccs(gunIndex) == YES) {
  3649. log_info("Gun Locked");
  3650. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  3651. }
  3652. }
  3653. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3654. if (pSysInfo->CurGunSelected == gunIndex) {
  3655. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3656. }
  3657. break;
  3658. case S_PREPARING_FOR_EVSE: // Ground fault test, 等待 Relay Board 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  3659. if (isModeChange(gunIndex)) {
  3660. log_info("============================= S_PREPARING_FOR_EVSE(%x) ============================= ", gunIndex);
  3661. StopGunInfoTimeoutDet(gunIndex);
  3662. StartGunInfoTimeoutDet(gunIndex, Timeout_EvseChargingDet);
  3663. }
  3664. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  3665. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3666. if (pSysInfo->CurGunSelected == gunIndex) {
  3667. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3668. }
  3669. break;
  3670. case S_CCS_PRECHARGE_ST0:
  3671. if (isModeChange(gunIndex)) {
  3672. log_info("============================= CCS Precharge Processing 1(%x) ============================= ", gunIndex);
  3673. StopGunInfoTimeoutDet(gunIndex);
  3674. StartGunInfoTimeoutDet(gunIndex, Timeout_ForCcsPrechargeDet);
  3675. }
  3676. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3677. // GFD 錯誤停止
  3678. RecordAlarmCode(gunIndex, "012235");
  3679. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3680. }
  3681. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3682. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  3683. // 切換 D+ Relay to Precharge Relay
  3684. if (isPrechargeStatus_ccs(gunIndex) == 39 ||
  3685. isPrechargeStatus_ccs(gunIndex) == 40) {
  3686. if ((pDcChargingInfo->RelayKPK2Status == YES || pDcChargingInfo->PantographFlag == YES) &&
  3687. pDcChargingInfo->PrechargeStatus != PRECHARGE_READY)
  3688. //if (pDcChargingInfo->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  3689. {
  3690. //log_info("Send precharge ready 1..........%x, status = %d ",
  3691. // gunIndex,
  3692. // isPrechargeStatus_ccs(gunIndex));
  3693. if (isPrechargeStatus_ccs(gunIndex) == 39) {
  3694. log_info("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) ",
  3695. gunIndex,
  3696. isPrechargeStatus_ccs(gunIndex));
  3697. } else if (isPrechargeStatus_ccs(gunIndex) == 40) {
  3698. log_info("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) ",
  3699. gunIndex,
  3700. isPrechargeStatus_ccs(gunIndex));
  3701. }
  3702. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  3703. }
  3704. } else if (isPrechargeStatus_ccs(gunIndex) == 45 ||
  3705. isPrechargeStatus_ccs(gunIndex) == 46
  3706. ) {
  3707. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP1);
  3708. }
  3709. if (pSysInfo->CurGunSelected == gunIndex) {
  3710. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3711. }
  3712. break;
  3713. case S_CCS_PRECHARGE_ST1:
  3714. if (isModeChange(gunIndex)) {
  3715. log_info("============================= CCS Precharge Processing 2(%x) ============================= ", gunIndex);
  3716. }
  3717. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3718. // GFD 錯誤停止
  3719. RecordAlarmCode(gunIndex, "012235");
  3720. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3721. }
  3722. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3723. // 等待小板通知進入充電
  3724. // 切換 D+ Relay to Precharge Relay
  3725. if (pDcChargingInfo->RelayK1K2Status == YES || pDcChargingInfo->PantographFlag == YES) {
  3726. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  3727. setChargerMode(gunIndex, MODE_CHARGING);
  3728. }
  3729. if (pSysInfo->CurGunSelected == gunIndex) {
  3730. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3731. }
  3732. break;
  3733. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  3734. if (isModeChange(gunIndex)) {
  3735. log_info("============================= S_CHARGING(%x) ============================= ", gunIndex);
  3736. StopGunInfoTimeoutDet(gunIndex);
  3737. ftime(&startChargingTime[gunIndex]);
  3738. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, ""); //DS60-120 add
  3739. ChangeStartOrStopDateTime(YES, gunIndex);
  3740. pSysInfo->SystemPage = _LCM_DETAIL_VIEW;
  3741. StartGunInfoTimeoutDet(gunIndex,Timeout_DetailView);
  3742. }
  3743. if (ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf) {
  3744. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf = NO;
  3745. }
  3746. ftime(&endChargingTime[gunIndex]);
  3747. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  3748. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  3749. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3750. // Check Stop Charging
  3751. AuthorizeStopCharging(gunIndex);
  3752. // LCM => Charging
  3753. /*
  3754. if (pSysInfo->SystemPage != _LCM_DETAIL_VIEW) {
  3755. pSysInfo->SystemPage = _LCM_VIEW;
  3756. }
  3757. */
  3758. break;
  3759. case S_ALARM:
  3760. if (isModeChange(gunIndex)) {
  3761. log_info("============================= S_ALARM(%x) ============================= ", gunIndex);
  3762. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3763. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  3764. }
  3765. UpdateErrorCodeToOcpp(gunIndex);
  3766. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  3767. OcppStopTransation(gunIndex);
  3768. }
  3769. TheEndCharging(gunIndex);
  3770. StopGunInfoTimeoutDet(gunIndex);
  3771. //pSysInfo->SystemPage = _LCM_ERROR;
  3772. }
  3773. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3774. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton)
  3775. pSysInfo->SystemPage = _LCM_EMERGENCY;
  3776. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  3777. GetTimeoutValue(pDcChargingInfo->TimeoutTimer) >= 10000000) {
  3778. setChargerMode(gunIndex, MODE_IDLE);
  3779. }
  3780. break;
  3781. case S_TERMINATING:
  3782. if (isModeChange(gunIndex)) {
  3783. log_info("============================= S_TERMINATING(%x) ============================= ", gunIndex);
  3784. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3785. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  3786. }
  3787. StopGunInfoTimeoutDet(gunIndex);
  3788. StartSystemTimeoutDet(Timeout_Terminating);
  3789. }
  3790. // For Precharging timeout
  3791. if (pDcChargingInfo->Type == _Type_Chademo) {
  3792. if (isEvStopCharging_chademo(gunIndex) == YES ||
  3793. isPrechargeStatus_chademo(gunIndex) <= 0) {
  3794. setChargerMode(gunIndex, MODE_COMPLETE);
  3795. }
  3796. } else if (pDcChargingInfo->Type == _Type_GB) {
  3797. if (isEvStopCharging_gb(gunIndex) == YES ||
  3798. isPrechargeStatus_gb(gunIndex) <= 0) {
  3799. setChargerMode(gunIndex, MODE_COMPLETE);
  3800. }
  3801. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3802. if (isEvStopCharging_ccs(gunIndex) == YES &&
  3803. (isPrechargeStatus_ccs(gunIndex) >= 53 ||
  3804. isPrechargeStatus_ccs(gunIndex) == 0 ||
  3805. isPrechargeStatus_ccs(gunIndex) == 13 ||
  3806. isPrechargeStatus_ccs(gunIndex) == 14)
  3807. ) {
  3808. setChargerMode(gunIndex, MODE_COMPLETE);
  3809. }
  3810. }
  3811. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  3812. if (pSysInfo->SystemPage == _LCM_MAINTAIN) {
  3813. break;
  3814. }
  3815. if (pSysInfo->CurGunSelected == gunIndex) {
  3816. pSysInfo->SystemPage = _LCM_STOPPING;
  3817. }
  3818. break;
  3819. case S_COMPLETE:
  3820. if (isModeChange(gunIndex)) {
  3821. log_info ("============================= S_COMPLETE(%x) ============================= ", gunIndex);
  3822. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  3823. OcppStopTransation(gunIndex);
  3824. }
  3825. TheEndCharging(gunIndex);
  3826. StopSystemTimeoutDet();
  3827. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  3828. if (pDcChargingInfo->ConnectorPlugIn)
  3829. pSysInfo->SystemPage = _LCM_WAIT_PLUGOUT;
  3830. else
  3831. pSysInfo->SystemPage = _LCM_SUMMARY;
  3832. }
  3833. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  3834. if (pSysInfo->SystemPage == _LCM_MAINTAIN) {
  3835. StopSystemTimeoutDet();
  3836. break;
  3837. }
  3838. if (pSysInfo->CurGunSelected == gunIndex) {
  3839. if (pDcChargingInfo->ConnectorPlugIn) {
  3840. if (pSysInfo->SystemPage != _LCM_SUMMARY)
  3841. pSysInfo->SystemPage = _LCM_WAIT_PLUGOUT;
  3842. } else {
  3843. if (pSysInfo->SystemPage == _LCM_WAIT_PLUGOUT) {
  3844. StopSystemTimeoutDet();
  3845. pSysInfo->SystemPage = _LCM_SUMMARY;
  3846. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  3847. }
  3848. }
  3849. }
  3850. break;
  3851. case S_MAINTAIN:
  3852. if (isModeChange(gunIndex)) {
  3853. log_info("============================= S_MAINTAIN(%x) ============================= ", gunIndex);
  3854. if (pSysInfo->FirmwareUpdate == YES)
  3855. continue;
  3856. if (pDcChargingInfo->IsAvailable == NO )
  3857. continue;
  3858. if (pSysWarning->Level != WARN_LV_ER) {
  3859. if (!DisplaySelfTestFailReason()) { //DS60-120 add
  3860. log_info("Soft reboot for retry self-tets. ");
  3861. sleep(3);
  3862. system("killall OcppBackend &");
  3863. KillAllTask();
  3864. system("/usr/bin/run_evse_restart.sh");
  3865. }
  3866. }
  3867. sleep(3);
  3868. if (pSysWarning->Level == WARN_LV_ER) { //DS60-120 add
  3869. KillTaskExceptPrimary();
  3870. } else {
  3871. KillTask();
  3872. }
  3873. if (pSysInfo->SelfTestSeq == _STEST_FAIL)
  3874. StopProcessingLoop();
  3875. }
  3876. break;
  3877. }//switch
  3878. }//for
  3879. //if (pSysInfo->SystemPage != _LCM_VIEW)
  3880. //ChangeLcmByIndex(pSysInfo->SystemPage);
  3881. TryFeedWatchdog();
  3882. usleep(WHILE_LOOP_TIME);
  3883. }
  3884. return FAIL;
  3885. }