main.c 245 KB

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