main.c 326 KB

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