main.c 199 KB

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