ReMain.c 271 KB

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