main.c 297 KB

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