main.c 321 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include <ifaddrs.h>
  27. #include <math.h>
  28. #include <stdbool.h>
  29. #include <dirent.h>
  30. #include "../../define.h"
  31. #include "CheckSystemTask.h"
  32. #include "Config.h"
  33. #include "timeout.h"
  34. #define DEBUG_ALSTON 0
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL -1
  38. #define MODELNAME_FAIL 0
  39. #define BUFFER_SIZE 128
  40. #define YES 1
  41. #define NO 0
  42. #define NORMAL 0
  43. #define ABNORMAL 1
  44. #define EQUAL 0
  45. #define BTN_RELEASE 0
  46. #define BTN_PRESS 1
  47. #define MAX_BUF 64
  48. //#define MtdBlockSize 0x600000
  49. #define MtdBlockSize 0x300000
  50. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  51. #define UPGRADE_FAN 0x02
  52. #define UPGRADE_RB 0x03
  53. #define UPGRADE_PRI 0x04
  54. #define UPGRADE_AC 0x05
  55. #define UPGRADE_LED 0x06
  56. #define SYSTEM_MIN_VOL 80
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  63. #define NETWORK_DB_FILE "/Storage/EventLog/Eventlog.db"
  64. #define GUN_OTP_VALUE 150
  65. #define GUN_OTP_RECOVERY 140
  66. #define UNDEFINED_TEMP 255
  67. #define uSEC_VAL 1000000
  68. #define SELFTEST_TIMEOUT 60
  69. #define AUTHORIZE_TIMEOUT 15
  70. #define AUTHORIZE_COMP_TIMEOUT 3
  71. #define AUTHORIZE_FAIL_TIMEOUT 3
  72. #define AUTHORIZE_STOP_TIMEOUT 30
  73. #define RETURN_TO_CHARGING_PAGE 30
  74. #define GUN_PREPARE_TIMEOUT 30
  75. #define GUN_EV_WAIT_TIMEOUT 120
  76. #define GUN_EVSE_WAIT_TIMEOUT 60
  77. #define GUN_COMP_WAIT_TIMEOUT 10
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. #define TIMEOUT_SPEC_HANDSHAKING 180000
  80. char *valid_Internet [2] = { "8.8.8.8", "180.76.76.76" };
  81. unsigned char mask_table [] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  82. int whileLoopTime = 10000; // 10 ms
  83. int wtdFd = - 1;
  84. byte _authorizeIndex = NO_DEFINE;
  85. bool IsAuthorizingMode();
  86. void ClearAuthorizedFlag();
  87. bool isDetectPlugin();
  88. void ClearDetectPluginFlag();
  89. int mystrcmp(unsigned char *p1, unsigned char *p2);
  90. void CheckTask();
  91. long long DiffTimebWithNow(struct timeb ST);
  92. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  93. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  94. void ChargingTerminalProcess(byte gunIndex);
  95. void ChkPrimaryStatus();
  96. void StartSystemTimeoutDet(unsigned char flag);
  97. void StopSystemTimeoutDet();
  98. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  99. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  100. int StoreLogMsg_1(const char *fmt, ...);
  101. int GetTimeoutValue(struct timespec *startTime);
  102. void gpio_set_value(unsigned int gpio, unsigned int value);
  103. void PRINTF_FUNC(char *string, ...);
  104. void ChangeGunSelectByIndex(byte sel);
  105. void ChargingAlarmProcess(byte gunIndex);
  106. void CheckSystemErrorFunction(byte index);
  107. int opcc_chk_reserve_expired(byte gunIndex);
  108. void ocpp_process_start();
  109. void ocpp_auto_response_BootNotification();
  110. void ocpp_auto_response_StartTransationConf(byte gunIndex);
  111. bool ocpp_chk_authrization_cmd();
  112. void ocpp_chk_reset_cmd(byte isforce);
  113. void ocpp_chk_reserved_cmd(byte gunIndex);
  114. void ocpp_chk_availability_cmd(byte gunIndex);
  115. void ocpp_chk_unlock_cmd(byte gunIndex);
  116. bool ocpp_chk_remoteStop_cmd(byte gunIndex);
  117. void ocpp_chk_remoteStart_cmd();
  118. void ocpp_chk_update_cmd();
  119. bool ocpp_chk_invalid_id_cmd(byte gunIndex);
  120. void ocpp_set_startTransation_cmd(byte gunIndex);
  121. void ocpp_set_stopTransation_cmd(byte gunIndex);
  122. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString);
  123. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason);
  124. void ocpp_set_authorizeReq_cmd(byte value);
  125. void ocpp_set_authorizeConf_cmd(byte value);
  126. void ocpp_set_errCode_cmd(byte gunIndex);
  127. void ocpp_clear_errorCode_cmd(byte gunIndex);
  128. void ocpp_clear_idTag_cmd(byte gun_index);
  129. void ocpp_clear_stopReason(byte gun_index);
  130. bool ocpp_get_authorize_conf();
  131. unsigned short _ocpp_get_connect_timeout();
  132. void ocpp_chargingProfile_process(byte gun_index);
  133. void RecordAlarmCode(byte gunIndex, char *code);
  134. void ReleaseSysAlarmCode(byte gunIndex);
  135. void ReviewCriticalAlarm();
  136. int DB_Open(sqlite3 *db);
  137. int DB_Insert_Record(sqlite3 *db, int gun_index);
  138. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  139. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  140. void InitialDHCP();
  141. int GetStartScheduleTime(unsigned char *time);
  142. void KillAllTask();
  143. void CheckSystemTaskAlive();
  144. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  145. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  146. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  147. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  148. struct StatusCodeData *ShmStatusCodeData;
  149. struct PsuData *ShmPsuData;
  150. struct CHAdeMOData *ShmCHAdeMOData;
  151. struct GBTData *ShmGBTData;
  152. struct CcsData *ShmCcsData;
  153. struct PrimaryMcuData *ShmPrimaryMcuData;
  154. struct FanModuleData *ShmFanModuleData;
  155. struct RelayModuleData *ShmRelayModuleData;
  156. struct LedModuleData *ShmLedModuleData;
  157. struct OCPP16Data *ShmOCPP16Data;
  158. struct OCPP20Data *ShmOCPP20Data;
  159. struct MeterInformation *ShmCsuMeterData;
  160. struct DcCommonInformation *ShmDcCommonData;
  161. struct ChargingInfoData *chargingInfo [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  162. struct ChargingInfoData *ac_chargingInfo [AC_QUANTITY];
  163. struct timeb startChargingTime [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  164. struct timeb endChargingTime [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  165. int _presentChargingTimeBuf [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  166. struct timespec _startTransation_time [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  167. float gunOutputVol [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  168. float _ChargingProfilePwBuffer [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  169. float _AcChargingProfilePwBuffer;
  170. GunErr errCollect;
  171. struct timespec _autoRuntestTime;
  172. byte _isTestRun = NO;
  173. // for initial index to check EV board type is correct
  174. byte _gunIndex = 0;
  175. byte _acgunIndex = 0;
  176. byte _chademoIndex = 0;
  177. byte _ccsIndex = 0;
  178. byte _gb_Index = 0;
  179. byte _ac_Index = 0;
  180. byte bd0_1_status = 0;
  181. byte bd0_2_status = 0;
  182. byte bd1_1_status = 0;
  183. byte bd1_2_status = 0;
  184. bool isCardScan = false;
  185. bool isModelNameMatch = true;
  186. int rfidFd = - 1;
  187. char* rfidPortName = "/dev/ttyS2";
  188. char* fwVersion = "V2.02.00.0000.00";
  189. sqlite3 *localDb;
  190. bool isDb_ready;
  191. sqlite3 *networkDb;
  192. char MyDefaultPriceString [128];
  193. byte reset4gStep = RESET_4G_STEP_NONE;
  194. //LWN_Debug
  195. bool CheckDebugInfoTimeout(byte index, byte timeout);
  196. //LWN_Zanobe
  197. bool _OCPP_LocalAuthorizeOffline = YES;
  198. bool isSendStartTransReq [2] = { FALSE, FALSE }; //紀錄是否已送start transaction req, 為避免重複發送.
  199. //byte AuthGunIndex = NO_DEFINE;
  200. //================================================
  201. // initial can-bus
  202. //================================================
  203. int InitCanBus()
  204. {
  205. int s0 , nbytes;
  206. struct timeval tv;
  207. struct ifreq ifr0;
  208. struct sockaddr_can addr0;
  209. system ( "/sbin/ip link set can0 down" );
  210. system ( "/sbin/ip link set can0 type can bitrate 500000 restart-ms 100" );
  211. system ( "/sbin/ip link set can0 up" );
  212. s0 = socket ( PF_CAN, SOCK_RAW, CAN_RAW );
  213. tv.tv_sec = 0;
  214. tv.tv_usec = 10000;
  215. if (setsockopt ( s0, SOL_SOCKET, SO_RCVTIMEO, (char *) & tv, sizeof(struct timeval) ) < 0)
  216. {
  217. #ifdef SystemLogMessage
  218. DEBUG_ERROR_MSG( "Set SO_RCVTIMEO NG" );
  219. #endif
  220. }
  221. nbytes = 40960;
  222. if (setsockopt ( s0, SOL_SOCKET, SO_RCVBUF, & nbytes, sizeof(int) ) < 0)
  223. {
  224. #ifdef SystemLogMessage
  225. DEBUG_ERROR_MSG( "Set SO_RCVBUF NG" );
  226. #endif
  227. }
  228. nbytes = 40960;
  229. if (setsockopt ( s0, SOL_SOCKET, SO_SNDBUF, & nbytes, sizeof(int) ) < 0)
  230. {
  231. #ifdef SystemLogMessage
  232. DEBUG_ERROR_MSG( "Set SO_SNDBUF NG" );
  233. #endif
  234. }
  235. strcpy ( ifr0.ifr_name, "can0" );
  236. ioctl ( s0, SIOCGIFINDEX, & ifr0 ); /* ifr.ifr_ifindex gets filled with that device's index */
  237. addr0.can_family = AF_CAN;
  238. addr0.can_ifindex = ifr0.ifr_ifindex;
  239. bind ( s0, (struct sockaddr *) & addr0, sizeof(addr0) );
  240. return s0;
  241. }
  242. //================================================
  243. // initial uart port
  244. //================================================
  245. char *_priPortName = "/dev/ttyS1";
  246. char *_485PortName = "/dev/ttyS5";
  247. int InitComPort(byte target)
  248. {
  249. int fd;
  250. struct termios tios;
  251. if (target == UPGRADE_PRI)
  252. fd = open ( _priPortName, O_RDWR );
  253. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC
  254. || target == UPGRADE_LED)
  255. fd = open ( _485PortName, O_RDWR );
  256. if (fd <= 0)
  257. {
  258. #ifdef SystemLogMessage
  259. DEBUG_ERROR_MSG( "open 407 Communication port NG \n" );
  260. #endif
  261. return - 1;
  262. }
  263. ioctl ( fd, TCGETS, & tios );
  264. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  265. tios.c_lflag = 0;
  266. tios.c_iflag = 0;
  267. tios.c_oflag = 0;
  268. tios.c_cc [VMIN] = 0;
  269. tios.c_cc [VTIME] = (unsigned char) 5;
  270. tios.c_lflag = 0;
  271. tcflush ( fd, TCIFLUSH );
  272. ioctl ( fd, TCSETS, & tios );
  273. return fd;
  274. }
  275. //=================================
  276. // Common routine
  277. //=================================
  278. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  279. {
  280. strncpy ( dest, src + start, cnt );
  281. dest [cnt] = 0;
  282. }
  283. int InitWatchDog()
  284. {
  285. int fd;
  286. int timeout = 180;
  287. system ( "/usr/bin/fuser -k /dev/watchdog" );
  288. sleep ( 1 );
  289. system ( "echo V > /dev/watchdog" );
  290. sleep ( 1 );
  291. fd = open ( "/dev/watchdog", O_RDWR );
  292. if (fd <= 0)
  293. {
  294. DEBUG_ERROR_MSG( "System watch dog initial fail.\r\n" );
  295. }
  296. ioctl ( fd, _IOWR( 'W', 6, int ), & timeout );
  297. return fd;
  298. }
  299. void CloseWatchDog()
  300. {
  301. if (wtdFd > 0)
  302. {
  303. close ( wtdFd );
  304. sleep ( 1 );
  305. system ( "/usr/bin/fuser -k /dev/watchdog" );
  306. sleep ( 1 );
  307. system ( "echo V > /dev/watchdog" );
  308. sleep ( 1 );
  309. }
  310. }
  311. int StoreLogMsg_1(const char *fmt, ...)
  312. {
  313. char Buf [4096 + 256];
  314. char buffer [4096];
  315. va_list args;
  316. struct timeb SeqEndTime;
  317. struct tm *tm;
  318. va_start( args, fmt );
  319. int rc = vsnprintf ( buffer, sizeof(buffer), fmt, args );
  320. va_end( args );
  321. memset ( Buf, 0, sizeof(Buf) );
  322. ftime ( & SeqEndTime );
  323. SeqEndTime.time = time ( NULL );
  324. tm = localtime ( & SeqEndTime.time );
  325. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES ||
  326. DEBUG_ALSTON)
  327. {
  328. sprintf ( Buf, "%02d:%02d:%02d:%03d - %s", tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer );
  329. printf ( "%s \n", Buf );
  330. }
  331. else
  332. {
  333. sprintf ( Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s",
  334. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer,
  335. tm->tm_year + 1900, tm->tm_mon + 1, ShmSysConfigAndInfo->SysConfig.SerialNumber );
  336. system ( Buf );
  337. }
  338. return rc;
  339. }
  340. //unsigned long GetTimeoutValue(struct timeval _sour_time)
  341. //{
  342. // struct timeval _end_time;
  343. // gettimeofday(&_end_time, NULL);
  344. //
  345. // return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  346. //}
  347. int GetTimeoutValue(struct timespec *startTime)
  348. {
  349. struct timespec endTime;
  350. clock_gettime ( CLOCK_MONOTONIC_COARSE, & endTime );
  351. return endTime.tv_sec - startTime->tv_sec;
  352. }
  353. void GetTimespecFunc(struct timespec *time)
  354. {
  355. clock_gettime ( CLOCK_MONOTONIC_COARSE, time );
  356. }
  357. int mystrcmp(unsigned char *p1, unsigned char *p2)
  358. {
  359. while ( * p1 == * p2)
  360. {
  361. if ( * p1 == '\0' || * p2 == '\0')
  362. break;
  363. p1 ++;
  364. p2 ++;
  365. }
  366. if ( * p1 == '\0' && * p2 == '\0')
  367. return (PASS);
  368. else
  369. return (FAIL);
  370. }
  371. int DiffTimeb(struct timeb ST, struct timeb ET)
  372. {
  373. //return milli-second
  374. unsigned int StartTime , StopTime;
  375. StartTime = (unsigned int) ST.time;
  376. StopTime = (unsigned int) ET.time;
  377. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  378. return (StopTime - StartTime);
  379. }
  380. int DiffTimebWithNowSec(struct timeb ST)
  381. {
  382. //return milli-second
  383. struct timeb ET;
  384. unsigned int StartTime , StopTime;
  385. ftime ( & ET );
  386. StartTime = (unsigned int) ST.time;
  387. StopTime = (unsigned int) ET.time;
  388. return (StopTime - StartTime);
  389. }
  390. int CheckTimeOut(uint8_t *start)
  391. {
  392. int result = YES;
  393. struct ParsingResult
  394. {
  395. int result;
  396. int scanedElement;
  397. int year;
  398. int month;
  399. int mday;
  400. int hour;
  401. int min;
  402. int sec;
  403. int tz_hour;
  404. int tz_min;
  405. float minSec;
  406. } parsingResult;
  407. struct tm tmStart;
  408. struct timeb tbStart;
  409. memset ( & parsingResult, 0x00, sizeof(struct ParsingResult) );
  410. if (strstr ( (char*) start, "." ) != NULL)
  411. {
  412. // Original data with mini second
  413. if (strstr ( (char*) start, "Z" ) != NULL)
  414. {
  415. // Original data with Z
  416. parsingResult.scanedElement = sscanf ( (char*) start, "%d-%d-%dT%d:%d:%d.%fZ", & parsingResult.year,
  417. & parsingResult.month, & parsingResult.mday, & parsingResult.hour, & parsingResult.min, & parsingResult.sec,
  418. & parsingResult.minSec );
  419. }
  420. else
  421. {
  422. // Original data without Z
  423. parsingResult.scanedElement = sscanf ( (char*) start, "%d-%d-%dT%d:%d:%d.%f%d:%d", & parsingResult.year,
  424. & parsingResult.month, & parsingResult.mday, & parsingResult.hour, & parsingResult.min, & parsingResult.sec,
  425. & parsingResult.minSec, & parsingResult.tz_hour, & parsingResult.tz_min );
  426. }
  427. }
  428. else
  429. {
  430. // Original data without mini second
  431. if (strstr ( (char*) start, "Z" ) != NULL)
  432. {
  433. // Original data with Z
  434. parsingResult.scanedElement = sscanf ( (char*) start, "%d-%d-%dT%d:%d:%dZ", & parsingResult.year,
  435. & parsingResult.month, & parsingResult.mday, & parsingResult.hour, & parsingResult.min, & parsingResult.sec );
  436. }
  437. else
  438. {
  439. // Original data without Z
  440. parsingResult.scanedElement = sscanf ( (char*) start, "%d-%d-%dT%d:%d:%d%d:%d", & parsingResult.year,
  441. & parsingResult.month, & parsingResult.mday, & parsingResult.hour, & parsingResult.min, & parsingResult.sec,
  442. & parsingResult.tz_hour, & parsingResult.tz_min );
  443. }
  444. }
  445. if (parsingResult.scanedElement >= 6)
  446. {
  447. tmStart.tm_year = parsingResult.year - 1900;
  448. tmStart.tm_mon = parsingResult.month - 1;
  449. tmStart.tm_mday = parsingResult.mday;
  450. tmStart.tm_hour = parsingResult.hour;
  451. tmStart.tm_min = parsingResult.min;
  452. tmStart.tm_sec = parsingResult.sec;
  453. tmStart.tm_gmtoff = 0;
  454. tbStart.time = mktime ( & tmStart );
  455. tbStart.timezone = 0;
  456. tbStart.millitm = 0;
  457. tbStart.time -= (parsingResult.tz_hour * 3600) + (parsingResult.tz_min * 60 * (parsingResult.tz_hour >= 0 ? 1 : - 1));
  458. if (DiffTimebWithNowSec ( tbStart ) <= 0)
  459. result = NO;
  460. else
  461. result = YES;
  462. }
  463. else
  464. {
  465. PRINTF_FUNC ( "Start date parsing error.\n" );
  466. }
  467. return result;
  468. }
  469. void setChargerMode(byte gun_index, byte mode)
  470. {
  471. chargingInfo [gun_index]->SystemStatus = mode;
  472. }
  473. void PRINTF_FUNC(char *string, ...)
  474. {
  475. va_list args;
  476. char buffer [4096];
  477. va_start( args, string );
  478. vsnprintf ( buffer, sizeof(buffer), string, args );
  479. va_end( args );
  480. DEBUG_INFO_MSG( "%s ", buffer );
  481. }
  482. long long DiffTimebWithNow(struct timeb ST)
  483. {
  484. //return milli-second
  485. struct timeb ET;
  486. long long StartTime , StopTime;
  487. ftime ( & ET );
  488. StartTime = (long long) ST.time;
  489. StopTime = (long long) ET.time;
  490. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  491. }
  492. bool CheckBackendChargingTimeout(byte gunIndex)
  493. {
  494. bool result = false;
  495. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  496. {
  497. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  498. {
  499. if (chargingInfo [gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  500. result = true;
  501. }
  502. }
  503. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  504. {
  505. // 隨插即充電的要看 offline
  506. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  507. {
  508. if (chargingInfo [gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  509. result = true;
  510. }
  511. }
  512. return result;
  513. }
  514. bool CheckBackendChargingEnergy(byte gunIndex)
  515. {
  516. bool result = false;
  517. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  518. {
  519. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  520. {
  521. if (chargingInfo [gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  522. result = true;
  523. }
  524. }
  525. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  526. {
  527. // 隨插即充電的要看 offline
  528. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  529. {
  530. if (chargingInfo [gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  531. result = true;
  532. }
  533. }
  534. return result;
  535. }
  536. bool IsGunUsing(byte dcGunIndex)
  537. {
  538. bool result = false;
  539. if ((chargingInfo [dcGunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo [dcGunIndex]->SystemStatus <= SYS_MODE_ALARM)
  540. || (chargingInfo [dcGunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  541. && chargingInfo [dcGunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  542. {
  543. result = true;
  544. }
  545. return result;
  546. }
  547. //==========================================
  548. // Log
  549. //==========================================
  550. void CheckFwSlotStatusLog()
  551. {
  552. if (bd0_1_status == 0 && bd0_2_status == 1)
  553. PRINTF_FUNC ( "Connector 1 : Chademo" );
  554. else if (bd0_1_status == 1 && bd0_2_status == 0)
  555. PRINTF_FUNC ( "Connector 1 : CCS" );
  556. else if (bd0_1_status == 1 && bd0_2_status == 1)
  557. PRINTF_FUNC ( "Connector 1 : GB" );
  558. if (bd1_1_status == 0 && bd1_2_status == 1)
  559. PRINTF_FUNC ( "Connector 2 : Chademo" );
  560. else if (bd1_1_status == 1 && bd1_2_status == 0)
  561. PRINTF_FUNC ( "Connector 2 : CCS" );
  562. else if (bd1_1_status == 1 && bd1_2_status == 1)
  563. PRINTF_FUNC ( "Connector 2 : GB" );
  564. }
  565. void CheckHwSlotStatusLog(byte index)
  566. {
  567. if (chargingInfo [index]->Type == _Type_Chademo)
  568. {
  569. PRINTF_FUNC ( "Hw check : Connector %d, Type : Chademo, Evboard_id = %d \n", index, chargingInfo [index]->Evboard_id );
  570. }
  571. else if (chargingInfo [index]->Type == _Type_CCS)
  572. {
  573. PRINTF_FUNC ( "Hw check : Connector %d, Type : CCS, Evboard_id = %d \n", index, chargingInfo [index]->Evboard_id );
  574. }
  575. else if (chargingInfo [index]->Type == _Type_GB)
  576. {
  577. PRINTF_FUNC ( "Hw check : Connector %d, Type : GB, Evboard_id = %d \n", index, chargingInfo [index]->Evboard_id );
  578. }
  579. }
  580. //==========================================
  581. // Check interface status
  582. //==========================================
  583. int isInterfaceUp(const char *interface)
  584. {
  585. int result = FAIL;
  586. FILE *fp;
  587. char cmd [256];
  588. char buf [512];
  589. strcpy ( cmd, "ifconfig" );
  590. fp = popen ( cmd, "r" );
  591. if (fp != NULL)
  592. {
  593. while (fgets ( buf, sizeof(buf), fp ) != NULL)
  594. {
  595. if (strstr ( buf, interface ) > 0)
  596. {
  597. result = PASS;
  598. }
  599. }
  600. }
  601. pclose ( fp );
  602. return result;
  603. }
  604. //=================================
  605. // Create all share memory
  606. //=================================
  607. int CreateShareMemory()
  608. {
  609. int MeterSMId;
  610. if ((MeterSMId = shmget ( ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777 )) < 0)
  611. {
  612. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n" );
  613. return 0;
  614. }
  615. else if ((ShmSysConfigAndInfo = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  616. {
  617. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n" );
  618. return 0;
  619. }
  620. memset ( ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo) );
  621. if ((MeterSMId = shmget ( ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777 )) < 0)
  622. {
  623. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmStatusCodeData NG \n" );
  624. return 0;
  625. }
  626. else if ((ShmStatusCodeData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  627. {
  628. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmStatusCodeData NG \n" );
  629. return 0;
  630. }
  631. memset ( ShmStatusCodeData, 0, sizeof(struct StatusCodeData) );
  632. //creat ShmPsuData
  633. if ((MeterSMId = shmget ( ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777 )) < 0)
  634. {
  635. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmPsuData NG \n" );
  636. return 0;
  637. }
  638. else if ((ShmPsuData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  639. {
  640. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmPsuData NG \n" );
  641. return 0;
  642. }
  643. memset ( ShmPsuData, 0, sizeof(struct PsuData) );
  644. if (CHAdeMO_QUANTITY > 0)
  645. {
  646. if ((MeterSMId = shmget ( ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777 )) < 0)
  647. {
  648. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n" );
  649. return 0;
  650. }
  651. else if ((ShmCHAdeMOData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  652. {
  653. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n" );
  654. return 0;
  655. }
  656. memset ( ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData) );
  657. }
  658. if (GB_QUANTITY > 0)
  659. {
  660. if ((MeterSMId = shmget ( ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777 )) < 0)
  661. {
  662. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmGBTData NG \n" );
  663. return 0;
  664. }
  665. else if ((ShmGBTData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  666. {
  667. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmGBTData NG \n" );
  668. return 0;
  669. }
  670. memset ( ShmGBTData, 0, sizeof(struct GBTData) );
  671. }
  672. //creat ShmCcsData
  673. if (CCS_QUANTITY > 0)
  674. {
  675. if ((MeterSMId = shmget ( ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777 )) < 0)
  676. {
  677. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmCcsData NG \n" );
  678. return 0;
  679. }
  680. else if ((ShmCcsData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  681. {
  682. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmCcsData NG \n" );
  683. return 0;
  684. }
  685. memset ( ShmCcsData, 0, sizeof(struct CcsData) );
  686. }
  687. //creat ShmPrimaryMcuData
  688. if ((MeterSMId = shmget ( ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777 )) < 0)
  689. {
  690. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n" );
  691. return 0;
  692. }
  693. else if ((ShmPrimaryMcuData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  694. {
  695. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n" );
  696. return 0;
  697. }
  698. memset ( ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData) );
  699. //creat ShmFanModuleData
  700. if ((MeterSMId = shmget ( ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777 )) < 0)
  701. {
  702. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmFanModuleData NG \n" );
  703. return 0;
  704. }
  705. else if ((ShmFanModuleData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  706. {
  707. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmFanModuleData NG \n" );
  708. return 0;
  709. }
  710. memset ( ShmFanModuleData, 0, sizeof(struct FanModuleData) );
  711. //creat ShmRelayModuleData
  712. if ((MeterSMId = shmget ( ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777 )) < 0)
  713. {
  714. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmRelayModuleData NG \n" );
  715. return 0;
  716. }
  717. else if ((ShmRelayModuleData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  718. {
  719. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmRelayModuleData NG \n" );
  720. return 0;
  721. }
  722. memset ( ShmRelayModuleData, 0, sizeof(struct RelayModuleData) );
  723. if ((MeterSMId = shmget ( ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777 )) < 0)
  724. {
  725. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmLedModuleData NG \n" );
  726. return 0;
  727. }
  728. else if ((ShmLedModuleData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  729. {
  730. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmLedModuleData NG \n" );
  731. return 0;
  732. }
  733. memset ( ShmLedModuleData, 0, sizeof(struct LedModuleData) );
  734. //creat ShmOCPP16Data
  735. if ((MeterSMId = shmget ( ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777 )) < 0)
  736. {
  737. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmOCPP16Data NG \n" );
  738. return 0;
  739. }
  740. else if ((ShmOCPP16Data = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  741. {
  742. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmOCPP16Data NG \n" );
  743. return 0;
  744. }
  745. memset ( ShmOCPP16Data, 0, sizeof(struct OCPP16Data) );
  746. if ((MeterSMId = shmget ( ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777 )) < 0)
  747. {
  748. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget OCPP20Data NG\n" );
  749. return 0;
  750. }
  751. else if ((ShmOCPP20Data = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  752. {
  753. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat OCPP20Data NG\n" );
  754. return 0;
  755. }
  756. memset ( ShmOCPP20Data, 0, sizeof(struct OCPP20Data) );
  757. if ((MeterSMId = shmget ( ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777 )) < 0)
  758. {
  759. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmCsuMeterKey NG \n" );
  760. return 0;
  761. }
  762. else if ((ShmCsuMeterData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  763. {
  764. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmCsuMeterData NG \n" );
  765. return 0;
  766. }
  767. memset ( ShmCsuMeterData, 0, sizeof(struct MeterInformation) );
  768. if ((MeterSMId = shmget ( ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777 )) < 0)
  769. {
  770. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmCommonKey NG \n" );
  771. return 0;
  772. }
  773. else if ((ShmDcCommonData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  774. {
  775. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmCommonKey NG \n" );
  776. return 0;
  777. }
  778. byte buf = 0;
  779. if (ShmDcCommonData->rebootCount == 1)
  780. buf = ShmDcCommonData->rebootCount;
  781. memset ( ShmDcCommonData, 0, sizeof(struct DcCommonInformation) );
  782. ShmDcCommonData->rebootCount = buf;
  783. return 1;
  784. }
  785. void SetupRebootCount(byte value)
  786. {
  787. int MeterSMId;
  788. if ((MeterSMId = shmget ( ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777 )) < 0)
  789. {
  790. DEBUG_ERROR_MSG( "[main]RecordRebootCount:shmget ShmCommonKey NG \n" );
  791. }
  792. else
  793. {
  794. ShmDcCommonData->rebootCount = value;
  795. shmctl ( MeterSMId, IPC_SET, 0 );
  796. }
  797. }
  798. //=================================
  799. // LCM Page
  800. //=================================
  801. void ChangeLcmByIndex(byte page_index)
  802. {
  803. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL || page_index == _LCM_COMPLETE || page_index == _LCM_FIX || page_index == _LCM_EMC)
  804. {
  805. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0 && (page_index == _LCM_FIX || page_index == _LCM_EMC))
  806. {
  807. bool isUpdate = false;
  808. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  809. {
  810. if (chargingInfo [_index]->SystemStatus != SYS_MODE_RESERVATION && chargingInfo [_index]->SystemStatus != SYS_MODE_MAINTAIN)
  811. {
  812. isUpdate = true;
  813. }
  814. }
  815. if (isUpdate && ShmDcCommonData->LcmFwVersion > 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  816. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_EMC;
  817. else
  818. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_FIX;
  819. }
  820. else
  821. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  822. }
  823. }
  824. //======================================================
  825. // Peripheral initial
  826. //======================================================
  827. void InitGPIO()
  828. {
  829. /*****************0~3, 4 bank, bank x 32+ num*********************/
  830. /***************************************************************/
  831. /*************** GPIO 0 ***************************************/
  832. /***************************************************************/
  833. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  834. system ( "echo 22 > /sys/class/gpio/export" );
  835. system ( "echo \"in\" > /sys/class/gpio/gpio22/direction" );
  836. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  837. system ( "echo 23 > /sys/class/gpio/export" );
  838. system ( "echo \"in\" > /sys/class/gpio/gpio23/direction" );
  839. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  840. system ( "echo 26 > /sys/class/gpio/export" );
  841. system ( "echo \"out\" > /sys/class/gpio/gpio26/direction" );
  842. system ( "echo 1 > /sys/class/gpio/gpio26/value" );
  843. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  844. system ( "echo 27 > /sys/class/gpio/export" );
  845. system ( "echo \"in\" > /sys/class/gpio/gpio27/direction" );
  846. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  847. system ( "echo 29 > /sys/class/gpio/export" );
  848. system ( "echo \"in\" > /sys/class/gpio/gpio29/direction" );
  849. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  850. system ( "echo 19 > /sys/class/gpio/export" );
  851. system ( "echo \"out\" > /sys/class/gpio/gpio19/direction" );
  852. system ( "echo 1 > /sys/class/gpio/gpio19/value" );
  853. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  854. system ( "echo 20 > /sys/class/gpio/export" );
  855. system ( "echo \"in\" > /sys/class/gpio/gpio20/direction" );
  856. /***************************************************************/
  857. /*************** GPIO 1 ***************************************/
  858. /***************************************************************/
  859. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  860. system ( "echo 44 > /sys/class/gpio/export" );
  861. system ( "echo \"in\" > /sys/class/gpio/gpio44/direction" );
  862. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  863. system ( "echo 45 > /sys/class/gpio/export" );
  864. system ( "echo \"in\" > /sys/class/gpio/gpio45/direction" );
  865. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  866. system ( "echo 46 > /sys/class/gpio/export" );
  867. system ( "echo \"out\" > /sys/class/gpio/gpio46/direction" );
  868. system ( "echo 0 > /sys/class/gpio/gpio46/value" );
  869. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  870. system ( "echo 47 > /sys/class/gpio/export" );
  871. system ( "echo \"in\" > /sys/class/gpio/gpio47/direction" );
  872. /***************************************************************/
  873. /*************** GPIO 2 ***************************************/
  874. /***************************************************************/
  875. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  876. system ( "echo 89 > /sys/class/gpio/export" );
  877. system ( "echo \"out\" > /sys/class/gpio/gpio89/direction" );
  878. system ( "echo 1 > /sys/class/gpio/gpio89/value" );
  879. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  880. system ( "echo 87 > /sys/class/gpio/export" );
  881. system ( "echo \"out\" > /sys/class/gpio/gpio87/direction" );
  882. system ( "echo 0 > /sys/class/gpio/gpio87/value" );
  883. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  884. system ( "echo 88 > /sys/class/gpio/export" );
  885. system ( "echo \"in\" > /sys/class/gpio/gpio88/direction" );
  886. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  887. system ( "echo 86 > /sys/class/gpio/export" );
  888. system ( "echo \"in\" > /sys/class/gpio/gpio86/direction" );
  889. /***************************************************************/
  890. /*************** GPIO 3 ***************************************/
  891. /***************************************************************/
  892. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  893. system ( "echo 111 > /sys/class/gpio/export" );
  894. system ( "echo \"in\" > /sys/class/gpio/gpio111/direction" );
  895. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  896. system ( "echo 114 > /sys/class/gpio/export" );
  897. system ( "echo \"in\" > /sys/class/gpio/gpio114/direction" );
  898. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  899. system ( "echo 113 > /sys/class/gpio/export" );
  900. system ( "echo \"in\" > /sys/class/gpio/gpio113/direction" );
  901. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  902. system ( "echo 110 > /sys/class/gpio/export" );
  903. system ( "echo \"out\" > /sys/class/gpio/gpio110/direction" );
  904. system ( "echo 0 > /sys/class/gpio/gpio110/value" );
  905. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  906. system ( "echo 115 > /sys/class/gpio/export" );
  907. system ( "echo \"out\" > /sys/class/gpio/gpio115/direction" );
  908. system ( "echo 0 > /sys/class/gpio/gpio115/value" );
  909. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  910. system ( "echo 112 > /sys/class/gpio/export" );
  911. system ( "echo \"out\" > /sys/class/gpio/gpio112/direction" );
  912. system ( "echo 1 > /sys/class/gpio/gpio112/value" );
  913. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  914. system ( "echo 116 > /sys/class/gpio/export" );
  915. system ( "echo \"out\" > /sys/class/gpio/gpio116/direction" );
  916. system ( "echo 0 > /sys/class/gpio/gpio116/value" );
  917. /* (C14) EMU0.gpio3[7] *//*CP open/short feature enable/disable, pull low for default enable*/
  918. system ( "echo 103 > /sys/class/gpio/export" );
  919. system ( "echo \"out\" > /sys/class/gpio/gpio103/direction" );
  920. system ( "echo 0 > /sys/class/gpio/gpio103/value" );
  921. /* (B14) EMU1.gpio3[8] *//*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  922. system ( "echo 104 > /sys/class/gpio/export" );
  923. system ( "echo \"out\" > /sys/class/gpio/gpio104/direction" );
  924. system ( "echo 0 > /sys/class/gpio/gpio104/value" );
  925. #ifdef SystemLogMessage
  926. DEBUG_INFO_MSG( "[main]InitGPIO: Initial GPIO OK \n" );
  927. #endif
  928. }
  929. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  930. {
  931. int fd , wrd;
  932. unsigned char *buf;
  933. unsigned int ChkSum , ChkSumOrg;
  934. if ((buf = malloc ( MtdBlockSize )) == NULL)
  935. {
  936. DEBUG_ERROR_MSG( "malloc buffer NG,rebooting..\r\n" );
  937. if (ShmStatusCodeData != NULL)
  938. {
  939. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  940. }
  941. sleep ( 5 );
  942. system ( "reboot -f" );
  943. sleep ( 5 );
  944. system ( "reboot -f" );
  945. }
  946. memset ( buf, 0, MtdBlockSize );
  947. //================================================
  948. // Load configuration from mtdblock10
  949. //================================================
  950. system ( "nanddump /dev/mtd10 -f /mnt/EvseConfig.bin" );
  951. //fd = open("/dev/mtdblock10", O_RDWR);
  952. fd = open ( "/mnt/EvseConfig.bin", O_RDWR );
  953. if (fd < 0)
  954. {
  955. free ( buf );
  956. DEBUG_ERROR_MSG( "open mtdblock10 NG,rebooting..\r\n" );
  957. if (ShmStatusCodeData != NULL)
  958. {
  959. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  960. }
  961. sleep ( 5 );
  962. system ( "reboot -f" );
  963. sleep ( 5 );
  964. system ( "reboot -f" );
  965. }
  966. wrd = read ( fd, buf, MtdBlockSize );
  967. close ( fd );
  968. if (wrd < MtdBlockSize)
  969. {
  970. free ( buf );
  971. DEBUG_ERROR_MSG( "read SysConfigData data NG,rebooting..\r\n" );
  972. if (ShmStatusCodeData != NULL)
  973. {
  974. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  975. }
  976. sleep ( 5 );
  977. system ( "reboot -f" );
  978. sleep ( 5 );
  979. system ( "reboot -f" );
  980. }
  981. ChkSum = 0;
  982. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  983. // {
  984. // ChkSum+=buf[wrd];
  985. // }
  986. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  987. for (wrd = ARRAY_SIZE( ptr->CsuBootLoadFwRev ); wrd < MtdBlockSize - 4; wrd ++)
  988. {
  989. ChkSum += buf [wrd];
  990. }
  991. memcpy ( & ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg) );
  992. memcpy ( & ptr->ModelName, buf + (ARRAY_SIZE( ptr->CsuBootLoadFwRev )), ARRAY_SIZE( ptr->ModelName ) );
  993. memcpy ( & ptr->SerialNumber,
  994. buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE( ptr->ModelName ) + ARRAY_SIZE( ptr->AcModelName )),
  995. ARRAY_SIZE( ptr->SerialNumber ) );
  996. //================================================
  997. // Load configuration from mtdblock11
  998. //================================================
  999. if (ChkSum != ChkSumOrg)
  1000. {
  1001. DEBUG_ERROR_MSG( "Primary SysConfigData checksum NG, read backup\r\n" );
  1002. system ( "nanddump /dev/mtd11 -f /mnt/EvseConfig.bin" );
  1003. //fd = open("/dev/mtdblock11", O_RDWR);
  1004. fd = open ( "/mnt/EvseConfig.bin", O_RDWR );
  1005. if (fd < 0)
  1006. {
  1007. free ( buf );
  1008. DEBUG_ERROR_MSG( "open mtdblock11 (backup) NG,rebooting..\r\n" );
  1009. if (ShmStatusCodeData != NULL)
  1010. {
  1011. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1012. }
  1013. sleep ( 5 );
  1014. system ( "reboot -f" );
  1015. sleep ( 5 );
  1016. system ( "reboot -f" );
  1017. }
  1018. memset ( buf, 0, MtdBlockSize );
  1019. wrd = read ( fd, buf, MtdBlockSize );
  1020. close ( fd );
  1021. if (wrd < MtdBlockSize)
  1022. {
  1023. free ( buf );
  1024. DEBUG_ERROR_MSG( "read backup SysConfigData data NG,rebooting..\r\n" );
  1025. if (ShmStatusCodeData != NULL)
  1026. {
  1027. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1028. }
  1029. sleep ( 5 );
  1030. system ( "reboot -f" );
  1031. sleep ( 5 );
  1032. system ( "reboot -f" );
  1033. }
  1034. ChkSum = 0;
  1035. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1036. // {
  1037. // ChkSum+=buf[wrd];
  1038. // }
  1039. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1040. for (wrd = ARRAY_SIZE( ptr->CsuBootLoadFwRev ); wrd < MtdBlockSize - 4; wrd ++)
  1041. {
  1042. ChkSum += buf [wrd];
  1043. }
  1044. memcpy ( & ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg) );
  1045. //================================================
  1046. // Load configuration from mtdblock12 (Factory default)
  1047. //================================================
  1048. if (ChkSum != ChkSumOrg)
  1049. {
  1050. DEBUG_ERROR_MSG( "backup SysConfigData checksum NG, read Factory default\r\n" );
  1051. system ( "nanddump /dev/mtd12 -f /mnt/EvseConfig.bin" );
  1052. //fd = open("/dev/mtdblock12", O_RDWR);
  1053. fd = open ( "/mnt/EvseConfig.bin", O_RDWR );
  1054. if (fd < 0)
  1055. {
  1056. free ( buf );
  1057. DEBUG_ERROR_MSG( "open mtdblock12 (Factory default) NG,rebooting..\r\n" );
  1058. if (ShmStatusCodeData != NULL)
  1059. {
  1060. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1061. }
  1062. sleep ( 5 );
  1063. system ( "reboot -f" );
  1064. sleep ( 5 );
  1065. system ( "reboot -f" );
  1066. }
  1067. memset ( buf, 0, MtdBlockSize );
  1068. wrd = read ( fd, buf, MtdBlockSize );
  1069. close ( fd );
  1070. if (wrd < MtdBlockSize)
  1071. {
  1072. free ( buf );
  1073. DEBUG_ERROR_MSG( "read factory default SysConfigData data NG,rebooting..\r\n" );
  1074. if (ShmStatusCodeData != NULL)
  1075. {
  1076. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1077. }
  1078. sleep ( 5 );
  1079. system ( "reboot -f" );
  1080. sleep ( 5 );
  1081. system ( "reboot -f" );
  1082. }
  1083. ChkSum = 0;
  1084. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1085. // {
  1086. // ChkSum+=buf[wrd];
  1087. // }
  1088. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1089. for (wrd = ARRAY_SIZE( ptr->CsuBootLoadFwRev ); wrd < MtdBlockSize - 4; wrd ++)
  1090. {
  1091. ChkSum += buf [wrd];
  1092. }
  1093. memcpy ( & ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg) );
  1094. memcpy ( buf + (ARRAY_SIZE( ptr->CsuBootLoadFwRev )), & ptr->ModelName, ARRAY_SIZE( ptr->ModelName ) );
  1095. memcpy ( buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE( ptr->ModelName ) + ARRAY_SIZE( ptr->AcModelName )),
  1096. & ptr->SerialNumber, ARRAY_SIZE( ptr->SerialNumber ) );
  1097. if (ChkSum != ChkSumOrg)
  1098. {
  1099. DEBUG_ERROR_MSG( "factory default SysConfigData checksum NG, restore factory default\r\n" );
  1100. free ( buf );
  1101. //system("cd /root;./FactoryConfig -m");
  1102. sleep ( 5 );
  1103. char facBuf [256];
  1104. sprintf ( facBuf, "cd /root;./FactoryConfig -m %s %s", ShmSysConfigAndInfo->SysConfig.ModelName,
  1105. ShmSysConfigAndInfo->SysConfig.SerialNumber );
  1106. system ( facBuf );
  1107. system ( "rm -f /Storage/OCPP/OCPPConfiguration" );
  1108. system ( "sync" );
  1109. sleep ( 5 );
  1110. system ( "reboot -f" );
  1111. sleep ( 5 );
  1112. system ( "reboot -f" );
  1113. return FAIL;
  1114. }
  1115. }
  1116. }
  1117. //load OK
  1118. memcpy ( (struct SysConfigData *) ptr, buf, sizeof(struct SysConfigData) );
  1119. free ( buf );
  1120. system ( "rm -f /mnt/EvseConfig.bin" );
  1121. // SysConfig in flash is empty (0xffffffff)
  1122. if ((strlen ( (char*) ShmSysConfigAndInfo->SysConfig.ModelName ) > ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.ModelName ))
  1123. || (strlen ( (char*) ShmSysConfigAndInfo->SysConfig.SerialNumber ) > ARRAY_SIZE(
  1124. ShmSysConfigAndInfo->SysConfig.SerialNumber ))
  1125. || (strlen ( (char*) ShmSysConfigAndInfo->SysConfig.SystemId ) > ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.SystemId ))
  1126. || (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1127. {
  1128. if (strlen ( (char*) ShmSysConfigAndInfo->SysConfig.ModelName ) > ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.ModelName ))
  1129. {
  1130. PRINTF_FUNC ( "Model name over length.\n" );
  1131. memset ( ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.ModelName ) );
  1132. }
  1133. if (strlen ( (char*) ShmSysConfigAndInfo->SysConfig.SerialNumber ) > ARRAY_SIZE(
  1134. ShmSysConfigAndInfo->SysConfig.SerialNumber ))
  1135. {
  1136. PRINTF_FUNC ( "Model serial number over length.\n" );
  1137. memset ( ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00,
  1138. ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.SerialNumber ) );
  1139. }
  1140. if (strlen ( (char*) ShmSysConfigAndInfo->SysConfig.SystemId ) > ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.SystemId ))
  1141. {
  1142. PRINTF_FUNC ( "System id over length.\n" );
  1143. memset ( ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.SystemId ) );
  1144. }
  1145. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1146. {
  1147. PRINTF_FUNC ( "Ethernet dhcp config is null.\n" );
  1148. }
  1149. system ( "cd /root;./Module_FactoryConfig -m" );
  1150. sleep ( 3 );
  1151. system ( "/usr/bin/run_evse_restart.sh" );
  1152. }
  1153. PRINTF_FUNC ( "*********************************************************** \n" );
  1154. DEBUG_INFO_MSG( "Load SysConfigData OK\r\n" );
  1155. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1156. {
  1157. memcpy ( (char*) ShmOCPP16Data->ChargeBoxId, (char*) ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1158. ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.ChargeBoxId ) );
  1159. }
  1160. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1161. {
  1162. memcpy ( (char*) ShmOCPP20Data->ChargeBoxId, (char*) ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1163. ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.ChargeBoxId ) );
  1164. }
  1165. return PASS;
  1166. }
  1167. int isRouteFail()
  1168. {
  1169. int result = YES;
  1170. FILE *fp;
  1171. char buf [512];
  1172. fp = popen ( "route -n", "r" );
  1173. if (fp != NULL)
  1174. {
  1175. while (fgets ( buf, sizeof(buf), fp ) != NULL)
  1176. {
  1177. if (strstr ( buf, "eth0" ) != NULL)
  1178. result = NO;
  1179. }
  1180. }
  1181. pclose ( fp );
  1182. return result;
  1183. }
  1184. int isReachableInternet()
  1185. {
  1186. int result = FAIL;
  1187. FILE *fp;
  1188. char cmd [256];
  1189. char buf [512];
  1190. char tmp [512];
  1191. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1192. // {
  1193. // result = PASS;
  1194. // }
  1195. // else
  1196. {
  1197. strcpy ( cmd, "ifconfig eth0" );
  1198. fp = popen ( cmd, "r" );
  1199. if (fp != NULL)
  1200. {
  1201. while (fgets ( buf, sizeof(buf), fp ) != NULL)
  1202. {
  1203. if (strstr ( buf, "inet addr:" ) > 0)
  1204. {
  1205. sscanf ( buf, "%*s%s", tmp );
  1206. substr ( tmp, tmp, strspn ( tmp, "addr:" ), strlen ( buf ) - strspn ( tmp, "addr:" ) );
  1207. if (strcmp ( tmp, (char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress ) != EQUAL)
  1208. {
  1209. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp );
  1210. }
  1211. sscanf ( buf, "%*s%*s%*s%s", tmp );
  1212. substr ( tmp, tmp, strspn ( tmp, "Mask:" ), strlen ( buf ) - strspn ( tmp, "Mask:" ) );
  1213. if (strcmp ( tmp, (char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress ) != 0)
  1214. {
  1215. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp );
  1216. }
  1217. }
  1218. }
  1219. }
  1220. pclose ( fp );
  1221. memset ( buf, 0x00, sizeof(buf) );
  1222. for (int idx = 0; idx < ARRAY_SIZE( valid_Internet ); idx ++)
  1223. {
  1224. sprintf ( cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet [idx] );
  1225. fp = popen ( cmd, "r" );
  1226. if (fp != NULL)
  1227. {
  1228. while (fgets ( buf, sizeof(buf), fp ) != NULL)
  1229. {
  1230. if (strstr ( buf, "transmitted" ) > 0)
  1231. {
  1232. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1233. if (strstr ( buf, "100%" ) != NULL)
  1234. {
  1235. }
  1236. else
  1237. {
  1238. result = PASS;
  1239. }
  1240. //DEBUG_INFO("%s",buf);
  1241. //DEBUG_INFO("%s\n",tmp);
  1242. }
  1243. }
  1244. }
  1245. pclose ( fp );
  1246. }
  1247. }
  1248. return result;
  1249. }
  1250. void InitEthernet()
  1251. {
  1252. system ( "ifconfig eth0 down" ); // eth0 down
  1253. system ( "ifconfig eth1 down" ); // eth1 down
  1254. sleep ( 2 );
  1255. char tmpbuf [256];
  1256. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1257. system ( "echo 1 > /sys/class/gpio/gpio110/value" ); //reset PHY
  1258. sleep ( 2 );
  1259. //Init Eth0 for internet
  1260. memset ( tmpbuf, 0, 256 );
  1261. sprintf ( tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1262. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress );
  1263. system ( tmpbuf );
  1264. memset ( tmpbuf, 0, 256 );
  1265. sprintf ( tmpbuf, "route add default gw %s eth0 ", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress );
  1266. system ( tmpbuf );
  1267. //system("ifconfig lo up");
  1268. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1269. //Init Eth1 for administrator tool
  1270. memset ( tmpbuf, 0, 256 );
  1271. sprintf ( tmpbuf, "/sbin/ifconfig eth1 %s netmask %s up", ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1272. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress );
  1273. system ( tmpbuf );
  1274. //Run DHCP client if enabled
  1275. system ( "killall udhcpc" );
  1276. system ( "rm -rf /etc/resolv.conf" );
  1277. system ( "echo nameserver 8.8.8.8 > /etc/resolv.conf" ); //Google DNS server
  1278. system ( "echo nameserver 180.76.76.76 > /etc/resolv.conf" ); //Baidu DNS server
  1279. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1280. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1281. {
  1282. sprintf ( tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  1283. ShmSysConfigAndInfo->SysConfig.SystemId );
  1284. system ( tmpbuf );
  1285. }
  1286. //Upgrade system id to /etc/hostname
  1287. sprintf ( tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId );
  1288. system ( tmpbuf );
  1289. pid_t pid = fork ();
  1290. uint8_t cnt_pingDNS_Fail;
  1291. if (pid == 0)
  1292. {
  1293. for (;;)
  1294. {
  1295. if (isRouteFail ())
  1296. {
  1297. //PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1298. system ( "/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up" );
  1299. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1300. {
  1301. InitialDHCP ();
  1302. }
  1303. else
  1304. {
  1305. system ( "pgrep -f \"udhcpc -i eth0\" | xargs kill" );
  1306. memset ( tmpbuf, 0, 256 );
  1307. sprintf ( tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up &",
  1308. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1309. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress );
  1310. system ( tmpbuf );
  1311. memset ( tmpbuf, 0, 256 );
  1312. sprintf ( tmpbuf, "route add default gw %s eth0 &",
  1313. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress );
  1314. system ( tmpbuf );
  1315. }
  1316. }
  1317. if (isReachableInternet () == PASS)
  1318. {
  1319. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1320. cnt_pingDNS_Fail = 0;
  1321. }
  1322. else
  1323. {
  1324. if ( ++ cnt_pingDNS_Fail > 3)
  1325. {
  1326. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1327. if ((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0))
  1328. {
  1329. system ( "pgrep -f \"udhcpc -i eth0\" | xargs kill" );
  1330. sprintf ( tmpbuf,
  1331. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  1332. ShmSysConfigAndInfo->SysConfig.SystemId );
  1333. system ( tmpbuf );
  1334. }
  1335. else
  1336. {
  1337. system ( "pgrep -f \"udhcpc -i eth0\" | xargs kill" );
  1338. memset ( tmpbuf, 0, 256 );
  1339. sprintf ( tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up &",
  1340. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1341. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress );
  1342. system ( tmpbuf );
  1343. memset ( tmpbuf, 0, 256 );
  1344. sprintf ( tmpbuf, "route add default gw %s eth0 &",
  1345. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress );
  1346. system ( tmpbuf );
  1347. }
  1348. cnt_pingDNS_Fail = 0;
  1349. }
  1350. }
  1351. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1352. if (ethResult == YES)
  1353. {
  1354. system ( "/sbin/ifmetric eth0 0" );
  1355. if ((ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1356. system ( "/sbin/ifmetric mlan0 1" );
  1357. if ((ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1358. system ( "/sbin/ifmetric ppp0 2" );
  1359. }
  1360. if ( ! ethResult && ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE
  1361. && (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1362. {
  1363. //ethResult = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn;
  1364. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1365. if (ethResult)
  1366. {
  1367. if ((ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1368. {
  1369. system ( "/sbin/ifmetric eth0 1" );
  1370. system ( "/sbin/ifmetric mlan0 0" );
  1371. }
  1372. if ((ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1373. system ( "/sbin/ifmetric ppp0 2" );
  1374. }
  1375. }
  1376. if ( ! ethResult && ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES
  1377. && (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1378. {
  1379. //ethResult = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn;
  1380. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  1381. if (ethResult)
  1382. {
  1383. if ((ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1384. system ( "/sbin/ifmetric mlan0 2" );
  1385. if ((ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  1386. {
  1387. system ( "/sbin/ifmetric eth0 1" );
  1388. system ( "/sbin/ifmetric ppp0 0" );
  1389. }
  1390. }
  1391. }
  1392. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1393. sleep ( 5 );
  1394. }
  1395. }
  1396. #ifdef SystemLogMessage
  1397. DEBUG_INFO_MSG( "[main]InitEthernet: Initial Ethernet OK. \n" );
  1398. #endif
  1399. }
  1400. int InitialRfidPort()
  1401. {
  1402. int uartO2 = open ( rfidPortName, O_RDWR );
  1403. struct termios tios;
  1404. if (uartO2 != FAIL)
  1405. {
  1406. ioctl ( uartO2, TCGETS, & tios );
  1407. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1408. tios.c_lflag = 0;
  1409. tios.c_iflag = 0;
  1410. tios.c_oflag = 0;
  1411. tios.c_cc [VMIN] = 0;
  1412. tios.c_cc [VTIME] = (unsigned char) 1;
  1413. tios.c_lflag = 0;
  1414. tcflush ( uartO2, TCIFLUSH );
  1415. ioctl ( uartO2, TCSETS, & tios );
  1416. }
  1417. if (uartO2 < 0)
  1418. {
  1419. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1420. }
  1421. return uartO2;
  1422. }
  1423. void GetMacAddress()
  1424. {
  1425. for (byte index = 0; index < 2; index ++)
  1426. {
  1427. int fd;
  1428. struct ifreq ifr;
  1429. char tarEth [5];
  1430. char Mac [18];
  1431. sprintf ( tarEth, "eth%d", index );
  1432. fd = socket ( AF_INET, SOCK_DGRAM, 0 );
  1433. ifr.ifr_addr.sa_family = AF_INET;
  1434. strncpy ( ifr.ifr_name, tarEth, IFNAMSIZ - 1 );
  1435. ioctl ( fd, SIOCGIFHWADDR, & ifr );
  1436. close ( fd );
  1437. sprintf ( Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", ifr.ifr_hwaddr.sa_data [0], ifr.ifr_hwaddr.sa_data [1],
  1438. ifr.ifr_hwaddr.sa_data [2], ifr.ifr_hwaddr.sa_data [3], ifr.ifr_hwaddr.sa_data [4], ifr.ifr_hwaddr.sa_data [5] );
  1439. if (index == 0)
  1440. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac );
  1441. else
  1442. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac );
  1443. }
  1444. }
  1445. void GetFirmwareVersion()
  1446. {
  1447. // Get CSU root file system version
  1448. sprintf ( (char*) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion );
  1449. byte count = 0 , chademo = 0 , ccs = 0 , gb = 0;
  1450. for (uint8_t idx = 0; idx < 3; idx ++)
  1451. {
  1452. if (ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'J' || ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'K')
  1453. {
  1454. chademo ++;
  1455. count ++;
  1456. }
  1457. else if (ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'G')
  1458. {
  1459. gb ++;
  1460. count ++;
  1461. }
  1462. else if (ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'U' || ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'E'
  1463. || ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'D'
  1464. || ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'M' || ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'N')
  1465. {
  1466. ccs ++;
  1467. count ++;
  1468. }
  1469. }
  1470. if (count == 1)
  1471. {
  1472. if (chademo > 0)
  1473. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '1';
  1474. else if (ccs > 0)
  1475. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '2';
  1476. else if (gb > 0)
  1477. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '3';
  1478. }
  1479. else
  1480. {
  1481. if (chademo == 1 && ccs == 1)
  1482. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '4';
  1483. else if (chademo == 1 && gb == 1)
  1484. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '5';
  1485. else if (ccs == 1 && gb == 1)
  1486. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '6';
  1487. else if (chademo == 2)
  1488. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '7';
  1489. else if (ccs == 2)
  1490. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '8';
  1491. else if (gb == 2)
  1492. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [7] = '9';
  1493. }
  1494. // Get network option from model name
  1495. switch (ShmSysConfigAndInfo->SysConfig.ModelName [10])
  1496. {
  1497. case 'B' :
  1498. case 'U' :
  1499. //Blue tooth
  1500. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [9] = '3';
  1501. break;
  1502. case 'W' :
  1503. // WIFI
  1504. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [9] = '1';
  1505. break;
  1506. case 'T' :
  1507. // 3G/4G
  1508. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [9] = '2';
  1509. break;
  1510. case 'D' :
  1511. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [9] = '5';
  1512. break;
  1513. default :
  1514. // LAN
  1515. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [9] = '0';
  1516. break;
  1517. }
  1518. // Get rating power from model name
  1519. memcpy ( & ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [10], & ShmSysConfigAndInfo->SysConfig.ModelName [4], 0x03 );
  1520. // Get IEC or UL
  1521. char _buf [3] = { 0 };
  1522. memcpy ( _buf, & ShmSysConfigAndInfo->SysConfig.ModelName [2], 1 );
  1523. if (strcmp ( _buf, "Y" ) == EQUAL || strcmp ( _buf, "Y" ) == EQUAL)
  1524. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1525. else if (strcmp ( _buf, "W" ) == EQUAL)
  1526. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1527. memcpy ( _buf, & ShmSysConfigAndInfo->SysConfig.ModelName [12], 2 );
  1528. memcpy ( & ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev [14], & ShmSysConfigAndInfo->SysConfig.ModelName [12], 2 );
  1529. if (strcmp ( _buf, "PN" ) == EQUAL || strcmp ( _buf, "N7" ) == EQUAL || strcmp ( _buf, "NX" ) == EQUAL)
  1530. ShmDcCommonData->ShowLogoFlag = NO;
  1531. else
  1532. ShmDcCommonData->ShowLogoFlag = YES;
  1533. if (ShmSysConfigAndInfo->SysConfig.ModelName [3] == 'G')
  1534. ShmDcCommonData->chargerType = CHARGER_TYPE_SIMPLE;
  1535. else
  1536. ShmDcCommonData->chargerType = CHARGER_TYPE_STANDARD;
  1537. }
  1538. void InitialGunIndexToUnUse()
  1539. {
  1540. for (byte index = 0; index < CHAdeMO_QUANTITY; index ++)
  1541. {
  1542. ShmSysConfigAndInfo->SysInfo.ChademoChargingData [index].Index = NO_DEFINE;
  1543. }
  1544. for (byte index = 0; index < CCS_QUANTITY; index ++)
  1545. {
  1546. ShmSysConfigAndInfo->SysInfo.CcsChargingData [index].Index = NO_DEFINE;
  1547. }
  1548. for (byte index = 0; index < GB_QUANTITY; index ++)
  1549. {
  1550. ShmSysConfigAndInfo->SysInfo.GbChargingData [index].Index = NO_DEFINE;
  1551. }
  1552. for (byte index = 0; index < AC_QUANTITY; index ++)
  1553. {
  1554. ShmSysConfigAndInfo->SysInfo.AcChargingData [index].Index = NO_DEFINE;
  1555. }
  1556. }
  1557. void InitialShareMemoryInfo()
  1558. {
  1559. FILE *fp;
  1560. char cmd [512];
  1561. char buf [512];
  1562. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1563. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1564. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1565. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1566. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1567. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1568. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1569. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1570. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1571. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1572. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1573. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1574. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1575. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1576. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1577. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0" );
  1578. memcpy ( ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev,
  1579. ARRAY_SIZE( ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev ) );
  1580. sprintf ( cmd, "/bin/uname -r" );
  1581. fp = popen ( cmd, "r" );
  1582. if (fp == NULL)
  1583. sprintf ( (char*) ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version" );
  1584. else
  1585. {
  1586. while (fgets ( buf, sizeof(buf), fp ) != NULL)
  1587. {
  1588. strcpy ( (char*) ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf );
  1589. }
  1590. }
  1591. GetFirmwareVersion ();
  1592. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1593. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " " );
  1594. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " " );
  1595. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " " );
  1596. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " " );
  1597. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " " );
  1598. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " " );
  1599. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " " );
  1600. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " " );
  1601. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " " );
  1602. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " " );
  1603. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " " );
  1604. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " " );
  1605. sprintf ( (char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " " );
  1606. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1607. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1608. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1609. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1610. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  1611. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = NO;
  1612. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1613. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  1614. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1615. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1616. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1617. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = NO;
  1618. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1619. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1620. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1621. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1622. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = NO;
  1623. memset ( ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE( ShmSysConfigAndInfo->SysInfo.FanModuleFwRev ) );
  1624. memset ( ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE( ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev ) );
  1625. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1626. ShmRelayModuleData->SelfTest_Comp = NO;
  1627. ShmFanModuleData->SelfTest_Comp = NO;
  1628. ShmLedModuleData->SelfTest_Comp = NO;
  1629. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1630. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1631. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1632. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1633. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOTTING;
  1634. ShmFanModuleData->TestFanSpeed = 0;
  1635. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1636. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1637. InitialGunIndexToUnUse ();
  1638. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1639. ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V015S0;
  1640. ShmDcCommonData->psuKeepCommunication = NO;
  1641. ShmDcCommonData->acContactSwitch = NO;
  1642. ShmDcCommonData->ConnectErrList [0].GunErrMessage = 0;
  1643. ShmDcCommonData->ConnectErrList [1].GunErrMessage = 0;
  1644. ShmDcCommonData->LcmFwVersion = 0;
  1645. ShmDcCommonData->minOutputCur = 0;
  1646. ShmDcCommonData->ocppMaxChargingProfilePow = - 1;
  1647. ShmDcCommonData->evBoardResetFlag = NO;
  1648. //LWN_Debug
  1649. #ifdef LW_DEBUG_INFO
  1650. ShmDcCommonData->DbgInfo.infoFlag = 0;
  1651. #endif
  1652. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1653. //LWN_Zanobe
  1654. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  1655. }
  1656. int Initialization()
  1657. {
  1658. // 初始化卡號驗證的 Flag
  1659. ClearAuthorizedFlag ();
  1660. // 初始化插槍驗證的 Flag
  1661. ClearDetectPluginFlag ();
  1662. // UART 2 for Rfid
  1663. rfidFd = InitialRfidPort ();
  1664. int pinOut [2] = { 116, 115 }; //D13(Cha)、C13(CCS)
  1665. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count ++)
  1666. {
  1667. chargingInfo [count]->RemoteStartFlag = NO;
  1668. chargingInfo [count]->ReservedStartFlag = NO;
  1669. if (chargingInfo [count]->Type == _Type_Chademo)
  1670. {
  1671. gpio_set_value ( pinOut [count], 0x00 );
  1672. ShmCHAdeMOData->evse [chargingInfo [count]->type_index].SelfTest_Comp = NO;
  1673. }
  1674. else if (chargingInfo [count]->Type == _Type_GB)
  1675. {
  1676. gpio_set_value ( pinOut [count], 0x00 );
  1677. ShmGBTData->evse [chargingInfo [count]->type_index].SelfTest_Comp = NO;
  1678. }
  1679. else if (chargingInfo [count]->Type == _Type_CCS)
  1680. {
  1681. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1682. gpio_set_value ( pinOut [1], 0x01 );
  1683. else
  1684. gpio_set_value ( pinOut [count], 0x01 );
  1685. ShmCcsData->V2GMessage_DIN70121 [chargingInfo [count]->type_index].SelfTest_Comp = NO;
  1686. }
  1687. strcpy ( (char *) ShmDcCommonData->_reserved_UserId [count], "" );
  1688. }
  1689. byte acDirIndex = 0;
  1690. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count ++)
  1691. {
  1692. if (count == 1)
  1693. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1694. ocpp_set_noErrorCode_cmd ( count + acDirIndex, "NoError" );
  1695. }
  1696. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  1697. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count ++)
  1698. {
  1699. ac_chargingInfo [count]->RemoteStartFlag = NO;
  1700. if (ac_chargingInfo [count]->Type == _Type_AC)
  1701. {
  1702. ac_chargingInfo [count]->SelfTest_Comp = NO;
  1703. ocpp_set_noErrorCode_cmd ( count + hasDc, "NoError" );
  1704. }
  1705. }
  1706. PRINTF_FUNC ( "Information check done \n" );
  1707. return PASS;
  1708. }
  1709. bool InitialSystemDefaultConfig()
  1710. {
  1711. bool result = true;
  1712. LoadSysConfigAndInfo ( & ShmSysConfigAndInfo->SysConfig );
  1713. PRINTF_FUNC ( "ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName );
  1714. InitGPIO ();
  1715. InitEthernet ();
  1716. GetMacAddress ();
  1717. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1718. // sleep(3);
  1719. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1720. // sleep(1);
  1721. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1722. return result;
  1723. }
  1724. bool DisplaySelfTestFailReason()
  1725. {
  1726. bool result = false;
  1727. // RB、FB、407、EV 小板中有些板子無回應
  1728. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1729. {
  1730. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES;
  1731. result = true;
  1732. }
  1733. if (ShmFanModuleData->SelfTest_Comp == NO)
  1734. {
  1735. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES;
  1736. result = true;
  1737. }
  1738. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1739. {
  1740. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES;
  1741. result = true;
  1742. }
  1743. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1744. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1745. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES)
  1746. {
  1747. result = true;
  1748. }
  1749. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index ++)
  1750. {
  1751. if (chargingInfo [index]->Type == _Type_Chademo)
  1752. {
  1753. if (ShmCHAdeMOData->evse [chargingInfo [index]->type_index].SelfTest_Comp == NO)
  1754. {
  1755. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES;
  1756. result = true;
  1757. }
  1758. }
  1759. else if (chargingInfo [index]->Type == _Type_GB)
  1760. {
  1761. if (ShmGBTData->evse [chargingInfo [index]->type_index].SelfTest_Comp == NO)
  1762. {
  1763. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES;
  1764. result = true;
  1765. }
  1766. }
  1767. else if (chargingInfo [index]->Type == _Type_CCS)
  1768. {
  1769. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1770. {
  1771. if (ShmCcsData->V2GMessage_DIN70121 [chargingInfo [index]->type_index].SelfTest_Comp == NO)
  1772. {
  1773. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES;
  1774. result = true;
  1775. }
  1776. }
  1777. }
  1778. }
  1779. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index ++)
  1780. {
  1781. // 先借 GBT 顯示
  1782. if (ac_chargingInfo [index]->SelfTest_Comp == NO)
  1783. {
  1784. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES;
  1785. result = true;
  1786. }
  1787. }
  1788. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1789. {
  1790. // AC Contact 未搭上
  1791. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1792. result = true;
  1793. }
  1794. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1795. {
  1796. result = true;
  1797. }
  1798. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1799. {
  1800. // PSU 通訊問題
  1801. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1802. result = true;
  1803. }
  1804. return result;
  1805. }
  1806. void SelfTestRun()
  1807. {
  1808. bool evInitFlag = false;
  1809. StartSystemTimeoutDet ( Timeout_SelftestChk );
  1810. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1811. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE || GetTimeoutValue (
  1812. & ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer ) <= 20)
  1813. {
  1814. ReviewCriticalAlarm ();
  1815. ChkPrimaryStatus ();
  1816. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL
  1817. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1818. {
  1819. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1820. return;
  1821. }
  1822. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1823. {
  1824. if (ShmPsuData->Work_Step == _NO_WORKING || ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1825. {
  1826. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1827. return;
  1828. }
  1829. switch (ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1830. {
  1831. case _STEST_VERSION :
  1832. {
  1833. if (strlen ( (char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev ) != 0
  1834. || ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev [0] != '\0')
  1835. {
  1836. //PRINTF_FUNC("RB pass \n");
  1837. ShmRelayModuleData->SelfTest_Comp = YES;
  1838. }
  1839. if (strlen ( (char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev ) != 0
  1840. || ShmSysConfigAndInfo->SysInfo.FanModuleFwRev [0] != '\0')
  1841. {
  1842. //PRINTF_FUNC("Fan pass \n");
  1843. ShmFanModuleData->SelfTest_Comp = YES;
  1844. }
  1845. if (strlen ( (char *) ShmPrimaryMcuData->version ) != 0 || ShmPrimaryMcuData->version [0] != '\0')
  1846. {
  1847. //PRINTF_FUNC("407 pass \n");
  1848. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1849. }
  1850. // EV 小板
  1851. if ( ! evInitFlag)
  1852. {
  1853. evInitFlag = YES;
  1854. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index ++)
  1855. {
  1856. if (chargingInfo [index]->Type == _Type_Chademo)
  1857. {
  1858. if (strlen ( (char *) ShmCHAdeMOData->evse [chargingInfo [index]->type_index].version ) != 0
  1859. || ShmCHAdeMOData->evse [chargingInfo [index]->type_index].version [0] != '\0')
  1860. {
  1861. //PRINTF_FUNC("chademo pass \n");
  1862. ShmCHAdeMOData->evse [chargingInfo [index]->type_index].SelfTest_Comp = YES;
  1863. }
  1864. else
  1865. {
  1866. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1867. evInitFlag = NO;
  1868. }
  1869. }
  1870. else if (chargingInfo [index]->Type == _Type_GB)
  1871. {
  1872. if (strlen ( (char *) ShmGBTData->evse [chargingInfo [index]->type_index].version ) != 0
  1873. || ShmGBTData->evse [chargingInfo [index]->type_index].version [0] != '\0')
  1874. {
  1875. //PRINTF_FUNC("GBT pass \n");
  1876. ShmGBTData->evse [chargingInfo [index]->type_index].SelfTest_Comp = YES;
  1877. }
  1878. else
  1879. {
  1880. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1881. evInitFlag = NO;
  1882. }
  1883. }
  1884. else if (chargingInfo [index]->Type == _Type_CCS)
  1885. {
  1886. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1887. {
  1888. if (strlen (
  1889. (char *) ShmCcsData->V2GMessage_DIN70121 [chargingInfo [index]->type_index].version ) != 0
  1890. || ShmCcsData->V2GMessage_DIN70121 [chargingInfo [index]->type_index].version [0] != '\0')
  1891. {
  1892. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1893. ShmCcsData->V2GMessage_DIN70121 [chargingInfo [index]->type_index].SelfTest_Comp = YES;
  1894. }
  1895. else
  1896. {
  1897. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1898. evInitFlag = NO;
  1899. }
  1900. }
  1901. }
  1902. }
  1903. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index ++)
  1904. {
  1905. if (ac_chargingInfo [index]->Type == _Type_AC)
  1906. {
  1907. if (strlen ( (char *) ac_chargingInfo [index]->version ) != 0 || ac_chargingInfo [index]->version [0] != '\0')
  1908. {
  1909. ac_chargingInfo [index]->SelfTest_Comp = YES;
  1910. }
  1911. else
  1912. {
  1913. evInitFlag = NO;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. if (ShmFanModuleData->SelfTest_Comp && ShmRelayModuleData->SelfTest_Comp
  1919. && ShmPrimaryMcuData->SelfTest_Comp && evInitFlag)
  1920. {
  1921. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1922. }
  1923. }
  1924. break;
  1925. case _STEST_AC_CONTACTOR :
  1926. {
  1927. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1928. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1929. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1930. {
  1931. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1932. PRINTF_FUNC ( "Communication board pass. \n" );
  1933. }
  1934. }
  1935. break;
  1936. case _STEST_PSU_DETECT :
  1937. {
  1938. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1939. {
  1940. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1941. }
  1942. }
  1943. break;
  1944. case _STEST_PSU_CAP :
  1945. {
  1946. // 此測試是要確認當前總輸出能力
  1947. // 如果沒有 PSU 模組請 bypass
  1948. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1949. {
  1950. sleep ( 1 );
  1951. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1952. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1953. }
  1954. }
  1955. break;
  1956. }
  1957. }
  1958. else
  1959. break;
  1960. usleep ( 100000 );
  1961. }
  1962. }
  1963. int SpawnTask()
  1964. {
  1965. sleep ( 2 );
  1966. system ( "/root/Module_EventLogging &" );
  1967. system ( "/root/Module_PrimaryComm &" );
  1968. system ( "/root/Module_EvComm &" );
  1969. system ( "/root/Module_LcmControl &" );
  1970. system ( "/root/Module_InternalComm &" );
  1971. system ( "/root/Module_PsuComm &" );
  1972. system ( "/root/Module_ProduceUtils &" );
  1973. ocpp_process_start ();
  1974. if (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'T')
  1975. {
  1976. system ( "/root/Module_4g &" );
  1977. }
  1978. if (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'W')
  1979. {
  1980. system ( "/root/Module_Wifi &" );
  1981. }
  1982. if (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D')
  1983. {
  1984. system ( "/root/Module_4g &" );
  1985. system ( "/root/Module_Wifi &" );
  1986. }
  1987. return PASS;
  1988. }
  1989. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1990. {
  1991. int result = PASS;
  1992. int fd , wrd;
  1993. unsigned int i , Chk;
  1994. unsigned char *ptr , *BufTmp;
  1995. Chk = 0;
  1996. ptr = (unsigned char *) UsrData;
  1997. if ((BufTmp = malloc ( MtdBlockSize )) != NULL)
  1998. {
  1999. memset ( BufTmp, 0, MtdBlockSize );
  2000. memcpy ( BufTmp, ptr, sizeof(struct SysConfigData) );
  2001. for (i = 0; i < MtdBlockSize - 4; i ++)
  2002. Chk += * (ptr + i);
  2003. memcpy ( BufTmp + MtdBlockSize - 4, & Chk, 4 );
  2004. fd = open ( "/dev/mtdblock10", O_RDWR );
  2005. if (fd > 0)
  2006. {
  2007. wrd = write ( fd, BufTmp, MtdBlockSize );
  2008. close ( fd );
  2009. if (wrd >= MtdBlockSize)
  2010. {
  2011. fd = open ( "/dev/mtdblock11", O_RDWR );
  2012. if (fd > 0)
  2013. {
  2014. wrd = write ( fd, BufTmp, MtdBlockSize );
  2015. close ( fd );
  2016. if (wrd < MtdBlockSize)
  2017. {
  2018. DEBUG_ERROR_MSG( "write /dev/mtdblock11(backup) NG\r\n" );
  2019. result = FAIL;
  2020. }
  2021. }
  2022. else
  2023. {
  2024. DEBUG_ERROR_MSG( "open /dev/mtdblock11(backup) NG\r\n" );
  2025. result = FAIL;
  2026. }
  2027. }
  2028. else
  2029. {
  2030. DEBUG_ERROR_MSG( "write /dev/mtdblock10 NG\r\n" );
  2031. result = FAIL;
  2032. }
  2033. }
  2034. else
  2035. {
  2036. DEBUG_ERROR_MSG( "open /dev/mtdblock10 NG\r\n" );
  2037. result = FAIL;
  2038. }
  2039. }
  2040. else
  2041. {
  2042. DEBUG_ERROR_MSG( "alloc BlockSize NG\r\n" );
  2043. result = FAIL;
  2044. }
  2045. if (BufTmp != NULL)
  2046. free ( BufTmp );
  2047. return result;
  2048. }
  2049. //===============================================
  2050. // Common Detect Chk - Stop Charging ?
  2051. //===============================================
  2052. bool isEvBoardStopChargeFlag(byte gunIndex)
  2053. {
  2054. return chargingInfo [gunIndex]->StopChargeFlag;
  2055. }
  2056. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  2057. {
  2058. return chargingInfo [gunIndex]->NormalStopChargeFlag;
  2059. }
  2060. //===============================================
  2061. // 插槍鎖槍狀況
  2062. //===============================================
  2063. void ClearDetectPluginFlag()
  2064. {
  2065. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2066. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  2067. {
  2068. if (chargingInfo [gun_index]->RemoteStartFlag == YES)
  2069. chargingInfo [gun_index]->RemoteStartFlag = NO;
  2070. if (chargingInfo [gun_index]->ReservedStartFlag == YES)
  2071. chargingInfo [gun_index]->ReservedStartFlag = NO;
  2072. }
  2073. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2074. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2075. }
  2076. void DetectPluginStart()
  2077. {
  2078. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2079. }
  2080. bool isDetectPlugin()
  2081. {
  2082. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2083. return YES;
  2084. return NO;
  2085. }
  2086. bool isEvGunLocked(byte gunIndex)
  2087. {
  2088. return chargingInfo [gunIndex]->GunLocked;
  2089. }
  2090. //===============================================
  2091. // Common Detect Chk - Chademo
  2092. //===============================================
  2093. bool isEvContactorWelding_chademo(byte gunIndex)
  2094. {
  2095. return DetectBitValue ( ShmCHAdeMOData->ev [chargingInfo [gunIndex]->type_index].EvDetection, 3 );
  2096. }
  2097. bool isEvStopReq_chademo(byte gunIndex)
  2098. {
  2099. return DetectBitValue ( ShmCHAdeMOData->ev [chargingInfo [gunIndex]->type_index].EvDetection, 4 );
  2100. }
  2101. byte isPrechargeStatus_chademo(byte gunIndex)
  2102. {
  2103. byte result = 0x00;
  2104. result = ShmCHAdeMOData->ev [chargingInfo [gunIndex]->type_index].PresentMsgFlowStatus;
  2105. return result;
  2106. }
  2107. //===============================================
  2108. // Common Detect Chk - GB
  2109. //===============================================
  2110. byte isPrechargeStatus_gb(byte gunIndex)
  2111. {
  2112. byte result = 0x00;
  2113. result = ShmGBTData->ev [chargingInfo [gunIndex]->type_index].PresentMsgFlowStatus;
  2114. return result;
  2115. }
  2116. //===============================================
  2117. // Common Detect Chk - CCS
  2118. //===============================================
  2119. byte isPrechargeStatus_ccs(byte gunIndex)
  2120. {
  2121. byte result = 0x00;
  2122. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2123. {
  2124. result = ShmCcsData->V2GMessage_DIN70121 [chargingInfo [gunIndex]->type_index].PresentMsgFlowStatus;
  2125. }
  2126. return result;
  2127. }
  2128. //===============================================
  2129. // Callback
  2130. //===============================================
  2131. void DisplayChargingInfo()
  2132. {
  2133. PRINTF_FUNC ( "*********** DisplayChargingInfo *********** \n" );
  2134. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  2135. {
  2136. if (chargingInfo [i]->SystemStatus != SYS_MODE_IDLE
  2137. && chargingInfo [i]->SystemStatus != SYS_MODE_RESERVATION)
  2138. {
  2139. ChangeGunSelectByIndex ( i );
  2140. usleep ( 50000 );
  2141. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2142. return;
  2143. }
  2144. }
  2145. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0
  2146. && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE
  2147. && ac_chargingInfo [0]->SystemStatus >= SYS_MODE_PREPARING
  2148. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_COMPLETE)
  2149. {
  2150. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2151. }
  2152. usleep ( 50000 );
  2153. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2154. }
  2155. void _AutoReturnTimeout()
  2156. {
  2157. PRINTF_FUNC ( "*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex );
  2158. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE)
  2159. {
  2160. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2161. {
  2162. ClearDetectPluginFlag ();
  2163. }
  2164. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2165. {
  2166. //正常刷卡process
  2167. DetectPluginStart ();
  2168. }
  2169. }
  2170. else
  2171. {
  2172. //LWN_Zanobe
  2173. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  2174. {
  2175. if (strcmp ( (char *) chargingInfo [i]->EVCCID, "" ) != EQUAL
  2176. && chargingInfo [i]->SystemStatus >= SYS_MODE_PREPARE_FOR_EV
  2177. && chargingInfo [i]->SystemStatus <= SYS_MODE_CHARGING)
  2178. {
  2179. ChargingTerminalProcess ( i );
  2180. break;
  2181. }
  2182. //LWADD0924
  2183. if (chargingInfo [i]->SystemStatus >= SYS_MODE_TERMINATING
  2184. && chargingInfo [i]->SystemStatus <= SYS_MODE_ALARM)
  2185. {
  2186. if (ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZING)
  2187. {
  2188. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  2189. ClearAuthorizedFlag ();
  2190. }
  2191. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  2192. break;
  2193. }
  2194. }
  2195. isCardScan = false;
  2196. }
  2197. usleep ( 50000 );
  2198. stopChargingChkByCard = false;
  2199. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2200. }
  2201. void _SelfTestTimeout()
  2202. {
  2203. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2204. {
  2205. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  2206. {
  2207. ChargingAlarmProcess ( gun_index );
  2208. }
  2209. }
  2210. ShmPsuData->Work_Step = _NO_WORKING;
  2211. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2212. PRINTF_FUNC ( "Self test timeout. \n" );
  2213. }
  2214. void _AuthorizedTimeout()
  2215. {
  2216. //if(IsAuthorizingMode())
  2217. {
  2218. PRINTF_FUNC ( "*********** _AuthorizedTimeout *********** \n" );
  2219. isCardScan = false;
  2220. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2221. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  2222. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  2223. ClearAuthorizedFlag ();
  2224. }
  2225. }
  2226. void _DetectPlugInTimeout()
  2227. {
  2228. PRINTF_FUNC ( "*********** _DetectPlugInTimeout (%d)*********** \n", _connectionTimeout );
  2229. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  2230. ClearDetectPluginFlag ();
  2231. usleep ( 50000 );
  2232. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2233. }
  2234. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2235. {
  2236. if (chargingInfo [gunIndex]->Type == _Type_Chademo)
  2237. {
  2238. if ( ! isEvGunLocked ( gunIndex ))
  2239. {
  2240. PRINTF_FUNC ( "*********** _DetectEvChargingEnableTimeout (chademo) ***********\n" );
  2241. }
  2242. }
  2243. else if (chargingInfo [gunIndex]->Type == _Type_GB)
  2244. {
  2245. if ( ! isEvGunLocked ( gunIndex ))
  2246. {
  2247. PRINTF_FUNC ( "*********** _DetectEvChargingEnableTimeout (gb) ***********\n" );
  2248. }
  2249. }
  2250. else if (chargingInfo [gunIndex]->Type == _Type_CCS)
  2251. {
  2252. if ( ! isEvGunLocked ( gunIndex ))
  2253. {
  2254. PRINTF_FUNC ( "*********** _DetectEvChargingEnableTimeout (ccs) ***********\n" );
  2255. }
  2256. }
  2257. ChargingTerminalProcess ( gunIndex );
  2258. _AutoReturnTimeout ();
  2259. }
  2260. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2261. {
  2262. PRINTF_FUNC ( "*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n" );
  2263. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2264. {
  2265. setChargerMode ( gunIndex, SYS_MODE_IDLE );
  2266. _AutoReturnTimeout ();
  2267. }
  2268. }
  2269. void _PrepareTimeout(byte gunIndex)
  2270. {
  2271. PRINTF_FUNC ( "*********** _PrepareTimeout ***********\n" );
  2272. setChargerMode ( gunIndex, SYS_MODE_IDLE );
  2273. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2274. _AutoReturnTimeout ();
  2275. }
  2276. void _CcsPrechargeTimeout(byte gunIndex)
  2277. {
  2278. PRINTF_FUNC ( "*********** _CcsPrechargeTimeout ***********\n" );
  2279. setChargerMode ( gunIndex, SYS_MODE_IDLE );
  2280. }
  2281. //===============================================
  2282. // 卡號驗證
  2283. //===============================================
  2284. void AuthorizingStart()
  2285. {
  2286. ocpp_set_authorizeReq_cmd ( YES );
  2287. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2288. }
  2289. void ClearAuthorizedFlag()
  2290. {
  2291. ocpp_set_authorizeConf_cmd ( NO );
  2292. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2293. }
  2294. bool isAuthorizedComplete()
  2295. {
  2296. return ocpp_get_authorize_conf ();
  2297. }
  2298. bool IsAuthorizingMode()
  2299. {
  2300. if (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2301. return false;
  2302. return true;
  2303. }
  2304. //===============================================
  2305. // 紀錄 Alarm Code
  2306. //===============================================
  2307. void RecordAlarmCode(byte gunIndex, char *code)
  2308. {
  2309. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 ) == EQUAL)
  2310. memcpy ( chargingInfo [gunIndex]->ConnectorAlarmCode, code, 6 );
  2311. if (strcmp ( code, "012234" ) == EQUAL)
  2312. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaGfdTrip = YES;
  2313. else if (strcmp ( code, "012235" ) == EQUAL)
  2314. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSGfdTrip = YES;
  2315. else if (strcmp ( code, "012236" ) == EQUAL)
  2316. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTGfdTrip = YES;
  2317. else if (strcmp ( code, "012288" ) == EQUAL)
  2318. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSUvpFail = YES;
  2319. else if (strcmp ( code, "012289" ) == EQUAL)
  2320. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaUvpFail = YES;
  2321. else if (strcmp ( code, "012290" ) == EQUAL)
  2322. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTUvpFail = YES;
  2323. else if (strcmp ( code, "012229" ) == EQUAL)
  2324. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectOTP = YES;
  2325. else if (strcmp ( code, "012230" ) == EQUAL)
  2326. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectOTP = YES;
  2327. else if (strcmp ( code, "012231" ) == EQUAL)
  2328. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectOTP = YES;
  2329. else if (strcmp ( code, "012296" ) == EQUAL)
  2330. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaGfdWarning = YES;
  2331. else if (strcmp ( code, "012297" ) == EQUAL)
  2332. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSGfdWarning = YES;
  2333. else if (strcmp ( code, "012298" ) == EQUAL)
  2334. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTGfdWarning = YES;
  2335. else if (strcmp ( code, "011011" ) == EQUAL)
  2336. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2337. else if (strcmp ( code, "011013" ) == EQUAL)
  2338. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2339. else if (strcmp ( code, "011015" ) == EQUAL)
  2340. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2341. else if (strcmp ( code, "011012" ) == EQUAL)
  2342. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaRelayDrivingFault = YES;
  2343. else if (strcmp ( code, "011014" ) == EQUAL)
  2344. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSRelayDrivingFault = YES;
  2345. else if (strcmp ( code, "011016" ) == EQUAL)
  2346. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTRelayDrivingFault = YES;
  2347. else if (strcmp ( code, "011018" ) == EQUAL)
  2348. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  2349. else if (strcmp ( code, "011019" ) == EQUAL)
  2350. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  2351. else if (strcmp ( code, "011020" ) == EQUAL)
  2352. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  2353. else if (strcmp ( code, "012320" ) == EQUAL)
  2354. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectUCP = YES;
  2355. else if (strcmp ( code, "012321" ) == EQUAL)
  2356. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectUCP = YES;
  2357. else if (strcmp ( code, "012322" ) == EQUAL)
  2358. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectUCP = YES;
  2359. }
  2360. void ResetChargerAlarmCode(byte gunIndex, char *code)
  2361. {
  2362. if (strcmp ( code, "012229" ) == EQUAL)
  2363. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectOTP = NO;
  2364. else if (strcmp ( code, "012230" ) == EQUAL)
  2365. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectOTP = NO;
  2366. else if (strcmp ( code, "012231" ) == EQUAL)
  2367. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectOTP = NO;
  2368. else if (strcmp ( code, "011011" ) == EQUAL)
  2369. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaRelayWeldingFault = NO;
  2370. else if (strcmp ( code, "011013" ) == EQUAL)
  2371. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSRelayWeldingFault = NO;
  2372. else if (strcmp ( code, "011015" ) == EQUAL)
  2373. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTRelayWeldingFault = NO;
  2374. else if (strcmp ( code, "011012" ) == EQUAL)
  2375. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaRelayDrivingFault = NO;
  2376. else if (strcmp ( code, "011014" ) == EQUAL)
  2377. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSRelayDrivingFault = NO;
  2378. else if (strcmp ( code, "011016" ) == EQUAL)
  2379. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTRelayDrivingFault = NO;
  2380. else if (strcmp ( code, "011018" ) == EQUAL)
  2381. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  2382. else if (strcmp ( code, "011019" ) == EQUAL)
  2383. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  2384. else if (strcmp ( code, "011020" ) == EQUAL)
  2385. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  2386. else if (strcmp ( code, "012320" ) == EQUAL)
  2387. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectUCP = NO;
  2388. else if (strcmp ( code, "012321" ) == EQUAL)
  2389. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectUCP = NO;
  2390. else if (strcmp ( code, "012322" ) == EQUAL)
  2391. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectUCP = NO;
  2392. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 ) != EQUAL)
  2393. {
  2394. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012229", 6 ) == EQUAL ||
  2395. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012230", 6 ) == EQUAL ||
  2396. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012231", 6 ) == EQUAL ||
  2397. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011011", 6 ) == EQUAL ||
  2398. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011013", 6 ) == EQUAL ||
  2399. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011015", 6 ) == EQUAL ||
  2400. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011012", 6 ) == EQUAL ||
  2401. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011014", 6 ) == EQUAL ||
  2402. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011016", 6 ) == EQUAL ||
  2403. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011018", 6 ) == EQUAL ||
  2404. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011019", 6 ) == EQUAL ||
  2405. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "011020", 6 ) == EQUAL ||
  2406. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012320", 6 ) == EQUAL ||
  2407. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012321", 6 ) == EQUAL ||
  2408. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012322", 6 ) == EQUAL)
  2409. {
  2410. memcpy ( chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 );
  2411. }
  2412. }
  2413. }
  2414. void ReleaseSysAlarmCode(byte gunIndex)
  2415. {
  2416. // 回 idle 後主要清除 GFD Trip、UVP、OVP、GFD Warning
  2417. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 ) != EQUAL)
  2418. {
  2419. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012251", 6 ) == EQUAL ||
  2420. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012252", 6 ) == EQUAL ||
  2421. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012324", 6 ) == EQUAL ||
  2422. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012325", 6 ) == EQUAL)
  2423. {
  2424. memcpy ( chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 );
  2425. }
  2426. }
  2427. if (gunIndex == 0)
  2428. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = NO;
  2429. else if (gunIndex == 1)
  2430. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = NO;
  2431. if (chargingInfo [gunIndex]->Type == _Type_Chademo)
  2432. {
  2433. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaGfdTrip = NO;
  2434. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaUvpFail = NO;
  2435. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectOVP = NO;
  2436. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaGfdWarning = NO;
  2437. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.ChaConnectUCP = NO;
  2438. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 ) != EQUAL)
  2439. {
  2440. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012234", 6 ) == EQUAL ||
  2441. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012289", 6 ) == EQUAL ||
  2442. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012217", 6 ) == EQUAL ||
  2443. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012296", 6 ) == EQUAL)
  2444. {
  2445. memcpy ( chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 );
  2446. }
  2447. }
  2448. }
  2449. else if (chargingInfo [gunIndex]->Type == _Type_CCS)
  2450. {
  2451. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSGfdTrip = NO;
  2452. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSUvpFail = NO;
  2453. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectOVP = NO;
  2454. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSGfdWarning = NO;
  2455. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.CCSConnectUCP = NO;
  2456. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 ) != EQUAL)
  2457. {
  2458. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012235", 6 ) == EQUAL ||
  2459. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012288", 6 ) == EQUAL ||
  2460. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012219", 6 ) == EQUAL ||
  2461. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012297", 6 ) == EQUAL)
  2462. {
  2463. memcpy ( chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 );
  2464. }
  2465. }
  2466. }
  2467. else if (chargingInfo [gunIndex]->Type == _Type_GB)
  2468. {
  2469. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTGfdTrip = NO;
  2470. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTUvpFail = NO;
  2471. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectOVP = NO;
  2472. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTGfdWarning = NO;
  2473. ShmDcCommonData->ConnectErrList [gunIndex].GunBits.GBTConnectUCP = NO;
  2474. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 ) != EQUAL)
  2475. {
  2476. if (strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012236", 6 ) == EQUAL ||
  2477. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012290", 6 ) == EQUAL ||
  2478. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012221", 6 ) == EQUAL ||
  2479. strncmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "012298", 6 ) == EQUAL)
  2480. {
  2481. memcpy ( chargingInfo [gunIndex]->ConnectorAlarmCode, "", 6 );
  2482. }
  2483. }
  2484. }
  2485. ocpp_clear_errorCode_cmd ( gunIndex );
  2486. }
  2487. void NormalStop(byte gunIndex)
  2488. {
  2489. PRINTF_FUNC ( "NormalStop (%d) \n", gunIndex );
  2490. }
  2491. //===============================================
  2492. // EmergencyStop and Charging Stop
  2493. //===============================================
  2494. void ChargingTerminalProcess(byte gunIndex)
  2495. {
  2496. setChargerMode ( gunIndex, SYS_MODE_TERMINATING );
  2497. }
  2498. void ChargingAlarmProcess(byte gunIndex)
  2499. {
  2500. CheckSystemErrorFunction ( gunIndex );
  2501. ocpp_set_errCode_cmd ( gunIndex );
  2502. setChargerMode ( gunIndex, SYS_MODE_ALARM );
  2503. }
  2504. void AcChargingTerminalProcess()
  2505. {
  2506. ac_chargingInfo [0]->SystemStatus = SYS_MODE_TERMINATING;
  2507. }
  2508. //void StopChargingProcessByString(byte level)
  2509. //{
  2510. // if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2511. // {
  2512. // ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2513. // }
  2514. //}
  2515. //void ReleaseChargingProcessByString(byte level)
  2516. //{
  2517. // if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2518. // ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2519. //}
  2520. // 一般錯誤停止充電處理函式
  2521. void BoardErrOccurByString(byte index, char *code)
  2522. {
  2523. //byte level = 1;
  2524. if ((chargingInfo [index]->SystemStatus > SYS_MODE_IDLE && chargingInfo [index]->SystemStatus < SYS_MODE_TERMINATING)
  2525. || (chargingInfo [index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo [index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2526. {
  2527. PRINTF_FUNC ( "BoardErrOccurByString (%d) - (%s) \n", index, code );
  2528. if (strncmp ( code, "023730", 6 ) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2529. {
  2530. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2531. }
  2532. ChargingAlarmProcess ( index );
  2533. }
  2534. //60StopChargingProcessByString(level);
  2535. }
  2536. // 急停狀況的停止充電處理函式
  2537. void EmcOccureThenStopCharging()
  2538. {
  2539. //byte level = 2;
  2540. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen...
  2541. // 其錯誤等級為 2
  2542. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun ++)
  2543. {
  2544. if ((chargingInfo [gun]->SystemStatus > SYS_MODE_IDLE && chargingInfo [gun]->SystemStatus < SYS_MODE_TERMINATING)
  2545. || (chargingInfo [gun]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  2546. && chargingInfo [gun]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2547. {
  2548. ChargingAlarmProcess ( gun );
  2549. }
  2550. }
  2551. //StopChargingProcessByString(level);
  2552. }
  2553. void ReleaseBoardErrOccurByString(byte index, char *code)
  2554. {
  2555. if (strncmp ( code, "023730", 6 ) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2556. {
  2557. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2558. }
  2559. }
  2560. void ReleaseEmsOccureByString(byte index, char *code)
  2561. {
  2562. if (strncmp ( code, "012251", 6 ) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2563. {
  2564. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2565. }
  2566. else if (strncmp ( code, "012252", 6 ) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2567. {
  2568. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2569. }
  2570. else if (strncmp ( code, "012237", 6 ) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2571. {
  2572. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2573. }
  2574. else if (strncmp ( code, "012238", 6 ) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2575. {
  2576. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2577. }
  2578. }
  2579. //===============================================
  2580. // 確認硬體 (按鈕) 狀態
  2581. //===============================================
  2582. bool leftBtnPush = false;
  2583. bool rightBtnPush = false;
  2584. void ChkPrimaryStatus()
  2585. {
  2586. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2587. {
  2588. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2589. EmcOccureThenStopCharging ();
  2590. }
  2591. else
  2592. ReleaseEmsOccureByString ( 0, "012251" );
  2593. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2594. {
  2595. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2596. EmcOccureThenStopCharging ();
  2597. }
  2598. else
  2599. ReleaseEmsOccureByString ( 0, "012238" );
  2600. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2601. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2602. else
  2603. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2604. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2605. {
  2606. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2607. EmcOccureThenStopCharging ();
  2608. }
  2609. else
  2610. ReleaseEmsOccureByString ( 0, "012252" );
  2611. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2612. {
  2613. EmcOccureThenStopCharging ();
  2614. }
  2615. else
  2616. ReleaseEmsOccureByString ( 0, "012267" );
  2617. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS)
  2618. ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2619. else
  2620. ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2621. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && ! leftBtnPush)
  2622. {
  2623. if ( ! leftBtnPush)
  2624. {
  2625. leftBtnPush = true;
  2626. PRINTF_FUNC ( "left btn down............................... \n" );
  2627. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2628. {
  2629. switch (ac_chargingInfo [0]->SystemStatus)
  2630. {
  2631. case SYS_MODE_IDLE :
  2632. case SYS_MODE_MAINTAIN :
  2633. case SYS_MODE_RESERVATION :
  2634. {
  2635. if (isDetectPlugin ())
  2636. {
  2637. _DetectPlugInTimeout ();
  2638. StopSystemTimeoutDet ();
  2639. }
  2640. }
  2641. break;
  2642. case SYS_MODE_MODE_REASSIGN_CHECK :
  2643. case SYS_MODE_REASSIGN :
  2644. case SYS_MODE_PREPARING :
  2645. case SYS_MODE_PREPARE_FOR_EV :
  2646. case SYS_MODE_PREPARE_FOR_EVSE :
  2647. case SYS_MODE_CCS_PRECHARGE_STEP0 :
  2648. case SYS_MODE_CCS_PRECHARGE_STEP1 :
  2649. {
  2650. // 取消充電
  2651. AcChargingTerminalProcess ();
  2652. }
  2653. break;
  2654. case SYS_MODE_CHARGING :
  2655. {
  2656. //LWN_Zanobe
  2657. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  2658. {
  2659. // 停止充電
  2660. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE; //LWADD0924
  2661. AcChargingTerminalProcess ();
  2662. }
  2663. }
  2664. break;
  2665. case SYS_MODE_COMPLETE :
  2666. {
  2667. }
  2668. break;
  2669. }
  2670. }
  2671. switch (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2672. {
  2673. case SYS_MODE_IDLE :
  2674. case SYS_MODE_MAINTAIN :
  2675. case SYS_MODE_RESERVATION :
  2676. {
  2677. if (isDetectPlugin ())
  2678. {
  2679. _DetectPlugInTimeout ();
  2680. StopSystemTimeoutDet ();
  2681. }
  2682. }
  2683. break;
  2684. case SYS_MODE_MODE_REASSIGN_CHECK :
  2685. case SYS_MODE_REASSIGN :
  2686. case SYS_MODE_PREPARING :
  2687. case SYS_MODE_PREPARE_FOR_EV :
  2688. case SYS_MODE_PREPARE_FOR_EVSE :
  2689. case SYS_MODE_CCS_PRECHARGE_STEP0 :
  2690. case SYS_MODE_CCS_PRECHARGE_STEP1 :
  2691. {
  2692. // 按鈕 - 取消充電
  2693. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2694. // AcChargingTerminalProcess();
  2695. // else
  2696. //LWN_Zanobe
  2697. //LWADD0924
  2698. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  2699. {
  2700. if (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_PREPARE_FOR_EV)
  2701. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  2702. }
  2703. ChargingTerminalProcess ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2704. }
  2705. break;
  2706. case SYS_MODE_CHARGING :
  2707. {
  2708. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES
  2709. || (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE
  2710. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO))
  2711. {
  2712. // 按鈕 - 停止充電
  2713. NormalStop ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2714. ChargingTerminalProcess ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2715. }
  2716. }
  2717. break;
  2718. case SYS_MODE_COMPLETE :
  2719. {
  2720. // 回 IDLE
  2721. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  2722. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = SYS_MODE_IDLE;
  2723. }
  2724. break;
  2725. }
  2726. }
  2727. }
  2728. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2729. {
  2730. if (leftBtnPush)
  2731. {
  2732. leftBtnPush = false;
  2733. PRINTF_FUNC ( "left btn up............................... \n" );
  2734. }
  2735. }
  2736. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && ! rightBtnPush)
  2737. {
  2738. if ( ! rightBtnPush)
  2739. {
  2740. rightBtnPush = true;
  2741. PRINTF_FUNC ( "right btn down .................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2742. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount
  2743. && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2744. {
  2745. ShmSysConfigAndInfo->SysInfo.CurGunSelected ++;
  2746. ChangeGunSelectByIndex ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2747. }
  2748. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0
  2749. && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  2750. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2751. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2752. {
  2753. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  2754. {
  2755. if (chargingInfo [_index]->SystemStatus != SYS_MODE_BOOTING
  2756. && chargingInfo [_index]->SystemStatus != SYS_MODE_IDLE
  2757. && chargingInfo [_index]->SystemStatus != SYS_MODE_RESERVATION)
  2758. {
  2759. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2760. ChangeGunSelectByIndex ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2761. return;
  2762. }
  2763. }
  2764. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2765. ChangeGunSelectByIndex ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2766. }
  2767. else
  2768. {
  2769. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2770. ChangeGunSelectByIndex ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  2771. }
  2772. }
  2773. }
  2774. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2775. {
  2776. if (rightBtnPush)
  2777. {
  2778. rightBtnPush = false;
  2779. PRINTF_FUNC ( "right btn up............................... \n" );
  2780. }
  2781. }
  2782. }
  2783. //===============================================
  2784. // 確認各小板偵測的錯誤狀況
  2785. //===============================================
  2786. void CheckSystemErrorFunction(byte index)
  2787. {
  2788. // System
  2789. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  2790. RecordAlarmCode ( index, "012251" );
  2791. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip)
  2792. RecordAlarmCode ( index, "012238" );
  2793. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2794. RecordAlarmCode ( index, "012252" );
  2795. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2796. RecordAlarmCode ( index, "012267" );
  2797. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2798. {
  2799. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES
  2800. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES
  2801. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2802. {
  2803. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2804. {
  2805. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2806. }
  2807. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2808. RecordAlarmCode ( index, "012203" );
  2809. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2810. RecordAlarmCode ( index, "012204" );
  2811. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2812. RecordAlarmCode ( index, "012205" );
  2813. chargingInfo [index]->StopChargeFlag = YES;
  2814. }
  2815. else
  2816. {
  2817. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2818. {
  2819. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2820. }
  2821. }
  2822. }
  2823. else
  2824. {
  2825. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2826. {
  2827. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2828. }
  2829. }
  2830. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES
  2831. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES
  2832. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2833. {
  2834. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2835. {
  2836. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2837. }
  2838. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2839. RecordAlarmCode ( index, "012200" );
  2840. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2841. RecordAlarmCode ( index, "012201" );
  2842. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2843. RecordAlarmCode ( index, "012202" );
  2844. chargingInfo [index]->StopChargeFlag = YES;
  2845. }
  2846. else
  2847. {
  2848. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2849. {
  2850. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2851. }
  2852. }
  2853. }
  2854. //===============================================
  2855. // 確認 GPIO 狀態
  2856. //===============================================
  2857. void gpio_set_value(unsigned int gpio, unsigned int value)
  2858. {
  2859. int fd;
  2860. char buf [MAX_BUF];
  2861. snprintf ( buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio );
  2862. fd = open ( buf, O_WRONLY );
  2863. if (fd < 0)
  2864. {
  2865. perror ( "gpio/set-value" );
  2866. return;
  2867. }
  2868. if (value)
  2869. write ( fd, "1", 2 );
  2870. else
  2871. write ( fd, "0", 2 );
  2872. close ( fd );
  2873. }
  2874. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2875. {
  2876. int fd;
  2877. char buf [MAX_BUF];
  2878. char ch;
  2879. snprintf ( buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio );
  2880. fd = open ( buf, O_RDONLY );
  2881. if (fd < 0)
  2882. {
  2883. perror ( "gpio/get-value" );
  2884. return fd;
  2885. }
  2886. read ( fd, & ch, 1 );
  2887. if (ch != '0')
  2888. {
  2889. * value = 1;
  2890. }
  2891. else
  2892. {
  2893. * value = 0;
  2894. }
  2895. close ( fd );
  2896. return 0;
  2897. }
  2898. void CheckGunTypeFromHw()
  2899. {
  2900. int pinIn [4] = { 22, 23, 44, 45 };
  2901. unsigned int gpioValue = 0;
  2902. for (int i = 0; i < ARRAY_SIZE( pinIn ); i ++)
  2903. {
  2904. gpio_get_value ( pinIn [i], & gpioValue );
  2905. {
  2906. switch (pinIn [i])
  2907. {
  2908. case 22 :
  2909. bd1_1_status = gpioValue;
  2910. break;
  2911. case 23 :
  2912. bd1_2_status = gpioValue;
  2913. break;
  2914. case 44 :
  2915. bd0_1_status = gpioValue;
  2916. break;
  2917. case 45 :
  2918. bd0_2_status = gpioValue;
  2919. break;
  2920. }
  2921. }
  2922. }
  2923. }
  2924. void CheckGpioInStatus()
  2925. {
  2926. int pinIn [2] = { 27, 47 };
  2927. unsigned int gpioValue = 0;
  2928. for (int i = 0; i < ARRAY_SIZE( pinIn ); i ++)
  2929. {
  2930. gpio_get_value ( pinIn [i], & gpioValue );
  2931. if (gpioValue == 0x01)
  2932. {
  2933. switch (pinIn [i])
  2934. {
  2935. // 小板緊急停止
  2936. case 47 :
  2937. {
  2938. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  2939. {
  2940. if (chargingInfo [i]->slotsIndex == 1)
  2941. {
  2942. if (chargingInfo [i]->Type == _Type_Chademo)
  2943. BoardErrOccurByString ( i, "023730" );
  2944. // else if (chargingInfo[i]->Type == _Type_CCS)
  2945. // BoardErrOccurByString(i, "013627");
  2946. break;
  2947. }
  2948. }
  2949. }
  2950. break;
  2951. case 27 :
  2952. {
  2953. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  2954. {
  2955. if (chargingInfo [i]->slotsIndex == 3)
  2956. {
  2957. if (chargingInfo [i]->Type == _Type_Chademo)
  2958. BoardErrOccurByString ( i, "023730" );
  2959. // else if (chargingInfo[i]->Type == _Type_CCS)
  2960. // BoardErrOccurByString(i, "013627");
  2961. break;
  2962. }
  2963. }
  2964. }
  2965. break;
  2966. }
  2967. }
  2968. else
  2969. {
  2970. switch (pinIn [i])
  2971. {
  2972. // 小板解除緊急停止
  2973. case 47 :
  2974. {
  2975. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  2976. {
  2977. if (chargingInfo [i]->slotsIndex == 1)
  2978. {
  2979. if (chargingInfo [i]->Type == _Type_Chademo)
  2980. ReleaseBoardErrOccurByString ( i, "023730" );
  2981. // else if (chargingInfo[i]->Type == _Type_CCS)
  2982. // ReleaseBoardErrOccurByString(i, "013627");
  2983. break;
  2984. }
  2985. }
  2986. }
  2987. break;
  2988. case 27 :
  2989. {
  2990. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  2991. {
  2992. if (chargingInfo [i]->slotsIndex == 3)
  2993. {
  2994. if (chargingInfo [i]->Type == _Type_Chademo)
  2995. ReleaseBoardErrOccurByString ( i, "023730" );
  2996. // else if (chargingInfo[i]->Type == _Type_CCS)
  2997. // ReleaseBoardErrOccurByString(i, "013627");
  2998. break;
  2999. }
  3000. }
  3001. }
  3002. break;
  3003. }
  3004. }
  3005. }
  3006. }
  3007. //===============================================
  3008. // Main process
  3009. //===============================================
  3010. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  3011. {
  3012. for (byte index = 0; index < CHAdeMO_QUANTITY; index ++)
  3013. {
  3014. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData [index].Index == target)
  3015. {
  3016. chargingData [target] = & ShmSysConfigAndInfo->SysInfo.ChademoChargingData [index];
  3017. return true;
  3018. }
  3019. }
  3020. for (byte index = 0; index < CCS_QUANTITY; index ++)
  3021. {
  3022. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData [index].Index == target)
  3023. {
  3024. chargingData [target] = & ShmSysConfigAndInfo->SysInfo.CcsChargingData [index];
  3025. return true;
  3026. }
  3027. }
  3028. for (byte index = 0; index < GB_QUANTITY; index ++)
  3029. {
  3030. if (ShmSysConfigAndInfo->SysInfo.GbChargingData [index].Index == target)
  3031. {
  3032. chargingData [target] = & ShmSysConfigAndInfo->SysInfo.GbChargingData [index];
  3033. return true;
  3034. }
  3035. }
  3036. return false;
  3037. }
  3038. // 檢查 Byte 中某個 Bit 的值
  3039. // _byte : 欲改變的 byte
  3040. // _bit : 該 byte 的第幾個 bit
  3041. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  3042. {
  3043. return (_byte & mask_table [_bit]) != 0x00;
  3044. }
  3045. // 設定 Byte 中某個 Bit的值
  3046. // _byte : 欲改變的 byte
  3047. // _bit : 該 byte 的第幾個 bit
  3048. // value : 修改的值為 0 or 1
  3049. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  3050. {
  3051. if (value == 1)
  3052. * _byte |= (1 << _bit);
  3053. else if (value == 0)
  3054. * _byte ^= (1 << _bit);
  3055. }
  3056. void UserScanFunction()
  3057. {
  3058. bool idleReq = false;
  3059. unsigned char idleIndex = 255;
  3060. //unsigned char stopReq = 255;
  3061. bool hasGunUsing = false;
  3062. bool isReservedCard = false;
  3063. bool isPlugAndCharge = NO; //LWN_Zanobe
  3064. // 當前非驗證的狀態
  3065. if ( ! IsAuthorizingMode ())
  3066. {
  3067. // 先判斷現在是否可以提供刷卡
  3068. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  3069. // 2. 停止充電
  3070. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX
  3071. || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_EMC)
  3072. {
  3073. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3074. //LWN_Zanobe
  3075. //To clear the EVCCID.
  3076. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  3077. {
  3078. memset ( chargingInfo [i]->EVCCID, 0, sizeof(chargingInfo [i]->EVCCID) );
  3079. }
  3080. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  3081. return;
  3082. }
  3083. if (strlen ( (char *) ShmSysConfigAndInfo->SysConfig.UserId ) > 0)
  3084. {
  3085. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  3086. {
  3087. // 如果選 AC 槍
  3088. if (ac_chargingInfo [0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  3089. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_ALARM)
  3090. {
  3091. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_CHARGING)
  3092. {
  3093. char value [32];
  3094. PRINTF_FUNC ( "ac stop charging \n" );
  3095. PRINTF_FUNC ( "index = %d, card number = %s, UserId = %s \n",
  3096. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc, ac_chargingInfo [0]->StartUserId,
  3097. ShmSysConfigAndInfo->SysConfig.UserId );
  3098. memcpy ( value, (unsigned char *) ac_chargingInfo [0]->StartUserId,
  3099. ARRAY_SIZE( ac_chargingInfo [0]->StartUserId ) );
  3100. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, value ) == EQUAL)
  3101. {
  3102. AcChargingTerminalProcess ();
  3103. }
  3104. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3105. }
  3106. else if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE)
  3107. {
  3108. idleReq = true;
  3109. }
  3110. }
  3111. }
  3112. else
  3113. {
  3114. //LWN_Zanobe
  3115. //LWADD0924
  3116. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE)
  3117. {
  3118. if (IsGunUsing ( ShmSysConfigAndInfo->SysInfo.CurGunSelected ))
  3119. {
  3120. if (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_CHARGING)
  3121. {
  3122. char value [32];
  3123. PRINTF_FUNC ( "stop charging \n" );
  3124. PRINTF_FUNC ( "index = %d, card number = %s, UserId = %s \n",
  3125. ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  3126. chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3127. ShmSysConfigAndInfo->SysConfig.UserId );
  3128. memcpy ( value,
  3129. (unsigned char *) chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3130. ARRAY_SIZE( chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId ) );
  3131. // 同一張卡直接停掉
  3132. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, value ) == EQUAL)
  3133. {
  3134. NormalStop ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  3135. ChargingTerminalProcess ( ShmSysConfigAndInfo->SysInfo.CurGunSelected );
  3136. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3137. }
  3138. else
  3139. {
  3140. PRINTF_FUNC ( "_LCM_AUTHORIZ_FAIL \n" );
  3141. stopChargingChkByCard = true;
  3142. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3143. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3144. }
  3145. }
  3146. }
  3147. else
  3148. {
  3149. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  3150. {
  3151. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_IDLE)
  3152. {
  3153. idleIndex = gun_index;
  3154. idleReq = true;
  3155. }
  3156. else if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3157. {
  3158. idleReq = true;
  3159. }
  3160. else
  3161. {
  3162. hasGunUsing = IsGunUsing ( gun_index );
  3163. }
  3164. }
  3165. // 壁掛不給充 - 已經有至少一把槍在充電
  3166. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES && hasGunUsing)
  3167. {
  3168. PRINTF_FUNC ( "DW Serial not support dual charging. \n" );
  3169. idleReq = false;
  3170. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3171. }
  3172. }
  3173. }
  3174. else
  3175. {
  3176. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  3177. {
  3178. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_PREPARE_FOR_EV)
  3179. {
  3180. idleReq = true;
  3181. isPlugAndCharge = YES;
  3182. break;
  3183. }
  3184. }
  3185. }
  3186. }
  3187. if (idleReq)
  3188. {
  3189. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  3190. {
  3191. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3192. {
  3193. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3194. (char *) ShmDcCommonData->_reserved_UserId [gun_index] ) == EQUAL)
  3195. {
  3196. isReservedCard = true;
  3197. // 驗證通過
  3198. chargingInfo [gun_index]->ReservedStartFlag = YES;
  3199. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3200. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3201. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3202. break;
  3203. }
  3204. }
  3205. }
  3206. //LWN_Zanobe
  3207. if ( ! isReservedCard && ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  3208. || (idleIndex != 255 && chargingInfo [idleIndex]->SystemStatus == SYS_MODE_IDLE) || isPlugAndCharge == YES))
  3209. {
  3210. PRINTF_FUNC ( "Start Authorizing... \n" );
  3211. if (isPlugAndCharge != YES) //非隨插即充
  3212. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING; // LCM => Authorizing
  3213. else //隨插即充
  3214. {
  3215. //Connect with the Backend.
  3216. if (ShmOCPP16Data->OcppConnStatus != NO)
  3217. {
  3218. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZING;
  3219. PRINTF_FUNC ( "AuthStatus = AUTHORIZING \n" );
  3220. }
  3221. else //Disconnect with the Backend.
  3222. {
  3223. //Not allow for charging.
  3224. if (_OCPP_LocalAuthorizeOffline == NO)
  3225. {
  3226. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_FAIL;
  3227. PRINTF_FUNC ( "Disconnect with the Backend. Not allow for charging. \n" );
  3228. }
  3229. //Allow for charging.
  3230. else
  3231. {
  3232. //Check OfflinePolicy setting.
  3233. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  3234. {
  3235. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_COMP;
  3236. PRINTF_FUNC ( "Disconnect with the Backend. Allow for charging. \n" );
  3237. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3238. }
  3239. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3240. {
  3241. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZING;
  3242. }
  3243. else //_OFFLINE_POLICY_NO_CHARGING
  3244. {
  3245. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_FAIL;
  3246. PRINTF_FUNC ( "Disconnect with the Backend. Offline Policy = No Charging.\n" );
  3247. PRINTF_FUNC ( "Not allow Charging.\n" );
  3248. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3249. }
  3250. }
  3251. return;
  3252. }
  3253. }
  3254. // 進入確認卡號狀態
  3255. AuthorizingStart ();
  3256. }
  3257. else
  3258. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3259. }
  3260. else
  3261. {
  3262. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3263. }
  3264. }
  3265. }
  3266. // else
  3267. // {
  3268. // // 透過後臺停止充電的判斷
  3269. // if (isAuthorizedComplete() ||
  3270. // (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3271. // ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3272. // {
  3273. // // 判斷後台回覆狀態
  3274. // if(ocpp_chk_authrization_cmd() ||
  3275. // (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3276. // ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3277. // {
  3278. // if (_authorizeIndex != NO_DEFINE)
  3279. // {
  3280. // // 先找 AC
  3281. // if (_authorizeIndex == DEFAULT_AC_INDEX)
  3282. // {
  3283. // if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  3284. // strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL)
  3285. // {
  3286. // AcChargingTerminalProcess();
  3287. // }
  3288. // }
  3289. // else
  3290. // {
  3291. // if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  3292. // strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL)
  3293. // {
  3294. // // 刷卡停止
  3295. // ChargingTerminalProcess(_authorizeIndex);
  3296. // }
  3297. // }
  3298. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3299. // _authorizeIndex = NO_DEFINE;
  3300. // }
  3301. // }
  3302. // else
  3303. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3304. // ClearAuthorizedFlag();
  3305. // }
  3306. // else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3307. // {
  3308. // // 白名單驗證
  3309. // for (int i = 0; i < 10; i++)
  3310. // {
  3311. // if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3312. // {
  3313. // if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3314. // {
  3315. // // 白名單驗證停止
  3316. // ChargingTerminalProcess(_authorizeIndex);
  3317. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3318. // ClearAuthorizedFlag();
  3319. // break;
  3320. // }
  3321. // }
  3322. // }
  3323. // }
  3324. // }
  3325. }
  3326. unsigned char isModeChange(unsigned char gun_index)
  3327. {
  3328. unsigned char result = NO;
  3329. if (chargingInfo [gun_index]->SystemStatus != chargingInfo [gun_index]->PreviousSystemStatus)
  3330. {
  3331. result = YES;
  3332. chargingInfo [gun_index]->PreviousSystemStatus = chargingInfo [gun_index]->SystemStatus;
  3333. }
  3334. return result;
  3335. }
  3336. void ScannerCardProcess()
  3337. {
  3338. //LWN_Zanobe
  3339. //Copying the EVCCID to the SysConfig.UserId for authoizing from backend.
  3340. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3341. {
  3342. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  3343. {
  3344. if (chargingInfo [i]->SystemStatus == SYS_MODE_PREPARE_FOR_EV
  3345. //&& strcmp ( (char *) chargingInfo [i]->StartUserId, "" ) == EQUAL //LWADD
  3346. && ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_NONE
  3347. && strcmp ( (char *) chargingInfo [i]->EVCCID, "" ) != EQUAL)
  3348. {
  3349. if (strcmp ( (char*) ShmSysConfigAndInfo->SysConfig.UserId, (char*) chargingInfo [i]->EVCCID ) != EQUAL)
  3350. {
  3351. memcpy ( ShmSysConfigAndInfo->SysConfig.UserId, chargingInfo [i]->EVCCID, sizeof(chargingInfo [i]->EVCCID) );
  3352. PRINTF_FUNC ( " Copy the EVCCID to UserId Success! (%s)\n", ShmSysConfigAndInfo->SysConfig.UserId );
  3353. break;
  3354. }
  3355. }
  3356. }
  3357. }
  3358. //LWN_Zanobe
  3359. //Remove ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE
  3360. if ( ! isDetectPlugin () && ! isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  3361. {
  3362. isCardScan = true;
  3363. // 處理刷卡及驗證卡號的動作
  3364. UserScanFunction ();
  3365. }
  3366. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE)
  3367. {
  3368. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  3369. {
  3370. StartSystemTimeoutDet ( Timeout_Authorizing );
  3371. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3372. {
  3373. // 白名單驗證
  3374. for (int i = 0; i < 10; i ++)
  3375. {
  3376. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.LocalWhiteCard [i], "" ) != EQUAL)
  3377. {
  3378. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.LocalWhiteCard [i],
  3379. (char *) ShmSysConfigAndInfo->SysConfig.UserId ) == EQUAL)
  3380. {
  3381. PRINTF_FUNC ( "White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId );
  3382. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3383. ClearAuthorizedFlag ();
  3384. break;
  3385. }
  3386. }
  3387. }
  3388. }
  3389. // 確認驗證卡號完成沒
  3390. if (isAuthorizedComplete ()
  3391. || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO
  3392. && ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3393. {
  3394. StopSystemTimeoutDet ();
  3395. // 判斷後台回覆狀態
  3396. if (ocpp_chk_authrization_cmd ()
  3397. || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO
  3398. && ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3399. {
  3400. // LCM => Authorize complete
  3401. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3402. }
  3403. else
  3404. {
  3405. // LCM => Authorize fail
  3406. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3407. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3408. }
  3409. ClearAuthorizedFlag ();
  3410. }
  3411. }
  3412. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  3413. {
  3414. StartSystemTimeoutDet ( Timeout_VerifyFail );
  3415. isCardScan = false;
  3416. }
  3417. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  3418. {
  3419. StartSystemTimeoutDet ( Timeout_VerifyComp );
  3420. }
  3421. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE
  3422. && ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  3423. {
  3424. StartSystemTimeoutDet ( Timeout_WaitPlug );
  3425. }
  3426. else
  3427. isCardScan = false;
  3428. }
  3429. else //AUTH_MODE_DISABLE.
  3430. {
  3431. //LWN_Zanobe
  3432. if (ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZING)
  3433. {
  3434. //StartSystemTimeoutDet ( Timeout_Authorizing );//LWADD
  3435. // 白名單驗證
  3436. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO
  3437. && ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST
  3438. && _OCPP_LocalAuthorizeOffline == YES)
  3439. {
  3440. bool isPass = FALSE;
  3441. for (int i = 0; i < 10; i ++)
  3442. {
  3443. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.LocalWhiteCard [i], "" ) != EQUAL)
  3444. {
  3445. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.LocalWhiteCard [i],
  3446. (char *) ShmSysConfigAndInfo->SysConfig.UserId ) == EQUAL)
  3447. {
  3448. PRINTF_FUNC ( "White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId );
  3449. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_COMP;
  3450. isPass = TRUE;
  3451. ClearAuthorizedFlag ();
  3452. break;
  3453. }
  3454. }
  3455. }
  3456. //LWADD
  3457. if (isPass != TRUE)
  3458. {
  3459. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_FAIL;
  3460. PRINTF_FUNC ( "White Card Fail (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId );
  3461. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3462. }
  3463. }
  3464. // 確認卡號驗證是否完成 or 允許離線充電
  3465. if (isAuthorizedComplete ())
  3466. {
  3467. StopSystemTimeoutDet ();
  3468. PRINTF_FUNC ( "EVCCID Authorized Complite ...\n" );
  3469. // 判斷後台回覆狀態
  3470. if (ocpp_chk_authrization_cmd ())
  3471. {
  3472. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_COMP;
  3473. PRINTF_FUNC ( "EVCCID Authrization Accept ... \n" );
  3474. //Copy the SysConfig.UserId to StartUserId, and clear the SysConfig.UserId.
  3475. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i ++)
  3476. {
  3477. if (strcmp ( (char *) chargingInfo [i]->EVCCID, "" ) != EQUAL
  3478. && strcmp ( (char *) chargingInfo [i]->EVCCID, (char *) ShmSysConfigAndInfo->SysConfig.UserId ) == EQUAL)
  3479. {
  3480. strcpy ( (char *) chargingInfo [i]->StartUserId, (char *) ShmSysConfigAndInfo->SysConfig.UserId );
  3481. break;
  3482. }
  3483. }
  3484. }
  3485. else
  3486. {
  3487. // LCM => Authorize fail
  3488. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_AUTHORIZ_FAIL;
  3489. PRINTF_FUNC ( "EVCCID Authorized Reject ...\n" );
  3490. }
  3491. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" ); //LWADD
  3492. ClearAuthorizedFlag ();
  3493. }
  3494. }
  3495. else if (ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZ_FAIL)
  3496. {
  3497. //StartSystemTimeoutDet ( Timeout_VerifyFail );//LWADD
  3498. isCardScan = false;
  3499. }
  3500. else if (ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZ_COMP)
  3501. {
  3502. //StartSystemTimeoutDet ( Timeout_VerifyComp );
  3503. }
  3504. else
  3505. isCardScan = false;
  3506. }
  3507. }
  3508. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3509. {
  3510. bool result = true;
  3511. switch (typeValue)
  3512. {
  3513. case '0' : // none
  3514. break;
  3515. case '1' : // IEC 62196-2 Type 1/SAE J1772 Plug
  3516. break;
  3517. case '2' : // IEC 62196-2 Type 1/SAE J1772 Socket
  3518. break;
  3519. case '3' : // IEC 62196-2 Type 2 Plug
  3520. case '4' : // IEC 62196-2 Type 2 Socket
  3521. if (AC_QUANTITY > _ac_Index)
  3522. {
  3523. ac_chargingInfo [_acgunIndex] = & ShmSysConfigAndInfo->SysInfo.AcChargingData [_ac_Index];
  3524. // AC 固定 index
  3525. ac_chargingInfo [_acgunIndex]->Index = 0;
  3526. ac_chargingInfo [_acgunIndex]->ReservationId = - 1;
  3527. ac_chargingInfo [_acgunIndex]->SystemStatus = SYS_MODE_IDLE;
  3528. ac_chargingInfo [_acgunIndex]->Type = _Type_AC;
  3529. ac_chargingInfo [_acgunIndex]->IsAvailable = YES;
  3530. ac_chargingInfo [_acgunIndex]->schedule.isTriggerStart = NO;
  3531. ac_chargingInfo [_acgunIndex]->schedule.isTriggerStop = NO;
  3532. _ac_Index ++;
  3533. _acgunIndex ++;
  3534. }
  3535. else
  3536. {
  3537. result = false;
  3538. }
  3539. break;
  3540. case '5' : // GB/T AC Plug
  3541. break;
  3542. case '6' : // GB/T AC Socket
  3543. break;
  3544. case 'J' : // CHAdeMO
  3545. case 'K' : // CHAdeMO - 200A / 500V
  3546. {
  3547. if (CHAdeMO_QUANTITY > _chademoIndex)
  3548. {
  3549. chargingInfo [_gunIndex] = & ShmSysConfigAndInfo->SysInfo.ChademoChargingData [_chademoIndex];
  3550. chargingInfo [_gunIndex]->Index = _gunIndex;
  3551. chargingInfo [_gunIndex]->ReservationId = - 1;
  3552. chargingInfo [_gunIndex]->slotsIndex = slots;
  3553. chargingInfo [_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3554. chargingInfo [_gunIndex]->Type = _Type_Chademo;
  3555. chargingInfo [_gunIndex]->type_index = _chademoIndex;
  3556. chargingInfo [_gunIndex]->IsAvailable = YES;
  3557. chargingInfo [_acgunIndex]->schedule.isTriggerStart = NO;
  3558. chargingInfo [_acgunIndex]->schedule.isTriggerStop = NO;
  3559. chargingInfo [_gunIndex]->ModelType = typeValue;
  3560. _chademoIndex ++;
  3561. _gunIndex ++;
  3562. }
  3563. else
  3564. result = false;
  3565. }
  3566. break;
  3567. case 'U' : // Natural cooling CCS1
  3568. case 'E' : // Natural cooling CCS2
  3569. case 'T' : // Rema CCS1 - 300A
  3570. case 'D' : // Rema CCS2 - 300A
  3571. case 'M' : // CCS2 - 80A
  3572. case 'N' : // CCS1 - 80A
  3573. {
  3574. if (CCS_QUANTITY > _ccsIndex)
  3575. {
  3576. chargingInfo [_gunIndex] = & ShmSysConfigAndInfo->SysInfo.CcsChargingData [_ccsIndex];
  3577. chargingInfo [_gunIndex]->Index = _gunIndex;
  3578. chargingInfo [_gunIndex]->ReservationId = - 1;
  3579. chargingInfo [_gunIndex]->slotsIndex = slots;
  3580. chargingInfo [_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3581. chargingInfo [_gunIndex]->Type = _Type_CCS;
  3582. chargingInfo [_gunIndex]->type_index = _ccsIndex;
  3583. chargingInfo [_gunIndex]->IsAvailable = YES;
  3584. chargingInfo [_acgunIndex]->schedule.isTriggerStart = NO;
  3585. chargingInfo [_acgunIndex]->schedule.isTriggerStop = NO;
  3586. chargingInfo [_gunIndex]->ModelType = typeValue;
  3587. // 現階段預設為走 DIN70121
  3588. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3589. if (typeValue == 'U' || typeValue == 'T' || typeValue == 'N')
  3590. ShmDcCommonData->CcsTypeSaved [_gunIndex] = _CCS_TYPE_CCS1;
  3591. else if (typeValue == 'E' || typeValue == 'D' || typeValue == 'M')
  3592. ShmDcCommonData->CcsTypeSaved [_gunIndex] = _CCS_TYPE_CCS2;
  3593. _ccsIndex ++;
  3594. _gunIndex ++;
  3595. }
  3596. else
  3597. result = false;
  3598. }
  3599. break;
  3600. case 'G' : // GBT DC
  3601. case 'B' :
  3602. {
  3603. if (GB_QUANTITY > _gb_Index)
  3604. {
  3605. chargingInfo [_gunIndex] = & ShmSysConfigAndInfo->SysInfo.GbChargingData [_gb_Index];
  3606. chargingInfo [_gunIndex]->Index = _gunIndex;
  3607. chargingInfo [_gunIndex]->ReservationId = - 1;
  3608. chargingInfo [_gunIndex]->slotsIndex = slots;
  3609. chargingInfo [_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3610. chargingInfo [_gunIndex]->Type = _Type_GB;
  3611. chargingInfo [_gunIndex]->type_index = _gb_Index;
  3612. chargingInfo [_gunIndex]->IsAvailable = YES;
  3613. chargingInfo [_acgunIndex]->schedule.isTriggerStart = NO;
  3614. chargingInfo [_acgunIndex]->schedule.isTriggerStop = NO;
  3615. chargingInfo [_gunIndex]->ModelType = typeValue;
  3616. _gb_Index ++;
  3617. _gunIndex ++;
  3618. }
  3619. else
  3620. result = false;
  3621. }
  3622. break;
  3623. }
  3624. return result;
  3625. }
  3626. bool CheckConnectorTypeStatus()
  3627. {
  3628. bool result = true;
  3629. // 硬體 0 1 => CCS,1 0 => Chademo,1 1 => GB
  3630. //CheckFwSlotSta.tusLog();
  3631. if (strlen ( (char *) ShmSysConfigAndInfo->SysConfig.ModelName ) >= 9)
  3632. {
  3633. byte slots = 1;
  3634. for (byte typeIndex = 7; typeIndex <= 9; typeIndex ++)
  3635. {
  3636. if ( ! AddGunInfoByConnector ( ShmSysConfigAndInfo->SysConfig.ModelName [typeIndex], slots ))
  3637. {
  3638. return false;
  3639. }
  3640. slots ++;
  3641. }
  3642. // AC index 接在 DC 後面
  3643. if (_ac_Index > 0)
  3644. ac_chargingInfo [0]->Index += _gunIndex;
  3645. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3646. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3647. PRINTF_FUNC ( "Charger Count => DC-Count = %d, AC-Count = %d \n", ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3648. ShmSysConfigAndInfo->SysConfig.AcConnectorCount );
  3649. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 && ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3650. result = false;
  3651. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3652. {
  3653. bool isFind = false;
  3654. if (chargingInfo [0]->Type == _Type_Chademo)
  3655. {
  3656. if ((bd0_1_status == 0 && bd0_2_status == 1) || (bd1_1_status == 0 && bd1_2_status == 1))
  3657. {
  3658. isFind = true;
  3659. }
  3660. }
  3661. else if (chargingInfo [0]->Type == _Type_CCS)
  3662. {
  3663. if ((bd0_1_status == 1 && bd0_2_status == 0) || (bd1_1_status == 1 && bd1_2_status == 0))
  3664. {
  3665. isFind = true;
  3666. }
  3667. }
  3668. else if (chargingInfo [0]->Type == _Type_GB)
  3669. {
  3670. if ((bd0_1_status == 1 && bd0_2_status == 1) || (bd1_1_status == 1 && bd1_2_status == 1))
  3671. {
  3672. isFind = true;
  3673. }
  3674. }
  3675. if (isFind)
  3676. {
  3677. chargingInfo [0]->Evboard_id = 0x01;
  3678. CheckHwSlotStatusLog ( 0 );
  3679. }
  3680. else
  3681. result = false;
  3682. }
  3683. else
  3684. {
  3685. char type = NO_DEFINE;
  3686. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3687. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex ++)
  3688. {
  3689. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3690. {
  3691. PRINTF_FUNC ( "HW Conn %d (Chademo) \n", gunIndex );
  3692. // 與硬體相同 type : Chademo
  3693. if (chargingInfo [gunIndex]->Type == _Type_Chademo)
  3694. {
  3695. chargingInfo [gunIndex]->Evboard_id = 0x01;
  3696. }
  3697. }
  3698. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3699. {
  3700. PRINTF_FUNC ( "HW Conn %d (CCS) \n", gunIndex );
  3701. // 與硬體相同 type : CCS
  3702. if (chargingInfo [gunIndex]->Type == _Type_CCS)
  3703. {
  3704. chargingInfo [gunIndex]->Evboard_id = 0x01;
  3705. }
  3706. }
  3707. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3708. {
  3709. PRINTF_FUNC ( "HW Conn %d (GB) \n", gunIndex );
  3710. // 與硬體相同 type : GB
  3711. if (chargingInfo [gunIndex]->Type == _Type_GB)
  3712. {
  3713. chargingInfo [gunIndex]->Evboard_id = 0x01;
  3714. }
  3715. }
  3716. else if (gunIndex == 0)
  3717. {
  3718. PRINTF_FUNC ( "HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n", gunIndex, bd0_1_status, bd0_2_status );
  3719. }
  3720. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3721. {
  3722. PRINTF_FUNC ( "HW Conn %d (Chademo) \n", gunIndex );
  3723. // 與硬體相同 type : Chademo
  3724. if (chargingInfo [gunIndex]->Type == _Type_Chademo)
  3725. {
  3726. chargingInfo [gunIndex]->Evboard_id = 0x02;
  3727. }
  3728. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3729. chargingInfo [gunIndex]->Evboard_id = 0x01;
  3730. }
  3731. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3732. {
  3733. PRINTF_FUNC ( "HW Conn %d (CCS) \n", gunIndex );
  3734. // 與硬體相同 type : CCS
  3735. if (chargingInfo [gunIndex]->Type == _Type_CCS)
  3736. {
  3737. chargingInfo [gunIndex]->Evboard_id = 0x02;
  3738. }
  3739. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3740. chargingInfo [gunIndex]->Evboard_id = 0x01;
  3741. }
  3742. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3743. {
  3744. PRINTF_FUNC ( "HW Conn %d (GB) \n", gunIndex );
  3745. // 與硬體相同 type : GB
  3746. if (chargingInfo [gunIndex]->Type == _Type_GB)
  3747. {
  3748. chargingInfo [gunIndex]->Evboard_id = 0x02;
  3749. }
  3750. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3751. chargingInfo [gunIndex]->Evboard_id = 0x01;
  3752. }
  3753. else if (gunIndex == 1)
  3754. {
  3755. PRINTF_FUNC ( "HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n", gunIndex, bd1_1_status, bd1_2_status );
  3756. }
  3757. CheckHwSlotStatusLog ( gunIndex );
  3758. if (type == NO_DEFINE)
  3759. type = chargingInfo [gunIndex]->Type;
  3760. else
  3761. {
  3762. // 雙槍都是同一個 type
  3763. if (type == chargingInfo [gunIndex]->Type)
  3764. ShmDcCommonData->SysGunAreSameType = YES;
  3765. }
  3766. if (chargingInfo [gunIndex]->Evboard_id == 0x00)
  3767. result = false;
  3768. }
  3769. }
  3770. }
  3771. else
  3772. {
  3773. // Module Name 不正確 - 告警
  3774. result = false;
  3775. }
  3776. return result;
  3777. }
  3778. void KillTask()
  3779. {
  3780. ChangeLcmByIndex ( _LCM_FIX );
  3781. system ( "killall Module_EventLogging" );
  3782. system ( "killall Module_PrimaryComm" );
  3783. system ( "killall Module_EvComm" );
  3784. system ( "killall Module_InternalComm" );
  3785. system ( "killall Module_PsuComm" );
  3786. system ( "killall Module_4g &" );
  3787. system ( "killall Module_Wifi &" );
  3788. }
  3789. void KillTaskExceptDetectTask()
  3790. {
  3791. ChangeLcmByIndex ( _LCM_FIX );
  3792. sleep ( 3 );
  3793. system ( "killall Module_EvComm" );
  3794. //system("killall Module_InternalComm");
  3795. system ( "killall Module_PsuComm" );
  3796. system ( "killall Module_4g &" );
  3797. system ( "killall Module_Wifi &" );
  3798. }
  3799. void KillAllTask()
  3800. {
  3801. ChangeLcmByIndex ( _LCM_FIX );
  3802. system ( "killall Module_EventLogging" );
  3803. system ( "killall Module_PrimaryComm" );
  3804. system ( "killall Module_EvComm" );
  3805. system ( "killall Module_LcmControl" );
  3806. system ( "killall Module_InternalComm" );
  3807. system ( "killall Module_PsuComm" );
  3808. system ( "killall OcppBackend &" );
  3809. system ( "killall Module_4g &" );
  3810. system ( "killall Module_Wifi &" );
  3811. system ( "killall Module_DcMeter &" );
  3812. }
  3813. void LogUpgradeType(unsigned int type)
  3814. {
  3815. switch (type)
  3816. {
  3817. case 0x10000001 :
  3818. PRINTF_FUNC ( "CSU bootloader (uboot) \n" );
  3819. break;
  3820. case 0x10000002 :
  3821. PRINTF_FUNC ( "CSU kernel configuration (dtb) \n" );
  3822. break;
  3823. case 0x10000003 :
  3824. PRINTF_FUNC ( "CSU kernel image (zImage) \n" );
  3825. break;
  3826. case 0x10000004 :
  3827. PRINTF_FUNC ( "CSU root file system (ramdisk.gz) \n" );
  3828. break;
  3829. case 0x10000005 :
  3830. PRINTF_FUNC ( "CSU user configuration (bin) \n" );
  3831. break;
  3832. case 0x10000007 :
  3833. PRINTF_FUNC ( "CCS board bootloader (uboot) \n" );
  3834. break;
  3835. case 0x10000008 :
  3836. PRINTF_FUNC ( "CCS board kernel configuration (dtb) \n" );
  3837. break;
  3838. case 0x10000009 :
  3839. PRINTF_FUNC ( "CCS board kernel image (zImage) \n" );
  3840. break;
  3841. case 0x1000000A :
  3842. PRINTF_FUNC ( "CCS board file system (ramdisk.gz) \n" );
  3843. break;
  3844. case 0x10000006 :
  3845. PRINTF_FUNC ( "DCM 407 primary controller \n" );
  3846. break;
  3847. case 0x1000000D :
  3848. PRINTF_FUNC ( "Relay control board \n" );
  3849. break;
  3850. case 0x1000000E :
  3851. PRINTF_FUNC ( "Fan control board \n" );
  3852. break;
  3853. case 0x20000002 :
  3854. PRINTF_FUNC ( "AC wall-mount (low-end) controller \n" );
  3855. break;
  3856. case 0x10000014 :
  3857. PRINTF_FUNC ( "LED control board \n" );
  3858. break;
  3859. case 0x1000000B :
  3860. PRINTF_FUNC ( "CAHdeMO board \n" );
  3861. break;
  3862. case 0x1000000C :
  3863. PRINTF_FUNC ( "GBT board \n" );
  3864. break;
  3865. }
  3866. }
  3867. int CheckUpdateProcess(void)
  3868. {
  3869. //bool isPass = true;
  3870. uint8_t retSucc = 0;
  3871. uint8_t retFail = 0;
  3872. byte index = 0;
  3873. byte target = 0;
  3874. char new_str [256];
  3875. unsigned char *ptr = NULL;
  3876. int fd = 0;
  3877. int CanFd = 0;
  3878. int uartFd = 0;
  3879. unsigned int Type = 0;
  3880. long int MaxLen = 48 * 1024 * 1024 , ImageLen = 0;
  3881. DIR *d;
  3882. struct dirent *dir;
  3883. byte result = PASS;
  3884. char Buf [256];
  3885. d = opendir ( "/mnt/" );
  3886. if (d)
  3887. {
  3888. while ((dir = readdir ( d )) != NULL)
  3889. {
  3890. if (strcmp ( dir->d_name, "." ) == 0 || strcmp ( dir->d_name, ".." ) == 0)
  3891. {
  3892. continue;
  3893. }
  3894. memset ( new_str, 0x00, sizeof(new_str) );
  3895. strcat ( new_str, "/mnt/" );
  3896. strcat ( new_str, dir->d_name );
  3897. PRINTF_FUNC ( "%s \n", new_str );
  3898. fd = open ( new_str, O_RDONLY );
  3899. if (fd < 0)
  3900. {
  3901. close ( fd );
  3902. result = FAIL;
  3903. sleep ( 1 );
  3904. continue;
  3905. }
  3906. ptr = malloc ( MaxLen ); //-48 is take out the header
  3907. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3908. //get the image length
  3909. ImageLen = read ( fd, ptr, MaxLen );
  3910. bool isModelNameFail = false;
  3911. // DS-----J0E-2PH
  3912. if ((ShmSysConfigAndInfo->SysConfig.ModelName [0] != ptr [0])
  3913. || (ShmSysConfigAndInfo->SysConfig.ModelName [1] != ptr [1])
  3914. || (ShmSysConfigAndInfo->SysConfig.ModelName [7] != ptr [7])
  3915. || (ShmSysConfigAndInfo->SysConfig.ModelName [8] != ptr [8])
  3916. || (ShmSysConfigAndInfo->SysConfig.ModelName [9] != ptr [9])
  3917. || (ShmSysConfigAndInfo->SysConfig.ModelName [11] != ptr [11])
  3918. || (ShmSysConfigAndInfo->SysConfig.ModelName [12] != ptr [12])
  3919. || (ShmSysConfigAndInfo->SysConfig.ModelName [13] != ptr [13]))
  3920. {
  3921. result = MODELNAME_FAIL;
  3922. isModelNameFail = true;
  3923. }
  3924. if (isModelNameFail)
  3925. {
  3926. close ( fd );
  3927. continue;
  3928. }
  3929. PRINTF_FUNC ( "model name check pass. \n" );
  3930. if (ImageLen > 20)
  3931. {
  3932. Type = (((unsigned int) ptr [16]) << 24 | ((unsigned int) ptr [17]) << 16 | ((unsigned int) ptr [18]) << 8 | ((unsigned int) ptr [19]));
  3933. LogUpgradeType ( Type );
  3934. PRINTF_FUNC ( "Upgrade Status : PASS : %d, FAIL : %d \n", retSucc, retFail );
  3935. free ( ptr );
  3936. switch (Type)
  3937. {
  3938. case 0x10000001 :
  3939. case 0x10000002 :
  3940. case 0x10000003 :
  3941. case 0x10000004 :
  3942. case 0x10000005 :
  3943. system ( "echo 3 > /proc/sys/vm/drop_caches" );
  3944. sleep ( 2 );
  3945. if (Upgrade_Flash ( Type, new_str, (char *) ShmSysConfigAndInfo->SysConfig.ModelName ) == PASS)
  3946. retSucc ++;
  3947. else
  3948. {
  3949. PRINTF_FUNC ( "Upgrade CSU Failed (%d) \n", Type );
  3950. retFail ++;
  3951. }
  3952. memset ( Buf, 0, sizeof(Buf) );
  3953. sprintf ( Buf, "rm -rvf /mnt/%s", new_str );
  3954. system ( Buf );
  3955. break;
  3956. case 0x10000007 :
  3957. case 0x10000008 :
  3958. case 0x10000009 :
  3959. case 0x1000000A :
  3960. CanFd = InitCanBus ();
  3961. if (CanFd > 0)
  3962. {
  3963. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index ++)
  3964. {
  3965. if (chargingInfo [index]->Type == _Type_CCS)
  3966. {
  3967. byte targetID = chargingInfo [index]->Evboard_id;
  3968. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1
  3969. && ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0)
  3970. {
  3971. targetID += 1;
  3972. }
  3973. system ( "echo 3 > /proc/sys/vm/drop_caches" );
  3974. sleep ( 2 );
  3975. PRINTF_FUNC ( "Upgrade CCS Processing..target id = %d \n", targetID );
  3976. if (Upgrade_CCS ( CanFd, Type, targetID, new_str,
  3977. (char *) ShmSysConfigAndInfo->SysConfig.ModelName ) == FAIL)
  3978. {
  3979. PRINTF_FUNC ( "Upgrade CCS Failed \n" );
  3980. retFail ++;
  3981. }
  3982. else
  3983. retSucc ++;
  3984. }
  3985. }
  3986. close ( CanFd );
  3987. }
  3988. memset ( Buf, 0, sizeof(Buf) );
  3989. sprintf ( Buf, "rm -rvf /mnt/%s", new_str );
  3990. system ( Buf );
  3991. break;
  3992. case 0x10000006 :
  3993. case 0x1000000D :
  3994. case 0x1000000E :
  3995. case 0x20000002 :
  3996. case 0x10000014 :
  3997. // CSU_PRIMARY_CONTROLLER : 0x10000006
  3998. target = 0x00;
  3999. if (Type == 0x10000006)
  4000. {
  4001. target = UPGRADE_PRI;
  4002. }
  4003. else if (Type == 0x1000000D)
  4004. {
  4005. target = UPGRADE_RB;
  4006. }
  4007. else if (Type == 0x1000000E)
  4008. {
  4009. target = UPGRADE_FAN;
  4010. }
  4011. else if (Type == 0x20000002)
  4012. {
  4013. target = UPGRADE_AC;
  4014. }
  4015. else if (Type == 0x10000014)
  4016. {
  4017. target = UPGRADE_LED;
  4018. }
  4019. uartFd = InitComPort ( target );
  4020. if (Upgrade_UART ( uartFd, Type, target, new_str, (char *) ShmSysConfigAndInfo->SysConfig.ModelName ) == PASS)
  4021. {
  4022. retSucc ++;
  4023. }
  4024. else
  4025. {
  4026. PRINTF_FUNC ( "Upgrade %x Failed\r\n", Type );
  4027. retFail ++;
  4028. }
  4029. if (uartFd > 0)
  4030. {
  4031. close ( uartFd );
  4032. }
  4033. memset ( Buf, 0, sizeof(Buf) );
  4034. sprintf ( Buf, "rm -rvf /mnt/%s", new_str );
  4035. system ( Buf );
  4036. break;
  4037. case 0x1000000B :
  4038. case 0x1000000C :
  4039. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  4040. CanFd = InitCanBus ();
  4041. if (CanFd > 0)
  4042. {
  4043. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index ++)
  4044. {
  4045. if ((Type == 0x1000000B && chargingInfo [index]->Type == _Type_Chademo)
  4046. || (Type == 0x1000000C && chargingInfo [index]->Type == _Type_GB))
  4047. {
  4048. if (Upgrade_CAN ( CanFd, Type, chargingInfo [index]->Evboard_id, new_str,
  4049. (char *) ShmSysConfigAndInfo->SysConfig.ModelName ) == PASS)
  4050. retSucc ++;
  4051. else
  4052. {
  4053. if (chargingInfo [index]->Type == _Type_Chademo)
  4054. PRINTF_FUNC ( "Upgrade Chademo Failed \n" );
  4055. else
  4056. PRINTF_FUNC ( "Upgrade GBT Failed \n" );
  4057. retFail ++;
  4058. }
  4059. }
  4060. }
  4061. close ( CanFd );
  4062. }
  4063. memset ( Buf, 0, sizeof(Buf) );
  4064. sprintf ( Buf, "rm -rvf /mnt/%s", new_str );
  4065. system ( Buf );
  4066. break;
  4067. }
  4068. }
  4069. close ( fd );
  4070. }
  4071. }
  4072. free ( dir );
  4073. closedir ( d );
  4074. PRINTF_FUNC ( "Upgrade Result : PASS : %d, FAIL : %d \n", retSucc, retFail );
  4075. sleep ( 1 );
  4076. system ( "rm -rvf /mnt/* " );
  4077. if (retFail != 0)
  4078. {
  4079. result = FAIL;
  4080. }
  4081. return result;
  4082. }
  4083. void CreateRfidFork()
  4084. {
  4085. pid_t rfidRecPid;
  4086. rfidRecPid = fork ();
  4087. if (rfidRecPid == 0)
  4088. {
  4089. while (true)
  4090. {
  4091. // 刷卡判斷
  4092. RFID rfid;
  4093. if ( ! ShmSysConfigAndInfo->SysConfig.isRFID)
  4094. {
  4095. }
  4096. else if (getRequestCardSN ( rfidFd, 0, & rfid ))
  4097. {
  4098. //PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4099. if (strlen ( (char *) ShmSysConfigAndInfo->SysConfig.UserId ) == 0)
  4100. {
  4101. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  4102. {
  4103. switch (rfid.snType)
  4104. {
  4105. case RFID_SN_TYPE_6BYTE :
  4106. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X",
  4107. rfid.currentCard [0], rfid.currentCard [1], rfid.currentCard [2], rfid.currentCard [3],
  4108. rfid.currentCard [4], rfid.currentCard [5] );
  4109. break;
  4110. case RFID_SN_TYPE_7BYTE :
  4111. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X",
  4112. rfid.currentCard [0], rfid.currentCard [1], rfid.currentCard [2], rfid.currentCard [3],
  4113. rfid.currentCard [4], rfid.currentCard [5], rfid.currentCard [6] );
  4114. break;
  4115. case RFID_SN_TYPE_10BYTE :
  4116. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4117. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard [0], rfid.currentCard [1],
  4118. rfid.currentCard [2], rfid.currentCard [3], rfid.currentCard [4], rfid.currentCard [5],
  4119. rfid.currentCard [6], rfid.currentCard [7], rfid.currentCard [8], rfid.currentCard [9] );
  4120. break;
  4121. case RFID_SN_TYPE_4BYTE :
  4122. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X",
  4123. rfid.currentCard [0], rfid.currentCard [1], rfid.currentCard [2], rfid.currentCard [3] );
  4124. break;
  4125. }
  4126. }
  4127. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  4128. {
  4129. switch (rfid.snType)
  4130. {
  4131. case RFID_SN_TYPE_6BYTE :
  4132. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X",
  4133. rfid.currentCard [5], rfid.currentCard [4], rfid.currentCard [3], rfid.currentCard [2],
  4134. rfid.currentCard [1], rfid.currentCard [0] );
  4135. break;
  4136. case RFID_SN_TYPE_7BYTE :
  4137. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X",
  4138. rfid.currentCard [6], rfid.currentCard [5], rfid.currentCard [4], rfid.currentCard [3],
  4139. rfid.currentCard [2], rfid.currentCard [1], rfid.currentCard [0] );
  4140. break;
  4141. case RFID_SN_TYPE_10BYTE :
  4142. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4143. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard [9], rfid.currentCard [8],
  4144. rfid.currentCard [7], rfid.currentCard [6], rfid.currentCard [5], rfid.currentCard [4],
  4145. rfid.currentCard [3], rfid.currentCard [2], rfid.currentCard [1], rfid.currentCard [0] );
  4146. break;
  4147. case RFID_SN_TYPE_4BYTE :
  4148. sprintf ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X",
  4149. rfid.currentCard [3], rfid.currentCard [2], rfid.currentCard [1], rfid.currentCard [0] );
  4150. break;
  4151. }
  4152. }
  4153. PRINTF_FUNC ( "RFID : Get card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId );
  4154. }
  4155. }
  4156. sleep ( 1 );
  4157. }
  4158. }
  4159. }
  4160. void CreateCheckSystemTaskFork()
  4161. {
  4162. pid_t taskPid;
  4163. taskPid = fork ();
  4164. if (taskPid == 0)
  4165. {
  4166. bool stopToDet = false;
  4167. while (true)
  4168. {
  4169. if ( ! stopToDet)
  4170. {
  4171. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  4172. {
  4173. if (chargingInfo [_index]->SystemStatus == SYS_MODE_UPDATE || chargingInfo [_index]->Type == 0x09)
  4174. {
  4175. stopToDet = true;
  4176. continue;
  4177. }
  4178. }
  4179. if ( ! stopToDet)
  4180. CheckSystemTaskAlive ();
  4181. }
  4182. sleep ( 5 );
  4183. }
  4184. }
  4185. }
  4186. void StartSystemTimeoutDet(unsigned char flag)
  4187. {
  4188. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  4189. {
  4190. GetTimespecFunc ( & ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer );
  4191. }
  4192. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  4193. }
  4194. void StopSystemTimeoutDet()
  4195. {
  4196. GetTimespecFunc ( & ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer );
  4197. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  4198. }
  4199. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  4200. {
  4201. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4202. {
  4203. if (chargingInfo [gunIndex]->TimeoutFlag != flag)
  4204. {
  4205. GetTimespecFunc ( & chargingInfo [gunIndex]->ConnectorTimeout );
  4206. }
  4207. chargingInfo [gunIndex]->TimeoutFlag = flag;
  4208. }
  4209. }
  4210. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  4211. {
  4212. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4213. {
  4214. chargingInfo [gunIndex]->TimeoutFlag = Timeout_None;
  4215. }
  4216. }
  4217. void CheckConnectionTimeout()
  4218. {
  4219. if (system ( "pidof -s OcppBackend > /dev/null" ) != 0)
  4220. {
  4221. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4222. }
  4223. else
  4224. {
  4225. _connectionTimeout = _ocpp_get_connect_timeout ();
  4226. if (_connectionTimeout <= 0)
  4227. {
  4228. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4229. }
  4230. }
  4231. }
  4232. void CreateTimeoutFork()
  4233. {
  4234. pid_t timeoutPid;
  4235. timeoutPid = fork ();
  4236. if (timeoutPid > 0)
  4237. {
  4238. GetTimespecFunc ( & _cmdSubPriority_time );
  4239. CheckConnectionTimeout ();
  4240. int _timebuf = 0;
  4241. while (true)
  4242. {
  4243. _timebuf = GetTimeoutValue ( & _cmdSubPriority_time );
  4244. if (_timebuf < 0)
  4245. {
  4246. GetTimespecFunc ( & _cmdSubPriority_time );
  4247. }
  4248. else
  4249. {
  4250. if (_timebuf >= 5)
  4251. {
  4252. CheckConnectionTimeout ();
  4253. GetTimespecFunc ( & _cmdSubPriority_time );
  4254. }
  4255. }
  4256. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  4257. // 系統
  4258. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_None)
  4259. {
  4260. _timebuf = GetTimeoutValue ( & ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer );
  4261. if (_timebuf < 0)
  4262. {
  4263. GetTimespecFunc ( & ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer );
  4264. }
  4265. else
  4266. {
  4267. switch (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  4268. {
  4269. case Timeout_SelftestChk :
  4270. if (_timebuf >= SELFTEST_TIMEOUT)
  4271. {
  4272. _SelfTestTimeout ();
  4273. StopSystemTimeoutDet ();
  4274. }
  4275. break;
  4276. case Timeout_Authorizing :
  4277. if (_timebuf >= AUTHORIZE_TIMEOUT)
  4278. {
  4279. _AuthorizedTimeout ();
  4280. StopSystemTimeoutDet ();
  4281. }
  4282. break;
  4283. case Timeout_VerifyFail :
  4284. if (_timebuf >= AUTHORIZE_FAIL_TIMEOUT)
  4285. {
  4286. _AutoReturnTimeout ();
  4287. StopSystemTimeoutDet ();
  4288. }
  4289. break;
  4290. case Timeout_VerifyComp :
  4291. if (_timebuf >= AUTHORIZE_COMP_TIMEOUT)
  4292. {
  4293. _AutoReturnTimeout ();
  4294. StopSystemTimeoutDet ();
  4295. }
  4296. break;
  4297. case Timeout_WaitPlug :
  4298. if (_timebuf >= _connectionTimeout)
  4299. {
  4300. _DetectPlugInTimeout ();
  4301. StopSystemTimeoutDet ();
  4302. }
  4303. break;
  4304. case Timeout_ReturnToChargingGunDet :
  4305. {
  4306. if (_timebuf >= RETURN_TO_CHARGING_PAGE)
  4307. {
  4308. DisplayChargingInfo ();
  4309. StopSystemTimeoutDet ();
  4310. }
  4311. }
  4312. break;
  4313. // case Timeout_AuthorizingForStop:
  4314. // {
  4315. // if (_timebuf / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4316. // {
  4317. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4318. // ClearAuthorizedFlag();
  4319. // StopSystemTimeoutDet();
  4320. // }
  4321. // }
  4322. // break;
  4323. }
  4324. }
  4325. }
  4326. // 各槍
  4327. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  4328. {
  4329. if (chargingInfo [gun_index]->TimeoutFlag != Timeout_None)
  4330. {
  4331. _timebuf = GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout );
  4332. if (_timebuf < 0)
  4333. {
  4334. GetTimespecFunc ( & chargingInfo [gun_index]->ConnectorTimeout );
  4335. }
  4336. else
  4337. {
  4338. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4339. switch (chargingInfo [gun_index]->TimeoutFlag)
  4340. {
  4341. case Timeout_Preparing :
  4342. {
  4343. if (_timebuf >= GUN_PREPARE_TIMEOUT)
  4344. {
  4345. _PrepareTimeout ( gun_index );
  4346. StopGunInfoTimeoutDet ( gun_index );
  4347. }
  4348. }
  4349. break;
  4350. case Timeout_EvChargingDet :
  4351. {
  4352. if (_timebuf >= GUN_EV_WAIT_TIMEOUT)
  4353. {
  4354. _DetectEvChargingEnableTimeout ( gun_index );
  4355. StopGunInfoTimeoutDet ( gun_index );
  4356. }
  4357. }
  4358. break;
  4359. case Timeout_EvseChargingDet :
  4360. {
  4361. if (chargingInfo [gun_index]->Type == _Type_GB)
  4362. {
  4363. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT * 3)
  4364. {
  4365. _DetectEvseChargingEnableTimeout ( gun_index );
  4366. StopGunInfoTimeoutDet ( gun_index );
  4367. }
  4368. }
  4369. else
  4370. {
  4371. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT)
  4372. {
  4373. _DetectEvseChargingEnableTimeout ( gun_index );
  4374. StopGunInfoTimeoutDet ( gun_index );
  4375. }
  4376. }
  4377. }
  4378. break;
  4379. case Timeout_EvseCompleteDet :
  4380. {
  4381. if (_timebuf >= GUN_COMP_WAIT_TIMEOUT)
  4382. {
  4383. StopGunInfoTimeoutDet ( gun_index );
  4384. }
  4385. }
  4386. break;
  4387. case Timeout_ForCcsPrechargeDet :
  4388. {
  4389. if (_timebuf >= GUN_PRECHARGING_TIMEOUT)
  4390. {
  4391. _CcsPrechargeTimeout ( gun_index );
  4392. StopGunInfoTimeoutDet ( gun_index );
  4393. }
  4394. }
  4395. break;
  4396. }
  4397. }
  4398. }
  4399. }
  4400. sleep ( 1 );
  4401. }
  4402. }
  4403. }
  4404. void GetSystemTime()
  4405. {
  4406. struct timeb csuTime;
  4407. struct tm *tmCSU;
  4408. ftime ( & csuTime );
  4409. tmCSU = localtime ( & csuTime.time );
  4410. PRINTF_FUNC ( "Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900, tmCSU->tm_mon + 1, tmCSU->tm_mday,
  4411. tmCSU->tm_hour, tmCSU->tm_min, tmCSU->tm_sec );
  4412. // byte date[14];
  4413. //
  4414. //
  4415. // //sprintf(&date, "%d", );
  4416. //
  4417. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4418. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4419. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4420. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4421. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4422. //
  4423. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4424. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4425. //
  4426. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4427. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4428. //
  4429. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4430. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4431. //
  4432. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4433. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4434. //
  4435. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4436. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4437. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  4438. // date[4], date[5], date[6], date[7],
  4439. // date[8], date[9], date[10], date[11],
  4440. // date[12], date[13]);
  4441. }
  4442. void CheckFactoryConfigFunction()
  4443. {
  4444. if (ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4445. {
  4446. char Buf [256];
  4447. sleep ( 5 );
  4448. sprintf ( Buf, "cd /root;./FactoryConfig -m %s %s", ShmSysConfigAndInfo->SysConfig.ModelName,
  4449. ShmSysConfigAndInfo->SysConfig.SerialNumber );
  4450. system ( Buf );
  4451. system ( "rm -f /Storage/OCPP/OCPPConfiguration" );
  4452. system ( "sync" );
  4453. sleep ( 5 );
  4454. system ( "reboot -f" );
  4455. sleep ( 5 );
  4456. system ( "reboot -f" );
  4457. }
  4458. }
  4459. void CheckFwUpdateFunction()
  4460. {
  4461. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  4462. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4463. {
  4464. DEBUG_INFO_MSG( "ftp : update start. \n" );
  4465. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  4466. {
  4467. setChargerMode ( gun_index, SYS_MODE_UPDATE );
  4468. }
  4469. CloseWatchDog ();
  4470. sleep ( 2 );
  4471. KillTask ();
  4472. byte updateResult = CheckUpdateProcess ();
  4473. if (updateResult == PASS)
  4474. DEBUG_INFO_MSG( "ftp : update complete. \n" );
  4475. else if (updateResult == MODELNAME_FAIL)
  4476. {
  4477. DEBUG_INFO_MSG( "ftp : model name is none match. \n" );
  4478. KillAllTask ();
  4479. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4480. sleep ( 5 );
  4481. system ( "/usr/bin/run_evse_restart.sh" );
  4482. return;
  4483. }
  4484. else
  4485. DEBUG_INFO_MSG( "ftp : update fail. \n" );
  4486. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4487. sleep ( 5 );
  4488. system ( "reboot -f" );
  4489. }
  4490. else
  4491. {
  4492. ocpp_chk_update_cmd ();
  4493. }
  4494. }
  4495. int TariffParsing(char *StringItem, char *TariffCode)
  4496. {
  4497. char *ptrSave , *ptrToken;
  4498. char strSource [128];
  4499. int fee = 0;
  4500. memcpy ( strSource, StringItem, 128 );
  4501. ptrToken = strtok_r ( strSource, ";", & ptrSave );
  4502. while (ptrToken != NULL)
  4503. {
  4504. char *strTariff = strstr ( ptrToken, TariffCode );
  4505. if (strTariff != NULL)
  4506. {
  4507. char *strFee = strstr ( strTariff, "$" );
  4508. if (strFee != NULL)
  4509. {
  4510. strFee ++;
  4511. float fFee = atof ( strFee );
  4512. fee = (int) (fFee * 100);
  4513. break;
  4514. }
  4515. }
  4516. ptrToken = strtok_r ( NULL, ";", & ptrSave );
  4517. }
  4518. return fee;
  4519. }
  4520. void DefaultPriceHandlerOcpp16(char *StrDefaultPrice)
  4521. {
  4522. unsigned int connectionFee = 0 , currentRate = 0 , occupancyFee = 0;
  4523. PRINTF_FUNC ( "DefaultPriceHandler - StrDefaultPrice = %s \n", StrDefaultPrice );
  4524. connectionFee = TariffParsing ( StrDefaultPrice, "Connection Fee" );
  4525. currentRate = TariffParsing ( StrDefaultPrice, "Current Rate" );
  4526. occupancyFee = TariffParsing ( StrDefaultPrice, "Occupancy Fee" );
  4527. PRINTF_FUNC ( "*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100) );
  4528. PRINTF_FUNC ( "*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100) );
  4529. PRINTF_FUNC ( "*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100) );
  4530. ShmDcCommonData->balanceInfo.defaultPrice = currentRate;
  4531. }
  4532. void UserPriceHandlerOcpp16(char *UserId, char *UserPrice)
  4533. {
  4534. unsigned int connectionFee = 0 , currentRate = 0 , occupancyFee = 0;
  4535. int accountBalance = 0;
  4536. byte acGunIndex = 0;
  4537. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex ++)
  4538. {
  4539. if (gunIndex == 1)
  4540. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4541. else
  4542. acGunIndex = 0;
  4543. if ((chargingInfo [gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  4544. && chargingInfo [gunIndex]->SystemStatus <= SYS_MODE_CHARGING)
  4545. && strcmp ( (char *) chargingInfo [gunIndex]->StartUserId, UserId ) == EQUAL)
  4546. {
  4547. connectionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee" );
  4548. currentRate = TariffParsing ( (char *) ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate" );
  4549. occupancyFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee" );
  4550. accountBalance = TariffParsing ( (char *) ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance" );
  4551. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].UserPrice != currentRate
  4552. || ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].AccountBalance != accountBalance)
  4553. {
  4554. PRINTF_FUNC ( "*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100) );
  4555. PRINTF_FUNC ( "*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100) );
  4556. PRINTF_FUNC ( "*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100) );
  4557. PRINTF_FUNC ( "*** Account Balance: %d.%02d ***", (accountBalance / 100), (accountBalance % 100) );
  4558. PRINTF_FUNC ( "*** Connector Id %d User Price %d.%02d, Account Balance %d.%02d ***", (gunIndex),
  4559. (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100) );
  4560. }
  4561. ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].UserPrice = currentRate;
  4562. ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].AccountBalance = accountBalance;
  4563. }
  4564. }
  4565. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex ++)
  4566. {
  4567. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4568. if (strcmp ( (char *) ac_chargingInfo [gunIndex]->StartUserId, UserId ) == EQUAL)
  4569. {
  4570. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo [acGunIndex].UserPrice != currentRate
  4571. || ShmDcCommonData->balanceInfo.connectorBalanceInfo [acGunIndex].AccountBalance != accountBalance)
  4572. {
  4573. PRINTF_FUNC ( "*** AC Connector User Price %d.%02d, Account Balance %d.%02d ***", (currentRate / 100),
  4574. (currentRate % 100), (accountBalance / 100), (accountBalance % 100) );
  4575. }
  4576. ShmDcCommonData->balanceInfo.connectorBalanceInfo [acGunIndex].UserPrice = currentRate;
  4577. ShmDcCommonData->balanceInfo.connectorBalanceInfo [acGunIndex].AccountBalance = accountBalance;
  4578. }
  4579. }
  4580. }
  4581. void RunningFinalCostHandlerOcpp16()
  4582. {
  4583. unsigned int connectionFee = 0 , sessionFee = 0 , occupancyFee = 0 , totalCost = 0;
  4584. int accountBalance = 0;
  4585. byte acGunIndex = 0;
  4586. bool isFind = false;
  4587. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex ++)
  4588. {
  4589. isFind = false;
  4590. if (gunIndex == 1)
  4591. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4592. else
  4593. acGunIndex = 0;
  4594. // PRINTF_FUNC("*** gunIndex = %d, description = %s *** \n", gunIndex + acGunIndex,
  4595. // ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description);
  4596. // PRINTF_FUNC("*** gunIndex = %d, f description = %s *** \n", gunIndex + acGunIndex,
  4597. // ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description);
  4598. if (chargingInfo [gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  4599. && chargingInfo [gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4600. {
  4601. if (strlen ( (char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description ) > 0)
  4602. {
  4603. connectionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description,
  4604. "Connection Fee" );
  4605. sessionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description,
  4606. "Session Fee" );
  4607. occupancyFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description,
  4608. "Occupancy Fee" );
  4609. totalCost = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description,
  4610. "Total Cost" );
  4611. accountBalance = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description,
  4612. "Account Balance" );
  4613. PRINTF_FUNC ( "*** Connector Id: %d Running Cost *** \n", gunIndex );
  4614. isFind = true;
  4615. memset ( & ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex], 0x00, sizeof(struct StrcutRunningCost) );
  4616. }
  4617. if (strlen ( (char *) ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex].description ) > 0)
  4618. {
  4619. connectionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex].description,
  4620. "Connection Fee" );
  4621. sessionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex].description,
  4622. "Session Fee" );
  4623. occupancyFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex].description,
  4624. "Occupancy Fee" );
  4625. totalCost = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex].description,
  4626. "Total Cost" );
  4627. accountBalance = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex].description,
  4628. "Account Balance" );
  4629. PRINTF_FUNC ( "********** Connector Id: %d Final Cost ********** \n", gunIndex );
  4630. isFind = true;
  4631. memset ( & ShmOCPP16Data->Cost.FinalCost [gunIndex + acGunIndex], 0x00, sizeof(struct StrcutFinalCost) );
  4632. }
  4633. if (isFind)
  4634. {
  4635. PRINTF_FUNC ( "*** Connection Fee: %d.%02d *** \n", (connectionFee / 100), (connectionFee % 100) );
  4636. PRINTF_FUNC ( "*** Session Fee: %d.%02d *** \n", (sessionFee / 100), (sessionFee % 100) );
  4637. PRINTF_FUNC ( "*** Occupancy Fee: %d.%02d *** \n", (occupancyFee / 100), (occupancyFee % 100) );
  4638. PRINTF_FUNC ( "*** Total Cost: %d.%02d *** \n", (totalCost / 100), (totalCost % 100) );
  4639. PRINTF_FUNC ( "*** Account Balance: %d.%02d *** \n", (accountBalance / 100), (accountBalance % 100) );
  4640. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].TotalCost != totalCost
  4641. || ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].AccountBalance != accountBalance)
  4642. {
  4643. PRINTF_FUNC ( "*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d *** \n", (gunIndex),
  4644. (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100) );
  4645. }
  4646. ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].TotalCost = totalCost;
  4647. ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].AccountBalance = accountBalance;
  4648. }
  4649. }
  4650. }
  4651. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex ++)
  4652. {
  4653. isFind = false;
  4654. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4655. if (ac_chargingInfo [gunIndex]->SystemStatus >= SYS_MODE_PREPARING
  4656. && ac_chargingInfo [gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4657. {
  4658. if (strlen ( (char *) ShmOCPP16Data->Cost.RunningCost [acGunIndex].description ) > 0)
  4659. {
  4660. connectionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [acGunIndex].description,
  4661. "Connection Fee" );
  4662. sessionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [acGunIndex].description, "Session Fee" );
  4663. occupancyFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [acGunIndex].description,
  4664. "Occupancy Fee" );
  4665. totalCost = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [acGunIndex].description, "Total Cost" );
  4666. accountBalance = TariffParsing ( (char *) ShmOCPP16Data->Cost.RunningCost [acGunIndex].description,
  4667. "Account Balance" );
  4668. PRINTF_FUNC ( "*** AC Connector : Running Cost ***\n" );
  4669. isFind = true;
  4670. memset ( & ShmOCPP16Data->Cost.RunningCost [acGunIndex], 0x00, sizeof(struct StrcutRunningCost) );
  4671. }
  4672. if (strlen ( (char *) ShmOCPP16Data->Cost.FinalCost [acGunIndex].description ) > 0)
  4673. {
  4674. connectionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [acGunIndex].description,
  4675. "Connection Fee" );
  4676. sessionFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [acGunIndex].description, "Session Fee" );
  4677. occupancyFee = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [acGunIndex].description, "Occupancy Fee" );
  4678. totalCost = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [acGunIndex].description, "Total Cost" );
  4679. accountBalance = TariffParsing ( (char *) ShmOCPP16Data->Cost.FinalCost [acGunIndex].description,
  4680. "Account Balance" );
  4681. PRINTF_FUNC ( "*** AC Connector : Final Cost ***\n" );
  4682. isFind = true;
  4683. memset ( & ShmOCPP16Data->Cost.FinalCost [acGunIndex], 0x00, sizeof(struct StrcutFinalCost) );
  4684. }
  4685. if (isFind)
  4686. {
  4687. PRINTF_FUNC ( "*** Connection Fee: %d.%02d ***\n", (connectionFee / 100), (connectionFee % 100) );
  4688. PRINTF_FUNC ( "*** Session Fee: %d.%02d ***\n", (sessionFee / 100), (sessionFee % 100) );
  4689. PRINTF_FUNC ( "*** Occupancy Fee: %d.%02d ***\n", (occupancyFee / 100), (occupancyFee % 100) );
  4690. PRINTF_FUNC ( "*** Total Cost: %d.%02d ***\n", (totalCost / 100), (totalCost % 100) );
  4691. PRINTF_FUNC ( "*** Account Balance: %d.%02d ***\n", (accountBalance / 100), (accountBalance % 100) );
  4692. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].TotalCost != totalCost
  4693. || ShmDcCommonData->balanceInfo.connectorBalanceInfo [gunIndex + acGunIndex].AccountBalance != accountBalance)
  4694. {
  4695. PRINTF_FUNC ( "*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d ***\n", (gunIndex),
  4696. (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100) );
  4697. }
  4698. ShmDcCommonData->balanceInfo.connectorBalanceInfo [acGunIndex].TotalCost = totalCost;
  4699. ShmDcCommonData->balanceInfo.connectorBalanceInfo [acGunIndex].AccountBalance = accountBalance;
  4700. }
  4701. }
  4702. }
  4703. }
  4704. //===============================================
  4705. // ocpp key
  4706. //===============================================
  4707. bool ocpp_authorizeRemoteTxReqChk_key()
  4708. {
  4709. bool result = false;
  4710. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4711. {
  4712. if (strstr ( (char *) ShmOCPP16Data->ConfigurationTable.CoreProfile [AuthorizeRemoteTxRequests].ItemData, "TRUE" ))
  4713. result = true;
  4714. }
  4715. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4716. {
  4717. }
  4718. return result;
  4719. }
  4720. //===============================================
  4721. // Check reservation date is expired
  4722. //===============================================
  4723. int opcc_chk_reserve_expired(byte gun_index)
  4724. {
  4725. int result = NO;
  4726. byte acDirIndex = 0;
  4727. if (gun_index == 1)
  4728. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4729. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4730. {
  4731. if (chargingInfo [gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4732. PRINTF_FUNC ( "%s \n", ShmOCPP16Data->ReserveNow [gun_index + acDirIndex].ExpiryDate );
  4733. if (CheckTimeOut ( ShmOCPP16Data->ReserveNow [gun_index + acDirIndex].ExpiryDate ))
  4734. result = YES;
  4735. }
  4736. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4737. {
  4738. if (chargingInfo [gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4739. PRINTF_FUNC ( "%s \n", ShmOCPP20Data->ReserveNow [gun_index + acDirIndex].expiryDateTime );
  4740. if (CheckTimeOut ( ShmOCPP20Data->ReserveNow [gun_index + acDirIndex].expiryDateTime ))
  4741. result = YES;
  4742. }
  4743. return result;
  4744. }
  4745. //===============================================
  4746. // AC OCPP routine
  4747. //===============================================
  4748. void ocpp_ac_chk_availability_cmd()
  4749. {
  4750. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4751. byte type = 0;
  4752. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4753. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4754. {
  4755. if (ShmOCPP16Data->CsMsg.bits [hasDc].ChangeAvailabilityReq == YES)
  4756. {
  4757. PRINTF_FUNC ( "***************ChkOcppStatus : AC OcppChangeAvailability******************** \n" );
  4758. DEBUG_ERROR_MSG( "***************ChkOcppStatus : AC OcppChangeAvailability******************** \n" );
  4759. ShmOCPP16Data->CsMsg.bits [hasDc].ChangeAvailabilityReq = NO;
  4760. if (strcmp ( (char *) ShmOCPP16Data->ChangeAvailability [hasDc].Type, "Operative" ) == EQUAL)
  4761. {
  4762. if (isDb_ready)
  4763. DB_Update_Operactive ( localDb, hasDc, true );
  4764. type = 1;
  4765. }
  4766. else if (strcmp ( (char *) ShmOCPP16Data->ChangeAvailability [hasDc].Type, "Inoperative" ) == EQUAL)
  4767. {
  4768. if (isDb_ready)
  4769. DB_Update_Operactive ( localDb, hasDc, false );
  4770. type = 2;
  4771. }
  4772. }
  4773. }
  4774. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4775. {
  4776. if (ShmOCPP20Data->CsMsg.bits [hasDc].ChangeAvailabilityReq == YES)
  4777. {
  4778. PRINTF_FUNC ( "***************ChkOcppStatus : OcppChangeAvailability******************** \n" );
  4779. DEBUG_ERROR_MSG( "***************ChkOcppStatus : OcppChangeAvailability******************** \n" );
  4780. ShmOCPP20Data->CsMsg.bits [hasDc].ChangeAvailabilityReq = NO;
  4781. if (strcmp ( (char *) ShmOCPP20Data->ChangeAvailability [hasDc].operationalStatus, "Operative" ) == EQUAL)
  4782. {
  4783. if (isDb_ready)
  4784. DB_Update_Operactive ( localDb, hasDc, true );
  4785. type = 1;
  4786. }
  4787. else if (strcmp ( (char *) ShmOCPP20Data->ChangeAvailability [hasDc].operationalStatus, "Inoperative" ) == EQUAL)
  4788. {
  4789. if (isDb_ready)
  4790. DB_Update_Operactive ( localDb, hasDc, false );
  4791. type = 2;
  4792. }
  4793. }
  4794. }
  4795. if (type == 1)
  4796. {
  4797. ac_chargingInfo [0]->IsAvailable = YES;
  4798. }
  4799. else if (type == 2)
  4800. {
  4801. ac_chargingInfo [0]->IsAvailable = NO;
  4802. }
  4803. }
  4804. void ocpp_ac_chk_unlock_cmd()
  4805. {
  4806. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4807. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4808. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4809. {
  4810. if (ShmOCPP16Data->CsMsg.bits [hasDc].UnlockConnectorReq == YES)
  4811. {
  4812. ShmOCPP16Data->CsMsg.bits [hasDc].UnlockConnectorReq = NO;
  4813. if (ac_chargingInfo [0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  4814. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_CHARGING)
  4815. {
  4816. // Unlocked - 充電中,需停止充電
  4817. strcpy ( (char *) ShmOCPP16Data->StopTransaction [hasDc].StopReason, "UnlockCommand" );
  4818. ac_chargingInfo [0]->StopChargeFlag = YES;
  4819. }
  4820. strcpy ( (char *) ShmOCPP16Data->UnlockConnector [hasDc].ResponseStatus, "Unlocked" );
  4821. ShmOCPP16Data->CsMsg.bits [hasDc].UnlockConnectorConf = YES;
  4822. }
  4823. }
  4824. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4825. {
  4826. if (ShmOCPP20Data->CsMsg.bits [hasDc].UnlockConnectorReq == YES)
  4827. {
  4828. ShmOCPP20Data->CsMsg.bits [hasDc].UnlockConnectorReq = NO;
  4829. if (ac_chargingInfo [0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  4830. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_CHARGING)
  4831. {
  4832. // Unlocked - 充電中,需停止充電
  4833. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [hasDc].transactionInfo.stoppedReason, "UnlockCommand" );
  4834. ac_chargingInfo [0]->StopChargeFlag = YES;
  4835. }
  4836. strcpy ( (char*) ShmOCPP20Data->UnlockConnector [hasDc].Response_status, "Unlocked" );
  4837. ShmOCPP20Data->CsMsg.bits [hasDc].UnlockConnectorConf = YES;
  4838. }
  4839. }
  4840. }
  4841. bool ocpp_ac_chk_profileConf_cmd()
  4842. {
  4843. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4844. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4845. {
  4846. if (ShmOCPP16Data->CSUMsg.bits [hasDc].ChargingProfileConf == YES)
  4847. {
  4848. ShmOCPP16Data->CSUMsg.bits [hasDc].ChargingProfileConf = NO;
  4849. return true;
  4850. }
  4851. }
  4852. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4853. {
  4854. if (ShmOCPP20Data->CSUMsg.bits [hasDc].ChargingProfileConf == YES)
  4855. {
  4856. ShmOCPP20Data->CSUMsg.bits [hasDc].ChargingProfileConf = NO;
  4857. return true;
  4858. }
  4859. }
  4860. return false;
  4861. }
  4862. void ocpp_ac_chargingProfile_process()
  4863. {
  4864. int _time = 0;
  4865. int _startCount = NO_DEFINE;
  4866. int _maxCount = 0;
  4867. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4868. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4869. {
  4870. if (strcmp ( (char *) ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingProfileKind, "Absolute" ) == EQUAL
  4871. && ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingProfileId == YES)
  4872. {
  4873. _time = GetStartScheduleTime ( ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.StartSchedule );
  4874. _maxCount = ARRAY_SIZE( ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod );
  4875. _startCount = NO_DEFINE;
  4876. for (byte _count = 0; _count < _maxCount; _count ++)
  4877. {
  4878. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4879. if (_time >= ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod [_count].StartPeriod)
  4880. {
  4881. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod [_count].Limit >= MIN_OUTPUT_CUR)
  4882. || ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod [_count].Limit > MIN_OUTPUT_CUR)
  4883. {
  4884. _startCount = _count;
  4885. }
  4886. }
  4887. }
  4888. PRINTF_FUNC ( "AC - Gun_%d, Get Profile - Profile Index = %d \n", hasDc, _startCount );
  4889. if (_startCount < _maxCount)
  4890. {
  4891. PRINTF_FUNC ( "Profile Limit = %.2f \n",
  4892. ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod [_startCount].Limit );
  4893. ac_chargingInfo [0]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod [_startCount].Limit
  4894. * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile [hasDc].ChargingSchedule.ChargingSchedulePeriod [_startCount].NumberPhases;
  4895. if (ac_chargingInfo [0]->EvBatterytargetVoltage > 0 || ac_chargingInfo [0]->PresentChargingVoltage > 0)
  4896. ac_chargingInfo [0]->ChargingProfileCurrent = (ac_chargingInfo [0]->ChargingProfilePower / ac_chargingInfo [0]->PresentChargingVoltage) * 10;
  4897. else
  4898. ac_chargingInfo [0]->ChargingProfileCurrent = 0;
  4899. }
  4900. else
  4901. {
  4902. ac_chargingInfo [0]->ChargingProfilePower = - 1;
  4903. ac_chargingInfo [0]->ChargingProfileCurrent = - 1;
  4904. }
  4905. }
  4906. else
  4907. {
  4908. ac_chargingInfo [0]->ChargingProfilePower = - 1;
  4909. ac_chargingInfo [0]->ChargingProfileCurrent = - 1;
  4910. }
  4911. }
  4912. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4913. {
  4914. if (strcmp ( (char *) ShmOCPP20Data->SmartChargingProfile [hasDc].chargingProfileKind, "Absolute" ) == EQUAL
  4915. && ShmOCPP20Data->SmartChargingProfile [hasDc].id == YES)
  4916. {
  4917. _time = GetStartScheduleTime ( ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].startSchedule );
  4918. _maxCount = ARRAY_SIZE( ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].chargingSchedulePeriod );
  4919. _startCount = NO_DEFINE;
  4920. for (byte _count = 0; _count < _maxCount; _count ++)
  4921. {
  4922. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4923. if (_time >= ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].chargingSchedulePeriod [_count].startPeriod)
  4924. {
  4925. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].chargingSchedulePeriod [_count].limit >= MIN_OUTPUT_CUR)
  4926. || ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].chargingSchedulePeriod [_count].limit > MIN_OUTPUT_CUR)
  4927. {
  4928. _startCount = _count;
  4929. }
  4930. }
  4931. }
  4932. PRINTF_FUNC ( "Gun_%d, Get Profile - Profile Index = %d \n", 1, _startCount );
  4933. if (_startCount < _maxCount)
  4934. {
  4935. PRINTF_FUNC ( "Profile Limit = %.2f \n",
  4936. ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].chargingSchedulePeriod [_startCount].limit );
  4937. ac_chargingInfo [0]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile [hasDc].chargingSchedule [0].chargingSchedulePeriod [_startCount].limit * AC_OUTPUT_VOL;
  4938. if (ac_chargingInfo [0]->EvBatterytargetVoltage > 0 && ac_chargingInfo [0]->PresentChargingVoltage > 0)
  4939. ac_chargingInfo [0]->ChargingProfileCurrent = (ac_chargingInfo [0]->ChargingProfilePower / ac_chargingInfo [0]->PresentChargingVoltage) * 10;
  4940. else
  4941. ac_chargingInfo [0]->ChargingProfileCurrent = 0;
  4942. }
  4943. else
  4944. {
  4945. ac_chargingInfo [0]->ChargingProfilePower = - 1;
  4946. ac_chargingInfo [0]->ChargingProfileCurrent = - 1;
  4947. }
  4948. }
  4949. else
  4950. {
  4951. ac_chargingInfo [0]->ChargingProfilePower = - 1;
  4952. ac_chargingInfo [0]->ChargingProfileCurrent = - 1;
  4953. }
  4954. }
  4955. else
  4956. {
  4957. ac_chargingInfo [0]->ChargingProfilePower = - 1;
  4958. ac_chargingInfo [0]->ChargingProfileCurrent = - 1;
  4959. }
  4960. if (ac_chargingInfo [0]->ChargingProfilePower > 0 &&
  4961. _AcChargingProfilePwBuffer != chargingInfo [0]->ChargingProfilePower)
  4962. {
  4963. _AcChargingProfilePwBuffer = chargingInfo [0]->ChargingProfilePower;
  4964. PRINTF_FUNC ( "Profile : ChargingProfilePower = %.2f KW \n", ac_chargingInfo [0]->ChargingProfilePower / 1000 );
  4965. PRINTF_FUNC ( "Profile : ChargingProfileCurrent = %.2f \n", ac_chargingInfo [0]->ChargingProfileCurrent / 10 );
  4966. }
  4967. }
  4968. void CheckAcSmartChargeProfile()
  4969. {
  4970. if (ocpp_ac_chk_profileConf_cmd ())
  4971. {
  4972. ocpp_ac_chargingProfile_process ();
  4973. }
  4974. }
  4975. void ocpp_ac_lift_profileReq_cmd(byte gunIndex)
  4976. {
  4977. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4978. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4979. {
  4980. if (ShmOCPP16Data->CSUMsg.bits [hasDc].ChargingProfileConf == NO)
  4981. {
  4982. if (ShmOCPP16Data->CSUMsg.bits [hasDc].ChargingProfileReq == NO)
  4983. ShmOCPP16Data->CSUMsg.bits [hasDc].ChargingProfileReq = YES;
  4984. }
  4985. }
  4986. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4987. {
  4988. if (ShmOCPP20Data->CSUMsg.bits [hasDc].ChargingProfileConf == NO)
  4989. {
  4990. if (ShmOCPP20Data->CSUMsg.bits [hasDc].ChargingProfileReq == NO)
  4991. ShmOCPP20Data->CSUMsg.bits [hasDc].ChargingProfileReq = YES;
  4992. }
  4993. }
  4994. }
  4995. //===============================================
  4996. // OCPP routine
  4997. //===============================================
  4998. void ocpp_auto_response_BootNotification()
  4999. {
  5000. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5001. {
  5002. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  5003. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  5004. }
  5005. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5006. {
  5007. if (ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES)
  5008. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = NO;
  5009. }
  5010. }
  5011. bool ocpp_is_resPass_StartTransactionConf(byte gun_index)
  5012. {
  5013. bool result = false;
  5014. byte acDirIndex = 0;
  5015. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5016. if (gun_index == 1)
  5017. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5018. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5019. {
  5020. result = ShmOCPP16Data->CpMsg.bits [gun_index + acDirIndex].StartTransactionConf;
  5021. }
  5022. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5023. {
  5024. result = ShmOCPP20Data->CpMsg.bits [gun_index + acDirIndex].TransactionEventConf;
  5025. }
  5026. return result;
  5027. }
  5028. void ocpp_auto_response_StartTransationConf(byte gun_index)
  5029. {
  5030. byte acDirIndex = 0;
  5031. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5032. if (gun_index == 1)
  5033. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5034. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5035. {
  5036. if (ShmOCPP16Data->CpMsg.bits [gun_index + acDirIndex].StartTransactionConf)
  5037. ShmOCPP16Data->CpMsg.bits [gun_index + acDirIndex].StartTransactionConf = NO;
  5038. }
  5039. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5040. {
  5041. if (ShmOCPP20Data->CpMsg.bits [gun_index + acDirIndex].TransactionEventConf)
  5042. ShmOCPP20Data->CpMsg.bits [gun_index + acDirIndex].TransactionEventConf = NO;
  5043. }
  5044. }
  5045. void ocpp_auto_response_ReserveConf(byte gun_index)
  5046. {
  5047. byte acDirIndex = 0;
  5048. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5049. if (gun_index == 1)
  5050. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5051. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5052. ShmOCPP16Data->CsMsg.bits [gun_index + acDirIndex].ReserveNowConf = YES;
  5053. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5054. ShmOCPP20Data->CsMsg.bits [gun_index + acDirIndex].ReserveNowConf = YES;
  5055. }
  5056. bool ocpp_chk_authrization_cmd()
  5057. {
  5058. char buf2 [16] = "";
  5059. memset ( buf2, 0, ARRAY_SIZE( buf2 ) );
  5060. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5061. sprintf ( buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status );
  5062. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5063. sprintf ( buf2, "%s", ShmOCPP20Data->Authorize.Response_idTokenInfo.status );
  5064. // 因為無法得知實際的長度,所以只能用搜尋的方式
  5065. if (strcmp ( buf2, "Accepted" ) == EQUAL)
  5066. return true;
  5067. return false;
  5068. }
  5069. bool opcc_sub_get_reset_req()
  5070. {
  5071. bool result = false;
  5072. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5073. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  5074. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5075. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  5076. return result;
  5077. }
  5078. void ocpp_sub_run_reset(bool canReset)
  5079. {
  5080. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5081. {
  5082. if (canReset && strcmp ( (char *) ShmOCPP16Data->Reset.Type, "Hard" ) == EQUAL)
  5083. {
  5084. DEBUG_ERROR_MSG( "****** Hard Reboot ****** \n" );
  5085. sprintf ( (char*) ShmOCPP16Data->Reset.ResponseStatus, "Accepted" );
  5086. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5087. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5088. sleep ( 3 );
  5089. system ( "reboot -f" );
  5090. }
  5091. else if (canReset && strcmp ( (char *) ShmOCPP16Data->Reset.Type, "Soft" ) == EQUAL)
  5092. {
  5093. DEBUG_ERROR_MSG( "****** Soft Reboot ****** \n" );
  5094. sprintf ( (char*) ShmOCPP16Data->Reset.ResponseStatus, "Accepted" );
  5095. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5096. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5097. sleep ( 3 );
  5098. KillAllTask ();
  5099. CloseWatchDog ();
  5100. system ( "/usr/bin/run_evse_restart.sh" );
  5101. }
  5102. }
  5103. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5104. {
  5105. if (canReset && strcmp ( (char *) ShmOCPP20Data->Reset.type, "Immediate" ) == EQUAL)
  5106. {
  5107. DEBUG_ERROR_MSG( "****** Hard Reboot ****** \n" );
  5108. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  5109. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5110. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5111. sleep ( 3 );
  5112. system ( "reboot -f" );
  5113. }
  5114. else if (canReset && strcmp ( (char *) ShmOCPP20Data->Reset.type, "OnIdle" ) == EQUAL)
  5115. {
  5116. DEBUG_ERROR_MSG( "****** Soft Reboot ****** \n" );
  5117. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  5118. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5119. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5120. sleep ( 3 );
  5121. KillAllTask ();
  5122. CloseWatchDog ();
  5123. system ( "/usr/bin/run_evse_restart.sh" );
  5124. }
  5125. }
  5126. }
  5127. void ocpp_set_authorizeReq_cmd(byte value)
  5128. {
  5129. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5130. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = value;
  5131. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5132. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = value;
  5133. }
  5134. void ocpp_chk_reset_cmd(byte isforce)
  5135. {
  5136. if (opcc_sub_get_reset_req ())
  5137. {
  5138. bool canReset = true;
  5139. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  5140. {
  5141. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index ++)
  5142. {
  5143. if (ac_chargingInfo [0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  5144. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_CHARGING)
  5145. {
  5146. // Reset - AC 先停止充電
  5147. ac_chargingInfo [0]->StopChargeFlag = YES;
  5148. GetTimespecFunc ( & _resetChkTime );
  5149. }
  5150. }
  5151. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  5152. {
  5153. if (chargingInfo [_index]->SystemStatus >= SYS_MODE_REASSIGN
  5154. && chargingInfo [_index]->SystemStatus <= SYS_MODE_TERMINATING)
  5155. {
  5156. canReset = false;
  5157. // Reset 系統先停止充電
  5158. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5159. {
  5160. if (strcmp ( (char *) ShmOCPP16Data->Reset.Type, "Hard" ) == EQUAL)
  5161. ocpp_set_stopReason_by_cmd ( _index, "HardReset" );
  5162. else
  5163. ocpp_set_stopReason_by_cmd ( _index, "SoftReset" );
  5164. }
  5165. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5166. {
  5167. if (strcmp ( (char *) ShmOCPP20Data->Reset.type, "Immediate" ) == EQUAL)
  5168. ocpp_set_stopReason_by_cmd ( _index, "HardReset" );
  5169. else
  5170. ocpp_set_stopReason_by_cmd ( _index, "SoftReset" );
  5171. }
  5172. ChargingTerminalProcess ( _index );
  5173. GetTimespecFunc ( & _resetChkTime );
  5174. }
  5175. }
  5176. }
  5177. if (canReset)
  5178. {
  5179. if (GetTimeoutValue ( & _resetChkTime ) < 0)
  5180. GetTimespecFunc ( & _resetChkTime );
  5181. if (GetTimeoutValue ( & _resetChkTime ) >= 5)
  5182. {
  5183. ShmDcCommonData->evBoardResetFlag = YES;
  5184. // Reset -> change status to maintain.
  5185. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  5186. {
  5187. setChargerMode ( _index, SYS_MODE_MAINTAIN );
  5188. }
  5189. ChangeLcmByIndex ( _LCM_FIX );
  5190. }
  5191. else
  5192. canReset = false;
  5193. }
  5194. else
  5195. GetTimespecFunc ( & _resetChkTime );
  5196. if (isforce == YES)
  5197. canReset = true;
  5198. ocpp_sub_run_reset ( canReset );
  5199. }
  5200. }
  5201. void ocpp_chk_reserved_cmd(byte gunIndex)
  5202. {
  5203. byte acDirIndex = 0;
  5204. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5205. if (gunIndex == 1)
  5206. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5207. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5208. {
  5209. if (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_IDLE
  5210. && ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].ReserveNowReq == YES)
  5211. {
  5212. PRINTF_FUNC ( "***************ChkOcppStatus : OcppReservedStatus******************** \n" );
  5213. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].ReserveNowReq = NO;
  5214. if ( ! opcc_chk_reserve_expired ( gunIndex ))
  5215. {
  5216. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [gunIndex + acDirIndex].ReservationId );
  5217. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP16Data->ReserveNow [gunIndex + acDirIndex].IdTag );
  5218. PRINTF_FUNC ( "*************ChkOcppStatus : OcppReservedStatus End****************** \n" );
  5219. chargingInfo [gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow [gunIndex].ReservationId;
  5220. chargingInfo [gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5221. strcpy ( (char *) ShmDcCommonData->_reserved_UserId [gunIndex],
  5222. (char *) ShmOCPP16Data->ReserveNow [gunIndex + acDirIndex].IdTag );
  5223. }
  5224. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].ReserveNowConf = YES;
  5225. }
  5226. if (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_RESERVATION
  5227. && ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].CancelReservationReq == YES)
  5228. {
  5229. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].CancelReservationReq = NO;
  5230. if (ShmOCPP16Data->ReserveNow [gunIndex + acDirIndex].ReservationId == chargingInfo [gunIndex]->ReservationId)
  5231. {
  5232. PRINTF_FUNC ( "***************ChkOcppStatus : OcppReservedStatus******************** \n" );
  5233. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [gunIndex + acDirIndex].ReservationId );
  5234. PRINTF_FUNC ( "***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n" );
  5235. chargingInfo [gunIndex]->ReservationId = 0;
  5236. chargingInfo [gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5237. strcpy ( (char *) ShmDcCommonData->_reserved_UserId [gunIndex], "" );
  5238. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].CancelReservationConf = YES;
  5239. }
  5240. }
  5241. }
  5242. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5243. {
  5244. if (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_IDLE
  5245. && ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].ReserveNowReq == YES)
  5246. {
  5247. PRINTF_FUNC ( "***************ChkOcppStatus : OcppReservedStatus******************** \n" );
  5248. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].ReserveNowReq = NO;
  5249. if ( ! opcc_chk_reserve_expired ( gunIndex ))
  5250. {
  5251. PRINTF_FUNC ( "id = %d \n", ShmOCPP20Data->ReserveNow [gunIndex + acDirIndex].id );
  5252. PRINTF_FUNC ( "idToken = %s \n", ShmOCPP20Data->ReserveNow [gunIndex + acDirIndex].idToken.idToken );
  5253. PRINTF_FUNC ( "*************ChkOcppStatus : OcppReservedStatus End****************** \n" );
  5254. chargingInfo [gunIndex]->ReservationId = ShmOCPP20Data->ReserveNow [gunIndex].id;
  5255. chargingInfo [gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5256. strncpy ( (char *) ShmDcCommonData->_reserved_UserId [gunIndex],
  5257. (char *) ShmOCPP20Data->ReserveNow [gunIndex + acDirIndex].idToken.idToken, 30 );
  5258. }
  5259. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].ReserveNowConf = YES;
  5260. }
  5261. if (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_RESERVATION
  5262. && ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].CancelReservationReq == YES)
  5263. {
  5264. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].CancelReservationReq = NO;
  5265. if (ShmOCPP20Data->ReserveNow [gunIndex + acDirIndex].id == chargingInfo [gunIndex]->ReservationId)
  5266. {
  5267. PRINTF_FUNC ( "***************ChkOcppStatus : OcppReservedStatus******************** \n" );
  5268. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP20Data->ReserveNow [gunIndex + acDirIndex].id );
  5269. PRINTF_FUNC ( "***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n" );
  5270. chargingInfo [gunIndex]->ReservationId = 0;
  5271. chargingInfo [gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5272. strcpy ( (char *) ShmDcCommonData->_reserved_UserId [gunIndex], "" );
  5273. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].CancelReservationConf = YES;
  5274. }
  5275. }
  5276. }
  5277. }
  5278. void ocpp_chk_availability_cmd(byte gunIndex)
  5279. {
  5280. byte type = 0; // 0 : none, 1 : operative, 2 inoperative
  5281. byte acDirIndex = 0;
  5282. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5283. if (gunIndex == 1)
  5284. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5285. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5286. {
  5287. if (ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5288. {
  5289. PRINTF_FUNC ( "***************ChkOcppStatus : OcppChangeAvailability******************** \n" );
  5290. DEBUG_ERROR_MSG( "***************ChkOcppStatus : OcppChangeAvailability******************** \n" );
  5291. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5292. if (strcmp ( (char *) ShmOCPP16Data->ChangeAvailability [gunIndex + acDirIndex].Type, "Operative" ) == EQUAL)
  5293. {
  5294. if (isDb_ready)
  5295. DB_Update_Operactive ( localDb, gunIndex + acDirIndex, true );
  5296. type = 1;
  5297. }
  5298. else if (strcmp ( (char *) ShmOCPP16Data->ChangeAvailability [gunIndex + acDirIndex].Type, "Inoperative" ) == EQUAL)
  5299. {
  5300. if (isDb_ready)
  5301. DB_Update_Operactive ( localDb, gunIndex + acDirIndex, false );
  5302. type = 2;
  5303. }
  5304. }
  5305. }
  5306. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5307. {
  5308. if (ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5309. {
  5310. PRINTF_FUNC ( "***************ChkOcppStatus : OcppChangeAvailability******************** \n" );
  5311. DEBUG_ERROR_MSG( "***************ChkOcppStatus : OcppChangeAvailability******************** \n" );
  5312. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5313. if (strcmp ( (char *) ShmOCPP20Data->ChangeAvailability [gunIndex + acDirIndex].operationalStatus, "Operative" ) == EQUAL)
  5314. {
  5315. if (isDb_ready)
  5316. DB_Update_Operactive ( localDb, gunIndex + acDirIndex, true );
  5317. type = 1;
  5318. }
  5319. else if (strcmp ( (char *) ShmOCPP20Data->ChangeAvailability [gunIndex + acDirIndex].operationalStatus,
  5320. "Inoperative" ) == EQUAL)
  5321. {
  5322. if (isDb_ready)
  5323. DB_Update_Operactive ( localDb, gunIndex + acDirIndex, false );
  5324. type = 2;
  5325. }
  5326. }
  5327. }
  5328. if (type == 1)
  5329. {
  5330. chargingInfo [gunIndex]->IsAvailable = YES;
  5331. if (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_IDLE
  5332. || chargingInfo [gunIndex]->SystemStatus == SYS_MODE_RESERVATION
  5333. || chargingInfo [gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5334. {
  5335. setChargerMode ( gunIndex, SYS_MODE_IDLE );
  5336. }
  5337. }
  5338. else if (type == 2)
  5339. {
  5340. chargingInfo [gunIndex]->IsAvailable = NO;
  5341. if (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_IDLE
  5342. || chargingInfo [gunIndex]->SystemStatus == SYS_MODE_RESERVATION
  5343. || chargingInfo [gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5344. {
  5345. setChargerMode ( gunIndex, SYS_MODE_MAINTAIN );
  5346. }
  5347. }
  5348. }
  5349. void ocpp_chk_unlock_cmd(byte gunIndex)
  5350. {
  5351. byte acDirIndex = 0;
  5352. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5353. if (gunIndex == 1)
  5354. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5355. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5356. {
  5357. if (ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5358. {
  5359. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5360. if (chargingInfo [gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  5361. && chargingInfo [gunIndex]->SystemStatus <= SYS_MODE_CHARGING)
  5362. {
  5363. // Unlocked - 充電中,需停止充電
  5364. strcpy ( (char *) ShmOCPP16Data->StopTransaction [gunIndex + acDirIndex].StopReason, "UnlockCommand" );
  5365. ChargingTerminalProcess ( gunIndex );
  5366. }
  5367. if (chargingInfo [gunIndex]->Type == _Type_Chademo || chargingInfo [gunIndex]->Type == _Type_GB)
  5368. strcpy ( (char *) ShmOCPP16Data->UnlockConnector [gunIndex + acDirIndex].ResponseStatus, "UnlockCommand" );
  5369. else
  5370. strcpy ( (char *) ShmOCPP16Data->UnlockConnector [gunIndex + acDirIndex].ResponseStatus, "UnlockCommand" );
  5371. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5372. }
  5373. }
  5374. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5375. {
  5376. if (ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5377. {
  5378. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5379. if (chargingInfo [gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  5380. && chargingInfo [gunIndex]->SystemStatus <= SYS_MODE_CHARGING)
  5381. {
  5382. // Unlocked - 充電中,需停止充電
  5383. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].transactionInfo.stoppedReason,
  5384. "UnlockCommand" );
  5385. ChargingTerminalProcess ( gunIndex );
  5386. }
  5387. if (chargingInfo [gunIndex]->Type == _Type_Chademo || chargingInfo [gunIndex]->Type == _Type_GB)
  5388. strcpy ( (char*) ShmOCPP20Data->UnlockConnector [gunIndex + acDirIndex].Response_status, "UnlockCommand" );
  5389. else
  5390. strcpy ( (char *) ShmOCPP16Data->UnlockConnector [gunIndex + acDirIndex].ResponseStatus, "UnlockCommand" );
  5391. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5392. }
  5393. }
  5394. }
  5395. bool ocpp_chk_remoteStop_cmd(byte gunIndex)
  5396. {
  5397. bool result = false;
  5398. byte acDirIndex = 0;
  5399. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5400. if (gunIndex == 1)
  5401. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5402. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5403. result = ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].RemoteStopTransactionReq;
  5404. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5405. result = ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].RequestStopTransactionReq;
  5406. if (result)
  5407. {
  5408. DEBUG_INFO_MSG( "Remote Stop by OCPP (%d) \n", gunIndex + acDirIndex );
  5409. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5410. {
  5411. strcpy ( (char *) ShmOCPP16Data->StopTransaction [gunIndex + acDirIndex].StopReason, "Remote" );
  5412. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].RemoteStopTransactionReq = NO;
  5413. }
  5414. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5415. {
  5416. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].transactionInfo.stoppedReason, "Remote" );
  5417. ShmOCPP20Data->CsMsg.bits [gunIndex + acDirIndex].RequestStopTransactionReq = NO;
  5418. }
  5419. }
  5420. return result;
  5421. }
  5422. bool ocpp_sub_get_remote_start_transaction_req(byte gun_index)
  5423. {
  5424. bool result = false;
  5425. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5426. {
  5427. result = ShmOCPP16Data->CsMsg.bits [gun_index].RemoteStartTransactionReq;
  5428. }
  5429. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5430. {
  5431. result = ShmOCPP20Data->CsMsg.bits [gun_index].RequestStartTransactionReq;
  5432. }
  5433. if (result && ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5434. {
  5435. strcpy ( (char *) ShmOCPP16Data->StartTransaction [gun_index].IdTag,
  5436. (char *) ShmOCPP16Data->RemoteStartTransaction [gun_index].IdTag );
  5437. }
  5438. return result;
  5439. }
  5440. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result)
  5441. {
  5442. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5443. ShmOCPP16Data->CsMsg.bits [gunIndex].RemoteStartTransactionReq = result;
  5444. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5445. ShmOCPP20Data->CsMsg.bits [gunIndex].RequestStartTransactionReq = result;
  5446. }
  5447. void ocpp_chk_remoteStart_cmd()
  5448. {
  5449. if ( ! isDetectPlugin ())
  5450. {
  5451. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  5452. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5453. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index ++)
  5454. {
  5455. if (ocpp_sub_get_remote_start_transaction_req ( acDirIndex ))
  5456. {
  5457. if (ac_chargingInfo [ac_index]->SystemStatus == SYS_MODE_IDLE
  5458. || ac_chargingInfo [ac_index]->SystemStatus == SYS_MODE_RESERVATION)
  5459. {
  5460. ac_chargingInfo [ac_index]->RemoteStartFlag = YES;
  5461. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5462. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  5463. //ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  5464. DetectPluginStart ();
  5465. return;
  5466. }
  5467. ocpp_sub_set_remote_start_transaction_req ( acDirIndex, NO );
  5468. }
  5469. }
  5470. byte threeGunIndex = 0;
  5471. byte dcIndex = 0;
  5472. bool isGunUsingStatus = false;
  5473. byte scheduleIndex = 0;
  5474. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  5475. {
  5476. // 如果有 AC 槍,且 DC 槍也有兩把
  5477. if (acDirIndex == 1 && _index == 1)
  5478. threeGunIndex = 1;
  5479. if (ocpp_sub_get_remote_start_transaction_req ( _index + threeGunIndex ))
  5480. dcIndex = _index;
  5481. if (chargingInfo [_index]->SystemStatus != SYS_MODE_IDLE)
  5482. {
  5483. isGunUsingStatus = true;
  5484. }
  5485. if (chargingInfo [_index]->schedule.isTriggerStart)
  5486. scheduleIndex = _index;
  5487. }
  5488. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  5489. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  5490. {
  5491. if (dcIndex == 0)
  5492. threeGunIndex = 0;
  5493. ocpp_sub_set_remote_start_transaction_req ( dcIndex + threeGunIndex, NO );
  5494. return;
  5495. }
  5496. if (dcIndex == 0)
  5497. threeGunIndex = 0;
  5498. if (ocpp_sub_get_remote_start_transaction_req ( dcIndex + threeGunIndex ))
  5499. {
  5500. if (chargingInfo [dcIndex]->SystemStatus == SYS_MODE_IDLE
  5501. || chargingInfo [dcIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5502. {
  5503. if (ocpp_authorizeRemoteTxReqChk_key ())
  5504. {
  5505. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  5506. (char *) ShmOCPP16Data->RemoteStartTransaction [dcIndex + threeGunIndex].IdTag );
  5507. }
  5508. else
  5509. {
  5510. chargingInfo [dcIndex]->RemoteStartFlag = YES;
  5511. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5512. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  5513. DetectPluginStart ();
  5514. }
  5515. }
  5516. ocpp_sub_set_remote_start_transaction_req ( dcIndex + threeGunIndex, NO );
  5517. }
  5518. else if (chargingInfo [scheduleIndex]->schedule.isTriggerStart)
  5519. {
  5520. if (chargingInfo [scheduleIndex]->SystemStatus == SYS_MODE_IDLE
  5521. || chargingInfo [scheduleIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5522. {
  5523. chargingInfo [scheduleIndex]->RemoteStartFlag = YES;
  5524. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5525. DetectPluginStart ();
  5526. }
  5527. chargingInfo [scheduleIndex]->schedule.isTriggerStart = NO;
  5528. }
  5529. }
  5530. }
  5531. void ocpp_chk_update_cmd()
  5532. {
  5533. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5534. {
  5535. if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5536. {
  5537. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5538. if (strcmp ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded" ) == EQUAL)
  5539. {
  5540. DEBUG_INFO_MSG( "Backend : update start. \n" );
  5541. sleep ( 2 );
  5542. strcpy ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "" );
  5543. strcpy ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing" );
  5544. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5545. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  5546. {
  5547. setChargerMode ( _index, SYS_MODE_UPDATE );
  5548. }
  5549. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index ++)
  5550. {
  5551. ac_chargingInfo [_index]->SystemStatus = SYS_MODE_UPDATE;
  5552. }
  5553. CloseWatchDog ();
  5554. sleep ( 2 );
  5555. KillTask ();
  5556. byte updateResult = CheckUpdateProcess ();
  5557. if (updateResult == PASS)
  5558. {
  5559. DEBUG_INFO_MSG( "Backend : update complete. \n" );
  5560. strcpy ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed" );
  5561. }
  5562. else if (updateResult == MODELNAME_FAIL)
  5563. {
  5564. DEBUG_INFO_MSG( "Backend : model name is none match. \n" );
  5565. strcpy ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed" );
  5566. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5567. sleep ( 5 );
  5568. KillAllTask ();
  5569. system ( "/usr/bin/run_evse_restart.sh" );
  5570. return;
  5571. }
  5572. else
  5573. {
  5574. DEBUG_INFO_MSG( "Backend : update fail. \n" );
  5575. strcpy ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed" );
  5576. }
  5577. strcpy ( (char *) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed" );
  5578. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5579. sleep ( 5 );
  5580. system ( "reboot -f" );
  5581. }
  5582. }
  5583. }
  5584. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5585. {
  5586. if (ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5587. {
  5588. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5589. if (strcmp ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded" ) == EQUAL)
  5590. {
  5591. DEBUG_INFO_MSG( "Backend : update start. \n" );
  5592. sleep ( 2 );
  5593. strcpy ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "" );
  5594. strcpy ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "Installing" );
  5595. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5596. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  5597. {
  5598. setChargerMode ( _index, SYS_MODE_UPDATE );
  5599. }
  5600. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index ++)
  5601. {
  5602. ac_chargingInfo [_index]->SystemStatus = SYS_MODE_UPDATE;
  5603. }
  5604. CloseWatchDog ();
  5605. sleep ( 2 );
  5606. KillTask ();
  5607. byte updateResult = CheckUpdateProcess ();
  5608. if (updateResult == PASS)
  5609. {
  5610. DEBUG_INFO_MSG( "Backend : update complete. \n" );
  5611. strcpy ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "Installed" );
  5612. }
  5613. else if (updateResult == MODELNAME_FAIL)
  5614. {
  5615. DEBUG_INFO_MSG( "Backend : model name is none match. \n" );
  5616. strcpy ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed" );
  5617. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5618. sleep ( 5 );
  5619. KillAllTask ();
  5620. system ( "/usr/bin/run_evse_restart.sh" );
  5621. return;
  5622. }
  5623. else
  5624. {
  5625. DEBUG_INFO_MSG( "Backend : update fail. \n" );
  5626. strcpy ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed" );
  5627. }
  5628. strcpy ( (char *) ShmOCPP20Data->FirmwareStatusNotification.status, "Installed" );
  5629. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5630. sleep ( 5 );
  5631. system ( "reboot -f" );
  5632. }
  5633. }
  5634. }
  5635. }
  5636. bool ocpp_chk_invalid_id_cmd(byte gunIndex)
  5637. {
  5638. byte acDirIndex = 0;
  5639. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5640. if (gunIndex == 1)
  5641. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5642. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5643. {
  5644. if (strstr ( (char *) ShmOCPP16Data->ConfigurationTable.CoreProfile [StopTransactionOnInvalidId].ItemData, "TRUE" ))
  5645. {
  5646. if (strcmp ( (char *) ShmOCPP16Data->StartTransaction [gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Blocked" ) == EQUAL
  5647. || strcmp ( (char *) ShmOCPP16Data->StartTransaction [gunIndex + acDirIndex].ResponseIdTagInfo.Status,
  5648. "Expired" ) == EQUAL
  5649. || strcmp ( (char *) ShmOCPP16Data->StartTransaction [gunIndex + acDirIndex].ResponseIdTagInfo.Status,
  5650. "Invalid" ) == EQUAL)
  5651. return true;
  5652. }
  5653. }
  5654. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5655. {
  5656. if (strstr ( (char *) ShmOCPP20Data->ControllerComponentVariable [TxCtrlr_StopTxOnInvalidId].variableAttribute [0].value,
  5657. "TRUE" ))
  5658. {
  5659. if (strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status, "Blocked" ) == EQUAL
  5660. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5661. "Expired" ) == EQUAL
  5662. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5663. "Invalid" ) == EQUAL
  5664. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5665. "NoCredit" ) == EQUAL
  5666. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5667. "NotAllowedTypeEVSE" ) == EQUAL
  5668. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5669. "NotAtThisLocation" ) == EQUAL
  5670. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5671. "NotAtThisTime" ) == EQUAL
  5672. || strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].Response_idTokenInfo.status,
  5673. "Unknown" ) == EQUAL)
  5674. return true;
  5675. }
  5676. }
  5677. return false;
  5678. }
  5679. bool ocpp_chk_profileConf_cmd(byte gunIndex)
  5680. {
  5681. byte acDirIndex = 0;
  5682. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5683. if (gunIndex == 1)
  5684. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5685. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5686. {
  5687. if (ShmOCPP16Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileConf == YES)
  5688. {
  5689. ShmOCPP16Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileConf = NO;
  5690. return true;
  5691. }
  5692. }
  5693. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5694. {
  5695. if (ShmOCPP20Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileConf == YES)
  5696. {
  5697. ShmOCPP20Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileConf = NO;
  5698. return true;
  5699. }
  5700. }
  5701. return false;
  5702. }
  5703. //LWN ocpp工具中 偵測有無插槍訊號 chademo轉tesla
  5704. //外接讀卡機, 後台收到, 做驗證, 電樁不知道, 預期會有一把槍進去preparer, 做這個功能 ..
  5705. void ocpp_chk_notificationMessage_cmd(byte gunIndex)
  5706. {
  5707. byte acDirIndex = 0;
  5708. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5709. if (gunIndex == 1)
  5710. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5711. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5712. {
  5713. if (ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].TriggerMessageReq == YES)
  5714. {
  5715. if (chargingInfo [gunIndex]->Type == _Type_Chademo
  5716. && (chargingInfo [gunIndex]->SystemStatus == SYS_MODE_IDLE
  5717. || chargingInfo [gunIndex]->SystemStatus == SYS_MODE_RESERVATION
  5718. || chargingInfo [gunIndex]->SystemStatus == SYS_MODE_MAINTAIN))
  5719. {
  5720. if (ShmDcCommonData->_cha_try_communication [gunIndex].try2CommunicationFlag == NO)
  5721. {
  5722. PRINTF_FUNC ( "Trigger msg (StatusNotification) = %d (chademo) \n", gunIndex );
  5723. ShmDcCommonData->_cha_try_communication [gunIndex].elapsedTime = 0;
  5724. GetTimespecFunc ( & ShmDcCommonData->_cha_try_communication [gunIndex].detectTimer );
  5725. ShmDcCommonData->_cha_try_communication [gunIndex].try2CommunicationFlag = YES;
  5726. }
  5727. }
  5728. else if (chargingInfo [gunIndex]->Type == _Type_CCS
  5729. || chargingInfo [gunIndex]->Type == _Type_GB
  5730. || chargingInfo [gunIndex]->Type == _Type_AC)
  5731. {
  5732. PRINTF_FUNC ( "Trigger msg (StatusNotification) = %d (ccs) \n", gunIndex );
  5733. }
  5734. ShmOCPP16Data->CsMsg.bits [gunIndex + acDirIndex].TriggerMessageReq = NO;
  5735. }
  5736. }
  5737. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5738. {
  5739. }
  5740. if (ShmDcCommonData->_cha_try_communication [gunIndex].try2CommunicationFlag)
  5741. {
  5742. int _timeBuf = GetTimeoutValue ( & ShmDcCommonData->_cha_try_communication [gunIndex].detectTimer );
  5743. if (_timeBuf < 0)
  5744. {
  5745. ShmDcCommonData->_cha_try_communication [gunIndex].elapsedTime = 0;
  5746. GetTimespecFunc ( & ShmDcCommonData->_cha_try_communication [gunIndex].detectTimer );
  5747. }
  5748. if (_timeBuf > 30 || (_timeBuf > 2 && chargingInfo [gunIndex]->ConnectorPlugIn))
  5749. {
  5750. PRINTF_FUNC ( "Trigger msg - timeout or Plugin. \n" );
  5751. ShmDcCommonData->_cha_try_communication [gunIndex].try2CommunicationFlag = NO;
  5752. }
  5753. else
  5754. ShmDcCommonData->_cha_try_communication [gunIndex].elapsedTime = (_timeBuf / uSEC_VAL);
  5755. }
  5756. }
  5757. void ocpp_lift_profileReq_cmd(byte gunIndex)
  5758. {
  5759. byte acDirIndex = 0;
  5760. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5761. if (gunIndex == 1)
  5762. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5763. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5764. {
  5765. if (ShmOCPP16Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileConf == NO)
  5766. {
  5767. if (ShmOCPP16Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileReq == NO)
  5768. ShmOCPP16Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileReq = YES;
  5769. }
  5770. }
  5771. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5772. {
  5773. if (ShmOCPP20Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileConf == NO)
  5774. {
  5775. if (ShmOCPP20Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileReq == NO)
  5776. ShmOCPP20Data->CSUMsg.bits [gunIndex + acDirIndex].ChargingProfileReq = YES;
  5777. }
  5778. }
  5779. }
  5780. void ocpp_set_startTransation_cmd(byte gunIndex)
  5781. {
  5782. byte _OcppGunIndex = gunIndex;
  5783. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5784. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  5785. _OcppGunIndex = 2;
  5786. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5787. {
  5788. if (strcmp ( (char *) chargingInfo [gunIndex]->StartUserId, "" ) == EQUAL)
  5789. strcpy ( (char *) chargingInfo [gunIndex]->StartUserId,
  5790. (char *) ShmOCPP16Data->StartTransaction [_OcppGunIndex].IdTag );
  5791. else
  5792. strcpy ( (char *) ShmOCPP16Data->StartTransaction [_OcppGunIndex].IdTag,
  5793. (char *) chargingInfo [gunIndex]->StartUserId );
  5794. PRINTF_FUNC ( "IdTag = %s \n", chargingInfo [gunIndex]->StartUserId );
  5795. ShmOCPP16Data->CpMsg.bits [_OcppGunIndex].StartTransactionReq = YES;
  5796. }
  5797. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5798. {
  5799. if (strcmp ( (char *) chargingInfo [gunIndex]->StartUserId, "" ) == EQUAL)
  5800. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken,
  5801. (char *) ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken );
  5802. else
  5803. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken,
  5804. (char *) chargingInfo [gunIndex]->StartUserId );
  5805. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken );
  5806. ShmOCPP20Data->CpMsg.bits [_OcppGunIndex].TransactionEventReq = YES;
  5807. }
  5808. }
  5809. void ocpp_set_stopTransation_cmd(byte gunIndex)
  5810. {
  5811. byte _OcppGunIndex = gunIndex;
  5812. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5813. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  5814. _OcppGunIndex = 2;
  5815. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5816. {
  5817. if (strcmp ( (char *) chargingInfo [gunIndex]->StartUserId, "" ) == EQUAL)
  5818. strcpy ( (char *) ShmOCPP16Data->StopTransaction [_OcppGunIndex].IdTag,
  5819. (char *) ShmOCPP16Data->StopTransaction [_OcppGunIndex].IdTag );
  5820. else
  5821. strcpy ( (char *) ShmOCPP16Data->StopTransaction [_OcppGunIndex].IdTag,
  5822. (char *) chargingInfo [gunIndex]->StartUserId );
  5823. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP16Data->StopTransaction [_OcppGunIndex].IdTag );
  5824. ShmOCPP16Data->CpMsg.bits [_OcppGunIndex].StopTransactionReq = YES;
  5825. }
  5826. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5827. {
  5828. if (strcmp ( (char *) chargingInfo [gunIndex]->StartUserId, "" ) == EQUAL)
  5829. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken,
  5830. (char *) ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken );
  5831. else
  5832. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken,
  5833. (char *) chargingInfo [gunIndex]->StartUserId );
  5834. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP20Data->TransactionEvent [_OcppGunIndex].idToken.idToken );
  5835. ShmOCPP20Data->CpMsg.bits [_OcppGunIndex].TransactionEventReq = YES;
  5836. }
  5837. }
  5838. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString)
  5839. {
  5840. // 參數 gunIndex 已包含 AC 槍
  5841. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5842. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex].ErrorCode, errString );
  5843. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5844. {
  5845. }
  5846. }
  5847. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason)
  5848. {
  5849. byte acDirIndex = 0;
  5850. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5851. if (gunIndex == 1)
  5852. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5853. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5854. {
  5855. if (strcmp ( (char *) ShmOCPP16Data->StopTransaction [gunIndex + acDirIndex].StopReason, "" ) == EQUAL)
  5856. {
  5857. strcpy ( (char *) ShmOCPP16Data->StopTransaction [gunIndex + acDirIndex].StopReason, reason );
  5858. }
  5859. }
  5860. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5861. {
  5862. if (strcmp ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].transactionInfo.stoppedReason, "" ) == EQUAL)
  5863. {
  5864. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [gunIndex + acDirIndex].transactionInfo.stoppedReason, reason );
  5865. }
  5866. }
  5867. }
  5868. void ocpp_set_authorizeConf_cmd(byte value)
  5869. {
  5870. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5871. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = value;
  5872. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5873. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = value;
  5874. }
  5875. void ocpp_set_errCode_cmd(byte gunIndex)
  5876. {
  5877. byte acDirIndex = 0;
  5878. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5879. if (gunIndex == 1)
  5880. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5881. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5882. {
  5883. if (strcmp ( (char *) chargingInfo [gunIndex]->ConnectorAlarmCode, "" ) != EQUAL)
  5884. {
  5885. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "InternalError" );
  5886. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].VendorErrorCode,
  5887. (char *) chargingInfo [gunIndex]->ConnectorAlarmCode );
  5888. }
  5889. else if (strcmp ( (char *) chargingInfo [gunIndex]->EvConnAlarmCode, "" ) != EQUAL)
  5890. {
  5891. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "OtherError" );
  5892. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].VendorErrorCode,
  5893. (char *) chargingInfo [gunIndex]->EvConnAlarmCode );
  5894. }
  5895. else
  5896. {
  5897. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "NoError" );
  5898. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].VendorErrorCode, "" );
  5899. }
  5900. }
  5901. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5902. {
  5903. }
  5904. }
  5905. void ocpp_clear_errorCode_cmd(byte gunIndex)
  5906. {
  5907. byte acDirIndex = 0;
  5908. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5909. if (gunIndex == 1)
  5910. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5911. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5912. {
  5913. if (strcmp ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "NoError" ) != EQUAL)
  5914. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "NoError" );
  5915. if (strcmp ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].VendorErrorCode, "" ) != EQUAL)
  5916. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].VendorErrorCode, "" );
  5917. }
  5918. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5919. {
  5920. }
  5921. }
  5922. void ocpp_clear_idTag_cmd(byte gun_index)
  5923. {
  5924. byte acDirIndex = 0;
  5925. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5926. if (gun_index == 1)
  5927. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5928. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5929. strcpy ( (char *) ShmOCPP16Data->StartTransaction [gun_index + acDirIndex].ResponseIdTagInfo.Status, "" );
  5930. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5931. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [gun_index + acDirIndex].Response_idTokenInfo.status, "" );
  5932. }
  5933. void ocpp_clear_stopReason(byte gun_index)
  5934. {
  5935. byte acDirIndex = 0;
  5936. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5937. if (gun_index == 1)
  5938. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5939. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5940. strcpy ( (char *) ShmOCPP16Data->StopTransaction [gun_index + acDirIndex].StopReason, "" );
  5941. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5942. strcpy ( (char *) ShmOCPP20Data->TransactionEvent [gun_index + acDirIndex].transactionInfo.stoppedReason, "" );
  5943. }
  5944. unsigned short _ocpp_get_connect_timeout()
  5945. {
  5946. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING / 1000);
  5947. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5948. {
  5949. if (strcmp ( (char *) ShmOCPP16Data->ConfigurationTable.CoreProfile [ConnectionTimeOut].ItemData, "" ) != 0)
  5950. result = atoi ( (char *) ShmOCPP16Data->ConfigurationTable.CoreProfile [ConnectionTimeOut].ItemData );
  5951. }
  5952. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5953. {
  5954. if (strcmp (
  5955. (char *) ShmOCPP20Data->ControllerComponentVariable [TxCtrlr_EVConnectionTimeOut].variableAttribute [0].value,
  5956. "" ) != 0)
  5957. result = atoi (
  5958. (char *) ShmOCPP20Data->ControllerComponentVariable [TxCtrlr_EVConnectionTimeOut].variableAttribute [0].value );
  5959. }
  5960. return result;
  5961. }
  5962. bool ocpp_get_authorize_conf()
  5963. {
  5964. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5965. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  5966. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5967. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  5968. return false;
  5969. }
  5970. bool ocpp_get_startTransation_req(byte gunIndex)
  5971. {
  5972. bool result = false;
  5973. byte acDirIndex = 0;
  5974. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5975. if (gunIndex == 1)
  5976. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5977. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5978. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5979. {
  5980. result = ShmOCPP16Data->CpMsg.bits [gunIndex + acDirIndex].StartTransactionReq;
  5981. }
  5982. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5983. {
  5984. result = ShmOCPP20Data->CpMsg.bits [gunIndex + acDirIndex].TransactionEventReq;
  5985. }
  5986. return result;
  5987. }
  5988. void ocpp_parsing_DT_defaultPrice_cmd()
  5989. {
  5990. //LWN20210816 _ Charger的OCPP module從DefaultPrice讀到的字串, 寫至CoreProfile[DefaultPrice].ItemData.
  5991. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5992. {
  5993. if (strcmp ( MyDefaultPriceString, (char *) ShmOCPP16Data->ConfigurationTable.CoreProfile [DefaultPrice].ItemData ) != EQUAL)
  5994. {
  5995. DefaultPriceHandlerOcpp16 ( (char *) ShmOCPP16Data->ConfigurationTable.CoreProfile [DefaultPrice].ItemData );
  5996. memcpy ( MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile [DefaultPrice].ItemData, 128 );
  5997. }
  5998. }
  5999. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6000. {
  6001. //ShmOCPP20Data->CostUpdated
  6002. }
  6003. }
  6004. void ocpp_process_start()
  6005. {
  6006. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.OcppServerURL, "" ) != EQUAL
  6007. && strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "" ) != EQUAL)
  6008. {
  6009. if (system ( "pidof -s OcppBackend > /dev/null" ) != 0)
  6010. {
  6011. DEBUG_ERROR_MSG( "OcppBackend not running, restart it.\r\n" );
  6012. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6013. system ( "/root/OcppBackend &" );
  6014. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6015. system ( "/root/OcppBackend20 &" );
  6016. }
  6017. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6018. {
  6019. if ((time ( (time_t*) NULL ) - ShmOCPP16Data->procDogTime) > 180)
  6020. {
  6021. DEBUG_ERROR_MSG( "OcppBackend watch dog timeout task restart.\n" );
  6022. ShmOCPP16Data->procDogTime = time ( (time_t*) NULL );
  6023. system ( "pkill OcppBackend" );
  6024. }
  6025. }
  6026. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6027. {
  6028. if ((time ( (time_t*) NULL ) - ShmOCPP20Data->procDogTime) > 180)
  6029. {
  6030. DEBUG_ERROR_MSG( "OcppBackend20 watch dog timeout task restart.\n" );
  6031. ShmOCPP16Data->procDogTime = time ( (time_t*) NULL );
  6032. system ( "pkill OcppBackend20" );
  6033. }
  6034. }
  6035. }
  6036. }
  6037. void ocpp_parsing_DT_userPrice_cmd()
  6038. {
  6039. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6040. {
  6041. if (strlen ( (char *) ShmOCPP16Data->Cost.SetUserPrice.idToken ) > 0 && strlen (
  6042. (char *) ShmOCPP16Data->Cost.SetUserPrice.price ) > 0)
  6043. {
  6044. UserPriceHandlerOcpp16 ( (char *) ShmOCPP16Data->Cost.SetUserPrice.idToken,
  6045. (char *) ShmOCPP16Data->Cost.SetUserPrice.price );
  6046. //memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  6047. }
  6048. }
  6049. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6050. {
  6051. //ShmOCPP20Data->CostUpdated
  6052. }
  6053. }
  6054. void ocpp_parsing_DT_running_final_cmd()
  6055. {
  6056. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6057. {
  6058. RunningFinalCostHandlerOcpp16 ();
  6059. }
  6060. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6061. {
  6062. //ShmOCPP20Data->CostUpdated
  6063. }
  6064. }
  6065. void ocpp_chargingProfile_process(byte _index)
  6066. {
  6067. int _time = 0;
  6068. int _startCount = NO_DEFINE;
  6069. int _maxCount = 0;
  6070. byte acDirIndex = 0;
  6071. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6072. if (_index == 1)
  6073. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6074. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6075. {
  6076. if (strcmp ( (char *) ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingProfileKind, "Absolute" ) == EQUAL && ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingProfileId == YES)
  6077. {
  6078. _time = GetStartScheduleTime (
  6079. ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.StartSchedule );
  6080. _maxCount = ARRAY_SIZE(
  6081. ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod );
  6082. _startCount = NO_DEFINE;
  6083. for (byte _count = 0; _count < _maxCount; _count ++)
  6084. {
  6085. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6086. if (_time >= ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod [_count].StartPeriod)
  6087. {
  6088. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod [_count].Limit >= MIN_OUTPUT_CUR) || ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod [_count].Limit > MIN_OUTPUT_CUR)
  6089. {
  6090. _startCount = _count;
  6091. }
  6092. }
  6093. }
  6094. PRINTF_FUNC ( "Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount );
  6095. if (_startCount < _maxCount)
  6096. {
  6097. chargingInfo [_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod [_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile [_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod [_startCount].NumberPhases;
  6098. if (chargingInfo [_index]->EvBatterytargetVoltage > 0 && chargingInfo [_index]->PresentChargingVoltage > 0)
  6099. chargingInfo [_index]->ChargingProfileCurrent = (chargingInfo [_index]->ChargingProfilePower / chargingInfo [_index]->PresentChargingVoltage) * 10;
  6100. else
  6101. chargingInfo [_index]->ChargingProfileCurrent = 0;
  6102. }
  6103. else
  6104. {
  6105. chargingInfo [_index]->ChargingProfilePower = - 1;
  6106. chargingInfo [_index]->ChargingProfileCurrent = - 1;
  6107. }
  6108. }
  6109. else
  6110. {
  6111. chargingInfo [_index]->ChargingProfilePower = - 1;
  6112. chargingInfo [_index]->ChargingProfileCurrent = - 1;
  6113. }
  6114. if (strcmp ( (char *) ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute" ) == EQUAL &&
  6115. ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES)
  6116. {
  6117. _time = GetStartScheduleTime ( ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule );
  6118. _maxCount = ARRAY_SIZE( ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod );
  6119. _startCount = NO_DEFINE;
  6120. for (byte _count = 0; _count < _maxCount; _count ++)
  6121. {
  6122. if (_time >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod [_count].StartPeriod)
  6123. {
  6124. if ((_count == 0 && ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod [_count].Limit >= MIN_OUTPUT_CUR) ||
  6125. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod [_count].Limit > MIN_OUTPUT_CUR)
  6126. {
  6127. _startCount = _count;
  6128. }
  6129. }
  6130. }
  6131. PRINTF_FUNC ( "Max Profile Limit = %f \n",
  6132. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod [_startCount].Limit );
  6133. if (_startCount < _maxCount)
  6134. {
  6135. float maxChargingProfilePow = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod [_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod [_startCount].NumberPhases;
  6136. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  6137. maxChargingProfilePow /= 2;
  6138. ShmDcCommonData->ocppMaxChargingProfilePow = maxChargingProfilePow;
  6139. }
  6140. else
  6141. ShmDcCommonData->ocppMaxChargingProfilePow = - 1;
  6142. }
  6143. else
  6144. ShmDcCommonData->ocppMaxChargingProfilePow = - 1;
  6145. }
  6146. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6147. {
  6148. if (strcmp ( (char *) ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingProfileKind, "Absolute" ) == EQUAL &&
  6149. ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].id == YES)
  6150. {
  6151. _time = GetStartScheduleTime (
  6152. ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].startSchedule );
  6153. _maxCount = ARRAY_SIZE(
  6154. ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].chargingSchedulePeriod );
  6155. _startCount = NO_DEFINE;
  6156. for (byte _count = 0; _count < _maxCount; _count ++)
  6157. {
  6158. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6159. if (_time >= ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].chargingSchedulePeriod [_count].startPeriod)
  6160. {
  6161. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].chargingSchedulePeriod [_count].limit >= MIN_OUTPUT_CUR) ||
  6162. ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].chargingSchedulePeriod [_count].limit > MIN_OUTPUT_CUR)
  6163. {
  6164. _startCount = _count;
  6165. }
  6166. }
  6167. }
  6168. PRINTF_FUNC ( "Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount );
  6169. if (_startCount < _maxCount)
  6170. {
  6171. PRINTF_FUNC ( "Profile Limit = %f \n",
  6172. ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].chargingSchedulePeriod [_startCount].limit );
  6173. chargingInfo [_index]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile [_index + acDirIndex].chargingSchedule [0].chargingSchedulePeriod [_startCount].limit * AC_OUTPUT_VOL;
  6174. if (chargingInfo [_index]->EvBatterytargetVoltage > 0 && chargingInfo [_index]->PresentChargingVoltage > 0)
  6175. chargingInfo [_index]->ChargingProfileCurrent = (chargingInfo [_index]->ChargingProfilePower / chargingInfo [_index]->PresentChargingVoltage) * 10;
  6176. else
  6177. chargingInfo [_index]->ChargingProfileCurrent = 0;
  6178. }
  6179. else
  6180. {
  6181. chargingInfo [_index]->ChargingProfilePower = - 1;
  6182. chargingInfo [_index]->ChargingProfileCurrent = - 1;
  6183. }
  6184. }
  6185. else
  6186. {
  6187. chargingInfo [_index]->ChargingProfilePower = - 1;
  6188. chargingInfo [_index]->ChargingProfileCurrent = - 1;
  6189. }
  6190. }
  6191. else
  6192. {
  6193. chargingInfo [_index]->ChargingProfilePower = - 1;
  6194. chargingInfo [_index]->ChargingProfileCurrent = - 1;
  6195. }
  6196. if (chargingInfo [_index]->ChargingProfilePower > 0 &&
  6197. _ChargingProfilePwBuffer [_index] != chargingInfo [_index]->ChargingProfilePower)
  6198. {
  6199. _ChargingProfilePwBuffer[_index] = chargingInfo[_index]->ChargingProfilePower;
  6200. PRINTF_FUNC ( "Profile : ChargingProfilePower = %f \n", chargingInfo [_index]->ChargingProfilePower );
  6201. PRINTF_FUNC ( "Profile : ChargingProfileCurrent = %f \n", chargingInfo [_index]->ChargingProfileCurrent );
  6202. }
  6203. }
  6204. //LWN_Zanobe
  6205. void ocpp_get_LocalAuthorizeOffline_req()
  6206. {
  6207. bool _printInfo = FALSE; //判斷是否要印LocalAuthorizeOffline狀態的flag.
  6208. if (strcmp ( (char*) ShmOCPP16Data->ConfigurationTable.CoreProfile [LocalAuthorizeOffline].ItemData, "" ) != 0)
  6209. {
  6210. //判斷LocalAuthorizeOffline狀態有無改變
  6211. if (_OCPP_LocalAuthorizeOffline == YES)
  6212. {
  6213. if (strcmp ( (char*) ShmOCPP16Data->ConfigurationTable.CoreProfile [LocalAuthorizeOffline].ItemData, "TRUE" ) != EQUAL)
  6214. {
  6215. _printInfo = TRUE;
  6216. _OCPP_LocalAuthorizeOffline = NO;
  6217. }
  6218. }
  6219. else
  6220. {
  6221. if (strcmp ( (char*) ShmOCPP16Data->ConfigurationTable.CoreProfile [LocalAuthorizeOffline].ItemData, "FALSE" ) != EQUAL)
  6222. {
  6223. _printInfo = TRUE;
  6224. _OCPP_LocalAuthorizeOffline = YES;
  6225. }
  6226. }
  6227. }
  6228. else
  6229. _OCPP_LocalAuthorizeOffline = NO;
  6230. // if (strcmp ( (char*) ShmOCPP16Data->ConfigurationTable.CoreProfile [LocalAuthorizeOffline].ItemData, "" ) != 0)
  6231. // {
  6232. // if (strcmp ( (char*) ShmOCPP16Data->ConfigurationTable.CoreProfile [LocalAuthorizeOffline].ItemData, "TRUE" ) == EQUAL)
  6233. // _OCPP_LocalAuthorizeOffline = YES;
  6234. // else
  6235. // _OCPP_LocalAuthorizeOffline = NO;
  6236. // }
  6237. // else
  6238. // _OCPP_LocalAuthorizeOffline = NO;
  6239. //print log info.
  6240. if (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_PREPARE_FOR_EV
  6241. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CHARGING)
  6242. {
  6243. if (_printInfo)
  6244. {
  6245. if (_OCPP_LocalAuthorizeOffline == YES)
  6246. PRINTF_FUNC ( "LocalAuthorizeOfflinel = YES.\n" );
  6247. else
  6248. PRINTF_FUNC ( "LocalAuthorizeOfflinel = NO.\n" );
  6249. }
  6250. }
  6251. }
  6252. //===============================================
  6253. // wifi schedule
  6254. //===============================================
  6255. bool WifiScheduleStop(byte gunIndex)
  6256. {
  6257. bool result = false;
  6258. result = chargingInfo [gunIndex]->schedule.isTriggerStop;
  6259. chargingInfo [gunIndex]->schedule.isTriggerStop = NO;
  6260. return result;
  6261. }
  6262. //===============================================
  6263. // SQLite3 related routine
  6264. //===============================================
  6265. int NetworkDB_Open(sqlite3 *db)
  6266. {
  6267. int result = PASS;
  6268. char* errMsg = NULL;
  6269. char* createNerRecordSql = "CREATE TABLE IF NOT EXISTS network_record("
  6270. "idx integer primary key AUTOINCREMENT, "
  6271. "occurDatetime text NOT NULL, "
  6272. "isInternet text NOT NULL, "
  6273. "isOcppConnected text NOT NULL, "
  6274. "isEth0Internet text NOT NULL, "
  6275. "isMlan0Internet text NOT NULL, "
  6276. "isPpp0Internet text NOT NULL, "
  6277. "rssiMlan0 text NOT NULL, "
  6278. "rssiPpp0 text NOT NULL"
  6279. ");";
  6280. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  6281. if (sqlite3_open ( NETWORK_DB_FILE, & db ))
  6282. {
  6283. result = FAIL;
  6284. PRINTF_FUNC ( "Can't open database (Net) : %s\n", sqlite3_errmsg ( db ) );
  6285. sqlite3_close ( db );
  6286. }
  6287. else
  6288. {
  6289. PRINTF_FUNC ( "Local network status record database open successfully.\n" );
  6290. if (sqlite3_exec ( db, createNerRecordSql, 0, 0, & errMsg ) != SQLITE_OK)
  6291. {
  6292. result = FAIL;
  6293. PRINTF_FUNC ( "Create local network status record table error message: %s\n", errMsg );
  6294. }
  6295. else
  6296. {
  6297. PRINTF_FUNC ( "Opened local network status record table successfully\n" );
  6298. }
  6299. sqlite3_close ( db );
  6300. }
  6301. return result;
  6302. }
  6303. int DB_Open(sqlite3 *db)
  6304. {
  6305. int result = PASS;
  6306. char* errMsg = NULL;
  6307. char* createRecordSql = "CREATE TABLE IF NOT EXISTS charging_record("
  6308. "idx integer primary key AUTOINCREMENT, "
  6309. "reservationId text, "
  6310. "transactionId text, "
  6311. "startMethod text, "
  6312. "userId text, "
  6313. "dateTimeStart text, "
  6314. "dateTimeStop text,"
  6315. "socStart text, "
  6316. "socStop text, "
  6317. "chargeEnergy text, "
  6318. "stopReason text,"
  6319. "vincode text"
  6320. ");";
  6321. char* createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  6322. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6323. "`item` TEXT NOT NULL, "
  6324. "`connector` INTEGER NOT NULL, "
  6325. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  6326. char* creatererrecordSql = "CREATE TABLE IF NOT EXISTS `event_record` ( "
  6327. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6328. "`occurDatetime` TEXT NOT NULL, "
  6329. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  6330. char* createrebootSql = "CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  6331. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6332. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  6333. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  6334. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6335. "`connector` INTEGER NOT NULL, "
  6336. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6337. // char * createTestSql = "CREATE TABLE IF NOT EXISTS `for_test` ( "
  6338. // "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6339. // "`connector` INTEGER NOT NULL, "
  6340. // "`testID` TEXT, "
  6341. // "`checkStatus` INTEGER NOT NULL "
  6342. // ");";
  6343. if (sqlite3_open ( DB_FILE, & db ))
  6344. {
  6345. result = FAIL;
  6346. PRINTF_FUNC ( "Can't open database: %s\r\n", sqlite3_errmsg ( db ) );
  6347. sqlite3_close ( db );
  6348. }
  6349. else
  6350. {
  6351. PRINTF_FUNC ( "Local charging record database open successfully.\r\n" );
  6352. if (sqlite3_exec ( db, createRecordSql, 0, 0, & errMsg ) != SQLITE_OK)
  6353. {
  6354. result = FAIL;
  6355. PRINTF_FUNC ( "Create local charging record table error message: %s\n", errMsg );
  6356. }
  6357. else
  6358. {
  6359. PRINTF_FUNC ( "Opened local charging record table successfully\n" );
  6360. }
  6361. if (sqlite3_exec ( db, createCfgSql, 0, 0, & errMsg ) != SQLITE_OK)
  6362. {
  6363. result = FAIL;
  6364. PRINTF_FUNC ( "Create local config table error message: %s\n", errMsg );
  6365. }
  6366. else
  6367. {
  6368. PRINTF_FUNC ( "Opened local config table successfully\n" );
  6369. }
  6370. if (sqlite3_exec ( db, creatererrecordSql, 0, 0, & errMsg ) != SQLITE_OK)
  6371. {
  6372. result = FAIL;
  6373. PRINTF_FUNC ( "Create local record table error message: %s\n", errMsg );
  6374. }
  6375. else
  6376. {
  6377. PRINTF_FUNC ( "Opened local record table successfully\n" );
  6378. }
  6379. if (sqlite3_exec ( db, createrebootSql, 0, 0, & errMsg ) != SQLITE_OK)
  6380. {
  6381. result = FAIL;
  6382. PRINTF_FUNC ( "Create reboot record table error message: %s\n", errMsg );
  6383. }
  6384. else
  6385. {
  6386. PRINTF_FUNC ( "Opened reboot record table successfully\n" );
  6387. }
  6388. if (sqlite3_exec ( db, createconsumptionSql, 0, 0, & errMsg ) != SQLITE_OK)
  6389. {
  6390. result = FAIL;
  6391. PRINTF_FUNC ( "Create power consumption record table error message: %s\n", errMsg );
  6392. }
  6393. else
  6394. {
  6395. PRINTF_FUNC ( "Opened power consumption record table successfully\n" );
  6396. }
  6397. // if (sqlite3_exec(db, createTestSql, 0, 0, &errMsg) != SQLITE_OK)
  6398. // {
  6399. // result = FAIL;
  6400. // printf( "Create test table error message: %s\n", errMsg);
  6401. // }
  6402. // else
  6403. // {
  6404. // printf( "Opened test table successfully\n");
  6405. // }
  6406. sqlite3_close ( db );
  6407. }
  6408. return result;
  6409. }
  6410. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6411. {
  6412. int result = PASS;
  6413. char* errMsg = NULL;
  6414. char insertSql [1024];
  6415. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6416. {
  6417. sprintf ( insertSql,
  6418. "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, vincode) "
  6419. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  6420. chargingInfo [gun_index]->ReservationId, ShmOCPP16Data->StartTransaction [gun_index].ResponseTransactionId,
  6421. chargingInfo [gun_index]->StartMethod, chargingInfo [gun_index]->StartUserId,
  6422. chargingInfo [gun_index]->StartDateTime, chargingInfo [gun_index]->StopDateTime,
  6423. chargingInfo [gun_index]->EvBatteryStartSoc, chargingInfo [gun_index]->EvBatterySoc,
  6424. chargingInfo [gun_index]->PresentChargedEnergy, ShmOCPP16Data->StopTransaction [gun_index].StopReason,
  6425. chargingInfo [gun_index]->EVCCID );
  6426. }
  6427. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6428. {
  6429. sprintf ( insertSql,
  6430. "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6431. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6432. chargingInfo [gun_index]->ReservationId,
  6433. ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId, chargingInfo [gun_index]->StartMethod,
  6434. chargingInfo [gun_index]->StartUserId, chargingInfo [gun_index]->StartDateTime,
  6435. chargingInfo [gun_index]->StopDateTime, chargingInfo [gun_index]->EvBatteryStartSoc,
  6436. chargingInfo [gun_index]->EvBatterySoc, chargingInfo [gun_index]->PresentChargedEnergy,
  6437. ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.stoppedReason );
  6438. }
  6439. if (sqlite3_open ( DB_FILE, & db ))
  6440. {
  6441. result = FAIL;
  6442. PRINTF_FUNC ( "Can't open database: %s\n", sqlite3_errmsg ( db ) );
  6443. sqlite3_close ( db );
  6444. }
  6445. else
  6446. {
  6447. PRINTF_FUNC ( "Local charging record database open successfully.\n" );
  6448. if (sqlite3_exec ( db, insertSql, 0, 0, & errMsg ) != SQLITE_OK)
  6449. {
  6450. result = FAIL;
  6451. PRINTF_FUNC ( "Insert local charging record error message: %s\n", errMsg );
  6452. }
  6453. else
  6454. {
  6455. PRINTF_FUNC ( "Insert local charging record successfully\n" );
  6456. }
  6457. sprintf ( insertSql,
  6458. "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;" );
  6459. if (sqlite3_exec ( db, insertSql, 0, 0, & errMsg ) != SQLITE_OK)
  6460. {
  6461. result = FAIL;
  6462. PRINTF_FUNC ( "delete local charging error message: %s\n", errMsg );
  6463. }
  6464. else
  6465. {
  6466. PRINTF_FUNC ( "delete local charging record successfully\n" );
  6467. }
  6468. sqlite3_close ( db );
  6469. }
  6470. return result;
  6471. }
  6472. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6473. {
  6474. uint8_t result = false;
  6475. char* errMsg = NULL;
  6476. char sqlStr [1024];
  6477. srand ( time ( NULL ) );
  6478. if (sqlite3_open ( DB_FILE, & db ))
  6479. {
  6480. result = FAIL;
  6481. PRINTF_FUNC ( "Can't open database: %s\r\n", sqlite3_errmsg ( db ) );
  6482. sqlite3_close ( db );
  6483. }
  6484. else
  6485. {
  6486. PRINTF_FUNC ( "Local charging record database open successfully (%d).\r\n", IsAvailable );
  6487. sprintf ( sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index,
  6488. IsAvailable );
  6489. PRINTF_FUNC ( "sqlStr= %s\r\n", sqlStr );
  6490. if (sqlite3_exec ( db, sqlStr, 0, 0, & errMsg ) != SQLITE_OK)
  6491. {
  6492. result = FAIL;
  6493. PRINTF_FUNC ( "update config error message: %s\n", errMsg );
  6494. }
  6495. else
  6496. {
  6497. PRINTF_FUNC ( "update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable );
  6498. }
  6499. sqlite3_close ( db );
  6500. }
  6501. return result;
  6502. }
  6503. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6504. {
  6505. uint8_t result = true;
  6506. char* errMsg = NULL;
  6507. char sqlStr [1024];
  6508. char **rs;
  6509. int rows , cols;
  6510. sprintf ( sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index );
  6511. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  6512. if (sqlite3_open ( DB_FILE, & db ))
  6513. {
  6514. result = FAIL;
  6515. PRINTF_FUNC ( "Can't open database: %s\r\n", sqlite3_errmsg ( db ) );
  6516. sqlite3_close ( db );
  6517. }
  6518. else
  6519. {
  6520. PRINTF_FUNC ( "Local config query database open successfully.\r\n" );
  6521. sqlite3_get_table ( db, sqlStr, & rs, & rows, & cols, & errMsg );
  6522. if (rows > 0)
  6523. {
  6524. for (int idxRow = 1; idxRow <= rows; idxRow ++)
  6525. {
  6526. if (strcmp ( rs [(idxRow * cols) + 3], "0" ) == 0)
  6527. {
  6528. result = false;
  6529. }
  6530. PRINTF_FUNC ( "Query connector-%d isOperactive: %s\r\n", gun_index, rs [(idxRow * cols) + 3] );
  6531. }
  6532. }
  6533. else
  6534. {
  6535. PRINTF_FUNC ( "Query connector-%d fail, set default value to operactive.\r\n", gun_index );
  6536. }
  6537. sqlite3_free_table ( rs );
  6538. sqlite3_close ( db );
  6539. }
  6540. return result;
  6541. }
  6542. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  6543. {
  6544. char* errMsg = NULL;
  6545. char sqlStr [1024];
  6546. srand ( time ( NULL ) );
  6547. if (sqlite3_open ( DB_FILE, & db ))
  6548. {
  6549. PRINTF_FUNC ( "Can't open database: %s\r\n", sqlite3_errmsg ( db ) );
  6550. sqlite3_close ( db );
  6551. }
  6552. else
  6553. {
  6554. sprintf ( sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value );
  6555. if (sqlite3_exec ( db, sqlStr, 0, 0, & errMsg ) != SQLITE_OK)
  6556. {
  6557. PRINTF_FUNC ( "update power_consumption error message: %s\n", errMsg );
  6558. }
  6559. else
  6560. {
  6561. //PRINTF_FUNC("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  6562. }
  6563. sqlite3_close ( db );
  6564. }
  6565. }
  6566. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  6567. {
  6568. float result = 0;
  6569. char* errMsg = NULL;
  6570. char sqlStr [1024];
  6571. char **rs;
  6572. int rows , cols;
  6573. sprintf ( sqlStr, "select * from power_consumption where connector=%d;", gun_index );
  6574. if (sqlite3_open ( DB_FILE, & db ))
  6575. {
  6576. result = FAIL;
  6577. PRINTF_FUNC ( "Can't open database: %s\r\n", sqlite3_errmsg ( db ) );
  6578. sqlite3_close ( db );
  6579. }
  6580. else
  6581. {
  6582. PRINTF_FUNC ( "Local config query database open successfully.\r\n" );
  6583. sqlite3_get_table ( db, sqlStr, & rs, & rows, & cols, & errMsg );
  6584. if (rows > 0)
  6585. {
  6586. for (int idxRow = 1; idxRow <= rows; idxRow ++)
  6587. {
  6588. result = atof ( rs [(idxRow * cols) + 2] );
  6589. }
  6590. }
  6591. else
  6592. {
  6593. PRINTF_FUNC ( "Query connector-%d fail, set default value to consumption.\r\n", gun_index );
  6594. }
  6595. sqlite3_free_table ( rs );
  6596. sqlite3_close ( db );
  6597. }
  6598. return result;
  6599. }
  6600. int DB_Reboot_Record(sqlite3 *db)
  6601. {
  6602. int result = PASS;
  6603. char* errMsg = NULL;
  6604. char insertSql [256];
  6605. sprintf ( insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);" );
  6606. if (sqlite3_open ( DB_FILE, & db ))
  6607. {
  6608. result = FAIL;
  6609. PRINTF_FUNC ( "Can't open database: %s\n", sqlite3_errmsg ( db ) );
  6610. sqlite3_close ( db );
  6611. }
  6612. else
  6613. {
  6614. PRINTF_FUNC ( "Local charging record database open successfully.\n" );
  6615. if (sqlite3_exec ( db, insertSql, 0, 0, & errMsg ) != SQLITE_OK)
  6616. {
  6617. result = FAIL;
  6618. PRINTF_FUNC ( "Insert reboot record error message: %s\n", errMsg );
  6619. }
  6620. else
  6621. {
  6622. PRINTF_FUNC ( "Insert reboot record successfully\n" );
  6623. }
  6624. sprintf ( insertSql,
  6625. "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;" );
  6626. if (sqlite3_exec ( db, insertSql, 0, 0, & errMsg ) != SQLITE_OK)
  6627. {
  6628. result = FAIL;
  6629. PRINTF_FUNC ( "delete reboot record error message: %s\n", errMsg );
  6630. }
  6631. else
  6632. {
  6633. PRINTF_FUNC ( "delete reboot record successfully\n" );
  6634. }
  6635. sqlite3_close ( db );
  6636. }
  6637. return result;
  6638. }
  6639. int DB_Insert_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6640. {
  6641. int result = PASS;
  6642. char* errMsg = NULL;
  6643. char insertSql [1024];
  6644. sprintf ( insertSql, "insert into for_test (connector, testID, checkStatus) values(%d, %s, 0);", gun_index, textID );
  6645. if (sqlite3_open ( DB_FILE, & db ))
  6646. {
  6647. result = FAIL;
  6648. PRINTF_FUNC ( "Can't open database: %s\n", sqlite3_errmsg ( db ) );
  6649. sqlite3_close ( db );
  6650. }
  6651. else
  6652. {
  6653. if (sqlite3_exec ( db, insertSql, 0, 0, & errMsg ) != SQLITE_OK)
  6654. {
  6655. result = FAIL;
  6656. PRINTF_FUNC ( "Insert sdlu test error message : %s\n", errMsg );
  6657. }
  6658. else
  6659. {
  6660. PRINTF_FUNC ( "Insert sdlu test successfully\n" );
  6661. }
  6662. sqlite3_close ( db );
  6663. }
  6664. return result;
  6665. }
  6666. int DB_delete_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6667. {
  6668. int result = PASS;
  6669. char* errMsg = NULL;
  6670. char insertSql [1024];
  6671. sprintf ( insertSql, "delete from for_test where testID = %s", textID );
  6672. if (sqlite3_open ( DB_FILE, & db ))
  6673. {
  6674. result = FAIL;
  6675. PRINTF_FUNC ( "Can't open database: %s\n", sqlite3_errmsg ( db ) );
  6676. sqlite3_close ( db );
  6677. }
  6678. else
  6679. {
  6680. if (sqlite3_exec ( db, insertSql, 0, 0, & errMsg ) != SQLITE_OK)
  6681. {
  6682. result = FAIL;
  6683. PRINTF_FUNC ( "Test message 2: %s\n", errMsg );
  6684. }
  6685. else
  6686. {
  6687. PRINTF_FUNC ( "Test message 2 successfully\n" );
  6688. }
  6689. sqlite3_close ( db );
  6690. }
  6691. return result;
  6692. }
  6693. int DB_sql_test(sqlite3 *db, uint8_t gun_index)
  6694. {
  6695. int result = PASS;
  6696. char* errMsg = NULL;
  6697. char insertSql [1024];
  6698. char valueStr [128];
  6699. char **rs;
  6700. int rows , cols;
  6701. sprintf ( insertSql, "select testID from for_test where checkStatus=0;" );
  6702. if (sqlite3_open ( DB_FILE, & db ))
  6703. {
  6704. result = FAIL;
  6705. PRINTF_FUNC ( "Can't open database: %s\r\n", sqlite3_errmsg ( db ) );
  6706. sqlite3_close ( db );
  6707. }
  6708. else
  6709. {
  6710. PRINTF_FUNC ( "Local for_test query database open successfully.\r\n" );
  6711. sqlite3_get_table ( db, insertSql, & rs, & rows, & cols, & errMsg );
  6712. if (rows > 0)
  6713. {
  6714. for (int idxRow = 1; idxRow <= rows; idxRow ++)
  6715. {
  6716. strcpy ( valueStr, rs [(idxRow * cols)] );
  6717. printf ( "valueStr = %s \n", valueStr );
  6718. }
  6719. }
  6720. else
  6721. {
  6722. PRINTF_FUNC ( "select testID from for_test fail \r\n" );
  6723. }
  6724. sqlite3_free_table ( rs );
  6725. sqlite3_close ( db );
  6726. }
  6727. return result;
  6728. }
  6729. //===============================================
  6730. // Config process
  6731. //===============================================
  6732. void AddPlugInTimes(byte gunIndex)
  6733. {
  6734. if (chargingInfo [gunIndex]->Type == _Type_Chademo)
  6735. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  6736. else if (chargingInfo [gunIndex]->Type == _Type_CCS)
  6737. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  6738. else if (chargingInfo [gunIndex]->Type == _Type_GB)
  6739. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  6740. }
  6741. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  6742. {
  6743. char cmdBuf [32];
  6744. struct timeb csuTime;
  6745. struct tm *tmCSU;
  6746. ftime ( & csuTime );
  6747. tmCSU = localtime ( & csuTime.time );
  6748. sprintf ( cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900, tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour,
  6749. tmCSU->tm_min, tmCSU->tm_sec );
  6750. if (isStart)
  6751. strcpy ( (char *) chargingInfo [gunIndex]->StartDateTime, cmdBuf );
  6752. else
  6753. {
  6754. if (strcmp ( (char *) chargingInfo [gunIndex]->StartDateTime, "00:00:00" ) != EQUAL)
  6755. strcpy ( (char *) chargingInfo [gunIndex]->StopDateTime, cmdBuf );
  6756. }
  6757. }
  6758. void zipLogFiles()
  6759. {
  6760. const char* logPath = "/Storage/SystemLog";
  6761. // 獲取目錄
  6762. DIR* pDir = opendir ( logPath );
  6763. if (pDir != NULL)
  6764. {
  6765. struct timeb csuTime;
  6766. struct tm *tmCSU;
  6767. ftime ( & csuTime );
  6768. tmCSU = localtime ( & csuTime.time );
  6769. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  6770. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6771. // tmCSU->tm_sec);
  6772. // Read items inside the folder
  6773. struct dirent* pEntry = NULL;
  6774. while ((pEntry = readdir ( pDir )) != NULL)
  6775. {
  6776. if (strcmp ( pEntry->d_name, "." ) != 0 && strcmp ( pEntry->d_name, ".." ) != 0 && strncmp ( pEntry->d_name, "[", 1 ) == 0 && strstr (
  6777. pEntry->d_name, "tar" ) < 0)
  6778. {
  6779. char yearC [5];
  6780. unsigned short year = 0;
  6781. char monthC [3];
  6782. unsigned short month = 0;
  6783. yearC [4] = '\0';
  6784. strncpy ( yearC, pEntry->d_name + 1, 4 );
  6785. monthC [2] = '\0';
  6786. strncpy ( monthC, pEntry->d_name + 6, 2 );
  6787. year = atoi ( yearC );
  6788. month = atoi ( monthC );
  6789. if (year != 0)
  6790. {
  6791. if (year < tmCSU->tm_year + 1900 || (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  6792. {
  6793. DEBUG_INFO_MSG( "tar file name : %s \n", pEntry->d_name );
  6794. char file [256];
  6795. memset ( file, 0x00, sizeof(file) );
  6796. strcat ( file, "tar zcvf " );
  6797. strcat ( file, logPath );
  6798. strncat ( file, "/", 1 );
  6799. strcat ( file, pEntry->d_name );
  6800. strcat ( file, ".tar" );
  6801. strncat ( file, " ", 1 );
  6802. strcat ( file, logPath );
  6803. strncat ( file, "/", 1 );
  6804. strcat ( file, pEntry->d_name );
  6805. PRINTF_FUNC ( "zip = %s \n", file );
  6806. system ( file );
  6807. }
  6808. }
  6809. }
  6810. }
  6811. }
  6812. // Close folder
  6813. closedir ( pDir );
  6814. }
  6815. void ChangeGunSelectByIndex(byte sel)
  6816. {
  6817. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  6818. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  6819. }
  6820. void CheckIsAlternatvieByModelName()
  6821. {
  6822. if (ShmSysConfigAndInfo->SysConfig.ModelName [1] == 'W')
  6823. {
  6824. // 壁掛
  6825. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  6826. }
  6827. else
  6828. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  6829. }
  6830. void StopProcessingLoop()
  6831. {
  6832. ocpp_process_start ();
  6833. for (;;)
  6834. {
  6835. CheckTask ();
  6836. CheckFactoryConfigFunction ();
  6837. CheckFwUpdateFunction ();
  6838. ocpp_chk_reset_cmd ( YES );
  6839. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  6840. {
  6841. // 經過底下的 function 後,ShmSysConfigAndInfo->SysWarningInfo.Level 會再被更新一次
  6842. ChkPrimaryStatus ();
  6843. ReviewCriticalAlarm ();
  6844. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_NORMAL)
  6845. {
  6846. PRINTF_FUNC ( "Soft reboot for retry self-tets (Primary). \n" );
  6847. KillAllTask ();
  6848. sleep ( 3 );
  6849. system ( "/usr/bin/run_evse_restart.sh" );
  6850. return;
  6851. }
  6852. }
  6853. sleep ( 1 );
  6854. }
  6855. }
  6856. void CreateWatchdog()
  6857. {
  6858. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  6859. {
  6860. wtdFd = InitWatchDog ();
  6861. if (wtdFd < 0)
  6862. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  6863. }
  6864. }
  6865. bool IsConnectorWholeIdle()
  6866. {
  6867. bool result = true;
  6868. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count ++)
  6869. {
  6870. if (chargingInfo [count]->SystemStatus != SYS_MODE_IDLE && chargingInfo [count]->SystemStatus != SYS_MODE_RESERVATION)
  6871. {
  6872. result = false;
  6873. break;
  6874. }
  6875. }
  6876. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count ++)
  6877. {
  6878. if (ac_chargingInfo [count]->SystemStatus != SYS_MODE_IDLE && ac_chargingInfo [count]->IsErrorOccur == NO)
  6879. {
  6880. result = false;
  6881. break;
  6882. }
  6883. }
  6884. return result;
  6885. }
  6886. void ClearAlarmCodeWhenAcOff()
  6887. {
  6888. if ( ! ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  6889. {
  6890. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  6891. }
  6892. }
  6893. //==========================================
  6894. // Check task processing
  6895. //==========================================
  6896. void CheckTask()
  6897. {
  6898. if (reset4gStep == RESET_4G_STEP_NONE
  6899. && (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'T'
  6900. || ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  6901. {
  6902. if (system ( "pidof -s Module_4g > /dev/null" ) != 0)
  6903. {
  6904. DEBUG_ERROR_MSG( "Module_4g not running, restart it.\r\n" );
  6905. system ( "/root/Module_4g &" );
  6906. }
  6907. }
  6908. if (reset4gStep == RESET_4G_STEP_NONE && (ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'W'
  6909. || ShmSysConfigAndInfo->SysConfig.ModelName [10] == 'D'))
  6910. {
  6911. if (system ( "pidof -s Module_Wifi > /dev/null" ) != 0)
  6912. {
  6913. DEBUG_ERROR_MSG( "Module_Wifi not running, restart it.\r\n" );
  6914. system ( "/root/Module_Wifi &" );
  6915. }
  6916. }
  6917. if (ShmSysConfigAndInfo->SysConfig.ModelName [3] == 'P')
  6918. {
  6919. if (system ( "pidof -s Module_DcMeter > /dev/null" ) != 0)
  6920. {
  6921. DEBUG_ERROR_MSG( "Module_DcMeter not running, restart it.\r\n" );
  6922. system ( "/root/Module_DcMeter &" );
  6923. }
  6924. }
  6925. ocpp_process_start ();
  6926. if (system ( "pidof -s Module_ProduceUtils > /dev/null" ) != 0)
  6927. {
  6928. DEBUG_ERROR_MSG( "Module_ProduceUtils not running, restart it.\r\n" );
  6929. system ( "/root/Module_ProduceUtils &" );
  6930. }
  6931. }
  6932. void CheckSystemTaskAlive()
  6933. {
  6934. unsigned char lostId = CheckSystemTask ( ShmSysConfigAndInfo->SysInfo.SystemPage );
  6935. if (lostId != 0)
  6936. {
  6937. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost == NO)
  6938. {
  6939. EmcOccureThenStopCharging ();
  6940. sleep ( 2 );
  6941. if (lostId == _SYSTEM_TASK_LOST_ITEM_MAIN)
  6942. PRINTF_FUNC ( "System task lost (CSU). \n" );
  6943. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVCOMM)
  6944. {
  6945. PRINTF_FUNC ( "System task lost (EVComm). \n" );
  6946. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun ++)
  6947. {
  6948. chargingInfo [gun]->ConnectorPlugIn = NO;
  6949. }
  6950. }
  6951. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PSUCOMM)
  6952. PRINTF_FUNC ( "System task lost (PSU Task). \n" );
  6953. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVENTLOG)
  6954. PRINTF_FUNC ( "System task lost (Event log). \n" );
  6955. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PRIMARYCOMM)
  6956. PRINTF_FUNC ( "System task lost (Primary). \n" );
  6957. else if (lostId == _SYSTEM_TASK_LOST_ITEM_LCMCONTROL)
  6958. PRINTF_FUNC ( "System task lost (LCM Comm). \n" );
  6959. else if (lostId == _SYSTEM_TASK_LOST_ITEM_INTERCOMM)
  6960. PRINTF_FUNC ( "System task lost (Internal Comm). \n" );
  6961. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = YES;
  6962. }
  6963. }
  6964. else
  6965. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = NO;
  6966. }
  6967. void InitialDHCP()
  6968. {
  6969. char tmpbuf [256];
  6970. memset ( tmpbuf, 0, 256 );
  6971. system ( "pgrep -f \"udhcpc -i eth0\" | xargs kill" );
  6972. sprintf ( tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  6973. ShmSysConfigAndInfo->SysConfig.SystemId );
  6974. system ( tmpbuf );
  6975. }
  6976. void Run4gResetProc()
  6977. {
  6978. if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == _SYS_4G_MODE_ENABLE || ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_STATION)
  6979. {
  6980. if ( ! ShmSysConfigAndInfo->SysInfo.OcppConnStatus && ! ShmSysConfigAndInfo->SysInfo.InternetConn)
  6981. {
  6982. if (reset4gStep == RESET_4G_STEP_NONE)
  6983. {
  6984. if ((ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES && _4gResetChkFlag >= 12) || _4gResetChkFlag >= 60)
  6985. {
  6986. PRINTF_FUNC ( "Reset 4G/Wifi module. \n" );
  6987. reset4gStep = RESET_4G_STEP_DELETE_TASK;
  6988. }
  6989. else
  6990. _4gResetChkFlag ++;
  6991. }
  6992. }
  6993. else
  6994. _4gResetChkFlag = 0;
  6995. switch (reset4gStep)
  6996. {
  6997. case RESET_4G_STEP_DELETE_TASK :
  6998. {
  6999. PRINTF_FUNC ( "Killall 4G / Wifi task. \n" );
  7000. system ( "killall Module_4g &" );
  7001. system ( "killall Module_Wifi &" );
  7002. reset4gStep = RESET_4G_STEP_GPIO_HIGH;
  7003. }
  7004. break;
  7005. case RESET_4G_STEP_GPIO_HIGH :
  7006. {
  7007. PRINTF_FUNC ( "Disable 4G/Wifi. GPIO-104 -> High. \n" );
  7008. system ( "echo 1 > /sys/class/gpio/gpio104/value" );
  7009. reset4gStep = RESET_4G_STEP_GPIO_LOW_COMP;
  7010. }
  7011. break;
  7012. case RESET_4G_STEP_GPIO_LOW_COMP :
  7013. {
  7014. PRINTF_FUNC ( "Enable 4G/Wifi. GPIO-104 -> Low. \n" );
  7015. system ( "echo 0 > /sys/class/gpio/gpio104/value" );
  7016. reset4gStep = RESET_4G_STEP_NONE;
  7017. _4gResetChkFlag = 0;
  7018. }
  7019. break;
  7020. }
  7021. }
  7022. else
  7023. {
  7024. if (reset4gStep != RESET_4G_STEP_NONE)
  7025. system ( "echo 0 > /sys/class/gpio/gpio104/value" );
  7026. reset4gStep = RESET_4G_STEP_NONE;
  7027. _4gResetChkFlag = 0;
  7028. }
  7029. }
  7030. //==========================================
  7031. // Check Smart Charging Profile
  7032. //==========================================
  7033. int GetStartScheduleTime(unsigned char *time)
  7034. {
  7035. int result = - 1;
  7036. struct tm tmScheduleStart;
  7037. struct timeb tbScheduleStart;
  7038. if ((sscanf ( (char *) time, "%4d-%2d-%2dT%2d:%2d:%2d", & tmScheduleStart.tm_year, & tmScheduleStart.tm_mon,
  7039. & tmScheduleStart.tm_mday, & tmScheduleStart.tm_hour, & tmScheduleStart.tm_min, & tmScheduleStart.tm_sec ) == 6))
  7040. {
  7041. tmScheduleStart.tm_year -= 1900;
  7042. tmScheduleStart.tm_mon -= 1;
  7043. tbScheduleStart.time = mktime ( & tmScheduleStart );
  7044. tbScheduleStart.millitm = 0;
  7045. result = DiffTimebWithNow ( tbScheduleStart ) / 1000;
  7046. }
  7047. return result;
  7048. }
  7049. void CheckSmartChargeProfile(byte _index)
  7050. {
  7051. if (ocpp_chk_profileConf_cmd ( _index ))
  7052. {
  7053. ocpp_chargingProfile_process ( _index );
  7054. }
  7055. }
  7056. //==========================================
  7057. // sub function
  7058. //==========================================
  7059. void CheckReturnToChargingConn()
  7060. {
  7061. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1
  7062. && ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING
  7063. && ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL
  7064. && ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP
  7065. && ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  7066. {
  7067. bool isReturnTimeout = false;
  7068. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count ++)
  7069. {
  7070. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  7071. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7072. {
  7073. if ((chargingInfo [count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  7074. && chargingInfo [count]->SystemStatus <= SYS_MODE_COMPLETE)
  7075. || (chargingInfo [count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  7076. && chargingInfo [count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7077. {
  7078. isReturnTimeout = false;
  7079. break;
  7080. }
  7081. }
  7082. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7083. {
  7084. if ((chargingInfo [count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  7085. && chargingInfo [count]->SystemStatus <= SYS_MODE_COMPLETE)
  7086. || (chargingInfo [count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  7087. && chargingInfo [count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7088. {
  7089. isReturnTimeout = true;
  7090. StartSystemTimeoutDet ( Timeout_ReturnToChargingGunDet );
  7091. }
  7092. }
  7093. }
  7094. // AC 槍
  7095. if ( ! isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  7096. {
  7097. // 沒有選中 AC,且 AC 在充電中
  7098. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE
  7099. && (ac_chargingInfo [0]->SystemStatus >= SYS_MODE_PREPARING
  7100. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_COMPLETE))
  7101. {
  7102. // 當前 DC 充電槍在 idle 狀態
  7103. if (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE
  7104. || chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  7105. {
  7106. isReturnTimeout = true;
  7107. StartSystemTimeoutDet ( Timeout_ReturnToChargingGunDet );
  7108. }
  7109. }
  7110. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX
  7111. && ((chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK
  7112. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_COMPLETE)
  7113. || (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  7114. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  7115. {
  7116. // 當前 DC 充電槍在 idle 狀態
  7117. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE || ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION)
  7118. {
  7119. isReturnTimeout = true;
  7120. StartSystemTimeoutDet ( Timeout_ReturnToChargingGunDet );
  7121. }
  7122. }
  7123. }
  7124. if ( ! isReturnTimeout)
  7125. StopSystemTimeoutDet ();
  7126. }
  7127. }
  7128. bool GetStartChargingByAlterMode(byte _gun)
  7129. {
  7130. bool result = true;
  7131. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2
  7132. && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  7133. {
  7134. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select ++)
  7135. {
  7136. if (_select != _gun)
  7137. {
  7138. if (chargingInfo [_select]->SystemStatus != SYS_MODE_IDLE
  7139. && chargingInfo [_select]->SystemStatus != SYS_MODE_RESERVATION)
  7140. {
  7141. result = false;
  7142. break;
  7143. }
  7144. }
  7145. }
  7146. }
  7147. return result;
  7148. }
  7149. void TheEndCharging(byte gun_index)
  7150. {
  7151. chargingInfo [gun_index]->isRemoteStart = NO;
  7152. StopGunInfoTimeoutDet ( gun_index );
  7153. StartGunInfoTimeoutDet ( gun_index, Timeout_EvseCompleteDet );
  7154. ChangeStartOrStopDateTime ( NO, gun_index );
  7155. DB_Insert_Record ( localDb, gun_index );
  7156. }
  7157. void AdjustChargerCurrent()
  7158. {
  7159. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  7160. // 設定的電流~ 如超過可輸出的電流,則 bypass
  7161. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent || ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0)
  7162. {
  7163. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  7164. }
  7165. PRINTF_FUNC ( "PSU : MaxChargingPower = %d, MaxChargingCurrent = %d", ShmPsuData->SystemAvailablePower / 10,
  7166. ShmPsuData->SystemAvailableCurrent / 10 );
  7167. PRINTF_FUNC ( "Config : ChargingPower = %d, ChargingCurrent = %d", ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  7168. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent );
  7169. }
  7170. void CheckStatusOfMeter()
  7171. {
  7172. // 橋接如果搭上~ 則兩個電表都是使用狀態
  7173. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  7174. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  7175. {
  7176. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex ++)
  7177. ShmCsuMeterData->_meter [mIndex].isCalculation = YES;
  7178. }
  7179. else
  7180. {
  7181. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex ++)
  7182. {
  7183. if (chargingInfo [mIndex]->RelayKPK2Status == YES || chargingInfo [mIndex]->RelayK1K2Status)
  7184. ShmCsuMeterData->_meter [mIndex].isCalculation = YES;
  7185. else
  7186. ShmCsuMeterData->_meter [mIndex].isCalculation = NO;
  7187. }
  7188. }
  7189. }
  7190. void ReviewCriticalAlarm()
  7191. {
  7192. if (ShmDcCommonData->GunRelayDrivingOccur [0] == YES
  7193. || ShmDcCommonData->GunRelayDrivingOccur [1] == YES
  7194. || ShmDcCommonData->GunRelayWeldingOccur [0] == YES
  7195. || ShmDcCommonData->GunRelayWeldingOccur [1] == YES
  7196. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip
  7197. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip
  7198. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen
  7199. || ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE
  7200. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm
  7201. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost)
  7202. {
  7203. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_CRITICAL;
  7204. }
  7205. else
  7206. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_NORMAL;
  7207. }
  7208. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  7209. {
  7210. // relay welding fault then stop the charging process.
  7211. byte faultCode = RELAY_STATUS_ERROR_NONE;
  7212. if (gun_index == 0)
  7213. {
  7214. if (ShmDcCommonData->RelayCheckStatus [RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING || ShmDcCommonData->RelayCheckStatus [RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7215. {
  7216. faultCode = RELAY_STATUS_ERROR_WELDING;
  7217. }
  7218. else if (ShmDcCommonData->RelayCheckStatus [RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING || ShmDcCommonData->RelayCheckStatus [RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7219. {
  7220. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7221. }
  7222. }
  7223. else if (gun_index == 1)
  7224. {
  7225. if (ShmDcCommonData->RelayCheckStatus [RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING || ShmDcCommonData->RelayCheckStatus [RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7226. {
  7227. faultCode = RELAY_STATUS_ERROR_WELDING;
  7228. }
  7229. else if (ShmDcCommonData->RelayCheckStatus [RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING || ShmDcCommonData->RelayCheckStatus [RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7230. {
  7231. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7232. }
  7233. }
  7234. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 && ! ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  7235. {
  7236. // 橋接
  7237. if (ShmDcCommonData->RelayCheckStatus [RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING || ShmDcCommonData->RelayCheckStatus [RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7238. {
  7239. faultCode = RELAY_STATUS_ERROR_WELDING;
  7240. }
  7241. else if (ShmDcCommonData->RelayCheckStatus [RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING || ShmDcCommonData->RelayCheckStatus [RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7242. {
  7243. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7244. }
  7245. }
  7246. if (faultCode == RELAY_STATUS_ERROR_WELDING)
  7247. {
  7248. // welding
  7249. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  7250. RecordAlarmCode ( gun_index, "011011" );
  7251. else if (chargingInfo [gun_index]->Type == _Type_GB)
  7252. RecordAlarmCode ( gun_index, "011015" );
  7253. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  7254. RecordAlarmCode ( gun_index, "011013" );
  7255. ShmDcCommonData->GunRelayWeldingOccur [gun_index] = YES;
  7256. EmcOccureThenStopCharging ();
  7257. }
  7258. else if (faultCode == RELAY_STATUS_ERROR_DRIVING)
  7259. {
  7260. // driving
  7261. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  7262. RecordAlarmCode ( gun_index, "011012" );
  7263. else if (chargingInfo [gun_index]->Type == _Type_GB)
  7264. RecordAlarmCode ( gun_index, "011016" );
  7265. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  7266. RecordAlarmCode ( gun_index, "011014" );
  7267. ShmDcCommonData->GunRelayDrivingOccur [gun_index] = YES;
  7268. EmcOccureThenStopCharging ();
  7269. }
  7270. else
  7271. {
  7272. ShmDcCommonData->GunRelayWeldingOccur [gun_index] = NO;
  7273. ShmDcCommonData->GunRelayDrivingOccur [gun_index] = NO;
  7274. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  7275. {
  7276. ResetChargerAlarmCode ( gun_index, "011012" );
  7277. ResetChargerAlarmCode ( gun_index, "011011" );
  7278. }
  7279. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  7280. {
  7281. ResetChargerAlarmCode ( gun_index, "011014" );
  7282. ResetChargerAlarmCode ( gun_index, "011013" );
  7283. }
  7284. else if (chargingInfo [gun_index]->Type == _Type_GB)
  7285. {
  7286. ResetChargerAlarmCode ( gun_index, "011016" );
  7287. ResetChargerAlarmCode ( gun_index, "011015" );
  7288. }
  7289. }
  7290. }
  7291. bool PrecheckIsPass(byte gun_index)
  7292. {
  7293. bool result = true;
  7294. // relay welding or driving 是反向
  7295. if (result)
  7296. result = ! ShmDcCommonData->GunRelayWeldingOccur [gun_index];
  7297. return result;
  7298. }
  7299. void CollectError(byte gun_index)
  7300. {
  7301. errCollect.GunErrMessage |= ShmDcCommonData->ConnectErrList [gun_index].GunErrMessage;
  7302. }
  7303. void AlarmCheck(byte type)
  7304. {
  7305. // type : Cha = 0、CCS = 1、GBT = 2
  7306. if (type == _Type_Chademo)
  7307. {
  7308. // GFD Trip
  7309. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 0 ))
  7310. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  7311. else
  7312. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  7313. // UVP
  7314. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 1 ))
  7315. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  7316. else
  7317. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  7318. // OTP
  7319. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 2 ))
  7320. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = YES;
  7321. else
  7322. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = NO;
  7323. // OVP
  7324. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 3 ))
  7325. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  7326. else
  7327. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  7328. // GFD Warning
  7329. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 4 ))
  7330. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  7331. else
  7332. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  7333. // Relay Welding
  7334. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 5 ))
  7335. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  7336. else
  7337. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  7338. // Relay Driving
  7339. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 6 ))
  7340. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  7341. else
  7342. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  7343. // Connect temp Sensor broken
  7344. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 7 ))
  7345. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  7346. else
  7347. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  7348. // Output UCP
  7349. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * 3), 0 ))
  7350. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = YES;
  7351. else
  7352. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = NO;
  7353. }
  7354. else if (type == _Type_CCS)
  7355. {
  7356. // GFD Trip
  7357. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 0 ))
  7358. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  7359. else
  7360. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  7361. // UVP
  7362. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 1 ))
  7363. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  7364. else
  7365. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  7366. // OTP
  7367. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 2 ))
  7368. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = YES;
  7369. else
  7370. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = NO;
  7371. // OVP
  7372. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 3 ))
  7373. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  7374. else
  7375. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  7376. // GFD Warning
  7377. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 4 ))
  7378. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  7379. else
  7380. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  7381. // Relay Welding
  7382. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 5 ))
  7383. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  7384. else
  7385. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  7386. // Relay Driving
  7387. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 6 ))
  7388. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  7389. else
  7390. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  7391. // Connect temp Sensor broken
  7392. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 7 ))
  7393. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  7394. else
  7395. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  7396. // Output UCP
  7397. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * 3), 1 ))
  7398. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = YES;
  7399. else
  7400. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = NO;
  7401. }
  7402. else if (type == _Type_GB)
  7403. {
  7404. // GFD Trip
  7405. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 0 ))
  7406. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  7407. else
  7408. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  7409. // UVP
  7410. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 1 ))
  7411. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  7412. else
  7413. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  7414. // OTP
  7415. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 2 ))
  7416. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = YES;
  7417. else
  7418. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = NO;
  7419. // OVP
  7420. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 3 ))
  7421. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  7422. else
  7423. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  7424. // GFD Warning
  7425. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 4 ))
  7426. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  7427. else
  7428. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  7429. // Relay Welding
  7430. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 5 ))
  7431. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  7432. else
  7433. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  7434. // Relay Driving
  7435. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 6 ))
  7436. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  7437. else
  7438. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  7439. // Connect temp Sensor broken
  7440. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * type), 7 ))
  7441. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  7442. else
  7443. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  7444. // Output UCP
  7445. if (DetectBitValue ( errCollect.GunErrMessage >> (8 * 3), 2 ))
  7446. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = YES;
  7447. else
  7448. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = NO;
  7449. }
  7450. }
  7451. void SavePresentChargedDuration(byte gun_index)
  7452. {
  7453. int _diffTime = 0;
  7454. ftime ( & endChargingTime [gun_index] );
  7455. _diffTime = DiffTimeb ( startChargingTime [gun_index], endChargingTime [gun_index] );
  7456. // below 0 or over 5 sec is abnormal
  7457. if (_diffTime < 0 || _diffTime > chargingInfo [gun_index]->PresentChargedDuration + 5)
  7458. {
  7459. _presentChargingTimeBuf [gun_index] = chargingInfo [gun_index]->PresentChargedDuration;
  7460. ftime ( & startChargingTime [gun_index] );
  7461. PRINTF_FUNC ( "TimeZone changed (%d) \n", gun_index );
  7462. }
  7463. else
  7464. chargingInfo [gun_index]->PresentChargedDuration = _presentChargingTimeBuf [gun_index] + _diffTime;
  7465. }
  7466. void ClearEnergyTimePeriod(byte gun_index)
  7467. {
  7468. for (int i = 0; i < 24; i ++)
  7469. {
  7470. ShmDcCommonData->energy_time_period [gun_index] [i] = 0;
  7471. }
  7472. }
  7473. void ChangeToMaxChargingMode()
  7474. {
  7475. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  7476. {
  7477. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  7478. {
  7479. // 均充 -> 最大充
  7480. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  7481. {
  7482. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  7483. {
  7484. // 切換最大充起始位置
  7485. PRINTF_FUNC ( "======= Aver -> Max : Preparing. (Step 11) ======= \n" );
  7486. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  7487. }
  7488. }
  7489. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP && ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  7490. {
  7491. PRINTF_FUNC ( "======= Aver -> Max : Current Balancing. (Step 14) ======= \n" );
  7492. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  7493. }
  7494. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  7495. {
  7496. PRINTF_FUNC ( "======= Aver -> Max : Charging mode = MAX ======= \n" );
  7497. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7498. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7499. }
  7500. }
  7501. else
  7502. {
  7503. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7504. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7505. }
  7506. }
  7507. else
  7508. {
  7509. // 需考量如果再切換均充過程 ? (預計要進入充電的臨時取消充電)
  7510. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7511. }
  7512. }
  7513. void CheckOcppCostAndPrice()
  7514. {
  7515. ocpp_parsing_DT_defaultPrice_cmd ();
  7516. ocpp_parsing_DT_userPrice_cmd ();
  7517. ocpp_parsing_DT_running_final_cmd ();
  7518. }
  7519. void CheckConnectorOTP(byte _index)
  7520. {
  7521. if (chargingInfo [_index]->Type == _Type_Chademo)
  7522. {
  7523. // if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7524. // {
  7525. // ResetChargerAlarmCode(_index, "011018");
  7526. // if (chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7527. // RecordAlarmCode(_index, "012229");
  7528. // else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7529. // chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7530. // ResetChargerAlarmCode(_index, "012229");
  7531. // }
  7532. // else
  7533. // {
  7534. // // 沒接上 Sensor or 異常
  7535. // //RecordAlarmCode(_index, "011018");
  7536. // ResetChargerAlarmCode(_index, "012229");
  7537. // }
  7538. }
  7539. else if (chargingInfo [_index]->Type == _Type_CCS)
  7540. {
  7541. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  7542. if (chargingInfo [_index]->ConnectorTemp != UNDEFINED_TEMP)
  7543. {
  7544. ResetChargerAlarmCode ( _index, "011019" );
  7545. if (chargingInfo [_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7546. RecordAlarmCode ( _index, "012230" );
  7547. else if (chargingInfo [_index]->ConnectorTemp != 0 && chargingInfo [_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7548. ResetChargerAlarmCode ( _index, "012230" );
  7549. }
  7550. else
  7551. {
  7552. // 沒接上 Sensor or 異常
  7553. RecordAlarmCode ( _index, "011019" );
  7554. ResetChargerAlarmCode ( _index, "012230" );
  7555. }
  7556. }
  7557. else if (chargingInfo [_index]->Type == _Type_GB && chargingInfo [_index]->ModelType == 'B')
  7558. {
  7559. if (chargingInfo [_index]->ConnectorTemp != UNDEFINED_TEMP)
  7560. {
  7561. ResetChargerAlarmCode ( _index, "011020" );
  7562. if (chargingInfo [_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7563. RecordAlarmCode ( _index, "012231" );
  7564. else if (chargingInfo [_index]->ConnectorTemp != 0 && chargingInfo [_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7565. ResetChargerAlarmCode ( _index, "012231" );
  7566. }
  7567. else
  7568. {
  7569. // 沒接上 Sensor or 異常
  7570. RecordAlarmCode ( _index, "011020" );
  7571. ResetChargerAlarmCode ( _index, "012231" );
  7572. }
  7573. }
  7574. }
  7575. void ChkConnectorBoardStop(byte index)
  7576. {
  7577. if (isEvBoardStopChargeFlag ( index ) == YES)
  7578. {
  7579. // 板端要求停止 (錯誤)
  7580. ocpp_set_stopReason_by_cmd ( index, "EVDisconnected" );
  7581. ChargingAlarmProcess ( index );
  7582. //LWN_Zanobe
  7583. //LWADD0924
  7584. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7585. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  7586. }
  7587. else if (isEvBoardNormalStopChargeFlag ( index ) == YES)
  7588. {
  7589. // 板端要求停止 (正常)
  7590. ocpp_set_stopReason_by_cmd ( index, "EVDisconnected" );
  7591. ChargingTerminalProcess ( index );
  7592. //LWN_Zanobe
  7593. //LWADD0924
  7594. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7595. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  7596. }
  7597. }
  7598. void ChkBackendStop(byte index)
  7599. {
  7600. if (ocpp_chk_invalid_id_cmd ( index ))
  7601. {
  7602. // Start Transaction Stop
  7603. ocpp_set_stopReason_by_cmd ( index, "DeAuthorized" );
  7604. ChargingTerminalProcess ( index );
  7605. }
  7606. else if (ocpp_chk_remoteStop_cmd ( index ) == YES || WifiScheduleStop ( index ))
  7607. {
  7608. // 後臺要求停止
  7609. ChargingTerminalProcess ( index );
  7610. }
  7611. }
  7612. void ChkConfigurationStop(byte index)
  7613. {
  7614. if (CheckBackendChargingTimeout ( index ) || CheckBackendChargingEnergy ( index ))
  7615. {
  7616. // 後臺要求停止
  7617. ChargingTerminalProcess ( index );
  7618. }
  7619. }
  7620. void RecordGfdResult(byte index)
  7621. {
  7622. if (chargingInfo [index]->Type == _Type_Chademo)
  7623. {
  7624. if (chargingInfo [index]->GroundFaultStatus == GFD_FAIL)
  7625. {
  7626. // GFD 錯誤停止
  7627. RecordAlarmCode ( index, "012234" );
  7628. }
  7629. else if (chargingInfo [index]->GroundFaultStatus == GFD_WARNING)
  7630. {
  7631. // GFD 警告
  7632. RecordAlarmCode ( index, "012296" );
  7633. }
  7634. }
  7635. else if (chargingInfo [index]->Type == _Type_GB)
  7636. {
  7637. if (chargingInfo [index]->GroundFaultStatus == GFD_FAIL)
  7638. {
  7639. // GFD 錯誤停止
  7640. RecordAlarmCode ( index, "012236" );
  7641. }
  7642. else if (chargingInfo [index]->GroundFaultStatus == GFD_WARNING)
  7643. {
  7644. // GFD 警告
  7645. RecordAlarmCode ( index, "012298" );
  7646. }
  7647. }
  7648. else if (chargingInfo [index]->Type == _Type_CCS)
  7649. {
  7650. if (chargingInfo [index]->GroundFaultStatus == GFD_FAIL)
  7651. {
  7652. // GFD 錯誤停止
  7653. RecordAlarmCode ( index, "012235" );
  7654. }
  7655. else if (chargingInfo [index]->GroundFaultStatus == GFD_WARNING)
  7656. {
  7657. // GFD 警告
  7658. RecordAlarmCode ( index, "012297" );
  7659. }
  7660. }
  7661. }
  7662. #ifdef LW_DEBUG_INFO
  7663. bool CheckDebugInfoTimeout(byte index, byte timeout)
  7664. {
  7665. int _timebuf = 0;
  7666. struct timespec *Timer = NULL;
  7667. if (index == 0)
  7668. Timer = & ShmDcCommonData->DbgInfo.Timer_0;
  7669. else if (index == 1)
  7670. Timer = & ShmDcCommonData->DbgInfo.Timer_1;
  7671. else if (index == 2)
  7672. Timer = & ShmDcCommonData->DbgInfo.Timer_2;
  7673. else if (index == 3)
  7674. Timer = & ShmDcCommonData->DbgInfo.Timer_3;
  7675. else if (index == 4)
  7676. Timer = & ShmDcCommonData->DbgInfo.Timer_4;
  7677. _timebuf = GetTimeoutValue ( Timer );
  7678. if (_timebuf < 0)
  7679. GetTimespecFunc ( Timer );
  7680. else
  7681. {
  7682. if (_timebuf >= timeout)
  7683. {
  7684. GetTimespecFunc ( Timer );
  7685. return TRUE;
  7686. }
  7687. }
  7688. return FALSE;
  7689. }
  7690. #endif
  7691. int main(void)
  7692. {
  7693. if (CreateShareMemory () == 0)
  7694. {
  7695. #ifdef SystemLogMessage
  7696. DEBUG_ERROR_MSG( "CreatShareMemory NG \n" );
  7697. #endif
  7698. if (ShmStatusCodeData != NULL)
  7699. {
  7700. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  7701. }
  7702. sleep ( 5 );
  7703. system ( "reboot -f" );
  7704. sleep ( 5 );
  7705. system ( "reboot -f" );
  7706. }
  7707. PRINTF_FUNC ( "===== Boot and Initialization start ===== \n" );
  7708. if ( ! InitialSystemDefaultConfig ())
  7709. {
  7710. DEBUG_ERROR_MSG( "InitialSystemDefaultConfig NG \n" );
  7711. StopProcessingLoop ();
  7712. }
  7713. PRINTF_FUNC ( "CheckConnectorTypeStatus and CheckIsAlternatvie \n" );
  7714. CheckGunTypeFromHw ();
  7715. CheckIsAlternatvieByModelName ();
  7716. InitialShareMemoryInfo ();
  7717. PRINTF_FUNC ( "InitialShareMemoryInfo OK. \n" );
  7718. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  7719. PRINTF_FUNC ( "ChargerType (IEC or UL) = IEC \n" );
  7720. else
  7721. PRINTF_FUNC ( "ChargerType (IEC or UL) = UL \n" );
  7722. ChangeLcmByIndex ( _LCM_INIT );
  7723. if ( ! CheckConnectorTypeStatus ())
  7724. isModelNameMatch = false;
  7725. Initialization ();
  7726. PRINTF_FUNC ( "===== Initial Complete ===== \n" );
  7727. PRINTF_FUNC ( "===== Spawn all Task and self test start ===== \n" );
  7728. SpawnTask ();
  7729. if ( ! isModelNameMatch)
  7730. {
  7731. // Module Name 與硬體對應不正確
  7732. PRINTF_FUNC ( "*** Module Name & HW info none match : Please check if the Model name and HW are matched. ***\n" );
  7733. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  7734. ChangeLcmByIndex ( _LCM_FIX );
  7735. sleep ( 3 );
  7736. KillTask ();
  7737. StopProcessingLoop ();
  7738. }
  7739. PRINTF_FUNC ( "Module Name & HW info correct. \n" );
  7740. CreateTimeoutFork ();
  7741. if ( ! DEBUG_ALSTON)
  7742. {
  7743. PRINTF_FUNC ( "Self test run..\n" );
  7744. SelfTestRun ();
  7745. }
  7746. else
  7747. {
  7748. sleep ( 3 );
  7749. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  7750. }
  7751. StopSystemTimeoutDet ();
  7752. if ( ! DEBUG_ALSTON && (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL
  7753. || ShmPsuData->Work_Step == _NO_WORKING
  7754. || ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES
  7755. || strcmp ( (char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " " ) == EQUAL))
  7756. {
  7757. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  7758. {
  7759. if ( ! DisplaySelfTestFailReason () && ShmDcCommonData->rebootCount < 1)
  7760. {
  7761. PRINTF_FUNC ( "=======Soft reboot for retry self-tets======= \n" );
  7762. SetupRebootCount ( 1 );
  7763. sleep ( 3 );
  7764. KillAllTask ();
  7765. system ( "/usr/bin/run_evse_restart.sh" );
  7766. }
  7767. else if (ShmDcCommonData->rebootCount < 1)
  7768. {
  7769. PRINTF_FUNC ( "=======Soft reboot for retry self-tets (Normal)======= \n" );
  7770. SetupRebootCount ( 1 );
  7771. sleep ( 3 );
  7772. KillAllTask ();
  7773. system ( "/usr/bin/run_evse_restart.sh" );
  7774. }
  7775. else
  7776. SetupRebootCount ( 0 );
  7777. }
  7778. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  7779. {
  7780. ChargingAlarmProcess ( gun_index );
  7781. }
  7782. //ChangeLcmByIndex(_LCM_FIX);
  7783. sleep ( 3 );
  7784. //if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  7785. //{
  7786. KillTaskExceptDetectTask ();
  7787. //}
  7788. //else
  7789. // KillTask();
  7790. StopProcessingLoop ();
  7791. }
  7792. else
  7793. {
  7794. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  7795. {
  7796. setChargerMode ( gun_index, SYS_MODE_IDLE );
  7797. }
  7798. SetupRebootCount ( 0 );
  7799. }
  7800. PRINTF_FUNC ( "===== Self Test Complete ===== \n" );
  7801. PRINTF_FUNC ( "===== Finally Create DB ===== \n" );
  7802. // Local DB
  7803. if (DB_Open ( localDb ) != PASS)
  7804. {
  7805. PRINTF_FUNC ( "DB_Open fail. \n" );
  7806. isDb_ready = false;
  7807. }
  7808. else
  7809. {
  7810. isDb_ready = true;
  7811. for (int _acIndex = 0; _acIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _acIndex ++)
  7812. {
  7813. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  7814. ac_chargingInfo [0]->IsAvailable = DB_Get_Operactive ( localDb, _acIndex + hasDc );
  7815. }
  7816. for (int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  7817. {
  7818. chargingInfo [_index]->IsAvailable = DB_Get_Operactive ( localDb, _index );
  7819. chargingInfo [_index]->PowerConsumption = DB_Get_PowerConsumption ( localDb, _index );
  7820. }
  7821. DB_Reboot_Record ( localDb );
  7822. }
  7823. if (NetworkDB_Open ( networkDb ) != PASS)
  7824. {
  7825. PRINTF_FUNC ( "NetworkDB_Open fail. \n" );
  7826. }
  7827. ChangeLcmByIndex ( _LCM_IDLE );
  7828. sleep ( 1 );
  7829. CreateRfidFork ();
  7830. sleep ( 1 );
  7831. if ( ! DEBUG_ALSTON)
  7832. {
  7833. //CreateCheckSystemTaskFork ();
  7834. // Create Watchdog
  7835. CreateWatchdog ();
  7836. }
  7837. PRINTF_FUNC ( "===== Create DB End ===== \n" );
  7838. PRINTF_FUNC ( "===== Charger info ===== " );
  7839. PRINTF_FUNC ( "SW Version = %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev );
  7840. PRINTF_FUNC ( "ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName );
  7841. CheckFwSlotStatusLog ();
  7842. AdjustChargerCurrent ();
  7843. GetTimespecFunc ( & _cmdMainPriority_time );
  7844. _ocppProfileChkFlag_1 = 0;
  7845. _ocppProfileChkFlag_2 = 0;
  7846. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.OcppServerURL, "" ) == EQUAL)
  7847. {
  7848. DEBUG_ERROR_MSG( "URL is empty kill Module_OcppBackend...\n" );
  7849. system ( "pkill OcppBackend" );
  7850. }
  7851. int _mainTimebuf = 0;
  7852. for (;;)
  7853. {
  7854. if (ShmDcCommonData->_isAutoRunTest != _isTestRun)
  7855. {
  7856. if (ShmDcCommonData->_isAutoRunTest == YES)
  7857. {
  7858. GetTimespecFunc ( & _autoRuntestTime );
  7859. }
  7860. _isTestRun = ShmDcCommonData->_isAutoRunTest;
  7861. }
  7862. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  7863. {
  7864. ocpp_ac_chk_availability_cmd ();
  7865. ocpp_ac_chk_unlock_cmd ();
  7866. if ((ac_chargingInfo [0]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE
  7867. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_CHARGING)
  7868. || (ac_chargingInfo [0]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  7869. && ac_chargingInfo [0]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7870. {
  7871. CheckAcSmartChargeProfile ();
  7872. }
  7873. }
  7874. ocpp_auto_response_BootNotification ();
  7875. ocpp_chk_reset_cmd ( NO );
  7876. if ( ! DEBUG_ALSTON)
  7877. ChkPrimaryStatus ();
  7878. if ((IsConnectorWholeIdle () || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX))
  7879. {
  7880. CheckFactoryConfigFunction ();
  7881. CheckFwUpdateFunction ();
  7882. }
  7883. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  7884. {
  7885. sleep ( 1 );
  7886. continue;
  7887. }
  7888. // OCPP 邏輯
  7889. ocpp_chk_remoteStart_cmd ();
  7890. //LWN_Zanobe
  7891. ocpp_get_LocalAuthorizeOffline_req ();
  7892. // 讀卡邏輯
  7893. ScannerCardProcess ();
  7894. // 當 AC 沒有搭上時,清除一些錯誤碼
  7895. ClearAlarmCodeWhenAcOff ();
  7896. // 確認是否要回到充電中的槍畫面邏輯判斷
  7897. CheckReturnToChargingConn ();
  7898. if ((_acgunIndex > 0 && isDetectPlugin () && ! isCardScan))
  7899. {
  7900. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  7901. }
  7902. _mainTimebuf = GetTimeoutValue ( & _cmdMainPriority_time );
  7903. if (_mainTimebuf < 0)
  7904. GetTimespecFunc ( & _cmdMainPriority_time );
  7905. if (GetTimeoutValue ( & _cmdMainPriority_time ) >= 5)
  7906. {
  7907. CheckTask ();
  7908. Run4gResetProc ();
  7909. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  7910. {
  7911. if ((chargingInfo [_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE
  7912. && chargingInfo [_index]->SystemStatus <= SYS_MODE_CHARGING)
  7913. || (chargingInfo [_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  7914. && chargingInfo [_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7915. {
  7916. if (chargingInfo [_index]->SystemStatus == SYS_MODE_CHARGING
  7917. && ((_index == 0 && _ocppProfileChkFlag_1 == 12)
  7918. || (_index == 1 && _ocppProfileChkFlag_2 == 12)))
  7919. {
  7920. ocpp_lift_profileReq_cmd ( _index );
  7921. gunOutputVol [_index] = chargingInfo [_index]->PresentChargingVoltage;
  7922. if (_index == 0)
  7923. _ocppProfileChkFlag_1 = 0;
  7924. else if (_index == 1)
  7925. _ocppProfileChkFlag_2 = 0;
  7926. }
  7927. else if (chargingInfo [_index]->SystemStatus != SYS_MODE_CHARGING)
  7928. {
  7929. ocpp_lift_profileReq_cmd ( _index );
  7930. if (_index == 0)
  7931. _ocppProfileChkFlag_1 = 0;
  7932. else if (_index == 1)
  7933. _ocppProfileChkFlag_2 = 0;
  7934. }
  7935. else
  7936. {
  7937. if (_index == 0)
  7938. _ocppProfileChkFlag_1 ++;
  7939. else if (_index == 1)
  7940. _ocppProfileChkFlag_2 ++;
  7941. }
  7942. if (chargingInfo [_index]->SystemStatus >= SYS_MODE_CHARGING
  7943. && chargingInfo [_index]->SystemStatus <= SYS_MODE_ALARM)
  7944. {
  7945. DB_Update_PowerConsumption ( localDb, _index, chargingInfo [_index]->PowerConsumption );
  7946. }
  7947. }
  7948. if (chargingInfo [_index]->SystemStatus == SYS_MODE_RESERVATION)
  7949. {
  7950. if (opcc_chk_reserve_expired ( _index ))
  7951. {
  7952. chargingInfo [_index]->SystemStatus = SYS_MODE_IDLE;
  7953. }
  7954. }
  7955. // OTP
  7956. CheckConnectorOTP ( _index );
  7957. }
  7958. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index ++)
  7959. {
  7960. if ((ac_chargingInfo [_index]->SystemStatus >= SYS_MODE_PREPARING
  7961. && ac_chargingInfo [_index]->SystemStatus <= SYS_MODE_CHARGING))
  7962. {
  7963. if (ac_chargingInfo [_index]->SystemStatus == SYS_MODE_CHARGING
  7964. && _ac_ocppProfileChkFlag == 12)
  7965. {
  7966. ocpp_ac_lift_profileReq_cmd ( _index );
  7967. _ac_ocppProfileChkFlag = 0;
  7968. }
  7969. else if (ac_chargingInfo [_index]->SystemStatus != SYS_MODE_CHARGING)
  7970. {
  7971. ocpp_ac_lift_profileReq_cmd ( _index );
  7972. _ac_ocppProfileChkFlag = 0;
  7973. }
  7974. else
  7975. _ac_ocppProfileChkFlag ++;
  7976. }
  7977. }
  7978. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling && strcmp (
  7979. (char *) ShmSysConfigAndInfo->SysConfig.OcppServerURL, "" ) != EQUAL
  7980. && strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "" ) != EQUAL)
  7981. CheckOcppCostAndPrice ();
  7982. GetTimespecFunc ( & _cmdMainPriority_time );
  7983. }
  7984. if (ShmSysConfigAndInfo->SysConfig.ModelName [3] == 'M' || ShmSysConfigAndInfo->SysConfig.ModelName [3] == 'L')
  7985. CheckStatusOfMeter ();
  7986. // 確認當前錯誤 Level = 2 ?
  7987. ReviewCriticalAlarm ();
  7988. errCollect.GunErrMessage = 0;
  7989. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  7990. {
  7991. CheckGpioInStatus ();
  7992. // 確認系統的錯誤狀態
  7993. CheckSystemErrorFunction ( gun_index );
  7994. // 確認 Relay Welding or Driving Fault
  7995. CheckRelayWeldingOrDrivingFault ( gun_index );
  7996. // 收集各槍的錯誤狀態
  7997. CollectError ( gun_index );
  7998. ocpp_chk_reserved_cmd ( gun_index );
  7999. ocpp_chk_availability_cmd ( gun_index );
  8000. ocpp_chk_unlock_cmd ( gun_index );
  8001. if ((chargingInfo [gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE
  8002. && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CHARGING)
  8003. || (chargingInfo [gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  8004. && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8005. {
  8006. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_CHARGING)
  8007. {
  8008. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  8009. if (chargingInfo [gun_index]->PresentChargingVoltage >= gunOutputVol [gun_index] + 10
  8010. || chargingInfo [gun_index]->PresentChargingVoltage <= gunOutputVol [gun_index] - 10)
  8011. {
  8012. if (gun_index == 0)
  8013. _ocppProfileChkFlag_1 = 12;
  8014. else if (gun_index == 1)
  8015. _ocppProfileChkFlag_2 = 12;
  8016. gunOutputVol [gun_index] = chargingInfo [gun_index]->PresentChargingVoltage;
  8017. }
  8018. }
  8019. CheckSmartChargeProfile ( gun_index );
  8020. }
  8021. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  8022. switch (chargingInfo [gun_index]->SystemStatus)
  8023. {
  8024. case SYS_MODE_IDLE :
  8025. case SYS_MODE_RESERVATION :
  8026. case SYS_MODE_MAINTAIN :
  8027. case SYS_MODE_FAULT :
  8028. {
  8029. if ((chargingInfo [gun_index]->SystemStatus == SYS_MODE_IDLE
  8030. || chargingInfo [gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  8031. && isModeChange ( gun_index ))
  8032. {
  8033. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_IDLE)
  8034. {
  8035. PRINTF_FUNC ( "================= S_IDLE (%x) ================ \n", gun_index );
  8036. chargingInfo [gun_index]->ReservationId = - 1;
  8037. }
  8038. else if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  8039. {
  8040. PRINTF_FUNC ( "================= S_RESERVATION (%x) ================ \n", gun_index );
  8041. PRINTF_FUNC ( "================= Id : %s ================ \n",
  8042. ShmDcCommonData->_reserved_UserId [gun_index] );
  8043. }
  8044. chargingInfo [gun_index]->PresentChargedDuration = 0;
  8045. chargingInfo [gun_index]->RemainChargingDuration = 0;
  8046. chargingInfo [gun_index]->PresentChargingVoltage = 0;
  8047. chargingInfo [gun_index]->PresentChargingCurrent = 0;
  8048. strcpy ( (char *) chargingInfo [gun_index]->StartDateTime, "00:00:00" );
  8049. strcpy ( (char *) chargingInfo [gun_index]->StopDateTime, "00:00:00" );
  8050. strcpy ( (char *) chargingInfo [gun_index]->StartUserId, "" );
  8051. strcpy ( (char *) chargingInfo [gun_index]->EVCCID, "" );
  8052. ClearEnergyTimePeriod ( gun_index );
  8053. gunOutputVol [gun_index] = 0.0;
  8054. ocpp_clear_stopReason ( gun_index );
  8055. ocpp_auto_response_StartTransationConf ( gun_index );
  8056. ShmDcCommonData->startTransactionFlag [gun_index] = START_TRANSATION_STATUS_WAIT;
  8057. //LWN_Zanobe
  8058. isSendStartTransReq [gun_index] = false;
  8059. }
  8060. if (chargingInfo [gun_index]->IsAvailable == NO)
  8061. {
  8062. setChargerMode ( gun_index, SYS_MODE_MAINTAIN );
  8063. }
  8064. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  8065. {
  8066. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8067. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8068. else if (chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_IDLE
  8069. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_RESERVATION
  8070. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_MAINTAIN
  8071. && chargingInfo [ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_FAULT)
  8072. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8073. ClearDetectPluginFlag ();
  8074. ocpp_set_errCode_cmd ( gun_index );
  8075. setChargerMode ( gun_index, SYS_MODE_FAULT );
  8076. }
  8077. else
  8078. {
  8079. ChangeToMaxChargingMode ();
  8080. if (PrecheckIsPass ( gun_index ))
  8081. {
  8082. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_FAULT)
  8083. {
  8084. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8085. setChargerMode ( gun_index, SYS_MODE_IDLE );
  8086. }
  8087. // if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  8088. // {
  8089. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8090. // ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  8091. // }
  8092. // else
  8093. {
  8094. // Idle 正常程序起點
  8095. // 判斷是否有啟用檢查插槍
  8096. if (isDetectPlugin ())
  8097. {
  8098. // 卡號驗證成功後,等待充電槍插入充電車
  8099. if (chargingInfo [gun_index]->RemoteStartFlag == YES)
  8100. {
  8101. if (chargingInfo [gun_index]->ConnectorPlugIn == YES
  8102. && chargingInfo [gun_index]->IsAvailable)
  8103. {
  8104. PRINTF_FUNC ( "Conn %d Start Charging : Remote \n", gun_index );
  8105. chargingInfo [gun_index]->RemoteStartFlag = NO;
  8106. chargingInfo [gun_index]->isRemoteStart = YES;
  8107. ChangeGunSelectByIndex ( gun_index );
  8108. AddPlugInTimes ( gun_index );
  8109. setChargerMode ( gun_index, SYS_MODE_MODE_REASSIGN_CHECK );
  8110. strcpy ( (char *) chargingInfo [gun_index]->StartUserId, "" );
  8111. ClearDetectPluginFlag ();
  8112. continue;
  8113. }
  8114. }
  8115. else if (chargingInfo [gun_index]->ReservedStartFlag == YES)
  8116. {
  8117. if (chargingInfo [gun_index]->ConnectorPlugIn == YES && chargingInfo [gun_index]->IsAvailable)
  8118. {
  8119. chargingInfo [gun_index]->ReservedStartFlag = NO;
  8120. ChangeGunSelectByIndex ( gun_index );
  8121. AddPlugInTimes ( gun_index );
  8122. strcpy ( (char *) chargingInfo [gun_index]->StartUserId,
  8123. (char *) ShmDcCommonData->_reserved_UserId [gun_index] );
  8124. PRINTF_FUNC ( "Conn %d Start Charging : Reserved CardNumber = %s \n", gun_index,
  8125. chargingInfo [gun_index]->StartUserId );
  8126. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  8127. strcpy ( (char *) ShmDcCommonData->_reserved_UserId [gun_index], "" );
  8128. // 當前操作的槍號,進入 Preparing
  8129. setChargerMode ( gun_index, SYS_MODE_MODE_REASSIGN_CHECK );
  8130. ClearDetectPluginFlag ();
  8131. continue;
  8132. }
  8133. }
  8134. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  8135. {
  8136. if (chargingInfo [gun_index]->ConnectorPlugIn == YES
  8137. && chargingInfo [gun_index]->IsAvailable
  8138. && chargingInfo [gun_index]->SystemStatus == SYS_MODE_IDLE)
  8139. {
  8140. ChangeGunSelectByIndex ( gun_index );
  8141. AddPlugInTimes ( gun_index );
  8142. strcpy ( (char *) chargingInfo [gun_index]->StartUserId,
  8143. (char *) ShmSysConfigAndInfo->SysConfig.UserId );
  8144. PRINTF_FUNC ( "Conn %d Start Charging : RFID CardNumber = %s \n", gun_index,
  8145. chargingInfo [gun_index]->StartUserId );
  8146. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  8147. // 當前操作的槍號,進入 Preparing
  8148. setChargerMode ( gun_index, SYS_MODE_MODE_REASSIGN_CHECK );
  8149. ClearDetectPluginFlag ();
  8150. continue;
  8151. }
  8152. }
  8153. if ( ! isCardScan)
  8154. {
  8155. // LCM => Waiting for plugging
  8156. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8157. }
  8158. }
  8159. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE
  8160. && (chargingInfo [gun_index]->ConnectorPlugIn == YES
  8161. && chargingInfo [gun_index]->IsAvailable))
  8162. {
  8163. PRINTF_FUNC ( "Conn %d Start Charging : Plug and charing \n" );
  8164. bool isCanStartChargingFlag = GetStartChargingByAlterMode ( gun_index );
  8165. if (isCanStartChargingFlag)
  8166. {
  8167. ChangeGunSelectByIndex ( gun_index );
  8168. AddPlugInTimes ( gun_index );
  8169. setChargerMode ( gun_index, SYS_MODE_MODE_REASSIGN_CHECK );
  8170. ClearDetectPluginFlag ();
  8171. strncpy ( (char *) ShmOCPP16Data->StartTransaction [gun_index].IdTag,
  8172. (char *) ShmSysConfigAndInfo->SysConfig.SerialNumber,
  8173. sizeof(ShmOCPP16Data->StartTransaction [gun_index].IdTag) );
  8174. continue;
  8175. }
  8176. }
  8177. else
  8178. {
  8179. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8180. {
  8181. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8182. {
  8183. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8184. }
  8185. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE
  8186. && ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_WAIT_FOR_PLUG)
  8187. {
  8188. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8189. }
  8190. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  8191. }
  8192. }
  8193. } // Idle 正常程序終點
  8194. }
  8195. else
  8196. {
  8197. setChargerMode ( gun_index, SYS_MODE_FAULT );
  8198. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8199. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8200. }
  8201. ReleaseSysAlarmCode ( gun_index );
  8202. }
  8203. }
  8204. break;
  8205. case SYS_MODE_MODE_REASSIGN_CHECK :
  8206. {
  8207. if (isModeChange ( gun_index ))
  8208. {
  8209. PRINTF_FUNC ( "================= S_REASSIGN_CHECK (%x) ================ \n", gun_index );
  8210. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8211. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  8212. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  8213. StopSystemTimeoutDet ();
  8214. }
  8215. bool isRessign = false;
  8216. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  8217. {
  8218. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  8219. {
  8220. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index ++)
  8221. {
  8222. if (chargingInfo [index]->SystemStatus != SYS_MODE_MODE_REASSIGN_CHECK)
  8223. {
  8224. if (((chargingInfo [index]->SystemStatus >= SYS_MODE_PREPARING
  8225. && chargingInfo [index]->SystemStatus <= SYS_MODE_CHARGING)
  8226. || (chargingInfo [index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0
  8227. && chargingInfo [index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8228. && chargingInfo [index]->SystemStatus != SYS_MODE_MAINTAIN
  8229. && chargingInfo [index]->SystemStatus != SYS_MODE_RESERVATION)
  8230. {
  8231. // A 槍在充電過程中 - 進行分配動作
  8232. PRINTF_FUNC ( "======= Max -> Aver : Preparing. (Step 1) ======= \n" );
  8233. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  8234. isRessign = true;
  8235. break;
  8236. }
  8237. else
  8238. {
  8239. // A 槍已結束充電 - 直接回 B 槍最大充
  8240. PRINTF_FUNC ( "======= Max -> Aver : Charging mode = MAX (Keep) ======= \n" );
  8241. //ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  8242. break;
  8243. }
  8244. }
  8245. }
  8246. }
  8247. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  8248. {
  8249. // 如果在切換最大充的過程中,需等待最大充切換完成後,再走均充流程
  8250. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  8251. {
  8252. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP
  8253. && ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  8254. {
  8255. PRINTF_FUNC ( "======= Aver -> Max : Aver -> Max : Current Balancing. (Step 14) ======= \n" );
  8256. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  8257. }
  8258. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  8259. {
  8260. PRINTF_FUNC ( "======= Aver -> Max : Charging mode = MAX ======= \n" );
  8261. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  8262. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  8263. continue;
  8264. }
  8265. }
  8266. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8267. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8268. continue;
  8269. }
  8270. }
  8271. if (isRessign)
  8272. setChargerMode ( gun_index, SYS_MODE_REASSIGN );
  8273. else
  8274. setChargerMode ( gun_index, SYS_MODE_PREPARING );
  8275. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8276. {
  8277. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8278. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8279. }
  8280. }
  8281. break;
  8282. case SYS_MODE_REASSIGN :
  8283. {
  8284. if (isModeChange ( gun_index ))
  8285. {
  8286. PRINTF_FUNC ( "================== S_REASSIGN (%x) ================ \n", gun_index );
  8287. }
  8288. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  8289. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  8290. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A
  8291. && ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  8292. {
  8293. PRINTF_FUNC ( "======= Max -> Aver : Charging mode = AVER ======= \n" );
  8294. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  8295. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  8296. }
  8297. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  8298. {
  8299. if ((ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO
  8300. && ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  8301. || (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES
  8302. && ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX))
  8303. {
  8304. setChargerMode ( gun_index, SYS_MODE_PREPARING );
  8305. }
  8306. }
  8307. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8308. {
  8309. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8310. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8311. }
  8312. }
  8313. break;
  8314. case SYS_MODE_PREPARING :
  8315. {
  8316. if (isModeChange ( gun_index ))
  8317. {
  8318. PRINTF_FUNC ( "=================== S_PREPARNING (%x) =============== \n", gun_index );
  8319. StopGunInfoTimeoutDet ( gun_index );
  8320. StartGunInfoTimeoutDet ( gun_index, Timeout_Preparing );
  8321. }
  8322. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10
  8323. && ShmPsuData->Work_Step == _WORK_CHARGING
  8324. && GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 5)
  8325. {
  8326. setChargerMode ( gun_index, SYS_MODE_PREPARE_FOR_EV );
  8327. }
  8328. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2)
  8329. {
  8330. bool oughtAver = true;
  8331. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index ++)
  8332. {
  8333. // 共同進入充電邏輯
  8334. if (chargingInfo [index]->SystemStatus != SYS_MODE_PREPARING)
  8335. {
  8336. oughtAver = false;
  8337. break;
  8338. }
  8339. }
  8340. if (oughtAver)
  8341. {
  8342. PRINTF_FUNC ( "********* Smart Charging : _REASSIGNED_NONE (AVER) ********** \n" );
  8343. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  8344. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  8345. }
  8346. }
  8347. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8348. {
  8349. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8350. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8351. }
  8352. }
  8353. break;
  8354. case SYS_MODE_PREPARE_FOR_EV : // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  8355. {
  8356. if (isModeChange ( gun_index ))
  8357. {
  8358. PRINTF_FUNC ( "================== S_PREPARING_FOR_EV (%x) ================ \n", gun_index );
  8359. _presentChargingTimeBuf [gun_index] = 0;
  8360. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8361. StopGunInfoTimeoutDet ( gun_index );
  8362. StartGunInfoTimeoutDet ( gun_index, Timeout_EvChargingDet );
  8363. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE)
  8364. {
  8365. // Start Transation
  8366. ocpp_set_startTransation_cmd ( gun_index );
  8367. GetTimespecFunc ( & _startTransation_time [gun_index] );
  8368. }
  8369. }
  8370. //LWN_Zanobe
  8371. //Connect with the backend.
  8372. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE
  8373. && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == YES)
  8374. {
  8375. //Already to get the EVCCID, and authorize complite, send the Start Tansaction req.
  8376. if (strcmp ( (char *) chargingInfo [gun_index]->EVCCID, "" ) != EQUAL
  8377. //&& strcmp ( (char *) chargingInfo [gun_index]->StartUserId, "" ) != EQUAL //LWADD
  8378. && ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZ_COMP
  8379. && ! isSendStartTransReq [gun_index])
  8380. {
  8381. StopSystemTimeoutDet ();
  8382. ocpp_set_startTransation_cmd ( gun_index );
  8383. GetTimespecFunc ( & _startTransation_time [gun_index] );
  8384. isSendStartTransReq [gun_index] = true;
  8385. }
  8386. }
  8387. //LWADD
  8388. //LWN_Zanobe
  8389. //需一般刷卡驗證、隨插即充 + 與後台連線, 才須chk start transaction conf.
  8390. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE
  8391. || (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE
  8392. && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == YES
  8393. && ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZ_COMP))
  8394. {
  8395. ShmDcCommonData->startTransactionFlag [gun_index] = ocpp_is_resPass_StartTransactionConf ( gun_index );
  8396. if (ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_PASS)
  8397. {
  8398. if (ocpp_chk_invalid_id_cmd ( gun_index ))
  8399. ShmDcCommonData->startTransactionFlag [gun_index] = START_TRANSATION_STATUS_FAIL;
  8400. }
  8401. else
  8402. {
  8403. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE)
  8404. {
  8405. //Disconnect with the Backend.
  8406. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  8407. {
  8408. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING
  8409. || ! ocpp_get_startTransation_req ( gun_index )
  8410. || strcmp ( (char *) chargingInfo [gun_index]->StartUserId, "" ) != EQUAL
  8411. || _isTestRun)
  8412. ShmDcCommonData->startTransactionFlag [gun_index] = START_TRANSATION_STATUS_PASS;
  8413. }
  8414. }
  8415. }
  8416. }
  8417. // 小板停止
  8418. ChkConnectorBoardStop ( gun_index );
  8419. if (ocpp_chk_remoteStop_cmd ( gun_index ) == YES
  8420. || WifiScheduleStop ( gun_index )
  8421. || ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_FAIL
  8422. || (ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_WAIT
  8423. && GetTimeoutValue ( & _startTransation_time [gun_index] ) > 45))
  8424. {
  8425. //LWN_Zanobe
  8426. //一般驗證流程
  8427. //隨插即充 + 驗證完成 + 取完EVCCID
  8428. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE
  8429. || (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE
  8430. //&& strcmp ( (char *) chargingInfo [gun_index]->EVCCID, "" ) != EQUAL //LWADD
  8431. && ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZ_COMP
  8432. && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == YES))
  8433. {
  8434. // 後臺要求停止
  8435. ChargingTerminalProcess ( gun_index );
  8436. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  8437. if (ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_FAIL
  8438. || ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_WAIT)
  8439. {
  8440. if (ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_FAIL)
  8441. PRINTF_FUNC ( "StartTransaction req fail. (%d) \n", gun_index );
  8442. if (ShmDcCommonData->startTransactionFlag [gun_index] == START_TRANSATION_STATUS_WAIT)
  8443. PRINTF_FUNC ( "StartTransaction req wait. (%d) \n", gun_index );
  8444. }
  8445. }
  8446. else if (ShmDcCommonData->PlugAndCharge_AuthStatus == _LCM_AUTHORIZ_FAIL)
  8447. {
  8448. //Authorize fail, or disconnect with the Backend, switch to the terminate state.
  8449. ChargingTerminalProcess ( gun_index );
  8450. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  8451. }
  8452. }
  8453. // 檢查車端的槍鎖是否為鎖上
  8454. if (isEvGunLocked ( gun_index ) == YES)
  8455. {
  8456. setChargerMode ( gun_index, SYS_MODE_PREPARE_FOR_EVSE );
  8457. ShmDcCommonData->PlugAndCharge_AuthStatus = _LCM_NONE;
  8458. }
  8459. // LCM => Pre-charging
  8460. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8461. {
  8462. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8463. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8464. }
  8465. }
  8466. break;
  8467. case SYS_MODE_PREPARE_FOR_EVSE : // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  8468. {
  8469. if (isModeChange ( gun_index ))
  8470. {
  8471. PRINTF_FUNC ( "================== S_PREPARING_FOR_EVSE (%x) ================ \n", gun_index );
  8472. StopGunInfoTimeoutDet ( gun_index );
  8473. StartGunInfoTimeoutDet ( gun_index, Timeout_EvseChargingDet );
  8474. }
  8475. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  8476. {
  8477. // 檢查樁端的 GFD 結果
  8478. if (isPrechargeStatus_chademo ( gun_index ) == 8)
  8479. {
  8480. // 當前操作的槍號,進入 Charging
  8481. setChargerMode ( gun_index, SYS_MODE_CHARGING );
  8482. }
  8483. }
  8484. else if (chargingInfo [gun_index]->Type == _Type_GB)
  8485. {
  8486. // 檢查樁端的 GFD 結果
  8487. if (isPrechargeStatus_gb ( gun_index ) > 9)
  8488. {
  8489. setChargerMode ( gun_index, SYS_MODE_CHARGING );
  8490. }
  8491. }
  8492. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  8493. {
  8494. // 檢查樁端的 GFD 結果
  8495. if ((chargingInfo [gun_index]->GroundFaultStatus == GFD_PASS
  8496. || chargingInfo [gun_index]->GroundFaultStatus == GFD_WARNING))
  8497. {
  8498. setChargerMode ( gun_index, SYS_MODE_CCS_PRECHARGE_STEP0 );
  8499. }
  8500. }
  8501. RecordGfdResult ( gun_index );
  8502. // 小板停止
  8503. ChkConnectorBoardStop ( gun_index );
  8504. // 後台要求停止
  8505. ChkBackendStop ( gun_index );
  8506. // LCM => Pre-charging
  8507. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8508. {
  8509. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8510. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8511. }
  8512. }
  8513. break;
  8514. case SYS_MODE_CHARGING : // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  8515. {
  8516. if (isModeChange ( gun_index ))
  8517. {
  8518. PRINTF_FUNC ( "================== S_CHARGING (%x) ================ \n", gun_index );
  8519. StopGunInfoTimeoutDet ( gun_index );
  8520. ftime ( & startChargingTime [gun_index] );
  8521. ocpp_clear_idTag_cmd ( gun_index );
  8522. ChangeStartOrStopDateTime ( YES, gun_index );
  8523. if (_isTestRun == YES)
  8524. GetTimespecFunc ( & _autoRuntestTime );
  8525. }
  8526. if (_isTestRun == YES)
  8527. {
  8528. if (GetTimeoutValue ( & _autoRuntestTime ) > 60)
  8529. {
  8530. ChargingTerminalProcess ( gun_index );
  8531. }
  8532. }
  8533. SavePresentChargedDuration ( gun_index );
  8534. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  8535. {
  8536. if (ShmDcCommonData->minOutputCur != 0 && chargingInfo [gun_index]->PresentChargedDuration >= 10
  8537. && chargingInfo [gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8538. {
  8539. // UCP
  8540. RecordAlarmCode ( gun_index, "012320" );
  8541. ChargingTerminalProcess ( gun_index );
  8542. }
  8543. }
  8544. else if (chargingInfo [gun_index]->Type == _Type_GB)
  8545. {
  8546. if (ShmDcCommonData->minOutputCur != 0 && chargingInfo [gun_index]->PresentChargedDuration >= 10
  8547. && chargingInfo [gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8548. {
  8549. // UCP
  8550. RecordAlarmCode ( gun_index, "012322" );
  8551. ChargingTerminalProcess ( gun_index );
  8552. }
  8553. }
  8554. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  8555. {
  8556. if (ShmDcCommonData->minOutputCur != 0 && chargingInfo [gun_index]->PresentChargedDuration >= 10
  8557. && chargingInfo [gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8558. {
  8559. // UCP
  8560. RecordAlarmCode ( gun_index, "012321" );
  8561. ChargingTerminalProcess ( gun_index );
  8562. }
  8563. }
  8564. RecordGfdResult ( gun_index );
  8565. // 小板停止
  8566. ChkConnectorBoardStop ( gun_index );
  8567. // 後台要求停止
  8568. ChkBackendStop ( gun_index );
  8569. //LWN_Zanobe
  8570. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8571. {
  8572. // Disconnect with the Backend, and not allow charging.
  8573. if (ShmOCPP16Data->OcppConnStatus == NO && _OCPP_LocalAuthorizeOffline == NO)
  8574. {
  8575. PRINTF_FUNC ( "S_CHARGING=======Stop (Ocpp)=========================== %f \n",
  8576. (chargingInfo [gun_index]->EvBatterytargetVoltage * 10) );
  8577. PRINTF_FUNC ( "Disconnect with the Backend. Not allow for charging. \n" );
  8578. ChargingTerminalProcess ( gun_index );
  8579. }
  8580. }
  8581. // 設定停止
  8582. ChkConfigurationStop ( gun_index );
  8583. // LCM => Charging
  8584. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8585. {
  8586. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  8587. if ( ! stopChargingChkByCard)
  8588. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8589. }
  8590. }
  8591. break;
  8592. case SYS_MODE_ALARM :
  8593. case SYS_MODE_TERMINATING :
  8594. {
  8595. if (isModeChange ( gun_index ))
  8596. {
  8597. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip
  8598. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  8599. ocpp_set_stopReason_by_cmd ( gun_index, "EmergencyStop" );
  8600. else
  8601. ocpp_set_stopReason_by_cmd ( gun_index, "Local" );
  8602. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_ALARM)
  8603. {
  8604. PRINTF_FUNC ( "================== S_ALARM (%x) ================ \n", gun_index );
  8605. if (strcmp ( (char *) chargingInfo [gun_index]->StartDateTime, "" ) != EQUAL)
  8606. {
  8607. ocpp_auto_response_StartTransationConf ( gun_index );
  8608. ocpp_set_stopTransation_cmd ( gun_index );
  8609. }
  8610. TheEndCharging ( gun_index );
  8611. }
  8612. else
  8613. {
  8614. ocpp_set_errCode_cmd ( gun_index );
  8615. PRINTF_FUNC ( "================== S_TERMINATING (%x) ================ \n", gun_index );
  8616. }
  8617. StopGunInfoTimeoutDet ( gun_index );
  8618. }
  8619. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_ALARM)
  8620. ChangeToMaxChargingMode ();
  8621. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  8622. {
  8623. }
  8624. else if (chargingInfo [gun_index]->Type == _Type_GB)
  8625. {
  8626. }
  8627. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  8628. {
  8629. }
  8630. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_ALARM)
  8631. {
  8632. if (chargingInfo [gun_index]->ConnectorPlugIn == NO && GetTimeoutValue (
  8633. & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  8634. //if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  8635. {
  8636. setChargerMode ( gun_index, SYS_MODE_IDLE );
  8637. }
  8638. else if (_isTestRun == YES && GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  8639. {
  8640. setChargerMode ( gun_index, SYS_MODE_IDLE );
  8641. }
  8642. }
  8643. else
  8644. {
  8645. if (chargingInfo [gun_index]->Type == _Type_Chademo)
  8646. {
  8647. if (isEvGunLocked ( gun_index ) == NO || isPrechargeStatus_chademo ( gun_index ) <= 0)
  8648. {
  8649. setChargerMode ( gun_index, SYS_MODE_COMPLETE );
  8650. }
  8651. }
  8652. else if (chargingInfo [gun_index]->Type == _Type_GB)
  8653. {
  8654. if (isEvGunLocked ( gun_index ) == NO || isPrechargeStatus_gb ( gun_index ) <= 0)
  8655. {
  8656. setChargerMode ( gun_index, SYS_MODE_COMPLETE );
  8657. }
  8658. }
  8659. else if (chargingInfo [gun_index]->Type == _Type_CCS)
  8660. {
  8661. if (isEvGunLocked ( gun_index ) == NO && (isPrechargeStatus_ccs ( gun_index ) >= 53
  8662. || isPrechargeStatus_ccs ( gun_index ) == 0 || isPrechargeStatus_ccs ( gun_index ) == 13
  8663. || isPrechargeStatus_ccs ( gun_index ) == 14))
  8664. {
  8665. setChargerMode ( gun_index, SYS_MODE_COMPLETE );
  8666. }
  8667. }
  8668. }
  8669. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8670. {
  8671. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  8672. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8673. }
  8674. }
  8675. break;
  8676. case SYS_MODE_COMPLETE :
  8677. {
  8678. if (isModeChange ( gun_index ))
  8679. {
  8680. PRINTF_FUNC ( "================== S_COMPLETE (%x) ================ \n", gun_index );
  8681. if (strcmp ( (char *) chargingInfo [gun_index]->StartDateTime, "" ) != EQUAL)
  8682. {
  8683. ocpp_set_errCode_cmd ( gun_index );
  8684. ocpp_auto_response_StartTransationConf ( gun_index );
  8685. ocpp_set_stopTransation_cmd ( gun_index );
  8686. }
  8687. TheEndCharging ( gun_index );
  8688. }
  8689. ChangeToMaxChargingMode ();
  8690. if (chargingInfo [gun_index]->ConnectorPlugIn == NO
  8691. && GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  8692. //if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  8693. {
  8694. setChargerMode ( gun_index, SYS_MODE_IDLE );
  8695. }
  8696. else if (_isTestRun == YES && GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  8697. {
  8698. setChargerMode ( gun_index, SYS_MODE_IDLE );
  8699. }
  8700. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8701. {
  8702. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  8703. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8704. }
  8705. }
  8706. break;
  8707. case SYS_MODE_CCS_PRECHARGE_STEP0 :
  8708. {
  8709. if (isModeChange ( gun_index ))
  8710. {
  8711. PRINTF_FUNC ( "================== S_CCS_PRECHARGE_ST0 (%x) ================ \n", gun_index );
  8712. StopGunInfoTimeoutDet ( gun_index );
  8713. StartGunInfoTimeoutDet ( gun_index, Timeout_ForCcsPrechargeDet );
  8714. }
  8715. RecordGfdResult ( gun_index );
  8716. // 小板停止
  8717. ChkConnectorBoardStop ( gun_index );
  8718. // 後台要求停止
  8719. ChkBackendStop ( gun_index );
  8720. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  8721. // 切換 D+ Relay to Precharge Relay
  8722. if (isPrechargeStatus_ccs ( gun_index ) == 39 || isPrechargeStatus_ccs ( gun_index ) == 40)
  8723. {
  8724. if (chargingInfo [gun_index]->RelayKPK2Status == YES && chargingInfo [gun_index]->PrechargeStatus != PRECHARGE_READY)
  8725. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  8726. {
  8727. if (isPrechargeStatus_ccs ( gun_index ) == 39)
  8728. PRINTF_FUNC ( "Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index,
  8729. isPrechargeStatus_ccs ( gun_index ) );
  8730. else if (isPrechargeStatus_ccs ( gun_index ) == 40)
  8731. PRINTF_FUNC ( "Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n",
  8732. gun_index, isPrechargeStatus_ccs ( gun_index ) );
  8733. chargingInfo [gun_index]->PrechargeStatus = PRECHARGE_READY;
  8734. }
  8735. }
  8736. else if (isPrechargeStatus_ccs ( gun_index ) == 45 || isPrechargeStatus_ccs ( gun_index ) == 46)
  8737. {
  8738. setChargerMode ( gun_index, SYS_MODE_CCS_PRECHARGE_STEP1 );
  8739. }
  8740. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8741. {
  8742. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8743. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8744. }
  8745. break;
  8746. }
  8747. case SYS_MODE_CCS_PRECHARGE_STEP1 :
  8748. {
  8749. if (isModeChange ( gun_index ))
  8750. {
  8751. PRINTF_FUNC ( "================== S_CCS_PRECHARGE_ST1 (%x) ================ \n", gun_index );
  8752. }
  8753. RecordGfdResult ( gun_index );
  8754. // 小板停止
  8755. ChkConnectorBoardStop ( gun_index );
  8756. // 後台要求停止
  8757. ChkBackendStop ( gun_index );
  8758. // 等待小板通知進入充電
  8759. // 切換 D+ Relay to Precharge Relay
  8760. if (chargingInfo [gun_index]->RelayK1K2Status == YES)
  8761. {
  8762. chargingInfo [gun_index]->PrechargeStatus = PRECHARGE_READY;
  8763. setChargerMode ( gun_index, SYS_MODE_CHARGING );
  8764. }
  8765. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8766. {
  8767. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8768. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8769. }
  8770. break;
  8771. }
  8772. }
  8773. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_IDLE
  8774. || chargingInfo [gun_index]->SystemStatus == SYS_MODE_RESERVATION
  8775. || chargingInfo [gun_index]->SystemStatus == SYS_MODE_MAINTAIN
  8776. || chargingInfo [gun_index]->SystemStatus == SYS_MODE_FAULT)
  8777. {
  8778. ocpp_chk_notificationMessage_cmd ( gun_index );
  8779. }
  8780. }
  8781. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  8782. {
  8783. ChangeLcmByIndex ( ShmSysConfigAndInfo->SysInfo.SystemPage );
  8784. }
  8785. else
  8786. {
  8787. bool dcPageRun = false;
  8788. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  8789. {
  8790. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE)
  8791. ChangeLcmByIndex ( _LCM_IDLE );
  8792. else if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_PREPARING)
  8793. ChangeLcmByIndex ( _LCM_PRE_CHARGE );
  8794. else if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_CHARGING)
  8795. ChangeLcmByIndex ( _LCM_CHARGING );
  8796. else if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_TERMINATING || ac_chargingInfo [0]->SystemStatus == SYS_MODE_COMPLETE)
  8797. ChangeLcmByIndex ( _LCM_COMPLETE );
  8798. else
  8799. dcPageRun = true;
  8800. }
  8801. else
  8802. dcPageRun = true;
  8803. if (dcPageRun)
  8804. ChangeLcmByIndex ( ShmSysConfigAndInfo->SysInfo.ConnectorPage );
  8805. }
  8806. bool isCharging = false;
  8807. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index ++)
  8808. {
  8809. AlarmCheck ( chargingInfo [gun_index]->Type );
  8810. if (_isTestRun == YES)
  8811. {
  8812. if (IsGunUsing ( gun_index ))
  8813. {
  8814. isCharging = true;
  8815. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_COMPLETE
  8816. || chargingInfo [gun_index]->SystemStatus == SYS_MODE_ALARM)
  8817. {
  8818. GetTimespecFunc ( & _autoRuntestTime );
  8819. }
  8820. }
  8821. }
  8822. }
  8823. if (_isTestRun == YES)
  8824. {
  8825. if ( ! isCharging)
  8826. {
  8827. if (GetTimeoutValue ( & _autoRuntestTime ) >= 60)
  8828. {
  8829. DetectPluginStart ();
  8830. GetTimespecFunc ( & _autoRuntestTime );
  8831. }
  8832. }
  8833. else
  8834. {
  8835. ClearDetectPluginFlag ();
  8836. }
  8837. }
  8838. write ( wtdFd, "a", 1 );
  8839. usleep ( whileLoopTime );
  8840. }
  8841. return FAIL;
  8842. }