main.c 229 KB

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