main.c 230 KB

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