main.c 247 KB

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