main.c 288 KB

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