main.c 274 KB

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