main.c 332 KB

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