main.c 253 KB

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