main.c 301 KB

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