main.c 278 KB

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