main.c 229 KB

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