main.c 335 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include <ifaddrs.h>
  27. #include <math.h>
  28. #include <stdbool.h>
  29. #include <dirent.h>
  30. #include "../../define.h"
  31. #include "CheckSystemTask.h"
  32. #include "Config.h"
  33. #include "timeout.h"
  34. #define DEBUG_ALSTON 0
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL -1
  38. #define MODELNAME_FAIL 0
  39. #define BUFFER_SIZE 128
  40. #define YES 1
  41. #define NO 0
  42. #define NORMAL 0
  43. #define ABNORMAL 1
  44. #define EQUAL 0
  45. #define BTN_RELEASE 0
  46. #define BTN_PRESS 1
  47. #define MAX_BUF 64
  48. //#define MtdBlockSize 0x600000
  49. #define MtdBlockSize 0x300000
  50. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  51. #define UPGRADE_FAN 0x02
  52. #define UPGRADE_RB 0x03
  53. #define UPGRADE_PRI 0x04
  54. #define UPGRADE_AC 0x05
  55. #define UPGRADE_LED 0x06
  56. #define SYSTEM_MIN_VOL 80
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  63. #define NETWORK_DB_FILE "/Storage/EventLog/Eventlog.db"
  64. #define GUN_OTP_VALUE 150
  65. #define GUN_OTP_RECOVERY 140
  66. #define UNDEFINED_TEMP 255
  67. #define uSEC_VAL 1000000
  68. #define SELFTEST_TIMEOUT 60
  69. #define AUTHORIZE_TIMEOUT 15
  70. #define AUTHORIZE_COMP_TIMEOUT 3
  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 3
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. #define TIMEOUT_SPEC_HANDSHAKING 180000
  80. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  81. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  82. int whileLoopTime = 10000; // 10 ms
  83. int wtdFd = -1;
  84. byte _authorizeIndex = NO_DEFINE;
  85. bool IsAuthorizingMode();
  86. void ClearAuthorizedFlag();
  87. bool isDetectPlugin();
  88. void ClearDetectPluginFlag();
  89. int mystrcmp(unsigned char *p1, unsigned char *p2);
  90. void CheckTask();
  91. long long DiffTimebWithNow(struct timeb ST);
  92. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  93. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  94. void ChargingTerminalProcess(byte gunIndex);
  95. void ChkPrimaryStatus();
  96. void StartSystemTimeoutDet(unsigned char flag);
  97. void StopSystemTimeoutDet();
  98. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  99. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  100. int StoreLogMsg_1(const char *fmt, ...);
  101. int GetTimeoutValue(struct timespec *startTime);
  102. void gpio_set_value(unsigned int gpio, unsigned int value);
  103. void PRINTF_FUNC(char *string, ...);
  104. void ChangeGunSelectByIndex(byte sel);
  105. void ChargingAlarmProcess(byte gunIndex);
  106. void CheckSystemErrorFunction(byte index);
  107. int opcc_chk_reserve_expired(byte gunIndex);
  108. void ocpp_process_start();
  109. void ocpp_auto_response_BootNotification();
  110. void ocpp_auto_response_StartTransationConf(byte gunIndex);
  111. bool ocpp_chk_authrization_cmd();
  112. void ocpp_chk_reset_cmd(byte isforce);
  113. void ocpp_chk_reserved_cmd(byte gunIndex);
  114. void ocpp_chk_availability_cmd(byte gunIndex);
  115. void ocpp_chk_unlock_cmd(byte gunIndex);
  116. bool ocpp_chk_remoteStop_cmd(byte gunIndex);
  117. void ocpp_chk_remoteStart_cmd();
  118. void ocpp_chk_update_cmd();
  119. bool ocpp_chk_invalid_id_cmd(byte gunIndex);
  120. void ocpp_set_startTransation_cmd(byte gunIndex);
  121. void ocpp_set_stopTransation_cmd(byte gunIndex);
  122. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString);
  123. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason);
  124. void ocpp_set_authorizeReq_cmd(byte value);
  125. void ocpp_set_authorizeConf_cmd(byte value);
  126. void ocpp_set_errCode_cmd(byte gunIndex);
  127. void ocpp_clear_errorCode_cmd(byte gunIndex);
  128. void ocpp_clear_idTag_cmd(byte gun_index);
  129. void ocpp_clear_stopReason(byte gun_index);
  130. bool ocpp_get_authorize_conf();
  131. unsigned short _ocpp_get_connect_timeout();
  132. void ocpp_chargingProfile_process(byte gun_index);
  133. void RecordAlarmCode(byte gunIndex, char *code);
  134. void ReleaseSysAlarmCode(byte gunIndex);
  135. void ReviewCriticalAlarm();
  136. int DB_Open(sqlite3 *db);
  137. int DB_Insert_Record(sqlite3 *db, int gun_index);
  138. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  139. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  140. void InitialDHCP();
  141. int GetStartScheduleTime(unsigned char *time);
  142. void KillAllTask();
  143. void CheckSystemTaskAlive();
  144. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  145. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  146. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  147. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  148. struct StatusCodeData *ShmStatusCodeData;
  149. struct PsuData *ShmPsuData;
  150. struct CHAdeMOData *ShmCHAdeMOData;
  151. struct GBTData *ShmGBTData;
  152. struct CcsData *ShmCcsData;
  153. struct PrimaryMcuData *ShmPrimaryMcuData;
  154. struct FanModuleData *ShmFanModuleData;
  155. struct RelayModuleData *ShmRelayModuleData;
  156. struct LedModuleData *ShmLedModuleData;
  157. struct OCPP16Data *ShmOCPP16Data;
  158. struct OCPP20Data *ShmOCPP20Data;
  159. struct MeterInformation *ShmCsuMeterData;
  160. struct DcCommonInformation *ShmDcCommonData;
  161. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  162. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  163. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  164. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  165. int _presentChargingTimeBuf[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  166. struct timespec _startTransation_time[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  167. float gunOutputVol [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  168. float _ChargingProfilePwBuffer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  169. float _AcChargingProfileCurBuffer;
  170. bool _isSendStartTransaction[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  171. GunErr errCollect;
  172. struct timespec _autoRuntestTime;
  173. byte _isTestRun = NO;
  174. // for initial index to check EV board type is correct
  175. byte _gunIndex = 0;
  176. byte _acgunIndex = 0;
  177. byte _chademoIndex = 0;
  178. byte _ccsIndex = 0;
  179. byte _gb_Index = 0;
  180. byte _ac_Index = 0;
  181. byte bd0_1_status = 0;
  182. byte bd0_2_status = 0;
  183. byte bd1_1_status = 0;
  184. byte bd1_2_status = 0;
  185. bool isCardScan = false;
  186. bool isModelNameMatch = true;
  187. int rfidFd = -1;
  188. char* rfidPortName = "/dev/ttyS2";
  189. char* fwVersion = "V2.05.00.0000.00";
  190. sqlite3 *localDb;
  191. bool isDb_ready;
  192. sqlite3 *networkDb;
  193. char MyDefaultPriceString[128];
  194. byte reset4gStep = RESET_4G_STEP_NONE;
  195. //================================================
  196. // initial can-bus
  197. //================================================
  198. int InitCanBus()
  199. {
  200. int s0,nbytes;
  201. struct timeval tv;
  202. struct ifreq ifr0;
  203. struct sockaddr_can addr0;
  204. system("/sbin/ip link set can0 down");
  205. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  206. system("/sbin/ip link set can0 up");
  207. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  208. tv.tv_sec = 0;
  209. tv.tv_usec = 10000;
  210. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  211. {
  212. #ifdef SystemLogMessage
  213. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  214. #endif
  215. }
  216. nbytes=40960;
  217. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  218. {
  219. #ifdef SystemLogMessage
  220. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  221. #endif
  222. }
  223. nbytes=40960;
  224. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  225. {
  226. #ifdef SystemLogMessage
  227. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  228. #endif
  229. }
  230. strcpy(ifr0.ifr_name, "can0" );
  231. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  232. addr0.can_family = AF_CAN;
  233. addr0.can_ifindex = ifr0.ifr_ifindex;
  234. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  235. return s0;
  236. }
  237. //================================================
  238. // initial uart port
  239. //================================================
  240. char *_priPortName = "/dev/ttyS1";
  241. char *_485PortName = "/dev/ttyS5";
  242. int InitComPort(byte target)
  243. {
  244. int fd;
  245. struct termios tios;
  246. if(target == UPGRADE_PRI)
  247. fd = open(_priPortName, O_RDWR);
  248. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  249. fd = open(_485PortName, O_RDWR);
  250. if(fd<=0)
  251. {
  252. #ifdef SystemLogMessage
  253. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  254. #endif
  255. return -1;
  256. }
  257. ioctl (fd, TCGETS, &tios);
  258. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  259. tios.c_lflag = 0;
  260. tios.c_iflag = 0;
  261. tios.c_oflag = 0;
  262. tios.c_cc[VMIN]=0;
  263. tios.c_cc[VTIME]=(unsigned char)5;
  264. tios.c_lflag=0;
  265. tcflush(fd, TCIFLUSH);
  266. ioctl (fd, TCSETS, &tios);
  267. return fd;
  268. }
  269. //=================================
  270. // Common routine
  271. //=================================
  272. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  273. {
  274. strncpy(dest, src + start, cnt);
  275. dest[cnt] = 0;
  276. }
  277. int InitWatchDog()
  278. {
  279. int fd;
  280. int timeout = 180;
  281. system("/usr/bin/fuser -k /dev/watchdog");
  282. sleep(1);
  283. system("echo V > /dev/watchdog");
  284. sleep(1);
  285. fd = open("/dev/watchdog", O_RDWR);
  286. if(fd <= 0)
  287. {
  288. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  289. }
  290. ioctl(fd, _IOWR('W', 6, int), &timeout);
  291. return fd;
  292. }
  293. void CloseWatchDog()
  294. {
  295. if (wtdFd > 0)
  296. {
  297. close(wtdFd);
  298. sleep(1);
  299. system("/usr/bin/fuser -k /dev/watchdog");
  300. sleep(1);
  301. system("echo V > /dev/watchdog");
  302. sleep(1);
  303. }
  304. }
  305. int StoreLogMsg_1(const char *fmt, ...)
  306. {
  307. char Buf[4096+256];
  308. char buffer[4096];
  309. va_list args;
  310. struct timeb SeqEndTime;
  311. struct tm *tm;
  312. va_start(args, fmt);
  313. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  314. va_end(args);
  315. memset(Buf,0,sizeof(Buf));
  316. ftime(&SeqEndTime);
  317. SeqEndTime.time = time(NULL);
  318. tm=localtime(&SeqEndTime.time);
  319. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES ||
  320. DEBUG_ALSTON)
  321. {
  322. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  323. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  324. printf("%s \n", Buf);
  325. }
  326. else
  327. {
  328. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s",
  329. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  330. buffer,
  331. tm->tm_year+1900,tm->tm_mon+1,
  332. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  333. system(Buf);
  334. }
  335. return rc;
  336. }
  337. //unsigned long GetTimeoutValue(struct timeval _sour_time)
  338. //{
  339. // struct timeval _end_time;
  340. // gettimeofday(&_end_time, NULL);
  341. //
  342. // return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  343. //}
  344. int GetTimeoutValue(struct timespec *startTime)
  345. {
  346. struct timespec endTime;
  347. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  348. return endTime.tv_sec - startTime->tv_sec;
  349. }
  350. void GetTimespecFunc(struct timespec *time)
  351. {
  352. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  353. }
  354. int mystrcmp(unsigned char *p1, unsigned char *p2)
  355. {
  356. while(*p1==*p2)
  357. {
  358. if(*p1=='\0' || *p2=='\0')
  359. break;
  360. p1++;
  361. p2++;
  362. }
  363. if(*p1=='\0' && *p2=='\0')
  364. return(PASS);
  365. else
  366. return(FAIL);
  367. }
  368. int DiffTimeb(struct timeb ST, struct timeb ET)
  369. {
  370. //return milli-second
  371. unsigned int StartTime,StopTime;
  372. StartTime=(unsigned int)ST.time;
  373. StopTime=(unsigned int)ET.time;
  374. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  375. return (StopTime-StartTime);
  376. }
  377. int DiffTimebWithNowSec(struct timeb ST)
  378. {
  379. //return milli-second
  380. struct timeb ET;
  381. unsigned int StartTime,StopTime;
  382. ftime(&ET);
  383. StartTime=(unsigned int)ST.time;
  384. StopTime=(unsigned int)ET.time;
  385. return (StopTime-StartTime);
  386. }
  387. int CheckTimeOut(uint8_t *start)
  388. {
  389. int result = YES;
  390. struct ParsingResult
  391. {
  392. int result;
  393. int scanedElement;
  394. int year;
  395. int month;
  396. int mday;
  397. int hour;
  398. int min;
  399. int sec;
  400. int tz_hour;
  401. int tz_min;
  402. float minSec;
  403. }parsingResult;
  404. struct tm tmStart;
  405. struct timeb tbStart;
  406. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  407. if(strstr((char*)start, ".") != NULL)
  408. {
  409. // Original data with mini second
  410. if(strstr((char*)start, "Z") != NULL)
  411. {
  412. // Original data with Z
  413. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  414. &parsingResult.year,
  415. &parsingResult.month,
  416. &parsingResult.mday,
  417. &parsingResult.hour,
  418. &parsingResult.min,
  419. &parsingResult.sec,
  420. &parsingResult.minSec);
  421. }
  422. else
  423. {
  424. // Original data without Z
  425. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  426. &parsingResult.year,
  427. &parsingResult.month,
  428. &parsingResult.mday,
  429. &parsingResult.hour,
  430. &parsingResult.min,
  431. &parsingResult.sec,
  432. &parsingResult.minSec,
  433. &parsingResult.tz_hour,
  434. &parsingResult.tz_min);
  435. }
  436. }
  437. else
  438. {
  439. // Original data without mini second
  440. if(strstr((char*)start, "Z") != NULL)
  441. {
  442. // Original data with Z
  443. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  444. &parsingResult.year,
  445. &parsingResult.month,
  446. &parsingResult.mday,
  447. &parsingResult.hour,
  448. &parsingResult.min,
  449. &parsingResult.sec);
  450. }
  451. else
  452. {
  453. // Original data without Z
  454. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  455. &parsingResult.year,
  456. &parsingResult.month,
  457. &parsingResult.mday,
  458. &parsingResult.hour,
  459. &parsingResult.min,
  460. &parsingResult.sec,
  461. &parsingResult.tz_hour,
  462. &parsingResult.tz_min);
  463. }
  464. }
  465. if(parsingResult.scanedElement >= 6)
  466. {
  467. tmStart.tm_year = parsingResult.year - 1900;
  468. tmStart.tm_mon = parsingResult.month - 1;
  469. tmStart.tm_mday = parsingResult.mday;
  470. tmStart.tm_hour = parsingResult.hour;
  471. tmStart.tm_min = parsingResult.min;
  472. tmStart.tm_sec = parsingResult.sec;
  473. tmStart.tm_gmtoff = 0;
  474. tbStart.time = mktime(&tmStart);
  475. tbStart.timezone = 0;
  476. tbStart.millitm = 0;
  477. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  478. if(DiffTimebWithNowSec(tbStart) <= 0)
  479. result = NO;
  480. else
  481. result = YES;
  482. }
  483. else
  484. {
  485. PRINTF_FUNC("Start date parsing error.\n");
  486. }
  487. return result;
  488. }
  489. void setChargerMode(byte gun_index, byte mode)
  490. {
  491. chargingInfo[gun_index]->SystemStatus = mode;
  492. }
  493. void PRINTF_FUNC(char *string, ...)
  494. {
  495. va_list args;
  496. char buffer[4096];
  497. va_start(args, string);
  498. vsnprintf(buffer, sizeof(buffer), string, args);
  499. va_end(args);
  500. DEBUG_INFO_MSG("%s ", buffer);
  501. }
  502. long long DiffTimebWithNow(struct timeb ST)
  503. {
  504. //return milli-second
  505. struct timeb ET;
  506. long long StartTime, StopTime;
  507. ftime(&ET);
  508. StartTime = (long long)ST.time;
  509. StopTime = (long long)ET.time;
  510. return ((StopTime - StartTime)*1000) + (ET.millitm - ST.millitm);
  511. }
  512. bool CheckBackendChargingTimeout(byte gunIndex)
  513. {
  514. bool result = false;
  515. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  516. {
  517. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  518. {
  519. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  520. result = true;
  521. }
  522. }
  523. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  524. {
  525. // 隨插即充電的要看 offline
  526. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  527. {
  528. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  529. result = true;
  530. }
  531. }
  532. return result;
  533. }
  534. bool CheckBackendChargingEnergy(byte gunIndex)
  535. {
  536. bool result = false;
  537. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  538. {
  539. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  540. {
  541. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  542. result = true;
  543. }
  544. }
  545. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  546. {
  547. // 隨插即充電的要看 offline
  548. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  549. {
  550. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  551. result = true;
  552. }
  553. }
  554. return result;
  555. }
  556. bool CheckOcppChargingTimeout(byte gunIndex)
  557. {
  558. bool result = false;
  559. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  560. {
  561. if (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration > 0)
  562. {
  563. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration * 60))
  564. {
  565. PRINTF_FUNC("CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  566. chargingInfo[gunIndex]->PresentChargedDuration);
  567. result = true;
  568. }
  569. }
  570. }
  571. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  572. {
  573. if (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration > 0)
  574. {
  575. if (chargingInfo [gunIndex]->PresentChargedDuration >= (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration * 60))
  576. {
  577. PRINTF_FUNC ( "CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  578. chargingInfo [gunIndex]->PresentChargedDuration );
  579. result = true;
  580. }
  581. }
  582. }
  583. return result;
  584. }
  585. bool CheckOcppChargingEnergy(byte gunIndex)
  586. {
  587. bool result = false;
  588. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  589. {
  590. if (ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy > 0)
  591. {
  592. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy)
  593. {
  594. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  595. chargingInfo[gunIndex]->PresentChargedEnergy );
  596. result = true;
  597. }
  598. }
  599. }
  600. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  601. {
  602. if (ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy > 0)
  603. {
  604. if (chargingInfo [gunIndex]->PresentChargedEnergy >= ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy)
  605. {
  606. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  607. chargingInfo [gunIndex]->PresentChargedEnergy );
  608. result = true;
  609. }
  610. }
  611. }
  612. return result;
  613. }
  614. bool CheckOcppChargingSOC(byte gunIndex)
  615. {
  616. bool result = false;
  617. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  618. {
  619. if (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc > 0)
  620. {
  621. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc))
  622. {
  623. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  624. chargingInfo [gunIndex]->PresentChargedDuration );
  625. result = true;
  626. }
  627. }
  628. }
  629. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  630. {
  631. if (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc > 0)
  632. {
  633. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc))
  634. {
  635. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  636. chargingInfo [gunIndex]->PresentChargedDuration );
  637. result = true;
  638. }
  639. }
  640. }
  641. return result;
  642. }
  643. bool IsGunUsing(byte dcGunIndex)
  644. {
  645. bool result = false;
  646. if((chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  647. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_ALARM) ||
  648. (chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  649. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  650. {
  651. result = true;
  652. }
  653. return result;
  654. }
  655. //==========================================
  656. // Log
  657. //==========================================
  658. void CheckFwSlotStatusLog()
  659. {
  660. if (bd0_1_status == 0 && bd0_2_status == 1)
  661. PRINTF_FUNC("Connector 1 : Chademo");
  662. else if (bd0_1_status == 1 && bd0_2_status == 0)
  663. PRINTF_FUNC("Connector 1 : CCS");
  664. else if (bd0_1_status == 1 && bd0_2_status == 1)
  665. PRINTF_FUNC("Connector 1 : GB");
  666. if (bd1_1_status == 0 && bd1_2_status == 1)
  667. PRINTF_FUNC("Connector 2 : Chademo");
  668. else if (bd1_1_status == 1 && bd1_2_status == 0)
  669. PRINTF_FUNC("Connector 2 : CCS");
  670. else if (bd1_1_status == 1 && bd1_2_status == 1)
  671. PRINTF_FUNC("Connector 2 : GB");
  672. }
  673. void CheckHwSlotStatusLog(byte index)
  674. {
  675. if (chargingInfo[index]->Type == _Type_Chademo)
  676. {
  677. PRINTF_FUNC("Hw check : Connector %d, Type : Chademo, Evboard_id = %d \n",
  678. index, chargingInfo[index]->Evboard_id);
  679. }
  680. else if (chargingInfo[index]->Type == _Type_CCS)
  681. {
  682. PRINTF_FUNC("Hw check : Connector %d, Type : CCS, Evboard_id = %d \n",
  683. index, chargingInfo[index]->Evboard_id);
  684. }
  685. else if (chargingInfo[index]->Type == _Type_GB)
  686. {
  687. PRINTF_FUNC("Hw check : Connector %d, Type : GB, Evboard_id = %d \n",
  688. index, chargingInfo[index]->Evboard_id);
  689. }
  690. }
  691. //==========================================
  692. // Check interface status
  693. //==========================================
  694. int isInterfaceUp(const char *interface)
  695. {
  696. int result = FAIL;
  697. FILE *fp;
  698. char cmd[256];
  699. char buf[512];
  700. strcpy(cmd, "ifconfig");
  701. fp = popen(cmd, "r");
  702. if(fp != NULL)
  703. {
  704. while(fgets(buf, sizeof(buf), fp) != NULL)
  705. {
  706. if(strstr(buf, interface) > 0)
  707. {
  708. result = PASS;
  709. }
  710. }
  711. }
  712. pclose(fp);
  713. return result;
  714. }
  715. //=================================
  716. // Create all share memory
  717. //=================================
  718. int CreateShareMemory()
  719. {
  720. int MeterSMId;
  721. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  722. {
  723. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  724. return 0;
  725. }
  726. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  727. {
  728. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  729. return 0;
  730. }
  731. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  732. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  733. {
  734. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  735. return 0;
  736. }
  737. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  738. {
  739. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  740. return 0;
  741. }
  742. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  743. //creat ShmPsuData
  744. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  745. {
  746. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  747. return 0;
  748. }
  749. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  750. {
  751. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  752. return 0;
  753. }
  754. memset(ShmPsuData, 0, sizeof(struct PsuData));
  755. if(CHAdeMO_QUANTITY > 0)
  756. {
  757. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  758. {
  759. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  760. return 0;
  761. }
  762. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  763. {
  764. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  765. return 0;
  766. }
  767. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  768. }
  769. if(GB_QUANTITY > 0)
  770. {
  771. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  772. {
  773. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  774. return 0;
  775. }
  776. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  777. {
  778. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  779. return 0;
  780. }
  781. memset(ShmGBTData, 0, sizeof(struct GBTData));
  782. }
  783. //creat ShmCcsData
  784. if(CCS_QUANTITY > 0)
  785. {
  786. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  787. {
  788. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  789. return 0;
  790. }
  791. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  792. {
  793. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  794. return 0;
  795. }
  796. memset(ShmCcsData, 0, sizeof(struct CcsData));
  797. }
  798. //creat ShmPrimaryMcuData
  799. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  800. {
  801. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  802. return 0;
  803. }
  804. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  805. {
  806. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  807. return 0;
  808. }
  809. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  810. //creat ShmFanModuleData
  811. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  812. {
  813. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  814. return 0;
  815. }
  816. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  817. {
  818. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  819. return 0;
  820. }
  821. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  822. //creat ShmRelayModuleData
  823. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  824. {
  825. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  826. return 0;
  827. }
  828. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  829. {
  830. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  831. return 0;
  832. }
  833. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  834. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  835. {
  836. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  837. return 0;
  838. }
  839. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  840. {
  841. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  842. return 0;
  843. }
  844. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  845. //creat ShmOCPP16Data
  846. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  847. {
  848. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  849. return 0;
  850. }
  851. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  852. {
  853. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  854. return 0;
  855. }
  856. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  857. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  858. {
  859. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  860. return 0;
  861. }
  862. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  863. {
  864. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  865. return 0;
  866. }
  867. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  868. if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
  869. {
  870. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCsuMeterKey NG \n");
  871. return 0;
  872. }
  873. else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  874. {
  875. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCsuMeterData NG \n");
  876. return 0;
  877. }
  878. memset(ShmCsuMeterData, 0, sizeof(struct MeterInformation));
  879. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  880. {
  881. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCommonKey NG \n");
  882. return 0;
  883. }
  884. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  885. {
  886. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCommonKey NG \n");
  887. return 0;
  888. }
  889. byte buf = 0;
  890. if (ShmDcCommonData->rebootCount == 1)
  891. buf = ShmDcCommonData->rebootCount;
  892. memset(ShmDcCommonData, 0, sizeof(struct DcCommonInformation));
  893. ShmDcCommonData->rebootCount = buf;
  894. return 1;
  895. }
  896. void SetupRebootCount(byte value)
  897. {
  898. int MeterSMId;
  899. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  900. {
  901. DEBUG_ERROR_MSG("[main]RecordRebootCount:shmget ShmCommonKey NG \n");
  902. }
  903. else
  904. {
  905. ShmDcCommonData->rebootCount = value;
  906. shmctl(MeterSMId, IPC_SET, 0);
  907. }
  908. }
  909. //=================================
  910. // LCM Page
  911. //=================================
  912. void ChangeLcmByIndex(byte page_index)
  913. {
  914. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL ||
  915. page_index == _LCM_COMPLETE || page_index == _LCM_FIX || page_index == _LCM_EMC)
  916. {
  917. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0 &&
  918. (page_index == _LCM_FIX || page_index == _LCM_EMC))
  919. {
  920. bool isUpdate = false;
  921. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  922. {
  923. if (chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION &&
  924. chargingInfo[_index]->SystemStatus != SYS_MODE_MAINTAIN)
  925. {
  926. isUpdate = true;
  927. }
  928. }
  929. if (isUpdate &&
  930. ShmDcCommonData->LcmFwVersion > 0 &&
  931. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  932. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_EMC;
  933. else
  934. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_FIX;
  935. }
  936. else
  937. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  938. }
  939. }
  940. //======================================================
  941. // Peripheral initial
  942. //======================================================
  943. void InitGPIO()
  944. {
  945. /*****************0~3, 4 bank, bank x 32+ num*********************/
  946. /***************************************************************/
  947. /*************** GPIO 0 ***************************************/
  948. /***************************************************************/
  949. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  950. system("echo 22 > /sys/class/gpio/export");
  951. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  952. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  953. system("echo 23 > /sys/class/gpio/export");
  954. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  955. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  956. system("echo 26 > /sys/class/gpio/export");
  957. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  958. system("echo 1 > /sys/class/gpio/gpio26/value");
  959. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  960. system("echo 27 > /sys/class/gpio/export");
  961. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  962. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  963. system("echo 29 > /sys/class/gpio/export");
  964. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  965. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  966. system("echo 19 > /sys/class/gpio/export");
  967. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  968. system("echo 1 > /sys/class/gpio/gpio19/value");
  969. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  970. system("echo 20 > /sys/class/gpio/export");
  971. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  972. /***************************************************************/
  973. /*************** GPIO 1 ***************************************/
  974. /***************************************************************/
  975. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  976. system("echo 44 > /sys/class/gpio/export");
  977. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  978. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  979. system("echo 45 > /sys/class/gpio/export");
  980. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  981. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  982. system("echo 46 > /sys/class/gpio/export");
  983. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  984. system("echo 0 > /sys/class/gpio/gpio46/value");
  985. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  986. system("echo 47 > /sys/class/gpio/export");
  987. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  988. /***************************************************************/
  989. /*************** GPIO 2 ***************************************/
  990. /***************************************************************/
  991. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  992. system("echo 89 > /sys/class/gpio/export");
  993. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  994. system("echo 1 > /sys/class/gpio/gpio89/value");
  995. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  996. system("echo 87 > /sys/class/gpio/export");
  997. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  998. system("echo 0 > /sys/class/gpio/gpio87/value");
  999. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  1000. system("echo 88 > /sys/class/gpio/export");
  1001. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  1002. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  1003. system("echo 86 > /sys/class/gpio/export");
  1004. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  1005. /***************************************************************/
  1006. /*************** GPIO 3 ***************************************/
  1007. /***************************************************************/
  1008. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  1009. system("echo 111 > /sys/class/gpio/export");
  1010. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  1011. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  1012. system("echo 114 > /sys/class/gpio/export");
  1013. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  1014. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  1015. system("echo 113 > /sys/class/gpio/export");
  1016. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  1017. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  1018. system("echo 110 > /sys/class/gpio/export");
  1019. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1020. system("echo 0 > /sys/class/gpio/gpio110/value");
  1021. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  1022. system("echo 115 > /sys/class/gpio/export");
  1023. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1024. system("echo 0 > /sys/class/gpio/gpio115/value");
  1025. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  1026. system("echo 112 > /sys/class/gpio/export");
  1027. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  1028. system("echo 1 > /sys/class/gpio/gpio112/value");
  1029. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  1030. system("echo 116 > /sys/class/gpio/export");
  1031. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1032. system("echo 0 > /sys/class/gpio/gpio116/value");
  1033. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  1034. system("echo 103 > /sys/class/gpio/export");
  1035. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  1036. system("echo 0 > /sys/class/gpio/gpio103/value");
  1037. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  1038. system("echo 104 > /sys/class/gpio/export");
  1039. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  1040. system("echo 0 > /sys/class/gpio/gpio104/value");
  1041. #ifdef SystemLogMessage
  1042. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK \n");
  1043. #endif
  1044. }
  1045. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1046. {
  1047. int fd,wrd;
  1048. unsigned char *buf;
  1049. unsigned int ChkSum,ChkSumOrg;
  1050. if((buf=malloc(MtdBlockSize))==NULL)
  1051. {
  1052. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  1053. if(ShmStatusCodeData!=NULL)
  1054. {
  1055. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1056. }
  1057. sleep(5);
  1058. system("reboot -f");
  1059. sleep(5);
  1060. system("reboot -f");
  1061. }
  1062. memset(buf, 0, MtdBlockSize);
  1063. //================================================
  1064. // Load configuration from mtdblock10
  1065. //================================================
  1066. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1067. //fd = open("/dev/mtdblock10", O_RDWR);
  1068. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1069. if (fd < 0)
  1070. {
  1071. free(buf);
  1072. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  1073. if(ShmStatusCodeData!=NULL)
  1074. {
  1075. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1076. }
  1077. sleep(5);
  1078. system("reboot -f");
  1079. sleep(5);
  1080. system("reboot -f");
  1081. }
  1082. wrd=read(fd, buf, MtdBlockSize);
  1083. close(fd);
  1084. if(wrd<MtdBlockSize)
  1085. {
  1086. free(buf);
  1087. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  1088. if(ShmStatusCodeData!=NULL)
  1089. {
  1090. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1091. }
  1092. sleep(5);
  1093. system("reboot -f");
  1094. sleep(5);
  1095. system("reboot -f");
  1096. }
  1097. ChkSum=0;
  1098. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1099. // {
  1100. // ChkSum+=buf[wrd];
  1101. // }
  1102. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1103. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1104. {
  1105. ChkSum+=buf[wrd];
  1106. }
  1107. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1108. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1109. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1110. //================================================
  1111. // Load configuration from mtdblock11
  1112. //================================================
  1113. if(ChkSum!=ChkSumOrg)
  1114. {
  1115. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  1116. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1117. //fd = open("/dev/mtdblock11", O_RDWR);
  1118. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1119. if (fd < 0)
  1120. {
  1121. free(buf);
  1122. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  1123. if(ShmStatusCodeData!=NULL)
  1124. {
  1125. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1126. }
  1127. sleep(5);
  1128. system("reboot -f");
  1129. sleep(5);
  1130. system("reboot -f");
  1131. }
  1132. memset(buf, 0, MtdBlockSize);
  1133. wrd=read(fd, buf,MtdBlockSize);
  1134. close(fd);
  1135. if(wrd<MtdBlockSize)
  1136. {
  1137. free(buf);
  1138. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  1139. if(ShmStatusCodeData!=NULL)
  1140. {
  1141. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1142. }
  1143. sleep(5);
  1144. system("reboot -f");
  1145. sleep(5);
  1146. system("reboot -f");
  1147. }
  1148. ChkSum=0;
  1149. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1150. // {
  1151. // ChkSum+=buf[wrd];
  1152. // }
  1153. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1154. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1155. {
  1156. ChkSum+=buf[wrd];
  1157. }
  1158. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1159. //================================================
  1160. // Load configuration from mtdblock12 (Factory default)
  1161. //================================================
  1162. if(ChkSum!=ChkSumOrg)
  1163. {
  1164. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  1165. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1166. //fd = open("/dev/mtdblock12", O_RDWR);
  1167. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1168. if (fd < 0)
  1169. {
  1170. free(buf);
  1171. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  1172. if(ShmStatusCodeData!=NULL)
  1173. {
  1174. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1175. }
  1176. sleep(5);
  1177. system("reboot -f");
  1178. sleep(5);
  1179. system("reboot -f");
  1180. }
  1181. memset(buf, 0, MtdBlockSize);
  1182. wrd=read(fd, buf,MtdBlockSize);
  1183. close(fd);
  1184. if(wrd<MtdBlockSize)
  1185. {
  1186. free(buf);
  1187. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  1188. if(ShmStatusCodeData!=NULL)
  1189. {
  1190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1191. }
  1192. sleep(5);
  1193. system("reboot -f");
  1194. sleep(5);
  1195. system("reboot -f");
  1196. }
  1197. ChkSum=0;
  1198. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1199. // {
  1200. // ChkSum+=buf[wrd];
  1201. // }
  1202. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1203. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1204. {
  1205. ChkSum+=buf[wrd];
  1206. }
  1207. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1208. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1209. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1210. if(ChkSum!=ChkSumOrg)
  1211. {
  1212. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  1213. free(buf);
  1214. //system("cd /root;./FactoryConfig -m");
  1215. sleep(5);
  1216. char facBuf[256];
  1217. sprintf(facBuf, "cd /root;./FactoryConfig -m %s %s",
  1218. ShmSysConfigAndInfo->SysConfig.ModelName,
  1219. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  1220. system(facBuf);
  1221. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1222. system("sync");
  1223. sleep(5);
  1224. system("reboot -f");
  1225. sleep(5);
  1226. system("reboot -f");
  1227. return FAIL;
  1228. }
  1229. }
  1230. }
  1231. //load OK
  1232. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1233. free(buf);
  1234. system("rm -f /mnt/EvseConfig.bin");
  1235. // SysConfig in flash is empty (0xffffffff)
  1236. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1237. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1238. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1239. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1240. {
  1241. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1242. {
  1243. PRINTF_FUNC("Model name over length.\n");
  1244. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1245. }
  1246. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1247. {
  1248. PRINTF_FUNC("Model serial number over length.\n");
  1249. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1250. }
  1251. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1252. {
  1253. PRINTF_FUNC("System id over length.\n");
  1254. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1255. }
  1256. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1257. {
  1258. PRINTF_FUNC("Ethernet dhcp config is null.\n");
  1259. }
  1260. system("cd /root;./Module_FactoryConfig -m");
  1261. sleep(3);
  1262. system("/usr/bin/run_evse_restart.sh");
  1263. }
  1264. PRINTF_FUNC("*********************************************************** \n");
  1265. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  1266. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1267. {
  1268. memcpy((char*)ShmOCPP16Data->ChargeBoxId,
  1269. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1270. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1271. }
  1272. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1273. {
  1274. memcpy((char*)ShmOCPP20Data->ChargeBoxId,
  1275. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1276. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1277. }
  1278. return PASS;
  1279. }
  1280. int isRouteFail()
  1281. {
  1282. int result = YES;
  1283. FILE *fp;
  1284. char buf[512];
  1285. fp = popen("route -n", "r");
  1286. if(fp != NULL)
  1287. {
  1288. while(fgets(buf, sizeof(buf), fp) != NULL)
  1289. {
  1290. if(strstr(buf, "eth0") != NULL)
  1291. result = NO;
  1292. }
  1293. }
  1294. pclose(fp);
  1295. return result;
  1296. }
  1297. int isReachableInternet()
  1298. {
  1299. int result = FAIL;
  1300. FILE *fp;
  1301. char cmd[256];
  1302. char buf[512];
  1303. char tmp[512];
  1304. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1305. // {
  1306. // result = PASS;
  1307. // }
  1308. // else
  1309. {
  1310. strcpy(cmd, "ifconfig eth0");
  1311. fp = popen(cmd, "r");
  1312. if (fp != NULL)
  1313. {
  1314. while(fgets(buf, sizeof(buf), fp) != NULL)
  1315. {
  1316. if (strstr(buf, "inet addr:") > 0)
  1317. {
  1318. sscanf(buf, "%*s%s", tmp);
  1319. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1320. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1321. {
  1322. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1323. }
  1324. sscanf(buf, "%*s%*s%*s%s", tmp);
  1325. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  1326. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  1327. {
  1328. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  1329. }
  1330. }
  1331. }
  1332. }
  1333. pclose(fp);
  1334. memset(buf, 0x00, sizeof(buf));
  1335. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1336. {
  1337. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1338. fp = popen(cmd, "r");
  1339. if(fp != NULL)
  1340. {
  1341. while(fgets(buf, sizeof(buf), fp) != NULL)
  1342. {
  1343. if(strstr(buf, "transmitted") > 0)
  1344. {
  1345. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1346. if(strstr(buf,"100%") != NULL)
  1347. {
  1348. }
  1349. else
  1350. {
  1351. result = PASS;
  1352. }
  1353. //DEBUG_INFO("%s",buf);
  1354. //DEBUG_INFO("%s\n",tmp);
  1355. }
  1356. }
  1357. }
  1358. pclose(fp);
  1359. }
  1360. }
  1361. return result;
  1362. }
  1363. void InitEthernet()
  1364. {
  1365. system("ifconfig eth0 down");// eth0 down
  1366. system("ifconfig eth1 down");// eth1 down
  1367. sleep(2);
  1368. char tmpbuf[256];
  1369. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1370. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1371. sleep(2);
  1372. //Init Eth0 for internet
  1373. memset(tmpbuf,0,256);
  1374. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1375. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1376. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1377. system(tmpbuf);
  1378. memset(tmpbuf,0,256);
  1379. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1380. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1381. system(tmpbuf);
  1382. //system("ifconfig lo up");
  1383. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1384. //Init Eth1 for administrator tool
  1385. memset(tmpbuf,0,256);
  1386. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  1387. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1388. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1389. system(tmpbuf);
  1390. //Run DHCP client if enabled
  1391. system("killall udhcpc");
  1392. system("rm -rf /etc/resolv.conf");
  1393. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1394. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  1395. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1396. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1397. {
  1398. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1399. system(tmpbuf);
  1400. }
  1401. //Upgrade system id to /etc/hostname
  1402. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1403. system(tmpbuf);
  1404. pid_t pid = fork();
  1405. uint8_t cnt_pingDNS_Fail;
  1406. if(pid == 0)
  1407. {
  1408. for(;;)
  1409. {
  1410. if (isRouteFail())
  1411. {
  1412. //PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1413. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1414. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1415. {
  1416. InitialDHCP();
  1417. }
  1418. else
  1419. {
  1420. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1421. memset(tmpbuf,0,256);
  1422. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1423. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1424. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1425. system(tmpbuf);
  1426. memset(tmpbuf,0,256);
  1427. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1428. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1429. system(tmpbuf);
  1430. }
  1431. }
  1432. if(isReachableInternet() == PASS)
  1433. {
  1434. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1435. cnt_pingDNS_Fail = 0;
  1436. }
  1437. else
  1438. {
  1439. if(++cnt_pingDNS_Fail > 3)
  1440. {
  1441. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1442. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1443. {
  1444. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1445. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1446. system(tmpbuf);
  1447. }
  1448. else
  1449. {
  1450. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1451. memset(tmpbuf,0,256);
  1452. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1453. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1454. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1455. system(tmpbuf);
  1456. memset(tmpbuf,0,256);
  1457. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1458. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1459. system(tmpbuf);
  1460. }
  1461. cnt_pingDNS_Fail = 0;
  1462. }
  1463. }
  1464. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1465. if(ethResult == YES)
  1466. {
  1467. system("/sbin/ifmetric eth0 0");
  1468. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') ||
  1469. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1470. system("/sbin/ifmetric mlan0 1");
  1471. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') ||
  1472. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1473. system("/sbin/ifmetric ppp0 2");
  1474. }
  1475. if (!ethResult &&
  1476. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1477. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1478. {
  1479. //ethResult = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn;
  1480. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1481. if (ethResult)
  1482. {
  1483. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1484. {
  1485. system("/sbin/ifmetric eth0 1");
  1486. system("/sbin/ifmetric mlan0 0");
  1487. }
  1488. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1489. system("/sbin/ifmetric ppp0 2");
  1490. }
  1491. }
  1492. if (!ethResult &&
  1493. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1494. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1495. {
  1496. //ethResult = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn;
  1497. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  1498. if (ethResult)
  1499. {
  1500. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1501. system("/sbin/ifmetric mlan0 2");
  1502. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1503. {
  1504. system("/sbin/ifmetric eth0 1");
  1505. system("/sbin/ifmetric ppp0 0");
  1506. }
  1507. }
  1508. }
  1509. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1510. sleep(5);
  1511. }
  1512. }
  1513. #ifdef SystemLogMessage
  1514. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1515. #endif
  1516. }
  1517. int InitialRfidPort()
  1518. {
  1519. int uartO2 = open(rfidPortName, O_RDWR);
  1520. struct termios tios;
  1521. if (uartO2 != FAIL)
  1522. {
  1523. ioctl (uartO2, TCGETS, &tios);
  1524. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1525. tios.c_lflag = 0;
  1526. tios.c_iflag = 0;
  1527. tios.c_oflag = 0;
  1528. tios.c_cc[VMIN] = 0;
  1529. tios.c_cc[VTIME] = (unsigned char) 1;
  1530. tios.c_lflag = 0;
  1531. tcflush(uartO2, TCIFLUSH);
  1532. ioctl(uartO2, TCSETS, &tios);
  1533. }
  1534. if (uartO2 < 0)
  1535. {
  1536. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1537. }
  1538. return uartO2;
  1539. }
  1540. void GetMacAddress()
  1541. {
  1542. for (byte index = 0; index < 2; index++)
  1543. {
  1544. int fd;
  1545. struct ifreq ifr;
  1546. char tarEth[5];
  1547. char Mac[18];
  1548. sprintf(tarEth,"eth%d",index);
  1549. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1550. ifr.ifr_addr.sa_family = AF_INET;
  1551. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1552. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1553. close(fd);
  1554. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1555. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1556. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1557. if (index == 0)
  1558. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1559. else
  1560. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1561. }
  1562. }
  1563. void GetFirmwareVersion()
  1564. {
  1565. // Get CSU root file system version
  1566. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1567. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1568. for(uint8_t idx=0;idx<3;idx++)
  1569. {
  1570. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J' ||
  1571. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'K' ||
  1572. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'L')
  1573. {
  1574. chademo++;
  1575. count++;
  1576. }
  1577. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1578. {
  1579. gb++;
  1580. count++;
  1581. }
  1582. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1583. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E' ||
  1584. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'T' ||
  1585. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'D' ||
  1586. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'M' ||
  1587. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'N' ||
  1588. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'R' ||
  1589. ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'V' ||
  1590. ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'F' ||
  1591. ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'Y' ||
  1592. ShmSysConfigAndInfo->SysConfig.ModelName [7 + idx] == 'Z' )
  1593. {
  1594. ccs++;
  1595. count++;
  1596. }
  1597. }
  1598. if (count == 1)
  1599. {
  1600. if (chademo > 0)
  1601. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1602. else if (ccs > 0)
  1603. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1604. else if (gb > 0)
  1605. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1606. }
  1607. else
  1608. {
  1609. if (chademo == 1 && ccs == 1)
  1610. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1611. else if (chademo == 1 && gb == 1)
  1612. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1613. else if (ccs == 1 && gb == 1)
  1614. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1615. else if (chademo == 2)
  1616. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '7';
  1617. else if (ccs == 2)
  1618. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '8';
  1619. else if (gb == 2)
  1620. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '9';
  1621. }
  1622. // Get network option from model name
  1623. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1624. {
  1625. case 'B':
  1626. case 'U':
  1627. //Blue tooth
  1628. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1629. break;
  1630. case 'W':
  1631. // WIFI
  1632. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1633. break;
  1634. case 'T':
  1635. // 3G/4G
  1636. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1637. break;
  1638. case 'D':
  1639. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1640. break;
  1641. default:
  1642. // LAN
  1643. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1644. break;
  1645. }
  1646. // Get rating power from model name
  1647. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1648. // Get IEC or UL
  1649. char _buf[3] = {0};
  1650. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 1);
  1651. if (strcmp(_buf, "Y") == EQUAL || strcmp(_buf, "Y") == EQUAL)
  1652. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1653. else if (strcmp(_buf, "W") == EQUAL)
  1654. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1655. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1656. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1657. if (strcmp(_buf, "PN") == EQUAL ||
  1658. strcmp(_buf, "N7") == EQUAL ||
  1659. strcmp(_buf, "NX") == EQUAL ||
  1660. strcmp(_buf, "OL") == EQUAL ||
  1661. strcmp(_buf, "DE") == EQUAL)
  1662. ShmDcCommonData->ShowLogoFlag = NO;
  1663. else
  1664. ShmDcCommonData->ShowLogoFlag = YES;
  1665. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'G')
  1666. ShmDcCommonData->chargerType = CHARGER_TYPE_SIMPLE;
  1667. else
  1668. ShmDcCommonData->chargerType = CHARGER_TYPE_STANDARD;
  1669. }
  1670. void InitialGunIndexToUnUse()
  1671. {
  1672. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  1673. {
  1674. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index = NO_DEFINE;
  1675. }
  1676. for (byte index = 0; index < CCS_QUANTITY; index++)
  1677. {
  1678. ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index = NO_DEFINE;
  1679. }
  1680. for (byte index = 0; index < GB_QUANTITY; index++)
  1681. {
  1682. ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index = NO_DEFINE;
  1683. }
  1684. for (byte index = 0; index < AC_QUANTITY; index++)
  1685. {
  1686. ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index = NO_DEFINE;
  1687. }
  1688. }
  1689. void InitialShareMemoryInfo()
  1690. {
  1691. FILE *fp;
  1692. char cmd[512];
  1693. char buf[512];
  1694. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1695. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1696. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1697. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1698. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1699. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1700. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1701. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1702. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1703. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1704. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1705. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1706. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1707. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1708. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1709. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1710. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1711. sprintf(cmd, "/bin/uname -r");
  1712. fp = popen(cmd, "r");
  1713. if(fp == NULL)
  1714. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1715. else
  1716. {
  1717. while(fgets(buf, sizeof(buf), fp) != NULL)
  1718. {
  1719. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1720. }
  1721. }
  1722. GetFirmwareVersion();
  1723. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1724. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1725. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1726. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1727. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1728. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1729. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1730. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1731. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1732. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1733. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1734. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1735. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1736. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1737. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1738. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1739. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1740. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1741. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  1742. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = NO;
  1743. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1744. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1745. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1746. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1747. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1748. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = NO;
  1749. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1750. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1751. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1752. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1753. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = NO;
  1754. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1755. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1756. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1757. ShmRelayModuleData->SelfTest_Comp = NO;
  1758. ShmFanModuleData->SelfTest_Comp = NO;
  1759. ShmLedModuleData->SelfTest_Comp = NO;
  1760. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1761. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1762. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1763. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1764. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1765. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOTTING;
  1766. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1767. ShmFanModuleData->TestFanSpeed = 0;
  1768. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1769. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1770. InitialGunIndexToUnUse();
  1771. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1772. ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V015S0;
  1773. ShmDcCommonData->psuKeepCommunication = NO;
  1774. ShmDcCommonData->acContactSwitch = NO;
  1775. ShmDcCommonData->ConnectErrList[0].GunErrMessage = 0;
  1776. ShmDcCommonData->ConnectErrList[1].GunErrMessage = 0;
  1777. ShmDcCommonData->LcmFwVersion = 0;
  1778. ShmDcCommonData->minOutputCur = 0;
  1779. ShmDcCommonData->evBoardResetFlag = NO;
  1780. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  1781. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  1782. ShmDcCommonData->_isPsuErrorOccur = NO;
  1783. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1784. }
  1785. int Initialization()
  1786. {
  1787. // 初始化卡號驗證的 Flag
  1788. ClearAuthorizedFlag();
  1789. // 初始化插槍驗證的 Flag
  1790. ClearDetectPluginFlag();
  1791. // UART 2 for Rfid
  1792. rfidFd = InitialRfidPort();
  1793. int pinOut[2] = { 116, 115 }; //D13(Cha)、C13(CCS)
  1794. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1795. {
  1796. chargingInfo[count]->RemoteStartFlag = NO;
  1797. chargingInfo[count]->ReservedStartFlag = NO;
  1798. if (chargingInfo[count]->Type == _Type_Chademo)
  1799. {
  1800. gpio_set_value(pinOut[count], 0x00);
  1801. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1802. }
  1803. else if (chargingInfo[count]->Type == _Type_GB)
  1804. {
  1805. gpio_set_value(pinOut[count], 0x00);
  1806. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1807. }
  1808. else if (chargingInfo[count]->Type == _Type_CCS)
  1809. {
  1810. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1811. gpio_set_value(pinOut[1], 0x00);
  1812. else
  1813. gpio_set_value(pinOut[count], 0x00);
  1814. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1815. }
  1816. strcpy((char *)ShmDcCommonData->_reserved_UserId[count], "");
  1817. }
  1818. byte acDirIndex = 0;
  1819. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1820. {
  1821. if (count == 1)
  1822. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1823. ocpp_set_noErrorCode_cmd(count + acDirIndex, "NoError");
  1824. }
  1825. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  1826. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1827. {
  1828. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1829. if (ac_chargingInfo[count]->Type == _Type_AC)
  1830. {
  1831. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1832. ocpp_set_noErrorCode_cmd(count + hasDc, "NoError");
  1833. }
  1834. }
  1835. PRINTF_FUNC("Information check done \n");
  1836. return PASS;
  1837. }
  1838. bool InitialSystemDefaultConfig()
  1839. {
  1840. bool result = true;
  1841. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1842. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  1843. InitGPIO();
  1844. InitEthernet();
  1845. GetMacAddress();
  1846. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1847. // sleep(3);
  1848. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1849. // sleep(1);
  1850. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1851. return result;
  1852. }
  1853. bool DisplaySelfTestFailReason()
  1854. {
  1855. bool result = false;
  1856. // RB、FB、407、EV 小板中有些板子無回應
  1857. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1858. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1859. if (ShmFanModuleData->SelfTest_Comp == NO)
  1860. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1861. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1862. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1863. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1864. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1865. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES)
  1866. { result = true; }
  1867. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1868. {
  1869. if (chargingInfo[index]->Type == _Type_Chademo)
  1870. {
  1871. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1872. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1873. }
  1874. else if (chargingInfo[index]->Type == _Type_GB)
  1875. {
  1876. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1877. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1878. }
  1879. else if (chargingInfo[index]->Type == _Type_CCS)
  1880. {
  1881. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1882. {
  1883. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1884. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1885. }
  1886. }
  1887. }
  1888. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1889. {
  1890. // 先借 GBT 顯示
  1891. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1892. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1893. }
  1894. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1895. {
  1896. // AC Contact 未搭上
  1897. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1898. result = true;
  1899. }
  1900. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1901. {
  1902. result = true;
  1903. }
  1904. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1905. {
  1906. // PSU 通訊問題
  1907. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1908. result = true;
  1909. }
  1910. return result;
  1911. }
  1912. void SelfTestRun()
  1913. {
  1914. bool evInitFlag = false;
  1915. StartSystemTimeoutDet(Timeout_SelftestChk);
  1916. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1917. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  1918. GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) <= 20)
  1919. {
  1920. ReviewCriticalAlarm();
  1921. ChkPrimaryStatus();
  1922. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL ||
  1923. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1924. {
  1925. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1926. return;
  1927. }
  1928. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1929. {
  1930. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1931. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1932. {
  1933. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1934. return;
  1935. }
  1936. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1937. {
  1938. case _STEST_VERSION:
  1939. {
  1940. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  1941. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  1942. {
  1943. //PRINTF_FUNC("RB pass \n");
  1944. ShmRelayModuleData->SelfTest_Comp = YES;
  1945. }
  1946. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  1947. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  1948. {
  1949. //PRINTF_FUNC("Fan pass \n");
  1950. ShmFanModuleData->SelfTest_Comp = YES;
  1951. }
  1952. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  1953. ShmPrimaryMcuData->version[0] != '\0')
  1954. {
  1955. //PRINTF_FUNC("407 pass \n");
  1956. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1957. }
  1958. // EV 小板
  1959. if (!evInitFlag)
  1960. {
  1961. evInitFlag = YES;
  1962. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1963. {
  1964. if (chargingInfo[index]->Type == _Type_Chademo)
  1965. {
  1966. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1967. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1968. {
  1969. //PRINTF_FUNC("chademo pass \n");
  1970. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1971. }
  1972. else
  1973. {
  1974. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1975. evInitFlag = NO;
  1976. }
  1977. }
  1978. else if (chargingInfo[index]->Type == _Type_GB)
  1979. {
  1980. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1981. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1982. {
  1983. //PRINTF_FUNC("GBT pass \n");
  1984. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1985. }
  1986. else
  1987. {
  1988. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1989. evInitFlag = NO;
  1990. }
  1991. }
  1992. else if (chargingInfo[index]->Type == _Type_CCS)
  1993. {
  1994. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1995. {
  1996. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  1997. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  1998. {
  1999. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2000. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2001. }
  2002. else
  2003. {
  2004. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2005. evInitFlag = NO;
  2006. }
  2007. }
  2008. }
  2009. }
  2010. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  2011. {
  2012. if (ac_chargingInfo[index]->Type == _Type_AC)
  2013. {
  2014. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  2015. ac_chargingInfo[index]->version[0] != '\0')
  2016. {
  2017. ac_chargingInfo[index]->SelfTest_Comp = YES;
  2018. }
  2019. else
  2020. {
  2021. evInitFlag = NO;
  2022. }
  2023. }
  2024. }
  2025. }
  2026. if (ShmFanModuleData->SelfTest_Comp &&
  2027. ShmRelayModuleData->SelfTest_Comp &&
  2028. ShmPrimaryMcuData->SelfTest_Comp &&
  2029. evInitFlag)
  2030. {
  2031. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  2032. }
  2033. }
  2034. break;
  2035. case _STEST_AC_CONTACTOR:
  2036. {
  2037. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  2038. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  2039. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  2040. {
  2041. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  2042. PRINTF_FUNC("Communication board pass. \n");
  2043. }
  2044. }
  2045. break;
  2046. case _STEST_PSU_DETECT:
  2047. {
  2048. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  2049. {
  2050. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  2051. }
  2052. }
  2053. break;
  2054. case _STEST_PSU_CAP:
  2055. {
  2056. // 此測試是要確認當前總輸出能力
  2057. // 如果沒有 PSU 模組請 bypass
  2058. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  2059. {
  2060. sleep(1);
  2061. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  2062. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  2063. }
  2064. }
  2065. break;
  2066. }
  2067. }
  2068. else
  2069. break;
  2070. usleep(100000);
  2071. }
  2072. }
  2073. int SpawnTask()
  2074. {
  2075. sleep(2);
  2076. system("/root/Module_EventLogging &");
  2077. system("/root/Module_PrimaryComm &");
  2078. system("/root/Module_EvComm &");
  2079. system("/root/Module_LcmControl &");
  2080. system("/root/Module_InternalComm &");
  2081. system("/root/Module_PsuComm &");
  2082. system("/root/Module_ProduceUtils &");
  2083. ocpp_process_start();
  2084. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2085. {
  2086. system("/root/Module_4g &");
  2087. }
  2088. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2089. {
  2090. system("/root/Module_Wifi &");
  2091. }
  2092. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2093. {
  2094. system("/root/Module_4g &");
  2095. system("/root/Module_Wifi &");
  2096. }
  2097. return PASS;
  2098. }
  2099. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2100. {
  2101. int result = PASS;
  2102. int fd,wrd;
  2103. unsigned int i,Chk;
  2104. unsigned char *ptr, *BufTmp;
  2105. Chk=0;
  2106. ptr=(unsigned char *)UsrData;
  2107. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2108. {
  2109. memset(BufTmp, 0, MtdBlockSize);
  2110. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2111. for(i=0; i<MtdBlockSize-4; i++)
  2112. Chk+=*(ptr+i);
  2113. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  2114. fd = open("/dev/mtdblock10", O_RDWR);
  2115. if (fd > 0)
  2116. {
  2117. wrd=write(fd, BufTmp, MtdBlockSize);
  2118. close(fd);
  2119. if(wrd >= MtdBlockSize)
  2120. {
  2121. fd = open("/dev/mtdblock11", O_RDWR);
  2122. if (fd > 0)
  2123. {
  2124. wrd=write(fd, BufTmp, MtdBlockSize);
  2125. close(fd);
  2126. if(wrd < MtdBlockSize)
  2127. {
  2128. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  2129. result = FAIL;
  2130. }
  2131. }
  2132. else
  2133. {
  2134. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  2135. result = FAIL;
  2136. }
  2137. }
  2138. else
  2139. {
  2140. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  2141. result = FAIL;
  2142. }
  2143. }
  2144. else
  2145. {
  2146. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  2147. result = FAIL;
  2148. }
  2149. }
  2150. else
  2151. {
  2152. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  2153. result = FAIL;
  2154. }
  2155. if(BufTmp != NULL)
  2156. free(BufTmp);
  2157. return result;
  2158. }
  2159. //===============================================
  2160. // Common Detect Chk - Stop Charging ?
  2161. //===============================================
  2162. bool isEvBoardStopChargeFlag(byte gunIndex)
  2163. {
  2164. return chargingInfo[gunIndex]->StopChargeFlag;
  2165. }
  2166. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  2167. {
  2168. return chargingInfo[gunIndex]->NormalStopChargeFlag;
  2169. }
  2170. //===============================================
  2171. // 插槍鎖槍狀況
  2172. //===============================================
  2173. void ClearDetectPluginFlag()
  2174. {
  2175. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2176. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2177. {
  2178. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  2179. chargingInfo[gun_index]->RemoteStartFlag = NO;
  2180. if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  2181. chargingInfo[gun_index]->ReservedStartFlag = NO;
  2182. }
  2183. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2184. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2185. }
  2186. void DetectPluginStart()
  2187. {
  2188. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2189. }
  2190. bool isDetectPlugin()
  2191. {
  2192. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2193. return YES;
  2194. return NO;
  2195. }
  2196. bool isEvGunLocked(byte gunIndex)
  2197. {
  2198. return chargingInfo[gunIndex]->GunLocked;
  2199. }
  2200. //===============================================
  2201. // Common Detect Chk - Chademo
  2202. //===============================================
  2203. bool isEvContactorWelding_chademo(byte gunIndex)
  2204. {
  2205. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  2206. }
  2207. bool isEvStopReq_chademo(byte gunIndex)
  2208. {
  2209. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  2210. }
  2211. byte isPrechargeStatus_chademo(byte gunIndex)
  2212. {
  2213. byte result = 0x00;
  2214. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2215. return result;
  2216. }
  2217. //===============================================
  2218. // Common Detect Chk - GB
  2219. //===============================================
  2220. byte isPrechargeStatus_gb(byte gunIndex)
  2221. {
  2222. byte result = 0x00;
  2223. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2224. return result;
  2225. }
  2226. //===============================================
  2227. // Common Detect Chk - CCS
  2228. //===============================================
  2229. byte isPrechargeStatus_ccs(byte gunIndex)
  2230. {
  2231. byte result = 0x00;
  2232. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2233. {
  2234. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2235. }
  2236. return result;
  2237. }
  2238. //===============================================
  2239. // Callback
  2240. //===============================================
  2241. void DisplayChargingInfo()
  2242. {
  2243. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  2244. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2245. {
  2246. if (chargingInfo[i]->SystemStatus != SYS_MODE_IDLE &&
  2247. chargingInfo[i]->SystemStatus != SYS_MODE_RESERVATION)
  2248. {
  2249. ChangeGunSelectByIndex(i);
  2250. usleep(50000);
  2251. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2252. return;
  2253. }
  2254. }
  2255. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2256. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2257. ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE)
  2258. {
  2259. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2260. }
  2261. usleep(50000);
  2262. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2263. }
  2264. void _AutoReturnTimeout()
  2265. {
  2266. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  2267. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2268. {
  2269. ClearDetectPluginFlag();
  2270. }
  2271. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2272. {
  2273. bool needPluginDet = true;
  2274. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  2275. {
  2276. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2277. {
  2278. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  2279. {
  2280. needPluginDet = false;
  2281. break;
  2282. }
  2283. }
  2284. }
  2285. if (needPluginDet)
  2286. DetectPluginStart();
  2287. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHCOMP;
  2288. }
  2289. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2290. {
  2291. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2292. }
  2293. usleep(50000);
  2294. stopChargingChkByCard = false;
  2295. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2296. }
  2297. void _SelfTestTimeout()
  2298. {
  2299. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2300. {
  2301. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2302. {
  2303. ChargingAlarmProcess(gun_index);
  2304. }
  2305. }
  2306. ShmPsuData->Work_Step = _NO_WORKING;
  2307. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2308. PRINTF_FUNC("Self test timeout. \n");
  2309. }
  2310. void _AuthorizedTimeout()
  2311. {
  2312. //if(IsAuthorizingMode())
  2313. {
  2314. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  2315. isCardScan = false;
  2316. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2317. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2318. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  2319. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2320. ClearAuthorizedFlag();
  2321. }
  2322. }
  2323. void _DetectPlugInTimeout()
  2324. {
  2325. PRINTF_FUNC("*********** _DetectPlugInTimeout (%d)*********** \n", _connectionTimeout);
  2326. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2327. ClearDetectPluginFlag();
  2328. usleep(50000);
  2329. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2330. }
  2331. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2332. {
  2333. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2334. {
  2335. if(!isEvGunLocked(gunIndex))
  2336. {
  2337. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  2338. }
  2339. }
  2340. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2341. {
  2342. if(!isEvGunLocked(gunIndex))
  2343. {
  2344. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  2345. }
  2346. }
  2347. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2348. {
  2349. if(!isEvGunLocked(gunIndex))
  2350. {
  2351. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  2352. }
  2353. }
  2354. ChargingTerminalProcess(gunIndex);
  2355. _AutoReturnTimeout();
  2356. }
  2357. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2358. {
  2359. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  2360. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2361. {
  2362. setChargerMode(gunIndex, SYS_MODE_IDLE);
  2363. _AutoReturnTimeout();
  2364. }
  2365. }
  2366. void _PrepareTimeout(byte gunIndex)
  2367. {
  2368. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  2369. setChargerMode(gunIndex, SYS_MODE_IDLE);
  2370. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2371. _AutoReturnTimeout();
  2372. }
  2373. void _CcsPrechargeTimeout(byte gunIndex)
  2374. {
  2375. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  2376. setChargerMode(gunIndex, SYS_MODE_IDLE);
  2377. }
  2378. //===============================================
  2379. // 卡號驗證
  2380. //===============================================
  2381. void AuthorizingStart()
  2382. {
  2383. ocpp_set_authorizeReq_cmd(YES);
  2384. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2385. }
  2386. void ClearAuthorizedFlag()
  2387. {
  2388. ocpp_set_authorizeConf_cmd(NO);
  2389. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2390. }
  2391. bool isAuthorizedComplete()
  2392. {
  2393. return ocpp_get_authorize_conf();
  2394. }
  2395. bool IsAuthorizingMode()
  2396. {
  2397. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2398. return false;
  2399. return true;
  2400. }
  2401. //===============================================
  2402. // 紀錄 Alarm Code
  2403. //===============================================
  2404. void RecordAlarmCode(byte gunIndex, char *code)
  2405. {
  2406. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) == EQUAL)
  2407. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  2408. if (strcmp(code, "012234") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  2409. else if (strcmp(code, "012235") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  2410. else if (strcmp(code, "012236") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  2411. else if (strcmp(code, "012288") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = YES;
  2412. else if (strcmp(code, "012289") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = YES;
  2413. else if (strcmp(code, "012290") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = YES;
  2414. else if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  2415. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  2416. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  2417. else if (strcmp(code, "012296") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  2418. else if (strcmp(code, "012297") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  2419. else if (strcmp(code, "012298") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  2420. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2421. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2422. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2423. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  2424. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  2425. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  2426. else if (strcmp(code, "011018") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  2427. else if (strcmp(code, "011019") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  2428. else if (strcmp(code, "011020") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  2429. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = YES;
  2430. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = YES;
  2431. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = YES;
  2432. else if (strcmp(code, "011039") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = YES;
  2433. else if (strcmp(code, "011040") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = YES;
  2434. }
  2435. void ResetChargerAlarmCode(byte gunIndex, char *code)
  2436. {
  2437. if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  2438. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  2439. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  2440. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  2441. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  2442. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  2443. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  2444. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  2445. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  2446. else if (strcmp(code, "011018") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  2447. else if (strcmp(code, "011019") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  2448. else if (strcmp(code, "011020") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  2449. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2450. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2451. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2452. else if (strcmp(code, "011039") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = NO;
  2453. else if (strcmp(code, "011040") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = NO;
  2454. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2455. {
  2456. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012229", 6) == EQUAL ||
  2457. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012230", 6) == EQUAL ||
  2458. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012231", 6) == EQUAL ||
  2459. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011011", 6) == EQUAL ||
  2460. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011013", 6) == EQUAL ||
  2461. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011015", 6) == EQUAL ||
  2462. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011012", 6) == EQUAL ||
  2463. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011014", 6) == EQUAL ||
  2464. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011016", 6) == EQUAL ||
  2465. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011018", 6) == EQUAL ||
  2466. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011019", 6) == EQUAL ||
  2467. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011020", 6) == EQUAL ||
  2468. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012320", 6) == EQUAL ||
  2469. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012321", 6) == EQUAL ||
  2470. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012322", 6) == EQUAL ||
  2471. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011039", 6) == EQUAL ||
  2472. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011040", 6) == EQUAL)
  2473. {
  2474. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2475. }
  2476. }
  2477. }
  2478. void ReleaseSysAlarmCode(byte gunIndex)
  2479. {
  2480. // 回 idle 後主要清除 GFD Trip、UVP、OVP、GFD Warning,PSU fail
  2481. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2482. {
  2483. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012251", 6) == EQUAL ||
  2484. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012252", 6) == EQUAL ||
  2485. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012324", 6) == EQUAL ||
  2486. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012325", 6) == EQUAL ||
  2487. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012267", 6) == EQUAL ||
  2488. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012346", 6) == EQUAL)
  2489. {
  2490. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2491. }
  2492. }
  2493. if (gunIndex == 0)
  2494. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = NO;
  2495. else if (gunIndex == 1)
  2496. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = NO;
  2497. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2498. {
  2499. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  2500. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  2501. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  2502. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  2503. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2504. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2505. {
  2506. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL ||
  2507. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL ||
  2508. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012217", 6) == EQUAL ||
  2509. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012296", 6) == EQUAL)
  2510. {
  2511. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2512. }
  2513. }
  2514. }
  2515. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2516. {
  2517. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  2518. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  2519. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  2520. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  2521. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2522. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2523. {
  2524. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL ||
  2525. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL ||
  2526. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012219", 6) == EQUAL ||
  2527. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012297", 6) == EQUAL)
  2528. {
  2529. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2530. }
  2531. }
  2532. }
  2533. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2534. {
  2535. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  2536. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  2537. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  2538. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  2539. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2540. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2541. {
  2542. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL ||
  2543. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL ||
  2544. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012221", 6) == EQUAL ||
  2545. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012298", 6) == EQUAL)
  2546. {
  2547. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2548. }
  2549. }
  2550. }
  2551. ocpp_clear_errorCode_cmd(gunIndex);
  2552. }
  2553. void NormalStop(byte gunIndex)
  2554. {
  2555. PRINTF_FUNC("NormalStop (%d) \n", gunIndex);
  2556. }
  2557. //===============================================
  2558. // EmergencyStop and Charging Stop
  2559. //===============================================
  2560. void ChargingTerminalProcess(byte gunIndex)
  2561. {
  2562. setChargerMode(gunIndex, SYS_MODE_TERMINATING);
  2563. }
  2564. void ChargingAlarmProcess(byte gunIndex)
  2565. {
  2566. CheckSystemErrorFunction(gunIndex);
  2567. ocpp_set_errCode_cmd(gunIndex);
  2568. setChargerMode(gunIndex, SYS_MODE_ALARM);
  2569. }
  2570. void AcChargingTerminalProcess()
  2571. {
  2572. ac_chargingInfo[0]->SystemStatus = SYS_MODE_TERMINATING;
  2573. }
  2574. //void StopChargingProcessByString(byte level)
  2575. //{
  2576. // if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2577. // {
  2578. // ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2579. // }
  2580. //}
  2581. //void ReleaseChargingProcessByString(byte level)
  2582. //{
  2583. // if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2584. // ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2585. //}
  2586. // 一般錯誤停止充電處理函式
  2587. void BoardErrOccurByString(byte index, char *code)
  2588. {
  2589. //byte level = 1;
  2590. if ((chargingInfo[index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[index]->SystemStatus < SYS_MODE_TERMINATING) ||
  2591. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2592. {
  2593. PRINTF_FUNC("BoardErrOccurByString (%d) - (%s) \n", index, code);
  2594. if (strncmp(code, "023730", 6) == EQUAL &&
  2595. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2596. {
  2597. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2598. }
  2599. ChargingAlarmProcess(index);
  2600. }
  2601. //60StopChargingProcessByString(level);
  2602. }
  2603. // 急停狀況的停止充電處理函式
  2604. void EmcOccurThenStopCharging()
  2605. {
  2606. //byte level = 2;
  2607. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen...
  2608. // 其錯誤等級為 2
  2609. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2610. {
  2611. if ((chargingInfo[gun]->SystemStatus > SYS_MODE_IDLE && chargingInfo[gun]->SystemStatus < SYS_MODE_TERMINATING) ||
  2612. (chargingInfo[gun]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2613. {
  2614. ChargingAlarmProcess(gun);
  2615. }
  2616. }
  2617. //StopChargingProcessByString(level);
  2618. }
  2619. void ReleaseBoardErrOccurByString(byte index, char *code)
  2620. {
  2621. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2622. {
  2623. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2624. }
  2625. }
  2626. void ReleaseEmsOccureByString(byte index, char *code)
  2627. {
  2628. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2629. {
  2630. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2631. }
  2632. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2633. {
  2634. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2635. }
  2636. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2637. {
  2638. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2639. }
  2640. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2641. {
  2642. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2643. }
  2644. }
  2645. //===============================================
  2646. // 確認硬體 (按鈕) 狀態
  2647. //===============================================
  2648. bool leftBtnPush = false;
  2649. bool rightBtnPush = false;
  2650. void ButtonTriggerEvent()
  2651. {
  2652. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2653. ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS)
  2654. ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2655. else
  2656. ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2657. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2658. {
  2659. if(!leftBtnPush)
  2660. {
  2661. leftBtnPush = BTN_PRESS;
  2662. PRINTF_FUNC("left btn down............................... \n");
  2663. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2664. {
  2665. switch(ac_chargingInfo[0]->SystemStatus)
  2666. {
  2667. case SYS_MODE_IDLE:
  2668. case SYS_MODE_MAINTAIN:
  2669. case SYS_MODE_RESERVATION:
  2670. {
  2671. if(isDetectPlugin())
  2672. {
  2673. _DetectPlugInTimeout();
  2674. StopSystemTimeoutDet();
  2675. }
  2676. }
  2677. break;
  2678. case SYS_MODE_MODE_REASSIGN_CHECK:
  2679. case SYS_MODE_REASSIGN:
  2680. case SYS_MODE_PREPARING:
  2681. case SYS_MODE_PREPARE_FOR_EV:
  2682. case SYS_MODE_PREPARE_FOR_EVSE:
  2683. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2684. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2685. {
  2686. // 取消充電
  2687. AcChargingTerminalProcess();
  2688. }
  2689. break;
  2690. case SYS_MODE_CHARGING:
  2691. {
  2692. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2693. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2694. ac_chargingInfo[0]->isRemoteStart == NO) ||
  2695. ac_chargingInfo[0]->isRemoteStart)
  2696. {
  2697. // 停止充電
  2698. AcChargingTerminalProcess();
  2699. }
  2700. }
  2701. break;
  2702. case SYS_MODE_COMPLETE:
  2703. {}
  2704. break;
  2705. }
  2706. }
  2707. else
  2708. {
  2709. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2710. {
  2711. case SYS_MODE_IDLE:
  2712. case SYS_MODE_MAINTAIN:
  2713. case SYS_MODE_RESERVATION:
  2714. {
  2715. if(isDetectPlugin())
  2716. {
  2717. _DetectPlugInTimeout();
  2718. StopSystemTimeoutDet();
  2719. }
  2720. }
  2721. break;
  2722. case SYS_MODE_MODE_REASSIGN_CHECK:
  2723. case SYS_MODE_REASSIGN:
  2724. case SYS_MODE_PREPARING:
  2725. case SYS_MODE_PREPARE_FOR_EV:
  2726. case SYS_MODE_PREPARE_FOR_EVSE:
  2727. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2728. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2729. {
  2730. // 按鈕 - 取消充電
  2731. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2732. // AcChargingTerminalProcess();
  2733. // else
  2734. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2735. }
  2736. break;
  2737. case SYS_MODE_CHARGING:
  2738. {
  2739. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2740. ShmDcCommonData->_authWithCcidFlag [ShmSysConfigAndInfo->SysInfo.CurGunSelected] == _CCID_AUTHCOMP ||
  2741. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2742. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO) ||
  2743. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart)
  2744. {
  2745. // 按鈕 - 停止充電
  2746. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2747. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2748. }
  2749. }
  2750. break;
  2751. case SYS_MODE_COMPLETE:
  2752. {
  2753. // 回 IDLE
  2754. //PRINTF_FUNC("right btn down.................SYS_MODE_COMPLETE \n");
  2755. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = SYS_MODE_IDLE;
  2756. }
  2757. break;
  2758. }
  2759. }
  2760. }
  2761. }
  2762. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2763. {
  2764. if(leftBtnPush)
  2765. {
  2766. leftBtnPush = BTN_RELEASE;
  2767. PRINTF_FUNC("left btn up............................... \n");
  2768. }
  2769. }
  2770. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2771. {
  2772. if(!rightBtnPush)
  2773. {
  2774. rightBtnPush = BTN_PRESS;
  2775. PRINTF_FUNC("right btn down .................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2776. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG &&
  2777. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2778. return;
  2779. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2780. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2781. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 == ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2782. {
  2783. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2784. }
  2785. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2786. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2787. {
  2788. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2789. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2790. }
  2791. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2792. {
  2793. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  2794. {
  2795. if (chargingInfo[_index]->SystemStatus != SYS_MODE_BOOTING &&
  2796. chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE &&
  2797. chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION)
  2798. {
  2799. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2800. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2801. return;
  2802. }
  2803. }
  2804. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2805. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2806. }
  2807. else
  2808. {
  2809. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2810. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2811. }
  2812. }
  2813. }
  2814. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2815. {
  2816. if(rightBtnPush)
  2817. {
  2818. rightBtnPush = BTN_RELEASE;
  2819. PRINTF_FUNC("right btn up............................... \n");
  2820. }
  2821. }
  2822. }
  2823. void ChkPrimaryStatus()
  2824. {
  2825. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2826. {
  2827. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2828. EmcOccurThenStopCharging();
  2829. }
  2830. else
  2831. ReleaseEmsOccureByString(0, "012251");
  2832. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2833. {
  2834. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2835. EmcOccurThenStopCharging();
  2836. }
  2837. else
  2838. ReleaseEmsOccureByString(0, "012238");
  2839. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2840. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2841. else
  2842. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2843. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2844. {
  2845. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2846. EmcOccurThenStopCharging();
  2847. }
  2848. else
  2849. ReleaseEmsOccureByString(0, "012252");
  2850. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2851. {
  2852. EmcOccurThenStopCharging();
  2853. }
  2854. else
  2855. ReleaseEmsOccureByString(0, "012267");
  2856. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  2857. {
  2858. EmcOccurThenStopCharging ();
  2859. }
  2860. else
  2861. ReleaseEmsOccureByString (0,"012346");
  2862. }
  2863. //===============================================
  2864. // 確認各小板偵測的錯誤狀況
  2865. //===============================================
  2866. void CheckSystemErrorFunction(byte index)
  2867. {
  2868. // System
  2869. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  2870. RecordAlarmCode(index, "012251");
  2871. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip)
  2872. RecordAlarmCode(index, "012238");
  2873. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2874. RecordAlarmCode(index, "012252");
  2875. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2876. RecordAlarmCode(index, "012267");
  2877. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  2878. RecordAlarmCode(index, "012346");
  2879. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2880. {
  2881. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2882. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2883. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2884. {
  2885. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2886. {
  2887. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2888. }
  2889. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2890. RecordAlarmCode(index, "012203");
  2891. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2892. RecordAlarmCode(index, "012204");
  2893. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2894. RecordAlarmCode(index, "012205");
  2895. chargingInfo[index]->StopChargeFlag = YES;
  2896. }
  2897. else
  2898. {
  2899. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2900. {
  2901. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2902. }
  2903. }
  2904. }
  2905. else
  2906. {
  2907. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2908. {
  2909. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2910. }
  2911. }
  2912. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2913. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2914. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2915. {
  2916. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2917. {
  2918. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2919. }
  2920. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2921. RecordAlarmCode(index, "012200");
  2922. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2923. RecordAlarmCode(index, "012201");
  2924. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2925. RecordAlarmCode(index, "012202");
  2926. chargingInfo[index]->StopChargeFlag = YES;
  2927. }
  2928. else
  2929. {
  2930. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2931. {
  2932. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2933. }
  2934. }
  2935. }
  2936. //===============================================
  2937. // 確認 GPIO 狀態
  2938. //===============================================
  2939. void gpio_set_value(unsigned int gpio, unsigned int value)
  2940. {
  2941. int fd;
  2942. char buf[MAX_BUF];
  2943. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2944. fd = open(buf, O_WRONLY);
  2945. if (fd < 0)
  2946. {
  2947. perror("gpio/set-value");
  2948. return;
  2949. }
  2950. if (value)
  2951. write(fd, "1", 2);
  2952. else
  2953. write(fd, "0", 2);
  2954. close(fd);
  2955. }
  2956. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2957. {
  2958. int fd;
  2959. char buf[MAX_BUF];
  2960. char ch;
  2961. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2962. fd = open(buf, O_RDONLY);
  2963. if (fd < 0) {
  2964. perror("gpio/get-value");
  2965. return fd;
  2966. }
  2967. read(fd, &ch, 1);
  2968. if (ch != '0') {
  2969. *value = 1;
  2970. } else {
  2971. *value = 0;
  2972. }
  2973. close(fd);
  2974. return 0;
  2975. }
  2976. void CheckGunTypeFromHw()
  2977. {
  2978. int pinIn[4] = { 22, 23, 44, 45 };
  2979. unsigned int gpioValue = 0;
  2980. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2981. gpio_get_value(pinIn[i], &gpioValue);
  2982. {
  2983. switch (pinIn[i])
  2984. {
  2985. case 22:
  2986. bd1_1_status = gpioValue;
  2987. break;
  2988. case 23:
  2989. bd1_2_status = gpioValue;
  2990. break;
  2991. case 44:
  2992. bd0_1_status = gpioValue;
  2993. break;
  2994. case 45:
  2995. bd0_2_status = gpioValue;
  2996. break;
  2997. }
  2998. }
  2999. }
  3000. }
  3001. // Bypass useless functions
  3002. void CheckGpioInStatus()
  3003. {
  3004. int pinIn[2] = { 27, 47 };
  3005. unsigned int gpioValue = 0;
  3006. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  3007. {
  3008. gpio_get_value(pinIn[i], &gpioValue);
  3009. if (gpioValue == 0x01)
  3010. {
  3011. switch(pinIn[i])
  3012. {
  3013. // 小板緊急停止
  3014. case 47:
  3015. {
  3016. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3017. {
  3018. if (chargingInfo[i]->slotsIndex == 1)
  3019. {
  3020. // if (chargingInfo[i]->Type == _Type_Chademo)
  3021. // BoardErrOccurByString(i, "023730");
  3022. // else if (chargingInfo[i]->Type == _Type_CCS)
  3023. // BoardErrOccurByString(i, "013627");
  3024. break;
  3025. }
  3026. }
  3027. }
  3028. break;
  3029. case 27:
  3030. {
  3031. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3032. {
  3033. if (chargingInfo[i]->slotsIndex == 3)
  3034. {
  3035. // if (chargingInfo[i]->Type == _Type_Chademo)
  3036. // BoardErrOccurByString(i, "023730");
  3037. // else if (chargingInfo[i]->Type == _Type_CCS)
  3038. // BoardErrOccurByString(i, "013627");
  3039. break;
  3040. }
  3041. }
  3042. }
  3043. break;
  3044. }
  3045. }
  3046. else
  3047. {
  3048. switch (pinIn[i])
  3049. {
  3050. // 小板解除緊急停止
  3051. case 47:
  3052. {
  3053. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3054. {
  3055. if (chargingInfo[i]->slotsIndex == 1)
  3056. {
  3057. if (chargingInfo[i]->Type == _Type_Chademo)
  3058. ReleaseBoardErrOccurByString(i, "023730");
  3059. // else if (chargingInfo[i]->Type == _Type_CCS)
  3060. // ReleaseBoardErrOccurByString(i, "013627");
  3061. break;
  3062. }
  3063. }
  3064. }
  3065. break;
  3066. case 27:
  3067. {
  3068. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3069. {
  3070. if (chargingInfo[i]->slotsIndex == 3)
  3071. {
  3072. if (chargingInfo[i]->Type == _Type_Chademo)
  3073. ReleaseBoardErrOccurByString(i, "023730");
  3074. // else if (chargingInfo[i]->Type == _Type_CCS)
  3075. // ReleaseBoardErrOccurByString(i, "013627");
  3076. break;
  3077. }
  3078. }
  3079. }
  3080. break;
  3081. }
  3082. }
  3083. }
  3084. }
  3085. //===============================================
  3086. // Main process
  3087. //===============================================
  3088. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  3089. {
  3090. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  3091. {
  3092. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  3093. {
  3094. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  3095. return true;
  3096. }
  3097. }
  3098. for (byte index = 0; index < CCS_QUANTITY; index++)
  3099. {
  3100. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  3101. {
  3102. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  3103. return true;
  3104. }
  3105. }
  3106. for (byte index = 0; index < GB_QUANTITY; index++)
  3107. {
  3108. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  3109. {
  3110. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  3111. return true;
  3112. }
  3113. }
  3114. return false;
  3115. }
  3116. // 檢查 Byte 中某個 Bit 的值
  3117. // _byte : 欲改變的 byte
  3118. // _bit : 該 byte 的第幾個 bit
  3119. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  3120. {
  3121. return ( _byte & mask_table[_bit] ) != 0x00;
  3122. }
  3123. // 設定 Byte 中某個 Bit的值
  3124. // _byte : 欲改變的 byte
  3125. // _bit : 該 byte 的第幾個 bit
  3126. // value : 修改的值為 0 or 1
  3127. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  3128. {
  3129. if(value == 1)
  3130. *_byte |= (1 << _bit);
  3131. else if (value == 0)
  3132. *_byte ^= (1 << _bit);
  3133. }
  3134. void GetCcidAuthPossible(bool *idleReq, byte *idleIndex)
  3135. {
  3136. for (byte gun_index = 0;
  3137. gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  3138. gun_index ++)
  3139. {
  3140. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_PREPARE_FOR_EV &&
  3141. ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  3142. {
  3143. *idleIndex = gun_index;
  3144. *idleReq = true;
  3145. break;
  3146. }
  3147. }
  3148. }
  3149. void UserScanFunction()
  3150. {
  3151. bool idleReq = false;
  3152. unsigned char idleIndex = 255;
  3153. //unsigned char stopReq = 255;
  3154. bool hasGunUsing = false;
  3155. bool isReservedCard = false;
  3156. // 當前非驗證的狀態
  3157. if(!IsAuthorizingMode())
  3158. {
  3159. // 先判斷現在是否可以提供刷卡
  3160. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  3161. // 2. 停止充電
  3162. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX ||
  3163. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_EMC)
  3164. {
  3165. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3166. return;
  3167. }
  3168. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  3169. {
  3170. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  3171. ac_chargingInfo[0]->SystemStatus != SYS_MODE_ALARM)
  3172. {
  3173. // 如果選 AC 槍
  3174. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  3175. ac_chargingInfo[0]->SystemStatus < SYS_MODE_ALARM)
  3176. {
  3177. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  3178. {
  3179. char value[32];
  3180. PRINTF_FUNC("ac stop charging \n");
  3181. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  3182. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3183. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId, ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  3184. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3185. {
  3186. AcChargingTerminalProcess();
  3187. }
  3188. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3189. }
  3190. }
  3191. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  3192. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  3193. {
  3194. idleReq = true;
  3195. }
  3196. }
  3197. else
  3198. {
  3199. if (IsGunUsing(ShmSysConfigAndInfo->SysInfo.CurGunSelected))
  3200. {
  3201. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_CHARGING)
  3202. {
  3203. char value[32];
  3204. PRINTF_FUNC("stop charging \n");
  3205. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  3206. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3207. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3208. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  3209. // 同一張卡直接停掉
  3210. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3211. {
  3212. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3213. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3214. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3215. }
  3216. else
  3217. {
  3218. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3219. {
  3220. GetCcidAuthPossible(&idleReq, &idleIndex);
  3221. }
  3222. if (!idleReq)
  3223. {
  3224. PRINTF_FUNC ( "_LCM_AUTHORIZ_FAIL \n" );
  3225. stopChargingChkByCard = true;
  3226. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3227. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3228. }
  3229. }
  3230. }
  3231. else
  3232. {
  3233. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3234. {
  3235. GetCcidAuthPossible(&idleReq, &idleIndex);
  3236. }
  3237. }
  3238. }
  3239. // else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  3240. // chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  3241. // {
  3242. // idleIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  3243. // idleReq = true;
  3244. // }
  3245. else
  3246. {
  3247. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3248. (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZING ||
  3249. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_COMP ||
  3250. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_FAIL))
  3251. return;
  3252. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3253. {
  3254. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  3255. {
  3256. idleIndex = gun_index;
  3257. idleReq = true;
  3258. }
  3259. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3260. {
  3261. idleReq = true;
  3262. }
  3263. else
  3264. {
  3265. hasGunUsing = IsGunUsing(gun_index);
  3266. }
  3267. }
  3268. // 壁掛不給充 - 已經有至少一把槍在充電
  3269. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES &&
  3270. hasGunUsing)
  3271. {
  3272. PRINTF_FUNC("DW Serial not support dual charging. \n");
  3273. idleReq = false;
  3274. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3275. }
  3276. }
  3277. }
  3278. if (idleReq)
  3279. {
  3280. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  3281. // stopReq != 255 &&
  3282. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  3283. // {
  3284. // idleReq = false;
  3285. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3286. // }
  3287. // else
  3288. {
  3289. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gun_index++)
  3290. {
  3291. if (ac_chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3292. {
  3293. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3294. (char *) ShmDcCommonData->_reserved_ac_UserId [gun_index] ) == EQUAL)
  3295. {
  3296. isReservedCard = true;
  3297. // 驗證通過
  3298. ac_chargingInfo[gun_index]->ReservedStartFlag = YES;
  3299. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3300. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3301. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3302. break;
  3303. }
  3304. }
  3305. }
  3306. if (!isReservedCard)
  3307. {
  3308. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3309. {
  3310. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3311. {
  3312. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  3313. (char *)ShmDcCommonData->_reserved_UserId[gun_index]) == EQUAL)
  3314. {
  3315. isReservedCard = true;
  3316. // 驗證通過
  3317. chargingInfo[gun_index]->ReservedStartFlag = YES;
  3318. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3319. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3320. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3321. break;
  3322. }
  3323. }
  3324. }
  3325. }
  3326. if (!isReservedCard &&
  3327. ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  3328. (idleIndex != 255 && (chargingInfo[idleIndex]->SystemStatus == SYS_MODE_IDLE || chargingInfo[idleIndex]->SystemStatus == SYS_MODE_PREPARE_FOR_EV))))
  3329. {
  3330. if (ShmDcCommonData->_CcidAuthProcessStep != _CCID_AUTH)
  3331. {
  3332. PRINTF_FUNC("Start Authorizing... \n");
  3333. // LCM => Authorizing
  3334. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3335. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != DEFAULT_AC_INDEX)
  3336. {
  3337. ShmSysConfigAndInfo->SysInfo.CurGunSelected = idleIndex;
  3338. }
  3339. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  3340. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTH;
  3341. // 進入確認卡號狀態
  3342. AuthorizingStart();
  3343. }
  3344. }
  3345. else
  3346. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3347. }
  3348. }
  3349. else
  3350. {
  3351. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3352. }
  3353. }
  3354. }
  3355. }
  3356. unsigned char isModeChange(unsigned char gun_index)
  3357. {
  3358. unsigned char result = NO;
  3359. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3360. {
  3361. result = YES;
  3362. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3363. }
  3364. return result;
  3365. }
  3366. void ScannerCardProcess()
  3367. {
  3368. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL &&
  3369. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3370. {
  3371. isCardScan = true;
  3372. // 處理刷卡及驗證卡號的動作
  3373. UserScanFunction();
  3374. }
  3375. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3376. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3377. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  3378. {
  3379. StartSystemTimeoutDet(Timeout_Authorizing);
  3380. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3381. {
  3382. // 白名單驗證
  3383. for (int i = 0; i < 10; i++)
  3384. {
  3385. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3386. {
  3387. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3388. {
  3389. PRINTF_FUNC("White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3390. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3391. ClearAuthorizedFlag();
  3392. break;
  3393. }
  3394. }
  3395. }
  3396. }
  3397. // 確認驗證卡號完成沒
  3398. if (isAuthorizedComplete() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3399. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3400. {
  3401. StopSystemTimeoutDet();
  3402. // 判斷後台回覆狀態
  3403. if(ocpp_chk_authrization_cmd() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3404. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3405. {
  3406. // LCM => Authorize complete
  3407. PRINTF_FUNC("_LCM_AUTHORIZ_COMP \n");
  3408. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3409. }
  3410. else
  3411. {
  3412. // LCM => Authorize fail
  3413. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3414. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3415. }
  3416. ClearAuthorizedFlag();
  3417. }
  3418. }
  3419. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  3420. {
  3421. StartSystemTimeoutDet(Timeout_VerifyFail);
  3422. isCardScan = false;
  3423. }
  3424. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  3425. {
  3426. StartSystemTimeoutDet(Timeout_VerifyComp);
  3427. }
  3428. else if(ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  3429. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  3430. {
  3431. StartSystemTimeoutDet(Timeout_WaitPlug);
  3432. }
  3433. else
  3434. {
  3435. isCardScan = false;
  3436. }
  3437. }
  3438. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3439. {
  3440. bool result = true;
  3441. switch (typeValue)
  3442. {
  3443. case '0': // none
  3444. break;
  3445. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3446. break;
  3447. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3448. break;
  3449. case '3': // IEC 62196-2 Type 2 Plug
  3450. case '4': // IEC 62196-2 Type 2 Socket
  3451. if (AC_QUANTITY > _ac_Index)
  3452. {
  3453. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3454. // AC 固定 index
  3455. ac_chargingInfo[_acgunIndex]->Index = DEFAULT_AC_INDEX;
  3456. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3457. ac_chargingInfo[_acgunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3458. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3459. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3460. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3461. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3462. _ac_Index++;
  3463. _acgunIndex++;
  3464. }
  3465. else
  3466. result = false;
  3467. break;
  3468. case '5': // GB/T AC Plug
  3469. break;
  3470. case '6': // GB/T AC Socket
  3471. break;
  3472. case 'J': // CHAdeMO - 60A
  3473. case 'K': // CHAdeMO - 200A / 500V
  3474. case 'L': // CHAdeMO - 80A
  3475. {
  3476. if (CHAdeMO_QUANTITY > _chademoIndex)
  3477. {
  3478. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3479. chargingInfo[_gunIndex]->Index = _gunIndex;
  3480. chargingInfo[_gunIndex]->ReservationId = -1;
  3481. chargingInfo[_gunIndex]->slotsIndex = slots;
  3482. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3483. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3484. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3485. chargingInfo[_gunIndex]->IsAvailable = YES;
  3486. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3487. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3488. chargingInfo[_gunIndex]->ModelType = typeValue;
  3489. _chademoIndex++;
  3490. _gunIndex++;
  3491. }
  3492. else
  3493. result = false;
  3494. }
  3495. break;
  3496. case 'U': // Natural cooling CCS1
  3497. case 'E': // Natural cooling CCS2
  3498. case 'T': // Rema CCS1 - 300A
  3499. case 'D': // Rema CCS2 - 300A
  3500. case 'M': // CCS2 - 80A
  3501. case 'N': // CCS1 - 80A
  3502. case 'R': // CCS2 - 250A
  3503. case 'V' : // Liquid cooling CCS1 - 500A
  3504. case 'F' : // Liquid cooling CCS2 - 500A
  3505. case 'Y' : // CCS1 - 150A
  3506. case 'Z' : // CCS2 - 150A
  3507. {
  3508. if (CCS_QUANTITY > _ccsIndex)
  3509. {
  3510. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3511. chargingInfo[_gunIndex]->Index = _gunIndex;
  3512. chargingInfo[_gunIndex]->ReservationId = -1;
  3513. chargingInfo[_gunIndex]->slotsIndex = slots;
  3514. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3515. chargingInfo[_gunIndex]->Type = _Type_CCS;
  3516. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3517. chargingInfo[_gunIndex]->IsAvailable = YES;
  3518. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3519. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3520. chargingInfo[_gunIndex]->ModelType = typeValue;
  3521. // 現階段預設為走 DIN70121
  3522. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3523. if (typeValue == 'U' || typeValue == 'T' || typeValue == 'N' || typeValue == 'V' || typeValue == 'Y')
  3524. ShmDcCommonData->CcsTypeSaved [_gunIndex] = _CCS_TYPE_CCS1;
  3525. else if (typeValue == 'E' || typeValue == 'D' || typeValue == 'M' || typeValue == 'R' || typeValue == 'F' || typeValue == 'Z')
  3526. ShmDcCommonData->CcsTypeSaved [_gunIndex] = _CCS_TYPE_CCS2;
  3527. _ccsIndex++;
  3528. _gunIndex++;
  3529. }
  3530. else
  3531. result = false;
  3532. }
  3533. break;
  3534. case 'G': // GBT DC
  3535. case 'B':
  3536. {
  3537. if (GB_QUANTITY > _gb_Index)
  3538. {
  3539. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3540. chargingInfo[_gunIndex]->Index = _gunIndex;
  3541. chargingInfo[_gunIndex]->ReservationId = -1;
  3542. chargingInfo[_gunIndex]->slotsIndex = slots;
  3543. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3544. chargingInfo[_gunIndex]->Type = _Type_GB;
  3545. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3546. chargingInfo[_gunIndex]->IsAvailable = YES;
  3547. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3548. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3549. chargingInfo[_gunIndex]->ModelType = typeValue;
  3550. _gb_Index++;
  3551. _gunIndex++;
  3552. }
  3553. else
  3554. result = false;
  3555. }
  3556. break;
  3557. }
  3558. return result;
  3559. }
  3560. bool CheckConnectorTypeStatus()
  3561. {
  3562. bool result = true;
  3563. // 硬體 0 1 => CCS,1 0 => Chademo,1 1 => GB
  3564. //CheckFwSlotSta.tusLog();
  3565. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3566. {
  3567. byte slots = 1;
  3568. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3569. {
  3570. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3571. {
  3572. return false;
  3573. }
  3574. slots++;
  3575. }
  3576. // AC index 接在 DC 後面
  3577. // if (_ac_Index > 0)
  3578. // ac_chargingInfo[0]->Index += _gunIndex;
  3579. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3580. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3581. PRINTF_FUNC("Charger Count => DC-Count = %d, AC-Count = %d \n",
  3582. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3583. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3584. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3585. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3586. result = false;
  3587. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3588. {
  3589. bool isFind = false;
  3590. if (chargingInfo[0]->Type == _Type_Chademo)
  3591. {
  3592. if ((bd0_1_status == 0 && bd0_2_status == 1) ||
  3593. (bd1_1_status == 0 && bd1_2_status == 1))
  3594. {
  3595. isFind = true;
  3596. }
  3597. }
  3598. else if (chargingInfo[0]->Type == _Type_CCS)
  3599. {
  3600. if ((bd0_1_status == 1 && bd0_2_status == 0) ||
  3601. (bd1_1_status == 1 && bd1_2_status == 0))
  3602. {
  3603. isFind = true;
  3604. }
  3605. }
  3606. else if (chargingInfo[0]->Type == _Type_GB)
  3607. {
  3608. if ((bd0_1_status == 1 && bd0_2_status == 1) ||
  3609. (bd1_1_status == 1 && bd1_2_status == 1))
  3610. {
  3611. isFind = true;
  3612. }
  3613. }
  3614. if (isFind)
  3615. {
  3616. chargingInfo[0]->Evboard_id = 0x01;
  3617. CheckHwSlotStatusLog(0);
  3618. }
  3619. else
  3620. result = false;
  3621. }
  3622. else
  3623. {
  3624. char type = NO_DEFINE;
  3625. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3626. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3627. {
  3628. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3629. {
  3630. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3631. // 與硬體相同 type : Chademo
  3632. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3633. {
  3634. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3635. }
  3636. }
  3637. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3638. {
  3639. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3640. // 與硬體相同 type : CCS
  3641. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3642. {
  3643. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3644. }
  3645. }
  3646. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3647. {
  3648. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3649. // 與硬體相同 type : GB
  3650. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3651. {
  3652. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3653. }
  3654. }
  3655. else if (gunIndex == 0)
  3656. {
  3657. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3658. gunIndex, bd0_1_status, bd0_2_status);
  3659. }
  3660. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3661. {
  3662. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3663. // 與硬體相同 type : Chademo
  3664. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3665. {
  3666. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3667. }
  3668. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3669. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3670. }
  3671. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3672. {
  3673. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3674. // 與硬體相同 type : CCS
  3675. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3676. {
  3677. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3678. }
  3679. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3680. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3681. }
  3682. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3683. {
  3684. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3685. // 與硬體相同 type : GB
  3686. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3687. {
  3688. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3689. }
  3690. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3691. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3692. }
  3693. else if (gunIndex == 1)
  3694. {
  3695. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3696. gunIndex, bd1_1_status, bd1_2_status);
  3697. }
  3698. CheckHwSlotStatusLog(gunIndex);
  3699. if (type == NO_DEFINE)
  3700. type = chargingInfo[gunIndex]->Type;
  3701. else
  3702. {
  3703. // 雙槍都是同一個 type
  3704. if (type == chargingInfo[gunIndex]->Type)
  3705. ShmDcCommonData->SysGunAreSameType = YES;
  3706. }
  3707. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3708. result = false;
  3709. }
  3710. }
  3711. }
  3712. else
  3713. {
  3714. // Module Name 不正確 - 告警
  3715. result = false;
  3716. }
  3717. return result;
  3718. }
  3719. void KillTask()
  3720. {
  3721. ChangeLcmByIndex(_LCM_FIX);
  3722. system("killall Module_EventLogging");
  3723. system("killall Module_PrimaryComm");
  3724. system("killall Module_EvComm");
  3725. system("killall Module_InternalComm");
  3726. system("killall Module_PsuComm");
  3727. system("killall Module_4g &");
  3728. system("killall Module_Wifi &");
  3729. }
  3730. void KillTaskExceptDetectTask()
  3731. {
  3732. ChangeLcmByIndex(_LCM_FIX);
  3733. sleep(3);
  3734. system("killall Module_EvComm");
  3735. //system("killall Module_InternalComm");
  3736. system("killall Module_PsuComm");
  3737. system("killall Module_4g &");
  3738. system("killall Module_Wifi &");
  3739. }
  3740. void KillAllTask()
  3741. {
  3742. ChangeLcmByIndex(_LCM_FIX);
  3743. system("killall Module_EventLogging");
  3744. system("killall Module_PrimaryComm");
  3745. system("killall Module_EvComm");
  3746. system("killall Module_LcmControl");
  3747. system("killall Module_InternalComm");
  3748. system("killall Module_PsuComm");
  3749. system("killall OcppBackend &");
  3750. system("killall Module_4g &");
  3751. system("killall Module_Wifi &");
  3752. system("killall Module_DcMeter &");
  3753. }
  3754. void LogUpgradeType(unsigned int type)
  3755. {
  3756. switch (type)
  3757. {
  3758. case _UPDATE_TYPE_CSU_UBOOT: PRINTF_FUNC("CSU bootloader (uboot) \n"); break;
  3759. case _UPDATE_TYPE_CSU_DTB: PRINTF_FUNC("CSU kernel configuration (dtb) \n"); break;
  3760. case _UPDATE_TYPE_CSU_ZIMAGE: PRINTF_FUNC("CSU kernel image (zImage) \n"); break;
  3761. case _UPDATE_TYPE_CSU_RFILES: PRINTF_FUNC("CSU root file system (ramdisk.gz) \n"); break;
  3762. case _UPDATE_TYPE_CSU_CONFIG: PRINTF_FUNC("CSU user configuration (bin) \n"); break;
  3763. case _UPDATE_TYPE_CCS_UBOOT: PRINTF_FUNC("CCS board bootloader (uboot) \n"); break;
  3764. case _UPDATE_TYPE_CCS_DTB: PRINTF_FUNC("CCS board kernel configuration (dtb) \n"); break;
  3765. case _UPDATE_TYPE_CCS_ZIMAGE: PRINTF_FUNC("CCS board kernel image (zImage) \n"); break;
  3766. case _UPDATE_TYPE_CCS_RFILES: PRINTF_FUNC("CCS board file system (ramdisk.gz) \n"); break;
  3767. case _UPDATE_TYPE_DCM_ST407: PRINTF_FUNC("DCM 407 primary controller \n"); break;
  3768. case _UPDATE_TYPE_RELAY_CTR: PRINTF_FUNC("Relay control board \n"); break;
  3769. case _UPDATE_TYPE_FAN_CTR: PRINTF_FUNC("Fan control board \n"); break;
  3770. case _UPDATE_TYPE_AC_WLMOUNT_CTR: PRINTF_FUNC("AC wall-mount (low-end) controller \n"); break;
  3771. case _UPDATE_TYPE_LED_CTR: PRINTF_FUNC("LED control board \n"); break;
  3772. case _UPDATE_TYPE_CHADEMO: PRINTF_FUNC("CAHdeMO board \n"); break;
  3773. case _UPDATE_TYPE_GBT: PRINTF_FUNC("GBT board \n"); break;
  3774. }
  3775. }
  3776. int CheckUpdateProcess(void)
  3777. {
  3778. //bool isPass = true;
  3779. uint8_t retSucc = 0;
  3780. uint8_t retFail = 0;
  3781. byte index = 0;
  3782. byte target = 0;
  3783. char new_str[256];
  3784. unsigned char *ptr = NULL;
  3785. int fd = 0;
  3786. int CanFd = 0;
  3787. int uartFd = 0;
  3788. unsigned int Type = 0;
  3789. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3790. DIR *d;
  3791. struct dirent *dir;
  3792. byte result = PASS;
  3793. char Buf[256];
  3794. d = opendir("/mnt/");
  3795. if (d)
  3796. {
  3797. while ((dir = readdir(d)) != NULL)
  3798. {
  3799. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
  3800. {
  3801. continue;
  3802. }
  3803. memset(new_str, 0x00, sizeof(new_str));
  3804. strcat(new_str, "/mnt/");
  3805. strcat(new_str, dir->d_name);
  3806. PRINTF_FUNC("%s \n", new_str);
  3807. fd = open(new_str, O_RDONLY);
  3808. if (fd < 0)
  3809. {
  3810. close(fd);
  3811. result = FAIL;
  3812. sleep(1);
  3813. continue;
  3814. }
  3815. ptr = malloc(MaxLen); //-48 is take out the header
  3816. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3817. //get the image length
  3818. ImageLen = read(fd, ptr, MaxLen);
  3819. bool isModelNameFail = false;
  3820. // DS-----J0E-2PH
  3821. if((ShmSysConfigAndInfo->SysConfig.ModelName[0] != ptr[0]) ||
  3822. (ShmSysConfigAndInfo->SysConfig.ModelName[1] != ptr[1]) ||
  3823. (ShmSysConfigAndInfo->SysConfig.ModelName[7] != ptr[7]) ||
  3824. (ShmSysConfigAndInfo->SysConfig.ModelName[8] != ptr[8]) ||
  3825. (ShmSysConfigAndInfo->SysConfig.ModelName[9] != ptr[9]) ||
  3826. (ShmSysConfigAndInfo->SysConfig.ModelName[11] != ptr[11]) ||
  3827. (ShmSysConfigAndInfo->SysConfig.ModelName[12] != ptr[12]) ||
  3828. (ShmSysConfigAndInfo->SysConfig.ModelName[13] != ptr[13]))
  3829. {
  3830. result = MODELNAME_FAIL;
  3831. isModelNameFail = true;
  3832. }
  3833. if (isModelNameFail)
  3834. {
  3835. close(fd);
  3836. continue;
  3837. }
  3838. PRINTF_FUNC("model name check pass. \n");
  3839. if (ImageLen > 20) {
  3840. Type = (((unsigned int)ptr[16]) << 24 |
  3841. ((unsigned int)ptr[17]) << 16 |
  3842. ((unsigned int)ptr[18]) << 8 |
  3843. ((unsigned int)ptr[19]));
  3844. LogUpgradeType(Type);
  3845. PRINTF_FUNC("Upgrade Status : PASS : %d, FAIL : %d \n", retSucc, retFail);
  3846. free(ptr);
  3847. switch (Type)
  3848. {
  3849. case _UPDATE_TYPE_CSU_UBOOT:
  3850. case _UPDATE_TYPE_CSU_DTB:
  3851. case _UPDATE_TYPE_CSU_ZIMAGE:
  3852. case _UPDATE_TYPE_CSU_RFILES:
  3853. case _UPDATE_TYPE_CSU_CONFIG:
  3854. system("echo 3 > /proc/sys/vm/drop_caches");
  3855. sleep(2);
  3856. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3857. retSucc++;
  3858. else
  3859. {
  3860. PRINTF_FUNC("Upgrade CSU Failed (%d) \n", Type);
  3861. retFail++;
  3862. }
  3863. memset(Buf, 0, sizeof(Buf));
  3864. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3865. system(Buf);
  3866. break;
  3867. case _UPDATE_TYPE_CCS_UBOOT:
  3868. case _UPDATE_TYPE_CCS_DTB:
  3869. case _UPDATE_TYPE_CCS_ZIMAGE:
  3870. case _UPDATE_TYPE_CCS_RFILES:
  3871. CanFd = InitCanBus();
  3872. if (CanFd > 0)
  3873. {
  3874. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3875. {
  3876. if (chargingInfo[index]->Type == _Type_CCS)
  3877. {
  3878. byte targetID = chargingInfo[index]->Evboard_id;
  3879. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1 &&
  3880. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0)
  3881. {
  3882. targetID += 1;
  3883. }
  3884. system("echo 3 > /proc/sys/vm/drop_caches");
  3885. sleep(2);
  3886. PRINTF_FUNC("Upgrade CCS Processing..target id = %d \n", targetID);
  3887. if (Upgrade_CCS(CanFd, Type, targetID, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  3888. {
  3889. PRINTF_FUNC("Upgrade CCS Failed \n");
  3890. retFail++;
  3891. }
  3892. else
  3893. retSucc++;
  3894. }
  3895. }
  3896. close(CanFd);
  3897. }
  3898. memset(Buf, 0, sizeof(Buf));
  3899. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3900. system(Buf);
  3901. break;
  3902. case _UPDATE_TYPE_DCM_ST407:
  3903. case _UPDATE_TYPE_RELAY_CTR:
  3904. case _UPDATE_TYPE_FAN_CTR:
  3905. case _UPDATE_TYPE_AC_WLMOUNT_CTR:
  3906. case _UPDATE_TYPE_LED_CTR:
  3907. target = 0x00;
  3908. if (Type == _UPDATE_TYPE_DCM_ST407) {
  3909. target = UPGRADE_PRI;
  3910. } else if (Type == _UPDATE_TYPE_RELAY_CTR) {
  3911. target = UPGRADE_RB;
  3912. } else if (Type == _UPDATE_TYPE_FAN_CTR) {
  3913. target = UPGRADE_FAN;
  3914. } else if (Type == _UPDATE_TYPE_AC_WLMOUNT_CTR) {
  3915. target = UPGRADE_AC;
  3916. } else if (Type == _UPDATE_TYPE_LED_CTR) {
  3917. target = UPGRADE_LED;
  3918. }
  3919. uartFd = InitComPort(target);
  3920. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3921. {
  3922. retSucc++;
  3923. }
  3924. else
  3925. {
  3926. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3927. retFail++;
  3928. }
  3929. if (uartFd > 0)
  3930. {
  3931. close(uartFd);
  3932. }
  3933. memset(Buf, 0, sizeof(Buf));
  3934. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3935. system(Buf);
  3936. break;
  3937. case _UPDATE_TYPE_CHADEMO:
  3938. case _UPDATE_TYPE_GBT:
  3939. CanFd = InitCanBus();
  3940. if (CanFd > 0)
  3941. {
  3942. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3943. {
  3944. if ((Type == _UPDATE_TYPE_CHADEMO && chargingInfo[index]->Type == _Type_Chademo) ||
  3945. (Type == _UPDATE_TYPE_GBT && chargingInfo[index]->Type == _Type_GB))
  3946. {
  3947. PRINTF_FUNC("Upgrade GBT / Chademo Processing..target id = %d \n", chargingInfo[index]->Evboard_id);
  3948. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3949. retSucc++;
  3950. else
  3951. {
  3952. if (chargingInfo[index]->Type == _Type_Chademo)
  3953. PRINTF_FUNC("Upgrade Chademo Failed \n");
  3954. else
  3955. PRINTF_FUNC("Upgrade GBT Failed \n");
  3956. retFail++;
  3957. }
  3958. }
  3959. }
  3960. close(CanFd);
  3961. }
  3962. memset(Buf, 0, sizeof(Buf));
  3963. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3964. system(Buf);
  3965. break;
  3966. }
  3967. }
  3968. close(fd);
  3969. }
  3970. }
  3971. free(dir);
  3972. closedir(d);
  3973. PRINTF_FUNC("Upgrade Result : PASS : %d, FAIL : %d \n", retSucc, retFail);
  3974. sleep(1);
  3975. system("rm -rvf /mnt/* ");
  3976. if (retFail != 0)
  3977. {
  3978. result = FAIL;
  3979. }
  3980. return result;
  3981. }
  3982. void CreateRfidFork()
  3983. {
  3984. pid_t rfidRecPid;
  3985. rfidRecPid = fork();
  3986. if (rfidRecPid == 0)
  3987. {
  3988. while(true)
  3989. {
  3990. // 刷卡判斷
  3991. RFID rfid;
  3992. if (!ShmSysConfigAndInfo->SysConfig.isRFID)
  3993. {}
  3994. else if(getRequestCardSN(rfidFd, 0, &rfid))
  3995. {
  3996. //PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3997. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  3998. {
  3999. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  4000. {
  4001. switch (rfid.snType)
  4002. {
  4003. case RFID_SN_TYPE_6BYTE:
  4004. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4005. "%02X%02X%02X%02X%02X%02X",
  4006. rfid.currentCard[0], rfid.currentCard[1],
  4007. rfid.currentCard[2], rfid.currentCard[3],
  4008. rfid.currentCard[4], rfid.currentCard[5]);
  4009. break;
  4010. case RFID_SN_TYPE_7BYTE:
  4011. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4012. "%02X%02X%02X%02X%02X%02X%02X",
  4013. rfid.currentCard[0], rfid.currentCard[1],
  4014. rfid.currentCard[2], rfid.currentCard[3],
  4015. rfid.currentCard[4], rfid.currentCard[5],
  4016. rfid.currentCard[6]);
  4017. break;
  4018. case RFID_SN_TYPE_10BYTE:
  4019. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4020. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4021. rfid.currentCard[0], rfid.currentCard[1],
  4022. rfid.currentCard[2], rfid.currentCard[3],
  4023. rfid.currentCard[4], rfid.currentCard[5],
  4024. rfid.currentCard[6], rfid.currentCard[7],
  4025. rfid.currentCard[8], rfid.currentCard[9]);
  4026. break;
  4027. case RFID_SN_TYPE_4BYTE:
  4028. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4029. "%02X%02X%02X%02X",
  4030. rfid.currentCard[0], rfid.currentCard[1],
  4031. rfid.currentCard[2], rfid.currentCard[3]);
  4032. break;
  4033. }
  4034. }
  4035. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  4036. {
  4037. switch (rfid.snType)
  4038. {
  4039. case RFID_SN_TYPE_6BYTE:
  4040. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4041. "%02X%02X%02X%02X%02X%02X",
  4042. rfid.currentCard[5], rfid.currentCard[4],
  4043. rfid.currentCard[3], rfid.currentCard[2],
  4044. rfid.currentCard[1], rfid.currentCard[0]);
  4045. break;
  4046. case RFID_SN_TYPE_7BYTE:
  4047. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4048. "%02X%02X%02X%02X%02X%02X%02X",
  4049. rfid.currentCard[6], rfid.currentCard[5],
  4050. rfid.currentCard[4], rfid.currentCard[3],
  4051. rfid.currentCard[2], rfid.currentCard[1],
  4052. rfid.currentCard[0]);
  4053. break;
  4054. case RFID_SN_TYPE_10BYTE:
  4055. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4056. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4057. rfid.currentCard[9], rfid.currentCard[8],
  4058. rfid.currentCard[7], rfid.currentCard[6],
  4059. rfid.currentCard[5], rfid.currentCard[4],
  4060. rfid.currentCard[3], rfid.currentCard[2],
  4061. rfid.currentCard[1], rfid.currentCard[0]);
  4062. break;
  4063. case RFID_SN_TYPE_4BYTE:
  4064. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4065. "%02X%02X%02X%02X",
  4066. rfid.currentCard[3], rfid.currentCard[2],
  4067. rfid.currentCard[1], rfid.currentCard[0]);
  4068. break;
  4069. }
  4070. }
  4071. PRINTF_FUNC("RFID : Get card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4072. }
  4073. }
  4074. sleep(1);
  4075. }
  4076. }
  4077. }
  4078. void CreateCheckSystemTaskFork()
  4079. {
  4080. pid_t taskPid;
  4081. taskPid = fork();
  4082. if (taskPid == 0)
  4083. {
  4084. bool stopToDet = false;
  4085. while(true)
  4086. {
  4087. if (!stopToDet)
  4088. {
  4089. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4090. {
  4091. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE ||
  4092. chargingInfo[_index]->Type == 0x09)
  4093. {
  4094. stopToDet = true;
  4095. continue;
  4096. }
  4097. }
  4098. if (!stopToDet)
  4099. CheckSystemTaskAlive();
  4100. }
  4101. sleep(5);
  4102. }
  4103. }
  4104. }
  4105. void StartSystemTimeoutDet(unsigned char flag)
  4106. {
  4107. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  4108. {
  4109. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4110. }
  4111. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  4112. }
  4113. void StopSystemTimeoutDet()
  4114. {
  4115. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4116. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  4117. }
  4118. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  4119. {
  4120. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4121. {
  4122. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  4123. {
  4124. GetTimespecFunc(&chargingInfo[gunIndex]->ConnectorTimeout);
  4125. }
  4126. chargingInfo[gunIndex]->TimeoutFlag = flag;
  4127. }
  4128. }
  4129. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  4130. {
  4131. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4132. {
  4133. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  4134. }
  4135. }
  4136. void CheckConnectionTimeout()
  4137. {
  4138. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4139. {
  4140. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4141. }
  4142. else
  4143. {
  4144. _connectionTimeout = _ocpp_get_connect_timeout();
  4145. if(_connectionTimeout <= 0)
  4146. {
  4147. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4148. }
  4149. }
  4150. }
  4151. void CreateTimeoutFork()
  4152. {
  4153. pid_t timeoutPid;
  4154. timeoutPid = fork();
  4155. if (timeoutPid > 0)
  4156. {
  4157. GetTimespecFunc(&_cmdSubPriority_time);
  4158. CheckConnectionTimeout();
  4159. int _timebuf = 0;
  4160. while(true)
  4161. {
  4162. _timebuf = GetTimeoutValue(&_cmdSubPriority_time);
  4163. if (_timebuf < 0)
  4164. {
  4165. GetTimespecFunc(&_cmdSubPriority_time);
  4166. }
  4167. else
  4168. {
  4169. if (_timebuf >= 5)
  4170. {
  4171. CheckConnectionTimeout();
  4172. GetTimespecFunc(&_cmdSubPriority_time);
  4173. }
  4174. }
  4175. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  4176. // 系統
  4177. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_None)
  4178. {
  4179. _timebuf = GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4180. if (_timebuf < 0)
  4181. {
  4182. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4183. }
  4184. else
  4185. {
  4186. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  4187. {
  4188. case Timeout_SelftestChk:
  4189. if (_timebuf >= SELFTEST_TIMEOUT)
  4190. {
  4191. _SelfTestTimeout();
  4192. StopSystemTimeoutDet();
  4193. }
  4194. break;
  4195. case Timeout_Authorizing:
  4196. if (_timebuf >= AUTHORIZE_TIMEOUT)
  4197. {
  4198. _AuthorizedTimeout();
  4199. StopSystemTimeoutDet();
  4200. }
  4201. break;
  4202. case Timeout_VerifyFail:
  4203. if (_timebuf >= AUTHORIZE_FAIL_TIMEOUT)
  4204. {
  4205. _AutoReturnTimeout();
  4206. StopSystemTimeoutDet();
  4207. }
  4208. break;
  4209. case Timeout_VerifyComp:
  4210. if (_timebuf >= AUTHORIZE_COMP_TIMEOUT)
  4211. {
  4212. _AutoReturnTimeout();
  4213. StopSystemTimeoutDet();
  4214. }
  4215. break;
  4216. case Timeout_WaitPlug:
  4217. if (_timebuf >= _connectionTimeout)
  4218. {
  4219. _DetectPlugInTimeout();
  4220. StopSystemTimeoutDet();
  4221. }
  4222. break;
  4223. case Timeout_ReturnToChargingGunDet:
  4224. {
  4225. if (_timebuf >= RETURN_TO_CHARGING_PAGE)
  4226. {
  4227. DisplayChargingInfo();
  4228. StopSystemTimeoutDet();
  4229. }
  4230. }
  4231. break;
  4232. // case Timeout_AuthorizingForStop:
  4233. // {
  4234. // if (_timebuf / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4235. // {
  4236. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4237. // ClearAuthorizedFlag();
  4238. // StopSystemTimeoutDet();
  4239. // }
  4240. // }
  4241. // break;
  4242. }
  4243. }
  4244. }
  4245. // 各槍
  4246. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4247. {
  4248. if (chargingInfo[gun_index]->TimeoutFlag != Timeout_None)
  4249. {
  4250. _timebuf = GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout);
  4251. if (_timebuf < 0)
  4252. {
  4253. GetTimespecFunc(&chargingInfo[gun_index]->ConnectorTimeout);
  4254. }
  4255. else
  4256. {
  4257. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4258. switch(chargingInfo[gun_index]->TimeoutFlag)
  4259. {
  4260. case Timeout_Preparing:
  4261. {
  4262. if (_timebuf >= GUN_PREPARE_TIMEOUT)
  4263. {
  4264. _PrepareTimeout(gun_index);
  4265. StopGunInfoTimeoutDet(gun_index);
  4266. }
  4267. }
  4268. break;
  4269. case Timeout_EvChargingDet:
  4270. {
  4271. if (_timebuf >= GUN_EV_WAIT_TIMEOUT)
  4272. {
  4273. _DetectEvChargingEnableTimeout(gun_index);
  4274. StopGunInfoTimeoutDet(gun_index);
  4275. }
  4276. }
  4277. break;
  4278. case Timeout_EvseChargingDet:
  4279. {
  4280. if (chargingInfo[gun_index]->Type == _Type_GB)
  4281. {
  4282. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT * 3)
  4283. {
  4284. _DetectEvseChargingEnableTimeout(gun_index);
  4285. StopGunInfoTimeoutDet(gun_index);
  4286. }
  4287. }
  4288. else
  4289. {
  4290. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT)
  4291. {
  4292. _DetectEvseChargingEnableTimeout(gun_index);
  4293. StopGunInfoTimeoutDet(gun_index);
  4294. }
  4295. }
  4296. }
  4297. break;
  4298. case Timeout_EvseCompleteDet:
  4299. {
  4300. if (_timebuf >= GUN_COMP_WAIT_TIMEOUT)
  4301. {
  4302. StopGunInfoTimeoutDet(gun_index);
  4303. }
  4304. }
  4305. break;
  4306. case Timeout_ForCcsPrechargeDet:
  4307. {
  4308. if (_timebuf >= GUN_PRECHARGING_TIMEOUT)
  4309. {
  4310. _CcsPrechargeTimeout(gun_index);
  4311. StopGunInfoTimeoutDet(gun_index);
  4312. }
  4313. }
  4314. break;
  4315. }
  4316. }
  4317. }
  4318. }
  4319. sleep(1);
  4320. }
  4321. }
  4322. }
  4323. void GetSystemTime()
  4324. {
  4325. struct timeb csuTime;
  4326. struct tm *tmCSU;
  4327. ftime(&csuTime);
  4328. tmCSU = localtime(&csuTime.time);
  4329. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4330. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4331. tmCSU->tm_sec);
  4332. // byte date[14];
  4333. //
  4334. //
  4335. // //sprintf(&date, "%d", );
  4336. //
  4337. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4338. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4339. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4340. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4341. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4342. //
  4343. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4344. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4345. //
  4346. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4347. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4348. //
  4349. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4350. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4351. //
  4352. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4353. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4354. //
  4355. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4356. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4357. // 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],
  4358. // date[4], date[5], date[6], date[7],
  4359. // date[8], date[9], date[10], date[11],
  4360. // date[12], date[13]);
  4361. }
  4362. void CheckFactoryConfigFunction()
  4363. {
  4364. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4365. {
  4366. char Buf[256];
  4367. sleep(5);
  4368. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  4369. ShmSysConfigAndInfo->SysConfig.ModelName,
  4370. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  4371. system(Buf);
  4372. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4373. system("sync");
  4374. sleep(5);
  4375. system("reboot -f");
  4376. sleep(5);
  4377. system("reboot -f");
  4378. }
  4379. }
  4380. void CheckFwUpdateFunction()
  4381. {
  4382. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  4383. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4384. {
  4385. DEBUG_INFO_MSG("ftp : update start. \n");
  4386. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4387. {
  4388. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4389. }
  4390. CloseWatchDog();
  4391. sleep(2);
  4392. KillTask();
  4393. byte updateResult = CheckUpdateProcess();
  4394. if (updateResult == PASS)
  4395. DEBUG_INFO_MSG("ftp : update complete. \n");
  4396. else if (updateResult == MODELNAME_FAIL)
  4397. {
  4398. DEBUG_INFO_MSG("ftp : model name is none match. \n");
  4399. KillAllTask();
  4400. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4401. sleep(5);
  4402. system("/usr/bin/run_evse_restart.sh");
  4403. return;
  4404. }
  4405. else
  4406. DEBUG_INFO_MSG("ftp : update fail. \n");
  4407. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4408. sleep(5);
  4409. system("reboot -f");
  4410. }
  4411. else
  4412. {
  4413. ocpp_chk_update_cmd();
  4414. }
  4415. }
  4416. int TariffParsing(char *StringItem, char *TariffCode)
  4417. {
  4418. char *ptrSave, *ptrToken;
  4419. char strSource[128];
  4420. int fee = 0;
  4421. memcpy(strSource, StringItem, 128);
  4422. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4423. while(ptrToken != NULL)
  4424. {
  4425. char *strTariff = strstr(ptrToken, TariffCode);
  4426. if(strTariff != NULL)
  4427. {
  4428. char *strFee = strstr(strTariff, "$");
  4429. if(strFee != NULL)
  4430. {
  4431. strFee++;
  4432. float fFee = atof(strFee);
  4433. fee = (int)(fFee * 100);
  4434. break;
  4435. }
  4436. }
  4437. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4438. }
  4439. return fee;
  4440. }
  4441. void DefaultPriceHandlerOcpp16(char *StrDefaultPrice)
  4442. {
  4443. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4444. PRINTF_FUNC("DefaultPriceHandler - StrDefaultPrice = %s \n", StrDefaultPrice);
  4445. connectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4446. currentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4447. occupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4448. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4449. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4450. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4451. ShmDcCommonData->balanceInfo.defaultPrice = currentRate;
  4452. }
  4453. void UserPriceHandlerOcpp16(char *UserId, char *UserPrice)
  4454. {
  4455. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4456. int accountBalance = 0;
  4457. byte acGunIndex = 0;
  4458. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4459. {
  4460. if (gunIndex == 1)
  4461. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4462. else
  4463. acGunIndex = 0;
  4464. if((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4465. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) &&
  4466. strcmp((char *)chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4467. {
  4468. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4469. currentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4470. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4471. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4472. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice != currentRate ||
  4473. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4474. {
  4475. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4476. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4477. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4478. PRINTF_FUNC("*** Account Balance: %d.%02d ***", (accountBalance / 100), (accountBalance % 100));
  4479. PRINTF_FUNC("*** Connector Id %d User Price %d.%02d, Account Balance %d.%02d ***",
  4480. (gunIndex), (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4481. }
  4482. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice = currentRate;
  4483. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4484. }
  4485. }
  4486. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4487. {
  4488. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4489. if(strcmp((char *)ac_chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4490. {
  4491. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice != currentRate ||
  4492. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance != accountBalance)
  4493. {
  4494. PRINTF_FUNC("*** AC Connector User Price %d.%02d, Account Balance %d.%02d ***",
  4495. (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4496. }
  4497. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice = currentRate;
  4498. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4499. }
  4500. }
  4501. }
  4502. void RunningFinalCostHandlerOcpp16()
  4503. {
  4504. unsigned int connectionFee = 0, sessionFee = 0, occupancyFee = 0, totalCost = 0;
  4505. int accountBalance = 0;
  4506. byte acGunIndex = 0;
  4507. bool isFind = false;
  4508. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4509. {
  4510. isFind = false;
  4511. if (gunIndex == 1)
  4512. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4513. else
  4514. acGunIndex = 0;
  4515. // PRINTF_FUNC("*** gunIndex = %d, description = %s *** \n", gunIndex + acGunIndex,
  4516. // ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description);
  4517. // PRINTF_FUNC("*** gunIndex = %d, f description = %s *** \n", gunIndex + acGunIndex,
  4518. // ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description);
  4519. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4520. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4521. {
  4522. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description) > 0)
  4523. {
  4524. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Connection Fee");
  4525. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Session Fee");
  4526. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4527. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Total Cost");
  4528. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Account Balance");
  4529. PRINTF_FUNC("*** Connector Id: %d Running Cost *** \n", gunIndex);
  4530. isFind = true;
  4531. memset(&ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4532. }
  4533. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description) > 0)
  4534. {
  4535. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Connection Fee");
  4536. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Session Fee");
  4537. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4538. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Total Cost");
  4539. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Account Balance");
  4540. PRINTF_FUNC("********** Connector Id: %d Final Cost ********** \n", gunIndex);
  4541. isFind = true;
  4542. memset(&ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4543. }
  4544. if (isFind)
  4545. {
  4546. PRINTF_FUNC("*** Connection Fee: %d.%02d *** \n", (connectionFee / 100), (connectionFee % 100));
  4547. PRINTF_FUNC("*** Session Fee: %d.%02d *** \n", (sessionFee / 100), (sessionFee % 100));
  4548. PRINTF_FUNC("*** Occupancy Fee: %d.%02d *** \n", (occupancyFee / 100), (occupancyFee % 100));
  4549. PRINTF_FUNC("*** Total Cost: %d.%02d *** \n", (totalCost / 100), (totalCost % 100));
  4550. PRINTF_FUNC("*** Account Balance: %d.%02d *** \n", (accountBalance / 100), (accountBalance % 100));
  4551. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4552. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4553. {
  4554. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d *** \n",
  4555. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4556. }
  4557. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost = totalCost;
  4558. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4559. }
  4560. }
  4561. }
  4562. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4563. {
  4564. isFind = false;
  4565. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4566. if (ac_chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_PREPARING &&
  4567. ac_chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4568. {
  4569. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description) > 0)
  4570. {
  4571. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Connection Fee");
  4572. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Session Fee");
  4573. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Occupancy Fee");
  4574. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Total Cost");
  4575. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Account Balance");
  4576. PRINTF_FUNC("*** AC Connector : Running Cost ***\n");
  4577. isFind = true;
  4578. memset(&ShmOCPP16Data->Cost.RunningCost[acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4579. }
  4580. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description) > 0)
  4581. {
  4582. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Connection Fee");
  4583. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Session Fee");
  4584. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Occupancy Fee");
  4585. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Total Cost");
  4586. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Account Balance");
  4587. PRINTF_FUNC("*** AC Connector : Final Cost ***\n");
  4588. isFind = true;
  4589. memset(&ShmOCPP16Data->Cost.FinalCost[acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4590. }
  4591. if (isFind)
  4592. {
  4593. PRINTF_FUNC("*** Connection Fee: %d.%02d ***\n", (connectionFee / 100), (connectionFee % 100));
  4594. PRINTF_FUNC("*** Session Fee: %d.%02d ***\n", (sessionFee / 100), (sessionFee % 100));
  4595. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***\n", (occupancyFee / 100), (occupancyFee % 100));
  4596. PRINTF_FUNC("*** Total Cost: %d.%02d ***\n", (totalCost / 100), (totalCost % 100));
  4597. PRINTF_FUNC("*** Account Balance: %d.%02d ***\n", (accountBalance / 100), (accountBalance % 100));
  4598. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4599. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4600. {
  4601. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d ***\n",
  4602. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4603. }
  4604. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].TotalCost = totalCost;
  4605. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4606. }
  4607. }
  4608. }
  4609. }
  4610. //===============================================
  4611. // ocpp key
  4612. //===============================================
  4613. bool ocpp_authorizeRemoteTxReqChk_key()
  4614. {
  4615. bool result = false;
  4616. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4617. {
  4618. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  4619. result = true;
  4620. }
  4621. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4622. {
  4623. }
  4624. return result;
  4625. }
  4626. //===============================================
  4627. // Check reservation date is expired
  4628. //===============================================
  4629. int opcc_chk_reserve_expired(byte gun_index)
  4630. {
  4631. int result = NO;
  4632. byte acDirIndex = 0;
  4633. if (gun_index == 1)
  4634. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4636. {
  4637. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4638. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate);
  4639. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate))
  4640. result = YES;
  4641. }
  4642. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4643. {
  4644. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4645. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime);
  4646. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime))
  4647. result = YES;
  4648. }
  4649. return result;
  4650. }
  4651. //===============================================
  4652. // AC OCPP routine
  4653. //===============================================
  4654. void ocpp_ac_chk_availability_cmd()
  4655. {
  4656. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4657. byte type = 0;
  4658. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4659. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4660. {
  4661. if (ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4662. {
  4663. PRINTF_FUNC("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4664. DEBUG_ERROR_MSG("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4665. ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4666. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Operative") == EQUAL)
  4667. {
  4668. if (isDb_ready)
  4669. DB_Update_Operactive(localDb, hasDc, true);
  4670. type = 1;
  4671. }
  4672. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Inoperative") == EQUAL)
  4673. {
  4674. if (isDb_ready)
  4675. DB_Update_Operactive(localDb, hasDc, false);
  4676. type = 2;
  4677. }
  4678. }
  4679. }
  4680. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4681. {
  4682. if (ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4683. {
  4684. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4685. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4686. ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4687. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Operative") == EQUAL)
  4688. {
  4689. if (isDb_ready)
  4690. DB_Update_Operactive(localDb, hasDc, true);
  4691. type = 1;
  4692. }
  4693. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Inoperative") == EQUAL)
  4694. {
  4695. if (isDb_ready)
  4696. DB_Update_Operactive(localDb, hasDc, false);
  4697. type = 2;
  4698. }
  4699. }
  4700. }
  4701. if (type == 1)
  4702. {
  4703. ac_chargingInfo[0]->IsAvailable = YES;
  4704. }
  4705. else if (type == 2)
  4706. {
  4707. ac_chargingInfo[0]->IsAvailable = NO;
  4708. }
  4709. }
  4710. void ocpp_ac_chk_unlock_cmd()
  4711. {
  4712. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4713. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4715. {
  4716. if (ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4717. {
  4718. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4719. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4720. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4721. {
  4722. // Unlocked - 充電中,需停止充電
  4723. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "UnlockCommand");
  4724. ac_chargingInfo[0]->StopChargeFlag = YES;
  4725. }
  4726. strcpy((char *)ShmOCPP16Data->UnlockConnector[hasDc].ResponseStatus, "Unlocked");
  4727. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4728. }
  4729. }
  4730. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4731. {
  4732. if (ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4733. {
  4734. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4735. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4736. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4737. {
  4738. // Unlocked - 充電中,需停止充電
  4739. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "UnlockCommand");
  4740. ac_chargingInfo[0]->StopChargeFlag = YES;
  4741. }
  4742. strcpy((char*)ShmOCPP20Data->UnlockConnector[hasDc].Response_status, "Unlocked");
  4743. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4744. }
  4745. }
  4746. }
  4747. void ocpp_ac_chk_profileConf_cmd()
  4748. {
  4749. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4750. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4751. {
  4752. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4753. {
  4754. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4755. }
  4756. }
  4757. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4758. {
  4759. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4760. {
  4761. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4762. }
  4763. }
  4764. }
  4765. void ocpp_ac_chargingProfile_process()
  4766. {
  4767. int _time = 0;
  4768. int _startCount = NO_DEFINE;
  4769. int _maxCount = 0;
  4770. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4771. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4772. {
  4773. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileKind, "Absolute") == EQUAL &&
  4774. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileId == YES)
  4775. {
  4776. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.StartSchedule);
  4777. if (_time > -1)
  4778. {
  4779. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod);
  4780. _startCount = NO_DEFINE;
  4781. for (byte _count = 0; _count < _maxCount; _count++)
  4782. {
  4783. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4784. if (_time >= ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  4785. {
  4786. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  4787. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  4788. {
  4789. _startCount = _count;
  4790. }
  4791. }
  4792. }
  4793. if (_startCount < _maxCount)
  4794. {
  4795. //PRINTF_FUNC("AC - Gun_%d, Get Profile - Profile Index = %d \n", hasDc, _startCount);
  4796. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4797. if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit >= 1 &&
  4798. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit <= 5)
  4799. {
  4800. ac_chargingInfo[0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  4801. }
  4802. else if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit == 0)
  4803. {
  4804. ac_chargingInfo[0]->ChargingProfileCurrent = AC_STOP_DUTY;
  4805. }
  4806. else
  4807. {
  4808. ac_chargingInfo[0]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit;
  4809. }
  4810. }
  4811. else
  4812. {
  4813. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4814. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4815. }
  4816. }
  4817. else
  4818. {
  4819. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4820. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4821. }
  4822. }
  4823. else
  4824. {
  4825. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4826. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4827. }
  4828. }
  4829. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4830. {
  4831. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[hasDc].chargingProfileKind, "Absolute") == EQUAL &&
  4832. ShmOCPP20Data->SmartChargingProfile[hasDc].id == YES)
  4833. {
  4834. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].startSchedule);
  4835. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod);
  4836. _startCount = NO_DEFINE;
  4837. for (byte _count = 0; _count < _maxCount; _count++)
  4838. {
  4839. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4840. if (_time >= ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  4841. {
  4842. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  4843. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  4844. {
  4845. _startCount = _count;
  4846. }
  4847. }
  4848. }
  4849. if (_startCount < _maxCount)
  4850. {
  4851. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", 1, _startCount);
  4852. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  4853. if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit >= 1 &&
  4854. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit <= 5)
  4855. {
  4856. ac_chargingInfo [0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  4857. }
  4858. else if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit == 0)
  4859. {
  4860. ac_chargingInfo [0]->ChargingProfileCurrent = AC_STOP_DUTY;
  4861. }
  4862. else
  4863. {
  4864. ac_chargingInfo [0]->ChargingProfileCurrent = ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit;
  4865. }
  4866. }
  4867. else
  4868. {
  4869. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4870. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4871. }
  4872. }
  4873. else
  4874. {
  4875. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4876. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4877. }
  4878. }
  4879. else
  4880. {
  4881. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4882. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4883. }
  4884. if (ac_chargingInfo[0]->ChargingProfileCurrent >= 0 &&
  4885. _AcChargingProfileCurBuffer != ac_chargingInfo[0]->ChargingProfileCurrent)
  4886. {
  4887. _AcChargingProfileCurBuffer = ac_chargingInfo[0]->ChargingProfileCurrent;
  4888. PRINTF_FUNC ( "AC Profile (duty) : ChargingProfileCurrent = %.2f \n", ac_chargingInfo[0]->ChargingProfileCurrent);
  4889. }
  4890. }
  4891. void ocpp_ac_lift_profileReq_cmd(byte gunIndex)
  4892. {
  4893. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4894. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4895. {
  4896. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  4897. {
  4898. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  4899. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  4900. }
  4901. }
  4902. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4903. {
  4904. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  4905. {
  4906. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  4907. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  4908. }
  4909. }
  4910. }
  4911. int opcc_ac_chk_reserve_expired()
  4912. {
  4913. int result = NO;
  4914. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4915. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4916. {
  4917. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  4918. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate);
  4919. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate))
  4920. result = YES;
  4921. }
  4922. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4923. {
  4924. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  4925. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime);
  4926. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime))
  4927. result = YES;
  4928. }
  4929. return result;
  4930. }
  4931. void ocpp_ac_chk_reserved_cmd()
  4932. {
  4933. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4934. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4935. {
  4936. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  4937. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  4938. {
  4939. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  4940. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  4941. if ( ! opcc_ac_chk_reserve_expired ())
  4942. {
  4943. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  4944. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  4945. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  4946. ac_chargingInfo [0]->ReservationId = ShmOCPP16Data->ReserveNow [hasDc].ReservationId;
  4947. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  4948. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0],
  4949. (char *) ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  4950. }
  4951. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  4952. }
  4953. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  4954. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  4955. {
  4956. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  4957. if (ShmOCPP16Data->ReserveNow [hasDc].ReservationId == ac_chargingInfo [0]->ReservationId)
  4958. {
  4959. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  4960. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  4961. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  4962. ac_chargingInfo [0]->ReservationId = -1;
  4963. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  4964. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0], "" );
  4965. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  4966. }
  4967. }
  4968. }
  4969. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4970. {
  4971. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  4972. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  4973. {
  4974. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  4975. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  4976. if ( ! opcc_ac_chk_reserve_expired ())
  4977. {
  4978. PRINTF_FUNC ( "id = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  4979. PRINTF_FUNC ( "idToken = %s \n", ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken );
  4980. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  4981. ac_chargingInfo [0]->ReservationId = ShmOCPP20Data->ReserveNow [hasDc].id;
  4982. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  4983. strncpy ( (char *) ShmDcCommonData->_reserved_UserId,
  4984. (char *) ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken, 30 );
  4985. }
  4986. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  4987. }
  4988. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  4989. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  4990. {
  4991. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  4992. if (ShmOCPP20Data->ReserveNow [hasDc].id == ac_chargingInfo [0]->ReservationId)
  4993. {
  4994. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  4995. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  4996. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  4997. ac_chargingInfo [0]->ReservationId = -1;
  4998. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  4999. strcpy ( (char *) ShmDcCommonData->_reserved_UserId, "" );
  5000. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5001. }
  5002. }
  5003. }
  5004. }
  5005. //===============================================
  5006. // OCPP routine
  5007. //===============================================
  5008. void ocpp_process_start()
  5009. {
  5010. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5011. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5012. {
  5013. if(system("pidof -s OcppBackend > /dev/null") != 0)
  5014. {
  5015. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  5016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5017. system("/root/OcppBackend &");
  5018. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5019. system("/root/OcppBackend20 &");
  5020. }
  5021. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5022. {
  5023. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  5024. {
  5025. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  5026. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5027. system("pkill OcppBackend");
  5028. }
  5029. }
  5030. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5031. {
  5032. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  5033. {
  5034. DEBUG_ERROR_MSG("OcppBackend20 watch dog timeout task restart.\n");
  5035. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5036. system("pkill OcppBackend20");
  5037. }
  5038. }
  5039. }
  5040. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  5041. {
  5042. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  5043. {
  5044. DEBUG_ERROR_MSG("OcppBackendPH not running, restart it.\r\n");
  5045. system("/root/OcppBackendPH &");
  5046. }
  5047. }
  5048. }
  5049. void ocpp_auto_response_BootNotification()
  5050. {
  5051. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5052. {
  5053. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  5054. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  5055. }
  5056. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5057. {
  5058. if (ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES)
  5059. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = NO;
  5060. }
  5061. }
  5062. bool ocpp_is_resPass_StartTransactionConf(byte gun_index)
  5063. {
  5064. bool result = false;
  5065. byte acDirIndex = 0;
  5066. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5067. if (gun_index == 1)
  5068. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5069. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5070. {
  5071. result = ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf;
  5072. }
  5073. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5074. {
  5075. result = ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf;
  5076. }
  5077. return result;
  5078. }
  5079. void ocpp_auto_response_StartTransationConf(byte gun_index)
  5080. {
  5081. byte acDirIndex = 0;
  5082. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5083. if (gun_index == 1)
  5084. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5085. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5086. {
  5087. if (ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5088. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5089. }
  5090. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5091. {
  5092. if (ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf)
  5093. ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf = NO;
  5094. }
  5095. }
  5096. void ocpp_auto_response_ReserveConf(byte gun_index)
  5097. {
  5098. byte acDirIndex = 0;
  5099. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5100. if (gun_index == 1)
  5101. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5102. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5103. ShmOCPP16Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5104. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5105. ShmOCPP20Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5106. }
  5107. bool ocpp_chk_authrization_cmd()
  5108. {
  5109. char buf2[16] = "";
  5110. memset(buf2, 0, ARRAY_SIZE(buf2));
  5111. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5112. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  5113. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5114. sprintf(buf2, "%s", ShmOCPP20Data->Authorize.Response_idTokenInfo.status);
  5115. // 因為無法得知實際的長度,所以只能用搜尋的方式
  5116. if(strcmp(buf2, "Accepted") == EQUAL)
  5117. return true;
  5118. return false;
  5119. }
  5120. bool opcc_sub_get_reset_req()
  5121. {
  5122. bool result = false;
  5123. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5124. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  5125. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5126. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  5127. return result;
  5128. }
  5129. void ocpp_sub_run_reset(bool canReset)
  5130. {
  5131. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5132. {
  5133. if(canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5134. {
  5135. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5136. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5137. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5138. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5139. sleep(3);
  5140. system("reboot -f");
  5141. }
  5142. else if (canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  5143. {
  5144. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5145. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5146. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5147. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5148. sleep(3);
  5149. KillAllTask();
  5150. CloseWatchDog();
  5151. system("/usr/bin/run_evse_restart.sh");
  5152. }
  5153. }
  5154. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5155. {
  5156. if(canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5157. {
  5158. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5159. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5160. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5161. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5162. sleep(3);
  5163. system("reboot -f");
  5164. }
  5165. else if (canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "OnIdle") == EQUAL)
  5166. {
  5167. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5168. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5169. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5170. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5171. sleep(3);
  5172. KillAllTask();
  5173. CloseWatchDog();
  5174. system("/usr/bin/run_evse_restart.sh");
  5175. }
  5176. }
  5177. }
  5178. void ocpp_chk_reset_cmd(byte isforce)
  5179. {
  5180. if (opcc_sub_get_reset_req())
  5181. {
  5182. bool canReset = true;
  5183. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  5184. {
  5185. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5186. {
  5187. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5188. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  5189. {
  5190. // Reset - AC 先停止充電
  5191. ac_chargingInfo[0]->StopChargeFlag = YES;
  5192. GetTimespecFunc(&_resetChkTime);
  5193. }
  5194. }
  5195. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5196. {
  5197. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_REASSIGN &&
  5198. chargingInfo[_index]->SystemStatus <= SYS_MODE_TERMINATING)
  5199. {
  5200. canReset = false;
  5201. // Reset 系統先停止充電
  5202. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5203. {
  5204. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5205. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5206. else
  5207. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5208. }
  5209. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5210. {
  5211. if(strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5212. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5213. else
  5214. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5215. }
  5216. ChargingTerminalProcess(_index);
  5217. GetTimespecFunc(&_resetChkTime);
  5218. }
  5219. }
  5220. }
  5221. if (canReset)
  5222. {
  5223. if (GetTimeoutValue(&_resetChkTime) < 0)
  5224. GetTimespecFunc(&_resetChkTime);
  5225. if (GetTimeoutValue(&_resetChkTime) >= 5)
  5226. {
  5227. ShmDcCommonData->evBoardResetFlag = YES;
  5228. // Reset -> change status to maintain.
  5229. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5230. {
  5231. setChargerMode(_index, SYS_MODE_MAINTAIN);
  5232. }
  5233. ChangeLcmByIndex(_LCM_FIX);
  5234. }
  5235. else
  5236. canReset = false;
  5237. }
  5238. else
  5239. GetTimespecFunc(&_resetChkTime);
  5240. if (isforce == YES)
  5241. canReset = true;
  5242. ocpp_sub_run_reset(canReset);
  5243. }
  5244. }
  5245. void ocpp_chk_reserved_cmd(byte gunIndex)
  5246. {
  5247. byte acDirIndex = 0;
  5248. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5249. if (gunIndex == 1)
  5250. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5251. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5252. {
  5253. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5254. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5255. {
  5256. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5257. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5258. if (!opcc_chk_reserve_expired(gunIndex))
  5259. {
  5260. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5261. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5262. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5263. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  5264. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5265. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5266. (char *)ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5267. }
  5268. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5269. }
  5270. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5271. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5272. {
  5273. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5274. if (ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId == chargingInfo[gunIndex]->ReservationId)
  5275. {
  5276. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5277. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5278. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5279. chargingInfo[gunIndex]->ReservationId = -1;
  5280. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5281. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5282. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5283. }
  5284. }
  5285. }
  5286. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5287. {
  5288. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5289. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5290. {
  5291. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5292. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5293. if (!opcc_chk_reserve_expired(gunIndex))
  5294. {
  5295. PRINTF_FUNC("id = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5296. PRINTF_FUNC("idToken = %s \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken);
  5297. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5298. chargingInfo[gunIndex]->ReservationId = ShmOCPP20Data->ReserveNow[gunIndex].id;
  5299. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5300. strncpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5301. (char *)ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken, 30);
  5302. }
  5303. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5304. }
  5305. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5306. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5307. {
  5308. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5309. if (ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id == chargingInfo[gunIndex]->ReservationId)
  5310. {
  5311. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5312. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5313. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5314. chargingInfo[gunIndex]->ReservationId = -1;
  5315. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5316. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5317. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5318. }
  5319. }
  5320. }
  5321. }
  5322. void ocpp_chk_availability_cmd(byte gunIndex)
  5323. {
  5324. byte type = 0; // 0 : none, 1 : operative, 2 inoperative
  5325. byte acDirIndex = 0;
  5326. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5327. if (gunIndex == 1)
  5328. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5329. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5330. {
  5331. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5332. {
  5333. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5334. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5335. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5336. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Operative") == EQUAL)
  5337. {
  5338. if (isDb_ready)
  5339. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5340. type = 1;
  5341. }
  5342. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Inoperative") == EQUAL)
  5343. {
  5344. if (isDb_ready)
  5345. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5346. type = 2;
  5347. }
  5348. }
  5349. }
  5350. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5351. {
  5352. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5353. {
  5354. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5355. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5356. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5357. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Operative") == EQUAL)
  5358. {
  5359. if (isDb_ready)
  5360. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5361. type = 1;
  5362. }
  5363. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Inoperative") == EQUAL)
  5364. {
  5365. if (isDb_ready)
  5366. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5367. type = 2;
  5368. }
  5369. }
  5370. }
  5371. if (type == 1)
  5372. {
  5373. chargingInfo[gunIndex]->IsAvailable = YES;
  5374. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5375. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5376. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5377. {
  5378. setChargerMode(gunIndex, SYS_MODE_IDLE);
  5379. }
  5380. }
  5381. else if (type == 2)
  5382. {
  5383. chargingInfo[gunIndex]->IsAvailable = NO;
  5384. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5385. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5386. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5387. {
  5388. setChargerMode(gunIndex, SYS_MODE_MAINTAIN);
  5389. }
  5390. }
  5391. }
  5392. void ocpp_chk_unlock_cmd(byte gunIndex)
  5393. {
  5394. byte acDirIndex = 0;
  5395. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5396. if (gunIndex == 1)
  5397. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5398. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5399. {
  5400. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5401. {
  5402. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5403. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5404. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5405. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5406. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5407. {
  5408. // Unlocked - 充電中,需停止充電
  5409. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "UnlockCommand");
  5410. ChargingTerminalProcess(gunIndex);
  5411. }
  5412. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5413. chargingInfo[gunIndex]->Type == _Type_GB)
  5414. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5415. else
  5416. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5417. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5418. }
  5419. }
  5420. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5421. {
  5422. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5423. {
  5424. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5425. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5426. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5427. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5428. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5429. {
  5430. // Unlocked - 充電中,需停止充電
  5431. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "UnlockCommand");
  5432. ChargingTerminalProcess(gunIndex);
  5433. }
  5434. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5435. chargingInfo[gunIndex]->Type == _Type_GB)
  5436. strcpy((char*)ShmOCPP20Data->UnlockConnector[gunIndex + acDirIndex].Response_status, "Unlocked");
  5437. else
  5438. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5439. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5440. }
  5441. }
  5442. }
  5443. bool ocpp_chk_remoteStop_cmd(byte gunIndex)
  5444. {
  5445. bool result = false;
  5446. byte acDirIndex = 0;
  5447. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5448. if (gunIndex == 1)
  5449. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5450. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5451. result = ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq;
  5452. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5453. result = ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq;
  5454. if (result)
  5455. {
  5456. DEBUG_INFO_MSG("Remote Stop by OCPP (%d) \n", gunIndex + acDirIndex);
  5457. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5458. {
  5459. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "Remote");
  5460. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq = NO;
  5461. }
  5462. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5463. {
  5464. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "Remote");
  5465. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq = NO;
  5466. }
  5467. }
  5468. return result;
  5469. }
  5470. bool ocpp_sub_get_remote_start_transaction_req(byte gun_index)
  5471. {
  5472. bool result = false;
  5473. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5474. {
  5475. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  5476. }
  5477. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5478. {
  5479. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  5480. }
  5481. if (result && ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5482. {
  5483. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  5484. (char *)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag);
  5485. }
  5486. return result;
  5487. }
  5488. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result)
  5489. {
  5490. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5491. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStartTransactionReq = result;
  5492. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5493. ShmOCPP20Data->CsMsg.bits[gunIndex].RequestStartTransactionReq = result;
  5494. }
  5495. void ocpp_chk_remoteStart_cmd()
  5496. {
  5497. if(!isDetectPlugin())
  5498. {
  5499. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  5500. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5501. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  5502. {
  5503. if (ocpp_sub_get_remote_start_transaction_req(acDirIndex))
  5504. {
  5505. if (ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_IDLE ||
  5506. ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_RESERVATION)
  5507. {
  5508. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  5509. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5510. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  5511. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  5512. //ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  5513. DetectPluginStart();
  5514. return;
  5515. }
  5516. ocpp_sub_set_remote_start_transaction_req(acDirIndex, NO);
  5517. }
  5518. }
  5519. byte threeGunIndex = 0;
  5520. byte dcIndex = 0;
  5521. bool isGunUsingStatus = false;
  5522. byte scheduleIndex = 0;
  5523. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5524. {
  5525. // 如果有 AC 槍,且 DC 槍也有兩把
  5526. if (acDirIndex == 1 && _index == 1)
  5527. threeGunIndex = 1;
  5528. if (ocpp_sub_get_remote_start_transaction_req(_index + threeGunIndex))
  5529. dcIndex = _index;
  5530. if (chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE)
  5531. {
  5532. isGunUsingStatus = true;
  5533. }
  5534. if (chargingInfo[_index]->schedule.isTriggerStart)
  5535. scheduleIndex = _index;
  5536. }
  5537. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  5538. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  5539. {
  5540. if (dcIndex == 0)
  5541. threeGunIndex = 0;
  5542. //ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5543. return;
  5544. }
  5545. if (dcIndex == 0)
  5546. threeGunIndex = 0;
  5547. if (ocpp_sub_get_remote_start_transaction_req(dcIndex + threeGunIndex))
  5548. {
  5549. if (chargingInfo[dcIndex]->SystemStatus == SYS_MODE_IDLE ||
  5550. chargingInfo[dcIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5551. {
  5552. if (ocpp_authorizeRemoteTxReqChk_key())
  5553. {
  5554. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  5555. (char *)ShmOCPP16Data->RemoteStartTransaction[dcIndex + threeGunIndex].IdTag);
  5556. }
  5557. else
  5558. {
  5559. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  5560. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5561. ChangeGunSelectByIndex(dcIndex);
  5562. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  5563. DetectPluginStart();
  5564. }
  5565. }
  5566. ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5567. }
  5568. else if (chargingInfo[scheduleIndex]->schedule.isTriggerStart)
  5569. {
  5570. if (chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_IDLE ||
  5571. chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5572. {
  5573. chargingInfo[scheduleIndex]->RemoteStartFlag = YES;
  5574. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5575. DetectPluginStart();
  5576. }
  5577. chargingInfo[scheduleIndex]->schedule.isTriggerStart = NO;
  5578. }
  5579. }
  5580. }
  5581. bool ocpp_remote_start_wait_cmd()
  5582. {
  5583. bool result = false;
  5584. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5585. {
  5586. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  5587. }
  5588. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5589. {
  5590. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  5591. }
  5592. return result;
  5593. }
  5594. void ocpp_chk_update_cmd()
  5595. {
  5596. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5597. {
  5598. if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5599. {
  5600. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5601. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  5602. {
  5603. DEBUG_INFO_MSG("Backend : update start. \n");
  5604. sleep(2);
  5605. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  5606. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  5607. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5608. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5609. {
  5610. setChargerMode(_index, SYS_MODE_UPDATE);
  5611. }
  5612. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5613. {
  5614. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5615. }
  5616. CloseWatchDog();
  5617. sleep(2);
  5618. KillTask();
  5619. byte updateResult = CheckUpdateProcess();
  5620. if (updateResult == PASS)
  5621. {
  5622. DEBUG_INFO_MSG("Backend : update complete. \n");
  5623. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5624. }
  5625. else if (updateResult == MODELNAME_FAIL)
  5626. {
  5627. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5628. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5629. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5630. sleep(5);
  5631. KillAllTask();
  5632. system("/usr/bin/run_evse_restart.sh");
  5633. return;
  5634. }
  5635. else
  5636. {
  5637. DEBUG_INFO_MSG("Backend : update fail. \n");
  5638. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5639. }
  5640. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5641. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5642. sleep(5);
  5643. system("reboot -f");
  5644. }
  5645. }
  5646. }
  5647. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5648. {
  5649. if(ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5650. {
  5651. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5652. if (strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == EQUAL)
  5653. {
  5654. DEBUG_INFO_MSG("Backend : update start. \n");
  5655. sleep(2);
  5656. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "");
  5657. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installing");
  5658. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5659. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5660. {
  5661. setChargerMode(_index, SYS_MODE_UPDATE);
  5662. }
  5663. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5664. {
  5665. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5666. }
  5667. CloseWatchDog();
  5668. sleep(2);
  5669. KillTask();
  5670. byte updateResult = CheckUpdateProcess();
  5671. if (updateResult == PASS)
  5672. {
  5673. DEBUG_INFO_MSG("Backend : update complete. \n");
  5674. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5675. }
  5676. else if (updateResult == MODELNAME_FAIL)
  5677. {
  5678. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5679. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5680. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5681. sleep(5);
  5682. KillAllTask();
  5683. system("/usr/bin/run_evse_restart.sh");
  5684. return;
  5685. }
  5686. else
  5687. {
  5688. DEBUG_INFO_MSG("Backend : update fail. \n");
  5689. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5690. }
  5691. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5692. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5693. sleep(5);
  5694. system("reboot -f");
  5695. }
  5696. }
  5697. }
  5698. }
  5699. bool ocpp_chk_invalid_id_cmd(byte gunIndex)
  5700. {
  5701. byte acDirIndex = 0;
  5702. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5703. if (gunIndex == 1)
  5704. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5705. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5706. {
  5707. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  5708. {
  5709. if (strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Blocked") == EQUAL ||
  5710. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Expired") == EQUAL ||
  5711. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  5712. return true;
  5713. }
  5714. }
  5715. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5716. {
  5717. if (strstr((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  5718. {
  5719. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Blocked") == EQUAL ||
  5720. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Expired") == EQUAL ||
  5721. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Invalid") == EQUAL ||
  5722. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NoCredit") == EQUAL ||
  5723. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == EQUAL ||
  5724. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisLocation") == EQUAL ||
  5725. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisTime") == EQUAL ||
  5726. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Unknown") == EQUAL)
  5727. return true;
  5728. }
  5729. }
  5730. return false;
  5731. }
  5732. bool ocpp_chk_profileConf_cmd(byte gunIndex)
  5733. {
  5734. byte acDirIndex = 0;
  5735. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5736. if (gunIndex == 1)
  5737. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5738. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5739. {
  5740. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5741. {
  5742. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5743. return true;
  5744. }
  5745. }
  5746. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5747. {
  5748. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5749. {
  5750. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5751. return true;
  5752. }
  5753. }
  5754. return false;
  5755. }
  5756. void ocpp_chk_notificationMessage_cmd(byte gunIndex)
  5757. {
  5758. byte acDirIndex = 0;
  5759. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5760. if (gunIndex == 1)
  5761. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5762. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5763. {
  5764. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq == YES)
  5765. {
  5766. if (chargingInfo[gunIndex]->Type == _Type_Chademo &&
  5767. (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5768. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_RESERVATION ||
  5769. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_MAINTAIN))
  5770. {
  5771. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag == NO)
  5772. {
  5773. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (chademo) \n", gunIndex);
  5774. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  5775. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5776. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = YES;
  5777. }
  5778. }
  5779. else if (chargingInfo[gunIndex]->Type == _Type_CCS ||
  5780. chargingInfo[gunIndex]->Type == _Type_GB ||
  5781. chargingInfo[gunIndex]->Type == _Type_AC)
  5782. {
  5783. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (ccs) \n", gunIndex);
  5784. }
  5785. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq = NO;
  5786. }
  5787. }
  5788. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5789. {
  5790. }
  5791. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag)
  5792. {
  5793. int _timeBuf = GetTimeoutValue(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5794. if (_timeBuf < 0)
  5795. {
  5796. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  5797. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5798. }
  5799. if (_timeBuf > 30 ||
  5800. (_timeBuf > 2 &&
  5801. chargingInfo[gunIndex]->ConnectorPlugIn))
  5802. {
  5803. PRINTF_FUNC("Trigger msg - timeout or Plugin. \n");
  5804. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = NO;
  5805. }
  5806. else
  5807. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = (_timeBuf / uSEC_VAL);
  5808. }
  5809. }
  5810. void ocpp_lift_profileReq_cmd(byte gunIndex)
  5811. {
  5812. byte acDirIndex = 0;
  5813. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5814. if (gunIndex == 1)
  5815. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5817. {
  5818. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  5819. {
  5820. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  5821. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  5822. }
  5823. }
  5824. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5825. {
  5826. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  5827. {
  5828. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  5829. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  5830. }
  5831. }
  5832. }
  5833. void ocpp_set_startTransation_cmd(byte gunIndex)
  5834. {
  5835. byte _OcppGunIndex = gunIndex;
  5836. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5837. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  5838. _OcppGunIndex = 2;
  5839. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5840. {
  5841. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5842. strcpy((char *)chargingInfo[gunIndex]->StartUserId, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  5843. else
  5844. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  5845. if (strcmp((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  5846. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  5847. PRINTF_FUNC("IdTag = %s \n", chargingInfo[gunIndex]->StartUserId);
  5848. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  5849. }
  5850. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5851. {
  5852. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5853. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5854. else
  5855. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  5856. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5857. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  5858. }
  5859. }
  5860. void ocpp_set_stopTransation_cmd(byte gunIndex)
  5861. {
  5862. byte _OcppGunIndex = gunIndex;
  5863. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5864. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  5865. _OcppGunIndex = 2;
  5866. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5867. {
  5868. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5869. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  5870. else
  5871. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  5872. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  5873. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  5874. }
  5875. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5876. {
  5877. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5878. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5879. else
  5880. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  5881. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5882. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  5883. }
  5884. }
  5885. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString)
  5886. {
  5887. // 參數 gunIndex 已包含 AC 槍
  5888. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5889. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, errString);
  5890. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5891. {
  5892. }
  5893. }
  5894. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason)
  5895. {
  5896. byte acDirIndex = 0;
  5897. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5898. if (gunIndex == 1)
  5899. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5900. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5901. {
  5902. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "") == EQUAL)
  5903. {
  5904. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, reason);
  5905. }
  5906. }
  5907. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5908. {
  5909. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "") == EQUAL)
  5910. {
  5911. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, reason);
  5912. }
  5913. }
  5914. }
  5915. void ocpp_set_authorizeReq_cmd(byte value)
  5916. {
  5917. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5918. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = value;
  5919. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5920. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = value;
  5921. }
  5922. void ocpp_set_authorizeConf_cmd(byte value)
  5923. {
  5924. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5925. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = value;
  5926. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5927. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = value;
  5928. }
  5929. void ocpp_set_errCode_cmd(byte gunIndex)
  5930. {
  5931. byte acDirIndex = 0;
  5932. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5933. if (gunIndex == 1)
  5934. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5935. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5936. {
  5937. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  5938. {
  5939. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "InternalError");
  5940. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->ConnectorAlarmCode);
  5941. }
  5942. else if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  5943. {
  5944. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "OtherError");
  5945. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->EvConnAlarmCode);
  5946. }
  5947. else
  5948. {
  5949. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  5950. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  5951. }
  5952. }
  5953. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5954. {
  5955. }
  5956. }
  5957. void ocpp_clear_errorCode_cmd(byte gunIndex)
  5958. {
  5959. byte acDirIndex = 0;
  5960. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5961. if (gunIndex == 1)
  5962. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5963. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5964. {
  5965. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError") != EQUAL)
  5966. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  5967. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "") != EQUAL)
  5968. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  5969. }
  5970. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5971. {
  5972. }
  5973. }
  5974. void ocpp_clear_idTag_cmd(byte gun_index)
  5975. {
  5976. byte acDirIndex = 0;
  5977. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5978. if (gun_index == 1)
  5979. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5980. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5981. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseIdTagInfo.Status, "");
  5982. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5983. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].Response_idTokenInfo.status, "");
  5984. }
  5985. void ocpp_clear_stopReason(byte gun_index)
  5986. {
  5987. byte acDirIndex = 0;
  5988. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5989. if (gun_index == 1)
  5990. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5991. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5992. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, "");
  5993. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5994. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].transactionInfo.stoppedReason, "");
  5995. }
  5996. unsigned short _ocpp_get_connect_timeout()
  5997. {
  5998. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING / 1000);
  5999. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6000. {
  6001. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  6002. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  6003. }
  6004. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6005. {
  6006. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  6007. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  6008. }
  6009. return result;
  6010. }
  6011. bool ocpp_get_authorize_conf()
  6012. {
  6013. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6014. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  6015. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6016. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  6017. return false;
  6018. }
  6019. bool ocpp_get_startTransation_req(byte gunIndex)
  6020. {
  6021. bool result = false;
  6022. byte acDirIndex = 0;
  6023. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6024. if (gunIndex == 1)
  6025. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6026. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6027. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6028. {
  6029. result = ShmOCPP16Data->CpMsg.bits[gunIndex + acDirIndex].StartTransactionReq;
  6030. }
  6031. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6032. {
  6033. result = ShmOCPP20Data->CpMsg.bits[gunIndex + acDirIndex].TransactionEventReq;
  6034. }
  6035. return result;
  6036. }
  6037. void ocpp_get_freeVendor(byte gunIndex)
  6038. {
  6039. byte _OcppGunIndex = gunIndex;
  6040. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6041. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6042. _OcppGunIndex = 2;
  6043. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6044. {
  6045. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "");
  6046. if (strlen((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData) > 0)
  6047. {
  6048. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag,
  6049. (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  6050. }
  6051. else
  6052. {
  6053. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag,
  6054. (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber);
  6055. }
  6056. }
  6057. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6058. {
  6059. if (strlen((char *)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value) > 0)
  6060. {
  6061. }
  6062. else
  6063. {
  6064. }
  6065. }
  6066. }
  6067. void ocpp_parsing_DT_defaultPrice_cmd()
  6068. {
  6069. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6070. {
  6071. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  6072. {
  6073. DefaultPriceHandlerOcpp16((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  6074. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  6075. }
  6076. }
  6077. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6078. {
  6079. //ShmOCPP20Data->CostUpdated
  6080. }
  6081. }
  6082. void ocpp_parsing_DT_userPrice_cmd()
  6083. {
  6084. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6085. {
  6086. if (strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 &&
  6087. strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  6088. {
  6089. UserPriceHandlerOcpp16((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken,
  6090. (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  6091. //memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  6092. }
  6093. }
  6094. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6095. {
  6096. //ShmOCPP20Data->CostUpdated
  6097. }
  6098. }
  6099. void ocpp_parsing_DT_running_final_cmd()
  6100. {
  6101. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6102. {
  6103. RunningFinalCostHandlerOcpp16();
  6104. }
  6105. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6106. {
  6107. //ShmOCPP20Data->CostUpdated
  6108. }
  6109. }
  6110. void ocpp_chargingProfile_process(byte _index)
  6111. {
  6112. int _time = 0;
  6113. int _startCount = NO_DEFINE;
  6114. int _maxCount = 0;
  6115. byte acDirIndex = 0;
  6116. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6117. if (_index == 1)
  6118. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6119. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6120. {
  6121. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileKind, "Absolute") == EQUAL &&
  6122. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileId == YES)
  6123. {
  6124. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.StartSchedule);
  6125. if (_time > -1)
  6126. {
  6127. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod);
  6128. _startCount = NO_DEFINE;
  6129. for (byte _count = 0; _count < _maxCount; _count++)
  6130. {
  6131. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6132. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6133. {
  6134. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6135. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6136. {
  6137. _startCount = _count;
  6138. }
  6139. }
  6140. }
  6141. if (_startCount < _maxCount)
  6142. {
  6143. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6144. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6145. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  6146. chargingInfo[_index]->PresentChargingVoltage > 0)
  6147. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6148. else
  6149. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6150. }
  6151. else
  6152. {
  6153. chargingInfo[_index]->ChargingProfilePower = -1;
  6154. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6155. }
  6156. }
  6157. else
  6158. {
  6159. chargingInfo[_index]->ChargingProfilePower = -1;
  6160. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6161. }
  6162. }
  6163. else
  6164. {
  6165. chargingInfo[_index]->ChargingProfilePower = -1;
  6166. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6167. }
  6168. if (strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == EQUAL &&
  6169. ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES)
  6170. {
  6171. _time = GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  6172. if (_time > -1)
  6173. {
  6174. _maxCount = ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  6175. _startCount = NO_DEFINE;
  6176. for (byte _count = 0; _count < _maxCount; _count++)
  6177. {
  6178. if (_time >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6179. {
  6180. if ((_count == 0 && ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6181. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6182. {
  6183. _startCount = _count;
  6184. }
  6185. }
  6186. }
  6187. //PRINTF_FUNC("_startCount = %d, Max Profile Limit = %f \n", _startCount, ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  6188. if (_startCount < _maxCount)
  6189. {
  6190. float maxChargingProfilePow = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6191. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  6192. maxChargingProfilePow /= 2;
  6193. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = maxChargingProfilePow;
  6194. }
  6195. else
  6196. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6197. }
  6198. }
  6199. else
  6200. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6201. }
  6202. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6203. {
  6204. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingProfileKind, "Absolute") == EQUAL &&
  6205. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].id == YES)
  6206. {
  6207. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].startSchedule);
  6208. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod);
  6209. _startCount = NO_DEFINE;
  6210. for (byte _count = 0; _count < _maxCount; _count++)
  6211. {
  6212. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6213. if (_time >= ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  6214. {
  6215. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  6216. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  6217. {
  6218. _startCount = _count;
  6219. }
  6220. }
  6221. }
  6222. if (_startCount < _maxCount)
  6223. {
  6224. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6225. //PRINTF_FUNC("Profile Limit = %f \n", ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  6226. chargingInfo[_index]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit * AC_OUTPUT_VOL;
  6227. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0)
  6228. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6229. else
  6230. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6231. }
  6232. else
  6233. {
  6234. chargingInfo[_index]->ChargingProfilePower = -1;
  6235. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6236. }
  6237. }
  6238. else
  6239. {
  6240. chargingInfo[_index]->ChargingProfilePower = -1;
  6241. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6242. }
  6243. }
  6244. else
  6245. {
  6246. chargingInfo[_index]->ChargingProfilePower = -1;
  6247. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6248. }
  6249. if (chargingInfo[_index]->ChargingProfilePower >= 0 &&
  6250. _ChargingProfilePwBuffer[_index] != chargingInfo[_index]->ChargingProfilePower)
  6251. {
  6252. _ChargingProfilePwBuffer[_index] = chargingInfo[_index]->ChargingProfilePower;
  6253. PRINTF_FUNC("Profile : ChargingProfilePower = %.2f \n", chargingInfo[_index]->ChargingProfilePower);
  6254. PRINTF_FUNC("Profile : ChargingProfileCurrent = %.2f \n", chargingInfo[_index]->ChargingProfileCurrent);
  6255. }
  6256. }
  6257. void CheckOcppProfileConfirm(byte gun_index)
  6258. {
  6259. if ((chargingInfo [gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  6260. (chargingInfo [gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6261. {
  6262. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_CHARGING)
  6263. {
  6264. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  6265. if (chargingInfo [gun_index]->PresentChargingVoltage >= gunOutputVol [gun_index] + 10 ||
  6266. chargingInfo [gun_index]->PresentChargingVoltage <= gunOutputVol [gun_index] - 10)
  6267. {
  6268. gunOutputVol [gun_index] = chargingInfo [gun_index]->PresentChargingVoltage;
  6269. }
  6270. }
  6271. ocpp_chk_profileConf_cmd ( gun_index );
  6272. }
  6273. }
  6274. void CheckAcOcppProfileConfirm()
  6275. {
  6276. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING &&
  6277. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  6278. {
  6279. ocpp_ac_chk_profileConf_cmd();
  6280. }
  6281. }
  6282. //===============================================
  6283. // wifi schedule
  6284. //===============================================
  6285. bool WifiScheduleStop(byte gunIndex)
  6286. {
  6287. bool result = false;
  6288. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  6289. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  6290. return result;
  6291. }
  6292. //===============================================
  6293. // SQLite3 related routine
  6294. //===============================================
  6295. int NetworkDB_Open(sqlite3 *db)
  6296. {
  6297. int result = PASS;
  6298. char* errMsg = NULL;
  6299. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  6300. "idx integer primary key AUTOINCREMENT, "
  6301. "occurDatetime text NOT NULL, "
  6302. "isInternet text NOT NULL, "
  6303. "isOcppConnected text NOT NULL, "
  6304. "isEth0Internet text NOT NULL, "
  6305. "isMlan0Internet text NOT NULL, "
  6306. "isPpp0Internet text NOT NULL, "
  6307. "rssiMlan0 text NOT NULL, "
  6308. "rssiPpp0 text NOT NULL"
  6309. ");";
  6310. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  6311. if(sqlite3_open(NETWORK_DB_FILE, &db))
  6312. {
  6313. result = FAIL;
  6314. PRINTF_FUNC( "Can't open database (Net) : %s\n", sqlite3_errmsg(db));
  6315. sqlite3_close(db);
  6316. }
  6317. else
  6318. {
  6319. PRINTF_FUNC( "Local network status record database open successfully.\n");
  6320. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6321. {
  6322. result = FAIL;
  6323. PRINTF_FUNC( "Create local network status record table error message: %s\n", errMsg);
  6324. }
  6325. else
  6326. {
  6327. PRINTF_FUNC( "Opened local network status record table successfully\n");
  6328. }
  6329. sqlite3_close(db);
  6330. }
  6331. return result;
  6332. }
  6333. int DB_Open(sqlite3 *db)
  6334. {
  6335. int result = PASS;
  6336. char* errMsg = NULL;
  6337. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  6338. "idx integer primary key AUTOINCREMENT, "
  6339. "reservationId text, "
  6340. "transactionId text, "
  6341. "startMethod text, "
  6342. "userId text, "
  6343. "dateTimeStart text, "
  6344. "dateTimeStop text,"
  6345. "socStart text, "
  6346. "socStop text, "
  6347. "chargeEnergy text, "
  6348. "stopReason text,"
  6349. "vincode text"
  6350. ");";
  6351. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  6352. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6353. "`item` TEXT NOT NULL, "
  6354. "`connector` INTEGER NOT NULL, "
  6355. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  6356. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  6357. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6358. "`occurDatetime` TEXT NOT NULL, "
  6359. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  6360. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  6361. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6362. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  6363. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  6364. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6365. "`connector` INTEGER NOT NULL, "
  6366. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6367. // char * createTestSql = "CREATE TABLE IF NOT EXISTS `for_test` ( "
  6368. // "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6369. // "`connector` INTEGER NOT NULL, "
  6370. // "`testID` TEXT, "
  6371. // "`checkStatus` INTEGER NOT NULL "
  6372. // ");";
  6373. if(sqlite3_open(DB_FILE, &db))
  6374. {
  6375. result = FAIL;
  6376. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6377. sqlite3_close(db);
  6378. }
  6379. else
  6380. {
  6381. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  6382. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6383. {
  6384. result = FAIL;
  6385. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  6386. }
  6387. else
  6388. {
  6389. PRINTF_FUNC( "Opened local charging record table successfully\n");
  6390. }
  6391. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  6392. {
  6393. result = FAIL;
  6394. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  6395. }
  6396. else
  6397. {
  6398. PRINTF_FUNC( "Opened local config table successfully\n");
  6399. }
  6400. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6401. {
  6402. result = FAIL;
  6403. PRINTF_FUNC( "Create local record table error message: %s\n", errMsg);
  6404. }
  6405. else
  6406. {
  6407. PRINTF_FUNC( "Opened local record table successfully\n");
  6408. }
  6409. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  6410. {
  6411. result = FAIL;
  6412. PRINTF_FUNC( "Create reboot record table error message: %s\n", errMsg);
  6413. }
  6414. else
  6415. {
  6416. PRINTF_FUNC( "Opened reboot record table successfully\n");
  6417. }
  6418. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  6419. {
  6420. result = FAIL;
  6421. PRINTF_FUNC( "Create power consumption record table error message: %s\n", errMsg);
  6422. }
  6423. else
  6424. {
  6425. PRINTF_FUNC( "Opened power consumption record table successfully\n");
  6426. }
  6427. // if (sqlite3_exec(db, createTestSql, 0, 0, &errMsg) != SQLITE_OK)
  6428. // {
  6429. // result = FAIL;
  6430. // printf( "Create test table error message: %s\n", errMsg);
  6431. // }
  6432. // else
  6433. // {
  6434. // printf( "Opened test table successfully\n");
  6435. // }
  6436. sqlite3_close(db);
  6437. }
  6438. return result;
  6439. }
  6440. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6441. {
  6442. int result = PASS;
  6443. char* errMsg = NULL;
  6444. char insertSql[1024];
  6445. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6446. {
  6447. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, vincode) "
  6448. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  6449. chargingInfo[gun_index]->ReservationId,
  6450. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  6451. chargingInfo[gun_index]->StartMethod,
  6452. chargingInfo[gun_index]->StartUserId,
  6453. chargingInfo[gun_index]->StartDateTime,
  6454. chargingInfo[gun_index]->StopDateTime,
  6455. chargingInfo[gun_index]->EvBatteryStartSoc,
  6456. chargingInfo[gun_index]->EvBatterySoc,
  6457. chargingInfo[gun_index]->PresentChargedEnergy,
  6458. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  6459. chargingInfo[gun_index]->EVCCID);
  6460. }
  6461. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6462. {
  6463. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6464. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6465. chargingInfo[gun_index]->ReservationId,
  6466. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  6467. chargingInfo[gun_index]->StartMethod,
  6468. chargingInfo[gun_index]->StartUserId,
  6469. chargingInfo[gun_index]->StartDateTime,
  6470. chargingInfo[gun_index]->StopDateTime,
  6471. chargingInfo[gun_index]->EvBatteryStartSoc,
  6472. chargingInfo[gun_index]->EvBatterySoc,
  6473. chargingInfo[gun_index]->PresentChargedEnergy,
  6474. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  6475. }
  6476. if(sqlite3_open(DB_FILE, &db))
  6477. {
  6478. result = FAIL;
  6479. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6480. sqlite3_close(db);
  6481. }
  6482. else
  6483. {
  6484. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6485. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6486. {
  6487. result = FAIL;
  6488. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  6489. }
  6490. else
  6491. {
  6492. PRINTF_FUNC( "Insert local charging record successfully\n");
  6493. }
  6494. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6495. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6496. {
  6497. result = FAIL;
  6498. PRINTF_FUNC( "delete local charging error message: %s\n", errMsg);
  6499. }
  6500. else
  6501. {
  6502. PRINTF_FUNC( "delete local charging record successfully\n");
  6503. }
  6504. sqlite3_close(db);
  6505. }
  6506. return result;
  6507. }
  6508. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6509. {
  6510. uint8_t result = false;
  6511. char* errMsg = NULL;
  6512. char sqlStr[1024];
  6513. srand(time(NULL));
  6514. if(sqlite3_open(DB_FILE, &db))
  6515. {
  6516. result = FAIL;
  6517. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6518. sqlite3_close(db);
  6519. }
  6520. else
  6521. {
  6522. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  6523. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  6524. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  6525. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6526. {
  6527. result = FAIL;
  6528. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  6529. }
  6530. else
  6531. {
  6532. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  6533. }
  6534. sqlite3_close(db);
  6535. }
  6536. return result;
  6537. }
  6538. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6539. {
  6540. uint8_t result = true;
  6541. char* errMsg = NULL;
  6542. char sqlStr[1024];
  6543. char **rs;
  6544. int rows, cols;
  6545. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index);
  6546. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  6547. if(sqlite3_open(DB_FILE, &db))
  6548. {
  6549. result = FAIL;
  6550. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6551. sqlite3_close(db);
  6552. }
  6553. else
  6554. {
  6555. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  6556. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6557. if(rows>0)
  6558. {
  6559. for(int idxRow=1;idxRow<=rows;idxRow++)
  6560. {
  6561. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  6562. {
  6563. result = false;
  6564. }
  6565. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  6566. }
  6567. }
  6568. else
  6569. {
  6570. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  6571. }
  6572. sqlite3_free_table(rs);
  6573. sqlite3_close(db);
  6574. }
  6575. return result;
  6576. }
  6577. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  6578. {
  6579. char* errMsg = NULL;
  6580. char sqlStr[1024];
  6581. srand(time(NULL));
  6582. if(sqlite3_open(DB_FILE, &db))
  6583. {
  6584. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6585. sqlite3_close(db);
  6586. }
  6587. else
  6588. {
  6589. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  6590. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6591. {
  6592. PRINTF_FUNC( "update power_consumption error message: %s\n", errMsg);
  6593. }
  6594. else
  6595. {
  6596. //PRINTF_FUNC("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  6597. }
  6598. sqlite3_close(db);
  6599. }
  6600. }
  6601. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  6602. {
  6603. float result = 0;
  6604. char* errMsg = NULL;
  6605. char sqlStr[1024];
  6606. char **rs;
  6607. int rows, cols;
  6608. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  6609. if (sqlite3_open(DB_FILE, &db))
  6610. {
  6611. result = FAIL;
  6612. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  6613. sqlite3_close(db);
  6614. }
  6615. else
  6616. {
  6617. PRINTF_FUNC("Local config query database open successfully.\r\n");
  6618. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6619. if (rows > 0)
  6620. {
  6621. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6622. {
  6623. result = atof(rs[(idxRow * cols) + 2]);
  6624. }
  6625. }
  6626. else
  6627. {
  6628. PRINTF_FUNC( "Query connector-%d fail, set default value to consumption.\r\n", gun_index);
  6629. }
  6630. sqlite3_free_table(rs);
  6631. sqlite3_close(db);
  6632. }
  6633. return result;
  6634. }
  6635. int DB_Reboot_Record(sqlite3 *db)
  6636. {
  6637. int result = PASS;
  6638. char* errMsg = NULL;
  6639. char insertSql[256];
  6640. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  6641. if(sqlite3_open(DB_FILE, &db))
  6642. {
  6643. result = FAIL;
  6644. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6645. sqlite3_close(db);
  6646. }
  6647. else
  6648. {
  6649. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6650. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6651. {
  6652. result = FAIL;
  6653. PRINTF_FUNC( "Insert reboot record error message: %s\n", errMsg);
  6654. }
  6655. else
  6656. {
  6657. PRINTF_FUNC( "Insert reboot record successfully\n");
  6658. }
  6659. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6660. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6661. {
  6662. result = FAIL;
  6663. PRINTF_FUNC( "delete reboot record error message: %s\n", errMsg);
  6664. }
  6665. else
  6666. {
  6667. PRINTF_FUNC( "delete reboot record successfully\n");
  6668. }
  6669. sqlite3_close(db);
  6670. }
  6671. return result;
  6672. }
  6673. int DB_Insert_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6674. {
  6675. int result = PASS;
  6676. char* errMsg = NULL;
  6677. char insertSql[1024];
  6678. sprintf(insertSql, "insert into for_test (connector, testID, checkStatus) values(%d, %s, 0);",
  6679. gun_index, textID);
  6680. if(sqlite3_open(DB_FILE, &db))
  6681. {
  6682. result = FAIL;
  6683. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6684. sqlite3_close(db);
  6685. }
  6686. else
  6687. {
  6688. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6689. {
  6690. result = FAIL;
  6691. PRINTF_FUNC( "Insert sdlu test error message : %s\n", errMsg);
  6692. }
  6693. else
  6694. {
  6695. PRINTF_FUNC( "Insert sdlu test successfully\n");
  6696. }
  6697. sqlite3_close(db);
  6698. }
  6699. return result;
  6700. }
  6701. int DB_delete_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6702. {
  6703. int result = PASS;
  6704. char* errMsg = NULL;
  6705. char insertSql[1024];
  6706. sprintf(insertSql, "delete from for_test where testID = %s", textID);
  6707. if (sqlite3_open(DB_FILE, &db))
  6708. {
  6709. result = FAIL;
  6710. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  6711. sqlite3_close(db);
  6712. }
  6713. else
  6714. {
  6715. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6716. {
  6717. result = FAIL;
  6718. PRINTF_FUNC( "Test message 2: %s\n", errMsg);
  6719. }
  6720. else
  6721. {
  6722. PRINTF_FUNC( "Test message 2 successfully\n");
  6723. }
  6724. sqlite3_close(db);
  6725. }
  6726. return result;
  6727. }
  6728. int DB_sql_test(sqlite3 *db, uint8_t gun_index)
  6729. {
  6730. int result = PASS;
  6731. char* errMsg = NULL;
  6732. char insertSql[1024];
  6733. char valueStr[128];
  6734. char **rs;
  6735. int rows, cols;
  6736. sprintf(insertSql, "select testID from for_test where checkStatus=0;");
  6737. if (sqlite3_open(DB_FILE, &db))
  6738. {
  6739. result = FAIL;
  6740. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  6741. sqlite3_close(db);
  6742. }
  6743. else
  6744. {
  6745. PRINTF_FUNC("Local for_test query database open successfully.\r\n");
  6746. sqlite3_get_table(db, insertSql, &rs, &rows, &cols, &errMsg);
  6747. if (rows > 0)
  6748. {
  6749. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6750. {
  6751. strcpy(valueStr, rs[(idxRow * cols)]);
  6752. printf("valueStr = %s \n", valueStr);
  6753. }
  6754. }
  6755. else
  6756. {
  6757. PRINTF_FUNC("select testID from for_test fail \r\n");
  6758. }
  6759. sqlite3_free_table(rs);
  6760. sqlite3_close(db);
  6761. }
  6762. return result;
  6763. }
  6764. //===============================================
  6765. // Config process
  6766. //===============================================
  6767. void AddPlugInTimes(byte gunIndex)
  6768. {
  6769. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  6770. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  6771. else if(chargingInfo[gunIndex]->Type == _Type_CCS)
  6772. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  6773. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  6774. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  6775. }
  6776. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  6777. {
  6778. char cmdBuf[32];
  6779. struct timeb csuTime;
  6780. struct tm *tmCSU;
  6781. ftime(&csuTime);
  6782. tmCSU = localtime(&csuTime.time);
  6783. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  6784. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6785. tmCSU->tm_sec);
  6786. if (isStart)
  6787. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  6788. else
  6789. {
  6790. if (strcmp((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00") != EQUAL)
  6791. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  6792. }
  6793. }
  6794. void zipLogFiles()
  6795. {
  6796. const char* logPath = "/Storage/SystemLog";
  6797. // 獲取目錄
  6798. DIR* pDir = opendir(logPath);
  6799. if (pDir != NULL)
  6800. {
  6801. struct timeb csuTime;
  6802. struct tm *tmCSU;
  6803. ftime(&csuTime);
  6804. tmCSU = localtime(&csuTime.time);
  6805. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  6806. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6807. // tmCSU->tm_sec);
  6808. // Read items inside the folder
  6809. struct dirent* pEntry = NULL;
  6810. while ((pEntry = readdir(pDir)) != NULL)
  6811. {
  6812. if (strcmp(pEntry->d_name, ".") != 0 &&
  6813. strcmp(pEntry->d_name, "..") != 0 &&
  6814. strncmp(pEntry->d_name, "[", 1) == 0 &&
  6815. strstr(pEntry->d_name, "tar") < 0)
  6816. {
  6817. char yearC[5];
  6818. unsigned short year = 0;
  6819. char monthC[3];
  6820. unsigned short month = 0;
  6821. yearC[4] = '\0';
  6822. strncpy(yearC, pEntry->d_name + 1, 4);
  6823. monthC[2] = '\0';
  6824. strncpy(monthC, pEntry->d_name + 6, 2);
  6825. year = atoi(yearC);
  6826. month = atoi(monthC);
  6827. if (year != 0)
  6828. {
  6829. if (year < tmCSU->tm_year + 1900 ||
  6830. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  6831. {
  6832. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  6833. char file[256];
  6834. memset(file, 0x00, sizeof(file));
  6835. strcat(file, "tar zcvf ");
  6836. strcat(file, logPath);
  6837. strncat(file, "/", 1);
  6838. strcat(file, pEntry->d_name);
  6839. strcat(file, ".tar");
  6840. strncat(file, " ", 1);
  6841. strcat(file, logPath);
  6842. strncat(file, "/", 1);
  6843. strcat(file, pEntry->d_name);
  6844. PRINTF_FUNC("zip = %s \n", file);
  6845. system(file);
  6846. }
  6847. }
  6848. }
  6849. }
  6850. }
  6851. // Close folder
  6852. closedir(pDir);
  6853. }
  6854. void ChangeGunSelectByIndex(byte sel)
  6855. {
  6856. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  6857. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  6858. }
  6859. void CheckIsAlternatvieByModelName()
  6860. {
  6861. if(ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'W')
  6862. {
  6863. // 壁掛
  6864. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  6865. }
  6866. else
  6867. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  6868. }
  6869. void StopProcessingLoop()
  6870. {
  6871. ocpp_process_start();
  6872. for (;;)
  6873. {
  6874. CheckTask();
  6875. CheckFactoryConfigFunction();
  6876. CheckFwUpdateFunction();
  6877. ocpp_chk_reset_cmd(YES);
  6878. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  6879. {
  6880. // 經過底下的 function 後,ShmSysConfigAndInfo->SysWarningInfo.Level 會再被更新一次
  6881. ChkPrimaryStatus();
  6882. ButtonTriggerEvent();
  6883. ReviewCriticalAlarm();
  6884. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_NORMAL &&
  6885. ShmDcCommonData->rebootCount < 1)
  6886. {
  6887. PRINTF_FUNC("Soft reboot for retry self-test (Primary). \n");
  6888. SetupRebootCount(1);
  6889. KillAllTask();
  6890. sleep(3);
  6891. system("/usr/bin/run_evse_restart.sh");
  6892. return;
  6893. }
  6894. }
  6895. sleep(1);
  6896. }
  6897. }
  6898. void CreateWatchdog()
  6899. {
  6900. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  6901. {
  6902. wtdFd = InitWatchDog();
  6903. if (wtdFd < 0)
  6904. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  6905. }
  6906. }
  6907. bool IsConnectorWholeIdle()
  6908. {
  6909. bool result = true;
  6910. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  6911. {
  6912. if (chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  6913. chargingInfo[count]->SystemStatus != SYS_MODE_RESERVATION)
  6914. {
  6915. result = false;
  6916. break;
  6917. }
  6918. }
  6919. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  6920. {
  6921. if (ac_chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  6922. ac_chargingInfo[count]->IsErrorOccur == NO)
  6923. {
  6924. result = false;
  6925. break;
  6926. }
  6927. }
  6928. return result;
  6929. }
  6930. void ClearAlarmCodeWhenAcOff()
  6931. {
  6932. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  6933. {
  6934. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  6935. }
  6936. }
  6937. //==========================================
  6938. // Check task processing
  6939. //==========================================
  6940. void CheckTask()
  6941. {
  6942. if(reset4gStep == RESET_4G_STEP_NONE &&
  6943. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' ||
  6944. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  6945. {
  6946. if(system("pidof -s Module_4g > /dev/null") != 0)
  6947. {
  6948. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  6949. system("/root/Module_4g &");
  6950. }
  6951. }
  6952. if(reset4gStep == RESET_4G_STEP_NONE &&
  6953. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' ||
  6954. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  6955. {
  6956. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  6957. {
  6958. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  6959. system("/root/Module_Wifi &");
  6960. }
  6961. }
  6962. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  6963. {
  6964. if(system("pidof -s Module_DcMeter > /dev/null") != 0)
  6965. {
  6966. DEBUG_ERROR_MSG("Module_DcMeter not running, restart it.\r\n");
  6967. system("/root/Module_DcMeter &");
  6968. }
  6969. }
  6970. ocpp_process_start();
  6971. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  6972. {
  6973. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  6974. system ("/root/Module_ProduceUtils &");
  6975. }
  6976. }
  6977. void Check267Alarm()
  6978. {
  6979. bool canRun = true;
  6980. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6981. {
  6982. if((chargingInfo[_index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[_index]->SystemStatus < SYS_MODE_TERMINATING) ||
  6983. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6984. {
  6985. canRun = false;
  6986. break;
  6987. }
  6988. }
  6989. if (canRun)
  6990. {
  6991. if ( ! ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  6992. {
  6993. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  6994. }
  6995. }
  6996. }
  6997. void CheckSystemTaskAlive()
  6998. {
  6999. unsigned char lostId = CheckSystemTask(ShmSysConfigAndInfo->SysInfo.SystemPage);
  7000. if (lostId != 0)
  7001. {
  7002. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost == NO)
  7003. {
  7004. EmcOccurThenStopCharging();
  7005. sleep(2);
  7006. if (lostId == _SYSTEM_TASK_LOST_ITEM_MAIN) PRINTF_FUNC ( "System task lost (CSU). \n" );
  7007. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVCOMM)
  7008. {
  7009. PRINTF_FUNC ( "System task lost (EVComm). \n" );
  7010. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun ++)
  7011. {
  7012. chargingInfo [gun]->ConnectorPlugIn = NO;
  7013. }
  7014. }
  7015. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PSUCOMM) PRINTF_FUNC ( "System task lost (PSU Task). \n" );
  7016. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVENTLOG) PRINTF_FUNC ( "System task lost (Event log). \n" );
  7017. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PRIMARYCOMM) PRINTF_FUNC ( "System task lost (Primary). \n" );
  7018. else if (lostId == _SYSTEM_TASK_LOST_ITEM_LCMCONTROL) PRINTF_FUNC ( "System task lost (LCM Comm). \n" );
  7019. else if (lostId == _SYSTEM_TASK_LOST_ITEM_INTERCOMM) PRINTF_FUNC("System task lost (Internal Comm). \n");
  7020. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = YES;
  7021. }
  7022. }
  7023. else
  7024. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = NO;
  7025. }
  7026. void InitialDHCP()
  7027. {
  7028. char tmpbuf[256];
  7029. memset(tmpbuf,0,256);
  7030. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  7031. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  7032. system(tmpbuf);
  7033. }
  7034. void Run4gResetProc()
  7035. {
  7036. if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == _SYS_4G_MODE_ENABLE ||
  7037. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_STATION)
  7038. {
  7039. if (!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  7040. !ShmSysConfigAndInfo->SysInfo.InternetConn)
  7041. {
  7042. if (reset4gStep == RESET_4G_STEP_NONE)
  7043. {
  7044. if ((ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES && _4gResetChkFlag >= 12) ||
  7045. _4gResetChkFlag >= 60)
  7046. {
  7047. PRINTF_FUNC("Reset 4G/Wifi module. \n");
  7048. reset4gStep = RESET_4G_STEP_DELETE_TASK;
  7049. }
  7050. else
  7051. _4gResetChkFlag++;
  7052. }
  7053. }
  7054. else
  7055. _4gResetChkFlag = 0;
  7056. switch(reset4gStep)
  7057. {
  7058. case RESET_4G_STEP_DELETE_TASK:
  7059. {
  7060. PRINTF_FUNC("Killall 4G / Wifi task. \n");
  7061. system("killall Module_4g &");
  7062. system("killall Module_Wifi &");
  7063. reset4gStep = RESET_4G_STEP_GPIO_HIGH;
  7064. }
  7065. break;
  7066. case RESET_4G_STEP_GPIO_HIGH:
  7067. {
  7068. PRINTF_FUNC("Disable 4G/Wifi. GPIO-104 -> High. \n");
  7069. system("echo 1 > /sys/class/gpio/gpio104/value");
  7070. reset4gStep = RESET_4G_STEP_GPIO_LOW_COMP;
  7071. }
  7072. break;
  7073. case RESET_4G_STEP_GPIO_LOW_COMP:
  7074. {
  7075. PRINTF_FUNC("Enable 4G/Wifi. GPIO-104 -> Low. \n");
  7076. system("echo 0 > /sys/class/gpio/gpio104/value");
  7077. reset4gStep = RESET_4G_STEP_NONE;
  7078. _4gResetChkFlag = 0;
  7079. }
  7080. break;
  7081. }
  7082. }
  7083. else
  7084. {
  7085. if (reset4gStep != RESET_4G_STEP_NONE)
  7086. system("echo 0 > /sys/class/gpio/gpio104/value");
  7087. reset4gStep = RESET_4G_STEP_NONE;
  7088. _4gResetChkFlag = 0;
  7089. }
  7090. }
  7091. //==========================================
  7092. // Check Smart Charging Profile
  7093. //==========================================
  7094. int GetStartScheduleTime(unsigned char *time)
  7095. {
  7096. int result = -1;
  7097. struct tm tmScheduleStart;
  7098. struct timeb tbScheduleStart;
  7099. 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))
  7100. {
  7101. tmScheduleStart.tm_year -= 1900;
  7102. tmScheduleStart.tm_mon -= 1;
  7103. tbScheduleStart.time = mktime(&tmScheduleStart);
  7104. tbScheduleStart.millitm = 0;
  7105. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  7106. }
  7107. return result;
  7108. }
  7109. //==========================================
  7110. // sub function
  7111. //==========================================
  7112. void CheckReturnToChargingConn()
  7113. {
  7114. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  7115. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  7116. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  7117. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  7118. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  7119. {
  7120. bool isReturnTimeout = false;
  7121. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7122. {
  7123. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  7124. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7125. {
  7126. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7127. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7128. {
  7129. isReturnTimeout = false;
  7130. break;
  7131. }
  7132. }
  7133. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7134. {
  7135. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7136. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7137. {
  7138. isReturnTimeout = true;
  7139. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7140. }
  7141. }
  7142. }
  7143. // AC 槍
  7144. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  7145. {
  7146. // 沒有選中 AC,且 AC 在充電中
  7147. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  7148. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE))
  7149. {
  7150. // 當前 DC 充電槍在 idle 狀態
  7151. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  7152. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  7153. {
  7154. isReturnTimeout = true;
  7155. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7156. }
  7157. }
  7158. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  7159. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7160. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  7161. {
  7162. // 當前 DC 充電槍在 idle 狀態
  7163. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  7164. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  7165. {
  7166. isReturnTimeout = true;
  7167. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7168. }
  7169. }
  7170. }
  7171. if (!isReturnTimeout)
  7172. StopSystemTimeoutDet();
  7173. }
  7174. }
  7175. bool GetStartChargingByAlterMode(byte _gun)
  7176. {
  7177. bool result = true;
  7178. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  7179. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  7180. {
  7181. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  7182. {
  7183. if (_select != _gun)
  7184. {
  7185. if (chargingInfo[_select]->SystemStatus != SYS_MODE_IDLE &&
  7186. chargingInfo[_select]->SystemStatus != SYS_MODE_RESERVATION)
  7187. {
  7188. result = false;
  7189. break;
  7190. }
  7191. }
  7192. }
  7193. }
  7194. return result;
  7195. }
  7196. void TheEndCharging(byte gun_index)
  7197. {
  7198. chargingInfo[gun_index]->isRemoteStart = NO;
  7199. StopGunInfoTimeoutDet(gun_index);
  7200. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  7201. ChangeStartOrStopDateTime(NO, gun_index);
  7202. DB_Insert_Record(localDb, gun_index);
  7203. }
  7204. void AdjustChargerCurrent()
  7205. {
  7206. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  7207. // 設定的電流~ 如超過可輸出的電流,則 bypass
  7208. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent ||
  7209. ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0)
  7210. {
  7211. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  7212. }
  7213. PRINTF_FUNC ("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  7214. ShmPsuData->SystemAvailablePower / 10,
  7215. ShmPsuData->SystemAvailableCurrent / 10
  7216. );
  7217. PRINTF_FUNC ("Config : ChargingPower = %d, ChargingCurrent = %d",
  7218. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  7219. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent
  7220. );
  7221. }
  7222. void CheckStatusOfMeter()
  7223. {
  7224. // 橋接如果搭上~ 則兩個電表都是使用狀態
  7225. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  7226. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  7227. {
  7228. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  7229. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  7230. }
  7231. else
  7232. {
  7233. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  7234. {
  7235. if (chargingInfo[mIndex]->RelayKPK2Status == YES || chargingInfo[mIndex]->RelayK1K2Status)
  7236. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  7237. else
  7238. ShmCsuMeterData->_meter[mIndex].isCalculation = NO;
  7239. }
  7240. }
  7241. }
  7242. void ReviewCriticalAlarm()
  7243. {
  7244. if (ShmDcCommonData->GunRelayDrivingOccur[0] == YES ||
  7245. ShmDcCommonData->GunRelayDrivingOccur[1] == YES ||
  7246. ShmDcCommonData->GunRelayWeldingOccur[0] == YES ||
  7247. ShmDcCommonData->GunRelayWeldingOccur[1] == YES ||
  7248. ShmDcCommonData->ParaRelayWeldingOccur == YES ||
  7249. ShmDcCommonData->ParaRelayDrivingOccur == YES ||
  7250. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  7251. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip ||
  7252. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen ||
  7253. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  7254. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm ||
  7255. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU ||
  7256. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost)
  7257. {
  7258. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_CRITICAL;
  7259. }
  7260. else
  7261. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_NORMAL;
  7262. }
  7263. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  7264. {
  7265. // relay welding fault then stop the charging process.
  7266. byte faultCode = RELAY_STATUS_ERROR_NONE;
  7267. if (gun_index == 0)
  7268. {
  7269. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7270. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7271. {
  7272. faultCode = RELAY_STATUS_ERROR_WELDING;
  7273. }
  7274. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7275. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7276. {
  7277. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7278. }
  7279. }
  7280. else if (gun_index == 1)
  7281. {
  7282. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7283. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7284. {
  7285. faultCode = RELAY_STATUS_ERROR_WELDING;
  7286. }
  7287. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7288. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7289. {
  7290. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7291. }
  7292. }
  7293. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 &&
  7294. !ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  7295. {
  7296. // 橋接
  7297. if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7298. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7299. {
  7300. faultCode = RELAY_STATUS_ERROR_PARA_WELDING;
  7301. }
  7302. else if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7303. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7304. {
  7305. faultCode = RELAY_STATUS_ERROR_PARA_DRIVING;
  7306. }
  7307. }
  7308. if (faultCode == RELAY_STATUS_ERROR_WELDING)
  7309. {
  7310. // welding
  7311. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7312. RecordAlarmCode(gun_index, "011011");
  7313. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7314. RecordAlarmCode(gun_index, "011015");
  7315. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7316. RecordAlarmCode(gun_index, "011013");
  7317. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = YES;
  7318. EmcOccurThenStopCharging();
  7319. }
  7320. else if (faultCode == RELAY_STATUS_ERROR_DRIVING)
  7321. {
  7322. // driving
  7323. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7324. RecordAlarmCode(gun_index, "011012");
  7325. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7326. RecordAlarmCode(gun_index, "011016");
  7327. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7328. RecordAlarmCode(gun_index, "011014");
  7329. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = YES;
  7330. EmcOccurThenStopCharging();
  7331. }
  7332. else
  7333. {
  7334. if (chargingInfo[gun_index]->SystemStatus <= SYS_MODE_TERMINATING ||
  7335. chargingInfo[gun_index]->SystemStatus >= SYS_MODE_ALARM)
  7336. {
  7337. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = NO;
  7338. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = NO;
  7339. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7340. {
  7341. ResetChargerAlarmCode(gun_index, "011012");
  7342. ResetChargerAlarmCode(gun_index, "011011");
  7343. }
  7344. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7345. {
  7346. ResetChargerAlarmCode(gun_index, "011014");
  7347. ResetChargerAlarmCode(gun_index, "011013");
  7348. }
  7349. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7350. {
  7351. ResetChargerAlarmCode(gun_index, "011016");
  7352. ResetChargerAlarmCode(gun_index, "011015");
  7353. }
  7354. }
  7355. }
  7356. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2)
  7357. {
  7358. if ((chargingInfo[0]->SystemStatus <= SYS_MODE_TERMINATING ||
  7359. chargingInfo[0]->SystemStatus >= SYS_MODE_ALARM) &&
  7360. (chargingInfo[1]->SystemStatus <= SYS_MODE_TERMINATING ||
  7361. chargingInfo[1]->SystemStatus >= SYS_MODE_ALARM))
  7362. {
  7363. if (faultCode == RELAY_STATUS_ERROR_PARA_WELDING)
  7364. {
  7365. // parallel welding
  7366. RecordAlarmCode ( gun_index, "011039" );
  7367. ShmDcCommonData->ParaRelayWeldingOccur = YES;
  7368. EmcOccurThenStopCharging ();
  7369. }
  7370. else if (faultCode == RELAY_STATUS_ERROR_PARA_DRIVING)
  7371. {
  7372. // parallel driving
  7373. RecordAlarmCode ( gun_index, "011040" );
  7374. ShmDcCommonData->ParaRelayDrivingOccur = YES;
  7375. EmcOccurThenStopCharging ();
  7376. }
  7377. else
  7378. {
  7379. ShmDcCommonData->ParaRelayWeldingOccur = NO;
  7380. ShmDcCommonData->ParaRelayDrivingOccur = NO;
  7381. ResetChargerAlarmCode ( gun_index, "011039" );
  7382. ResetChargerAlarmCode ( gun_index, "011040" );
  7383. }
  7384. }
  7385. }
  7386. }
  7387. bool PrecheckIsPass(byte gun_index)
  7388. {
  7389. bool result = true;
  7390. // relay welding or driving 是反向
  7391. if (result)
  7392. result = !ShmDcCommonData->GunRelayWeldingOccur[gun_index];
  7393. if (result)
  7394. result = !ShmDcCommonData->ParaRelayWeldingOccur;
  7395. return result;
  7396. }
  7397. void CollectError(byte gun_index)
  7398. {
  7399. errCollect.GunErrMessage |= ShmDcCommonData->ConnectErrList[gun_index].GunErrMessage;
  7400. }
  7401. void AlarmCheck(byte type)
  7402. {
  7403. // type : Cha = 0、CCS = 1、GBT = 2
  7404. if (type == _Type_Chademo)
  7405. {
  7406. // GFD Trip
  7407. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7408. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  7409. else
  7410. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  7411. // UVP
  7412. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7413. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  7414. else
  7415. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  7416. // OTP
  7417. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7418. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = YES;
  7419. else
  7420. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = NO;
  7421. // OVP
  7422. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7423. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  7424. else
  7425. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  7426. // GFD Warning
  7427. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7428. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  7429. else
  7430. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  7431. // Relay Welding
  7432. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7433. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  7434. else
  7435. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  7436. // Relay Driving
  7437. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7438. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  7439. else
  7440. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  7441. // Connect temp Sensor broken
  7442. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7443. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  7444. else
  7445. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  7446. // Output UCP
  7447. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 0))
  7448. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = YES;
  7449. else
  7450. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = NO;
  7451. }
  7452. else if (type == _Type_CCS)
  7453. {
  7454. // GFD Trip
  7455. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7456. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  7457. else
  7458. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  7459. // UVP
  7460. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7461. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  7462. else
  7463. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  7464. // OTP
  7465. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7466. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = YES;
  7467. else
  7468. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = NO;
  7469. // OVP
  7470. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7471. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  7472. else
  7473. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  7474. // GFD Warning
  7475. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7476. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  7477. else
  7478. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  7479. // Relay Welding
  7480. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7481. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  7482. else
  7483. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  7484. // Relay Driving
  7485. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7486. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  7487. else
  7488. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  7489. // Connect temp Sensor broken
  7490. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7491. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  7492. else
  7493. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  7494. // Output UCP
  7495. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 1))
  7496. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = YES;
  7497. else
  7498. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = NO;
  7499. }
  7500. else if (type == _Type_GB)
  7501. {
  7502. // GFD Trip
  7503. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7504. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  7505. else
  7506. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  7507. // UVP
  7508. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7509. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  7510. else
  7511. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  7512. // OTP
  7513. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7514. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = YES;
  7515. else
  7516. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = NO;
  7517. // OVP
  7518. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7519. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  7520. else
  7521. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  7522. // GFD Warning
  7523. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7524. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  7525. else
  7526. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  7527. // Relay Welding
  7528. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7529. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  7530. else
  7531. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  7532. // Relay Driving
  7533. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7534. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  7535. else
  7536. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  7537. // Connect temp Sensor broken
  7538. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7539. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  7540. else
  7541. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  7542. // Output UCP
  7543. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 2))
  7544. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = YES;
  7545. else
  7546. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = NO;
  7547. }
  7548. }
  7549. void SystemAlarmCheck()
  7550. {
  7551. byte system = 3;
  7552. // Parallel Welding
  7553. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 3))
  7554. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7555. else
  7556. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7557. // Parallel Driving
  7558. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 4))
  7559. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7560. else
  7561. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7562. }
  7563. void SavePresentChargedDuration(byte gun_index)
  7564. {
  7565. int _diffTime = 0;
  7566. ftime(&endChargingTime[gun_index]);
  7567. _diffTime = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  7568. // below 0 or over 5 sec is abnormal
  7569. if (_diffTime < 0 ||
  7570. _diffTime > chargingInfo[gun_index]->PresentChargedDuration + 5)
  7571. {
  7572. _presentChargingTimeBuf[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  7573. ftime(&startChargingTime[gun_index]);
  7574. PRINTF_FUNC("TimeZone changed (%d) \n", gun_index);
  7575. }
  7576. else
  7577. chargingInfo[gun_index]->PresentChargedDuration = _presentChargingTimeBuf[gun_index] + _diffTime;
  7578. }
  7579. void ClearEnergyTimePeriod(byte gun_index)
  7580. {
  7581. for (int i = 0; i < 24; i++)
  7582. {
  7583. ShmDcCommonData->energy_time_period[gun_index][i] = 0;
  7584. }
  7585. }
  7586. void ChangeToMaxChargingMode()
  7587. {
  7588. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  7589. ShmPsuData->PsuStopChargeFlag == NO)
  7590. {
  7591. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  7592. {
  7593. // 均充 -> 最大充
  7594. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  7595. {
  7596. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  7597. {
  7598. // 切換最大充起始位置
  7599. PRINTF_FUNC("======= Aver -> Max : Preparing. (Step 11) ======= \n");
  7600. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  7601. }
  7602. }
  7603. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  7604. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  7605. {
  7606. PRINTF_FUNC("======= Aver -> Max : Current Balancing. (Step 14) ======= \n");
  7607. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  7608. }
  7609. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  7610. {
  7611. PRINTF_FUNC("======= Aver -> Max : Charging mode = MAX ======= \n");
  7612. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7613. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7614. }
  7615. }
  7616. else
  7617. {
  7618. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7619. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7620. }
  7621. }
  7622. else
  7623. {
  7624. // 需考量如果再切換均充過程 ? (預計要進入充電的臨時取消充電)
  7625. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7626. }
  7627. }
  7628. void CheckOcppCostAndPrice()
  7629. {
  7630. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling &&
  7631. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  7632. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  7633. {
  7634. ocpp_parsing_DT_defaultPrice_cmd();
  7635. ocpp_parsing_DT_userPrice_cmd();
  7636. ocpp_parsing_DT_running_final_cmd();
  7637. }
  7638. }
  7639. void CheckAuthrizeByEvccid()
  7640. {
  7641. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  7642. ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID)
  7643. ShmDcCommonData->enObtainAuthorizbyCCID = YES;
  7644. else
  7645. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  7646. }
  7647. void CheckConnectorOTP(byte _index)
  7648. {
  7649. if (chargingInfo[_index]->Type == _Type_Chademo)
  7650. {
  7651. // if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7652. // {
  7653. // ResetChargerAlarmCode(_index, "011018");
  7654. // if (chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7655. // RecordAlarmCode(_index, "012229");
  7656. // else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7657. // chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7658. // ResetChargerAlarmCode(_index, "012229");
  7659. // }
  7660. // else
  7661. // {
  7662. // // 沒接上 Sensor or 異常
  7663. // //RecordAlarmCode(_index, "011018");
  7664. // ResetChargerAlarmCode(_index, "012229");
  7665. // }
  7666. }
  7667. else if (chargingInfo[_index]->Type == _Type_CCS)
  7668. {
  7669. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  7670. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7671. {
  7672. ResetChargerAlarmCode(_index, "011019");
  7673. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7674. RecordAlarmCode(_index, "012230");
  7675. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7676. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7677. ResetChargerAlarmCode(_index, "012230");
  7678. }
  7679. else
  7680. {
  7681. // 沒接上 Sensor or 異常
  7682. RecordAlarmCode(_index, "011019");
  7683. ResetChargerAlarmCode(_index, "012230");
  7684. }
  7685. }
  7686. else if (chargingInfo[_index]->Type == _Type_GB &&
  7687. chargingInfo[_index]->ModelType == 'B')
  7688. {
  7689. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7690. {
  7691. ResetChargerAlarmCode(_index, "011020");
  7692. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7693. RecordAlarmCode(_index, "012231");
  7694. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7695. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7696. ResetChargerAlarmCode(_index, "012231");
  7697. }
  7698. else
  7699. {
  7700. // 沒接上 Sensor or 異常
  7701. RecordAlarmCode(_index, "011020");
  7702. ResetChargerAlarmCode(_index, "012231");
  7703. }
  7704. }
  7705. }
  7706. void ChkConnectorBoardStop(byte index)
  7707. {
  7708. if (isEvBoardStopChargeFlag(index) == YES)
  7709. {
  7710. // 板端要求停止 (錯誤)
  7711. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  7712. ChargingAlarmProcess(index);
  7713. }
  7714. else if (isEvBoardNormalStopChargeFlag(index) == YES)
  7715. {
  7716. // 板端要求停止 (正常)
  7717. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  7718. ChargingTerminalProcess(index);
  7719. }
  7720. }
  7721. void ChkPsuErrorStop(byte index)
  7722. {
  7723. if (ShmPsuData->PsuGroup[index].GroupErrorFlag.bits.PsuFailure == YES ||
  7724. (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf && ShmPsuData->PsuGroup[0].GroupErrorFlag.bits.PsuFailure))
  7725. {
  7726. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  7727. {
  7728. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7729. {
  7730. if (chargingInfo[gun_index]->PresentChargingCurrent <= 5 &&
  7731. ((chargingInfo[gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  7732. (chargingInfo[gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  7733. {
  7734. ocpp_set_stopReason_by_cmd(gun_index, "Local");
  7735. ChargingAlarmProcess(gun_index);
  7736. }
  7737. }
  7738. }
  7739. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  7740. {
  7741. if (chargingInfo[index]->PresentChargingCurrent <= 5)
  7742. {
  7743. ocpp_set_stopReason_by_cmd(index, "Local");
  7744. ChargingAlarmProcess(index);
  7745. }
  7746. }
  7747. }
  7748. }
  7749. void ChkBackendStop(byte index)
  7750. {
  7751. if (ocpp_chk_invalid_id_cmd(index))
  7752. {
  7753. // Start Transaction Stop
  7754. ocpp_set_stopReason_by_cmd(index, "DeAuthorized");
  7755. ChargingTerminalProcess(index);
  7756. }
  7757. else if (ocpp_chk_remoteStop_cmd(index) == YES ||
  7758. WifiScheduleStop(index))
  7759. {
  7760. // 後臺要求停止
  7761. ChargingTerminalProcess(index);
  7762. }
  7763. }
  7764. void ChkConfigurationStop(byte index)
  7765. {
  7766. if (CheckBackendChargingTimeout(index) ||
  7767. CheckBackendChargingEnergy(index))
  7768. {
  7769. // 網頁設定
  7770. PRINTF_FUNC("CheckBackendCharging stop charging (%d) \n", index);
  7771. ChargingTerminalProcess(index);
  7772. return;
  7773. }
  7774. if (CheckOcppChargingTimeout(index) ||
  7775. CheckOcppChargingEnergy(index) ||
  7776. CheckOcppChargingSOC(index))
  7777. {
  7778. // OCPP 設定
  7779. PRINTF_FUNC("CheckOcppCharging stop charging (%d) \n", index);
  7780. ChargingTerminalProcess(index);
  7781. }
  7782. }
  7783. void RecordGfdResult(byte index)
  7784. {
  7785. if (chargingInfo[index]->Type == _Type_Chademo)
  7786. {
  7787. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  7788. {
  7789. // GFD 錯誤停止
  7790. RecordAlarmCode(index, "012234");
  7791. }
  7792. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  7793. {
  7794. // GFD 警告
  7795. RecordAlarmCode(index, "012296");
  7796. }
  7797. }
  7798. else if (chargingInfo[index]->Type == _Type_GB)
  7799. {
  7800. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  7801. {
  7802. // GFD 錯誤停止
  7803. RecordAlarmCode(index, "012236");
  7804. }
  7805. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  7806. {
  7807. // GFD 警告
  7808. RecordAlarmCode(index, "012298");
  7809. }
  7810. }
  7811. else if (chargingInfo[index]->Type == _Type_CCS)
  7812. {
  7813. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  7814. {
  7815. // GFD 錯誤停止
  7816. RecordAlarmCode(index, "012235");
  7817. }
  7818. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  7819. {
  7820. // GFD 警告
  7821. RecordAlarmCode(index, "012297");
  7822. }
  7823. }
  7824. }
  7825. void CcidAuthorizationProcessing(byte gun_index)
  7826. {
  7827. if (chargingInfo [gun_index]->Type == _Type_CCS &&
  7828. chargingInfo [gun_index]->IsAvailable &&
  7829. ShmDcCommonData->enObtainAuthorizbyCCID &&
  7830. chargingInfo[gun_index]->ConnectorPlugIn &&
  7831. strcmp ((char *)ShmSysConfigAndInfo->SysConfig.UserId, "") == EQUAL)
  7832. {
  7833. AddPlugInTimes(gun_index);
  7834. PRINTF_FUNC("CcidAuthorizationProcessing. \n");
  7835. ChangeGunSelectByIndex(gun_index);
  7836. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  7837. }
  7838. }
  7839. void CheckCcidAuthorizationFunction(byte gun_index)
  7840. {
  7841. if (chargingInfo [gun_index]->Type == _Type_CCS &&
  7842. ShmDcCommonData->enObtainAuthorizbyCCID)
  7843. {
  7844. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_GET &&
  7845. ShmDcCommonData->_CcidAuthProcessStep == _CCID_NONE &&
  7846. strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" ) == EQUAL)
  7847. {
  7848. PRINTF_FUNC ( "index = %d, Get for CCID (main). \n", gun_index );
  7849. ShmDcCommonData->_authWithCcidFlag [gun_index] = _CCID_CHECK;
  7850. ShmDcCommonData->_CcidAuthProcessStep = _CCID_CHECK;
  7851. memcpy ( ShmSysConfigAndInfo->SysConfig.UserId, chargingInfo [gun_index]->EVCCID,
  7852. sizeof(chargingInfo [gun_index]->EVCCID) );
  7853. }
  7854. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  7855. {
  7856. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHFAIL)
  7857. {
  7858. PRINTF_FUNC ( "index = %d, _CCID_AUTHFAIL. \n", gun_index );
  7859. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  7860. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  7861. }
  7862. else if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  7863. {
  7864. PRINTF_FUNC ( "index = %d, _CCID_AUTHCOMP. \n", gun_index );
  7865. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  7866. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  7867. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  7868. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  7869. }
  7870. }
  7871. }
  7872. }
  7873. bool isCcidEnterToCharging(byte gun_index)
  7874. {
  7875. bool result = false;
  7876. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  7877. {
  7878. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  7879. {
  7880. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7881. {
  7882. if (chargingInfo [_index]->Type == _Type_CCS &&
  7883. chargingInfo [_index]->IsAvailable &&
  7884. ShmDcCommonData->_authWithCcidFlag [_index] == _CCID_CHECK)
  7885. {
  7886. if (_index != gun_index)
  7887. result = true;
  7888. break;
  7889. }
  7890. }
  7891. if (!result)
  7892. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  7893. }
  7894. }
  7895. return result;
  7896. }
  7897. bool IsNoneSelectAcModule()
  7898. {
  7899. bool result = false;
  7900. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0 ||
  7901. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  7902. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))
  7903. {
  7904. result = true;
  7905. }
  7906. return result;
  7907. }
  7908. bool IsSelectAcModule()
  7909. {
  7910. bool result = false;
  7911. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  7912. return result;
  7913. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  7914. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  7915. {
  7916. result = true;
  7917. }
  7918. return result;
  7919. }
  7920. void DisplayPageChkByAcModule()
  7921. {
  7922. if (IsSelectAcModule())
  7923. {
  7924. if (ac_chargingInfo [0]->ConnectorPlugIn == AC_SYS_A)
  7925. {
  7926. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  7927. }
  7928. else
  7929. {
  7930. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7931. }
  7932. }
  7933. }
  7934. int main(void)
  7935. {
  7936. if(CreateShareMemory() == 0)
  7937. {
  7938. #ifdef SystemLogMessage
  7939. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  7940. #endif
  7941. if(ShmStatusCodeData!=NULL)
  7942. {
  7943. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  7944. }
  7945. sleep(5);
  7946. system("reboot -f");
  7947. sleep(5);
  7948. system("reboot -f");
  7949. }
  7950. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  7951. if (!InitialSystemDefaultConfig())
  7952. {
  7953. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  7954. StopProcessingLoop();
  7955. }
  7956. PRINTF_FUNC("CheckConnectorTypeStatus and CheckIsAlternatvie \n");
  7957. CheckGunTypeFromHw();
  7958. CheckIsAlternatvieByModelName();
  7959. InitialShareMemoryInfo();
  7960. PRINTF_FUNC("InitialShareMemoryInfo OK. \n");
  7961. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  7962. PRINTF_FUNC("ChargerType (IEC or UL) = IEC \n");
  7963. else
  7964. PRINTF_FUNC("ChargerType (IEC or UL) = UL \n");
  7965. ChangeLcmByIndex(_LCM_INIT);
  7966. if (!CheckConnectorTypeStatus())
  7967. isModelNameMatch = false;
  7968. Initialization();
  7969. PRINTF_FUNC("===== Initial Complete ===== \n");
  7970. PRINTF_FUNC("===== Spawn all Task and self test start ===== \n");
  7971. SpawnTask();
  7972. if (!isModelNameMatch)
  7973. {
  7974. // Module Name 與硬體對應不正確
  7975. PRINTF_FUNC("*** Module Name & HW info none match : Please check if the Model name and HW are matched. ***\n");
  7976. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  7977. ChangeLcmByIndex(_LCM_FIX);
  7978. sleep(3);
  7979. KillTask();
  7980. StopProcessingLoop();
  7981. }
  7982. PRINTF_FUNC("Module Name & HW info correct. \n");
  7983. CreateTimeoutFork();
  7984. if (!DEBUG_ALSTON)
  7985. {
  7986. PRINTF_FUNC("Self test run..\n");
  7987. SelfTestRun();
  7988. }
  7989. else
  7990. {
  7991. sleep(3);
  7992. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  7993. }
  7994. StopSystemTimeoutDet();
  7995. if (!DEBUG_ALSTON &&
  7996. (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  7997. ShmPsuData->Work_Step == _NO_WORKING ||
  7998. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ||
  7999. strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL))
  8000. {
  8001. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  8002. {
  8003. if (!DisplaySelfTestFailReason() &&
  8004. ShmDcCommonData->rebootCount < 1)
  8005. {
  8006. PRINTF_FUNC("=======Soft reboot for retry self-test======= \n");
  8007. SetupRebootCount(1);
  8008. sleep(3);
  8009. KillAllTask();
  8010. system("/usr/bin/run_evse_restart.sh");
  8011. }
  8012. else if (ShmDcCommonData->rebootCount < 1)
  8013. {
  8014. PRINTF_FUNC("=======Soft reboot for retry self-test (Normal)======= \n");
  8015. SetupRebootCount(1);
  8016. sleep(3);
  8017. KillAllTask();
  8018. system("/usr/bin/run_evse_restart.sh");
  8019. }
  8020. else
  8021. SetupRebootCount(0);
  8022. }
  8023. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8024. {
  8025. ChargingAlarmProcess(gun_index);
  8026. }
  8027. //ChangeLcmByIndex(_LCM_FIX);
  8028. sleep(3);
  8029. //if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  8030. //{
  8031. KillTaskExceptDetectTask();
  8032. //}
  8033. //else
  8034. // KillTask();
  8035. StopProcessingLoop();
  8036. }
  8037. else
  8038. {
  8039. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8040. {
  8041. setChargerMode(gun_index, SYS_MODE_IDLE);
  8042. }
  8043. SetupRebootCount(0);
  8044. }
  8045. PRINTF_FUNC("===== Self Test Complete ===== \n");
  8046. PRINTF_FUNC("===== Finally Create DB ===== \n");
  8047. // Local DB
  8048. if(DB_Open(localDb) != PASS)
  8049. {
  8050. PRINTF_FUNC("DB_Open fail. \n");
  8051. isDb_ready = false;
  8052. }
  8053. else
  8054. {
  8055. isDb_ready = true;
  8056. for (int _acIndex = 0; _acIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _acIndex++)
  8057. {
  8058. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  8059. ac_chargingInfo[0]->IsAvailable = DB_Get_Operactive(localDb, _acIndex + hasDc);
  8060. }
  8061. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  8062. {
  8063. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  8064. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  8065. }
  8066. DB_Reboot_Record(localDb);
  8067. }
  8068. if (NetworkDB_Open(networkDb) != PASS)
  8069. {
  8070. PRINTF_FUNC("NetworkDB_Open fail. \n");
  8071. }
  8072. ChangeLcmByIndex(_LCM_IDLE);
  8073. sleep(1);
  8074. CreateRfidFork();
  8075. sleep(1);
  8076. if (!DEBUG_ALSTON)
  8077. {
  8078. PRINTF_FUNC("===== Create chk system Task ===== \n");
  8079. CreateCheckSystemTaskFork();
  8080. }
  8081. PRINTF_FUNC("===== Create DB End ===== \n");
  8082. PRINTF_FUNC("===== Charger info ===== ");
  8083. PRINTF_FUNC("SW Version = %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  8084. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  8085. CheckFwSlotStatusLog();
  8086. AdjustChargerCurrent();
  8087. GetTimespecFunc(&_cmdMainPriority_time);
  8088. _ocppProfileChkFlag_1 = 0;
  8089. _ocppProfileChkFlag_2 = 0;
  8090. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == EQUAL)
  8091. {
  8092. DEBUG_ERROR_MSG("URL is empty kill Module_OcppBackend...\n");
  8093. system ("pkill OcppBackend");
  8094. }
  8095. int _mainTimebuf = 0;
  8096. for (;;)
  8097. {
  8098. if (ShmDcCommonData->_isAutoRunTest != _isTestRun)
  8099. {
  8100. if (ShmDcCommonData->_isAutoRunTest == YES)
  8101. {
  8102. GetTimespecFunc(&_autoRuntestTime);
  8103. }
  8104. _isTestRun = ShmDcCommonData->_isAutoRunTest;
  8105. }
  8106. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8107. {
  8108. ocpp_ac_chk_availability_cmd();
  8109. ocpp_ac_chk_unlock_cmd();
  8110. }
  8111. ocpp_auto_response_BootNotification();
  8112. ocpp_chk_reset_cmd(NO);
  8113. ButtonTriggerEvent();
  8114. if (!DEBUG_ALSTON)
  8115. ChkPrimaryStatus();
  8116. if ((IsConnectorWholeIdle() ||
  8117. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX))
  8118. {
  8119. CheckFactoryConfigFunction();
  8120. CheckFwUpdateFunction();
  8121. }
  8122. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  8123. {
  8124. sleep(1);
  8125. continue;
  8126. }
  8127. // OCPP 邏輯
  8128. ocpp_chk_remoteStart_cmd();
  8129. // 讀卡邏輯
  8130. ScannerCardProcess();
  8131. // 當 AC 沒有搭上時,清除一些錯誤碼
  8132. ClearAlarmCodeWhenAcOff();
  8133. // 確認是否要回到充電中的槍畫面邏輯判斷
  8134. CheckReturnToChargingConn();
  8135. if ((_acgunIndex > 0 && isDetectPlugin() && !isCardScan))
  8136. {
  8137. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8138. }
  8139. _mainTimebuf = GetTimeoutValue(&_cmdMainPriority_time);
  8140. if (_mainTimebuf < 0)
  8141. GetTimespecFunc(&_cmdMainPriority_time);
  8142. if (GetTimeoutValue(&_cmdMainPriority_time) >= 5)
  8143. {
  8144. if (ShmPsuData->PsuStopChargeFlag)
  8145. Check267Alarm();
  8146. CheckTask();
  8147. Run4gResetProc();
  8148. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8149. {
  8150. if ((chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8151. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8152. {
  8153. ocpp_chargingProfile_process(_index);
  8154. if (chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  8155. ((_index == 0 && _ocppProfileChkFlag_1 >= 12) ||
  8156. (_index == 1 && _ocppProfileChkFlag_2 >= 12)))
  8157. {
  8158. ocpp_lift_profileReq_cmd(_index);
  8159. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  8160. if (_index == 0)
  8161. _ocppProfileChkFlag_1 = 0;
  8162. else if (_index == 1)
  8163. _ocppProfileChkFlag_2 = 0;
  8164. }
  8165. else if (chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  8166. {
  8167. ocpp_lift_profileReq_cmd(_index);
  8168. if (_index == 0)
  8169. _ocppProfileChkFlag_1 = 0;
  8170. else if (_index == 1)
  8171. _ocppProfileChkFlag_2 = 0;
  8172. }
  8173. else
  8174. {
  8175. if (_index == 0)
  8176. _ocppProfileChkFlag_1++;
  8177. else if (_index == 1)
  8178. _ocppProfileChkFlag_2++;
  8179. }
  8180. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  8181. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  8182. {
  8183. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  8184. }
  8185. }
  8186. if (chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION)
  8187. {
  8188. if (opcc_chk_reserve_expired(_index))
  8189. {
  8190. chargingInfo[_index]->SystemStatus = SYS_MODE_IDLE;
  8191. }
  8192. }
  8193. // OTP
  8194. CheckConnectorOTP(_index);
  8195. }
  8196. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  8197. {
  8198. if ((ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING) ||
  8199. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8200. {
  8201. ocpp_ac_chargingProfile_process();
  8202. }
  8203. if ((ac_chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARING &&
  8204. ac_chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING))
  8205. {
  8206. if (ac_chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  8207. _ac_ocppProfileChkFlag == 12)
  8208. {
  8209. ocpp_ac_lift_profileReq_cmd(_index);
  8210. _ac_ocppProfileChkFlag = 0;
  8211. }
  8212. else if (ac_chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  8213. {
  8214. ocpp_ac_lift_profileReq_cmd(_index);
  8215. _ac_ocppProfileChkFlag = 0;
  8216. }
  8217. else
  8218. _ac_ocppProfileChkFlag++;
  8219. }
  8220. }
  8221. CheckOcppCostAndPrice();
  8222. CheckAuthrizeByEvccid();
  8223. GetTimespecFunc(&_cmdMainPriority_time);
  8224. if (!DEBUG_ALSTON && wtdFd == -1)
  8225. {
  8226. PRINTF_FUNC("===== Create crazy dog ===== \n");
  8227. CreateWatchdog();
  8228. }
  8229. }
  8230. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  8231. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  8232. CheckStatusOfMeter();
  8233. // 確認當前錯誤 Level = 2 ?
  8234. ReviewCriticalAlarm();
  8235. errCollect.GunErrMessage = 0;
  8236. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8237. {
  8238. CheckAcOcppProfileConfirm();
  8239. ocpp_ac_chk_reserved_cmd();
  8240. }
  8241. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8242. {
  8243. //CheckGpioInStatus();
  8244. // 確認系統的錯誤狀態
  8245. CheckSystemErrorFunction(gun_index);
  8246. // 確認 Relay Welding or Driving Fault
  8247. CheckRelayWeldingOrDrivingFault(gun_index);
  8248. // 收集各槍的錯誤狀態
  8249. CollectError(gun_index);
  8250. ocpp_chk_reserved_cmd(gun_index);
  8251. ocpp_chk_availability_cmd(gun_index);
  8252. ocpp_chk_unlock_cmd(gun_index);
  8253. // check charging profile confirm flag
  8254. CheckOcppProfileConfirm(gun_index);
  8255. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  8256. switch(chargingInfo[gun_index]->SystemStatus)
  8257. {
  8258. case SYS_MODE_IDLE:
  8259. case SYS_MODE_RESERVATION:
  8260. case SYS_MODE_MAINTAIN:
  8261. case SYS_MODE_FAULT:
  8262. {
  8263. if ((chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  8264. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION) &&
  8265. isModeChange(gun_index))
  8266. {
  8267. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  8268. {
  8269. PRINTF_FUNC("================= SYS_MODE_IDLE (%x) ================ \n", gun_index);
  8270. chargingInfo[gun_index]->ReservationId = -1;
  8271. }
  8272. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  8273. {
  8274. PRINTF_FUNC("================= SYS_MODE_RESERVATION (%x) ================ \n", gun_index);
  8275. PRINTF_FUNC("================= Id : %s ================ \n", ShmDcCommonData->_reserved_UserId[gun_index]);
  8276. }
  8277. chargingInfo[gun_index]->PresentChargedDuration = 0;
  8278. chargingInfo[gun_index]->RemainChargingDuration = 0;
  8279. chargingInfo[gun_index]->PresentChargingVoltage = 0;
  8280. chargingInfo[gun_index]->PresentChargingCurrent = 0;
  8281. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "00:00:00");
  8282. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "00:00:00");
  8283. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8284. strcpy((char *)chargingInfo[gun_index]->EVCCID, "");
  8285. _isSendStartTransaction[gun_index] = false;
  8286. ClearEnergyTimePeriod(gun_index);
  8287. gunOutputVol[gun_index] = 0.0;
  8288. ocpp_clear_stopReason(gun_index);
  8289. ocpp_auto_response_StartTransationConf(gun_index);
  8290. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  8291. ShmDcCommonData->_authWithCcidFlag[gun_index] = _CCID_NONE;
  8292. }
  8293. if (chargingInfo[gun_index]->IsAvailable == NO)
  8294. {
  8295. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  8296. }
  8297. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  8298. {
  8299. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8300. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8301. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_IDLE &&
  8302. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_RESERVATION &&
  8303. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_MAINTAIN &&
  8304. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_FAULT)
  8305. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8306. ClearDetectPluginFlag();
  8307. ocpp_set_errCode_cmd(gun_index);
  8308. setChargerMode(gun_index, SYS_MODE_FAULT);
  8309. }
  8310. else
  8311. {
  8312. ChangeToMaxChargingMode();
  8313. if (PrecheckIsPass(gun_index))
  8314. {
  8315. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  8316. {
  8317. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8318. setChargerMode(gun_index, SYS_MODE_IDLE);
  8319. }
  8320. CcidAuthorizationProcessing(gun_index);
  8321. //
  8322. {
  8323. // Idle 正常程序起點
  8324. // 判斷是否有啟用檢查插槍
  8325. if(isDetectPlugin())
  8326. {
  8327. // 卡號驗證成功後,等待充電槍插入充電車
  8328. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  8329. {
  8330. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8331. chargingInfo[gun_index]->IsAvailable)
  8332. {
  8333. PRINTF_FUNC("Conn %d Start Charging : Remote \n", gun_index);
  8334. chargingInfo[gun_index]->RemoteStartFlag = NO;
  8335. chargingInfo[gun_index]->isRemoteStart = YES;
  8336. ChangeGunSelectByIndex(gun_index);
  8337. AddPlugInTimes(gun_index);
  8338. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8339. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8340. ClearDetectPluginFlag();
  8341. continue;
  8342. }
  8343. }
  8344. else if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  8345. {
  8346. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8347. chargingInfo[gun_index]->IsAvailable)
  8348. {
  8349. chargingInfo[gun_index]->ReservedStartFlag = NO;
  8350. ChangeGunSelectByIndex(gun_index);
  8351. AddPlugInTimes(gun_index);
  8352. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmDcCommonData->_reserved_UserId[gun_index]);
  8353. PRINTF_FUNC("Conn %d Start Charging : Reserved CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  8354. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8355. strcpy((char *)ShmDcCommonData->_reserved_UserId[gun_index], "");
  8356. // 當前操作的槍號,進入 Preparing
  8357. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8358. ClearDetectPluginFlag();
  8359. continue;
  8360. }
  8361. }
  8362. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  8363. {
  8364. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8365. chargingInfo[gun_index]->IsAvailable &&
  8366. chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  8367. {
  8368. ChangeGunSelectByIndex(gun_index);
  8369. AddPlugInTimes(gun_index);
  8370. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  8371. PRINTF_FUNC("Conn %d Start Charging : RFID CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  8372. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8373. // 當前操作的槍號,進入 Preparing
  8374. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8375. ClearDetectPluginFlag();
  8376. continue;
  8377. }
  8378. }
  8379. if (!isCardScan &&
  8380. (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  8381. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)))
  8382. {
  8383. // LCM => Waiting for plugging
  8384. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8385. }
  8386. }
  8387. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  8388. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  8389. {
  8390. PRINTF_FUNC("Conn %d Start Charging : Plug and charing \n", gun_index);
  8391. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  8392. if (isCanStartChargingFlag)
  8393. {
  8394. ChangeGunSelectByIndex(gun_index);
  8395. AddPlugInTimes(gun_index);
  8396. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8397. ClearDetectPluginFlag();
  8398. ocpp_get_freeVendor(gun_index);
  8399. continue;
  8400. }
  8401. }
  8402. else
  8403. {
  8404. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8405. {
  8406. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8407. ocpp_remote_start_wait_cmd()))
  8408. {
  8409. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8410. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  8411. {
  8412. if (ac_chargingInfo[0]->ConnectorPlugIn == AC_SYS_A)
  8413. {
  8414. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8415. }
  8416. else
  8417. {
  8418. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8419. }
  8420. }
  8421. else
  8422. {
  8423. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8424. }
  8425. }
  8426. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  8427. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_WAIT_FOR_PLUG)
  8428. {
  8429. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8430. }
  8431. // else if (ChkAcModuleStatusIsNone() &&
  8432. // (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL))
  8433. // {
  8434. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8435. // }
  8436. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  8437. }
  8438. }
  8439. } // Idle 正常程序終點
  8440. }
  8441. else
  8442. {
  8443. setChargerMode(gun_index, SYS_MODE_FAULT);
  8444. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8445. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8446. }
  8447. ReleaseSysAlarmCode(gun_index);
  8448. }
  8449. }
  8450. break;
  8451. case SYS_MODE_MODE_REASSIGN_CHECK:
  8452. {
  8453. if (isModeChange(gun_index))
  8454. {
  8455. PRINTF_FUNC("================= SYS_MODE_MODE_REASSIGN_CHECK (%x) ================ \n", gun_index);
  8456. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8457. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  8458. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  8459. StopSystemTimeoutDet();
  8460. }
  8461. ocpp_clear_errorCode_cmd(gun_index);
  8462. bool isRessign = false;
  8463. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  8464. {
  8465. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  8466. {
  8467. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  8468. {
  8469. if (chargingInfo[index]->SystemStatus != SYS_MODE_MODE_REASSIGN_CHECK)
  8470. {
  8471. if (((chargingInfo[index]->SystemStatus >= SYS_MODE_PREPARING && chargingInfo[index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8472. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)) &&
  8473. chargingInfo[index]->SystemStatus != SYS_MODE_MAINTAIN &&
  8474. chargingInfo[index]->SystemStatus != SYS_MODE_RESERVATION)
  8475. {
  8476. // A 槍在充電過程中 - 進行分配動作
  8477. PRINTF_FUNC("======= Max -> Aver : Preparing. (Step 1) ======= \n");
  8478. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  8479. isRessign = true;
  8480. break;
  8481. }
  8482. else
  8483. {
  8484. // A 槍已結束充電 - 直接回 B 槍最大充
  8485. PRINTF_FUNC("======= Max -> Aver : Charging mode = MAX (Keep) ======= \n");
  8486. //ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  8487. break;
  8488. }
  8489. }
  8490. }
  8491. }
  8492. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  8493. {
  8494. // 如果在切換最大充的過程中,需等待最大充切換完成後,再走均充流程
  8495. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  8496. {
  8497. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  8498. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  8499. {
  8500. PRINTF_FUNC("======= Aver -> Max : Aver -> Max : Current Balancing. (Step 14) ======= \n");
  8501. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  8502. }
  8503. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  8504. {
  8505. PRINTF_FUNC("======= Aver -> Max : Charging mode = MAX ======= \n");
  8506. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  8507. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  8508. continue;
  8509. }
  8510. }
  8511. else
  8512. {
  8513. setChargerMode(gun_index, SYS_MODE_PREPARING);
  8514. }
  8515. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8516. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8517. continue;
  8518. }
  8519. }
  8520. if (isRessign)
  8521. setChargerMode(gun_index, SYS_MODE_REASSIGN);
  8522. else
  8523. setChargerMode(gun_index, SYS_MODE_PREPARING);
  8524. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8525. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8526. {
  8527. if (IsNoneSelectAcModule())
  8528. {
  8529. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8530. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8531. {
  8532. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8533. }
  8534. }
  8535. else
  8536. {
  8537. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8538. ocpp_remote_start_wait_cmd ()))
  8539. {
  8540. DisplayPageChkByAcModule();
  8541. }
  8542. }
  8543. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8544. }
  8545. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8546. {
  8547. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8548. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8549. }
  8550. }
  8551. break;
  8552. case SYS_MODE_REASSIGN:
  8553. {
  8554. if (isModeChange(gun_index))
  8555. {
  8556. PRINTF_FUNC("================== SYS_MODE_REASSIGN (%x) ================ \n", gun_index);
  8557. }
  8558. ocpp_clear_errorCode_cmd(gun_index);
  8559. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  8560. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  8561. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  8562. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  8563. {
  8564. PRINTF_FUNC("======= Max -> Aver : Charging mode = AVER ======= \n");
  8565. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  8566. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  8567. }
  8568. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  8569. {
  8570. if ((ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO &&
  8571. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER) ||
  8572. (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES &&
  8573. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX))
  8574. {
  8575. setChargerMode(gun_index, SYS_MODE_PREPARING);
  8576. }
  8577. }
  8578. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8579. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8580. {
  8581. if (IsNoneSelectAcModule())
  8582. {
  8583. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8584. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8585. {
  8586. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8587. }
  8588. }
  8589. else
  8590. {
  8591. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8592. ocpp_remote_start_wait_cmd ()))
  8593. {
  8594. DisplayPageChkByAcModule();
  8595. }
  8596. }
  8597. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8598. }
  8599. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8600. {
  8601. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8602. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8603. }
  8604. }
  8605. break;
  8606. case SYS_MODE_PREPARING:
  8607. {
  8608. if (isModeChange(gun_index))
  8609. {
  8610. PRINTF_FUNC("=================== SYS_MODE_PREPARING (%x) =============== \n", gun_index);
  8611. StopGunInfoTimeoutDet(gun_index);
  8612. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  8613. }
  8614. ocpp_clear_errorCode_cmd(gun_index);
  8615. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10 &&
  8616. ShmPsuData->Work_Step == _WORK_CHARGING &&
  8617. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 5)
  8618. {
  8619. setChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EV);
  8620. }
  8621. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2)
  8622. {
  8623. bool oughtAver = true;
  8624. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  8625. {
  8626. // 共同進入充電邏輯
  8627. if (chargingInfo[index]->SystemStatus != SYS_MODE_PREPARING)
  8628. {
  8629. oughtAver = false;
  8630. break;
  8631. }
  8632. }
  8633. if (oughtAver)
  8634. {
  8635. PRINTF_FUNC("********* Smart Charging : _REASSIGNED_NONE (AVER) ********** \n");
  8636. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  8637. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  8638. }
  8639. }
  8640. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8641. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8642. {
  8643. if (IsNoneSelectAcModule())
  8644. {
  8645. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8646. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8647. {
  8648. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8649. }
  8650. }
  8651. else
  8652. {
  8653. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8654. ocpp_remote_start_wait_cmd ()))
  8655. {
  8656. DisplayPageChkByAcModule();
  8657. }
  8658. }
  8659. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8660. }
  8661. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8662. {
  8663. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8664. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8665. }
  8666. }
  8667. break;
  8668. case SYS_MODE_PREPARE_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  8669. {
  8670. if (isModeChange(gun_index))
  8671. {
  8672. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EV (%x) ================ \n", gun_index);
  8673. _presentChargingTimeBuf[gun_index] = 0;
  8674. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8675. StopGunInfoTimeoutDet(gun_index);
  8676. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  8677. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  8678. // Start Transation
  8679. printf("StartUserId = %s \n", chargingInfo[gun_index]->StartUserId);
  8680. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8681. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  8682. chargingInfo[gun_index]->isRemoteStart == YES)
  8683. {
  8684. _isSendStartTransaction[gun_index] = true;
  8685. ocpp_set_startTransation_cmd(gun_index);
  8686. GetTimespecFunc(&_startTransation_time[gun_index]);
  8687. }
  8688. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8689. }
  8690. ocpp_clear_errorCode_cmd(gun_index);
  8691. CheckCcidAuthorizationFunction(gun_index);
  8692. if (!_isSendStartTransaction[gun_index] &&
  8693. chargingInfo [gun_index]->Type == _Type_CCS &&
  8694. ShmDcCommonData->enObtainAuthorizbyCCID)
  8695. {
  8696. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_AUTHCOMP)
  8697. {
  8698. PRINTF_FUNC("_CCID_AUTHCOMP \n");
  8699. _isSendStartTransaction[gun_index] = true;
  8700. ocpp_set_startTransation_cmd(gun_index);
  8701. GetTimespecFunc(&_startTransation_time[gun_index]);
  8702. }
  8703. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_AUTHFAIL)
  8704. {
  8705. PRINTF_FUNC("_CCID_AUTHFAIL \n");
  8706. ChargingTerminalProcess(gun_index);
  8707. }
  8708. }
  8709. // if (isCcidEnterToCharging(gun_index))
  8710. // {
  8711. //
  8712. // }
  8713. ShmDcCommonData->startTransactionFlag[gun_index] = ocpp_is_resPass_StartTransactionConf(gun_index);
  8714. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_PASS)
  8715. {
  8716. if (ocpp_chk_invalid_id_cmd(gun_index))
  8717. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_FAIL;
  8718. }
  8719. else
  8720. {
  8721. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  8722. {
  8723. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING ||
  8724. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8725. !ocpp_get_startTransation_req(gun_index) ||
  8726. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  8727. _isTestRun)
  8728. {
  8729. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_PASS;
  8730. }
  8731. }
  8732. }
  8733. // 小板停止
  8734. ChkConnectorBoardStop(gun_index);
  8735. // psu 停止
  8736. ChkPsuErrorStop(gun_index);
  8737. if (ocpp_chk_remoteStop_cmd(gun_index) == YES ||
  8738. WifiScheduleStop(gun_index) ||
  8739. (_isSendStartTransaction[gun_index] && ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL) ||
  8740. (_isSendStartTransaction[gun_index] && ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT && GetTimeoutValue(&_startTransation_time[gun_index]) > 45))
  8741. {
  8742. // 後臺要求停止
  8743. ChargingTerminalProcess(gun_index);
  8744. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL)
  8745. PRINTF_FUNC("StartTransaction req fail. (%d) \n", gun_index);
  8746. else if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT)
  8747. PRINTF_FUNC("StartTransaction req timeout. (%d) \n", gun_index);
  8748. }
  8749. // 檢查車端的槍鎖是否為鎖上
  8750. if (isEvGunLocked(gun_index) == YES)
  8751. {
  8752. setChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EVSE);
  8753. }
  8754. // LCM => Pre-charging
  8755. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8756. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8757. {
  8758. if (IsNoneSelectAcModule())
  8759. {
  8760. // if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8761. // ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8762. // {
  8763. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8764. // }
  8765. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  8766. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  8767. { }
  8768. else
  8769. {
  8770. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8771. }
  8772. }
  8773. else
  8774. {
  8775. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8776. ocpp_remote_start_wait_cmd ()))
  8777. {
  8778. DisplayPageChkByAcModule();
  8779. }
  8780. }
  8781. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_NONE)
  8782. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8783. }
  8784. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8785. {
  8786. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  8787. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  8788. {}
  8789. else
  8790. {
  8791. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8792. }
  8793. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8794. }
  8795. }
  8796. break;
  8797. case SYS_MODE_PREPARE_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  8798. {
  8799. if (isModeChange(gun_index))
  8800. {
  8801. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EVSE (%x) ================ \n", gun_index);
  8802. StopGunInfoTimeoutDet(gun_index);
  8803. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  8804. }
  8805. ocpp_clear_errorCode_cmd(gun_index);
  8806. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8807. {
  8808. // 檢查樁端的 GFD 結果
  8809. if (isPrechargeStatus_chademo(gun_index) >= 6)
  8810. {
  8811. // 當前操作的槍號,進入 Charging
  8812. setChargerMode(gun_index, SYS_MODE_CHARGING);
  8813. }
  8814. }
  8815. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8816. {
  8817. // 檢查樁端的 GFD 結果
  8818. if (isPrechargeStatus_gb(gun_index) >= 6)
  8819. {
  8820. setChargerMode(gun_index, SYS_MODE_CHARGING);
  8821. }
  8822. }
  8823. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8824. {
  8825. // 檢查樁端的 GFD 結果
  8826. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  8827. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  8828. {
  8829. setChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP0);
  8830. }
  8831. }
  8832. RecordGfdResult(gun_index);
  8833. // 小板停止
  8834. ChkConnectorBoardStop(gun_index);
  8835. // psu 停止
  8836. ChkPsuErrorStop(gun_index);
  8837. // 後台要求停止
  8838. ChkBackendStop(gun_index);
  8839. // LCM => Pre-charging
  8840. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8841. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8842. {
  8843. if (IsNoneSelectAcModule())
  8844. {
  8845. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8846. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8847. {
  8848. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8849. }
  8850. }
  8851. else
  8852. {
  8853. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8854. ocpp_remote_start_wait_cmd ()))
  8855. {
  8856. DisplayPageChkByAcModule();
  8857. }
  8858. }
  8859. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8860. }
  8861. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8862. {
  8863. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8864. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8865. }
  8866. }
  8867. break;
  8868. case SYS_MODE_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  8869. {
  8870. if (isModeChange(gun_index))
  8871. {
  8872. PRINTF_FUNC("================== SYS_MODE_CHARGING (%x) ================ \n", gun_index);
  8873. StopGunInfoTimeoutDet(gun_index);
  8874. ftime(&startChargingTime[gun_index]);
  8875. ocpp_clear_idTag_cmd(gun_index);
  8876. ChangeStartOrStopDateTime(YES, gun_index);
  8877. if (_isTestRun == YES)
  8878. {
  8879. GetTimespecFunc(&_autoRuntestTime);
  8880. }
  8881. }
  8882. ocpp_clear_errorCode_cmd(gun_index);
  8883. if (_isTestRun == YES)
  8884. {
  8885. if (GetTimeoutValue(&_autoRuntestTime) > 60)
  8886. {
  8887. ChargingTerminalProcess(gun_index);
  8888. }
  8889. }
  8890. SavePresentChargedDuration(gun_index);
  8891. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8892. {
  8893. if (ShmDcCommonData->minOutputCur != 0 &&
  8894. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  8895. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8896. {
  8897. // UCP
  8898. RecordAlarmCode(gun_index, "012320");
  8899. ChargingTerminalProcess(gun_index);
  8900. }
  8901. }
  8902. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8903. {
  8904. if (ShmDcCommonData->minOutputCur != 0 &&
  8905. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  8906. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8907. {
  8908. // UCP
  8909. RecordAlarmCode(gun_index, "012322");
  8910. ChargingTerminalProcess(gun_index);
  8911. }
  8912. }
  8913. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8914. {
  8915. if (ShmDcCommonData->minOutputCur != 0 &&
  8916. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  8917. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8918. {
  8919. // UCP
  8920. RecordAlarmCode(gun_index, "012321");
  8921. ChargingTerminalProcess(gun_index);
  8922. }
  8923. }
  8924. RecordGfdResult(gun_index);
  8925. // 小板停止
  8926. ChkConnectorBoardStop(gun_index);
  8927. // psu 停止
  8928. ChkPsuErrorStop(gun_index);
  8929. // 後台要求停止
  8930. ChkBackendStop(gun_index);
  8931. // 設定停止
  8932. ChkConfigurationStop(gun_index);
  8933. // LCM => Charging
  8934. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8935. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8936. {
  8937. if (IsNoneSelectAcModule() || !stopChargingChkByCard)
  8938. {
  8939. // if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8940. // ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8941. // {
  8942. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8943. // }
  8944. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  8945. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  8946. { }
  8947. else
  8948. {
  8949. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8950. }
  8951. }
  8952. else
  8953. {
  8954. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8955. ocpp_remote_start_wait_cmd ()))
  8956. {
  8957. DisplayPageChkByAcModule();
  8958. }
  8959. }
  8960. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  8961. }
  8962. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8963. {
  8964. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  8965. if (!stopChargingChkByCard)
  8966. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8967. }
  8968. }
  8969. break;
  8970. case SYS_MODE_ALARM:
  8971. case SYS_MODE_TERMINATING:
  8972. {
  8973. if (isModeChange(gun_index))
  8974. {
  8975. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  8976. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  8977. ocpp_set_stopReason_by_cmd(gun_index, "EmergencyStop");
  8978. else
  8979. ocpp_set_stopReason_by_cmd(gun_index, "Local");
  8980. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  8981. {
  8982. PRINTF_FUNC("================== SYS_MODE_ALARM (%x) ================ \n", gun_index);
  8983. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  8984. {
  8985. ocpp_auto_response_StartTransationConf(gun_index);
  8986. ocpp_set_stopTransation_cmd(gun_index);
  8987. }
  8988. TheEndCharging(gun_index);
  8989. }
  8990. else
  8991. {
  8992. PRINTF_FUNC("================== SYS_MODE_TERMINATING (%x) ================ \n", gun_index);
  8993. }
  8994. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  8995. StopGunInfoTimeoutDet(gun_index);
  8996. }
  8997. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  8998. {
  8999. ChangeToMaxChargingMode();
  9000. ocpp_set_errCode_cmd(gun_index);
  9001. }
  9002. if (chargingInfo[gun_index]->Type == _Type_Chademo) { }
  9003. else if (chargingInfo[gun_index]->Type == _Type_GB) { }
  9004. else if (chargingInfo[gun_index]->Type == _Type_CCS) { }
  9005. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9006. {
  9007. if (chargingInfo [gun_index]->ConnectorPlugIn == NO)
  9008. {
  9009. if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == NO &&
  9010. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  9011. {
  9012. ShmDcCommonData->_returnIdleHoldFlag [gun_index] = YES;
  9013. StopGunInfoTimeoutDet ( gun_index );
  9014. StartGunInfoTimeoutDet ( gun_index, Timeout_EvseCompleteDet );
  9015. }
  9016. else if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == YES &&
  9017. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 2)
  9018. {
  9019. setChargerMode ( gun_index, SYS_MODE_IDLE );
  9020. }
  9021. }
  9022. else if (_isTestRun == YES &&
  9023. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9024. {
  9025. setChargerMode(gun_index, SYS_MODE_IDLE);
  9026. }
  9027. }
  9028. else
  9029. {
  9030. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9031. {
  9032. if (isEvGunLocked(gun_index) == NO ||
  9033. isPrechargeStatus_chademo(gun_index) <= 0)
  9034. {
  9035. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  9036. }
  9037. }
  9038. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9039. {
  9040. if (isEvGunLocked(gun_index) == NO ||
  9041. isPrechargeStatus_gb(gun_index) <= 0)
  9042. {
  9043. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  9044. }
  9045. }
  9046. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9047. {
  9048. if (isEvGunLocked(gun_index) == NO &&
  9049. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  9050. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  9051. {
  9052. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  9053. }
  9054. }
  9055. }
  9056. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9057. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9058. {
  9059. if (IsNoneSelectAcModule())
  9060. {
  9061. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9062. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9063. {
  9064. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9065. }
  9066. }
  9067. else
  9068. {
  9069. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9070. ocpp_remote_start_wait_cmd ()))
  9071. {
  9072. DisplayPageChkByAcModule();
  9073. }
  9074. }
  9075. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9076. }
  9077. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9078. {
  9079. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9080. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9081. }
  9082. }
  9083. break;
  9084. case SYS_MODE_COMPLETE:
  9085. {
  9086. if (isModeChange(gun_index))
  9087. {
  9088. PRINTF_FUNC("================== SYS_MODE_COMPLETE (%x) ================ \n", gun_index);
  9089. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  9090. {
  9091. ocpp_set_errCode_cmd(gun_index);
  9092. ocpp_auto_response_StartTransationConf(gun_index);
  9093. ocpp_set_stopTransation_cmd(gun_index);
  9094. }
  9095. TheEndCharging(gun_index);
  9096. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  9097. }
  9098. ocpp_clear_errorCode_cmd(gun_index);
  9099. ChangeToMaxChargingMode();
  9100. if (chargingInfo[gun_index]->ConnectorPlugIn == NO)
  9101. {
  9102. if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == NO &&
  9103. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9104. {
  9105. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = YES;
  9106. StopGunInfoTimeoutDet(gun_index);
  9107. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  9108. }
  9109. else if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == YES &&
  9110. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 2)
  9111. {
  9112. setChargerMode(gun_index, SYS_MODE_IDLE);
  9113. }
  9114. }
  9115. else if (_isTestRun == YES &&
  9116. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9117. {
  9118. setChargerMode(gun_index, SYS_MODE_IDLE);
  9119. }
  9120. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9121. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9122. {
  9123. if (IsNoneSelectAcModule())
  9124. {
  9125. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9126. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9127. {
  9128. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9129. }
  9130. }
  9131. else
  9132. {
  9133. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9134. ocpp_remote_start_wait_cmd ()))
  9135. {
  9136. DisplayPageChkByAcModule();
  9137. }
  9138. }
  9139. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9140. }
  9141. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9142. {
  9143. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9144. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9145. }
  9146. }
  9147. break;
  9148. case SYS_MODE_CCS_PRECHARGE_STEP0:
  9149. {
  9150. if (isModeChange(gun_index))
  9151. {
  9152. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP0 (%x) ================ \n", gun_index);
  9153. StopGunInfoTimeoutDet(gun_index);
  9154. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  9155. }
  9156. ocpp_clear_errorCode_cmd(gun_index);
  9157. RecordGfdResult(gun_index);
  9158. // 小板停止
  9159. ChkConnectorBoardStop(gun_index);
  9160. // psu 停止
  9161. ChkPsuErrorStop(gun_index);
  9162. // 後台要求停止
  9163. ChkBackendStop(gun_index);
  9164. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  9165. // 切換 D+ Relay to Precharge Relay
  9166. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  9167. {
  9168. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  9169. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  9170. {
  9171. if (isPrechargeStatus_ccs(gun_index) == 39)
  9172. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  9173. else if (isPrechargeStatus_ccs(gun_index) == 40)
  9174. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  9175. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  9176. }
  9177. }
  9178. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  9179. {
  9180. setChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP1);
  9181. }
  9182. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9183. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9184. {
  9185. if (IsNoneSelectAcModule())
  9186. {
  9187. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9188. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9189. {
  9190. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9191. }
  9192. }
  9193. else
  9194. {
  9195. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9196. ocpp_remote_start_wait_cmd ()))
  9197. {
  9198. DisplayPageChkByAcModule();
  9199. }
  9200. }
  9201. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9202. }
  9203. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9204. {
  9205. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9206. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9207. }
  9208. break;
  9209. }
  9210. case SYS_MODE_CCS_PRECHARGE_STEP1:
  9211. {
  9212. if (isModeChange(gun_index))
  9213. {
  9214. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP1 (%x) ================ \n", gun_index);
  9215. }
  9216. ocpp_clear_errorCode_cmd(gun_index);
  9217. RecordGfdResult(gun_index);
  9218. // 小板停止
  9219. ChkConnectorBoardStop(gun_index);
  9220. // psu 停止
  9221. ChkPsuErrorStop(gun_index);
  9222. // 後台要求停止
  9223. ChkBackendStop(gun_index);
  9224. // 等待小板通知進入充電
  9225. // 切換 D+ Relay to Precharge Relay
  9226. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  9227. {
  9228. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  9229. setChargerMode(gun_index, SYS_MODE_CHARGING);
  9230. }
  9231. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9232. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9233. {
  9234. if (IsNoneSelectAcModule())
  9235. {
  9236. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9237. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9238. {
  9239. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9240. }
  9241. }
  9242. else
  9243. {
  9244. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9245. ocpp_remote_start_wait_cmd ()))
  9246. {
  9247. DisplayPageChkByAcModule();
  9248. }
  9249. }
  9250. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9251. }
  9252. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9253. {
  9254. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9255. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9256. }
  9257. break;
  9258. }
  9259. }
  9260. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  9261. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION ||
  9262. chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN ||
  9263. chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  9264. {
  9265. ocpp_chk_notificationMessage_cmd(gun_index);
  9266. }
  9267. }
  9268. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  9269. {
  9270. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  9271. }
  9272. else
  9273. {
  9274. bool dcPageRun = false;
  9275. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  9276. {
  9277. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  9278. ChangeLcmByIndex(_LCM_IDLE);
  9279. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING)
  9280. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  9281. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  9282. ChangeLcmByIndex(_LCM_CHARGING);
  9283. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING ||
  9284. ac_chargingInfo[0]->SystemStatus == SYS_MODE_COMPLETE)
  9285. ChangeLcmByIndex(_LCM_COMPLETE);
  9286. else
  9287. dcPageRun = true;
  9288. }
  9289. else
  9290. dcPageRun = true;
  9291. if (dcPageRun)
  9292. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  9293. }
  9294. bool isCharging = false;
  9295. SystemAlarmCheck();
  9296. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9297. {
  9298. AlarmCheck(chargingInfo[gun_index]->Type);
  9299. if (_isTestRun == YES)
  9300. {
  9301. if (IsGunUsing(gun_index))
  9302. {
  9303. isCharging = true;
  9304. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_COMPLETE ||
  9305. chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9306. {
  9307. GetTimespecFunc(&_autoRuntestTime);
  9308. }
  9309. }
  9310. }
  9311. }
  9312. if (_isTestRun == YES)
  9313. {
  9314. if (!isCharging)
  9315. {
  9316. if (GetTimeoutValue(&_autoRuntestTime) >= 60)
  9317. {
  9318. DetectPluginStart();
  9319. GetTimespecFunc(&_autoRuntestTime);
  9320. }
  9321. }
  9322. else
  9323. {
  9324. ClearDetectPluginFlag();
  9325. }
  9326. }
  9327. write(wtdFd, "a", 1);
  9328. usleep(whileLoopTime);
  9329. }
  9330. return FAIL;
  9331. }