main.c 259 KB

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