main.c 361 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336
  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. #include "Module_CabinetParallel.h"
  42. #include "Common.h"
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define MtdBlockSize 0x300000
  47. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  48. #define UPGRADE_FAN 0x02
  49. #define UPGRADE_RB 0x03
  50. #define UPGRADE_PRI 0x04
  51. #define UPGRADE_AC 0x05
  52. #define UPGRADE_LED 0x06
  53. #define UPGRADE_DORC1 0x07
  54. #define UPGRADE_DORC2 0x08
  55. #define UPGRADE_DDRB 0x09
  56. #define SYSTEM_MIN_VOL 150
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define MAIN_PRIORITY_INTERVAL 5
  63. #define CABINET_ROLE_TIMEOUT 3 // unit: 1s
  64. #define GUN_PREPARE_TIMEOUT 30
  65. #define GUN_EV_WAIT_TIMEOUT (120 + 10)
  66. #define GUN_EVSE_WAIT_TIMEOUT (60 + 10)
  67. #define GUN_COMP_WAIT_TIMEOUT 10
  68. #define GUN_PRECHARGING_TIMEOUT 60
  69. #define REBOOT_TIMEOUT 10
  70. #define RESET_TIMEOUT 10
  71. #define OCPP_COST_REQ_INTERVAL 1
  72. #define DISPENSER_RECONNECTE_TIME 30
  73. #define DISPENSER_AUTH_COMP_TIME 3
  74. #define FORCE_BALANCE_TIME 5
  75. #define DISPENSER_UPGRADE_TIME 600
  76. #define AC_CONTACTOR_REOPEN_TIME 60
  77. #define PSU_FAILURE_RESUME_TIME 180
  78. #define MAX_AC_CONTACTOR_COUNT 3
  79. #define STANDBY_TIME 600
  80. #define FAST_STANDBY_TIME 30
  81. #define WIRING_INFO_DELAY 30
  82. #define WAIT_REASSIGN_TIME 40 // unit: second
  83. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  84. #define MIN_WAIT_PREPARNING_TIME 1 // unit: second
  85. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  86. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  87. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  88. #define ENERAGY_INTERVAL 1 // unit: second
  89. #define FAULT_RELEASE_TIME 1 // unit: second
  90. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  91. #define PARALLEL_WAIT_TIME 3 // unit: second
  92. #define START_UPGRADE_TIME 60 // unit: second
  93. #define PCABINET_UPGRADE_TIME 300 // unit: second
  94. #define MCABINET_UPGRADE_TIME 60 // unit: second
  95. #define CONNECTION_TIME_OFFSET 5 // unit: second
  96. #define PLUG_IN_HOLD_TIME 1 // unit: second
  97. #define AUTO_START_CHARGING "AutoStartCharging"
  98. #define CONTACTOR_AUTO_ON_OFF 0
  99. #define CONTACTOR_AUTO_ON_TIME 60
  100. #define CONTACTOR_AUTO_OFF_TIME 300
  101. #define WAIT_DISPENSER_STOP_TIMEOUT 3 // unit: second
  102. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  103. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  104. int whileLoopTime = 10000; // 10 ms
  105. int wtdFd = -1;
  106. byte _authorizeIndex = NO_DEFINE;
  107. BOOL _UpgradeNeedReboot = FALSE;
  108. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  109. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  110. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  111. {
  112. {0, 1, 0, 0},
  113. {1, 0, 0, 0},
  114. {0, 0, 0, 0},
  115. {0, 0, 0, 0}
  116. };
  117. //Location: 1 2 3 4
  118. // Gun: 1 3 4 2
  119. //Parallel: 1 2 3
  120. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  121. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  122. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  123. //{
  124. // {0, 3, 1, 2}, // group 0
  125. // {1, 0, 2, 3}, // group 1
  126. // {1, 3, 0, 2}, // group 2
  127. // {1, 3, 2, 0} // group 3
  128. //};
  129. //Location: 1 2 3 4
  130. // Gun: 1 4 3 2
  131. //Parallel: 1 2 3
  132. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  133. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  134. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  135. {
  136. {0, 3, 2, 1}, // group 0
  137. {1, 0, 3, 2}, // group 1
  138. {1, 3, 0, 2}, // group 2
  139. {1, 3, 2, 0} // group 3
  140. };
  141. const byte GroupParallelRelay_4_Gun_6Parallel[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  142. {
  143. {0, 4, 6, 1}, // group 0
  144. {4, 0, 3, 5}, // group 1
  145. {6, 3, 0, 2}, // group 2
  146. {1, 5, 2, 0} // group 3
  147. };
  148. long long DiffTimebWithNow(struct timeb ST);
  149. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  150. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  151. void ChargingTerminalProcess(byte gunIndex);
  152. void ChkPrimaryStatus();
  153. void ChargingAlarmProcess(byte gunIndex);
  154. void InformOcppErrOccur(byte codeType);
  155. void SetOcppErrorCode(unsigned char _index, char *errCode);
  156. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  157. void ReleaseAlarmCode(byte gunIndex);
  158. int DB_Open(sqlite3 *db);
  159. int DB_Insert_Record(sqlite3 *db, int gun_index);
  160. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  161. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  162. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value);
  163. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value);
  164. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index);
  165. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index);
  166. float Get_PowerConsumption(uint8_t gun_index);
  167. void Update_PowerConsumption(uint8_t gun_index);
  168. int DB_Reboot_Record(sqlite3 *db);
  169. void InitialDHCP();
  170. void InitialDispenserDhcpServerConfig(void);
  171. void StartDispenserDhcpServer(void);
  172. int GetStartScheduleTime(unsigned char *time);
  173. void CheckConnectionTimeout(void);
  174. bool IsParallelCabinetIdle(void);
  175. bool IsStandbyEnable(void);
  176. bool IsConnectorWholeIdle(void);
  177. void SetAcContactor(unsigned char OnOff);
  178. void UpdateErrorCodeToOcpp(byte index);
  179. bool CheckConnectorTypeStatus(void);
  180. void CreateWatchdog(void);
  181. void TryCloseWatchdog(void);
  182. void TryFeedWatchdog(void);
  183. bool IsReadyParalleling(void);
  184. void SetParallelingStart(void);
  185. void SetParallelingDone(void);
  186. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  187. void SetParallelCabinetAcContactor(unsigned char OnOff);
  188. unsigned short GetTotalParallelCabinetPower(void);
  189. void MCabinetParallelingProcess(void);
  190. void SCabinetControllProcess(void);
  191. bool Get_Ocpp_ConnectionStatus(void);
  192. void Run_Ocpp_Service(void);
  193. bool Is_Ocpp_Task_Alive(void);
  194. void Kill_Ocpp_Service(void);
  195. void Run_MaintainOcpp_Service(void);
  196. bool Is_MaintainOcpp_Task_Alive(void);
  197. void Kill_MaintainOcpp_Service(void);
  198. unsigned int Get_Ocpp_procDogTime(void);
  199. void Clean_Ocpp_procDogTime(void);
  200. bool Is_Ocpp_BootNotification(void);
  201. void Clean_Ocpp_BootNotification(void);
  202. bool Is_Ocpp_ResetReq(void);
  203. bool Chk_Ocpp_Reset_Type(char *type);
  204. void Clean_Ocpp_ResetReq(void);
  205. void Set_Ocpp_AuthorizeReq(void);
  206. bool Is_Ocpp_AuthorizeConf(void);
  207. bool Is_Ocpp_Authorize_Status(char *status);
  208. bool Is_Ocpp_UpdateFirmwareReq(void);
  209. void Clean_Ocpp_UpdateFirmwareReq(void);
  210. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  211. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  212. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  213. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  214. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  215. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  216. int Get_Ocpp_ConnectionTimeOut(void);
  217. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  218. void Get_Ocpp_UserPrice(char * userId, char *userPrice);
  219. void Clean_Ocpp_UserPrice(void);
  220. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  221. void Clean_Ocpp_RunningCost(int gun_index);
  222. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  223. void Clean_Ocpp_FinalCost(int gun_index);
  224. int Get_Ocpp_FinalCostTxId(int gun_index);
  225. void Get_Ocpp_TimeOffset(char *timeOffset);
  226. void Get_Ocpp_PreAuthAmount(char *amount);
  227. void Get_Ocpp_PowerSharingServerIP(char *ip);
  228. void Get_Ocpp_Refunding(char *Refunding);
  229. void Clean_Ocpp_Refunding(void);
  230. bool Is_Ocpp_LcdOverideReq(int *gun, int *page);
  231. void Clean_Ocpp_LcdOverideReq(void);
  232. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name);
  233. bool Is_Ocpp_ChargerStationReq(void);
  234. void Clean_Ocpp_ChargerStationReq(void);
  235. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature);
  236. bool Is_Ocpp_WeatherReq(void);
  237. void Clean_Ocpp_WeatherReq(void);
  238. void Set_Ocpp_DeductTransaction(int transaction);
  239. void Set_Ocpp_DeductCreditNo(char *userId);
  240. void Set_Ocpp_DeductVemData(char *vemData);
  241. void Set_Ocpp_DeductAmount(double value);
  242. void Set_Ocpp_DeductResult(unsigned char result);
  243. void Set_Ocpp_DonateInvoice(unsigned char donate);
  244. bool Is_Ocpp_ReportCreditDeductReq(void);
  245. void Set_Ocpp_ReportCreditDeductReq(void);
  246. void Clean_Ocpp_TcciSerialNo(int gun_index);
  247. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus);
  248. void Set_Ocpp_TcciReaderStatusReq(int gun_index);
  249. bool Is_Ocpp_TcciReaderStatusReq(int gun_index);
  250. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id);
  251. void Set_Ocpp_StartTransactionReq(int gun_index);
  252. bool Is_Ocpp_StartTransactionConf(int gun_index);
  253. void Clean_Ocpp_StartTransactionConf(int gun_index);
  254. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  255. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  256. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status);
  257. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  258. bool Is_Invalid_StartTransactionId(int gun_index);
  259. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  260. void Set_Ocpp_StopTransactionReq(int gun_index);
  261. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  262. bool Is_Ocpp_ReserveNowReq(int gun_index);
  263. void Clean_Ocpp_ReserveNowReq(int gun_index);
  264. int Get_Ocpp_ReserveNowId(int gun_index);
  265. void Set_Ocpp_ReserveNowConf(int gun_index);
  266. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  267. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  268. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  269. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  270. bool Is_Ocpp_CancelReservationReq(int gun_index);
  271. void Clean_Ocpp_CancelReservationReq(int gun_index);
  272. void Set_Ocpp_CancelReservationConf(int gun_index);
  273. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  274. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  275. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  276. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  277. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  278. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  279. bool Is_Ocpp_RemoteStartReq(int gun_index);
  280. void Clean_Ocpp_RemoteStartReq(int gun_index);
  281. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  282. bool Is_Ocpp_RemoteStopReq(int gun_index);
  283. void Clean_Ocpp_RemoteStopReq(int gun_index);
  284. void Set_Ocpp_StopReason(int gun_index, char *reason);
  285. void Get_Ocpp_StopReason(int gun_index, char *reason);
  286. void Clean_Ocpp_StopReason(int gun_index);
  287. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  288. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  289. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  290. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  291. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  292. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  293. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  294. void Set_Ocpp_ChargingProfileReq(int gun_index);
  295. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  296. bool Is_Ocpp_ChargingProfileId(int gun_index);
  297. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  298. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  299. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  300. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  301. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  302. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  303. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  304. bool Is_Ocpp_MaxChargingProfileId(void);
  305. int Get_Ocpp_MaxProfileStartTime(void);
  306. int Get_Ocpp_MaxProfileScheduleCount(void);
  307. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  308. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  309. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  310. int Get_Ocpp_MaxProfileStartIndex(void);
  311. int Is_Ocpp_RemoteStartWaitReq(void);
  312. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget);
  313. void Set_Deduct_UserId(int gunIndex, char *userId);
  314. void Set_Deduct_TransactionId(int gunIndex, int transaction);
  315. void Clean_DeductInfo(int gunIndex);
  316. bool Is_DeductResultReq(int gunIndex);
  317. bool Is_ReaderStatusReq(int gunIndex);
  318. void Clean_ReaderStatusInfo(int gunIndex);
  319. void Set_UserTransactionId(int gunIndex, int transaction);
  320. void Set_UserPrice(int gunIndex, float price);
  321. void Set_UserTotalCost(int gunIndex, float cost);
  322. void Set_UserAccountBalance(int gunIndex, float balance);
  323. void Set_UserDiscount(int gunIndex, float discount);
  324. void Clean_UserTransaction(int gunIndex);
  325. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  326. struct StatusCodeData *ShmStatusCodeData;
  327. struct PsuData *ShmPsuData;
  328. struct CHAdeMOData *ShmCHAdeMOData;
  329. struct GBTData *ShmGBTData;
  330. struct CcsData *ShmCcsData;
  331. struct PrimaryMcuData *ShmPrimaryMcuData;
  332. struct FanModuleData *ShmFanModuleData;
  333. struct RelayModuleData *ShmRelayModuleData[2];
  334. struct LedModuleData *ShmLedModuleData;
  335. struct OCPP16Data *ShmOCPP16Data;
  336. struct OCPP20Data *ShmOCPP20Data;
  337. struct OCPP16Data *ShmOCPP16DataPH;
  338. struct MeterInformation *ShmCsuMeterData;
  339. ChargerInfoData *ShmChargerInfo;
  340. PsuPositionInfoData *ShmPsuPosition;
  341. PsuGroupingInfoData *ShmPsuGrouping;
  342. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  343. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  344. struct timespec _cmdMainPriority_time;
  345. struct timespec _cmdSubPriority_time;
  346. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  347. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  348. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  349. struct timespec _DispenserUpgrade_time;
  350. struct timespec _Standby_time;
  351. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  352. struct timespec _Parallel_Time;
  353. struct timespec _RemoteStartNoId_Time;
  354. float gunOutputVol[CONNECTOR_QUANTITY];
  355. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  356. float _lastProfilePower[CONNECTOR_QUANTITY];
  357. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  358. float _lastMaxProfilePower;
  359. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  360. bool _useGunMeterValue[MAX_GUN_QUANTITY];
  361. bool _triggerTransaction[MAX_GUN_QUANTITY];
  362. bool _plugInHold[MAX_GUN_QUANTITY];
  363. struct timespec _PlugInHold_Time[MAX_GUN_QUANTITY];
  364. bool _NeedReset4gWifi;
  365. struct timespec _4gWifiReset_time;
  366. // for initial index to check EV board type is correct
  367. byte _gunIndex = 0;
  368. byte _acgunIndex = 0;
  369. byte _chademoIndex = 0;
  370. byte _ccsIndex = 0;
  371. byte _gb_Index = 0;
  372. byte _ac_Index = 0;
  373. bool _isStandbyEnable = true;
  374. bool isModelNameMatch = true;
  375. //int rfidFd = -1;
  376. //char* rfidPortName = "/dev/ttyS2";
  377. #if ENABLE_PCBA_TEST == 0
  378. char* fwVersion = "V2.02.00.0000.00";
  379. char* subVersion = "00";
  380. #else
  381. char* fwVersion = "PCBA.00.04";
  382. char* subVersion = "00";
  383. #endif
  384. sqlite3 *localDb;
  385. bool isDb_ready;
  386. sqlite3 *networkDb;
  387. //================================================
  388. // initial can-bus
  389. //================================================
  390. int InitCanBus()
  391. {
  392. int s0,nbytes;
  393. struct timeval tv;
  394. struct ifreq ifr0;
  395. struct sockaddr_can addr0;
  396. system("/sbin/ip link set can0 down");
  397. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  398. system("/sbin/ip link set can0 up");
  399. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  400. tv.tv_sec = 0;
  401. tv.tv_usec = 10000;
  402. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  403. {
  404. #ifdef SystemLogMessage
  405. LOG_ERROR("Set SO_RCVTIMEO NG");
  406. #endif
  407. }
  408. nbytes=40960;
  409. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  410. {
  411. #ifdef SystemLogMessage
  412. LOG_ERROR("Set SO_RCVBUF NG");
  413. #endif
  414. }
  415. nbytes=40960;
  416. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  417. {
  418. #ifdef SystemLogMessage
  419. LOG_ERROR("Set SO_SNDBUF NG");
  420. #endif
  421. }
  422. strcpy(ifr0.ifr_name, "can0" );
  423. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  424. addr0.can_family = AF_CAN;
  425. addr0.can_ifindex = ifr0.ifr_ifindex;
  426. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  427. return s0;
  428. }
  429. //================================================
  430. // initial uart port
  431. //================================================
  432. char *_priPortName = "/dev/ttyS1";
  433. char *_485PortName = "/dev/ttyS5";
  434. int InitComPort(byte target)
  435. {
  436. int fd;
  437. struct termios tios;
  438. if(target == UPGRADE_PRI)
  439. fd = open(_priPortName, O_RDWR);
  440. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  441. fd = open(_485PortName, O_RDWR);
  442. if(fd<=0)
  443. {
  444. #ifdef SystemLogMessage
  445. LOG_ERROR("open 407 Communication port NG");
  446. #endif
  447. return -1;
  448. }
  449. ioctl (fd, TCGETS, &tios);
  450. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  451. tios.c_lflag = 0;
  452. tios.c_iflag = 0;
  453. tios.c_oflag = 0;
  454. tios.c_cc[VMIN]=0;
  455. tios.c_cc[VTIME]=(unsigned char)1;
  456. tios.c_lflag=0;
  457. tcflush(fd, TCIFLUSH);
  458. ioctl (fd, TCSETS, &tios);
  459. return fd;
  460. }
  461. //=================================
  462. // Common routine
  463. //=================================
  464. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  465. {
  466. strncpy(dest, src + start, cnt);
  467. dest[cnt] = 0;
  468. }
  469. int InitWatchDog()
  470. {
  471. int fd;
  472. int timeout = 180;
  473. system("/usr/bin/fuser -k /dev/watchdog");
  474. sleep(1);
  475. system("echo V > /dev/watchdog");
  476. sleep(1);
  477. fd=open("/dev/watchdog", O_RDWR);
  478. if(fd<=0)
  479. {
  480. LOG_ERROR("System watch dog initial fail.\r\n");
  481. }
  482. ioctl(fd, _IOWR('W', 6, int), &timeout);
  483. return fd;
  484. }
  485. void setChargerMode(byte gun_index, byte mode)
  486. {
  487. chargingInfo[gun_index]->SystemStatus = mode;
  488. }
  489. long long DiffTimebWithNow(struct timeb ST)
  490. {
  491. //return milli-second
  492. struct timeb ET;
  493. long long StartTime,StopTime;
  494. ftime(&ET);
  495. StartTime=(long long)ST.time;
  496. StopTime=(long long)ET.time;
  497. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  498. }
  499. //=================================
  500. // Create all share memory
  501. //=================================
  502. int CreateShareMemory()
  503. {
  504. int MeterSMId;
  505. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  506. {
  507. #ifdef SystemLogMessage
  508. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  509. #endif
  510. return 0;
  511. }
  512. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  513. {
  514. #ifdef SystemLogMessage
  515. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  516. #endif
  517. return 0;
  518. }
  519. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  520. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  521. {
  522. #ifdef SystemLogMessage
  523. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  524. #endif
  525. return 0;
  526. }
  527. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  528. {
  529. #ifdef SystemLogMessage
  530. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  531. #endif
  532. return 0;
  533. }
  534. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  535. //creat ShmPsuData
  536. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  537. {
  538. #ifdef SystemLogMessage
  539. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  540. #endif
  541. return 0;
  542. }
  543. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  544. {
  545. #ifdef SystemLogMessage
  546. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  547. #endif
  548. return 0;
  549. }
  550. memset(ShmPsuData, 0, sizeof(struct PsuData));
  551. /*
  552. if(CHAdeMO_QUANTITY > 0)
  553. {
  554. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  555. {
  556. #ifdef SystemLogMessage
  557. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  558. #endif
  559. return 0;
  560. }
  561. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  562. {
  563. #ifdef SystemLogMessage
  564. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  565. #endif
  566. return 0;
  567. }
  568. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  569. }
  570. if(GB_QUANTITY > 0)
  571. {
  572. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  573. {
  574. #ifdef SystemLogMessage
  575. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  576. #endif
  577. return 0;
  578. }
  579. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  580. {
  581. #ifdef SystemLogMessage
  582. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  583. #endif
  584. return 0;
  585. }
  586. memset(ShmGBTData, 0, sizeof(struct GBTData));
  587. }
  588. //creat ShmCcsData
  589. if(CCS_QUANTITY > 0)
  590. {
  591. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  592. {
  593. #ifdef SystemLogMessage
  594. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  595. #endif
  596. return 0;
  597. }
  598. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  599. {
  600. #ifdef SystemLogMessage
  601. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  602. #endif
  603. return 0;
  604. }
  605. memset(ShmCcsData, 0, sizeof(struct CcsData));
  606. }
  607. */
  608. //creat ShmPrimaryMcuData
  609. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  610. {
  611. #ifdef SystemLogMessage
  612. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  613. #endif
  614. return 0;
  615. }
  616. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  617. {
  618. #ifdef SystemLogMessage
  619. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  620. #endif
  621. return 0;
  622. }
  623. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  624. //creat ShmFanModuleData
  625. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  626. {
  627. #ifdef SystemLogMessage
  628. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  629. #endif
  630. return 0;
  631. }
  632. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  633. {
  634. #ifdef SystemLogMessage
  635. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  636. #endif
  637. return 0;
  638. }
  639. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  640. //creat ShmRelayModuleData
  641. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  642. {
  643. #ifdef SystemLogMessage
  644. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  645. #endif
  646. return 0;
  647. }
  648. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  649. {
  650. #ifdef SystemLogMessage
  651. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  652. #endif
  653. return 0;
  654. }
  655. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  656. // DO360 RC2
  657. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  658. {
  659. #ifdef SystemLogMessage
  660. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  661. #endif
  662. return 0;
  663. }
  664. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  665. {
  666. #ifdef SystemLogMessage
  667. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  668. #endif
  669. return 0;
  670. }
  671. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  672. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  673. {
  674. #ifdef SystemLogMessage
  675. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  676. #endif
  677. return 0;
  678. }
  679. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  680. {
  681. #ifdef SystemLogMessage
  682. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  683. #endif
  684. return 0;
  685. }
  686. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  687. //creat ShmOCPP16Data
  688. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  689. {
  690. #ifdef SystemLogMessage
  691. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  692. #endif
  693. return 0;
  694. }
  695. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  696. {
  697. #ifdef SystemLogMessage
  698. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  699. #endif
  700. return 0;
  701. }
  702. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  703. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  704. {
  705. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG");
  706. return 0;
  707. }
  708. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  709. {
  710. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG");
  711. return 0;
  712. }
  713. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  714. //creat ShmOCPP16DataPH
  715. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  716. {
  717. LOG_ERROR("shmget ShmOCPP16DataPH NG");
  718. return 0;
  719. }
  720. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  721. {
  722. LOG_ERROR("shmat ShmOCPP16DataPH NG");
  723. return 0;
  724. }
  725. memset(ShmOCPP16DataPH,0,sizeof(struct OCPP16Data));
  726. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  727. {
  728. #ifdef SystemLogMessage
  729. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  730. #endif
  731. return 0;
  732. }
  733. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  734. {
  735. #ifdef SystemLogMessage
  736. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  737. #endif
  738. return 0;
  739. }
  740. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  741. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  742. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  743. return 1;
  744. }
  745. //======================================================
  746. // Peripheral initial
  747. //======================================================
  748. void InitGPIO()
  749. {
  750. /*****************0~3, 4 bank, bank x 32+ num*********************/
  751. /***************************************************************/
  752. /*************** GPIO 0 ***************************************/
  753. /***************************************************************/
  754. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  755. system("echo 22 > /sys/class/gpio/export");
  756. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  757. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  758. system("echo 23 > /sys/class/gpio/export");
  759. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  760. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  761. system("echo 26 > /sys/class/gpio/export");
  762. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  763. system("echo 1 > /sys/class/gpio/gpio26/value");
  764. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  765. system("echo 27 > /sys/class/gpio/export");
  766. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  767. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  768. system("echo 29 > /sys/class/gpio/export");
  769. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  770. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  771. system("echo 19 > /sys/class/gpio/export");
  772. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  773. system("echo 1 > /sys/class/gpio/gpio19/value");
  774. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  775. system("echo 20 > /sys/class/gpio/export");
  776. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  777. /***************************************************************/
  778. /*************** GPIO 1 ***************************************/
  779. /***************************************************************/
  780. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  781. system("echo 44 > /sys/class/gpio/export");
  782. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  783. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  784. system("echo 45 > /sys/class/gpio/export");
  785. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  786. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  787. system("echo 46 > /sys/class/gpio/export");
  788. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  789. system("echo 0 > /sys/class/gpio/gpio46/value");
  790. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  791. system("echo 47 > /sys/class/gpio/export");
  792. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  793. /***************************************************************/
  794. /*************** GPIO 2 ***************************************/
  795. /***************************************************************/
  796. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  797. system("echo 89 > /sys/class/gpio/export");
  798. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  799. system("echo 1 > /sys/class/gpio/gpio89/value");
  800. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  801. system("echo 87 > /sys/class/gpio/export");
  802. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  803. system("echo 0 > /sys/class/gpio/gpio87/value");
  804. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  805. system("echo 88 > /sys/class/gpio/export");
  806. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  807. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  808. system("echo 86 > /sys/class/gpio/export");
  809. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  810. /***************************************************************/
  811. /*************** GPIO 3 ***************************************/
  812. /***************************************************************/
  813. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  814. system("echo 111 > /sys/class/gpio/export");
  815. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  816. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  817. system("echo 114 > /sys/class/gpio/export");
  818. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  819. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  820. system("echo 113 > /sys/class/gpio/export");
  821. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  822. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  823. system("echo 110 > /sys/class/gpio/export");
  824. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  825. system("echo 0 > /sys/class/gpio/gpio110/value");
  826. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  827. system("echo 115 > /sys/class/gpio/export");
  828. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  829. system("echo 0 > /sys/class/gpio/gpio115/value");
  830. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  831. system("echo 112 > /sys/class/gpio/export");
  832. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  833. system("echo 1 > /sys/class/gpio/gpio112/value");
  834. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  835. system("echo 116 > /sys/class/gpio/export");
  836. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  837. system("echo 0 > /sys/class/gpio/gpio116/value");
  838. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  839. system("echo 103 > /sys/class/gpio/export");
  840. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  841. system("echo 0 > /sys/class/gpio/gpio103/value");
  842. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  843. system("echo 104 > /sys/class/gpio/export");
  844. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  845. system("echo 0 > /sys/class/gpio/gpio104/value");
  846. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  847. }
  848. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  849. {
  850. int fd, wrd;
  851. unsigned char *buf;
  852. unsigned int ChkSum, ChkSumOrg;
  853. if((buf = malloc(MtdBlockSize)) == NULL)
  854. {
  855. LOG_ERROR("malloc buffer NG, rebooting..\n");
  856. if(ShmStatusCodeData!=NULL)
  857. {
  858. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  859. }
  860. sleep(5);
  861. system("reboot -f");
  862. sleep(5);
  863. system("reboot -f");
  864. }
  865. memset(buf, 0, MtdBlockSize);
  866. //================================================
  867. // Load configuration from mtdblock10
  868. //================================================
  869. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  870. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  871. if(fd < 0)
  872. {
  873. free(buf);
  874. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  875. if(ShmStatusCodeData != NULL)
  876. {
  877. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  878. }
  879. sleep(5);
  880. system("reboot -f");
  881. sleep(5);
  882. system("reboot -f");
  883. }
  884. wrd = read(fd, buf, MtdBlockSize);
  885. close(fd);
  886. if(wrd < MtdBlockSize)
  887. {
  888. free(buf);
  889. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  890. if(ShmStatusCodeData != NULL)
  891. {
  892. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  893. }
  894. sleep(5);
  895. system("reboot -f");
  896. sleep(5);
  897. system("reboot -f");
  898. }
  899. ChkSum = 0;
  900. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  901. {
  902. ChkSum += buf[wrd];
  903. }
  904. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  905. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  906. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  907. //================================================
  908. // Load configuration from mtdblock11
  909. //================================================
  910. if(ChkSum != ChkSumOrg)
  911. {
  912. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  913. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  914. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  915. if(fd < 0)
  916. {
  917. free(buf);
  918. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  919. if(ShmStatusCodeData != NULL)
  920. {
  921. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  922. }
  923. sleep(5);
  924. system("reboot -f");
  925. sleep(5);
  926. system("reboot -f");
  927. }
  928. memset(buf, 0, MtdBlockSize);
  929. wrd = read(fd, buf, MtdBlockSize);
  930. close(fd);
  931. if(wrd < MtdBlockSize)
  932. {
  933. free(buf);
  934. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  935. if(ShmStatusCodeData != NULL)
  936. {
  937. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  938. }
  939. sleep(5);
  940. system("reboot -f");
  941. sleep(5);
  942. system("reboot -f");
  943. }
  944. ChkSum = 0;
  945. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  946. {
  947. ChkSum += buf[wrd];
  948. }
  949. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  950. //================================================
  951. // Load configuration from mtdblock12 (Factory default)
  952. //================================================
  953. if(ChkSum != ChkSumOrg)
  954. {
  955. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  956. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  957. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  958. if(fd < 0)
  959. {
  960. free(buf);
  961. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  962. if(ShmStatusCodeData != NULL)
  963. {
  964. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  965. }
  966. sleep(5);
  967. system("reboot -f");
  968. sleep(5);
  969. system("reboot -f");
  970. }
  971. memset(buf, 0, MtdBlockSize);
  972. wrd = read(fd, buf, MtdBlockSize);
  973. close(fd);
  974. if(wrd < MtdBlockSize)
  975. {
  976. free(buf);
  977. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  978. if(ShmStatusCodeData != NULL)
  979. {
  980. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  981. }
  982. sleep(5);
  983. system("reboot -f");
  984. sleep(5);
  985. system("reboot -f");
  986. }
  987. ChkSum = 0;
  988. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  989. {
  990. ChkSum += buf[wrd];
  991. }
  992. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  993. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  994. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  995. if(ChkSum != ChkSumOrg)
  996. {
  997. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  998. free(buf);
  999. system("cd /root;./FactoryConfig -m");
  1000. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1001. system("sync");
  1002. sleep(5);
  1003. system("reboot -f");
  1004. sleep(5);
  1005. system("reboot -f");
  1006. return FAIL;
  1007. }
  1008. }
  1009. }
  1010. //load OK
  1011. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  1012. free(buf);
  1013. system("rm -f /mnt/EvseConfig.bin");
  1014. // SysConfig in flash is empty (0xffffffff)
  1015. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1016. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1017. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1018. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1019. {
  1020. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1021. {
  1022. LOG_INFO("Model name over length.");
  1023. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1024. }
  1025. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1026. {
  1027. LOG_INFO("Model serial number over length.");
  1028. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1029. }
  1030. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1031. {
  1032. LOG_INFO("System id over length.");
  1033. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1034. }
  1035. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1036. {
  1037. LOG_INFO("Ethernet dhcp config is null.");
  1038. }
  1039. system("cd /root;./Module_FactoryConfig -m");
  1040. sleep(3);
  1041. system("/usr/bin/run_evse_restart.sh");
  1042. }
  1043. LOG_INFO("Load SysConfigData OK");
  1044. return PASS;
  1045. }
  1046. int isEthernetCablePlugIn(int ethernet)
  1047. {
  1048. int fd, PlugInStatus = 0;
  1049. char ch[2];
  1050. char buffer[64];
  1051. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1052. fd = open(buffer, O_RDONLY);
  1053. if(fd < 0)
  1054. {
  1055. return -1;
  1056. }
  1057. if(read(fd, ch, 1) < 0)
  1058. {
  1059. return -1;
  1060. }
  1061. PlugInStatus = atoi(ch);
  1062. close(fd);
  1063. return PlugInStatus;
  1064. }
  1065. int isRouteFail()
  1066. {
  1067. int result = YES;
  1068. FILE *fp;
  1069. char buf[512];
  1070. fp = popen("route -n", "r");
  1071. if(fp != NULL)
  1072. {
  1073. while(fgets(buf, sizeof(buf), fp) != NULL)
  1074. {
  1075. if(strstr(buf, "eth0") != NULL)
  1076. result = NO;
  1077. }
  1078. }
  1079. pclose(fp);
  1080. return result;
  1081. }
  1082. int isReachableInternet()
  1083. {
  1084. int result = FAIL;
  1085. FILE *fp;
  1086. char cmd[256];
  1087. char buf[512];
  1088. char tmp[512];
  1089. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1090. // {
  1091. // result = PASS;
  1092. // }
  1093. // else
  1094. {
  1095. strcpy(cmd, "ifconfig eth0");
  1096. fp = popen(cmd, "r");
  1097. if (fp != NULL)
  1098. {
  1099. while(fgets(buf, sizeof(buf), fp) != NULL)
  1100. {
  1101. if (strstr(buf, "inet addr:") > 0)
  1102. {
  1103. sscanf(buf, "%*s%s", tmp);
  1104. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1105. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1106. {
  1107. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1108. }
  1109. }
  1110. }
  1111. }
  1112. pclose(fp);
  1113. memset(buf, 0x00, sizeof(buf));
  1114. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1115. {
  1116. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1117. fp = popen(cmd, "r");
  1118. if(fp != NULL)
  1119. {
  1120. while(fgets(buf, sizeof(buf), fp) != NULL)
  1121. {
  1122. if(strstr(buf, "transmitted") > 0)
  1123. {
  1124. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1125. if(strstr(buf,"100%") != NULL)
  1126. {
  1127. }
  1128. else
  1129. {
  1130. result = PASS;
  1131. }
  1132. //DEBUG_INFO("%s",buf);
  1133. //DEBUG_INFO("%s\n",tmp);
  1134. }
  1135. }
  1136. }
  1137. pclose(fp);
  1138. }
  1139. }
  1140. return result;
  1141. }
  1142. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1143. {
  1144. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1145. }
  1146. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1147. {
  1148. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1149. }
  1150. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1151. {
  1152. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1153. }
  1154. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1155. {
  1156. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1157. }
  1158. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1159. {
  1160. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1161. }
  1162. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1163. {
  1164. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1165. }
  1166. void AnnounceEthernetStatus(void)
  1167. {
  1168. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1169. {
  1170. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1171. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1172. {
  1173. Set_Dispenser_MiscCommand(index, MISC_DISP_ETHERNET_STATUS);
  1174. }
  1175. }
  1176. }
  1177. void AnnounceWiFiStatus(void)
  1178. {
  1179. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1180. {
  1181. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1182. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1183. {
  1184. Set_Dispenser_MiscCommand(index, MISC_DISP_WIFI_STATUS);
  1185. }
  1186. }
  1187. }
  1188. void AnnounceTelcomModemStatus(void)
  1189. {
  1190. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1191. {
  1192. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1193. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1194. {
  1195. Set_Dispenser_MiscCommand(index, MISC_DISP_TELCOM_MODEM_STATUS);
  1196. }
  1197. }
  1198. }
  1199. void AnnounceBackendStatus(void)
  1200. {
  1201. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1202. {
  1203. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1204. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1205. {
  1206. Set_Dispenser_MiscCommand(index, MISC_DISP_BACKEND_STATUS);
  1207. }
  1208. }
  1209. }
  1210. void AnnounceBillingStatus(void)
  1211. {
  1212. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1213. {
  1214. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1215. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1216. {
  1217. Set_Dispenser_MiscCommand(index, MISC_DISP_BILLING_STATUS);
  1218. }
  1219. }
  1220. }
  1221. void AnnounceStopChargingButtonStatus(void)
  1222. {
  1223. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1224. {
  1225. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1226. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1227. {
  1228. Set_Dispenser_MiscCommand(index, MISC_DISP_STOP_BUTTON_STATUS);
  1229. }
  1230. }
  1231. }
  1232. void AnnounceAuthModeConfig(void)
  1233. {
  1234. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1235. {
  1236. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1237. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1238. {
  1239. Set_Dispenser_MiscCommand(index, MISC_DISP_AUTH_MODE_CONFIG);
  1240. }
  1241. }
  1242. }
  1243. void AnnounceEVCCIDConfig(void)
  1244. {
  1245. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1246. {
  1247. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1248. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1249. {
  1250. Set_Dispenser_MiscCommand(index, MISC_DISP_EVCCID_CONFIG);
  1251. }
  1252. }
  1253. }
  1254. void AnnounceLEDIntensity(void)
  1255. {
  1256. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1257. {
  1258. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1259. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1260. {
  1261. Set_Dispenser_MiscCommand(index, MISC_DISP_LED_INTENSITY);
  1262. }
  1263. }
  1264. }
  1265. void AnnounceCurrency(void)
  1266. {
  1267. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1268. {
  1269. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1270. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1271. {
  1272. Set_Dispenser_MiscCommand(index, MISC_DISP_CURRENCY);
  1273. }
  1274. }
  1275. }
  1276. void AnnounceRemoteStartNoID(void)
  1277. {
  1278. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  1279. {
  1280. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1281. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1282. {
  1283. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  1284. {
  1285. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  1286. if(chargingInfo[gun]->SystemStatus == S_IDLE)
  1287. {
  1288. ShmChargerInfo->ConnectorPageReq[gun].bits.RemoteStartNoID = true;
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. }
  1295. void AnnounceLineStatusCode(int gun, int status)
  1296. {
  1297. int dispenser = 0;
  1298. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].ParentDispensetIndex;
  1299. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  1300. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  1301. {
  1302. ShmChargerInfo->LineApi.StatusCode[gun] = status;
  1303. Set_Connector_MiscCommand(gun, MISC_CONN_LINE_STATUS);
  1304. }
  1305. }
  1306. void AnnounceStationInfo(void)
  1307. {
  1308. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1309. {
  1310. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1311. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1312. {
  1313. Set_Dispenser_MiscCommand(index, MISC_DISP_STATION_INFO);
  1314. }
  1315. }
  1316. }
  1317. void UpdateConfigAndIcon(void)
  1318. {
  1319. unsigned char status = _Connnection_Disable;
  1320. // update ethernet connection status
  1321. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1322. if(ShmChargerInfo->CabinetMiscValue.EthernetStatus != status)
  1323. {
  1324. ShmChargerInfo->CabinetMiscValue.EthernetStatus = status;
  1325. AnnounceEthernetStatus();
  1326. }
  1327. // update wifi connection status
  1328. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1329. {
  1330. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1331. }
  1332. else
  1333. {
  1334. status = _Connnection_Disable;
  1335. }
  1336. if(ShmChargerInfo->CabinetMiscValue.WiFiStatus != status)
  1337. {
  1338. ShmChargerInfo->CabinetMiscValue.WiFiStatus = status;
  1339. AnnounceWiFiStatus();
  1340. }
  1341. // update 4g connection status
  1342. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1343. {
  1344. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1345. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1346. }
  1347. else
  1348. {
  1349. status = _Connnection_Disable;
  1350. }
  1351. if(ShmChargerInfo->CabinetMiscValue.TelcomModemStatus != status)
  1352. {
  1353. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = status;
  1354. AnnounceTelcomModemStatus();
  1355. }
  1356. // update backend connection status
  1357. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1358. if(ShmChargerInfo->CabinetMiscValue.BackendStatus != status)
  1359. {
  1360. ShmChargerInfo->CabinetMiscValue.BackendStatus = status;
  1361. AnnounceBackendStatus();
  1362. }
  1363. // update billing status
  1364. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1365. if(ShmChargerInfo->CabinetMiscValue.BillingStatus != status)
  1366. {
  1367. ShmChargerInfo->CabinetMiscValue.BillingStatus = status;
  1368. AnnounceBillingStatus();
  1369. }
  1370. // update stop charging button
  1371. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1372. if(ShmChargerInfo->CabinetMiscValue.StopChargingButton != status)
  1373. {
  1374. ShmChargerInfo->CabinetMiscValue.StopChargingButton = status;
  1375. AnnounceStopChargingButtonStatus();
  1376. }
  1377. // update AuthMode config
  1378. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1379. if(ShmChargerInfo->CabinetMiscValue.AuthModeConfig != status)
  1380. {
  1381. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = status;
  1382. AnnounceAuthModeConfig();
  1383. AnnounceEVCCIDConfig();
  1384. }
  1385. // update EVCCID config
  1386. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1387. if(ShmChargerInfo->CabinetMiscValue.EVCCIDConfig != status)
  1388. {
  1389. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = status;
  1390. AnnounceEVCCIDConfig();
  1391. }
  1392. // update LED Intensity
  1393. status = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1394. if(ShmChargerInfo->CabinetMiscValue.LEDIntensity != status)
  1395. {
  1396. ShmChargerInfo->CabinetMiscValue.LEDIntensity = status;
  1397. AnnounceLEDIntensity();
  1398. }
  1399. // update currency
  1400. status = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  1401. if(ShmChargerInfo->CabinetMiscValue.Currency != status)
  1402. {
  1403. ShmChargerInfo->CabinetMiscValue.Currency = status;
  1404. AnnounceCurrency();
  1405. }
  1406. }
  1407. void Check4gWifiReset(void)
  1408. {
  1409. int timeDelay = 0;
  1410. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1411. {
  1412. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1413. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1414. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1415. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1416. {
  1417. if(!_NeedReset4gWifi)
  1418. {
  1419. GetClockTime(&_4gWifiReset_time);
  1420. LOG_INFO("Backend Enable But Internet Not Available!");
  1421. }
  1422. _NeedReset4gWifi = true;
  1423. }
  1424. else
  1425. {
  1426. _NeedReset4gWifi = false;
  1427. }
  1428. if(_NeedReset4gWifi)
  1429. {
  1430. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1431. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1432. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1433. {
  1434. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1435. usleep(100000);
  1436. // before reset 4g io, kill Module_4g & Module_Wifi task
  1437. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1438. {
  1439. system("killall Module_4g &");
  1440. }
  1441. usleep(100000);
  1442. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1443. {
  1444. system("killall Module_Wifi &");
  1445. }
  1446. LOG_INFO("Reset 4g_Wifi Module");
  1447. system("echo 1 > /sys/class/gpio/gpio104/value");
  1448. sleep(1);
  1449. system("echo 0 > /sys/class/gpio/gpio104/value");
  1450. _NeedReset4gWifi = false;
  1451. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1452. }
  1453. }
  1454. }
  1455. else
  1456. {
  1457. _NeedReset4gWifi = false;
  1458. }
  1459. }
  1460. void MasterCabinetDhcpClientConfig(void)
  1461. {
  1462. char tmpbuf[256];
  1463. //Run DHCP client if enabled
  1464. system("killall udhcpc");
  1465. system("rm -rf /etc/resolv.conf");
  1466. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1467. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1468. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1469. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1470. {
  1471. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1472. system(tmpbuf);
  1473. }
  1474. //Upgrade system id to /etc/hostname
  1475. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1476. system(tmpbuf);
  1477. }
  1478. void InitMasterCabinetEthernet(void)
  1479. {
  1480. char tmpbuf[256];
  1481. //Init Eth0 for internet
  1482. memset(tmpbuf,0,256);
  1483. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1484. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1485. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1486. system(tmpbuf);
  1487. memset(tmpbuf,0,256);
  1488. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1489. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1490. system(tmpbuf);
  1491. //system("ifconfig lo up");
  1492. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1493. //Init Eth1 for administrator tool
  1494. memset(tmpbuf,0,256);
  1495. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1496. system(tmpbuf);
  1497. }
  1498. void InitSlaveCabinetEthernet(void)
  1499. {
  1500. char tmpbuf[256];
  1501. system("/sbin/ifconfig eth0 down");
  1502. system("/sbin/ifconfig eth1 down");
  1503. system("killall udhcpc");
  1504. system("rm -rf /etc/resolv.conf");
  1505. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1506. //Init Eth0 for internet
  1507. memset(tmpbuf,0,256);
  1508. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1509. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1510. "255.255.255.0");
  1511. system(tmpbuf);
  1512. system("/sbin/route add default gw 192.168.100.1 eth0");
  1513. //Init Eth1 for internet
  1514. memset(tmpbuf,0,256);
  1515. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1516. system(tmpbuf);
  1517. }
  1518. void InitEthernet(void)
  1519. {
  1520. system("ifconfig eth0 down");
  1521. system("ifconfig eth1 down");
  1522. sleep(2);
  1523. char tmpbuf[256];
  1524. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1525. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1526. sleep(2);
  1527. InitMasterCabinetEthernet();
  1528. MasterCabinetDhcpClientConfig();
  1529. // DO360 DHCP Server
  1530. InitialDispenserDhcpServerConfig();
  1531. StartDispenserDhcpServer();
  1532. pid_t pid = fork();
  1533. uint8_t cnt_pingDNS_Fail;
  1534. if(pid == 0)
  1535. {
  1536. ShmChargerInfo->CabinetMiscValue.BackendStatus = _Connnection_Disconnected;
  1537. ShmChargerInfo->CabinetMiscValue.EthernetStatus = _Connnection_Disconnected;
  1538. ShmChargerInfo->CabinetMiscValue.WiFiStatus = _Connnection_Disable;
  1539. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = _Connnection_Disable;
  1540. LOG_INFO("Internet Fork PID = %d", getpid());
  1541. for(;;)
  1542. {
  1543. if(isEthernetCablePlugIn(0) == YES)
  1544. {
  1545. if(isRouteFail())
  1546. {
  1547. LOG_INFO("eth0 not in route, restart eht0.");
  1548. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1549. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1550. {
  1551. InitialDHCP();
  1552. }
  1553. else
  1554. {
  1555. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1556. memset(tmpbuf,0,256);
  1557. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1558. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1559. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1560. system(tmpbuf);
  1561. memset(tmpbuf,0,256);
  1562. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1563. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1564. system(tmpbuf);
  1565. }
  1566. }
  1567. }
  1568. if(isReachableInternet() == PASS)
  1569. {
  1570. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1571. {
  1572. LOG_INFO("Internet is Reachable");
  1573. }
  1574. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1575. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1576. cnt_pingDNS_Fail = 0;
  1577. }
  1578. else
  1579. {
  1580. if(++cnt_pingDNS_Fail > 3)
  1581. {
  1582. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1583. {
  1584. LOG_INFO("Internet is Disconnect Via Ethernet");
  1585. }
  1586. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1587. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1588. }
  1589. }
  1590. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1591. if(ethResult == YES)
  1592. {
  1593. system("/sbin/ifmetric eth0 0");
  1594. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1595. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1596. {
  1597. system("/sbin/ifmetric mlan0 1");
  1598. }
  1599. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1600. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1601. {
  1602. system("/sbin/ifmetric ppp0 2");
  1603. }
  1604. }
  1605. if (!ethResult &&
  1606. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1607. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1608. {
  1609. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1610. if (ethResult)
  1611. {
  1612. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1613. {
  1614. system("/sbin/ifmetric eth0 1");
  1615. system("/sbin/ifmetric mlan0 0");
  1616. }
  1617. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1618. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1619. {
  1620. system("/sbin/ifmetric ppp0 2");
  1621. }
  1622. }
  1623. }
  1624. if (!ethResult &&
  1625. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1626. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1627. {
  1628. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1629. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1630. if (ethResult)
  1631. {
  1632. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1633. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1634. {
  1635. system("/sbin/ifmetric mlan0 2");
  1636. }
  1637. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1638. {
  1639. system("/sbin/ifmetric eth0 1");
  1640. system("/sbin/ifmetric ppp0 0");
  1641. }
  1642. }
  1643. }
  1644. if(!Is_Ocpp_Task_Alive())
  1645. {
  1646. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1647. }
  1648. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1649. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == false ? true : false;
  1650. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1651. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1652. UpdateConfigAndIcon();
  1653. Check4gWifiReset();
  1654. sleep(5);
  1655. }
  1656. }
  1657. #ifdef SystemLogMessage
  1658. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1659. #endif
  1660. }
  1661. void GetMacAddress()
  1662. {
  1663. for (byte index = 0; index < 2; index++)
  1664. {
  1665. int fd;
  1666. struct ifreq ifr;
  1667. char tarEth[5];
  1668. char Mac[18];
  1669. sprintf(tarEth,"eth%d",index);
  1670. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1671. ifr.ifr_addr.sa_family = AF_INET;
  1672. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1673. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1674. close(fd);
  1675. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1676. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1677. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1678. if (index == 0)
  1679. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1680. else
  1681. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1682. }
  1683. }
  1684. void GetFirmwareVersion()
  1685. {
  1686. // Get CSU root file system version
  1687. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1688. sprintf(ShmChargerInfo->SysMisc.MainVersion, fwVersion);
  1689. sprintf(ShmChargerInfo->SysMisc.SubVersion, subVersion);
  1690. #if ENABLE_PCBA_TEST == 0
  1691. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1692. for(uint8_t idx=0;idx<3;idx++)
  1693. {
  1694. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1695. {
  1696. chademo++;
  1697. count++;
  1698. }
  1699. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1700. {
  1701. gb++;
  1702. count++;
  1703. }
  1704. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1705. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1706. {
  1707. ccs++;
  1708. count++;
  1709. }
  1710. }
  1711. if (count == 1)
  1712. {
  1713. if (chademo > 0)
  1714. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1715. else if (ccs > 0)
  1716. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1717. else if (gb > 0)
  1718. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1719. }
  1720. else
  1721. {
  1722. if (chademo > 0 && ccs > 0)
  1723. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1724. else if (chademo > 0 && gb > 0)
  1725. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1726. else if (ccs > 0 && gb > 0)
  1727. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1728. }
  1729. // Get network option from model name
  1730. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1731. {
  1732. case 'B':
  1733. case 'U':
  1734. //Blue tooth
  1735. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1736. break;
  1737. case 'W':
  1738. // WIFI
  1739. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1740. break;
  1741. case 'T':
  1742. // 3G/4G
  1743. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1744. break;
  1745. case 'D':
  1746. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1747. break;
  1748. default:
  1749. // LAN
  1750. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1751. break;
  1752. }
  1753. // Get rating power from model name
  1754. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1755. // Get IEC or UL
  1756. char _buf[3] = {0};
  1757. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1758. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1759. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1760. else if (strcmp(_buf, "WU") == EQUAL)
  1761. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1762. #endif
  1763. }
  1764. void InitialShareMemoryInfo()
  1765. {
  1766. FILE *fp;
  1767. char cmd[512];
  1768. char buf[512];
  1769. _UpgradeNeedReboot = FALSE;
  1770. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1771. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1772. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1773. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1774. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1775. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1776. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1777. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1778. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1779. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1780. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1781. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1782. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1783. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1784. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1785. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1786. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1787. sprintf(cmd, "/bin/uname -r");
  1788. fp = popen(cmd, "r");
  1789. if(fp == NULL)
  1790. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1791. else
  1792. {
  1793. while(fgets(buf, sizeof(buf), fp) != NULL)
  1794. {
  1795. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1796. }
  1797. }
  1798. // 雙槍 CCS + Chademo
  1799. GetFirmwareVersion();
  1800. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1801. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1802. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1803. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1804. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1805. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1806. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1807. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1808. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1809. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1810. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1811. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1812. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1813. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1814. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1815. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1816. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1817. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1818. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1819. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1820. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1821. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1822. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1823. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1824. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1825. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1826. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1827. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1828. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1829. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1830. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1831. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1832. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1833. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1834. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1835. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1836. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1837. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1838. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1839. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1840. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1841. ShmFanModuleData->SelfTest_Comp = NO;
  1842. ShmLedModuleData->SelfTest_Comp = NO;
  1843. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1844. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1845. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1846. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1847. ShmFanModuleData->TestFanSpeed = 0;
  1848. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1849. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1850. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1851. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1852. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  1853. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  1854. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  1855. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  1856. {
  1857. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  1858. }
  1859. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1860. {
  1861. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  1862. _useGunMeterValue[i] = false;
  1863. }
  1864. }
  1865. int Initialization(void)
  1866. {
  1867. // int pinOut[2] = { 116, 115 };
  1868. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  1869. {
  1870. chargingInfo[count]->RemoteStartFlag = NO;
  1871. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  1872. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  1873. SetOcppErrorCode(count, "NoError");
  1874. }
  1875. LOG_INFO("Initialization OK");
  1876. return PASS;
  1877. }
  1878. void InitialPsuGroupingAndLocation(void)
  1879. {
  1880. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1881. {
  1882. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  1883. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  1884. }
  1885. else
  1886. {
  1887. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  1888. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  1889. }
  1890. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1891. {
  1892. ShmPsuGrouping->GroupCollection[i].Index = i;
  1893. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  1894. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1895. {
  1896. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1897. {
  1898. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  1899. }
  1900. else
  1901. {
  1902. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun_6Parallel[i][0], GENERAL_GUN_QUANTITY);
  1903. }
  1904. }
  1905. else
  1906. {
  1907. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  1908. }
  1909. ShmChargerInfo->Control.GunAvailable[i] = NO;
  1910. }
  1911. }
  1912. void InitialCustomerCode(void)
  1913. {
  1914. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1915. {
  1916. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  1917. }
  1918. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  1919. {
  1920. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  1921. }
  1922. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  1923. {
  1924. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  1925. }
  1926. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  1927. {
  1928. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  1929. }
  1930. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'O' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'L')
  1931. {
  1932. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Shell;
  1933. }
  1934. else
  1935. {
  1936. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  1937. }
  1938. }
  1939. void InitialMaxDispenserConnectorQuantity(void)
  1940. {
  1941. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  1942. {
  1943. ShmChargerInfo->Control.MaxDispenser = 2;
  1944. ShmChargerInfo->Control.MaxConnector = 2;
  1945. }
  1946. else
  1947. {
  1948. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  1949. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  1950. }
  1951. }
  1952. void InitialAuthMode(void)
  1953. {
  1954. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1955. {
  1956. }
  1957. }
  1958. void InitialQRCodeMode(void)
  1959. {
  1960. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi ||
  1961. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  1962. {
  1963. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  1964. }
  1965. else
  1966. {
  1967. // customized
  1968. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  1969. {
  1970. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  1971. }
  1972. else
  1973. {
  1974. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  1975. }
  1976. }
  1977. }
  1978. void InitialPowerCabinetSetting(void)
  1979. {
  1980. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1981. {
  1982. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  1983. }
  1984. ShmChargerInfo->CabinetMiscValue.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1985. ShmChargerInfo->CabinetMiscValue.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1986. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1987. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1988. }
  1989. void Initial6ParallelSetting(void)
  1990. {
  1991. if(ShmSysConfigAndInfo->SysConfig.ModelName[11] == '3')
  1992. {
  1993. ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay = 1;
  1994. }
  1995. }
  1996. bool InitialChargerSetting(void)
  1997. {
  1998. bool result = true;
  1999. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O')
  2000. {
  2001. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  2002. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  2003. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2004. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  2005. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2006. }
  2007. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2008. {
  2009. if(!(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'P' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'K'))
  2010. {
  2011. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2012. }
  2013. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2014. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2015. }
  2016. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  2017. {
  2018. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2019. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2020. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2021. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2022. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  2023. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  2024. }
  2025. else
  2026. {
  2027. result = false;
  2028. }
  2029. InitialCustomerCode();
  2030. InitialMaxDispenserConnectorQuantity();
  2031. InitialAuthMode();
  2032. InitialQRCodeMode();
  2033. Initial6ParallelSetting();
  2034. InitialPsuGroupingAndLocation();
  2035. InitialPowerCabinetSetting();
  2036. if(!CheckConnectorTypeStatus())
  2037. {
  2038. result = false;
  2039. }
  2040. return result;
  2041. }
  2042. bool InitialSystemDefaultConfig()
  2043. {
  2044. bool result = true;
  2045. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2046. InitGPIO();
  2047. InitEthernet();
  2048. GetMacAddress();
  2049. return result;
  2050. }
  2051. void SelfTestRun(void)
  2052. {
  2053. LOG_INFO("Self test.");
  2054. system("/root/Module_ChargerSelfTest &");
  2055. }
  2056. void SpawnNormalService(void)
  2057. {
  2058. system("/root/Module_EventLogging &");
  2059. system("/root/Module_PrimaryComm &");
  2060. system("/root/Module_EvComm &");
  2061. //system("/root/Module_LcmControl &");
  2062. system("/root/Module_InternalComm &");
  2063. system("/root/Module_PsuComm &");
  2064. system("/root/Module_ProduceUtils &");
  2065. system("/root/Module_CabinetParallel &");
  2066. system("/root/Module_LedIndication &");
  2067. system("/root/Module_Authorize &");
  2068. }
  2069. void SpawnOcppService(void)
  2070. {
  2071. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2072. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2073. {
  2074. Run_Ocpp_Service();
  2075. }
  2076. }
  2077. void SpawnMaintainOcppService(void)
  2078. {
  2079. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2080. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2081. {
  2082. Run_MaintainOcpp_Service();
  2083. }
  2084. }
  2085. void SpawnInternetService(void)
  2086. {
  2087. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2088. {
  2089. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2090. {
  2091. system("/root/Module_4g &");
  2092. }
  2093. }
  2094. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2095. {
  2096. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2097. {
  2098. system("/root/Module_Wifi &");
  2099. }
  2100. }
  2101. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2102. {
  2103. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2104. {
  2105. system("/root/Module_4g &");
  2106. }
  2107. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2108. {
  2109. system("/root/Module_Wifi &");
  2110. }
  2111. }
  2112. }
  2113. int SpawnTask(void)
  2114. {
  2115. sleep(2);
  2116. SpawnNormalService();
  2117. SpawnInternetService();
  2118. return PASS;
  2119. }
  2120. int runFlashShellCmd(const char *cmd)
  2121. {
  2122. int result = FAIL;
  2123. char buf[256];
  2124. FILE *fp;
  2125. fp = popen(cmd, "r");
  2126. if(fp != NULL)
  2127. {
  2128. while(fgets(buf, sizeof(buf), fp) != NULL)
  2129. {
  2130. LOG_INFO("%s\n", buf);
  2131. }
  2132. result = PASS;
  2133. }
  2134. pclose(fp);
  2135. return result;
  2136. }
  2137. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2138. {
  2139. int result = PASS;
  2140. int fd, wrd;
  2141. unsigned int i, Chk;
  2142. unsigned char *ptr, *BufTmp;
  2143. Chk = 0;
  2144. ptr = (unsigned char *)UsrData;
  2145. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2146. {
  2147. memset(BufTmp, 0, MtdBlockSize);
  2148. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2149. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2150. {
  2151. Chk += *(BufTmp+i);
  2152. }
  2153. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2154. // Output configuration to file.
  2155. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2156. if(fd < 0)
  2157. {
  2158. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2159. free(BufTmp);
  2160. return 0;
  2161. }
  2162. wrd = write(fd, BufTmp, MtdBlockSize);
  2163. close(fd);
  2164. if(wrd < MtdBlockSize)
  2165. {
  2166. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2167. free(BufTmp);
  2168. return 0;
  2169. }
  2170. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2171. LOG_INFO("Erase /dev/mtd10.");
  2172. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2173. LOG_INFO("Write /dev/mtd10.");
  2174. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2175. LOG_INFO("Erase /dev/mtd11.");
  2176. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2177. LOG_INFO("Write /dev/mtd11.");
  2178. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2179. system("rm -f /mnt/EvseTempConfig.bin");
  2180. LOG_INFO("EvseTempConfig write to flash OK");
  2181. }
  2182. else
  2183. {
  2184. LOG_ERROR("alloc BlockSize NG");
  2185. result = FAIL;
  2186. }
  2187. if(BufTmp != NULL)
  2188. {
  2189. free(BufTmp);
  2190. }
  2191. return result;
  2192. }
  2193. //===============================================
  2194. // Common Detect Chk - Stop Charging ?
  2195. //===============================================
  2196. bool isEvBoardStopChargeFlag(byte gunIndex)
  2197. {
  2198. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2199. }
  2200. bool isNormalStopChargeFlag(byte gunIndex)
  2201. {
  2202. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2203. }
  2204. bool isResetStopChargeFlag(byte gunIndex)
  2205. {
  2206. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2207. }
  2208. bool isInvalidIdStopFlag(byte gunIndex)
  2209. {
  2210. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2211. }
  2212. bool isSessionTargetStopFlag(byte gunIndex)
  2213. {
  2214. return chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop;
  2215. }
  2216. void ReleaseAlarmCode(byte gunIndex)
  2217. {
  2218. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2219. {
  2220. SetOcppErrorCode(gunIndex, "NoError");
  2221. }
  2222. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2223. {
  2224. SetOcppVendorErrorCode(gunIndex, "");
  2225. }
  2226. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2227. {
  2228. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2229. }
  2230. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2231. {
  2232. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2233. }
  2234. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2235. {
  2236. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2237. }
  2238. }
  2239. //===============================================
  2240. // EmergencyStop and Charging Stop
  2241. //===============================================
  2242. void ChargingTerminalProcess(byte gunIndex)
  2243. {
  2244. setChargerMode(gunIndex, MODE_TERMINATING);
  2245. }
  2246. void ChargingAlarmProcess(byte gunIndex)
  2247. {
  2248. UpdateErrorCodeToOcpp(gunIndex);
  2249. setChargerMode(gunIndex, MODE_ALARM);
  2250. }
  2251. void AcChargingTerminalProcess()
  2252. {
  2253. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2254. }
  2255. void StopChargingProcessByString(byte level)
  2256. {
  2257. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2258. {
  2259. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2260. }
  2261. }
  2262. void ReleaseChargingProcessByString(byte level)
  2263. {
  2264. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2265. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2266. }
  2267. // 一般錯誤停止充電處理函式
  2268. void BoardErrOccurByString(byte index, char *code)
  2269. {
  2270. byte level = 1;
  2271. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2272. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2273. {
  2274. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2275. {
  2276. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2277. }
  2278. ChargingTerminalProcess(index);
  2279. }
  2280. StopChargingProcessByString(level);
  2281. }
  2282. // 急停狀況的停止充電處理函式
  2283. void EmcOccureByString(char *code)
  2284. {
  2285. byte level = 2;
  2286. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2287. {
  2288. return;
  2289. }
  2290. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2291. // 其錯誤等級為 2
  2292. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2293. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2294. strncmp(code, "042328", 6) == EQUAL)
  2295. {
  2296. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2297. {
  2298. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2299. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2300. {
  2301. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2302. {
  2303. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2304. }
  2305. InformOcppErrOccur(4);
  2306. ChargingAlarmProcess(gun);
  2307. }
  2308. }
  2309. StopChargingProcessByString(level);
  2310. }
  2311. }
  2312. void ReleaseEmsOccureByString(byte index, char *code)
  2313. {
  2314. bool isTrigger = false;
  2315. byte level = 2;
  2316. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2317. {
  2318. isTrigger = true;
  2319. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2320. }
  2321. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2322. {
  2323. isTrigger = true;
  2324. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2325. }
  2326. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2327. {
  2328. isTrigger = true;
  2329. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2330. }
  2331. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2332. {
  2333. isTrigger = true;
  2334. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2335. }
  2336. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2337. {
  2338. isTrigger = true;
  2339. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2340. }
  2341. if (isTrigger)
  2342. {
  2343. ReleaseChargingProcessByString(level);
  2344. }
  2345. }
  2346. //===============================================
  2347. // 確認硬體 (按鈕) 狀態
  2348. //===============================================
  2349. void ChkPrimaryStatus()
  2350. {
  2351. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2352. {
  2353. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2354. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2355. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2356. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2357. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2358. return;
  2359. }
  2360. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2361. {
  2362. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2363. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2364. EmcOccureByString("042251");
  2365. }
  2366. else
  2367. {
  2368. ReleaseEmsOccureByString(0, "042251");
  2369. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2370. }
  2371. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2372. {
  2373. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2374. //EmcOccureByString("042238");
  2375. }
  2376. else
  2377. {
  2378. //ReleaseEmsOccureByString(0, "042238");
  2379. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2380. }
  2381. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2382. {
  2383. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2384. }
  2385. else
  2386. {
  2387. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2388. }
  2389. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2390. {
  2391. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2392. EmcOccureByString("042252");
  2393. }
  2394. else
  2395. {
  2396. ReleaseEmsOccureByString(0, "042252");
  2397. }
  2398. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2399. {
  2400. EmcOccureByString("042328");
  2401. SetAcContactor(OFF);
  2402. }
  2403. }
  2404. struct timespec _AcContactReOpen_Time;
  2405. void ChkPsuStatus(void)
  2406. {
  2407. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2408. {
  2409. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2410. {
  2411. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2412. {
  2413. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2414. }
  2415. }
  2416. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2417. {
  2418. EmcOccureByString("042267");
  2419. }
  2420. }
  2421. else
  2422. {
  2423. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2424. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2425. {
  2426. bool allFaultStatus = true;
  2427. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2428. {
  2429. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2430. {
  2431. allFaultStatus = false;
  2432. }
  2433. }
  2434. if(allFaultStatus)
  2435. {
  2436. LOG_INFO("PSU Failure, Need Resume");
  2437. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2438. }
  2439. }
  2440. }
  2441. }
  2442. int AcContactorReOpenCount = 0;
  2443. void PsuFailureResume(void)
  2444. {
  2445. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2446. {
  2447. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2448. {
  2449. LOG_INFO("Ac Contactor Off By Psu Failure");
  2450. // update _AcContactReOpen_Time
  2451. GetClockTime(&_AcContactReOpen_Time);
  2452. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2453. AcContactorReOpenCount++;
  2454. }
  2455. else
  2456. {
  2457. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2458. {
  2459. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2460. {
  2461. ReleaseEmsOccureByString(0, "042267");
  2462. // update _AcContactReOpen_Time
  2463. GetClockTime(&_AcContactReOpen_Time);
  2464. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2465. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2466. LOG_INFO("Psu Failure Resume");
  2467. }
  2468. }
  2469. }
  2470. }
  2471. else
  2472. {
  2473. if(AcContactorReOpenCount > 0)
  2474. {
  2475. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2476. {
  2477. AcContactorReOpenCount = 0;
  2478. }
  2479. }
  2480. }
  2481. }
  2482. void SetAcContactor(unsigned char OnOff)
  2483. {
  2484. OnOff = OnOff ? ON : OFF;
  2485. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2486. {
  2487. if(OnOff)
  2488. {
  2489. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2490. }
  2491. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2492. }
  2493. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2494. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2495. {
  2496. SetParallelCabinetAcContactor(OnOff);
  2497. }
  2498. }
  2499. int GetStandbyTime(void)
  2500. {
  2501. int time = STANDBY_TIME;
  2502. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2503. {
  2504. time = FAST_STANDBY_TIME;
  2505. }
  2506. return time;
  2507. }
  2508. void StandbyCheck(void)
  2509. {
  2510. bool enable = IsStandbyEnable();
  2511. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2512. {
  2513. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2514. {
  2515. int time = GetStandbyTime();
  2516. #if CONTACTOR_AUTO_ON_OFF == 1
  2517. time = CONTACTOR_AUTO_ON_TIME;
  2518. #endif
  2519. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2520. {
  2521. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2522. {
  2523. SetAcContactor(OFF);
  2524. }
  2525. }
  2526. #if CONTACTOR_AUTO_ON_OFF == 1
  2527. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2528. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2529. {
  2530. SetAcContactor(ON);
  2531. }
  2532. #endif
  2533. }
  2534. else
  2535. {
  2536. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2537. GetClockTime(&_Standby_time);
  2538. }
  2539. }
  2540. else
  2541. {
  2542. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2543. {
  2544. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2545. }
  2546. }
  2547. }
  2548. void CheckDisconnectionStatusRecovery(void)
  2549. {
  2550. bool recovery = true;
  2551. // check all connector
  2552. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2553. {
  2554. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2555. {
  2556. recovery = false;
  2557. }
  2558. }
  2559. if(recovery)
  2560. {
  2561. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2562. }
  2563. }
  2564. //===============================================
  2565. // 確認各小板偵測的錯誤狀況
  2566. //===============================================
  2567. void CheckErrorOccurStatus(byte index)
  2568. {
  2569. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2570. {
  2571. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2572. }
  2573. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2574. {
  2575. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2576. {
  2577. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2578. }
  2579. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2580. {
  2581. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2582. }
  2583. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2584. }
  2585. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2586. {
  2587. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2588. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2589. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2590. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2591. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2592. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2593. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2594. chargingInfo[index]->SystemStatus == S_CHARGING)
  2595. {
  2596. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2597. {
  2598. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2599. }
  2600. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2601. {
  2602. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2603. }
  2604. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2605. }
  2606. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2607. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2608. {
  2609. setChargerMode(index, MODE_IDLE);
  2610. }
  2611. }
  2612. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2613. {
  2614. CheckDisconnectionStatusRecovery();
  2615. }
  2616. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2617. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2618. {
  2619. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2620. {
  2621. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2622. StopChargingProcessByString(2);
  2623. }
  2624. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2625. {
  2626. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2627. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2628. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2629. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2630. }
  2631. }
  2632. else
  2633. {
  2634. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2635. {
  2636. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2637. ReleaseChargingProcessByString(2);
  2638. }
  2639. }
  2640. // RB
  2641. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2642. {
  2643. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2644. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2645. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2646. {
  2647. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2648. {
  2649. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2650. StopChargingProcessByString(2);
  2651. }
  2652. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2653. {
  2654. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2655. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2656. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2657. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2658. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2659. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2660. }
  2661. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2662. {
  2663. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2664. }
  2665. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2666. }
  2667. else
  2668. {
  2669. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2670. {
  2671. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2672. ReleaseChargingProcessByString(2);
  2673. }
  2674. }
  2675. }
  2676. else
  2677. {
  2678. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2679. {
  2680. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2681. ReleaseChargingProcessByString(2);
  2682. }
  2683. }
  2684. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2685. {
  2686. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2687. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2688. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2689. {
  2690. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2691. {
  2692. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2693. StopChargingProcessByString(2);
  2694. }
  2695. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2696. {
  2697. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2698. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2699. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2700. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2701. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2702. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2703. }
  2704. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2705. {
  2706. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2707. }
  2708. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2709. }
  2710. else
  2711. {
  2712. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2713. {
  2714. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2715. ReleaseChargingProcessByString(2);
  2716. }
  2717. }
  2718. }
  2719. else
  2720. {
  2721. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  2722. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2723. {
  2724. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2725. {
  2726. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2727. StopChargingProcessByString(2);
  2728. }
  2729. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2730. {
  2731. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  2732. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  2733. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2734. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  2735. }
  2736. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2737. {
  2738. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2739. }
  2740. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2741. }
  2742. else
  2743. {
  2744. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2745. {
  2746. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2747. ReleaseChargingProcessByString(2);
  2748. }
  2749. }
  2750. }
  2751. }
  2752. int DiffTimebWithNowSec(struct timeb ST)
  2753. {
  2754. //return milli-second
  2755. struct timeb ET;
  2756. unsigned int StartTime,StopTime;
  2757. ftime(&ET);
  2758. StartTime=(unsigned int)ST.time;
  2759. StopTime=(unsigned int)ET.time;
  2760. return (StopTime-StartTime);
  2761. }
  2762. int CheckTimeOut(char *start)
  2763. {
  2764. int result = YES;
  2765. struct ParsingResult
  2766. {
  2767. int result;
  2768. int scanedElement;
  2769. int year;
  2770. int month;
  2771. int mday;
  2772. int hour;
  2773. int min;
  2774. int sec;
  2775. int tz_hour;
  2776. int tz_min;
  2777. float minSec;
  2778. }parsingResult;
  2779. struct tm tmStart;
  2780. struct timeb tbStart;
  2781. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  2782. if(strstr((char*)start, ".") != NULL)
  2783. {
  2784. // Original data with mini second
  2785. if(strstr((char*)start, "Z") != NULL)
  2786. {
  2787. // Original data with Z
  2788. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  2789. &parsingResult.year,
  2790. &parsingResult.month,
  2791. &parsingResult.mday,
  2792. &parsingResult.hour,
  2793. &parsingResult.min,
  2794. &parsingResult.sec,
  2795. &parsingResult.minSec);
  2796. }
  2797. else
  2798. {
  2799. // Original data without Z
  2800. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  2801. &parsingResult.year,
  2802. &parsingResult.month,
  2803. &parsingResult.mday,
  2804. &parsingResult.hour,
  2805. &parsingResult.min,
  2806. &parsingResult.sec,
  2807. &parsingResult.minSec,
  2808. &parsingResult.tz_hour,
  2809. &parsingResult.tz_min);
  2810. }
  2811. }
  2812. else
  2813. {
  2814. // Original data without mini second
  2815. if(strstr((char*)start, "Z") != NULL)
  2816. {
  2817. // Original data with Z
  2818. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  2819. &parsingResult.year,
  2820. &parsingResult.month,
  2821. &parsingResult.mday,
  2822. &parsingResult.hour,
  2823. &parsingResult.min,
  2824. &parsingResult.sec);
  2825. }
  2826. else
  2827. {
  2828. // Original data without Z
  2829. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  2830. &parsingResult.year,
  2831. &parsingResult.month,
  2832. &parsingResult.mday,
  2833. &parsingResult.hour,
  2834. &parsingResult.min,
  2835. &parsingResult.sec,
  2836. &parsingResult.tz_hour,
  2837. &parsingResult.tz_min);
  2838. }
  2839. }
  2840. if(parsingResult.scanedElement >= 6)
  2841. {
  2842. tmStart.tm_year = parsingResult.year - 1900;
  2843. tmStart.tm_mon = parsingResult.month - 1;
  2844. tmStart.tm_mday = parsingResult.mday;
  2845. tmStart.tm_hour = parsingResult.hour;
  2846. tmStart.tm_min = parsingResult.min;
  2847. tmStart.tm_sec = parsingResult.sec;
  2848. tmStart.tm_gmtoff = 0;
  2849. tbStart.time = mktime(&tmStart);
  2850. tbStart.timezone = 0;
  2851. tbStart.millitm = 0;
  2852. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  2853. if(DiffTimebWithNowSec(tbStart) <= 0)
  2854. result = NO;
  2855. else
  2856. result = YES;
  2857. }
  2858. else
  2859. {
  2860. LOG_INFO("Start date parsing error");
  2861. }
  2862. return result;
  2863. }
  2864. //===============================================
  2865. // Ocpp_Service
  2866. //===============================================
  2867. void Run_Ocpp_Service(void)
  2868. {
  2869. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2870. {
  2871. system("/root/OcppBackend &");
  2872. return;
  2873. }
  2874. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2875. {
  2876. system("/root/OcppBackend20 &");
  2877. return;
  2878. }
  2879. }
  2880. bool Is_Ocpp_Task_Alive(void)
  2881. {
  2882. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2883. {
  2884. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2885. {
  2886. return false;
  2887. }
  2888. return true;
  2889. }
  2890. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2891. {
  2892. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  2893. {
  2894. return false;
  2895. }
  2896. return true;
  2897. }
  2898. return false;
  2899. }
  2900. void Kill_Ocpp_Service(void)
  2901. {
  2902. system("killall OcppBackend");
  2903. system("killall OcppBackend20");
  2904. }
  2905. //===============================================
  2906. // Maintain Ocpp_Service
  2907. //===============================================
  2908. void Run_MaintainOcpp_Service(void)
  2909. {
  2910. system("/root/OcppBackendPH &");
  2911. }
  2912. bool Is_MaintainOcpp_Task_Alive(void)
  2913. {
  2914. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  2915. {
  2916. return false;
  2917. }
  2918. return true;
  2919. }
  2920. void Kill_MaintainOcpp_Service(void)
  2921. {
  2922. system("killall OcppBackendPH");
  2923. }
  2924. //===============================================
  2925. // Ocpp OcppConnStatus
  2926. //===============================================
  2927. bool Get_Ocpp_ConnectionStatus(void)
  2928. {
  2929. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2930. {
  2931. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  2932. }
  2933. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2934. {
  2935. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  2936. }
  2937. return false;
  2938. }
  2939. //===============================================
  2940. // Ocpp procDogTime
  2941. //===============================================
  2942. unsigned int Get_Ocpp_procDogTime(void)
  2943. {
  2944. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2945. {
  2946. return ShmOCPP16Data->procDogTime;
  2947. }
  2948. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2949. {
  2950. return ShmOCPP20Data->procDogTime;
  2951. }
  2952. return 0;
  2953. }
  2954. void Clean_Ocpp_procDogTime(void)
  2955. {
  2956. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2957. {
  2958. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2959. return;
  2960. }
  2961. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2962. {
  2963. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2964. return;
  2965. }
  2966. }
  2967. //===============================================
  2968. // Ocpp Boot Notification
  2969. //===============================================
  2970. bool Is_Ocpp_BootNotification(void)
  2971. {
  2972. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2973. {
  2974. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  2975. }
  2976. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2977. {
  2978. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  2979. }
  2980. return false;
  2981. }
  2982. void Clean_Ocpp_BootNotification(void)
  2983. {
  2984. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2985. {
  2986. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  2987. return;
  2988. }
  2989. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2990. {
  2991. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  2992. return;
  2993. }
  2994. }
  2995. //===============================================
  2996. // Ocpp Rest
  2997. //===============================================
  2998. bool Is_Ocpp_ResetReq(void)
  2999. {
  3000. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3001. {
  3002. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  3003. }
  3004. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3005. {
  3006. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  3007. }
  3008. return false;
  3009. }
  3010. bool Chk_Ocpp_Reset_Type(char *type)
  3011. {
  3012. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3013. {
  3014. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  3015. }
  3016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3017. {
  3018. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  3019. }
  3020. return false;
  3021. }
  3022. void Clean_Ocpp_ResetReq(void)
  3023. {
  3024. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3025. {
  3026. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3027. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  3028. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  3029. return;
  3030. }
  3031. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3032. {
  3033. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  3034. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  3035. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  3036. return;
  3037. }
  3038. }
  3039. //===============================================
  3040. // Ocpp AuthorizeReq
  3041. //===============================================
  3042. void Set_Ocpp_AuthorizeReq(void)
  3043. {
  3044. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3045. {
  3046. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3047. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3048. return;
  3049. }
  3050. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3051. {
  3052. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  3053. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  3054. return;
  3055. }
  3056. }
  3057. bool Is_Ocpp_AuthorizeConf(void)
  3058. {
  3059. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3060. {
  3061. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3062. }
  3063. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3064. {
  3065. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3066. }
  3067. return false;
  3068. }
  3069. bool Is_Ocpp_Authorize_Status(char *status)
  3070. {
  3071. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3072. {
  3073. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3074. }
  3075. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3076. {
  3077. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3078. }
  3079. return false;
  3080. }
  3081. //===============================================
  3082. // Ocpp UpdateFirmwareReq
  3083. //===============================================
  3084. bool Is_Ocpp_UpdateFirmwareReq(void)
  3085. {
  3086. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3087. {
  3088. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3089. }
  3090. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3091. {
  3092. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3093. }
  3094. return false;
  3095. }
  3096. void Clean_Ocpp_UpdateFirmwareReq(void)
  3097. {
  3098. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3099. {
  3100. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3101. return;
  3102. }
  3103. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3104. {
  3105. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3106. return;
  3107. }
  3108. }
  3109. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3110. {
  3111. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3112. {
  3113. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3114. }
  3115. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3116. {
  3117. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3118. }
  3119. return false;
  3120. }
  3121. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3122. {
  3123. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3124. {
  3125. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3126. return;
  3127. }
  3128. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3129. {
  3130. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3131. return;
  3132. }
  3133. }
  3134. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3135. {
  3136. Set_Ocpp_UpdateFirmwareStatus("");
  3137. }
  3138. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3139. {
  3140. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3141. {
  3142. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3143. return;
  3144. }
  3145. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3146. {
  3147. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3148. return;
  3149. }
  3150. }
  3151. //===============================================
  3152. // Ocpp StopTransactionOnInvalidId
  3153. //===============================================
  3154. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3155. {
  3156. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3157. {
  3158. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3159. }
  3160. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3161. {
  3162. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3163. }
  3164. return false;
  3165. }
  3166. //===============================================
  3167. // Ocpp AuthorizeRemoteTxRequests
  3168. //===============================================
  3169. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3170. {
  3171. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3172. {
  3173. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3174. }
  3175. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3176. {
  3177. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3178. }
  3179. return false;
  3180. }
  3181. //===============================================
  3182. // Ocpp ConnectionTimeOut
  3183. //===============================================
  3184. int Get_Ocpp_ConnectionTimeOut(void)
  3185. {
  3186. int timeout = 0;
  3187. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3188. {
  3189. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3190. {
  3191. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3192. }
  3193. }
  3194. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3195. {
  3196. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3197. {
  3198. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3199. }
  3200. }
  3201. return timeout > 0 ? timeout : 0;
  3202. }
  3203. //===============================================
  3204. // Ocpp DefaultPrice
  3205. //===============================================
  3206. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3207. {
  3208. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3209. {
  3210. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3211. return;
  3212. }
  3213. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3214. {
  3215. strcpy(defaultPrice, "");
  3216. return;
  3217. }
  3218. strcpy(defaultPrice, "");
  3219. }
  3220. //===============================================
  3221. // Ocpp UserPrice
  3222. //===============================================
  3223. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3224. {
  3225. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3226. {
  3227. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3228. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3229. return;
  3230. }
  3231. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3232. {
  3233. strcpy(userId, "");
  3234. strcpy(userPrice, "");
  3235. return;
  3236. }
  3237. strcpy(userId, "");
  3238. strcpy(userPrice, "");
  3239. }
  3240. void Clean_Ocpp_UserPrice(void)
  3241. {
  3242. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3243. {
  3244. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3245. return;
  3246. }
  3247. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3248. {
  3249. return;
  3250. }
  3251. }
  3252. //===============================================
  3253. // Ocpp RunningCost
  3254. //===============================================
  3255. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3256. {
  3257. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3258. {
  3259. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3260. return;
  3261. }
  3262. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3263. {
  3264. strcpy(cost, (char *)"");
  3265. return;
  3266. }
  3267. }
  3268. void Clean_Ocpp_RunningCost(int gun_index)
  3269. {
  3270. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3271. {
  3272. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3273. return;
  3274. }
  3275. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3276. {
  3277. return;
  3278. }
  3279. }
  3280. //===============================================
  3281. // Ocpp FinalCost
  3282. //===============================================
  3283. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3284. {
  3285. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3286. {
  3287. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3288. return;
  3289. }
  3290. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3291. {
  3292. strcpy(cost, (char *)"");
  3293. return;
  3294. }
  3295. }
  3296. void Clean_Ocpp_FinalCost(int gun_index)
  3297. {
  3298. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3299. {
  3300. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3301. return;
  3302. }
  3303. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3304. {
  3305. return;
  3306. }
  3307. }
  3308. int Get_Ocpp_FinalCostTxId(int gun_index)
  3309. {
  3310. int txId = 0;
  3311. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3312. {
  3313. txId = ShmOCPP16Data->Cost.FinalCost[gun_index].txId;
  3314. }
  3315. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3316. {
  3317. }
  3318. return txId;
  3319. }
  3320. //===============================================
  3321. // Ocpp TimeOffset
  3322. //===============================================
  3323. void Get_Ocpp_TimeOffset(char *timeOffset)
  3324. {
  3325. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3326. {
  3327. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3328. return;
  3329. }
  3330. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3331. {
  3332. strcpy(timeOffset, "");
  3333. return;
  3334. }
  3335. strcpy(timeOffset, "");
  3336. }
  3337. //===============================================
  3338. // Ocpp PreAuthAmount
  3339. //===============================================
  3340. void Get_Ocpp_PreAuthAmount(char *amount)
  3341. {
  3342. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3343. {
  3344. strcpy(amount, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  3345. return;
  3346. }
  3347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3348. {
  3349. strcpy(amount, "");
  3350. return;
  3351. }
  3352. strcpy(amount, "");
  3353. }
  3354. //===============================================
  3355. // Ocpp PowerSharingServerIP
  3356. //===============================================
  3357. void Get_Ocpp_PowerSharingServerIP(char *ip)
  3358. {
  3359. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3360. {
  3361. strcpy(ip, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PowerSharingServerIP].ItemData);
  3362. return;
  3363. }
  3364. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3365. {
  3366. strcpy(ip, "");
  3367. return;
  3368. }
  3369. strcpy(ip, "");
  3370. }
  3371. //===============================================
  3372. // Ocpp Refunding
  3373. //===============================================
  3374. void Get_Ocpp_Refunding(char *Refunding)
  3375. {
  3376. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3377. {
  3378. return;
  3379. }
  3380. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3381. {
  3382. return;
  3383. }
  3384. }
  3385. void Clean_Ocpp_Refunding(void)
  3386. {
  3387. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3388. {
  3389. return;
  3390. }
  3391. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3392. {
  3393. return;
  3394. }
  3395. }
  3396. //===============================================
  3397. // Ocpp LcdOveride
  3398. //===============================================
  3399. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3400. {
  3401. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3402. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3403. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3404. }
  3405. void Clean_Ocpp_LcdOverideReq(void)
  3406. {
  3407. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3408. }
  3409. //===============================================
  3410. // Ocpp StationInfo
  3411. //===============================================
  3412. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3413. {
  3414. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3415. {
  3416. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3417. }
  3418. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3419. {
  3420. }
  3421. }
  3422. bool Is_Ocpp_ChargerStationReq(void)
  3423. {
  3424. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3425. {
  3426. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3427. }
  3428. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3429. {
  3430. return false;
  3431. }
  3432. return false;
  3433. }
  3434. void Clean_Ocpp_ChargerStationReq(void)
  3435. {
  3436. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3437. {
  3438. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3439. }
  3440. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3441. {
  3442. }
  3443. }
  3444. //===============================================
  3445. // Ocpp WeatherInfo
  3446. //===============================================
  3447. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3448. {
  3449. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3450. {
  3451. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3452. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3453. }
  3454. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3455. {
  3456. }
  3457. }
  3458. bool Is_Ocpp_WeatherReq(void)
  3459. {
  3460. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3461. {
  3462. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3463. }
  3464. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3465. {
  3466. return false;
  3467. }
  3468. return false;
  3469. }
  3470. void Clean_Ocpp_WeatherReq(void)
  3471. {
  3472. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3473. {
  3474. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3475. }
  3476. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3477. {
  3478. }
  3479. }
  3480. //===============================================
  3481. // Ocpp DeductInfo
  3482. //===============================================
  3483. void Set_Ocpp_DeductTransaction(int transaction)
  3484. {
  3485. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3486. {
  3487. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3488. }
  3489. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3490. {
  3491. }
  3492. }
  3493. void Set_Ocpp_DeductCreditNo(char *userId)
  3494. {
  3495. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3496. {
  3497. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3498. }
  3499. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3500. {
  3501. }
  3502. }
  3503. void Set_Ocpp_DeductVemData(char *vemData)
  3504. {
  3505. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3506. {
  3507. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3508. }
  3509. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3510. {
  3511. }
  3512. }
  3513. void Set_Ocpp_DeductAmount(double value)
  3514. {
  3515. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3516. {
  3517. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3518. }
  3519. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3520. {
  3521. }
  3522. }
  3523. void Set_Ocpp_DeductResult(unsigned char result)
  3524. {
  3525. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3526. {
  3527. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3528. }
  3529. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3530. {
  3531. }
  3532. }
  3533. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3534. {
  3535. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3536. {
  3537. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3538. }
  3539. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3540. {
  3541. }
  3542. }
  3543. bool Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3544. {
  3545. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3546. {
  3547. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3548. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, sizeof(deductInfo->creditNo));
  3549. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, deductInfo->VemData, sizeof(deductInfo->VemData));
  3550. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3551. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber, sizeof(deductInfo->ApprovalNumber));
  3552. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3553. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3554. return true;
  3555. }
  3556. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3557. {
  3558. }
  3559. return false;
  3560. }
  3561. bool Is_Ocpp_ReportCreditDeductReq(void)
  3562. {
  3563. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3564. {
  3565. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3566. }
  3567. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3568. {
  3569. return false;
  3570. }
  3571. return false;
  3572. }
  3573. void Set_Ocpp_ReportCreditDeductReq(void)
  3574. {
  3575. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3576. {
  3577. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3578. }
  3579. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3580. {
  3581. }
  3582. }
  3583. //===============================================
  3584. // Ocpp Tcci SerialNo
  3585. //===============================================
  3586. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3587. {
  3588. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3589. {
  3590. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3591. }
  3592. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3593. {
  3594. }
  3595. }
  3596. //===============================================
  3597. // Ocpp Tcci ReaderStatus
  3598. //===============================================
  3599. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus)
  3600. {
  3601. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3602. {
  3603. if(ReaderStatus->DeductResult == _Deduct_Cancel || ReaderStatus->DeductResult == _Deduct_PreAuth || ReaderStatus->DeductResult == _Deduct_Pass)
  3604. {
  3605. switch(ReaderStatus->DeductResult)
  3606. {
  3607. case _Deduct_Pass:
  3608. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 1;
  3609. break;
  3610. case _Deduct_Cancel:
  3611. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 2;
  3612. break;
  3613. case _Deduct_PreAuth:
  3614. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 0;
  3615. break;
  3616. }
  3617. memcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, ReaderStatus->creditNo, sizeof(ReaderStatus->creditNo));
  3618. memcpy((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], ReaderStatus->VemData, sizeof(ReaderStatus->VemData));
  3619. return true;
  3620. }
  3621. }
  3622. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3623. {
  3624. }
  3625. return false;
  3626. }
  3627. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3628. {
  3629. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3630. {
  3631. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3632. }
  3633. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3634. {
  3635. }
  3636. }
  3637. bool Is_Ocpp_TcciReaderStatusReq(int gun_index)
  3638. {
  3639. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3640. {
  3641. return ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq ? true : false;
  3642. }
  3643. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3644. {
  3645. return false;
  3646. }
  3647. return false;
  3648. }
  3649. //===============================================
  3650. // Ocpp Start Transaction
  3651. //===============================================
  3652. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  3653. {
  3654. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3655. {
  3656. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  3657. }
  3658. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3659. {
  3660. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  3661. }
  3662. // MaintainServer
  3663. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  3664. {
  3665. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  3666. }
  3667. }
  3668. void Set_Ocpp_StartTransactionReq(int gun_index)
  3669. {
  3670. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3671. {
  3672. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  3673. }
  3674. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3675. {
  3676. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  3677. }
  3678. // MaintainServer
  3679. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  3680. {
  3681. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  3682. }
  3683. }
  3684. bool Is_Ocpp_StartTransactionConf(int gun_index)
  3685. {
  3686. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3687. {
  3688. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  3689. }
  3690. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3691. {
  3692. return ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES ? true : false;
  3693. }
  3694. return false;
  3695. }
  3696. void Clean_Ocpp_StartTransactionConf(int gun_index)
  3697. {
  3698. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3699. {
  3700. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  3701. }
  3702. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3703. {
  3704. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  3705. }
  3706. // MaintainServer
  3707. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  3708. {
  3709. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  3710. }
  3711. }
  3712. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  3713. {
  3714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3715. {
  3716. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3717. }
  3718. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3719. {
  3720. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3721. }
  3722. return false;
  3723. }
  3724. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  3725. {
  3726. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  3727. }
  3728. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  3729. {
  3730. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3731. {
  3732. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  3733. }
  3734. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3735. {
  3736. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  3737. }
  3738. }
  3739. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  3740. {
  3741. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3742. {
  3743. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  3744. return;
  3745. }
  3746. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3747. {
  3748. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  3749. return;
  3750. }
  3751. sprintf(transactionId, "%d", 0);
  3752. }
  3753. bool Is_Invalid_StartTransactionId(int gun_index)
  3754. {
  3755. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  3756. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  3757. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  3758. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  3759. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  3760. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  3761. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  3762. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  3763. {
  3764. return true;
  3765. }
  3766. return false;
  3767. }
  3768. //===============================================
  3769. // Ocpp Stop Transaction
  3770. //===============================================
  3771. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  3772. {
  3773. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3774. {
  3775. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  3776. return;
  3777. }
  3778. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3779. {
  3780. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  3781. return;
  3782. }
  3783. }
  3784. void Set_Ocpp_StopTransactionReq(int gun_index)
  3785. {
  3786. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3787. {
  3788. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  3789. }
  3790. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3791. {
  3792. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  3793. }
  3794. // MaintainServer
  3795. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  3796. {
  3797. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  3798. }
  3799. }
  3800. //===============================================
  3801. // Ocpp Check Backup Error Code
  3802. //===============================================
  3803. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  3804. {
  3805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3806. {
  3807. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  3808. {
  3809. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  3810. {
  3811. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  3812. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  3813. }
  3814. }
  3815. }
  3816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3817. {
  3818. }
  3819. }
  3820. //===============================================
  3821. // Ocpp Reserve Now
  3822. //===============================================
  3823. bool Is_Ocpp_ReserveNowReq(int gun_index)
  3824. {
  3825. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3826. {
  3827. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  3828. }
  3829. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3830. {
  3831. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  3832. }
  3833. return false;
  3834. }
  3835. void Clean_Ocpp_ReserveNowReq(int gun_index)
  3836. {
  3837. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3838. {
  3839. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  3840. return;
  3841. }
  3842. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3843. {
  3844. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  3845. return;
  3846. }
  3847. }
  3848. int Get_Ocpp_ReserveNowId(int gun_index)
  3849. {
  3850. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3851. {
  3852. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3853. }
  3854. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3855. {
  3856. return ShmOCPP20Data->ReserveNow[gun_index].id;
  3857. }
  3858. return 0;
  3859. }
  3860. void Set_Ocpp_ReserveNowConf(int gun_index)
  3861. {
  3862. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3863. {
  3864. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  3865. return;
  3866. }
  3867. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3868. {
  3869. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  3870. return;
  3871. }
  3872. }
  3873. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  3874. {
  3875. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3876. {
  3877. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  3878. return;
  3879. }
  3880. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3881. {
  3882. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  3883. return;
  3884. }
  3885. strcpy(date, "");
  3886. return;
  3887. }
  3888. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  3889. {
  3890. bool _expired = true;
  3891. char _expiryDate[128];
  3892. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  3893. _expired = CheckTimeOut(_expiryDate);
  3894. return _expired;
  3895. }
  3896. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  3897. {
  3898. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3899. {
  3900. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  3901. return;
  3902. }
  3903. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3904. {
  3905. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  3906. return;
  3907. }
  3908. strcpy(idTag, "");
  3909. return;
  3910. }
  3911. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  3912. {
  3913. char ReserveIdTag[32];
  3914. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  3915. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  3916. }
  3917. //===============================================
  3918. // Ocpp Cancel Reservation
  3919. //===============================================
  3920. bool Is_Ocpp_CancelReservationReq(int gun_index)
  3921. {
  3922. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3923. {
  3924. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  3925. }
  3926. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3927. {
  3928. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  3929. }
  3930. return false;
  3931. }
  3932. void Clean_Ocpp_CancelReservationReq(int gun_index)
  3933. {
  3934. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3935. {
  3936. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  3937. return;
  3938. }
  3939. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3940. {
  3941. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  3942. return;
  3943. }
  3944. }
  3945. void Set_Ocpp_CancelReservationConf(int gun_index)
  3946. {
  3947. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3948. {
  3949. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  3950. return;
  3951. }
  3952. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3953. {
  3954. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  3955. return;
  3956. }
  3957. }
  3958. //===============================================
  3959. // Ocpp Change Availability
  3960. //===============================================
  3961. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  3962. {
  3963. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3964. {
  3965. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  3966. }
  3967. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3968. {
  3969. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  3970. }
  3971. return false;
  3972. }
  3973. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  3974. {
  3975. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3976. {
  3977. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  3978. return;
  3979. }
  3980. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3981. {
  3982. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  3983. return;
  3984. }
  3985. }
  3986. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  3987. {
  3988. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3989. {
  3990. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  3991. }
  3992. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3993. {
  3994. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  3995. }
  3996. return false;
  3997. }
  3998. //===============================================
  3999. // Ocpp Unlock Connector
  4000. //===============================================
  4001. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4002. {
  4003. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4004. {
  4005. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4006. }
  4007. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4008. {
  4009. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4010. }
  4011. return false;
  4012. }
  4013. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4014. {
  4015. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4016. {
  4017. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4018. return;
  4019. }
  4020. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4021. {
  4022. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4023. return;
  4024. }
  4025. }
  4026. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4027. {
  4028. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4029. {
  4030. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4031. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4032. return;
  4033. }
  4034. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4035. {
  4036. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4037. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4038. return;
  4039. }
  4040. }
  4041. //===============================================
  4042. // Ocpp Remote Start
  4043. //===============================================
  4044. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4045. {
  4046. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4047. {
  4048. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4049. }
  4050. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4051. {
  4052. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4053. }
  4054. return false;
  4055. }
  4056. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4057. {
  4058. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4059. {
  4060. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4061. return;
  4062. }
  4063. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4064. {
  4065. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4066. return;
  4067. }
  4068. }
  4069. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4070. {
  4071. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4072. {
  4073. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4074. return;
  4075. }
  4076. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4077. {
  4078. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4079. return;
  4080. }
  4081. strcpy(idTag, "");
  4082. }
  4083. //===============================================
  4084. // Ocpp Remote Stop
  4085. //===============================================
  4086. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4087. {
  4088. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4089. {
  4090. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4091. }
  4092. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4093. {
  4094. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4095. }
  4096. return false;
  4097. }
  4098. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4099. {
  4100. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4101. {
  4102. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4103. return;
  4104. }
  4105. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4106. {
  4107. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4108. return;
  4109. }
  4110. }
  4111. //===============================================
  4112. // Ocpp StopReason
  4113. //===============================================
  4114. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4115. {
  4116. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4117. {
  4118. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4119. {
  4120. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4121. }
  4122. }
  4123. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4124. {
  4125. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4126. {
  4127. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4128. }
  4129. }
  4130. // Maintain server
  4131. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4132. {
  4133. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4134. }
  4135. }
  4136. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4137. {
  4138. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4139. {
  4140. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4141. return;
  4142. }
  4143. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4144. {
  4145. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4146. return;
  4147. }
  4148. strcpy(reason, "");
  4149. }
  4150. void Clean_Ocpp_StopReason(int gun_index)
  4151. {
  4152. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4153. {
  4154. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4155. }
  4156. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4157. {
  4158. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4159. }
  4160. // Maintain server
  4161. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4162. {
  4163. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4164. }
  4165. }
  4166. //===============================================
  4167. // Ocpp StatusNotification ErrorCode
  4168. //===============================================
  4169. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4170. {
  4171. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4172. {
  4173. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4174. return;
  4175. }
  4176. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4177. {
  4178. return;
  4179. }
  4180. }
  4181. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4182. {
  4183. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4184. {
  4185. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4186. }
  4187. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4188. {
  4189. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4190. }
  4191. return false;
  4192. }
  4193. //===============================================
  4194. // Ocpp StatusNotification VendorErrorCode
  4195. //===============================================
  4196. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4197. {
  4198. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4199. {
  4200. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4201. return;
  4202. }
  4203. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4204. {
  4205. return;
  4206. }
  4207. }
  4208. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4209. {
  4210. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4211. {
  4212. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4213. }
  4214. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4215. {
  4216. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4217. }
  4218. return false;
  4219. }
  4220. //===============================================
  4221. // Ocpp ChargingProfileConf
  4222. //===============================================
  4223. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4224. {
  4225. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4226. {
  4227. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4228. }
  4229. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4230. {
  4231. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4232. }
  4233. return false;
  4234. }
  4235. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4236. {
  4237. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4238. {
  4239. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4240. return;
  4241. }
  4242. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4243. {
  4244. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4245. return;
  4246. }
  4247. }
  4248. //===============================================
  4249. // Ocpp ChargingProfileReq
  4250. //===============================================
  4251. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4252. {
  4253. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4254. {
  4255. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4256. }
  4257. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4258. {
  4259. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4260. }
  4261. return false;
  4262. }
  4263. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4264. {
  4265. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4266. {
  4267. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4268. return;
  4269. }
  4270. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4271. {
  4272. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4273. return;
  4274. }
  4275. }
  4276. //===============================================
  4277. // Ocpp SmartChargingProfile ChargingProfileKind
  4278. //===============================================
  4279. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4280. {
  4281. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4282. {
  4283. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4284. }
  4285. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4286. {
  4287. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4288. }
  4289. return false;
  4290. }
  4291. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4292. {
  4293. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4294. {
  4295. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4296. }
  4297. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4298. {
  4299. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4300. }
  4301. return false;
  4302. }
  4303. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4304. {
  4305. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4306. {
  4307. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4308. }
  4309. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4310. {
  4311. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4312. }
  4313. return 0;
  4314. }
  4315. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4316. {
  4317. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4318. {
  4319. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4320. }
  4321. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4322. {
  4323. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4324. }
  4325. return 0;
  4326. }
  4327. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4328. {
  4329. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4330. {
  4331. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4332. }
  4333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4334. {
  4335. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4336. }
  4337. return 0;
  4338. }
  4339. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4340. {
  4341. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4342. {
  4343. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4344. }
  4345. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4346. {
  4347. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4348. }
  4349. return 0;
  4350. }
  4351. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4352. {
  4353. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4354. {
  4355. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4356. }
  4357. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4358. {
  4359. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4360. }
  4361. return 0;
  4362. }
  4363. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4364. {
  4365. int _time = 0, _scheduleTime = 0;
  4366. int _maxCount = 0;
  4367. float _limit = 0;
  4368. int _startCount = NO_DEFINE;
  4369. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4370. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4371. for(int i = 0; i < _maxCount; i++)
  4372. {
  4373. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4374. if(_time >= _scheduleTime)
  4375. {
  4376. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4377. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4378. {
  4379. _startCount = i;
  4380. }
  4381. }
  4382. }
  4383. return _startCount;
  4384. }
  4385. //===============================================
  4386. // Ocpp MaxChargingProfile ChargingProfileKind
  4387. //===============================================
  4388. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4389. {
  4390. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4391. {
  4392. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4393. }
  4394. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4395. {
  4396. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4397. }
  4398. return false;
  4399. }
  4400. bool Is_Ocpp_MaxChargingProfileId(void)
  4401. {
  4402. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4403. {
  4404. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4405. }
  4406. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4407. {
  4408. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4409. }
  4410. return false;
  4411. }
  4412. int Get_Ocpp_MaxProfileStartTime(void)
  4413. {
  4414. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4415. {
  4416. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4417. }
  4418. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4419. {
  4420. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4421. }
  4422. return 0;
  4423. }
  4424. int Get_Ocpp_MaxProfileScheduleCount(void)
  4425. {
  4426. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4427. {
  4428. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4429. }
  4430. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4431. {
  4432. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4433. }
  4434. return 0;
  4435. }
  4436. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4437. {
  4438. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4439. {
  4440. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4441. }
  4442. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4443. {
  4444. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4445. }
  4446. return 0;
  4447. }
  4448. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  4449. {
  4450. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4451. {
  4452. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4453. }
  4454. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4455. {
  4456. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4457. }
  4458. return 0;
  4459. }
  4460. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  4461. {
  4462. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4463. {
  4464. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4465. }
  4466. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4467. {
  4468. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4469. }
  4470. return 0;
  4471. }
  4472. int Get_Ocpp_MaxProfileStartIndex(void)
  4473. {
  4474. int _time = 0, _scheduleTime = 0;
  4475. int _maxCount = 0;
  4476. float _limit = 0;
  4477. int _startCount = NO_DEFINE;
  4478. _time = Get_Ocpp_MaxProfileStartTime();
  4479. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  4480. for(int i = 0; i < _maxCount; i++)
  4481. {
  4482. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  4483. if(_time >= _scheduleTime)
  4484. {
  4485. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  4486. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4487. {
  4488. _startCount = i;
  4489. }
  4490. }
  4491. }
  4492. return _startCount;
  4493. }
  4494. //===============================================
  4495. // Ocpp isRemoteStartWaitReq
  4496. //===============================================
  4497. int Is_Ocpp_RemoteStartWaitReq(void)
  4498. {
  4499. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4500. {
  4501. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  4502. }
  4503. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4504. {
  4505. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  4506. }
  4507. return 0;
  4508. }
  4509. //===============================================
  4510. // Ocpp SessionTarget
  4511. //===============================================
  4512. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  4513. {
  4514. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4515. {
  4516. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  4517. }
  4518. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4519. {
  4520. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  4521. }
  4522. }
  4523. //===============================================
  4524. // DeductResult
  4525. //===============================================
  4526. void Set_Deduct_UserId(int gunIndex, char *userId)
  4527. {
  4528. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  4529. }
  4530. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  4531. {
  4532. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  4533. }
  4534. void Clean_DeductInfo(int gunIndex)
  4535. {
  4536. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  4537. }
  4538. bool Is_DeductResultReq(int gunIndex)
  4539. {
  4540. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  4541. }
  4542. //===============================================
  4543. // ReaderStatus
  4544. //===============================================
  4545. bool Is_ReaderStatusReq(int gunIndex)
  4546. {
  4547. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  4548. }
  4549. void Clean_ReaderStatusInfo(int gunIndex)
  4550. {
  4551. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  4552. }
  4553. //===============================================
  4554. // TransactionInfoData
  4555. //===============================================
  4556. void Set_UserTransactionId(int gunIndex, int transaction)
  4557. {
  4558. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  4559. }
  4560. void Set_UserPrice(int gunIndex, float price)
  4561. {
  4562. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  4563. }
  4564. void Set_UserTotalCost(int gunIndex, float cost)
  4565. {
  4566. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  4567. }
  4568. void Set_UserAccountBalance(int gunIndex, float balance)
  4569. {
  4570. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  4571. }
  4572. void Set_UserDiscount(int gunIndex, float discount)
  4573. {
  4574. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  4575. }
  4576. void Clean_UserTransaction(int gunIndex)
  4577. {
  4578. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  4579. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  4580. }
  4581. void Clean_UserPriceAndReceipt(int gunIndex)
  4582. {
  4583. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  4584. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  4585. }
  4586. //===============================================
  4587. // Main process
  4588. //===============================================
  4589. // 檢查 Byte 中某個 Bit 的值
  4590. // _byte : 欲改變的 byte
  4591. // _bit : 該 byte 的第幾個 bit
  4592. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  4593. {
  4594. return ( _byte & mask_table[_bit] ) != 0x00;
  4595. }
  4596. // 設定 Byte 中某個 Bit的值
  4597. // _byte : 欲改變的 byte
  4598. // _bit : 該 byte 的第幾個 bit
  4599. // value : 修改的值為 0 or 1
  4600. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  4601. {
  4602. if(value == 1)
  4603. *_byte |= (1 << _bit);
  4604. else if (value == 0)
  4605. *_byte ^= (1 << _bit);
  4606. }
  4607. void UserScanFunction()
  4608. {
  4609. }
  4610. unsigned char isModeChange(unsigned char gun_index)
  4611. {
  4612. unsigned char result = NO;
  4613. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  4614. {
  4615. result = YES;
  4616. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  4617. }
  4618. return result;
  4619. }
  4620. void AnnounceAccountBalance(unsigned char index, float balance)
  4621. {
  4622. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  4623. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  4624. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  4625. {
  4626. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  4627. }
  4628. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  4629. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  4630. }
  4631. void FouceAnnounceAccountBalance(unsigned char index)
  4632. {
  4633. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  4634. {
  4635. AnnounceAccountBalance(index, 0);
  4636. }
  4637. }
  4638. void CleanConnectorUserIdAndPrice(unsigned char index)
  4639. {
  4640. if(chargingInfo[index]->SystemStatus == S_IDLE)
  4641. {
  4642. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  4643. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  4644. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  4645. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  4646. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  4647. }
  4648. }
  4649. void DispenserAuthInitial(unsigned char dispenser)
  4650. {
  4651. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  4652. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4653. // clean dispenser price
  4654. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  4655. }
  4656. void GunAuthInitial(unsigned char connector)
  4657. {
  4658. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  4659. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4660. }
  4661. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  4662. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  4663. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  4664. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  4665. void AuthorizeTimeoutProcess(void)
  4666. {
  4667. int CompletedTime = 0;
  4668. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  4669. {
  4670. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  4671. {
  4672. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  4673. {
  4674. GetClockTime(&_GunAuth_Time[index]);
  4675. }
  4676. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  4677. {
  4678. FouceAnnounceAccountBalance(index);
  4679. }
  4680. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  4681. {
  4682. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  4683. {
  4684. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  4685. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4686. CleanConnectorUserIdAndPrice(index);
  4687. }
  4688. }
  4689. }
  4690. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  4691. }
  4692. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  4693. {
  4694. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  4695. {
  4696. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  4697. {
  4698. GetClockTime(&_DispenserAuth_Time[index]);
  4699. }
  4700. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  4701. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  4702. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  4703. {
  4704. DispenserAuthInitial(index);
  4705. }
  4706. }
  4707. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  4708. }
  4709. }
  4710. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  4711. {
  4712. BOOL success = false;
  4713. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  4714. {
  4715. success = true;
  4716. }
  4717. return success;
  4718. }
  4719. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  4720. {
  4721. BOOL success = false;
  4722. unsigned char dispenser = 0;
  4723. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  4724. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  4725. {
  4726. success = true;
  4727. }
  4728. return success;
  4729. }
  4730. unsigned short ParsingRatingPower(char *modelname)
  4731. {
  4732. char strVal[3], strExp[2];
  4733. unsigned short value = 0, exponent = 0;
  4734. memset(strVal, 0x00, sizeof(strVal));
  4735. memset(strExp, 0x00, sizeof(strExp));
  4736. memcpy(strVal, &modelname[4], 2);
  4737. memcpy(strExp, &modelname[6], 1);
  4738. value = atoi(strVal);
  4739. exponent = atoi(strExp);
  4740. if(value < 0 || exponent < 0)
  4741. {
  4742. return 0;
  4743. }
  4744. for(int i = 0; i < exponent; i++)
  4745. {
  4746. value *= 10;
  4747. }
  4748. return value;
  4749. }
  4750. bool CheckConnectorTypeStatus(void)
  4751. {
  4752. bool result = true;
  4753. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  4754. {
  4755. // DO360 for Audi, two gun only
  4756. _gunIndex = ShmChargerInfo->Control.MaxConnector;
  4757. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  4758. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4759. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  4760. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  4761. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  4762. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  4763. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  4764. {
  4765. result = false;
  4766. }
  4767. //DO360 charging info default setting
  4768. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4769. {
  4770. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  4771. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  4772. chargingInfo[i]->Evboard_id = 0x00;
  4773. chargingInfo[i]->ReservationId = -1;
  4774. chargingInfo[i]->slotsIndex = 0;
  4775. chargingInfo[i]->SystemStatus = S_BOOTING;
  4776. chargingInfo[i]->Type = _Type_Unknown;
  4777. chargingInfo[i]->type_index = 0;
  4778. chargingInfo[i]->IsAvailable = YES;
  4779. }
  4780. }
  4781. else
  4782. {
  4783. // Module Name 不正確 - 告警
  4784. result = false;
  4785. }
  4786. return result;
  4787. }
  4788. void KillTaskExceptPrimary()
  4789. {
  4790. system("killall Module_Authorize");
  4791. system("killall Module_CabinetParallel");
  4792. system("killall Module_ChargerSelfTest");
  4793. system("killall Module_LedIndication");
  4794. system("killall Module_EvComm");
  4795. system("killall Module_InternalComm");
  4796. system("killall Module_PsuComm");
  4797. system("killall Module_4g &");
  4798. system("killall Module_Wifi &");
  4799. }
  4800. void KillAllTask()
  4801. {
  4802. system("killall Module_Authorize");
  4803. system("killall Module_CabinetParallel");
  4804. system("killall Module_ChargerSelfTest");
  4805. system("killall Module_LedIndication");
  4806. system("killall Module_EventLogging");
  4807. system("killall Module_PrimaryComm");
  4808. system("killall Module_EvComm");
  4809. system("killall Module_LcmControl");
  4810. system("killall Module_InternalComm");
  4811. system("killall Module_PsuComm");
  4812. Kill_Ocpp_Service();
  4813. Kill_MaintainOcpp_Service();
  4814. system("killall Module_4g &");
  4815. system("killall Module_Wifi &");
  4816. }
  4817. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  4818. {
  4819. int isPass = PASS;
  4820. unsigned char ftpcmdbuf[256];
  4821. LOG_INFO("Transfer image(%s) to ip: %s ", SourcePath, IpAddress);
  4822. //Using ftpput command to transfer upgrade image,
  4823. //User name : root
  4824. //User password : y42j/4cj84
  4825. //Destination : as same as source path
  4826. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  4827. if(system((char*)ftpcmdbuf) != 0)
  4828. {
  4829. isPass = FAIL;
  4830. LOG_INFO("Transfer Image NG - FTP put upgrade image to fail");
  4831. }
  4832. else
  4833. {
  4834. LOG_INFO("FTP put Image OK");
  4835. }
  4836. return isPass;
  4837. }
  4838. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  4839. {
  4840. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  4841. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  4842. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  4843. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  4844. }
  4845. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName)
  4846. {
  4847. int isPass = FAIL;
  4848. BOOL transferred = FALSE;
  4849. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4850. {
  4851. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  4852. {
  4853. char DispenserIpAddress[16];
  4854. memset((char*)DispenserIpAddress, 0x00, 16);
  4855. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  4856. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  4857. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  4858. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  4859. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  4860. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  4861. if(isPass == PASS)
  4862. {
  4863. TriggerDispenserUpgrade(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex, FileName);
  4864. }
  4865. transferred = true;
  4866. }
  4867. }
  4868. return transferred;
  4869. }
  4870. BOOL WaitAllDispenserUpgradeCompleted(void)
  4871. {
  4872. BOOL completed = TRUE;
  4873. GetClockTime(&_DispenserUpgrade_time);
  4874. do
  4875. {
  4876. completed = TRUE;
  4877. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4878. {
  4879. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  4880. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  4881. {
  4882. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  4883. {
  4884. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  4885. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  4886. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  4887. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  4888. }
  4889. else
  4890. {
  4891. completed = FALSE;
  4892. }
  4893. }
  4894. }
  4895. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  4896. {
  4897. LOG_INFO("Wait Upgrade Timeout");
  4898. completed = FALSE;
  4899. break;
  4900. }
  4901. if(completed == FALSE)
  4902. {
  4903. sleep(1);
  4904. }
  4905. }while(!completed);
  4906. return completed;
  4907. }
  4908. void UpgradePrepare(void)
  4909. {
  4910. int count = 0;
  4911. SetAcContactor(OFF);
  4912. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4913. {
  4914. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4915. {
  4916. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4917. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4918. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4919. {
  4920. count++;
  4921. }
  4922. }
  4923. }
  4924. if(count > 0)
  4925. {
  4926. sleep(3);
  4927. }
  4928. else
  4929. {
  4930. sleep(1);
  4931. }
  4932. }
  4933. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  4934. {
  4935. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  4936. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  4937. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  4938. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  4939. }
  4940. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  4941. {
  4942. int isPass = FAIL;
  4943. BOOL transferred = FALSE;
  4944. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4945. {
  4946. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4947. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4948. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4949. {
  4950. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  4951. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  4952. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  4953. {
  4954. char PCabinetIpAddress[16];
  4955. memset((char*)PCabinetIpAddress, 0x00, 16);
  4956. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  4957. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  4958. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  4959. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  4960. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  4961. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  4962. if(isPass == PASS)
  4963. {
  4964. TriggerPCabinetUpgrade(i, FileName);
  4965. }
  4966. transferred = true;
  4967. }
  4968. }
  4969. }
  4970. return transferred;
  4971. }
  4972. BOOL WaitAllPCabinetStartUpgrade(void)
  4973. {
  4974. BOOL isStart = true;
  4975. int count = 0;
  4976. struct timespec _waitStart;
  4977. GetClockTime(&_waitStart);
  4978. do
  4979. {
  4980. isStart = true;
  4981. count = 0;
  4982. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4983. {
  4984. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4985. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4986. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4987. {
  4988. count++;
  4989. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  4990. {
  4991. isStart = false;
  4992. }
  4993. }
  4994. }
  4995. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  4996. {
  4997. isStart = false;
  4998. break;
  4999. }
  5000. usleep(10000);
  5001. }while(!isStart);
  5002. if(isStart && count > 0)
  5003. {
  5004. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5005. }
  5006. else
  5007. {
  5008. if(count > 0)
  5009. {
  5010. LOG_INFO("Wait PCabinet upgrade timeout");
  5011. }
  5012. else
  5013. {
  5014. LOG_INFO("No PCabinet need to upgrade");
  5015. }
  5016. }
  5017. return isStart;
  5018. }
  5019. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5020. {
  5021. BOOL completed = true;
  5022. int count = 0;
  5023. struct timespec _waitCompleted;
  5024. GetClockTime(&_waitCompleted);
  5025. do
  5026. {
  5027. completed = true;
  5028. count = 0;
  5029. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5030. {
  5031. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5032. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5033. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5034. {
  5035. count++;
  5036. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5037. {
  5038. completed = false;
  5039. }
  5040. }
  5041. }
  5042. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5043. {
  5044. completed = false;
  5045. break;
  5046. }
  5047. usleep(10000);
  5048. }while(!completed);
  5049. if(completed && count > 0)
  5050. {
  5051. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5052. }
  5053. else
  5054. {
  5055. if(count > 0)
  5056. {
  5057. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5058. }
  5059. else
  5060. {
  5061. LOG_INFO("Need not to wait PCabinet completed");
  5062. }
  5063. }
  5064. return completed;
  5065. }
  5066. BOOL WaitMCabinetUpgradeCompleted(void)
  5067. {
  5068. BOOL completed = true;
  5069. struct timespec _waitCompleted;
  5070. GetClockTime(&_waitCompleted);
  5071. do
  5072. {
  5073. completed = false;
  5074. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5075. {
  5076. completed = true;
  5077. }
  5078. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5079. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5080. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5081. {
  5082. completed = false;
  5083. break;
  5084. }
  5085. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5086. {
  5087. completed = false;
  5088. break;
  5089. }
  5090. usleep(10000);
  5091. }while(!completed);
  5092. if(completed)
  5093. {
  5094. LOG_INFO("MCabinet upgrade completed!");
  5095. }
  5096. else
  5097. {
  5098. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5099. }
  5100. return completed;
  5101. }
  5102. bool IsModelNameMatch(char *model)
  5103. {
  5104. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5105. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5106. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5107. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5108. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5109. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5110. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5111. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5112. {
  5113. return true;
  5114. }
  5115. return false;
  5116. }
  5117. unsigned long long getAvailableMemory(void)
  5118. {
  5119. long pages = sysconf(_SC_AVPHYS_PAGES);
  5120. long page_size = sysconf(_SC_PAGE_SIZE);
  5121. return pages * page_size;
  5122. }
  5123. char CheckUpdateProcess()
  5124. {
  5125. char model[32];
  5126. bool support = false;
  5127. bool OneImageOnly = false;
  5128. int isPass = true;
  5129. uint8_t retSucc = 0;
  5130. uint8_t retFail = 0;
  5131. byte target = 0;
  5132. char *new_str = NULL;
  5133. unsigned char *ptr = NULL;
  5134. int fd = 0;
  5135. int uartFd = 0;
  5136. unsigned int Type = 0;
  5137. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5138. DIR *d;
  5139. struct dirent *dir;
  5140. char cmdBuf[128];
  5141. LOG_INFO("AvailableMemory: %dMByte", (getAvailableMemory() / (1024 * 1024)));
  5142. d = opendir("/mnt/");
  5143. if(d)
  5144. {
  5145. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5146. {
  5147. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5148. continue;
  5149. }
  5150. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5151. strcat(new_str, "/mnt/");
  5152. strcat(new_str, dir->d_name);
  5153. fd = open(new_str, O_RDONLY);
  5154. if(fd < 0)
  5155. {
  5156. close(fd);
  5157. free(new_str);
  5158. continue;
  5159. }
  5160. LOG_INFO("%s\n", new_str);
  5161. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5162. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5163. //get the image length
  5164. ImageLen = read(fd, ptr, MaxLen);
  5165. if(ImageLen > 20)
  5166. {
  5167. memcpy(model, ptr, 16);
  5168. LOG_INFO("Model: %s", model);
  5169. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5170. LOG_INFO("Typed...%x", Type);
  5171. }
  5172. free(ptr);
  5173. if (ImageLen > 20)
  5174. {
  5175. support = false;
  5176. LOG_INFO("AvailableMemory: %dMByte", (getAvailableMemory() / (1024 * 1024)));
  5177. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5178. if(IsModelNameMatch(model))
  5179. {
  5180. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5181. {
  5182. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5183. WaitAllPCabinetStartUpgrade();
  5184. }
  5185. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5186. {
  5187. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5188. sleep(1);
  5189. }
  5190. switch (Type)
  5191. {
  5192. case 0x10000001:
  5193. case 0x10000002:
  5194. case 0x10000003:
  5195. case 0x10000004:
  5196. case 0x10000005:
  5197. support = true;
  5198. _UpgradeNeedReboot = TRUE;
  5199. LOG_INFO("Upgrade Flash Start...", Type);
  5200. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5201. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5202. break;
  5203. case 0x10000006: // primary mcu
  5204. case 0x1000000D: // relay board
  5205. case 0x1000000E: // fan board
  5206. case 0x10000014: // led board
  5207. support = true;
  5208. target = 0x00;
  5209. if (Type == 0x10000006)
  5210. {
  5211. target = UPGRADE_PRI;
  5212. }
  5213. else if (Type == 0x1000000D)
  5214. {
  5215. target = UPGRADE_RB;
  5216. }
  5217. else if (Type == 0x1000000E)
  5218. {
  5219. target = UPGRADE_FAN;
  5220. }
  5221. else if (Type == 0x10000014)
  5222. {
  5223. target = UPGRADE_LED;
  5224. }
  5225. uartFd = InitComPort(target);
  5226. if(target == UPGRADE_RB)
  5227. {
  5228. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5229. if(isPass == PASS)
  5230. {
  5231. retSucc++;
  5232. }
  5233. else
  5234. {
  5235. retFail++;
  5236. }
  5237. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5238. sleep(3);
  5239. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5240. }
  5241. else
  5242. {
  5243. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5244. }
  5245. close(uartFd);
  5246. break;
  5247. }
  5248. if(support)
  5249. {
  5250. if(isPass == PASS)
  5251. {
  5252. retSucc++;
  5253. }
  5254. else
  5255. {
  5256. retFail++;
  5257. }
  5258. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5259. }
  5260. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5261. {
  5262. LOG_INFO("MCabinet upgrade completed");
  5263. WaitAllPCabinetUpgradeCompleted();
  5264. }
  5265. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  5266. {
  5267. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  5268. LOG_INFO("SCabinet upgrade completed");
  5269. WaitMCabinetUpgradeCompleted();
  5270. }
  5271. }
  5272. else
  5273. {
  5274. if(model[0] == 'D' && (model[1] == 'D' || model[1] == 'X'))
  5275. {
  5276. OneImageOnly = true;
  5277. _UpgradeNeedReboot = TRUE;
  5278. LOG_INFO("Start Put Image To All Dispenser");
  5279. PutImageToAllDispenser(new_str, dir->d_name);
  5280. WaitAllDispenserUpgradeCompleted();
  5281. LOG_INFO("All Dispenser Upgrade Completed");
  5282. }
  5283. }
  5284. }
  5285. else
  5286. {
  5287. retFail++;
  5288. LOG_INFO("Model Name Is Not Match: %s", model);
  5289. }
  5290. sprintf(cmdBuf, "rm -f %s", new_str);
  5291. system(cmdBuf);
  5292. close(fd);
  5293. free(new_str);
  5294. }
  5295. }
  5296. free(dir);
  5297. closedir(d);
  5298. if(retFail != 0)
  5299. {
  5300. return FAIL;
  5301. }
  5302. return PASS;
  5303. }
  5304. void TriggerDispenserConnectionTimeoutSetting(void)
  5305. {
  5306. BOOL trigger = FALSE;
  5307. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5308. {
  5309. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  5310. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  5311. {
  5312. trigger = TRUE;
  5313. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  5314. }
  5315. }
  5316. if(trigger)
  5317. {
  5318. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  5319. }
  5320. }
  5321. void CheckConnectionTimeout(void)
  5322. {
  5323. if(!Is_Ocpp_Task_Alive())
  5324. {
  5325. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5326. }
  5327. else
  5328. {
  5329. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5330. {
  5331. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  5332. if(_connectionTimeout <= 0)
  5333. {
  5334. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5335. }
  5336. }
  5337. else
  5338. {
  5339. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5340. }
  5341. }
  5342. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  5343. {
  5344. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  5345. TriggerDispenserConnectionTimeoutSetting();
  5346. }
  5347. }
  5348. void CreateTimeoutFork()
  5349. {
  5350. pid_t timeoutPid;
  5351. timeoutPid = fork();
  5352. if (timeoutPid == 0)
  5353. {
  5354. GetClockTime(&_cmdSubPriority_time);
  5355. CheckConnectionTimeout();
  5356. LOG_INFO("Timeout Fork PID = %d", getpid());
  5357. while(true)
  5358. {
  5359. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  5360. {
  5361. CheckConnectionTimeout();
  5362. GetClockTime(&_cmdSubPriority_time);
  5363. }
  5364. sleep(1);
  5365. }
  5366. }
  5367. }
  5368. void CheckFactoryConfigFunction(void)
  5369. {
  5370. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5371. {
  5372. char Buf[256];
  5373. sprintf(Buf, "cd /root;./FactoryConfig -m");
  5374. system(Buf);
  5375. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5376. system("sync");
  5377. sleep(5);
  5378. system("reboot -f");
  5379. sleep(5);
  5380. system("reboot -f");
  5381. }
  5382. }
  5383. void CheckFwUpdateFunction(void)
  5384. {
  5385. char UpdateResult = PASS;
  5386. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  5387. {
  5388. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5389. UpgradePrepare();
  5390. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5391. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5392. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5393. LOG_INFO("ftp : update start.");
  5394. TryCloseWatchdog();
  5395. //KillTask();
  5396. system("killall Module_PrimaryComm");
  5397. system("killall Module_InternalComm");
  5398. UpdateResult = CheckUpdateProcess();
  5399. if (UpdateResult == PASS)
  5400. {
  5401. LOG_INFO("ftp : update complete.");
  5402. }
  5403. else
  5404. {
  5405. LOG_INFO("ftp : update fail.");
  5406. }
  5407. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  5408. sleep(10);
  5409. if(_UpgradeNeedReboot)
  5410. {
  5411. system("reboot -f");
  5412. }
  5413. else
  5414. {
  5415. KillAllTask();
  5416. system("/usr/bin/run_evse_restart.sh");
  5417. }
  5418. }
  5419. else if(Is_Ocpp_UpdateFirmwareReq())
  5420. {
  5421. Clean_Ocpp_UpdateFirmwareReq();
  5422. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5423. UpgradePrepare();
  5424. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5425. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5426. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5427. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  5428. {
  5429. LOG_INFO("Backend : update start.");
  5430. TryCloseWatchdog();
  5431. Set_Ocpp_UpdateFirmwareStatus("Installing");
  5432. Set_Ocpp_FirmwareStatusNotificationReq();
  5433. //KillTask();
  5434. system("killall Module_PrimaryComm");
  5435. system("killall Module_InternalComm");
  5436. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5437. {
  5438. setChargerMode(_index, MODE_UPDATE);
  5439. }
  5440. UpdateResult = CheckUpdateProcess();
  5441. if (UpdateResult == PASS)
  5442. {
  5443. LOG_INFO("Backend : update complete.");
  5444. Set_Ocpp_UpdateFirmwareStatus("Installed");
  5445. }
  5446. else
  5447. {
  5448. LOG_INFO("Backend : update fail.");
  5449. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  5450. }
  5451. Set_Ocpp_FirmwareStatusNotificationReq();
  5452. sleep(10);
  5453. if(_UpgradeNeedReboot)
  5454. {
  5455. system("reboot -f");
  5456. }
  5457. else
  5458. {
  5459. KillAllTask();
  5460. system("/usr/bin/run_evse_restart.sh");
  5461. }
  5462. }
  5463. }
  5464. }
  5465. void ExecuteWriteFlash(void)
  5466. {
  5467. struct SysConfigData config;
  5468. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  5469. config.SwitchDebugFlag = NO;
  5470. strcpy((char *) config.UserId, "");
  5471. int result = StoreUsrConfigData(&config);
  5472. if(result != 1)
  5473. {
  5474. LOG_INFO("Store User Config NG");
  5475. }
  5476. else
  5477. {
  5478. LOG_INFO("Store User Config OK");
  5479. }
  5480. }
  5481. struct timespec _WiringInfo_time;
  5482. int CheckFlashUpdate(void)
  5483. {
  5484. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  5485. {
  5486. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  5487. LOG_INFO("Flash Config Changed");
  5488. GetClockTime(&_WiringInfo_time);
  5489. // wait for 30s to write flash or write flash immediately
  5490. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  5491. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  5492. {
  5493. LOG_INFO("Start Writing Flash");
  5494. pid_t writePid = fork();
  5495. if(writePid == 0)
  5496. {
  5497. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  5498. ExecuteWriteFlash();
  5499. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  5500. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  5501. {
  5502. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  5503. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  5504. LOG_INFO("Write Flash OK And Need Soft Reset");
  5505. }
  5506. return NO;
  5507. }
  5508. LOG_INFO("Write Flash Conpleted");
  5509. }
  5510. }
  5511. return YES;
  5512. }
  5513. void CheckSoftHardReset(void)
  5514. {
  5515. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  5516. {
  5517. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  5518. LOG_INFO("********** Need Soft Reset **********");
  5519. sleep(5);
  5520. Kill_Ocpp_Service();
  5521. KillAllTask();
  5522. TryCloseWatchdog();
  5523. system("/usr/bin/run_evse_restart.sh");
  5524. }
  5525. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  5526. {
  5527. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  5528. LOG_INFO("********** Need Hard Reset **********");
  5529. sleep(5);
  5530. system("reboot -f");
  5531. }
  5532. }
  5533. struct timespec _Reboot_time;
  5534. void TriggerDispenserHardwareReboot(void)
  5535. {
  5536. BOOL trigger = FALSE;
  5537. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5538. {
  5539. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5540. {
  5541. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  5542. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  5543. {
  5544. trigger = TRUE;
  5545. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  5546. }
  5547. }
  5548. }
  5549. if(trigger)
  5550. {
  5551. GetClockTime(&_Reboot_time);
  5552. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  5553. }
  5554. }
  5555. BOOL IsDispenserHardwareRebootResponsed(void)
  5556. {
  5557. BOOL response_ok = TRUE;
  5558. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5559. {
  5560. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5561. {
  5562. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  5563. {
  5564. response_ok = FALSE;
  5565. // 10s for reboot request timeout
  5566. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  5567. {
  5568. response_ok = TRUE;
  5569. LOG_INFO("Reboot request timeout");
  5570. }
  5571. }
  5572. }
  5573. }
  5574. return response_ok;
  5575. }
  5576. struct timespec _Reset_time;
  5577. void TriggerDispenserSoftwareReset(void)
  5578. {
  5579. BOOL trigger = FALSE;
  5580. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5581. {
  5582. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5583. {
  5584. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  5585. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  5586. {
  5587. trigger = TRUE;
  5588. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  5589. }
  5590. }
  5591. }
  5592. if(trigger)
  5593. {
  5594. GetClockTime(&_Reset_time);
  5595. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  5596. }
  5597. }
  5598. BOOL IsDispenserSoftwareResetResponsed(void)
  5599. {
  5600. BOOL response_ok = TRUE;
  5601. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5602. {
  5603. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5604. {
  5605. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  5606. {
  5607. response_ok = FALSE;
  5608. // 10s for reset request timeout
  5609. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  5610. {
  5611. response_ok = TRUE;
  5612. LOG_INFO("Reset request timeout");
  5613. }
  5614. }
  5615. }
  5616. }
  5617. return response_ok;
  5618. }
  5619. float TariffParsing(char *StringItem, char *TariffCode)
  5620. {
  5621. char *ptrSave, *ptrToken;
  5622. char strSource[128];
  5623. float fee = 0;
  5624. memcpy(strSource, StringItem, 128);
  5625. ptrToken = strtok_r(strSource, ";", &ptrSave);
  5626. while(ptrToken != NULL)
  5627. {
  5628. char *strTariff = strstr(ptrToken, TariffCode);
  5629. if(strTariff != NULL)
  5630. {
  5631. char *strFee = strstr(strTariff, "$");
  5632. if(strFee != NULL)
  5633. {
  5634. strFee++;
  5635. fee = atof(strFee);
  5636. break;
  5637. }
  5638. }
  5639. ptrToken = strtok_r(NULL, ";", &ptrSave);
  5640. }
  5641. return fee;
  5642. }
  5643. float PhTariffParsing(char *StringItem, char *TariffCode)
  5644. {
  5645. char *ptrSave, *ptrToken;
  5646. char strSource[128];
  5647. float fee = 0;
  5648. memcpy(strSource, StringItem, 128);
  5649. ptrToken = strtok_r(strSource, ",", &ptrSave);
  5650. while(ptrToken != NULL)
  5651. {
  5652. char *strTariff = strstr(ptrToken, TariffCode);
  5653. if(strTariff != NULL)
  5654. {
  5655. char *strFee = strstr(strTariff, ":");
  5656. if(strFee != NULL)
  5657. {
  5658. strFee++;
  5659. fee = atof(strFee);
  5660. break;
  5661. }
  5662. }
  5663. ptrToken = strtok_r(NULL, ",", &ptrSave);
  5664. }
  5665. return fee;
  5666. }
  5667. float DefaultPriceParsing(char *StringItem)
  5668. {
  5669. char *ptrSave, *ptrToken;
  5670. char strSource[128];
  5671. float fee = 0;
  5672. memcpy(strSource, StringItem, 128);
  5673. ptrToken = strtok_r(strSource, ",", &ptrSave);
  5674. while(ptrToken != NULL)
  5675. {
  5676. char *strFee = strstr(ptrToken, "$");
  5677. if(strFee != NULL)
  5678. {
  5679. strFee++;
  5680. fee = atof(strFee);
  5681. break;
  5682. }
  5683. else
  5684. {
  5685. fee = atof(ptrToken);
  5686. break;
  5687. }
  5688. ptrToken = strtok_r(NULL, ",", &ptrSave);
  5689. }
  5690. return fee;
  5691. }
  5692. // return time offset
  5693. int TimeOffsetKeyParsing(char *StringItem)
  5694. {
  5695. int offset = 0;
  5696. int hour = 0, minute = 0, sign = 1;
  5697. char *ptrSave, *ptrToken;
  5698. char strSource[501];
  5699. memset(strSource, 0x00, sizeof(strSource));
  5700. strcpy(strSource, StringItem);
  5701. if(strSource[0] != '+' && strSource[0] != '-')
  5702. {
  5703. return offset;
  5704. }
  5705. sign = strSource[0] == '+' ? (1) : (-1);
  5706. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  5707. if(ptrToken != NULL)
  5708. {
  5709. hour = atoi(ptrToken);
  5710. ptrToken = strtok_r(NULL, ":", &ptrSave);
  5711. if(ptrToken != NULL)
  5712. {
  5713. minute = atoi(ptrToken);
  5714. offset = ((hour * 60) + minute) * sign;
  5715. }
  5716. }
  5717. return offset;
  5718. }
  5719. char MyDefaultPriceString[501];
  5720. void DefaultPriceHandler(void)
  5721. {
  5722. char price[501];
  5723. char defaultPrice[512];
  5724. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  5725. Get_Ocpp_DefaultPrice((char *)price);
  5726. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  5727. {
  5728. BOOL trigger = FALSE;
  5729. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  5730. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5731. {
  5732. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  5733. CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  5734. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  5735. LOG_INFO("******** Customer Audi Default Price *******");
  5736. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  5737. LOG_INFO("********** Current Rate: %4.2f **********", CurrentRate);
  5738. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  5739. }
  5740. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  5741. {
  5742. CurrentRate = DefaultPriceParsing(price);
  5743. ShellPriceParsing(price, defaultPrice);
  5744. LOG_INFO("******* Customer Shell Default Price *******");
  5745. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  5746. }
  5747. else
  5748. {
  5749. CurrentRate = DefaultPriceParsing(price);
  5750. strcpy(defaultPrice, price);
  5751. LOG_INFO("********** Standard Default Price **********");
  5752. LOG_INFO("********** Current Rate: %4.2f **********", CurrentRate);
  5753. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  5754. }
  5755. ShmChargerInfo->CabinetMiscValue.DefaultPrice = CurrentRate;
  5756. if(strlen(defaultPrice) > 0)
  5757. {
  5758. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  5759. }
  5760. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5761. {
  5762. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5763. {
  5764. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  5765. {
  5766. trigger = TRUE;
  5767. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  5768. }
  5769. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  5770. {
  5771. trigger = TRUE;
  5772. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  5773. }
  5774. if(strlen(defaultPrice) > 0)
  5775. {
  5776. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  5777. {
  5778. trigger = TRUE;
  5779. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  5780. }
  5781. }
  5782. }
  5783. }
  5784. if(trigger)
  5785. {
  5786. LOG_INFO("********** Trigger Dispenser Default Price **********");
  5787. }
  5788. strcpy(MyDefaultPriceString, price);
  5789. }
  5790. }
  5791. void UserPriceHandler(void)
  5792. {
  5793. char UserId[32];
  5794. char price[500];
  5795. char userPrice[512];
  5796. memset(userPrice, 0x00, sizeof(userPrice));
  5797. Get_Ocpp_UserPrice(UserId, price);
  5798. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  5799. {
  5800. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  5801. float AccountBalance = 0;
  5802. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5803. {
  5804. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  5805. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  5806. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  5807. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  5808. LOG_INFO("********* Customer Audi User Price *********");
  5809. LOG_INFO("********* User Id: [%s]", UserId);
  5810. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  5811. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  5812. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  5813. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  5814. }
  5815. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  5816. {
  5817. ShellPriceParsing(price, userPrice);
  5818. LOG_INFO("******** Customer Shell User Price *********");
  5819. LOG_INFO("******** User Id: [%s]", UserId);
  5820. LOG_INFO("******** User Price: [%s]", userPrice);
  5821. }
  5822. else
  5823. {
  5824. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  5825. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  5826. strcpy(userPrice, price);
  5827. LOG_INFO("*********** Standard User Price ************");
  5828. LOG_INFO("*********** User Id: [%s]", UserId);
  5829. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  5830. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  5831. LOG_INFO("*********** User Price: [%s]", userPrice);
  5832. }
  5833. #if 0
  5834. LOG_INFO("********** User Id: %s **********", UserId);
  5835. LOG_INFO("********** User Price: %s", price);
  5836. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  5837. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  5838. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  5839. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  5840. #endif
  5841. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5842. {
  5843. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  5844. {
  5845. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  5846. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  5847. {
  5848. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  5849. (i + 1), CurrentRate, AccountBalance);
  5850. }
  5851. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  5852. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  5853. if(strlen(userPrice) > 0)
  5854. {
  5855. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  5856. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  5857. }
  5858. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  5859. {
  5860. AnnounceAccountBalance(i, AccountBalance);
  5861. }
  5862. }
  5863. }
  5864. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5865. {
  5866. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  5867. {
  5868. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  5869. {
  5870. if(strlen(userPrice) > 0)
  5871. {
  5872. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  5873. }
  5874. }
  5875. }
  5876. }
  5877. Clean_Ocpp_UserPrice();
  5878. }
  5879. }
  5880. void RunningCostParsing(int gunIndex, char *strCost)
  5881. {
  5882. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  5883. float AccountBalance = 0;
  5884. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5885. {
  5886. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  5887. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  5888. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  5889. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  5890. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  5891. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  5892. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  5893. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  5894. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  5895. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  5896. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  5897. }
  5898. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  5899. {
  5900. TotalCost = ShellRunningCostParsing(strCost);
  5901. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  5902. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  5903. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  5904. }
  5905. else
  5906. {
  5907. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  5908. TotalCost = EnergyCost;
  5909. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  5910. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  5911. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  5912. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  5913. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  5914. }
  5915. #if 0
  5916. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  5917. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  5918. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  5919. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  5920. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  5921. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  5922. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  5923. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  5924. {
  5925. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  5926. (gunIndex + 1), TotalCost, AccountBalance);
  5927. }
  5928. #endif
  5929. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  5930. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  5931. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  5932. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  5933. }
  5934. void RunningCostHandler(void)
  5935. {
  5936. char strCost[256];
  5937. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5938. {
  5939. Get_Ocpp_RunningCost(i, strCost);
  5940. if(strlen((char *)strCost) > 0)
  5941. {
  5942. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  5943. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_ALARM)
  5944. {
  5945. RunningCostParsing(i, strCost);
  5946. }
  5947. Clean_Ocpp_RunningCost(i);
  5948. }
  5949. }
  5950. }
  5951. void FinalCostParsing(int gunIndex, char *strCost)
  5952. {
  5953. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, CostDiscount = 0, EnergyCost = -1;
  5954. float FarewellCost = 0;
  5955. float AccountBalance = 0;
  5956. char receiptString[512];
  5957. int receiptLength = 0;
  5958. memset(receiptString, 0x00, sizeof(receiptString));
  5959. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5960. {
  5961. ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  5962. SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  5963. OccupancyFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  5964. TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  5965. AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  5966. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  5967. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  5968. LOG_INFO("********* Session Fee: [%7.2f]", SessionFee);
  5969. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  5970. LOG_INFO("********* Total Cost: [%7.2f]", TotalCost);
  5971. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  5972. }
  5973. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  5974. {
  5975. TotalCost = ShellFinalCostParsing(strCost);
  5976. receiptLength = GetShellReceiptInfo(strCost, receiptString);
  5977. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  5978. LOG_INFO("********* Total Cost: [%7.2f]", TotalCost);
  5979. LOG_INFO("********* Receipt: [%s]", receiptString);
  5980. }
  5981. else
  5982. {
  5983. TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  5984. OccupancyFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  5985. AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  5986. CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  5987. FarewellCost = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  5988. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  5989. {
  5990. sprintf(receiptString, "%s%d",
  5991. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  5992. ShmChargerInfo->UserTransaction[gunIndex].TransactionId);
  5993. receiptLength = strlen(receiptString);
  5994. }
  5995. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  5996. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  5997. LOG_INFO("*********** Total Cost: [%7.2f]", TotalCost);
  5998. LOG_INFO("*********** Account Balance: [%7.2f]", AccountBalance);
  5999. LOG_INFO("*********** Coupon Point: [%7.2f]", CostDiscount);
  6000. LOG_INFO("*********** Remain Amount: [%7.2f]", FarewellCost);
  6001. LOG_INFO("*********** Receipt: [%s]", receiptString);
  6002. }
  6003. #if 0
  6004. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6005. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6006. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6007. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6008. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6009. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6010. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6011. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6012. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6013. {
  6014. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6015. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6016. }
  6017. #endif
  6018. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6019. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6020. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = CostDiscount;
  6021. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6022. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6023. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = FarewellCost;
  6024. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6025. if(receiptLength > 0)
  6026. {
  6027. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], receiptString);
  6028. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6029. }
  6030. }
  6031. void FinalCostHandler(void)
  6032. {
  6033. char strCost[256];
  6034. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6035. {
  6036. Get_Ocpp_FinalCost(i, strCost);
  6037. if(strlen((char *)strCost) > 0)
  6038. {
  6039. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6040. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_ALARM)
  6041. {
  6042. FinalCostParsing(i, (char *)strCost);
  6043. }
  6044. Clean_Ocpp_FinalCost(i);
  6045. }
  6046. }
  6047. }
  6048. char MyTimeOffsetString[501];
  6049. void TimeOffsetHandler(void)
  6050. {
  6051. char timeOffset[501];
  6052. Get_Ocpp_TimeOffset((char *)timeOffset);
  6053. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6054. {
  6055. BOOL trigger = FALSE;
  6056. int offset = 0;
  6057. offset = TimeOffsetKeyParsing(timeOffset);
  6058. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  6059. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  6060. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  6061. #if 0
  6062. time_t timep;
  6063. struct tm *tm;
  6064. time(&timep);
  6065. tm = localtime(&timep);
  6066. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  6067. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6068. timep += (offset * 60);
  6069. tm = localtime(&timep);
  6070. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  6071. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6072. #endif
  6073. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6074. {
  6075. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6076. {
  6077. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  6078. {
  6079. trigger = TRUE;
  6080. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  6081. }
  6082. }
  6083. }
  6084. if(trigger)
  6085. {
  6086. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  6087. }
  6088. strcpy(MyTimeOffsetString, timeOffset);
  6089. }
  6090. }
  6091. struct timespec _Cost_Price_time;
  6092. void CheckOcppCostAndPrice(void)
  6093. {
  6094. // 5s for reboot request timeout
  6095. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  6096. {
  6097. GetClockTime(&_Cost_Price_time);
  6098. DefaultPriceHandler();
  6099. UserPriceHandler();
  6100. RunningCostHandler();
  6101. FinalCostHandler();
  6102. TimeOffsetHandler();
  6103. }
  6104. }
  6105. void CheckRemoteStartNoIdRequest(void)
  6106. {
  6107. if(Is_Ocpp_RemoteStartWaitReq())
  6108. {
  6109. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6110. {
  6111. LOG_INFO("Ocpp remote start without connector id is trigger");
  6112. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  6113. AnnounceRemoteStartNoID();
  6114. GetClockTime(&_RemoteStartNoId_Time);
  6115. }
  6116. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6117. {
  6118. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  6119. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  6120. {
  6121. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6122. }
  6123. }
  6124. }
  6125. else
  6126. {
  6127. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6128. {
  6129. LOG_INFO("Ocpp remote start without connector id is expired");
  6130. }
  6131. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6132. }
  6133. }
  6134. void CheckOcppPageChangeRequest(void)
  6135. {
  6136. int gunId = 0, pageIndex = 0;
  6137. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  6138. {
  6139. Clean_Ocpp_LcdOverideReq();
  6140. switch(pageIndex)
  6141. {
  6142. case _OCPP_Page_LineStatus_1:
  6143. case _OCPP_Page_LineStatus_2:
  6144. case _OCPP_Page_LineStatus_3:
  6145. case _OCPP_Page_LineStatus_4:
  6146. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  6147. AnnounceLineStatusCode(gunId - 1, pageIndex);
  6148. break;
  6149. default:
  6150. LOG_INFO("Gun %d Ocpp page change [Unknown][%d]", gunId, pageIndex);
  6151. break;
  6152. }
  6153. }
  6154. }
  6155. void CheckLcmPageChangeRequest(void)
  6156. {
  6157. bool isTrigger = false;
  6158. CheckRemoteStartNoIdRequest();
  6159. CheckOcppPageChangeRequest();
  6160. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  6161. {
  6162. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  6163. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  6164. {
  6165. isTrigger = false;
  6166. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  6167. {
  6168. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  6169. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  6170. {
  6171. isTrigger = true;
  6172. break;
  6173. }
  6174. }
  6175. if(isTrigger)
  6176. {
  6177. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6178. }
  6179. else
  6180. {
  6181. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6182. }
  6183. }
  6184. }
  6185. }
  6186. void CheckStationInfo(void)
  6187. {
  6188. int station_id = 0;
  6189. char station_name[64];
  6190. if(Is_Ocpp_ChargerStationReq())
  6191. {
  6192. memset(station_name, 0x00, sizeof(station_name));
  6193. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  6194. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  6195. ShmChargerInfo->StationInfo.StationID != station_id)
  6196. {
  6197. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  6198. ShmChargerInfo->StationInfo.StationID = station_id;
  6199. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  6200. AnnounceStationInfo();
  6201. }
  6202. Clean_Ocpp_ChargerStationReq();
  6203. }
  6204. }
  6205. void CheckWeatherInfo(void)
  6206. {
  6207. int weatherID = 0;
  6208. float temperature = 0;
  6209. char strWeather[16];
  6210. char *str_weather_type[] = {STR_WEATHER_NONE, STR_WEATHER_SUNNY, STR_WEATHER_CLOUDY, STR_WEATHER_RAIN, STR_WEATHER_THUNDERSTORM, STR_WEATHER_SNOW, STR_WEATHER_MIST};
  6211. if(Is_Ocpp_WeatherReq())
  6212. {
  6213. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  6214. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  6215. temperature != ShmChargerInfo->StationInfo.Temperature)
  6216. {
  6217. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  6218. ShmChargerInfo->StationInfo.Temperature = temperature;
  6219. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  6220. {
  6221. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  6222. }
  6223. else
  6224. {
  6225. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  6226. }
  6227. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  6228. AnnounceStationInfo();
  6229. }
  6230. Clean_Ocpp_WeatherReq();
  6231. }
  6232. }
  6233. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  6234. void CheckDeductResultRequest(void)
  6235. {
  6236. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  6237. char cardNo[21];
  6238. char appNo[10];
  6239. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6240. {
  6241. if(Is_DeductResultReq(i))
  6242. {
  6243. if(!_WriteDeductBusy[i])
  6244. {
  6245. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  6246. {
  6247. _WriteDeductBusy[i] = true;
  6248. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  6249. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  6250. //Set_Ocpp_DeductVemData("");
  6251. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  6252. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  6253. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  6254. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  6255. {
  6256. memset(cardNo, 0x00, sizeof(cardNo));
  6257. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  6258. memset(appNo, 0x00, sizeof(appNo));
  6259. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  6260. Set_Ocpp_ReportCreditDeductReq();
  6261. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  6262. i + 1,
  6263. ShmChargerInfo->DeductResultReq[i].TransactionId,
  6264. cardNo,
  6265. appNo,
  6266. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  6267. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  6268. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  6269. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  6270. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_Pass)
  6271. {
  6272. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  6273. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  6274. }
  6275. }
  6276. }
  6277. }
  6278. else
  6279. {
  6280. if(!Is_Ocpp_ReportCreditDeductReq())
  6281. {
  6282. Clean_DeductInfo(i);
  6283. _WriteDeductBusy[i] = false;
  6284. }
  6285. }
  6286. }
  6287. else
  6288. {
  6289. _WriteDeductBusy[i] = false;
  6290. }
  6291. }
  6292. }
  6293. bool _WriteReDeductBusy = false;
  6294. void CheckReDeductRequest(void)
  6295. {
  6296. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  6297. char cardNo[21];
  6298. char appNo[10];
  6299. if(ShmChargerInfo->ReDeductReq.DeductReq)
  6300. {
  6301. if(!_WriteReDeductBusy)
  6302. {
  6303. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  6304. {
  6305. _WriteReDeductBusy = true;
  6306. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  6307. {
  6308. memset(cardNo, 0x00, sizeof(cardNo));
  6309. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  6310. memset(appNo, 0x00, sizeof(appNo));
  6311. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  6312. Set_Ocpp_ReportCreditDeductReq();
  6313. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  6314. ShmChargerInfo->ReDeductReq.TransactionId,
  6315. cardNo,
  6316. appNo,
  6317. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  6318. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  6319. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  6320. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  6321. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  6322. {
  6323. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  6324. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  6325. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  6326. }
  6327. }
  6328. }
  6329. }
  6330. else
  6331. {
  6332. if(!Is_Ocpp_ReportCreditDeductReq())
  6333. {
  6334. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  6335. _WriteReDeductBusy = false;
  6336. }
  6337. }
  6338. }
  6339. else
  6340. {
  6341. _WriteReDeductBusy = false;
  6342. }
  6343. }
  6344. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  6345. void CheckReaderStatusRequest(void)
  6346. {
  6347. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  6348. char cardNo[21];
  6349. char appNo[10];
  6350. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6351. {
  6352. if(Is_ReaderStatusReq(i))
  6353. {
  6354. if(!_WriteReaderStatusBusy[i])
  6355. {
  6356. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  6357. {
  6358. _WriteReaderStatusBusy[i] = true;
  6359. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  6360. {
  6361. memset(cardNo, 0x00, sizeof(cardNo));
  6362. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  6363. memset(appNo, 0x00, sizeof(appNo));
  6364. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  6365. Set_Ocpp_TcciReaderStatusReq(i);
  6366. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  6367. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  6368. }
  6369. }
  6370. }
  6371. else
  6372. {
  6373. if(!Is_Ocpp_TcciReaderStatusReq(i))
  6374. {
  6375. Clean_ReaderStatusInfo(i);
  6376. _WriteReaderStatusBusy[i] = false;
  6377. }
  6378. }
  6379. }
  6380. else
  6381. {
  6382. _WriteReaderStatusBusy[i] = false;
  6383. }
  6384. }
  6385. }
  6386. //===============================================
  6387. // OCPP
  6388. //===============================================
  6389. void CheckOcppStatus()
  6390. {
  6391. if (Is_Ocpp_BootNotification())
  6392. {
  6393. Clean_Ocpp_BootNotification();
  6394. LOG_INFO("*********************OCPP Boot Notification****************************");
  6395. }
  6396. if (Is_Ocpp_ResetReq())
  6397. {
  6398. bool canReset = true;
  6399. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  6400. {
  6401. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6402. {
  6403. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  6404. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  6405. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  6406. {
  6407. canReset = false;
  6408. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  6409. //{
  6410. // ChargingTerminalProcess(_index);
  6411. //}
  6412. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  6413. {
  6414. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  6415. }
  6416. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  6417. {
  6418. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  6419. }
  6420. }
  6421. }
  6422. }
  6423. if(canReset)
  6424. {
  6425. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  6426. {
  6427. //trigger dispenser to hardware reboot
  6428. TriggerDispenserHardwareReboot();
  6429. if(IsDispenserHardwareRebootResponsed())
  6430. {
  6431. Clean_Ocpp_ResetReq();
  6432. LOG_INFO("****** Hard Reboot ******");
  6433. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  6434. {
  6435. setChargerMode(i, MODE_MAINTAIN);
  6436. }
  6437. sleep(3);
  6438. system("reboot -f");
  6439. }
  6440. }
  6441. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  6442. {
  6443. //trigger dispenser to software reset
  6444. TriggerDispenserSoftwareReset();
  6445. if(IsDispenserSoftwareResetResponsed())
  6446. {
  6447. Clean_Ocpp_ResetReq();
  6448. LOG_INFO("****** Soft Reboot ******");
  6449. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  6450. {
  6451. setChargerMode(i, MODE_MAINTAIN);
  6452. }
  6453. sleep(3);
  6454. Kill_Ocpp_Service();
  6455. KillAllTask();
  6456. TryCloseWatchdog();
  6457. system("/usr/bin/run_evse_restart.sh");
  6458. }
  6459. }
  6460. }
  6461. }
  6462. CheckOcppCostAndPrice();
  6463. CheckLcmPageChangeRequest();
  6464. CheckStationInfo();
  6465. CheckWeatherInfo();
  6466. CheckDeductResultRequest();
  6467. CheckReDeductRequest();
  6468. CheckReaderStatusRequest();
  6469. }
  6470. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  6471. {
  6472. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  6473. {
  6474. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  6475. {
  6476. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  6477. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  6478. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  6479. }
  6480. }
  6481. }
  6482. void UpdateChargingTimestamp(char *timestamp)
  6483. {
  6484. struct timeb SeqEndTime;
  6485. struct tm *tm;
  6486. ftime(&SeqEndTime);
  6487. SeqEndTime.time = time(NULL);
  6488. tm = localtime(&SeqEndTime.time);
  6489. sprintf(timestamp, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6490. }
  6491. void OcppStartTransaction(byte gunIndex)
  6492. {
  6493. // clean deduct info
  6494. Clean_DeductInfo(gunIndex);
  6495. // set deduct user id
  6496. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  6497. // initial start & stop datetime
  6498. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  6499. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  6500. Update_PowerConsumption(gunIndex);
  6501. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  6502. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  6503. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  6504. Clean_Ocpp_StartTransactionConf(gunIndex);
  6505. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  6506. Set_Ocpp_StartTransactionReq(gunIndex);
  6507. // set _triggerTransaction
  6508. _triggerTransaction[gunIndex] = true;
  6509. }
  6510. void OcppStopTransaction(byte gunIndex)
  6511. {
  6512. if(!_triggerTransaction[gunIndex])
  6513. {
  6514. return;
  6515. }
  6516. Update_PowerConsumption(gunIndex);
  6517. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  6518. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  6519. chargingInfo[gunIndex]->Index + 1,
  6520. chargingInfo[gunIndex]->StartUserId,
  6521. chargingInfo[gunIndex]->EvBatterySoc,
  6522. chargingInfo[gunIndex]->PowerConsumption);
  6523. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  6524. Set_Ocpp_StopTransactionReq(gunIndex);
  6525. // clean _triggerTransaction
  6526. _triggerTransaction[gunIndex] = false;
  6527. }
  6528. bool Is_DispenserStop(byte gunIndex)
  6529. {
  6530. bool _isStop = false;
  6531. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  6532. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  6533. {
  6534. _isStop = true;
  6535. }
  6536. return _isStop;
  6537. }
  6538. void ChkOcppStatus(byte gunIndex)
  6539. {
  6540. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  6541. if(Is_Ocpp_ReserveNowReq(gunIndex))
  6542. {
  6543. Clean_Ocpp_ReserveNowReq(gunIndex);
  6544. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  6545. {
  6546. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  6547. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  6548. setChargerMode(gunIndex, MODE_RESERVATION);
  6549. }
  6550. Set_Ocpp_ReserveNowConf(gunIndex);
  6551. }
  6552. if(Is_Ocpp_CancelReservationReq(gunIndex))
  6553. {
  6554. Clean_Ocpp_CancelReservationReq(gunIndex);
  6555. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  6556. {
  6557. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  6558. chargingInfo[gunIndex]->ReservationId = -1;
  6559. setChargerMode(gunIndex, MODE_IDLE);
  6560. }
  6561. Set_Ocpp_CancelReservationConf(gunIndex);
  6562. }
  6563. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  6564. {
  6565. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  6566. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  6567. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  6568. {
  6569. if (isDb_ready)
  6570. DB_Update_Operactive(localDb, gunIndex, true);
  6571. chargingInfo[gunIndex]->IsAvailable = YES;
  6572. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  6573. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  6574. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  6575. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  6576. {
  6577. setChargerMode(gunIndex, MODE_IDLE);
  6578. }
  6579. }
  6580. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  6581. {
  6582. if (isDb_ready)
  6583. DB_Update_Operactive(localDb, gunIndex, false);
  6584. chargingInfo[gunIndex]->IsAvailable = NO;
  6585. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  6586. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  6587. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  6588. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  6589. {
  6590. setChargerMode(gunIndex, MODE_MAINTAIN);
  6591. }
  6592. }
  6593. }
  6594. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  6595. {
  6596. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  6597. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  6598. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  6599. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  6600. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  6601. {
  6602. // 充電中,需停止充電
  6603. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  6604. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  6605. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  6606. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  6607. }
  6608. Set_Ocpp_UnlockConnectorConf(gunIndex);
  6609. }
  6610. if (Is_Ocpp_RemoteStopReq(gunIndex))
  6611. {
  6612. Clean_Ocpp_RemoteStopReq(gunIndex);
  6613. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  6614. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  6615. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  6616. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  6617. {
  6618. Set_Ocpp_StopReason(gunIndex, "Remote");
  6619. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  6620. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  6621. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  6622. }
  6623. }
  6624. if(Is_Ocpp_StartTransactionConf(gunIndex))
  6625. {
  6626. char acceptId[128] = {0};
  6627. char status[32] = {0};
  6628. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  6629. Get_Ocpp_TransactionId(gunIndex, acceptId);
  6630. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  6631. Set_UserTransactionId(gunIndex, atoi(acceptId));
  6632. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  6633. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  6634. {
  6635. if(Is_Ocpp_StopOnInvalidId_Enable())
  6636. {
  6637. if(Is_Invalid_StartTransactionId(gunIndex))
  6638. {
  6639. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  6640. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  6641. }
  6642. }
  6643. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  6644. gunIndex + 1, acceptId,
  6645. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  6646. }
  6647. Clean_Ocpp_StartTransactionConf(gunIndex);
  6648. }
  6649. }
  6650. bool CheckBackendChargingTimeout(byte gunIndex)
  6651. {
  6652. bool result = false;
  6653. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  6654. {
  6655. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6656. {
  6657. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  6658. result = true;
  6659. }
  6660. }
  6661. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  6662. {
  6663. // 隨插即充電的要看 offline
  6664. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  6665. {
  6666. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  6667. result = true;
  6668. }
  6669. }
  6670. return result;
  6671. }
  6672. bool CheckBackendChargingEnergy(byte gunIndex)
  6673. {
  6674. bool result = false;
  6675. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  6676. {
  6677. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6678. {
  6679. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  6680. result = true;
  6681. }
  6682. }
  6683. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  6684. {
  6685. // 隨插即充電的要看 offline
  6686. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  6687. {
  6688. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  6689. result = true;
  6690. }
  6691. }
  6692. return result;
  6693. }
  6694. bool CheckSessionTargetStop(byte gunIndex)
  6695. {
  6696. bool stop = false;
  6697. struct StructSessionTarget target;
  6698. Get_Ocpp_SessionTarget(gunIndex, &target);
  6699. if(target.targetDuration > 0)
  6700. {
  6701. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  6702. {
  6703. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  6704. gunIndex + 1,
  6705. chargingInfo[gunIndex]->PresentChargedDuration,
  6706. target.targetDuration);
  6707. stop = true;
  6708. }
  6709. }
  6710. if(target.targetSoc > 0)
  6711. {
  6712. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  6713. {
  6714. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  6715. gunIndex + 1,
  6716. chargingInfo[gunIndex]->EvBatterySoc,
  6717. target.targetSoc);
  6718. stop = true;
  6719. }
  6720. }
  6721. if(target.targetEnergy > 0)
  6722. {
  6723. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  6724. {
  6725. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  6726. gunIndex + 1,
  6727. chargingInfo[gunIndex]->PresentChargedEnergy,
  6728. target.targetEnergy);
  6729. stop = true;
  6730. }
  6731. }
  6732. if(stop)
  6733. {
  6734. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  6735. }
  6736. return stop;
  6737. }
  6738. void InformOcppErrOccur(byte codeType)
  6739. {
  6740. char _error[25];
  6741. switch(codeType)
  6742. {
  6743. case 4: strcpy(_error, "InternalError"); break;
  6744. case 6: strcpy(_error, "NoError"); break;
  6745. case 7: strcpy(_error, "OtherError"); break;
  6746. case 13: strcpy(_error, "UnderVoltage"); break;
  6747. case 14: strcpy(_error, "OverVoltage"); break;
  6748. }
  6749. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  6750. {
  6751. SetOcppErrorCode(gun_index, _error);
  6752. }
  6753. }
  6754. void SetOcppErrorCode(unsigned char _index, char *errCode)
  6755. {
  6756. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  6757. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  6758. }
  6759. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  6760. {
  6761. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  6762. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  6763. }
  6764. //===============================================
  6765. // SQLite3 related routine
  6766. //===============================================
  6767. int NetworkDB_Open(sqlite3 *db)
  6768. {
  6769. int result = PASS;
  6770. char* errMsg = NULL;
  6771. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  6772. "idx integer primary key AUTOINCREMENT, "
  6773. "occurDatetime text NOT NULL, "
  6774. "isInternet text NOT NULL, "
  6775. "isOcppConnected text NOT NULL, "
  6776. "isEth0Internet text NOT NULL, "
  6777. "isMlan0Internet text NOT NULL, "
  6778. "isPpp0Internet text NOT NULL, "
  6779. "rssiMlan0 text NOT NULL, "
  6780. "rssiPpp0 text NOT NULL"
  6781. ");";
  6782. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  6783. if(sqlite3_open(NETWORK_DB_FILE, &db))
  6784. {
  6785. result = FAIL;
  6786. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  6787. sqlite3_close(db);
  6788. }
  6789. else
  6790. {
  6791. LOG_INFO("Local network status record database open successfully.");
  6792. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6793. {
  6794. result = FAIL;
  6795. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  6796. }
  6797. else
  6798. {
  6799. LOG_INFO("Opened local network status record table successfully");
  6800. }
  6801. sqlite3_close(db);
  6802. }
  6803. return result;
  6804. }
  6805. int DB_Open(sqlite3 *db)
  6806. {
  6807. int result = PASS;
  6808. char* errMsg = NULL;
  6809. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  6810. "idx integer primary key AUTOINCREMENT, "
  6811. "reservationId text, "
  6812. "transactionId text, "
  6813. "startMethod text, "
  6814. "userId text, "
  6815. "dateTimeStart text, "
  6816. "dateTimeStop text,"
  6817. "socStart text, "
  6818. "socStop text, "
  6819. "chargeEnergy text, "
  6820. "stopReason text"
  6821. ");";
  6822. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  6823. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6824. "`IsAvailable` TEXT NOT NULL, "
  6825. "`connector` INTEGER NOT NULL, "
  6826. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  6827. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  6828. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6829. "`occurDatetime` TEXT NOT NULL, "
  6830. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  6831. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  6832. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6833. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  6834. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  6835. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6836. "`connector` INTEGER NOT NULL, "
  6837. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6838. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  6839. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6840. "`connector` INTEGER NOT NULL, "
  6841. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6842. if(sqlite3_open(DB_FILE, &db))
  6843. {
  6844. result = FAIL;
  6845. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  6846. sqlite3_close(db);
  6847. }
  6848. else
  6849. {
  6850. LOG_INFO( "Local charging record database open successfully.");
  6851. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6852. {
  6853. result = FAIL;
  6854. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  6855. }
  6856. else
  6857. {
  6858. LOG_INFO( "Opened local charging record table successfully");
  6859. }
  6860. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  6861. {
  6862. result = FAIL;
  6863. LOG_ERROR( "Create local config table error message: %s", errMsg);
  6864. }
  6865. else
  6866. {
  6867. LOG_INFO( "Opened local config table successfully");
  6868. }
  6869. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6870. {
  6871. result = FAIL;
  6872. LOG_ERROR( "Create local record table error message: %s", errMsg);
  6873. }
  6874. else
  6875. {
  6876. LOG_INFO( "Opened local record table successfully");
  6877. }
  6878. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  6879. {
  6880. result = FAIL;
  6881. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  6882. }
  6883. else
  6884. {
  6885. LOG_INFO( "Opened reboot record table successfully");
  6886. }
  6887. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  6888. {
  6889. result = FAIL;
  6890. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  6891. }
  6892. else
  6893. {
  6894. LOG_INFO( "Opened power consumption record table successfully");
  6895. }
  6896. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  6897. {
  6898. result = FAIL;
  6899. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  6900. }
  6901. else
  6902. {
  6903. LOG_INFO( "Opened gun consumption record table successfully");
  6904. }
  6905. sqlite3_close(db);
  6906. }
  6907. return result;
  6908. }
  6909. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6910. {
  6911. int result = PASS;
  6912. char* errMsg = NULL;
  6913. char insertSql[1024];
  6914. char transactionId[128];
  6915. char reason[32];
  6916. Get_Ocpp_TransactionId(gun_index, transactionId);
  6917. Get_Ocpp_StopReason(gun_index, reason);
  6918. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6919. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6920. chargingInfo[gun_index]->ReservationId,
  6921. transactionId,
  6922. chargingInfo[gun_index]->StartMethod,
  6923. chargingInfo[gun_index]->StartUserId,
  6924. chargingInfo[gun_index]->StartDateTime,
  6925. chargingInfo[gun_index]->StopDateTime,
  6926. chargingInfo[gun_index]->EvBatteryStartSoc,
  6927. chargingInfo[gun_index]->EvBatterySoc,
  6928. chargingInfo[gun_index]->PresentChargedEnergy,
  6929. reason);
  6930. if(sqlite3_open(DB_FILE, &db))
  6931. {
  6932. result = FAIL;
  6933. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  6934. sqlite3_close(db);
  6935. }
  6936. else
  6937. {
  6938. //LOG_INFO( "Local charging record database open successfully.");
  6939. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6940. {
  6941. result = FAIL;
  6942. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  6943. }
  6944. else
  6945. {
  6946. //LOG_INFO("Insert local charging record successfully");
  6947. }
  6948. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6949. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6950. {
  6951. result = FAIL;
  6952. LOG_ERROR("delete local charging error message: %s", errMsg);
  6953. }
  6954. else
  6955. {
  6956. //LOG_INFO("delete local charging record successfully");
  6957. }
  6958. sqlite3_close(db);
  6959. }
  6960. return result;
  6961. }
  6962. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6963. {
  6964. uint8_t result = false;
  6965. char* errMsg = NULL;
  6966. char sqlStr[1024];
  6967. srand(time(NULL));
  6968. if(sqlite3_open(DB_FILE, &db))
  6969. {
  6970. result = FAIL;
  6971. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  6972. sqlite3_close(db);
  6973. }
  6974. else
  6975. {
  6976. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  6977. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  6978. LOG_INFO("sqlStr= %s", sqlStr);
  6979. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6980. {
  6981. result = FAIL;
  6982. LOG_INFO( "update config error message: %s", errMsg);
  6983. }
  6984. else
  6985. {
  6986. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  6987. }
  6988. sqlite3_close(db);
  6989. }
  6990. return result;
  6991. }
  6992. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6993. {
  6994. uint8_t result = true;
  6995. char* errMsg = NULL;
  6996. char sqlStr[1024];
  6997. char **rs;
  6998. int rows, cols;
  6999. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  7000. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  7001. if(sqlite3_open(DB_FILE, &db))
  7002. {
  7003. result = FAIL;
  7004. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  7005. sqlite3_close(db);
  7006. }
  7007. else
  7008. {
  7009. LOG_INFO( "Local config query database open successfully.");
  7010. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  7011. if(rows>0)
  7012. {
  7013. for(int idxRow=1;idxRow<=rows;idxRow++)
  7014. {
  7015. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  7016. {
  7017. result = false;
  7018. }
  7019. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  7020. }
  7021. }
  7022. else
  7023. {
  7024. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  7025. }
  7026. sqlite3_free_table(rs);
  7027. sqlite3_close(db);
  7028. }
  7029. return result;
  7030. }
  7031. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  7032. {
  7033. char* errMsg = NULL;
  7034. char sqlStr[1024];
  7035. srand(time(NULL));
  7036. if(sqlite3_open(DB_FILE, &db))
  7037. {
  7038. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  7039. sqlite3_close(db);
  7040. }
  7041. else
  7042. {
  7043. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  7044. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  7045. {
  7046. LOG_ERROR("update power_consumption error message: %s", errMsg);
  7047. }
  7048. else
  7049. {
  7050. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  7051. }
  7052. sqlite3_close(db);
  7053. }
  7054. }
  7055. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  7056. {
  7057. char* errMsg = NULL;
  7058. char sqlStr[1024];
  7059. srand(time(NULL));
  7060. if(sqlite3_open(DB_FILE, &db))
  7061. {
  7062. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  7063. sqlite3_close(db);
  7064. }
  7065. else
  7066. {
  7067. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  7068. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  7069. {
  7070. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  7071. }
  7072. else
  7073. {
  7074. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  7075. }
  7076. sqlite3_close(db);
  7077. }
  7078. }
  7079. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  7080. {
  7081. float result = 0;
  7082. char* errMsg = NULL;
  7083. char sqlStr[1024];
  7084. char **rs;
  7085. int rows, cols;
  7086. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  7087. if (sqlite3_open(DB_FILE, &db))
  7088. {
  7089. result = FAIL;
  7090. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  7091. sqlite3_close(db);
  7092. }
  7093. else
  7094. {
  7095. //LOG_INFO("Local config query database open successfully.");
  7096. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  7097. if (rows > 0)
  7098. {
  7099. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7100. {
  7101. result = atof(rs[(idxRow * cols) + 2]);
  7102. }
  7103. }
  7104. else
  7105. {
  7106. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  7107. }
  7108. sqlite3_free_table(rs);
  7109. sqlite3_close(db);
  7110. }
  7111. return result;
  7112. }
  7113. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  7114. {
  7115. float result = 0;
  7116. char* errMsg = NULL;
  7117. char sqlStr[1024];
  7118. char **rs;
  7119. int rows, cols;
  7120. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  7121. if (sqlite3_open(DB_FILE, &db))
  7122. {
  7123. result = FAIL;
  7124. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  7125. sqlite3_close(db);
  7126. }
  7127. else
  7128. {
  7129. //LOG_INFO("Local config query database open successfully.");
  7130. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  7131. if (rows > 0)
  7132. {
  7133. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7134. {
  7135. result = atof(rs[(idxRow * cols) + 2]);
  7136. }
  7137. }
  7138. else
  7139. {
  7140. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  7141. result = -1;
  7142. }
  7143. sqlite3_free_table(rs);
  7144. sqlite3_close(db);
  7145. }
  7146. return result;
  7147. }
  7148. float Get_PowerConsumption(uint8_t gun_index)
  7149. {
  7150. float value = 0;
  7151. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  7152. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  7153. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  7154. {
  7155. if(!_useGunMeterValue[gun_index])
  7156. {
  7157. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  7158. }
  7159. _useGunMeterValue[gun_index] = true;
  7160. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  7161. }
  7162. else
  7163. {
  7164. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  7165. }
  7166. return value;
  7167. }
  7168. void Update_PowerConsumption(uint8_t gun_index)
  7169. {
  7170. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  7171. }
  7172. void CheckInProgress(uint8_t gun_index)
  7173. {
  7174. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  7175. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  7176. {
  7177. if(chargingInfo[gun_index]->InProgress == NO)
  7178. {
  7179. LOG_INFO("Gun %d In Progress", gun_index + 1);
  7180. }
  7181. chargingInfo[gun_index]->InProgress = YES;
  7182. }
  7183. else
  7184. {
  7185. if(chargingInfo[gun_index]->InProgress == YES)
  7186. {
  7187. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  7188. }
  7189. chargingInfo[gun_index]->InProgress = NO;
  7190. }
  7191. }
  7192. void CheckDebugMode(uint8_t gun_index)
  7193. {
  7194. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  7195. {
  7196. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  7197. {
  7198. setChargerMode(gun_index, MODE_DEBUG);
  7199. }
  7200. }
  7201. }
  7202. int DB_Reboot_Record(sqlite3 *db)
  7203. {
  7204. int result = PASS;
  7205. char* errMsg = NULL;
  7206. char insertSql[256];
  7207. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  7208. if(sqlite3_open(DB_FILE, &db))
  7209. {
  7210. result = FAIL;
  7211. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  7212. sqlite3_close(db);
  7213. }
  7214. else
  7215. {
  7216. LOG_INFO("Local charging record database open successfully.");
  7217. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7218. {
  7219. result = FAIL;
  7220. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  7221. }
  7222. else
  7223. {
  7224. //LOG_INFO("Insert reboot record successfully");
  7225. }
  7226. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  7227. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7228. {
  7229. result = FAIL;
  7230. LOG_ERROR("delete reboot record error message: %s", errMsg);
  7231. }
  7232. else
  7233. {
  7234. //LOG_INFO("delete reboot record successfully");
  7235. }
  7236. sqlite3_close(db);
  7237. }
  7238. return result;
  7239. }
  7240. //===============================================
  7241. // Config process
  7242. //===============================================
  7243. void AddPlugInTimes(byte gunIndex)
  7244. {
  7245. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  7246. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  7247. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  7248. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  7249. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  7250. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  7251. }
  7252. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  7253. {
  7254. char cmdBuf[32];
  7255. struct timeb csuTime;
  7256. struct tm *tmCSU;
  7257. ftime(&csuTime);
  7258. tmCSU = localtime(&csuTime.time);
  7259. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  7260. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7261. tmCSU->tm_sec);
  7262. if (isStart)
  7263. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  7264. else
  7265. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  7266. }
  7267. void zipLogFiles()
  7268. {
  7269. const char* logPath = "/Storage/SystemLog";
  7270. // 獲取目錄
  7271. DIR* pDir = opendir(logPath);
  7272. if (pDir != NULL)
  7273. {
  7274. struct timeb csuTime;
  7275. struct tm *tmCSU;
  7276. ftime(&csuTime);
  7277. tmCSU = localtime(&csuTime.time);
  7278. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  7279. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7280. // tmCSU->tm_sec);
  7281. // Read items inside the folder
  7282. struct dirent* pEntry = NULL;
  7283. while ((pEntry = readdir(pDir)) != NULL)
  7284. {
  7285. if (strcmp(pEntry->d_name, ".") != 0 &&
  7286. strcmp(pEntry->d_name, "..") != 0 &&
  7287. strncmp(pEntry->d_name, "[", 1) == 0 &&
  7288. strstr(pEntry->d_name, "tar") < 0)
  7289. {
  7290. char yearC[5];
  7291. unsigned short year = 0;
  7292. char monthC[3];
  7293. unsigned short month = 0;
  7294. yearC[4] = '\0';
  7295. strncpy(yearC, pEntry->d_name + 1, 4);
  7296. monthC[2] = '\0';
  7297. strncpy(monthC, pEntry->d_name + 6, 2);
  7298. year = atoi(yearC);
  7299. month = atoi(monthC);
  7300. if (year != 0)
  7301. {
  7302. if (year < tmCSU->tm_year + 1900 ||
  7303. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  7304. {
  7305. LOG_INFO("tar file name : %s", pEntry->d_name);
  7306. char file[256];
  7307. memset(file, 0x00, sizeof(file));
  7308. strcat(file, "tar zcvf ");
  7309. strcat(file, logPath);
  7310. strncat(file, "/", 1);
  7311. strcat(file, pEntry->d_name);
  7312. strcat(file, ".tar");
  7313. strncat(file, " ", 1);
  7314. strcat(file, logPath);
  7315. strncat(file, "/", 1);
  7316. strcat(file, pEntry->d_name);
  7317. LOG_INFO("zip = %s \n", file);
  7318. system(file);
  7319. }
  7320. }
  7321. }
  7322. }
  7323. }
  7324. // Close folder
  7325. closedir(pDir);
  7326. }
  7327. void StopProcessingLoop()
  7328. {
  7329. for (;;)
  7330. {
  7331. CheckFactoryConfigFunction();
  7332. CheckFwUpdateFunction();
  7333. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  7334. {
  7335. ChkPrimaryStatus();
  7336. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  7337. {
  7338. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  7339. KillAllTask();
  7340. sleep(3);
  7341. TryCloseWatchdog();
  7342. system("/usr/bin/run_evse_restart.sh");
  7343. return;
  7344. }
  7345. }
  7346. if(Is_Ocpp_ResetReq())
  7347. {
  7348. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7349. {
  7350. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  7351. }
  7352. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7353. {
  7354. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  7355. }
  7356. }
  7357. CheckSoftHardReset();
  7358. sleep(1);
  7359. }
  7360. }
  7361. void CreateWatchdog(void)
  7362. {
  7363. wtdFd = InitWatchDog();
  7364. if(wtdFd < 0)
  7365. {
  7366. LOG_INFO("Watchdog Initial Fail");
  7367. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  7368. }
  7369. else
  7370. {
  7371. LOG_INFO("Watchdog Initial Success");
  7372. }
  7373. }
  7374. void TryCloseWatchdog(void)
  7375. {
  7376. if(wtdFd > 0)
  7377. {
  7378. write(wtdFd, "V", 1);
  7379. close(wtdFd);
  7380. }
  7381. }
  7382. void TryFeedWatchdog(void)
  7383. {
  7384. if(wtdFd > 0)
  7385. {
  7386. write(wtdFd, "a", 1);
  7387. }
  7388. }
  7389. bool IsParallelCabinetIdle(void)
  7390. {
  7391. bool result = true;
  7392. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  7393. {
  7394. result = false;
  7395. }
  7396. return result;
  7397. }
  7398. bool IsStandbyEnable(void)
  7399. {
  7400. bool result = true;
  7401. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  7402. {
  7403. time_t timep;
  7404. struct tm *tm;
  7405. time(&timep);
  7406. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  7407. tm = localtime(&timep);
  7408. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  7409. {
  7410. if(!_isStandbyEnable)
  7411. {
  7412. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  7413. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  7414. }
  7415. _isStandbyEnable = true;
  7416. result = true;
  7417. }
  7418. else
  7419. {
  7420. if(_isStandbyEnable)
  7421. {
  7422. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  7423. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  7424. }
  7425. _isStandbyEnable = false;
  7426. result = false;
  7427. }
  7428. }
  7429. return result;
  7430. }
  7431. bool IsConnectorWholeIdle(void)
  7432. {
  7433. bool result = true;
  7434. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7435. {
  7436. if(chargingInfo[count]->SystemStatus == S_IDLE && chargingInfo[count]->ConnectorPlugIn)
  7437. {
  7438. result = false;
  7439. break;
  7440. }
  7441. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  7442. chargingInfo[count]->SystemStatus != S_RESERVATION &&
  7443. chargingInfo[count]->SystemStatus != S_FAULT &&
  7444. chargingInfo[count]->SystemStatus != S_MAINTAIN)
  7445. {
  7446. result = false;
  7447. break;
  7448. }
  7449. }
  7450. return result;
  7451. }
  7452. void ClearAlarmCodeWhenAcOff()
  7453. {
  7454. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  7455. {
  7456. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  7457. }
  7458. }
  7459. //==========================================
  7460. // Check task processing
  7461. //==========================================
  7462. void CheckTask()
  7463. {
  7464. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  7465. {
  7466. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  7467. {
  7468. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  7469. {
  7470. LOG_ERROR("Module_4g not running, restart it.");
  7471. system("/root/Module_4g &");
  7472. }
  7473. }
  7474. }
  7475. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  7476. {
  7477. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  7478. {
  7479. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  7480. {
  7481. LOG_ERROR("Module_Wifi not running, restart it.");
  7482. system("/root/Module_Wifi &");
  7483. }
  7484. }
  7485. }
  7486. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  7487. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  7488. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  7489. {
  7490. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  7491. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  7492. {
  7493. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  7494. Clean_Ocpp_procDogTime();
  7495. Kill_Ocpp_Service();
  7496. }
  7497. if(!Is_Ocpp_Task_Alive())
  7498. {
  7499. LOG_ERROR("OcppBackend not running, restart it.");
  7500. Run_Ocpp_Service();
  7501. }
  7502. }
  7503. else
  7504. {
  7505. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  7506. }
  7507. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  7508. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  7509. {
  7510. if(!Is_MaintainOcpp_Task_Alive())
  7511. {
  7512. LOG_ERROR("Maintain Backend not running, restart it.");
  7513. Run_MaintainOcpp_Service();
  7514. }
  7515. }
  7516. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  7517. {
  7518. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  7519. system ("/root/Module_ProduceUtils &");
  7520. }
  7521. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  7522. {
  7523. LOG_ERROR("Module_EvComm not running, restart it.");
  7524. system ("/root/Module_EvComm &");
  7525. }
  7526. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  7527. {
  7528. LOG_ERROR("Module_InternalComm not running, restart it.");
  7529. system ("/root/Module_InternalComm &");
  7530. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  7531. }
  7532. }
  7533. void InitialDHCP()
  7534. {
  7535. char tmpbuf[256];
  7536. memset(tmpbuf,0,256);
  7537. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  7538. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  7539. system(tmpbuf);
  7540. }
  7541. void InitialDispenserDhcpServerConfig(void)
  7542. {
  7543. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  7544. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  7545. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  7546. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  7547. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  7548. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  7549. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  7550. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  7551. usleep(1000);
  7552. }
  7553. void StartDispenserDhcpServer(void)
  7554. {
  7555. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  7556. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  7557. }
  7558. //==========================================
  7559. // Check Smart Charging Profile
  7560. //==========================================
  7561. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  7562. {
  7563. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  7564. {
  7565. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  7566. {
  7567. Set_Ocpp_ChargingProfileReq(gunIndex);
  7568. }
  7569. }
  7570. }
  7571. int GetStartScheduleTime(unsigned char *time)
  7572. {
  7573. int result = -1;
  7574. struct tm tmScheduleStart;
  7575. struct timeb tbScheduleStart;
  7576. 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))
  7577. {
  7578. tmScheduleStart.tm_year -= 1900;
  7579. tmScheduleStart.tm_mon -= 1;
  7580. tbScheduleStart.time = mktime(&tmScheduleStart);
  7581. tbScheduleStart.millitm = 0;
  7582. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  7583. }
  7584. return result;
  7585. }
  7586. void Ocpp_ChargingProfile_Process(byte _index)
  7587. {
  7588. int _startCount = NO_DEFINE;
  7589. int _maxCount = 0;
  7590. float _limit = 0;
  7591. int _numberPhases = 0;
  7592. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  7593. {
  7594. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  7595. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  7596. if(_startCount < _maxCount)
  7597. {
  7598. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  7599. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  7600. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  7601. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  7602. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  7603. {
  7604. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  7605. }
  7606. else
  7607. {
  7608. chargingInfo[_index]->ChargingProfileCurrent = -1;
  7609. }
  7610. }
  7611. else
  7612. {
  7613. chargingInfo[_index]->ChargingProfilePower = -1;
  7614. chargingInfo[_index]->ChargingProfileCurrent = -1;
  7615. }
  7616. }
  7617. else
  7618. {
  7619. chargingInfo[_index]->ChargingProfilePower = -1;
  7620. chargingInfo[_index]->ChargingProfileCurrent = -1;
  7621. }
  7622. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  7623. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  7624. {
  7625. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  7626. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  7627. _index + 1, _startCount, _limit);
  7628. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  7629. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  7630. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  7631. }
  7632. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  7633. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  7634. }
  7635. void Ocpp_MaxChargingProfile_Process(void)
  7636. {
  7637. int _startCount = NO_DEFINE;
  7638. int _maxCount = 0;
  7639. float _limit = 0;
  7640. int _numberPhases = 0;
  7641. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  7642. {
  7643. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  7644. _startCount = Get_Ocpp_MaxProfileStartIndex();
  7645. if(_startCount < _maxCount)
  7646. {
  7647. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  7648. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  7649. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  7650. }
  7651. else
  7652. {
  7653. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  7654. }
  7655. }
  7656. else
  7657. {
  7658. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  7659. }
  7660. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  7661. {
  7662. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  7663. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  7664. _startCount, _limit);
  7665. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  7666. }
  7667. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  7668. }
  7669. void CheckSmartChargeProfile(byte _index)
  7670. {
  7671. if(Is_Ocpp_ChargingProfileConf(_index))
  7672. {
  7673. Clean_Ocpp_ChargingProfileConf(_index);
  7674. Ocpp_MaxChargingProfile_Process();
  7675. Ocpp_ChargingProfile_Process(_index);
  7676. }
  7677. }
  7678. void TheEndCharging(byte gun_index)
  7679. {
  7680. chargingInfo[gun_index]->isRemoteStart = NO;
  7681. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  7682. {
  7683. char receiptString[512];
  7684. sprintf(receiptString, "%s%d",
  7685. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  7686. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  7687. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  7688. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  7689. }
  7690. ChangeStartOrStopDateTime(NO, gun_index);
  7691. DB_Insert_Record(localDb, gun_index);
  7692. }
  7693. void UpdateErrorCodeToOcpp(byte index)
  7694. {
  7695. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  7696. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  7697. {
  7698. SetOcppErrorCode(index, "InternalError");
  7699. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  7700. }
  7701. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  7702. {
  7703. SetOcppErrorCode(index, "OtherError");
  7704. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  7705. }
  7706. }
  7707. void CheckMiscCommandRequirement(void)
  7708. {
  7709. BOOL NeedAnnouncement = FALSE;
  7710. unsigned char connector = 0;
  7711. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  7712. {
  7713. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7714. {
  7715. NeedAnnouncement = FALSE;
  7716. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  7717. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  7718. {
  7719. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  7720. {
  7721. NeedAnnouncement = TRUE;
  7722. }
  7723. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  7724. {
  7725. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  7726. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  7727. {
  7728. NeedAnnouncement = TRUE;
  7729. }
  7730. }
  7731. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  7732. {
  7733. if(NeedAnnouncement)
  7734. {
  7735. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  7736. }
  7737. else
  7738. {
  7739. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  7740. }
  7741. }
  7742. }
  7743. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  7744. }
  7745. }
  7746. }
  7747. void CheckDispenserVersionUpdateRequirement(void)
  7748. {
  7749. }
  7750. bool IsReadyParalleling(void)
  7751. {
  7752. bool ready = true;
  7753. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  7754. {
  7755. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  7756. {
  7757. ready = false;
  7758. break;
  7759. }
  7760. }
  7761. return ready;
  7762. }
  7763. void SetParallelingStart(void)
  7764. {
  7765. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7766. {
  7767. if(chargingInfo[i]->SystemStatus == S_IDLE)
  7768. {
  7769. setChargerMode(i, MODE_MAINTAIN);
  7770. }
  7771. }
  7772. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  7773. }
  7774. void SetParallelingDone(void)
  7775. {
  7776. unsigned short totalPow = 0;
  7777. totalPow = GetTotalParallelCabinetPower();
  7778. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  7779. {
  7780. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  7781. }
  7782. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  7783. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  7784. }
  7785. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  7786. {
  7787. unsigned char relay = 0;
  7788. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  7789. {
  7790. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7791. {
  7792. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  7793. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  7794. {
  7795. relay = OnOff == YES ? YES : NO;
  7796. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  7797. {
  7798. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  7799. }
  7800. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  7801. }
  7802. }
  7803. }
  7804. }
  7805. void SetParallelCabinetAcContactor(unsigned char OnOff)
  7806. {
  7807. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7808. {
  7809. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  7810. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  7811. {
  7812. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  7813. }
  7814. }
  7815. }
  7816. unsigned short GetTotalParallelCabinetPower(void)
  7817. {
  7818. unsigned short power = 0, slavePow = 0;
  7819. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  7820. {
  7821. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  7822. {
  7823. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  7824. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  7825. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  7826. {
  7827. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  7828. power += slavePow;
  7829. }
  7830. }
  7831. }
  7832. return power;
  7833. }
  7834. unsigned char preParallelStatus = 0;
  7835. void MCabinetParallelingProcess(void)
  7836. {
  7837. bool PsuReInitial = false;
  7838. unsigned char status = 0;
  7839. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  7840. {
  7841. if(IsReadyParalleling())
  7842. {
  7843. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7844. {
  7845. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  7846. {
  7847. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  7848. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  7849. PsuReInitial = true;
  7850. }
  7851. }
  7852. if(PsuReInitial)
  7853. {
  7854. SetParallelingStart();
  7855. }
  7856. }
  7857. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  7858. switch(status)
  7859. {
  7860. case _Parallel_None:
  7861. if(preParallelStatus != status)
  7862. {
  7863. LOG_INFO("MCabinet Parallel Status [None]");
  7864. GetClockTime(&_Parallel_Time);
  7865. }
  7866. unsigned short totalPow = 0;
  7867. totalPow = GetTotalParallelCabinetPower();
  7868. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  7869. {
  7870. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  7871. }
  7872. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  7873. break;
  7874. case _Parallel_Wait:
  7875. if(preParallelStatus != status)
  7876. {
  7877. LOG_INFO("MCabinet Parallel Status [Wait]");
  7878. SetAcContactor(OFF);
  7879. SetParallelCabinetAcContactor(OFF);
  7880. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7881. {
  7882. SetPCabinetOutputRelay(i, OFF);
  7883. }
  7884. GetClockTime(&_Parallel_Time);
  7885. }
  7886. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  7887. {
  7888. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  7889. }
  7890. break;
  7891. case _Parallel_Working:
  7892. if(preParallelStatus != status)
  7893. {
  7894. LOG_INFO("MCabinet Parallel Status [Working]");
  7895. SetAcContactor(ON);
  7896. SetParallelCabinetAcContactor(ON);
  7897. GetClockTime(&_Parallel_Time);
  7898. }
  7899. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  7900. {
  7901. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  7902. }
  7903. break;
  7904. case _Parallel_Done:
  7905. if(preParallelStatus != status)
  7906. {
  7907. LOG_INFO("MCabinet Parallel Status [Done]");
  7908. GetClockTime(&_Parallel_Time);
  7909. }
  7910. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  7911. {
  7912. SetParallelingDone();
  7913. }
  7914. break;
  7915. }
  7916. preParallelStatus = status;
  7917. }
  7918. }
  7919. void SCabinetControllProcess(void)
  7920. {
  7921. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  7922. {
  7923. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  7924. {
  7925. SetAcContactor(ON);
  7926. }
  7927. else
  7928. {
  7929. SetAcContactor(OFF);
  7930. }
  7931. unsigned short _parallelRelay = 0;
  7932. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  7933. {
  7934. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  7935. }
  7936. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  7937. {
  7938. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  7939. }
  7940. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  7941. }
  7942. }
  7943. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  7944. {
  7945. unsigned char dispenser = 0;
  7946. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  7947. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  7948. {
  7949. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  7950. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  7951. }
  7952. }
  7953. bool ConnectorRequestToCharging(unsigned char connector)
  7954. {
  7955. unsigned char role = 0;
  7956. bool done = false;
  7957. role = ShmPsuGrouping->GroupCollection[connector].Role;
  7958. switch(role)
  7959. {
  7960. case _GROLE_IDLE:
  7961. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  7962. {
  7963. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  7964. }
  7965. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  7966. break;
  7967. case _GROLE_MASTER:
  7968. done = true;
  7969. break;
  7970. case _GROLE_SLAVE:
  7971. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  7972. {
  7973. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  7974. }
  7975. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  7976. break;
  7977. case _GROLE_REQUEST_TO_CHARGING:
  7978. done = true;
  7979. break;
  7980. default:
  7981. break;
  7982. }
  7983. return done;
  7984. }
  7985. bool IsConnectorGroupingCompleted(unsigned char connector)
  7986. {
  7987. unsigned char role = 0;
  7988. bool completed = false;
  7989. role = ShmPsuGrouping->GroupCollection[connector].Role;
  7990. switch(role)
  7991. {
  7992. case _GROLE_MASTER:
  7993. completed = true;
  7994. break;
  7995. default:
  7996. break;
  7997. }
  7998. return completed;
  7999. }
  8000. void SelfTestFailReason(void)
  8001. {
  8002. char reason[50];
  8003. memset(reason, 0x00, sizeof(reason));
  8004. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  8005. {
  8006. StatusCodeCompose(reason, "042251");
  8007. }
  8008. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  8009. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  8010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  8011. {
  8012. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  8013. {
  8014. StatusCodeCompose(reason, "042280");
  8015. }
  8016. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  8017. {
  8018. StatusCodeCompose(reason, "042281");
  8019. }
  8020. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  8021. {
  8022. StatusCodeCompose(reason, "042282");
  8023. }
  8024. }
  8025. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  8026. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  8027. {
  8028. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  8029. {
  8030. StatusCodeCompose(reason, "042285");
  8031. }
  8032. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  8033. {
  8034. StatusCodeCompose(reason, "042286");
  8035. }
  8036. }
  8037. else
  8038. {
  8039. StatusCodeCompose(reason, "Self Test Fail");
  8040. }
  8041. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8042. {
  8043. SetOcppErrorCode(gun_index, "InternalError");
  8044. SetOcppVendorErrorCode(gun_index, reason);
  8045. setChargerMode(gun_index, MODE_MAINTAIN);
  8046. }
  8047. }
  8048. bool IsAvailableBackToIdle(int gun_index)
  8049. {
  8050. if(_plugInHold[gun_index] == false &&
  8051. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  8052. {
  8053. _plugInHold[gun_index] = true;
  8054. GetClockTime(&_PlugInHold_Time[gun_index]);
  8055. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  8056. }
  8057. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  8058. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  8059. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  8060. isResetStopChargeFlag(gun_index) == YES)
  8061. {
  8062. if(!_plugInHold[gun_index])
  8063. {
  8064. _plugInHold[gun_index] = true;
  8065. GetClockTime(&_PlugInHold_Time[gun_index]);
  8066. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  8067. }
  8068. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  8069. {
  8070. return true;
  8071. }
  8072. }
  8073. return false;
  8074. }
  8075. void CheckGunAvailable(void)
  8076. {
  8077. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  8078. {
  8079. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  8080. {
  8081. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  8082. {
  8083. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  8084. {
  8085. ShmChargerInfo->Control.GunAvailable[i] = YES;
  8086. }
  8087. }
  8088. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  8089. }
  8090. }
  8091. }
  8092. int presentChargedEnergyClear(unsigned char gun_index)
  8093. {
  8094. int result = FAIL;
  8095. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  8096. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  8097. result = PASS;
  8098. return result;
  8099. }
  8100. float presentChargedEnergyTotal(unsigned char gun_index)
  8101. {
  8102. float result = 0.0f;
  8103. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  8104. {
  8105. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  8106. }
  8107. return result;
  8108. }
  8109. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  8110. {
  8111. int result = FAIL;
  8112. time_t CurrentTime;
  8113. struct tm *tm;
  8114. CurrentTime = time(NULL);
  8115. tm=localtime(&CurrentTime);
  8116. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  8117. return result;
  8118. }
  8119. void ChkConnectorAction(void)
  8120. {
  8121. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  8122. {
  8123. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  8124. {
  8125. Clean_Ocpp_TcciSerialNo(i);
  8126. LOG_INFO("Gun %d Charging Canceled", i + 1);
  8127. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  8128. }
  8129. }
  8130. }
  8131. void Update_DispenserAvailability(int dispenserIndex)
  8132. {
  8133. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  8134. {
  8135. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  8136. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  8137. }
  8138. }
  8139. void InitialDataBaseValue(void)
  8140. {
  8141. // Local DB
  8142. if(DB_Open(localDb) != PASS)
  8143. {
  8144. LOG_INFO("DB_Open fail.");
  8145. isDb_ready = false;
  8146. }
  8147. else
  8148. {
  8149. isDb_ready = true;
  8150. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8151. {
  8152. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  8153. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  8154. }
  8155. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8156. {
  8157. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  8158. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  8159. }
  8160. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8161. {
  8162. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  8163. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  8164. }
  8165. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8166. {
  8167. Update_PowerConsumption(_index);
  8168. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  8169. }
  8170. DB_Reboot_Record(localDb);
  8171. }
  8172. if (NetworkDB_Open(networkDb) != PASS)
  8173. {
  8174. LOG_ERROR("NetworkDB_Open fail.");
  8175. }
  8176. }
  8177. int main(void)
  8178. {
  8179. unsigned long time = 0;
  8180. int _dispenser = 0;
  8181. _isStandbyEnable = true;
  8182. if(CreateShareMemory() == 0)
  8183. {
  8184. #ifdef SystemLogMessage
  8185. LOG_ERROR("CreatShareMemory NG");
  8186. #endif
  8187. if(ShmStatusCodeData!=NULL)
  8188. {
  8189. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  8190. }
  8191. sleep(5);
  8192. system("reboot -f");
  8193. sleep(5);
  8194. system("reboot -f");
  8195. }
  8196. LOG_INFO("\n");
  8197. LOG_INFO("===== Boot and Initialization start =====");
  8198. LOG_INFO("Main Fork PID = %d", getpid());
  8199. if (!InitialSystemDefaultConfig())
  8200. {
  8201. LOG_INFO("InitialSystemDefaultConfig NG");
  8202. StopProcessingLoop();
  8203. }
  8204. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  8205. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  8206. LOG_INFO("InitialShareMemoryInfo");
  8207. InitialShareMemoryInfo();
  8208. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  8209. if(!InitialChargerSetting())
  8210. {
  8211. isModelNameMatch = false;
  8212. }
  8213. Initialization();
  8214. InitialDataBaseValue();
  8215. LOG_INFO("Spawn all Task.");
  8216. SpawnTask();
  8217. if (!isModelNameMatch)
  8218. {
  8219. LOG_INFO("Module Name & HW info none match.");
  8220. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  8221. // Module Name 與硬體對應不正確
  8222. LOG_ERROR("Module Name & HW info none match.");
  8223. sleep(3);
  8224. KillAllTask();
  8225. StopProcessingLoop();
  8226. }
  8227. LOG_INFO("Module Name & HW info correct. Initialize.......");
  8228. CreateTimeoutFork();
  8229. SelfTestRun();
  8230. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  8231. {
  8232. sleep(1);
  8233. }
  8234. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  8235. {
  8236. sleep(1);
  8237. SelfTestFailReason();
  8238. }
  8239. else
  8240. {
  8241. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8242. {
  8243. setChargerMode(gun_index, MODE_IDLE);
  8244. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  8245. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  8246. {
  8247. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  8248. }
  8249. }
  8250. }
  8251. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  8252. {
  8253. SetAcContactor(ON);
  8254. SpawnOcppService();
  8255. SpawnMaintainOcppService();
  8256. }
  8257. else
  8258. {
  8259. InitSlaveCabinetEthernet();
  8260. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8261. {
  8262. setChargerMode(gun_index, MODE_MAINTAIN);
  8263. }
  8264. }
  8265. sleep(1);
  8266. CreateWatchdog();
  8267. // Main loop
  8268. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  8269. "****************************Main Loop**********************************" :
  8270. "********************** SelfTest Fail **********************");
  8271. GetClockTime(&_cmdMainPriority_time);
  8272. for (;;)
  8273. {
  8274. CheckGunAvailable();
  8275. CheckOcppStatus();
  8276. ChkPrimaryStatus();
  8277. ChkPsuStatus();
  8278. PsuFailureResume();
  8279. ChkConnectorAction();
  8280. if (IsConnectorWholeIdle())
  8281. {
  8282. CheckFactoryConfigFunction();
  8283. CheckFwUpdateFunction();
  8284. }
  8285. if(CheckFlashUpdate() != YES)
  8286. {
  8287. // return fork process
  8288. return 0;
  8289. }
  8290. CheckSoftHardReset();
  8291. StandbyCheck();
  8292. // 讀卡邏輯
  8293. //ScannerCardProcess();
  8294. AuthorizeTimeoutProcess();
  8295. // 當 AC 沒有搭上時,清除一些錯誤碼
  8296. ClearAlarmCodeWhenAcOff();
  8297. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  8298. {
  8299. CheckTask();
  8300. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8301. {
  8302. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  8303. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  8304. {
  8305. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  8306. {
  8307. Ocpp_ProfileReq_Cmd(_index);
  8308. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  8309. _ocppProfileChkFlag[_index] = 0;
  8310. }
  8311. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  8312. {
  8313. Ocpp_ProfileReq_Cmd(_index);
  8314. _ocppProfileChkFlag[_index] = 0;
  8315. }
  8316. else
  8317. {
  8318. _ocppProfileChkFlag[_index]++;
  8319. }
  8320. }
  8321. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  8322. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  8323. {
  8324. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  8325. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  8326. {
  8327. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  8328. }
  8329. }
  8330. }
  8331. GetClockTime(&_cmdMainPriority_time);
  8332. }
  8333. CheckMiscCommandRequirement();
  8334. CheckDispenserVersionUpdateRequirement();
  8335. MCabinetParallelingProcess();
  8336. SCabinetControllProcess();
  8337. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8338. {
  8339. //CheckGpioInStatus();
  8340. CheckErrorOccurStatus(gun_index);
  8341. ChkOcppStatus(gun_index);
  8342. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  8343. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  8344. {
  8345. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  8346. {
  8347. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  8348. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  8349. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  8350. {
  8351. _ocppProfileChkFlag[gun_index] = 12;
  8352. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  8353. }
  8354. }
  8355. }
  8356. CheckSmartChargeProfile(gun_index);
  8357. Update_PowerConsumption(gun_index);
  8358. CheckInProgress(gun_index);
  8359. CheckDebugMode(gun_index);
  8360. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  8361. switch(chargingInfo[gun_index]->SystemStatus)
  8362. {
  8363. case S_MAINTAIN:
  8364. if(isModeChange(gun_index))
  8365. {
  8366. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  8367. {
  8368. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  8369. }
  8370. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  8371. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  8372. GetClockTime(&_SystemStatus_Time[gun_index]);
  8373. }
  8374. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  8375. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  8376. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  8377. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  8378. {
  8379. break;
  8380. }
  8381. if(chargingInfo[gun_index]->IsAvailable == YES &&
  8382. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  8383. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  8384. {
  8385. if(time >= MAINTAIN_RECOVERY_TIME)
  8386. {
  8387. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  8388. setChargerMode(gun_index, MODE_IDLE);
  8389. }
  8390. }
  8391. break;
  8392. case S_RESERVATION:
  8393. if(isModeChange(gun_index))
  8394. {
  8395. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  8396. char expiryDate[128];
  8397. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  8398. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  8399. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  8400. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  8401. GetClockTime(&_SystemStatus_Time[gun_index]);
  8402. }
  8403. if(chargingInfo[gun_index]->ConnectorPlugIn)
  8404. {
  8405. SetAcContactor(ON);
  8406. }
  8407. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  8408. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  8409. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  8410. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  8411. {
  8412. UpdateErrorCodeToOcpp(gun_index);
  8413. setChargerMode(gun_index, MODE_FAULT);
  8414. continue;
  8415. }
  8416. if(Is_Ocpp_ReserveNowExpired(gun_index))
  8417. {
  8418. chargingInfo[gun_index]->ReservationId = -1;
  8419. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  8420. setChargerMode(gun_index, MODE_IDLE);
  8421. }
  8422. else
  8423. {
  8424. if(IsConnectorAuthorizeSuccess(gun_index))
  8425. {
  8426. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  8427. {
  8428. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  8429. }
  8430. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  8431. {
  8432. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  8433. }
  8434. setChargerMode(gun_index, MODE_AUTHORIZING);
  8435. }
  8436. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  8437. {
  8438. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  8439. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  8440. {
  8441. SetAcContactor(ON);
  8442. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  8443. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  8444. {
  8445. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  8446. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  8447. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  8448. {
  8449. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  8450. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  8451. }
  8452. DispenserAuthInitial(_dispenser);
  8453. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  8454. }
  8455. }
  8456. }
  8457. else
  8458. {
  8459. if(chargingInfo[gun_index]->ReservationId == -1)
  8460. {
  8461. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  8462. setChargerMode(gun_index, MODE_IDLE);
  8463. }
  8464. }
  8465. }
  8466. break;
  8467. case S_IDLE:
  8468. case S_FAULT:
  8469. {
  8470. // clean PermissionRequest flag
  8471. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  8472. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  8473. if(isModeChange(gun_index))
  8474. {
  8475. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  8476. {
  8477. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  8478. chargingInfo[gun_index]->ReservationId = -1;
  8479. _triggerTransaction[gun_index] = false;
  8480. _plugInHold[gun_index] = false;
  8481. chargingInfo[gun_index]->PresentChargedDuration = 0;
  8482. chargingInfo[gun_index]->RemainChargingDuration = 0;
  8483. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  8484. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  8485. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8486. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  8487. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  8488. Clean_Ocpp_StopReason(gun_index);
  8489. ReleaseAlarmCode(gun_index);
  8490. // clean connector information
  8491. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  8492. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  8493. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  8494. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  8495. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  8496. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  8497. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  8498. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  8499. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  8500. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  8501. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  8502. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  8503. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  8504. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  8505. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  8506. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  8507. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  8508. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  8509. chargingInfo[gun_index]->EvBatterySoc = 0;
  8510. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  8511. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  8512. // clean RemoteStart & RemoteStop flag
  8513. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  8514. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  8515. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  8516. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  8517. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  8518. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  8519. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  8520. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  8521. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  8522. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  8523. Clean_Ocpp_RunningCost(gun_index);
  8524. Clean_Ocpp_FinalCost(gun_index);
  8525. Clean_DeductInfo(gun_index);
  8526. Clean_Ocpp_TcciSerialNo(gun_index);
  8527. Clean_UserTransaction(gun_index);
  8528. Clean_UserPriceAndReceipt(gun_index);
  8529. presentChargedEnergyClear(gun_index);
  8530. chargingInfo[gun_index]->PresentChargingPower = 0;
  8531. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  8532. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  8533. chargingInfo[gun_index]->ChargingProfilePower = -1;
  8534. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  8535. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  8536. {
  8537. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  8538. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  8539. // reset dispenser availability
  8540. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  8541. }
  8542. // clean force charging info
  8543. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  8544. }
  8545. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  8546. {
  8547. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  8548. }
  8549. GetClockTime(&_SystemStatus_Time[gun_index]);
  8550. }
  8551. if (chargingInfo[gun_index]->IsAvailable == NO ||
  8552. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  8553. {
  8554. setChargerMode(gun_index, MODE_MAINTAIN);
  8555. continue;
  8556. }
  8557. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  8558. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  8559. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  8560. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  8561. {
  8562. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  8563. {
  8564. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  8565. {
  8566. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  8567. }
  8568. }
  8569. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  8570. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  8571. {
  8572. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  8573. {
  8574. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  8575. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  8576. }
  8577. }
  8578. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  8579. {
  8580. UpdateErrorCodeToOcpp(gun_index);
  8581. setChargerMode(gun_index, MODE_FAULT);
  8582. }
  8583. else
  8584. {
  8585. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  8586. {
  8587. CheckConnectorDisconnectionRecovery(gun_index);
  8588. }
  8589. }
  8590. }
  8591. else
  8592. {
  8593. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  8594. {
  8595. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  8596. if(time >= FAULT_RELEASE_TIME)
  8597. {
  8598. ReleaseAlarmCode(gun_index);
  8599. setChargerMode(gun_index, MODE_IDLE);
  8600. }
  8601. break;
  8602. }
  8603. // clean stop charge flag
  8604. chargingInfo[gun_index]->StopChargeFlag = false;
  8605. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  8606. if(chargingInfo[gun_index]->ConnectorPlugIn)
  8607. {
  8608. SetAcContactor(ON);
  8609. }
  8610. { // Idle 正常程序起點
  8611. // 判斷是否有啟用檢查插槍
  8612. if(IsConnectorAuthorizeSuccess(gun_index))
  8613. {
  8614. if(chargingInfo[gun_index]->IsAvailable &&
  8615. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  8616. {
  8617. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  8618. {
  8619. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  8620. }
  8621. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  8622. {
  8623. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  8624. }
  8625. setChargerMode(gun_index, MODE_AUTHORIZING);
  8626. continue;
  8627. }
  8628. }
  8629. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  8630. {
  8631. SetAcContactor(ON);
  8632. if(chargingInfo[gun_index]->IsAvailable &&
  8633. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  8634. chargingInfo[gun_index]->ConnectorPlugIn &&
  8635. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  8636. {
  8637. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  8638. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  8639. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  8640. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  8641. {
  8642. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  8643. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  8644. }
  8645. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  8646. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  8647. continue;
  8648. }
  8649. }
  8650. // only for test & debug purpose
  8651. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  8652. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  8653. {
  8654. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  8655. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  8656. LOG_INFO("Gun %d Start Force Charging%s",
  8657. gun_index + 1,
  8658. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  8659. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  8660. {
  8661. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  8662. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  8663. }
  8664. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  8665. {
  8666. LOG_INFO("Set Dispenser %d Inoperative",
  8667. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  8668. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  8669. }
  8670. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  8671. continue;
  8672. }
  8673. else
  8674. {
  8675. }
  8676. } // Idle 正常程序終點
  8677. }
  8678. }
  8679. break;
  8680. case S_AUTHORIZING:
  8681. if(isModeChange(gun_index))
  8682. {
  8683. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  8684. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8685. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  8686. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  8687. //StopSystemTimeoutDet();
  8688. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  8689. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  8690. SetAcContactor(ON);
  8691. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  8692. }
  8693. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  8694. {
  8695. FouceAnnounceAccountBalance(gun_index);
  8696. }
  8697. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  8698. {
  8699. // sync with dispenser
  8700. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  8701. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  8702. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  8703. FouceAnnounceAccountBalance(gun_index);
  8704. }
  8705. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  8706. {
  8707. // sync with dispenser
  8708. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  8709. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  8710. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  8711. }
  8712. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  8713. {
  8714. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  8715. {
  8716. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  8717. }
  8718. else
  8719. {
  8720. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  8721. }
  8722. setChargerMode(gun_index, MODE_IDLE);
  8723. }
  8724. else
  8725. {
  8726. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  8727. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  8728. {
  8729. AddPlugInTimes(gun_index);
  8730. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  8731. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  8732. GunAuthInitial(gun_index);
  8733. }
  8734. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  8735. {
  8736. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  8737. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  8738. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  8739. setChargerMode(gun_index, MODE_IDLE);
  8740. }
  8741. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  8742. {
  8743. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  8744. setChargerMode(gun_index, MODE_IDLE);
  8745. }
  8746. }
  8747. break;
  8748. case S_REASSIGN_CHECK:
  8749. {
  8750. if (isModeChange(gun_index))
  8751. {
  8752. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  8753. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8754. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  8755. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  8756. //StopSystemTimeoutDet();
  8757. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  8758. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  8759. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  8760. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  8761. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  8762. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  8763. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  8764. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  8765. SetAcContactor(ON);
  8766. SetPCabinetOutputRelay(gun_index, ON);
  8767. GetClockTime(&_SystemStatus_Time[gun_index]);
  8768. }
  8769. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8770. if(ConnectorRequestToCharging(gun_index))
  8771. {
  8772. setChargerMode(gun_index, MODE_REASSIGN);
  8773. }
  8774. else if(time >= WAIT_REASSIGN_TIME)
  8775. {
  8776. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  8777. {
  8778. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  8779. }
  8780. else
  8781. {
  8782. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  8783. }
  8784. setChargerMode(gun_index, MODE_IDLE);
  8785. }
  8786. }
  8787. break;
  8788. case S_REASSIGN:
  8789. {
  8790. if (isModeChange(gun_index))
  8791. {
  8792. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  8793. //gettimeofday(&_toAverage_time, NULL);
  8794. GetClockTime(&_SystemStatus_Time[gun_index]);
  8795. }
  8796. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8797. if(IsConnectorGroupingCompleted(gun_index))
  8798. {
  8799. setChargerMode(gun_index, MODE_PRECHARGE);
  8800. }
  8801. else if(time >= MAX_REQUEST_CHARGING_TIME)
  8802. {
  8803. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  8804. setChargerMode(gun_index, MODE_IDLE);
  8805. }
  8806. }
  8807. break;
  8808. case S_PREPARNING:
  8809. {
  8810. if (isModeChange(gun_index))
  8811. {
  8812. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  8813. //StopGunInfoTimeoutDet(gun_index);
  8814. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  8815. SetAcContactor(ON);
  8816. GetClockTime(&_SystemStatus_Time[gun_index]);
  8817. }
  8818. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8819. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  8820. {
  8821. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  8822. break;;
  8823. }
  8824. if (isEvBoardStopChargeFlag(gun_index) == YES)
  8825. {
  8826. // 板端或後臺要求停止
  8827. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  8828. Set_Ocpp_StopReason(gun_index, "Local");
  8829. ChargingAlarmProcess(gun_index);
  8830. }
  8831. else if(isNormalStopChargeFlag(gun_index) == YES)
  8832. {
  8833. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  8834. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8835. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8836. ChargingTerminalProcess(gun_index);
  8837. }
  8838. else if(isResetStopChargeFlag(gun_index) == YES)
  8839. {
  8840. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  8841. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8842. char *reason[16];
  8843. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8844. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8845. ChargingTerminalProcess(gun_index);
  8846. }
  8847. if(time > GUN_PREPARE_TIMEOUT)
  8848. {
  8849. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  8850. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  8851. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  8852. {
  8853. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  8854. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  8855. }
  8856. Set_Ocpp_StopReason(gun_index, "Local");
  8857. ChargingAlarmProcess(gun_index);
  8858. }
  8859. }
  8860. break;
  8861. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  8862. {
  8863. if (isModeChange(gun_index))
  8864. {
  8865. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  8866. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8867. //StopGunInfoTimeoutDet(gun_index);
  8868. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  8869. GetClockTime(&_SystemStatus_Time[gun_index]);
  8870. }
  8871. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8872. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  8873. {
  8874. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  8875. }
  8876. // only for test & debug purpose
  8877. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8878. {
  8879. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  8880. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  8881. }
  8882. if (isEvBoardStopChargeFlag(gun_index) == YES)
  8883. {
  8884. // 板端或後臺要求停止
  8885. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  8886. Set_Ocpp_StopReason(gun_index, "Local");
  8887. ChargingAlarmProcess(gun_index);
  8888. }
  8889. else if(isNormalStopChargeFlag(gun_index) == YES)
  8890. {
  8891. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  8892. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8893. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8894. ChargingTerminalProcess(gun_index);
  8895. }
  8896. else if(isResetStopChargeFlag(gun_index) == YES)
  8897. {
  8898. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  8899. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8900. char *reason[16];
  8901. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8902. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8903. ChargingTerminalProcess(gun_index);
  8904. }
  8905. if(time >= GUN_EV_WAIT_TIMEOUT)
  8906. {
  8907. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  8908. ChargingTerminalProcess(gun_index);
  8909. }
  8910. }
  8911. break;
  8912. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  8913. {
  8914. if (isModeChange(gun_index))
  8915. {
  8916. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  8917. GetClockTime(&_SystemStatus_Time[gun_index]);
  8918. #ifdef STARTTRANSATION_IMMEDIATELY
  8919. OcppStartTransaction(gun_index);
  8920. #endif
  8921. }
  8922. CheckoutStartChargingSoc(gun_index, NO);
  8923. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8924. // only for test & debug purpose
  8925. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8926. {
  8927. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  8928. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  8929. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  8930. {
  8931. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  8932. }
  8933. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  8934. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  8935. }
  8936. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  8937. {
  8938. setChargerMode(gun_index, MODE_CHARGING);
  8939. }
  8940. // only for test & debug purpose
  8941. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8942. {
  8943. if(time >= FORCE_CHARGING_WAIT_EVSE)
  8944. {
  8945. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  8946. setChargerMode(gun_index, MODE_CHARGING);
  8947. }
  8948. }
  8949. if (isEvBoardStopChargeFlag(gun_index) == YES)
  8950. {
  8951. // 板端或後臺要求停止
  8952. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  8953. Set_Ocpp_StopReason(gun_index, "Local");
  8954. ChargingAlarmProcess(gun_index);
  8955. }
  8956. else if(isNormalStopChargeFlag(gun_index) == YES)
  8957. {
  8958. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  8959. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8960. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8961. ChargingTerminalProcess(gun_index);
  8962. }
  8963. else if(isResetStopChargeFlag(gun_index) == YES)
  8964. {
  8965. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  8966. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8967. char *reason[16];
  8968. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8969. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8970. ChargingTerminalProcess(gun_index);
  8971. }
  8972. else if(isInvalidIdStopFlag(gun_index) == YES)
  8973. {
  8974. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  8975. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  8976. ChargingTerminalProcess(gun_index);
  8977. }
  8978. if(time >= GUN_EV_WAIT_TIMEOUT)
  8979. {
  8980. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  8981. ChargingTerminalProcess(gun_index);
  8982. }
  8983. }
  8984. break;
  8985. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  8986. {
  8987. if (isModeChange(gun_index))
  8988. {
  8989. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  8990. //StopGunInfoTimeoutDet(gun_index);
  8991. GetClockTime(&_SystemStatus_Time[gun_index]);
  8992. CheckoutStartChargingSoc(gun_index, YES);
  8993. chargingInfo[gun_index]->PresentChargedDuration = 0;
  8994. ChangeStartOrStopDateTime(YES, gun_index);
  8995. #ifndef STARTTRANSATION_IMMEDIATELY
  8996. OcppStartTransaction(gun_index);
  8997. #endif
  8998. }
  8999. // only for test & debug purpose
  9000. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  9001. {
  9002. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  9003. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  9004. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  9005. {
  9006. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  9007. }
  9008. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  9009. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  9010. }
  9011. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  9012. // 計算 Power
  9013. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  9014. chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  9015. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  9016. {
  9017. chargingInfo[gun_index]->PresentChargedDuration++;
  9018. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  9019. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  9020. {
  9021. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  9022. }
  9023. presentChargedEnergyUpdate(gun_index, changingPow);
  9024. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  9025. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changingPow;
  9026. }
  9027. CheckSessionTargetStop(gun_index);
  9028. if (isEvBoardStopChargeFlag(gun_index))
  9029. {
  9030. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  9031. Set_Ocpp_StopReason(gun_index, "Local");
  9032. ChargingAlarmProcess(gun_index);
  9033. }
  9034. else if(isNormalStopChargeFlag(gun_index) == YES)
  9035. {
  9036. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  9037. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  9038. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  9039. ChargingTerminalProcess(gun_index);
  9040. }
  9041. else if(isResetStopChargeFlag(gun_index) == YES)
  9042. {
  9043. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  9044. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  9045. char *reason[16];
  9046. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  9047. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  9048. ChargingTerminalProcess(gun_index);
  9049. }
  9050. else if(isInvalidIdStopFlag(gun_index) == YES)
  9051. {
  9052. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  9053. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  9054. ChargingTerminalProcess(gun_index);
  9055. }
  9056. else if(CheckBackendChargingTimeout(gun_index) ||
  9057. CheckBackendChargingEnergy(gun_index))
  9058. {
  9059. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  9060. // 板端或後臺要求停止
  9061. ChargingTerminalProcess(gun_index);
  9062. }
  9063. else if(isSessionTargetStopFlag(gun_index) == YES)
  9064. {
  9065. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  9066. Set_Ocpp_StopReason(gun_index, "Other");
  9067. ChargingTerminalProcess(gun_index);
  9068. }
  9069. }
  9070. break;
  9071. case S_ALARM:
  9072. case S_TERMINATING:
  9073. {
  9074. if (isModeChange(gun_index))
  9075. {
  9076. Set_Ocpp_StopReason(gun_index, "Other");
  9077. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  9078. {
  9079. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  9080. UpdateErrorCodeToOcpp(gun_index);
  9081. TheEndCharging(gun_index);
  9082. }
  9083. else
  9084. {
  9085. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  9086. }
  9087. //StopGunInfoTimeoutDet(gun_index);
  9088. GetClockTime(&_SystemStatus_Time[gun_index]);
  9089. }
  9090. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  9091. if(Is_DispenserStop(gun_index) || time >= WAIT_DISPENSER_STOP_TIMEOUT)
  9092. {
  9093. OcppStopTransaction(gun_index);
  9094. }
  9095. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  9096. {
  9097. if(time >= GUN_COMP_WAIT_TIMEOUT)
  9098. {
  9099. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  9100. {
  9101. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  9102. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  9103. }
  9104. }
  9105. if(IsAvailableBackToIdle(gun_index) == YES &&
  9106. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  9107. {
  9108. ReleaseAlarmCode(gun_index);
  9109. setChargerMode(gun_index, MODE_IDLE);
  9110. }
  9111. }
  9112. else
  9113. {
  9114. setChargerMode(gun_index, MODE_COMPLETE);
  9115. }
  9116. }
  9117. break;
  9118. case S_COMPLETE:
  9119. {
  9120. if (isModeChange(gun_index))
  9121. {
  9122. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  9123. TheEndCharging(gun_index);
  9124. GetClockTime(&_SystemStatus_Time[gun_index]);
  9125. }
  9126. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  9127. if(Is_DispenserStop(gun_index) || time >= WAIT_DISPENSER_STOP_TIMEOUT)
  9128. {
  9129. OcppStopTransaction(gun_index);
  9130. }
  9131. if(time >= GUN_COMP_WAIT_TIMEOUT)
  9132. {
  9133. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  9134. {
  9135. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  9136. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  9137. }
  9138. }
  9139. if(IsAvailableBackToIdle(gun_index) == YES &&
  9140. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  9141. {
  9142. setChargerMode(gun_index, MODE_IDLE);
  9143. }
  9144. }
  9145. break;
  9146. case S_CCS_PRECHARGE_ST0:
  9147. if (isModeChange(gun_index))
  9148. {
  9149. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  9150. //StopGunInfoTimeoutDet(gun_index);
  9151. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  9152. }
  9153. break;
  9154. case S_CCS_PRECHARGE_ST1:
  9155. if (isModeChange(gun_index))
  9156. {
  9157. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  9158. }
  9159. break;
  9160. case S_DEBUG:
  9161. if (isModeChange(gun_index))
  9162. {
  9163. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  9164. }
  9165. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  9166. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  9167. {
  9168. setChargerMode(gun_index, MODE_IDLE);
  9169. }
  9170. break;
  9171. }
  9172. }
  9173. TryFeedWatchdog();
  9174. usleep(whileLoopTime);
  9175. }
  9176. return FAIL;
  9177. }