main.c 236 KB

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