main.c 240 KB

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