main.c 257 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2020年9月10日
  5. * Author: Wendell
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/mman.h>
  17. #include <linux/wireless.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> /*標準輸入輸出定義*/
  23. #include <stdlib.h> /*標準函數庫定義*/
  24. #include <unistd.h> /*Unix 標準函數定義*/
  25. #include <fcntl.h> /*檔控制定義*/
  26. #include <termios.h> /*PPSIX 終端控制定義*/
  27. #include <errno.h> /*錯誤號定義*/
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "../../define.h"
  36. #include "Config.h"
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. #include "timeout.h"
  40. #include "Module_EvComm.h"
  41. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  42. #define PASS 1
  43. #define FAIL -1
  44. #define BUFFER_SIZE 128
  45. #define YES 1
  46. #define NO 0
  47. #define ON 1
  48. #define OFF 0
  49. #define NORMAL 0
  50. #define ABNORMAL 1
  51. #define EQUAL 0
  52. #define BTN_RELEASE 0
  53. #define BTN_PRESS 1
  54. #define MAX_BUF 64
  55. #define MtdBlockSize 0x300000
  56. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  57. #define UPGRADE_FAN 0x02
  58. #define UPGRADE_RB 0x03
  59. #define UPGRADE_PRI 0x04
  60. #define UPGRADE_AC 0x05
  61. #define UPGRADE_LED 0x06
  62. #define UPGRADE_DORC1 0x07
  63. #define UPGRADE_DORC2 0x08
  64. #define UPGRADE_DDRB 0x09
  65. #define SYSTEM_MIN_VOL 150
  66. #define MIN_OUTPUT_CUR 0
  67. #define AC_OUTPUT_VOL 220
  68. #define NO_DEFINE 255
  69. #define DEFAULT_AC_INDEX 2
  70. #define PSU_MIN_CUR 100
  71. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  72. #define SELFTEST_TIMEOUT 60
  73. #define AUTHORIZE_TIMEOUT 30
  74. #define AUTHORIZE_COMP_TIMEOUT 1
  75. #define AUTHORIZE_FAIL_TIMEOUT 3
  76. #define AUTHORIZE_STOP_TIMEOUT 30
  77. #define RETURN_TO_CHARGING_PAGE 30
  78. #define GUN_PREPARE_TIMEOUT 30
  79. #define GUN_EV_WAIT_TIMEOUT 120
  80. #define GUN_EVSE_WAIT_TIMEOUT 60
  81. #define GUN_COMP_WAIT_TIMEOUT 10
  82. #define GUN_PRECHARGING_TIMEOUT 60
  83. #define REBOOT_TIMEOUT 10
  84. #define RESET_TIMEOUT 10
  85. #define OCPP_COST_REQ_INTERVAL 5
  86. #define DISPENSER_RECONNECTE_TIME 30
  87. #define DISPENSER_AUTH_COMP_TIME 3
  88. #define FORCE_BALANCE_TIME 5
  89. #define DISPENSER_UPGRADE_TIME 600
  90. #define AC_CONTACTOR_REOPEN_TIME 40
  91. #define PSU_FAILURE_RESUME_TIME 180
  92. #define MAX_AC_CONTACTOR_COUNT 3
  93. #define STANDBY_TIME 600
  94. #define FAST_STANDBY_TIME 30
  95. #define INDICATION_BLINK_INTERVAL 1000
  96. #define E4YOU_BLINK_INTERVAL 500
  97. #define WIRING_INFO_DELAY 30
  98. #define WAIT_REASSIGN_TIME 30 // unit: second
  99. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  100. #define MIN_WAIT_PREPARNING_TIME 5 // unit: second
  101. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  102. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  103. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  104. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  105. int whileLoopTime = 10000; // 10 ms
  106. int wtdFd = -1;
  107. byte _authorizeIndex = NO_DEFINE;
  108. BOOL _UpgradeNeedReboot = FALSE;
  109. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  110. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  111. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  112. {
  113. {0, 1, 0, 0},
  114. {1, 0, 0, 0},
  115. {0, 0, 0, 0},
  116. {0, 0, 0, 0}
  117. };
  118. //Location: 1 2 3 4
  119. // Gun: 1 3 4 2
  120. //Parallel: 1 2 3
  121. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  122. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  123. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  124. //{
  125. // {0, 3, 1, 2}, // group 0
  126. // {1, 0, 2, 3}, // group 1
  127. // {1, 3, 0, 2}, // group 2
  128. // {1, 3, 2, 0} // group 3
  129. //};
  130. //Location: 1 2 3 4
  131. // Gun: 1 4 3 2
  132. //Parallel: 1 2 3
  133. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  134. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  135. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  136. {
  137. {0, 3, 2, 1}, // group 0
  138. {1, 0, 3, 2}, // group 1
  139. {1, 3, 0, 2}, // group 2
  140. {1, 3, 2, 0} // group 3
  141. };
  142. bool IsAuthorizingMode();
  143. void ClearAuthorizedFlag();
  144. bool isDetectPlugin();
  145. void ClearDetectPluginFlag();
  146. int mystrcmp(unsigned char *p1, unsigned char *p2);
  147. long long DiffTimebWithNow(struct timeb ST);
  148. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  149. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  150. void ChargingTerminalProcess(byte gunIndex);
  151. void ChkPrimaryStatus();
  152. void StartSystemTimeoutDet(unsigned char flag);
  153. void StopSystemTimeoutDet();
  154. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  155. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  156. int StoreLogMsg_1(const char *fmt, ...);
  157. unsigned long GetTimeoutValue(struct timeval _sour_time);
  158. void gpio_set_value(unsigned int gpio, unsigned int value);
  159. void PRINTF_FUNC(char *string, ...);
  160. void ChangeGunSelectByIndex(byte sel);
  161. void ChargingAlarmProcess(byte gunIndex);
  162. void InformOcppErrOccur(byte codeType);
  163. void ReleaseAlarmCode(byte gunIndex);
  164. int DB_Open(sqlite3 *db);
  165. int DB_Insert_Record(sqlite3 *db, int gun_index);
  166. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  167. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  168. void InitialDHCP();
  169. void InitialDispenserDhcpServerConfig(void);
  170. void StartDispenserDhcpServer(void);
  171. void _SelfTestTimeout(void);
  172. void CheckConnectionTimeout(void);
  173. bool IsConnectorWholeIdle();
  174. void SetAcContactor(unsigned char OnOff);
  175. void UpdateErrorCodeToOcpp(byte index);
  176. bool CheckConnectorTypeStatus(void);
  177. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  178. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  179. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  180. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  181. struct StatusCodeData *ShmStatusCodeData;
  182. struct PsuData *ShmPsuData;
  183. struct CHAdeMOData *ShmCHAdeMOData;
  184. struct GBTData *ShmGBTData;
  185. struct CcsData *ShmCcsData;
  186. struct PrimaryMcuData *ShmPrimaryMcuData;
  187. struct FanModuleData *ShmFanModuleData;
  188. struct RelayModuleData *ShmRelayModuleData[2];
  189. struct LedModuleData *ShmLedModuleData;
  190. struct OCPP16Data *ShmOCPP16Data;
  191. ChargerInfoData *ShmChargerInfo;
  192. PsuPositionInfoData *ShmPsuPosition;
  193. PsuGroupingInfoData *ShmPsuGrouping;
  194. PsuGroupCollectionData *ShmGroupCollection;
  195. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  196. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  197. struct timeb startChargingTime[CONNECTOR_QUANTITY];
  198. struct timeb endChargingTime[CONNECTOR_QUANTITY];
  199. struct timeval _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  200. struct timeval _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  201. struct timeval _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  202. int chargingTime[CONNECTOR_QUANTITY];
  203. struct timeval _DispenserUpgrade_time;
  204. struct timeval _Standby_time;
  205. struct timeval _SystemStatus_Time[CONNECTOR_QUANTITY];
  206. unsigned char _PsuGroupAvailable[CONNECTOR_QUANTITY];
  207. bool _NeedReset4gWifi;
  208. struct timeval _4gWifiReset_time;
  209. // for initial index to check EV board type is correct
  210. byte _gunIndex = 0;
  211. byte _acgunIndex = 0;
  212. byte _chademoIndex = 0;
  213. byte _ccsIndex = 0;
  214. byte _gb_Index = 0;
  215. byte _ac_Index = 0;
  216. //byte bd0_1_status = 0;
  217. //byte bd0_2_status = 0;
  218. //byte bd1_1_status = 0;
  219. //byte bd1_2_status = 0;
  220. bool isCardScan = false;
  221. bool isModelNameMatch = true;
  222. //int rfidFd = -1;
  223. //char* rfidPortName = "/dev/ttyS2";
  224. char* fwVersion = "V1.00.00.0000.00";
  225. sqlite3 *localDb;
  226. bool isDb_ready;
  227. //================================================
  228. // initial can-bus
  229. //================================================
  230. int InitCanBus()
  231. {
  232. int s0,nbytes;
  233. struct timeval tv;
  234. struct ifreq ifr0;
  235. struct sockaddr_can addr0;
  236. system("/sbin/ip link set can0 down");
  237. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  238. system("/sbin/ip link set can0 up");
  239. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  240. tv.tv_sec = 0;
  241. tv.tv_usec = 10000;
  242. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  243. {
  244. #ifdef SystemLogMessage
  245. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  246. #endif
  247. }
  248. nbytes=40960;
  249. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  250. {
  251. #ifdef SystemLogMessage
  252. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  253. #endif
  254. }
  255. nbytes=40960;
  256. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  257. {
  258. #ifdef SystemLogMessage
  259. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  260. #endif
  261. }
  262. strcpy(ifr0.ifr_name, "can0" );
  263. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  264. addr0.can_family = AF_CAN;
  265. addr0.can_ifindex = ifr0.ifr_ifindex;
  266. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  267. return s0;
  268. }
  269. //================================================
  270. // initial uart port
  271. //================================================
  272. char *_priPortName = "/dev/ttyS1";
  273. char *_485PortName = "/dev/ttyS5";
  274. int InitComPort(byte target)
  275. {
  276. int fd;
  277. struct termios tios;
  278. if(target == UPGRADE_PRI)
  279. fd = open(_priPortName, O_RDWR);
  280. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  281. fd = open(_485PortName, O_RDWR);
  282. if(fd<=0)
  283. {
  284. #ifdef SystemLogMessage
  285. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  286. #endif
  287. return -1;
  288. }
  289. ioctl (fd, TCGETS, &tios);
  290. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  291. tios.c_lflag = 0;
  292. tios.c_iflag = 0;
  293. tios.c_oflag = 0;
  294. tios.c_cc[VMIN]=0;
  295. tios.c_cc[VTIME]=(unsigned char)1;
  296. tios.c_lflag=0;
  297. tcflush(fd, TCIFLUSH);
  298. ioctl (fd, TCSETS, &tios);
  299. return fd;
  300. }
  301. //=================================
  302. // Common routine
  303. //=================================
  304. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  305. {
  306. strncpy(dest, src + start, cnt);
  307. dest[cnt] = 0;
  308. }
  309. int InitWatchDog()
  310. {
  311. int fd;
  312. system("/usr/bin/fuser -k /dev/watchdog");
  313. sleep(1);
  314. system("echo V > /dev/watchdog");
  315. sleep(1);
  316. fd=open("/dev/watchdog", O_RDWR);
  317. if(fd<=0)
  318. {
  319. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  320. }
  321. return fd;
  322. }
  323. int StoreLogMsg_1(const char *fmt, ...)
  324. {
  325. char Buf[4096+256];
  326. char buffer[4096];
  327. va_list args;
  328. struct timeb SeqEndTime;
  329. struct tm *tm;
  330. va_start(args, fmt);
  331. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  332. va_end(args);
  333. memset(Buf,0,sizeof(Buf));
  334. ftime(&SeqEndTime);
  335. SeqEndTime.time = time(NULL);
  336. tm=localtime(&SeqEndTime.time);
  337. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  338. {
  339. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  340. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  341. printf("%s \n", Buf);
  342. }
  343. else
  344. {
  345. sprintf(Buf,"echo \'%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\' >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  346. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  347. buffer,
  348. tm->tm_year+1900,tm->tm_mon+1);
  349. system(Buf);
  350. }
  351. return rc;
  352. }
  353. unsigned long GetTimeoutValue(struct timeval _sour_time)
  354. {
  355. struct timeval _end_time;
  356. gettimeofday(&_end_time, NULL);
  357. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  358. }
  359. int mystrcmp(unsigned char *p1, unsigned char *p2)
  360. {
  361. while(*p1==*p2)
  362. {
  363. if(*p1=='\0' || *p2=='\0')
  364. break;
  365. p1++;
  366. p2++;
  367. }
  368. if(*p1=='\0' && *p2=='\0')
  369. return(PASS);
  370. else
  371. return(FAIL);
  372. }
  373. int DiffTimeb(struct timeb ST, struct timeb ET)
  374. {
  375. //return milli-second
  376. unsigned int StartTime,StopTime;
  377. StartTime=(unsigned int)ST.time;
  378. StopTime=(unsigned int)ET.time;
  379. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  380. return (StopTime-StartTime);
  381. }
  382. bool CheckTimeOut(struct timeb ST)
  383. {
  384. struct timeb ET;
  385. unsigned int StartTime, StopTime;
  386. ftime(&ET);
  387. StartTime = (unsigned int) ST.time;
  388. StopTime = (unsigned int) ET.time;
  389. return (StopTime > StartTime)? YES : NO;
  390. }
  391. void setChargerMode(byte gun_index, byte mode)
  392. {
  393. chargingInfo[gun_index]->SystemStatus = mode;
  394. }
  395. void PRINTF_FUNC(char *string, ...)
  396. {
  397. va_list args;
  398. char buffer[4096];
  399. va_start(args, string);
  400. vsnprintf(buffer, sizeof(buffer), string, args);
  401. va_end(args);
  402. DEBUG_INFO_MSG("%s \n", buffer);
  403. }
  404. long long DiffTimebWithNow(struct timeb ST)
  405. {
  406. //return milli-second
  407. struct timeb ET;
  408. long long StartTime,StopTime;
  409. ftime(&ET);
  410. StartTime=(long long)ST.time;
  411. StopTime=(long long)ET.time;
  412. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  413. }
  414. /*
  415. //==========================================
  416. // Check interface status
  417. //==========================================
  418. int isInterfaceUp(const char *interface)
  419. {
  420. int result = FAIL;
  421. FILE *fp;
  422. char cmd[256];
  423. char buf[512];
  424. strcpy(cmd, "ifconfig");
  425. fp = popen(cmd, "r");
  426. if(fp != NULL)
  427. {
  428. while(fgets(buf, sizeof(buf), fp) != NULL)
  429. {
  430. if(strstr(buf, interface) > 0)
  431. {
  432. result = PASS;
  433. }
  434. }
  435. }
  436. pclose(fp);
  437. return result;
  438. }
  439. */
  440. //=================================
  441. // Create all share memory
  442. //=================================
  443. int CreateShareMemory()
  444. {
  445. int MeterSMId;
  446. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  447. {
  448. #ifdef SystemLogMessage
  449. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  450. #endif
  451. return 0;
  452. }
  453. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  454. {
  455. #ifdef SystemLogMessage
  456. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  457. #endif
  458. return 0;
  459. }
  460. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  461. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  462. {
  463. #ifdef SystemLogMessage
  464. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  465. #endif
  466. return 0;
  467. }
  468. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  469. {
  470. #ifdef SystemLogMessage
  471. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  472. #endif
  473. return 0;
  474. }
  475. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  476. //creat ShmPsuData
  477. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  478. {
  479. #ifdef SystemLogMessage
  480. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  481. #endif
  482. return 0;
  483. }
  484. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  485. {
  486. #ifdef SystemLogMessage
  487. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  488. #endif
  489. return 0;
  490. }
  491. memset(ShmPsuData, 0, sizeof(struct PsuData));
  492. /*
  493. if(CHAdeMO_QUANTITY > 0)
  494. {
  495. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  496. {
  497. #ifdef SystemLogMessage
  498. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  499. #endif
  500. return 0;
  501. }
  502. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  503. {
  504. #ifdef SystemLogMessage
  505. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  506. #endif
  507. return 0;
  508. }
  509. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  510. }
  511. if(GB_QUANTITY > 0)
  512. {
  513. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  514. {
  515. #ifdef SystemLogMessage
  516. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  517. #endif
  518. return 0;
  519. }
  520. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  521. {
  522. #ifdef SystemLogMessage
  523. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  524. #endif
  525. return 0;
  526. }
  527. memset(ShmGBTData, 0, sizeof(struct GBTData));
  528. }
  529. //creat ShmCcsData
  530. if(CCS_QUANTITY > 0)
  531. {
  532. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  533. {
  534. #ifdef SystemLogMessage
  535. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  536. #endif
  537. return 0;
  538. }
  539. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  540. {
  541. #ifdef SystemLogMessage
  542. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  543. #endif
  544. return 0;
  545. }
  546. memset(ShmCcsData, 0, sizeof(struct CcsData));
  547. }
  548. */
  549. //creat ShmPrimaryMcuData
  550. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  551. {
  552. #ifdef SystemLogMessage
  553. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  554. #endif
  555. return 0;
  556. }
  557. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  558. {
  559. #ifdef SystemLogMessage
  560. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  561. #endif
  562. return 0;
  563. }
  564. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  565. //creat ShmFanModuleData
  566. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  567. {
  568. #ifdef SystemLogMessage
  569. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  570. #endif
  571. return 0;
  572. }
  573. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  574. {
  575. #ifdef SystemLogMessage
  576. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  577. #endif
  578. return 0;
  579. }
  580. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  581. //creat ShmRelayModuleData
  582. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  583. {
  584. #ifdef SystemLogMessage
  585. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG \n");
  586. #endif
  587. return 0;
  588. }
  589. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  590. {
  591. #ifdef SystemLogMessage
  592. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG \n");
  593. #endif
  594. return 0;
  595. }
  596. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  597. // DO360 RC2
  598. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  599. {
  600. #ifdef SystemLogMessage
  601. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG \n");
  602. #endif
  603. return 0;
  604. }
  605. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  606. {
  607. #ifdef SystemLogMessage
  608. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG \n");
  609. #endif
  610. return 0;
  611. }
  612. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  613. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  614. {
  615. #ifdef SystemLogMessage
  616. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  617. #endif
  618. return 0;
  619. }
  620. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  621. {
  622. #ifdef SystemLogMessage
  623. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  624. #endif
  625. return 0;
  626. }
  627. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  628. //creat ShmOCPP16Data
  629. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  630. {
  631. #ifdef SystemLogMessage
  632. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  633. #endif
  634. return 0;
  635. }
  636. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  637. {
  638. #ifdef SystemLogMessage
  639. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  640. #endif
  641. return 0;
  642. }
  643. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  644. //creat ShmOCPP16Data
  645. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  646. {
  647. #ifdef SystemLogMessage
  648. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ChargerInfoData NG \n");
  649. #endif
  650. return 0;
  651. }
  652. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  653. {
  654. #ifdef SystemLogMessage
  655. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ChargerInfoData NG \n");
  656. #endif
  657. return 0;
  658. }
  659. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  660. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  661. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  662. ShmGroupCollection = &ShmChargerInfo->PsuGrouping.GroupCollection[0];
  663. return 1;
  664. }
  665. //=================================
  666. // LCM Page
  667. //=================================
  668. void ChangeLcmByIndex(byte page_index)
  669. {
  670. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  671. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  672. {
  673. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  674. }
  675. }
  676. //======================================================
  677. // Peripheral initial
  678. //======================================================
  679. void InitGPIO()
  680. {
  681. /*****************0~3, 4 bank, bank x 32+ num*********************/
  682. /***************************************************************/
  683. /*************** GPIO 0 ***************************************/
  684. /***************************************************************/
  685. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  686. system("echo 22 > /sys/class/gpio/export");
  687. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  688. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  689. system("echo 23 > /sys/class/gpio/export");
  690. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  691. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  692. system("echo 26 > /sys/class/gpio/export");
  693. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  694. system("echo 1 > /sys/class/gpio/gpio26/value");
  695. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  696. system("echo 27 > /sys/class/gpio/export");
  697. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  698. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  699. system("echo 29 > /sys/class/gpio/export");
  700. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  701. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  702. system("echo 19 > /sys/class/gpio/export");
  703. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  704. system("echo 1 > /sys/class/gpio/gpio19/value");
  705. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  706. system("echo 20 > /sys/class/gpio/export");
  707. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  708. /***************************************************************/
  709. /*************** GPIO 1 ***************************************/
  710. /***************************************************************/
  711. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  712. system("echo 44 > /sys/class/gpio/export");
  713. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  714. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  715. system("echo 45 > /sys/class/gpio/export");
  716. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  717. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  718. system("echo 46 > /sys/class/gpio/export");
  719. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  720. system("echo 0 > /sys/class/gpio/gpio46/value");
  721. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  722. system("echo 47 > /sys/class/gpio/export");
  723. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  724. /***************************************************************/
  725. /*************** GPIO 2 ***************************************/
  726. /***************************************************************/
  727. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  728. system("echo 89 > /sys/class/gpio/export");
  729. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  730. system("echo 1 > /sys/class/gpio/gpio89/value");
  731. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  732. system("echo 87 > /sys/class/gpio/export");
  733. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  734. system("echo 0 > /sys/class/gpio/gpio87/value");
  735. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  736. system("echo 88 > /sys/class/gpio/export");
  737. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  738. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  739. system("echo 86 > /sys/class/gpio/export");
  740. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  741. /***************************************************************/
  742. /*************** GPIO 3 ***************************************/
  743. /***************************************************************/
  744. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  745. system("echo 111 > /sys/class/gpio/export");
  746. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  747. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  748. system("echo 114 > /sys/class/gpio/export");
  749. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  750. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  751. system("echo 113 > /sys/class/gpio/export");
  752. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  753. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  754. system("echo 110 > /sys/class/gpio/export");
  755. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  756. system("echo 0 > /sys/class/gpio/gpio110/value");
  757. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  758. system("echo 115 > /sys/class/gpio/export");
  759. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  760. system("echo 0 > /sys/class/gpio/gpio115/value");
  761. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  762. system("echo 112 > /sys/class/gpio/export");
  763. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  764. system("echo 1 > /sys/class/gpio/gpio112/value");
  765. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  766. system("echo 116 > /sys/class/gpio/export");
  767. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  768. system("echo 0 > /sys/class/gpio/gpio116/value");
  769. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  770. system("echo 103 > /sys/class/gpio/export");
  771. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  772. system("echo 0 > /sys/class/gpio/gpio103/value");
  773. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  774. system("echo 104 > /sys/class/gpio/export");
  775. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  776. system("echo 0 > /sys/class/gpio/gpio104/value");
  777. #ifdef SystemLogMessage
  778. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  779. #endif
  780. }
  781. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  782. {
  783. int fd, wrd;
  784. unsigned char *buf;
  785. unsigned int ChkSum, ChkSumOrg;
  786. if((buf = malloc(MtdBlockSize)) == NULL)
  787. {
  788. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  789. if(ShmStatusCodeData!=NULL)
  790. {
  791. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  792. }
  793. sleep(5);
  794. system("reboot -f");
  795. sleep(5);
  796. system("reboot -f");
  797. }
  798. memset(buf, 0, MtdBlockSize);
  799. //================================================
  800. // Load configuration from mtdblock10
  801. //================================================
  802. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  803. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  804. if(fd < 0)
  805. {
  806. free(buf);
  807. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  808. if(ShmStatusCodeData != NULL)
  809. {
  810. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  811. }
  812. sleep(5);
  813. system("reboot -f");
  814. sleep(5);
  815. system("reboot -f");
  816. }
  817. wrd = read(fd, buf, MtdBlockSize);
  818. close(fd);
  819. if(wrd < MtdBlockSize)
  820. {
  821. free(buf);
  822. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  823. if(ShmStatusCodeData != NULL)
  824. {
  825. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  826. }
  827. sleep(5);
  828. system("reboot -f");
  829. sleep(5);
  830. system("reboot -f");
  831. }
  832. ChkSum = 0;
  833. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  834. {
  835. ChkSum += buf[wrd];
  836. }
  837. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  838. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  839. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  840. //================================================
  841. // Load configuration from mtdblock11
  842. //================================================
  843. if(ChkSum != ChkSumOrg)
  844. {
  845. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  846. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  847. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  848. if(fd < 0)
  849. {
  850. free(buf);
  851. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  852. if(ShmStatusCodeData != NULL)
  853. {
  854. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  855. }
  856. sleep(5);
  857. system("reboot -f");
  858. sleep(5);
  859. system("reboot -f");
  860. }
  861. memset(buf, 0, MtdBlockSize);
  862. wrd = read(fd, buf, MtdBlockSize);
  863. close(fd);
  864. if(wrd < MtdBlockSize)
  865. {
  866. free(buf);
  867. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  868. if(ShmStatusCodeData != NULL)
  869. {
  870. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  871. }
  872. sleep(5);
  873. system("reboot -f");
  874. sleep(5);
  875. system("reboot -f");
  876. }
  877. ChkSum = 0;
  878. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  879. {
  880. ChkSum += buf[wrd];
  881. }
  882. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  883. //================================================
  884. // Load configuration from mtdblock12 (Factory default)
  885. //================================================
  886. if(ChkSum != ChkSumOrg)
  887. {
  888. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  889. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  890. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  891. if(fd < 0)
  892. {
  893. free(buf);
  894. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  895. if(ShmStatusCodeData != NULL)
  896. {
  897. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  898. }
  899. sleep(5);
  900. system("reboot -f");
  901. sleep(5);
  902. system("reboot -f");
  903. }
  904. memset(buf, 0, MtdBlockSize);
  905. wrd = read(fd, buf, MtdBlockSize);
  906. close(fd);
  907. if(wrd < MtdBlockSize)
  908. {
  909. free(buf);
  910. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  911. if(ShmStatusCodeData != NULL)
  912. {
  913. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  914. }
  915. sleep(5);
  916. system("reboot -f");
  917. sleep(5);
  918. system("reboot -f");
  919. }
  920. ChkSum = 0;
  921. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  922. {
  923. ChkSum += buf[wrd];
  924. }
  925. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  926. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  927. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  928. if(ChkSum != ChkSumOrg)
  929. {
  930. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  931. free(buf);
  932. system("cd /root;./FactoryConfig -m");
  933. system("rm -f /Storage/OCPP/OCPPConfiguration");
  934. system("sync");
  935. sleep(5);
  936. system("reboot -f");
  937. sleep(5);
  938. system("reboot -f");
  939. return FAIL;
  940. }
  941. }
  942. }
  943. //load OK
  944. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  945. free(buf);
  946. system("rm -f /mnt/EvseConfig.bin");
  947. // SysConfig in flash is empty (0xffffffff)
  948. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  949. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  950. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  951. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  952. {
  953. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  954. {
  955. PRINTF_FUNC("Model name over length.\n");
  956. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  957. }
  958. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  959. {
  960. PRINTF_FUNC("Model serial number over length.\n");
  961. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  962. }
  963. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  964. {
  965. PRINTF_FUNC("System id over length.\n");
  966. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  967. }
  968. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  969. {
  970. PRINTF_FUNC("Ethernet dhcp config is null.\n");
  971. }
  972. system("cd /root;./Module_FactoryConfig -m");
  973. sleep(3);
  974. system("/usr/bin/run_evse_restart.sh");
  975. }
  976. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  977. return PASS;
  978. }
  979. /*
  980. byte GetCurEthUsage()
  981. {
  982. byte result = _ETHERNET_USAGE_NONE;
  983. FILE *fp;
  984. char cmd[256];
  985. char buf[512];
  986. byte targetC = 2;
  987. strcpy(cmd, "route -n");
  988. fp = popen(cmd, "r");
  989. if (fp != NULL)
  990. {
  991. while(fgets(buf, sizeof(buf), fp) != NULL)
  992. {
  993. if (targetC-- == 0)
  994. {
  995. if (strstr(buf, "eth0") > 0)
  996. result = _ETHERNET_USAGE_LAN;
  997. else if (strstr(buf, "mlan0") > 0)
  998. result = _ETHERNET_USAGE_WIFI;
  999. else if (strstr(buf, "ppp0") > 0)
  1000. result = _ETHERNET_USAGE_3G_4g;
  1001. break;
  1002. }
  1003. // if (strstr(buf, "inet addr:") > 0)
  1004. // {
  1005. // }
  1006. }
  1007. }
  1008. pclose(fp);
  1009. memset(buf, 0x00, sizeof(buf));
  1010. return result;
  1011. }
  1012. */
  1013. int isEthernetCablePlugIn(int ethernet)
  1014. {
  1015. int fd, PlugInStatus = 0;
  1016. char ch[2];
  1017. char buffer[64];
  1018. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1019. fd = open(buffer, O_RDONLY);
  1020. if(fd < 0)
  1021. {
  1022. return -1;
  1023. }
  1024. if(read(fd, ch, 1) < 0)
  1025. {
  1026. return -1;
  1027. }
  1028. PlugInStatus = atoi(ch);
  1029. close(fd);
  1030. return PlugInStatus;
  1031. }
  1032. int isRouteFail()
  1033. {
  1034. int result = YES;
  1035. FILE *fp;
  1036. char buf[512];
  1037. fp = popen("route -n", "r");
  1038. if(fp != NULL)
  1039. {
  1040. while(fgets(buf, sizeof(buf), fp) != NULL)
  1041. {
  1042. if(strstr(buf, "eth0") != NULL)
  1043. result = NO;
  1044. }
  1045. }
  1046. pclose(fp);
  1047. return result;
  1048. }
  1049. int isReachableInternet()
  1050. {
  1051. int result = FAIL;
  1052. FILE *fp;
  1053. char cmd[256];
  1054. char buf[512];
  1055. char tmp[512];
  1056. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1057. // {
  1058. // result = PASS;
  1059. // }
  1060. // else
  1061. {
  1062. strcpy(cmd, "ifconfig eth0");
  1063. fp = popen(cmd, "r");
  1064. if (fp != NULL)
  1065. {
  1066. while(fgets(buf, sizeof(buf), fp) != NULL)
  1067. {
  1068. if (strstr(buf, "inet addr:") > 0)
  1069. {
  1070. sscanf(buf, "%*s%s", tmp);
  1071. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1072. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1073. {
  1074. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. pclose(fp);
  1080. memset(buf, 0x00, sizeof(buf));
  1081. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1082. {
  1083. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1084. fp = popen(cmd, "r");
  1085. if(fp != NULL)
  1086. {
  1087. while(fgets(buf, sizeof(buf), fp) != NULL)
  1088. {
  1089. if(strstr(buf, "transmitted") > 0)
  1090. {
  1091. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1092. if(strstr(buf,"100%") != NULL)
  1093. {
  1094. }
  1095. else
  1096. {
  1097. result = PASS;
  1098. }
  1099. //DEBUG_INFO("%s",buf);
  1100. //DEBUG_INFO("%s\n",tmp);
  1101. }
  1102. }
  1103. }
  1104. pclose(fp);
  1105. }
  1106. }
  1107. return result;
  1108. }
  1109. void AnnounceEthernetStatus(void)
  1110. {
  1111. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1112. {
  1113. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1114. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1115. {
  1116. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.EthernetStatusRequest = true;
  1117. }
  1118. }
  1119. }
  1120. void AnnounceWiFiStatus(void)
  1121. {
  1122. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1123. {
  1124. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1125. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1126. {
  1127. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.WiFiStatusRequest = true;
  1128. }
  1129. }
  1130. }
  1131. void AnnounceTelcomModemStatus(void)
  1132. {
  1133. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1134. {
  1135. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1136. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1137. {
  1138. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.TelcomModemStatusRequest = true;
  1139. }
  1140. }
  1141. }
  1142. void AnnounceBackendStatus(void)
  1143. {
  1144. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1145. {
  1146. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1147. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1148. {
  1149. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.BackendStatusRequest = true;
  1150. }
  1151. }
  1152. }
  1153. void AnnounceBillingStatus(void)
  1154. {
  1155. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1156. {
  1157. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1158. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1159. {
  1160. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.BillingStatusRequest = true;
  1161. }
  1162. }
  1163. }
  1164. void AnnounceStopChargingButtonStatus(void)
  1165. {
  1166. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1167. {
  1168. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1169. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1170. {
  1171. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.StopButtonStatusRequest = true;
  1172. }
  1173. }
  1174. }
  1175. void UpdateConnectionIcon(void)
  1176. {
  1177. unsigned char status = _Connnection_Disable;
  1178. // update ethernet connection status
  1179. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1180. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EthernetStatus != status)
  1181. {
  1182. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EthernetStatus = status;
  1183. AnnounceEthernetStatus();
  1184. }
  1185. // update wifi connection status
  1186. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1187. {
  1188. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1189. }
  1190. else
  1191. {
  1192. status = _Connnection_Disable;
  1193. }
  1194. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.WiFiStatus != status)
  1195. {
  1196. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.WiFiStatus = status;
  1197. AnnounceWiFiStatus();
  1198. }
  1199. // update 4g connection status
  1200. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1201. {
  1202. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1203. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1204. }
  1205. else
  1206. {
  1207. status = _Connnection_Disable;
  1208. }
  1209. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.TelcomModemStatus != status)
  1210. {
  1211. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.TelcomModemStatus = status;
  1212. AnnounceTelcomModemStatus();
  1213. }
  1214. // update backend connection status
  1215. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1216. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BackendStatus != status)
  1217. {
  1218. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BackendStatus = status;
  1219. AnnounceBackendStatus();
  1220. }
  1221. // update billing status
  1222. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1223. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BillingStatus != status)
  1224. {
  1225. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BillingStatus = status;
  1226. AnnounceBillingStatus();
  1227. }
  1228. // update stop charging button
  1229. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1230. if(ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.StopChargingButton != status)
  1231. {
  1232. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.StopChargingButton = status;
  1233. AnnounceStopChargingButtonStatus();
  1234. }
  1235. }
  1236. void Check4gWifiReset(void)
  1237. {
  1238. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1239. {
  1240. PRINTF_FUNC("Internet: %d, Ocpp: %d, Wifi: %d, Telcom: %d",
  1241. ShmSysConfigAndInfo->SysInfo.InternetConn,
  1242. ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
  1243. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode,
  1244. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled);
  1245. if(!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1246. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1247. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES))
  1248. {
  1249. if(!_NeedReset4gWifi)
  1250. {
  1251. gettimeofday(&_4gWifiReset_time, NULL);
  1252. PRINTF_FUNC("Backend Enable But Internet Not Available!");
  1253. }
  1254. _NeedReset4gWifi = true;
  1255. }
  1256. else
  1257. {
  1258. _NeedReset4gWifi = false;
  1259. }
  1260. if(_NeedReset4gWifi)
  1261. {
  1262. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= TRY_RESET_4G_WIFI_TIME)
  1263. {
  1264. PRINTF_FUNC("Reset 4g_Wifi Module");
  1265. system("echo 1 > /sys/class/gpio/gpio104/value");
  1266. sleep(1);
  1267. system("echo 0 > /sys/class/gpio/gpio104/value");
  1268. _NeedReset4gWifi = false;
  1269. }
  1270. }
  1271. }
  1272. else
  1273. {
  1274. _NeedReset4gWifi = false;
  1275. }
  1276. }
  1277. void InitEthernet()
  1278. {
  1279. system("ifconfig eth0 down");
  1280. system("ifconfig eth1 down");
  1281. sleep(2);
  1282. char tmpbuf[256];
  1283. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1284. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1285. sleep(2);
  1286. //Init Eth0 for internet
  1287. memset(tmpbuf,0,256);
  1288. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1289. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1290. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1291. system(tmpbuf);
  1292. memset(tmpbuf,0,256);
  1293. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1294. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1295. system(tmpbuf);
  1296. //system("ifconfig lo up");
  1297. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1298. //Init Eth1 for administrator tool
  1299. memset(tmpbuf,0,256);
  1300. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  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. // DO360 DHCP Server
  1317. InitialDispenserDhcpServerConfig();
  1318. StartDispenserDhcpServer();
  1319. pid_t pid = fork();
  1320. uint8_t cnt_pingDNS_Fail;
  1321. if(pid == 0)
  1322. {
  1323. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BackendStatus = _Connnection_Disconnected;
  1324. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.EthernetStatus = _Connnection_Disconnected;
  1325. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.WiFiStatus = _Connnection_Disable;
  1326. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.TelcomModemStatus = _Connnection_Disable;
  1327. for(;;)
  1328. {
  1329. if(isEthernetCablePlugIn(0) == YES)
  1330. {
  1331. if(isRouteFail())
  1332. {
  1333. PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1334. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1335. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1336. {
  1337. InitialDHCP();
  1338. }
  1339. else
  1340. {
  1341. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1342. memset(tmpbuf,0,256);
  1343. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1344. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1345. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1346. system(tmpbuf);
  1347. memset(tmpbuf,0,256);
  1348. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1349. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1350. system(tmpbuf);
  1351. }
  1352. }
  1353. }
  1354. if(isReachableInternet() == PASS)
  1355. {
  1356. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1357. {
  1358. PRINTF_FUNC("Internet is Reachable");
  1359. }
  1360. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1361. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1362. cnt_pingDNS_Fail = 0;
  1363. }
  1364. else
  1365. {
  1366. if(++cnt_pingDNS_Fail > 3)
  1367. {
  1368. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1369. {
  1370. PRINTF_FUNC("Internet is Disconnect Via Ethernet");
  1371. }
  1372. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1373. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1374. }
  1375. }
  1376. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1377. if(ethResult == YES)
  1378. {
  1379. system("/sbin/ifmetric eth0 0");
  1380. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1381. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1382. {
  1383. system("/sbin/ifmetric mlan0 1");
  1384. }
  1385. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1386. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1387. {
  1388. system("/sbin/ifmetric ppp0 2");
  1389. }
  1390. }
  1391. if (!ethResult &&
  1392. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1393. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1394. {
  1395. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1396. if (ethResult)
  1397. {
  1398. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1399. {
  1400. system("/sbin/ifmetric eth0 1");
  1401. system("/sbin/ifmetric mlan0 0");
  1402. }
  1403. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1404. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1405. {
  1406. system("/sbin/ifmetric ppp0 2");
  1407. }
  1408. }
  1409. }
  1410. if (!ethResult &&
  1411. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1412. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1413. {
  1414. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1415. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1416. if (ethResult)
  1417. {
  1418. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1419. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1420. {
  1421. system("/sbin/ifmetric mlan0 2");
  1422. }
  1423. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1424. {
  1425. system("/sbin/ifmetric eth0 1");
  1426. system("/sbin/ifmetric ppp0 0");
  1427. }
  1428. }
  1429. }
  1430. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1431. {
  1432. ShmOCPP16Data->OcppConnStatus = false;
  1433. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1434. }
  1435. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1436. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == false ? true : false;
  1437. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1438. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1439. UpdateConnectionIcon();
  1440. Check4gWifiReset();
  1441. sleep(5);
  1442. }
  1443. }
  1444. #ifdef SystemLogMessage
  1445. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1446. #endif
  1447. }
  1448. /*
  1449. int InitialRfidPort()
  1450. {
  1451. int uartO2 = open(rfidPortName, O_RDWR);
  1452. struct termios tios;
  1453. if (uartO2 != FAIL)
  1454. {
  1455. ioctl (uartO2, TCGETS, &tios);
  1456. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1457. tios.c_lflag = 0;
  1458. tios.c_iflag = 0;
  1459. tios.c_oflag = 0;
  1460. tios.c_cc[VMIN] = 0;
  1461. tios.c_cc[VTIME] = (unsigned char) 1;
  1462. tios.c_lflag = 0;
  1463. tcflush(uartO2, TCIFLUSH);
  1464. ioctl(uartO2, TCSETS, &tios);
  1465. }
  1466. if (uartO2 < 0)
  1467. {
  1468. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1469. }
  1470. return uartO2;
  1471. }
  1472. */
  1473. void GetMacAddress()
  1474. {
  1475. for (byte index = 0; index < 2; index++)
  1476. {
  1477. int fd;
  1478. struct ifreq ifr;
  1479. char tarEth[5];
  1480. char Mac[18];
  1481. sprintf(tarEth,"eth%d",index);
  1482. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1483. ifr.ifr_addr.sa_family = AF_INET;
  1484. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1485. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1486. close(fd);
  1487. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1488. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1489. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1490. if (index == 0)
  1491. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1492. else
  1493. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1494. }
  1495. }
  1496. void GetFirmwareVersion()
  1497. {
  1498. // Get CSU root file system version
  1499. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1500. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1501. for(uint8_t idx=0;idx<3;idx++)
  1502. {
  1503. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1504. {
  1505. chademo++;
  1506. count++;
  1507. }
  1508. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1509. {
  1510. gb++;
  1511. count++;
  1512. }
  1513. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1514. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1515. {
  1516. ccs++;
  1517. count++;
  1518. }
  1519. }
  1520. if (count == 1)
  1521. {
  1522. if (chademo > 0)
  1523. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1524. else if (ccs > 0)
  1525. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1526. else if (gb > 0)
  1527. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1528. }
  1529. else
  1530. {
  1531. if (chademo > 0 && ccs > 0)
  1532. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1533. else if (chademo > 0 && gb > 0)
  1534. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1535. else if (ccs > 0 && gb > 0)
  1536. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1537. }
  1538. // Get network option from model name
  1539. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1540. {
  1541. case 'B':
  1542. case 'U':
  1543. //Blue tooth
  1544. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1545. break;
  1546. case 'W':
  1547. // WIFI
  1548. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1549. break;
  1550. case 'T':
  1551. // 3G/4G
  1552. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1553. break;
  1554. case 'D':
  1555. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1556. break;
  1557. default:
  1558. // LAN
  1559. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1560. break;
  1561. }
  1562. // Get rating power from model name
  1563. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1564. // Get IEC or UL
  1565. char _buf[3] = {0};
  1566. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1567. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1568. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1569. else if (strcmp(_buf, "WU") == EQUAL)
  1570. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1571. }
  1572. void InitialShareMemoryInfo()
  1573. {
  1574. FILE *fp;
  1575. char cmd[512];
  1576. char buf[512];
  1577. _UpgradeNeedReboot = FALSE;
  1578. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1579. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1580. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1581. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1582. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1583. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1584. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1585. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1586. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1587. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1588. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1589. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1590. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1591. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1592. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1593. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1594. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1595. sprintf(cmd, "/bin/uname -r");
  1596. fp = popen(cmd, "r");
  1597. if(fp == NULL)
  1598. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1599. else
  1600. {
  1601. while(fgets(buf, sizeof(buf), fp) != NULL)
  1602. {
  1603. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1604. }
  1605. }
  1606. // 雙槍 CCS + Chademo
  1607. GetFirmwareVersion();
  1608. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1609. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1610. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1611. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1612. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1613. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1614. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1615. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1616. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1617. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1618. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1619. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1620. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1621. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1622. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1623. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1624. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1625. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1626. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1627. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1628. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1629. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1630. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1631. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1632. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1633. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1634. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1635. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1636. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1637. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1638. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1639. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1640. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1641. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1642. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1643. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1644. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1645. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1646. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1647. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1648. ShmFanModuleData->SelfTest_Comp = NO;
  1649. ShmLedModuleData->SelfTest_Comp = NO;
  1650. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1651. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1652. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1653. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1654. ShmFanModuleData->TestFanSpeed = 0;
  1655. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1656. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1657. char EvsePower[2];
  1658. EvsePower[2] = '\0';
  1659. unsigned short buf_pow = 0;
  1660. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  1661. {
  1662. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  1663. if (strcmp(EvsePower, "15") == EQUAL)
  1664. buf_pow = 150;
  1665. else if (strcmp(EvsePower, "30") == EQUAL)
  1666. buf_pow = 30;
  1667. else if (strcmp(EvsePower, "60") == EQUAL)
  1668. buf_pow = 60;
  1669. else if (strcmp(EvsePower, "12") == EQUAL)
  1670. buf_pow = 120;
  1671. else if (strcmp(EvsePower, "18") == EQUAL)
  1672. buf_pow = 180;
  1673. else if (strcmp(EvsePower, "36") == EQUAL)
  1674. buf_pow = 360;
  1675. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (buf_pow / 30) * PSU_MIN_CUR;
  1676. if(ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1677. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow)
  1678. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1679. }
  1680. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1681. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1682. PRINTF_FUNC("DispenserSequence = %d, MaxConnectorQuantity = %d",
  1683. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  1684. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  1685. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  1686. {
  1687. PRINTF_FUNC("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  1688. }
  1689. }
  1690. int Initialization(void)
  1691. {
  1692. // 初始化卡號驗證的 Flag
  1693. ClearAuthorizedFlag();
  1694. // 初始化插槍驗證的 Flag
  1695. ClearDetectPluginFlag();
  1696. // int pinOut[2] = { 116, 115 };
  1697. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  1698. {
  1699. chargingInfo[count]->RemoteStartFlag = NO;
  1700. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartRequest = false;
  1701. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  1702. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopRequest = false;
  1703. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  1704. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1705. }
  1706. PRINTF_FUNC("Initialization OK");
  1707. return PASS;
  1708. }
  1709. void InitialPsuGroupingAndLocation(void)
  1710. {
  1711. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1712. {
  1713. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  1714. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  1715. }
  1716. else
  1717. {
  1718. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  1719. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  1720. }
  1721. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1722. {
  1723. ShmGroupCollection[i].Index = i;
  1724. ShmGroupCollection[i].Location = ShmPsuGrouping->Location[i];
  1725. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1726. {
  1727. memcpy(&ShmGroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  1728. }
  1729. else
  1730. {
  1731. memcpy(&ShmGroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  1732. }
  1733. _PsuGroupAvailable[i] = NO;
  1734. }
  1735. }
  1736. void InitialMaxDispenserConnectorQuantity(void)
  1737. {
  1738. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1739. {
  1740. ShmChargerInfo->Control.MaxDispenser = 2;
  1741. ShmChargerInfo->Control.MaxConnector = 2;
  1742. }
  1743. else
  1744. {
  1745. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  1746. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  1747. }
  1748. }
  1749. void InitialAuthMode(void)
  1750. {
  1751. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1752. {
  1753. ShmChargerInfo->AuthInfo.AuthMode.bits.AutoStartEnable = true;
  1754. }
  1755. }
  1756. void InitialQRCodeMode(void)
  1757. {
  1758. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1759. {
  1760. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  1761. }
  1762. else
  1763. {
  1764. // customized
  1765. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  1766. {
  1767. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  1768. }
  1769. else
  1770. {
  1771. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  1772. }
  1773. }
  1774. }
  1775. void InitialPowerCabinetSetting(void)
  1776. {
  1777. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1778. {
  1779. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  1780. }
  1781. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1782. ShmSysConfigAndInfo->SysInfo.CabinetMicsStatus.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1783. }
  1784. bool InitialChargerSetting(void)
  1785. {
  1786. bool result = true;
  1787. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O')
  1788. {
  1789. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  1790. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  1791. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1792. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  1793. ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication = true;
  1794. }
  1795. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1796. {
  1797. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  1798. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1799. ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication = true;
  1800. }
  1801. else
  1802. {
  1803. result = false;
  1804. }
  1805. InitialMaxDispenserConnectorQuantity();
  1806. InitialAuthMode();
  1807. InitialQRCodeMode();
  1808. InitialPsuGroupingAndLocation();
  1809. InitialPowerCabinetSetting();
  1810. if(!CheckConnectorTypeStatus())
  1811. {
  1812. result = false;
  1813. }
  1814. return result;
  1815. }
  1816. bool InitialSystemDefaultConfig()
  1817. {
  1818. bool result = true;
  1819. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1820. InitGPIO();
  1821. InitEthernet();
  1822. GetMacAddress();
  1823. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1824. // sleep(3);
  1825. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1826. // sleep(1);
  1827. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1828. return result;
  1829. }
  1830. /*
  1831. bool DisplaySelfTestFailReason()
  1832. {
  1833. bool result = false;
  1834. // RB、FB、407、EV 小板中有些板子無回應
  1835. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1836. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1837. if (ShmFanModuleData->SelfTest_Comp == NO)
  1838. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1839. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1840. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1841. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1842. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1843. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1844. {
  1845. if (chargingInfo[index]->Type == _Type_Chademo)
  1846. {
  1847. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1848. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1849. }
  1850. else if (chargingInfo[index]->Type == _Type_GB)
  1851. {
  1852. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1853. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1854. }
  1855. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1856. {
  1857. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1858. {
  1859. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1860. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1861. }
  1862. }
  1863. }
  1864. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1865. {
  1866. // 先借 GBT 顯示
  1867. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1868. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1869. }
  1870. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1871. {
  1872. // AC Contact 未搭上
  1873. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1874. result = true;
  1875. }
  1876. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1877. {
  1878. // PSU 通訊問題
  1879. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1880. result = true;
  1881. }
  1882. return result;
  1883. }
  1884. */
  1885. void SelfTestRun()
  1886. {
  1887. struct timeval _selfTest_time;
  1888. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1889. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1890. ShmFanModuleData->SelfTest_Comp = NO;
  1891. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1892. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1893. gettimeofday(&_selfTest_time, NULL);
  1894. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1895. {
  1896. ChkPrimaryStatus();
  1897. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1898. {
  1899. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1900. PRINTF_FUNC("Self Test Fail: WarningInfo Level 2");
  1901. return;
  1902. }
  1903. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1904. {
  1905. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1906. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1907. {
  1908. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1909. PRINTF_FUNC("Self Test Fail: PSU No Working");
  1910. return;
  1911. }
  1912. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1913. {
  1914. case _STEST_VERSION:
  1915. {
  1916. if ((ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable || ShmFanModuleData->SelfTest_Comp) &&
  1917. (ShmChargerInfo->Control.SysCtrl.bits.RelayBoardDisable || ShmRelayModuleData[0]->SelfTest_Comp) &&
  1918. (ShmChargerInfo->Control.SysCtrl.bits.RelayBoardDisable ||
  1919. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false ||
  1920. ShmRelayModuleData[1]->SelfTest_Comp) &&
  1921. ShmPrimaryMcuData->SelfTest_Comp)
  1922. {
  1923. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1924. PRINTF_FUNC("Self test version check ok");
  1925. SetAcContactor(ON);
  1926. }
  1927. }
  1928. break;
  1929. case _STEST_AC_CONTACTOR:
  1930. {
  1931. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1932. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1933. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1934. {
  1935. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1936. PRINTF_FUNC("Communication board pass.");
  1937. }
  1938. }
  1939. break;
  1940. case _STEST_PSU_DETECT:
  1941. {
  1942. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1943. {
  1944. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1945. }
  1946. }
  1947. break;
  1948. case _STEST_PSU_CAP:
  1949. {
  1950. // 此測試是要確認當前總輸出能力
  1951. // 如果沒有 PSU 模組請 bypass
  1952. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1953. {
  1954. sleep(1);
  1955. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1956. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1957. }
  1958. }
  1959. break;
  1960. }
  1961. }
  1962. else
  1963. break;
  1964. usleep(100000);
  1965. if(GetTimeoutValue(_selfTest_time) / uSEC_VAL >= SELFTEST_TIMEOUT)
  1966. {
  1967. _SelfTestTimeout();
  1968. break;
  1969. }
  1970. }
  1971. }
  1972. int SpawnTask()
  1973. {
  1974. sleep(2);
  1975. system("/root/Module_EventLogging &");
  1976. system("/root/Module_PrimaryComm &");
  1977. system("/root/Module_EvComm &");
  1978. //system("/root/Module_LcmControl &");
  1979. system("/root/Module_InternalComm &");
  1980. system("/root/Module_PsuComm &");
  1981. system("/root/Module_ProduceUtils &");
  1982. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1983. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  1984. {
  1985. system("/root/OcppBackend &");
  1986. }
  1987. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1988. {
  1989. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1990. {
  1991. system("/root/Module_4g &");
  1992. }
  1993. }
  1994. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1995. {
  1996. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1997. {
  1998. system("/root/Module_Wifi &");
  1999. }
  2000. }
  2001. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2002. {
  2003. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2004. {
  2005. system("/root/Module_4g &");
  2006. }
  2007. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2008. {
  2009. system("/root/Module_Wifi &");
  2010. }
  2011. }
  2012. return PASS;
  2013. }
  2014. int runFlashShellCmd(const char *cmd)
  2015. {
  2016. int result = FAIL;
  2017. char buf[256];
  2018. FILE *fp;
  2019. fp = popen(cmd, "r");
  2020. if(fp != NULL)
  2021. {
  2022. while(fgets(buf, sizeof(buf), fp) != NULL)
  2023. {
  2024. DEBUG_INFO_MSG("%s\n", buf);
  2025. }
  2026. result = PASS;
  2027. }
  2028. pclose(fp);
  2029. return result;
  2030. }
  2031. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2032. {
  2033. int result = PASS;
  2034. int fd, wrd;
  2035. unsigned int i, Chk;
  2036. unsigned char *ptr, *BufTmp;
  2037. Chk = 0;
  2038. ptr = (unsigned char *)UsrData;
  2039. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2040. {
  2041. memset(BufTmp, 0, MtdBlockSize);
  2042. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2043. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2044. {
  2045. Chk += *(BufTmp+i);
  2046. }
  2047. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2048. // Output configuration to file.
  2049. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2050. if(fd < 0)
  2051. {
  2052. DEBUG_ERROR_MSG("open /mnt/EvseTempConfig.bin NG\n");
  2053. free(BufTmp);
  2054. return 0;
  2055. }
  2056. wrd = write(fd, BufTmp, MtdBlockSize);
  2057. close(fd);
  2058. if(wrd < MtdBlockSize)
  2059. {
  2060. DEBUG_ERROR_MSG("write /mnt/EvseTempConfig.bin NG\n");
  2061. free(BufTmp);
  2062. return 0;
  2063. }
  2064. DEBUG_INFO_MSG("EvseTempConfig write to file in /mnt OK.\n");
  2065. DEBUG_INFO_MSG("Erase /dev/mtd10.\n");
  2066. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2067. DEBUG_INFO_MSG("Write /dev/mtd10.\n");
  2068. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2069. DEBUG_INFO_MSG("Erase /dev/mtd11.\n");
  2070. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2071. DEBUG_INFO_MSG("Write /dev/mtd11.\n");
  2072. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2073. system("rm -f /mnt/EvseTempConfig.bin");
  2074. DEBUG_INFO_MSG("EvseTempConfig write to flash OK\n");
  2075. }
  2076. else
  2077. {
  2078. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  2079. result = FAIL;
  2080. }
  2081. if(BufTmp != NULL)
  2082. {
  2083. free(BufTmp);
  2084. }
  2085. return result;
  2086. }
  2087. //===============================================
  2088. // Common Detect Chk - Stop Charging ?
  2089. //===============================================
  2090. bool isEvBoardStopChargeFlag(byte gunIndex)
  2091. {
  2092. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2093. }
  2094. bool isNormalStopChargeFlag(byte gunIndex)
  2095. {
  2096. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2097. }
  2098. //===============================================
  2099. // 掃描插槍狀況
  2100. //===============================================
  2101. void ClearDetectPluginFlag()
  2102. {
  2103. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2104. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2105. {
  2106. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  2107. chargingInfo[gun_index]->RemoteStartFlag = NO;
  2108. }
  2109. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2110. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2111. }
  2112. void DetectPluginStart()
  2113. {
  2114. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2115. }
  2116. bool isDetectPlugin()
  2117. {
  2118. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2119. return YES;
  2120. return NO;
  2121. }
  2122. //===============================================
  2123. // Common Detect Chk - Chademo
  2124. //===============================================
  2125. bool isEvGunLocked_chademo(byte gunIndex)
  2126. {
  2127. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  2128. }
  2129. bool isEvContactorWelding_chademo(byte gunIndex)
  2130. {
  2131. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  2132. }
  2133. bool isEvStopReq_chademo(byte gunIndex)
  2134. {
  2135. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  2136. }
  2137. bool isEvStopCharging_chademo(byte gunIndex)
  2138. {
  2139. if (isEvGunLocked_chademo(gunIndex) == NO)
  2140. {
  2141. // 無鎖槍 = 停止
  2142. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  2143. return YES;
  2144. }
  2145. return NO;
  2146. }
  2147. byte isPrechargeStatus_chademo(byte gunIndex)
  2148. {
  2149. byte result = 0x00;
  2150. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2151. return result;
  2152. }
  2153. //===============================================
  2154. // Common Detect Chk - GB
  2155. //===============================================
  2156. bool isEvGunLocked_gb(byte gunIndex)
  2157. {
  2158. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  2159. }
  2160. bool isEvStopCharging_gb(byte gunIndex)
  2161. {
  2162. if (isEvGunLocked_gb(gunIndex) == NO)
  2163. {
  2164. // 無鎖槍 = 停止
  2165. PRINTF_FUNC("gun locked none. \n");
  2166. return YES;
  2167. }
  2168. return NO;
  2169. }
  2170. byte isPrechargeStatus_gb(byte gunIndex)
  2171. {
  2172. byte result = 0x00;
  2173. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2174. return result;
  2175. }
  2176. //===============================================
  2177. // Common Detect Chk - CCS
  2178. //===============================================
  2179. bool isEvGunLocked_ccs(byte gunIndex)
  2180. {
  2181. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  2182. }
  2183. byte isPrechargeStatus_ccs(byte gunIndex)
  2184. {
  2185. byte result = 0x00;
  2186. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2187. {
  2188. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2189. }
  2190. return result;
  2191. }
  2192. bool isEvStopCharging_ccs(byte gunIndex)
  2193. {
  2194. if (isEvGunLocked_ccs(gunIndex) == NO)
  2195. {
  2196. // 無鎖槍 = 停止
  2197. PRINTF_FUNC("gun locked none. \n");
  2198. return YES;
  2199. }
  2200. return NO;
  2201. }
  2202. //===============================================
  2203. // Callback
  2204. //===============================================
  2205. void DisplayChargingInfo()
  2206. {
  2207. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  2208. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2209. {
  2210. if (chargingInfo[i]->SystemStatus != S_IDLE &&
  2211. chargingInfo[i]->SystemStatus != S_RESERVATION)
  2212. {
  2213. ChangeGunSelectByIndex(i);
  2214. return;
  2215. }
  2216. }
  2217. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2218. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2219. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  2220. {
  2221. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2222. }
  2223. usleep(50000);
  2224. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2225. }
  2226. void _AutoReturnTimeout()
  2227. {
  2228. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  2229. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2230. {
  2231. ClearDetectPluginFlag();
  2232. }
  2233. // else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2234. // {
  2235. // DetectPluginStart();
  2236. // }
  2237. usleep(50000);
  2238. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2239. }
  2240. void _SelfTestTimeout(void)
  2241. {
  2242. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2243. {
  2244. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2245. {
  2246. setChargerMode(gun_index, MODE_FAULT);
  2247. }
  2248. }
  2249. ShmPsuData->Work_Step = _NO_WORKING;
  2250. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2251. PRINTF_FUNC("Self test timeout. \n");
  2252. }
  2253. void _AuthorizedTimeout()
  2254. {
  2255. if(IsAuthorizingMode())
  2256. {
  2257. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  2258. isCardScan = false;
  2259. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2260. ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  2261. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2262. ClearAuthorizedFlag();
  2263. }
  2264. }
  2265. void _DetectPlugInTimeout()
  2266. {
  2267. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  2268. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2269. ClearDetectPluginFlag();
  2270. usleep(50000);
  2271. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2272. }
  2273. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2274. {
  2275. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2276. {
  2277. if(!isEvGunLocked_chademo(gunIndex))
  2278. {
  2279. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  2280. }
  2281. }
  2282. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2283. {
  2284. if(!isEvGunLocked_ccs(gunIndex))
  2285. {
  2286. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  2287. }
  2288. }
  2289. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2290. {
  2291. if(!isEvGunLocked_ccs(gunIndex))
  2292. {
  2293. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  2294. }
  2295. }
  2296. ChargingTerminalProcess(gunIndex);
  2297. _AutoReturnTimeout();
  2298. }
  2299. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2300. {
  2301. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  2302. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2303. {
  2304. setChargerMode(gunIndex, MODE_TERMINATING);
  2305. _AutoReturnTimeout();
  2306. }
  2307. }
  2308. void _PrepareTimeout(byte gunIndex)
  2309. {
  2310. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  2311. setChargerMode(gunIndex, MODE_TERMINATING);
  2312. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2313. _AutoReturnTimeout();
  2314. }
  2315. void _CcsPrechargeTimeout(byte gunIndex)
  2316. {
  2317. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  2318. setChargerMode(gunIndex, MODE_IDLE);
  2319. }
  2320. //===============================================
  2321. // 取得卡號與卡號驗證
  2322. //===============================================
  2323. bool canStartCharging()
  2324. {
  2325. char buf2[16] = "";
  2326. memset(buf2, 0, ARRAY_SIZE(buf2));
  2327. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  2328. {
  2329. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  2330. }
  2331. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  2332. // 因為無法得知實際的長度,所以只能用搜尋的方式
  2333. if(strcmp(buf2, "Accepted") == EQUAL)
  2334. return true;
  2335. else
  2336. {
  2337. }
  2338. return false;
  2339. }
  2340. void AuthorizingStart()
  2341. {
  2342. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  2343. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2344. }
  2345. void ClearAuthorizedFlag()
  2346. {
  2347. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  2348. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2349. }
  2350. bool isAuthorizedComplete()
  2351. {
  2352. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  2353. return false;
  2354. return true;
  2355. }
  2356. bool IsAuthorizingMode()
  2357. {
  2358. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2359. return false;
  2360. return true;
  2361. }
  2362. void ReleaseAlarmCode(byte gunIndex)
  2363. {
  2364. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, "NoError") != EQUAL)
  2365. {
  2366. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, "NoError");
  2367. }
  2368. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "") != EQUAL)
  2369. {
  2370. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "");
  2371. }
  2372. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2373. {
  2374. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2375. }
  2376. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2377. {
  2378. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2379. }
  2380. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2381. {
  2382. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2383. }
  2384. }
  2385. //===============================================
  2386. // EmergencyStop and Charging Stop
  2387. //===============================================
  2388. void ChargingTerminalProcess(byte gunIndex)
  2389. {
  2390. setChargerMode(gunIndex, MODE_TERMINATING);
  2391. }
  2392. void ChargingAlarmProcess(byte gunIndex)
  2393. {
  2394. UpdateErrorCodeToOcpp(gunIndex);
  2395. setChargerMode(gunIndex, MODE_ALARM);
  2396. }
  2397. void AcChargingTerminalProcess()
  2398. {
  2399. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2400. }
  2401. void StopChargingProcessByString(byte level)
  2402. {
  2403. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2404. {
  2405. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2406. }
  2407. }
  2408. void ReleaseChargingProcessByString(byte level)
  2409. {
  2410. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2411. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2412. }
  2413. // 一般錯誤停止充電處理函式
  2414. void BoardErrOccurByString(byte index, char *code)
  2415. {
  2416. byte level = 1;
  2417. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2418. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2419. {
  2420. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2421. {
  2422. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2423. }
  2424. ChargingTerminalProcess(index);
  2425. }
  2426. StopChargingProcessByString(level);
  2427. }
  2428. // 急停狀況的停止充電處理函式
  2429. void EmcOccureByString(char *code)
  2430. {
  2431. byte level = 2;
  2432. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2433. // 其錯誤等級為 2
  2434. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2435. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL)
  2436. {
  2437. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2438. {
  2439. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2440. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2441. {
  2442. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2443. {
  2444. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2445. }
  2446. ChargingAlarmProcess(gun);
  2447. }
  2448. }
  2449. StopChargingProcessByString(level);
  2450. InformOcppErrOccur(4);
  2451. }
  2452. }
  2453. void ReleaseBoardErrOccurByString(byte index, char *code)
  2454. {
  2455. bool isTrigger = false;
  2456. byte level = 1;
  2457. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2458. {
  2459. isTrigger = true;
  2460. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2461. }
  2462. if (isTrigger)
  2463. {
  2464. ReleaseChargingProcessByString(level);
  2465. }
  2466. }
  2467. void ReleaseEmsOccureByString(byte index, char *code)
  2468. {
  2469. bool isTrigger = false;
  2470. byte level = 2;
  2471. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2472. {
  2473. isTrigger = true;
  2474. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2475. }
  2476. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2477. {
  2478. isTrigger = true;
  2479. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2480. }
  2481. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2482. {
  2483. isTrigger = true;
  2484. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2485. }
  2486. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2487. {
  2488. isTrigger = true;
  2489. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2490. }
  2491. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2492. {
  2493. isTrigger = true;
  2494. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2495. }
  2496. if (isTrigger)
  2497. {
  2498. ReleaseChargingProcessByString(level);
  2499. InformOcppErrOccur(6);
  2500. }
  2501. }
  2502. //===============================================
  2503. // 確認硬體 (按鈕) 狀態
  2504. //===============================================
  2505. void ChkPrimaryStatus()
  2506. {
  2507. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2508. {
  2509. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2510. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2511. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2512. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2513. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2514. return;
  2515. }
  2516. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2517. {
  2518. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2519. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2520. EmcOccureByString("042251");
  2521. }
  2522. else
  2523. {
  2524. ReleaseEmsOccureByString(0, "042251");
  2525. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2526. }
  2527. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2528. {
  2529. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2530. //EmcOccureByString("042238");
  2531. }
  2532. else
  2533. {
  2534. //ReleaseEmsOccureByString(0, "042238");
  2535. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2536. }
  2537. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2538. {
  2539. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2540. }
  2541. else
  2542. {
  2543. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2544. }
  2545. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2546. {
  2547. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2548. EmcOccureByString("042252");
  2549. }
  2550. else
  2551. {
  2552. ReleaseEmsOccureByString(0, "042252");
  2553. }
  2554. }
  2555. struct timeval _AcContactReOpen_Time;
  2556. void ChkPsuStatus(void)
  2557. {
  2558. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2559. {
  2560. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2561. {
  2562. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2563. {
  2564. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2565. }
  2566. }
  2567. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2568. {
  2569. EmcOccureByString("042267");
  2570. }
  2571. }
  2572. else
  2573. {
  2574. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2575. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2576. {
  2577. bool allFaultStatus = true;
  2578. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2579. {
  2580. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT)
  2581. {
  2582. allFaultStatus = false;
  2583. }
  2584. }
  2585. if(allFaultStatus)
  2586. {
  2587. PRINTF_FUNC("PSU Failure, Need Resume");
  2588. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2589. }
  2590. }
  2591. }
  2592. }
  2593. int AcContactorReOpenCount = 0;
  2594. void PsuFailureResume(void)
  2595. {
  2596. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2597. {
  2598. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2599. {
  2600. PRINTF_FUNC("Ac Contactor Off By Psu Failure");
  2601. // update _AcContactReOpen_Time
  2602. gettimeofday(&_AcContactReOpen_Time, NULL);
  2603. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2604. AcContactorReOpenCount++;
  2605. }
  2606. else
  2607. {
  2608. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2609. {
  2610. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2611. {
  2612. ReleaseEmsOccureByString(0, "042267");
  2613. // update _AcContactReOpen_Time
  2614. gettimeofday(&_AcContactReOpen_Time, NULL);
  2615. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2616. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2617. PRINTF_FUNC("Psu Failure Resume");
  2618. }
  2619. }
  2620. }
  2621. }
  2622. else
  2623. {
  2624. if(AcContactorReOpenCount > 0)
  2625. {
  2626. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2627. {
  2628. AcContactorReOpenCount = 0;
  2629. }
  2630. }
  2631. }
  2632. }
  2633. void SetAcContactor(unsigned char OnOff)
  2634. {
  2635. OnOff = OnOff ? ON : OFF;
  2636. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2637. {
  2638. if(OnOff)
  2639. {
  2640. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2641. }
  2642. PRINTF_FUNC("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2643. }
  2644. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2645. }
  2646. void StandbyCheck(void)
  2647. {
  2648. if(IsConnectorWholeIdle())
  2649. {
  2650. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2651. {
  2652. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2653. {
  2654. int time = STANDBY_TIME;
  2655. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2656. {
  2657. time = FAST_STANDBY_TIME;
  2658. }
  2659. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2660. {
  2661. SetAcContactor(OFF);
  2662. }
  2663. }
  2664. }
  2665. else
  2666. {
  2667. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2668. gettimeofday(&_Standby_time, NULL);
  2669. }
  2670. }
  2671. else
  2672. {
  2673. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2674. {
  2675. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2676. }
  2677. }
  2678. }
  2679. void CheckDisconnectionStatusRecovery(void)
  2680. {
  2681. bool recovery = true;
  2682. // check all connector
  2683. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2684. {
  2685. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2686. {
  2687. recovery = false;
  2688. }
  2689. }
  2690. if(recovery)
  2691. {
  2692. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2693. }
  2694. }
  2695. //===============================================
  2696. // 確認各小板偵測的錯誤狀況
  2697. //===============================================
  2698. void CheckErrorOccurStatus(byte index)
  2699. {
  2700. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2701. {
  2702. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2703. }
  2704. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2705. {
  2706. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2707. {
  2708. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2709. }
  2710. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2711. {
  2712. PRINTF_FUNC("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2713. }
  2714. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2715. }
  2716. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2717. {
  2718. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2719. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2720. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2721. if(chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2722. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2723. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2724. chargingInfo[index]->SystemStatus == S_CHARGING)
  2725. {
  2726. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2727. {
  2728. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2729. }
  2730. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2731. {
  2732. PRINTF_FUNC("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2733. }
  2734. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2735. }
  2736. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2737. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2738. {
  2739. setChargerMode(index, MODE_IDLE);
  2740. }
  2741. }
  2742. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2743. {
  2744. CheckDisconnectionStatusRecovery();
  2745. }
  2746. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2747. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2748. {
  2749. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2750. {
  2751. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2752. StopChargingProcessByString(2);
  2753. }
  2754. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2755. {
  2756. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2757. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2758. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2759. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2760. }
  2761. }
  2762. else
  2763. {
  2764. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2765. {
  2766. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2767. ReleaseChargingProcessByString(2);
  2768. }
  2769. }
  2770. // RB
  2771. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2772. {
  2773. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2774. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2775. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2776. {
  2777. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2778. {
  2779. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2780. StopChargingProcessByString(2);
  2781. }
  2782. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2783. {
  2784. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2785. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2786. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2787. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2788. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2789. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2790. }
  2791. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2792. {
  2793. PRINTF_FUNC("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2794. }
  2795. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2796. }
  2797. else
  2798. {
  2799. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2800. {
  2801. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2802. ReleaseChargingProcessByString(2);
  2803. }
  2804. }
  2805. }
  2806. else
  2807. {
  2808. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2809. {
  2810. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2811. ReleaseChargingProcessByString(2);
  2812. }
  2813. }
  2814. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2815. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2816. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2817. {
  2818. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2819. {
  2820. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2821. StopChargingProcessByString(2);
  2822. }
  2823. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2824. {
  2825. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2826. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2827. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2828. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2829. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2830. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2831. }
  2832. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2833. {
  2834. PRINTF_FUNC("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2835. }
  2836. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2837. }
  2838. else
  2839. {
  2840. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2841. {
  2842. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2843. ReleaseChargingProcessByString(2);
  2844. }
  2845. }
  2846. }
  2847. /*
  2848. //===============================================
  2849. // 確認 GPIO 狀態
  2850. //===============================================
  2851. void gpio_set_value(unsigned int gpio, unsigned int value)
  2852. {
  2853. int fd;
  2854. char buf[MAX_BUF];
  2855. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2856. fd = open(buf, O_WRONLY);
  2857. if (fd < 0)
  2858. {
  2859. perror("gpio/set-value");
  2860. return;
  2861. }
  2862. if (value)
  2863. write(fd, "1", 2);
  2864. else
  2865. write(fd, "0", 2);
  2866. close(fd);
  2867. }
  2868. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2869. {
  2870. int fd;
  2871. char buf[MAX_BUF];
  2872. char ch;
  2873. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2874. fd = open(buf, O_RDONLY);
  2875. if (fd < 0) {
  2876. perror("gpio/get-value");
  2877. return fd;
  2878. }
  2879. read(fd, &ch, 1);
  2880. if (ch != '0') {
  2881. *value = 1;
  2882. } else {
  2883. *value = 0;
  2884. }
  2885. close(fd);
  2886. return 0;
  2887. }
  2888. void CheckGpioInStatus()
  2889. {
  2890. int pinIn[2] = { 27, 47 };
  2891. unsigned int gpioValue = 0;
  2892. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2893. {
  2894. gpio_get_value(pinIn[i], &gpioValue);
  2895. if (gpioValue == 0x01)
  2896. {
  2897. switch(pinIn[i])
  2898. {
  2899. // 小板緊急停止
  2900. case 47:
  2901. {
  2902. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2903. {
  2904. if (chargingInfo[i]->slotsIndex == 1)
  2905. {
  2906. if (chargingInfo[i]->Type == _Type_Chademo)
  2907. BoardErrOccurByString(i, "023730");
  2908. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2909. BoardErrOccurByString(i, "013627");
  2910. break;
  2911. }
  2912. }
  2913. }
  2914. break;
  2915. case 27:
  2916. {
  2917. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2918. {
  2919. if (chargingInfo[i]->slotsIndex == 3)
  2920. {
  2921. if (chargingInfo[i]->Type == _Type_Chademo)
  2922. BoardErrOccurByString(i, "023730");
  2923. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2924. BoardErrOccurByString(i, "013627");
  2925. break;
  2926. }
  2927. }
  2928. }
  2929. break;
  2930. }
  2931. }
  2932. else
  2933. {
  2934. switch (pinIn[i])
  2935. {
  2936. // 小板解除緊急停止
  2937. case 47:
  2938. {
  2939. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2940. {
  2941. if (chargingInfo[i]->slotsIndex == 1)
  2942. {
  2943. if (chargingInfo[i]->Type == _Type_Chademo)
  2944. ReleaseBoardErrOccurByString(i, "023730");
  2945. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2946. ReleaseBoardErrOccurByString(i, "013627");
  2947. break;
  2948. }
  2949. }
  2950. }
  2951. break;
  2952. case 27:
  2953. {
  2954. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2955. {
  2956. if (chargingInfo[i]->slotsIndex == 3)
  2957. {
  2958. if (chargingInfo[i]->Type == _Type_Chademo)
  2959. ReleaseBoardErrOccurByString(i, "023730");
  2960. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2961. ReleaseBoardErrOccurByString(i, "013627");
  2962. break;
  2963. }
  2964. }
  2965. }
  2966. break;
  2967. }
  2968. }
  2969. }
  2970. }
  2971. */
  2972. //===============================================
  2973. // Main process
  2974. //===============================================
  2975. // 檢查 Byte 中某個 Bit 的值
  2976. // _byte : 欲改變的 byte
  2977. // _bit : 該 byte 的第幾個 bit
  2978. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2979. {
  2980. return ( _byte & mask_table[_bit] ) != 0x00;
  2981. }
  2982. // 設定 Byte 中某個 Bit的值
  2983. // _byte : 欲改變的 byte
  2984. // _bit : 該 byte 的第幾個 bit
  2985. // value : 修改的值為 0 or 1
  2986. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2987. {
  2988. if(value == 1)
  2989. *_byte |= (1 << _bit);
  2990. else if (value == 0)
  2991. *_byte ^= (1 << _bit);
  2992. }
  2993. void UserScanFunction()
  2994. {
  2995. }
  2996. unsigned char isModeChange(unsigned char gun_index)
  2997. {
  2998. unsigned char result = NO;
  2999. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3000. {
  3001. result = YES;
  3002. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3003. }
  3004. return result;
  3005. }
  3006. void ScannerCardProcess()
  3007. {
  3008. }
  3009. struct timeval _StartAuthorizing_Time;
  3010. void PowerCabinetAuthorizingSettingInitial(void)
  3011. {
  3012. ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = 0;
  3013. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = 0;
  3014. ShmSysConfigAndInfo->SysInfo.AuthorizedType = _AuthResult_None;
  3015. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Idle;
  3016. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = false;
  3017. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  3018. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = false;
  3019. }
  3020. void AnnounceAccountBalance(unsigned char index, int balance)
  3021. {
  3022. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  3023. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  3024. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  3025. {
  3026. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AccountBalanceRequest = true;
  3027. }
  3028. PRINTF_FUNC("%sAnnounce Account Balance %d To Connector %d",
  3029. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  3030. }
  3031. void FouceAnnounceAccountBalance(unsigned char index)
  3032. {
  3033. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  3034. {
  3035. AnnounceAccountBalance(index, 0);
  3036. }
  3037. }
  3038. void CleanConnectorUserIdAndPrice(unsigned char index)
  3039. {
  3040. if(chargingInfo[index]->SystemStatus == S_IDLE)
  3041. {
  3042. PRINTF_FUNC("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  3043. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  3044. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  3045. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -6553500;
  3046. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  3047. }
  3048. }
  3049. void DispenserAuthorizationInitial(unsigned char dispenser)
  3050. {
  3051. PRINTF_FUNC("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  3052. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].UserId, 0x00, 32);
  3053. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Idle;
  3054. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthTarget = 0;
  3055. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType = _AuthType_None;
  3056. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_None;
  3057. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.EnableAutoGunSelection = false;
  3058. }
  3059. void ConnectorAuthorizationInitial(unsigned char connector)
  3060. {
  3061. PRINTF_FUNC("*********** Connector %d Authorizing Initial ***********", connector + 1);
  3062. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingType = _AuthType_None;
  3063. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingResult = _AuthResult_None;
  3064. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.SwipeRfidConfirm = false;
  3065. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest = false;
  3066. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartConfirm = false;
  3067. chargingInfo[connector]->RemoteStartFlag = false;
  3068. CleanConnectorUserIdAndPrice(connector);
  3069. }
  3070. void SetAuthorizingTarget(unsigned char dispenser, unsigned char target, unsigned char type)
  3071. {
  3072. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  3073. switch(type)
  3074. {
  3075. case _AuthType_RFID:
  3076. PRINTF_FUNC("*********** Dispenser %d Target %02X RFID Authorize ***********", dispenser + 1, target);
  3077. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].UserId, 32);
  3078. break;
  3079. case _AuthType_RemoteStart:
  3080. PRINTF_FUNC("*********** Connector %d Remote Start Authorize ***********", target);
  3081. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[target - 1].IdTag, 20);
  3082. break;
  3083. }
  3084. if(target == AUTO_GUN_SELECTION)
  3085. {
  3086. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.EnableAutoGunSelection = true;
  3087. }
  3088. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Busy;
  3089. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType = type;
  3090. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = true;
  3091. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  3092. ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = dispenser;
  3093. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = target;
  3094. ShmSysConfigAndInfo->SysInfo.AuthorizedType = type;
  3095. }
  3096. void OCPPAuthorizingStart(void)
  3097. {
  3098. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3099. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3100. }
  3101. void AuthorizeTimeoutProcess(void)
  3102. {
  3103. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3104. {
  3105. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizingResult == _AuthResult_Invalid)
  3106. {
  3107. FouceAnnounceAccountBalance(index);
  3108. if(GetTimeoutValue(_ConnectorAuthorizeFail_Time[index]) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3109. {
  3110. PRINTF_FUNC("*********** Connector id %d Authorized Fail Return ***********", index + 1);
  3111. ConnectorAuthorizationInitial(index);
  3112. }
  3113. }
  3114. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthStatus == _AuthorizeStatus_End)
  3115. {
  3116. int CompletedTime = 0;
  3117. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthTarget != AUTO_GUN_SELECTION)
  3118. {
  3119. CompletedTime = DISPENSER_AUTH_COMP_TIME;
  3120. }
  3121. else
  3122. {
  3123. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthResult == _AuthResult_Valid)
  3124. {
  3125. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  3126. }
  3127. else
  3128. {
  3129. CompletedTime = AUTHORIZE_FAIL_TIMEOUT;
  3130. }
  3131. }
  3132. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= CompletedTime)
  3133. {
  3134. PRINTF_FUNC("*********** Dispenser id %d Authorized Return ***********", index + 1);
  3135. DispenserAuthorizationInitial(index);
  3136. }
  3137. }
  3138. }
  3139. }
  3140. bool IsNewAuthorizingRequest(void)
  3141. {
  3142. bool NewRequest = false;
  3143. bool FindRemoteStartRequest = false;
  3144. // remote start check
  3145. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3146. {
  3147. if(ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq == true)
  3148. {
  3149. unsigned char dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].ParentDispensetIndex;
  3150. SetAuthorizingTarget(dispenser, index + 1, _AuthType_RemoteStart);
  3151. ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq = false;
  3152. chargingInfo[index]->RemoteStartFlag = true;
  3153. FindRemoteStartRequest = true;
  3154. NewRequest = true;
  3155. break;
  3156. }
  3157. }
  3158. if(!FindRemoteStartRequest)
  3159. {
  3160. // local rfid authorize check
  3161. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3162. {
  3163. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthStatus == _AuthorizeStatus_Idle &&
  3164. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest == true)
  3165. {
  3166. if(strlen((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId) > 0)
  3167. {
  3168. SetAuthorizingTarget(index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthTarget, _AuthType_RFID);
  3169. NewRequest = true;
  3170. }
  3171. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest = false;
  3172. }
  3173. }
  3174. }
  3175. return NewRequest;
  3176. }
  3177. void PowerCabinetAuthorizeProcess(void)
  3178. {
  3179. unsigned char dispenser = 0, connector = 0;
  3180. dispenser = ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser;
  3181. AuthorizeTimeoutProcess();
  3182. switch(ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  3183. {
  3184. case _AuthorizeStatus_Idle:
  3185. if(IsNewAuthorizingRequest())
  3186. {
  3187. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  3188. }
  3189. break;
  3190. case _AuthorizeStatus_Wait:
  3191. if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RemoteStart)
  3192. {
  3193. PRINTF_FUNC("*********** Connector %d Remote Start Authorizing Bypass ***********",
  3194. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget);
  3195. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = true;
  3196. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Valid;
  3197. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3198. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3199. }
  3200. else
  3201. {
  3202. if(ShmOCPP16Data->OcppConnStatus)
  3203. {
  3204. PRINTF_FUNC("*********** Dispenser %d OPCC AuthorizeReq ***********", dispenser + 1);
  3205. OCPPAuthorizingStart();
  3206. }
  3207. else
  3208. {
  3209. PRINTF_FUNC("*********** Dispenser %d Start Local Authorizing ***********", dispenser + 1);
  3210. }
  3211. // update start authorizing timeout
  3212. gettimeofday(&_StartAuthorizing_Time, NULL);
  3213. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Busy;
  3214. }
  3215. break;
  3216. case _AuthorizeStatus_Busy:
  3217. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3218. {
  3219. PRINTF_FUNC("*********** Dispenser %d Authorizing Timeout ***********", dispenser + 1);
  3220. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Invalid;
  3221. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3222. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3223. }
  3224. else
  3225. {
  3226. if(ShmOCPP16Data->OcppConnStatus)
  3227. {
  3228. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf)
  3229. {
  3230. BOOL accept = false;
  3231. if(strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == EQUAL)
  3232. {
  3233. accept = true;
  3234. }
  3235. PRINTF_FUNC("*********** Dispenser %d OCPP Authorize %s ***********", dispenser + 1, accept ? "OK" : "NG");
  3236. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = true;
  3237. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = accept ? _AuthResult_Valid : _AuthResult_Invalid;
  3238. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3239. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3240. }
  3241. }
  3242. else
  3243. {
  3244. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3245. {
  3246. PRINTF_FUNC("*********** Dispenser %d No Charging ***********", dispenser + 1);
  3247. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Invalid;
  3248. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3249. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3250. }
  3251. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  3252. {
  3253. // set authorize pass when offline policy is free charge
  3254. PRINTF_FUNC("*********** Dispenser %d Free Charging ***********", dispenser + 1);
  3255. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Valid;
  3256. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3257. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3258. }
  3259. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3260. {
  3261. BOOL find = false;
  3262. // check white list
  3263. for(int i = 0; i < 10; i++)
  3264. {
  3265. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3266. {
  3267. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3268. {
  3269. find = true;
  3270. break;
  3271. }
  3272. }
  3273. }
  3274. PRINTF_FUNC("*********** Dispenser %d White Card %s ***********", dispenser + 1, find ? "OK" : "NG");
  3275. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = find ? _AuthResult_Valid : _AuthResult_Invalid;
  3276. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3277. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3278. }
  3279. else
  3280. {
  3281. PRINTF_FUNC("*********** Dispenser %d Invalid Policy ***********", dispenser + 1);
  3282. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Invalid;
  3283. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3284. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3285. }
  3286. }
  3287. }
  3288. break;
  3289. case _AuthorizeStatus_Done:
  3290. if(ShmSysConfigAndInfo->SysInfo.AuthorizedTarget == AUTO_GUN_SELECTION)
  3291. {
  3292. // auto gun selection
  3293. }
  3294. else
  3295. {
  3296. connector = ShmSysConfigAndInfo->SysInfo.AuthorizedTarget - 1;
  3297. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingType = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType;
  3298. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingResult = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult;
  3299. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].GeneralChargingData.SystemStatus == S_IDLE &&
  3300. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult == _AuthResult_Valid)
  3301. {
  3302. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType == _AuthType_RFID)
  3303. {
  3304. PRINTF_FUNC("*********** Connector %d RFID Authorize OK ***********", connector + 1);
  3305. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  3306. }
  3307. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType == _AuthType_RemoteStart)
  3308. {
  3309. PRINTF_FUNC("*********** Connector id %d RemoteAuthorize OK ***********", connector + 1);
  3310. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  3311. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest = true;
  3312. }
  3313. }
  3314. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult == _AuthResult_Invalid)
  3315. {
  3316. // update connector authorizing timeout
  3317. gettimeofday(&_ConnectorAuthorizeFail_Time[connector], NULL);
  3318. }
  3319. }
  3320. // authorize completed
  3321. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = true;
  3322. // update dispenser authorizing timeout
  3323. gettimeofday(&_DispenserAuthorizing_Time[dispenser], NULL);
  3324. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_End;
  3325. // update authorizing completed timeout
  3326. gettimeofday(&_StartAuthorizing_Time, NULL);
  3327. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  3328. break;
  3329. case _AuthorizeStatus_End:
  3330. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3331. {
  3332. PRINTF_FUNC("*********** PowerCabinet Authorize Completed ***********");
  3333. PowerCabinetAuthorizingSettingInitial();
  3334. }
  3335. break;
  3336. default:
  3337. // update authorizing completed timeout
  3338. gettimeofday(&_StartAuthorizing_Time, NULL);
  3339. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  3340. break;
  3341. }
  3342. }
  3343. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  3344. {
  3345. BOOL success = false;
  3346. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingResult == _AuthResult_Valid)
  3347. {
  3348. success = true;
  3349. }
  3350. return success;
  3351. }
  3352. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  3353. {
  3354. BOOL success = false;
  3355. unsigned char dispenser = 0;
  3356. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  3357. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.EnableAutoGunSelection &&
  3358. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult == _AuthResult_Valid)
  3359. {
  3360. success = true;
  3361. }
  3362. return success;
  3363. }
  3364. /*
  3365. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3366. {
  3367. bool result = true;
  3368. switch (typeValue)
  3369. {
  3370. case '0': // none
  3371. break;
  3372. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3373. break;
  3374. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3375. break;
  3376. case '3': // IEC 62196-2 Type 2 Plug
  3377. case '4': // IEC 62196-2 Type 2 Socket
  3378. if (AC_QUANTITY > _ac_Index)
  3379. {
  3380. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3381. // AC 固定 index
  3382. ac_chargingInfo[_acgunIndex]->Index = 0;
  3383. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3384. ac_chargingInfo[_acgunIndex]->SystemStatus = S_IDLE;
  3385. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3386. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3387. _ac_Index++;
  3388. _acgunIndex++;
  3389. }
  3390. else
  3391. result = false;
  3392. break;
  3393. case '5': // GB/T AC Plug
  3394. break;
  3395. case '6': // GB/T AC Socket
  3396. break;
  3397. case 'J': // CHAdeMO
  3398. {
  3399. if (CHAdeMO_QUANTITY > _chademoIndex)
  3400. {
  3401. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3402. chargingInfo[_gunIndex]->Index = _gunIndex;
  3403. chargingInfo[_gunIndex]->ReservationId = -1;
  3404. chargingInfo[_gunIndex]->slotsIndex = slots;
  3405. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3406. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3407. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3408. chargingInfo[_gunIndex]->IsAvailable = YES;
  3409. _chademoIndex++;
  3410. _gunIndex++;
  3411. }
  3412. else
  3413. result = false;
  3414. }
  3415. break;
  3416. case 'U': // CCS1 combo
  3417. case 'E': // CCS2 combo
  3418. {
  3419. if (CCS_QUANTITY > _ccsIndex)
  3420. {
  3421. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3422. chargingInfo[_gunIndex]->Index = _gunIndex;
  3423. chargingInfo[_gunIndex]->ReservationId = -1;
  3424. chargingInfo[_gunIndex]->slotsIndex = slots;
  3425. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3426. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  3427. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3428. chargingInfo[_gunIndex]->IsAvailable = YES;
  3429. // 現階段預設為走 DIN70121
  3430. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3431. _ccsIndex++;
  3432. _gunIndex++;
  3433. }
  3434. else
  3435. result = false;
  3436. }
  3437. break;
  3438. case 'G': // GBT DC
  3439. {
  3440. if (GB_QUANTITY > _gb_Index)
  3441. {
  3442. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3443. chargingInfo[_gunIndex]->Index = _gunIndex;
  3444. chargingInfo[_gunIndex]->ReservationId = -1;
  3445. chargingInfo[_gunIndex]->slotsIndex = slots;
  3446. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3447. chargingInfo[_gunIndex]->Type = _Type_GB;
  3448. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3449. chargingInfo[_gunIndex]->IsAvailable = YES;
  3450. _gb_Index++;
  3451. _gunIndex++;
  3452. }
  3453. else
  3454. result = false;
  3455. }
  3456. break;
  3457. case 'D': // GBT DC x 2
  3458. break;
  3459. }
  3460. return result;
  3461. }
  3462. */
  3463. bool CheckConnectorTypeStatus(void)
  3464. {
  3465. bool result = true;
  3466. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  3467. {
  3468. // DO360 for Audi, two gun only
  3469. _gunIndex = ShmChargerInfo->Control.MaxConnector;
  3470. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3471. PRINTF_FUNC("DC Gun Count = %d", ShmSysConfigAndInfo->SysConfig.TotalConnectorCount);
  3472. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  3473. {
  3474. result = false;
  3475. }
  3476. //DO360 charging info default setting
  3477. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3478. {
  3479. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  3480. chargingInfo[i]->Index = 0xFF;
  3481. chargingInfo[i]->Evboard_id = 0x00;
  3482. chargingInfo[i]->ReservationId = -1;
  3483. chargingInfo[i]->slotsIndex = 0;
  3484. chargingInfo[i]->SystemStatus = S_BOOTING;
  3485. chargingInfo[i]->Type = _Type_Unknown;
  3486. chargingInfo[i]->type_index = 0;
  3487. chargingInfo[i]->IsAvailable = YES;
  3488. }
  3489. }
  3490. else
  3491. {
  3492. // Module Name 不正確 - 告警
  3493. result = false;
  3494. }
  3495. return result;
  3496. }
  3497. void KillTaskExceptPrimary()
  3498. {
  3499. ChangeLcmByIndex(_LCM_FIX);
  3500. system("killall Module_EvComm");
  3501. system("killall Module_InternalComm");
  3502. system("killall Module_PsuComm");
  3503. system("killall Module_4g &");
  3504. system("killall Module_Wifi &");
  3505. }
  3506. void KillAllTask()
  3507. {
  3508. ChangeLcmByIndex(_LCM_FIX);
  3509. system("killall Module_EventLogging");
  3510. system("killall Module_PrimaryComm");
  3511. system("killall Module_EvComm");
  3512. system("killall Module_LcmControl");
  3513. system("killall Module_InternalComm");
  3514. system("killall Module_PsuComm");
  3515. system("killall OcppBackend &");
  3516. system("killall Module_4g &");
  3517. system("killall Module_Wifi &");
  3518. }
  3519. int PutDispenserImage(char *IpAddress, char *SourcePath)
  3520. {
  3521. int isPass = PASS;
  3522. unsigned char ftpcmdbuf[256];
  3523. PRINTF_FUNC("Transfer image(%s) to ip: %s ", SourcePath, IpAddress);
  3524. //Using ftpput command to transfer upgrade image,
  3525. //User name : root
  3526. //User password : y42j/4cj84
  3527. //Destination : as same as source path
  3528. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  3529. if(system((char*)ftpcmdbuf) != 0)
  3530. {
  3531. isPass = FAIL;
  3532. PRINTF_FUNC("Transfer Image NG - FTP put upgrade image to fail");
  3533. }
  3534. else
  3535. {
  3536. PRINTF_FUNC("FTP put Image OK");
  3537. }
  3538. return isPass;
  3539. }
  3540. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  3541. {
  3542. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  3543. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  3544. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  3545. PRINTF_FUNC("Trigger dispenser %d to update image %s", index + 1, FileName);
  3546. }
  3547. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName)
  3548. {
  3549. int isPass = FAIL;
  3550. BOOL transferred = FALSE;
  3551. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3552. {
  3553. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  3554. {
  3555. char DispenserIpAddress[16];
  3556. memset((char*)DispenserIpAddress, 0x00, 16);
  3557. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  3558. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  3559. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  3560. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  3561. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  3562. isPass = PutDispenserImage(DispenserIpAddress, SourcePath);
  3563. if(isPass == PASS)
  3564. {
  3565. TriggerDispenserUpgrade(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex, FileName);
  3566. }
  3567. transferred = true;
  3568. }
  3569. }
  3570. return transferred;
  3571. }
  3572. BOOL WaitAllDispenserUpgradeCompleted(void)
  3573. {
  3574. BOOL completed = TRUE;
  3575. gettimeofday(&_DispenserUpgrade_time, NULL);
  3576. do
  3577. {
  3578. completed = TRUE;
  3579. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3580. {
  3581. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  3582. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  3583. {
  3584. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  3585. {
  3586. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  3587. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  3588. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  3589. PRINTF_FUNC("Dispenser %d Upgrade Success", i + 1);
  3590. }
  3591. else
  3592. {
  3593. completed = FALSE;
  3594. }
  3595. }
  3596. }
  3597. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  3598. {
  3599. PRINTF_FUNC("Wait Upgrade Timeout");
  3600. completed = FALSE;
  3601. break;
  3602. }
  3603. if(completed == FALSE)
  3604. {
  3605. sleep(1);
  3606. }
  3607. }while(!completed);
  3608. return completed;
  3609. }
  3610. bool IsModelNameMatch(char *model)
  3611. {
  3612. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  3613. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  3614. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  3615. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  3616. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  3617. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  3618. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  3619. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  3620. {
  3621. return true;
  3622. }
  3623. return false;
  3624. }
  3625. char CheckUpdateProcess()
  3626. {
  3627. char model[32];
  3628. bool support = false;
  3629. bool OneImageOnly = false;
  3630. int isPass = true;
  3631. uint8_t retSucc = 0;
  3632. uint8_t retFail = 0;
  3633. byte target = 0;
  3634. char *new_str = NULL;
  3635. unsigned char *ptr = NULL;
  3636. int fd = 0;
  3637. int uartFd = 0;
  3638. unsigned int Type = 0;
  3639. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3640. DIR *d;
  3641. struct dirent *dir;
  3642. char cmdBuf[128];
  3643. d = opendir("/mnt/");
  3644. if(d)
  3645. {
  3646. while((dir = readdir(d)) != NULL && !OneImageOnly)
  3647. {
  3648. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  3649. continue;
  3650. }
  3651. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  3652. strcat(new_str, "/mnt/");
  3653. strcat(new_str, dir->d_name);
  3654. fd = open(new_str, O_RDONLY);
  3655. if(fd < 0)
  3656. {
  3657. close(fd);
  3658. free(new_str);
  3659. continue;
  3660. }
  3661. DEBUG_INFO_MSG("%s\n", new_str);
  3662. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  3663. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3664. //get the image length
  3665. ImageLen = read(fd, ptr, MaxLen);
  3666. if (ImageLen > 20)
  3667. {
  3668. support = false;
  3669. memcpy(model, ptr, 16);
  3670. PRINTF_FUNC("Model: %s", model);
  3671. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  3672. PRINTF_FUNC("Typed...%x", Type);
  3673. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  3674. if(IsModelNameMatch(model))
  3675. {
  3676. switch (Type)
  3677. {
  3678. case 0x10000001:
  3679. case 0x10000002:
  3680. case 0x10000003:
  3681. case 0x10000004:
  3682. case 0x10000005:
  3683. support = true;
  3684. _UpgradeNeedReboot = TRUE;
  3685. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3686. break;
  3687. case 0x10000006: // primary mcu
  3688. case 0x1000000D: // relay board
  3689. case 0x1000000E: // fan board
  3690. case 0x10000014: // led board
  3691. support = true;
  3692. target = 0x00;
  3693. if (Type == 0x10000006)
  3694. {
  3695. target = UPGRADE_PRI;
  3696. }
  3697. else if (Type == 0x1000000D)
  3698. {
  3699. target = UPGRADE_RB;
  3700. }
  3701. else if (Type == 0x1000000E)
  3702. {
  3703. target = UPGRADE_FAN;
  3704. }
  3705. else if (Type == 0x10000014)
  3706. {
  3707. target = UPGRADE_LED;
  3708. }
  3709. uartFd = InitComPort(target);
  3710. if(target == UPGRADE_RB)
  3711. {
  3712. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3713. if(isPass == PASS)
  3714. {
  3715. retSucc++;
  3716. }
  3717. else
  3718. {
  3719. retFail++;
  3720. }
  3721. PRINTF_FUNC("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  3722. sleep(3);
  3723. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3724. }
  3725. else
  3726. {
  3727. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3728. }
  3729. close(uartFd);
  3730. break;
  3731. }
  3732. if(support)
  3733. {
  3734. if(isPass == PASS)
  3735. {
  3736. retSucc++;
  3737. }
  3738. else
  3739. {
  3740. retFail++;
  3741. }
  3742. PRINTF_FUNC("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  3743. }
  3744. }
  3745. else
  3746. {
  3747. if(model[0] == 'D' && (model[1] == 'D' || model[1] == 'X'))
  3748. {
  3749. OneImageOnly = true;
  3750. _UpgradeNeedReboot = TRUE;
  3751. PRINTF_FUNC("Start Put Image To All Dispenser");
  3752. PutImageToAllDispenser(new_str, dir->d_name);
  3753. WaitAllDispenserUpgradeCompleted();
  3754. PRINTF_FUNC("All Dispenser Upgrade Completed");
  3755. }
  3756. }
  3757. }
  3758. sprintf(cmdBuf, "rm -f %s", new_str);
  3759. system(cmdBuf);
  3760. close(fd);
  3761. free(new_str);
  3762. free(ptr);
  3763. }
  3764. }
  3765. free(dir);
  3766. closedir(d);
  3767. if(retFail != 0)
  3768. {
  3769. return FAIL;
  3770. }
  3771. return PASS;
  3772. }
  3773. /*
  3774. void CreateRfidFork()
  3775. {
  3776. pid_t rfidRecPid;
  3777. rfidRecPid = fork();
  3778. if (rfidRecPid == 0)
  3779. {
  3780. while(true)
  3781. {
  3782. // 刷卡判斷
  3783. RFID rfid;
  3784. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  3785. !ShmSysConfigAndInfo->SysConfig.isRFID)
  3786. {}
  3787. else if(getRequestCardSN(rfidFd, 0, &rfid))
  3788. {
  3789. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3790. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  3791. {
  3792. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  3793. {
  3794. switch (rfid.snType)
  3795. {
  3796. case RFID_SN_TYPE_6BYTE:
  3797. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3798. "%02X%02X%02X%02X%02X%02X",
  3799. rfid.currentCard[0], rfid.currentCard[1],
  3800. rfid.currentCard[2], rfid.currentCard[3],
  3801. rfid.currentCard[4], rfid.currentCard[5]);
  3802. break;
  3803. case RFID_SN_TYPE_7BYTE:
  3804. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3805. "%02X%02X%02X%02X%02X%02X%02X",
  3806. rfid.currentCard[0], rfid.currentCard[1],
  3807. rfid.currentCard[2], rfid.currentCard[3],
  3808. rfid.currentCard[4], rfid.currentCard[5],
  3809. rfid.currentCard[6]);
  3810. break;
  3811. case RFID_SN_TYPE_10BYTE:
  3812. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3813. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3814. rfid.currentCard[0], rfid.currentCard[1],
  3815. rfid.currentCard[2], rfid.currentCard[3],
  3816. rfid.currentCard[4], rfid.currentCard[5],
  3817. rfid.currentCard[6], rfid.currentCard[7],
  3818. rfid.currentCard[8], rfid.currentCard[9]);
  3819. break;
  3820. case RFID_SN_TYPE_4BYTE:
  3821. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3822. "%02X%02X%02X%02X",
  3823. rfid.currentCard[0], rfid.currentCard[1],
  3824. rfid.currentCard[2], rfid.currentCard[3]);
  3825. break;
  3826. }
  3827. }
  3828. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  3829. {
  3830. switch (rfid.snType)
  3831. {
  3832. case RFID_SN_TYPE_6BYTE:
  3833. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3834. "%02X%02X%02X%02X%02X%02X",
  3835. rfid.currentCard[5], rfid.currentCard[4],
  3836. rfid.currentCard[3], rfid.currentCard[2],
  3837. rfid.currentCard[1], rfid.currentCard[0]);
  3838. break;
  3839. case RFID_SN_TYPE_7BYTE:
  3840. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3841. "%02X%02X%02X%02X%02X%02X%02X",
  3842. rfid.currentCard[6], rfid.currentCard[5],
  3843. rfid.currentCard[4], rfid.currentCard[3],
  3844. rfid.currentCard[2], rfid.currentCard[1],
  3845. rfid.currentCard[0]);
  3846. break;
  3847. case RFID_SN_TYPE_10BYTE:
  3848. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3849. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3850. rfid.currentCard[9], rfid.currentCard[8],
  3851. rfid.currentCard[7], rfid.currentCard[6],
  3852. rfid.currentCard[5], rfid.currentCard[4],
  3853. rfid.currentCard[3], rfid.currentCard[2],
  3854. rfid.currentCard[1], rfid.currentCard[0]);
  3855. break;
  3856. case RFID_SN_TYPE_4BYTE:
  3857. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3858. "%02X%02X%02X%02X",
  3859. rfid.currentCard[3], rfid.currentCard[2],
  3860. rfid.currentCard[1], rfid.currentCard[0]);
  3861. break;
  3862. }
  3863. }
  3864. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3865. }
  3866. }
  3867. sleep(1);
  3868. }
  3869. }
  3870. }
  3871. */
  3872. void StartSystemTimeoutDet(unsigned char flag)
  3873. {
  3874. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  3875. {
  3876. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3877. }
  3878. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3879. }
  3880. void StopSystemTimeoutDet()
  3881. {
  3882. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3883. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3884. }
  3885. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3886. {
  3887. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3888. {
  3889. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  3890. {
  3891. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  3892. }
  3893. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3894. }
  3895. }
  3896. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3897. {
  3898. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3899. {
  3900. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3901. }
  3902. }
  3903. void TriggerDispenserConnectionTimeoutSetting(void)
  3904. {
  3905. BOOL trigger = FALSE;
  3906. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3907. {
  3908. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  3909. {
  3910. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  3911. trigger = TRUE;
  3912. }
  3913. }
  3914. if(trigger)
  3915. {
  3916. PRINTF_FUNC("********** Trigger Dispenser Connector Timeout Value: %d **********\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout);
  3917. }
  3918. }
  3919. void CheckConnectionTimeout(void)
  3920. {
  3921. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3922. {
  3923. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3924. }
  3925. else
  3926. {
  3927. if(ShmOCPP16Data->OcppConnStatus &&
  3928. strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  3929. {
  3930. _connectionTimeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3931. if(_connectionTimeout <= 0)
  3932. {
  3933. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3934. }
  3935. }
  3936. else
  3937. {
  3938. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3939. }
  3940. }
  3941. if(_connectionTimeout != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  3942. {
  3943. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout = _connectionTimeout;
  3944. TriggerDispenserConnectionTimeoutSetting();
  3945. }
  3946. }
  3947. void CreateTimeoutFork()
  3948. {
  3949. pid_t timeoutPid;
  3950. timeoutPid = fork();
  3951. if (timeoutPid > 0)
  3952. {
  3953. gettimeofday(&_cmdSubPriority_time, NULL);
  3954. CheckConnectionTimeout();
  3955. while(true)
  3956. {
  3957. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  3958. {
  3959. CheckConnectionTimeout();
  3960. gettimeofday(&_cmdSubPriority_time, NULL);
  3961. }
  3962. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3963. // 系統
  3964. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  3965. {
  3966. // case Timeout_SelftestChk:
  3967. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT)
  3968. // {
  3969. // _SelfTestTimeout();
  3970. // StopSystemTimeoutDet();
  3971. // }
  3972. // break;
  3973. // case Timeout_Authorizing:
  3974. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3975. // {
  3976. // _AuthorizedTimeout();
  3977. // StopSystemTimeoutDet();
  3978. // }
  3979. // break;
  3980. // case Timeout_VerifyFail:
  3981. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3982. // {
  3983. // _AutoReturnTimeout();
  3984. // StopSystemTimeoutDet();
  3985. // }
  3986. // break;
  3987. // case Timeout_VerifyComp:
  3988. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3989. // {
  3990. // _AutoReturnTimeout();
  3991. // StopSystemTimeoutDet();
  3992. // }
  3993. // break;
  3994. case Timeout_WaitPlug:
  3995. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout)
  3996. {
  3997. _DetectPlugInTimeout();
  3998. StopSystemTimeoutDet();
  3999. }
  4000. break;
  4001. case Timeout_ReturnToChargingGunDet:
  4002. {
  4003. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE)
  4004. {
  4005. DisplayChargingInfo();
  4006. StopSystemTimeoutDet();
  4007. }
  4008. }
  4009. break;
  4010. case Timeout_AuthorizingForStop:
  4011. {
  4012. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4013. {
  4014. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4015. ClearAuthorizedFlag();
  4016. StopSystemTimeoutDet();
  4017. }
  4018. }
  4019. break;
  4020. }
  4021. // 各槍
  4022. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4023. {
  4024. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4025. switch(chargingInfo[gun_index]->TimeoutFlag)
  4026. {
  4027. case Timeout_Preparing:
  4028. {
  4029. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT)
  4030. {
  4031. _PrepareTimeout(gun_index);
  4032. StopGunInfoTimeoutDet(gun_index);
  4033. }
  4034. }
  4035. break;
  4036. case Timeout_EvChargingDet:
  4037. {
  4038. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT)
  4039. {
  4040. _DetectEvChargingEnableTimeout(gun_index);
  4041. StopGunInfoTimeoutDet(gun_index);
  4042. }
  4043. }
  4044. break;
  4045. case Timeout_EvseChargingDet:
  4046. {
  4047. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT)
  4048. {
  4049. _DetectEvseChargingEnableTimeout(gun_index);
  4050. StopGunInfoTimeoutDet(gun_index);
  4051. }
  4052. }
  4053. break;
  4054. case Timeout_EvseCompleteDet:
  4055. {
  4056. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT)
  4057. {
  4058. StopGunInfoTimeoutDet(gun_index);
  4059. }
  4060. }
  4061. break;
  4062. case Timeout_ForCcsPrechargeDet:
  4063. {
  4064. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT)
  4065. {
  4066. _CcsPrechargeTimeout(gun_index);
  4067. StopGunInfoTimeoutDet(gun_index);
  4068. }
  4069. }
  4070. break;
  4071. }
  4072. }
  4073. sleep(1);
  4074. }
  4075. }
  4076. }
  4077. void GetSystemTime()
  4078. {
  4079. struct timeb csuTime;
  4080. struct tm *tmCSU;
  4081. ftime(&csuTime);
  4082. tmCSU = localtime(&csuTime.time);
  4083. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4084. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4085. tmCSU->tm_sec);
  4086. // byte date[14];
  4087. //
  4088. //
  4089. // //sprintf(&date, "%d", );
  4090. //
  4091. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4092. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4093. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4094. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4095. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4096. //
  4097. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4098. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4099. //
  4100. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4101. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4102. //
  4103. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4104. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4105. //
  4106. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4107. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4108. //
  4109. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4110. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4111. // 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],
  4112. // date[4], date[5], date[6], date[7],
  4113. // date[8], date[9], date[10], date[11],
  4114. // date[12], date[13]);
  4115. }
  4116. void CheckFactoryConfigFunction(void)
  4117. {
  4118. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4119. {
  4120. char Buf[256];
  4121. sprintf(Buf, "cd /root;./FactoryConfig -m");
  4122. system(Buf);
  4123. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4124. system("sync");
  4125. sleep(5);
  4126. system("reboot -f");
  4127. sleep(5);
  4128. system("reboot -f");
  4129. }
  4130. }
  4131. void CheckFwUpdateFunction()
  4132. {
  4133. char UpdateResult = PASS;
  4134. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4135. {
  4136. DEBUG_INFO_MSG("ftp : update start.");
  4137. //KillTask();
  4138. ChangeLcmByIndex(_LCM_FIX);
  4139. system("killall Module_PrimaryComm");
  4140. system("killall Module_InternalComm");
  4141. UpdateResult = CheckUpdateProcess();
  4142. if (UpdateResult == PASS)
  4143. {
  4144. DEBUG_INFO_MSG("ftp : update complete.");
  4145. }
  4146. else
  4147. {
  4148. DEBUG_INFO_MSG("ftp : update fail.");
  4149. }
  4150. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4151. sleep(5);
  4152. if(_UpgradeNeedReboot)
  4153. {
  4154. system("reboot -f");
  4155. }
  4156. else
  4157. {
  4158. KillAllTask();
  4159. system("/usr/bin/run_evse_restart.sh");
  4160. }
  4161. }
  4162. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  4163. {
  4164. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  4165. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  4166. {
  4167. DEBUG_INFO_MSG("Backend : update start. \n");
  4168. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  4169. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4170. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  4171. //KillTask();
  4172. system("killall Module_PrimaryComm");
  4173. system("killall Module_InternalComm");
  4174. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4175. {
  4176. setChargerMode(_index, MODE_UPDATE);
  4177. }
  4178. UpdateResult = CheckUpdateProcess();
  4179. if (UpdateResult == PASS)
  4180. {
  4181. DEBUG_INFO_MSG("Backend : update complete. \n");
  4182. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4183. }
  4184. else
  4185. {
  4186. DEBUG_INFO_MSG("Backend : update fail. \n");
  4187. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4188. }
  4189. //strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4190. sleep(1);
  4191. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  4192. sleep(5);
  4193. if(_UpgradeNeedReboot)
  4194. {
  4195. system("reboot -f");
  4196. }
  4197. else
  4198. {
  4199. KillAllTask();
  4200. system("/usr/bin/run_evse_restart.sh");
  4201. }
  4202. }
  4203. }
  4204. }
  4205. void ExecuteWriteWiringInfo(void)
  4206. {
  4207. struct SysConfigData config;
  4208. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  4209. config.SwitchDebugFlag = NO;
  4210. strcpy((char *) config.UserId, "");
  4211. int result = StoreUsrConfigData(&config);
  4212. if(result != 1)
  4213. {
  4214. PRINTF_FUNC("Write Wiring Info NG");
  4215. }
  4216. else
  4217. {
  4218. PRINTF_FUNC("WriteWiringInfo OK");
  4219. }
  4220. }
  4221. struct timeval _WiringInfo_time;
  4222. int CheckWiringInfoUpdate(void)
  4223. {
  4224. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.WiringInfoChanged)
  4225. {
  4226. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.WiringInfoChanged = false;
  4227. PRINTF_FUNC("Wiring Info Changed");
  4228. gettimeofday(&_WiringInfo_time, NULL);
  4229. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteWiringInfo = true;
  4230. }
  4231. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteWiringInfo)
  4232. {
  4233. // 30s for reboot request timeout
  4234. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  4235. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo)
  4236. {
  4237. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteWiringInfo = false;
  4238. PRINTF_FUNC("Start Wirte Wiring Info To Flash");
  4239. pid_t writePid = fork();
  4240. if(writePid == 0)
  4241. {
  4242. ExecuteWriteWiringInfo();
  4243. PRINTF_FUNC("Wirte Wiring Info OK");
  4244. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo)
  4245. {
  4246. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo = false;
  4247. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  4248. PRINTF_FUNC("Clean Wiring Info And Need Soft Reset");
  4249. }
  4250. return NO;
  4251. }
  4252. PRINTF_FUNC("Clean Wirte Wiring Info Flag");
  4253. }
  4254. }
  4255. return YES;
  4256. }
  4257. void CheckSoftHardReset(void)
  4258. {
  4259. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  4260. {
  4261. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  4262. PRINTF_FUNC("********** Need Soft Reset **********");
  4263. sleep(5);
  4264. system("killall OcppBackend &");
  4265. KillAllTask();
  4266. system("/usr/bin/run_evse_restart.sh");
  4267. }
  4268. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  4269. {
  4270. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  4271. PRINTF_FUNC("********** Need Hard Reset **********");
  4272. sleep(5);
  4273. system("reboot -f");
  4274. }
  4275. }
  4276. //===============================================
  4277. // Check reservation date is expired
  4278. //===============================================
  4279. int isReservationExpired(unsigned char gun_index)
  4280. {
  4281. int result = NO;
  4282. struct tm expiredDate;
  4283. struct timeb expiredTime;
  4284. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  4285. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  4286. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  4287. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  4288. {
  4289. expiredDate.tm_year -= 1900;
  4290. expiredDate.tm_mon -= 1;
  4291. expiredTime.time = mktime(&expiredDate);
  4292. if (!CheckTimeOut(expiredTime))
  4293. {
  4294. result = YES;
  4295. }
  4296. }
  4297. return result;
  4298. }
  4299. struct timeval _Reboot_time;
  4300. void TriggerDispenserHardwareReboot(void)
  4301. {
  4302. BOOL trigger = FALSE;
  4303. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4304. {
  4305. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4306. {
  4307. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  4308. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest == 0)
  4309. {
  4310. trigger = TRUE;
  4311. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest = 1;
  4312. }
  4313. }
  4314. }
  4315. if(trigger)
  4316. {
  4317. gettimeofday(&_Reboot_time, NULL);
  4318. PRINTF_FUNC("********** Trigger Dispenser Hardware Reboot **********");
  4319. }
  4320. }
  4321. BOOL IsDispenserHardwareRebootResponsed(void)
  4322. {
  4323. BOOL response_ok = TRUE;
  4324. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4325. {
  4326. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4327. {
  4328. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  4329. {
  4330. response_ok = FALSE;
  4331. // 10s for reboot request timeout
  4332. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  4333. {
  4334. response_ok = TRUE;
  4335. PRINTF_FUNC("Reboot request timeout");
  4336. }
  4337. }
  4338. }
  4339. }
  4340. return response_ok;
  4341. }
  4342. struct timeval _Reset_time;
  4343. void TriggerDispenserSoftwareReset(void)
  4344. {
  4345. BOOL trigger = FALSE;
  4346. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4347. {
  4348. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4349. {
  4350. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  4351. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest == 0)
  4352. {
  4353. trigger = TRUE;
  4354. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest = 1;
  4355. }
  4356. }
  4357. }
  4358. if(trigger)
  4359. {
  4360. gettimeofday(&_Reset_time, NULL);
  4361. PRINTF_FUNC("********** Trigger Dispenser Software Reset **********");
  4362. }
  4363. }
  4364. BOOL IsDispenserSoftwareResetResponsed(void)
  4365. {
  4366. BOOL response_ok = TRUE;
  4367. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4368. {
  4369. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4370. {
  4371. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  4372. {
  4373. response_ok = FALSE;
  4374. // 10s for reset request timeout
  4375. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  4376. {
  4377. response_ok = TRUE;
  4378. PRINTF_FUNC("Reset request timeout");
  4379. }
  4380. }
  4381. }
  4382. }
  4383. return response_ok;
  4384. }
  4385. // the unit of return value: 0.01 dollar
  4386. int TariffParsing(char *StringItem, char *TariffCode)
  4387. {
  4388. char *ptrSave, *ptrToken;
  4389. char strSource[128];
  4390. int fee = 0;
  4391. memcpy(strSource, StringItem, 128);
  4392. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4393. while(ptrToken != NULL)
  4394. {
  4395. char *strTariff = strstr(ptrToken, TariffCode);
  4396. if(strTariff != NULL)
  4397. {
  4398. char *strFee = strstr(strTariff, "$");
  4399. if(strFee != NULL)
  4400. {
  4401. strFee++;
  4402. float fFee = atof(strFee);
  4403. fee = (int)(fFee * 100);
  4404. break;
  4405. }
  4406. }
  4407. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4408. }
  4409. return fee;
  4410. }
  4411. void DefaultPriceHandler(char *StrDefaultPrice)
  4412. {
  4413. BOOL trigger = FALSE;
  4414. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4415. PRINTF_FUNC("********** Default Price: %s", StrDefaultPrice);
  4416. ConnectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4417. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4418. CurrentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4419. PRINTF_FUNC("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4420. OccupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4421. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4422. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice = CurrentRate;
  4423. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4424. {
  4425. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4426. {
  4427. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest == 0)
  4428. {
  4429. trigger = TRUE;
  4430. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest = 1;
  4431. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest = 1;
  4432. }
  4433. }
  4434. }
  4435. if(trigger)
  4436. {
  4437. PRINTF_FUNC("********** Trigger Dispenser Default Price **********");
  4438. }
  4439. }
  4440. void UserPriceHandler(char *UserId, char *UserPrice)
  4441. {
  4442. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4443. int AccountBalance = 0;
  4444. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4445. CurrentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4446. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4447. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4448. PRINTF_FUNC("********** User Id: %s **********", ShmOCPP16Data->Cost.SetUserPrice.idToken);
  4449. PRINTF_FUNC("********** User Price: %s", ShmOCPP16Data->Cost.SetUserPrice.price);
  4450. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4451. PRINTF_FUNC("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4452. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4453. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4454. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4455. {
  4456. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  4457. {
  4458. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  4459. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4460. {
  4461. PRINTF_FUNC("********** Connector Id %d User Price %d.%02d, Account Balance %d.%02d **********",
  4462. (i + 1), (CurrentRate / 100), (CurrentRate % 100), (AccountBalance / 100), (AccountBalance % 100));
  4463. }
  4464. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  4465. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4466. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  4467. {
  4468. AnnounceAccountBalance(i, AccountBalance);
  4469. }
  4470. }
  4471. }
  4472. }
  4473. void RunningFinalCostHandler(void)
  4474. {
  4475. unsigned int ConnectionFee = 0, SessionFee = 0, OccupancyFee = 0, TotalCost = 0;
  4476. int AccountBalance = 0;
  4477. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4478. {
  4479. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  4480. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_ALARM)
  4481. {
  4482. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[i].description) > 0)
  4483. {
  4484. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Connection Fee");
  4485. SessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Session Fee");
  4486. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Occupancy Fee");
  4487. TotalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Total Cost");
  4488. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Account Balance");
  4489. PRINTF_FUNC("********** Connector Id: %d Running Cost **********", (i + 1));
  4490. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4491. PRINTF_FUNC("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4492. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4493. PRINTF_FUNC("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4494. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4495. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4496. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4497. {
  4498. PRINTF_FUNC("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4499. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4500. }
  4501. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4502. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4503. memset(&ShmOCPP16Data->Cost.RunningCost[i], 0x00, sizeof(struct StrcutRunningFinalCost));
  4504. }
  4505. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[i].description) > 0)
  4506. {
  4507. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Connection Fee");
  4508. SessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Session Fee");
  4509. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Occupancy Fee");
  4510. TotalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Total Cost");
  4511. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Account Balance");
  4512. PRINTF_FUNC("********** Connector Id: %d Final Cost **********", (i + 1));
  4513. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4514. PRINTF_FUNC("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4515. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4516. PRINTF_FUNC("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4517. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4518. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4519. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4520. {
  4521. PRINTF_FUNC("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4522. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4523. }
  4524. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4525. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4526. memset(&ShmOCPP16Data->Cost.FinalCost[i], 0x00, sizeof(struct StrcutRunningFinalCost));
  4527. }
  4528. }
  4529. }
  4530. }
  4531. struct timeval _Cost_Price_time;
  4532. char MyDefaultPriceString[128];
  4533. struct StrcutSetUserPrice MyUserPrice;
  4534. char MyConnectorRunningCostString[CONNECTOR_QUANTITY][128];
  4535. char MyConnectorFinalCostString[CONNECTOR_QUANTITY][128];
  4536. void CheckOcppCostAndPrice(void)
  4537. {
  4538. // 5s for reboot request timeout
  4539. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  4540. {
  4541. gettimeofday(&_Cost_Price_time, NULL);
  4542. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  4543. {
  4544. DefaultPriceHandler((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  4545. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  4546. }
  4547. if(strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 && strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  4548. {
  4549. UserPriceHandler((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  4550. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  4551. }
  4552. RunningFinalCostHandler();
  4553. }
  4554. }
  4555. //===============================================
  4556. // OCPP
  4557. //===============================================
  4558. void CheckOcppStatus()
  4559. {
  4560. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  4561. {
  4562. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  4563. PRINTF_FUNC("*********************OCPP Boot Notification****************************\n");
  4564. }
  4565. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  4566. {
  4567. bool canReset = true;
  4568. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  4569. {
  4570. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4571. {
  4572. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  4573. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  4574. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  4575. {
  4576. canReset = false;
  4577. if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  4578. {
  4579. ChargingTerminalProcess(_index);
  4580. }
  4581. }
  4582. }
  4583. }
  4584. if(canReset)
  4585. {
  4586. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  4587. {
  4588. //trigger dispenser to hardware reboot
  4589. TriggerDispenserHardwareReboot();
  4590. if(IsDispenserHardwareRebootResponsed())
  4591. {
  4592. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4593. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4594. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  4595. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4596. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  4597. {
  4598. setChargerMode(i, MODE_MAINTAIN);
  4599. }
  4600. sleep(3);
  4601. system("reboot -f");
  4602. }
  4603. }
  4604. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  4605. {
  4606. //trigger dispenser to software reset
  4607. TriggerDispenserSoftwareReset();
  4608. if(IsDispenserSoftwareResetResponsed())
  4609. {
  4610. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4611. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4612. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  4613. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4614. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  4615. {
  4616. setChargerMode(i, MODE_MAINTAIN);
  4617. }
  4618. sleep(3);
  4619. system("killall OcppBackend &");
  4620. KillAllTask();
  4621. system("/usr/bin/run_evse_restart.sh");
  4622. }
  4623. }
  4624. }
  4625. }
  4626. CheckOcppCostAndPrice();
  4627. }
  4628. void OcppStartTransation(byte gunIndex)
  4629. {
  4630. byte _OcppGunIndex = gunIndex;
  4631. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4632. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  4633. PRINTF_FUNC("Gun index %d, OCPP Start Transation Index %d, IdTag = %s, StartSoc = %d",
  4634. chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, chargingInfo[gunIndex]->EvBatteryStartSoc);
  4635. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  4636. }
  4637. void OcppStopTransation(byte gunIndex)
  4638. {
  4639. byte _OcppGunIndex = gunIndex;
  4640. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4641. PRINTF_FUNC("Gun index %d, OCPP Stop Transation Index %d, IdTag = %s, StopSoc = %d",
  4642. chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, chargingInfo[gunIndex]->EvBatterySoc);
  4643. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  4644. }
  4645. // no use
  4646. bool OcppRemoteStop(byte gunIndex)
  4647. {
  4648. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4649. // 有 AC 槍的話
  4650. if (acDirIndex > 0 && gunIndex > 0)
  4651. {
  4652. gunIndex += acDirIndex;
  4653. }
  4654. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  4655. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4656. {
  4657. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4658. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4659. }
  4660. return result;
  4661. }
  4662. void OcppRemoteStartChk()
  4663. {
  4664. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  4665. {}
  4666. else if(!isDetectPlugin())
  4667. {
  4668. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  4669. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4670. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  4671. {
  4672. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES)
  4673. {
  4674. if (ac_chargingInfo[ac_index]->SystemStatus == S_IDLE ||
  4675. ac_chargingInfo[ac_index]->SystemStatus == S_RESERVATION)
  4676. {
  4677. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4678. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  4679. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4680. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  4681. ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  4682. DetectPluginStart();
  4683. return;
  4684. }
  4685. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4686. }
  4687. }
  4688. byte threeGunIndex = 0;
  4689. byte dcIndex = 0;
  4690. bool isGunUsingStatus = false;
  4691. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4692. {
  4693. // 如果有 AC 槍,且 DC 槍也有兩把
  4694. if (acDirIndex == 1 && _index == 1)
  4695. threeGunIndex = 1;
  4696. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES)
  4697. dcIndex = _index;
  4698. if (chargingInfo[_index]->SystemStatus != S_IDLE)
  4699. {
  4700. isGunUsingStatus = true;
  4701. }
  4702. }
  4703. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  4704. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  4705. {
  4706. if (dcIndex == 0)
  4707. threeGunIndex = 0;
  4708. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4709. return;
  4710. }
  4711. if (dcIndex == 0)
  4712. threeGunIndex = 0;
  4713. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES)
  4714. {
  4715. if (chargingInfo[dcIndex]->SystemStatus == S_IDLE ||
  4716. chargingInfo[dcIndex]->SystemStatus == S_RESERVATION)
  4717. {
  4718. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  4719. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4720. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  4721. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4722. DetectPluginStart();
  4723. }
  4724. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4725. }
  4726. }
  4727. }
  4728. void ChkOcppStatus(byte gunIndex)
  4729. {
  4730. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  4731. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  4732. {
  4733. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  4734. if (isReservationExpired(gunIndex))
  4735. {
  4736. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4737. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4738. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  4739. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  4740. }
  4741. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  4742. }
  4743. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  4744. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  4745. {
  4746. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  4747. if (isReservationExpired(gunIndex))
  4748. {
  4749. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4750. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4751. chargingInfo[gunIndex]->ReservationId = 0;
  4752. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  4753. }
  4754. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  4755. }
  4756. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  4757. {
  4758. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4759. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4760. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  4761. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  4762. {
  4763. if (isDb_ready)
  4764. DB_Update_Operactive(localDb, gunIndex, true);
  4765. chargingInfo[gunIndex]->IsAvailable = YES;
  4766. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  4767. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4768. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4769. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4770. {
  4771. setChargerMode(gunIndex, MODE_IDLE);
  4772. }
  4773. }
  4774. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  4775. {
  4776. if (isDb_ready)
  4777. DB_Update_Operactive(localDb, gunIndex, false);
  4778. chargingInfo[gunIndex]->IsAvailable = NO;
  4779. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  4780. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4781. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4782. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4783. {
  4784. setChargerMode(gunIndex, MODE_MAINTAIN);
  4785. }
  4786. }
  4787. }
  4788. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES)
  4789. {
  4790. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  4791. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  4792. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  4793. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  4794. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  4795. {
  4796. // 充電中,需停止充電
  4797. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  4798. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.UnlockStopRequest = true;
  4799. PRINTF_FUNC("********** OcppUnlockStop Gun %d **********\n", gunIndex);
  4800. }
  4801. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  4802. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  4803. }
  4804. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4805. {
  4806. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4807. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  4808. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  4809. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  4810. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  4811. {
  4812. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4813. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.RemoteStopRequest = true;
  4814. PRINTF_FUNC("********** OcppRemoteStop Gun %d **********\n", gunIndex);
  4815. }
  4816. }
  4817. }
  4818. bool CheckBackendChargingTimeout(byte gunIndex)
  4819. {
  4820. bool result = false;
  4821. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4822. {
  4823. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  4824. {
  4825. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  4826. result = true;
  4827. }
  4828. }
  4829. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4830. {
  4831. // 隨插即充電的要看 offline
  4832. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  4833. {
  4834. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  4835. result = true;
  4836. }
  4837. }
  4838. return result;
  4839. }
  4840. bool CheckBackendChargingEnergy(byte gunIndex)
  4841. {
  4842. bool result = false;
  4843. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4844. {
  4845. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  4846. {
  4847. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  4848. result = true;
  4849. }
  4850. }
  4851. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4852. {
  4853. // 隨插即充電的要看 offline
  4854. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  4855. {
  4856. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  4857. result = true;
  4858. }
  4859. }
  4860. return result;
  4861. }
  4862. void InformOcppErrOccur(byte codeType)
  4863. {
  4864. char _error[25];
  4865. switch(codeType)
  4866. {
  4867. case 4: strcpy(_error, "InternalError"); break;
  4868. case 6: strcpy(_error, "NoError"); break;
  4869. case 7: strcpy(_error, "OtherError"); break;
  4870. case 13: strcpy(_error, "UnderVoltage"); break;
  4871. case 14: strcpy(_error, "OverVoltage"); break;
  4872. }
  4873. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4874. {
  4875. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, _error);
  4876. }
  4877. }
  4878. //===============================================
  4879. // SQLite3 related routine
  4880. //===============================================
  4881. int DB_Open(sqlite3 *db)
  4882. {
  4883. int result = PASS;
  4884. char* errMsg = NULL;
  4885. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  4886. "idx integer primary key AUTOINCREMENT, "
  4887. "reservationId text, "
  4888. "transactionId text, "
  4889. "startMethod text, "
  4890. "userId text, "
  4891. "dateTimeStart text, "
  4892. "dateTimeStop text,"
  4893. "socStart text, "
  4894. "socStop text, "
  4895. "chargeEnergy text, "
  4896. "stopReason text"
  4897. ");";
  4898. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  4899. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4900. "`IsAvailable` TEXT NOT NULL, "
  4901. "`connector` INTEGER NOT NULL, "
  4902. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  4903. if(sqlite3_open(DB_FILE, &db))
  4904. {
  4905. result = FAIL;
  4906. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4907. sqlite3_close(db);
  4908. }
  4909. else
  4910. {
  4911. PRINTF_FUNC( "Local charging record database open successfully.");
  4912. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  4913. {
  4914. result = FAIL;
  4915. PRINTF_FUNC( "Create local charging record table error message: %s", errMsg);
  4916. }
  4917. else
  4918. {
  4919. PRINTF_FUNC( "Opened local charging record table successfully");
  4920. }
  4921. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  4922. {
  4923. result = FAIL;
  4924. PRINTF_FUNC( "Create local config table error message: %s", errMsg);
  4925. }
  4926. else
  4927. {
  4928. PRINTF_FUNC( "Opened local config table successfully");
  4929. }
  4930. sqlite3_close(db);
  4931. }
  4932. return result;
  4933. }
  4934. int DB_Insert_Record(sqlite3 *db, int gun_index)
  4935. {
  4936. int result = PASS;
  4937. char* errMsg = NULL;
  4938. char insertSql[1024];
  4939. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  4940. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  4941. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  4942. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  4943. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  4944. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  4945. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  4946. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  4947. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4948. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4949. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  4950. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4951. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  4952. {
  4953. result = FAIL;
  4954. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4955. sqlite3_close(db);
  4956. }
  4957. else
  4958. {
  4959. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4960. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  4961. {
  4962. result = FAIL;
  4963. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  4964. }
  4965. else
  4966. {
  4967. PRINTF_FUNC( "Insert local charging record successfully\n");
  4968. }
  4969. sqlite3_close(db);
  4970. }
  4971. return result;
  4972. }
  4973. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  4974. {
  4975. uint8_t result = false;
  4976. char* errMsg = NULL;
  4977. char sqlStr[1024];
  4978. srand(time(NULL));
  4979. if(sqlite3_open(DB_FILE, &db))
  4980. {
  4981. result = FAIL;
  4982. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4983. sqlite3_close(db);
  4984. }
  4985. else
  4986. {
  4987. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  4988. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  4989. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  4990. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  4991. {
  4992. result = FAIL;
  4993. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  4994. }
  4995. else
  4996. {
  4997. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  4998. }
  4999. sqlite3_close(db);
  5000. }
  5001. return result;
  5002. }
  5003. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  5004. {
  5005. uint8_t result = true;
  5006. char* errMsg = NULL;
  5007. char sqlStr[1024];
  5008. char **rs;
  5009. int rows, cols;
  5010. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  5011. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  5012. if(sqlite3_open(DB_FILE, &db))
  5013. {
  5014. result = FAIL;
  5015. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  5016. sqlite3_close(db);
  5017. }
  5018. else
  5019. {
  5020. PRINTF_FUNC( "Local config query database open successfully.");
  5021. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  5022. if(rows>0)
  5023. {
  5024. for(int idxRow=1;idxRow<=rows;idxRow++)
  5025. {
  5026. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  5027. {
  5028. result = false;
  5029. }
  5030. PRINTF_FUNC("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  5031. }
  5032. }
  5033. else
  5034. {
  5035. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.", gun_index);
  5036. }
  5037. sqlite3_free_table(rs);
  5038. sqlite3_close(db);
  5039. }
  5040. return result;
  5041. }
  5042. //===============================================
  5043. // Config process
  5044. //===============================================
  5045. void AddPlugInTimes(byte gunIndex)
  5046. {
  5047. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  5048. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  5049. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  5050. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  5051. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  5052. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  5053. }
  5054. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  5055. {
  5056. char cmdBuf[32];
  5057. struct timeb csuTime;
  5058. struct tm *tmCSU;
  5059. ftime(&csuTime);
  5060. tmCSU = localtime(&csuTime.time);
  5061. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  5062. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  5063. tmCSU->tm_sec);
  5064. if (isStart)
  5065. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  5066. else
  5067. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  5068. }
  5069. void zipLogFiles()
  5070. {
  5071. const char* logPath = "/Storage/SystemLog";
  5072. // 獲取目錄
  5073. DIR* pDir = opendir(logPath);
  5074. if (pDir != NULL)
  5075. {
  5076. struct timeb csuTime;
  5077. struct tm *tmCSU;
  5078. ftime(&csuTime);
  5079. tmCSU = localtime(&csuTime.time);
  5080. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  5081. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  5082. // tmCSU->tm_sec);
  5083. // Read items inside the folder
  5084. struct dirent* pEntry = NULL;
  5085. while ((pEntry = readdir(pDir)) != NULL)
  5086. {
  5087. if (strcmp(pEntry->d_name, ".") != 0 &&
  5088. strcmp(pEntry->d_name, "..") != 0 &&
  5089. strncmp(pEntry->d_name, "[", 1) == 0 &&
  5090. strstr(pEntry->d_name, "tar") < 0)
  5091. {
  5092. char yearC[5];
  5093. unsigned short year = 0;
  5094. char monthC[3];
  5095. unsigned short month = 0;
  5096. yearC[4] = '\0';
  5097. strncpy(yearC, pEntry->d_name + 1, 4);
  5098. monthC[2] = '\0';
  5099. strncpy(monthC, pEntry->d_name + 6, 2);
  5100. year = atoi(yearC);
  5101. month = atoi(monthC);
  5102. if (year != 0)
  5103. {
  5104. if (year < tmCSU->tm_year + 1900 ||
  5105. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  5106. {
  5107. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  5108. char file[256];
  5109. memset(file, 0x00, sizeof(file));
  5110. strcat(file, "tar zcvf ");
  5111. strcat(file, logPath);
  5112. strncat(file, "/", 1);
  5113. strcat(file, pEntry->d_name);
  5114. strcat(file, ".tar");
  5115. strncat(file, " ", 1);
  5116. strcat(file, logPath);
  5117. strncat(file, "/", 1);
  5118. strcat(file, pEntry->d_name);
  5119. PRINTF_FUNC("zip = %s \n", file);
  5120. system(file);
  5121. }
  5122. }
  5123. }
  5124. }
  5125. }
  5126. // Close folder
  5127. closedir(pDir);
  5128. }
  5129. void ChangeGunSelectByIndex(byte sel)
  5130. {
  5131. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  5132. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  5133. }
  5134. void StopProcessingLoop()
  5135. {
  5136. for (;;)
  5137. {
  5138. CheckFactoryConfigFunction();
  5139. CheckFwUpdateFunction();
  5140. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5141. {
  5142. ChkPrimaryStatus();
  5143. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  5144. {
  5145. PRINTF_FUNC("Soft reboot for retry self-tets (Primary). \n");
  5146. KillAllTask();
  5147. sleep(3);
  5148. system("/usr/bin/run_evse_restart.sh");
  5149. return;
  5150. }
  5151. }
  5152. if(ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  5153. {
  5154. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5155. {
  5156. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  5157. }
  5158. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  5159. {
  5160. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  5161. }
  5162. }
  5163. CheckSoftHardReset();
  5164. sleep(1);
  5165. }
  5166. }
  5167. void CreateWatchdog()
  5168. {
  5169. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  5170. {
  5171. wtdFd = InitWatchDog();
  5172. if (wtdFd < 0)
  5173. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  5174. }
  5175. }
  5176. bool IsConnectorWholeIdle()
  5177. {
  5178. bool result = true;
  5179. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  5180. {
  5181. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  5182. chargingInfo[count]->SystemStatus != S_RESERVATION &&
  5183. chargingInfo[count]->SystemStatus != S_FAULT &&
  5184. chargingInfo[count]->SystemStatus != S_MAINTAIN)
  5185. {
  5186. result = false;
  5187. break;
  5188. }
  5189. }
  5190. return result;
  5191. }
  5192. void ClearAlarmCodeWhenAcOff()
  5193. {
  5194. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  5195. {
  5196. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  5197. }
  5198. }
  5199. //==========================================
  5200. // Check task processing
  5201. //==========================================
  5202. void CheckTask()
  5203. {
  5204. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  5205. {
  5206. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  5207. {
  5208. if(system("pidof -s Module_4g > /dev/null") != 0)
  5209. {
  5210. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  5211. system("/root/Module_4g &");
  5212. }
  5213. }
  5214. }
  5215. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  5216. {
  5217. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  5218. {
  5219. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  5220. {
  5221. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  5222. system("/root/Module_Wifi &");
  5223. }
  5224. }
  5225. }
  5226. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5227. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5228. {
  5229. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  5230. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  5231. {
  5232. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  5233. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5234. system("pkill OcppBackend");
  5235. }
  5236. if(system("pidof -s OcppBackend > /dev/null") != 0)
  5237. {
  5238. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  5239. system("/root/OcppBackend &");
  5240. }
  5241. }
  5242. else
  5243. {
  5244. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  5245. }
  5246. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  5247. {
  5248. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  5249. system ("/root/Module_ProduceUtils &");
  5250. }
  5251. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  5252. {
  5253. DEBUG_ERROR_MSG("Module_EvComm not running, restart it.\r\n");
  5254. system ("/root/Module_EvComm &");
  5255. }
  5256. }
  5257. void InitialDHCP()
  5258. {
  5259. char tmpbuf[256];
  5260. memset(tmpbuf,0,256);
  5261. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  5262. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  5263. system(tmpbuf);
  5264. }
  5265. void InitialDispenserDhcpServerConfig(void)
  5266. {
  5267. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  5268. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  5269. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  5270. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  5271. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  5272. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  5273. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  5274. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  5275. usleep(1000);
  5276. }
  5277. void StartDispenserDhcpServer(void)
  5278. {
  5279. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  5280. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  5281. }
  5282. //==========================================
  5283. // Check Smart Charging Profile
  5284. //==========================================
  5285. int GetStartScheduleTime(unsigned char *time)
  5286. {
  5287. int result = -1;
  5288. struct tm tmScheduleStart;
  5289. struct timeb tbScheduleStart;
  5290. 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))
  5291. {
  5292. tmScheduleStart.tm_year -= 1900;
  5293. tmScheduleStart.tm_mon -= 1;
  5294. tbScheduleStart.time = mktime(&tmScheduleStart);
  5295. tbScheduleStart.millitm = 0;
  5296. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  5297. }
  5298. return result;
  5299. }
  5300. void CheckSmartChargeProfile(byte _index)
  5301. {
  5302. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO)
  5303. {
  5304. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO)
  5305. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  5306. }
  5307. else
  5308. {
  5309. // Get Charging Profile
  5310. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  5311. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL)
  5312. {
  5313. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  5314. byte _startCount = NO_DEFINE;
  5315. byte _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  5316. for (byte _count = 0; _count < _maxCount; _count++)
  5317. {
  5318. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5319. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod &&
  5320. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  5321. {
  5322. _startCount = _count;
  5323. }
  5324. }
  5325. PRINTF_FUNC("_startCount = %d \n", _startCount);
  5326. if (_startCount < _maxCount)
  5327. {
  5328. PRINTF_FUNC("*********Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  5329. chargingInfo[_index]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * 10;
  5330. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * chargingInfo[_index]->EvBatterytargetVoltage / 100;
  5331. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  5332. // if ((chargingInfo[_index]->EvBatterytargetVoltage * 10) > 0)
  5333. // {
  5334. // chargingInfo[_index]->ChargingProfileCurrent = chargingInfo[_index]->ChargingProfilePower / (chargingInfo[_index]->EvBatterytargetVoltage * 10);
  5335. // }
  5336. }
  5337. else
  5338. {
  5339. chargingInfo[_index]->ChargingProfilePower = -1;
  5340. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5341. }
  5342. PRINTF_FUNC("ChargingProfilePower = %f \n", chargingInfo[_index]->ChargingProfilePower);
  5343. PRINTF_FUNC("ChargingProfileCurrent = %f \n", chargingInfo[_index]->ChargingProfileCurrent);
  5344. }
  5345. //
  5346. // printf("-------------Schedule------------\n");
  5347. // printf("index = %d \n", _index);
  5348. // printf("StartSchedule = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  5349. // printf("ChargingRateUnit = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingRateUnit);
  5350. // printf("----------SchedulePeriod---------\n");
  5351. // for (int v = 0; v < 10; v++)
  5352. // {
  5353. // printf("StartPeriod = %d \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].StartPeriod);
  5354. // printf("Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].Limit);
  5355. // }
  5356. // printf("---------------------------------\n");
  5357. }
  5358. }
  5359. void TheEndCharging(byte gun_index)
  5360. {
  5361. ftime(&endChargingTime[gun_index]);
  5362. if (chargingInfo[gun_index]->PresentChargedDuration != 0)
  5363. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5364. chargingInfo[gun_index]->isRemoteStart = NO;
  5365. StopGunInfoTimeoutDet(gun_index);
  5366. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  5367. ChangeStartOrStopDateTime(NO, gun_index);
  5368. //DB_Insert_Record(localDb, gun_index);
  5369. }
  5370. void UpdateErrorCodeToOcpp(byte index)
  5371. {
  5372. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  5373. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  5374. {
  5375. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  5376. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)chargingInfo[index]->ConnectorAlarmCode);
  5377. }
  5378. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  5379. {
  5380. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  5381. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)chargingInfo[index]->EvConnAlarmCode);
  5382. }
  5383. }
  5384. void CheckMiscCommandRequirement(void)
  5385. {
  5386. BOOL NeedAnnouncement = FALSE;
  5387. unsigned char connector = 0;
  5388. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  5389. {
  5390. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5391. {
  5392. NeedAnnouncement = FALSE;
  5393. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  5394. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  5395. {
  5396. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest ||
  5397. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest ||
  5398. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest ||
  5399. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest ||
  5400. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest ||
  5401. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.BackendStatusRequest ||
  5402. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.EthernetStatusRequest ||
  5403. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.WiFiStatusRequest ||
  5404. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.TelcomModemStatusRequest ||
  5405. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.BillingStatusRequest ||
  5406. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.StopButtonStatusRequest)
  5407. {
  5408. NeedAnnouncement = TRUE;
  5409. }
  5410. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  5411. {
  5412. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  5413. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.AvailabilityRequest ||
  5414. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest ||
  5415. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.AccountBalanceRequest)
  5416. {
  5417. NeedAnnouncement = TRUE;
  5418. }
  5419. }
  5420. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  5421. {
  5422. if(NeedAnnouncement)
  5423. {
  5424. PRINTF_FUNC("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  5425. }
  5426. else
  5427. {
  5428. PRINTF_FUNC("********** Dispenser %d Misc Command Announced **********", i + 1);
  5429. }
  5430. }
  5431. }
  5432. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  5433. }
  5434. }
  5435. }
  5436. void CheckDispenserVersionUpdateRequirement(void)
  5437. {
  5438. }
  5439. // connector: connector infex, 0 ~ 3
  5440. void SetLedIndicationStatus(unsigned char connector, unsigned char indication)
  5441. {
  5442. int greenOffset = 0, redOffset = 0;
  5443. if(ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication)
  5444. {
  5445. greenOffset = 2;
  5446. redOffset = 3;
  5447. }
  5448. if(ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication)
  5449. {
  5450. if(connector == 0)
  5451. {
  5452. greenOffset = 4;
  5453. redOffset = 5;
  5454. }
  5455. else if(connector == 1)
  5456. {
  5457. greenOffset = 2;
  5458. redOffset = 3;
  5459. }
  5460. else
  5461. {
  5462. return;
  5463. }
  5464. }
  5465. switch(indication)
  5466. {
  5467. case _LED_INDICATION_OFF:
  5468. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << greenOffset); // green off
  5469. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << redOffset); // red off
  5470. break;
  5471. case _LED_INDICATION_RUN:
  5472. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << greenOffset); // green on
  5473. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << redOffset); // red off
  5474. break;
  5475. case _LED_INDICATION_FAULT:
  5476. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << greenOffset); // green off
  5477. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << redOffset); // red on
  5478. break;
  5479. case _LED_INDICATION_ON:
  5480. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << greenOffset); // green on
  5481. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << redOffset); // red on
  5482. break;
  5483. case _LED_INDICATION_GREEN_TOGGLE:
  5484. if(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] & (0x01 << greenOffset))
  5485. {
  5486. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << greenOffset); // green off
  5487. }
  5488. else
  5489. {
  5490. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << greenOffset); // green on
  5491. }
  5492. break;
  5493. case _LED_INDICATION_RED_TOGGLE:
  5494. if(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] & (0x01 << redOffset))
  5495. {
  5496. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << redOffset); // red off
  5497. }
  5498. else
  5499. {
  5500. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << redOffset); // red on
  5501. }
  5502. break;
  5503. }
  5504. }
  5505. struct timeval _standardIndication_time;
  5506. unsigned char _standardLedStatus, _preStandardLedStatus;
  5507. unsigned char _e4youLedStatus[2], _preE4youLedStatus[2];
  5508. struct timeval _e4youIndication_time[2];
  5509. void LedIndicationProcess(void)
  5510. {
  5511. if(ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication)
  5512. {
  5513. if(ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5514. {
  5515. _standardLedStatus = _STANDARD_LED_Alarm;
  5516. if(_preStandardLedStatus != _standardLedStatus)
  5517. {
  5518. _preStandardLedStatus = _standardLedStatus;
  5519. }
  5520. SetLedIndicationStatus(0, _LED_INDICATION_FAULT);
  5521. }
  5522. else
  5523. {
  5524. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity == 0)
  5525. {
  5526. _standardLedStatus = _STANDARD_LED_Disconnection;
  5527. if(_preStandardLedStatus != _standardLedStatus)
  5528. {
  5529. _preStandardLedStatus = _standardLedStatus;
  5530. // reset led status & indication time
  5531. gettimeofday(&_standardIndication_time, NULL);
  5532. SetLedIndicationStatus(0, _LED_INDICATION_OFF);
  5533. }
  5534. if((GetTimeoutValue(_standardIndication_time) / mSEC_VAL) >= INDICATION_BLINK_INTERVAL)
  5535. {
  5536. SetLedIndicationStatus(0, _LED_INDICATION_GREEN_TOGGLE);
  5537. SetLedIndicationStatus(0, _LED_INDICATION_RED_TOGGLE);
  5538. gettimeofday(&_standardIndication_time, NULL);
  5539. }
  5540. }
  5541. else
  5542. {
  5543. _standardLedStatus = _STANDARD_LED_Running;
  5544. if(_preStandardLedStatus != _standardLedStatus)
  5545. {
  5546. _preStandardLedStatus = _standardLedStatus;
  5547. }
  5548. SetLedIndicationStatus(0, _LED_INDICATION_RUN);
  5549. }
  5550. }
  5551. }
  5552. if(ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication)
  5553. {
  5554. for(int i = 0; i < 2; i++)
  5555. {
  5556. if((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_FAULT ||
  5557. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_ALARM) &&
  5558. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable)
  5559. {
  5560. _e4youLedStatus[i] = _E4YOU_LED_Alarm;
  5561. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5562. {
  5563. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5564. }
  5565. SetLedIndicationStatus(i, _LED_INDICATION_FAULT);
  5566. }
  5567. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable == false)
  5568. {
  5569. _e4youLedStatus[i] = _E4YOU_LED_Disconnection;
  5570. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5571. {
  5572. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5573. // reset led status & indication time
  5574. gettimeofday(&_e4youIndication_time[i], NULL);
  5575. SetLedIndicationStatus(i, _LED_INDICATION_OFF);
  5576. }
  5577. if((GetTimeoutValue(_e4youIndication_time[i]) / mSEC_VAL) >= INDICATION_BLINK_INTERVAL)
  5578. {
  5579. gettimeofday(&_e4youIndication_time[i], NULL);
  5580. SetLedIndicationStatus(i, _LED_INDICATION_GREEN_TOGGLE);
  5581. SetLedIndicationStatus(i, _LED_INDICATION_RED_TOGGLE);
  5582. }
  5583. }
  5584. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_IDLE ||
  5585. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_MAINTAIN ||
  5586. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_RESERVATION)
  5587. {
  5588. _e4youLedStatus[i] = _E4YOU_LED_Idle;
  5589. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5590. {
  5591. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5592. }
  5593. SetLedIndicationStatus(i, _LED_INDICATION_RUN);
  5594. }
  5595. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  5596. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_COMPLETE)
  5597. {
  5598. _e4youLedStatus[i] = _E4YOU_LED_Charging;
  5599. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5600. {
  5601. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5602. // reset led status & indication time
  5603. gettimeofday(&_e4youIndication_time[i], NULL);
  5604. SetLedIndicationStatus(i, _LED_INDICATION_OFF);
  5605. }
  5606. if((GetTimeoutValue(_e4youIndication_time[i]) / mSEC_VAL) >= E4YOU_BLINK_INTERVAL)
  5607. {
  5608. gettimeofday(&_e4youIndication_time[i], NULL);
  5609. SetLedIndicationStatus(i, _LED_INDICATION_GREEN_TOGGLE);
  5610. }
  5611. }
  5612. }
  5613. }
  5614. }
  5615. void CreateLedIndicationFork(void)
  5616. {
  5617. pid_t pid;
  5618. pid = fork();
  5619. if(pid == 0)
  5620. {
  5621. while(1)
  5622. {
  5623. LedIndicationProcess();
  5624. usleep(100000);
  5625. }
  5626. }
  5627. }
  5628. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  5629. {
  5630. unsigned char dispenser = 0;
  5631. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5632. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  5633. {
  5634. PRINTF_FUNC("Connector %d Disconnection Recovery", connector + 1);
  5635. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  5636. }
  5637. }
  5638. bool ConnectorRequestToCharging(unsigned char connector)
  5639. {
  5640. unsigned char role = 0;
  5641. bool done = false;
  5642. role = ShmGroupCollection[connector].Role;
  5643. switch(role)
  5644. {
  5645. case _GROLE_IDLE:
  5646. if(!ShmGroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  5647. {
  5648. PRINTF_FUNC("Request Gun %d To Start Charging", connector + 1);
  5649. }
  5650. ShmGroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  5651. break;
  5652. case _GROLE_MASTER:
  5653. done = true;
  5654. break;
  5655. case _GROLE_SLAVE:
  5656. if(!ShmGroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  5657. {
  5658. PRINTF_FUNC("Gun %d Wait For Re-Grouping", connector + 1);
  5659. }
  5660. ShmGroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  5661. break;
  5662. case _GROLE_REQUEST_TO_CHARGING:
  5663. done = true;
  5664. break;
  5665. default:
  5666. break;
  5667. }
  5668. return done;
  5669. }
  5670. bool IsConnectorGroupingCompleted(unsigned char connector)
  5671. {
  5672. unsigned char role = 0;
  5673. bool completed = false;
  5674. role = ShmGroupCollection[connector].Role;
  5675. switch(role)
  5676. {
  5677. case _GROLE_MASTER:
  5678. completed = true;
  5679. break;
  5680. default:
  5681. break;
  5682. }
  5683. return completed;
  5684. }
  5685. int main(void)
  5686. {
  5687. int time = 0;
  5688. if(CreateShareMemory() == 0)
  5689. {
  5690. #ifdef SystemLogMessage
  5691. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  5692. #endif
  5693. if(ShmStatusCodeData!=NULL)
  5694. {
  5695. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  5696. }
  5697. sleep(5);
  5698. system("reboot -f");
  5699. sleep(5);
  5700. system("reboot -f");
  5701. }
  5702. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  5703. if (!InitialSystemDefaultConfig())
  5704. {
  5705. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  5706. StopProcessingLoop();
  5707. }
  5708. PRINTF_FUNC("ModelName: [%s], SN: [%s], SW Version: [%s]",
  5709. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion);
  5710. PRINTF_FUNC("InitialShareMemoryInfo");
  5711. InitialShareMemoryInfo();
  5712. PRINTF_FUNC("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  5713. ChangeLcmByIndex(_LCM_INIT);
  5714. if(!InitialChargerSetting())
  5715. {
  5716. isModelNameMatch = false;
  5717. }
  5718. Initialization();
  5719. PRINTF_FUNC("Spawn all Task.");
  5720. SpawnTask();
  5721. if (!isModelNameMatch)
  5722. {
  5723. PRINTF_FUNC("Module Name & HW info none match.");
  5724. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  5725. ChangeLcmByIndex(_LCM_FIX);
  5726. // Module Name 與硬體對應不正確
  5727. DEBUG_ERROR_MSG("Module Name & HW info none match.");
  5728. sleep(3);
  5729. KillAllTask();
  5730. StopProcessingLoop();
  5731. }
  5732. PRINTF_FUNC("Module Name & HW info correct. Initialize.......");
  5733. CreateTimeoutFork();
  5734. PRINTF_FUNC("Self test.");
  5735. if(ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication)
  5736. {
  5737. _standardLedStatus = _STANDARD_LED_None;
  5738. _preStandardLedStatus = _STANDARD_LED_None;
  5739. SetLedIndicationStatus(0, _LED_INDICATION_ON);
  5740. }
  5741. if(ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication)
  5742. {
  5743. _e4youLedStatus[0] = _E4YOU_LED_None;
  5744. _e4youLedStatus[1] = _E4YOU_LED_None;
  5745. _preE4youLedStatus[0] = _E4YOU_LED_None;
  5746. _preE4youLedStatus[1] = _E4YOU_LED_None;
  5747. SetLedIndicationStatus(0, _LED_INDICATION_ON);
  5748. SetLedIndicationStatus(1, _LED_INDICATION_ON);
  5749. }
  5750. SelfTestRun();
  5751. // StopSystemTimeoutDet();
  5752. SetAcContactor(ON);
  5753. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  5754. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  5755. ShmPsuData->Work_Step == _NO_WORKING)
  5756. {
  5757. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5758. {
  5759. setChargerMode(gun_index, MODE_FAULT);
  5760. }
  5761. ChangeLcmByIndex(_LCM_FIX);
  5762. sleep(3);
  5763. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5764. {
  5765. KillTaskExceptPrimary();
  5766. }
  5767. else
  5768. {
  5769. KillAllTask();
  5770. }
  5771. StopProcessingLoop();
  5772. }
  5773. else
  5774. {
  5775. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5776. {
  5777. setChargerMode(gun_index, MODE_IDLE);
  5778. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0)
  5779. {
  5780. _PsuGroupAvailable[gun_index] = YES;
  5781. }
  5782. }
  5783. }
  5784. // Local DB
  5785. if(DB_Open(localDb) != PASS)
  5786. {
  5787. PRINTF_FUNC("DB_Open fail.");
  5788. isDb_ready = false;
  5789. }
  5790. else
  5791. {
  5792. isDb_ready = true;
  5793. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  5794. {
  5795. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  5796. }
  5797. }
  5798. ChangeLcmByIndex(_LCM_IDLE);
  5799. sleep(1);
  5800. CreateLedIndicationFork();
  5801. // Main loop
  5802. PRINTF_FUNC("****************************Main Loop********************************** \n");
  5803. gettimeofday(&_cmdMainPriority_time, NULL);
  5804. for (;;)
  5805. {
  5806. CheckOcppStatus();
  5807. ChkPrimaryStatus();
  5808. ChkPsuStatus();
  5809. PsuFailureResume();
  5810. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  5811. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  5812. {
  5813. CheckFactoryConfigFunction();
  5814. CheckFwUpdateFunction();
  5815. }
  5816. if(CheckWiringInfoUpdate() != YES)
  5817. {
  5818. // return fork process
  5819. return 0;
  5820. }
  5821. CheckSoftHardReset();
  5822. StandbyCheck();
  5823. // OCPP 邏輯
  5824. //OcppRemoteStartChk();
  5825. // 讀卡邏輯
  5826. //ScannerCardProcess();
  5827. PowerCabinetAuthorizeProcess();
  5828. // 當 AC 沒有搭上時,清除一些錯誤碼
  5829. ClearAlarmCodeWhenAcOff();
  5830. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000)
  5831. {
  5832. CheckTask();
  5833. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5834. {
  5835. if (chargingInfo[_index]->SystemStatus == S_CHARGING)
  5836. CheckSmartChargeProfile(_index);
  5837. }
  5838. gettimeofday(&_cmdMainPriority_time, NULL);
  5839. }
  5840. //LedIndicationProcess();
  5841. CheckMiscCommandRequirement();
  5842. CheckDispenserVersionUpdateRequirement();
  5843. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5844. {
  5845. //CheckGpioInStatus();
  5846. CheckErrorOccurStatus(gun_index);
  5847. ChkOcppStatus(gun_index);
  5848. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  5849. switch(chargingInfo[gun_index]->SystemStatus)
  5850. {
  5851. case S_MAINTAIN:
  5852. if(isModeChange(gun_index))
  5853. {
  5854. PRINTF_FUNC("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  5855. }
  5856. if(chargingInfo[gun_index]->IsAvailable == YES &&
  5857. (_PsuGroupAvailable[gun_index] == YES || ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0))
  5858. {
  5859. setChargerMode(gun_index, MODE_IDLE);
  5860. }
  5861. break;
  5862. case S_IDLE:
  5863. case S_RESERVATION:
  5864. case S_FAULT:
  5865. {
  5866. // clean PermissionRequest flag
  5867. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  5868. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  5869. if(isModeChange(gun_index))
  5870. {
  5871. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  5872. {
  5873. PRINTF_FUNC("================== S_IDLE (%x) ================== \n", gun_index + 1);
  5874. chargingInfo[gun_index]->PresentChargedDuration = 0;
  5875. chargingInfo[gun_index]->RemainChargingDuration = 0;
  5876. chargingTime[gun_index] = 0;
  5877. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  5878. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  5879. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5880. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  5881. ReleaseAlarmCode(gun_index);
  5882. // clean connector infomation
  5883. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  5884. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  5885. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  5886. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  5887. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  5888. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  5889. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -6553500;
  5890. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  5891. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  5892. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  5893. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  5894. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  5895. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  5896. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  5897. chargingInfo[gun_index]->EvBatterySoc = 0;
  5898. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  5899. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  5900. // clean RemoteStart & RemoteStop flag
  5901. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartRequest = false;
  5902. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  5903. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopRequest = false;
  5904. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  5905. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopRequest = false;
  5906. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  5907. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  5908. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  5909. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  5910. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  5911. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningFinalCost));
  5912. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutRunningFinalCost));
  5913. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  5914. chargingInfo[gun_index]->PresentChargingPower = 0;
  5915. // clean force charging info
  5916. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  5917. }
  5918. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  5919. {
  5920. PRINTF_FUNC("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  5921. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  5922. }
  5923. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  5924. {
  5925. PRINTF_FUNC("================== S_FAULT (%x) ================== \n", gun_index + 1);
  5926. }
  5927. }
  5928. if (chargingInfo[gun_index]->IsAvailable == NO ||
  5929. (_PsuGroupAvailable[gun_index] == NO && ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity == 0))
  5930. {
  5931. setChargerMode(gun_index, MODE_MAINTAIN);
  5932. continue;
  5933. }
  5934. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  5935. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  5936. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  5937. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  5938. {
  5939. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  5940. {
  5941. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  5942. {
  5943. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  5944. }
  5945. }
  5946. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  5947. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  5948. {
  5949. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  5950. {
  5951. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  5952. PRINTF_FUNC("Connector %d Disconnect Occur", gun_index + 1);
  5953. }
  5954. }
  5955. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  5956. {
  5957. ClearDetectPluginFlag();
  5958. UpdateErrorCodeToOcpp(gun_index);
  5959. setChargerMode(gun_index, MODE_FAULT);
  5960. }
  5961. else
  5962. {
  5963. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  5964. {
  5965. CheckConnectorDisconnectionRecovery(gun_index);
  5966. }
  5967. }
  5968. }
  5969. else
  5970. {
  5971. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  5972. {
  5973. ReleaseAlarmCode(gun_index);
  5974. setChargerMode(gun_index, MODE_IDLE);
  5975. }
  5976. // clean stop charge flag
  5977. chargingInfo[gun_index]->StopChargeFlag = false;
  5978. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  5979. { // Idle 正常程序起點
  5980. // 判斷是否有啟用檢查插槍
  5981. if(IsConnectorAuthorizeSuccess(gun_index))
  5982. {
  5983. if(chargingInfo[gun_index]->IsAvailable &&
  5984. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  5985. {
  5986. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RemoteStart)
  5987. {
  5988. PRINTF_FUNC("Connector %d remote start charging", gun_index + 1);
  5989. }
  5990. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RFID)
  5991. {
  5992. PRINTF_FUNC("Connector %d swipe rfid start charging", gun_index + 1);
  5993. }
  5994. setChargerMode(gun_index, MODE_AUTHORIZING);
  5995. continue;
  5996. }
  5997. }
  5998. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  5999. {
  6000. if(chargingInfo[gun_index]->IsAvailable &&
  6001. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  6002. chargingInfo[gun_index]->ConnectorPlugIn &&
  6003. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  6004. {
  6005. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex].UserId);
  6006. PRINTF_FUNC("Auto Gun Selection %d, CardNumber = %s", gun_index, chargingInfo[gun_index]->StartUserId);
  6007. DispenserAuthorizationInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  6008. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6009. continue;
  6010. }
  6011. }
  6012. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  6013. {
  6014. if (!isReservationExpired(gun_index))
  6015. {
  6016. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  6017. }
  6018. }
  6019. // only for test & debug purpose
  6020. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  6021. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  6022. {
  6023. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  6024. PRINTF_FUNC("Gun %d Start Force Charging", gun_index + 1);
  6025. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6026. continue;
  6027. }
  6028. else
  6029. {
  6030. }
  6031. } // Idle 正常程序終點
  6032. }
  6033. }
  6034. break;
  6035. case S_AUTHORIZING:
  6036. if(isModeChange(gun_index))
  6037. {
  6038. PRINTF_FUNC("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  6039. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  6040. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  6041. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  6042. StopSystemTimeoutDet();
  6043. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  6044. SetAcContactor(ON);
  6045. gettimeofday(&_ConnectorAuthorizing_Time[gun_index], NULL);
  6046. }
  6047. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  6048. {
  6049. FouceAnnounceAccountBalance(gun_index);
  6050. }
  6051. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RemoteStart)
  6052. {
  6053. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  6054. {
  6055. // sync with dispenser
  6056. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  6057. gettimeofday(&_ConnectorAuthorizing_Time[gun_index], NULL);
  6058. PRINTF_FUNC("Sync with Connector %d through remote start", gun_index + 1);
  6059. FouceAnnounceAccountBalance(gun_index);
  6060. }
  6061. }
  6062. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RFID)
  6063. {
  6064. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  6065. {
  6066. // sync with dispenser
  6067. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  6068. gettimeofday(&_ConnectorAuthorizing_Time[gun_index], NULL);
  6069. PRINTF_FUNC("Sync with Connector %d through swipe rfid", gun_index + 1);
  6070. }
  6071. }
  6072. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  6073. {
  6074. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  6075. {
  6076. PRINTF_FUNC("*********** Connector %d Plug In Timeout ***********", gun_index + 1);
  6077. }
  6078. else
  6079. {
  6080. PRINTF_FUNC("*********** Connector %d Preparing State Timeout ***********", gun_index + 1);
  6081. }
  6082. setChargerMode(gun_index, MODE_IDLE);
  6083. ConnectorAuthorizationInitial(gun_index);
  6084. }
  6085. else
  6086. {
  6087. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  6088. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  6089. {
  6090. AddPlugInTimes(gun_index);
  6091. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  6092. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6093. ConnectorAuthorizationInitial(gun_index);
  6094. }
  6095. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  6096. {
  6097. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  6098. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  6099. PRINTF_FUNC("*********** Connector id %d User Stop Plug In ***********\n", gun_index + 1);
  6100. setChargerMode(gun_index, MODE_IDLE);
  6101. ConnectorAuthorizationInitial(gun_index);
  6102. }
  6103. }
  6104. break;
  6105. case S_REASSIGN_CHECK:
  6106. {
  6107. if (isModeChange(gun_index))
  6108. {
  6109. PRINTF_FUNC("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  6110. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  6111. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  6112. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  6113. StopSystemTimeoutDet();
  6114. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  6115. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  6116. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  6117. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  6118. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  6119. PRINTF_FUNC("Max Total Current %d, Max Total Power %d, Total Energy %d, Total Duration %d",
  6120. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent,
  6121. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower,
  6122. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  6123. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  6124. SetAcContactor(ON);
  6125. gettimeofday(&_SystemStatus_Time[gun_index], NULL);
  6126. }
  6127. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  6128. if(ConnectorRequestToCharging(gun_index))
  6129. {
  6130. setChargerMode(gun_index, MODE_REASSIGN);
  6131. }
  6132. else if(time >= WAIT_REASSIGN_TIME)
  6133. {
  6134. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  6135. {
  6136. PRINTF_FUNC("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  6137. }
  6138. else
  6139. {
  6140. PRINTF_FUNC("Gun %d Request Charging Timeout", gun_index + 1);
  6141. }
  6142. setChargerMode(gun_index, MODE_IDLE);
  6143. }
  6144. }
  6145. break;
  6146. case S_REASSIGN:
  6147. {
  6148. if (isModeChange(gun_index))
  6149. {
  6150. PRINTF_FUNC("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  6151. gettimeofday(&_toAverage_time, NULL);
  6152. gettimeofday(&_SystemStatus_Time[gun_index], NULL);
  6153. }
  6154. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  6155. if(IsConnectorGroupingCompleted(gun_index))
  6156. {
  6157. setChargerMode(gun_index, MODE_PRECHARGE);
  6158. }
  6159. else if(time >= MAX_REQUEST_CHARGING_TIME)
  6160. {
  6161. PRINTF_FUNC("Gun %d Grouping Timeout", gun_index + 1);
  6162. setChargerMode(gun_index, MODE_IDLE);
  6163. }
  6164. }
  6165. break;
  6166. case S_PREPARNING:
  6167. {
  6168. if (isModeChange(gun_index))
  6169. {
  6170. PRINTF_FUNC("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  6171. StopGunInfoTimeoutDet(gun_index);
  6172. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  6173. SetAcContactor(ON);
  6174. gettimeofday(&_SystemStatus_Time[gun_index], NULL);
  6175. }
  6176. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  6177. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  6178. {
  6179. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  6180. continue;
  6181. }
  6182. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6183. {
  6184. // 板端或後臺要求停止
  6185. PRINTF_FUNC("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  6186. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6187. {
  6188. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6189. }
  6190. ChargingAlarmProcess(gun_index);
  6191. }
  6192. else if(isNormalStopChargeFlag(gun_index) == YES)
  6193. {
  6194. PRINTF_FUNC("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  6195. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  6196. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6197. {
  6198. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6199. }
  6200. ChargingTerminalProcess(gun_index);
  6201. }
  6202. /*
  6203. if(time >= GUN_PREPARE_TIMEOUT)
  6204. {
  6205. PRINTF_FUNC("********** Gun %d PrepareTimeout **********", gun_index + 1);
  6206. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  6207. ChargingTerminalProcess(gun_index);
  6208. }
  6209. */
  6210. }
  6211. break;
  6212. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  6213. {
  6214. if (isModeChange(gun_index))
  6215. {
  6216. PRINTF_FUNC("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  6217. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  6218. StopGunInfoTimeoutDet(gun_index);
  6219. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  6220. gettimeofday(&_SystemStatus_Time[gun_index], NULL);
  6221. }
  6222. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  6223. {
  6224. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  6225. }
  6226. // only for test & debug purpose
  6227. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  6228. {
  6229. PRINTF_FUNC("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  6230. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  6231. }
  6232. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6233. {
  6234. // 板端或後臺要求停止
  6235. PRINTF_FUNC("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  6236. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6237. {
  6238. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6239. }
  6240. ChargingAlarmProcess(gun_index);
  6241. }
  6242. else if(isNormalStopChargeFlag(gun_index) == YES)
  6243. {
  6244. PRINTF_FUNC("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  6245. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  6246. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6247. {
  6248. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6249. }
  6250. ChargingTerminalProcess(gun_index);
  6251. }
  6252. }
  6253. break;
  6254. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  6255. {
  6256. if (isModeChange(gun_index))
  6257. {
  6258. PRINTF_FUNC("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  6259. StopGunInfoTimeoutDet(gun_index);
  6260. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  6261. gettimeofday(&_SystemStatus_Time[gun_index], NULL);
  6262. }
  6263. // only for test & debug purpose
  6264. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  6265. {
  6266. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  6267. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  6268. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  6269. {
  6270. PRINTF_FUNC("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  6271. }
  6272. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  6273. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  6274. }
  6275. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  6276. {
  6277. setChargerMode(gun_index, MODE_CHARGING);
  6278. }
  6279. // only for test & debug purpose
  6280. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  6281. {
  6282. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  6283. if(time >= FORCE_CHARGING_WAIT_EVSE)
  6284. {
  6285. PRINTF_FUNC("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  6286. setChargerMode(gun_index, MODE_CHARGING);
  6287. }
  6288. }
  6289. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6290. {
  6291. // 板端或後臺要求停止
  6292. PRINTF_FUNC("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  6293. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6294. {
  6295. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6296. }
  6297. ChargingAlarmProcess(gun_index);
  6298. }
  6299. else if(isNormalStopChargeFlag(gun_index) == YES)
  6300. {
  6301. PRINTF_FUNC("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  6302. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  6303. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6304. {
  6305. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6306. }
  6307. ChargingTerminalProcess(gun_index);
  6308. }
  6309. }
  6310. break;
  6311. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  6312. {
  6313. if (isModeChange(gun_index))
  6314. {
  6315. PRINTF_FUNC("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  6316. StopGunInfoTimeoutDet(gun_index);
  6317. ftime(&startChargingTime[gun_index]);
  6318. ChangeStartOrStopDateTime(YES, gun_index);
  6319. OcppStartTransation(gun_index);
  6320. }
  6321. // only for test & debug purpose
  6322. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  6323. {
  6324. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  6325. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  6326. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  6327. {
  6328. PRINTF_FUNC("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  6329. }
  6330. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  6331. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  6332. }
  6333. ftime(&endChargingTime[gun_index]);
  6334. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  6335. // 計算 Power
  6336. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  6337. chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  6338. if (chargingTime[gun_index] == 0 || chargingTime[gun_index] > chargingInfo[gun_index]->PresentChargedDuration)
  6339. {
  6340. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  6341. }
  6342. else
  6343. {
  6344. int passTime = chargingInfo[gun_index]->PresentChargedDuration - chargingTime[gun_index];
  6345. if(passTime > 0)
  6346. {
  6347. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) * passTime / 3600;
  6348. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  6349. {
  6350. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  6351. }
  6352. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  6353. chargingInfo[gun_index]->PowerConsumption += changingPow;
  6354. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  6355. }
  6356. }
  6357. if (isEvBoardStopChargeFlag(gun_index))
  6358. {
  6359. PRINTF_FUNC("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  6360. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6361. {
  6362. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6363. }
  6364. ChargingAlarmProcess(gun_index);
  6365. }
  6366. else if(isNormalStopChargeFlag(gun_index) == YES)
  6367. {
  6368. PRINTF_FUNC("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  6369. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  6370. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6371. {
  6372. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6373. }
  6374. ChargingTerminalProcess(gun_index);
  6375. }
  6376. else if(CheckBackendChargingTimeout(gun_index) ||
  6377. CheckBackendChargingEnergy(gun_index) ||
  6378. (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf &&
  6379. strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL))
  6380. {
  6381. PRINTF_FUNC("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  6382. // 板端或後臺要求停止
  6383. ChargingTerminalProcess(gun_index);
  6384. }
  6385. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf)
  6386. {
  6387. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = NO;
  6388. }
  6389. }
  6390. break;
  6391. case S_ALARM:
  6392. case S_TERMINATING:
  6393. {
  6394. if (isModeChange(gun_index))
  6395. {
  6396. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6397. {
  6398. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  6399. }
  6400. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  6401. {
  6402. PRINTF_FUNC("================== S_ALARM (%x) ================== \n", gun_index + 1);
  6403. UpdateErrorCodeToOcpp(gun_index);
  6404. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  6405. {
  6406. OcppStopTransation(gun_index);
  6407. }
  6408. TheEndCharging(gun_index);
  6409. }
  6410. else
  6411. {
  6412. PRINTF_FUNC("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  6413. }
  6414. StopGunInfoTimeoutDet(gun_index);
  6415. }
  6416. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  6417. {
  6418. if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  6419. {
  6420. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6421. {
  6422. PRINTF_FUNC ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  6423. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  6424. }
  6425. }
  6426. if((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false) &&
  6427. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6428. {
  6429. setChargerMode(gun_index, MODE_IDLE);
  6430. }
  6431. }
  6432. else
  6433. {
  6434. setChargerMode(gun_index, MODE_COMPLETE);
  6435. }
  6436. }
  6437. break;
  6438. case S_COMPLETE:
  6439. {
  6440. if (isModeChange(gun_index))
  6441. {
  6442. PRINTF_FUNC("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  6443. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  6444. {
  6445. OcppStopTransation(gun_index);
  6446. }
  6447. TheEndCharging(gun_index);
  6448. }
  6449. if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  6450. {
  6451. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6452. {
  6453. PRINTF_FUNC ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  6454. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  6455. }
  6456. }
  6457. if((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false) &&
  6458. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6459. {
  6460. setChargerMode(gun_index, MODE_IDLE);
  6461. }
  6462. }
  6463. break;
  6464. case S_CCS_PRECHARGE_ST0:
  6465. if (isModeChange(gun_index))
  6466. {
  6467. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  6468. StopGunInfoTimeoutDet(gun_index);
  6469. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  6470. }
  6471. break;
  6472. case S_CCS_PRECHARGE_ST1:
  6473. if (isModeChange(gun_index))
  6474. {
  6475. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  6476. }
  6477. break;
  6478. }
  6479. }
  6480. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  6481. {
  6482. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  6483. }
  6484. else
  6485. {
  6486. bool dcPageRun = true;
  6487. if (dcPageRun)
  6488. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  6489. }
  6490. write(wtdFd, "a", 1);
  6491. usleep(whileLoopTime);
  6492. }
  6493. return FAIL;
  6494. }