main.c 347 KB

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