main.c 293 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2020年9月10日
  5. * Author: Wendell
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/mman.h>
  17. #include <linux/wireless.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> /*標準輸入輸出定義*/
  23. #include <stdlib.h> /*標準函數庫定義*/
  24. #include <unistd.h> /*Unix 標準函數定義*/
  25. #include <fcntl.h> /*檔控制定義*/
  26. #include <termios.h> /*PPSIX 終端控制定義*/
  27. #include <errno.h> /*錯誤號定義*/
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "../../define.h"
  36. #include "Config.h"
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. #include "timeout.h"
  40. #include "Module_EvComm.h"
  41. #include "Module_CabinetParallel.h"
  42. #include "Common.h"
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define MtdBlockSize 0x300000
  47. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  48. #define UPGRADE_FAN 0x02
  49. #define UPGRADE_RB 0x03
  50. #define UPGRADE_PRI 0x04
  51. #define UPGRADE_AC 0x05
  52. #define UPGRADE_LED 0x06
  53. #define UPGRADE_DORC1 0x07
  54. #define UPGRADE_DORC2 0x08
  55. #define UPGRADE_DDRB 0x09
  56. #define SYSTEM_MIN_VOL 150
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define MAIN_PRIORITY_INTERVAL 5
  63. #define CABINET_ROLE_TIMEOUT 3 // unit: 1s
  64. #define GUN_PREPARE_TIMEOUT 30
  65. #define GUN_EV_WAIT_TIMEOUT (120 + 10)
  66. #define GUN_EVSE_WAIT_TIMEOUT (60 + 10)
  67. #define GUN_COMP_WAIT_TIMEOUT 10
  68. #define GUN_PRECHARGING_TIMEOUT 60
  69. #define REBOOT_TIMEOUT 10
  70. #define RESET_TIMEOUT 10
  71. #define OCPP_COST_REQ_INTERVAL 5
  72. #define DISPENSER_RECONNECTE_TIME 30
  73. #define DISPENSER_AUTH_COMP_TIME 3
  74. #define FORCE_BALANCE_TIME 5
  75. #define DISPENSER_UPGRADE_TIME 600
  76. #define AC_CONTACTOR_REOPEN_TIME 60
  77. #define PSU_FAILURE_RESUME_TIME 180
  78. #define MAX_AC_CONTACTOR_COUNT 3
  79. #define STANDBY_TIME 600
  80. #define FAST_STANDBY_TIME 30
  81. #define WIRING_INFO_DELAY 30
  82. #define WAIT_REASSIGN_TIME 30 // unit: second
  83. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  84. #define MIN_WAIT_PREPARNING_TIME 5 // unit: second
  85. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  86. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  87. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  88. #define ENERAGY_INTERVAL 1 // unit: second
  89. #define FAULT_RELEASE_TIME 1 // unit: second
  90. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  91. #define PARALLEL_WAIT_TIME 3 // unit: second
  92. #define START_UPGRADE_TIME 60 // unit: second
  93. #define PCABINET_UPGRADE_TIME 300 // unit: second
  94. #define MCABINET_UPGRADE_TIME 60 // unit: second
  95. #define AUTO_START_CHARGING "AutoStartCharging"
  96. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  97. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  98. int whileLoopTime = 10000; // 10 ms
  99. int wtdFd = -1;
  100. byte _authorizeIndex = NO_DEFINE;
  101. BOOL _UpgradeNeedReboot = FALSE;
  102. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  103. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  104. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  105. {
  106. {0, 1, 0, 0},
  107. {1, 0, 0, 0},
  108. {0, 0, 0, 0},
  109. {0, 0, 0, 0}
  110. };
  111. //Location: 1 2 3 4
  112. // Gun: 1 3 4 2
  113. //Parallel: 1 2 3
  114. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  115. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  116. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  117. //{
  118. // {0, 3, 1, 2}, // group 0
  119. // {1, 0, 2, 3}, // group 1
  120. // {1, 3, 0, 2}, // group 2
  121. // {1, 3, 2, 0} // group 3
  122. //};
  123. //Location: 1 2 3 4
  124. // Gun: 1 4 3 2
  125. //Parallel: 1 2 3
  126. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  127. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  128. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  129. {
  130. {0, 3, 2, 1}, // group 0
  131. {1, 0, 3, 2}, // group 1
  132. {1, 3, 0, 2}, // group 2
  133. {1, 3, 2, 0} // group 3
  134. };
  135. long long DiffTimebWithNow(struct timeb ST);
  136. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  137. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  138. void ChargingTerminalProcess(byte gunIndex);
  139. void ChkPrimaryStatus();
  140. void ChargingAlarmProcess(byte gunIndex);
  141. void InformOcppErrOccur(byte codeType);
  142. void SetOcppErrorCode(unsigned char _index, char *errCode);
  143. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  144. void ReleaseAlarmCode(byte gunIndex);
  145. int DB_Open(sqlite3 *db);
  146. int DB_Insert_Record(sqlite3 *db, int gun_index);
  147. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  148. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  149. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value);
  150. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index);
  151. int DB_Reboot_Record(sqlite3 *db);
  152. void InitialDHCP();
  153. void InitialDispenserDhcpServerConfig(void);
  154. void StartDispenserDhcpServer(void);
  155. int GetStartScheduleTime(unsigned char *time);
  156. void CheckConnectionTimeout(void);
  157. bool IsParallelCabinetIdle(void);
  158. bool IsConnectorWholeIdle(void);
  159. void SetAcContactor(unsigned char OnOff);
  160. void UpdateErrorCodeToOcpp(byte index);
  161. bool CheckConnectorTypeStatus(void);
  162. void CreateWatchdog(void);
  163. void TryCloseWatchdog(void);
  164. void TryFeedWatchdog(void);
  165. bool IsReadyParalleling(void);
  166. void SetParallelingStart(void);
  167. void SetParallelingDone(void);
  168. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  169. void SetParallelCabinetAcContactor(unsigned char OnOff);
  170. unsigned short GetTotalParallelCabinetPower(void);
  171. void MCabinetParallelingProcess(void);
  172. void SCabinetControllProcess(void);
  173. bool Get_Ocpp_ConnectionStatus(void);
  174. unsigned int Get_Ocpp_procDogTime(void);
  175. void Clean_Ocpp_procDogTime(void);
  176. bool Is_Ocpp_BootNotification(void);
  177. void Clean_Ocpp_BootNotification(void);
  178. bool Is_Ocpp_ResetReq(void);
  179. bool Chk_Ocpp_Reset_Type(char *type);
  180. void Clean_Ocpp_ResetReq(void);
  181. void Set_Ocpp_AuthorizeReq(void);
  182. bool Is_Ocpp_AuthorizeConf(void);
  183. bool Is_Ocpp_Authorize_Status(char *status);
  184. bool Is_Ocpp_UpdateFirmwareReq(void);
  185. void Clean_Ocpp_UpdateFirmwareReq(void);
  186. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  187. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  188. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  189. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  190. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  191. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  192. int Get_Ocpp_ConnectionTimeOut(void);
  193. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  194. void Get_Ocpp_UserPrice(char * userId, char *defaultPrice);
  195. void Clean_Ocpp_UserPrice(void);
  196. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  197. void Clean_Ocpp_RunningCost(int gun_index);
  198. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  199. void Clean_Ocpp_FinalCost(int gun_index);
  200. void Set_Ocpp_StartTransactionId(int gun_index, char *id);
  201. void Set_Ocpp_StartTransactionReq(int gun_index);
  202. bool Is_Ocpp_StartTransactionConf(int gun_index);
  203. void Clean_Ocpp_StartTransactionConf(int gun_index);
  204. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  205. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  206. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  207. bool Is_Invalid_StartTransactionId(int gun_index);
  208. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  209. void Set_Ocpp_StopTransactionReq(int gun_index);
  210. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  211. bool Is_Ocpp_ReserveNowReq(int gun_index);
  212. void Clean_Ocpp_ReserveNowReq(int gun_index);
  213. int Get_Ocpp_ReserveNowId(int gun_index);
  214. void Set_Ocpp_ReserveNowConf(int gun_index);
  215. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  216. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  217. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  218. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  219. bool Is_Ocpp_CancelReservationReq(int gun_index);
  220. void Clean_Ocpp_CancelReservationReq(int gun_index);
  221. void Set_Ocpp_CancelReservationConf(int gun_index);
  222. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  223. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  224. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  225. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  226. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  227. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  228. bool Is_Ocpp_RemoteStartReq(int gun_index);
  229. void Clean_Ocpp_RemoteStartReq(int gun_index);
  230. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  231. bool Is_Ocpp_RemoteStopReq(int gun_index);
  232. void Clean_Ocpp_RemoteStopReq(int gun_index);
  233. void Set_Ocpp_StopReason(int gun_index, char *reason);
  234. void Get_Ocpp_StopReason(int gun_index, char *reason);
  235. void Clean_Ocpp_StopReason(int gun_index);
  236. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  237. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  238. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  239. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  240. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  241. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  242. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  243. void Set_Ocpp_ChargingProfileReq(int gun_index);
  244. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  245. bool Is_Ocpp_ChargingProfileId(int gun_index);
  246. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  247. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  248. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  249. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  250. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  251. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  252. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  253. bool Is_Ocpp_MaxChargingProfileId(void);
  254. int Get_Ocpp_MaxProfileStartTime(void);
  255. int Get_Ocpp_MaxProfileScheduleCount(void);
  256. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  257. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  258. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  259. int Get_Ocpp_MaxProfileStartIndex(void);
  260. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  261. struct StatusCodeData *ShmStatusCodeData;
  262. struct PsuData *ShmPsuData;
  263. struct CHAdeMOData *ShmCHAdeMOData;
  264. struct GBTData *ShmGBTData;
  265. struct CcsData *ShmCcsData;
  266. struct PrimaryMcuData *ShmPrimaryMcuData;
  267. struct FanModuleData *ShmFanModuleData;
  268. struct RelayModuleData *ShmRelayModuleData[2];
  269. struct LedModuleData *ShmLedModuleData;
  270. struct OCPP16Data *ShmOCPP16Data;
  271. struct OCPP20Data *ShmOCPP20Data;
  272. struct MeterInformation *ShmCsuMeterData;
  273. ChargerInfoData *ShmChargerInfo;
  274. PsuPositionInfoData *ShmPsuPosition;
  275. PsuGroupingInfoData *ShmPsuGrouping;
  276. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  277. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  278. struct timespec _cmdMainPriority_time;
  279. struct timespec _cmdSubPriority_time;
  280. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  281. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  282. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  283. struct timespec _DispenserUpgrade_time;
  284. struct timespec _Standby_time;
  285. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  286. struct timespec _Parallel_Time;
  287. float gunOutputVol[CONNECTOR_QUANTITY];
  288. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  289. float _lastProfilePower[CONNECTOR_QUANTITY];
  290. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  291. float _lastMaxProfilePower;
  292. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  293. bool _NeedReset4gWifi;
  294. struct timespec _4gWifiReset_time;
  295. // for initial index to check EV board type is correct
  296. byte _gunIndex = 0;
  297. byte _acgunIndex = 0;
  298. byte _chademoIndex = 0;
  299. byte _ccsIndex = 0;
  300. byte _gb_Index = 0;
  301. byte _ac_Index = 0;
  302. bool isCardScan = false;
  303. bool isModelNameMatch = true;
  304. //int rfidFd = -1;
  305. //char* rfidPortName = "/dev/ttyS2";
  306. char* fwVersion = "V1.07.00.0000.00";
  307. char* subVersion = "00";
  308. sqlite3 *localDb;
  309. bool isDb_ready;
  310. sqlite3 *networkDb;
  311. //================================================
  312. // initial can-bus
  313. //================================================
  314. int InitCanBus()
  315. {
  316. int s0,nbytes;
  317. struct timeval tv;
  318. struct ifreq ifr0;
  319. struct sockaddr_can addr0;
  320. system("/sbin/ip link set can0 down");
  321. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  322. system("/sbin/ip link set can0 up");
  323. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  324. tv.tv_sec = 0;
  325. tv.tv_usec = 10000;
  326. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  327. {
  328. #ifdef SystemLogMessage
  329. LOG_ERROR("Set SO_RCVTIMEO NG");
  330. #endif
  331. }
  332. nbytes=40960;
  333. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  334. {
  335. #ifdef SystemLogMessage
  336. LOG_ERROR("Set SO_RCVBUF NG");
  337. #endif
  338. }
  339. nbytes=40960;
  340. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  341. {
  342. #ifdef SystemLogMessage
  343. LOG_ERROR("Set SO_SNDBUF NG");
  344. #endif
  345. }
  346. strcpy(ifr0.ifr_name, "can0" );
  347. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  348. addr0.can_family = AF_CAN;
  349. addr0.can_ifindex = ifr0.ifr_ifindex;
  350. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  351. return s0;
  352. }
  353. //================================================
  354. // initial uart port
  355. //================================================
  356. char *_priPortName = "/dev/ttyS1";
  357. char *_485PortName = "/dev/ttyS5";
  358. int InitComPort(byte target)
  359. {
  360. int fd;
  361. struct termios tios;
  362. if(target == UPGRADE_PRI)
  363. fd = open(_priPortName, O_RDWR);
  364. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  365. fd = open(_485PortName, O_RDWR);
  366. if(fd<=0)
  367. {
  368. #ifdef SystemLogMessage
  369. LOG_ERROR("open 407 Communication port NG");
  370. #endif
  371. return -1;
  372. }
  373. ioctl (fd, TCGETS, &tios);
  374. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  375. tios.c_lflag = 0;
  376. tios.c_iflag = 0;
  377. tios.c_oflag = 0;
  378. tios.c_cc[VMIN]=0;
  379. tios.c_cc[VTIME]=(unsigned char)1;
  380. tios.c_lflag=0;
  381. tcflush(fd, TCIFLUSH);
  382. ioctl (fd, TCSETS, &tios);
  383. return fd;
  384. }
  385. //=================================
  386. // Common routine
  387. //=================================
  388. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  389. {
  390. strncpy(dest, src + start, cnt);
  391. dest[cnt] = 0;
  392. }
  393. int InitWatchDog()
  394. {
  395. int fd;
  396. int timeout = 180;
  397. system("/usr/bin/fuser -k /dev/watchdog");
  398. sleep(1);
  399. system("echo V > /dev/watchdog");
  400. sleep(1);
  401. fd=open("/dev/watchdog", O_RDWR);
  402. if(fd<=0)
  403. {
  404. LOG_ERROR("System watch dog initial fail.\r\n");
  405. }
  406. ioctl(fd, _IOWR('W', 6, int), &timeout);
  407. return fd;
  408. }
  409. void setChargerMode(byte gun_index, byte mode)
  410. {
  411. chargingInfo[gun_index]->SystemStatus = mode;
  412. }
  413. long long DiffTimebWithNow(struct timeb ST)
  414. {
  415. //return milli-second
  416. struct timeb ET;
  417. long long StartTime,StopTime;
  418. ftime(&ET);
  419. StartTime=(long long)ST.time;
  420. StopTime=(long long)ET.time;
  421. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  422. }
  423. //=================================
  424. // Create all share memory
  425. //=================================
  426. int CreateShareMemory()
  427. {
  428. int MeterSMId;
  429. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  430. {
  431. #ifdef SystemLogMessage
  432. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  433. #endif
  434. return 0;
  435. }
  436. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  437. {
  438. #ifdef SystemLogMessage
  439. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  440. #endif
  441. return 0;
  442. }
  443. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  444. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  445. {
  446. #ifdef SystemLogMessage
  447. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  448. #endif
  449. return 0;
  450. }
  451. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  452. {
  453. #ifdef SystemLogMessage
  454. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  455. #endif
  456. return 0;
  457. }
  458. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  459. //creat ShmPsuData
  460. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  461. {
  462. #ifdef SystemLogMessage
  463. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  464. #endif
  465. return 0;
  466. }
  467. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  468. {
  469. #ifdef SystemLogMessage
  470. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  471. #endif
  472. return 0;
  473. }
  474. memset(ShmPsuData, 0, sizeof(struct PsuData));
  475. /*
  476. if(CHAdeMO_QUANTITY > 0)
  477. {
  478. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  479. {
  480. #ifdef SystemLogMessage
  481. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  482. #endif
  483. return 0;
  484. }
  485. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  486. {
  487. #ifdef SystemLogMessage
  488. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  489. #endif
  490. return 0;
  491. }
  492. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  493. }
  494. if(GB_QUANTITY > 0)
  495. {
  496. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  497. {
  498. #ifdef SystemLogMessage
  499. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  500. #endif
  501. return 0;
  502. }
  503. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  504. {
  505. #ifdef SystemLogMessage
  506. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  507. #endif
  508. return 0;
  509. }
  510. memset(ShmGBTData, 0, sizeof(struct GBTData));
  511. }
  512. //creat ShmCcsData
  513. if(CCS_QUANTITY > 0)
  514. {
  515. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  516. {
  517. #ifdef SystemLogMessage
  518. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  519. #endif
  520. return 0;
  521. }
  522. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  523. {
  524. #ifdef SystemLogMessage
  525. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  526. #endif
  527. return 0;
  528. }
  529. memset(ShmCcsData, 0, sizeof(struct CcsData));
  530. }
  531. */
  532. //creat ShmPrimaryMcuData
  533. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  534. {
  535. #ifdef SystemLogMessage
  536. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  537. #endif
  538. return 0;
  539. }
  540. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  541. {
  542. #ifdef SystemLogMessage
  543. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  544. #endif
  545. return 0;
  546. }
  547. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  548. //creat ShmFanModuleData
  549. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  550. {
  551. #ifdef SystemLogMessage
  552. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  553. #endif
  554. return 0;
  555. }
  556. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  557. {
  558. #ifdef SystemLogMessage
  559. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  560. #endif
  561. return 0;
  562. }
  563. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  564. //creat ShmRelayModuleData
  565. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  566. {
  567. #ifdef SystemLogMessage
  568. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  569. #endif
  570. return 0;
  571. }
  572. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  573. {
  574. #ifdef SystemLogMessage
  575. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  576. #endif
  577. return 0;
  578. }
  579. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  580. // DO360 RC2
  581. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  582. {
  583. #ifdef SystemLogMessage
  584. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  585. #endif
  586. return 0;
  587. }
  588. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  589. {
  590. #ifdef SystemLogMessage
  591. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  592. #endif
  593. return 0;
  594. }
  595. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  596. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  597. {
  598. #ifdef SystemLogMessage
  599. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  600. #endif
  601. return 0;
  602. }
  603. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  604. {
  605. #ifdef SystemLogMessage
  606. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  607. #endif
  608. return 0;
  609. }
  610. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  611. //creat ShmOCPP16Data
  612. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  613. {
  614. #ifdef SystemLogMessage
  615. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  616. #endif
  617. return 0;
  618. }
  619. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  620. {
  621. #ifdef SystemLogMessage
  622. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  623. #endif
  624. return 0;
  625. }
  626. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  627. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  628. {
  629. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  630. return 0;
  631. }
  632. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  633. {
  634. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  635. return 0;
  636. }
  637. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  638. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  639. {
  640. #ifdef SystemLogMessage
  641. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  642. #endif
  643. return 0;
  644. }
  645. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  646. {
  647. #ifdef SystemLogMessage
  648. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  649. #endif
  650. return 0;
  651. }
  652. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  653. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  654. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  655. return 1;
  656. }
  657. //======================================================
  658. // Peripheral initial
  659. //======================================================
  660. void InitGPIO()
  661. {
  662. /*****************0~3, 4 bank, bank x 32+ num*********************/
  663. /***************************************************************/
  664. /*************** GPIO 0 ***************************************/
  665. /***************************************************************/
  666. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  667. system("echo 22 > /sys/class/gpio/export");
  668. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  669. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  670. system("echo 23 > /sys/class/gpio/export");
  671. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  672. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  673. system("echo 26 > /sys/class/gpio/export");
  674. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  675. system("echo 1 > /sys/class/gpio/gpio26/value");
  676. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  677. system("echo 27 > /sys/class/gpio/export");
  678. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  679. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  680. system("echo 29 > /sys/class/gpio/export");
  681. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  682. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  683. system("echo 19 > /sys/class/gpio/export");
  684. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  685. system("echo 1 > /sys/class/gpio/gpio19/value");
  686. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  687. system("echo 20 > /sys/class/gpio/export");
  688. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  689. /***************************************************************/
  690. /*************** GPIO 1 ***************************************/
  691. /***************************************************************/
  692. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  693. system("echo 44 > /sys/class/gpio/export");
  694. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  695. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  696. system("echo 45 > /sys/class/gpio/export");
  697. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  698. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  699. system("echo 46 > /sys/class/gpio/export");
  700. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  701. system("echo 0 > /sys/class/gpio/gpio46/value");
  702. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  703. system("echo 47 > /sys/class/gpio/export");
  704. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  705. /***************************************************************/
  706. /*************** GPIO 2 ***************************************/
  707. /***************************************************************/
  708. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  709. system("echo 89 > /sys/class/gpio/export");
  710. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  711. system("echo 1 > /sys/class/gpio/gpio89/value");
  712. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  713. system("echo 87 > /sys/class/gpio/export");
  714. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  715. system("echo 0 > /sys/class/gpio/gpio87/value");
  716. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  717. system("echo 88 > /sys/class/gpio/export");
  718. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  719. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  720. system("echo 86 > /sys/class/gpio/export");
  721. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  722. /***************************************************************/
  723. /*************** GPIO 3 ***************************************/
  724. /***************************************************************/
  725. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  726. system("echo 111 > /sys/class/gpio/export");
  727. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  728. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  729. system("echo 114 > /sys/class/gpio/export");
  730. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  731. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  732. system("echo 113 > /sys/class/gpio/export");
  733. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  734. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  735. system("echo 110 > /sys/class/gpio/export");
  736. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  737. system("echo 0 > /sys/class/gpio/gpio110/value");
  738. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  739. system("echo 115 > /sys/class/gpio/export");
  740. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  741. system("echo 0 > /sys/class/gpio/gpio115/value");
  742. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  743. system("echo 112 > /sys/class/gpio/export");
  744. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  745. system("echo 1 > /sys/class/gpio/gpio112/value");
  746. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  747. system("echo 116 > /sys/class/gpio/export");
  748. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  749. system("echo 0 > /sys/class/gpio/gpio116/value");
  750. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  751. system("echo 103 > /sys/class/gpio/export");
  752. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  753. system("echo 0 > /sys/class/gpio/gpio103/value");
  754. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  755. system("echo 104 > /sys/class/gpio/export");
  756. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  757. system("echo 0 > /sys/class/gpio/gpio104/value");
  758. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  759. }
  760. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  761. {
  762. int fd, wrd;
  763. unsigned char *buf;
  764. unsigned int ChkSum, ChkSumOrg;
  765. if((buf = malloc(MtdBlockSize)) == NULL)
  766. {
  767. LOG_ERROR("malloc buffer NG, rebooting..\n");
  768. if(ShmStatusCodeData!=NULL)
  769. {
  770. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  771. }
  772. sleep(5);
  773. system("reboot -f");
  774. sleep(5);
  775. system("reboot -f");
  776. }
  777. memset(buf, 0, MtdBlockSize);
  778. //================================================
  779. // Load configuration from mtdblock10
  780. //================================================
  781. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  782. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  783. if(fd < 0)
  784. {
  785. free(buf);
  786. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  787. if(ShmStatusCodeData != NULL)
  788. {
  789. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  790. }
  791. sleep(5);
  792. system("reboot -f");
  793. sleep(5);
  794. system("reboot -f");
  795. }
  796. wrd = read(fd, buf, MtdBlockSize);
  797. close(fd);
  798. if(wrd < MtdBlockSize)
  799. {
  800. free(buf);
  801. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  802. if(ShmStatusCodeData != NULL)
  803. {
  804. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  805. }
  806. sleep(5);
  807. system("reboot -f");
  808. sleep(5);
  809. system("reboot -f");
  810. }
  811. ChkSum = 0;
  812. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  813. {
  814. ChkSum += buf[wrd];
  815. }
  816. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  817. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  818. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  819. //================================================
  820. // Load configuration from mtdblock11
  821. //================================================
  822. if(ChkSum != ChkSumOrg)
  823. {
  824. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  825. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  826. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  827. if(fd < 0)
  828. {
  829. free(buf);
  830. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  831. if(ShmStatusCodeData != NULL)
  832. {
  833. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  834. }
  835. sleep(5);
  836. system("reboot -f");
  837. sleep(5);
  838. system("reboot -f");
  839. }
  840. memset(buf, 0, MtdBlockSize);
  841. wrd = read(fd, buf, MtdBlockSize);
  842. close(fd);
  843. if(wrd < MtdBlockSize)
  844. {
  845. free(buf);
  846. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  847. if(ShmStatusCodeData != NULL)
  848. {
  849. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  850. }
  851. sleep(5);
  852. system("reboot -f");
  853. sleep(5);
  854. system("reboot -f");
  855. }
  856. ChkSum = 0;
  857. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  858. {
  859. ChkSum += buf[wrd];
  860. }
  861. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  862. //================================================
  863. // Load configuration from mtdblock12 (Factory default)
  864. //================================================
  865. if(ChkSum != ChkSumOrg)
  866. {
  867. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  868. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  869. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  870. if(fd < 0)
  871. {
  872. free(buf);
  873. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  874. if(ShmStatusCodeData != NULL)
  875. {
  876. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  877. }
  878. sleep(5);
  879. system("reboot -f");
  880. sleep(5);
  881. system("reboot -f");
  882. }
  883. memset(buf, 0, MtdBlockSize);
  884. wrd = read(fd, buf, MtdBlockSize);
  885. close(fd);
  886. if(wrd < MtdBlockSize)
  887. {
  888. free(buf);
  889. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  890. if(ShmStatusCodeData != NULL)
  891. {
  892. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  893. }
  894. sleep(5);
  895. system("reboot -f");
  896. sleep(5);
  897. system("reboot -f");
  898. }
  899. ChkSum = 0;
  900. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  901. {
  902. ChkSum += buf[wrd];
  903. }
  904. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  905. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  906. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  907. if(ChkSum != ChkSumOrg)
  908. {
  909. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  910. free(buf);
  911. system("cd /root;./FactoryConfig -m");
  912. system("rm -f /Storage/OCPP/OCPPConfiguration");
  913. system("sync");
  914. sleep(5);
  915. system("reboot -f");
  916. sleep(5);
  917. system("reboot -f");
  918. return FAIL;
  919. }
  920. }
  921. }
  922. //load OK
  923. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  924. free(buf);
  925. system("rm -f /mnt/EvseConfig.bin");
  926. // SysConfig in flash is empty (0xffffffff)
  927. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  928. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  929. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  930. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  931. {
  932. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  933. {
  934. LOG_INFO("Model name over length.");
  935. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  936. }
  937. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  938. {
  939. LOG_INFO("Model serial number over length.");
  940. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  941. }
  942. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  943. {
  944. LOG_INFO("System id over length.");
  945. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  946. }
  947. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  948. {
  949. LOG_INFO("Ethernet dhcp config is null.");
  950. }
  951. system("cd /root;./Module_FactoryConfig -m");
  952. sleep(3);
  953. system("/usr/bin/run_evse_restart.sh");
  954. }
  955. LOG_INFO("Load SysConfigData OK");
  956. return PASS;
  957. }
  958. int isEthernetCablePlugIn(int ethernet)
  959. {
  960. int fd, PlugInStatus = 0;
  961. char ch[2];
  962. char buffer[64];
  963. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  964. fd = open(buffer, O_RDONLY);
  965. if(fd < 0)
  966. {
  967. return -1;
  968. }
  969. if(read(fd, ch, 1) < 0)
  970. {
  971. return -1;
  972. }
  973. PlugInStatus = atoi(ch);
  974. close(fd);
  975. return PlugInStatus;
  976. }
  977. int isRouteFail()
  978. {
  979. int result = YES;
  980. FILE *fp;
  981. char buf[512];
  982. fp = popen("route -n", "r");
  983. if(fp != NULL)
  984. {
  985. while(fgets(buf, sizeof(buf), fp) != NULL)
  986. {
  987. if(strstr(buf, "eth0") != NULL)
  988. result = NO;
  989. }
  990. }
  991. pclose(fp);
  992. return result;
  993. }
  994. int isReachableInternet()
  995. {
  996. int result = FAIL;
  997. FILE *fp;
  998. char cmd[256];
  999. char buf[512];
  1000. char tmp[512];
  1001. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1002. // {
  1003. // result = PASS;
  1004. // }
  1005. // else
  1006. {
  1007. strcpy(cmd, "ifconfig eth0");
  1008. fp = popen(cmd, "r");
  1009. if (fp != NULL)
  1010. {
  1011. while(fgets(buf, sizeof(buf), fp) != NULL)
  1012. {
  1013. if (strstr(buf, "inet addr:") > 0)
  1014. {
  1015. sscanf(buf, "%*s%s", tmp);
  1016. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1017. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1018. {
  1019. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1020. }
  1021. }
  1022. }
  1023. }
  1024. pclose(fp);
  1025. memset(buf, 0x00, sizeof(buf));
  1026. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1027. {
  1028. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1029. fp = popen(cmd, "r");
  1030. if(fp != NULL)
  1031. {
  1032. while(fgets(buf, sizeof(buf), fp) != NULL)
  1033. {
  1034. if(strstr(buf, "transmitted") > 0)
  1035. {
  1036. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1037. if(strstr(buf,"100%") != NULL)
  1038. {
  1039. }
  1040. else
  1041. {
  1042. result = PASS;
  1043. }
  1044. //DEBUG_INFO("%s",buf);
  1045. //DEBUG_INFO("%s\n",tmp);
  1046. }
  1047. }
  1048. }
  1049. pclose(fp);
  1050. }
  1051. }
  1052. return result;
  1053. }
  1054. void AnnounceEthernetStatus(void)
  1055. {
  1056. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1057. {
  1058. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1059. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1060. {
  1061. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.EthernetStatusRequest = true;
  1062. }
  1063. }
  1064. }
  1065. void AnnounceWiFiStatus(void)
  1066. {
  1067. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1068. {
  1069. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1070. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1071. {
  1072. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.WiFiStatusRequest = true;
  1073. }
  1074. }
  1075. }
  1076. void AnnounceTelcomModemStatus(void)
  1077. {
  1078. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1079. {
  1080. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1081. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1082. {
  1083. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.TelcomModemStatusRequest = true;
  1084. }
  1085. }
  1086. }
  1087. void AnnounceBackendStatus(void)
  1088. {
  1089. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1090. {
  1091. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1092. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1093. {
  1094. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.BackendStatusRequest = true;
  1095. }
  1096. }
  1097. }
  1098. void AnnounceBillingStatus(void)
  1099. {
  1100. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1101. {
  1102. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1103. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1104. {
  1105. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.BillingStatusRequest = true;
  1106. }
  1107. }
  1108. }
  1109. void AnnounceStopChargingButtonStatus(void)
  1110. {
  1111. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1112. {
  1113. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1114. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1115. {
  1116. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.StopButtonStatusRequest = true;
  1117. }
  1118. }
  1119. }
  1120. void AnnounceAuthModeConfig(void)
  1121. {
  1122. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1123. {
  1124. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1125. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1126. {
  1127. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthModeConfigRequest = true;
  1128. }
  1129. }
  1130. }
  1131. void AnnounceEVCCIDConfig(void)
  1132. {
  1133. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1134. {
  1135. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1136. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1137. {
  1138. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.EVCCIDConfigRequest = true;
  1139. }
  1140. }
  1141. }
  1142. void UpdateConfigAndIcon(void)
  1143. {
  1144. unsigned char status = _Connnection_Disable;
  1145. // update ethernet connection status
  1146. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1147. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EthernetStatus != status)
  1148. {
  1149. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EthernetStatus = status;
  1150. AnnounceEthernetStatus();
  1151. }
  1152. // update wifi connection status
  1153. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1154. {
  1155. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1156. }
  1157. else
  1158. {
  1159. status = _Connnection_Disable;
  1160. }
  1161. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.WiFiStatus != status)
  1162. {
  1163. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.WiFiStatus = status;
  1164. AnnounceWiFiStatus();
  1165. }
  1166. // update 4g connection status
  1167. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1168. {
  1169. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1170. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1171. }
  1172. else
  1173. {
  1174. status = _Connnection_Disable;
  1175. }
  1176. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.TelcomModemStatus != status)
  1177. {
  1178. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.TelcomModemStatus = status;
  1179. AnnounceTelcomModemStatus();
  1180. }
  1181. // update backend connection status
  1182. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1183. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BackendStatus != status)
  1184. {
  1185. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BackendStatus = status;
  1186. AnnounceBackendStatus();
  1187. }
  1188. // update billing status
  1189. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1190. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BillingStatus != status)
  1191. {
  1192. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BillingStatus = status;
  1193. AnnounceBillingStatus();
  1194. }
  1195. // update stop charging button
  1196. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1197. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.StopChargingButton != status)
  1198. {
  1199. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.StopChargingButton = status;
  1200. AnnounceStopChargingButtonStatus();
  1201. }
  1202. // update AuthMode config
  1203. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1204. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.AuthModeConfig != status)
  1205. {
  1206. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.AuthModeConfig = status;
  1207. AnnounceAuthModeConfig();
  1208. AnnounceEVCCIDConfig();
  1209. }
  1210. // update EVCCID config
  1211. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1212. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EVCCIDConfig != status)
  1213. {
  1214. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EVCCIDConfig = status;
  1215. AnnounceEVCCIDConfig();
  1216. }
  1217. }
  1218. void Check4gWifiReset(void)
  1219. {
  1220. int timeDelay = 0;
  1221. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1222. {
  1223. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1224. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1225. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1226. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1227. {
  1228. if(!_NeedReset4gWifi)
  1229. {
  1230. GetClockTime(&_4gWifiReset_time);
  1231. LOG_INFO("Backend Enable But Internet Not Available!");
  1232. }
  1233. _NeedReset4gWifi = true;
  1234. }
  1235. else
  1236. {
  1237. _NeedReset4gWifi = false;
  1238. }
  1239. if(_NeedReset4gWifi)
  1240. {
  1241. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1242. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1243. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1244. {
  1245. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1246. usleep(100000);
  1247. // before reset 4g io, kill Module_4g & Module_Wifi task
  1248. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1249. {
  1250. system("killall Module_4g &");
  1251. }
  1252. usleep(100000);
  1253. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1254. {
  1255. system("killall Module_Wifi &");
  1256. }
  1257. LOG_INFO("Reset 4g_Wifi Module");
  1258. system("echo 1 > /sys/class/gpio/gpio104/value");
  1259. sleep(1);
  1260. system("echo 0 > /sys/class/gpio/gpio104/value");
  1261. _NeedReset4gWifi = false;
  1262. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1263. }
  1264. }
  1265. }
  1266. else
  1267. {
  1268. _NeedReset4gWifi = false;
  1269. }
  1270. }
  1271. void MasterCabinetDhcpClientConfig(void)
  1272. {
  1273. char tmpbuf[256];
  1274. //Run DHCP client if enabled
  1275. system("killall udhcpc");
  1276. system("rm -rf /etc/resolv.conf");
  1277. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1278. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  1279. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1280. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1281. {
  1282. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1283. system(tmpbuf);
  1284. }
  1285. //Upgrade system id to /etc/hostname
  1286. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1287. system(tmpbuf);
  1288. }
  1289. void InitMasterCabinetEthernet(void)
  1290. {
  1291. char tmpbuf[256];
  1292. //Init Eth0 for internet
  1293. memset(tmpbuf,0,256);
  1294. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1295. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1296. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1297. system(tmpbuf);
  1298. memset(tmpbuf,0,256);
  1299. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1300. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1301. system(tmpbuf);
  1302. //system("ifconfig lo up");
  1303. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1304. //Init Eth1 for administrator tool
  1305. memset(tmpbuf,0,256);
  1306. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1307. system(tmpbuf);
  1308. }
  1309. void InitSlaveCabinetEthernet(void)
  1310. {
  1311. char tmpbuf[256];
  1312. system("/sbin/ifconfig eth0 down");
  1313. system("/sbin/ifconfig eth1 down");
  1314. system("killall udhcpc");
  1315. system("rm -rf /etc/resolv.conf");
  1316. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1317. //Init Eth0 for internet
  1318. memset(tmpbuf,0,256);
  1319. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1320. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1321. "255.255.255.0");
  1322. system(tmpbuf);
  1323. system("/sbin/route add default gw 192.168.100.1 eth0");
  1324. //Init Eth1 for internet
  1325. memset(tmpbuf,0,256);
  1326. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1327. system(tmpbuf);
  1328. }
  1329. void InitEthernet(void)
  1330. {
  1331. system("ifconfig eth0 down");
  1332. system("ifconfig eth1 down");
  1333. sleep(2);
  1334. char tmpbuf[256];
  1335. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1336. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1337. sleep(2);
  1338. InitMasterCabinetEthernet();
  1339. MasterCabinetDhcpClientConfig();
  1340. // DO360 DHCP Server
  1341. InitialDispenserDhcpServerConfig();
  1342. StartDispenserDhcpServer();
  1343. pid_t pid = fork();
  1344. uint8_t cnt_pingDNS_Fail;
  1345. if(pid == 0)
  1346. {
  1347. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BackendStatus = _Connnection_Disconnected;
  1348. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EthernetStatus = _Connnection_Disconnected;
  1349. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.WiFiStatus = _Connnection_Disable;
  1350. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.TelcomModemStatus = _Connnection_Disable;
  1351. LOG_INFO("Internet Fork PID = %d", getpid());
  1352. for(;;)
  1353. {
  1354. if(isEthernetCablePlugIn(0) == YES)
  1355. {
  1356. if(isRouteFail())
  1357. {
  1358. LOG_INFO("eth0 not in route, restart eht0.");
  1359. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1360. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1361. {
  1362. InitialDHCP();
  1363. }
  1364. else
  1365. {
  1366. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1367. memset(tmpbuf,0,256);
  1368. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1369. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1370. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1371. system(tmpbuf);
  1372. memset(tmpbuf,0,256);
  1373. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1374. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1375. system(tmpbuf);
  1376. }
  1377. }
  1378. }
  1379. if(isReachableInternet() == PASS)
  1380. {
  1381. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1382. {
  1383. LOG_INFO("Internet is Reachable");
  1384. }
  1385. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1386. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1387. cnt_pingDNS_Fail = 0;
  1388. }
  1389. else
  1390. {
  1391. if(++cnt_pingDNS_Fail > 3)
  1392. {
  1393. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1394. {
  1395. LOG_INFO("Internet is Disconnect Via Ethernet");
  1396. }
  1397. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1398. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1399. }
  1400. }
  1401. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1402. if(ethResult == YES)
  1403. {
  1404. system("/sbin/ifmetric eth0 0");
  1405. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1406. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1407. {
  1408. system("/sbin/ifmetric mlan0 1");
  1409. }
  1410. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1411. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1412. {
  1413. system("/sbin/ifmetric ppp0 2");
  1414. }
  1415. }
  1416. if (!ethResult &&
  1417. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1418. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1419. {
  1420. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1421. if (ethResult)
  1422. {
  1423. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1424. {
  1425. system("/sbin/ifmetric eth0 1");
  1426. system("/sbin/ifmetric mlan0 0");
  1427. }
  1428. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1429. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1430. {
  1431. system("/sbin/ifmetric ppp0 2");
  1432. }
  1433. }
  1434. }
  1435. if (!ethResult &&
  1436. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1437. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1438. {
  1439. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1440. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1441. if (ethResult)
  1442. {
  1443. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1444. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1445. {
  1446. system("/sbin/ifmetric mlan0 2");
  1447. }
  1448. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1449. {
  1450. system("/sbin/ifmetric eth0 1");
  1451. system("/sbin/ifmetric ppp0 0");
  1452. }
  1453. }
  1454. }
  1455. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1456. {
  1457. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1458. }
  1459. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1460. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == false ? true : false;
  1461. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1462. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1463. UpdateConfigAndIcon();
  1464. Check4gWifiReset();
  1465. sleep(5);
  1466. }
  1467. }
  1468. #ifdef SystemLogMessage
  1469. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1470. #endif
  1471. }
  1472. void GetMacAddress()
  1473. {
  1474. for (byte index = 0; index < 2; index++)
  1475. {
  1476. int fd;
  1477. struct ifreq ifr;
  1478. char tarEth[5];
  1479. char Mac[18];
  1480. sprintf(tarEth,"eth%d",index);
  1481. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1482. ifr.ifr_addr.sa_family = AF_INET;
  1483. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1484. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1485. close(fd);
  1486. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1487. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1488. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1489. if (index == 0)
  1490. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1491. else
  1492. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1493. }
  1494. }
  1495. void GetFirmwareVersion()
  1496. {
  1497. // Get CSU root file system version
  1498. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1499. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1500. for(uint8_t idx=0;idx<3;idx++)
  1501. {
  1502. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1503. {
  1504. chademo++;
  1505. count++;
  1506. }
  1507. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1508. {
  1509. gb++;
  1510. count++;
  1511. }
  1512. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1513. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1514. {
  1515. ccs++;
  1516. count++;
  1517. }
  1518. }
  1519. if (count == 1)
  1520. {
  1521. if (chademo > 0)
  1522. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1523. else if (ccs > 0)
  1524. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1525. else if (gb > 0)
  1526. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1527. }
  1528. else
  1529. {
  1530. if (chademo > 0 && ccs > 0)
  1531. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1532. else if (chademo > 0 && gb > 0)
  1533. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1534. else if (ccs > 0 && gb > 0)
  1535. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1536. }
  1537. // Get network option from model name
  1538. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1539. {
  1540. case 'B':
  1541. case 'U':
  1542. //Blue tooth
  1543. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1544. break;
  1545. case 'W':
  1546. // WIFI
  1547. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1548. break;
  1549. case 'T':
  1550. // 3G/4G
  1551. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1552. break;
  1553. case 'D':
  1554. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1555. break;
  1556. default:
  1557. // LAN
  1558. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1559. break;
  1560. }
  1561. // Get rating power from model name
  1562. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1563. // Get IEC or UL
  1564. char _buf[3] = {0};
  1565. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1566. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1567. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1568. else if (strcmp(_buf, "WU") == EQUAL)
  1569. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1570. }
  1571. void InitialShareMemoryInfo()
  1572. {
  1573. FILE *fp;
  1574. char cmd[512];
  1575. char buf[512];
  1576. _UpgradeNeedReboot = FALSE;
  1577. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1578. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1579. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1580. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1581. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1582. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1583. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1584. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1585. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1586. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1587. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1588. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1589. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1590. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1591. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1592. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1593. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1594. sprintf(cmd, "/bin/uname -r");
  1595. fp = popen(cmd, "r");
  1596. if(fp == NULL)
  1597. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1598. else
  1599. {
  1600. while(fgets(buf, sizeof(buf), fp) != NULL)
  1601. {
  1602. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1603. }
  1604. }
  1605. // 雙槍 CCS + Chademo
  1606. GetFirmwareVersion();
  1607. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1608. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1609. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1610. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1611. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1612. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1613. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1614. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1615. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1616. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1617. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1618. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1619. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1620. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1621. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1622. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1623. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1624. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1625. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1626. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1627. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1628. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1629. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1630. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1631. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1632. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1633. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1634. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1635. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1636. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1637. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1638. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1639. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1640. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1641. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1642. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1643. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1644. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1645. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1646. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1647. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1648. ShmFanModuleData->SelfTest_Comp = NO;
  1649. ShmLedModuleData->SelfTest_Comp = NO;
  1650. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1651. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1652. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1653. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1654. ShmFanModuleData->TestFanSpeed = 0;
  1655. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1656. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1657. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1658. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1659. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  1660. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  1661. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  1662. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  1663. {
  1664. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  1665. }
  1666. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1667. {
  1668. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  1669. }
  1670. }
  1671. int Initialization(void)
  1672. {
  1673. // int pinOut[2] = { 116, 115 };
  1674. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  1675. {
  1676. chargingInfo[count]->RemoteStartFlag = NO;
  1677. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartRequest = false;
  1678. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  1679. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopRequest = false;
  1680. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  1681. SetOcppErrorCode(count, "NoError");
  1682. }
  1683. LOG_INFO("Initialization OK");
  1684. return PASS;
  1685. }
  1686. void InitialPsuGroupingAndLocation(void)
  1687. {
  1688. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1689. {
  1690. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  1691. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  1692. }
  1693. else
  1694. {
  1695. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  1696. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  1697. }
  1698. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1699. {
  1700. ShmPsuGrouping->GroupCollection[i].Index = i;
  1701. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  1702. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1703. {
  1704. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  1705. }
  1706. else
  1707. {
  1708. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  1709. }
  1710. ShmChargerInfo->Control.GunAvailable[i] = NO;
  1711. }
  1712. }
  1713. void InitialCustomerCode(void)
  1714. {
  1715. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1716. {
  1717. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  1718. }
  1719. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  1720. {
  1721. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  1722. }
  1723. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  1724. {
  1725. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  1726. }
  1727. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  1728. {
  1729. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  1730. }
  1731. else
  1732. {
  1733. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  1734. }
  1735. }
  1736. void InitialMaxDispenserConnectorQuantity(void)
  1737. {
  1738. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  1739. {
  1740. ShmChargerInfo->Control.MaxDispenser = 2;
  1741. ShmChargerInfo->Control.MaxConnector = 2;
  1742. }
  1743. else
  1744. {
  1745. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  1746. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  1747. }
  1748. }
  1749. void InitialAuthMode(void)
  1750. {
  1751. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1752. {
  1753. ShmChargerInfo->AuthInfo.AuthMode.bits.AutoStartEnable = true;
  1754. }
  1755. }
  1756. void InitialQRCodeMode(void)
  1757. {
  1758. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1759. {
  1760. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  1761. }
  1762. else
  1763. {
  1764. // customized
  1765. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  1766. {
  1767. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  1768. }
  1769. else
  1770. {
  1771. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  1772. }
  1773. }
  1774. }
  1775. void InitialPowerCabinetSetting(void)
  1776. {
  1777. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1778. {
  1779. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  1780. }
  1781. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1782. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1783. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1784. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1785. }
  1786. bool InitialChargerSetting(void)
  1787. {
  1788. bool result = true;
  1789. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O')
  1790. {
  1791. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  1792. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  1793. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1794. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  1795. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  1796. }
  1797. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1798. {
  1799. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  1800. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1801. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  1802. }
  1803. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  1804. {
  1805. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  1806. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1807. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  1808. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  1809. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  1810. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  1811. }
  1812. else
  1813. {
  1814. result = false;
  1815. }
  1816. InitialCustomerCode();
  1817. InitialMaxDispenserConnectorQuantity();
  1818. InitialAuthMode();
  1819. InitialQRCodeMode();
  1820. InitialPsuGroupingAndLocation();
  1821. InitialPowerCabinetSetting();
  1822. if(!CheckConnectorTypeStatus())
  1823. {
  1824. result = false;
  1825. }
  1826. return result;
  1827. }
  1828. bool InitialSystemDefaultConfig()
  1829. {
  1830. bool result = true;
  1831. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1832. InitGPIO();
  1833. InitEthernet();
  1834. GetMacAddress();
  1835. return result;
  1836. }
  1837. void SelfTestRun(void)
  1838. {
  1839. LOG_INFO("Self test.");
  1840. system("/root/Module_ChargerSelfTest &");
  1841. }
  1842. void SpawnNormalService(void)
  1843. {
  1844. system("/root/Module_EventLogging &");
  1845. system("/root/Module_PrimaryComm &");
  1846. system("/root/Module_EvComm &");
  1847. //system("/root/Module_LcmControl &");
  1848. system("/root/Module_InternalComm &");
  1849. system("/root/Module_PsuComm &");
  1850. system("/root/Module_ProduceUtils &");
  1851. system("/root/Module_CabinetParallel &");
  1852. system("/root/Module_LedIndication &");
  1853. system("/root/Module_Authorize &");
  1854. }
  1855. void SpawnOcppService(void)
  1856. {
  1857. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1858. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  1859. {
  1860. system("/root/OcppBackend &");
  1861. }
  1862. }
  1863. void SpawnMaintainOcppService(void)
  1864. {
  1865. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  1866. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  1867. {
  1868. system("/root/OcppBackendPH &");
  1869. }
  1870. }
  1871. void SpawnInternetService(void)
  1872. {
  1873. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1874. {
  1875. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1876. {
  1877. system("/root/Module_4g &");
  1878. }
  1879. }
  1880. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1881. {
  1882. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1883. {
  1884. system("/root/Module_Wifi &");
  1885. }
  1886. }
  1887. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  1888. {
  1889. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1890. {
  1891. system("/root/Module_4g &");
  1892. }
  1893. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1894. {
  1895. system("/root/Module_Wifi &");
  1896. }
  1897. }
  1898. }
  1899. int SpawnTask(void)
  1900. {
  1901. sleep(2);
  1902. SpawnNormalService();
  1903. SpawnInternetService();
  1904. return PASS;
  1905. }
  1906. int runFlashShellCmd(const char *cmd)
  1907. {
  1908. int result = FAIL;
  1909. char buf[256];
  1910. FILE *fp;
  1911. fp = popen(cmd, "r");
  1912. if(fp != NULL)
  1913. {
  1914. while(fgets(buf, sizeof(buf), fp) != NULL)
  1915. {
  1916. LOG_INFO("%s\n", buf);
  1917. }
  1918. result = PASS;
  1919. }
  1920. pclose(fp);
  1921. return result;
  1922. }
  1923. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1924. {
  1925. int result = PASS;
  1926. int fd, wrd;
  1927. unsigned int i, Chk;
  1928. unsigned char *ptr, *BufTmp;
  1929. Chk = 0;
  1930. ptr = (unsigned char *)UsrData;
  1931. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  1932. {
  1933. memset(BufTmp, 0, MtdBlockSize);
  1934. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1935. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  1936. {
  1937. Chk += *(BufTmp+i);
  1938. }
  1939. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  1940. // Output configuration to file.
  1941. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  1942. if(fd < 0)
  1943. {
  1944. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  1945. free(BufTmp);
  1946. return 0;
  1947. }
  1948. wrd = write(fd, BufTmp, MtdBlockSize);
  1949. close(fd);
  1950. if(wrd < MtdBlockSize)
  1951. {
  1952. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  1953. free(BufTmp);
  1954. return 0;
  1955. }
  1956. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  1957. LOG_INFO("Erase /dev/mtd10.");
  1958. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  1959. LOG_INFO("Write /dev/mtd10.");
  1960. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  1961. LOG_INFO("Erase /dev/mtd11.");
  1962. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  1963. LOG_INFO("Write /dev/mtd11.");
  1964. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  1965. system("rm -f /mnt/EvseTempConfig.bin");
  1966. LOG_INFO("EvseTempConfig write to flash OK");
  1967. }
  1968. else
  1969. {
  1970. LOG_ERROR("alloc BlockSize NG");
  1971. result = FAIL;
  1972. }
  1973. if(BufTmp != NULL)
  1974. {
  1975. free(BufTmp);
  1976. }
  1977. return result;
  1978. }
  1979. //===============================================
  1980. // Common Detect Chk - Stop Charging ?
  1981. //===============================================
  1982. bool isEvBoardStopChargeFlag(byte gunIndex)
  1983. {
  1984. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  1985. }
  1986. bool isNormalStopChargeFlag(byte gunIndex)
  1987. {
  1988. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  1989. }
  1990. bool isResetStopChargeFlag(byte gunIndex)
  1991. {
  1992. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  1993. }
  1994. bool isInvalidIdStopFlag(byte gunIndex)
  1995. {
  1996. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  1997. }
  1998. void ReleaseAlarmCode(byte gunIndex)
  1999. {
  2000. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2001. {
  2002. SetOcppErrorCode(gunIndex, "NoError");
  2003. }
  2004. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2005. {
  2006. SetOcppVendorErrorCode(gunIndex, "");
  2007. }
  2008. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2009. {
  2010. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2011. }
  2012. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2013. {
  2014. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2015. }
  2016. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2017. {
  2018. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2019. }
  2020. }
  2021. //===============================================
  2022. // EmergencyStop and Charging Stop
  2023. //===============================================
  2024. void ChargingTerminalProcess(byte gunIndex)
  2025. {
  2026. setChargerMode(gunIndex, MODE_TERMINATING);
  2027. }
  2028. void ChargingAlarmProcess(byte gunIndex)
  2029. {
  2030. UpdateErrorCodeToOcpp(gunIndex);
  2031. setChargerMode(gunIndex, MODE_ALARM);
  2032. }
  2033. void AcChargingTerminalProcess()
  2034. {
  2035. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2036. }
  2037. void StopChargingProcessByString(byte level)
  2038. {
  2039. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2040. {
  2041. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2042. }
  2043. }
  2044. void ReleaseChargingProcessByString(byte level)
  2045. {
  2046. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2047. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2048. }
  2049. // 一般錯誤停止充電處理函式
  2050. void BoardErrOccurByString(byte index, char *code)
  2051. {
  2052. byte level = 1;
  2053. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2054. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2055. {
  2056. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2057. {
  2058. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2059. }
  2060. ChargingTerminalProcess(index);
  2061. }
  2062. StopChargingProcessByString(level);
  2063. }
  2064. // 急停狀況的停止充電處理函式
  2065. void EmcOccureByString(char *code)
  2066. {
  2067. byte level = 2;
  2068. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2069. {
  2070. return;
  2071. }
  2072. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2073. // 其錯誤等級為 2
  2074. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2075. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2076. strncmp(code, "042328", 6) == EQUAL)
  2077. {
  2078. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2079. {
  2080. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2081. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2082. {
  2083. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2084. {
  2085. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2086. }
  2087. InformOcppErrOccur(4);
  2088. ChargingAlarmProcess(gun);
  2089. }
  2090. }
  2091. StopChargingProcessByString(level);
  2092. }
  2093. }
  2094. void ReleaseEmsOccureByString(byte index, char *code)
  2095. {
  2096. bool isTrigger = false;
  2097. byte level = 2;
  2098. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2099. {
  2100. isTrigger = true;
  2101. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2102. }
  2103. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2104. {
  2105. isTrigger = true;
  2106. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2107. }
  2108. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2109. {
  2110. isTrigger = true;
  2111. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2112. }
  2113. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2114. {
  2115. isTrigger = true;
  2116. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2117. }
  2118. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2119. {
  2120. isTrigger = true;
  2121. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2122. }
  2123. if (isTrigger)
  2124. {
  2125. ReleaseChargingProcessByString(level);
  2126. }
  2127. }
  2128. //===============================================
  2129. // 確認硬體 (按鈕) 狀態
  2130. //===============================================
  2131. void ChkPrimaryStatus()
  2132. {
  2133. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2134. {
  2135. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2136. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2137. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2138. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2139. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2140. return;
  2141. }
  2142. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2143. {
  2144. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2145. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2146. EmcOccureByString("042251");
  2147. }
  2148. else
  2149. {
  2150. ReleaseEmsOccureByString(0, "042251");
  2151. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2152. }
  2153. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2154. {
  2155. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2156. //EmcOccureByString("042238");
  2157. }
  2158. else
  2159. {
  2160. //ReleaseEmsOccureByString(0, "042238");
  2161. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2162. }
  2163. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2164. {
  2165. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2166. }
  2167. else
  2168. {
  2169. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2170. }
  2171. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2172. {
  2173. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2174. EmcOccureByString("042252");
  2175. }
  2176. else
  2177. {
  2178. ReleaseEmsOccureByString(0, "042252");
  2179. }
  2180. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2181. {
  2182. EmcOccureByString("042328");
  2183. SetAcContactor(OFF);
  2184. }
  2185. }
  2186. struct timespec _AcContactReOpen_Time;
  2187. void ChkPsuStatus(void)
  2188. {
  2189. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2190. {
  2191. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2192. {
  2193. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2194. {
  2195. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2196. }
  2197. }
  2198. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2199. {
  2200. EmcOccureByString("042267");
  2201. }
  2202. }
  2203. else
  2204. {
  2205. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2206. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2207. {
  2208. bool allFaultStatus = true;
  2209. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2210. {
  2211. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2212. {
  2213. allFaultStatus = false;
  2214. }
  2215. }
  2216. if(allFaultStatus)
  2217. {
  2218. LOG_INFO("PSU Failure, Need Resume");
  2219. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2220. }
  2221. }
  2222. }
  2223. }
  2224. int AcContactorReOpenCount = 0;
  2225. void PsuFailureResume(void)
  2226. {
  2227. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2228. {
  2229. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2230. {
  2231. LOG_INFO("Ac Contactor Off By Psu Failure");
  2232. // update _AcContactReOpen_Time
  2233. GetClockTime(&_AcContactReOpen_Time);
  2234. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2235. AcContactorReOpenCount++;
  2236. }
  2237. else
  2238. {
  2239. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2240. {
  2241. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2242. {
  2243. ReleaseEmsOccureByString(0, "042267");
  2244. // update _AcContactReOpen_Time
  2245. GetClockTime(&_AcContactReOpen_Time);
  2246. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2247. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2248. LOG_INFO("Psu Failure Resume");
  2249. }
  2250. }
  2251. }
  2252. }
  2253. else
  2254. {
  2255. if(AcContactorReOpenCount > 0)
  2256. {
  2257. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2258. {
  2259. AcContactorReOpenCount = 0;
  2260. }
  2261. }
  2262. }
  2263. }
  2264. void SetAcContactor(unsigned char OnOff)
  2265. {
  2266. OnOff = OnOff ? ON : OFF;
  2267. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2268. {
  2269. if(OnOff)
  2270. {
  2271. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2272. }
  2273. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2274. }
  2275. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2276. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2277. {
  2278. SetParallelCabinetAcContactor(OnOff);
  2279. }
  2280. }
  2281. void StandbyCheck(void)
  2282. {
  2283. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2284. {
  2285. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2286. {
  2287. int time = STANDBY_TIME;
  2288. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2289. {
  2290. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2291. {
  2292. time = FAST_STANDBY_TIME;
  2293. }
  2294. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2295. {
  2296. SetAcContactor(OFF);
  2297. }
  2298. }
  2299. }
  2300. else
  2301. {
  2302. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2303. GetClockTime(&_Standby_time);
  2304. }
  2305. }
  2306. else
  2307. {
  2308. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2309. {
  2310. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2311. }
  2312. }
  2313. }
  2314. void CheckDisconnectionStatusRecovery(void)
  2315. {
  2316. bool recovery = true;
  2317. // check all connector
  2318. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2319. {
  2320. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2321. {
  2322. recovery = false;
  2323. }
  2324. }
  2325. if(recovery)
  2326. {
  2327. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2328. }
  2329. }
  2330. //===============================================
  2331. // 確認各小板偵測的錯誤狀況
  2332. //===============================================
  2333. void CheckErrorOccurStatus(byte index)
  2334. {
  2335. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2336. {
  2337. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2338. }
  2339. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2340. {
  2341. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2342. {
  2343. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2344. }
  2345. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2346. {
  2347. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2348. }
  2349. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2350. }
  2351. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2352. {
  2353. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2354. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2355. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2356. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2357. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2358. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2359. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2360. chargingInfo[index]->SystemStatus == S_CHARGING)
  2361. {
  2362. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2363. {
  2364. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2365. }
  2366. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2367. {
  2368. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2369. }
  2370. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2371. }
  2372. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2373. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2374. {
  2375. setChargerMode(index, MODE_IDLE);
  2376. }
  2377. }
  2378. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2379. {
  2380. CheckDisconnectionStatusRecovery();
  2381. }
  2382. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2383. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2384. {
  2385. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2386. {
  2387. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2388. StopChargingProcessByString(2);
  2389. }
  2390. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2391. {
  2392. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2393. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2394. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2395. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2396. }
  2397. }
  2398. else
  2399. {
  2400. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2401. {
  2402. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2403. ReleaseChargingProcessByString(2);
  2404. }
  2405. }
  2406. // RB
  2407. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2408. {
  2409. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2410. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2411. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2412. {
  2413. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2414. {
  2415. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2416. StopChargingProcessByString(2);
  2417. }
  2418. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2419. {
  2420. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2421. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2422. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2423. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2424. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2425. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2426. }
  2427. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2428. {
  2429. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2430. }
  2431. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2432. }
  2433. else
  2434. {
  2435. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2436. {
  2437. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2438. ReleaseChargingProcessByString(2);
  2439. }
  2440. }
  2441. }
  2442. else
  2443. {
  2444. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2445. {
  2446. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2447. ReleaseChargingProcessByString(2);
  2448. }
  2449. }
  2450. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2451. {
  2452. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2453. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2454. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2455. {
  2456. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2457. {
  2458. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2459. StopChargingProcessByString(2);
  2460. }
  2461. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2462. {
  2463. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2464. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2465. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2466. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2467. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2468. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2469. }
  2470. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2471. {
  2472. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2473. }
  2474. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2475. }
  2476. else
  2477. {
  2478. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2479. {
  2480. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2481. ReleaseChargingProcessByString(2);
  2482. }
  2483. }
  2484. }
  2485. else
  2486. {
  2487. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  2488. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2489. {
  2490. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2491. {
  2492. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2493. StopChargingProcessByString(2);
  2494. }
  2495. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2496. {
  2497. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  2498. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  2499. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2500. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  2501. }
  2502. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2503. {
  2504. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2505. }
  2506. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2507. }
  2508. else
  2509. {
  2510. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2511. {
  2512. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2513. ReleaseChargingProcessByString(2);
  2514. }
  2515. }
  2516. }
  2517. }
  2518. int DiffTimebWithNowSec(struct timeb ST)
  2519. {
  2520. //return milli-second
  2521. struct timeb ET;
  2522. unsigned int StartTime,StopTime;
  2523. ftime(&ET);
  2524. StartTime=(unsigned int)ST.time;
  2525. StopTime=(unsigned int)ET.time;
  2526. return (StopTime-StartTime);
  2527. }
  2528. int CheckTimeOut(char *start)
  2529. {
  2530. int result = YES;
  2531. struct ParsingResult
  2532. {
  2533. int result;
  2534. int scanedElement;
  2535. int year;
  2536. int month;
  2537. int mday;
  2538. int hour;
  2539. int min;
  2540. int sec;
  2541. int tz_hour;
  2542. int tz_min;
  2543. float minSec;
  2544. }parsingResult;
  2545. struct tm tmStart;
  2546. struct timeb tbStart;
  2547. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  2548. if(strstr((char*)start, ".") != NULL)
  2549. {
  2550. // Original data with mini second
  2551. if(strstr((char*)start, "Z") != NULL)
  2552. {
  2553. // Original data with Z
  2554. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  2555. &parsingResult.year,
  2556. &parsingResult.month,
  2557. &parsingResult.mday,
  2558. &parsingResult.hour,
  2559. &parsingResult.min,
  2560. &parsingResult.sec,
  2561. &parsingResult.minSec);
  2562. }
  2563. else
  2564. {
  2565. // Original data without Z
  2566. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  2567. &parsingResult.year,
  2568. &parsingResult.month,
  2569. &parsingResult.mday,
  2570. &parsingResult.hour,
  2571. &parsingResult.min,
  2572. &parsingResult.sec,
  2573. &parsingResult.minSec,
  2574. &parsingResult.tz_hour,
  2575. &parsingResult.tz_min);
  2576. }
  2577. }
  2578. else
  2579. {
  2580. // Original data without mini second
  2581. if(strstr((char*)start, "Z") != NULL)
  2582. {
  2583. // Original data with Z
  2584. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  2585. &parsingResult.year,
  2586. &parsingResult.month,
  2587. &parsingResult.mday,
  2588. &parsingResult.hour,
  2589. &parsingResult.min,
  2590. &parsingResult.sec);
  2591. }
  2592. else
  2593. {
  2594. // Original data without Z
  2595. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  2596. &parsingResult.year,
  2597. &parsingResult.month,
  2598. &parsingResult.mday,
  2599. &parsingResult.hour,
  2600. &parsingResult.min,
  2601. &parsingResult.sec,
  2602. &parsingResult.tz_hour,
  2603. &parsingResult.tz_min);
  2604. }
  2605. }
  2606. if(parsingResult.scanedElement >= 6)
  2607. {
  2608. tmStart.tm_year = parsingResult.year - 1900;
  2609. tmStart.tm_mon = parsingResult.month - 1;
  2610. tmStart.tm_mday = parsingResult.mday;
  2611. tmStart.tm_hour = parsingResult.hour;
  2612. tmStart.tm_min = parsingResult.min;
  2613. tmStart.tm_sec = parsingResult.sec;
  2614. tmStart.tm_gmtoff = 0;
  2615. tbStart.time = mktime(&tmStart);
  2616. tbStart.timezone = 0;
  2617. tbStart.millitm = 0;
  2618. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  2619. if(DiffTimebWithNowSec(tbStart) <= 0)
  2620. result = NO;
  2621. else
  2622. result = YES;
  2623. }
  2624. else
  2625. {
  2626. LOG_INFO("Start date parsing error");
  2627. }
  2628. return result;
  2629. }
  2630. //===============================================
  2631. // Ocpp OcppConnStatus
  2632. //===============================================
  2633. bool Get_Ocpp_ConnectionStatus(void)
  2634. {
  2635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2636. {
  2637. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  2638. }
  2639. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2640. {
  2641. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  2642. }
  2643. return false;
  2644. }
  2645. //===============================================
  2646. // Ocpp procDogTime
  2647. //===============================================
  2648. unsigned int Get_Ocpp_procDogTime(void)
  2649. {
  2650. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2651. {
  2652. return ShmOCPP16Data->procDogTime;
  2653. }
  2654. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2655. {
  2656. return ShmOCPP20Data->procDogTime;
  2657. }
  2658. return 0;
  2659. }
  2660. void Clean_Ocpp_procDogTime(void)
  2661. {
  2662. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2663. {
  2664. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2665. return;
  2666. }
  2667. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2668. {
  2669. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2670. return;
  2671. }
  2672. }
  2673. //===============================================
  2674. // Ocpp Boot Notification
  2675. //===============================================
  2676. bool Is_Ocpp_BootNotification(void)
  2677. {
  2678. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2679. {
  2680. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  2681. }
  2682. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2683. {
  2684. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  2685. }
  2686. return false;
  2687. }
  2688. void Clean_Ocpp_BootNotification(void)
  2689. {
  2690. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2691. {
  2692. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  2693. return;
  2694. }
  2695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2696. {
  2697. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  2698. return;
  2699. }
  2700. }
  2701. //===============================================
  2702. // Ocpp Rest
  2703. //===============================================
  2704. bool Is_Ocpp_ResetReq(void)
  2705. {
  2706. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2707. {
  2708. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  2709. }
  2710. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2711. {
  2712. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  2713. }
  2714. return false;
  2715. }
  2716. bool Chk_Ocpp_Reset_Type(char *type)
  2717. {
  2718. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2719. {
  2720. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  2721. }
  2722. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2723. {
  2724. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  2725. }
  2726. return false;
  2727. }
  2728. void Clean_Ocpp_ResetReq(void)
  2729. {
  2730. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2731. {
  2732. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2733. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  2734. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  2735. return;
  2736. }
  2737. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2738. {
  2739. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  2740. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  2741. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  2742. return;
  2743. }
  2744. }
  2745. //===============================================
  2746. // Ocpp AuthorizeReq
  2747. //===============================================
  2748. void Set_Ocpp_AuthorizeReq(void)
  2749. {
  2750. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2751. {
  2752. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  2753. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  2754. return;
  2755. }
  2756. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2757. {
  2758. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  2759. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  2760. return;
  2761. }
  2762. }
  2763. bool Is_Ocpp_AuthorizeConf(void)
  2764. {
  2765. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2766. {
  2767. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  2768. }
  2769. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2770. {
  2771. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  2772. }
  2773. return false;
  2774. }
  2775. bool Is_Ocpp_Authorize_Status(char *status)
  2776. {
  2777. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2778. {
  2779. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  2780. }
  2781. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2782. {
  2783. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  2784. }
  2785. return false;
  2786. }
  2787. //===============================================
  2788. // Ocpp UpdateFirmwareReq
  2789. //===============================================
  2790. bool Is_Ocpp_UpdateFirmwareReq(void)
  2791. {
  2792. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2793. {
  2794. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  2795. }
  2796. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2797. {
  2798. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  2799. }
  2800. return false;
  2801. }
  2802. void Clean_Ocpp_UpdateFirmwareReq(void)
  2803. {
  2804. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2805. {
  2806. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  2807. return;
  2808. }
  2809. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2810. {
  2811. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  2812. return;
  2813. }
  2814. }
  2815. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  2816. {
  2817. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2818. {
  2819. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  2820. }
  2821. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2822. {
  2823. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  2824. }
  2825. return false;
  2826. }
  2827. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  2828. {
  2829. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2830. {
  2831. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  2832. return;
  2833. }
  2834. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2835. {
  2836. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  2837. return;
  2838. }
  2839. }
  2840. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  2841. {
  2842. Set_Ocpp_UpdateFirmwareStatus("");
  2843. }
  2844. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  2845. {
  2846. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2847. {
  2848. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  2849. return;
  2850. }
  2851. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2852. {
  2853. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  2854. return;
  2855. }
  2856. }
  2857. //===============================================
  2858. // Ocpp StopTransactionOnInvalidId
  2859. //===============================================
  2860. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  2861. {
  2862. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2863. {
  2864. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  2865. }
  2866. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2867. {
  2868. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  2869. }
  2870. return false;
  2871. }
  2872. //===============================================
  2873. // Ocpp AuthorizeRemoteTxRequests
  2874. //===============================================
  2875. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  2876. {
  2877. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2878. {
  2879. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  2880. }
  2881. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2882. {
  2883. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  2884. }
  2885. return false;
  2886. }
  2887. //===============================================
  2888. // Ocpp ConnectionTimeOut
  2889. //===============================================
  2890. int Get_Ocpp_ConnectionTimeOut(void)
  2891. {
  2892. int timeout = 0;
  2893. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2894. {
  2895. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  2896. {
  2897. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2898. }
  2899. }
  2900. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2901. {
  2902. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  2903. {
  2904. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  2905. }
  2906. }
  2907. return timeout > 0 ? timeout : 0;
  2908. }
  2909. //===============================================
  2910. // Ocpp DefaultPrice
  2911. //===============================================
  2912. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  2913. {
  2914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2915. {
  2916. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  2917. return;
  2918. }
  2919. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2920. {
  2921. strcpy(defaultPrice, "");
  2922. return;
  2923. }
  2924. strcpy(defaultPrice, "");
  2925. }
  2926. //===============================================
  2927. // Ocpp UserPrice
  2928. //===============================================
  2929. void Get_Ocpp_UserPrice(char * userId, char *defaultPrice)
  2930. {
  2931. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2932. {
  2933. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  2934. return;
  2935. }
  2936. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2937. {
  2938. strcpy(defaultPrice, "");
  2939. return;
  2940. }
  2941. strcpy(defaultPrice, "");
  2942. }
  2943. void Clean_Ocpp_UserPrice(void)
  2944. {
  2945. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2946. {
  2947. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  2948. return;
  2949. }
  2950. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2951. {
  2952. return;
  2953. }
  2954. }
  2955. //===============================================
  2956. // Ocpp RunningCost
  2957. //===============================================
  2958. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  2959. {
  2960. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2961. {
  2962. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  2963. return;
  2964. }
  2965. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2966. {
  2967. strcpy(cost, (char *)"");
  2968. return;
  2969. }
  2970. }
  2971. void Clean_Ocpp_RunningCost(int gun_index)
  2972. {
  2973. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2974. {
  2975. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  2976. return;
  2977. }
  2978. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2979. {
  2980. return;
  2981. }
  2982. }
  2983. //===============================================
  2984. // Ocpp FinalCost
  2985. //===============================================
  2986. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  2987. {
  2988. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2989. {
  2990. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  2991. return;
  2992. }
  2993. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2994. {
  2995. strcpy(cost, (char *)"");
  2996. return;
  2997. }
  2998. }
  2999. void Clean_Ocpp_FinalCost(int gun_index)
  3000. {
  3001. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3002. {
  3003. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3004. return;
  3005. }
  3006. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3007. {
  3008. return;
  3009. }
  3010. }
  3011. //===============================================
  3012. // Ocpp Start Transaction
  3013. //===============================================
  3014. void Set_Ocpp_StartTransactionId(int gun_index, char *id)
  3015. {
  3016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3017. {
  3018. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  3019. return;
  3020. }
  3021. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3022. {
  3023. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  3024. return;
  3025. }
  3026. }
  3027. void Set_Ocpp_StartTransactionReq(int gun_index)
  3028. {
  3029. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3030. {
  3031. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  3032. return;
  3033. }
  3034. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3035. {
  3036. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  3037. return;
  3038. }
  3039. }
  3040. bool Is_Ocpp_StartTransactionConf(int gun_index)
  3041. {
  3042. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3043. {
  3044. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  3045. }
  3046. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3047. {
  3048. return ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES ? true : false;
  3049. }
  3050. return false;
  3051. }
  3052. void Clean_Ocpp_StartTransactionConf(int gun_index)
  3053. {
  3054. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3055. {
  3056. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  3057. return;
  3058. }
  3059. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3060. {
  3061. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  3062. return;
  3063. }
  3064. }
  3065. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  3066. {
  3067. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3068. {
  3069. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3070. }
  3071. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3072. {
  3073. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3074. }
  3075. return false;
  3076. }
  3077. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  3078. {
  3079. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  3080. }
  3081. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  3082. {
  3083. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3084. {
  3085. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  3086. return;
  3087. }
  3088. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3089. {
  3090. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  3091. return;
  3092. }
  3093. sprintf(transactionId, "%d", 0);
  3094. }
  3095. bool Is_Invalid_StartTransactionId(int gun_index)
  3096. {
  3097. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  3098. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  3099. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  3100. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  3101. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  3102. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  3103. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  3104. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  3105. {
  3106. return true;
  3107. }
  3108. return false;
  3109. }
  3110. //===============================================
  3111. // Ocpp Stop Transaction
  3112. //===============================================
  3113. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  3114. {
  3115. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3116. {
  3117. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  3118. return;
  3119. }
  3120. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3121. {
  3122. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  3123. return;
  3124. }
  3125. }
  3126. void Set_Ocpp_StopTransactionReq(int gun_index)
  3127. {
  3128. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3129. {
  3130. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  3131. return;
  3132. }
  3133. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3134. {
  3135. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  3136. return;
  3137. }
  3138. }
  3139. //===============================================
  3140. // Ocpp Check Backup Error Code
  3141. //===============================================
  3142. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  3143. {
  3144. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3145. {
  3146. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  3147. {
  3148. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  3149. {
  3150. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  3151. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  3152. }
  3153. }
  3154. }
  3155. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3156. {
  3157. }
  3158. }
  3159. //===============================================
  3160. // Ocpp Reserve Now
  3161. //===============================================
  3162. bool Is_Ocpp_ReserveNowReq(int gun_index)
  3163. {
  3164. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3165. {
  3166. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  3167. }
  3168. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3169. {
  3170. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  3171. }
  3172. return false;
  3173. }
  3174. void Clean_Ocpp_ReserveNowReq(int gun_index)
  3175. {
  3176. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3177. {
  3178. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  3179. return;
  3180. }
  3181. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3182. {
  3183. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  3184. return;
  3185. }
  3186. }
  3187. int Get_Ocpp_ReserveNowId(int gun_index)
  3188. {
  3189. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3190. {
  3191. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3192. }
  3193. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3194. {
  3195. return ShmOCPP20Data->ReserveNow[gun_index].id;
  3196. }
  3197. return 0;
  3198. }
  3199. void Set_Ocpp_ReserveNowConf(int gun_index)
  3200. {
  3201. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3202. {
  3203. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  3204. return;
  3205. }
  3206. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3207. {
  3208. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  3209. return;
  3210. }
  3211. }
  3212. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  3213. {
  3214. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3215. {
  3216. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  3217. return;
  3218. }
  3219. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3220. {
  3221. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  3222. return;
  3223. }
  3224. strcpy(date, "");
  3225. return;
  3226. }
  3227. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  3228. {
  3229. bool _expired = true;
  3230. char _expiryDate[128];
  3231. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  3232. _expired = CheckTimeOut(_expiryDate);
  3233. return _expired;
  3234. }
  3235. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  3236. {
  3237. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3238. {
  3239. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  3240. return;
  3241. }
  3242. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3243. {
  3244. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  3245. return;
  3246. }
  3247. strcpy(idTag, "");
  3248. return;
  3249. }
  3250. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  3251. {
  3252. char ReserveIdTag[32];
  3253. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  3254. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  3255. }
  3256. //===============================================
  3257. // Ocpp Cancel Reservation
  3258. //===============================================
  3259. bool Is_Ocpp_CancelReservationReq(int gun_index)
  3260. {
  3261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3262. {
  3263. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  3264. }
  3265. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3266. {
  3267. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  3268. }
  3269. return false;
  3270. }
  3271. void Clean_Ocpp_CancelReservationReq(int gun_index)
  3272. {
  3273. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3274. {
  3275. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  3276. return;
  3277. }
  3278. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3279. {
  3280. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  3281. return;
  3282. }
  3283. }
  3284. void Set_Ocpp_CancelReservationConf(int gun_index)
  3285. {
  3286. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3287. {
  3288. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  3289. return;
  3290. }
  3291. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3292. {
  3293. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  3294. return;
  3295. }
  3296. }
  3297. //===============================================
  3298. // Ocpp Change Availability
  3299. //===============================================
  3300. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  3301. {
  3302. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3303. {
  3304. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  3305. }
  3306. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3307. {
  3308. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  3309. }
  3310. return false;
  3311. }
  3312. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  3313. {
  3314. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3315. {
  3316. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  3317. return;
  3318. }
  3319. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3320. {
  3321. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  3322. return;
  3323. }
  3324. }
  3325. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  3326. {
  3327. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3328. {
  3329. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  3330. }
  3331. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3332. {
  3333. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  3334. }
  3335. return false;
  3336. }
  3337. //===============================================
  3338. // Ocpp Unlock Connector
  3339. //===============================================
  3340. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  3341. {
  3342. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3343. {
  3344. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  3345. }
  3346. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3347. {
  3348. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  3349. }
  3350. return false;
  3351. }
  3352. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  3353. {
  3354. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3355. {
  3356. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  3357. return;
  3358. }
  3359. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3360. {
  3361. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  3362. return;
  3363. }
  3364. }
  3365. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  3366. {
  3367. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3368. {
  3369. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  3370. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  3371. return;
  3372. }
  3373. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3374. {
  3375. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  3376. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  3377. return;
  3378. }
  3379. }
  3380. //===============================================
  3381. // Ocpp Remote Start
  3382. //===============================================
  3383. bool Is_Ocpp_RemoteStartReq(int gun_index)
  3384. {
  3385. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3386. {
  3387. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  3388. }
  3389. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3390. {
  3391. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  3392. }
  3393. return false;
  3394. }
  3395. void Clean_Ocpp_RemoteStartReq(int gun_index)
  3396. {
  3397. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3398. {
  3399. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  3400. return;
  3401. }
  3402. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3403. {
  3404. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  3405. return;
  3406. }
  3407. }
  3408. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  3409. {
  3410. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3411. {
  3412. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  3413. return;
  3414. }
  3415. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3416. {
  3417. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  3418. return;
  3419. }
  3420. strcpy(idTag, "");
  3421. }
  3422. //===============================================
  3423. // Ocpp Remote Stop
  3424. //===============================================
  3425. bool Is_Ocpp_RemoteStopReq(int gun_index)
  3426. {
  3427. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3428. {
  3429. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  3430. }
  3431. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3432. {
  3433. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  3434. }
  3435. return false;
  3436. }
  3437. void Clean_Ocpp_RemoteStopReq(int gun_index)
  3438. {
  3439. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3440. {
  3441. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  3442. return;
  3443. }
  3444. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3445. {
  3446. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  3447. return;
  3448. }
  3449. }
  3450. //===============================================
  3451. // Ocpp StopReason
  3452. //===============================================
  3453. void Set_Ocpp_StopReason(int gun_index, char *reason)
  3454. {
  3455. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3456. {
  3457. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  3458. {
  3459. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  3460. return;
  3461. }
  3462. }
  3463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3464. {
  3465. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  3466. {
  3467. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  3468. return;
  3469. }
  3470. }
  3471. }
  3472. void Get_Ocpp_StopReason(int gun_index, char *reason)
  3473. {
  3474. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3475. {
  3476. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3477. return;
  3478. }
  3479. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3480. {
  3481. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3482. return;
  3483. }
  3484. strcpy(reason, "");
  3485. }
  3486. void Clean_Ocpp_StopReason(int gun_index)
  3487. {
  3488. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3489. {
  3490. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  3491. return;
  3492. }
  3493. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3494. {
  3495. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  3496. return;
  3497. }
  3498. }
  3499. //===============================================
  3500. // Ocpp StatusNotification ErrorCode
  3501. //===============================================
  3502. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  3503. {
  3504. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3505. {
  3506. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  3507. return;
  3508. }
  3509. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3510. {
  3511. return;
  3512. }
  3513. }
  3514. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  3515. {
  3516. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3517. {
  3518. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  3519. }
  3520. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3521. {
  3522. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  3523. }
  3524. return false;
  3525. }
  3526. //===============================================
  3527. // Ocpp StatusNotification VendorErrorCode
  3528. //===============================================
  3529. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  3530. {
  3531. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3532. {
  3533. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  3534. return;
  3535. }
  3536. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3537. {
  3538. return;
  3539. }
  3540. }
  3541. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  3542. {
  3543. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3544. {
  3545. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  3546. }
  3547. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3548. {
  3549. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  3550. }
  3551. return false;
  3552. }
  3553. //===============================================
  3554. // Ocpp ChargingProfileConf
  3555. //===============================================
  3556. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  3557. {
  3558. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3559. {
  3560. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  3561. }
  3562. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3563. {
  3564. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  3565. }
  3566. return false;
  3567. }
  3568. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  3569. {
  3570. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3571. {
  3572. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  3573. return;
  3574. }
  3575. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3576. {
  3577. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  3578. return;
  3579. }
  3580. }
  3581. //===============================================
  3582. // Ocpp ChargingProfileReq
  3583. //===============================================
  3584. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  3585. {
  3586. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3587. {
  3588. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  3589. }
  3590. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3591. {
  3592. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  3593. }
  3594. return false;
  3595. }
  3596. void Set_Ocpp_ChargingProfileReq(int gun_index)
  3597. {
  3598. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3599. {
  3600. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  3601. return;
  3602. }
  3603. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3604. {
  3605. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  3606. return;
  3607. }
  3608. }
  3609. //===============================================
  3610. // Ocpp SmartChargingProfile ChargingProfileKind
  3611. //===============================================
  3612. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  3613. {
  3614. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3615. {
  3616. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  3617. }
  3618. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3619. {
  3620. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  3621. }
  3622. return false;
  3623. }
  3624. bool Is_Ocpp_ChargingProfileId(int gun_index)
  3625. {
  3626. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3627. {
  3628. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  3629. }
  3630. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3631. {
  3632. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  3633. }
  3634. return false;
  3635. }
  3636. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  3637. {
  3638. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3639. {
  3640. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  3641. }
  3642. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3643. {
  3644. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  3645. }
  3646. return 0;
  3647. }
  3648. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  3649. {
  3650. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3651. {
  3652. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  3653. }
  3654. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3655. {
  3656. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  3657. }
  3658. return 0;
  3659. }
  3660. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  3661. {
  3662. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3663. {
  3664. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  3665. }
  3666. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3667. {
  3668. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  3669. }
  3670. return 0;
  3671. }
  3672. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  3673. {
  3674. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3675. {
  3676. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  3677. }
  3678. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3679. {
  3680. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  3681. }
  3682. return 0;
  3683. }
  3684. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  3685. {
  3686. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3687. {
  3688. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  3689. }
  3690. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3691. {
  3692. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  3693. }
  3694. return 0;
  3695. }
  3696. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  3697. {
  3698. int _time = 0, _scheduleTime = 0;
  3699. int _maxCount = 0;
  3700. float _limit = 0;
  3701. int _startCount = NO_DEFINE;
  3702. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  3703. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  3704. for(int i = 0; i < _maxCount; i++)
  3705. {
  3706. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  3707. if(_time >= _scheduleTime)
  3708. {
  3709. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  3710. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  3711. {
  3712. _startCount = i;
  3713. }
  3714. }
  3715. }
  3716. return _startCount;
  3717. }
  3718. //===============================================
  3719. // Ocpp MaxChargingProfile ChargingProfileKind
  3720. //===============================================
  3721. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  3722. {
  3723. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3724. {
  3725. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  3726. }
  3727. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3728. {
  3729. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  3730. }
  3731. return false;
  3732. }
  3733. bool Is_Ocpp_MaxChargingProfileId(void)
  3734. {
  3735. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3736. {
  3737. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  3738. }
  3739. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3740. {
  3741. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  3742. }
  3743. return false;
  3744. }
  3745. int Get_Ocpp_MaxProfileStartTime(void)
  3746. {
  3747. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3748. {
  3749. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  3750. }
  3751. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3752. {
  3753. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  3754. }
  3755. return 0;
  3756. }
  3757. int Get_Ocpp_MaxProfileScheduleCount(void)
  3758. {
  3759. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3760. {
  3761. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  3762. }
  3763. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3764. {
  3765. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  3766. }
  3767. return 0;
  3768. }
  3769. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  3770. {
  3771. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3772. {
  3773. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  3774. }
  3775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3776. {
  3777. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  3778. }
  3779. return 0;
  3780. }
  3781. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  3782. {
  3783. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3784. {
  3785. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  3786. }
  3787. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3788. {
  3789. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  3790. }
  3791. return 0;
  3792. }
  3793. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  3794. {
  3795. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3796. {
  3797. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  3798. }
  3799. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3800. {
  3801. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  3802. }
  3803. return 0;
  3804. }
  3805. int Get_Ocpp_MaxProfileStartIndex(void)
  3806. {
  3807. int _time = 0, _scheduleTime = 0;
  3808. int _maxCount = 0;
  3809. float _limit = 0;
  3810. int _startCount = NO_DEFINE;
  3811. _time = Get_Ocpp_MaxProfileStartTime();
  3812. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  3813. for(int i = 0; i < _maxCount; i++)
  3814. {
  3815. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  3816. if(_time >= _scheduleTime)
  3817. {
  3818. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  3819. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  3820. {
  3821. _startCount = i;
  3822. }
  3823. }
  3824. }
  3825. return _startCount;
  3826. }
  3827. //===============================================
  3828. // Main process
  3829. //===============================================
  3830. // 檢查 Byte 中某個 Bit 的值
  3831. // _byte : 欲改變的 byte
  3832. // _bit : 該 byte 的第幾個 bit
  3833. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  3834. {
  3835. return ( _byte & mask_table[_bit] ) != 0x00;
  3836. }
  3837. // 設定 Byte 中某個 Bit的值
  3838. // _byte : 欲改變的 byte
  3839. // _bit : 該 byte 的第幾個 bit
  3840. // value : 修改的值為 0 or 1
  3841. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  3842. {
  3843. if(value == 1)
  3844. *_byte |= (1 << _bit);
  3845. else if (value == 0)
  3846. *_byte ^= (1 << _bit);
  3847. }
  3848. void UserScanFunction()
  3849. {
  3850. }
  3851. unsigned char isModeChange(unsigned char gun_index)
  3852. {
  3853. unsigned char result = NO;
  3854. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3855. {
  3856. result = YES;
  3857. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3858. }
  3859. return result;
  3860. }
  3861. void AnnounceAccountBalance(unsigned char index, int balance)
  3862. {
  3863. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  3864. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  3865. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  3866. {
  3867. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AccountBalanceRequest = true;
  3868. }
  3869. LOG_INFO("%sAnnounce Account Balance %d To Connector %d",
  3870. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  3871. }
  3872. void FouceAnnounceAccountBalance(unsigned char index)
  3873. {
  3874. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  3875. {
  3876. AnnounceAccountBalance(index, 0);
  3877. }
  3878. }
  3879. void CleanConnectorUserIdAndPrice(unsigned char index)
  3880. {
  3881. if(chargingInfo[index]->SystemStatus == S_IDLE)
  3882. {
  3883. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  3884. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  3885. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  3886. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -6553500;
  3887. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  3888. }
  3889. }
  3890. void DispenserAuthInitial(unsigned char dispenser)
  3891. {
  3892. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  3893. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  3894. }
  3895. void GunAuthInitial(unsigned char connector)
  3896. {
  3897. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  3898. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  3899. }
  3900. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  3901. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  3902. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  3903. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  3904. void AuthorizeTimeoutProcess(void)
  3905. {
  3906. int CompletedTime = 0;
  3907. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3908. {
  3909. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  3910. {
  3911. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  3912. {
  3913. GetClockTime(&_GunAuth_Time[index]);
  3914. }
  3915. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  3916. {
  3917. FouceAnnounceAccountBalance(index);
  3918. }
  3919. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  3920. {
  3921. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  3922. {
  3923. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  3924. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  3925. CleanConnectorUserIdAndPrice(index);
  3926. }
  3927. }
  3928. }
  3929. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  3930. }
  3931. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3932. {
  3933. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  3934. {
  3935. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  3936. {
  3937. GetClockTime(&_DispenserAuth_Time[index]);
  3938. }
  3939. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  3940. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout : AUTHORIZE_COMPLETED_TIME;
  3941. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  3942. {
  3943. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", index + 1);
  3944. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  3945. }
  3946. }
  3947. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  3948. }
  3949. }
  3950. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  3951. {
  3952. BOOL success = false;
  3953. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  3954. {
  3955. success = true;
  3956. }
  3957. return success;
  3958. }
  3959. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  3960. {
  3961. BOOL success = false;
  3962. unsigned char dispenser = 0;
  3963. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  3964. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  3965. {
  3966. success = true;
  3967. }
  3968. return success;
  3969. }
  3970. unsigned short ParsingRatingPower(char *modelname)
  3971. {
  3972. char strVal[3], strExp[2];
  3973. unsigned short value = 0, exponent = 0;
  3974. memset(strVal, 0x00, sizeof(strVal));
  3975. memset(strExp, 0x00, sizeof(strExp));
  3976. memcpy(strVal, &modelname[4], 2);
  3977. memcpy(strExp, &modelname[6], 1);
  3978. value = atoi(strVal);
  3979. exponent = atoi(strExp);
  3980. if(value < 0 || exponent < 0)
  3981. {
  3982. return 0;
  3983. }
  3984. for(int i = 0; i < exponent; i++)
  3985. {
  3986. value *= 10;
  3987. }
  3988. return value;
  3989. }
  3990. bool CheckConnectorTypeStatus(void)
  3991. {
  3992. bool result = true;
  3993. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  3994. {
  3995. // DO360 for Audi, two gun only
  3996. _gunIndex = ShmChargerInfo->Control.MaxConnector;
  3997. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3998. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3999. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  4000. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  4001. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  4002. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  4003. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  4004. {
  4005. result = false;
  4006. }
  4007. //DO360 charging info default setting
  4008. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4009. {
  4010. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  4011. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  4012. chargingInfo[i]->Evboard_id = 0x00;
  4013. chargingInfo[i]->ReservationId = -1;
  4014. chargingInfo[i]->slotsIndex = 0;
  4015. chargingInfo[i]->SystemStatus = S_BOOTING;
  4016. chargingInfo[i]->Type = _Type_Unknown;
  4017. chargingInfo[i]->type_index = 0;
  4018. chargingInfo[i]->IsAvailable = YES;
  4019. }
  4020. }
  4021. else
  4022. {
  4023. // Module Name 不正確 - 告警
  4024. result = false;
  4025. }
  4026. return result;
  4027. }
  4028. void KillTaskExceptPrimary()
  4029. {
  4030. system("killall Module_Authorize");
  4031. system("killall Module_CabinetParallel");
  4032. system("killall Module_ChargerSelfTest");
  4033. system("killall Module_LedIndication");
  4034. system("killall Module_EvComm");
  4035. system("killall Module_InternalComm");
  4036. system("killall Module_PsuComm");
  4037. system("killall Module_4g &");
  4038. system("killall Module_Wifi &");
  4039. }
  4040. void KillAllTask()
  4041. {
  4042. system("killall Module_Authorize");
  4043. system("killall Module_CabinetParallel");
  4044. system("killall Module_ChargerSelfTest");
  4045. system("killall Module_LedIndication");
  4046. system("killall Module_EventLogging");
  4047. system("killall Module_PrimaryComm");
  4048. system("killall Module_EvComm");
  4049. system("killall Module_LcmControl");
  4050. system("killall Module_InternalComm");
  4051. system("killall Module_PsuComm");
  4052. system("killall OcppBackend &");
  4053. system("killall Module_4g &");
  4054. system("killall Module_Wifi &");
  4055. }
  4056. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  4057. {
  4058. int isPass = PASS;
  4059. unsigned char ftpcmdbuf[256];
  4060. LOG_INFO("Transfer image(%s) to ip: %s ", SourcePath, IpAddress);
  4061. //Using ftpput command to transfer upgrade image,
  4062. //User name : root
  4063. //User password : y42j/4cj84
  4064. //Destination : as same as source path
  4065. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  4066. if(system((char*)ftpcmdbuf) != 0)
  4067. {
  4068. isPass = FAIL;
  4069. LOG_INFO("Transfer Image NG - FTP put upgrade image to fail");
  4070. }
  4071. else
  4072. {
  4073. LOG_INFO("FTP put Image OK");
  4074. }
  4075. return isPass;
  4076. }
  4077. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  4078. {
  4079. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  4080. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  4081. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  4082. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  4083. }
  4084. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName)
  4085. {
  4086. int isPass = FAIL;
  4087. BOOL transferred = FALSE;
  4088. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4089. {
  4090. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  4091. {
  4092. char DispenserIpAddress[16];
  4093. memset((char*)DispenserIpAddress, 0x00, 16);
  4094. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  4095. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  4096. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  4097. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  4098. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  4099. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  4100. if(isPass == PASS)
  4101. {
  4102. TriggerDispenserUpgrade(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex, FileName);
  4103. }
  4104. transferred = true;
  4105. }
  4106. }
  4107. return transferred;
  4108. }
  4109. BOOL WaitAllDispenserUpgradeCompleted(void)
  4110. {
  4111. BOOL completed = TRUE;
  4112. GetClockTime(&_DispenserUpgrade_time);
  4113. do
  4114. {
  4115. completed = TRUE;
  4116. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4117. {
  4118. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  4119. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  4120. {
  4121. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  4122. {
  4123. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  4124. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  4125. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  4126. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  4127. }
  4128. else
  4129. {
  4130. completed = FALSE;
  4131. }
  4132. }
  4133. }
  4134. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  4135. {
  4136. LOG_INFO("Wait Upgrade Timeout");
  4137. completed = FALSE;
  4138. break;
  4139. }
  4140. if(completed == FALSE)
  4141. {
  4142. sleep(1);
  4143. }
  4144. }while(!completed);
  4145. return completed;
  4146. }
  4147. void UpgradePrepare(void)
  4148. {
  4149. int count = 0;
  4150. SetAcContactor(OFF);
  4151. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4152. {
  4153. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4154. {
  4155. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4156. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4157. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4158. {
  4159. count++;
  4160. }
  4161. }
  4162. }
  4163. if(count > 0)
  4164. {
  4165. sleep(3);
  4166. }
  4167. else
  4168. {
  4169. sleep(1);
  4170. }
  4171. }
  4172. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  4173. {
  4174. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  4175. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  4176. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  4177. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  4178. }
  4179. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  4180. {
  4181. int isPass = FAIL;
  4182. BOOL transferred = FALSE;
  4183. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4184. {
  4185. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4186. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4187. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4188. {
  4189. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  4190. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  4191. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  4192. {
  4193. char PCabinetIpAddress[16];
  4194. memset((char*)PCabinetIpAddress, 0x00, 16);
  4195. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  4196. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  4197. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  4198. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  4199. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  4200. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  4201. if(isPass == PASS)
  4202. {
  4203. TriggerPCabinetUpgrade(i, FileName);
  4204. }
  4205. transferred = true;
  4206. }
  4207. }
  4208. }
  4209. return transferred;
  4210. }
  4211. BOOL WaitAllPCabinetStartUpgrade(void)
  4212. {
  4213. BOOL isStart = true;
  4214. int count = 0;
  4215. struct timespec _waitStart;
  4216. GetClockTime(&_waitStart);
  4217. do
  4218. {
  4219. isStart = true;
  4220. count = 0;
  4221. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4222. {
  4223. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4224. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4225. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4226. {
  4227. count++;
  4228. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  4229. {
  4230. isStart = false;
  4231. }
  4232. }
  4233. }
  4234. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  4235. {
  4236. isStart = false;
  4237. break;
  4238. }
  4239. usleep(10000);
  4240. }while(!isStart);
  4241. if(isStart && count > 0)
  4242. {
  4243. LOG_INFO("Total %d PCabinet upgrade start...", count);
  4244. }
  4245. else
  4246. {
  4247. if(count > 0)
  4248. {
  4249. LOG_INFO("Wait PCabinet upgrade timeout");
  4250. }
  4251. else
  4252. {
  4253. LOG_INFO("No PCabinet need to upgrade");
  4254. }
  4255. }
  4256. return isStart;
  4257. }
  4258. BOOL WaitAllPCabinetUpgradeCompleted(void)
  4259. {
  4260. BOOL completed = true;
  4261. int count = 0;
  4262. struct timespec _waitCompleted;
  4263. GetClockTime(&_waitCompleted);
  4264. do
  4265. {
  4266. completed = true;
  4267. count = 0;
  4268. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4269. {
  4270. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4271. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4272. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4273. {
  4274. count++;
  4275. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  4276. {
  4277. completed = false;
  4278. }
  4279. }
  4280. }
  4281. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  4282. {
  4283. completed = false;
  4284. break;
  4285. }
  4286. usleep(10000);
  4287. }while(!completed);
  4288. if(completed && count > 0)
  4289. {
  4290. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  4291. }
  4292. else
  4293. {
  4294. if(count > 0)
  4295. {
  4296. LOG_INFO("Wait PCabinet upgrade completed timeout");
  4297. }
  4298. else
  4299. {
  4300. LOG_INFO("Need not to wait PCabinet completed");
  4301. }
  4302. }
  4303. return completed;
  4304. }
  4305. BOOL WaitMCabinetUpgradeCompleted(void)
  4306. {
  4307. BOOL completed = true;
  4308. struct timespec _waitCompleted;
  4309. GetClockTime(&_waitCompleted);
  4310. do
  4311. {
  4312. completed = false;
  4313. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  4314. {
  4315. completed = true;
  4316. }
  4317. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  4318. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  4319. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  4320. {
  4321. completed = false;
  4322. break;
  4323. }
  4324. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  4325. {
  4326. completed = false;
  4327. break;
  4328. }
  4329. usleep(10000);
  4330. }while(!completed);
  4331. if(completed)
  4332. {
  4333. LOG_INFO("MCabinet upgrade completed!");
  4334. }
  4335. else
  4336. {
  4337. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  4338. }
  4339. return completed;
  4340. }
  4341. bool IsModelNameMatch(char *model)
  4342. {
  4343. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  4344. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  4345. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  4346. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  4347. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  4348. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  4349. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  4350. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  4351. {
  4352. return true;
  4353. }
  4354. return false;
  4355. }
  4356. char CheckUpdateProcess()
  4357. {
  4358. char model[32];
  4359. bool support = false;
  4360. bool OneImageOnly = false;
  4361. int isPass = true;
  4362. uint8_t retSucc = 0;
  4363. uint8_t retFail = 0;
  4364. byte target = 0;
  4365. char *new_str = NULL;
  4366. unsigned char *ptr = NULL;
  4367. int fd = 0;
  4368. int uartFd = 0;
  4369. unsigned int Type = 0;
  4370. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  4371. DIR *d;
  4372. struct dirent *dir;
  4373. char cmdBuf[128];
  4374. d = opendir("/mnt/");
  4375. if(d)
  4376. {
  4377. while((dir = readdir(d)) != NULL && !OneImageOnly)
  4378. {
  4379. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  4380. continue;
  4381. }
  4382. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  4383. strcat(new_str, "/mnt/");
  4384. strcat(new_str, dir->d_name);
  4385. fd = open(new_str, O_RDONLY);
  4386. if(fd < 0)
  4387. {
  4388. close(fd);
  4389. free(new_str);
  4390. continue;
  4391. }
  4392. LOG_INFO("%s\n", new_str);
  4393. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  4394. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  4395. //get the image length
  4396. ImageLen = read(fd, ptr, MaxLen);
  4397. if (ImageLen > 20)
  4398. {
  4399. support = false;
  4400. memcpy(model, ptr, 16);
  4401. LOG_INFO("Model: %s", model);
  4402. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  4403. LOG_INFO("Typed...%x", Type);
  4404. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  4405. if(IsModelNameMatch(model))
  4406. {
  4407. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4408. {
  4409. PutImageToAllParallelCabinet(new_str, dir->d_name);
  4410. WaitAllPCabinetStartUpgrade();
  4411. }
  4412. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  4413. {
  4414. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  4415. sleep(1);
  4416. }
  4417. switch (Type)
  4418. {
  4419. case 0x10000001:
  4420. case 0x10000002:
  4421. case 0x10000003:
  4422. case 0x10000004:
  4423. case 0x10000005:
  4424. support = true;
  4425. _UpgradeNeedReboot = TRUE;
  4426. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4427. break;
  4428. case 0x10000006: // primary mcu
  4429. case 0x1000000D: // relay board
  4430. case 0x1000000E: // fan board
  4431. case 0x10000014: // led board
  4432. support = true;
  4433. target = 0x00;
  4434. if (Type == 0x10000006)
  4435. {
  4436. target = UPGRADE_PRI;
  4437. }
  4438. else if (Type == 0x1000000D)
  4439. {
  4440. target = UPGRADE_RB;
  4441. }
  4442. else if (Type == 0x1000000E)
  4443. {
  4444. target = UPGRADE_FAN;
  4445. }
  4446. else if (Type == 0x10000014)
  4447. {
  4448. target = UPGRADE_LED;
  4449. }
  4450. uartFd = InitComPort(target);
  4451. if(target == UPGRADE_RB)
  4452. {
  4453. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4454. if(isPass == PASS)
  4455. {
  4456. retSucc++;
  4457. }
  4458. else
  4459. {
  4460. retFail++;
  4461. }
  4462. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  4463. sleep(3);
  4464. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4465. }
  4466. else
  4467. {
  4468. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4469. }
  4470. close(uartFd);
  4471. break;
  4472. }
  4473. if(support)
  4474. {
  4475. if(isPass == PASS)
  4476. {
  4477. retSucc++;
  4478. }
  4479. else
  4480. {
  4481. retFail++;
  4482. }
  4483. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  4484. }
  4485. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4486. {
  4487. LOG_INFO("MCabinet upgrade completed");
  4488. WaitAllPCabinetUpgradeCompleted();
  4489. }
  4490. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  4491. {
  4492. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  4493. LOG_INFO("SCabinet upgrade completed");
  4494. WaitMCabinetUpgradeCompleted();
  4495. }
  4496. }
  4497. else
  4498. {
  4499. if(model[0] == 'D' && (model[1] == 'D' || model[1] == 'X'))
  4500. {
  4501. OneImageOnly = true;
  4502. _UpgradeNeedReboot = TRUE;
  4503. LOG_INFO("Start Put Image To All Dispenser");
  4504. PutImageToAllDispenser(new_str, dir->d_name);
  4505. WaitAllDispenserUpgradeCompleted();
  4506. LOG_INFO("All Dispenser Upgrade Completed");
  4507. }
  4508. }
  4509. }
  4510. sprintf(cmdBuf, "rm -f %s", new_str);
  4511. system(cmdBuf);
  4512. close(fd);
  4513. free(new_str);
  4514. free(ptr);
  4515. }
  4516. }
  4517. free(dir);
  4518. closedir(d);
  4519. if(retFail != 0)
  4520. {
  4521. return FAIL;
  4522. }
  4523. return PASS;
  4524. }
  4525. void TriggerDispenserConnectionTimeoutSetting(void)
  4526. {
  4527. BOOL trigger = FALSE;
  4528. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4529. {
  4530. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4531. {
  4532. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  4533. trigger = TRUE;
  4534. }
  4535. }
  4536. if(trigger)
  4537. {
  4538. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout);
  4539. }
  4540. }
  4541. void CheckConnectionTimeout(void)
  4542. {
  4543. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4544. {
  4545. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4546. }
  4547. else
  4548. {
  4549. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  4550. {
  4551. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  4552. if(_connectionTimeout <= 0)
  4553. {
  4554. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4555. }
  4556. }
  4557. else
  4558. {
  4559. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4560. }
  4561. }
  4562. if(_connectionTimeout != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  4563. {
  4564. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout = _connectionTimeout;
  4565. TriggerDispenserConnectionTimeoutSetting();
  4566. }
  4567. }
  4568. void CreateTimeoutFork()
  4569. {
  4570. pid_t timeoutPid;
  4571. timeoutPid = fork();
  4572. if (timeoutPid == 0)
  4573. {
  4574. GetClockTime(&_cmdSubPriority_time);
  4575. CheckConnectionTimeout();
  4576. LOG_INFO("Timeout Fork PID = %d", getpid());
  4577. while(true)
  4578. {
  4579. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  4580. {
  4581. CheckConnectionTimeout();
  4582. GetClockTime(&_cmdSubPriority_time);
  4583. }
  4584. sleep(1);
  4585. }
  4586. }
  4587. }
  4588. void CheckFactoryConfigFunction(void)
  4589. {
  4590. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4591. {
  4592. char Buf[256];
  4593. sprintf(Buf, "cd /root;./FactoryConfig -m");
  4594. system(Buf);
  4595. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4596. system("sync");
  4597. sleep(5);
  4598. system("reboot -f");
  4599. sleep(5);
  4600. system("reboot -f");
  4601. }
  4602. }
  4603. void CheckFwUpdateFunction(void)
  4604. {
  4605. char UpdateResult = PASS;
  4606. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4607. {
  4608. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  4609. UpgradePrepare();
  4610. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  4611. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  4612. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  4613. LOG_INFO("ftp : update start.");
  4614. TryCloseWatchdog();
  4615. //KillTask();
  4616. system("killall Module_PrimaryComm");
  4617. system("killall Module_InternalComm");
  4618. UpdateResult = CheckUpdateProcess();
  4619. if (UpdateResult == PASS)
  4620. {
  4621. LOG_INFO("ftp : update complete.");
  4622. }
  4623. else
  4624. {
  4625. LOG_INFO("ftp : update fail.");
  4626. }
  4627. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4628. sleep(10);
  4629. if(_UpgradeNeedReboot)
  4630. {
  4631. system("reboot -f");
  4632. }
  4633. else
  4634. {
  4635. KillAllTask();
  4636. system("/usr/bin/run_evse_restart.sh");
  4637. }
  4638. }
  4639. else if(Is_Ocpp_UpdateFirmwareReq())
  4640. {
  4641. Clean_Ocpp_UpdateFirmwareReq();
  4642. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  4643. UpgradePrepare();
  4644. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  4645. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  4646. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  4647. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  4648. {
  4649. LOG_INFO("Backend : update start.");
  4650. TryCloseWatchdog();
  4651. Set_Ocpp_UpdateFirmwareStatus("Installing");
  4652. Set_Ocpp_FirmwareStatusNotificationReq();
  4653. //KillTask();
  4654. system("killall Module_PrimaryComm");
  4655. system("killall Module_InternalComm");
  4656. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4657. {
  4658. setChargerMode(_index, MODE_UPDATE);
  4659. }
  4660. UpdateResult = CheckUpdateProcess();
  4661. if (UpdateResult == PASS)
  4662. {
  4663. LOG_INFO("Backend : update complete.");
  4664. Set_Ocpp_UpdateFirmwareStatus("Installed");
  4665. }
  4666. else
  4667. {
  4668. LOG_INFO("Backend : update fail.");
  4669. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  4670. }
  4671. Set_Ocpp_FirmwareStatusNotificationReq();
  4672. sleep(10);
  4673. if(_UpgradeNeedReboot)
  4674. {
  4675. system("reboot -f");
  4676. }
  4677. else
  4678. {
  4679. KillAllTask();
  4680. system("/usr/bin/run_evse_restart.sh");
  4681. }
  4682. }
  4683. }
  4684. }
  4685. void ExecuteWriteWiringInfo(void)
  4686. {
  4687. struct SysConfigData config;
  4688. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  4689. config.SwitchDebugFlag = NO;
  4690. strcpy((char *) config.UserId, "");
  4691. int result = StoreUsrConfigData(&config);
  4692. if(result != 1)
  4693. {
  4694. LOG_INFO("Store User Config NG");
  4695. }
  4696. else
  4697. {
  4698. LOG_INFO("Store User Config OK");
  4699. }
  4700. }
  4701. struct timespec _WiringInfo_time;
  4702. int CheckWiringInfoUpdate(void)
  4703. {
  4704. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.FlashConfigChanged)
  4705. {
  4706. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.FlashConfigChanged = false;
  4707. LOG_INFO("Flash Config Changed");
  4708. GetClockTime(&_WiringInfo_time);
  4709. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteFlash = true;
  4710. }
  4711. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteFlash)
  4712. {
  4713. // 30s for reboot request timeout
  4714. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  4715. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo)
  4716. {
  4717. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteFlash = false;
  4718. LOG_INFO("Start Write To Flash");
  4719. pid_t writePid = fork();
  4720. if(writePid == 0)
  4721. {
  4722. ExecuteWriteWiringInfo();
  4723. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo)
  4724. {
  4725. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo = false;
  4726. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  4727. LOG_INFO("Clean Wiring Info And Need Soft Reset");
  4728. }
  4729. return NO;
  4730. }
  4731. LOG_INFO("Clean Write Flahs Flag");
  4732. }
  4733. }
  4734. return YES;
  4735. }
  4736. void CheckSoftHardReset(void)
  4737. {
  4738. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  4739. {
  4740. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  4741. LOG_INFO("********** Need Soft Reset **********");
  4742. sleep(5);
  4743. system("killall OcppBackend &");
  4744. KillAllTask();
  4745. TryCloseWatchdog();
  4746. system("/usr/bin/run_evse_restart.sh");
  4747. }
  4748. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  4749. {
  4750. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  4751. LOG_INFO("********** Need Hard Reset **********");
  4752. sleep(5);
  4753. system("reboot -f");
  4754. }
  4755. }
  4756. struct timespec _Reboot_time;
  4757. void TriggerDispenserHardwareReboot(void)
  4758. {
  4759. BOOL trigger = FALSE;
  4760. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4761. {
  4762. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4763. {
  4764. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  4765. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest == 0)
  4766. {
  4767. trigger = TRUE;
  4768. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest = 1;
  4769. }
  4770. }
  4771. }
  4772. if(trigger)
  4773. {
  4774. GetClockTime(&_Reboot_time);
  4775. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  4776. }
  4777. }
  4778. BOOL IsDispenserHardwareRebootResponsed(void)
  4779. {
  4780. BOOL response_ok = TRUE;
  4781. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4782. {
  4783. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4784. {
  4785. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  4786. {
  4787. response_ok = FALSE;
  4788. // 10s for reboot request timeout
  4789. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  4790. {
  4791. response_ok = TRUE;
  4792. LOG_INFO("Reboot request timeout");
  4793. }
  4794. }
  4795. }
  4796. }
  4797. return response_ok;
  4798. }
  4799. struct timespec _Reset_time;
  4800. void TriggerDispenserSoftwareReset(void)
  4801. {
  4802. BOOL trigger = FALSE;
  4803. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4804. {
  4805. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4806. {
  4807. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  4808. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest == 0)
  4809. {
  4810. trigger = TRUE;
  4811. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest = 1;
  4812. }
  4813. }
  4814. }
  4815. if(trigger)
  4816. {
  4817. GetClockTime(&_Reset_time);
  4818. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  4819. }
  4820. }
  4821. BOOL IsDispenserSoftwareResetResponsed(void)
  4822. {
  4823. BOOL response_ok = TRUE;
  4824. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4825. {
  4826. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4827. {
  4828. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  4829. {
  4830. response_ok = FALSE;
  4831. // 10s for reset request timeout
  4832. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  4833. {
  4834. response_ok = TRUE;
  4835. LOG_INFO("Reset request timeout");
  4836. }
  4837. }
  4838. }
  4839. }
  4840. return response_ok;
  4841. }
  4842. // the unit of return value: 0.01 dollar
  4843. int TariffParsing(char *StringItem, char *TariffCode)
  4844. {
  4845. char *ptrSave, *ptrToken;
  4846. char strSource[128];
  4847. int fee = 0;
  4848. memcpy(strSource, StringItem, 128);
  4849. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4850. while(ptrToken != NULL)
  4851. {
  4852. char *strTariff = strstr(ptrToken, TariffCode);
  4853. if(strTariff != NULL)
  4854. {
  4855. char *strFee = strstr(strTariff, "$");
  4856. if(strFee != NULL)
  4857. {
  4858. strFee++;
  4859. float fFee = atof(strFee);
  4860. fee = (int)(fFee * 100);
  4861. break;
  4862. }
  4863. }
  4864. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4865. }
  4866. return fee;
  4867. }
  4868. char MyDefaultPriceString[500];
  4869. void DefaultPriceHandler(void)
  4870. {
  4871. char price[500];
  4872. Get_Ocpp_DefaultPrice((char *)price);
  4873. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  4874. {
  4875. BOOL trigger = FALSE;
  4876. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4877. LOG_INFO("********** Default Price: %s", price);
  4878. ConnectionFee = TariffParsing(price, "Connection Fee");
  4879. LOG_INFO("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4880. CurrentRate = TariffParsing(price, "Current Rate");
  4881. LOG_INFO("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4882. OccupancyFee = TariffParsing(price, "Occupancy Fee");
  4883. LOG_INFO("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4884. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice = CurrentRate;
  4885. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4886. {
  4887. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4888. {
  4889. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest == 0)
  4890. {
  4891. trigger = TRUE;
  4892. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest = 1;
  4893. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest = 1;
  4894. }
  4895. }
  4896. }
  4897. if(trigger)
  4898. {
  4899. LOG_INFO("********** Trigger Dispenser Default Price **********");
  4900. }
  4901. strcpy(MyDefaultPriceString, price);
  4902. }
  4903. }
  4904. void UserPriceHandler(void)
  4905. {
  4906. char UserId[32];
  4907. char preice[500];
  4908. Get_Ocpp_UserPrice(UserId, preice);
  4909. if(strlen((char *)UserId) > 0 && strlen((char *)preice) > 0)
  4910. {
  4911. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4912. int AccountBalance = 0;
  4913. ConnectionFee = TariffParsing((char *)preice, "Connection Fee");
  4914. CurrentRate = TariffParsing((char *)preice, "Current Rate");
  4915. OccupancyFee = TariffParsing((char *)preice, "Occupancy Fee");
  4916. AccountBalance = TariffParsing((char *)preice, "Account Balance");
  4917. LOG_INFO("********** User Id: %s **********", UserId);
  4918. LOG_INFO("********** User Price: %s", preice);
  4919. LOG_INFO("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4920. LOG_INFO("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4921. LOG_INFO("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4922. LOG_INFO("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4923. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4924. {
  4925. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  4926. {
  4927. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  4928. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4929. {
  4930. LOG_INFO("********** Connector Id %d User Price %d.%02d, Account Balance %d.%02d **********",
  4931. (i + 1), (CurrentRate / 100), (CurrentRate % 100), (AccountBalance / 100), (AccountBalance % 100));
  4932. }
  4933. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  4934. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4935. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  4936. {
  4937. AnnounceAccountBalance(i, AccountBalance);
  4938. }
  4939. }
  4940. }
  4941. Clean_Ocpp_UserPrice();
  4942. }
  4943. }
  4944. void RunningFinalCostHandler(void)
  4945. {
  4946. char strCost[256];
  4947. unsigned int ConnectionFee = 0, SessionFee = 0, OccupancyFee = 0, TotalCost = 0;
  4948. int AccountBalance = 0;
  4949. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4950. {
  4951. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  4952. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_ALARM)
  4953. {
  4954. Get_Ocpp_RunningCost(i, strCost);
  4955. if(strlen((char *)strCost) > 0)
  4956. {
  4957. ConnectionFee = TariffParsing((char *)strCost, "Connection Fee");
  4958. SessionFee = TariffParsing((char *)strCost, "Session Fee");
  4959. OccupancyFee = TariffParsing((char *)strCost, "Occupancy Fee");
  4960. TotalCost = TariffParsing((char *)strCost, "Total Cost");
  4961. AccountBalance = TariffParsing((char *)strCost, "Account Balance");
  4962. LOG_INFO("********** Connector Id: %d Running Cost **********", (i + 1));
  4963. LOG_INFO("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4964. LOG_INFO("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4965. LOG_INFO("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4966. LOG_INFO("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4967. LOG_INFO("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4968. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4969. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4970. {
  4971. LOG_INFO("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4972. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4973. }
  4974. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4975. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4976. Clean_Ocpp_RunningCost(i);
  4977. }
  4978. Get_Ocpp_FinalCost(i, strCost);
  4979. if(strlen((char *)strCost) > 0)
  4980. {
  4981. ConnectionFee = TariffParsing((char *)strCost, "Connection Fee");
  4982. SessionFee = TariffParsing((char *)strCost, "Session Fee");
  4983. OccupancyFee = TariffParsing((char *)strCost, "Occupancy Fee");
  4984. TotalCost = TariffParsing((char *)strCost, "Total Cost");
  4985. AccountBalance = TariffParsing((char *)strCost, "Account Balance");
  4986. LOG_INFO("********** Connector Id: %d Final Cost **********", (i + 1));
  4987. LOG_INFO("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4988. LOG_INFO("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4989. LOG_INFO("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4990. LOG_INFO("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4991. LOG_INFO("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4992. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4993. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4994. {
  4995. LOG_INFO("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4996. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4997. }
  4998. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4999. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  5000. Clean_Ocpp_FinalCost(i);
  5001. }
  5002. }
  5003. }
  5004. }
  5005. struct timespec _Cost_Price_time;
  5006. char MyConnectorRunningCostString[CONNECTOR_QUANTITY][128];
  5007. char MyConnectorFinalCostString[CONNECTOR_QUANTITY][128];
  5008. void CheckOcppCostAndPrice(void)
  5009. {
  5010. // 5s for reboot request timeout
  5011. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  5012. {
  5013. GetClockTime(&_Cost_Price_time);
  5014. DefaultPriceHandler();
  5015. UserPriceHandler();
  5016. RunningFinalCostHandler();
  5017. }
  5018. }
  5019. //===============================================
  5020. // OCPP
  5021. //===============================================
  5022. void CheckOcppStatus()
  5023. {
  5024. if (Is_Ocpp_BootNotification())
  5025. {
  5026. Clean_Ocpp_BootNotification();
  5027. LOG_INFO("*********************OCPP Boot Notification****************************");
  5028. }
  5029. if (Is_Ocpp_ResetReq())
  5030. {
  5031. bool canReset = true;
  5032. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  5033. {
  5034. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5035. {
  5036. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  5037. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  5038. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  5039. {
  5040. canReset = false;
  5041. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  5042. //{
  5043. // ChargingTerminalProcess(_index);
  5044. //}
  5045. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  5046. {
  5047. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  5048. }
  5049. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  5050. {
  5051. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  5052. }
  5053. }
  5054. }
  5055. }
  5056. if(canReset)
  5057. {
  5058. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  5059. {
  5060. //trigger dispenser to hardware reboot
  5061. TriggerDispenserHardwareReboot();
  5062. if(IsDispenserHardwareRebootResponsed())
  5063. {
  5064. Clean_Ocpp_ResetReq();
  5065. LOG_INFO("****** Hard Reboot ******");
  5066. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  5067. {
  5068. setChargerMode(i, MODE_MAINTAIN);
  5069. }
  5070. sleep(3);
  5071. system("reboot -f");
  5072. }
  5073. }
  5074. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  5075. {
  5076. //trigger dispenser to software reset
  5077. TriggerDispenserSoftwareReset();
  5078. if(IsDispenserSoftwareResetResponsed())
  5079. {
  5080. Clean_Ocpp_ResetReq();
  5081. LOG_INFO("****** Soft Reboot ******");
  5082. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  5083. {
  5084. setChargerMode(i, MODE_MAINTAIN);
  5085. }
  5086. sleep(3);
  5087. system("killall OcppBackend &");
  5088. KillAllTask();
  5089. TryCloseWatchdog();
  5090. system("/usr/bin/run_evse_restart.sh");
  5091. }
  5092. }
  5093. }
  5094. }
  5095. CheckOcppCostAndPrice();
  5096. }
  5097. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  5098. {
  5099. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  5100. {
  5101. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  5102. {
  5103. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  5104. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  5105. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  5106. }
  5107. }
  5108. }
  5109. void OcppStartTransaction(byte gunIndex)
  5110. {
  5111. Set_Ocpp_StartTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  5112. LOG_INFO("Gun %d Start Transaction IdTag = %s", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId);
  5113. Clean_Ocpp_StartTransactionConf(gunIndex);
  5114. Set_Ocpp_StartTransactionReq(gunIndex);
  5115. }
  5116. void OcppStopTransaction(byte gunIndex)
  5117. {
  5118. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  5119. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d",
  5120. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->EvBatterySoc);
  5121. Set_Ocpp_StopTransactionReq(gunIndex);
  5122. }
  5123. void ChkOcppStatus(byte gunIndex)
  5124. {
  5125. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  5126. if(Is_Ocpp_ReserveNowReq(gunIndex))
  5127. {
  5128. Clean_Ocpp_ReserveNowReq(gunIndex);
  5129. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  5130. {
  5131. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  5132. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  5133. setChargerMode(gunIndex, MODE_RESERVATION);
  5134. }
  5135. Set_Ocpp_ReserveNowConf(gunIndex);
  5136. }
  5137. if(Is_Ocpp_CancelReservationReq(gunIndex))
  5138. {
  5139. Clean_Ocpp_CancelReservationReq(gunIndex);
  5140. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  5141. {
  5142. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  5143. chargingInfo[gunIndex]->ReservationId = -1;
  5144. setChargerMode(gunIndex, MODE_IDLE);
  5145. }
  5146. Set_Ocpp_CancelReservationConf(gunIndex);
  5147. }
  5148. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  5149. {
  5150. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  5151. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  5152. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  5153. {
  5154. if (isDb_ready)
  5155. DB_Update_Operactive(localDb, gunIndex, true);
  5156. chargingInfo[gunIndex]->IsAvailable = YES;
  5157. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  5158. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  5159. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  5160. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  5161. {
  5162. setChargerMode(gunIndex, MODE_IDLE);
  5163. }
  5164. }
  5165. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  5166. {
  5167. if (isDb_ready)
  5168. DB_Update_Operactive(localDb, gunIndex, false);
  5169. chargingInfo[gunIndex]->IsAvailable = NO;
  5170. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  5171. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  5172. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  5173. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  5174. {
  5175. setChargerMode(gunIndex, MODE_MAINTAIN);
  5176. }
  5177. }
  5178. }
  5179. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  5180. {
  5181. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  5182. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  5183. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  5184. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  5185. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  5186. {
  5187. // 充電中,需停止充電
  5188. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  5189. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.UnlockStopRequest = true;
  5190. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  5191. }
  5192. Set_Ocpp_UnlockConnectorConf(gunIndex);
  5193. }
  5194. if (Is_Ocpp_RemoteStopReq(gunIndex))
  5195. {
  5196. Clean_Ocpp_RemoteStopReq(gunIndex);
  5197. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  5198. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  5199. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  5200. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  5201. {
  5202. Set_Ocpp_StopReason(gunIndex, "Remote");
  5203. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.RemoteStopRequest = true;
  5204. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  5205. }
  5206. }
  5207. if(Is_Ocpp_StartTransactionConf(gunIndex))
  5208. {
  5209. if(Is_Ocpp_StartTransaction_Accepted(gunIndex))
  5210. {
  5211. char acceptId[128];
  5212. Get_Ocpp_TransactionId(gunIndex, acceptId);
  5213. LOG_INFO("Gun %d OCPP Start Transaction Accepted, TransactionId [%s]", gunIndex + 1, acceptId);
  5214. }
  5215. else
  5216. {
  5217. if(Is_Ocpp_StopOnInvalidId_Enable())
  5218. {
  5219. if(Is_Invalid_StartTransactionId(gunIndex))
  5220. {
  5221. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  5222. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  5223. }
  5224. }
  5225. }
  5226. Clean_Ocpp_StartTransactionConf(gunIndex);
  5227. }
  5228. }
  5229. bool CheckBackendChargingTimeout(byte gunIndex)
  5230. {
  5231. bool result = false;
  5232. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  5233. {
  5234. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  5235. {
  5236. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  5237. result = true;
  5238. }
  5239. }
  5240. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  5241. {
  5242. // 隨插即充電的要看 offline
  5243. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  5244. {
  5245. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  5246. result = true;
  5247. }
  5248. }
  5249. return result;
  5250. }
  5251. bool CheckBackendChargingEnergy(byte gunIndex)
  5252. {
  5253. bool result = false;
  5254. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  5255. {
  5256. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  5257. {
  5258. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  5259. result = true;
  5260. }
  5261. }
  5262. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  5263. {
  5264. // 隨插即充電的要看 offline
  5265. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  5266. {
  5267. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  5268. result = true;
  5269. }
  5270. }
  5271. return result;
  5272. }
  5273. void InformOcppErrOccur(byte codeType)
  5274. {
  5275. char _error[25];
  5276. switch(codeType)
  5277. {
  5278. case 4: strcpy(_error, "InternalError"); break;
  5279. case 6: strcpy(_error, "NoError"); break;
  5280. case 7: strcpy(_error, "OtherError"); break;
  5281. case 13: strcpy(_error, "UnderVoltage"); break;
  5282. case 14: strcpy(_error, "OverVoltage"); break;
  5283. }
  5284. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5285. {
  5286. SetOcppErrorCode(gun_index, _error);
  5287. }
  5288. }
  5289. void SetOcppErrorCode(unsigned char _index, char *errCode)
  5290. {
  5291. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  5292. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  5293. }
  5294. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  5295. {
  5296. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  5297. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  5298. }
  5299. //===============================================
  5300. // SQLite3 related routine
  5301. //===============================================
  5302. int NetworkDB_Open(sqlite3 *db)
  5303. {
  5304. int result = PASS;
  5305. char* errMsg = NULL;
  5306. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  5307. "idx integer primary key AUTOINCREMENT, "
  5308. "occurDatetime text NOT NULL, "
  5309. "isInternet text NOT NULL, "
  5310. "isOcppConnected text NOT NULL, "
  5311. "isEth0Internet text NOT NULL, "
  5312. "isMlan0Internet text NOT NULL, "
  5313. "isPpp0Internet text NOT NULL, "
  5314. "rssiMlan0 text NOT NULL, "
  5315. "rssiPpp0 text NOT NULL"
  5316. ");";
  5317. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  5318. if(sqlite3_open(NETWORK_DB_FILE, &db))
  5319. {
  5320. result = FAIL;
  5321. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  5322. sqlite3_close(db);
  5323. }
  5324. else
  5325. {
  5326. LOG_INFO("Local network status record database open successfully.");
  5327. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  5328. {
  5329. result = FAIL;
  5330. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  5331. }
  5332. else
  5333. {
  5334. LOG_INFO("Opened local network status record table successfully");
  5335. }
  5336. sqlite3_close(db);
  5337. }
  5338. return result;
  5339. }
  5340. int DB_Open(sqlite3 *db)
  5341. {
  5342. int result = PASS;
  5343. char* errMsg = NULL;
  5344. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  5345. "idx integer primary key AUTOINCREMENT, "
  5346. "reservationId text, "
  5347. "transactionId text, "
  5348. "startMethod text, "
  5349. "userId text, "
  5350. "dateTimeStart text, "
  5351. "dateTimeStop text,"
  5352. "socStart text, "
  5353. "socStop text, "
  5354. "chargeEnergy text, "
  5355. "stopReason text"
  5356. ");";
  5357. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  5358. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5359. "`IsAvailable` TEXT NOT NULL, "
  5360. "`connector` INTEGER NOT NULL, "
  5361. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  5362. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  5363. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5364. "`occurDatetime` TEXT NOT NULL, "
  5365. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  5366. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  5367. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5368. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  5369. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  5370. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5371. "`connector` INTEGER NOT NULL, "
  5372. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  5373. if(sqlite3_open(DB_FILE, &db))
  5374. {
  5375. result = FAIL;
  5376. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  5377. sqlite3_close(db);
  5378. }
  5379. else
  5380. {
  5381. LOG_INFO( "Local charging record database open successfully.");
  5382. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  5383. {
  5384. result = FAIL;
  5385. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  5386. }
  5387. else
  5388. {
  5389. LOG_INFO( "Opened local charging record table successfully");
  5390. }
  5391. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  5392. {
  5393. result = FAIL;
  5394. LOG_ERROR( "Create local config table error message: %s", errMsg);
  5395. }
  5396. else
  5397. {
  5398. LOG_INFO( "Opened local config table successfully");
  5399. }
  5400. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  5401. {
  5402. result = FAIL;
  5403. LOG_ERROR( "Create local record table error message: %s", errMsg);
  5404. }
  5405. else
  5406. {
  5407. LOG_INFO( "Opened local record table successfully");
  5408. }
  5409. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  5410. {
  5411. result = FAIL;
  5412. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  5413. }
  5414. else
  5415. {
  5416. LOG_INFO( "Opened reboot record table successfully");
  5417. }
  5418. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  5419. {
  5420. result = FAIL;
  5421. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  5422. }
  5423. else
  5424. {
  5425. LOG_INFO( "Opened power consumption record table successfully");
  5426. }
  5427. sqlite3_close(db);
  5428. }
  5429. return result;
  5430. }
  5431. int DB_Insert_Record(sqlite3 *db, int gun_index)
  5432. {
  5433. int result = PASS;
  5434. char* errMsg = NULL;
  5435. char insertSql[1024];
  5436. char transactionId[128];
  5437. char reason[32];
  5438. Get_Ocpp_TransactionId(gun_index, transactionId);
  5439. Get_Ocpp_StopReason(gun_index, reason);
  5440. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  5441. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  5442. chargingInfo[gun_index]->ReservationId,
  5443. transactionId,
  5444. chargingInfo[gun_index]->StartMethod,
  5445. chargingInfo[gun_index]->StartUserId,
  5446. chargingInfo[gun_index]->StartDateTime,
  5447. chargingInfo[gun_index]->StopDateTime,
  5448. chargingInfo[gun_index]->EvBatteryStartSoc,
  5449. chargingInfo[gun_index]->EvBatterySoc,
  5450. chargingInfo[gun_index]->PresentChargedEnergy,
  5451. reason);
  5452. if(sqlite3_open(DB_FILE, &db))
  5453. {
  5454. result = FAIL;
  5455. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  5456. sqlite3_close(db);
  5457. }
  5458. else
  5459. {
  5460. //LOG_INFO( "Local charging record database open successfully.");
  5461. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  5462. {
  5463. result = FAIL;
  5464. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  5465. }
  5466. else
  5467. {
  5468. //LOG_INFO("Insert local charging record successfully");
  5469. }
  5470. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  5471. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  5472. {
  5473. result = FAIL;
  5474. LOG_ERROR("delete local charging error message: %s", errMsg);
  5475. }
  5476. else
  5477. {
  5478. //LOG_INFO("delete local charging record successfully");
  5479. }
  5480. sqlite3_close(db);
  5481. }
  5482. return result;
  5483. }
  5484. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  5485. {
  5486. uint8_t result = false;
  5487. char* errMsg = NULL;
  5488. char sqlStr[1024];
  5489. srand(time(NULL));
  5490. if(sqlite3_open(DB_FILE, &db))
  5491. {
  5492. result = FAIL;
  5493. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  5494. sqlite3_close(db);
  5495. }
  5496. else
  5497. {
  5498. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  5499. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  5500. LOG_INFO("sqlStr= %s", sqlStr);
  5501. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  5502. {
  5503. result = FAIL;
  5504. LOG_INFO( "update config error message: %s", errMsg);
  5505. }
  5506. else
  5507. {
  5508. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  5509. }
  5510. sqlite3_close(db);
  5511. }
  5512. return result;
  5513. }
  5514. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  5515. {
  5516. uint8_t result = true;
  5517. char* errMsg = NULL;
  5518. char sqlStr[1024];
  5519. char **rs;
  5520. int rows, cols;
  5521. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  5522. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  5523. if(sqlite3_open(DB_FILE, &db))
  5524. {
  5525. result = FAIL;
  5526. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  5527. sqlite3_close(db);
  5528. }
  5529. else
  5530. {
  5531. LOG_INFO( "Local config query database open successfully.");
  5532. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  5533. if(rows>0)
  5534. {
  5535. for(int idxRow=1;idxRow<=rows;idxRow++)
  5536. {
  5537. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  5538. {
  5539. result = false;
  5540. }
  5541. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  5542. }
  5543. }
  5544. else
  5545. {
  5546. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  5547. }
  5548. sqlite3_free_table(rs);
  5549. sqlite3_close(db);
  5550. }
  5551. return result;
  5552. }
  5553. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  5554. {
  5555. char* errMsg = NULL;
  5556. char sqlStr[1024];
  5557. srand(time(NULL));
  5558. if(sqlite3_open(DB_FILE, &db))
  5559. {
  5560. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  5561. sqlite3_close(db);
  5562. }
  5563. else
  5564. {
  5565. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  5566. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  5567. {
  5568. LOG_ERROR("update power_consumption error message: %s", errMsg);
  5569. }
  5570. else
  5571. {
  5572. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  5573. }
  5574. sqlite3_close(db);
  5575. }
  5576. }
  5577. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  5578. {
  5579. float result = 0;
  5580. char* errMsg = NULL;
  5581. char sqlStr[1024];
  5582. char **rs;
  5583. int rows, cols;
  5584. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  5585. if (sqlite3_open(DB_FILE, &db))
  5586. {
  5587. result = FAIL;
  5588. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  5589. sqlite3_close(db);
  5590. }
  5591. else
  5592. {
  5593. //LOG_INFO("Local config query database open successfully.");
  5594. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  5595. if (rows > 0)
  5596. {
  5597. for (int idxRow = 1; idxRow <= rows; idxRow++)
  5598. {
  5599. result = atof(rs[(idxRow * cols) + 2]);
  5600. }
  5601. }
  5602. else
  5603. {
  5604. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  5605. }
  5606. sqlite3_free_table(rs);
  5607. sqlite3_close(db);
  5608. }
  5609. return result;
  5610. }
  5611. int DB_Reboot_Record(sqlite3 *db)
  5612. {
  5613. int result = PASS;
  5614. char* errMsg = NULL;
  5615. char insertSql[256];
  5616. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  5617. if(sqlite3_open(DB_FILE, &db))
  5618. {
  5619. result = FAIL;
  5620. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  5621. sqlite3_close(db);
  5622. }
  5623. else
  5624. {
  5625. LOG_INFO("Local charging record database open successfully.");
  5626. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  5627. {
  5628. result = FAIL;
  5629. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  5630. }
  5631. else
  5632. {
  5633. //LOG_INFO("Insert reboot record successfully");
  5634. }
  5635. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  5636. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  5637. {
  5638. result = FAIL;
  5639. LOG_ERROR("delete reboot record error message: %s", errMsg);
  5640. }
  5641. else
  5642. {
  5643. //LOG_INFO("delete reboot record successfully");
  5644. }
  5645. sqlite3_close(db);
  5646. }
  5647. return result;
  5648. }
  5649. //===============================================
  5650. // Config process
  5651. //===============================================
  5652. void AddPlugInTimes(byte gunIndex)
  5653. {
  5654. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  5655. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  5656. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  5657. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  5658. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  5659. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  5660. }
  5661. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  5662. {
  5663. char cmdBuf[32];
  5664. struct timeb csuTime;
  5665. struct tm *tmCSU;
  5666. ftime(&csuTime);
  5667. tmCSU = localtime(&csuTime.time);
  5668. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  5669. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  5670. tmCSU->tm_sec);
  5671. if (isStart)
  5672. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  5673. else
  5674. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  5675. }
  5676. void zipLogFiles()
  5677. {
  5678. const char* logPath = "/Storage/SystemLog";
  5679. // 獲取目錄
  5680. DIR* pDir = opendir(logPath);
  5681. if (pDir != NULL)
  5682. {
  5683. struct timeb csuTime;
  5684. struct tm *tmCSU;
  5685. ftime(&csuTime);
  5686. tmCSU = localtime(&csuTime.time);
  5687. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  5688. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  5689. // tmCSU->tm_sec);
  5690. // Read items inside the folder
  5691. struct dirent* pEntry = NULL;
  5692. while ((pEntry = readdir(pDir)) != NULL)
  5693. {
  5694. if (strcmp(pEntry->d_name, ".") != 0 &&
  5695. strcmp(pEntry->d_name, "..") != 0 &&
  5696. strncmp(pEntry->d_name, "[", 1) == 0 &&
  5697. strstr(pEntry->d_name, "tar") < 0)
  5698. {
  5699. char yearC[5];
  5700. unsigned short year = 0;
  5701. char monthC[3];
  5702. unsigned short month = 0;
  5703. yearC[4] = '\0';
  5704. strncpy(yearC, pEntry->d_name + 1, 4);
  5705. monthC[2] = '\0';
  5706. strncpy(monthC, pEntry->d_name + 6, 2);
  5707. year = atoi(yearC);
  5708. month = atoi(monthC);
  5709. if (year != 0)
  5710. {
  5711. if (year < tmCSU->tm_year + 1900 ||
  5712. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  5713. {
  5714. LOG_INFO("tar file name : %s", pEntry->d_name);
  5715. char file[256];
  5716. memset(file, 0x00, sizeof(file));
  5717. strcat(file, "tar zcvf ");
  5718. strcat(file, logPath);
  5719. strncat(file, "/", 1);
  5720. strcat(file, pEntry->d_name);
  5721. strcat(file, ".tar");
  5722. strncat(file, " ", 1);
  5723. strcat(file, logPath);
  5724. strncat(file, "/", 1);
  5725. strcat(file, pEntry->d_name);
  5726. LOG_INFO("zip = %s \n", file);
  5727. system(file);
  5728. }
  5729. }
  5730. }
  5731. }
  5732. }
  5733. // Close folder
  5734. closedir(pDir);
  5735. }
  5736. void StopProcessingLoop()
  5737. {
  5738. for (;;)
  5739. {
  5740. CheckFactoryConfigFunction();
  5741. CheckFwUpdateFunction();
  5742. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5743. {
  5744. ChkPrimaryStatus();
  5745. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  5746. {
  5747. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  5748. KillAllTask();
  5749. sleep(3);
  5750. TryCloseWatchdog();
  5751. system("/usr/bin/run_evse_restart.sh");
  5752. return;
  5753. }
  5754. }
  5755. if(Is_Ocpp_ResetReq())
  5756. {
  5757. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  5758. {
  5759. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  5760. }
  5761. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  5762. {
  5763. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  5764. }
  5765. }
  5766. CheckSoftHardReset();
  5767. sleep(1);
  5768. }
  5769. }
  5770. void CreateWatchdog(void)
  5771. {
  5772. wtdFd = InitWatchDog();
  5773. if(wtdFd < 0)
  5774. {
  5775. LOG_INFO("Watchdog Initial Fail");
  5776. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  5777. }
  5778. else
  5779. {
  5780. LOG_INFO("Watchdog Initial Success");
  5781. }
  5782. }
  5783. void TryCloseWatchdog(void)
  5784. {
  5785. if(wtdFd > 0)
  5786. {
  5787. write(wtdFd, "V", 1);
  5788. close(wtdFd);
  5789. }
  5790. }
  5791. void TryFeedWatchdog(void)
  5792. {
  5793. if(wtdFd > 0)
  5794. {
  5795. write(wtdFd, "a", 1);
  5796. }
  5797. }
  5798. bool IsParallelCabinetIdle(void)
  5799. {
  5800. bool result = true;
  5801. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  5802. {
  5803. result = false;
  5804. }
  5805. return result;
  5806. }
  5807. bool IsConnectorWholeIdle(void)
  5808. {
  5809. bool result = true;
  5810. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  5811. {
  5812. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  5813. chargingInfo[count]->SystemStatus != S_RESERVATION &&
  5814. chargingInfo[count]->SystemStatus != S_FAULT &&
  5815. chargingInfo[count]->SystemStatus != S_MAINTAIN)
  5816. {
  5817. result = false;
  5818. break;
  5819. }
  5820. }
  5821. return result;
  5822. }
  5823. void ClearAlarmCodeWhenAcOff()
  5824. {
  5825. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  5826. {
  5827. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  5828. }
  5829. }
  5830. //==========================================
  5831. // Check task processing
  5832. //==========================================
  5833. void CheckTask()
  5834. {
  5835. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  5836. {
  5837. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  5838. {
  5839. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  5840. {
  5841. LOG_ERROR("Module_4g not running, restart it.");
  5842. system("/root/Module_4g &");
  5843. }
  5844. }
  5845. }
  5846. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  5847. {
  5848. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  5849. {
  5850. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  5851. {
  5852. LOG_ERROR("Module_Wifi not running, restart it.");
  5853. system("/root/Module_Wifi &");
  5854. }
  5855. }
  5856. }
  5857. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  5858. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5859. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5860. {
  5861. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  5862. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  5863. {
  5864. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  5865. Clean_Ocpp_procDogTime();
  5866. system("pkill OcppBackend");
  5867. }
  5868. if(system("pidof -s OcppBackend > /dev/null") != 0)
  5869. {
  5870. LOG_ERROR("OcppBackend not running, restart it.");
  5871. system("/root/OcppBackend &");
  5872. }
  5873. }
  5874. else
  5875. {
  5876. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  5877. }
  5878. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  5879. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  5880. {
  5881. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  5882. {
  5883. LOG_ERROR("Maintain Backend not running, restart it.");
  5884. system("/root/OcppBackendPH &");
  5885. }
  5886. }
  5887. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  5888. {
  5889. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  5890. system ("/root/Module_ProduceUtils &");
  5891. }
  5892. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  5893. {
  5894. LOG_ERROR("Module_EvComm not running, restart it.");
  5895. system ("/root/Module_EvComm &");
  5896. }
  5897. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  5898. {
  5899. LOG_ERROR("Module_InternalComm not running, restart it.");
  5900. system ("/root/Module_InternalComm &");
  5901. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  5902. }
  5903. }
  5904. void InitialDHCP()
  5905. {
  5906. char tmpbuf[256];
  5907. memset(tmpbuf,0,256);
  5908. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  5909. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  5910. system(tmpbuf);
  5911. }
  5912. void InitialDispenserDhcpServerConfig(void)
  5913. {
  5914. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  5915. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  5916. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  5917. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  5918. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  5919. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  5920. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  5921. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  5922. usleep(1000);
  5923. }
  5924. void StartDispenserDhcpServer(void)
  5925. {
  5926. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  5927. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  5928. }
  5929. //==========================================
  5930. // Check Smart Charging Profile
  5931. //==========================================
  5932. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  5933. {
  5934. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  5935. {
  5936. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  5937. {
  5938. Set_Ocpp_ChargingProfileReq(gunIndex);
  5939. }
  5940. }
  5941. }
  5942. int GetStartScheduleTime(unsigned char *time)
  5943. {
  5944. int result = -1;
  5945. struct tm tmScheduleStart;
  5946. struct timeb tbScheduleStart;
  5947. 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))
  5948. {
  5949. tmScheduleStart.tm_year -= 1900;
  5950. tmScheduleStart.tm_mon -= 1;
  5951. tbScheduleStart.time = mktime(&tmScheduleStart);
  5952. tbScheduleStart.millitm = 0;
  5953. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  5954. }
  5955. return result;
  5956. }
  5957. void Ocpp_ChargingProfile_Process(byte _index)
  5958. {
  5959. int _startCount = NO_DEFINE;
  5960. int _maxCount = 0;
  5961. float _limit = 0;
  5962. int _numberPhases = 0;
  5963. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  5964. {
  5965. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  5966. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  5967. if(_startCount < _maxCount)
  5968. {
  5969. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  5970. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  5971. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  5972. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  5973. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  5974. {
  5975. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  5976. }
  5977. else
  5978. {
  5979. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5980. }
  5981. }
  5982. else
  5983. {
  5984. chargingInfo[_index]->ChargingProfilePower = -1;
  5985. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5986. }
  5987. }
  5988. else
  5989. {
  5990. chargingInfo[_index]->ChargingProfilePower = -1;
  5991. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5992. }
  5993. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  5994. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  5995. {
  5996. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  5997. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  5998. _index + 1, _startCount, _limit);
  5999. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  6000. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  6001. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  6002. }
  6003. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  6004. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  6005. }
  6006. void Ocpp_MaxChargingProfile_Process(void)
  6007. {
  6008. int _startCount = NO_DEFINE;
  6009. int _maxCount = 0;
  6010. float _limit = 0;
  6011. int _numberPhases = 0;
  6012. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  6013. {
  6014. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  6015. _startCount = Get_Ocpp_MaxProfileStartIndex();
  6016. if(_startCount < _maxCount)
  6017. {
  6018. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  6019. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  6020. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  6021. }
  6022. else
  6023. {
  6024. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6025. }
  6026. }
  6027. else
  6028. {
  6029. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6030. }
  6031. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  6032. {
  6033. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  6034. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  6035. _startCount, _limit);
  6036. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  6037. }
  6038. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  6039. }
  6040. void CheckSmartChargeProfile(byte _index)
  6041. {
  6042. if(Is_Ocpp_ChargingProfileConf(_index))
  6043. {
  6044. Clean_Ocpp_ChargingProfileConf(_index);
  6045. Ocpp_MaxChargingProfile_Process();
  6046. Ocpp_ChargingProfile_Process(_index);
  6047. }
  6048. }
  6049. void TheEndCharging(byte gun_index)
  6050. {
  6051. chargingInfo[gun_index]->isRemoteStart = NO;
  6052. //StopGunInfoTimeoutDet(gun_index);
  6053. //StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  6054. ChangeStartOrStopDateTime(NO, gun_index);
  6055. DB_Insert_Record(localDb, gun_index);
  6056. }
  6057. void UpdateErrorCodeToOcpp(byte index)
  6058. {
  6059. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  6060. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  6061. {
  6062. SetOcppErrorCode(index, "InternalError");
  6063. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  6064. }
  6065. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  6066. {
  6067. SetOcppErrorCode(index, "OtherError");
  6068. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  6069. }
  6070. }
  6071. void CheckMiscCommandRequirement(void)
  6072. {
  6073. BOOL NeedAnnouncement = FALSE;
  6074. unsigned char connector = 0;
  6075. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  6076. {
  6077. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6078. {
  6079. NeedAnnouncement = FALSE;
  6080. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6081. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6082. {
  6083. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest ||
  6084. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest ||
  6085. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest ||
  6086. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest ||
  6087. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest ||
  6088. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.BackendStatusRequest ||
  6089. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.EthernetStatusRequest ||
  6090. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.WiFiStatusRequest ||
  6091. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.TelcomModemStatusRequest ||
  6092. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.BillingStatusRequest ||
  6093. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.StopButtonStatusRequest ||
  6094. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.AuthModeConfigRequest ||
  6095. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.EVCCIDConfigRequest ||
  6096. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.LEDIntensityRequest)
  6097. {
  6098. NeedAnnouncement = TRUE;
  6099. }
  6100. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  6101. {
  6102. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  6103. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.AvailabilityRequest ||
  6104. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest ||
  6105. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.AccountBalanceRequest)
  6106. {
  6107. NeedAnnouncement = TRUE;
  6108. }
  6109. }
  6110. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  6111. {
  6112. if(NeedAnnouncement)
  6113. {
  6114. LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  6115. }
  6116. else
  6117. {
  6118. LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  6119. }
  6120. }
  6121. }
  6122. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  6123. }
  6124. }
  6125. }
  6126. void CheckDispenserVersionUpdateRequirement(void)
  6127. {
  6128. }
  6129. bool IsReadyParalleling(void)
  6130. {
  6131. bool ready = true;
  6132. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  6133. {
  6134. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  6135. {
  6136. ready = false;
  6137. break;
  6138. }
  6139. }
  6140. return ready;
  6141. }
  6142. void SetParallelingStart(void)
  6143. {
  6144. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6145. {
  6146. if(chargingInfo[i]->SystemStatus == S_IDLE)
  6147. {
  6148. setChargerMode(i, MODE_MAINTAIN);
  6149. }
  6150. }
  6151. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  6152. }
  6153. void SetParallelingDone(void)
  6154. {
  6155. unsigned short totalPow = 0;
  6156. totalPow = GetTotalParallelCabinetPower();
  6157. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  6158. {
  6159. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  6160. }
  6161. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  6162. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  6163. }
  6164. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  6165. {
  6166. unsigned char relay = 0;
  6167. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  6168. {
  6169. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6170. {
  6171. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  6172. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  6173. {
  6174. relay = OnOff == YES ? YES : NO;
  6175. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  6176. {
  6177. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  6178. }
  6179. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  6180. }
  6181. }
  6182. }
  6183. }
  6184. void SetParallelCabinetAcContactor(unsigned char OnOff)
  6185. {
  6186. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6187. {
  6188. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  6189. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  6190. {
  6191. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  6192. }
  6193. }
  6194. }
  6195. unsigned short GetTotalParallelCabinetPower(void)
  6196. {
  6197. unsigned short power = 0, slavePow = 0;
  6198. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  6199. {
  6200. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  6201. {
  6202. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  6203. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  6204. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  6205. {
  6206. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  6207. power += slavePow;
  6208. }
  6209. }
  6210. }
  6211. return power;
  6212. }
  6213. unsigned char preParallelStatus = 0;
  6214. void MCabinetParallelingProcess(void)
  6215. {
  6216. bool PsuReInitial = false;
  6217. unsigned char status = 0;
  6218. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  6219. {
  6220. if(IsReadyParalleling())
  6221. {
  6222. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6223. {
  6224. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  6225. {
  6226. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  6227. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  6228. PsuReInitial = true;
  6229. }
  6230. }
  6231. if(PsuReInitial)
  6232. {
  6233. SetParallelingStart();
  6234. }
  6235. }
  6236. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  6237. switch(status)
  6238. {
  6239. case _Parallel_None:
  6240. if(preParallelStatus != status)
  6241. {
  6242. LOG_INFO("MCabinet Parallel Status [None]");
  6243. GetClockTime(&_Parallel_Time);
  6244. }
  6245. unsigned short totalPow = 0;
  6246. totalPow = GetTotalParallelCabinetPower();
  6247. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  6248. {
  6249. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  6250. }
  6251. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  6252. break;
  6253. case _Parallel_Wait:
  6254. if(preParallelStatus != status)
  6255. {
  6256. LOG_INFO("MCabinet Parallel Status [Wait]");
  6257. SetAcContactor(OFF);
  6258. SetParallelCabinetAcContactor(OFF);
  6259. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6260. {
  6261. SetPCabinetOutputRelay(i, OFF);
  6262. }
  6263. GetClockTime(&_Parallel_Time);
  6264. }
  6265. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  6266. {
  6267. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  6268. }
  6269. break;
  6270. case _Parallel_Working:
  6271. if(preParallelStatus != status)
  6272. {
  6273. LOG_INFO("MCabinet Parallel Status [Working]");
  6274. SetAcContactor(ON);
  6275. SetParallelCabinetAcContactor(ON);
  6276. GetClockTime(&_Parallel_Time);
  6277. }
  6278. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  6279. {
  6280. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  6281. }
  6282. break;
  6283. case _Parallel_Done:
  6284. if(preParallelStatus != status)
  6285. {
  6286. LOG_INFO("MCabinet Parallel Status [Done]");
  6287. GetClockTime(&_Parallel_Time);
  6288. }
  6289. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  6290. {
  6291. SetParallelingDone();
  6292. }
  6293. break;
  6294. }
  6295. preParallelStatus = status;
  6296. }
  6297. }
  6298. void SCabinetControllProcess(void)
  6299. {
  6300. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  6301. {
  6302. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  6303. {
  6304. SetAcContactor(ON);
  6305. }
  6306. else
  6307. {
  6308. SetAcContactor(OFF);
  6309. }
  6310. unsigned short _parallelRelay = 0;
  6311. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  6312. {
  6313. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  6314. }
  6315. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  6316. {
  6317. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  6318. }
  6319. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  6320. }
  6321. }
  6322. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  6323. {
  6324. unsigned char dispenser = 0;
  6325. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  6326. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  6327. {
  6328. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  6329. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  6330. }
  6331. }
  6332. bool ConnectorRequestToCharging(unsigned char connector)
  6333. {
  6334. unsigned char role = 0;
  6335. bool done = false;
  6336. role = ShmPsuGrouping->GroupCollection[connector].Role;
  6337. switch(role)
  6338. {
  6339. case _GROLE_IDLE:
  6340. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  6341. {
  6342. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  6343. }
  6344. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  6345. break;
  6346. case _GROLE_MASTER:
  6347. done = true;
  6348. break;
  6349. case _GROLE_SLAVE:
  6350. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  6351. {
  6352. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  6353. }
  6354. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  6355. break;
  6356. case _GROLE_REQUEST_TO_CHARGING:
  6357. done = true;
  6358. break;
  6359. default:
  6360. break;
  6361. }
  6362. return done;
  6363. }
  6364. bool IsConnectorGroupingCompleted(unsigned char connector)
  6365. {
  6366. unsigned char role = 0;
  6367. bool completed = false;
  6368. role = ShmPsuGrouping->GroupCollection[connector].Role;
  6369. switch(role)
  6370. {
  6371. case _GROLE_MASTER:
  6372. completed = true;
  6373. break;
  6374. default:
  6375. break;
  6376. }
  6377. return completed;
  6378. }
  6379. void SelfTestFailReason(void)
  6380. {
  6381. char reason[50];
  6382. memset(reason, 0x00, sizeof(reason));
  6383. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  6384. {
  6385. StatusCodeCompose(reason, "042251");
  6386. }
  6387. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  6388. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  6389. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  6390. {
  6391. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  6392. {
  6393. StatusCodeCompose(reason, "042280");
  6394. }
  6395. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  6396. {
  6397. StatusCodeCompose(reason, "042281");
  6398. }
  6399. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  6400. {
  6401. StatusCodeCompose(reason, "042282");
  6402. }
  6403. }
  6404. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  6405. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  6406. {
  6407. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  6408. {
  6409. StatusCodeCompose(reason, "042285");
  6410. }
  6411. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  6412. {
  6413. StatusCodeCompose(reason, "042286");
  6414. }
  6415. }
  6416. else
  6417. {
  6418. StatusCodeCompose(reason, "Self Test Fail");
  6419. }
  6420. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  6421. {
  6422. SetOcppErrorCode(gun_index, "InternalError");
  6423. SetOcppVendorErrorCode(gun_index, reason);
  6424. setChargerMode(gun_index, MODE_MAINTAIN);
  6425. }
  6426. }
  6427. bool IsAvailableBackToIdle(int gun_index)
  6428. {
  6429. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  6430. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  6431. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  6432. isResetStopChargeFlag(gun_index) == YES)
  6433. {
  6434. return true;
  6435. }
  6436. return false;
  6437. }
  6438. void CheckGunAvailable(void)
  6439. {
  6440. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  6441. {
  6442. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  6443. {
  6444. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  6445. {
  6446. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  6447. {
  6448. ShmChargerInfo->Control.GunAvailable[i] = YES;
  6449. }
  6450. }
  6451. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  6452. }
  6453. }
  6454. }
  6455. int main(void)
  6456. {
  6457. unsigned long time = 0;
  6458. int _dispenser = 0;
  6459. if(CreateShareMemory() == 0)
  6460. {
  6461. #ifdef SystemLogMessage
  6462. LOG_ERROR("CreatShareMemory NG");
  6463. #endif
  6464. if(ShmStatusCodeData!=NULL)
  6465. {
  6466. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  6467. }
  6468. sleep(5);
  6469. system("reboot -f");
  6470. sleep(5);
  6471. system("reboot -f");
  6472. }
  6473. LOG_INFO("\n");
  6474. LOG_INFO("===== Boot and Initialization start =====");
  6475. LOG_INFO("Main Fork PID = %d", getpid());
  6476. if (!InitialSystemDefaultConfig())
  6477. {
  6478. LOG_INFO("InitialSystemDefaultConfig NG");
  6479. StopProcessingLoop();
  6480. }
  6481. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  6482. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  6483. LOG_INFO("InitialShareMemoryInfo");
  6484. InitialShareMemoryInfo();
  6485. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  6486. if(!InitialChargerSetting())
  6487. {
  6488. isModelNameMatch = false;
  6489. }
  6490. Initialization();
  6491. LOG_INFO("Spawn all Task.");
  6492. SpawnTask();
  6493. if (!isModelNameMatch)
  6494. {
  6495. LOG_INFO("Module Name & HW info none match.");
  6496. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  6497. // Module Name 與硬體對應不正確
  6498. LOG_ERROR("Module Name & HW info none match.");
  6499. sleep(3);
  6500. KillAllTask();
  6501. StopProcessingLoop();
  6502. }
  6503. LOG_INFO("Module Name & HW info correct. Initialize.......");
  6504. CreateTimeoutFork();
  6505. SelfTestRun();
  6506. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  6507. {
  6508. sleep(1);
  6509. }
  6510. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  6511. {
  6512. sleep(1);
  6513. SelfTestFailReason();
  6514. }
  6515. else
  6516. {
  6517. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  6518. {
  6519. setChargerMode(gun_index, MODE_IDLE);
  6520. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  6521. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  6522. {
  6523. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  6524. }
  6525. }
  6526. }
  6527. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  6528. {
  6529. SetAcContactor(ON);
  6530. SpawnOcppService();
  6531. SpawnMaintainOcppService();
  6532. }
  6533. else
  6534. {
  6535. InitSlaveCabinetEthernet();
  6536. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  6537. {
  6538. setChargerMode(gun_index, MODE_MAINTAIN);
  6539. }
  6540. }
  6541. // Local DB
  6542. if(DB_Open(localDb) != PASS)
  6543. {
  6544. LOG_INFO("DB_Open fail.");
  6545. isDb_ready = false;
  6546. }
  6547. else
  6548. {
  6549. isDb_ready = true;
  6550. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  6551. {
  6552. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  6553. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  6554. }
  6555. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  6556. {
  6557. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  6558. LOG_INFO("Gun %d Consumption Initial: [%.1f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  6559. }
  6560. DB_Reboot_Record(localDb);
  6561. }
  6562. if (NetworkDB_Open(networkDb) != PASS)
  6563. {
  6564. LOG_ERROR("NetworkDB_Open fail.");
  6565. }
  6566. sleep(1);
  6567. CreateWatchdog();
  6568. // Main loop
  6569. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  6570. "****************************Main Loop**********************************" :
  6571. "********************** SelfTest Fail **********************");
  6572. GetClockTime(&_cmdMainPriority_time);
  6573. for (;;)
  6574. {
  6575. CheckGunAvailable();
  6576. CheckOcppStatus();
  6577. ChkPrimaryStatus();
  6578. ChkPsuStatus();
  6579. PsuFailureResume();
  6580. if (IsConnectorWholeIdle())
  6581. {
  6582. CheckFactoryConfigFunction();
  6583. CheckFwUpdateFunction();
  6584. }
  6585. if(CheckWiringInfoUpdate() != YES)
  6586. {
  6587. // return fork process
  6588. return 0;
  6589. }
  6590. CheckSoftHardReset();
  6591. StandbyCheck();
  6592. // 讀卡邏輯
  6593. //ScannerCardProcess();
  6594. AuthorizeTimeoutProcess();
  6595. // 當 AC 沒有搭上時,清除一些錯誤碼
  6596. ClearAlarmCodeWhenAcOff();
  6597. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  6598. {
  6599. CheckTask();
  6600. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6601. {
  6602. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  6603. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  6604. {
  6605. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  6606. {
  6607. Ocpp_ProfileReq_Cmd(_index);
  6608. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  6609. _ocppProfileChkFlag[_index] = 0;
  6610. }
  6611. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  6612. {
  6613. Ocpp_ProfileReq_Cmd(_index);
  6614. _ocppProfileChkFlag[_index] = 0;
  6615. }
  6616. else
  6617. {
  6618. _ocppProfileChkFlag[_index]++;
  6619. }
  6620. }
  6621. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  6622. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  6623. {
  6624. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  6625. }
  6626. }
  6627. GetClockTime(&_cmdMainPriority_time);
  6628. }
  6629. CheckMiscCommandRequirement();
  6630. CheckDispenserVersionUpdateRequirement();
  6631. MCabinetParallelingProcess();
  6632. SCabinetControllProcess();
  6633. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  6634. {
  6635. //CheckGpioInStatus();
  6636. CheckErrorOccurStatus(gun_index);
  6637. ChkOcppStatus(gun_index);
  6638. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  6639. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  6640. {
  6641. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  6642. {
  6643. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  6644. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  6645. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  6646. {
  6647. _ocppProfileChkFlag[gun_index] = 12;
  6648. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  6649. }
  6650. }
  6651. }
  6652. CheckSmartChargeProfile(gun_index);
  6653. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  6654. switch(chargingInfo[gun_index]->SystemStatus)
  6655. {
  6656. case S_MAINTAIN:
  6657. if(isModeChange(gun_index))
  6658. {
  6659. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  6660. {
  6661. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  6662. }
  6663. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  6664. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AvailabilityRequest = true;
  6665. GetClockTime(&_SystemStatus_Time[gun_index]);
  6666. }
  6667. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  6668. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  6669. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  6670. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  6671. {
  6672. break;
  6673. }
  6674. if(chargingInfo[gun_index]->IsAvailable == YES &&
  6675. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  6676. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  6677. {
  6678. if(time >= MAINTAIN_RECOVERY_TIME)
  6679. {
  6680. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AvailabilityRequest = true;
  6681. setChargerMode(gun_index, MODE_IDLE);
  6682. }
  6683. }
  6684. break;
  6685. case S_RESERVATION:
  6686. if(isModeChange(gun_index))
  6687. {
  6688. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  6689. char reserveIdTag[32];
  6690. Get_Ocpp_ReserveNowIdTag(gun_index, reserveIdTag);
  6691. LOG_INFO("****** Reservation IdTag: [%s] ******", reserveIdTag);
  6692. GetClockTime(&_SystemStatus_Time[gun_index]);
  6693. }
  6694. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  6695. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  6696. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  6697. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  6698. {
  6699. UpdateErrorCodeToOcpp(gun_index);
  6700. setChargerMode(gun_index, MODE_FAULT);
  6701. continue;
  6702. }
  6703. if(Is_Ocpp_ReserveNowExpired(gun_index))
  6704. {
  6705. chargingInfo[gun_index]->ReservationId = -1;
  6706. LOG_INFO("Gun %d reservation time's up...", gun_index + 1);
  6707. setChargerMode(gun_index, MODE_IDLE);
  6708. }
  6709. else
  6710. {
  6711. if(IsConnectorAuthorizeSuccess(gun_index))
  6712. {
  6713. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  6714. {
  6715. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  6716. }
  6717. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  6718. {
  6719. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  6720. }
  6721. setChargerMode(gun_index, MODE_AUTHORIZING);
  6722. }
  6723. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  6724. {
  6725. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  6726. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  6727. {
  6728. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  6729. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  6730. {
  6731. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  6732. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  6733. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  6734. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6735. }
  6736. }
  6737. }
  6738. }
  6739. break;
  6740. case S_IDLE:
  6741. case S_FAULT:
  6742. {
  6743. // clean PermissionRequest flag
  6744. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  6745. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  6746. if(isModeChange(gun_index))
  6747. {
  6748. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  6749. {
  6750. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  6751. chargingInfo[gun_index]->PresentChargedDuration = 0;
  6752. chargingInfo[gun_index]->RemainChargingDuration = 0;
  6753. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  6754. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  6755. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  6756. Clean_Ocpp_StopReason(gun_index);
  6757. ReleaseAlarmCode(gun_index);
  6758. // clean connector information
  6759. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  6760. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  6761. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  6762. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  6763. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  6764. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  6765. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -6553500;
  6766. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  6767. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  6768. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  6769. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  6770. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  6771. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  6772. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  6773. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  6774. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  6775. chargingInfo[gun_index]->EvBatterySoc = 0;
  6776. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  6777. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  6778. // clean RemoteStart & RemoteStop flag
  6779. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartRequest = false;
  6780. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  6781. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopRequest = false;
  6782. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  6783. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopRequest = false;
  6784. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  6785. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  6786. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  6787. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  6788. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  6789. Clean_Ocpp_RunningCost(gun_index);
  6790. Clean_Ocpp_FinalCost(gun_index);
  6791. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  6792. chargingInfo[gun_index]->PresentChargingPower = 0;
  6793. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  6794. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  6795. chargingInfo[gun_index]->ChargingProfilePower = -1;
  6796. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  6797. // clean force charging info
  6798. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  6799. if(chargingInfo[gun_index]->ReservationId != -1)
  6800. {
  6801. setChargerMode(gun_index, MODE_RESERVATION);
  6802. continue;
  6803. }
  6804. }
  6805. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  6806. {
  6807. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  6808. }
  6809. GetClockTime(&_SystemStatus_Time[gun_index]);
  6810. }
  6811. if (chargingInfo[gun_index]->IsAvailable == NO ||
  6812. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  6813. {
  6814. setChargerMode(gun_index, MODE_MAINTAIN);
  6815. continue;
  6816. }
  6817. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  6818. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  6819. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  6820. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  6821. {
  6822. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  6823. {
  6824. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  6825. {
  6826. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  6827. }
  6828. }
  6829. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  6830. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  6831. {
  6832. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  6833. {
  6834. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  6835. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  6836. }
  6837. }
  6838. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  6839. {
  6840. UpdateErrorCodeToOcpp(gun_index);
  6841. setChargerMode(gun_index, MODE_FAULT);
  6842. }
  6843. else
  6844. {
  6845. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  6846. {
  6847. CheckConnectorDisconnectionRecovery(gun_index);
  6848. }
  6849. }
  6850. }
  6851. else
  6852. {
  6853. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  6854. {
  6855. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  6856. if(time >= FAULT_RELEASE_TIME)
  6857. {
  6858. ReleaseAlarmCode(gun_index);
  6859. setChargerMode(gun_index, MODE_IDLE);
  6860. }
  6861. break;
  6862. }
  6863. // clean stop charge flag
  6864. chargingInfo[gun_index]->StopChargeFlag = false;
  6865. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  6866. { // Idle 正常程序起點
  6867. // 判斷是否有啟用檢查插槍
  6868. if(IsConnectorAuthorizeSuccess(gun_index))
  6869. {
  6870. if(chargingInfo[gun_index]->IsAvailable &&
  6871. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  6872. {
  6873. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  6874. {
  6875. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  6876. }
  6877. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  6878. {
  6879. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  6880. }
  6881. setChargerMode(gun_index, MODE_AUTHORIZING);
  6882. continue;
  6883. }
  6884. }
  6885. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  6886. {
  6887. if(chargingInfo[gun_index]->IsAvailable &&
  6888. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  6889. chargingInfo[gun_index]->ConnectorPlugIn &&
  6890. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  6891. {
  6892. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  6893. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  6894. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  6895. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  6896. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6897. continue;
  6898. }
  6899. }
  6900. // only for test & debug purpose
  6901. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  6902. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  6903. {
  6904. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  6905. LOG_INFO("Gun %d Start Force Charging", gun_index + 1);
  6906. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6907. continue;
  6908. }
  6909. else
  6910. {
  6911. }
  6912. } // Idle 正常程序終點
  6913. }
  6914. }
  6915. break;
  6916. case S_AUTHORIZING:
  6917. if(isModeChange(gun_index))
  6918. {
  6919. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  6920. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  6921. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  6922. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  6923. //StopSystemTimeoutDet();
  6924. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  6925. SetAcContactor(ON);
  6926. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  6927. }
  6928. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  6929. {
  6930. FouceAnnounceAccountBalance(gun_index);
  6931. }
  6932. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  6933. {
  6934. // sync with dispenser
  6935. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  6936. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  6937. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  6938. FouceAnnounceAccountBalance(gun_index);
  6939. }
  6940. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  6941. {
  6942. // sync with dispenser
  6943. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  6944. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  6945. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  6946. }
  6947. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  6948. {
  6949. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  6950. {
  6951. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  6952. }
  6953. else
  6954. {
  6955. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  6956. }
  6957. setChargerMode(gun_index, MODE_IDLE);
  6958. }
  6959. else
  6960. {
  6961. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  6962. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  6963. {
  6964. AddPlugInTimes(gun_index);
  6965. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  6966. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6967. GunAuthInitial(gun_index);
  6968. }
  6969. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  6970. {
  6971. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  6972. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  6973. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  6974. setChargerMode(gun_index, MODE_IDLE);
  6975. }
  6976. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  6977. {
  6978. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  6979. setChargerMode(gun_index, MODE_IDLE);
  6980. }
  6981. }
  6982. break;
  6983. case S_REASSIGN_CHECK:
  6984. {
  6985. if (isModeChange(gun_index))
  6986. {
  6987. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  6988. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  6989. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  6990. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  6991. //StopSystemTimeoutDet();
  6992. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  6993. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  6994. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  6995. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  6996. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  6997. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  6998. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  6999. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  7000. SetAcContactor(ON);
  7001. SetPCabinetOutputRelay(gun_index, ON);
  7002. GetClockTime(&_SystemStatus_Time[gun_index]);
  7003. }
  7004. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7005. if(ConnectorRequestToCharging(gun_index))
  7006. {
  7007. setChargerMode(gun_index, MODE_REASSIGN);
  7008. }
  7009. else if(time >= WAIT_REASSIGN_TIME)
  7010. {
  7011. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  7012. {
  7013. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  7014. }
  7015. else
  7016. {
  7017. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  7018. }
  7019. setChargerMode(gun_index, MODE_IDLE);
  7020. }
  7021. }
  7022. break;
  7023. case S_REASSIGN:
  7024. {
  7025. if (isModeChange(gun_index))
  7026. {
  7027. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  7028. //gettimeofday(&_toAverage_time, NULL);
  7029. GetClockTime(&_SystemStatus_Time[gun_index]);
  7030. }
  7031. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7032. if(IsConnectorGroupingCompleted(gun_index))
  7033. {
  7034. setChargerMode(gun_index, MODE_PRECHARGE);
  7035. }
  7036. else if(time >= MAX_REQUEST_CHARGING_TIME)
  7037. {
  7038. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  7039. setChargerMode(gun_index, MODE_IDLE);
  7040. }
  7041. }
  7042. break;
  7043. case S_PREPARNING:
  7044. {
  7045. if (isModeChange(gun_index))
  7046. {
  7047. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  7048. //StopGunInfoTimeoutDet(gun_index);
  7049. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  7050. SetAcContactor(ON);
  7051. GetClockTime(&_SystemStatus_Time[gun_index]);
  7052. }
  7053. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7054. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  7055. {
  7056. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  7057. continue;
  7058. }
  7059. if (isEvBoardStopChargeFlag(gun_index) == YES)
  7060. {
  7061. // 板端或後臺要求停止
  7062. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  7063. Set_Ocpp_StopReason(gun_index, "Local");
  7064. ChargingAlarmProcess(gun_index);
  7065. }
  7066. else if(isNormalStopChargeFlag(gun_index) == YES)
  7067. {
  7068. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  7069. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  7070. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  7071. ChargingTerminalProcess(gun_index);
  7072. }
  7073. else if(isResetStopChargeFlag(gun_index) == YES)
  7074. {
  7075. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  7076. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  7077. char *reason[16];
  7078. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  7079. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  7080. ChargingTerminalProcess(gun_index);
  7081. }
  7082. if(time > GUN_PREPARE_TIMEOUT)
  7083. {
  7084. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  7085. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  7086. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  7087. {
  7088. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  7089. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  7090. }
  7091. Set_Ocpp_StopReason(gun_index, "Local");
  7092. ChargingAlarmProcess(gun_index);
  7093. }
  7094. }
  7095. break;
  7096. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  7097. {
  7098. if (isModeChange(gun_index))
  7099. {
  7100. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  7101. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  7102. //StopGunInfoTimeoutDet(gun_index);
  7103. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  7104. GetClockTime(&_SystemStatus_Time[gun_index]);
  7105. }
  7106. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7107. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  7108. {
  7109. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  7110. }
  7111. // only for test & debug purpose
  7112. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  7113. {
  7114. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  7115. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  7116. }
  7117. if (isEvBoardStopChargeFlag(gun_index) == YES)
  7118. {
  7119. // 板端或後臺要求停止
  7120. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  7121. Set_Ocpp_StopReason(gun_index, "Local");
  7122. ChargingAlarmProcess(gun_index);
  7123. }
  7124. else if(isNormalStopChargeFlag(gun_index) == YES)
  7125. {
  7126. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  7127. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  7128. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  7129. ChargingTerminalProcess(gun_index);
  7130. }
  7131. else if(isResetStopChargeFlag(gun_index) == YES)
  7132. {
  7133. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  7134. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  7135. char *reason[16];
  7136. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  7137. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  7138. ChargingTerminalProcess(gun_index);
  7139. }
  7140. if(time >= GUN_EV_WAIT_TIMEOUT)
  7141. {
  7142. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  7143. ChargingTerminalProcess(gun_index);
  7144. }
  7145. }
  7146. break;
  7147. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  7148. {
  7149. if (isModeChange(gun_index))
  7150. {
  7151. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  7152. GetClockTime(&_SystemStatus_Time[gun_index]);
  7153. #ifdef STARTTRANSATION_IMMEDIATELY
  7154. OcppStartTransaction(gun_index);
  7155. #endif
  7156. }
  7157. CheckoutStartChargingSoc(gun_index, NO);
  7158. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7159. // only for test & debug purpose
  7160. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  7161. {
  7162. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  7163. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  7164. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  7165. {
  7166. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  7167. }
  7168. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  7169. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  7170. }
  7171. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  7172. {
  7173. setChargerMode(gun_index, MODE_CHARGING);
  7174. }
  7175. // only for test & debug purpose
  7176. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  7177. {
  7178. if(time >= FORCE_CHARGING_WAIT_EVSE)
  7179. {
  7180. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  7181. setChargerMode(gun_index, MODE_CHARGING);
  7182. }
  7183. }
  7184. if (isEvBoardStopChargeFlag(gun_index) == YES)
  7185. {
  7186. // 板端或後臺要求停止
  7187. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  7188. Set_Ocpp_StopReason(gun_index, "Local");
  7189. ChargingAlarmProcess(gun_index);
  7190. }
  7191. else if(isNormalStopChargeFlag(gun_index) == YES)
  7192. {
  7193. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  7194. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  7195. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  7196. ChargingTerminalProcess(gun_index);
  7197. }
  7198. else if(isResetStopChargeFlag(gun_index) == YES)
  7199. {
  7200. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  7201. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  7202. char *reason[16];
  7203. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  7204. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  7205. ChargingTerminalProcess(gun_index);
  7206. }
  7207. else if(isInvalidIdStopFlag(gun_index) == YES)
  7208. {
  7209. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  7210. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  7211. ChargingTerminalProcess(gun_index);
  7212. }
  7213. if(time >= GUN_EV_WAIT_TIMEOUT)
  7214. {
  7215. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  7216. ChargingTerminalProcess(gun_index);
  7217. }
  7218. }
  7219. break;
  7220. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  7221. {
  7222. if (isModeChange(gun_index))
  7223. {
  7224. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  7225. //StopGunInfoTimeoutDet(gun_index);
  7226. GetClockTime(&_SystemStatus_Time[gun_index]);
  7227. CheckoutStartChargingSoc(gun_index, YES);
  7228. chargingInfo[gun_index]->PresentChargedDuration = 0;
  7229. ChangeStartOrStopDateTime(YES, gun_index);
  7230. #ifndef STARTTRANSATION_IMMEDIATELY
  7231. OcppStartTransaction(gun_index);
  7232. #endif
  7233. }
  7234. // only for test & debug purpose
  7235. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  7236. {
  7237. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  7238. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  7239. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  7240. {
  7241. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  7242. }
  7243. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  7244. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  7245. }
  7246. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  7247. // 計算 Power
  7248. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  7249. chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  7250. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  7251. {
  7252. chargingInfo[gun_index]->PresentChargedDuration++;
  7253. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  7254. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  7255. {
  7256. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  7257. }
  7258. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  7259. chargingInfo[gun_index]->PowerConsumption += changingPow;
  7260. }
  7261. if (isEvBoardStopChargeFlag(gun_index))
  7262. {
  7263. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  7264. Set_Ocpp_StopReason(gun_index, "Local");
  7265. ChargingAlarmProcess(gun_index);
  7266. }
  7267. else if(isNormalStopChargeFlag(gun_index) == YES)
  7268. {
  7269. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  7270. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  7271. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  7272. ChargingTerminalProcess(gun_index);
  7273. }
  7274. else if(isResetStopChargeFlag(gun_index) == YES)
  7275. {
  7276. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  7277. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  7278. char *reason[16];
  7279. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  7280. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  7281. ChargingTerminalProcess(gun_index);
  7282. }
  7283. else if(isInvalidIdStopFlag(gun_index) == YES)
  7284. {
  7285. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  7286. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  7287. ChargingTerminalProcess(gun_index);
  7288. }
  7289. else if(CheckBackendChargingTimeout(gun_index) ||
  7290. CheckBackendChargingEnergy(gun_index))
  7291. {
  7292. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  7293. // 板端或後臺要求停止
  7294. ChargingTerminalProcess(gun_index);
  7295. }
  7296. }
  7297. break;
  7298. case S_ALARM:
  7299. case S_TERMINATING:
  7300. {
  7301. if (isModeChange(gun_index))
  7302. {
  7303. Set_Ocpp_StopReason(gun_index, "Other");
  7304. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  7305. {
  7306. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  7307. UpdateErrorCodeToOcpp(gun_index);
  7308. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  7309. {
  7310. OcppStopTransaction(gun_index);
  7311. }
  7312. TheEndCharging(gun_index);
  7313. }
  7314. else
  7315. {
  7316. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  7317. }
  7318. //StopGunInfoTimeoutDet(gun_index);
  7319. GetClockTime(&_SystemStatus_Time[gun_index]);
  7320. }
  7321. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7322. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  7323. {
  7324. if(time >= GUN_COMP_WAIT_TIMEOUT)
  7325. {
  7326. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  7327. {
  7328. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  7329. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  7330. }
  7331. }
  7332. if(IsAvailableBackToIdle(gun_index) == YES &&
  7333. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  7334. {
  7335. setChargerMode(gun_index, MODE_IDLE);
  7336. }
  7337. }
  7338. else
  7339. {
  7340. setChargerMode(gun_index, MODE_COMPLETE);
  7341. }
  7342. }
  7343. break;
  7344. case S_COMPLETE:
  7345. {
  7346. if (isModeChange(gun_index))
  7347. {
  7348. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  7349. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  7350. {
  7351. OcppStopTransaction(gun_index);
  7352. }
  7353. TheEndCharging(gun_index);
  7354. GetClockTime(&_SystemStatus_Time[gun_index]);
  7355. }
  7356. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7357. if(time >= GUN_COMP_WAIT_TIMEOUT)
  7358. {
  7359. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  7360. {
  7361. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  7362. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  7363. }
  7364. }
  7365. if(IsAvailableBackToIdle(gun_index) == YES &&
  7366. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  7367. {
  7368. setChargerMode(gun_index, MODE_IDLE);
  7369. }
  7370. }
  7371. break;
  7372. case S_CCS_PRECHARGE_ST0:
  7373. if (isModeChange(gun_index))
  7374. {
  7375. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  7376. //StopGunInfoTimeoutDet(gun_index);
  7377. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  7378. }
  7379. break;
  7380. case S_CCS_PRECHARGE_ST1:
  7381. if (isModeChange(gun_index))
  7382. {
  7383. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  7384. }
  7385. break;
  7386. }
  7387. }
  7388. TryFeedWatchdog();
  7389. usleep(whileLoopTime);
  7390. }
  7391. return FAIL;
  7392. }