main.c 391 KB

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