ReMain.c 275 KB

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