main.c 255 KB

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