main.c 189 KB

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