main.c 274 KB

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