main.c 388 KB

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