mainNew.c 287 KB

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