main.c 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233
  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.12.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. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2422. EmcOccureByString("012251");
  2423. }
  2424. else
  2425. {
  2426. ReleaseEmsOccureByString(0, "012251");
  2427. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2428. }
  2429. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2430. {
  2431. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2432. //EmcOccureByString("012238");
  2433. }
  2434. else
  2435. {
  2436. //ReleaseEmsOccureByString(0, "012238");
  2437. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2438. }
  2439. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2440. {
  2441. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2442. }
  2443. else
  2444. {
  2445. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2446. }
  2447. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2448. {
  2449. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2450. EmcOccureByString("012252");
  2451. }
  2452. else
  2453. {
  2454. ReleaseEmsOccureByString(0, "012252");
  2455. }
  2456. }
  2457. struct timeval _AcContactReOpen_Time;
  2458. void ChkPsuStatus(void)
  2459. {
  2460. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2461. {
  2462. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2463. {
  2464. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2465. {
  2466. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2467. }
  2468. }
  2469. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2470. {
  2471. EmcOccureByString("012267");
  2472. }
  2473. }
  2474. else
  2475. {
  2476. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2477. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2478. {
  2479. bool allFaultStatus = true;
  2480. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2481. {
  2482. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT)
  2483. {
  2484. allFaultStatus = false;
  2485. }
  2486. }
  2487. if(allFaultStatus)
  2488. {
  2489. PRINTF_FUNC("PSU Failure, Need Resume");
  2490. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2491. }
  2492. }
  2493. }
  2494. }
  2495. int AcContactorReOpenCount = 0;
  2496. void PsuFailureResume(void)
  2497. {
  2498. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2499. {
  2500. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2501. {
  2502. PRINTF_FUNC("Ac Contactor Off By Psu Failure");
  2503. // update _AcContactReOpen_Time
  2504. gettimeofday(&_AcContactReOpen_Time, NULL);
  2505. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2506. AcContactorReOpenCount++;
  2507. }
  2508. else
  2509. {
  2510. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2511. {
  2512. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2513. {
  2514. ReleaseEmsOccureByString(0, "012267");
  2515. // update _AcContactReOpen_Time
  2516. gettimeofday(&_AcContactReOpen_Time, NULL);
  2517. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2518. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2519. PRINTF_FUNC("Psu Failure Resume");
  2520. }
  2521. }
  2522. }
  2523. }
  2524. else
  2525. {
  2526. if(AcContactorReOpenCount > 0)
  2527. {
  2528. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2529. {
  2530. AcContactorReOpenCount = 0;
  2531. }
  2532. }
  2533. }
  2534. }
  2535. void SetAcContactor(unsigned char OnOff)
  2536. {
  2537. OnOff = OnOff ? ON : OFF;
  2538. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2539. {
  2540. if(OnOff)
  2541. {
  2542. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2543. }
  2544. PRINTF_FUNC("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2545. }
  2546. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2547. }
  2548. void StandbyCheck(void)
  2549. {
  2550. if(IsConnectorWholeIdle())
  2551. {
  2552. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2553. {
  2554. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2555. {
  2556. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= STANDBY_TIME)
  2557. {
  2558. SetAcContactor(OFF);
  2559. }
  2560. }
  2561. }
  2562. else
  2563. {
  2564. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2565. gettimeofday(&_Standby_time, NULL);
  2566. }
  2567. }
  2568. else
  2569. {
  2570. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2571. {
  2572. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2573. }
  2574. }
  2575. }
  2576. void CheckDisconnectionStatusRecovery(void)
  2577. {
  2578. bool recovery = true;
  2579. // check all connector
  2580. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2581. {
  2582. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2583. {
  2584. recovery = false;
  2585. }
  2586. }
  2587. if(recovery)
  2588. {
  2589. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2590. }
  2591. }
  2592. //===============================================
  2593. // 確認各小板偵測的錯誤狀況
  2594. //===============================================
  2595. void CheckErrorOccurStatus(byte index)
  2596. {
  2597. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2598. {
  2599. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2600. }
  2601. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2602. {
  2603. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2604. {
  2605. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2606. }
  2607. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2608. {
  2609. PRINTF_FUNC("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2610. }
  2611. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2612. }
  2613. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2614. {
  2615. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2616. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2617. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2618. if(chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2619. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2620. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2621. chargingInfo[index]->SystemStatus == S_CHARGING)
  2622. {
  2623. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2624. {
  2625. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012304", 6);
  2626. }
  2627. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2628. {
  2629. PRINTF_FUNC("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2630. }
  2631. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2632. }
  2633. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2634. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2635. {
  2636. setChargerMode(index, MODE_IDLE);
  2637. }
  2638. }
  2639. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2640. {
  2641. CheckDisconnectionStatusRecovery();
  2642. }
  2643. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2644. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2645. {
  2646. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2647. {
  2648. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2649. StopChargingProcessByString(2);
  2650. }
  2651. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2652. {
  2653. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2654. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012251", 6);
  2655. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2656. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012252", 6);
  2657. }
  2658. }
  2659. else
  2660. {
  2661. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2662. {
  2663. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2664. ReleaseChargingProcessByString(2);
  2665. }
  2666. }
  2667. // RB
  2668. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2669. {
  2670. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2671. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2672. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2673. {
  2674. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2675. {
  2676. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2677. StopChargingProcessByString(2);
  2678. }
  2679. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2680. {
  2681. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2682. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012203", 6);
  2683. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2684. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012204", 6);
  2685. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2686. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012205", 6);
  2687. }
  2688. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2689. {
  2690. PRINTF_FUNC("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2691. }
  2692. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2693. }
  2694. else
  2695. {
  2696. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2697. {
  2698. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2699. ReleaseChargingProcessByString(2);
  2700. }
  2701. }
  2702. }
  2703. else
  2704. {
  2705. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2706. {
  2707. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2708. ReleaseChargingProcessByString(2);
  2709. }
  2710. }
  2711. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2712. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2713. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2714. {
  2715. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2716. {
  2717. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2718. StopChargingProcessByString(2);
  2719. }
  2720. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2721. {
  2722. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2723. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012200", 6);
  2724. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2725. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012201", 6);
  2726. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2727. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012202", 6);
  2728. }
  2729. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2730. {
  2731. PRINTF_FUNC("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2732. }
  2733. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2734. }
  2735. else
  2736. {
  2737. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2738. {
  2739. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2740. ReleaseChargingProcessByString(2);
  2741. }
  2742. }
  2743. }
  2744. /*
  2745. //===============================================
  2746. // 確認 GPIO 狀態
  2747. //===============================================
  2748. void gpio_set_value(unsigned int gpio, unsigned int value)
  2749. {
  2750. int fd;
  2751. char buf[MAX_BUF];
  2752. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2753. fd = open(buf, O_WRONLY);
  2754. if (fd < 0)
  2755. {
  2756. perror("gpio/set-value");
  2757. return;
  2758. }
  2759. if (value)
  2760. write(fd, "1", 2);
  2761. else
  2762. write(fd, "0", 2);
  2763. close(fd);
  2764. }
  2765. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2766. {
  2767. int fd;
  2768. char buf[MAX_BUF];
  2769. char ch;
  2770. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2771. fd = open(buf, O_RDONLY);
  2772. if (fd < 0) {
  2773. perror("gpio/get-value");
  2774. return fd;
  2775. }
  2776. read(fd, &ch, 1);
  2777. if (ch != '0') {
  2778. *value = 1;
  2779. } else {
  2780. *value = 0;
  2781. }
  2782. close(fd);
  2783. return 0;
  2784. }
  2785. void CheckGunTypeFromHw()
  2786. {
  2787. int pinIn[4] = { 22, 23, 44, 45 };
  2788. unsigned int gpioValue = 0;
  2789. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2790. gpio_get_value(pinIn[i], &gpioValue);
  2791. {
  2792. switch (pinIn[i])
  2793. {
  2794. case 22:
  2795. bd1_1_status = gpioValue;
  2796. break;
  2797. case 23:
  2798. bd1_2_status = gpioValue;
  2799. break;
  2800. case 44:
  2801. bd0_1_status = gpioValue;
  2802. break;
  2803. case 45:
  2804. bd0_2_status = gpioValue;
  2805. break;
  2806. }
  2807. }
  2808. }
  2809. }
  2810. void CheckGpioInStatus()
  2811. {
  2812. int pinIn[2] = { 27, 47 };
  2813. unsigned int gpioValue = 0;
  2814. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2815. {
  2816. gpio_get_value(pinIn[i], &gpioValue);
  2817. if (gpioValue == 0x01)
  2818. {
  2819. switch(pinIn[i])
  2820. {
  2821. // 小板緊急停止
  2822. case 47:
  2823. {
  2824. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2825. {
  2826. if (chargingInfo[i]->slotsIndex == 1)
  2827. {
  2828. if (chargingInfo[i]->Type == _Type_Chademo)
  2829. BoardErrOccurByString(i, "023730");
  2830. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2831. BoardErrOccurByString(i, "013627");
  2832. break;
  2833. }
  2834. }
  2835. }
  2836. break;
  2837. case 27:
  2838. {
  2839. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2840. {
  2841. if (chargingInfo[i]->slotsIndex == 3)
  2842. {
  2843. if (chargingInfo[i]->Type == _Type_Chademo)
  2844. BoardErrOccurByString(i, "023730");
  2845. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2846. BoardErrOccurByString(i, "013627");
  2847. break;
  2848. }
  2849. }
  2850. }
  2851. break;
  2852. }
  2853. }
  2854. else
  2855. {
  2856. switch (pinIn[i])
  2857. {
  2858. // 小板解除緊急停止
  2859. case 47:
  2860. {
  2861. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2862. {
  2863. if (chargingInfo[i]->slotsIndex == 1)
  2864. {
  2865. if (chargingInfo[i]->Type == _Type_Chademo)
  2866. ReleaseBoardErrOccurByString(i, "023730");
  2867. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2868. ReleaseBoardErrOccurByString(i, "013627");
  2869. break;
  2870. }
  2871. }
  2872. }
  2873. break;
  2874. case 27:
  2875. {
  2876. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2877. {
  2878. if (chargingInfo[i]->slotsIndex == 3)
  2879. {
  2880. if (chargingInfo[i]->Type == _Type_Chademo)
  2881. ReleaseBoardErrOccurByString(i, "023730");
  2882. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2883. ReleaseBoardErrOccurByString(i, "013627");
  2884. break;
  2885. }
  2886. }
  2887. }
  2888. break;
  2889. }
  2890. }
  2891. }
  2892. }
  2893. */
  2894. //===============================================
  2895. // Main process
  2896. //===============================================
  2897. // 檢查 Byte 中某個 Bit 的值
  2898. // _byte : 欲改變的 byte
  2899. // _bit : 該 byte 的第幾個 bit
  2900. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2901. {
  2902. return ( _byte & mask_table[_bit] ) != 0x00;
  2903. }
  2904. // 設定 Byte 中某個 Bit的值
  2905. // _byte : 欲改變的 byte
  2906. // _bit : 該 byte 的第幾個 bit
  2907. // value : 修改的值為 0 or 1
  2908. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2909. {
  2910. if(value == 1)
  2911. *_byte |= (1 << _bit);
  2912. else if (value == 0)
  2913. *_byte ^= (1 << _bit);
  2914. }
  2915. void UserScanFunction()
  2916. {
  2917. }
  2918. unsigned char isModeChange(unsigned char gun_index)
  2919. {
  2920. unsigned char result = NO;
  2921. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  2922. {
  2923. result = YES;
  2924. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2925. }
  2926. return result;
  2927. }
  2928. void ScannerCardProcess()
  2929. {
  2930. }
  2931. struct timeval _StartAuthorizing_Time;
  2932. void PowerCabinetAuthorizingSettingInitial(void)
  2933. {
  2934. ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = 0;
  2935. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = 0;
  2936. ShmSysConfigAndInfo->SysInfo.AuthorizedType = _AuthResult_None;
  2937. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Idle;
  2938. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = false;
  2939. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  2940. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = false;
  2941. }
  2942. void AnnounceAccountBalance(unsigned char index, int balance)
  2943. {
  2944. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  2945. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  2946. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AccountBalanceRequest = true;
  2947. PRINTF_FUNC("Announce Account Balance %d To Connector %d", balance, index + 1);
  2948. }
  2949. void FouceAnnounceAccountBalance(unsigned char index)
  2950. {
  2951. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  2952. {
  2953. AnnounceAccountBalance(index, 0);
  2954. }
  2955. }
  2956. void CleanConnectorUserIdAndPrice(unsigned char index)
  2957. {
  2958. if(chargingInfo[index]->SystemStatus == S_IDLE)
  2959. {
  2960. PRINTF_FUNC("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  2961. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  2962. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  2963. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -6553500;
  2964. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  2965. }
  2966. }
  2967. void DispenserAuthorizationInitial(unsigned char dispenser)
  2968. {
  2969. PRINTF_FUNC("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  2970. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].UserId, 0x00, 32);
  2971. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Idle;
  2972. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthTarget = 0;
  2973. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType = _AuthType_None;
  2974. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_None;
  2975. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.EnableAutoGunSelection = false;
  2976. }
  2977. void ConnectorAuthorizationInitial(unsigned char connector)
  2978. {
  2979. PRINTF_FUNC("*********** Connector %d Authorizing Initial ***********", connector + 1);
  2980. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingType = _AuthType_None;
  2981. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingResult = _AuthResult_None;
  2982. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.SwipeRfidConfirm = false;
  2983. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest = false;
  2984. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartConfirm = false;
  2985. chargingInfo[connector]->RemoteStartFlag = false;
  2986. CleanConnectorUserIdAndPrice(connector);
  2987. }
  2988. void SetAuthorizingTarget(unsigned char dispenser, unsigned char target, unsigned char type)
  2989. {
  2990. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  2991. switch(type)
  2992. {
  2993. case _AuthType_RFID:
  2994. PRINTF_FUNC("*********** Dispenser %d Target %02X RFID Authorize ***********", dispenser + 1, target);
  2995. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].UserId, 32);
  2996. break;
  2997. case _AuthType_RemoteStart:
  2998. PRINTF_FUNC("*********** Connector %d Remote Start Authorize ***********", target);
  2999. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[target - 1].IdTag, 20);
  3000. break;
  3001. }
  3002. if(target == AUTO_GUN_SELECTION)
  3003. {
  3004. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.EnableAutoGunSelection = true;
  3005. }
  3006. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Busy;
  3007. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType = type;
  3008. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = true;
  3009. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  3010. ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser = dispenser;
  3011. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget = target;
  3012. ShmSysConfigAndInfo->SysInfo.AuthorizedType = type;
  3013. }
  3014. void OCPPAuthorizingStart(void)
  3015. {
  3016. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3017. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3018. }
  3019. void AuthorizeTimeoutProcess(void)
  3020. {
  3021. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3022. {
  3023. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizingResult == _AuthResult_Invalid)
  3024. {
  3025. FouceAnnounceAccountBalance(index);
  3026. if(GetTimeoutValue(_ConnectorAuthorizeFail_Time[index]) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3027. {
  3028. PRINTF_FUNC("*********** Connector id %d Authorized Fail Return ***********", index + 1);
  3029. ConnectorAuthorizationInitial(index);
  3030. }
  3031. }
  3032. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthStatus == _AuthorizeStatus_End)
  3033. {
  3034. int CompletedTime = 0;
  3035. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthTarget != AUTO_GUN_SELECTION)
  3036. {
  3037. CompletedTime = DISPENSER_AUTH_COMP_TIME;
  3038. }
  3039. else
  3040. {
  3041. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthResult == _AuthResult_Valid)
  3042. {
  3043. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  3044. }
  3045. else
  3046. {
  3047. CompletedTime = AUTHORIZE_FAIL_TIMEOUT;
  3048. }
  3049. }
  3050. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= CompletedTime)
  3051. {
  3052. PRINTF_FUNC("*********** Dispenser id %d Authorized Return ***********", index + 1);
  3053. DispenserAuthorizationInitial(index);
  3054. }
  3055. }
  3056. }
  3057. }
  3058. bool IsNewAuthorizingRequest(void)
  3059. {
  3060. bool NewRequest = false;
  3061. bool FindRemoteStartRequest = false;
  3062. // remote start check
  3063. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3064. {
  3065. if(ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq == true)
  3066. {
  3067. unsigned char dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].ParentDispensetIndex;
  3068. SetAuthorizingTarget(dispenser, index + 1, _AuthType_RemoteStart);
  3069. ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq = false;
  3070. chargingInfo[index]->RemoteStartFlag = true;
  3071. FindRemoteStartRequest = true;
  3072. NewRequest = true;
  3073. break;
  3074. }
  3075. }
  3076. if(!FindRemoteStartRequest)
  3077. {
  3078. // local rfid authorize check
  3079. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3080. {
  3081. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthStatus == _AuthorizeStatus_Idle &&
  3082. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest == true)
  3083. {
  3084. if(strlen((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId) > 0)
  3085. {
  3086. SetAuthorizingTarget(index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthTarget, _AuthType_RFID);
  3087. NewRequest = true;
  3088. }
  3089. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest = false;
  3090. }
  3091. }
  3092. }
  3093. return NewRequest;
  3094. }
  3095. void PowerCabinetAuthorizeProcess(void)
  3096. {
  3097. unsigned char dispenser = 0, connector = 0;
  3098. dispenser = ShmSysConfigAndInfo->SysInfo.AuthorizedDispenser;
  3099. AuthorizeTimeoutProcess();
  3100. switch(ShmSysConfigAndInfo->SysInfo.AuthorizedStatus)
  3101. {
  3102. case _AuthorizeStatus_Idle:
  3103. if(IsNewAuthorizingRequest())
  3104. {
  3105. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Wait;
  3106. }
  3107. break;
  3108. case _AuthorizeStatus_Wait:
  3109. if(ShmSysConfigAndInfo->SysInfo.AuthorizedType == _AuthType_RemoteStart)
  3110. {
  3111. PRINTF_FUNC("*********** Connector %d Remote Start Authorizing Bypass ***********",
  3112. ShmSysConfigAndInfo->SysInfo.AuthorizedTarget);
  3113. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = true;
  3114. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Valid;
  3115. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3116. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3117. }
  3118. else
  3119. {
  3120. if(ShmOCPP16Data->OcppConnStatus)
  3121. {
  3122. PRINTF_FUNC("*********** Dispenser %d OPCC AuthorizeReq ***********", dispenser + 1);
  3123. OCPPAuthorizingStart();
  3124. }
  3125. else
  3126. {
  3127. PRINTF_FUNC("*********** Dispenser %d Start Local Authorizing ***********", dispenser + 1);
  3128. }
  3129. // update start authorizing timeout
  3130. gettimeofday(&_StartAuthorizing_Time, NULL);
  3131. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Busy;
  3132. }
  3133. break;
  3134. case _AuthorizeStatus_Busy:
  3135. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3136. {
  3137. PRINTF_FUNC("*********** Dispenser %d Authorizing Timeout ***********", dispenser + 1);
  3138. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Invalid;
  3139. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3140. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3141. }
  3142. else
  3143. {
  3144. if(ShmOCPP16Data->OcppConnStatus)
  3145. {
  3146. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf)
  3147. {
  3148. BOOL accept = false;
  3149. if(strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == EQUAL)
  3150. {
  3151. accept = true;
  3152. }
  3153. PRINTF_FUNC("*********** Dispenser %d OCPP Authorize %s ***********", dispenser + 1, accept ? "OK" : "NG");
  3154. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = true;
  3155. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = accept ? _AuthResult_Valid : _AuthResult_Invalid;
  3156. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3157. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3158. }
  3159. }
  3160. else
  3161. {
  3162. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3163. {
  3164. PRINTF_FUNC("*********** Dispenser %d No Charging ***********", dispenser + 1);
  3165. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Invalid;
  3166. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3167. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3168. }
  3169. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  3170. {
  3171. // set authorize pass when offline policy is free charge
  3172. PRINTF_FUNC("*********** Dispenser %d Free Charging ***********", dispenser + 1);
  3173. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Valid;
  3174. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3175. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3176. }
  3177. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3178. {
  3179. BOOL find = false;
  3180. // check white list
  3181. for(int i = 0; i < 10; i++)
  3182. {
  3183. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3184. {
  3185. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3186. {
  3187. find = true;
  3188. break;
  3189. }
  3190. }
  3191. }
  3192. PRINTF_FUNC("*********** Dispenser %d White Card %s ***********", dispenser + 1, find ? "OK" : "NG");
  3193. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = find ? _AuthResult_Valid : _AuthResult_Invalid;
  3194. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3195. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3196. }
  3197. else
  3198. {
  3199. PRINTF_FUNC("*********** Dispenser %d Invalid Policy ***********", dispenser + 1);
  3200. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult = _AuthResult_Invalid;
  3201. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_Done;
  3202. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_Done;
  3203. }
  3204. }
  3205. }
  3206. break;
  3207. case _AuthorizeStatus_Done:
  3208. if(ShmSysConfigAndInfo->SysInfo.AuthorizedTarget == AUTO_GUN_SELECTION)
  3209. {
  3210. // auto gun selection
  3211. }
  3212. else
  3213. {
  3214. connector = ShmSysConfigAndInfo->SysInfo.AuthorizedTarget - 1;
  3215. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingType = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType;
  3216. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingResult = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult;
  3217. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].GeneralChargingData.SystemStatus == S_IDLE &&
  3218. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult == _AuthResult_Valid)
  3219. {
  3220. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType == _AuthType_RFID)
  3221. {
  3222. PRINTF_FUNC("*********** Connector %d RFID Authorize OK ***********", connector + 1);
  3223. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  3224. }
  3225. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthType == _AuthType_RemoteStart)
  3226. {
  3227. PRINTF_FUNC("*********** Connector id %d RemoteAuthorize OK ***********", connector + 1);
  3228. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  3229. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.RemoteStartRequest = true;
  3230. }
  3231. }
  3232. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult == _AuthResult_Invalid)
  3233. {
  3234. // update connector authorizing timeout
  3235. gettimeofday(&_ConnectorAuthorizeFail_Time[connector], NULL);
  3236. }
  3237. }
  3238. // authorize completed
  3239. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = true;
  3240. // update dispenser authorizing timeout
  3241. gettimeofday(&_DispenserAuthorizing_Time[dispenser], NULL);
  3242. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthStatus = _AuthorizeStatus_End;
  3243. // update authorizing completed timeout
  3244. gettimeofday(&_StartAuthorizing_Time, NULL);
  3245. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  3246. break;
  3247. case _AuthorizeStatus_End:
  3248. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3249. {
  3250. PRINTF_FUNC("*********** PowerCabinet Authorize Completed ***********");
  3251. PowerCabinetAuthorizingSettingInitial();
  3252. }
  3253. break;
  3254. default:
  3255. // update authorizing completed timeout
  3256. gettimeofday(&_StartAuthorizing_Time, NULL);
  3257. ShmSysConfigAndInfo->SysInfo.AuthorizedStatus = _AuthorizeStatus_End;
  3258. break;
  3259. }
  3260. }
  3261. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  3262. {
  3263. BOOL success = false;
  3264. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthorizingResult == _AuthResult_Valid)
  3265. {
  3266. success = true;
  3267. }
  3268. return success;
  3269. }
  3270. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  3271. {
  3272. BOOL success = false;
  3273. unsigned char dispenser = 0;
  3274. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  3275. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.EnableAutoGunSelection &&
  3276. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthResult == _AuthResult_Valid)
  3277. {
  3278. success = true;
  3279. }
  3280. return success;
  3281. }
  3282. /*
  3283. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3284. {
  3285. bool result = true;
  3286. switch (typeValue)
  3287. {
  3288. case '0': // none
  3289. break;
  3290. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3291. break;
  3292. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3293. break;
  3294. case '3': // IEC 62196-2 Type 2 Plug
  3295. case '4': // IEC 62196-2 Type 2 Socket
  3296. if (AC_QUANTITY > _ac_Index)
  3297. {
  3298. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3299. // AC 固定 index
  3300. ac_chargingInfo[_acgunIndex]->Index = 0;
  3301. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3302. ac_chargingInfo[_acgunIndex]->SystemStatus = S_IDLE;
  3303. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3304. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3305. _ac_Index++;
  3306. _acgunIndex++;
  3307. }
  3308. else
  3309. result = false;
  3310. break;
  3311. case '5': // GB/T AC Plug
  3312. break;
  3313. case '6': // GB/T AC Socket
  3314. break;
  3315. case 'J': // CHAdeMO
  3316. {
  3317. if (CHAdeMO_QUANTITY > _chademoIndex)
  3318. {
  3319. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3320. chargingInfo[_gunIndex]->Index = _gunIndex;
  3321. chargingInfo[_gunIndex]->ReservationId = -1;
  3322. chargingInfo[_gunIndex]->slotsIndex = slots;
  3323. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3324. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3325. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3326. chargingInfo[_gunIndex]->IsAvailable = YES;
  3327. _chademoIndex++;
  3328. _gunIndex++;
  3329. }
  3330. else
  3331. result = false;
  3332. }
  3333. break;
  3334. case 'U': // CCS1 combo
  3335. case 'E': // CCS2 combo
  3336. {
  3337. if (CCS_QUANTITY > _ccsIndex)
  3338. {
  3339. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3340. chargingInfo[_gunIndex]->Index = _gunIndex;
  3341. chargingInfo[_gunIndex]->ReservationId = -1;
  3342. chargingInfo[_gunIndex]->slotsIndex = slots;
  3343. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3344. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  3345. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3346. chargingInfo[_gunIndex]->IsAvailable = YES;
  3347. // 現階段預設為走 DIN70121
  3348. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3349. _ccsIndex++;
  3350. _gunIndex++;
  3351. }
  3352. else
  3353. result = false;
  3354. }
  3355. break;
  3356. case 'G': // GBT DC
  3357. {
  3358. if (GB_QUANTITY > _gb_Index)
  3359. {
  3360. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3361. chargingInfo[_gunIndex]->Index = _gunIndex;
  3362. chargingInfo[_gunIndex]->ReservationId = -1;
  3363. chargingInfo[_gunIndex]->slotsIndex = slots;
  3364. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3365. chargingInfo[_gunIndex]->Type = _Type_GB;
  3366. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3367. chargingInfo[_gunIndex]->IsAvailable = YES;
  3368. _gb_Index++;
  3369. _gunIndex++;
  3370. }
  3371. else
  3372. result = false;
  3373. }
  3374. break;
  3375. case 'D': // GBT DC x 2
  3376. break;
  3377. }
  3378. return result;
  3379. }
  3380. */
  3381. bool CheckConnectorTypeStatus()
  3382. {
  3383. bool result = true;
  3384. // PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  3385. // bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  3386. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3387. {
  3388. // byte slots = 1;
  3389. // for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3390. // {
  3391. // if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3392. // {
  3393. // return false;
  3394. // }
  3395. // slots++;
  3396. // }
  3397. // AC index 接在 DC 後面
  3398. // if (AC_QUANTITY > 0)
  3399. // ac_chargingInfo[0]->Index += _gunIndex;
  3400. // DO360 for Audi, two gun only
  3401. _gunIndex = 2;
  3402. _acgunIndex = 0;
  3403. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3404. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3405. PRINTF_FUNC("DC _gunCount = %d, AC _gunCount = %d \n",
  3406. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3407. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3408. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3409. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3410. result = false;
  3411. //DO360 charging info default setting
  3412. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3413. {
  3414. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  3415. chargingInfo[i]->Index = 0xFF;
  3416. chargingInfo[i]->Evboard_id = 0x00;
  3417. chargingInfo[i]->ReservationId = -1;
  3418. chargingInfo[i]->slotsIndex = 0;
  3419. chargingInfo[i]->SystemStatus = S_BOOTING;
  3420. chargingInfo[i]->Type = _Type_Unknown;
  3421. chargingInfo[i]->type_index = 0;
  3422. chargingInfo[i]->IsAvailable = YES;
  3423. }
  3424. /*
  3425. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3426. {
  3427. chargingInfo[0]->Evboard_id = 0x01;
  3428. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", 0, chargingInfo[0]->Type, chargingInfo[0]->Evboard_id);
  3429. }
  3430. else
  3431. {
  3432. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3433. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3434. {
  3435. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3436. {
  3437. // 與硬體相同 type : Chademo
  3438. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3439. {
  3440. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3441. }
  3442. }
  3443. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3444. {
  3445. // 與硬體相同 type : CCS
  3446. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3447. {
  3448. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3449. }
  3450. }
  3451. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3452. {
  3453. // 與硬體相同 type : GB
  3454. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3455. {
  3456. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3457. }
  3458. }
  3459. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3460. {
  3461. // 與硬體相同 type : Chademo
  3462. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3463. {
  3464. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3465. }
  3466. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3467. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3468. }
  3469. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3470. {
  3471. // 與硬體相同 type : CCS
  3472. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3473. {
  3474. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3475. }
  3476. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3477. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3478. }
  3479. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3480. {
  3481. // 與硬體相同 type : GB
  3482. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3483. {
  3484. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3485. }
  3486. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3487. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3488. }
  3489. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  3490. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3491. result = false;
  3492. }
  3493. }
  3494. */
  3495. }
  3496. else
  3497. {
  3498. // Module Name 不正確 - 告警
  3499. result = false;
  3500. }
  3501. return result;
  3502. }
  3503. void KillTaskExceptPrimary()
  3504. {
  3505. ChangeLcmByIndex(_LCM_FIX);
  3506. system("killall Module_EvComm");
  3507. system("killall Module_InternalComm");
  3508. system("killall Module_PsuComm");
  3509. system("killall Module_4g &");
  3510. system("killall Module_Wifi &");
  3511. }
  3512. void KillAllTask()
  3513. {
  3514. ChangeLcmByIndex(_LCM_FIX);
  3515. system("killall Module_EventLogging");
  3516. system("killall Module_PrimaryComm");
  3517. system("killall Module_EvComm");
  3518. system("killall Module_LcmControl");
  3519. system("killall Module_InternalComm");
  3520. system("killall Module_PsuComm");
  3521. system("killall OcppBackend &");
  3522. system("killall Module_4g &");
  3523. system("killall Module_Wifi &");
  3524. }
  3525. int PutDispenserImage(char *IpAddress, char *SourcePath)
  3526. {
  3527. int isPass = PASS;
  3528. unsigned char ftpcmdbuf[256];
  3529. PRINTF_FUNC("Transfer image(%s) to ip: %s ", SourcePath, IpAddress);
  3530. //Using ftpput command to transfer upgrade image,
  3531. //User name : root
  3532. //User password : y42j/4cj84
  3533. //Destination : as same as source path
  3534. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  3535. if(system((char*)ftpcmdbuf) != 0)
  3536. {
  3537. isPass = FAIL;
  3538. PRINTF_FUNC("Transfer Image NG - FTP put upgrade image to fail");
  3539. }
  3540. else
  3541. {
  3542. PRINTF_FUNC("FTP put Image OK");
  3543. }
  3544. return isPass;
  3545. }
  3546. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  3547. {
  3548. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  3549. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  3550. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  3551. PRINTF_FUNC("Trigger dispenser %d to update image %s", index + 1, FileName);
  3552. }
  3553. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName)
  3554. {
  3555. int isPass = FAIL;
  3556. BOOL transferred = FALSE;
  3557. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3558. {
  3559. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  3560. {
  3561. char DispenserIpAddress[16];
  3562. memset((char*)DispenserIpAddress, 0x00, 16);
  3563. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  3564. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  3565. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  3566. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  3567. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  3568. isPass = PutDispenserImage(DispenserIpAddress, SourcePath);
  3569. if(isPass == PASS)
  3570. {
  3571. TriggerDispenserUpgrade(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex, FileName);
  3572. }
  3573. transferred = true;
  3574. }
  3575. }
  3576. return transferred;
  3577. }
  3578. BOOL WaitAllDispenserUpgradeCompleted(void)
  3579. {
  3580. BOOL completed = TRUE;
  3581. gettimeofday(&_DispenserUpgrade_time, NULL);
  3582. do
  3583. {
  3584. completed = TRUE;
  3585. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3586. {
  3587. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  3588. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  3589. {
  3590. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  3591. {
  3592. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  3593. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  3594. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  3595. PRINTF_FUNC("Dispenser %d Upgrade Success", i + 1);
  3596. }
  3597. else
  3598. {
  3599. completed = FALSE;
  3600. }
  3601. }
  3602. }
  3603. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  3604. {
  3605. PRINTF_FUNC("Wait Upgrade Timeout");
  3606. completed = FALSE;
  3607. break;
  3608. }
  3609. if(completed == FALSE)
  3610. {
  3611. sleep(1);
  3612. }
  3613. }while(!completed);
  3614. return completed;
  3615. }
  3616. bool IsModelNameMatch(char *model)
  3617. {
  3618. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  3619. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  3620. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  3621. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  3622. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  3623. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  3624. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  3625. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  3626. {
  3627. return true;
  3628. }
  3629. return false;
  3630. }
  3631. char CheckUpdateProcess()
  3632. {
  3633. char model[32];
  3634. bool support = false;
  3635. bool OneImageOnly = false;
  3636. int isPass = true;
  3637. uint8_t retSucc = 0;
  3638. uint8_t retFail = 0;
  3639. byte target = 0;
  3640. char *new_str = NULL;
  3641. unsigned char *ptr = NULL;
  3642. int fd = 0;
  3643. int uartFd = 0;
  3644. unsigned int Type = 0;
  3645. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3646. DIR *d;
  3647. struct dirent *dir;
  3648. char cmdBuf[128];
  3649. d = opendir("/mnt/");
  3650. if(d)
  3651. {
  3652. while((dir = readdir(d)) != NULL && !OneImageOnly)
  3653. {
  3654. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  3655. continue;
  3656. }
  3657. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  3658. strcat(new_str, "/mnt/");
  3659. strcat(new_str, dir->d_name);
  3660. fd = open(new_str, O_RDONLY);
  3661. if(fd < 0)
  3662. {
  3663. free(new_str);
  3664. continue;
  3665. }
  3666. DEBUG_INFO_MSG("%s\n", new_str);
  3667. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  3668. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3669. //get the image length
  3670. ImageLen = read(fd, ptr, MaxLen);
  3671. if (ImageLen > 20)
  3672. {
  3673. support = false;
  3674. memcpy(model, ptr, 16);
  3675. PRINTF_FUNC("Model: %s", model);
  3676. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  3677. PRINTF_FUNC("Typed...%x", Type);
  3678. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  3679. if(IsModelNameMatch(model))
  3680. {
  3681. switch (Type)
  3682. {
  3683. case 0x10000001:
  3684. case 0x10000002:
  3685. case 0x10000003:
  3686. case 0x10000004:
  3687. case 0x10000005:
  3688. support = true;
  3689. _UpgradeNeedReboot = TRUE;
  3690. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3691. break;
  3692. case 0x10000006: // primary mcu
  3693. case 0x1000000D: // relay board
  3694. case 0x1000000E: // fan board
  3695. case 0x10000014: // led board
  3696. support = true;
  3697. target = 0x00;
  3698. if (Type == 0x10000006)
  3699. {
  3700. target = UPGRADE_PRI;
  3701. }
  3702. else if (Type == 0x1000000D)
  3703. {
  3704. target = UPGRADE_RB;
  3705. }
  3706. else if (Type == 0x1000000E)
  3707. {
  3708. target = UPGRADE_FAN;
  3709. }
  3710. else if (Type == 0x10000014)
  3711. {
  3712. target = UPGRADE_LED;
  3713. }
  3714. uartFd = InitComPort(target);
  3715. if(target == UPGRADE_RB)
  3716. {
  3717. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3718. if(isPass == PASS)
  3719. {
  3720. retSucc++;
  3721. }
  3722. else
  3723. {
  3724. retFail++;
  3725. }
  3726. PRINTF_FUNC("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  3727. sleep(3);
  3728. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3729. }
  3730. else
  3731. {
  3732. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  3733. }
  3734. close(fd);
  3735. break;
  3736. }
  3737. if(support)
  3738. {
  3739. if(isPass == PASS)
  3740. {
  3741. retSucc++;
  3742. }
  3743. else
  3744. {
  3745. retFail++;
  3746. }
  3747. PRINTF_FUNC("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  3748. }
  3749. }
  3750. else
  3751. {
  3752. if(model[0] == 'D' && model[1] == 'D')
  3753. {
  3754. OneImageOnly = true;
  3755. _UpgradeNeedReboot = TRUE;
  3756. PRINTF_FUNC("Start Put Image To All Dispenser");
  3757. PutImageToAllDispenser(new_str, dir->d_name);
  3758. WaitAllDispenserUpgradeCompleted();
  3759. PRINTF_FUNC("All Dispenser Upgrade Completed");
  3760. }
  3761. }
  3762. }
  3763. sprintf(cmdBuf, "rm -f %s", new_str);
  3764. system(cmdBuf);
  3765. free(new_str);
  3766. free(ptr);
  3767. }
  3768. }
  3769. free(dir);
  3770. closedir(d);
  3771. if(retFail != 0)
  3772. {
  3773. return FAIL;
  3774. }
  3775. return PASS;
  3776. }
  3777. /*
  3778. void CreateRfidFork()
  3779. {
  3780. pid_t rfidRecPid;
  3781. rfidRecPid = fork();
  3782. if (rfidRecPid == 0)
  3783. {
  3784. while(true)
  3785. {
  3786. // 刷卡判斷
  3787. RFID rfid;
  3788. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  3789. !ShmSysConfigAndInfo->SysConfig.isRFID)
  3790. {}
  3791. else if(getRequestCardSN(rfidFd, 0, &rfid))
  3792. {
  3793. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3794. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  3795. {
  3796. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  3797. {
  3798. switch (rfid.snType)
  3799. {
  3800. case RFID_SN_TYPE_6BYTE:
  3801. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3802. "%02X%02X%02X%02X%02X%02X",
  3803. rfid.currentCard[0], rfid.currentCard[1],
  3804. rfid.currentCard[2], rfid.currentCard[3],
  3805. rfid.currentCard[4], rfid.currentCard[5]);
  3806. break;
  3807. case RFID_SN_TYPE_7BYTE:
  3808. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3809. "%02X%02X%02X%02X%02X%02X%02X",
  3810. rfid.currentCard[0], rfid.currentCard[1],
  3811. rfid.currentCard[2], rfid.currentCard[3],
  3812. rfid.currentCard[4], rfid.currentCard[5],
  3813. rfid.currentCard[6]);
  3814. break;
  3815. case RFID_SN_TYPE_10BYTE:
  3816. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3817. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3818. rfid.currentCard[0], rfid.currentCard[1],
  3819. rfid.currentCard[2], rfid.currentCard[3],
  3820. rfid.currentCard[4], rfid.currentCard[5],
  3821. rfid.currentCard[6], rfid.currentCard[7],
  3822. rfid.currentCard[8], rfid.currentCard[9]);
  3823. break;
  3824. case RFID_SN_TYPE_4BYTE:
  3825. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3826. "%02X%02X%02X%02X",
  3827. rfid.currentCard[0], rfid.currentCard[1],
  3828. rfid.currentCard[2], rfid.currentCard[3]);
  3829. break;
  3830. }
  3831. }
  3832. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  3833. {
  3834. switch (rfid.snType)
  3835. {
  3836. case RFID_SN_TYPE_6BYTE:
  3837. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3838. "%02X%02X%02X%02X%02X%02X",
  3839. rfid.currentCard[5], rfid.currentCard[4],
  3840. rfid.currentCard[3], rfid.currentCard[2],
  3841. rfid.currentCard[1], rfid.currentCard[0]);
  3842. break;
  3843. case RFID_SN_TYPE_7BYTE:
  3844. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3845. "%02X%02X%02X%02X%02X%02X%02X",
  3846. rfid.currentCard[6], rfid.currentCard[5],
  3847. rfid.currentCard[4], rfid.currentCard[3],
  3848. rfid.currentCard[2], rfid.currentCard[1],
  3849. rfid.currentCard[0]);
  3850. break;
  3851. case RFID_SN_TYPE_10BYTE:
  3852. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3853. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3854. rfid.currentCard[9], rfid.currentCard[8],
  3855. rfid.currentCard[7], rfid.currentCard[6],
  3856. rfid.currentCard[5], rfid.currentCard[4],
  3857. rfid.currentCard[3], rfid.currentCard[2],
  3858. rfid.currentCard[1], rfid.currentCard[0]);
  3859. break;
  3860. case RFID_SN_TYPE_4BYTE:
  3861. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3862. "%02X%02X%02X%02X",
  3863. rfid.currentCard[3], rfid.currentCard[2],
  3864. rfid.currentCard[1], rfid.currentCard[0]);
  3865. break;
  3866. }
  3867. }
  3868. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3869. }
  3870. }
  3871. sleep(1);
  3872. }
  3873. }
  3874. }
  3875. */
  3876. void StartSystemTimeoutDet(unsigned char flag)
  3877. {
  3878. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  3879. {
  3880. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3881. }
  3882. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3883. }
  3884. void StopSystemTimeoutDet()
  3885. {
  3886. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3887. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3888. }
  3889. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3890. {
  3891. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3892. {
  3893. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  3894. {
  3895. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  3896. }
  3897. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3898. }
  3899. }
  3900. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3901. {
  3902. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3903. {
  3904. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3905. }
  3906. }
  3907. void TriggerDispenserConnectionTimeoutSetting(void)
  3908. {
  3909. BOOL trigger = FALSE;
  3910. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3911. {
  3912. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  3913. {
  3914. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  3915. trigger = TRUE;
  3916. }
  3917. }
  3918. if(trigger)
  3919. {
  3920. PRINTF_FUNC("********** Trigger Dispenser Connector Timeout Value: %d **********\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout);
  3921. }
  3922. }
  3923. void CheckConnectionTimeout(void)
  3924. {
  3925. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3926. {
  3927. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3928. }
  3929. else
  3930. {
  3931. if(ShmOCPP16Data->OcppConnStatus &&
  3932. strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  3933. {
  3934. _connectionTimeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3935. if(_connectionTimeout <= 0)
  3936. {
  3937. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3938. }
  3939. }
  3940. else
  3941. {
  3942. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3943. }
  3944. }
  3945. if(_connectionTimeout != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  3946. {
  3947. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout = _connectionTimeout;
  3948. TriggerDispenserConnectionTimeoutSetting();
  3949. }
  3950. }
  3951. void CreateTimeoutFork()
  3952. {
  3953. pid_t timeoutPid;
  3954. timeoutPid = fork();
  3955. if (timeoutPid > 0)
  3956. {
  3957. gettimeofday(&_cmdSubPriority_time, NULL);
  3958. CheckConnectionTimeout();
  3959. while(true)
  3960. {
  3961. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  3962. {
  3963. CheckConnectionTimeout();
  3964. gettimeofday(&_cmdSubPriority_time, NULL);
  3965. }
  3966. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3967. // 系統
  3968. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  3969. {
  3970. // case Timeout_SelftestChk:
  3971. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT)
  3972. // {
  3973. // _SelfTestTimeout();
  3974. // StopSystemTimeoutDet();
  3975. // }
  3976. // break;
  3977. // case Timeout_Authorizing:
  3978. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3979. // {
  3980. // _AuthorizedTimeout();
  3981. // StopSystemTimeoutDet();
  3982. // }
  3983. // break;
  3984. // case Timeout_VerifyFail:
  3985. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3986. // {
  3987. // _AutoReturnTimeout();
  3988. // StopSystemTimeoutDet();
  3989. // }
  3990. // break;
  3991. // case Timeout_VerifyComp:
  3992. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3993. // {
  3994. // _AutoReturnTimeout();
  3995. // StopSystemTimeoutDet();
  3996. // }
  3997. // break;
  3998. case Timeout_WaitPlug:
  3999. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout)
  4000. {
  4001. _DetectPlugInTimeout();
  4002. StopSystemTimeoutDet();
  4003. }
  4004. break;
  4005. case Timeout_ReturnToChargingGunDet:
  4006. {
  4007. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE)
  4008. {
  4009. DisplayChargingInfo();
  4010. StopSystemTimeoutDet();
  4011. }
  4012. }
  4013. break;
  4014. case Timeout_AuthorizingForStop:
  4015. {
  4016. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4017. {
  4018. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4019. ClearAuthorizedFlag();
  4020. StopSystemTimeoutDet();
  4021. }
  4022. }
  4023. break;
  4024. }
  4025. // 各槍
  4026. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4027. {
  4028. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4029. switch(chargingInfo[gun_index]->TimeoutFlag)
  4030. {
  4031. case Timeout_Preparing:
  4032. {
  4033. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT)
  4034. {
  4035. _PrepareTimeout(gun_index);
  4036. StopGunInfoTimeoutDet(gun_index);
  4037. }
  4038. }
  4039. break;
  4040. case Timeout_EvChargingDet:
  4041. {
  4042. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT)
  4043. {
  4044. _DetectEvChargingEnableTimeout(gun_index);
  4045. StopGunInfoTimeoutDet(gun_index);
  4046. }
  4047. }
  4048. break;
  4049. case Timeout_EvseChargingDet:
  4050. {
  4051. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT)
  4052. {
  4053. _DetectEvseChargingEnableTimeout(gun_index);
  4054. StopGunInfoTimeoutDet(gun_index);
  4055. }
  4056. }
  4057. break;
  4058. case Timeout_EvseCompleteDet:
  4059. {
  4060. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT)
  4061. {
  4062. StopGunInfoTimeoutDet(gun_index);
  4063. }
  4064. }
  4065. break;
  4066. case Timeout_ForCcsPrechargeDet:
  4067. {
  4068. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT)
  4069. {
  4070. _CcsPrechargeTimeout(gun_index);
  4071. StopGunInfoTimeoutDet(gun_index);
  4072. }
  4073. }
  4074. break;
  4075. }
  4076. }
  4077. sleep(1);
  4078. }
  4079. }
  4080. }
  4081. void GetSystemTime()
  4082. {
  4083. struct timeb csuTime;
  4084. struct tm *tmCSU;
  4085. ftime(&csuTime);
  4086. tmCSU = localtime(&csuTime.time);
  4087. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4088. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4089. tmCSU->tm_sec);
  4090. // byte date[14];
  4091. //
  4092. //
  4093. // //sprintf(&date, "%d", );
  4094. //
  4095. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4096. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4097. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4098. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4099. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4100. //
  4101. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4102. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4103. //
  4104. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4105. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4106. //
  4107. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4108. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4109. //
  4110. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4111. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4112. //
  4113. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4114. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4115. // 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],
  4116. // date[4], date[5], date[6], date[7],
  4117. // date[8], date[9], date[10], date[11],
  4118. // date[12], date[13]);
  4119. }
  4120. void CheckFactoryConfigFunction()
  4121. {
  4122. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4123. {
  4124. system("cd /root;./FactoryConfig -m");
  4125. system("sync");
  4126. sleep(5);
  4127. system("reboot -f");
  4128. sleep(5);
  4129. system("reboot -f");
  4130. }
  4131. }
  4132. void CheckFwUpdateFunction()
  4133. {
  4134. char UpdateResult = PASS;
  4135. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4136. {
  4137. DEBUG_INFO_MSG("ftp : update start.");
  4138. //KillTask();
  4139. ChangeLcmByIndex(_LCM_FIX);
  4140. system("killall Module_PrimaryComm");
  4141. system("killall Module_InternalComm");
  4142. UpdateResult = CheckUpdateProcess();
  4143. if (UpdateResult == PASS)
  4144. {
  4145. DEBUG_INFO_MSG("ftp : update complete.");
  4146. }
  4147. else
  4148. {
  4149. DEBUG_INFO_MSG("ftp : update fail.");
  4150. }
  4151. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4152. sleep(5);
  4153. if(_UpgradeNeedReboot)
  4154. {
  4155. system("reboot -f");
  4156. }
  4157. else
  4158. {
  4159. KillAllTask();
  4160. system("/usr/bin/run_evse_restart.sh");
  4161. }
  4162. }
  4163. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  4164. {
  4165. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  4166. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  4167. {
  4168. DEBUG_INFO_MSG("Backend : update start. \n");
  4169. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  4170. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4171. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  4172. //KillTask();
  4173. system("killall Module_PrimaryComm");
  4174. system("killall Module_InternalComm");
  4175. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4176. {
  4177. setChargerMode(_index, MODE_UPDATE);
  4178. }
  4179. UpdateResult = CheckUpdateProcess();
  4180. if (UpdateResult == PASS)
  4181. {
  4182. DEBUG_INFO_MSG("Backend : update complete. \n");
  4183. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4184. }
  4185. else
  4186. {
  4187. DEBUG_INFO_MSG("Backend : update fail. \n");
  4188. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4189. }
  4190. //strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4191. sleep(1);
  4192. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  4193. sleep(5);
  4194. if(_UpgradeNeedReboot)
  4195. {
  4196. system("reboot -f");
  4197. }
  4198. else
  4199. {
  4200. KillAllTask();
  4201. system("/usr/bin/run_evse_restart.sh");
  4202. }
  4203. }
  4204. }
  4205. }
  4206. //===============================================
  4207. // Check reservation date is expired
  4208. //===============================================
  4209. int isReservationExpired(unsigned char gun_index)
  4210. {
  4211. int result = NO;
  4212. struct tm expiredDate;
  4213. struct timeb expiredTime;
  4214. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  4215. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  4216. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  4217. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  4218. {
  4219. expiredDate.tm_year -= 1900;
  4220. expiredDate.tm_mon -= 1;
  4221. expiredTime.time = mktime(&expiredDate);
  4222. if (!CheckTimeOut(expiredTime))
  4223. {
  4224. result = YES;
  4225. }
  4226. }
  4227. return result;
  4228. }
  4229. struct timeval _Reboot_time;
  4230. void TriggerDispenserHardwareReboot(void)
  4231. {
  4232. BOOL trigger = FALSE;
  4233. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4234. {
  4235. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4236. {
  4237. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm &&
  4238. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest == 0)
  4239. {
  4240. trigger = TRUE;
  4241. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest = 1;
  4242. }
  4243. }
  4244. }
  4245. if(trigger)
  4246. {
  4247. gettimeofday(&_Reboot_time, NULL);
  4248. PRINTF_FUNC("********** Trigger Dispenser Hardware Reboot **********");
  4249. }
  4250. }
  4251. BOOL IsDispenserHardwareRebootResponsed(void)
  4252. {
  4253. BOOL response_ok = TRUE;
  4254. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4255. {
  4256. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4257. {
  4258. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  4259. {
  4260. response_ok = FALSE;
  4261. // 10s for reboot request timeout
  4262. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  4263. {
  4264. response_ok = TRUE;
  4265. PRINTF_FUNC("Reboot request timeout");
  4266. }
  4267. }
  4268. }
  4269. }
  4270. return response_ok;
  4271. }
  4272. struct timeval _Reset_time;
  4273. void TriggerDispenserSoftwareReset(void)
  4274. {
  4275. BOOL trigger = FALSE;
  4276. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4277. {
  4278. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4279. {
  4280. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  4281. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest == 0)
  4282. {
  4283. trigger = TRUE;
  4284. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest = 1;
  4285. }
  4286. }
  4287. }
  4288. if(trigger)
  4289. {
  4290. gettimeofday(&_Reset_time, NULL);
  4291. PRINTF_FUNC("********** Trigger Dispenser Software Reset **********");
  4292. }
  4293. }
  4294. BOOL IsDispenserSoftwareResetResponsed(void)
  4295. {
  4296. BOOL response_ok = TRUE;
  4297. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4298. {
  4299. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4300. {
  4301. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  4302. {
  4303. response_ok = FALSE;
  4304. // 10s for reset request timeout
  4305. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  4306. {
  4307. response_ok = TRUE;
  4308. PRINTF_FUNC("Reset request timeout");
  4309. }
  4310. }
  4311. }
  4312. }
  4313. return response_ok;
  4314. }
  4315. // the unit of return value: 0.01 dollar
  4316. int TariffParsing(char *StringItem, char *TariffCode)
  4317. {
  4318. char *ptrSave, *ptrToken;
  4319. char strSource[128];
  4320. int fee = 0;
  4321. memcpy(strSource, StringItem, 128);
  4322. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4323. while(ptrToken != NULL)
  4324. {
  4325. char *strTariff = strstr(ptrToken, TariffCode);
  4326. if(strTariff != NULL)
  4327. {
  4328. char *strFee = strstr(strTariff, "$");
  4329. if(strFee != NULL)
  4330. {
  4331. strFee++;
  4332. float fFee = atof(strFee);
  4333. fee = (int)(fFee * 100);
  4334. break;
  4335. }
  4336. }
  4337. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4338. }
  4339. return fee;
  4340. }
  4341. void DefaultPriceHandler(char *StrDefaultPrice)
  4342. {
  4343. BOOL trigger = FALSE;
  4344. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4345. PRINTF_FUNC("********** Default Price: %s", StrDefaultPrice);
  4346. ConnectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4347. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4348. CurrentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4349. PRINTF_FUNC("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4350. OccupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4351. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4352. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice = CurrentRate;
  4353. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4354. {
  4355. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4356. {
  4357. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest == 0)
  4358. {
  4359. trigger = TRUE;
  4360. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest = 1;
  4361. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest = 1;
  4362. }
  4363. }
  4364. }
  4365. if(trigger)
  4366. {
  4367. PRINTF_FUNC("********** Trigger Dispenser Default Price **********");
  4368. }
  4369. }
  4370. void UserPriceHandler(char *UserId, char *UserPrice)
  4371. {
  4372. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4373. int AccountBalance = 0;
  4374. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4375. CurrentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4376. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4377. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4378. PRINTF_FUNC("********** User Id: %s **********", ShmOCPP16Data->Cost.SetUserPrice.idToken);
  4379. PRINTF_FUNC("********** User Price: %s", ShmOCPP16Data->Cost.SetUserPrice.price);
  4380. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4381. PRINTF_FUNC("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4382. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4383. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4384. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4385. {
  4386. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  4387. {
  4388. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  4389. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4390. {
  4391. PRINTF_FUNC("********** Connector Id %d User Price %d.%02d, Account Balance %d.%02d **********",
  4392. (i + 1), (CurrentRate / 100), (CurrentRate % 100), (AccountBalance / 100), (AccountBalance % 100));
  4393. }
  4394. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  4395. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4396. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  4397. {
  4398. AnnounceAccountBalance(i, AccountBalance);
  4399. }
  4400. }
  4401. }
  4402. }
  4403. void RunningFinalCostHandler(void)
  4404. {
  4405. unsigned int ConnectionFee = 0, SessionFee = 0, OccupancyFee = 0, TotalCost = 0;
  4406. int AccountBalance = 0;
  4407. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4408. {
  4409. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  4410. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_COMPLETE)
  4411. {
  4412. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[i].description) > 0)
  4413. {
  4414. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Connection Fee");
  4415. SessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Session Fee");
  4416. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Occupancy Fee");
  4417. TotalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Total Cost");
  4418. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Account Balance");
  4419. PRINTF_FUNC("********** Connector Id: %d Running Cost **********", (i + 1));
  4420. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4421. PRINTF_FUNC("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4422. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4423. PRINTF_FUNC("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4424. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4425. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4426. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4427. {
  4428. PRINTF_FUNC("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4429. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4430. }
  4431. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4432. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4433. memset(&ShmOCPP16Data->Cost.RunningCost[i], 0x00, sizeof(struct StrcutRunningFinalCost));
  4434. }
  4435. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[i].description) > 0)
  4436. {
  4437. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Connection Fee");
  4438. SessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Session Fee");
  4439. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Occupancy Fee");
  4440. TotalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Total Cost");
  4441. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Account Balance");
  4442. PRINTF_FUNC("********** Connector Id: %d Final Cost **********", (i + 1));
  4443. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4444. PRINTF_FUNC("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4445. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4446. PRINTF_FUNC("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4447. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4448. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4449. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4450. {
  4451. PRINTF_FUNC("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4452. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4453. }
  4454. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4455. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4456. memset(&ShmOCPP16Data->Cost.FinalCost[i], 0x00, sizeof(struct StrcutRunningFinalCost));
  4457. }
  4458. }
  4459. }
  4460. }
  4461. struct timeval _Cost_Price_time;
  4462. char MyDefaultPriceString[128];
  4463. struct StrcutSetUserPrice MyUserPrice;
  4464. char MyConnectorRunningCostString[CONNECTOR_QUANTITY][128];
  4465. char MyConnectorFinalCostString[CONNECTOR_QUANTITY][128];
  4466. void CheckOcppCostAndPrice(void)
  4467. {
  4468. // 5s for reboot request timeout
  4469. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  4470. {
  4471. gettimeofday(&_Cost_Price_time, NULL);
  4472. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  4473. {
  4474. DefaultPriceHandler((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  4475. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  4476. }
  4477. if(strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 && strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  4478. {
  4479. UserPriceHandler((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  4480. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  4481. }
  4482. RunningFinalCostHandler();
  4483. }
  4484. }
  4485. //===============================================
  4486. // OCPP
  4487. //===============================================
  4488. void CheckOcppStatus()
  4489. {
  4490. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  4491. {
  4492. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  4493. PRINTF_FUNC("*********************OCPP Boot Notification****************************\n");
  4494. }
  4495. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  4496. {
  4497. bool canReset = true;
  4498. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  4499. {
  4500. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4501. {
  4502. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  4503. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  4504. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  4505. {
  4506. canReset = false;
  4507. if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  4508. {
  4509. ChargingTerminalProcess(_index);
  4510. }
  4511. }
  4512. }
  4513. }
  4514. if(canReset)
  4515. {
  4516. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  4517. {
  4518. //trigger dispenser to hardware reboot
  4519. TriggerDispenserHardwareReboot();
  4520. if(IsDispenserHardwareRebootResponsed())
  4521. {
  4522. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4523. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4524. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  4525. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4526. sleep(3);
  4527. system("reboot -f");
  4528. }
  4529. }
  4530. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  4531. {
  4532. //trigger dispenser to software reset
  4533. TriggerDispenserSoftwareReset();
  4534. if(IsDispenserSoftwareResetResponsed())
  4535. {
  4536. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4537. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4538. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  4539. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4540. sleep(3);
  4541. system("killall OcppBackend &");
  4542. KillAllTask();
  4543. system("/usr/bin/run_evse_restart.sh");
  4544. }
  4545. }
  4546. }
  4547. }
  4548. CheckOcppCostAndPrice();
  4549. }
  4550. void OcppStartTransation(byte gunIndex)
  4551. {
  4552. byte _OcppGunIndex = gunIndex;
  4553. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4554. PRINTF_FUNC("Gun index %d, OCPP Start Transation Index %d, IdTag = %s \n", chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  4555. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  4556. }
  4557. void OcppStopTransation(byte gunIndex)
  4558. {
  4559. byte _OcppGunIndex = gunIndex;
  4560. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4561. PRINTF_FUNC("Gun index %d, OCPP Stop Transation Index %d, IdTag = %s \n", chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  4562. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  4563. }
  4564. // no use
  4565. bool OcppRemoteStop(byte gunIndex)
  4566. {
  4567. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4568. // 有 AC 槍的話
  4569. if (acDirIndex > 0 && gunIndex > 0)
  4570. {
  4571. gunIndex += acDirIndex;
  4572. }
  4573. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  4574. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4575. {
  4576. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4577. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4578. }
  4579. return result;
  4580. }
  4581. void OcppRemoteStartChk()
  4582. {
  4583. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  4584. {}
  4585. else if(!isDetectPlugin())
  4586. {
  4587. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  4588. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4589. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  4590. {
  4591. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES)
  4592. {
  4593. if (ac_chargingInfo[ac_index]->SystemStatus == S_IDLE ||
  4594. ac_chargingInfo[ac_index]->SystemStatus == S_RESERVATION)
  4595. {
  4596. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4597. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  4598. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4599. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  4600. ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  4601. DetectPluginStart();
  4602. return;
  4603. }
  4604. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4605. }
  4606. }
  4607. byte threeGunIndex = 0;
  4608. byte dcIndex = 0;
  4609. bool isGunUsingStatus = false;
  4610. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4611. {
  4612. // 如果有 AC 槍,且 DC 槍也有兩把
  4613. if (acDirIndex == 1 && _index == 1)
  4614. threeGunIndex = 1;
  4615. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES)
  4616. dcIndex = _index;
  4617. if (chargingInfo[_index]->SystemStatus != S_IDLE)
  4618. {
  4619. isGunUsingStatus = true;
  4620. }
  4621. }
  4622. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  4623. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  4624. {
  4625. if (dcIndex == 0)
  4626. threeGunIndex = 0;
  4627. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4628. return;
  4629. }
  4630. if (dcIndex == 0)
  4631. threeGunIndex = 0;
  4632. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES)
  4633. {
  4634. if (chargingInfo[dcIndex]->SystemStatus == S_IDLE ||
  4635. chargingInfo[dcIndex]->SystemStatus == S_RESERVATION)
  4636. {
  4637. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  4638. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4639. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  4640. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4641. DetectPluginStart();
  4642. }
  4643. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4644. }
  4645. }
  4646. }
  4647. void ChkOcppStatus(byte gunIndex)
  4648. {
  4649. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  4650. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  4651. {
  4652. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  4653. if (isReservationExpired(gunIndex))
  4654. {
  4655. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4656. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4657. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  4658. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  4659. }
  4660. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  4661. }
  4662. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  4663. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  4664. {
  4665. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  4666. if (isReservationExpired(gunIndex))
  4667. {
  4668. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4669. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4670. chargingInfo[gunIndex]->ReservationId = 0;
  4671. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  4672. }
  4673. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  4674. }
  4675. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  4676. {
  4677. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4678. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4679. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  4680. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  4681. {
  4682. if (isDb_ready)
  4683. DB_Update_Operactive(localDb, gunIndex, true);
  4684. chargingInfo[gunIndex]->IsAvailable = YES;
  4685. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  4686. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4687. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4688. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4689. {
  4690. setChargerMode(gunIndex, MODE_IDLE);
  4691. }
  4692. }
  4693. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  4694. {
  4695. if (isDb_ready)
  4696. DB_Update_Operactive(localDb, gunIndex, false);
  4697. chargingInfo[gunIndex]->IsAvailable = NO;
  4698. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  4699. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4700. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4701. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4702. {
  4703. setChargerMode(gunIndex, MODE_MAINTAIN);
  4704. }
  4705. }
  4706. }
  4707. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES)
  4708. {
  4709. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  4710. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  4711. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  4712. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  4713. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  4714. {
  4715. // 充電中,需停止充電
  4716. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  4717. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.UnlockStopRequest = true;
  4718. PRINTF_FUNC("********** OcppUnlockStop Gun %d **********\n", gunIndex);
  4719. }
  4720. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  4721. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  4722. }
  4723. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4724. {
  4725. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4726. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  4727. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  4728. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  4729. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  4730. {
  4731. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4732. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.RemoteStopRequest = true;
  4733. PRINTF_FUNC("********** OcppRemoteStop Gun %d **********\n", gunIndex);
  4734. }
  4735. }
  4736. }
  4737. bool CheckBackendChargingTimeout(byte gunIndex)
  4738. {
  4739. bool result = false;
  4740. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4741. {
  4742. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  4743. {
  4744. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  4745. result = true;
  4746. }
  4747. }
  4748. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4749. {
  4750. // 隨插即充電的要看 offline
  4751. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  4752. {
  4753. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  4754. result = true;
  4755. }
  4756. }
  4757. return result;
  4758. }
  4759. bool CheckBackendChargingEnergy(byte gunIndex)
  4760. {
  4761. bool result = false;
  4762. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4763. {
  4764. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  4765. {
  4766. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  4767. result = true;
  4768. }
  4769. }
  4770. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4771. {
  4772. // 隨插即充電的要看 offline
  4773. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  4774. {
  4775. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  4776. result = true;
  4777. }
  4778. }
  4779. return result;
  4780. }
  4781. void InformOcppErrOccur(byte codeType)
  4782. {
  4783. char _error[25];
  4784. switch(codeType)
  4785. {
  4786. case 4: strcpy(_error, "InternalError"); break;
  4787. case 6: strcpy(_error, "NoError"); break;
  4788. case 7: strcpy(_error, "OtherError"); break;
  4789. case 13: strcpy(_error, "UnderVoltage"); break;
  4790. case 14: strcpy(_error, "OverVoltage"); break;
  4791. }
  4792. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4793. {
  4794. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, _error);
  4795. }
  4796. }
  4797. //===============================================
  4798. // SQLite3 related routine
  4799. //===============================================
  4800. int DB_Open(sqlite3 *db)
  4801. {
  4802. int result = PASS;
  4803. char* errMsg = NULL;
  4804. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  4805. "idx integer primary key AUTOINCREMENT, "
  4806. "reservationId text, "
  4807. "transactionId text, "
  4808. "startMethod text, "
  4809. "userId text, "
  4810. "dateTimeStart text, "
  4811. "dateTimeStop text,"
  4812. "socStart text, "
  4813. "socStop text, "
  4814. "chargeEnergy text, "
  4815. "stopReason text"
  4816. ");";
  4817. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  4818. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4819. "`IsAvailable` TEXT NOT NULL, "
  4820. "`connector` INTEGER NOT NULL, "
  4821. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  4822. if(sqlite3_open(DB_FILE, &db))
  4823. {
  4824. result = FAIL;
  4825. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4826. sqlite3_close(db);
  4827. }
  4828. else
  4829. {
  4830. PRINTF_FUNC( "Local charging record database open successfully.");
  4831. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  4832. {
  4833. result = FAIL;
  4834. PRINTF_FUNC( "Create local charging record table error message: %s", errMsg);
  4835. }
  4836. else
  4837. {
  4838. PRINTF_FUNC( "Opened local charging record table successfully");
  4839. }
  4840. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  4841. {
  4842. result = FAIL;
  4843. PRINTF_FUNC( "Create local config table error message: %s", errMsg);
  4844. }
  4845. else
  4846. {
  4847. PRINTF_FUNC( "Opened local config table successfully");
  4848. }
  4849. sqlite3_close(db);
  4850. }
  4851. return result;
  4852. }
  4853. int DB_Insert_Record(sqlite3 *db, int gun_index)
  4854. {
  4855. int result = PASS;
  4856. char* errMsg = NULL;
  4857. char insertSql[1024];
  4858. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  4859. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  4860. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  4861. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  4862. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  4863. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  4864. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  4865. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  4866. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4867. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4868. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  4869. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4870. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  4871. {
  4872. result = FAIL;
  4873. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4874. sqlite3_close(db);
  4875. }
  4876. else
  4877. {
  4878. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4879. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  4880. {
  4881. result = FAIL;
  4882. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  4883. }
  4884. else
  4885. {
  4886. PRINTF_FUNC( "Insert local charging record successfully\n");
  4887. }
  4888. sqlite3_close(db);
  4889. }
  4890. return result;
  4891. }
  4892. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  4893. {
  4894. uint8_t result = false;
  4895. char* errMsg = NULL;
  4896. char sqlStr[1024];
  4897. srand(time(NULL));
  4898. if(sqlite3_open(DB_FILE, &db))
  4899. {
  4900. result = FAIL;
  4901. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4902. sqlite3_close(db);
  4903. }
  4904. else
  4905. {
  4906. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  4907. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  4908. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  4909. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  4910. {
  4911. result = FAIL;
  4912. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  4913. }
  4914. else
  4915. {
  4916. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  4917. }
  4918. sqlite3_close(db);
  4919. }
  4920. return result;
  4921. }
  4922. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  4923. {
  4924. uint8_t result = true;
  4925. char* errMsg = NULL;
  4926. char sqlStr[1024];
  4927. char **rs;
  4928. int rows, cols;
  4929. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  4930. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  4931. if(sqlite3_open(DB_FILE, &db))
  4932. {
  4933. result = FAIL;
  4934. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4935. sqlite3_close(db);
  4936. }
  4937. else
  4938. {
  4939. PRINTF_FUNC( "Local config query database open successfully.");
  4940. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  4941. if(rows>0)
  4942. {
  4943. for(int idxRow=1;idxRow<=rows;idxRow++)
  4944. {
  4945. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  4946. {
  4947. result = false;
  4948. }
  4949. PRINTF_FUNC("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  4950. }
  4951. }
  4952. else
  4953. {
  4954. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.", gun_index);
  4955. }
  4956. sqlite3_free_table(rs);
  4957. sqlite3_close(db);
  4958. }
  4959. return result;
  4960. }
  4961. //===============================================
  4962. // Config process
  4963. //===============================================
  4964. void AddPlugInTimes(byte gunIndex)
  4965. {
  4966. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  4967. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  4968. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  4969. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  4970. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  4971. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  4972. }
  4973. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  4974. {
  4975. char cmdBuf[32];
  4976. struct timeb csuTime;
  4977. struct tm *tmCSU;
  4978. ftime(&csuTime);
  4979. tmCSU = localtime(&csuTime.time);
  4980. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  4981. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4982. tmCSU->tm_sec);
  4983. if (isStart)
  4984. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  4985. else
  4986. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  4987. }
  4988. void zipLogFiles()
  4989. {
  4990. const char* logPath = "/Storage/SystemLog";
  4991. // 獲取目錄
  4992. DIR* pDir = opendir(logPath);
  4993. if (pDir != NULL)
  4994. {
  4995. struct timeb csuTime;
  4996. struct tm *tmCSU;
  4997. ftime(&csuTime);
  4998. tmCSU = localtime(&csuTime.time);
  4999. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  5000. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  5001. // tmCSU->tm_sec);
  5002. // Read items inside the folder
  5003. struct dirent* pEntry = NULL;
  5004. while ((pEntry = readdir(pDir)) != NULL)
  5005. {
  5006. if (strcmp(pEntry->d_name, ".") != 0 &&
  5007. strcmp(pEntry->d_name, "..") != 0 &&
  5008. strncmp(pEntry->d_name, "[", 1) == 0 &&
  5009. strstr(pEntry->d_name, "tar") < 0)
  5010. {
  5011. char yearC[5];
  5012. unsigned short year = 0;
  5013. char monthC[3];
  5014. unsigned short month = 0;
  5015. yearC[4] = '\0';
  5016. strncpy(yearC, pEntry->d_name + 1, 4);
  5017. monthC[2] = '\0';
  5018. strncpy(monthC, pEntry->d_name + 6, 2);
  5019. year = atoi(yearC);
  5020. month = atoi(monthC);
  5021. if (year != 0)
  5022. {
  5023. if (year < tmCSU->tm_year + 1900 ||
  5024. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  5025. {
  5026. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  5027. char file[256];
  5028. memset(file, 0x00, sizeof(file));
  5029. strcat(file, "tar zcvf ");
  5030. strcat(file, logPath);
  5031. strncat(file, "/", 1);
  5032. strcat(file, pEntry->d_name);
  5033. strcat(file, ".tar");
  5034. strncat(file, " ", 1);
  5035. strcat(file, logPath);
  5036. strncat(file, "/", 1);
  5037. strcat(file, pEntry->d_name);
  5038. PRINTF_FUNC("zip = %s \n", file);
  5039. system(file);
  5040. }
  5041. }
  5042. }
  5043. }
  5044. }
  5045. // Close folder
  5046. closedir(pDir);
  5047. }
  5048. void ChangeGunSelectByIndex(byte sel)
  5049. {
  5050. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  5051. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  5052. }
  5053. void CheckIsAlternatvieByModelName()
  5054. {
  5055. // 黑白機 ?
  5056. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  5057. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL ||
  5058. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DSYE301J3EW2PH") == EQUAL ||
  5059. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UW1PH") == EQUAL ||
  5060. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UD1PH") == EQUAL)
  5061. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  5062. else
  5063. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  5064. }
  5065. void StopProcessingLoop()
  5066. {
  5067. for (;;)
  5068. {
  5069. CheckFactoryConfigFunction();
  5070. CheckFwUpdateFunction();
  5071. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5072. {
  5073. ChkPrimaryStatus();
  5074. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  5075. {
  5076. PRINTF_FUNC("Soft reboot for retry self-tets (Primary). \n");
  5077. KillAllTask();
  5078. sleep(3);
  5079. system("/usr/bin/run_evse_restart.sh");
  5080. return;
  5081. }
  5082. }
  5083. sleep(1);
  5084. }
  5085. }
  5086. void CreateWatchdog()
  5087. {
  5088. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  5089. {
  5090. wtdFd = InitWatchDog();
  5091. if (wtdFd < 0)
  5092. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  5093. }
  5094. }
  5095. bool IsConnectorWholeIdle()
  5096. {
  5097. bool result = true;
  5098. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  5099. {
  5100. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  5101. chargingInfo[count]->SystemStatus != S_RESERVATION &&
  5102. chargingInfo[count]->SystemStatus != S_FAULT)
  5103. {
  5104. result = false;
  5105. break;
  5106. }
  5107. }
  5108. return result;
  5109. }
  5110. void ClearAlarmCodeWhenAcOff()
  5111. {
  5112. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  5113. {
  5114. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  5115. }
  5116. }
  5117. //==========================================
  5118. // Check task processing
  5119. //==========================================
  5120. void CheckTask()
  5121. {
  5122. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  5123. {
  5124. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  5125. {
  5126. if(system("pidof -s Module_4g > /dev/null") != 0)
  5127. {
  5128. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  5129. system("/root/Module_4g &");
  5130. }
  5131. }
  5132. }
  5133. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  5134. {
  5135. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  5136. {
  5137. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  5138. {
  5139. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  5140. system("/root/Module_Wifi &");
  5141. }
  5142. }
  5143. }
  5144. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5145. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5146. {
  5147. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  5148. {
  5149. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  5150. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5151. system("pkill OcppBackend");
  5152. }
  5153. if(system("pidof -s OcppBackend > /dev/null") != 0)
  5154. {
  5155. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  5156. system("/root/OcppBackend &");
  5157. }
  5158. }
  5159. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  5160. {
  5161. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  5162. system ("/root/Module_ProduceUtils &");
  5163. }
  5164. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  5165. {
  5166. DEBUG_ERROR_MSG("Module_EvComm not running, restart it.\r\n");
  5167. system ("/root/Module_EvComm &");
  5168. }
  5169. }
  5170. void InitialDHCP()
  5171. {
  5172. char tmpbuf[256];
  5173. memset(tmpbuf,0,256);
  5174. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  5175. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  5176. system(tmpbuf);
  5177. }
  5178. void InitialDispenserDhcpServerConfig(void)
  5179. {
  5180. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  5181. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  5182. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  5183. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  5184. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  5185. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  5186. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  5187. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  5188. usleep(1000);
  5189. }
  5190. void StartDispenserDhcpServer(void)
  5191. {
  5192. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  5193. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  5194. }
  5195. //==========================================
  5196. // Check Smart Charging Profile
  5197. //==========================================
  5198. int GetStartScheduleTime(unsigned char *time)
  5199. {
  5200. int result = -1;
  5201. struct tm tmScheduleStart;
  5202. struct timeb tbScheduleStart;
  5203. 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))
  5204. {
  5205. tmScheduleStart.tm_year -= 1900;
  5206. tmScheduleStart.tm_mon -= 1;
  5207. tbScheduleStart.time = mktime(&tmScheduleStart);
  5208. tbScheduleStart.millitm = 0;
  5209. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  5210. }
  5211. return result;
  5212. }
  5213. void CheckSmartChargeProfile(byte _index)
  5214. {
  5215. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO)
  5216. {
  5217. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO)
  5218. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  5219. }
  5220. else
  5221. {
  5222. // Get Charging Profile
  5223. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  5224. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL)
  5225. {
  5226. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  5227. byte _startCount = NO_DEFINE;
  5228. byte _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  5229. for (byte _count = 0; _count < _maxCount; _count++)
  5230. {
  5231. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5232. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod &&
  5233. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  5234. {
  5235. _startCount = _count;
  5236. }
  5237. }
  5238. PRINTF_FUNC("_startCount = %d \n", _startCount);
  5239. if (_startCount < _maxCount)
  5240. {
  5241. PRINTF_FUNC("*********Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  5242. chargingInfo[_index]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * 10;
  5243. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * chargingInfo[_index]->EvBatterytargetVoltage / 100;
  5244. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  5245. // if ((chargingInfo[_index]->EvBatterytargetVoltage * 10) > 0)
  5246. // {
  5247. // chargingInfo[_index]->ChargingProfileCurrent = chargingInfo[_index]->ChargingProfilePower / (chargingInfo[_index]->EvBatterytargetVoltage * 10);
  5248. // }
  5249. }
  5250. else
  5251. {
  5252. chargingInfo[_index]->ChargingProfilePower = -1;
  5253. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5254. }
  5255. PRINTF_FUNC("ChargingProfilePower = %f \n", chargingInfo[_index]->ChargingProfilePower);
  5256. PRINTF_FUNC("ChargingProfileCurrent = %f \n", chargingInfo[_index]->ChargingProfileCurrent);
  5257. }
  5258. //
  5259. // printf("-------------Schedule------------\n");
  5260. // printf("index = %d \n", _index);
  5261. // printf("StartSchedule = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  5262. // printf("ChargingRateUnit = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingRateUnit);
  5263. // printf("----------SchedulePeriod---------\n");
  5264. // for (int v = 0; v < 10; v++)
  5265. // {
  5266. // printf("StartPeriod = %d \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].StartPeriod);
  5267. // printf("Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].Limit);
  5268. // }
  5269. // printf("---------------------------------\n");
  5270. }
  5271. }
  5272. void CheckReturnToChargingConn()
  5273. {
  5274. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  5275. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  5276. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  5277. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  5278. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  5279. {
  5280. bool isReturnTimeout = false;
  5281. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  5282. {
  5283. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  5284. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  5285. {
  5286. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  5287. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  5288. {
  5289. isReturnTimeout = false;
  5290. break;
  5291. }
  5292. }
  5293. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  5294. {
  5295. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  5296. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  5297. {
  5298. isReturnTimeout = true;
  5299. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  5300. }
  5301. }
  5302. }
  5303. // AC 槍
  5304. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  5305. {
  5306. // 沒有選中 AC,且 AC 在充電中
  5307. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  5308. (ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE))
  5309. {
  5310. // 當前 DC 充電槍在 idle 狀態
  5311. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE ||
  5312. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_RESERVATION)
  5313. {
  5314. isReturnTimeout = true;
  5315. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  5316. }
  5317. }
  5318. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  5319. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_COMPLETE) ||
  5320. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_CCS_PRECHARGE_ST1)))
  5321. {
  5322. // 當前 DC 充電槍在 idle 狀態
  5323. if (ac_chargingInfo[0]->SystemStatus == S_IDLE ||
  5324. ac_chargingInfo[0]->SystemStatus == S_RESERVATION)
  5325. {
  5326. isReturnTimeout = true;
  5327. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  5328. }
  5329. }
  5330. }
  5331. if (!isReturnTimeout)
  5332. StopSystemTimeoutDet();
  5333. }
  5334. }
  5335. bool GetStartChargingByAlterMode(byte _gun)
  5336. {
  5337. bool result = true;
  5338. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  5339. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  5340. {
  5341. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  5342. {
  5343. if (_select != _gun)
  5344. {
  5345. if (chargingInfo[_select]->SystemStatus != S_IDLE &&
  5346. chargingInfo[_select]->SystemStatus != S_RESERVATION)
  5347. {
  5348. result = false;
  5349. break;
  5350. }
  5351. }
  5352. }
  5353. }
  5354. return result;
  5355. }
  5356. void TheEndCharging(byte gun_index)
  5357. {
  5358. ftime(&endChargingTime[gun_index]);
  5359. if (chargingInfo[gun_index]->PresentChargedDuration != 0)
  5360. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5361. chargingInfo[gun_index]->isRemoteStart = NO;
  5362. StopGunInfoTimeoutDet(gun_index);
  5363. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  5364. ChangeStartOrStopDateTime(NO, gun_index);
  5365. //DB_Insert_Record(localDb, gun_index);
  5366. }
  5367. void UpdateErrorCodeToOcpp(byte index)
  5368. {
  5369. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  5370. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  5371. {
  5372. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  5373. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)chargingInfo[index]->ConnectorAlarmCode);
  5374. }
  5375. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  5376. {
  5377. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  5378. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)chargingInfo[index]->EvConnAlarmCode);
  5379. }
  5380. }
  5381. void CheckMiscCommandRequirement(void)
  5382. {
  5383. BOOL NeedAnnouncement = FALSE;
  5384. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  5385. {
  5386. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5387. {
  5388. NeedAnnouncement = FALSE;
  5389. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  5390. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  5391. {
  5392. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest ||
  5393. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest ||
  5394. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest ||
  5395. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest ||
  5396. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest ||
  5397. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.BackendStatusRequest ||
  5398. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.EthernetStatusRequest ||
  5399. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.WiFiStatusRequest ||
  5400. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.TelcomModemStatusRequest)
  5401. {
  5402. NeedAnnouncement = TRUE;
  5403. }
  5404. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  5405. {
  5406. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[j].Parameter.bits.AvailabilityRequest ||
  5407. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[j].Parameter.bits.RemoteStartRequest ||
  5408. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[j].Parameter.bits.AccountBalanceRequest)
  5409. {
  5410. NeedAnnouncement = TRUE;
  5411. }
  5412. }
  5413. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  5414. {
  5415. if(NeedAnnouncement)
  5416. {
  5417. PRINTF_FUNC("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  5418. }
  5419. else
  5420. {
  5421. PRINTF_FUNC("********** Dispenser %d Misc Command Announced **********", i + 1);
  5422. }
  5423. }
  5424. }
  5425. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  5426. }
  5427. }
  5428. }
  5429. void CheckDispenserVersionUpdateRequirement(void)
  5430. {
  5431. }
  5432. void DispenserConnectionRetryHandler(void)
  5433. {
  5434. unsigned short _timeout = 0;
  5435. unsigned int _price = 0;
  5436. unsigned char _currency = 0;
  5437. PRINTF_FUNC("********** Kill ModuleEvComm Task & Retry To Excute ModuleEvComm **********");
  5438. _timeout = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  5439. _price = ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice;
  5440. _currency = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  5441. system("killall Module_EvComm");
  5442. usleep(100000);
  5443. system("/root/Module_EvComm &");
  5444. usleep(100000);
  5445. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout = _timeout;
  5446. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice = _price;
  5447. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency = _currency;
  5448. }
  5449. struct timeval _dispenserConnectionRetry_time;
  5450. void DispenserReconnectionProcess(void)
  5451. {
  5452. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity == 0)
  5453. {
  5454. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.DispenserDisconnection == 0)
  5455. {
  5456. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.DispenserDisconnection = 1;
  5457. gettimeofday(&_dispenserConnectionRetry_time, NULL);
  5458. }
  5459. if((GetTimeoutValue(_dispenserConnectionRetry_time) / uSEC_VAL) >= DISPENSER_RECONNECTE_TIME)
  5460. {
  5461. DispenserConnectionRetryHandler();
  5462. gettimeofday(&_dispenserConnectionRetry_time, NULL);
  5463. }
  5464. }
  5465. else
  5466. {
  5467. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.DispenserDisconnection = 0;
  5468. }
  5469. }
  5470. // connector: connector infex, 0 ~ 3
  5471. void SetLedIndicationStatus(unsigned char connector, unsigned char indication)
  5472. {
  5473. int greenOffset = 0, redOffset = 0;
  5474. if(ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication)
  5475. {
  5476. greenOffset = 2;
  5477. redOffset = 3;
  5478. }
  5479. if(ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication)
  5480. {
  5481. if(connector == 0)
  5482. {
  5483. greenOffset = 4;
  5484. redOffset = 5;
  5485. }
  5486. else if(connector == 1)
  5487. {
  5488. greenOffset = 2;
  5489. redOffset = 3;
  5490. }
  5491. else
  5492. {
  5493. return;
  5494. }
  5495. }
  5496. switch(indication)
  5497. {
  5498. case _LED_INDICATION_OFF:
  5499. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << greenOffset); // green off
  5500. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << redOffset); // red off
  5501. break;
  5502. case _LED_INDICATION_RUN:
  5503. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << greenOffset); // green on
  5504. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << redOffset); // red off
  5505. break;
  5506. case _LED_INDICATION_FAULT:
  5507. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << greenOffset); // green off
  5508. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << redOffset); // red on
  5509. break;
  5510. case _LED_INDICATION_ON:
  5511. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << greenOffset); // green on
  5512. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << redOffset); // red on
  5513. break;
  5514. case _LED_INDICATION_GREEN_TOGGLE:
  5515. if(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] & (0x01 << greenOffset))
  5516. {
  5517. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << greenOffset); // green off
  5518. }
  5519. else
  5520. {
  5521. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << greenOffset); // green on
  5522. }
  5523. break;
  5524. case _LED_INDICATION_RED_TOGGLE:
  5525. if(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] & (0x01 << redOffset))
  5526. {
  5527. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] &= ~(0x01 << redOffset); // red off
  5528. }
  5529. else
  5530. {
  5531. ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0] |= (0x01 << redOffset); // red on
  5532. }
  5533. break;
  5534. }
  5535. }
  5536. struct timeval _standardIndication_time;
  5537. unsigned char _standardLedStatus, _preStandardLedStatus;
  5538. unsigned char _e4youLedStatus[2], _preE4youLedStatus[2];
  5539. struct timeval _e4youIndication_time[2];
  5540. void LedIndicationProcess(void)
  5541. {
  5542. if(ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication)
  5543. {
  5544. if(ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5545. {
  5546. _standardLedStatus = _STANDARD_LED_Alarm;
  5547. if(_preStandardLedStatus != _standardLedStatus)
  5548. {
  5549. _preStandardLedStatus = _standardLedStatus;
  5550. }
  5551. SetLedIndicationStatus(0, _LED_INDICATION_FAULT);
  5552. }
  5553. else
  5554. {
  5555. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity == 0)
  5556. {
  5557. _standardLedStatus = _STANDARD_LED_Disconnection;
  5558. if(_preStandardLedStatus != _standardLedStatus)
  5559. {
  5560. _preStandardLedStatus = _standardLedStatus;
  5561. // reset led status & indication time
  5562. gettimeofday(&_standardIndication_time, NULL);
  5563. SetLedIndicationStatus(0, _LED_INDICATION_OFF);
  5564. }
  5565. if((GetTimeoutValue(_standardIndication_time) / mSEC_VAL) >= INDICATION_BLINK_INTERVAL)
  5566. {
  5567. SetLedIndicationStatus(0, _LED_INDICATION_GREEN_TOGGLE);
  5568. SetLedIndicationStatus(0, _LED_INDICATION_RED_TOGGLE);
  5569. gettimeofday(&_standardIndication_time, NULL);
  5570. }
  5571. }
  5572. else
  5573. {
  5574. _standardLedStatus = _STANDARD_LED_Running;
  5575. if(_preStandardLedStatus != _standardLedStatus)
  5576. {
  5577. _preStandardLedStatus = _standardLedStatus;
  5578. }
  5579. SetLedIndicationStatus(0, _LED_INDICATION_RUN);
  5580. }
  5581. }
  5582. }
  5583. if(ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication)
  5584. {
  5585. for(int i = 0; i < 2; i++)
  5586. {
  5587. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable == false)
  5588. {
  5589. _e4youLedStatus[i] = _E4YOU_LED_Disconnection;
  5590. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5591. {
  5592. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5593. // reset led status & indication time
  5594. gettimeofday(&_e4youIndication_time[i], NULL);
  5595. SetLedIndicationStatus(i, _LED_INDICATION_OFF);
  5596. }
  5597. if((GetTimeoutValue(_e4youIndication_time[i]) / mSEC_VAL) >= INDICATION_BLINK_INTERVAL)
  5598. {
  5599. gettimeofday(&_e4youIndication_time[i], NULL);
  5600. SetLedIndicationStatus(i, _LED_INDICATION_GREEN_TOGGLE);
  5601. SetLedIndicationStatus(i, _LED_INDICATION_RED_TOGGLE);
  5602. }
  5603. }
  5604. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_IDLE ||
  5605. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_MAINTAIN ||
  5606. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_RESERVATION)
  5607. {
  5608. _e4youLedStatus[i] = _E4YOU_LED_Idle;
  5609. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5610. {
  5611. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5612. }
  5613. SetLedIndicationStatus(i, _LED_INDICATION_RUN);
  5614. }
  5615. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  5616. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_COMPLETE)
  5617. {
  5618. _e4youLedStatus[i] = _E4YOU_LED_Charging;
  5619. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5620. {
  5621. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5622. // reset led status & indication time
  5623. gettimeofday(&_e4youIndication_time[i], NULL);
  5624. SetLedIndicationStatus(i, _LED_INDICATION_OFF);
  5625. }
  5626. if((GetTimeoutValue(_e4youIndication_time[i]) / mSEC_VAL) >= E4YOU_BLINK_INTERVAL)
  5627. {
  5628. gettimeofday(&_e4youIndication_time[i], NULL);
  5629. SetLedIndicationStatus(i, _LED_INDICATION_GREEN_TOGGLE);
  5630. }
  5631. }
  5632. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_FAULT ||
  5633. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus == S_ALARM)
  5634. {
  5635. _e4youLedStatus[i] = _E4YOU_LED_Alarm;
  5636. if(_preE4youLedStatus[i] != _e4youLedStatus[i])
  5637. {
  5638. _preE4youLedStatus[i] = _e4youLedStatus[i];
  5639. }
  5640. SetLedIndicationStatus(i, _LED_INDICATION_FAULT);
  5641. }
  5642. }
  5643. }
  5644. }
  5645. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  5646. {
  5647. unsigned char dispenser = 0;
  5648. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5649. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  5650. {
  5651. PRINTF_FUNC("Connector %d Disconnection Recovery", connector + 1);
  5652. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  5653. }
  5654. }
  5655. int main(void)
  5656. {
  5657. if(CreateShareMemory() == 0)
  5658. {
  5659. #ifdef SystemLogMessage
  5660. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  5661. #endif
  5662. if(ShmStatusCodeData!=NULL)
  5663. {
  5664. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  5665. }
  5666. sleep(5);
  5667. system("reboot -f");
  5668. sleep(5);
  5669. system("reboot -f");
  5670. }
  5671. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  5672. if (!InitialSystemDefaultConfig())
  5673. {
  5674. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  5675. StopProcessingLoop();
  5676. }
  5677. PRINTF_FUNC("SW Version = %s \n", fwVersion);
  5678. PRINTF_FUNC("Chademo = %d, CCS = %d, GB = %d, AC = %d, General: %d\n",
  5679. CHAdeMO_QUANTITY, CCS_QUANTITY, GB_QUANTITY, AC_QUANTITY, GENERAL_GUN_QUANTITY);
  5680. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  5681. // CheckGunTypeFromHw();
  5682. PRINTF_FUNC("CheckIsAlternatvie, ModelName = %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  5683. // CheckIsAlternatvieByModelName();
  5684. PRINTF_FUNC("InitialShareMemoryInfo \n");
  5685. InitialShareMemoryInfo();
  5686. PRINTF_FUNC("ChargerType (IEC or UL) = %d \n", ShmSysConfigAndInfo->SysInfo.ChargerType);
  5687. ChangeLcmByIndex(_LCM_INIT);
  5688. if (!CheckConnectorTypeStatus())
  5689. isModelNameMatch = false;
  5690. Initialization();
  5691. InitialPsuGroupingAndLocation();
  5692. InitialChargerSetting();
  5693. PRINTF_FUNC("Spawn all Task. \n");
  5694. SpawnTask();
  5695. if (!isModelNameMatch)
  5696. {
  5697. PRINTF_FUNC("Module Name & HW info none match. \n");
  5698. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  5699. ChangeLcmByIndex(_LCM_FIX);
  5700. // Module Name 與硬體對應不正確
  5701. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  5702. sleep(3);
  5703. KillAllTask();
  5704. StopProcessingLoop();
  5705. }
  5706. PRINTF_FUNC("Module Name & HW info correct. Initialize.......");
  5707. CreateTimeoutFork();
  5708. PRINTF_FUNC("Self test.");
  5709. if(ShmChargerInfo->Control.SysCtrl.bits.StandardLedIndication)
  5710. {
  5711. _standardLedStatus = _STANDARD_LED_None;
  5712. _preStandardLedStatus = _STANDARD_LED_None;
  5713. SetLedIndicationStatus(0, _LED_INDICATION_ON);
  5714. }
  5715. if(ShmChargerInfo->Control.SysCtrl.bits.E4YOULedIndication)
  5716. {
  5717. _e4youLedStatus[0] = _E4YOU_LED_None;
  5718. _e4youLedStatus[1] = _E4YOU_LED_None;
  5719. _preE4youLedStatus[0] = _E4YOU_LED_None;
  5720. _preE4youLedStatus[1] = _E4YOU_LED_None;
  5721. SetLedIndicationStatus(0, _LED_INDICATION_ON);
  5722. SetLedIndicationStatus(1, _LED_INDICATION_ON);
  5723. }
  5724. SelfTestRun();
  5725. // StopSystemTimeoutDet();
  5726. SetAcContactor(ON);
  5727. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  5728. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  5729. ShmPsuData->Work_Step == _NO_WORKING)
  5730. {
  5731. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5732. {
  5733. setChargerMode(gun_index, MODE_FAULT);
  5734. }
  5735. ChangeLcmByIndex(_LCM_FIX);
  5736. sleep(3);
  5737. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5738. {
  5739. KillTaskExceptPrimary();
  5740. }
  5741. else
  5742. {
  5743. KillAllTask();
  5744. }
  5745. StopProcessingLoop();
  5746. }
  5747. else
  5748. {
  5749. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5750. {
  5751. setChargerMode(gun_index, MODE_IDLE);
  5752. }
  5753. }
  5754. // Local DB
  5755. if(DB_Open(localDb) != PASS)
  5756. {
  5757. PRINTF_FUNC("DB_Open fail.");
  5758. isDb_ready = false;
  5759. }
  5760. else
  5761. {
  5762. isDb_ready = true;
  5763. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  5764. {
  5765. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  5766. }
  5767. }
  5768. ChangeLcmByIndex(_LCM_IDLE);
  5769. sleep(1);
  5770. // Main loop
  5771. PRINTF_FUNC("****************************Main Loop********************************** \n");
  5772. gettimeofday(&_cmdMainPriority_time, NULL);
  5773. for (;;)
  5774. {
  5775. CheckOcppStatus();
  5776. ChkPrimaryStatus();
  5777. ChkPsuStatus();
  5778. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  5779. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  5780. {
  5781. CheckFactoryConfigFunction();
  5782. CheckFwUpdateFunction();
  5783. }
  5784. StandbyCheck();
  5785. // OCPP 邏輯
  5786. //OcppRemoteStartChk();
  5787. // 讀卡邏輯
  5788. //ScannerCardProcess();
  5789. PowerCabinetAuthorizeProcess();
  5790. // 當 AC 沒有搭上時,清除一些錯誤碼
  5791. ClearAlarmCodeWhenAcOff();
  5792. // 確認是否要回到充電中的槍畫面邏輯判斷
  5793. CheckReturnToChargingConn();
  5794. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000)
  5795. {
  5796. CheckTask();
  5797. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5798. {
  5799. if (chargingInfo[_index]->SystemStatus == S_CHARGING)
  5800. CheckSmartChargeProfile(_index);
  5801. }
  5802. gettimeofday(&_cmdMainPriority_time, NULL);
  5803. }
  5804. //DispenserReconnectionProcess();
  5805. LedIndicationProcess();
  5806. CheckMiscCommandRequirement();
  5807. CheckDispenserVersionUpdateRequirement();
  5808. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5809. {
  5810. //CheckGpioInStatus();
  5811. CheckErrorOccurStatus(gun_index);
  5812. ChkOcppStatus(gun_index);
  5813. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  5814. switch(chargingInfo[gun_index]->SystemStatus)
  5815. {
  5816. case S_IDLE:
  5817. case S_RESERVATION:
  5818. case S_MAINTAIN:
  5819. case S_FAULT:
  5820. {
  5821. // clean PermissionRequest flag
  5822. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  5823. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  5824. if(isModeChange(gun_index))
  5825. {
  5826. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  5827. {
  5828. PRINTF_FUNC("================== S_IDLE (%x) ================== \n", gun_index);
  5829. chargingInfo[gun_index]->PresentChargedDuration = 0;
  5830. chargingInfo[gun_index]->RemainChargingDuration = 0;
  5831. chargingTime[gun_index] = 0;
  5832. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  5833. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  5834. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5835. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  5836. ReleaseAlarmCode(gun_index);
  5837. // clean connector infomation
  5838. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  5839. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  5840. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  5841. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  5842. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  5843. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  5844. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -6553500;
  5845. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  5846. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  5847. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  5848. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  5849. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  5850. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  5851. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  5852. chargingInfo[gun_index]->EvBatterySoc = 0;
  5853. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  5854. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  5855. // clean RemoteStart & RemoteStop flag
  5856. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartRequest = false;
  5857. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  5858. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopRequest = false;
  5859. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  5860. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopRequest = false;
  5861. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  5862. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  5863. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  5864. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  5865. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  5866. }
  5867. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  5868. {
  5869. PRINTF_FUNC("================== S_RESERVATION (%x) ================== \n", gun_index);
  5870. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  5871. }
  5872. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  5873. {
  5874. PRINTF_FUNC("================== S_FAULT (%x) ================== \n", gun_index);
  5875. }
  5876. }
  5877. if (chargingInfo[gun_index]->IsAvailable == NO)
  5878. {
  5879. setChargerMode(gun_index, MODE_MAINTAIN);
  5880. }
  5881. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  5882. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  5883. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  5884. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  5885. {
  5886. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  5887. {
  5888. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  5889. {
  5890. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  5891. }
  5892. }
  5893. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  5894. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  5895. {
  5896. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  5897. {
  5898. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012304", 6);
  5899. PRINTF_FUNC("Connector %d Disconnect Occur", gun_index + 1);
  5900. }
  5901. }
  5902. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  5903. {
  5904. ClearDetectPluginFlag();
  5905. UpdateErrorCodeToOcpp(gun_index);
  5906. setChargerMode(gun_index, MODE_FAULT);
  5907. }
  5908. else
  5909. {
  5910. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  5911. {
  5912. CheckConnectorDisconnectionRecovery(gun_index);
  5913. }
  5914. }
  5915. }
  5916. else
  5917. {
  5918. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  5919. {
  5920. ReleaseAlarmCode(gun_index);
  5921. setChargerMode(gun_index, MODE_IDLE);
  5922. }
  5923. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  5924. {
  5925. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  5926. {
  5927. // 均充 -> 最大充
  5928. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  5929. {
  5930. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  5931. {
  5932. PRINTF_FUNC("=============Smart Charging============= Step 11 \n");
  5933. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  5934. }
  5935. }
  5936. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5937. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  5938. {
  5939. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  5940. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5941. }
  5942. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  5943. {
  5944. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5945. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5946. }
  5947. }
  5948. else
  5949. {
  5950. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5951. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5952. }
  5953. }
  5954. else
  5955. {
  5956. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5957. }
  5958. // clean stop charge flag
  5959. chargingInfo[gun_index]->StopChargeFlag = false;
  5960. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  5961. { // Idle 正常程序起點
  5962. // 判斷是否有啟用檢查插槍
  5963. if(IsConnectorAuthorizeSuccess(gun_index))
  5964. {
  5965. if(chargingInfo[gun_index]->IsAvailable &&
  5966. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  5967. {
  5968. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RemoteStart)
  5969. {
  5970. PRINTF_FUNC("Connector %d remote start charging", gun_index + 1);
  5971. }
  5972. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RFID)
  5973. {
  5974. PRINTF_FUNC("Connector %d swipe rfid start charging", gun_index + 1);
  5975. }
  5976. setChargerMode(gun_index, MODE_AUTHORIZING);
  5977. continue;
  5978. }
  5979. }
  5980. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  5981. {
  5982. if(chargingInfo[gun_index]->IsAvailable &&
  5983. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  5984. chargingInfo[gun_index]->ConnectorPlugIn &&
  5985. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  5986. {
  5987. ChangeGunSelectByIndex(gun_index);
  5988. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex].UserId);
  5989. PRINTF_FUNC("Auto Gun Selection %d, CardNumber = %s", gun_index, chargingInfo[gun_index]->StartUserId);
  5990. DispenserAuthorizationInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  5991. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5992. continue;
  5993. }
  5994. }
  5995. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  5996. {
  5997. if (!isReservationExpired(gun_index))
  5998. {
  5999. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  6000. }
  6001. }
  6002. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  6003. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  6004. {
  6005. PRINTF_FUNC("-----------------3----------------- \n");
  6006. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  6007. if (isCanStartChargingFlag)
  6008. {
  6009. ChangeGunSelectByIndex(gun_index);
  6010. AddPlugInTimes(gun_index);
  6011. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6012. ClearDetectPluginFlag();
  6013. continue;
  6014. }
  6015. }
  6016. else
  6017. {
  6018. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6019. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  6020. }
  6021. } // Idle 正常程序終點
  6022. }
  6023. }
  6024. break;
  6025. case S_AUTHORIZING:
  6026. if(isModeChange(gun_index))
  6027. {
  6028. PRINTF_FUNC("================== S_AUTHORIZING (%x) ================== \n", gun_index);
  6029. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  6030. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  6031. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  6032. StopSystemTimeoutDet();
  6033. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  6034. gettimeofday(&_ConnectorAuthorizing_Time[gun_index], NULL);
  6035. }
  6036. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  6037. {
  6038. FouceAnnounceAccountBalance(gun_index);
  6039. }
  6040. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RemoteStart)
  6041. {
  6042. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  6043. {
  6044. // sync with dispenser
  6045. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  6046. gettimeofday(&_ConnectorAuthorizing_Time[gun_index], NULL);
  6047. PRINTF_FUNC("Sync with Connector %d through remote start", gun_index + 1);
  6048. FouceAnnounceAccountBalance(gun_index);
  6049. }
  6050. }
  6051. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthorizingType == _AuthType_RFID)
  6052. {
  6053. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  6054. {
  6055. // sync with dispenser
  6056. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  6057. gettimeofday(&_ConnectorAuthorizing_Time[gun_index], NULL);
  6058. PRINTF_FUNC("Sync with Connector %d through swipe rfid", gun_index + 1);
  6059. }
  6060. }
  6061. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  6062. {
  6063. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  6064. {
  6065. PRINTF_FUNC("*********** Connector %d Plug In Timeout ***********", gun_index + 1);
  6066. }
  6067. else
  6068. {
  6069. PRINTF_FUNC("*********** Connector %d Preparing State Timeout ***********", gun_index + 1);
  6070. }
  6071. setChargerMode(gun_index, MODE_IDLE);
  6072. ConnectorAuthorizationInitial(gun_index);
  6073. }
  6074. else
  6075. {
  6076. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  6077. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  6078. {
  6079. AddPlugInTimes(gun_index);
  6080. ChangeGunSelectByIndex(gun_index);
  6081. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  6082. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  6083. ConnectorAuthorizationInitial(gun_index);
  6084. }
  6085. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  6086. {
  6087. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  6088. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  6089. PRINTF_FUNC("*********** Connector id %d User Stop Plug In ***********\n", gun_index + 1);
  6090. setChargerMode(gun_index, MODE_IDLE);
  6091. ConnectorAuthorizationInitial(gun_index);
  6092. }
  6093. }
  6094. break;
  6095. case S_REASSIGN_CHECK:
  6096. {
  6097. if (isModeChange(gun_index))
  6098. {
  6099. PRINTF_FUNC("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index);
  6100. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  6101. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  6102. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  6103. StopSystemTimeoutDet();
  6104. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  6105. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  6106. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  6107. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  6108. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  6109. PRINTF_FUNC("Max Total Current %d, Max Total Power %d, Total Energy %d, Total Duration %d",
  6110. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent,
  6111. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower,
  6112. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  6113. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  6114. SetAcContactor(ON);
  6115. }
  6116. bool isRessign = false;
  6117. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  6118. {
  6119. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  6120. {
  6121. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  6122. {
  6123. // 有其他槍已經分配好 psu 模塊
  6124. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  6125. chargingInfo[index]->SystemStatus >= S_PREPARNING &&
  6126. chargingInfo[index]->SystemStatus != S_MAINTAIN)
  6127. {
  6128. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  6129. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  6130. isRessign = true;
  6131. break;
  6132. }
  6133. }
  6134. }
  6135. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  6136. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  6137. {
  6138. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  6139. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  6140. {
  6141. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  6142. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  6143. {
  6144. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  6145. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  6146. }
  6147. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  6148. {
  6149. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  6150. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  6151. continue;
  6152. }
  6153. }
  6154. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6155. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  6156. continue;
  6157. }
  6158. }
  6159. if (isRessign)
  6160. setChargerMode(gun_index, MODE_REASSIGN);
  6161. else
  6162. setChargerMode(gun_index, MODE_PRECHARGE);
  6163. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6164. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  6165. }
  6166. break;
  6167. case S_REASSIGN:
  6168. {
  6169. if (isModeChange(gun_index))
  6170. {
  6171. PRINTF_FUNC("================== S_REASSIGN (%x) ================== \n", gun_index);
  6172. gettimeofday(&_toAverage_time, NULL);
  6173. }
  6174. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  6175. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  6176. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  6177. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  6178. {
  6179. if (ShmSysConfigAndInfo->SysInfo.CanAverageCharging)
  6180. setChargerMode(gun_index, MODE_PRECHARGE);
  6181. else
  6182. setChargerMode(gun_index, MODE_IDLE);
  6183. }
  6184. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  6185. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  6186. {
  6187. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  6188. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  6189. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  6190. setChargerMode(gun_index, MODE_PRECHARGE);
  6191. }
  6192. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  6193. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6194. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  6195. }
  6196. break;
  6197. case S_PREPARNING:
  6198. {
  6199. if (isModeChange(gun_index))
  6200. {
  6201. PRINTF_FUNC("================== S_PREPARNING (%x) ================== \n", gun_index);
  6202. StopGunInfoTimeoutDet(gun_index);
  6203. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  6204. SetAcContactor(ON);
  6205. }
  6206. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10 &&
  6207. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 5000000)
  6208. {
  6209. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  6210. }
  6211. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6212. {
  6213. // 板端或後臺要求停止
  6214. PRINTF_FUNC("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index);
  6215. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6216. {
  6217. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6218. }
  6219. ChargingAlarmProcess(gun_index);
  6220. }
  6221. else if(isNormalStopChargeFlag(gun_index) == YES)
  6222. {
  6223. PRINTF_FUNC("********** Gun %d Normal Stop (S_PREPARNING) **********", gun_index);
  6224. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6225. {
  6226. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6227. }
  6228. ChargingTerminalProcess(gun_index);
  6229. }
  6230. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6231. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  6232. }
  6233. break;
  6234. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  6235. {
  6236. if (isModeChange(gun_index))
  6237. {
  6238. PRINTF_FUNC("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index);
  6239. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  6240. StopGunInfoTimeoutDet(gun_index);
  6241. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  6242. }
  6243. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  6244. {
  6245. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  6246. }
  6247. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6248. {
  6249. // 板端或後臺要求停止
  6250. PRINTF_FUNC("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index);
  6251. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6252. {
  6253. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6254. }
  6255. ChargingAlarmProcess(gun_index);
  6256. }
  6257. else if(isNormalStopChargeFlag(gun_index) == YES)
  6258. {
  6259. PRINTF_FUNC("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) **********", gun_index);
  6260. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6261. {
  6262. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6263. }
  6264. ChargingTerminalProcess(gun_index);
  6265. }
  6266. // LCM => Pre-charging
  6267. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6268. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  6269. }
  6270. break;
  6271. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  6272. {
  6273. if (isModeChange(gun_index))
  6274. {
  6275. PRINTF_FUNC("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index);
  6276. StopGunInfoTimeoutDet(gun_index);
  6277. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  6278. }
  6279. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  6280. {
  6281. setChargerMode(gun_index, MODE_CHARGING);
  6282. }
  6283. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6284. {
  6285. // 板端或後臺要求停止
  6286. PRINTF_FUNC("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index);
  6287. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6288. {
  6289. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6290. }
  6291. ChargingAlarmProcess(gun_index);
  6292. }
  6293. else if(isNormalStopChargeFlag(gun_index) == YES)
  6294. {
  6295. PRINTF_FUNC("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) **********", gun_index);
  6296. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6297. {
  6298. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6299. }
  6300. ChargingTerminalProcess(gun_index);
  6301. }
  6302. // LCM => Pre-charging
  6303. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6304. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  6305. }
  6306. break;
  6307. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  6308. {
  6309. if (isModeChange(gun_index))
  6310. {
  6311. PRINTF_FUNC("================== S_CHARGING (%x) ================== \n", gun_index);
  6312. StopGunInfoTimeoutDet(gun_index);
  6313. ftime(&startChargingTime[gun_index]);
  6314. ChangeStartOrStopDateTime(YES, gun_index);
  6315. OcppStartTransation(gun_index);
  6316. }
  6317. ftime(&endChargingTime[gun_index]);
  6318. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  6319. // 計算 Power
  6320. chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  6321. if (chargingTime[gun_index] == 0 || chargingTime[gun_index] > chargingInfo[gun_index]->PresentChargedDuration)
  6322. {
  6323. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  6324. }
  6325. else
  6326. {
  6327. int passTime = chargingInfo[gun_index]->PresentChargedDuration - chargingTime[gun_index];
  6328. if(passTime > 0)
  6329. {
  6330. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) * passTime / 3600;
  6331. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  6332. {
  6333. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  6334. }
  6335. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  6336. chargingInfo[gun_index]->PowerConsumption += changingPow;
  6337. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  6338. }
  6339. }
  6340. if (isEvBoardStopChargeFlag(gun_index))
  6341. {
  6342. PRINTF_FUNC("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index);
  6343. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6344. {
  6345. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6346. }
  6347. ChargingAlarmProcess(gun_index);
  6348. }
  6349. else if(isNormalStopChargeFlag(gun_index) == YES)
  6350. {
  6351. PRINTF_FUNC("********** Gun %d Normal Stop (S_CHARGING) **********", gun_index);
  6352. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6353. {
  6354. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6355. }
  6356. ChargingTerminalProcess(gun_index);
  6357. }
  6358. else if(CheckBackendChargingTimeout(gun_index) ||
  6359. CheckBackendChargingEnergy(gun_index) ||
  6360. (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf &&
  6361. strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL))
  6362. {
  6363. PRINTF_FUNC("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index);
  6364. // 板端或後臺要求停止
  6365. ChargingTerminalProcess(gun_index);
  6366. }
  6367. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf)
  6368. {
  6369. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = NO;
  6370. }
  6371. // LCM => Charging
  6372. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6373. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  6374. }
  6375. break;
  6376. case S_ALARM:
  6377. case S_TERMINATING:
  6378. {
  6379. if (isModeChange(gun_index))
  6380. {
  6381. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6382. {
  6383. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  6384. }
  6385. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  6386. {
  6387. PRINTF_FUNC("================== S_ALARM (%x) ================== \n", gun_index);
  6388. UpdateErrorCodeToOcpp(gun_index);
  6389. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  6390. {
  6391. OcppStopTransation(gun_index);
  6392. }
  6393. TheEndCharging(gun_index);
  6394. }
  6395. else
  6396. {
  6397. PRINTF_FUNC("================== S_TERMINATING (%x) ================== \n", gun_index);
  6398. }
  6399. StopGunInfoTimeoutDet(gun_index);
  6400. }
  6401. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  6402. {
  6403. if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  6404. {
  6405. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6406. {
  6407. PRINTF_FUNC ("********** Gun %d psu is releasable **********\n", gun_index);
  6408. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  6409. }
  6410. }
  6411. if((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false) &&
  6412. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6413. {
  6414. setChargerMode(gun_index, MODE_IDLE);
  6415. }
  6416. }
  6417. else
  6418. {
  6419. setChargerMode(gun_index, MODE_COMPLETE);
  6420. }
  6421. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6422. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  6423. }
  6424. break;
  6425. case S_COMPLETE:
  6426. {
  6427. if (isModeChange(gun_index))
  6428. {
  6429. PRINTF_FUNC("================== S_COMPLETE (%x) ================== \n", gun_index);
  6430. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  6431. {
  6432. OcppStopTransation(gun_index);
  6433. }
  6434. TheEndCharging(gun_index);
  6435. }
  6436. if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  6437. {
  6438. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6439. {
  6440. PRINTF_FUNC ("********** Gun %d psu is releasable **********\n", gun_index);
  6441. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  6442. }
  6443. }
  6444. if((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false) &&
  6445. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6446. {
  6447. setChargerMode(gun_index, MODE_IDLE);
  6448. }
  6449. }
  6450. break;
  6451. case S_CCS_PRECHARGE_ST0:
  6452. if (isModeChange(gun_index))
  6453. {
  6454. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  6455. StopGunInfoTimeoutDet(gun_index);
  6456. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  6457. }
  6458. break;
  6459. case S_CCS_PRECHARGE_ST1:
  6460. if (isModeChange(gun_index))
  6461. {
  6462. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  6463. }
  6464. break;
  6465. }
  6466. }
  6467. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  6468. {
  6469. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  6470. }
  6471. else
  6472. {
  6473. bool dcPageRun = true;
  6474. if (dcPageRun)
  6475. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  6476. }
  6477. write(wtdFd, "a", 1);
  6478. usleep(whileLoopTime);
  6479. }
  6480. return FAIL;
  6481. }