main.c 293 KB

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