main.c 238 KB

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