main.c 290 KB

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