main.c 388 KB

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