main.c 247 KB

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