main.c 299 KB

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