main.c 264 KB

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