main.c 474 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287
  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 10
  81. #define E4U_STANDBY_TIME 1800
  82. #define WIRING_INFO_DELAY 30
  83. #define WAIT_REASSIGN_TIME 40 // unit: second
  84. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  85. #define MIN_WAIT_PREPARNING_TIME 1 // unit: second
  86. #define FORCE_CHARGING_CMD_DELAY 3 // unit: second
  87. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  88. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  89. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  90. #define ENERAGY_INTERVAL 1 // unit: second
  91. #define FAULT_RELEASE_TIME 2 // unit: second
  92. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  93. #define PARALLEL_WAIT_TIME 3 // unit: second
  94. #define START_UPGRADE_TIME 60 // unit: second
  95. #define PCABINET_UPGRADE_TIME 300 // unit: second
  96. #define MCABINET_UPGRADE_TIME 60 // unit: second
  97. #define CONNECTION_TIME_OFFSET 5 // unit: second
  98. #define PLUG_IN_HOLD_TIME 1 // unit: second
  99. #define FINAL_COST_TIMEOUT 3 // unit: second
  100. #define FINAL_COST_UPDATE_TIME 10 // unit: second
  101. #define MAX_FINAL_COST_MISC_COUNT 3
  102. #define PARKING_STATUS_TIMEOUT 3 // unit: second
  103. #define OCCUPANCY_FEE_TIMEOUT 10 // unit: second
  104. #define MIN_AVAILABLE_MEMORY 200 // unit: MB
  105. #define AUTO_START_CHARGING "AutoStartCharging"
  106. #define CONTACTOR_AUTO_ON_OFF 0
  107. #define CONTACTOR_AUTO_ON_TIME 60
  108. #define CONTACTOR_AUTO_OFF_TIME 300
  109. #define WAIT_DISPENSER_STOP_TIMEOUT 3 // unit: second
  110. #define STARTTRANSATION_TIME 1
  111. #define STOP_TARTTRANSATION_DELAY 2
  112. #define WAIT_DIAGNOSTICS_CONFIRM 10 // unit: second
  113. #define WAIT_DIAGNOSTICS_TIMEOUT 120 // unit: second
  114. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  115. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  116. int whileLoopTime = 10000; // 10 ms
  117. int wtdFd = -1;
  118. byte _authorizeIndex = NO_DEFINE;
  119. BOOL _UpgradeNeedReboot = FALSE;
  120. bool _isBridgeModeConfig = false;
  121. #define GUN_DERATING_COUNT 2
  122. const int GunDeratingTemp[GUN_DERATING_COUNT] = {80, 85};
  123. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  124. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  125. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  126. {
  127. {0, 1, 0, 0},
  128. {1, 0, 0, 0},
  129. {0, 0, 0, 0},
  130. {0, 0, 0, 0}
  131. };
  132. //Location: 1 2 3 4
  133. // Gun: 1 3 4 2
  134. //Parallel: 1 2 3
  135. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  136. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  137. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  138. //{
  139. // {0, 3, 1, 2}, // group 0
  140. // {1, 0, 2, 3}, // group 1
  141. // {1, 3, 0, 2}, // group 2
  142. // {1, 3, 2, 0} // group 3
  143. //};
  144. //Location: 1 2 3 4
  145. // Gun: 1 4 3 2
  146. //Parallel: 1 2 3
  147. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  148. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  149. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  150. {
  151. {0, 3, 2, 1}, // group 0
  152. {1, 0, 3, 2}, // group 1
  153. {1, 3, 0, 2}, // group 2
  154. {1, 3, 2, 0} // group 3
  155. };
  156. const byte GroupParallelRelay_4_Gun_6Parallel[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  157. {
  158. {0, 4, 6, 1}, // group 0
  159. {4, 0, 3, 5}, // group 1
  160. {6, 3, 0, 2}, // group 2
  161. {1, 5, 2, 0} // group 3
  162. };
  163. long long DiffTimebWithNow(struct timeb ST);
  164. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  165. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  166. void ChargingTerminalProcess(byte gunIndex);
  167. void ChkPrimaryStatus();
  168. void ChargingAlarmProcess(byte gunIndex);
  169. void InformOcppErrOccur(byte codeType);
  170. void SetOcppErrorCode(unsigned char _index, char *errCode);
  171. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  172. void ReleaseAlarmCode(byte gunIndex);
  173. int DB_Open(sqlite3 *db);
  174. int DB_Insert_Record(sqlite3 *db, int gun_index);
  175. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  176. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  177. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value);
  178. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value);
  179. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index);
  180. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index);
  181. float Get_PowerConsumption(uint8_t gun_index);
  182. void Update_PowerConsumption(uint8_t gun_index);
  183. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description);
  184. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status);
  185. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString);
  186. int DB_Reboot_Record(sqlite3 *db);
  187. int DB_Insert_Ocmf(sqlite3 *db, int gun_index, char *txId, char *ocmfKey, char *ocmfInfo);
  188. int DB_Update_Ocmf_Status(sqlite3 *db, char *txId, int status);
  189. int DB_Get_Undisposed_Ocmf(sqlite3 *db, int *gunIndex, char *txId, char *ocmfKey, char *ocmfInfo);
  190. void InitialDHCP();
  191. void InitialDispenserDhcpServerConfig(void);
  192. void StartDispenserDhcpServer(void);
  193. int GetStartScheduleTime(unsigned char *time);
  194. void CheckConnectionTimeout(void);
  195. bool IsParallelCabinetIdle(void);
  196. bool IsStandbyEnable(void);
  197. bool IsGunIdle(int gunIndex, int idleType);
  198. bool IsDispenserIdle(int dispenserIndex);
  199. bool IsConnectorWholeIdle(void);
  200. void SetAcContactor(unsigned char OnOff);
  201. void UpdateErrorCodeToOcpp(byte index);
  202. bool CheckConnectorTypeStatus(void);
  203. void CreateWatchdog(void);
  204. void TryCloseWatchdog(void);
  205. void TryFeedWatchdog(void);
  206. bool IsReadyParalleling(void);
  207. void SetParallelingStart(void);
  208. void SetParallelingDone(void);
  209. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  210. void SetParallelCabinetAcContactor(unsigned char OnOff);
  211. unsigned short GetTotalParallelCabinetPower(void);
  212. void MCabinetParallelingProcess(void);
  213. void SCabinetControllProcess(void);
  214. bool Is_WebServer_Alive(void);
  215. void Run_WebServer_Service(void);
  216. bool Get_Ocpp_ConnectionStatus(void);
  217. void Run_Ocpp_Service(void);
  218. bool Is_Ocpp_Task_Alive(void);
  219. void Kill_Ocpp_Service(void);
  220. void Run_MaintainOcpp_Service(void);
  221. bool Is_MaintainOcpp_Task_Alive(void);
  222. void Kill_MaintainOcpp_Service(void);
  223. long int Get_Ocpp_procDogTime(void);
  224. void Clean_Ocpp_procDogTime(void);
  225. bool Is_Ocpp_BootNotification(void);
  226. void Clean_Ocpp_BootNotification(void);
  227. bool Is_Ocpp_ResetReq(void);
  228. bool Chk_Ocpp_Reset_Type(char *type);
  229. void Clean_Ocpp_ResetReq(void);
  230. void Set_Ocpp_AuthorizeReq(void);
  231. bool Is_Ocpp_AuthorizeConf(void);
  232. bool Is_Ocpp_Authorize_Status(char *status);
  233. bool Is_Ocpp_UpdateFirmwareReq(void);
  234. void Clean_Ocpp_UpdateFirmwareReq(void);
  235. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  236. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  237. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  238. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  239. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  240. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  241. int Get_Ocpp_ConnectionTimeOut(void);
  242. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  243. void Get_Ocpp_UserPrice(char * userId, char *userPrice);
  244. void Clean_Ocpp_UserPrice(void);
  245. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  246. void Clean_Ocpp_RunningCost(int gun_index);
  247. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  248. void Clean_Ocpp_FinalCost(int gun_index);
  249. int Get_Ocpp_FinalCostTxId(int gun_index);
  250. void Get_Ocpp_TimeOffset(char *timeOffset);
  251. void Get_Ocpp_PreAuthAmount(char *amount);
  252. void Get_Ocpp_PowerSharingServerIP(char *ip);
  253. void Get_Ocpp_Refunding(char *Refunding);
  254. void Clean_Ocpp_Refunding(void);
  255. bool Is_Ocpp_LcdOverideReq(int *gun, int *page);
  256. void Clean_Ocpp_LcdOverideReq(void);
  257. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name);
  258. bool Is_Ocpp_ChargerStationReq(void);
  259. void Clean_Ocpp_ChargerStationReq(void);
  260. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature);
  261. bool Is_Ocpp_WeatherReq(void);
  262. void Clean_Ocpp_WeatherReq(void);
  263. void Set_Ocpp_DeductTransaction(int transaction);
  264. void Set_Ocpp_DeductCreditNo(char *userId);
  265. void Set_Ocpp_DeductVemData(char *vemData);
  266. void Set_Ocpp_DeductAmount(double value);
  267. void Set_Ocpp_DeductResult(unsigned char result);
  268. void Set_Ocpp_DonateInvoice(unsigned char donate);
  269. bool Is_Ocpp_ReportCreditDeductReq(void);
  270. void Set_Ocpp_ReportCreditDeductReq(void);
  271. void Clean_Ocpp_TcciSerialNo(int gun_index);
  272. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus);
  273. void Clean_Ocpp_TcciCreditInfo(int gun_index);
  274. void Set_Ocpp_TcciReaderStatusReq(int gun_index);
  275. bool Is_Ocpp_TcciReaderStatusReq(int gun_index);
  276. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id);
  277. void Set_Ocpp_StartTransactionReq(int gun_index);
  278. bool Is_Ocpp_StartTransactionConf(int gun_index);
  279. void Clean_Ocpp_StartTransactionConf(int gun_index);
  280. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  281. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  282. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status);
  283. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  284. bool Is_Invalid_StartTransactionId(int gun_index);
  285. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  286. void Set_Ocpp_StopTransactionReq(int gun_index);
  287. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  288. bool Is_Ocpp_ReserveNowReq(int gun_index);
  289. void Clean_Ocpp_ReserveNowReq(int gun_index);
  290. int Get_Ocpp_ReserveNowId(int gun_index);
  291. void Set_Ocpp_ReserveNowConf(int gun_index);
  292. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  293. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  294. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  295. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  296. bool Is_Ocpp_CancelReservationReq(int gun_index);
  297. void Clean_Ocpp_CancelReservationReq(int gun_index);
  298. void Set_Ocpp_CancelReservationConf(int gun_index);
  299. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  300. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  301. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  302. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  303. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  304. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  305. bool Is_Ocpp_RemoteStartReq(int gun_index);
  306. void Clean_Ocpp_RemoteStartReq(int gun_index);
  307. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  308. bool Is_Ocpp_RemoteStopReq(int gun_index);
  309. void Clean_Ocpp_RemoteStopReq(int gun_index);
  310. void Set_Ocpp_StopReason(int gun_index, char *reason);
  311. void Get_Ocpp_StopReason(int gun_index, char *reason);
  312. void Clean_Ocpp_StopReason(int gun_index);
  313. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  314. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  315. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  316. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  317. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  318. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  319. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  320. void Set_Ocpp_ChargingProfileReq(int gun_index);
  321. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  322. bool Is_Ocpp_ChargingProfileId(int gun_index);
  323. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  324. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  325. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  326. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  327. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  328. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  329. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  330. bool Is_Ocpp_MaxChargingProfileId(void);
  331. int Get_Ocpp_MaxProfileStartTime(void);
  332. int Get_Ocpp_MaxProfileScheduleCount(void);
  333. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  334. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  335. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  336. int Get_Ocpp_MaxProfileStartIndex(void);
  337. int Is_Ocpp_RemoteStartWaitReq(void);
  338. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget);
  339. int Get_Ocpp_OffLineMaxChargingPower(void);
  340. bool Is_Ocpp_SendOcmfDataReq(int gunIndex);
  341. void Set_Ocpp_SendOcmfDataReq(int gunIndex);
  342. void Clean_Ocpp_SendOcmfDataReq(int gunIndex);
  343. void Set_Ocpp_OcmfData(int gunIndex, char *key, char *ocmfInfo);
  344. void Set_Deduct_UserId(int gunIndex, char *userId);
  345. void Set_Deduct_TransactionId(int gunIndex, int transaction);
  346. void Clean_DeductInfo(int gunIndex);
  347. bool Is_DeductResultReq(int gunIndex);
  348. bool Is_ReaderStatusReq(int gunIndex);
  349. void Clean_ReaderStatusInfo(int gunIndex);
  350. void Set_UserTransactionId(int gunIndex, int transaction);
  351. void Set_UserPrice(int gunIndex, float price);
  352. void Set_UserTotalCost(int gunIndex, float cost);
  353. void Set_UserAccountBalance(int gunIndex, float balance);
  354. void Set_UserDiscount(int gunIndex, float discount);
  355. void Clean_UserTransaction(int gunIndex);
  356. bool Is_OccupancyDeductReq(int gunIndex);
  357. void Clean_OccupancyDeductReq(int gunIndex);
  358. bool Is_OccupancyKeepCountingReq(int gunIndex);
  359. void Set_OccupancyKeepCountingReq(int gunIndex);
  360. void Clean_OccupancyKeepCountingReq(int gunIndex);
  361. bool Is_OccupancyFeeForceCancelReq(int gunIndex);
  362. void Set_OccupancyFeeForceCancelReq(int gunIndex);
  363. void Clean_OccupancyFeeForceCancelReq(int gunIndex);
  364. bool Is_Ocpp_GetDiagnosticsReq(void);
  365. void Clean_Ocpp_GetDiagnosticsReq(void);
  366. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  367. struct StatusCodeData *ShmStatusCodeData;
  368. struct PsuData *ShmPsuData;
  369. struct CHAdeMOData *ShmCHAdeMOData;
  370. struct GBTData *ShmGBTData;
  371. struct CcsData *ShmCcsData;
  372. struct PrimaryMcuData *ShmPrimaryMcuData;
  373. struct FanModuleData *ShmFanModuleData;
  374. struct RelayModuleData *ShmRelayModuleData[2];
  375. struct LedModuleData *ShmLedModuleData;
  376. struct OCPP16Data *ShmOCPP16Data;
  377. struct OCPP20Data *ShmOCPP20Data;
  378. struct OCPP16Data *ShmOCPP16DataPH;
  379. struct MeterInformation *ShmCsuMeterData;
  380. ChargerInfoData *ShmChargerInfo;
  381. PsuPositionInfoData *ShmPsuPosition;
  382. PsuGroupingInfoData *ShmPsuGrouping;
  383. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  384. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  385. struct timespec _cmdMainPriority_time;
  386. struct timespec _cmdSubPriority_time;
  387. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  388. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  389. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  390. struct timespec _DispenserUpgrade_time;
  391. struct timespec _Standby_time;
  392. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  393. struct timespec _ChargedEnergy_Time[CONNECTOR_QUANTITY];
  394. struct timespec _Parallel_Time;
  395. struct timespec _RemoteStartNoId_Time;
  396. float gunOutputVol[CONNECTOR_QUANTITY];
  397. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  398. float _lastProfilePower[CONNECTOR_QUANTITY];
  399. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  400. float _lastMaxProfilePower;
  401. float _lsatGunMeterValue[CONNECTOR_QUANTITY];
  402. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  403. bool _useGunMeterValue[MAX_GUN_QUANTITY];
  404. bool _triggerTransaction[MAX_GUN_QUANTITY];
  405. bool _plugInHold[MAX_GUN_QUANTITY];
  406. struct timespec _PlugInHold_Time[MAX_GUN_QUANTITY];
  407. struct timespec _DispenserBill_Time[MAX_DISPENSER_QUANTITY];
  408. struct timespec _ConnectorBill_Time[MAX_GUN_QUANTITY];
  409. struct timespec _ParkingStatus_Time[MAX_GUN_QUANTITY];
  410. struct timespec _GetOccupancyFee_Time[MAX_GUN_QUANTITY];
  411. struct timespec _Diagnostics_Time;
  412. bool _canUpdateFinalCost[MAX_DISPENSER_QUANTITY];
  413. bool _isUndisposedFinalCost[MAX_DISPENSER_QUANTITY];
  414. int _notifyOccupancyFeeAct[MAX_GUN_QUANTITY];
  415. bool _NeedReset4gWifi;
  416. struct timespec _4gWifiReset_time;
  417. int _psuLostQuantity = 0;
  418. bool _isStandbyEnable = true;
  419. bool isModelNameMatch = true;
  420. bool _NeedDiagnostics = false;
  421. //int rfidFd = -1;
  422. //char* rfidPortName = "/dev/ttyS2";
  423. #if ENABLE_PCBA_TEST == 0
  424. char* fwVersion = "V2.05.00.0000.00";
  425. char* subVersion = "04";
  426. #else
  427. char* fwVersion = "PCBA.00.04";
  428. char* subVersion = "00";
  429. #endif
  430. sqlite3 *localDb;
  431. bool isDb_ready;
  432. sqlite3 *networkDb;
  433. //================================================
  434. // initial can-bus
  435. //================================================
  436. int InitCanBus()
  437. {
  438. int s0,nbytes;
  439. struct timeval tv;
  440. struct ifreq ifr0;
  441. struct sockaddr_can addr0;
  442. system("/sbin/ip link set can0 down");
  443. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  444. system("/sbin/ip link set can0 up");
  445. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  446. tv.tv_sec = 0;
  447. tv.tv_usec = 10000;
  448. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  449. {
  450. #ifdef SystemLogMessage
  451. LOG_ERROR("Set SO_RCVTIMEO NG");
  452. #endif
  453. }
  454. nbytes=40960;
  455. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  456. {
  457. #ifdef SystemLogMessage
  458. LOG_ERROR("Set SO_RCVBUF NG");
  459. #endif
  460. }
  461. nbytes=40960;
  462. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  463. {
  464. #ifdef SystemLogMessage
  465. LOG_ERROR("Set SO_SNDBUF NG");
  466. #endif
  467. }
  468. strcpy(ifr0.ifr_name, "can0" );
  469. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  470. addr0.can_family = AF_CAN;
  471. addr0.can_ifindex = ifr0.ifr_ifindex;
  472. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  473. return s0;
  474. }
  475. //================================================
  476. // initial uart port
  477. //================================================
  478. char *_priPortName = "/dev/ttyS1";
  479. char *_485PortName = "/dev/ttyS5";
  480. int InitComPort(byte target)
  481. {
  482. int fd;
  483. struct termios tios;
  484. if(target == UPGRADE_PRI)
  485. fd = open(_priPortName, O_RDWR);
  486. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  487. fd = open(_485PortName, O_RDWR);
  488. if(fd<=0)
  489. {
  490. #ifdef SystemLogMessage
  491. LOG_ERROR("open 407 Communication port NG");
  492. #endif
  493. return -1;
  494. }
  495. ioctl (fd, TCGETS, &tios);
  496. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  497. tios.c_lflag = 0;
  498. tios.c_iflag = 0;
  499. tios.c_oflag = 0;
  500. tios.c_cc[VMIN]=0;
  501. tios.c_cc[VTIME]=(unsigned char)1;
  502. tios.c_lflag=0;
  503. tcflush(fd, TCIFLUSH);
  504. ioctl (fd, TCSETS, &tios);
  505. return fd;
  506. }
  507. //=================================
  508. // Common routine
  509. //=================================
  510. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  511. {
  512. strncpy(dest, src + start, cnt);
  513. dest[cnt] = 0;
  514. }
  515. int InitWatchDog()
  516. {
  517. int fd;
  518. int timeout = 180;
  519. system("/usr/bin/fuser -k /dev/watchdog");
  520. sleep(1);
  521. system("echo V > /dev/watchdog");
  522. sleep(1);
  523. fd=open("/dev/watchdog", O_RDWR);
  524. if(fd<=0)
  525. {
  526. LOG_ERROR("System watch dog initial fail.\r\n");
  527. }
  528. ioctl(fd, _IOWR('W', 6, int), &timeout);
  529. return fd;
  530. }
  531. void setChargerMode(byte gun_index, byte mode)
  532. {
  533. chargingInfo[gun_index]->SystemStatus = mode;
  534. }
  535. long long DiffTimebWithNow(struct timeb ST)
  536. {
  537. //return milli-second
  538. struct timeb ET;
  539. long long StartTime,StopTime;
  540. ftime(&ET);
  541. StartTime=(long long)ST.time;
  542. StopTime=(long long)ET.time;
  543. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  544. }
  545. //=================================
  546. // Create all share memory
  547. //=================================
  548. int CreateShareMemory()
  549. {
  550. int MeterSMId;
  551. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  552. {
  553. #ifdef SystemLogMessage
  554. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  555. #endif
  556. return 0;
  557. }
  558. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  559. {
  560. #ifdef SystemLogMessage
  561. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  562. #endif
  563. return 0;
  564. }
  565. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  566. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  567. {
  568. #ifdef SystemLogMessage
  569. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  570. #endif
  571. return 0;
  572. }
  573. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  574. {
  575. #ifdef SystemLogMessage
  576. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  577. #endif
  578. return 0;
  579. }
  580. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  581. //creat ShmPsuData
  582. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  583. {
  584. #ifdef SystemLogMessage
  585. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  586. #endif
  587. return 0;
  588. }
  589. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  590. {
  591. #ifdef SystemLogMessage
  592. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  593. #endif
  594. return 0;
  595. }
  596. memset(ShmPsuData, 0, sizeof(struct PsuData));
  597. /*
  598. if(CHAdeMO_QUANTITY > 0)
  599. {
  600. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  601. {
  602. #ifdef SystemLogMessage
  603. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  604. #endif
  605. return 0;
  606. }
  607. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  608. {
  609. #ifdef SystemLogMessage
  610. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  611. #endif
  612. return 0;
  613. }
  614. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  615. }
  616. if(GB_QUANTITY > 0)
  617. {
  618. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  619. {
  620. #ifdef SystemLogMessage
  621. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  622. #endif
  623. return 0;
  624. }
  625. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  626. {
  627. #ifdef SystemLogMessage
  628. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  629. #endif
  630. return 0;
  631. }
  632. memset(ShmGBTData, 0, sizeof(struct GBTData));
  633. }
  634. //creat ShmCcsData
  635. if(CCS_QUANTITY > 0)
  636. {
  637. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  638. {
  639. #ifdef SystemLogMessage
  640. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  641. #endif
  642. return 0;
  643. }
  644. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  645. {
  646. #ifdef SystemLogMessage
  647. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  648. #endif
  649. return 0;
  650. }
  651. memset(ShmCcsData, 0, sizeof(struct CcsData));
  652. }
  653. */
  654. //creat ShmPrimaryMcuData
  655. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  656. {
  657. #ifdef SystemLogMessage
  658. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  659. #endif
  660. return 0;
  661. }
  662. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  663. {
  664. #ifdef SystemLogMessage
  665. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  666. #endif
  667. return 0;
  668. }
  669. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  670. //creat ShmFanModuleData
  671. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  672. {
  673. #ifdef SystemLogMessage
  674. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  675. #endif
  676. return 0;
  677. }
  678. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  679. {
  680. #ifdef SystemLogMessage
  681. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  682. #endif
  683. return 0;
  684. }
  685. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  686. //creat ShmRelayModuleData
  687. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  688. {
  689. #ifdef SystemLogMessage
  690. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  691. #endif
  692. return 0;
  693. }
  694. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  695. {
  696. #ifdef SystemLogMessage
  697. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  698. #endif
  699. return 0;
  700. }
  701. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  702. // DO360 RC2
  703. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  704. {
  705. #ifdef SystemLogMessage
  706. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  707. #endif
  708. return 0;
  709. }
  710. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  711. {
  712. #ifdef SystemLogMessage
  713. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  714. #endif
  715. return 0;
  716. }
  717. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  718. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  719. {
  720. #ifdef SystemLogMessage
  721. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  722. #endif
  723. return 0;
  724. }
  725. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  726. {
  727. #ifdef SystemLogMessage
  728. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  729. #endif
  730. return 0;
  731. }
  732. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  733. //creat ShmOCPP16Data
  734. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  735. {
  736. #ifdef SystemLogMessage
  737. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  738. #endif
  739. return 0;
  740. }
  741. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  742. {
  743. #ifdef SystemLogMessage
  744. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  745. #endif
  746. return 0;
  747. }
  748. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  749. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  750. {
  751. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG");
  752. return 0;
  753. }
  754. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  755. {
  756. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG");
  757. return 0;
  758. }
  759. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  760. //creat ShmOCPP16DataPH
  761. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  762. {
  763. LOG_ERROR("shmget ShmOCPP16DataPH NG");
  764. return 0;
  765. }
  766. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  767. {
  768. LOG_ERROR("shmat ShmOCPP16DataPH NG");
  769. return 0;
  770. }
  771. memset(ShmOCPP16DataPH,0,sizeof(struct OCPP16Data));
  772. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  773. {
  774. #ifdef SystemLogMessage
  775. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  776. #endif
  777. return 0;
  778. }
  779. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  780. {
  781. #ifdef SystemLogMessage
  782. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  783. #endif
  784. return 0;
  785. }
  786. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  787. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  788. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  789. return 1;
  790. }
  791. //======================================================
  792. // Peripheral initial
  793. //======================================================
  794. void InitGPIO()
  795. {
  796. /*****************0~3, 4 bank, bank x 32+ num*********************/
  797. /***************************************************************/
  798. /*************** GPIO 0 ***************************************/
  799. /***************************************************************/
  800. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  801. system("echo 22 > /sys/class/gpio/export");
  802. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  803. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  804. system("echo 23 > /sys/class/gpio/export");
  805. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  806. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  807. system("echo 26 > /sys/class/gpio/export");
  808. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  809. system("echo 1 > /sys/class/gpio/gpio26/value");
  810. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  811. system("echo 27 > /sys/class/gpio/export");
  812. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  813. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  814. system("echo 29 > /sys/class/gpio/export");
  815. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  816. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  817. system("echo 19 > /sys/class/gpio/export");
  818. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  819. system("echo 1 > /sys/class/gpio/gpio19/value");
  820. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  821. system("echo 20 > /sys/class/gpio/export");
  822. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  823. /***************************************************************/
  824. /*************** GPIO 1 ***************************************/
  825. /***************************************************************/
  826. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  827. system("echo 44 > /sys/class/gpio/export");
  828. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  829. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  830. system("echo 45 > /sys/class/gpio/export");
  831. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  832. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  833. system("echo 46 > /sys/class/gpio/export");
  834. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  835. system("echo 0 > /sys/class/gpio/gpio46/value");
  836. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  837. system("echo 47 > /sys/class/gpio/export");
  838. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  839. /***************************************************************/
  840. /*************** GPIO 2 ***************************************/
  841. /***************************************************************/
  842. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  843. system("echo 89 > /sys/class/gpio/export");
  844. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  845. system("echo 1 > /sys/class/gpio/gpio89/value");
  846. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  847. system("echo 87 > /sys/class/gpio/export");
  848. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  849. system("echo 0 > /sys/class/gpio/gpio87/value");
  850. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  851. system("echo 88 > /sys/class/gpio/export");
  852. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  853. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  854. system("echo 86 > /sys/class/gpio/export");
  855. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  856. /***************************************************************/
  857. /*************** GPIO 3 ***************************************/
  858. /***************************************************************/
  859. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  860. system("echo 111 > /sys/class/gpio/export");
  861. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  862. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  863. system("echo 114 > /sys/class/gpio/export");
  864. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  865. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  866. system("echo 113 > /sys/class/gpio/export");
  867. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  868. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  869. system("echo 110 > /sys/class/gpio/export");
  870. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  871. system("echo 0 > /sys/class/gpio/gpio110/value");
  872. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  873. system("echo 115 > /sys/class/gpio/export");
  874. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  875. system("echo 0 > /sys/class/gpio/gpio115/value");
  876. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  877. system("echo 112 > /sys/class/gpio/export");
  878. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  879. system("echo 1 > /sys/class/gpio/gpio112/value");
  880. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  881. system("echo 116 > /sys/class/gpio/export");
  882. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  883. system("echo 0 > /sys/class/gpio/gpio116/value");
  884. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  885. system("echo 103 > /sys/class/gpio/export");
  886. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  887. system("echo 0 > /sys/class/gpio/gpio103/value");
  888. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  889. system("echo 104 > /sys/class/gpio/export");
  890. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  891. system("echo 0 > /sys/class/gpio/gpio104/value");
  892. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  893. }
  894. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  895. {
  896. int fd, wrd;
  897. unsigned char *buf;
  898. unsigned int ChkSum, ChkSumOrg;
  899. if((buf = malloc(MtdBlockSize)) == NULL)
  900. {
  901. LOG_ERROR("malloc buffer 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. memset(buf, 0, MtdBlockSize);
  912. //================================================
  913. // Load configuration from mtdblock10
  914. //================================================
  915. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  916. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  917. if(fd < 0)
  918. {
  919. free(buf);
  920. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  921. if(ShmStatusCodeData != NULL)
  922. {
  923. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  924. }
  925. sleep(5);
  926. system("reboot -f");
  927. sleep(5);
  928. system("reboot -f");
  929. }
  930. wrd = read(fd, buf, MtdBlockSize);
  931. close(fd);
  932. if(wrd < MtdBlockSize)
  933. {
  934. free(buf);
  935. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  936. if(ShmStatusCodeData != NULL)
  937. {
  938. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  939. }
  940. sleep(5);
  941. system("reboot -f");
  942. sleep(5);
  943. system("reboot -f");
  944. }
  945. ChkSum = 0;
  946. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  947. {
  948. ChkSum += buf[wrd];
  949. }
  950. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  951. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  952. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  953. //================================================
  954. // Load configuration from mtdblock11
  955. //================================================
  956. if(ChkSum != ChkSumOrg)
  957. {
  958. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  959. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  960. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  961. if(fd < 0)
  962. {
  963. free(buf);
  964. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  965. if(ShmStatusCodeData != NULL)
  966. {
  967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  968. }
  969. sleep(5);
  970. system("reboot -f");
  971. sleep(5);
  972. system("reboot -f");
  973. }
  974. memset(buf, 0, MtdBlockSize);
  975. wrd = read(fd, buf, MtdBlockSize);
  976. close(fd);
  977. if(wrd < MtdBlockSize)
  978. {
  979. free(buf);
  980. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  981. if(ShmStatusCodeData != NULL)
  982. {
  983. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  984. }
  985. sleep(5);
  986. system("reboot -f");
  987. sleep(5);
  988. system("reboot -f");
  989. }
  990. ChkSum = 0;
  991. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  992. {
  993. ChkSum += buf[wrd];
  994. }
  995. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  996. //================================================
  997. // Load configuration from mtdblock12 (Factory default)
  998. //================================================
  999. if(ChkSum != ChkSumOrg)
  1000. {
  1001. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  1002. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1003. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1004. if(fd < 0)
  1005. {
  1006. free(buf);
  1007. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  1008. if(ShmStatusCodeData != NULL)
  1009. {
  1010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1011. }
  1012. sleep(5);
  1013. system("reboot -f");
  1014. sleep(5);
  1015. system("reboot -f");
  1016. }
  1017. memset(buf, 0, MtdBlockSize);
  1018. wrd = read(fd, buf, MtdBlockSize);
  1019. close(fd);
  1020. if(wrd < MtdBlockSize)
  1021. {
  1022. free(buf);
  1023. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  1024. if(ShmStatusCodeData != NULL)
  1025. {
  1026. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1027. }
  1028. sleep(5);
  1029. system("reboot -f");
  1030. sleep(5);
  1031. system("reboot -f");
  1032. }
  1033. ChkSum = 0;
  1034. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  1035. {
  1036. ChkSum += buf[wrd];
  1037. }
  1038. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  1039. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1040. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1041. if(ChkSum != ChkSumOrg)
  1042. {
  1043. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  1044. free(buf);
  1045. system("cd /root;./FactoryConfig -m");
  1046. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1047. system("sync");
  1048. sleep(5);
  1049. system("reboot -f");
  1050. sleep(5);
  1051. system("reboot -f");
  1052. return FAIL;
  1053. }
  1054. }
  1055. }
  1056. //load OK
  1057. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  1058. free(buf);
  1059. system("rm -f /mnt/EvseConfig.bin");
  1060. // SysConfig in flash is empty (0xffffffff)
  1061. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1062. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1063. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1064. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1065. {
  1066. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1067. {
  1068. LOG_INFO("Model name over length.");
  1069. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1070. }
  1071. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1072. {
  1073. LOG_INFO("Model serial number over length.");
  1074. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1075. }
  1076. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1077. {
  1078. LOG_INFO("System id over length.");
  1079. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1080. }
  1081. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1082. {
  1083. LOG_INFO("Ethernet dhcp config is null.");
  1084. }
  1085. system("cd /root;./Module_FactoryConfig -m");
  1086. sleep(3);
  1087. system("/usr/bin/run_evse_restart.sh");
  1088. }
  1089. LOG_INFO("Load SysConfigData OK");
  1090. return PASS;
  1091. }
  1092. int isEthernetCablePlugIn(int ethernet)
  1093. {
  1094. int fd, PlugInStatus = 0;
  1095. char ch[2];
  1096. char buffer[64];
  1097. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1098. fd = open(buffer, O_RDONLY);
  1099. if(fd < 0)
  1100. {
  1101. return -1;
  1102. }
  1103. if(read(fd, ch, 1) < 0)
  1104. {
  1105. return -1;
  1106. }
  1107. PlugInStatus = atoi(ch);
  1108. close(fd);
  1109. return PlugInStatus;
  1110. }
  1111. int isRouteFail()
  1112. {
  1113. int result = YES;
  1114. FILE *fp;
  1115. char buf[512];
  1116. fp = popen("route -n", "r");
  1117. if(fp != NULL)
  1118. {
  1119. while(fgets(buf, sizeof(buf), fp) != NULL)
  1120. {
  1121. if(strstr(buf, "eth0") != NULL)
  1122. result = NO;
  1123. }
  1124. }
  1125. pclose(fp);
  1126. return result;
  1127. }
  1128. int isReachableInternet()
  1129. {
  1130. int result = FAIL;
  1131. FILE *fp;
  1132. char cmd[256];
  1133. char buf[512];
  1134. char tmp[512];
  1135. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1136. // {
  1137. // result = PASS;
  1138. // }
  1139. // else
  1140. {
  1141. strcpy(cmd, "ifconfig eth0");
  1142. fp = popen(cmd, "r");
  1143. if (fp != NULL)
  1144. {
  1145. while(fgets(buf, sizeof(buf), fp) != NULL)
  1146. {
  1147. if (strstr(buf, "inet addr:") > 0)
  1148. {
  1149. sscanf(buf, "%*s%s", tmp);
  1150. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1151. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1152. {
  1153. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1154. }
  1155. }
  1156. }
  1157. }
  1158. pclose(fp);
  1159. memset(buf, 0x00, sizeof(buf));
  1160. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1161. {
  1162. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1163. fp = popen(cmd, "r");
  1164. if(fp != NULL)
  1165. {
  1166. while(fgets(buf, sizeof(buf), fp) != NULL)
  1167. {
  1168. if(strstr(buf, "transmitted") > 0)
  1169. {
  1170. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1171. if(strstr(buf,"100%") != NULL)
  1172. {
  1173. }
  1174. else
  1175. {
  1176. result = PASS;
  1177. }
  1178. //DEBUG_INFO("%s",buf);
  1179. //DEBUG_INFO("%s\n",tmp);
  1180. }
  1181. }
  1182. }
  1183. pclose(fp);
  1184. }
  1185. }
  1186. return result;
  1187. }
  1188. int isUap0up(void)
  1189. {
  1190. int result = FAIL;
  1191. FILE *fp;
  1192. char cmd[256];
  1193. char buf[512];
  1194. strcpy(cmd, "ifconfig uap0");;
  1195. fp = popen(cmd, "r");
  1196. if(fp != NULL)
  1197. {
  1198. while(fgets(buf, sizeof(buf), fp) != NULL)
  1199. {
  1200. if(strstr(buf, "uap0") > 0)
  1201. {
  1202. result = PASS;
  1203. }
  1204. }
  1205. }
  1206. pclose(fp);
  1207. return result;
  1208. }
  1209. void InitialApModeDhcpServerConfig(void)
  1210. {
  1211. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1212. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1213. system("echo 'interface uap0' >> /etc/udhcpd.conf");
  1214. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1215. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1216. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1217. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1218. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1219. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1220. usleep(1000);
  1221. }
  1222. void InitialBridgeModeDhcpServerConfig(void)
  1223. {
  1224. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1225. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1226. system("echo 'interface br0' >> /etc/udhcpd.conf");
  1227. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1228. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1229. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1230. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1231. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1232. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1233. usleep(1000);
  1234. }
  1235. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1236. {
  1237. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1238. }
  1239. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1240. {
  1241. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1242. }
  1243. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1244. {
  1245. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1246. }
  1247. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1248. {
  1249. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1250. }
  1251. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1252. {
  1253. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1254. }
  1255. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1256. {
  1257. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1258. }
  1259. void AnnounceEthernetStatus(void)
  1260. {
  1261. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1262. {
  1263. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1264. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1265. {
  1266. Set_Dispenser_MiscCommand(index, MISC_DISP_ETHERNET_STATUS);
  1267. }
  1268. }
  1269. }
  1270. void AnnounceWiFiStatus(void)
  1271. {
  1272. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1273. {
  1274. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1275. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1276. {
  1277. Set_Dispenser_MiscCommand(index, MISC_DISP_WIFI_STATUS);
  1278. }
  1279. }
  1280. }
  1281. void AnnounceTelcomModemStatus(void)
  1282. {
  1283. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1284. {
  1285. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1286. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1287. {
  1288. Set_Dispenser_MiscCommand(index, MISC_DISP_TELCOM_MODEM_STATUS);
  1289. }
  1290. }
  1291. }
  1292. void AnnounceBackendStatus(void)
  1293. {
  1294. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1295. {
  1296. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1297. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1298. {
  1299. Set_Dispenser_MiscCommand(index, MISC_DISP_BACKEND_STATUS);
  1300. }
  1301. }
  1302. }
  1303. void AnnounceBillingStatus(void)
  1304. {
  1305. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1306. {
  1307. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1308. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1309. {
  1310. Set_Dispenser_MiscCommand(index, MISC_DISP_BILLING_STATUS);
  1311. }
  1312. }
  1313. }
  1314. void AnnounceStopChargingButtonStatus(void)
  1315. {
  1316. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1317. {
  1318. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1319. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1320. {
  1321. Set_Dispenser_MiscCommand(index, MISC_DISP_STOP_BUTTON_STATUS);
  1322. }
  1323. }
  1324. }
  1325. void AnnounceAuthModeConfig(void)
  1326. {
  1327. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1328. {
  1329. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1330. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1331. {
  1332. Set_Dispenser_MiscCommand(index, MISC_DISP_AUTH_MODE_CONFIG);
  1333. }
  1334. }
  1335. }
  1336. void AnnounceEVCCIDConfig(void)
  1337. {
  1338. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1339. {
  1340. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1341. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1342. {
  1343. Set_Dispenser_MiscCommand(index, MISC_DISP_EVCCID_CONFIG);
  1344. }
  1345. }
  1346. }
  1347. void AnnounceLEDIntensity(void)
  1348. {
  1349. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1350. {
  1351. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1352. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1353. {
  1354. Set_Dispenser_MiscCommand(index, MISC_DISP_LED_INTENSITY);
  1355. }
  1356. }
  1357. }
  1358. void AnnounceCurrency(void)
  1359. {
  1360. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1361. {
  1362. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1363. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1364. {
  1365. Set_Dispenser_MiscCommand(index, MISC_DISP_CURRENCY);
  1366. }
  1367. }
  1368. }
  1369. void AnnounceRfidEndian(void)
  1370. {
  1371. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1372. {
  1373. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1374. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1375. {
  1376. Set_Dispenser_MiscCommand(index, MISC_DISP_RFID_CARD_ENDIAN);
  1377. }
  1378. }
  1379. }
  1380. void AnnounceRfidReader(void)
  1381. {
  1382. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1383. {
  1384. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1385. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1386. {
  1387. Set_Dispenser_MiscCommand(index, MISC_DISP_RFID_ENABLE);
  1388. }
  1389. }
  1390. }
  1391. void AnnounceRemoteStartNoID(void)
  1392. {
  1393. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  1394. {
  1395. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1396. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1397. {
  1398. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  1399. {
  1400. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  1401. if(chargingInfo[gun]->SystemStatus == S_IDLE)
  1402. {
  1403. ShmChargerInfo->ConnectorPageReq[gun].bits.RemoteStartNoID = true;
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. void AnnounceLineStatusCode(int gun, int status)
  1411. {
  1412. int dispenser = 0;
  1413. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].ParentDispensetIndex;
  1414. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  1415. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  1416. {
  1417. ShmChargerInfo->LineApi.StatusCode[gun] = status;
  1418. Set_Connector_MiscCommand(gun, MISC_CONN_LINE_STATUS);
  1419. }
  1420. }
  1421. void AnnounceStationInfo(void)
  1422. {
  1423. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1424. {
  1425. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1426. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1427. {
  1428. Set_Dispenser_MiscCommand(index, MISC_DISP_STATION_INFO);
  1429. }
  1430. }
  1431. }
  1432. void AnnounceOccupancyPrice(void)
  1433. {
  1434. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1435. {
  1436. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1437. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1438. {
  1439. Set_Dispenser_MiscCommand(index, MISC_DISP_PARKING_PRICE);
  1440. }
  1441. }
  1442. }
  1443. void UpdateConfigAndIcon(void)
  1444. {
  1445. unsigned char status = _Connnection_Disable;
  1446. // update ethernet connection status
  1447. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1448. if(ShmChargerInfo->CabinetMiscValue.EthernetStatus != status)
  1449. {
  1450. ShmChargerInfo->CabinetMiscValue.EthernetStatus = status;
  1451. AnnounceEthernetStatus();
  1452. }
  1453. // update wifi connection status
  1454. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1455. {
  1456. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1457. }
  1458. else
  1459. {
  1460. status = _Connnection_Disable;
  1461. }
  1462. if(ShmChargerInfo->CabinetMiscValue.WiFiStatus != status)
  1463. {
  1464. ShmChargerInfo->CabinetMiscValue.WiFiStatus = status;
  1465. AnnounceWiFiStatus();
  1466. }
  1467. // update 4g connection status
  1468. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1469. {
  1470. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1471. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1472. }
  1473. else
  1474. {
  1475. status = _Connnection_Disable;
  1476. }
  1477. if(ShmChargerInfo->CabinetMiscValue.TelcomModemStatus != status)
  1478. {
  1479. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = status;
  1480. AnnounceTelcomModemStatus();
  1481. }
  1482. // update backend connection status
  1483. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1484. if(ShmChargerInfo->CabinetMiscValue.BackendStatus != status)
  1485. {
  1486. ShmChargerInfo->CabinetMiscValue.BackendStatus = status;
  1487. AnnounceBackendStatus();
  1488. }
  1489. // update billing status
  1490. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1491. if(ShmChargerInfo->CabinetMiscValue.BillingStatus != status)
  1492. {
  1493. ShmChargerInfo->CabinetMiscValue.BillingStatus = status;
  1494. AnnounceBillingStatus();
  1495. }
  1496. // update stop charging button
  1497. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1498. if(ShmChargerInfo->CabinetMiscValue.StopChargingButton != status)
  1499. {
  1500. ShmChargerInfo->CabinetMiscValue.StopChargingButton = status;
  1501. AnnounceStopChargingButtonStatus();
  1502. }
  1503. // update AuthMode config
  1504. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1505. if(ShmChargerInfo->CabinetMiscValue.AuthModeConfig != status)
  1506. {
  1507. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = status;
  1508. AnnounceAuthModeConfig();
  1509. AnnounceEVCCIDConfig();
  1510. }
  1511. // update EVCCID config
  1512. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1513. if(ShmChargerInfo->CabinetMiscValue.EVCCIDConfig != status)
  1514. {
  1515. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = status;
  1516. AnnounceEVCCIDConfig();
  1517. }
  1518. // update LED Intensity
  1519. status = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1520. if(ShmChargerInfo->CabinetMiscValue.LEDIntensity != status)
  1521. {
  1522. ShmChargerInfo->CabinetMiscValue.LEDIntensity = status;
  1523. AnnounceLEDIntensity();
  1524. }
  1525. // update currency
  1526. status = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  1527. if(ShmChargerInfo->CabinetMiscValue.Currency != status)
  1528. {
  1529. ShmChargerInfo->CabinetMiscValue.Currency = status;
  1530. AnnounceCurrency();
  1531. }
  1532. // update RfidCardNumEndian
  1533. status = ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian;
  1534. if(ShmChargerInfo->CabinetMiscValue.RfidEndian != status)
  1535. {
  1536. ShmChargerInfo->CabinetMiscValue.RfidEndian = status;
  1537. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Phihong ||
  1538. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  1539. {
  1540. AnnounceRfidEndian();
  1541. }
  1542. }
  1543. // update AuthMode config
  1544. status = ShmSysConfigAndInfo->SysConfig.isRFID;
  1545. if(ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable != status)
  1546. {
  1547. ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable = status;
  1548. ShmChargerInfo->CabinetMiscValue.RfidReaderEnable = status;
  1549. AnnounceRfidReader();
  1550. }
  1551. }
  1552. void Check4gWifiReset(void)
  1553. {
  1554. int timeDelay = 0;
  1555. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1556. {
  1557. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1558. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1559. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1560. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1561. {
  1562. if(!_NeedReset4gWifi)
  1563. {
  1564. GetClockTime(&_4gWifiReset_time);
  1565. LOG_INFO("Backend Enable But Internet Not Available!");
  1566. }
  1567. _NeedReset4gWifi = true;
  1568. }
  1569. else
  1570. {
  1571. _NeedReset4gWifi = false;
  1572. }
  1573. if(_NeedReset4gWifi)
  1574. {
  1575. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1576. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1577. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1578. {
  1579. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1580. usleep(100000);
  1581. // before reset 4g io, kill Module_4g & Module_Wifi task
  1582. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1583. {
  1584. system("killall Module_4g &");
  1585. }
  1586. usleep(100000);
  1587. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1588. {
  1589. system("killall Module_Wifi &");
  1590. }
  1591. LOG_INFO("Reset 4g_Wifi Module");
  1592. system("echo 1 > /sys/class/gpio/gpio104/value");
  1593. sleep(1);
  1594. system("echo 0 > /sys/class/gpio/gpio104/value");
  1595. _NeedReset4gWifi = false;
  1596. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1597. }
  1598. }
  1599. }
  1600. else
  1601. {
  1602. _NeedReset4gWifi = false;
  1603. }
  1604. }
  1605. void MasterCabinetDhcpClientConfig(void)
  1606. {
  1607. char tmpbuf[256];
  1608. //Run DHCP client if enabled
  1609. system("killall udhcpc");
  1610. system("rm -rf /etc/resolv.conf");
  1611. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1612. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1613. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1614. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1615. {
  1616. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1617. system(tmpbuf);
  1618. }
  1619. //Upgrade system id to /etc/hostname
  1620. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1621. system(tmpbuf);
  1622. }
  1623. void InitMasterCabinetEthernet(void)
  1624. {
  1625. char tmpbuf[256];
  1626. //Init Eth0 for internet
  1627. memset(tmpbuf,0,256);
  1628. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1629. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1630. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1631. system(tmpbuf);
  1632. memset(tmpbuf,0,256);
  1633. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1634. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1635. system(tmpbuf);
  1636. //system("ifconfig lo up");
  1637. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1638. //Init Eth1 for administrator tool
  1639. memset(tmpbuf,0,256);
  1640. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1641. system(tmpbuf);
  1642. }
  1643. void InitSlaveCabinetEthernet(void)
  1644. {
  1645. char tmpbuf[256];
  1646. system("/sbin/ifconfig eth0 down");
  1647. system("/sbin/ifconfig eth1 down");
  1648. system("killall udhcpc");
  1649. system("rm -rf /etc/resolv.conf");
  1650. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1651. //Init Eth0 for internet
  1652. memset(tmpbuf,0,256);
  1653. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1654. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1655. "255.255.255.0");
  1656. system(tmpbuf);
  1657. system("/sbin/route add default gw 192.168.100.1 eth0");
  1658. //Init Eth1 for internet
  1659. memset(tmpbuf,0,256);
  1660. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1661. system(tmpbuf);
  1662. }
  1663. void InitEthernet(void)
  1664. {
  1665. system("ifconfig eth0 down");
  1666. system("ifconfig eth1 down");
  1667. sleep(2);
  1668. char tmpbuf[256];
  1669. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1670. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1671. sleep(2);
  1672. InitMasterCabinetEthernet();
  1673. MasterCabinetDhcpClientConfig();
  1674. // DO360 DHCP Server
  1675. InitialDispenserDhcpServerConfig();
  1676. StartDispenserDhcpServer();
  1677. pid_t pid = fork();
  1678. uint8_t cnt_pingDNS_Fail;
  1679. if(pid == 0)
  1680. {
  1681. ShmChargerInfo->CabinetMiscValue.BackendStatus = _Connnection_Disconnected;
  1682. ShmChargerInfo->CabinetMiscValue.EthernetStatus = _Connnection_Disconnected;
  1683. ShmChargerInfo->CabinetMiscValue.WiFiStatus = _Connnection_Disable;
  1684. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = _Connnection_Disable;
  1685. LOG_INFO("Internet Fork PID = %d", getpid());
  1686. for(;;)
  1687. {
  1688. if(isEthernetCablePlugIn(0) == YES)
  1689. {
  1690. if(isRouteFail())
  1691. {
  1692. LOG_INFO("eth0 not in route, restart eht0.");
  1693. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1694. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1695. {
  1696. InitialDHCP();
  1697. }
  1698. else
  1699. {
  1700. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1701. memset(tmpbuf,0,256);
  1702. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1703. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1704. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1705. system(tmpbuf);
  1706. memset(tmpbuf,0,256);
  1707. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1708. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1709. system(tmpbuf);
  1710. }
  1711. }
  1712. }
  1713. if(isReachableInternet() == PASS)
  1714. {
  1715. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1716. {
  1717. LOG_INFO("Internet is Reachable");
  1718. }
  1719. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1720. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1721. cnt_pingDNS_Fail = 0;
  1722. }
  1723. else
  1724. {
  1725. if(++cnt_pingDNS_Fail > 3)
  1726. {
  1727. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1728. {
  1729. LOG_INFO("Internet is Disconnect Via Ethernet");
  1730. }
  1731. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1732. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1733. }
  1734. }
  1735. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1736. if(ethResult == YES)
  1737. {
  1738. system("/sbin/ifmetric eth0 0");
  1739. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1740. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1741. {
  1742. system("/sbin/ifmetric mlan0 1");
  1743. }
  1744. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1745. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1746. {
  1747. system("/sbin/ifmetric ppp0 2");
  1748. }
  1749. }
  1750. if (!ethResult &&
  1751. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1752. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1753. {
  1754. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1755. if (ethResult)
  1756. {
  1757. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1758. {
  1759. system("/sbin/ifmetric eth0 1");
  1760. system("/sbin/ifmetric mlan0 0");
  1761. }
  1762. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1763. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1764. {
  1765. system("/sbin/ifmetric ppp0 2");
  1766. }
  1767. }
  1768. }
  1769. if (!ethResult &&
  1770. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1771. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1772. {
  1773. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1774. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1775. if (ethResult)
  1776. {
  1777. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1778. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1779. {
  1780. system("/sbin/ifmetric mlan0 2");
  1781. }
  1782. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1783. {
  1784. system("/sbin/ifmetric eth0 1");
  1785. system("/sbin/ifmetric ppp0 0");
  1786. }
  1787. }
  1788. }
  1789. // Bridge ethernet to uap0 to get dynamic ip address
  1790. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1791. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP))
  1792. {
  1793. if((isUap0up() == PASS) && access("/sys/class/net/br0/address", F_OK) == -1)
  1794. {
  1795. system("/usr/sbin/brctl addbr br0");
  1796. system("/usr/sbin/brctl addif br0 eth0");
  1797. system("/usr/sbin/brctl addif br0 uap0");
  1798. system("/sbin/ifconfig br0 192.168.10.200 up");
  1799. InitialBridgeModeDhcpServerConfig();
  1800. //system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  1801. //system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  1802. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1803. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1804. LOG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.");
  1805. _isBridgeModeConfig = true;
  1806. }
  1807. }
  1808. else
  1809. {
  1810. if(access("/sys/class/net/br0/address", F_OK) != -1)
  1811. {
  1812. system("/sbin/ifconfig br0 down");
  1813. system("/usr/sbin/brctl delbr br0");
  1814. LOG_INFO("Disable uap0 & eth0 Bridge.");
  1815. }
  1816. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP && _isBridgeModeConfig)
  1817. {
  1818. InitialApModeDhcpServerConfig();
  1819. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1820. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1821. LOG_INFO("Restart uap0 udhcpd.");
  1822. _isBridgeModeConfig = false;
  1823. }
  1824. }
  1825. // Change uap0 to low priority
  1826. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1827. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP) &&
  1828. (isUap0up() == PASS))
  1829. {
  1830. system("/sbin/ifmetric uap0 1");
  1831. }
  1832. //==============================================
  1833. // Check Local Power Sharing Communication
  1834. //==============================================
  1835. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == _SYS_POWER_SHARING_MODE_SLAVE)
  1836. {
  1837. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  1838. {
  1839. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  1840. {
  1841. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1842. }
  1843. }
  1844. else
  1845. {
  1846. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  1847. {
  1848. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  1849. }
  1850. }
  1851. }
  1852. else
  1853. {
  1854. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1855. }
  1856. if(!Is_Ocpp_Task_Alive())
  1857. {
  1858. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1859. }
  1860. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1861. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_DISABLE ? true : false;
  1862. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1863. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1864. UpdateConfigAndIcon();
  1865. Check4gWifiReset();
  1866. sleep(5);
  1867. }
  1868. }
  1869. #ifdef SystemLogMessage
  1870. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1871. #endif
  1872. }
  1873. void GetMacAddress()
  1874. {
  1875. for (byte index = 0; index < 2; index++)
  1876. {
  1877. int fd;
  1878. struct ifreq ifr;
  1879. char tarEth[5];
  1880. char Mac[18];
  1881. sprintf(tarEth,"eth%d",index);
  1882. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1883. ifr.ifr_addr.sa_family = AF_INET;
  1884. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1885. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1886. close(fd);
  1887. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1888. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1889. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1890. if (index == 0)
  1891. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1892. else
  1893. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1894. }
  1895. }
  1896. void GetFirmwareVersion()
  1897. {
  1898. // Get CSU root file system version
  1899. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1900. sprintf(ShmChargerInfo->SysMisc.MainVersion, fwVersion);
  1901. sprintf(ShmChargerInfo->SysMisc.SubVersion, subVersion);
  1902. #if ENABLE_PCBA_TEST == 0
  1903. GetFullFirmwareVersion(fwVersion, (char *)ShmSysConfigAndInfo->SysConfig.ModelName, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1904. // Get IEC or UL
  1905. char _buf[3] = {0};
  1906. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1907. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1908. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1909. else if (strcmp(_buf, "WU") == EQUAL)
  1910. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1911. #endif
  1912. }
  1913. void InitialShareMemoryInfo()
  1914. {
  1915. FILE *fp;
  1916. char cmd[512];
  1917. char buf[512];
  1918. _UpgradeNeedReboot = FALSE;
  1919. _psuLostQuantity = 0;
  1920. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1921. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1922. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1923. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1924. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1925. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1926. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1927. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1928. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1929. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1930. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1931. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1932. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1933. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1934. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1935. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1936. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1937. sprintf(cmd, "/bin/uname -r");
  1938. fp = popen(cmd, "r");
  1939. if(fp == NULL)
  1940. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1941. else
  1942. {
  1943. while(fgets(buf, sizeof(buf), fp) != NULL)
  1944. {
  1945. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1946. }
  1947. }
  1948. // 雙槍 CCS + Chademo
  1949. GetFirmwareVersion();
  1950. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1951. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1952. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1953. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1954. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1955. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1956. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1957. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1958. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1959. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1960. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1961. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1962. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1963. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1964. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1965. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1966. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1967. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1968. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1969. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1970. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1971. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1972. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1973. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1974. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1975. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1976. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1977. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1978. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1979. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1980. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1981. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1982. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1983. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1984. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1985. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1986. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1987. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1988. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1989. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1990. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1991. ShmFanModuleData->SelfTest_Comp = NO;
  1992. ShmLedModuleData->SelfTest_Comp = NO;
  1993. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1994. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1995. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1996. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1997. ShmFanModuleData->TestFanSpeed = 0;
  1998. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1999. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  2000. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  2001. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  2002. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  2003. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  2004. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  2005. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  2006. {
  2007. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  2008. }
  2009. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2010. {
  2011. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  2012. _useGunMeterValue[i] = false;
  2013. _notifyOccupancyFeeAct[i] = _Notify_None;
  2014. }
  2015. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  2016. {
  2017. _canUpdateFinalCost[i] = false;
  2018. _isUndisposedFinalCost[i] = true;
  2019. }
  2020. }
  2021. int Initialization(void)
  2022. {
  2023. // int pinOut[2] = { 116, 115 };
  2024. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  2025. {
  2026. chargingInfo[count]->RemoteStartFlag = NO;
  2027. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  2028. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  2029. SetOcppErrorCode(count, "NoError");
  2030. }
  2031. LOG_INFO("Initialization OK");
  2032. return PASS;
  2033. }
  2034. void InitialPsuGroupingAndLocation(void)
  2035. {
  2036. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2037. {
  2038. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  2039. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  2040. }
  2041. else
  2042. {
  2043. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  2044. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  2045. }
  2046. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2047. {
  2048. ShmPsuGrouping->GroupCollection[i].Index = i;
  2049. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  2050. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2051. {
  2052. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2053. {
  2054. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  2055. }
  2056. else
  2057. {
  2058. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun_6Parallel[i][0], GENERAL_GUN_QUANTITY);
  2059. }
  2060. }
  2061. else
  2062. {
  2063. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  2064. }
  2065. ShmChargerInfo->Control.GunAvailable[i] = NO;
  2066. }
  2067. }
  2068. void InitialCustomerCode(void)
  2069. {
  2070. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2071. {
  2072. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  2073. }
  2074. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  2075. {
  2076. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  2077. }
  2078. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  2079. {
  2080. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  2081. }
  2082. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  2083. {
  2084. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  2085. }
  2086. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'O' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'L')
  2087. {
  2088. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Shell;
  2089. }
  2090. else
  2091. {
  2092. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  2093. }
  2094. }
  2095. void InitialCustomizedSetting(void)
  2096. {
  2097. ShmChargerInfo->Control.CustomizedInfo.StandbyTime = STANDBY_TIME;
  2098. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  2099. {
  2100. ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisableForTcc = true;
  2101. }
  2102. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_E4U)
  2103. {
  2104. ShmChargerInfo->Control.CustomizedInfo.Flag.bits.CustomizedStandbyTime = true;
  2105. ShmChargerInfo->Control.CustomizedInfo.StandbyTime = E4U_STANDBY_TIME;
  2106. }
  2107. }
  2108. void InitialMaxDispenserConnectorQuantity(void)
  2109. {
  2110. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  2111. {
  2112. ShmChargerInfo->Control.MaxDispenser = 2;
  2113. ShmChargerInfo->Control.MaxConnector = 2;
  2114. }
  2115. else
  2116. {
  2117. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  2118. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  2119. }
  2120. }
  2121. void InitialAuthMode(void)
  2122. {
  2123. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2124. {
  2125. }
  2126. }
  2127. void InitialQRCodeMode(void)
  2128. {
  2129. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi ||
  2130. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  2131. {
  2132. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  2133. }
  2134. else
  2135. {
  2136. // customized
  2137. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  2138. {
  2139. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  2140. }
  2141. else
  2142. {
  2143. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  2144. }
  2145. }
  2146. }
  2147. void InitialPowerCabinetSetting(void)
  2148. {
  2149. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2150. {
  2151. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  2152. }
  2153. ShmChargerInfo->CabinetMiscValue.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  2154. ShmChargerInfo->CabinetMiscValue.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  2155. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  2156. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  2157. }
  2158. void Initial6ParallelSetting(void)
  2159. {
  2160. if(ShmSysConfigAndInfo->SysConfig.ModelName[11] >= '3' && ShmSysConfigAndInfo->SysConfig.ModelName[11] <= '9')
  2161. {
  2162. ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay = 1;
  2163. }
  2164. }
  2165. void PowerModuleLibSelection(void)
  2166. {
  2167. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L')
  2168. {
  2169. ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib = true;
  2170. }
  2171. else
  2172. {
  2173. ShmChargerInfo->Control.LibCtrl.bits.InfyPwrLib = true;
  2174. }
  2175. }
  2176. bool InitialChargerSetting(void)
  2177. {
  2178. bool result = true;
  2179. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' &&
  2180. (ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O' || ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L'))
  2181. {
  2182. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  2183. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  2184. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2185. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  2186. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2187. }
  2188. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2189. {
  2190. if(!(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'P' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'K'))
  2191. {
  2192. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2193. }
  2194. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2195. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2196. }
  2197. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  2198. {
  2199. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2200. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2201. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2202. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2203. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  2204. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  2205. }
  2206. else
  2207. {
  2208. result = false;
  2209. }
  2210. InitialCustomerCode();
  2211. InitialCustomizedSetting();
  2212. InitialMaxDispenserConnectorQuantity();
  2213. InitialAuthMode();
  2214. InitialQRCodeMode();
  2215. Initial6ParallelSetting();
  2216. InitialPsuGroupingAndLocation();
  2217. InitialPowerCabinetSetting();
  2218. PowerModuleLibSelection();
  2219. if(!CheckConnectorTypeStatus())
  2220. {
  2221. result = false;
  2222. }
  2223. return result;
  2224. }
  2225. bool InitialSystemDefaultConfig()
  2226. {
  2227. bool result = true;
  2228. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2229. InitGPIO();
  2230. InitEthernet();
  2231. GetMacAddress();
  2232. return result;
  2233. }
  2234. void SelfTestRun(void)
  2235. {
  2236. LOG_INFO("Self test.");
  2237. system("/root/Module_ChargerSelfTest &");
  2238. }
  2239. void SpawnPowerModuleTask(void)
  2240. {
  2241. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2242. {
  2243. system("/root/Module_PsuComm_PH &");
  2244. }
  2245. else
  2246. {
  2247. system("/root/Module_PsuComm &");
  2248. }
  2249. }
  2250. void KillPowerModuleTask(void)
  2251. {
  2252. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2253. {
  2254. system("killall Module_PsuComm_PH");
  2255. }
  2256. else
  2257. {
  2258. system("killall Module_PsuComm");
  2259. }
  2260. }
  2261. void SpawnNormalService(void)
  2262. {
  2263. system("/root/Module_EventLogging &");
  2264. system("/root/Module_PrimaryComm &");
  2265. system("/root/Module_EvComm &");
  2266. //system("/root/Module_LcmControl &");
  2267. system("/root/Module_InternalComm &");
  2268. SpawnPowerModuleTask();
  2269. system("/root/Module_ProduceUtils &");
  2270. system("/root/Module_CabinetParallel &");
  2271. system("/root/Module_LedIndication &");
  2272. system("/root/Module_Authorize &");
  2273. system("/root/Module_Firewall &");
  2274. }
  2275. void SpawnOcppService(void)
  2276. {
  2277. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2278. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2279. {
  2280. Run_Ocpp_Service();
  2281. }
  2282. }
  2283. void SpawnMaintainOcppService(void)
  2284. {
  2285. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2286. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2287. {
  2288. Run_MaintainOcpp_Service();
  2289. }
  2290. }
  2291. void SpawnInternetService(void)
  2292. {
  2293. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2294. {
  2295. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2296. {
  2297. system("/root/Module_4g &");
  2298. }
  2299. }
  2300. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2301. {
  2302. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2303. {
  2304. system("/root/Module_Wifi &");
  2305. }
  2306. }
  2307. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2308. {
  2309. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2310. {
  2311. system("/root/Module_4g &");
  2312. }
  2313. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2314. {
  2315. system("/root/Module_Wifi &");
  2316. }
  2317. }
  2318. }
  2319. int SpawnTask(void)
  2320. {
  2321. sleep(2);
  2322. SpawnNormalService();
  2323. SpawnInternetService();
  2324. return PASS;
  2325. }
  2326. int runFlashShellCmd(const char *cmd)
  2327. {
  2328. int result = FAIL;
  2329. char buf[256];
  2330. FILE *fp;
  2331. fp = popen(cmd, "r");
  2332. if(fp != NULL)
  2333. {
  2334. while(fgets(buf, sizeof(buf), fp) != NULL)
  2335. {
  2336. LOG_INFO("%s\n", buf);
  2337. }
  2338. result = PASS;
  2339. }
  2340. pclose(fp);
  2341. return result;
  2342. }
  2343. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2344. {
  2345. int result = PASS;
  2346. int fd, wrd;
  2347. unsigned int i, Chk;
  2348. unsigned char *ptr, *BufTmp;
  2349. Chk = 0;
  2350. ptr = (unsigned char *)UsrData;
  2351. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2352. {
  2353. memset(BufTmp, 0, MtdBlockSize);
  2354. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2355. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2356. {
  2357. Chk += *(BufTmp+i);
  2358. }
  2359. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2360. // Output configuration to file.
  2361. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2362. if(fd < 0)
  2363. {
  2364. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2365. free(BufTmp);
  2366. return 0;
  2367. }
  2368. wrd = write(fd, BufTmp, MtdBlockSize);
  2369. close(fd);
  2370. if(wrd < MtdBlockSize)
  2371. {
  2372. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2373. free(BufTmp);
  2374. return 0;
  2375. }
  2376. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2377. LOG_INFO("Erase /dev/mtd10.");
  2378. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2379. LOG_INFO("Write /dev/mtd10.");
  2380. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2381. LOG_INFO("Erase /dev/mtd11.");
  2382. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2383. LOG_INFO("Write /dev/mtd11.");
  2384. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2385. system("rm -f /mnt/EvseTempConfig.bin");
  2386. LOG_INFO("EvseTempConfig write to flash OK");
  2387. }
  2388. else
  2389. {
  2390. LOG_ERROR("alloc BlockSize NG");
  2391. result = FAIL;
  2392. }
  2393. if(BufTmp != NULL)
  2394. {
  2395. free(BufTmp);
  2396. }
  2397. return result;
  2398. }
  2399. //===============================================
  2400. // Common Detect Chk - Stop Charging ?
  2401. //===============================================
  2402. bool isEvBoardStopChargeFlag(byte gunIndex)
  2403. {
  2404. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2405. }
  2406. bool isNormalStopChargeFlag(byte gunIndex)
  2407. {
  2408. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2409. }
  2410. bool isResetStopChargeFlag(byte gunIndex)
  2411. {
  2412. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2413. }
  2414. bool isInvalidIdStopFlag(byte gunIndex)
  2415. {
  2416. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2417. }
  2418. bool isSessionTargetStopFlag(byte gunIndex)
  2419. {
  2420. return chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop;
  2421. }
  2422. bool isBackendStopFlag(byte gunIndex)
  2423. {
  2424. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.BackendStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop);
  2425. }
  2426. void ReleaseAlarmCode(byte gunIndex)
  2427. {
  2428. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2429. {
  2430. SetOcppErrorCode(gunIndex, "NoError");
  2431. }
  2432. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2433. {
  2434. SetOcppVendorErrorCode(gunIndex, "");
  2435. }
  2436. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2437. {
  2438. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2439. }
  2440. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2441. {
  2442. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2443. }
  2444. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2445. {
  2446. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2447. }
  2448. if (strcmp((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "") != EQUAL)
  2449. {
  2450. strcpy((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "");
  2451. }
  2452. }
  2453. //===============================================
  2454. // EmergencyStop and Charging Stop
  2455. //===============================================
  2456. void ChargingTerminalProcess(byte gunIndex)
  2457. {
  2458. setChargerMode(gunIndex, MODE_TERMINATING);
  2459. }
  2460. void ChargingAlarmProcess(byte gunIndex)
  2461. {
  2462. UpdateErrorCodeToOcpp(gunIndex);
  2463. setChargerMode(gunIndex, MODE_ALARM);
  2464. }
  2465. void AcChargingTerminalProcess()
  2466. {
  2467. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2468. }
  2469. void StopChargingProcessByString(byte level)
  2470. {
  2471. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2472. {
  2473. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2474. }
  2475. }
  2476. void ReleaseChargingProcessByString(byte level)
  2477. {
  2478. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2479. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2480. }
  2481. // 一般錯誤停止充電處理函式
  2482. void BoardErrOccurByString(byte index, char *code)
  2483. {
  2484. byte level = 1;
  2485. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2486. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2487. {
  2488. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2489. {
  2490. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2491. }
  2492. ChargingTerminalProcess(index);
  2493. }
  2494. StopChargingProcessByString(level);
  2495. }
  2496. // 急停狀況的停止充電處理函式
  2497. void EmcOccureByString(char *code)
  2498. {
  2499. byte level = 2;
  2500. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2501. {
  2502. return;
  2503. }
  2504. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2505. // 其錯誤等級為 2
  2506. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2507. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2508. strncmp(code, "042328", 6) == EQUAL)
  2509. {
  2510. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2511. {
  2512. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2513. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2514. {
  2515. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2516. {
  2517. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2518. }
  2519. InformOcppErrOccur(4);
  2520. ChargingAlarmProcess(gun);
  2521. }
  2522. }
  2523. StopChargingProcessByString(level);
  2524. }
  2525. }
  2526. void ReleaseEmsOccureByString(byte index, char *code)
  2527. {
  2528. bool isTrigger = false;
  2529. byte level = 2;
  2530. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2531. {
  2532. isTrigger = true;
  2533. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2534. }
  2535. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2536. {
  2537. isTrigger = true;
  2538. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2539. }
  2540. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2541. {
  2542. isTrigger = true;
  2543. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2544. }
  2545. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2546. {
  2547. isTrigger = true;
  2548. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2549. }
  2550. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2551. {
  2552. isTrigger = true;
  2553. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2554. }
  2555. if (isTrigger)
  2556. {
  2557. ReleaseChargingProcessByString(level);
  2558. }
  2559. }
  2560. //===============================================
  2561. // 確認硬體 (按鈕) 狀態
  2562. //===============================================
  2563. void ChkPrimaryStatus()
  2564. {
  2565. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2566. {
  2567. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2568. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2569. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2570. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2571. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2572. return;
  2573. }
  2574. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2575. {
  2576. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2577. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2578. EmcOccureByString("042251");
  2579. }
  2580. else
  2581. {
  2582. ReleaseEmsOccureByString(0, "042251");
  2583. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2584. }
  2585. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2586. {
  2587. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2588. //EmcOccureByString("042238");
  2589. }
  2590. else
  2591. {
  2592. //ReleaseEmsOccureByString(0, "042238");
  2593. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2594. }
  2595. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2596. {
  2597. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2598. }
  2599. else
  2600. {
  2601. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2602. }
  2603. if(ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL &&
  2604. (ShmChargerInfo->Control.CustomizedInfo.TiltSensorStep == _TILT_SENSOR_NONE ||
  2605. ShmChargerInfo->Control.CustomizedInfo.TiltSensorStep == _TILT_SENSOR_FINISH))
  2606. {
  2607. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2608. EmcOccureByString("042252");
  2609. }
  2610. else
  2611. {
  2612. ReleaseEmsOccureByString(0, "042252");
  2613. }
  2614. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2615. {
  2616. EmcOccureByString("042328");
  2617. SetAcContactor(OFF);
  2618. }
  2619. if(ShmChargerInfo->Control.CustomizedInfo.TiltSensorFail)
  2620. {
  2621. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = YES;
  2622. }
  2623. else
  2624. {
  2625. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = NO;
  2626. }
  2627. }
  2628. struct timespec _AcContactReOpen_Time;
  2629. void ChkPsuStatus(void)
  2630. {
  2631. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2632. {
  2633. if(IsConnectorWholeIdle())
  2634. {
  2635. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2636. {
  2637. //if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2638. //{
  2639. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2640. //}
  2641. if(ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[psuGroup].StatusVal & PSU_STATUS_FLAG_FAULT)
  2642. {
  2643. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2644. }
  2645. }
  2646. }
  2647. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2648. {
  2649. EmcOccureByString("042267");
  2650. }
  2651. }
  2652. else
  2653. {
  2654. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2655. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2656. {
  2657. bool allFaultStatus = true;
  2658. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2659. {
  2660. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2661. {
  2662. allFaultStatus = false;
  2663. }
  2664. }
  2665. if(allFaultStatus)
  2666. {
  2667. LOG_INFO("PSU Failure, Need Resume");
  2668. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2669. }
  2670. }
  2671. }
  2672. }
  2673. int AcContactorReOpenCount = 0;
  2674. void PsuFailureResume(void)
  2675. {
  2676. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2677. {
  2678. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2679. {
  2680. LOG_INFO("Ac Contactor Off By Psu Failure");
  2681. // update _AcContactReOpen_Time
  2682. GetClockTime(&_AcContactReOpen_Time);
  2683. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2684. AcContactorReOpenCount++;
  2685. }
  2686. else
  2687. {
  2688. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2689. {
  2690. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2691. {
  2692. ReleaseEmsOccureByString(0, "042267");
  2693. // update _AcContactReOpen_Time
  2694. GetClockTime(&_AcContactReOpen_Time);
  2695. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2696. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2697. LOG_INFO("Psu Failure Resume");
  2698. }
  2699. }
  2700. }
  2701. }
  2702. else
  2703. {
  2704. if(AcContactorReOpenCount > 0)
  2705. {
  2706. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2707. {
  2708. AcContactorReOpenCount = 0;
  2709. }
  2710. }
  2711. }
  2712. }
  2713. void SetAcContactor(unsigned char OnOff)
  2714. {
  2715. OnOff = OnOff ? ON : OFF;
  2716. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2717. {
  2718. if(OnOff)
  2719. {
  2720. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2721. }
  2722. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2723. }
  2724. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2725. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2726. {
  2727. SetParallelCabinetAcContactor(OnOff);
  2728. }
  2729. }
  2730. int GetStandbyTime(void)
  2731. {
  2732. int time = STANDBY_TIME;
  2733. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2734. {
  2735. time = FAST_STANDBY_TIME;
  2736. }
  2737. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.CustomizedStandbyTime)
  2738. {
  2739. time = ShmChargerInfo->Control.CustomizedInfo.StandbyTime;
  2740. }
  2741. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyImmediately)
  2742. {
  2743. time = FAST_STANDBY_TIME;
  2744. }
  2745. return time;
  2746. }
  2747. void StandbyCheck(void)
  2748. {
  2749. bool enable = IsStandbyEnable();
  2750. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2751. {
  2752. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2753. {
  2754. int time = GetStandbyTime();
  2755. #if CONTACTOR_AUTO_ON_OFF == 1
  2756. time = CONTACTOR_AUTO_ON_TIME;
  2757. #endif
  2758. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2759. {
  2760. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2761. {
  2762. SetAcContactor(OFF);
  2763. }
  2764. }
  2765. #if CONTACTOR_AUTO_ON_OFF == 1
  2766. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2767. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2768. {
  2769. SetAcContactor(ON);
  2770. }
  2771. #endif
  2772. }
  2773. else
  2774. {
  2775. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2776. GetClockTime(&_Standby_time);
  2777. }
  2778. }
  2779. else
  2780. {
  2781. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2782. {
  2783. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2784. }
  2785. }
  2786. }
  2787. void CheckDisconnectionStatusRecovery(void)
  2788. {
  2789. bool recovery = true;
  2790. // check all connector
  2791. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2792. {
  2793. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2794. {
  2795. recovery = false;
  2796. }
  2797. }
  2798. if(recovery)
  2799. {
  2800. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2801. }
  2802. }
  2803. //===============================================
  2804. // 確認各小板偵測的錯誤狀況
  2805. //===============================================
  2806. void CheckErrorOccurStatus(byte index)
  2807. {
  2808. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2809. {
  2810. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2811. }
  2812. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2813. {
  2814. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2815. {
  2816. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2817. }
  2818. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2819. {
  2820. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2821. }
  2822. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2823. }
  2824. if(ShmChargerInfo->GunError[index].ErrFlag.ErrorValue != 0)
  2825. {
  2826. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2827. {
  2828. char *strGunError[] = {"042279", "041039", "041040", "042358", "042359"};
  2829. for(int i = 0; i < MAX_GUN_ERROR_QUANTITY; i++)
  2830. {
  2831. if((ShmChargerInfo->GunError[index].ErrFlag.ErrorValue & (1 << i)) > 0)
  2832. {
  2833. memcpy(ShmChargerInfo->GunError[index].GunAlarmCode, strGunError[i], 6);
  2834. break;
  2835. }
  2836. }
  2837. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmChargerInfo->GunError[index].GunAlarmCode, 6);
  2838. }
  2839. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2840. {
  2841. LOG_INFO("Connector %d GunError Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2842. }
  2843. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2844. }
  2845. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2846. {
  2847. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2848. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2849. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2850. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2851. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2852. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2853. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2854. chargingInfo[index]->SystemStatus == S_CHARGING)
  2855. {
  2856. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2857. {
  2858. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2859. }
  2860. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2861. {
  2862. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2863. }
  2864. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2865. }
  2866. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2867. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2868. {
  2869. setChargerMode(index, MODE_IDLE);
  2870. }
  2871. }
  2872. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2873. {
  2874. CheckDisconnectionStatusRecovery();
  2875. }
  2876. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2877. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2878. {
  2879. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2880. {
  2881. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2882. StopChargingProcessByString(2);
  2883. }
  2884. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2885. {
  2886. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2887. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2888. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2889. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2890. }
  2891. }
  2892. else
  2893. {
  2894. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2895. {
  2896. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2897. ReleaseChargingProcessByString(2);
  2898. }
  2899. }
  2900. // RB
  2901. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2902. {
  2903. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2904. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2905. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2906. {
  2907. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2908. {
  2909. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2910. StopChargingProcessByString(2);
  2911. }
  2912. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2913. {
  2914. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2915. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2916. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2917. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2918. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2919. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2920. }
  2921. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2922. {
  2923. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2924. }
  2925. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2926. }
  2927. else
  2928. {
  2929. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2930. {
  2931. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2932. ReleaseChargingProcessByString(2);
  2933. }
  2934. }
  2935. }
  2936. else
  2937. {
  2938. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2939. {
  2940. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2941. ReleaseChargingProcessByString(2);
  2942. }
  2943. }
  2944. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2945. {
  2946. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2947. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2948. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2949. {
  2950. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2951. {
  2952. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2953. StopChargingProcessByString(2);
  2954. }
  2955. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2956. {
  2957. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2958. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2959. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2960. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2961. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2962. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2963. }
  2964. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2965. {
  2966. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2967. }
  2968. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2969. }
  2970. else
  2971. {
  2972. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2973. {
  2974. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2975. ReleaseChargingProcessByString(2);
  2976. }
  2977. }
  2978. }
  2979. else
  2980. {
  2981. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  2982. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2983. {
  2984. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2985. {
  2986. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2987. StopChargingProcessByString(2);
  2988. }
  2989. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2990. {
  2991. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  2992. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  2993. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2994. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  2995. }
  2996. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2997. {
  2998. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2999. }
  3000. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  3001. }
  3002. else
  3003. {
  3004. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3005. {
  3006. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3007. ReleaseChargingProcessByString(2);
  3008. }
  3009. }
  3010. }
  3011. }
  3012. int DiffTimebWithNowSec(struct timeb ST)
  3013. {
  3014. //return milli-second
  3015. struct timeb ET;
  3016. unsigned int StartTime,StopTime;
  3017. ftime(&ET);
  3018. StartTime=(unsigned int)ST.time;
  3019. StopTime=(unsigned int)ET.time;
  3020. return (StopTime-StartTime);
  3021. }
  3022. int CheckTimeOut(char *start)
  3023. {
  3024. int result = YES;
  3025. struct ParsingResult
  3026. {
  3027. int result;
  3028. int scanedElement;
  3029. int year;
  3030. int month;
  3031. int mday;
  3032. int hour;
  3033. int min;
  3034. int sec;
  3035. int tz_hour;
  3036. int tz_min;
  3037. float minSec;
  3038. }parsingResult;
  3039. struct tm tmStart;
  3040. struct timeb tbStart;
  3041. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  3042. if(strstr((char*)start, ".") != NULL)
  3043. {
  3044. // Original data with mini second
  3045. if(strstr((char*)start, "Z") != NULL)
  3046. {
  3047. // Original data with Z
  3048. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  3049. &parsingResult.year,
  3050. &parsingResult.month,
  3051. &parsingResult.mday,
  3052. &parsingResult.hour,
  3053. &parsingResult.min,
  3054. &parsingResult.sec,
  3055. &parsingResult.minSec);
  3056. }
  3057. else
  3058. {
  3059. // Original data without Z
  3060. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  3061. &parsingResult.year,
  3062. &parsingResult.month,
  3063. &parsingResult.mday,
  3064. &parsingResult.hour,
  3065. &parsingResult.min,
  3066. &parsingResult.sec,
  3067. &parsingResult.minSec,
  3068. &parsingResult.tz_hour,
  3069. &parsingResult.tz_min);
  3070. }
  3071. }
  3072. else
  3073. {
  3074. // Original data without mini second
  3075. if(strstr((char*)start, "Z") != NULL)
  3076. {
  3077. // Original data with Z
  3078. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  3079. &parsingResult.year,
  3080. &parsingResult.month,
  3081. &parsingResult.mday,
  3082. &parsingResult.hour,
  3083. &parsingResult.min,
  3084. &parsingResult.sec);
  3085. }
  3086. else
  3087. {
  3088. // Original data without Z
  3089. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  3090. &parsingResult.year,
  3091. &parsingResult.month,
  3092. &parsingResult.mday,
  3093. &parsingResult.hour,
  3094. &parsingResult.min,
  3095. &parsingResult.sec,
  3096. &parsingResult.tz_hour,
  3097. &parsingResult.tz_min);
  3098. }
  3099. }
  3100. if(parsingResult.scanedElement >= 6)
  3101. {
  3102. tmStart.tm_year = parsingResult.year - 1900;
  3103. tmStart.tm_mon = parsingResult.month - 1;
  3104. tmStart.tm_mday = parsingResult.mday;
  3105. tmStart.tm_hour = parsingResult.hour;
  3106. tmStart.tm_min = parsingResult.min;
  3107. tmStart.tm_sec = parsingResult.sec;
  3108. tmStart.tm_gmtoff = 0;
  3109. tbStart.time = mktime(&tmStart);
  3110. tbStart.timezone = 0;
  3111. tbStart.millitm = 0;
  3112. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  3113. if(DiffTimebWithNowSec(tbStart) <= 0)
  3114. result = NO;
  3115. else
  3116. result = YES;
  3117. }
  3118. else
  3119. {
  3120. LOG_INFO("Start date parsing error");
  3121. }
  3122. return result;
  3123. }
  3124. bool Is_WebServer_Alive(void)
  3125. {
  3126. if(system("pidof -s lighttpd > /dev/null") != 0)
  3127. {
  3128. return false;
  3129. }
  3130. return true;
  3131. }
  3132. void Run_WebServer_Service(void)
  3133. {
  3134. system("/root/web.sh");
  3135. }
  3136. bool Is_LocalPowerSharing_Alive(void)
  3137. {
  3138. if(system("pidof -s Module_PowerSharing > /dev/null") != 0)
  3139. {
  3140. return false;
  3141. }
  3142. return true;
  3143. }
  3144. void Run_LocalPowerSharing_Service(void)
  3145. {
  3146. system ("/root/Module_PowerSharing &");
  3147. }
  3148. //===============================================
  3149. // Ocpp_Service
  3150. //===============================================
  3151. void Run_Ocpp_Service(void)
  3152. {
  3153. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3154. {
  3155. system("/root/OcppBackend &");
  3156. return;
  3157. }
  3158. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3159. {
  3160. system("/root/OcppBackend20 &");
  3161. return;
  3162. }
  3163. }
  3164. bool Is_Ocpp_Task_Alive(void)
  3165. {
  3166. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3167. {
  3168. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3169. {
  3170. return false;
  3171. }
  3172. return true;
  3173. }
  3174. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3175. {
  3176. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3177. {
  3178. return false;
  3179. }
  3180. return true;
  3181. }
  3182. return false;
  3183. }
  3184. void Kill_Ocpp_Service(void)
  3185. {
  3186. system("killall OcppBackend");
  3187. system("killall OcppBackend20");
  3188. }
  3189. //===============================================
  3190. // Maintain Ocpp_Service
  3191. //===============================================
  3192. void Run_MaintainOcpp_Service(void)
  3193. {
  3194. system("/root/OcppBackendPH &");
  3195. }
  3196. bool Is_MaintainOcpp_Task_Alive(void)
  3197. {
  3198. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3199. {
  3200. return false;
  3201. }
  3202. return true;
  3203. }
  3204. void Kill_MaintainOcpp_Service(void)
  3205. {
  3206. system("killall OcppBackendPH");
  3207. }
  3208. //===============================================
  3209. // Ocpp OcppConnStatus
  3210. //===============================================
  3211. bool Get_Ocpp_ConnectionStatus(void)
  3212. {
  3213. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3214. {
  3215. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  3216. }
  3217. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3218. {
  3219. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  3220. }
  3221. return false;
  3222. }
  3223. //===============================================
  3224. // Ocpp procDogTime
  3225. //===============================================
  3226. long int Get_Ocpp_procDogTime(void)
  3227. {
  3228. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3229. {
  3230. return ShmOCPP16Data->procDogTime;
  3231. }
  3232. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3233. {
  3234. return ShmOCPP20Data->procDogTime;
  3235. }
  3236. return 0;
  3237. }
  3238. void Clean_Ocpp_procDogTime(void)
  3239. {
  3240. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3241. {
  3242. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3243. return;
  3244. }
  3245. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3246. {
  3247. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3248. return;
  3249. }
  3250. }
  3251. //===============================================
  3252. // Ocpp Boot Notification
  3253. //===============================================
  3254. bool Is_Ocpp_BootNotification(void)
  3255. {
  3256. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3257. {
  3258. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3259. }
  3260. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3261. {
  3262. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3263. }
  3264. return false;
  3265. }
  3266. void Clean_Ocpp_BootNotification(void)
  3267. {
  3268. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3269. {
  3270. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  3271. return;
  3272. }
  3273. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3274. {
  3275. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  3276. return;
  3277. }
  3278. }
  3279. //===============================================
  3280. // Ocpp Rest
  3281. //===============================================
  3282. bool Is_Ocpp_ResetReq(void)
  3283. {
  3284. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3285. {
  3286. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  3287. }
  3288. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3289. {
  3290. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  3291. }
  3292. return false;
  3293. }
  3294. bool Chk_Ocpp_Reset_Type(char *type)
  3295. {
  3296. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3297. {
  3298. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  3299. }
  3300. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3301. {
  3302. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  3303. }
  3304. return false;
  3305. }
  3306. void Clean_Ocpp_ResetReq(void)
  3307. {
  3308. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3309. {
  3310. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3311. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  3312. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  3313. return;
  3314. }
  3315. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3316. {
  3317. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  3318. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  3319. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  3320. return;
  3321. }
  3322. }
  3323. //===============================================
  3324. // Ocpp AuthorizeReq
  3325. //===============================================
  3326. void Set_Ocpp_AuthorizeReq(void)
  3327. {
  3328. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3329. {
  3330. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3331. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3332. return;
  3333. }
  3334. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3335. {
  3336. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  3337. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  3338. return;
  3339. }
  3340. }
  3341. bool Is_Ocpp_AuthorizeConf(void)
  3342. {
  3343. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3344. {
  3345. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3346. }
  3347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3348. {
  3349. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3350. }
  3351. return false;
  3352. }
  3353. bool Is_Ocpp_Authorize_Status(char *status)
  3354. {
  3355. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3356. {
  3357. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3358. }
  3359. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3360. {
  3361. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3362. }
  3363. return false;
  3364. }
  3365. //===============================================
  3366. // Ocpp UpdateFirmwareReq
  3367. //===============================================
  3368. bool Is_Ocpp_UpdateFirmwareReq(void)
  3369. {
  3370. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3371. {
  3372. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3373. }
  3374. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3375. {
  3376. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3377. }
  3378. return false;
  3379. }
  3380. void Clean_Ocpp_UpdateFirmwareReq(void)
  3381. {
  3382. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3383. {
  3384. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3385. return;
  3386. }
  3387. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3388. {
  3389. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3390. return;
  3391. }
  3392. }
  3393. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3394. {
  3395. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3396. {
  3397. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3398. }
  3399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3400. {
  3401. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3402. }
  3403. return false;
  3404. }
  3405. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3406. {
  3407. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3408. {
  3409. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3410. return;
  3411. }
  3412. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3413. {
  3414. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3415. return;
  3416. }
  3417. }
  3418. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3419. {
  3420. Set_Ocpp_UpdateFirmwareStatus("");
  3421. }
  3422. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3423. {
  3424. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3425. {
  3426. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3427. return;
  3428. }
  3429. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3430. {
  3431. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3432. return;
  3433. }
  3434. }
  3435. //===============================================
  3436. // Ocpp StopTransactionOnInvalidId
  3437. //===============================================
  3438. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3439. {
  3440. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3441. {
  3442. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3443. }
  3444. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3445. {
  3446. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3447. }
  3448. return false;
  3449. }
  3450. //===============================================
  3451. // Ocpp AuthorizeRemoteTxRequests
  3452. //===============================================
  3453. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3454. {
  3455. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3456. {
  3457. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3458. }
  3459. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3460. {
  3461. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3462. }
  3463. return false;
  3464. }
  3465. //===============================================
  3466. // Ocpp ConnectionTimeOut
  3467. //===============================================
  3468. int Get_Ocpp_ConnectionTimeOut(void)
  3469. {
  3470. int timeout = 0;
  3471. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3472. {
  3473. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3474. {
  3475. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3476. }
  3477. }
  3478. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3479. {
  3480. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3481. {
  3482. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3483. }
  3484. }
  3485. return timeout > 0 ? timeout : 0;
  3486. }
  3487. //===============================================
  3488. // Ocpp DefaultPrice
  3489. //===============================================
  3490. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3491. {
  3492. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3493. {
  3494. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3495. return;
  3496. }
  3497. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3498. {
  3499. strcpy(defaultPrice, "");
  3500. return;
  3501. }
  3502. strcpy(defaultPrice, "");
  3503. }
  3504. //===============================================
  3505. // Ocpp UserPrice
  3506. //===============================================
  3507. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3508. {
  3509. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3510. {
  3511. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3512. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3513. return;
  3514. }
  3515. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3516. {
  3517. strcpy(userId, "");
  3518. strcpy(userPrice, "");
  3519. return;
  3520. }
  3521. strcpy(userId, "");
  3522. strcpy(userPrice, "");
  3523. }
  3524. void Clean_Ocpp_UserPrice(void)
  3525. {
  3526. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3527. {
  3528. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3529. return;
  3530. }
  3531. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3532. {
  3533. return;
  3534. }
  3535. }
  3536. //===============================================
  3537. // Ocpp RunningCost
  3538. //===============================================
  3539. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3540. {
  3541. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3542. {
  3543. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3544. return;
  3545. }
  3546. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3547. {
  3548. strcpy(cost, (char *)"");
  3549. return;
  3550. }
  3551. }
  3552. void Clean_Ocpp_RunningCost(int gun_index)
  3553. {
  3554. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3555. {
  3556. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3557. return;
  3558. }
  3559. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3560. {
  3561. return;
  3562. }
  3563. }
  3564. //===============================================
  3565. // Ocpp FinalCost
  3566. //===============================================
  3567. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3568. {
  3569. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3570. {
  3571. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3572. return;
  3573. }
  3574. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3575. {
  3576. strcpy(cost, (char *)"");
  3577. return;
  3578. }
  3579. }
  3580. void Clean_Ocpp_FinalCost(int gun_index)
  3581. {
  3582. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3583. {
  3584. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3585. return;
  3586. }
  3587. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3588. {
  3589. return;
  3590. }
  3591. }
  3592. int Get_Ocpp_FinalCostTxId(int gun_index)
  3593. {
  3594. int txId = 0;
  3595. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3596. {
  3597. txId = ShmOCPP16Data->Cost.FinalCost[gun_index].txId;
  3598. }
  3599. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3600. {
  3601. }
  3602. return txId;
  3603. }
  3604. //===============================================
  3605. // Ocpp TimeOffset
  3606. //===============================================
  3607. void Get_Ocpp_TimeOffset(char *timeOffset)
  3608. {
  3609. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3610. {
  3611. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3612. return;
  3613. }
  3614. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3615. {
  3616. strcpy(timeOffset, "");
  3617. return;
  3618. }
  3619. strcpy(timeOffset, "");
  3620. }
  3621. //===============================================
  3622. // Ocpp PreAuthAmount
  3623. //===============================================
  3624. void Get_Ocpp_PreAuthAmount(char *amount)
  3625. {
  3626. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3627. {
  3628. strcpy(amount, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  3629. return;
  3630. }
  3631. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3632. {
  3633. strcpy(amount, "");
  3634. return;
  3635. }
  3636. strcpy(amount, "");
  3637. }
  3638. //===============================================
  3639. // Ocpp PowerSharingServerIP
  3640. //===============================================
  3641. void Get_Ocpp_PowerSharingServerIP(char *ip)
  3642. {
  3643. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3644. {
  3645. strcpy(ip, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PowerSharingServerIP].ItemData);
  3646. return;
  3647. }
  3648. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3649. {
  3650. strcpy(ip, "");
  3651. return;
  3652. }
  3653. strcpy(ip, "");
  3654. }
  3655. //===============================================
  3656. // Ocpp Refunding
  3657. //===============================================
  3658. void Get_Ocpp_Refunding(char *Refunding)
  3659. {
  3660. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3661. {
  3662. return;
  3663. }
  3664. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3665. {
  3666. return;
  3667. }
  3668. }
  3669. void Clean_Ocpp_Refunding(void)
  3670. {
  3671. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3672. {
  3673. return;
  3674. }
  3675. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3676. {
  3677. return;
  3678. }
  3679. }
  3680. //===============================================
  3681. // Ocpp LcdOveride
  3682. //===============================================
  3683. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3684. {
  3685. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3686. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3687. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3688. }
  3689. void Clean_Ocpp_LcdOverideReq(void)
  3690. {
  3691. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3692. }
  3693. //===============================================
  3694. // Ocpp StationInfo
  3695. //===============================================
  3696. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3697. {
  3698. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3699. {
  3700. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3701. }
  3702. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3703. {
  3704. }
  3705. }
  3706. bool Is_Ocpp_ChargerStationReq(void)
  3707. {
  3708. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3709. {
  3710. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3711. }
  3712. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3713. {
  3714. return false;
  3715. }
  3716. return false;
  3717. }
  3718. void Clean_Ocpp_ChargerStationReq(void)
  3719. {
  3720. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3721. {
  3722. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3723. }
  3724. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3725. {
  3726. }
  3727. }
  3728. //===============================================
  3729. // Ocpp WeatherInfo
  3730. //===============================================
  3731. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3732. {
  3733. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3734. {
  3735. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3736. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3737. }
  3738. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3739. {
  3740. }
  3741. }
  3742. bool Is_Ocpp_WeatherReq(void)
  3743. {
  3744. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3745. {
  3746. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3747. }
  3748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3749. {
  3750. return false;
  3751. }
  3752. return false;
  3753. }
  3754. void Clean_Ocpp_WeatherReq(void)
  3755. {
  3756. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3757. {
  3758. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3759. }
  3760. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3761. {
  3762. }
  3763. }
  3764. //===============================================
  3765. // Ocpp DeductInfo
  3766. //===============================================
  3767. void Set_Ocpp_DeductTransaction(int transaction)
  3768. {
  3769. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3770. {
  3771. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3772. }
  3773. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3774. {
  3775. }
  3776. }
  3777. void Set_Ocpp_DeductCreditNo(char *userId)
  3778. {
  3779. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3780. {
  3781. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3782. }
  3783. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3784. {
  3785. }
  3786. }
  3787. void Set_Ocpp_DeductVemData(char *vemData)
  3788. {
  3789. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3790. {
  3791. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3792. }
  3793. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3794. {
  3795. }
  3796. }
  3797. void Set_Ocpp_DeductAmount(double value)
  3798. {
  3799. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3800. {
  3801. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3802. }
  3803. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3804. {
  3805. }
  3806. }
  3807. void Set_Ocpp_DeductResult(unsigned char result)
  3808. {
  3809. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3810. {
  3811. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3812. }
  3813. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3814. {
  3815. }
  3816. }
  3817. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3818. {
  3819. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3820. {
  3821. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3822. }
  3823. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3824. {
  3825. }
  3826. }
  3827. bool Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3828. {
  3829. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3830. {
  3831. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3832. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, sizeof(deductInfo->creditNo));
  3833. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, deductInfo->VemData, sizeof(deductInfo->VemData));
  3834. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3835. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber, sizeof(deductInfo->ApprovalNumber));
  3836. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3837. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3838. return true;
  3839. }
  3840. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3841. {
  3842. }
  3843. return false;
  3844. }
  3845. bool Is_Ocpp_ReportCreditDeductReq(void)
  3846. {
  3847. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3848. {
  3849. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3850. }
  3851. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3852. {
  3853. return false;
  3854. }
  3855. return false;
  3856. }
  3857. void Set_Ocpp_ReportCreditDeductReq(void)
  3858. {
  3859. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3860. {
  3861. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3862. }
  3863. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3864. {
  3865. }
  3866. }
  3867. //===============================================
  3868. // Ocpp Tcci SerialNo
  3869. //===============================================
  3870. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3871. {
  3872. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3873. {
  3874. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3875. }
  3876. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3877. {
  3878. }
  3879. }
  3880. //===============================================
  3881. // Ocpp Tcci ReaderStatus
  3882. //===============================================
  3883. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus)
  3884. {
  3885. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3886. {
  3887. if(ReaderStatus->DeductResult == _Deduct_PreAuth_Cancel || ReaderStatus->DeductResult == _Deduct_PreAuth || ReaderStatus->DeductResult == _Deduct_PreAuth_Pass)
  3888. {
  3889. switch(ReaderStatus->DeductResult)
  3890. {
  3891. case _Deduct_PreAuth_Pass:
  3892. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 1;
  3893. break;
  3894. case _Deduct_PreAuth_Cancel:
  3895. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 2;
  3896. break;
  3897. case _Deduct_PreAuth:
  3898. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 0;
  3899. break;
  3900. }
  3901. memcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, ReaderStatus->creditNo, sizeof(ReaderStatus->creditNo));
  3902. memcpy((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], ReaderStatus->VemData, sizeof(ReaderStatus->VemData));
  3903. return true;
  3904. }
  3905. }
  3906. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3907. {
  3908. }
  3909. return false;
  3910. }
  3911. void Clean_Ocpp_TcciCreditInfo(int gun_index)
  3912. {
  3913. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3914. {
  3915. memset((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo));
  3916. memset((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], 0x00, sizeof(ShmOCPP16Data->TcciCustomData.VEMData[gun_index]));
  3917. }
  3918. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3919. {
  3920. }
  3921. }
  3922. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3923. {
  3924. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3925. {
  3926. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3927. }
  3928. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3929. {
  3930. }
  3931. }
  3932. bool Is_Ocpp_TcciReaderStatusReq(int gun_index)
  3933. {
  3934. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3935. {
  3936. return ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq ? true : false;
  3937. }
  3938. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3939. {
  3940. return false;
  3941. }
  3942. return false;
  3943. }
  3944. //===============================================
  3945. // Ocpp Tcci OccupancyFeeDisplay
  3946. //===============================================
  3947. bool Is_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  3948. {
  3949. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3950. {
  3951. return ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq ? true : false;
  3952. }
  3953. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3954. {
  3955. return false;
  3956. }
  3957. return false;
  3958. }
  3959. void Clean_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  3960. {
  3961. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3962. {
  3963. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq = false;
  3964. }
  3965. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3966. {
  3967. }
  3968. }
  3969. // input: gun_index, sn_len
  3970. // output: sn
  3971. // return: action
  3972. int Get_Ocpp_TcciOccupancyAction(int gun_index, int sn_len, char *sn)
  3973. {
  3974. int action = 0;
  3975. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3976. {
  3977. action = ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId;
  3978. if(action == _ParkingAction_DisplayButton)
  3979. {
  3980. memcpy(sn, ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, sn_len);
  3981. }
  3982. }
  3983. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3984. {
  3985. }
  3986. return action;
  3987. }
  3988. void Clean_Ocpp_TcciOccupancyActionAndSn(int gun_index)
  3989. {
  3990. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3991. {
  3992. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId = 0;
  3993. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN));
  3994. }
  3995. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3996. {
  3997. }
  3998. }
  3999. //===============================================
  4000. // Ocpp Tcci SetOccupancyPrice
  4001. //===============================================
  4002. bool Is_Ocpp_TcciSetOccupancyPriceReq(void)
  4003. {
  4004. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4005. {
  4006. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq ? true : false;
  4007. }
  4008. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4009. {
  4010. return false;
  4011. }
  4012. return false;
  4013. }
  4014. void Clean_Ocpp_TcciSetOccupancyPriceReq(void)
  4015. {
  4016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4017. {
  4018. ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq = false;
  4019. }
  4020. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4021. {
  4022. }
  4023. }
  4024. float Get_Ocpp_TcciOccupancyPrice(void)
  4025. {
  4026. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4027. {
  4028. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.price;
  4029. }
  4030. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4031. {
  4032. }
  4033. return 0;
  4034. }
  4035. //===============================================
  4036. // Ocpp Tcci GetOccupancyFee
  4037. //===============================================
  4038. bool Is_Ocpp_TcciOccupancyFeeActReq(int gun_index)
  4039. {
  4040. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4041. {
  4042. return ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq ? true : false;
  4043. }
  4044. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4045. {
  4046. return false;
  4047. }
  4048. return false;
  4049. }
  4050. void Set_Ocpp_TcciOccupancyFeeAct(int gun_index, OccupancyBillInfo *bill, int action)
  4051. {
  4052. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4053. {
  4054. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN));
  4055. memcpy(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, bill->Sn, sizeof(bill->Sn));
  4056. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = action;
  4057. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = true;
  4058. }
  4059. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4060. {
  4061. }
  4062. }
  4063. void Clean_Ocpp_TcciNotifyOccupancyFeeActReq(int gun_index)
  4064. {
  4065. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4066. {
  4067. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = _Notify_None;
  4068. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = false;
  4069. }
  4070. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4071. {
  4072. }
  4073. }
  4074. void Get_Ocpp_TcciGetOccupancyFeeResponse(int gun_index, OccupancyBillInfo *bill)
  4075. {
  4076. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4077. {
  4078. bill->Duration = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_duration;
  4079. bill->Fee = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_occupancyFee;
  4080. memcpy(bill->StartTime, ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_startTime, sizeof(bill->StartTime));
  4081. }
  4082. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4083. {
  4084. }
  4085. }
  4086. bool Is_Ocpp_TcciOccupancyFeeActStatus(int gun_index, char *status)
  4087. {
  4088. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4089. {
  4090. return strcmp((char *)ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_status, status) == EQUAL ? true : false;
  4091. }
  4092. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4093. {
  4094. }
  4095. return false;
  4096. }
  4097. //===============================================
  4098. // Ocpp Tcci ReportOccupancyDeductReq
  4099. //===============================================
  4100. bool Is_Ocpp_TcciReportOccupancyDeductReq(void)
  4101. {
  4102. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4103. {
  4104. return ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq ? true : false;
  4105. }
  4106. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4107. {
  4108. return false;
  4109. }
  4110. return false;
  4111. }
  4112. void Set_Ocpp_TcciReportOccupancyDeductReq(void)
  4113. {
  4114. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4115. {
  4116. ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq = true;
  4117. }
  4118. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4119. {
  4120. }
  4121. }
  4122. //===============================================
  4123. // Ocpp Tcci OccupancyDeductResult
  4124. //===============================================
  4125. bool Set_Ocpp_OccupancyDeductResult(OccupancyDeductInfo *deduct, bool pass_fail)
  4126. {
  4127. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4128. {
  4129. memset(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult, 0x00, sizeof(struct StructOccupancyDeductResult));
  4130. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.deductResult = pass_fail;
  4131. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.amount = deduct->Fee;
  4132. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.occupancySN[0], &deduct->Sn[0], sizeof(deduct->Sn));
  4133. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.creditNo[0], &deduct->creditNo[0], sizeof(deduct->creditNo));
  4134. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.approvalNo[0], &deduct->ApprovalNumber[0], sizeof(deduct->ApprovalNumber));
  4135. return true;
  4136. }
  4137. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4138. {
  4139. }
  4140. return false;
  4141. }
  4142. //===============================================
  4143. // Ocpp Start Transaction
  4144. //===============================================
  4145. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  4146. {
  4147. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4148. {
  4149. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  4150. }
  4151. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4152. {
  4153. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4154. }
  4155. // MaintainServer
  4156. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4157. {
  4158. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  4159. }
  4160. }
  4161. void Set_Ocpp_StartTransactionReq(int gun_index)
  4162. {
  4163. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4164. {
  4165. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  4166. }
  4167. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4168. {
  4169. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4170. }
  4171. // MaintainServer
  4172. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4173. {
  4174. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  4175. }
  4176. }
  4177. bool Is_Ocpp_StartTransactionConf(int gun_index)
  4178. {
  4179. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4180. {
  4181. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  4182. }
  4183. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4184. {
  4185. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES)
  4186. {
  4187. if(strstr((char*)ShmOCPP20Data->TransactionEvent[gun_index].eventType, "Started") != NULL)
  4188. {
  4189. return true;
  4190. }
  4191. else
  4192. {
  4193. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4194. return false;
  4195. }
  4196. }
  4197. else
  4198. {
  4199. return false;
  4200. }
  4201. }
  4202. return false;
  4203. }
  4204. void Clean_Ocpp_StartTransactionConf(int gun_index)
  4205. {
  4206. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4207. {
  4208. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  4209. }
  4210. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4211. {
  4212. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4213. }
  4214. // MaintainServer
  4215. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4216. {
  4217. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  4218. }
  4219. }
  4220. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4221. {
  4222. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4223. {
  4224. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  4225. }
  4226. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4227. {
  4228. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  4229. }
  4230. return false;
  4231. }
  4232. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  4233. {
  4234. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  4235. }
  4236. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4237. {
  4238. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4239. {
  4240. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  4241. }
  4242. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4243. {
  4244. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  4245. }
  4246. }
  4247. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  4248. {
  4249. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4250. {
  4251. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  4252. return;
  4253. }
  4254. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4255. {
  4256. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  4257. return;
  4258. }
  4259. sprintf(transactionId, "%d", 0);
  4260. }
  4261. bool Is_Invalid_StartTransactionId(int gun_index)
  4262. {
  4263. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  4264. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  4265. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  4266. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  4267. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  4268. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  4269. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  4270. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  4271. {
  4272. return true;
  4273. }
  4274. return false;
  4275. }
  4276. //===============================================
  4277. // Ocpp Stop Transaction
  4278. //===============================================
  4279. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  4280. {
  4281. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4282. {
  4283. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  4284. return;
  4285. }
  4286. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4287. {
  4288. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4289. return;
  4290. }
  4291. }
  4292. void Set_Ocpp_StopTransactionReq(int gun_index)
  4293. {
  4294. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4295. {
  4296. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  4297. }
  4298. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4299. {
  4300. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4301. }
  4302. // MaintainServer
  4303. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4304. {
  4305. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  4306. }
  4307. }
  4308. //===============================================
  4309. // Ocpp Check Backup Error Code
  4310. //===============================================
  4311. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  4312. {
  4313. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4314. {
  4315. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  4316. {
  4317. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  4318. {
  4319. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  4320. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  4321. }
  4322. }
  4323. }
  4324. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4325. {
  4326. }
  4327. }
  4328. //===============================================
  4329. // Ocpp Reserve Now
  4330. //===============================================
  4331. bool Is_Ocpp_ReserveNowReq(int gun_index)
  4332. {
  4333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4334. {
  4335. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4336. }
  4337. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4338. {
  4339. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4340. }
  4341. return false;
  4342. }
  4343. void Clean_Ocpp_ReserveNowReq(int gun_index)
  4344. {
  4345. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4346. {
  4347. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4348. return;
  4349. }
  4350. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4351. {
  4352. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4353. return;
  4354. }
  4355. }
  4356. int Get_Ocpp_ReserveNowId(int gun_index)
  4357. {
  4358. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4359. {
  4360. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  4361. }
  4362. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4363. {
  4364. return ShmOCPP20Data->ReserveNow[gun_index].id;
  4365. }
  4366. return 0;
  4367. }
  4368. void Set_Ocpp_ReserveNowConf(int gun_index)
  4369. {
  4370. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4371. {
  4372. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4373. return;
  4374. }
  4375. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4376. {
  4377. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4378. return;
  4379. }
  4380. }
  4381. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  4382. {
  4383. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4384. {
  4385. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  4386. return;
  4387. }
  4388. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4389. {
  4390. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  4391. return;
  4392. }
  4393. strcpy(date, "");
  4394. return;
  4395. }
  4396. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  4397. {
  4398. bool _expired = true;
  4399. char _expiryDate[128];
  4400. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  4401. _expired = CheckTimeOut(_expiryDate);
  4402. return _expired;
  4403. }
  4404. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  4405. {
  4406. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4407. {
  4408. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4409. return;
  4410. }
  4411. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4412. {
  4413. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  4414. return;
  4415. }
  4416. strcpy(idTag, "");
  4417. return;
  4418. }
  4419. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  4420. {
  4421. char ReserveIdTag[32];
  4422. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  4423. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  4424. }
  4425. //===============================================
  4426. // Ocpp Cancel Reservation
  4427. //===============================================
  4428. bool Is_Ocpp_CancelReservationReq(int gun_index)
  4429. {
  4430. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4431. {
  4432. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4433. }
  4434. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4435. {
  4436. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4437. }
  4438. return false;
  4439. }
  4440. void Clean_Ocpp_CancelReservationReq(int gun_index)
  4441. {
  4442. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4443. {
  4444. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4445. return;
  4446. }
  4447. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4448. {
  4449. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4450. return;
  4451. }
  4452. }
  4453. void Set_Ocpp_CancelReservationConf(int gun_index)
  4454. {
  4455. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4456. {
  4457. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4458. return;
  4459. }
  4460. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4461. {
  4462. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4463. return;
  4464. }
  4465. }
  4466. //===============================================
  4467. // Ocpp Change Availability
  4468. //===============================================
  4469. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  4470. {
  4471. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4472. {
  4473. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4474. }
  4475. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4476. {
  4477. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4478. }
  4479. return false;
  4480. }
  4481. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  4482. {
  4483. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4484. {
  4485. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4486. return;
  4487. }
  4488. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4489. {
  4490. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4491. return;
  4492. }
  4493. }
  4494. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  4495. {
  4496. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4497. {
  4498. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  4499. }
  4500. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4501. {
  4502. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  4503. }
  4504. return false;
  4505. }
  4506. //===============================================
  4507. // Ocpp Unlock Connector
  4508. //===============================================
  4509. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4510. {
  4511. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4512. {
  4513. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4514. }
  4515. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4516. {
  4517. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4518. }
  4519. return false;
  4520. }
  4521. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4522. {
  4523. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4524. {
  4525. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4526. return;
  4527. }
  4528. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4529. {
  4530. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4531. return;
  4532. }
  4533. }
  4534. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4535. {
  4536. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4537. {
  4538. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4539. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4540. return;
  4541. }
  4542. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4543. {
  4544. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4545. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4546. return;
  4547. }
  4548. }
  4549. //===============================================
  4550. // Ocpp Remote Start
  4551. //===============================================
  4552. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4553. {
  4554. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4555. {
  4556. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4557. }
  4558. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4559. {
  4560. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4561. }
  4562. return false;
  4563. }
  4564. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4565. {
  4566. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4567. {
  4568. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4569. return;
  4570. }
  4571. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4572. {
  4573. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4574. return;
  4575. }
  4576. }
  4577. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4578. {
  4579. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4580. {
  4581. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4582. return;
  4583. }
  4584. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4585. {
  4586. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4587. return;
  4588. }
  4589. strcpy(idTag, "");
  4590. }
  4591. //===============================================
  4592. // Ocpp Remote Stop
  4593. //===============================================
  4594. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4595. {
  4596. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4597. {
  4598. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4599. }
  4600. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4601. {
  4602. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4603. }
  4604. return false;
  4605. }
  4606. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4607. {
  4608. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4609. {
  4610. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4611. return;
  4612. }
  4613. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4614. {
  4615. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4616. return;
  4617. }
  4618. }
  4619. //===============================================
  4620. // Ocpp StopReason
  4621. //===============================================
  4622. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4623. {
  4624. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4625. {
  4626. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4627. {
  4628. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4629. }
  4630. }
  4631. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4632. {
  4633. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4634. {
  4635. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4636. }
  4637. }
  4638. // Maintain server
  4639. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4640. {
  4641. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4642. }
  4643. }
  4644. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4645. {
  4646. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4647. {
  4648. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4649. return;
  4650. }
  4651. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4652. {
  4653. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4654. return;
  4655. }
  4656. strcpy(reason, "");
  4657. }
  4658. void Clean_Ocpp_StopReason(int gun_index)
  4659. {
  4660. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4661. {
  4662. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4663. }
  4664. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4665. {
  4666. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4667. }
  4668. // Maintain server
  4669. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4670. {
  4671. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4672. }
  4673. }
  4674. //===============================================
  4675. // Ocpp StatusNotification ErrorCode
  4676. //===============================================
  4677. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4678. {
  4679. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4680. {
  4681. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4682. return;
  4683. }
  4684. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4685. {
  4686. return;
  4687. }
  4688. }
  4689. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4690. {
  4691. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4692. {
  4693. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4694. }
  4695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4696. {
  4697. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4698. }
  4699. return false;
  4700. }
  4701. //===============================================
  4702. // Ocpp StatusNotification VendorErrorCode
  4703. //===============================================
  4704. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4705. {
  4706. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4707. {
  4708. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4709. return;
  4710. }
  4711. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4712. {
  4713. return;
  4714. }
  4715. }
  4716. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4717. {
  4718. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4719. {
  4720. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4721. }
  4722. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4723. {
  4724. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4725. }
  4726. return false;
  4727. }
  4728. //===============================================
  4729. // Ocpp ChargingProfileConf
  4730. //===============================================
  4731. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4732. {
  4733. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4734. {
  4735. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4736. }
  4737. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4738. {
  4739. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4740. }
  4741. return false;
  4742. }
  4743. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4744. {
  4745. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4746. {
  4747. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4748. return;
  4749. }
  4750. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4751. {
  4752. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4753. return;
  4754. }
  4755. }
  4756. //===============================================
  4757. // Ocpp ChargingProfileReq
  4758. //===============================================
  4759. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4760. {
  4761. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4762. {
  4763. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4764. }
  4765. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4766. {
  4767. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4768. }
  4769. return false;
  4770. }
  4771. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4772. {
  4773. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4774. {
  4775. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4776. return;
  4777. }
  4778. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4779. {
  4780. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4781. return;
  4782. }
  4783. }
  4784. //===============================================
  4785. // Ocpp SmartChargingProfile ChargingProfileKind
  4786. //===============================================
  4787. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4788. {
  4789. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4790. {
  4791. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4792. }
  4793. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4794. {
  4795. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4796. }
  4797. return false;
  4798. }
  4799. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4800. {
  4801. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4802. {
  4803. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4804. }
  4805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4806. {
  4807. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4808. }
  4809. return false;
  4810. }
  4811. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4812. {
  4813. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4814. {
  4815. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4816. }
  4817. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4818. {
  4819. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4820. }
  4821. return 0;
  4822. }
  4823. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4824. {
  4825. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4826. {
  4827. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4828. }
  4829. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4830. {
  4831. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4832. }
  4833. return 0;
  4834. }
  4835. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4836. {
  4837. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4838. {
  4839. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4840. }
  4841. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4842. {
  4843. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4844. }
  4845. return 0;
  4846. }
  4847. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4848. {
  4849. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4850. {
  4851. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4852. }
  4853. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4854. {
  4855. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4856. }
  4857. return 0;
  4858. }
  4859. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4860. {
  4861. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4862. {
  4863. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4864. }
  4865. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4866. {
  4867. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4868. }
  4869. return 0;
  4870. }
  4871. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4872. {
  4873. int _time = 0, _scheduleTime = 0;
  4874. int _maxCount = 0;
  4875. float _limit = 0;
  4876. int _startCount = NO_DEFINE;
  4877. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4878. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4879. for(int i = 0; i < _maxCount; i++)
  4880. {
  4881. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4882. if(_time >= _scheduleTime)
  4883. {
  4884. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4885. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4886. {
  4887. _startCount = i;
  4888. }
  4889. }
  4890. }
  4891. return _startCount;
  4892. }
  4893. //===============================================
  4894. // Ocpp MaxChargingProfile ChargingProfileKind
  4895. //===============================================
  4896. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4897. {
  4898. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4899. {
  4900. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4901. }
  4902. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4903. {
  4904. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4905. }
  4906. return false;
  4907. }
  4908. bool Is_Ocpp_MaxChargingProfileId(void)
  4909. {
  4910. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4911. {
  4912. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4913. }
  4914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4915. {
  4916. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4917. }
  4918. return false;
  4919. }
  4920. int Get_Ocpp_MaxProfileStartTime(void)
  4921. {
  4922. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4923. {
  4924. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4925. }
  4926. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4927. {
  4928. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4929. }
  4930. return 0;
  4931. }
  4932. int Get_Ocpp_MaxProfileScheduleCount(void)
  4933. {
  4934. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4935. {
  4936. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4937. }
  4938. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4939. {
  4940. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4941. }
  4942. return 0;
  4943. }
  4944. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4945. {
  4946. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4947. {
  4948. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4949. }
  4950. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4951. {
  4952. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4953. }
  4954. return 0;
  4955. }
  4956. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  4957. {
  4958. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4959. {
  4960. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4961. }
  4962. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4963. {
  4964. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4965. }
  4966. return 0;
  4967. }
  4968. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  4969. {
  4970. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4971. {
  4972. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4973. }
  4974. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4975. {
  4976. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4977. }
  4978. return 0;
  4979. }
  4980. int Get_Ocpp_MaxProfileStartIndex(void)
  4981. {
  4982. int _time = 0, _scheduleTime = 0;
  4983. int _maxCount = 0;
  4984. float _limit = 0;
  4985. int _startCount = NO_DEFINE;
  4986. _time = Get_Ocpp_MaxProfileStartTime();
  4987. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  4988. for(int i = 0; i < _maxCount; i++)
  4989. {
  4990. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  4991. if(_time >= _scheduleTime)
  4992. {
  4993. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  4994. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4995. {
  4996. _startCount = i;
  4997. }
  4998. }
  4999. }
  5000. return _startCount;
  5001. }
  5002. //===============================================
  5003. // Ocpp isRemoteStartWaitReq
  5004. //===============================================
  5005. int Is_Ocpp_RemoteStartWaitReq(void)
  5006. {
  5007. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5008. {
  5009. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  5010. }
  5011. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5012. {
  5013. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  5014. }
  5015. return 0;
  5016. }
  5017. //===============================================
  5018. // Ocpp SessionTarget
  5019. //===============================================
  5020. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  5021. {
  5022. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5023. {
  5024. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5025. }
  5026. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5027. {
  5028. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5029. }
  5030. }
  5031. //===============================================
  5032. // Ocpp OffLineMaxChargingPower
  5033. //===============================================
  5034. int Get_Ocpp_OffLineMaxChargingPower(void)
  5035. {
  5036. // unit: W
  5037. int maxChargingPower = 0;
  5038. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5039. {
  5040. maxChargingPower = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);
  5041. }
  5042. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5043. {
  5044. maxChargingPower = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OffLineMaxChargingPower].variableAttribute[0].value);
  5045. }
  5046. return maxChargingPower > 0 ? maxChargingPower : 0;
  5047. }
  5048. //===============================================
  5049. // Ocpp OcmfData
  5050. //===============================================
  5051. bool Is_Ocpp_SendOcmfDataReq(int gunIndex)
  5052. {
  5053. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5054. {
  5055. return ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq == YES ? true : false;
  5056. }
  5057. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5058. {
  5059. }
  5060. return false;
  5061. }
  5062. void Set_Ocpp_SendOcmfDataReq(int gunIndex)
  5063. {
  5064. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5065. {
  5066. ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq = YES;
  5067. }
  5068. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5069. {
  5070. }
  5071. }
  5072. void Clean_Ocpp_SendOcmfDataReq(int gunIndex)
  5073. {
  5074. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5075. {
  5076. ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq = NO;
  5077. }
  5078. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5079. {
  5080. }
  5081. }
  5082. void Set_Ocpp_OcmfData(int gunIndex, char *key, char *ocmfInfo)
  5083. {
  5084. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5085. {
  5086. strncpy((char *)ShmOCPP16Data->OcmfData[gunIndex].PublicKey, key, sizeof(ShmOCPP16Data->OcmfData[gunIndex].PublicKey));
  5087. strncpy((char *)ShmOCPP16Data->OcmfData[gunIndex].DataString, ocmfInfo, sizeof(ShmOCPP16Data->OcmfData[gunIndex].DataString));
  5088. }
  5089. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5090. {
  5091. }
  5092. }
  5093. //===============================================
  5094. // Set_StartIdType
  5095. //===============================================
  5096. void Set_StartIdType(int gunIndex, int type)
  5097. {
  5098. chargingInfo[gunIndex]->StartIdType = type;
  5099. }
  5100. //===============================================
  5101. // DeductResult
  5102. //===============================================
  5103. void Set_Deduct_UserId(int gunIndex, char *userId)
  5104. {
  5105. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  5106. }
  5107. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  5108. {
  5109. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  5110. }
  5111. void Clean_DeductInfo(int gunIndex)
  5112. {
  5113. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5114. }
  5115. bool Is_DeductResultReq(int gunIndex)
  5116. {
  5117. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  5118. }
  5119. //===============================================
  5120. // ReaderStatus
  5121. //===============================================
  5122. bool Is_ReaderStatusReq(int gunIndex)
  5123. {
  5124. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  5125. }
  5126. void Clean_ReaderStatusInfo(int gunIndex)
  5127. {
  5128. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5129. }
  5130. //===============================================
  5131. // TransactionInfoData
  5132. //===============================================
  5133. void Set_UserTransactionId(int gunIndex, int transaction)
  5134. {
  5135. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  5136. }
  5137. void Set_UserPrice(int gunIndex, float price)
  5138. {
  5139. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  5140. }
  5141. void Set_UserTotalCost(int gunIndex, float cost)
  5142. {
  5143. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  5144. }
  5145. void Set_UserAccountBalance(int gunIndex, float balance)
  5146. {
  5147. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  5148. }
  5149. void Set_UserDiscount(int gunIndex, float discount)
  5150. {
  5151. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  5152. }
  5153. void Clean_UserTransaction(int gunIndex)
  5154. {
  5155. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  5156. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  5157. }
  5158. void Clean_UserPriceAndReceipt(int gunIndex)
  5159. {
  5160. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  5161. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  5162. }
  5163. //===============================================
  5164. // OccupancyDeductReq
  5165. //===============================================
  5166. bool Is_OccupancyDeductReq(int gunIndex)
  5167. {
  5168. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq ? true : false;
  5169. }
  5170. void Clean_OccupancyDeductReq(int gunIndex)
  5171. {
  5172. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq = false;
  5173. }
  5174. bool Is_OccupancyKeepCountingReq(int gunIndex)
  5175. {
  5176. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting ? true : false;
  5177. }
  5178. void Set_OccupancyKeepCountingReq(int gunIndex)
  5179. {
  5180. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = true;
  5181. }
  5182. void Clean_OccupancyKeepCountingReq(int gunIndex)
  5183. {
  5184. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = false;
  5185. }
  5186. bool Is_OccupancyFeeForceCancelReq(int gunIndex)
  5187. {
  5188. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel ? true : false;
  5189. }
  5190. void Set_OccupancyFeeForceCancelReq(int gunIndex)
  5191. {
  5192. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel = true;
  5193. }
  5194. void Clean_OccupancyFeeForceCancelReq(int gunIndex)
  5195. {
  5196. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel = false;
  5197. }
  5198. //===============================================
  5199. // Ocpp GetDiagnosticsReq
  5200. //===============================================
  5201. bool Is_Ocpp_GetDiagnosticsReq(void)
  5202. {
  5203. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5204. {
  5205. return ShmOCPP16Data->MsMsg.bits.GetDiagnosticsReq == YES ? true : false;
  5206. }
  5207. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5208. {
  5209. }
  5210. return false;
  5211. }
  5212. void Clean_Ocpp_GetDiagnosticsReq(void)
  5213. {
  5214. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5215. {
  5216. ShmOCPP16Data->MsMsg.bits.GetDiagnosticsReq = false;
  5217. }
  5218. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5219. {
  5220. }
  5221. }
  5222. //===============================================
  5223. // Main process
  5224. //===============================================
  5225. // 檢查 Byte 中某個 Bit 的值
  5226. // _byte : 欲改變的 byte
  5227. // _bit : 該 byte 的第幾個 bit
  5228. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  5229. {
  5230. return ( _byte & mask_table[_bit] ) != 0x00;
  5231. }
  5232. // 設定 Byte 中某個 Bit的值
  5233. // _byte : 欲改變的 byte
  5234. // _bit : 該 byte 的第幾個 bit
  5235. // value : 修改的值為 0 or 1
  5236. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  5237. {
  5238. if(value == 1)
  5239. *_byte |= (1 << _bit);
  5240. else if (value == 0)
  5241. *_byte ^= (1 << _bit);
  5242. }
  5243. void UserScanFunction()
  5244. {
  5245. }
  5246. unsigned char isModeChange(unsigned char gun_index)
  5247. {
  5248. unsigned char result = NO;
  5249. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  5250. {
  5251. result = YES;
  5252. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  5253. }
  5254. return result;
  5255. }
  5256. void AnnounceAccountBalance(unsigned char index, float balance)
  5257. {
  5258. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  5259. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  5260. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  5261. {
  5262. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  5263. }
  5264. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  5265. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  5266. }
  5267. void FouceAnnounceAccountBalance(unsigned char index)
  5268. {
  5269. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  5270. {
  5271. AnnounceAccountBalance(index, 0);
  5272. }
  5273. }
  5274. void CleanConnectorUserIdAndPrice(unsigned char index)
  5275. {
  5276. if(chargingInfo[index]->SystemStatus == S_IDLE)
  5277. {
  5278. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  5279. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  5280. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  5281. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  5282. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  5283. }
  5284. }
  5285. void DispenserAuthInitial(unsigned char dispenser)
  5286. {
  5287. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  5288. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5289. // clean dispenser price
  5290. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  5291. }
  5292. void GunAuthInitial(unsigned char connector)
  5293. {
  5294. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  5295. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5296. }
  5297. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  5298. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  5299. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  5300. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  5301. void AuthorizeTimeoutProcess(void)
  5302. {
  5303. int CompletedTime = 0;
  5304. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5305. {
  5306. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  5307. {
  5308. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  5309. {
  5310. GetClockTime(&_GunAuth_Time[index]);
  5311. }
  5312. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5313. {
  5314. FouceAnnounceAccountBalance(index);
  5315. }
  5316. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5317. {
  5318. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  5319. {
  5320. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  5321. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5322. CleanConnectorUserIdAndPrice(index);
  5323. }
  5324. }
  5325. }
  5326. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  5327. }
  5328. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5329. {
  5330. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  5331. {
  5332. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  5333. {
  5334. GetClockTime(&_DispenserAuth_Time[index]);
  5335. }
  5336. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  5337. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  5338. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  5339. {
  5340. DispenserAuthInitial(index);
  5341. }
  5342. }
  5343. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  5344. }
  5345. }
  5346. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  5347. {
  5348. BOOL success = false;
  5349. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  5350. {
  5351. success = true;
  5352. }
  5353. return success;
  5354. }
  5355. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  5356. {
  5357. BOOL success = false;
  5358. unsigned char dispenser = 0;
  5359. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5360. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  5361. {
  5362. success = true;
  5363. }
  5364. return success;
  5365. }
  5366. bool CheckConnectorTypeStatus(void)
  5367. {
  5368. bool result = true;
  5369. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  5370. {
  5371. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = ShmChargerInfo->Control.MaxConnector;
  5372. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5373. ShmChargerInfo->Control.PsuQuantityByModel = ShmChargerInfo->Control.ChargerRatingPower / (INFY_SINGLE_PSU_POWER / 100);
  5374. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  5375. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  5376. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  5377. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  5378. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  5379. {
  5380. result = false;
  5381. }
  5382. //DO360 charging info default setting
  5383. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5384. {
  5385. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  5386. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  5387. chargingInfo[i]->Evboard_id = 0x00;
  5388. chargingInfo[i]->ReservationId = -1;
  5389. chargingInfo[i]->slotsIndex = 0;
  5390. chargingInfo[i]->SystemStatus = S_BOOTING;
  5391. chargingInfo[i]->Type = _Type_Unknown;
  5392. chargingInfo[i]->type_index = 0;
  5393. chargingInfo[i]->IsAvailable = YES;
  5394. }
  5395. }
  5396. else
  5397. {
  5398. // Module Name 不正確 - 告警
  5399. result = false;
  5400. }
  5401. return result;
  5402. }
  5403. void KillTaskExceptPrimary()
  5404. {
  5405. system("killall Module_Authorize");
  5406. system("killall Module_CabinetParallel");
  5407. system("killall Module_ChargerSelfTest");
  5408. system("killall Module_LedIndication");
  5409. system("killall Module_EvComm");
  5410. system("killall Module_InternalComm");
  5411. system("killall Module_Firewall");
  5412. KillPowerModuleTask();
  5413. system("killall Module_4g &");
  5414. system("killall Module_Wifi &");
  5415. }
  5416. void KillNtpd(void)
  5417. {
  5418. system("killall ntpd");
  5419. }
  5420. void KillAllTask()
  5421. {
  5422. system("killall Module_Authorize");
  5423. system("killall Module_CabinetParallel");
  5424. system("killall Module_ChargerSelfTest");
  5425. system("killall Module_LedIndication");
  5426. system("killall Module_EventLogging");
  5427. system("killall Module_PrimaryComm");
  5428. system("killall Module_EvComm");
  5429. system("killall Module_LcmControl");
  5430. system("killall Module_InternalComm");
  5431. system("killall Module_Firewall");
  5432. KillPowerModuleTask();
  5433. Kill_Ocpp_Service();
  5434. Kill_MaintainOcpp_Service();
  5435. system("killall Module_4g &");
  5436. system("killall Module_Wifi &");
  5437. }
  5438. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  5439. {
  5440. int isPass = PASS;
  5441. unsigned char ftpcmdbuf[256];
  5442. //Using ftpput command to transfer upgrade image,
  5443. //User name : root
  5444. //User password : y42j/4cj84
  5445. //Destination : as same as source path
  5446. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s \"%s\" \"%s\"", IpAddress, SourcePath, SourcePath);
  5447. if(system((char*)ftpcmdbuf) != 0)
  5448. {
  5449. isPass = FAIL;
  5450. }
  5451. return isPass;
  5452. }
  5453. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  5454. {
  5455. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  5456. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  5457. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  5458. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  5459. }
  5460. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName, char *model_name)
  5461. {
  5462. int isPass = FAIL;
  5463. int isImageOK[GENERAL_GUN_QUANTITY];
  5464. int dispenser_index = 0;
  5465. BOOL transferred = FALSE;
  5466. memset(isImageOK, 0x00, sizeof(isImageOK));
  5467. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5468. {
  5469. isPass = FAIL;
  5470. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  5471. {
  5472. dispenser_index = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  5473. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName, (char *)model_name) != EQUAL)
  5474. {
  5475. LOG_INFO("Dispenser %d model name [%s] update ignore",
  5476. dispenser_index + 1,
  5477. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName);
  5478. continue;
  5479. }
  5480. char DispenserIpAddress[16];
  5481. memset((char*)DispenserIpAddress, 0x00, 16);
  5482. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  5483. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  5484. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  5485. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  5486. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  5487. LOG_INFO("Transfer Image(%s) to Dispenser %d (IP: %s)", SourcePath, dispenser_index + 1, DispenserIpAddress);
  5488. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  5489. isImageOK[dispenser_index] = isPass;
  5490. LOG_INFO("Dispenser %d Put Image %s", dispenser_index + 1, isPass == PASS ? "OK" : "Fail");
  5491. }
  5492. }
  5493. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5494. {
  5495. if(isImageOK[i] > 0)
  5496. {
  5497. TriggerDispenserUpgrade(i, FileName);
  5498. transferred = true;
  5499. }
  5500. }
  5501. return transferred;
  5502. }
  5503. BOOL WaitAllDispenserUpgradeCompleted(void)
  5504. {
  5505. BOOL completed = TRUE;
  5506. GetClockTime(&_DispenserUpgrade_time);
  5507. do
  5508. {
  5509. completed = TRUE;
  5510. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5511. {
  5512. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  5513. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  5514. {
  5515. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  5516. {
  5517. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  5518. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  5519. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  5520. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  5521. }
  5522. else
  5523. {
  5524. completed = FALSE;
  5525. }
  5526. }
  5527. }
  5528. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  5529. {
  5530. LOG_INFO("Wait Upgrade Timeout");
  5531. completed = FALSE;
  5532. break;
  5533. }
  5534. if(completed == FALSE)
  5535. {
  5536. sleep(1);
  5537. }
  5538. }while(!completed);
  5539. return completed;
  5540. }
  5541. void UpgradePrepare(void)
  5542. {
  5543. int count = 0;
  5544. SetAcContactor(OFF);
  5545. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5546. {
  5547. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5548. {
  5549. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5550. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5551. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5552. {
  5553. count++;
  5554. }
  5555. }
  5556. }
  5557. if(count > 0)
  5558. {
  5559. sleep(3);
  5560. }
  5561. else
  5562. {
  5563. sleep(1);
  5564. }
  5565. }
  5566. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  5567. {
  5568. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  5569. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  5570. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  5571. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  5572. }
  5573. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  5574. {
  5575. int isPass = FAIL;
  5576. BOOL transferred = FALSE;
  5577. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5578. {
  5579. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5580. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5581. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5582. {
  5583. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  5584. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  5585. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  5586. {
  5587. char PCabinetIpAddress[16];
  5588. memset((char*)PCabinetIpAddress, 0x00, 16);
  5589. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  5590. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  5591. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  5592. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  5593. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  5594. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  5595. if(isPass == PASS)
  5596. {
  5597. TriggerPCabinetUpgrade(i, FileName);
  5598. }
  5599. transferred = true;
  5600. }
  5601. }
  5602. }
  5603. return transferred;
  5604. }
  5605. BOOL WaitAllPCabinetStartUpgrade(void)
  5606. {
  5607. BOOL isStart = true;
  5608. int count = 0;
  5609. struct timespec _waitStart;
  5610. GetClockTime(&_waitStart);
  5611. do
  5612. {
  5613. isStart = true;
  5614. count = 0;
  5615. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5616. {
  5617. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5618. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5619. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5620. {
  5621. count++;
  5622. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  5623. {
  5624. isStart = false;
  5625. }
  5626. }
  5627. }
  5628. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  5629. {
  5630. isStart = false;
  5631. break;
  5632. }
  5633. usleep(10000);
  5634. }while(!isStart);
  5635. if(isStart && count > 0)
  5636. {
  5637. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5638. }
  5639. else
  5640. {
  5641. if(count > 0)
  5642. {
  5643. LOG_INFO("Wait PCabinet upgrade timeout");
  5644. }
  5645. else
  5646. {
  5647. LOG_INFO("No PCabinet need to upgrade");
  5648. }
  5649. }
  5650. return isStart;
  5651. }
  5652. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5653. {
  5654. BOOL completed = true;
  5655. int count = 0;
  5656. struct timespec _waitCompleted;
  5657. GetClockTime(&_waitCompleted);
  5658. do
  5659. {
  5660. completed = true;
  5661. count = 0;
  5662. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5663. {
  5664. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5665. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5666. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5667. {
  5668. count++;
  5669. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5670. {
  5671. completed = false;
  5672. }
  5673. }
  5674. }
  5675. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5676. {
  5677. completed = false;
  5678. break;
  5679. }
  5680. usleep(10000);
  5681. }while(!completed);
  5682. if(completed && count > 0)
  5683. {
  5684. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5685. }
  5686. else
  5687. {
  5688. if(count > 0)
  5689. {
  5690. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5691. }
  5692. else
  5693. {
  5694. LOG_INFO("Need not to wait PCabinet completed");
  5695. }
  5696. }
  5697. return completed;
  5698. }
  5699. BOOL WaitMCabinetUpgradeCompleted(void)
  5700. {
  5701. BOOL completed = true;
  5702. struct timespec _waitCompleted;
  5703. GetClockTime(&_waitCompleted);
  5704. do
  5705. {
  5706. completed = false;
  5707. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5708. {
  5709. completed = true;
  5710. }
  5711. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5712. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5713. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5714. {
  5715. completed = false;
  5716. break;
  5717. }
  5718. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5719. {
  5720. completed = false;
  5721. break;
  5722. }
  5723. usleep(10000);
  5724. }while(!completed);
  5725. if(completed)
  5726. {
  5727. LOG_INFO("MCabinet upgrade completed!");
  5728. }
  5729. else
  5730. {
  5731. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5732. }
  5733. return completed;
  5734. }
  5735. bool IsModelNameMatch(char *model)
  5736. {
  5737. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5738. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5739. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5740. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5741. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5742. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5743. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5744. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5745. {
  5746. return true;
  5747. }
  5748. return false;
  5749. }
  5750. unsigned long long getAvailableMemory(void)
  5751. {
  5752. long pages = sysconf(_SC_AVPHYS_PAGES);
  5753. long page_size = sysconf(_SC_PAGE_SIZE);
  5754. return pages * page_size;
  5755. }
  5756. void ImageTypeParsing(unsigned int type, char *type_str)
  5757. {
  5758. switch(type)
  5759. {
  5760. case 0x10000001:
  5761. sprintf(type_str, "Image_uboot");
  5762. break;
  5763. case 0x10000002:
  5764. sprintf(type_str, "Image_dtb");
  5765. break;
  5766. case 0x10000003:
  5767. sprintf(type_str, "Image_zImage");
  5768. break;
  5769. case 0x10000004:
  5770. sprintf(type_str, "Image_ramdisk");
  5771. break;
  5772. case 0x10000005:
  5773. sprintf(type_str, "Image_Factory");
  5774. break;
  5775. case 0x10000006:
  5776. sprintf(type_str, "Image_Primary");
  5777. break;
  5778. case 0x10000007:
  5779. sprintf(type_str, "Image_CCS_uboot");
  5780. break;
  5781. case 0x10000008:
  5782. sprintf(type_str, "Image_CCS_dtb");
  5783. break;
  5784. case 0x10000009:
  5785. sprintf(type_str, "Image_CCS_zImage");
  5786. break;
  5787. case 0x1000000A:
  5788. sprintf(type_str, "Image_CCS_ramdisk");
  5789. break;
  5790. case 0x1000000B:
  5791. sprintf(type_str, "Image_CAHdeMO");
  5792. break;
  5793. case 0x1000000C:
  5794. sprintf(type_str, "Image_GBT");
  5795. break;
  5796. case 0x1000000D:
  5797. sprintf(type_str, "Image_Relay");
  5798. break;
  5799. case 0x1000000E:
  5800. sprintf(type_str, "Image_Fan");
  5801. break;
  5802. case 0x1000000F:
  5803. sprintf(type_str, "Image_LCM");
  5804. break;
  5805. case 0x10000010:
  5806. sprintf(type_str, "Image_Infy_PFC");
  5807. break;
  5808. case 0x10000011:
  5809. sprintf(type_str, "Image_Infy_DD");
  5810. break;
  5811. case 0x10000014:
  5812. sprintf(type_str, "Image_LED");
  5813. break;
  5814. case 0x10000015:
  5815. sprintf(type_str, "Image_LCM_APK");
  5816. break;
  5817. default:
  5818. sprintf(type_str, "Image_Unknown");
  5819. break;
  5820. }
  5821. }
  5822. char CheckUpdateProcess()
  5823. {
  5824. char _model[32];
  5825. char _type[32];
  5826. bool support = false;
  5827. bool OneImageOnly = false;
  5828. int isPass = true;
  5829. uint8_t retSucc = 0;
  5830. uint8_t retFail = 0;
  5831. byte target = 0;
  5832. char *new_str = NULL;
  5833. unsigned char *ptr = NULL;
  5834. int fd = 0;
  5835. int uartFd = 0;
  5836. unsigned int Type = 0;
  5837. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5838. DIR *d;
  5839. struct dirent *dir;
  5840. char cmdBuf[128];
  5841. int availableMem = 0;
  5842. memset(_model, 0x00, sizeof(_model));
  5843. memset(_type, 0x00, sizeof(_type));
  5844. availableMem = getAvailableMemory() / (1024 * 1024);
  5845. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5846. // If available memory too small, free memory cache first
  5847. if(availableMem < MIN_AVAILABLE_MEMORY)
  5848. {
  5849. LOG_INFO("Available memory less than %d MBytes, free cache & kill ntpd.", MIN_AVAILABLE_MEMORY);
  5850. system("echo 3 > /proc/sys/vm/drop_caches");
  5851. KillNtpd();
  5852. availableMem = getAvailableMemory() / (1024 * 1024);
  5853. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5854. }
  5855. d = opendir("/mnt/");
  5856. if(d)
  5857. {
  5858. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5859. {
  5860. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5861. continue;
  5862. }
  5863. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5864. strcat(new_str, "/mnt/");
  5865. strcat(new_str, dir->d_name);
  5866. fd = open(new_str, O_RDONLY);
  5867. if(fd < 0)
  5868. {
  5869. close(fd);
  5870. free(new_str);
  5871. continue;
  5872. }
  5873. LOG_INFO("%s\n", new_str);
  5874. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5875. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5876. //get the image length
  5877. ImageLen = read(fd, ptr, MaxLen);
  5878. if(ImageLen > 20)
  5879. {
  5880. memcpy(_model, ptr, 16);
  5881. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5882. ImageTypeParsing(Type, _type);
  5883. LOG_INFO("Model: %s, Type: %s", _model, _type);
  5884. }
  5885. free(ptr);
  5886. if (ImageLen > 20)
  5887. {
  5888. support = false;
  5889. availableMem = getAvailableMemory() / (1024 * 1024);
  5890. LOG_INFO("AvailableMemory: %dMByte", availableMem);
  5891. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5892. if(IsModelNameMatch(_model))
  5893. {
  5894. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5895. {
  5896. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5897. WaitAllPCabinetStartUpgrade();
  5898. }
  5899. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5900. {
  5901. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5902. sleep(1);
  5903. }
  5904. switch (Type)
  5905. {
  5906. case 0x10000001:
  5907. case 0x10000002:
  5908. case 0x10000003:
  5909. case 0x10000004:
  5910. case 0x10000005:
  5911. support = true;
  5912. _UpgradeNeedReboot = TRUE;
  5913. LOG_INFO("Upgrade Flash Start...", Type);
  5914. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5915. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5916. break;
  5917. case 0x10000006: // primary mcu
  5918. case 0x1000000D: // relay board
  5919. case 0x1000000E: // fan board
  5920. case 0x10000014: // led board
  5921. support = true;
  5922. target = 0x00;
  5923. if (Type == 0x10000006)
  5924. {
  5925. target = UPGRADE_PRI;
  5926. }
  5927. else if (Type == 0x1000000D)
  5928. {
  5929. target = UPGRADE_RB;
  5930. }
  5931. else if (Type == 0x1000000E)
  5932. {
  5933. target = UPGRADE_FAN;
  5934. }
  5935. else if (Type == 0x10000014)
  5936. {
  5937. target = UPGRADE_LED;
  5938. }
  5939. uartFd = InitComPort(target);
  5940. if(target == UPGRADE_RB)
  5941. {
  5942. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5943. if(isPass == PASS)
  5944. {
  5945. retSucc++;
  5946. }
  5947. else
  5948. {
  5949. retFail++;
  5950. }
  5951. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5952. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  5953. {
  5954. sleep(3);
  5955. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5956. }
  5957. }
  5958. else
  5959. {
  5960. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5961. }
  5962. close(uartFd);
  5963. break;
  5964. }
  5965. if(support)
  5966. {
  5967. if(isPass == PASS)
  5968. {
  5969. retSucc++;
  5970. }
  5971. else
  5972. {
  5973. retFail++;
  5974. }
  5975. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5976. }
  5977. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5978. {
  5979. LOG_INFO("MCabinet upgrade completed");
  5980. WaitAllPCabinetUpgradeCompleted();
  5981. }
  5982. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  5983. {
  5984. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  5985. LOG_INFO("SCabinet upgrade completed");
  5986. WaitMCabinetUpgradeCompleted();
  5987. }
  5988. }
  5989. else
  5990. {
  5991. if(_model[0] == 'D' && (_model[1] == 'D' || _model[1] == 'X'))
  5992. {
  5993. OneImageOnly = true;
  5994. _UpgradeNeedReboot = TRUE;
  5995. LOG_INFO("Start Put Image To Dispenser");
  5996. PutImageToAllDispenser(new_str, dir->d_name, _model);
  5997. WaitAllDispenserUpgradeCompleted();
  5998. LOG_INFO("All Dispenser Upgrade Completed");
  5999. }
  6000. else
  6001. {
  6002. retFail++;
  6003. LOG_INFO("Model Name Is Not Match: %s", _model);
  6004. }
  6005. }
  6006. }
  6007. else
  6008. {
  6009. retFail++;
  6010. LOG_INFO("Image length is illegal");
  6011. }
  6012. sprintf(cmdBuf, "rm -f %s", new_str);
  6013. system(cmdBuf);
  6014. close(fd);
  6015. free(new_str);
  6016. }
  6017. }
  6018. closedir(d);
  6019. if(retFail != 0)
  6020. {
  6021. return FAIL;
  6022. }
  6023. return PASS;
  6024. }
  6025. void TriggerDispenserConnectionTimeoutSetting(void)
  6026. {
  6027. BOOL trigger = FALSE;
  6028. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6029. {
  6030. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  6031. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  6032. {
  6033. trigger = TRUE;
  6034. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  6035. }
  6036. }
  6037. if(trigger)
  6038. {
  6039. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  6040. }
  6041. }
  6042. void CheckConnectionTimeout(void)
  6043. {
  6044. if(!Is_Ocpp_Task_Alive())
  6045. {
  6046. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6047. }
  6048. else
  6049. {
  6050. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  6051. {
  6052. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  6053. if(_connectionTimeout <= 0)
  6054. {
  6055. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6056. }
  6057. }
  6058. else
  6059. {
  6060. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6061. }
  6062. }
  6063. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  6064. {
  6065. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  6066. TriggerDispenserConnectionTimeoutSetting();
  6067. }
  6068. }
  6069. void CreateTimeoutFork()
  6070. {
  6071. pid_t timeoutPid;
  6072. timeoutPid = fork();
  6073. if (timeoutPid == 0)
  6074. {
  6075. GetClockTime(&_cmdSubPriority_time);
  6076. CheckConnectionTimeout();
  6077. LOG_INFO("Timeout Fork PID = %d", getpid());
  6078. while(true)
  6079. {
  6080. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  6081. {
  6082. CheckConnectionTimeout();
  6083. GetClockTime(&_cmdSubPriority_time);
  6084. }
  6085. sleep(1);
  6086. }
  6087. }
  6088. }
  6089. void CheckFactoryConfigFunction(void)
  6090. {
  6091. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  6092. {
  6093. char Buf[256];
  6094. sprintf(Buf, "cd /root;./FactoryConfig -m");
  6095. system(Buf);
  6096. system("rm -f /Storage/OCPP/OCPPConfiguration");
  6097. system("sync");
  6098. sleep(5);
  6099. system("reboot -f");
  6100. sleep(5);
  6101. system("reboot -f");
  6102. }
  6103. }
  6104. void CheckFwUpdateFunction(void)
  6105. {
  6106. char UpdateResult = PASS;
  6107. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  6108. {
  6109. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6110. UpgradePrepare();
  6111. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6112. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6113. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6114. LOG_INFO("ftp : update start.");
  6115. TryCloseWatchdog();
  6116. //KillTask();
  6117. system("killall Module_PrimaryComm");
  6118. system("killall Module_InternalComm");
  6119. UpdateResult = CheckUpdateProcess();
  6120. if (UpdateResult == PASS)
  6121. {
  6122. LOG_INFO("ftp : update complete.");
  6123. }
  6124. else
  6125. {
  6126. LOG_INFO("ftp : update fail.");
  6127. }
  6128. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  6129. sleep(10);
  6130. if(_UpgradeNeedReboot)
  6131. {
  6132. system("reboot -f");
  6133. }
  6134. else
  6135. {
  6136. KillAllTask();
  6137. system("/usr/bin/run_evse_restart.sh");
  6138. }
  6139. }
  6140. else if(Is_Ocpp_UpdateFirmwareReq())
  6141. {
  6142. Clean_Ocpp_UpdateFirmwareReq();
  6143. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6144. UpgradePrepare();
  6145. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6146. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6147. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6148. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  6149. {
  6150. LOG_INFO("Backend : update start.");
  6151. TryCloseWatchdog();
  6152. Set_Ocpp_UpdateFirmwareStatus("Installing");
  6153. Set_Ocpp_FirmwareStatusNotificationReq();
  6154. //KillTask();
  6155. system("killall Module_PrimaryComm");
  6156. system("killall Module_InternalComm");
  6157. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6158. {
  6159. setChargerMode(_index, MODE_UPDATE);
  6160. }
  6161. UpdateResult = CheckUpdateProcess();
  6162. if (UpdateResult == PASS)
  6163. {
  6164. LOG_INFO("Backend : update complete.");
  6165. Set_Ocpp_UpdateFirmwareStatus("Installed");
  6166. }
  6167. else
  6168. {
  6169. LOG_INFO("Backend : update fail.");
  6170. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  6171. }
  6172. Set_Ocpp_FirmwareStatusNotificationReq();
  6173. sleep(10);
  6174. if(_UpgradeNeedReboot)
  6175. {
  6176. system("reboot -f");
  6177. }
  6178. else
  6179. {
  6180. KillAllTask();
  6181. system("/usr/bin/run_evse_restart.sh");
  6182. }
  6183. }
  6184. }
  6185. }
  6186. void ExecuteWriteFlash(void)
  6187. {
  6188. struct SysConfigData config;
  6189. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  6190. config.SwitchDebugFlag = NO;
  6191. strcpy((char *) config.UserId, "");
  6192. int result = StoreUsrConfigData(&config);
  6193. if(result != 1)
  6194. {
  6195. LOG_INFO("Store User Config NG");
  6196. }
  6197. else
  6198. {
  6199. LOG_INFO("Store User Config OK");
  6200. }
  6201. }
  6202. struct timespec _WiringInfo_time;
  6203. int CheckFlashUpdate(void)
  6204. {
  6205. if(ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged)
  6206. {
  6207. ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged = false;
  6208. LOG_INFO("Flash Config Changed");
  6209. GetClockTime(&_WiringInfo_time);
  6210. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = true;
  6211. }
  6212. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  6213. {
  6214. // wait for 30s to write flash or write flash immediately
  6215. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  6216. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  6217. {
  6218. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  6219. LOG_INFO("Start Writing Flash");
  6220. pid_t writePid = fork();
  6221. if(writePid == 0)
  6222. {
  6223. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  6224. ExecuteWriteFlash();
  6225. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  6226. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  6227. {
  6228. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  6229. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6230. LOG_INFO("Write Flash OK And Need Soft Reset");
  6231. }
  6232. return NO;
  6233. }
  6234. LOG_INFO("Write Flash Conpleted");
  6235. }
  6236. }
  6237. return YES;
  6238. }
  6239. void CheckSoftHardReset(void)
  6240. {
  6241. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  6242. {
  6243. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  6244. LOG_INFO("********** Need Soft Reset **********");
  6245. sleep(5);
  6246. Kill_Ocpp_Service();
  6247. KillAllTask();
  6248. TryCloseWatchdog();
  6249. system("/usr/bin/run_evse_restart.sh");
  6250. }
  6251. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  6252. {
  6253. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  6254. LOG_INFO("********** Need Hard Reset **********");
  6255. sleep(5);
  6256. system("reboot -f");
  6257. }
  6258. }
  6259. struct timespec _Reboot_time;
  6260. void TriggerDispenserHardwareReboot(void)
  6261. {
  6262. BOOL trigger = FALSE;
  6263. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6264. {
  6265. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6266. {
  6267. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  6268. !Is_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT))
  6269. {
  6270. trigger = TRUE;
  6271. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  6272. }
  6273. }
  6274. }
  6275. if(trigger)
  6276. {
  6277. GetClockTime(&_Reboot_time);
  6278. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  6279. }
  6280. }
  6281. BOOL IsDispenserHardwareRebootResponsed(void)
  6282. {
  6283. BOOL response_ok = TRUE;
  6284. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6285. {
  6286. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6287. {
  6288. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  6289. {
  6290. response_ok = FALSE;
  6291. // 10s for reboot request timeout
  6292. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  6293. {
  6294. response_ok = TRUE;
  6295. LOG_INFO("Reboot request timeout");
  6296. }
  6297. }
  6298. }
  6299. }
  6300. return response_ok;
  6301. }
  6302. struct timespec _Reset_time;
  6303. void TriggerDispenserSoftwareReset(void)
  6304. {
  6305. BOOL trigger = FALSE;
  6306. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6307. {
  6308. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6309. {
  6310. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  6311. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6312. {
  6313. trigger = TRUE;
  6314. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  6315. }
  6316. }
  6317. }
  6318. if(trigger)
  6319. {
  6320. GetClockTime(&_Reset_time);
  6321. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  6322. }
  6323. }
  6324. BOOL IsDispenserSoftwareResetResponsed(void)
  6325. {
  6326. BOOL response_ok = TRUE;
  6327. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6328. {
  6329. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6330. {
  6331. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  6332. {
  6333. response_ok = FALSE;
  6334. // 10s for reset request timeout
  6335. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  6336. {
  6337. response_ok = TRUE;
  6338. LOG_INFO("Reset request timeout");
  6339. }
  6340. }
  6341. }
  6342. }
  6343. return response_ok;
  6344. }
  6345. // return time offset
  6346. int TimeOffsetKeyParsing(char *StringItem)
  6347. {
  6348. int offset = 0;
  6349. int hour = 0, minute = 0, sign = 1;
  6350. char *ptrSave, *ptrToken;
  6351. char strSource[501];
  6352. memset(strSource, 0x00, sizeof(strSource));
  6353. strcpy(strSource, StringItem);
  6354. if(strSource[0] != '+' && strSource[0] != '-')
  6355. {
  6356. return offset;
  6357. }
  6358. sign = strSource[0] == '+' ? (1) : (-1);
  6359. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  6360. if(ptrToken != NULL)
  6361. {
  6362. hour = atoi(ptrToken);
  6363. ptrToken = strtok_r(NULL, ":", &ptrSave);
  6364. if(ptrToken != NULL)
  6365. {
  6366. minute = atoi(ptrToken);
  6367. offset = ((hour * 60) + minute) * sign;
  6368. }
  6369. }
  6370. return offset;
  6371. }
  6372. char MyDefaultPriceString[501];
  6373. void DefaultPriceHandler(void)
  6374. {
  6375. char price[501];
  6376. char defaultPrice[512];
  6377. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  6378. Get_Ocpp_DefaultPrice((char *)price);
  6379. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  6380. {
  6381. float ConnectionFee = 0, OccupancyFee = 0;
  6382. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6383. {
  6384. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6385. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6386. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6387. LOG_INFO("******** Customer Audi Default Price *******");
  6388. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  6389. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6390. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  6391. }
  6392. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6393. {
  6394. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6395. ShellPriceParsing(price, defaultPrice);
  6396. LOG_INFO("******* Customer Shell Default Price *******");
  6397. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6398. }
  6399. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6400. {
  6401. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = TccDefaultPriceParsing(price, &ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6402. LOG_INFO("******* Customer Tcc Default Price *******");
  6403. LOG_INFO("*** TimeOfDayPricing: %4.2f", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6404. }
  6405. else
  6406. {
  6407. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6408. strcpy(defaultPrice, price);
  6409. LOG_INFO("********** Standard Default Price **********");
  6410. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6411. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6412. }
  6413. strcpy(MyDefaultPriceString, price);
  6414. }
  6415. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6416. {
  6417. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = GetNowTimePricing(&ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6418. }
  6419. if(ShmChargerInfo->CabinetMiscValue.DefaultPrice != ShmChargerInfo->PriceAndReceiptInfo.CurrentRate)
  6420. {
  6421. ShmChargerInfo->CabinetMiscValue.DefaultPrice = ShmChargerInfo->PriceAndReceiptInfo.CurrentRate;
  6422. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6423. {
  6424. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6425. {
  6426. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  6427. {
  6428. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  6429. }
  6430. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  6431. {
  6432. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  6433. }
  6434. }
  6435. }
  6436. LOG_INFO("********** Trigger Dispenser Default Price Value **********");
  6437. }
  6438. if(strlen(defaultPrice) > 0)
  6439. {
  6440. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  6441. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6442. {
  6443. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6444. {
  6445. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  6446. {
  6447. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  6448. }
  6449. }
  6450. }
  6451. LOG_INFO("********** Trigger Dispenser Default Price Value String **********");
  6452. }
  6453. }
  6454. void UserPriceHandler(void)
  6455. {
  6456. char UserId[32];
  6457. char price[500];
  6458. char userPrice[512];
  6459. memset(userPrice, 0x00, sizeof(userPrice));
  6460. Get_Ocpp_UserPrice(UserId, price);
  6461. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  6462. {
  6463. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  6464. float AccountBalance = 0;
  6465. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6466. {
  6467. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6468. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6469. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6470. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6471. LOG_INFO("********* Customer Audi User Price *********");
  6472. LOG_INFO("********* User Id: [%s]", UserId);
  6473. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  6474. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  6475. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  6476. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  6477. }
  6478. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6479. {
  6480. ShellPriceParsing(price, userPrice);
  6481. LOG_INFO("******** Customer Shell User Price *********");
  6482. LOG_INFO("******** User Id: [%s]", UserId);
  6483. LOG_INFO("******** User Price: [%s]", userPrice);
  6484. }
  6485. else
  6486. {
  6487. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  6488. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6489. strcpy(userPrice, price);
  6490. LOG_INFO("*********** Standard User Price ************");
  6491. LOG_INFO("*********** User Id: [%s]", UserId);
  6492. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  6493. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6494. LOG_INFO("*********** User Price: [%s]", userPrice);
  6495. }
  6496. #if 0
  6497. LOG_INFO("********** User Id: %s **********", UserId);
  6498. LOG_INFO("********** User Price: %s", price);
  6499. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6500. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  6501. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6502. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6503. #endif
  6504. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6505. {
  6506. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  6507. {
  6508. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  6509. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  6510. {
  6511. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  6512. (i + 1), CurrentRate, AccountBalance);
  6513. }
  6514. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  6515. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  6516. if(strlen(userPrice) > 0)
  6517. {
  6518. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  6519. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  6520. }
  6521. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  6522. {
  6523. AnnounceAccountBalance(i, AccountBalance);
  6524. }
  6525. }
  6526. }
  6527. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6528. {
  6529. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  6530. {
  6531. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  6532. {
  6533. if(strlen(userPrice) > 0)
  6534. {
  6535. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  6536. }
  6537. }
  6538. }
  6539. }
  6540. Clean_Ocpp_UserPrice();
  6541. }
  6542. }
  6543. void RunningCostParsing(int gunIndex, char *strCost)
  6544. {
  6545. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  6546. float AccountBalance = 0;
  6547. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6548. {
  6549. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6550. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6551. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6552. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6553. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6554. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  6555. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  6556. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  6557. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  6558. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6559. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  6560. }
  6561. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6562. {
  6563. TotalCost = ShellRunningCostParsing(strCost);
  6564. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  6565. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  6566. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6567. }
  6568. else
  6569. {
  6570. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6571. TotalCost = EnergyCost;
  6572. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6573. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  6574. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  6575. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6576. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  6577. }
  6578. #if 0
  6579. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  6580. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6581. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6582. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6583. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6584. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6585. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6586. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  6587. {
  6588. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  6589. (gunIndex + 1), TotalCost, AccountBalance);
  6590. }
  6591. #endif
  6592. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6593. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6594. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6595. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6596. }
  6597. void RunningCostHandler(void)
  6598. {
  6599. char strCost[256];
  6600. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6601. {
  6602. Get_Ocpp_RunningCost(i, strCost);
  6603. if(strlen((char *)strCost) > 0)
  6604. {
  6605. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6606. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_CHARGING)
  6607. {
  6608. RunningCostParsing(i, strCost);
  6609. }
  6610. Clean_Ocpp_RunningCost(i);
  6611. }
  6612. }
  6613. }
  6614. void EnableConnectorBillRequest(int gunIndex, ChargingBillInfoData *bill)
  6615. {
  6616. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6617. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6618. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = bill->TxId;
  6619. memcpy(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, &bill->FeeInfo, sizeof(FeeInfoData));
  6620. }
  6621. void DisableConnectorBill(int gunIndex)
  6622. {
  6623. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Idle;
  6624. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6625. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = 0;
  6626. memset(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, 0x00, sizeof(FeeInfoData));
  6627. }
  6628. void FinalCostParsing(int txId, char *strCost, ChargingBillInfoData *_bill)
  6629. {
  6630. memset(_bill, 0x00, sizeof(ChargingBillInfoData));
  6631. _bill->TxId = txId;
  6632. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6633. {
  6634. _bill->FeeInfo.ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6635. _bill->FeeInfo.SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6636. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6637. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6638. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6639. }
  6640. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6641. {
  6642. _bill->FeeInfo.TotalCost = ShellFinalCostParsing(strCost);
  6643. GetShellReceiptInfo(strCost, (char *)_bill->Receipt);
  6644. }
  6645. else
  6646. {
  6647. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6648. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6649. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  6650. _bill->FeeInfo.CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  6651. _bill->FeeInfo.RemainAmount = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  6652. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  6653. {
  6654. sprintf(_bill->Receipt, "%s%d",
  6655. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  6656. txId);
  6657. }
  6658. }
  6659. }
  6660. void UpdateCurrentFinalCost(int gunIndex, ChargingBillInfoData *_bill)
  6661. {
  6662. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6663. {
  6664. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  6665. LOG_INFO("********* Connection Fee: [%7.2f]", _bill->FeeInfo.ConnectionFee);
  6666. LOG_INFO("********* Session Fee: [%7.2f]", _bill->FeeInfo.SessionFee);
  6667. LOG_INFO("********* Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6668. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6669. LOG_INFO("********* Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6670. }
  6671. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6672. {
  6673. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  6674. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6675. LOG_INFO("********* Receipt: [%s]", _bill->Receipt);
  6676. }
  6677. else
  6678. {
  6679. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  6680. LOG_INFO("*********** Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6681. LOG_INFO("*********** Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6682. LOG_INFO("*********** Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6683. LOG_INFO("*********** Coupon Point: [%7.2f]", _bill->FeeInfo.CostDiscount);
  6684. LOG_INFO("*********** Remain Amount: [%7.2f]", _bill->FeeInfo.RemainAmount);
  6685. LOG_INFO("*********** Receipt: [%s]", _bill->Receipt);
  6686. }
  6687. #if 0
  6688. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6689. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6690. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6691. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6692. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6693. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6694. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6695. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6696. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6697. {
  6698. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6699. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6700. }
  6701. #endif
  6702. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = _bill->FeeInfo.TotalCost;
  6703. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = _bill->FeeInfo.AccountBalance;
  6704. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = _bill->FeeInfo.CostDiscount;
  6705. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = _bill->FeeInfo.EnergyCost;
  6706. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = _bill->FeeInfo.ParkingFee;
  6707. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = _bill->FeeInfo.RemainAmount;
  6708. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6709. if(strlen(_bill->Receipt) > 0)
  6710. {
  6711. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], _bill->Receipt);
  6712. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6713. }
  6714. DB_Update_FinalCost_Status(localDb, _bill->TxId, BILL_SATAUS_Bypass_OK);
  6715. }
  6716. bool IsBillStatusChanged(int gunIndex)
  6717. {
  6718. return ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus == ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus ?
  6719. false : true;
  6720. }
  6721. void SyncBillStatus(int gunIndex)
  6722. {
  6723. ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6724. }
  6725. void EnableUndisposedBill(int dispenserIndex)
  6726. {
  6727. LOG_INFO("Enable Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6728. _isUndisposedFinalCost[dispenserIndex] = true;
  6729. }
  6730. void FindUndisposedAndTrigger(int dispenserIndex)
  6731. {
  6732. int txId = 0;
  6733. char strCost[256];
  6734. ChargingBillInfoData _bill;
  6735. bool find = false;
  6736. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6737. {
  6738. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6739. txId = DB_Get_Undisposed_FinalCost(localDb, gun, strCost);
  6740. if(txId >= 0)
  6741. {
  6742. LOG_INFO("Find Gun %d Undisposed ChargingBill, txId: [%d]", gun + 1, txId);
  6743. FinalCostParsing(txId, (char *)strCost, &_bill);
  6744. EnableConnectorBillRequest(gun, &_bill);
  6745. find = true;
  6746. break;
  6747. }
  6748. }
  6749. if(!find)
  6750. {
  6751. //LOG_INFO("Dispenser %d Undisposed ChargingBill Not Found", dispenserIndex + 1);
  6752. _isUndisposedFinalCost[dispenserIndex] = false;
  6753. }
  6754. }
  6755. bool IsDispenserBillStatusBusy(int dispenserIndex)
  6756. {
  6757. bool busy = false;
  6758. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6759. {
  6760. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6761. if(ShmChargerInfo->AllBill.GunBill[gun].BillStatus != _BillStatus_Idle)
  6762. {
  6763. busy = true;
  6764. break;
  6765. }
  6766. }
  6767. return busy;
  6768. }
  6769. void DispenserBillRequestProcess(int dispenserIndex)
  6770. {
  6771. if(IsDispenserIdle(dispenserIndex) && !IsDispenserBillStatusBusy(dispenserIndex))
  6772. {
  6773. if(!_canUpdateFinalCost[dispenserIndex])
  6774. {
  6775. _canUpdateFinalCost[dispenserIndex] = true;
  6776. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6777. }
  6778. if(_canUpdateFinalCost[dispenserIndex] && _isUndisposedFinalCost[dispenserIndex])
  6779. {
  6780. if(GetSecTimeoutValue(_DispenserBill_Time[dispenserIndex]) >= FINAL_COST_UPDATE_TIME)
  6781. {
  6782. //LOG_INFO("Try Find Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6783. FindUndisposedAndTrigger(dispenserIndex);
  6784. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6785. }
  6786. }
  6787. }
  6788. else
  6789. {
  6790. _canUpdateFinalCost[dispenserIndex] = false;
  6791. }
  6792. }
  6793. void GunBillRequestProcess(int gunIndex)
  6794. {
  6795. unsigned char _status = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6796. switch(_status)
  6797. {
  6798. case _BillStatus_Idle:
  6799. if(IsBillStatusChanged(gunIndex))
  6800. {
  6801. SyncBillStatus(gunIndex);
  6802. }
  6803. break;
  6804. case _BillStatus_MiscReq:
  6805. if(IsBillStatusChanged(gunIndex))
  6806. {
  6807. Database_INFO("Gun %d ChargingBill MiscRequest", gunIndex + 1);
  6808. SyncBillStatus(gunIndex);
  6809. }
  6810. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_WaitMiscClean;
  6811. Set_Connector_MiscCommand(gunIndex, MISC_CONN_CHARGING_BILL);
  6812. break;
  6813. case _BillStatus_WaitMiscClean:
  6814. if(IsBillStatusChanged(gunIndex))
  6815. {
  6816. SyncBillStatus(gunIndex);
  6817. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6818. }
  6819. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6820. {
  6821. Database_INFO("Gun %d ChargingBill WaitMisc Timeout", gunIndex + 1);
  6822. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6823. }
  6824. break;
  6825. case _BillStatus_MiscClean:
  6826. if(IsBillStatusChanged(gunIndex))
  6827. {
  6828. Database_INFO("Gun %d ChargingBill MiscClean", gunIndex + 1);
  6829. SyncBillStatus(gunIndex);
  6830. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6831. }
  6832. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6833. {
  6834. Database_INFO("Gun %d ChargingBill Wait Dispenser Timeout", gunIndex + 1);
  6835. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt++;
  6836. if(ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt >= MAX_FINAL_COST_MISC_COUNT)
  6837. {
  6838. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6839. }
  6840. else
  6841. {
  6842. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6843. }
  6844. }
  6845. break;
  6846. case _BillStatus_UpdateDone:
  6847. if(IsBillStatusChanged(gunIndex))
  6848. {
  6849. Database_INFO("Gun %d ChargingBill UpdateDone", gunIndex + 1);
  6850. SyncBillStatus(gunIndex);
  6851. }
  6852. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Completed;
  6853. break;
  6854. case _BillStatus_Completed:
  6855. if(IsBillStatusChanged(gunIndex))
  6856. {
  6857. SyncBillStatus(gunIndex);
  6858. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6859. }
  6860. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6861. {
  6862. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_End;
  6863. }
  6864. break;
  6865. case _BillStatus_Fail:
  6866. if(IsBillStatusChanged(gunIndex))
  6867. {
  6868. SyncBillStatus(gunIndex);
  6869. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_FAIL);
  6870. }
  6871. DisableConnectorBill(gunIndex);
  6872. break;
  6873. case _BillStatus_End:
  6874. if(IsBillStatusChanged(gunIndex))
  6875. {
  6876. SyncBillStatus(gunIndex);
  6877. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_OK);
  6878. }
  6879. DisableConnectorBill(gunIndex);
  6880. break;
  6881. default:
  6882. DisableConnectorBill(gunIndex);
  6883. break;
  6884. }
  6885. }
  6886. void FinalCostHandler(void)
  6887. {
  6888. int txId = 0;
  6889. char strCost[256];
  6890. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6891. {
  6892. txId = Get_Ocpp_FinalCostTxId(i);
  6893. Get_Ocpp_FinalCost(i, strCost);
  6894. if(strlen((char *)strCost) > 0)
  6895. {
  6896. DB_Insert_FinalCost(localDb, i, txId, strCost);
  6897. Database_INFO("Insert FinalCost: Gun %d TxId[%d]", i + 1, txId);
  6898. if(ShmChargerInfo->UserTransaction[i].TransactionId == txId)
  6899. {
  6900. ChargingBillInfoData _bill;
  6901. FinalCostParsing(txId, (char *)strCost, &_bill);
  6902. UpdateCurrentFinalCost(i, &_bill);
  6903. }
  6904. else
  6905. {
  6906. EnableUndisposedBill(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex);
  6907. }
  6908. Clean_Ocpp_FinalCost(i);
  6909. }
  6910. }
  6911. if(ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill)
  6912. {
  6913. ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill = false;
  6914. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6915. {
  6916. _isUndisposedFinalCost[i] = true;
  6917. }
  6918. }
  6919. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6920. {
  6921. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6922. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6923. {
  6924. DispenserBillRequestProcess(i);
  6925. }
  6926. else
  6927. {
  6928. _canUpdateFinalCost[i] = false;
  6929. }
  6930. }
  6931. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6932. {
  6933. GunBillRequestProcess(i);
  6934. }
  6935. }
  6936. char MyTimeOffsetString[501];
  6937. void TimeOffsetHandler(void)
  6938. {
  6939. char timeOffset[501];
  6940. Get_Ocpp_TimeOffset((char *)timeOffset);
  6941. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6942. {
  6943. BOOL trigger = FALSE;
  6944. int offset = 0;
  6945. offset = TimeOffsetKeyParsing(timeOffset);
  6946. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  6947. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  6948. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  6949. #if 0
  6950. time_t timep;
  6951. struct tm *tm;
  6952. time(&timep);
  6953. tm = localtime(&timep);
  6954. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  6955. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6956. timep += (offset * 60);
  6957. tm = localtime(&timep);
  6958. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  6959. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6960. #endif
  6961. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6962. {
  6963. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6964. {
  6965. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  6966. {
  6967. trigger = TRUE;
  6968. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  6969. }
  6970. }
  6971. }
  6972. if(trigger)
  6973. {
  6974. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  6975. }
  6976. strcpy(MyTimeOffsetString, timeOffset);
  6977. }
  6978. }
  6979. struct timespec _Cost_Price_time;
  6980. void CheckOcppCostAndPrice(void)
  6981. {
  6982. // 5s for reboot request timeout
  6983. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  6984. {
  6985. GetClockTime(&_Cost_Price_time);
  6986. DefaultPriceHandler();
  6987. UserPriceHandler();
  6988. RunningCostHandler();
  6989. TimeOffsetHandler();
  6990. }
  6991. FinalCostHandler();
  6992. }
  6993. void CheckRemoteStartNoIdRequest(void)
  6994. {
  6995. if(Is_Ocpp_RemoteStartWaitReq())
  6996. {
  6997. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6998. {
  6999. LOG_INFO("Ocpp remote start without connector id is trigger");
  7000. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  7001. AnnounceRemoteStartNoID();
  7002. GetClockTime(&_RemoteStartNoId_Time);
  7003. }
  7004. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7005. {
  7006. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  7007. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  7008. {
  7009. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  7010. }
  7011. }
  7012. }
  7013. else
  7014. {
  7015. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7016. {
  7017. LOG_INFO("Ocpp remote start without connector id is expired");
  7018. }
  7019. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  7020. }
  7021. }
  7022. void CheckOcppPageChangeRequest(void)
  7023. {
  7024. int gunId = 0, pageIndex = 0;
  7025. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  7026. {
  7027. Clean_Ocpp_LcdOverideReq();
  7028. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  7029. AnnounceLineStatusCode(gunId - 1, pageIndex);
  7030. }
  7031. }
  7032. void CheckLcmPageChangeRequest(void)
  7033. {
  7034. bool isTrigger = false;
  7035. CheckRemoteStartNoIdRequest();
  7036. CheckOcppPageChangeRequest();
  7037. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  7038. {
  7039. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  7040. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  7041. {
  7042. isTrigger = false;
  7043. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  7044. {
  7045. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  7046. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  7047. {
  7048. isTrigger = true;
  7049. break;
  7050. }
  7051. }
  7052. if(isTrigger)
  7053. {
  7054. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  7055. }
  7056. else
  7057. {
  7058. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  7059. }
  7060. }
  7061. }
  7062. }
  7063. void CheckStationInfo(void)
  7064. {
  7065. int station_id = 0;
  7066. char station_name[64];
  7067. if(Is_Ocpp_ChargerStationReq())
  7068. {
  7069. memset(station_name, 0x00, sizeof(station_name));
  7070. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  7071. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  7072. ShmChargerInfo->StationInfo.StationID != station_id)
  7073. {
  7074. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  7075. ShmChargerInfo->StationInfo.StationID = station_id;
  7076. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  7077. AnnounceStationInfo();
  7078. }
  7079. Clean_Ocpp_ChargerStationReq();
  7080. }
  7081. }
  7082. void CheckWeatherInfo(void)
  7083. {
  7084. int weatherID = 0;
  7085. float temperature = 0;
  7086. char strWeather[16];
  7087. 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};
  7088. if(Is_Ocpp_WeatherReq())
  7089. {
  7090. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  7091. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  7092. temperature != ShmChargerInfo->StationInfo.Temperature)
  7093. {
  7094. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  7095. ShmChargerInfo->StationInfo.Temperature = temperature;
  7096. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  7097. {
  7098. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  7099. }
  7100. else
  7101. {
  7102. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  7103. }
  7104. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  7105. AnnounceStationInfo();
  7106. }
  7107. Clean_Ocpp_WeatherReq();
  7108. }
  7109. }
  7110. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  7111. void CheckDeductResultRequest(void)
  7112. {
  7113. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7114. char cardNo[21];
  7115. char appNo[10];
  7116. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7117. {
  7118. if(Is_DeductResultReq(i))
  7119. {
  7120. if(!_WriteDeductBusy[i])
  7121. {
  7122. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7123. {
  7124. _WriteDeductBusy[i] = true;
  7125. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  7126. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  7127. //Set_Ocpp_DeductVemData("");
  7128. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  7129. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  7130. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  7131. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  7132. {
  7133. memset(cardNo, 0x00, sizeof(cardNo));
  7134. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  7135. memset(appNo, 0x00, sizeof(appNo));
  7136. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  7137. Set_Ocpp_ReportCreditDeductReq();
  7138. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7139. i + 1,
  7140. ShmChargerInfo->DeductResultReq[i].TransactionId,
  7141. cardNo,
  7142. appNo,
  7143. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  7144. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  7145. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  7146. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  7147. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_PreAuth_Pass)
  7148. {
  7149. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  7150. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  7151. }
  7152. }
  7153. }
  7154. }
  7155. else
  7156. {
  7157. if(!Is_Ocpp_ReportCreditDeductReq())
  7158. {
  7159. Clean_DeductInfo(i);
  7160. _WriteDeductBusy[i] = false;
  7161. }
  7162. }
  7163. }
  7164. else
  7165. {
  7166. _WriteDeductBusy[i] = false;
  7167. }
  7168. }
  7169. }
  7170. bool _WriteReDeductBusy = false;
  7171. void CheckReDeductRequest(void)
  7172. {
  7173. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7174. char cardNo[21];
  7175. char appNo[10];
  7176. if(ShmChargerInfo->ReDeductReq.DeductReq)
  7177. {
  7178. if(!_WriteReDeductBusy)
  7179. {
  7180. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7181. {
  7182. _WriteReDeductBusy = true;
  7183. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  7184. {
  7185. memset(cardNo, 0x00, sizeof(cardNo));
  7186. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  7187. memset(appNo, 0x00, sizeof(appNo));
  7188. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  7189. Set_Ocpp_ReportCreditDeductReq();
  7190. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7191. ShmChargerInfo->ReDeductReq.TransactionId,
  7192. cardNo,
  7193. appNo,
  7194. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  7195. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  7196. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  7197. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  7198. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_PreAuth_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  7199. {
  7200. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  7201. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  7202. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  7203. }
  7204. }
  7205. }
  7206. }
  7207. else
  7208. {
  7209. if(!Is_Ocpp_ReportCreditDeductReq())
  7210. {
  7211. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  7212. _WriteReDeductBusy = false;
  7213. }
  7214. }
  7215. }
  7216. else
  7217. {
  7218. _WriteReDeductBusy = false;
  7219. }
  7220. }
  7221. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  7222. void CheckReaderStatusRequest(void)
  7223. {
  7224. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7225. char cardNo[21];
  7226. char appNo[10];
  7227. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7228. {
  7229. if(Is_ReaderStatusReq(i))
  7230. {
  7231. if(!_WriteReaderStatusBusy[i])
  7232. {
  7233. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7234. {
  7235. _WriteReaderStatusBusy[i] = true;
  7236. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  7237. {
  7238. memset(cardNo, 0x00, sizeof(cardNo));
  7239. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  7240. memset(appNo, 0x00, sizeof(appNo));
  7241. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  7242. Set_Ocpp_TcciReaderStatusReq(i);
  7243. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  7244. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  7245. }
  7246. }
  7247. }
  7248. else
  7249. {
  7250. if(!Is_Ocpp_TcciReaderStatusReq(i))
  7251. {
  7252. Clean_ReaderStatusInfo(i);
  7253. _WriteReaderStatusBusy[i] = false;
  7254. }
  7255. }
  7256. }
  7257. else
  7258. {
  7259. _WriteReaderStatusBusy[i] = false;
  7260. }
  7261. }
  7262. }
  7263. void CheckOccupancyPrice(void)
  7264. {
  7265. float _price = 0;
  7266. if(Is_Ocpp_TcciSetOccupancyPriceReq())
  7267. {
  7268. _price = Get_Ocpp_TcciOccupancyPrice();
  7269. if(ShmChargerInfo->CabinetMiscValue.ParkingPrice != _price)
  7270. {
  7271. ShmChargerInfo->CabinetMiscValue.ParkingPrice = _price;
  7272. LOG_INFO("Set OccupancyPrice: [%6.2f]", ShmChargerInfo->CabinetMiscValue.ParkingPrice);
  7273. AnnounceOccupancyPrice();
  7274. }
  7275. Clean_Ocpp_TcciSetOccupancyPriceReq();
  7276. }
  7277. }
  7278. bool IsParkingButtonStatusChanged(int gunIndex)
  7279. {
  7280. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus ?
  7281. false : true;
  7282. }
  7283. void SyncParkingButtonStatus(int gunIndex)
  7284. {
  7285. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7286. }
  7287. int OccupancyButtonSync(int gunIndex)
  7288. {
  7289. int _pass = FAIL;
  7290. unsigned char _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7291. switch(_status)
  7292. {
  7293. case _BillStatus_Idle:
  7294. if(IsParkingButtonStatusChanged(gunIndex))
  7295. {
  7296. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7297. SyncParkingButtonStatus(gunIndex);
  7298. }
  7299. break;
  7300. case _BillStatus_MiscReq:
  7301. if(IsParkingButtonStatusChanged(gunIndex))
  7302. {
  7303. Occupancy_INFO("Gun %d OccupancyButton [MiscRequest]", gunIndex + 1);
  7304. SyncParkingButtonStatus(gunIndex);
  7305. }
  7306. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_WaitMiscClean;
  7307. Set_Connector_MiscCommand(gunIndex, MISC_CONN_PARKING_STATUS);
  7308. break;
  7309. case _BillStatus_WaitMiscClean:
  7310. if(IsParkingButtonStatusChanged(gunIndex))
  7311. {
  7312. SyncParkingButtonStatus(gunIndex);
  7313. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7314. }
  7315. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7316. {
  7317. Occupancy_INFO("Gun %d OccupancyButton [WaitMisc Timeout]", gunIndex + 1);
  7318. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7319. }
  7320. break;
  7321. case _BillStatus_MiscClean:
  7322. if(IsParkingButtonStatusChanged(gunIndex))
  7323. {
  7324. Occupancy_INFO("Gun %d OccupancyButton [MiscClean]", gunIndex + 1);
  7325. SyncParkingButtonStatus(gunIndex);
  7326. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7327. }
  7328. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7329. {
  7330. LOG_INFO("Gun %d OccupancyButton [Wait Timeout]", gunIndex + 1);
  7331. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7332. }
  7333. break;
  7334. case _BillStatus_UpdateDone:
  7335. if(IsParkingButtonStatusChanged(gunIndex))
  7336. {
  7337. Occupancy_INFO("Gun %d OccupancyButton [UpdateDone]", gunIndex + 1);
  7338. SyncParkingButtonStatus(gunIndex);
  7339. }
  7340. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_Idle;
  7341. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7342. _pass = PASS;
  7343. break;
  7344. default:
  7345. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7346. break;
  7347. }
  7348. return _pass;
  7349. }
  7350. int NotifyOccupancyFeeActRequest(int gunIndex, int action)
  7351. {
  7352. char *str_notify_occupancy_act[] = {STR_NOTIFY_NONE, STR_GET_OCCUPANCY_FEE, STR_CANCEL_OCCUPANCY_DEDUCT, STR_CANCEL_OCCUPANCY_FEE};
  7353. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_None)
  7354. {
  7355. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7356. {
  7357. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7358. Set_Ocpp_TcciOccupancyFeeAct(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill, action);
  7359. _notifyOccupancyFeeAct[gunIndex] = action;
  7360. LOG_INFO("Gun %d Set OccupancyFee Action [%s] Request", gunIndex + 1, str_notify_occupancy_act[action]);
  7361. }
  7362. }
  7363. else
  7364. {
  7365. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7366. {
  7367. if(Is_Ocpp_TcciOccupancyFeeActStatus(gunIndex, "Accepted"))
  7368. {
  7369. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_GetOccupancyFee)
  7370. {
  7371. Get_Ocpp_TcciGetOccupancyFeeResponse(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7372. LOG_INFO("Gun %d Get Occupancy Fee: [%.2f], Duration: [%d], StartTime: [%s]",
  7373. gunIndex + 1,
  7374. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Fee,
  7375. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Duration,
  7376. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.StartTime);
  7377. }
  7378. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelLocalDeduction)
  7379. {
  7380. LOG_INFO("Gun %d Occupancy Deduction Stopped", gunIndex + 1);
  7381. }
  7382. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelOccupancyFee)
  7383. {
  7384. LOG_INFO("Gun %d Occupancy Fee Force Clean", gunIndex + 1);
  7385. }
  7386. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7387. return PASS;
  7388. }
  7389. else
  7390. {
  7391. LOG_INFO("Gun %d OccupancyFee Action [%s] Rejected", gunIndex + 1, str_notify_occupancy_act[action]);
  7392. return FAIL;
  7393. }
  7394. }
  7395. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7396. {
  7397. // shall not reach here
  7398. Clean_Ocpp_TcciNotifyOccupancyFeeActReq(gunIndex);
  7399. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7400. LOG_INFO("Gun %d OccupancyFee Action [%s] Request Timeout", gunIndex + 1, str_notify_occupancy_act[action]);
  7401. return FAIL;
  7402. }
  7403. }
  7404. return WAIT;
  7405. }
  7406. bool IsParkingStatusChanged(int gunIndex)
  7407. {
  7408. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus ?
  7409. false : true;
  7410. }
  7411. void SyncParkingStatus(int gunIndex)
  7412. {
  7413. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7414. }
  7415. bool Is_Occupancy_Cancel(int gunIndex)
  7416. {
  7417. unsigned char dispenser = 0;
  7418. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex;
  7419. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus && !ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq) ||
  7420. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_None ||
  7421. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_Timeout)
  7422. {
  7423. return true;
  7424. }
  7425. return false;
  7426. }
  7427. void OccupancyRequest(int gunIndex)
  7428. {
  7429. int _action = 0;
  7430. bool feeRequest = false;
  7431. bool keepCounting = false;
  7432. int res = FAIL;
  7433. char _sn[37];
  7434. unsigned char _status = 0;
  7435. char *str_action[] = {STR_OCCUPANCY_ACT_NONE, STR_OCCUPANCY_ACT_DISPLAY, STR_OCCUPANCY_ACT_CANCEL, STR_OCCUPANCY_ACT_PAID_OK, STR_OCCUPANCY_ACT_PAID_NG};
  7436. _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7437. if(Is_Ocpp_TcciOccupancyDisplayReq(gunIndex))
  7438. {
  7439. memset(_sn, 0x00, sizeof(_sn));
  7440. _action = Get_Ocpp_TcciOccupancyAction(gunIndex, sizeof(_sn) - 1, _sn);
  7441. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ActionId = _action;
  7442. LOG_INFO("Gun %d OccupancyAction is [%s]", gunIndex + 1, str_action[_action]);
  7443. Clean_Ocpp_TcciOccupancyDisplayReq(gunIndex);
  7444. }
  7445. if(ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest)
  7446. {
  7447. feeRequest = true;
  7448. ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest =false;
  7449. }
  7450. if(Is_OccupancyKeepCountingReq(gunIndex))
  7451. {
  7452. if(_status == _Parking_WaitToPay)
  7453. {
  7454. keepCounting = true;
  7455. }
  7456. else
  7457. {
  7458. Clean_OccupancyKeepCountingReq(gunIndex);
  7459. }
  7460. }
  7461. if(_status != _Parking_NoOccupancy && chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7462. {
  7463. if(!Is_OccupancyFeeForceCancelReq(gunIndex))
  7464. {
  7465. Set_OccupancyFeeForceCancelReq(gunIndex);
  7466. Occupancy_INFO("Gun %d Need Force Cancel OccupancyStatus", gunIndex + 1);
  7467. }
  7468. }
  7469. if(Is_OccupancyFeeForceCancelReq(gunIndex))
  7470. {
  7471. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelOccupancyFee);
  7472. if(res == PASS || res == FAIL)
  7473. {
  7474. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7475. Clean_OccupancyFeeForceCancelReq(gunIndex);
  7476. return;
  7477. }
  7478. else
  7479. {
  7480. // wait response
  7481. }
  7482. }
  7483. switch(_status)
  7484. {
  7485. case _Parking_NoOccupancy:
  7486. if(IsParkingStatusChanged(gunIndex))
  7487. {
  7488. Occupancy_INFO("Gun %d OccupancyStatus [Clean]", gunIndex + 1);
  7489. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex], 0x00, sizeof(OccupancyInfoData));
  7490. SyncParkingStatus(gunIndex);
  7491. }
  7492. if(_action == _ParkingAction_DisplayButton)
  7493. {
  7494. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7495. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_SyncOccupancy;
  7496. }
  7497. break;
  7498. case _Parking_SyncOccupancy:
  7499. if(IsParkingStatusChanged(gunIndex))
  7500. {
  7501. Occupancy_INFO("Gun %d OccupancyStatus [Sync]", gunIndex + 1);
  7502. SyncParkingStatus(gunIndex);
  7503. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7504. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7505. }
  7506. if(_action == _ParkingAction_DisplayButton)
  7507. {
  7508. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7509. }
  7510. else if(_action == _ParkingAction_CancelOccupancy ||
  7511. Is_Occupancy_Cancel(gunIndex))
  7512. {
  7513. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7514. }
  7515. else if(_action == _ParkingAction_PaidOnlineOK)
  7516. {
  7517. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7518. }
  7519. else
  7520. {
  7521. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7522. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7523. {
  7524. if(OccupancyButtonSync(gunIndex) == PASS)
  7525. {
  7526. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7527. }
  7528. }
  7529. }
  7530. break;
  7531. case _Parking_Occupied:
  7532. if(IsParkingStatusChanged(gunIndex))
  7533. {
  7534. Occupancy_INFO("Gun %d OccupancyStatus [Start]", gunIndex + 1);
  7535. SyncParkingStatus(gunIndex);
  7536. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7537. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7538. }
  7539. if(_action == _ParkingAction_DisplayButton)
  7540. {
  7541. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7542. }
  7543. else if(_action == _ParkingAction_CancelOccupancy ||
  7544. Is_Occupancy_Cancel(gunIndex))
  7545. {
  7546. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7547. }
  7548. else if(_action == _ParkingAction_PaidOnlineOK)
  7549. {
  7550. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7551. }
  7552. else if(_action == _ParkingAction_PaidOnlineFail)
  7553. {
  7554. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7555. }
  7556. else
  7557. {
  7558. if(feeRequest)
  7559. {
  7560. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_RequestFee;
  7561. }
  7562. }
  7563. break;
  7564. case _Parking_RequestFee:
  7565. if(IsParkingStatusChanged(gunIndex))
  7566. {
  7567. Occupancy_INFO("Gun %d OccupancyStatus [RequestFee]", gunIndex + 1);
  7568. SyncParkingStatus(gunIndex);
  7569. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7570. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7571. }
  7572. if(_action == _ParkingAction_CancelOccupancy ||
  7573. Is_Occupancy_Cancel(gunIndex))
  7574. {
  7575. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7576. }
  7577. else if(_action == _ParkingAction_PaidOnlineOK)
  7578. {
  7579. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7580. }
  7581. else if(_action == _ParkingAction_PaidOnlineFail)
  7582. {
  7583. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7584. }
  7585. else
  7586. {
  7587. if(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq)
  7588. {
  7589. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7590. }
  7591. else
  7592. {
  7593. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_GetOccupancyFee);
  7594. if(res == PASS)
  7595. {
  7596. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7597. }
  7598. else if(res == FAIL)
  7599. {
  7600. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7601. }
  7602. else
  7603. {
  7604. // wait response
  7605. }
  7606. }
  7607. }
  7608. break;
  7609. case _Parking_WaitToPay:
  7610. if(IsParkingStatusChanged(gunIndex))
  7611. {
  7612. Occupancy_INFO("Gun %d OccupancyStatus [WaitToPay]", gunIndex + 1);
  7613. SyncParkingStatus(gunIndex);
  7614. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].BillReqStatus, 0x00, sizeof(MiscRequestStatus));
  7615. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7616. }
  7617. if(_action == _ParkingAction_CancelOccupancy ||
  7618. Is_Occupancy_Cancel(gunIndex))
  7619. {
  7620. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7621. }
  7622. else if(_action == _ParkingAction_PaidOnlineOK)
  7623. {
  7624. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7625. }
  7626. else if(_action == _ParkingAction_PaidOnlineFail)
  7627. {
  7628. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7629. }
  7630. else if(keepCounting)
  7631. {
  7632. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelLocalDeduction);
  7633. if(res == PASS)
  7634. {
  7635. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7636. Clean_OccupancyKeepCountingReq(gunIndex);
  7637. }
  7638. else if(res == FAIL)
  7639. {
  7640. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7641. Clean_OccupancyKeepCountingReq(gunIndex);
  7642. }
  7643. else
  7644. {
  7645. // wait response
  7646. }
  7647. }
  7648. break;
  7649. case _Parking_PaidOnlineOK:
  7650. if(IsParkingStatusChanged(gunIndex))
  7651. {
  7652. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineOK]", gunIndex + 1);
  7653. SyncParkingStatus(gunIndex);
  7654. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7655. }
  7656. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7657. {
  7658. LOG_INFO("Gun %d Reset OccupancyStatus via PaidOnlineOK", gunIndex + 1);
  7659. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7660. }
  7661. break;
  7662. case _Parking_PaidOnlineFail:
  7663. if(IsParkingStatusChanged(gunIndex))
  7664. {
  7665. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineFail]", gunIndex + 1);
  7666. SyncParkingStatus(gunIndex);
  7667. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7668. }
  7669. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7670. {
  7671. LOG_INFO("Gun %d Return to Occupied via PaidOnlineFail", gunIndex + 1);
  7672. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7673. }
  7674. break;
  7675. default:
  7676. DisableConnectorBill(gunIndex);
  7677. break;
  7678. }
  7679. }
  7680. void CheckOccupancyFee(void)
  7681. {
  7682. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7683. {
  7684. OccupancyRequest(i);
  7685. }
  7686. }
  7687. #define OCCUPANCY_DEDUCT_IDLE 0
  7688. #define OCCUPANCY_DEDUCT_REQUEST 1
  7689. unsigned char _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7690. int _OccupancyReportGunId = 0;
  7691. void CheckOccupancyDeduct(void)
  7692. {
  7693. char cardNo[21];
  7694. char appNo[10];
  7695. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_IDLE)
  7696. {
  7697. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7698. {
  7699. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7700. {
  7701. if(Is_OccupancyDeductReq(i))
  7702. {
  7703. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq &&
  7704. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_WaitToPay)
  7705. {
  7706. ShmChargerInfo->AllBill.OccupancyInfo[i].Status = _Parking_NoOccupancy;
  7707. LOG_INFO("Gun %d Occupancy Concluded!", i + 1);
  7708. break;
  7709. }
  7710. Set_Ocpp_OccupancyDeductResult(&ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyDeduct, YES);
  7711. Set_Ocpp_TcciReportOccupancyDeductReq();
  7712. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_REQUEST;
  7713. _OccupancyReportGunId = i + 1;
  7714. Clean_OccupancyDeductReq(i);
  7715. break;
  7716. }
  7717. }
  7718. if(_OccupancyDeductStatus != OCCUPANCY_DEDUCT_IDLE)
  7719. {
  7720. memset(cardNo, 0x00, sizeof(cardNo));
  7721. memset(appNo, 0x00, sizeof(appNo));
  7722. memcpy(cardNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo));
  7723. memcpy(appNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber));
  7724. LOG_INFO("Gun %d Occupancy Deduct [Request] Sn: [%s], Fee: [%.2f], CreditNo: [%s], ApprovalNo: [%s]",
  7725. _OccupancyReportGunId,
  7726. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Sn,
  7727. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Fee,
  7728. cardNo,
  7729. appNo);
  7730. }
  7731. }
  7732. }
  7733. else
  7734. {
  7735. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7736. {
  7737. if(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status == _Parking_WaitToPay)
  7738. {
  7739. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_NoOccupancy;
  7740. LOG_INFO("Gun %d OccupancyDeduct Request Completed!", _OccupancyReportGunId);
  7741. }
  7742. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7743. }
  7744. }
  7745. }
  7746. void CheckDiagnosticsReq(void)
  7747. {
  7748. bool _DiagnosticsCompleted = true;
  7749. int _DispenserId = 0;
  7750. char _IpAddress[32];
  7751. char cmdBuf[256] = {0};
  7752. if(Is_Ocpp_GetDiagnosticsReq() || ShmChargerInfo->Control.Diagnostics.DiagnosticsType != DIAGNOSTICS_TYPE_NONE ||
  7753. ShmSysConfigAndInfo->SysInfo.isDispenserLog)
  7754. {
  7755. // check dispenser status and set diagnostics request
  7756. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7757. {
  7758. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  7759. {
  7760. _DispenserId = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex + 1;
  7761. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].bits.Request)
  7762. {
  7763. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].CtrlValue = 0;
  7764. sprintf((char*)_IpAddress, "%d.%d.%d.%d",
  7765. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  7766. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  7767. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  7768. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  7769. sprintf(cmdBuf, "/root/Module_Diagnostics %d %s &", _DispenserId, _IpAddress);
  7770. system(cmdBuf);
  7771. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].bits.Request = true;
  7772. }
  7773. if(!_NeedDiagnostics)
  7774. {
  7775. GetClockTime(&_Diagnostics_Time);
  7776. LOG_INFO("Dispenser Need Diagnostics!");
  7777. }
  7778. _NeedDiagnostics = true;
  7779. }
  7780. }
  7781. if(_NeedDiagnostics)
  7782. {
  7783. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7784. {
  7785. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  7786. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  7787. {
  7788. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop)
  7789. {
  7790. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Confirm)
  7791. {
  7792. if(GetSecTimeoutValue(_Diagnostics_Time) >= WAIT_DIAGNOSTICS_CONFIRM)
  7793. {
  7794. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop = true;
  7795. LOG_INFO("Wait Dispenser %d diagnostics confirm timeout!", i + 1);
  7796. }
  7797. _DiagnosticsCompleted = false;
  7798. }
  7799. else if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Completed)
  7800. {
  7801. if(GetSecTimeoutValue(_Diagnostics_Time) >= WAIT_DIAGNOSTICS_TIMEOUT)
  7802. {
  7803. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop = true;
  7804. LOG_INFO("Wait Dispenser %d diagnostics completed timeout!", i + 1);
  7805. }
  7806. _DiagnosticsCompleted = false;
  7807. }
  7808. }
  7809. }
  7810. }
  7811. }
  7812. if(_DiagnosticsCompleted)
  7813. {
  7814. // diagnostics completed, clean ocpp GetDiagnosticsReq
  7815. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7816. {
  7817. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].CtrlValue = 0;
  7818. }
  7819. if(Is_Ocpp_GetDiagnosticsReq())
  7820. {
  7821. Clean_Ocpp_GetDiagnosticsReq();
  7822. }
  7823. if(ShmChargerInfo->Control.Diagnostics.DiagnosticsType != DIAGNOSTICS_TYPE_NONE)
  7824. {
  7825. ShmChargerInfo->Control.Diagnostics.DiagnosticsType = DIAGNOSTICS_TYPE_NONE;
  7826. }
  7827. if(ShmSysConfigAndInfo->SysInfo.isDispenserLog)
  7828. {
  7829. ShmSysConfigAndInfo->SysInfo.isDispenserLog = false;
  7830. }
  7831. LOG_INFO("Dispenser Diagnostics Completed!");
  7832. }
  7833. }
  7834. else
  7835. {
  7836. _NeedDiagnostics = false;
  7837. }
  7838. }
  7839. //===============================================
  7840. // OCPP
  7841. //===============================================
  7842. void CheckOcppStatus()
  7843. {
  7844. if (Is_Ocpp_BootNotification())
  7845. {
  7846. Clean_Ocpp_BootNotification();
  7847. LOG_INFO("*********************OCPP Boot Notification****************************");
  7848. }
  7849. if (Is_Ocpp_ResetReq())
  7850. {
  7851. bool canReset = true;
  7852. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  7853. {
  7854. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7855. {
  7856. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  7857. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  7858. chargingInfo[_index]->SystemStatus != S_MAINTAIN &&
  7859. chargingInfo[_index]->SystemStatus != S_FAULT &&
  7860. !(chargingInfo[_index]->SystemStatus == S_COMPLETE && ShmSysConfigAndInfo->SysInfo.ConnectorInfo[_index].Parameter.bits.PsuReleasable) &&
  7861. !(chargingInfo[_index]->SystemStatus == S_ALARM && ShmSysConfigAndInfo->SysInfo.ConnectorInfo[_index].Parameter.bits.PsuReleasable))
  7862. {
  7863. canReset = false;
  7864. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  7865. //{
  7866. // ChargingTerminalProcess(_index);
  7867. //}
  7868. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7869. {
  7870. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  7871. }
  7872. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7873. {
  7874. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  7875. }
  7876. }
  7877. }
  7878. }
  7879. if(canReset)
  7880. {
  7881. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7882. {
  7883. //trigger dispenser to hardware reboot
  7884. TriggerDispenserHardwareReboot();
  7885. if(IsDispenserHardwareRebootResponsed())
  7886. {
  7887. Clean_Ocpp_ResetReq();
  7888. LOG_INFO("****** Hard Reboot ******");
  7889. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7890. {
  7891. setChargerMode(i, MODE_MAINTAIN);
  7892. }
  7893. sleep(5);
  7894. system("reboot -f");
  7895. }
  7896. }
  7897. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7898. {
  7899. //trigger dispenser to software reset
  7900. TriggerDispenserSoftwareReset();
  7901. if(IsDispenserSoftwareResetResponsed())
  7902. {
  7903. Clean_Ocpp_ResetReq();
  7904. LOG_INFO("****** Soft Reboot ******");
  7905. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7906. {
  7907. setChargerMode(i, MODE_MAINTAIN);
  7908. }
  7909. sleep(5);
  7910. Kill_Ocpp_Service();
  7911. KillAllTask();
  7912. TryCloseWatchdog();
  7913. system("/usr/bin/run_evse_restart.sh");
  7914. }
  7915. }
  7916. }
  7917. }
  7918. CheckOcppCostAndPrice();
  7919. CheckLcmPageChangeRequest();
  7920. CheckStationInfo();
  7921. CheckWeatherInfo();
  7922. CheckDeductResultRequest();
  7923. CheckReDeductRequest();
  7924. CheckReaderStatusRequest();
  7925. CheckOccupancyPrice();
  7926. CheckOccupancyFee();
  7927. CheckOccupancyDeduct();
  7928. CheckDiagnosticsReq();
  7929. }
  7930. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  7931. {
  7932. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  7933. {
  7934. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  7935. {
  7936. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  7937. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  7938. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  7939. }
  7940. }
  7941. }
  7942. void UpdateChargingTimestamp(char *timestamp)
  7943. {
  7944. struct timeb SeqEndTime;
  7945. struct tm *tm;
  7946. ftime(&SeqEndTime);
  7947. SeqEndTime.time = time(NULL);
  7948. tm = localtime(&SeqEndTime.time);
  7949. 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);
  7950. }
  7951. void OcppStartTransaction(byte gunIndex)
  7952. {
  7953. if(_triggerTransaction[gunIndex])
  7954. {
  7955. return;
  7956. }
  7957. // clean deduct info
  7958. Clean_DeductInfo(gunIndex);
  7959. // set deduct user id
  7960. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  7961. // initial start & stop datetime
  7962. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  7963. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  7964. Update_PowerConsumption(gunIndex);
  7965. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7966. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7967. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  7968. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  7969. Clean_Ocpp_StartTransactionConf(gunIndex);
  7970. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  7971. Set_Ocpp_StartTransactionReq(gunIndex);
  7972. // set _triggerTransaction
  7973. _triggerTransaction[gunIndex] = true;
  7974. }
  7975. void OcppStopTransaction(byte gunIndex)
  7976. {
  7977. if(!_triggerTransaction[gunIndex])
  7978. {
  7979. return;
  7980. }
  7981. Update_PowerConsumption(gunIndex);
  7982. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7983. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7984. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  7985. chargingInfo[gunIndex]->Index + 1,
  7986. chargingInfo[gunIndex]->StartUserId,
  7987. chargingInfo[gunIndex]->EvBatterySoc,
  7988. chargingInfo[gunIndex]->PowerConsumption);
  7989. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  7990. Set_Ocpp_StopTransactionReq(gunIndex);
  7991. // clean _triggerTransaction
  7992. _triggerTransaction[gunIndex] = false;
  7993. }
  7994. bool Is_DispenserStop(byte gunIndex)
  7995. {
  7996. bool _isStop = false;
  7997. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7998. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7999. {
  8000. _isStop = true;
  8001. }
  8002. return _isStop;
  8003. }
  8004. void ChkOcppStatus(byte gunIndex)
  8005. {
  8006. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  8007. if(Is_Ocpp_ReserveNowReq(gunIndex))
  8008. {
  8009. Clean_Ocpp_ReserveNowReq(gunIndex);
  8010. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  8011. {
  8012. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  8013. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  8014. setChargerMode(gunIndex, MODE_RESERVATION);
  8015. }
  8016. Set_Ocpp_ReserveNowConf(gunIndex);
  8017. }
  8018. if(Is_Ocpp_CancelReservationReq(gunIndex))
  8019. {
  8020. Clean_Ocpp_CancelReservationReq(gunIndex);
  8021. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  8022. {
  8023. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  8024. chargingInfo[gunIndex]->ReservationId = -1;
  8025. setChargerMode(gunIndex, MODE_IDLE);
  8026. }
  8027. Set_Ocpp_CancelReservationConf(gunIndex);
  8028. }
  8029. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  8030. {
  8031. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  8032. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  8033. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  8034. {
  8035. if (isDb_ready)
  8036. DB_Update_Operactive(localDb, gunIndex, true);
  8037. chargingInfo[gunIndex]->IsAvailable = YES;
  8038. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  8039. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  8040. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  8041. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  8042. {
  8043. setChargerMode(gunIndex, MODE_IDLE);
  8044. }
  8045. }
  8046. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  8047. {
  8048. if (isDb_ready)
  8049. DB_Update_Operactive(localDb, gunIndex, false);
  8050. chargingInfo[gunIndex]->IsAvailable = NO;
  8051. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  8052. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  8053. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  8054. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  8055. {
  8056. setChargerMode(gunIndex, MODE_MAINTAIN);
  8057. }
  8058. }
  8059. }
  8060. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  8061. {
  8062. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  8063. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  8064. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  8065. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  8066. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  8067. {
  8068. // 充電中,需停止充電
  8069. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  8070. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  8071. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  8072. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  8073. }
  8074. Set_Ocpp_UnlockConnectorConf(gunIndex);
  8075. }
  8076. if (Is_Ocpp_RemoteStopReq(gunIndex))
  8077. {
  8078. Clean_Ocpp_RemoteStopReq(gunIndex);
  8079. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  8080. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  8081. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  8082. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  8083. {
  8084. Set_Ocpp_StopReason(gunIndex, "Remote");
  8085. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  8086. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  8087. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  8088. }
  8089. }
  8090. if(Is_Ocpp_StartTransactionConf(gunIndex))
  8091. {
  8092. char acceptId[128] = {0};
  8093. char status[32] = {0};
  8094. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  8095. Get_Ocpp_TransactionId(gunIndex, acceptId);
  8096. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  8097. Set_UserTransactionId(gunIndex, atoi(acceptId));
  8098. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  8099. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  8100. {
  8101. if(Is_Ocpp_StopOnInvalidId_Enable())
  8102. {
  8103. if(Is_Invalid_StartTransactionId(gunIndex))
  8104. {
  8105. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  8106. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  8107. }
  8108. }
  8109. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  8110. gunIndex + 1, acceptId,
  8111. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  8112. }
  8113. Clean_Ocpp_StartTransactionConf(gunIndex);
  8114. }
  8115. }
  8116. bool CheckBackendChargingTimeout(byte gunIndex)
  8117. {
  8118. bool result = false;
  8119. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  8120. {
  8121. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  8122. {
  8123. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  8124. result = true;
  8125. }
  8126. }
  8127. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8128. {
  8129. // 隨插即充電的要看 offline
  8130. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  8131. {
  8132. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  8133. result = true;
  8134. }
  8135. }
  8136. return result;
  8137. }
  8138. bool CheckBackendChargingEnergy(byte gunIndex)
  8139. {
  8140. bool result = false;
  8141. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  8142. {
  8143. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  8144. {
  8145. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  8146. result = true;
  8147. }
  8148. }
  8149. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8150. {
  8151. // 隨插即充電的要看 offline
  8152. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  8153. {
  8154. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  8155. result = true;
  8156. }
  8157. }
  8158. return result;
  8159. }
  8160. bool IsReachConfigMaxChargingSoc(byte gunIndex)
  8161. {
  8162. bool result = false;
  8163. if(ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  8164. {
  8165. if(chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  8166. {
  8167. LOG_INFO("Gun %d MaxChargingSoc: [%d%%] Stop", gunIndex + 1, ShmSysConfigAndInfo->SysConfig.MaxChargingSoc);
  8168. result = true;
  8169. }
  8170. }
  8171. return result;
  8172. }
  8173. bool CheckSessionTargetStop(byte gunIndex)
  8174. {
  8175. bool stop = false;
  8176. struct StructSessionTarget target;
  8177. Get_Ocpp_SessionTarget(gunIndex, &target);
  8178. if(target.targetDuration > 0)
  8179. {
  8180. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  8181. {
  8182. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  8183. gunIndex + 1,
  8184. chargingInfo[gunIndex]->PresentChargedDuration,
  8185. target.targetDuration);
  8186. stop = true;
  8187. }
  8188. }
  8189. if(target.targetSoc > 0)
  8190. {
  8191. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  8192. {
  8193. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  8194. gunIndex + 1,
  8195. chargingInfo[gunIndex]->EvBatterySoc,
  8196. target.targetSoc);
  8197. stop = true;
  8198. }
  8199. }
  8200. if(target.targetEnergy > 0)
  8201. {
  8202. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  8203. {
  8204. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  8205. gunIndex + 1,
  8206. chargingInfo[gunIndex]->PresentChargedEnergy,
  8207. target.targetEnergy);
  8208. stop = true;
  8209. }
  8210. }
  8211. if(stop)
  8212. {
  8213. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  8214. }
  8215. return stop;
  8216. }
  8217. void InformOcppErrOccur(byte codeType)
  8218. {
  8219. char _error[25];
  8220. switch(codeType)
  8221. {
  8222. case 4: strcpy(_error, "InternalError"); break;
  8223. case 6: strcpy(_error, "NoError"); break;
  8224. case 7: strcpy(_error, "OtherError"); break;
  8225. case 13: strcpy(_error, "UnderVoltage"); break;
  8226. case 14: strcpy(_error, "OverVoltage"); break;
  8227. }
  8228. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8229. {
  8230. SetOcppErrorCode(gun_index, _error);
  8231. }
  8232. }
  8233. void SetOcppErrorCode(unsigned char _index, char *errCode)
  8234. {
  8235. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  8236. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  8237. }
  8238. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  8239. {
  8240. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  8241. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  8242. }
  8243. //===============================================
  8244. // SQLite3 related routine
  8245. //===============================================
  8246. int NetworkDB_Open(sqlite3 *db)
  8247. {
  8248. int result = PASS;
  8249. char* errMsg = NULL;
  8250. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  8251. "idx integer primary key AUTOINCREMENT, "
  8252. "occurDatetime text NOT NULL, "
  8253. "isInternet text NOT NULL, "
  8254. "isOcppConnected text NOT NULL, "
  8255. "isEth0Internet text NOT NULL, "
  8256. "isMlan0Internet text NOT NULL, "
  8257. "isPpp0Internet text NOT NULL, "
  8258. "rssiMlan0 text NOT NULL, "
  8259. "rssiPpp0 text NOT NULL"
  8260. ");";
  8261. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  8262. if(sqlite3_open(NETWORK_DB_FILE, &db))
  8263. {
  8264. result = FAIL;
  8265. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  8266. sqlite3_close(db);
  8267. }
  8268. else
  8269. {
  8270. LOG_INFO("Local network status record database open successfully.");
  8271. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8272. {
  8273. result = FAIL;
  8274. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  8275. }
  8276. else
  8277. {
  8278. LOG_INFO("Opened local network status record table successfully");
  8279. }
  8280. sqlite3_close(db);
  8281. }
  8282. return result;
  8283. }
  8284. int DB_Open(sqlite3 *db)
  8285. {
  8286. int result = PASS;
  8287. char* errMsg = NULL;
  8288. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  8289. "idx integer primary key AUTOINCREMENT, "
  8290. "reservationId text, "
  8291. "transactionId text, "
  8292. "startMethod text, "
  8293. "userId text, "
  8294. "dateTimeStart text, "
  8295. "dateTimeStop text,"
  8296. "socStart text, "
  8297. "socStop text, "
  8298. "chargeEnergy text, "
  8299. "stopReason text, "
  8300. "connector INTEGER"
  8301. ");";
  8302. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  8303. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8304. "`IsAvailable` TEXT NOT NULL, "
  8305. "`connector` INTEGER NOT NULL, "
  8306. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  8307. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  8308. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8309. "`occurDatetime` TEXT NOT NULL, "
  8310. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  8311. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  8312. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8313. "`rebootDatetime` TEXT NOT NULL, "
  8314. "`model_name` TEXT, "
  8315. "`serial_number` TEXT"
  8316. "`version` TEXT, unique(rebootDatetime) on conflict replace);";
  8317. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  8318. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8319. "`connector` INTEGER NOT NULL, "
  8320. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8321. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  8322. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8323. "`connector` INTEGER NOT NULL, "
  8324. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8325. char *createFinalCostSql = "CREATE TABLE IF NOT EXISTS `final_cost` ( "
  8326. "idx integer primary key AUTOINCREMENT, "
  8327. "gun text, "
  8328. "transactionId text, "
  8329. "occurDatetime text, "
  8330. "updateDatetime text, "
  8331. "description text, "
  8332. "updateStatus integer"
  8333. ");";
  8334. char *createOcmfRecordSql = "CREATE TABLE IF NOT EXISTS `ocmf_record` ( "
  8335. "idx integer primary key AUTOINCREMENT, "
  8336. "gun text, "
  8337. "transactionId text, "
  8338. "publicKey text, "
  8339. "ocmfInfo text, "
  8340. "updateStatus integer, "
  8341. "occurDatetime text, "
  8342. "updateDatetime text"
  8343. ");";
  8344. if(sqlite3_open(DB_FILE, &db))
  8345. {
  8346. result = FAIL;
  8347. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8348. sqlite3_close(db);
  8349. }
  8350. else
  8351. {
  8352. LOG_INFO( "Local charging record database open successfully.");
  8353. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8354. {
  8355. result = FAIL;
  8356. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  8357. }
  8358. else
  8359. {
  8360. LOG_INFO( "Opened local charging record table successfully");
  8361. }
  8362. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  8363. {
  8364. result = FAIL;
  8365. LOG_ERROR( "Create local config table error message: %s", errMsg);
  8366. }
  8367. else
  8368. {
  8369. LOG_INFO( "Opened local config table successfully");
  8370. }
  8371. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8372. {
  8373. result = FAIL;
  8374. LOG_ERROR( "Create local record table error message: %s", errMsg);
  8375. }
  8376. else
  8377. {
  8378. LOG_INFO( "Opened local record table successfully");
  8379. }
  8380. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  8381. {
  8382. result = FAIL;
  8383. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  8384. }
  8385. else
  8386. {
  8387. LOG_INFO( "Opened reboot record table successfully");
  8388. }
  8389. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8390. {
  8391. result = FAIL;
  8392. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  8393. }
  8394. else
  8395. {
  8396. LOG_INFO( "Opened power consumption record table successfully");
  8397. }
  8398. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8399. {
  8400. result = FAIL;
  8401. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  8402. }
  8403. else
  8404. {
  8405. LOG_INFO( "Opened gun consumption record table successfully");
  8406. }
  8407. if (sqlite3_exec(db, createFinalCostSql, 0, 0, &errMsg) != SQLITE_OK)
  8408. {
  8409. result = FAIL;
  8410. LOG_ERROR( "Create final cost table error message: %s", errMsg);
  8411. }
  8412. else
  8413. {
  8414. LOG_INFO( "Opened final cost table successfully");
  8415. }
  8416. if(sqlite3_exec(db, createOcmfRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8417. {
  8418. result = FAIL;
  8419. LOG_ERROR("Create OCMF record table error message: %s", errMsg);
  8420. }
  8421. else
  8422. {
  8423. LOG_INFO("Opened OCMF record table successfully");
  8424. }
  8425. sqlite3_close(db);
  8426. }
  8427. return result;
  8428. }
  8429. int DB_Insert_Record(sqlite3 *db, int gun_index)
  8430. {
  8431. int result = PASS;
  8432. char* errMsg = NULL;
  8433. char insertSql[1024];
  8434. char transactionId[128];
  8435. char reason[32];
  8436. Get_Ocpp_TransactionId(gun_index, transactionId);
  8437. Get_Ocpp_StopReason(gun_index, reason);
  8438. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, connector) "
  8439. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', %d);",
  8440. chargingInfo[gun_index]->ReservationId,
  8441. transactionId,
  8442. chargingInfo[gun_index]->StartMethod,
  8443. chargingInfo[gun_index]->StartUserId,
  8444. chargingInfo[gun_index]->StartDateTime,
  8445. chargingInfo[gun_index]->StopDateTime,
  8446. chargingInfo[gun_index]->EvBatteryStartSoc,
  8447. chargingInfo[gun_index]->EvBatterySoc,
  8448. chargingInfo[gun_index]->PresentChargedEnergy,
  8449. reason,
  8450. gun_index + 1);
  8451. if(sqlite3_open(DB_FILE, &db))
  8452. {
  8453. result = FAIL;
  8454. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8455. sqlite3_close(db);
  8456. }
  8457. else
  8458. {
  8459. //LOG_INFO( "Local charging record database open successfully.");
  8460. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8461. {
  8462. result = FAIL;
  8463. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  8464. }
  8465. else
  8466. {
  8467. //LOG_INFO("Insert local charging record successfully");
  8468. }
  8469. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  8470. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8471. {
  8472. result = FAIL;
  8473. LOG_ERROR("delete local charging error message: %s", errMsg);
  8474. }
  8475. else
  8476. {
  8477. //LOG_INFO("delete local charging record successfully");
  8478. }
  8479. sqlite3_close(db);
  8480. }
  8481. return result;
  8482. }
  8483. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  8484. {
  8485. uint8_t result = false;
  8486. char* errMsg = NULL;
  8487. char sqlStr[1024];
  8488. srand(time(NULL));
  8489. if(sqlite3_open(DB_FILE, &db))
  8490. {
  8491. result = FAIL;
  8492. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8493. sqlite3_close(db);
  8494. }
  8495. else
  8496. {
  8497. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  8498. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  8499. LOG_INFO("sqlStr= %s", sqlStr);
  8500. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8501. {
  8502. result = FAIL;
  8503. LOG_INFO( "update config error message: %s", errMsg);
  8504. }
  8505. else
  8506. {
  8507. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  8508. }
  8509. sqlite3_close(db);
  8510. }
  8511. return result;
  8512. }
  8513. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  8514. {
  8515. uint8_t result = true;
  8516. char* errMsg = NULL;
  8517. char sqlStr[1024];
  8518. char **rs;
  8519. int rows, cols;
  8520. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  8521. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  8522. if(sqlite3_open(DB_FILE, &db))
  8523. {
  8524. result = FAIL;
  8525. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8526. sqlite3_close(db);
  8527. }
  8528. else
  8529. {
  8530. LOG_INFO( "Local config query database open successfully.");
  8531. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8532. if(rows>0)
  8533. {
  8534. for(int idxRow=1;idxRow<=rows;idxRow++)
  8535. {
  8536. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  8537. {
  8538. result = false;
  8539. }
  8540. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  8541. }
  8542. }
  8543. else
  8544. {
  8545. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  8546. }
  8547. sqlite3_free_table(rs);
  8548. sqlite3_close(db);
  8549. }
  8550. return result;
  8551. }
  8552. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8553. {
  8554. char* errMsg = NULL;
  8555. char sqlStr[1024];
  8556. srand(time(NULL));
  8557. if(sqlite3_open(DB_FILE, &db))
  8558. {
  8559. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8560. sqlite3_close(db);
  8561. }
  8562. else
  8563. {
  8564. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  8565. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8566. {
  8567. LOG_ERROR("update power_consumption error message: %s", errMsg);
  8568. }
  8569. else
  8570. {
  8571. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8572. }
  8573. sqlite3_close(db);
  8574. }
  8575. }
  8576. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8577. {
  8578. char* errMsg = NULL;
  8579. char sqlStr[1024];
  8580. srand(time(NULL));
  8581. if(sqlite3_open(DB_FILE, &db))
  8582. {
  8583. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8584. sqlite3_close(db);
  8585. }
  8586. else
  8587. {
  8588. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  8589. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8590. {
  8591. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  8592. }
  8593. else
  8594. {
  8595. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8596. }
  8597. sqlite3_close(db);
  8598. }
  8599. }
  8600. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  8601. {
  8602. float result = 0;
  8603. char* errMsg = NULL;
  8604. char sqlStr[1024];
  8605. char **rs;
  8606. int rows, cols;
  8607. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  8608. if (sqlite3_open(DB_FILE, &db))
  8609. {
  8610. result = FAIL;
  8611. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8612. sqlite3_close(db);
  8613. }
  8614. else
  8615. {
  8616. //LOG_INFO("Local config query database open successfully.");
  8617. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8618. if (rows > 0)
  8619. {
  8620. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8621. {
  8622. result = atof(rs[(idxRow * cols) + 2]);
  8623. }
  8624. }
  8625. else
  8626. {
  8627. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  8628. }
  8629. sqlite3_free_table(rs);
  8630. sqlite3_close(db);
  8631. }
  8632. return result;
  8633. }
  8634. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  8635. {
  8636. float result = 0;
  8637. char* errMsg = NULL;
  8638. char sqlStr[1024];
  8639. char **rs;
  8640. int rows, cols;
  8641. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  8642. if (sqlite3_open(DB_FILE, &db))
  8643. {
  8644. result = FAIL;
  8645. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8646. sqlite3_close(db);
  8647. }
  8648. else
  8649. {
  8650. //LOG_INFO("Local config query database open successfully.");
  8651. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8652. if (rows > 0)
  8653. {
  8654. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8655. {
  8656. result = atof(rs[(idxRow * cols) + 2]);
  8657. }
  8658. }
  8659. else
  8660. {
  8661. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  8662. result = -1;
  8663. }
  8664. sqlite3_free_table(rs);
  8665. sqlite3_close(db);
  8666. }
  8667. return result;
  8668. }
  8669. float Get_PowerConsumption(uint8_t gun_index)
  8670. {
  8671. float value = 0;
  8672. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  8673. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8674. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  8675. {
  8676. if(!_useGunMeterValue[gun_index])
  8677. {
  8678. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  8679. }
  8680. _useGunMeterValue[gun_index] = true;
  8681. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  8682. }
  8683. else
  8684. {
  8685. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8686. }
  8687. return value;
  8688. }
  8689. void Update_PowerConsumption(uint8_t gun_index)
  8690. {
  8691. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  8692. }
  8693. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description)
  8694. {
  8695. char timeBuf[32];
  8696. int result = PASS;
  8697. char* errMsg = NULL;
  8698. char insertSql[1024];
  8699. getNowDatetime((unsigned char *)timeBuf);
  8700. sprintf(insertSql, "insert into final_cost(gun, transactionId, occurDatetime, updateDatetime, description, updateStatus) "
  8701. "values('%d', '%d', '%s', '%s', '%s', '%d');",
  8702. gun_index + 1,
  8703. txId,
  8704. timeBuf,
  8705. "",
  8706. description,
  8707. BILL_SATAUS_UNDISPOSED);
  8708. if(sqlite3_open(DB_FILE, &db))
  8709. {
  8710. result = FAIL;
  8711. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8712. sqlite3_close(db);
  8713. }
  8714. else
  8715. {
  8716. //LOG_INFO("FinalCost database open successfully.");
  8717. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8718. {
  8719. result = FAIL;
  8720. LOG_ERROR("Insert final_cost error message: %s", errMsg);
  8721. }
  8722. else
  8723. {
  8724. //LOG_INFO("Insert final_cost successfully");
  8725. }
  8726. sprintf(insertSql, "delete from final_cost where idx < (select idx from final_cost order by idx desc limit 1)-2000;");
  8727. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8728. {
  8729. result = FAIL;
  8730. LOG_ERROR("delete final_cost error message: %s", errMsg);
  8731. }
  8732. else
  8733. {
  8734. //LOG_INFO("delete final_cost successfully");
  8735. }
  8736. sqlite3_close(db);
  8737. }
  8738. return result;
  8739. }
  8740. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status)
  8741. {
  8742. char timeBuf[32];
  8743. int result = PASS;
  8744. char* errMsg = NULL;
  8745. char updateSql[1024];
  8746. getNowDatetime((unsigned char *)timeBuf);
  8747. sprintf(updateSql, "update final_cost set updateStatus = %d, updateDatetime = '%s' where transactionId = '%d';",
  8748. status,
  8749. timeBuf,
  8750. txId);
  8751. if(sqlite3_open(DB_FILE, &db))
  8752. {
  8753. result = FAIL;
  8754. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8755. sqlite3_close(db);
  8756. }
  8757. else
  8758. {
  8759. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8760. {
  8761. LOG_ERROR("update final_cost status error message: %s", errMsg);
  8762. result = FAIL;
  8763. }
  8764. else
  8765. {
  8766. //LOG_INFO("update final_cost txId[%d] status %d ok", txId, status);
  8767. }
  8768. sqlite3_close(db);
  8769. }
  8770. return result;
  8771. }
  8772. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString)
  8773. {
  8774. int txId = -1;
  8775. char **rs;
  8776. int rows, cols;
  8777. char* errMsg = NULL;
  8778. char sqlString[1024];
  8779. sprintf(sqlString, "select * from final_cost where updateStatus = -1 and gun = '%d' order by idx asc limit 1;",
  8780. gunIndex + 1);
  8781. if(sqlite3_open(DB_FILE, &db))
  8782. {
  8783. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8784. sqlite3_close(db);
  8785. }
  8786. else
  8787. {
  8788. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8789. if(rows > 0)
  8790. {
  8791. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8792. {
  8793. txId = atoi(rs[(idxRow * cols) + 2]);
  8794. strcpy(costString, rs[(idxRow * cols) + 5]);
  8795. }
  8796. }
  8797. sqlite3_free_table(rs);
  8798. sqlite3_close(db);
  8799. }
  8800. return txId;
  8801. }
  8802. void CheckInProgress(uint8_t gun_index)
  8803. {
  8804. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  8805. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  8806. {
  8807. if(chargingInfo[gun_index]->InProgress == NO)
  8808. {
  8809. LOG_INFO("Gun %d In Progress", gun_index + 1);
  8810. }
  8811. chargingInfo[gun_index]->InProgress = YES;
  8812. }
  8813. else
  8814. {
  8815. if(chargingInfo[gun_index]->InProgress == YES)
  8816. {
  8817. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  8818. }
  8819. chargingInfo[gun_index]->InProgress = NO;
  8820. }
  8821. }
  8822. void CheckDebugMode(uint8_t gun_index)
  8823. {
  8824. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  8825. {
  8826. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  8827. {
  8828. setChargerMode(gun_index, MODE_DEBUG);
  8829. }
  8830. }
  8831. }
  8832. int DB_Reboot_Record(sqlite3 *db)
  8833. {
  8834. int result = PASS;
  8835. char* errMsg = NULL;
  8836. char insertSql[256];
  8837. sprintf(insertSql, "insert into reboot_record(rebootDatetime, model_name, serial_number, version) values(CURRENT_TIMESTAMP, '%s', '%s', '%s_%s');",
  8838. (char *)ShmSysConfigAndInfo->SysConfig.ModelName,
  8839. (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber,
  8840. fwVersion,
  8841. subVersion);
  8842. if(sqlite3_open(DB_FILE, &db))
  8843. {
  8844. result = FAIL;
  8845. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8846. sqlite3_close(db);
  8847. }
  8848. else
  8849. {
  8850. LOG_INFO("Local charging record database open successfully.");
  8851. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8852. {
  8853. result = FAIL;
  8854. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  8855. }
  8856. else
  8857. {
  8858. //LOG_INFO("Insert reboot record successfully");
  8859. }
  8860. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  8861. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8862. {
  8863. result = FAIL;
  8864. LOG_ERROR("delete reboot record error message: %s", errMsg);
  8865. }
  8866. else
  8867. {
  8868. //LOG_INFO("delete reboot record successfully");
  8869. }
  8870. sqlite3_close(db);
  8871. }
  8872. return result;
  8873. }
  8874. int DB_Insert_Ocmf(sqlite3 *db, int gun_index, char *txId, char *ocmfKey, char *ocmfInfo)
  8875. {
  8876. char timeBuf[32];
  8877. int result = PASS;
  8878. char* errMsg = NULL;
  8879. char insertSql[3072];
  8880. getNowDatetime((unsigned char *)timeBuf);
  8881. sprintf(insertSql, "insert into ocmf_record(gun, transactionId, publicKey, ocmfInfo, updateStatus, occurDatetime, updateDatetime) "
  8882. "values('%d', '%s', '%s', '%s', '%d', '%s', '%s');",
  8883. gun_index + 1,
  8884. txId,
  8885. ocmfKey,
  8886. ocmfInfo,
  8887. OCMF_SATAUS_INSERTED,
  8888. timeBuf,
  8889. "");
  8890. if(sqlite3_open(DB_FILE, &db))
  8891. {
  8892. result = FAIL;
  8893. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8894. sqlite3_close(db);
  8895. }
  8896. else
  8897. {
  8898. //LOG_INFO("OCMF database open successfully.");
  8899. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8900. {
  8901. result = FAIL;
  8902. LOG_ERROR("Insert ocmf_record error message: %s", errMsg);
  8903. }
  8904. else
  8905. {
  8906. //LOG_INFO("Insert ocmf_record successfully");
  8907. }
  8908. sprintf(insertSql, "delete from ocmf_record where idx < (select idx from ocmf_record order by idx desc limit 1)-2000;");
  8909. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8910. {
  8911. result = FAIL;
  8912. LOG_ERROR("delete ocmf_record error message: %s", errMsg);
  8913. }
  8914. else
  8915. {
  8916. //LOG_INFO("delete ocmf_record successfully");
  8917. }
  8918. sqlite3_close(db);
  8919. }
  8920. return result;
  8921. }
  8922. int DB_Update_Ocmf_Status(sqlite3 *db, char *txId, int status)
  8923. {
  8924. char timeBuf[32];
  8925. int result = PASS;
  8926. char* errMsg = NULL;
  8927. char updateSql[1024];
  8928. getNowDatetime((unsigned char *)timeBuf);
  8929. sprintf(updateSql, "update ocmf_record set updateStatus = %d, updateDatetime = '%s' where transactionId = '%s';",
  8930. status,
  8931. timeBuf,
  8932. txId);
  8933. if(sqlite3_open(DB_FILE, &db))
  8934. {
  8935. result = FAIL;
  8936. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8937. sqlite3_close(db);
  8938. }
  8939. else
  8940. {
  8941. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8942. {
  8943. LOG_ERROR("update updateStatus status error message: %s", errMsg);
  8944. result = FAIL;
  8945. }
  8946. else
  8947. {
  8948. //LOG_INFO("update updateStatus txId[%s] status %d ok", txId, status);
  8949. }
  8950. sqlite3_close(db);
  8951. }
  8952. return result;
  8953. }
  8954. // return YES if exist undisposed ocmf
  8955. int DB_Get_Undisposed_Ocmf(sqlite3 *db, int *gunIndex, char *txId, char *ocmfKey, char *ocmfInfo)
  8956. {
  8957. char **rs;
  8958. int rows, cols;
  8959. char* errMsg = NULL;
  8960. char sqlString[1024];
  8961. int result = NO;
  8962. sprintf(sqlString, "select * from ocmf_record where updateStatus = 0 order by idx asc limit 1;");
  8963. if(sqlite3_open(DB_FILE, &db))
  8964. {
  8965. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8966. sqlite3_close(db);
  8967. }
  8968. else
  8969. {
  8970. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8971. if(rows > 0)
  8972. {
  8973. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8974. {
  8975. *gunIndex = atoi(rs[(idxRow * cols + 1)]) - 1;
  8976. strncpy(txId, rs[(idxRow * cols) + 2], 36);
  8977. strncpy(ocmfKey, rs[(idxRow * cols) + 3], 200);
  8978. strcpy(ocmfInfo, rs[(idxRow * cols) + 4]);
  8979. }
  8980. result = YES;
  8981. }
  8982. sqlite3_free_table(rs);
  8983. sqlite3_close(db);
  8984. }
  8985. return result;
  8986. }
  8987. void DB_TryAddRebootRecordColumn_Model(sqlite3 *db)
  8988. {
  8989. char **rs;
  8990. int rows, cols;
  8991. char* errMsg = NULL;
  8992. char sqlString[1024];
  8993. int result = 0;
  8994. strcpy(sqlString, "select count(*) from pragma_table_info('reboot_record') where name = 'model_name';");
  8995. if(sqlite3_open(DB_FILE, &db))
  8996. {
  8997. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8998. sqlite3_close(db);
  8999. }
  9000. else
  9001. {
  9002. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9003. if(rows > 0)
  9004. {
  9005. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9006. {
  9007. result = atoi(rs[(idxRow * cols) + 0]);
  9008. }
  9009. }
  9010. sqlite3_free_table(rs);
  9011. if(result == 0)
  9012. {
  9013. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'model_name' TEXT;");
  9014. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9015. {
  9016. result = FAIL;
  9017. LOG_ERROR("reboot_record add column 'model_name' error message: %s", errMsg);
  9018. }
  9019. else
  9020. {
  9021. LOG_INFO("reboot_record ADD COLUMN 'model_name' successfully");
  9022. }
  9023. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'serial_number' TEXT;");
  9024. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9025. {
  9026. result = FAIL;
  9027. LOG_ERROR("reboot_record add column 'serial_number' error message: %s", errMsg);
  9028. }
  9029. else
  9030. {
  9031. LOG_INFO("reboot_record ADD COLUMN 'serial_number' successfully");
  9032. }
  9033. }
  9034. sqlite3_close(db);
  9035. }
  9036. }
  9037. void DB_TryAddRebootRecordColumn_Version(sqlite3 *db)
  9038. {
  9039. char **rs;
  9040. int rows, cols;
  9041. char* errMsg = NULL;
  9042. char sqlString[1024];
  9043. int result = 0;
  9044. strcpy(sqlString, "select count(*) from pragma_table_info('reboot_record') where name = 'version';");
  9045. if(sqlite3_open(DB_FILE, &db))
  9046. {
  9047. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9048. sqlite3_close(db);
  9049. }
  9050. else
  9051. {
  9052. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9053. if(rows > 0)
  9054. {
  9055. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9056. {
  9057. result = atoi(rs[(idxRow * cols) + 0]);
  9058. }
  9059. }
  9060. sqlite3_free_table(rs);
  9061. if(result == 0)
  9062. {
  9063. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'version' TEXT;");
  9064. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9065. {
  9066. result = FAIL;
  9067. LOG_ERROR("reboot_record add column 'version' error message: %s", errMsg);
  9068. }
  9069. else
  9070. {
  9071. LOG_INFO("reboot_record ADD COLUMN 'version' successfully");
  9072. }
  9073. }
  9074. sqlite3_close(db);
  9075. }
  9076. }
  9077. void DB_TryAddChargingRecordColumn(sqlite3 *db)
  9078. {
  9079. char **rs;
  9080. int rows, cols;
  9081. char* errMsg = NULL;
  9082. char sqlString[1024];
  9083. int result = 0;
  9084. strcpy(sqlString, "select count(*) from pragma_table_info('charging_record') where name = 'connector';");
  9085. if(sqlite3_open(DB_FILE, &db))
  9086. {
  9087. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9088. sqlite3_close(db);
  9089. }
  9090. else
  9091. {
  9092. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9093. if(rows > 0)
  9094. {
  9095. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9096. {
  9097. result = atoi(rs[(idxRow * cols) + 0]);
  9098. }
  9099. }
  9100. sqlite3_free_table(rs);
  9101. if(result == 0)
  9102. {
  9103. strcpy(sqlString, "ALTER TABLE 'charging_record' ADD COLUMN 'connector' INTEGER;");
  9104. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9105. {
  9106. result = FAIL;
  9107. LOG_ERROR("charging_record add column connector error message: %s", errMsg);
  9108. }
  9109. else
  9110. {
  9111. LOG_INFO("charging_record ADD COLUMN connector successfully");
  9112. }
  9113. }
  9114. sqlite3_close(db);
  9115. }
  9116. }
  9117. //===============================================
  9118. // Config process
  9119. //===============================================
  9120. void AddPlugInTimes(byte gunIndex)
  9121. {
  9122. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  9123. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  9124. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  9125. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  9126. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  9127. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  9128. }
  9129. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  9130. {
  9131. char cmdBuf[32];
  9132. struct timeb csuTime;
  9133. struct tm *tmCSU;
  9134. ftime(&csuTime);
  9135. tmCSU = localtime(&csuTime.time);
  9136. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  9137. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  9138. tmCSU->tm_sec);
  9139. if (isStart)
  9140. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  9141. else
  9142. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  9143. }
  9144. void zipLogFiles()
  9145. {
  9146. const char* logPath = "/Storage/SystemLog";
  9147. // 獲取目錄
  9148. DIR* pDir = opendir(logPath);
  9149. if (pDir != NULL)
  9150. {
  9151. struct timeb csuTime;
  9152. struct tm *tmCSU;
  9153. ftime(&csuTime);
  9154. tmCSU = localtime(&csuTime.time);
  9155. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  9156. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  9157. // tmCSU->tm_sec);
  9158. // Read items inside the folder
  9159. struct dirent* pEntry = NULL;
  9160. while ((pEntry = readdir(pDir)) != NULL)
  9161. {
  9162. if (strcmp(pEntry->d_name, ".") != 0 &&
  9163. strcmp(pEntry->d_name, "..") != 0 &&
  9164. strncmp(pEntry->d_name, "[", 1) == 0 &&
  9165. strstr(pEntry->d_name, "tar") < 0)
  9166. {
  9167. char yearC[5];
  9168. unsigned short year = 0;
  9169. char monthC[3];
  9170. unsigned short month = 0;
  9171. yearC[4] = '\0';
  9172. strncpy(yearC, pEntry->d_name + 1, 4);
  9173. monthC[2] = '\0';
  9174. strncpy(monthC, pEntry->d_name + 6, 2);
  9175. year = atoi(yearC);
  9176. month = atoi(monthC);
  9177. if (year != 0)
  9178. {
  9179. if (year < tmCSU->tm_year + 1900 ||
  9180. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  9181. {
  9182. LOG_INFO("tar file name : %s", pEntry->d_name);
  9183. char file[256];
  9184. memset(file, 0x00, sizeof(file));
  9185. strcat(file, "tar zcvf ");
  9186. strcat(file, logPath);
  9187. strncat(file, "/", 1);
  9188. strcat(file, pEntry->d_name);
  9189. strcat(file, ".tar");
  9190. strncat(file, " ", 1);
  9191. strcat(file, logPath);
  9192. strncat(file, "/", 1);
  9193. strcat(file, pEntry->d_name);
  9194. LOG_INFO("zip = %s \n", file);
  9195. system(file);
  9196. }
  9197. }
  9198. }
  9199. }
  9200. }
  9201. // Close folder
  9202. closedir(pDir);
  9203. }
  9204. void StopProcessingLoop()
  9205. {
  9206. for (;;)
  9207. {
  9208. CheckFactoryConfigFunction();
  9209. CheckFwUpdateFunction();
  9210. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  9211. {
  9212. ChkPrimaryStatus();
  9213. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  9214. {
  9215. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  9216. KillAllTask();
  9217. sleep(3);
  9218. TryCloseWatchdog();
  9219. system("/usr/bin/run_evse_restart.sh");
  9220. return;
  9221. }
  9222. }
  9223. if(Is_Ocpp_ResetReq())
  9224. {
  9225. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  9226. {
  9227. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  9228. }
  9229. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  9230. {
  9231. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  9232. }
  9233. }
  9234. CheckSoftHardReset();
  9235. sleep(1);
  9236. }
  9237. }
  9238. void CreateWatchdog(void)
  9239. {
  9240. wtdFd = InitWatchDog();
  9241. if(wtdFd < 0)
  9242. {
  9243. LOG_INFO("Watchdog Initial Fail");
  9244. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  9245. }
  9246. else
  9247. {
  9248. LOG_INFO("Watchdog Initial Success");
  9249. }
  9250. }
  9251. void TryCloseWatchdog(void)
  9252. {
  9253. if(wtdFd > 0)
  9254. {
  9255. write(wtdFd, "V", 1);
  9256. close(wtdFd);
  9257. }
  9258. }
  9259. void TryFeedWatchdog(void)
  9260. {
  9261. if(wtdFd > 0)
  9262. {
  9263. write(wtdFd, "a", 1);
  9264. }
  9265. }
  9266. bool IsParallelCabinetIdle(void)
  9267. {
  9268. bool result = true;
  9269. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9270. {
  9271. result = false;
  9272. }
  9273. return result;
  9274. }
  9275. bool IsStandbyEnable(void)
  9276. {
  9277. bool result = true;
  9278. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisableForTcc)
  9279. {
  9280. time_t timep;
  9281. struct tm *tm;
  9282. time(&timep);
  9283. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  9284. tm = localtime(&timep);
  9285. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  9286. {
  9287. if(!_isStandbyEnable)
  9288. {
  9289. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  9290. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  9291. }
  9292. _isStandbyEnable = true;
  9293. result = true;
  9294. }
  9295. else
  9296. {
  9297. if(_isStandbyEnable)
  9298. {
  9299. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  9300. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  9301. }
  9302. _isStandbyEnable = false;
  9303. result = false;
  9304. }
  9305. }
  9306. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisable)
  9307. {
  9308. result = false;
  9309. }
  9310. return result;
  9311. }
  9312. bool IsGunIdle(int gunIndex, int idleType)
  9313. {
  9314. bool idle = true;
  9315. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  9316. chargingInfo[gunIndex]->ConnectorPlugIn &&
  9317. idleType == GUN_IDLE_TYPE_WHOLE)
  9318. {
  9319. idle = false;
  9320. }
  9321. else if (chargingInfo[gunIndex]->SystemStatus != S_IDLE &&
  9322. chargingInfo[gunIndex]->SystemStatus != S_RESERVATION &&
  9323. chargingInfo[gunIndex]->SystemStatus != S_FAULT &&
  9324. chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  9325. {
  9326. idle = false;
  9327. }
  9328. return idle;
  9329. }
  9330. bool IsDispenserIdle(int dispenserIndex)
  9331. {
  9332. bool dispenserIdle = true;
  9333. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  9334. {
  9335. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  9336. if(!IsGunIdle(gun, GUN_IDLE_TYPE_NORMAL))
  9337. {
  9338. dispenserIdle = false;
  9339. break;
  9340. }
  9341. }
  9342. return dispenserIdle;
  9343. }
  9344. bool IsConnectorWholeIdle(void)
  9345. {
  9346. bool result = true;
  9347. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  9348. {
  9349. if(!IsGunIdle(count, GUN_IDLE_TYPE_WHOLE))
  9350. {
  9351. result = false;
  9352. break;
  9353. }
  9354. }
  9355. return result;
  9356. }
  9357. bool IsUpgradeAvailable(void)
  9358. {
  9359. bool result = true;
  9360. for(int count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  9361. {
  9362. if(!IsGunIdle(count, GUN_IDLE_TYPE_NORMAL))
  9363. {
  9364. result = false;
  9365. break;
  9366. }
  9367. }
  9368. return result;
  9369. }
  9370. void ClearAlarmCodeWhenAcOff()
  9371. {
  9372. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  9373. {
  9374. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  9375. }
  9376. }
  9377. //==========================================
  9378. // Check task processing
  9379. //==========================================
  9380. void CheckTask()
  9381. {
  9382. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  9383. {
  9384. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  9385. {
  9386. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  9387. {
  9388. LOG_ERROR("Module_4g not running, restart it.");
  9389. system("/root/Module_4g &");
  9390. }
  9391. }
  9392. }
  9393. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  9394. {
  9395. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  9396. {
  9397. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  9398. {
  9399. LOG_ERROR("Module_Wifi not running, restart it.");
  9400. system("/root/Module_Wifi &");
  9401. }
  9402. }
  9403. }
  9404. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  9405. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  9406. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  9407. {
  9408. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  9409. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  9410. {
  9411. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  9412. Clean_Ocpp_procDogTime();
  9413. Kill_Ocpp_Service();
  9414. }
  9415. if(!Is_Ocpp_Task_Alive())
  9416. {
  9417. LOG_ERROR("OcppBackend not running, restart it.");
  9418. Run_Ocpp_Service();
  9419. }
  9420. }
  9421. else
  9422. {
  9423. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  9424. }
  9425. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  9426. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  9427. {
  9428. if(!Is_MaintainOcpp_Task_Alive())
  9429. {
  9430. LOG_ERROR("Maintain Backend not running, restart it.");
  9431. Run_MaintainOcpp_Service();
  9432. }
  9433. }
  9434. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  9435. {
  9436. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  9437. system("/root/Module_ProduceUtils &");
  9438. }
  9439. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  9440. {
  9441. LOG_ERROR("Module_EvComm not running, restart it.");
  9442. system("/root/Module_EvComm &");
  9443. }
  9444. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  9445. {
  9446. LOG_ERROR("Module_InternalComm not running, restart it.");
  9447. system("/root/Module_InternalComm &");
  9448. }
  9449. if(system("pidof -s Module_Firewall > /dev/null") != 0)
  9450. {
  9451. LOG_ERROR("Module_Firewall not running, restart it.");
  9452. system("/root/Module_Firewall &");
  9453. }
  9454. if(!Is_WebServer_Alive())
  9455. {
  9456. LOG_ERROR("lighttpd not running, restart it.");
  9457. Run_WebServer_Service();
  9458. }
  9459. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9460. {
  9461. if(!Is_LocalPowerSharing_Alive())
  9462. {
  9463. LOG_ERROR("Module_PowerSharing not running, restart it.");
  9464. Run_LocalPowerSharing_Service();
  9465. }
  9466. }
  9467. }
  9468. void InitialDHCP()
  9469. {
  9470. char tmpbuf[256];
  9471. memset(tmpbuf,0,256);
  9472. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  9473. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  9474. system(tmpbuf);
  9475. }
  9476. void InitialDispenserDhcpServerConfig(void)
  9477. {
  9478. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  9479. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  9480. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  9481. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  9482. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  9483. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  9484. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  9485. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  9486. usleep(1000);
  9487. }
  9488. void StartDispenserDhcpServer(void)
  9489. {
  9490. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  9491. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  9492. }
  9493. //==========================================
  9494. // Check Smart Charging Profile
  9495. //==========================================
  9496. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  9497. {
  9498. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  9499. {
  9500. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  9501. {
  9502. Set_Ocpp_ChargingProfileReq(gunIndex);
  9503. }
  9504. }
  9505. }
  9506. int GetStartScheduleTime(unsigned char *time)
  9507. {
  9508. int result = -1;
  9509. struct tm tmScheduleStart;
  9510. struct timeb tbScheduleStart;
  9511. 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))
  9512. {
  9513. tmScheduleStart.tm_year -= 1900;
  9514. tmScheduleStart.tm_mon -= 1;
  9515. tbScheduleStart.time = mktime(&tmScheduleStart);
  9516. tbScheduleStart.millitm = 0;
  9517. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  9518. }
  9519. return result;
  9520. }
  9521. void Ocpp_ChargingProfile_Process(byte _index)
  9522. {
  9523. int _startCount = NO_DEFINE;
  9524. int _maxCount = 0;
  9525. float _limit = 0;
  9526. int _numberPhases = 0;
  9527. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  9528. {
  9529. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  9530. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  9531. if(_startCount < _maxCount)
  9532. {
  9533. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9534. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  9535. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9536. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  9537. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  9538. {
  9539. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  9540. }
  9541. else
  9542. {
  9543. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9544. }
  9545. }
  9546. else
  9547. {
  9548. chargingInfo[_index]->ChargingProfilePower = -1;
  9549. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9550. }
  9551. }
  9552. else
  9553. {
  9554. chargingInfo[_index]->ChargingProfilePower = -1;
  9555. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9556. }
  9557. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  9558. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  9559. {
  9560. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9561. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  9562. _index + 1, _startCount, _limit);
  9563. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  9564. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  9565. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  9566. }
  9567. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  9568. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  9569. }
  9570. void Ocpp_MaxChargingProfile_Process(void)
  9571. {
  9572. int _startCount = NO_DEFINE;
  9573. int _maxCount = 0;
  9574. float _limit = 0;
  9575. int _numberPhases = 0;
  9576. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  9577. {
  9578. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  9579. _startCount = Get_Ocpp_MaxProfileStartIndex();
  9580. if(_startCount < _maxCount)
  9581. {
  9582. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9583. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  9584. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9585. }
  9586. else
  9587. {
  9588. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9589. }
  9590. }
  9591. else
  9592. {
  9593. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9594. }
  9595. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  9596. {
  9597. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9598. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  9599. _startCount, _limit);
  9600. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  9601. }
  9602. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  9603. }
  9604. void CheckSmartChargeProfile(byte _index)
  9605. {
  9606. if(Is_Ocpp_ChargingProfileConf(_index))
  9607. {
  9608. Clean_Ocpp_ChargingProfileConf(_index);
  9609. Ocpp_MaxChargingProfile_Process();
  9610. Ocpp_ChargingProfile_Process(_index);
  9611. }
  9612. }
  9613. void GetLocalPowerLimit(byte gunIndex)
  9614. {
  9615. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9616. {
  9617. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  9618. {
  9619. float _localLimitPower = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gunIndex] * AC_OUTPUT_VOL;
  9620. if(chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 && (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9621. {
  9622. chargingInfo[gunIndex]->LocalPowerLimitCurrent = _localLimitPower / chargingInfo[gunIndex]->PresentChargingVoltage;
  9623. }
  9624. else
  9625. {
  9626. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9627. }
  9628. }
  9629. else
  9630. {
  9631. chargingInfo[gunIndex]->LocalPowerLimitCurrent = 0;
  9632. }
  9633. }
  9634. else
  9635. {
  9636. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9637. }
  9638. }
  9639. void TheEndCharging(byte gun_index)
  9640. {
  9641. chargingInfo[gun_index]->isRemoteStart = NO;
  9642. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  9643. {
  9644. char receiptString[512];
  9645. sprintf(receiptString, "%s%d",
  9646. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  9647. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  9648. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  9649. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  9650. }
  9651. ChangeStartOrStopDateTime(NO, gun_index);
  9652. DB_Insert_Record(localDb, gun_index);
  9653. }
  9654. void UpdateErrorCodeToOcpp(byte index)
  9655. {
  9656. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  9657. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  9658. {
  9659. SetOcppErrorCode(index, "InternalError");
  9660. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  9661. }
  9662. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  9663. {
  9664. SetOcppErrorCode(index, "OtherError");
  9665. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  9666. }
  9667. }
  9668. void CheckMiscCommandRequirement(void)
  9669. {
  9670. BOOL NeedAnnouncement = FALSE;
  9671. unsigned char connector = 0;
  9672. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  9673. {
  9674. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9675. {
  9676. NeedAnnouncement = FALSE;
  9677. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  9678. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  9679. {
  9680. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  9681. {
  9682. NeedAnnouncement = TRUE;
  9683. }
  9684. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  9685. {
  9686. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  9687. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  9688. {
  9689. NeedAnnouncement = TRUE;
  9690. }
  9691. }
  9692. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  9693. {
  9694. if(NeedAnnouncement)
  9695. {
  9696. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  9697. }
  9698. else
  9699. {
  9700. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  9701. }
  9702. }
  9703. }
  9704. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  9705. }
  9706. }
  9707. }
  9708. void CheckDispenserVersionUpdateRequirement(void)
  9709. {
  9710. }
  9711. bool IsReadyParalleling(void)
  9712. {
  9713. bool ready = true;
  9714. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  9715. {
  9716. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  9717. {
  9718. ready = false;
  9719. break;
  9720. }
  9721. }
  9722. return ready;
  9723. }
  9724. void SetParallelingStart(void)
  9725. {
  9726. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9727. {
  9728. if(chargingInfo[i]->SystemStatus == S_IDLE)
  9729. {
  9730. setChargerMode(i, MODE_MAINTAIN);
  9731. }
  9732. }
  9733. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  9734. }
  9735. void SetParallelingDone(void)
  9736. {
  9737. unsigned short totalPow = 0;
  9738. totalPow = GetTotalParallelCabinetPower();
  9739. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9740. {
  9741. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9742. }
  9743. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9744. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  9745. }
  9746. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  9747. {
  9748. unsigned char relay = 0;
  9749. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9750. {
  9751. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9752. {
  9753. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9754. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9755. {
  9756. relay = OnOff == YES ? YES : NO;
  9757. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  9758. {
  9759. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  9760. }
  9761. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  9762. }
  9763. }
  9764. }
  9765. }
  9766. void SetParallelCabinetAcContactor(unsigned char OnOff)
  9767. {
  9768. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9769. {
  9770. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9771. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9772. {
  9773. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  9774. }
  9775. }
  9776. }
  9777. unsigned short GetTotalParallelCabinetPower(void)
  9778. {
  9779. unsigned short power = 0, slavePow = 0;
  9780. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9781. {
  9782. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  9783. {
  9784. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  9785. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  9786. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  9787. {
  9788. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  9789. power += slavePow;
  9790. }
  9791. }
  9792. }
  9793. return power;
  9794. }
  9795. unsigned char preParallelStatus = 0;
  9796. void MCabinetParallelingProcess(void)
  9797. {
  9798. bool PsuReInitial = false;
  9799. unsigned char status = 0;
  9800. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9801. {
  9802. if(IsReadyParalleling())
  9803. {
  9804. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9805. {
  9806. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  9807. {
  9808. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  9809. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  9810. PsuReInitial = true;
  9811. }
  9812. }
  9813. if(PsuReInitial)
  9814. {
  9815. SetParallelingStart();
  9816. }
  9817. }
  9818. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  9819. switch(status)
  9820. {
  9821. case _Parallel_None:
  9822. if(preParallelStatus != status)
  9823. {
  9824. LOG_INFO("MCabinet Parallel Status [None]");
  9825. GetClockTime(&_Parallel_Time);
  9826. }
  9827. unsigned short totalPow = 0;
  9828. totalPow = GetTotalParallelCabinetPower();
  9829. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9830. {
  9831. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9832. }
  9833. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9834. break;
  9835. case _Parallel_Wait:
  9836. if(preParallelStatus != status)
  9837. {
  9838. LOG_INFO("MCabinet Parallel Status [Wait]");
  9839. SetAcContactor(OFF);
  9840. SetParallelCabinetAcContactor(OFF);
  9841. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9842. {
  9843. SetPCabinetOutputRelay(i, OFF);
  9844. }
  9845. GetClockTime(&_Parallel_Time);
  9846. }
  9847. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9848. {
  9849. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  9850. }
  9851. break;
  9852. case _Parallel_Working:
  9853. if(preParallelStatus != status)
  9854. {
  9855. LOG_INFO("MCabinet Parallel Status [Working]");
  9856. SetAcContactor(ON);
  9857. SetParallelCabinetAcContactor(ON);
  9858. GetClockTime(&_Parallel_Time);
  9859. }
  9860. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9861. {
  9862. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  9863. }
  9864. break;
  9865. case _Parallel_Done:
  9866. if(preParallelStatus != status)
  9867. {
  9868. LOG_INFO("MCabinet Parallel Status [Done]");
  9869. GetClockTime(&_Parallel_Time);
  9870. }
  9871. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9872. {
  9873. SetParallelingDone();
  9874. }
  9875. break;
  9876. }
  9877. preParallelStatus = status;
  9878. }
  9879. }
  9880. void SCabinetControllProcess(void)
  9881. {
  9882. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9883. {
  9884. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  9885. {
  9886. SetAcContactor(ON);
  9887. }
  9888. else
  9889. {
  9890. SetAcContactor(OFF);
  9891. }
  9892. unsigned short _parallelRelay = 0;
  9893. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9894. {
  9895. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  9896. }
  9897. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  9898. {
  9899. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  9900. }
  9901. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  9902. }
  9903. }
  9904. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  9905. {
  9906. unsigned char dispenser = 0;
  9907. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  9908. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  9909. {
  9910. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  9911. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  9912. }
  9913. }
  9914. bool IsPsuResourceAvailable(unsigned char connector)
  9915. {
  9916. bool _available = true;
  9917. if(ShmPsuGrouping->GroupCollection[connector].Role == _GROLE_NO_RESOURCE)
  9918. {
  9919. _available = false;
  9920. }
  9921. return _available;
  9922. }
  9923. int ConnectorRequestToCharging(unsigned char connector)
  9924. {
  9925. unsigned char role = 0;
  9926. int result = WAIT;
  9927. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9928. switch(role)
  9929. {
  9930. case _GROLE_IDLE:
  9931. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  9932. {
  9933. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  9934. }
  9935. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  9936. break;
  9937. case _GROLE_MASTER:
  9938. result = PASS;
  9939. break;
  9940. case _GROLE_SLAVE:
  9941. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  9942. {
  9943. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  9944. }
  9945. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  9946. break;
  9947. case _GROLE_REQUEST_TO_CHARGING:
  9948. result = PASS;
  9949. break;
  9950. case _GROLE_DUMMY_MASTER:
  9951. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.DummyMasterRequest)
  9952. {
  9953. LOG_INFO("Gun %d Wait For Dummy Master", connector + 1);
  9954. }
  9955. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.DummyMasterRequest = true;
  9956. break;
  9957. case _GROLE_DUMMY_FAULT:
  9958. result = FAIL;
  9959. break;
  9960. default:
  9961. break;
  9962. }
  9963. return result;
  9964. }
  9965. bool IsConnectorGroupingCompleted(unsigned char connector)
  9966. {
  9967. unsigned char role = 0;
  9968. bool completed = false;
  9969. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9970. switch(role)
  9971. {
  9972. case _GROLE_MASTER:
  9973. completed = true;
  9974. break;
  9975. default:
  9976. break;
  9977. }
  9978. return completed;
  9979. }
  9980. void SelfTestFailReason(void)
  9981. {
  9982. char reason[50];
  9983. memset(reason, 0x00, sizeof(reason));
  9984. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  9985. {
  9986. StatusCodeCompose(reason, "042251");
  9987. }
  9988. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  9989. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  9990. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9991. {
  9992. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  9993. {
  9994. StatusCodeCompose(reason, "042280");
  9995. }
  9996. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9997. {
  9998. StatusCodeCompose(reason, "042281");
  9999. }
  10000. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  10001. {
  10002. StatusCodeCompose(reason, "042282");
  10003. }
  10004. }
  10005. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  10006. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  10007. {
  10008. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  10009. {
  10010. StatusCodeCompose(reason, "042285");
  10011. }
  10012. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  10013. {
  10014. StatusCodeCompose(reason, "042286");
  10015. }
  10016. }
  10017. else
  10018. {
  10019. StatusCodeCompose(reason, "Self Test Fail");
  10020. }
  10021. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10022. {
  10023. SetOcppErrorCode(gun_index, "InternalError");
  10024. SetOcppVendorErrorCode(gun_index, reason);
  10025. setChargerMode(gun_index, MODE_MAINTAIN);
  10026. }
  10027. }
  10028. bool IsAvailableBackToIdle(int gun_index)
  10029. {
  10030. if(_plugInHold[gun_index] == false &&
  10031. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  10032. {
  10033. _plugInHold[gun_index] = true;
  10034. GetClockTime(&_PlugInHold_Time[gun_index]);
  10035. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  10036. }
  10037. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  10038. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  10039. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  10040. isResetStopChargeFlag(gun_index) == YES)
  10041. {
  10042. if(!_plugInHold[gun_index])
  10043. {
  10044. _plugInHold[gun_index] = true;
  10045. GetClockTime(&_PlugInHold_Time[gun_index]);
  10046. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  10047. }
  10048. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  10049. {
  10050. return true;
  10051. }
  10052. }
  10053. return false;
  10054. }
  10055. void CheckGunAvailable(void)
  10056. {
  10057. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  10058. {
  10059. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  10060. {
  10061. ShmChargerInfo->Control.GunAvailable[i] = IsPsuResourceAvailable(i) ? YES : NO;
  10062. }
  10063. }
  10064. }
  10065. int presentChargedEnergyClear(unsigned char gun_index)
  10066. {
  10067. int result = FAIL;
  10068. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  10069. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  10070. result = PASS;
  10071. return result;
  10072. }
  10073. float presentChargedEnergyTotal(unsigned char gun_index)
  10074. {
  10075. float result = 0.0f;
  10076. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  10077. {
  10078. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  10079. }
  10080. return result;
  10081. }
  10082. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  10083. {
  10084. int result = FAIL;
  10085. time_t CurrentTime;
  10086. struct tm *tm;
  10087. CurrentTime = time(NULL);
  10088. tm=localtime(&CurrentTime);
  10089. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  10090. return result;
  10091. }
  10092. void ChargedEnergySummation(unsigned char gun_index)
  10093. {
  10094. unsigned long time = 0;
  10095. time = GetSecTimeoutValue(_ChargedEnergy_Time[gun_index]);
  10096. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  10097. {
  10098. chargingInfo[gun_index]->PresentChargedDuration++;
  10099. float changedEnergy = 0;
  10100. float _meterEnergy = 0;
  10101. float _calChangingEnergy = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  10102. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  10103. {
  10104. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= _lsatGunMeterValue[gun_index])
  10105. {
  10106. _meterEnergy = ShmChargerInfo->MeterValue[gun_index].GunConsumption - _lsatGunMeterValue[gun_index];
  10107. }
  10108. else
  10109. {
  10110. _meterEnergy = 0;
  10111. }
  10112. changedEnergy = _meterEnergy;
  10113. _lsatGunMeterValue[gun_index] = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  10114. }
  10115. else
  10116. {
  10117. changedEnergy = _calChangingEnergy;
  10118. }
  10119. if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  10120. {
  10121. chargingInfo[gun_index]->ChargingFee += changedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  10122. }
  10123. presentChargedEnergyUpdate(gun_index, changedEnergy);
  10124. chargingInfo[gun_index]->PresentChargedEnergy += changedEnergy;
  10125. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changedEnergy;
  10126. }
  10127. }
  10128. void ChkConnectorAction(void)
  10129. {
  10130. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  10131. {
  10132. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  10133. {
  10134. if(ShmChargerInfo->AllBill.OccupancyInfo[i].Status != _Parking_WaitToPay)
  10135. {
  10136. LOG_INFO("Gun %d Charging Canceled", i + 1);
  10137. Clean_Ocpp_TcciSerialNo(i);
  10138. }
  10139. else
  10140. {
  10141. LOG_INFO("Gun %d Occupancy Keep Counting", i + 1);
  10142. Set_OccupancyKeepCountingReq(i);
  10143. }
  10144. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  10145. }
  10146. }
  10147. }
  10148. void Update_DispenserAvailability(int dispenserIndex)
  10149. {
  10150. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  10151. {
  10152. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  10153. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  10154. }
  10155. }
  10156. void InitialDataBaseValue(void)
  10157. {
  10158. // Local DB
  10159. if(DB_Open(localDb) != PASS)
  10160. {
  10161. LOG_INFO("DB_Open fail.");
  10162. isDb_ready = false;
  10163. }
  10164. else
  10165. {
  10166. isDb_ready = true;
  10167. DB_TryAddRebootRecordColumn_Model(localDb);
  10168. DB_TryAddRebootRecordColumn_Version(localDb);
  10169. DB_TryAddChargingRecordColumn(localDb);
  10170. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10171. {
  10172. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  10173. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  10174. }
  10175. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10176. {
  10177. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  10178. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10179. }
  10180. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10181. {
  10182. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  10183. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10184. }
  10185. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10186. {
  10187. Update_PowerConsumption(_index);
  10188. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  10189. }
  10190. DB_Reboot_Record(localDb);
  10191. }
  10192. if (NetworkDB_Open(networkDb) != PASS)
  10193. {
  10194. LOG_ERROR("NetworkDB_Open fail.");
  10195. }
  10196. }
  10197. float CalculateMaxOutputRatio(int gunIndex)
  10198. {
  10199. float ratio = 0;
  10200. int inUsingCnt = 0;
  10201. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  10202. {
  10203. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  10204. {
  10205. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  10206. }
  10207. }
  10208. if(inUsingCnt != 0)
  10209. {
  10210. ratio = ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].Role == _GROLE_MASTER ?
  10211. ((float)ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].GunPsuQuantity / inUsingCnt) : 0;
  10212. }
  10213. ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio = ratio;
  10214. return ratio;
  10215. }
  10216. // return unit: 0.1kW
  10217. unsigned short GetModelNameLimitPower(int gunIndex)
  10218. {
  10219. unsigned short limitPower = 0;
  10220. float fPower = 0;
  10221. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10222. ShmChargerInfo->Control.ChargerRatingPower != 0)
  10223. {
  10224. fPower = (float)ShmChargerInfo->Control.ChargerRatingPower / 10;
  10225. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  10226. {
  10227. fPower += (float)ShmChargerInfo->ParallelCabinet.TotalParallelPower / 10;
  10228. }
  10229. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10230. limitPower = (unsigned short)(fPower * 10);
  10231. }
  10232. return limitPower;
  10233. }
  10234. void GetWebPageConfigPowerLimit(int gunIndex)
  10235. {
  10236. unsigned short limitPower = 0;
  10237. float fPower = 0;
  10238. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10239. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  10240. {
  10241. fPower = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
  10242. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10243. limitPower = (unsigned short)(fPower * 10);
  10244. }
  10245. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower = limitPower;
  10246. }
  10247. void GetWebPageConfigCurrentLimit(int gunIndex)
  10248. {
  10249. unsigned short limitCurrent = 0;
  10250. float fCurrent = 0;
  10251. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10252. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  10253. {
  10254. fCurrent = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
  10255. fCurrent = fCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10256. limitCurrent = (unsigned short)(fCurrent * 10);
  10257. }
  10258. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent = limitCurrent;
  10259. }
  10260. void GetOffLineMaxChargingPowerLimit(int gunIndex)
  10261. {
  10262. unsigned short limitPower = 0;
  10263. float fPower = 0;
  10264. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10265. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower != 0 &&
  10266. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES)
  10267. {
  10268. fPower = ShmChargerInfo->OutputLimit.Offline_MaxChargingPower / 1000;
  10269. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10270. limitPower = (unsigned short)(fPower * 10);
  10271. }
  10272. ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] = limitPower * 100;
  10273. }
  10274. void GetMaxChargingProfileLimit(int gunIndex)
  10275. {
  10276. unsigned short limitPower = 0, limitCurrent = 0;
  10277. float fPower = 0;
  10278. // max charging profile
  10279. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10280. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  10281. {
  10282. fPower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000;
  10283. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10284. limitPower = (unsigned short)(fPower * 10);
  10285. if (chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 &&
  10286. (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  10287. {
  10288. limitCurrent = (limitPower * 100 / chargingInfo[gunIndex]->PresentChargingVoltage) * 10;
  10289. }
  10290. }
  10291. ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] = limitPower * 100;
  10292. ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] = limitCurrent;
  10293. }
  10294. void GetMaxChargingLimit(int gunIndex)
  10295. {
  10296. GetWebPageConfigPowerLimit(gunIndex);
  10297. GetWebPageConfigCurrentLimit(gunIndex);
  10298. GetOffLineMaxChargingPowerLimit(gunIndex);
  10299. GetMaxChargingProfileLimit(gunIndex);
  10300. }
  10301. void GetGunOtpDerating(int gunIndex)
  10302. {
  10303. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp.isNeedDerating &&
  10304. chargingInfo[gunIndex]->SystemStatus > S_IDLE &&
  10305. chargingInfo[gunIndex]->SystemStatus <= S_CHARGING)
  10306. {
  10307. struct DERATING_BY_OTP *_derating;
  10308. _derating = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp;
  10309. if(_derating->deratingIndex < GUN_DERATING_COUNT)
  10310. {
  10311. int _connectorTemp = (int)chargingInfo[gunIndex]->ConnectorTemp - 60;
  10312. int _chillerTemp = (int)chargingInfo[gunIndex]->ChillerTemp - 60;
  10313. if(_connectorTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  10314. chargingInfo[gunIndex]->ConnectorTemp != 0xFF &&
  10315. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  10316. {
  10317. LOG_INFO("Gun %d GunTemp %d is Derating(%d) to %d A",
  10318. gunIndex + 1,
  10319. _connectorTemp,
  10320. GunDeratingTemp[_derating->deratingIndex],
  10321. ((int)_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  10322. _derating->deratingIndex++;
  10323. }
  10324. else if(_chillerTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  10325. chargingInfo[gunIndex]->ChillerTemp != 0xFF &&
  10326. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  10327. {
  10328. LOG_INFO("Gun %d ChillerTemp %d is Derating(%d) to %d A",
  10329. gunIndex + 1,
  10330. _chillerTemp,
  10331. GunDeratingTemp[_derating->deratingIndex],
  10332. ((int)_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  10333. _derating->deratingIndex++;
  10334. }
  10335. }
  10336. if(chargingInfo[gunIndex]->ConnectorTemp != 0xFF || chargingInfo[gunIndex]->ChillerTemp != 0xFF)
  10337. {
  10338. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = (int)_derating->deratingTargetCurrent[_derating->deratingIndex];
  10339. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10340. }
  10341. else
  10342. {
  10343. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  10344. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10345. }
  10346. }
  10347. else
  10348. {
  10349. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  10350. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10351. }
  10352. }
  10353. void CalculateGunAbsMaxPower(int gunIndex)
  10354. {
  10355. int maxValue = 0;
  10356. maxValue = (int)GetModelNameLimitPower(gunIndex);
  10357. ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] = maxValue != 0 ? (maxValue * 100) : 0;
  10358. }
  10359. void CalculateGunAbsMaxVoltage(int gunIndex)
  10360. {
  10361. int maxValue = 0;
  10362. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage;
  10363. ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] = maxValue;
  10364. }
  10365. void CalculateGunAbsMaxCurrent(int gunIndex)
  10366. {
  10367. int maxValue = 0;
  10368. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent;
  10369. ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] = maxValue;
  10370. }
  10371. void RestrictChargingCapability(int gunIndex, unsigned short *power, unsigned short *voltage, unsigned short *current)
  10372. {
  10373. unsigned short limitPower = 0;
  10374. int _limitPowerResult = 0;
  10375. int _limitVoltageResult = 0;
  10376. int _limitCurrentResult = 0;
  10377. int _IndividualLimit = NO;
  10378. _limitPowerResult = ShmPsuData->SystemAvailablePower;
  10379. _limitVoltageResult = MAX_OUTPUT_VOLTAGE;
  10380. _limitCurrentResult = ShmPsuData->SystemAvailableCurrent;
  10381. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] != 0)
  10382. {
  10383. limitPower = ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] / 100;
  10384. if(limitPower != 0 && _limitPowerResult > limitPower)
  10385. {
  10386. _limitPowerResult = limitPower;
  10387. }
  10388. }
  10389. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] != 0 &&
  10390. _limitVoltageResult > ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex])
  10391. {
  10392. _limitVoltageResult = ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex];
  10393. }
  10394. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] != 0 &&
  10395. _limitCurrentResult > ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex])
  10396. {
  10397. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex];
  10398. }
  10399. // restrict from WebPageConfig power
  10400. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower != 0)
  10401. {
  10402. if(_limitPowerResult > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower)
  10403. {
  10404. _limitPowerResult = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower;
  10405. }
  10406. }
  10407. // restrict from WebPageConfig current
  10408. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent != 0)
  10409. {
  10410. if(_limitCurrentResult > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent)
  10411. {
  10412. _limitCurrentResult = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent;
  10413. }
  10414. }
  10415. // restrict from Offline_MaxChargingPower
  10416. if(ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] != 0)
  10417. {
  10418. limitPower = ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] / 100;
  10419. if(limitPower != 0 && _limitPowerResult > limitPower)
  10420. {
  10421. _limitPowerResult = limitPower;
  10422. }
  10423. }
  10424. // restrict from MaxChargingProfile
  10425. if(ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] != 0)
  10426. {
  10427. limitPower = ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] / 100;
  10428. if(limitPower != 0 && _limitPowerResult > limitPower)
  10429. {
  10430. _limitPowerResult = limitPower;
  10431. }
  10432. }
  10433. // restrict from MaxChargingProfile
  10434. if(ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] != 0)
  10435. {
  10436. if(_limitCurrentResult > ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex])
  10437. {
  10438. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex];
  10439. }
  10440. }
  10441. // restrict from ChargingProfile
  10442. if((chargingInfo[gunIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) ||
  10443. (chargingInfo[gunIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gunIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10444. {
  10445. if(chargingInfo[gunIndex]->ChargingProfilePower >= 0 && _limitPowerResult > ((int)chargingInfo[gunIndex]->ChargingProfilePower / 100))
  10446. {
  10447. _limitPowerResult = (int)chargingInfo[gunIndex]->ChargingProfilePower / 100;
  10448. _IndividualLimit = true;
  10449. }
  10450. if(chargingInfo[gunIndex]->ChargingProfileCurrent >= 0 && _limitCurrentResult > (int)chargingInfo[gunIndex]->ChargingProfileCurrent)
  10451. {
  10452. _limitCurrentResult = (int)chargingInfo[gunIndex]->ChargingProfileCurrent;
  10453. _IndividualLimit = true;
  10454. }
  10455. }
  10456. // restrict from local power limit
  10457. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  10458. {
  10459. if(chargingInfo[gunIndex]->LocalPowerLimitCurrent >= 0 && _limitCurrentResult > (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10))
  10460. {
  10461. _limitCurrentResult = (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10);
  10462. _IndividualLimit = true;
  10463. }
  10464. }
  10465. // restrict after otp
  10466. if(ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] != 0)
  10467. {
  10468. limitPower = ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] / 100;
  10469. if(limitPower != 0 && _limitPowerResult > limitPower)
  10470. {
  10471. _limitPowerResult = limitPower;
  10472. _IndividualLimit = true;
  10473. }
  10474. }
  10475. // restrict after otp
  10476. if(ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] != 0 &&
  10477. _limitCurrentResult > ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex])
  10478. {
  10479. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex];
  10480. _IndividualLimit = true;
  10481. }
  10482. ShmChargerInfo->OutputLimit.GunLimitPower[gunIndex] = _limitPowerResult;
  10483. ShmChargerInfo->OutputLimit.GunLimitVoltage[gunIndex] = _limitVoltageResult;
  10484. ShmChargerInfo->OutputLimit.GunLimitCurrent[gunIndex] = _limitCurrentResult;
  10485. ShmChargerInfo->OutputLimit.IndividualLimit[gunIndex] = _IndividualLimit;
  10486. if(_limitPowerResult != 0 && *power > _limitPowerResult)
  10487. {
  10488. *power = _limitPowerResult;
  10489. }
  10490. if(_limitVoltageResult != 0 && *voltage > _limitVoltageResult)
  10491. {
  10492. *voltage = _limitVoltageResult;
  10493. }
  10494. if(_limitCurrentResult != 0 && *current > _limitCurrentResult)
  10495. {
  10496. *current = _limitCurrentResult;
  10497. }
  10498. }
  10499. void UpdateChargingCapability(void)
  10500. {
  10501. unsigned short voltage = 0, current = 0, power = 0;
  10502. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower = Get_Ocpp_OffLineMaxChargingPower();
  10503. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10504. {
  10505. voltage = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.MaximumChargingVoltage;
  10506. current = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingCurrent;
  10507. power = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingPower;
  10508. CalculateGunAbsMaxPower(i);
  10509. CalculateGunAbsMaxVoltage(i);
  10510. CalculateGunAbsMaxCurrent(i);
  10511. CalculateMaxOutputRatio(i);
  10512. GetMaxChargingLimit(i);
  10513. GetLocalPowerLimit(i);
  10514. GetGunOtpDerating(i);
  10515. RestrictChargingCapability(i, &power, &voltage, &current);
  10516. if((chargingInfo[i]->SystemStatus >= S_CHARGING && chargingInfo[i]->SystemStatus <= S_ALARM) ||
  10517. (chargingInfo[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[i]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  10518. (chargingInfo[i]->SystemStatus == S_PREPARING_FOR_EVSE && ShmPsuGrouping->GroupCollection[i].GroupCtrl.bits.CableCheckDone))
  10519. {
  10520. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  10521. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  10522. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  10523. }
  10524. else
  10525. {
  10526. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage > 0 ?
  10527. (float)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage : (float)voltage;
  10528. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent > 0 ?
  10529. (float)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent : (float)current;
  10530. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = ShmPsuData->SystemAvailablePower > 0 ?
  10531. (float)ShmPsuData->SystemAvailablePower : (float)power;
  10532. }
  10533. }
  10534. }
  10535. void GunErrorCollection(void)
  10536. {
  10537. bool _PsuNoResource = false;
  10538. bool _ParallelRelayWelding = false;
  10539. bool _ParallelRelayDrivingFault = false;
  10540. bool _OutputRelayWelding = false;
  10541. bool _OutputRelayDrivingFault = false;
  10542. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10543. {
  10544. if(ShmPsuData->Work_Step == INITIAL_START)
  10545. {
  10546. ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupNoResource = false;
  10547. }
  10548. else
  10549. {
  10550. if(ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupNoResource)
  10551. {
  10552. _PsuNoResource = true;
  10553. }
  10554. }
  10555. if(ShmChargerInfo->GunError[i].ErrFlag.bits.ParallelRelayWelding)
  10556. {
  10557. _ParallelRelayWelding = true;
  10558. }
  10559. if(ShmChargerInfo->GunError[i].ErrFlag.bits.ParallelRelayDrivingFault)
  10560. {
  10561. _ParallelRelayDrivingFault = true;
  10562. }
  10563. if(ShmChargerInfo->GunError[i].ErrFlag.bits.OutputRelayWelding)
  10564. {
  10565. _OutputRelayWelding = true;
  10566. }
  10567. if(ShmChargerInfo->GunError[i].ErrFlag.bits.OutputRelayDrivingFault)
  10568. {
  10569. _OutputRelayDrivingFault = true;
  10570. }
  10571. }
  10572. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = _PsuNoResource;
  10573. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = _ParallelRelayWelding;
  10574. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = _ParallelRelayDrivingFault;
  10575. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.NormalOutputRelayWelding = _OutputRelayWelding;
  10576. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.NormalOutputRelayDrivingFault = _OutputRelayDrivingFault;
  10577. }
  10578. void UpdateGunPresentChargingPower(int gunIndex)
  10579. {
  10580. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Enable)
  10581. {
  10582. chargingInfo[gunIndex]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingCurrent))) / 1000 / 100);
  10583. }
  10584. else
  10585. {
  10586. chargingInfo[gunIndex]->PresentChargingPower = ((float)((chargingInfo[gunIndex]->PresentChargingVoltage) * (chargingInfo[gunIndex]->PresentChargingCurrent)) / 1000);
  10587. }
  10588. }
  10589. void CheckPsuLostQuantity(void)
  10590. {
  10591. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  10592. {
  10593. if(ShmChargerInfo->Control.PsuQuantityByModel == ShmPsuData->SystemPresentPsuQuantity)
  10594. {
  10595. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = false;
  10596. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = 0;
  10597. }
  10598. else
  10599. {
  10600. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = ShmChargerInfo->Control.PsuQuantityByModel > ShmPsuData->SystemPresentPsuQuantity ?
  10601. (ShmChargerInfo->Control.PsuQuantityByModel - ShmPsuData->SystemPresentPsuQuantity) : 0;
  10602. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch ||
  10603. _psuLostQuantity != ShmSysConfigAndInfo->SysInfo.PsuLostQuantity)
  10604. {
  10605. LOG_INFO("Psu Quantity is %d, it should be %d, Psu Lost: %d",
  10606. ShmPsuData->SystemPresentPsuQuantity, ShmChargerInfo->Control.PsuQuantityByModel,
  10607. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity);
  10608. }
  10609. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = true;
  10610. _psuLostQuantity = ShmSysConfigAndInfo->SysInfo.PsuLostQuantity;
  10611. }
  10612. }
  10613. }
  10614. void CheckOcmfHandler(void)
  10615. {
  10616. bool _newOcmf = false;
  10617. int _ocmfLength = 0;
  10618. int _gunIndex = 0;
  10619. char _txId[37];
  10620. char _ocmfKey[201];
  10621. char _ocmfInfo[2048];
  10622. // check new ocmf info
  10623. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10624. {
  10625. if(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfFlag.bits.OcmfReceived)
  10626. {
  10627. _newOcmf = false;
  10628. char fileName[256] = {0};
  10629. sprintf(fileName, "/mnt/Gun_%d_%s_OCMF", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId);
  10630. if(access(fileName, F_OK) != -1)
  10631. {
  10632. _newOcmf = true;
  10633. }
  10634. if(_newOcmf)
  10635. {
  10636. int fd = 0;
  10637. fd = open(fileName, O_RDONLY);
  10638. if(fd < 0)
  10639. {
  10640. LOG_INFO("Gun %d TxId[%s] OCMF file [%s] open fail", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, fileName);
  10641. }
  10642. else
  10643. {
  10644. memset(_txId, 0x00, sizeof(_txId));
  10645. memset(_ocmfKey, 0x00, sizeof(_ocmfKey));
  10646. memset(_ocmfInfo, 0x00, sizeof(_ocmfInfo)); //-48 is take out the header
  10647. _ocmfLength = read(fd, _ocmfInfo, sizeof(_ocmfInfo) - 1);
  10648. close(fd);
  10649. memcpy(_txId, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId));
  10650. memcpy(_ocmfKey, ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey));
  10651. DB_Insert_Ocmf(localDb, i, _txId, _ocmfKey, _ocmfInfo);
  10652. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending = true;
  10653. char cmdBuf[256] = {0};
  10654. sprintf(cmdBuf, "rm -f %s", fileName);
  10655. system(cmdBuf);
  10656. LOG_INFO("Gun %d TxId[%s] insert OCMF info ok, length: %d", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, _ocmfLength);
  10657. }
  10658. }
  10659. else
  10660. {
  10661. LOG_INFO("Gun %d TxId[%s] OCMF info [%s] access fail", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, fileName);
  10662. }
  10663. memset(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, 0x00, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId));
  10664. memset(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey, 0x00, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey));
  10665. ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfFlag.bits.OcmfReceived = false;
  10666. }
  10667. }
  10668. // try to find ocmf pending in database
  10669. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending && !ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload)
  10670. {
  10671. _gunIndex = 0;
  10672. memset(_txId, 0x00, sizeof(_txId));
  10673. memset(_ocmfKey, 0x00, sizeof(_ocmfKey));
  10674. memset(_ocmfInfo, 0x00, sizeof(_ocmfInfo));
  10675. ShmChargerInfo->OcmfInfo.SysOcmfIndex = 0;
  10676. memset(ShmChargerInfo->OcmfInfo.SysOcmfTxId, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfTxId));
  10677. memset(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey));
  10678. memset(ShmChargerInfo->OcmfInfo.SysOcmfInfo, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfInfo));
  10679. if(DB_Get_Undisposed_Ocmf(localDb, &_gunIndex, _txId, _ocmfKey, _ocmfInfo))
  10680. {
  10681. ShmChargerInfo->OcmfInfo.SysOcmfIndex = _gunIndex;
  10682. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfTxId, _txId, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfTxId));
  10683. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, _ocmfKey, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey));
  10684. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfInfo, _ocmfInfo, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfInfo));
  10685. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload = true;
  10686. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10687. LOG_INFO("Gun %d TxId[%s] OCMF info need upload", _gunIndex + 1, _txId);
  10688. }
  10689. else
  10690. {
  10691. LOG_INFO("There is no ocmf info pending");
  10692. ShmChargerInfo->OcmfInfo.SysOcmfFlag.CtrlValue = 0;
  10693. }
  10694. }
  10695. // try to upload ocmf to backend
  10696. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload)
  10697. {
  10698. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  10699. {
  10700. if(!ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading)
  10701. {
  10702. if(!Is_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex))
  10703. {
  10704. Set_Ocpp_OcmfData(ShmChargerInfo->OcmfInfo.SysOcmfIndex, ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, ShmChargerInfo->OcmfInfo.SysOcmfInfo);
  10705. Set_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex);
  10706. LOG_INFO("Set Gun %d TxId[%s] update OCMF request", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10707. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = true;
  10708. }
  10709. }
  10710. else
  10711. {
  10712. if(!Is_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex))
  10713. {
  10714. DB_Update_Ocmf_Status(localDb, ShmChargerInfo->OcmfInfo.SysOcmfTxId, OCMF_SATAUS_UPDATED_OK);
  10715. LOG_INFO("Gun %d TxId[%s] update OCMF info to backend ok", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10716. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload = false;
  10717. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10718. }
  10719. }
  10720. }
  10721. else
  10722. {
  10723. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading)
  10724. {
  10725. Clean_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex);
  10726. LOG_INFO("Backend disconnected, Gun %d TxId[%s] ocmf will update later", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10727. }
  10728. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10729. }
  10730. }
  10731. }
  10732. int main(void)
  10733. {
  10734. unsigned long time = 0;
  10735. int _dispenser = 0;
  10736. _isStandbyEnable = true;
  10737. if(CreateShareMemory() == 0)
  10738. {
  10739. #ifdef SystemLogMessage
  10740. LOG_ERROR("CreatShareMemory NG");
  10741. #endif
  10742. if(ShmStatusCodeData!=NULL)
  10743. {
  10744. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  10745. }
  10746. sleep(5);
  10747. system("reboot -f");
  10748. sleep(5);
  10749. system("reboot -f");
  10750. }
  10751. LOG_INFO("\n");
  10752. LOG_INFO("===== Boot and Initialization start =====");
  10753. LOG_INFO("Main Fork PID = %d", getpid());
  10754. if (!InitialSystemDefaultConfig())
  10755. {
  10756. LOG_INFO("InitialSystemDefaultConfig NG");
  10757. StopProcessingLoop();
  10758. }
  10759. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  10760. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  10761. LOG_INFO("InitialShareMemoryInfo");
  10762. InitialShareMemoryInfo();
  10763. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  10764. if(!InitialChargerSetting())
  10765. {
  10766. isModelNameMatch = false;
  10767. }
  10768. Initialization();
  10769. InitialDataBaseValue();
  10770. LOG_INFO("Spawn all Task.");
  10771. SpawnTask();
  10772. if (!isModelNameMatch)
  10773. {
  10774. LOG_INFO("Module Name & HW info none match.");
  10775. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  10776. // Module Name 與硬體對應不正確
  10777. LOG_ERROR("Module Name & HW info none match.");
  10778. sleep(3);
  10779. KillAllTask();
  10780. StopProcessingLoop();
  10781. }
  10782. LOG_INFO("Module Name & HW info correct. Initialize.......");
  10783. CreateTimeoutFork();
  10784. SelfTestRun();
  10785. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  10786. {
  10787. sleep(1);
  10788. }
  10789. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  10790. {
  10791. sleep(1);
  10792. SelfTestFailReason();
  10793. }
  10794. else
  10795. {
  10796. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10797. {
  10798. setChargerMode(gun_index, MODE_IDLE);
  10799. CheckGunAvailable();
  10800. }
  10801. }
  10802. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  10803. {
  10804. SetAcContactor(ON);
  10805. SpawnOcppService();
  10806. SpawnMaintainOcppService();
  10807. }
  10808. else
  10809. {
  10810. InitSlaveCabinetEthernet();
  10811. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10812. {
  10813. setChargerMode(gun_index, MODE_MAINTAIN);
  10814. }
  10815. }
  10816. sleep(1);
  10817. CreateWatchdog();
  10818. // Main loop
  10819. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  10820. "****************************Main Loop**********************************" :
  10821. "********************** SelfTest Fail **********************");
  10822. GetClockTime(&_cmdMainPriority_time);
  10823. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending = true;
  10824. for (;;)
  10825. {
  10826. CheckGunAvailable();
  10827. CheckOcppStatus();
  10828. ChkPrimaryStatus();
  10829. ChkPsuStatus();
  10830. PsuFailureResume();
  10831. ChkConnectorAction();
  10832. if(IsUpgradeAvailable())
  10833. {
  10834. CheckFactoryConfigFunction();
  10835. CheckFwUpdateFunction();
  10836. }
  10837. if(CheckFlashUpdate() != YES)
  10838. {
  10839. // return fork process
  10840. return 0;
  10841. }
  10842. CheckSoftHardReset();
  10843. StandbyCheck();
  10844. AuthorizeTimeoutProcess();
  10845. // check ocmf request
  10846. CheckOcmfHandler();
  10847. // 當 AC 沒有搭上時,清除一些錯誤碼
  10848. //ClearAlarmCodeWhenAcOff();
  10849. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  10850. {
  10851. CheckTask();
  10852. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10853. {
  10854. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  10855. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10856. {
  10857. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  10858. {
  10859. Ocpp_ProfileReq_Cmd(_index);
  10860. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  10861. _ocppProfileChkFlag[_index] = 0;
  10862. }
  10863. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  10864. {
  10865. Ocpp_ProfileReq_Cmd(_index);
  10866. _ocppProfileChkFlag[_index] = 0;
  10867. }
  10868. else
  10869. {
  10870. _ocppProfileChkFlag[_index]++;
  10871. }
  10872. }
  10873. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  10874. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  10875. {
  10876. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10877. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  10878. {
  10879. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10880. }
  10881. }
  10882. }
  10883. CheckPsuLostQuantity();
  10884. GetClockTime(&_cmdMainPriority_time);
  10885. }
  10886. CheckMiscCommandRequirement();
  10887. CheckDispenserVersionUpdateRequirement();
  10888. MCabinetParallelingProcess();
  10889. SCabinetControllProcess();
  10890. UpdateChargingCapability();
  10891. GunErrorCollection();
  10892. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10893. {
  10894. //CheckGpioInStatus();
  10895. CheckErrorOccurStatus(gun_index);
  10896. ChkOcppStatus(gun_index);
  10897. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  10898. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10899. {
  10900. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  10901. {
  10902. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  10903. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  10904. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  10905. {
  10906. _ocppProfileChkFlag[gun_index] = 12;
  10907. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  10908. }
  10909. }
  10910. }
  10911. CheckSmartChargeProfile(gun_index);
  10912. Update_PowerConsumption(gun_index);
  10913. CheckInProgress(gun_index);
  10914. CheckDebugMode(gun_index);
  10915. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  10916. switch(chargingInfo[gun_index]->SystemStatus)
  10917. {
  10918. case S_MAINTAIN:
  10919. if(isModeChange(gun_index))
  10920. {
  10921. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10922. {
  10923. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  10924. }
  10925. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  10926. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10927. GetClockTime(&_SystemStatus_Time[gun_index]);
  10928. }
  10929. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10930. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  10931. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  10932. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10933. {
  10934. break;
  10935. }
  10936. if(chargingInfo[gun_index]->IsAvailable == YES &&
  10937. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  10938. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  10939. {
  10940. if(time >= MAINTAIN_RECOVERY_TIME)
  10941. {
  10942. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10943. setChargerMode(gun_index, MODE_IDLE);
  10944. }
  10945. }
  10946. break;
  10947. case S_RESERVATION:
  10948. if(isModeChange(gun_index))
  10949. {
  10950. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  10951. char expiryDate[128];
  10952. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  10953. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  10954. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  10955. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  10956. GetClockTime(&_SystemStatus_Time[gun_index]);
  10957. }
  10958. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10959. {
  10960. SetAcContactor(ON);
  10961. }
  10962. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10963. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10964. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10965. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10966. {
  10967. UpdateErrorCodeToOcpp(gun_index);
  10968. setChargerMode(gun_index, MODE_FAULT);
  10969. continue;
  10970. }
  10971. if(Is_Ocpp_ReserveNowExpired(gun_index))
  10972. {
  10973. chargingInfo[gun_index]->ReservationId = -1;
  10974. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  10975. setChargerMode(gun_index, MODE_IDLE);
  10976. }
  10977. else
  10978. {
  10979. if(IsConnectorAuthorizeSuccess(gun_index))
  10980. {
  10981. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10982. {
  10983. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  10984. }
  10985. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10986. {
  10987. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  10988. }
  10989. setChargerMode(gun_index, MODE_AUTHORIZING);
  10990. }
  10991. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10992. {
  10993. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10994. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  10995. {
  10996. SetAcContactor(ON);
  10997. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10998. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10999. {
  11000. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  11001. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11002. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  11003. {
  11004. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  11005. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  11006. }
  11007. DispenserAuthInitial(_dispenser);
  11008. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11009. }
  11010. }
  11011. }
  11012. else
  11013. {
  11014. if(chargingInfo[gun_index]->ReservationId == -1)
  11015. {
  11016. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  11017. setChargerMode(gun_index, MODE_IDLE);
  11018. }
  11019. }
  11020. }
  11021. break;
  11022. case S_IDLE:
  11023. case S_FAULT:
  11024. {
  11025. // clean PermissionRequest flag
  11026. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  11027. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11028. if(isModeChange(gun_index))
  11029. {
  11030. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  11031. {
  11032. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  11033. chargingInfo[gun_index]->ReservationId = -1;
  11034. _triggerTransaction[gun_index] = false;
  11035. _plugInHold[gun_index] = false;
  11036. chargingInfo[gun_index]->PresentChargedDuration = 0;
  11037. chargingInfo[gun_index]->RemainChargingDuration = 0;
  11038. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  11039. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  11040. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  11041. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  11042. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  11043. Clean_Ocpp_StopReason(gun_index);
  11044. ReleaseAlarmCode(gun_index);
  11045. // clean connector information
  11046. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  11047. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  11048. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  11049. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  11050. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  11051. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  11052. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  11053. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].CostDiscount = 0;
  11054. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemainAmount = 0;
  11055. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  11056. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  11057. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  11058. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  11059. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  11060. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  11061. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11062. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  11063. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.deratingByConnOtp.deratingIndex = 0;
  11064. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  11065. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  11066. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  11067. chargingInfo[gun_index]->EvBatterySoc = 0;
  11068. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  11069. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  11070. // clean RemoteStart & RemoteStop flag
  11071. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  11072. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  11073. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  11074. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  11075. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  11076. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  11077. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  11078. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  11079. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  11080. ShmChargerInfo->GunError[gun_index].ErrFlag.ErrorValue = 0;
  11081. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  11082. Clean_Ocpp_RunningCost(gun_index);
  11083. Clean_Ocpp_FinalCost(gun_index);
  11084. Clean_DeductInfo(gun_index);
  11085. Clean_Ocpp_TcciSerialNo(gun_index);
  11086. Clean_UserTransaction(gun_index);
  11087. Clean_UserPriceAndReceipt(gun_index);
  11088. presentChargedEnergyClear(gun_index);
  11089. chargingInfo[gun_index]->PresentChargingPower = 0;
  11090. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  11091. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  11092. chargingInfo[gun_index]->ChargingProfilePower = -1;
  11093. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  11094. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  11095. {
  11096. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  11097. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  11098. // reset dispenser availability
  11099. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11100. }
  11101. // clean force charging info
  11102. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  11103. }
  11104. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  11105. {
  11106. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  11107. }
  11108. GetClockTime(&_SystemStatus_Time[gun_index]);
  11109. }
  11110. if (chargingInfo[gun_index]->IsAvailable == NO ||
  11111. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  11112. {
  11113. setChargerMode(gun_index, MODE_MAINTAIN);
  11114. continue;
  11115. }
  11116. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  11117. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  11118. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  11119. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11120. {
  11121. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  11122. {
  11123. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11124. {
  11125. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  11126. }
  11127. }
  11128. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  11129. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11130. {
  11131. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11132. {
  11133. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  11134. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  11135. }
  11136. }
  11137. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  11138. {
  11139. UpdateErrorCodeToOcpp(gun_index);
  11140. setChargerMode(gun_index, MODE_FAULT);
  11141. }
  11142. else
  11143. {
  11144. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11145. {
  11146. CheckConnectorDisconnectionRecovery(gun_index);
  11147. }
  11148. }
  11149. }
  11150. else
  11151. {
  11152. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  11153. {
  11154. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  11155. if(time >= FAULT_RELEASE_TIME)
  11156. {
  11157. ReleaseAlarmCode(gun_index);
  11158. setChargerMode(gun_index, MODE_IDLE);
  11159. }
  11160. break;
  11161. }
  11162. // clean stop charge flag
  11163. chargingInfo[gun_index]->StopChargeFlag = false;
  11164. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  11165. if(chargingInfo[gun_index]->ConnectorPlugIn || ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisable)
  11166. {
  11167. SetAcContactor(ON);
  11168. }
  11169. { // Idle 正常程序起點
  11170. // 判斷是否有啟用檢查插槍
  11171. if(IsConnectorAuthorizeSuccess(gun_index))
  11172. {
  11173. if(chargingInfo[gun_index]->IsAvailable &&
  11174. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  11175. {
  11176. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  11177. {
  11178. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  11179. Clean_Ocpp_TcciCreditInfo(gun_index);
  11180. }
  11181. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  11182. {
  11183. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  11184. }
  11185. Set_StartIdType(gun_index, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthIdType);
  11186. setChargerMode(gun_index, MODE_AUTHORIZING);
  11187. continue;
  11188. }
  11189. }
  11190. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  11191. {
  11192. SetAcContactor(ON);
  11193. if(chargingInfo[gun_index]->IsAvailable &&
  11194. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  11195. chargingInfo[gun_index]->ConnectorPlugIn &&
  11196. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  11197. {
  11198. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  11199. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  11200. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11201. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  11202. {
  11203. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  11204. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  11205. }
  11206. Set_StartIdType(gun_index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthIdType);
  11207. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11208. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11209. continue;
  11210. }
  11211. }
  11212. // only for test & debug purpose
  11213. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  11214. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  11215. {
  11216. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  11217. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  11218. LOG_INFO("Gun %d Start Force Charging%s",
  11219. gun_index + 1,
  11220. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  11221. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  11222. {
  11223. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  11224. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  11225. }
  11226. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  11227. {
  11228. LOG_INFO("Set Dispenser %d Inoperative",
  11229. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  11230. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11231. }
  11232. Set_StartIdType(gun_index, _Type_Local);
  11233. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11234. continue;
  11235. }
  11236. else
  11237. {
  11238. }
  11239. } // Idle 正常程序終點
  11240. }
  11241. }
  11242. break;
  11243. case S_AUTHORIZING:
  11244. if(isModeChange(gun_index))
  11245. {
  11246. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  11247. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  11248. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  11249. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  11250. //StopSystemTimeoutDet();
  11251. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  11252. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11253. SetAcContactor(ON);
  11254. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11255. }
  11256. if(!_triggerTransaction[gun_index] &&
  11257. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC &&
  11258. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  11259. {
  11260. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= STARTTRANSATION_TIME)
  11261. {
  11262. OcppStartTransaction(gun_index);
  11263. }
  11264. }
  11265. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  11266. {
  11267. FouceAnnounceAccountBalance(gun_index);
  11268. }
  11269. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  11270. {
  11271. // sync with dispenser
  11272. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  11273. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11274. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  11275. FouceAnnounceAccountBalance(gun_index);
  11276. }
  11277. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  11278. {
  11279. // sync with dispenser
  11280. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  11281. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11282. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  11283. }
  11284. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  11285. {
  11286. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  11287. {
  11288. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  11289. }
  11290. else
  11291. {
  11292. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  11293. }
  11294. OcppStopTransaction(gun_index);
  11295. setChargerMode(gun_index, MODE_IDLE);
  11296. }
  11297. else
  11298. {
  11299. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  11300. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  11301. {
  11302. AddPlugInTimes(gun_index);
  11303. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11304. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11305. GunAuthInitial(gun_index);
  11306. }
  11307. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  11308. {
  11309. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  11310. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11311. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  11312. OcppStopTransaction(gun_index);
  11313. setChargerMode(gun_index, MODE_IDLE);
  11314. }
  11315. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  11316. {
  11317. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  11318. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11319. ChargingAlarmProcess(gun_index);
  11320. }
  11321. else if(isNormalStopChargeFlag(gun_index) == YES)
  11322. {
  11323. LOG_INFO("********** Gun %d Normal Stop (S_AUTHORIZING) **********", gun_index + 1);
  11324. Set_Ocpp_StopReason(gun_index, "Local");
  11325. ChargingTerminalProcess(gun_index);
  11326. }
  11327. }
  11328. break;
  11329. case S_REASSIGN_CHECK:
  11330. {
  11331. if (isModeChange(gun_index))
  11332. {
  11333. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  11334. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  11335. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  11336. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  11337. //StopSystemTimeoutDet();
  11338. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  11339. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  11340. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  11341. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  11342. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  11343. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  11344. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  11345. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  11346. SetAcContactor(ON);
  11347. SetPCabinetOutputRelay(gun_index, ON);
  11348. GetClockTime(&_SystemStatus_Time[gun_index]);
  11349. }
  11350. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11351. int isRequestOK = ConnectorRequestToCharging(gun_index);
  11352. if(isRequestOK == PASS)
  11353. {
  11354. setChargerMode(gun_index, MODE_REASSIGN);
  11355. }
  11356. else if(time >= WAIT_REASSIGN_TIME || isRequestOK == FAIL)
  11357. {
  11358. if(isRequestOK == FAIL)
  11359. {
  11360. LOG_INFO("Gun %d Request To Charging Fail", gun_index + 1);
  11361. }
  11362. else if(ShmPsuData->Work_Step != _WORK_CHARGING)
  11363. {
  11364. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  11365. }
  11366. else
  11367. {
  11368. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  11369. }
  11370. ShmChargerInfo->GunError[gun_index].ErrFlag.bits.PsuGroupNoResource = true;
  11371. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11372. {
  11373. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042279", 6);
  11374. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  11375. }
  11376. Set_Ocpp_StopReason(gun_index, "Other");
  11377. ChargingAlarmProcess(gun_index);
  11378. //setChargerMode(gun_index, MODE_IDLE);
  11379. }
  11380. }
  11381. break;
  11382. case S_REASSIGN:
  11383. {
  11384. if (isModeChange(gun_index))
  11385. {
  11386. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  11387. //gettimeofday(&_toAverage_time, NULL);
  11388. GetClockTime(&_SystemStatus_Time[gun_index]);
  11389. }
  11390. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11391. if(IsConnectorGroupingCompleted(gun_index))
  11392. {
  11393. setChargerMode(gun_index, MODE_PRECHARGE);
  11394. }
  11395. else if(time >= MAX_REQUEST_CHARGING_TIME)
  11396. {
  11397. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  11398. setChargerMode(gun_index, MODE_IDLE);
  11399. }
  11400. }
  11401. break;
  11402. case S_PREPARNING:
  11403. {
  11404. if (isModeChange(gun_index))
  11405. {
  11406. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  11407. //StopGunInfoTimeoutDet(gun_index);
  11408. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  11409. SetAcContactor(ON);
  11410. GetClockTime(&_SystemStatus_Time[gun_index]);
  11411. }
  11412. UpdateGunPresentChargingPower(gun_index);
  11413. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11414. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  11415. {
  11416. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  11417. break;;
  11418. }
  11419. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11420. {
  11421. // 板端或後臺要求停止
  11422. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  11423. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11424. ChargingAlarmProcess(gun_index);
  11425. }
  11426. else if(isNormalStopChargeFlag(gun_index) == YES)
  11427. {
  11428. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  11429. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11430. Set_Ocpp_StopReason(gun_index, "Local");
  11431. ChargingTerminalProcess(gun_index);
  11432. }
  11433. else if(isResetStopChargeFlag(gun_index) == YES)
  11434. {
  11435. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  11436. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11437. char *reason[16];
  11438. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11439. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11440. ChargingTerminalProcess(gun_index);
  11441. }
  11442. else if(isBackendStopFlag(gun_index))
  11443. {
  11444. LOG_INFO("********** Gun %d Backend Stop (S_PREPARNING) **********", gun_index + 1);
  11445. Set_Ocpp_StopReason(gun_index, "Local");
  11446. ChargingTerminalProcess(gun_index);
  11447. }
  11448. if(time > GUN_PREPARE_TIMEOUT)
  11449. {
  11450. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  11451. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  11452. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11453. {
  11454. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042279", 6);
  11455. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  11456. }
  11457. Set_Ocpp_StopReason(gun_index, "Other");
  11458. ChargingAlarmProcess(gun_index);
  11459. }
  11460. }
  11461. break;
  11462. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  11463. {
  11464. if (isModeChange(gun_index))
  11465. {
  11466. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  11467. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  11468. //StopGunInfoTimeoutDet(gun_index);
  11469. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  11470. GetClockTime(&_SystemStatus_Time[gun_index]);
  11471. }
  11472. UpdateGunPresentChargingPower(gun_index);
  11473. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11474. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  11475. {
  11476. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  11477. }
  11478. // only for test & debug purpose
  11479. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11480. {
  11481. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  11482. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  11483. }
  11484. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11485. {
  11486. // 板端或後臺要求停止
  11487. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  11488. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11489. ChargingAlarmProcess(gun_index);
  11490. }
  11491. else if(isNormalStopChargeFlag(gun_index) == YES)
  11492. {
  11493. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  11494. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11495. Set_Ocpp_StopReason(gun_index, "Local");
  11496. ChargingTerminalProcess(gun_index);
  11497. }
  11498. else if(isResetStopChargeFlag(gun_index) == YES)
  11499. {
  11500. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  11501. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11502. char *reason[16];
  11503. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11504. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11505. ChargingTerminalProcess(gun_index);
  11506. }
  11507. else if(isBackendStopFlag(gun_index))
  11508. {
  11509. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  11510. Set_Ocpp_StopReason(gun_index, "Local");
  11511. ChargingTerminalProcess(gun_index);
  11512. }
  11513. if(time >= GUN_EV_WAIT_TIMEOUT)
  11514. {
  11515. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  11516. ChargingTerminalProcess(gun_index);
  11517. }
  11518. }
  11519. break;
  11520. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  11521. {
  11522. if (isModeChange(gun_index))
  11523. {
  11524. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  11525. GetClockTime(&_SystemStatus_Time[gun_index]);
  11526. #ifdef STARTTRANSATION_IMMEDIATELY
  11527. OcppStartTransaction(gun_index);
  11528. #endif
  11529. }
  11530. CheckoutStartChargingSoc(gun_index, NO);
  11531. UpdateGunPresentChargingPower(gun_index);
  11532. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11533. // only for test & debug purpose
  11534. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11535. {
  11536. if(time >= FORCE_CHARGING_CMD_DELAY)
  11537. {
  11538. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  11539. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  11540. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  11541. {
  11542. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  11543. }
  11544. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  11545. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  11546. }
  11547. }
  11548. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  11549. {
  11550. setChargerMode(gun_index, MODE_CHARGING);
  11551. }
  11552. // only for test & debug purpose
  11553. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11554. {
  11555. if(time >= FORCE_CHARGING_WAIT_EVSE)
  11556. {
  11557. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  11558. setChargerMode(gun_index, MODE_CHARGING);
  11559. }
  11560. }
  11561. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11562. {
  11563. // 板端或後臺要求停止
  11564. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11565. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11566. ChargingAlarmProcess(gun_index);
  11567. }
  11568. else if(isNormalStopChargeFlag(gun_index) == YES)
  11569. {
  11570. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  11571. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11572. Set_Ocpp_StopReason(gun_index, "Local");
  11573. ChargingTerminalProcess(gun_index);
  11574. }
  11575. else if(isResetStopChargeFlag(gun_index) == YES)
  11576. {
  11577. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  11578. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11579. char *reason[16];
  11580. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11581. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11582. ChargingTerminalProcess(gun_index);
  11583. }
  11584. else if(isInvalidIdStopFlag(gun_index) == YES)
  11585. {
  11586. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11587. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  11588. ChargingTerminalProcess(gun_index);
  11589. }
  11590. else if(isBackendStopFlag(gun_index))
  11591. {
  11592. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11593. Set_Ocpp_StopReason(gun_index, "Local");
  11594. ChargingTerminalProcess(gun_index);
  11595. }
  11596. if(time >= GUN_EV_WAIT_TIMEOUT)
  11597. {
  11598. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  11599. ChargingTerminalProcess(gun_index);
  11600. }
  11601. }
  11602. break;
  11603. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  11604. {
  11605. if (isModeChange(gun_index))
  11606. {
  11607. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  11608. //StopGunInfoTimeoutDet(gun_index);
  11609. GetClockTime(&_SystemStatus_Time[gun_index]);
  11610. GetClockTime(&_ChargedEnergy_Time[gun_index]);
  11611. CheckoutStartChargingSoc(gun_index, YES);
  11612. chargingInfo[gun_index]->PresentChargedDuration = 0;
  11613. ChangeStartOrStopDateTime(YES, gun_index);
  11614. #ifndef STARTTRANSATION_IMMEDIATELY
  11615. OcppStartTransaction(gun_index);
  11616. #endif
  11617. }
  11618. // only for test & debug purpose
  11619. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11620. {
  11621. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  11622. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  11623. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  11624. {
  11625. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  11626. }
  11627. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  11628. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  11629. }
  11630. // 計算 Power
  11631. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  11632. //chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  11633. UpdateGunPresentChargingPower(gun_index);
  11634. ChargedEnergySummation(gun_index);
  11635. CheckSessionTargetStop(gun_index);
  11636. if (isEvBoardStopChargeFlag(gun_index))
  11637. {
  11638. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  11639. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11640. ChargingAlarmProcess(gun_index);
  11641. }
  11642. else if(isNormalStopChargeFlag(gun_index) == YES)
  11643. {
  11644. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  11645. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11646. Set_Ocpp_StopReason(gun_index, "Local");
  11647. ChargingTerminalProcess(gun_index);
  11648. }
  11649. else if(isResetStopChargeFlag(gun_index) == YES)
  11650. {
  11651. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  11652. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11653. char *reason[16];
  11654. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11655. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11656. ChargingTerminalProcess(gun_index);
  11657. }
  11658. else if(isInvalidIdStopFlag(gun_index) == YES)
  11659. {
  11660. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  11661. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  11662. ChargingTerminalProcess(gun_index);
  11663. }
  11664. else if(CheckBackendChargingTimeout(gun_index) ||
  11665. CheckBackendChargingEnergy(gun_index) ||
  11666. IsReachConfigMaxChargingSoc(gun_index))
  11667. {
  11668. LOG_INFO("********** Gun %d Backend Condition Stop (S_CHARGING) **********", gun_index + 1);
  11669. Set_Ocpp_StopReason(gun_index, "Other");
  11670. ChargingTerminalProcess(gun_index);
  11671. }
  11672. else if(isSessionTargetStopFlag(gun_index) == YES)
  11673. {
  11674. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  11675. Set_Ocpp_StopReason(gun_index, "Other");
  11676. ChargingTerminalProcess(gun_index);
  11677. }
  11678. else if(isBackendStopFlag(gun_index))
  11679. {
  11680. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  11681. Set_Ocpp_StopReason(gun_index, "Local");
  11682. ChargingTerminalProcess(gun_index);
  11683. }
  11684. }
  11685. break;
  11686. case S_ALARM:
  11687. case S_TERMINATING:
  11688. {
  11689. if (isModeChange(gun_index))
  11690. {
  11691. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  11692. {
  11693. Set_Ocpp_StopReason(gun_index, "Other");
  11694. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  11695. UpdateErrorCodeToOcpp(gun_index);
  11696. TheEndCharging(gun_index);
  11697. }
  11698. else
  11699. {
  11700. Set_Ocpp_StopReason(gun_index, "Local");
  11701. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  11702. }
  11703. //StopGunInfoTimeoutDet(gun_index);
  11704. GetClockTime(&_SystemStatus_Time[gun_index]);
  11705. }
  11706. UpdateGunPresentChargingPower(gun_index);
  11707. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11708. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  11709. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  11710. {
  11711. OcppStopTransaction(gun_index);
  11712. }
  11713. else
  11714. {
  11715. ChargedEnergySummation(gun_index);
  11716. }
  11717. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  11718. {
  11719. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11720. {
  11721. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11722. {
  11723. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  11724. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11725. }
  11726. }
  11727. if(IsAvailableBackToIdle(gun_index) == YES &&
  11728. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11729. {
  11730. ReleaseAlarmCode(gun_index);
  11731. setChargerMode(gun_index, MODE_IDLE);
  11732. }
  11733. }
  11734. else
  11735. {
  11736. setChargerMode(gun_index, MODE_COMPLETE);
  11737. }
  11738. }
  11739. break;
  11740. case S_COMPLETE:
  11741. {
  11742. if (isModeChange(gun_index))
  11743. {
  11744. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  11745. TheEndCharging(gun_index);
  11746. GetClockTime(&_SystemStatus_Time[gun_index]);
  11747. }
  11748. UpdateGunPresentChargingPower(gun_index);
  11749. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11750. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  11751. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  11752. {
  11753. OcppStopTransaction(gun_index);
  11754. }
  11755. else
  11756. {
  11757. ChargedEnergySummation(gun_index);
  11758. }
  11759. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11760. {
  11761. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11762. {
  11763. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  11764. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11765. }
  11766. }
  11767. if(IsAvailableBackToIdle(gun_index) == YES &&
  11768. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11769. {
  11770. setChargerMode(gun_index, MODE_IDLE);
  11771. }
  11772. }
  11773. break;
  11774. case S_CCS_PRECHARGE_ST0:
  11775. if (isModeChange(gun_index))
  11776. {
  11777. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  11778. //StopGunInfoTimeoutDet(gun_index);
  11779. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  11780. }
  11781. break;
  11782. case S_CCS_PRECHARGE_ST1:
  11783. if (isModeChange(gun_index))
  11784. {
  11785. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  11786. }
  11787. break;
  11788. case S_DEBUG:
  11789. if (isModeChange(gun_index))
  11790. {
  11791. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  11792. }
  11793. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  11794. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  11795. {
  11796. setChargerMode(gun_index, MODE_IDLE);
  11797. }
  11798. break;
  11799. }
  11800. }
  11801. TryFeedWatchdog();
  11802. usleep(whileLoopTime);
  11803. }
  11804. return FAIL;
  11805. }