main.c 224 KB

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