main.c 202 KB

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