main.c 254 KB

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