main.c 272 KB

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