main.c 252 KB

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