ReMain.c 268 KB

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