main.c 276 KB

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