main.c 283 KB

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