main.c 389 KB

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