main.c 258 KB

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