main.c 241 KB

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