main.c 186 KB

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