mainOrg.c 300 KB

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