main.c 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393
  1. #include "define.h"
  2. #include "main.h"
  3. //#define CDFA_CERTIFICATE // Only enable for CDFA certificate to send receipt by Phihong back end API
  4. //==========================
  5. // System basic sample constant
  6. //==========================
  7. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  8. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  9. #define PASS 1
  10. #define FAIL -1
  11. #define YES 1
  12. #define NO 0
  13. #define ON 1
  14. #define OFF 0
  15. #define BUFFER_SIZE 128
  16. //==========================
  17. // Timeout constant define
  18. //==========================
  19. #define TIMEOUT_SPEC_HANDSHAKING 180
  20. #define TIMEOUT_SPEC_AUTH 15
  21. #define TIMEOUT_SPEC_HANDSHAKING_LED 185
  22. #define TIMEOUT_SPEC_REFRESH_CHARGING_INFO 30
  23. #define TIMEOUT_SPEC_PROFILE_PREPARE 60
  24. #define TIMEOUT_SPEC_BS_HLC_HANDSHAKE 30
  25. #define TIMEOUT_SPEC_EV_READY 30
  26. #define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET 10
  27. #define TIMEOUT_SPEC_CCS_HANDSHAKE 120
  28. #define TIMEOUT_SPEC_PWN_CHANGE 5
  29. #define TIMEOUT_SPEC_POWERSAVING_LCD 120
  30. #define TIMEOUT_SPEC_POWERSAVING_RFID 120
  31. #define TIMEOUT_SPEC_POWERSAVING_METER 120
  32. #define TIMEOUT_SPEC_POWERSAVING_LED_STATUS 120
  33. #define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION 15
  34. #define TIMEOUT_SPEC_UPGRADE 300
  35. #define TIMEOUT_SPEC_RESET_WIFI_MODULE 300
  36. #define TIMEOUT_SPEC_STOPSUM_DISP 15
  37. //==========================
  38. // GPIO constant define
  39. //==========================
  40. #define GPIO_OUT_RST_RFID 62
  41. #define GPIO_OUT_RST_4G 114
  42. #define GPIO_OUT_RST_4G_WIFI 59
  43. #define GPIO_OUT_RST_QCA 115
  44. #define GPIO_OUT_RST_ETH 56
  45. #define GPIO_IN_WAKEUP 63
  46. #define MtdBlockSize 0x300000
  47. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  48. //==========================
  49. // Declare method
  50. //==========================
  51. void trim(char *s);
  52. int mystrcmp(char *p1,char *p2);
  53. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  54. void split(char **arr, char *str, const char *del);
  55. int isReachableInternet();
  56. int isRouteFail();
  57. int InitRfidPort(void);
  58. int GetCardSerialNumber();
  59. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  60. void setRequest(unsigned char gun_index,unsigned char isOn);
  61. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  62. //==========================
  63. // Declare RFID module type
  64. //==========================
  65. #define MODULE_EWT 0
  66. int wtdFd = -1;
  67. int rfidFd = -1;
  68. char* rfidPortName = "/dev/ttyS2";
  69. RFID rfid;
  70. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  71. //==========================
  72. // Declare share memory
  73. //==========================
  74. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  75. struct StatusCodeData *ShmStatusCodeData;
  76. struct PsuData *ShmPsuData;
  77. struct CHAdeMOData *ShmCHAdeMOData;
  78. struct CcsData *ShmCcsData;
  79. struct PrimaryMcuData *ShmPrimaryMcuData;
  80. struct FanModuleData *ShmFanModuleData;
  81. struct RelayModuleData *ShmRelayModuleData;
  82. struct OCPP16Data *ShmOCPP16Data;
  83. struct OCPP20Data *ShmOCPP20Data;
  84. struct Charger *ShmCharger;
  85. struct DISPENSER *ShmDispenser;
  86. struct timespec startTime[AC_QUANTITY][TMR_IDX_CNT];
  87. struct timespec startChargingTime[AC_QUANTITY];
  88. struct timespec endChargingTime[AC_QUANTITY];
  89. sqlite3 *localDb;
  90. struct SysConfigData SysConfigOrg;
  91. ParsingRatedCur modelnameInfo={0};
  92. //=================================
  93. // Common routine
  94. //=================================
  95. void trim(char *s)
  96. {
  97. int i=0, j, k, l=0;
  98. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  99. i++;
  100. j = strlen(s)-1;
  101. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  102. j--;
  103. if(i==0 && j==strlen(s)-1) { }
  104. else if(i==0) s[j+1] = '\0';
  105. else {
  106. for(k=i; k<=j; k++) s[l++] = s[k];
  107. s[l] = '\0';
  108. }
  109. }
  110. int mystrcmp(char *p1,char *p2)
  111. {
  112. while(*p1==*p2)
  113. {
  114. if(*p1=='\0' || *p2=='\0')
  115. break;
  116. p1++;
  117. p2++;
  118. }
  119. if(*p1=='\0' && *p2=='\0')
  120. return(PASS);
  121. else
  122. return(FAIL);
  123. }
  124. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  125. {
  126. strncpy(dest, src + start, cnt);
  127. dest[cnt] = 0;
  128. }
  129. void split(char **arr, char *str, const char *del)
  130. {
  131. char *s = strtok(str, del);
  132. while(s != NULL)
  133. {
  134. *arr++ = s;
  135. s = strtok(NULL, del);
  136. }
  137. }
  138. int StoreLogMsg(const char *fmt, ...)
  139. {
  140. char Buf[4096+256];
  141. char buffer[4096];
  142. time_t CurrentTime;
  143. struct tm *tm;
  144. struct timeval tv;
  145. va_list args;
  146. va_start(args, fmt);
  147. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  148. va_end(args);
  149. memset(Buf,0,sizeof(Buf));
  150. CurrentTime = time(NULL);
  151. tm=localtime(&CurrentTime);
  152. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  153. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  154. {
  155. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  156. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  157. buffer,
  158. tm->tm_year+1900,tm->tm_mon+1,
  159. ShmSysConfigAndInfo->SysConfig.ModelName,
  160. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  161. }
  162. else
  163. {
  164. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  165. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  166. buffer,
  167. tm->tm_year+1900,tm->tm_mon+1);
  168. }
  169. #ifdef SystemLogMessage
  170. system(Buf);
  171. #endif
  172. #ifdef ConsloePrintLog
  173. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  174. #endif
  175. return rc;
  176. }
  177. void refreshStartTimer(struct timespec *timer)
  178. {
  179. clock_gettime(CLOCK_MONOTONIC, timer);
  180. }
  181. int getDiffSecNow(struct timespec timer)
  182. {
  183. struct timespec timerNow;
  184. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  185. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  186. }
  187. int getDiffSecBetween(struct timespec start, struct timespec end)
  188. {
  189. return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
  190. }
  191. long long DiffTimebWithNow(struct timeb ST)
  192. {
  193. //return milli-second
  194. struct timeb ET;
  195. long long StartTime,StopTime;
  196. ftime(&ET);
  197. StartTime=(long long)ST.time;
  198. StopTime=(long long)ET.time;
  199. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  200. }
  201. long long DiffTimeb(struct timeb ST, struct timeb ET)
  202. {
  203. //return milli-second
  204. long long StartTime,StopTime;
  205. StartTime=(long long)ST.time;
  206. StopTime=(long long)ET.time;
  207. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  208. }
  209. int DiffTimebWithNowSec(struct timeb ST)
  210. {
  211. //return milli-second
  212. struct timeb ET;
  213. unsigned int StartTime,StopTime;
  214. ftime(&ET);
  215. StartTime=(unsigned int)ST.time;
  216. StopTime=(unsigned int)ET.time;
  217. return (StopTime-StartTime);
  218. }
  219. int isOvertNow(uint8_t *start)
  220. {
  221. int result = YES;
  222. struct ParsingResult
  223. {
  224. int result;
  225. int scanedElement;
  226. int year;
  227. int month;
  228. int mday;
  229. int hour;
  230. int min;
  231. int sec;
  232. int tz_hour;
  233. int tz_min;
  234. float minSec;
  235. }parsingResult;
  236. struct tm tmStart;
  237. struct timeb tbStart;
  238. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  239. if(strstr((char*)start, ".") != NULL)
  240. {
  241. // Original data with mini second
  242. if(strstr((char*)start, "Z") != NULL)
  243. {
  244. // Original data with Z
  245. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  246. &parsingResult.year,
  247. &parsingResult.month,
  248. &parsingResult.mday,
  249. &parsingResult.hour,
  250. &parsingResult.min,
  251. &parsingResult.sec,
  252. &parsingResult.minSec);
  253. }
  254. else
  255. {
  256. // Original data without Z
  257. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  258. &parsingResult.year,
  259. &parsingResult.month,
  260. &parsingResult.mday,
  261. &parsingResult.hour,
  262. &parsingResult.min,
  263. &parsingResult.sec,
  264. &parsingResult.minSec,
  265. &parsingResult.tz_hour,
  266. &parsingResult.tz_min);
  267. }
  268. }
  269. else
  270. {
  271. // Original data without mini second
  272. if(strstr((char*)start, "Z") != NULL)
  273. {
  274. // Original data with Z
  275. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  276. &parsingResult.year,
  277. &parsingResult.month,
  278. &parsingResult.mday,
  279. &parsingResult.hour,
  280. &parsingResult.min,
  281. &parsingResult.sec);
  282. }
  283. else
  284. {
  285. // Original data without Z
  286. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  287. &parsingResult.year,
  288. &parsingResult.month,
  289. &parsingResult.mday,
  290. &parsingResult.hour,
  291. &parsingResult.min,
  292. &parsingResult.sec,
  293. &parsingResult.tz_hour,
  294. &parsingResult.tz_min);
  295. }
  296. }
  297. if(parsingResult.scanedElement >= 6)
  298. {
  299. tmStart.tm_year = parsingResult.year - 1900;
  300. tmStart.tm_mon = parsingResult.month - 1;
  301. tmStart.tm_mday = parsingResult.mday;
  302. tmStart.tm_hour = parsingResult.hour;
  303. tmStart.tm_min = parsingResult.min;
  304. tmStart.tm_sec = parsingResult.sec;
  305. tmStart.tm_gmtoff = 0;
  306. tbStart.time = mktime(&tmStart);
  307. tbStart.timezone = 0;
  308. tbStart.millitm = 0;
  309. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  310. if(DiffTimebWithNowSec(tbStart) <= 0)
  311. result = NO;
  312. else
  313. result = YES;
  314. }
  315. else
  316. {
  317. DEBUG_WARN("Start date parsing error.\n");
  318. }
  319. return result;
  320. }
  321. void getDateTimeString(char* result)
  322. {
  323. time_t CurrentTime;
  324. struct tm *tm;
  325. CurrentTime = time(NULL);
  326. tm=localtime(&CurrentTime);
  327. sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  328. }
  329. int getCurrentYear()
  330. {
  331. int result = 0;
  332. time_t CurrentTime;
  333. struct tm *tm;
  334. CurrentTime = time(NULL);
  335. tm=localtime(&CurrentTime);
  336. result = (tm->tm_year + 1900);
  337. return result;
  338. }
  339. unsigned long long getAvailableMemory()
  340. {
  341. long pages = sysconf(_SC_AVPHYS_PAGES);
  342. long page_size = sysconf(_SC_PAGE_SIZE);
  343. return pages * page_size;
  344. }
  345. unsigned int isKernelSupportNAT()
  346. {
  347. unsigned int result = NO;
  348. unsigned int version = 0;
  349. FILE *fp;
  350. char cmd[256];
  351. char buf[512];
  352. // Get IP address & net mask
  353. strcpy(cmd, "uname -v");
  354. fp = popen(cmd, "r");
  355. if(fp != NULL)
  356. {
  357. if(fgets(buf, sizeof(buf), fp) != NULL)
  358. {
  359. sscanf(buf, "#%d", &version);
  360. //DEBUG_INFO("Kernel version: %d\n", version);
  361. if(version >= 30)
  362. result = YES;
  363. }
  364. }
  365. pclose(fp);
  366. return result;
  367. }
  368. int getEth0MacAddress()
  369. {
  370. int result = PASS;
  371. FILE *fp;
  372. char cmd[256];
  373. char buf[512];
  374. char tmp[512];
  375. strcpy(cmd, "ifconfig eth0");
  376. fp = popen(cmd, "r");
  377. if(fp != NULL)
  378. {
  379. while(fgets(buf, sizeof(buf), fp) != NULL)
  380. {
  381. if(strstr(buf, "eth0") > 0)
  382. {
  383. result = PASS;
  384. }
  385. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  386. {
  387. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  388. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  389. }
  390. }
  391. }
  392. pclose(fp);
  393. return result;
  394. }
  395. char* getSystemModeName(unsigned char mode)
  396. {
  397. char* result;
  398. switch(mode)
  399. {
  400. case SYS_MODE_BOOTING:
  401. result = "booting";
  402. break;
  403. case SYS_MODE_IDLE:
  404. result = "idle";
  405. break;
  406. case SYS_MODE_AUTHORIZING:
  407. result = "authorizing";
  408. break;
  409. case SYS_MODE_PREPARING:
  410. result = "preparing";
  411. break;
  412. case SYS_MODE_CHARGING:
  413. result = "charging";
  414. break;
  415. case SYS_MODE_TERMINATING:
  416. result = "terminating";
  417. break;
  418. case SYS_MODE_COMPLETE:
  419. result = "complete";
  420. break;
  421. case SYS_MODE_ALARM:
  422. result = "alarm";
  423. break;
  424. case SYS_MODE_FAULT:
  425. result = "fault";
  426. break;
  427. case SYS_MODE_MAINTAIN:
  428. result = "maintain";
  429. break;
  430. case SYS_MODE_RESERVATION:
  431. result = "reservation";
  432. break;
  433. case SYS_MODE_BOOKING:
  434. result = "booking";
  435. break;
  436. case SYS_MODE_DEBUG:
  437. result = "debug";
  438. break;
  439. case SYS_MODE_UPDATE:
  440. result = "upgrade";
  441. break;
  442. default:
  443. result = "unknown";
  444. break;
  445. }
  446. return result;
  447. }
  448. void setChargerMode(unsigned char gun_index, unsigned char mode)
  449. {
  450. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  451. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  452. DEBUG_INFO("Gun-%02d mode switch from %s to %s\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
  453. }
  454. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  455. {
  456. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  457. }
  458. unsigned char isModeChange(unsigned char gun_index)
  459. {
  460. unsigned char result = NO;
  461. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  462. {
  463. result = YES;
  464. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  465. }
  466. return result;
  467. }
  468. int isGpioInitialized(unsigned int gpio)
  469. {
  470. int result = FAIL;
  471. char gpioPath[64];
  472. sprintf(gpioPath, "/sys/class/gpio/gpio%d/value", gpio);
  473. if((access(gpioPath, F_OK)) != -1)
  474. {
  475. result = PASS;
  476. }
  477. return result;
  478. }
  479. void gpio_set_value(unsigned int gpio, unsigned int value)
  480. {
  481. int fd;
  482. char buf[256];
  483. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  484. fd = open(buf, O_WRONLY);
  485. if (fd < 0)
  486. {
  487. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  488. return;
  489. }
  490. if (value)
  491. write(fd, "1", 2);
  492. else
  493. write(fd, "0", 2);
  494. close(fd);
  495. }
  496. int gpio_get_value(unsigned int gpio)
  497. {
  498. int fd;
  499. char buf[256];
  500. char ch;
  501. int8_t result = FAIL;
  502. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  503. fd = open(buf, O_RDONLY);
  504. if (fd < 0)
  505. {
  506. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  507. return result;
  508. }
  509. read(fd, &ch, 1);
  510. if (ch != '0')
  511. result = 1;
  512. else
  513. result = 0;
  514. close(fd);
  515. return result;
  516. }
  517. int presentChargedEnergyClear(unsigned char gun_index)
  518. {
  519. int result = FAIL;
  520. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  521. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  522. result = PASS;
  523. return result;
  524. }
  525. float presentChargedEnergyTotal(unsigned char gun_index)
  526. {
  527. float result = 0.0f;
  528. for(int idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod);idx++)
  529. {
  530. result += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idx];
  531. }
  532. return result;
  533. }
  534. int presentChargedEnergyUpdate(unsigned char gun_index)
  535. {
  536. int result = FAIL;
  537. time_t CurrentTime;
  538. struct tm *tm;
  539. CurrentTime = time(NULL);
  540. tm=localtime(&CurrentTime);
  541. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  542. {
  543. // Resolution: 0.0001 kwh
  544. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += (((float)(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start))/10000.0) - presentChargedEnergyTotal(gun_index);
  545. }
  546. else
  547. {
  548. // Resolution: 0.0001 kwh
  549. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += ((((float)(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start))/10000.0) +
  550. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/10000.0) +
  551. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/10000.0))
  552. - presentChargedEnergyTotal(gun_index);
  553. }
  554. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = presentChargedEnergyTotal(gun_index);
  555. return result;
  556. }
  557. //======================================================
  558. // OCPP routine
  559. //======================================================
  560. void ocpp_process_start()
  561. {
  562. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  563. system("/root/OcppBackend &");
  564. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  565. system("/root/OcppBackend20 &");
  566. }
  567. uint8_t ocpp_get_isRemoteStartWait()
  568. {
  569. uint8_t result = OFF;
  570. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  571. {
  572. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  573. }
  574. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  575. {
  576. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  577. }
  578. return result;
  579. }
  580. uint8_t ocpp_get_connection_status()
  581. {
  582. uint8_t result = OFF;
  583. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  584. {
  585. result = ShmOCPP16Data->OcppConnStatus;
  586. }
  587. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  588. {
  589. result = ShmOCPP20Data->OcppConnStatus;
  590. }
  591. return result;
  592. }
  593. uint16_t ocpp_get_connection_timeout()
  594. {
  595. uint16_t result = TIMEOUT_SPEC_HANDSHAKING;
  596. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  597. {
  598. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  599. {
  600. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  601. }
  602. }
  603. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  604. {
  605. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  606. {
  607. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  608. }
  609. }
  610. return result;
  611. }
  612. uint8_t ocpp_get_update_firmware_req()
  613. {
  614. uint8_t result = NO;
  615. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  616. {
  617. result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq;
  618. }
  619. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  620. {
  621. result = ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq;
  622. }
  623. return result;
  624. }
  625. uint8_t ocpp_get_reset_req()
  626. {
  627. uint8_t result = NO;
  628. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  629. {
  630. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  631. }
  632. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  633. {
  634. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  635. }
  636. return result;
  637. }
  638. void ocpp_boot_info_sync()
  639. {
  640. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  641. {
  642. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  643. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  644. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  645. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  646. switch(ShmSysConfigAndInfo->SysConfig.ModelName[3])
  647. {
  648. case 'M':
  649. case 'Z':
  650. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "MID");
  651. break;
  652. case 'P':
  653. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
  654. break;
  655. case 'I':
  656. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
  657. break;
  658. case 'U':
  659. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "UL");
  660. break;
  661. default:
  662. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
  663. break;
  664. }
  665. }
  666. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  667. {
  668. memcpy((char*)ShmOCPP20Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  669. memcpy((char*)ShmOCPP20Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  670. sprintf((char*)ShmOCPP20Data->BootNotification.chargingStation.firmwareVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  671. }
  672. }
  673. void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
  674. {
  675. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  676. {
  677. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
  678. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
  679. }
  680. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  681. {
  682. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq != status)
  683. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = status;
  684. }
  685. }
  686. void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
  687. {
  688. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  689. {
  690. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
  691. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
  692. }
  693. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  694. {
  695. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq != status)
  696. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = status;
  697. }
  698. }
  699. uint8_t ocpp_get_remotestart(uint8_t gun_index)
  700. {
  701. uint8_t result = OFF;
  702. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  703. {
  704. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  705. }
  706. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  707. {
  708. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  709. }
  710. return result;
  711. }
  712. void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
  713. {
  714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  715. {
  716. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  717. }
  718. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  719. {
  720. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  721. }
  722. }
  723. uint8_t ocpp_get_remotestop(uint8_t gun_index)
  724. {
  725. uint8_t result = OFF;
  726. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  727. {
  728. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
  729. }
  730. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  731. {
  732. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq;
  733. }
  734. return result;
  735. }
  736. void ocpp_set_auth_req(uint8_t status, ...)
  737. {
  738. va_list args;
  739. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  740. {
  741. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
  742. {
  743. if(status == ON)
  744. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  745. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
  746. }
  747. }
  748. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  749. {
  750. if(ShmOCPP20Data->SpMsg.bits.AuthorizeReq != status)
  751. {
  752. if(status == ON)
  753. {
  754. memset(&ShmOCPP20Data->Authorize.Response_idTokenInfo, 0x00, sizeof(struct IdTokenInfoType));
  755. va_start(args, status);
  756. sprintf((char*)ShmOCPP20Data->Authorize.idToken.type, "%s", va_arg(args, char*));
  757. va_end(args);
  758. }
  759. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = status;
  760. }
  761. }
  762. }
  763. uint8_t ocpp_get_auth_req()
  764. {
  765. uint8_t result = OFF;
  766. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  767. {
  768. result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
  769. }
  770. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  771. {
  772. result = ShmOCPP20Data->SpMsg.bits.AuthorizeReq;
  773. }
  774. return result;
  775. }
  776. void ocpp_set_auth_conf(uint8_t status)
  777. {
  778. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  779. {
  780. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
  781. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
  782. }
  783. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  784. {
  785. if(ShmOCPP20Data->SpMsg.bits.AuthorizeConf != status)
  786. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = status;
  787. }
  788. }
  789. uint8_t ocpp_get_auth_conf()
  790. {
  791. uint8_t result = OFF;
  792. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  793. {
  794. result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  795. }
  796. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  797. {
  798. result = ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  799. }
  800. return result;
  801. }
  802. uint8_t ocpp_get_auth_result(uint8_t isValidParent, ...)
  803. {
  804. uint8_t result = OFF;
  805. va_list args;
  806. uint8_t gun_index;
  807. if(isValidParent)
  808. {
  809. va_start(args, isValidParent);
  810. gun_index = va_arg(args, int);
  811. va_end(args);
  812. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  813. {
  814. case SYS_MODE_AUTHORIZING:
  815. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  816. {
  817. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0))
  818. result = PASS;
  819. }
  820. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  821. {
  822. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0))
  823. result = PASS;
  824. }
  825. break;
  826. case SYS_MODE_CHARGING:
  827. case SYS_MODE_TERMINATING:
  828. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  829. {
  830. /*
  831. DEBUG_INFO("==========================================\n");
  832. DEBUG_INFO("=== OCPP GETTING AUTHORIZE RESULT 1.6 ====\n");
  833. DEBUG_INFO("==========================================\n");
  834. DEBUG_INFO("Authorize.ResponseIdTagInfo.ParentIdTag : %s \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  835. DEBUG_INFO("StartTransaction[%d].ResponseIdTagInfo.ParentIdTag : %s \n", gun_index ,ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag);
  836. */
  837. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  838. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0))
  839. result = PASS;
  840. }
  841. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  842. {
  843. /*
  844. DEBUG_INFO("==========================================\n");
  845. DEBUG_INFO("=== OCPP GETTING AUTHORIZE RESULT 2.0 ====\n");
  846. DEBUG_INFO("==========================================\n");
  847. DEBUG_INFO("Authorize.Response_idTokenInfo.groupIdToken.idToken : %s \n", ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken);
  848. DEBUG_INFO("TransactionEvent[%d].Response_idTokenInfo.groupIdToken.idToken : %s \n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken);
  849. */
  850. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
  851. (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken)==0))
  852. result = PASS;
  853. }
  854. break;
  855. default:
  856. break;
  857. }
  858. }
  859. else
  860. {
  861. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  862. {
  863. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0))
  864. result = PASS;
  865. }
  866. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  867. {
  868. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0))
  869. result = PASS;
  870. }
  871. }
  872. //DEBUG_INFO("Authorize result : %s \n", ((result == PASS)?"Pass":"Fail"));
  873. return result;
  874. }
  875. uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
  876. {
  877. uint8_t result = OFF;
  878. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  879. {
  880. result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor;
  881. }
  882. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  883. {
  884. result = ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor;
  885. }
  886. return result;
  887. }
  888. void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
  889. {
  890. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  891. {
  892. if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor != status)
  893. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = status;
  894. }
  895. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  896. {
  897. if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor != status)
  898. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = status;
  899. }
  900. }
  901. void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
  902. {
  903. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  904. {
  905. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
  906. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
  907. }
  908. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  909. {
  910. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  911. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  912. }
  913. }
  914. void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
  915. {
  916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  917. {
  918. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
  919. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
  920. }
  921. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  922. {
  923. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  924. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  925. }
  926. }
  927. void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
  928. {
  929. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  930. {
  931. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
  932. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
  933. }
  934. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  935. {
  936. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  937. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  938. }
  939. }
  940. void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
  941. {
  942. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  943. {
  944. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
  945. {
  946. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
  947. }
  948. }
  949. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  950. {
  951. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  952. {
  953. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  954. }
  955. }
  956. }
  957. void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
  958. {
  959. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  960. {
  961. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  962. }
  963. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  964. {
  965. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  966. }
  967. }
  968. uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
  969. {
  970. uint8_t result = PASS;
  971. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  972. {
  973. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  974. {
  975. if((strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked")==0) ||
  976. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired")==0) ||
  977. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid")==0))
  978. result = NO;
  979. }
  980. }
  981. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  982. {
  983. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  984. {
  985. if((strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Blocked")==0) ||
  986. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Expired")==0) ||
  987. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Invalid")==0) ||
  988. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NoCredit")==0) ||
  989. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAllowedTypeEVSE")==0) ||
  990. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisLocation")==0) ||
  991. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisTime")==0) ||
  992. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Unknown")==0))
  993. result = NO;
  994. }
  995. }
  996. return result;
  997. }
  998. uint8_t ocpp_get_maxcharging_profileId()
  999. {
  1000. uint8_t result = 0;
  1001. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1002. {
  1003. result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
  1004. }
  1005. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1006. {
  1007. result = ShmOCPP20Data->MaxChargingProfile.id;
  1008. }
  1009. return result;
  1010. }
  1011. uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
  1012. {
  1013. uint8_t result = 0;
  1014. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1015. {
  1016. result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
  1017. }
  1018. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1019. {
  1020. result = ShmOCPP20Data->SmartChargingProfile[gun_index].id;
  1021. }
  1022. return result;
  1023. }
  1024. void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
  1025. {
  1026. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1027. {
  1028. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  1029. }
  1030. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1031. {
  1032. ShmOCPP20Data->SmartChargingProfile[gun_index].id = 0;
  1033. }
  1034. }
  1035. uint8_t ocpp_get_profile_req(uint8_t gun_index)
  1036. {
  1037. uint8_t result = OFF;
  1038. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1039. {
  1040. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  1041. }
  1042. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1043. {
  1044. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  1045. }
  1046. return result;
  1047. }
  1048. void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
  1049. {
  1050. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1051. {
  1052. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  1053. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  1054. }
  1055. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1056. {
  1057. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  1058. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  1059. }
  1060. }
  1061. uint8_t ocpp_get_profile_conf(uint8_t gun_index)
  1062. {
  1063. uint8_t result = OFF;
  1064. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1065. {
  1066. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  1067. }
  1068. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1069. {
  1070. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  1071. }
  1072. return result;
  1073. }
  1074. void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
  1075. {
  1076. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1077. {
  1078. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  1079. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  1080. }
  1081. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1082. {
  1083. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  1084. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  1085. }
  1086. }
  1087. uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
  1088. {
  1089. uint8_t result = OFF;
  1090. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1091. {
  1092. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
  1093. result = ON;
  1094. }
  1095. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1096. {
  1097. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnEVSideDisconnect].variableAttribute[0].value, "TRUE") == 0)
  1098. result = ON;
  1099. }
  1100. return result;
  1101. }
  1102. uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
  1103. {
  1104. uint8_t result = OFF;
  1105. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1106. {
  1107. result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
  1108. }
  1109. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1110. {
  1111. result = ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq;
  1112. }
  1113. return result;
  1114. }
  1115. void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
  1116. {
  1117. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1118. {
  1119. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1120. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1121. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1122. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1123. }
  1124. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1125. {
  1126. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1127. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1128. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1129. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1130. }
  1131. }
  1132. uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
  1133. {
  1134. uint8_t result = OFF;
  1135. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1136. {
  1137. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  1138. result = ON;
  1139. }
  1140. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1141. {
  1142. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1143. result = ON;
  1144. }
  1145. return result;
  1146. }
  1147. uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
  1148. {
  1149. uint8_t result = OFF;
  1150. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1151. {
  1152. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  1153. {
  1154. result = ON;
  1155. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  1156. }
  1157. }
  1158. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1159. {
  1160. if(strcmp((char*)ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1161. {
  1162. result = ON;
  1163. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1164. }
  1165. }
  1166. return result;
  1167. }
  1168. uint8_t ocpp_isAuthorizeRemoteStart()
  1169. {
  1170. uint8_t result = NO;
  1171. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1172. {
  1173. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  1174. {
  1175. result = YES;
  1176. }
  1177. }
  1178. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1179. {
  1180. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE"))
  1181. {
  1182. result = YES;
  1183. }
  1184. }
  1185. return result;
  1186. }
  1187. uint8_t ocpp_get_freevend_idtag(uint8_t * userId)
  1188. {
  1189. uint8_t result = NO;
  1190. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1191. {
  1192. strcpy((char*)userId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  1193. result = YES;
  1194. }
  1195. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1196. {
  1197. strcpy((char*)userId, (char*)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
  1198. result = YES;
  1199. }
  1200. return result;
  1201. }
  1202. //======================================================
  1203. // Check interface status
  1204. //======================================================
  1205. int isInterfaceUp(const char *interface)
  1206. {
  1207. int result = FAIL;
  1208. FILE *fp;
  1209. char cmd[256];
  1210. char buf[512];
  1211. strcpy(cmd, "ifconfig");;
  1212. fp = popen(cmd, "r");
  1213. if(fp != NULL)
  1214. {
  1215. while(fgets(buf, sizeof(buf), fp) != NULL)
  1216. {
  1217. if(strstr(buf, interface) > 0)
  1218. {
  1219. result = PASS;
  1220. }
  1221. }
  1222. }
  1223. pclose(fp);
  1224. return result;
  1225. }
  1226. //======================================================
  1227. // Create all share memory
  1228. //======================================================
  1229. int CreatShareMemory()
  1230. {
  1231. int result = PASS;
  1232. int MeterSMId;
  1233. //creat ShmSysConfigAndInfo
  1234. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  1235. {
  1236. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1237. result = FAIL;
  1238. }
  1239. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1240. {
  1241. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  1242. result = FAIL;
  1243. }
  1244. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  1245. //creat ShmPsuData
  1246. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  1247. {
  1248. DEBUG_ERROR("shmget ShmPsuData NG\n");
  1249. result = FAIL;
  1250. }
  1251. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1252. {
  1253. DEBUG_ERROR("shmat ShmPsuData NG\n");
  1254. result = FAIL;
  1255. }
  1256. else
  1257. {}
  1258. //creat ShmStatusCodeData
  1259. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  1260. {
  1261. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1262. result = FAIL;
  1263. }
  1264. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1265. {
  1266. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1267. result = FAIL;
  1268. }
  1269. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  1270. //creat ShmPrimaryMcuData
  1271. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  1272. {
  1273. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1274. result = FAIL;
  1275. }
  1276. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1277. {
  1278. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1279. result = FAIL;
  1280. }
  1281. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  1282. //creat ShmOCPP16Data
  1283. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  1284. {
  1285. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  1286. result = FAIL;
  1287. }
  1288. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1289. {
  1290. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  1291. result = FAIL;
  1292. }
  1293. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  1294. //creat ShmOCPP20Data
  1295. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  1296. {
  1297. DEBUG_ERROR("shmget OCPP20Data NG\n");
  1298. result = FAIL;
  1299. }
  1300. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1301. {
  1302. DEBUG_ERROR("shmat OCPP20Data NG\n");
  1303. result = FAIL;
  1304. }
  1305. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  1306. //creat ShmCharger
  1307. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  1308. {
  1309. DEBUG_ERROR("shmget ShmCharger NG\n");
  1310. result = FAIL;
  1311. }
  1312. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1313. {
  1314. DEBUG_ERROR("shmat ShmCharger NG\n");
  1315. result = FAIL;
  1316. }
  1317. memset(ShmCharger,0,sizeof(struct Charger));
  1318. //creat ShmDispenser
  1319. if ((MeterSMId = shmget(ShmDispenserKey, sizeof(struct DISPENSER), IPC_CREAT | 0777)) < 0)
  1320. {
  1321. DEBUG_ERROR("shmget ShmDispenser NG\n");
  1322. result = FAIL;
  1323. }
  1324. else if ((ShmDispenser = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1325. {
  1326. DEBUG_ERROR("shmat ShmDispenser NG\n");
  1327. result = FAIL;
  1328. }
  1329. memset(ShmDispenser,0,sizeof(struct DISPENSER));
  1330. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1331. return result;
  1332. }
  1333. //======================================================
  1334. // Call WEBAPI to mail receipt (Only for CDFA verify)
  1335. //======================================================
  1336. #ifdef CDFA_CERTIFICATE
  1337. int sendReceiptMail(char *startDateTime, char *stopDateTime, char *receiptInfo)
  1338. {
  1339. int result = FAIL;
  1340. char reciever[128];
  1341. char subject[128];
  1342. char content[8192];
  1343. char priceDetail[4098]={0};
  1344. char cmdBuf[8192];
  1345. double totalEnergy;
  1346. double totalPrice;
  1347. json_object *detailPrice = json_tokener_parse(receiptInfo);
  1348. if(!is_error(detailPrice))
  1349. {
  1350. if(json_object_object_get(detailPrice, "totalChargedEnergy") != NULL)
  1351. totalEnergy = json_object_get_double(json_object_object_get(detailPrice, "totalChargedEnergy"));
  1352. if(json_object_object_get(detailPrice, "totalPrice") != NULL)
  1353. totalPrice = json_object_get_double(json_object_object_get(detailPrice, "totalPrice"));
  1354. if(json_object_object_get(detailPrice, "receiptDetail") != NULL)
  1355. {
  1356. for(int idx=0;idx<json_object_array_length(json_object_object_get(detailPrice, "receiptDetail"));idx++)
  1357. {
  1358. sprintf(priceDetail, "%s---%02d:00, %.4f KWH @ $ %.2f = $ %.2f<br/>\n", priceDetail
  1359. , json_object_get_int(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "hour"))
  1360. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "chargedEnergy"))
  1361. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "unitPrice"))
  1362. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "periodPrice")));
  1363. }
  1364. }
  1365. }
  1366. json_object_put(detailPrice);
  1367. // Generate mail content
  1368. sprintf(reciever, "folus_wen@phihong.com.tw");
  1369. sprintf(subject, "Charging Receipt From %s", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1370. sprintf(content, "<img src=\"https://www.phihong.com/images/logo.png\"/><br/>\n"
  1371. "<h1>Receipt of charging on %s</h1><br/>\n"
  1372. "<b>Location Name:</b> CDFA Lab<br/>\n"
  1373. "<b>Location Address:</b> xxxxxxxxxxx<br/>\n"
  1374. "<b>EVSE ID:</b> %s<br/>\n"
  1375. "<b>Maximum Rate of Energy Transfer:</b> 11.5 KW AC<br/>\n"
  1376. "<b>Charging start date time:</b> %s<br/>\n"
  1377. "<b>Charging stop date time:</b> %s<br/>\n"
  1378. "<b>Energy Delivered:</b> %.4f KWH<br/>\n"
  1379. "<b>Cost of Charging:</b><br/>\n"
  1380. "%s"
  1381. "***Total charged cost: $ %.2f<br/>\n"
  1382. "<br/>\n"
  1383. "<br/>\n"
  1384. "%s<br/>\n"
  1385. "47800 Fremont Blvd.<br/>\n"
  1386. "Fremont, CA 94538, USA<br/>\n", startDateTime
  1387. , ShmSysConfigAndInfo->SysConfig.ChargeBoxId
  1388. , startDateTime
  1389. , stopDateTime
  1390. , totalEnergy
  1391. , priceDetail
  1392. , totalPrice
  1393. , ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1394. json_object *post = json_object_new_object();
  1395. json_object_object_add(post, "Receiver", json_object_new_string(reciever));
  1396. json_object_object_add(post, "Subject", json_object_new_string(subject));
  1397. json_object_object_add(post, "Body", json_object_new_string(content));
  1398. json_object_object_add(post, "UseHtml", json_object_new_boolean(1));
  1399. // Call WEBAPI
  1400. sprintf(cmdBuf, "curl -X POST -k -d '%s' -H \"Content-Type: application/json\" https://ocpp.phihong.com.tw:5014/api/Notification/ &", json_object_to_json_string_ext(post, JSON_C_TO_STRING_PLAIN));
  1401. system(cmdBuf);
  1402. //DEBUG_INFO("cmdBuf: %s\n", cmdBuf);
  1403. json_object_put(post);
  1404. return result;
  1405. }
  1406. #endif /* CDFA_CERTIFICATE */
  1407. //======================================================
  1408. // SQLite3 related routine
  1409. //======================================================
  1410. int getReceiptInfo(uint8_t gun_index, char *receiptInfo)
  1411. {
  1412. int result = PASS;
  1413. double totalEnergy=0.0;
  1414. double totalPrice=0.0;
  1415. json_object *receipt = json_object_new_object();
  1416. json_object *receiptAry = json_object_new_array();
  1417. for(uint8_t idxHour=0;idxHour<ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod);idxHour++)
  1418. {
  1419. json_object *receiptDetail = json_object_new_object();
  1420. json_object_object_add(receiptDetail, "hour", json_object_new_int(idxHour));
  1421. json_object_object_add(receiptDetail, "chargedEnergy", json_object_new_double(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]));
  1422. json_object_object_add(receiptDetail, "unitPrice", json_object_new_double(ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1423. json_object_object_add(receiptDetail, "periodPrice", json_object_new_double(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1424. totalEnergy += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour];
  1425. totalPrice += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour];
  1426. json_object_array_add(receiptAry, receiptDetail);
  1427. }
  1428. json_object_object_add(receipt, "receiptDetail", receiptAry);
  1429. json_object_object_add(receipt, "totalChargedEnergy", json_object_new_double(totalEnergy));
  1430. json_object_object_add(receipt, "totalPrice", json_object_new_double(totalPrice));
  1431. json_object_object_add(receipt, "serviceProvider", json_object_new_string((char*)ShmSysConfigAndInfo->SysConfig.chargePointVendor));
  1432. json_object_object_add(receipt, "serviceProviderAddress", json_object_new_string("Address"));
  1433. json_object_object_add(receipt, "serviceProviderTel", json_object_new_string("TEL"));
  1434. json_object_object_add(receipt, "chargerBoxId", json_object_new_string((char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1435. sprintf(receiptInfo, "%s", json_object_to_json_string_ext(receipt, JSON_C_TO_STRING_PLAIN));
  1436. json_object_put(receipt);
  1437. //DEBUG_INFO("receiptInfo: %s\n", receiptInfo);
  1438. return result;
  1439. }
  1440. int DB_Open(sqlite3 *db)
  1441. {
  1442. int result = PASS;
  1443. char* errMsg = NULL;
  1444. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  1445. "idx integer primary key AUTOINCREMENT, "
  1446. "reservationId text, "
  1447. "transactionId text, "
  1448. "startMethod text, "
  1449. "userId text, "
  1450. "dateTimeStart text, "
  1451. "dateTimeStop text,"
  1452. "socStart text, "
  1453. "socStop text, "
  1454. "chargeEnergy text, "
  1455. "stopReason text, "
  1456. "finalCost text"
  1457. ");";
  1458. char* createRecordBufSql="CREATE TABLE IF NOT EXISTS charging_record_buffer("
  1459. "reservationId text, "
  1460. "transactionId text, "
  1461. "startMethod text, "
  1462. "userId text, "
  1463. "dateTimeStart text, "
  1464. "dateTimeStop text,"
  1465. "socStart text, "
  1466. "socStop text, "
  1467. "chargeEnergy text, "
  1468. "stopReason text, "
  1469. "finalCost text"
  1470. ");";
  1471. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  1472. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  1473. "`item` TEXT NOT NULL, "
  1474. "`connector` INTEGER NOT NULL, "
  1475. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  1476. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1477. if(sqlite3_open(DB_FILE, &db))
  1478. {
  1479. result = FAIL;
  1480. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1481. sqlite3_close(db);
  1482. }
  1483. else
  1484. {
  1485. DEBUG_INFO( "Local charging record database open successfully.\n");
  1486. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  1487. {
  1488. result = FAIL;
  1489. DEBUG_ERROR( "Create local charging record table error message: %s\n", errMsg);
  1490. }
  1491. else
  1492. {
  1493. DEBUG_INFO( "Opened local charging record table successfully\n");
  1494. }
  1495. if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
  1496. {
  1497. result = FAIL;
  1498. DEBUG_ERROR( "Create local charging record buffer table error message: %s\n", errMsg);
  1499. }
  1500. else
  1501. {
  1502. DEBUG_INFO( "Opened local charging record buffer table successfully\n");
  1503. }
  1504. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  1505. {
  1506. result = FAIL;
  1507. DEBUG_ERROR( "Create local config table error message: %s\n", errMsg);
  1508. }
  1509. else
  1510. {
  1511. DEBUG_INFO( "Opened local config table successfully\n");
  1512. }
  1513. sqlite3_close(db);
  1514. }
  1515. return result;
  1516. }
  1517. int DB_Check_Record_Buf(sqlite3 *db, int gun_index)
  1518. {
  1519. int result = PASS;
  1520. char* errMsg = NULL;
  1521. char sqlStr[4096];
  1522. char **rs;
  1523. int rows, cols;
  1524. if(sqlite3_open(DB_FILE, &db))
  1525. {
  1526. result = FAIL;
  1527. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1528. sqlite3_close(db);
  1529. }
  1530. else
  1531. {
  1532. sqlite3_get_table(db, "select * from charging_record_buffer", &rs, &rows, &cols, &errMsg);
  1533. if(rows>0)
  1534. {
  1535. // Copy record buffer
  1536. for(int idxRow=1;idxRow<=rows;idxRow++)
  1537. {
  1538. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1539. {
  1540. result = false;
  1541. }
  1542. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1543. "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
  1544. rs[(idxRow*cols)+0],
  1545. rs[(idxRow*cols)+1],
  1546. rs[(idxRow*cols)+2],
  1547. rs[(idxRow*cols)+3],
  1548. rs[(idxRow*cols)+4],
  1549. rs[(idxRow*cols)+5],
  1550. rs[(idxRow*cols)+6],
  1551. rs[(idxRow*cols)+7],
  1552. rs[(idxRow*cols)+8],
  1553. rs[(idxRow*cols)+9],
  1554. rs[(idxRow*cols)+10]);
  1555. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1556. {
  1557. result = FAIL;
  1558. DEBUG_INFO( "Copy local charging record buffer error message: %s\n", errMsg);
  1559. }
  1560. else
  1561. {
  1562. DEBUG_INFO( "Copy local charging record buffer successfully\n");
  1563. }
  1564. #ifdef CDFA_CERTIFICATE
  1565. // Only for CDFA certificate
  1566. sendReceiptMail(rs[(idxRow*cols)+4],
  1567. rs[(idxRow*cols)+5],
  1568. rs[(idxRow*cols)+10]);
  1569. #endif /* CDFA_CERTIFICATE */
  1570. }
  1571. // Delete buffer
  1572. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1573. {
  1574. result = FAIL;
  1575. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1576. }
  1577. else
  1578. {
  1579. DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1580. }
  1581. }
  1582. else
  1583. {
  1584. DEBUG_INFO("There is not any charging record buffer.\n", gun_index);
  1585. }
  1586. sqlite3_free_table(rs);
  1587. sqlite3_close(db);
  1588. }
  1589. return result;
  1590. }
  1591. int DB_Update_Record_Buf(sqlite3 *db, int gun_index)
  1592. {
  1593. int result = PASS;
  1594. char* errMsg = NULL;
  1595. char sqlStr[4096];
  1596. char receiptInfo[2048];
  1597. getReceiptInfo(gun_index, receiptInfo);
  1598. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1599. {
  1600. sprintf(sqlStr, "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1601. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1602. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1603. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1604. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1605. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1606. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1607. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1608. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1609. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1610. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1611. "Power Loss",
  1612. #ifdef CDFA_CERTIFICATE
  1613. receiptInfo);// Only for CDFA certificate
  1614. #else
  1615. ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  1616. #endif /* CDFA_CERTIFICATE */
  1617. }
  1618. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1619. {
  1620. sprintf(sqlStr, "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1621. #ifdef CDFA_CERTIFICATE
  1622. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1623. #else
  1624. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
  1625. #endif /* CDFA_CERTIFICATE */
  1626. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1627. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1628. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1629. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1630. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1631. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1632. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1633. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1634. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1635. "Power Loss",
  1636. #ifdef CDFA_CERTIFICATE
  1637. //receiptInfo);// Only for CDFA certificate
  1638. #else
  1639. ShmOCPP20Data->CostUpdated.totalCost);
  1640. #endif /* CDFA_CERTIFICATE */
  1641. }
  1642. if(sqlite3_open(DB_FILE, &db))
  1643. {
  1644. result = FAIL;
  1645. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1646. sqlite3_close(db);
  1647. }
  1648. else
  1649. {
  1650. // Delete buffer
  1651. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1652. {
  1653. result = FAIL;
  1654. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1655. }
  1656. else
  1657. {
  1658. //DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1659. }
  1660. // Insert record buffer
  1661. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1662. {
  1663. result = FAIL;
  1664. DEBUG_INFO( "Insert local charging record buffer error message: %s\n", errMsg);
  1665. }
  1666. else
  1667. {
  1668. //DEBUG_INFO( "Insert local charging record buffer successfully\n");
  1669. }
  1670. sqlite3_close(db);
  1671. }
  1672. return result;
  1673. }
  1674. int DB_Insert_Record(sqlite3 *db, int gun_index)
  1675. {
  1676. int result = PASS;
  1677. char* errMsg = NULL;
  1678. char sqlStr[4096];
  1679. char receiptInfo[2048];
  1680. getReceiptInfo(gun_index, receiptInfo);
  1681. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1682. {
  1683. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1684. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1685. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1686. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1687. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1688. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1689. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1690. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1691. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1692. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1693. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1694. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  1695. #ifdef CDFA_CERTIFICATE
  1696. receiptInfo);// Only for CDFA certificate
  1697. #else
  1698. ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  1699. #endif /* CDFA_CERTIFICATE */
  1700. }
  1701. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1702. {
  1703. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1704. #ifdef CDFA_CERTIFICATE
  1705. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1706. #else
  1707. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
  1708. #endif /* CDFA_CERTIFICATE */
  1709. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1710. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1711. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1712. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1713. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1714. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1715. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1716. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1717. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1718. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason,
  1719. #ifdef CDFA_CERTIFICATE
  1720. receiptInfo);// Only for CDFA certificate
  1721. #else
  1722. ShmOCPP20Data->CostUpdated.totalCost);
  1723. #endif /* CDFA_CERTIFICATE */
  1724. }
  1725. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1726. if(sqlite3_open(DB_FILE, &db))
  1727. {
  1728. result = FAIL;
  1729. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1730. sqlite3_close(db);
  1731. }
  1732. else
  1733. {
  1734. // Delete buffer
  1735. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1736. {
  1737. result = FAIL;
  1738. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1739. }
  1740. else
  1741. {
  1742. DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1743. }
  1744. // Insert charging record
  1745. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1746. {
  1747. result = FAIL;
  1748. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  1749. }
  1750. else
  1751. {
  1752. DEBUG_INFO( "Insert local charging record successfully\n");
  1753. }
  1754. sprintf(sqlStr, "delete from charging_record where (idx < (select idx from charging_record order by idx desc limit 1)-2000) and (dateTimeStop < '%04d.01.01 00:00:00');", (getCurrentYear()-3));
  1755. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1756. {
  1757. result = FAIL;
  1758. DEBUG_INFO( "delete local charging error message: %s\n", errMsg);
  1759. }
  1760. else
  1761. {
  1762. DEBUG_INFO( "delete local charging record successfully\n");
  1763. }
  1764. sqlite3_close(db);
  1765. }
  1766. #ifdef CDFA_CERTIFICATE
  1767. // Only for CDFA certificate
  1768. sendReceiptMail((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1769. (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1770. receiptInfo);
  1771. #endif
  1772. return result;
  1773. }
  1774. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  1775. {
  1776. uint8_t result = false;
  1777. char* errMsg = NULL;
  1778. char sqlStr[1024];
  1779. srand(time(NULL));
  1780. if(sqlite3_open(DB_FILE, &db))
  1781. {
  1782. result = FAIL;
  1783. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1784. sqlite3_close(db);
  1785. }
  1786. else
  1787. {
  1788. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
  1789. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1790. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1791. {
  1792. result = FAIL;
  1793. DEBUG_INFO( "update config error message: %s\n", errMsg);
  1794. }
  1795. else
  1796. {
  1797. DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
  1798. }
  1799. sqlite3_close(db);
  1800. }
  1801. return result;
  1802. }
  1803. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  1804. {
  1805. uint8_t result = true;
  1806. char* errMsg = NULL;
  1807. char sqlStr[1024];
  1808. char **rs;
  1809. int rows, cols;
  1810. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  1811. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1812. if(sqlite3_open(DB_FILE, &db))
  1813. {
  1814. result = FAIL;
  1815. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1816. sqlite3_close(db);
  1817. }
  1818. else
  1819. {
  1820. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1821. if(rows>0)
  1822. {
  1823. for(int idxRow=1;idxRow<=rows;idxRow++)
  1824. {
  1825. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1826. {
  1827. result = false;
  1828. }
  1829. DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow*cols)+3]);
  1830. }
  1831. }
  1832. else
  1833. {
  1834. DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
  1835. }
  1836. sqlite3_free_table(rs);
  1837. sqlite3_close(db);
  1838. }
  1839. return result;
  1840. }
  1841. //======================================================
  1842. // Peripheral initial
  1843. //======================================================
  1844. int InitWatchDog()
  1845. {
  1846. int fd;
  1847. int timeout = 330;
  1848. system("/usr/bin/fuser -k /dev/watchdog");
  1849. sleep(1);
  1850. system("echo V > /dev/watchdog");
  1851. sleep(1);
  1852. fd=open("/dev/watchdog", O_RDWR);
  1853. if(fd<=0)
  1854. {
  1855. DEBUG_ERROR("System watch dog initial fail.\n");
  1856. }
  1857. ioctl(fd, _IOWR('W', 6, int), &timeout);
  1858. return fd;
  1859. }
  1860. void InitGPIO()
  1861. {
  1862. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1863. /***************************************************************/
  1864. /*************** INPUT PIN ***************************************/
  1865. /***************************************************************/
  1866. /***************************************************************/
  1867. /*************** OUTPUT PIN ************************************/
  1868. /***************************************************************/
  1869. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  1870. system("echo 116 > /sys/class/gpio/export");
  1871. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1872. system("echo 0 > /sys/class/gpio/gpio116/value");
  1873. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  1874. system("echo 19 > /sys/class/gpio/export");
  1875. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1876. system("echo 1 > /sys/class/gpio/gpio19/value");
  1877. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  1878. system("echo 65 > /sys/class/gpio/export");
  1879. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  1880. system("echo 0 > /sys/class/gpio/gpio65/value");
  1881. /*Ethernet RST:GPIO1_24 => H:ON; L:OFF*/
  1882. system("echo 56 > /sys/class/gpio/export");
  1883. system("echo \"out\" > /sys/class/gpio/gpio56/direction");
  1884. system("echo 0 > /sys/class/gpio/gpio56/value");
  1885. /*4G RST:GPIO3_18 => H:OFF; L:ON*/
  1886. system("echo 114 > /sys/class/gpio/export");
  1887. system("echo \"out\" > /sys/class/gpio/gpio114/direction");
  1888. system("echo 1 > /sys/class/gpio/gpio114/value");
  1889. /*4G/WIFI RST:GPIO1_27 => H:OFF; L:ON*/
  1890. system("echo 59 > /sys/class/gpio/export");
  1891. system("echo \"out\" > /sys/class/gpio/gpio59/direction");
  1892. system("echo 1 > /sys/class/gpio/gpio59/value");
  1893. /*RFID ICC:GPIO0_20 => H:ON; L:OFF*/
  1894. system("echo 20 > /sys/class/gpio/export");
  1895. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  1896. /*QCA7000 interrupt:GPIO2_00 => H:ON; L:OFF*/
  1897. system("echo 64 > /sys/class/gpio/export");
  1898. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  1899. /*QCA7000 Reset:GPIO3_19 => H:ON; L:OFF*/
  1900. system("echo 115 > /sys/class/gpio/export");
  1901. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1902. system("echo 0 > /sys/class/gpio/gpio115/value");
  1903. /*RFID RST: GPIO1_30 => H:OFF; L:ON*/
  1904. system("echo 62 > /sys/class/gpio/export");
  1905. system("echo \"out\" > /sys/class/gpio/gpio62/direction");
  1906. system("echo 0 > /sys/class/gpio/gpio62/value");
  1907. /*Wake up button GPIO1_31 => H:OFF; L:ON*/
  1908. system("echo 63 > /sys/class/gpio/export");
  1909. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  1910. sleep(1);
  1911. system("echo 1 > /sys/class/gpio/gpio56/value");
  1912. system("echo 0 > /sys/class/gpio/gpio114/value");
  1913. system("echo 0 > /sys/class/gpio/gpio59/value");
  1914. system("echo 1 > /sys/class/gpio/gpio115/value");
  1915. sleep(1);
  1916. DEBUG_INFO("Initial GPIO OK\n");
  1917. }
  1918. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1919. {
  1920. int fd,wrd;
  1921. unsigned char *buf;
  1922. unsigned int ChkSum,ChkSumOrg;
  1923. if((buf=malloc(MtdBlockSize))==NULL)
  1924. {
  1925. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  1926. if(ShmStatusCodeData!=NULL)
  1927. {
  1928. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1929. }
  1930. sleep(5);
  1931. system("reboot -f");
  1932. sleep(5);
  1933. system("reboot -f");
  1934. }
  1935. memset(buf, 0, MtdBlockSize);
  1936. //================================================
  1937. // Load configuration from mtdblock10
  1938. //================================================
  1939. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1940. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1941. if (fd < 0)
  1942. {
  1943. free(buf);
  1944. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  1945. if(ShmStatusCodeData!=NULL)
  1946. {
  1947. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1948. }
  1949. sleep(5);
  1950. system("reboot -f");
  1951. sleep(5);
  1952. system("reboot -f");
  1953. }
  1954. wrd=read(fd, buf, MtdBlockSize);
  1955. close(fd);
  1956. if(wrd<MtdBlockSize)
  1957. {
  1958. free(buf);
  1959. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  1960. if(ShmStatusCodeData!=NULL)
  1961. {
  1962. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1963. }
  1964. sleep(5);
  1965. system("reboot -f");
  1966. sleep(5);
  1967. system("reboot -f");
  1968. }
  1969. ChkSum=0;
  1970. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1971. {
  1972. ChkSum+=buf[wrd];
  1973. }
  1974. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1975. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1976. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1977. //================================================
  1978. // Load configuration from mtdblock11
  1979. //================================================
  1980. if(ChkSum!=ChkSumOrg)
  1981. {
  1982. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1983. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1984. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1985. if (fd < 0)
  1986. {
  1987. free(buf);
  1988. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1989. if(ShmStatusCodeData!=NULL)
  1990. {
  1991. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1992. }
  1993. sleep(5);
  1994. system("reboot -f");
  1995. sleep(5);
  1996. system("reboot -f");
  1997. }
  1998. memset(buf, 0, MtdBlockSize);
  1999. wrd=read(fd, buf,MtdBlockSize);
  2000. close(fd);
  2001. if(wrd<MtdBlockSize)
  2002. {
  2003. free(buf);
  2004. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  2005. if(ShmStatusCodeData!=NULL)
  2006. {
  2007. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  2008. }
  2009. sleep(5);
  2010. system("reboot -f");
  2011. sleep(5);
  2012. system("reboot -f");
  2013. }
  2014. ChkSum=0;
  2015. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  2016. {
  2017. ChkSum+=buf[wrd];
  2018. }
  2019. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  2020. //================================================
  2021. // Load configuration from mtdblock12 (Factory default)
  2022. //================================================
  2023. if(ChkSum!=ChkSumOrg)
  2024. {
  2025. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  2026. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  2027. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  2028. if (fd < 0)
  2029. {
  2030. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  2031. free(buf);
  2032. if(ShmStatusCodeData!=NULL)
  2033. {
  2034. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  2035. }
  2036. sleep(5);
  2037. system("reboot -f");
  2038. sleep(5);
  2039. system("reboot -f");
  2040. }
  2041. memset(buf, 0, MtdBlockSize);
  2042. wrd=read(fd, buf,MtdBlockSize);
  2043. close(fd);
  2044. if(wrd<MtdBlockSize)
  2045. {
  2046. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  2047. free(buf);
  2048. if(ShmStatusCodeData!=NULL)
  2049. {
  2050. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  2051. }
  2052. sleep(5);
  2053. system("reboot -f");
  2054. sleep(5);
  2055. system("reboot -f");
  2056. }
  2057. ChkSum=0;
  2058. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  2059. {
  2060. ChkSum+=buf[wrd];
  2061. }
  2062. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  2063. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  2064. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  2065. if(ChkSum!=ChkSumOrg)
  2066. {
  2067. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  2068. free(buf);
  2069. system("cd /root;./Module_FactoryConfig -m");
  2070. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2071. system("sync");
  2072. sleep(5);
  2073. system("reboot -f");
  2074. sleep(5);
  2075. system("reboot -f");
  2076. return FAIL;
  2077. }
  2078. }
  2079. }
  2080. //load OK
  2081. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  2082. free(buf);
  2083. system("rm -f /mnt/EvseConfig.bin");
  2084. // SysConfig in flash is empty (0xffffffff)
  2085. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  2086. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  2087. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  2088. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  2089. {
  2090. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  2091. {
  2092. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  2093. }
  2094. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  2095. {
  2096. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2097. }
  2098. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  2099. {
  2100. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  2101. }
  2102. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  2103. {
  2104. DEBUG_INFO("Ethernet dhcp config is null.\n");
  2105. }
  2106. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  2107. {
  2108. DEBUG_INFO("Model name over length.\n");
  2109. }
  2110. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  2111. {
  2112. DEBUG_INFO("Model serial number over length.\n");
  2113. }
  2114. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  2115. {
  2116. DEBUG_INFO("SystemId over length.\n");
  2117. }
  2118. system("cd /root;./Module_FactoryConfig -m");
  2119. sleep(3);
  2120. system("/usr/bin/run_evse_restart.sh");
  2121. }
  2122. DEBUG_INFO("Load SysConfigData OK\n");
  2123. ShmCharger->isCcsEnable = OFF;
  2124. for(uint8_t idx=0;idx<3;idx++)
  2125. {
  2126. if(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == '7')
  2127. ShmCharger->isCcsEnable = ON;
  2128. }
  2129. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  2130. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  2131. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  2132. ?3:1);
  2133. RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  2134. if(modelnameInfo.GetGunCount == 0)
  2135. {
  2136. modelnameInfo.GetGunCount = 1;
  2137. DEBUG_WARN("Model name parsing error, force assign gun count to 1.\n");
  2138. }
  2139. DEBUG_INFO("Model name parsing gun count: %d\n", modelnameInfo.GetGunCount);
  2140. return PASS;
  2141. }
  2142. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2143. {
  2144. int result = PASS;
  2145. int fd,wrd;
  2146. unsigned int i,Chk;
  2147. unsigned char *ptr, *BufTmp;
  2148. Chk=0;
  2149. ptr=(unsigned char *)UsrData;
  2150. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  2151. {
  2152. memset(BufTmp,0,MtdBlockSize);
  2153. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  2154. for(i=0;i<MtdBlockSize-4;i++)
  2155. Chk+=*(BufTmp+i);
  2156. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  2157. // Output configuration to file.
  2158. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  2159. if (fd < 0)
  2160. {
  2161. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  2162. free(BufTmp);
  2163. return 0;
  2164. }
  2165. wrd=write(fd, BufTmp, MtdBlockSize);
  2166. close(fd);
  2167. if(wrd<MtdBlockSize)
  2168. {
  2169. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  2170. free(BufTmp);
  2171. return 0;
  2172. }
  2173. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  2174. DEBUG_INFO("Erase /dev/mtd10.\n");
  2175. runShellCmd("flash_erase /dev/mtd10 0 0");
  2176. DEBUG_INFO("Write /dev/mtd10.\n");
  2177. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  2178. DEBUG_INFO("Erase /dev/mtd11.\n");
  2179. runShellCmd("flash_erase /dev/mtd11 0 0");
  2180. DEBUG_INFO("Write /dev/mtd11.\n");
  2181. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  2182. system("rm -f /mnt/EvseConfig.bin");
  2183. DEBUG_INFO("EvseConfig write to flash OK\n");
  2184. }
  2185. else
  2186. {
  2187. DEBUG_ERROR("alloc BlockSize NG\r\n");
  2188. result = FAIL;
  2189. }
  2190. if(BufTmp!=NULL)
  2191. free(BufTmp);
  2192. return result;
  2193. }
  2194. void InitEthernet()
  2195. {
  2196. pid_t pid;
  2197. uint8_t cnt_pingDNS_Fail;
  2198. char tmpbuf[256];
  2199. unsigned int natInterface = 0;
  2200. //Init Eth0 for internet
  2201. memset(tmpbuf,0,256);
  2202. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2203. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2204. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2205. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2206. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2207. system(tmpbuf);
  2208. memset(tmpbuf,0,256);
  2209. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2210. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2211. system(tmpbuf);
  2212. system("ifconfig lo up &");
  2213. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  2214. //system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  2215. //Run DHCP client if enabled
  2216. system("killall udhcpc");
  2217. system("rm -rf /etc/resolv.conf");
  2218. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  2219. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  2220. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  2221. {
  2222. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2223. system(tmpbuf);
  2224. }
  2225. // Upgrade system id to /etc/hostname
  2226. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  2227. system(tmpbuf);
  2228. // Ethernet MAC address
  2229. getEth0MacAddress();
  2230. //check internet status
  2231. pid = fork();
  2232. if(pid == 0)
  2233. {
  2234. for(;;)
  2235. {
  2236. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  2237. {
  2238. if(isRouteFail())
  2239. {
  2240. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  2241. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  2242. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  2243. {
  2244. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2245. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2246. system(tmpbuf);
  2247. }
  2248. else
  2249. {
  2250. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2251. memset(tmpbuf,0,256);
  2252. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2253. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2254. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2255. system(tmpbuf);
  2256. memset(tmpbuf,0,256);
  2257. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2258. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2259. system(tmpbuf);
  2260. }
  2261. }
  2262. if(isReachableInternet() == PASS)
  2263. {
  2264. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  2265. cnt_pingDNS_Fail = 0;
  2266. }
  2267. else
  2268. {
  2269. if(cnt_pingDNS_Fail >= 3)
  2270. {
  2271. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  2272. if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  2273. {
  2274. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  2275. {
  2276. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2277. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2278. system(tmpbuf);
  2279. }
  2280. else
  2281. {
  2282. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2283. memset(tmpbuf,0,256);
  2284. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2285. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2286. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2287. system(tmpbuf);
  2288. memset(tmpbuf,0,256);
  2289. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2290. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2291. system(tmpbuf);
  2292. }
  2293. cnt_pingDNS_Fail = 0;
  2294. }
  2295. }
  2296. else
  2297. {
  2298. cnt_pingDNS_Fail++;
  2299. }
  2300. }
  2301. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  2302. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  2303. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi))
  2304. {
  2305. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2306. }
  2307. else
  2308. {
  2309. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2310. }
  2311. //============================================================
  2312. // Priority for internet 0 : First / 1 : Second / 2: Third
  2313. //============================================================
  2314. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  2315. {
  2316. system("/sbin/ifmetric eth0 0");
  2317. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2318. {
  2319. system("/sbin/ifmetric mlan0 1");
  2320. }
  2321. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2322. {
  2323. system("/sbin/ifmetric ppp0 2");
  2324. }
  2325. if(isKernelSupportNAT() == YES)
  2326. {
  2327. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2328. {
  2329. if(natInterface != 1)
  2330. {
  2331. system("/sbin/iptables -t nat -F");
  2332. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  2333. natInterface = 1;
  2334. }
  2335. }
  2336. else
  2337. {
  2338. system("/sbin/iptables -t nat -F");
  2339. }
  2340. }
  2341. }
  2342. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2343. {
  2344. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2345. {
  2346. system("/sbin/ifmetric eth0 1");
  2347. system("/sbin/ifmetric mlan0 0");
  2348. }
  2349. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2350. {
  2351. system("/sbin/ifmetric ppp0 2");
  2352. }
  2353. }
  2354. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2355. {
  2356. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2357. {
  2358. system("/sbin/ifmetric mlan0 2");
  2359. }
  2360. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2361. {
  2362. system("/sbin/ifmetric eth0 1");
  2363. system("/sbin/ifmetric ppp0 0");
  2364. }
  2365. if(isKernelSupportNAT() == YES)
  2366. {
  2367. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2368. {
  2369. if(natInterface != 2)
  2370. {
  2371. system("/sbin/iptables -t nat -F");
  2372. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  2373. natInterface = 2;
  2374. }
  2375. }
  2376. else
  2377. {
  2378. system("/sbin/iptables -t nat -F");
  2379. }
  2380. }
  2381. }
  2382. else
  2383. {
  2384. if(isKernelSupportNAT() == YES)
  2385. {
  2386. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2387. {
  2388. system("/sbin/iptables -t nat -F");
  2389. natInterface = 0;
  2390. }
  2391. }
  2392. }
  2393. if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  2394. (((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == ON)) || (((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON)))))
  2395. {
  2396. if(getDiffSecNow(startTime[0][TMR_IDX_RESET_WIFI]) > TIMEOUT_SPEC_RESET_WIFI_MODULE)
  2397. {
  2398. DEBUG_WARN("WIFI/4G module reset.\n");
  2399. gpio_set_value(GPIO_OUT_RST_4G_WIFI, ON);
  2400. sleep(1);
  2401. gpio_set_value(GPIO_OUT_RST_4G_WIFI, OFF);
  2402. }
  2403. }
  2404. else
  2405. {
  2406. refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
  2407. }
  2408. }
  2409. else
  2410. {
  2411. if(ShmDispenser->gun_info.mcuFlag.isReadFwVerPass ||
  2412. ShmDispenser->gun_info.mcuFlag.isSetModelNamePass ||
  2413. ShmDispenser->gun_info.mcuFlag.isSetSerialNumberPass)
  2414. {
  2415. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2416. system("/sbin/ifconfig eth0:1 192.168.201.202 netmask 255.255.255.248 up &");
  2417. }
  2418. }
  2419. sleep(5);
  2420. }
  2421. }
  2422. DEBUG_INFO("Initial Ethernet OK\n");
  2423. }
  2424. int SpawnTask()
  2425. {
  2426. system ("pkill Module_");
  2427. system ("pkill OcppBackend");
  2428. /*
  2429. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2430. {
  2431. system("/root/Module_4g &");
  2432. }
  2433. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2434. {
  2435. system("/root/Module_Wifi &");
  2436. }*/
  2437. system("/root/Module_EventLogging &");
  2438. ocpp_process_start();
  2439. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  2440. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2441. //system ("/root/Module_AlarmDetect &");
  2442. system ("/root/Module_InternalComm &");
  2443. //system ("/root/Module_Speaker &");
  2444. system ("/root/Module_ProduceUtils &");
  2445. //system ("/root/Module_LcmControl &");
  2446. system ("/root/Module_Cabinet &");
  2447. system ("/root/Module_Dispenser &");
  2448. return PASS;
  2449. }
  2450. int InitQca7000()
  2451. {
  2452. int result = PASS;
  2453. system("/sbin/rmmod qcaspi");
  2454. if(isKernelSupportNAT() == YES)
  2455. system("/sbin/insmod /lib/qcaspi_nat.ko");
  2456. else
  2457. system("/sbin/insmod /lib/qcaspi.ko");
  2458. sleep(2);
  2459. system("/sbin/ifconfig eth1 192.168.0.11 netmask 255.255.255.0 up");
  2460. sleep(1);
  2461. return result;
  2462. }
  2463. int Initialization(uint8_t gun_index)
  2464. {
  2465. int result = PASS;
  2466. InitGPIO();
  2467. InitEthernet();
  2468. if(InitQca7000() != PASS)
  2469. {
  2470. DEBUG_ERROR("QCA7000 initial fail.\n");
  2471. result = FAIL;
  2472. }
  2473. if(DB_Open(localDb) != PASS)
  2474. {
  2475. DEBUG_ERROR("Local database initial fail.\n");
  2476. result = FAIL;
  2477. }
  2478. if((rfidFd = InitRfidPort()) == FAIL)
  2479. {
  2480. DEBUG_ERROR("RFID port initial fail.\n");
  2481. result = FAIL;
  2482. }
  2483. if((wtdFd = InitWatchDog()) == FAIL)
  2484. {
  2485. DEBUG_ERROR("Watchdog initial fail.\n");
  2486. result = FAIL;
  2487. }
  2488. if(result == PASS)
  2489. DEBUG_INFO("Initialization OK.\n");
  2490. else
  2491. DEBUG_INFO("Initialization Fail.\n");
  2492. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2493. return result;
  2494. }
  2495. //===============================================
  2496. // Get firmware version
  2497. //===============================================
  2498. void get_firmware_version(unsigned char gun_index)
  2499. {
  2500. FILE *fp;
  2501. char cmd[512];
  2502. char buf[512];
  2503. if(gun_index == 0)
  2504. {
  2505. // Get CSU hardware version
  2506. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2507. // Get CSU boot loader version
  2508. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2509. // Get CSU kernel version
  2510. sprintf(cmd, "/bin/uname -r");
  2511. fp = popen(cmd, "r");
  2512. if(fp == NULL)
  2513. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2514. else
  2515. {
  2516. while(fgets(buf, sizeof(buf), fp) != NULL)
  2517. {
  2518. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  2519. }
  2520. }
  2521. // Get MCU firmware version
  2522. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2523. // Get CSU root file system version
  2524. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.01.00.0000.00");
  2525. // Get AC connector type from model name
  2526. for(uint8_t idx=0;idx<3;idx++)
  2527. {
  2528. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2529. {
  2530. case '1':
  2531. // J1772 Plug
  2532. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2533. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2534. break;
  2535. case '2':
  2536. // J1772 Socket
  2537. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2538. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2539. break;
  2540. case '3':
  2541. // CE Plug
  2542. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2543. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2544. break;
  2545. case '4':
  2546. // CE Socket
  2547. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2548. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2549. break;
  2550. case '5':
  2551. // GB Plug
  2552. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2553. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2554. break;
  2555. case '6':
  2556. // GB Socket
  2557. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2558. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2559. break;
  2560. }
  2561. }
  2562. // Get network option from model name
  2563. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2564. {
  2565. case 'B':
  2566. case 'U':
  2567. //Blue tooth
  2568. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2569. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2570. break;
  2571. case 'W':
  2572. // WIFI
  2573. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2574. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2575. break;
  2576. case 'T':
  2577. // 3G/4G
  2578. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2579. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2580. break;
  2581. case 'D':
  2582. // WIFI + 4G
  2583. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2584. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2585. break;
  2586. default:
  2587. // LAN
  2588. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2589. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2590. break;
  2591. }
  2592. // Get rating power from model name
  2593. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2594. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2595. // Get vender code from model name
  2596. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2597. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2598. DEBUG_INFO("========================================\n");
  2599. DEBUG_INFO("------------- Cabinet info -------------\n");
  2600. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2601. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2602. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2603. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2604. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2605. DEBUG_INFO("========================================\n");
  2606. }
  2607. //===========================================
  2608. // Put info to dispenser share memory
  2609. //===========================================
  2610. // Get CSU boot loader version
  2611. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2612. // Get CSU kernel version
  2613. sprintf(cmd, "/bin/uname -r");
  2614. fp = popen(cmd, "r");
  2615. if(fp == NULL)
  2616. sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuKernelFwRev, "Unknown version");
  2617. else
  2618. {
  2619. while(fgets(buf, sizeof(buf), fp) != NULL)
  2620. {
  2621. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuKernelFwRev, buf, strlen(buf)-1);
  2622. }
  2623. }
  2624. // Get MCU firmware version
  2625. strcpy((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2626. // Get CSU root file system version
  2627. sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev, "D0.01.00.0000.00");
  2628. // Get AC connector type from model name
  2629. for(uint8_t idx=0;idx<3;idx++)
  2630. {
  2631. switch(ShmDispenser->ConfigData.ModelName[7+idx])
  2632. {
  2633. case '1':
  2634. // J1772 Plug
  2635. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '4';
  2636. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '4';
  2637. break;
  2638. case '2':
  2639. // J1772 Socket
  2640. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '1';
  2641. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '1';
  2642. break;
  2643. case '3':
  2644. // CE Plug
  2645. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '5';
  2646. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '5';
  2647. break;
  2648. case '4':
  2649. // CE Socket
  2650. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '2';
  2651. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '2';
  2652. break;
  2653. case '5':
  2654. // GB Plug
  2655. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '6';
  2656. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '6';
  2657. break;
  2658. case '6':
  2659. // GB Socket
  2660. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '3';
  2661. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '3';
  2662. break;
  2663. }
  2664. }
  2665. // Get network option from model name
  2666. switch(ShmDispenser->ConfigData.ModelName[10])
  2667. {
  2668. case 'B':
  2669. case 'U':
  2670. //Blue tooth
  2671. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '3';
  2672. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '3';
  2673. break;
  2674. case 'W':
  2675. // WIFI
  2676. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '1';
  2677. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '1';
  2678. break;
  2679. case 'T':
  2680. // 3G/4G
  2681. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '2';
  2682. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '2';
  2683. break;
  2684. case 'D':
  2685. // WIFI + 4G
  2686. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '5';
  2687. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '5';
  2688. break;
  2689. default:
  2690. // LAN
  2691. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '0';
  2692. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '0';
  2693. break;
  2694. }
  2695. // Get rating power from model name
  2696. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[10], &ShmDispenser->ConfigData.ModelName[4], 0x03);
  2697. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[10], &ShmDispenser->ConfigData.ModelName[4], 0x03);
  2698. // Get vender code from model name
  2699. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[14], &ShmDispenser->ConfigData.ModelName[12], 0x02);
  2700. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[14], &ShmDispenser->ConfigData.ModelName[12], 0x02);
  2701. DEBUG_INFO("========================================\n");
  2702. DEBUG_INFO("----------- Dispenser-%01d info -----------\n", (gun_index+1));
  2703. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuBootLoadFwRev);
  2704. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuKernelFwRev);
  2705. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev);
  2706. DEBUG_INFO("CSU MCU firmware version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev);
  2707. DEBUG_INFO("========================================\n");
  2708. }
  2709. //===============================================
  2710. // Upgrade firmware
  2711. //===============================================
  2712. int upgrade_check()
  2713. {
  2714. int result = PASS;
  2715. int fd;
  2716. DIR *dir;
  2717. struct dirent *file;
  2718. char cmd[512];
  2719. long int MaxLen=48*1024*1024, ImageLen=0, wrd;
  2720. if ((dir = opendir ("/mnt")) != NULL)
  2721. {
  2722. refreshStartTimer(&startTime[0][TMR_IDX_UPGRADE]);
  2723. /* print all the files and directories within directory */
  2724. while ((file = readdir (dir)) != NULL)
  2725. {
  2726. if((strlen(file->d_name)>2))
  2727. {
  2728. // Wait for MCU upgrade finish.
  2729. while(ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (modelnameInfo.GetGunCount>1?ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq:FALSE))sleep(1);
  2730. // Wait for LCM upgrade finish.
  2731. while(ShmCharger->isUpgradeLcmReq)sleep(1);
  2732. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2733. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2734. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2735. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2736. {
  2737. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2738. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2739. ImageLen = read(fd, ptr, MaxLen);
  2740. close(fd);
  2741. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2742. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2743. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2744. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2745. if((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0]) &&
  2746. (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1]) &&
  2747. (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7]) &&
  2748. (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8]) &&
  2749. (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9]) &&
  2750. (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11]) &&
  2751. (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12]) &&
  2752. (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13]) &&
  2753. (ShmCharger->fwUpgradeInfo.fwType>0))
  2754. {
  2755. switch(ShmCharger->fwUpgradeInfo.fwType)
  2756. {
  2757. case CSU_MLO:
  2758. case CSU_BOOTLOADER:
  2759. case CSU_KERNEL_CONFIGURATION:
  2760. case CSU_KERNEL_IMAGE:
  2761. case CSU_ROOT_FILE_SYSTEM:
  2762. case CSU_USER_CONFIGURATION:
  2763. case CSU_PRIMARY_CONTROLLER:
  2764. case AC_WALLMOUNT_CONTROLLER:
  2765. // Put image file
  2766. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2767. {
  2768. char cmd[768];
  2769. if(gun_index>0)
  2770. {
  2771. sprintf(cmd, "ftpput -u root -p y42j/4cj84 192.168.201.%d /mnt/%s %s", (201+gun_index), file->d_name, file->d_name);
  2772. if(system(cmd) != 0)
  2773. {
  2774. DEBUG_ERROR("FTP put %s to dispenser-%d fail\n", file->d_name, gun_index);
  2775. result = FAIL;
  2776. }
  2777. else
  2778. {
  2779. DEBUG_INFO("FTP put %s to dispenser-%d finish\n", file->d_name, gun_index);
  2780. }
  2781. }
  2782. }
  2783. break;
  2784. case LCM:
  2785. fd = open("/mnt/lcm.zip", O_RDWR | O_CREAT | O_EXCL);
  2786. if (fd < 0)
  2787. {
  2788. DEBUG_ERROR("Can not create lcm.zip file.\n");
  2789. result = FAIL;
  2790. }
  2791. else
  2792. {
  2793. // Write image to flash
  2794. DEBUG_INFO("Writing data to lcm.zip file...\n");
  2795. wrd=write(fd, ptr+48, ImageLen-48);
  2796. close(fd);
  2797. DEBUG_INFO(">> lcm.zip Written length: 0x%x\n", wrd);
  2798. if(wrd != (ImageLen-48))
  2799. {
  2800. result = FAIL;
  2801. }
  2802. else
  2803. {
  2804. runShellCmd("mkdir -p /mnt/lcd");
  2805. runShellCmd("unzip /mnt/lcm.zip -d /mnt/lcd");
  2806. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2807. system(cmd);
  2808. system("rm -f /mnt/lcm.zip");
  2809. result = PASS;
  2810. }
  2811. }
  2812. ShmCharger->isUpgradeLcmReq = ON;
  2813. sleep(10);
  2814. break;
  2815. default:
  2816. result = FAIL;
  2817. DEBUG_WARN("Image file is unknown type.\n");
  2818. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2819. system(cmd);
  2820. break;
  2821. }
  2822. }
  2823. else
  2824. {
  2825. result = FAIL;
  2826. DEBUG_ERROR("Model name and Firmware type error.\n");
  2827. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2828. system(cmd);
  2829. }
  2830. free(ptr);
  2831. }
  2832. else
  2833. {
  2834. result = FAIL;
  2835. DEBUG_ERROR("New firmware open error.\n");
  2836. }
  2837. }
  2838. else
  2839. {
  2840. if(strlen(file->d_name) >= 3)
  2841. {
  2842. result = FAIL;
  2843. DEBUG_ERROR("File name error.\n");
  2844. }
  2845. else
  2846. {
  2847. DEBUG_ERROR("Searching file.\n");
  2848. }
  2849. }
  2850. }
  2851. closedir (dir);
  2852. // Wait dispenser upgrade result
  2853. if(result != FAIL)
  2854. {
  2855. uint8_t isCompleted = ON;
  2856. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2857. ShmCharger->gun_info[gun_index].isUpgradeReq = ON;
  2858. do
  2859. {
  2860. isCompleted = ON;
  2861. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2862. {
  2863. isCompleted &= ShmCharger->gun_info[gun_index].isUpgradeEnd;
  2864. }
  2865. sleep(1);
  2866. }while((getDiffSecNow(startTime[0][TMR_IDX_UPGRADE]) < TIMEOUT_SPEC_UPGRADE) && !isCompleted);
  2867. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2868. {
  2869. result &= ShmCharger->gun_info[gun_index].isUpgradePASS;
  2870. }
  2871. result &= (getDiffSecNow(startTime[0][TMR_IDX_UPGRADE]) <= TIMEOUT_SPEC_UPGRADE);
  2872. if(getDiffSecNow(startTime[0][TMR_IDX_UPGRADE]) > TIMEOUT_SPEC_UPGRADE)DEBUG_INFO("Upgrade process timeout.\n");
  2873. }
  2874. }
  2875. else
  2876. {
  2877. result = FAIL;
  2878. DEBUG_ERROR("/mnt does not valid.\n");
  2879. }
  2880. return result;
  2881. }
  2882. //===============================================
  2883. // Check RFID is match with start user
  2884. //===============================================
  2885. int isMatchStartUser(unsigned char gun_index)
  2886. {
  2887. uint8_t tmpUser[32];
  2888. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2889. {
  2890. // Big endian
  2891. switch(rfid.snType)
  2892. {
  2893. case RFID_SN_TYPE_6BYTE:
  2894. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2895. break;
  2896. case RFID_SN_TYPE_7BYTE:
  2897. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2898. break;
  2899. case RFID_SN_TYPE_10BYTE:
  2900. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2901. break;
  2902. case RFID_SN_TYPE_4BYTE:
  2903. default:
  2904. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2905. break;
  2906. }
  2907. }
  2908. else
  2909. {
  2910. // Little endian
  2911. switch(rfid.snType)
  2912. {
  2913. case RFID_SN_TYPE_6BYTE:
  2914. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2915. break;
  2916. case RFID_SN_TYPE_7BYTE:
  2917. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2918. break;
  2919. case RFID_SN_TYPE_10BYTE:
  2920. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2921. break;
  2922. case RFID_SN_TYPE_4BYTE:
  2923. default:
  2924. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2925. break;
  2926. }
  2927. }
  2928. DEBUG_INFO("==== isMatchStartUser ==== \n");
  2929. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2930. DEBUG_INFO("StartUserId : %s \n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  2931. DEBUG_INFO("========================== \n");
  2932. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2933. }
  2934. int isMatchPresentUser()
  2935. {
  2936. uint8_t tmpUser[32];
  2937. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2938. {
  2939. // Big endian
  2940. switch(rfid.snType)
  2941. {
  2942. case RFID_SN_TYPE_6BYTE:
  2943. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2944. break;
  2945. case RFID_SN_TYPE_7BYTE:
  2946. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2947. break;
  2948. case RFID_SN_TYPE_10BYTE:
  2949. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2950. break;
  2951. case RFID_SN_TYPE_4BYTE:
  2952. default:
  2953. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2954. break;
  2955. }
  2956. }
  2957. else
  2958. {
  2959. // Little endian
  2960. switch(rfid.snType)
  2961. {
  2962. case RFID_SN_TYPE_6BYTE:
  2963. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2964. break;
  2965. case RFID_SN_TYPE_7BYTE:
  2966. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2967. break;
  2968. case RFID_SN_TYPE_10BYTE:
  2969. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2970. break;
  2971. case RFID_SN_TYPE_4BYTE:
  2972. default:
  2973. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2974. break;
  2975. }
  2976. }
  2977. DEBUG_INFO("==== isMatchPresentUser ==== \n");
  2978. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2979. DEBUG_INFO("UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2980. DEBUG_INFO("============================ \n");
  2981. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysConfig.UserId)==0)?YES:NO);
  2982. }
  2983. //===============================================
  2984. // Read RFID Serial Number
  2985. //===============================================
  2986. int GetCardSerialNumber()
  2987. {
  2988. int isSuccess = FAIL;
  2989. int module_type = MODULE_EWT;
  2990. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2991. {
  2992. if(rfid.cardType == ISO14443A)
  2993. {
  2994. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2995. {
  2996. isSuccess = PASS;
  2997. }
  2998. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2999. {
  3000. isSuccess = PASS;
  3001. }
  3002. sethaltCard(rfidFd,module_type);
  3003. }
  3004. else if(rfid.cardType == IS014443B)
  3005. {
  3006. isSuccess = PASS;
  3007. }
  3008. else if(rfid.cardType == FELICA)
  3009. {
  3010. isSuccess = PASS;
  3011. }
  3012. else
  3013. {}
  3014. }
  3015. else
  3016. {}
  3017. return isSuccess;
  3018. }
  3019. //===============================================
  3020. // Set led motion
  3021. //===============================================
  3022. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  3023. {
  3024. switch(led_mode)
  3025. {
  3026. case LED_ACTION_INIT:
  3027. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  3028. break;
  3029. case LED_ACTION_IDLE:
  3030. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  3031. break;
  3032. case LED_ACTION_AUTHED:
  3033. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  3034. break;
  3035. case LED_ACTION_CONNECTED:
  3036. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  3037. break;
  3038. case LED_ACTION_CHARGING:
  3039. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  3040. break;
  3041. case LED_ACTION_STOP:
  3042. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  3043. break;
  3044. case LED_ACTION_ALARM:
  3045. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  3046. break;
  3047. case LED_ACTION_MAINTAIN:
  3048. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  3049. break;
  3050. case LED_ACTION_RFID_PASS:
  3051. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  3052. break;
  3053. case LED_ACTION_RFID_FAIL:
  3054. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  3055. break;
  3056. case LED_ACTION_BLE_CONNECT:
  3057. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  3058. break;
  3059. case LED_ACTION_BLE_DISABLE:
  3060. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  3061. break;
  3062. case LED_ACTION_DEBUG:
  3063. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  3064. break;
  3065. case LED_ACTION_ALL_OFF:
  3066. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  3067. break;
  3068. case LED_RELAY_ON:
  3069. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  3070. break;
  3071. case LED_RELAY_OFF:
  3072. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  3073. break;
  3074. case LED_ACTION_HANDSHAKE_FAIL:
  3075. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  3076. break;
  3077. case LED_ACTION_INTERNET_DISCONNECT:
  3078. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  3079. break;
  3080. case LED_ACTION_RESTORE_SETTING:
  3081. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESTORE_SETTING;
  3082. break;
  3083. case LED_ACTION_IDLE_BACKEND_CONNECTED:
  3084. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED;
  3085. break;
  3086. case LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP:
  3087. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP;
  3088. break;
  3089. case LED_ACTION_IDLE_BACKEND_DISCONNECTED:
  3090. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED;
  3091. break;
  3092. case LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP:
  3093. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP;
  3094. break;
  3095. case LED_ACTION_RESERVATION_MODE:
  3096. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESERVATION_MODE;
  3097. break;
  3098. }
  3099. }
  3100. //===============================================
  3101. // Request on/off set
  3102. //===============================================
  3103. void setRequest(unsigned char gun_index,unsigned char isOn)
  3104. {
  3105. if(isOn == ON)
  3106. {
  3107. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  3108. {
  3109. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  3110. DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
  3111. }
  3112. }
  3113. else
  3114. {
  3115. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3116. {
  3117. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  3118. DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
  3119. }
  3120. }
  3121. }
  3122. //===============================================
  3123. // Request on/off get
  3124. //===============================================
  3125. int getRequest(unsigned char gun_index)
  3126. {
  3127. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  3128. }
  3129. //===============================================
  3130. // Relay on/off set
  3131. //===============================================
  3132. void setRelay(unsigned char gun_index,unsigned char isOn)
  3133. {
  3134. if(isOn == ON)
  3135. {
  3136. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  3137. {
  3138. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3139. {
  3140. case CHARGING_MODE_BS:
  3141. case CHARGING_MODE_HLC:
  3142. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3143. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3144. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3145. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3146. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3147. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_BS] \n",gun_index);
  3148. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3149. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_HLC] \n",gun_index);
  3150. break;
  3151. case CHARGING_MODE_SOCKETE:
  3152. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3153. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0x01;
  3154. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3155. break;
  3156. default:
  3157. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3158. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3159. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3160. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3161. DEBUG_INFO("Gun-%d Output relay status: ON. [DEFFAULT] \n",gun_index);
  3162. break;
  3163. }
  3164. }
  3165. }
  3166. else
  3167. {
  3168. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  3169. {
  3170. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3171. {
  3172. case CHARGING_MODE_BS:
  3173. case CHARGING_MODE_HLC:
  3174. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3175. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3176. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3177. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3178. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3179. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_BS] \n",gun_index);
  3180. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3181. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_HLC] \n",gun_index);
  3182. break;
  3183. case CHARGING_MODE_SOCKETE:
  3184. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3185. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0;
  3186. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3187. break;
  3188. default:
  3189. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3190. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3191. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3192. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3193. DEBUG_INFO("Gun-%d Output relay status: OFF. [DEFFAULT] \n",gun_index);
  3194. break;
  3195. }
  3196. }
  3197. }
  3198. }
  3199. //===============================================
  3200. // Relay on/off get
  3201. //===============================================
  3202. int getRelay(unsigned char gun_index)
  3203. {
  3204. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  3205. }
  3206. //===============================================
  3207. // Set speaker on/off request
  3208. //===============================================
  3209. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  3210. {
  3211. if(isOn == ON)
  3212. {
  3213. ShmCharger->isSpeakerOn = ON;
  3214. ShmCharger->speaker_type = speaker_mode;
  3215. }
  3216. }
  3217. //===============================================
  3218. // Initialization RFID communication port
  3219. //===============================================
  3220. int InitRfidPort()
  3221. {
  3222. int uartO2 = open(rfidPortName, O_RDWR);
  3223. struct termios tios;
  3224. if (uartO2 != FAIL)
  3225. {
  3226. ioctl (uartO2, TCGETS, &tios);
  3227. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  3228. tios.c_lflag = 0;
  3229. tios.c_iflag = 0;
  3230. tios.c_oflag = 0;
  3231. tios.c_cc[VMIN] = 0;
  3232. tios.c_cc[VTIME] = (unsigned char) 1;
  3233. tios.c_lflag = 0;
  3234. tcflush(uartO2, TCIFLUSH);
  3235. ioctl(uartO2, TCSETS, &tios);
  3236. }
  3237. if (uartO2 < 0)
  3238. {
  3239. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  3240. }
  3241. return uartO2;
  3242. }
  3243. //===============================================
  3244. // Check internet access status
  3245. //===============================================
  3246. int isReachableInternet()
  3247. {
  3248. int result = FAIL;
  3249. FILE *fp;
  3250. char cmd[256];
  3251. char buf[512];
  3252. char tmp[512];
  3253. // Get ip address & net mask
  3254. strcpy(cmd, "ifconfig eth0");
  3255. fp = popen(cmd, "r");
  3256. if (fp != NULL)
  3257. {
  3258. while(fgets(buf, sizeof(buf), fp) != NULL)
  3259. {
  3260. if (strstr(buf, "inet addr:") > 0)
  3261. {
  3262. sscanf(buf, "%*s%s", tmp);
  3263. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  3264. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  3265. {
  3266. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  3267. }
  3268. sscanf(buf, "%*s%*s%*s%s", tmp);
  3269. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  3270. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  3271. {
  3272. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  3273. }
  3274. }
  3275. }
  3276. }
  3277. pclose(fp);
  3278. memset(buf, 0x00, sizeof(buf));
  3279. // Get gateway
  3280. fp = popen("ip route", "r");
  3281. if(fp != NULL)
  3282. {
  3283. while(fgets(buf, sizeof(buf), fp) != NULL)
  3284. {
  3285. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  3286. break;
  3287. }
  3288. if(strstr(buf, "default") != NULL)
  3289. {
  3290. sscanf(buf, "%*s%*s%s", tmp);
  3291. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  3292. }
  3293. }
  3294. pclose(fp);
  3295. memset(buf, 0x00, sizeof(buf));
  3296. // Check internet access ability
  3297. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  3298. {
  3299. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  3300. fp = popen(cmd, "r");
  3301. if(fp != NULL)
  3302. {
  3303. while(fgets(buf, sizeof(buf), fp) != NULL)
  3304. {
  3305. if(strstr(buf, "transmitted") > 0)
  3306. {
  3307. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  3308. if(strstr(buf,"100%") != NULL)
  3309. {
  3310. }
  3311. else
  3312. {
  3313. result = PASS;
  3314. }
  3315. //DEBUG_INFO("%s",buf);
  3316. //DEBUG_INFO("%s\n",tmp);
  3317. }
  3318. }
  3319. }
  3320. pclose(fp);
  3321. }
  3322. return result;
  3323. }
  3324. int isRouteFail()
  3325. {
  3326. int result = YES;
  3327. FILE *fp;
  3328. char buf[512];
  3329. fp = popen("route -n", "r");
  3330. if(fp != NULL)
  3331. {
  3332. while(fgets(buf, sizeof(buf), fp) != NULL)
  3333. {
  3334. if(strstr(buf, "eth0") != NULL)
  3335. result = NO;
  3336. }
  3337. }
  3338. pclose(fp);
  3339. return result;
  3340. }
  3341. //===============================================
  3342. // Check reservation date is expired
  3343. //===============================================
  3344. int isReservationExpired(unsigned char gun_index)
  3345. {
  3346. int result = NO;
  3347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3348. {
  3349. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  3350. result = YES;
  3351. }
  3352. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3353. {
  3354. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  3355. result = YES;
  3356. }
  3357. return result;
  3358. }
  3359. //===============================================
  3360. // Check charging profile related date routine
  3361. //===============================================
  3362. int isProfileValid(uint8_t gun_index)
  3363. {
  3364. int result = NO;
  3365. struct tm tmFrom, tmTo;
  3366. struct timeb tbFrom, tbTo;
  3367. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3368. {
  3369. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  3370. (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3371. {
  3372. tmFrom.tm_year -= 1900;
  3373. tmFrom.tm_mon -= 1;
  3374. tbFrom.time = mktime(&tmFrom);
  3375. tmTo.tm_year -= 1900;
  3376. tmTo.tm_mon -= 1;
  3377. tbTo.time = mktime(&tmTo);
  3378. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3379. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3380. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3381. {
  3382. result = YES;
  3383. }
  3384. }
  3385. else
  3386. {
  3387. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3388. }
  3389. }
  3390. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3391. {
  3392. if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  3393. (sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3394. {
  3395. tmFrom.tm_year -= 1900;
  3396. tmFrom.tm_mon -= 1;
  3397. tbFrom.time = mktime(&tmFrom);
  3398. tmTo.tm_year -= 1900;
  3399. tmTo.tm_mon -= 1;
  3400. tbTo.time = mktime(&tmTo);
  3401. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3402. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3403. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3404. {
  3405. result = YES;
  3406. }
  3407. }
  3408. else
  3409. {
  3410. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3411. }
  3412. }
  3413. return result;
  3414. }
  3415. int getMaxScheduleStart()
  3416. {
  3417. int result = -1;
  3418. struct tm tmScheduleStart;;
  3419. struct timeb tbScheduleStart;
  3420. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3421. {
  3422. if((sscanf((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule, "%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))
  3423. {
  3424. tmScheduleStart.tm_year -= 1900;
  3425. tmScheduleStart.tm_mon -= 1;
  3426. tbScheduleStart.time = mktime(&tmScheduleStart);
  3427. tbScheduleStart.millitm = 0;
  3428. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3429. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3430. }
  3431. else
  3432. {
  3433. DEBUG_WARN("Max schedule start date parsing error.\n");
  3434. }
  3435. }
  3436. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3437. {
  3438. if((sscanf((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule, "%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))
  3439. {
  3440. tmScheduleStart.tm_year -= 1900;
  3441. tmScheduleStart.tm_mon -= 1;
  3442. tbScheduleStart.time = mktime(&tmScheduleStart);
  3443. tbScheduleStart.millitm = 0;
  3444. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3445. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3446. }
  3447. else
  3448. {
  3449. DEBUG_WARN("Max schedule start date parsing error.\n");
  3450. }
  3451. }
  3452. return result;
  3453. }
  3454. int getScheduleStart(int gun_index)
  3455. {
  3456. int result = -1;
  3457. struct tm tmScheduleStart;;
  3458. struct timeb tbScheduleStart;
  3459. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3460. {
  3461. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%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))
  3462. {
  3463. tmScheduleStart.tm_year -= 1900;
  3464. tmScheduleStart.tm_mon -= 1;
  3465. tbScheduleStart.time = mktime(&tmScheduleStart);
  3466. tbScheduleStart.millitm = 0;
  3467. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3468. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3469. }
  3470. else
  3471. {
  3472. DEBUG_WARN("Schedule start date parsing error.\n");
  3473. }
  3474. }
  3475. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3476. {
  3477. if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule, "%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))
  3478. {
  3479. tmScheduleStart.tm_year -= 1900;
  3480. tmScheduleStart.tm_mon -= 1;
  3481. tbScheduleStart.time = mktime(&tmScheduleStart);
  3482. tbScheduleStart.millitm = 0;
  3483. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3484. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3485. }
  3486. else
  3487. {
  3488. DEBUG_WARN("Schedule start date parsing error.\n");
  3489. }
  3490. }
  3491. return result;
  3492. }
  3493. int getStartSinceToday()
  3494. {
  3495. int result = -1;
  3496. time_t t;
  3497. struct tm *tmStartToday;
  3498. struct timeb tbStartToday;
  3499. t=time(NULL);
  3500. tmStartToday=localtime(&t);
  3501. tmStartToday->tm_hour = 0;
  3502. tmStartToday->tm_min = 0;
  3503. tmStartToday->tm_sec = 0;
  3504. tbStartToday.time = mktime(tmStartToday);
  3505. result = DiffTimebWithNow(tbStartToday)/1000;
  3506. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3507. return result;
  3508. }
  3509. int getStartSinceWeek()
  3510. {
  3511. int result = -1;
  3512. time_t t;
  3513. struct tm *tmStartWeek;
  3514. struct timeb tbStartWeek;
  3515. t=time(NULL);
  3516. tmStartWeek=localtime(&t);
  3517. t-=86400*tmStartWeek->tm_wday;
  3518. tmStartWeek=localtime(&t);
  3519. tmStartWeek->tm_hour = 0;
  3520. tmStartWeek->tm_min = 0;
  3521. tmStartWeek->tm_sec = 0;
  3522. tbStartWeek.time = mktime(tmStartWeek);
  3523. result = DiffTimebWithNow(tbStartWeek)/1000;
  3524. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3525. return result;
  3526. }
  3527. //===============================================
  3528. // Valid from local white list
  3529. //===============================================
  3530. int isValidLocalWhiteCard()
  3531. {
  3532. uint8_t result = FAIL;
  3533. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  3534. {
  3535. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3536. {
  3537. result = PASS;
  3538. }
  3539. }
  3540. return result;
  3541. }
  3542. //==========================================
  3543. // Check routine
  3544. //==========================================
  3545. void checkTask()
  3546. {
  3547. /*
  3548. * TODO:
  3549. * 1. Rotary switch define change with system specification
  3550. */
  3551. // Task run only on cabinet
  3552. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  3553. {
  3554. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3555. {
  3556. if(system("pidof -s Module_4g > /dev/null") != 0)
  3557. {
  3558. DEBUG_INFO("Module_4g not running, restart it.\n");
  3559. system("/root/Module_4g &");
  3560. }
  3561. }
  3562. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3563. {
  3564. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  3565. {
  3566. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3567. system("/root/Module_Wifi &");
  3568. }
  3569. }
  3570. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0) && !ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  3571. {
  3572. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3573. {
  3574. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  3575. {
  3576. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3577. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3578. system("killall OcppBackend");
  3579. sleep(3);
  3580. ocpp_process_start();
  3581. }
  3582. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3583. {
  3584. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3585. ocpp_process_start();
  3586. }
  3587. }
  3588. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3589. {
  3590. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  3591. {
  3592. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3593. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3594. system("killall OcppBackend20");
  3595. sleep(3);
  3596. ocpp_process_start();
  3597. }
  3598. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3599. {
  3600. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3601. ocpp_process_start();
  3602. }
  3603. }
  3604. }
  3605. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3606. {
  3607. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3608. {
  3609. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3610. system("/root/OcppBackendPH &");
  3611. }
  3612. }
  3613. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  3614. {
  3615. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3616. system("/root/Module_Speaker &");
  3617. }
  3618. if(system("pidof -s Module_LcmControl > /dev/null") != 0)
  3619. {
  3620. DEBUG_INFO("Module_LcmControl not running, restart it.\n");
  3621. system ("/root/Module_LcmControl &");
  3622. }
  3623. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  3624. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  3625. {
  3626. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  3627. system ("/root/Module_PowerSharing &");
  3628. }
  3629. /*
  3630. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  3631. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  3632. {
  3633. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  3634. system ("/root/Module_InitUpgrade &");
  3635. }*/
  3636. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  3637. {
  3638. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  3639. system("/root/Module_AlarmDetect &");
  3640. }
  3641. }
  3642. // Task run both on cabinet and dispenser
  3643. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  3644. {
  3645. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3646. system("/root/Module_EventLogging &");
  3647. }
  3648. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  3649. {
  3650. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  3651. system("/root/Module_InternalComm &");
  3652. }
  3653. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3654. {
  3655. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3656. system ("/root/Module_ProduceUtils &");
  3657. }
  3658. if((system("pidof -s Module_Cabinet > /dev/null") != 0))
  3659. {
  3660. DEBUG_INFO("Module_Cabinet not running, restart it.\n");
  3661. system ("/root/Module_Cabinet &");
  3662. }
  3663. if((system("pidof -s Module_Dispenser > /dev/null") != 0))
  3664. {
  3665. DEBUG_INFO("Module_Dispenser not running, restart it.\n");
  3666. system ("/root/Module_Dispenser &");
  3667. }
  3668. if(ShmCharger->isCcsEnable == ON)
  3669. {
  3670. if((system("pidof -s CsuComm > /dev/null") != 0))
  3671. {
  3672. DEBUG_INFO("CsuComm not running, restart it.\n");
  3673. system ("/root/CsuComm &");
  3674. }
  3675. if((system("pidof -s SeccComm > /dev/null") != 0))
  3676. {
  3677. sleep(3);
  3678. DEBUG_INFO("SeccComm not running, restart it.\n");
  3679. system ("/root/SeccComm &");
  3680. }
  3681. }
  3682. }
  3683. void checkConnectionTimeout()
  3684. {
  3685. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3686. {
  3687. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3688. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  3689. }
  3690. else
  3691. {
  3692. ShmCharger->timeoutSpec.Present_Timeout_Spec = ocpp_get_connection_timeout();
  3693. /*
  3694. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3695. {
  3696. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10);
  3697. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3698. }
  3699. else
  3700. {
  3701. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec);
  3702. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3703. }*/
  3704. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3705. }
  3706. }
  3707. void checkReset()
  3708. {
  3709. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3710. {
  3711. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3712. {
  3713. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3714. ((modelnameInfo.GetGunCount==2)?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE) &&
  3715. ((modelnameInfo.GetGunCount==3)?(!isMode(2, SYS_MODE_CHARGING) && !isMode(2, SYS_MODE_TERMINATING) && !isMode(2, SYS_MODE_COMPLETE)):TRUE))
  3716. {
  3717. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3718. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3719. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3720. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3721. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3722. {
  3723. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3724. {
  3725. ShmCharger->gun_info[gun_index].isResetHardReq = ON;
  3726. setLedMotion(gun_index,LED_ACTION_INIT);
  3727. }
  3728. }
  3729. else
  3730. {
  3731. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3732. {
  3733. ShmCharger->gun_info[gun_index].isResetSoftReq = ON;
  3734. setLedMotion(gun_index,LED_ACTION_INIT);
  3735. }
  3736. }
  3737. sleep(2);
  3738. }
  3739. }
  3740. }
  3741. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3742. {
  3743. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3744. {
  3745. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3746. ((modelnameInfo.GetGunCount==2)?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE) &&
  3747. ((modelnameInfo.GetGunCount==3)?(!isMode(2, SYS_MODE_CHARGING) && !isMode(2, SYS_MODE_TERMINATING) && !isMode(2, SYS_MODE_COMPLETE)):TRUE))
  3748. {
  3749. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3750. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  3751. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3752. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3753. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3754. {
  3755. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3756. {
  3757. ShmCharger->gun_info[gun_index].isResetHardReq = ON;
  3758. setLedMotion(gun_index,LED_ACTION_INIT);
  3759. }
  3760. }
  3761. else
  3762. {
  3763. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3764. {
  3765. ShmCharger->gun_info[gun_index].isResetSoftReq = ON;
  3766. setLedMotion(gun_index,LED_ACTION_INIT);
  3767. }
  3768. }
  3769. sleep(2);
  3770. }
  3771. }
  3772. }
  3773. if(ShmDispenser->gun_info.isResetHardReq)
  3774. {
  3775. system("sync");
  3776. sleep(5);
  3777. system("reboot -f");
  3778. sleep(5);
  3779. system("reboot -f");
  3780. }
  3781. else if(ShmDispenser->gun_info.isResetSoftReq)
  3782. {
  3783. sleep(5);
  3784. close(wtdFd);
  3785. system("/usr/bin/run_evse_restart.sh");
  3786. }
  3787. }
  3788. void checkReservation(uint8_t gun_index)
  3789. {
  3790. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3791. {
  3792. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3793. {
  3794. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3795. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3796. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3797. {
  3798. sleep(5);
  3799. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3800. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3801. }
  3802. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3803. }
  3804. }
  3805. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3806. {
  3807. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3808. {
  3809. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3810. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3811. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3812. {
  3813. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3814. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3815. }
  3816. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3817. }
  3818. }
  3819. }
  3820. void checkUnlocker(uint8_t gun_index)
  3821. {
  3822. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3823. {
  3824. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3825. {
  3826. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3827. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  3828. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  3829. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  3830. }
  3831. }
  3832. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3833. {
  3834. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3835. {
  3836. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3837. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
  3838. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  3839. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  3840. }
  3841. }
  3842. }
  3843. void checkAvailability(uint8_t gun_index)
  3844. {
  3845. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3846. {
  3847. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3848. {
  3849. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3850. {
  3851. DB_Update_Operactive(localDb, gun_index, true);
  3852. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3853. }
  3854. else
  3855. {
  3856. DB_Update_Operactive(localDb, gun_index, false);
  3857. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3858. }
  3859. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3860. }
  3861. }
  3862. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3863. {
  3864. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3865. {
  3866. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  3867. {
  3868. DB_Update_Operactive(localDb, gun_index, true);
  3869. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3870. }
  3871. else
  3872. {
  3873. DB_Update_Operactive(localDb, gun_index, false);
  3874. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3875. }
  3876. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3877. }
  3878. }
  3879. }
  3880. void checkChargingProfileLimit(uint8_t gun_index)
  3881. {
  3882. static struct PreviousData
  3883. {
  3884. uint16_t targetCurrent;
  3885. uint16_t current_limit;
  3886. }previousData[3];
  3887. uint16_t targetMaxCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3888. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3889. {
  3890. // Get profile charging profile limit
  3891. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3892. {
  3893. // Checking limitation
  3894. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3895. {
  3896. if((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3897. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3898. )
  3899. {
  3900. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  3901. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3902. }
  3903. else
  3904. break;
  3905. }
  3906. }
  3907. else
  3908. {
  3909. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3910. }
  3911. // Get max charging profile limit
  3912. if((ocpp_get_maxcharging_profileId() > 0))
  3913. {
  3914. // Checking limitation
  3915. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3916. {
  3917. if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3918. ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3919. )
  3920. {
  3921. targetMaxCurrent = (mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  3922. //DEBUG_INFO("targetMaxCurrent on period[%d]: %d\n", idx_period, targetMaxCurrent);
  3923. }
  3924. else
  3925. break;
  3926. }
  3927. }
  3928. // Compare target current is over max current for EVSE limit
  3929. if(gun_index == 0)
  3930. {
  3931. if(ShmCharger->gun_info[gun_index].targetCurrent > targetMaxCurrent)
  3932. ShmCharger->gun_info[gun_index].targetCurrent = targetMaxCurrent;
  3933. }
  3934. else
  3935. {
  3936. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > targetMaxCurrent)
  3937. ShmCharger->gun_info[gun_index].targetCurrent = (targetMaxCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3938. }
  3939. }
  3940. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3941. {
  3942. // Get profile charging profile limit
  3943. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3944. {
  3945. // Checking limitation
  3946. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3947. {
  3948. if((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3949. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3950. )
  3951. {
  3952. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  3953. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3954. }
  3955. else
  3956. break;
  3957. }
  3958. }
  3959. else
  3960. {
  3961. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3962. }
  3963. // Get max charging profile limit
  3964. if((ocpp_get_maxcharging_profileId() > 0))
  3965. {
  3966. // Checking limitation
  3967. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3968. {
  3969. if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3970. ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3971. )
  3972. {
  3973. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  3974. //DEBUG_INFO("targetMaxCurrent on period[%d]: %d\n", idx_period, targetMaxCurrent);
  3975. }
  3976. else
  3977. break;
  3978. }
  3979. }
  3980. // Compare target current is over max current for EVSE limit
  3981. if(gun_index == 0)
  3982. {
  3983. if(ShmCharger->gun_info[gun_index].targetCurrent > targetMaxCurrent)
  3984. ShmCharger->gun_info[gun_index].targetCurrent = targetMaxCurrent;
  3985. }
  3986. else
  3987. {
  3988. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > targetMaxCurrent)
  3989. ShmCharger->gun_info[gun_index].targetCurrent = (targetMaxCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3990. }
  3991. }
  3992. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3993. {
  3994. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3995. ShmCharger->gun_info[gun_index].targetCurrent = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)?6:ShmCharger->gun_info[gun_index].targetCurrent);
  3996. }
  3997. else
  3998. {
  3999. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4000. ShmCharger->gun_info[gun_index].targetCurrent = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)?6:ShmCharger->gun_info[gun_index].targetCurrent);
  4001. }
  4002. if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  4003. {
  4004. DEBUG_INFO("SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4005. DEBUG_INFO("gun-%02d target current: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4006. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  4007. }
  4008. if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  4009. {
  4010. DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  4011. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  4012. }
  4013. }
  4014. void checkStopReason(uint8_t gun_index)
  4015. {
  4016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4017. {
  4018. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4019. if(!ocpp_get_starttransaction_result(gun_index))
  4020. {
  4021. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  4022. }
  4023. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4024. {
  4025. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  4026. }
  4027. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4028. {
  4029. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4030. }
  4031. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  4032. {
  4033. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  4034. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  4035. else
  4036. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  4037. }
  4038. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4039. {
  4040. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  4041. if(!isMatchStartUser(gun_index))
  4042. {
  4043. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4044. }
  4045. else
  4046. {
  4047. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4048. }
  4049. DEBUG_INFO("Gun-%d [ IdTag ] : %s \n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  4050. }
  4051. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  4052. {
  4053. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  4054. }
  4055. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  4056. {
  4057. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  4058. }
  4059. else
  4060. {
  4061. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  4062. }
  4063. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4064. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4065. presentChargedEnergyUpdate(gun_index);
  4066. DEBUG_INFO("PresentChargedEnergy Gun-[%d] : %.4f \n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  4067. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4068. }
  4069. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4070. {
  4071. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4072. if(!ocpp_get_starttransaction_result(gun_index))
  4073. {
  4074. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  4075. }
  4076. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4077. {
  4078. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  4079. }
  4080. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4081. {
  4082. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  4083. }
  4084. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  4085. {
  4086. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  4087. }
  4088. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  4089. {
  4090. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  4091. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  4092. else
  4093. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  4094. }
  4095. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4096. {
  4097. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  4098. if(!isMatchStartUser(gun_index))
  4099. {
  4100. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4101. }
  4102. else
  4103. {
  4104. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4105. }
  4106. DEBUG_INFO("Gun-%d [ idToken ] : %s \n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  4107. }
  4108. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT) ||
  4109. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT) ||
  4110. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  4111. {
  4112. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  4113. }
  4114. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  4115. {
  4116. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  4117. }
  4118. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  4119. {
  4120. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  4121. }
  4122. else
  4123. {
  4124. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  4125. }
  4126. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4127. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4128. presentChargedEnergyUpdate(gun_index);
  4129. DEBUG_INFO("PresentChargedEnergy Gun-[%d] : %.4f \n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  4130. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  4131. }
  4132. }
  4133. void checkRemoteUpgradeStatus()
  4134. {
  4135. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4136. {
  4137. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  4138. {
  4139. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4140. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4141. ShmCharger->isUpdateSuccess = NO;
  4142. }
  4143. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  4144. {
  4145. DEBUG_INFO("Firmware remote upgrading...\n");
  4146. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4147. sleep(1);
  4148. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4149. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4150. if(upgrade_check() == PASS)
  4151. {
  4152. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4153. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4154. ShmCharger->isUpdateSuccess = YES;
  4155. DEBUG_INFO("Remote update success.\n");
  4156. }
  4157. else
  4158. {
  4159. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4160. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4161. ShmCharger->isUpdateSuccess = NO;
  4162. DEBUG_INFO("Remote update fail.\n");
  4163. }
  4164. }
  4165. else
  4166. {}
  4167. }
  4168. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4169. {
  4170. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  4171. {
  4172. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4173. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4174. ShmCharger->isUpdateSuccess = NO;
  4175. }
  4176. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  4177. {
  4178. DEBUG_INFO("Firmware remote upgrading...\n");
  4179. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4180. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4181. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4182. if(upgrade_check() == PASS)
  4183. {
  4184. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4185. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4186. ShmCharger->isUpdateSuccess = YES;
  4187. DEBUG_INFO("Remote update success.\n");
  4188. }
  4189. else
  4190. {
  4191. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4192. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4193. ShmCharger->isUpdateSuccess = NO;
  4194. DEBUG_INFO("Remote update fail.\n");
  4195. }
  4196. }
  4197. else
  4198. {}
  4199. }
  4200. }
  4201. void checkHandshakeTime()
  4202. {
  4203. // Handskake timer needs to refresh
  4204. if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4205. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4206. // Timer for lcm to shows
  4207. ShmCharger->timeoutSpec.Handshake_Timeout = (ocpp_get_connection_timeout() - getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]));
  4208. }
  4209. void checkRfidAuthrize()
  4210. {
  4211. static uint8_t isCheckdResult = FALSE;
  4212. if(!ShmCharger->isAuthrizing)
  4213. {
  4214. // Read RFID
  4215. if(GetCardSerialNumber() != FAIL)
  4216. {
  4217. uint8_t isSnStart = FALSE;
  4218. uint8_t bufferRFID[ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId)];
  4219. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4220. {
  4221. // Big endian
  4222. switch(rfid.snType)
  4223. {
  4224. case RFID_SN_TYPE_6BYTE:
  4225. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  4226. break;
  4227. case RFID_SN_TYPE_7BYTE:
  4228. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  4229. break;
  4230. case RFID_SN_TYPE_10BYTE:
  4231. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  4232. break;
  4233. case RFID_SN_TYPE_4BYTE:
  4234. default:
  4235. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4236. break;
  4237. }
  4238. }
  4239. else
  4240. {
  4241. // Little endian
  4242. switch(rfid.snType)
  4243. {
  4244. case RFID_SN_TYPE_6BYTE:
  4245. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4246. break;
  4247. case RFID_SN_TYPE_7BYTE:
  4248. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4249. break;
  4250. case RFID_SN_TYPE_10BYTE:
  4251. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4252. break;
  4253. case RFID_SN_TYPE_4BYTE:
  4254. default:
  4255. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4256. break;
  4257. }
  4258. }
  4259. DEBUG_INFO("Authorize request User Id : %s\n", bufferRFID);
  4260. //DEBUG_INFO("Authorize request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4261. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4262. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4263. // Check SN already start
  4264. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4265. {
  4266. if(isMatchStartUser(gun_index) || (isMatchPresentUser(gun_index) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING)))
  4267. {
  4268. DEBUG_INFO("%s running on connector-%02d.\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, gun_index);
  4269. isSnStart = TRUE;
  4270. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4271. ocpp_set_auth_conf(ON);
  4272. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4273. setSpeaker(ON, SPEAKER_SHORT);
  4274. sleep(3);
  4275. break;
  4276. }
  4277. }
  4278. // Request authorize if isSnStart is false
  4279. if(!isSnStart)
  4280. {
  4281. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4282. refreshStartTimer(&startTime[0][TMR_IDX_AUTH]);
  4283. ocpp_set_auth_conf(OFF);
  4284. ocpp_set_auth_req(ON, "ISO14443");
  4285. setLedMotion(0,LED_ACTION_AUTHED);
  4286. ShmCharger->isAuthrizing = TRUE;
  4287. ShmCharger->isGetAuthResult = FALSE;
  4288. isCheckdResult = FALSE;
  4289. //sleep(1);
  4290. }
  4291. }
  4292. }
  4293. else
  4294. {
  4295. // Wait authorize result
  4296. if(!ocpp_get_auth_conf() && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
  4297. {
  4298. // Authorization timeout process.
  4299. DEBUG_WARN("Authorize timeout !!!\n");
  4300. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4301. {
  4302. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4303. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4304. }
  4305. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4306. sleep(3);
  4307. ShmCharger->isAuthrizing = FALSE;
  4308. }
  4309. else
  4310. {
  4311. if(ocpp_get_auth_conf() ||
  4312. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  4313. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2))
  4314. )
  4315. {
  4316. if(ocpp_get_auth_result(NO) ||
  4317. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  4318. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4319. {
  4320. if(!isCheckdResult)
  4321. {
  4322. DEBUG_INFO("Authorize pass.\n");
  4323. setSpeaker(ON, SPEAKER_SHORT);
  4324. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4325. ShmCharger->isGetAuthResult = TRUE;
  4326. isCheckdResult = TRUE;
  4327. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4328. {
  4329. ocpp_set_auth_conf(ON);
  4330. }
  4331. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4332. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4333. }
  4334. if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < ocpp_get_connection_timeout())
  4335. {
  4336. checkHandshakeTime();
  4337. if(GetCardSerialNumber() != FAIL)
  4338. {
  4339. if(isMatchPresentUser())
  4340. {
  4341. DEBUG_INFO("Cancel present user.\n");
  4342. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4343. {
  4344. setLedMotion(gun_index,LED_ACTION_IDLE);
  4345. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4346. }
  4347. ShmCharger->isAuthrizing = FALSE;
  4348. }
  4349. }
  4350. else
  4351. {
  4352. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4353. {
  4354. if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
  4355. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4356. else
  4357. setLedMotion(gun_index,LED_ACTION_AUTHED);
  4358. if((((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_RESERVATION)) && (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON))) ||
  4359. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) && (ShmCharger->gun_selectd == gun_index)) ||
  4360. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING)) && (ShmCharger->gun_selectd == gun_index))
  4361. )
  4362. {
  4363. DEBUG_INFO("Connector-%02d action detect.\n", gun_index);
  4364. ShmCharger->isAuthrizing = FALSE;
  4365. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4366. break;
  4367. }
  4368. }
  4369. }
  4370. }
  4371. else
  4372. {
  4373. DEBUG_INFO("Connector action detect timeout.\n");
  4374. ShmCharger->isAuthrizing = FALSE;
  4375. ShmCharger->isGetAuthResult = TRUE;
  4376. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4377. {
  4378. setLedMotion(gun_index,LED_ACTION_IDLE);
  4379. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4380. }
  4381. }
  4382. }
  4383. else
  4384. {
  4385. DEBUG_INFO("Authorize fail.\n");
  4386. ShmCharger->isAuthrizing = FALSE;
  4387. ShmCharger->isGetAuthResult = TRUE;
  4388. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4389. {
  4390. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4391. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4392. }
  4393. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4394. sleep(3);
  4395. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4396. {
  4397. setLedMotion(gun_index,LED_ACTION_IDLE);
  4398. }
  4399. }
  4400. }
  4401. }
  4402. }
  4403. }
  4404. //======================================================
  4405. // Main process
  4406. //======================================================
  4407. int main(void)
  4408. {
  4409. //==================================================
  4410. // Create all share memory
  4411. //==================================================
  4412. if(CreatShareMemory()==0)
  4413. {
  4414. DEBUG_ERROR("CreatShareMemory NG\n");
  4415. if(ShmStatusCodeData!=NULL)
  4416. {
  4417. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  4418. }
  4419. sleep(5);
  4420. system("reboot -f");
  4421. sleep(5);
  4422. system("reboot -f");
  4423. }
  4424. else
  4425. {
  4426. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  4427. DEBUG_INFO("CreatShareMemory OK\n");
  4428. }
  4429. if(SpawnTask() != PASS)
  4430. {
  4431. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4432. DEBUG_ERROR("Task spawn fail.\n");
  4433. }
  4434. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4435. {
  4436. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4437. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = 0xff;
  4438. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  4439. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  4440. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  4441. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  4442. ShmCharger->gun_selectd = 0;
  4443. }
  4444. //==================================================
  4445. // Main loop
  4446. //==================================================
  4447. for(;;)
  4448. {
  4449. //==============================================
  4450. // Period check for 10 seconds
  4451. //==============================================
  4452. if(getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10)
  4453. {
  4454. //==============================================
  4455. // Check task processing
  4456. //==============================================
  4457. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4458. checkTask();
  4459. //==============================================
  4460. // Check connection timeout specification
  4461. //==============================================
  4462. checkConnectionTimeout();
  4463. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  4464. }
  4465. //==============================================
  4466. // Check remote reset request
  4467. //==============================================
  4468. checkReset();
  4469. //==============================================
  4470. // Connector loop
  4471. //==============================================
  4472. /*
  4473. * TODO:
  4474. * 1. Rotary switch define change with system specification
  4475. */
  4476. // Only process main logic on cabinet
  4477. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  4478. {
  4479. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4480. {
  4481. //==============================================
  4482. // Synchronize share memory from OCPP struct
  4483. //==============================================
  4484. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4485. //==============================================
  4486. // Check RFID authorization
  4487. //==============================================
  4488. // The user is not allowed to use RFID when the system change to under those modes.
  4489. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM) &&
  4490. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_FAULT) &&
  4491. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_DEBUG) &&
  4492. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE) &&
  4493. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_BOOTING) &&
  4494. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_COMPLETE) &&
  4495. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_MAINTAIN))
  4496. {
  4497. checkRfidAuthrize();
  4498. }
  4499. //==============================================
  4500. // Something need run in Idle mode
  4501. //==============================================
  4502. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_DEBUG)) &&
  4503. ((modelnameInfo.GetGunCount==2)?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_DEBUG)):true) &&
  4504. ((modelnameInfo.GetGunCount==3)?((ShmSysConfigAndInfo->SysInfo.AcChargingData[2].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[2].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[2].SystemStatus==SYS_MODE_DEBUG)):true))
  4505. {
  4506. // Check restore factory setting request
  4507. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4508. {
  4509. // Set led to initial
  4510. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4511. {
  4512. setLedMotion(gun_index,LED_ACTION_INIT);
  4513. }
  4514. sleep(5);
  4515. system("cd /root;./Module_FactoryConfig -m");
  4516. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4517. system("sync");
  4518. sleep(5);
  4519. system("reboot -f");
  4520. sleep(5);
  4521. system("reboot -f");
  4522. }
  4523. // Check upgrade firmware request
  4524. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  4525. ocpp_get_update_firmware_req())
  4526. {
  4527. // If available memory too small, free memory cache first
  4528. if(getAvailableMemory() < (200*1024*1024))
  4529. {
  4530. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  4531. system("echo 3 > /proc/sys/vm/drop_caches");
  4532. }
  4533. ShmCharger->isUpdateSuccess = NO;
  4534. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4535. {
  4536. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4537. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4538. }
  4539. }
  4540. }
  4541. //==========================================
  4542. // Power saving logic
  4543. //==========================================
  4544. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus > SYS_MODE_BOOTING)
  4545. {
  4546. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4547. {
  4548. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4549. }
  4550. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
  4551. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_FAULT) ||
  4552. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
  4553. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
  4554. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
  4555. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING) ||
  4556. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE))
  4557. {
  4558. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4559. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4560. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4561. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4562. }
  4563. else
  4564. {
  4565. if((ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES) ||
  4566. (gpio_get_value(GPIO_IN_WAKEUP) == OFF) ||
  4567. (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
  4568. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus != ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus) ||
  4569. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_RESERVATION)) &&
  4570. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) &&
  4571. (getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10)))
  4572. {
  4573. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4574. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4575. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4576. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4577. if(((gpio_get_value(GPIO_IN_WAKEUP) == OFF) || (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON)) &&
  4578. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) &&
  4579. (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_CHECK_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4580. {
  4581. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = YES;
  4582. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4583. }
  4584. }
  4585. else
  4586. {
  4587. refreshStartTimer(&startTime[gun_index][TMR_IDX_CHECK_POWER_CONSUMPTION]);
  4588. if((getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_LCM_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4589. {
  4590. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = NO;
  4591. }
  4592. }
  4593. }
  4594. if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  4595. {
  4596. if(ShmCharger->isLcdOn == ON)
  4597. {
  4598. DEBUG_INFO("LCD into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_LCD]));
  4599. ShmCharger->isLcdOn = OFF;
  4600. }
  4601. }
  4602. else
  4603. {
  4604. if(ShmCharger->isLcdOn == OFF)
  4605. {
  4606. DEBUG_INFO("LCD exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_LCD]));
  4607. ShmCharger->isLcdOn = ON;
  4608. }
  4609. }
  4610. if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  4611. {
  4612. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  4613. {
  4614. DEBUG_INFO("RFID into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4615. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  4616. }
  4617. }
  4618. else
  4619. {
  4620. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  4621. {
  4622. DEBUG_INFO("RFID exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4623. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  4624. }
  4625. }
  4626. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  4627. {
  4628. if(ShmCharger->gun_info[gun_index].isMeterOn)
  4629. {
  4630. DEBUG_INFO("Meter into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4631. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  4632. }
  4633. }
  4634. else
  4635. {
  4636. if(!ShmCharger->gun_info[gun_index].isMeterOn)
  4637. {
  4638. DEBUG_INFO("Meter exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4639. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  4640. }
  4641. }
  4642. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]) > TIMEOUT_SPEC_POWERSAVING_LED_STATUS)
  4643. {
  4644. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4645. {
  4646. DEBUG_INFO("LED status into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4647. ShmCharger->gun_info[gun_index].isSleepOn = YES;
  4648. }
  4649. }
  4650. else
  4651. {
  4652. if(ShmCharger->gun_info[gun_index].isSleepOn == YES)
  4653. {
  4654. DEBUG_INFO("LED status exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4655. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4656. }
  4657. }
  4658. }
  4659. //==========================================
  4660. // Synchronize current rating value from MCU
  4661. //==========================================
  4662. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4663. //==========================================
  4664. // Assign connector location index for OCPP
  4665. //==========================================
  4666. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  4667. //==========================================
  4668. // Synchronize present charging power
  4669. //==========================================
  4670. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  4671. {
  4672. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4673. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000.0;
  4674. else
  4675. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4676. }
  4677. else
  4678. {
  4679. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4680. {
  4681. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000.0) +
  4682. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2)/1000.0) +
  4683. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3)/1000.0));
  4684. }
  4685. else
  4686. {
  4687. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4688. }
  4689. }
  4690. //==========================================
  4691. // Check initialization "PASS" or "FAIL"
  4692. //==========================================
  4693. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4694. {
  4695. // Alarm event check
  4696. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0) || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP)
  4697. {
  4698. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  4699. {
  4700. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  4701. {
  4702. setChargerMode(gun_index, SYS_MODE_ALARM);
  4703. }
  4704. }
  4705. }
  4706. }
  4707. //==========================================
  4708. // Reservation request check
  4709. //==========================================
  4710. checkReservation(gun_index);
  4711. //==========================================
  4712. // Change availability check
  4713. //==========================================
  4714. checkAvailability(gun_index);
  4715. if(ShmCharger->gun_info[gun_index].isOperactive)
  4716. {
  4717. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  4718. {
  4719. setChargerMode(gun_index, SYS_MODE_IDLE);
  4720. }
  4721. }
  4722. else
  4723. {
  4724. if(isMode(gun_index, SYS_MODE_IDLE))
  4725. {
  4726. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4727. }
  4728. }
  4729. //==========================================
  4730. // Unlock Connector signal check
  4731. //==========================================
  4732. checkUnlocker(gun_index);
  4733. //==========================================
  4734. // Connector process
  4735. //==========================================
  4736. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4737. {
  4738. case SYS_MODE_BOOTING:
  4739. if(isModeChange(gun_index))
  4740. {
  4741. setLedMotion(gun_index,LED_ACTION_INIT);
  4742. //CSU Initialization & task spawn
  4743. if(Initialization(gun_index) != PASS)
  4744. {
  4745. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4746. }
  4747. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4748. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4749. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4750. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4751. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4752. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4753. }
  4754. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  4755. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  4756. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  4757. {
  4758. // OCPP BootNotification info set
  4759. DEBUG_INFO("==========================================\n");
  4760. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  4761. DEBUG_INFO("==========================================\n");
  4762. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  4763. ocpp_boot_info_sync();
  4764. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  4765. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  4766. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  4767. DEBUG_INFO("==========================================\n");
  4768. DEBUG_INFO("========== Set Wifi information ==========\n");
  4769. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  4770. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  4771. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  4772. DEBUG_INFO("==========================================\n");
  4773. // Default Ethernet / Wifi / 4G to 1:disconnected
  4774. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  4775. {
  4776. case 'E':
  4777. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4778. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  4779. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  4780. break;
  4781. case 'W':
  4782. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4783. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4784. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  4785. break;
  4786. case 'T':
  4787. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4788. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  4789. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4790. break;
  4791. case 'D':
  4792. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4793. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4794. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4795. break;
  4796. }
  4797. // Default LCM brightness to 100
  4798. ShmCharger->isLcdOn = ON;
  4799. // Defaule led status to not sleep mode
  4800. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4801. // If Web Server OPCC URL is empty kill Module_OcppBackend
  4802. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0) || ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  4803. {
  4804. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4805. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  4806. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  4807. DEBUG_INFO("Local power sharing enable kill Module_OcppBackend...\n");
  4808. system ("pkill OcppBackend");
  4809. }
  4810. // If rotate switch equal zero, the system needs to change Debug mode
  4811. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  4812. setChargerMode(gun_index, SYS_MODE_DEBUG);
  4813. else
  4814. {
  4815. if((isGpioInitialized(GPIO_OUT_RST_RFID) == PASS) &&
  4816. (isGpioInitialized(GPIO_IN_WAKEUP) == PASS) &&
  4817. (isGpioInitialized(GPIO_OUT_RST_ETH) == PASS) &&
  4818. (isGpioInitialized(GPIO_OUT_RST_4G) == PASS) &&
  4819. (isGpioInitialized(GPIO_OUT_RST_QCA) == PASS))
  4820. {
  4821. //==================================================
  4822. // Get firmware version
  4823. //==================================================
  4824. sleep(3);
  4825. get_firmware_version(gun_index);
  4826. setChargerMode(gun_index, SYS_MODE_IDLE);
  4827. }
  4828. }
  4829. }
  4830. break;
  4831. case SYS_MODE_IDLE:
  4832. if(isModeChange(gun_index))
  4833. {
  4834. setLedMotion(gun_index,LED_ACTION_IDLE);
  4835. setRelay(gun_index, OFF);
  4836. setRequest(gun_index,OFF);
  4837. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4838. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4839. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4840. ocpp_set_remotestart(gun_index, OFF);
  4841. ocpp_set_remotestop(gun_index, OFF);
  4842. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  4843. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  4844. presentChargedEnergyClear(gun_index);
  4845. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  4846. ocpp_set_unlocker_req(gun_index, OFF);
  4847. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  4848. // Response StopTransactionConf
  4849. ocpp_set_stoptransaction_conf(gun_index, OFF);
  4850. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4851. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  4852. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4853. DB_Check_Record_Buf(localDb, gun_index);
  4854. }
  4855. // LED status in Idle mode
  4856. if(!ShmCharger->isAuthrizing)
  4857. {
  4858. if(ocpp_get_isRemoteStartWait())
  4859. {
  4860. ShmCharger->gun_info[gun_index].isSleepOn = OFF;
  4861. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4862. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4863. checkHandshakeTime();
  4864. }
  4865. else
  4866. {
  4867. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON)
  4868. {
  4869. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  4870. {
  4871. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4872. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_CONNECTED);
  4873. else
  4874. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP);
  4875. }
  4876. }
  4877. else
  4878. {
  4879. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  4880. {
  4881. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4882. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_DISCONNECTED);
  4883. else
  4884. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP);
  4885. }
  4886. }
  4887. }
  4888. }
  4889. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  4890. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  4891. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) ||
  4892. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4893. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  4894. (ocpp_get_remotestart(gun_index) == ON) ||
  4895. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  4896. {
  4897. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4898. {
  4899. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4900. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  4901. DEBUG_INFO("Start Method : RFID...\n");
  4902. }
  4903. else if(ocpp_get_remotestart(gun_index))
  4904. {
  4905. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4906. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  4907. ocpp_copy_userid_from_remotestart(gun_index);
  4908. setSpeaker(ON, SPEAKER_SHORT);
  4909. DEBUG_INFO("Start Method : BACKEND...\n");
  4910. }
  4911. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  4912. {
  4913. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  4914. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
  4915. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  4916. setSpeaker(ON, SPEAKER_SHORT);
  4917. DEBUG_INFO("Start Method : BLE...\n");
  4918. }
  4919. else
  4920. {
  4921. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  4922. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
  4923. //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  4924. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  4925. setSpeaker(ON, SPEAKER_SHORT);
  4926. DEBUG_INFO("Start Method : FREE...\n");
  4927. }
  4928. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4929. ocpp_set_remotestart(gun_index, OFF);
  4930. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4931. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  4932. // Get target current
  4933. ocpp_set_profile_req(gun_index, ON);
  4934. sleep(1);
  4935. checkChargingProfileLimit(gun_index);
  4936. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4937. }
  4938. else
  4939. {}
  4940. break;
  4941. case SYS_MODE_AUTHORIZING:
  4942. if(isModeChange(gun_index))
  4943. {
  4944. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  4945. if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  4946. {
  4947. DEBUG_INFO("Remote start request authorize.\n");
  4948. ocpp_set_auth_req(ON, "ISO14443");
  4949. }
  4950. }
  4951. if(getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  4952. {
  4953. // Authorization timeout process.
  4954. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4955. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4956. sleep(3);
  4957. setChargerMode(gun_index, SYS_MODE_IDLE);
  4958. DEBUG_WARN("Authorize timeout !!!\n");
  4959. }
  4960. else
  4961. {
  4962. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  4963. {
  4964. case START_METHOD_BACKEND:
  4965. if(ocpp_isAuthorizeRemoteStart())
  4966. {
  4967. if(ocpp_get_auth_conf())
  4968. {
  4969. if(ocpp_get_auth_result(gun_index))
  4970. {
  4971. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4972. DEBUG_INFO("Authorize pass.\n");
  4973. setSpeaker(ON, SPEAKER_SHORT);
  4974. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  4975. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4976. }
  4977. else
  4978. {
  4979. DEBUG_INFO("Authorize fail.\n");
  4980. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4981. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4982. sleep(3);
  4983. setChargerMode(gun_index, SYS_MODE_IDLE);
  4984. }
  4985. ocpp_set_auth_conf(OFF);
  4986. }
  4987. }
  4988. else
  4989. {
  4990. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4991. }
  4992. break;
  4993. case START_METHOD_RFID:
  4994. case START_METHOD_BLE:
  4995. case START_METHOD_FREE:
  4996. default:
  4997. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4998. break;
  4999. }
  5000. }
  5001. break;
  5002. case SYS_MODE_PREPARING:
  5003. if(isModeChange(gun_index))
  5004. {
  5005. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  5006. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5007. setLedMotion(gun_index, LED_ACTION_AUTHED);
  5008. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5009. if(ShmCharger->isCcsEnable)refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5010. }
  5011. // If control pilot detect Bx, skip watch dog time out.
  5012. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  5013. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  5014. {
  5015. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5016. if(!getRequest(gun_index))
  5017. {
  5018. setRequest(gun_index, ON);
  5019. setLedMotion(gun_index, LED_ACTION_CONNECTED);
  5020. DEBUG_INFO("Set Request On.\n");
  5021. }
  5022. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  5023. {
  5024. case HANDSHAKE_DUTY_5:
  5025. break;
  5026. case HANDSHAKE_DUTY_5_CHECK:
  5027. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5028. DEBUG_INFO("HLC slac handshake start.\n");
  5029. break;
  5030. case HANDSHAKE_CCS:
  5031. //CCS handshake timeout
  5032. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  5033. {
  5034. DEBUG_INFO("HLC %d secs slac handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  5035. }
  5036. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  5037. {
  5038. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  5039. }
  5040. //CCS status check
  5041. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  5042. {
  5043. DEBUG_INFO("Enter HLC Mode charging.\n");
  5044. }
  5045. break;
  5046. case HANDSHAKE_CP_STATE_E:
  5047. //if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5048. {
  5049. // restore normal CP PWM duty
  5050. // Determine max charging current to MCU
  5051. DEBUG_INFO("Determine max charging current to MCU.\n");
  5052. ocpp_set_profile_req(gun_index, ON);
  5053. sleep(1);
  5054. checkChargingProfileLimit(gun_index);
  5055. }
  5056. break;
  5057. case HANDSHAKE_SET_MAX_CURRENT:
  5058. case HANDSHAKE_BS_MODE:
  5059. DEBUG_INFO("Enter BS Mode charging.\n");
  5060. checkChargingProfileLimit(gun_index);
  5061. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5062. setRelay(gun_index,ON);
  5063. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  5064. {
  5065. ocpp_set_unlocker_req(gun_index, OFF);
  5066. presentChargedEnergyClear(gun_index);
  5067. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  5068. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5069. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5070. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5071. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5072. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5073. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5074. else
  5075. {
  5076. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5077. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5078. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5079. }
  5080. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5081. ocpp_copy_userid_to_starttransaction(gun_index);
  5082. ocpp_set_starttransaction_req(gun_index, ON);
  5083. refreshStartTimer(&startChargingTime[gun_index]);
  5084. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5085. }
  5086. break;
  5087. case HANDSHAKE_HLC_MODE:
  5088. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)//powerDelivery
  5089. {
  5090. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  5091. {
  5092. setRelay(gun_index,ON);
  5093. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  5094. DEBUG_INFO("HANDSHAKE_HLC_MODE:setRelay ON\n");
  5095. }
  5096. }
  5097. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  5098. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  5099. {
  5100. DEBUG_INFO("HANDSHAKE_HLC_MODE:Relay ON done\n");
  5101. ocpp_set_unlocker_req(gun_index, OFF);
  5102. presentChargedEnergyClear(gun_index);
  5103. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  5104. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5105. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5106. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5107. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5108. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5109. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5110. else
  5111. {
  5112. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5113. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5114. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5115. }
  5116. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5117. ocpp_copy_userid_to_starttransaction(gun_index);
  5118. ocpp_set_starttransaction_req(gun_index, ON);
  5119. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  5120. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5121. DEBUG_INFO("set SYS_MODE_CHARGING\n");
  5122. refreshStartTimer(&startChargingTime[gun_index]);
  5123. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5124. }
  5125. //120 sec timeout
  5126. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  5127. {
  5128. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5129. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > (TIMEOUT_SPEC_CCS_HANDSHAKE+5))
  5130. {
  5131. DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
  5132. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  5133. {
  5134. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  5135. }
  5136. }
  5137. }
  5138. break;
  5139. default:
  5140. break;
  5141. }
  5142. }
  5143. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  5144. {
  5145. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  5146. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  5147. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5148. setRelay(gun_index,ON);
  5149. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5150. ocpp_copy_userid_to_starttransaction(gun_index);
  5151. ocpp_set_starttransaction_req(gun_index, ON);
  5152. refreshStartTimer(&startChargingTime[gun_index]);
  5153. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5154. }
  5155. // Unplug charging gun to Idle mode
  5156. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  5157. {
  5158. DEBUG_INFO("Charging gun is plugged before.\n");
  5159. setChargerMode(gun_index, SYS_MODE_IDLE);
  5160. }
  5161. // Use RFID card to stop handshaking
  5162. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchPresentUser(gun_index))
  5163. {
  5164. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  5165. setChargerMode(gun_index, SYS_MODE_IDLE);
  5166. }
  5167. else
  5168. {
  5169. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5170. }
  5171. checkHandshakeTime();
  5172. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  5173. {
  5174. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5175. DEBUG_INFO("Handshaking timeout...%d\n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  5176. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5))
  5177. {
  5178. setChargerMode(gun_index, SYS_MODE_IDLE);
  5179. }
  5180. }
  5181. break;
  5182. case SYS_MODE_CHARGING:
  5183. if(isModeChange(gun_index))
  5184. {
  5185. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5186. //ftime(&startChargingTime[gun_index]);
  5187. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5188. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5189. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  5190. ocpp_set_auth_req(OFF);
  5191. ocpp_set_profile_req(gun_index, ON);
  5192. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5193. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5194. }
  5195. //if time up, clear CCS MSG count
  5196. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5197. (getDiffSecNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  5198. {
  5199. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5200. }
  5201. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5202. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5203. ocpp_get_remotestop(gun_index) ||
  5204. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5205. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  5206. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  5207. ocpp_get_reset_req() ||
  5208. ocpp_get_unlocker_req(gun_index) ||
  5209. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5210. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  5211. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  5212. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE) ||
  5213. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ((49 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus <= 255))))
  5214. {
  5215. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5216. }
  5217. else
  5218. {
  5219. // Charging session info calculation
  5220. setLedMotion(gun_index,LED_ACTION_CHARGING);
  5221. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5222. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5223. else
  5224. {
  5225. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5226. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5227. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5228. }
  5229. refreshStartTimer(&endChargingTime[gun_index]);
  5230. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5231. presentChargedEnergyUpdate(gun_index);
  5232. // Response StartTransactionConf
  5233. ocpp_set_starttransaction_conf(gun_index, OFF);
  5234. // Charging profile preparation
  5235. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  5236. {
  5237. if(!ocpp_get_profile_conf(gun_index))
  5238. {
  5239. ocpp_set_profile_req(gun_index, ON);
  5240. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5241. }
  5242. else
  5243. {
  5244. ocpp_set_profile_conf(gun_index, OFF);
  5245. }
  5246. }
  5247. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5248. checkChargingProfileLimit(gun_index);
  5249. // Charging session target current check if OCPP disconnect and power sharing server connected
  5250. if(!ocpp_get_connection_status() && ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  5251. {
  5252. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  5253. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent;
  5254. else
  5255. {
  5256. if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
  5257. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  5258. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  5259. }
  5260. }
  5261. // Charging session local limit condition check
  5262. if(ocpp_get_connection_status())
  5263. {
  5264. // On-line max condition check
  5265. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  5266. {
  5267. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5268. {
  5269. setRelay(gun_index, OFF);
  5270. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5271. }
  5272. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5273. {
  5274. setRelay(gun_index, OFF);
  5275. }
  5276. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5277. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  5278. }
  5279. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  5280. {
  5281. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5282. {
  5283. setRelay(gun_index, OFF);
  5284. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5285. }
  5286. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5287. {
  5288. setRelay(gun_index, OFF);
  5289. }
  5290. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5291. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  5292. }
  5293. else
  5294. {
  5295. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5296. {
  5297. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5298. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5299. setRelay(gun_index, ON);
  5300. else
  5301. setRelay(gun_index, OFF);
  5302. }
  5303. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5304. {
  5305. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5306. setRelay(gun_index, ON);
  5307. }
  5308. }
  5309. }
  5310. else
  5311. {
  5312. // Off-line max condition check
  5313. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  5314. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  5315. {
  5316. if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
  5317. {
  5318. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5319. {
  5320. setRelay(gun_index, OFF);
  5321. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5322. }
  5323. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5324. {
  5325. setRelay(gun_index, OFF);
  5326. }
  5327. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5328. DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  5329. }
  5330. else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  5331. {
  5332. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5333. {
  5334. setRelay(gun_index, OFF);
  5335. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5336. }
  5337. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5338. {
  5339. setRelay(gun_index, OFF);
  5340. }
  5341. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5342. DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  5343. }
  5344. else
  5345. {
  5346. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5347. {
  5348. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5349. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5350. setRelay(gun_index, ON);
  5351. else
  5352. setRelay(gun_index, OFF);
  5353. }
  5354. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5355. {
  5356. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5357. setRelay(gun_index, ON);
  5358. }
  5359. }
  5360. }
  5361. else
  5362. {
  5363. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state > 0)
  5364. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  5365. setRelay(gun_index, OFF);
  5366. }
  5367. }
  5368. // Debug information
  5369. if(getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_REFRESH_CHARGING_INFO)
  5370. {
  5371. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  5372. DB_Update_Record_Buf(localDb, gun_index);
  5373. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5374. }
  5375. }
  5376. break;
  5377. case SYS_MODE_TERMINATING:
  5378. if(isModeChange(gun_index))
  5379. {
  5380. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  5381. {
  5382. setLedMotion(gun_index, LED_ACTION_STOP);
  5383. }
  5384. else
  5385. {}
  5386. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  5387. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5388. }
  5389. refreshStartTimer(&endChargingTime[gun_index]);
  5390. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  5391. {
  5392. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5393. }
  5394. // End authorize pass
  5395. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) &&isMatchStartUser(gun_index)) ||
  5396. ((ShmCharger->gun_info[gun_index].rfidReq == ON) && ocpp_get_auth_result(YES, gun_index)) ||
  5397. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  5398. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  5399. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5400. ocpp_get_remotestop(gun_index) ||
  5401. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5402. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  5403. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  5404. ocpp_get_reset_req() ||
  5405. ocpp_get_unlocker_req(gun_index) ||
  5406. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5407. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  5408. {
  5409. //setRelay(gun_index, OFF);
  5410. setLedMotion(gun_index, LED_ACTION_STOP);
  5411. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5412. {
  5413. checkStopReason(gun_index);
  5414. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5415. }
  5416. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5417. {
  5418. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5419. //Cancel CCS task negotiating
  5420. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5421. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5422. setRelay(gun_index,OFF);
  5423. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5424. }
  5425. else
  5426. {
  5427. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5428. }
  5429. }
  5430. else
  5431. {
  5432. // If the charger stops charging, the led should be stoped blink
  5433. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  5434. {
  5435. setLedMotion(gun_index, LED_ACTION_STOP);
  5436. }
  5437. // Charging profile preparation
  5438. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  5439. {
  5440. if(!ocpp_get_profile_conf(gun_index))
  5441. {
  5442. ocpp_set_profile_req(gun_index, ON);
  5443. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5444. }
  5445. else
  5446. {
  5447. ocpp_set_profile_conf(gun_index, OFF);
  5448. }
  5449. }
  5450. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5451. checkChargingProfileLimit(gun_index);
  5452. if(!ocpp_get_auth_result(YES, gun_index))
  5453. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5454. if((((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)) &&
  5455. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  5456. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  5457. !ocpp_get_remotestop(gun_index) &&
  5458. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  5459. !ocpp_get_reset_req() &&
  5460. ocpp_get_starttransaction_result(gun_index) &&
  5461. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  5462. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  5463. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
  5464. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))) &&
  5465. (((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS))
  5466. )
  5467. {
  5468. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5469. }
  5470. }
  5471. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5472. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  5473. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  5474. {
  5475. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  5476. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  5477. checkStopReason(gun_index);
  5478. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5479. }
  5480. break;
  5481. case SYS_MODE_COMPLETE:
  5482. if(isModeChange(gun_index))
  5483. {
  5484. setLedMotion(gun_index, LED_ACTION_STOP);
  5485. setRelay(gun_index, OFF);
  5486. setRequest(gun_index, OFF);
  5487. refreshStartTimer(&startTime[0][TMR_IDX_DISP_STOPSUM]);
  5488. }
  5489. if(((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (getDiffSecNow(startTime[0][TMR_IDX_DISP_STOPSUM]) >= TIMEOUT_SPEC_STOPSUM_DISP)) ||
  5490. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)) ||
  5491. ocpp_get_reset_req())
  5492. {
  5493. sleep(2);
  5494. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5495. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  5496. ocpp_set_remotestop(gun_index, OFF);
  5497. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  5498. DB_Insert_Record(localDb, gun_index);
  5499. setChargerMode(gun_index, SYS_MODE_IDLE);
  5500. }
  5501. break;
  5502. case SYS_MODE_ALARM:
  5503. setLedMotion(gun_index,LED_ACTION_ALARM);
  5504. setRelay(gun_index, OFF);
  5505. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0) && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP)
  5506. {
  5507. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  5508. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  5509. {
  5510. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  5511. }
  5512. else
  5513. {
  5514. setChargerMode(gun_index, SYS_MODE_IDLE);
  5515. }
  5516. }
  5517. break;
  5518. case SYS_MODE_FAULT:
  5519. if(isModeChange(gun_index))
  5520. {}
  5521. break;
  5522. case SYS_MODE_MAINTAIN:
  5523. if(isModeChange(gun_index))
  5524. {
  5525. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5526. }
  5527. break;
  5528. case SYS_MODE_UPDATE:
  5529. if(isModeChange(gun_index))
  5530. {
  5531. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5532. }
  5533. //======================================
  5534. // Check local upgrade firmware request
  5535. //======================================
  5536. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  5537. {
  5538. DEBUG_INFO("Firmware local upgrading...\n");
  5539. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  5540. if(upgrade_check() == PASS)
  5541. {
  5542. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  5543. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  5544. ShmCharger->isUpdateSuccess = YES;
  5545. DEBUG_INFO("Local update success.\n");
  5546. }
  5547. else
  5548. {
  5549. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  5550. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  5551. ShmCharger->isUpdateSuccess = NO;
  5552. DEBUG_INFO("Local update fail.\n");
  5553. }
  5554. }
  5555. else if(ocpp_get_update_firmware_req())
  5556. {
  5557. //======================================
  5558. // Check remote upgrade firmware request
  5559. //======================================
  5560. checkRemoteUpgradeStatus();
  5561. }
  5562. else
  5563. {
  5564. //======================================
  5565. // Upgrade complete reboot system
  5566. //======================================
  5567. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((modelnameInfo.GetGunCount>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES) && !ShmCharger->isUpgradeLcmReq)
  5568. {
  5569. if(ShmCharger->isUpdateSuccess == YES)
  5570. {
  5571. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5572. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5573. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5574. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5575. DEBUG_WARN("Firmware upgrade success.\n");
  5576. sleep(5);
  5577. DEBUG_INFO("Firmware upgraded, reboot...\n");
  5578. system("reboot -f");
  5579. sleep(5);
  5580. system("reboot -f");
  5581. }
  5582. else
  5583. {
  5584. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5585. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5586. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5587. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5588. DEBUG_WARN("Firmware upgrade fail.\n");
  5589. sleep(5);
  5590. system("rm -rvf /mnt/* ");
  5591. close(wtdFd);
  5592. system("/usr/bin/run_evse_restart.sh");
  5593. }
  5594. }
  5595. }
  5596. break;
  5597. case SYS_MODE_RESERVATION:
  5598. if(isModeChange(gun_index))
  5599. {}
  5600. setLedMotion(gun_index,LED_ACTION_RESERVATION_MODE);
  5601. if(isReservationExpired(gun_index))
  5602. {
  5603. DEBUG_INFO("Reservation: Time's up...\n");
  5604. setChargerMode(gun_index, SYS_MODE_IDLE);
  5605. }
  5606. else if(ocpp_get_cancelreservation_req(gun_index))
  5607. {
  5608. DEBUG_INFO("Reservation: Cancel reservation...\n");
  5609. setChargerMode(gun_index, SYS_MODE_IDLE);
  5610. ocpp_set_cancelreservation_req(gun_index, OFF);
  5611. }
  5612. else
  5613. {
  5614. // Check is there RFID or back end request start
  5615. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5616. ocpp_get_remotestart(gun_index))
  5617. {
  5618. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5619. {
  5620. if(ocpp_compare_reserve_id_with_user(gun_index))
  5621. {
  5622. DEBUG_INFO("Start Method in reservation : RFID...\n");
  5623. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5624. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5625. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5626. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5627. }
  5628. else
  5629. {
  5630. setLedMotion(gun_index,LED_ACTION_IDLE);
  5631. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5632. DEBUG_INFO("It's not reserve user id.\n");
  5633. }
  5634. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5635. }
  5636. else if(ocpp_get_remotestart(gun_index))
  5637. {
  5638. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  5639. {
  5640. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  5641. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  5642. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  5643. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5644. }
  5645. ocpp_set_remotestop(gun_index, OFF);
  5646. }
  5647. }
  5648. }
  5649. break;
  5650. case SYS_MODE_BOOKING:
  5651. if(isModeChange(gun_index))
  5652. {}
  5653. break;
  5654. case SYS_MODE_DEBUG:
  5655. if(isModeChange(gun_index))
  5656. {
  5657. setLedMotion(gun_index,LED_ACTION_DEBUG);
  5658. }
  5659. break;
  5660. }
  5661. }
  5662. }
  5663. // System watch dog reset
  5664. write(wtdFd, "a", 1);
  5665. usleep(50000);
  5666. }
  5667. return FAIL;
  5668. }